1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
13 #include <sys/types.h>
16 #include <sys/queue.h>
18 #include <rte_common.h>
19 #include <rte_byteorder.h>
20 #include <rte_malloc.h>
22 #include <rte_debug.h>
23 #include <rte_cycles.h>
25 #include <rte_per_lcore.h>
26 #include <rte_lcore.h>
27 #include <rte_atomic.h>
29 #include <rte_ethdev.h>
31 #include <rte_bpf_ethdev.h>
35 * information about installed BPF rx/tx callback
39 /* used by both data & control path */
40 uint32_t use; /*usage counter */
41 const struct rte_eth_rxtx_callback *cb; /* callback handle */
43 struct rte_bpf_jit jit;
44 /* used by control path only */
45 LIST_ENTRY(bpf_eth_cbi) link;
48 } __rte_cache_aligned;
51 * Odd number means that callback is used by datapath.
52 * Even number means that callback is not used by datapath.
54 #define BPF_ETH_CBI_INUSE 1
57 * List to manage RX/TX installed callbacks.
59 LIST_HEAD(bpf_eth_cbi_list, bpf_eth_cbi);
68 * information about all installed BPF rx/tx callbacks
72 struct bpf_eth_cbi_list list;
76 static struct bpf_eth_cbh rx_cbh = {
77 .lock = RTE_SPINLOCK_INITIALIZER,
78 .list = LIST_HEAD_INITIALIZER(list),
82 static struct bpf_eth_cbh tx_cbh = {
83 .lock = RTE_SPINLOCK_INITIALIZER,
84 .list = LIST_HEAD_INITIALIZER(list),
89 * Marks given callback as used by datapath.
91 static __rte_always_inline void
92 bpf_eth_cbi_inuse(struct bpf_eth_cbi *cbi)
95 /* make sure no store/load reordering could happen */
100 * Marks given callback list as not used by datapath.
102 static __rte_always_inline void
103 bpf_eth_cbi_unuse(struct bpf_eth_cbi *cbi)
105 /* make sure all previous loads are completed */
111 * Waits till datapath finished using given callback.
114 bpf_eth_cbi_wait(const struct bpf_eth_cbi *cbi)
118 /* make sure all previous loads and stores are completed */
123 /* in use, busy wait till current RX/TX iteration is finished */
124 if ((puse & BPF_ETH_CBI_INUSE) != 0) {
127 rte_compiler_barrier();
129 } while (nuse == puse);
134 bpf_eth_cbi_cleanup(struct bpf_eth_cbi *bc)
137 memset(&bc->jit, 0, sizeof(bc->jit));
140 static struct bpf_eth_cbi *
141 bpf_eth_cbh_find(struct bpf_eth_cbh *cbh, uint16_t port, uint16_t queue)
143 struct bpf_eth_cbi *cbi;
145 LIST_FOREACH(cbi, &cbh->list, link) {
146 if (cbi->port == port && cbi->queue == queue)
152 static struct bpf_eth_cbi *
153 bpf_eth_cbh_add(struct bpf_eth_cbh *cbh, uint16_t port, uint16_t queue)
155 struct bpf_eth_cbi *cbi;
157 /* return an existing one */
158 cbi = bpf_eth_cbh_find(cbh, port, queue);
162 cbi = rte_zmalloc(NULL, sizeof(*cbi), RTE_CACHE_LINE_SIZE);
166 LIST_INSERT_HEAD(&cbh->list, cbi, link);
172 * BPF packet processing routinies.
175 static inline uint32_t
176 apply_filter(struct rte_mbuf *mb[], const uint64_t rc[], uint32_t num,
180 struct rte_mbuf *dr[num];
182 for (i = 0, j = 0, k = 0; i != num; i++) {
193 /* free filtered out mbufs */
194 for (i = 0; i != k; i++)
195 rte_pktmbuf_free(dr[i]);
197 /* copy filtered out mbufs beyond good ones */
198 for (i = 0; i != k; i++)
205 static inline uint32_t
206 pkt_filter_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num,
213 for (i = 0; i != num; i++)
214 dp[i] = rte_pktmbuf_mtod(mb[i], void *);
216 rte_bpf_exec_burst(bpf, dp, rc, num);
217 return apply_filter(mb, rc, num, drop);
220 static inline uint32_t
221 pkt_filter_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[],
222 uint32_t num, uint32_t drop)
229 for (i = 0; i != num; i++) {
230 dp = rte_pktmbuf_mtod(mb[i], void *);
231 rc[i] = jit->func(dp);
236 num = apply_filter(mb, rc, num, drop);
241 static inline uint32_t
242 pkt_filter_mb_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num,
247 rte_bpf_exec_burst(bpf, (void **)mb, rc, num);
248 return apply_filter(mb, rc, num, drop);
251 static inline uint32_t
252 pkt_filter_mb_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[],
253 uint32_t num, uint32_t drop)
259 for (i = 0; i != num; i++) {
260 rc[i] = jit->func(mb[i]);
265 num = apply_filter(mb, rc, num, drop);
271 * RX/TX callbacks for raw data bpf.
275 bpf_rx_callback_vm(__rte_unused uint16_t port, __rte_unused uint16_t queue,
276 struct rte_mbuf *pkt[], uint16_t nb_pkts,
277 __rte_unused uint16_t max_pkts, void *user_param)
279 struct bpf_eth_cbi *cbi;
284 bpf_eth_cbi_inuse(cbi);
285 rc = (cbi->cb != NULL) ?
286 pkt_filter_vm(cbi->bpf, pkt, nb_pkts, 1) :
288 bpf_eth_cbi_unuse(cbi);
293 bpf_rx_callback_jit(__rte_unused uint16_t port, __rte_unused uint16_t queue,
294 struct rte_mbuf *pkt[], uint16_t nb_pkts,
295 __rte_unused uint16_t max_pkts, void *user_param)
297 struct bpf_eth_cbi *cbi;
301 bpf_eth_cbi_inuse(cbi);
302 rc = (cbi->cb != NULL) ?
303 pkt_filter_jit(&cbi->jit, pkt, nb_pkts, 1) :
305 bpf_eth_cbi_unuse(cbi);
310 bpf_tx_callback_vm(__rte_unused uint16_t port, __rte_unused uint16_t queue,
311 struct rte_mbuf *pkt[], uint16_t nb_pkts, void *user_param)
313 struct bpf_eth_cbi *cbi;
317 bpf_eth_cbi_inuse(cbi);
318 rc = (cbi->cb != NULL) ?
319 pkt_filter_vm(cbi->bpf, pkt, nb_pkts, 0) :
321 bpf_eth_cbi_unuse(cbi);
326 bpf_tx_callback_jit(__rte_unused uint16_t port, __rte_unused uint16_t queue,
327 struct rte_mbuf *pkt[], uint16_t nb_pkts, void *user_param)
329 struct bpf_eth_cbi *cbi;
333 bpf_eth_cbi_inuse(cbi);
334 rc = (cbi->cb != NULL) ?
335 pkt_filter_jit(&cbi->jit, pkt, nb_pkts, 0) :
337 bpf_eth_cbi_unuse(cbi);
342 * RX/TX callbacks for mbuf.
346 bpf_rx_callback_mb_vm(__rte_unused uint16_t port, __rte_unused uint16_t queue,
347 struct rte_mbuf *pkt[], uint16_t nb_pkts,
348 __rte_unused uint16_t max_pkts, void *user_param)
350 struct bpf_eth_cbi *cbi;
354 bpf_eth_cbi_inuse(cbi);
355 rc = (cbi->cb != NULL) ?
356 pkt_filter_mb_vm(cbi->bpf, pkt, nb_pkts, 1) :
358 bpf_eth_cbi_unuse(cbi);
363 bpf_rx_callback_mb_jit(__rte_unused uint16_t port, __rte_unused uint16_t queue,
364 struct rte_mbuf *pkt[], uint16_t nb_pkts,
365 __rte_unused uint16_t max_pkts, void *user_param)
367 struct bpf_eth_cbi *cbi;
371 bpf_eth_cbi_inuse(cbi);
372 rc = (cbi->cb != NULL) ?
373 pkt_filter_mb_jit(&cbi->jit, pkt, nb_pkts, 1) :
375 bpf_eth_cbi_unuse(cbi);
380 bpf_tx_callback_mb_vm(__rte_unused uint16_t port, __rte_unused uint16_t queue,
381 struct rte_mbuf *pkt[], uint16_t nb_pkts, void *user_param)
383 struct bpf_eth_cbi *cbi;
387 bpf_eth_cbi_inuse(cbi);
388 rc = (cbi->cb != NULL) ?
389 pkt_filter_mb_vm(cbi->bpf, pkt, nb_pkts, 0) :
391 bpf_eth_cbi_unuse(cbi);
396 bpf_tx_callback_mb_jit(__rte_unused uint16_t port, __rte_unused uint16_t queue,
397 struct rte_mbuf *pkt[], uint16_t nb_pkts, void *user_param)
399 struct bpf_eth_cbi *cbi;
403 bpf_eth_cbi_inuse(cbi);
404 rc = (cbi->cb != NULL) ?
405 pkt_filter_mb_jit(&cbi->jit, pkt, nb_pkts, 0) :
407 bpf_eth_cbi_unuse(cbi);
411 static rte_rx_callback_fn
412 select_rx_callback(enum rte_bpf_arg_type type, uint32_t flags)
414 if (flags & RTE_BPF_ETH_F_JIT) {
415 if (type == RTE_BPF_ARG_PTR)
416 return bpf_rx_callback_jit;
417 else if (type == RTE_BPF_ARG_PTR_MBUF)
418 return bpf_rx_callback_mb_jit;
419 } else if (type == RTE_BPF_ARG_PTR)
420 return bpf_rx_callback_vm;
421 else if (type == RTE_BPF_ARG_PTR_MBUF)
422 return bpf_rx_callback_mb_vm;
427 static rte_tx_callback_fn
428 select_tx_callback(enum rte_bpf_arg_type type, uint32_t flags)
430 if (flags & RTE_BPF_ETH_F_JIT) {
431 if (type == RTE_BPF_ARG_PTR)
432 return bpf_tx_callback_jit;
433 else if (type == RTE_BPF_ARG_PTR_MBUF)
434 return bpf_tx_callback_mb_jit;
435 } else if (type == RTE_BPF_ARG_PTR)
436 return bpf_tx_callback_vm;
437 else if (type == RTE_BPF_ARG_PTR_MBUF)
438 return bpf_tx_callback_mb_vm;
444 * helper function to perform BPF unload for given port/queue.
445 * have to introduce extra complexity (and possible slowdown) here,
446 * as right now there is no safe generic way to remove RX/TX callback
447 * while IO is active.
448 * Still don't free memory allocated for callback handle itself,
449 * again right now there is no safe way to do that without stopping RX/TX
450 * on given port/queue first.
453 bpf_eth_cbi_unload(struct bpf_eth_cbi *bc)
455 /* mark this cbi as empty */
459 /* make sure datapath doesn't use bpf anymore, then destroy bpf */
460 bpf_eth_cbi_wait(bc);
461 rte_bpf_destroy(bc->bpf);
462 bpf_eth_cbi_cleanup(bc);
466 bpf_eth_unload(struct bpf_eth_cbh *cbh, uint16_t port, uint16_t queue)
468 struct bpf_eth_cbi *bc;
470 bc = bpf_eth_cbh_find(cbh, port, queue);
471 if (bc == NULL || bc->cb == NULL)
474 if (cbh->type == BPF_ETH_RX)
475 rte_eth_remove_rx_callback(port, queue, bc->cb);
477 rte_eth_remove_tx_callback(port, queue, bc->cb);
479 bpf_eth_cbi_unload(bc);
484 rte_bpf_eth_rx_unload(uint16_t port, uint16_t queue)
486 struct bpf_eth_cbh *cbh;
489 rte_spinlock_lock(&cbh->lock);
490 bpf_eth_unload(cbh, port, queue);
491 rte_spinlock_unlock(&cbh->lock);
495 rte_bpf_eth_tx_unload(uint16_t port, uint16_t queue)
497 struct bpf_eth_cbh *cbh;
500 rte_spinlock_lock(&cbh->lock);
501 bpf_eth_unload(cbh, port, queue);
502 rte_spinlock_unlock(&cbh->lock);
506 bpf_eth_elf_load(struct bpf_eth_cbh *cbh, uint16_t port, uint16_t queue,
507 const struct rte_bpf_prm *prm, const char *fname, const char *sname,
511 struct bpf_eth_cbi *bc;
513 rte_rx_callback_fn frx;
514 rte_tx_callback_fn ftx;
515 struct rte_bpf_jit jit;
520 if (prm == NULL || rte_eth_dev_is_valid_port(port) == 0 ||
521 queue >= RTE_MAX_QUEUES_PER_PORT)
524 if (cbh->type == BPF_ETH_RX)
525 frx = select_rx_callback(prm->prog_arg.type, flags);
527 ftx = select_tx_callback(prm->prog_arg.type, flags);
529 if (frx == NULL && ftx == NULL) {
530 RTE_BPF_LOG(ERR, "%s(%u, %u): no callback selected;\n",
531 __func__, port, queue);
535 bpf = rte_bpf_elf_load(prm, fname, sname);
539 rte_bpf_get_jit(bpf, &jit);
541 if ((flags & RTE_BPF_ETH_F_JIT) != 0 && jit.func == NULL) {
542 RTE_BPF_LOG(ERR, "%s(%u, %u): no JIT generated;\n",
543 __func__, port, queue);
544 rte_bpf_destroy(bpf);
548 /* setup/update global callback info */
549 bc = bpf_eth_cbh_add(cbh, port, queue);
553 /* remove old one, if any */
555 bpf_eth_unload(cbh, port, queue);
560 if (cbh->type == BPF_ETH_RX)
561 bc->cb = rte_eth_add_rx_callback(port, queue, frx, bc);
563 bc->cb = rte_eth_add_tx_callback(port, queue, ftx, bc);
565 if (bc->cb == NULL) {
567 rte_bpf_destroy(bpf);
568 bpf_eth_cbi_cleanup(bc);
576 rte_bpf_eth_rx_elf_load(uint16_t port, uint16_t queue,
577 const struct rte_bpf_prm *prm, const char *fname, const char *sname,
581 struct bpf_eth_cbh *cbh;
584 rte_spinlock_lock(&cbh->lock);
585 rc = bpf_eth_elf_load(cbh, port, queue, prm, fname, sname, flags);
586 rte_spinlock_unlock(&cbh->lock);
592 rte_bpf_eth_tx_elf_load(uint16_t port, uint16_t queue,
593 const struct rte_bpf_prm *prm, const char *fname, const char *sname,
597 struct bpf_eth_cbh *cbh;
600 rte_spinlock_lock(&cbh->lock);
601 rc = bpf_eth_elf_load(cbh, port, queue, prm, fname, sname, flags);
602 rte_spinlock_unlock(&cbh->lock);