net/virtio: add virtio-user vring file ops
[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 <rte_string_fns.h>
17 #include <rte_eal_memconfig.h>
18
19 #include "vhost.h"
20 #include "virtio_user_dev.h"
21 #include "../virtio_ethdev.h"
22
23 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb"
24
25 const char * const virtio_user_backend_strings[] = {
26         [VIRTIO_USER_BACKEND_UNKNOWN] = "VIRTIO_USER_BACKEND_UNKNOWN",
27         [VIRTIO_USER_BACKEND_VHOST_USER] = "VHOST_USER",
28         [VIRTIO_USER_BACKEND_VHOST_KERNEL] = "VHOST_NET",
29         [VIRTIO_USER_BACKEND_VHOST_VDPA] = "VHOST_VDPA",
30 };
31
32 static int
33 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
34 {
35         /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
36          * firstly because vhost depends on this msg to allocate virtqueue
37          * pair.
38          */
39         struct vhost_vring_file file;
40
41         file.index = queue_sel;
42         file.fd = dev->callfds[queue_sel];
43         dev->ops->set_vring_call(dev, &file);
44
45         return 0;
46 }
47
48 static int
49 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
50 {
51         struct vhost_vring_file file;
52         struct vhost_vring_state state;
53         struct vring *vring = &dev->vrings[queue_sel];
54         struct vring_packed *pq_vring = &dev->packed_vrings[queue_sel];
55         struct vhost_vring_addr addr = {
56                 .index = queue_sel,
57                 .log_guest_addr = 0,
58                 .flags = 0, /* disable log */
59         };
60
61         if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) {
62                 addr.desc_user_addr =
63                         (uint64_t)(uintptr_t)pq_vring->desc;
64                 addr.avail_user_addr =
65                         (uint64_t)(uintptr_t)pq_vring->driver;
66                 addr.used_user_addr =
67                         (uint64_t)(uintptr_t)pq_vring->device;
68         } else {
69                 addr.desc_user_addr = (uint64_t)(uintptr_t)vring->desc;
70                 addr.avail_user_addr = (uint64_t)(uintptr_t)vring->avail;
71                 addr.used_user_addr = (uint64_t)(uintptr_t)vring->used;
72         }
73
74         state.index = queue_sel;
75         state.num = vring->num;
76         dev->ops->set_vring_num(dev, &state);
77
78         state.index = queue_sel;
79         state.num = 0; /* no reservation */
80         if (dev->features & (1ULL << VIRTIO_F_RING_PACKED))
81                 state.num |= (1 << 15);
82         dev->ops->set_vring_base(dev, &state);
83
84         dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
85
86         /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
87          * lastly because vhost depends on this msg to judge if
88          * virtio is ready.
89          */
90         file.index = queue_sel;
91         file.fd = dev->kickfds[queue_sel];
92         dev->ops->set_vring_kick(dev, &file);
93
94         return 0;
95 }
96
97 static int
98 virtio_user_queue_setup(struct virtio_user_dev *dev,
99                         int (*fn)(struct virtio_user_dev *, uint32_t))
100 {
101         uint32_t i, queue_sel;
102
103         for (i = 0; i < dev->max_queue_pairs; ++i) {
104                 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
105                 if (fn(dev, queue_sel) < 0) {
106                         PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
107                         return -1;
108                 }
109         }
110         for (i = 0; i < dev->max_queue_pairs; ++i) {
111                 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
112                 if (fn(dev, queue_sel) < 0) {
113                         PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
114                         return -1;
115                 }
116         }
117
118         return 0;
119 }
120
121 int
122 virtio_user_dev_set_features(struct virtio_user_dev *dev)
123 {
124         uint64_t features;
125         int ret = -1;
126
127         pthread_mutex_lock(&dev->mutex);
128
129         if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER &&
130                         dev->vhostfd < 0)
131                 goto error;
132
133         /* Step 0: tell vhost to create queues */
134         if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
135                 goto error;
136
137         features = dev->features;
138
139         /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
140         features &= ~(1ull << VIRTIO_NET_F_MAC);
141         /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
142         features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
143         features &= ~(1ull << VIRTIO_NET_F_STATUS);
144         ret = dev->ops->set_features(dev, features);
145         if (ret < 0)
146                 goto error;
147         PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
148 error:
149         pthread_mutex_unlock(&dev->mutex);
150
151         return ret;
152 }
153
154 int
155 virtio_user_start_device(struct virtio_user_dev *dev)
156 {
157         int ret;
158
159         /*
160          * XXX workaround!
161          *
162          * We need to make sure that the locks will be
163          * taken in the correct order to avoid deadlocks.
164          *
165          * Before releasing this lock, this thread should
166          * not trigger any memory hotplug events.
167          *
168          * This is a temporary workaround, and should be
169          * replaced when we get proper supports from the
170          * memory subsystem in the future.
171          */
172         rte_mcfg_mem_read_lock();
173         pthread_mutex_lock(&dev->mutex);
174
175         if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER &&
176                         dev->vhostfd < 0)
177                 goto error;
178
179         /* Step 2: share memory regions */
180         ret = dev->ops->set_memory_table(dev);
181         if (ret < 0)
182                 goto error;
183
184         /* Step 3: kick queues */
185         if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
186                 goto error;
187
188         /* Step 4: enable queues
189          * we enable the 1st queue pair by default.
190          */
191         dev->ops->enable_qp(dev, 0, 1);
192
193         dev->started = true;
194         pthread_mutex_unlock(&dev->mutex);
195         rte_mcfg_mem_read_unlock();
196
197         return 0;
198 error:
199         pthread_mutex_unlock(&dev->mutex);
200         rte_mcfg_mem_read_unlock();
201         /* TODO: free resource here or caller to check */
202         return -1;
203 }
204
205 int virtio_user_stop_device(struct virtio_user_dev *dev)
206 {
207         struct vhost_vring_state state;
208         uint32_t i;
209         int error = 0;
210
211         pthread_mutex_lock(&dev->mutex);
212         if (!dev->started)
213                 goto out;
214
215         for (i = 0; i < dev->max_queue_pairs; ++i)
216                 dev->ops->enable_qp(dev, i, 0);
217
218         /* Stop the backend. */
219         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
220                 state.index = i;
221                 if (dev->ops->get_vring_base(dev, &state) < 0) {
222                         PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u",
223                                     i);
224                         error = -1;
225                         goto out;
226                 }
227         }
228
229         dev->started = false;
230 out:
231         pthread_mutex_unlock(&dev->mutex);
232
233         return error;
234 }
235
236 static inline void
237 parse_mac(struct virtio_user_dev *dev, const char *mac)
238 {
239         struct rte_ether_addr tmp;
240
241         if (!mac)
242                 return;
243
244         if (rte_ether_unformat_addr(mac, &tmp) == 0) {
245                 memcpy(dev->mac_addr, &tmp, RTE_ETHER_ADDR_LEN);
246                 dev->mac_specified = 1;
247         } else {
248                 /* ignore the wrong mac, use random mac */
249                 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
250         }
251 }
252
253 static int
254 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
255 {
256         uint32_t i, j;
257         int callfd;
258         int kickfd;
259
260         for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
261                 if (i >= dev->max_queue_pairs * 2) {
262                         dev->kickfds[i] = -1;
263                         dev->callfds[i] = -1;
264                         continue;
265                 }
266
267                 /* May use invalid flag, but some backend uses kickfd and
268                  * callfd as criteria to judge if dev is alive. so finally we
269                  * use real event_fd.
270                  */
271                 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
272                 if (callfd < 0) {
273                         PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
274                         break;
275                 }
276                 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
277                 if (kickfd < 0) {
278                         close(callfd);
279                         PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
280                         break;
281                 }
282                 dev->callfds[i] = callfd;
283                 dev->kickfds[i] = kickfd;
284         }
285
286         if (i < VIRTIO_MAX_VIRTQUEUES) {
287                 for (j = 0; j < i; ++j) {
288                         close(dev->callfds[j]);
289                         close(dev->kickfds[j]);
290                 }
291
292                 return -1;
293         }
294
295         return 0;
296 }
297
298 static int
299 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
300 {
301         uint32_t i;
302         struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
303
304         if (!eth_dev->intr_handle) {
305                 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
306                 if (!eth_dev->intr_handle) {
307                         PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
308                         return -1;
309                 }
310                 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
311         }
312
313         for (i = 0; i < dev->max_queue_pairs; ++i)
314                 eth_dev->intr_handle->efds[i] = dev->callfds[i];
315         eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
316         eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
317         eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
318         /* For virtio vdev, no need to read counter for clean */
319         eth_dev->intr_handle->efd_counter_size = 0;
320         eth_dev->intr_handle->fd = -1;
321         if (dev->vhostfd >= 0)
322                 eth_dev->intr_handle->fd = dev->vhostfd;
323         else if (dev->is_server)
324                 eth_dev->intr_handle->fd = dev->listenfd;
325
326         return 0;
327 }
328
329 static void
330 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
331                          const void *addr,
332                          size_t len __rte_unused,
333                          void *arg)
334 {
335         struct virtio_user_dev *dev = arg;
336         struct rte_memseg_list *msl;
337         uint16_t i;
338
339         /* ignore externally allocated memory */
340         msl = rte_mem_virt2memseg_list(addr);
341         if (msl->external)
342                 return;
343
344         pthread_mutex_lock(&dev->mutex);
345
346         if (dev->started == false)
347                 goto exit;
348
349         /* Step 1: pause the active queues */
350         for (i = 0; i < dev->queue_pairs; i++)
351                 dev->ops->enable_qp(dev, i, 0);
352
353         /* Step 2: update memory regions */
354         dev->ops->set_memory_table(dev);
355
356         /* Step 3: resume the active queues */
357         for (i = 0; i < dev->queue_pairs; i++)
358                 dev->ops->enable_qp(dev, i, 1);
359
360 exit:
361         pthread_mutex_unlock(&dev->mutex);
362 }
363
364 static int
365 virtio_user_dev_setup(struct virtio_user_dev *dev)
366 {
367         uint32_t q;
368
369         dev->vhostfd = -1;
370         dev->vhostfds = NULL;
371         dev->tapfds = NULL;
372
373         if (dev->is_server) {
374                 if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER) {
375                         PMD_DRV_LOG(ERR, "Server mode only supports vhost-user!");
376                         return -1;
377                 }
378                 dev->ops = &virtio_ops_user;
379         } else {
380                 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) {
381                         dev->ops = &virtio_ops_user;
382                 } else if (dev->backend_type ==
383                                         VIRTIO_USER_BACKEND_VHOST_KERNEL) {
384                         dev->ops = &virtio_ops_kernel;
385
386                         dev->vhostfds = malloc(dev->max_queue_pairs *
387                                                sizeof(int));
388                         dev->tapfds = malloc(dev->max_queue_pairs *
389                                              sizeof(int));
390                         if (!dev->vhostfds || !dev->tapfds) {
391                                 PMD_INIT_LOG(ERR, "Failed to malloc");
392                                 return -1;
393                         }
394
395                         for (q = 0; q < dev->max_queue_pairs; ++q) {
396                                 dev->vhostfds[q] = -1;
397                                 dev->tapfds[q] = -1;
398                         }
399                 } else if (dev->backend_type ==
400                                 VIRTIO_USER_BACKEND_VHOST_VDPA) {
401                         dev->ops = &virtio_ops_vdpa;
402                 } else {
403                         PMD_DRV_LOG(ERR, "Unknown backend type");
404                         return -1;
405                 }
406         }
407
408         if (dev->ops->setup(dev) < 0)
409                 return -1;
410
411         if (virtio_user_dev_init_notify(dev) < 0)
412                 return -1;
413
414         if (virtio_user_fill_intr_handle(dev) < 0)
415                 return -1;
416
417         return 0;
418 }
419
420 /* Use below macro to filter features from vhost backend */
421 #define VIRTIO_USER_SUPPORTED_FEATURES                  \
422         (1ULL << VIRTIO_NET_F_MAC               |       \
423          1ULL << VIRTIO_NET_F_STATUS            |       \
424          1ULL << VIRTIO_NET_F_MQ                |       \
425          1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR     |       \
426          1ULL << VIRTIO_NET_F_CTRL_VQ           |       \
427          1ULL << VIRTIO_NET_F_CTRL_RX           |       \
428          1ULL << VIRTIO_NET_F_CTRL_VLAN         |       \
429          1ULL << VIRTIO_NET_F_CSUM              |       \
430          1ULL << VIRTIO_NET_F_HOST_TSO4         |       \
431          1ULL << VIRTIO_NET_F_HOST_TSO6         |       \
432          1ULL << VIRTIO_NET_F_MRG_RXBUF         |       \
433          1ULL << VIRTIO_RING_F_INDIRECT_DESC    |       \
434          1ULL << VIRTIO_NET_F_GUEST_CSUM        |       \
435          1ULL << VIRTIO_NET_F_GUEST_TSO4        |       \
436          1ULL << VIRTIO_NET_F_GUEST_TSO6        |       \
437          1ULL << VIRTIO_F_IN_ORDER              |       \
438          1ULL << VIRTIO_F_VERSION_1             |       \
439          1ULL << VIRTIO_F_RING_PACKED           |       \
440          1ULL << VHOST_USER_F_PROTOCOL_FEATURES)
441
442 #define VHOST_USER_SUPPORTED_PROTOCOL_FEATURES          \
443         (1ULL << VHOST_USER_PROTOCOL_F_MQ |             \
444          1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK |      \
445          1ULL << VHOST_USER_PROTOCOL_F_STATUS)
446
447 #define VHOST_VDPA_SUPPORTED_PROTOCOL_FEATURES          \
448         (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2   |       \
449         1ULL << VHOST_BACKEND_F_IOTLB_BATCH)
450 int
451 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
452                      int cq, int queue_size, const char *mac, char **ifname,
453                      int server, int mrg_rxbuf, int in_order, int packed_vq,
454                      enum virtio_user_backend_type backend_type)
455 {
456         uint64_t protocol_features = 0;
457
458         pthread_mutex_init(&dev->mutex, NULL);
459         strlcpy(dev->path, path, PATH_MAX);
460         dev->started = 0;
461         dev->max_queue_pairs = queues;
462         dev->queue_pairs = 1; /* mq disabled by default */
463         dev->queue_size = queue_size;
464         dev->is_server = server;
465         dev->mac_specified = 0;
466         dev->frontend_features = 0;
467         dev->unsupported_features = ~VIRTIO_USER_SUPPORTED_FEATURES;
468         dev->backend_type = backend_type;
469
470         if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
471                 dev->protocol_features = VHOST_USER_SUPPORTED_PROTOCOL_FEATURES;
472         else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
473                 dev->protocol_features = VHOST_VDPA_SUPPORTED_PROTOCOL_FEATURES;
474
475         parse_mac(dev, mac);
476
477         if (*ifname) {
478                 dev->ifname = *ifname;
479                 *ifname = NULL;
480         }
481
482         if (virtio_user_dev_setup(dev) < 0) {
483                 PMD_INIT_LOG(ERR, "backend set up fails");
484                 return -1;
485         }
486
487         if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER)
488                 dev->unsupported_features |=
489                         (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
490
491         if (!dev->is_server) {
492                 if (dev->ops->set_owner(dev) < 0) {
493                         PMD_INIT_LOG(ERR, "set_owner fails: %s",
494                                      strerror(errno));
495                         return -1;
496                 }
497
498                 if (dev->ops->get_features(dev, &dev->device_features) < 0) {
499                         PMD_INIT_LOG(ERR, "get_features failed: %s",
500                                      strerror(errno));
501                         return -1;
502                 }
503
504
505                 if ((dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
506                                 (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)) {
507                         if (dev->ops->get_protocol_features(dev, &protocol_features))
508                                 return -1;
509
510                         dev->protocol_features &= protocol_features;
511
512                         if (dev->ops->set_protocol_features(dev, dev->protocol_features))
513                                 return -1;
514
515                         if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
516                                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
517                 }
518         } else {
519                 /* We just pretend vhost-user can support all these features.
520                  * Note that this could be problematic that if some feature is
521                  * negotiated but not supported by the vhost-user which comes
522                  * later.
523                  */
524                 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
525
526                 /* We cannot assume VHOST_USER_PROTOCOL_F_STATUS is supported
527                  * until it's negotiated
528                  */
529                 dev->protocol_features &=
530                         ~(1ULL << VHOST_USER_PROTOCOL_F_STATUS);
531         }
532
533
534
535         if (!mrg_rxbuf)
536                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
537
538         if (!in_order)
539                 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
540
541         if (!packed_vq)
542                 dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);
543
544         if (dev->mac_specified)
545                 dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
546         else
547                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
548
549         if (cq) {
550                 /* device does not really need to know anything about CQ,
551                  * so if necessary, we just claim to support CQ
552                  */
553                 dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
554         } else {
555                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
556                 /* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */
557                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
558                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
559                 dev->unsupported_features |=
560                         (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
561                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
562                 dev->unsupported_features |=
563                         (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
564         }
565
566         /* The backend will not report this feature, we add it explicitly */
567         if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
568                 dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS);
569
570         /*
571          * Device features =
572          *     (frontend_features | backend_features) & ~unsupported_features;
573          */
574         dev->device_features |= dev->frontend_features;
575         dev->device_features &= ~dev->unsupported_features;
576
577         if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
578                                 virtio_user_mem_event_cb, dev)) {
579                 if (rte_errno != ENOTSUP) {
580                         PMD_INIT_LOG(ERR, "Failed to register mem event"
581                                         " callback\n");
582                         return -1;
583                 }
584         }
585
586         return 0;
587 }
588
589 void
590 virtio_user_dev_uninit(struct virtio_user_dev *dev)
591 {
592         uint32_t i;
593
594         virtio_user_stop_device(dev);
595
596         rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
597
598         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
599                 close(dev->callfds[i]);
600                 close(dev->kickfds[i]);
601         }
602
603         if (dev->vhostfd >= 0)
604                 close(dev->vhostfd);
605
606         if (dev->is_server && dev->listenfd >= 0) {
607                 close(dev->listenfd);
608                 dev->listenfd = -1;
609         }
610
611         if (dev->vhostfds) {
612                 for (i = 0; i < dev->max_queue_pairs; ++i) {
613                         close(dev->vhostfds[i]);
614                         if (dev->tapfds[i] >= 0)
615                                 close(dev->tapfds[i]);
616                 }
617                 free(dev->vhostfds);
618                 free(dev->tapfds);
619         }
620
621         free(dev->ifname);
622
623         if (dev->is_server)
624                 unlink(dev->path);
625 }
626
627 uint8_t
628 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
629 {
630         uint16_t i;
631         uint8_t ret = 0;
632
633         if (q_pairs > dev->max_queue_pairs) {
634                 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
635                              q_pairs, dev->max_queue_pairs);
636                 return -1;
637         }
638
639         /* Server mode can't enable queue pairs if vhostfd is invalid,
640          * always return 0 in this case.
641          */
642         if (!dev->is_server || dev->vhostfd >= 0) {
643                 for (i = 0; i < q_pairs; ++i)
644                         ret |= dev->ops->enable_qp(dev, i, 1);
645                 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
646                         ret |= dev->ops->enable_qp(dev, i, 0);
647         }
648         dev->queue_pairs = q_pairs;
649
650         return ret;
651 }
652
653 static uint32_t
654 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
655                             uint16_t idx_hdr)
656 {
657         struct virtio_net_ctrl_hdr *hdr;
658         virtio_net_ctrl_ack status = ~0;
659         uint16_t i, idx_data, idx_status;
660         uint32_t n_descs = 0;
661
662         /* locate desc for header, data, and status */
663         idx_data = vring->desc[idx_hdr].next;
664         n_descs++;
665
666         i = idx_data;
667         while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
668                 i = vring->desc[i].next;
669                 n_descs++;
670         }
671
672         /* locate desc for status */
673         idx_status = i;
674         n_descs++;
675
676         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
677         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
678             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
679                 uint16_t queues;
680
681                 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
682                 status = virtio_user_handle_mq(dev, queues);
683         } else if (hdr->class == VIRTIO_NET_CTRL_RX  ||
684                    hdr->class == VIRTIO_NET_CTRL_MAC ||
685                    hdr->class == VIRTIO_NET_CTRL_VLAN) {
686                 status = 0;
687         }
688
689         /* Update status */
690         *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
691
692         return n_descs;
693 }
694
695 static inline int
696 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
697 {
698         uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
699
700         return wrap_counter == !!(flags & VRING_PACKED_DESC_F_AVAIL) &&
701                 wrap_counter != !!(flags & VRING_PACKED_DESC_F_USED);
702 }
703
704 static uint32_t
705 virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev,
706                                    struct vring_packed *vring,
707                                    uint16_t idx_hdr)
708 {
709         struct virtio_net_ctrl_hdr *hdr;
710         virtio_net_ctrl_ack status = ~0;
711         uint16_t idx_data, idx_status;
712         /* initialize to one, header is first */
713         uint32_t n_descs = 1;
714
715         /* locate desc for header, data, and status */
716         idx_data = idx_hdr + 1;
717         if (idx_data >= dev->queue_size)
718                 idx_data -= dev->queue_size;
719
720         n_descs++;
721
722         idx_status = idx_data;
723         while (vring->desc[idx_status].flags & VRING_DESC_F_NEXT) {
724                 idx_status++;
725                 if (idx_status >= dev->queue_size)
726                         idx_status -= dev->queue_size;
727                 n_descs++;
728         }
729
730         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
731         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
732             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
733                 uint16_t queues;
734
735                 queues = *(uint16_t *)(uintptr_t)
736                                 vring->desc[idx_data].addr;
737                 status = virtio_user_handle_mq(dev, queues);
738         } else if (hdr->class == VIRTIO_NET_CTRL_RX  ||
739                    hdr->class == VIRTIO_NET_CTRL_MAC ||
740                    hdr->class == VIRTIO_NET_CTRL_VLAN) {
741                 status = 0;
742         }
743
744         /* Update status */
745         *(virtio_net_ctrl_ack *)(uintptr_t)
746                 vring->desc[idx_status].addr = status;
747
748         /* Update used descriptor */
749         vring->desc[idx_hdr].id = vring->desc[idx_status].id;
750         vring->desc[idx_hdr].len = sizeof(status);
751
752         return n_descs;
753 }
754
755 void
756 virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx)
757 {
758         struct virtio_user_queue *vq = &dev->packed_queues[queue_idx];
759         struct vring_packed *vring = &dev->packed_vrings[queue_idx];
760         uint16_t n_descs, flags;
761
762         /* Perform a load-acquire barrier in desc_is_avail to
763          * enforce the ordering between desc flags and desc
764          * content.
765          */
766         while (desc_is_avail(&vring->desc[vq->used_idx],
767                              vq->used_wrap_counter)) {
768
769                 n_descs = virtio_user_handle_ctrl_msg_packed(dev, vring,
770                                 vq->used_idx);
771
772                 flags = VRING_DESC_F_WRITE;
773                 if (vq->used_wrap_counter)
774                         flags |= VRING_PACKED_DESC_F_AVAIL_USED;
775
776                 __atomic_store_n(&vring->desc[vq->used_idx].flags, flags,
777                                  __ATOMIC_RELEASE);
778
779                 vq->used_idx += n_descs;
780                 if (vq->used_idx >= dev->queue_size) {
781                         vq->used_idx -= dev->queue_size;
782                         vq->used_wrap_counter ^= 1;
783                 }
784         }
785 }
786
787 void
788 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
789 {
790         uint16_t avail_idx, desc_idx;
791         struct vring_used_elem *uep;
792         uint32_t n_descs;
793         struct vring *vring = &dev->vrings[queue_idx];
794
795         /* Consume avail ring, using used ring idx as first one */
796         while (__atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
797                != vring->avail->idx) {
798                 avail_idx = __atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
799                             & (vring->num - 1);
800                 desc_idx = vring->avail->ring[avail_idx];
801
802                 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
803
804                 /* Update used ring */
805                 uep = &vring->used->ring[avail_idx];
806                 uep->id = desc_idx;
807                 uep->len = n_descs;
808
809                 __atomic_add_fetch(&vring->used->idx, 1, __ATOMIC_RELAXED);
810         }
811 }
812
813 int
814 virtio_user_dev_set_status(struct virtio_user_dev *dev, uint8_t status)
815 {
816         int ret;
817         uint64_t arg = status;
818
819         pthread_mutex_lock(&dev->mutex);
820         dev->status = status;
821         if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
822                 ret = dev->ops->send_request(dev,
823                                 VHOST_USER_SET_STATUS, &arg);
824         else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
825                 ret = dev->ops->send_request(dev,
826                                 VHOST_USER_SET_STATUS, &status);
827         else
828                 ret = -ENOTSUP;
829
830         if (ret && ret != -ENOTSUP) {
831                 PMD_INIT_LOG(ERR, "VHOST_USER_SET_STATUS failed (%d): %s", ret,
832                              strerror(errno));
833         }
834
835         pthread_mutex_unlock(&dev->mutex);
836         return ret;
837 }
838
839 int
840 virtio_user_dev_update_status(struct virtio_user_dev *dev)
841 {
842         uint64_t ret;
843         uint8_t status;
844         int err;
845
846         pthread_mutex_lock(&dev->mutex);
847         if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) {
848                 err = dev->ops->send_request(dev, VHOST_USER_GET_STATUS, &ret);
849                 if (!err && ret > UINT8_MAX) {
850                         PMD_INIT_LOG(ERR, "Invalid VHOST_USER_GET_STATUS "
851                                         "response 0x%" PRIx64 "\n", ret);
852                         err = -1;
853                         goto error;
854                 }
855
856                 status = ret;
857         } else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA) {
858                 err = dev->ops->send_request(dev, VHOST_USER_GET_STATUS,
859                                 &status);
860         } else {
861                 err = -ENOTSUP;
862         }
863
864         if (!err) {
865                 dev->status = status;
866                 PMD_INIT_LOG(DEBUG, "Updated Device Status(0x%08x):\n"
867                         "\t-RESET: %u\n"
868                         "\t-ACKNOWLEDGE: %u\n"
869                         "\t-DRIVER: %u\n"
870                         "\t-DRIVER_OK: %u\n"
871                         "\t-FEATURES_OK: %u\n"
872                         "\t-DEVICE_NEED_RESET: %u\n"
873                         "\t-FAILED: %u\n",
874                         dev->status,
875                         (dev->status == VIRTIO_CONFIG_STATUS_RESET),
876                         !!(dev->status & VIRTIO_CONFIG_STATUS_ACK),
877                         !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER),
878                         !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK),
879                         !!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK),
880                         !!(dev->status & VIRTIO_CONFIG_STATUS_DEV_NEED_RESET),
881                         !!(dev->status & VIRTIO_CONFIG_STATUS_FAILED));
882         } else if (err != -ENOTSUP) {
883                 PMD_INIT_LOG(ERR, "VHOST_USER_GET_STATUS failed (%d): %s", err,
884                              strerror(errno));
885         }
886
887 error:
888         pthread_mutex_unlock(&dev->mutex);
889         return err;
890 }