2 * f_bpf.c BPF-based Classifier
4 * This program is free software; you can distribute 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.
9 * Authors: Daniel Borkmann <dborkman@redhat.com>
15 #include <linux/bpf.h>
21 static const enum bpf_prog_type bpf_type
= BPF_PROG_TYPE_SCHED_CLS
;
23 static const int nla_tbl
[BPF_NLA_MAX
] = {
24 [BPF_NLA_OPS_LEN
] = TCA_BPF_OPS_LEN
,
25 [BPF_NLA_OPS
] = TCA_BPF_OPS
,
26 [BPF_NLA_FD
] = TCA_BPF_FD
,
27 [BPF_NLA_NAME
] = TCA_BPF_NAME
,
30 static void explain(void)
32 fprintf(stderr
, "Usage: ... bpf ...\n");
33 fprintf(stderr
, "\n");
34 fprintf(stderr
, "BPF use case:\n");
35 fprintf(stderr
, " bytecode BPF_BYTECODE\n");
36 fprintf(stderr
, " bytecode-file FILE\n");
37 fprintf(stderr
, "\n");
38 fprintf(stderr
, "eBPF use case:\n");
39 fprintf(stderr
, " object-file FILE [ section CLS_NAME ] [ export UDS_FILE ]");
40 fprintf(stderr
, " [ verbose ] [ direct-action ]\n");
41 fprintf(stderr
, " object-pinned FILE [ direct-action ]\n");
42 fprintf(stderr
, "\n");
43 fprintf(stderr
, "Common remaining options:\n");
44 fprintf(stderr
, " [ action ACTION_SPEC ]\n");
45 fprintf(stderr
, " [ classid CLASSID ]\n");
46 fprintf(stderr
, "\n");
47 fprintf(stderr
, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n");
48 fprintf(stderr
, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n");
49 fprintf(stderr
, "\n");
50 fprintf(stderr
, "Where FILE points to a file containing the BPF_BYTECODE string,\n");
51 fprintf(stderr
, "an ELF file containing eBPF map definitions and bytecode, or a\n");
52 fprintf(stderr
, "pinned eBPF program.\n");
53 fprintf(stderr
, "\n");
54 fprintf(stderr
, "Where CLS_NAME refers to the section name containing the\n");
55 fprintf(stderr
, "classifier (default \'%s\').\n", bpf_default_section(bpf_type
));
56 fprintf(stderr
, "\n");
57 fprintf(stderr
, "Where UDS_FILE points to a unix domain socket file in order\n");
58 fprintf(stderr
, "to hand off control of all created eBPF maps to an agent.\n");
59 fprintf(stderr
, "\n");
60 fprintf(stderr
, "ACTION_SPEC := ... look at individual actions\n");
61 fprintf(stderr
, "NOTE: CLASSID is parsed as hexadecimal input.\n");
64 static int bpf_parse_opt(struct filter_util
*qu
, char *handle
,
65 int argc
, char **argv
, struct nlmsghdr
*n
)
67 const char *bpf_obj
= NULL
, *bpf_uds_name
= NULL
;
68 struct tcmsg
*t
= NLMSG_DATA(n
);
69 unsigned int bpf_flags
= 0;
70 bool seen_run
= false;
78 if (get_u32(&t
->tcm_handle
, handle
, 0)) {
79 fprintf(stderr
, "Illegal \"handle\"\n");
84 tail
= (struct rtattr
*)(((void *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
85 addattr_l(n
, MAX_MSG
, TCA_OPTIONS
, NULL
, 0);
88 if (matches(*argv
, "run") == 0) {
92 if (bpf_parse_common(&argc
, &argv
, nla_tbl
, bpf_type
,
93 &bpf_obj
, &bpf_uds_name
, n
)) {
94 fprintf(stderr
, "Failed to retrieve (e)BPF data!\n");
97 } else if (matches(*argv
, "classid") == 0 ||
98 matches(*argv
, "flowid") == 0) {
102 if (get_tc_classid(&handle
, *argv
)) {
103 fprintf(stderr
, "Illegal \"classid\"\n");
106 addattr32(n
, MAX_MSG
, TCA_BPF_CLASSID
, handle
);
107 } else if (matches(*argv
, "direct-action") == 0 ||
108 matches(*argv
, "da") == 0) {
109 bpf_flags
|= TCA_BPF_FLAG_ACT_DIRECT
;
110 } else if (matches(*argv
, "action") == 0) {
112 if (parse_action(&argc
, &argv
, TCA_BPF_ACT
, n
)) {
113 fprintf(stderr
, "Illegal \"action\"\n");
117 } else if (matches(*argv
, "police") == 0) {
119 if (parse_police(&argc
, &argv
, TCA_BPF_POLICE
, n
)) {
120 fprintf(stderr
, "Illegal \"police\"\n");
124 } else if (matches(*argv
, "help") == 0) {
131 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
139 if (bpf_obj
&& bpf_flags
)
140 addattr32(n
, MAX_MSG
, TCA_BPF_FLAGS
, bpf_flags
);
142 tail
->rta_len
= (((void *)n
) + n
->nlmsg_len
) - (void *)tail
;
145 ret
= bpf_send_map_fds(bpf_uds_name
, bpf_obj
);
150 static int bpf_print_opt(struct filter_util
*qu
, FILE *f
,
151 struct rtattr
*opt
, __u32 handle
)
153 struct rtattr
*tb
[TCA_BPF_MAX
+ 1];
158 parse_rtattr_nested(tb
, TCA_BPF_MAX
, opt
);
161 fprintf(f
, "handle 0x%x ", handle
);
163 if (tb
[TCA_BPF_CLASSID
]) {
165 fprintf(f
, "flowid %s ",
166 sprint_tc_classid(rta_getattr_u32(tb
[TCA_BPF_CLASSID
]), b1
));
169 if (tb
[TCA_BPF_NAME
])
170 fprintf(f
, "%s ", rta_getattr_str(tb
[TCA_BPF_NAME
]));
171 else if (tb
[TCA_BPF_FD
])
172 fprintf(f
, "pfd %u ", rta_getattr_u32(tb
[TCA_BPF_FD
]));
174 if (tb
[TCA_BPF_FLAGS
]) {
175 unsigned int flags
= rta_getattr_u32(tb
[TCA_BPF_FLAGS
]);
177 if (flags
& TCA_BPF_FLAG_ACT_DIRECT
)
178 fprintf(f
, "direct-action ");
181 if (tb
[TCA_BPF_OPS
] && tb
[TCA_BPF_OPS_LEN
]) {
182 bpf_print_ops(f
, tb
[TCA_BPF_OPS
],
183 rta_getattr_u16(tb
[TCA_BPF_OPS_LEN
]));
187 if (tb
[TCA_BPF_POLICE
]) {
189 tc_print_police(f
, tb
[TCA_BPF_POLICE
]);
192 if (tb
[TCA_BPF_ACT
]) {
193 tc_print_action(f
, tb
[TCA_BPF_ACT
]);
199 struct filter_util bpf_filter_util
= {
201 .parse_fopt
= bpf_parse_opt
,
202 .print_fopt
= bpf_print_opt
,