xdp_fwd_user.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2017-18 David Ahern <dsahern@gmail.com>
  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_set_link_xdp_fd(idx, prog_fd, xdp_flags);
  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 idx, const char *name)
  43. {
  44. int err;
  45. err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
  46. if (err < 0)
  47. printf("ERROR: failed to detach program from %s\n", name);
  48. /* TODO: Remember to cleanup map, when adding use of shared map
  49. * bpf_map_delete_elem((map_fd, &idx);
  50. */
  51. return err;
  52. }
  53. static void usage(const char *prog)
  54. {
  55. fprintf(stderr,
  56. "usage: %s [OPTS] interface-list\n"
  57. "\nOPTS:\n"
  58. " -d detach program\n"
  59. " -D direct table lookups (skip fib rules)\n",
  60. prog);
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. struct bpf_prog_load_attr prog_load_attr = {
  65. .prog_type = BPF_PROG_TYPE_XDP,
  66. };
  67. const char *prog_name = "xdp_fwd";
  68. struct bpf_program *prog;
  69. int prog_fd, map_fd = -1;
  70. char filename[PATH_MAX];
  71. struct bpf_object *obj;
  72. int opt, i, idx, err;
  73. int attach = 1;
  74. int ret = 0;
  75. while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
  76. switch (opt) {
  77. case 'd':
  78. attach = 0;
  79. break;
  80. case 'S':
  81. xdp_flags |= XDP_FLAGS_SKB_MODE;
  82. break;
  83. case 'F':
  84. xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
  85. break;
  86. case 'D':
  87. prog_name = "xdp_fwd_direct";
  88. break;
  89. default:
  90. usage(basename(argv[0]));
  91. return 1;
  92. }
  93. }
  94. if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
  95. xdp_flags |= XDP_FLAGS_DRV_MODE;
  96. if (optind == argc) {
  97. usage(basename(argv[0]));
  98. return 1;
  99. }
  100. if (attach) {
  101. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  102. prog_load_attr.file = filename;
  103. if (access(filename, O_RDONLY) < 0) {
  104. printf("error accessing file %s: %s\n",
  105. filename, strerror(errno));
  106. return 1;
  107. }
  108. err = bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd);
  109. if (err) {
  110. printf("Does kernel support devmap lookup?\n");
  111. /* If not, the error message will be:
  112. * "cannot pass map_type 14 into func bpf_map_lookup_elem#1"
  113. */
  114. return 1;
  115. }
  116. prog = bpf_object__find_program_by_title(obj, prog_name);
  117. prog_fd = bpf_program__fd(prog);
  118. if (prog_fd < 0) {
  119. printf("program not found: %s\n", strerror(prog_fd));
  120. return 1;
  121. }
  122. map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
  123. "xdp_tx_ports"));
  124. if (map_fd < 0) {
  125. printf("map not found: %s\n", strerror(map_fd));
  126. return 1;
  127. }
  128. }
  129. for (i = optind; i < argc; ++i) {
  130. idx = if_nametoindex(argv[i]);
  131. if (!idx)
  132. idx = strtoul(argv[i], NULL, 0);
  133. if (!idx) {
  134. fprintf(stderr, "Invalid arg\n");
  135. return 1;
  136. }
  137. if (!attach) {
  138. err = do_detach(idx, argv[i]);
  139. if (err)
  140. ret = err;
  141. } else {
  142. err = do_attach(idx, prog_fd, map_fd, argv[i]);
  143. if (err)
  144. ret = err;
  145. }
  146. }
  147. return ret;
  148. }