net/virtio: add missing backend features negotiation
[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->send_request(dev, VHOST_USER_SET_VRING_CALL, &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->send_request(dev, VHOST_USER_SET_VRING_NUM, &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->send_request(dev, VHOST_USER_SET_VRING_BASE, &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->send_request(dev, VHOST_USER_SET_VRING_KICK, &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->send_request(dev, VHOST_USER_SET_FEATURES, &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->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
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->send_request(dev, VHOST_USER_GET_VRING_BASE,
222                                            &state) < 0) {
223                         PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u\n",
224                                     i);
225                         error = -1;
226                         goto out;
227                 }
228         }
229
230         dev->started = false;
231 out:
232         pthread_mutex_unlock(&dev->mutex);
233
234         return error;
235 }
236
237 static inline void
238 parse_mac(struct virtio_user_dev *dev, const char *mac)
239 {
240         struct rte_ether_addr tmp;
241
242         if (!mac)
243                 return;
244
245         if (rte_ether_unformat_addr(mac, &tmp) == 0) {
246                 memcpy(dev->mac_addr, &tmp, RTE_ETHER_ADDR_LEN);
247                 dev->mac_specified = 1;
248         } else {
249                 /* ignore the wrong mac, use random mac */
250                 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
251         }
252 }
253
254 static int
255 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
256 {
257         uint32_t i, j;
258         int callfd;
259         int kickfd;
260
261         for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
262                 if (i >= dev->max_queue_pairs * 2) {
263                         dev->kickfds[i] = -1;
264                         dev->callfds[i] = -1;
265                         continue;
266                 }
267
268                 /* May use invalid flag, but some backend uses kickfd and
269                  * callfd as criteria to judge if dev is alive. so finally we
270                  * use real event_fd.
271                  */
272                 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
273                 if (callfd < 0) {
274                         PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
275                         break;
276                 }
277                 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
278                 if (kickfd < 0) {
279                         close(callfd);
280                         PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
281                         break;
282                 }
283                 dev->callfds[i] = callfd;
284                 dev->kickfds[i] = kickfd;
285         }
286
287         if (i < VIRTIO_MAX_VIRTQUEUES) {
288                 for (j = 0; j < i; ++j) {
289                         close(dev->callfds[j]);
290                         close(dev->kickfds[j]);
291                 }
292
293                 return -1;
294         }
295
296         return 0;
297 }
298
299 static int
300 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
301 {
302         uint32_t i;
303         struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
304
305         if (!eth_dev->intr_handle) {
306                 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
307                 if (!eth_dev->intr_handle) {
308                         PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
309                         return -1;
310                 }
311                 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
312         }
313
314         for (i = 0; i < dev->max_queue_pairs; ++i)
315                 eth_dev->intr_handle->efds[i] = dev->callfds[i];
316         eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
317         eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
318         eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
319         /* For virtio vdev, no need to read counter for clean */
320         eth_dev->intr_handle->efd_counter_size = 0;
321         eth_dev->intr_handle->fd = -1;
322         if (dev->vhostfd >= 0)
323                 eth_dev->intr_handle->fd = dev->vhostfd;
324         else if (dev->is_server)
325                 eth_dev->intr_handle->fd = dev->listenfd;
326
327         return 0;
328 }
329
330 static void
331 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
332                          const void *addr,
333                          size_t len __rte_unused,
334                          void *arg)
335 {
336         struct virtio_user_dev *dev = arg;
337         struct rte_memseg_list *msl;
338         uint16_t i;
339
340         /* ignore externally allocated memory */
341         msl = rte_mem_virt2memseg_list(addr);
342         if (msl->external)
343                 return;
344
345         pthread_mutex_lock(&dev->mutex);
346
347         if (dev->started == false)
348                 goto exit;
349
350         /* Step 1: pause the active queues */
351         for (i = 0; i < dev->queue_pairs; i++)
352                 dev->ops->enable_qp(dev, i, 0);
353
354         /* Step 2: update memory regions */
355         dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
356
357         /* Step 3: resume the active queues */
358         for (i = 0; i < dev->queue_pairs; i++)
359                 dev->ops->enable_qp(dev, i, 1);
360
361 exit:
362         pthread_mutex_unlock(&dev->mutex);
363 }
364
365 static int
366 virtio_user_dev_setup(struct virtio_user_dev *dev)
367 {
368         uint32_t q;
369
370         dev->vhostfd = -1;
371         dev->vhostfds = NULL;
372         dev->tapfds = NULL;
373
374         if (dev->is_server) {
375                 if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER) {
376                         PMD_DRV_LOG(ERR, "Server mode only supports vhost-user!");
377                         return -1;
378                 }
379                 dev->ops = &virtio_ops_user;
380         } else {
381                 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) {
382                         dev->ops = &virtio_ops_user;
383                 } else if (dev->backend_type ==
384                                         VIRTIO_USER_BACKEND_VHOST_KERNEL) {
385                         dev->ops = &virtio_ops_kernel;
386
387                         dev->vhostfds = malloc(dev->max_queue_pairs *
388                                                sizeof(int));
389                         dev->tapfds = malloc(dev->max_queue_pairs *
390                                              sizeof(int));
391                         if (!dev->vhostfds || !dev->tapfds) {
392                                 PMD_INIT_LOG(ERR, "Failed to malloc");
393                                 return -1;
394                         }
395
396                         for (q = 0; q < dev->max_queue_pairs; ++q) {
397                                 dev->vhostfds[q] = -1;
398                                 dev->tapfds[q] = -1;
399                         }
400                 } else if (dev->backend_type ==
401                                 VIRTIO_USER_BACKEND_VHOST_VDPA) {
402                         dev->ops = &virtio_ops_vdpa;
403                 } else {
404                         PMD_DRV_LOG(ERR, "Unknown backend type");
405                         return -1;
406                 }
407         }
408
409         if (dev->ops->setup(dev) < 0)
410                 return -1;
411
412         if (virtio_user_dev_init_notify(dev) < 0)
413                 return -1;
414
415         if (virtio_user_fill_intr_handle(dev) < 0)
416                 return -1;
417
418         return 0;
419 }
420
421 /* Use below macro to filter features from vhost backend */
422 #define VIRTIO_USER_SUPPORTED_FEATURES                  \
423         (1ULL << VIRTIO_NET_F_MAC               |       \
424          1ULL << VIRTIO_NET_F_STATUS            |       \
425          1ULL << VIRTIO_NET_F_MQ                |       \
426          1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR     |       \
427          1ULL << VIRTIO_NET_F_CTRL_VQ           |       \
428          1ULL << VIRTIO_NET_F_CTRL_RX           |       \
429          1ULL << VIRTIO_NET_F_CTRL_VLAN         |       \
430          1ULL << VIRTIO_NET_F_CSUM              |       \
431          1ULL << VIRTIO_NET_F_HOST_TSO4         |       \
432          1ULL << VIRTIO_NET_F_HOST_TSO6         |       \
433          1ULL << VIRTIO_NET_F_MRG_RXBUF         |       \
434          1ULL << VIRTIO_RING_F_INDIRECT_DESC    |       \
435          1ULL << VIRTIO_NET_F_GUEST_CSUM        |       \
436          1ULL << VIRTIO_NET_F_GUEST_TSO4        |       \
437          1ULL << VIRTIO_NET_F_GUEST_TSO6        |       \
438          1ULL << VIRTIO_F_IN_ORDER              |       \
439          1ULL << VIRTIO_F_VERSION_1             |       \
440          1ULL << VIRTIO_F_RING_PACKED           |       \
441          1ULL << VHOST_USER_F_PROTOCOL_FEATURES)
442
443 #define VHOST_USER_SUPPORTED_PROTOCOL_FEATURES          \
444         (1ULL << VHOST_USER_PROTOCOL_F_MQ |             \
445          1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK |      \
446          1ULL << VHOST_USER_PROTOCOL_F_STATUS)
447
448 #define VHOST_VDPA_SUPPORTED_PROTOCOL_FEATURES          \
449         (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)
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->send_request(dev, VHOST_USER_SET_OWNER,
493                                            NULL) < 0) {
494                         PMD_INIT_LOG(ERR, "set_owner fails: %s",
495                                      strerror(errno));
496                         return -1;
497                 }
498
499                 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
500                                            &dev->device_features) < 0) {
501                         PMD_INIT_LOG(ERR, "get_features failed: %s",
502                                      strerror(errno));
503                         return -1;
504                 }
505
506
507                 if ((dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
508                                 (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)) {
509                         if (dev->ops->send_request(dev,
510                                         VHOST_USER_GET_PROTOCOL_FEATURES,
511                                         &protocol_features))
512                                 return -1;
513
514                         dev->protocol_features &= protocol_features;
515
516                         if (dev->ops->send_request(dev,
517                                         VHOST_USER_SET_PROTOCOL_FEATURES,
518                                         &dev->protocol_features))
519                                 return -1;
520
521                         if (!(dev->protocol_features &
522                                         (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
523                                 dev->unsupported_features |=
524                                         (1ull << VIRTIO_NET_F_MQ);
525                 }
526         } else {
527                 /* We just pretend vhost-user can support all these features.
528                  * Note that this could be problematic that if some feature is
529                  * negotiated but not supported by the vhost-user which comes
530                  * later.
531                  */
532                 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
533
534                 /* We cannot assume VHOST_USER_PROTOCOL_F_STATUS is supported
535                  * until it's negotiated
536                  */
537                 dev->protocol_features &=
538                         ~(1ULL << VHOST_USER_PROTOCOL_F_STATUS);
539         }
540
541
542
543         if (!mrg_rxbuf)
544                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
545
546         if (!in_order)
547                 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
548
549         if (!packed_vq)
550                 dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);
551
552         if (dev->mac_specified)
553                 dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
554         else
555                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
556
557         if (cq) {
558                 /* device does not really need to know anything about CQ,
559                  * so if necessary, we just claim to support CQ
560                  */
561                 dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
562         } else {
563                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
564                 /* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */
565                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
566                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
567                 dev->unsupported_features |=
568                         (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
569                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
570                 dev->unsupported_features |=
571                         (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
572         }
573
574         /* The backend will not report this feature, we add it explicitly */
575         if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
576                 dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS);
577
578         /*
579          * Device features =
580          *     (frontend_features | backend_features) & ~unsupported_features;
581          */
582         dev->device_features |= dev->frontend_features;
583         dev->device_features &= ~dev->unsupported_features;
584
585         if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
586                                 virtio_user_mem_event_cb, dev)) {
587                 if (rte_errno != ENOTSUP) {
588                         PMD_INIT_LOG(ERR, "Failed to register mem event"
589                                         " callback\n");
590                         return -1;
591                 }
592         }
593
594         return 0;
595 }
596
597 void
598 virtio_user_dev_uninit(struct virtio_user_dev *dev)
599 {
600         uint32_t i;
601
602         virtio_user_stop_device(dev);
603
604         rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
605
606         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
607                 close(dev->callfds[i]);
608                 close(dev->kickfds[i]);
609         }
610
611         if (dev->vhostfd >= 0)
612                 close(dev->vhostfd);
613
614         if (dev->is_server && dev->listenfd >= 0) {
615                 close(dev->listenfd);
616                 dev->listenfd = -1;
617         }
618
619         if (dev->vhostfds) {
620                 for (i = 0; i < dev->max_queue_pairs; ++i) {
621                         close(dev->vhostfds[i]);
622                         if (dev->tapfds[i] >= 0)
623                                 close(dev->tapfds[i]);
624                 }
625                 free(dev->vhostfds);
626                 free(dev->tapfds);
627         }
628
629         free(dev->ifname);
630
631         if (dev->is_server)
632                 unlink(dev->path);
633 }
634
635 uint8_t
636 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
637 {
638         uint16_t i;
639         uint8_t ret = 0;
640
641         if (q_pairs > dev->max_queue_pairs) {
642                 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
643                              q_pairs, dev->max_queue_pairs);
644                 return -1;
645         }
646
647         /* Server mode can't enable queue pairs if vhostfd is invalid,
648          * always return 0 in this case.
649          */
650         if (!dev->is_server || dev->vhostfd >= 0) {
651                 for (i = 0; i < q_pairs; ++i)
652                         ret |= dev->ops->enable_qp(dev, i, 1);
653                 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
654                         ret |= dev->ops->enable_qp(dev, i, 0);
655         }
656         dev->queue_pairs = q_pairs;
657
658         return ret;
659 }
660
661 static uint32_t
662 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
663                             uint16_t idx_hdr)
664 {
665         struct virtio_net_ctrl_hdr *hdr;
666         virtio_net_ctrl_ack status = ~0;
667         uint16_t i, idx_data, idx_status;
668         uint32_t n_descs = 0;
669
670         /* locate desc for header, data, and status */
671         idx_data = vring->desc[idx_hdr].next;
672         n_descs++;
673
674         i = idx_data;
675         while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
676                 i = vring->desc[i].next;
677                 n_descs++;
678         }
679
680         /* locate desc for status */
681         idx_status = i;
682         n_descs++;
683
684         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
685         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
686             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
687                 uint16_t queues;
688
689                 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
690                 status = virtio_user_handle_mq(dev, queues);
691         } else if (hdr->class == VIRTIO_NET_CTRL_RX  ||
692                    hdr->class == VIRTIO_NET_CTRL_MAC ||
693                    hdr->class == VIRTIO_NET_CTRL_VLAN) {
694                 status = 0;
695         }
696
697         /* Update status */
698         *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
699
700         return n_descs;
701 }
702
703 static inline int
704 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
705 {
706         uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
707
708         return wrap_counter == !!(flags & VRING_PACKED_DESC_F_AVAIL) &&
709                 wrap_counter != !!(flags & VRING_PACKED_DESC_F_USED);
710 }
711
712 static uint32_t
713 virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev,
714                                    struct vring_packed *vring,
715                                    uint16_t idx_hdr)
716 {
717         struct virtio_net_ctrl_hdr *hdr;
718         virtio_net_ctrl_ack status = ~0;
719         uint16_t idx_data, idx_status;
720         /* initialize to one, header is first */
721         uint32_t n_descs = 1;
722
723         /* locate desc for header, data, and status */
724         idx_data = idx_hdr + 1;
725         if (idx_data >= dev->queue_size)
726                 idx_data -= dev->queue_size;
727
728         n_descs++;
729
730         idx_status = idx_data;
731         while (vring->desc[idx_status].flags & VRING_DESC_F_NEXT) {
732                 idx_status++;
733                 if (idx_status >= dev->queue_size)
734                         idx_status -= dev->queue_size;
735                 n_descs++;
736         }
737
738         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
739         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
740             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
741                 uint16_t queues;
742
743                 queues = *(uint16_t *)(uintptr_t)
744                                 vring->desc[idx_data].addr;
745                 status = virtio_user_handle_mq(dev, queues);
746         } else if (hdr->class == VIRTIO_NET_CTRL_RX  ||
747                    hdr->class == VIRTIO_NET_CTRL_MAC ||
748                    hdr->class == VIRTIO_NET_CTRL_VLAN) {
749                 status = 0;
750         }
751
752         /* Update status */
753         *(virtio_net_ctrl_ack *)(uintptr_t)
754                 vring->desc[idx_status].addr = status;
755
756         /* Update used descriptor */
757         vring->desc[idx_hdr].id = vring->desc[idx_status].id;
758         vring->desc[idx_hdr].len = sizeof(status);
759
760         return n_descs;
761 }
762
763 void
764 virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx)
765 {
766         struct virtio_user_queue *vq = &dev->packed_queues[queue_idx];
767         struct vring_packed *vring = &dev->packed_vrings[queue_idx];
768         uint16_t n_descs, flags;
769
770         /* Perform a load-acquire barrier in desc_is_avail to
771          * enforce the ordering between desc flags and desc
772          * content.
773          */
774         while (desc_is_avail(&vring->desc[vq->used_idx],
775                              vq->used_wrap_counter)) {
776
777                 n_descs = virtio_user_handle_ctrl_msg_packed(dev, vring,
778                                 vq->used_idx);
779
780                 flags = VRING_DESC_F_WRITE;
781                 if (vq->used_wrap_counter)
782                         flags |= VRING_PACKED_DESC_F_AVAIL_USED;
783
784                 __atomic_store_n(&vring->desc[vq->used_idx].flags, flags,
785                                  __ATOMIC_RELEASE);
786
787                 vq->used_idx += n_descs;
788                 if (vq->used_idx >= dev->queue_size) {
789                         vq->used_idx -= dev->queue_size;
790                         vq->used_wrap_counter ^= 1;
791                 }
792         }
793 }
794
795 void
796 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
797 {
798         uint16_t avail_idx, desc_idx;
799         struct vring_used_elem *uep;
800         uint32_t n_descs;
801         struct vring *vring = &dev->vrings[queue_idx];
802
803         /* Consume avail ring, using used ring idx as first one */
804         while (__atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
805                != vring->avail->idx) {
806                 avail_idx = __atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
807                             & (vring->num - 1);
808                 desc_idx = vring->avail->ring[avail_idx];
809
810                 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
811
812                 /* Update used ring */
813                 uep = &vring->used->ring[avail_idx];
814                 uep->id = desc_idx;
815                 uep->len = n_descs;
816
817                 __atomic_add_fetch(&vring->used->idx, 1, __ATOMIC_RELAXED);
818         }
819 }
820
821 int
822 virtio_user_dev_set_status(struct virtio_user_dev *dev, uint8_t status)
823 {
824         int ret;
825         uint64_t arg = status;
826
827         pthread_mutex_lock(&dev->mutex);
828         dev->status = status;
829         if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
830                 ret = dev->ops->send_request(dev,
831                                 VHOST_USER_SET_STATUS, &arg);
832         else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
833                 ret = dev->ops->send_request(dev,
834                                 VHOST_USER_SET_STATUS, &status);
835         else
836                 ret = -ENOTSUP;
837
838         if (ret && ret != -ENOTSUP) {
839                 PMD_INIT_LOG(ERR, "VHOST_USER_SET_STATUS failed (%d): %s", ret,
840                              strerror(errno));
841         }
842
843         pthread_mutex_unlock(&dev->mutex);
844         return ret;
845 }
846
847 int
848 virtio_user_dev_update_status(struct virtio_user_dev *dev)
849 {
850         uint64_t ret;
851         uint8_t status;
852         int err;
853
854         pthread_mutex_lock(&dev->mutex);
855         if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) {
856                 err = dev->ops->send_request(dev, VHOST_USER_GET_STATUS, &ret);
857                 if (!err && ret > UINT8_MAX) {
858                         PMD_INIT_LOG(ERR, "Invalid VHOST_USER_GET_STATUS "
859                                         "response 0x%" PRIx64 "\n", ret);
860                         err = -1;
861                         goto error;
862                 }
863
864                 status = ret;
865         } else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA) {
866                 err = dev->ops->send_request(dev, VHOST_USER_GET_STATUS,
867                                 &status);
868         } else {
869                 err = -ENOTSUP;
870         }
871
872         if (!err) {
873                 dev->status = status;
874                 PMD_INIT_LOG(DEBUG, "Updated Device Status(0x%08x):\n"
875                         "\t-RESET: %u\n"
876                         "\t-ACKNOWLEDGE: %u\n"
877                         "\t-DRIVER: %u\n"
878                         "\t-DRIVER_OK: %u\n"
879                         "\t-FEATURES_OK: %u\n"
880                         "\t-DEVICE_NEED_RESET: %u\n"
881                         "\t-FAILED: %u\n",
882                         dev->status,
883                         (dev->status == VIRTIO_CONFIG_STATUS_RESET),
884                         !!(dev->status & VIRTIO_CONFIG_STATUS_ACK),
885                         !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER),
886                         !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK),
887                         !!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK),
888                         !!(dev->status & VIRTIO_CONFIG_STATUS_DEV_NEED_RESET),
889                         !!(dev->status & VIRTIO_CONFIG_STATUS_FAILED));
890         } else if (err != -ENOTSUP) {
891                 PMD_INIT_LOG(ERR, "VHOST_USER_GET_STATUS failed (%d): %s", err,
892                              strerror(errno));
893         }
894
895 error:
896         pthread_mutex_unlock(&dev->mutex);
897         return err;
898 }