xref: /freebsd/sbin/ggate/shared/ggate.h (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
1 /*-
2  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _GGATE_H_
30 #define	_GGATE_H_
31 
32 #include <sys/endian.h>
33 #include <stdarg.h>
34 
35 #define	G_GATE_PORT		3080
36 
37 #define	G_GATE_RCVBUF		131072
38 #define	G_GATE_SNDBUF		131072
39 #define	G_GATE_QUEUE_SIZE	1024
40 #define	G_GATE_TIMEOUT		0
41 
42 #define	GGATE_MAGIC		"GEOM_GATE       "
43 #define	GGATE_VERSION		0
44 
45 #define	GGATE_FLAG_RDONLY	0x0001
46 #define	GGATE_FLAG_WRONLY	0x0002
47 /*
48  * If GGATE_FLAG_SEND not GGATE_FLAG_RECV flag is set, this is initial
49  * connection.
50  * If GGATE_FLAG_SEND flag is set - this is socket to send data.
51  * If GGATE_FLAG_RECV flag is set - this is socket to receive data.
52  */
53 #define	GGATE_FLAG_SEND		0x0004
54 #define	GGATE_FLAG_RECV		0x0008
55 
56 #define	GGATE_CMD_READ		0
57 #define	GGATE_CMD_WRITE		1
58 
59 extern int g_gate_devfd;
60 extern int g_gate_verbose;
61 
62 extern int nagle;
63 extern unsigned rcvbuf, sndbuf;
64 
65 struct g_gate_version {
66 	char		gv_magic[16];
67 	uint16_t	gv_version;
68 	uint16_t	gv_error;
69 } __packed;
70 
71 /* Client's initial packet. */
72 struct g_gate_cinit {
73 	char		gc_path[PATH_MAX + 1];
74 	uint64_t	gc_flags;
75 	uint16_t	gc_nconn;
76 	uint32_t	gc_token;
77 } __packed;
78 
79 /* Server's initial packet. */
80 struct g_gate_sinit {
81 	uint8_t		gs_flags;
82 	uint64_t	gs_mediasize;
83 	uint32_t	gs_sectorsize;
84 	uint16_t	gs_error;
85 } __packed;
86 
87 /* Control struct. */
88 struct g_gate_hdr {
89 	uint8_t		gh_cmd;		/* command */
90 	uint64_t	gh_offset;	/* device offset */
91 	uint32_t	gh_length;	/* size of block */
92 	uint64_t	gh_seq;		/* request number */
93 	uint16_t	gh_error;	/* error value (0 if ok) */
94 } __packed;
95 
96 void	g_gate_vlog(int priority, const char *message, va_list ap);
97 void	g_gate_log(int priority, const char *message, ...);
98 void	g_gate_xvlog(const char *message, va_list ap) __dead2;
99 void	g_gate_xlog(const char *message, ...) __dead2;
100 off_t	g_gate_mediasize(int fd);
101 unsigned g_gate_sectorsize(int fd);
102 void	g_gate_open_device(void);
103 void	g_gate_close_device(void);
104 void	g_gate_ioctl(unsigned long req, void *data);
105 void	g_gate_destroy(int unit, int force);
106 void	g_gate_load_module(void);
107 ssize_t	g_gate_recv(int s, void *buf, size_t len, int flags);
108 ssize_t	g_gate_send(int s, const void *buf, size_t len, int flags);
109 void	g_gate_socket_settings(int sfd);
110 #ifdef LIBGEOM
111 void	g_gate_list(int unit, int verbose);
112 #endif
113 in_addr_t g_gate_str2ip(const char *str);
114 
115 /*
116  * g_gate_swap2h_* - functions swap bytes to host byte order (from big endian).
117  * g_gate_swap2n_* - functions swap bytes to network byte order (actually
118  *                   to big endian byte order).
119  */
120 
121 static __inline void
122 g_gate_swap2h_version(struct g_gate_version *ver)
123 {
124 
125 	ver->gv_version = be16toh(ver->gv_version);
126 	ver->gv_error = be16toh(ver->gv_error);
127 }
128 
129 static __inline void
130 g_gate_swap2n_version(struct g_gate_version *ver)
131 {
132 
133 	ver->gv_version = htobe16(ver->gv_version);
134 	ver->gv_error = htobe16(ver->gv_error);
135 }
136 
137 static __inline void
138 g_gate_swap2h_cinit(struct g_gate_cinit *cinit)
139 {
140 
141 	cinit->gc_flags = be64toh(cinit->gc_flags);
142 	cinit->gc_nconn = be16toh(cinit->gc_nconn);
143 	cinit->gc_token = be32toh(cinit->gc_token);
144 }
145 
146 static __inline void
147 g_gate_swap2n_cinit(struct g_gate_cinit *cinit)
148 {
149 
150 	cinit->gc_flags = htobe64(cinit->gc_flags);
151 	cinit->gc_nconn = htobe16(cinit->gc_nconn);
152 	cinit->gc_token = htobe32(cinit->gc_token);
153 }
154 
155 static __inline void
156 g_gate_swap2h_sinit(struct g_gate_sinit *sinit)
157 {
158 
159 	/* Swap only used fields. */
160 	sinit->gs_mediasize = be64toh(sinit->gs_mediasize);
161 	sinit->gs_sectorsize = be32toh(sinit->gs_sectorsize);
162 	sinit->gs_error = be16toh(sinit->gs_error);
163 }
164 
165 static __inline void
166 g_gate_swap2n_sinit(struct g_gate_sinit *sinit)
167 {
168 
169 	/* Swap only used fields. */
170 	sinit->gs_mediasize = htobe64(sinit->gs_mediasize);
171 	sinit->gs_sectorsize = htobe32(sinit->gs_sectorsize);
172 	sinit->gs_error = htobe16(sinit->gs_error);
173 }
174 
175 static __inline void
176 g_gate_swap2h_hdr(struct g_gate_hdr *hdr)
177 {
178 
179 	/* Swap only used fields. */
180 	hdr->gh_offset = be64toh(hdr->gh_offset);
181 	hdr->gh_length = be32toh(hdr->gh_length);
182 	hdr->gh_seq = be64toh(hdr->gh_seq);
183 	hdr->gh_error = be16toh(hdr->gh_error);
184 }
185 
186 static __inline void
187 g_gate_swap2n_hdr(struct g_gate_hdr *hdr)
188 {
189 
190 	/* Swap only used fields. */
191 	hdr->gh_offset = htobe64(hdr->gh_offset);
192 	hdr->gh_length = htobe32(hdr->gh_length);
193 	hdr->gh_seq = htobe64(hdr->gh_seq);
194 	hdr->gh_error = htobe16(hdr->gh_error);
195 }
196 #endif	/* _GGATE_H_ */
197