]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_bpf.c
m_bpf: remove unrelevant help lines
[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 }
35
36 static void usage(void)
37 {
38 explain();
39 exit(-1);
40 }
41
42 static int parse_bpf(struct action_util *a, int *argc_p, char ***argv_p,
43 int tca_id, struct nlmsghdr *n)
44 {
45 int argc = *argc_p;
46 char **argv = *argv_p;
47 struct rtattr *tail;
48 struct tc_act_bpf parm = { 0 };
49 struct sock_filter bpf_ops[BPF_MAXINSNS];
50 __u16 bpf_len = 0;
51
52 if (matches(*argv, "bpf") != 0)
53 return -1;
54
55 NEXT_ARG();
56
57 while (argc > 0) {
58 if (matches(*argv, "run") == 0) {
59 bool from_file;
60 int ret;
61
62 NEXT_ARG();
63 if (strcmp(*argv, "bytecode-file") == 0) {
64 from_file = true;
65 } else if (strcmp(*argv, "bytecode") == 0) {
66 from_file = false;
67 } else {
68 fprintf(stderr, "unexpected \"%s\"\n", *argv);
69 explain();
70 return -1;
71 }
72 NEXT_ARG();
73 ret = bpf_parse_ops(argc, argv, bpf_ops, from_file);
74 if (ret < 0) {
75 fprintf(stderr, "Illegal \"bytecode\"\n");
76 return -1;
77 }
78 bpf_len = ret;
79 } else if (matches(*argv, "help") == 0) {
80 usage();
81 } else {
82 break;
83 }
84 argc--;
85 argv++;
86 }
87
88 parm.action = TC_ACT_PIPE;
89 if (argc) {
90 if (matches(*argv, "reclassify") == 0) {
91 parm.action = TC_ACT_RECLASSIFY;
92 NEXT_ARG();
93 } else if (matches(*argv, "pipe") == 0) {
94 parm.action = TC_ACT_PIPE;
95 NEXT_ARG();
96 } else if (matches(*argv, "drop") == 0 ||
97 matches(*argv, "shot") == 0) {
98 parm.action = TC_ACT_SHOT;
99 NEXT_ARG();
100 } else if (matches(*argv, "continue") == 0) {
101 parm.action = TC_ACT_UNSPEC;
102 NEXT_ARG();
103 } else if (matches(*argv, "pass") == 0) {
104 parm.action = TC_ACT_OK;
105 NEXT_ARG();
106 }
107 }
108
109 if (argc) {
110 if (matches(*argv, "index") == 0) {
111 NEXT_ARG();
112 if (get_u32(&parm.index, *argv, 10)) {
113 fprintf(stderr, "bpf: Illegal \"index\"\n");
114 return -1;
115 }
116 argc--;
117 argv++;
118 }
119 }
120
121 if (!bpf_len) {
122 fprintf(stderr, "bpf: Bytecode needs to be passed\n");
123 explain();
124 return -1;
125 }
126
127 tail = NLMSG_TAIL(n);
128 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
129 addattr_l(n, MAX_MSG, TCA_ACT_BPF_PARMS, &parm, sizeof(parm));
130 addattr16(n, MAX_MSG, TCA_ACT_BPF_OPS_LEN, bpf_len);
131 addattr_l(n, MAX_MSG, TCA_ACT_BPF_OPS, &bpf_ops,
132 bpf_len * sizeof(struct sock_filter));
133 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
134
135 *argc_p = argc;
136 *argv_p = argv;
137 return 0;
138 }
139
140 static int print_bpf(struct action_util *au, FILE *f, struct rtattr *arg)
141 {
142 struct rtattr *tb[TCA_ACT_BPF_MAX + 1];
143 struct tc_act_bpf *parm;
144
145 if (arg == NULL)
146 return -1;
147
148 parse_rtattr_nested(tb, TCA_ACT_BPF_MAX, arg);
149
150 if (!tb[TCA_ACT_BPF_PARMS]) {
151 fprintf(f, "[NULL bpf parameters]");
152 return -1;
153 }
154 parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]);
155
156 fprintf(f, " bpf ");
157
158 if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN])
159 bpf_print_ops(f, tb[TCA_ACT_BPF_OPS],
160 rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN]));
161
162 fprintf(f, "\n\tindex %d ref %d bind %d", parm->index, parm->refcnt,
163 parm->bindcnt);
164
165 if (show_stats) {
166 if (tb[TCA_ACT_BPF_TM]) {
167 struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]);
168 print_tm(f, tm);
169 }
170 }
171
172 fprintf(f, "\n ");
173
174 return 0;
175 }
176
177 struct action_util bpf_action_util = {
178 .id = "bpf",
179 .parse_aopt = parse_bpf,
180 .print_aopt = print_bpf,
181 };