]>
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" | |
86ab59a6 JP |
20 | #include "tc_util.h" |
21 | #include "tc_bpf.h" | |
22 | ||
6256f8c9 DB |
23 | static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_ACT; |
24 | ||
32e93fb7 DB |
25 | static const int nla_tbl[BPF_NLA_MAX] = { |
26 | [BPF_NLA_OPS_LEN] = TCA_ACT_BPF_OPS_LEN, | |
27 | [BPF_NLA_OPS] = TCA_ACT_BPF_OPS, | |
28 | [BPF_NLA_FD] = TCA_ACT_BPF_FD, | |
29 | [BPF_NLA_NAME] = TCA_ACT_BPF_NAME, | |
30 | }; | |
31 | ||
86ab59a6 JP |
32 | static void explain(void) |
33 | { | |
baed9084 | 34 | fprintf(stderr, "Usage: ... bpf ... [ index INDEX ]\n"); |
86ab59a6 | 35 | fprintf(stderr, "\n"); |
6256f8c9 DB |
36 | fprintf(stderr, "BPF use case:\n"); |
37 | fprintf(stderr, " bytecode BPF_BYTECODE\n"); | |
38 | fprintf(stderr, " bytecode-file FILE\n"); | |
39 | fprintf(stderr, "\n"); | |
40 | fprintf(stderr, "eBPF use case:\n"); | |
d937a74b DB |
41 | fprintf(stderr, " object-file FILE [ section ACT_NAME ] [ export UDS_FILE ]"); |
42 | fprintf(stderr, " [ verbose ]\n"); | |
32e93fb7 | 43 | fprintf(stderr, " object-pinned FILE\n"); |
86ab59a6 JP |
44 | fprintf(stderr, "\n"); |
45 | fprintf(stderr, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n"); | |
6256f8c9 DB |
46 | fprintf(stderr, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n"); |
47 | fprintf(stderr, "\n"); | |
48 | fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n"); | |
32e93fb7 DB |
49 | fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode, or a\n"); |
50 | fprintf(stderr, "pinned eBPF program.\n"); | |
6256f8c9 DB |
51 | fprintf(stderr, "\n"); |
52 | fprintf(stderr, "Where ACT_NAME refers to the section name containing the\n"); | |
53 | fprintf(stderr, "action (default \'%s\').\n", bpf_default_section(bpf_type)); | |
54 | fprintf(stderr, "\n"); | |
55 | fprintf(stderr, "Where UDS_FILE points to a unix domain socket file in order\n"); | |
56 | fprintf(stderr, "to hand off control of all created eBPF maps to an agent.\n"); | |
baed9084 DB |
57 | fprintf(stderr, "\n"); |
58 | fprintf(stderr, "Where optionally INDEX points to an existing action, or\n"); | |
59 | fprintf(stderr, "explicitly specifies an action index upon creation.\n"); | |
86ab59a6 JP |
60 | } |
61 | ||
32e93fb7 DB |
62 | static int bpf_parse_opt(struct action_util *a, int *ptr_argc, char ***ptr_argv, |
63 | int tca_id, struct nlmsghdr *n) | |
86ab59a6 | 64 | { |
32e93fb7 | 65 | const char *bpf_obj = NULL, *bpf_uds_name = NULL; |
d17b136f | 66 | struct tc_act_bpf parm = { .action = TC_ACT_PIPE }; |
32e93fb7 | 67 | bool seen_run = false; |
86ab59a6 | 68 | struct rtattr *tail; |
32e93fb7 DB |
69 | int argc, ret = 0; |
70 | char **argv; | |
71 | ||
72 | argv = *ptr_argv; | |
73 | argc = *ptr_argc; | |
86ab59a6 JP |
74 | |
75 | if (matches(*argv, "bpf") != 0) | |
76 | return -1; | |
77 | ||
78 | NEXT_ARG(); | |
79 | ||
32e93fb7 DB |
80 | tail = NLMSG_TAIL(n); |
81 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); | |
82 | ||
86ab59a6 JP |
83 | while (argc > 0) { |
84 | if (matches(*argv, "run") == 0) { | |
86ab59a6 | 85 | NEXT_ARG(); |
6256f8c9 | 86 | opt_bpf: |
6256f8c9 | 87 | seen_run = true; |
32e93fb7 DB |
88 | if (bpf_parse_common(&argc, &argv, nla_tbl, bpf_type, |
89 | &bpf_obj, &bpf_uds_name, n)) { | |
90 | fprintf(stderr, "Failed to retrieve (e)BPF data!\n"); | |
86ab59a6 JP |
91 | return -1; |
92 | } | |
86ab59a6 | 93 | } else if (matches(*argv, "help") == 0) { |
32e93fb7 DB |
94 | explain(); |
95 | return -1; | |
baed9084 DB |
96 | } else if (matches(*argv, "index") == 0) { |
97 | break; | |
86ab59a6 | 98 | } else { |
6256f8c9 DB |
99 | if (!seen_run) |
100 | goto opt_bpf; | |
86ab59a6 JP |
101 | break; |
102 | } | |
343dc908 DB |
103 | |
104 | NEXT_ARG_FWD(); | |
86ab59a6 JP |
105 | } |
106 | ||
86ab59a6 JP |
107 | if (argc) { |
108 | if (matches(*argv, "reclassify") == 0) { | |
109 | parm.action = TC_ACT_RECLASSIFY; | |
343dc908 | 110 | NEXT_ARG_FWD(); |
86ab59a6 JP |
111 | } else if (matches(*argv, "pipe") == 0) { |
112 | parm.action = TC_ACT_PIPE; | |
343dc908 | 113 | NEXT_ARG_FWD(); |
86ab59a6 JP |
114 | } else if (matches(*argv, "drop") == 0 || |
115 | matches(*argv, "shot") == 0) { | |
116 | parm.action = TC_ACT_SHOT; | |
343dc908 | 117 | NEXT_ARG_FWD(); |
86ab59a6 JP |
118 | } else if (matches(*argv, "continue") == 0) { |
119 | parm.action = TC_ACT_UNSPEC; | |
343dc908 DB |
120 | NEXT_ARG_FWD(); |
121 | } else if (matches(*argv, "pass") == 0 || | |
122 | matches(*argv, "ok") == 0) { | |
86ab59a6 | 123 | parm.action = TC_ACT_OK; |
343dc908 | 124 | NEXT_ARG_FWD(); |
86ab59a6 JP |
125 | } |
126 | } | |
127 | ||
128 | if (argc) { | |
129 | if (matches(*argv, "index") == 0) { | |
130 | NEXT_ARG(); | |
131 | if (get_u32(&parm.index, *argv, 10)) { | |
132 | fprintf(stderr, "bpf: Illegal \"index\"\n"); | |
133 | return -1; | |
134 | } | |
343dc908 DB |
135 | |
136 | NEXT_ARG_FWD(); | |
86ab59a6 JP |
137 | } |
138 | } | |
139 | ||
86ab59a6 | 140 | addattr_l(n, MAX_MSG, TCA_ACT_BPF_PARMS, &parm, sizeof(parm)); |
86ab59a6 JP |
141 | tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail; |
142 | ||
6256f8c9 | 143 | if (bpf_uds_name) |
4bd62446 | 144 | ret = bpf_send_map_fds(bpf_uds_name, bpf_obj); |
6256f8c9 | 145 | |
32e93fb7 DB |
146 | *ptr_argc = argc; |
147 | *ptr_argv = argv; | |
148 | ||
6256f8c9 | 149 | return ret; |
86ab59a6 JP |
150 | } |
151 | ||
32e93fb7 | 152 | static int bpf_print_opt(struct action_util *au, FILE *f, struct rtattr *arg) |
86ab59a6 JP |
153 | { |
154 | struct rtattr *tb[TCA_ACT_BPF_MAX + 1]; | |
155 | struct tc_act_bpf *parm; | |
32a121cb | 156 | |
6256f8c9 | 157 | SPRINT_BUF(action_buf); |
86ab59a6 JP |
158 | |
159 | if (arg == NULL) | |
160 | return -1; | |
161 | ||
162 | parse_rtattr_nested(tb, TCA_ACT_BPF_MAX, arg); | |
163 | ||
164 | if (!tb[TCA_ACT_BPF_PARMS]) { | |
165 | fprintf(f, "[NULL bpf parameters]"); | |
166 | return -1; | |
167 | } | |
6256f8c9 | 168 | |
86ab59a6 | 169 | parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]); |
6256f8c9 DB |
170 | fprintf(f, "bpf "); |
171 | ||
172 | if (tb[TCA_ACT_BPF_NAME]) | |
173 | fprintf(f, "%s ", rta_getattr_str(tb[TCA_ACT_BPF_NAME])); | |
174 | else if (tb[TCA_ACT_BPF_FD]) | |
175 | fprintf(f, "pfd %u ", rta_getattr_u32(tb[TCA_ACT_BPF_FD])); | |
86ab59a6 | 176 | |
6256f8c9 | 177 | if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN]) { |
86ab59a6 JP |
178 | bpf_print_ops(f, tb[TCA_ACT_BPF_OPS], |
179 | rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN])); | |
6256f8c9 DB |
180 | fprintf(f, " "); |
181 | } | |
86ab59a6 | 182 | |
6256f8c9 DB |
183 | fprintf(f, "default-action %s\n", action_n2a(parm->action, action_buf, |
184 | sizeof(action_buf))); | |
185 | fprintf(f, "\tindex %d ref %d bind %d", parm->index, parm->refcnt, | |
86ab59a6 JP |
186 | parm->bindcnt); |
187 | ||
188 | if (show_stats) { | |
189 | if (tb[TCA_ACT_BPF_TM]) { | |
190 | struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]); | |
32a121cb | 191 | |
86ab59a6 JP |
192 | print_tm(f, tm); |
193 | } | |
194 | } | |
195 | ||
196 | fprintf(f, "\n "); | |
86ab59a6 JP |
197 | return 0; |
198 | } | |
199 | ||
200 | struct action_util bpf_action_util = { | |
32e93fb7 DB |
201 | .id = "bpf", |
202 | .parse_aopt = bpf_parse_opt, | |
203 | .print_aopt = bpf_print_opt, | |
86ab59a6 | 204 | }; |