~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/flow_dissector_load.c

Version: ~ [ linux-5.17-rc1 ] ~ [ linux-5.16.2 ] ~ [ linux-5.15.16 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.93 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.173 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.225 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.262 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.297 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.299 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <error.h>
  3 #include <errno.h>
  4 #include <getopt.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8 #include <sys/stat.h>
  9 #include <fcntl.h>
 10 #include <unistd.h>
 11 #include <bpf/bpf.h>
 12 #include <bpf/libbpf.h>
 13 
 14 #include "bpf_rlimit.h"
 15 #include "flow_dissector_load.h"
 16 
 17 const char *cfg_pin_path = "/sys/fs/bpf/flow_dissector";
 18 const char *cfg_map_name = "jmp_table";
 19 bool cfg_attach = true;
 20 char *cfg_section_name;
 21 char *cfg_path_name;
 22 
 23 static void load_and_attach_program(void)
 24 {
 25         int prog_fd, ret;
 26         struct bpf_object *obj;
 27 
 28         ret = bpf_flow_load(&obj, cfg_path_name, cfg_section_name,
 29                             cfg_map_name, NULL, &prog_fd, NULL);
 30         if (ret)
 31                 error(1, 0, "bpf_flow_load %s", cfg_path_name);
 32 
 33         ret = bpf_prog_attach(prog_fd, 0 /* Ignore */, BPF_FLOW_DISSECTOR, 0);
 34         if (ret)
 35                 error(1, 0, "bpf_prog_attach %s", cfg_path_name);
 36 
 37         ret = bpf_object__pin(obj, cfg_pin_path);
 38         if (ret)
 39                 error(1, 0, "bpf_object__pin %s", cfg_pin_path);
 40 }
 41 
 42 static void detach_program(void)
 43 {
 44         char command[64];
 45         int ret;
 46 
 47         ret = bpf_prog_detach(0, BPF_FLOW_DISSECTOR);
 48         if (ret)
 49                 error(1, 0, "bpf_prog_detach");
 50 
 51         /* To unpin, it is necessary and sufficient to just remove this dir */
 52         sprintf(command, "rm -r %s", cfg_pin_path);
 53         ret = system(command);
 54         if (ret)
 55                 error(1, errno, "%s", command);
 56 }
 57 
 58 static void parse_opts(int argc, char **argv)
 59 {
 60         bool attach = false;
 61         bool detach = false;
 62         int c;
 63 
 64         while ((c = getopt(argc, argv, "adp:s:")) != -1) {
 65                 switch (c) {
 66                 case 'a':
 67                         if (detach)
 68                                 error(1, 0, "attach/detach are exclusive");
 69                         attach = true;
 70                         break;
 71                 case 'd':
 72                         if (attach)
 73                                 error(1, 0, "attach/detach are exclusive");
 74                         detach = true;
 75                         break;
 76                 case 'p':
 77                         if (cfg_path_name)
 78                                 error(1, 0, "only one prog name can be given");
 79 
 80                         cfg_path_name = optarg;
 81                         break;
 82                 case 's':
 83                         if (cfg_section_name)
 84                                 error(1, 0, "only one section can be given");
 85 
 86                         cfg_section_name = optarg;
 87                         break;
 88                 }
 89         }
 90 
 91         if (detach)
 92                 cfg_attach = false;
 93 
 94         if (cfg_attach && !cfg_path_name)
 95                 error(1, 0, "must provide a path to the BPF program");
 96 
 97         if (cfg_attach && !cfg_section_name)
 98                 error(1, 0, "must provide a section name");
 99 }
100 
101 int main(int argc, char **argv)
102 {
103         parse_opts(argc, argv);
104         if (cfg_attach)
105                 load_and_attach_program();
106         else
107                 detach_program();
108         return 0;
109 }
110 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp