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
102 * @cmsg_cache_cnt: number of entries to read for caching
104 * @adjust_head: adjust head capability
105 * @adjust_head.flags: extra flags for adjust head
106 * @adjust_head.off_min: minimal packet offset within buffer required
107 * @adjust_head.off_max: maximum packet offset within buffer required
108 * @adjust_head.guaranteed_sub: negative adjustment guaranteed possible
109 * @adjust_head.guaranteed_add: positive adjustment guaranteed possible
111 * @maps: map capability
112 * @maps.types: supported map types
113 * @maps.max_maps: max number of maps supported
114 * @maps.max_elems: max number of entries in each map
115 * @maps.max_key_sz: max size of map key
116 * @maps.max_val_sz: max size of map value
117 * @maps.max_elem_sz: max size of map entry (key + value)
119 * @helpers: helper addressess for various calls
120 * @helpers.map_lookup: map lookup helper address
121 * @helpers.map_update: map update helper address
122 * @helpers.map_delete: map delete helper address
123 * @helpers.perf_event_output: output perf event to a ring buffer
125 * @pseudo_random: FW initialized the pseudo-random machinery (CSRs)
126 * @queue_select: BPF can set the RX queue ID in packet vector
127 * @adjust_tail: BPF can simply trunc packet size for adjust tail
128 * @cmsg_multi_ent: FW can pack multiple map entries in a single cmsg
134 struct bpf_offload_dev
*bpf_dev
;
136 unsigned int cmsg_key_sz
;
137 unsigned int cmsg_val_sz
;
139 unsigned int cmsg_cache_cnt
;
141 struct list_head map_list
;
142 unsigned int maps_in_use
;
143 unsigned int map_elems_in_use
;
145 struct rhashtable maps_neutral
;
149 struct nfp_bpf_cap_adjust_head
{
170 u32 perf_event_output
;
179 enum nfp_bpf_map_use
{
183 NFP_MAP_USE_ATOMIC_CNT
,
186 struct nfp_bpf_map_word
{
187 unsigned char type
:4;
188 unsigned char non_zero_update
:1;
191 #define NFP_BPF_MAP_CACHE_CNT 4U
192 #define NFP_BPF_MAP_CACHE_TIME_NS (250 * 1000)
195 * struct nfp_bpf_map - private per-map data attached to BPF maps for offload
196 * @offmap: pointer to the offloaded BPF map
197 * @bpf: back pointer to bpf app private structure
198 * @tid: table id identifying map on datapath
200 * @cache_lock: protects @cache_blockers, @cache_to, @cache
201 * @cache_blockers: number of ops in flight which block caching
202 * @cache_gen: counter incremented by every blocker on exit
203 * @cache_to: time when cache will no longer be valid (ns)
204 * @cache: skb with cached response
206 * @l: link on the nfp_app_bpf->map_list list
207 * @use_map: map of how the value is used (in 4B chunks)
210 struct bpf_offloaded_map
*offmap
;
211 struct nfp_app_bpf
*bpf
;
214 spinlock_t cache_lock
;
218 struct sk_buff
*cache
;
221 struct nfp_bpf_map_word use_map
[];
224 struct nfp_bpf_neutral_map
{
231 extern const struct rhashtable_params nfp_bpf_maps_neutral_params
;
234 struct nfp_insn_meta
;
235 typedef int (*instr_cb_t
)(struct nfp_prog
*, struct nfp_insn_meta
*);
237 #define nfp_prog_first_meta(nfp_prog) \
238 list_first_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l)
239 #define nfp_prog_last_meta(nfp_prog) \
240 list_last_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l)
241 #define nfp_meta_next(meta) list_next_entry(meta, l)
242 #define nfp_meta_prev(meta) list_prev_entry(meta, l)
245 * struct nfp_bpf_reg_state - register state for calls
246 * @reg: BPF register state from latest path
247 * @var_off: for stack arg - changes stack offset on different paths
249 struct nfp_bpf_reg_state
{
250 struct bpf_reg_state reg
;
254 #define FLAG_INSN_IS_JUMP_DST BIT(0)
255 #define FLAG_INSN_IS_SUBPROG_START BIT(1)
256 #define FLAG_INSN_PTR_CALLER_STACK_FRAME BIT(2)
257 /* Instruction is pointless, noop even on its own */
258 #define FLAG_INSN_SKIP_NOOP BIT(3)
259 /* Instruction is optimized out based on preceding instructions */
260 #define FLAG_INSN_SKIP_PREC_DEPENDENT BIT(4)
261 /* Instruction is optimized by the verifier */
262 #define FLAG_INSN_SKIP_VERIFIER_OPT BIT(5)
263 /* Instruction needs to zero extend to high 32-bit */
264 #define FLAG_INSN_DO_ZEXT BIT(6)
266 #define FLAG_INSN_SKIP_MASK (FLAG_INSN_SKIP_NOOP | \
267 FLAG_INSN_SKIP_PREC_DEPENDENT | \
268 FLAG_INSN_SKIP_VERIFIER_OPT)
271 * struct nfp_insn_meta - BPF instruction wrapper
272 * @insn: BPF instruction
273 * @ptr: pointer type for memory operations
274 * @ldst_gather_len: memcpy length gathered from load/store sequence
275 * @paired_st: the paired store insn at the head of the sequence
276 * @ptr_not_const: pointer is not always constant
277 * @pkt_cache: packet data cache information
278 * @pkt_cache.range_start: start offset for associated packet data cache
279 * @pkt_cache.range_end: end offset for associated packet data cache
280 * @pkt_cache.do_init: this read needs to initialize packet data cache
281 * @xadd_over_16bit: 16bit immediate is not guaranteed
282 * @xadd_maybe_16bit: 16bit immediate is possible
283 * @jmp_dst: destination info for jump instructions
284 * @jump_neg_op: jump instruction has inverted immediate, use ADD instead of SUB
285 * @num_insns_after_br: number of insns following a branch jump, used for fixup
286 * @func_id: function id for call instructions
287 * @arg1: arg1 for call instructions
288 * @arg2: arg2 for call instructions
289 * @umin_src: copy of core verifier umin_value for src opearnd.
290 * @umax_src: copy of core verifier umax_value for src operand.
291 * @umin_dst: copy of core verifier umin_value for dst opearnd.
292 * @umax_dst: copy of core verifier umax_value for dst operand.
293 * @off: index of first generated machine instruction (in nfp_prog.prog)
294 * @n: eBPF instruction number
295 * @flags: eBPF instruction extra optimization flags
296 * @subprog_idx: index of subprogram to which the instruction belongs
297 * @double_cb: callback for second part of the instruction
298 * @l: link on nfp_prog->insns list
300 struct nfp_insn_meta
{
301 struct bpf_insn insn
;
303 /* pointer ops (ld/st/xadd) */
305 struct bpf_reg_state ptr
;
306 struct bpf_insn
*paired_st
;
314 bool xadd_over_16bit
;
315 bool xadd_maybe_16bit
;
319 struct nfp_insn_meta
*jmp_dst
;
321 u32 num_insns_after_br
; /* only for BPF-to-BPF calls */
326 struct bpf_reg_state arg1
;
327 struct nfp_bpf_reg_state arg2
;
329 /* We are interested in range info for operands of ALU
330 * operations. For example, shift amount, multiplicand and
342 unsigned short flags
;
343 unsigned short subprog_idx
;
344 instr_cb_t double_cb
;
349 #define BPF_SIZE_MASK 0x18
351 static inline u8
mbpf_class(const struct nfp_insn_meta
*meta
)
353 return BPF_CLASS(meta
->insn
.code
);
356 static inline u8
mbpf_src(const struct nfp_insn_meta
*meta
)
358 return BPF_SRC(meta
->insn
.code
);
361 static inline u8
mbpf_op(const struct nfp_insn_meta
*meta
)
363 return BPF_OP(meta
->insn
.code
);
366 static inline u8
mbpf_mode(const struct nfp_insn_meta
*meta
)
368 return BPF_MODE(meta
->insn
.code
);
371 static inline bool is_mbpf_alu(const struct nfp_insn_meta
*meta
)
373 return mbpf_class(meta
) == BPF_ALU64
|| mbpf_class(meta
) == BPF_ALU
;
376 static inline bool is_mbpf_load(const struct nfp_insn_meta
*meta
)
378 return (meta
->insn
.code
& ~BPF_SIZE_MASK
) == (BPF_LDX
| BPF_MEM
);
381 static inline bool is_mbpf_jmp32(const struct nfp_insn_meta
*meta
)
383 return mbpf_class(meta
) == BPF_JMP32
;
386 static inline bool is_mbpf_jmp64(const struct nfp_insn_meta
*meta
)
388 return mbpf_class(meta
) == BPF_JMP
;
391 static inline bool is_mbpf_jmp(const struct nfp_insn_meta
*meta
)
393 return is_mbpf_jmp32(meta
) || is_mbpf_jmp64(meta
);
396 static inline bool is_mbpf_store(const struct nfp_insn_meta
*meta
)
398 return (meta
->insn
.code
& ~BPF_SIZE_MASK
) == (BPF_STX
| BPF_MEM
);
401 static inline bool is_mbpf_load_pkt(const struct nfp_insn_meta
*meta
)
403 return is_mbpf_load(meta
) && meta
->ptr
.type
== PTR_TO_PACKET
;
406 static inline bool is_mbpf_store_pkt(const struct nfp_insn_meta
*meta
)
408 return is_mbpf_store(meta
) && meta
->ptr
.type
== PTR_TO_PACKET
;
411 static inline bool is_mbpf_classic_load(const struct nfp_insn_meta
*meta
)
413 u8 code
= meta
->insn
.code
;
415 return BPF_CLASS(code
) == BPF_LD
&&
416 (BPF_MODE(code
) == BPF_ABS
|| BPF_MODE(code
) == BPF_IND
);
419 static inline bool is_mbpf_classic_store(const struct nfp_insn_meta
*meta
)
421 u8 code
= meta
->insn
.code
;
423 return BPF_CLASS(code
) == BPF_ST
&& BPF_MODE(code
) == BPF_MEM
;
426 static inline bool is_mbpf_classic_store_pkt(const struct nfp_insn_meta
*meta
)
428 return is_mbpf_classic_store(meta
) && meta
->ptr
.type
== PTR_TO_PACKET
;
431 static inline bool is_mbpf_xadd(const struct nfp_insn_meta
*meta
)
433 return (meta
->insn
.code
& ~BPF_SIZE_MASK
) == (BPF_STX
| BPF_XADD
);
436 static inline bool is_mbpf_mul(const struct nfp_insn_meta
*meta
)
438 return is_mbpf_alu(meta
) && mbpf_op(meta
) == BPF_MUL
;
441 static inline bool is_mbpf_div(const struct nfp_insn_meta
*meta
)
443 return is_mbpf_alu(meta
) && mbpf_op(meta
) == BPF_DIV
;
446 static inline bool is_mbpf_cond_jump(const struct nfp_insn_meta
*meta
)
450 if (is_mbpf_jmp32(meta
))
453 if (!is_mbpf_jmp64(meta
))
457 return op
!= BPF_JA
&& op
!= BPF_EXIT
&& op
!= BPF_CALL
;
460 static inline bool is_mbpf_helper_call(const struct nfp_insn_meta
*meta
)
462 struct bpf_insn insn
= meta
->insn
;
464 return insn
.code
== (BPF_JMP
| BPF_CALL
) &&
465 insn
.src_reg
!= BPF_PSEUDO_CALL
;
468 static inline bool is_mbpf_pseudo_call(const struct nfp_insn_meta
*meta
)
470 struct bpf_insn insn
= meta
->insn
;
472 return insn
.code
== (BPF_JMP
| BPF_CALL
) &&
473 insn
.src_reg
== BPF_PSEUDO_CALL
;
476 #define STACK_FRAME_ALIGN 64
479 * struct nfp_bpf_subprog_info - nfp BPF sub-program (a.k.a. function) info
480 * @stack_depth: maximum stack depth used by this sub-program
481 * @needs_reg_push: whether sub-program uses callee-saved registers
483 struct nfp_bpf_subprog_info
{
485 u8 needs_reg_push
: 1;
489 * struct nfp_prog - nfp BPF program
490 * @bpf: backpointer to the bpf app priv structure
491 * @prog: machine code
492 * @prog_len: number of valid instructions in @prog array
493 * @__prog_alloc_len: alloc size of @prog array
494 * @stack_size: total amount of stack used
495 * @verifier_meta: temporary storage for verifier's insn meta
496 * @type: BPF program type
497 * @last_bpf_off: address of the last instruction translated from BPF
498 * @tgt_out: jump target for normal exit
499 * @tgt_abort: jump target for abort (e.g. access outside of packet buffer)
500 * @tgt_call_push_regs: jump target for subroutine for saving R6~R9 to stack
501 * @tgt_call_pop_regs: jump target for subroutine used for restoring R6~R9
502 * @n_translated: number of successfully translated instructions (for errors)
503 * @error: error code if something went wrong
504 * @stack_frame_depth: max stack depth for current frame
505 * @adjust_head_location: if program has single adjust head call - the insn no.
506 * @map_records_cnt: the number of map pointers recorded for this prog
507 * @subprog_cnt: number of sub-programs, including main function
508 * @map_records: the map record pointers from bpf->maps_neutral
509 * @subprog: pointer to an array of objects holding info about sub-programs
510 * @n_insns: number of instructions on @insns list
511 * @insns: list of BPF instruction wrappers (struct nfp_insn_meta)
514 struct nfp_app_bpf
*bpf
;
517 unsigned int prog_len
;
518 unsigned int __prog_alloc_len
;
520 unsigned int stack_size
;
522 struct nfp_insn_meta
*verifier_meta
;
524 enum bpf_prog_type type
;
526 unsigned int last_bpf_off
;
527 unsigned int tgt_out
;
528 unsigned int tgt_abort
;
529 unsigned int tgt_call_push_regs
;
530 unsigned int tgt_call_pop_regs
;
532 unsigned int n_translated
;
535 unsigned int stack_frame_depth
;
536 unsigned int adjust_head_location
;
538 unsigned int map_records_cnt
;
539 unsigned int subprog_cnt
;
540 struct nfp_bpf_neutral_map
**map_records
;
541 struct nfp_bpf_subprog_info
*subprog
;
543 unsigned int n_insns
;
544 struct list_head insns
;
548 * struct nfp_bpf_vnic - per-vNIC BPF priv structure
549 * @tc_prog: currently loaded cls_bpf program
550 * @start_off: address of the first instruction in the memory
551 * @tgt_done: jump target to get the next packet
553 struct nfp_bpf_vnic
{
554 struct bpf_prog
*tc_prog
;
555 unsigned int start_off
;
556 unsigned int tgt_done
;
559 bool nfp_is_subprog_start(struct nfp_insn_meta
*meta
);
560 void nfp_bpf_jit_prepare(struct nfp_prog
*nfp_prog
);
561 int nfp_bpf_jit(struct nfp_prog
*prog
);
562 bool nfp_bpf_supported_opcode(u8 code
);
564 int nfp_verify_insn(struct bpf_verifier_env
*env
, int insn_idx
,
566 int nfp_bpf_finalize(struct bpf_verifier_env
*env
);
568 int nfp_bpf_opt_replace_insn(struct bpf_verifier_env
*env
, u32 off
,
569 struct bpf_insn
*insn
);
570 int nfp_bpf_opt_remove_insns(struct bpf_verifier_env
*env
, u32 off
, u32 cnt
);
572 extern const struct bpf_prog_offload_ops nfp_bpf_dev_ops
;
578 int nfp_ndo_bpf(struct nfp_app
*app
, struct nfp_net
*nn
,
579 struct netdev_bpf
*bpf
);
580 int nfp_net_bpf_offload(struct nfp_net
*nn
, struct bpf_prog
*prog
,
581 bool old_prog
, struct netlink_ext_ack
*extack
);
583 struct nfp_insn_meta
*
584 nfp_bpf_goto_meta(struct nfp_prog
*nfp_prog
, struct nfp_insn_meta
*meta
,
585 unsigned int insn_idx
);
587 void *nfp_bpf_relo_for_vnic(struct nfp_prog
*nfp_prog
, struct nfp_bpf_vnic
*bv
);
589 unsigned int nfp_bpf_ctrl_cmsg_min_mtu(struct nfp_app_bpf
*bpf
);
590 unsigned int nfp_bpf_ctrl_cmsg_mtu(struct nfp_app_bpf
*bpf
);
591 unsigned int nfp_bpf_ctrl_cmsg_cache_cnt(struct nfp_app_bpf
*bpf
);
593 nfp_bpf_ctrl_alloc_map(struct nfp_app_bpf
*bpf
, struct bpf_map
*map
);
595 nfp_bpf_ctrl_free_map(struct nfp_app_bpf
*bpf
, struct nfp_bpf_map
*nfp_map
);
596 int nfp_bpf_ctrl_getfirst_entry(struct bpf_offloaded_map
*offmap
,
598 int nfp_bpf_ctrl_update_entry(struct bpf_offloaded_map
*offmap
,
599 void *key
, void *value
, u64 flags
);
600 int nfp_bpf_ctrl_del_entry(struct bpf_offloaded_map
*offmap
, void *key
);
601 int nfp_bpf_ctrl_lookup_entry(struct bpf_offloaded_map
*offmap
,
602 void *key
, void *value
);
603 int nfp_bpf_ctrl_getnext_entry(struct bpf_offloaded_map
*offmap
,
604 void *key
, void *next_key
);
606 int nfp_bpf_event_output(struct nfp_app_bpf
*bpf
, const void *data
,
609 void nfp_bpf_ctrl_msg_rx(struct nfp_app
*app
, struct sk_buff
*skb
);
611 nfp_bpf_ctrl_msg_rx_raw(struct nfp_app
*app
, const void *data
,