rnbd-proto.h 7.1 KB

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