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