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