xref: /dpdk/lib/eal/include/rte_devargs.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright 2014 6WIND S.A.
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _RTE_DEVARGS_H_
699a2dd95SBruce Richardson #define _RTE_DEVARGS_H_
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson /**
999a2dd95SBruce Richardson  * @file
1099a2dd95SBruce Richardson  *
1199a2dd95SBruce Richardson  * RTE devargs: list of devices and their user arguments
1299a2dd95SBruce Richardson  *
1399a2dd95SBruce Richardson  * This file stores a list of devices and their arguments given by
1499a2dd95SBruce Richardson  * the user when a DPDK application is started. These devices can be PCI
1599a2dd95SBruce Richardson  * devices or virtual devices. These devices are stored at startup in a
1699a2dd95SBruce Richardson  * list of rte_devargs structures.
1799a2dd95SBruce Richardson  */
1899a2dd95SBruce Richardson 
1999a2dd95SBruce Richardson #include <stdio.h>
203348f60aSStephen Hemminger 
2182ee04a8SDavid Marchand #include <rte_dev.h>
2282ee04a8SDavid Marchand 
23*719834a6SMattias Rönnblom #ifdef __cplusplus
24*719834a6SMattias Rönnblom extern "C" {
25*719834a6SMattias Rönnblom #endif
26*719834a6SMattias Rönnblom 
2782ee04a8SDavid Marchand struct rte_bus;
2899a2dd95SBruce Richardson 
2999a2dd95SBruce Richardson /**
3035d4f17bSXueming Li  * Bus type key in global devargs syntax.
3135d4f17bSXueming Li  *
3235d4f17bSXueming Li  * Legacy devargs parser doesn't use this key as bus type
3335d4f17bSXueming Li  * is resolved as first optional value separated by ":".
3435d4f17bSXueming Li  */
3535d4f17bSXueming Li #define RTE_DEVARGS_KEY_BUS "bus"
3635d4f17bSXueming Li 
3735d4f17bSXueming Li /**
3835d4f17bSXueming Li  * Class type key in global devargs syntax.
3935d4f17bSXueming Li  *
40f8dbaebbSSean Morrissey  * Legacy devargs parser doesn't parse class type. PMD is
4135d4f17bSXueming Li  * encouraged to use this key to resolve class type.
4235d4f17bSXueming Li  */
4335d4f17bSXueming Li #define RTE_DEVARGS_KEY_CLASS "class"
4435d4f17bSXueming Li 
4535d4f17bSXueming Li /**
4635d4f17bSXueming Li  * Driver type key in global devargs syntax.
4735d4f17bSXueming Li  *
48f8dbaebbSSean Morrissey  * Legacy devargs parser doesn't parse driver type. PMD is
4935d4f17bSXueming Li  * encouraged to use this key to resolve driver type.
5035d4f17bSXueming Li  */
5135d4f17bSXueming Li #define RTE_DEVARGS_KEY_DRIVER "driver"
5235d4f17bSXueming Li 
5335d4f17bSXueming Li /**
5499a2dd95SBruce Richardson  * Type of generic device
5599a2dd95SBruce Richardson  */
5699a2dd95SBruce Richardson enum rte_devtype {
5799a2dd95SBruce Richardson 	RTE_DEVTYPE_ALLOWED,
5899a2dd95SBruce Richardson 	RTE_DEVTYPE_BLOCKED,
5999a2dd95SBruce Richardson 	RTE_DEVTYPE_VIRTUAL,
6099a2dd95SBruce Richardson };
6199a2dd95SBruce Richardson 
6299a2dd95SBruce Richardson /**
6399a2dd95SBruce Richardson  * Structure that stores a device given by the user with its arguments
6499a2dd95SBruce Richardson  *
6599a2dd95SBruce Richardson  * A user device is a physical or a virtual device given by the user to
6699a2dd95SBruce Richardson  * the DPDK application at startup through command line arguments.
6799a2dd95SBruce Richardson  *
6899a2dd95SBruce Richardson  * The structure stores the configuration of the device, its PCI
6999a2dd95SBruce Richardson  * identifier if it's a PCI device or the driver name if it's a virtual
7099a2dd95SBruce Richardson  * device.
7199a2dd95SBruce Richardson  */
7299a2dd95SBruce Richardson struct rte_devargs {
7399a2dd95SBruce Richardson 	/** Next in list. */
74f1f6ebc0SWilliam Tu 	RTE_TAILQ_ENTRY(rte_devargs) next;
7599a2dd95SBruce Richardson 	/** Type of device. */
7699a2dd95SBruce Richardson 	enum rte_devtype type;
7799a2dd95SBruce Richardson 	/** Device policy. */
7899a2dd95SBruce Richardson 	enum rte_dev_policy policy;
7999a2dd95SBruce Richardson 	/** Name of the device. */
8099a2dd95SBruce Richardson 	char name[RTE_DEV_NAME_MAX_LEN];
8199a2dd95SBruce Richardson 	union {
8299a2dd95SBruce Richardson 		const char *args; /**< legacy name. */
8399a2dd95SBruce Richardson 		const char *drv_str; /**< driver-related part of device string. */
8499a2dd95SBruce Richardson 	};
8599a2dd95SBruce Richardson 	struct rte_bus *bus; /**< bus handle. */
8699a2dd95SBruce Richardson 	struct rte_class *cls; /**< class handle. */
8799a2dd95SBruce Richardson 	const char *bus_str; /**< bus-related part of device string. */
8899a2dd95SBruce Richardson 	const char *cls_str; /**< class-related part of device string. */
8999a2dd95SBruce Richardson 	char *data; /**< raw string including bus, class and driver parts. */
9099a2dd95SBruce Richardson };
9199a2dd95SBruce Richardson 
9299a2dd95SBruce Richardson /**
9399a2dd95SBruce Richardson  * Parse a device string.
9499a2dd95SBruce Richardson  *
9599a2dd95SBruce Richardson  * Verify that a bus is capable of handling the device passed
9699a2dd95SBruce Richardson  * in argument. Store which bus will handle the device, its name
9799a2dd95SBruce Richardson  * and the eventual device parameters.
9899a2dd95SBruce Richardson  *
9999a2dd95SBruce Richardson  * The syntax is:
10099a2dd95SBruce Richardson  *
10199a2dd95SBruce Richardson  *     bus:device_identifier,arg1=val1,arg2=val2
10299a2dd95SBruce Richardson  *
10399a2dd95SBruce Richardson  * where "bus:" is the bus name followed by any character separator.
10499a2dd95SBruce Richardson  * The bus name is optional. If no bus name is specified, each bus
10599a2dd95SBruce Richardson  * will attempt to recognize the device identifier. The first one
10699a2dd95SBruce Richardson  * to succeed will be used.
10799a2dd95SBruce Richardson  *
10899a2dd95SBruce Richardson  * Examples:
10999a2dd95SBruce Richardson  *
11099a2dd95SBruce Richardson  *     pci:0000:05.00.0,arg=val
11199a2dd95SBruce Richardson  *     05.00.0,arg=val
11299a2dd95SBruce Richardson  *     vdev:net_ring0
11399a2dd95SBruce Richardson  *
11499a2dd95SBruce Richardson  * @param da
11599a2dd95SBruce Richardson  *   The devargs structure holding the device information.
11699a2dd95SBruce Richardson  *
11799a2dd95SBruce Richardson  * @param dev
11899a2dd95SBruce Richardson  *   String describing a device.
11999a2dd95SBruce Richardson  *
12099a2dd95SBruce Richardson  * @return
12199a2dd95SBruce Richardson  *   - 0 on success.
12299a2dd95SBruce Richardson  *   - Negative errno on error.
12399a2dd95SBruce Richardson  */
12499a2dd95SBruce Richardson int
12599a2dd95SBruce Richardson rte_devargs_parse(struct rte_devargs *da, const char *dev);
12699a2dd95SBruce Richardson 
12799a2dd95SBruce Richardson /**
12899a2dd95SBruce Richardson  * Parse a device string.
12999a2dd95SBruce Richardson  *
13099a2dd95SBruce Richardson  * Verify that a bus is capable of handling the device passed
13199a2dd95SBruce Richardson  * in argument. Store which bus will handle the device, its name
13299a2dd95SBruce Richardson  * and the eventual device parameters.
13399a2dd95SBruce Richardson  *
13499a2dd95SBruce Richardson  * The device string is built with a printf-like syntax.
13599a2dd95SBruce Richardson  *
13699a2dd95SBruce Richardson  * The syntax is:
13799a2dd95SBruce Richardson  *
13899a2dd95SBruce Richardson  *     bus:device_identifier,arg1=val1,arg2=val2
13999a2dd95SBruce Richardson  *
14099a2dd95SBruce Richardson  * where "bus:" is the bus name followed by any character separator.
14199a2dd95SBruce Richardson  * The bus name is optional. If no bus name is specified, each bus
14299a2dd95SBruce Richardson  * will attempt to recognize the device identifier. The first one
14399a2dd95SBruce Richardson  * to succeed will be used.
14499a2dd95SBruce Richardson  *
14599a2dd95SBruce Richardson  * Examples:
14699a2dd95SBruce Richardson  *
14799a2dd95SBruce Richardson  *     pci:0000:05.00.0,arg=val
14899a2dd95SBruce Richardson  *     05.00.0,arg=val
14999a2dd95SBruce Richardson  *     vdev:net_ring0
15099a2dd95SBruce Richardson  *
15199a2dd95SBruce Richardson  * @param da
15299a2dd95SBruce Richardson  *   The devargs structure holding the device information.
15399a2dd95SBruce Richardson  * @param format
15499a2dd95SBruce Richardson  *   Format string describing a device.
15599a2dd95SBruce Richardson  *
15699a2dd95SBruce Richardson  * @return
15799a2dd95SBruce Richardson  *   - 0 on success.
15899a2dd95SBruce Richardson  *   - Negative errno on error.
15999a2dd95SBruce Richardson  */
16099a2dd95SBruce Richardson int
16199a2dd95SBruce Richardson rte_devargs_parsef(struct rte_devargs *da,
16299a2dd95SBruce Richardson 		   const char *format, ...)
16399a2dd95SBruce Richardson __rte_format_printf(2, 0);
16499a2dd95SBruce Richardson 
16599a2dd95SBruce Richardson /**
16699a2dd95SBruce Richardson  * Free resources in devargs.
16799a2dd95SBruce Richardson  *
16899a2dd95SBruce Richardson  * @param da
16999a2dd95SBruce Richardson  *   The devargs structure holding the device information.
17099a2dd95SBruce Richardson  */
17199a2dd95SBruce Richardson void
17299a2dd95SBruce Richardson rte_devargs_reset(struct rte_devargs *da);
17399a2dd95SBruce Richardson 
17499a2dd95SBruce Richardson /**
17599a2dd95SBruce Richardson  * Insert an rte_devargs in the global list.
17699a2dd95SBruce Richardson  *
17799a2dd95SBruce Richardson  * @param da
17899a2dd95SBruce Richardson  *  The devargs structure to insert.
17999a2dd95SBruce Richardson  *  If a devargs for the same device is already inserted,
18099a2dd95SBruce Richardson  *  it will be updated and returned. It means *da pointer can change.
18199a2dd95SBruce Richardson  *
18299a2dd95SBruce Richardson  * @return
18399a2dd95SBruce Richardson  *   - 0 on success
18499a2dd95SBruce Richardson  *   - Negative on error.
18599a2dd95SBruce Richardson  */
18699a2dd95SBruce Richardson int
18799a2dd95SBruce Richardson rte_devargs_insert(struct rte_devargs **da);
18899a2dd95SBruce Richardson 
18999a2dd95SBruce Richardson /**
19099a2dd95SBruce Richardson  * Add a device to the user device list
19199a2dd95SBruce Richardson  * See rte_devargs_parse() for details.
19299a2dd95SBruce Richardson  *
19399a2dd95SBruce Richardson  * @param devtype
19499a2dd95SBruce Richardson  *   The type of the device.
19599a2dd95SBruce Richardson  * @param devargs_str
19699a2dd95SBruce Richardson  *   The arguments as given by the user.
19799a2dd95SBruce Richardson  *
19899a2dd95SBruce Richardson  * @return
19999a2dd95SBruce Richardson  *   - 0 on success
20099a2dd95SBruce Richardson  *   - A negative value on error
20199a2dd95SBruce Richardson  */
20299a2dd95SBruce Richardson int rte_devargs_add(enum rte_devtype devtype, const char *devargs_str);
20399a2dd95SBruce Richardson 
20499a2dd95SBruce Richardson /**
20599a2dd95SBruce Richardson  * Remove a device from the user device list.
20699a2dd95SBruce Richardson  * Its resources are freed.
20799a2dd95SBruce Richardson  * If the devargs cannot be found, nothing happens.
20899a2dd95SBruce Richardson  *
20999a2dd95SBruce Richardson  * @param devargs
21099a2dd95SBruce Richardson  *   The instance or a copy of devargs to remove.
21199a2dd95SBruce Richardson  *
21299a2dd95SBruce Richardson  * @return
21399a2dd95SBruce Richardson  *   0 on success.
21499a2dd95SBruce Richardson  *   <0 on error.
21599a2dd95SBruce Richardson  *   >0 if the devargs was not within the user device list.
21699a2dd95SBruce Richardson  */
21799a2dd95SBruce Richardson int rte_devargs_remove(struct rte_devargs *devargs);
21899a2dd95SBruce Richardson 
21999a2dd95SBruce Richardson /**
22099a2dd95SBruce Richardson  * Count the number of user devices of a specified type
22199a2dd95SBruce Richardson  *
22299a2dd95SBruce Richardson  * @param devtype
22399a2dd95SBruce Richardson  *   The type of the devices to counted.
22499a2dd95SBruce Richardson  *
22599a2dd95SBruce Richardson  * @return
22699a2dd95SBruce Richardson  *   The number of devices.
22799a2dd95SBruce Richardson  */
22899a2dd95SBruce Richardson unsigned int
22999a2dd95SBruce Richardson rte_devargs_type_count(enum rte_devtype devtype);
23099a2dd95SBruce Richardson 
23199a2dd95SBruce Richardson /**
23299a2dd95SBruce Richardson  * This function dumps the list of user device and their arguments.
23399a2dd95SBruce Richardson  *
23499a2dd95SBruce Richardson  * @param f
23599a2dd95SBruce Richardson  *   A pointer to a file for output
23699a2dd95SBruce Richardson  */
23799a2dd95SBruce Richardson void rte_devargs_dump(FILE *f);
23899a2dd95SBruce Richardson 
23999a2dd95SBruce Richardson /**
24099a2dd95SBruce Richardson  * Find next rte_devargs matching the provided bus name.
24199a2dd95SBruce Richardson  *
24299a2dd95SBruce Richardson  * @param busname
24399a2dd95SBruce Richardson  *   Limit the iteration to devargs related to buses
24499a2dd95SBruce Richardson  *   matching this name.
24599a2dd95SBruce Richardson  *   Will return any next rte_devargs if NULL.
24699a2dd95SBruce Richardson  *
24799a2dd95SBruce Richardson  * @param start
24899a2dd95SBruce Richardson  *   Starting iteration point. The iteration will start at
24999a2dd95SBruce Richardson  *   the first rte_devargs if NULL.
25099a2dd95SBruce Richardson  *
25199a2dd95SBruce Richardson  * @return
25299a2dd95SBruce Richardson  *   Next rte_devargs entry matching the requested bus,
25399a2dd95SBruce Richardson  *   NULL if there is none.
25499a2dd95SBruce Richardson  */
25599a2dd95SBruce Richardson struct rte_devargs *
25699a2dd95SBruce Richardson rte_devargs_next(const char *busname, const struct rte_devargs *start);
25799a2dd95SBruce Richardson 
25899a2dd95SBruce Richardson /**
25999a2dd95SBruce Richardson  * Iterate over all rte_devargs for a specific bus.
26099a2dd95SBruce Richardson  */
26199a2dd95SBruce Richardson #define RTE_EAL_DEVARGS_FOREACH(busname, da) \
26299a2dd95SBruce Richardson 	for (da = rte_devargs_next(busname, NULL); \
26399a2dd95SBruce Richardson 	     da != NULL; \
26499a2dd95SBruce Richardson 	     da = rte_devargs_next(busname, da)) \
26599a2dd95SBruce Richardson 
26699a2dd95SBruce Richardson #ifdef __cplusplus
26799a2dd95SBruce Richardson }
26899a2dd95SBruce Richardson #endif
26999a2dd95SBruce Richardson 
27099a2dd95SBruce Richardson #endif /* _RTE_DEVARGS_H_ */
271