]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_bpf.c
tc: add support for BPF based actions
[mirror_iproute2.git] / tc / m_bpf.c
1 /*
2 * m_bpf.c BFP based action module
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>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <stdbool.h>
17 #include <linux/tc_act/tc_bpf.h>
18
19 #include "utils.h"
20 #include "rt_names.h"
21 #include "tc_util.h"
22 #include "tc_bpf.h"
23
24 static void explain(void)
25 {
26 fprintf(stderr, "Usage: ... bpf ...\n");
27 fprintf(stderr, "\n");
28 fprintf(stderr, " [inline]: run bytecode BPF_BYTECODE\n");
29 fprintf(stderr, " [from file]: run bytecode-file FILE\n");
30 fprintf(stderr, "\n");
31 fprintf(stderr, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n");
32 fprintf(stderr, " c,t,f,k and s are decimals; s denotes number of 4-tuples\n");
33 fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string\n");
34 fprintf(stderr, "\nACTION_SPEC := ... look at individual actions\n");
35 fprintf(stderr, "NOTE: CLASSID is parsed as hexadecimal input.\n");
36 }
37
38 static void usage(void)
39 {
40 explain();
41 exit(-1);
42 }
43
44 static int parse_bpf(struct action_util *a, int *argc_p, char ***argv_p,
45 int tca_id, struct nlmsghdr *n)
46 {
47 int argc = *argc_p;
48 char **argv = *argv_p;
49 struct rtattr *tail;
50 struct tc_act_bpf parm = { 0 };
51 struct sock_filter bpf_ops[BPF_MAXINSNS];
52 __u16 bpf_len = 0;
53
54 if (matches(*argv, "bpf") != 0)
55 return -1;
56
57 NEXT_ARG();
58
59 while (argc > 0) {
60 if (matches(*argv, "run") == 0) {
61 bool from_file;
62 int ret;
63
64 NEXT_ARG();
65 if (strcmp(*argv, "bytecode-file") == 0) {
66 from_file = true;
67 } else if (strcmp(*argv, "bytecode") == 0) {
68 from_file = false;
69 } else {
70 fprintf(stderr, "unexpected \"%s\"\n", *argv);
71 explain();
72 return -1;
73 }
74 NEXT_ARG();
75 ret = bpf_parse_ops(argc, argv, bpf_ops, from_file);
76 if (ret < 0) {
77 fprintf(stderr, "Illegal \"bytecode\"\n");
78 return -1;
79 }
80 bpf_len = ret;
81 } else if (matches(*argv, "help") == 0) {
82 usage();
83 } else {
84 break;
85 }
86 argc--;
87 argv++;
88 }
89
90 parm.action = TC_ACT_PIPE;
91 if (argc) {
92 if (matches(*argv, "reclassify") == 0) {
93 parm.action = TC_ACT_RECLASSIFY;
94 NEXT_ARG();
95 } else if (matches(*argv, "pipe") == 0) {
96 parm.action = TC_ACT_PIPE;
97 NEXT_ARG();
98 } else if (matches(*argv, "drop") == 0 ||
99 matches(*argv, "shot") == 0) {
100 parm.action = TC_ACT_SHOT;
101 NEXT_ARG();
102 } else if (matches(*argv, "continue") == 0) {
103 parm.action = TC_ACT_UNSPEC;
104 NEXT_ARG();
105 } else if (matches(*argv, "pass") == 0) {
106 parm.action = TC_ACT_OK;
107 NEXT_ARG();
108 }
109 }
110
111 if (argc) {
112 if (matches(*argv, "index") == 0) {
113 NEXT_ARG();
114 if (get_u32(&parm.index, *argv, 10)) {
115 fprintf(stderr, "bpf: Illegal \"index\"\n");
116 return -1;
117 }
118 argc--;
119 argv++;
120 }
121 }
122
123 if (!bpf_len) {
124 fprintf(stderr, "bpf: Bytecode needs to be passed\n");
125 explain();
126 return -1;
127 }
128
129 tail = NLMSG_TAIL(n);
130 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
131 addattr_l(n, MAX_MSG, TCA_ACT_BPF_PARMS, &parm, sizeof(parm));
132 addattr16(n, MAX_MSG, TCA_ACT_BPF_OPS_LEN, bpf_len);
133 addattr_l(n, MAX_MSG, TCA_ACT_BPF_OPS, &bpf_ops,
134 bpf_len * sizeof(struct sock_filter));
135 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
136
137 *argc_p = argc;
138 *argv_p = argv;
139 return 0;
140 }
141
142 static int print_bpf(struct action_util *au, FILE *f, struct rtattr *arg)
143 {
144 struct rtattr *tb[TCA_ACT_BPF_MAX + 1];
145 struct tc_act_bpf *parm;
146
147 if (arg == NULL)
148 return -1;
149
150 parse_rtattr_nested(tb, TCA_ACT_BPF_MAX, arg);
151
152 if (!tb[TCA_ACT_BPF_PARMS]) {
153 fprintf(f, "[NULL bpf parameters]");
154 return -1;
155 }
156 parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]);
157
158 fprintf(f, " bpf ");
159
160 if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN])
161 bpf_print_ops(f, tb[TCA_ACT_BPF_OPS],
162 rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN]));
163
164 fprintf(f, "\n\tindex %d ref %d bind %d", parm->index, parm->refcnt,
165 parm->bindcnt);
166
167 if (show_stats) {
168 if (tb[TCA_ACT_BPF_TM]) {
169 struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]);
170 print_tm(f, tm);
171 }
172 }
173
174 fprintf(f, "\n ");
175
176 return 0;
177 }
178
179 struct action_util bpf_action_util = {
180 .id = "bpf",
181 .parse_aopt = parse_bpf,
182 .print_aopt = print_bpf,
183 };