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