|
7 | 7 |
|
8 | 8 | try:
|
9 | 9 | import os
|
| 10 | + import re |
10 | 11 | import sys
|
| 12 | + from collections import OrderedDict |
11 | 13 |
|
12 | 14 | import click
|
13 | 15 | from sonic_py_common import device_info, logger
|
| 16 | + from swsssdk import SonicV2Connector |
| 17 | + from tabulate import tabulate |
| 18 | + import utilities_common.cli as clicommon |
14 | 19 | except ImportError as e:
|
15 | 20 | raise ImportError("%s - required module not found" % str(e))
|
16 | 21 |
|
@@ -105,6 +110,168 @@ def show():
|
105 | 110 | click.echo("bus:dev.fn %s:%s.%s - dev_id=0x%s, %s" % (Bus, Dev, Fn, Id, Name))
|
106 | 111 |
|
107 | 112 |
|
| 113 | +# PCIe AER stats helpers |
| 114 | + |
| 115 | +aer_fields = { |
| 116 | + "correctable": ['RxErr', 'BadTLP', 'BadDLLP', 'Rollover', 'Timeout', 'NonFatalErr', 'CorrIntErr', 'HeaderOF', 'TOTAL_ERR_COR'], |
| 117 | + "fatal": ['Undefined', 'DLP', 'SDES', 'TLP', 'FCP', 'CmpltTO', 'CmpltAbrt', 'UnxCmplt', 'RxOF', 'MalfTLP', 'ECRC', 'UnsupReq', |
| 118 | + 'ACSViol', 'UncorrIntErr', 'BlockedTLP', 'AtomicOpBlocked', 'TLPBlockedErr', 'TOTAL_ERR_FATAL'], |
| 119 | + "non_fatal": ['Undefined', 'DLP', 'SDES', 'TLP', 'FCP', 'CmpltTO', 'CmpltAbrt', 'UnxCmplt', 'RxOF', 'MalfTLP', 'ECRC', 'UnsupReq', |
| 120 | + 'ACSViol', 'UncorrIntErr', 'BlockedTLP', 'AtomicOpBlocked', 'TLPBlockedErr', 'TOTAL_ERR_NONFATAL'] |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +class PcieDevice(click.ParamType): |
| 125 | + name = "<Bus>:<Dev>.<Fn>" |
| 126 | + |
| 127 | + def convert(self, value, param, ctx): |
| 128 | + match = re.match(r'([0-9A-Fa-f]{1,2}):([0-9A-Fa-f]{1,2})\.([0-9A-Fa-f])', value) |
| 129 | + |
| 130 | + if not match: |
| 131 | + self.fail('{} is not in <Bus>:<Dev>.<Fn> format'.format(value), param, ctx) |
| 132 | + |
| 133 | + Bus, Dev, Fn = [int(val, 16) for val in match.groups()] |
| 134 | + if Bus > 255: |
| 135 | + self.fail('Invalid Bus number', param, ctx) |
| 136 | + |
| 137 | + if Dev > 31: |
| 138 | + self.fail('Invalid Dev number', param, ctx) |
| 139 | + |
| 140 | + if Fn > 7: |
| 141 | + self.fail('Invalid Fn number', param, ctx) |
| 142 | + |
| 143 | + return "%02x:%02x.%d" % (Bus, Dev, Fn) |
| 144 | + |
| 145 | + |
| 146 | +_pcie_aer_click_options = [ |
| 147 | + click.Option(['-d', '--device', 'device_key'], |
| 148 | + type=PcieDevice(), |
| 149 | + help="Display stats only for the specified device"), |
| 150 | + click.Option(['-v', '--verbose'], |
| 151 | + is_flag=True, |
| 152 | + help="Display all stats") |
| 153 | +] |
| 154 | + |
| 155 | + |
| 156 | +class PcieAerCommand(click.Command): |
| 157 | + '''This subclass of click.Command provides common options, help |
| 158 | + and short help text for PCIe AER commands''' |
| 159 | + |
| 160 | + def __init__(self, *args, **kwargs): |
| 161 | + super(PcieAerCommand, self).__init__(*args, **kwargs) |
| 162 | + self.params = _pcie_aer_click_options |
| 163 | + |
| 164 | + def format_help_text(self, ctx, formatter): |
| 165 | + formatter.write_paragraph() |
| 166 | + with formatter.indentation(): |
| 167 | + formatter.write_text("Show {} PCIe AER attributes".format(self.name.replace("_", "-"))) |
| 168 | + formatter.write_text("(Default: Display only non-zero attributes)") |
| 169 | + |
| 170 | + def get_short_help_str(self, limit): |
| 171 | + return "Show {} PCIe AER attributes".format(self.name.replace("_", "-")) |
| 172 | + |
| 173 | + |
| 174 | +def pcie_aer_display(ctx, severity): |
| 175 | + device_key = ctx.params['device_key'] |
| 176 | + no_zero = not ctx.params['verbose'] |
| 177 | + header = ["AER - " + severity.upper().replace("_", "")] |
| 178 | + fields = aer_fields[severity] |
| 179 | + pcie_dev_list = list() |
| 180 | + dev_found = False |
| 181 | + |
| 182 | + statedb = SonicV2Connector() |
| 183 | + statedb.connect(statedb.STATE_DB) |
| 184 | + |
| 185 | + table = OrderedDict() |
| 186 | + for field in fields: |
| 187 | + table[field] = [field] |
| 188 | + |
| 189 | + if device_key: |
| 190 | + pcie_dev_list = ["PCIE_DEVICE|%s" % device_key] |
| 191 | + else: |
| 192 | + keys = statedb.keys(statedb.STATE_DB, "PCIE_DEVICE|*") |
| 193 | + if keys: |
| 194 | + pcie_dev_list = sorted(keys) |
| 195 | + |
| 196 | + for pcie_dev_key in pcie_dev_list: |
| 197 | + aer_attribute = statedb.get_all(statedb.STATE_DB, pcie_dev_key) |
| 198 | + if not aer_attribute: |
| 199 | + continue |
| 200 | + |
| 201 | + if device_key: |
| 202 | + dev_found = True |
| 203 | + |
| 204 | + if no_zero and all(val == '0' for key, val in aer_attribute.items() if key.startswith(severity)): |
| 205 | + continue |
| 206 | + |
| 207 | + pcie_dev = pcie_dev_key.split("|")[1] |
| 208 | + Id = aer_attribute['id'] |
| 209 | + |
| 210 | + # Tabulate Header |
| 211 | + device_name = "%s\n%s" % (pcie_dev, Id) |
| 212 | + header.append(device_name) |
| 213 | + |
| 214 | + # Tabulate Row |
| 215 | + for field in fields: |
| 216 | + key = severity + "|" + field |
| 217 | + table[field].append(aer_attribute.get(key, 'NA')) |
| 218 | + |
| 219 | + if device_key and not dev_found: |
| 220 | + ctx.exit("Device not found in DB") |
| 221 | + |
| 222 | + # Strip fields with no non-zero value |
| 223 | + if no_zero: |
| 224 | + for field in fields: |
| 225 | + if all(val == '0' for val in table[field][1:]): |
| 226 | + del table[field] |
| 227 | + |
| 228 | + if not (no_zero and (len(header) == 1)): |
| 229 | + if ctx.obj: |
| 230 | + click.echo("") |
| 231 | + |
| 232 | + click.echo(tabulate(list(table.values()), header, tablefmt="grid")) |
| 233 | + ctx.obj = True |
| 234 | + |
| 235 | + |
| 236 | +# Show PCIe AER status |
| 237 | +@cli.group(cls=clicommon.AliasedGroup) |
| 238 | +@click.pass_context |
| 239 | +def pcie_aer(ctx): |
| 240 | + '''Display PCIe AER status''' |
| 241 | + # Set True to insert a line between severities in 'all' context |
| 242 | + ctx.obj = False |
| 243 | + |
| 244 | + |
| 245 | +@pcie_aer.command(cls=PcieAerCommand) |
| 246 | +@click.pass_context |
| 247 | +def correctable(ctx, device_key, verbose): |
| 248 | + '''Show correctable PCIe AER attributes''' |
| 249 | + pcie_aer_display(ctx, "correctable") |
| 250 | + |
| 251 | + |
| 252 | +@pcie_aer.command(cls=PcieAerCommand) |
| 253 | +@click.pass_context |
| 254 | +def fatal(ctx, device_key, verbose): |
| 255 | + '''Show fatal PCIe AER attributes''' |
| 256 | + pcie_aer_display(ctx, "fatal") |
| 257 | + |
| 258 | + |
| 259 | +@pcie_aer.command(cls=PcieAerCommand) |
| 260 | +@click.pass_context |
| 261 | +def non_fatal(ctx, device_key, verbose): |
| 262 | + '''Show non-fatal PCIe AER attributes''' |
| 263 | + pcie_aer_display(ctx, "non_fatal") |
| 264 | + |
| 265 | + |
| 266 | +@pcie_aer.command(name='all', cls=PcieAerCommand) |
| 267 | +@click.pass_context |
| 268 | +def all_errors(ctx, device_key, verbose): |
| 269 | + '''Show all PCIe AER attributes''' |
| 270 | + pcie_aer_display(ctx, "correctable") |
| 271 | + pcie_aer_display(ctx, "fatal") |
| 272 | + pcie_aer_display(ctx, "non_fatal") |
| 273 | + |
| 274 | + |
108 | 275 | # Show PCIE Vender ID and Device ID
|
109 | 276 | @cli.command()
|
110 | 277 | def check():
|
|
0 commit comments