xdp_fwd_user.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2017-18 David Ahern <[email protected]>
  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. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/bpf.h>
  14. #include <linux/if_link.h>
  15. #include <linux/limits.h>
  16. #include <net/if.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdbool.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <libgen.h>
  25. #include <bpf/libbpf.h>
  26. #include <bpf/bpf.h>
  27. static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
  28. static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
  29. {
  30. int err;
  31. err = bpf_xdp_attach(idx, prog_fd, xdp_flags, NULL);
  32. if (err < 0) {
  33. printf("ERROR: failed to attach program to %s\n", name);
  34. return err;
  35. }
  36. /* Adding ifindex as a possible egress TX port */
  37. err = bpf_map_update_elem(map_fd, &idx, &idx, 0);
  38. if (err)
  39. printf("ERROR: failed using device %s as TX-port\n", name);
  40. return err;
  41. }
  42. static int do_detach(int ifindex, const char *ifname, const char *app_name)
  43. {
  44. LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
  45. struct bpf_prog_info prog_info = {};
  46. char prog_name[BPF_OBJ_NAME_LEN];
  47. __u32 info_len, curr_prog_id;
  48. int prog_fd;
  49. int err = 1;
  50. if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
  51. printf("ERROR: bpf_xdp_query_id failed (%s)\n",
  52. strerror(errno));
  53. return err;
  54. }
  55. if (!curr_prog_id) {
  56. printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
  57. xdp_flags, ifname);
  58. return err;
  59. }
  60. info_len = sizeof(prog_info);
  61. prog_fd = bpf_prog_get_fd_by_id(curr_prog_id);
  62. if (prog_fd < 0) {
  63. printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\n",
  64. strerror(errno));
  65. return prog_fd;
  66. }
  67. err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
  68. if (err) {
  69. printf("ERROR: bpf_obj_get_info_by_fd failed (%s)\n",
  70. strerror(errno));
  71. goto close_out;
  72. }
  73. snprintf(prog_name, sizeof(prog_name), "%s_prog", app_name);
  74. prog_name[BPF_OBJ_NAME_LEN - 1] = '\0';
  75. if (strcmp(prog_info.name, prog_name)) {
  76. printf("ERROR: %s isn't attached to %s\n", app_name, ifname);
  77. err = 1;
  78. goto close_out;
  79. }
  80. opts.old_prog_fd = prog_fd;
  81. err = bpf_xdp_detach(ifindex, xdp_flags, &opts);
  82. if (err < 0)
  83. printf("ERROR: failed to detach program from %s (%s)\n",
  84. ifname, strerror(errno));
  85. /* TODO: Remember to cleanup map, when adding use of shared map
  86. * bpf_map_delete_elem((map_fd, &idx);
  87. */
  88. close_out:
  89. close(prog_fd);
  90. return err;
  91. }
  92. static void usage(const char *prog)
  93. {
  94. fprintf(stderr,
  95. "usage: %s [OPTS] interface-list\n"
  96. "\nOPTS:\n"
  97. " -d detach program\n"
  98. " -S use skb-mode\n"
  99. " -F force loading prog\n"
  100. " -D direct table lookups (skip fib rules)\n",
  101. prog);
  102. }
  103. int main(int argc, char **argv)
  104. {
  105. const char *prog_name = "xdp_fwd";
  106. struct bpf_program *prog = NULL;
  107. struct bpf_program *pos;
  108. const char *sec_name;
  109. int prog_fd = -1, map_fd = -1;
  110. char filename[PATH_MAX];
  111. struct bpf_object *obj;
  112. int opt, i, idx, err;
  113. int attach = 1;
  114. int ret = 0;
  115. while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
  116. switch (opt) {
  117. case 'd':
  118. attach = 0;
  119. break;
  120. case 'S':
  121. xdp_flags |= XDP_FLAGS_SKB_MODE;
  122. break;
  123. case 'F':
  124. xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
  125. break;
  126. case 'D':
  127. prog_name = "xdp_fwd_direct";
  128. break;
  129. default:
  130. usage(basename(argv[0]));
  131. return 1;
  132. }
  133. }
  134. if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
  135. xdp_flags |= XDP_FLAGS_DRV_MODE;
  136. if (optind == argc) {
  137. usage(basename(argv[0]));
  138. return 1;
  139. }
  140. if (attach) {
  141. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  142. if (access(filename, O_RDONLY) < 0) {
  143. printf("error accessing file %s: %s\n",
  144. filename, strerror(errno));
  145. return 1;
  146. }
  147. obj = bpf_object__open_file(filename, NULL);
  148. if (libbpf_get_error(obj))
  149. return 1;
  150. prog = bpf_object__next_program(obj, NULL);
  151. bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
  152. err = bpf_object__load(obj);
  153. if (err) {
  154. printf("Does kernel support devmap lookup?\n");
  155. /* If not, the error message will be:
  156. * "cannot pass map_type 14 into func bpf_map_lookup_elem#1"
  157. */
  158. return 1;
  159. }
  160. bpf_object__for_each_program(pos, obj) {
  161. sec_name = bpf_program__section_name(pos);
  162. if (sec_name && !strcmp(sec_name, prog_name)) {
  163. prog = pos;
  164. break;
  165. }
  166. }
  167. prog_fd = bpf_program__fd(prog);
  168. if (prog_fd < 0) {
  169. printf("program not found: %s\n", strerror(prog_fd));
  170. return 1;
  171. }
  172. map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
  173. "xdp_tx_ports"));
  174. if (map_fd < 0) {
  175. printf("map not found: %s\n", strerror(map_fd));
  176. return 1;
  177. }
  178. }
  179. for (i = optind; i < argc; ++i) {
  180. idx = if_nametoindex(argv[i]);
  181. if (!idx)
  182. idx = strtoul(argv[i], NULL, 0);
  183. if (!idx) {
  184. fprintf(stderr, "Invalid arg\n");
  185. return 1;
  186. }
  187. if (!attach) {
  188. err = do_detach(idx, argv[i], prog_name);
  189. if (err)
  190. ret = err;
  191. } else {
  192. err = do_attach(idx, prog_fd, map_fd, argv[i]);
  193. if (err)
  194. ret = err;
  195. }
  196. }
  197. return ret;
  198. }