| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * RDMA Network Block Driver |
| 4 | * |
| 5 | * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved. |
| 6 | * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved. |
| 7 | * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved. |
| 8 | */ |
| 9 | #ifndef RNBD_PROTO_H |
| 10 | #define RNBD_PROTO_H |
| 11 | |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/blk-mq.h> |
| 14 | #include <linux/limits.h> |
| 15 | #include <linux/inet.h> |
| 16 | #include <linux/in.h> |
| 17 | #include <linux/in6.h> |
| 18 | #include <rdma/ib.h> |
| 19 | |
| 20 | #define RNBD_PROTO_VER_MAJOR 2 |
| 21 | #define RNBD_PROTO_VER_MINOR 0 |
| 22 | |
| 23 | /* The default port number the RTRS server is listening on. */ |
| 24 | #define RTRS_PORT 1234 |
| 25 | |
| 26 | /** |
| 27 | * enum rnbd_msg_type - RNBD message types |
| 28 | * @RNBD_MSG_SESS_INFO: initial session info from client to server |
| 29 | * @RNBD_MSG_SESS_INFO_RSP: initial session info from server to client |
| 30 | * @RNBD_MSG_OPEN: open (map) device request |
| 31 | * @RNBD_MSG_OPEN_RSP: response to an @RNBD_MSG_OPEN |
| 32 | * @RNBD_MSG_IO: block IO request operation |
| 33 | * @RNBD_MSG_CLOSE: close (unmap) device request |
| 34 | */ |
| 35 | enum rnbd_msg_type { |
| 36 | RNBD_MSG_SESS_INFO, |
| 37 | RNBD_MSG_SESS_INFO_RSP, |
| 38 | RNBD_MSG_OPEN, |
| 39 | RNBD_MSG_OPEN_RSP, |
| 40 | RNBD_MSG_IO, |
| 41 | RNBD_MSG_CLOSE, |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * struct rnbd_msg_hdr - header of RNBD messages |
| 46 | * @type: Message type, valid values see: enum rnbd_msg_types |
| 47 | */ |
| 48 | struct rnbd_msg_hdr { |
| 49 | __le16 type; |
| 50 | /* private: */ |
| 51 | __le16 __padding; |
| 52 | }; |
| 53 | |
| 54 | /* |
| 55 | * We allow to map RO many times and RW only once. We allow to map yet another |
| 56 | * time RW, if MIGRATION is provided (second RW export can be required for |
| 57 | * example for VM migration) |
| 58 | */ |
| 59 | enum rnbd_access_mode { |
| 60 | RNBD_ACCESS_RO, |
| 61 | RNBD_ACCESS_RW, |
| 62 | RNBD_ACCESS_MIGRATION, |
| 63 | }; |
| 64 | |
| 65 | static const __maybe_unused struct { |
| 66 | enum rnbd_access_mode mode; |
| 67 | const char *str; |
| 68 | } rnbd_access_modes[] = { |
| 69 | [RNBD_ACCESS_RO] = {RNBD_ACCESS_RO, "ro" }, |
| 70 | [RNBD_ACCESS_RW] = {RNBD_ACCESS_RW, "rw" }, |
| 71 | [RNBD_ACCESS_MIGRATION] = {RNBD_ACCESS_MIGRATION, "migration" }, |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * struct rnbd_msg_sess_info - initial session info from client to server |
| 76 | * @hdr: message header |
| 77 | * @ver: RNBD protocol version |
| 78 | */ |
| 79 | struct rnbd_msg_sess_info { |
| 80 | struct rnbd_msg_hdr hdr; |
| 81 | u8 ver; |
| 82 | /* private: */ |
| 83 | u8 reserved[31]; |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * struct rnbd_msg_sess_info_rsp - initial session info from server to client |
| 88 | * @hdr: message header |
| 89 | * @ver: RNBD protocol version |
| 90 | */ |
| 91 | struct rnbd_msg_sess_info_rsp { |
| 92 | struct rnbd_msg_hdr hdr; |
| 93 | u8 ver; |
| 94 | /* private: */ |
| 95 | u8 reserved[31]; |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * struct rnbd_msg_open - request to open a remote device. |
| 100 | * @hdr: message header |
| 101 | * @access_mode: the mode to open remote device, valid values see: |
| 102 | * enum rnbd_access_mode |
| 103 | * @dev_name: device path on remote side |
| 104 | */ |
| 105 | struct rnbd_msg_open { |
| 106 | struct rnbd_msg_hdr hdr; |
| 107 | u8 access_mode; |
| 108 | /* private: */ |
| 109 | u8 resv1; |
| 110 | /* public: */ |
| 111 | s8 dev_name[NAME_MAX]; |
| 112 | /* private: */ |
| 113 | u8 reserved[3]; |
| 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * struct rnbd_msg_close - request to close a remote device. |
| 118 | * @hdr: message header |
| 119 | * @device_id: device_id on server side to identify the device |
| 120 | */ |
| 121 | struct rnbd_msg_close { |
| 122 | struct rnbd_msg_hdr hdr; |
| 123 | __le32 device_id; |
| 124 | }; |
| 125 | |
| 126 | enum rnbd_cache_policy { |
| 127 | RNBD_FUA = 1 << 0, |
| 128 | RNBD_WRITEBACK = 1 << 1, |
| 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * struct rnbd_msg_open_rsp - response message to RNBD_MSG_OPEN |
| 133 | * @hdr: message header |
| 134 | * @device_id: device_id on server side to identify the device |
| 135 | * @nsectors: number of sectors in the usual 512b unit |
| 136 | * @max_hw_sectors: max hardware sectors in the usual 512b unit |
| 137 | * @max_write_zeroes_sectors: max sectors for WRITE ZEROES in the 512b unit |
| 138 | * @max_discard_sectors: max. sectors that can be discarded at once in 512b |
| 139 | * unit. |
| 140 | * @discard_granularity: size of the internal discard allocation unit in bytes |
| 141 | * @discard_alignment: offset from internal allocation assignment in bytes |
| 142 | * @physical_block_size: physical block size device supports in bytes |
| 143 | * @logical_block_size: logical block size device supports in bytes |
| 144 | * @max_segments: max segments hardware support in one transfer |
| 145 | * @secure_discard: supports secure discard |
| 146 | * @obsolete_rotational: obsolete, not in used. |
| 147 | * @cache_policy: support write-back caching or FUA? |
| 148 | */ |
| 149 | struct rnbd_msg_open_rsp { |
| 150 | struct rnbd_msg_hdr hdr; |
| 151 | __le32 device_id; |
| 152 | __le64 nsectors; |
| 153 | __le32 max_hw_sectors; |
| 154 | __le32 max_write_zeroes_sectors; |
| 155 | __le32 max_discard_sectors; |
| 156 | __le32 discard_granularity; |
| 157 | __le32 discard_alignment; |
| 158 | __le16 physical_block_size; |
| 159 | __le16 logical_block_size; |
| 160 | __le16 max_segments; |
| 161 | __le16 secure_discard; |
| 162 | u8 obsolete_rotational; |
| 163 | u8 cache_policy; |
| 164 | /* private: */ |
| 165 | u8 reserved[10]; |
| 166 | }; |
| 167 | |
| 168 | /** |
| 169 | * struct rnbd_msg_io - message for I/O read/write |
| 170 | * @hdr: message header |
| 171 | * @device_id: device_id on server side to find the right device |
| 172 | * @sector: bi_sector attribute from struct bio |
| 173 | * @rw: valid values are defined in enum rnbd_io_flags |
| 174 | * @bi_size: number of bytes for I/O read/write |
| 175 | * @prio: priority |
| 176 | */ |
| 177 | struct rnbd_msg_io { |
| 178 | struct rnbd_msg_hdr hdr; |
| 179 | __le32 device_id; |
| 180 | __le64 sector; |
| 181 | __le32 rw; |
| 182 | __le32 bi_size; |
| 183 | __le16 prio; |
| 184 | }; |
| 185 | |
| 186 | #define RNBD_OP_BITS 8 |
| 187 | #define RNBD_OP_MASK ((1 << RNBD_OP_BITS) - 1) |
| 188 | |
| 189 | /** |
| 190 | * enum rnbd_io_flags - RNBD request types from rq_flag_bits |
| 191 | * @RNBD_OP_READ: read sectors from the device |
| 192 | * @RNBD_OP_WRITE: write sectors to the device |
| 193 | * @RNBD_OP_FLUSH: flush the volatile write cache |
| 194 | * @RNBD_OP_DISCARD: discard sectors |
| 195 | * @RNBD_OP_SECURE_ERASE: securely erase sectors |
| 196 | * @RNBD_OP_WRITE_ZEROES: write zeroes sectors |
| 197 | * |
| 198 | * @RNBD_F_SYNC: request is sync (sync write or read) |
| 199 | * @RNBD_F_FUA: forced unit access |
| 200 | */ |
| 201 | enum rnbd_io_flags { |
| 202 | |
| 203 | /* Operations */ |
| 204 | RNBD_OP_READ = 0, |
| 205 | RNBD_OP_WRITE = 1, |
| 206 | RNBD_OP_FLUSH = 2, |
| 207 | RNBD_OP_DISCARD = 3, |
| 208 | RNBD_OP_SECURE_ERASE = 4, |
| 209 | RNBD_OP_WRITE_ZEROES = 5, |
| 210 | |
| 211 | /* Flags */ |
| 212 | RNBD_F_SYNC = 1<<(RNBD_OP_BITS + 0), |
| 213 | RNBD_F_FUA = 1<<(RNBD_OP_BITS + 1), |
| 214 | }; |
| 215 | |
| 216 | static inline u32 rnbd_op(u32 flags) |
| 217 | { |
| 218 | return flags & RNBD_OP_MASK; |
| 219 | } |
| 220 | |
| 221 | static inline u32 rnbd_flags(u32 flags) |
| 222 | { |
| 223 | return flags & ~RNBD_OP_MASK; |
| 224 | } |
| 225 | |
| 226 | static inline blk_opf_t rnbd_to_bio_flags(u32 rnbd_opf) |
| 227 | { |
| 228 | blk_opf_t bio_opf; |
| 229 | |
| 230 | switch (rnbd_op(flags: rnbd_opf)) { |
| 231 | case RNBD_OP_READ: |
| 232 | bio_opf = REQ_OP_READ; |
| 233 | break; |
| 234 | case RNBD_OP_WRITE: |
| 235 | bio_opf = REQ_OP_WRITE; |
| 236 | break; |
| 237 | case RNBD_OP_FLUSH: |
| 238 | bio_opf = REQ_OP_WRITE | REQ_PREFLUSH; |
| 239 | break; |
| 240 | case RNBD_OP_DISCARD: |
| 241 | bio_opf = REQ_OP_DISCARD; |
| 242 | break; |
| 243 | case RNBD_OP_SECURE_ERASE: |
| 244 | bio_opf = REQ_OP_SECURE_ERASE; |
| 245 | break; |
| 246 | case RNBD_OP_WRITE_ZEROES: |
| 247 | bio_opf = REQ_OP_WRITE_ZEROES; |
| 248 | break; |
| 249 | default: |
| 250 | WARN(1, "Unknown RNBD type: %d (flags %d)\n" , |
| 251 | rnbd_op(rnbd_opf), rnbd_opf); |
| 252 | bio_opf = 0; |
| 253 | } |
| 254 | |
| 255 | if (rnbd_opf & RNBD_F_SYNC) |
| 256 | bio_opf |= REQ_SYNC; |
| 257 | |
| 258 | if (rnbd_opf & RNBD_F_FUA) |
| 259 | bio_opf |= REQ_FUA; |
| 260 | |
| 261 | return bio_opf; |
| 262 | } |
| 263 | |
| 264 | static inline u32 rq_to_rnbd_flags(struct request *rq) |
| 265 | { |
| 266 | u32 rnbd_opf; |
| 267 | |
| 268 | switch (req_op(req: rq)) { |
| 269 | case REQ_OP_READ: |
| 270 | rnbd_opf = RNBD_OP_READ; |
| 271 | break; |
| 272 | case REQ_OP_WRITE: |
| 273 | rnbd_opf = RNBD_OP_WRITE; |
| 274 | break; |
| 275 | case REQ_OP_DISCARD: |
| 276 | rnbd_opf = RNBD_OP_DISCARD; |
| 277 | break; |
| 278 | case REQ_OP_SECURE_ERASE: |
| 279 | rnbd_opf = RNBD_OP_SECURE_ERASE; |
| 280 | break; |
| 281 | case REQ_OP_WRITE_ZEROES: |
| 282 | rnbd_opf = RNBD_OP_WRITE_ZEROES; |
| 283 | break; |
| 284 | case REQ_OP_FLUSH: |
| 285 | rnbd_opf = RNBD_OP_FLUSH; |
| 286 | break; |
| 287 | default: |
| 288 | WARN(1, "Unknown request type %d (flags %llu)\n" , |
| 289 | (__force u32)req_op(rq), |
| 290 | (__force unsigned long long)rq->cmd_flags); |
| 291 | rnbd_opf = 0; |
| 292 | } |
| 293 | |
| 294 | if (op_is_sync(op: rq->cmd_flags)) |
| 295 | rnbd_opf |= RNBD_F_SYNC; |
| 296 | |
| 297 | if (op_is_flush(op: rq->cmd_flags)) |
| 298 | rnbd_opf |= RNBD_F_FUA; |
| 299 | |
| 300 | return rnbd_opf; |
| 301 | } |
| 302 | |
| 303 | const char *rnbd_access_mode_str(enum rnbd_access_mode mode); |
| 304 | |
| 305 | #endif /* RNBD_PROTO_H */ |
| 306 | |