Skip to content

Commit

Permalink
Merge pull request #674 from rical/yanger-hide-internal-interfaces
Browse files Browse the repository at this point in the history
yanger: hide internal interfaces
  • Loading branch information
wkz authored Oct 1, 2024
2 parents 712780f + 707e7d1 commit b21945f
Show file tree
Hide file tree
Showing 20 changed files with 1,337 additions and 1,220 deletions.
72 changes: 42 additions & 30 deletions src/statd/python/yanger/yanger.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,22 @@ def get_brport_multicast(ifname):
return multicast


def add_ip_link(ifname, iface_out):
# We always get all interfaces for two reasons.
# 1) To increase speed on large iron with many ports.
# 2) To simplify testing (single dummy file ip-link-show.json).
def get_ip_link():
"""Fetch interface link information from kernel"""
data = run_json_cmd(['ip', '-s', '-d', '-j', 'link', 'show', 'dev', ifname],
f"ip-link-show-dev-{ifname}.json")
if len(data) != 1:
logger.error("expected ip link output to be array with length 1")
sys.exit(1)
return run_json_cmd(['ip', '-s', '-d', '-j', 'link', 'show'],
f"ip-link-show.json")


def get_ip_addr():
"""Fetch interface address information from kernel"""
return run_json_cmd(['ip', '-j', 'addr', 'show'],
f"ip-addr-show.json")

iface_in = data[0]

def add_ip_link(ifname, iface_in, iface_out):
if 'ifname' in iface_in:
iface_out['name'] = iface_in['ifname']

Expand Down Expand Up @@ -652,17 +658,7 @@ def add_ip_link(ifname, iface_out):
insert(iface_out, "statistics", "in-octets", str(val))


def add_ip_addr(ifname, iface_out):
"""Fetch interface address information from kernel"""

data = run_json_cmd(['ip', '-j', 'addr', 'show', 'dev', ifname],
f"ip-addr-show-dev-{ifname}.json")
if len(data) != 1:
logger.error("expected ip addr output to be array with length 1")
sys.exit(1)

iface_in = data[0]

def add_ip_addr(ifname, iface_in, iface_out):
if 'mtu' in iface_in and ifname != "lo":
insert(iface_out, "ietf-ip:ipv4", "mtu", iface_in['mtu'])

Expand Down Expand Up @@ -881,11 +877,11 @@ def add_vlans_to_bridge(brname, iface_out, mc_status):

insert(iface_out, "infix-interfaces:bridge", "vlans", "vlan", vlans)

def get_iface_data(ifname):
def get_iface_data(ifname, ip_link_data, ip_addr_data):
iface_out = {}

add_ip_link(ifname, iface_out)
add_ip_addr(ifname, iface_out)
add_ip_link(ifname, ip_link_data, iface_out)
add_ip_addr(ifname, ip_addr_data, iface_out)

if 'type' in iface_out and iface_out['type'] == "infix-if-type:ethernet":
add_ethtool_groups(ifname, iface_out)
Expand All @@ -902,6 +898,30 @@ def get_iface_data(ifname):

return iface_out

def _add_interface(ifname, ip_link_data, ip_addr_data, yang_ifaces):
# We expect both ip addr and link data to exist.
if not ip_link_data or not ip_addr_data:
return

# Skip internal interfaces.
if 'group' in ip_link_data and ip_link_data['group'] == "internal":
return

yang_ifaces.append(get_iface_data(ifname, ip_link_data, ip_addr_data))

def add_interface(ifname, yang_ifaces):
ip_link_data = get_ip_link()
ip_addr_data = get_ip_addr()

if ifname:
ip_link_data = next((d for d in ip_link_data if d.get('ifname') == ifname), None)
ip_addr_data = next((d for d in ip_addr_data if d.get('ifname') == ifname), None)
_add_interface(ifname, ip_link_data, ip_addr_data, yang_ifaces)
else:
for link in ip_link_data:
addr = next((d for d in ip_addr_data if d.get('ifname') == link["ifname"]), None)
_add_interface(link["ifname"], link, addr, yang_ifaces)

def main():
global TESTPATH
global logger
Expand Down Expand Up @@ -936,15 +956,7 @@ def main():
"interface": []
}
}

if args.param:
iface_data = get_iface_data(args.param)
yang_data['ietf-interfaces:interfaces']['interface'].append(iface_data)
else:
ifnames = os.listdir('/sys/class/net/')
for ifname in ifnames:
iface_data = get_iface_data(ifname)
yang_data['ietf-interfaces:interfaces']['interface'].append(iface_data)
add_interface(args.param, yang_data['ietf-interfaces:interfaces']['interface'])

elif args.model == 'ietf-routing':
yang_data = {
Expand Down
25 changes: 5 additions & 20 deletions test/case/cli/sysrepo-emulator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ ROOT_PATH="$SCRIPT_PATH/../../../"

YANGER_TOOL="$ROOT_PATH/src/statd/python/yanger/yanger.py"

INTERFACES="br0 br1 e0 e1 e2 e3 e4"

INTERFACE_OUTPUT_FILE="$(mktemp)"
INTERFACES_OUTPUT_FILE="$(mktemp)"
ROUTES_OUTPUT_FILE="$(mktemp)"
cleanup() {
rm -f "$INTERFACE_OUTPUT_FILE"
rm -f "$INTERFACES_OUTPUT_FILE"
rm -f "$ROUTES_OUTPUT_FILE"
}
trap cleanup EXIT
Expand All @@ -21,22 +18,10 @@ if [ ! -e "$YANGER_TOOL" ]; then
exit 1
fi


for iface in $INTERFACES; do
if ! "$YANGER_TOOL" "ietf-interfaces" \
-t "$SCRIPT_PATH/system-output/" \
-p "$iface" >> "$INTERFACE_OUTPUT_FILE"; then
echo "Error, running yanger for interface $iface" >&2
exit 1
fi
done

if ! jq -s 'reduce .[] as $item
({}; .["ietf-interfaces:interfaces"].interface +=
$item["ietf-interfaces:interfaces"].interface)' \
"$INTERFACE_OUTPUT_FILE" >> $INTERFACES_OUTPUT_FILE; then
echo "Error, merging yanger output data" >&2
exit 1
if ! "$YANGER_TOOL" "ietf-interfaces" \
-t "$SCRIPT_PATH/system-output/" >> "$INTERFACES_OUTPUT_FILE"; then
echo "Error, running yanger for interface $iface" >&2
exit 1
fi

$YANGER_TOOL "ietf-routing" -t "$SCRIPT_PATH/system-output/" > "$ROUTES_OUTPUT_FILE"
Expand Down
32 changes: 32 additions & 0 deletions test/case/cli/system-output/ethtool-dsa0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Settings for dsa0:
Supported ports: [ MII ]
Supported link modes: 10000baseT/Full
10000baseKX4/Full
10000baseKR/Full
10000baseCR/Full
10000baseSR/Full
10000baseLR/Full
10000baseLRM/Full
10000baseER/Full
Supported pause frame use: Symmetric Receive-only
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 10000baseT/Full
10000baseKX4/Full
10000baseKR/Full
10000baseCR/Full
10000baseSR/Full
10000baseLR/Full
10000baseLRM/Full
10000baseER/Full
Advertised pause frame use: Symmetric Receive-only
Advertised auto-negotiation: Yes
Advertised FEC modes: Not reported
Speed: 10000Mb/s
Duplex: Full
Auto-negotiation: on
Port: MII
PHYAD: 0
Transceiver: internal
netlink error: Operation not permitted
Link detected: yes
7 changes: 7 additions & 0 deletions test/case/cli/system-output/ethtool-groups-dsa0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ {
"ifname": "dsa0",
"eth-phy": {},
"eth-mac": {},
"eth-ctrl": {},
"rmon": {}
} ]
101 changes: 0 additions & 101 deletions test/case/cli/system-output/ip-addr-show-dev-br0.json

This file was deleted.

100 changes: 0 additions & 100 deletions test/case/cli/system-output/ip-addr-show-dev-br1.json

This file was deleted.

Loading

0 comments on commit b21945f

Please sign in to comment.