virtio_user_ethdev.c revision f7a9461e
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
52static void
53virtio_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
73static void
74virtio_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
89static void
90virtio_user_reset(struct virtio_hw *hw)
91{
92	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
93
94	if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
95		virtio_user_stop_device(dev);
96}
97
98static void
99virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
100{
101	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
102
103	if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
104		virtio_user_start_device(dev);
105	else if (status == VIRTIO_CONFIG_STATUS_RESET)
106		virtio_user_reset(hw);
107	dev->status = status;
108}
109
110static uint8_t
111virtio_user_get_status(struct virtio_hw *hw)
112{
113	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
114
115	return dev->status;
116}
117
118static uint64_t
119virtio_user_get_features(struct virtio_hw *hw)
120{
121	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
122
123	/* unmask feature bits defined in vhost user protocol */
124	return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
125}
126
127static void
128virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
129{
130	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
131
132	dev->features = features & dev->device_features;
133}
134
135static uint8_t
136virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
137{
138	/* When config interrupt happens, driver calls this function to query
139	 * what kinds of change happen. Interrupt mode not supported for now.
140	 */
141	return 0;
142}
143
144static uint16_t
145virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
146		    uint16_t vec __rte_unused)
147{
148	return VIRTIO_MSI_NO_VECTOR;
149}
150
151/* This function is to get the queue size, aka, number of descs, of a specified
152 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
153 * max supported queues.
154 */
155static uint16_t
156virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
157{
158	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
159
160	/* Currently, each queue has same queue size */
161	return dev->queue_size;
162}
163
164static int
165virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
166{
167	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
168	uint16_t queue_idx = vq->vq_queue_index;
169	uint64_t desc_addr, avail_addr, used_addr;
170
171	desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
172	avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
173	used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
174							 ring[vq->vq_nentries]),
175				   VIRTIO_PCI_VRING_ALIGN);
176
177	dev->vrings[queue_idx].num = vq->vq_nentries;
178	dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
179	dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
180	dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
181
182	return 0;
183}
184
185static void
186virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
187{
188	/* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
189	 * correspondingly stops the ioeventfds, and reset the status of
190	 * the device.
191	 * For modern devices, set queue desc, avail, used in PCI bar to 0,
192	 * not see any more behavior in QEMU.
193	 *
194	 * Here we just care about what information to deliver to vhost-user
195	 * or vhost-kernel. So we just close ioeventfd for now.
196	 */
197	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
198
199	close(dev->callfds[vq->vq_queue_index]);
200	close(dev->kickfds[vq->vq_queue_index]);
201}
202
203static void
204virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
205{
206	uint64_t buf = 1;
207	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
208
209	if (hw->cvq && (hw->cvq->vq == vq)) {
210		virtio_user_handle_cq(dev, vq->vq_queue_index);
211		return;
212	}
213
214	if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
215		PMD_DRV_LOG(ERR, "failed to kick backend: %s\n",
216			    strerror(errno));
217}
218
219const struct virtio_pci_ops virtio_user_ops = {
220	.read_dev_cfg	= virtio_user_read_dev_config,
221	.write_dev_cfg	= virtio_user_write_dev_config,
222	.reset		= virtio_user_reset,
223	.get_status	= virtio_user_get_status,
224	.set_status	= virtio_user_set_status,
225	.get_features	= virtio_user_get_features,
226	.set_features	= virtio_user_set_features,
227	.get_isr	= virtio_user_get_isr,
228	.set_config_irq	= virtio_user_set_config_irq,
229	.get_queue_num	= virtio_user_get_queue_num,
230	.setup_queue	= virtio_user_setup_queue,
231	.del_queue	= virtio_user_del_queue,
232	.notify_queue	= virtio_user_notify_queue,
233};
234
235static const char *valid_args[] = {
236#define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
237	VIRTIO_USER_ARG_QUEUES_NUM,
238#define VIRTIO_USER_ARG_CQ_NUM         "cq"
239	VIRTIO_USER_ARG_CQ_NUM,
240#define VIRTIO_USER_ARG_MAC            "mac"
241	VIRTIO_USER_ARG_MAC,
242#define VIRTIO_USER_ARG_PATH           "path"
243	VIRTIO_USER_ARG_PATH,
244#define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
245	VIRTIO_USER_ARG_QUEUE_SIZE,
246	NULL
247};
248
249#define VIRTIO_USER_DEF_CQ_EN	0
250#define VIRTIO_USER_DEF_Q_NUM	1
251#define VIRTIO_USER_DEF_Q_SZ	256
252
253static int
254get_string_arg(const char *key __rte_unused,
255	       const char *value, void *extra_args)
256{
257	if (!value || !extra_args)
258		return -EINVAL;
259
260	*(char **)extra_args = strdup(value);
261
262	return 0;
263}
264
265static int
266get_integer_arg(const char *key __rte_unused,
267		const char *value, void *extra_args)
268{
269	if (!value || !extra_args)
270		return -EINVAL;
271
272	*(uint64_t *)extra_args = strtoull(value, NULL, 0);
273
274	return 0;
275}
276
277static struct rte_eth_dev *
278virtio_user_eth_dev_alloc(const char *name)
279{
280	struct rte_eth_dev *eth_dev;
281	struct rte_eth_dev_data *data;
282	struct virtio_hw *hw;
283	struct virtio_user_dev *dev;
284
285	eth_dev = rte_eth_dev_allocate(name);
286	if (!eth_dev) {
287		PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
288		return NULL;
289	}
290
291	data = eth_dev->data;
292
293	hw = rte_zmalloc(NULL, sizeof(*hw), 0);
294	if (!hw) {
295		PMD_INIT_LOG(ERR, "malloc virtio_hw failed");
296		rte_eth_dev_release_port(eth_dev);
297		return NULL;
298	}
299
300	dev = rte_zmalloc(NULL, sizeof(*dev), 0);
301	if (!dev) {
302		PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
303		rte_eth_dev_release_port(eth_dev);
304		rte_free(hw);
305		return NULL;
306	}
307
308	hw->port_id = data->port_id;
309	virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
310	hw->use_msix = 0;
311	hw->modern   = 0;
312	hw->use_simple_rxtx = 0;
313	hw->virtio_user_dev = dev;
314	data->dev_private = hw;
315	data->numa_node = SOCKET_ID_ANY;
316	data->kdrv = RTE_KDRV_NONE;
317	data->dev_flags = RTE_ETH_DEV_DETACHABLE;
318	eth_dev->pci_dev = NULL;
319	eth_dev->driver = NULL;
320	return eth_dev;
321}
322
323static void
324virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
325{
326	struct rte_eth_dev_data *data = eth_dev->data;
327	struct virtio_hw *hw = data->dev_private;
328
329	rte_free(hw->virtio_user_dev);
330	rte_free(hw);
331	rte_eth_dev_release_port(eth_dev);
332}
333
334/* Dev initialization routine. Invoked once for each virtio vdev at
335 * EAL init time, see rte_eal_dev_init().
336 * Returns 0 on success.
337 */
338static int
339virtio_user_pmd_probe(const char *name, const char *params)
340{
341	struct rte_kvargs *kvlist = NULL;
342	struct rte_eth_dev *eth_dev;
343	struct virtio_hw *hw;
344	uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
345	uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
346	uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
347	char *path = NULL;
348	char *mac_addr = NULL;
349	int ret = -1;
350
351	if (!params || params[0] == '\0') {
352		PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
353			  VIRTIO_USER_ARG_QUEUE_SIZE);
354		goto end;
355	}
356
357	kvlist = rte_kvargs_parse(params, valid_args);
358	if (!kvlist) {
359		PMD_INIT_LOG(ERR, "error when parsing param");
360		goto end;
361	}
362
363	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
364		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
365				       &get_string_arg, &path) < 0) {
366			PMD_INIT_LOG(ERR, "error to parse %s",
367				     VIRTIO_USER_ARG_PATH);
368			goto end;
369		}
370	} else {
371		PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user\n",
372			     VIRTIO_USER_ARG_PATH);
373		goto end;
374	}
375
376	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
377		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
378				       &get_string_arg, &mac_addr) < 0) {
379			PMD_INIT_LOG(ERR, "error to parse %s",
380				     VIRTIO_USER_ARG_MAC);
381			goto end;
382		}
383	}
384
385	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
386		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
387				       &get_integer_arg, &queue_size) < 0) {
388			PMD_INIT_LOG(ERR, "error to parse %s",
389				     VIRTIO_USER_ARG_QUEUE_SIZE);
390			goto end;
391		}
392	}
393
394	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
395		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
396				       &get_integer_arg, &queues) < 0) {
397			PMD_INIT_LOG(ERR, "error to parse %s",
398				     VIRTIO_USER_ARG_QUEUES_NUM);
399			goto end;
400		}
401	}
402
403	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
404		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
405				       &get_integer_arg, &cq) < 0) {
406			PMD_INIT_LOG(ERR, "error to parse %s",
407				     VIRTIO_USER_ARG_CQ_NUM);
408			goto end;
409		}
410	} else if (queues > 1) {
411		cq = 1;
412	}
413
414	if (queues > 1 && cq == 0) {
415		PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
416		goto end;
417	}
418
419	if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
420		PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
421			VIRTIO_USER_ARG_QUEUES_NUM, queues,
422			VIRTIO_MAX_VIRTQUEUE_PAIRS);
423		goto end;
424	}
425
426	eth_dev = virtio_user_eth_dev_alloc(name);
427	if (!eth_dev) {
428		PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
429		goto end;
430	}
431
432	hw = eth_dev->data->dev_private;
433	if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
434				 queue_size, mac_addr) < 0) {
435		PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
436		virtio_user_eth_dev_free(eth_dev);
437		goto end;
438	}
439
440	/* previously called by rte_eal_pci_probe() for physical dev */
441	if (eth_virtio_dev_init(eth_dev) < 0) {
442		PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
443		virtio_user_eth_dev_free(eth_dev);
444		goto end;
445	}
446	ret = 0;
447
448end:
449	if (kvlist)
450		rte_kvargs_free(kvlist);
451	if (path)
452		free(path);
453	if (mac_addr)
454		free(mac_addr);
455	return ret;
456}
457
458/** Called by rte_eth_dev_detach() */
459static int
460virtio_user_pmd_remove(const char *name)
461{
462	struct rte_eth_dev *eth_dev;
463	struct virtio_hw *hw;
464	struct virtio_user_dev *dev;
465
466	if (!name)
467		return -EINVAL;
468
469	PMD_DRV_LOG(INFO, "Un-Initializing %s\n", name);
470	eth_dev = rte_eth_dev_allocated(name);
471	if (!eth_dev)
472		return -ENODEV;
473
474	/* make sure the device is stopped, queues freed */
475	rte_eth_dev_close(eth_dev->data->port_id);
476
477	hw = eth_dev->data->dev_private;
478	dev = hw->virtio_user_dev;
479	virtio_user_dev_uninit(dev);
480
481	rte_free(eth_dev->data->dev_private);
482	rte_eth_dev_release_port(eth_dev);
483
484	return 0;
485}
486
487static struct rte_vdev_driver virtio_user_driver = {
488	.probe = virtio_user_pmd_probe,
489	.remove = virtio_user_pmd_remove,
490};
491
492RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
493RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
494RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
495	"path=<path> "
496	"mac=<mac addr> "
497	"cq=<int> "
498	"queue_size=<int> "
499	"queues=<int>");
500