Imported Upstream version 16.04
[deb_dpdk.git] / drivers / net / xenvirt / rte_eth_xenvirt.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
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 Intel Corporation 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
34 #include <stdint.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/mman.h>
40 #include <errno.h>
41 #include <sys/user.h>
42 #include <linux/binfmts.h>
43 #include <xen/xen-compat.h>
44 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x00040200
45 #include <xs.h>
46 #else
47 #include <xenstore.h>
48 #endif
49 #include <linux/virtio_ring.h>
50
51 #include <rte_mbuf.h>
52 #include <rte_ethdev.h>
53 #include <rte_malloc.h>
54 #include <rte_memcpy.h>
55 #include <rte_string_fns.h>
56 #include <rte_dev.h>
57 #include <cmdline_parse.h>
58 #include <cmdline_parse_etheraddr.h>
59
60 #include "rte_xen_lib.h"
61 #include "virtqueue.h"
62 #include "rte_eth_xenvirt.h"
63
64 #define VQ_DESC_NUM 256
65 #define VIRTIO_MBUF_BURST_SZ 64
66
67 /* virtio_idx is increased after new device is created.*/
68 static int virtio_idx = 0;
69
70 static const char *drivername = "xen virtio PMD";
71
72 static struct rte_eth_link pmd_link = {
73                 .link_speed = ETH_SPEED_NUM_10G,
74                 .link_duplex = ETH_LINK_FULL_DUPLEX,
75                 .link_status = ETH_LINK_DOWN,
76                 .link_autoneg = ETH_LINK_SPEED_FIXED
77 };
78
79 static void
80 eth_xenvirt_free_queues(struct rte_eth_dev *dev);
81
82 static inline struct rte_mbuf *
83 rte_rxmbuf_alloc(struct rte_mempool *mp)
84 {
85         struct rte_mbuf *m;
86
87         m = __rte_mbuf_raw_alloc(mp);
88         __rte_mbuf_sanity_check_raw(m, 0);
89
90         return m;
91 }
92
93
94 static uint16_t
95 eth_xenvirt_rx(void *q, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
96 {
97         struct virtqueue *rxvq = q;
98         struct rte_mbuf *rxm, *new_mbuf;
99         uint16_t nb_used, num;
100         uint32_t len[VIRTIO_MBUF_BURST_SZ];
101         uint32_t i;
102         struct pmd_internals *pi = rxvq->internals;
103
104         nb_used = VIRTQUEUE_NUSED(rxvq);
105
106         rte_smp_rmb();
107         num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
108         num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
109         if (unlikely(num == 0)) return 0;
110
111         num = virtqueue_dequeue_burst(rxvq, rx_pkts, len, num);
112         PMD_RX_LOG(DEBUG, "used:%d dequeue:%d\n", nb_used, num);
113         for (i = 0; i < num ; i ++) {
114                 rxm = rx_pkts[i];
115                 PMD_RX_LOG(DEBUG, "packet len:%d\n", len[i]);
116                 rxm->next = NULL;
117                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
118                 rxm->data_len = (uint16_t)(len[i] - sizeof(struct virtio_net_hdr));
119                 rxm->nb_segs = 1;
120                 rxm->port = pi->port_id;
121                 rxm->pkt_len  = (uint32_t)(len[i] - sizeof(struct virtio_net_hdr));
122         }
123         /* allocate new mbuf for the used descriptor */
124         while (likely(!virtqueue_full(rxvq))) {
125                 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
126                 if (unlikely(new_mbuf == NULL)) {
127                         break;
128                 }
129                 if (unlikely(virtqueue_enqueue_recv_refill(rxvq, new_mbuf))) {
130                         rte_pktmbuf_free_seg(new_mbuf);
131                         break;
132                 }
133         }
134         pi->eth_stats.ipackets += num;
135         return num;
136 }
137
138 static uint16_t
139 eth_xenvirt_tx(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
140 {
141         struct virtqueue *txvq = tx_queue;
142         struct rte_mbuf *txm;
143         uint16_t nb_used, nb_tx, num, i;
144         int error;
145         uint32_t len[VIRTIO_MBUF_BURST_SZ];
146         struct rte_mbuf *snd_pkts[VIRTIO_MBUF_BURST_SZ];
147         struct pmd_internals *pi = txvq->internals;
148
149         nb_tx = 0;
150
151         if (unlikely(nb_pkts == 0))
152                 return 0;
153
154         PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
155         nb_used = VIRTQUEUE_NUSED(txvq);
156
157         rte_smp_rmb();
158
159         num = (uint16_t)(likely(nb_used <= VIRTIO_MBUF_BURST_SZ) ? nb_used : VIRTIO_MBUF_BURST_SZ);
160         num = virtqueue_dequeue_burst(txvq, snd_pkts, len, num);
161
162         for (i = 0; i < num ; i ++) {
163                 /* mergable not supported, one segment only */
164                 rte_pktmbuf_free_seg(snd_pkts[i]);
165         }
166
167         while (nb_tx < nb_pkts) {
168                 if (likely(!virtqueue_full(txvq))) {
169                 /* TODO drop tx_pkts if it contains multiple segments */
170                         txm = tx_pkts[nb_tx];
171                         error = virtqueue_enqueue_xmit(txvq, txm);
172                         if (unlikely(error)) {
173                                 if (error == ENOSPC)
174                                         PMD_TX_LOG(ERR, "virtqueue_enqueue Free count = 0\n");
175                                 else if (error == EMSGSIZE)
176                                         PMD_TX_LOG(ERR, "virtqueue_enqueue Free count < 1\n");
177                                 else
178                                         PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d\n", error);
179                                 break;
180                         }
181                         nb_tx++;
182                 } else {
183                         PMD_TX_LOG(ERR, "No free tx descriptors to transmit\n");
184                         /* virtqueue_notify not needed in our para-virt solution */
185                         break;
186                 }
187         }
188         pi->eth_stats.opackets += nb_tx;
189         return nb_tx;
190 }
191
192 static int
193 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
194 {
195         RTE_LOG(ERR, PMD, "%s\n", __func__);
196         return 0;
197 }
198
199 /*
200  * Create a shared page between guest and host.
201  * Host monitors this page if it is cleared on unmap, and then
202  * do necessary clean up.
203  */
204 static void
205 gntalloc_vring_flag(int vtidx)
206 {
207         char key_str[PATH_MAX];
208         char val_str[PATH_MAX];
209         uint32_t gref_tmp;
210         void *ptr;
211
212         if (grefwatch_from_alloc(&gref_tmp, &ptr)) {
213                 RTE_LOG(ERR, PMD, "grefwatch_from_alloc error\n");
214                 exit(0);
215         }
216
217         *(uint8_t *)ptr = MAP_FLAG;
218         snprintf(val_str, sizeof(val_str), "%u", gref_tmp);
219         snprintf(key_str, sizeof(key_str),
220                 DPDK_XENSTORE_PATH"%d"VRING_FLAG_STR, vtidx);
221         xenstore_write(key_str, val_str);
222 }
223
224 /*
225  * Notify host this virtio device is started.
226  * Host could start polling this device.
227  */
228 static void
229 dev_start_notify(int vtidx)
230 {
231         char key_str[PATH_MAX];
232         char val_str[PATH_MAX];
233
234         RTE_LOG(INFO, PMD, "%s: virtio %d is started\n", __func__, vtidx);
235         gntalloc_vring_flag(vtidx);
236
237         snprintf(key_str, sizeof(key_str), "%s%s%d",
238                 DPDK_XENSTORE_PATH, EVENT_TYPE_START_STR,
239                         vtidx);
240         snprintf(val_str, sizeof(val_str), "1");
241         xenstore_write(key_str, val_str);
242 }
243
244 /*
245  * Notify host this virtio device is stopped.
246  * Host could stop polling this device.
247  */
248 static void
249 dev_stop_notify(int vtidx)
250 {
251         RTE_SET_USED(vtidx);
252 }
253
254
255 static int
256 update_mac_address(struct ether_addr *mac_addrs, int vtidx)
257 {
258         char key_str[PATH_MAX];
259         char val_str[PATH_MAX];
260         int rv;
261
262         if (mac_addrs == NULL) {
263                 RTE_LOG(ERR, PMD, "%s: NULL pointer mac specified\n", __func__);
264                 return -1;
265         }
266         rv = snprintf(key_str, sizeof(key_str),
267                         DPDK_XENSTORE_PATH"%d_ether_addr", vtidx);
268         if (rv == -1)
269                 return rv;
270         rv = snprintf(val_str, sizeof(val_str), "%02x:%02x:%02x:%02x:%02x:%02x",
271                         mac_addrs->addr_bytes[0],
272                         mac_addrs->addr_bytes[1],
273                         mac_addrs->addr_bytes[2],
274                         mac_addrs->addr_bytes[3],
275                         mac_addrs->addr_bytes[4],
276                         mac_addrs->addr_bytes[5]);
277         if (rv == -1)
278                 return rv;
279         if (xenstore_write(key_str, val_str))
280                 return rv;
281         return 0;
282 }
283
284
285 static int
286 eth_dev_start(struct rte_eth_dev *dev)
287 {
288         struct virtqueue *rxvq = dev->data->rx_queues[0];
289         struct virtqueue *txvq = dev->data->tx_queues[0];
290         struct rte_mbuf *m;
291         struct pmd_internals *pi = (struct pmd_internals *)dev->data->dev_private;
292         int rv;
293
294         dev->data->dev_link.link_status = ETH_LINK_UP;
295         while (!virtqueue_full(rxvq)) {
296                 m = rte_rxmbuf_alloc(rxvq->mpool);
297                 if (m == NULL)
298                         break;
299                 /* Enqueue allocated buffers. */
300                 if (virtqueue_enqueue_recv_refill(rxvq, m)) {
301                         rte_pktmbuf_free_seg(m);
302                         break;
303                 }
304         }
305
306         rxvq->internals = pi;
307         txvq->internals = pi;
308
309         rv = update_mac_address(dev->data->mac_addrs, pi->virtio_idx);
310         if (rv)
311                 return -1;
312         dev_start_notify(pi->virtio_idx);
313
314         return 0;
315 }
316
317 static void
318 eth_dev_stop(struct rte_eth_dev *dev)
319 {
320         struct pmd_internals *pi = (struct pmd_internals *)dev->data->dev_private;
321
322         dev->data->dev_link.link_status = ETH_LINK_DOWN;
323         dev_stop_notify(pi->virtio_idx);
324 }
325
326 /*
327  * Notify host this virtio device is closed.
328  * Host could do necessary clean up to this device.
329  */
330 static void
331 eth_dev_close(struct rte_eth_dev *dev)
332 {
333         eth_xenvirt_free_queues(dev);
334 }
335
336 static void
337 eth_dev_info(struct rte_eth_dev *dev,
338                 struct rte_eth_dev_info *dev_info)
339 {
340         struct pmd_internals *internals = dev->data->dev_private;
341
342         RTE_SET_USED(internals);
343         dev_info->driver_name = drivername;
344         dev_info->max_mac_addrs = 1;
345         dev_info->max_rx_pktlen = (uint32_t)2048;
346         dev_info->max_rx_queues = (uint16_t)1;
347         dev_info->max_tx_queues = (uint16_t)1;
348         dev_info->min_rx_bufsize = 0;
349         dev_info->pci_dev = NULL;
350 }
351
352 static void
353 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
354 {
355         struct pmd_internals *internals = dev->data->dev_private;
356         if(stats)
357                 rte_memcpy(stats, &internals->eth_stats, sizeof(*stats));
358 }
359
360 static void
361 eth_stats_reset(struct rte_eth_dev *dev)
362 {
363         struct pmd_internals *internals = dev->data->dev_private;
364         /* Reset software totals */
365         memset(&internals->eth_stats, 0, sizeof(internals->eth_stats));
366 }
367
368 static void
369 eth_queue_release(void *q)
370 {
371         rte_free(q);
372 }
373
374 static int
375 eth_link_update(struct rte_eth_dev *dev __rte_unused,
376                 int wait_to_complete __rte_unused)
377 {
378         return 0;
379 }
380
381 /*
382  * Create shared vring between guest and host.
383  * Memory is allocated through grant alloc driver, so it is not physical continuous.
384  */
385 static void *
386 gntalloc_vring_create(int queue_type, uint32_t size, int vtidx)
387 {
388         char key_str[PATH_MAX] = {0};
389         char val_str[PATH_MAX] = {0};
390         void *va = NULL;
391         int pg_size;
392         uint32_t pg_num;
393         uint32_t *gref_arr = NULL;
394         phys_addr_t *pa_arr = NULL;
395         uint64_t start_index;
396         int rv;
397
398         pg_size = getpagesize();
399         size    = RTE_ALIGN_CEIL(size, pg_size);
400         pg_num  = size / pg_size;
401
402         gref_arr = calloc(pg_num, sizeof(gref_arr[0]));
403         pa_arr  = calloc(pg_num, sizeof(pa_arr[0]));
404
405         if (gref_arr == NULL || pa_arr == NULL) {
406                 RTE_LOG(ERR, PMD, "%s: calloc failed\n", __func__);
407                 goto out;
408         }
409
410         va  = gntalloc(size, gref_arr, &start_index);
411         if (va == NULL) {
412                 RTE_LOG(ERR, PMD, "%s: gntalloc failed\n", __func__);
413                 goto out;
414         }
415
416         if (get_phys_map(va, pa_arr, pg_num, pg_size))
417                 goto out;
418
419         /* write in xenstore gref and pfn for each page of vring */
420         if (grant_node_create(pg_num, gref_arr, pa_arr, val_str, sizeof(val_str))) {
421                 gntfree(va, size, start_index);
422                 va = NULL;
423                 goto out;
424         }
425
426         if (queue_type == VTNET_RQ)
427                 rv = snprintf(key_str, sizeof(key_str), DPDK_XENSTORE_PATH"%d"RXVRING_XENSTORE_STR, vtidx);
428         else
429                 rv = snprintf(key_str, sizeof(key_str), DPDK_XENSTORE_PATH"%d"TXVRING_XENSTORE_STR, vtidx);
430         if (rv == -1 || xenstore_write(key_str, val_str) == -1) {
431                 gntfree(va, size, start_index);
432                 va = NULL;
433         }
434 out:
435         free(pa_arr);
436         free(gref_arr);
437
438         return va;
439 }
440
441
442
443 static struct virtqueue *
444 virtio_queue_setup(struct rte_eth_dev *dev, int queue_type)
445 {
446         struct virtqueue *vq = NULL;
447         uint16_t vq_size = VQ_DESC_NUM;
448         int i = 0;
449         char vq_name[VIRTQUEUE_MAX_NAME_SZ];
450         size_t size;
451         struct vring *vr;
452
453         /* Allocate memory for virtqueue. */
454         if (queue_type == VTNET_RQ) {
455                 snprintf(vq_name, sizeof(vq_name), "port%d_rvq",
456                                 dev->data->port_id);
457                 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
458                         vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
459                 if (vq == NULL) {
460                         RTE_LOG(ERR, PMD, "%s: unabled to allocate virtqueue\n", __func__);
461                         return NULL;
462                 }
463                 memcpy(vq->vq_name, vq_name, sizeof(vq->vq_name));
464         } else if(queue_type == VTNET_TQ) {
465                 snprintf(vq_name, sizeof(vq_name), "port%d_tvq",
466                         dev->data->port_id);
467                 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
468                         vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
469                 if (vq == NULL) {
470                         RTE_LOG(ERR, PMD, "%s: unabled to allocate virtqueue\n", __func__);
471                         return NULL;
472                 }
473                 memcpy(vq->vq_name, vq_name, sizeof(vq->vq_name));
474         }
475
476         memcpy(vq->vq_name, vq_name, sizeof(vq->vq_name));
477
478         vq->vq_alignment = VIRTIO_PCI_VRING_ALIGN;
479         vq->vq_nentries = vq_size;
480         vq->vq_free_cnt = vq_size;
481         /* Calcuate vring size according to virtio spec */
482         size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
483         vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
484         /* Allocate memory for virtio vring through gntalloc driver*/
485         vq->vq_ring_virt_mem = gntalloc_vring_create(queue_type, vq->vq_ring_size,
486                 ((struct pmd_internals *)dev->data->dev_private)->virtio_idx);
487         memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
488         vr = &vq->vq_ring;
489         vring_init(vr, vq_size, vq->vq_ring_virt_mem, vq->vq_alignment);
490         /*
491          * Locally maintained last consumed index, this idex trails
492          * vq_ring.used->idx.
493          */
494         vq->vq_used_cons_idx = 0;
495         vq->vq_desc_head_idx = 0;
496         vq->vq_free_cnt = vq->vq_nentries;
497         memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
498
499         /* Chain all the descriptors in the ring with an END */
500         for (i = 0; i < vq_size - 1; i++)
501                 vr->desc[i].next = (uint16_t)(i + 1);
502         vr->desc[i].next = VQ_RING_DESC_CHAIN_END;
503
504         return vq;
505 }
506
507 static int
508 eth_rx_queue_setup(struct rte_eth_dev *dev,uint16_t rx_queue_id,
509                                 uint16_t nb_rx_desc __rte_unused,
510                                 unsigned int socket_id __rte_unused,
511                                 const struct rte_eth_rxconf *rx_conf __rte_unused,
512                                 struct rte_mempool *mb_pool)
513 {
514         struct virtqueue *vq;
515         vq = dev->data->rx_queues[rx_queue_id] = virtio_queue_setup(dev, VTNET_RQ);
516         vq->mpool = mb_pool;
517         return 0;
518 }
519
520 static int
521 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
522                                 uint16_t nb_tx_desc __rte_unused,
523                                 unsigned int socket_id __rte_unused,
524                                 const struct rte_eth_txconf *tx_conf __rte_unused)
525 {
526         dev->data->tx_queues[tx_queue_id] = virtio_queue_setup(dev, VTNET_TQ);
527         return 0;
528 }
529
530 static void
531 eth_xenvirt_free_queues(struct rte_eth_dev *dev)
532 {
533         int i;
534
535         for (i = 0; i < dev->data->nb_rx_queues; i++) {
536                 eth_queue_release(dev->data->rx_queues[i]);
537                 dev->data->rx_queues[i] = NULL;
538         }
539         dev->data->nb_rx_queues = 0;
540
541         for (i = 0; i < dev->data->nb_tx_queues; i++) {
542                 eth_queue_release(dev->data->tx_queues[i]);
543                 dev->data->tx_queues[i] = NULL;
544         }
545         dev->data->nb_tx_queues = 0;
546 }
547
548 static const struct eth_dev_ops ops = {
549         .dev_start = eth_dev_start,
550         .dev_stop = eth_dev_stop,
551         .dev_close = eth_dev_close,
552         .dev_configure = eth_dev_configure,
553         .dev_infos_get = eth_dev_info,
554         .rx_queue_setup = eth_rx_queue_setup,
555         .tx_queue_setup = eth_tx_queue_setup,
556         .rx_queue_release = eth_queue_release,
557         .tx_queue_release = eth_queue_release,
558         .link_update = eth_link_update,
559         .stats_get = eth_stats_get,
560         .stats_reset = eth_stats_reset,
561 };
562
563
564 static int
565 rte_eth_xenvirt_parse_args(struct xenvirt_dict *dict,
566                         const char *name, const char *params)
567 {
568         int i;
569         char *pairs[RTE_ETH_XENVIRT_MAX_ARGS];
570         int num_of_pairs;
571         char *pair[2];
572         char *args;
573         int ret = -1;
574
575         if (params == NULL)
576                 return 0;
577
578         args = rte_zmalloc(NULL, strlen(params) + 1, RTE_CACHE_LINE_SIZE);
579         if (args == NULL) {
580                 RTE_LOG(ERR, PMD, "Couldn't parse %s device \n", name);
581                 return -1;
582         }
583         rte_memcpy(args, params, strlen(params));
584
585         num_of_pairs = rte_strsplit(args, strnlen(args, MAX_ARG_STRLEN),
586                                         pairs,
587                                         RTE_ETH_XENVIRT_MAX_ARGS ,
588                                         RTE_ETH_XENVIRT_PAIRS_DELIM);
589
590         for (i = 0; i < num_of_pairs; i++) {
591                 pair[0] = NULL;
592                 pair[1] = NULL;
593                 rte_strsplit(pairs[i], strnlen(pairs[i], MAX_ARG_STRLEN),
594                                         pair, 2,
595                                         RTE_ETH_XENVIRT_KEY_VALUE_DELIM);
596
597                 if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0
598                         || pair[1][0] == 0) {
599                         RTE_LOG(ERR, PMD,
600                                 "Couldn't parse %s device,"
601                                 "wrong key or value \n", name);
602                         goto err;
603                 }
604
605                 if (!strncmp(pair[0], RTE_ETH_XENVIRT_MAC_PARAM,
606                                 sizeof(RTE_ETH_XENVIRT_MAC_PARAM))) {
607                         if (cmdline_parse_etheraddr(NULL,
608                                                     pair[1],
609                                                     &dict->addr,
610                                                     sizeof(dict->addr)) < 0) {
611                                 RTE_LOG(ERR, PMD,
612                                         "Invalid %s device ether address\n",
613                                         name);
614                                 goto err;
615                         }
616
617                         dict->addr_valid = 1;
618                 }
619         }
620
621         ret = 0;
622 err:
623         rte_free(args);
624         return ret;
625 }
626
627 enum dev_action {
628         DEV_CREATE,
629         DEV_ATTACH
630 };
631
632
633 static int
634 eth_dev_xenvirt_create(const char *name, const char *params,
635                 const unsigned numa_node,
636                 enum dev_action action)
637 {
638         struct rte_eth_dev_data *data = NULL;
639         struct pmd_internals *internals = NULL;
640         struct rte_eth_dev *eth_dev = NULL;
641         struct xenvirt_dict dict;
642
643         memset(&dict, 0, sizeof(struct xenvirt_dict));
644
645         RTE_LOG(INFO, PMD, "Creating virtio rings backed ethdev on numa socket %u\n",
646                         numa_node);
647         RTE_SET_USED(action);
648
649         if (rte_eth_xenvirt_parse_args(&dict, name, params) < 0) {
650                 RTE_LOG(ERR, PMD, "%s: Failed to parse ethdev parameters\n", __func__);
651                 return -1;
652         }
653
654         /* now do all data allocation - for eth_dev structure, dummy pci driver
655          * and internal (private) data
656          */
657         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
658         if (data == NULL)
659                 goto err;
660
661         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
662         if (internals == NULL)
663                 goto err;
664
665         /* reserve an ethdev entry */
666         eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
667         if (eth_dev == NULL)
668                 goto err;
669
670         data->dev_private = internals;
671         data->port_id = eth_dev->data->port_id;
672         data->nb_rx_queues = (uint16_t)1;
673         data->nb_tx_queues = (uint16_t)1;
674         data->dev_link = pmd_link;
675         data->mac_addrs = rte_zmalloc("xen_virtio", ETHER_ADDR_LEN, 0);
676
677         if(dict.addr_valid)
678                 memcpy(&data->mac_addrs->addr_bytes, &dict.addr, sizeof(struct ether_addr));
679         else
680                 eth_random_addr(&data->mac_addrs->addr_bytes[0]);
681
682         eth_dev->data = data;
683         eth_dev->dev_ops = &ops;
684
685         eth_dev->data->dev_flags = RTE_PCI_DRV_DETACHABLE;
686         eth_dev->data->kdrv = RTE_KDRV_NONE;
687         eth_dev->data->drv_name = drivername;
688         eth_dev->driver = NULL;
689         eth_dev->data->numa_node = numa_node;
690
691         eth_dev->rx_pkt_burst = eth_xenvirt_rx;
692         eth_dev->tx_pkt_burst = eth_xenvirt_tx;
693
694         internals->virtio_idx = virtio_idx++;
695         internals->port_id = eth_dev->data->port_id;
696
697         return 0;
698
699 err:
700         rte_free(data);
701         rte_free(internals);
702
703         return -1;
704 }
705
706
707 static int
708 eth_dev_xenvirt_free(const char *name, const unsigned numa_node)
709 {
710         struct rte_eth_dev *eth_dev = NULL;
711
712         RTE_LOG(DEBUG, PMD,
713                 "Free virtio rings backed ethdev on numa socket %u\n",
714                 numa_node);
715
716         /* find an ethdev entry */
717         eth_dev = rte_eth_dev_allocated(name);
718         if (eth_dev == NULL)
719                 return -1;
720
721         if (eth_dev->data->dev_started == 1) {
722                 eth_dev_stop(eth_dev);
723                 eth_dev_close(eth_dev);
724         }
725
726         eth_dev->rx_pkt_burst = NULL;
727         eth_dev->tx_pkt_burst = NULL;
728         eth_dev->dev_ops = NULL;
729
730         rte_free(eth_dev->data);
731         rte_free(eth_dev->data->dev_private);
732         rte_free(eth_dev->data->mac_addrs);
733
734         virtio_idx--;
735
736         return 0;
737 }
738
739 /*TODO: Support multiple process model */
740 static int
741 rte_pmd_xenvirt_devinit(const char *name, const char *params)
742 {
743         if (virtio_idx == 0) {
744                 if (xenstore_init() != 0) {
745                         RTE_LOG(ERR, PMD, "%s: xenstore init failed\n", __func__);
746                         return -1;
747                 }
748                 if (gntalloc_open() != 0) {
749                         RTE_LOG(ERR, PMD, "%s: grant init failed\n", __func__);
750                         return -1;
751                 }
752         }
753         eth_dev_xenvirt_create(name, params, rte_socket_id(), DEV_CREATE);
754         return 0;
755 }
756
757 static int
758 rte_pmd_xenvirt_devuninit(const char *name)
759 {
760         eth_dev_xenvirt_free(name, rte_socket_id());
761
762         if (virtio_idx == 0) {
763                 if (xenstore_uninit() != 0)
764                         RTE_LOG(ERR, PMD, "%s: xenstore uninit failed\n", __func__);
765
766                 gntalloc_close();
767         }
768         return 0;
769 }
770
771 static struct rte_driver pmd_xenvirt_drv = {
772         .name = "eth_xenvirt",
773         .type = PMD_VDEV,
774         .init = rte_pmd_xenvirt_devinit,
775         .uninit = rte_pmd_xenvirt_devuninit,
776 };
777
778 PMD_REGISTER_DRIVER(pmd_xenvirt_drv);