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