net/virtio: add device config support to vDPA
[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 <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.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(hwp) container_of(hwp, struct virtio_user_dev, hw)
30
31 static void
32 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
33                      void *dst, int length)
34 {
35         int i;
36         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
37
38         if (offset == offsetof(struct virtio_net_config, mac) &&
39             length == RTE_ETHER_ADDR_LEN) {
40                 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
41                         ((uint8_t *)dst)[i] = dev->mac_addr[i];
42                 return;
43         }
44
45         if (offset == offsetof(struct virtio_net_config, status)) {
46                 virtio_user_dev_update_link_state(dev);
47
48                 *(uint16_t *)dst = dev->net_status;
49         }
50
51         if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
52                 *(uint16_t *)dst = dev->max_queue_pairs;
53 }
54
55 static void
56 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
57                       const void *src, int length)
58 {
59         int i;
60         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
61
62         if ((offset == offsetof(struct virtio_net_config, mac)) &&
63             (length == RTE_ETHER_ADDR_LEN))
64                 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
65                         dev->mac_addr[i] = ((const uint8_t *)src)[i];
66         else
67                 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
68                             offset, length);
69 }
70
71 static void
72 virtio_user_reset(struct virtio_hw *hw)
73 {
74         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
75
76         if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
77                 virtio_user_stop_device(dev);
78 }
79
80 static void
81 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
82 {
83         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
84         uint8_t old_status = dev->status;
85
86         if (status & VIRTIO_CONFIG_STATUS_FEATURES_OK &&
87                         ~old_status & VIRTIO_CONFIG_STATUS_FEATURES_OK)
88                 virtio_user_dev_set_features(dev);
89         if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
90                 virtio_user_start_device(dev);
91         else if (status == VIRTIO_CONFIG_STATUS_RESET)
92                 virtio_user_reset(hw);
93
94         virtio_user_dev_set_status(dev, status);
95 }
96
97 static uint8_t
98 virtio_user_get_status(struct virtio_hw *hw)
99 {
100         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
101
102         virtio_user_dev_update_status(dev);
103
104         return dev->status;
105 }
106
107 static uint64_t
108 virtio_user_get_features(struct virtio_hw *hw)
109 {
110         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
111
112         /* unmask feature bits defined in vhost user protocol */
113         return (dev->device_features | dev->frontend_features) &
114                 VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
115 }
116
117 static void
118 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
119 {
120         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
121
122         dev->features = features & (dev->device_features | dev->frontend_features);
123 }
124
125 static int
126 virtio_user_features_ok(struct virtio_hw *hw __rte_unused)
127 {
128         return 0;
129 }
130
131 static uint8_t
132 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
133 {
134         /* rxq interrupts and config interrupt are separated in virtio-user,
135          * here we only report config change.
136          */
137         return VIRTIO_ISR_CONFIG;
138 }
139
140 static uint16_t
141 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
142                     uint16_t vec __rte_unused)
143 {
144         return 0;
145 }
146
147 static uint16_t
148 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
149                           struct virtqueue *vq __rte_unused,
150                           uint16_t vec)
151 {
152         /* pretend we have done that */
153         return vec;
154 }
155
156 /* This function is to get the queue size, aka, number of descs, of a specified
157  * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
158  * max supported queues.
159  */
160 static uint16_t
161 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
162 {
163         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
164
165         /* Currently, each queue has same queue size */
166         return dev->queue_size;
167 }
168
169 static void
170 virtio_user_setup_queue_packed(struct virtqueue *vq,
171                                struct virtio_user_dev *dev)
172 {
173         uint16_t queue_idx = vq->vq_queue_index;
174         struct vring_packed *vring;
175         uint64_t desc_addr;
176         uint64_t avail_addr;
177         uint64_t used_addr;
178         uint16_t i;
179
180         vring  = &dev->packed_vrings[queue_idx];
181         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
182         avail_addr = desc_addr + vq->vq_nentries *
183                 sizeof(struct vring_packed_desc);
184         used_addr = RTE_ALIGN_CEIL(avail_addr +
185                            sizeof(struct vring_packed_desc_event),
186                            VIRTIO_VRING_ALIGN);
187         vring->num = vq->vq_nentries;
188         vring->desc = (void *)(uintptr_t)desc_addr;
189         vring->driver = (void *)(uintptr_t)avail_addr;
190         vring->device = (void *)(uintptr_t)used_addr;
191         dev->packed_queues[queue_idx].avail_wrap_counter = true;
192         dev->packed_queues[queue_idx].used_wrap_counter = true;
193
194         for (i = 0; i < vring->num; i++)
195                 vring->desc[i].flags = 0;
196 }
197
198 static void
199 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
200 {
201         uint16_t queue_idx = vq->vq_queue_index;
202         uint64_t desc_addr, avail_addr, used_addr;
203
204         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
205         avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
206         used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
207                                                          ring[vq->vq_nentries]),
208                                    VIRTIO_VRING_ALIGN);
209
210         dev->vrings[queue_idx].num = vq->vq_nentries;
211         dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
212         dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
213         dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
214 }
215
216 static int
217 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
218 {
219         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
220
221         if (virtio_with_packed_queue(hw))
222                 virtio_user_setup_queue_packed(vq, dev);
223         else
224                 virtio_user_setup_queue_split(vq, dev);
225
226         return 0;
227 }
228
229 static void
230 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
231 {
232         /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
233          * correspondingly stops the ioeventfds, and reset the status of
234          * the device.
235          * For modern devices, set queue desc, avail, used in PCI bar to 0,
236          * not see any more behavior in QEMU.
237          *
238          * Here we just care about what information to deliver to vhost-user
239          * or vhost-kernel. So we just close ioeventfd for now.
240          */
241         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
242
243         close(dev->callfds[vq->vq_queue_index]);
244         close(dev->kickfds[vq->vq_queue_index]);
245 }
246
247 static void
248 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
249 {
250         uint64_t buf = 1;
251         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
252
253         if (hw->cvq && (virtnet_cq_to_vq(hw->cvq) == vq)) {
254                 if (virtio_with_packed_queue(vq->hw))
255                         virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
256                 else
257                         virtio_user_handle_cq(dev, vq->vq_queue_index);
258                 return;
259         }
260
261         if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
262                 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
263                             strerror(errno));
264 }
265
266 static int
267 virtio_user_dev_close(struct virtio_hw *hw)
268 {
269         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
270
271         virtio_user_dev_uninit(dev);
272
273         return 0;
274 }
275
276 const struct virtio_ops virtio_user_ops = {
277         .read_dev_cfg   = virtio_user_read_dev_config,
278         .write_dev_cfg  = virtio_user_write_dev_config,
279         .get_status     = virtio_user_get_status,
280         .set_status     = virtio_user_set_status,
281         .get_features   = virtio_user_get_features,
282         .set_features   = virtio_user_set_features,
283         .features_ok    = virtio_user_features_ok,
284         .get_isr        = virtio_user_get_isr,
285         .set_config_irq = virtio_user_set_config_irq,
286         .set_queue_irq  = virtio_user_set_queue_irq,
287         .get_queue_num  = virtio_user_get_queue_num,
288         .setup_queue    = virtio_user_setup_queue,
289         .del_queue      = virtio_user_del_queue,
290         .notify_queue   = virtio_user_notify_queue,
291         .dev_close      = virtio_user_dev_close,
292 };
293
294 static const char *valid_args[] = {
295 #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
296         VIRTIO_USER_ARG_QUEUES_NUM,
297 #define VIRTIO_USER_ARG_CQ_NUM         "cq"
298         VIRTIO_USER_ARG_CQ_NUM,
299 #define VIRTIO_USER_ARG_MAC            "mac"
300         VIRTIO_USER_ARG_MAC,
301 #define VIRTIO_USER_ARG_PATH           "path"
302         VIRTIO_USER_ARG_PATH,
303 #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
304         VIRTIO_USER_ARG_QUEUE_SIZE,
305 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
306         VIRTIO_USER_ARG_INTERFACE_NAME,
307 #define VIRTIO_USER_ARG_SERVER_MODE    "server"
308         VIRTIO_USER_ARG_SERVER_MODE,
309 #define VIRTIO_USER_ARG_MRG_RXBUF      "mrg_rxbuf"
310         VIRTIO_USER_ARG_MRG_RXBUF,
311 #define VIRTIO_USER_ARG_IN_ORDER       "in_order"
312         VIRTIO_USER_ARG_IN_ORDER,
313 #define VIRTIO_USER_ARG_PACKED_VQ      "packed_vq"
314         VIRTIO_USER_ARG_PACKED_VQ,
315 #define VIRTIO_USER_ARG_SPEED          "speed"
316         VIRTIO_USER_ARG_SPEED,
317 #define VIRTIO_USER_ARG_VECTORIZED     "vectorized"
318         VIRTIO_USER_ARG_VECTORIZED,
319         NULL
320 };
321
322 #define VIRTIO_USER_DEF_CQ_EN   0
323 #define VIRTIO_USER_DEF_Q_NUM   1
324 #define VIRTIO_USER_DEF_Q_SZ    256
325 #define VIRTIO_USER_DEF_SERVER_MODE     0
326
327 static int
328 get_string_arg(const char *key __rte_unused,
329                const char *value, void *extra_args)
330 {
331         if (!value || !extra_args)
332                 return -EINVAL;
333
334         *(char **)extra_args = strdup(value);
335
336         if (!*(char **)extra_args)
337                 return -ENOMEM;
338
339         return 0;
340 }
341
342 static int
343 get_integer_arg(const char *key __rte_unused,
344                 const char *value, void *extra_args)
345 {
346         uint64_t integer = 0;
347         if (!value || !extra_args)
348                 return -EINVAL;
349         errno = 0;
350         integer = strtoull(value, NULL, 0);
351         /* extra_args keeps default value, it should be replaced
352          * only in case of successful parsing of the 'value' arg
353          */
354         if (errno == 0)
355                 *(uint64_t *)extra_args = integer;
356         return -errno;
357 }
358
359 static uint32_t
360 vdpa_dynamic_major_num(void)
361 {
362         FILE *fp;
363         char *line = NULL;
364         size_t size = 0;
365         char name[11];
366         bool found = false;
367         uint32_t num;
368
369         fp = fopen("/proc/devices", "r");
370         if (fp == NULL) {
371                 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
372                              strerror(errno));
373                 return UNNAMED_MAJOR;
374         }
375
376         while (getline(&line, &size, fp) > 0) {
377                 char *stripped = line + strspn(line, " ");
378                 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
379                     (strncmp(name, "vhost-vdpa", 10) == 0)) {
380                         found = true;
381                         break;
382                 }
383         }
384         free(line);
385         fclose(fp);
386         return found ? num : UNNAMED_MAJOR;
387 }
388
389 static enum virtio_user_backend_type
390 virtio_user_backend_type(const char *path)
391 {
392         struct stat sb;
393
394         if (stat(path, &sb) == -1) {
395                 if (errno == ENOENT)
396                         return VIRTIO_USER_BACKEND_VHOST_USER;
397
398                 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path,
399                              strerror(errno));
400                 return VIRTIO_USER_BACKEND_UNKNOWN;
401         }
402
403         if (S_ISSOCK(sb.st_mode)) {
404                 return VIRTIO_USER_BACKEND_VHOST_USER;
405         } else if (S_ISCHR(sb.st_mode)) {
406                 if (major(sb.st_rdev) == MISC_MAJOR)
407                         return VIRTIO_USER_BACKEND_VHOST_KERNEL;
408                 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
409                         return VIRTIO_USER_BACKEND_VHOST_VDPA;
410         }
411         return VIRTIO_USER_BACKEND_UNKNOWN;
412 }
413
414 static struct rte_eth_dev *
415 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
416 {
417         struct rte_eth_dev *eth_dev;
418         struct rte_eth_dev_data *data;
419         struct virtio_hw *hw;
420         struct virtio_user_dev *dev;
421
422         eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*dev));
423         if (!eth_dev) {
424                 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
425                 return NULL;
426         }
427
428         data = eth_dev->data;
429         dev = eth_dev->data->dev_private;
430         hw = &dev->hw;
431
432         hw->port_id = data->port_id;
433         VIRTIO_OPS(hw) = &virtio_user_ops;
434
435         hw->intr_lsc = 1;
436         hw->use_vec_rx = 0;
437         hw->use_vec_tx = 0;
438         hw->use_inorder_rx = 0;
439         hw->use_inorder_tx = 0;
440
441         return eth_dev;
442 }
443
444 static void
445 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
446 {
447         rte_eth_dev_release_port(eth_dev);
448 }
449
450 /* Dev initialization routine. Invoked once for each virtio vdev at
451  * EAL init time, see rte_bus_probe().
452  * Returns 0 on success.
453  */
454 static int
455 virtio_user_pmd_probe(struct rte_vdev_device *vdev)
456 {
457         struct rte_kvargs *kvlist = NULL;
458         struct rte_eth_dev *eth_dev;
459         struct virtio_hw *hw;
460         struct virtio_user_dev *dev;
461         enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
462         uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
463         uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
464         uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
465         uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
466         uint64_t mrg_rxbuf = 1;
467         uint64_t in_order = 1;
468         uint64_t packed_vq = 0;
469         uint64_t vectorized = 0;
470         char *path = NULL;
471         char *ifname = NULL;
472         char *mac_addr = NULL;
473         int ret = -1;
474
475         RTE_BUILD_BUG_ON(offsetof(struct virtio_user_dev, hw) != 0);
476
477         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
478                 const char *name = rte_vdev_device_name(vdev);
479                 eth_dev = rte_eth_dev_attach_secondary(name);
480                 if (!eth_dev) {
481                         PMD_INIT_LOG(ERR, "Failed to probe %s", name);
482                         return -1;
483                 }
484
485                 dev = eth_dev->data->dev_private;
486                 hw = &dev->hw;
487                 VIRTIO_OPS(hw) = &virtio_user_ops;
488
489                 if (eth_virtio_dev_init(eth_dev) < 0) {
490                         PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
491                         rte_eth_dev_release_port(eth_dev);
492                         return -1;
493                 }
494
495                 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
496                 eth_dev->device = &vdev->device;
497                 rte_eth_dev_probing_finish(eth_dev);
498                 return 0;
499         }
500
501         kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_args);
502         if (!kvlist) {
503                 PMD_INIT_LOG(ERR, "error when parsing param");
504                 goto end;
505         }
506
507         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
508                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
509                                        &get_string_arg, &path) < 0) {
510                         PMD_INIT_LOG(ERR, "error to parse %s",
511                                      VIRTIO_USER_ARG_PATH);
512                         goto end;
513                 }
514         } else {
515                 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
516                              VIRTIO_USER_ARG_PATH);
517                 goto end;
518         }
519
520         backend_type = virtio_user_backend_type(path);
521         if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
522                 PMD_INIT_LOG(ERR,
523                              "unable to determine backend type for path %s",
524                         path);
525                 goto end;
526         }
527         PMD_INIT_LOG(INFO, "Backend type detected: %s",
528                      virtio_user_backend_strings[backend_type]);
529
530         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
531                 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
532                         PMD_INIT_LOG(ERR,
533                                 "arg %s applies only to vhost-kernel backend",
534                                 VIRTIO_USER_ARG_INTERFACE_NAME);
535                         goto end;
536                 }
537
538                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
539                                        &get_string_arg, &ifname) < 0) {
540                         PMD_INIT_LOG(ERR, "error to parse %s",
541                                      VIRTIO_USER_ARG_INTERFACE_NAME);
542                         goto end;
543                 }
544         }
545
546         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
547                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
548                                        &get_string_arg, &mac_addr) < 0) {
549                         PMD_INIT_LOG(ERR, "error to parse %s",
550                                      VIRTIO_USER_ARG_MAC);
551                         goto end;
552                 }
553         }
554
555         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
556                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
557                                        &get_integer_arg, &queue_size) < 0) {
558                         PMD_INIT_LOG(ERR, "error to parse %s",
559                                      VIRTIO_USER_ARG_QUEUE_SIZE);
560                         goto end;
561                 }
562         }
563
564         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
565                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
566                                        &get_integer_arg, &queues) < 0) {
567                         PMD_INIT_LOG(ERR, "error to parse %s",
568                                      VIRTIO_USER_ARG_QUEUES_NUM);
569                         goto end;
570                 }
571         }
572
573         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
574                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
575                                        &get_integer_arg, &server_mode) < 0) {
576                         PMD_INIT_LOG(ERR, "error to parse %s",
577                                      VIRTIO_USER_ARG_SERVER_MODE);
578                         goto end;
579                 }
580         }
581
582         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
583                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
584                                        &get_integer_arg, &cq) < 0) {
585                         PMD_INIT_LOG(ERR, "error to parse %s",
586                                      VIRTIO_USER_ARG_CQ_NUM);
587                         goto end;
588                 }
589         } else if (queues > 1) {
590                 cq = 1;
591         }
592
593         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
594                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
595                                        &get_integer_arg, &packed_vq) < 0) {
596                         PMD_INIT_LOG(ERR, "error to parse %s",
597                                      VIRTIO_USER_ARG_PACKED_VQ);
598                         goto end;
599                 }
600         }
601
602         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
603                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
604                                        &get_integer_arg, &vectorized) < 0) {
605                         PMD_INIT_LOG(ERR, "error to parse %s",
606                                      VIRTIO_USER_ARG_VECTORIZED);
607                         goto end;
608                 }
609         }
610
611         if (queues > 1 && cq == 0) {
612                 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
613                 goto end;
614         }
615
616         if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
617                 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
618                         VIRTIO_USER_ARG_QUEUES_NUM, queues,
619                         VIRTIO_MAX_VIRTQUEUE_PAIRS);
620                 goto end;
621         }
622
623         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
624                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
625                                        &get_integer_arg, &mrg_rxbuf) < 0) {
626                         PMD_INIT_LOG(ERR, "error to parse %s",
627                                      VIRTIO_USER_ARG_MRG_RXBUF);
628                         goto end;
629                 }
630         }
631
632         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
633                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
634                                        &get_integer_arg, &in_order) < 0) {
635                         PMD_INIT_LOG(ERR, "error to parse %s",
636                                      VIRTIO_USER_ARG_IN_ORDER);
637                         goto end;
638                 }
639         }
640
641         eth_dev = virtio_user_eth_dev_alloc(vdev);
642         if (!eth_dev) {
643                 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
644                 goto end;
645         }
646
647         dev = eth_dev->data->dev_private;
648         hw = &dev->hw;
649         if (virtio_user_dev_init(dev, path, queues, cq,
650                          queue_size, mac_addr, &ifname, server_mode,
651                          mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
652                 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
653                 virtio_user_eth_dev_free(eth_dev);
654                 goto end;
655         }
656
657         /* previously called by pci probing for physical dev */
658         if (eth_virtio_dev_init(eth_dev) < 0) {
659                 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
660                 virtio_user_eth_dev_free(eth_dev);
661                 goto end;
662         }
663
664         if (vectorized) {
665                 if (packed_vq) {
666 #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM)
667                         hw->use_vec_rx = 1;
668                         hw->use_vec_tx = 1;
669 #else
670                         PMD_INIT_LOG(INFO,
671                                 "building environment do not support packed ring vectorized");
672 #endif
673                 } else {
674                         hw->use_vec_rx = 1;
675                 }
676         }
677
678         rte_eth_dev_probing_finish(eth_dev);
679         ret = 0;
680
681 end:
682         if (kvlist)
683                 rte_kvargs_free(kvlist);
684         if (path)
685                 free(path);
686         if (mac_addr)
687                 free(mac_addr);
688         if (ifname)
689                 free(ifname);
690         return ret;
691 }
692
693 static int
694 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
695 {
696         const char *name;
697         struct rte_eth_dev *eth_dev;
698
699         if (!vdev)
700                 return -EINVAL;
701
702         name = rte_vdev_device_name(vdev);
703         PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
704         eth_dev = rte_eth_dev_allocated(name);
705         /* Port has already been released by close. */
706         if (!eth_dev)
707                 return 0;
708
709         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
710                 return rte_eth_dev_release_port(eth_dev);
711
712         /* make sure the device is stopped, queues freed */
713         return rte_eth_dev_close(eth_dev->data->port_id);
714 }
715
716 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
717                 uint64_t iova, size_t len)
718 {
719         const char *name;
720         struct rte_eth_dev *eth_dev;
721         struct virtio_user_dev *dev;
722
723         if (!vdev)
724                 return -EINVAL;
725
726         name = rte_vdev_device_name(vdev);
727         eth_dev = rte_eth_dev_allocated(name);
728         /* Port has already been released by close. */
729         if (!eth_dev)
730                 return 0;
731
732         dev = eth_dev->data->dev_private;
733
734         if (dev->ops->dma_map)
735                 return dev->ops->dma_map(dev, addr, iova, len);
736
737         return 0;
738 }
739
740 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
741                 uint64_t iova, size_t len)
742 {
743         const char *name;
744         struct rte_eth_dev *eth_dev;
745         struct virtio_user_dev *dev;
746
747         if (!vdev)
748                 return -EINVAL;
749
750         name = rte_vdev_device_name(vdev);
751         eth_dev = rte_eth_dev_allocated(name);
752         /* Port has already been released by close. */
753         if (!eth_dev)
754                 return 0;
755
756         dev = eth_dev->data->dev_private;
757
758         if (dev->ops->dma_unmap)
759                 return dev->ops->dma_unmap(dev, addr, iova, len);
760
761         return 0;
762 }
763
764 static struct rte_vdev_driver virtio_user_driver = {
765         .probe = virtio_user_pmd_probe,
766         .remove = virtio_user_pmd_remove,
767         .dma_map = virtio_user_pmd_dma_map,
768         .dma_unmap = virtio_user_pmd_dma_unmap,
769         .drv_flags = RTE_VDEV_DRV_NEED_IOVA_AS_VA,
770 };
771
772 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
773 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
774 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
775         "path=<path> "
776         "mac=<mac addr> "
777         "cq=<int> "
778         "queue_size=<int> "
779         "queues=<int> "
780         "iface=<string> "
781         "server=<0|1> "
782         "mrg_rxbuf=<0|1> "
783         "in_order=<0|1> "
784         "packed_vq=<0|1> "
785         "speed=<int> "
786         "vectorized=<0|1>");