xref: /freebsd/sbin/ggate/shared/ggate.h (revision 1b1e392aed4957a38c49599512b4f65b844a0772)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
29 #ifndef _GGATE_H_
30 #define	_GGATE_H_
31 
32 #include <geom/gate/g_gate.h>
33 #include <sys/endian.h>
34 #include <stdarg.h>
35 
36 #define	G_GATE_PORT		3080
37 
38 #define	G_GATE_RCVBUF		131072
39 #define	G_GATE_SNDBUF		131072
40 #define	G_GATE_QUEUE_SIZE	1024
41 #define	G_GATE_TIMEOUT		0
42 
43 #define	GGATE_MAGIC		"GEOM_GATE       "
44 #define	GGATE_VERSION		0
45 
46 #define	GGATE_FLAG_RDONLY	G_GATE_FLAG_READONLY
47 #define	GGATE_FLAG_WRONLY	G_GATE_FLAG_WRITEONLY
48 /*
49  * If neither the GGATE_FLAG_SEND nor the GGATE_FLAG_RECV flag is
50  * set - this is initial connection.
51  * If GGATE_FLAG_SEND flag is set - this is socket to send data.
52  * If GGATE_FLAG_RECV flag is set - this is socket to receive data.
53  */
54 #define	GGATE_FLAG_SEND		0x0004
55 #define	GGATE_FLAG_RECV		0x0008
56 
57 #define	GGATE_FLAG_DIRECT	0x0010
58 
59 #define	GGATE_CMD_READ		0
60 #define	GGATE_CMD_WRITE		1
61 #define	GGATE_CMD_FLUSH		3
62 
63 extern int g_gate_devfd;
64 extern int g_gate_verbose;
65 
66 extern int nagle;
67 extern unsigned rcvbuf, sndbuf;
68 
69 struct g_gate_version {
70 	char		gv_magic[16];
71 	uint16_t	gv_version;
72 	uint16_t	gv_error;
73 } __packed;
74 
75 /* Client's initial packet. */
76 struct g_gate_cinit {
77 	char		gc_path[PATH_MAX + 1];
78 	uint64_t	gc_flags;
79 	uint16_t	gc_nconn;
80 	uint32_t	gc_token;
81 } __packed;
82 
83 /* Server's initial packet. */
84 struct g_gate_sinit {
85 	uint8_t		gs_flags;
86 	uint64_t	gs_mediasize;
87 	uint32_t	gs_sectorsize;
88 	uint16_t	gs_error;
89 } __packed;
90 
91 /* Control struct. */
92 struct g_gate_hdr {
93 	uint8_t		gh_cmd;		/* command */
94 	uint64_t	gh_offset;	/* device offset */
95 	uint32_t	gh_length;	/* size of block */
96 	uint64_t	gh_seq;		/* request number */
97 	uint16_t	gh_error;	/* error value (0 if ok) */
98 } __packed;
99 
100 void	g_gate_vlog(int priority, const char *message, va_list ap);
101 void	g_gate_log(int priority, const char *message, ...);
102 void	g_gate_xvlog(const char *message, va_list ap) __dead2;
103 void	g_gate_xlog(const char *message, ...) __dead2;
104 off_t	g_gate_mediasize(int fd);
105 unsigned g_gate_sectorsize(int fd);
106 void	g_gate_open_device(void);
107 void	g_gate_close_device(void);
108 void	g_gate_ioctl(unsigned long req, void *data);
109 void	g_gate_destroy(int unit, int force);
110 void	g_gate_load_module(void);
111 ssize_t	g_gate_recv(int s, void *buf, size_t len, int flags);
112 ssize_t	g_gate_send(int s, const void *buf, size_t len, int flags);
113 void	g_gate_socket_settings(int sfd);
114 #ifdef LIBGEOM
115 void	g_gate_list(int unit, int verbose);
116 #endif
117 in_addr_t g_gate_str2ip(const char *str);
118 
119 /*
120  * g_gate_swap2h_* - functions swap bytes to host byte order (from big endian).
121  * g_gate_swap2n_* - functions swap bytes to network byte order (actually
122  *                   to big endian byte order).
123  */
124 
125 static __inline void
g_gate_swap2h_version(struct g_gate_version * ver)126 g_gate_swap2h_version(struct g_gate_version *ver)
127 {
128 
129 	ver->gv_version = be16toh(ver->gv_version);
130 	ver->gv_error = be16toh(ver->gv_error);
131 }
132 
133 static __inline void
g_gate_swap2n_version(struct g_gate_version * ver)134 g_gate_swap2n_version(struct g_gate_version *ver)
135 {
136 
137 	ver->gv_version = htobe16(ver->gv_version);
138 	ver->gv_error = htobe16(ver->gv_error);
139 }
140 
141 static __inline void
g_gate_swap2h_cinit(struct g_gate_cinit * cinit)142 g_gate_swap2h_cinit(struct g_gate_cinit *cinit)
143 {
144 
145 	cinit->gc_flags = be64toh(cinit->gc_flags);
146 	cinit->gc_nconn = be16toh(cinit->gc_nconn);
147 	cinit->gc_token = be32toh(cinit->gc_token);
148 }
149 
150 static __inline void
g_gate_swap2n_cinit(struct g_gate_cinit * cinit)151 g_gate_swap2n_cinit(struct g_gate_cinit *cinit)
152 {
153 
154 	cinit->gc_flags = htobe64(cinit->gc_flags);
155 	cinit->gc_nconn = htobe16(cinit->gc_nconn);
156 	cinit->gc_token = htobe32(cinit->gc_token);
157 }
158 
159 static __inline void
g_gate_swap2h_sinit(struct g_gate_sinit * sinit)160 g_gate_swap2h_sinit(struct g_gate_sinit *sinit)
161 {
162 
163 	/* Swap only used fields. */
164 	sinit->gs_mediasize = be64toh(sinit->gs_mediasize);
165 	sinit->gs_sectorsize = be32toh(sinit->gs_sectorsize);
166 	sinit->gs_error = be16toh(sinit->gs_error);
167 }
168 
169 static __inline void
g_gate_swap2n_sinit(struct g_gate_sinit * sinit)170 g_gate_swap2n_sinit(struct g_gate_sinit *sinit)
171 {
172 
173 	/* Swap only used fields. */
174 	sinit->gs_mediasize = htobe64(sinit->gs_mediasize);
175 	sinit->gs_sectorsize = htobe32(sinit->gs_sectorsize);
176 	sinit->gs_error = htobe16(sinit->gs_error);
177 }
178 
179 static __inline void
g_gate_swap2h_hdr(struct g_gate_hdr * hdr)180 g_gate_swap2h_hdr(struct g_gate_hdr *hdr)
181 {
182 
183 	/* Swap only used fields. */
184 	hdr->gh_offset = be64toh(hdr->gh_offset);
185 	hdr->gh_length = be32toh(hdr->gh_length);
186 	hdr->gh_seq = be64toh(hdr->gh_seq);
187 	hdr->gh_error = be16toh(hdr->gh_error);
188 }
189 
190 static __inline void
g_gate_swap2n_hdr(struct g_gate_hdr * hdr)191 g_gate_swap2n_hdr(struct g_gate_hdr *hdr)
192 {
193 
194 	/* Swap only used fields. */
195 	hdr->gh_offset = htobe64(hdr->gh_offset);
196 	hdr->gh_length = htobe32(hdr->gh_length);
197 	hdr->gh_seq = htobe64(hdr->gh_seq);
198 	hdr->gh_error = htobe16(hdr->gh_error);
199 }
200 #endif	/* _GGATE_H_ */
201