From d319b0deaccf69550da9e941d753c60de7281d8d Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 27 Jul 2019 17:11:40 +0200 Subject: [PATCH] lib: expose management interface index --- src/client/display.c | 2 ++ src/lib/atoms/mgmt.c | 16 ++++++++++++++++ src/lib/lldpctl.h | 1 + tests/integration/test_basic.py | 6 ++++++ tests/integration/test_lldpcli.py | 8 ++++++++ 5 files changed, 33 insertions(+) diff --git a/src/client/display.c b/src/client/display.c index 312091de..dc130703 100644 --- a/src/client/display.c +++ b/src/client/display.c @@ -258,6 +258,8 @@ display_chassis(struct writer* w, lldpctl_atom_t* chassis, int details) lldpctl_atom_foreach(mgmts, mgmt) { tag_datatag(w, "mgmt-ip", "MgmtIP", lldpctl_atom_get_str(mgmt, lldpctl_k_mgmt_ip)); + tag_datatag(w, "mgmt-iface", "MgmtIface", + lldpctl_atom_get_str(mgmt, lldpctl_k_mgmt_iface_index)); } lldpctl_atom_dec_ref(mgmts); diff --git a/src/lib/atoms/mgmt.c b/src/lib/atoms/mgmt.c index 8afb6b07..702d9457 100644 --- a/src/lib/atoms/mgmt.c +++ b/src/lib/atoms/mgmt.c @@ -87,6 +87,21 @@ _lldpctl_atom_free_mgmt(lldpctl_atom_t *atom) lldpctl_atom_dec_ref(mgmt->parent); } +static long int +_lldpctl_atom_get_int_mgmt(lldpctl_atom_t *atom, lldpctl_key_t key) +{ + struct _lldpctl_atom_mgmt_t *m = + (struct _lldpctl_atom_mgmt_t *)atom; + + /* Local and remote port */ + switch (key) { + case lldpctl_k_mgmt_iface_index: + return m->mgmt->m_iface; + default: + return SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); + } +} + static const char* _lldpctl_atom_get_str_mgmt(lldpctl_atom_t *atom, lldpctl_key_t key) { @@ -136,6 +151,7 @@ static struct atom_builder mgmt = { atom_mgmt, sizeof(struct _lldpctl_atom_mgmt_t), .init = _lldpctl_atom_new_mgmt, .free = _lldpctl_atom_free_mgmt, + .get_int = _lldpctl_atom_get_int_mgmt, .get_str = _lldpctl_atom_get_str_mgmt }; ATOM_BUILDER_REGISTER(mgmts_list, 6); diff --git a/src/lib/lldpctl.h b/src/lib/lldpctl.h index 53d3c4bc..c10a83dc 100644 --- a/src/lib/lldpctl.h +++ b/src/lib/lldpctl.h @@ -778,6 +778,7 @@ typedef enum { lldpctl_k_med_power_val, /**< `(I,W)` LLDP MED power value */ lldpctl_k_mgmt_ip = 3000, /**< `(S)` IP address */ + lldpctl_k_mgmt_iface_index = 30001, /**< `(I)` Interface index */ lldpctl_k_tx_cnt = 4000, /**< `(I)` tx cnt. Only works for a local port. */ lldpctl_k_rx_cnt, /**< `(I)` rx cnt. Only works for a local port. */ diff --git a/tests/integration/test_basic.py b/tests/integration/test_basic.py index 28c1896f..6d79020c 100644 --- a/tests/integration/test_basic.py +++ b/tests/integration/test_basic.py @@ -22,6 +22,7 @@ def test_one_neighbor(lldpd1, lldpd, lldpcli, namespaces): "lldp.eth0.chassis.mac": "00:00:00:00:00:02", "lldp.eth0.chassis.name": "ns-2.example.com", "lldp.eth0.chassis.mgmt-ip": "fe80::200:ff:fe00:2", + "lldp.eth0.chassis.mgmt-iface": "2", "lldp.eth0.chassis.Bridge.enabled": "off", "lldp.eth0.chassis.Wlan.enabled": "off", "lldp.eth0.port.mac": "00:00:00:00:00:02", @@ -63,6 +64,7 @@ def test_one_interface(lldpd1, lldpd, lldpcli, namespaces): "lldp.eth0.chassis.mac": "00:00:00:00:00:01", "lldp.eth0.chassis.name": "ns-1.example.com", "lldp.eth0.chassis.mgmt-ip": "fe80::200:ff:fe00:1", + "lldp.eth0.chassis.mgmt-iface": "3", "lldp.eth0.chassis.Bridge.enabled": "off", "lldp.eth0.chassis.Wlan.enabled": "off", "lldp.eth0.port.mac": "00:00:00:00:00:01", @@ -150,6 +152,7 @@ def test_forced_management_address(lldpd1, lldpd, lldpcli, namespaces): with namespaces(1): out = lldpcli("-f", "keyvalue", "show", "neighbors") assert out["lldp.eth0.chassis.mgmt-ip"] == "2001:db8::47" + assert out["lldp.eth0.chassis.mgmt-iface"] == "0" def test_management_address(lldpd1, lldpd, lldpcli, links, namespaces): @@ -162,6 +165,7 @@ def test_management_address(lldpd1, lldpd, lldpcli, links, namespaces): with namespaces(1): out = lldpcli("-f", "keyvalue", "show", "neighbors") assert out["lldp.eth0.chassis.mgmt-ip"] == "172.25.21.47" + assert out["lldp.eth0.chassis.mgmt-iface"] == "2" def test_change_management_address(lldpd1, lldpd, lldpcli, links, namespaces): @@ -176,6 +180,7 @@ def test_change_management_address(lldpd1, lldpd, lldpcli, links, namespaces): with namespaces(1): out = lldpcli("-f", "keyvalue", "show", "neighbors") assert out["lldp.eth0.chassis.mgmt-ip"] == "192.168.14.2" + assert out["lldp.eth0.chassis.mgmt-iface"] == "2" with namespaces(2): ipr.addr('del', index=idx, address="192.168.14.2", mask=24) ipr.addr('add', index=idx, address="192.168.14.5", mask=24) @@ -183,6 +188,7 @@ def test_change_management_address(lldpd1, lldpd, lldpcli, links, namespaces): with namespaces(1): out = lldpcli("-f", "keyvalue", "show", "neighbors") assert out["lldp.eth0.chassis.mgmt-ip"] == "192.168.14.5" + assert out["lldp.eth0.chassis.mgmt-iface"] == "2" def test_portid_subtype_ifname(lldpd1, lldpd, lldpcli, namespaces): diff --git a/tests/integration/test_lldpcli.py b/tests/integration/test_lldpcli.py index 5502da27..8949d6f6 100644 --- a/tests/integration/test_lldpcli.py +++ b/tests/integration/test_lldpcli.py @@ -26,6 +26,7 @@ Interface: eth0, via: LLDP, RID: 1, Time: 0 day, 00:00:{seconds} SysName: ns-2.example.com SysDescr: Spectacular GNU/Linux 2016 {uname} MgmtIP: fe80::200:ff:fe00:2 + MgmtIface: 2 Capability: Bridge, off Capability: Router, {router} Capability: Wlan, off @@ -46,6 +47,7 @@ Interface: eth0, via: unknown, Time: {time} SysName: ns-1.example.com SysDescr: Spectacular GNU/Linux 2016 {uname} MgmtIP: fe80::200:ff:fe00:1 + MgmtIface: 3 Capability: Bridge, off Capability: Router, {router} Capability: Wlan, off @@ -110,6 +112,7 @@ def test_text_output(lldpd1, lldpd, lldpcli, namespaces, uname, command, "value": "00:00:00:00:00:02"}, "descr": "Spectacular GNU/Linux 2016 {}".format(uname), "mgmt-ip": "fe80::200:ff:fe00:2", + "mgmt-iface": "2", "capability": [ {"type": "Bridge", "enabled": False}, {"type": "Wlan", "enabled": False},]}}, @@ -131,6 +134,7 @@ def test_text_output(lldpd1, lldpd, lldpcli, namespaces, uname, command, "value": "00:00:00:00:00:01"}, "descr": "Spectacular GNU/Linux 2016 {}".format(uname), "mgmt-ip": "fe80::200:ff:fe00:1", + "mgmt-iface": "3", "capability": [ {"type": "Bridge", "enabled": False}, {"type": "Wlan", "enabled": False},]}}, @@ -187,6 +191,7 @@ def test_json_output(lldpd1, lldpd, lldpcli, namespaces, uname, command, "name": [{"value": "ns-2.example.com"}], "descr": [{"value": "Spectacular GNU/Linux 2016 {}".format(uname)}], "mgmt-ip": [{"value": "fe80::200:ff:fe00:2"}], + "mgmt-iface": [{"value": "2"}], "capability": [ {"type": "Bridge", "enabled": False}, {"type": "Wlan", "enabled": False}, @@ -215,6 +220,7 @@ def test_json_output(lldpd1, lldpd, lldpcli, namespaces, uname, command, "name": [{"value": "ns-1.example.com"}], "descr": [{"value": "Spectacular GNU/Linux 2016 {}".format(uname)}], "mgmt-ip": [{"value": "fe80::200:ff:fe00:1"}], + "mgmt-iface": [{"value": "3"}], "capability": [ {"type": "Bridge", "enabled": False}, {"type": "Wlan", "enabled": False}, @@ -271,6 +277,7 @@ def test_json0_output(lldpd1, lldpd, lldpcli, namespaces, uname, command, ns-2.example.com Spectacular GNU/Linux 2016 {uname} fe80::200:ff:fe00:2 + 2 @@ -293,6 +300,7 @@ def test_json0_output(lldpd1, lldpd, lldpcli, namespaces, uname, command, ns-1.example.com Spectacular GNU/Linux 2016 {uname} fe80::200:ff:fe00:1 + 3 -- 2.39.5