2 * Copyright (C) 2013 Red Hat Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * 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.
16 * * Neither the name of Red Hat nor the names of its contributors may be
17 * used to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #ifndef NBDKIT_PROTOCOL_H
35 #define NBDKIT_PROTOCOL_H
39 /* Old-style handshake */
40 struct old_handshake
{
41 char nbdmagic
[8]; /* "NBDMAGIC" */
42 uint64_t version
; /* OLD_VERSION, in network byte order */
43 uint64_t exportsize
; /* in network byte order */
44 uint16_t gflags
; /* global flags, in network byte order */
45 uint16_t eflags
; /* per-export flags, in network byte order */
46 char zeroes
[124]; /* must be sent as zero bytes */
47 } __attribute__((packed
));
49 #define OLD_VERSION UINT64_C(0x420281861253)
52 #define NBD_FLAG_FIXED_NEWSTYLE 1
54 /* Per-export flags. */
55 #define NBD_FLAG_HAS_FLAGS 1
56 #define NBD_FLAG_READ_ONLY 2
57 #define NBD_FLAG_SEND_FLUSH 4
58 #define NBD_FLAG_SEND_FUA 8
59 #define NBD_FLAG_ROTATIONAL 16
60 #define NBD_FLAG_SEND_TRIM 32
62 /* Request (client -> server). */
64 uint32_t magic
; /* NBD_REQUEST_MAGIC. */
65 uint32_t type
; /* Request type. */
66 uint64_t handle
; /* Opaque handle. */
67 uint64_t offset
; /* Request offset. */
68 uint32_t count
; /* Request length. */
69 } __attribute__((packed
));
71 /* Reply (server -> client). */
73 uint32_t magic
; /* NBD_REPLY_MAGIC. */
74 uint32_t error
; /* 0 = ok, error code */
75 uint64_t handle
; /* Opaque handle. */
76 } __attribute__((packed
));
78 #define NBD_REQUEST_MAGIC 0x25609513
79 #define NBD_REPLY_MAGIC 0x67446698
81 #define NBD_CMD_READ 0
82 #define NBD_CMD_WRITE 1
83 #define NBD_CMD_DISC 2 /* Disconnect. */
84 #define NBD_CMD_FLUSH 3
85 #define NBD_CMD_TRIM 4
86 #define NBD_CMD_MASK_COMMAND 0xffff
87 #define NBD_CMD_FLAG_FUA (1<<16)
89 #endif /* NBDKIT_PROTOCOL_H */