xdp_adjust_tail_user.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* SPDX-License-Identifier: GPL-2.0
  2. * Copyright (c) 2018 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. */
  8. #include <linux/bpf.h>
  9. #include <linux/if_link.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 <net/if.h>
  17. #include <sys/resource.h>
  18. #include <arpa/inet.h>
  19. #include <netinet/ether.h>
  20. #include <unistd.h>
  21. #include <time.h>
  22. #include <bpf/bpf.h>
  23. #include <bpf/libbpf.h>
  24. #define STATS_INTERVAL_S 2U
  25. #define MAX_PCKT_SIZE 600
  26. static int ifindex = -1;
  27. static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
  28. static __u32 prog_id;
  29. static void int_exit(int sig)
  30. {
  31. __u32 curr_prog_id = 0;
  32. if (ifindex > -1) {
  33. if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
  34. printf("bpf_get_link_xdp_id failed\n");
  35. exit(1);
  36. }
  37. if (prog_id == curr_prog_id)
  38. bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
  39. else if (!curr_prog_id)
  40. printf("couldn't find a prog id on a given iface\n");
  41. else
  42. printf("program on interface changed, not removing\n");
  43. }
  44. exit(0);
  45. }
  46. /* simple "icmp packet too big sent" counter
  47. */
  48. static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
  49. {
  50. time_t started_at = time(NULL);
  51. __u64 value = 0;
  52. int key = 0;
  53. while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
  54. sleep(STATS_INTERVAL_S);
  55. assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
  56. printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
  57. }
  58. }
  59. static void usage(const char *cmd)
  60. {
  61. printf("Start a XDP prog which send ICMP \"packet too big\" \n"
  62. "messages if ingress packet is bigger then MAX_SIZE bytes\n");
  63. printf("Usage: %s [...]\n", cmd);
  64. printf(" -i <ifname|ifindex> Interface\n");
  65. printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
  66. printf(" -P <MAX_PCKT_SIZE> Default: %u\n", MAX_PCKT_SIZE);
  67. printf(" -S use skb-mode\n");
  68. printf(" -N enforce native mode\n");
  69. printf(" -F force loading prog\n");
  70. printf(" -h Display this help\n");
  71. }
  72. int main(int argc, char **argv)
  73. {
  74. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  75. struct bpf_prog_load_attr prog_load_attr = {
  76. .prog_type = BPF_PROG_TYPE_XDP,
  77. };
  78. unsigned char opt_flags[256] = {};
  79. const char *optstr = "i:T:P:SNFh";
  80. struct bpf_prog_info info = {};
  81. __u32 info_len = sizeof(info);
  82. unsigned int kill_after_s = 0;
  83. int i, prog_fd, map_fd, opt;
  84. struct bpf_object *obj;
  85. __u32 max_pckt_size = 0;
  86. __u32 key = 0;
  87. char filename[256];
  88. int err;
  89. for (i = 0; i < strlen(optstr); i++)
  90. if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
  91. opt_flags[(unsigned char)optstr[i]] = 1;
  92. while ((opt = getopt(argc, argv, optstr)) != -1) {
  93. switch (opt) {
  94. case 'i':
  95. ifindex = if_nametoindex(optarg);
  96. if (!ifindex)
  97. ifindex = atoi(optarg);
  98. break;
  99. case 'T':
  100. kill_after_s = atoi(optarg);
  101. break;
  102. case 'P':
  103. max_pckt_size = atoi(optarg);
  104. break;
  105. case 'S':
  106. xdp_flags |= XDP_FLAGS_SKB_MODE;
  107. break;
  108. case 'N':
  109. /* default, set below */
  110. break;
  111. case 'F':
  112. xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
  113. break;
  114. default:
  115. usage(argv[0]);
  116. return 1;
  117. }
  118. opt_flags[opt] = 0;
  119. }
  120. if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
  121. xdp_flags |= XDP_FLAGS_DRV_MODE;
  122. for (i = 0; i < strlen(optstr); i++) {
  123. if (opt_flags[(unsigned int)optstr[i]]) {
  124. fprintf(stderr, "Missing argument -%c\n", optstr[i]);
  125. usage(argv[0]);
  126. return 1;
  127. }
  128. }
  129. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  130. perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
  131. return 1;
  132. }
  133. if (!ifindex) {
  134. fprintf(stderr, "Invalid ifname\n");
  135. return 1;
  136. }
  137. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  138. prog_load_attr.file = filename;
  139. if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
  140. return 1;
  141. /* static global var 'max_pcktsz' is accessible from .data section */
  142. if (max_pckt_size) {
  143. map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data");
  144. if (map_fd < 0) {
  145. printf("finding a max_pcktsz map in obj file failed\n");
  146. return 1;
  147. }
  148. bpf_map_update_elem(map_fd, &key, &max_pckt_size, BPF_ANY);
  149. }
  150. /* fetch icmpcnt map */
  151. map_fd = bpf_object__find_map_fd_by_name(obj, "icmpcnt");
  152. if (map_fd < 0) {
  153. printf("finding a icmpcnt map in obj file failed\n");
  154. return 1;
  155. }
  156. signal(SIGINT, int_exit);
  157. signal(SIGTERM, int_exit);
  158. if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
  159. printf("link set xdp fd failed\n");
  160. return 1;
  161. }
  162. err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
  163. if (err) {
  164. printf("can't get prog info - %s\n", strerror(errno));
  165. return 1;
  166. }
  167. prog_id = info.id;
  168. poll_stats(map_fd, kill_after_s);
  169. int_exit(0);
  170. return 0;
  171. }