xref: /dpdk/app/test/test_devargs.c (revision 148c51a3deff2ea4be3bc3fd0cb045a96f28ec64)
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 <rte_common.h>
10 #include <rte_devargs.h>
11 #include <rte_kvargs.h>
12 #include <rte_bus.h>
13 #include <rte_class.h>
14 
15 #include "test.h"
16 
17 /* Check layer arguments. */
18 static int
19 test_args(const char *devargs, const char *layer, const char *args, const int n)
20 {
21 	struct rte_kvargs *kvlist;
22 
23 	if (n == 0) {
24 		if (args != NULL && strlen(args) > 0) {
25 			printf("rte_devargs_parse(%s) %s args parsed (not expected)\n",
26 			       devargs, layer);
27 			return -1;
28 		} else {
29 			return 0;
30 		}
31 	}
32 	if (args == NULL) {
33 		printf("rte_devargs_parse(%s) %s args not parsed\n",
34 		       devargs, layer);
35 		return -1;
36 	}
37 	kvlist = rte_kvargs_parse(args, NULL);
38 	if (kvlist == NULL) {
39 		printf("rte_devargs_parse(%s) %s_str: %s not parsed\n",
40 		       devargs, layer, args);
41 		return -1;
42 	}
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);
47 		return -1;
48 	}
49 	rte_kvargs_free(kvlist);
50 	return 0;
51 }
52 
53 struct devargs_case {
54 	const char *devargs;
55 	int bus_kv;
56 	int class_kv;
57 	int driver_kv;
58 	const char *bus;
59 	const char *name;
60 	const char *class;
61 };
62 
63 static int
64 test_valid_devargs_cases(const struct devargs_case *list, size_t n)
65 {
66 	struct rte_devargs da;
67 	uint32_t i;
68 	int ret;
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");
73 
74 	for (i = 0; i < n; i++) {
75 		if (pci_bus == NULL && list[i].bus != NULL &&
76 		    strcmp(list[i].bus, "pci") == 0)
77 			continue;
78 		if (vdev_bus == NULL && list[i].bus != NULL &&
79 		    strcmp(list[i].bus, "vdev") == 0)
80 			continue;
81 		if (eth_class == NULL && list[i].class != NULL &&
82 		    strcmp(list[i].class, "eth") == 0)
83 			continue;
84 		memset(&da, 0, sizeof(da));
85 		ret = rte_devargs_parse(&da, list[i].devargs);
86 		if (ret < 0) {
87 			printf("rte_devargs_parse(%s) returned %d (but should not)\n",
88 			       list[i].devargs, ret);
89 			goto fail;
90 		}
91 		if ((list[i].bus_kv > 0 || list[i].bus != NULL) &&
92 		    da.bus == NULL) {
93 			printf("rte_devargs_parse(%s) bus not parsed\n",
94 			       list[i].devargs);
95 			goto fail;
96 		}
97 		if (test_args(list[i].devargs, "bus", da.bus_str,
98 			      list[i].bus_kv) != 0)
99 			goto fail;
100 		if (list[i].bus != NULL &&
101 		    strcmp(rte_bus_name(da.bus), list[i].bus) != 0) {
102 			printf("rte_devargs_parse(%s) bus name (%s) not expected (%s)\n",
103 			       list[i].devargs, rte_bus_name(da.bus), list[i].bus);
104 			goto fail;
105 		}
106 		if ((list[i].class_kv > 0 || list[i].class != NULL) &&
107 		    da.cls == NULL) {
108 			printf("rte_devargs_parse(%s) class not parsed\n",
109 			       list[i].devargs);
110 			goto fail;
111 		}
112 		if (test_args(list[i].devargs, "class", da.cls_str,
113 			      list[i].class_kv) != 0)
114 			goto fail;
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);
119 			goto fail;
120 		}
121 		if (test_args(list[i].devargs, "driver", da.drv_str,
122 			      list[i].driver_kv) != 0)
123 			goto fail;
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);
128 			goto fail;
129 		}
130 		goto cleanup;
131 fail:
132 		fail = TEST_FAILED;
133 cleanup:
134 		rte_devargs_reset(&da);
135 	}
136 	return fail;
137 }
138 
139 /* Test several valid cases */
140 static int
141 test_valid_devargs(void)
142 {
143 	static const struct devargs_case list[] = {
144 		/* Global devargs syntax: */
145 		{ "bus=pci",
146 		  1, 0, 0, "pci", NULL, NULL},
147 		{ "class=eth",
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: */
158 		{ "1:2.3", 0, 0, 0,
159 		  "pci", "1:2.3", NULL },
160 		{ "pci:1:2.3,k0=v0",
161 		  0, 0, 1, "pci", "1:2.3", NULL },
162 	};
163 	static const struct devargs_case legacy_ring_list[] = {
164 		{ "net_ring0",
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 },
168 	};
169 	struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
170 	int ret;
171 
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));
177 	return ret;
178 }
179 
180 /* Test several invalid cases */
181 static int
182 test_invalid_devargs(void)
183 {
184 	static const char * const list[] = {
185 		"bus=wrong-bus",
186 		"class=wrong-class"};
187 	struct rte_devargs da;
188 	uint32_t i;
189 	int ret;
190 	int fail = 0;
191 
192 	for (i = 0; i < RTE_DIM(list); i++) {
193 		ret = rte_devargs_parse(&da, list[i]);
194 		if (ret >= 0) {
195 			printf("rte_devargs_parse(%s) returned %d (but should not)\n",
196 			       list[i], ret);
197 			fail = ret;
198 		}
199 		rte_devargs_reset(&da);
200 	}
201 	return fail;
202 }
203 
204 static int
205 test_devargs(void)
206 {
207 	printf("== test valid case ==\n");
208 	if (test_valid_devargs() < 0)
209 		return -1;
210 	printf("== test invalid case ==\n");
211 	if (test_invalid_devargs() < 0)
212 		return -1;
213 	return 0;
214 }
215 
216 REGISTER_TEST_COMMAND(devargs_autotest, test_devargs);
217