From 38747657358463c56d25b82535075dd42ab809e0 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 23 Sep 2022 10:39:42 +0900 Subject: [PATCH 1/3] networkctl: use "-" for empty LLDP entries --- src/network/networkctl.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 5189ff2280..bb4aa7d52e 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -2482,6 +2482,7 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { assert_se(cell = table_get_cell(table, 0, 3)); table_set_minimum_width(table, cell, 11); + table_set_ersatz_string(table, TABLE_ERSATZ_DASH); for (int i = 0; i < c; i++) { _cleanup_fclose_ FILE *f = NULL; @@ -2520,11 +2521,11 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { r = table_add_many(table, TABLE_STRING, links[i].name, - TABLE_STRING, strna(chassis_id), - TABLE_STRING, strna(system_name), - TABLE_STRING, strna(capabilities), - TABLE_STRING, strna(port_id), - TABLE_STRING, strna(port_description)); + TABLE_STRING, chassis_id, + TABLE_STRING, system_name, + TABLE_STRING, capabilities, + TABLE_STRING, port_id, + TABLE_STRING, port_description); if (r < 0) return table_log_add_error(r); From 767bc538c511501f714a3630a614eaf21804dd55 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 23 Sep 2022 10:43:10 +0900 Subject: [PATCH 2/3] test-network: fix matching string This partially reverts 5515f2169cb5980996044eabb5f1b35e00fd81eb. As the commit changes 'networkctl list', not 'networkctl status'. --- test/test-network/systemd-networkd-tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index b90a07e22b..2f638d158a 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -1047,8 +1047,8 @@ class NetworkctlTests(unittest.TestCase, Utilities): output = check_output(*networkctl_cmd, '-n', '0', 'status', 'lo', env=env) print(output) - self.assertRegex(output, r'Link File: -') - self.assertRegex(output, r'Network File: -') + self.assertRegex(output, r'Link File: n/a') + self.assertRegex(output, r'Network File: n/a') def test_delete_links(self): copy_network_unit('11-dummy.netdev', '11-dummy.network', From f8d7c0c55ecfb4681b31329821b55ca132415413 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 23 Sep 2022 11:18:30 +0900 Subject: [PATCH 3/3] networkctl: re-order entries in status command Also fixes "Speed:" field, which may show empty value. --- src/network/networkctl.c | 140 ++++++++++++++++++++------------------- 1 file changed, 71 insertions(+), 69 deletions(-) diff --git a/src/network/networkctl.c b/src/network/networkctl.c index bb4aa7d52e..7ddf72ddcd 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -1518,6 +1518,25 @@ static int show_logs(const LinkInfo *info) { NULL); } +static int table_add_string_line(Table *table, const char *key, const char *value) { + int r; + + assert(table); + assert(key); + + if (isempty(value)) + return 0; + + r = table_add_many(table, + TABLE_EMPTY, + TABLE_STRING, key, + TABLE_STRING, value); + if (r < 0) + return table_log_add_error(r); + + return 0; +} + static int link_status_one( sd_bus *bus, sd_netlink *rtnl, @@ -1596,6 +1615,7 @@ static int link_status_one( table_set_header(table, false); + /* First line: circle, ifindex, ifname. */ r = table_add_many(table, TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), TABLE_SET_COLOR, on_color_operational); @@ -1604,10 +1624,14 @@ static int link_status_one( r = table_add_cell_stringf(table, &cell, "%i: %s", info->ifindex, info->name); if (r < 0) return table_log_add_error(r); + r = table_add_many(table, TABLE_EMPTY); + if (r < 0) + return table_log_add_error(r); + (void) table_set_align_percent(table, cell, 0); + /* unit files and basic states. */ r = table_add_many(table, - TABLE_EMPTY, TABLE_EMPTY, TABLE_STRING, "Link File:", TABLE_SET_ALIGN_PERCENT, 100, @@ -1616,15 +1640,10 @@ static int link_status_one( TABLE_STRING, "Network File:", TABLE_STRING, strna(network), TABLE_EMPTY, - TABLE_STRING, "Type:", - TABLE_STRING, strna(t), - TABLE_EMPTY, - TABLE_STRING, "Kind:", - TABLE_STRING, strna(info->netdev_kind), - TABLE_EMPTY, TABLE_STRING, "State:"); if (r < 0) return table_log_add_error(r); + r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)", on_color_operational, strna(operational_state), off_color_operational, on_color_setup, strna(setup_state), off_color_setup); @@ -1639,44 +1658,35 @@ static int link_status_one( if (r < 0) return table_log_add_error(r); + r = table_add_string_line(table, "Type:", t); + if (r < 0) + return r; + + r = table_add_string_line(table, "Kind:", info->netdev_kind); + if (r < 0) + return r; + + r = table_add_string_line(table, "Path:", path); + if (r < 0) + return r; + + r = table_add_string_line(table, "Driver:", driver); + if (r < 0) + return r; + + r = table_add_string_line(table, "Vendor:", vendor); + if (r < 0) + return r; + + r = table_add_string_line(table, "Model:", model); + if (r < 0) + return r; + strv_sort(info->alternative_names); r = dump_list(table, "Alternative Names:", info->alternative_names); if (r < 0) return r; - if (path) { - r = table_add_many(table, - TABLE_EMPTY, - TABLE_STRING, "Path:", - TABLE_STRING, path); - if (r < 0) - return table_log_add_error(r); - } - if (driver) { - r = table_add_many(table, - TABLE_EMPTY, - TABLE_STRING, "Driver:", - TABLE_STRING, driver); - if (r < 0) - return table_log_add_error(r); - } - if (vendor) { - r = table_add_many(table, - TABLE_EMPTY, - TABLE_STRING, "Vendor:", - TABLE_STRING, vendor); - if (r < 0) - return table_log_add_error(r); - } - if (model) { - r = table_add_many(table, - TABLE_EMPTY, - TABLE_STRING, "Model:", - TABLE_STRING, model); - if (r < 0) - return table_log_add_error(r); - } - if (info->has_hw_address) { r = dump_hw_address(table, hwdb, "Hardware Address:", &info->hw_address); if (r < 0) @@ -1713,14 +1723,9 @@ static int link_status_one( return table_log_add_error(r); } - if (info->qdisc) { - r = table_add_many(table, - TABLE_EMPTY, - TABLE_STRING, "QDisc:", - TABLE_STRING, info->qdisc); - if (r < 0) - return table_log_add_error(r); - } + r = table_add_string_line(table, "QDisc:", info->qdisc); + if (r < 0) + return r; if (info->master > 0) { r = table_add_many(table, @@ -1778,11 +1783,15 @@ static int link_status_one( if (r < 0) return table_log_add_error(r); - if (info->port_state <= BR_STATE_BLOCKING) + if (info->port_state <= BR_STATE_BLOCKING) { r = table_add_many(table, TABLE_EMPTY, TABLE_STRING, "Port State:", TABLE_STRING, bridge_state_to_string(info->port_state)); + if (r < 0) + return table_log_add_error(r); + } + } else if (streq_ptr(info->netdev_kind, "bond")) { r = table_add_many(table, TABLE_EMPTY, @@ -1906,6 +1915,7 @@ static int link_status_one( TABLE_STRING, ttl); if (r < 0) return table_log_add_error(r); + } else if (streq_ptr(info->netdev_kind, "vlan") && info->vlan_id > 0) { r = table_add_many(table, TABLE_EMPTY, @@ -1913,6 +1923,7 @@ static int link_status_one( TABLE_UINT16, info->vlan_id); if (r < 0) return table_log_add_error(r); + } else if (STRPTR_IN_SET(info->netdev_kind, "ipip", "sit", "gre", "gretap", "erspan", "vti")) { if (in_addr_is_set(AF_INET, &info->local)) { r = table_add_many(table, @@ -1931,6 +1942,7 @@ static int link_status_one( if (r < 0) return table_log_add_error(r); } + } else if (STRPTR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) { if (in_addr_is_set(AF_INET6, &info->local)) { r = table_add_many(table, @@ -1949,6 +1961,7 @@ static int link_status_one( if (r < 0) return table_log_add_error(r); } + } else if (streq_ptr(info->netdev_kind, "geneve")) { r = table_add_many(table, TABLE_EMPTY, @@ -2043,6 +2056,7 @@ static int link_status_one( if (r < 0) return table_log_add_error(r); } + } else if (STRPTR_IN_SET(info->netdev_kind, "macvlan", "macvtap")) { r = table_add_many(table, TABLE_EMPTY, @@ -2050,6 +2064,7 @@ static int link_status_one( TABLE_STRING, macvlan_mode_to_string(info->macvlan_mode)); if (r < 0) return table_log_add_error(r); + } else if (streq_ptr(info->netdev_kind, "ipvlan")) { _cleanup_free_ char *p = NULL, *s = NULL; @@ -2118,9 +2133,6 @@ static int link_status_one( } if (info->has_ethtool_link_info) { - const char *duplex = duplex_to_string(info->duplex); - const char *port = port_to_string(info->port); - if (IN_SET(info->autonegotiation, AUTONEG_DISABLE, AUTONEG_ENABLE)) { r = table_add_many(table, TABLE_EMPTY, @@ -2130,7 +2142,7 @@ static int link_status_one( return table_log_add_error(r); } - if (info->speed > 0) { + if (info->speed > 0 && info->speed != UINT64_MAX) { r = table_add_many(table, TABLE_EMPTY, TABLE_STRING, "Speed:", @@ -2139,23 +2151,13 @@ static int link_status_one( return table_log_add_error(r); } - if (duplex) { - r = table_add_many(table, - TABLE_EMPTY, - TABLE_STRING, "Duplex:", - TABLE_STRING, duplex); - if (r < 0) - return table_log_add_error(r); - } + r = table_add_string_line(table, "Duplex:", duplex_to_string(info->duplex)); + if (r < 0) + return r; - if (port) { - r = table_add_many(table, - TABLE_EMPTY, - TABLE_STRING, "Port:", - TABLE_STRING, port); - if (r < 0) - return table_log_add_error(r); - } + r = table_add_string_line(table, "Port:", port_to_string(info->port)); + if (r < 0) + return r; } r = dump_addresses(rtnl, lease, table, info->ifindex);