Merge tag 'for-linus-5.12-1' of git://github.com/cminyard/linux-ipmi
[linux-2.6-microblaze.git] / samples / bpf / tc_l2_redirect_user.c
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
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <errno.h>
12
13 #include <bpf/bpf.h>
14
15 static void usage(void)
16 {
17         printf("Usage: tc_l2_ipip_redirect [...]\n");
18         printf("       -U <file>   Update an already pinned BPF array\n");
19         printf("       -i <ifindex> Interface index\n");
20         printf("       -h          Display this help\n");
21 }
22
23 int main(int argc, char **argv)
24 {
25         const char *pinned_file = NULL;
26         int ifindex = -1;
27         int array_key = 0;
28         int array_fd = -1;
29         int ret = -1;
30         int opt;
31
32         while ((opt = getopt(argc, argv, "F:U:i:")) != -1) {
33                 switch (opt) {
34                 /* General args */
35                 case 'U':
36                         pinned_file = optarg;
37                         break;
38                 case 'i':
39                         ifindex = atoi(optarg);
40                         break;
41                 default:
42                         usage();
43                         goto out;
44                 }
45         }
46
47         if (ifindex < 0 || !pinned_file) {
48                 usage();
49                 goto out;
50         }
51
52         array_fd = bpf_obj_get(pinned_file);
53         if (array_fd < 0) {
54                 fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
55                         pinned_file, strerror(errno), errno);
56                 goto out;
57         }
58
59         /* bpf_tunnel_key.remote_ipv4 expects host byte orders */
60         ret = bpf_map_update_elem(array_fd, &array_key, &ifindex, 0);
61         if (ret) {
62                 perror("bpf_map_update_elem");
63                 goto out;
64         }
65
66 out:
67         if (array_fd != -1)
68                 close(array_fd);
69         return ret;
70 }