ibumad_user.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /**
  3. * ibumad BPF sample user side
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * Copyright(c) 2018 Ira Weiny, Intel Corporation
  10. */
  11. #include <linux/bpf.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include <limits.h>
  19. #include <sys/resource.h>
  20. #include <getopt.h>
  21. #include <net/if.h>
  22. #include "bpf_load.h"
  23. #include "bpf_util.h"
  24. #include <bpf/libbpf.h>
  25. static void dump_counts(int fd)
  26. {
  27. __u32 key;
  28. __u64 value;
  29. for (key = 0; key < 256; key++) {
  30. if (bpf_map_lookup_elem(fd, &key, &value)) {
  31. printf("failed to read key %u\n", key);
  32. continue;
  33. }
  34. if (value)
  35. printf("0x%02x : %llu\n", key, value);
  36. }
  37. }
  38. static void dump_all_counts(void)
  39. {
  40. printf("Read 'Class : count'\n");
  41. dump_counts(map_fd[0]);
  42. printf("Write 'Class : count'\n");
  43. dump_counts(map_fd[1]);
  44. }
  45. static void dump_exit(int sig)
  46. {
  47. dump_all_counts();
  48. exit(0);
  49. }
  50. static const struct option long_options[] = {
  51. {"help", no_argument, NULL, 'h'},
  52. {"delay", required_argument, NULL, 'd'},
  53. };
  54. static void usage(char *cmd)
  55. {
  56. printf("eBPF test program to count packets from various IP addresses\n"
  57. "Usage: %s <options>\n"
  58. " --help, -h this menu\n"
  59. " --delay, -d <delay> wait <delay> sec between prints [1 - 1000000]\n"
  60. , cmd
  61. );
  62. }
  63. int main(int argc, char **argv)
  64. {
  65. unsigned long delay = 5;
  66. int longindex = 0;
  67. int opt;
  68. char bpf_file[256];
  69. /* Create the eBPF kernel code path name.
  70. * This follows the pattern of all of the other bpf samples
  71. */
  72. snprintf(bpf_file, sizeof(bpf_file), "%s_kern.o", argv[0]);
  73. /* Do one final dump when exiting */
  74. signal(SIGINT, dump_exit);
  75. signal(SIGTERM, dump_exit);
  76. while ((opt = getopt_long(argc, argv, "hd:rSw",
  77. long_options, &longindex)) != -1) {
  78. switch (opt) {
  79. case 'd':
  80. delay = strtoul(optarg, NULL, 0);
  81. if (delay == ULONG_MAX || delay < 0 ||
  82. delay > 1000000) {
  83. fprintf(stderr, "ERROR: invalid delay : %s\n",
  84. optarg);
  85. usage(argv[0]);
  86. return 1;
  87. }
  88. break;
  89. default:
  90. case 'h':
  91. usage(argv[0]);
  92. return 1;
  93. }
  94. }
  95. if (load_bpf_file(bpf_file)) {
  96. fprintf(stderr, "ERROR: failed to load eBPF from file : %s\n",
  97. bpf_file);
  98. return 1;
  99. }
  100. while (1) {
  101. sleep(delay);
  102. dump_all_counts();
  103. }
  104. return 0;
  105. }