1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2016-2018 Netronome Systems, Inc. */
3 
4 #ifndef __NFP_BPF_H__
5 #define __NFP_BPF_H__ 1
6 
7 #include <linux/bitfield.h>
8 #include <linux/bpf.h>
9 #include <linux/bpf_verifier.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/rhashtable.h>
13 #include <linux/skbuff.h>
14 #include <linux/types.h>
15 #include <linux/wait.h>
16 
17 #include "../nfp_asm.h"
18 #include "fw.h"
19 
20 #define cmsg_warn(bpf, msg...)	nn_dp_warn(&(bpf)->app->ctrl->dp, msg)
21 
22 /* For relocation logic use up-most byte of branch instruction as scratch
23  * area.  Remember to clear this before sending instructions to HW!
24  */
25 #define OP_RELO_TYPE	0xff00000000000000ULL
26 
27 enum nfp_relo_type {
28 	RELO_NONE = 0,
29 	/* standard internal jumps */
30 	RELO_BR_REL,
31 	/* internal jumps to parts of the outro */
32 	RELO_BR_GO_OUT,
33 	RELO_BR_GO_ABORT,
34 	RELO_BR_GO_CALL_PUSH_REGS,
35 	RELO_BR_GO_CALL_POP_REGS,
36 	/* external jumps to fixed addresses */
37 	RELO_BR_NEXT_PKT,
38 	RELO_BR_HELPER,
39 	/* immediate relocation against load address */
40 	RELO_IMMED_REL,
41 };
42 
43 /* To make absolute relocated branches (branches other than RELO_BR_REL)
44  * distinguishable in user space dumps from normal jumps, add a large offset
45  * to them.
46  */
47 #define BR_OFF_RELO		15000
48 
49 enum static_regs {
50 	STATIC_REG_IMMA		= 20, /* Bank AB */
51 	STATIC_REG_IMM		= 21, /* Bank AB */
52 	STATIC_REG_STACK	= 22, /* Bank A */
53 	STATIC_REG_PKT_LEN	= 22, /* Bank B */
54 };
55 
56 enum pkt_vec {
57 	PKT_VEC_PKT_LEN		= 0,
58 	PKT_VEC_PKT_PTR		= 2,
59 	PKT_VEC_QSEL_SET	= 4,
60 	PKT_VEC_QSEL_VAL	= 6,
61 };
62 
63 #define PKT_VEL_QSEL_SET_BIT	4
64 
65 #define pv_len(np)	reg_lm(1, PKT_VEC_PKT_LEN)
66 #define pv_ctm_ptr(np)	reg_lm(1, PKT_VEC_PKT_PTR)
67 #define pv_qsel_set(np)	reg_lm(1, PKT_VEC_QSEL_SET)
68 #define pv_qsel_val(np)	reg_lm(1, PKT_VEC_QSEL_VAL)
69 
70 #define stack_reg(np)	reg_a(STATIC_REG_STACK)
71 #define stack_imm(np)	imm_b(np)
72 #define plen_reg(np)	reg_b(STATIC_REG_PKT_LEN)
73 #define pptr_reg(np)	pv_ctm_ptr(np)
74 #define imm_a(np)	reg_a(STATIC_REG_IMM)
75 #define imm_b(np)	reg_b(STATIC_REG_IMM)
76 #define imma_a(np)	reg_a(STATIC_REG_IMMA)
77 #define imma_b(np)	reg_b(STATIC_REG_IMMA)
78 #define imm_both(np)	reg_both(STATIC_REG_IMM)
79 #define ret_reg(np)	imm_a(np)
80 
81 #define NFP_BPF_ABI_FLAGS	reg_imm(0)
82 #define   NFP_BPF_ABI_FLAG_MARK	1
83 
84 /**
85  * struct nfp_app_bpf - bpf app priv structure
86  * @app:		backpointer to the app
87  *
88  * @bpf_dev:		BPF offload device handle
89  *
90  * @tag_allocator:	bitmap of control message tags in use
91  * @tag_alloc_next:	next tag bit to allocate
92  * @tag_alloc_last:	next tag bit to be freed
93  *
94  * @cmsg_replies:	received cmsg replies waiting to be consumed
95  * @cmsg_wq:		work queue for waiting for cmsg replies
96  *
97  * @cmsg_key_sz:	size of key in cmsg element array
98  * @cmsg_val_sz:	size of value in cmsg element array
99  *
100  * @map_list:		list of offloaded maps
101  * @maps_in_use:	number of currently offloaded maps
102  * @map_elems_in_use:	number of elements allocated to offloaded maps
103  *
104  * @maps_neutral:	hash table of offload-neutral maps (on pointer)
105  *
106  * @abi_version:	global BPF ABI version
107  *
108  * @adjust_head:	adjust head capability
109  * @adjust_head.flags:		extra flags for adjust head
110  * @adjust_head.off_min:	minimal packet offset within buffer required
111  * @adjust_head.off_max:	maximum packet offset within buffer required
112  * @adjust_head.guaranteed_sub:	negative adjustment guaranteed possible
113  * @adjust_head.guaranteed_add:	positive adjustment guaranteed possible
114  *
115  * @maps:		map capability
116  * @maps.types:			supported map types
117  * @maps.max_maps:		max number of maps supported
118  * @maps.max_elems:		max number of entries in each map
119  * @maps.max_key_sz:		max size of map key
120  * @maps.max_val_sz:		max size of map value
121  * @maps.max_elem_sz:		max size of map entry (key + value)
122  *
123  * @helpers:		helper addressess for various calls
124  * @helpers.map_lookup:		map lookup helper address
125  * @helpers.map_update:		map update helper address
126  * @helpers.map_delete:		map delete helper address
127  * @helpers.perf_event_output:	output perf event to a ring buffer
128  *
129  * @pseudo_random:	FW initialized the pseudo-random machinery (CSRs)
130  * @queue_select:	BPF can set the RX queue ID in packet vector
131  * @adjust_tail:	BPF can simply trunc packet size for adjust tail
132  */
133 struct nfp_app_bpf {
134 	struct nfp_app *app;
135 
136 	struct bpf_offload_dev *bpf_dev;
137 
138 	DECLARE_BITMAP(tag_allocator, U16_MAX + 1);
139 	u16 tag_alloc_next;
140 	u16 tag_alloc_last;
141 
142 	struct sk_buff_head cmsg_replies;
143 	struct wait_queue_head cmsg_wq;
144 
145 	unsigned int cmsg_key_sz;
146 	unsigned int cmsg_val_sz;
147 
148 	struct list_head map_list;
149 	unsigned int maps_in_use;
150 	unsigned int map_elems_in_use;
151 
152 	struct rhashtable maps_neutral;
153 
154 	u32 abi_version;
155 
156 	struct nfp_bpf_cap_adjust_head {
157 		u32 flags;
158 		int off_min;
159 		int off_max;
160 		int guaranteed_sub;
161 		int guaranteed_add;
162 	} adjust_head;
163 
164 	struct {
165 		u32 types;
166 		u32 max_maps;
167 		u32 max_elems;
168 		u32 max_key_sz;
169 		u32 max_val_sz;
170 		u32 max_elem_sz;
171 	} maps;
172 
173 	struct {
174 		u32 map_lookup;
175 		u32 map_update;
176 		u32 map_delete;
177 		u32 perf_event_output;
178 	} helpers;
179 
180 	bool pseudo_random;
181 	bool queue_select;
182 	bool adjust_tail;
183 };
184 
185 enum nfp_bpf_map_use {
186 	NFP_MAP_UNUSED = 0,
187 	NFP_MAP_USE_READ,
188 	NFP_MAP_USE_WRITE,
189 	NFP_MAP_USE_ATOMIC_CNT,
190 };
191 
192 /**
193  * struct nfp_bpf_map - private per-map data attached to BPF maps for offload
194  * @offmap:	pointer to the offloaded BPF map
195  * @bpf:	back pointer to bpf app private structure
196  * @tid:	table id identifying map on datapath
197  * @l:		link on the nfp_app_bpf->map_list list
198  * @use_map:	map of how the value is used (in 4B chunks)
199  */
200 struct nfp_bpf_map {
201 	struct bpf_offloaded_map *offmap;
202 	struct nfp_app_bpf *bpf;
203 	u32 tid;
204 	struct list_head l;
205 	enum nfp_bpf_map_use use_map[];
206 };
207 
208 struct nfp_bpf_neutral_map {
209 	struct rhash_head l;
210 	struct bpf_map *ptr;
211 	u32 map_id;
212 	u32 count;
213 };
214 
215 extern const struct rhashtable_params nfp_bpf_maps_neutral_params;
216 
217 struct nfp_prog;
218 struct nfp_insn_meta;
219 typedef int (*instr_cb_t)(struct nfp_prog *, struct nfp_insn_meta *);
220 
221 #define nfp_prog_first_meta(nfp_prog)					\
222 	list_first_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l)
223 #define nfp_prog_last_meta(nfp_prog)					\
224 	list_last_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l)
225 #define nfp_meta_next(meta)	list_next_entry(meta, l)
226 #define nfp_meta_prev(meta)	list_prev_entry(meta, l)
227 
228 /**
229  * struct nfp_bpf_reg_state - register state for calls
230  * @reg: BPF register state from latest path
231  * @var_off: for stack arg - changes stack offset on different paths
232  */
233 struct nfp_bpf_reg_state {
234 	struct bpf_reg_state reg;
235 	bool var_off;
236 };
237 
238 #define FLAG_INSN_IS_JUMP_DST			BIT(0)
239 #define FLAG_INSN_IS_SUBPROG_START		BIT(1)
240 #define FLAG_INSN_PTR_CALLER_STACK_FRAME	BIT(2)
241 
242 /**
243  * struct nfp_insn_meta - BPF instruction wrapper
244  * @insn: BPF instruction
245  * @ptr: pointer type for memory operations
246  * @ldst_gather_len: memcpy length gathered from load/store sequence
247  * @paired_st: the paired store insn at the head of the sequence
248  * @ptr_not_const: pointer is not always constant
249  * @pkt_cache: packet data cache information
250  * @pkt_cache.range_start: start offset for associated packet data cache
251  * @pkt_cache.range_end: end offset for associated packet data cache
252  * @pkt_cache.do_init: this read needs to initialize packet data cache
253  * @xadd_over_16bit: 16bit immediate is not guaranteed
254  * @xadd_maybe_16bit: 16bit immediate is possible
255  * @jmp_dst: destination info for jump instructions
256  * @jump_neg_op: jump instruction has inverted immediate, use ADD instead of SUB
257  * @num_insns_after_br: number of insns following a branch jump, used for fixup
258  * @func_id: function id for call instructions
259  * @arg1: arg1 for call instructions
260  * @arg2: arg2 for call instructions
261  * @umin_src: copy of core verifier umin_value for src opearnd.
262  * @umax_src: copy of core verifier umax_value for src operand.
263  * @umin_dst: copy of core verifier umin_value for dst opearnd.
264  * @umax_dst: copy of core verifier umax_value for dst operand.
265  * @off: index of first generated machine instruction (in nfp_prog.prog)
266  * @n: eBPF instruction number
267  * @flags: eBPF instruction extra optimization flags
268  * @subprog_idx: index of subprogram to which the instruction belongs
269  * @skip: skip this instruction (optimized out)
270  * @double_cb: callback for second part of the instruction
271  * @l: link on nfp_prog->insns list
272  */
273 struct nfp_insn_meta {
274 	struct bpf_insn insn;
275 	union {
276 		/* pointer ops (ld/st/xadd) */
277 		struct {
278 			struct bpf_reg_state ptr;
279 			struct bpf_insn *paired_st;
280 			s16 ldst_gather_len;
281 			bool ptr_not_const;
282 			struct {
283 				s16 range_start;
284 				s16 range_end;
285 				bool do_init;
286 			} pkt_cache;
287 			bool xadd_over_16bit;
288 			bool xadd_maybe_16bit;
289 		};
290 		/* jump */
291 		struct {
292 			struct nfp_insn_meta *jmp_dst;
293 			bool jump_neg_op;
294 			u32 num_insns_after_br; /* only for BPF-to-BPF calls */
295 		};
296 		/* function calls */
297 		struct {
298 			u32 func_id;
299 			struct bpf_reg_state arg1;
300 			struct nfp_bpf_reg_state arg2;
301 		};
302 		/* We are interested in range info for operands of ALU
303 		 * operations. For example, shift amount, multiplicand and
304 		 * multiplier etc.
305 		 */
306 		struct {
307 			u64 umin_src;
308 			u64 umax_src;
309 			u64 umin_dst;
310 			u64 umax_dst;
311 		};
312 	};
313 	unsigned int off;
314 	unsigned short n;
315 	unsigned short flags;
316 	unsigned short subprog_idx;
317 	bool skip;
318 	instr_cb_t double_cb;
319 
320 	struct list_head l;
321 };
322 
323 #define BPF_SIZE_MASK	0x18
324 
325 static inline u8 mbpf_class(const struct nfp_insn_meta *meta)
326 {
327 	return BPF_CLASS(meta->insn.code);
328 }
329 
330 static inline u8 mbpf_src(const struct nfp_insn_meta *meta)
331 {
332 	return BPF_SRC(meta->insn.code);
333 }
334 
335 static inline u8 mbpf_op(const struct nfp_insn_meta *meta)
336 {
337 	return BPF_OP(meta->insn.code);
338 }
339 
340 static inline u8 mbpf_mode(const struct nfp_insn_meta *meta)
341 {
342 	return BPF_MODE(meta->insn.code);
343 }
344 
345 static inline bool is_mbpf_alu(const struct nfp_insn_meta *meta)
346 {
347 	return mbpf_class(meta) == BPF_ALU64 || mbpf_class(meta) == BPF_ALU;
348 }
349 
350 static inline bool is_mbpf_load(const struct nfp_insn_meta *meta)
351 {
352 	return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_LDX | BPF_MEM);
353 }
354 
355 static inline bool is_mbpf_store(const struct nfp_insn_meta *meta)
356 {
357 	return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_MEM);
358 }
359 
360 static inline bool is_mbpf_load_pkt(const struct nfp_insn_meta *meta)
361 {
362 	return is_mbpf_load(meta) && meta->ptr.type == PTR_TO_PACKET;
363 }
364 
365 static inline bool is_mbpf_store_pkt(const struct nfp_insn_meta *meta)
366 {
367 	return is_mbpf_store(meta) && meta->ptr.type == PTR_TO_PACKET;
368 }
369 
370 static inline bool is_mbpf_classic_load(const struct nfp_insn_meta *meta)
371 {
372 	u8 code = meta->insn.code;
373 
374 	return BPF_CLASS(code) == BPF_LD &&
375 	       (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND);
376 }
377 
378 static inline bool is_mbpf_classic_store(const struct nfp_insn_meta *meta)
379 {
380 	u8 code = meta->insn.code;
381 
382 	return BPF_CLASS(code) == BPF_ST && BPF_MODE(code) == BPF_MEM;
383 }
384 
385 static inline bool is_mbpf_classic_store_pkt(const struct nfp_insn_meta *meta)
386 {
387 	return is_mbpf_classic_store(meta) && meta->ptr.type == PTR_TO_PACKET;
388 }
389 
390 static inline bool is_mbpf_xadd(const struct nfp_insn_meta *meta)
391 {
392 	return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_XADD);
393 }
394 
395 static inline bool is_mbpf_mul(const struct nfp_insn_meta *meta)
396 {
397 	return is_mbpf_alu(meta) && mbpf_op(meta) == BPF_MUL;
398 }
399 
400 static inline bool is_mbpf_div(const struct nfp_insn_meta *meta)
401 {
402 	return is_mbpf_alu(meta) && mbpf_op(meta) == BPF_DIV;
403 }
404 
405 static inline bool is_mbpf_helper_call(const struct nfp_insn_meta *meta)
406 {
407 	struct bpf_insn insn = meta->insn;
408 
409 	return insn.code == (BPF_JMP | BPF_CALL) &&
410 		insn.src_reg != BPF_PSEUDO_CALL;
411 }
412 
413 static inline bool is_mbpf_pseudo_call(const struct nfp_insn_meta *meta)
414 {
415 	struct bpf_insn insn = meta->insn;
416 
417 	return insn.code == (BPF_JMP | BPF_CALL) &&
418 		insn.src_reg == BPF_PSEUDO_CALL;
419 }
420 
421 #define STACK_FRAME_ALIGN 64
422 
423 /**
424  * struct nfp_bpf_subprog_info - nfp BPF sub-program (a.k.a. function) info
425  * @stack_depth:	maximum stack depth used by this sub-program
426  * @needs_reg_push:	whether sub-program uses callee-saved registers
427  */
428 struct nfp_bpf_subprog_info {
429 	u16 stack_depth;
430 	u8 needs_reg_push : 1;
431 };
432 
433 /**
434  * struct nfp_prog - nfp BPF program
435  * @bpf: backpointer to the bpf app priv structure
436  * @prog: machine code
437  * @prog_len: number of valid instructions in @prog array
438  * @__prog_alloc_len: alloc size of @prog array
439  * @verifier_meta: temporary storage for verifier's insn meta
440  * @type: BPF program type
441  * @last_bpf_off: address of the last instruction translated from BPF
442  * @tgt_out: jump target for normal exit
443  * @tgt_abort: jump target for abort (e.g. access outside of packet buffer)
444  * @tgt_call_push_regs: jump target for subroutine for saving R6~R9 to stack
445  * @tgt_call_pop_regs: jump target for subroutine used for restoring R6~R9
446  * @n_translated: number of successfully translated instructions (for errors)
447  * @error: error code if something went wrong
448  * @stack_frame_depth: max stack depth for current frame
449  * @adjust_head_location: if program has single adjust head call - the insn no.
450  * @map_records_cnt: the number of map pointers recorded for this prog
451  * @subprog_cnt: number of sub-programs, including main function
452  * @map_records: the map record pointers from bpf->maps_neutral
453  * @subprog: pointer to an array of objects holding info about sub-programs
454  * @insns: list of BPF instruction wrappers (struct nfp_insn_meta)
455  */
456 struct nfp_prog {
457 	struct nfp_app_bpf *bpf;
458 
459 	u64 *prog;
460 	unsigned int prog_len;
461 	unsigned int __prog_alloc_len;
462 
463 	struct nfp_insn_meta *verifier_meta;
464 
465 	enum bpf_prog_type type;
466 
467 	unsigned int last_bpf_off;
468 	unsigned int tgt_out;
469 	unsigned int tgt_abort;
470 	unsigned int tgt_call_push_regs;
471 	unsigned int tgt_call_pop_regs;
472 
473 	unsigned int n_translated;
474 	int error;
475 
476 	unsigned int stack_frame_depth;
477 	unsigned int adjust_head_location;
478 
479 	unsigned int map_records_cnt;
480 	unsigned int subprog_cnt;
481 	struct nfp_bpf_neutral_map **map_records;
482 	struct nfp_bpf_subprog_info *subprog;
483 
484 	struct list_head insns;
485 };
486 
487 /**
488  * struct nfp_bpf_vnic - per-vNIC BPF priv structure
489  * @tc_prog:	currently loaded cls_bpf program
490  * @start_off:	address of the first instruction in the memory
491  * @tgt_done:	jump target to get the next packet
492  */
493 struct nfp_bpf_vnic {
494 	struct bpf_prog *tc_prog;
495 	unsigned int start_off;
496 	unsigned int tgt_done;
497 };
498 
499 bool nfp_is_subprog_start(struct nfp_insn_meta *meta);
500 void nfp_bpf_jit_prepare(struct nfp_prog *nfp_prog, unsigned int cnt);
501 int nfp_bpf_jit(struct nfp_prog *prog);
502 bool nfp_bpf_supported_opcode(u8 code);
503 
504 extern const struct bpf_prog_offload_ops nfp_bpf_analyzer_ops;
505 
506 struct netdev_bpf;
507 struct nfp_app;
508 struct nfp_net;
509 
510 int nfp_ndo_bpf(struct nfp_app *app, struct nfp_net *nn,
511 		struct netdev_bpf *bpf);
512 int nfp_net_bpf_offload(struct nfp_net *nn, struct bpf_prog *prog,
513 			bool old_prog, struct netlink_ext_ack *extack);
514 
515 struct nfp_insn_meta *
516 nfp_bpf_goto_meta(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
517 		  unsigned int insn_idx, unsigned int n_insns);
518 
519 void *nfp_bpf_relo_for_vnic(struct nfp_prog *nfp_prog, struct nfp_bpf_vnic *bv);
520 
521 unsigned int nfp_bpf_ctrl_cmsg_mtu(struct nfp_app_bpf *bpf);
522 long long int
523 nfp_bpf_ctrl_alloc_map(struct nfp_app_bpf *bpf, struct bpf_map *map);
524 void
525 nfp_bpf_ctrl_free_map(struct nfp_app_bpf *bpf, struct nfp_bpf_map *nfp_map);
526 int nfp_bpf_ctrl_getfirst_entry(struct bpf_offloaded_map *offmap,
527 				void *next_key);
528 int nfp_bpf_ctrl_update_entry(struct bpf_offloaded_map *offmap,
529 			      void *key, void *value, u64 flags);
530 int nfp_bpf_ctrl_del_entry(struct bpf_offloaded_map *offmap, void *key);
531 int nfp_bpf_ctrl_lookup_entry(struct bpf_offloaded_map *offmap,
532 			      void *key, void *value);
533 int nfp_bpf_ctrl_getnext_entry(struct bpf_offloaded_map *offmap,
534 			       void *key, void *next_key);
535 
536 int nfp_bpf_event_output(struct nfp_app_bpf *bpf, const void *data,
537 			 unsigned int len);
538 
539 void nfp_bpf_ctrl_msg_rx(struct nfp_app *app, struct sk_buff *skb);
540 void
541 nfp_bpf_ctrl_msg_rx_raw(struct nfp_app *app, const void *data,
542 			unsigned int len);
543 #endif
544