|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Script to generate PFC storm. |
| 5 | +
|
| 6 | +""" |
| 7 | +import sys |
| 8 | +import optparse |
| 9 | +import logging |
| 10 | +import logging.handlers |
| 11 | +import re |
| 12 | +import signal |
| 13 | +import subprocess |
| 14 | +import time |
| 15 | + |
| 16 | + |
| 17 | +logger = logging.getLogger('MyLogger') |
| 18 | +logger.setLevel(logging.DEBUG) |
| 19 | + |
| 20 | + |
| 21 | +class SignalCleanup(): |
| 22 | + def __init__(self, fanoutPfcStorm, endMsg): |
| 23 | + self.fanoutPfcStorm = fanoutPfcStorm |
| 24 | + self.endMsg = endMsg |
| 25 | + signal.signal(signal.SIGTERM, self.sigHandler) |
| 26 | + |
| 27 | + def sigHandler(self, *args): |
| 28 | + self.fanoutPfcStorm.endAllPfcStorm() |
| 29 | + |
| 30 | + logger.debug(self.endMsg) |
| 31 | + sys.exit(0) |
| 32 | + |
| 33 | + |
| 34 | +class FanoutPfcStorm(): |
| 35 | + ''' |
| 36 | + For eos this class expects all interfaces to be in the front panel interface format |
| 37 | + ex. Ethernet1/1 and not et1_1 |
| 38 | + For sonic, the interfaces are the default format |
| 39 | + ''' |
| 40 | + def __init__(self, priority, chipName, os): |
| 41 | + self.intfsEnabled = [] |
| 42 | + self.priority = priority |
| 43 | + self.switchChip = chipName |
| 44 | + self.os = os |
| 45 | + if os == 'sonic': |
| 46 | + self.intfToMmuPort, self.intfToPort = self._parseInterfaceMapFullSonic() |
| 47 | + else: |
| 48 | + self.intfToMmuPort, self.intfToPort = self._parseInterfaceMapFull() |
| 49 | + |
| 50 | + def _shellCmd(self, cmd): |
| 51 | + output = "" |
| 52 | + result = subprocess.run([f"{cmd}"], capture_output=True, text=True, shell=True) |
| 53 | + if result.returncode == 0: |
| 54 | + output = result.stdout |
| 55 | + return output |
| 56 | + |
| 57 | + def _cliCmd(self, cmd): |
| 58 | + output = "" |
| 59 | + if self.os == 'sonic': |
| 60 | + result = subprocess.run([f"bcmcmd '{cmd}'"], capture_output=True, text=True, shell=True) |
| 61 | + else: |
| 62 | + result = subprocess.run( |
| 63 | + ["Cli", "-c", f"{cmd}"], capture_output=True, text=True) |
| 64 | + if result.returncode == 0: |
| 65 | + output = result.stdout |
| 66 | + return output |
| 67 | + |
| 68 | + def _bcmltshellCmd(self, cmd): |
| 69 | + if self.os == 'sonic': |
| 70 | + return self._cliCmd(f"bsh -c \"{cmd}\"") |
| 71 | + else: |
| 72 | + return self._cliCmd(f"en\nplatform trident shell\nbcmltshell\n{cmd}") |
| 73 | + |
| 74 | + def _bcmshellCmd(self, cmd): |
| 75 | + return self._cliCmd(f"en\nplatform trident shell\n{cmd}") |
| 76 | + |
| 77 | + def _parseInterfaceMapFull(self): |
| 78 | + intfToMmuPort = {} |
| 79 | + intfToPort = {} |
| 80 | + |
| 81 | + output = self._cliCmd("en\nshow platform trident interface map full") |
| 82 | + |
| 83 | + for line in output.splitlines(): |
| 84 | + mo = re.search( |
| 85 | + r'Intf: (?P<intf>Ethernet\S+).{1,50}Port: {1,3}(?P<port>\S+)' |
| 86 | + r'.{1,100}P2M\[ {0,3}\d+\]: {1,3}(?P<mmu>\S+)', |
| 87 | + line |
| 88 | + ) |
| 89 | + if mo is None: |
| 90 | + continue |
| 91 | + intfToMmuPort[mo.group('intf')] = mo.group('mmu') |
| 92 | + intfToPort[mo.group('intf')] = mo.group('port') |
| 93 | + |
| 94 | + return intfToMmuPort, intfToPort |
| 95 | + |
| 96 | + def _parseInterfaceMapFullSonic(self): |
| 97 | + intfToMmuPort = {} |
| 98 | + intfTolPort = {} |
| 99 | + lPortToIntf = {} |
| 100 | + |
| 101 | + if self.switchChip.startswith(("Tomahawk5", "Tomahawk4")): |
| 102 | + output = self._bcmltshellCmd('knet netif info') |
| 103 | + for info in output.split("Network interface Info:"): |
| 104 | + mo = re.search(r"Name: (?P<intf>Ethernet\d+)[\s\S]{1,100}Port: (?P<lport>\d+)", info) |
| 105 | + if mo is None: |
| 106 | + continue |
| 107 | + lPortToIntf[mo.group('lport')] = mo.group('intf') |
| 108 | + intfTolPort[mo.group('intf')] = mo.group('lport') |
| 109 | + output = self._cliCmd("show portmap") |
| 110 | + for line in output.splitlines(): |
| 111 | + entries = line.split() |
| 112 | + if len(entries) == 7: |
| 113 | + lport = entries[2] |
| 114 | + mmuPort = entries[4] |
| 115 | + if lport in lPortToIntf: |
| 116 | + intfToMmuPort[lPortToIntf[lport]] = mmuPort |
| 117 | + intfTolPort[mo.group('intf')] = mo.group('lport') |
| 118 | + else: |
| 119 | + output = self._cliCmd('knet netif show') |
| 120 | + for info in output.split("Interface ID"): |
| 121 | + mo = re.search(r"name=(?P<intf>Ethernet\d+)[\s\S]{1,100}port=(?P<lport>\S+)", info) |
| 122 | + if mo is None: |
| 123 | + continue |
| 124 | + lPortToIntf[mo.group('lport')] = mo.group('intf') |
| 125 | + intfTolPort[mo.group('intf')] = mo.group('lport') |
| 126 | + output = self._cliCmd("show portmap") |
| 127 | + for line in output.splitlines(): |
| 128 | + entries = line.split() |
| 129 | + if len(entries) == 9: |
| 130 | + lport = entries[0] |
| 131 | + mmuPort = entries[4] |
| 132 | + if lport in lPortToIntf: |
| 133 | + intfToMmuPort[lPortToIntf[lport]] = mmuPort |
| 134 | + intfTolPort[mo.group('intf')] = mo.group('lport') |
| 135 | + |
| 136 | + return intfToMmuPort, intfTolPort |
| 137 | + |
| 138 | + def _endPfcStorm(self, intf): |
| 139 | + ''' |
| 140 | + Intf format is Ethernet1/1 |
| 141 | +
|
| 142 | + The users of this class are only expected to call |
| 143 | + startPfcStorm and endAllPfcStorm |
| 144 | + ''' |
| 145 | + mmuPort = self.intfToMmuPort[intf] |
| 146 | + port = self.intfToPort[intf] |
| 147 | + if self.switchChip.startswith(("Tomahawk5", "Tomahawk4")): |
| 148 | + self._bcmltshellCmd(f"pt MMU_INTFO_XPORT_BKP_HW_UPDATE_DISr set BCMLT_PT_PORT={mmuPort} PAUSE_PFC_BKP=0") |
| 149 | + self._bcmltshellCmd(f"pt MMU_INTFO_TO_XPORT_BKPr set BCMLT_PT_PORT={mmuPort} PAUSE_PFC_BKP=0") |
| 150 | + else: |
| 151 | + self._bcmshellCmd(f"setreg CHFC2PFC_STATE.{port} PRI_BKP=0") |
| 152 | + self._cliCmd(f"en\nconf\n\nint {intf}\nno priority-flow-control on") |
| 153 | + if self.os == 'sonic': |
| 154 | + for prio in range(8): |
| 155 | + self._cliCmd(f"config interface pfc priority {intf} {prio} off") |
| 156 | + else: |
| 157 | + for prio in range(8): |
| 158 | + self._cliCmd(f"en\nconf\n\nint {intf}\nno priority-flow-control priority {prio} no-drop") |
| 159 | + |
| 160 | + def startPfcStorm(self, intf): |
| 161 | + if intf in self.intfsEnabled: |
| 162 | + return |
| 163 | + self.intfsEnabled.append(intf) |
| 164 | + |
| 165 | + mmuPort = self.intfToMmuPort[intf] |
| 166 | + port = self.intfToPort[intf] |
| 167 | + if self.os == 'sonic': |
| 168 | + for prio in range(8): |
| 169 | + if (1 << prio) & self.priority: |
| 170 | + self._shellCmd(f"config interface pfc priority {intf} {prio} on") |
| 171 | + else: |
| 172 | + self._cliCmd(f"en\nconf\n\nint {intf}\npriority-flow-control on") |
| 173 | + for prio in range(8): |
| 174 | + if (1 << prio) & self.priority: |
| 175 | + self._cliCmd(f"en\nconf\n\nint {intf}\npriority-flow-control priority {prio} no-drop") |
| 176 | + |
| 177 | + if self.switchChip.startswith(("Tomahawk5", "Tomahawk4")): |
| 178 | + self._bcmltshellCmd(f"pt MMU_INTFO_XPORT_BKP_HW_UPDATE_DISr set BCMLT_PT_PORT={mmuPort} PAUSE_PFC_BKP=1") |
| 179 | + self._bcmltshellCmd(f"pt MMU_INTFO_TO_XPORT_BKPr set BCMLT_PT_PORT={mmuPort} PAUSE_PFC_BKP={self.priority}") |
| 180 | + else: |
| 181 | + self._bcmshellCmd(f"setreg CHFC2PFC_STATE.{port} PRI_BKP={self.priority}") |
| 182 | + |
| 183 | + def endAllPfcStorm(self): |
| 184 | + for intf in self.intfsEnabled: |
| 185 | + self._endPfcStorm(intf) |
| 186 | + |
| 187 | + |
| 188 | +def main(): |
| 189 | + usage = "usage: %prog [options] arg1 arg2" |
| 190 | + parser = optparse.OptionParser(usage=usage) |
| 191 | + parser.add_option("-i", "--interface", type="string", dest="interface", |
| 192 | + help="Interface list to send packets, separated by ','", metavar="Interface") |
| 193 | + parser.add_option('-p', "--priority", type="int", dest="priority", |
| 194 | + help="PFC class enable bitmap.", metavar="Priority", default=-1) |
| 195 | + parser.add_option("-r", "--rsyslog-server", type="string", dest="rsyslog_server", |
| 196 | + default="127.0.0.1", help="Rsyslog server IPv4 address", metavar="IPAddress") |
| 197 | + parser.add_option("-c", "--chipName", type="string", dest="chipName", metavar="ChipName", |
| 198 | + help="Name of chip in the switch, i.e. Tomahawk5") |
| 199 | + parser.add_option("-o", "--os", type="string", dest="os", |
| 200 | + help="Operating system (eos or sonic)", default="eos") |
| 201 | + |
| 202 | + (options, args) = parser.parse_args() |
| 203 | + |
| 204 | + if options.interface is None: |
| 205 | + print("Need to specify the interface to send PFC pause frame packets.") |
| 206 | + parser.print_help() |
| 207 | + sys.exit(1) |
| 208 | + |
| 209 | + if options.chipName is None: |
| 210 | + print("Need to specify the ChipName to determine what cli to generate PFC pause frame packets.") |
| 211 | + parser.print_help() |
| 212 | + sys.exit(1) |
| 213 | + |
| 214 | + if options.priority > 255 or options.priority < 0: |
| 215 | + print("Enable class bitmap is not valid. Need to be in range 0-255.") |
| 216 | + parser.print_help() |
| 217 | + sys.exit(1) |
| 218 | + |
| 219 | + # Configure logging |
| 220 | + handler = logging.handlers.SysLogHandler(address=(options.rsyslog_server, 514)) |
| 221 | + logger.addHandler(handler) |
| 222 | + |
| 223 | + # List of front panel kernel intfs |
| 224 | + interfaces = options.interface.split(',') |
| 225 | + |
| 226 | + fs = FanoutPfcStorm(options.priority, options.chipName, options.os) |
| 227 | + SignalCleanup(fs, 'PFC_STORM_END') |
| 228 | + |
| 229 | + logger.debug('PFC_STORM_DEBUG') |
| 230 | + for intf in interfaces: |
| 231 | + if options.os == 'eos': |
| 232 | + intf = frontPanelIntfFromKernelIntfName(intf) |
| 233 | + fs.startPfcStorm(intf) |
| 234 | + logger.debug('PFC_STORM_START') |
| 235 | + |
| 236 | + # wait forever until stop |
| 237 | + while True: |
| 238 | + time.sleep(100) |
| 239 | + |
| 240 | + |
| 241 | +def frontPanelIntfFromKernelIntfName(intf): |
| 242 | + return intf.replace("et", "Ethernet").replace("_", "/") |
| 243 | + |
| 244 | + |
| 245 | +if __name__ == "__main__": |
| 246 | + main() |
0 commit comments