410bfd8b9872736742ed8428c92e7f80d46d97dc
[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_vdev.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 #define ETH_VHOST_CLIENT_ARG            "client"
54 #define ETH_VHOST_DEQUEUE_ZERO_COPY     "dequeue-zero-copy"
55
56 static const char *drivername = "VHOST PMD";
57
58 static const char *valid_arguments[] = {
59         ETH_VHOST_IFACE_ARG,
60         ETH_VHOST_QUEUES_ARG,
61         ETH_VHOST_CLIENT_ARG,
62         ETH_VHOST_DEQUEUE_ZERO_COPY,
63         NULL
64 };
65
66 static struct ether_addr base_eth_addr = {
67         .addr_bytes = {
68                 0x56 /* V */,
69                 0x48 /* H */,
70                 0x4F /* O */,
71                 0x53 /* S */,
72                 0x54 /* T */,
73                 0x00
74         }
75 };
76
77 enum vhost_xstats_pkts {
78         VHOST_UNDERSIZE_PKT = 0,
79         VHOST_64_PKT,
80         VHOST_65_TO_127_PKT,
81         VHOST_128_TO_255_PKT,
82         VHOST_256_TO_511_PKT,
83         VHOST_512_TO_1023_PKT,
84         VHOST_1024_TO_1522_PKT,
85         VHOST_1523_TO_MAX_PKT,
86         VHOST_BROADCAST_PKT,
87         VHOST_MULTICAST_PKT,
88         VHOST_UNICAST_PKT,
89         VHOST_ERRORS_PKT,
90         VHOST_ERRORS_FRAGMENTED,
91         VHOST_ERRORS_JABBER,
92         VHOST_UNKNOWN_PROTOCOL,
93         VHOST_XSTATS_MAX,
94 };
95
96 struct vhost_stats {
97         uint64_t pkts;
98         uint64_t bytes;
99         uint64_t missed_pkts;
100         uint64_t xstats[VHOST_XSTATS_MAX];
101 };
102
103 struct vhost_queue {
104         int vid;
105         rte_atomic32_t allow_queuing;
106         rte_atomic32_t while_queuing;
107         struct pmd_internal *internal;
108         struct rte_mempool *mb_pool;
109         uint8_t port;
110         uint16_t virtqueue_id;
111         struct vhost_stats stats;
112 };
113
114 struct pmd_internal {
115         rte_atomic32_t dev_attached;
116         char *dev_name;
117         char *iface_name;
118         uint16_t max_queues;
119         rte_atomic32_t started;
120 };
121
122 struct internal_list {
123         TAILQ_ENTRY(internal_list) next;
124         struct rte_eth_dev *eth_dev;
125 };
126
127 TAILQ_HEAD(internal_list_head, internal_list);
128 static struct internal_list_head internal_list =
129         TAILQ_HEAD_INITIALIZER(internal_list);
130
131 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
132
133 static rte_atomic16_t nb_started_ports;
134 static pthread_t session_th;
135
136 static struct rte_eth_link pmd_link = {
137                 .link_speed = 10000,
138                 .link_duplex = ETH_LINK_FULL_DUPLEX,
139                 .link_status = ETH_LINK_DOWN
140 };
141
142 struct rte_vhost_vring_state {
143         rte_spinlock_t lock;
144
145         bool cur[RTE_MAX_QUEUES_PER_PORT * 2];
146         bool seen[RTE_MAX_QUEUES_PER_PORT * 2];
147         unsigned int index;
148         unsigned int max_vring;
149 };
150
151 static struct rte_vhost_vring_state *vring_states[RTE_MAX_ETHPORTS];
152
153 #define VHOST_XSTATS_NAME_SIZE 64
154
155 struct vhost_xstats_name_off {
156         char name[VHOST_XSTATS_NAME_SIZE];
157         uint64_t offset;
158 };
159
160 /* [rx]_is prepended to the name string here */
161 static const struct vhost_xstats_name_off vhost_rxport_stat_strings[] = {
162         {"good_packets",
163          offsetof(struct vhost_queue, stats.pkts)},
164         {"total_bytes",
165          offsetof(struct vhost_queue, stats.bytes)},
166         {"missed_pkts",
167          offsetof(struct vhost_queue, stats.missed_pkts)},
168         {"broadcast_packets",
169          offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
170         {"multicast_packets",
171          offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
172         {"unicast_packets",
173          offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
174          {"undersize_packets",
175          offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
176         {"size_64_packets",
177          offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
178         {"size_65_to_127_packets",
179          offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
180         {"size_128_to_255_packets",
181          offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
182         {"size_256_to_511_packets",
183          offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
184         {"size_512_to_1023_packets",
185          offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
186         {"size_1024_to_1522_packets",
187          offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
188         {"size_1523_to_max_packets",
189          offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
190         {"errors_with_bad_CRC",
191          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
192         {"fragmented_errors",
193          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_FRAGMENTED])},
194         {"jabber_errors",
195          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_JABBER])},
196         {"unknown_protos_packets",
197          offsetof(struct vhost_queue, stats.xstats[VHOST_UNKNOWN_PROTOCOL])},
198 };
199
200 /* [tx]_ is prepended to the name string here */
201 static const struct vhost_xstats_name_off vhost_txport_stat_strings[] = {
202         {"good_packets",
203          offsetof(struct vhost_queue, stats.pkts)},
204         {"total_bytes",
205          offsetof(struct vhost_queue, stats.bytes)},
206         {"missed_pkts",
207          offsetof(struct vhost_queue, stats.missed_pkts)},
208         {"broadcast_packets",
209          offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
210         {"multicast_packets",
211          offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
212         {"unicast_packets",
213          offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
214         {"undersize_packets",
215          offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
216         {"size_64_packets",
217          offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
218         {"size_65_to_127_packets",
219          offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
220         {"size_128_to_255_packets",
221          offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
222         {"size_256_to_511_packets",
223          offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
224         {"size_512_to_1023_packets",
225          offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
226         {"size_1024_to_1522_packets",
227          offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
228         {"size_1523_to_max_packets",
229          offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
230         {"errors_with_bad_CRC",
231          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
232 };
233
234 #define VHOST_NB_XSTATS_RXPORT (sizeof(vhost_rxport_stat_strings) / \
235                                 sizeof(vhost_rxport_stat_strings[0]))
236
237 #define VHOST_NB_XSTATS_TXPORT (sizeof(vhost_txport_stat_strings) / \
238                                 sizeof(vhost_txport_stat_strings[0]))
239
240 static void
241 vhost_dev_xstats_reset(struct rte_eth_dev *dev)
242 {
243         struct vhost_queue *vq = NULL;
244         unsigned int i = 0;
245
246         for (i = 0; i < dev->data->nb_rx_queues; i++) {
247                 vq = dev->data->rx_queues[i];
248                 if (!vq)
249                         continue;
250                 memset(&vq->stats, 0, sizeof(vq->stats));
251         }
252         for (i = 0; i < dev->data->nb_tx_queues; i++) {
253                 vq = dev->data->tx_queues[i];
254                 if (!vq)
255                         continue;
256                 memset(&vq->stats, 0, sizeof(vq->stats));
257         }
258 }
259
260 static int
261 vhost_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
262                            struct rte_eth_xstat_name *xstats_names,
263                            unsigned int limit __rte_unused)
264 {
265         unsigned int t = 0;
266         int count = 0;
267         int nstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
268
269         if (!xstats_names)
270                 return nstats;
271         for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
272                 snprintf(xstats_names[count].name,
273                          sizeof(xstats_names[count].name),
274                          "rx_%s", vhost_rxport_stat_strings[t].name);
275                 count++;
276         }
277         for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
278                 snprintf(xstats_names[count].name,
279                          sizeof(xstats_names[count].name),
280                          "tx_%s", vhost_txport_stat_strings[t].name);
281                 count++;
282         }
283         return count;
284 }
285
286 static int
287 vhost_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
288                      unsigned int n)
289 {
290         unsigned int i;
291         unsigned int t;
292         unsigned int count = 0;
293         struct vhost_queue *vq = NULL;
294         unsigned int nxstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
295
296         if (n < nxstats)
297                 return nxstats;
298
299         for (i = 0; i < dev->data->nb_rx_queues; i++) {
300                 vq = dev->data->rx_queues[i];
301                 if (!vq)
302                         continue;
303                 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
304                                 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
305                                 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
306         }
307         for (i = 0; i < dev->data->nb_tx_queues; i++) {
308                 vq = dev->data->tx_queues[i];
309                 if (!vq)
310                         continue;
311                 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
312                                 + vq->stats.missed_pkts
313                                 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
314                                 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
315         }
316         for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
317                 xstats[count].value = 0;
318                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
319                         vq = dev->data->rx_queues[i];
320                         if (!vq)
321                                 continue;
322                         xstats[count].value +=
323                                 *(uint64_t *)(((char *)vq)
324                                 + vhost_rxport_stat_strings[t].offset);
325                 }
326                 xstats[count].id = count;
327                 count++;
328         }
329         for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
330                 xstats[count].value = 0;
331                 for (i = 0; i < dev->data->nb_tx_queues; i++) {
332                         vq = dev->data->tx_queues[i];
333                         if (!vq)
334                                 continue;
335                         xstats[count].value +=
336                                 *(uint64_t *)(((char *)vq)
337                                 + vhost_txport_stat_strings[t].offset);
338                 }
339                 xstats[count].id = count;
340                 count++;
341         }
342         return count;
343 }
344
345 static inline void
346 vhost_count_multicast_broadcast(struct vhost_queue *vq,
347                                 struct rte_mbuf *mbuf)
348 {
349         struct ether_addr *ea = NULL;
350         struct vhost_stats *pstats = &vq->stats;
351
352         ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
353         if (is_multicast_ether_addr(ea)) {
354                 if (is_broadcast_ether_addr(ea))
355                         pstats->xstats[VHOST_BROADCAST_PKT]++;
356                 else
357                         pstats->xstats[VHOST_MULTICAST_PKT]++;
358         }
359 }
360
361 static void
362 vhost_update_packet_xstats(struct vhost_queue *vq,
363                            struct rte_mbuf **bufs,
364                            uint16_t count)
365 {
366         uint32_t pkt_len = 0;
367         uint64_t i = 0;
368         uint64_t index;
369         struct vhost_stats *pstats = &vq->stats;
370
371         for (i = 0; i < count ; i++) {
372                 pkt_len = bufs[i]->pkt_len;
373                 if (pkt_len == 64) {
374                         pstats->xstats[VHOST_64_PKT]++;
375                 } else if (pkt_len > 64 && pkt_len < 1024) {
376                         index = (sizeof(pkt_len) * 8)
377                                 - __builtin_clz(pkt_len) - 5;
378                         pstats->xstats[index]++;
379                 } else {
380                         if (pkt_len < 64)
381                                 pstats->xstats[VHOST_UNDERSIZE_PKT]++;
382                         else if (pkt_len <= 1522)
383                                 pstats->xstats[VHOST_1024_TO_1522_PKT]++;
384                         else if (pkt_len > 1522)
385                                 pstats->xstats[VHOST_1523_TO_MAX_PKT]++;
386                 }
387                 vhost_count_multicast_broadcast(vq, bufs[i]);
388         }
389 }
390
391 static uint16_t
392 eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
393 {
394         struct vhost_queue *r = q;
395         uint16_t i, nb_rx = 0;
396
397         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
398                 return 0;
399
400         rte_atomic32_set(&r->while_queuing, 1);
401
402         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
403                 goto out;
404
405         /* Dequeue packets from guest TX queue */
406         nb_rx = rte_vhost_dequeue_burst(r->vid,
407                         r->virtqueue_id, r->mb_pool, bufs, nb_bufs);
408
409         r->stats.pkts += nb_rx;
410
411         for (i = 0; likely(i < nb_rx); i++) {
412                 bufs[i]->port = r->port;
413                 r->stats.bytes += bufs[i]->pkt_len;
414         }
415
416         vhost_update_packet_xstats(r, bufs, nb_rx);
417
418 out:
419         rte_atomic32_set(&r->while_queuing, 0);
420
421         return nb_rx;
422 }
423
424 static uint16_t
425 eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
426 {
427         struct vhost_queue *r = q;
428         uint16_t i, nb_tx = 0;
429
430         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
431                 return 0;
432
433         rte_atomic32_set(&r->while_queuing, 1);
434
435         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
436                 goto out;
437
438         /* Enqueue packets to guest RX queue */
439         nb_tx = rte_vhost_enqueue_burst(r->vid,
440                         r->virtqueue_id, bufs, nb_bufs);
441
442         r->stats.pkts += nb_tx;
443         r->stats.missed_pkts += nb_bufs - nb_tx;
444
445         for (i = 0; likely(i < nb_tx); i++)
446                 r->stats.bytes += bufs[i]->pkt_len;
447
448         vhost_update_packet_xstats(r, bufs, nb_tx);
449
450         /* According to RFC2863 page42 section ifHCOutMulticastPkts and
451          * ifHCOutBroadcastPkts, the counters "multicast" and "broadcast"
452          * are increased when packets are not transmitted successfully.
453          */
454         for (i = nb_tx; i < nb_bufs; i++)
455                 vhost_count_multicast_broadcast(r, bufs[i]);
456
457         for (i = 0; likely(i < nb_tx); i++)
458                 rte_pktmbuf_free(bufs[i]);
459 out:
460         rte_atomic32_set(&r->while_queuing, 0);
461
462         return nb_tx;
463 }
464
465 static int
466 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
467 {
468         return 0;
469 }
470
471 static inline struct internal_list *
472 find_internal_resource(char *ifname)
473 {
474         int found = 0;
475         struct internal_list *list;
476         struct pmd_internal *internal;
477
478         if (ifname == NULL)
479                 return NULL;
480
481         pthread_mutex_lock(&internal_list_lock);
482
483         TAILQ_FOREACH(list, &internal_list, next) {
484                 internal = list->eth_dev->data->dev_private;
485                 if (!strcmp(internal->iface_name, ifname)) {
486                         found = 1;
487                         break;
488                 }
489         }
490
491         pthread_mutex_unlock(&internal_list_lock);
492
493         if (!found)
494                 return NULL;
495
496         return list;
497 }
498
499 static void
500 update_queuing_status(struct rte_eth_dev *dev)
501 {
502         struct pmd_internal *internal = dev->data->dev_private;
503         struct vhost_queue *vq;
504         unsigned int i;
505         int allow_queuing = 1;
506
507         if (rte_atomic32_read(&internal->started) == 0 ||
508             rte_atomic32_read(&internal->dev_attached) == 0)
509                 allow_queuing = 0;
510
511         /* Wait until rx/tx_pkt_burst stops accessing vhost device */
512         for (i = 0; i < dev->data->nb_rx_queues; i++) {
513                 vq = dev->data->rx_queues[i];
514                 if (vq == NULL)
515                         continue;
516                 rte_atomic32_set(&vq->allow_queuing, allow_queuing);
517                 while (rte_atomic32_read(&vq->while_queuing))
518                         rte_pause();
519         }
520
521         for (i = 0; i < dev->data->nb_tx_queues; i++) {
522                 vq = dev->data->tx_queues[i];
523                 if (vq == NULL)
524                         continue;
525                 rte_atomic32_set(&vq->allow_queuing, allow_queuing);
526                 while (rte_atomic32_read(&vq->while_queuing))
527                         rte_pause();
528         }
529 }
530
531 static int
532 new_device(int vid)
533 {
534         struct rte_eth_dev *eth_dev;
535         struct internal_list *list;
536         struct pmd_internal *internal;
537         struct vhost_queue *vq;
538         unsigned i;
539         char ifname[PATH_MAX];
540 #ifdef RTE_LIBRTE_VHOST_NUMA
541         int newnode;
542 #endif
543
544         rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
545         list = find_internal_resource(ifname);
546         if (list == NULL) {
547                 RTE_LOG(INFO, PMD, "Invalid device name: %s\n", ifname);
548                 return -1;
549         }
550
551         eth_dev = list->eth_dev;
552         internal = eth_dev->data->dev_private;
553
554 #ifdef RTE_LIBRTE_VHOST_NUMA
555         newnode = rte_vhost_get_numa_node(vid);
556         if (newnode >= 0)
557                 eth_dev->data->numa_node = newnode;
558 #endif
559
560         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
561                 vq = eth_dev->data->rx_queues[i];
562                 if (vq == NULL)
563                         continue;
564                 vq->vid = vid;
565                 vq->internal = internal;
566                 vq->port = eth_dev->data->port_id;
567         }
568         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
569                 vq = eth_dev->data->tx_queues[i];
570                 if (vq == NULL)
571                         continue;
572                 vq->vid = vid;
573                 vq->internal = internal;
574                 vq->port = eth_dev->data->port_id;
575         }
576
577         for (i = 0; i < rte_vhost_get_queue_num(vid) * VIRTIO_QNUM; i++)
578                 rte_vhost_enable_guest_notification(vid, i, 0);
579
580         eth_dev->data->dev_link.link_status = ETH_LINK_UP;
581
582         rte_atomic32_set(&internal->dev_attached, 1);
583         update_queuing_status(eth_dev);
584
585         RTE_LOG(INFO, PMD, "New connection established\n");
586
587         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
588
589         return 0;
590 }
591
592 static void
593 destroy_device(int vid)
594 {
595         struct rte_eth_dev *eth_dev;
596         struct pmd_internal *internal;
597         struct vhost_queue *vq;
598         struct internal_list *list;
599         char ifname[PATH_MAX];
600         unsigned i;
601         struct rte_vhost_vring_state *state;
602
603         rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
604         list = find_internal_resource(ifname);
605         if (list == NULL) {
606                 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
607                 return;
608         }
609         eth_dev = list->eth_dev;
610         internal = eth_dev->data->dev_private;
611
612         rte_atomic32_set(&internal->dev_attached, 0);
613         update_queuing_status(eth_dev);
614
615         eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
616
617         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
618                 vq = eth_dev->data->rx_queues[i];
619                 if (vq == NULL)
620                         continue;
621                 vq->vid = -1;
622         }
623         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
624                 vq = eth_dev->data->tx_queues[i];
625                 if (vq == NULL)
626                         continue;
627                 vq->vid = -1;
628         }
629
630         state = vring_states[eth_dev->data->port_id];
631         rte_spinlock_lock(&state->lock);
632         for (i = 0; i <= state->max_vring; i++) {
633                 state->cur[i] = false;
634                 state->seen[i] = false;
635         }
636         state->max_vring = 0;
637         rte_spinlock_unlock(&state->lock);
638
639         RTE_LOG(INFO, PMD, "Connection closed\n");
640
641         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
642 }
643
644 static int
645 vring_state_changed(int vid, uint16_t vring, int enable)
646 {
647         struct rte_vhost_vring_state *state;
648         struct rte_eth_dev *eth_dev;
649         struct internal_list *list;
650         char ifname[PATH_MAX];
651
652         rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
653         list = find_internal_resource(ifname);
654         if (list == NULL) {
655                 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
656                 return -1;
657         }
658
659         eth_dev = list->eth_dev;
660         /* won't be NULL */
661         state = vring_states[eth_dev->data->port_id];
662         rte_spinlock_lock(&state->lock);
663         state->cur[vring] = enable;
664         state->max_vring = RTE_MAX(vring, state->max_vring);
665         rte_spinlock_unlock(&state->lock);
666
667         RTE_LOG(INFO, PMD, "vring%u is %s\n",
668                         vring, enable ? "enabled" : "disabled");
669
670         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE, NULL);
671
672         return 0;
673 }
674
675 int
676 rte_eth_vhost_get_queue_event(uint8_t port_id,
677                 struct rte_eth_vhost_queue_event *event)
678 {
679         struct rte_vhost_vring_state *state;
680         unsigned int i;
681         int idx;
682
683         if (port_id >= RTE_MAX_ETHPORTS) {
684                 RTE_LOG(ERR, PMD, "Invalid port id\n");
685                 return -1;
686         }
687
688         state = vring_states[port_id];
689         if (!state) {
690                 RTE_LOG(ERR, PMD, "Unused port\n");
691                 return -1;
692         }
693
694         rte_spinlock_lock(&state->lock);
695         for (i = 0; i <= state->max_vring; i++) {
696                 idx = state->index++ % (state->max_vring + 1);
697
698                 if (state->cur[idx] != state->seen[idx]) {
699                         state->seen[idx] = state->cur[idx];
700                         event->queue_id = idx / 2;
701                         event->rx = idx & 1;
702                         event->enable = state->cur[idx];
703                         rte_spinlock_unlock(&state->lock);
704                         return 0;
705                 }
706         }
707         rte_spinlock_unlock(&state->lock);
708
709         return -1;
710 }
711
712 int
713 rte_eth_vhost_get_vid_from_port_id(uint8_t port_id)
714 {
715         struct internal_list *list;
716         struct rte_eth_dev *eth_dev;
717         struct vhost_queue *vq;
718         int vid = -1;
719
720         if (!rte_eth_dev_is_valid_port(port_id))
721                 return -1;
722
723         pthread_mutex_lock(&internal_list_lock);
724
725         TAILQ_FOREACH(list, &internal_list, next) {
726                 eth_dev = list->eth_dev;
727                 if (eth_dev->data->port_id == port_id) {
728                         vq = eth_dev->data->rx_queues[0];
729                         if (vq) {
730                                 vid = vq->vid;
731                         }
732                         break;
733                 }
734         }
735
736         pthread_mutex_unlock(&internal_list_lock);
737
738         return vid;
739 }
740
741 static void *
742 vhost_driver_session(void *param __rte_unused)
743 {
744         static struct virtio_net_device_ops vhost_ops;
745
746         /* set vhost arguments */
747         vhost_ops.new_device = new_device;
748         vhost_ops.destroy_device = destroy_device;
749         vhost_ops.vring_state_changed = vring_state_changed;
750         if (rte_vhost_driver_callback_register(&vhost_ops) < 0)
751                 RTE_LOG(ERR, PMD, "Can't register callbacks\n");
752
753         /* start event handling */
754         rte_vhost_driver_session_start();
755
756         return NULL;
757 }
758
759 static int
760 vhost_driver_session_start(void)
761 {
762         int ret;
763
764         ret = pthread_create(&session_th,
765                         NULL, vhost_driver_session, NULL);
766         if (ret)
767                 RTE_LOG(ERR, PMD, "Can't create a thread\n");
768
769         return ret;
770 }
771
772 static void
773 vhost_driver_session_stop(void)
774 {
775         int ret;
776
777         ret = pthread_cancel(session_th);
778         if (ret)
779                 RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
780
781         ret = pthread_join(session_th, NULL);
782         if (ret)
783                 RTE_LOG(ERR, PMD, "Can't join the thread\n");
784 }
785
786 static int
787 eth_dev_start(struct rte_eth_dev *dev)
788 {
789         struct pmd_internal *internal = dev->data->dev_private;
790
791         rte_atomic32_set(&internal->started, 1);
792         update_queuing_status(dev);
793
794         return 0;
795 }
796
797 static void
798 eth_dev_stop(struct rte_eth_dev *dev)
799 {
800         struct pmd_internal *internal = dev->data->dev_private;
801
802         rte_atomic32_set(&internal->started, 0);
803         update_queuing_status(dev);
804 }
805
806 static int
807 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
808                    uint16_t nb_rx_desc __rte_unused,
809                    unsigned int socket_id,
810                    const struct rte_eth_rxconf *rx_conf __rte_unused,
811                    struct rte_mempool *mb_pool)
812 {
813         struct vhost_queue *vq;
814
815         vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
816                         RTE_CACHE_LINE_SIZE, socket_id);
817         if (vq == NULL) {
818                 RTE_LOG(ERR, PMD, "Failed to allocate memory for rx queue\n");
819                 return -ENOMEM;
820         }
821
822         vq->mb_pool = mb_pool;
823         vq->virtqueue_id = rx_queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
824         dev->data->rx_queues[rx_queue_id] = vq;
825
826         return 0;
827 }
828
829 static int
830 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
831                    uint16_t nb_tx_desc __rte_unused,
832                    unsigned int socket_id,
833                    const struct rte_eth_txconf *tx_conf __rte_unused)
834 {
835         struct vhost_queue *vq;
836
837         vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
838                         RTE_CACHE_LINE_SIZE, socket_id);
839         if (vq == NULL) {
840                 RTE_LOG(ERR, PMD, "Failed to allocate memory for tx queue\n");
841                 return -ENOMEM;
842         }
843
844         vq->virtqueue_id = tx_queue_id * VIRTIO_QNUM + VIRTIO_RXQ;
845         dev->data->tx_queues[tx_queue_id] = vq;
846
847         return 0;
848 }
849
850 static void
851 eth_dev_info(struct rte_eth_dev *dev,
852              struct rte_eth_dev_info *dev_info)
853 {
854         struct pmd_internal *internal;
855
856         internal = dev->data->dev_private;
857         if (internal == NULL) {
858                 RTE_LOG(ERR, PMD, "Invalid device specified\n");
859                 return;
860         }
861
862         dev_info->driver_name = drivername;
863         dev_info->max_mac_addrs = 1;
864         dev_info->max_rx_pktlen = (uint32_t)-1;
865         dev_info->max_rx_queues = internal->max_queues;
866         dev_info->max_tx_queues = internal->max_queues;
867         dev_info->min_rx_bufsize = 0;
868 }
869
870 static void
871 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
872 {
873         unsigned i;
874         unsigned long rx_total = 0, tx_total = 0, tx_missed_total = 0;
875         unsigned long rx_total_bytes = 0, tx_total_bytes = 0;
876         struct vhost_queue *vq;
877
878         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
879                         i < dev->data->nb_rx_queues; i++) {
880                 if (dev->data->rx_queues[i] == NULL)
881                         continue;
882                 vq = dev->data->rx_queues[i];
883                 stats->q_ipackets[i] = vq->stats.pkts;
884                 rx_total += stats->q_ipackets[i];
885
886                 stats->q_ibytes[i] = vq->stats.bytes;
887                 rx_total_bytes += stats->q_ibytes[i];
888         }
889
890         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
891                         i < dev->data->nb_tx_queues; i++) {
892                 if (dev->data->tx_queues[i] == NULL)
893                         continue;
894                 vq = dev->data->tx_queues[i];
895                 stats->q_opackets[i] = vq->stats.pkts;
896                 tx_missed_total += vq->stats.missed_pkts;
897                 tx_total += stats->q_opackets[i];
898
899                 stats->q_obytes[i] = vq->stats.bytes;
900                 tx_total_bytes += stats->q_obytes[i];
901         }
902
903         stats->ipackets = rx_total;
904         stats->opackets = tx_total;
905         stats->oerrors = tx_missed_total;
906         stats->ibytes = rx_total_bytes;
907         stats->obytes = tx_total_bytes;
908 }
909
910 static void
911 eth_stats_reset(struct rte_eth_dev *dev)
912 {
913         struct vhost_queue *vq;
914         unsigned i;
915
916         for (i = 0; i < dev->data->nb_rx_queues; i++) {
917                 if (dev->data->rx_queues[i] == NULL)
918                         continue;
919                 vq = dev->data->rx_queues[i];
920                 vq->stats.pkts = 0;
921                 vq->stats.bytes = 0;
922         }
923         for (i = 0; i < dev->data->nb_tx_queues; i++) {
924                 if (dev->data->tx_queues[i] == NULL)
925                         continue;
926                 vq = dev->data->tx_queues[i];
927                 vq->stats.pkts = 0;
928                 vq->stats.bytes = 0;
929                 vq->stats.missed_pkts = 0;
930         }
931 }
932
933 static void
934 eth_queue_release(void *q)
935 {
936         rte_free(q);
937 }
938
939 static int
940 eth_link_update(struct rte_eth_dev *dev __rte_unused,
941                 int wait_to_complete __rte_unused)
942 {
943         return 0;
944 }
945
946 /**
947  * Disable features in feature_mask. Returns 0 on success.
948  */
949 int
950 rte_eth_vhost_feature_disable(uint64_t feature_mask)
951 {
952         return rte_vhost_feature_disable(feature_mask);
953 }
954
955 /**
956  * Enable features in feature_mask. Returns 0 on success.
957  */
958 int
959 rte_eth_vhost_feature_enable(uint64_t feature_mask)
960 {
961         return rte_vhost_feature_enable(feature_mask);
962 }
963
964 /* Returns currently supported vhost features */
965 uint64_t
966 rte_eth_vhost_feature_get(void)
967 {
968         return rte_vhost_feature_get();
969 }
970
971 static const struct eth_dev_ops ops = {
972         .dev_start = eth_dev_start,
973         .dev_stop = eth_dev_stop,
974         .dev_configure = eth_dev_configure,
975         .dev_infos_get = eth_dev_info,
976         .rx_queue_setup = eth_rx_queue_setup,
977         .tx_queue_setup = eth_tx_queue_setup,
978         .rx_queue_release = eth_queue_release,
979         .tx_queue_release = eth_queue_release,
980         .link_update = eth_link_update,
981         .stats_get = eth_stats_get,
982         .stats_reset = eth_stats_reset,
983         .xstats_reset = vhost_dev_xstats_reset,
984         .xstats_get = vhost_dev_xstats_get,
985         .xstats_get_names = vhost_dev_xstats_get_names,
986 };
987
988 static int
989 eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
990                      const unsigned numa_node, uint64_t flags)
991 {
992         struct rte_eth_dev_data *data = NULL;
993         struct pmd_internal *internal = NULL;
994         struct rte_eth_dev *eth_dev = NULL;
995         struct ether_addr *eth_addr = NULL;
996         struct rte_vhost_vring_state *vring_state = NULL;
997         struct internal_list *list = NULL;
998
999         RTE_LOG(INFO, PMD, "Creating VHOST-USER backend on numa socket %u\n",
1000                 numa_node);
1001
1002         /* now do all data allocation - for eth_dev structure, dummy pci driver
1003          * and internal (private) data
1004          */
1005         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
1006         if (data == NULL)
1007                 goto error;
1008
1009         internal = rte_zmalloc_socket(name, sizeof(*internal), 0, numa_node);
1010         if (internal == NULL)
1011                 goto error;
1012
1013         list = rte_zmalloc_socket(name, sizeof(*list), 0, numa_node);
1014         if (list == NULL)
1015                 goto error;
1016
1017         /* reserve an ethdev entry */
1018         eth_dev = rte_eth_dev_allocate(name);
1019         if (eth_dev == NULL)
1020                 goto error;
1021
1022         eth_addr = rte_zmalloc_socket(name, sizeof(*eth_addr), 0, numa_node);
1023         if (eth_addr == NULL)
1024                 goto error;
1025         *eth_addr = base_eth_addr;
1026         eth_addr->addr_bytes[5] = eth_dev->data->port_id;
1027
1028         vring_state = rte_zmalloc_socket(name,
1029                         sizeof(*vring_state), 0, numa_node);
1030         if (vring_state == NULL)
1031                 goto error;
1032
1033         /* now put it all together
1034          * - store queue data in internal,
1035          * - store numa_node info in ethdev data
1036          * - point eth_dev_data to internals
1037          * - and point eth_dev structure to new eth_dev_data structure
1038          */
1039         internal->dev_name = strdup(name);
1040         if (internal->dev_name == NULL)
1041                 goto error;
1042         internal->iface_name = strdup(iface_name);
1043         if (internal->iface_name == NULL)
1044                 goto error;
1045
1046         list->eth_dev = eth_dev;
1047         pthread_mutex_lock(&internal_list_lock);
1048         TAILQ_INSERT_TAIL(&internal_list, list, next);
1049         pthread_mutex_unlock(&internal_list_lock);
1050
1051         rte_spinlock_init(&vring_state->lock);
1052         vring_states[eth_dev->data->port_id] = vring_state;
1053
1054         data->dev_private = internal;
1055         data->port_id = eth_dev->data->port_id;
1056         memmove(data->name, eth_dev->data->name, sizeof(data->name));
1057         data->nb_rx_queues = queues;
1058         data->nb_tx_queues = queues;
1059         internal->max_queues = queues;
1060         data->dev_link = pmd_link;
1061         data->mac_addrs = eth_addr;
1062
1063         /* We'll replace the 'data' originally allocated by eth_dev. So the
1064          * vhost PMD resources won't be shared between multi processes.
1065          */
1066         eth_dev->data = data;
1067         eth_dev->dev_ops = &ops;
1068         eth_dev->driver = NULL;
1069         data->dev_flags =
1070                 RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
1071         data->kdrv = RTE_KDRV_NONE;
1072         data->drv_name = internal->dev_name;
1073         data->numa_node = numa_node;
1074
1075         /* finally assign rx and tx ops */
1076         eth_dev->rx_pkt_burst = eth_vhost_rx;
1077         eth_dev->tx_pkt_burst = eth_vhost_tx;
1078
1079         if (rte_vhost_driver_register(iface_name, flags))
1080                 goto error;
1081
1082         /* We need only one message handling thread */
1083         if (rte_atomic16_add_return(&nb_started_ports, 1) == 1) {
1084                 if (vhost_driver_session_start())
1085                         goto error;
1086         }
1087
1088         return data->port_id;
1089
1090 error:
1091         if (internal)
1092                 free(internal->dev_name);
1093         rte_free(vring_state);
1094         rte_free(eth_addr);
1095         if (eth_dev)
1096                 rte_eth_dev_release_port(eth_dev);
1097         rte_free(internal);
1098         rte_free(list);
1099         rte_free(data);
1100
1101         return -1;
1102 }
1103
1104 static inline int
1105 open_iface(const char *key __rte_unused, const char *value, void *extra_args)
1106 {
1107         const char **iface_name = extra_args;
1108
1109         if (value == NULL)
1110                 return -1;
1111
1112         *iface_name = value;
1113
1114         return 0;
1115 }
1116
1117 static inline int
1118 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1119 {
1120         uint16_t *n = extra_args;
1121
1122         if (value == NULL || extra_args == NULL)
1123                 return -EINVAL;
1124
1125         *n = (uint16_t)strtoul(value, NULL, 0);
1126         if (*n == USHRT_MAX && errno == ERANGE)
1127                 return -1;
1128
1129         return 0;
1130 }
1131
1132 static int
1133 rte_pmd_vhost_probe(const char *name, const char *params)
1134 {
1135         struct rte_kvargs *kvlist = NULL;
1136         int ret = 0;
1137         char *iface_name;
1138         uint16_t queues;
1139         uint64_t flags = 0;
1140         int client_mode = 0;
1141         int dequeue_zero_copy = 0;
1142
1143         RTE_LOG(INFO, PMD, "Initializing pmd_vhost for %s\n", name);
1144
1145         kvlist = rte_kvargs_parse(params, valid_arguments);
1146         if (kvlist == NULL)
1147                 return -1;
1148
1149         if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
1150                 ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
1151                                          &open_iface, &iface_name);
1152                 if (ret < 0)
1153                         goto out_free;
1154         } else {
1155                 ret = -1;
1156                 goto out_free;
1157         }
1158
1159         if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
1160                 ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
1161                                          &open_int, &queues);
1162                 if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
1163                         goto out_free;
1164
1165         } else
1166                 queues = 1;
1167
1168         if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
1169                 ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
1170                                          &open_int, &client_mode);
1171                 if (ret < 0)
1172                         goto out_free;
1173
1174                 if (client_mode)
1175                         flags |= RTE_VHOST_USER_CLIENT;
1176         }
1177
1178         if (rte_kvargs_count(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY) == 1) {
1179                 ret = rte_kvargs_process(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY,
1180                                          &open_int, &dequeue_zero_copy);
1181                 if (ret < 0)
1182                         goto out_free;
1183
1184                 if (dequeue_zero_copy)
1185                         flags |= RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
1186         }
1187
1188         eth_dev_vhost_create(name, iface_name, queues, rte_socket_id(), flags);
1189
1190 out_free:
1191         rte_kvargs_free(kvlist);
1192         return ret;
1193 }
1194
1195 static int
1196 rte_pmd_vhost_remove(const char *name)
1197 {
1198         struct rte_eth_dev *eth_dev = NULL;
1199         struct pmd_internal *internal;
1200         struct internal_list *list;
1201         unsigned int i;
1202
1203         RTE_LOG(INFO, PMD, "Un-Initializing pmd_vhost for %s\n", name);
1204
1205         /* find an ethdev entry */
1206         eth_dev = rte_eth_dev_allocated(name);
1207         if (eth_dev == NULL)
1208                 return -ENODEV;
1209
1210         internal = eth_dev->data->dev_private;
1211         if (internal == NULL)
1212                 return -ENODEV;
1213
1214         list = find_internal_resource(internal->iface_name);
1215         if (list == NULL)
1216                 return -ENODEV;
1217
1218         pthread_mutex_lock(&internal_list_lock);
1219         TAILQ_REMOVE(&internal_list, list, next);
1220         pthread_mutex_unlock(&internal_list_lock);
1221         rte_free(list);
1222
1223         eth_dev_stop(eth_dev);
1224
1225         rte_vhost_driver_unregister(internal->iface_name);
1226
1227         if (rte_atomic16_sub_return(&nb_started_ports, 1) == 0)
1228                 vhost_driver_session_stop();
1229
1230         rte_free(vring_states[eth_dev->data->port_id]);
1231         vring_states[eth_dev->data->port_id] = NULL;
1232
1233         free(internal->dev_name);
1234         free(internal->iface_name);
1235
1236         for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
1237                 rte_free(eth_dev->data->rx_queues[i]);
1238         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1239                 rte_free(eth_dev->data->tx_queues[i]);
1240
1241         rte_free(eth_dev->data->mac_addrs);
1242         rte_free(eth_dev->data);
1243         rte_free(internal);
1244
1245         rte_eth_dev_release_port(eth_dev);
1246
1247         return 0;
1248 }
1249
1250 static struct rte_vdev_driver pmd_vhost_drv = {
1251         .probe = rte_pmd_vhost_probe,
1252         .remove = rte_pmd_vhost_remove,
1253 };
1254
1255 RTE_PMD_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
1256 RTE_PMD_REGISTER_ALIAS(net_vhost, eth_vhost);
1257 RTE_PMD_REGISTER_PARAM_STRING(net_vhost,
1258         "iface=<ifc> "
1259         "queues=<int>");