8c0d15a220ebefa08db22afe2a4df10e5227cde5
[dpdk.git] / examples / vhost_xen / xen_vhost.h
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 #ifndef _XEN_VHOST_H_
35 #define _XEN_VHOST_H_
36
37 #include <stdint.h>
38
39 #include <rte_tailq.h>
40 #include <rte_ether.h>
41
42 #include "virtio-net.h"
43
44 #define RTE_LOGTYPE_XENHOST RTE_LOGTYPE_USER1
45
46 #define XEN_VM_ROOTNODE_FMT  "/local/domain/%d/control/dpdk"
47 #define XEN_VM_NODE_FMT      "/local/domain/%d/control/dpdk/%s"
48 #define XEN_MEMPOOL_SUFFIX   "mempool_gref"
49 #define XEN_RXVRING_SUFFIX   "rx_vring_gref"
50 #define XEN_TXVRING_SUFFIX   "tx_vring_gref"
51 #define XEN_GVA_SUFFIX       "mempool_va"
52 #define XEN_VRINGFLAG_SUFFIX "vring_flag"
53 #define XEN_ADDR_SUFFIX      "ether_addr"
54 #define VIRTIO_START         "event_type_start_"
55
56 #define XEN_GREF_SPLITTOKEN  ','
57
58 #define MAX_XENVIRT_MEMPOOL 16
59 #define MAX_VIRTIO  32
60 #define MAX_GREF_PER_NODE 64  /* 128 MB memory */
61
62 #define PAGE_SIZE   4096
63 #define PAGE_PFNNUM (PAGE_SIZE / sizeof(uint32_t))
64
65 #define XEN_GNTDEV_FNAME "/dev/xen/gntdev"
66
67 /* xen grant reference info in one grant node */
68 struct xen_gnt {
69         uint32_t gref;  /* grant reference for this node */
70         union {
71                 int gref;               /* grant reference */
72                 uint32_t pfn_num;       /* guest pfn number of grant reference */
73         } gref_pfn[PAGE_PFNNUM];
74 }__attribute__((__packed__));
75
76
77 /* structure for mempool or vring node list */
78 struct xen_gntnode {
79         uint32_t gnt_num;           /* grant reference number */
80         struct xen_gnt *gnt_info;   /* grant reference info */
81 };
82
83
84 struct xen_vring {
85         uint32_t dom_id;
86         uint32_t virtio_idx;    /* index of virtio device */
87         void *rxvring_addr;     /* mapped virtual address of rxvring */
88         void *txvring_addr;     /* mapped virtual address of txvring */
89         uint32_t rxpfn_num;     /* number of gpfn for rxvring */
90         uint32_t txpfn_num;     /* number of gpfn for txvring */
91         uint32_t *rxpfn_tbl;    /* array of rxvring gpfn */
92         uint32_t *txpfn_tbl;    /* array of txvring gpfn */
93         uint64_t *rx_pindex;    /* index used to release rx grefs */
94         uint64_t *tx_pindex;    /* index used to release tx grefs */
95         uint64_t  flag_index;
96         uint8_t  *flag;         /* cleared to zero on guest unmap */
97         struct ether_addr addr; /* ethernet address of virtio device */
98         uint8_t   removed;
99
100 };
101
102 struct xen_mempool {
103         uint32_t dom_id;      /* guest domain id */
104         uint32_t pool_idx;    /* index of memory pool */
105         void *gva;            /* guest virtual address of mbuf pool */
106         void *hva;            /* host virtual address of mbuf pool */
107         uint32_t mempfn_num;  /* number of gpfn for mbuf pool */
108         uint32_t *mempfn_tbl; /* array of mbuf pool gpfn */
109         uint64_t *pindex;     /* index used to release grefs */
110 };
111
112 struct xen_guest {
113         TAILQ_ENTRY(xen_guest) next;
114         int32_t dom_id;       /* guest domain id */
115         uint32_t pool_num;    /* number of mbuf pool of the guest */
116         uint32_t vring_num;   /* number of virtio ports of the guest */
117         /* array contain the guest mbuf pool info */
118         struct xen_mempool mempool[MAX_XENVIRT_MEMPOOL];
119         /* array contain the guest rx/tx vring info */
120         struct xen_vring vring[MAX_VIRTIO];
121 };
122
123 TAILQ_HEAD(xen_guestlist, xen_guest);
124
125 int
126 parse_mempoolnode(struct xen_guest *guest);
127
128 int
129 xenhost_init(void);
130
131 int
132 parse_vringnode(struct xen_guest *guest, uint32_t virtio_idx);
133
134 int
135 parse_mempoolnode(struct xen_guest *guest);
136
137 void
138 cleanup_mempool(struct xen_mempool *mempool);
139
140 void
141 cleanup_vring(struct xen_vring *vring);
142
143 void
144 virtio_monitor_loop(void);
145
146 int
147 init_virtio_xen(struct virtio_net_device_ops const * const);
148
149 #endif