13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 24bbf8e30SJasvinder Singh * Copyright(c) 2010-2018 Intel Corporation 377a33467SCristian Dumitrescu */ 477a33467SCristian Dumitrescu 5fbc74e66SJasvinder Singh #include <stdio.h> 6fbc74e66SJasvinder Singh #include <string.h> 7fbc74e66SJasvinder Singh #include <fcntl.h> 8fbc74e66SJasvinder Singh #include <unistd.h> 9fbc74e66SJasvinder Singh #include <getopt.h> 1077a33467SCristian Dumitrescu 11fbc74e66SJasvinder Singh #include <rte_eal.h> 1277a33467SCristian Dumitrescu 134bbf8e30SJasvinder Singh #include "cli.h" 144bbf8e30SJasvinder Singh #include "conn.h" 15133c2c65SJasvinder Singh #include "link.h" 166bfe74f8SJasvinder Singh #include "mempool.h" 17*8245472cSJasvinder Singh #include "swq.h" 184bbf8e30SJasvinder Singh 194bbf8e30SJasvinder Singh static const char usage[] = 204bbf8e30SJasvinder Singh "%s EAL_ARGS -- [-h HOST] [-p PORT] [-s SCRIPT]\n"; 214bbf8e30SJasvinder Singh 224bbf8e30SJasvinder Singh static const char welcome[] = 234bbf8e30SJasvinder Singh "\n" 244bbf8e30SJasvinder Singh "Welcome to IP Pipeline!\n" 254bbf8e30SJasvinder Singh "\n"; 264bbf8e30SJasvinder Singh 274bbf8e30SJasvinder Singh static const char prompt[] = "pipeline> "; 284bbf8e30SJasvinder Singh 294bbf8e30SJasvinder Singh static struct app_params { 304bbf8e30SJasvinder Singh struct conn_params conn; 314bbf8e30SJasvinder Singh char *script_name; 324bbf8e30SJasvinder Singh } app = { 334bbf8e30SJasvinder Singh .conn = { 344bbf8e30SJasvinder Singh .welcome = welcome, 354bbf8e30SJasvinder Singh .prompt = prompt, 364bbf8e30SJasvinder Singh .addr = "0.0.0.0", 374bbf8e30SJasvinder Singh .port = 8086, 384bbf8e30SJasvinder Singh .buf_size = 1024 * 1024, 394bbf8e30SJasvinder Singh .msg_in_len_max = 1024, 404bbf8e30SJasvinder Singh .msg_out_len_max = 1024 * 1024, 414bbf8e30SJasvinder Singh .msg_handle = cli_process, 424bbf8e30SJasvinder Singh }, 434bbf8e30SJasvinder Singh .script_name = NULL, 444bbf8e30SJasvinder Singh }; 454bbf8e30SJasvinder Singh 464bbf8e30SJasvinder Singh static int 474bbf8e30SJasvinder Singh parse_args(int argc, char **argv) 484bbf8e30SJasvinder Singh { 494bbf8e30SJasvinder Singh char *app_name = argv[0]; 504bbf8e30SJasvinder Singh struct option lgopts[] = { 514bbf8e30SJasvinder Singh { NULL, 0, 0, 0 } 524bbf8e30SJasvinder Singh }; 534bbf8e30SJasvinder Singh int opt, option_index; 544bbf8e30SJasvinder Singh int h_present, p_present, s_present, n_args, i; 554bbf8e30SJasvinder Singh 564bbf8e30SJasvinder Singh /* Skip EAL input args */ 574bbf8e30SJasvinder Singh n_args = argc; 584bbf8e30SJasvinder Singh for (i = 0; i < n_args; i++) 594bbf8e30SJasvinder Singh if (strcmp(argv[i], "--") == 0) { 604bbf8e30SJasvinder Singh argc -= i; 614bbf8e30SJasvinder Singh argv += i; 624bbf8e30SJasvinder Singh break; 634bbf8e30SJasvinder Singh } 644bbf8e30SJasvinder Singh 654bbf8e30SJasvinder Singh if (i == n_args) 664bbf8e30SJasvinder Singh return 0; 674bbf8e30SJasvinder Singh 684bbf8e30SJasvinder Singh /* Parse args */ 694bbf8e30SJasvinder Singh h_present = 0; 704bbf8e30SJasvinder Singh p_present = 0; 714bbf8e30SJasvinder Singh s_present = 0; 724bbf8e30SJasvinder Singh 734bbf8e30SJasvinder Singh while ((opt = getopt_long(argc, argv, "h:p:s:", lgopts, &option_index)) 744bbf8e30SJasvinder Singh != EOF) 754bbf8e30SJasvinder Singh switch (opt) { 764bbf8e30SJasvinder Singh case 'h': 774bbf8e30SJasvinder Singh if (h_present) { 784bbf8e30SJasvinder Singh printf("Error: Multiple -h arguments\n"); 794bbf8e30SJasvinder Singh return -1; 804bbf8e30SJasvinder Singh } 814bbf8e30SJasvinder Singh h_present = 1; 824bbf8e30SJasvinder Singh 834bbf8e30SJasvinder Singh if (!strlen(optarg)) { 844bbf8e30SJasvinder Singh printf("Error: Argument for -h not provided\n"); 854bbf8e30SJasvinder Singh return -1; 864bbf8e30SJasvinder Singh } 874bbf8e30SJasvinder Singh 884bbf8e30SJasvinder Singh app.conn.addr = strdup(optarg); 894bbf8e30SJasvinder Singh if (app.conn.addr == NULL) { 904bbf8e30SJasvinder Singh printf("Error: Not enough memory\n"); 914bbf8e30SJasvinder Singh return -1; 924bbf8e30SJasvinder Singh } 934bbf8e30SJasvinder Singh break; 944bbf8e30SJasvinder Singh 954bbf8e30SJasvinder Singh case 'p': 964bbf8e30SJasvinder Singh if (p_present) { 974bbf8e30SJasvinder Singh printf("Error: Multiple -p arguments\n"); 984bbf8e30SJasvinder Singh return -1; 994bbf8e30SJasvinder Singh } 1004bbf8e30SJasvinder Singh p_present = 1; 1014bbf8e30SJasvinder Singh 1024bbf8e30SJasvinder Singh if (!strlen(optarg)) { 1034bbf8e30SJasvinder Singh printf("Error: Argument for -p not provided\n"); 1044bbf8e30SJasvinder Singh return -1; 1054bbf8e30SJasvinder Singh } 1064bbf8e30SJasvinder Singh 1074bbf8e30SJasvinder Singh app.conn.port = (uint16_t) atoi(optarg); 1084bbf8e30SJasvinder Singh break; 1094bbf8e30SJasvinder Singh 1104bbf8e30SJasvinder Singh case 's': 1114bbf8e30SJasvinder Singh if (s_present) { 1124bbf8e30SJasvinder Singh printf("Error: Multiple -s arguments\n"); 1134bbf8e30SJasvinder Singh return -1; 1144bbf8e30SJasvinder Singh } 1154bbf8e30SJasvinder Singh s_present = 1; 1164bbf8e30SJasvinder Singh 1174bbf8e30SJasvinder Singh if (!strlen(optarg)) { 1184bbf8e30SJasvinder Singh printf("Error: Argument for -s not provided\n"); 1194bbf8e30SJasvinder Singh return -1; 1204bbf8e30SJasvinder Singh } 1214bbf8e30SJasvinder Singh 1224bbf8e30SJasvinder Singh app.script_name = strdup(optarg); 1234bbf8e30SJasvinder Singh if (app.script_name == NULL) { 1244bbf8e30SJasvinder Singh printf("Error: Not enough memory\n"); 1254bbf8e30SJasvinder Singh return -1; 1264bbf8e30SJasvinder Singh } 1274bbf8e30SJasvinder Singh break; 1284bbf8e30SJasvinder Singh 1294bbf8e30SJasvinder Singh default: 1304bbf8e30SJasvinder Singh printf(usage, app_name); 1314bbf8e30SJasvinder Singh return -1; 1324bbf8e30SJasvinder Singh } 1334bbf8e30SJasvinder Singh 1344bbf8e30SJasvinder Singh optind = 1; /* reset getopt lib */ 1354bbf8e30SJasvinder Singh 1364bbf8e30SJasvinder Singh return 0; 1374bbf8e30SJasvinder Singh } 1384bbf8e30SJasvinder Singh 13977a33467SCristian Dumitrescu int 14098a16481SDavid Marchand main(int argc, char **argv) 14177a33467SCristian Dumitrescu { 1424bbf8e30SJasvinder Singh struct conn *conn; 143fbc74e66SJasvinder Singh int status; 14477a33467SCristian Dumitrescu 1454bbf8e30SJasvinder Singh /* Parse application arguments */ 1464bbf8e30SJasvinder Singh status = parse_args(argc, argv); 1474bbf8e30SJasvinder Singh if (status < 0) 1484bbf8e30SJasvinder Singh return status; 1494bbf8e30SJasvinder Singh 150fbc74e66SJasvinder Singh /* EAL */ 151fbc74e66SJasvinder Singh status = rte_eal_init(argc, argv); 152fbc74e66SJasvinder Singh if (status < 0) { 153fbc74e66SJasvinder Singh printf("Error: EAL initialization failed (%d)\n", status); 154fbc74e66SJasvinder Singh return status; 155fbc74e66SJasvinder Singh }; 15677a33467SCristian Dumitrescu 1574bbf8e30SJasvinder Singh /* Connectivity */ 1584bbf8e30SJasvinder Singh conn = conn_init(&app.conn); 1594bbf8e30SJasvinder Singh if (conn == NULL) { 1604bbf8e30SJasvinder Singh printf("Error: Connectivity initialization failed (%d)\n", 1614bbf8e30SJasvinder Singh status); 1624bbf8e30SJasvinder Singh return status; 1634bbf8e30SJasvinder Singh }; 1644bbf8e30SJasvinder Singh 1656bfe74f8SJasvinder Singh /* Mempool */ 1666bfe74f8SJasvinder Singh status = mempool_init(); 1676bfe74f8SJasvinder Singh if (status) { 1686bfe74f8SJasvinder Singh printf("Error: Mempool initialization failed (%d)\n", status); 1696bfe74f8SJasvinder Singh return status; 1706bfe74f8SJasvinder Singh } 1716bfe74f8SJasvinder Singh 172133c2c65SJasvinder Singh /* Link */ 173133c2c65SJasvinder Singh status = link_init(); 174133c2c65SJasvinder Singh if (status) { 175133c2c65SJasvinder Singh printf("Error: Link initialization failed (%d)\n", status); 176133c2c65SJasvinder Singh return status; 177133c2c65SJasvinder Singh } 178133c2c65SJasvinder Singh 179*8245472cSJasvinder Singh /* SWQ */ 180*8245472cSJasvinder Singh status = swq_init(); 181*8245472cSJasvinder Singh if (status) { 182*8245472cSJasvinder Singh printf("Error: SWQ initialization failed (%d)\n", status); 183*8245472cSJasvinder Singh return status; 184*8245472cSJasvinder Singh } 185*8245472cSJasvinder Singh 1864bbf8e30SJasvinder Singh /* Script */ 1874bbf8e30SJasvinder Singh if (app.script_name) 1884bbf8e30SJasvinder Singh cli_script_process(app.script_name, 1894bbf8e30SJasvinder Singh app.conn.msg_in_len_max, 1904bbf8e30SJasvinder Singh app.conn.msg_out_len_max); 1914bbf8e30SJasvinder Singh 1924bbf8e30SJasvinder Singh /* Dispatch loop */ 1934bbf8e30SJasvinder Singh for ( ; ; ) { 1944bbf8e30SJasvinder Singh conn_poll_for_conn(conn); 1954bbf8e30SJasvinder Singh 1964bbf8e30SJasvinder Singh conn_poll_for_msg(conn); 1974bbf8e30SJasvinder Singh } 19877a33467SCristian Dumitrescu } 199