1 /* blktaplib.h
2  *
3  * Blktap library userspace code.
4  *
5  * Copyright (c) 2007, XenSource Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in the
14  *       documentation and/or other materials provided with the distribution.
15  *     * Neither the name of XenSource Inc. nor the names of its contributors
16  *       may be used to endorse or promote products derived from this software
17  *       without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef __BLKTAPLIB_H__
33 #define __BLKTAPLIB_H__
34 
35 #include <syslog.h>
36 #include <sys/time.h>
37 #include <xenctrl.h>
38 #include <xen/io/blkif.h>
39 
40 #define BLK_RING_SIZE __CONST_RING_SIZE(blkif, XC_PAGE_SIZE)
41 
42 /* size of the extra VMA area to map in attached pages. */
43 #define BLKTAP_VMA_PAGES BLK_RING_SIZE
44 
45 /* blktap IOCTLs: These must correspond with the blktap driver ioctls */
46 #define BLKTAP_IOCTL_KICK_FE         1
47 #define BLKTAP_IOCTL_KICK_BE         2
48 #define BLKTAP_IOCTL_SETMODE         3
49 #define BLKTAP_IOCTL_SENDPID	     4
50 #define BLKTAP_IOCTL_NEWINTF	     5
51 #define BLKTAP_IOCTL_MINOR	     6
52 #define BLKTAP_IOCTL_MAJOR	     7
53 #define BLKTAP_QUERY_ALLOC_REQS      8
54 #define BLKTAP_IOCTL_FREEINTF	     9
55 #define BLKTAP_IOCTL_PRINT_IDXS      100
56 #define BLKTAP_IOCTL_BACKDEV_SETUP   200
57 
58 #define PRIO_SPECIAL_IO             -9999
59 
60 /* blktap switching modes: (Set with BLKTAP_IOCTL_SETMODE)             */
61 #define BLKTAP_MODE_PASSTHROUGH      0x00000000  /* default            */
62 #define BLKTAP_MODE_INTERCEPT_FE     0x00000001
63 #define BLKTAP_MODE_INTERCEPT_BE     0x00000002
64 
65 #define BLKTAP_MODE_INTERPOSE \
66            (BLKTAP_MODE_INTERCEPT_FE | BLKTAP_MODE_INTERCEPT_BE)
67 
BLKTAP_MODE_VALID(unsigned long arg)68 static inline int BLKTAP_MODE_VALID(unsigned long arg)
69 {
70 	return (
71 		( arg == BLKTAP_MODE_PASSTHROUGH  ) ||
72 		( arg == BLKTAP_MODE_INTERCEPT_FE ) ||
73 		( arg == BLKTAP_MODE_INTERPOSE    ) );
74 }
75 
76 #define MAX_REQUESTS            BLK_RING_SIZE
77 
78 #define BLKTAP_IOCTL_KICK       1
79 #define MAX_PENDING_REQS	BLK_RING_SIZE
80 #define BLKTAP_DEV_DIR          "/dev/xen"
81 #define BLKTAP_DEV_NAME         "blktap"
82 #define BACKDEV_NAME            "backdev"
83 #define BLKTAP_DEV_MINOR        0
84 #define BLKTAP_CTRL_DIR         "/var/run/tap"
85 
86 extern int blktap_major;
87 
88 #define BLKTAP_RING_PAGES       1 /* Front */
89 #define BLKTAP_MMAP_REGION_SIZE (BLKTAP_RING_PAGES + MMAP_PAGES)
90 
91 struct blkif;
92 struct blkif_info;
93 
94 typedef struct {
95 	blkif_request_t  req;
96 	int              submitting;
97 	int              secs_pending;
98         int16_t          status;
99 	int              num_retries;
100 	struct timeval   last_try;
101 } pending_req_t;
102 
103 typedef struct blkif {
104 	domid_t domid;
105 	long int handle;
106 
107 	long int pdev;
108 	long int readonly;
109 
110 	enum { DISCONNECTED, DISCONNECTING, CONNECTED } state;
111 
112 	struct blkif_ops *ops;
113 	struct blkif *hash_next;
114 
115 	void *prv;  /* device-specific data */
116 	struct blkif_info *info; /*Image parameter passing */
117 	pending_req_t pending_list[MAX_REQUESTS];
118 	int devnum;
119 	int fds[2];
120 	int be_id;
121 	char *backend_path;
122 	int major;
123 	int minor;
124 	pid_t tappid;
125 	int drivertype;
126 	uint16_t cookie;
127 	int err;
128 } blkif_t;
129 
130 typedef struct blkif_info {
131 	char *params;
132 	int   readonly;
133 	int   storage;
134 } blkif_info_t;
135 
136 typedef struct tapdev_info {
137 	int fd;
138 	char *mem;
139 	blkif_sring_t *sring;
140 	blkif_back_ring_t  fe_ring;
141 	unsigned long vstart;
142 	blkif_t *blkif;
143 } tapdev_info_t;
144 
145 typedef struct domid_translate {
146 	unsigned short domid;
147 	unsigned short busid;
148 } domid_translate_t ;
149 
150 typedef struct image {
151 	unsigned long long size;
152 	unsigned long secsize;
153 	unsigned int info;
154 } image_t;
155 
156 typedef struct msg_hdr {
157 	uint16_t   type;
158 	uint16_t   len;
159 	uint16_t   drivertype;
160 	uint16_t   cookie;
161 } msg_hdr_t;
162 
163 typedef struct msg_params {
164 	uint8_t    readonly;
165 	int        path_off;
166 	int        path_len;
167 	int        storage;
168 } msg_params_t;
169 
170 typedef struct msg_newdev {
171 	uint8_t     devnum;
172 	uint16_t    domid;
173 } msg_newdev_t;
174 
175 typedef struct msg_pid {
176 	pid_t     pid;
177 } msg_pid_t;
178 
179 typedef struct msg_cp {
180 	int       cp_uuid_off;
181 	int       cp_uuid_len;
182 	int       cp_drivertype;
183 } msg_cp_t;
184 
185 typedef struct msg_lock {
186 	int       ro;
187 	int       enforce;
188 	int       uuid_off;
189 	int       uuid_len;
190 } msg_lock_t;
191 
192 #define READ 0
193 #define WRITE 1
194 
195 /*Control Messages between manager and tapdev*/
196 #define CTLMSG_PARAMS          1
197 #define CTLMSG_IMG             2
198 #define CTLMSG_IMG_FAIL        3
199 #define CTLMSG_NEWDEV          4
200 #define CTLMSG_NEWDEV_RSP      5
201 #define CTLMSG_NEWDEV_FAIL     6
202 #define CTLMSG_CLOSE           7
203 #define CTLMSG_CLOSE_RSP       8
204 #define CTLMSG_PID             9
205 #define CTLMSG_PID_RSP         10
206 #define CTLMSG_CHECKPOINT      11
207 #define CTLMSG_CHECKPOINT_RSP  12
208 #define CTLMSG_LOCK            13
209 #define CTLMSG_LOCK_RSP        14
210 #define CTLMSG_PAUSE           15
211 #define CTLMSG_PAUSE_RSP       16
212 #define CTLMSG_RESUME          17
213 #define CTLMSG_RESUME_RSP      18
214 
215 #define TAPDISK_STORAGE_TYPE_NFS       1
216 #define TAPDISK_STORAGE_TYPE_EXT       2
217 #define TAPDISK_STORAGE_TYPE_LVM       3
218 #define TAPDISK_STORAGE_TYPE_DEFAULT   TAPDISK_STORAGE_TYPE_EXT
219 
220 /* Abitrary values, must match the underlying driver... */
221 #define MAX_TAP_DEV 256
222 
223 /* Accessing attached data page mappings */
224 #define MMAP_PAGES                                                    \
225     (MAX_PENDING_REQS * BLKIF_MAX_SEGMENTS_PER_REQUEST)
226 #define MMAP_VADDR(_vstart,_req,_seg)                                 \
227     ((_vstart) +                                                      \
228      ((_req) * BLKIF_MAX_SEGMENTS_PER_REQUEST * getpagesize()) +      \
229      ((_seg) * getpagesize()))
230 
231 /* Defines that are only used by library clients */
232 
233 #ifndef __COMPILING_BLKTAP_LIB
234 
235 static char *blkif_op_name[] = {
236 	[BLKIF_OP_READ]       = "READ",
237 	[BLKIF_OP_WRITE]      = "WRITE",
238 };
239 
240 #endif /* __COMPILING_BLKTAP_LIB */
241 
242 #endif /* __BLKTAPLIB_H__ */
243