vhost: add driver on top of the library
[dpdk.git] / drivers / net / vhost / rte_eth_vhost.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 IGEL Co., Ltd.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of IGEL Co.,Ltd. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <unistd.h>
34 #include <pthread.h>
35 #include <stdbool.h>
36 #ifdef RTE_LIBRTE_VHOST_NUMA
37 #include <numaif.h>
38 #endif
39
40 #include <rte_mbuf.h>
41 #include <rte_ethdev.h>
42 #include <rte_malloc.h>
43 #include <rte_memcpy.h>
44 #include <rte_dev.h>
45 #include <rte_kvargs.h>
46 #include <rte_virtio_net.h>
47 #include <rte_spinlock.h>
48
49 #include "rte_eth_vhost.h"
50
51 #define ETH_VHOST_IFACE_ARG             "iface"
52 #define ETH_VHOST_QUEUES_ARG            "queues"
53
54 static const char *drivername = "VHOST PMD";
55
56 static const char *valid_arguments[] = {
57         ETH_VHOST_IFACE_ARG,
58         ETH_VHOST_QUEUES_ARG,
59         NULL
60 };
61
62 static struct ether_addr base_eth_addr = {
63         .addr_bytes = {
64                 0x56 /* V */,
65                 0x48 /* H */,
66                 0x4F /* O */,
67                 0x53 /* S */,
68                 0x54 /* T */,
69                 0x00
70         }
71 };
72
73 struct vhost_queue {
74         rte_atomic32_t allow_queuing;
75         rte_atomic32_t while_queuing;
76         struct virtio_net *device;
77         struct pmd_internal *internal;
78         struct rte_mempool *mb_pool;
79         uint8_t port;
80         uint16_t virtqueue_id;
81         uint64_t rx_pkts;
82         uint64_t tx_pkts;
83         uint64_t missed_pkts;
84         uint64_t rx_bytes;
85         uint64_t tx_bytes;
86 };
87
88 struct pmd_internal {
89         char *dev_name;
90         char *iface_name;
91
92         volatile uint16_t once;
93 };
94
95 struct internal_list {
96         TAILQ_ENTRY(internal_list) next;
97         struct rte_eth_dev *eth_dev;
98 };
99
100 TAILQ_HEAD(internal_list_head, internal_list);
101 static struct internal_list_head internal_list =
102         TAILQ_HEAD_INITIALIZER(internal_list);
103
104 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
105
106 static rte_atomic16_t nb_started_ports;
107 static pthread_t session_th;
108
109 static struct rte_eth_link pmd_link = {
110                 .link_speed = 10000,
111                 .link_duplex = ETH_LINK_FULL_DUPLEX,
112                 .link_status = 0
113 };
114
115 struct rte_vhost_vring_state {
116         rte_spinlock_t lock;
117
118         bool cur[RTE_MAX_QUEUES_PER_PORT * 2];
119         bool seen[RTE_MAX_QUEUES_PER_PORT * 2];
120         unsigned int index;
121         unsigned int max_vring;
122 };
123
124 static struct rte_vhost_vring_state *vring_states[RTE_MAX_ETHPORTS];
125
126 static uint16_t
127 eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
128 {
129         struct vhost_queue *r = q;
130         uint16_t i, nb_rx = 0;
131
132         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
133                 return 0;
134
135         rte_atomic32_set(&r->while_queuing, 1);
136
137         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
138                 goto out;
139
140         /* Dequeue packets from guest TX queue */
141         nb_rx = rte_vhost_dequeue_burst(r->device,
142                         r->virtqueue_id, r->mb_pool, bufs, nb_bufs);
143
144         r->rx_pkts += nb_rx;
145
146         for (i = 0; likely(i < nb_rx); i++) {
147                 bufs[i]->port = r->port;
148                 r->rx_bytes += bufs[i]->pkt_len;
149         }
150
151 out:
152         rte_atomic32_set(&r->while_queuing, 0);
153
154         return nb_rx;
155 }
156
157 static uint16_t
158 eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
159 {
160         struct vhost_queue *r = q;
161         uint16_t i, nb_tx = 0;
162
163         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
164                 return 0;
165
166         rte_atomic32_set(&r->while_queuing, 1);
167
168         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
169                 goto out;
170
171         /* Enqueue packets to guest RX queue */
172         nb_tx = rte_vhost_enqueue_burst(r->device,
173                         r->virtqueue_id, bufs, nb_bufs);
174
175         r->tx_pkts += nb_tx;
176         r->missed_pkts += nb_bufs - nb_tx;
177
178         for (i = 0; likely(i < nb_tx); i++)
179                 r->tx_bytes += bufs[i]->pkt_len;
180
181         for (i = 0; likely(i < nb_tx); i++)
182                 rte_pktmbuf_free(bufs[i]);
183 out:
184         rte_atomic32_set(&r->while_queuing, 0);
185
186         return nb_tx;
187 }
188
189 static int
190 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
191 {
192         return 0;
193 }
194
195 static inline struct internal_list *
196 find_internal_resource(char *ifname)
197 {
198         int found = 0;
199         struct internal_list *list;
200         struct pmd_internal *internal;
201
202         if (ifname == NULL)
203                 return NULL;
204
205         pthread_mutex_lock(&internal_list_lock);
206
207         TAILQ_FOREACH(list, &internal_list, next) {
208                 internal = list->eth_dev->data->dev_private;
209                 if (!strcmp(internal->iface_name, ifname)) {
210                         found = 1;
211                         break;
212                 }
213         }
214
215         pthread_mutex_unlock(&internal_list_lock);
216
217         if (!found)
218                 return NULL;
219
220         return list;
221 }
222
223 static int
224 new_device(struct virtio_net *dev)
225 {
226         struct rte_eth_dev *eth_dev;
227         struct internal_list *list;
228         struct pmd_internal *internal;
229         struct vhost_queue *vq;
230         unsigned i;
231
232         if (dev == NULL) {
233                 RTE_LOG(INFO, PMD, "Invalid argument\n");
234                 return -1;
235         }
236
237         list = find_internal_resource(dev->ifname);
238         if (list == NULL) {
239                 RTE_LOG(INFO, PMD, "Invalid device name\n");
240                 return -1;
241         }
242
243         eth_dev = list->eth_dev;
244         internal = eth_dev->data->dev_private;
245
246         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
247                 vq = eth_dev->data->rx_queues[i];
248                 if (vq == NULL)
249                         continue;
250                 vq->device = dev;
251                 vq->internal = internal;
252                 vq->port = eth_dev->data->port_id;
253                 rte_vhost_enable_guest_notification(dev, vq->virtqueue_id, 0);
254         }
255         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
256                 vq = eth_dev->data->tx_queues[i];
257                 if (vq == NULL)
258                         continue;
259                 vq->device = dev;
260                 vq->internal = internal;
261                 vq->port = eth_dev->data->port_id;
262                 rte_vhost_enable_guest_notification(dev, vq->virtqueue_id, 0);
263         }
264
265         dev->flags |= VIRTIO_DEV_RUNNING;
266         dev->priv = eth_dev;
267         eth_dev->data->dev_link.link_status = 1;
268
269         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
270                 vq = eth_dev->data->rx_queues[i];
271                 if (vq == NULL)
272                         continue;
273                 rte_atomic32_set(&vq->allow_queuing, 1);
274         }
275         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
276                 vq = eth_dev->data->tx_queues[i];
277                 if (vq == NULL)
278                         continue;
279                 rte_atomic32_set(&vq->allow_queuing, 1);
280         }
281
282         RTE_LOG(INFO, PMD, "New connection established\n");
283
284         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC);
285
286         return 0;
287 }
288
289 static void
290 destroy_device(volatile struct virtio_net *dev)
291 {
292         struct rte_eth_dev *eth_dev;
293         struct vhost_queue *vq;
294         unsigned i;
295
296         if (dev == NULL) {
297                 RTE_LOG(INFO, PMD, "Invalid argument\n");
298                 return;
299         }
300
301         eth_dev = (struct rte_eth_dev *)dev->priv;
302         if (eth_dev == NULL) {
303                 RTE_LOG(INFO, PMD, "Failed to find a ethdev\n");
304                 return;
305         }
306
307         /* Wait until rx/tx_pkt_burst stops accessing vhost device */
308         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
309                 vq = eth_dev->data->rx_queues[i];
310                 if (vq == NULL)
311                         continue;
312                 rte_atomic32_set(&vq->allow_queuing, 0);
313                 while (rte_atomic32_read(&vq->while_queuing))
314                         rte_pause();
315         }
316         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
317                 vq = eth_dev->data->tx_queues[i];
318                 if (vq == NULL)
319                         continue;
320                 rte_atomic32_set(&vq->allow_queuing, 0);
321                 while (rte_atomic32_read(&vq->while_queuing))
322                         rte_pause();
323         }
324
325         eth_dev->data->dev_link.link_status = 0;
326
327         dev->priv = NULL;
328         dev->flags &= ~VIRTIO_DEV_RUNNING;
329
330         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
331                 vq = eth_dev->data->rx_queues[i];
332                 if (vq == NULL)
333                         continue;
334                 vq->device = NULL;
335         }
336         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
337                 vq = eth_dev->data->tx_queues[i];
338                 if (vq == NULL)
339                         continue;
340                 vq->device = NULL;
341         }
342
343         RTE_LOG(INFO, PMD, "Connection closed\n");
344
345         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC);
346 }
347
348 static int
349 vring_state_changed(struct virtio_net *dev, uint16_t vring, int enable)
350 {
351         struct rte_vhost_vring_state *state;
352         struct rte_eth_dev *eth_dev;
353         struct internal_list *list;
354 #ifdef RTE_LIBRTE_VHOST_NUMA
355         int newnode, ret;
356 #endif
357
358         if (dev == NULL) {
359                 RTE_LOG(ERR, PMD, "Invalid argument\n");
360                 return -1;
361         }
362
363         list = find_internal_resource(dev->ifname);
364         if (list == NULL) {
365                 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", dev->ifname);
366                 return -1;
367         }
368
369         eth_dev = list->eth_dev;
370         /* won't be NULL */
371         state = vring_states[eth_dev->data->port_id];
372
373 #ifdef RTE_LIBRTE_VHOST_NUMA
374         ret  = get_mempolicy(&newnode, NULL, 0, dev,
375                         MPOL_F_NODE | MPOL_F_ADDR);
376         if (ret < 0) {
377                 RTE_LOG(ERR, PMD, "Unknown numa node\n");
378                 return -1;
379         }
380
381         eth_dev->data->numa_node = newnode;
382 #endif
383         rte_spinlock_lock(&state->lock);
384         state->cur[vring] = enable;
385         state->max_vring = RTE_MAX(vring, state->max_vring);
386         rte_spinlock_unlock(&state->lock);
387
388         RTE_LOG(INFO, PMD, "vring%u is %s\n",
389                         vring, enable ? "enabled" : "disabled");
390
391         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE);
392
393         return 0;
394 }
395
396 int
397 rte_eth_vhost_get_queue_event(uint8_t port_id,
398                 struct rte_eth_vhost_queue_event *event)
399 {
400         struct rte_vhost_vring_state *state;
401         unsigned int i;
402         int idx;
403
404         if (port_id >= RTE_MAX_ETHPORTS) {
405                 RTE_LOG(ERR, PMD, "Invalid port id\n");
406                 return -1;
407         }
408
409         state = vring_states[port_id];
410         if (!state) {
411                 RTE_LOG(ERR, PMD, "Unused port\n");
412                 return -1;
413         }
414
415         rte_spinlock_lock(&state->lock);
416         for (i = 0; i <= state->max_vring; i++) {
417                 idx = state->index++ % (state->max_vring + 1);
418
419                 if (state->cur[idx] != state->seen[idx]) {
420                         state->seen[idx] = state->cur[idx];
421                         event->queue_id = idx / 2;
422                         event->rx = idx & 1;
423                         event->enable = state->cur[idx];
424                         rte_spinlock_unlock(&state->lock);
425                         return 0;
426                 }
427         }
428         rte_spinlock_unlock(&state->lock);
429
430         return -1;
431 }
432
433 static void *
434 vhost_driver_session(void *param __rte_unused)
435 {
436         static struct virtio_net_device_ops vhost_ops;
437
438         /* set vhost arguments */
439         vhost_ops.new_device = new_device;
440         vhost_ops.destroy_device = destroy_device;
441         vhost_ops.vring_state_changed = vring_state_changed;
442         if (rte_vhost_driver_callback_register(&vhost_ops) < 0)
443                 RTE_LOG(ERR, PMD, "Can't register callbacks\n");
444
445         /* start event handling */
446         rte_vhost_driver_session_start();
447
448         return NULL;
449 }
450
451 static int
452 vhost_driver_session_start(void)
453 {
454         int ret;
455
456         ret = pthread_create(&session_th,
457                         NULL, vhost_driver_session, NULL);
458         if (ret)
459                 RTE_LOG(ERR, PMD, "Can't create a thread\n");
460
461         return ret;
462 }
463
464 static void
465 vhost_driver_session_stop(void)
466 {
467         int ret;
468
469         ret = pthread_cancel(session_th);
470         if (ret)
471                 RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
472
473         ret = pthread_join(session_th, NULL);
474         if (ret)
475                 RTE_LOG(ERR, PMD, "Can't join the thread\n");
476 }
477
478 static int
479 eth_dev_start(struct rte_eth_dev *dev)
480 {
481         struct pmd_internal *internal = dev->data->dev_private;
482         int ret = 0;
483
484         if (rte_atomic16_cmpset(&internal->once, 0, 1)) {
485                 ret = rte_vhost_driver_register(internal->iface_name);
486                 if (ret)
487                         return ret;
488         }
489
490         /* We need only one message handling thread */
491         if (rte_atomic16_add_return(&nb_started_ports, 1) == 1)
492                 ret = vhost_driver_session_start();
493
494         return ret;
495 }
496
497 static void
498 eth_dev_stop(struct rte_eth_dev *dev)
499 {
500         struct pmd_internal *internal = dev->data->dev_private;
501
502         if (rte_atomic16_cmpset(&internal->once, 1, 0))
503                 rte_vhost_driver_unregister(internal->iface_name);
504
505         if (rte_atomic16_sub_return(&nb_started_ports, 1) == 0)
506                 vhost_driver_session_stop();
507 }
508
509 static int
510 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
511                    uint16_t nb_rx_desc __rte_unused,
512                    unsigned int socket_id,
513                    const struct rte_eth_rxconf *rx_conf __rte_unused,
514                    struct rte_mempool *mb_pool)
515 {
516         struct vhost_queue *vq;
517
518         vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
519                         RTE_CACHE_LINE_SIZE, socket_id);
520         if (vq == NULL) {
521                 RTE_LOG(ERR, PMD, "Failed to allocate memory for rx queue\n");
522                 return -ENOMEM;
523         }
524
525         vq->mb_pool = mb_pool;
526         vq->virtqueue_id = rx_queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
527         dev->data->rx_queues[rx_queue_id] = vq;
528
529         return 0;
530 }
531
532 static int
533 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
534                    uint16_t nb_tx_desc __rte_unused,
535                    unsigned int socket_id,
536                    const struct rte_eth_txconf *tx_conf __rte_unused)
537 {
538         struct vhost_queue *vq;
539
540         vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
541                         RTE_CACHE_LINE_SIZE, socket_id);
542         if (vq == NULL) {
543                 RTE_LOG(ERR, PMD, "Failed to allocate memory for tx queue\n");
544                 return -ENOMEM;
545         }
546
547         vq->virtqueue_id = tx_queue_id * VIRTIO_QNUM + VIRTIO_RXQ;
548         dev->data->tx_queues[tx_queue_id] = vq;
549
550         return 0;
551 }
552
553 static void
554 eth_dev_info(struct rte_eth_dev *dev,
555              struct rte_eth_dev_info *dev_info)
556 {
557         dev_info->driver_name = drivername;
558         dev_info->max_mac_addrs = 1;
559         dev_info->max_rx_pktlen = (uint32_t)-1;
560         dev_info->max_rx_queues = dev->data->nb_rx_queues;
561         dev_info->max_tx_queues = dev->data->nb_tx_queues;
562         dev_info->min_rx_bufsize = 0;
563 }
564
565 static void
566 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
567 {
568         unsigned i;
569         unsigned long rx_total = 0, tx_total = 0, tx_missed_total = 0;
570         unsigned long rx_total_bytes = 0, tx_total_bytes = 0;
571         struct vhost_queue *vq;
572
573         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
574                         i < dev->data->nb_rx_queues; i++) {
575                 if (dev->data->rx_queues[i] == NULL)
576                         continue;
577                 vq = dev->data->rx_queues[i];
578                 stats->q_ipackets[i] = vq->rx_pkts;
579                 rx_total += stats->q_ipackets[i];
580
581                 stats->q_ibytes[i] = vq->rx_bytes;
582                 rx_total_bytes += stats->q_ibytes[i];
583         }
584
585         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
586                         i < dev->data->nb_tx_queues; i++) {
587                 if (dev->data->tx_queues[i] == NULL)
588                         continue;
589                 vq = dev->data->tx_queues[i];
590                 stats->q_opackets[i] = vq->tx_pkts;
591                 tx_missed_total += vq->missed_pkts;
592                 tx_total += stats->q_opackets[i];
593
594                 stats->q_obytes[i] = vq->tx_bytes;
595                 tx_total_bytes += stats->q_obytes[i];
596         }
597
598         stats->ipackets = rx_total;
599         stats->opackets = tx_total;
600         stats->imissed = tx_missed_total;
601         stats->ibytes = rx_total_bytes;
602         stats->obytes = tx_total_bytes;
603 }
604
605 static void
606 eth_stats_reset(struct rte_eth_dev *dev)
607 {
608         struct vhost_queue *vq;
609         unsigned i;
610
611         for (i = 0; i < dev->data->nb_rx_queues; i++) {
612                 if (dev->data->rx_queues[i] == NULL)
613                         continue;
614                 vq = dev->data->rx_queues[i];
615                 vq->rx_pkts = 0;
616                 vq->rx_bytes = 0;
617         }
618         for (i = 0; i < dev->data->nb_tx_queues; i++) {
619                 if (dev->data->tx_queues[i] == NULL)
620                         continue;
621                 vq = dev->data->tx_queues[i];
622                 vq->tx_pkts = 0;
623                 vq->tx_bytes = 0;
624                 vq->missed_pkts = 0;
625         }
626 }
627
628 static void
629 eth_queue_release(void *q)
630 {
631         rte_free(q);
632 }
633
634 static int
635 eth_link_update(struct rte_eth_dev *dev __rte_unused,
636                 int wait_to_complete __rte_unused)
637 {
638         return 0;
639 }
640
641 /**
642  * Disable features in feature_mask. Returns 0 on success.
643  */
644 int
645 rte_eth_vhost_feature_disable(uint64_t feature_mask)
646 {
647         return rte_vhost_feature_disable(feature_mask);
648 }
649
650 /**
651  * Enable features in feature_mask. Returns 0 on success.
652  */
653 int
654 rte_eth_vhost_feature_enable(uint64_t feature_mask)
655 {
656         return rte_vhost_feature_enable(feature_mask);
657 }
658
659 /* Returns currently supported vhost features */
660 uint64_t
661 rte_eth_vhost_feature_get(void)
662 {
663         return rte_vhost_feature_get();
664 }
665
666 static const struct eth_dev_ops ops = {
667         .dev_start = eth_dev_start,
668         .dev_stop = eth_dev_stop,
669         .dev_configure = eth_dev_configure,
670         .dev_infos_get = eth_dev_info,
671         .rx_queue_setup = eth_rx_queue_setup,
672         .tx_queue_setup = eth_tx_queue_setup,
673         .rx_queue_release = eth_queue_release,
674         .tx_queue_release = eth_queue_release,
675         .link_update = eth_link_update,
676         .stats_get = eth_stats_get,
677         .stats_reset = eth_stats_reset,
678 };
679
680 static int
681 eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
682                      const unsigned numa_node)
683 {
684         struct rte_eth_dev_data *data = NULL;
685         struct pmd_internal *internal = NULL;
686         struct rte_eth_dev *eth_dev = NULL;
687         struct ether_addr *eth_addr = NULL;
688         struct rte_vhost_vring_state *vring_state = NULL;
689         struct internal_list *list = NULL;
690
691         RTE_LOG(INFO, PMD, "Creating VHOST-USER backend on numa socket %u\n",
692                 numa_node);
693
694         /* now do all data allocation - for eth_dev structure, dummy pci driver
695          * and internal (private) data
696          */
697         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
698         if (data == NULL)
699                 goto error;
700
701         internal = rte_zmalloc_socket(name, sizeof(*internal), 0, numa_node);
702         if (internal == NULL)
703                 goto error;
704
705         list = rte_zmalloc_socket(name, sizeof(*list), 0, numa_node);
706         if (list == NULL)
707                 goto error;
708
709         /* reserve an ethdev entry */
710         eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
711         if (eth_dev == NULL)
712                 goto error;
713
714         eth_addr = rte_zmalloc_socket(name, sizeof(*eth_addr), 0, numa_node);
715         if (eth_addr == NULL)
716                 goto error;
717         *eth_addr = base_eth_addr;
718         eth_addr->addr_bytes[5] = eth_dev->data->port_id;
719
720         vring_state = rte_zmalloc_socket(name,
721                         sizeof(*vring_state), 0, numa_node);
722         if (vring_state == NULL)
723                 goto error;
724
725         TAILQ_INIT(&eth_dev->link_intr_cbs);
726
727         /* now put it all together
728          * - store queue data in internal,
729          * - store numa_node info in ethdev data
730          * - point eth_dev_data to internals
731          * - and point eth_dev structure to new eth_dev_data structure
732          */
733         internal->dev_name = strdup(name);
734         if (internal->dev_name == NULL)
735                 goto error;
736         internal->iface_name = strdup(iface_name);
737         if (internal->iface_name == NULL)
738                 goto error;
739
740         list->eth_dev = eth_dev;
741         pthread_mutex_lock(&internal_list_lock);
742         TAILQ_INSERT_TAIL(&internal_list, list, next);
743         pthread_mutex_unlock(&internal_list_lock);
744
745         rte_spinlock_init(&vring_state->lock);
746         vring_states[eth_dev->data->port_id] = vring_state;
747
748         data->dev_private = internal;
749         data->port_id = eth_dev->data->port_id;
750         memmove(data->name, eth_dev->data->name, sizeof(data->name));
751         data->nb_rx_queues = queues;
752         data->nb_tx_queues = queues;
753         data->dev_link = pmd_link;
754         data->mac_addrs = eth_addr;
755
756         /* We'll replace the 'data' originally allocated by eth_dev. So the
757          * vhost PMD resources won't be shared between multi processes.
758          */
759         eth_dev->data = data;
760         eth_dev->dev_ops = &ops;
761         eth_dev->driver = NULL;
762         data->dev_flags =
763                 RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
764         data->kdrv = RTE_KDRV_NONE;
765         data->drv_name = internal->dev_name;
766         data->numa_node = numa_node;
767
768         /* finally assign rx and tx ops */
769         eth_dev->rx_pkt_burst = eth_vhost_rx;
770         eth_dev->tx_pkt_burst = eth_vhost_tx;
771
772         return data->port_id;
773
774 error:
775         if (internal)
776                 free(internal->dev_name);
777         rte_free(vring_state);
778         rte_free(eth_addr);
779         if (eth_dev)
780                 rte_eth_dev_release_port(eth_dev);
781         rte_free(internal);
782         rte_free(list);
783         rte_free(data);
784
785         return -1;
786 }
787
788 static inline int
789 open_iface(const char *key __rte_unused, const char *value, void *extra_args)
790 {
791         const char **iface_name = extra_args;
792
793         if (value == NULL)
794                 return -1;
795
796         *iface_name = value;
797
798         return 0;
799 }
800
801 static inline int
802 open_queues(const char *key __rte_unused, const char *value, void *extra_args)
803 {
804         uint16_t *q = extra_args;
805
806         if (value == NULL || extra_args == NULL)
807                 return -EINVAL;
808
809         *q = (uint16_t)strtoul(value, NULL, 0);
810         if (*q == USHRT_MAX && errno == ERANGE)
811                 return -1;
812
813         if (*q > RTE_MAX_QUEUES_PER_PORT)
814                 return -1;
815
816         return 0;
817 }
818
819 static int
820 rte_pmd_vhost_devinit(const char *name, const char *params)
821 {
822         struct rte_kvargs *kvlist = NULL;
823         int ret = 0;
824         char *iface_name;
825         uint16_t queues;
826
827         RTE_LOG(INFO, PMD, "Initializing pmd_vhost for %s\n", name);
828
829         kvlist = rte_kvargs_parse(params, valid_arguments);
830         if (kvlist == NULL)
831                 return -1;
832
833         if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
834                 ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
835                                          &open_iface, &iface_name);
836                 if (ret < 0)
837                         goto out_free;
838         } else {
839                 ret = -1;
840                 goto out_free;
841         }
842
843         if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
844                 ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
845                                          &open_queues, &queues);
846                 if (ret < 0)
847                         goto out_free;
848
849         } else
850                 queues = 1;
851
852         eth_dev_vhost_create(name, iface_name, queues, rte_socket_id());
853
854 out_free:
855         rte_kvargs_free(kvlist);
856         return ret;
857 }
858
859 static int
860 rte_pmd_vhost_devuninit(const char *name)
861 {
862         struct rte_eth_dev *eth_dev = NULL;
863         struct pmd_internal *internal;
864         struct internal_list *list;
865         unsigned int i;
866
867         RTE_LOG(INFO, PMD, "Un-Initializing pmd_vhost for %s\n", name);
868
869         /* find an ethdev entry */
870         eth_dev = rte_eth_dev_allocated(name);
871         if (eth_dev == NULL)
872                 return -ENODEV;
873
874         internal = eth_dev->data->dev_private;
875         if (internal == NULL)
876                 return -ENODEV;
877
878         list = find_internal_resource(internal->iface_name);
879         if (list == NULL)
880                 return -ENODEV;
881
882         pthread_mutex_lock(&internal_list_lock);
883         TAILQ_REMOVE(&internal_list, list, next);
884         pthread_mutex_unlock(&internal_list_lock);
885         rte_free(list);
886
887         eth_dev_stop(eth_dev);
888
889         rte_free(vring_states[eth_dev->data->port_id]);
890         vring_states[eth_dev->data->port_id] = NULL;
891
892         free(internal->dev_name);
893         free(internal->iface_name);
894
895         for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
896                 rte_free(eth_dev->data->rx_queues[i]);
897         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
898                 rte_free(eth_dev->data->tx_queues[i]);
899
900         rte_free(eth_dev->data->mac_addrs);
901         rte_free(eth_dev->data);
902         rte_free(internal);
903
904         rte_eth_dev_release_port(eth_dev);
905
906         return 0;
907 }
908
909 static struct rte_driver pmd_vhost_drv = {
910         .name = "eth_vhost",
911         .type = PMD_VDEV,
912         .init = rte_pmd_vhost_devinit,
913         .uninit = rte_pmd_vhost_devuninit,
914 };
915
916 PMD_REGISTER_DRIVER(pmd_vhost_drv);