rnbd-proto.h 7.8 KB

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