New upstream version 18.08
[deb_dpdk.git] / lib / librte_kvargs / rte_kvargs.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2013 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  */
5
6 #ifndef _RTE_KVARGS_H_
7 #define _RTE_KVARGS_H_
8
9 /**
10  * @file
11  * RTE Argument parsing
12  *
13  * This module can be used to parse arguments whose format is
14  * key1=value1,key2=value2,key3=value3,...
15  *
16  * The same key can appear several times with the same or a different
17  * value. Indeed, the arguments are stored as a list of key/values
18  * associations and not as a dictionary.
19  *
20  * This file provides some helpers that are especially used by virtual
21  * ethernet devices at initialization for arguments parsing.
22  */
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #include <rte_compat.h>
29
30 /** Maximum number of key/value associations */
31 #define RTE_KVARGS_MAX 32
32
33 /** separator character used between each pair */
34 #define RTE_KVARGS_PAIRS_DELIM  ","
35
36 /** separator character used between key and value */
37 #define RTE_KVARGS_KV_DELIM     "="
38
39 /** Type of callback function used by rte_kvargs_process() */
40 typedef int (*arg_handler_t)(const char *key, const char *value, void *opaque);
41
42 /** A key/value association */
43 struct rte_kvargs_pair {
44         char *key;      /**< the name (key) of the association  */
45         char *value;    /**< the value associated to that key */
46 };
47
48 /** Store a list of key/value associations */
49 struct rte_kvargs {
50         char *str;      /**< copy of the argument string */
51         unsigned count; /**< number of entries in the list */
52         struct rte_kvargs_pair pairs[RTE_KVARGS_MAX]; /**< list of key/values */
53 };
54
55 /**
56  * Allocate a rte_kvargs and store key/value associations from a string
57  *
58  * The function allocates and fills a rte_kvargs structure from a given
59  * string whose format is key1=value1,key2=value2,...
60  *
61  * The structure can be freed with rte_kvargs_free().
62  *
63  * @param args
64  *   The input string containing the key/value associations
65  * @param valid_keys
66  *   A list of valid keys (table of const char *, the last must be NULL).
67  *   This argument is ignored if NULL
68  *
69  * @return
70  *   - A pointer to an allocated rte_kvargs structure on success
71  *   - NULL on error
72  */
73 struct rte_kvargs *rte_kvargs_parse(const char *args,
74                 const char *const valid_keys[]);
75
76 /**
77  * Allocate a rte_kvargs and store key/value associations from a string.
78  * This version will consider any byte from valid_ends as a possible
79  * terminating character, and will not parse beyond any of their occurrence.
80  *
81  * The function allocates and fills an rte_kvargs structure from a given
82  * string whose format is key1=value1,key2=value2,...
83  *
84  * The structure can be freed with rte_kvargs_free().
85  *
86  * @param args
87  *   The input string containing the key/value associations
88  *
89  * @param valid_keys
90  *   A list of valid keys (table of const char *, the last must be NULL).
91  *   This argument is ignored if NULL
92  *
93  * @param valid_ends
94  *   Acceptable terminating characters.
95  *   If NULL, the behavior is the same as ``rte_kvargs_parse``.
96  *
97  * @return
98  *   - A pointer to an allocated rte_kvargs structure on success
99  *   - NULL on error
100  */
101 __rte_experimental
102 struct rte_kvargs *rte_kvargs_parse_delim(const char *args,
103                 const char *const valid_keys[],
104                 const char *valid_ends);
105
106 /**
107  * Free a rte_kvargs structure
108  *
109  * Free a rte_kvargs structure previously allocated with
110  * rte_kvargs_parse().
111  *
112  * @param kvlist
113  *   The rte_kvargs structure
114  */
115 void rte_kvargs_free(struct rte_kvargs *kvlist);
116
117 /**
118  * Call a handler function for each key/value matching the key
119  *
120  * For each key/value association that matches the given key, calls the
121  * handler function with the for a given arg_name passing the value on the
122  * dictionary for that key and a given extra argument. If *kvlist* is NULL
123  * function does nothing.
124  *
125  * @param kvlist
126  *   The rte_kvargs structure
127  * @param key_match
128  *   The key on which the handler should be called, or NULL to process handler
129  *   on all associations
130  * @param handler
131  *   The function to call for each matching key
132  * @param opaque_arg
133  *   A pointer passed unchanged to the handler
134  *
135  * @return
136  *   - 0 on success
137  *   - Negative on error
138  */
139 int rte_kvargs_process(const struct rte_kvargs *kvlist,
140         const char *key_match, arg_handler_t handler, void *opaque_arg);
141
142 /**
143  * Count the number of associations matching the given key
144  *
145  * @param kvlist
146  *   The rte_kvargs structure
147  * @param key_match
148  *   The key that should match, or NULL to count all associations
149
150  * @return
151  *   The number of entries
152  */
153 unsigned rte_kvargs_count(const struct rte_kvargs *kvlist,
154         const char *key_match);
155
156 /**
157  * Generic kvarg handler for string comparison.
158  *
159  * This function can be used for a generic string comparison processing
160  * on a list of kvargs.
161  *
162  * @param key
163  *   kvarg pair key.
164  *
165  * @param value
166  *   kvarg pair value.
167  *
168  * @param opaque
169  *   Opaque pointer to a string.
170  *
171  * @return
172  *   0 if the strings match.
173  *   !0 otherwise or on error.
174  *
175  *   Unless strcmp, comparison ordering is not kept.
176  *   In order for rte_kvargs_process to stop processing on match error,
177  *   a negative value is returned even if strcmp had returned a positive one.
178  */
179 __rte_experimental
180 int rte_kvargs_strcmp(const char *key, const char *value, void *opaque);
181
182 #ifdef __cplusplus
183 }
184 #endif
185
186 #endif