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