xref: /dpdk/drivers/net/virtio/virtio_user_ethdev.c (revision c6dab2a873f65c5a4ea9735aa24d9539426adba4)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <stdint.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 
38 #include <rte_malloc.h>
39 #include <rte_kvargs.h>
40 #include <rte_vdev.h>
41 
42 #include "virtio_ethdev.h"
43 #include "virtio_logs.h"
44 #include "virtio_pci.h"
45 #include "virtqueue.h"
46 #include "virtio_rxtx.h"
47 #include "virtio_user/virtio_user_dev.h"
48 
49 #define virtio_user_get_dev(hw) \
50 	((struct virtio_user_dev *)(hw)->virtio_user_dev)
51 
52 static void
53 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
54 		     void *dst, int length)
55 {
56 	int i;
57 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
58 
59 	if (offset == offsetof(struct virtio_net_config, mac) &&
60 	    length == ETHER_ADDR_LEN) {
61 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
62 			((uint8_t *)dst)[i] = dev->mac_addr[i];
63 		return;
64 	}
65 
66 	if (offset == offsetof(struct virtio_net_config, status))
67 		*(uint16_t *)dst = dev->status;
68 
69 	if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
70 		*(uint16_t *)dst = dev->max_queue_pairs;
71 }
72 
73 static void
74 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
75 		      const void *src, int length)
76 {
77 	int i;
78 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
79 
80 	if ((offset == offsetof(struct virtio_net_config, mac)) &&
81 	    (length == ETHER_ADDR_LEN))
82 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
83 			dev->mac_addr[i] = ((const uint8_t *)src)[i];
84 	else
85 		PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d\n",
86 			    offset, length);
87 }
88 
89 static void
90 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
91 {
92 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
93 
94 	if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
95 		virtio_user_start_device(dev);
96 	dev->status = status;
97 }
98 
99 static void
100 virtio_user_reset(struct virtio_hw *hw)
101 {
102 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
103 
104 	virtio_user_stop_device(dev);
105 }
106 
107 static uint8_t
108 virtio_user_get_status(struct virtio_hw *hw)
109 {
110 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
111 
112 	return dev->status;
113 }
114 
115 static uint64_t
116 virtio_user_get_features(struct virtio_hw *hw)
117 {
118 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
119 
120 	return dev->features;
121 }
122 
123 static void
124 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
125 {
126 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
127 
128 	dev->features = features;
129 }
130 
131 static uint8_t
132 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
133 {
134 	/* When config interrupt happens, driver calls this function to query
135 	 * what kinds of change happen. Interrupt mode not supported for now.
136 	 */
137 	return 0;
138 }
139 
140 static uint16_t
141 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
142 		    uint16_t vec __rte_unused)
143 {
144 	return VIRTIO_MSI_NO_VECTOR;
145 }
146 
147 /* This function is to get the queue size, aka, number of descs, of a specified
148  * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
149  * max supported queues.
150  */
151 static uint16_t
152 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
153 {
154 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
155 
156 	/* Currently, each queue has same queue size */
157 	return dev->queue_size;
158 }
159 
160 static int
161 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
162 {
163 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
164 	uint16_t queue_idx = vq->vq_queue_index;
165 	uint64_t desc_addr, avail_addr, used_addr;
166 
167 	desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
168 	avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
169 	used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
170 							 ring[vq->vq_nentries]),
171 				   VIRTIO_PCI_VRING_ALIGN);
172 
173 	dev->vrings[queue_idx].num = vq->vq_nentries;
174 	dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
175 	dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
176 	dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
177 
178 	return 0;
179 }
180 
181 static void
182 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
183 {
184 	/* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
185 	 * correspondingly stops the ioeventfds, and reset the status of
186 	 * the device.
187 	 * For modern devices, set queue desc, avail, used in PCI bar to 0,
188 	 * not see any more behavior in QEMU.
189 	 *
190 	 * Here we just care about what information to deliver to vhost-user
191 	 * or vhost-kernel. So we just close ioeventfd for now.
192 	 */
193 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
194 
195 	close(dev->callfds[vq->vq_queue_index]);
196 	close(dev->kickfds[vq->vq_queue_index]);
197 }
198 
199 static void
200 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
201 {
202 	uint64_t buf = 1;
203 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
204 
205 	if (hw->cvq && (hw->cvq->vq == vq)) {
206 		virtio_user_handle_cq(dev, vq->vq_queue_index);
207 		return;
208 	}
209 
210 	if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
211 		PMD_DRV_LOG(ERR, "failed to kick backend: %s\n",
212 			    strerror(errno));
213 }
214 
215 static const struct virtio_pci_ops virtio_user_ops = {
216 	.read_dev_cfg	= virtio_user_read_dev_config,
217 	.write_dev_cfg	= virtio_user_write_dev_config,
218 	.reset		= virtio_user_reset,
219 	.get_status	= virtio_user_get_status,
220 	.set_status	= virtio_user_set_status,
221 	.get_features	= virtio_user_get_features,
222 	.set_features	= virtio_user_set_features,
223 	.get_isr	= virtio_user_get_isr,
224 	.set_config_irq	= virtio_user_set_config_irq,
225 	.get_queue_num	= virtio_user_get_queue_num,
226 	.setup_queue	= virtio_user_setup_queue,
227 	.del_queue	= virtio_user_del_queue,
228 	.notify_queue	= virtio_user_notify_queue,
229 };
230 
231 static const char *valid_args[] = {
232 #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
233 	VIRTIO_USER_ARG_QUEUES_NUM,
234 #define VIRTIO_USER_ARG_CQ_NUM         "cq"
235 	VIRTIO_USER_ARG_CQ_NUM,
236 #define VIRTIO_USER_ARG_MAC            "mac"
237 	VIRTIO_USER_ARG_MAC,
238 #define VIRTIO_USER_ARG_PATH           "path"
239 	VIRTIO_USER_ARG_PATH,
240 #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
241 	VIRTIO_USER_ARG_QUEUE_SIZE,
242 	NULL
243 };
244 
245 #define VIRTIO_USER_DEF_CQ_EN	0
246 #define VIRTIO_USER_DEF_Q_NUM	1
247 #define VIRTIO_USER_DEF_Q_SZ	256
248 
249 static int
250 get_string_arg(const char *key __rte_unused,
251 	       const char *value, void *extra_args)
252 {
253 	if (!value || !extra_args)
254 		return -EINVAL;
255 
256 	*(char **)extra_args = strdup(value);
257 
258 	return 0;
259 }
260 
261 static int
262 get_integer_arg(const char *key __rte_unused,
263 		const char *value, void *extra_args)
264 {
265 	if (!value || !extra_args)
266 		return -EINVAL;
267 
268 	*(uint64_t *)extra_args = strtoull(value, NULL, 0);
269 
270 	return 0;
271 }
272 
273 static struct rte_eth_dev *
274 virtio_user_eth_dev_alloc(const char *name)
275 {
276 	struct rte_eth_dev *eth_dev;
277 	struct rte_eth_dev_data *data;
278 	struct virtio_hw *hw;
279 	struct virtio_user_dev *dev;
280 
281 	eth_dev = rte_eth_dev_allocate(name);
282 	if (!eth_dev) {
283 		PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
284 		return NULL;
285 	}
286 
287 	data = eth_dev->data;
288 
289 	hw = rte_zmalloc(NULL, sizeof(*hw), 0);
290 	if (!hw) {
291 		PMD_INIT_LOG(ERR, "malloc virtio_hw failed");
292 		rte_eth_dev_release_port(eth_dev);
293 		return NULL;
294 	}
295 
296 	dev = rte_zmalloc(NULL, sizeof(*dev), 0);
297 	if (!dev) {
298 		PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
299 		rte_eth_dev_release_port(eth_dev);
300 		rte_free(hw);
301 		return NULL;
302 	}
303 
304 	hw->vtpci_ops = &virtio_user_ops;
305 	hw->use_msix = 0;
306 	hw->modern   = 0;
307 	hw->use_simple_rxtx = 0;
308 	hw->virtio_user_dev = dev;
309 	data->dev_private = hw;
310 	data->numa_node = SOCKET_ID_ANY;
311 	data->kdrv = RTE_KDRV_NONE;
312 	data->dev_flags = RTE_ETH_DEV_DETACHABLE;
313 	eth_dev->driver = NULL;
314 	return eth_dev;
315 }
316 
317 static void
318 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
319 {
320 	struct rte_eth_dev_data *data = eth_dev->data;
321 	struct virtio_hw *hw = data->dev_private;
322 
323 	rte_free(hw->virtio_user_dev);
324 	rte_free(hw);
325 	rte_eth_dev_release_port(eth_dev);
326 }
327 
328 /* Dev initialization routine. Invoked once for each virtio vdev at
329  * EAL init time, see rte_eal_dev_init().
330  * Returns 0 on success.
331  */
332 static int
333 virtio_user_pmd_probe(const char *name, const char *params)
334 {
335 	struct rte_kvargs *kvlist = NULL;
336 	struct rte_eth_dev *eth_dev;
337 	struct virtio_hw *hw;
338 	uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
339 	uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
340 	uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
341 	char *path = NULL;
342 	char *mac_addr = NULL;
343 	int ret = -1;
344 
345 	if (!params || params[0] == '\0') {
346 		PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
347 			  VIRTIO_USER_ARG_QUEUE_SIZE);
348 		goto end;
349 	}
350 
351 	kvlist = rte_kvargs_parse(params, valid_args);
352 	if (!kvlist) {
353 		PMD_INIT_LOG(ERR, "error when parsing param");
354 		goto end;
355 	}
356 
357 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
358 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
359 				       &get_string_arg, &path) < 0) {
360 			PMD_INIT_LOG(ERR, "error to parse %s",
361 				     VIRTIO_USER_ARG_PATH);
362 			goto end;
363 		}
364 	} else {
365 		PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user\n",
366 			  VIRTIO_USER_ARG_QUEUE_SIZE);
367 		goto end;
368 	}
369 
370 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
371 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
372 				       &get_string_arg, &mac_addr) < 0) {
373 			PMD_INIT_LOG(ERR, "error to parse %s",
374 				     VIRTIO_USER_ARG_MAC);
375 			goto end;
376 		}
377 	}
378 
379 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
380 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
381 				       &get_integer_arg, &queue_size) < 0) {
382 			PMD_INIT_LOG(ERR, "error to parse %s",
383 				     VIRTIO_USER_ARG_QUEUE_SIZE);
384 			goto end;
385 		}
386 	}
387 
388 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
389 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
390 				       &get_integer_arg, &queues) < 0) {
391 			PMD_INIT_LOG(ERR, "error to parse %s",
392 				     VIRTIO_USER_ARG_QUEUES_NUM);
393 			goto end;
394 		}
395 	}
396 
397 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
398 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
399 				       &get_integer_arg, &cq) < 0) {
400 			PMD_INIT_LOG(ERR, "error to parse %s",
401 				     VIRTIO_USER_ARG_CQ_NUM);
402 			goto end;
403 		}
404 	} else if (queues > 1) {
405 		cq = 1;
406 	}
407 
408 	if (queues > 1 && cq == 0) {
409 		PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
410 		goto end;
411 	}
412 
413 	eth_dev = virtio_user_eth_dev_alloc(name);
414 	if (!eth_dev) {
415 		PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
416 		goto end;
417 	}
418 
419 	hw = eth_dev->data->dev_private;
420 	if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
421 				 queue_size, mac_addr) < 0) {
422 		PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
423 		virtio_user_eth_dev_free(eth_dev);
424 		goto end;
425 	}
426 
427 	/* previously called by rte_eal_pci_probe() for physical dev */
428 	if (eth_virtio_dev_init(eth_dev) < 0) {
429 		PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
430 		virtio_user_eth_dev_free(eth_dev);
431 		goto end;
432 	}
433 	ret = 0;
434 
435 end:
436 	if (kvlist)
437 		rte_kvargs_free(kvlist);
438 	if (path)
439 		free(path);
440 	if (mac_addr)
441 		free(mac_addr);
442 	return ret;
443 }
444 
445 /** Called by rte_eth_dev_detach() */
446 static int
447 virtio_user_pmd_remove(const char *name)
448 {
449 	struct rte_eth_dev *eth_dev;
450 	struct virtio_hw *hw;
451 	struct virtio_user_dev *dev;
452 
453 	if (!name)
454 		return -EINVAL;
455 
456 	PMD_DRV_LOG(INFO, "Un-Initializing %s\n", name);
457 	eth_dev = rte_eth_dev_allocated(name);
458 	if (!eth_dev)
459 		return -ENODEV;
460 
461 	/* make sure the device is stopped, queues freed */
462 	rte_eth_dev_close(eth_dev->data->port_id);
463 
464 	hw = eth_dev->data->dev_private;
465 	dev = hw->virtio_user_dev;
466 	virtio_user_dev_uninit(dev);
467 
468 	rte_free(eth_dev->data->dev_private);
469 	rte_free(eth_dev->data);
470 	rte_eth_dev_release_port(eth_dev);
471 
472 	return 0;
473 }
474 
475 static struct rte_vdev_driver virtio_user_driver = {
476 	.probe = virtio_user_pmd_probe,
477 	.remove = virtio_user_pmd_remove,
478 };
479 
480 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
481 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
482 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
483 	"path=<path> "
484 	"mac=<mac addr> "
485 	"cq=<int> "
486 	"queue_size=<int> "
487 	"queues=<int>");
488