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);
46 rte_kvargs_free(kvlist);
49 rte_kvargs_free(kvlist);
64 test_valid_devargs_cases(const struct devargs_case *list, size_t n)
66 struct rte_devargs da;
69 int fail = TEST_SUCCESS;
70 struct rte_bus *pci_bus = rte_bus_find_by_name("pci");
71 struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
72 struct rte_class *eth_class = rte_class_find_by_name("eth");
74 for (i = 0; i < n; i++) {
75 if (pci_bus == NULL && list[i].bus != NULL &&
76 strcmp(list[i].bus, "pci") == 0)
78 if (vdev_bus == NULL && list[i].bus != NULL &&
79 strcmp(list[i].bus, "vdev") == 0)
81 if (eth_class == NULL && list[i].class != NULL &&
82 strcmp(list[i].class, "eth") == 0)
84 memset(&da, 0, sizeof(da));
85 ret = rte_devargs_parse(&da, list[i].devargs);
87 printf("rte_devargs_parse(%s) returned %d (but should not)\n",
88 list[i].devargs, ret);
91 if ((list[i].bus_kv > 0 || list[i].bus != NULL) &&
93 printf("rte_devargs_parse(%s) bus not parsed\n",
97 if (test_args(list[i].devargs, "bus", da.bus_str,
100 if (list[i].bus != NULL &&
101 strcmp(da.bus->name, list[i].bus) != 0) {
102 printf("rte_devargs_parse(%s) bus name (%s) not expected (%s)\n",
103 list[i].devargs, da.bus->name, list[i].bus);
106 if ((list[i].class_kv > 0 || list[i].class != NULL) &&
108 printf("rte_devargs_parse(%s) class not parsed\n",
112 if (test_args(list[i].devargs, "class", da.cls_str,
113 list[i].class_kv) != 0)
115 if (list[i].class != NULL &&
116 strcmp(da.cls->name, list[i].class) != 0) {
117 printf("rte_devargs_parse(%s) class name (%s) not expected (%s)\n",
118 list[i].devargs, da.cls->name, list[i].class);
121 if (test_args(list[i].devargs, "driver", da.drv_str,
122 list[i].driver_kv) != 0)
124 if (list[i].name != NULL &&
125 strcmp(da.name, list[i].name) != 0) {
126 printf("rte_devargs_parse(%s) device name (%s) not expected (%s)\n",
127 list[i].devargs, da.name, list[i].name);
134 rte_devargs_reset(&da);
139 /* Test several valid cases */
141 test_valid_devargs(void)
143 static const struct devargs_case list[] = {
144 /* Global devargs syntax: */
146 1, 0, 0, "pci", NULL, NULL},
148 0, 1, 0, NULL, NULL, "eth" },
149 { "bus=pci,addr=1:2.3/class=eth/driver=abc,k0=v0",
150 2, 1, 2, "pci", "0000:01:02.3", "eth" },
151 { "bus=vdev,name=/dev/file/name/class=eth",
152 2, 1, 0, "vdev", "/dev/file/name", "eth" },
153 { "bus=vdev,name=/class/bus/path/class=eth",
154 2, 1, 0, "vdev", "/class/bus/path", "eth" },
155 { "bus=vdev,name=///dblslsh/class=eth",
156 2, 1, 0, "vdev", "///dblslsh", "eth" },
157 /* Legacy devargs syntax: */
159 "pci", "1:2.3", NULL },
161 0, 0, 1, "pci", "1:2.3", NULL },
163 static const struct devargs_case legacy_ring_list[] = {
165 0, 0, 0, "vdev", "net_ring0", NULL },
166 { "net_ring0,iface=test,path=/class/bus/,queues=1",
167 0, 0, 3, "vdev", "net_ring0", NULL },
169 struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
172 ret = test_valid_devargs_cases(list, RTE_DIM(list));
173 if (vdev_bus != NULL && vdev_bus->parse("net_ring0", NULL) == 0)
174 /* Ring vdev driver enabled. */
175 ret |= test_valid_devargs_cases(legacy_ring_list,
176 RTE_DIM(legacy_ring_list));
180 /* Test several invalid cases */
182 test_invalid_devargs(void)
184 static const char * const list[] = {
186 "class=wrong-class"};
187 struct rte_devargs da;
192 for (i = 0; i < RTE_DIM(list); i++) {
193 ret = rte_devargs_parse(&da, list[i]);
195 printf("rte_devargs_parse(%s) returned %d (but should not)\n",
199 rte_devargs_reset(&da);
207 printf("== test valid case ==\n");
208 if (test_valid_devargs() < 0)
210 printf("== test invalid case ==\n");
211 if (test_invalid_devargs() < 0)
216 REGISTER_TEST_COMMAND(devargs_autotest, test_devargs);