2 # SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
12 sys.path.append(pathlib.Path(__file__).resolve().parent.as_posix())
13 from lib import YnlFamily
14 from cli import schema_dir, spec_dir
16 def args_to_req(ynl, op_name, args, req):
18 Verify and convert command-line arguments to the ynl-compatible request.
20 valid_attrs = ynl.operation_do_attributes(op_name)
21 valid_attrs.remove('header') # not user-provided
24 print(f'no attributes, expected: {valid_attrs}')
30 if i + 1 >= len(args):
31 print(f'expected value for \'{attr}\'')
34 if attr not in valid_attrs:
35 print(f'invalid attribute \'{attr}\', expected: {valid_attrs}')
43 def print_field(reply, *desc):
45 Pretty-print a set of fields from the reply. desc specifies the
46 fields and the optional type (bool/yn).
49 return print_field(reply, *zip(reply.keys(), reply.keys()))
53 field, name, tp = spec
58 value = reply.get(field, None)
60 value = 'yes' if value else 'no'
61 elif tp == 'bool' or isinstance(value, bool):
62 value = 'on' if value else 'off'
64 value = 'n/a' if value is None else value
66 print(f'{name}: {value}')
68 def print_speed(name, value):
70 Print out the speed-like strings from the value dict.
72 speed_re = re.compile(r'[0-9]+base[^/]+/.+')
73 speed = [ k for k, v in value.items() if v and speed_re.match(k) ]
74 print(f'{name}: {" ".join(speed)}')
76 def doit(ynl, args, op_name):
78 Prepare request header, parse arguments and doit.
82 'dev-name': args.device,
86 args_to_req(ynl, op_name, args.args, req)
89 def dumpit(ynl, args, op_name, extra = {}):
91 Prepare request header, parse arguments and dumpit (filtering out the
92 devices we're not interested in).
94 reply = ynl.dump(op_name, { 'header': {} } | extra)
99 if msg['header']['dev-name'] == args.device:
101 pprint.PrettyPrinter().pprint(msg)
103 msg.pop('header', None)
106 print(f"Not supported for device {args.device}")
109 def bits_to_dict(attr):
111 Convert ynl-formatted bitmask to a dict of bit=value.
114 if 'bits' not in attr:
116 if 'bit' not in attr['bits']:
118 for bit in attr['bits']['bit']:
119 if bit['name'] == '':
122 value = bit.get('value', False)
127 parser = argparse.ArgumentParser(description='ethtool wannabe')
128 parser.add_argument('--json', action=argparse.BooleanOptionalAction)
129 parser.add_argument('--show-priv-flags', action=argparse.BooleanOptionalAction)
130 parser.add_argument('--set-priv-flags', action=argparse.BooleanOptionalAction)
131 parser.add_argument('--show-eee', action=argparse.BooleanOptionalAction)
132 parser.add_argument('--set-eee', action=argparse.BooleanOptionalAction)
133 parser.add_argument('-a', '--show-pause', action=argparse.BooleanOptionalAction)
134 parser.add_argument('-A', '--set-pause', action=argparse.BooleanOptionalAction)
135 parser.add_argument('-c', '--show-coalesce', action=argparse.BooleanOptionalAction)
136 parser.add_argument('-C', '--set-coalesce', action=argparse.BooleanOptionalAction)
137 parser.add_argument('-g', '--show-ring', action=argparse.BooleanOptionalAction)
138 parser.add_argument('-G', '--set-ring', action=argparse.BooleanOptionalAction)
139 parser.add_argument('-k', '--show-features', action=argparse.BooleanOptionalAction)
140 parser.add_argument('-K', '--set-features', action=argparse.BooleanOptionalAction)
141 parser.add_argument('-l', '--show-channels', action=argparse.BooleanOptionalAction)
142 parser.add_argument('-L', '--set-channels', action=argparse.BooleanOptionalAction)
143 parser.add_argument('-T', '--show-time-stamping', action=argparse.BooleanOptionalAction)
144 parser.add_argument('-S', '--statistics', action=argparse.BooleanOptionalAction)
145 # TODO: --show-tunnels tunnel-info-get
146 # TODO: --show-module module-get
147 # TODO: --get-plca-cfg plca-get
148 # TODO: --get-plca-status plca-get-status
149 # TODO: --show-mm mm-get
150 # TODO: --show-fec fec-get
151 # TODO: --dump-module-eerpom module-eeprom-get
154 parser.add_argument('device', metavar='device', type=str)
155 parser.add_argument('args', metavar='args', type=str, nargs='*')
157 args = parser.parse_args()
159 script_abs_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
160 spec = os.path.join(spec_dir(), 'ethtool.yaml')
161 schema = os.path.join(schema_dir(), 'genetlink-legacy.yaml')
163 ynl = YnlFamily(spec, schema)
165 if args.set_priv_flags:
166 # TODO: parse the bitmask
167 print("not implemented")
171 return doit(ynl, args, 'eee-set')
174 return doit(ynl, args, 'pause-set')
176 if args.set_coalesce:
177 return doit(ynl, args, 'coalesce-set')
179 if args.set_features:
180 # TODO: parse the bitmask
181 print("not implemented")
184 if args.set_channels:
185 return doit(ynl, args, 'channels-set')
188 return doit(ynl, args, 'rings-set')
190 if args.show_priv_flags:
191 flags = bits_to_dict(dumpit(ynl, args, 'privflags-get')['flags'])
196 eee = dumpit(ynl, args, 'eee-get')
197 ours = bits_to_dict(eee['modes-ours'])
198 peer = bits_to_dict(eee['modes-peer'])
201 status = 'enabled' if eee['enabled'] else 'disabled'
202 if 'active' in eee and eee['active']:
203 status = status + ' - active'
205 status = status + ' - inactive'
207 status = 'not supported'
209 print(f'EEE status: {status}')
210 print_field(eee, ('tx-lpi-timer', 'Tx LPI'))
211 print_speed('Advertised EEE link modes', ours)
212 print_speed('Link partner advertised EEE link modes', peer)
217 print_field(dumpit(ynl, args, 'pause-get'),
218 ('autoneg', 'Autonegotiate', 'bool'),
219 ('rx', 'RX', 'bool'),
220 ('tx', 'TX', 'bool'))
223 if args.show_coalesce:
224 print_field(dumpit(ynl, args, 'coalesce-get'))
227 if args.show_features:
228 reply = dumpit(ynl, args, 'features-get')
229 available = bits_to_dict(reply['hw'])
230 requested = bits_to_dict(reply['wanted']).keys()
231 active = bits_to_dict(reply['active']).keys()
232 never_changed = bits_to_dict(reply['nochange']).keys()
234 for f in sorted(available):
240 if f not in available or f in never_changed:
246 req = " [requested on]"
248 req = " [requested off]"
250 print(f'{f}: {value}{fixed}{req}')
254 if args.show_channels:
255 reply = dumpit(ynl, args, 'channels-get')
256 print(f'Channel parameters for {args.device}:')
258 print(f'Pre-set maximums:')
262 ('other-max', 'Other'),
263 ('combined-max', 'Combined'))
265 print(f'Current hardware settings:')
269 ('other-count', 'Other'),
270 ('combined-count', 'Combined'))
275 reply = dumpit(ynl, args, 'channels-get')
277 print(f'Ring parameters for {args.device}:')
279 print(f'Pre-set maximums:')
282 ('rx-mini-max', 'RX Mini'),
283 ('rx-jumbo-max', 'RX Jumbo'),
286 print(f'Current hardware settings:')
289 ('rx-mini', 'RX Mini'),
290 ('rx-jumbo', 'RX Jumbo'),
294 ('rx-buf-len', 'RX Buf Len'),
295 ('cqe-size', 'CQE Size'),
296 ('tx-push', 'TX Push', 'bool'))
301 print(f'NIC statistics:')
304 strset = dumpit(ynl, args, 'strset-get')
305 pprint.PrettyPrinter().pprint(strset)
312 # TODO: support passing the bitmask
314 #{ 'name': 'eth-phy', 'value': True },
315 { 'name': 'eth-mac', 'value': True },
316 #{ 'name': 'eth-ctrl', 'value': True },
317 #{ 'name': 'rmon', 'value': True },
323 rsp = dumpit(ynl, args, 'stats-get', req)
324 pprint.PrettyPrinter().pprint(rsp)
327 if args.show_time_stamping:
334 tsinfo = dumpit(ynl, args, 'tsinfo-get', req)
336 print(f'Time stamping parameters for {args.device}:')
338 print('Capabilities:')
339 [print(f'\t{v}') for v in bits_to_dict(tsinfo['timestamping'])]
341 print(f'PTP Hardware Clock: {tsinfo["phc-index"]}')
343 print('Hardware Transmit Timestamp Modes:')
344 [print(f'\t{v}') for v in bits_to_dict(tsinfo['tx-types'])]
346 print('Hardware Receive Filter Modes:')
347 [print(f'\t{v}') for v in bits_to_dict(tsinfo['rx-filters'])]
350 [print(f'\t{k}: {v}') for k, v in tsinfo['stats'].items()]
353 print(f'Settings for {args.device}:')
354 linkmodes = dumpit(ynl, args, 'linkmodes-get')
355 ours = bits_to_dict(linkmodes['ours'])
357 supported_ports = ('TP', 'AUI', 'BNC', 'MII', 'FIBRE', 'Backplane')
358 ports = [ p for p in supported_ports if ours.get(p, False)]
359 print(f'Supported ports: [ {" ".join(ports)} ]')
361 print_speed('Supported link modes', ours)
363 print_field(ours, ('Pause', 'Supported pause frame use', 'yn'))
364 print_field(ours, ('Autoneg', 'Supports auto-negotiation', 'yn'))
366 supported_fec = ('None', 'PS', 'BASER', 'LLRS')
367 fec = [ p for p in supported_fec if ours.get(p, False)]
368 fec_str = " ".join(fec)
370 fec_str = "Not reported"
372 print(f'Supported FEC modes: {fec_str}')
375 if linkmodes['speed'] > 0 and linkmodes['speed'] < 0xffffffff:
376 speed = f'{linkmodes["speed"]}Mb/s'
377 print(f'Speed: {speed}')
383 duplex = duplex_modes.get(linkmodes["duplex"], None)
385 duplex = f'Unknown! ({linkmodes["duplex"]})'
386 print(f'Duplex: {duplex}')
389 if linkmodes.get("autoneg", 0) != 0:
391 print(f'Auto-negotiation: {autoneg}')
399 5: 'Directly Attached Copper',
402 linkinfo = dumpit(ynl, args, 'linkinfo-get')
403 print(f'Port: {ports.get(linkinfo["port"], "Other")}')
405 print_field(linkinfo, ('phyaddr', 'PHYAD'))
411 print(f'Transceiver: {transceiver.get(linkinfo["transceiver"], "Unknown")}')
417 mdix = mdix_ctrl.get(linkinfo['tp-mdix-ctrl'], None)
419 mdix = mdix + ' (forced)'
421 mdix = mdix_ctrl.get(linkinfo['tp-mdix'], 'Unknown (auto)')
422 print(f'MDI-X: {mdix}')
424 debug = dumpit(ynl, args, 'debug-get')
425 msgmask = bits_to_dict(debug.get("msgmask", [])).keys()
426 print(f'Current message level: {" ".join(msgmask)}')
428 linkstate = dumpit(ynl, args, 'linkstate-get')
434 detected = detected_states.get(linkstate['link'], 'unknown')
435 print(f'Link detected: {detected}')
437 if __name__ == '__main__':