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