1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation. 3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org> 4 * All rights reserved. 5 */ 6 7 #include <stdio.h> 8 #include <stdint.h> 9 #include <string.h> 10 #include <stdlib.h> 11 #include <stdarg.h> 12 #include <errno.h> 13 #include <netinet/in.h> 14 #include <termios.h> 15 #ifdef RTE_EXEC_ENV_FREEBSD 16 #include <sys/socket.h> 17 #endif 18 19 #include <cmdline_rdline.h> 20 #include <cmdline_parse.h> 21 #include <cmdline_parse_ipaddr.h> 22 #include <cmdline_parse_num.h> 23 #include <cmdline_parse_string.h> 24 #include <cmdline.h> 25 26 #include <rte_string_fns.h> 27 28 #include "parse_obj_list.h" 29 30 struct object_list global_obj_list; 31 32 /* not defined under linux */ 33 #ifndef NIPQUAD 34 #define NIPQUAD_FMT "%u.%u.%u.%u" 35 #define NIPQUAD(addr) \ 36 (unsigned)((unsigned char *)&addr)[0], \ 37 (unsigned)((unsigned char *)&addr)[1], \ 38 (unsigned)((unsigned char *)&addr)[2], \ 39 (unsigned)((unsigned char *)&addr)[3] 40 #endif 41 42 #ifndef NIP6 43 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x" 44 #define NIP6(addr) \ 45 (unsigned)((addr).s6_addr[0]), \ 46 (unsigned)((addr).s6_addr[1]), \ 47 (unsigned)((addr).s6_addr[2]), \ 48 (unsigned)((addr).s6_addr[3]), \ 49 (unsigned)((addr).s6_addr[4]), \ 50 (unsigned)((addr).s6_addr[5]), \ 51 (unsigned)((addr).s6_addr[6]), \ 52 (unsigned)((addr).s6_addr[7]), \ 53 (unsigned)((addr).s6_addr[8]), \ 54 (unsigned)((addr).s6_addr[9]), \ 55 (unsigned)((addr).s6_addr[10]), \ 56 (unsigned)((addr).s6_addr[11]), \ 57 (unsigned)((addr).s6_addr[12]), \ 58 (unsigned)((addr).s6_addr[13]), \ 59 (unsigned)((addr).s6_addr[14]), \ 60 (unsigned)((addr).s6_addr[15]) 61 #endif 62 63 64 /**********************************************************/ 65 66 struct cmd_obj_del_show_result { 67 cmdline_fixed_string_t action; 68 struct object *obj; 69 }; 70 71 static void cmd_obj_del_show_parsed(void *parsed_result, 72 struct cmdline *cl, 73 __attribute__((unused)) void *data) 74 { 75 struct cmd_obj_del_show_result *res = parsed_result; 76 char ip_str[INET6_ADDRSTRLEN]; 77 78 if (res->obj->ip.family == AF_INET) 79 snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT, 80 NIPQUAD(res->obj->ip.addr.ipv4)); 81 else 82 snprintf(ip_str, sizeof(ip_str), NIP6_FMT, 83 NIP6(res->obj->ip.addr.ipv6)); 84 85 if (strcmp(res->action, "del") == 0) { 86 SLIST_REMOVE(&global_obj_list, res->obj, object, next); 87 cmdline_printf(cl, "Object %s removed, ip=%s\n", 88 res->obj->name, ip_str); 89 free(res->obj); 90 } 91 else if (strcmp(res->action, "show") == 0) { 92 cmdline_printf(cl, "Object %s, ip=%s\n", 93 res->obj->name, ip_str); 94 } 95 } 96 97 cmdline_parse_token_string_t cmd_obj_action = 98 TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result, 99 action, "show#del"); 100 parse_token_obj_list_t cmd_obj_obj = 101 TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj, 102 &global_obj_list); 103 104 cmdline_parse_inst_t cmd_obj_del_show = { 105 .f = cmd_obj_del_show_parsed, /* function to call */ 106 .data = NULL, /* 2nd arg of func */ 107 .help_str = "Show/del an object", 108 .tokens = { /* token list, NULL terminated */ 109 (void *)&cmd_obj_action, 110 (void *)&cmd_obj_obj, 111 NULL, 112 }, 113 }; 114 115 /**********************************************************/ 116 117 struct cmd_obj_add_result { 118 cmdline_fixed_string_t action; 119 cmdline_fixed_string_t name; 120 cmdline_ipaddr_t ip; 121 }; 122 123 static void cmd_obj_add_parsed(void *parsed_result, 124 struct cmdline *cl, 125 __attribute__((unused)) void *data) 126 { 127 struct cmd_obj_add_result *res = parsed_result; 128 struct object *o; 129 char ip_str[INET6_ADDRSTRLEN]; 130 131 SLIST_FOREACH(o, &global_obj_list, next) { 132 if (!strcmp(res->name, o->name)) { 133 cmdline_printf(cl, "Object %s already exist\n", res->name); 134 return; 135 } 136 break; 137 } 138 139 o = malloc(sizeof(*o)); 140 if (!o) { 141 cmdline_printf(cl, "mem error\n"); 142 return; 143 } 144 strlcpy(o->name, res->name, sizeof(o->name)); 145 o->ip = res->ip; 146 SLIST_INSERT_HEAD(&global_obj_list, o, next); 147 148 if (o->ip.family == AF_INET) 149 snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT, 150 NIPQUAD(o->ip.addr.ipv4)); 151 else 152 snprintf(ip_str, sizeof(ip_str), NIP6_FMT, 153 NIP6(o->ip.addr.ipv6)); 154 155 cmdline_printf(cl, "Object %s added, ip=%s\n", 156 o->name, ip_str); 157 } 158 159 cmdline_parse_token_string_t cmd_obj_action_add = 160 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add"); 161 cmdline_parse_token_string_t cmd_obj_name = 162 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL); 163 cmdline_parse_token_ipaddr_t cmd_obj_ip = 164 TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip); 165 166 cmdline_parse_inst_t cmd_obj_add = { 167 .f = cmd_obj_add_parsed, /* function to call */ 168 .data = NULL, /* 2nd arg of func */ 169 .help_str = "Add an object (name, val)", 170 .tokens = { /* token list, NULL terminated */ 171 (void *)&cmd_obj_action_add, 172 (void *)&cmd_obj_name, 173 (void *)&cmd_obj_ip, 174 NULL, 175 }, 176 }; 177 178 /**********************************************************/ 179 180 struct cmd_help_result { 181 cmdline_fixed_string_t help; 182 }; 183 184 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result, 185 struct cmdline *cl, 186 __attribute__((unused)) void *data) 187 { 188 cmdline_printf(cl, 189 "Demo example of command line interface in RTE\n\n" 190 "This is a readline-like interface that can be used to\n" 191 "debug your RTE application. It supports some features\n" 192 "of GNU readline like completion, cut/paste, and some\n" 193 "other special bindings.\n\n" 194 "This demo shows how rte_cmdline library can be\n" 195 "extended to handle a list of objects. There are\n" 196 "3 commands:\n" 197 "- add obj_name IP\n" 198 "- del obj_name\n" 199 "- show obj_name\n\n"); 200 } 201 202 cmdline_parse_token_string_t cmd_help_help = 203 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help"); 204 205 cmdline_parse_inst_t cmd_help = { 206 .f = cmd_help_parsed, /* function to call */ 207 .data = NULL, /* 2nd arg of func */ 208 .help_str = "show help", 209 .tokens = { /* token list, NULL terminated */ 210 (void *)&cmd_help_help, 211 NULL, 212 }, 213 }; 214 215 216 /**********************************************************/ 217 /**********************************************************/ 218 /****** CONTEXT (list of instruction) */ 219 220 cmdline_parse_ctx_t main_ctx[] = { 221 (cmdline_parse_inst_t *)&cmd_obj_del_show, 222 (cmdline_parse_inst_t *)&cmd_obj_add, 223 (cmdline_parse_inst_t *)&cmd_help, 224 NULL, 225 }; 226