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