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