2 * m_bpf.c BPF based action module
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.
9 * Authors: Jiri Pirko <jiri@resnulli.us>
10 * Daniel Borkmann <daniel@iogearbox.net>
16 #include <linux/bpf.h>
17 #include <linux/tc_act/tc_bpf.h>
24 static const enum bpf_prog_type bpf_type
= BPF_PROG_TYPE_SCHED_ACT
;
26 static void explain(void)
29 "Usage: ... bpf ... [ index INDEX ]\n"
32 " bytecode BPF_BYTECODE\n"
33 " bytecode-file FILE\n"
36 " object-file FILE [ section ACT_NAME ] [ export UDS_FILE ]"
38 " object-pinned FILE\n"
40 "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n"
41 "c,t,f,k and s are decimals; s denotes number of 4-tuples\n"
43 "Where FILE points to a file containing the BPF_BYTECODE string,\n"
44 "an ELF file containing eBPF map definitions and bytecode, or a\n"
45 "pinned eBPF program.\n"
47 "Where ACT_NAME refers to the section name containing the\n"
48 "action (default \'%s\').\n"
50 "Where UDS_FILE points to a unix domain socket file in order\n"
51 "to hand off control of all created eBPF maps to an agent.\n"
53 "Where optionally INDEX points to an existing action, or\n"
54 "explicitly specifies an action index upon creation.\n",
55 bpf_prog_to_default_section(bpf_type
));
58 static void bpf_cbpf_cb(void *nl
, const struct sock_filter
*ops
, int ops_len
)
60 addattr16(nl
, MAX_MSG
, TCA_ACT_BPF_OPS_LEN
, ops_len
);
61 addattr_l(nl
, MAX_MSG
, TCA_ACT_BPF_OPS
, ops
,
62 ops_len
* sizeof(struct sock_filter
));
65 static void bpf_ebpf_cb(void *nl
, int fd
, const char *annotation
)
67 addattr32(nl
, MAX_MSG
, TCA_ACT_BPF_FD
, fd
);
68 addattrstrz(nl
, MAX_MSG
, TCA_ACT_BPF_NAME
, annotation
);
71 static const struct bpf_cfg_ops bpf_cb_ops
= {
72 .cbpf_cb
= bpf_cbpf_cb
,
73 .ebpf_cb
= bpf_ebpf_cb
,
76 static int bpf_parse_opt(struct action_util
*a
, int *ptr_argc
, char ***ptr_argv
,
77 int tca_id
, struct nlmsghdr
*n
)
79 const char *bpf_obj
= NULL
, *bpf_uds_name
= NULL
;
80 struct tc_act_bpf parm
= {};
81 struct bpf_cfg_in cfg
= {};
82 bool seen_run
= false;
90 if (matches(*argv
, "bpf") != 0)
95 tail
= addattr_nest(n
, MAX_MSG
, tca_id
);
98 if (matches(*argv
, "run") == 0) {
102 duparg("run", *argv
);
109 if (bpf_parse_and_load_common(&cfg
, &bpf_cb_ops
, n
))
115 bpf_obj
= cfg
.object
;
116 bpf_uds_name
= cfg
.uds
;
117 } else if (matches(*argv
, "help") == 0) {
120 } else if (matches(*argv
, "index") == 0) {
131 parse_action_control_dflt(&argc
, &argv
, &parm
.action
,
135 if (matches(*argv
, "index") == 0) {
137 if (get_u32(&parm
.index
, *argv
, 10)) {
138 fprintf(stderr
, "bpf: Illegal \"index\"\n");
146 addattr_l(n
, MAX_MSG
, TCA_ACT_BPF_PARMS
, &parm
, sizeof(parm
));
147 addattr_nest_end(n
, tail
);
150 ret
= bpf_send_map_fds(bpf_uds_name
, bpf_obj
);
158 static int bpf_print_opt(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
160 struct rtattr
*tb
[TCA_ACT_BPF_MAX
+ 1];
161 struct tc_act_bpf
*parm
;
167 parse_rtattr_nested(tb
, TCA_ACT_BPF_MAX
, arg
);
169 if (!tb
[TCA_ACT_BPF_PARMS
]) {
170 fprintf(f
, "[NULL bpf parameters]");
174 parm
= RTA_DATA(tb
[TCA_ACT_BPF_PARMS
]);
175 print_string(PRINT_ANY
, "kind", "%s ", "bpf");
177 if (tb
[TCA_ACT_BPF_NAME
])
178 print_string(PRINT_ANY
, "bpf_name", "%s ",
179 rta_getattr_str(tb
[TCA_ACT_BPF_NAME
]));
180 if (tb
[TCA_ACT_BPF_OPS
] && tb
[TCA_ACT_BPF_OPS_LEN
]) {
181 bpf_print_ops(tb
[TCA_ACT_BPF_OPS
],
182 rta_getattr_u16(tb
[TCA_ACT_BPF_OPS_LEN
]));
183 print_string(PRINT_FP
, NULL
, "%s", " ");
186 if (tb
[TCA_ACT_BPF_ID
])
187 d_ok
= bpf_dump_prog_info(f
,
188 rta_getattr_u32(tb
[TCA_ACT_BPF_ID
]));
189 if (!d_ok
&& tb
[TCA_ACT_BPF_TAG
]) {
192 print_string(PRINT_ANY
, "tag", "tag %s ",
193 hexstring_n2a(RTA_DATA(tb
[TCA_ACT_BPF_TAG
]),
194 RTA_PAYLOAD(tb
[TCA_ACT_BPF_TAG
]),
198 print_action_control(f
, "default-action ", parm
->action
, _SL_
);
199 print_uint(PRINT_ANY
, "index", "\t index %u", parm
->index
);
200 print_int(PRINT_ANY
, "ref", " ref %d", parm
->refcnt
);
201 print_int(PRINT_ANY
, "bind", " bind %d", parm
->bindcnt
);
204 if (tb
[TCA_ACT_BPF_TM
]) {
205 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_ACT_BPF_TM
]);
215 struct action_util bpf_action_util
= {
217 .parse_aopt
= bpf_parse_opt
,
218 .print_aopt
= bpf_print_opt
,