tc_l2_redirect_user.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Facebook
  3. */
  4. #include <linux/unistd.h>
  5. #include <linux/bpf.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <bpf/bpf.h>
  12. static void usage(void)
  13. {
  14. printf("Usage: tc_l2_ipip_redirect [...]\n");
  15. printf(" -U <file> Update an already pinned BPF array\n");
  16. printf(" -i <ifindex> Interface index\n");
  17. printf(" -h Display this help\n");
  18. }
  19. int main(int argc, char **argv)
  20. {
  21. const char *pinned_file = NULL;
  22. int ifindex = -1;
  23. int array_key = 0;
  24. int array_fd = -1;
  25. int ret = -1;
  26. int opt;
  27. while ((opt = getopt(argc, argv, "F:U:i:")) != -1) {
  28. switch (opt) {
  29. /* General args */
  30. case 'U':
  31. pinned_file = optarg;
  32. break;
  33. case 'i':
  34. ifindex = atoi(optarg);
  35. break;
  36. default:
  37. usage();
  38. goto out;
  39. }
  40. }
  41. if (ifindex < 0 || !pinned_file) {
  42. usage();
  43. goto out;
  44. }
  45. array_fd = bpf_obj_get(pinned_file);
  46. if (array_fd < 0) {
  47. fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
  48. pinned_file, strerror(errno), errno);
  49. goto out;
  50. }
  51. /* bpf_tunnel_key.remote_ipv4 expects host byte orders */
  52. ret = bpf_map_update_elem(array_fd, &array_key, &ifindex, 0);
  53. if (ret) {
  54. perror("bpf_map_update_elem");
  55. goto out;
  56. }
  57. out:
  58. if (array_fd != -1)
  59. close(array_fd);
  60. return ret;
  61. }