1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
9 #include <rte_common.h>
10 #include <rte_devargs.h>
11 #include <rte_kvargs.h>
13 #include <rte_class.h>
17 /* Check layer arguments. */
19 test_args(const char *devargs, const char *layer, const char *args, const int n)
21 struct rte_kvargs *kvlist;
24 if (args != NULL && strlen(args) > 0) {
25 printf("rte_devargs_parse(%s) %s args parsed (not expected)\n",
33 printf("rte_devargs_parse(%s) %s args not parsed\n",
37 kvlist = rte_kvargs_parse(args, NULL);
39 printf("rte_devargs_parse(%s) %s_str: %s not parsed\n",
40 devargs, layer, args);
43 if ((int)kvlist->count != n) {
44 printf("rte_devargs_parse(%s) %s_str: %s kv number %u, not %d\n",
45 devargs, layer, args, kvlist->count, n);
62 test_valid_devargs_cases(const struct devargs_case *list, size_t n)
64 struct rte_devargs da;
67 int fail = TEST_SUCCESS;
68 struct rte_bus *pci_bus = rte_bus_find_by_name("pci");
69 struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
70 struct rte_class *eth_class = rte_class_find_by_name("eth");
72 for (i = 0; i < n; i++) {
73 if (pci_bus == NULL && list[i].bus != NULL &&
74 strcmp(list[i].bus, "pci") == 0)
76 if (vdev_bus == NULL && list[i].bus != NULL &&
77 strcmp(list[i].bus, "vdev") == 0)
79 if (eth_class == NULL && list[i].class != NULL &&
80 strcmp(list[i].class, "eth") == 0)
82 memset(&da, 0, sizeof(da));
83 ret = rte_devargs_parse(&da, list[i].devargs);
85 printf("rte_devargs_parse(%s) returned %d (but should not)\n",
86 list[i].devargs, ret);
89 if ((list[i].bus_kv > 0 || list[i].bus != NULL) &&
91 printf("rte_devargs_parse(%s) bus not parsed\n",
95 if (test_args(list[i].devargs, "bus", da.bus_str,
98 if (list[i].bus != NULL &&
99 strcmp(da.bus->name, list[i].bus) != 0) {
100 printf("rte_devargs_parse(%s) bus name (%s) not expected (%s)\n",
101 list[i].devargs, da.bus->name, list[i].bus);
104 if ((list[i].class_kv > 0 || list[i].class != NULL) &&
106 printf("rte_devargs_parse(%s) class not parsed\n",
110 if (test_args(list[i].devargs, "class", da.cls_str,
111 list[i].class_kv) != 0)
113 if (list[i].class != NULL &&
114 strcmp(da.cls->name, list[i].class) != 0) {
115 printf("rte_devargs_parse(%s) class name (%s) not expected (%s)\n",
116 list[i].devargs, da.cls->name, list[i].class);
119 if (test_args(list[i].devargs, "driver", da.drv_str,
120 list[i].driver_kv) != 0)
122 if (list[i].name != NULL &&
123 strcmp(da.name, list[i].name) != 0) {
124 printf("rte_devargs_parse(%s) device name (%s) not expected (%s)\n",
125 list[i].devargs, da.name, list[i].name);
132 rte_devargs_reset(&da);
137 /* Test several valid cases */
139 test_valid_devargs(void)
141 static const struct devargs_case list[] = {
142 /* Global devargs syntax: */
144 1, 0, 0, "pci", NULL, NULL},
146 0, 1, 0, NULL, NULL, "eth" },
147 { "bus=pci,addr=1:2.3/class=eth/driver=abc,k0=v0",
148 2, 1, 2, "pci", "0000:01:02.3", "eth" },
149 { "bus=vdev,name=/dev/file/name/class=eth",
150 2, 1, 0, "vdev", "/dev/file/name", "eth" },
151 { "bus=vdev,name=/class/bus/path/class=eth",
152 2, 1, 0, "vdev", "/class/bus/path", "eth" },
153 { "bus=vdev,name=///dblslsh/class=eth",
154 2, 1, 0, "vdev", "///dblslsh", "eth" },
155 /* Legacy devargs syntax: */
157 "pci", "1:2.3", NULL },
159 0, 0, 1, "pci", "1:2.3", NULL },
161 static const struct devargs_case legacy_ring_list[] = {
163 0, 0, 0, "vdev", "net_ring0", NULL },
164 { "net_ring0,iface=test,path=/class/bus/,queues=1",
165 0, 0, 3, "vdev", "net_ring0", NULL },
167 struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
170 ret = test_valid_devargs_cases(list, RTE_DIM(list));
171 if (vdev_bus != NULL && vdev_bus->parse("net_ring0", NULL) == 0)
172 /* Ring vdev driver enabled. */
173 ret |= test_valid_devargs_cases(legacy_ring_list,
174 RTE_DIM(legacy_ring_list));
178 /* Test several invalid cases */
180 test_invalid_devargs(void)
182 static const char * const list[] = {
184 "class=wrong-class"};
185 struct rte_devargs da;
190 for (i = 0; i < RTE_DIM(list); i++) {
191 ret = rte_devargs_parse(&da, list[i]);
193 printf("rte_devargs_parse(%s) returned %d (but should not)\n",
197 rte_devargs_reset(&da);
205 printf("== test valid case ==\n");
206 if (test_valid_devargs() < 0)
208 printf("== test invalid case ==\n");
209 if (test_invalid_devargs() < 0)
214 REGISTER_TEST_COMMAND(devargs_autotest, test_devargs);