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 2 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 * @RNBD_F_PREFLUSH: request for cache flush 201 * @RNBD_F_NOUNMAP: do not free blocks when zeroing 202 */ 203 enum rnbd_io_flags { 204 205 /* Operations */ 206 RNBD_OP_READ = 0, 207 RNBD_OP_WRITE = 1, 208 RNBD_OP_FLUSH = 2, 209 RNBD_OP_DISCARD = 3, 210 RNBD_OP_SECURE_ERASE = 4, 211 RNBD_OP_WRITE_ZEROES = 5, 212 213 /* Flags */ 214 RNBD_F_SYNC = 1<<(RNBD_OP_BITS + 0), 215 RNBD_F_FUA = 1<<(RNBD_OP_BITS + 1), 216 RNBD_F_PREFLUSH = 1<<(RNBD_OP_BITS + 2), 217 RNBD_F_NOUNMAP = 1<<(RNBD_OP_BITS + 3) 218 }; 219 220 static inline u32 rnbd_op(u32 flags) 221 { 222 return flags & RNBD_OP_MASK; 223 } 224 225 static inline u32 rnbd_flags(u32 flags) 226 { 227 return flags & ~RNBD_OP_MASK; 228 } 229 230 static inline blk_opf_t rnbd_to_bio_flags(u32 rnbd_opf) 231 { 232 blk_opf_t bio_opf; 233 234 switch (rnbd_op(rnbd_opf)) { 235 case RNBD_OP_READ: 236 bio_opf = REQ_OP_READ; 237 break; 238 case RNBD_OP_WRITE: 239 bio_opf = REQ_OP_WRITE; 240 break; 241 case RNBD_OP_FLUSH: 242 bio_opf = REQ_OP_WRITE | REQ_PREFLUSH; 243 break; 244 case RNBD_OP_DISCARD: 245 bio_opf = REQ_OP_DISCARD; 246 break; 247 case RNBD_OP_SECURE_ERASE: 248 bio_opf = REQ_OP_SECURE_ERASE; 249 break; 250 case RNBD_OP_WRITE_ZEROES: 251 bio_opf = REQ_OP_WRITE_ZEROES; 252 253 if (rnbd_opf & RNBD_F_NOUNMAP) 254 bio_opf |= REQ_NOUNMAP; 255 break; 256 default: 257 WARN(1, "Unknown RNBD type: %d (flags %d)\n", 258 rnbd_op(rnbd_opf), rnbd_opf); 259 bio_opf = 0; 260 } 261 262 if (rnbd_opf & RNBD_F_SYNC) 263 bio_opf |= REQ_SYNC; 264 265 if (rnbd_opf & RNBD_F_FUA) 266 bio_opf |= REQ_FUA; 267 268 if (rnbd_opf & RNBD_F_PREFLUSH) 269 bio_opf |= REQ_PREFLUSH; 270 271 return bio_opf; 272 } 273 274 static inline u32 rq_to_rnbd_flags(struct request *rq) 275 { 276 u32 rnbd_opf; 277 278 switch (req_op(rq)) { 279 case REQ_OP_READ: 280 rnbd_opf = RNBD_OP_READ; 281 break; 282 case REQ_OP_WRITE: 283 rnbd_opf = RNBD_OP_WRITE; 284 break; 285 case REQ_OP_DISCARD: 286 rnbd_opf = RNBD_OP_DISCARD; 287 break; 288 case REQ_OP_SECURE_ERASE: 289 rnbd_opf = RNBD_OP_SECURE_ERASE; 290 break; 291 case REQ_OP_WRITE_ZEROES: 292 rnbd_opf = RNBD_OP_WRITE_ZEROES; 293 294 if (rq->cmd_flags & REQ_NOUNMAP) 295 rnbd_opf |= RNBD_F_NOUNMAP; 296 break; 297 case REQ_OP_FLUSH: 298 rnbd_opf = RNBD_OP_FLUSH; 299 break; 300 default: 301 WARN(1, "Unknown request type %d (flags %llu)\n", 302 (__force u32)req_op(rq), 303 (__force unsigned long long)rq->cmd_flags); 304 rnbd_opf = 0; 305 } 306 307 if (op_is_sync(rq->cmd_flags)) 308 rnbd_opf |= RNBD_F_SYNC; 309 310 if (op_is_flush(rq->cmd_flags)) 311 rnbd_opf |= RNBD_F_FUA; 312 313 if (rq->cmd_flags & REQ_PREFLUSH) 314 rnbd_opf |= RNBD_F_PREFLUSH; 315 316 return rnbd_opf; 317 } 318 319 const char *rnbd_access_mode_str(enum rnbd_access_mode mode); 320 321 #endif /* RNBD_PROTO_H */ 322