xref: /linux/drivers/block/drbd/drbd_protocol.h (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __DRBD_PROTOCOL_H
3 #define __DRBD_PROTOCOL_H
4 
5 enum drbd_packet {
6 	/* receiver (data socket) */
7 	P_DATA		      = 0x00,
8 	P_DATA_REPLY	      = 0x01, /* Response to P_DATA_REQUEST */
9 	P_RS_DATA_REPLY	      = 0x02, /* Response to P_RS_DATA_REQUEST */
10 	P_BARRIER	      = 0x03,
11 	P_BITMAP	      = 0x04,
12 	P_BECOME_SYNC_TARGET  = 0x05,
13 	P_BECOME_SYNC_SOURCE  = 0x06,
14 	P_UNPLUG_REMOTE	      = 0x07, /* Used at various times to hint the peer */
15 	P_DATA_REQUEST	      = 0x08, /* Used to ask for a data block */
16 	P_RS_DATA_REQUEST     = 0x09, /* Used to ask for a data block for resync */
17 	P_SYNC_PARAM	      = 0x0a,
18 	P_PROTOCOL	      = 0x0b,
19 	P_UUIDS		      = 0x0c,
20 	P_SIZES		      = 0x0d,
21 	P_STATE		      = 0x0e,
22 	P_SYNC_UUID	      = 0x0f,
23 	P_AUTH_CHALLENGE      = 0x10,
24 	P_AUTH_RESPONSE	      = 0x11,
25 	P_STATE_CHG_REQ	      = 0x12,
26 
27 	/* (meta socket) */
28 	P_PING		      = 0x13,
29 	P_PING_ACK	      = 0x14,
30 	P_RECV_ACK	      = 0x15, /* Used in protocol B */
31 	P_WRITE_ACK	      = 0x16, /* Used in protocol C */
32 	P_RS_WRITE_ACK	      = 0x17, /* Is a P_WRITE_ACK, additionally call set_in_sync(). */
33 	P_SUPERSEDED	      = 0x18, /* Used in proto C, two-primaries conflict detection */
34 	P_NEG_ACK	      = 0x19, /* Sent if local disk is unusable */
35 	P_NEG_DREPLY	      = 0x1a, /* Local disk is broken... */
36 	P_NEG_RS_DREPLY	      = 0x1b, /* Local disk is broken... */
37 	P_BARRIER_ACK	      = 0x1c,
38 	P_STATE_CHG_REPLY     = 0x1d,
39 
40 	/* "new" commands, no longer fitting into the ordering scheme above */
41 
42 	P_OV_REQUEST	      = 0x1e, /* data socket */
43 	P_OV_REPLY	      = 0x1f,
44 	P_OV_RESULT	      = 0x20, /* meta socket */
45 	P_CSUM_RS_REQUEST     = 0x21, /* data socket */
46 	P_RS_IS_IN_SYNC	      = 0x22, /* meta socket */
47 	P_SYNC_PARAM89	      = 0x23, /* data socket, protocol version 89 replacement for P_SYNC_PARAM */
48 	P_COMPRESSED_BITMAP   = 0x24, /* compressed or otherwise encoded bitmap transfer */
49 	/* P_CKPT_FENCE_REQ      = 0x25, * currently reserved for protocol D */
50 	/* P_CKPT_DISABLE_REQ    = 0x26, * currently reserved for protocol D */
51 	P_DELAY_PROBE         = 0x27, /* is used on BOTH sockets */
52 	P_OUT_OF_SYNC         = 0x28, /* Mark as out of sync (Outrunning), data socket */
53 	P_RS_CANCEL           = 0x29, /* meta: Used to cancel RS_DATA_REQUEST packet by SyncSource */
54 	P_CONN_ST_CHG_REQ     = 0x2a, /* data sock: Connection wide state request */
55 	P_CONN_ST_CHG_REPLY   = 0x2b, /* meta sock: Connection side state req reply */
56 	P_RETRY_WRITE	      = 0x2c, /* Protocol C: retry conflicting write request */
57 	P_PROTOCOL_UPDATE     = 0x2d, /* data sock: is used in established connections */
58         /* 0x2e to 0x30 reserved, used in drbd 9 */
59 
60 	/* REQ_OP_DISCARD. We used "discard" in different contexts before,
61 	 * which is why I chose TRIM here, to disambiguate. */
62 	P_TRIM                = 0x31,
63 
64 	/* Only use these two if both support FF_THIN_RESYNC */
65 	P_RS_THIN_REQ         = 0x32, /* Request a block for resync or reply P_RS_DEALLOCATED */
66 	P_RS_DEALLOCATED      = 0x33, /* Contains only zeros on sync source node */
67 
68 	/* REQ_WRITE_SAME.
69 	 * On a receiving side without REQ_WRITE_SAME,
70 	 * we may fall back to an opencoded loop instead. */
71 	P_WSAME               = 0x34,
72 
73 	/* 0x35 already claimed in DRBD 9 */
74 	P_ZEROES              = 0x36, /* data sock: zero-out, WRITE_ZEROES */
75 
76 	/* 0x40 .. 0x48 already claimed in DRBD 9 */
77 
78 	P_MAY_IGNORE	      = 0x100, /* Flag to test if (cmd > P_MAY_IGNORE) ... */
79 	P_MAX_OPT_CMD	      = 0x101,
80 
81 	/* special command ids for handshake */
82 
83 	P_INITIAL_META	      = 0xfff1, /* First Packet on the MetaSock */
84 	P_INITIAL_DATA	      = 0xfff2, /* First Packet on the Socket */
85 
86 	P_CONNECTION_FEATURES = 0xfffe	/* FIXED for the next century! */
87 };
88 
89 #ifndef __packed
90 #define __packed __attribute__((packed))
91 #endif
92 
93 /* This is the layout for a packet on the wire.
94  * The byteorder is the network byte order.
95  *     (except block_id and barrier fields.
96  *	these are pointers to local structs
97  *	and have no relevance for the partner,
98  *	which just echoes them as received.)
99  *
100  * NOTE that the payload starts at a long aligned offset,
101  * regardless of 32 or 64 bit arch!
102  */
103 struct p_header80 {
104 	u32	  magic;
105 	u16	  command;
106 	u16	  length;	/* bytes of data after this header */
107 } __packed;
108 
109 /* Header for big packets, Used for data packets exceeding 64kB */
110 struct p_header95 {
111 	u16	  magic;	/* use DRBD_MAGIC_BIG here */
112 	u16	  command;
113 	u32	  length;
114 } __packed;
115 
116 struct p_header100 {
117 	u32	  magic;
118 	u16	  volume;
119 	u16	  command;
120 	u32	  length;
121 	u32	  pad;
122 } __packed;
123 
124 /* These defines must not be changed without changing the protocol version.
125  * New defines may only be introduced together with protocol version bump or
126  * new protocol feature flags.
127  */
128 #define DP_HARDBARRIER	      1 /* no longer used */
129 #define DP_RW_SYNC	      2 /* equals REQ_SYNC    */
130 #define DP_MAY_SET_IN_SYNC    4
131 #define DP_UNPLUG             8 /* not used anymore   */
132 #define DP_FUA               16 /* equals REQ_FUA     */
133 #define DP_FLUSH             32 /* equals REQ_PREFLUSH   */
134 #define DP_DISCARD           64 /* equals REQ_OP_DISCARD */
135 #define DP_SEND_RECEIVE_ACK 128 /* This is a proto B write request */
136 #define DP_SEND_WRITE_ACK   256 /* This is a proto C write request */
137 #define DP_WSAME            512 /* equiv. REQ_WRITE_SAME */
138 #define DP_ZEROES          1024 /* equiv. REQ_OP_WRITE_ZEROES */
139 
140 /* possible combinations:
141  * REQ_OP_WRITE_ZEROES:  DP_DISCARD | DP_ZEROES
142  * REQ_OP_WRITE_ZEROES + REQ_NOUNMAP: DP_ZEROES
143  */
144 
145 struct p_data {
146 	u64	    sector;    /* 64 bits sector number */
147 	u64	    block_id;  /* to identify the request in protocol B&C */
148 	u32	    seq_num;
149 	u32	    dp_flags;
150 } __packed;
151 
152 struct p_trim {
153 	struct p_data p_data;
154 	u32	    size;	/* == bio->bi_size */
155 } __packed;
156 
157 struct p_wsame {
158 	struct p_data p_data;
159 	u32           size;     /* == bio->bi_size */
160 } __packed;
161 
162 /*
163  * commands which share a struct:
164  *  p_block_ack:
165  *   P_RECV_ACK (proto B), P_WRITE_ACK (proto C),
166  *   P_SUPERSEDED (proto C, two-primaries conflict detection)
167  *  p_block_req:
168  *   P_DATA_REQUEST, P_RS_DATA_REQUEST
169  */
170 struct p_block_ack {
171 	u64	    sector;
172 	u64	    block_id;
173 	u32	    blksize;
174 	u32	    seq_num;
175 } __packed;
176 
177 struct p_block_req {
178 	u64 sector;
179 	u64 block_id;
180 	u32 blksize;
181 	u32 pad;	/* to multiple of 8 Byte */
182 } __packed;
183 
184 /*
185  * commands with their own struct for additional fields:
186  *   P_CONNECTION_FEATURES
187  *   P_BARRIER
188  *   P_BARRIER_ACK
189  *   P_SYNC_PARAM
190  *   ReportParams
191  */
192 
193 /* supports TRIM/DISCARD on the "wire" protocol */
194 #define DRBD_FF_TRIM 1
195 
196 /* Detect all-zeros during resync, and rather TRIM/UNMAP/DISCARD those blocks
197  * instead of fully allocate a supposedly thin volume on initial resync */
198 #define DRBD_FF_THIN_RESYNC 2
199 
200 /* supports REQ_WRITE_SAME on the "wire" protocol.
201  * Note: this flag is overloaded,
202  * its presence also
203  *   - indicates support for 128 MiB "batch bios",
204  *     max discard size of 128 MiB
205  *     instead of 4M before that.
206  *   - indicates that we exchange additional settings in p_sizes
207  *     drbd_send_sizes()/receive_sizes()
208  */
209 #define DRBD_FF_WSAME 4
210 
211 /* supports REQ_OP_WRITE_ZEROES on the "wire" protocol.
212  *
213  * We used to map that to "discard" on the sending side, and if we cannot
214  * guarantee that discard zeroes data, the receiving side would map discard
215  * back to zero-out.
216  *
217  * With the introduction of REQ_OP_WRITE_ZEROES,
218  * we started to use that for both WRITE_ZEROES and DISCARDS,
219  * hoping that WRITE_ZEROES would "do what we want",
220  * UNMAP if possible, zero-out the rest.
221  *
222  * The example scenario is some LVM "thin" backend.
223  *
224  * While an un-allocated block on dm-thin reads as zeroes, on a dm-thin
225  * with "skip_block_zeroing=true", after a partial block write allocated
226  * that block, that same block may well map "undefined old garbage" from
227  * the backends on LBAs that have not yet been written to.
228  *
229  * If we cannot distinguish between zero-out and discard on the receiving
230  * side, to avoid "undefined old garbage" to pop up randomly at later times
231  * on supposedly zero-initialized blocks, we'd need to map all discards to
232  * zero-out on the receiving side.  But that would potentially do a full
233  * alloc on thinly provisioned backends, even when the expectation was to
234  * unmap/trim/discard/de-allocate.
235  *
236  * We need to distinguish on the protocol level, whether we need to guarantee
237  * zeroes (and thus use zero-out, potentially doing the mentioned full-alloc),
238  * or if we want to put the emphasis on discard, and only do a "best effort
239  * zeroing" (by "discarding" blocks aligned to discard-granularity, and zeroing
240  * only potential unaligned head and tail clippings), to at least *try* to
241  * avoid "false positives" in an online-verify later, hoping that someone
242  * set skip_block_zeroing=false.
243  */
244 #define DRBD_FF_WZEROES 8
245 
246 
247 struct p_connection_features {
248 	u32 protocol_min;
249 	u32 feature_flags;
250 	u32 protocol_max;
251 
252 	/* should be more than enough for future enhancements
253 	 * for now, feature_flags and the reserved array shall be zero.
254 	 */
255 
256 	u32 _pad;
257 	u64 reserved[7];
258 } __packed;
259 
260 struct p_barrier {
261 	u32 barrier;	/* barrier number _handle_ only */
262 	u32 pad;	/* to multiple of 8 Byte */
263 } __packed;
264 
265 struct p_barrier_ack {
266 	u32 barrier;
267 	u32 set_size;
268 } __packed;
269 
270 struct p_rs_param {
271 	u32 resync_rate;
272 
273 	      /* Since protocol version 88 and higher. */
274 	char verify_alg[0];
275 } __packed;
276 
277 struct p_rs_param_89 {
278 	u32 resync_rate;
279 	/* protocol version 89: */
280 	char verify_alg[SHARED_SECRET_MAX];
281 	char csums_alg[SHARED_SECRET_MAX];
282 } __packed;
283 
284 struct p_rs_param_95 {
285 	u32 resync_rate;
286 	char verify_alg[SHARED_SECRET_MAX];
287 	char csums_alg[SHARED_SECRET_MAX];
288 	u32 c_plan_ahead;
289 	u32 c_delay_target;
290 	u32 c_fill_target;
291 	u32 c_max_rate;
292 } __packed;
293 
294 enum drbd_conn_flags {
295 	CF_DISCARD_MY_DATA = 1,
296 	CF_DRY_RUN = 2,
297 };
298 
299 struct p_protocol {
300 	u32 protocol;
301 	u32 after_sb_0p;
302 	u32 after_sb_1p;
303 	u32 after_sb_2p;
304 	u32 conn_flags;
305 	u32 two_primaries;
306 
307 	/* Since protocol version 87 and higher. */
308 	char integrity_alg[0];
309 
310 } __packed;
311 
312 struct p_uuids {
313 	u64 uuid[UI_EXTENDED_SIZE];
314 } __packed;
315 
316 struct p_rs_uuid {
317 	u64	    uuid;
318 } __packed;
319 
320 /* optional queue_limits if (agreed_features & DRBD_FF_WSAME)
321  * see also struct queue_limits, as of late 2015 */
322 struct o_qlim {
323 	/* we don't need it yet, but we may as well communicate it now */
324 	u32 physical_block_size;
325 
326 	/* so the original in struct queue_limits is unsigned short,
327 	 * but I'd have to put in padding anyways. */
328 	u32 logical_block_size;
329 
330 	/* One incoming bio becomes one DRBD request,
331 	 * which may be translated to several bio on the receiving side.
332 	 * We don't need to communicate chunk/boundary/segment ... limits.
333 	 */
334 
335 	/* various IO hints may be useful with "diskless client" setups */
336 	u32 alignment_offset;
337 	u32 io_min;
338 	u32 io_opt;
339 
340 	/* We may need to communicate integrity stuff at some point,
341 	 * but let's not get ahead of ourselves. */
342 
343 	/* Backend discard capabilities.
344 	 * Receiving side uses "blkdev_issue_discard()", no need to communicate
345 	 * more specifics.  If the backend cannot do discards, the DRBD peer
346 	 * may fall back to blkdev_issue_zeroout().
347 	 */
348 	u8 discard_enabled;
349 	u8 discard_zeroes_data;
350 	u8 write_same_capable;
351 	u8 _pad;
352 } __packed;
353 
354 struct p_sizes {
355 	u64	    d_size;  /* size of disk */
356 	u64	    u_size;  /* user requested size */
357 	u64	    c_size;  /* current exported size */
358 	u32	    max_bio_size;  /* Maximal size of a BIO */
359 	u16	    queue_order_type;  /* not yet implemented in DRBD*/
360 	u16	    dds_flags; /* use enum dds_flags here. */
361 
362 	/* optional queue_limits if (agreed_features & DRBD_FF_WSAME) */
363 	struct o_qlim qlim[0];
364 } __packed;
365 
366 struct p_state {
367 	u32	    state;
368 } __packed;
369 
370 struct p_req_state {
371 	u32	    mask;
372 	u32	    val;
373 } __packed;
374 
375 struct p_req_state_reply {
376 	u32	    retcode;
377 } __packed;
378 
379 struct p_drbd06_param {
380 	u64	  size;
381 	u32	  state;
382 	u32	  blksize;
383 	u32	  protocol;
384 	u32	  version;
385 	u32	  gen_cnt[5];
386 	u32	  bit_map_gen[5];
387 } __packed;
388 
389 struct p_block_desc {
390 	u64 sector;
391 	u32 blksize;
392 	u32 pad;	/* to multiple of 8 Byte */
393 } __packed;
394 
395 /* Valid values for the encoding field.
396  * Bump proto version when changing this. */
397 enum drbd_bitmap_code {
398 	/* RLE_VLI_Bytes = 0,
399 	 * and other bit variants had been defined during
400 	 * algorithm evaluation. */
401 	RLE_VLI_Bits = 2,
402 };
403 
404 struct p_compressed_bm {
405 	/* (encoding & 0x0f): actual encoding, see enum drbd_bitmap_code
406 	 * (encoding & 0x80): polarity (set/unset) of first runlength
407 	 * ((encoding >> 4) & 0x07): pad_bits, number of trailing zero bits
408 	 * used to pad up to head.length bytes
409 	 */
410 	u8 encoding;
411 
412 	u8 code[0];
413 } __packed;
414 
415 struct p_delay_probe93 {
416 	u32     seq_num; /* sequence number to match the two probe packets */
417 	u32     offset;  /* usecs the probe got sent after the reference time point */
418 } __packed;
419 
420 /*
421  * Bitmap packets need to fit within a single page on the sender and receiver,
422  * so we are limited to 4 KiB (and not to PAGE_SIZE, which can be bigger).
423  */
424 #define DRBD_SOCKET_BUFFER_SIZE 4096
425 
426 #endif  /* __DRBD_PROTOCOL_H */
427