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