Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* Copyright (C) 2017 Cavium, Inc.
  3 */
  4#include <linux/bpf.h>
  5#include <linux/netlink.h>
  6#include <linux/rtnetlink.h>
  7#include <assert.h>
  8#include <errno.h>
  9#include <signal.h>
 10#include <stdio.h>
 11#include <stdlib.h>
 12#include <string.h>
 13#include <sys/socket.h>
 14#include <unistd.h>
 15#include <bpf/bpf.h>
 16#include <arpa/inet.h>
 17#include <fcntl.h>
 18#include <poll.h>
 19#include <net/if.h>
 20#include <netdb.h>
 21#include <sys/ioctl.h>
 22#include <sys/syscall.h>
 23#include "bpf_util.h"
 24#include <bpf/libbpf.h>
 
 25#include <libgen.h>
 26#include <getopt.h>
 27#include <pthread.h>
 28#include "xdp_sample_user.h"
 29#include "xdp_router_ipv4.skel.h"
 30
 31static const char *__doc__ =
 32"XDP IPv4 router implementation\n"
 33"Usage: xdp_router_ipv4 <IFNAME-0> ... <IFNAME-N>\n";
 34
 35static char buf[8192];
 
 
 
 
 36static int lpm_map_fd;
 
 37static int arp_table_map_fd;
 38static int exact_match_map_fd;
 39static int tx_port_map_fd;
 40
 41static bool routes_thread_exit;
 42static int interval = 5;
 
 
 
 43
 44static int mask = SAMPLE_RX_CNT | SAMPLE_REDIRECT_ERR_MAP_CNT |
 45		  SAMPLE_DEVMAP_XMIT_CNT_MULTI | SAMPLE_EXCEPTION_CNT;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 46
 47DEFINE_SAMPLE_INIT(xdp_router_ipv4);
 
 
 
 48
 49static const struct option long_options[] = {
 50	{ "help", no_argument, NULL, 'h' },
 51	{ "skb-mode", no_argument, NULL, 'S' },
 52	{ "force", no_argument, NULL, 'F' },
 53	{ "interval", required_argument, NULL, 'i' },
 54	{ "verbose", no_argument, NULL, 'v' },
 55	{ "stats", no_argument, NULL, 's' },
 56	{}
 57};
 58
 59static int get_route_table(int rtm_family);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 60
 61static int recv_msg(struct sockaddr_nl sock_addr, int sock)
 62{
 63	struct nlmsghdr *nh;
 64	int len, nll = 0;
 65	char *buf_ptr;
 66
 67	buf_ptr = buf;
 68	while (1) {
 69		len = recv(sock, buf_ptr, sizeof(buf) - nll, 0);
 70		if (len < 0)
 71			return len;
 72
 73		nh = (struct nlmsghdr *)buf_ptr;
 74
 75		if (nh->nlmsg_type == NLMSG_DONE)
 76			break;
 77		buf_ptr += len;
 78		nll += len;
 79		if ((sock_addr.nl_groups & RTMGRP_NEIGH) == RTMGRP_NEIGH)
 80			break;
 81
 82		if ((sock_addr.nl_groups & RTMGRP_IPV4_ROUTE) == RTMGRP_IPV4_ROUTE)
 83			break;
 84	}
 85	return nll;
 86}
 87
 88/* Function to parse the route entry returned by netlink
 89 * Updates the route entry related map entries
 90 */
 91static void read_route(struct nlmsghdr *nh, int nll)
 92{
 93	char dsts[24], gws[24], ifs[16], dsts_len[24], metrics[24];
 94	struct bpf_lpm_trie_key_u8 *prefix_key;
 95	struct rtattr *rt_attr;
 96	struct rtmsg *rt_msg;
 97	int rtm_family;
 98	int rtl;
 99	int i;
100	struct route_table {
101		int  dst_len, iface, metric;
 
102		__be32 dst, gw;
103		__be64 mac;
104	} route;
105	struct arp_table {
106		__be64 mac;
107		__be32 dst;
108	};
109
110	struct direct_map {
111		struct arp_table arp;
112		int ifindex;
113		__be64 mac;
114	} direct_entry;
115
 
 
 
 
 
 
 
 
 
116	memset(&route, 0, sizeof(route));
 
117	for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
118		rt_msg = (struct rtmsg *)NLMSG_DATA(nh);
119		rtm_family = rt_msg->rtm_family;
120		if (rtm_family == AF_INET)
121			if (rt_msg->rtm_table != RT_TABLE_MAIN)
122				continue;
123		rt_attr = (struct rtattr *)RTM_RTA(rt_msg);
124		rtl = RTM_PAYLOAD(nh);
125
126		for (; RTA_OK(rt_attr, rtl); rt_attr = RTA_NEXT(rt_attr, rtl)) {
127			switch (rt_attr->rta_type) {
128			case NDA_DST:
129				sprintf(dsts, "%u",
130					(*((__be32 *)RTA_DATA(rt_attr))));
131				break;
132			case RTA_GATEWAY:
133				sprintf(gws, "%u",
134					*((__be32 *)RTA_DATA(rt_attr)));
135				break;
136			case RTA_OIF:
137				sprintf(ifs, "%u",
138					*((int *)RTA_DATA(rt_attr)));
139				break;
140			case RTA_METRICS:
141				sprintf(metrics, "%u",
142					*((int *)RTA_DATA(rt_attr)));
143			default:
144				break;
145			}
146		}
147		sprintf(dsts_len, "%d", rt_msg->rtm_dst_len);
148		route.dst = atoi(dsts);
149		route.dst_len = atoi(dsts_len);
150		route.gw = atoi(gws);
151		route.iface = atoi(ifs);
152		route.metric = atoi(metrics);
153		assert(get_mac_addr(route.iface, &route.mac) == 0);
 
 
 
 
154		assert(bpf_map_update_elem(tx_port_map_fd,
155					   &route.iface, &route.iface, 0) == 0);
156		if (rtm_family == AF_INET) {
157			struct trie_value {
158				__u8 prefix[4];
159				__be64 value;
160				int ifindex;
161				int metric;
162				__be32 gw;
163			} *prefix_value;
164
165			prefix_key = alloca(sizeof(*prefix_key) + 4);
166			prefix_value = alloca(sizeof(*prefix_value));
167
168			prefix_key->prefixlen = 32;
169			prefix_key->prefixlen = route.dst_len;
170			direct_entry.mac = route.mac & 0xffffffffffff;
171			direct_entry.ifindex = route.iface;
172			direct_entry.arp.mac = 0;
173			direct_entry.arp.dst = 0;
174			if (route.dst_len == 32) {
175				if (nh->nlmsg_type == RTM_DELROUTE) {
176					assert(bpf_map_delete_elem(exact_match_map_fd,
177								   &route.dst) == 0);
178				} else {
179					if (bpf_map_lookup_elem(arp_table_map_fd,
180								&route.dst,
181								&direct_entry.arp.mac) == 0)
182						direct_entry.arp.dst = route.dst;
183					assert(bpf_map_update_elem(exact_match_map_fd,
184								   &route.dst,
185								   &direct_entry, 0) == 0);
186				}
187			}
188			for (i = 0; i < 4; i++)
189				prefix_key->data[i] = (route.dst >> i * 8) & 0xff;
190
 
 
 
 
 
 
 
 
191			if (bpf_map_lookup_elem(lpm_map_fd, prefix_key,
192						prefix_value) < 0) {
193				for (i = 0; i < 4; i++)
194					prefix_value->prefix[i] = prefix_key->data[i];
195				prefix_value->value = route.mac & 0xffffffffffff;
196				prefix_value->ifindex = route.iface;
197				prefix_value->gw = route.gw;
198				prefix_value->metric = route.metric;
199
200				assert(bpf_map_update_elem(lpm_map_fd,
201							   prefix_key,
202							   prefix_value, 0
203							   ) == 0);
204			} else {
205				if (nh->nlmsg_type == RTM_DELROUTE) {
 
 
 
 
 
 
 
206					assert(bpf_map_delete_elem(lpm_map_fd,
207								   prefix_key
208								   ) == 0);
209					/* Rereading the route table to check if
210					 * there is an entry with the same
211					 * prefix but a different metric as the
212					 * deleted entry.
213					 */
214					get_route_table(AF_INET);
215				} else if (prefix_key->data[0] ==
216					   prefix_value->prefix[0] &&
217					   prefix_key->data[1] ==
218					   prefix_value->prefix[1] &&
219					   prefix_key->data[2] ==
220					   prefix_value->prefix[2] &&
221					   prefix_key->data[3] ==
222					   prefix_value->prefix[3] &&
223					   route.metric >= prefix_value->metric) {
224					continue;
225				} else {
226					for (i = 0; i < 4; i++)
227						prefix_value->prefix[i] =
228							prefix_key->data[i];
229					prefix_value->value =
230						route.mac & 0xffffffffffff;
231					prefix_value->ifindex = route.iface;
232					prefix_value->gw = route.gw;
233					prefix_value->metric = route.metric;
234					assert(bpf_map_update_elem(lpm_map_fd,
235								   prefix_key,
236								   prefix_value,
237								   0) == 0);
238				}
239			}
240		}
241		memset(&route, 0, sizeof(route));
242		memset(dsts, 0, sizeof(dsts));
243		memset(dsts_len, 0, sizeof(dsts_len));
244		memset(gws, 0, sizeof(gws));
245		memset(ifs, 0, sizeof(ifs));
246		memset(&route, 0, sizeof(route));
247	}
248}
249
250/* Function to read the existing route table  when the process is launched*/
251static int get_route_table(int rtm_family)
252{
253	struct sockaddr_nl sa;
254	struct nlmsghdr *nh;
255	int sock, seq = 0;
256	struct msghdr msg;
257	struct iovec iov;
258	int ret = 0;
259	int nll;
260
261	struct {
262		struct nlmsghdr nl;
263		struct rtmsg rt;
264		char buf[8192];
265	} req;
266
267	sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
268	if (sock < 0) {
269		fprintf(stderr, "open netlink socket: %s\n", strerror(errno));
270		return -errno;
271	}
272	memset(&sa, 0, sizeof(sa));
273	sa.nl_family = AF_NETLINK;
274	if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
275		fprintf(stderr, "bind netlink socket: %s\n", strerror(errno));
276		ret = -errno;
277		goto cleanup;
278	}
279	memset(&req, 0, sizeof(req));
280	req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
281	req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
282	req.nl.nlmsg_type = RTM_GETROUTE;
283
284	req.rt.rtm_family = rtm_family;
285	req.rt.rtm_table = RT_TABLE_MAIN;
286	req.nl.nlmsg_pid = 0;
287	req.nl.nlmsg_seq = ++seq;
288	memset(&msg, 0, sizeof(msg));
289	iov.iov_base = (void *)&req.nl;
290	iov.iov_len = req.nl.nlmsg_len;
291	msg.msg_iov = &iov;
292	msg.msg_iovlen = 1;
293	ret = sendmsg(sock, &msg, 0);
294	if (ret < 0) {
295		fprintf(stderr, "send to netlink: %s\n", strerror(errno));
296		ret = -errno;
297		goto cleanup;
298	}
299	memset(buf, 0, sizeof(buf));
300	nll = recv_msg(sa, sock);
301	if (nll < 0) {
302		fprintf(stderr, "recv from netlink: %s\n", strerror(nll));
303		ret = nll;
304		goto cleanup;
305	}
306	nh = (struct nlmsghdr *)buf;
307	read_route(nh, nll);
308cleanup:
309	close(sock);
310	return ret;
311}
312
313/* Function to parse the arp entry returned by netlink
314 * Updates the arp entry related map entries
315 */
316static void read_arp(struct nlmsghdr *nh, int nll)
317{
318	struct rtattr *rt_attr;
319	char dsts[24], mac[24];
320	struct ndmsg *rt_msg;
321	int rtl, ndm_family;
322
323	struct arp_table {
324		__be64 mac;
325		__be32 dst;
326	} arp_entry;
327	struct direct_map {
328		struct arp_table arp;
329		int ifindex;
330		__be64 mac;
331	} direct_entry;
332
 
 
 
333	for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
334		rt_msg = (struct ndmsg *)NLMSG_DATA(nh);
335		rt_attr = (struct rtattr *)RTM_RTA(rt_msg);
336		ndm_family = rt_msg->ndm_family;
337		rtl = RTM_PAYLOAD(nh);
338		for (; RTA_OK(rt_attr, rtl); rt_attr = RTA_NEXT(rt_attr, rtl)) {
339			switch (rt_attr->rta_type) {
340			case NDA_DST:
341				sprintf(dsts, "%u",
342					*((__be32 *)RTA_DATA(rt_attr)));
343				break;
344			case NDA_LLADDR:
345				sprintf(mac, "%lld",
346					*((__be64 *)RTA_DATA(rt_attr)));
347				break;
348			default:
349				break;
350			}
351		}
352		arp_entry.dst = atoi(dsts);
353		arp_entry.mac = atol(mac);
354
355		if (ndm_family == AF_INET) {
356			if (bpf_map_lookup_elem(exact_match_map_fd,
357						&arp_entry.dst,
358						&direct_entry) == 0) {
359				if (nh->nlmsg_type == RTM_DELNEIGH) {
360					direct_entry.arp.dst = 0;
361					direct_entry.arp.mac = 0;
362				} else if (nh->nlmsg_type == RTM_NEWNEIGH) {
363					direct_entry.arp.dst = arp_entry.dst;
364					direct_entry.arp.mac = arp_entry.mac;
365				}
366				assert(bpf_map_update_elem(exact_match_map_fd,
367							   &arp_entry.dst,
368							   &direct_entry, 0
369							   ) == 0);
370				memset(&direct_entry, 0, sizeof(direct_entry));
371			}
372			if (nh->nlmsg_type == RTM_DELNEIGH) {
373				assert(bpf_map_delete_elem(arp_table_map_fd,
374							   &arp_entry.dst) == 0);
375			} else if (nh->nlmsg_type == RTM_NEWNEIGH) {
376				assert(bpf_map_update_elem(arp_table_map_fd,
377							   &arp_entry.dst,
378							   &arp_entry.mac, 0
379							   ) == 0);
380			}
381		}
382		memset(&arp_entry, 0, sizeof(arp_entry));
383		memset(dsts, 0, sizeof(dsts));
384	}
385}
386
387/* Function to read the existing arp table  when the process is launched*/
388static int get_arp_table(int rtm_family)
389{
390	struct sockaddr_nl sa;
391	struct nlmsghdr *nh;
392	int sock, seq = 0;
393	struct msghdr msg;
394	struct iovec iov;
395	int ret = 0;
396	int nll;
397	struct {
398		struct nlmsghdr nl;
399		struct ndmsg rt;
400		char buf[8192];
401	} req;
402
403	sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
404	if (sock < 0) {
405		fprintf(stderr, "open netlink socket: %s\n", strerror(errno));
406		return -errno;
407	}
408	memset(&sa, 0, sizeof(sa));
409	sa.nl_family = AF_NETLINK;
410	if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
411		fprintf(stderr, "bind netlink socket: %s\n", strerror(errno));
412		ret = -errno;
413		goto cleanup;
414	}
415	memset(&req, 0, sizeof(req));
416	req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
417	req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
418	req.nl.nlmsg_type = RTM_GETNEIGH;
419	req.rt.ndm_state = NUD_REACHABLE;
420	req.rt.ndm_family = rtm_family;
421	req.nl.nlmsg_pid = 0;
422	req.nl.nlmsg_seq = ++seq;
423	memset(&msg, 0, sizeof(msg));
424	iov.iov_base = (void *)&req.nl;
425	iov.iov_len = req.nl.nlmsg_len;
426	msg.msg_iov = &iov;
427	msg.msg_iovlen = 1;
428	ret = sendmsg(sock, &msg, 0);
429	if (ret < 0) {
430		fprintf(stderr, "send to netlink: %s\n", strerror(errno));
431		ret = -errno;
432		goto cleanup;
433	}
434	memset(buf, 0, sizeof(buf));
435	nll = recv_msg(sa, sock);
436	if (nll < 0) {
437		fprintf(stderr, "recv from netlink: %s\n", strerror(nll));
438		ret = nll;
439		goto cleanup;
440	}
441	nh = (struct nlmsghdr *)buf;
442	read_arp(nh, nll);
443cleanup:
444	close(sock);
445	return ret;
446}
447
448/* Function to keep track and update changes in route and arp table
449 * Give regular statistics of packets forwarded
450 */
451static void *monitor_routes_thread(void *arg)
452{
 
 
453	struct pollfd fds_route, fds_arp;
 
454	struct sockaddr_nl la, lr;
455	int sock, sock_arp, nll;
456	struct nlmsghdr *nh;
 
 
 
 
457
458	sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
459	if (sock < 0) {
460		fprintf(stderr, "open netlink socket: %s\n", strerror(errno));
461		return NULL;
462	}
463
464	fcntl(sock, F_SETFL, O_NONBLOCK);
465	memset(&lr, 0, sizeof(lr));
466	lr.nl_family = AF_NETLINK;
467	lr.nl_groups = RTMGRP_IPV6_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_NOTIFY;
468	if (bind(sock, (struct sockaddr *)&lr, sizeof(lr)) < 0) {
469		fprintf(stderr, "bind netlink socket: %s\n", strerror(errno));
470		close(sock);
471		return NULL;
472	}
473
474	fds_route.fd = sock;
475	fds_route.events = POLL_IN;
476
477	sock_arp = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
478	if (sock_arp < 0) {
479		fprintf(stderr, "open netlink socket: %s\n", strerror(errno));
480		close(sock);
481		return NULL;
482	}
483
484	fcntl(sock_arp, F_SETFL, O_NONBLOCK);
485	memset(&la, 0, sizeof(la));
486	la.nl_family = AF_NETLINK;
487	la.nl_groups = RTMGRP_NEIGH | RTMGRP_NOTIFY;
488	if (bind(sock_arp, (struct sockaddr *)&la, sizeof(la)) < 0) {
489		fprintf(stderr, "bind netlink socket: %s\n", strerror(errno));
 
490		goto cleanup;
491	}
492
493	fds_arp.fd = sock_arp;
494	fds_arp.events = POLL_IN;
495
496	/* dump route and arp tables */
497	if (get_arp_table(AF_INET) < 0) {
498		fprintf(stderr, "Failed reading arp table\n");
499		goto cleanup;
500	}
501
502	if (get_route_table(AF_INET) < 0) {
503		fprintf(stderr, "Failed reading route table\n");
504		goto cleanup;
505	}
 
 
 
 
 
 
 
 
 
506
507	while (!routes_thread_exit) {
508		memset(buf, 0, sizeof(buf));
509		if (poll(&fds_route, 1, 3) == POLL_IN) {
510			nll = recv_msg(lr, sock);
511			if (nll < 0) {
512				fprintf(stderr, "recv from netlink: %s\n",
513					strerror(nll));
514				goto cleanup;
515			}
516
517			nh = (struct nlmsghdr *)buf;
 
518			read_route(nh, nll);
519		}
520
521		memset(buf, 0, sizeof(buf));
522		if (poll(&fds_arp, 1, 3) == POLL_IN) {
523			nll = recv_msg(la, sock_arp);
524			if (nll < 0) {
525				fprintf(stderr, "recv from netlink: %s\n",
526					strerror(nll));
527				goto cleanup;
528			}
529
530			nh = (struct nlmsghdr *)buf;
531			read_arp(nh, nll);
532		}
533
534		sleep(interval);
535	}
536
537cleanup:
538	close(sock_arp);
539	close(sock);
540	return NULL;
541}
542
543static void usage(char *argv[], const struct option *long_options,
544		  const char *doc, int mask, bool error,
545		  struct bpf_object *obj)
546{
547	sample_usage(argv, long_options, doc, mask, error);
 
 
 
 
 
548}
549
550int main(int argc, char **argv)
551{
552	bool error = true, generic = false, force = false;
553	int opt, ret = EXIT_FAIL_BPF;
554	struct xdp_router_ipv4 *skel;
555	int i, total_ifindex = argc - 1;
556	char **ifname_list = argv + 1;
557	pthread_t routes_thread;
558	int longindex = 0;
559
560	if (libbpf_set_strict_mode(LIBBPF_STRICT_ALL) < 0) {
561		fprintf(stderr, "Failed to set libbpf strict mode: %s\n",
562			strerror(errno));
563		goto end;
564	}
565
566	skel = xdp_router_ipv4__open();
567	if (!skel) {
568		fprintf(stderr, "Failed to xdp_router_ipv4__open: %s\n",
569			strerror(errno));
570		goto end;
571	}
572
573	ret = sample_init_pre_load(skel);
574	if (ret < 0) {
575		fprintf(stderr, "Failed to sample_init_pre_load: %s\n",
576			strerror(-ret));
577		ret = EXIT_FAIL_BPF;
578		goto end_destroy;
579	}
580
581	ret = xdp_router_ipv4__load(skel);
582	if (ret < 0) {
583		fprintf(stderr, "Failed to xdp_router_ipv4__load: %s\n",
584			strerror(errno));
585		goto end_destroy;
586	}
587
588	ret = sample_init(skel, mask);
589	if (ret < 0) {
590		fprintf(stderr, "Failed to initialize sample: %s\n", strerror(-ret));
591		ret = EXIT_FAIL;
592		goto end_destroy;
593	}
594
595	while ((opt = getopt_long(argc, argv, "si:SFvh",
596				  long_options, &longindex)) != -1) {
597		switch (opt) {
598		case 's':
599			mask |= SAMPLE_REDIRECT_MAP_CNT;
600			total_ifindex--;
601			ifname_list++;
602			break;
603		case 'i':
604			interval = strtoul(optarg, NULL, 0);
605			total_ifindex -= 2;
606			ifname_list += 2;
607			break;
608		case 'S':
609			generic = true;
610			total_ifindex--;
611			ifname_list++;
612			break;
613		case 'F':
614			force = true;
615			total_ifindex--;
616			ifname_list++;
617			break;
618		case 'v':
619			sample_switch_mode();
620			total_ifindex--;
621			ifname_list++;
622			break;
623		case 'h':
624			error = false;
625		default:
626			usage(argv, long_options, __doc__, mask, error, skel->obj);
627			goto end_destroy;
628		}
629	}
630
631	ret = EXIT_FAIL_OPTION;
632	if (optind == argc) {
633		usage(argv, long_options, __doc__, mask, true, skel->obj);
634		goto end_destroy;
635	}
636
637	lpm_map_fd = bpf_map__fd(skel->maps.lpm_map);
638	if (lpm_map_fd < 0) {
639		fprintf(stderr, "Failed loading lpm_map %s\n",
640			strerror(-lpm_map_fd));
641		goto end_destroy;
642	}
643	arp_table_map_fd = bpf_map__fd(skel->maps.arp_table);
644	if (arp_table_map_fd < 0) {
645		fprintf(stderr, "Failed loading arp_table_map_fd %s\n",
646			strerror(-arp_table_map_fd));
647		goto end_destroy;
648	}
649	exact_match_map_fd = bpf_map__fd(skel->maps.exact_match);
650	if (exact_match_map_fd < 0) {
651		fprintf(stderr, "Failed loading exact_match_map_fd %s\n",
652			strerror(-exact_match_map_fd));
653		goto end_destroy;
 
 
 
654	}
655	tx_port_map_fd = bpf_map__fd(skel->maps.tx_port);
656	if (tx_port_map_fd < 0) {
657		fprintf(stderr, "Failed loading tx_port_map_fd %s\n",
658			strerror(-tx_port_map_fd));
659		goto end_destroy;
 
 
 
 
 
 
660	}
661
662	ret = EXIT_FAIL_XDP;
663	for (i = 0; i < total_ifindex; i++) {
664		int index = if_nametoindex(ifname_list[i]);
665
666		if (!index) {
667			fprintf(stderr, "Interface %s not found %s\n",
668				ifname_list[i], strerror(-tx_port_map_fd));
669			goto end_destroy;
670		}
671		if (sample_install_xdp(skel->progs.xdp_router_ipv4_prog,
672				       index, generic, force) < 0)
673			goto end_destroy;
674	}
 
 
 
 
 
675
676	ret = pthread_create(&routes_thread, NULL, monitor_routes_thread, NULL);
677	if (ret) {
678		fprintf(stderr, "Failed creating routes_thread: %s\n", strerror(-ret));
679		ret = EXIT_FAIL;
680		goto end_destroy;
681	}
682
683	ret = sample_run(interval, NULL, NULL);
684	routes_thread_exit = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685
686	if (ret < 0) {
687		fprintf(stderr, "Failed during sample run: %s\n", strerror(-ret));
688		ret = EXIT_FAIL;
689		goto end_thread_wait;
690	}
691	ret = EXIT_OK;
692
693end_thread_wait:
694	pthread_join(routes_thread, NULL);
695end_destroy:
696	xdp_router_ipv4__destroy(skel);
697end:
698	sample_exit(ret);
699}
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* Copyright (C) 2017 Cavium, Inc.
  3 */
  4#include <linux/bpf.h>
  5#include <linux/netlink.h>
  6#include <linux/rtnetlink.h>
  7#include <assert.h>
  8#include <errno.h>
  9#include <signal.h>
 10#include <stdio.h>
 11#include <stdlib.h>
 12#include <string.h>
 13#include <sys/socket.h>
 14#include <unistd.h>
 15#include <bpf/bpf.h>
 16#include <arpa/inet.h>
 17#include <fcntl.h>
 18#include <poll.h>
 19#include <net/if.h>
 20#include <netdb.h>
 21#include <sys/ioctl.h>
 22#include <sys/syscall.h>
 23#include "bpf_util.h"
 24#include <bpf/libbpf.h>
 25#include <sys/resource.h>
 26#include <libgen.h>
 
 
 
 
 
 
 
 
 27
 28int sock, sock_arp, flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
 29static int total_ifindex;
 30static int *ifindex_list;
 31static __u32 *prog_id_list;
 32char buf[8192];
 33static 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;
 38
 39static int get_route_table(int rtm_family);
 40static void int_exit(int sig)
 41{
 42	__u32 prog_id = 0;
 43	int i = 0;
 44
 45	for (i = 0; i < total_ifindex; i++) {
 46		if (bpf_get_link_xdp_id(ifindex_list[i], &prog_id, flags)) {
 47			printf("bpf_get_link_xdp_id on iface %d failed\n",
 48			       ifindex_list[i]);
 49			exit(1);
 50		}
 51		if (prog_id_list[i] == prog_id)
 52			bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
 53		else if (!prog_id)
 54			printf("couldn't find a prog id on iface %d\n",
 55			       ifindex_list[i]);
 56		else
 57			printf("program on iface %d changed, not removing\n",
 58			       ifindex_list[i]);
 59		prog_id = 0;
 60	}
 61	exit(0);
 62}
 63
 64static void close_and_exit(int sig)
 65{
 66	close(sock);
 67	close(sock_arp);
 68
 69	int_exit(0);
 70}
 
 
 
 
 
 
 
 71
 72/* Get the mac address of the interface given interface name */
 73static __be64 getmac(char *iface)
 74{
 75	struct ifreq ifr;
 76	__be64 mac = 0;
 77	int fd, i;
 78
 79	fd = socket(AF_INET, SOCK_DGRAM, 0);
 80	ifr.ifr_addr.sa_family = AF_INET;
 81	strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);
 82	if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
 83		printf("ioctl failed leaving....\n");
 84		return -1;
 85	}
 86	for (i = 0; i < 6 ; i++)
 87		*((__u8 *)&mac + i) = (__u8)ifr.ifr_hwaddr.sa_data[i];
 88	close(fd);
 89	return mac;
 90}
 91
 92static int recv_msg(struct sockaddr_nl sock_addr, int sock)
 93{
 94	struct nlmsghdr *nh;
 95	int len, nll = 0;
 96	char *buf_ptr;
 97
 98	buf_ptr = buf;
 99	while (1) {
100		len = recv(sock, buf_ptr, sizeof(buf) - nll, 0);
101		if (len < 0)
102			return len;
103
104		nh = (struct nlmsghdr *)buf_ptr;
105
106		if (nh->nlmsg_type == NLMSG_DONE)
107			break;
108		buf_ptr += len;
109		nll += len;
110		if ((sock_addr.nl_groups & RTMGRP_NEIGH) == RTMGRP_NEIGH)
111			break;
112
113		if ((sock_addr.nl_groups & RTMGRP_IPV4_ROUTE) == RTMGRP_IPV4_ROUTE)
114			break;
115	}
116	return nll;
117}
118
119/* Function to parse the route entry returned by netlink
120 * Updates the route entry related map entries
121 */
122static void read_route(struct nlmsghdr *nh, int nll)
123{
124	char dsts[24], gws[24], ifs[16], dsts_len[24], metrics[24];
125	struct bpf_lpm_trie_key *prefix_key;
126	struct rtattr *rt_attr;
127	struct rtmsg *rt_msg;
128	int rtm_family;
129	int rtl;
130	int i;
131	struct route_table {
132		int  dst_len, iface, metric;
133		char *iface_name;
134		__be32 dst, gw;
135		__be64 mac;
136	} route;
137	struct arp_table {
138		__be64 mac;
139		__be32 dst;
140	};
141
142	struct direct_map {
143		struct arp_table arp;
144		int ifindex;
145		__be64 mac;
146	} direct_entry;
147
148	if (nh->nlmsg_type == RTM_DELROUTE)
149		printf("DELETING Route entry\n");
150	else if (nh->nlmsg_type == RTM_GETROUTE)
151		printf("READING Route entry\n");
152	else if (nh->nlmsg_type == RTM_NEWROUTE)
153		printf("NEW Route entry\n");
154	else
155		printf("%d\n", nh->nlmsg_type);
156
157	memset(&route, 0, sizeof(route));
158	printf("Destination\t\tGateway\t\tGenmask\t\tMetric\t\tIface\n");
159	for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
160		rt_msg = (struct rtmsg *)NLMSG_DATA(nh);
161		rtm_family = rt_msg->rtm_family;
162		if (rtm_family == AF_INET)
163			if (rt_msg->rtm_table != RT_TABLE_MAIN)
164				continue;
165		rt_attr = (struct rtattr *)RTM_RTA(rt_msg);
166		rtl = RTM_PAYLOAD(nh);
167
168		for (; RTA_OK(rt_attr, rtl); rt_attr = RTA_NEXT(rt_attr, rtl)) {
169			switch (rt_attr->rta_type) {
170			case NDA_DST:
171				sprintf(dsts, "%u",
172					(*((__be32 *)RTA_DATA(rt_attr))));
173				break;
174			case RTA_GATEWAY:
175				sprintf(gws, "%u",
176					*((__be32 *)RTA_DATA(rt_attr)));
177				break;
178			case RTA_OIF:
179				sprintf(ifs, "%u",
180					*((int *)RTA_DATA(rt_attr)));
181				break;
182			case RTA_METRICS:
183				sprintf(metrics, "%u",
184					*((int *)RTA_DATA(rt_attr)));
185			default:
186				break;
187			}
188		}
189		sprintf(dsts_len, "%d", rt_msg->rtm_dst_len);
190		route.dst = atoi(dsts);
191		route.dst_len = atoi(dsts_len);
192		route.gw = atoi(gws);
193		route.iface = atoi(ifs);
194		route.metric = atoi(metrics);
195		route.iface_name = alloca(sizeof(char *) * IFNAMSIZ);
196		route.iface_name = if_indextoname(route.iface, route.iface_name);
197		route.mac = getmac(route.iface_name);
198		if (route.mac == -1)
199			int_exit(0);
200		assert(bpf_map_update_elem(tx_port_map_fd,
201					   &route.iface, &route.iface, 0) == 0);
202		if (rtm_family == AF_INET) {
203			struct trie_value {
204				__u8 prefix[4];
205				__be64 value;
206				int ifindex;
207				int metric;
208				__be32 gw;
209			} *prefix_value;
210
211			prefix_key = alloca(sizeof(*prefix_key) + 3);
212			prefix_value = alloca(sizeof(*prefix_value));
213
214			prefix_key->prefixlen = 32;
215			prefix_key->prefixlen = route.dst_len;
216			direct_entry.mac = route.mac & 0xffffffffffff;
217			direct_entry.ifindex = route.iface;
218			direct_entry.arp.mac = 0;
219			direct_entry.arp.dst = 0;
220			if (route.dst_len == 32) {
221				if (nh->nlmsg_type == RTM_DELROUTE) {
222					assert(bpf_map_delete_elem(exact_match_map_fd,
223								   &route.dst) == 0);
224				} else {
225					if (bpf_map_lookup_elem(arp_table_map_fd,
226								&route.dst,
227								&direct_entry.arp.mac) == 0)
228						direct_entry.arp.dst = route.dst;
229					assert(bpf_map_update_elem(exact_match_map_fd,
230								   &route.dst,
231								   &direct_entry, 0) == 0);
232				}
233			}
234			for (i = 0; i < 4; i++)
235				prefix_key->data[i] = (route.dst >> i * 8) & 0xff;
236
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,
243			       route.metric,
244			       route.iface_name);
245			if (bpf_map_lookup_elem(lpm_map_fd, prefix_key,
246						prefix_value) < 0) {
247				for (i = 0; i < 4; i++)
248					prefix_value->prefix[i] = prefix_key->data[i];
249				prefix_value->value = route.mac & 0xffffffffffff;
250				prefix_value->ifindex = route.iface;
251				prefix_value->gw = route.gw;
252				prefix_value->metric = route.metric;
253
254				assert(bpf_map_update_elem(lpm_map_fd,
255							   prefix_key,
256							   prefix_value, 0
257							   ) == 0);
258			} else {
259				if (nh->nlmsg_type == RTM_DELROUTE) {
260					printf("deleting entry\n");
261					printf("prefix key=%d.%d.%d.%d/%d",
262					       prefix_key->data[0],
263					       prefix_key->data[1],
264					       prefix_key->data[2],
265					       prefix_key->data[3],
266					       prefix_key->prefixlen);
267					assert(bpf_map_delete_elem(lpm_map_fd,
268								   prefix_key
269								   ) == 0);
270					/* Rereading the route table to check if
271					 * there is an entry with the same
272					 * prefix but a different metric as the
273					 * deleted enty.
274					 */
275					get_route_table(AF_INET);
276				} else if (prefix_key->data[0] ==
277					   prefix_value->prefix[0] &&
278					   prefix_key->data[1] ==
279					   prefix_value->prefix[1] &&
280					   prefix_key->data[2] ==
281					   prefix_value->prefix[2] &&
282					   prefix_key->data[3] ==
283					   prefix_value->prefix[3] &&
284					   route.metric >= prefix_value->metric) {
285					continue;
286				} else {
287					for (i = 0; i < 4; i++)
288						prefix_value->prefix[i] =
289							prefix_key->data[i];
290					prefix_value->value =
291						route.mac & 0xffffffffffff;
292					prefix_value->ifindex = route.iface;
293					prefix_value->gw = route.gw;
294					prefix_value->metric = route.metric;
295					assert(bpf_map_update_elem(lpm_map_fd,
296								   prefix_key,
297								   prefix_value,
298								   0) == 0);
299				}
300			}
301		}
302		memset(&route, 0, sizeof(route));
303		memset(dsts, 0, sizeof(dsts));
304		memset(dsts_len, 0, sizeof(dsts_len));
305		memset(gws, 0, sizeof(gws));
306		memset(ifs, 0, sizeof(ifs));
307		memset(&route, 0, sizeof(route));
308	}
309}
310
311/* Function to read the existing route table  when the process is launched*/
312static int get_route_table(int rtm_family)
313{
314	struct sockaddr_nl sa;
315	struct nlmsghdr *nh;
316	int sock, seq = 0;
317	struct msghdr msg;
318	struct iovec iov;
319	int ret = 0;
320	int nll;
321
322	struct {
323		struct nlmsghdr nl;
324		struct rtmsg rt;
325		char buf[8192];
326	} req;
327
328	sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
329	if (sock < 0) {
330		printf("open netlink socket: %s\n", strerror(errno));
331		return -1;
332	}
333	memset(&sa, 0, sizeof(sa));
334	sa.nl_family = AF_NETLINK;
335	if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
336		printf("bind to netlink: %s\n", strerror(errno));
337		ret = -1;
338		goto cleanup;
339	}
340	memset(&req, 0, sizeof(req));
341	req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
342	req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
343	req.nl.nlmsg_type = RTM_GETROUTE;
344
345	req.rt.rtm_family = rtm_family;
346	req.rt.rtm_table = RT_TABLE_MAIN;
347	req.nl.nlmsg_pid = 0;
348	req.nl.nlmsg_seq = ++seq;
349	memset(&msg, 0, sizeof(msg));
350	iov.iov_base = (void *)&req.nl;
351	iov.iov_len = req.nl.nlmsg_len;
352	msg.msg_iov = &iov;
353	msg.msg_iovlen = 1;
354	ret = sendmsg(sock, &msg, 0);
355	if (ret < 0) {
356		printf("send to netlink: %s\n", strerror(errno));
357		ret = -1;
358		goto cleanup;
359	}
360	memset(buf, 0, sizeof(buf));
361	nll = recv_msg(sa, sock);
362	if (nll < 0) {
363		printf("recv from netlink: %s\n", strerror(nll));
364		ret = -1;
365		goto cleanup;
366	}
367	nh = (struct nlmsghdr *)buf;
368	read_route(nh, nll);
369cleanup:
370	close(sock);
371	return ret;
372}
373
374/* Function to parse the arp entry returned by netlink
375 * Updates the arp entry related map entries
376 */
377static void read_arp(struct nlmsghdr *nh, int nll)
378{
379	struct rtattr *rt_attr;
380	char dsts[24], mac[24];
381	struct ndmsg *rt_msg;
382	int rtl, ndm_family;
383
384	struct arp_table {
385		__be64 mac;
386		__be32 dst;
387	} arp_entry;
388	struct direct_map {
389		struct arp_table arp;
390		int ifindex;
391		__be64 mac;
392	} direct_entry;
393
394	if (nh->nlmsg_type == RTM_GETNEIGH)
395		printf("READING arp entry\n");
396	printf("Address\tHwAddress\n");
397	for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
398		rt_msg = (struct ndmsg *)NLMSG_DATA(nh);
399		rt_attr = (struct rtattr *)RTM_RTA(rt_msg);
400		ndm_family = rt_msg->ndm_family;
401		rtl = RTM_PAYLOAD(nh);
402		for (; RTA_OK(rt_attr, rtl); rt_attr = RTA_NEXT(rt_attr, rtl)) {
403			switch (rt_attr->rta_type) {
404			case NDA_DST:
405				sprintf(dsts, "%u",
406					*((__be32 *)RTA_DATA(rt_attr)));
407				break;
408			case NDA_LLADDR:
409				sprintf(mac, "%lld",
410					*((__be64 *)RTA_DATA(rt_attr)));
411				break;
412			default:
413				break;
414			}
415		}
416		arp_entry.dst = atoi(dsts);
417		arp_entry.mac = atol(mac);
418		printf("%x\t\t%llx\n", arp_entry.dst, arp_entry.mac);
419		if (ndm_family == AF_INET) {
420			if (bpf_map_lookup_elem(exact_match_map_fd,
421						&arp_entry.dst,
422						&direct_entry) == 0) {
423				if (nh->nlmsg_type == RTM_DELNEIGH) {
424					direct_entry.arp.dst = 0;
425					direct_entry.arp.mac = 0;
426				} else if (nh->nlmsg_type == RTM_NEWNEIGH) {
427					direct_entry.arp.dst = arp_entry.dst;
428					direct_entry.arp.mac = arp_entry.mac;
429				}
430				assert(bpf_map_update_elem(exact_match_map_fd,
431							   &arp_entry.dst,
432							   &direct_entry, 0
433							   ) == 0);
434				memset(&direct_entry, 0, sizeof(direct_entry));
435			}
436			if (nh->nlmsg_type == RTM_DELNEIGH) {
437				assert(bpf_map_delete_elem(arp_table_map_fd,
438							   &arp_entry.dst) == 0);
439			} else if (nh->nlmsg_type == RTM_NEWNEIGH) {
440				assert(bpf_map_update_elem(arp_table_map_fd,
441							   &arp_entry.dst,
442							   &arp_entry.mac, 0
443							   ) == 0);
444			}
445		}
446		memset(&arp_entry, 0, sizeof(arp_entry));
447		memset(dsts, 0, sizeof(dsts));
448	}
449}
450
451/* Function to read the existing arp table  when the process is launched*/
452static int get_arp_table(int rtm_family)
453{
454	struct sockaddr_nl sa;
455	struct nlmsghdr *nh;
456	int sock, seq = 0;
457	struct msghdr msg;
458	struct iovec iov;
459	int ret = 0;
460	int nll;
461	struct {
462		struct nlmsghdr nl;
463		struct ndmsg rt;
464		char buf[8192];
465	} req;
466
467	sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
468	if (sock < 0) {
469		printf("open netlink socket: %s\n", strerror(errno));
470		return -1;
471	}
472	memset(&sa, 0, sizeof(sa));
473	sa.nl_family = AF_NETLINK;
474	if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
475		printf("bind to netlink: %s\n", strerror(errno));
476		ret = -1;
477		goto cleanup;
478	}
479	memset(&req, 0, sizeof(req));
480	req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
481	req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
482	req.nl.nlmsg_type = RTM_GETNEIGH;
483	req.rt.ndm_state = NUD_REACHABLE;
484	req.rt.ndm_family = rtm_family;
485	req.nl.nlmsg_pid = 0;
486	req.nl.nlmsg_seq = ++seq;
487	memset(&msg, 0, sizeof(msg));
488	iov.iov_base = (void *)&req.nl;
489	iov.iov_len = req.nl.nlmsg_len;
490	msg.msg_iov = &iov;
491	msg.msg_iovlen = 1;
492	ret = sendmsg(sock, &msg, 0);
493	if (ret < 0) {
494		printf("send to netlink: %s\n", strerror(errno));
495		ret = -1;
496		goto cleanup;
497	}
498	memset(buf, 0, sizeof(buf));
499	nll = recv_msg(sa, sock);
500	if (nll < 0) {
501		printf("recv from netlink: %s\n", strerror(nll));
502		ret = -1;
503		goto cleanup;
504	}
505	nh = (struct nlmsghdr *)buf;
506	read_arp(nh, nll);
507cleanup:
508	close(sock);
509	return ret;
510}
511
512/* Function to keep track and update changes in route and arp table
513 * Give regular statistics of packets forwarded
514 */
515static int monitor_route(void)
516{
517	unsigned int nr_cpus = bpf_num_possible_cpus();
518	const unsigned int nr_keys = 256;
519	struct pollfd fds_route, fds_arp;
520	__u64 prev[nr_keys][nr_cpus];
521	struct sockaddr_nl la, lr;
522	__u64 values[nr_cpus];
523	struct nlmsghdr *nh;
524	int nll, ret = 0;
525	int interval = 5;
526	__u32 key;
527	int i;
528
529	sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
530	if (sock < 0) {
531		printf("open netlink socket: %s\n", strerror(errno));
532		return -1;
533	}
534
535	fcntl(sock, F_SETFL, O_NONBLOCK);
536	memset(&lr, 0, sizeof(lr));
537	lr.nl_family = AF_NETLINK;
538	lr.nl_groups = RTMGRP_IPV6_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_NOTIFY;
539	if (bind(sock, (struct sockaddr *)&lr, sizeof(lr)) < 0) {
540		printf("bind to netlink: %s\n", strerror(errno));
541		ret = -1;
542		goto cleanup;
543	}
 
544	fds_route.fd = sock;
545	fds_route.events = POLL_IN;
546
547	sock_arp = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
548	if (sock_arp < 0) {
549		printf("open netlink socket: %s\n", strerror(errno));
550		return -1;
 
551	}
552
553	fcntl(sock_arp, F_SETFL, O_NONBLOCK);
554	memset(&la, 0, sizeof(la));
555	la.nl_family = AF_NETLINK;
556	la.nl_groups = RTMGRP_NEIGH | RTMGRP_NOTIFY;
557	if (bind(sock_arp, (struct sockaddr *)&la, sizeof(la)) < 0) {
558		printf("bind to netlink: %s\n", strerror(errno));
559		ret = -1;
560		goto cleanup;
561	}
 
562	fds_arp.fd = sock_arp;
563	fds_arp.events = POLL_IN;
564
565	memset(prev, 0, sizeof(prev));
566	do {
567		signal(SIGINT, close_and_exit);
568		signal(SIGTERM, close_and_exit);
 
569
570		sleep(interval);
571		for (key = 0; key < nr_keys; key++) {
572			__u64 sum = 0;
573
574			assert(bpf_map_lookup_elem(rxcnt_map_fd,
575						   &key, values) == 0);
576			for (i = 0; i < nr_cpus; i++)
577				sum += (values[i] - prev[key][i]);
578			if (sum)
579				printf("proto %u: %10llu pkt/s\n",
580				       key, sum / interval);
581			memcpy(prev[key], values, sizeof(values));
582		}
583
 
584		memset(buf, 0, sizeof(buf));
585		if (poll(&fds_route, 1, 3) == POLL_IN) {
586			nll = recv_msg(lr, sock);
587			if (nll < 0) {
588				printf("recv from netlink: %s\n", strerror(nll));
589				ret = -1;
590				goto cleanup;
591			}
592
593			nh = (struct nlmsghdr *)buf;
594			printf("Routing table updated.\n");
595			read_route(nh, nll);
596		}
 
597		memset(buf, 0, sizeof(buf));
598		if (poll(&fds_arp, 1, 3) == POLL_IN) {
599			nll = recv_msg(la, sock_arp);
600			if (nll < 0) {
601				printf("recv from netlink: %s\n", strerror(nll));
602				ret = -1;
603				goto cleanup;
604			}
605
606			nh = (struct nlmsghdr *)buf;
607			read_arp(nh, nll);
608		}
609
610	} while (1);
 
 
611cleanup:
 
612	close(sock);
613	return ret;
614}
615
616static void usage(const char *prog)
 
 
617{
618	fprintf(stderr,
619		"%s: %s [OPTS] interface name list\n\n"
620		"OPTS:\n"
621		"    -S    use skb-mode\n"
622		"    -F    force loading prog\n",
623		__func__, prog);
624}
625
626int main(int ac, char **argv)
627{
628	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
629	struct bpf_prog_load_attr prog_load_attr = {
630		.prog_type	= BPF_PROG_TYPE_XDP,
631	};
632	struct bpf_prog_info info = {};
633	__u32 info_len = sizeof(info);
634	const char *optstr = "SF";
635	struct bpf_object *obj;
636	char filename[256];
637	char **ifname_list;
638	int prog_fd, opt;
639	int err, i = 1;
 
 
 
 
 
 
 
 
640
641	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
642	prog_load_attr.file = filename;
 
 
 
 
 
643
644	total_ifindex = ac - 1;
645	ifname_list = (argv + 1);
 
 
 
 
646
647	while ((opt = getopt(ac, argv, optstr)) != -1) {
 
 
 
 
 
 
 
 
648		switch (opt) {
 
 
 
 
 
 
 
 
 
 
649		case 'S':
650			flags |= XDP_FLAGS_SKB_MODE;
651			total_ifindex--;
652			ifname_list++;
653			break;
654		case 'F':
655			flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
656			total_ifindex--;
657			ifname_list++;
658			break;
 
 
 
 
 
 
 
659		default:
660			usage(basename(argv[0]));
661			return 1;
662		}
663	}
664
665	if (!(flags & XDP_FLAGS_SKB_MODE))
666		flags |= XDP_FLAGS_DRV_MODE;
 
 
 
667
668	if (optind == ac) {
669		usage(basename(argv[0]));
670		return 1;
 
 
671	}
672
673	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
674		perror("setrlimit(RLIMIT_MEMLOCK)");
675		return 1;
 
676	}
677
678	if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
679		return 1;
680
681	printf("\n**************loading bpf file*********************\n\n\n");
682	if (!prog_fd) {
683		printf("bpf_prog_load_xattr: %s\n", strerror(errno));
684		return 1;
685	}
686
687	lpm_map_fd = bpf_object__find_map_fd_by_name(obj, "lpm_map");
688	rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
689	arp_table_map_fd = bpf_object__find_map_fd_by_name(obj, "arp_table");
690	exact_match_map_fd = bpf_object__find_map_fd_by_name(obj,
691							     "exact_match");
692	tx_port_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_port");
693	if (lpm_map_fd < 0 || rxcnt_map_fd < 0 || arp_table_map_fd < 0 ||
694	    exact_match_map_fd < 0 || tx_port_map_fd < 0) {
695		printf("bpf_object__find_map_fd_by_name failed\n");
696		return 1;
697	}
698
699	ifindex_list = (int *)calloc(total_ifindex, sizeof(int *));
700	for (i = 0; i < total_ifindex; i++) {
701		ifindex_list[i] = if_nametoindex(ifname_list[i]);
702		if (!ifindex_list[i]) {
703			printf("Couldn't translate interface name: %s",
704			       strerror(errno));
705			return 1;
 
706		}
 
 
 
707	}
708	prog_id_list = (__u32 *)calloc(total_ifindex, sizeof(__u32 *));
709	for (i = 0; i < total_ifindex; i++) {
710		if (bpf_set_link_xdp_fd(ifindex_list[i], prog_fd, flags) < 0) {
711			printf("link set xdp fd failed\n");
712			int recovery_index = i;
713
714			for (i = 0; i < recovery_index; i++)
715				bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
 
 
 
 
716
717			return 1;
718		}
719		err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
720		if (err) {
721			printf("can't get prog info - %s\n", strerror(errno));
722			return err;
723		}
724		prog_id_list[i] = info.id;
725		memset(&info, 0, sizeof(info));
726		printf("Attached to %d\n", ifindex_list[i]);
727	}
728	signal(SIGINT, int_exit);
729	signal(SIGTERM, int_exit);
730
731	printf("*******************ROUTE TABLE*************************\n\n\n");
732	get_route_table(AF_INET);
733	printf("*******************ARP TABLE***************************\n\n\n");
734	get_arp_table(AF_INET);
735	if (monitor_route() < 0) {
736		printf("Error in receiving route update");
737		return 1;
738	}
739
740	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
741}