blob: cea2306f5ab75e34fd525b113daa3f699dd2c4d5 [file] [log] [blame]
Christina Jacob3e29cd02017-11-05 08:52:30 +05301/* Copyright (C) 2017 Cavium, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of version 2 of the GNU General Public License
5 * as published by the Free Software Foundation.
6 */
7#include <linux/bpf.h>
8#include <linux/netlink.h>
9#include <linux/rtnetlink.h>
10#include <assert.h>
11#include <errno.h>
12#include <signal.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/socket.h>
17#include <unistd.h>
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070018#include <bpf/bpf.h>
Christina Jacob3e29cd02017-11-05 08:52:30 +053019#include <arpa/inet.h>
20#include <fcntl.h>
21#include <poll.h>
22#include <net/if.h>
23#include <netdb.h>
24#include <sys/ioctl.h>
25#include <sys/syscall.h>
26#include "bpf_util.h"
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +010027#include "bpf/libbpf.h"
Christina Jacob3e29cd02017-11-05 08:52:30 +053028
29int sock, sock_arp, flags = 0;
30static int total_ifindex;
31int *ifindex_list;
32char buf[8192];
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +010033static int lpm_map_fd;
34static int rxcnt_map_fd;
35static int arp_table_map_fd;
36static int exact_match_map_fd;
37static int tx_port_map_fd;
Christina Jacob3e29cd02017-11-05 08:52:30 +053038
39static int get_route_table(int rtm_family);
40static void int_exit(int sig)
41{
42 int i = 0;
43
44 for (i = 0; i < total_ifindex; i++)
Eric Leblondb259c2f2018-01-30 21:55:04 +010045 bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
Christina Jacob3e29cd02017-11-05 08:52:30 +053046 exit(0);
47}
48
49static void close_and_exit(int sig)
50{
51 int i = 0;
52
53 close(sock);
54 close(sock_arp);
55
56 for (i = 0; i < total_ifindex; i++)
Eric Leblondb259c2f2018-01-30 21:55:04 +010057 bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
Christina Jacob3e29cd02017-11-05 08:52:30 +053058 exit(0);
59}
60
61/* Get the mac address of the interface given interface name */
62static __be64 getmac(char *iface)
63{
64 struct ifreq ifr;
65 __be64 mac = 0;
66 int fd, i;
67
68 fd = socket(AF_INET, SOCK_DGRAM, 0);
69 ifr.ifr_addr.sa_family = AF_INET;
70 strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);
71 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
72 printf("ioctl failed leaving....\n");
73 return -1;
74 }
75 for (i = 0; i < 6 ; i++)
76 *((__u8 *)&mac + i) = (__u8)ifr.ifr_hwaddr.sa_data[i];
77 close(fd);
78 return mac;
79}
80
81static int recv_msg(struct sockaddr_nl sock_addr, int sock)
82{
83 struct nlmsghdr *nh;
84 int len, nll = 0;
85 char *buf_ptr;
86
87 buf_ptr = buf;
88 while (1) {
89 len = recv(sock, buf_ptr, sizeof(buf) - nll, 0);
90 if (len < 0)
91 return len;
92
93 nh = (struct nlmsghdr *)buf_ptr;
94
95 if (nh->nlmsg_type == NLMSG_DONE)
96 break;
97 buf_ptr += len;
98 nll += len;
99 if ((sock_addr.nl_groups & RTMGRP_NEIGH) == RTMGRP_NEIGH)
100 break;
101
102 if ((sock_addr.nl_groups & RTMGRP_IPV4_ROUTE) == RTMGRP_IPV4_ROUTE)
103 break;
104 }
105 return nll;
106}
107
108/* Function to parse the route entry returned by netlink
109 * Updates the route entry related map entries
110 */
111static void read_route(struct nlmsghdr *nh, int nll)
112{
113 char dsts[24], gws[24], ifs[16], dsts_len[24], metrics[24];
114 struct bpf_lpm_trie_key *prefix_key;
115 struct rtattr *rt_attr;
116 struct rtmsg *rt_msg;
117 int rtm_family;
118 int rtl;
119 int i;
120 struct route_table {
121 int dst_len, iface, metric;
122 char *iface_name;
123 __be32 dst, gw;
124 __be64 mac;
125 } route;
126 struct arp_table {
127 __be64 mac;
128 __be32 dst;
129 };
130
131 struct direct_map {
132 struct arp_table arp;
133 int ifindex;
134 __be64 mac;
135 } direct_entry;
136
137 if (nh->nlmsg_type == RTM_DELROUTE)
138 printf("DELETING Route entry\n");
139 else if (nh->nlmsg_type == RTM_GETROUTE)
140 printf("READING Route entry\n");
141 else if (nh->nlmsg_type == RTM_NEWROUTE)
142 printf("NEW Route entry\n");
143 else
144 printf("%d\n", nh->nlmsg_type);
145
146 memset(&route, 0, sizeof(route));
147 printf("Destination\t\tGateway\t\tGenmask\t\tMetric\t\tIface\n");
148 for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
149 rt_msg = (struct rtmsg *)NLMSG_DATA(nh);
150 rtm_family = rt_msg->rtm_family;
151 if (rtm_family == AF_INET)
152 if (rt_msg->rtm_table != RT_TABLE_MAIN)
153 continue;
154 rt_attr = (struct rtattr *)RTM_RTA(rt_msg);
155 rtl = RTM_PAYLOAD(nh);
156
157 for (; RTA_OK(rt_attr, rtl); rt_attr = RTA_NEXT(rt_attr, rtl)) {
158 switch (rt_attr->rta_type) {
159 case NDA_DST:
160 sprintf(dsts, "%u",
161 (*((__be32 *)RTA_DATA(rt_attr))));
162 break;
163 case RTA_GATEWAY:
164 sprintf(gws, "%u",
165 *((__be32 *)RTA_DATA(rt_attr)));
166 break;
167 case RTA_OIF:
168 sprintf(ifs, "%u",
169 *((int *)RTA_DATA(rt_attr)));
170 break;
171 case RTA_METRICS:
172 sprintf(metrics, "%u",
173 *((int *)RTA_DATA(rt_attr)));
174 default:
175 break;
176 }
177 }
178 sprintf(dsts_len, "%d", rt_msg->rtm_dst_len);
179 route.dst = atoi(dsts);
180 route.dst_len = atoi(dsts_len);
181 route.gw = atoi(gws);
182 route.iface = atoi(ifs);
183 route.metric = atoi(metrics);
184 route.iface_name = alloca(sizeof(char *) * IFNAMSIZ);
185 route.iface_name = if_indextoname(route.iface, route.iface_name);
186 route.mac = getmac(route.iface_name);
187 if (route.mac == -1) {
188 int i = 0;
189
190 for (i = 0; i < total_ifindex; i++)
Eric Leblondb259c2f2018-01-30 21:55:04 +0100191 bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
Christina Jacob3e29cd02017-11-05 08:52:30 +0530192 exit(0);
193 }
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100194 assert(bpf_map_update_elem(tx_port_map_fd,
195 &route.iface, &route.iface, 0) == 0);
Christina Jacob3e29cd02017-11-05 08:52:30 +0530196 if (rtm_family == AF_INET) {
197 struct trie_value {
198 __u8 prefix[4];
199 __be64 value;
200 int ifindex;
201 int metric;
202 __be32 gw;
203 } *prefix_value;
204
205 prefix_key = alloca(sizeof(*prefix_key) + 3);
206 prefix_value = alloca(sizeof(*prefix_value));
207
208 prefix_key->prefixlen = 32;
209 prefix_key->prefixlen = route.dst_len;
210 direct_entry.mac = route.mac & 0xffffffffffff;
211 direct_entry.ifindex = route.iface;
212 direct_entry.arp.mac = 0;
213 direct_entry.arp.dst = 0;
214 if (route.dst_len == 32) {
Dan Carpenterfae45362017-11-14 09:12:03 +0300215 if (nh->nlmsg_type == RTM_DELROUTE) {
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100216 assert(bpf_map_delete_elem(exact_match_map_fd,
217 &route.dst) == 0);
Dan Carpenterfae45362017-11-14 09:12:03 +0300218 } else {
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100219 if (bpf_map_lookup_elem(arp_table_map_fd,
220 &route.dst,
221 &direct_entry.arp.mac) == 0)
Christina Jacob3e29cd02017-11-05 08:52:30 +0530222 direct_entry.arp.dst = route.dst;
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100223 assert(bpf_map_update_elem(exact_match_map_fd,
224 &route.dst,
225 &direct_entry, 0) == 0);
Dan Carpenterfae45362017-11-14 09:12:03 +0300226 }
Christina Jacob3e29cd02017-11-05 08:52:30 +0530227 }
228 for (i = 0; i < 4; i++)
229 prefix_key->data[i] = (route.dst >> i * 8) & 0xff;
230
231 printf("%3d.%d.%d.%d\t\t%3x\t\t%d\t\t%d\t\t%s\n",
232 (int)prefix_key->data[0],
233 (int)prefix_key->data[1],
234 (int)prefix_key->data[2],
235 (int)prefix_key->data[3],
236 route.gw, route.dst_len,
237 route.metric,
238 route.iface_name);
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100239 if (bpf_map_lookup_elem(lpm_map_fd, prefix_key,
Christina Jacob3e29cd02017-11-05 08:52:30 +0530240 prefix_value) < 0) {
241 for (i = 0; i < 4; i++)
242 prefix_value->prefix[i] = prefix_key->data[i];
243 prefix_value->value = route.mac & 0xffffffffffff;
244 prefix_value->ifindex = route.iface;
245 prefix_value->gw = route.gw;
246 prefix_value->metric = route.metric;
247
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100248 assert(bpf_map_update_elem(lpm_map_fd,
Christina Jacob3e29cd02017-11-05 08:52:30 +0530249 prefix_key,
250 prefix_value, 0
251 ) == 0);
252 } else {
253 if (nh->nlmsg_type == RTM_DELROUTE) {
254 printf("deleting entry\n");
255 printf("prefix key=%d.%d.%d.%d/%d",
256 prefix_key->data[0],
257 prefix_key->data[1],
258 prefix_key->data[2],
259 prefix_key->data[3],
260 prefix_key->prefixlen);
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100261 assert(bpf_map_delete_elem(lpm_map_fd,
Christina Jacob3e29cd02017-11-05 08:52:30 +0530262 prefix_key
263 ) == 0);
264 /* Rereading the route table to check if
265 * there is an entry with the same
266 * prefix but a different metric as the
267 * deleted enty.
268 */
269 get_route_table(AF_INET);
270 } else if (prefix_key->data[0] ==
271 prefix_value->prefix[0] &&
272 prefix_key->data[1] ==
273 prefix_value->prefix[1] &&
274 prefix_key->data[2] ==
275 prefix_value->prefix[2] &&
276 prefix_key->data[3] ==
277 prefix_value->prefix[3] &&
278 route.metric >= prefix_value->metric) {
279 continue;
280 } else {
281 for (i = 0; i < 4; i++)
282 prefix_value->prefix[i] =
283 prefix_key->data[i];
284 prefix_value->value =
285 route.mac & 0xffffffffffff;
286 prefix_value->ifindex = route.iface;
287 prefix_value->gw = route.gw;
288 prefix_value->metric = route.metric;
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100289 assert(bpf_map_update_elem(lpm_map_fd,
Christina Jacob3e29cd02017-11-05 08:52:30 +0530290 prefix_key,
291 prefix_value,
292 0) == 0);
293 }
294 }
295 }
296 memset(&route, 0, sizeof(route));
297 memset(dsts, 0, sizeof(dsts));
298 memset(dsts_len, 0, sizeof(dsts_len));
299 memset(gws, 0, sizeof(gws));
300 memset(ifs, 0, sizeof(ifs));
301 memset(&route, 0, sizeof(route));
302 }
303}
304
305/* Function to read the existing route table when the process is launched*/
306static int get_route_table(int rtm_family)
307{
308 struct sockaddr_nl sa;
309 struct nlmsghdr *nh;
310 int sock, seq = 0;
311 struct msghdr msg;
312 struct iovec iov;
313 int ret = 0;
314 int nll;
315
316 struct {
317 struct nlmsghdr nl;
318 struct rtmsg rt;
319 char buf[8192];
320 } req;
321
322 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
323 if (sock < 0) {
324 printf("open netlink socket: %s\n", strerror(errno));
325 return -1;
326 }
327 memset(&sa, 0, sizeof(sa));
328 sa.nl_family = AF_NETLINK;
329 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
330 printf("bind to netlink: %s\n", strerror(errno));
331 ret = -1;
332 goto cleanup;
333 }
334 memset(&req, 0, sizeof(req));
335 req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
336 req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
337 req.nl.nlmsg_type = RTM_GETROUTE;
338
339 req.rt.rtm_family = rtm_family;
340 req.rt.rtm_table = RT_TABLE_MAIN;
341 req.nl.nlmsg_pid = 0;
342 req.nl.nlmsg_seq = ++seq;
343 memset(&msg, 0, sizeof(msg));
344 iov.iov_base = (void *)&req.nl;
345 iov.iov_len = req.nl.nlmsg_len;
346 msg.msg_iov = &iov;
347 msg.msg_iovlen = 1;
348 ret = sendmsg(sock, &msg, 0);
349 if (ret < 0) {
350 printf("send to netlink: %s\n", strerror(errno));
351 ret = -1;
352 goto cleanup;
353 }
354 memset(buf, 0, sizeof(buf));
355 nll = recv_msg(sa, sock);
356 if (nll < 0) {
357 printf("recv from netlink: %s\n", strerror(nll));
358 ret = -1;
359 goto cleanup;
360 }
361 nh = (struct nlmsghdr *)buf;
362 read_route(nh, nll);
363cleanup:
364 close(sock);
365 return ret;
366}
367
368/* Function to parse the arp entry returned by netlink
369 * Updates the arp entry related map entries
370 */
371static void read_arp(struct nlmsghdr *nh, int nll)
372{
373 struct rtattr *rt_attr;
374 char dsts[24], mac[24];
375 struct ndmsg *rt_msg;
376 int rtl, ndm_family;
377
378 struct arp_table {
379 __be64 mac;
380 __be32 dst;
381 } arp_entry;
382 struct direct_map {
383 struct arp_table arp;
384 int ifindex;
385 __be64 mac;
386 } direct_entry;
387
388 if (nh->nlmsg_type == RTM_GETNEIGH)
389 printf("READING arp entry\n");
390 printf("Address\tHwAddress\n");
391 for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
392 rt_msg = (struct ndmsg *)NLMSG_DATA(nh);
393 rt_attr = (struct rtattr *)RTM_RTA(rt_msg);
394 ndm_family = rt_msg->ndm_family;
395 rtl = RTM_PAYLOAD(nh);
396 for (; RTA_OK(rt_attr, rtl); rt_attr = RTA_NEXT(rt_attr, rtl)) {
397 switch (rt_attr->rta_type) {
398 case NDA_DST:
399 sprintf(dsts, "%u",
400 *((__be32 *)RTA_DATA(rt_attr)));
401 break;
402 case NDA_LLADDR:
403 sprintf(mac, "%lld",
404 *((__be64 *)RTA_DATA(rt_attr)));
405 break;
406 default:
407 break;
408 }
409 }
410 arp_entry.dst = atoi(dsts);
411 arp_entry.mac = atol(mac);
412 printf("%x\t\t%llx\n", arp_entry.dst, arp_entry.mac);
413 if (ndm_family == AF_INET) {
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100414 if (bpf_map_lookup_elem(exact_match_map_fd,
415 &arp_entry.dst,
Christina Jacob3e29cd02017-11-05 08:52:30 +0530416 &direct_entry) == 0) {
417 if (nh->nlmsg_type == RTM_DELNEIGH) {
418 direct_entry.arp.dst = 0;
419 direct_entry.arp.mac = 0;
420 } else if (nh->nlmsg_type == RTM_NEWNEIGH) {
421 direct_entry.arp.dst = arp_entry.dst;
422 direct_entry.arp.mac = arp_entry.mac;
423 }
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100424 assert(bpf_map_update_elem(exact_match_map_fd,
Christina Jacob3e29cd02017-11-05 08:52:30 +0530425 &arp_entry.dst,
426 &direct_entry, 0
427 ) == 0);
428 memset(&direct_entry, 0, sizeof(direct_entry));
429 }
430 if (nh->nlmsg_type == RTM_DELNEIGH) {
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100431 assert(bpf_map_delete_elem(arp_table_map_fd,
432 &arp_entry.dst) == 0);
Christina Jacob3e29cd02017-11-05 08:52:30 +0530433 } else if (nh->nlmsg_type == RTM_NEWNEIGH) {
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100434 assert(bpf_map_update_elem(arp_table_map_fd,
Christina Jacob3e29cd02017-11-05 08:52:30 +0530435 &arp_entry.dst,
436 &arp_entry.mac, 0
437 ) == 0);
438 }
439 }
440 memset(&arp_entry, 0, sizeof(arp_entry));
441 memset(dsts, 0, sizeof(dsts));
442 }
443}
444
445/* Function to read the existing arp table when the process is launched*/
446static int get_arp_table(int rtm_family)
447{
448 struct sockaddr_nl sa;
449 struct nlmsghdr *nh;
450 int sock, seq = 0;
451 struct msghdr msg;
452 struct iovec iov;
453 int ret = 0;
454 int nll;
455 struct {
456 struct nlmsghdr nl;
457 struct ndmsg rt;
458 char buf[8192];
459 } req;
460
461 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
462 if (sock < 0) {
463 printf("open netlink socket: %s\n", strerror(errno));
464 return -1;
465 }
466 memset(&sa, 0, sizeof(sa));
467 sa.nl_family = AF_NETLINK;
468 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
469 printf("bind to netlink: %s\n", strerror(errno));
470 ret = -1;
471 goto cleanup;
472 }
473 memset(&req, 0, sizeof(req));
474 req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
475 req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
476 req.nl.nlmsg_type = RTM_GETNEIGH;
477 req.rt.ndm_state = NUD_REACHABLE;
478 req.rt.ndm_family = rtm_family;
479 req.nl.nlmsg_pid = 0;
480 req.nl.nlmsg_seq = ++seq;
481 memset(&msg, 0, sizeof(msg));
482 iov.iov_base = (void *)&req.nl;
483 iov.iov_len = req.nl.nlmsg_len;
484 msg.msg_iov = &iov;
485 msg.msg_iovlen = 1;
486 ret = sendmsg(sock, &msg, 0);
487 if (ret < 0) {
488 printf("send to netlink: %s\n", strerror(errno));
489 ret = -1;
490 goto cleanup;
491 }
492 memset(buf, 0, sizeof(buf));
493 nll = recv_msg(sa, sock);
494 if (nll < 0) {
495 printf("recv from netlink: %s\n", strerror(nll));
496 ret = -1;
497 goto cleanup;
498 }
499 nh = (struct nlmsghdr *)buf;
500 read_arp(nh, nll);
501cleanup:
502 close(sock);
503 return ret;
504}
505
506/* Function to keep track and update changes in route and arp table
507 * Give regular statistics of packets forwarded
508 */
509static int monitor_route(void)
510{
511 unsigned int nr_cpus = bpf_num_possible_cpus();
512 const unsigned int nr_keys = 256;
513 struct pollfd fds_route, fds_arp;
514 __u64 prev[nr_keys][nr_cpus];
515 struct sockaddr_nl la, lr;
516 __u64 values[nr_cpus];
517 struct nlmsghdr *nh;
518 int nll, ret = 0;
519 int interval = 5;
520 __u32 key;
521 int i;
522
523 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
524 if (sock < 0) {
525 printf("open netlink socket: %s\n", strerror(errno));
526 return -1;
527 }
528
529 fcntl(sock, F_SETFL, O_NONBLOCK);
530 memset(&lr, 0, sizeof(lr));
531 lr.nl_family = AF_NETLINK;
532 lr.nl_groups = RTMGRP_IPV6_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_NOTIFY;
533 if (bind(sock, (struct sockaddr *)&lr, sizeof(lr)) < 0) {
534 printf("bind to netlink: %s\n", strerror(errno));
535 ret = -1;
536 goto cleanup;
537 }
538 fds_route.fd = sock;
539 fds_route.events = POLL_IN;
540
541 sock_arp = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
542 if (sock_arp < 0) {
543 printf("open netlink socket: %s\n", strerror(errno));
544 return -1;
545 }
546
547 fcntl(sock_arp, F_SETFL, O_NONBLOCK);
548 memset(&la, 0, sizeof(la));
549 la.nl_family = AF_NETLINK;
550 la.nl_groups = RTMGRP_NEIGH | RTMGRP_NOTIFY;
551 if (bind(sock_arp, (struct sockaddr *)&la, sizeof(la)) < 0) {
552 printf("bind to netlink: %s\n", strerror(errno));
553 ret = -1;
554 goto cleanup;
555 }
556 fds_arp.fd = sock_arp;
557 fds_arp.events = POLL_IN;
558
559 memset(prev, 0, sizeof(prev));
560 do {
561 signal(SIGINT, close_and_exit);
562 signal(SIGTERM, close_and_exit);
563
564 sleep(interval);
565 for (key = 0; key < nr_keys; key++) {
566 __u64 sum = 0;
567
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100568 assert(bpf_map_lookup_elem(rxcnt_map_fd,
569 &key, values) == 0);
Christina Jacob3e29cd02017-11-05 08:52:30 +0530570 for (i = 0; i < nr_cpus; i++)
571 sum += (values[i] - prev[key][i]);
572 if (sum)
573 printf("proto %u: %10llu pkt/s\n",
574 key, sum / interval);
575 memcpy(prev[key], values, sizeof(values));
576 }
577
578 memset(buf, 0, sizeof(buf));
579 if (poll(&fds_route, 1, 3) == POLL_IN) {
580 nll = recv_msg(lr, sock);
581 if (nll < 0) {
582 printf("recv from netlink: %s\n", strerror(nll));
583 ret = -1;
584 goto cleanup;
585 }
586
587 nh = (struct nlmsghdr *)buf;
588 printf("Routing table updated.\n");
589 read_route(nh, nll);
590 }
591 memset(buf, 0, sizeof(buf));
592 if (poll(&fds_arp, 1, 3) == POLL_IN) {
593 nll = recv_msg(la, sock_arp);
594 if (nll < 0) {
595 printf("recv from netlink: %s\n", strerror(nll));
596 ret = -1;
597 goto cleanup;
598 }
599
600 nh = (struct nlmsghdr *)buf;
601 read_arp(nh, nll);
602 }
603
604 } while (1);
605cleanup:
606 close(sock);
607 return ret;
608}
609
610int main(int ac, char **argv)
611{
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100612 struct bpf_prog_load_attr prog_load_attr = {
613 .prog_type = BPF_PROG_TYPE_XDP,
614 };
615 struct bpf_object *obj;
Christina Jacob3e29cd02017-11-05 08:52:30 +0530616 char filename[256];
617 char **ifname_list;
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100618 int prog_fd;
Christina Jacob3e29cd02017-11-05 08:52:30 +0530619 int i = 1;
620
621 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100622 prog_load_attr.file = filename;
623
Christina Jacob3e29cd02017-11-05 08:52:30 +0530624 if (ac < 2) {
625 printf("usage: %s [-S] Interface name list\n", argv[0]);
626 return 1;
627 }
628 if (!strcmp(argv[1], "-S")) {
629 flags = XDP_FLAGS_SKB_MODE;
630 total_ifindex = ac - 2;
631 ifname_list = (argv + 2);
632 } else {
633 flags = 0;
634 total_ifindex = ac - 1;
635 ifname_list = (argv + 1);
636 }
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100637
638 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
Christina Jacob3e29cd02017-11-05 08:52:30 +0530639 return 1;
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100640
Christina Jacob3e29cd02017-11-05 08:52:30 +0530641 printf("\n**************loading bpf file*********************\n\n\n");
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100642 if (!prog_fd) {
643 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
Christina Jacob3e29cd02017-11-05 08:52:30 +0530644 return 1;
645 }
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100646
647 lpm_map_fd = bpf_object__find_map_fd_by_name(obj, "lpm_map");
648 rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
649 arp_table_map_fd = bpf_object__find_map_fd_by_name(obj, "arp_table");
650 exact_match_map_fd = bpf_object__find_map_fd_by_name(obj,
651 "exact_match");
652 tx_port_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_port");
653 if (lpm_map_fd < 0 || rxcnt_map_fd < 0 || arp_table_map_fd < 0 ||
654 exact_match_map_fd < 0 || tx_port_map_fd < 0) {
655 printf("bpf_object__find_map_fd_by_name failed\n");
656 return 1;
657 }
658
Christina Jacob3e29cd02017-11-05 08:52:30 +0530659 ifindex_list = (int *)malloc(total_ifindex * sizeof(int *));
660 for (i = 0; i < total_ifindex; i++) {
661 ifindex_list[i] = if_nametoindex(ifname_list[i]);
662 if (!ifindex_list[i]) {
663 printf("Couldn't translate interface name: %s",
664 strerror(errno));
665 return 1;
666 }
667 }
668 for (i = 0; i < total_ifindex; i++) {
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100669 if (bpf_set_link_xdp_fd(ifindex_list[i], prog_fd, flags) < 0) {
Christina Jacob3e29cd02017-11-05 08:52:30 +0530670 printf("link set xdp fd failed\n");
671 int recovery_index = i;
672
673 for (i = 0; i < recovery_index; i++)
Eric Leblondb259c2f2018-01-30 21:55:04 +0100674 bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
Christina Jacob3e29cd02017-11-05 08:52:30 +0530675
676 return 1;
677 }
678 printf("Attached to %d\n", ifindex_list[i]);
679 }
680 signal(SIGINT, int_exit);
681 signal(SIGTERM, int_exit);
682
683 printf("*******************ROUTE TABLE*************************\n\n\n");
684 get_route_table(AF_INET);
685 printf("*******************ARP TABLE***************************\n\n\n");
686 get_arp_table(AF_INET);
687 if (monitor_route() < 0) {
688 printf("Error in receiving route update");
689 return 1;
690 }
691
692 return 0;
693}