xdp_adjust_tail_user.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <sys/resource.h>
  17. #include <arpa/inet.h>
  18. #include <netinet/ether.h>
  19. #include <unistd.h>
  20. #include <time.h>
  21. #include "bpf/bpf.h"
  22. #include "bpf/libbpf.h"
  23. #define STATS_INTERVAL_S 2U
  24. static int ifindex = -1;
  25. static __u32 xdp_flags;
  26. static void int_exit(int sig)
  27. {
  28. if (ifindex > -1)
  29. bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
  30. exit(0);
  31. }
  32. /* simple "icmp packet too big sent" counter
  33. */
  34. static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
  35. {
  36. time_t started_at = time(NULL);
  37. __u64 value = 0;
  38. int key = 0;
  39. while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
  40. sleep(STATS_INTERVAL_S);
  41. assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
  42. printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
  43. }
  44. }
  45. static void usage(const char *cmd)
  46. {
  47. printf("Start a XDP prog which send ICMP \"packet too big\" \n"
  48. "messages if ingress packet is bigger then MAX_SIZE bytes\n");
  49. printf("Usage: %s [...]\n", cmd);
  50. printf(" -i <ifindex> Interface Index\n");
  51. printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
  52. printf(" -S use skb-mode\n");
  53. printf(" -N enforce native mode\n");
  54. printf(" -h Display this help\n");
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  59. struct bpf_prog_load_attr prog_load_attr = {
  60. .prog_type = BPF_PROG_TYPE_XDP,
  61. };
  62. unsigned char opt_flags[256] = {};
  63. unsigned int kill_after_s = 0;
  64. const char *optstr = "i:T:SNh";
  65. int i, prog_fd, map_fd, opt;
  66. struct bpf_object *obj;
  67. struct bpf_map *map;
  68. char filename[256];
  69. for (i = 0; i < strlen(optstr); i++)
  70. if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
  71. opt_flags[(unsigned char)optstr[i]] = 1;
  72. while ((opt = getopt(argc, argv, optstr)) != -1) {
  73. switch (opt) {
  74. case 'i':
  75. ifindex = atoi(optarg);
  76. break;
  77. case 'T':
  78. kill_after_s = atoi(optarg);
  79. break;
  80. case 'S':
  81. xdp_flags |= XDP_FLAGS_SKB_MODE;
  82. break;
  83. case 'N':
  84. xdp_flags |= XDP_FLAGS_DRV_MODE;
  85. break;
  86. default:
  87. usage(argv[0]);
  88. return 1;
  89. }
  90. opt_flags[opt] = 0;
  91. }
  92. for (i = 0; i < strlen(optstr); i++) {
  93. if (opt_flags[(unsigned int)optstr[i]]) {
  94. fprintf(stderr, "Missing argument -%c\n", optstr[i]);
  95. usage(argv[0]);
  96. return 1;
  97. }
  98. }
  99. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  100. perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
  101. return 1;
  102. }
  103. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  104. prog_load_attr.file = filename;
  105. if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
  106. return 1;
  107. map = bpf_map__next(NULL, obj);
  108. if (!map) {
  109. printf("finding a map in obj file failed\n");
  110. return 1;
  111. }
  112. map_fd = bpf_map__fd(map);
  113. if (!prog_fd) {
  114. printf("load_bpf_file: %s\n", strerror(errno));
  115. return 1;
  116. }
  117. signal(SIGINT, int_exit);
  118. signal(SIGTERM, int_exit);
  119. if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
  120. printf("link set xdp fd failed\n");
  121. return 1;
  122. }
  123. poll_stats(map_fd, kill_after_s);
  124. bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
  125. return 0;
  126. }