kvargs: use SPDX tags
[dpdk.git] / lib / librte_kvargs / rte_kvargs.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2013 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  */
5
6 #include <string.h>
7 #include <stdlib.h>
8
9 #include <rte_log.h>
10 #include <rte_string_fns.h>
11
12 #include "rte_kvargs.h"
13
14 /*
15  * Receive a string with a list of arguments following the pattern
16  * key=value;key=value;... and insert them into the list.
17  * strtok() is used so the params string will be copied to be modified.
18  */
19 static int
20 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
21 {
22         unsigned i;
23         char *str;
24         char *ctx1 = NULL;
25         char *ctx2 = NULL;
26
27         /* Copy the const char *params to a modifiable string
28          * to pass to rte_strsplit
29          */
30         kvlist->str = strdup(params);
31         if (kvlist->str == NULL) {
32                 RTE_LOG(ERR, PMD, "Cannot parse arguments: not enough memory\n");
33                 return -1;
34         }
35
36         /* browse each key/value pair and add it in kvlist */
37         str = kvlist->str;
38         while ((str = strtok_r(str, RTE_KVARGS_PAIRS_DELIM, &ctx1)) != NULL) {
39
40                 i = kvlist->count;
41                 if (i >= RTE_KVARGS_MAX) {
42                         RTE_LOG(ERR, PMD, "Cannot parse arguments: list full\n");
43                         return -1;
44                 }
45
46                 kvlist->pairs[i].key = strtok_r(str, RTE_KVARGS_KV_DELIM, &ctx2);
47                 kvlist->pairs[i].value = strtok_r(NULL, RTE_KVARGS_KV_DELIM, &ctx2);
48                 if (kvlist->pairs[i].key == NULL || kvlist->pairs[i].value == NULL) {
49                         RTE_LOG(ERR, PMD,
50                                 "Cannot parse arguments: wrong key or value\n"
51                                 "params=<%s>\n", params);
52                         return -1;
53                 }
54
55                 kvlist->count++;
56                 str = NULL;
57         }
58
59         return 0;
60 }
61
62 /*
63  * Determine whether a key is valid or not by looking
64  * into a list of valid keys.
65  */
66 static int
67 is_valid_key(const char * const valid[], const char *key_match)
68 {
69         const char * const *valid_ptr;
70
71         for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++) {
72                 if (strcmp(key_match, *valid_ptr) == 0)
73                         return 1;
74         }
75         return 0;
76 }
77
78 /*
79  * Determine whether all keys are valid or not by looking
80  * into a list of valid keys.
81  */
82 static int
83 check_for_valid_keys(struct rte_kvargs *kvlist,
84                 const char * const valid[])
85 {
86         unsigned i, ret;
87         struct rte_kvargs_pair *pair;
88
89         for (i = 0; i < kvlist->count; i++) {
90                 pair = &kvlist->pairs[i];
91                 ret = is_valid_key(valid, pair->key);
92                 if (!ret) {
93                         RTE_LOG(ERR, PMD,
94                                 "Error parsing device, invalid key <%s>\n",
95                                 pair->key);
96                         return -1;
97                 }
98         }
99         return 0;
100 }
101
102 /*
103  * Return the number of times a given arg_name exists in the key/value list.
104  * E.g. given a list = { rx = 0, rx = 1, tx = 2 } the number of args for
105  * arg "rx" will be 2.
106  */
107 unsigned
108 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
109 {
110         const struct rte_kvargs_pair *pair;
111         unsigned i, ret;
112
113         ret = 0;
114         for (i = 0; i < kvlist->count; i++) {
115                 pair = &kvlist->pairs[i];
116                 if (key_match == NULL || strcmp(pair->key, key_match) == 0)
117                         ret++;
118         }
119
120         return ret;
121 }
122
123 /*
124  * For each matching key, call the given handler function.
125  */
126 int
127 rte_kvargs_process(const struct rte_kvargs *kvlist,
128                 const char *key_match,
129                 arg_handler_t handler,
130                 void *opaque_arg)
131 {
132         const struct rte_kvargs_pair *pair;
133         unsigned i;
134
135         for (i = 0; i < kvlist->count; i++) {
136                 pair = &kvlist->pairs[i];
137                 if (key_match == NULL || strcmp(pair->key, key_match) == 0) {
138                         if ((*handler)(pair->key, pair->value, opaque_arg) < 0)
139                                 return -1;
140                 }
141         }
142         return 0;
143 }
144
145 /* free the rte_kvargs structure */
146 void
147 rte_kvargs_free(struct rte_kvargs *kvlist)
148 {
149         if (!kvlist)
150                 return;
151
152         free(kvlist->str);
153         free(kvlist);
154 }
155
156 /*
157  * Parse the arguments "key=value;key=value;..." string and return
158  * an allocated structure that contains a key/value list. Also
159  * check if only valid keys were used.
160  */
161 struct rte_kvargs *
162 rte_kvargs_parse(const char *args, const char * const valid_keys[])
163 {
164         struct rte_kvargs *kvlist;
165
166         kvlist = malloc(sizeof(*kvlist));
167         if (kvlist == NULL)
168                 return NULL;
169         memset(kvlist, 0, sizeof(*kvlist));
170
171         if (rte_kvargs_tokenize(kvlist, args) < 0) {
172                 rte_kvargs_free(kvlist);
173                 return NULL;
174         }
175
176         if (valid_keys != NULL && check_for_valid_keys(kvlist, valid_keys) < 0) {
177                 rte_kvargs_free(kvlist);
178                 return NULL;
179         }
180
181         return kvlist;
182 }