1 /* 2 * Copyright (C) 2016-2017 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 */ 33 34 #ifndef __NFP_BPF_H__ 35 #define __NFP_BPF_H__ 1 36 37 #include <linux/bitfield.h> 38 #include <linux/bpf.h> 39 #include <linux/bpf_verifier.h> 40 #include <linux/kernel.h> 41 #include <linux/list.h> 42 #include <linux/skbuff.h> 43 #include <linux/types.h> 44 #include <linux/wait.h> 45 46 #include "../nfp_asm.h" 47 #include "fw.h" 48 49 /* For relocation logic use up-most byte of branch instruction as scratch 50 * area. Remember to clear this before sending instructions to HW! 51 */ 52 #define OP_RELO_TYPE 0xff00000000000000ULL 53 54 enum nfp_relo_type { 55 RELO_NONE = 0, 56 /* standard internal jumps */ 57 RELO_BR_REL, 58 /* internal jumps to parts of the outro */ 59 RELO_BR_GO_OUT, 60 RELO_BR_GO_ABORT, 61 /* external jumps to fixed addresses */ 62 RELO_BR_NEXT_PKT, 63 }; 64 65 /* To make absolute relocated branches (branches other than RELO_BR_REL) 66 * distinguishable in user space dumps from normal jumps, add a large offset 67 * to them. 68 */ 69 #define BR_OFF_RELO 15000 70 71 enum static_regs { 72 STATIC_REG_IMM = 21, /* Bank AB */ 73 STATIC_REG_STACK = 22, /* Bank A */ 74 STATIC_REG_PKT_LEN = 22, /* Bank B */ 75 }; 76 77 enum pkt_vec { 78 PKT_VEC_PKT_LEN = 0, 79 PKT_VEC_PKT_PTR = 2, 80 }; 81 82 #define pv_len(np) reg_lm(1, PKT_VEC_PKT_LEN) 83 #define pv_ctm_ptr(np) reg_lm(1, PKT_VEC_PKT_PTR) 84 85 #define stack_reg(np) reg_a(STATIC_REG_STACK) 86 #define stack_imm(np) imm_b(np) 87 #define plen_reg(np) reg_b(STATIC_REG_PKT_LEN) 88 #define pptr_reg(np) pv_ctm_ptr(np) 89 #define imm_a(np) reg_a(STATIC_REG_IMM) 90 #define imm_b(np) reg_b(STATIC_REG_IMM) 91 #define imm_both(np) reg_both(STATIC_REG_IMM) 92 93 #define NFP_BPF_ABI_FLAGS reg_imm(0) 94 #define NFP_BPF_ABI_FLAG_MARK 1 95 96 /** 97 * struct nfp_app_bpf - bpf app priv structure 98 * @app: backpointer to the app 99 * 100 * @tag_allocator: bitmap of control message tags in use 101 * @tag_alloc_next: next tag bit to allocate 102 * @tag_alloc_last: next tag bit to be freed 103 * 104 * @cmsg_replies: received cmsg replies waiting to be consumed 105 * @cmsg_wq: work queue for waiting for cmsg replies 106 * 107 * @map_list: list of offloaded maps 108 * 109 * @adjust_head: adjust head capability 110 * @flags: extra flags for adjust head 111 * @off_min: minimal packet offset within buffer required 112 * @off_max: maximum packet offset within buffer required 113 * @guaranteed_sub: amount of negative adjustment guaranteed possible 114 * @guaranteed_add: amount of positive adjustment guaranteed possible 115 * 116 * @maps: map capability 117 * @types: supported map types 118 * @max_maps: max number of maps supported 119 * @max_elems: max number of entries in each map 120 * @max_key_sz: max size of map key 121 * @max_val_sz: max size of map value 122 * @max_elem_sz: max size of map entry (key + value) 123 * 124 * @helpers: helper addressess for various calls 125 * @map_lookup: map lookup helper address 126 */ 127 struct nfp_app_bpf { 128 struct nfp_app *app; 129 130 DECLARE_BITMAP(tag_allocator, U16_MAX + 1); 131 u16 tag_alloc_next; 132 u16 tag_alloc_last; 133 134 struct sk_buff_head cmsg_replies; 135 struct wait_queue_head cmsg_wq; 136 137 struct list_head map_list; 138 139 struct nfp_bpf_cap_adjust_head { 140 u32 flags; 141 int off_min; 142 int off_max; 143 int guaranteed_sub; 144 int guaranteed_add; 145 } adjust_head; 146 147 struct { 148 u32 types; 149 u32 max_maps; 150 u32 max_elems; 151 u32 max_key_sz; 152 u32 max_val_sz; 153 u32 max_elem_sz; 154 } maps; 155 156 struct { 157 u32 map_lookup; 158 } helpers; 159 }; 160 161 /** 162 * struct nfp_bpf_map - private per-map data attached to BPF maps for offload 163 * @offmap: pointer to the offloaded BPF map 164 * @bpf: back pointer to bpf app private structure 165 * @tid: table id identifying map on datapath 166 * @l: link on the nfp_app_bpf->map_list list 167 */ 168 struct nfp_bpf_map { 169 struct bpf_offloaded_map *offmap; 170 struct nfp_app_bpf *bpf; 171 u32 tid; 172 struct list_head l; 173 }; 174 175 struct nfp_prog; 176 struct nfp_insn_meta; 177 typedef int (*instr_cb_t)(struct nfp_prog *, struct nfp_insn_meta *); 178 179 #define nfp_prog_first_meta(nfp_prog) \ 180 list_first_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l) 181 #define nfp_prog_last_meta(nfp_prog) \ 182 list_last_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l) 183 #define nfp_meta_next(meta) list_next_entry(meta, l) 184 #define nfp_meta_prev(meta) list_prev_entry(meta, l) 185 186 #define FLAG_INSN_IS_JUMP_DST BIT(0) 187 188 /** 189 * struct nfp_insn_meta - BPF instruction wrapper 190 * @insn: BPF instruction 191 * @ptr: pointer type for memory operations 192 * @ldst_gather_len: memcpy length gathered from load/store sequence 193 * @paired_st: the paired store insn at the head of the sequence 194 * @arg2: arg2 for call instructions 195 * @ptr_not_const: pointer is not always constant 196 * @jmp_dst: destination info for jump instructions 197 * @off: index of first generated machine instruction (in nfp_prog.prog) 198 * @n: eBPF instruction number 199 * @flags: eBPF instruction extra optimization flags 200 * @skip: skip this instruction (optimized out) 201 * @double_cb: callback for second part of the instruction 202 * @l: link on nfp_prog->insns list 203 */ 204 struct nfp_insn_meta { 205 struct bpf_insn insn; 206 union { 207 struct { 208 struct bpf_reg_state ptr; 209 struct bpf_insn *paired_st; 210 s16 ldst_gather_len; 211 bool ptr_not_const; 212 }; 213 struct nfp_insn_meta *jmp_dst; 214 struct bpf_reg_state arg2; 215 }; 216 unsigned int off; 217 unsigned short n; 218 unsigned short flags; 219 bool skip; 220 instr_cb_t double_cb; 221 222 struct list_head l; 223 }; 224 225 #define BPF_SIZE_MASK 0x18 226 227 static inline u8 mbpf_class(const struct nfp_insn_meta *meta) 228 { 229 return BPF_CLASS(meta->insn.code); 230 } 231 232 static inline u8 mbpf_src(const struct nfp_insn_meta *meta) 233 { 234 return BPF_SRC(meta->insn.code); 235 } 236 237 static inline u8 mbpf_op(const struct nfp_insn_meta *meta) 238 { 239 return BPF_OP(meta->insn.code); 240 } 241 242 static inline u8 mbpf_mode(const struct nfp_insn_meta *meta) 243 { 244 return BPF_MODE(meta->insn.code); 245 } 246 247 static inline bool is_mbpf_load(const struct nfp_insn_meta *meta) 248 { 249 return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_LDX | BPF_MEM); 250 } 251 252 static inline bool is_mbpf_store(const struct nfp_insn_meta *meta) 253 { 254 return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_MEM); 255 } 256 257 /** 258 * struct nfp_prog - nfp BPF program 259 * @bpf: backpointer to the bpf app priv structure 260 * @prog: machine code 261 * @prog_len: number of valid instructions in @prog array 262 * @__prog_alloc_len: alloc size of @prog array 263 * @verifier_meta: temporary storage for verifier's insn meta 264 * @type: BPF program type 265 * @last_bpf_off: address of the last instruction translated from BPF 266 * @tgt_out: jump target for normal exit 267 * @tgt_abort: jump target for abort (e.g. access outside of packet buffer) 268 * @n_translated: number of successfully translated instructions (for errors) 269 * @error: error code if something went wrong 270 * @stack_depth: max stack depth from the verifier 271 * @adjust_head_location: if program has single adjust head call - the insn no. 272 * @insns: list of BPF instruction wrappers (struct nfp_insn_meta) 273 */ 274 struct nfp_prog { 275 struct nfp_app_bpf *bpf; 276 277 u64 *prog; 278 unsigned int prog_len; 279 unsigned int __prog_alloc_len; 280 281 struct nfp_insn_meta *verifier_meta; 282 283 enum bpf_prog_type type; 284 285 unsigned int last_bpf_off; 286 unsigned int tgt_out; 287 unsigned int tgt_abort; 288 289 unsigned int n_translated; 290 int error; 291 292 unsigned int stack_depth; 293 unsigned int adjust_head_location; 294 295 struct list_head insns; 296 }; 297 298 /** 299 * struct nfp_bpf_vnic - per-vNIC BPF priv structure 300 * @tc_prog: currently loaded cls_bpf program 301 * @start_off: address of the first instruction in the memory 302 * @tgt_done: jump target to get the next packet 303 */ 304 struct nfp_bpf_vnic { 305 struct bpf_prog *tc_prog; 306 unsigned int start_off; 307 unsigned int tgt_done; 308 }; 309 310 void nfp_bpf_jit_prepare(struct nfp_prog *nfp_prog, unsigned int cnt); 311 int nfp_bpf_jit(struct nfp_prog *prog); 312 313 extern const struct bpf_prog_offload_ops nfp_bpf_analyzer_ops; 314 315 struct netdev_bpf; 316 struct nfp_app; 317 struct nfp_net; 318 319 int nfp_ndo_bpf(struct nfp_app *app, struct nfp_net *nn, 320 struct netdev_bpf *bpf); 321 int nfp_net_bpf_offload(struct nfp_net *nn, struct bpf_prog *prog, 322 bool old_prog); 323 324 struct nfp_insn_meta * 325 nfp_bpf_goto_meta(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta, 326 unsigned int insn_idx, unsigned int n_insns); 327 328 void *nfp_bpf_relo_for_vnic(struct nfp_prog *nfp_prog, struct nfp_bpf_vnic *bv); 329 330 long long int 331 nfp_bpf_ctrl_alloc_map(struct nfp_app_bpf *bpf, struct bpf_map *map); 332 void 333 nfp_bpf_ctrl_free_map(struct nfp_app_bpf *bpf, struct nfp_bpf_map *nfp_map); 334 int nfp_bpf_ctrl_getfirst_entry(struct bpf_offloaded_map *offmap, 335 void *next_key); 336 int nfp_bpf_ctrl_update_entry(struct bpf_offloaded_map *offmap, 337 void *key, void *value, u64 flags); 338 int nfp_bpf_ctrl_del_entry(struct bpf_offloaded_map *offmap, void *key); 339 int nfp_bpf_ctrl_lookup_entry(struct bpf_offloaded_map *offmap, 340 void *key, void *value); 341 int nfp_bpf_ctrl_getnext_entry(struct bpf_offloaded_map *offmap, 342 void *key, void *next_key); 343 344 void nfp_bpf_ctrl_msg_rx(struct nfp_app *app, struct sk_buff *skb); 345 #endif 346