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 .type = RTE_BPF_ARG_PTR,
26 .size = sizeof(stdout),
31 .name = RTE_STR(rte_pktmbuf_dump),
32 .type = RTE_BPF_XTYPE_FUNC,
34 .val = (void *)rte_pktmbuf_dump,
38 .type = RTE_BPF_ARG_RAW,
39 .size = sizeof(uintptr_t),
42 .type = RTE_BPF_ARG_PTR_MBUF,
43 .size = sizeof(struct rte_mbuf),
46 .type = RTE_BPF_ARG_RAW,
47 .size = sizeof(uint32_t),
54 /* *** load BPF program *** */
55 struct cmd_bpf_ld_result {
56 cmdline_fixed_string_t bpf;
57 cmdline_fixed_string_t dir;
60 cmdline_fixed_string_t op;
61 cmdline_fixed_string_t flags;
62 cmdline_fixed_string_t prm;
66 bpf_parse_flags(const char *str, struct rte_bpf_arg *arg, uint32_t *flags)
70 *flags = RTE_BPF_ETH_F_NONE;
71 arg->type = RTE_BPF_ARG_PTR;
72 arg->size = mbuf_data_size;
74 for (i = 0; str[i] != 0; i++) {
77 *flags |= RTE_BPF_ETH_F_JIT;
79 arg->type = RTE_BPF_ARG_PTR_MBUF;
80 arg->size = sizeof(struct rte_mbuf);
81 arg->buf_size = mbuf_data_size;
85 printf("unknown flag: \'%c\'", v);
89 static void cmd_operate_bpf_ld_parsed(void *parsed_result,
90 __rte_unused struct cmdline *cl,
91 __rte_unused void *data)
95 struct cmd_bpf_ld_result *res;
96 struct rte_bpf_prm prm;
97 const char *fname, *sname;
100 memset(&prm, 0, sizeof(prm));
102 prm.nb_xsym = RTE_DIM(bpf_xsym);
104 bpf_parse_flags(res->flags, &prm.prog_arg, &flags);
108 if (strcmp(res->dir, "rx") == 0) {
109 rc = rte_bpf_eth_rx_elf_load(res->port, res->queue, &prm,
110 fname, sname, flags);
111 printf("%d:%s\n", rc, strerror(-rc));
112 } else if (strcmp(res->dir, "tx") == 0) {
113 rc = rte_bpf_eth_tx_elf_load(res->port, res->queue, &prm,
114 fname, sname, flags);
115 printf("%d:%s\n", rc, strerror(-rc));
117 printf("invalid value: %s\n", res->dir);
120 cmdline_parse_token_string_t cmd_load_bpf_start =
121 TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
123 cmdline_parse_token_string_t cmd_load_bpf_dir =
124 TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
126 cmdline_parse_token_num_t cmd_load_bpf_port =
127 TOKEN_NUM_INITIALIZER(struct cmd_bpf_ld_result, port, UINT8);
128 cmdline_parse_token_num_t cmd_load_bpf_queue =
129 TOKEN_NUM_INITIALIZER(struct cmd_bpf_ld_result, queue, UINT16);
130 cmdline_parse_token_string_t cmd_load_bpf_flags =
131 TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
133 cmdline_parse_token_string_t cmd_load_bpf_prm =
134 TOKEN_STRING_INITIALIZER(struct cmd_bpf_ld_result,
137 cmdline_parse_inst_t cmd_operate_bpf_ld_parse = {
138 .f = cmd_operate_bpf_ld_parsed,
140 .help_str = "bpf-load rx|tx <port> <queue> <J|M|B> <file_name>",
142 (void *)&cmd_load_bpf_start,
143 (void *)&cmd_load_bpf_dir,
144 (void *)&cmd_load_bpf_port,
145 (void *)&cmd_load_bpf_queue,
146 (void *)&cmd_load_bpf_flags,
147 (void *)&cmd_load_bpf_prm,
152 /* *** unload BPF program *** */
153 struct cmd_bpf_unld_result {
154 cmdline_fixed_string_t bpf;
155 cmdline_fixed_string_t dir;
160 static void cmd_operate_bpf_unld_parsed(void *parsed_result,
161 __rte_unused struct cmdline *cl,
162 __rte_unused void *data)
164 struct cmd_bpf_unld_result *res;
168 if (strcmp(res->dir, "rx") == 0)
169 rte_bpf_eth_rx_unload(res->port, res->queue);
170 else if (strcmp(res->dir, "tx") == 0)
171 rte_bpf_eth_tx_unload(res->port, res->queue);
173 printf("invalid value: %s\n", res->dir);
176 cmdline_parse_token_string_t cmd_unload_bpf_start =
177 TOKEN_STRING_INITIALIZER(struct cmd_bpf_unld_result,
179 cmdline_parse_token_string_t cmd_unload_bpf_dir =
180 TOKEN_STRING_INITIALIZER(struct cmd_bpf_unld_result,
182 cmdline_parse_token_num_t cmd_unload_bpf_port =
183 TOKEN_NUM_INITIALIZER(struct cmd_bpf_unld_result, port, UINT8);
184 cmdline_parse_token_num_t cmd_unload_bpf_queue =
185 TOKEN_NUM_INITIALIZER(struct cmd_bpf_unld_result, queue, UINT16);
187 cmdline_parse_inst_t cmd_operate_bpf_unld_parse = {
188 .f = cmd_operate_bpf_unld_parsed,
190 .help_str = "bpf-unload rx|tx <port> <queue>",
192 (void *)&cmd_unload_bpf_start,
193 (void *)&cmd_unload_bpf_dir,
194 (void *)&cmd_unload_bpf_port,
195 (void *)&cmd_unload_bpf_queue,