remove trailing whitespaces
[dpdk.git] / examples / vhost / virtio-net.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 _VIRTIO_NET_H_
35 #define _VIRTIO_NET_H_
36
37 /* Used to indicate that the device is running on a data core */
38 #define VIRTIO_DEV_RUNNING 1
39
40 /* Backend value set by guest. */
41 #define VIRTIO_DEV_STOPPED -1
42
43 #define PAGE_SIZE   4096
44
45 /* Enum for virtqueue management. */
46 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
47
48 /*
49  * Structure contains variables relevant to TX/RX virtqueues.
50  */
51 struct vhost_virtqueue
52 {
53         struct vring_desc       *desc;                          /* Virtqueue descriptor ring. */
54         struct vring_avail      *avail;                         /* Virtqueue available ring. */
55         struct vring_used       *used;                          /* Virtqueue used ring. */
56         uint32_t                        size;                           /* Size of descriptor ring. */
57         uint32_t                        backend;                        /* Backend value to determine if device should started/stopped. */
58         uint16_t                        vhost_hlen;                     /* Vhost header length (varies depending on RX merge buffers. */
59         volatile uint16_t       last_used_idx;          /* Last index used on the available ring */
60         volatile uint16_t       last_used_idx_res;      /* Used for multiple devices reserving buffers. */
61         eventfd_t                       callfd;                         /* Currently unused as polling mode is enabled. */
62         eventfd_t                       kickfd;                         /* Used to notify the guest (trigger interrupt). */
63 } __rte_cache_aligned;
64
65 /*
66  * Device structure contains all configuration information relating to the device.
67  */
68 struct virtio_net
69 {
70         struct vhost_virtqueue  *virtqueue[VIRTIO_QNUM];        /* Contains all virtqueue information. */
71         struct virtio_memory    *mem;                                           /* QEMU memory and memory region information. */
72         struct ether_addr               mac_address;                            /* Device MAC address (Obtained on first TX packet). */
73         uint64_t                                features;                                       /* Negotiated feature set. */
74         uint64_t                                device_fh;                                      /* device identifier. */
75         uint32_t                                vmdq_rx_q;                                      /* RX VMDQ queue number. */
76         uint32_t                                flags;                                          /* Device flags. Only used to check if device is running on data core. */
77         uint32_t                                vlan_tag;                                       /* Vlan tag for device. Currently set to device_id (0-63). */
78         uint16_t                                coreid;                                         /* Data core that the device is added to. */
79         volatile uint8_t                ready;                                          /* A device is set as ready if the MAC address has been set. */
80         volatile uint8_t                remove;                                         /* Device is marked for removal from the data core. */
81 } __rte_cache_aligned;
82
83 /*
84  * Device linked list structure for configuration.
85  */
86 struct virtio_net_config_ll
87 {
88         struct virtio_net               dev;    /* Virtio device. */
89         struct virtio_net_config_ll     *next; /* Next entry on linked list. */
90 };
91
92 /*
93  * Information relating to memory regions including offsets to addresses in QEMUs memory file.
94  */
95 struct virtio_memory_regions {
96         uint64_t        guest_phys_address;             /* Base guest physical address of region. */
97         uint64_t        guest_phys_address_end; /* End guest physical address of region. */
98         uint64_t        memory_size;                    /* Size of region. */
99         uint64_t        userspace_address;              /* Base userspace address of region. */
100         uint64_t        address_offset;                 /* Offset of region for address translation. */
101 };
102
103 /*
104  * Information relating to memory regions including offsets to
105  * addresses in host physical space.
106  */
107 struct virtio_memory_regions_hpa {
108         /* Base guest physical address of region. */
109         uint64_t        guest_phys_address;
110         /* End guest physical address of region. */
111         uint64_t        guest_phys_address_end;
112         /* Size of region. */
113         uint64_t        memory_size;
114         /* Offset of region for gpa to hpa translation. */
115         uint64_t        host_phys_addr_offset;
116 };
117
118 /*
119  * Memory structure includes region and mapping information.
120  */
121 struct virtio_memory {
122         uint64_t                        base_address;                   /* Base QEMU userspace address of the memory file. */
123         uint64_t                        mapped_address;                 /* Mapped address of memory file base in our applications memory space. */
124         uint64_t                        mapped_size;                    /* Total size of memory file. */
125         uint32_t                        nregions;                               /* Number of memory regions. */
126          /* Number of memory regions for gpa to hpa translation. */
127         uint32_t                        nregions_hpa;
128         /* Memory region information for gpa to hpa translation. */
129         struct virtio_memory_regions_hpa  *regions_hpa;
130         /* Memory region information. */
131         struct virtio_memory_regions      regions[0];
132 };
133
134 /*
135  * Device operations to add/remove device.
136  */
137 struct virtio_net_device_ops {
138         int (* new_device)              (struct virtio_net *);  /* Add device. */
139         void (* destroy_device) (volatile struct virtio_net *); /* Remove device. */
140 };
141
142 int init_virtio_net(struct virtio_net_device_ops const * const);
143 int deinit_virtio_net(void);
144
145 struct vhost_net_device_ops const * get_virtio_net_callbacks(void);
146
147 #endif /* _VIRTIO_NET_H_ */