samples/bpf: Be consistent with bpf_load_program bpf_insn parameter
[platform/kernel/linux-rpi.git] / samples / bpf / fds_example.c
1 #include <linux/unistd.h>
2 #include <linux/bpf.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <errno.h>
11
12 #include <sys/types.h>
13 #include <sys/socket.h>
14
15 #include "bpf_load.h"
16 #include "libbpf.h"
17
18 #define BPF_F_PIN       (1 << 0)
19 #define BPF_F_GET       (1 << 1)
20 #define BPF_F_PIN_GET   (BPF_F_PIN | BPF_F_GET)
21
22 #define BPF_F_KEY       (1 << 2)
23 #define BPF_F_VAL       (1 << 3)
24 #define BPF_F_KEY_VAL   (BPF_F_KEY | BPF_F_VAL)
25
26 #define BPF_M_UNSPEC    0
27 #define BPF_M_MAP       1
28 #define BPF_M_PROG      2
29
30 static void usage(void)
31 {
32         printf("Usage: fds_example [...]\n");
33         printf("       -F <file>   File to pin/get object\n");
34         printf("       -P          |- pin object\n");
35         printf("       -G          `- get object\n");
36         printf("       -m          eBPF map mode\n");
37         printf("       -k <key>    |- map key\n");
38         printf("       -v <value>  `- map value\n");
39         printf("       -p          eBPF prog mode\n");
40         printf("       -o <object> `- object file\n");
41         printf("       -h          Display this help.\n");
42 }
43
44 static int bpf_map_create(void)
45 {
46         return bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(uint32_t),
47                               sizeof(uint32_t), 1024, 0);
48 }
49
50 static int bpf_prog_create(const char *object)
51 {
52         static struct bpf_insn insns[] = {
53                 BPF_MOV64_IMM(BPF_REG_0, 1),
54                 BPF_EXIT_INSN(),
55         };
56         size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn);
57
58         if (object) {
59                 assert(!load_bpf_file((char *)object));
60                 return prog_fd[0];
61         } else {
62                 return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
63                                         insns, insns_cnt, "GPL", 0,
64                                         bpf_log_buf, BPF_LOG_BUF_SIZE);
65         }
66 }
67
68 static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
69                       uint32_t value)
70 {
71         int fd, ret;
72
73         if (flags & BPF_F_PIN) {
74                 fd = bpf_map_create();
75                 printf("bpf: map fd:%d (%s)\n", fd, strerror(errno));
76                 assert(fd > 0);
77
78                 ret = bpf_obj_pin(fd, file);
79                 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
80                 assert(ret == 0);
81         } else {
82                 fd = bpf_obj_get(file);
83                 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
84                 assert(fd > 0);
85         }
86
87         if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
88                 ret = bpf_map_update_elem(fd, &key, &value, 0);
89                 printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd, key, value,
90                        ret, strerror(errno));
91                 assert(ret == 0);
92         } else if (flags & BPF_F_KEY) {
93                 ret = bpf_map_lookup_elem(fd, &key, &value);
94                 printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
95                        ret, strerror(errno));
96                 assert(ret == 0);
97         }
98
99         return 0;
100 }
101
102 static int bpf_do_prog(const char *file, uint32_t flags, const char *object)
103 {
104         int fd, sock, ret;
105
106         if (flags & BPF_F_PIN) {
107                 fd = bpf_prog_create(object);
108                 printf("bpf: prog fd:%d (%s)\n", fd, strerror(errno));
109                 assert(fd > 0);
110
111                 ret = bpf_obj_pin(fd, file);
112                 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
113                 assert(ret == 0);
114         } else {
115                 fd = bpf_obj_get(file);
116                 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
117                 assert(fd > 0);
118         }
119
120         sock = open_raw_sock("lo");
121         assert(sock > 0);
122
123         ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &fd, sizeof(fd));
124         printf("bpf: sock:%d <- fd:%d attached ret:(%d,%s)\n", sock, fd,
125                ret, strerror(errno));
126         assert(ret == 0);
127
128         return 0;
129 }
130
131 int main(int argc, char **argv)
132 {
133         const char *file = NULL, *object = NULL;
134         uint32_t key = 0, value = 0, flags = 0;
135         int opt, mode = BPF_M_UNSPEC;
136
137         while ((opt = getopt(argc, argv, "F:PGmk:v:po:")) != -1) {
138                 switch (opt) {
139                 /* General args */
140                 case 'F':
141                         file = optarg;
142                         break;
143                 case 'P':
144                         flags |= BPF_F_PIN;
145                         break;
146                 case 'G':
147                         flags |= BPF_F_GET;
148                         break;
149                 /* Map-related args */
150                 case 'm':
151                         mode = BPF_M_MAP;
152                         break;
153                 case 'k':
154                         key = strtoul(optarg, NULL, 0);
155                         flags |= BPF_F_KEY;
156                         break;
157                 case 'v':
158                         value = strtoul(optarg, NULL, 0);
159                         flags |= BPF_F_VAL;
160                         break;
161                 /* Prog-related args */
162                 case 'p':
163                         mode = BPF_M_PROG;
164                         break;
165                 case 'o':
166                         object = optarg;
167                         break;
168                 default:
169                         goto out;
170                 }
171         }
172
173         if (!(flags & BPF_F_PIN_GET) || !file)
174                 goto out;
175
176         switch (mode) {
177         case BPF_M_MAP:
178                 return bpf_do_map(file, flags, key, value);
179         case BPF_M_PROG:
180                 return bpf_do_prog(file, flags, object);
181         }
182 out:
183         usage();
184         return -1;
185 }