net/vhost: add extended statistics
[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
55 static const char *drivername = "VHOST PMD";
56
57 static const char *valid_arguments[] = {
58         ETH_VHOST_IFACE_ARG,
59         ETH_VHOST_QUEUES_ARG,
60         ETH_VHOST_CLIENT_ARG,
61         NULL
62 };
63
64 static struct ether_addr base_eth_addr = {
65         .addr_bytes = {
66                 0x56 /* V */,
67                 0x48 /* H */,
68                 0x4F /* O */,
69                 0x53 /* S */,
70                 0x54 /* T */,
71                 0x00
72         }
73 };
74
75 enum vhost_xstats_pkts {
76         VHOST_UNDERSIZE_PKT = 0,
77         VHOST_64_PKT,
78         VHOST_65_TO_127_PKT,
79         VHOST_128_TO_255_PKT,
80         VHOST_256_TO_511_PKT,
81         VHOST_512_TO_1023_PKT,
82         VHOST_1024_TO_1522_PKT,
83         VHOST_1523_TO_MAX_PKT,
84         VHOST_BROADCAST_PKT,
85         VHOST_MULTICAST_PKT,
86         VHOST_UNICAST_PKT,
87         VHOST_ERRORS_PKT,
88         VHOST_ERRORS_FRAGMENTED,
89         VHOST_ERRORS_JABBER,
90         VHOST_UNKNOWN_PROTOCOL,
91         VHOST_XSTATS_MAX,
92 };
93
94 struct vhost_stats {
95         uint64_t pkts;
96         uint64_t bytes;
97         uint64_t missed_pkts;
98         uint64_t xstats[VHOST_XSTATS_MAX];
99 };
100
101 struct vhost_queue {
102         int vid;
103         rte_atomic32_t allow_queuing;
104         rte_atomic32_t while_queuing;
105         struct pmd_internal *internal;
106         struct rte_mempool *mb_pool;
107         uint8_t port;
108         uint16_t virtqueue_id;
109         struct vhost_stats stats;
110 };
111
112 struct pmd_internal {
113         char *dev_name;
114         char *iface_name;
115         uint16_t max_queues;
116         uint64_t flags;
117
118         volatile uint16_t once;
119 };
120
121 struct internal_list {
122         TAILQ_ENTRY(internal_list) next;
123         struct rte_eth_dev *eth_dev;
124 };
125
126 TAILQ_HEAD(internal_list_head, internal_list);
127 static struct internal_list_head internal_list =
128         TAILQ_HEAD_INITIALIZER(internal_list);
129
130 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
131
132 static rte_atomic16_t nb_started_ports;
133 static pthread_t session_th;
134
135 static struct rte_eth_link pmd_link = {
136                 .link_speed = 10000,
137                 .link_duplex = ETH_LINK_FULL_DUPLEX,
138                 .link_status = ETH_LINK_DOWN
139 };
140
141 struct rte_vhost_vring_state {
142         rte_spinlock_t lock;
143
144         bool cur[RTE_MAX_QUEUES_PER_PORT * 2];
145         bool seen[RTE_MAX_QUEUES_PER_PORT * 2];
146         unsigned int index;
147         unsigned int max_vring;
148 };
149
150 static struct rte_vhost_vring_state *vring_states[RTE_MAX_ETHPORTS];
151
152 #define VHOST_XSTATS_NAME_SIZE 64
153
154 struct vhost_xstats_name_off {
155         char name[VHOST_XSTATS_NAME_SIZE];
156         uint64_t offset;
157 };
158
159 /* [rx]_is prepended to the name string here */
160 static const struct vhost_xstats_name_off vhost_rxport_stat_strings[] = {
161         {"good_packets",
162          offsetof(struct vhost_queue, stats.pkts)},
163         {"total_bytes",
164          offsetof(struct vhost_queue, stats.bytes)},
165         {"missed_pkts",
166          offsetof(struct vhost_queue, stats.missed_pkts)},
167         {"broadcast_packets",
168          offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
169         {"multicast_packets",
170          offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
171         {"unicast_packets",
172          offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
173          {"undersize_packets",
174          offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
175         {"size_64_packets",
176          offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
177         {"size_65_to_127_packets",
178          offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
179         {"size_128_to_255_packets",
180          offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
181         {"size_256_to_511_packets",
182          offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
183         {"size_512_to_1023_packets",
184          offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
185         {"size_1024_to_1522_packets",
186          offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
187         {"size_1523_to_max_packets",
188          offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
189         {"errors_with_bad_CRC",
190          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
191         {"fragmented_errors",
192          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_FRAGMENTED])},
193         {"jabber_errors",
194          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_JABBER])},
195         {"unknown_protos_packets",
196          offsetof(struct vhost_queue, stats.xstats[VHOST_UNKNOWN_PROTOCOL])},
197 };
198
199 /* [tx]_ is prepended to the name string here */
200 static const struct vhost_xstats_name_off vhost_txport_stat_strings[] = {
201         {"good_packets",
202          offsetof(struct vhost_queue, stats.pkts)},
203         {"total_bytes",
204          offsetof(struct vhost_queue, stats.bytes)},
205         {"missed_pkts",
206          offsetof(struct vhost_queue, stats.missed_pkts)},
207         {"broadcast_packets",
208          offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
209         {"multicast_packets",
210          offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
211         {"unicast_packets",
212          offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
213         {"undersize_packets",
214          offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
215         {"size_64_packets",
216          offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
217         {"size_65_to_127_packets",
218          offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
219         {"size_128_to_255_packets",
220          offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
221         {"size_256_to_511_packets",
222          offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
223         {"size_512_to_1023_packets",
224          offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
225         {"size_1024_to_1522_packets",
226          offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
227         {"size_1523_to_max_packets",
228          offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
229         {"errors_with_bad_CRC",
230          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
231 };
232
233 #define VHOST_NB_XSTATS_RXPORT (sizeof(vhost_rxport_stat_strings) / \
234                                 sizeof(vhost_rxport_stat_strings[0]))
235
236 #define VHOST_NB_XSTATS_TXPORT (sizeof(vhost_txport_stat_strings) / \
237                                 sizeof(vhost_txport_stat_strings[0]))
238
239 static void
240 vhost_dev_xstats_reset(struct rte_eth_dev *dev)
241 {
242         struct vhost_queue *vq = NULL;
243         unsigned int i = 0;
244
245         for (i = 0; i < dev->data->nb_rx_queues; i++) {
246                 vq = dev->data->rx_queues[i];
247                 if (!vq)
248                         continue;
249                 memset(&vq->stats, 0, sizeof(vq->stats));
250         }
251         for (i = 0; i < dev->data->nb_tx_queues; i++) {
252                 vq = dev->data->tx_queues[i];
253                 if (!vq)
254                         continue;
255                 memset(&vq->stats, 0, sizeof(vq->stats));
256         }
257 }
258
259 static int
260 vhost_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
261                            struct rte_eth_xstat_name *xstats_names,
262                            unsigned int limit __rte_unused)
263 {
264         unsigned int t = 0;
265         int count = 0;
266         int nstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
267
268         if (!xstats_names)
269                 return nstats;
270         for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
271                 snprintf(xstats_names[count].name,
272                          sizeof(xstats_names[count].name),
273                          "rx_%s", vhost_rxport_stat_strings[t].name);
274                 count++;
275         }
276         for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
277                 snprintf(xstats_names[count].name,
278                          sizeof(xstats_names[count].name),
279                          "tx_%s", vhost_txport_stat_strings[t].name);
280                 count++;
281         }
282         return count;
283 }
284
285 static int
286 vhost_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
287                      unsigned int n)
288 {
289         unsigned int i;
290         unsigned int t;
291         unsigned int count = 0;
292         struct vhost_queue *vq = NULL;
293         unsigned int nxstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
294
295         if (n < nxstats)
296                 return nxstats;
297
298         for (i = 0; i < dev->data->nb_rx_queues; i++) {
299                 vq = dev->data->rx_queues[i];
300                 if (!vq)
301                         continue;
302                 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
303                                 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
304                                 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
305         }
306         for (i = 0; i < dev->data->nb_tx_queues; i++) {
307                 vq = dev->data->tx_queues[i];
308                 if (!vq)
309                         continue;
310                 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
311                                 + vq->stats.missed_pkts
312                                 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
313                                 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
314         }
315         for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
316                 xstats[count].value = 0;
317                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
318                         vq = dev->data->rx_queues[i];
319                         if (!vq)
320                                 continue;
321                         xstats[count].value +=
322                                 *(uint64_t *)(((char *)vq)
323                                 + vhost_rxport_stat_strings[t].offset);
324                 }
325                 count++;
326         }
327         for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
328                 xstats[count].value = 0;
329                 for (i = 0; i < dev->data->nb_tx_queues; i++) {
330                         vq = dev->data->tx_queues[i];
331                         if (!vq)
332                                 continue;
333                         xstats[count].value +=
334                                 *(uint64_t *)(((char *)vq)
335                                 + vhost_txport_stat_strings[t].offset);
336                 }
337                 count++;
338         }
339         return count;
340 }
341
342 static inline void
343 vhost_count_multicast_broadcast(struct vhost_queue *vq,
344                                 struct rte_mbuf *mbuf)
345 {
346         struct ether_addr *ea = NULL;
347         struct vhost_stats *pstats = &vq->stats;
348
349         ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
350         if (is_multicast_ether_addr(ea)) {
351                 if (is_broadcast_ether_addr(ea))
352                         pstats->xstats[VHOST_BROADCAST_PKT]++;
353                 else
354                         pstats->xstats[VHOST_MULTICAST_PKT]++;
355         }
356 }
357
358 static void
359 vhost_update_packet_xstats(struct vhost_queue *vq,
360                            struct rte_mbuf **bufs,
361                            uint16_t count)
362 {
363         uint32_t pkt_len = 0;
364         uint64_t i = 0;
365         uint64_t index;
366         struct vhost_stats *pstats = &vq->stats;
367
368         for (i = 0; i < count ; i++) {
369                 pkt_len = bufs[i]->pkt_len;
370                 if (pkt_len == 64) {
371                         pstats->xstats[VHOST_64_PKT]++;
372                 } else if (pkt_len > 64 && pkt_len < 1024) {
373                         index = (sizeof(pkt_len) * 8)
374                                 - __builtin_clz(pkt_len) - 5;
375                         pstats->xstats[index]++;
376                 } else {
377                         if (pkt_len < 64)
378                                 pstats->xstats[VHOST_UNDERSIZE_PKT]++;
379                         else if (pkt_len <= 1522)
380                                 pstats->xstats[VHOST_1024_TO_1522_PKT]++;
381                         else if (pkt_len > 1522)
382                                 pstats->xstats[VHOST_1523_TO_MAX_PKT]++;
383                 }
384                 vhost_count_multicast_broadcast(vq, bufs[i]);
385         }
386 }
387
388 static uint16_t
389 eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
390 {
391         struct vhost_queue *r = q;
392         uint16_t i, nb_rx = 0;
393
394         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
395                 return 0;
396
397         rte_atomic32_set(&r->while_queuing, 1);
398
399         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
400                 goto out;
401
402         /* Dequeue packets from guest TX queue */
403         nb_rx = rte_vhost_dequeue_burst(r->vid,
404                         r->virtqueue_id, r->mb_pool, bufs, nb_bufs);
405
406         r->stats.pkts += nb_rx;
407
408         for (i = 0; likely(i < nb_rx); i++) {
409                 bufs[i]->port = r->port;
410                 r->stats.bytes += bufs[i]->pkt_len;
411         }
412
413         vhost_update_packet_xstats(r, bufs, nb_rx);
414
415 out:
416         rte_atomic32_set(&r->while_queuing, 0);
417
418         return nb_rx;
419 }
420
421 static uint16_t
422 eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
423 {
424         struct vhost_queue *r = q;
425         uint16_t i, nb_tx = 0;
426
427         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
428                 return 0;
429
430         rte_atomic32_set(&r->while_queuing, 1);
431
432         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
433                 goto out;
434
435         /* Enqueue packets to guest RX queue */
436         nb_tx = rte_vhost_enqueue_burst(r->vid,
437                         r->virtqueue_id, bufs, nb_bufs);
438
439         r->stats.pkts += nb_tx;
440         r->stats.missed_pkts += nb_bufs - nb_tx;
441
442         for (i = 0; likely(i < nb_tx); i++)
443                 r->stats.bytes += bufs[i]->pkt_len;
444
445         vhost_update_packet_xstats(r, bufs, nb_tx);
446
447         /* According to RFC2863 page42 section ifHCOutMulticastPkts and
448          * ifHCOutBroadcastPkts, the counters "multicast" and "broadcast"
449          * are increased when packets are not transmitted successfully.
450          */
451         for (i = nb_tx; i < nb_bufs; i++)
452                 vhost_count_multicast_broadcast(r, bufs[i]);
453
454         for (i = 0; likely(i < nb_tx); i++)
455                 rte_pktmbuf_free(bufs[i]);
456 out:
457         rte_atomic32_set(&r->while_queuing, 0);
458
459         return nb_tx;
460 }
461
462 static int
463 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
464 {
465         return 0;
466 }
467
468 static inline struct internal_list *
469 find_internal_resource(char *ifname)
470 {
471         int found = 0;
472         struct internal_list *list;
473         struct pmd_internal *internal;
474
475         if (ifname == NULL)
476                 return NULL;
477
478         pthread_mutex_lock(&internal_list_lock);
479
480         TAILQ_FOREACH(list, &internal_list, next) {
481                 internal = list->eth_dev->data->dev_private;
482                 if (!strcmp(internal->iface_name, ifname)) {
483                         found = 1;
484                         break;
485                 }
486         }
487
488         pthread_mutex_unlock(&internal_list_lock);
489
490         if (!found)
491                 return NULL;
492
493         return list;
494 }
495
496 static int
497 new_device(int vid)
498 {
499         struct rte_eth_dev *eth_dev;
500         struct internal_list *list;
501         struct pmd_internal *internal;
502         struct vhost_queue *vq;
503         unsigned i;
504         char ifname[PATH_MAX];
505 #ifdef RTE_LIBRTE_VHOST_NUMA
506         int newnode;
507 #endif
508
509         rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
510         list = find_internal_resource(ifname);
511         if (list == NULL) {
512                 RTE_LOG(INFO, PMD, "Invalid device name: %s\n", ifname);
513                 return -1;
514         }
515
516         eth_dev = list->eth_dev;
517         internal = eth_dev->data->dev_private;
518
519 #ifdef RTE_LIBRTE_VHOST_NUMA
520         newnode = rte_vhost_get_numa_node(vid);
521         if (newnode >= 0)
522                 eth_dev->data->numa_node = newnode;
523 #endif
524
525         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
526                 vq = eth_dev->data->rx_queues[i];
527                 if (vq == NULL)
528                         continue;
529                 vq->vid = vid;
530                 vq->internal = internal;
531                 vq->port = eth_dev->data->port_id;
532         }
533         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
534                 vq = eth_dev->data->tx_queues[i];
535                 if (vq == NULL)
536                         continue;
537                 vq->vid = vid;
538                 vq->internal = internal;
539                 vq->port = eth_dev->data->port_id;
540         }
541
542         for (i = 0; i < rte_vhost_get_queue_num(vid) * VIRTIO_QNUM; i++)
543                 rte_vhost_enable_guest_notification(vid, i, 0);
544
545         eth_dev->data->dev_link.link_status = ETH_LINK_UP;
546
547         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
548                 vq = eth_dev->data->rx_queues[i];
549                 if (vq == NULL)
550                         continue;
551                 rte_atomic32_set(&vq->allow_queuing, 1);
552         }
553         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
554                 vq = eth_dev->data->tx_queues[i];
555                 if (vq == NULL)
556                         continue;
557                 rte_atomic32_set(&vq->allow_queuing, 1);
558         }
559
560         RTE_LOG(INFO, PMD, "New connection established\n");
561
562         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC);
563
564         return 0;
565 }
566
567 static void
568 destroy_device(int vid)
569 {
570         struct rte_eth_dev *eth_dev;
571         struct vhost_queue *vq;
572         struct internal_list *list;
573         char ifname[PATH_MAX];
574         unsigned i;
575         struct rte_vhost_vring_state *state;
576
577         rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
578         list = find_internal_resource(ifname);
579         if (list == NULL) {
580                 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
581                 return;
582         }
583         eth_dev = list->eth_dev;
584
585         /* Wait until rx/tx_pkt_burst stops accessing vhost device */
586         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
587                 vq = eth_dev->data->rx_queues[i];
588                 if (vq == NULL)
589                         continue;
590                 rte_atomic32_set(&vq->allow_queuing, 0);
591                 while (rte_atomic32_read(&vq->while_queuing))
592                         rte_pause();
593         }
594         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
595                 vq = eth_dev->data->tx_queues[i];
596                 if (vq == NULL)
597                         continue;
598                 rte_atomic32_set(&vq->allow_queuing, 0);
599                 while (rte_atomic32_read(&vq->while_queuing))
600                         rte_pause();
601         }
602
603         eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
604
605         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
606                 vq = eth_dev->data->rx_queues[i];
607                 if (vq == NULL)
608                         continue;
609                 vq->vid = -1;
610         }
611         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
612                 vq = eth_dev->data->tx_queues[i];
613                 if (vq == NULL)
614                         continue;
615                 vq->vid = -1;
616         }
617
618         state = vring_states[eth_dev->data->port_id];
619         rte_spinlock_lock(&state->lock);
620         for (i = 0; i <= state->max_vring; i++) {
621                 state->cur[i] = false;
622                 state->seen[i] = false;
623         }
624         state->max_vring = 0;
625         rte_spinlock_unlock(&state->lock);
626
627         RTE_LOG(INFO, PMD, "Connection closed\n");
628
629         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC);
630 }
631
632 static int
633 vring_state_changed(int vid, uint16_t vring, int enable)
634 {
635         struct rte_vhost_vring_state *state;
636         struct rte_eth_dev *eth_dev;
637         struct internal_list *list;
638         char ifname[PATH_MAX];
639
640         rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
641         list = find_internal_resource(ifname);
642         if (list == NULL) {
643                 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
644                 return -1;
645         }
646
647         eth_dev = list->eth_dev;
648         /* won't be NULL */
649         state = vring_states[eth_dev->data->port_id];
650         rte_spinlock_lock(&state->lock);
651         state->cur[vring] = enable;
652         state->max_vring = RTE_MAX(vring, state->max_vring);
653         rte_spinlock_unlock(&state->lock);
654
655         RTE_LOG(INFO, PMD, "vring%u is %s\n",
656                         vring, enable ? "enabled" : "disabled");
657
658         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE);
659
660         return 0;
661 }
662
663 int
664 rte_eth_vhost_get_queue_event(uint8_t port_id,
665                 struct rte_eth_vhost_queue_event *event)
666 {
667         struct rte_vhost_vring_state *state;
668         unsigned int i;
669         int idx;
670
671         if (port_id >= RTE_MAX_ETHPORTS) {
672                 RTE_LOG(ERR, PMD, "Invalid port id\n");
673                 return -1;
674         }
675
676         state = vring_states[port_id];
677         if (!state) {
678                 RTE_LOG(ERR, PMD, "Unused port\n");
679                 return -1;
680         }
681
682         rte_spinlock_lock(&state->lock);
683         for (i = 0; i <= state->max_vring; i++) {
684                 idx = state->index++ % (state->max_vring + 1);
685
686                 if (state->cur[idx] != state->seen[idx]) {
687                         state->seen[idx] = state->cur[idx];
688                         event->queue_id = idx / 2;
689                         event->rx = idx & 1;
690                         event->enable = state->cur[idx];
691                         rte_spinlock_unlock(&state->lock);
692                         return 0;
693                 }
694         }
695         rte_spinlock_unlock(&state->lock);
696
697         return -1;
698 }
699
700 int
701 rte_eth_vhost_get_vid_from_port_id(uint8_t port_id)
702 {
703         struct internal_list *list;
704         struct rte_eth_dev *eth_dev;
705         struct vhost_queue *vq;
706         int vid = -1;
707
708         if (!rte_eth_dev_is_valid_port(port_id))
709                 return -1;
710
711         pthread_mutex_lock(&internal_list_lock);
712
713         TAILQ_FOREACH(list, &internal_list, next) {
714                 eth_dev = list->eth_dev;
715                 if (eth_dev->data->port_id == port_id) {
716                         vq = eth_dev->data->rx_queues[0];
717                         if (vq) {
718                                 vid = vq->vid;
719                         }
720                         break;
721                 }
722         }
723
724         pthread_mutex_unlock(&internal_list_lock);
725
726         return vid;
727 }
728
729 static void *
730 vhost_driver_session(void *param __rte_unused)
731 {
732         static struct virtio_net_device_ops vhost_ops;
733
734         /* set vhost arguments */
735         vhost_ops.new_device = new_device;
736         vhost_ops.destroy_device = destroy_device;
737         vhost_ops.vring_state_changed = vring_state_changed;
738         if (rte_vhost_driver_callback_register(&vhost_ops) < 0)
739                 RTE_LOG(ERR, PMD, "Can't register callbacks\n");
740
741         /* start event handling */
742         rte_vhost_driver_session_start();
743
744         return NULL;
745 }
746
747 static int
748 vhost_driver_session_start(void)
749 {
750         int ret;
751
752         ret = pthread_create(&session_th,
753                         NULL, vhost_driver_session, NULL);
754         if (ret)
755                 RTE_LOG(ERR, PMD, "Can't create a thread\n");
756
757         return ret;
758 }
759
760 static void
761 vhost_driver_session_stop(void)
762 {
763         int ret;
764
765         ret = pthread_cancel(session_th);
766         if (ret)
767                 RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
768
769         ret = pthread_join(session_th, NULL);
770         if (ret)
771                 RTE_LOG(ERR, PMD, "Can't join the thread\n");
772 }
773
774 static int
775 eth_dev_start(struct rte_eth_dev *dev)
776 {
777         struct pmd_internal *internal = dev->data->dev_private;
778         int ret = 0;
779
780         if (rte_atomic16_cmpset(&internal->once, 0, 1)) {
781                 ret = rte_vhost_driver_register(internal->iface_name,
782                                                 internal->flags);
783                 if (ret)
784                         return ret;
785         }
786
787         /* We need only one message handling thread */
788         if (rte_atomic16_add_return(&nb_started_ports, 1) == 1)
789                 ret = vhost_driver_session_start();
790
791         return ret;
792 }
793
794 static void
795 eth_dev_stop(struct rte_eth_dev *dev)
796 {
797         struct pmd_internal *internal = dev->data->dev_private;
798
799         if (rte_atomic16_cmpset(&internal->once, 1, 0))
800                 rte_vhost_driver_unregister(internal->iface_name);
801
802         if (rte_atomic16_sub_return(&nb_started_ports, 1) == 0)
803                 vhost_driver_session_stop();
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         TAILQ_INIT(&eth_dev->link_intr_cbs);
1034
1035         /* now put it all together
1036          * - store queue data in internal,
1037          * - store numa_node info in ethdev data
1038          * - point eth_dev_data to internals
1039          * - and point eth_dev structure to new eth_dev_data structure
1040          */
1041         internal->dev_name = strdup(name);
1042         if (internal->dev_name == NULL)
1043                 goto error;
1044         internal->iface_name = strdup(iface_name);
1045         if (internal->iface_name == NULL)
1046                 goto error;
1047         internal->flags = flags;
1048
1049         list->eth_dev = eth_dev;
1050         pthread_mutex_lock(&internal_list_lock);
1051         TAILQ_INSERT_TAIL(&internal_list, list, next);
1052         pthread_mutex_unlock(&internal_list_lock);
1053
1054         rte_spinlock_init(&vring_state->lock);
1055         vring_states[eth_dev->data->port_id] = vring_state;
1056
1057         data->dev_private = internal;
1058         data->port_id = eth_dev->data->port_id;
1059         memmove(data->name, eth_dev->data->name, sizeof(data->name));
1060         data->nb_rx_queues = queues;
1061         data->nb_tx_queues = queues;
1062         internal->max_queues = queues;
1063         data->dev_link = pmd_link;
1064         data->mac_addrs = eth_addr;
1065
1066         /* We'll replace the 'data' originally allocated by eth_dev. So the
1067          * vhost PMD resources won't be shared between multi processes.
1068          */
1069         eth_dev->data = data;
1070         eth_dev->dev_ops = &ops;
1071         eth_dev->driver = NULL;
1072         data->dev_flags =
1073                 RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
1074         data->kdrv = RTE_KDRV_NONE;
1075         data->drv_name = internal->dev_name;
1076         data->numa_node = numa_node;
1077
1078         /* finally assign rx and tx ops */
1079         eth_dev->rx_pkt_burst = eth_vhost_rx;
1080         eth_dev->tx_pkt_burst = eth_vhost_tx;
1081
1082         return data->port_id;
1083
1084 error:
1085         if (internal)
1086                 free(internal->dev_name);
1087         rte_free(vring_state);
1088         rte_free(eth_addr);
1089         if (eth_dev)
1090                 rte_eth_dev_release_port(eth_dev);
1091         rte_free(internal);
1092         rte_free(list);
1093         rte_free(data);
1094
1095         return -1;
1096 }
1097
1098 static inline int
1099 open_iface(const char *key __rte_unused, const char *value, void *extra_args)
1100 {
1101         const char **iface_name = extra_args;
1102
1103         if (value == NULL)
1104                 return -1;
1105
1106         *iface_name = value;
1107
1108         return 0;
1109 }
1110
1111 static inline int
1112 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1113 {
1114         uint16_t *n = extra_args;
1115
1116         if (value == NULL || extra_args == NULL)
1117                 return -EINVAL;
1118
1119         *n = (uint16_t)strtoul(value, NULL, 0);
1120         if (*n == USHRT_MAX && errno == ERANGE)
1121                 return -1;
1122
1123         return 0;
1124 }
1125
1126 static int
1127 rte_pmd_vhost_devinit(const char *name, const char *params)
1128 {
1129         struct rte_kvargs *kvlist = NULL;
1130         int ret = 0;
1131         char *iface_name;
1132         uint16_t queues;
1133         uint64_t flags = 0;
1134         int client_mode = 0;
1135
1136         RTE_LOG(INFO, PMD, "Initializing pmd_vhost for %s\n", name);
1137
1138         kvlist = rte_kvargs_parse(params, valid_arguments);
1139         if (kvlist == NULL)
1140                 return -1;
1141
1142         if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
1143                 ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
1144                                          &open_iface, &iface_name);
1145                 if (ret < 0)
1146                         goto out_free;
1147         } else {
1148                 ret = -1;
1149                 goto out_free;
1150         }
1151
1152         if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
1153                 ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
1154                                          &open_int, &queues);
1155                 if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
1156                         goto out_free;
1157
1158         } else
1159                 queues = 1;
1160
1161         if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
1162                 ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
1163                                          &open_int, &client_mode);
1164                 if (ret < 0)
1165                         goto out_free;
1166
1167                 if (client_mode)
1168                         flags |= RTE_VHOST_USER_CLIENT;
1169         }
1170
1171         eth_dev_vhost_create(name, iface_name, queues, rte_socket_id(), flags);
1172
1173 out_free:
1174         rte_kvargs_free(kvlist);
1175         return ret;
1176 }
1177
1178 static int
1179 rte_pmd_vhost_devuninit(const char *name)
1180 {
1181         struct rte_eth_dev *eth_dev = NULL;
1182         struct pmd_internal *internal;
1183         struct internal_list *list;
1184         unsigned int i;
1185
1186         RTE_LOG(INFO, PMD, "Un-Initializing pmd_vhost for %s\n", name);
1187
1188         /* find an ethdev entry */
1189         eth_dev = rte_eth_dev_allocated(name);
1190         if (eth_dev == NULL)
1191                 return -ENODEV;
1192
1193         internal = eth_dev->data->dev_private;
1194         if (internal == NULL)
1195                 return -ENODEV;
1196
1197         list = find_internal_resource(internal->iface_name);
1198         if (list == NULL)
1199                 return -ENODEV;
1200
1201         pthread_mutex_lock(&internal_list_lock);
1202         TAILQ_REMOVE(&internal_list, list, next);
1203         pthread_mutex_unlock(&internal_list_lock);
1204         rte_free(list);
1205
1206         eth_dev_stop(eth_dev);
1207
1208         rte_free(vring_states[eth_dev->data->port_id]);
1209         vring_states[eth_dev->data->port_id] = NULL;
1210
1211         free(internal->dev_name);
1212         free(internal->iface_name);
1213
1214         for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
1215                 rte_free(eth_dev->data->rx_queues[i]);
1216         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1217                 rte_free(eth_dev->data->tx_queues[i]);
1218
1219         rte_free(eth_dev->data->mac_addrs);
1220         rte_free(eth_dev->data);
1221         rte_free(internal);
1222
1223         rte_eth_dev_release_port(eth_dev);
1224
1225         return 0;
1226 }
1227
1228 static struct rte_vdev_driver pmd_vhost_drv = {
1229         .init = rte_pmd_vhost_devinit,
1230         .uninit = rte_pmd_vhost_devuninit,
1231 };
1232
1233 DRIVER_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
1234 DRIVER_REGISTER_PARAM_STRING(net_vhost,
1235         "iface=<ifc> "
1236         "queues=<int>");