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