1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
12 #include <cmdline_parse_string.h>
13 #include <cmdline_socket.h>
15 #include <rte_common.h>
16 #include <rte_rawdev.h>
17 #include <rte_lcore.h>
19 #define NTB_DRV_NAME_LEN 7
20 static uint64_t max_file_size = 0x400000;
21 static uint8_t interactive = 1;
22 static uint16_t dev_id;
24 /* *** Help command with introduction. *** */
25 struct cmd_help_result {
26 cmdline_fixed_string_t help;
29 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
31 __attribute__((unused)) void *data)
36 "The following commands are currently available:\n\n"
39 " Quit the application.\n"
42 " Send [path] file. (No more than %"PRIu64")\n"
44 " Receive file to [path]. Make sure sending is done"
45 " on the other side.\n",
51 cmdline_parse_token_string_t cmd_help_help =
52 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
54 cmdline_parse_inst_t cmd_help = {
57 .help_str = "show help",
59 (void *)&cmd_help_help,
65 struct cmd_quit_result {
66 cmdline_fixed_string_t quit;
69 static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
71 __attribute__((unused)) void *data)
73 /* Stop traffic and Close port. */
74 rte_rawdev_stop(dev_id);
75 rte_rawdev_close(dev_id);
80 cmdline_parse_token_string_t cmd_quit_quit =
81 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
83 cmdline_parse_inst_t cmd_quit = {
86 .help_str = "exit application",
88 (void *)&cmd_quit_quit,
93 /* *** SEND FILE PARAMETERS *** */
94 struct cmd_sendfile_result {
95 cmdline_fixed_string_t send_string;
100 cmd_sendfile_parsed(void *parsed_result,
101 __attribute__((unused)) struct cmdline *cl,
102 __attribute__((unused)) void *data)
104 struct cmd_sendfile_result *res = parsed_result;
105 struct rte_rawdev_buf *pkts_send[1];
106 uint64_t rsize, size, link;
111 if (!rte_rawdevs[dev_id].started) {
112 printf("Device needs to be up first. Try later.\n");
116 rte_rawdev_get_attr(dev_id, "link_status", &link);
118 printf("Link is not up, cannot send file.\n");
122 file = fopen(res->filepath, "r");
124 printf("Fail to open the file.\n");
128 fseek(file, 0, SEEK_END);
130 fseek(file, 0, SEEK_SET);
133 * No FIFO now. Only test memory. Limit sending file
134 * size <= max_file_size.
136 if (size > max_file_size) {
137 printf("Warning: The file is too large. Only send first"
138 " %"PRIu64" bits.\n", max_file_size);
139 size = max_file_size;
142 buff = (uint8_t *)malloc(size);
143 rsize = fread(buff, size, 1, file);
145 printf("Fail to read file.\n");
151 /* Tell remote about the file size. */
153 rte_rawdev_set_attr(dev_id, "spad_user_0", val);
155 rte_rawdev_set_attr(dev_id, "spad_user_1", val);
157 pkts_send[0] = (struct rte_rawdev_buf *)malloc
158 (sizeof(struct rte_rawdev_buf));
159 pkts_send[0]->buf_addr = buff;
161 if (rte_rawdev_enqueue_buffers(dev_id, pkts_send, 1,
162 (void *)(size_t)size)) {
163 printf("Fail to enqueue.\n");
166 printf("Done sending file.\n");
174 cmdline_parse_token_string_t cmd_send_file_send =
175 TOKEN_STRING_INITIALIZER(struct cmd_sendfile_result, send_string,
177 cmdline_parse_token_string_t cmd_send_file_filepath =
178 TOKEN_STRING_INITIALIZER(struct cmd_sendfile_result, filepath, NULL);
181 cmdline_parse_inst_t cmd_send_file = {
182 .f = cmd_sendfile_parsed,
184 .help_str = "send <file_path>",
186 (void *)&cmd_send_file_send,
187 (void *)&cmd_send_file_filepath,
192 /* *** RECEIVE FILE PARAMETERS *** */
193 struct cmd_recvfile_result {
194 cmdline_fixed_string_t recv_string;
199 cmd_recvfile_parsed(void *parsed_result,
200 __attribute__((unused)) struct cmdline *cl,
201 __attribute__((unused)) void *data)
203 struct cmd_sendfile_result *res = parsed_result;
204 struct rte_rawdev_buf *pkts_recv[1];
210 if (!rte_rawdevs[dev_id].started) {
211 printf("Device needs to be up first. Try later.\n");
215 rte_rawdev_get_attr(dev_id, "link_status", &val);
217 printf("Link is not up, cannot receive file.\n");
221 file = fopen(res->filepath, "w");
223 printf("Fail to open the file.\n");
227 rte_rawdev_get_attr(dev_id, "spad_user_0", &val);
229 rte_rawdev_get_attr(dev_id, "spad_user_1", &val);
232 buff = (uint8_t *)malloc(size);
233 pkts_recv[0] = (struct rte_rawdev_buf *)malloc
234 (sizeof(struct rte_rawdev_buf));
235 pkts_recv[0]->buf_addr = buff;
237 if (rte_rawdev_dequeue_buffers(dev_id, pkts_recv, 1, (void *)size)) {
238 printf("Fail to dequeue.\n");
242 fwrite(buff, size, 1, file);
243 printf("Done receiving to file.\n");
251 cmdline_parse_token_string_t cmd_recv_file_recv =
252 TOKEN_STRING_INITIALIZER(struct cmd_recvfile_result, recv_string,
254 cmdline_parse_token_string_t cmd_recv_file_filepath =
255 TOKEN_STRING_INITIALIZER(struct cmd_recvfile_result, filepath, NULL);
258 cmdline_parse_inst_t cmd_recv_file = {
259 .f = cmd_recvfile_parsed,
261 .help_str = "recv <file_path>",
263 (void *)&cmd_recv_file_recv,
264 (void *)&cmd_recv_file_filepath,
269 /* list of instructions */
270 cmdline_parse_ctx_t main_ctx[] = {
271 (cmdline_parse_inst_t *)&cmd_help,
272 (cmdline_parse_inst_t *)&cmd_send_file,
273 (cmdline_parse_inst_t *)&cmd_recv_file,
274 (cmdline_parse_inst_t *)&cmd_quit,
278 /* prompt function, called from main on MASTER lcore */
284 cl = cmdline_stdin_new(main_ctx, "ntb> ");
288 cmdline_interact(cl);
289 cmdline_stdin_exit(cl);
293 signal_handler(int signum)
295 if (signum == SIGINT || signum == SIGTERM) {
296 printf("\nSignal %d received, preparing to exit...\n", signum);
297 signal(signum, SIG_DFL);
298 kill(getpid(), signum);
303 ntb_usage(const char *prgname)
305 printf("%s [EAL options] -- [options]\n"
306 "-i : run in interactive mode (default value is 1)\n",
311 parse_args(int argc, char **argv)
313 char *prgname = argv[0], **argvopt = argv;
316 /* Only support interactive mode to send/recv file first. */
317 while ((opt = getopt(argc, argvopt, "i")) != EOF) {
320 printf("Interactive-mode selected\n");
331 argv[optind-1] = prgname;
334 optind = 1; /* reset getopt lib */
339 main(int argc, char **argv)
343 signal(SIGINT, signal_handler);
344 signal(SIGTERM, signal_handler);
346 ret = rte_eal_init(argc, argv);
348 rte_exit(EXIT_FAILURE, "Error with EAL initialization.\n");
350 /* Find 1st ntb rawdev. */
351 for (i = 0; i < RTE_RAWDEV_MAX_DEVS; i++)
352 if (rte_rawdevs[i].driver_name &&
353 (strncmp(rte_rawdevs[i].driver_name, "raw_ntb",
354 NTB_DRV_NAME_LEN) == 0) && (rte_rawdevs[i].attached == 1))
357 if (i == RTE_RAWDEV_MAX_DEVS)
358 rte_exit(EXIT_FAILURE, "Cannot find any ntb device.\n");
365 ret = parse_args(argc, argv);
367 rte_exit(EXIT_FAILURE, "Invalid arguments\n");
369 rte_rawdev_start(dev_id);