Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (c) 2016 Facebook
0003  */
0004 #include <linux/unistd.h>
0005 #include <linux/bpf.h>
0006 
0007 #include <stdlib.h>
0008 #include <stdio.h>
0009 #include <unistd.h>
0010 #include <string.h>
0011 #include <errno.h>
0012 
0013 #include <bpf/bpf.h>
0014 
0015 static void usage(void)
0016 {
0017     printf("Usage: tc_l2_ipip_redirect [...]\n");
0018     printf("       -U <file>   Update an already pinned BPF array\n");
0019     printf("       -i <ifindex> Interface index\n");
0020     printf("       -h          Display this help\n");
0021 }
0022 
0023 int main(int argc, char **argv)
0024 {
0025     const char *pinned_file = NULL;
0026     int ifindex = -1;
0027     int array_key = 0;
0028     int array_fd = -1;
0029     int ret = -1;
0030     int opt;
0031 
0032     while ((opt = getopt(argc, argv, "F:U:i:")) != -1) {
0033         switch (opt) {
0034         /* General args */
0035         case 'U':
0036             pinned_file = optarg;
0037             break;
0038         case 'i':
0039             ifindex = atoi(optarg);
0040             break;
0041         default:
0042             usage();
0043             goto out;
0044         }
0045     }
0046 
0047     if (ifindex < 0 || !pinned_file) {
0048         usage();
0049         goto out;
0050     }
0051 
0052     array_fd = bpf_obj_get(pinned_file);
0053     if (array_fd < 0) {
0054         fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
0055             pinned_file, strerror(errno), errno);
0056         goto out;
0057     }
0058 
0059     /* bpf_tunnel_key.remote_ipv4 expects host byte orders */
0060     ret = bpf_map_update_elem(array_fd, &array_key, &ifindex, 0);
0061     if (ret) {
0062         perror("bpf_map_update_elem");
0063         goto out;
0064     }
0065 
0066 out:
0067     if (array_fd != -1)
0068         close(array_fd);
0069     return ret;
0070 }