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 struct nfp_bpf_map_word {
193 	unsigned char type		:4;
194 	unsigned char non_zero_update	:1;
195 };
196 
197 /**
198  * struct nfp_bpf_map - private per-map data attached to BPF maps for offload
199  * @offmap:	pointer to the offloaded BPF map
200  * @bpf:	back pointer to bpf app private structure
201  * @tid:	table id identifying map on datapath
202  * @l:		link on the nfp_app_bpf->map_list list
203  * @use_map:	map of how the value is used (in 4B chunks)
204  */
205 struct nfp_bpf_map {
206 	struct bpf_offloaded_map *offmap;
207 	struct nfp_app_bpf *bpf;
208 	u32 tid;
209 	struct list_head l;
210 	struct nfp_bpf_map_word use_map[];
211 };
212 
213 struct nfp_bpf_neutral_map {
214 	struct rhash_head l;
215 	struct bpf_map *ptr;
216 	u32 map_id;
217 	u32 count;
218 };
219 
220 extern const struct rhashtable_params nfp_bpf_maps_neutral_params;
221 
222 struct nfp_prog;
223 struct nfp_insn_meta;
224 typedef int (*instr_cb_t)(struct nfp_prog *, struct nfp_insn_meta *);
225 
226 #define nfp_prog_first_meta(nfp_prog)					\
227 	list_first_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l)
228 #define nfp_prog_last_meta(nfp_prog)					\
229 	list_last_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l)
230 #define nfp_meta_next(meta)	list_next_entry(meta, l)
231 #define nfp_meta_prev(meta)	list_prev_entry(meta, l)
232 
233 /**
234  * struct nfp_bpf_reg_state - register state for calls
235  * @reg: BPF register state from latest path
236  * @var_off: for stack arg - changes stack offset on different paths
237  */
238 struct nfp_bpf_reg_state {
239 	struct bpf_reg_state reg;
240 	bool var_off;
241 };
242 
243 #define FLAG_INSN_IS_JUMP_DST			BIT(0)
244 #define FLAG_INSN_IS_SUBPROG_START		BIT(1)
245 #define FLAG_INSN_PTR_CALLER_STACK_FRAME	BIT(2)
246 /* Instruction is pointless, noop even on its own */
247 #define FLAG_INSN_SKIP_NOOP			BIT(3)
248 /* Instruction is optimized out based on preceding instructions */
249 #define FLAG_INSN_SKIP_PREC_DEPENDENT		BIT(4)
250 
251 #define FLAG_INSN_SKIP_MASK		(FLAG_INSN_SKIP_NOOP | \
252 					 FLAG_INSN_SKIP_PREC_DEPENDENT)
253 
254 /**
255  * struct nfp_insn_meta - BPF instruction wrapper
256  * @insn: BPF instruction
257  * @ptr: pointer type for memory operations
258  * @ldst_gather_len: memcpy length gathered from load/store sequence
259  * @paired_st: the paired store insn at the head of the sequence
260  * @ptr_not_const: pointer is not always constant
261  * @pkt_cache: packet data cache information
262  * @pkt_cache.range_start: start offset for associated packet data cache
263  * @pkt_cache.range_end: end offset for associated packet data cache
264  * @pkt_cache.do_init: this read needs to initialize packet data cache
265  * @xadd_over_16bit: 16bit immediate is not guaranteed
266  * @xadd_maybe_16bit: 16bit immediate is possible
267  * @jmp_dst: destination info for jump instructions
268  * @jump_neg_op: jump instruction has inverted immediate, use ADD instead of SUB
269  * @num_insns_after_br: number of insns following a branch jump, used for fixup
270  * @func_id: function id for call instructions
271  * @arg1: arg1 for call instructions
272  * @arg2: arg2 for call instructions
273  * @umin_src: copy of core verifier umin_value for src opearnd.
274  * @umax_src: copy of core verifier umax_value for src operand.
275  * @umin_dst: copy of core verifier umin_value for dst opearnd.
276  * @umax_dst: copy of core verifier umax_value for dst operand.
277  * @off: index of first generated machine instruction (in nfp_prog.prog)
278  * @n: eBPF instruction number
279  * @flags: eBPF instruction extra optimization flags
280  * @subprog_idx: index of subprogram to which the instruction belongs
281  * @double_cb: callback for second part of the instruction
282  * @l: link on nfp_prog->insns list
283  */
284 struct nfp_insn_meta {
285 	struct bpf_insn insn;
286 	union {
287 		/* pointer ops (ld/st/xadd) */
288 		struct {
289 			struct bpf_reg_state ptr;
290 			struct bpf_insn *paired_st;
291 			s16 ldst_gather_len;
292 			bool ptr_not_const;
293 			struct {
294 				s16 range_start;
295 				s16 range_end;
296 				bool do_init;
297 			} pkt_cache;
298 			bool xadd_over_16bit;
299 			bool xadd_maybe_16bit;
300 		};
301 		/* jump */
302 		struct {
303 			struct nfp_insn_meta *jmp_dst;
304 			bool jump_neg_op;
305 			u32 num_insns_after_br; /* only for BPF-to-BPF calls */
306 		};
307 		/* function calls */
308 		struct {
309 			u32 func_id;
310 			struct bpf_reg_state arg1;
311 			struct nfp_bpf_reg_state arg2;
312 		};
313 		/* We are interested in range info for operands of ALU
314 		 * operations. For example, shift amount, multiplicand and
315 		 * multiplier etc.
316 		 */
317 		struct {
318 			u64 umin_src;
319 			u64 umax_src;
320 			u64 umin_dst;
321 			u64 umax_dst;
322 		};
323 	};
324 	unsigned int off;
325 	unsigned short n;
326 	unsigned short flags;
327 	unsigned short subprog_idx;
328 	instr_cb_t double_cb;
329 
330 	struct list_head l;
331 };
332 
333 #define BPF_SIZE_MASK	0x18
334 
335 static inline u8 mbpf_class(const struct nfp_insn_meta *meta)
336 {
337 	return BPF_CLASS(meta->insn.code);
338 }
339 
340 static inline u8 mbpf_src(const struct nfp_insn_meta *meta)
341 {
342 	return BPF_SRC(meta->insn.code);
343 }
344 
345 static inline u8 mbpf_op(const struct nfp_insn_meta *meta)
346 {
347 	return BPF_OP(meta->insn.code);
348 }
349 
350 static inline u8 mbpf_mode(const struct nfp_insn_meta *meta)
351 {
352 	return BPF_MODE(meta->insn.code);
353 }
354 
355 static inline bool is_mbpf_alu(const struct nfp_insn_meta *meta)
356 {
357 	return mbpf_class(meta) == BPF_ALU64 || mbpf_class(meta) == BPF_ALU;
358 }
359 
360 static inline bool is_mbpf_load(const struct nfp_insn_meta *meta)
361 {
362 	return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_LDX | BPF_MEM);
363 }
364 
365 static inline bool is_mbpf_store(const struct nfp_insn_meta *meta)
366 {
367 	return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_MEM);
368 }
369 
370 static inline bool is_mbpf_load_pkt(const struct nfp_insn_meta *meta)
371 {
372 	return is_mbpf_load(meta) && meta->ptr.type == PTR_TO_PACKET;
373 }
374 
375 static inline bool is_mbpf_store_pkt(const struct nfp_insn_meta *meta)
376 {
377 	return is_mbpf_store(meta) && meta->ptr.type == PTR_TO_PACKET;
378 }
379 
380 static inline bool is_mbpf_classic_load(const struct nfp_insn_meta *meta)
381 {
382 	u8 code = meta->insn.code;
383 
384 	return BPF_CLASS(code) == BPF_LD &&
385 	       (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND);
386 }
387 
388 static inline bool is_mbpf_classic_store(const struct nfp_insn_meta *meta)
389 {
390 	u8 code = meta->insn.code;
391 
392 	return BPF_CLASS(code) == BPF_ST && BPF_MODE(code) == BPF_MEM;
393 }
394 
395 static inline bool is_mbpf_classic_store_pkt(const struct nfp_insn_meta *meta)
396 {
397 	return is_mbpf_classic_store(meta) && meta->ptr.type == PTR_TO_PACKET;
398 }
399 
400 static inline bool is_mbpf_xadd(const struct nfp_insn_meta *meta)
401 {
402 	return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_XADD);
403 }
404 
405 static inline bool is_mbpf_mul(const struct nfp_insn_meta *meta)
406 {
407 	return is_mbpf_alu(meta) && mbpf_op(meta) == BPF_MUL;
408 }
409 
410 static inline bool is_mbpf_div(const struct nfp_insn_meta *meta)
411 {
412 	return is_mbpf_alu(meta) && mbpf_op(meta) == BPF_DIV;
413 }
414 
415 static inline bool is_mbpf_helper_call(const struct nfp_insn_meta *meta)
416 {
417 	struct bpf_insn insn = meta->insn;
418 
419 	return insn.code == (BPF_JMP | BPF_CALL) &&
420 		insn.src_reg != BPF_PSEUDO_CALL;
421 }
422 
423 static inline bool is_mbpf_pseudo_call(const struct nfp_insn_meta *meta)
424 {
425 	struct bpf_insn insn = meta->insn;
426 
427 	return insn.code == (BPF_JMP | BPF_CALL) &&
428 		insn.src_reg == BPF_PSEUDO_CALL;
429 }
430 
431 #define STACK_FRAME_ALIGN 64
432 
433 /**
434  * struct nfp_bpf_subprog_info - nfp BPF sub-program (a.k.a. function) info
435  * @stack_depth:	maximum stack depth used by this sub-program
436  * @needs_reg_push:	whether sub-program uses callee-saved registers
437  */
438 struct nfp_bpf_subprog_info {
439 	u16 stack_depth;
440 	u8 needs_reg_push : 1;
441 };
442 
443 /**
444  * struct nfp_prog - nfp BPF program
445  * @bpf: backpointer to the bpf app priv structure
446  * @prog: machine code
447  * @prog_len: number of valid instructions in @prog array
448  * @__prog_alloc_len: alloc size of @prog array
449  * @stack_size: total amount of stack used
450  * @verifier_meta: temporary storage for verifier's insn meta
451  * @type: BPF program type
452  * @last_bpf_off: address of the last instruction translated from BPF
453  * @tgt_out: jump target for normal exit
454  * @tgt_abort: jump target for abort (e.g. access outside of packet buffer)
455  * @tgt_call_push_regs: jump target for subroutine for saving R6~R9 to stack
456  * @tgt_call_pop_regs: jump target for subroutine used for restoring R6~R9
457  * @n_translated: number of successfully translated instructions (for errors)
458  * @error: error code if something went wrong
459  * @stack_frame_depth: max stack depth for current frame
460  * @adjust_head_location: if program has single adjust head call - the insn no.
461  * @map_records_cnt: the number of map pointers recorded for this prog
462  * @subprog_cnt: number of sub-programs, including main function
463  * @map_records: the map record pointers from bpf->maps_neutral
464  * @subprog: pointer to an array of objects holding info about sub-programs
465  * @n_insns: number of instructions on @insns list
466  * @insns: list of BPF instruction wrappers (struct nfp_insn_meta)
467  */
468 struct nfp_prog {
469 	struct nfp_app_bpf *bpf;
470 
471 	u64 *prog;
472 	unsigned int prog_len;
473 	unsigned int __prog_alloc_len;
474 
475 	unsigned int stack_size;
476 
477 	struct nfp_insn_meta *verifier_meta;
478 
479 	enum bpf_prog_type type;
480 
481 	unsigned int last_bpf_off;
482 	unsigned int tgt_out;
483 	unsigned int tgt_abort;
484 	unsigned int tgt_call_push_regs;
485 	unsigned int tgt_call_pop_regs;
486 
487 	unsigned int n_translated;
488 	int error;
489 
490 	unsigned int stack_frame_depth;
491 	unsigned int adjust_head_location;
492 
493 	unsigned int map_records_cnt;
494 	unsigned int subprog_cnt;
495 	struct nfp_bpf_neutral_map **map_records;
496 	struct nfp_bpf_subprog_info *subprog;
497 
498 	unsigned int n_insns;
499 	struct list_head insns;
500 };
501 
502 /**
503  * struct nfp_bpf_vnic - per-vNIC BPF priv structure
504  * @tc_prog:	currently loaded cls_bpf program
505  * @start_off:	address of the first instruction in the memory
506  * @tgt_done:	jump target to get the next packet
507  */
508 struct nfp_bpf_vnic {
509 	struct bpf_prog *tc_prog;
510 	unsigned int start_off;
511 	unsigned int tgt_done;
512 };
513 
514 bool nfp_is_subprog_start(struct nfp_insn_meta *meta);
515 void nfp_bpf_jit_prepare(struct nfp_prog *nfp_prog);
516 int nfp_bpf_jit(struct nfp_prog *prog);
517 bool nfp_bpf_supported_opcode(u8 code);
518 
519 int nfp_verify_insn(struct bpf_verifier_env *env, int insn_idx,
520 		    int prev_insn_idx);
521 int nfp_bpf_finalize(struct bpf_verifier_env *env);
522 
523 extern const struct bpf_prog_offload_ops nfp_bpf_dev_ops;
524 
525 struct netdev_bpf;
526 struct nfp_app;
527 struct nfp_net;
528 
529 int nfp_ndo_bpf(struct nfp_app *app, struct nfp_net *nn,
530 		struct netdev_bpf *bpf);
531 int nfp_net_bpf_offload(struct nfp_net *nn, struct bpf_prog *prog,
532 			bool old_prog, struct netlink_ext_ack *extack);
533 
534 struct nfp_insn_meta *
535 nfp_bpf_goto_meta(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
536 		  unsigned int insn_idx);
537 
538 void *nfp_bpf_relo_for_vnic(struct nfp_prog *nfp_prog, struct nfp_bpf_vnic *bv);
539 
540 unsigned int nfp_bpf_ctrl_cmsg_mtu(struct nfp_app_bpf *bpf);
541 long long int
542 nfp_bpf_ctrl_alloc_map(struct nfp_app_bpf *bpf, struct bpf_map *map);
543 void
544 nfp_bpf_ctrl_free_map(struct nfp_app_bpf *bpf, struct nfp_bpf_map *nfp_map);
545 int nfp_bpf_ctrl_getfirst_entry(struct bpf_offloaded_map *offmap,
546 				void *next_key);
547 int nfp_bpf_ctrl_update_entry(struct bpf_offloaded_map *offmap,
548 			      void *key, void *value, u64 flags);
549 int nfp_bpf_ctrl_del_entry(struct bpf_offloaded_map *offmap, void *key);
550 int nfp_bpf_ctrl_lookup_entry(struct bpf_offloaded_map *offmap,
551 			      void *key, void *value);
552 int nfp_bpf_ctrl_getnext_entry(struct bpf_offloaded_map *offmap,
553 			       void *key, void *next_key);
554 
555 int nfp_bpf_event_output(struct nfp_app_bpf *bpf, const void *data,
556 			 unsigned int len);
557 
558 void nfp_bpf_ctrl_msg_rx(struct nfp_app *app, struct sk_buff *skb);
559 void
560 nfp_bpf_ctrl_msg_rx_raw(struct nfp_app *app, const void *data,
561 			unsigned int len);
562 #endif
563