xdp_router_ipv4_user.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /* 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>
  18. #include "bpf_load.h"
  19. #include "libbpf.h"
  20. #include <arpa/inet.h>
  21. #include <fcntl.h>
  22. #include <poll.h>
  23. #include <net/if.h>
  24. #include <netdb.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/syscall.h>
  27. #include "bpf_util.h"
  28. int sock, sock_arp, flags = 0;
  29. static int total_ifindex;
  30. int *ifindex_list;
  31. char buf[8192];
  32. static int get_route_table(int rtm_family);
  33. static void int_exit(int sig)
  34. {
  35. int i = 0;
  36. for (i = 0; i < total_ifindex; i++)
  37. set_link_xdp_fd(ifindex_list[i], -1, flags);
  38. exit(0);
  39. }
  40. static void close_and_exit(int sig)
  41. {
  42. int i = 0;
  43. close(sock);
  44. close(sock_arp);
  45. for (i = 0; i < total_ifindex; i++)
  46. set_link_xdp_fd(ifindex_list[i], -1, flags);
  47. exit(0);
  48. }
  49. /* Get the mac address of the interface given interface name */
  50. static __be64 getmac(char *iface)
  51. {
  52. struct ifreq ifr;
  53. __be64 mac = 0;
  54. int fd, i;
  55. fd = socket(AF_INET, SOCK_DGRAM, 0);
  56. ifr.ifr_addr.sa_family = AF_INET;
  57. strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);
  58. if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
  59. printf("ioctl failed leaving....\n");
  60. return -1;
  61. }
  62. for (i = 0; i < 6 ; i++)
  63. *((__u8 *)&mac + i) = (__u8)ifr.ifr_hwaddr.sa_data[i];
  64. close(fd);
  65. return mac;
  66. }
  67. static int recv_msg(struct sockaddr_nl sock_addr, int sock)
  68. {
  69. struct nlmsghdr *nh;
  70. int len, nll = 0;
  71. char *buf_ptr;
  72. buf_ptr = buf;
  73. while (1) {
  74. len = recv(sock, buf_ptr, sizeof(buf) - nll, 0);
  75. if (len < 0)
  76. return len;
  77. nh = (struct nlmsghdr *)buf_ptr;
  78. if (nh->nlmsg_type == NLMSG_DONE)
  79. break;
  80. buf_ptr += len;
  81. nll += len;
  82. if ((sock_addr.nl_groups & RTMGRP_NEIGH) == RTMGRP_NEIGH)
  83. break;
  84. if ((sock_addr.nl_groups & RTMGRP_IPV4_ROUTE) == RTMGRP_IPV4_ROUTE)
  85. break;
  86. }
  87. return nll;
  88. }
  89. /* Function to parse the route entry returned by netlink
  90. * Updates the route entry related map entries
  91. */
  92. static void read_route(struct nlmsghdr *nh, int nll)
  93. {
  94. char dsts[24], gws[24], ifs[16], dsts_len[24], metrics[24];
  95. struct bpf_lpm_trie_key *prefix_key;
  96. struct rtattr *rt_attr;
  97. struct rtmsg *rt_msg;
  98. int rtm_family;
  99. int rtl;
  100. int i;
  101. struct route_table {
  102. int dst_len, iface, metric;
  103. char *iface_name;
  104. __be32 dst, gw;
  105. __be64 mac;
  106. } route;
  107. struct arp_table {
  108. __be64 mac;
  109. __be32 dst;
  110. };
  111. struct direct_map {
  112. struct arp_table arp;
  113. int ifindex;
  114. __be64 mac;
  115. } direct_entry;
  116. if (nh->nlmsg_type == RTM_DELROUTE)
  117. printf("DELETING Route entry\n");
  118. else if (nh->nlmsg_type == RTM_GETROUTE)
  119. printf("READING Route entry\n");
  120. else if (nh->nlmsg_type == RTM_NEWROUTE)
  121. printf("NEW Route entry\n");
  122. else
  123. printf("%d\n", nh->nlmsg_type);
  124. memset(&route, 0, sizeof(route));
  125. printf("Destination\t\tGateway\t\tGenmask\t\tMetric\t\tIface\n");
  126. for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
  127. rt_msg = (struct rtmsg *)NLMSG_DATA(nh);
  128. rtm_family = rt_msg->rtm_family;
  129. if (rtm_family == AF_INET)
  130. if (rt_msg->rtm_table != RT_TABLE_MAIN)
  131. continue;
  132. rt_attr = (struct rtattr *)RTM_RTA(rt_msg);
  133. rtl = RTM_PAYLOAD(nh);
  134. for (; RTA_OK(rt_attr, rtl); rt_attr = RTA_NEXT(rt_attr, rtl)) {
  135. switch (rt_attr->rta_type) {
  136. case NDA_DST:
  137. sprintf(dsts, "%u",
  138. (*((__be32 *)RTA_DATA(rt_attr))));
  139. break;
  140. case RTA_GATEWAY:
  141. sprintf(gws, "%u",
  142. *((__be32 *)RTA_DATA(rt_attr)));
  143. break;
  144. case RTA_OIF:
  145. sprintf(ifs, "%u",
  146. *((int *)RTA_DATA(rt_attr)));
  147. break;
  148. case RTA_METRICS:
  149. sprintf(metrics, "%u",
  150. *((int *)RTA_DATA(rt_attr)));
  151. default:
  152. break;
  153. }
  154. }
  155. sprintf(dsts_len, "%d", rt_msg->rtm_dst_len);
  156. route.dst = atoi(dsts);
  157. route.dst_len = atoi(dsts_len);
  158. route.gw = atoi(gws);
  159. route.iface = atoi(ifs);
  160. route.metric = atoi(metrics);
  161. route.iface_name = alloca(sizeof(char *) * IFNAMSIZ);
  162. route.iface_name = if_indextoname(route.iface, route.iface_name);
  163. route.mac = getmac(route.iface_name);
  164. if (route.mac == -1) {
  165. int i = 0;
  166. for (i = 0; i < total_ifindex; i++)
  167. set_link_xdp_fd(ifindex_list[i], -1, flags);
  168. exit(0);
  169. }
  170. assert(bpf_map_update_elem(map_fd[4], &route.iface, &route.iface, 0) == 0);
  171. if (rtm_family == AF_INET) {
  172. struct trie_value {
  173. __u8 prefix[4];
  174. __be64 value;
  175. int ifindex;
  176. int metric;
  177. __be32 gw;
  178. } *prefix_value;
  179. prefix_key = alloca(sizeof(*prefix_key) + 3);
  180. prefix_value = alloca(sizeof(*prefix_value));
  181. prefix_key->prefixlen = 32;
  182. prefix_key->prefixlen = route.dst_len;
  183. direct_entry.mac = route.mac & 0xffffffffffff;
  184. direct_entry.ifindex = route.iface;
  185. direct_entry.arp.mac = 0;
  186. direct_entry.arp.dst = 0;
  187. if (route.dst_len == 32) {
  188. if (nh->nlmsg_type == RTM_DELROUTE)
  189. assert(bpf_map_delete_elem(map_fd[3], &route.dst) == 0);
  190. else
  191. if (bpf_map_lookup_elem(map_fd[2], &route.dst, &direct_entry.arp.mac) == 0)
  192. direct_entry.arp.dst = route.dst;
  193. assert(bpf_map_update_elem(map_fd[3], &route.dst, &direct_entry, 0) == 0);
  194. }
  195. for (i = 0; i < 4; i++)
  196. prefix_key->data[i] = (route.dst >> i * 8) & 0xff;
  197. printf("%3d.%d.%d.%d\t\t%3x\t\t%d\t\t%d\t\t%s\n",
  198. (int)prefix_key->data[0],
  199. (int)prefix_key->data[1],
  200. (int)prefix_key->data[2],
  201. (int)prefix_key->data[3],
  202. route.gw, route.dst_len,
  203. route.metric,
  204. route.iface_name);
  205. if (bpf_map_lookup_elem(map_fd[0], prefix_key,
  206. prefix_value) < 0) {
  207. for (i = 0; i < 4; i++)
  208. prefix_value->prefix[i] = prefix_key->data[i];
  209. prefix_value->value = route.mac & 0xffffffffffff;
  210. prefix_value->ifindex = route.iface;
  211. prefix_value->gw = route.gw;
  212. prefix_value->metric = route.metric;
  213. assert(bpf_map_update_elem(map_fd[0],
  214. prefix_key,
  215. prefix_value, 0
  216. ) == 0);
  217. } else {
  218. if (nh->nlmsg_type == RTM_DELROUTE) {
  219. printf("deleting entry\n");
  220. printf("prefix key=%d.%d.%d.%d/%d",
  221. prefix_key->data[0],
  222. prefix_key->data[1],
  223. prefix_key->data[2],
  224. prefix_key->data[3],
  225. prefix_key->prefixlen);
  226. assert(bpf_map_delete_elem(map_fd[0],
  227. prefix_key
  228. ) == 0);
  229. /* Rereading the route table to check if
  230. * there is an entry with the same
  231. * prefix but a different metric as the
  232. * deleted enty.
  233. */
  234. get_route_table(AF_INET);
  235. } else if (prefix_key->data[0] ==
  236. prefix_value->prefix[0] &&
  237. prefix_key->data[1] ==
  238. prefix_value->prefix[1] &&
  239. prefix_key->data[2] ==
  240. prefix_value->prefix[2] &&
  241. prefix_key->data[3] ==
  242. prefix_value->prefix[3] &&
  243. route.metric >= prefix_value->metric) {
  244. continue;
  245. } else {
  246. for (i = 0; i < 4; i++)
  247. prefix_value->prefix[i] =
  248. prefix_key->data[i];
  249. prefix_value->value =
  250. route.mac & 0xffffffffffff;
  251. prefix_value->ifindex = route.iface;
  252. prefix_value->gw = route.gw;
  253. prefix_value->metric = route.metric;
  254. assert(bpf_map_update_elem(
  255. map_fd[0],
  256. prefix_key,
  257. prefix_value,
  258. 0) == 0);
  259. }
  260. }
  261. }
  262. memset(&route, 0, sizeof(route));
  263. memset(dsts, 0, sizeof(dsts));
  264. memset(dsts_len, 0, sizeof(dsts_len));
  265. memset(gws, 0, sizeof(gws));
  266. memset(ifs, 0, sizeof(ifs));
  267. memset(&route, 0, sizeof(route));
  268. }
  269. }
  270. /* Function to read the existing route table when the process is launched*/
  271. static int get_route_table(int rtm_family)
  272. {
  273. struct sockaddr_nl sa;
  274. struct nlmsghdr *nh;
  275. int sock, seq = 0;
  276. struct msghdr msg;
  277. struct iovec iov;
  278. int ret = 0;
  279. int nll;
  280. struct {
  281. struct nlmsghdr nl;
  282. struct rtmsg rt;
  283. char buf[8192];
  284. } req;
  285. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  286. if (sock < 0) {
  287. printf("open netlink socket: %s\n", strerror(errno));
  288. return -1;
  289. }
  290. memset(&sa, 0, sizeof(sa));
  291. sa.nl_family = AF_NETLINK;
  292. if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  293. printf("bind to netlink: %s\n", strerror(errno));
  294. ret = -1;
  295. goto cleanup;
  296. }
  297. memset(&req, 0, sizeof(req));
  298. req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  299. req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
  300. req.nl.nlmsg_type = RTM_GETROUTE;
  301. req.rt.rtm_family = rtm_family;
  302. req.rt.rtm_table = RT_TABLE_MAIN;
  303. req.nl.nlmsg_pid = 0;
  304. req.nl.nlmsg_seq = ++seq;
  305. memset(&msg, 0, sizeof(msg));
  306. iov.iov_base = (void *)&req.nl;
  307. iov.iov_len = req.nl.nlmsg_len;
  308. msg.msg_iov = &iov;
  309. msg.msg_iovlen = 1;
  310. ret = sendmsg(sock, &msg, 0);
  311. if (ret < 0) {
  312. printf("send to netlink: %s\n", strerror(errno));
  313. ret = -1;
  314. goto cleanup;
  315. }
  316. memset(buf, 0, sizeof(buf));
  317. nll = recv_msg(sa, sock);
  318. if (nll < 0) {
  319. printf("recv from netlink: %s\n", strerror(nll));
  320. ret = -1;
  321. goto cleanup;
  322. }
  323. nh = (struct nlmsghdr *)buf;
  324. read_route(nh, nll);
  325. cleanup:
  326. close(sock);
  327. return ret;
  328. }
  329. /* Function to parse the arp entry returned by netlink
  330. * Updates the arp entry related map entries
  331. */
  332. static void read_arp(struct nlmsghdr *nh, int nll)
  333. {
  334. struct rtattr *rt_attr;
  335. char dsts[24], mac[24];
  336. struct ndmsg *rt_msg;
  337. int rtl, ndm_family;
  338. struct arp_table {
  339. __be64 mac;
  340. __be32 dst;
  341. } arp_entry;
  342. struct direct_map {
  343. struct arp_table arp;
  344. int ifindex;
  345. __be64 mac;
  346. } direct_entry;
  347. if (nh->nlmsg_type == RTM_GETNEIGH)
  348. printf("READING arp entry\n");
  349. printf("Address\tHwAddress\n");
  350. for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
  351. rt_msg = (struct ndmsg *)NLMSG_DATA(nh);
  352. rt_attr = (struct rtattr *)RTM_RTA(rt_msg);
  353. ndm_family = rt_msg->ndm_family;
  354. rtl = RTM_PAYLOAD(nh);
  355. for (; RTA_OK(rt_attr, rtl); rt_attr = RTA_NEXT(rt_attr, rtl)) {
  356. switch (rt_attr->rta_type) {
  357. case NDA_DST:
  358. sprintf(dsts, "%u",
  359. *((__be32 *)RTA_DATA(rt_attr)));
  360. break;
  361. case NDA_LLADDR:
  362. sprintf(mac, "%lld",
  363. *((__be64 *)RTA_DATA(rt_attr)));
  364. break;
  365. default:
  366. break;
  367. }
  368. }
  369. arp_entry.dst = atoi(dsts);
  370. arp_entry.mac = atol(mac);
  371. printf("%x\t\t%llx\n", arp_entry.dst, arp_entry.mac);
  372. if (ndm_family == AF_INET) {
  373. if (bpf_map_lookup_elem(map_fd[3], &arp_entry.dst,
  374. &direct_entry) == 0) {
  375. if (nh->nlmsg_type == RTM_DELNEIGH) {
  376. direct_entry.arp.dst = 0;
  377. direct_entry.arp.mac = 0;
  378. } else if (nh->nlmsg_type == RTM_NEWNEIGH) {
  379. direct_entry.arp.dst = arp_entry.dst;
  380. direct_entry.arp.mac = arp_entry.mac;
  381. }
  382. assert(bpf_map_update_elem(map_fd[3],
  383. &arp_entry.dst,
  384. &direct_entry, 0
  385. ) == 0);
  386. memset(&direct_entry, 0, sizeof(direct_entry));
  387. }
  388. if (nh->nlmsg_type == RTM_DELNEIGH) {
  389. assert(bpf_map_delete_elem(map_fd[2], &arp_entry.dst) == 0);
  390. } else if (nh->nlmsg_type == RTM_NEWNEIGH) {
  391. assert(bpf_map_update_elem(map_fd[2],
  392. &arp_entry.dst,
  393. &arp_entry.mac, 0
  394. ) == 0);
  395. }
  396. }
  397. memset(&arp_entry, 0, sizeof(arp_entry));
  398. memset(dsts, 0, sizeof(dsts));
  399. }
  400. }
  401. /* Function to read the existing arp table when the process is launched*/
  402. static int get_arp_table(int rtm_family)
  403. {
  404. struct sockaddr_nl sa;
  405. struct nlmsghdr *nh;
  406. int sock, seq = 0;
  407. struct msghdr msg;
  408. struct iovec iov;
  409. int ret = 0;
  410. int nll;
  411. struct {
  412. struct nlmsghdr nl;
  413. struct ndmsg rt;
  414. char buf[8192];
  415. } req;
  416. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  417. if (sock < 0) {
  418. printf("open netlink socket: %s\n", strerror(errno));
  419. return -1;
  420. }
  421. memset(&sa, 0, sizeof(sa));
  422. sa.nl_family = AF_NETLINK;
  423. if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  424. printf("bind to netlink: %s\n", strerror(errno));
  425. ret = -1;
  426. goto cleanup;
  427. }
  428. memset(&req, 0, sizeof(req));
  429. req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  430. req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
  431. req.nl.nlmsg_type = RTM_GETNEIGH;
  432. req.rt.ndm_state = NUD_REACHABLE;
  433. req.rt.ndm_family = rtm_family;
  434. req.nl.nlmsg_pid = 0;
  435. req.nl.nlmsg_seq = ++seq;
  436. memset(&msg, 0, sizeof(msg));
  437. iov.iov_base = (void *)&req.nl;
  438. iov.iov_len = req.nl.nlmsg_len;
  439. msg.msg_iov = &iov;
  440. msg.msg_iovlen = 1;
  441. ret = sendmsg(sock, &msg, 0);
  442. if (ret < 0) {
  443. printf("send to netlink: %s\n", strerror(errno));
  444. ret = -1;
  445. goto cleanup;
  446. }
  447. memset(buf, 0, sizeof(buf));
  448. nll = recv_msg(sa, sock);
  449. if (nll < 0) {
  450. printf("recv from netlink: %s\n", strerror(nll));
  451. ret = -1;
  452. goto cleanup;
  453. }
  454. nh = (struct nlmsghdr *)buf;
  455. read_arp(nh, nll);
  456. cleanup:
  457. close(sock);
  458. return ret;
  459. }
  460. /* Function to keep track and update changes in route and arp table
  461. * Give regular statistics of packets forwarded
  462. */
  463. static int monitor_route(void)
  464. {
  465. unsigned int nr_cpus = bpf_num_possible_cpus();
  466. const unsigned int nr_keys = 256;
  467. struct pollfd fds_route, fds_arp;
  468. __u64 prev[nr_keys][nr_cpus];
  469. struct sockaddr_nl la, lr;
  470. __u64 values[nr_cpus];
  471. struct nlmsghdr *nh;
  472. int nll, ret = 0;
  473. int interval = 5;
  474. __u32 key;
  475. int i;
  476. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  477. if (sock < 0) {
  478. printf("open netlink socket: %s\n", strerror(errno));
  479. return -1;
  480. }
  481. fcntl(sock, F_SETFL, O_NONBLOCK);
  482. memset(&lr, 0, sizeof(lr));
  483. lr.nl_family = AF_NETLINK;
  484. lr.nl_groups = RTMGRP_IPV6_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_NOTIFY;
  485. if (bind(sock, (struct sockaddr *)&lr, sizeof(lr)) < 0) {
  486. printf("bind to netlink: %s\n", strerror(errno));
  487. ret = -1;
  488. goto cleanup;
  489. }
  490. fds_route.fd = sock;
  491. fds_route.events = POLL_IN;
  492. sock_arp = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  493. if (sock_arp < 0) {
  494. printf("open netlink socket: %s\n", strerror(errno));
  495. return -1;
  496. }
  497. fcntl(sock_arp, F_SETFL, O_NONBLOCK);
  498. memset(&la, 0, sizeof(la));
  499. la.nl_family = AF_NETLINK;
  500. la.nl_groups = RTMGRP_NEIGH | RTMGRP_NOTIFY;
  501. if (bind(sock_arp, (struct sockaddr *)&la, sizeof(la)) < 0) {
  502. printf("bind to netlink: %s\n", strerror(errno));
  503. ret = -1;
  504. goto cleanup;
  505. }
  506. fds_arp.fd = sock_arp;
  507. fds_arp.events = POLL_IN;
  508. memset(prev, 0, sizeof(prev));
  509. do {
  510. signal(SIGINT, close_and_exit);
  511. signal(SIGTERM, close_and_exit);
  512. sleep(interval);
  513. for (key = 0; key < nr_keys; key++) {
  514. __u64 sum = 0;
  515. assert(bpf_map_lookup_elem(map_fd[1], &key, values) == 0);
  516. for (i = 0; i < nr_cpus; i++)
  517. sum += (values[i] - prev[key][i]);
  518. if (sum)
  519. printf("proto %u: %10llu pkt/s\n",
  520. key, sum / interval);
  521. memcpy(prev[key], values, sizeof(values));
  522. }
  523. memset(buf, 0, sizeof(buf));
  524. if (poll(&fds_route, 1, 3) == POLL_IN) {
  525. nll = recv_msg(lr, sock);
  526. if (nll < 0) {
  527. printf("recv from netlink: %s\n", strerror(nll));
  528. ret = -1;
  529. goto cleanup;
  530. }
  531. nh = (struct nlmsghdr *)buf;
  532. printf("Routing table updated.\n");
  533. read_route(nh, nll);
  534. }
  535. memset(buf, 0, sizeof(buf));
  536. if (poll(&fds_arp, 1, 3) == POLL_IN) {
  537. nll = recv_msg(la, sock_arp);
  538. if (nll < 0) {
  539. printf("recv from netlink: %s\n", strerror(nll));
  540. ret = -1;
  541. goto cleanup;
  542. }
  543. nh = (struct nlmsghdr *)buf;
  544. read_arp(nh, nll);
  545. }
  546. } while (1);
  547. cleanup:
  548. close(sock);
  549. return ret;
  550. }
  551. int main(int ac, char **argv)
  552. {
  553. char filename[256];
  554. char **ifname_list;
  555. int i = 1;
  556. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  557. if (ac < 2) {
  558. printf("usage: %s [-S] Interface name list\n", argv[0]);
  559. return 1;
  560. }
  561. if (!strcmp(argv[1], "-S")) {
  562. flags = XDP_FLAGS_SKB_MODE;
  563. total_ifindex = ac - 2;
  564. ifname_list = (argv + 2);
  565. } else {
  566. flags = 0;
  567. total_ifindex = ac - 1;
  568. ifname_list = (argv + 1);
  569. }
  570. if (load_bpf_file(filename)) {
  571. printf("%s", bpf_log_buf);
  572. return 1;
  573. }
  574. printf("\n**************loading bpf file*********************\n\n\n");
  575. if (!prog_fd[0]) {
  576. printf("load_bpf_file: %s\n", strerror(errno));
  577. return 1;
  578. }
  579. ifindex_list = (int *)malloc(total_ifindex * sizeof(int *));
  580. for (i = 0; i < total_ifindex; i++) {
  581. ifindex_list[i] = if_nametoindex(ifname_list[i]);
  582. if (!ifindex_list[i]) {
  583. printf("Couldn't translate interface name: %s",
  584. strerror(errno));
  585. return 1;
  586. }
  587. }
  588. for (i = 0; i < total_ifindex; i++) {
  589. if (set_link_xdp_fd(ifindex_list[i], prog_fd[0], flags) < 0) {
  590. printf("link set xdp fd failed\n");
  591. int recovery_index = i;
  592. for (i = 0; i < recovery_index; i++)
  593. set_link_xdp_fd(ifindex_list[i], -1, flags);
  594. return 1;
  595. }
  596. printf("Attached to %d\n", ifindex_list[i]);
  597. }
  598. signal(SIGINT, int_exit);
  599. signal(SIGTERM, int_exit);
  600. printf("*******************ROUTE TABLE*************************\n\n\n");
  601. get_route_table(AF_INET);
  602. printf("*******************ARP TABLE***************************\n\n\n");
  603. get_arp_table(AF_INET);
  604. if (monitor_route() < 0) {
  605. printf("Error in receiving route update");
  606. return 1;
  607. }
  608. return 0;
  609. }