|
5 | 5 |
|
6 | 6 | try:
|
7 | 7 | import time
|
| 8 | + import subprocess |
8 | 9 | from sonic_sfp.sfputilbase import SfpUtilBase
|
9 | 10 | except ImportError as e:
|
10 | 11 | raise ImportError("%s - required module not found" % str(e))
|
@@ -46,17 +47,105 @@ def __init__(self):
|
46 | 47 | SfpUtilBase.__init__(self)
|
47 | 48 |
|
48 | 49 | def get_presence(self, port_num):
|
| 50 | + # Check for invalid port_num |
| 51 | + if port_num < self.port_start or port_num > self.port_end: |
| 52 | + return False |
49 | 53 |
|
50 |
| - raise NotImplementedError |
| 54 | + try: |
| 55 | + reg_file = open("/bsp/qsfp/qsfp%d_status" % (port_num+1)) |
| 56 | + except IOError as e: |
| 57 | + print "Error: unable to open file: %s" % str(e) |
| 58 | + return False |
| 59 | + |
| 60 | + content = reg_file.readline().rstrip() |
| 61 | + |
| 62 | + # content is a string with the qsfp status |
| 63 | + if content == "good": |
| 64 | + return True |
| 65 | + |
| 66 | + return False |
51 | 67 |
|
52 | 68 | def get_low_power_mode(self, port_num):
|
| 69 | + # Check for invalid port_num |
| 70 | + if port_num < self.port_start or port_num > self.port_end: |
| 71 | + return False |
53 | 72 |
|
54 |
| - raise NotImplementedError |
| 73 | + lpm_cmd = "docker exec syncd python /usr/share/sonic/platform/plugins/sfplpmget.py {}".format(port_num) |
55 | 74 |
|
56 |
| - def set_low_power_mode(self, port_num, lpmode): |
| 75 | + try: |
| 76 | + output = subprocess.check_output(lpm_cmd, shell=True) |
| 77 | + if 'LPM ON' in output: |
| 78 | + return True |
| 79 | + except subprocess.CalledProcessError as e: |
| 80 | + print "Error! Unable to get LPM for {}, rc = {}, err msg: {}".format(port_num, e.returncode, e.output) |
| 81 | + return False |
57 | 82 |
|
58 |
| - raise NotImplementedError |
| 83 | + return False |
| 84 | + |
| 85 | + def set_low_power_mode(self, port_num, lpmode): |
| 86 | + # Check for invalid port_num |
| 87 | + if port_num < self.port_start or port_num > self.port_end: |
| 88 | + return False |
| 89 | + |
| 90 | + curr_lpmode = self.get_low_power_mode(port_num) |
| 91 | + if curr_lpmode == lpmode: |
| 92 | + return True |
| 93 | + |
| 94 | + lpm = 'on' if lpmode else 'off' |
| 95 | + lpm_cmd = "docker exec syncd python /usr/share/sonic/platform/plugins/sfplpmset.py {} {}".format(port_num, lpm) |
| 96 | + sfp_port_names = self.physical_to_logical[port_num] |
| 97 | + |
| 98 | + # Get port admin status |
| 99 | + try: |
| 100 | + enabled_ports = subprocess.check_output("ip link show up", shell=True) |
| 101 | + except subprocess.CalledProcessError as e: |
| 102 | + print "Error! Unable to get ports status, err msg: {}".format(e.output) |
| 103 | + return False |
| 104 | + |
| 105 | + port_to_disable = [] |
| 106 | + for port in sfp_port_names: |
| 107 | + if port in enabled_ports: |
| 108 | + port_to_disable.append(port) |
| 109 | + |
| 110 | + # Disable ports before LPM settings |
| 111 | + for port in port_to_disable: |
| 112 | + try: |
| 113 | + subprocess.check_output("ifconfig {} down".format(port), shell=True) |
| 114 | + except subprocess.CalledProcessError as e: |
| 115 | + print "Error! Unable to set admin status to DOWN for {}, rc = {}, err msg: {}".format(port, e.returncode, e.output) |
| 116 | + return False |
| 117 | + |
| 118 | + time.sleep(3) |
| 119 | + |
| 120 | + # Set LPM |
| 121 | + try: |
| 122 | + subprocess.check_output(lpm_cmd, shell=True) |
| 123 | + except subprocess.CalledProcessError as e: |
| 124 | + print "Error! Unable to set LPM for {}, rc = {}, err msg: {}".format(port_num, e.returncode, e.output) |
| 125 | + return False |
| 126 | + |
| 127 | + # Enable ports after LPM settings |
| 128 | + for port in port_to_disable: |
| 129 | + try: |
| 130 | + subprocess.check_output("ifconfig {} up".format(port), shell=True) |
| 131 | + except subprocess.CalledProcessError as e: |
| 132 | + print "Error! Unable to set admin status to UP for {}, rc = {}, err msg: {}".format(port, e.returncode, e.output) |
| 133 | + return False |
| 134 | + |
| 135 | + return True |
59 | 136 |
|
60 | 137 | def reset(self, port_num):
|
| 138 | + # Check for invalid port_num |
| 139 | + if port_num < self.port_start or port_num > self.port_end: |
| 140 | + return False |
| 141 | + |
| 142 | + lpm_cmd = "docker exec syncd python /usr/share/sonic/platform/plugins/sfpreset.py {}".format(port_num) |
| 143 | + |
| 144 | + try: |
| 145 | + subprocess.check_output(lpm_cmd, shell=True) |
| 146 | + return True |
| 147 | + except subprocess.CalledProcessError as e: |
| 148 | + print "Error! Unable to set LPM for {}, rc = {}, err msg: {}".format(port_num, e.returncode, e.output) |
| 149 | + return False |
61 | 150 |
|
62 |
| - raise NotImplementedError |
| 151 | + return False |
0 commit comments