Skip to content

Commit

Permalink
Merge pull request #85 from ssahani/netlink-link-stat
Browse files Browse the repository at this point in the history
json: Add support show link stat
  • Loading branch information
ssahani authored Dec 20, 2020
2 parents 10523aa + 4baf11d commit 2b1180e
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 4 deletions.
212 changes: 208 additions & 4 deletions src/json/network-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,13 @@ int json_system_status(char **ret) {
if (ret) {
char *s;

s = strdup(json_object_to_json_string(jobj));
s = strdup(json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_NOSLASHESCAPE));
if (!s)
return log_oom();

*ret = steal_pointer(s);
} else
printf("%s\n", json_object_to_json_string(jobj));
printf("%s\n", json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_NOSLASHESCAPE));

return r;
}
Expand Down Expand Up @@ -844,6 +844,210 @@ int json_list_one_link(IfNameIndex *p, char **ret) {
steal_pointer(js);
}

if (l->contains_stats || l->contains_stats64) {
_cleanup_(json_object_putp) json_object *js = NULL;

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_bytes);
else
js = json_object_new_double(l->stats64.rx_bytes);

json_object_object_add(jobj, "RXBytes", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.tx_bytes);
else
js = json_object_new_double(l->stats64.tx_bytes);

json_object_object_add(jobj, "TXBytes", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_packets);
else
js = json_object_new_double(l->stats64.rx_packets);

json_object_object_add(jobj, "RXPackets", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.tx_packets);
else
js = json_object_new_double(l->stats64.tx_packets);

json_object_object_add(jobj, "TXPackets", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.tx_errors);
else
js = json_object_new_double(l->stats64.tx_errors);

json_object_object_add(jobj, "TXErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_errors);
else
js = json_object_new_double(l->stats64.rx_errors);

json_object_object_add(jobj, "RXErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_dropped);
else
js = json_object_new_double(l->stats64.rx_dropped);

json_object_object_add(jobj, "TXDropped", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.tx_dropped);
else
js = json_object_new_double(l->stats64.tx_dropped);

json_object_object_add(jobj, "RXDropped", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_over_errors);
else
js = json_object_new_double(l->stats64.rx_over_errors);

json_object_object_add(jobj, "RXOverErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.multicast);
else
js = json_object_new_double(l->stats64.multicast);

json_object_object_add(jobj, "MulticastPackets", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.collisions);
else
js = json_object_new_double(l->stats64.collisions);

json_object_object_add(jobj, "Collisions", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_length_errors);
else
js = json_object_new_double(l->stats64.rx_length_errors);

json_object_object_add(jobj, "RXLengthErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_over_errors);
else
js = json_object_new_double(l->stats64.rx_over_errors);

json_object_object_add(jobj, "RXOverErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_crc_errors);
else
js = json_object_new_double(l->stats64.rx_crc_errors);

json_object_object_add(jobj, "RXCRCErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_frame_errors);
else
js = json_object_new_double(l->stats64.rx_frame_errors);

json_object_object_add(jobj, "RXFrameErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_fifo_errors);
else
js = json_object_new_double(l->stats64.rx_fifo_errors);

json_object_object_add(jobj, "RXFIFOErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_missed_errors);
else
js = json_object_new_double(l->stats64.rx_missed_errors);

json_object_object_add(jobj, "RXMissedErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.tx_aborted_errors);
else
js = json_object_new_double(l->stats64.tx_aborted_errors);

json_object_object_add(jobj, "TXAbortedErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.tx_carrier_errors);
else
js = json_object_new_double(l->stats64.tx_carrier_errors);

json_object_object_add(jobj, "TXCarrierErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.tx_fifo_errors);
else
js = json_object_new_double(l->stats64.tx_fifo_errors);

json_object_object_add(jobj, "TXFIFOErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.tx_heartbeat_errors);
else
js = json_object_new_double(l->stats64.tx_heartbeat_errors);

json_object_object_add(jobj, "TXHeartBeatErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.tx_window_errors);
else
js = json_object_new_double(l->stats64.tx_window_errors);

json_object_object_add(jobj, "TXWindowErrors", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_compressed);
else
js = json_object_new_double(l->stats64.rx_compressed);

json_object_object_add(jobj, "RXCompressed", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.tx_compressed);
else
js = json_object_new_double(l->stats64.tx_compressed);

json_object_object_add(jobj, "TXCompressed", js);
steal_pointer(js);

if (l->contains_stats)
js = json_object_new_int(l->stats.rx_nohandler);
else
js = json_object_new_double(l->stats64.rx_nohandler);

json_object_object_add(jobj, "RXNoHandler", js);
steal_pointer(js);
}

(void) network_parse_link_dns(l->ifindex, &dns);
(void) network_parse_link_search_domains(l->ifindex, &search_domains);
(void) network_parse_link_route_domains(l->ifindex, &route_domains);
Expand Down Expand Up @@ -964,13 +1168,13 @@ int json_list_one_link(IfNameIndex *p, char **ret) {
if (ret) {
char *s;

s = strdup(json_object_to_json_string(jobj));
s = strdup(json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_NOSLASHESCAPE));
if (!s)
return log_oom();

*ret = steal_pointer(s);
} else
printf("%s\n", json_object_to_json_string(jobj));
printf("%s\n", json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_NOSLASHESCAPE));

return 0;
}
7 changes: 7 additions & 0 deletions src/lib-network/netlink/netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ struct rtattr *rtnl_message_parse_rtattr_one(int type, struct rtattr *rta, int l
return NULL;
}

int rtnl_message_read_attribute(const struct rtattr *rta, void *p, size_t size) {
assert(rta);

memcpy(p, RTA_DATA(rta), size);
return size;
}

uint8_t rtnl_message_read_attribute_u8(const struct rtattr *rta) {
assert(rta);

Expand Down
1 change: 1 addition & 0 deletions src/lib-network/netlink/netlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ const char *rtnl_message_read_attribute_string(const struct rtattr *rta);
int rtnl_message_read_attribute_ether_address(const struct rtattr *rta, struct ether_addr *data);
int rtnl_message_read_in_addr(const struct rtattr *rta, struct in_addr *data);
int rtnl_message_read_in6_addr(const struct rtattr *rta, struct in6_addr *data);
int rtnl_message_read_attribute(const struct rtattr *rta, void *p, size_t size);
9 changes: 9 additions & 0 deletions src/manager/network-link.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ static int fill_one_link_info(struct nlmsghdr *h, size_t len, Link **ret) {
n->contains_mac_address = true;
}

if (rta_tb[IFLA_STATS64]) {
rtnl_message_read_attribute(rta_tb[IFLA_STATS64], &n->stats64, sizeof(struct rtnl_link_stats64));
n->contains_stats64 = true;
}
if (rta_tb[IFLA_STATS]) {
rtnl_message_read_attribute(rta_tb[IFLA_STATS], &n->stats, sizeof(struct rtnl_link_stats));
n->contains_stats = true;
}

if (rta_tb[IFLA_PROP_LIST]) {
struct rtattr *i, *j = rta_tb[IFLA_PROP_LIST];
int k = RTA_PAYLOAD(j);
Expand Down
7 changes: 7 additions & 0 deletions src/manager/network-link.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ typedef struct Link {
uint32_t gso_max_segments;
uint32_t flags;

union {
struct rtnl_link_stats64 stats64;
struct rtnl_link_stats stats;
};

GPtrArray *alt_names;

bool contains_mac_address:1;
bool contains_mtu:1;
bool contains_stats:1;
bool contains_stats64:1;
} Link;

typedef struct Links {
Expand Down

0 comments on commit 2b1180e

Please sign in to comment.