1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2016-2018 Netronome Systems, Inc. */
3 
4 /*
5  * nfp_net_offload.c
6  * Netronome network device driver: TC offload functions for PF and VF
7  */
8 
9 #define pr_fmt(fmt)	"NFP net bpf: " fmt
10 
11 #include <linux/bpf.h>
12 #include <linux/kernel.h>
13 #include <linux/netdevice.h>
14 #include <linux/pci.h>
15 #include <linux/jiffies.h>
16 #include <linux/timer.h>
17 #include <linux/list.h>
18 #include <linux/mm.h>
19 
20 #include <net/pkt_cls.h>
21 #include <net/tc_act/tc_gact.h>
22 #include <net/tc_act/tc_mirred.h>
23 
24 #include "main.h"
25 #include "../ccm.h"
26 #include "../nfp_app.h"
27 #include "../nfp_net_ctrl.h"
28 #include "../nfp_net.h"
29 
30 static int
31 nfp_map_ptr_record(struct nfp_app_bpf *bpf, struct nfp_prog *nfp_prog,
32 		   struct bpf_map *map)
33 {
34 	struct nfp_bpf_neutral_map *record;
35 	int err;
36 
37 	/* Reuse path - other offloaded program is already tracking this map. */
38 	record = rhashtable_lookup_fast(&bpf->maps_neutral, &map->id,
39 					nfp_bpf_maps_neutral_params);
40 	if (record) {
41 		nfp_prog->map_records[nfp_prog->map_records_cnt++] = record;
42 		record->count++;
43 		return 0;
44 	}
45 
46 	/* Grab a single ref to the map for our record.  The prog destroy ndo
47 	 * happens after free_used_maps().
48 	 */
49 	bpf_map_inc(map);
50 
51 	record = kmalloc(sizeof(*record), GFP_KERNEL);
52 	if (!record) {
53 		err = -ENOMEM;
54 		goto err_map_put;
55 	}
56 
57 	record->ptr = map;
58 	record->map_id = map->id;
59 	record->count = 1;
60 
61 	err = rhashtable_insert_fast(&bpf->maps_neutral, &record->l,
62 				     nfp_bpf_maps_neutral_params);
63 	if (err)
64 		goto err_free_rec;
65 
66 	nfp_prog->map_records[nfp_prog->map_records_cnt++] = record;
67 
68 	return 0;
69 
70 err_free_rec:
71 	kfree(record);
72 err_map_put:
73 	bpf_map_put(map);
74 	return err;
75 }
76 
77 static void
78 nfp_map_ptrs_forget(struct nfp_app_bpf *bpf, struct nfp_prog *nfp_prog)
79 {
80 	bool freed = false;
81 	int i;
82 
83 	for (i = 0; i < nfp_prog->map_records_cnt; i++) {
84 		if (--nfp_prog->map_records[i]->count) {
85 			nfp_prog->map_records[i] = NULL;
86 			continue;
87 		}
88 
89 		WARN_ON(rhashtable_remove_fast(&bpf->maps_neutral,
90 					       &nfp_prog->map_records[i]->l,
91 					       nfp_bpf_maps_neutral_params));
92 		freed = true;
93 	}
94 
95 	if (freed) {
96 		synchronize_rcu();
97 
98 		for (i = 0; i < nfp_prog->map_records_cnt; i++)
99 			if (nfp_prog->map_records[i]) {
100 				bpf_map_put(nfp_prog->map_records[i]->ptr);
101 				kfree(nfp_prog->map_records[i]);
102 			}
103 	}
104 
105 	kfree(nfp_prog->map_records);
106 	nfp_prog->map_records = NULL;
107 	nfp_prog->map_records_cnt = 0;
108 }
109 
110 static int
111 nfp_map_ptrs_record(struct nfp_app_bpf *bpf, struct nfp_prog *nfp_prog,
112 		    struct bpf_prog *prog)
113 {
114 	int i, cnt, err;
115 
116 	/* Quickly count the maps we will have to remember */
117 	cnt = 0;
118 	for (i = 0; i < prog->aux->used_map_cnt; i++)
119 		if (bpf_map_offload_neutral(prog->aux->used_maps[i]))
120 			cnt++;
121 	if (!cnt)
122 		return 0;
123 
124 	nfp_prog->map_records = kmalloc_array(cnt,
125 					      sizeof(nfp_prog->map_records[0]),
126 					      GFP_KERNEL);
127 	if (!nfp_prog->map_records)
128 		return -ENOMEM;
129 
130 	for (i = 0; i < prog->aux->used_map_cnt; i++)
131 		if (bpf_map_offload_neutral(prog->aux->used_maps[i])) {
132 			err = nfp_map_ptr_record(bpf, nfp_prog,
133 						 prog->aux->used_maps[i]);
134 			if (err) {
135 				nfp_map_ptrs_forget(bpf, nfp_prog);
136 				return err;
137 			}
138 		}
139 	WARN_ON(cnt != nfp_prog->map_records_cnt);
140 
141 	return 0;
142 }
143 
144 static int
145 nfp_prog_prepare(struct nfp_prog *nfp_prog, const struct bpf_insn *prog,
146 		 unsigned int cnt)
147 {
148 	struct nfp_insn_meta *meta;
149 	unsigned int i;
150 
151 	for (i = 0; i < cnt; i++) {
152 		meta = kzalloc(sizeof(*meta), GFP_KERNEL);
153 		if (!meta)
154 			return -ENOMEM;
155 
156 		meta->insn = prog[i];
157 		meta->n = i;
158 		if (is_mbpf_alu(meta)) {
159 			meta->umin_src = U64_MAX;
160 			meta->umin_dst = U64_MAX;
161 		}
162 
163 		list_add_tail(&meta->l, &nfp_prog->insns);
164 	}
165 	nfp_prog->n_insns = cnt;
166 
167 	nfp_bpf_jit_prepare(nfp_prog);
168 
169 	return 0;
170 }
171 
172 static void nfp_prog_free(struct nfp_prog *nfp_prog)
173 {
174 	struct nfp_insn_meta *meta, *tmp;
175 
176 	kfree(nfp_prog->subprog);
177 
178 	list_for_each_entry_safe(meta, tmp, &nfp_prog->insns, l) {
179 		list_del(&meta->l);
180 		kfree(meta);
181 	}
182 	kfree(nfp_prog);
183 }
184 
185 static int nfp_bpf_verifier_prep(struct bpf_prog *prog)
186 {
187 	struct nfp_prog *nfp_prog;
188 	int ret;
189 
190 	nfp_prog = kzalloc(sizeof(*nfp_prog), GFP_KERNEL);
191 	if (!nfp_prog)
192 		return -ENOMEM;
193 	prog->aux->offload->dev_priv = nfp_prog;
194 
195 	INIT_LIST_HEAD(&nfp_prog->insns);
196 	nfp_prog->type = prog->type;
197 	nfp_prog->bpf = bpf_offload_dev_priv(prog->aux->offload->offdev);
198 
199 	ret = nfp_prog_prepare(nfp_prog, prog->insnsi, prog->len);
200 	if (ret)
201 		goto err_free;
202 
203 	nfp_prog->verifier_meta = nfp_prog_first_meta(nfp_prog);
204 
205 	return 0;
206 
207 err_free:
208 	nfp_prog_free(nfp_prog);
209 
210 	return ret;
211 }
212 
213 static int nfp_bpf_translate(struct bpf_prog *prog)
214 {
215 	struct nfp_net *nn = netdev_priv(prog->aux->offload->netdev);
216 	struct nfp_prog *nfp_prog = prog->aux->offload->dev_priv;
217 	unsigned int max_instr;
218 	int err;
219 
220 	/* We depend on dead code elimination succeeding */
221 	if (prog->aux->offload->opt_failed)
222 		return -EINVAL;
223 
224 	max_instr = nn_readw(nn, NFP_NET_CFG_BPF_MAX_LEN);
225 	nfp_prog->__prog_alloc_len = max_instr * sizeof(u64);
226 
227 	nfp_prog->prog = kvmalloc(nfp_prog->__prog_alloc_len, GFP_KERNEL);
228 	if (!nfp_prog->prog)
229 		return -ENOMEM;
230 
231 	err = nfp_bpf_jit(nfp_prog);
232 	if (err)
233 		return err;
234 
235 	prog->aux->offload->jited_len = nfp_prog->prog_len * sizeof(u64);
236 	prog->aux->offload->jited_image = nfp_prog->prog;
237 
238 	return nfp_map_ptrs_record(nfp_prog->bpf, nfp_prog, prog);
239 }
240 
241 static void nfp_bpf_destroy(struct bpf_prog *prog)
242 {
243 	struct nfp_prog *nfp_prog = prog->aux->offload->dev_priv;
244 
245 	kvfree(nfp_prog->prog);
246 	nfp_map_ptrs_forget(nfp_prog->bpf, nfp_prog);
247 	nfp_prog_free(nfp_prog);
248 }
249 
250 /* Atomic engine requires values to be in big endian, we need to byte swap
251  * the value words used with xadd.
252  */
253 static void nfp_map_bpf_byte_swap(struct nfp_bpf_map *nfp_map, void *value)
254 {
255 	u32 *word = value;
256 	unsigned int i;
257 
258 	for (i = 0; i < DIV_ROUND_UP(nfp_map->offmap->map.value_size, 4); i++)
259 		if (nfp_map->use_map[i].type == NFP_MAP_USE_ATOMIC_CNT)
260 			word[i] = (__force u32)cpu_to_be32(word[i]);
261 }
262 
263 /* Mark value as unsafely initialized in case it becomes atomic later
264  * and we didn't byte swap something non-byte swap neutral.
265  */
266 static void
267 nfp_map_bpf_byte_swap_record(struct nfp_bpf_map *nfp_map, void *value)
268 {
269 	u32 *word = value;
270 	unsigned int i;
271 
272 	for (i = 0; i < DIV_ROUND_UP(nfp_map->offmap->map.value_size, 4); i++)
273 		if (nfp_map->use_map[i].type == NFP_MAP_UNUSED &&
274 		    word[i] != (__force u32)cpu_to_be32(word[i]))
275 			nfp_map->use_map[i].non_zero_update = 1;
276 }
277 
278 static int
279 nfp_bpf_map_lookup_entry(struct bpf_offloaded_map *offmap,
280 			 void *key, void *value)
281 {
282 	int err;
283 
284 	err = nfp_bpf_ctrl_lookup_entry(offmap, key, value);
285 	if (err)
286 		return err;
287 
288 	nfp_map_bpf_byte_swap(offmap->dev_priv, value);
289 	return 0;
290 }
291 
292 static int
293 nfp_bpf_map_update_entry(struct bpf_offloaded_map *offmap,
294 			 void *key, void *value, u64 flags)
295 {
296 	nfp_map_bpf_byte_swap(offmap->dev_priv, value);
297 	nfp_map_bpf_byte_swap_record(offmap->dev_priv, value);
298 	return nfp_bpf_ctrl_update_entry(offmap, key, value, flags);
299 }
300 
301 static int
302 nfp_bpf_map_get_next_key(struct bpf_offloaded_map *offmap,
303 			 void *key, void *next_key)
304 {
305 	if (!key)
306 		return nfp_bpf_ctrl_getfirst_entry(offmap, next_key);
307 	return nfp_bpf_ctrl_getnext_entry(offmap, key, next_key);
308 }
309 
310 static int
311 nfp_bpf_map_delete_elem(struct bpf_offloaded_map *offmap, void *key)
312 {
313 	if (offmap->map.map_type == BPF_MAP_TYPE_ARRAY)
314 		return -EINVAL;
315 	return nfp_bpf_ctrl_del_entry(offmap, key);
316 }
317 
318 static const struct bpf_map_dev_ops nfp_bpf_map_ops = {
319 	.map_get_next_key	= nfp_bpf_map_get_next_key,
320 	.map_lookup_elem	= nfp_bpf_map_lookup_entry,
321 	.map_update_elem	= nfp_bpf_map_update_entry,
322 	.map_delete_elem	= nfp_bpf_map_delete_elem,
323 };
324 
325 static int
326 nfp_bpf_map_alloc(struct nfp_app_bpf *bpf, struct bpf_offloaded_map *offmap)
327 {
328 	struct nfp_bpf_map *nfp_map;
329 	unsigned int use_map_size;
330 	long long int res;
331 
332 	if (!bpf->maps.types)
333 		return -EOPNOTSUPP;
334 
335 	if (offmap->map.map_flags ||
336 	    offmap->map.numa_node != NUMA_NO_NODE) {
337 		pr_info("map flags are not supported\n");
338 		return -EINVAL;
339 	}
340 
341 	if (!(bpf->maps.types & 1 << offmap->map.map_type)) {
342 		pr_info("map type not supported\n");
343 		return -EOPNOTSUPP;
344 	}
345 	if (bpf->maps.max_maps == bpf->maps_in_use) {
346 		pr_info("too many maps for a device\n");
347 		return -ENOMEM;
348 	}
349 	if (bpf->maps.max_elems - bpf->map_elems_in_use <
350 	    offmap->map.max_entries) {
351 		pr_info("map with too many elements: %u, left: %u\n",
352 			offmap->map.max_entries,
353 			bpf->maps.max_elems - bpf->map_elems_in_use);
354 		return -ENOMEM;
355 	}
356 
357 	if (round_up(offmap->map.key_size, 8) +
358 	    round_up(offmap->map.value_size, 8) > bpf->maps.max_elem_sz) {
359 		pr_info("map elements too large: %u, FW max element size (key+value): %u\n",
360 			round_up(offmap->map.key_size, 8) +
361 			round_up(offmap->map.value_size, 8),
362 			bpf->maps.max_elem_sz);
363 		return -ENOMEM;
364 	}
365 	if (offmap->map.key_size > bpf->maps.max_key_sz) {
366 		pr_info("map key size %u, FW max is %u\n",
367 			offmap->map.key_size, bpf->maps.max_key_sz);
368 		return -ENOMEM;
369 	}
370 	if (offmap->map.value_size > bpf->maps.max_val_sz) {
371 		pr_info("map value size %u, FW max is %u\n",
372 			offmap->map.value_size, bpf->maps.max_val_sz);
373 		return -ENOMEM;
374 	}
375 
376 	use_map_size = DIV_ROUND_UP(offmap->map.value_size, 4) *
377 		       sizeof_field(struct nfp_bpf_map, use_map[0]);
378 
379 	nfp_map = kzalloc(sizeof(*nfp_map) + use_map_size, GFP_USER);
380 	if (!nfp_map)
381 		return -ENOMEM;
382 
383 	offmap->dev_priv = nfp_map;
384 	nfp_map->offmap = offmap;
385 	nfp_map->bpf = bpf;
386 	spin_lock_init(&nfp_map->cache_lock);
387 
388 	res = nfp_bpf_ctrl_alloc_map(bpf, &offmap->map);
389 	if (res < 0) {
390 		kfree(nfp_map);
391 		return res;
392 	}
393 
394 	nfp_map->tid = res;
395 	offmap->dev_ops = &nfp_bpf_map_ops;
396 	bpf->maps_in_use++;
397 	bpf->map_elems_in_use += offmap->map.max_entries;
398 	list_add_tail(&nfp_map->l, &bpf->map_list);
399 
400 	return 0;
401 }
402 
403 static int
404 nfp_bpf_map_free(struct nfp_app_bpf *bpf, struct bpf_offloaded_map *offmap)
405 {
406 	struct nfp_bpf_map *nfp_map = offmap->dev_priv;
407 
408 	nfp_bpf_ctrl_free_map(bpf, nfp_map);
409 	dev_consume_skb_any(nfp_map->cache);
410 	WARN_ON_ONCE(nfp_map->cache_blockers);
411 	list_del_init(&nfp_map->l);
412 	bpf->map_elems_in_use -= offmap->map.max_entries;
413 	bpf->maps_in_use--;
414 	kfree(nfp_map);
415 
416 	return 0;
417 }
418 
419 int nfp_ndo_bpf(struct nfp_app *app, struct nfp_net *nn, struct netdev_bpf *bpf)
420 {
421 	switch (bpf->command) {
422 	case BPF_OFFLOAD_MAP_ALLOC:
423 		return nfp_bpf_map_alloc(app->priv, bpf->offmap);
424 	case BPF_OFFLOAD_MAP_FREE:
425 		return nfp_bpf_map_free(app->priv, bpf->offmap);
426 	default:
427 		return -EINVAL;
428 	}
429 }
430 
431 static unsigned long
432 nfp_bpf_perf_event_copy(void *dst, const void *src,
433 			unsigned long off, unsigned long len)
434 {
435 	memcpy(dst, src + off, len);
436 	return 0;
437 }
438 
439 int nfp_bpf_event_output(struct nfp_app_bpf *bpf, const void *data,
440 			 unsigned int len)
441 {
442 	struct cmsg_bpf_event *cbe = (void *)data;
443 	struct nfp_bpf_neutral_map *record;
444 	u32 pkt_size, data_size, map_id;
445 	u64 map_id_full;
446 
447 	if (len < sizeof(struct cmsg_bpf_event))
448 		return -EINVAL;
449 
450 	pkt_size = be32_to_cpu(cbe->pkt_size);
451 	data_size = be32_to_cpu(cbe->data_size);
452 	map_id_full = be64_to_cpu(cbe->map_ptr);
453 	map_id = map_id_full;
454 
455 	if (len < sizeof(struct cmsg_bpf_event) + pkt_size + data_size)
456 		return -EINVAL;
457 	if (cbe->hdr.ver != NFP_CCM_ABI_VERSION)
458 		return -EINVAL;
459 
460 	rcu_read_lock();
461 	record = rhashtable_lookup(&bpf->maps_neutral, &map_id,
462 				   nfp_bpf_maps_neutral_params);
463 	if (!record || map_id_full > U32_MAX) {
464 		rcu_read_unlock();
465 		cmsg_warn(bpf, "perf event: map id %lld (0x%llx) not recognized, dropping event\n",
466 			  map_id_full, map_id_full);
467 		return -EINVAL;
468 	}
469 
470 	bpf_event_output(record->ptr, be32_to_cpu(cbe->cpu_id),
471 			 &cbe->data[round_up(pkt_size, 4)], data_size,
472 			 cbe->data, pkt_size, nfp_bpf_perf_event_copy);
473 	rcu_read_unlock();
474 
475 	return 0;
476 }
477 
478 static int
479 nfp_net_bpf_load(struct nfp_net *nn, struct bpf_prog *prog,
480 		 struct netlink_ext_ack *extack)
481 {
482 	struct nfp_prog *nfp_prog = prog->aux->offload->dev_priv;
483 	unsigned int fw_mtu, pkt_off, max_stack, max_prog_len;
484 	dma_addr_t dma_addr;
485 	void *img;
486 	int err;
487 
488 	fw_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
489 	pkt_off = min(prog->aux->max_pkt_offset, nn->dp.netdev->mtu);
490 	if (fw_mtu < pkt_off) {
491 		NL_SET_ERR_MSG_MOD(extack, "BPF offload not supported with potential packet access beyond HW packet split boundary");
492 		return -EOPNOTSUPP;
493 	}
494 
495 	max_stack = nn_readb(nn, NFP_NET_CFG_BPF_STACK_SZ) * 64;
496 	if (nfp_prog->stack_size > max_stack) {
497 		NL_SET_ERR_MSG_MOD(extack, "stack too large");
498 		return -EOPNOTSUPP;
499 	}
500 
501 	max_prog_len = nn_readw(nn, NFP_NET_CFG_BPF_MAX_LEN);
502 	if (nfp_prog->prog_len > max_prog_len) {
503 		NL_SET_ERR_MSG_MOD(extack, "program too long");
504 		return -EOPNOTSUPP;
505 	}
506 
507 	img = nfp_bpf_relo_for_vnic(nfp_prog, nn->app_priv);
508 	if (IS_ERR(img))
509 		return PTR_ERR(img);
510 
511 	dma_addr = dma_map_single(nn->dp.dev, img,
512 				  nfp_prog->prog_len * sizeof(u64),
513 				  DMA_TO_DEVICE);
514 	if (dma_mapping_error(nn->dp.dev, dma_addr)) {
515 		kfree(img);
516 		return -ENOMEM;
517 	}
518 
519 	nn_writew(nn, NFP_NET_CFG_BPF_SIZE, nfp_prog->prog_len);
520 	nn_writeq(nn, NFP_NET_CFG_BPF_ADDR, dma_addr);
521 
522 	/* Load up the JITed code */
523 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_BPF);
524 	if (err)
525 		NL_SET_ERR_MSG_MOD(extack,
526 				   "FW command error while loading BPF");
527 
528 	dma_unmap_single(nn->dp.dev, dma_addr, nfp_prog->prog_len * sizeof(u64),
529 			 DMA_TO_DEVICE);
530 	kfree(img);
531 
532 	return err;
533 }
534 
535 static void
536 nfp_net_bpf_start(struct nfp_net *nn, struct netlink_ext_ack *extack)
537 {
538 	int err;
539 
540 	/* Enable passing packets through BPF function */
541 	nn->dp.ctrl |= NFP_NET_CFG_CTRL_BPF;
542 	nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl);
543 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
544 	if (err)
545 		NL_SET_ERR_MSG_MOD(extack,
546 				   "FW command error while enabling BPF");
547 }
548 
549 static int nfp_net_bpf_stop(struct nfp_net *nn)
550 {
551 	if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF))
552 		return 0;
553 
554 	nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_BPF;
555 	nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl);
556 
557 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
558 }
559 
560 int nfp_net_bpf_offload(struct nfp_net *nn, struct bpf_prog *prog,
561 			bool old_prog, struct netlink_ext_ack *extack)
562 {
563 	int err;
564 
565 	if (prog && !bpf_offload_dev_match(prog, nn->dp.netdev))
566 		return -EINVAL;
567 
568 	if (prog && old_prog) {
569 		u8 cap;
570 
571 		cap = nn_readb(nn, NFP_NET_CFG_BPF_CAP);
572 		if (!(cap & NFP_NET_BPF_CAP_RELO)) {
573 			NL_SET_ERR_MSG_MOD(extack,
574 					   "FW does not support live reload");
575 			return -EBUSY;
576 		}
577 	}
578 
579 	/* Something else is loaded, different program type? */
580 	if (!old_prog && nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
581 		return -EBUSY;
582 
583 	if (old_prog && !prog)
584 		return nfp_net_bpf_stop(nn);
585 
586 	err = nfp_net_bpf_load(nn, prog, extack);
587 	if (err)
588 		return err;
589 
590 	if (!old_prog)
591 		nfp_net_bpf_start(nn, extack);
592 
593 	return 0;
594 }
595 
596 const struct bpf_prog_offload_ops nfp_bpf_dev_ops = {
597 	.insn_hook	= nfp_verify_insn,
598 	.finalize	= nfp_bpf_finalize,
599 	.replace_insn	= nfp_bpf_opt_replace_insn,
600 	.remove_insns	= nfp_bpf_opt_remove_insns,
601 	.prepare	= nfp_bpf_verifier_prep,
602 	.translate	= nfp_bpf_translate,
603 	.destroy	= nfp_bpf_destroy,
604 };
605