]>
Commit | Line | Data |
---|---|---|
86ab59a6 | 1 | /* |
d937a74b | 2 | * m_bpf.c BPF based action module |
86ab59a6 JP |
3 | * |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Jiri Pirko <jiri@resnulli.us> | |
6256f8c9 | 10 | * Daniel Borkmann <daniel@iogearbox.net> |
86ab59a6 JP |
11 | */ |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
32e93fb7 | 15 | |
6256f8c9 | 16 | #include <linux/bpf.h> |
86ab59a6 JP |
17 | #include <linux/tc_act/tc_bpf.h> |
18 | ||
19 | #include "utils.h" | |
e4225669 | 20 | |
86ab59a6 | 21 | #include "tc_util.h" |
e4225669 | 22 | #include "bpf_util.h" |
86ab59a6 | 23 | |
6256f8c9 DB |
24 | static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_ACT; |
25 | ||
86ab59a6 JP |
26 | static void explain(void) |
27 | { | |
baed9084 | 28 | fprintf(stderr, "Usage: ... bpf ... [ index INDEX ]\n"); |
86ab59a6 | 29 | fprintf(stderr, "\n"); |
6256f8c9 DB |
30 | fprintf(stderr, "BPF use case:\n"); |
31 | fprintf(stderr, " bytecode BPF_BYTECODE\n"); | |
32 | fprintf(stderr, " bytecode-file FILE\n"); | |
33 | fprintf(stderr, "\n"); | |
34 | fprintf(stderr, "eBPF use case:\n"); | |
d937a74b DB |
35 | fprintf(stderr, " object-file FILE [ section ACT_NAME ] [ export UDS_FILE ]"); |
36 | fprintf(stderr, " [ verbose ]\n"); | |
32e93fb7 | 37 | fprintf(stderr, " object-pinned FILE\n"); |
86ab59a6 JP |
38 | fprintf(stderr, "\n"); |
39 | fprintf(stderr, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n"); | |
6256f8c9 DB |
40 | fprintf(stderr, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n"); |
41 | fprintf(stderr, "\n"); | |
42 | fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n"); | |
32e93fb7 DB |
43 | fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode, or a\n"); |
44 | fprintf(stderr, "pinned eBPF program.\n"); | |
6256f8c9 DB |
45 | fprintf(stderr, "\n"); |
46 | fprintf(stderr, "Where ACT_NAME refers to the section name containing the\n"); | |
e4225669 | 47 | fprintf(stderr, "action (default \'%s\').\n", bpf_prog_to_default_section(bpf_type)); |
6256f8c9 DB |
48 | fprintf(stderr, "\n"); |
49 | fprintf(stderr, "Where UDS_FILE points to a unix domain socket file in order\n"); | |
50 | fprintf(stderr, "to hand off control of all created eBPF maps to an agent.\n"); | |
baed9084 DB |
51 | fprintf(stderr, "\n"); |
52 | fprintf(stderr, "Where optionally INDEX points to an existing action, or\n"); | |
53 | fprintf(stderr, "explicitly specifies an action index upon creation.\n"); | |
86ab59a6 JP |
54 | } |
55 | ||
e4225669 DB |
56 | static void bpf_cbpf_cb(void *nl, const struct sock_filter *ops, int ops_len) |
57 | { | |
58 | addattr16(nl, MAX_MSG, TCA_ACT_BPF_OPS_LEN, ops_len); | |
59 | addattr_l(nl, MAX_MSG, TCA_ACT_BPF_OPS, ops, | |
60 | ops_len * sizeof(struct sock_filter)); | |
61 | } | |
62 | ||
63 | static void bpf_ebpf_cb(void *nl, int fd, const char *annotation) | |
64 | { | |
65 | addattr32(nl, MAX_MSG, TCA_ACT_BPF_FD, fd); | |
66 | addattrstrz(nl, MAX_MSG, TCA_ACT_BPF_NAME, annotation); | |
67 | } | |
68 | ||
69 | static const struct bpf_cfg_ops bpf_cb_ops = { | |
70 | .cbpf_cb = bpf_cbpf_cb, | |
71 | .ebpf_cb = bpf_ebpf_cb, | |
72 | }; | |
73 | ||
32e93fb7 DB |
74 | static int bpf_parse_opt(struct action_util *a, int *ptr_argc, char ***ptr_argv, |
75 | int tca_id, struct nlmsghdr *n) | |
86ab59a6 | 76 | { |
32e93fb7 | 77 | const char *bpf_obj = NULL, *bpf_uds_name = NULL; |
e67aba55 | 78 | struct tc_act_bpf parm = {}; |
e4225669 | 79 | struct bpf_cfg_in cfg = {}; |
32e93fb7 | 80 | bool seen_run = false; |
86ab59a6 | 81 | struct rtattr *tail; |
32e93fb7 DB |
82 | int argc, ret = 0; |
83 | char **argv; | |
84 | ||
85 | argv = *ptr_argv; | |
86 | argc = *ptr_argc; | |
86ab59a6 JP |
87 | |
88 | if (matches(*argv, "bpf") != 0) | |
89 | return -1; | |
90 | ||
91 | NEXT_ARG(); | |
92 | ||
32e93fb7 DB |
93 | tail = NLMSG_TAIL(n); |
94 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); | |
95 | ||
86ab59a6 JP |
96 | while (argc > 0) { |
97 | if (matches(*argv, "run") == 0) { | |
86ab59a6 | 98 | NEXT_ARG(); |
67c857df JK |
99 | |
100 | if (seen_run) | |
101 | duparg("run", *argv); | |
6256f8c9 | 102 | opt_bpf: |
6256f8c9 | 103 | seen_run = true; |
658cfebc | 104 | cfg.type = bpf_type; |
e4225669 DB |
105 | cfg.argc = argc; |
106 | cfg.argv = argv; | |
107 | ||
399db839 | 108 | if (bpf_parse_and_load_common(&cfg, &bpf_cb_ops, n)) |
86ab59a6 | 109 | return -1; |
e4225669 DB |
110 | |
111 | argc = cfg.argc; | |
112 | argv = cfg.argv; | |
113 | ||
114 | bpf_obj = cfg.object; | |
115 | bpf_uds_name = cfg.uds; | |
86ab59a6 | 116 | } else if (matches(*argv, "help") == 0) { |
32e93fb7 DB |
117 | explain(); |
118 | return -1; | |
baed9084 DB |
119 | } else if (matches(*argv, "index") == 0) { |
120 | break; | |
86ab59a6 | 121 | } else { |
6256f8c9 DB |
122 | if (!seen_run) |
123 | goto opt_bpf; | |
86ab59a6 JP |
124 | break; |
125 | } | |
343dc908 DB |
126 | |
127 | NEXT_ARG_FWD(); | |
86ab59a6 JP |
128 | } |
129 | ||
e67aba55 JP |
130 | parse_action_control_dflt(&argc, &argv, &parm.action, |
131 | false, TC_ACT_PIPE); | |
86ab59a6 JP |
132 | |
133 | if (argc) { | |
134 | if (matches(*argv, "index") == 0) { | |
135 | NEXT_ARG(); | |
136 | if (get_u32(&parm.index, *argv, 10)) { | |
137 | fprintf(stderr, "bpf: Illegal \"index\"\n"); | |
138 | return -1; | |
139 | } | |
343dc908 DB |
140 | |
141 | NEXT_ARG_FWD(); | |
86ab59a6 JP |
142 | } |
143 | } | |
144 | ||
86ab59a6 | 145 | addattr_l(n, MAX_MSG, TCA_ACT_BPF_PARMS, &parm, sizeof(parm)); |
86ab59a6 JP |
146 | tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail; |
147 | ||
6256f8c9 | 148 | if (bpf_uds_name) |
4bd62446 | 149 | ret = bpf_send_map_fds(bpf_uds_name, bpf_obj); |
6256f8c9 | 150 | |
32e93fb7 DB |
151 | *ptr_argc = argc; |
152 | *ptr_argv = argv; | |
153 | ||
6256f8c9 | 154 | return ret; |
86ab59a6 JP |
155 | } |
156 | ||
32e93fb7 | 157 | static int bpf_print_opt(struct action_util *au, FILE *f, struct rtattr *arg) |
86ab59a6 JP |
158 | { |
159 | struct rtattr *tb[TCA_ACT_BPF_MAX + 1]; | |
160 | struct tc_act_bpf *parm; | |
a0b5b7cf | 161 | int dump_ok = 0; |
32a121cb | 162 | |
86ab59a6 JP |
163 | if (arg == NULL) |
164 | return -1; | |
165 | ||
166 | parse_rtattr_nested(tb, TCA_ACT_BPF_MAX, arg); | |
167 | ||
168 | if (!tb[TCA_ACT_BPF_PARMS]) { | |
169 | fprintf(f, "[NULL bpf parameters]"); | |
170 | return -1; | |
171 | } | |
6256f8c9 | 172 | |
86ab59a6 | 173 | parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]); |
6256f8c9 DB |
174 | fprintf(f, "bpf "); |
175 | ||
176 | if (tb[TCA_ACT_BPF_NAME]) | |
177 | fprintf(f, "%s ", rta_getattr_str(tb[TCA_ACT_BPF_NAME])); | |
86ab59a6 | 178 | |
6256f8c9 | 179 | if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN]) { |
86ab59a6 JP |
180 | bpf_print_ops(f, tb[TCA_ACT_BPF_OPS], |
181 | rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN])); | |
6256f8c9 DB |
182 | fprintf(f, " "); |
183 | } | |
86ab59a6 | 184 | |
a0b5b7cf DB |
185 | if (tb[TCA_ACT_BPF_ID]) |
186 | dump_ok = bpf_dump_prog_info(f, rta_getattr_u32(tb[TCA_ACT_BPF_ID])); | |
187 | if (!dump_ok && tb[TCA_ACT_BPF_TAG]) { | |
e37d706b DB |
188 | SPRINT_BUF(b); |
189 | ||
190 | fprintf(f, "tag %s ", | |
191 | hexstring_n2a(RTA_DATA(tb[TCA_ACT_BPF_TAG]), | |
192 | RTA_PAYLOAD(tb[TCA_ACT_BPF_TAG]), | |
193 | b, sizeof(b))); | |
194 | } | |
195 | ||
e67aba55 | 196 | print_action_control(f, "default-action ", parm->action, "\n"); |
53075318 | 197 | fprintf(f, "\tindex %u ref %d bind %d", parm->index, parm->refcnt, |
86ab59a6 JP |
198 | parm->bindcnt); |
199 | ||
200 | if (show_stats) { | |
201 | if (tb[TCA_ACT_BPF_TM]) { | |
202 | struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]); | |
32a121cb | 203 | |
86ab59a6 JP |
204 | print_tm(f, tm); |
205 | } | |
206 | } | |
207 | ||
208 | fprintf(f, "\n "); | |
86ab59a6 JP |
209 | return 0; |
210 | } | |
211 | ||
212 | struct action_util bpf_action_util = { | |
32e93fb7 DB |
213 | .id = "bpf", |
214 | .parse_aopt = bpf_parse_opt, | |
215 | .print_aopt = bpf_print_opt, | |
86ab59a6 | 216 | }; |