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