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

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
155 printf("%d\n", nh->nlmsg_type);155 printf("%d\n", nh->nlmsg_type);
156 156
157 memset(&route, 0, sizeof(route));157 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");
159 for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {159 for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
160 rt_msg = (struct rtmsg *)NLMSG_DATA(nh);160 rt_msg = (struct rtmsg *)NLMSG_DATA(nh);
161 rtm_family = rt_msg->rtm_family;161 rtm_family = rt_msg->rtm_family;
@@ -207,6 +207,7 @@ static void read_route(struct nlmsghdr *nh, int nll) · 45 unmodified lines
207 int metric;207 int metric;
208 __be32 gw;208 __be32 gw;
209 } *prefix_value;209 } *prefix_value;
210 struct in_addr dst_addr, gw_addr, mask_addr;
210 211
211 prefix_key = alloca(sizeof(*prefix_key) + 3);212 prefix_key = alloca(sizeof(*prefix_key) + 3);
212 prefix_value = alloca(sizeof(*prefix_value));213 prefix_value = alloca(sizeof(*prefix_value));
@@ -234,14 +235,17 @@ static void read_route(struct nlmsghdr *nh, int nll) · 21 unmodified lines
234 for (i = 0; i < 4; i++)235 for (i = 0; i < 4; i++)
235 prefix_key->data[i] = (route.dst >> i * 8) & 0xff;236 prefix_key->data[i] = (route.dst >> i * 8) & 0xff;
236 237
237 printf("%3d.%d.%d.%d\t\t%3x\t\t%d\t\t%d\t\t%s\n",238 dst_addr.s_addr = route.dst;
238 (int)prefix_key->data[0],239 printf("%-16s", inet_ntoa(dst_addr));
239 (int)prefix_key->data[1],240
240 (int)prefix_key->data[2],241 gw_addr.s_addr = route.gw;
241 (int)prefix_key->data[3],242 printf("%-16s", inet_ntoa(gw_addr));
242 route.gw, route.dst_len,243
244 mask_addr.s_addr = htonl(~(0xffffffffU >> route.dst_len));
245 printf("%-16s%-7d%s\n", inet_ntoa(mask_addr),
243 route.metric,246 route.metric,
244 route.iface_name);247 route.iface_name);
248
245 if (bpf_map_lookup_elem(lpm_map_fd, prefix_key,249 if (bpf_map_lookup_elem(lpm_map_fd, prefix_key,
246 prefix_value) < 0) {250 prefix_value) < 0) {
247 for (i = 0; i < 4; i++)251 for (i = 0; i < 4; i++)
@@ -672,7 +676,7 @@ int main(int ac, char **argv) · 424 unmodified lines
672 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))676 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
673 return 1;677 return 1;
674 678
675 printf("\n**************loading bpf file*********************\n\n\n");679 printf("\n******************loading bpf file*********************\n");
676 if (!prog_fd) {680 if (!prog_fd) {
677 printf("bpf_prog_load_xattr: %s\n", strerror(errno));681 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
678 return 1;682 return 1;
@@ -722,7 +726,7 @@ int main(int ac, char **argv) · 43 unmodified lines
722 signal(SIGINT, int_exit);726 signal(SIGINT, int_exit);
723 signal(SIGTERM, int_exit);727 signal(SIGTERM, int_exit);
724 728
725 printf("*******************ROUTE TABLE*************************\n\n\n");729 printf("\n*******************ROUTE TABLE*************************\n");
726 get_route_table(AF_INET);730 get_route_table(AF_INET);
727 printf("*******************ARP TABLE***************************\n\n\n");731 printf("*******************ARP TABLE***************************\n\n\n");
728 get_arp_table(AF_INET);732 get_arp_table(AF_INET);