1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation. 3 * Copyright (c) 2010, Keith Wiles <keith.wiles@windriver.com> 4 * All rights reserved. 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <errno.h> 11 12 #include <rte_string_fns.h> 13 #include "cmdline_parse.h" 14 #include "cmdline_parse_portlist.h" 15 16 struct cmdline_token_ops cmdline_token_portlist_ops = { 17 .parse = cmdline_parse_portlist, 18 .complete_get_nb = NULL, 19 .complete_get_elt = NULL, 20 .get_help = cmdline_get_help_portlist, 21 }; 22 23 static void 24 parse_set_list(cmdline_portlist_t *pl, size_t low, size_t high) 25 { 26 do { 27 pl->map |= (1 << low++); 28 } while (low <= high); 29 } 30 31 static int 32 parse_ports(cmdline_portlist_t *pl, const char *str) 33 { 34 size_t ps, pe; 35 const char *first, *last; 36 char *end; 37 38 for (first = str, last = first; 39 first != NULL && last != NULL; 40 first = last + 1) { 41 42 last = strchr(first, ','); 43 44 errno = 0; 45 ps = strtoul(first, &end, 10); 46 if (errno != 0 || end == first || 47 (end[0] != '-' && end[0] != 0 && end != last)) 48 return -1; 49 50 /* Support for N-M portlist format */ 51 if (end[0] == '-') { 52 errno = 0; 53 first = end + 1; 54 pe = strtoul(first, &end, 10); 55 if (errno != 0 || end == first || 56 (end[0] != 0 && end != last)) 57 return -1; 58 } else { 59 pe = ps; 60 } 61 62 if (ps > pe || pe >= sizeof (pl->map) * 8) 63 return -1; 64 65 parse_set_list(pl, ps, pe); 66 } 67 68 return 0; 69 } 70 71 int 72 cmdline_parse_portlist(__rte_unused cmdline_parse_token_hdr_t *tk, 73 const char *buf, void *res, unsigned ressize) 74 { 75 unsigned int token_len = 0; 76 char portlist_str[PORTLIST_TOKEN_SIZE+1]; 77 cmdline_portlist_t *pl; 78 79 if (!buf || ! *buf) 80 return -1; 81 82 if (res && ressize < sizeof(cmdline_portlist_t)) 83 return -1; 84 85 pl = res; 86 87 while (!cmdline_isendoftoken(buf[token_len]) && 88 (token_len < PORTLIST_TOKEN_SIZE)) 89 token_len++; 90 91 if (token_len >= PORTLIST_TOKEN_SIZE) 92 return -1; 93 94 strlcpy(portlist_str, buf, token_len + 1); 95 96 if (pl) { 97 pl->map = 0; 98 if (strcmp("all", portlist_str) == 0) 99 pl->map = UINT32_MAX; 100 else if (parse_ports(pl, portlist_str) != 0) 101 return -1; 102 } 103 104 return token_len; 105 } 106 107 int 108 cmdline_get_help_portlist(__rte_unused cmdline_parse_token_hdr_t *tk, 109 char *dstbuf, unsigned int size) 110 { 111 int ret; 112 ret = snprintf(dstbuf, size, "range of ports as 3,4-6,8-19,20"); 113 if (ret < 0) 114 return -1; 115 return 0; 116 } 117