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