net/virtio-user: do not assume vhost status feature
[dpdk.git] / drivers / net / virtio / virtio_user_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <linux/major.h>
10 #include <sys/stat.h>
11 #include <sys/sysmacros.h>
12 #include <sys/socket.h>
13
14 #include <rte_malloc.h>
15 #include <rte_kvargs.h>
16 #include <rte_ethdev_vdev.h>
17 #include <rte_bus_vdev.h>
18 #include <rte_alarm.h>
19 #include <rte_cycles.h>
20
21 #include "virtio_ethdev.h"
22 #include "virtio_logs.h"
23 #include "virtio_pci.h"
24 #include "virtqueue.h"
25 #include "virtio_rxtx.h"
26 #include "virtio_user/virtio_user_dev.h"
27 #include "virtio_user/vhost.h"
28
29 #define virtio_user_get_dev(hw) \
30         ((struct virtio_user_dev *)(hw)->virtio_user_dev)
31
32 static void
33 virtio_user_reset_queues_packed(struct rte_eth_dev *dev)
34 {
35         struct virtio_hw *hw = dev->data->dev_private;
36         struct virtnet_rx *rxvq;
37         struct virtnet_tx *txvq;
38         uint16_t i;
39
40         /* Add lock to avoid queue contention. */
41         rte_spinlock_lock(&hw->state_lock);
42         hw->started = 0;
43
44         /*
45          * Waitting for datapath to complete before resetting queues.
46          * 1 ms should be enough for the ongoing Tx/Rx function to finish.
47          */
48         rte_delay_ms(1);
49
50         /* Vring reset for each Tx queue and Rx queue. */
51         for (i = 0; i < dev->data->nb_rx_queues; i++) {
52                 rxvq = dev->data->rx_queues[i];
53                 virtqueue_rxvq_reset_packed(rxvq->vq);
54                 virtio_dev_rx_queue_setup_finish(dev, i);
55         }
56
57         for (i = 0; i < dev->data->nb_tx_queues; i++) {
58                 txvq = dev->data->tx_queues[i];
59                 virtqueue_txvq_reset_packed(txvq->vq);
60         }
61
62         hw->started = 1;
63         rte_spinlock_unlock(&hw->state_lock);
64 }
65
66
67 static int
68 virtio_user_server_reconnect(struct virtio_user_dev *dev)
69 {
70         int ret;
71         int connectfd;
72         struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
73         struct virtio_hw *hw = eth_dev->data->dev_private;
74         uint64_t protocol_features;
75
76         connectfd = accept(dev->listenfd, NULL, NULL);
77         if (connectfd < 0)
78                 return -1;
79
80         dev->vhostfd = connectfd;
81         if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
82                                    &dev->device_features) < 0) {
83                 PMD_INIT_LOG(ERR, "get_features failed: %s",
84                              strerror(errno));
85                 return -1;
86         }
87
88         if (dev->device_features &
89                         (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) {
90                 if (dev->ops->send_request(dev,
91                                         VHOST_USER_GET_PROTOCOL_FEATURES,
92                                         &protocol_features))
93                         return -1;
94
95                 /* Offer VHOST_USER_PROTOCOL_F_STATUS */
96                 dev->protocol_features |=
97                         (1ULL << VHOST_USER_PROTOCOL_F_STATUS);
98                 dev->protocol_features &= protocol_features;
99
100                 if (dev->ops->send_request(dev,
101                                         VHOST_USER_SET_PROTOCOL_FEATURES,
102                                         &dev->protocol_features))
103                         return -1;
104
105                 if (!(dev->protocol_features &
106                                 (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
107                         dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
108         }
109
110         dev->device_features |= dev->frontend_features;
111
112         /* umask vhost-user unsupported features */
113         dev->device_features &= ~(dev->unsupported_features);
114
115         dev->features &= dev->device_features;
116
117         /* For packed ring, resetting queues is required in reconnection. */
118         if (vtpci_packed_queue(hw) &&
119            (vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_DRIVER_OK)) {
120                 PMD_INIT_LOG(NOTICE, "Packets on the fly will be dropped"
121                                 " when packed ring reconnecting.");
122                 virtio_user_reset_queues_packed(eth_dev);
123         }
124
125         ret = virtio_user_start_device(dev);
126         if (ret < 0)
127                 return -1;
128
129         if (dev->queue_pairs > 1) {
130                 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
131                 if (ret != 0) {
132                         PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
133                         return -1;
134                 }
135         }
136         if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
137                 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
138                         PMD_DRV_LOG(ERR, "interrupt disable failed");
139                         return -1;
140                 }
141                 rte_intr_callback_unregister(eth_dev->intr_handle,
142                                              virtio_interrupt_handler,
143                                              eth_dev);
144                 eth_dev->intr_handle->fd = connectfd;
145                 rte_intr_callback_register(eth_dev->intr_handle,
146                                            virtio_interrupt_handler, eth_dev);
147
148                 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
149                         PMD_DRV_LOG(ERR, "interrupt enable failed");
150                         return -1;
151                 }
152         }
153         PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
154         return 0;
155 }
156
157 static void
158 virtio_user_delayed_handler(void *param)
159 {
160         struct virtio_hw *hw = (struct virtio_hw *)param;
161         struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
162         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
163
164         if (rte_intr_disable(eth_dev->intr_handle) < 0) {
165                 PMD_DRV_LOG(ERR, "interrupt disable failed");
166                 return;
167         }
168         rte_intr_callback_unregister(eth_dev->intr_handle,
169                                      virtio_interrupt_handler, eth_dev);
170         if (dev->is_server) {
171                 if (dev->vhostfd >= 0) {
172                         close(dev->vhostfd);
173                         dev->vhostfd = -1;
174                         /* Until the featuers are negotiated again, don't assume
175                          * the backend supports VHOST_USER_PROTOCOL_F_STATUS
176                          */
177                         dev->protocol_features &=
178                                 ~(1ULL << VHOST_USER_PROTOCOL_F_STATUS);
179                 }
180                 eth_dev->intr_handle->fd = dev->listenfd;
181                 rte_intr_callback_register(eth_dev->intr_handle,
182                                            virtio_interrupt_handler, eth_dev);
183                 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
184                         PMD_DRV_LOG(ERR, "interrupt enable failed");
185                         return;
186                 }
187         }
188 }
189
190 static void
191 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
192                      void *dst, int length)
193 {
194         int i;
195         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
196
197         if (offset == offsetof(struct virtio_net_config, mac) &&
198             length == RTE_ETHER_ADDR_LEN) {
199                 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
200                         ((uint8_t *)dst)[i] = dev->mac_addr[i];
201                 return;
202         }
203
204         if (offset == offsetof(struct virtio_net_config, status)) {
205                 char buf[128];
206
207                 if (dev->vhostfd >= 0) {
208                         int r;
209                         int flags;
210
211                         flags = fcntl(dev->vhostfd, F_GETFL);
212                         if (fcntl(dev->vhostfd, F_SETFL,
213                                         flags | O_NONBLOCK) == -1) {
214                                 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
215                                 return;
216                         }
217                         r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
218                         if (r == 0 || (r < 0 && errno != EAGAIN)) {
219                                 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
220                                 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
221                                             hw->port_id);
222
223                                 /* This function could be called in the process
224                                  * of interrupt handling, callback cannot be
225                                  * unregistered here, set an alarm to do it.
226                                  */
227                                 rte_eal_alarm_set(1,
228                                                   virtio_user_delayed_handler,
229                                                   (void *)hw);
230                         } else {
231                                 dev->net_status |= VIRTIO_NET_S_LINK_UP;
232                         }
233                         if (fcntl(dev->vhostfd, F_SETFL,
234                                         flags & ~O_NONBLOCK) == -1) {
235                                 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
236                                 return;
237                         }
238                 } else if (dev->is_server) {
239                         dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
240                         if (virtio_user_server_reconnect(dev) >= 0)
241                                 dev->net_status |= VIRTIO_NET_S_LINK_UP;
242                 }
243
244                 *(uint16_t *)dst = dev->net_status;
245         }
246
247         if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
248                 *(uint16_t *)dst = dev->max_queue_pairs;
249 }
250
251 static void
252 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
253                       const void *src, int length)
254 {
255         int i;
256         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
257
258         if ((offset == offsetof(struct virtio_net_config, mac)) &&
259             (length == RTE_ETHER_ADDR_LEN))
260                 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
261                         dev->mac_addr[i] = ((const uint8_t *)src)[i];
262         else
263                 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
264                             offset, length);
265 }
266
267 static void
268 virtio_user_reset(struct virtio_hw *hw)
269 {
270         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
271
272         if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
273                 virtio_user_stop_device(dev);
274 }
275
276 static void
277 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
278 {
279         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
280         uint8_t old_status = dev->status;
281
282         if (status & VIRTIO_CONFIG_STATUS_FEATURES_OK &&
283                         ~old_status & VIRTIO_CONFIG_STATUS_FEATURES_OK)
284                 virtio_user_dev_set_features(dev);
285         if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
286                 virtio_user_start_device(dev);
287         else if (status == VIRTIO_CONFIG_STATUS_RESET)
288                 virtio_user_reset(hw);
289
290         virtio_user_dev_set_status(dev, status);
291 }
292
293 static uint8_t
294 virtio_user_get_status(struct virtio_hw *hw)
295 {
296         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
297
298         virtio_user_dev_update_status(dev);
299
300         return dev->status;
301 }
302
303 static uint64_t
304 virtio_user_get_features(struct virtio_hw *hw)
305 {
306         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
307
308         /* unmask feature bits defined in vhost user protocol */
309         return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
310 }
311
312 static void
313 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
314 {
315         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
316
317         dev->features = features & dev->device_features;
318 }
319
320 static uint8_t
321 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
322 {
323         /* rxq interrupts and config interrupt are separated in virtio-user,
324          * here we only report config change.
325          */
326         return VIRTIO_PCI_ISR_CONFIG;
327 }
328
329 static uint16_t
330 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
331                     uint16_t vec __rte_unused)
332 {
333         return 0;
334 }
335
336 static uint16_t
337 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
338                           struct virtqueue *vq __rte_unused,
339                           uint16_t vec)
340 {
341         /* pretend we have done that */
342         return vec;
343 }
344
345 /* This function is to get the queue size, aka, number of descs, of a specified
346  * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
347  * max supported queues.
348  */
349 static uint16_t
350 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
351 {
352         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
353
354         /* Currently, each queue has same queue size */
355         return dev->queue_size;
356 }
357
358 static void
359 virtio_user_setup_queue_packed(struct virtqueue *vq,
360                                struct virtio_user_dev *dev)
361 {
362         uint16_t queue_idx = vq->vq_queue_index;
363         struct vring_packed *vring;
364         uint64_t desc_addr;
365         uint64_t avail_addr;
366         uint64_t used_addr;
367         uint16_t i;
368
369         vring  = &dev->packed_vrings[queue_idx];
370         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
371         avail_addr = desc_addr + vq->vq_nentries *
372                 sizeof(struct vring_packed_desc);
373         used_addr = RTE_ALIGN_CEIL(avail_addr +
374                            sizeof(struct vring_packed_desc_event),
375                            VIRTIO_PCI_VRING_ALIGN);
376         vring->num = vq->vq_nentries;
377         vring->desc = (void *)(uintptr_t)desc_addr;
378         vring->driver = (void *)(uintptr_t)avail_addr;
379         vring->device = (void *)(uintptr_t)used_addr;
380         dev->packed_queues[queue_idx].avail_wrap_counter = true;
381         dev->packed_queues[queue_idx].used_wrap_counter = true;
382
383         for (i = 0; i < vring->num; i++)
384                 vring->desc[i].flags = 0;
385 }
386
387 static void
388 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
389 {
390         uint16_t queue_idx = vq->vq_queue_index;
391         uint64_t desc_addr, avail_addr, used_addr;
392
393         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
394         avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
395         used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
396                                                          ring[vq->vq_nentries]),
397                                    VIRTIO_PCI_VRING_ALIGN);
398
399         dev->vrings[queue_idx].num = vq->vq_nentries;
400         dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
401         dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
402         dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
403 }
404
405 static int
406 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
407 {
408         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
409
410         if (vtpci_packed_queue(hw))
411                 virtio_user_setup_queue_packed(vq, dev);
412         else
413                 virtio_user_setup_queue_split(vq, dev);
414
415         return 0;
416 }
417
418 static void
419 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
420 {
421         /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
422          * correspondingly stops the ioeventfds, and reset the status of
423          * the device.
424          * For modern devices, set queue desc, avail, used in PCI bar to 0,
425          * not see any more behavior in QEMU.
426          *
427          * Here we just care about what information to deliver to vhost-user
428          * or vhost-kernel. So we just close ioeventfd for now.
429          */
430         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
431
432         close(dev->callfds[vq->vq_queue_index]);
433         close(dev->kickfds[vq->vq_queue_index]);
434 }
435
436 static void
437 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
438 {
439         uint64_t buf = 1;
440         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
441
442         if (hw->cvq && (hw->cvq->vq == vq)) {
443                 if (vtpci_packed_queue(vq->hw))
444                         virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
445                 else
446                         virtio_user_handle_cq(dev, vq->vq_queue_index);
447                 return;
448         }
449
450         if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
451                 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
452                             strerror(errno));
453 }
454
455 const struct virtio_pci_ops virtio_user_ops = {
456         .read_dev_cfg   = virtio_user_read_dev_config,
457         .write_dev_cfg  = virtio_user_write_dev_config,
458         .get_status     = virtio_user_get_status,
459         .set_status     = virtio_user_set_status,
460         .get_features   = virtio_user_get_features,
461         .set_features   = virtio_user_set_features,
462         .get_isr        = virtio_user_get_isr,
463         .set_config_irq = virtio_user_set_config_irq,
464         .set_queue_irq  = virtio_user_set_queue_irq,
465         .get_queue_num  = virtio_user_get_queue_num,
466         .setup_queue    = virtio_user_setup_queue,
467         .del_queue      = virtio_user_del_queue,
468         .notify_queue   = virtio_user_notify_queue,
469 };
470
471 static const char *valid_args[] = {
472 #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
473         VIRTIO_USER_ARG_QUEUES_NUM,
474 #define VIRTIO_USER_ARG_CQ_NUM         "cq"
475         VIRTIO_USER_ARG_CQ_NUM,
476 #define VIRTIO_USER_ARG_MAC            "mac"
477         VIRTIO_USER_ARG_MAC,
478 #define VIRTIO_USER_ARG_PATH           "path"
479         VIRTIO_USER_ARG_PATH,
480 #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
481         VIRTIO_USER_ARG_QUEUE_SIZE,
482 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
483         VIRTIO_USER_ARG_INTERFACE_NAME,
484 #define VIRTIO_USER_ARG_SERVER_MODE    "server"
485         VIRTIO_USER_ARG_SERVER_MODE,
486 #define VIRTIO_USER_ARG_MRG_RXBUF      "mrg_rxbuf"
487         VIRTIO_USER_ARG_MRG_RXBUF,
488 #define VIRTIO_USER_ARG_IN_ORDER       "in_order"
489         VIRTIO_USER_ARG_IN_ORDER,
490 #define VIRTIO_USER_ARG_PACKED_VQ      "packed_vq"
491         VIRTIO_USER_ARG_PACKED_VQ,
492 #define VIRTIO_USER_ARG_SPEED          "speed"
493         VIRTIO_USER_ARG_SPEED,
494 #define VIRTIO_USER_ARG_VECTORIZED     "vectorized"
495         VIRTIO_USER_ARG_VECTORIZED,
496         NULL
497 };
498
499 #define VIRTIO_USER_DEF_CQ_EN   0
500 #define VIRTIO_USER_DEF_Q_NUM   1
501 #define VIRTIO_USER_DEF_Q_SZ    256
502 #define VIRTIO_USER_DEF_SERVER_MODE     0
503
504 static int
505 get_string_arg(const char *key __rte_unused,
506                const char *value, void *extra_args)
507 {
508         if (!value || !extra_args)
509                 return -EINVAL;
510
511         *(char **)extra_args = strdup(value);
512
513         if (!*(char **)extra_args)
514                 return -ENOMEM;
515
516         return 0;
517 }
518
519 static int
520 get_integer_arg(const char *key __rte_unused,
521                 const char *value, void *extra_args)
522 {
523         uint64_t integer = 0;
524         if (!value || !extra_args)
525                 return -EINVAL;
526         errno = 0;
527         integer = strtoull(value, NULL, 0);
528         /* extra_args keeps default value, it should be replaced
529          * only in case of successful parsing of the 'value' arg
530          */
531         if (errno == 0)
532                 *(uint64_t *)extra_args = integer;
533         return -errno;
534 }
535
536 static uint32_t
537 vdpa_dynamic_major_num(void)
538 {
539         FILE *fp;
540         char *line = NULL;
541         size_t size;
542         char name[11];
543         bool found = false;
544         uint32_t num;
545
546         fp = fopen("/proc/devices", "r");
547         if (fp == NULL) {
548                 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
549                              strerror(errno));
550                 return UNNAMED_MAJOR;
551         }
552
553         while (getline(&line, &size, fp) > 0) {
554                 char *stripped = line + strspn(line, " ");
555                 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
556                     (strncmp(name, "vhost-vdpa", 10) == 0)) {
557                         found = true;
558                         break;
559                 }
560         }
561         fclose(fp);
562         return found ? num : UNNAMED_MAJOR;
563 }
564
565 static enum virtio_user_backend_type
566 virtio_user_backend_type(const char *path)
567 {
568         struct stat sb;
569
570         if (stat(path, &sb) == -1) {
571                 if (errno == ENOENT)
572                         return VIRTIO_USER_BACKEND_VHOST_USER;
573
574                 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path,
575                              strerror(errno));
576                 return VIRTIO_USER_BACKEND_UNKNOWN;
577         }
578
579         if (S_ISSOCK(sb.st_mode)) {
580                 return VIRTIO_USER_BACKEND_VHOST_USER;
581         } else if (S_ISCHR(sb.st_mode)) {
582                 if (major(sb.st_rdev) == MISC_MAJOR)
583                         return VIRTIO_USER_BACKEND_VHOST_KERNEL;
584                 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
585                         return VIRTIO_USER_BACKEND_VHOST_VDPA;
586         }
587         return VIRTIO_USER_BACKEND_UNKNOWN;
588 }
589
590 static struct rte_eth_dev *
591 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
592 {
593         struct rte_eth_dev *eth_dev;
594         struct rte_eth_dev_data *data;
595         struct virtio_hw *hw;
596         struct virtio_user_dev *dev;
597
598         eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
599         if (!eth_dev) {
600                 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
601                 return NULL;
602         }
603
604         data = eth_dev->data;
605         hw = eth_dev->data->dev_private;
606
607         dev = rte_zmalloc(NULL, sizeof(*dev), 0);
608         if (!dev) {
609                 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
610                 rte_eth_dev_release_port(eth_dev);
611                 return NULL;
612         }
613
614         hw->port_id = data->port_id;
615         dev->port_id = data->port_id;
616         virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
617         /*
618          * MSIX is required to enable LSC (see virtio_init_device).
619          * Here just pretend that we support msix.
620          */
621         hw->use_msix = 1;
622         hw->modern   = 0;
623         hw->use_vec_rx = 0;
624         hw->use_vec_tx = 0;
625         hw->use_inorder_rx = 0;
626         hw->use_inorder_tx = 0;
627         hw->virtio_user_dev = dev;
628         return eth_dev;
629 }
630
631 static void
632 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
633 {
634         struct rte_eth_dev_data *data = eth_dev->data;
635         struct virtio_hw *hw = data->dev_private;
636
637         rte_free(hw->virtio_user_dev);
638         rte_eth_dev_release_port(eth_dev);
639 }
640
641 /* Dev initialization routine. Invoked once for each virtio vdev at
642  * EAL init time, see rte_bus_probe().
643  * Returns 0 on success.
644  */
645 static int
646 virtio_user_pmd_probe(struct rte_vdev_device *dev)
647 {
648         struct rte_kvargs *kvlist = NULL;
649         struct rte_eth_dev *eth_dev;
650         struct virtio_hw *hw;
651         enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
652         uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
653         uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
654         uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
655         uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
656         uint64_t mrg_rxbuf = 1;
657         uint64_t in_order = 1;
658         uint64_t packed_vq = 0;
659         uint64_t vectorized = 0;
660         char *path = NULL;
661         char *ifname = NULL;
662         char *mac_addr = NULL;
663         int ret = -1;
664
665         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
666                 const char *name = rte_vdev_device_name(dev);
667                 eth_dev = rte_eth_dev_attach_secondary(name);
668                 if (!eth_dev) {
669                         PMD_INIT_LOG(ERR, "Failed to probe %s", name);
670                         return -1;
671                 }
672
673                 if (eth_virtio_dev_init(eth_dev) < 0) {
674                         PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
675                         rte_eth_dev_release_port(eth_dev);
676                         return -1;
677                 }
678
679                 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
680                 eth_dev->device = &dev->device;
681                 rte_eth_dev_probing_finish(eth_dev);
682                 return 0;
683         }
684
685         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
686         if (!kvlist) {
687                 PMD_INIT_LOG(ERR, "error when parsing param");
688                 goto end;
689         }
690
691         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
692                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
693                                        &get_string_arg, &path) < 0) {
694                         PMD_INIT_LOG(ERR, "error to parse %s",
695                                      VIRTIO_USER_ARG_PATH);
696                         goto end;
697                 }
698         } else {
699                 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
700                              VIRTIO_USER_ARG_PATH);
701                 goto end;
702         }
703
704         backend_type = virtio_user_backend_type(path);
705         if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
706                 PMD_INIT_LOG(ERR,
707                              "unable to determine backend type for path %s",
708                         path);
709                 goto end;
710         }
711         PMD_INIT_LOG(INFO, "Backend type detected: %s",
712                      virtio_user_backend_strings[backend_type]);
713
714         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
715                 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
716                         PMD_INIT_LOG(ERR,
717                                 "arg %s applies only to vhost-kernel backend",
718                                 VIRTIO_USER_ARG_INTERFACE_NAME);
719                         goto end;
720                 }
721
722                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
723                                        &get_string_arg, &ifname) < 0) {
724                         PMD_INIT_LOG(ERR, "error to parse %s",
725                                      VIRTIO_USER_ARG_INTERFACE_NAME);
726                         goto end;
727                 }
728         }
729
730         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
731                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
732                                        &get_string_arg, &mac_addr) < 0) {
733                         PMD_INIT_LOG(ERR, "error to parse %s",
734                                      VIRTIO_USER_ARG_MAC);
735                         goto end;
736                 }
737         }
738
739         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
740                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
741                                        &get_integer_arg, &queue_size) < 0) {
742                         PMD_INIT_LOG(ERR, "error to parse %s",
743                                      VIRTIO_USER_ARG_QUEUE_SIZE);
744                         goto end;
745                 }
746         }
747
748         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
749                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
750                                        &get_integer_arg, &queues) < 0) {
751                         PMD_INIT_LOG(ERR, "error to parse %s",
752                                      VIRTIO_USER_ARG_QUEUES_NUM);
753                         goto end;
754                 }
755         }
756
757         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
758                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
759                                        &get_integer_arg, &server_mode) < 0) {
760                         PMD_INIT_LOG(ERR, "error to parse %s",
761                                      VIRTIO_USER_ARG_SERVER_MODE);
762                         goto end;
763                 }
764         }
765
766         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
767                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
768                                        &get_integer_arg, &cq) < 0) {
769                         PMD_INIT_LOG(ERR, "error to parse %s",
770                                      VIRTIO_USER_ARG_CQ_NUM);
771                         goto end;
772                 }
773         } else if (queues > 1) {
774                 cq = 1;
775         }
776
777         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
778                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
779                                        &get_integer_arg, &packed_vq) < 0) {
780                         PMD_INIT_LOG(ERR, "error to parse %s",
781                                      VIRTIO_USER_ARG_PACKED_VQ);
782                         goto end;
783                 }
784         }
785
786         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
787                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
788                                        &get_integer_arg, &vectorized) < 0) {
789                         PMD_INIT_LOG(ERR, "error to parse %s",
790                                      VIRTIO_USER_ARG_VECTORIZED);
791                         goto end;
792                 }
793         }
794
795         if (queues > 1 && cq == 0) {
796                 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
797                 goto end;
798         }
799
800         if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
801                 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
802                         VIRTIO_USER_ARG_QUEUES_NUM, queues,
803                         VIRTIO_MAX_VIRTQUEUE_PAIRS);
804                 goto end;
805         }
806
807         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
808                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
809                                        &get_integer_arg, &mrg_rxbuf) < 0) {
810                         PMD_INIT_LOG(ERR, "error to parse %s",
811                                      VIRTIO_USER_ARG_MRG_RXBUF);
812                         goto end;
813                 }
814         }
815
816         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
817                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
818                                        &get_integer_arg, &in_order) < 0) {
819                         PMD_INIT_LOG(ERR, "error to parse %s",
820                                      VIRTIO_USER_ARG_IN_ORDER);
821                         goto end;
822                 }
823         }
824
825         eth_dev = virtio_user_eth_dev_alloc(dev);
826         if (!eth_dev) {
827                 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
828                 goto end;
829         }
830
831         hw = eth_dev->data->dev_private;
832         if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
833                          queue_size, mac_addr, &ifname, server_mode,
834                          mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
835                 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
836                 virtio_user_eth_dev_free(eth_dev);
837                 goto end;
838         }
839
840         /* previously called by pci probing for physical dev */
841         if (eth_virtio_dev_init(eth_dev) < 0) {
842                 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
843                 virtio_user_eth_dev_free(eth_dev);
844                 goto end;
845         }
846
847         if (vectorized) {
848                 if (packed_vq) {
849 #if defined(CC_AVX512_SUPPORT)
850                         hw->use_vec_rx = 1;
851                         hw->use_vec_tx = 1;
852 #else
853                         PMD_INIT_LOG(INFO,
854                                 "building environment do not support packed ring vectorized");
855 #endif
856                 } else {
857                         hw->use_vec_rx = 1;
858                 }
859         }
860
861         rte_eth_dev_probing_finish(eth_dev);
862         ret = 0;
863
864 end:
865         if (kvlist)
866                 rte_kvargs_free(kvlist);
867         if (path)
868                 free(path);
869         if (mac_addr)
870                 free(mac_addr);
871         if (ifname)
872                 free(ifname);
873         return ret;
874 }
875
876 static int
877 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
878 {
879         const char *name;
880         struct rte_eth_dev *eth_dev;
881
882         if (!vdev)
883                 return -EINVAL;
884
885         name = rte_vdev_device_name(vdev);
886         PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
887         eth_dev = rte_eth_dev_allocated(name);
888         /* Port has already been released by close. */
889         if (!eth_dev)
890                 return 0;
891
892         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
893                 return rte_eth_dev_release_port(eth_dev);
894
895         /* make sure the device is stopped, queues freed */
896         return rte_eth_dev_close(eth_dev->data->port_id);
897 }
898
899 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
900                 uint64_t iova, size_t len)
901 {
902         const char *name;
903         struct rte_eth_dev *eth_dev;
904         struct virtio_user_dev *dev;
905         struct virtio_hw *hw;
906
907         if (!vdev)
908                 return -EINVAL;
909
910         name = rte_vdev_device_name(vdev);
911         eth_dev = rte_eth_dev_allocated(name);
912         /* Port has already been released by close. */
913         if (!eth_dev)
914                 return 0;
915
916         hw = (struct virtio_hw *)eth_dev->data->dev_private;
917         dev = hw->virtio_user_dev;
918
919         if (dev->ops->dma_map)
920                 return dev->ops->dma_map(dev, addr, iova, len);
921
922         return 0;
923 }
924
925 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
926                 uint64_t iova, size_t len)
927 {
928         const char *name;
929         struct rte_eth_dev *eth_dev;
930         struct virtio_user_dev *dev;
931         struct virtio_hw *hw;
932
933         if (!vdev)
934                 return -EINVAL;
935
936         name = rte_vdev_device_name(vdev);
937         eth_dev = rte_eth_dev_allocated(name);
938         /* Port has already been released by close. */
939         if (!eth_dev)
940                 return 0;
941
942         hw = (struct virtio_hw *)eth_dev->data->dev_private;
943         dev = hw->virtio_user_dev;
944
945         if (dev->ops->dma_unmap)
946                 return dev->ops->dma_unmap(dev, addr, iova, len);
947
948         return 0;
949 }
950
951 static struct rte_vdev_driver virtio_user_driver = {
952         .probe = virtio_user_pmd_probe,
953         .remove = virtio_user_pmd_remove,
954         .dma_map = virtio_user_pmd_dma_map,
955         .dma_unmap = virtio_user_pmd_dma_unmap,
956 };
957
958 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
959 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
960 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
961         "path=<path> "
962         "mac=<mac addr> "
963         "cq=<int> "
964         "queue_size=<int> "
965         "queues=<int> "
966         "iface=<string> "
967         "server=<0|1> "
968         "mrg_rxbuf=<0|1> "
969         "in_order=<0|1> "
970         "packed_vq=<0|1> "
971         "speed=<int> "
972         "vectorized=<0|1>");