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
15static void usage(void)
16{
17 printf(format: "Usage: tc_l2_ipip_redirect [...]\n");
18 printf(format: " -U <file> Update an already pinned BPF array\n");
19 printf(format: " -i <ifindex> Interface index\n");
20 printf(format: " -h Display this help\n");
21}
22
23int 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: argc, argv: argv, shortopts: "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(nptr: 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, format: "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(s: "bpf_map_update_elem");
63 goto out;
64 }
65
66out:
67 if (array_fd != -1)
68 close(fd: array_fd);
69 return ret;
70}
71

source code of linux/samples/bpf/tc_l2_redirect_user.c