2 # SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
10 from lib import YnlFamily
12 def args_to_req(ynl, op_name, args, req):
14 Verify and convert command-line arguments to the ynl-compatible request.
16 valid_attrs = ynl.operation_do_attributes(op_name)
17 valid_attrs.remove('header') # not user-provided
20 print(f'no attributes, expected: {valid_attrs}')
26 if i + 1 >= len(args):
27 print(f'expected value for \'{attr}\'')
30 if attr not in valid_attrs:
31 print(f'invalid attribute \'{attr}\', expected: {valid_attrs}')
39 def print_field(reply, *desc):
41 Pretty-print a set of fields from the reply. desc specifies the
42 fields and the optional type (bool/yn).
45 return print_field(reply, *zip(reply.keys(), reply.keys()))
49 field, name, tp = spec
54 value = reply.get(field, None)
56 value = 'yes' if value else 'no'
57 elif tp == 'bool' or isinstance(value, bool):
58 value = 'on' if value else 'off'
60 value = 'n/a' if value is None else value
62 print(f'{name}: {value}')
64 def print_speed(name, value):
66 Print out the speed-like strings from the value dict.
68 speed_re = re.compile(r'[0-9]+base[^/]+/.+')
69 speed = [ k for k, v in value.items() if v and speed_re.match(k) ]
70 print(f'{name}: {" ".join(speed)}')
72 def doit(ynl, args, op_name):
74 Prepare request header, parse arguments and doit.
78 'dev-name': args.device,
82 args_to_req(ynl, op_name, args.args, req)
85 def dumpit(ynl, args, op_name, extra = {}):
87 Prepare request header, parse arguments and dumpit (filtering out the
88 devices we're not interested in).
90 reply = ynl.dump(op_name, { 'header': {} } | extra)
95 if msg['header']['dev-name'] == args.device:
97 pprint.PrettyPrinter().pprint(msg)
99 msg.pop('header', None)
102 print(f"Not supported for device {args.device}")
105 def bits_to_dict(attr):
107 Convert ynl-formatted bitmask to a dict of bit=value.
110 if 'bits' not in attr:
112 if 'bit' not in attr['bits']:
114 for bit in attr['bits']['bit']:
115 if bit['name'] == '':
118 value = bit.get('value', False)
123 parser = argparse.ArgumentParser(description='ethtool wannabe')
124 parser.add_argument('--json', action=argparse.BooleanOptionalAction)
125 parser.add_argument('--show-priv-flags', action=argparse.BooleanOptionalAction)
126 parser.add_argument('--set-priv-flags', action=argparse.BooleanOptionalAction)
127 parser.add_argument('--show-eee', action=argparse.BooleanOptionalAction)
128 parser.add_argument('--set-eee', action=argparse.BooleanOptionalAction)
129 parser.add_argument('-a', '--show-pause', action=argparse.BooleanOptionalAction)
130 parser.add_argument('-A', '--set-pause', action=argparse.BooleanOptionalAction)
131 parser.add_argument('-c', '--show-coalesce', action=argparse.BooleanOptionalAction)
132 parser.add_argument('-C', '--set-coalesce', action=argparse.BooleanOptionalAction)
133 parser.add_argument('-g', '--show-ring', action=argparse.BooleanOptionalAction)
134 parser.add_argument('-G', '--set-ring', action=argparse.BooleanOptionalAction)
135 parser.add_argument('-k', '--show-features', action=argparse.BooleanOptionalAction)
136 parser.add_argument('-K', '--set-features', action=argparse.BooleanOptionalAction)
137 parser.add_argument('-l', '--show-channels', action=argparse.BooleanOptionalAction)
138 parser.add_argument('-L', '--set-channels', action=argparse.BooleanOptionalAction)
139 parser.add_argument('-T', '--show-time-stamping', action=argparse.BooleanOptionalAction)
140 parser.add_argument('-S', '--statistics', action=argparse.BooleanOptionalAction)
141 # TODO: --show-tunnels tunnel-info-get
142 # TODO: --show-module module-get
143 # TODO: --get-plca-cfg plca-get
144 # TODO: --get-plca-status plca-get-status
145 # TODO: --show-mm mm-get
146 # TODO: --show-fec fec-get
147 # TODO: --dump-module-eerpom module-eeprom-get
150 parser.add_argument('device', metavar='device', type=str)
151 parser.add_argument('args', metavar='args', type=str, nargs='*')
153 args = parser.parse_args()
155 spec = '../../../Documentation/netlink/specs/ethtool.yaml'
156 schema = '../../../Documentation/netlink/genetlink-legacy.yaml'
158 ynl = YnlFamily(spec, schema)
160 if args.set_priv_flags:
161 # TODO: parse the bitmask
162 print("not implemented")
166 return doit(ynl, args, 'eee-set')
169 return doit(ynl, args, 'pause-set')
171 if args.set_coalesce:
172 return doit(ynl, args, 'coalesce-set')
174 if args.set_features:
175 # TODO: parse the bitmask
176 print("not implemented")
179 if args.set_channels:
180 return doit(ynl, args, 'channels-set')
183 return doit(ynl, args, 'rings-set')
185 if args.show_priv_flags:
186 flags = bits_to_dict(dumpit(ynl, args, 'privflags-get')['flags'])
191 eee = dumpit(ynl, args, 'eee-get')
192 ours = bits_to_dict(eee['modes-ours'])
193 peer = bits_to_dict(eee['modes-peer'])
196 status = 'enabled' if eee['enabled'] else 'disabled'
197 if 'active' in eee and eee['active']:
198 status = status + ' - active'
200 status = status + ' - inactive'
202 status = 'not supported'
204 print(f'EEE status: {status}')
205 print_field(eee, ('tx-lpi-timer', 'Tx LPI'))
206 print_speed('Advertised EEE link modes', ours)
207 print_speed('Link partner advertised EEE link modes', peer)
212 print_field(dumpit(ynl, args, 'pause-get'),
213 ('autoneg', 'Autonegotiate', 'bool'),
214 ('rx', 'RX', 'bool'),
215 ('tx', 'TX', 'bool'))
218 if args.show_coalesce:
219 print_field(dumpit(ynl, args, 'coalesce-get'))
222 if args.show_features:
223 reply = dumpit(ynl, args, 'features-get')
224 available = bits_to_dict(reply['hw'])
225 requested = bits_to_dict(reply['wanted']).keys()
226 active = bits_to_dict(reply['active']).keys()
227 never_changed = bits_to_dict(reply['nochange']).keys()
229 for f in sorted(available):
235 if f not in available or f in never_changed:
241 req = " [requested on]"
243 req = " [requested off]"
245 print(f'{f}: {value}{fixed}{req}')
249 if args.show_channels:
250 reply = dumpit(ynl, args, 'channels-get')
251 print(f'Channel parameters for {args.device}:')
253 print(f'Pre-set maximums:')
257 ('other-max', 'Other'),
258 ('combined-max', 'Combined'))
260 print(f'Current hardware settings:')
264 ('other-count', 'Other'),
265 ('combined-count', 'Combined'))
270 reply = dumpit(ynl, args, 'channels-get')
272 print(f'Ring parameters for {args.device}:')
274 print(f'Pre-set maximums:')
277 ('rx-mini-max', 'RX Mini'),
278 ('rx-jumbo-max', 'RX Jumbo'),
281 print(f'Current hardware settings:')
284 ('rx-mini', 'RX Mini'),
285 ('rx-jumbo', 'RX Jumbo'),
289 ('rx-buf-len', 'RX Buf Len'),
290 ('cqe-size', 'CQE Size'),
291 ('tx-push', 'TX Push', 'bool'))
296 print(f'NIC statistics:')
299 strset = dumpit(ynl, args, 'strset-get')
300 pprint.PrettyPrinter().pprint(strset)
307 # TODO: support passing the bitmask
309 #{ 'name': 'eth-phy', 'value': True },
310 { 'name': 'eth-mac', 'value': True },
311 #{ 'name': 'eth-ctrl', 'value': True },
312 #{ 'name': 'rmon', 'value': True },
318 rsp = dumpit(ynl, args, 'stats-get', req)
319 pprint.PrettyPrinter().pprint(rsp)
322 if args.show_time_stamping:
323 tsinfo = dumpit(ynl, args, 'tsinfo-get')
325 print(f'Time stamping parameters for {args.device}:')
327 print('Capabilities:')
328 [print(f'\t{v}') for v in bits_to_dict(tsinfo['timestamping'])]
330 print(f'PTP Hardware Clock: {tsinfo["phc-index"]}')
332 print('Hardware Transmit Timestamp Modes:')
333 [print(f'\t{v}') for v in bits_to_dict(tsinfo['tx-types'])]
335 print('Hardware Receive Filter Modes:')
336 [print(f'\t{v}') for v in bits_to_dict(tsinfo['rx-filters'])]
339 print(f'Settings for {args.device}:')
340 linkmodes = dumpit(ynl, args, 'linkmodes-get')
341 ours = bits_to_dict(linkmodes['ours'])
343 supported_ports = ('TP', 'AUI', 'BNC', 'MII', 'FIBRE', 'Backplane')
344 ports = [ p for p in supported_ports if ours.get(p, False)]
345 print(f'Supported ports: [ {" ".join(ports)} ]')
347 print_speed('Supported link modes', ours)
349 print_field(ours, ('Pause', 'Supported pause frame use', 'yn'))
350 print_field(ours, ('Autoneg', 'Supports auto-negotiation', 'yn'))
352 supported_fec = ('None', 'PS', 'BASER', 'LLRS')
353 fec = [ p for p in supported_fec if ours.get(p, False)]
354 fec_str = " ".join(fec)
356 fec_str = "Not reported"
358 print(f'Supported FEC modes: {fec_str}')
361 if linkmodes['speed'] > 0 and linkmodes['speed'] < 0xffffffff:
362 speed = f'{linkmodes["speed"]}Mb/s'
363 print(f'Speed: {speed}')
369 duplex = duplex_modes.get(linkmodes["duplex"], None)
371 duplex = f'Unknown! ({linkmodes["duplex"]})'
372 print(f'Duplex: {duplex}')
375 if linkmodes.get("autoneg", 0) != 0:
377 print(f'Auto-negotiation: {autoneg}')
385 5: 'Directly Attached Copper',
388 linkinfo = dumpit(ynl, args, 'linkinfo-get')
389 print(f'Port: {ports.get(linkinfo["port"], "Other")}')
391 print_field(linkinfo, ('phyaddr', 'PHYAD'))
397 print(f'Transceiver: {transceiver.get(linkinfo["transceiver"], "Unknown")}')
403 mdix = mdix_ctrl.get(linkinfo['tp-mdix-ctrl'], None)
405 mdix = mdix + ' (forced)'
407 mdix = mdix_ctrl.get(linkinfo['tp-mdix'], 'Unknown (auto)')
408 print(f'MDI-X: {mdix}')
410 debug = dumpit(ynl, args, 'debug-get')
411 msgmask = bits_to_dict(debug.get("msgmask", [])).keys()
412 print(f'Current message level: {" ".join(msgmask)}')
414 linkstate = dumpit(ynl, args, 'linkstate-get')
420 detected = detected_states.get(linkstate['link'], 'unknown')
421 print(f'Link detected: {detected}')
423 if __name__ == '__main__':