net/virtio-user: fix multiple queues fail in server mode
[dpdk.git] / drivers / net / virtio / virtio_user / virtio_user_dev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <sys/mman.h>
11 #include <unistd.h>
12 #include <sys/eventfd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15
16 #include "vhost.h"
17 #include "virtio_user_dev.h"
18 #include "../virtio_ethdev.h"
19
20 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb"
21
22 static int
23 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
24 {
25         /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
26          * firstly because vhost depends on this msg to allocate virtqueue
27          * pair.
28          */
29         struct vhost_vring_file file;
30
31         file.index = queue_sel;
32         file.fd = dev->callfds[queue_sel];
33         dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
34
35         return 0;
36 }
37
38 static int
39 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
40 {
41         struct vhost_vring_file file;
42         struct vhost_vring_state state;
43         struct vring *vring = &dev->vrings[queue_sel];
44         struct vhost_vring_addr addr = {
45                 .index = queue_sel,
46                 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
47                 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
48                 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
49                 .log_guest_addr = 0,
50                 .flags = 0, /* disable log */
51         };
52
53         state.index = queue_sel;
54         state.num = vring->num;
55         dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
56
57         state.index = queue_sel;
58         state.num = 0; /* no reservation */
59         dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
60
61         dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
62
63         /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
64          * lastly because vhost depends on this msg to judge if
65          * virtio is ready.
66          */
67         file.index = queue_sel;
68         file.fd = dev->kickfds[queue_sel];
69         dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
70
71         return 0;
72 }
73
74 static int
75 virtio_user_queue_setup(struct virtio_user_dev *dev,
76                         int (*fn)(struct virtio_user_dev *, uint32_t))
77 {
78         uint32_t i, queue_sel;
79
80         for (i = 0; i < dev->max_queue_pairs; ++i) {
81                 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
82                 if (fn(dev, queue_sel) < 0) {
83                         PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
84                         return -1;
85                 }
86         }
87         for (i = 0; i < dev->max_queue_pairs; ++i) {
88                 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
89                 if (fn(dev, queue_sel) < 0) {
90                         PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
91                         return -1;
92                 }
93         }
94
95         return 0;
96 }
97
98 int
99 is_vhost_user_by_type(const char *path)
100 {
101         struct stat sb;
102
103         if (stat(path, &sb) == -1)
104                 return 0;
105
106         return S_ISSOCK(sb.st_mode);
107 }
108
109 int
110 virtio_user_start_device(struct virtio_user_dev *dev)
111 {
112         uint64_t features;
113         int ret;
114
115         pthread_mutex_lock(&dev->mutex);
116
117         if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0)
118                 goto error;
119
120         /* Do not check return as already done in init, or reset in stop */
121         dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL);
122
123         /* Step 0: tell vhost to create queues */
124         if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
125                 goto error;
126
127         /* Step 1: set features */
128         features = dev->features;
129         /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
130         features &= ~(1ull << VIRTIO_NET_F_MAC);
131         /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
132         features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
133         features &= ~(1ull << VIRTIO_NET_F_STATUS);
134         ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
135         if (ret < 0)
136                 goto error;
137         PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
138
139         /* Step 2: share memory regions */
140         ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
141         if (ret < 0)
142                 goto error;
143
144         /* Step 3: kick queues */
145         if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
146                 goto error;
147
148         /* Step 4: enable queues
149          * we enable the 1st queue pair by default.
150          */
151         dev->ops->enable_qp(dev, 0, 1);
152
153         dev->started = true;
154         pthread_mutex_unlock(&dev->mutex);
155
156         return 0;
157 error:
158         pthread_mutex_unlock(&dev->mutex);
159         /* TODO: free resource here or caller to check */
160         return -1;
161 }
162
163 int virtio_user_stop_device(struct virtio_user_dev *dev)
164 {
165         uint32_t i;
166
167         pthread_mutex_lock(&dev->mutex);
168         for (i = 0; i < dev->max_queue_pairs; ++i)
169                 dev->ops->enable_qp(dev, i, 0);
170
171         if (dev->ops->send_request(dev, VHOST_USER_RESET_OWNER, NULL) < 0) {
172                 PMD_DRV_LOG(INFO, "Failed to reset the device\n");
173                 pthread_mutex_unlock(&dev->mutex);
174                 return -1;
175         }
176         dev->started = false;
177         pthread_mutex_unlock(&dev->mutex);
178
179         return 0;
180 }
181
182 static inline void
183 parse_mac(struct virtio_user_dev *dev, const char *mac)
184 {
185         int i, r;
186         uint32_t tmp[ETHER_ADDR_LEN];
187
188         if (!mac)
189                 return;
190
191         r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
192                         &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
193         if (r == ETHER_ADDR_LEN) {
194                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
195                         dev->mac_addr[i] = (uint8_t)tmp[i];
196                 dev->mac_specified = 1;
197         } else {
198                 /* ignore the wrong mac, use random mac */
199                 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
200         }
201 }
202
203 static int
204 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
205 {
206         uint32_t i, j;
207         int callfd;
208         int kickfd;
209
210         for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
211                 if (i >= dev->max_queue_pairs * 2) {
212                         dev->kickfds[i] = -1;
213                         dev->callfds[i] = -1;
214                         continue;
215                 }
216
217                 /* May use invalid flag, but some backend uses kickfd and
218                  * callfd as criteria to judge if dev is alive. so finally we
219                  * use real event_fd.
220                  */
221                 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
222                 if (callfd < 0) {
223                         PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
224                         break;
225                 }
226                 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
227                 if (kickfd < 0) {
228                         PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
229                         break;
230                 }
231                 dev->callfds[i] = callfd;
232                 dev->kickfds[i] = kickfd;
233         }
234
235         if (i < VIRTIO_MAX_VIRTQUEUES) {
236                 for (j = 0; j <= i; ++j) {
237                         close(dev->callfds[j]);
238                         close(dev->kickfds[j]);
239                 }
240
241                 return -1;
242         }
243
244         return 0;
245 }
246
247 static int
248 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
249 {
250         uint32_t i;
251         struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
252
253         if (!eth_dev->intr_handle) {
254                 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
255                 if (!eth_dev->intr_handle) {
256                         PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
257                         return -1;
258                 }
259                 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
260         }
261
262         for (i = 0; i < dev->max_queue_pairs; ++i)
263                 eth_dev->intr_handle->efds[i] = dev->callfds[i];
264         eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
265         eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
266         eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
267         /* For virtio vdev, no need to read counter for clean */
268         eth_dev->intr_handle->efd_counter_size = 0;
269         eth_dev->intr_handle->fd = -1;
270         if (dev->vhostfd >= 0)
271                 eth_dev->intr_handle->fd = dev->vhostfd;
272         else if (dev->is_server)
273                 eth_dev->intr_handle->fd = dev->listenfd;
274
275         return 0;
276 }
277
278 static void
279 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
280                                                  const void *addr __rte_unused,
281                                                  size_t len __rte_unused,
282                                                  void *arg)
283 {
284         struct virtio_user_dev *dev = arg;
285         uint16_t i;
286
287         pthread_mutex_lock(&dev->mutex);
288
289         if (dev->started == false)
290                 goto exit;
291
292         /* Step 1: pause the active queues */
293         for (i = 0; i < dev->queue_pairs; i++)
294                 dev->ops->enable_qp(dev, i, 0);
295
296         /* Step 2: update memory regions */
297         dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
298
299         /* Step 3: resume the active queues */
300         for (i = 0; i < dev->queue_pairs; i++)
301                 dev->ops->enable_qp(dev, i, 1);
302
303 exit:
304         pthread_mutex_unlock(&dev->mutex);
305 }
306
307 static int
308 virtio_user_dev_setup(struct virtio_user_dev *dev)
309 {
310         uint32_t q;
311
312         dev->vhostfd = -1;
313         dev->vhostfds = NULL;
314         dev->tapfds = NULL;
315
316         if (dev->is_server) {
317                 if (access(dev->path, F_OK) == 0 &&
318                     !is_vhost_user_by_type(dev->path)) {
319                         PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!");
320                         return -1;
321                 }
322                 dev->ops = &ops_user;
323         } else {
324                 if (is_vhost_user_by_type(dev->path)) {
325                         dev->ops = &ops_user;
326                 } else {
327                         dev->ops = &ops_kernel;
328
329                         dev->vhostfds = malloc(dev->max_queue_pairs *
330                                                sizeof(int));
331                         dev->tapfds = malloc(dev->max_queue_pairs *
332                                              sizeof(int));
333                         if (!dev->vhostfds || !dev->tapfds) {
334                                 PMD_INIT_LOG(ERR, "Failed to malloc");
335                                 return -1;
336                         }
337
338                         for (q = 0; q < dev->max_queue_pairs; ++q) {
339                                 dev->vhostfds[q] = -1;
340                                 dev->tapfds[q] = -1;
341                         }
342                 }
343         }
344
345         if (dev->ops->setup(dev) < 0)
346                 return -1;
347
348         if (virtio_user_dev_init_notify(dev) < 0)
349                 return -1;
350
351         if (virtio_user_fill_intr_handle(dev) < 0)
352                 return -1;
353
354         return 0;
355 }
356
357 /* Use below macro to filter features from vhost backend */
358 #define VIRTIO_USER_SUPPORTED_FEATURES                  \
359         (1ULL << VIRTIO_NET_F_MAC               |       \
360          1ULL << VIRTIO_NET_F_STATUS            |       \
361          1ULL << VIRTIO_NET_F_MQ                |       \
362          1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR     |       \
363          1ULL << VIRTIO_NET_F_CTRL_VQ           |       \
364          1ULL << VIRTIO_NET_F_CTRL_RX           |       \
365          1ULL << VIRTIO_NET_F_CTRL_VLAN         |       \
366          1ULL << VIRTIO_NET_F_CSUM              |       \
367          1ULL << VIRTIO_NET_F_HOST_TSO4         |       \
368          1ULL << VIRTIO_NET_F_HOST_TSO6         |       \
369          1ULL << VIRTIO_NET_F_MRG_RXBUF         |       \
370          1ULL << VIRTIO_RING_F_INDIRECT_DESC    |       \
371          1ULL << VIRTIO_NET_F_GUEST_CSUM        |       \
372          1ULL << VIRTIO_NET_F_GUEST_TSO4        |       \
373          1ULL << VIRTIO_NET_F_GUEST_TSO6        |       \
374          1ULL << VIRTIO_F_VERSION_1)
375
376 int
377 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
378                      int cq, int queue_size, const char *mac, char **ifname)
379 {
380         pthread_mutex_init(&dev->mutex, NULL);
381         snprintf(dev->path, PATH_MAX, "%s", path);
382         dev->started = 0;
383         dev->max_queue_pairs = queues;
384         dev->queue_pairs = 1; /* mq disabled by default */
385         dev->queue_size = queue_size;
386         dev->mac_specified = 0;
387         parse_mac(dev, mac);
388
389         if (*ifname) {
390                 dev->ifname = *ifname;
391                 *ifname = NULL;
392         }
393
394         if (virtio_user_dev_setup(dev) < 0) {
395                 PMD_INIT_LOG(ERR, "backend set up fails");
396                 return -1;
397         }
398
399         if (dev->vhostfd >= 0) {
400                 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
401                                            NULL) < 0) {
402                         PMD_INIT_LOG(ERR, "set_owner fails: %s",
403                                      strerror(errno));
404                         return -1;
405                 }
406
407                 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
408                                            &dev->device_features) < 0) {
409                         PMD_INIT_LOG(ERR, "get_features failed: %s",
410                                      strerror(errno));
411                         return -1;
412                 }
413         } else {
414                 /* We just pretend vhost-user can support all these features.
415                  * Note that this could be problematic that if some feature is
416                  * negotiated but not supported by the vhost-user which comes
417                  * later.
418                  */
419                 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
420         }
421
422         if (dev->mac_specified)
423                 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
424
425         if (cq) {
426                 /* device does not really need to know anything about CQ,
427                  * so if necessary, we just claim to support CQ
428                  */
429                 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
430         } else {
431                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
432                 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
433                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
434                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
435                 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
436                 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
437                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
438         }
439
440         /* The backend will not report this feature, we add it explicitly */
441         if (is_vhost_user_by_type(dev->path))
442                 dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
443
444         dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
445
446         if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
447                                 virtio_user_mem_event_cb, dev)) {
448                 PMD_INIT_LOG(ERR, "Failed to register mem event callback\n");
449                 return -1;
450         }
451
452         return 0;
453 }
454
455 void
456 virtio_user_dev_uninit(struct virtio_user_dev *dev)
457 {
458         uint32_t i;
459
460         virtio_user_stop_device(dev);
461
462         rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
463
464         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
465                 close(dev->callfds[i]);
466                 close(dev->kickfds[i]);
467         }
468
469         close(dev->vhostfd);
470
471         if (dev->is_server && dev->listenfd >= 0) {
472                 close(dev->listenfd);
473                 dev->listenfd = -1;
474         }
475
476         if (dev->vhostfds) {
477                 for (i = 0; i < dev->max_queue_pairs; ++i)
478                         close(dev->vhostfds[i]);
479                 free(dev->vhostfds);
480                 free(dev->tapfds);
481         }
482
483         free(dev->ifname);
484
485         if (dev->is_server)
486                 unlink(dev->path);
487 }
488
489 uint8_t
490 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
491 {
492         uint16_t i;
493         uint8_t ret = 0;
494
495         if (q_pairs > dev->max_queue_pairs) {
496                 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
497                              q_pairs, dev->max_queue_pairs);
498                 return -1;
499         }
500
501         /* Server mode can't enable queue pairs if vhostfd is invalid,
502          * always return 0 in this case.
503          */
504         if (dev->vhostfd >= 0) {
505                 for (i = 0; i < q_pairs; ++i)
506                         ret |= dev->ops->enable_qp(dev, i, 1);
507                 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
508                         ret |= dev->ops->enable_qp(dev, i, 0);
509         } else if (!dev->is_server) {
510                 ret = ~0;
511         }
512         dev->queue_pairs = q_pairs;
513
514         return ret;
515 }
516
517 static uint32_t
518 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
519                             uint16_t idx_hdr)
520 {
521         struct virtio_net_ctrl_hdr *hdr;
522         virtio_net_ctrl_ack status = ~0;
523         uint16_t i, idx_data, idx_status;
524         uint32_t n_descs = 0;
525
526         /* locate desc for header, data, and status */
527         idx_data = vring->desc[idx_hdr].next;
528         n_descs++;
529
530         i = idx_data;
531         while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
532                 i = vring->desc[i].next;
533                 n_descs++;
534         }
535
536         /* locate desc for status */
537         idx_status = i;
538         n_descs++;
539
540         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
541         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
542             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
543                 uint16_t queues;
544
545                 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
546                 status = virtio_user_handle_mq(dev, queues);
547         }
548
549         /* Update status */
550         *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
551
552         return n_descs;
553 }
554
555 void
556 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
557 {
558         uint16_t avail_idx, desc_idx;
559         struct vring_used_elem *uep;
560         uint32_t n_descs;
561         struct vring *vring = &dev->vrings[queue_idx];
562
563         /* Consume avail ring, using used ring idx as first one */
564         while (vring->used->idx != vring->avail->idx) {
565                 avail_idx = (vring->used->idx) & (vring->num - 1);
566                 desc_idx = vring->avail->ring[avail_idx];
567
568                 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
569
570                 /* Update used ring */
571                 uep = &vring->used->ring[avail_idx];
572                 uep->id = avail_idx;
573                 uep->len = n_descs;
574
575                 vring->used->idx++;
576         }
577 }