From 429e1463afc03d4fa763024d8ab446ec3393b5f1 Mon Sep 17 00:00:00 2001 From: Richard Alpe Date: Mon, 6 Nov 2023 14:19:55 +0100 Subject: [PATCH] cli: add support for printing veth pair They have link set in there json data from ip link show. Signed-off-by: Richard Alpe --- board/netconf/rootfs/lib/infix/cli-pretty | 24 ++++ test/case/cli_pretty/json/bloated.json | 138 ++++++++++++++++++++++ 2 files changed, 162 insertions(+) diff --git a/board/netconf/rootfs/lib/infix/cli-pretty b/board/netconf/rootfs/lib/infix/cli-pretty index 0ea7f3c0..cca1f729 100755 --- a/board/netconf/rootfs/lib/infix/cli-pretty +++ b/board/netconf/rootfs/lib/infix/cli-pretty @@ -86,6 +86,9 @@ class Iface: def is_bridge(self): return self.data['type'] == "infix-if-type:bridge" + def is_veth(self): + return self.data['type'] == "infix-if-type:veth" + def pr_name(self, pipe=""): print(f"{pipe}{self.name:<{Pad.iface - len(pipe)}}", end="") @@ -137,6 +140,20 @@ class Iface: lower.pr_name(pipe) lower.pr_proto_eth() + def pr_veth(self, _ifaces): + self.pr_name(pipe="") + self.pr_proto_eth() + + if self.lower_if: + row = f"{'':<{Pad.iface}}" + row += f"{'veth':<{Pad.proto}}" + row += f"{'':<{Pad.state}}" + row += f"peer:{self.lower_if}" + print(row) + + self.pr_proto_ipv4() + self.pr_proto_ipv6() + def pr_vlan(self, _ifaces): self.pr_name(pipe="") self.pr_proto_eth() @@ -164,6 +181,9 @@ class Iface: if self.oper_status: print(f"{'operational status':<{20}}: {self.oper_status}") + if self.lower_if: + print(f"{'lower-layer-if':<{20}}: {self.lower_if}") + if self.autoneg != 'unknown': val = "on" if self.autoneg else "off" print(f"{'auto-negotiation':<{20}}: {val}") @@ -232,6 +252,10 @@ def pr_interface_list(json): iface.pr_bridge(ifaces) continue + if iface.is_veth(): + iface.pr_veth(ifaces) + continue + if iface.is_vlan(): iface.pr_vlan(ifaces) continue diff --git a/test/case/cli_pretty/json/bloated.json b/test/case/cli_pretty/json/bloated.json index 11d35093..4790f2b2 100644 --- a/test/case/cli_pretty/json/bloated.json +++ b/test/case/cli_pretty/json/bloated.json @@ -383,6 +383,144 @@ "vlan-id": 10 } } + }, + { + "TEST-DESCR": "veth pair with minimal data", + "name": "veth0c", + "type": "infix-if-type:veth", + "oper-status": "up", + "if-index": 13, + "phys-address": "8a:f9:5e:d3:0c:80", + "statistics": { + "in-octets": "488", + "out-octets": "488" + }, + "ieee802-ethernet-interface:ethernet": { + "auto-negotiation": { + "enable": false + } + }, + "ietf-ip:ipv4": { + "mtu": 1500 + }, + "ietf-ip:ipv6": { + "mtu": 1500 + }, + "infix-interfaces:vlan": { + "lower-layer-if": "veth0b" + } + }, + { + "TEST-DESCR": "veth pair with minimal data", + "name": "veth0d", + "type": "infix-if-type:veth", + "oper-status": "up", + "if-index": 13, + "phys-address": "8a:f9:5e:d3:0c:81", + "statistics": { + "in-octets": "488", + "out-octets": "488" + }, + "ieee802-ethernet-interface:ethernet": { + "auto-negotiation": { + "enable": false + } + }, + "ietf-ip:ipv4": { + "mtu": 1500 + }, + "ietf-ip:ipv6": { + "mtu": 1500 + }, + "infix-interfaces:vlan": { + "lower-layer-if": "veth0b" + } + }, + { + "name": "veth0a", + "type": "infix-if-type:veth", + "oper-status": "up", + "if-index": 13, + "phys-address": "8a:f9:5e:d3:0c:32", + "statistics": { + "in-octets": "488", + "out-octets": "488" + }, + "ieee802-ethernet-interface:ethernet": { + "auto-negotiation": { + "enable": false + } + }, + "ietf-ip:ipv4": { + "mtu": 1500, + "address": [ + { + "ip": "192.168.1.1", + "prefix-length": 24, + "origin" : "static" + }, + { + "ip": "192.168.2.1", + "prefix-length": 24, + "origin" : "static" + } + ] + }, + "ietf-ip:ipv6": { + "mtu": 1500, + "address": [ + { + "ip": "fe80::beff:407a:d738:aaaa", + "prefix-length": 64, + "origin": "link-layer" + }, + { + "ip": "fe80::beff:aaaa:d738:aaab", + "prefix-length": 64, + "origin": "random" + } + ] + }, + "infix-interfaces:vlan": { + "lower-layer-if": "veth0b" + } + }, + { + "name": "veth0b", + "type": "infix-if-type:veth", + "oper-status": "up", + "if-index": 13, + "phys-address": "8a:f9:5e:d3:0c:32", + "statistics": { + "in-octets": "488", + "out-octets": "488" + }, + "ieee802-ethernet-interface:ethernet": { + "auto-negotiation": { + "enable": false + } + }, + "ietf-ip:ipv4": { + "mtu": 1500 + }, + "ietf-ip:ipv6": { + "mtu": 1500, + "address": [ + { + "ip": "fe80::beff:407a:d738:aaac", + "prefix-length": 64, + "origin": "link-layer" + }, + { + "ip": "fe80::beff:aaaa:d738:aaad", + "prefix-length": 64, + "origin": "random" + } + ] + }, + "infix-interfaces:vlan": { + "lower-layer-if": "veth0a" + } } ] }