devargs: add non-variadic parsing function
[dpdk.git] / lib / librte_eal / common / include / rte_devargs.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2014 6WIND S.A.
3  */
4
5 #ifndef _RTE_DEVARGS_H_
6 #define _RTE_DEVARGS_H_
7
8 /**
9  * @file
10  *
11  * RTE devargs: list of devices and their user arguments
12  *
13  * This file stores a list of devices and their arguments given by
14  * the user when a DPDK application is started. These devices can be PCI
15  * devices or virtual devices. These devices are stored at startup in a
16  * list of rte_devargs structures.
17  */
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 #include <stdio.h>
24 #include <sys/queue.h>
25 #include <rte_compat.h>
26 #include <rte_bus.h>
27
28 /**
29  * Type of generic device
30  */
31 enum rte_devtype {
32         RTE_DEVTYPE_WHITELISTED_PCI,
33         RTE_DEVTYPE_BLACKLISTED_PCI,
34         RTE_DEVTYPE_VIRTUAL,
35 };
36
37 /**
38  * Structure that stores a device given by the user with its arguments
39  *
40  * A user device is a physical or a virtual device given by the user to
41  * the DPDK application at startup through command line arguments.
42  *
43  * The structure stores the configuration of the device, its PCI
44  * identifier if it's a PCI device or the driver name if it's a virtual
45  * device.
46  */
47 struct rte_devargs {
48         /** Next in list. */
49         TAILQ_ENTRY(rte_devargs) next;
50         /** Type of device. */
51         enum rte_devtype type;
52         /** Device policy. */
53         enum rte_dev_policy policy;
54         /** Bus handle for the device. */
55         struct rte_bus *bus;
56         /** Name of the device. */
57         char name[RTE_DEV_NAME_MAX_LEN];
58         /** Arguments string as given by user or "" for no argument. */
59         char *args;
60 };
61
62 /**
63  * @deprecated
64  * Parse a devargs string.
65  *
66  * For PCI devices, the format of arguments string is "PCI_ADDR" or
67  * "PCI_ADDR,key=val,key2=val2,...". Examples: "08:00.1", "0000:5:00.0",
68  * "04:00.0,arg=val".
69  *
70  * For virtual devices, the format of arguments string is "DRIVER_NAME*"
71  * or "DRIVER_NAME*,key=val,key2=val2,...". Examples: "net_ring",
72  * "net_ring0", "net_pmdAnything,arg=0:arg2=1".
73  *
74  * The function parses the arguments string to get driver name and driver
75  * arguments.
76  *
77  * @param devargs_str
78  *   The arguments as given by the user.
79  * @param drvname
80  *   The pointer to the string to store parsed driver name.
81  * @param drvargs
82  *   The pointer to the string to store parsed driver arguments.
83  *
84  * @return
85  *   - 0 on success
86  *   - A negative value on error
87  */
88 __rte_deprecated
89 int rte_eal_parse_devargs_str(const char *devargs_str,
90                                 char **drvname, char **drvargs);
91
92 /**
93  * Parse a device string.
94  *
95  * Verify that a bus is capable of handling the device passed
96  * in argument. Store which bus will handle the device, its name
97  * and the eventual device parameters.
98  *
99  * The syntax is:
100  *
101  *     bus:device_identifier,arg1=val1,arg2=val2
102  *
103  * where "bus:" is the bus name followed by any character separator.
104  * The bus name is optional. If no bus name is specified, each bus
105  * will attempt to recognize the device identifier. The first one
106  * to succeed will be used.
107  *
108  * Examples:
109  *
110  *     pci:0000:05.00.0,arg=val
111  *     05.00.0,arg=val
112  *     vdev:net_ring0
113  *
114  * @param da
115  *   The devargs structure holding the device information.
116  *
117  * @param dev
118  *   String describing a device.
119  *
120  * @return
121  *   - 0 on success.
122  *   - Negative errno on error.
123  */
124 __rte_experimental
125 int
126 rte_devargs_parse(struct rte_devargs *da, const char *dev);
127
128 /**
129  * Parse a device string.
130  *
131  * Verify that a bus is capable of handling the device passed
132  * in argument. Store which bus will handle the device, its name
133  * and the eventual device parameters.
134  *
135  * The device string is built with a printf-like syntax.
136  *
137  * The syntax is:
138  *
139  *     bus:device_identifier,arg1=val1,arg2=val2
140  *
141  * where "bus:" is the bus name followed by any character separator.
142  * The bus name is optional. If no bus name is specified, each bus
143  * will attempt to recognize the device identifier. The first one
144  * to succeed will be used.
145  *
146  * Examples:
147  *
148  *     pci:0000:05.00.0,arg=val
149  *     05.00.0,arg=val
150  *     vdev:net_ring0
151  *
152  * @param da
153  *   The devargs structure holding the device information.
154  * @param format
155  *   Format string describing a device.
156  *
157  * @return
158  *   - 0 on success.
159  *   - Negative errno on error.
160  */
161 __rte_experimental
162 int
163 rte_devargs_parsef(struct rte_devargs *da,
164                    const char *format, ...)
165 __attribute__((format(printf, 2, 0)));
166
167 /**
168  * Insert an rte_devargs in the global list.
169  *
170  * @param da
171  *  The devargs structure to insert.
172  *
173  * @return
174  *   - 0 on success
175  *   - Negative on error.
176  */
177 __rte_experimental
178 int
179 rte_devargs_insert(struct rte_devargs *da);
180
181 /**
182  * Add a device to the user device list
183  * See rte_devargs_parse() for details.
184  *
185  * @param devtype
186  *   The type of the device.
187  * @param devargs_str
188  *   The arguments as given by the user.
189  *
190  * @return
191  *   - 0 on success
192  *   - A negative value on error
193  */
194 __rte_experimental
195 int rte_devargs_add(enum rte_devtype devtype, const char *devargs_str);
196
197 /**
198  * @deprecated
199  * Add a device to the user device list
200  * See rte_devargs_parse() for details.
201  *
202  * @param devtype
203  *   The type of the device.
204  * @param devargs_str
205  *   The arguments as given by the user.
206  *
207  * @return
208  *   - 0 on success
209  *   - A negative value on error
210  */
211 __rte_deprecated
212 int rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str);
213
214 /**
215  * Remove a device from the user device list.
216  * Its resources are freed.
217  * If the devargs cannot be found, nothing happens.
218  *
219  * @param busname
220  *   bus name of the devargs to remove.
221  *
222  * @param devname
223  *   device name of the devargs to remove.
224  *
225  * @return
226  *   0 on success.
227  *   <0 on error.
228  *   >0 if the devargs was not within the user device list.
229  */
230 __rte_experimental
231 int rte_devargs_remove(const char *busname,
232                        const char *devname);
233
234 /**
235  * Count the number of user devices of a specified type
236  *
237  * @param devtype
238  *   The type of the devices to counted.
239  *
240  * @return
241  *   The number of devices.
242  */
243 __rte_experimental
244 unsigned int
245 rte_devargs_type_count(enum rte_devtype devtype);
246
247 /**
248  * @deprecated
249  * Count the number of user devices of a specified type
250  *
251  * @param devtype
252  *   The type of the devices to counted.
253  *
254  * @return
255  *   The number of devices.
256  */
257 __rte_deprecated
258 unsigned int
259 rte_eal_devargs_type_count(enum rte_devtype devtype);
260
261 /**
262  * This function dumps the list of user device and their arguments.
263  *
264  * @param f
265  *   A pointer to a file for output
266  */
267 __rte_experimental
268 void rte_devargs_dump(FILE *f);
269
270 /**
271  * @deprecated
272  * This function dumps the list of user device and their arguments.
273  *
274  * @param f
275  *   A pointer to a file for output
276  */
277 __rte_deprecated
278 void rte_eal_devargs_dump(FILE *f);
279
280 /**
281  * Find next rte_devargs matching the provided bus name.
282  *
283  * @param busname
284  *   Limit the iteration to devargs related to buses
285  *   matching this name.
286  *   Will return any next rte_devargs if NULL.
287  *
288  * @param start
289  *   Starting iteration point. The iteration will start at
290  *   the first rte_devargs if NULL.
291  *
292  * @return
293  *   Next rte_devargs entry matching the requested bus,
294  *   NULL if there is none.
295  */
296 __rte_experimental
297 struct rte_devargs *
298 rte_devargs_next(const char *busname, const struct rte_devargs *start);
299
300 /**
301  * Iterate over all rte_devargs for a specific bus.
302  */
303 #define RTE_EAL_DEVARGS_FOREACH(busname, da) \
304         for (da = rte_devargs_next(busname, NULL); \
305              da != NULL; \
306              da = rte_devargs_next(busname, da)) \
307
308 #ifdef __cplusplus
309 }
310 #endif
311
312 #endif /* _RTE_DEVARGS_H_ */