xref: /aosp_15_r20/external/ublksrv/nbd/cliserv.h (revision 94c4a1e103eb1715230460aab379dff275992c20)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #ifndef UBLK_NBD_CLISERV_INC_H
4 #define UBLK_NBD_CLISERV_INC_H
5 
6 /* This header file is shared by client & server. They really have
7  * something to share...
8  * */
9 
10 /* Client/server protocol is as follows:
11    Send INIT_PASSWD
12    Send 64-bit cliserv_magic
13    Send 64-bit size of exported device
14    Send 128 bytes of zeros (reserved for future use)
15  */
16 
17 #include <errno.h>
18 #include <string.h>
19 #include <netdb.h>
20 #include <netinet/tcp.h>
21 #include <netinet/in.h>
22 #include <stdlib.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define NOTLS
29 
30 #define	 u64	uint64_t
31 #define	 u32	uint32_t
32 #define	 u16	uint16_t
33 #define	 u8	uint8_t
34 
35 #define	 s64	int64_t
36 #define	 s32	int32_t
37 #define	 s16	int16_t
38 #define	 s8	int8_t
39 
40 #define __be32 u32
41 #define __be64 u64
42 #include "nbd.h"
43 
44 #ifndef HAVE_FDATASYNC
45 #define fdatasync(arg) fsync(arg)
46 #endif
47 
48 #if NBD_LFS==1
49 /* /usr/include/features.h (included from /usr/include/sys/types.h)
50    defines this when _GNU_SOURCE is defined
51  */
52 #ifndef _LARGEFILE_SOURCE
53 #define _LARGEFILE_SOURCE
54 #endif
55 #define _FILE_OFFSET_BITS 64
56 #endif
57 
58 #ifndef G_GNUC_NORETURN
59 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
60 #define G_GNUC_NORETURN __attribute__((__noreturn__))
61 #define G_GNUC_UNUSED __attribute__((unused))
62 #else
63 #define G_GNUC_NORETURN
64 #define G_GNUC_UNUSED
65 #endif
66 #endif
67 
68 extern const u64 cliserv_magic;
69 extern const u64 opts_magic;
70 extern const u64 rep_magic;
71 
72 #define INIT_PASSWD "NBDMAGIC"
73 
74 #define INFO(a) do { } while(0)
75 
76 int set_nonblocking(int fd, int nb);
77 void setmysockopt(int sock);
78 void err_nonfatal(const char *s);
79 
80 void nbd_err(const char *s) G_GNUC_NORETURN;
81 #define err(S) nbd_err(S)
82 
83 void logging(const char* name);
84 
85 #ifndef ntohll
86 uint64_t ntohll(uint64_t a);
87 #endif
88 #ifndef htonll
89 #define htonll ntohll
90 #endif
91 
92 int readit(int f, void *buf, size_t len);
93 int writeit(int f, void *buf, size_t len);
94 
95 #define NBD_DEFAULT_PORT	"10809"	/* Port on which named exports are
96 					 * served */
97 
98 /* Options that the client can select to the server */
99 #define NBD_OPT_EXPORT_NAME	(1)	/**< Client wants to select a named export (is followed by name of export) */
100 #define NBD_OPT_ABORT		(2)	/**< Client wishes to abort negotiation */
101 #define NBD_OPT_LIST		(3)	/**< Client request list of supported exports (not followed by data) */
102 #define NBD_OPT_STARTTLS	(5)	/**< Client wishes to initiate TLS */
103 #define NBD_OPT_INFO		(6)	/**< Client wants information about the given export */
104 #define NBD_OPT_GO		(7)	/**< Client wants to select the given and move to the transmission phase */
105 
106 /* Replies the server can send during negotiation */
107 #define NBD_REP_ACK		(1)	/**< ACK a request. Data: option number to be acked */
108 #define NBD_REP_SERVER		(2)	/**< Reply to NBD_OPT_LIST (one of these per server; must be followed by NBD_REP_ACK to signal the end of the list */
109 #define NBD_REP_INFO		(3)	/**< Reply to NBD_OPT_INFO */
110 #define NBD_REP_FLAG_ERROR	(1 << 31)	/** If the high bit is set, the reply is an error */
111 #define NBD_REP_ERR_UNSUP	(1U | NBD_REP_FLAG_ERROR)	/**< Client requested an option not understood by this version of the server */
112 #define NBD_REP_ERR_POLICY	(2U | NBD_REP_FLAG_ERROR)	/**< Client requested an option not allowed by server configuration. (e.g., the option was disabled) */
113 #define NBD_REP_ERR_INVALID	(3U | NBD_REP_FLAG_ERROR)	/**< Client issued an invalid request */
114 #define NBD_REP_ERR_PLATFORM	(4U | NBD_REP_FLAG_ERROR)	/**< Option not supported on this platform */
115 #define NBD_REP_ERR_TLS_REQD	(5U | NBD_REP_FLAG_ERROR)	/**< TLS required */
116 #define NBD_REP_ERR_UNKNOWN	(6U | NBD_REP_FLAG_ERROR)	/**< NBD_OPT_INFO or ..._GO requested on unknown export */
117 #define NBD_REP_ERR_BLOCK_SIZE_REQD (8 | NBD_REP_FLAG_ERROR)	/**< Server is not willing to serve the export without the block size being negotiated */
118 
119 /* Global flags */
120 #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0)	/**< new-style export that actually supports extending */
121 #define NBD_FLAG_NO_ZEROES	(1 << 1)	/**< we won't send the 128 bits of zeroes if the client sends NBD_FLAG_C_NO_ZEROES */
122 /* Flags from client to server. */
123 #define NBD_FLAG_C_FIXED_NEWSTYLE NBD_FLAG_FIXED_NEWSTYLE
124 #define NBD_FLAG_C_NO_ZEROES	NBD_FLAG_NO_ZEROES
125 
126 /* Info types */
127 #define NBD_INFO_EXPORT		(0)
128 #define NBD_INFO_NAME		(1)
129 #define NBD_INFO_DESCRIPTION	(2)
130 #define NBD_INFO_BLOCK_SIZE	(3)
131 
132 #ifdef __cplusplus
133 }
134 #endif
135 #endif
136