1*af75078fSIntel /*- 2*af75078fSIntel * BSD LICENSE 3*af75078fSIntel * 4*af75078fSIntel * Copyright(c) 2010-2012 Intel Corporation. All rights reserved. 5*af75078fSIntel * All rights reserved. 6*af75078fSIntel * 7*af75078fSIntel * Redistribution and use in source and binary forms, with or without 8*af75078fSIntel * modification, are permitted provided that the following conditions 9*af75078fSIntel * are met: 10*af75078fSIntel * 11*af75078fSIntel * * Redistributions of source code must retain the above copyright 12*af75078fSIntel * notice, this list of conditions and the following disclaimer. 13*af75078fSIntel * * Redistributions in binary form must reproduce the above copyright 14*af75078fSIntel * notice, this list of conditions and the following disclaimer in 15*af75078fSIntel * the documentation and/or other materials provided with the 16*af75078fSIntel * distribution. 17*af75078fSIntel * * Neither the name of Intel Corporation nor the names of its 18*af75078fSIntel * contributors may be used to endorse or promote products derived 19*af75078fSIntel * from this software without specific prior written permission. 20*af75078fSIntel * 21*af75078fSIntel * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*af75078fSIntel * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*af75078fSIntel * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24*af75078fSIntel * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25*af75078fSIntel * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26*af75078fSIntel * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27*af75078fSIntel * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28*af75078fSIntel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29*af75078fSIntel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30*af75078fSIntel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31*af75078fSIntel * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*af75078fSIntel * 33*af75078fSIntel * version: DPDK.L.1.2.3-3 34*af75078fSIntel */ 35*af75078fSIntel 36*af75078fSIntel /* 37*af75078fSIntel * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org> 38*af75078fSIntel * All rights reserved. 39*af75078fSIntel * Redistribution and use in source and binary forms, with or without 40*af75078fSIntel * modification, are permitted provided that the following conditions are met: 41*af75078fSIntel * 42*af75078fSIntel * * Redistributions of source code must retain the above copyright 43*af75078fSIntel * notice, this list of conditions and the following disclaimer. 44*af75078fSIntel * * Redistributions in binary form must reproduce the above copyright 45*af75078fSIntel * notice, this list of conditions and the following disclaimer in the 46*af75078fSIntel * documentation and/or other materials provided with the distribution. 47*af75078fSIntel * * Neither the name of the University of California, Berkeley nor the 48*af75078fSIntel * names of its contributors may be used to endorse or promote products 49*af75078fSIntel * derived from this software without specific prior written permission. 50*af75078fSIntel * 51*af75078fSIntel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY 52*af75078fSIntel * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 53*af75078fSIntel * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 54*af75078fSIntel * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY 55*af75078fSIntel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 56*af75078fSIntel * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 57*af75078fSIntel * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 58*af75078fSIntel * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 59*af75078fSIntel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 60*af75078fSIntel * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61*af75078fSIntel */ 62*af75078fSIntel 63*af75078fSIntel #include <stdio.h> 64*af75078fSIntel #include <stdint.h> 65*af75078fSIntel #include <string.h> 66*af75078fSIntel #include <stdlib.h> 67*af75078fSIntel #include <stdarg.h> 68*af75078fSIntel #include <errno.h> 69*af75078fSIntel #include <netinet/in.h> 70*af75078fSIntel #include <termios.h> 71*af75078fSIntel #ifndef __linux__ 72*af75078fSIntel #include <net/socket.h> 73*af75078fSIntel #endif 74*af75078fSIntel 75*af75078fSIntel #include <cmdline_rdline.h> 76*af75078fSIntel #include <cmdline_parse.h> 77*af75078fSIntel #include <cmdline_parse_ipaddr.h> 78*af75078fSIntel #include <cmdline_parse_num.h> 79*af75078fSIntel #include <cmdline_parse_string.h> 80*af75078fSIntel #include <cmdline.h> 81*af75078fSIntel 82*af75078fSIntel #include <rte_string_fns.h> 83*af75078fSIntel 84*af75078fSIntel #include "parse_obj_list.h" 85*af75078fSIntel 86*af75078fSIntel struct object_list global_obj_list; 87*af75078fSIntel 88*af75078fSIntel /* not defined under linux */ 89*af75078fSIntel #ifndef NIPQUAD 90*af75078fSIntel #define NIPQUAD_FMT "%u.%u.%u.%u" 91*af75078fSIntel #define NIPQUAD(addr) \ 92*af75078fSIntel (unsigned)((unsigned char *)&addr)[0], \ 93*af75078fSIntel (unsigned)((unsigned char *)&addr)[1], \ 94*af75078fSIntel (unsigned)((unsigned char *)&addr)[2], \ 95*af75078fSIntel (unsigned)((unsigned char *)&addr)[3] 96*af75078fSIntel #endif 97*af75078fSIntel 98*af75078fSIntel #ifndef NIP6 99*af75078fSIntel #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x" 100*af75078fSIntel #define NIP6(addr) \ 101*af75078fSIntel (unsigned)((addr).s6_addr[0]), \ 102*af75078fSIntel (unsigned)((addr).s6_addr[1]), \ 103*af75078fSIntel (unsigned)((addr).s6_addr[2]), \ 104*af75078fSIntel (unsigned)((addr).s6_addr[3]), \ 105*af75078fSIntel (unsigned)((addr).s6_addr[4]), \ 106*af75078fSIntel (unsigned)((addr).s6_addr[5]), \ 107*af75078fSIntel (unsigned)((addr).s6_addr[6]), \ 108*af75078fSIntel (unsigned)((addr).s6_addr[7]), \ 109*af75078fSIntel (unsigned)((addr).s6_addr[8]), \ 110*af75078fSIntel (unsigned)((addr).s6_addr[9]), \ 111*af75078fSIntel (unsigned)((addr).s6_addr[10]), \ 112*af75078fSIntel (unsigned)((addr).s6_addr[11]), \ 113*af75078fSIntel (unsigned)((addr).s6_addr[12]), \ 114*af75078fSIntel (unsigned)((addr).s6_addr[13]), \ 115*af75078fSIntel (unsigned)((addr).s6_addr[14]), \ 116*af75078fSIntel (unsigned)((addr).s6_addr[15]) 117*af75078fSIntel #endif 118*af75078fSIntel 119*af75078fSIntel 120*af75078fSIntel /**********************************************************/ 121*af75078fSIntel 122*af75078fSIntel struct cmd_obj_del_show_result { 123*af75078fSIntel cmdline_fixed_string_t action; 124*af75078fSIntel struct object *obj; 125*af75078fSIntel }; 126*af75078fSIntel 127*af75078fSIntel static void cmd_obj_del_show_parsed(void *parsed_result, 128*af75078fSIntel struct cmdline *cl, 129*af75078fSIntel __attribute__((unused)) void *data) 130*af75078fSIntel { 131*af75078fSIntel struct cmd_obj_del_show_result *res = parsed_result; 132*af75078fSIntel char ip_str[INET6_ADDRSTRLEN]; 133*af75078fSIntel 134*af75078fSIntel if (res->obj->ip.family == AF_INET) 135*af75078fSIntel rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT, 136*af75078fSIntel NIPQUAD(res->obj->ip.addr.ipv4)); 137*af75078fSIntel else 138*af75078fSIntel rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT, 139*af75078fSIntel NIP6(res->obj->ip.addr.ipv6)); 140*af75078fSIntel 141*af75078fSIntel if (strcmp(res->action, "del") == 0) { 142*af75078fSIntel SLIST_REMOVE(&global_obj_list, res->obj, object, next); 143*af75078fSIntel cmdline_printf(cl, "Object %s removed, ip=%s\n", 144*af75078fSIntel res->obj->name, ip_str); 145*af75078fSIntel free(res->obj); 146*af75078fSIntel } 147*af75078fSIntel else if (strcmp(res->action, "show") == 0) { 148*af75078fSIntel cmdline_printf(cl, "Object %s, ip=%s\n", 149*af75078fSIntel res->obj->name, ip_str); 150*af75078fSIntel } 151*af75078fSIntel } 152*af75078fSIntel 153*af75078fSIntel cmdline_parse_token_string_t cmd_obj_action = 154*af75078fSIntel TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result, 155*af75078fSIntel action, "show#del"); 156*af75078fSIntel parse_token_obj_list_t cmd_obj_obj = 157*af75078fSIntel TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj, 158*af75078fSIntel &global_obj_list); 159*af75078fSIntel 160*af75078fSIntel cmdline_parse_inst_t cmd_obj_del_show = { 161*af75078fSIntel .f = cmd_obj_del_show_parsed, /* function to call */ 162*af75078fSIntel .data = NULL, /* 2nd arg of func */ 163*af75078fSIntel .help_str = "Show/del an object", 164*af75078fSIntel .tokens = { /* token list, NULL terminated */ 165*af75078fSIntel (void *)&cmd_obj_action, 166*af75078fSIntel (void *)&cmd_obj_obj, 167*af75078fSIntel NULL, 168*af75078fSIntel }, 169*af75078fSIntel }; 170*af75078fSIntel 171*af75078fSIntel /**********************************************************/ 172*af75078fSIntel 173*af75078fSIntel struct cmd_obj_add_result { 174*af75078fSIntel cmdline_fixed_string_t action; 175*af75078fSIntel cmdline_fixed_string_t name; 176*af75078fSIntel cmdline_ipaddr_t ip; 177*af75078fSIntel }; 178*af75078fSIntel 179*af75078fSIntel static void cmd_obj_add_parsed(void *parsed_result, 180*af75078fSIntel struct cmdline *cl, 181*af75078fSIntel __attribute__((unused)) void *data) 182*af75078fSIntel { 183*af75078fSIntel struct cmd_obj_add_result *res = parsed_result; 184*af75078fSIntel struct object *o; 185*af75078fSIntel char ip_str[INET6_ADDRSTRLEN]; 186*af75078fSIntel 187*af75078fSIntel SLIST_FOREACH(o, &global_obj_list, next) { 188*af75078fSIntel if (!strcmp(res->name, o->name)) { 189*af75078fSIntel cmdline_printf(cl, "Object %s already exist\n", res->name); 190*af75078fSIntel return; 191*af75078fSIntel } 192*af75078fSIntel break; 193*af75078fSIntel } 194*af75078fSIntel 195*af75078fSIntel o = malloc(sizeof(*o)); 196*af75078fSIntel if (!o) { 197*af75078fSIntel cmdline_printf(cl, "mem error\n"); 198*af75078fSIntel return; 199*af75078fSIntel } 200*af75078fSIntel rte_snprintf(o->name, sizeof(o->name), "%s", res->name); 201*af75078fSIntel o->ip = res->ip; 202*af75078fSIntel SLIST_INSERT_HEAD(&global_obj_list, o, next); 203*af75078fSIntel 204*af75078fSIntel if (o->ip.family == AF_INET) 205*af75078fSIntel rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT, 206*af75078fSIntel NIPQUAD(o->ip.addr.ipv4)); 207*af75078fSIntel else 208*af75078fSIntel rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT, 209*af75078fSIntel NIP6(o->ip.addr.ipv6)); 210*af75078fSIntel 211*af75078fSIntel cmdline_printf(cl, "Object %s added, ip=%s\n", 212*af75078fSIntel o->name, ip_str); 213*af75078fSIntel } 214*af75078fSIntel 215*af75078fSIntel cmdline_parse_token_string_t cmd_obj_action_add = 216*af75078fSIntel TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add"); 217*af75078fSIntel cmdline_parse_token_string_t cmd_obj_name = 218*af75078fSIntel TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL); 219*af75078fSIntel cmdline_parse_token_ipaddr_t cmd_obj_ip = 220*af75078fSIntel TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip); 221*af75078fSIntel 222*af75078fSIntel cmdline_parse_inst_t cmd_obj_add = { 223*af75078fSIntel .f = cmd_obj_add_parsed, /* function to call */ 224*af75078fSIntel .data = NULL, /* 2nd arg of func */ 225*af75078fSIntel .help_str = "Add an object (name, val)", 226*af75078fSIntel .tokens = { /* token list, NULL terminated */ 227*af75078fSIntel (void *)&cmd_obj_action_add, 228*af75078fSIntel (void *)&cmd_obj_name, 229*af75078fSIntel (void *)&cmd_obj_ip, 230*af75078fSIntel NULL, 231*af75078fSIntel }, 232*af75078fSIntel }; 233*af75078fSIntel 234*af75078fSIntel /**********************************************************/ 235*af75078fSIntel 236*af75078fSIntel struct cmd_help_result { 237*af75078fSIntel cmdline_fixed_string_t help; 238*af75078fSIntel }; 239*af75078fSIntel 240*af75078fSIntel static void cmd_help_parsed(__attribute__((unused)) void *parsed_result, 241*af75078fSIntel struct cmdline *cl, 242*af75078fSIntel __attribute__((unused)) void *data) 243*af75078fSIntel { 244*af75078fSIntel cmdline_printf(cl, 245*af75078fSIntel "Demo example of command line interface in RTE\n\n" 246*af75078fSIntel "This is a readline-like interface that can be used to\n" 247*af75078fSIntel "debug your RTE application. It supports some features\n" 248*af75078fSIntel "of GNU readline like completion, cut/paste, and some\n" 249*af75078fSIntel "other special bindings.\n\n" 250*af75078fSIntel "This demo shows how rte_cmdline library can be\n" 251*af75078fSIntel "extended to handle a list of objects. There are\n" 252*af75078fSIntel "3 commands:\n" 253*af75078fSIntel "- add obj_name IP\n" 254*af75078fSIntel "- del obj_name\n" 255*af75078fSIntel "- show obj_name\n\n"); 256*af75078fSIntel } 257*af75078fSIntel 258*af75078fSIntel cmdline_parse_token_string_t cmd_help_help = 259*af75078fSIntel TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help"); 260*af75078fSIntel 261*af75078fSIntel cmdline_parse_inst_t cmd_help = { 262*af75078fSIntel .f = cmd_help_parsed, /* function to call */ 263*af75078fSIntel .data = NULL, /* 2nd arg of func */ 264*af75078fSIntel .help_str = "show help", 265*af75078fSIntel .tokens = { /* token list, NULL terminated */ 266*af75078fSIntel (void *)&cmd_help_help, 267*af75078fSIntel NULL, 268*af75078fSIntel }, 269*af75078fSIntel }; 270*af75078fSIntel 271*af75078fSIntel 272*af75078fSIntel /**********************************************************/ 273*af75078fSIntel /**********************************************************/ 274*af75078fSIntel /****** CONTEXT (list of instruction) */ 275*af75078fSIntel 276*af75078fSIntel cmdline_parse_ctx_t main_ctx[] = { 277*af75078fSIntel (cmdline_parse_inst_t *)&cmd_obj_del_show, 278*af75078fSIntel (cmdline_parse_inst_t *)&cmd_obj_add, 279*af75078fSIntel (cmdline_parse_inst_t *)&cmd_help, 280*af75078fSIntel NULL, 281*af75078fSIntel }; 282*af75078fSIntel 283