rnbd-proto.h 7.2 KB

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