1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
7 #include <rte_ethdev.h>
9 #include <rte_bpf_ethdev.h>
12 #include <cmdline_parse.h>
13 #include <cmdline_parse_num.h>
14 #include <cmdline_parse_string.h>
18 static const struct rte_bpf_xsym bpf_xsym[] = {
20 .name = RTE_STR(stdout),
21 .type = RTE_BPF_XTYPE_VAR,
25 .name = RTE_STR(rte_pktmbuf_dump),
26 .type = RTE_BPF_XTYPE_FUNC,
27 .func = (void *)rte_pktmbuf_dump,
31 /* *** load BPF program *** */
32 struct cmd_bpf_ld_result {
33 cmdline_fixed_string_t bpf;
34 cmdline_fixed_string_t dir;
37 cmdline_fixed_string_t op;
38 cmdline_fixed_string_t flags;
39 cmdline_fixed_string_t prm;
43 bpf_parse_flags(const char *str, struct rte_bpf_arg *arg, uint32_t *flags)
47 *flags = RTE_BPF_ETH_F_NONE;
48 arg->type = RTE_BPF_ARG_PTR;
49 arg->size = mbuf_data_size;
51 for (i = 0; str[i] != 0; i++) {
54 *flags |= RTE_BPF_ETH_F_JIT;
56 arg->type = RTE_BPF_ARG_PTR_MBUF;
57 arg->size = sizeof(struct rte_mbuf);
58 arg->buf_size = mbuf_data_size;
62 printf("unknown flag: \'%c\'", v);
66 static void cmd_operate_bpf_ld_parsed(void *parsed_result,
67 __attribute__((unused)) struct cmdline *cl,
68 __attribute__((unused)) void *data)
72 struct cmd_bpf_ld_result *res;
73 struct rte_bpf_prm prm;
74 const char *fname, *sname;
77 memset(&prm, 0, sizeof(prm));
79 prm.nb_xsym = RTE_DIM(bpf_xsym);
81 bpf_parse_flags(res->flags, &prm.prog_arg, &flags);
85 if (strcmp(res->dir, "rx") == 0) {
86 rc = rte_bpf_eth_rx_elf_load(res->port, res->queue, &prm,
88 printf("%d:%s\n", rc, strerror(-rc));
89 } else if (strcmp(res->dir, "tx") == 0) {
90 rc = rte_bpf_eth_tx_elf_load(res->port, res->queue, &prm,
92 printf("%d:%s\n", rc, strerror(-rc));
94 printf("invalid value: %s\n", res->dir);
97 cmdline_parse_token_string_t cmd_load_bpf_start =
98 TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
100 cmdline_parse_token_string_t cmd_load_bpf_dir =
101 TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
103 cmdline_parse_token_num_t cmd_load_bpf_port =
104 TOKEN_NUM_INITIALIZER(struct cmd_bpf_ld_result, port, UINT8);
105 cmdline_parse_token_num_t cmd_load_bpf_queue =
106 TOKEN_NUM_INITIALIZER(struct cmd_bpf_ld_result, queue, UINT16);
107 cmdline_parse_token_string_t cmd_load_bpf_flags =
108 TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
110 cmdline_parse_token_string_t cmd_load_bpf_prm =
111 TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
114 cmdline_parse_inst_t cmd_operate_bpf_ld_parse = {
115 .f = cmd_operate_bpf_ld_parsed,
117 .help_str = "bpf-load rx|tx <port> <queue> <J|M|B> <file_name>",
119 (void *)&cmd_load_bpf_start,
120 (void *)&cmd_load_bpf_dir,
121 (void *)&cmd_load_bpf_port,
122 (void *)&cmd_load_bpf_queue,
123 (void *)&cmd_load_bpf_flags,
124 (void *)&cmd_load_bpf_prm,
129 /* *** unload BPF program *** */
130 struct cmd_bpf_unld_result {
131 cmdline_fixed_string_t bpf;
132 cmdline_fixed_string_t dir;
137 static void cmd_operate_bpf_unld_parsed(void *parsed_result,
138 __attribute__((unused)) struct cmdline *cl,
139 __attribute__((unused)) void *data)
141 struct cmd_bpf_unld_result *res;
145 if (strcmp(res->dir, "rx") == 0)
146 rte_bpf_eth_rx_unload(res->port, res->queue);
147 else if (strcmp(res->dir, "tx") == 0)
148 rte_bpf_eth_tx_unload(res->port, res->queue);
150 printf("invalid value: %s\n", res->dir);
153 cmdline_parse_token_string_t cmd_unload_bpf_start =
154 TOKEN_STRING_INITIALIZER(struct cmd_bpf_unld_result,
156 cmdline_parse_token_string_t cmd_unload_bpf_dir =
157 TOKEN_STRING_INITIALIZER(struct cmd_bpf_unld_result,
159 cmdline_parse_token_num_t cmd_unload_bpf_port =
160 TOKEN_NUM_INITIALIZER(struct cmd_bpf_unld_result, port, UINT8);
161 cmdline_parse_token_num_t cmd_unload_bpf_queue =
162 TOKEN_NUM_INITIALIZER(struct cmd_bpf_unld_result, queue, UINT16);
164 cmdline_parse_inst_t cmd_operate_bpf_unld_parse = {
165 .f = cmd_operate_bpf_unld_parsed,
167 .help_str = "bpf-unload rx|tx <port> <queue>",
169 (void *)&cmd_unload_bpf_start,
170 (void *)&cmd_unload_bpf_dir,
171 (void *)&cmd_unload_bpf_port,
172 (void *)&cmd_unload_bpf_queue,