1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
3 */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 #include <ethdev_driver.h>
10 #include <rte_common.h>
11 #include <rte_devargs.h>
12 #include <rte_kvargs.h>
13 #include <bus_driver.h>
14 #include <rte_class.h>
15
16 #include "test.h"
17
18 /* Check layer arguments. */
19 static int
test_args(const char * devargs,const char * layer,const char * args,const int n)20 test_args(const char *devargs, const char *layer, const char *args, const int n)
21 {
22 struct rte_kvargs *kvlist;
23
24 if (n == 0) {
25 if (args != NULL && strlen(args) > 0) {
26 printf("rte_devargs_parse(%s) %s args parsed (not expected)\n",
27 devargs, layer);
28 return -1;
29 } else {
30 return 0;
31 }
32 }
33 if (args == NULL) {
34 printf("rte_devargs_parse(%s) %s args not parsed\n",
35 devargs, layer);
36 return -1;
37 }
38 kvlist = rte_kvargs_parse(args, NULL);
39 if (kvlist == NULL) {
40 printf("rte_devargs_parse(%s) %s_str: %s not parsed\n",
41 devargs, layer, args);
42 return -1;
43 }
44 if ((int)kvlist->count != n) {
45 printf("rte_devargs_parse(%s) %s_str: %s kv number %u, not %d\n",
46 devargs, layer, args, kvlist->count, n);
47 rte_kvargs_free(kvlist);
48 return -1;
49 }
50 rte_kvargs_free(kvlist);
51 return 0;
52 }
53
54 struct devargs_case {
55 const char *devargs;
56 int bus_kv;
57 int class_kv;
58 int driver_kv;
59 const char *bus;
60 const char *name;
61 const char *class;
62 };
63
64 static int
test_valid_devargs_cases(const struct devargs_case * list,size_t n)65 test_valid_devargs_cases(const struct devargs_case *list, size_t n)
66 {
67 struct rte_devargs da;
68 uint32_t i;
69 int ret;
70 int fail = TEST_SUCCESS;
71 struct rte_bus *pci_bus = rte_bus_find_by_name("pci");
72 struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
73 struct rte_class *eth_class = rte_class_find_by_name("eth");
74
75 for (i = 0; i < n; i++) {
76 if (pci_bus == NULL && list[i].bus != NULL &&
77 strcmp(list[i].bus, "pci") == 0)
78 continue;
79 if (vdev_bus == NULL && list[i].bus != NULL &&
80 strcmp(list[i].bus, "vdev") == 0)
81 continue;
82 if (eth_class == NULL && list[i].class != NULL &&
83 strcmp(list[i].class, "eth") == 0)
84 continue;
85 memset(&da, 0, sizeof(da));
86 ret = rte_devargs_parse(&da, list[i].devargs);
87 if (ret < 0) {
88 printf("rte_devargs_parse(%s) returned %d (but should not)\n",
89 list[i].devargs, ret);
90 goto fail;
91 }
92 if ((list[i].bus_kv > 0 || list[i].bus != NULL) &&
93 da.bus == NULL) {
94 printf("rte_devargs_parse(%s) bus not parsed\n",
95 list[i].devargs);
96 goto fail;
97 }
98 if (test_args(list[i].devargs, "bus", da.bus_str,
99 list[i].bus_kv) != 0)
100 goto fail;
101 if (list[i].bus != NULL &&
102 strcmp(rte_bus_name(da.bus), list[i].bus) != 0) {
103 printf("rte_devargs_parse(%s) bus name (%s) not expected (%s)\n",
104 list[i].devargs, rte_bus_name(da.bus), list[i].bus);
105 goto fail;
106 }
107 if ((list[i].class_kv > 0 || list[i].class != NULL) &&
108 da.cls == NULL) {
109 printf("rte_devargs_parse(%s) class not parsed\n",
110 list[i].devargs);
111 goto fail;
112 }
113 if (test_args(list[i].devargs, "class", da.cls_str,
114 list[i].class_kv) != 0)
115 goto fail;
116 if (list[i].class != NULL &&
117 strcmp(da.cls->name, list[i].class) != 0) {
118 printf("rte_devargs_parse(%s) class name (%s) not expected (%s)\n",
119 list[i].devargs, da.cls->name, list[i].class);
120 goto fail;
121 }
122 if (test_args(list[i].devargs, "driver", da.drv_str,
123 list[i].driver_kv) != 0)
124 goto fail;
125 if (list[i].name != NULL &&
126 strcmp(da.name, list[i].name) != 0) {
127 printf("rte_devargs_parse(%s) device name (%s) not expected (%s)\n",
128 list[i].devargs, da.name, list[i].name);
129 goto fail;
130 }
131 goto cleanup;
132 fail:
133 fail = TEST_FAILED;
134 cleanup:
135 rte_devargs_reset(&da);
136 }
137 return fail;
138 }
139
140 /* Test several valid cases */
141 static int
test_valid_devargs(void)142 test_valid_devargs(void)
143 {
144 static const struct devargs_case list[] = {
145 /* Global devargs syntax: */
146 { "bus=pci",
147 1, 0, 0, "pci", NULL, NULL},
148 { "class=eth",
149 0, 1, 0, NULL, NULL, "eth" },
150 { "bus=pci,addr=1:2.3/class=eth/driver=abc,k0=v0",
151 2, 1, 2, "pci", "0000:01:02.3", "eth" },
152 { "bus=vdev,name=/dev/file/name/class=eth",
153 2, 1, 0, "vdev", "/dev/file/name", "eth" },
154 { "bus=vdev,name=/class/bus/path/class=eth",
155 2, 1, 0, "vdev", "/class/bus/path", "eth" },
156 { "bus=vdev,name=///dblslsh/class=eth",
157 2, 1, 0, "vdev", "///dblslsh", "eth" },
158 /* Legacy devargs syntax: */
159 { "1:2.3", 0, 0, 0,
160 "pci", "1:2.3", NULL },
161 { "pci:1:2.3,k0=v0",
162 0, 0, 1, "pci", "1:2.3", NULL },
163 };
164 static const struct devargs_case legacy_ring_list[] = {
165 { "net_ring0",
166 0, 0, 0, "vdev", "net_ring0", NULL },
167 { "net_ring0,iface=test,path=/class/bus/,queues=1",
168 0, 0, 3, "vdev", "net_ring0", NULL },
169 };
170 struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
171 int ret;
172
173 ret = test_valid_devargs_cases(list, RTE_DIM(list));
174 if (vdev_bus != NULL && vdev_bus->parse("net_ring0", NULL) == 0)
175 /* Ring vdev driver enabled. */
176 ret |= test_valid_devargs_cases(legacy_ring_list,
177 RTE_DIM(legacy_ring_list));
178 return ret;
179 }
180
181 /* Test several invalid cases */
182 static int
test_invalid_devargs(void)183 test_invalid_devargs(void)
184 {
185 static const char * const list[] = {
186 "bus=wrong-bus",
187 "class=wrong-class"};
188 struct rte_devargs da;
189 uint32_t i;
190 int ret;
191 int fail = 0;
192
193 for (i = 0; i < RTE_DIM(list); i++) {
194 ret = rte_devargs_parse(&da, list[i]);
195 if (ret >= 0) {
196 printf("rte_devargs_parse(%s) returned %d (but should not)\n",
197 list[i], ret);
198 fail = ret;
199 }
200 rte_devargs_reset(&da);
201 }
202 return fail;
203 }
204
205 struct devargs_parse_case {
206 const char *devargs;
207 uint8_t devargs_count;
208 };
209
210 static int
test_valid_devargs_parsing(void)211 test_valid_devargs_parsing(void)
212 {
213 static const struct devargs_parse_case list[] = {
214 /* Single representors patterns */
215 {"representor=1", 1},
216 {"representor=[1,2,3]", 1},
217 {"representor=[0-3,5,7]", 1},
218 {"representor=vf[0-1]", 1},
219 {"representor=sf[0-1]", 1},
220 {"representor=pf1vf[0-1]", 1},
221 {"representor=pf[0-1]vf[1-2]", 1},
222 {"representor=pf[0-1]sf[1-2]", 1},
223 {"representor=c0pf[0-1]", 1},
224 {"representor=sf[1,2]vf[2]", 1}, /* Only SF ports will be represented */
225 {"representor=vf2sf[1-2]", 1}, /* Only VF ports will be represented */
226 {"representor=c[0-1]pf[0-1]vf[1-2]", 1},
227 /* Single devarg inside multiple representor pattern */
228 {"representor=[c[0-1]pf[0-1]vf[1-2]]", 1},
229 /* Multiple representors patterns */
230 {"representor=[vf0,3]", 2},
231 {"representor=[vf0,vf1]", 2},
232 {"representor=[vf[0],vf[1]]", 2},
233 {"representor=[vf[0,1],3]", 2},
234 {"representor=[vf[0,1],[3]]", 2},
235 {"representor=[pf1vf[0-1],3]", 2},
236 {"representor=[pf1vf[0-1],pf[0-1]]", 2},
237 {"representor=[pf1,pf3,pf1vf[0-1],vf[0],vf[1],vf0,vf1,vf2]", 8},
238 {"representor=[1,3,5,pf1,pf2,pf3,pf1vf[0-1],vf[0],vf[1],vf0,vf1,vf2]", 12},
239 {"representor=[[1,3,5],pf1,pf2,pf3,pf1vf[0-1],vf[0],vf[1],vf0,vf1,vf2]", 10},
240 {"representor=[c[0,2-4]pf[1,6]vf[0-1],vf[0],vf4,[3-5,7],pf1vf[0-1,6],pf[2,4,6]]", 6}
241 };
242 struct rte_eth_devargs eth_da[RTE_MAX_ETHPORTS];
243 uint32_t i;
244 int ret;
245 int fail = 0;
246
247 for (i = 0; i < RTE_DIM(list); i++) {
248 memset(eth_da, 0, RTE_MAX_ETHPORTS * sizeof(*eth_da));
249 ret = rte_eth_devargs_parse(list[i].devargs, eth_da, RTE_MAX_ETHPORTS);
250 if (ret <= 0) {
251 printf("rte_devargs_parse(%s) returned %d (but should not)\n",
252 list[i].devargs, ret);
253 fail = ret;
254 break;
255 }
256
257 /* Check the return value, count should be equivalent to no of devargs. */
258 if (ret != list[i].devargs_count) {
259 printf("Devargs returned count %d != expected count %d\n", ret,
260 list[i].devargs_count);
261 fail = -1;
262 break;
263 }
264 }
265 return fail;
266 }
267
268 static int
test_invalid_devargs_parsing(void)269 test_invalid_devargs_parsing(void)
270 {
271 static const char * const list[] = {
272 "representor=1,2,3,4", /* Out [] missing */
273 "representor=[1 2 3]", /* , missing */
274 "representor=c[1,2]", /* Only controller no valid */
275 "representor=c0vf[0-1]", /* Controller with vf alone not valid */
276 "representor=c0sf[0-1]", /* Controller with sf alone not valid */
277 "representor=vf[0-1],vf3", /* Out [] missing */
278 "representor=[[vf0,3]]", /* Double [] is invalid */
279 "representor=pfvf[1-2]", /* No PF index provided */
280 "representor=pf1[vf[1-2]]", /* Additional [] across vf */
281 "representor=c0[pf1vf[1-2]]", /* Additional [] across pfvf */
282 "representor=c0[pf1[vf[1-2]]]", /* Additional [] across pfvf */
283 "representor=[pf1vf[0-1], pf[0-1]]", /* Space between two devargs is invalid */
284 "representor=[pf1vf[0-1], 3]", /* Space between two devargs is invalid */
285 "representor=pf1vf[1-2],representor=1", /* Multiple representor keys */
286 };
287 struct rte_eth_devargs eth_da[RTE_MAX_ETHPORTS];
288 uint32_t i;
289 int ret;
290 int fail = 0;
291
292 for (i = 0; i < RTE_DIM(list); i++) {
293 memset(eth_da, 0, RTE_MAX_ETHPORTS * sizeof(*eth_da));
294 ret = rte_eth_devargs_parse(list[i], eth_da, RTE_MAX_ETHPORTS);
295 if (ret > 0) {
296 printf("rte_devargs_parse(%s) returned %d (but should not)\n",
297 list[i], ret);
298 fail = ret;
299 break;
300 }
301 }
302 return fail;
303 }
304
305 static int
test_devargs(void)306 test_devargs(void)
307 {
308 printf("== test valid case ==\n");
309 if (test_valid_devargs() < 0)
310 return -1;
311 printf("== test invalid case ==\n");
312 if (test_invalid_devargs() < 0)
313 return -1;
314 printf("== test devargs parsing valid case ==\n");
315 if (test_valid_devargs_parsing() < 0)
316 return -1;
317 printf("== test devargs parsing invalid case ==\n");
318 if (test_invalid_devargs_parsing() < 0)
319 return -1;
320 return 0;
321 }
322
323 REGISTER_FAST_TEST(devargs_autotest, true, true, test_devargs);
324