hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/net/ethernet/netronome/nfp/bpf/main.h
....@@ -1,35 +1,5 @@
1
-/*
2
- * Copyright (C) 2016-2018 Netronome Systems, Inc.
3
- *
4
- * This software is dual licensed under the GNU General License Version 2,
5
- * June 1991 as shown in the file COPYING in the top-level directory of this
6
- * source tree or the BSD 2-Clause License provided below. You have the
7
- * option to license this software under the complete terms of either license.
8
- *
9
- * The BSD 2-Clause License:
10
- *
11
- * Redistribution and use in source and binary forms, with or
12
- * without modification, are permitted provided that the following
13
- * conditions are met:
14
- *
15
- * 1. Redistributions of source code must retain the above
16
- * copyright notice, this list of conditions and the following
17
- * disclaimer.
18
- *
19
- * 2. Redistributions in binary form must reproduce the above
20
- * copyright notice, this list of conditions and the following
21
- * disclaimer in the documentation and/or other materials
22
- * provided with the distribution.
23
- *
24
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
- * SOFTWARE.
32
- */
1
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2
+/* Copyright (C) 2016-2018 Netronome Systems, Inc. */
333
344 #ifndef __NFP_BPF_H__
355 #define __NFP_BPF_H__ 1
....@@ -44,6 +14,7 @@
4414 #include <linux/types.h>
4515 #include <linux/wait.h>
4616
17
+#include "../ccm.h"
4718 #include "../nfp_asm.h"
4819 #include "fw.h"
4920
....@@ -61,6 +32,8 @@
6132 /* internal jumps to parts of the outro */
6233 RELO_BR_GO_OUT,
6334 RELO_BR_GO_ABORT,
35
+ RELO_BR_GO_CALL_PUSH_REGS,
36
+ RELO_BR_GO_CALL_POP_REGS,
6437 /* external jumps to fixed addresses */
6538 RELO_BR_NEXT_PKT,
6639 RELO_BR_HELPER,
....@@ -104,6 +77,7 @@
10477 #define imma_a(np) reg_a(STATIC_REG_IMMA)
10578 #define imma_b(np) reg_b(STATIC_REG_IMMA)
10679 #define imm_both(np) reg_both(STATIC_REG_IMM)
80
+#define ret_reg(np) imm_a(np)
10781
10882 #define NFP_BPF_ABI_FLAGS reg_imm(0)
10983 #define NFP_BPF_ABI_FLAG_MARK 1
....@@ -111,21 +85,21 @@
11185 /**
11286 * struct nfp_app_bpf - bpf app priv structure
11387 * @app: backpointer to the app
88
+ * @ccm: common control message handler data
11489 *
11590 * @bpf_dev: BPF offload device handle
11691 *
117
- * @tag_allocator: bitmap of control message tags in use
118
- * @tag_alloc_next: next tag bit to allocate
119
- * @tag_alloc_last: next tag bit to be freed
120
- *
121
- * @cmsg_replies: received cmsg replies waiting to be consumed
122
- * @cmsg_wq: work queue for waiting for cmsg replies
92
+ * @cmsg_key_sz: size of key in cmsg element array
93
+ * @cmsg_val_sz: size of value in cmsg element array
12394 *
12495 * @map_list: list of offloaded maps
12596 * @maps_in_use: number of currently offloaded maps
12697 * @map_elems_in_use: number of elements allocated to offloaded maps
12798 *
12899 * @maps_neutral: hash table of offload-neutral maps (on pointer)
100
+ *
101
+ * @abi_version: global BPF ABI version
102
+ * @cmsg_cache_cnt: number of entries to read for caching
129103 *
130104 * @adjust_head: adjust head capability
131105 * @adjust_head.flags: extra flags for adjust head
....@@ -151,24 +125,26 @@
151125 * @pseudo_random: FW initialized the pseudo-random machinery (CSRs)
152126 * @queue_select: BPF can set the RX queue ID in packet vector
153127 * @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
154129 */
155130 struct nfp_app_bpf {
156131 struct nfp_app *app;
132
+ struct nfp_ccm ccm;
157133
158134 struct bpf_offload_dev *bpf_dev;
159135
160
- DECLARE_BITMAP(tag_allocator, U16_MAX + 1);
161
- u16 tag_alloc_next;
162
- u16 tag_alloc_last;
136
+ unsigned int cmsg_key_sz;
137
+ unsigned int cmsg_val_sz;
163138
164
- struct sk_buff_head cmsg_replies;
165
- struct wait_queue_head cmsg_wq;
139
+ unsigned int cmsg_cache_cnt;
166140
167141 struct list_head map_list;
168142 unsigned int maps_in_use;
169143 unsigned int map_elems_in_use;
170144
171145 struct rhashtable maps_neutral;
146
+
147
+ u32 abi_version;
172148
173149 struct nfp_bpf_cap_adjust_head {
174150 u32 flags;
....@@ -197,6 +173,7 @@
197173 bool pseudo_random;
198174 bool queue_select;
199175 bool adjust_tail;
176
+ bool cmsg_multi_ent;
200177 };
201178
202179 enum nfp_bpf_map_use {
....@@ -211,11 +188,21 @@
211188 unsigned char non_zero_update :1;
212189 };
213190
191
+#define NFP_BPF_MAP_CACHE_CNT 4U
192
+#define NFP_BPF_MAP_CACHE_TIME_NS (250 * 1000)
193
+
214194 /**
215195 * struct nfp_bpf_map - private per-map data attached to BPF maps for offload
216196 * @offmap: pointer to the offloaded BPF map
217197 * @bpf: back pointer to bpf app private structure
218198 * @tid: table id identifying map on datapath
199
+ *
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
205
+ *
219206 * @l: link on the nfp_app_bpf->map_list list
220207 * @use_map: map of how the value is used (in 4B chunks)
221208 */
....@@ -223,6 +210,13 @@
223210 struct bpf_offloaded_map *offmap;
224211 struct nfp_app_bpf *bpf;
225212 u32 tid;
213
+
214
+ spinlock_t cache_lock;
215
+ u32 cache_blockers;
216
+ u32 cache_gen;
217
+ u64 cache_to;
218
+ struct sk_buff *cache;
219
+
226220 struct list_head l;
227221 struct nfp_bpf_map_word use_map[];
228222 };
....@@ -257,7 +251,21 @@
257251 bool var_off;
258252 };
259253
260
-#define FLAG_INSN_IS_JUMP_DST BIT(0)
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)
265
+
266
+#define FLAG_INSN_SKIP_MASK (FLAG_INSN_SKIP_NOOP | \
267
+ FLAG_INSN_SKIP_PREC_DEPENDENT | \
268
+ FLAG_INSN_SKIP_VERIFIER_OPT)
261269
262270 /**
263271 * struct nfp_insn_meta - BPF instruction wrapper
....@@ -274,6 +282,7 @@
274282 * @xadd_maybe_16bit: 16bit immediate is possible
275283 * @jmp_dst: destination info for jump instructions
276284 * @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
277286 * @func_id: function id for call instructions
278287 * @arg1: arg1 for call instructions
279288 * @arg2: arg2 for call instructions
....@@ -284,7 +293,7 @@
284293 * @off: index of first generated machine instruction (in nfp_prog.prog)
285294 * @n: eBPF instruction number
286295 * @flags: eBPF instruction extra optimization flags
287
- * @skip: skip this instruction (optimized out)
296
+ * @subprog_idx: index of subprogram to which the instruction belongs
288297 * @double_cb: callback for second part of the instruction
289298 * @l: link on nfp_prog->insns list
290299 */
....@@ -309,6 +318,7 @@
309318 struct {
310319 struct nfp_insn_meta *jmp_dst;
311320 bool jump_neg_op;
321
+ u32 num_insns_after_br; /* only for BPF-to-BPF calls */
312322 };
313323 /* function calls */
314324 struct {
....@@ -330,7 +340,7 @@
330340 unsigned int off;
331341 unsigned short n;
332342 unsigned short flags;
333
- bool skip;
343
+ unsigned short subprog_idx;
334344 instr_cb_t double_cb;
335345
336346 struct list_head l;
....@@ -366,6 +376,21 @@
366376 static inline bool is_mbpf_load(const struct nfp_insn_meta *meta)
367377 {
368378 return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_LDX | BPF_MEM);
379
+}
380
+
381
+static inline bool is_mbpf_jmp32(const struct nfp_insn_meta *meta)
382
+{
383
+ return mbpf_class(meta) == BPF_JMP32;
384
+}
385
+
386
+static inline bool is_mbpf_jmp64(const struct nfp_insn_meta *meta)
387
+{
388
+ return mbpf_class(meta) == BPF_JMP;
389
+}
390
+
391
+static inline bool is_mbpf_jmp(const struct nfp_insn_meta *meta)
392
+{
393
+ return is_mbpf_jmp32(meta) || is_mbpf_jmp64(meta);
369394 }
370395
371396 static inline bool is_mbpf_store(const struct nfp_insn_meta *meta)
....@@ -418,23 +443,71 @@
418443 return is_mbpf_alu(meta) && mbpf_op(meta) == BPF_DIV;
419444 }
420445
446
+static inline bool is_mbpf_cond_jump(const struct nfp_insn_meta *meta)
447
+{
448
+ u8 op;
449
+
450
+ if (is_mbpf_jmp32(meta))
451
+ return true;
452
+
453
+ if (!is_mbpf_jmp64(meta))
454
+ return false;
455
+
456
+ op = mbpf_op(meta);
457
+ return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
458
+}
459
+
460
+static inline bool is_mbpf_helper_call(const struct nfp_insn_meta *meta)
461
+{
462
+ struct bpf_insn insn = meta->insn;
463
+
464
+ return insn.code == (BPF_JMP | BPF_CALL) &&
465
+ insn.src_reg != BPF_PSEUDO_CALL;
466
+}
467
+
468
+static inline bool is_mbpf_pseudo_call(const struct nfp_insn_meta *meta)
469
+{
470
+ struct bpf_insn insn = meta->insn;
471
+
472
+ return insn.code == (BPF_JMP | BPF_CALL) &&
473
+ insn.src_reg == BPF_PSEUDO_CALL;
474
+}
475
+
476
+#define STACK_FRAME_ALIGN 64
477
+
478
+/**
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
482
+ */
483
+struct nfp_bpf_subprog_info {
484
+ u16 stack_depth;
485
+ u8 needs_reg_push : 1;
486
+};
487
+
421488 /**
422489 * struct nfp_prog - nfp BPF program
423490 * @bpf: backpointer to the bpf app priv structure
424491 * @prog: machine code
425492 * @prog_len: number of valid instructions in @prog array
426493 * @__prog_alloc_len: alloc size of @prog array
494
+ * @stack_size: total amount of stack used
427495 * @verifier_meta: temporary storage for verifier's insn meta
428496 * @type: BPF program type
429497 * @last_bpf_off: address of the last instruction translated from BPF
430498 * @tgt_out: jump target for normal exit
431499 * @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
432502 * @n_translated: number of successfully translated instructions (for errors)
433503 * @error: error code if something went wrong
434
- * @stack_depth: max stack depth from the verifier
504
+ * @stack_frame_depth: max stack depth for current frame
435505 * @adjust_head_location: if program has single adjust head call - the insn no.
436506 * @map_records_cnt: the number of map pointers recorded for this prog
507
+ * @subprog_cnt: number of sub-programs, including main function
437508 * @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
438511 * @insns: list of BPF instruction wrappers (struct nfp_insn_meta)
439512 */
440513 struct nfp_prog {
....@@ -444,6 +517,8 @@
444517 unsigned int prog_len;
445518 unsigned int __prog_alloc_len;
446519
520
+ unsigned int stack_size;
521
+
447522 struct nfp_insn_meta *verifier_meta;
448523
449524 enum bpf_prog_type type;
....@@ -451,16 +526,21 @@
451526 unsigned int last_bpf_off;
452527 unsigned int tgt_out;
453528 unsigned int tgt_abort;
529
+ unsigned int tgt_call_push_regs;
530
+ unsigned int tgt_call_pop_regs;
454531
455532 unsigned int n_translated;
456533 int error;
457534
458
- unsigned int stack_depth;
535
+ unsigned int stack_frame_depth;
459536 unsigned int adjust_head_location;
460537
461538 unsigned int map_records_cnt;
539
+ unsigned int subprog_cnt;
462540 struct nfp_bpf_neutral_map **map_records;
541
+ struct nfp_bpf_subprog_info *subprog;
463542
543
+ unsigned int n_insns;
464544 struct list_head insns;
465545 };
466546
....@@ -476,11 +556,22 @@
476556 unsigned int tgt_done;
477557 };
478558
479
-void nfp_bpf_jit_prepare(struct nfp_prog *nfp_prog, unsigned int cnt);
559
+bool nfp_is_subprog_start(struct nfp_insn_meta *meta);
560
+void nfp_bpf_jit_prepare(struct nfp_prog *nfp_prog);
480561 int nfp_bpf_jit(struct nfp_prog *prog);
481562 bool nfp_bpf_supported_opcode(u8 code);
563
+bool nfp_bpf_offload_check_mtu(struct nfp_net *nn, struct bpf_prog *prog,
564
+ unsigned int mtu);
482565
483
-extern const struct bpf_prog_offload_ops nfp_bpf_analyzer_ops;
566
+int nfp_verify_insn(struct bpf_verifier_env *env, int insn_idx,
567
+ int prev_insn_idx);
568
+int nfp_bpf_finalize(struct bpf_verifier_env *env);
569
+
570
+int nfp_bpf_opt_replace_insn(struct bpf_verifier_env *env, u32 off,
571
+ struct bpf_insn *insn);
572
+int nfp_bpf_opt_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
573
+
574
+extern const struct bpf_prog_offload_ops nfp_bpf_dev_ops;
484575
485576 struct netdev_bpf;
486577 struct nfp_app;
....@@ -493,10 +584,13 @@
493584
494585 struct nfp_insn_meta *
495586 nfp_bpf_goto_meta(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
496
- unsigned int insn_idx, unsigned int n_insns);
587
+ unsigned int insn_idx);
497588
498589 void *nfp_bpf_relo_for_vnic(struct nfp_prog *nfp_prog, struct nfp_bpf_vnic *bv);
499590
591
+unsigned int nfp_bpf_ctrl_cmsg_min_mtu(struct nfp_app_bpf *bpf);
592
+unsigned int nfp_bpf_ctrl_cmsg_mtu(struct nfp_app_bpf *bpf);
593
+unsigned int nfp_bpf_ctrl_cmsg_cache_cnt(struct nfp_app_bpf *bpf);
500594 long long int
501595 nfp_bpf_ctrl_alloc_map(struct nfp_app_bpf *bpf, struct bpf_map *map);
502596 void