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

TOMOYO Linux Cross Reference
Linux/samples/bpf/xdp_redirect_user.c

Version: ~ [ linux-5.18-rc6 ] ~ [ linux-5.17.6 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.38 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.114 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.192 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.241 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.277 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.312 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ 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.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of version 2 of the GNU General Public
  5  * License as published by the Free Software Foundation.
  6  *
  7  * This program is distributed in the hope that it will be useful, but
  8  * WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 10  * General Public License for more details.
 11  */
 12 #include <linux/bpf.h>
 13 #include <linux/if_link.h>
 14 #include <assert.h>
 15 #include <errno.h>
 16 #include <signal.h>
 17 #include <stdio.h>
 18 #include <stdlib.h>
 19 #include <stdbool.h>
 20 #include <string.h>
 21 #include <unistd.h>
 22 #include <libgen.h>
 23 #include <sys/resource.h>
 24 
 25 #include "bpf_load.h"
 26 #include "bpf_util.h"
 27 #include <bpf/bpf.h>
 28 
 29 static int ifindex_in;
 30 static int ifindex_out;
 31 static bool ifindex_out_xdp_dummy_attached = true;
 32 
 33 static __u32 xdp_flags;
 34 
 35 static void int_exit(int sig)
 36 {
 37         bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
 38         if (ifindex_out_xdp_dummy_attached)
 39                 bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
 40         exit(0);
 41 }
 42 
 43 static void poll_stats(int interval, int ifindex)
 44 {
 45         unsigned int nr_cpus = bpf_num_possible_cpus();
 46         __u64 values[nr_cpus], prev[nr_cpus];
 47 
 48         memset(prev, 0, sizeof(prev));
 49 
 50         while (1) {
 51                 __u64 sum = 0;
 52                 __u32 key = 0;
 53                 int i;
 54 
 55                 sleep(interval);
 56                 assert(bpf_map_lookup_elem(map_fd[1], &key, values) == 0);
 57                 for (i = 0; i < nr_cpus; i++)
 58                         sum += (values[i] - prev[i]);
 59                 if (sum)
 60                         printf("ifindex %i: %10llu pkt/s\n",
 61                                ifindex, sum / interval);
 62                 memcpy(prev, values, sizeof(values));
 63         }
 64 }
 65 
 66 static void usage(const char *prog)
 67 {
 68         fprintf(stderr,
 69                 "usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
 70                 "OPTS:\n"
 71                 "    -S    use skb-mode\n"
 72                 "    -N    enforce native mode\n",
 73                 prog);
 74 }
 75 
 76 
 77 int main(int argc, char **argv)
 78 {
 79         struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
 80         const char *optstr = "SN";
 81         char filename[256];
 82         int ret, opt, key = 0;
 83 
 84         while ((opt = getopt(argc, argv, optstr)) != -1) {
 85                 switch (opt) {
 86                 case 'S':
 87                         xdp_flags |= XDP_FLAGS_SKB_MODE;
 88                         break;
 89                 case 'N':
 90                         xdp_flags |= XDP_FLAGS_DRV_MODE;
 91                         break;
 92                 default:
 93                         usage(basename(argv[0]));
 94                         return 1;
 95                 }
 96         }
 97 
 98         if (optind == argc) {
 99                 printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
100                 return 1;
101         }
102 
103         if (setrlimit(RLIMIT_MEMLOCK, &r)) {
104                 perror("setrlimit(RLIMIT_MEMLOCK)");
105                 return 1;
106         }
107 
108         ifindex_in = strtoul(argv[optind], NULL, 0);
109         ifindex_out = strtoul(argv[optind + 1], NULL, 0);
110         printf("input: %d output: %d\n", ifindex_in, ifindex_out);
111 
112         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
113 
114         if (load_bpf_file(filename)) {
115                 printf("%s", bpf_log_buf);
116                 return 1;
117         }
118 
119         if (!prog_fd[0]) {
120                 printf("load_bpf_file: %s\n", strerror(errno));
121                 return 1;
122         }
123 
124         if (bpf_set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
125                 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
126                 return 1;
127         }
128 
129         /* Loading dummy XDP prog on out-device */
130         if (bpf_set_link_xdp_fd(ifindex_out, prog_fd[1],
131                             (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
132                 printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
133                 ifindex_out_xdp_dummy_attached = false;
134         }
135 
136         signal(SIGINT, int_exit);
137         signal(SIGTERM, int_exit);
138 
139         /* bpf redirect port */
140         ret = bpf_map_update_elem(map_fd[0], &key, &ifindex_out, 0);
141         if (ret) {
142                 perror("bpf_update_elem");
143                 goto out;
144         }
145 
146         poll_stats(2, ifindex_out);
147 
148 out:
149         return 0;
150 }
151 

~ [ 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