xdp_fwd_user.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 int do_attach(int idx, int fd, const char *name)
  28. {
  29. int err;
  30. err = bpf_set_link_xdp_fd(idx, fd, 0);
  31. if (err < 0)
  32. printf("ERROR: failed to attach program to %s\n", name);
  33. return err;
  34. }
  35. static int do_detach(int idx, const char *name)
  36. {
  37. int err;
  38. err = bpf_set_link_xdp_fd(idx, -1, 0);
  39. if (err < 0)
  40. printf("ERROR: failed to detach program from %s\n", name);
  41. return err;
  42. }
  43. static void usage(const char *prog)
  44. {
  45. fprintf(stderr,
  46. "usage: %s [OPTS] interface-list\n"
  47. "\nOPTS:\n"
  48. " -d detach program\n"
  49. " -D direct table lookups (skip fib rules)\n",
  50. prog);
  51. }
  52. int main(int argc, char **argv)
  53. {
  54. struct bpf_prog_load_attr prog_load_attr = {
  55. .prog_type = BPF_PROG_TYPE_XDP,
  56. };
  57. const char *prog_name = "xdp_fwd";
  58. struct bpf_program *prog;
  59. char filename[PATH_MAX];
  60. struct bpf_object *obj;
  61. int opt, i, idx, err;
  62. int prog_fd, map_fd;
  63. int attach = 1;
  64. int ret = 0;
  65. while ((opt = getopt(argc, argv, ":dD")) != -1) {
  66. switch (opt) {
  67. case 'd':
  68. attach = 0;
  69. break;
  70. case 'D':
  71. prog_name = "xdp_fwd_direct";
  72. break;
  73. default:
  74. usage(basename(argv[0]));
  75. return 1;
  76. }
  77. }
  78. if (optind == argc) {
  79. usage(basename(argv[0]));
  80. return 1;
  81. }
  82. if (attach) {
  83. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  84. prog_load_attr.file = filename;
  85. if (access(filename, O_RDONLY) < 0) {
  86. printf("error accessing file %s: %s\n",
  87. filename, strerror(errno));
  88. return 1;
  89. }
  90. if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
  91. return 1;
  92. prog = bpf_object__find_program_by_title(obj, prog_name);
  93. prog_fd = bpf_program__fd(prog);
  94. if (prog_fd < 0) {
  95. printf("program not found: %s\n", strerror(prog_fd));
  96. return 1;
  97. }
  98. map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
  99. "tx_port"));
  100. if (map_fd < 0) {
  101. printf("map not found: %s\n", strerror(map_fd));
  102. return 1;
  103. }
  104. }
  105. if (attach) {
  106. for (i = 1; i < 64; ++i)
  107. bpf_map_update_elem(map_fd, &i, &i, 0);
  108. }
  109. for (i = optind; i < argc; ++i) {
  110. idx = if_nametoindex(argv[i]);
  111. if (!idx)
  112. idx = strtoul(argv[i], NULL, 0);
  113. if (!idx) {
  114. fprintf(stderr, "Invalid arg\n");
  115. return 1;
  116. }
  117. if (!attach) {
  118. err = do_detach(idx, argv[i]);
  119. if (err)
  120. ret = err;
  121. } else {
  122. err = do_attach(idx, prog_fd, argv[i]);
  123. if (err)
  124. ret = err;
  125. }
  126. }
  127. return ret;
  128. }