0d000c71c078b52e22703cb373eb68ceac669704
[dpdk.git] / drivers / net / vhost / rte_eth_vhost.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 IGEL Co., Ltd.
3  * Copyright(c) 2016-2018 Intel Corporation
4  */
5 #include <unistd.h>
6 #include <pthread.h>
7 #include <stdbool.h>
8
9 #include <rte_mbuf.h>
10 #include <rte_ethdev_driver.h>
11 #include <rte_ethdev_vdev.h>
12 #include <rte_malloc.h>
13 #include <rte_memcpy.h>
14 #include <rte_bus_vdev.h>
15 #include <rte_kvargs.h>
16 #include <rte_vhost.h>
17 #include <rte_spinlock.h>
18
19 #include "rte_eth_vhost.h"
20
21 static int vhost_logtype;
22
23 #define VHOST_LOG(level, ...) \
24         rte_log(RTE_LOG_ ## level, vhost_logtype, __VA_ARGS__)
25
26 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
27
28 #define ETH_VHOST_IFACE_ARG             "iface"
29 #define ETH_VHOST_QUEUES_ARG            "queues"
30 #define ETH_VHOST_CLIENT_ARG            "client"
31 #define ETH_VHOST_DEQUEUE_ZERO_COPY     "dequeue-zero-copy"
32 #define ETH_VHOST_IOMMU_SUPPORT         "iommu-support"
33 #define VHOST_MAX_PKT_BURST 32
34
35 static const char *valid_arguments[] = {
36         ETH_VHOST_IFACE_ARG,
37         ETH_VHOST_QUEUES_ARG,
38         ETH_VHOST_CLIENT_ARG,
39         ETH_VHOST_DEQUEUE_ZERO_COPY,
40         ETH_VHOST_IOMMU_SUPPORT,
41         NULL
42 };
43
44 static struct ether_addr base_eth_addr = {
45         .addr_bytes = {
46                 0x56 /* V */,
47                 0x48 /* H */,
48                 0x4F /* O */,
49                 0x53 /* S */,
50                 0x54 /* T */,
51                 0x00
52         }
53 };
54
55 enum vhost_xstats_pkts {
56         VHOST_UNDERSIZE_PKT = 0,
57         VHOST_64_PKT,
58         VHOST_65_TO_127_PKT,
59         VHOST_128_TO_255_PKT,
60         VHOST_256_TO_511_PKT,
61         VHOST_512_TO_1023_PKT,
62         VHOST_1024_TO_1522_PKT,
63         VHOST_1523_TO_MAX_PKT,
64         VHOST_BROADCAST_PKT,
65         VHOST_MULTICAST_PKT,
66         VHOST_UNICAST_PKT,
67         VHOST_ERRORS_PKT,
68         VHOST_ERRORS_FRAGMENTED,
69         VHOST_ERRORS_JABBER,
70         VHOST_UNKNOWN_PROTOCOL,
71         VHOST_XSTATS_MAX,
72 };
73
74 struct vhost_stats {
75         uint64_t pkts;
76         uint64_t bytes;
77         uint64_t missed_pkts;
78         uint64_t xstats[VHOST_XSTATS_MAX];
79 };
80
81 struct vhost_queue {
82         int vid;
83         rte_atomic32_t allow_queuing;
84         rte_atomic32_t while_queuing;
85         struct pmd_internal *internal;
86         struct rte_mempool *mb_pool;
87         uint16_t port;
88         uint16_t virtqueue_id;
89         struct vhost_stats stats;
90 };
91
92 struct pmd_internal {
93         rte_atomic32_t dev_attached;
94         char *dev_name;
95         char *iface_name;
96         uint16_t max_queues;
97         int vid;
98         rte_atomic32_t started;
99         uint8_t vlan_strip;
100 };
101
102 struct internal_list {
103         TAILQ_ENTRY(internal_list) next;
104         struct rte_eth_dev *eth_dev;
105 };
106
107 TAILQ_HEAD(internal_list_head, internal_list);
108 static struct internal_list_head internal_list =
109         TAILQ_HEAD_INITIALIZER(internal_list);
110
111 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
112
113 static struct rte_eth_link pmd_link = {
114                 .link_speed = 10000,
115                 .link_duplex = ETH_LINK_FULL_DUPLEX,
116                 .link_status = ETH_LINK_DOWN
117 };
118
119 struct rte_vhost_vring_state {
120         rte_spinlock_t lock;
121
122         bool cur[RTE_MAX_QUEUES_PER_PORT * 2];
123         bool seen[RTE_MAX_QUEUES_PER_PORT * 2];
124         unsigned int index;
125         unsigned int max_vring;
126 };
127
128 static struct rte_vhost_vring_state *vring_states[RTE_MAX_ETHPORTS];
129
130 #define VHOST_XSTATS_NAME_SIZE 64
131
132 struct vhost_xstats_name_off {
133         char name[VHOST_XSTATS_NAME_SIZE];
134         uint64_t offset;
135 };
136
137 /* [rx]_is prepended to the name string here */
138 static const struct vhost_xstats_name_off vhost_rxport_stat_strings[] = {
139         {"good_packets",
140          offsetof(struct vhost_queue, stats.pkts)},
141         {"total_bytes",
142          offsetof(struct vhost_queue, stats.bytes)},
143         {"missed_pkts",
144          offsetof(struct vhost_queue, stats.missed_pkts)},
145         {"broadcast_packets",
146          offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
147         {"multicast_packets",
148          offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
149         {"unicast_packets",
150          offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
151          {"undersize_packets",
152          offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
153         {"size_64_packets",
154          offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
155         {"size_65_to_127_packets",
156          offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
157         {"size_128_to_255_packets",
158          offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
159         {"size_256_to_511_packets",
160          offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
161         {"size_512_to_1023_packets",
162          offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
163         {"size_1024_to_1522_packets",
164          offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
165         {"size_1523_to_max_packets",
166          offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
167         {"errors_with_bad_CRC",
168          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
169         {"fragmented_errors",
170          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_FRAGMENTED])},
171         {"jabber_errors",
172          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_JABBER])},
173         {"unknown_protos_packets",
174          offsetof(struct vhost_queue, stats.xstats[VHOST_UNKNOWN_PROTOCOL])},
175 };
176
177 /* [tx]_ is prepended to the name string here */
178 static const struct vhost_xstats_name_off vhost_txport_stat_strings[] = {
179         {"good_packets",
180          offsetof(struct vhost_queue, stats.pkts)},
181         {"total_bytes",
182          offsetof(struct vhost_queue, stats.bytes)},
183         {"missed_pkts",
184          offsetof(struct vhost_queue, stats.missed_pkts)},
185         {"broadcast_packets",
186          offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
187         {"multicast_packets",
188          offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
189         {"unicast_packets",
190          offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
191         {"undersize_packets",
192          offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
193         {"size_64_packets",
194          offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
195         {"size_65_to_127_packets",
196          offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
197         {"size_128_to_255_packets",
198          offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
199         {"size_256_to_511_packets",
200          offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
201         {"size_512_to_1023_packets",
202          offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
203         {"size_1024_to_1522_packets",
204          offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
205         {"size_1523_to_max_packets",
206          offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
207         {"errors_with_bad_CRC",
208          offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
209 };
210
211 #define VHOST_NB_XSTATS_RXPORT (sizeof(vhost_rxport_stat_strings) / \
212                                 sizeof(vhost_rxport_stat_strings[0]))
213
214 #define VHOST_NB_XSTATS_TXPORT (sizeof(vhost_txport_stat_strings) / \
215                                 sizeof(vhost_txport_stat_strings[0]))
216
217 static void
218 vhost_dev_xstats_reset(struct rte_eth_dev *dev)
219 {
220         struct vhost_queue *vq = NULL;
221         unsigned int i = 0;
222
223         for (i = 0; i < dev->data->nb_rx_queues; i++) {
224                 vq = dev->data->rx_queues[i];
225                 if (!vq)
226                         continue;
227                 memset(&vq->stats, 0, sizeof(vq->stats));
228         }
229         for (i = 0; i < dev->data->nb_tx_queues; i++) {
230                 vq = dev->data->tx_queues[i];
231                 if (!vq)
232                         continue;
233                 memset(&vq->stats, 0, sizeof(vq->stats));
234         }
235 }
236
237 static int
238 vhost_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
239                            struct rte_eth_xstat_name *xstats_names,
240                            unsigned int limit __rte_unused)
241 {
242         unsigned int t = 0;
243         int count = 0;
244         int nstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
245
246         if (!xstats_names)
247                 return nstats;
248         for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
249                 snprintf(xstats_names[count].name,
250                          sizeof(xstats_names[count].name),
251                          "rx_%s", vhost_rxport_stat_strings[t].name);
252                 count++;
253         }
254         for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
255                 snprintf(xstats_names[count].name,
256                          sizeof(xstats_names[count].name),
257                          "tx_%s", vhost_txport_stat_strings[t].name);
258                 count++;
259         }
260         return count;
261 }
262
263 static int
264 vhost_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
265                      unsigned int n)
266 {
267         unsigned int i;
268         unsigned int t;
269         unsigned int count = 0;
270         struct vhost_queue *vq = NULL;
271         unsigned int nxstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
272
273         if (n < nxstats)
274                 return nxstats;
275
276         for (i = 0; i < dev->data->nb_rx_queues; i++) {
277                 vq = dev->data->rx_queues[i];
278                 if (!vq)
279                         continue;
280                 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
281                                 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
282                                 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
283         }
284         for (i = 0; i < dev->data->nb_tx_queues; i++) {
285                 vq = dev->data->tx_queues[i];
286                 if (!vq)
287                         continue;
288                 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
289                                 + vq->stats.missed_pkts
290                                 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
291                                 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
292         }
293         for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
294                 xstats[count].value = 0;
295                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
296                         vq = dev->data->rx_queues[i];
297                         if (!vq)
298                                 continue;
299                         xstats[count].value +=
300                                 *(uint64_t *)(((char *)vq)
301                                 + vhost_rxport_stat_strings[t].offset);
302                 }
303                 xstats[count].id = count;
304                 count++;
305         }
306         for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
307                 xstats[count].value = 0;
308                 for (i = 0; i < dev->data->nb_tx_queues; i++) {
309                         vq = dev->data->tx_queues[i];
310                         if (!vq)
311                                 continue;
312                         xstats[count].value +=
313                                 *(uint64_t *)(((char *)vq)
314                                 + vhost_txport_stat_strings[t].offset);
315                 }
316                 xstats[count].id = count;
317                 count++;
318         }
319         return count;
320 }
321
322 static inline void
323 vhost_count_multicast_broadcast(struct vhost_queue *vq,
324                                 struct rte_mbuf *mbuf)
325 {
326         struct ether_addr *ea = NULL;
327         struct vhost_stats *pstats = &vq->stats;
328
329         ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
330         if (is_multicast_ether_addr(ea)) {
331                 if (is_broadcast_ether_addr(ea))
332                         pstats->xstats[VHOST_BROADCAST_PKT]++;
333                 else
334                         pstats->xstats[VHOST_MULTICAST_PKT]++;
335         }
336 }
337
338 static void
339 vhost_update_packet_xstats(struct vhost_queue *vq,
340                            struct rte_mbuf **bufs,
341                            uint16_t count)
342 {
343         uint32_t pkt_len = 0;
344         uint64_t i = 0;
345         uint64_t index;
346         struct vhost_stats *pstats = &vq->stats;
347
348         for (i = 0; i < count ; i++) {
349                 pkt_len = bufs[i]->pkt_len;
350                 if (pkt_len == 64) {
351                         pstats->xstats[VHOST_64_PKT]++;
352                 } else if (pkt_len > 64 && pkt_len < 1024) {
353                         index = (sizeof(pkt_len) * 8)
354                                 - __builtin_clz(pkt_len) - 5;
355                         pstats->xstats[index]++;
356                 } else {
357                         if (pkt_len < 64)
358                                 pstats->xstats[VHOST_UNDERSIZE_PKT]++;
359                         else if (pkt_len <= 1522)
360                                 pstats->xstats[VHOST_1024_TO_1522_PKT]++;
361                         else if (pkt_len > 1522)
362                                 pstats->xstats[VHOST_1523_TO_MAX_PKT]++;
363                 }
364                 vhost_count_multicast_broadcast(vq, bufs[i]);
365         }
366 }
367
368 static uint16_t
369 eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
370 {
371         struct vhost_queue *r = q;
372         uint16_t i, nb_rx = 0;
373         uint16_t nb_receive = nb_bufs;
374
375         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
376                 return 0;
377
378         rte_atomic32_set(&r->while_queuing, 1);
379
380         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
381                 goto out;
382
383         /* Dequeue packets from guest TX queue */
384         while (nb_receive) {
385                 uint16_t nb_pkts;
386                 uint16_t num = (uint16_t)RTE_MIN(nb_receive,
387                                                  VHOST_MAX_PKT_BURST);
388
389                 nb_pkts = rte_vhost_dequeue_burst(r->vid, r->virtqueue_id,
390                                                   r->mb_pool, &bufs[nb_rx],
391                                                   num);
392
393                 nb_rx += nb_pkts;
394                 nb_receive -= nb_pkts;
395                 if (nb_pkts < num)
396                         break;
397         }
398
399         r->stats.pkts += nb_rx;
400
401         for (i = 0; likely(i < nb_rx); i++) {
402                 bufs[i]->port = r->port;
403                 bufs[i]->ol_flags = 0;
404                 bufs[i]->vlan_tci = 0;
405
406                 if (r->internal->vlan_strip)
407                         rte_vlan_strip(bufs[i]);
408
409                 r->stats.bytes += bufs[i]->pkt_len;
410         }
411
412         vhost_update_packet_xstats(r, bufs, nb_rx);
413
414 out:
415         rte_atomic32_set(&r->while_queuing, 0);
416
417         return nb_rx;
418 }
419
420 static uint16_t
421 eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
422 {
423         struct vhost_queue *r = q;
424         uint16_t i, nb_tx = 0;
425         uint16_t nb_send = 0;
426
427         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
428                 return 0;
429
430         rte_atomic32_set(&r->while_queuing, 1);
431
432         if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
433                 goto out;
434
435         for (i = 0; i < nb_bufs; i++) {
436                 struct rte_mbuf *m = bufs[i];
437
438                 /* Do VLAN tag insertion */
439                 if (m->ol_flags & PKT_TX_VLAN_PKT) {
440                         int error = rte_vlan_insert(&m);
441                         if (unlikely(error)) {
442                                 rte_pktmbuf_free(m);
443                                 continue;
444                         }
445                 }
446
447                 bufs[nb_send] = m;
448                 ++nb_send;
449         }
450
451         /* Enqueue packets to guest RX queue */
452         while (nb_send) {
453                 uint16_t nb_pkts;
454                 uint16_t num = (uint16_t)RTE_MIN(nb_send,
455                                                  VHOST_MAX_PKT_BURST);
456
457                 nb_pkts = rte_vhost_enqueue_burst(r->vid, r->virtqueue_id,
458                                                   &bufs[nb_tx], num);
459
460                 nb_tx += nb_pkts;
461                 nb_send -= nb_pkts;
462                 if (nb_pkts < num)
463                         break;
464         }
465
466         r->stats.pkts += nb_tx;
467         r->stats.missed_pkts += nb_bufs - nb_tx;
468
469         for (i = 0; likely(i < nb_tx); i++)
470                 r->stats.bytes += bufs[i]->pkt_len;
471
472         vhost_update_packet_xstats(r, bufs, nb_tx);
473
474         /* According to RFC2863 page42 section ifHCOutMulticastPkts and
475          * ifHCOutBroadcastPkts, the counters "multicast" and "broadcast"
476          * are increased when packets are not transmitted successfully.
477          */
478         for (i = nb_tx; i < nb_bufs; i++)
479                 vhost_count_multicast_broadcast(r, bufs[i]);
480
481         for (i = 0; likely(i < nb_tx); i++)
482                 rte_pktmbuf_free(bufs[i]);
483 out:
484         rte_atomic32_set(&r->while_queuing, 0);
485
486         return nb_tx;
487 }
488
489 static int
490 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
491 {
492         struct pmd_internal *internal = dev->data->dev_private;
493         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
494
495         internal->vlan_strip = rxmode->hw_vlan_strip;
496
497         if (rxmode->hw_vlan_filter)
498                 VHOST_LOG(WARNING,
499                         "vhost(%s): vlan filtering not available\n",
500                         internal->dev_name);
501
502         return 0;
503 }
504
505 static inline struct internal_list *
506 find_internal_resource(char *ifname)
507 {
508         int found = 0;
509         struct internal_list *list;
510         struct pmd_internal *internal;
511
512         if (ifname == NULL)
513                 return NULL;
514
515         pthread_mutex_lock(&internal_list_lock);
516
517         TAILQ_FOREACH(list, &internal_list, next) {
518                 internal = list->eth_dev->data->dev_private;
519                 if (!strcmp(internal->iface_name, ifname)) {
520                         found = 1;
521                         break;
522                 }
523         }
524
525         pthread_mutex_unlock(&internal_list_lock);
526
527         if (!found)
528                 return NULL;
529
530         return list;
531 }
532
533 static int
534 eth_rxq_intr_enable(struct rte_eth_dev *dev, uint16_t qid)
535 {
536         struct rte_vhost_vring vring;
537         struct vhost_queue *vq;
538         int ret = 0;
539
540         vq = dev->data->rx_queues[qid];
541         if (!vq) {
542                 VHOST_LOG(ERR, "rxq%d is not setup yet\n", qid);
543                 return -1;
544         }
545
546         ret = rte_vhost_get_vhost_vring(vq->vid, (qid << 1) + 1, &vring);
547         if (ret < 0) {
548                 VHOST_LOG(ERR, "Failed to get rxq%d's vring\n", qid);
549                 return ret;
550         }
551         VHOST_LOG(INFO, "Enable interrupt for rxq%d\n", qid);
552         rte_vhost_enable_guest_notification(vq->vid, (qid << 1) + 1, 1);
553         rte_wmb();
554
555         return ret;
556 }
557
558 static int
559 eth_rxq_intr_disable(struct rte_eth_dev *dev, uint16_t qid)
560 {
561         struct rte_vhost_vring vring;
562         struct vhost_queue *vq;
563         int ret = 0;
564
565         vq = dev->data->rx_queues[qid];
566         if (!vq) {
567                 VHOST_LOG(ERR, "rxq%d is not setup yet\n", qid);
568                 return -1;
569         }
570
571         ret = rte_vhost_get_vhost_vring(vq->vid, (qid << 1) + 1, &vring);
572         if (ret < 0) {
573                 VHOST_LOG(ERR, "Failed to get rxq%d's vring", qid);
574                 return ret;
575         }
576         VHOST_LOG(INFO, "Disable interrupt for rxq%d\n", qid);
577         rte_vhost_enable_guest_notification(vq->vid, (qid << 1) + 1, 0);
578         rte_wmb();
579
580         return 0;
581 }
582
583 static void
584 eth_vhost_uninstall_intr(struct rte_eth_dev *dev)
585 {
586         struct rte_intr_handle *intr_handle = dev->intr_handle;
587
588         if (intr_handle) {
589                 if (intr_handle->intr_vec)
590                         free(intr_handle->intr_vec);
591                 free(intr_handle);
592         }
593
594         dev->intr_handle = NULL;
595 }
596
597 static int
598 eth_vhost_install_intr(struct rte_eth_dev *dev)
599 {
600         struct rte_vhost_vring vring;
601         struct vhost_queue *vq;
602         int count = 0;
603         int nb_rxq = dev->data->nb_rx_queues;
604         int i;
605         int ret;
606
607         /* uninstall firstly if we are reconnecting */
608         if (dev->intr_handle)
609                 eth_vhost_uninstall_intr(dev);
610
611         dev->intr_handle = malloc(sizeof(*dev->intr_handle));
612         if (!dev->intr_handle) {
613                 VHOST_LOG(ERR, "Fail to allocate intr_handle\n");
614                 return -ENOMEM;
615         }
616         memset(dev->intr_handle, 0, sizeof(*dev->intr_handle));
617
618         dev->intr_handle->efd_counter_size = sizeof(uint64_t);
619
620         dev->intr_handle->intr_vec =
621                 malloc(nb_rxq * sizeof(dev->intr_handle->intr_vec[0]));
622
623         if (!dev->intr_handle->intr_vec) {
624                 VHOST_LOG(ERR,
625                         "Failed to allocate memory for interrupt vector\n");
626                 free(dev->intr_handle);
627                 return -ENOMEM;
628         }
629
630         VHOST_LOG(INFO, "Prepare intr vec\n");
631         for (i = 0; i < nb_rxq; i++) {
632                 vq = dev->data->rx_queues[i];
633                 if (!vq) {
634                         VHOST_LOG(INFO, "rxq-%d not setup yet, skip!\n", i);
635                         continue;
636                 }
637
638                 ret = rte_vhost_get_vhost_vring(vq->vid, (i << 1) + 1, &vring);
639                 if (ret < 0) {
640                         VHOST_LOG(INFO,
641                                 "Failed to get rxq-%d's vring, skip!\n", i);
642                         continue;
643                 }
644
645                 if (vring.kickfd < 0) {
646                         VHOST_LOG(INFO,
647                                 "rxq-%d's kickfd is invalid, skip!\n", i);
648                         continue;
649                 }
650                 dev->intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + i;
651                 dev->intr_handle->efds[i] = vring.kickfd;
652                 count++;
653                 VHOST_LOG(INFO, "Installed intr vec for rxq-%d\n", i);
654         }
655
656         dev->intr_handle->nb_efd = count;
657         dev->intr_handle->max_intr = count + 1;
658         dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
659
660         return 0;
661 }
662
663 static void
664 update_queuing_status(struct rte_eth_dev *dev)
665 {
666         struct pmd_internal *internal = dev->data->dev_private;
667         struct vhost_queue *vq;
668         unsigned int i;
669         int allow_queuing = 1;
670
671         if (!dev->data->rx_queues || !dev->data->tx_queues)
672                 return;
673
674         if (rte_atomic32_read(&internal->started) == 0 ||
675             rte_atomic32_read(&internal->dev_attached) == 0)
676                 allow_queuing = 0;
677
678         /* Wait until rx/tx_pkt_burst stops accessing vhost device */
679         for (i = 0; i < dev->data->nb_rx_queues; i++) {
680                 vq = dev->data->rx_queues[i];
681                 if (vq == NULL)
682                         continue;
683                 rte_atomic32_set(&vq->allow_queuing, allow_queuing);
684                 while (rte_atomic32_read(&vq->while_queuing))
685                         rte_pause();
686         }
687
688         for (i = 0; i < dev->data->nb_tx_queues; i++) {
689                 vq = dev->data->tx_queues[i];
690                 if (vq == NULL)
691                         continue;
692                 rte_atomic32_set(&vq->allow_queuing, allow_queuing);
693                 while (rte_atomic32_read(&vq->while_queuing))
694                         rte_pause();
695         }
696 }
697
698 static void
699 queue_setup(struct rte_eth_dev *eth_dev, struct pmd_internal *internal)
700 {
701         struct vhost_queue *vq;
702         int i;
703
704         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
705                 vq = eth_dev->data->rx_queues[i];
706                 if (!vq)
707                         continue;
708                 vq->vid = internal->vid;
709                 vq->internal = internal;
710                 vq->port = eth_dev->data->port_id;
711         }
712         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
713                 vq = eth_dev->data->tx_queues[i];
714                 if (!vq)
715                         continue;
716                 vq->vid = internal->vid;
717                 vq->internal = internal;
718                 vq->port = eth_dev->data->port_id;
719         }
720 }
721
722 static int
723 new_device(int vid)
724 {
725         struct rte_eth_dev *eth_dev;
726         struct internal_list *list;
727         struct pmd_internal *internal;
728         struct rte_eth_conf *dev_conf;
729         unsigned i;
730         char ifname[PATH_MAX];
731 #ifdef RTE_LIBRTE_VHOST_NUMA
732         int newnode;
733 #endif
734
735         rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
736         list = find_internal_resource(ifname);
737         if (list == NULL) {
738                 VHOST_LOG(INFO, "Invalid device name: %s\n", ifname);
739                 return -1;
740         }
741
742         eth_dev = list->eth_dev;
743         internal = eth_dev->data->dev_private;
744         dev_conf = &eth_dev->data->dev_conf;
745
746 #ifdef RTE_LIBRTE_VHOST_NUMA
747         newnode = rte_vhost_get_numa_node(vid);
748         if (newnode >= 0)
749                 eth_dev->data->numa_node = newnode;
750 #endif
751
752         internal->vid = vid;
753         if (rte_atomic32_read(&internal->started) == 1) {
754                 queue_setup(eth_dev, internal);
755
756                 if (dev_conf->intr_conf.rxq) {
757                         if (eth_vhost_install_intr(eth_dev) < 0) {
758                                 VHOST_LOG(INFO,
759                                         "Failed to install interrupt handler.");
760                                         return -1;
761                         }
762                 }
763         } else {
764                 VHOST_LOG(INFO, "RX/TX queues not exist yet\n");
765         }
766
767         for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
768                 rte_vhost_enable_guest_notification(vid, i, 0);
769
770         rte_vhost_get_mtu(vid, &eth_dev->data->mtu);
771
772         eth_dev->data->dev_link.link_status = ETH_LINK_UP;
773
774         rte_atomic32_set(&internal->dev_attached, 1);
775         update_queuing_status(eth_dev);
776
777         VHOST_LOG(INFO, "Vhost device %d created\n", vid);
778
779         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
780
781         return 0;
782 }
783
784 static void
785 destroy_device(int vid)
786 {
787         struct rte_eth_dev *eth_dev;
788         struct pmd_internal *internal;
789         struct vhost_queue *vq;
790         struct internal_list *list;
791         char ifname[PATH_MAX];
792         unsigned i;
793         struct rte_vhost_vring_state *state;
794
795         rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
796         list = find_internal_resource(ifname);
797         if (list == NULL) {
798                 VHOST_LOG(ERR, "Invalid interface name: %s\n", ifname);
799                 return;
800         }
801         eth_dev = list->eth_dev;
802         internal = eth_dev->data->dev_private;
803
804         rte_atomic32_set(&internal->dev_attached, 0);
805         update_queuing_status(eth_dev);
806
807         eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
808
809         if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
810                 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
811                         vq = eth_dev->data->rx_queues[i];
812                         if (!vq)
813                                 continue;
814                         vq->vid = -1;
815                 }
816                 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
817                         vq = eth_dev->data->tx_queues[i];
818                         if (!vq)
819                                 continue;
820                         vq->vid = -1;
821                 }
822         }
823
824         state = vring_states[eth_dev->data->port_id];
825         rte_spinlock_lock(&state->lock);
826         for (i = 0; i <= state->max_vring; i++) {
827                 state->cur[i] = false;
828                 state->seen[i] = false;
829         }
830         state->max_vring = 0;
831         rte_spinlock_unlock(&state->lock);
832
833         VHOST_LOG(INFO, "Vhost device %d destroyed\n", vid);
834         eth_vhost_uninstall_intr(eth_dev);
835
836         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
837 }
838
839 static int
840 vring_state_changed(int vid, uint16_t vring, int enable)
841 {
842         struct rte_vhost_vring_state *state;
843         struct rte_eth_dev *eth_dev;
844         struct internal_list *list;
845         char ifname[PATH_MAX];
846
847         rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
848         list = find_internal_resource(ifname);
849         if (list == NULL) {
850                 VHOST_LOG(ERR, "Invalid interface name: %s\n", ifname);
851                 return -1;
852         }
853
854         eth_dev = list->eth_dev;
855         /* won't be NULL */
856         state = vring_states[eth_dev->data->port_id];
857         rte_spinlock_lock(&state->lock);
858         state->cur[vring] = enable;
859         state->max_vring = RTE_MAX(vring, state->max_vring);
860         rte_spinlock_unlock(&state->lock);
861
862         VHOST_LOG(INFO, "vring%u is %s\n",
863                         vring, enable ? "enabled" : "disabled");
864
865         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE, NULL);
866
867         return 0;
868 }
869
870 static struct vhost_device_ops vhost_ops = {
871         .new_device          = new_device,
872         .destroy_device      = destroy_device,
873         .vring_state_changed = vring_state_changed,
874 };
875
876 int
877 rte_eth_vhost_get_queue_event(uint16_t port_id,
878                 struct rte_eth_vhost_queue_event *event)
879 {
880         struct rte_vhost_vring_state *state;
881         unsigned int i;
882         int idx;
883
884         if (port_id >= RTE_MAX_ETHPORTS) {
885                 VHOST_LOG(ERR, "Invalid port id\n");
886                 return -1;
887         }
888
889         state = vring_states[port_id];
890         if (!state) {
891                 VHOST_LOG(ERR, "Unused port\n");
892                 return -1;
893         }
894
895         rte_spinlock_lock(&state->lock);
896         for (i = 0; i <= state->max_vring; i++) {
897                 idx = state->index++ % (state->max_vring + 1);
898
899                 if (state->cur[idx] != state->seen[idx]) {
900                         state->seen[idx] = state->cur[idx];
901                         event->queue_id = idx / 2;
902                         event->rx = idx & 1;
903                         event->enable = state->cur[idx];
904                         rte_spinlock_unlock(&state->lock);
905                         return 0;
906                 }
907         }
908         rte_spinlock_unlock(&state->lock);
909
910         return -1;
911 }
912
913 int
914 rte_eth_vhost_get_vid_from_port_id(uint16_t port_id)
915 {
916         struct internal_list *list;
917         struct rte_eth_dev *eth_dev;
918         struct vhost_queue *vq;
919         int vid = -1;
920
921         if (!rte_eth_dev_is_valid_port(port_id))
922                 return -1;
923
924         pthread_mutex_lock(&internal_list_lock);
925
926         TAILQ_FOREACH(list, &internal_list, next) {
927                 eth_dev = list->eth_dev;
928                 if (eth_dev->data->port_id == port_id) {
929                         vq = eth_dev->data->rx_queues[0];
930                         if (vq) {
931                                 vid = vq->vid;
932                         }
933                         break;
934                 }
935         }
936
937         pthread_mutex_unlock(&internal_list_lock);
938
939         return vid;
940 }
941
942 static int
943 eth_dev_start(struct rte_eth_dev *eth_dev)
944 {
945         struct pmd_internal *internal = eth_dev->data->dev_private;
946         struct rte_eth_conf *dev_conf = &eth_dev->data->dev_conf;
947
948         queue_setup(eth_dev, internal);
949
950         if (rte_atomic32_read(&internal->dev_attached) == 1) {
951                 if (dev_conf->intr_conf.rxq) {
952                         if (eth_vhost_install_intr(eth_dev) < 0) {
953                                 VHOST_LOG(INFO,
954                                         "Failed to install interrupt handler.");
955                                         return -1;
956                         }
957                 }
958         }
959
960         rte_atomic32_set(&internal->started, 1);
961         update_queuing_status(eth_dev);
962
963         return 0;
964 }
965
966 static void
967 eth_dev_stop(struct rte_eth_dev *dev)
968 {
969         struct pmd_internal *internal = dev->data->dev_private;
970
971         rte_atomic32_set(&internal->started, 0);
972         update_queuing_status(dev);
973 }
974
975 static void
976 eth_dev_close(struct rte_eth_dev *dev)
977 {
978         struct pmd_internal *internal;
979         struct internal_list *list;
980         unsigned int i;
981
982         internal = dev->data->dev_private;
983         if (!internal)
984                 return;
985
986         eth_dev_stop(dev);
987
988         rte_vhost_driver_unregister(internal->iface_name);
989
990         list = find_internal_resource(internal->iface_name);
991         if (!list)
992                 return;
993
994         pthread_mutex_lock(&internal_list_lock);
995         TAILQ_REMOVE(&internal_list, list, next);
996         pthread_mutex_unlock(&internal_list_lock);
997         rte_free(list);
998
999         if (dev->data->rx_queues)
1000                 for (i = 0; i < dev->data->nb_rx_queues; i++)
1001                         rte_free(dev->data->rx_queues[i]);
1002
1003         if (dev->data->tx_queues)
1004                 for (i = 0; i < dev->data->nb_tx_queues; i++)
1005                         rte_free(dev->data->tx_queues[i]);
1006
1007         rte_free(dev->data->mac_addrs);
1008         free(internal->dev_name);
1009         free(internal->iface_name);
1010         rte_free(internal);
1011
1012         dev->data->dev_private = NULL;
1013 }
1014
1015 static int
1016 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
1017                    uint16_t nb_rx_desc __rte_unused,
1018                    unsigned int socket_id,
1019                    const struct rte_eth_rxconf *rx_conf __rte_unused,
1020                    struct rte_mempool *mb_pool)
1021 {
1022         struct vhost_queue *vq;
1023
1024         vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
1025                         RTE_CACHE_LINE_SIZE, socket_id);
1026         if (vq == NULL) {
1027                 VHOST_LOG(ERR, "Failed to allocate memory for rx queue\n");
1028                 return -ENOMEM;
1029         }
1030
1031         vq->mb_pool = mb_pool;
1032         vq->virtqueue_id = rx_queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
1033         dev->data->rx_queues[rx_queue_id] = vq;
1034
1035         return 0;
1036 }
1037
1038 static int
1039 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
1040                    uint16_t nb_tx_desc __rte_unused,
1041                    unsigned int socket_id,
1042                    const struct rte_eth_txconf *tx_conf __rte_unused)
1043 {
1044         struct vhost_queue *vq;
1045
1046         vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
1047                         RTE_CACHE_LINE_SIZE, socket_id);
1048         if (vq == NULL) {
1049                 VHOST_LOG(ERR, "Failed to allocate memory for tx queue\n");
1050                 return -ENOMEM;
1051         }
1052
1053         vq->virtqueue_id = tx_queue_id * VIRTIO_QNUM + VIRTIO_RXQ;
1054         dev->data->tx_queues[tx_queue_id] = vq;
1055
1056         return 0;
1057 }
1058
1059 static void
1060 eth_dev_info(struct rte_eth_dev *dev,
1061              struct rte_eth_dev_info *dev_info)
1062 {
1063         struct pmd_internal *internal;
1064
1065         internal = dev->data->dev_private;
1066         if (internal == NULL) {
1067                 VHOST_LOG(ERR, "Invalid device specified\n");
1068                 return;
1069         }
1070
1071         dev_info->max_mac_addrs = 1;
1072         dev_info->max_rx_pktlen = (uint32_t)-1;
1073         dev_info->max_rx_queues = internal->max_queues;
1074         dev_info->max_tx_queues = internal->max_queues;
1075         dev_info->min_rx_bufsize = 0;
1076 }
1077
1078 static int
1079 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1080 {
1081         unsigned i;
1082         unsigned long rx_total = 0, tx_total = 0, tx_missed_total = 0;
1083         unsigned long rx_total_bytes = 0, tx_total_bytes = 0;
1084         struct vhost_queue *vq;
1085
1086         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
1087                         i < dev->data->nb_rx_queues; i++) {
1088                 if (dev->data->rx_queues[i] == NULL)
1089                         continue;
1090                 vq = dev->data->rx_queues[i];
1091                 stats->q_ipackets[i] = vq->stats.pkts;
1092                 rx_total += stats->q_ipackets[i];
1093
1094                 stats->q_ibytes[i] = vq->stats.bytes;
1095                 rx_total_bytes += stats->q_ibytes[i];
1096         }
1097
1098         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
1099                         i < dev->data->nb_tx_queues; i++) {
1100                 if (dev->data->tx_queues[i] == NULL)
1101                         continue;
1102                 vq = dev->data->tx_queues[i];
1103                 stats->q_opackets[i] = vq->stats.pkts;
1104                 tx_missed_total += vq->stats.missed_pkts;
1105                 tx_total += stats->q_opackets[i];
1106
1107                 stats->q_obytes[i] = vq->stats.bytes;
1108                 tx_total_bytes += stats->q_obytes[i];
1109         }
1110
1111         stats->ipackets = rx_total;
1112         stats->opackets = tx_total;
1113         stats->oerrors = tx_missed_total;
1114         stats->ibytes = rx_total_bytes;
1115         stats->obytes = tx_total_bytes;
1116
1117         return 0;
1118 }
1119
1120 static void
1121 eth_stats_reset(struct rte_eth_dev *dev)
1122 {
1123         struct vhost_queue *vq;
1124         unsigned i;
1125
1126         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1127                 if (dev->data->rx_queues[i] == NULL)
1128                         continue;
1129                 vq = dev->data->rx_queues[i];
1130                 vq->stats.pkts = 0;
1131                 vq->stats.bytes = 0;
1132         }
1133         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1134                 if (dev->data->tx_queues[i] == NULL)
1135                         continue;
1136                 vq = dev->data->tx_queues[i];
1137                 vq->stats.pkts = 0;
1138                 vq->stats.bytes = 0;
1139                 vq->stats.missed_pkts = 0;
1140         }
1141 }
1142
1143 static void
1144 eth_queue_release(void *q)
1145 {
1146         rte_free(q);
1147 }
1148
1149 static int
1150 eth_tx_done_cleanup(void *txq __rte_unused, uint32_t free_cnt __rte_unused)
1151 {
1152         /*
1153          * vHost does not hang onto mbuf. eth_vhost_tx() copies packet data
1154          * and releases mbuf, so nothing to cleanup.
1155          */
1156         return 0;
1157 }
1158
1159 static int
1160 eth_link_update(struct rte_eth_dev *dev __rte_unused,
1161                 int wait_to_complete __rte_unused)
1162 {
1163         return 0;
1164 }
1165
1166 static uint32_t
1167 eth_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1168 {
1169         struct vhost_queue *vq;
1170
1171         vq = dev->data->rx_queues[rx_queue_id];
1172         if (vq == NULL)
1173                 return 0;
1174
1175         return rte_vhost_rx_queue_count(vq->vid, vq->virtqueue_id);
1176 }
1177
1178 static const struct eth_dev_ops ops = {
1179         .dev_start = eth_dev_start,
1180         .dev_stop = eth_dev_stop,
1181         .dev_close = eth_dev_close,
1182         .dev_configure = eth_dev_configure,
1183         .dev_infos_get = eth_dev_info,
1184         .rx_queue_setup = eth_rx_queue_setup,
1185         .tx_queue_setup = eth_tx_queue_setup,
1186         .rx_queue_release = eth_queue_release,
1187         .tx_queue_release = eth_queue_release,
1188         .tx_done_cleanup = eth_tx_done_cleanup,
1189         .rx_queue_count = eth_rx_queue_count,
1190         .link_update = eth_link_update,
1191         .stats_get = eth_stats_get,
1192         .stats_reset = eth_stats_reset,
1193         .xstats_reset = vhost_dev_xstats_reset,
1194         .xstats_get = vhost_dev_xstats_get,
1195         .xstats_get_names = vhost_dev_xstats_get_names,
1196         .rx_queue_intr_enable = eth_rxq_intr_enable,
1197         .rx_queue_intr_disable = eth_rxq_intr_disable,
1198 };
1199
1200 static struct rte_vdev_driver pmd_vhost_drv;
1201
1202 static int
1203 eth_dev_vhost_create(struct rte_vdev_device *dev, char *iface_name,
1204         int16_t queues, const unsigned int numa_node, uint64_t flags)
1205 {
1206         const char *name = rte_vdev_device_name(dev);
1207         struct rte_eth_dev_data *data;
1208         struct pmd_internal *internal = NULL;
1209         struct rte_eth_dev *eth_dev = NULL;
1210         struct ether_addr *eth_addr = NULL;
1211         struct rte_vhost_vring_state *vring_state = NULL;
1212         struct internal_list *list = NULL;
1213
1214         VHOST_LOG(INFO, "Creating VHOST-USER backend on numa socket %u\n",
1215                 numa_node);
1216
1217         list = rte_zmalloc_socket(name, sizeof(*list), 0, numa_node);
1218         if (list == NULL)
1219                 goto error;
1220
1221         /* reserve an ethdev entry */
1222         eth_dev = rte_eth_vdev_allocate(dev, sizeof(*internal));
1223         if (eth_dev == NULL)
1224                 goto error;
1225
1226         eth_addr = rte_zmalloc_socket(name, sizeof(*eth_addr), 0, numa_node);
1227         if (eth_addr == NULL)
1228                 goto error;
1229         *eth_addr = base_eth_addr;
1230         eth_addr->addr_bytes[5] = eth_dev->data->port_id;
1231
1232         vring_state = rte_zmalloc_socket(name,
1233                         sizeof(*vring_state), 0, numa_node);
1234         if (vring_state == NULL)
1235                 goto error;
1236
1237         /* now put it all together
1238          * - store queue data in internal,
1239          * - point eth_dev_data to internals
1240          * - and point eth_dev structure to new eth_dev_data structure
1241          */
1242         internal = eth_dev->data->dev_private;
1243         internal->dev_name = strdup(name);
1244         if (internal->dev_name == NULL)
1245                 goto error;
1246         internal->iface_name = strdup(iface_name);
1247         if (internal->iface_name == NULL)
1248                 goto error;
1249
1250         list->eth_dev = eth_dev;
1251         pthread_mutex_lock(&internal_list_lock);
1252         TAILQ_INSERT_TAIL(&internal_list, list, next);
1253         pthread_mutex_unlock(&internal_list_lock);
1254
1255         rte_spinlock_init(&vring_state->lock);
1256         vring_states[eth_dev->data->port_id] = vring_state;
1257
1258         data = eth_dev->data;
1259         data->nb_rx_queues = queues;
1260         data->nb_tx_queues = queues;
1261         internal->max_queues = queues;
1262         internal->vid = -1;
1263         data->dev_link = pmd_link;
1264         data->mac_addrs = eth_addr;
1265         data->dev_flags = RTE_ETH_DEV_INTR_LSC;
1266
1267         eth_dev->dev_ops = &ops;
1268
1269         /* finally assign rx and tx ops */
1270         eth_dev->rx_pkt_burst = eth_vhost_rx;
1271         eth_dev->tx_pkt_burst = eth_vhost_tx;
1272
1273         if (rte_vhost_driver_register(iface_name, flags))
1274                 goto error;
1275
1276         if (rte_vhost_driver_callback_register(iface_name, &vhost_ops) < 0) {
1277                 VHOST_LOG(ERR, "Can't register callbacks\n");
1278                 goto error;
1279         }
1280
1281         if (rte_vhost_driver_start(iface_name) < 0) {
1282                 VHOST_LOG(ERR, "Failed to start driver for %s\n",
1283                         iface_name);
1284                 goto error;
1285         }
1286
1287         rte_eth_dev_probing_finish(eth_dev);
1288         return data->port_id;
1289
1290 error:
1291         if (internal) {
1292                 free(internal->iface_name);
1293                 free(internal->dev_name);
1294         }
1295         rte_free(vring_state);
1296         rte_free(eth_addr);
1297         if (eth_dev)
1298                 rte_eth_dev_release_port(eth_dev);
1299         rte_free(internal);
1300         rte_free(list);
1301
1302         return -1;
1303 }
1304
1305 static inline int
1306 open_iface(const char *key __rte_unused, const char *value, void *extra_args)
1307 {
1308         const char **iface_name = extra_args;
1309
1310         if (value == NULL)
1311                 return -1;
1312
1313         *iface_name = value;
1314
1315         return 0;
1316 }
1317
1318 static inline int
1319 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1320 {
1321         uint16_t *n = extra_args;
1322
1323         if (value == NULL || extra_args == NULL)
1324                 return -EINVAL;
1325
1326         *n = (uint16_t)strtoul(value, NULL, 0);
1327         if (*n == USHRT_MAX && errno == ERANGE)
1328                 return -1;
1329
1330         return 0;
1331 }
1332
1333 static int
1334 rte_pmd_vhost_probe(struct rte_vdev_device *dev)
1335 {
1336         struct rte_kvargs *kvlist = NULL;
1337         int ret = 0;
1338         char *iface_name;
1339         uint16_t queues;
1340         uint64_t flags = 0;
1341         int client_mode = 0;
1342         int dequeue_zero_copy = 0;
1343         int iommu_support = 0;
1344         struct rte_eth_dev *eth_dev;
1345         const char *name = rte_vdev_device_name(dev);
1346
1347         VHOST_LOG(INFO, "Initializing pmd_vhost for %s\n", name);
1348
1349         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1350             strlen(rte_vdev_device_args(dev)) == 0) {
1351                 eth_dev = rte_eth_dev_attach_secondary(name);
1352                 if (!eth_dev) {
1353                         VHOST_LOG(ERR, "Failed to probe %s\n", name);
1354                         return -1;
1355                 }
1356                 /* TODO: request info from primary to set up Rx and Tx */
1357                 eth_dev->dev_ops = &ops;
1358                 rte_eth_dev_probing_finish(eth_dev);
1359                 return 0;
1360         }
1361
1362         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1363         if (kvlist == NULL)
1364                 return -1;
1365
1366         if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
1367                 ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
1368                                          &open_iface, &iface_name);
1369                 if (ret < 0)
1370                         goto out_free;
1371         } else {
1372                 ret = -1;
1373                 goto out_free;
1374         }
1375
1376         if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
1377                 ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
1378                                          &open_int, &queues);
1379                 if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
1380                         goto out_free;
1381
1382         } else
1383                 queues = 1;
1384
1385         if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
1386                 ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
1387                                          &open_int, &client_mode);
1388                 if (ret < 0)
1389                         goto out_free;
1390
1391                 if (client_mode)
1392                         flags |= RTE_VHOST_USER_CLIENT;
1393         }
1394
1395         if (rte_kvargs_count(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY) == 1) {
1396                 ret = rte_kvargs_process(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY,
1397                                          &open_int, &dequeue_zero_copy);
1398                 if (ret < 0)
1399                         goto out_free;
1400
1401                 if (dequeue_zero_copy)
1402                         flags |= RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
1403         }
1404
1405         if (rte_kvargs_count(kvlist, ETH_VHOST_IOMMU_SUPPORT) == 1) {
1406                 ret = rte_kvargs_process(kvlist, ETH_VHOST_IOMMU_SUPPORT,
1407                                          &open_int, &iommu_support);
1408                 if (ret < 0)
1409                         goto out_free;
1410
1411                 if (iommu_support)
1412                         flags |= RTE_VHOST_USER_IOMMU_SUPPORT;
1413         }
1414
1415         if (dev->device.numa_node == SOCKET_ID_ANY)
1416                 dev->device.numa_node = rte_socket_id();
1417
1418         eth_dev_vhost_create(dev, iface_name, queues, dev->device.numa_node,
1419                 flags);
1420
1421 out_free:
1422         rte_kvargs_free(kvlist);
1423         return ret;
1424 }
1425
1426 static int
1427 rte_pmd_vhost_remove(struct rte_vdev_device *dev)
1428 {
1429         const char *name;
1430         struct rte_eth_dev *eth_dev = NULL;
1431
1432         name = rte_vdev_device_name(dev);
1433         VHOST_LOG(INFO, "Un-Initializing pmd_vhost for %s\n", name);
1434
1435         /* find an ethdev entry */
1436         eth_dev = rte_eth_dev_allocated(name);
1437         if (eth_dev == NULL)
1438                 return -ENODEV;
1439
1440         eth_dev_close(eth_dev);
1441
1442         rte_free(vring_states[eth_dev->data->port_id]);
1443         vring_states[eth_dev->data->port_id] = NULL;
1444
1445         rte_eth_dev_release_port(eth_dev);
1446
1447         return 0;
1448 }
1449
1450 static struct rte_vdev_driver pmd_vhost_drv = {
1451         .probe = rte_pmd_vhost_probe,
1452         .remove = rte_pmd_vhost_remove,
1453 };
1454
1455 RTE_PMD_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
1456 RTE_PMD_REGISTER_ALIAS(net_vhost, eth_vhost);
1457 RTE_PMD_REGISTER_PARAM_STRING(net_vhost,
1458         "iface=<ifc> "
1459         "queues=<int>");
1460
1461 RTE_INIT(vhost_init_log);
1462 static void
1463 vhost_init_log(void)
1464 {
1465         vhost_logtype = rte_log_register("pmd.net.vhost");
1466         if (vhost_logtype >= 0)
1467                 rte_log_set_level(vhost_logtype, RTE_LOG_NOTICE);
1468 }