Skip to content

Commit

Permalink
app/testpmd: add commands to load/unload BPF filters
Browse files Browse the repository at this point in the history
Introduce new testpmd commands to load/unload RX/TX BPF-based filters.

Signed-off-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Ferruh Yigit <ferruh.yigit@intel.com>
  • Loading branch information
kananyev authored and tmonjalo committed May 11, 2018
1 parent a93ff62 commit e977e41
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/test-pmd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SRCS-y += txonly.c
SRCS-y += csumonly.c
SRCS-y += icmpecho.c
SRCS-$(CONFIG_RTE_LIBRTE_IEEE1588) += ieee1588fwd.c
SRCS-$(CONFIG_RTE_LIBRTE_BPF) += bpf_cmd.c

ifeq ($(CONFIG_RTE_LIBRTE_PMD_SOFTNIC)$(CONFIG_RTE_LIBRTE_SCHED),yy)
SRCS-y += tm.c
Expand Down
175 changes: 175 additions & 0 deletions app/test-pmd/bpf_cmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2018 Intel Corporation
*/

#include <stdio.h>
#include <rte_mbuf.h>
#include <rte_ethdev.h>
#include <rte_flow.h>
#include <rte_bpf_ethdev.h>

#include <cmdline.h>
#include <cmdline_parse.h>
#include <cmdline_parse_num.h>
#include <cmdline_parse_string.h>

#include "testpmd.h"

static const struct rte_bpf_xsym bpf_xsym[] = {
{
.name = RTE_STR(stdout),
.type = RTE_BPF_XTYPE_VAR,
.var = &stdout,
},
{
.name = RTE_STR(rte_pktmbuf_dump),
.type = RTE_BPF_XTYPE_FUNC,
.func = (void *)rte_pktmbuf_dump,
},
};

/* *** load BPF program *** */
struct cmd_bpf_ld_result {
cmdline_fixed_string_t bpf;
cmdline_fixed_string_t dir;
uint8_t port;
uint16_t queue;
cmdline_fixed_string_t op;
cmdline_fixed_string_t flags;
cmdline_fixed_string_t prm;
};

static void
bpf_parse_flags(const char *str, struct rte_bpf_arg *arg, uint32_t *flags)
{
uint32_t i, v;

*flags = RTE_BPF_ETH_F_NONE;
arg->type = RTE_BPF_ARG_PTR;
arg->size = mbuf_data_size;

for (i = 0; str[i] != 0; i++) {
v = toupper(str[i]);
if (v == 'J')
*flags |= RTE_BPF_ETH_F_JIT;
else if (v == 'M') {
arg->type = RTE_BPF_ARG_PTR_MBUF;
arg->size = sizeof(struct rte_mbuf);
arg->buf_size = mbuf_data_size;
} else if (v == '-')
continue;
else
printf("unknown flag: \'%c\'", v);
}
}

static void cmd_operate_bpf_ld_parsed(void *parsed_result,
__attribute__((unused)) struct cmdline *cl,
__attribute__((unused)) void *data)
{
int32_t rc;
uint32_t flags;
struct cmd_bpf_ld_result *res;
struct rte_bpf_prm prm;
const char *fname, *sname;

res = parsed_result;
memset(&prm, 0, sizeof(prm));
prm.xsym = bpf_xsym;
prm.nb_xsym = RTE_DIM(bpf_xsym);

bpf_parse_flags(res->flags, &prm.prog_arg, &flags);
fname = res->prm;
sname = ".text";

if (strcmp(res->dir, "rx") == 0) {
rc = rte_bpf_eth_rx_elf_load(res->port, res->queue, &prm,
fname, sname, flags);
printf("%d:%s\n", rc, strerror(-rc));
} else if (strcmp(res->dir, "tx") == 0) {
rc = rte_bpf_eth_tx_elf_load(res->port, res->queue, &prm,
fname, sname, flags);
printf("%d:%s\n", rc, strerror(-rc));
} else
printf("invalid value: %s\n", res->dir);
}

cmdline_parse_token_string_t cmd_load_bpf_start =
TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
bpf, "bpf-load");
cmdline_parse_token_string_t cmd_load_bpf_dir =
TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
dir, "rx#tx");
cmdline_parse_token_num_t cmd_load_bpf_port =
TOKEN_NUM_INITIALIZER(struct cmd_bpf_ld_result, port, UINT8);
cmdline_parse_token_num_t cmd_load_bpf_queue =
TOKEN_NUM_INITIALIZER(struct cmd_bpf_ld_result, queue, UINT16);
cmdline_parse_token_string_t cmd_load_bpf_flags =
TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
flags, NULL);
cmdline_parse_token_string_t cmd_load_bpf_prm =
TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
prm, NULL);

cmdline_parse_inst_t cmd_operate_bpf_ld_parse = {
.f = cmd_operate_bpf_ld_parsed,
.data = NULL,
.help_str = "bpf-load rx|tx <port> <queue> <J|M|B> <file_name>",
.tokens = {
(void *)&cmd_load_bpf_start,
(void *)&cmd_load_bpf_dir,
(void *)&cmd_load_bpf_port,
(void *)&cmd_load_bpf_queue,
(void *)&cmd_load_bpf_flags,
(void *)&cmd_load_bpf_prm,
NULL,
},
};

/* *** unload BPF program *** */
struct cmd_bpf_unld_result {
cmdline_fixed_string_t bpf;
cmdline_fixed_string_t dir;
uint8_t port;
uint16_t queue;
};

static void cmd_operate_bpf_unld_parsed(void *parsed_result,
__attribute__((unused)) struct cmdline *cl,
__attribute__((unused)) void *data)
{
struct cmd_bpf_unld_result *res;

res = parsed_result;

if (strcmp(res->dir, "rx") == 0)
rte_bpf_eth_rx_unload(res->port, res->queue);
else if (strcmp(res->dir, "tx") == 0)
rte_bpf_eth_tx_unload(res->port, res->queue);
else
printf("invalid value: %s\n", res->dir);
}

cmdline_parse_token_string_t cmd_unload_bpf_start =
TOKEN_STRING_INITIALIZER(struct cmd_bpf_unld_result,
bpf, "bpf-unload");
cmdline_parse_token_string_t cmd_unload_bpf_dir =
TOKEN_STRING_INITIALIZER(struct cmd_bpf_unld_result,
dir, "rx#tx");
cmdline_parse_token_num_t cmd_unload_bpf_port =
TOKEN_NUM_INITIALIZER(struct cmd_bpf_unld_result, port, UINT8);
cmdline_parse_token_num_t cmd_unload_bpf_queue =
TOKEN_NUM_INITIALIZER(struct cmd_bpf_unld_result, queue, UINT16);

cmdline_parse_inst_t cmd_operate_bpf_unld_parse = {
.f = cmd_operate_bpf_unld_parsed,
.data = NULL,
.help_str = "bpf-unload rx|tx <port> <queue>",
.tokens = {
(void *)&cmd_unload_bpf_start,
(void *)&cmd_unload_bpf_dir,
(void *)&cmd_unload_bpf_port,
(void *)&cmd_unload_bpf_queue,
NULL,
},
};
16 changes: 16 additions & 0 deletions app/test-pmd/bpf_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2018 Intel Corporation
*/

#ifndef _BPF_CMD_H_
#define _BPF_CMD_H_

#ifdef RTE_LIBRTE_BPF

/* BPF CLI */
extern cmdline_parse_inst_t cmd_operate_bpf_ld_parse;
extern cmdline_parse_inst_t cmd_operate_bpf_unld_parse;

#endif /* RTE_LIBRTE_BPF */

#endif /* _BPF_CMD_H_ */
5 changes: 5 additions & 0 deletions app/test-pmd/cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include "testpmd.h"
#include "cmdline_mtr.h"
#include "cmdline_tm.h"
#include "bpf_cmd.h"

static struct cmdline *testpmd_cl;

Expand Down Expand Up @@ -16703,6 +16704,10 @@ cmdline_parse_ctx_t main_ctx[] = {
(cmdline_parse_inst_t *)&cmd_resume_port_tm_node,
(cmdline_parse_inst_t *)&cmd_port_tm_hierarchy_commit,
(cmdline_parse_inst_t *)&cmd_cfg_tunnel_udp_port,
#ifdef RTE_LIBRTE_BPF
(cmdline_parse_inst_t *)&cmd_operate_bpf_ld_parse,
(cmdline_parse_inst_t *)&cmd_operate_bpf_unld_parse,
#endif
NULL,
};

Expand Down
4 changes: 4 additions & 0 deletions app/test-pmd/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ endif
if dpdk_conf.has('RTE_LIBRTE_DPAA_PMD')
deps += ['bus_dpaa', 'mempool_dpaa', 'pmd_dpaa']
endif
if dpdk_conf.has('RTE_LIBRTE_BPF')
sources += files('bpf_cmd.c')
deps += 'bpf'
endif
56 changes: 56 additions & 0 deletions doc/guides/testpmd_app_ug/testpmd_funcs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3823,3 +3823,59 @@ Validate and create a QinQ rule on port 0 to steer traffic to a queue on the hos
ID Group Prio Attr Rule
0 0 0 i- ETH VLAN VLAN=>VF QUEUE
1 0 0 i- ETH VLAN VLAN=>PF QUEUE

BPF Functions
--------------

The following sections show functions to load/unload eBPF based filters.

bpf-load
~~~~~~~~

Load an eBPF program as a callback for partciular RX/TX queue::

testpmd> bpf-load rx|tx (portid) (queueid) (load-flags) (bpf-prog-filename)

The available load-flags are:

* ``J``: use JIT generated native code, otherwise BPF interpreter will be used.

* ``M``: assume input parameter is a pointer to rte_mbuf, otherwise assume it is a pointer to first segment's data.

* ``-``: none.

.. note::

You'll need clang v3.7 or above to build bpf program you'd like to load

For example:

.. code-block:: console
cd test/bpf
clang -O2 -target bpf -c t1.c
Then to load (and JIT compile) t1.o at RX queue 0, port 1::

.. code-block:: console
testpmd> bpf-load rx 1 0 J ./dpdk.org/test/bpf/t1.o
To load (not JITed) t1.o at TX queue 0, port 0::

.. code-block:: console
testpmd> bpf-load tx 0 0 - ./dpdk.org/test/bpf/t1.o
bpf-unload
~~~~~~~~~~

Unload previously loaded eBPF program for partciular RX/TX queue::

testpmd> bpf-unload rx|tx (portid) (queueid)

For example to unload BPF filter from TX queue 0, port 0:

.. code-block:: console
testpmd> bpf-load tx 0 0 - ./dpdk.org/test/bpf/t1.o

0 comments on commit e977e41

Please sign in to comment.