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