History

samples: bpf: Convert route table network order fields into readable format

The route table that is dumped when the xdp_router_ipv4 process is launched has the "Gateway" field in non-readable network byte order format, also the alignment is off when printing the table. Destination Gateway Genmask Metric Iface 0.0.0.0 196a8c0 0 0 enp7s0 0.0.0.0 196a8c0 0 0 wlp6s0 169.254.0.0 196a8c0 16 0 enp7s0 172.17.0.0 0 16 0 docker0 192.168.150.0 0 24 0 enp7s0 192.168.150.0 0 24 0 wlp6s0 Fix this by converting the "Gateway" field from network byte order Hex into dotted decimal notation IPv4 format and "Genmask" from CIDR notation into dotted decimal notation IPv4 format. Also fix the aligntment of the fields in the route table. Destination Gateway Genmask Metric Iface 0.0.0.0 192.168.150.1 0.0.0.0 0 enp7s0 0.0.0.0 192.168.150.1 0.0.0.0 0 wlp6s0 169.254.0.0 192.168.150.1 255.255.0.0 0 enp7s0 172.17.0.0 0.0.0.0 255.255.0.0 0 docker0 192.168.150.0 0.0.0.0 255.255.255.0 0 enp7s0 192.168.150.0 0.0.0.0 255.255.255.0 0 wlp6s0

Gokul Sivakumar committed 5 years ago

Commit f5c4e411 parent 97c140d1 file · +13 −9Browse files

Showing the changes to samples (1 of 1 file). Show all

Unified · Split

samples/bpf/xdp_router_ipv4_user.c+13−9
@@ -155,7 +155,7 @@ static void read_route(struct nlmsghdr *nh, int nll) · 154 unmodified lines
155155 printf("%d\n", nh->nlmsg_type);
156156
157157 memset(&route, 0, sizeof(route));
158− printf("Destination\t\tGateway\t\tGenmask\t\tMetric\t\tIface\n");
158+ printf("Destination Gateway Genmask Metric Iface\n");
159159 for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
160160 rt_msg = (struct rtmsg *)NLMSG_DATA(nh);
161161 rtm_family = rt_msg->rtm_family;
@@ -207,6 +207,7 @@ static void read_route(struct nlmsghdr *nh, int nll) · 45 unmodified lines
207207 int metric;
208208 __be32 gw;
209209 } *prefix_value;
210+ struct in_addr dst_addr, gw_addr, mask_addr;
210211
211212 prefix_key = alloca(sizeof(*prefix_key) + 3);
212213 prefix_value = alloca(sizeof(*prefix_value));
@@ -234,14 +235,17 @@ static void read_route(struct nlmsghdr *nh, int nll) · 21 unmodified lines
234235 for (i = 0; i < 4; i++)
235236 prefix_key->data[i] = (route.dst >> i * 8) & 0xff;
236237
237− printf("%3d.%d.%d.%d\t\t%3x\t\t%d\t\t%d\t\t%s\n",
238− (int)prefix_key->data[0],
239− (int)prefix_key->data[1],
240− (int)prefix_key->data[2],
241− (int)prefix_key->data[3],
242− route.gw, route.dst_len,
238+ dst_addr.s_addr = route.dst;
239+ printf("%-16s", inet_ntoa(dst_addr));
240+
241+ gw_addr.s_addr = route.gw;
242+ printf("%-16s", inet_ntoa(gw_addr));
243+
244+ mask_addr.s_addr = htonl(~(0xffffffffU >> route.dst_len));
245+ printf("%-16s%-7d%s\n", inet_ntoa(mask_addr),
243246 route.metric,
244247 route.iface_name);
248+
245249 if (bpf_map_lookup_elem(lpm_map_fd, prefix_key,
246250 prefix_value) < 0) {
247251 for (i = 0; i < 4; i++)
@@ -672,7 +676,7 @@ int main(int ac, char **argv) · 424 unmodified lines
672676 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
673677 return 1;
674678
675− printf("\n**************loading bpf file*********************\n\n\n");
679+ printf("\n******************loading bpf file*********************\n");
676680 if (!prog_fd) {
677681 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
678682 return 1;
@@ -722,7 +726,7 @@ int main(int ac, char **argv) · 43 unmodified lines
722726 signal(SIGINT, int_exit);
723727 signal(SIGTERM, int_exit);
724728
725− printf("*******************ROUTE TABLE*************************\n\n\n");
729+ printf("\n*******************ROUTE TABLE*************************\n");
726730 get_route_table(AF_INET);
727731 printf("*******************ARP TABLE***************************\n\n\n");
728732 get_arp_table(AF_INET);