2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-08-12 01:41:42 +02:00
|
|
|
|
|
|
|
#include <getopt.h>
|
2017-06-26 19:58:10 +02:00
|
|
|
#include <linux/if_addrlabel.h>
|
2014-12-12 19:07:26 +01:00
|
|
|
#include <net/if.h>
|
2015-09-23 03:01:06 +02:00
|
|
|
#include <stdbool.h>
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2019-05-25 17:22:05 +02:00
|
|
|
#include "sd-bus.h"
|
2014-12-05 01:16:05 +01:00
|
|
|
#include "sd-device.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "sd-hwdb.h"
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
#include "sd-lldp.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "sd-netlink.h"
|
|
|
|
#include "sd-network.h"
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "arphrd-list.h"
|
2019-05-25 17:22:05 +02:00
|
|
|
#include "bus-common-errors.h"
|
|
|
|
#include "bus-error.h"
|
|
|
|
#include "bus-util.h"
|
2014-12-05 01:16:05 +01:00
|
|
|
#include "device-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "ether-addr-util.h"
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
#include "fd-util.h"
|
2019-05-22 19:21:17 +02:00
|
|
|
#include "format-table.h"
|
2019-05-29 07:18:41 +02:00
|
|
|
#include "format-util.h"
|
2014-12-15 20:07:34 +01:00
|
|
|
#include "hwdb-util.h"
|
2014-08-12 01:41:42 +02:00
|
|
|
#include "local-addresses.h"
|
2015-10-26 23:01:30 +01:00
|
|
|
#include "locale-util.h"
|
2017-06-26 19:58:10 +02:00
|
|
|
#include "macro.h"
|
2018-11-20 09:57:30 +01:00
|
|
|
#include "main-func.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "netlink-util.h"
|
|
|
|
#include "pager.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "parse-util.h"
|
2018-11-20 15:42:57 +01:00
|
|
|
#include "pretty-print.h"
|
2019-05-22 11:46:41 +02:00
|
|
|
#include "set.h"
|
2014-08-15 13:18:50 +02:00
|
|
|
#include "socket-util.h"
|
2019-03-13 12:14:47 +01:00
|
|
|
#include "sort-util.h"
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
#include "sparse-endian.h"
|
2016-01-12 15:34:20 +01:00
|
|
|
#include "stdio-util.h"
|
2015-10-26 22:31:05 +01:00
|
|
|
#include "string-table.h"
|
2015-10-26 23:01:30 +01:00
|
|
|
#include "string-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "strv.h"
|
2016-03-02 21:43:41 +01:00
|
|
|
#include "strxcpyx.h"
|
2015-04-10 23:15:59 +02:00
|
|
|
#include "terminal-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "verbs.h"
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2018-11-11 12:56:29 +01:00
|
|
|
static PagerFlags arg_pager_flags = 0;
|
2014-08-12 01:41:42 +02:00
|
|
|
static bool arg_legend = true;
|
2014-08-12 15:19:30 +02:00
|
|
|
static bool arg_all = false;
|
2019-05-24 17:37:48 +02:00
|
|
|
static bool arg_stats = false;
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2018-01-29 21:08:39 +01:00
|
|
|
static char *link_get_type_string(unsigned short iftype, sd_device *d) {
|
2018-09-01 16:12:47 +02:00
|
|
|
const char *t, *devtype;
|
2014-08-12 01:41:42 +02:00
|
|
|
char *p;
|
|
|
|
|
2018-09-01 16:12:47 +02:00
|
|
|
if (d &&
|
|
|
|
sd_device_get_devtype(d, &devtype) >= 0 &&
|
|
|
|
!isempty(devtype))
|
|
|
|
return strdup(devtype);
|
2014-08-12 01:41:42 +02:00
|
|
|
|
|
|
|
t = arphrd_to_name(iftype);
|
2018-01-29 21:08:39 +01:00
|
|
|
if (!t)
|
|
|
|
return NULL;
|
2014-08-12 01:41:42 +02:00
|
|
|
|
|
|
|
p = strdup(t);
|
|
|
|
if (!p)
|
2018-01-29 21:08:39 +01:00
|
|
|
return NULL;
|
2014-08-12 01:41:42 +02:00
|
|
|
|
|
|
|
ascii_strlower(p);
|
2018-01-29 21:08:39 +01:00
|
|
|
return p;
|
2014-08-12 01:41:42 +02:00
|
|
|
}
|
|
|
|
|
2016-02-19 19:21:30 +01:00
|
|
|
static void operational_state_to_color(const char *state, const char **on, const char **off) {
|
|
|
|
assert(on);
|
|
|
|
assert(off);
|
|
|
|
|
2019-02-09 19:49:12 +01:00
|
|
|
if (STRPTR_IN_SET(state, "routable", "enslaved")) {
|
2016-02-19 19:21:30 +01:00
|
|
|
*on = ansi_highlight_green();
|
|
|
|
*off = ansi_normal();
|
|
|
|
} else if (streq_ptr(state, "degraded")) {
|
|
|
|
*on = ansi_highlight_yellow();
|
|
|
|
*off = ansi_normal();
|
|
|
|
} else
|
|
|
|
*on = *off = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_state_to_color(const char *state, const char **on, const char **off) {
|
|
|
|
assert(on);
|
|
|
|
assert(off);
|
|
|
|
|
|
|
|
if (streq_ptr(state, "configured")) {
|
|
|
|
*on = ansi_highlight_green();
|
|
|
|
*off = ansi_normal();
|
|
|
|
} else if (streq_ptr(state, "configuring")) {
|
|
|
|
*on = ansi_highlight_yellow();
|
|
|
|
*off = ansi_normal();
|
2016-09-25 02:18:02 +02:00
|
|
|
} else if (STRPTR_IN_SET(state, "failed", "linger")) {
|
2016-02-19 19:21:30 +01:00
|
|
|
*on = ansi_highlight_red();
|
|
|
|
*off = ansi_normal();
|
|
|
|
} else
|
|
|
|
*on = *off = "";
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:03:45 +02:00
|
|
|
typedef struct LinkInfo {
|
2016-02-19 18:57:11 +01:00
|
|
|
char name[IFNAMSIZ+1];
|
2014-08-12 16:03:45 +02:00
|
|
|
int ifindex;
|
2016-02-18 22:47:34 +01:00
|
|
|
unsigned short iftype;
|
2016-02-19 19:18:12 +01:00
|
|
|
struct ether_addr mac_address;
|
|
|
|
uint32_t mtu;
|
2019-05-21 14:14:36 +02:00
|
|
|
uint32_t min_mtu;
|
|
|
|
uint32_t max_mtu;
|
2019-05-22 10:53:12 +02:00
|
|
|
uint32_t tx_queues;
|
|
|
|
uint32_t rx_queues;
|
2016-02-19 19:18:12 +01:00
|
|
|
|
2019-05-24 17:37:48 +02:00
|
|
|
union {
|
|
|
|
struct rtnl_link_stats64 stats64;
|
|
|
|
struct rtnl_link_stats stats;
|
|
|
|
};
|
|
|
|
|
2019-05-25 17:22:05 +02:00
|
|
|
double tx_bitrate;
|
|
|
|
double rx_bitrate;
|
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
bool has_mac_address:1;
|
2019-05-22 10:53:12 +02:00
|
|
|
bool has_tx_queues:1;
|
|
|
|
bool has_rx_queues:1;
|
2019-05-24 17:37:48 +02:00
|
|
|
bool has_stats64:1;
|
|
|
|
bool has_stats:1;
|
2019-05-25 17:22:05 +02:00
|
|
|
bool has_bitrates:1;
|
2014-08-12 16:03:45 +02:00
|
|
|
} LinkInfo;
|
|
|
|
|
2018-09-18 01:39:24 +02:00
|
|
|
static int link_info_compare(const LinkInfo *a, const LinkInfo *b) {
|
|
|
|
return CMP(a->ifindex, b->ifindex);
|
2014-08-12 16:03:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 05:32:51 +01:00
|
|
|
static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns) {
|
2016-02-19 18:57:11 +01:00
|
|
|
const char *name;
|
|
|
|
uint16_t type;
|
2019-02-15 05:32:51 +01:00
|
|
|
int ifindex, r;
|
2014-08-12 16:03:45 +02:00
|
|
|
|
2016-02-19 18:57:11 +01:00
|
|
|
assert(m);
|
|
|
|
assert(info);
|
2014-08-12 16:03:45 +02:00
|
|
|
|
2016-02-19 18:57:11 +01:00
|
|
|
r = sd_netlink_message_get_type(m, &type);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-08-12 16:03:45 +02:00
|
|
|
|
2016-02-19 18:57:11 +01:00
|
|
|
if (type != RTM_NEWLINK)
|
|
|
|
return 0;
|
2014-08-12 16:03:45 +02:00
|
|
|
|
2019-02-15 05:32:51 +01:00
|
|
|
r = sd_rtnl_message_link_get_ifindex(m, &ifindex);
|
2016-02-19 18:57:11 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-08-12 16:03:45 +02:00
|
|
|
|
2016-02-19 18:57:11 +01:00
|
|
|
r = sd_netlink_message_read_string(m, IFLA_IFNAME, &name);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-08-12 16:03:45 +02:00
|
|
|
|
2019-02-15 05:32:51 +01:00
|
|
|
if (patterns) {
|
|
|
|
char str[DECIMAL_STR_MAX(int)];
|
|
|
|
|
|
|
|
xsprintf(str, "%i", ifindex);
|
|
|
|
|
|
|
|
if (!strv_fnmatch(patterns, str, 0) && !strv_fnmatch(patterns, name, 0))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
r = sd_rtnl_message_link_get_type(m, &info->iftype);
|
2016-02-19 18:57:11 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-08-12 16:03:45 +02:00
|
|
|
|
2016-03-02 21:43:41 +01:00
|
|
|
strscpy(info->name, sizeof info->name, name);
|
2019-02-15 05:32:51 +01:00
|
|
|
info->ifindex = ifindex;
|
2016-02-19 19:18:12 +01:00
|
|
|
|
|
|
|
info->has_mac_address =
|
|
|
|
sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &info->mac_address) >= 0 &&
|
2016-03-02 21:43:30 +01:00
|
|
|
memcmp(&info->mac_address, ÐER_ADDR_NULL, sizeof(struct ether_addr)) != 0;
|
2016-02-19 19:18:12 +01:00
|
|
|
|
2019-05-30 20:30:31 +02:00
|
|
|
(void) sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu);
|
|
|
|
(void) sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu);
|
|
|
|
(void) sd_netlink_message_read_u32(m, IFLA_MAX_MTU, &info->max_mtu);
|
2019-05-21 14:14:36 +02:00
|
|
|
|
2019-05-22 10:53:12 +02:00
|
|
|
info->has_rx_queues =
|
|
|
|
sd_netlink_message_read_u32(m, IFLA_NUM_RX_QUEUES, &info->rx_queues) >= 0 &&
|
|
|
|
info->rx_queues > 0;
|
|
|
|
|
|
|
|
info->has_tx_queues =
|
|
|
|
sd_netlink_message_read_u32(m, IFLA_NUM_TX_QUEUES, &info->tx_queues) >= 0 &&
|
|
|
|
info->tx_queues > 0;
|
|
|
|
|
2019-05-24 17:37:48 +02:00
|
|
|
if (sd_netlink_message_read(m, IFLA_STATS64, sizeof info->stats64, &info->stats64) >= 0)
|
|
|
|
info->has_stats64 = true;
|
|
|
|
else if (sd_netlink_message_read(m, IFLA_STATS, sizeof info->stats, &info->stats) >= 0)
|
|
|
|
info->has_stats = true;
|
|
|
|
|
2016-02-19 18:57:11 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-25 17:22:05 +02:00
|
|
|
static int acquire_link_bitrates(sd_bus *bus, LinkInfo *link) {
|
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
_cleanup_free_ char *path = NULL, *ifindex_str = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (asprintf(&ifindex_str, "%i", link->ifindex) < 0)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str, &path);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_call_method(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.network1",
|
|
|
|
path,
|
|
|
|
"org.freedesktop.DBus.Properties",
|
|
|
|
"Get",
|
|
|
|
&error,
|
|
|
|
&reply,
|
|
|
|
"ss",
|
|
|
|
"org.freedesktop.network1.Link",
|
|
|
|
"BitRates");
|
|
|
|
if (r < 0) {
|
|
|
|
if (sd_bus_error_has_name(&error, BUS_ERROR_SPEED_METER_INACTIVE))
|
|
|
|
return 0;
|
|
|
|
return log_error_errno(r, "%s", bus_error_message(&error, r));
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_message_enter_container(reply, 'v', "(dd)");
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_parse_error(r);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(reply, "(dd)", &link->tx_bitrate, &link->rx_bitrate);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_parse_error(r);
|
|
|
|
|
|
|
|
r = sd_bus_message_exit_container(reply);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_parse_error(r);
|
|
|
|
|
|
|
|
link->has_bitrates = link->tx_bitrate >= 0 && link->rx_bitrate >= 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char **patterns, LinkInfo **ret) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
|
2016-02-19 19:21:30 +01:00
|
|
|
_cleanup_free_ LinkInfo *links = NULL;
|
2019-05-25 17:22:05 +02:00
|
|
|
size_t allocated = 0, c = 0, j;
|
2016-02-19 19:21:30 +01:00
|
|
|
sd_netlink_message *i;
|
2016-02-19 18:26:18 +01:00
|
|
|
int r;
|
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
assert(rtnl);
|
2016-02-19 18:26:18 +01:00
|
|
|
assert(ret);
|
2014-08-12 01:41:42 +02:00
|
|
|
|
|
|
|
r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return rtnl_log_create_error(r);
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_request_dump(req, true);
|
2014-08-12 01:41:42 +02:00
|
|
|
if (r < 0)
|
|
|
|
return rtnl_log_create_error(r);
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_call(rtnl, req, 0, &reply);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to enumerate links: %m");
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2016-02-19 19:21:30 +01:00
|
|
|
for (i = reply; i; i = sd_netlink_message_next(i)) {
|
|
|
|
if (!GREEDY_REALLOC(links, allocated, c+1))
|
|
|
|
return -ENOMEM;
|
2016-02-19 18:26:18 +01:00
|
|
|
|
2019-02-15 05:32:51 +01:00
|
|
|
r = decode_link(i, links + c, patterns);
|
2016-02-19 19:21:30 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r > 0)
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
2018-09-18 01:39:24 +02:00
|
|
|
typesafe_qsort(links, c, link_info_compare);
|
2016-02-19 19:21:30 +01:00
|
|
|
|
2019-05-25 17:22:05 +02:00
|
|
|
if (bus)
|
|
|
|
for (j = 0; j < c; j++)
|
|
|
|
(void) acquire_link_bitrates(bus, links + j);
|
|
|
|
|
2018-04-05 07:26:26 +02:00
|
|
|
*ret = TAKE_PTR(links);
|
2016-02-19 19:21:30 +01:00
|
|
|
|
|
|
|
return (int) c;
|
2016-02-19 18:26:18 +01:00
|
|
|
}
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2016-02-19 18:26:18 +01:00
|
|
|
static int list_links(int argc, char *argv[], void *userdata) {
|
2016-02-19 19:18:12 +01:00
|
|
|
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
2016-02-19 18:26:18 +01:00
|
|
|
_cleanup_free_ LinkInfo *links = NULL;
|
2019-05-22 19:21:17 +02:00
|
|
|
_cleanup_(table_unrefp) Table *table = NULL;
|
|
|
|
TableCell *cell;
|
2016-02-19 19:18:12 +01:00
|
|
|
int c, i, r;
|
|
|
|
|
|
|
|
r = sd_netlink_open(&rtnl);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to connect to netlink: %m");
|
2016-02-19 18:26:18 +01:00
|
|
|
|
2019-05-25 17:22:05 +02:00
|
|
|
c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
|
2014-08-12 16:03:45 +02:00
|
|
|
if (c < 0)
|
2016-02-19 18:26:18 +01:00
|
|
|
return c;
|
|
|
|
|
2018-11-11 12:56:29 +01:00
|
|
|
(void) pager_open(arg_pager_flags);
|
2016-02-19 18:26:18 +01:00
|
|
|
|
2019-05-22 19:21:17 +02:00
|
|
|
table = table_new("IDX", "LINK", "TYPE", "OPERATIONAL", "SETUP");
|
|
|
|
if (!table)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
table_set_header(table, arg_legend);
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 0));
|
|
|
|
(void) table_set_minimum_width(table, cell, 3);
|
|
|
|
(void) table_set_weight(table, cell, 0);
|
2019-06-03 09:04:16 +02:00
|
|
|
(void) table_set_ellipsize_percent(table, cell, 100);
|
2019-05-22 19:21:17 +02:00
|
|
|
(void) table_set_align_percent(table, cell, 100);
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 1));
|
2019-06-03 09:04:16 +02:00
|
|
|
(void) table_set_ellipsize_percent(table, cell, 100);
|
2014-08-12 16:03:45 +02:00
|
|
|
|
|
|
|
for (i = 0; i < c; i++) {
|
2014-08-14 01:10:08 +02:00
|
|
|
_cleanup_free_ char *setup_state = NULL, *operational_state = NULL;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
|
2014-08-14 01:18:37 +02:00
|
|
|
const char *on_color_operational, *off_color_operational,
|
|
|
|
*on_color_setup, *off_color_setup;
|
2015-08-06 00:31:09 +02:00
|
|
|
char devid[2 + DECIMAL_STR_MAX(int)];
|
2014-08-12 01:41:42 +02:00
|
|
|
_cleanup_free_ char *t = NULL;
|
|
|
|
|
2016-02-20 21:34:38 +01:00
|
|
|
(void) sd_network_link_get_operational_state(links[i].ifindex, &operational_state);
|
2014-08-14 01:18:37 +02:00
|
|
|
operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
|
|
|
|
|
2016-02-20 22:12:14 +01:00
|
|
|
r = sd_network_link_get_setup_state(links[i].ifindex, &setup_state);
|
|
|
|
if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */
|
|
|
|
setup_state = strdup("unmanaged");
|
2014-08-14 01:18:37 +02:00
|
|
|
setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2016-02-19 19:24:16 +01:00
|
|
|
xsprintf(devid, "n%i", links[i].ifindex);
|
|
|
|
(void) sd_device_new_from_device_id(&d, devid);
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2018-01-29 21:08:39 +01:00
|
|
|
t = link_get_type_string(links[i].iftype, d);
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_INT, &links[i].ifindex);
|
2019-05-22 19:21:17 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = table_add_many(table,
|
|
|
|
TABLE_STRING, links[i].name,
|
|
|
|
TABLE_STRING, strna(t));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = table_add_cell(table, &cell, TABLE_STRING, strna(operational_state));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
(void) table_set_color(table, cell, on_color_operational);
|
|
|
|
|
|
|
|
r = table_add_cell(table, &cell, TABLE_STRING, strna(setup_state));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
(void) table_set_color(table, cell, on_color_setup);
|
2014-08-12 01:41:42 +02:00
|
|
|
}
|
|
|
|
|
2019-05-22 19:21:17 +02:00
|
|
|
r = table_print(table, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to print table: %m");
|
|
|
|
|
2014-08-12 01:41:42 +02:00
|
|
|
if (arg_legend)
|
2014-08-12 16:03:45 +02:00
|
|
|
printf("\n%i links listed.\n", c);
|
2014-08-12 01:41:42 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-02 01:05:52 +01:00
|
|
|
/* IEEE Organizationally Unique Identifier vendor string */
|
2016-02-19 19:18:12 +01:00
|
|
|
static int ieee_oui(sd_hwdb *hwdb, const struct ether_addr *mac, char **ret) {
|
2014-12-15 20:07:34 +01:00
|
|
|
const char *description;
|
2017-12-14 19:02:29 +01:00
|
|
|
char modalias[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc;
|
2014-12-15 20:07:34 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(ret);
|
2014-12-02 01:05:52 +01:00
|
|
|
|
2014-12-12 18:50:06 +01:00
|
|
|
if (!hwdb)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-12-15 20:07:34 +01:00
|
|
|
if (!mac)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-12-02 01:05:52 +01:00
|
|
|
/* skip commonly misused 00:00:00 (Xerox) prefix */
|
|
|
|
if (memcmp(mac, "\0\0\0", 3) == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2016-01-12 15:34:20 +01:00
|
|
|
xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR,
|
|
|
|
ETHER_ADDR_FORMAT_VAL(*mac));
|
2014-12-02 01:05:52 +01:00
|
|
|
|
2014-12-15 20:07:34 +01:00
|
|
|
r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-02 01:05:52 +01:00
|
|
|
|
2014-12-15 20:07:34 +01:00
|
|
|
desc = strdup(description);
|
|
|
|
if (!desc)
|
|
|
|
return -ENOMEM;
|
2014-12-02 01:05:52 +01:00
|
|
|
|
2014-12-15 20:07:34 +01:00
|
|
|
*ret = desc;
|
|
|
|
|
|
|
|
return 0;
|
2014-12-02 01:05:52 +01:00
|
|
|
}
|
|
|
|
|
2014-12-12 18:57:15 +01:00
|
|
|
static int get_gateway_description(
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink *rtnl,
|
2014-12-15 20:07:34 +01:00
|
|
|
sd_hwdb *hwdb,
|
2014-12-12 18:57:15 +01:00
|
|
|
int ifindex,
|
|
|
|
int family,
|
|
|
|
union in_addr_union *gateway,
|
|
|
|
char **gateway_description) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message *m;
|
2014-12-02 01:05:52 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(rtnl);
|
|
|
|
assert(ifindex >= 0);
|
2017-10-04 16:01:32 +02:00
|
|
|
assert(IN_SET(family, AF_INET, AF_INET6));
|
2014-12-02 01:05:52 +01:00
|
|
|
assert(gateway);
|
|
|
|
assert(gateway_description);
|
|
|
|
|
|
|
|
r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_request_dump(req, true);
|
2014-12-02 01:05:52 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_call(rtnl, req, 0, &reply);
|
2014-12-02 01:05:52 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
for (m = reply; m; m = sd_netlink_message_next(m)) {
|
2019-05-04 20:14:08 +02:00
|
|
|
union in_addr_union gw = IN_ADDR_NULL;
|
|
|
|
struct ether_addr mac = ETHER_ADDR_NULL;
|
2014-12-02 01:05:52 +01:00
|
|
|
uint16_t type;
|
|
|
|
int ifi, fam;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_get_errno(m);
|
2014-12-02 01:05:52 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "got error: %m");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_get_type(m, &type);
|
2014-12-02 01:05:52 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not get type: %m");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type != RTM_NEWNEIGH) {
|
|
|
|
log_error("type is not RTM_NEWNEIGH");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_rtnl_message_neigh_get_family(m, &fam);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not get family: %m");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fam != family) {
|
|
|
|
log_error("family is not correct");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_rtnl_message_neigh_get_ifindex(m, &ifi);
|
|
|
|
if (r < 0) {
|
2014-12-03 20:59:00 +01:00
|
|
|
log_error_errno(r, "could not get ifindex: %m");
|
2014-12-02 01:05:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ifindex > 0 && ifi != ifindex)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (fam) {
|
|
|
|
case AF_INET:
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_read_in_addr(m, NDA_DST, &gw.in);
|
2014-12-02 01:05:52 +01:00
|
|
|
if (r < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_read_in6_addr(m, NDA_DST, &gw.in6);
|
2014-12-02 01:05:52 +01:00
|
|
|
if (r < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_addr_equal(fam, &gw, gateway))
|
|
|
|
continue;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_read_ether_addr(m, NDA_LLADDR, &mac);
|
2014-12-02 01:05:52 +01:00
|
|
|
if (r < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = ieee_oui(hwdb, &mac, gateway_description);
|
|
|
|
if (r < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODATA;
|
|
|
|
}
|
|
|
|
|
2014-12-12 18:57:15 +01:00
|
|
|
static int dump_gateways(
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink *rtnl,
|
2014-12-15 20:07:34 +01:00
|
|
|
sd_hwdb *hwdb,
|
2019-05-22 18:43:21 +02:00
|
|
|
Table *table,
|
2014-12-12 18:57:15 +01:00
|
|
|
int ifindex) {
|
2014-12-04 12:19:27 +01:00
|
|
|
_cleanup_free_ struct local_address *local = NULL;
|
|
|
|
int r, n, i;
|
2014-12-02 01:05:52 +01:00
|
|
|
|
2016-02-19 19:50:14 +01:00
|
|
|
assert(rtnl);
|
2019-05-22 18:43:21 +02:00
|
|
|
assert(table);
|
2016-02-19 19:50:14 +01:00
|
|
|
|
2014-12-04 12:19:27 +01:00
|
|
|
n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local);
|
|
|
|
if (n < 0)
|
|
|
|
return n;
|
2014-12-02 01:05:52 +01:00
|
|
|
|
2014-12-04 12:19:27 +01:00
|
|
|
for (i = 0; i < n; i++) {
|
2019-05-22 18:43:21 +02:00
|
|
|
_cleanup_free_ char *gateway = NULL, *description = NULL, *with_description = NULL;
|
|
|
|
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, i == 0 ? "Gateway:" : "");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-02 01:05:52 +01:00
|
|
|
|
2014-12-04 12:19:27 +01:00
|
|
|
r = in_addr_to_string(local[i].family, &local[i].address, &gateway);
|
2014-12-02 01:05:52 +01:00
|
|
|
if (r < 0)
|
2014-12-04 12:19:27 +01:00
|
|
|
return r;
|
2014-12-02 01:05:52 +01:00
|
|
|
|
2014-12-12 18:57:15 +01:00
|
|
|
r = get_gateway_description(rtnl, hwdb, local[i].ifindex, local[i].family, &local[i].address, &description);
|
2014-12-02 01:05:52 +01:00
|
|
|
if (r < 0)
|
2014-12-04 12:19:27 +01:00
|
|
|
log_debug_errno(r, "Could not get description of gateway: %m");
|
2014-12-02 01:05:52 +01:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
if (description) {
|
|
|
|
with_description = strjoin(gateway, " (", description, ")");
|
|
|
|
if (!with_description)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2014-12-12 19:07:26 +01:00
|
|
|
|
|
|
|
/* Show interface name for the entry if we show
|
|
|
|
* entries for all interfaces */
|
|
|
|
if (ifindex <= 0) {
|
2019-05-29 07:18:41 +02:00
|
|
|
char name[IF_NAMESIZE+1];
|
2019-05-22 18:43:21 +02:00
|
|
|
|
2019-05-29 07:18:41 +02:00
|
|
|
if (format_ifname(local[i].ifindex, name))
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell_stringf(table, NULL, "%s on %s", with_description ?: gateway, name);
|
|
|
|
else
|
|
|
|
r = table_add_cell_stringf(table, NULL, "%s on %%%i", with_description ?: gateway, local[i].ifindex);
|
|
|
|
} else
|
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, with_description ?: gateway);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-02 01:05:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-12 18:57:15 +01:00
|
|
|
static int dump_addresses(
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink *rtnl,
|
2019-05-22 18:43:21 +02:00
|
|
|
Table *table,
|
2014-12-12 18:57:15 +01:00
|
|
|
int ifindex) {
|
|
|
|
|
2014-08-12 01:41:42 +02:00
|
|
|
_cleanup_free_ struct local_address *local = NULL;
|
|
|
|
int r, n, i;
|
|
|
|
|
2016-02-19 19:50:14 +01:00
|
|
|
assert(rtnl);
|
2019-05-22 18:43:21 +02:00
|
|
|
assert(table);
|
2016-02-19 19:50:14 +01:00
|
|
|
|
2014-12-04 01:41:12 +01:00
|
|
|
n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local);
|
2014-08-12 01:41:42 +02:00
|
|
|
if (n < 0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
_cleanup_free_ char *pretty = NULL;
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
2014-08-12 01:41:42 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, i == 0 ? "Address:" : "");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-12 19:07:26 +01:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
r = in_addr_to_string(local[i].family, &local[i].address, &pretty);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-12 19:07:26 +01:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
if (ifindex <= 0) {
|
2019-05-29 07:18:41 +02:00
|
|
|
char name[IF_NAMESIZE+1];
|
2019-05-22 18:43:21 +02:00
|
|
|
|
2019-05-29 07:18:41 +02:00
|
|
|
if (format_ifname(local[i].ifindex, name))
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell_stringf(table, NULL, "%s on %s", pretty, name);
|
|
|
|
else
|
|
|
|
r = table_add_cell_stringf(table, NULL, "%s on %%%i", pretty, local[i].ifindex);
|
|
|
|
} else
|
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, pretty);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-08-12 01:41:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-26 19:58:10 +02:00
|
|
|
static int dump_address_labels(sd_netlink *rtnl) {
|
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
|
2019-05-22 22:03:42 +02:00
|
|
|
_cleanup_(table_unrefp) Table *table = NULL;
|
2017-06-26 19:58:10 +02:00
|
|
|
sd_netlink_message *m;
|
2019-05-22 22:03:42 +02:00
|
|
|
TableCell *cell;
|
2017-06-26 19:58:10 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(rtnl);
|
|
|
|
|
|
|
|
r = sd_rtnl_message_new_addrlabel(rtnl, &req, RTM_GETADDRLABEL, 0, AF_INET6);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m");
|
|
|
|
|
|
|
|
r = sd_netlink_message_request_dump(req, true);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_netlink_call(rtnl, req, 0, &reply);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2019-05-22 22:03:42 +02:00
|
|
|
table = table_new("Label", "Prefix/Prefixlen");
|
|
|
|
if (!table)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
r = table_set_sort(table, 0, SIZE_MAX);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 0));
|
|
|
|
(void) table_set_align_percent(table, cell, 100);
|
2019-06-03 09:04:16 +02:00
|
|
|
(void) table_set_ellipsize_percent(table, cell, 100);
|
2019-05-22 22:03:42 +02:00
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 1));
|
|
|
|
(void) table_set_align_percent(table, cell, 100);
|
2017-06-26 19:58:10 +02:00
|
|
|
|
|
|
|
for (m = reply; m; m = sd_netlink_message_next(m)) {
|
|
|
|
_cleanup_free_ char *pretty = NULL;
|
2019-05-04 20:14:08 +02:00
|
|
|
union in_addr_union prefix = IN_ADDR_NULL;
|
2017-06-26 19:58:10 +02:00
|
|
|
uint8_t prefixlen;
|
|
|
|
uint32_t label;
|
|
|
|
|
|
|
|
r = sd_netlink_message_get_errno(m);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "got error: %m");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_netlink_message_read_u32(m, IFAL_LABEL, &label);
|
|
|
|
if (r < 0 && r != -ENODATA) {
|
|
|
|
log_error_errno(r, "Could not read IFAL_LABEL, ignoring: %m");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix.in6);
|
|
|
|
if (r < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = in_addr_to_string(AF_INET6, &prefix, &pretty);
|
|
|
|
if (r < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = sd_rtnl_message_addrlabel_get_prefixlen(m, &prefixlen);
|
|
|
|
if (r < 0)
|
|
|
|
continue;
|
|
|
|
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_UINT32, &label);
|
2019-05-22 22:03:42 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = table_add_cell_stringf(table, &cell, "%s/%u", pretty, prefixlen);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2017-06-26 19:58:10 +02:00
|
|
|
}
|
|
|
|
|
2019-05-22 22:03:42 +02:00
|
|
|
return table_print(table, NULL);
|
2017-06-26 19:58:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int list_address_labels(int argc, char *argv[], void *userdata) {
|
|
|
|
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = sd_netlink_open(&rtnl);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to connect to netlink: %m");
|
|
|
|
|
|
|
|
dump_address_labels(rtnl);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-19 19:50:14 +01:00
|
|
|
static int open_lldp_neighbors(int ifindex, FILE **ret) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (asprintf(&p, "/run/systemd/netif/lldp/%i", ifindex) < 0)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
f = fopen(p, "re");
|
|
|
|
if (!f)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
*ret = f;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int next_lldp_neighbor(FILE *f, sd_lldp_neighbor **ret) {
|
|
|
|
_cleanup_free_ void *raw = NULL;
|
|
|
|
size_t l;
|
|
|
|
le64_t u;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(f);
|
|
|
|
assert(ret);
|
|
|
|
|
|
|
|
l = fread(&u, 1, sizeof(u), f);
|
|
|
|
if (l == 0 && feof(f))
|
|
|
|
return 0;
|
|
|
|
if (l != sizeof(u))
|
|
|
|
return -EBADMSG;
|
|
|
|
|
2018-06-07 22:46:32 +02:00
|
|
|
/* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
|
|
|
|
if (le64toh(u) >= 4096)
|
|
|
|
return -EBADMSG;
|
|
|
|
|
2016-02-19 19:50:14 +01:00
|
|
|
raw = new(uint8_t, le64toh(u));
|
|
|
|
if (!raw)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (fread(raw, 1, le64toh(u), f) != le64toh(u))
|
|
|
|
return -EBADMSG;
|
|
|
|
|
|
|
|
r = sd_lldp_neighbor_from_raw(ret, raw, le64toh(u));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
static int dump_lldp_neighbors(Table *table, const char *prefix, int ifindex) {
|
2016-02-19 19:50:14 +01:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
|
|
|
int r, c = 0;
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
assert(table);
|
2016-02-19 19:50:14 +01:00
|
|
|
assert(prefix);
|
|
|
|
assert(ifindex > 0);
|
|
|
|
|
|
|
|
r = open_lldp_neighbors(ifindex, &f);
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return 0;
|
2016-02-19 19:50:14 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const char *system_name = NULL, *port_id = NULL, *port_description = NULL;
|
|
|
|
_cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
|
2019-05-22 18:43:21 +02:00
|
|
|
_cleanup_free_ char *str = NULL;
|
2016-02-19 19:50:14 +01:00
|
|
|
|
|
|
|
r = next_lldp_neighbor(f, &n);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
break;
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, c == 0 ? prefix : "");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 19:50:14 +01:00
|
|
|
|
|
|
|
(void) sd_lldp_neighbor_get_system_name(n, &system_name);
|
|
|
|
(void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
|
|
|
|
(void) sd_lldp_neighbor_get_port_description(n, &port_description);
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
if (asprintf(&str, "%s on port %s%s%s%s",
|
|
|
|
strna(system_name), strna(port_id),
|
|
|
|
isempty(port_description) ? "" : " (",
|
|
|
|
port_description,
|
|
|
|
isempty(port_description) ? "" : ")") < 0)
|
|
|
|
return -ENOMEM;
|
2016-02-19 19:50:14 +01:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, str);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 19:50:14 +01:00
|
|
|
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) {
|
2016-02-19 20:43:03 +01:00
|
|
|
unsigned c;
|
2019-05-22 18:43:21 +02:00
|
|
|
int r;
|
2016-02-19 20:43:03 +01:00
|
|
|
|
|
|
|
assert(prefix);
|
|
|
|
|
|
|
|
if (!ifindexes || ifindexes[0] <= 0)
|
2019-05-22 18:43:21 +02:00
|
|
|
return 0;
|
2016-02-19 20:43:03 +01:00
|
|
|
|
|
|
|
for (c = 0; ifindexes[c] > 0; c++) {
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 20:43:03 +01:00
|
|
|
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, c == 0 ? prefix : "");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 20:43:03 +01:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_IFINDEX, ifindexes + c);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 20:43:03 +01:00
|
|
|
}
|
2019-05-22 18:43:21 +02:00
|
|
|
|
|
|
|
return 0;
|
2016-02-19 20:43:03 +01:00
|
|
|
}
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
static int dump_list(Table *table, const char *prefix, char **l) {
|
2014-08-12 01:41:42 +02:00
|
|
|
char **i;
|
2019-05-22 18:43:21 +02:00
|
|
|
int r;
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2016-01-25 20:14:58 +01:00
|
|
|
if (strv_isempty(l))
|
2019-05-22 18:43:21 +02:00
|
|
|
return 0;
|
2016-01-25 20:14:58 +01:00
|
|
|
|
2014-08-12 01:41:42 +02:00
|
|
|
STRV_FOREACH(i, l) {
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, i == l ? prefix : "");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, *i);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-08-12 01:41:42 +02:00
|
|
|
}
|
2019-05-22 18:43:21 +02:00
|
|
|
|
|
|
|
return 0;
|
2014-08-12 01:41:42 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 17:37:48 +02:00
|
|
|
#define DUMP_STATS_ONE(name, val_name) \
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL); \
|
|
|
|
if (r < 0) \
|
|
|
|
return r; \
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, name ":"); \
|
2019-05-24 17:37:48 +02:00
|
|
|
if (r < 0) \
|
|
|
|
return r; \
|
|
|
|
r = table_add_cell(table, NULL, info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
|
|
|
|
info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
|
|
|
|
if (r < 0) \
|
|
|
|
return r;
|
|
|
|
|
|
|
|
static int dump_statistics(Table *table, const LinkInfo *info) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!arg_stats)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!info->has_stats64 && !info->has_stats)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
DUMP_STATS_ONE("Rx Packets", rx_packets);
|
|
|
|
DUMP_STATS_ONE("Tx Packets", tx_packets);
|
|
|
|
DUMP_STATS_ONE("Rx Bytes", rx_bytes);
|
|
|
|
DUMP_STATS_ONE("Tx Bytes", tx_bytes);
|
|
|
|
DUMP_STATS_ONE("Rx Errors", rx_errors);
|
|
|
|
DUMP_STATS_ONE("Tx Errors", tx_errors);
|
|
|
|
DUMP_STATS_ONE("Rx Dropped", rx_dropped);
|
|
|
|
DUMP_STATS_ONE("Tx Dropped", tx_dropped);
|
|
|
|
DUMP_STATS_ONE("Multicast Packets", multicast);
|
|
|
|
DUMP_STATS_ONE("Collisions", collisions);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-25 17:22:05 +02:00
|
|
|
static const struct {
|
|
|
|
double val;
|
|
|
|
const char *str;
|
|
|
|
} prefix_table[] = {
|
|
|
|
{ .val = 1e15, .str = "P" },
|
|
|
|
{ .val = 1e12, .str = "T" },
|
|
|
|
{ .val = 1e9, .str = "G" },
|
|
|
|
{ .val = 1e6, .str = "M" },
|
|
|
|
{ .val = 1e3, .str = "k" },
|
|
|
|
};
|
|
|
|
|
|
|
|
static void get_prefix(double val, double *ret_div, const char **ret_prefix) {
|
|
|
|
assert(ret_div);
|
|
|
|
assert(ret_prefix);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ELEMENTSOF(prefix_table); i++)
|
|
|
|
if (val > prefix_table[i].val) {
|
|
|
|
*ret_div = prefix_table[i].val;
|
|
|
|
*ret_prefix = prefix_table[i].str;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret_div = 1;
|
|
|
|
*ret_prefix = NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-12 18:57:15 +01:00
|
|
|
static int link_status_one(
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink *rtnl,
|
2014-12-15 20:07:34 +01:00
|
|
|
sd_hwdb *hwdb,
|
2016-02-19 19:18:12 +01:00
|
|
|
const LinkInfo *info) {
|
|
|
|
|
networkd: rework Domains= setting
Previously, .network files only knew a vaguely defined "Domains=" concept, for which the documentation declared it was
the "DNS domain" for the network connection, without specifying what that means.
With this the Domains setting is reworked, so that there are now "routing" domains and "search" domains. The former are
to be used by resolved to route DNS request to specific network interfaces, the latter is to be used for searching
single-label hostnames with (in addition to being used for routing). Both settings are configured in the "Domains="
setting. Normal domain names listed in it are now considered search domains (for compatibility with existing setups),
while those prefixed with "~" are considered routing domains only. To route all lookups to a specific interface the
routing domain "." may be used, referring to the root domain. An alternative syntax for this is the "*", as was already
implemented before using the "wildcard" domain concept.
This commit adds proper parsers for this new logic, and exposes this via the sd-network API. This information is not
used by resolved yet, this will be added in a later commit.
2016-01-25 19:46:00 +01:00
|
|
|
_cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
|
2015-08-30 03:18:33 +02:00
|
|
|
_cleanup_free_ char *setup_state = NULL, *operational_state = NULL, *tz = NULL;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
|
2014-08-12 15:19:30 +02:00
|
|
|
char devid[2 + DECIMAL_STR_MAX(int)];
|
2014-09-08 14:00:34 +02:00
|
|
|
_cleanup_free_ char *t = NULL, *network = NULL;
|
2014-09-08 14:18:32 +02:00
|
|
|
const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL;
|
2014-08-14 01:18:37 +02:00
|
|
|
const char *on_color_operational, *off_color_operational,
|
2019-05-22 18:43:21 +02:00
|
|
|
*on_color_setup, *off_color_setup;
|
2016-02-19 20:43:03 +01:00
|
|
|
_cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL;
|
2019-05-22 18:43:21 +02:00
|
|
|
_cleanup_(table_unrefp) Table *table = NULL;
|
|
|
|
TableCell *cell;
|
2016-02-19 19:18:12 +01:00
|
|
|
int r;
|
2014-08-12 15:19:30 +02:00
|
|
|
|
|
|
|
assert(rtnl);
|
2016-02-19 19:18:12 +01:00
|
|
|
assert(info);
|
2014-08-12 15:19:30 +02:00
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
(void) sd_network_link_get_operational_state(info->ifindex, &operational_state);
|
2014-08-14 01:18:37 +02:00
|
|
|
operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
|
|
|
|
|
2016-02-20 22:12:14 +01:00
|
|
|
r = sd_network_link_get_setup_state(info->ifindex, &setup_state);
|
|
|
|
if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */
|
|
|
|
setup_state = strdup("unmanaged");
|
2014-08-14 01:18:37 +02:00
|
|
|
setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
|
2014-08-12 15:19:30 +02:00
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
(void) sd_network_link_get_dns(info->ifindex, &dns);
|
|
|
|
(void) sd_network_link_get_search_domains(info->ifindex, &search_domains);
|
|
|
|
(void) sd_network_link_get_route_domains(info->ifindex, &route_domains);
|
|
|
|
(void) sd_network_link_get_ntp(info->ifindex, &ntp);
|
2014-08-12 15:19:30 +02:00
|
|
|
|
2016-02-19 19:24:16 +01:00
|
|
|
xsprintf(devid, "n%i", info->ifindex);
|
2014-12-05 01:16:05 +01:00
|
|
|
|
2016-01-25 20:14:58 +01:00
|
|
|
(void) sd_device_new_from_device_id(&d, devid);
|
2014-12-05 01:16:05 +01:00
|
|
|
|
2014-08-12 15:19:30 +02:00
|
|
|
if (d) {
|
2016-01-25 20:14:58 +01:00
|
|
|
(void) sd_device_get_property_value(d, "ID_NET_LINK_FILE", &link);
|
|
|
|
(void) sd_device_get_property_value(d, "ID_NET_DRIVER", &driver);
|
|
|
|
(void) sd_device_get_property_value(d, "ID_PATH", &path);
|
2014-08-12 15:19:30 +02:00
|
|
|
|
2018-09-01 16:12:47 +02:00
|
|
|
if (sd_device_get_property_value(d, "ID_VENDOR_FROM_DATABASE", &vendor) < 0)
|
2016-01-25 20:14:58 +01:00
|
|
|
(void) sd_device_get_property_value(d, "ID_VENDOR", &vendor);
|
2014-08-12 15:19:30 +02:00
|
|
|
|
2018-09-01 16:12:47 +02:00
|
|
|
if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) < 0)
|
2016-01-25 20:14:58 +01:00
|
|
|
(void) sd_device_get_property_value(d, "ID_MODEL", &model);
|
2014-08-12 15:19:30 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:08:39 +01:00
|
|
|
t = link_get_type_string(info->iftype, d);
|
2014-12-12 19:11:35 +01:00
|
|
|
|
2016-02-20 21:34:38 +01:00
|
|
|
(void) sd_network_link_get_network_file(info->ifindex, &network);
|
2014-08-12 15:41:01 +02:00
|
|
|
|
2016-02-20 21:34:38 +01:00
|
|
|
(void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to);
|
|
|
|
(void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by);
|
2015-02-17 13:06:57 +01:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
table = table_new("DOT", "KEY", "VALUE");
|
|
|
|
if (!table)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-06-03 09:04:16 +02:00
|
|
|
assert_se(cell = table_get_cell(table, 0, 0));
|
|
|
|
(void) table_set_ellipsize_percent(table, cell, 100);
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 1));
|
|
|
|
(void) table_set_ellipsize_percent(table, cell, 100);
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
table_set_header(table, false);
|
|
|
|
|
|
|
|
r = table_add_cell(table, &cell, TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
(void) table_set_color(table, cell, on_color_operational);
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell_stringf(table, &cell, "%i: %s", info->ifindex, info->name);
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
(void) table_set_align_percent(table, cell, 0);
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, &cell, TABLE_STRING, "Link File:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
(void) table_set_align_percent(table, cell, 100);
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, strna(link));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "Network File:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, strna(network));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "Type:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, strna(t));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "State:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return 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);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "Path:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, path);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (driver) {
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "Driver:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, driver);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (vendor) {
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "Vendor:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, vendor);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (model) {
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "Model:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, model);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2014-08-12 15:19:30 +02:00
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
if (info->has_mac_address) {
|
2014-12-12 18:50:06 +01:00
|
|
|
_cleanup_free_ char *description = NULL;
|
2014-08-15 13:18:50 +02:00
|
|
|
char ea[ETHER_ADDR_TO_STRING_MAX];
|
2014-12-12 18:50:06 +01:00
|
|
|
|
2016-02-20 21:34:38 +01:00
|
|
|
(void) ieee_oui(hwdb, &info->mac_address, &description);
|
2014-12-12 18:50:06 +01:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "HW Address:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
|
|
|
|
ether_addr_to_string(&info->mac_address, ea),
|
|
|
|
description ? " (" : "",
|
|
|
|
description,
|
|
|
|
description ? ")" : "");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-08-15 13:18:50 +02:00
|
|
|
}
|
2014-08-12 15:19:30 +02:00
|
|
|
|
2019-05-30 20:30:31 +02:00
|
|
|
if (info->mtu > 0) {
|
|
|
|
char min_str[DECIMAL_STR_MAX(uint32_t)], max_str[DECIMAL_STR_MAX(uint32_t)];
|
|
|
|
|
|
|
|
xsprintf(min_str, "%" PRIu32, info->min_mtu);
|
|
|
|
xsprintf(max_str, "%" PRIu32, info->max_mtu);
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "MTU:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-05-30 20:30:31 +02:00
|
|
|
r = table_add_cell_stringf(table, NULL, "%" PRIu32 "%s%s%s%s%s%s%s",
|
|
|
|
info->mtu,
|
|
|
|
info->min_mtu > 0 || info->max_mtu > 0 ? " (" : "",
|
2019-05-31 10:56:45 +02:00
|
|
|
info->min_mtu > 0 ? "min: " : "",
|
2019-05-30 20:30:31 +02:00
|
|
|
info->min_mtu > 0 ? min_str : "",
|
|
|
|
info->min_mtu > 0 && info->max_mtu > 0 ? ", " : "",
|
2019-05-31 10:56:45 +02:00
|
|
|
info->max_mtu > 0 ? "max: " : "",
|
2019-05-30 20:30:31 +02:00
|
|
|
info->max_mtu > 0 ? max_str : "",
|
|
|
|
info->min_mtu > 0 || info->max_mtu > 0 ? ")" : "");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2015-08-26 19:19:32 +02:00
|
|
|
|
2019-05-25 17:22:05 +02:00
|
|
|
if (info->has_bitrates) {
|
|
|
|
const char *tx_prefix, *rx_prefix;
|
|
|
|
double tx_div, rx_div;
|
|
|
|
|
|
|
|
get_prefix(info->tx_bitrate, &tx_div, &tx_prefix);
|
|
|
|
get_prefix(info->rx_bitrate, &rx_div, &rx_prefix);
|
|
|
|
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "Bit Rate (Tx/Rx):");
|
2019-05-25 17:22:05 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = table_add_cell_stringf(table, NULL, "%.4g %sbps/%.4g %sbps",
|
|
|
|
info->tx_bitrate / tx_div, strempty(tx_prefix),
|
|
|
|
info->rx_bitrate / rx_div, strempty(rx_prefix));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
if (info->has_tx_queues || info->has_rx_queues) {
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "Queue Length (Tx/Rx):");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2015-02-17 13:06:57 +01:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
r = dump_addresses(rtnl, table, info->ifindex);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = dump_gateways(rtnl, hwdb, table, info->ifindex);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = dump_list(table, "DNS:", dns);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = dump_list(table, "Search Domains:", search_domains);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = dump_list(table, "Route Domains:", route_domains);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = dump_list(table, "NTP:", ntp);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = dump_ifindexes(table, "Carrier Bound To:", carrier_bound_to);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = dump_ifindexes(table, "Carrier Bound By:", carrier_bound_by);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-08-12 15:19:30 +02:00
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
(void) sd_network_link_get_timezone(info->ifindex, &tz);
|
2019-05-22 18:43:21 +02:00
|
|
|
if (tz) {
|
|
|
|
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "Time Zone:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, tz);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2015-08-26 19:19:32 +02:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
r = dump_lldp_neighbors(table, "Connected To:", info->ifindex);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 19:50:14 +01:00
|
|
|
|
2019-05-24 17:37:48 +02:00
|
|
|
r = dump_statistics(table, info);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
return table_print(table, NULL);
|
2014-08-12 15:19:30 +02:00
|
|
|
}
|
|
|
|
|
2016-02-19 18:20:40 +01:00
|
|
|
static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) {
|
|
|
|
_cleanup_free_ char *operational_state = NULL;
|
|
|
|
_cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
|
|
|
|
const char *on_color_operational, *off_color_operational;
|
2019-05-22 18:43:21 +02:00
|
|
|
_cleanup_(table_unrefp) Table *table = NULL;
|
|
|
|
TableCell *cell;
|
|
|
|
int r;
|
2016-02-19 18:20:40 +01:00
|
|
|
|
|
|
|
assert(rtnl);
|
|
|
|
|
2016-02-20 21:34:38 +01:00
|
|
|
(void) sd_network_get_operational_state(&operational_state);
|
2016-02-19 18:20:40 +01:00
|
|
|
operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
table = table_new("DOT", "KEY", "VALUE");
|
|
|
|
if (!table)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-06-03 09:04:16 +02:00
|
|
|
assert_se(cell = table_get_cell(table, 0, 0));
|
|
|
|
(void) table_set_ellipsize_percent(table, cell, 100);
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 1));
|
|
|
|
(void) table_set_align_percent(table, cell, 100);
|
|
|
|
(void) table_set_ellipsize_percent(table, cell, 100);
|
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
table_set_header(table, false);
|
|
|
|
|
|
|
|
r = table_add_cell(table, &cell, TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
(void) table_set_color(table, cell, on_color_operational);
|
|
|
|
|
2019-06-03 09:04:16 +02:00
|
|
|
r = table_add_cell(table, NULL, TABLE_STRING, "State:");
|
2019-05-22 18:43:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = table_add_cell(table, &cell, TABLE_STRING, strna(operational_state));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
(void) table_set_color(table, cell, on_color_operational);
|
2016-02-19 18:20:40 +01:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
r = dump_addresses(rtnl, table, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = dump_gateways(rtnl, hwdb, table, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 18:20:40 +01:00
|
|
|
|
2016-02-20 21:34:38 +01:00
|
|
|
(void) sd_network_get_dns(&dns);
|
2019-05-22 18:43:21 +02:00
|
|
|
r = dump_list(table, "DNS:", dns);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 18:20:40 +01:00
|
|
|
|
2016-02-20 21:34:38 +01:00
|
|
|
(void) sd_network_get_search_domains(&search_domains);
|
2019-05-22 18:43:21 +02:00
|
|
|
r = dump_list(table, "Search Domains:", search_domains);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 18:20:40 +01:00
|
|
|
|
2016-02-20 21:34:38 +01:00
|
|
|
(void) sd_network_get_route_domains(&route_domains);
|
2019-05-22 18:43:21 +02:00
|
|
|
r = dump_list(table, "Route Domains:", route_domains);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 18:20:40 +01:00
|
|
|
|
2016-02-20 21:34:38 +01:00
|
|
|
(void) sd_network_get_ntp(&ntp);
|
2019-05-22 18:43:21 +02:00
|
|
|
r = dump_list(table, "NTP:", ntp);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 18:20:40 +01:00
|
|
|
|
2019-05-22 18:43:21 +02:00
|
|
|
return table_print(table, NULL);
|
2016-02-19 18:20:40 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 03:16:45 +01:00
|
|
|
static int link_status(int argc, char *argv[], void *userdata) {
|
2019-05-25 17:22:05 +02:00
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
2016-02-19 19:18:12 +01:00
|
|
|
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
|
|
|
|
_cleanup_free_ LinkInfo *links = NULL;
|
|
|
|
int r, c, i;
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2018-11-11 12:56:29 +01:00
|
|
|
(void) pager_open(arg_pager_flags);
|
2016-02-19 18:20:40 +01:00
|
|
|
|
2019-05-25 17:22:05 +02:00
|
|
|
r = sd_bus_open_system(&bus);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to connect system bus: %m");
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_open(&rtnl);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to connect to netlink: %m");
|
2014-08-12 16:19:37 +02:00
|
|
|
|
2014-12-15 20:07:34 +01:00
|
|
|
r = sd_hwdb_new(&hwdb);
|
|
|
|
if (r < 0)
|
|
|
|
log_debug_errno(r, "Failed to open hardware database: %m");
|
2014-12-12 18:57:15 +01:00
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
if (arg_all)
|
2019-05-25 17:22:05 +02:00
|
|
|
c = acquire_link_info(bus, rtnl, NULL, &links);
|
2016-02-19 19:18:12 +01:00
|
|
|
else if (argc <= 1)
|
2016-02-19 18:20:40 +01:00
|
|
|
return system_status(rtnl, hwdb);
|
2016-02-19 19:18:12 +01:00
|
|
|
else
|
2019-05-25 17:22:05 +02:00
|
|
|
c = acquire_link_info(bus, rtnl, argv + 1, &links);
|
2016-02-19 19:18:12 +01:00
|
|
|
if (c < 0)
|
|
|
|
return c;
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
for (i = 0; i < c; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
fputc('\n', stdout);
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
link_status_one(rtnl, hwdb, links + i);
|
2014-08-12 01:41:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
static char *lldp_capabilities_to_string(uint16_t x) {
|
|
|
|
static const char characters[] = {
|
|
|
|
'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
|
|
|
|
};
|
|
|
|
char *ret;
|
|
|
|
unsigned i;
|
2014-12-11 05:29:55 +01:00
|
|
|
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
ret = new(char, ELEMENTSOF(characters) + 1);
|
|
|
|
if (!ret)
|
2014-12-11 05:29:55 +01:00
|
|
|
return NULL;
|
|
|
|
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
for (i = 0; i < ELEMENTSOF(characters); i++)
|
|
|
|
ret[i] = (x & (1U << i)) ? characters[i] : '.';
|
2014-12-11 05:29:55 +01:00
|
|
|
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
ret[i] = 0;
|
|
|
|
return ret;
|
2014-12-11 05:29:55 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 05:23:56 +01:00
|
|
|
static void lldp_capabilities_legend(uint16_t x) {
|
|
|
|
unsigned w, i, cols = columns();
|
2016-02-23 20:25:48 +01:00
|
|
|
static const char* const table[] = {
|
2016-02-23 05:23:56 +01:00
|
|
|
"o - Other",
|
|
|
|
"p - Repeater",
|
|
|
|
"b - Bridge",
|
|
|
|
"w - WLAN Access Point",
|
|
|
|
"r - Router",
|
|
|
|
"t - Telephone",
|
|
|
|
"d - DOCSIS cable device",
|
|
|
|
"a - Station",
|
|
|
|
"c - Customer VLAN",
|
|
|
|
"s - Service VLAN",
|
|
|
|
"m - Two-port MAC Relay (TPMR)",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (x == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("\nCapability Flags:\n");
|
|
|
|
for (w = 0, i = 0; i < ELEMENTSOF(table); i++)
|
|
|
|
if (x & (1U << i) || arg_all) {
|
|
|
|
bool newline;
|
|
|
|
|
|
|
|
newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols;
|
|
|
|
if (newline)
|
|
|
|
w = 0;
|
|
|
|
w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]);
|
|
|
|
}
|
|
|
|
puts("");
|
|
|
|
}
|
|
|
|
|
2014-12-11 05:29:55 +01:00
|
|
|
static int link_lldp_status(int argc, char *argv[], void *userdata) {
|
2016-02-19 19:18:12 +01:00
|
|
|
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
2014-12-11 05:29:55 +01:00
|
|
|
_cleanup_free_ LinkInfo *links = NULL;
|
2019-05-22 22:13:13 +02:00
|
|
|
_cleanup_(table_unrefp) Table *table = NULL;
|
2016-02-19 19:18:12 +01:00
|
|
|
int i, r, c, m = 0;
|
2016-02-23 05:23:56 +01:00
|
|
|
uint16_t all = 0;
|
2019-05-22 22:13:13 +02:00
|
|
|
TableCell *cell;
|
2016-02-19 19:18:12 +01:00
|
|
|
|
|
|
|
r = sd_netlink_open(&rtnl);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to connect to netlink: %m");
|
2014-12-11 05:29:55 +01:00
|
|
|
|
2019-05-25 17:22:05 +02:00
|
|
|
c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
|
2014-12-11 05:29:55 +01:00
|
|
|
if (c < 0)
|
2016-02-19 18:26:18 +01:00
|
|
|
return c;
|
|
|
|
|
2018-11-11 12:56:29 +01:00
|
|
|
(void) pager_open(arg_pager_flags);
|
2014-12-11 05:29:55 +01:00
|
|
|
|
2019-05-22 22:13:13 +02:00
|
|
|
table = table_new("LINK",
|
|
|
|
"CHASSIS ID",
|
|
|
|
"SYSTEM NAME",
|
|
|
|
"CAPS",
|
|
|
|
"PORT ID",
|
|
|
|
"PORT DESCRIPTION");
|
|
|
|
if (!table)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
table_set_header(table, arg_legend);
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 0));
|
|
|
|
table_set_minimum_width(table, cell, 16);
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 1));
|
|
|
|
table_set_minimum_width(table, cell, 17);
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 2));
|
|
|
|
table_set_minimum_width(table, cell, 16);
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 3));
|
|
|
|
table_set_minimum_width(table, cell, 11);
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 4));
|
|
|
|
table_set_minimum_width(table, cell, 17);
|
|
|
|
|
|
|
|
assert_se(cell = table_get_cell(table, 0, 5));
|
|
|
|
table_set_minimum_width(table, cell, 16);
|
2014-12-11 05:29:55 +01:00
|
|
|
|
2016-02-19 19:18:12 +01:00
|
|
|
for (i = 0; i < c; i++) {
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2014-12-11 05:29:55 +01:00
|
|
|
|
2016-02-19 19:50:14 +01:00
|
|
|
r = open_lldp_neighbors(links[i].ifindex, &f);
|
|
|
|
if (r == -ENOENT)
|
|
|
|
continue;
|
|
|
|
if (r < 0) {
|
|
|
|
log_warning_errno(r, "Failed to open LLDP data for %i, ignoring: %m", links[i].ifindex);
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-11 05:29:55 +01:00
|
|
|
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
for (;;) {
|
2019-05-30 13:58:33 +02:00
|
|
|
_cleanup_free_ char *cid = NULL, *pid = NULL, *sname = NULL, *pdesc = NULL, *capabilities = NULL;
|
|
|
|
const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL;
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
_cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
|
|
|
|
uint16_t cc;
|
2014-12-11 05:29:55 +01:00
|
|
|
|
2016-02-19 19:50:14 +01:00
|
|
|
r = next_lldp_neighbor(f, &n);
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
if (r < 0) {
|
2016-02-19 19:50:14 +01:00
|
|
|
log_warning_errno(r, "Failed to read neighbor data: %m");
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
break;
|
2014-12-11 05:29:55 +01:00
|
|
|
}
|
2016-02-19 19:50:14 +01:00
|
|
|
if (r == 0)
|
|
|
|
break;
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
|
|
|
|
(void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id);
|
|
|
|
(void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
|
|
|
|
(void) sd_lldp_neighbor_get_system_name(n, &system_name);
|
|
|
|
(void) sd_lldp_neighbor_get_port_description(n, &port_description);
|
|
|
|
|
2016-02-21 14:31:51 +01:00
|
|
|
if (chassis_id) {
|
|
|
|
cid = ellipsize(chassis_id, 17, 100);
|
|
|
|
if (cid)
|
|
|
|
chassis_id = cid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port_id) {
|
|
|
|
pid = ellipsize(port_id, 17, 100);
|
|
|
|
if (pid)
|
|
|
|
port_id = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (system_name) {
|
|
|
|
sname = ellipsize(system_name, 16, 100);
|
|
|
|
if (sname)
|
|
|
|
system_name = sname;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port_description) {
|
|
|
|
pdesc = ellipsize(port_description, 16, 100);
|
|
|
|
if (pdesc)
|
|
|
|
port_description = pdesc;
|
|
|
|
}
|
|
|
|
|
2016-02-23 05:23:56 +01:00
|
|
|
if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) {
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
capabilities = lldp_capabilities_to_string(cc);
|
2016-02-23 05:23:56 +01:00
|
|
|
all |= cc;
|
|
|
|
}
|
sd-lldp: rework sd-lldp API
This reworks the sd-lldp substantially, simplifying things on one hand, and
extending the logic a bit on the other.
Specifically:
- Besides the sd_lldp object only one other object is maintained now,
sd_lldp_neighbor. It's used both as storage for literal LLDP packets, and for
maintainging info about peers in the database. Separation between packet, TLV
and chassis data is not maintained anymore. This should be a major
simplification.
- The sd-lldp API has been extended so that a couple of per-neighbor fields may
be queried directly, without iterating through the object. Other fields that
may appear multiple times, OTOH have to be iterated through.
- The maximum number of entries in the neighbor database is now configurable
during runtime.
- The generation of callbacks from sd_lldp objects is more restricted:
callbacks are only invoked when actual data changed.
- The TTL information is now hooked with a timer event, so that removals from
the neighbor database due to TTLs now result in a callback event.
- Querying LLDP neighbor database will now return a strictly ordered array, to
guarantee stability.
- A "capabilities" mask may now be configured, that selects what type of LLDP
neighbor data is collected. This may be used to restrict collection of LLDP
info about routers instead of all neighbors. This is now exposed via
networkd's LLDP= setting.
- sd-lldp's API to serialize the collected data to text files has been removed.
Instead, there's now an API to extract the raw binary data from LLDP neighbor
objects, as well as one to convert this raw binary data back to an LLDP
neighbor object. networkd will save this raw binary data to /run now, and the
client side can simply parse the information.
- support for parsing the more exotic TLVs has been removed, since we are not
using that. Instead there are now APIs to extract the raw data from TLVs.
Given how easy it is to parse the TLVs clients should do so now directly
instead of relying on our APIs for that.
- A lot of the APIs that parse out LLDP strings have been simplified so that
they actually return strings, instead of char arrays with a length. To deal
with possibly dangerous characters the strings are escaped if needed.
- APIs to extract and format the chassis and port IDs as strings has been
added.
- lldp.h has been simplified a lot. The enums are anonymous now, since they
were never used as enums, but simply as constants. Most definitions we don't
actually use ourselves have eben removed.
2016-02-19 17:58:52 +01:00
|
|
|
|
2019-05-22 22:13:13 +02:00
|
|
|
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));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-02-19 19:18:12 +01:00
|
|
|
|
|
|
|
m++;
|
2014-12-11 05:29:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 22:13:13 +02:00
|
|
|
r = table_print(table, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2016-02-23 05:23:56 +01:00
|
|
|
if (arg_legend) {
|
|
|
|
lldp_capabilities_legend(all);
|
|
|
|
printf("\n%i neighbors listed.\n", m);
|
|
|
|
}
|
2014-12-11 05:29:55 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-22 11:46:41 +02:00
|
|
|
static int link_delete_send_message(sd_netlink *rtnl, int index) {
|
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(rtnl);
|
|
|
|
|
|
|
|
r = sd_rtnl_message_new_link(rtnl, &req, RTM_DELLINK, index);
|
|
|
|
if (r < 0)
|
|
|
|
return rtnl_log_create_error(r);
|
|
|
|
|
|
|
|
r = sd_netlink_call(rtnl, req, 0, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int link_delete(int argc, char *argv[], void *userdata) {
|
|
|
|
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
|
|
|
_cleanup_set_free_ Set *indexes = NULL;
|
|
|
|
int index, r, i;
|
|
|
|
Iterator j;
|
|
|
|
|
|
|
|
r = sd_netlink_open(&rtnl);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to connect to netlink: %m");
|
|
|
|
|
|
|
|
indexes = set_new(NULL);
|
|
|
|
if (!indexes)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
2019-05-29 04:02:09 +02:00
|
|
|
r = parse_ifindex_or_ifname(argv[i], &index);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to resolve interface %s", argv[i]);
|
2019-05-22 11:46:41 +02:00
|
|
|
|
|
|
|
r = set_put(indexes, INT_TO_PTR(index));
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
|
|
|
|
SET_FOREACH(index, indexes, j) {
|
|
|
|
r = link_delete_send_message(rtnl, index);
|
|
|
|
if (r < 0) {
|
2019-05-29 07:18:41 +02:00
|
|
|
char ifname[IF_NAMESIZE + 1];
|
|
|
|
|
|
|
|
if (format_ifname(index, ifname))
|
2019-05-22 11:46:41 +02:00
|
|
|
return log_error_errno(r, "Failed to delete interface %s: %m", ifname);
|
|
|
|
else
|
|
|
|
return log_error_errno(r, "Failed to delete interface %d: %m", index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-08-09 10:32:31 +02:00
|
|
|
static int help(void) {
|
|
|
|
_cleanup_free_ char *link = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = terminal_urlify_man("networkctl", "1", &link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
2014-08-12 01:41:42 +02:00
|
|
|
printf("%s [OPTIONS...]\n\n"
|
|
|
|
"Query and control the networking subsystem.\n\n"
|
|
|
|
" -h --help Show this help\n"
|
2014-08-12 15:19:30 +02:00
|
|
|
" --version Show package version\n"
|
|
|
|
" --no-pager Do not pipe output into a pager\n"
|
|
|
|
" --no-legend Do not show the headers and footers\n"
|
2019-05-24 17:37:48 +02:00
|
|
|
" -a --all Show status for all links\n"
|
|
|
|
" -s --stats Show detailed link statics\n"
|
|
|
|
"\nCommands:\n"
|
2019-02-15 05:32:51 +01:00
|
|
|
" list [PATTERN...] List links\n"
|
|
|
|
" status [PATTERN...] Show link status\n"
|
|
|
|
" lldp [PATTERN...] Show LLDP neighbors\n"
|
2017-06-26 19:58:10 +02:00
|
|
|
" label Show current address label entries in the kernel\n"
|
2019-05-22 11:46:41 +02:00
|
|
|
" delete DEVICES Delete virtual netdevs\n"
|
2018-08-09 10:32:31 +02:00
|
|
|
"\nSee the %s for details.\n"
|
|
|
|
, program_invocation_short_name
|
|
|
|
, link
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
2014-08-12 01:41:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
ARG_NO_PAGER,
|
|
|
|
ARG_NO_LEGEND,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
|
|
|
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
|
|
|
|
{ "no-legend", no_argument, NULL, ARG_NO_LEGEND },
|
2014-08-12 15:19:30 +02:00
|
|
|
{ "all", no_argument, NULL, 'a' },
|
2019-05-24 17:37:48 +02:00
|
|
|
{ "stats", no_argument, NULL, 's' },
|
2014-08-12 01:41:42 +02:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2019-05-24 17:37:48 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "has", options, NULL)) >= 0) {
|
2014-08-12 01:41:42 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
2018-08-09 10:32:31 +02:00
|
|
|
return help();
|
2014-08-12 01:41:42 +02:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
2015-09-23 03:01:06 +02:00
|
|
|
return version();
|
2014-08-12 01:41:42 +02:00
|
|
|
|
|
|
|
case ARG_NO_PAGER:
|
2018-11-11 12:56:29 +01:00
|
|
|
arg_pager_flags |= PAGER_DISABLE;
|
2014-08-12 01:41:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_NO_LEGEND:
|
|
|
|
arg_legend = false;
|
|
|
|
break;
|
|
|
|
|
2014-08-12 15:19:30 +02:00
|
|
|
case 'a':
|
|
|
|
arg_all = true;
|
|
|
|
break;
|
|
|
|
|
2019-05-24 17:37:48 +02:00
|
|
|
case 's':
|
|
|
|
arg_stats = true;
|
|
|
|
break;
|
|
|
|
|
2014-08-12 01:41:42 +02:00
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unhandled option");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int networkctl_main(int argc, char *argv[]) {
|
2018-03-12 06:05:08 +01:00
|
|
|
static const Verb verbs[] = {
|
|
|
|
{ "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, list_links },
|
|
|
|
{ "status", VERB_ANY, VERB_ANY, 0, link_status },
|
|
|
|
{ "lldp", VERB_ANY, VERB_ANY, 0, link_lldp_status },
|
|
|
|
{ "label", VERB_ANY, VERB_ANY, 0, list_address_labels },
|
2019-05-22 11:46:41 +02:00
|
|
|
{ "delete", 2, VERB_ANY, 0, link_delete },
|
2014-12-19 03:16:45 +01:00
|
|
|
{}
|
2014-08-12 01:41:42 +02:00
|
|
|
};
|
|
|
|
|
2014-12-19 03:16:45 +01:00
|
|
|
return dispatch_verb(argc, argv, verbs, NULL);
|
2014-08-12 01:41:42 +02:00
|
|
|
}
|
|
|
|
|
2016-02-20 22:25:43 +01:00
|
|
|
static void warn_networkd_missing(void) {
|
|
|
|
|
|
|
|
if (access("/run/systemd/netif/state", F_OK) >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(stderr, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
|
|
|
|
}
|
|
|
|
|
2018-11-20 09:57:30 +01:00
|
|
|
static int run(int argc, char* argv[]) {
|
2014-08-12 01:41:42 +02:00
|
|
|
int r;
|
|
|
|
|
2019-04-26 12:28:25 +02:00
|
|
|
log_show_color(true);
|
2014-08-12 01:41:42 +02:00
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
2018-11-20 09:57:30 +01:00
|
|
|
return r;
|
2014-08-12 01:41:42 +02:00
|
|
|
|
2016-02-20 22:25:43 +01:00
|
|
|
warn_networkd_missing();
|
|
|
|
|
2018-11-20 09:57:30 +01:00
|
|
|
return networkctl_main(argc, argv);
|
2014-08-12 01:41:42 +02:00
|
|
|
}
|
2018-11-20 09:57:30 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|