4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
41 #include <sys/eventfd.h>
42 #include <sys/types.h>
47 #include "virtio_user_dev.h"
48 #include "../virtio_ethdev.h"
51 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
53 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
54 * firstly because vhost depends on this msg to allocate virtqueue
57 struct vhost_vring_file file;
59 file.index = queue_sel;
60 file.fd = dev->callfds[queue_sel];
61 dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
67 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
69 struct vhost_vring_file file;
70 struct vhost_vring_state state;
71 struct vring *vring = &dev->vrings[queue_sel];
72 struct vhost_vring_addr addr = {
74 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
75 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
76 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
78 .flags = 0, /* disable log */
81 state.index = queue_sel;
82 state.num = vring->num;
83 dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
85 state.index = queue_sel;
86 state.num = 0; /* no reservation */
87 dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
89 dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
91 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
92 * lastly because vhost depends on this msg to judge if
95 file.index = queue_sel;
96 file.fd = dev->kickfds[queue_sel];
97 dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
103 virtio_user_queue_setup(struct virtio_user_dev *dev,
104 int (*fn)(struct virtio_user_dev *, uint32_t))
106 uint32_t i, queue_sel;
108 for (i = 0; i < dev->max_queue_pairs; ++i) {
109 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
110 if (fn(dev, queue_sel) < 0) {
111 PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
115 for (i = 0; i < dev->max_queue_pairs; ++i) {
116 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
117 if (fn(dev, queue_sel) < 0) {
118 PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
127 virtio_user_start_device(struct virtio_user_dev *dev)
132 /* Step 0: tell vhost to create queues */
133 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
136 /* Step 1: set features */
137 features = dev->features;
138 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
139 features &= ~(1ull << VIRTIO_NET_F_MAC);
140 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
141 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
142 features &= ~(1ull << VIRTIO_NET_F_STATUS);
143 ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
146 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
148 /* Step 2: share memory regions */
149 ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
153 /* Step 3: kick queues */
154 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
157 /* Step 4: enable queues
158 * we enable the 1st queue pair by default.
160 dev->ops->enable_qp(dev, 0, 1);
164 /* TODO: free resource here or caller to check */
168 int virtio_user_stop_device(struct virtio_user_dev *dev)
172 for (i = 0; i < dev->max_queue_pairs; ++i)
173 dev->ops->enable_qp(dev, i, 0);
179 parse_mac(struct virtio_user_dev *dev, const char *mac)
182 uint32_t tmp[ETHER_ADDR_LEN];
187 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
188 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
189 if (r == ETHER_ADDR_LEN) {
190 for (i = 0; i < ETHER_ADDR_LEN; ++i)
191 dev->mac_addr[i] = (uint8_t)tmp[i];
192 dev->mac_specified = 1;
194 /* ignore the wrong mac, use random mac */
195 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
200 is_vhost_user_by_type(const char *path)
204 if (stat(path, &sb) == -1)
207 return S_ISSOCK(sb.st_mode);
211 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
217 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
218 if (i >= dev->max_queue_pairs * 2) {
219 dev->kickfds[i] = -1;
220 dev->callfds[i] = -1;
224 /* May use invalid flag, but some backend uses kickfd and
225 * callfd as criteria to judge if dev is alive. so finally we
228 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
230 PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
233 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
235 PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
238 dev->callfds[i] = callfd;
239 dev->kickfds[i] = kickfd;
242 if (i < VIRTIO_MAX_VIRTQUEUES) {
243 for (j = 0; j <= i; ++j) {
244 close(dev->callfds[j]);
245 close(dev->kickfds[j]);
255 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
258 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
260 if (!eth_dev->intr_handle) {
261 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
262 if (!eth_dev->intr_handle) {
263 PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
266 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
269 for (i = 0; i < dev->max_queue_pairs; ++i)
270 eth_dev->intr_handle->efds[i] = dev->callfds[i];
271 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
272 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
273 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
279 virtio_user_dev_setup(struct virtio_user_dev *dev)
284 dev->vhostfds = NULL;
287 if (virtio_user_dev_init_notify(dev) < 0)
290 if (virtio_user_fill_intr_handle(dev) < 0)
293 if (is_vhost_user_by_type(dev->path)) {
294 dev->ops = &ops_user;
296 dev->ops = &ops_kernel;
298 dev->vhostfds = malloc(dev->max_queue_pairs * sizeof(int));
299 dev->tapfds = malloc(dev->max_queue_pairs * sizeof(int));
300 if (!dev->vhostfds || !dev->tapfds) {
301 PMD_INIT_LOG(ERR, "Failed to malloc");
305 for (q = 0; q < dev->max_queue_pairs; ++q) {
306 dev->vhostfds[q] = -1;
311 return dev->ops->setup(dev);
314 /* Use below macro to filter features from vhost backend */
315 #define VIRTIO_USER_SUPPORTED_FEATURES \
316 (1ULL << VIRTIO_NET_F_MAC | \
317 1ULL << VIRTIO_NET_F_STATUS | \
318 1ULL << VIRTIO_NET_F_MQ | \
319 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \
320 1ULL << VIRTIO_NET_F_CTRL_VQ | \
321 1ULL << VIRTIO_NET_F_CTRL_RX | \
322 1ULL << VIRTIO_NET_F_CTRL_VLAN | \
323 1ULL << VIRTIO_NET_F_CSUM | \
324 1ULL << VIRTIO_NET_F_HOST_TSO4 | \
325 1ULL << VIRTIO_NET_F_HOST_TSO6 | \
326 1ULL << VIRTIO_NET_F_MRG_RXBUF | \
327 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \
328 1ULL << VIRTIO_NET_F_GUEST_CSUM | \
329 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \
330 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \
331 1ULL << VIRTIO_F_VERSION_1)
334 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
335 int cq, int queue_size, const char *mac, char **ifname)
337 snprintf(dev->path, PATH_MAX, "%s", path);
338 dev->max_queue_pairs = queues;
339 dev->queue_pairs = 1; /* mq disabled by default */
340 dev->queue_size = queue_size;
341 dev->mac_specified = 0;
345 dev->ifname = *ifname;
349 if (virtio_user_dev_setup(dev) < 0) {
350 PMD_INIT_LOG(ERR, "backend set up fails");
353 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL) < 0) {
354 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
358 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
359 &dev->device_features) < 0) {
360 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
363 if (dev->mac_specified)
364 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
367 /* device does not really need to know anything about CQ,
368 * so if necessary, we just claim to support CQ
370 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
372 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
373 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
374 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
375 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
376 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
377 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
378 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
381 /* The backend will not report this feature, we add it explicitly */
382 if (is_vhost_user_by_type(dev->path))
383 dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
385 dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
391 virtio_user_dev_uninit(struct virtio_user_dev *dev)
395 virtio_user_stop_device(dev);
397 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
398 close(dev->callfds[i]);
399 close(dev->kickfds[i]);
405 for (i = 0; i < dev->max_queue_pairs; ++i)
406 close(dev->vhostfds[i]);
415 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
420 if (q_pairs > dev->max_queue_pairs) {
421 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
422 q_pairs, dev->max_queue_pairs);
426 for (i = 0; i < q_pairs; ++i)
427 ret |= dev->ops->enable_qp(dev, i, 1);
428 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
429 ret |= dev->ops->enable_qp(dev, i, 0);
431 dev->queue_pairs = q_pairs;
437 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
440 struct virtio_net_ctrl_hdr *hdr;
441 virtio_net_ctrl_ack status = ~0;
442 uint16_t i, idx_data, idx_status;
443 uint32_t n_descs = 0;
445 /* locate desc for header, data, and status */
446 idx_data = vring->desc[idx_hdr].next;
450 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
451 i = vring->desc[i].next;
455 /* locate desc for status */
459 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
460 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
461 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
464 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
465 status = virtio_user_handle_mq(dev, queues);
469 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
475 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
477 uint16_t avail_idx, desc_idx;
478 struct vring_used_elem *uep;
480 struct vring *vring = &dev->vrings[queue_idx];
482 /* Consume avail ring, using used ring idx as first one */
483 while (vring->used->idx != vring->avail->idx) {
484 avail_idx = (vring->used->idx) & (vring->num - 1);
485 desc_idx = vring->avail->ring[avail_idx];
487 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
489 /* Update used ring */
490 uep = &vring->used->ring[avail_idx];