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 if (fseek(file, 0, SEEK_END) < 0) {
129 printf("Fail to get file size.\n");
133 if (fseek(file, 0, SEEK_SET) < 0) {
134 printf("Fail to get file size.\n");
139 * No FIFO now. Only test memory. Limit sending file
140 * size <= max_file_size.
142 if (size > max_file_size) {
143 printf("Warning: The file is too large. Only send first"
144 " %"PRIu64" bits.\n", max_file_size);
145 size = max_file_size;
148 buff = (uint8_t *)malloc(size);
149 rsize = fread(buff, size, 1, file);
151 printf("Fail to read file.\n");
157 /* Tell remote about the file size. */
159 rte_rawdev_set_attr(dev_id, "spad_user_0", val);
161 rte_rawdev_set_attr(dev_id, "spad_user_1", val);
163 pkts_send[0] = (struct rte_rawdev_buf *)malloc
164 (sizeof(struct rte_rawdev_buf));
165 pkts_send[0]->buf_addr = buff;
167 if (rte_rawdev_enqueue_buffers(dev_id, pkts_send, 1,
168 (void *)(size_t)size)) {
169 printf("Fail to enqueue.\n");
172 printf("Done sending file.\n");
180 cmdline_parse_token_string_t cmd_send_file_send =
181 TOKEN_STRING_INITIALIZER(struct cmd_sendfile_result, send_string,
183 cmdline_parse_token_string_t cmd_send_file_filepath =
184 TOKEN_STRING_INITIALIZER(struct cmd_sendfile_result, filepath, NULL);
187 cmdline_parse_inst_t cmd_send_file = {
188 .f = cmd_sendfile_parsed,
190 .help_str = "send <file_path>",
192 (void *)&cmd_send_file_send,
193 (void *)&cmd_send_file_filepath,
198 /* *** RECEIVE FILE PARAMETERS *** */
199 struct cmd_recvfile_result {
200 cmdline_fixed_string_t recv_string;
205 cmd_recvfile_parsed(void *parsed_result,
206 __attribute__((unused)) struct cmdline *cl,
207 __attribute__((unused)) void *data)
209 struct cmd_sendfile_result *res = parsed_result;
210 struct rte_rawdev_buf *pkts_recv[1];
216 if (!rte_rawdevs[dev_id].started) {
217 printf("Device needs to be up first. Try later.\n");
221 rte_rawdev_get_attr(dev_id, "link_status", &val);
223 printf("Link is not up, cannot receive file.\n");
227 file = fopen(res->filepath, "w");
229 printf("Fail to open the file.\n");
233 rte_rawdev_get_attr(dev_id, "spad_user_0", &val);
235 rte_rawdev_get_attr(dev_id, "spad_user_1", &val);
238 buff = (uint8_t *)malloc(size);
239 pkts_recv[0] = (struct rte_rawdev_buf *)malloc
240 (sizeof(struct rte_rawdev_buf));
241 pkts_recv[0]->buf_addr = buff;
243 if (rte_rawdev_dequeue_buffers(dev_id, pkts_recv, 1, (void *)size)) {
244 printf("Fail to dequeue.\n");
248 fwrite(buff, size, 1, file);
249 printf("Done receiving to file.\n");
257 cmdline_parse_token_string_t cmd_recv_file_recv =
258 TOKEN_STRING_INITIALIZER(struct cmd_recvfile_result, recv_string,
260 cmdline_parse_token_string_t cmd_recv_file_filepath =
261 TOKEN_STRING_INITIALIZER(struct cmd_recvfile_result, filepath, NULL);
264 cmdline_parse_inst_t cmd_recv_file = {
265 .f = cmd_recvfile_parsed,
267 .help_str = "recv <file_path>",
269 (void *)&cmd_recv_file_recv,
270 (void *)&cmd_recv_file_filepath,
275 /* list of instructions */
276 cmdline_parse_ctx_t main_ctx[] = {
277 (cmdline_parse_inst_t *)&cmd_help,
278 (cmdline_parse_inst_t *)&cmd_send_file,
279 (cmdline_parse_inst_t *)&cmd_recv_file,
280 (cmdline_parse_inst_t *)&cmd_quit,
284 /* prompt function, called from main on MASTER lcore */
290 cl = cmdline_stdin_new(main_ctx, "ntb> ");
294 cmdline_interact(cl);
295 cmdline_stdin_exit(cl);
299 signal_handler(int signum)
301 if (signum == SIGINT || signum == SIGTERM) {
302 printf("\nSignal %d received, preparing to exit...\n", signum);
303 signal(signum, SIG_DFL);
304 kill(getpid(), signum);
309 ntb_usage(const char *prgname)
311 printf("%s [EAL options] -- [options]\n"
312 "-i : run in interactive mode (default value is 1)\n",
317 parse_args(int argc, char **argv)
319 char *prgname = argv[0], **argvopt = argv;
322 /* Only support interactive mode to send/recv file first. */
323 while ((opt = getopt(argc, argvopt, "i")) != EOF) {
326 printf("Interactive-mode selected\n");
337 argv[optind-1] = prgname;
340 optind = 1; /* reset getopt lib */
345 main(int argc, char **argv)
349 signal(SIGINT, signal_handler);
350 signal(SIGTERM, signal_handler);
352 ret = rte_eal_init(argc, argv);
354 rte_exit(EXIT_FAILURE, "Error with EAL initialization.\n");
356 /* Find 1st ntb rawdev. */
357 for (i = 0; i < RTE_RAWDEV_MAX_DEVS; i++)
358 if (rte_rawdevs[i].driver_name &&
359 (strncmp(rte_rawdevs[i].driver_name, "raw_ntb",
360 NTB_DRV_NAME_LEN) == 0) && (rte_rawdevs[i].attached == 1))
363 if (i == RTE_RAWDEV_MAX_DEVS)
364 rte_exit(EXIT_FAILURE, "Cannot find any ntb device.\n");
371 ret = parse_args(argc, argv);
373 rte_exit(EXIT_FAILURE, "Invalid arguments\n");
375 rte_rawdev_start(dev_id);