1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2016-2018 Netronome Systems, Inc. */
5 #define __NFP_BPF_H__ 1
7 #include <linux/bitfield.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>
18 #include "../nfp_asm.h"
21 #define cmsg_warn(bpf, msg...) nn_dp_warn(&(bpf)->app->ctrl->dp, msg)
23 /* For relocation logic use up-most byte of branch instruction as scratch
24 * area. Remember to clear this before sending instructions to HW!
26 #define OP_RELO_TYPE 0xff00000000000000ULL
30 /* standard internal jumps */
32 /* internal jumps to parts of the outro */
35 RELO_BR_GO_CALL_PUSH_REGS,
36 RELO_BR_GO_CALL_POP_REGS,
37 /* external jumps to fixed addresses */
40 /* immediate relocation against load address */
44 /* To make absolute relocated branches (branches other than RELO_BR_REL)
45 * distinguishable in user space dumps from normal jumps, add a large offset
48 #define BR_OFF_RELO 15000
51 STATIC_REG_IMMA = 20, /* Bank AB */
52 STATIC_REG_IMM = 21, /* Bank AB */
53 STATIC_REG_STACK = 22, /* Bank A */
54 STATIC_REG_PKT_LEN = 22, /* Bank B */
64 #define PKT_VEL_QSEL_SET_BIT 4
66 #define pv_len(np) reg_lm(1, PKT_VEC_PKT_LEN)
67 #define pv_ctm_ptr(np) reg_lm(1, PKT_VEC_PKT_PTR)
68 #define pv_qsel_set(np) reg_lm(1, PKT_VEC_QSEL_SET)
69 #define pv_qsel_val(np) reg_lm(1, PKT_VEC_QSEL_VAL)
71 #define stack_reg(np) reg_a(STATIC_REG_STACK)
72 #define stack_imm(np) imm_b(np)
73 #define plen_reg(np) reg_b(STATIC_REG_PKT_LEN)
74 #define pptr_reg(np) pv_ctm_ptr(np)
75 #define imm_a(np) reg_a(STATIC_REG_IMM)
76 #define imm_b(np) reg_b(STATIC_REG_IMM)
77 #define imma_a(np) reg_a(STATIC_REG_IMMA)
78 #define imma_b(np) reg_b(STATIC_REG_IMMA)
79 #define imm_both(np) reg_both(STATIC_REG_IMM)
80 #define ret_reg(np) imm_a(np)
82 #define NFP_BPF_ABI_FLAGS reg_imm(0)
83 #define NFP_BPF_ABI_FLAG_MARK 1
86 * struct nfp_app_bpf - bpf app priv structure
87 * @app: backpointer to the app
88 * @ccm: common control message handler data
90 * @bpf_dev: BPF offload device handle
92 * @cmsg_key_sz: size of key in cmsg element array
93 * @cmsg_val_sz: size of value in cmsg element array
95 * @map_list: list of offloaded maps
96 * @maps_in_use: number of currently offloaded maps
97 * @map_elems_in_use: number of elements allocated to offloaded maps
99 * @maps_neutral: hash table of offload-neutral maps (on pointer)
101 * @abi_version: global BPF ABI version
103 * @adjust_head: adjust head capability
104 * @adjust_head.flags: extra flags for adjust head
105 * @adjust_head.off_min: minimal packet offset within buffer required
106 * @adjust_head.off_max: maximum packet offset within buffer required
107 * @adjust_head.guaranteed_sub: negative adjustment guaranteed possible
108 * @adjust_head.guaranteed_add: positive adjustment guaranteed possible
110 * @maps: map capability
111 * @maps.types: supported map types
112 * @maps.max_maps: max number of maps supported
113 * @maps.max_elems: max number of entries in each map
114 * @maps.max_key_sz: max size of map key
115 * @maps.max_val_sz: max size of map value
116 * @maps.max_elem_sz: max size of map entry (key + value)
118 * @helpers: helper addressess for various calls
119 * @helpers.map_lookup: map lookup helper address
120 * @helpers.map_update: map update helper address
121 * @helpers.map_delete: map delete helper address
122 * @helpers.perf_event_output: output perf event to a ring buffer
124 * @pseudo_random: FW initialized the pseudo-random machinery (CSRs)
125 * @queue_select: BPF can set the RX queue ID in packet vector
126 * @adjust_tail: BPF can simply trunc packet size for adjust tail
132 struct bpf_offload_dev *bpf_dev;
134 unsigned int cmsg_key_sz;
135 unsigned int cmsg_val_sz;
137 struct list_head map_list;
138 unsigned int maps_in_use;
139 unsigned int map_elems_in_use;
141 struct rhashtable maps_neutral;
145 struct nfp_bpf_cap_adjust_head {
166 u32 perf_event_output;
174 enum nfp_bpf_map_use {
178 NFP_MAP_USE_ATOMIC_CNT,
181 struct nfp_bpf_map_word {
182 unsigned char type :4;
183 unsigned char non_zero_update :1;
187 * struct nfp_bpf_map - private per-map data attached to BPF maps for offload
188 * @offmap: pointer to the offloaded BPF map
189 * @bpf: back pointer to bpf app private structure
190 * @tid: table id identifying map on datapath
191 * @l: link on the nfp_app_bpf->map_list list
192 * @use_map: map of how the value is used (in 4B chunks)
195 struct bpf_offloaded_map *offmap;
196 struct nfp_app_bpf *bpf;
199 struct nfp_bpf_map_word use_map[];
202 struct nfp_bpf_neutral_map {
209 extern const struct rhashtable_params nfp_bpf_maps_neutral_params;
212 struct nfp_insn_meta;
213 typedef int (*instr_cb_t)(struct nfp_prog *, struct nfp_insn_meta *);
215 #define nfp_prog_first_meta(nfp_prog) \
216 list_first_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l)
217 #define nfp_prog_last_meta(nfp_prog) \
218 list_last_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l)
219 #define nfp_meta_next(meta) list_next_entry(meta, l)
220 #define nfp_meta_prev(meta) list_prev_entry(meta, l)
223 * struct nfp_bpf_reg_state - register state for calls
224 * @reg: BPF register state from latest path
225 * @var_off: for stack arg - changes stack offset on different paths
227 struct nfp_bpf_reg_state {
228 struct bpf_reg_state reg;
232 #define FLAG_INSN_IS_JUMP_DST BIT(0)
233 #define FLAG_INSN_IS_SUBPROG_START BIT(1)
234 #define FLAG_INSN_PTR_CALLER_STACK_FRAME BIT(2)
235 /* Instruction is pointless, noop even on its own */
236 #define FLAG_INSN_SKIP_NOOP BIT(3)
237 /* Instruction is optimized out based on preceding instructions */
238 #define FLAG_INSN_SKIP_PREC_DEPENDENT BIT(4)
239 /* Instruction is optimized by the verifier */
240 #define FLAG_INSN_SKIP_VERIFIER_OPT BIT(5)
241 /* Instruction needs to zero extend to high 32-bit */
242 #define FLAG_INSN_DO_ZEXT BIT(6)
244 #define FLAG_INSN_SKIP_MASK (FLAG_INSN_SKIP_NOOP | \
245 FLAG_INSN_SKIP_PREC_DEPENDENT | \
246 FLAG_INSN_SKIP_VERIFIER_OPT)
249 * struct nfp_insn_meta - BPF instruction wrapper
250 * @insn: BPF instruction
251 * @ptr: pointer type for memory operations
252 * @ldst_gather_len: memcpy length gathered from load/store sequence
253 * @paired_st: the paired store insn at the head of the sequence
254 * @ptr_not_const: pointer is not always constant
255 * @pkt_cache: packet data cache information
256 * @pkt_cache.range_start: start offset for associated packet data cache
257 * @pkt_cache.range_end: end offset for associated packet data cache
258 * @pkt_cache.do_init: this read needs to initialize packet data cache
259 * @xadd_over_16bit: 16bit immediate is not guaranteed
260 * @xadd_maybe_16bit: 16bit immediate is possible
261 * @jmp_dst: destination info for jump instructions
262 * @jump_neg_op: jump instruction has inverted immediate, use ADD instead of SUB
263 * @num_insns_after_br: number of insns following a branch jump, used for fixup
264 * @func_id: function id for call instructions
265 * @arg1: arg1 for call instructions
266 * @arg2: arg2 for call instructions
267 * @umin_src: copy of core verifier umin_value for src opearnd.
268 * @umax_src: copy of core verifier umax_value for src operand.
269 * @umin_dst: copy of core verifier umin_value for dst opearnd.
270 * @umax_dst: copy of core verifier umax_value for dst operand.
271 * @off: index of first generated machine instruction (in nfp_prog.prog)
272 * @n: eBPF instruction number
273 * @flags: eBPF instruction extra optimization flags
274 * @subprog_idx: index of subprogram to which the instruction belongs
275 * @double_cb: callback for second part of the instruction
276 * @l: link on nfp_prog->insns list
278 struct nfp_insn_meta {
279 struct bpf_insn insn;
281 /* pointer ops (ld/st/xadd) */
283 struct bpf_reg_state ptr;
284 struct bpf_insn *paired_st;
292 bool xadd_over_16bit;
293 bool xadd_maybe_16bit;
297 struct nfp_insn_meta *jmp_dst;
299 u32 num_insns_after_br; /* only for BPF-to-BPF calls */
304 struct bpf_reg_state arg1;
305 struct nfp_bpf_reg_state arg2;
307 /* We are interested in range info for operands of ALU
308 * operations. For example, shift amount, multiplicand and
320 unsigned short flags;
321 unsigned short subprog_idx;
322 instr_cb_t double_cb;
327 #define BPF_SIZE_MASK 0x18
329 static inline u8 mbpf_class(const struct nfp_insn_meta *meta)
331 return BPF_CLASS(meta->insn.code);
334 static inline u8 mbpf_src(const struct nfp_insn_meta *meta)
336 return BPF_SRC(meta->insn.code);
339 static inline u8 mbpf_op(const struct nfp_insn_meta *meta)
341 return BPF_OP(meta->insn.code);
344 static inline u8 mbpf_mode(const struct nfp_insn_meta *meta)
346 return BPF_MODE(meta->insn.code);
349 static inline bool is_mbpf_alu(const struct nfp_insn_meta *meta)
351 return mbpf_class(meta) == BPF_ALU64 || mbpf_class(meta) == BPF_ALU;
354 static inline bool is_mbpf_load(const struct nfp_insn_meta *meta)
356 return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_LDX | BPF_MEM);
359 static inline bool is_mbpf_jmp32(const struct nfp_insn_meta *meta)
361 return mbpf_class(meta) == BPF_JMP32;
364 static inline bool is_mbpf_jmp64(const struct nfp_insn_meta *meta)
366 return mbpf_class(meta) == BPF_JMP;
369 static inline bool is_mbpf_jmp(const struct nfp_insn_meta *meta)
371 return is_mbpf_jmp32(meta) || is_mbpf_jmp64(meta);
374 static inline bool is_mbpf_store(const struct nfp_insn_meta *meta)
376 return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_MEM);
379 static inline bool is_mbpf_load_pkt(const struct nfp_insn_meta *meta)
381 return is_mbpf_load(meta) && meta->ptr.type == PTR_TO_PACKET;
384 static inline bool is_mbpf_store_pkt(const struct nfp_insn_meta *meta)
386 return is_mbpf_store(meta) && meta->ptr.type == PTR_TO_PACKET;
389 static inline bool is_mbpf_classic_load(const struct nfp_insn_meta *meta)
391 u8 code = meta->insn.code;
393 return BPF_CLASS(code) == BPF_LD &&
394 (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND);
397 static inline bool is_mbpf_classic_store(const struct nfp_insn_meta *meta)
399 u8 code = meta->insn.code;
401 return BPF_CLASS(code) == BPF_ST && BPF_MODE(code) == BPF_MEM;
404 static inline bool is_mbpf_classic_store_pkt(const struct nfp_insn_meta *meta)
406 return is_mbpf_classic_store(meta) && meta->ptr.type == PTR_TO_PACKET;
409 static inline bool is_mbpf_xadd(const struct nfp_insn_meta *meta)
411 return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_XADD);
414 static inline bool is_mbpf_mul(const struct nfp_insn_meta *meta)
416 return is_mbpf_alu(meta) && mbpf_op(meta) == BPF_MUL;
419 static inline bool is_mbpf_div(const struct nfp_insn_meta *meta)
421 return is_mbpf_alu(meta) && mbpf_op(meta) == BPF_DIV;
424 static inline bool is_mbpf_cond_jump(const struct nfp_insn_meta *meta)
428 if (is_mbpf_jmp32(meta))
431 if (!is_mbpf_jmp64(meta))
435 return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
438 static inline bool is_mbpf_helper_call(const struct nfp_insn_meta *meta)
440 struct bpf_insn insn = meta->insn;
442 return insn.code == (BPF_JMP | BPF_CALL) &&
443 insn.src_reg != BPF_PSEUDO_CALL;
446 static inline bool is_mbpf_pseudo_call(const struct nfp_insn_meta *meta)
448 struct bpf_insn insn = meta->insn;
450 return insn.code == (BPF_JMP | BPF_CALL) &&
451 insn.src_reg == BPF_PSEUDO_CALL;
454 #define STACK_FRAME_ALIGN 64
457 * struct nfp_bpf_subprog_info - nfp BPF sub-program (a.k.a. function) info
458 * @stack_depth: maximum stack depth used by this sub-program
459 * @needs_reg_push: whether sub-program uses callee-saved registers
461 struct nfp_bpf_subprog_info {
463 u8 needs_reg_push : 1;
467 * struct nfp_prog - nfp BPF program
468 * @bpf: backpointer to the bpf app priv structure
469 * @prog: machine code
470 * @prog_len: number of valid instructions in @prog array
471 * @__prog_alloc_len: alloc size of @prog array
472 * @stack_size: total amount of stack used
473 * @verifier_meta: temporary storage for verifier's insn meta
474 * @type: BPF program type
475 * @last_bpf_off: address of the last instruction translated from BPF
476 * @tgt_out: jump target for normal exit
477 * @tgt_abort: jump target for abort (e.g. access outside of packet buffer)
478 * @tgt_call_push_regs: jump target for subroutine for saving R6~R9 to stack
479 * @tgt_call_pop_regs: jump target for subroutine used for restoring R6~R9
480 * @n_translated: number of successfully translated instructions (for errors)
481 * @error: error code if something went wrong
482 * @stack_frame_depth: max stack depth for current frame
483 * @adjust_head_location: if program has single adjust head call - the insn no.
484 * @map_records_cnt: the number of map pointers recorded for this prog
485 * @subprog_cnt: number of sub-programs, including main function
486 * @map_records: the map record pointers from bpf->maps_neutral
487 * @subprog: pointer to an array of objects holding info about sub-programs
488 * @n_insns: number of instructions on @insns list
489 * @insns: list of BPF instruction wrappers (struct nfp_insn_meta)
492 struct nfp_app_bpf *bpf;
495 unsigned int prog_len;
496 unsigned int __prog_alloc_len;
498 unsigned int stack_size;
500 struct nfp_insn_meta *verifier_meta;
502 enum bpf_prog_type type;
504 unsigned int last_bpf_off;
505 unsigned int tgt_out;
506 unsigned int tgt_abort;
507 unsigned int tgt_call_push_regs;
508 unsigned int tgt_call_pop_regs;
510 unsigned int n_translated;
513 unsigned int stack_frame_depth;
514 unsigned int adjust_head_location;
516 unsigned int map_records_cnt;
517 unsigned int subprog_cnt;
518 struct nfp_bpf_neutral_map **map_records;
519 struct nfp_bpf_subprog_info *subprog;
521 unsigned int n_insns;
522 struct list_head insns;
526 * struct nfp_bpf_vnic - per-vNIC BPF priv structure
527 * @tc_prog: currently loaded cls_bpf program
528 * @start_off: address of the first instruction in the memory
529 * @tgt_done: jump target to get the next packet
531 struct nfp_bpf_vnic {
532 struct bpf_prog *tc_prog;
533 unsigned int start_off;
534 unsigned int tgt_done;
537 bool nfp_is_subprog_start(struct nfp_insn_meta *meta);
538 void nfp_bpf_jit_prepare(struct nfp_prog *nfp_prog);
539 int nfp_bpf_jit(struct nfp_prog *prog);
540 bool nfp_bpf_supported_opcode(u8 code);
542 int nfp_verify_insn(struct bpf_verifier_env *env, int insn_idx,
544 int nfp_bpf_finalize(struct bpf_verifier_env *env);
546 int nfp_bpf_opt_replace_insn(struct bpf_verifier_env *env, u32 off,
547 struct bpf_insn *insn);
548 int nfp_bpf_opt_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
550 extern const struct bpf_prog_offload_ops nfp_bpf_dev_ops;
556 int nfp_ndo_bpf(struct nfp_app *app, struct nfp_net *nn,
557 struct netdev_bpf *bpf);
558 int nfp_net_bpf_offload(struct nfp_net *nn, struct bpf_prog *prog,
559 bool old_prog, struct netlink_ext_ack *extack);
561 struct nfp_insn_meta *
562 nfp_bpf_goto_meta(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
563 unsigned int insn_idx);
565 void *nfp_bpf_relo_for_vnic(struct nfp_prog *nfp_prog, struct nfp_bpf_vnic *bv);
567 unsigned int nfp_bpf_ctrl_cmsg_mtu(struct nfp_app_bpf *bpf);
569 nfp_bpf_ctrl_alloc_map(struct nfp_app_bpf *bpf, struct bpf_map *map);
571 nfp_bpf_ctrl_free_map(struct nfp_app_bpf *bpf, struct nfp_bpf_map *nfp_map);
572 int nfp_bpf_ctrl_getfirst_entry(struct bpf_offloaded_map *offmap,
574 int nfp_bpf_ctrl_update_entry(struct bpf_offloaded_map *offmap,
575 void *key, void *value, u64 flags);
576 int nfp_bpf_ctrl_del_entry(struct bpf_offloaded_map *offmap, void *key);
577 int nfp_bpf_ctrl_lookup_entry(struct bpf_offloaded_map *offmap,
578 void *key, void *value);
579 int nfp_bpf_ctrl_getnext_entry(struct bpf_offloaded_map *offmap,
580 void *key, void *next_key);
582 int nfp_bpf_event_output(struct nfp_app_bpf *bpf, const void *data,
585 void nfp_bpf_ctrl_msg_rx(struct nfp_app *app, struct sk_buff *skb);
587 nfp_bpf_ctrl_msg_rx_raw(struct nfp_app *app, const void *data,