1b77f6600SCristian Dumitrescu /* SPDX-License-Identifier: BSD-3-Clause 2b77f6600SCristian Dumitrescu * Copyright(c) 2020 Intel Corporation 3b77f6600SCristian Dumitrescu */ 4b77f6600SCristian Dumitrescu 5b77f6600SCristian Dumitrescu #include <stdio.h> 6b77f6600SCristian Dumitrescu #include <string.h> 7b77f6600SCristian Dumitrescu #include <fcntl.h> 8b77f6600SCristian Dumitrescu #include <unistd.h> 9b77f6600SCristian Dumitrescu #include <getopt.h> 10b77f6600SCristian Dumitrescu 11b77f6600SCristian Dumitrescu #include <rte_launch.h> 12b77f6600SCristian Dumitrescu #include <rte_eal.h> 13b77f6600SCristian Dumitrescu 14*5f657a7fSCristian Dumitrescu #include "conn.h" 15b77f6600SCristian Dumitrescu #include "obj.h" 16b77f6600SCristian Dumitrescu #include "thread.h" 17b77f6600SCristian Dumitrescu 18*5f657a7fSCristian Dumitrescu static const char usage[] = 19*5f657a7fSCristian Dumitrescu "%s EAL_ARGS -- [-h HOST] [-p PORT] [-s SCRIPT]\n"; 20*5f657a7fSCristian Dumitrescu 21*5f657a7fSCristian Dumitrescu static struct app_params { 22*5f657a7fSCristian Dumitrescu struct conn_params conn; 23*5f657a7fSCristian Dumitrescu char *script_name; 24*5f657a7fSCristian Dumitrescu } app = { 25*5f657a7fSCristian Dumitrescu .conn = { 26*5f657a7fSCristian Dumitrescu .welcome = "\nWelcome!\n\n", 27*5f657a7fSCristian Dumitrescu .prompt = "pipeline> ", 28*5f657a7fSCristian Dumitrescu .addr = "0.0.0.0", 29*5f657a7fSCristian Dumitrescu .port = 8086, 30*5f657a7fSCristian Dumitrescu .buf_size = 1024 * 1024, 31*5f657a7fSCristian Dumitrescu .msg_in_len_max = 1024, 32*5f657a7fSCristian Dumitrescu .msg_out_len_max = 1024 * 1024, 33*5f657a7fSCristian Dumitrescu .msg_handle = NULL, 34*5f657a7fSCristian Dumitrescu .msg_handle_arg = NULL, /* set later. */ 35*5f657a7fSCristian Dumitrescu }, 36*5f657a7fSCristian Dumitrescu .script_name = NULL, 37*5f657a7fSCristian Dumitrescu }; 38*5f657a7fSCristian Dumitrescu 39*5f657a7fSCristian Dumitrescu static int 40*5f657a7fSCristian Dumitrescu parse_args(int argc, char **argv) 41*5f657a7fSCristian Dumitrescu { 42*5f657a7fSCristian Dumitrescu char *app_name = argv[0]; 43*5f657a7fSCristian Dumitrescu struct option lgopts[] = { 44*5f657a7fSCristian Dumitrescu { NULL, 0, 0, 0 } 45*5f657a7fSCristian Dumitrescu }; 46*5f657a7fSCristian Dumitrescu int opt, option_index; 47*5f657a7fSCristian Dumitrescu int h_present, p_present, s_present, n_args, i; 48*5f657a7fSCristian Dumitrescu 49*5f657a7fSCristian Dumitrescu /* Skip EAL input args */ 50*5f657a7fSCristian Dumitrescu n_args = argc; 51*5f657a7fSCristian Dumitrescu for (i = 0; i < n_args; i++) 52*5f657a7fSCristian Dumitrescu if (strcmp(argv[i], "--") == 0) { 53*5f657a7fSCristian Dumitrescu argc -= i; 54*5f657a7fSCristian Dumitrescu argv += i; 55*5f657a7fSCristian Dumitrescu break; 56*5f657a7fSCristian Dumitrescu } 57*5f657a7fSCristian Dumitrescu 58*5f657a7fSCristian Dumitrescu if (i == n_args) 59*5f657a7fSCristian Dumitrescu return 0; 60*5f657a7fSCristian Dumitrescu 61*5f657a7fSCristian Dumitrescu /* Parse args */ 62*5f657a7fSCristian Dumitrescu h_present = 0; 63*5f657a7fSCristian Dumitrescu p_present = 0; 64*5f657a7fSCristian Dumitrescu s_present = 0; 65*5f657a7fSCristian Dumitrescu 66*5f657a7fSCristian Dumitrescu while ((opt = getopt_long(argc, argv, "h:p:s:", lgopts, &option_index)) 67*5f657a7fSCristian Dumitrescu != EOF) 68*5f657a7fSCristian Dumitrescu switch (opt) { 69*5f657a7fSCristian Dumitrescu case 'h': 70*5f657a7fSCristian Dumitrescu if (h_present) { 71*5f657a7fSCristian Dumitrescu printf("Error: Multiple -h arguments\n"); 72*5f657a7fSCristian Dumitrescu return -1; 73*5f657a7fSCristian Dumitrescu } 74*5f657a7fSCristian Dumitrescu h_present = 1; 75*5f657a7fSCristian Dumitrescu 76*5f657a7fSCristian Dumitrescu if (!strlen(optarg)) { 77*5f657a7fSCristian Dumitrescu printf("Error: Argument for -h not provided\n"); 78*5f657a7fSCristian Dumitrescu return -1; 79*5f657a7fSCristian Dumitrescu } 80*5f657a7fSCristian Dumitrescu 81*5f657a7fSCristian Dumitrescu app.conn.addr = strdup(optarg); 82*5f657a7fSCristian Dumitrescu if (app.conn.addr == NULL) { 83*5f657a7fSCristian Dumitrescu printf("Error: Not enough memory\n"); 84*5f657a7fSCristian Dumitrescu return -1; 85*5f657a7fSCristian Dumitrescu } 86*5f657a7fSCristian Dumitrescu break; 87*5f657a7fSCristian Dumitrescu 88*5f657a7fSCristian Dumitrescu case 'p': 89*5f657a7fSCristian Dumitrescu if (p_present) { 90*5f657a7fSCristian Dumitrescu printf("Error: Multiple -p arguments\n"); 91*5f657a7fSCristian Dumitrescu return -1; 92*5f657a7fSCristian Dumitrescu } 93*5f657a7fSCristian Dumitrescu p_present = 1; 94*5f657a7fSCristian Dumitrescu 95*5f657a7fSCristian Dumitrescu if (!strlen(optarg)) { 96*5f657a7fSCristian Dumitrescu printf("Error: Argument for -p not provided\n"); 97*5f657a7fSCristian Dumitrescu return -1; 98*5f657a7fSCristian Dumitrescu } 99*5f657a7fSCristian Dumitrescu 100*5f657a7fSCristian Dumitrescu app.conn.port = (uint16_t) atoi(optarg); 101*5f657a7fSCristian Dumitrescu break; 102*5f657a7fSCristian Dumitrescu 103*5f657a7fSCristian Dumitrescu case 's': 104*5f657a7fSCristian Dumitrescu if (s_present) { 105*5f657a7fSCristian Dumitrescu printf("Error: Multiple -s arguments\n"); 106*5f657a7fSCristian Dumitrescu return -1; 107*5f657a7fSCristian Dumitrescu } 108*5f657a7fSCristian Dumitrescu s_present = 1; 109*5f657a7fSCristian Dumitrescu 110*5f657a7fSCristian Dumitrescu if (!strlen(optarg)) { 111*5f657a7fSCristian Dumitrescu printf("Error: Argument for -s not provided\n"); 112*5f657a7fSCristian Dumitrescu return -1; 113*5f657a7fSCristian Dumitrescu } 114*5f657a7fSCristian Dumitrescu 115*5f657a7fSCristian Dumitrescu app.script_name = strdup(optarg); 116*5f657a7fSCristian Dumitrescu if (app.script_name == NULL) { 117*5f657a7fSCristian Dumitrescu printf("Error: Not enough memory\n"); 118*5f657a7fSCristian Dumitrescu return -1; 119*5f657a7fSCristian Dumitrescu } 120*5f657a7fSCristian Dumitrescu break; 121*5f657a7fSCristian Dumitrescu 122*5f657a7fSCristian Dumitrescu default: 123*5f657a7fSCristian Dumitrescu printf(usage, app_name); 124*5f657a7fSCristian Dumitrescu return -1; 125*5f657a7fSCristian Dumitrescu } 126*5f657a7fSCristian Dumitrescu 127*5f657a7fSCristian Dumitrescu optind = 1; /* reset getopt lib */ 128*5f657a7fSCristian Dumitrescu 129*5f657a7fSCristian Dumitrescu return 0; 130*5f657a7fSCristian Dumitrescu } 131*5f657a7fSCristian Dumitrescu 132b77f6600SCristian Dumitrescu int 133b77f6600SCristian Dumitrescu main(int argc, char **argv) 134b77f6600SCristian Dumitrescu { 135*5f657a7fSCristian Dumitrescu struct conn *conn; 136b77f6600SCristian Dumitrescu struct obj *obj; 137b77f6600SCristian Dumitrescu int status; 138b77f6600SCristian Dumitrescu 139*5f657a7fSCristian Dumitrescu /* Parse application arguments */ 140*5f657a7fSCristian Dumitrescu status = parse_args(argc, argv); 141*5f657a7fSCristian Dumitrescu if (status < 0) 142*5f657a7fSCristian Dumitrescu return status; 143*5f657a7fSCristian Dumitrescu 144b77f6600SCristian Dumitrescu /* EAL */ 145b77f6600SCristian Dumitrescu status = rte_eal_init(argc, argv); 146b77f6600SCristian Dumitrescu if (status < 0) { 147b77f6600SCristian Dumitrescu printf("Error: EAL initialization failed (%d)\n", status); 148b77f6600SCristian Dumitrescu return status; 149b77f6600SCristian Dumitrescu }; 150b77f6600SCristian Dumitrescu 151b77f6600SCristian Dumitrescu /* Obj */ 152b77f6600SCristian Dumitrescu obj = obj_init(); 153b77f6600SCristian Dumitrescu if (!obj) { 154b77f6600SCristian Dumitrescu printf("Error: Obj initialization failed (%d)\n", status); 155b77f6600SCristian Dumitrescu return status; 156b77f6600SCristian Dumitrescu } 157b77f6600SCristian Dumitrescu 158b77f6600SCristian Dumitrescu /* Thread */ 159b77f6600SCristian Dumitrescu status = thread_init(); 160b77f6600SCristian Dumitrescu if (status) { 161b77f6600SCristian Dumitrescu printf("Error: Thread initialization failed (%d)\n", status); 162b77f6600SCristian Dumitrescu return status; 163b77f6600SCristian Dumitrescu } 164b77f6600SCristian Dumitrescu 165b77f6600SCristian Dumitrescu rte_eal_mp_remote_launch( 166b77f6600SCristian Dumitrescu thread_main, 167b77f6600SCristian Dumitrescu NULL, 168b77f6600SCristian Dumitrescu SKIP_MASTER); 169b77f6600SCristian Dumitrescu 170*5f657a7fSCristian Dumitrescu /* Connectivity */ 171*5f657a7fSCristian Dumitrescu app.conn.msg_handle_arg = obj; 172*5f657a7fSCristian Dumitrescu conn = conn_init(&app.conn); 173*5f657a7fSCristian Dumitrescu if (!conn) { 174*5f657a7fSCristian Dumitrescu printf("Error: Connectivity initialization failed (%d)\n", 175*5f657a7fSCristian Dumitrescu status); 176*5f657a7fSCristian Dumitrescu return status; 177*5f657a7fSCristian Dumitrescu }; 178*5f657a7fSCristian Dumitrescu 179*5f657a7fSCristian Dumitrescu /* Dispatch loop */ 180*5f657a7fSCristian Dumitrescu for ( ; ; ) { 181*5f657a7fSCristian Dumitrescu conn_poll_for_conn(conn); 182*5f657a7fSCristian Dumitrescu 183*5f657a7fSCristian Dumitrescu conn_poll_for_msg(conn); 184*5f657a7fSCristian Dumitrescu } 185b77f6600SCristian Dumitrescu } 186