Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * RDMA Network Block Driver
0004  *
0005  * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
0006  * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
0007  * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
0008  */
0009 #ifndef RNBD_PROTO_H
0010 #define RNBD_PROTO_H
0011 
0012 #include <linux/types.h>
0013 #include <linux/blk-mq.h>
0014 #include <linux/limits.h>
0015 #include <linux/inet.h>
0016 #include <linux/in.h>
0017 #include <linux/in6.h>
0018 #include <rdma/ib.h>
0019 
0020 #define RNBD_PROTO_VER_MAJOR 2
0021 #define RNBD_PROTO_VER_MINOR 0
0022 
0023 /* The default port number the RTRS server is listening on. */
0024 #define RTRS_PORT 1234
0025 
0026 /**
0027  * enum rnbd_msg_types - RNBD message types
0028  * @RNBD_MSG_SESS_INFO: initial session info from client to server
0029  * @RNBD_MSG_SESS_INFO_RSP: initial session info from server to client
0030  * @RNBD_MSG_OPEN:      open (map) device request
0031  * @RNBD_MSG_OPEN_RSP:      response to an @RNBD_MSG_OPEN
0032  * @RNBD_MSG_IO:        block IO request operation
0033  * @RNBD_MSG_CLOSE:     close (unmap) device request
0034  */
0035 enum rnbd_msg_type {
0036     RNBD_MSG_SESS_INFO,
0037     RNBD_MSG_SESS_INFO_RSP,
0038     RNBD_MSG_OPEN,
0039     RNBD_MSG_OPEN_RSP,
0040     RNBD_MSG_IO,
0041     RNBD_MSG_CLOSE,
0042 };
0043 
0044 /**
0045  * struct rnbd_msg_hdr - header of RNBD messages
0046  * @type:   Message type, valid values see: enum rnbd_msg_types
0047  */
0048 struct rnbd_msg_hdr {
0049     __le16      type;
0050     __le16      __padding;
0051 };
0052 
0053 /**
0054  * We allow to map RO many times and RW only once. We allow to map yet another
0055  * time RW, if MIGRATION is provided (second RW export can be required for
0056  * example for VM migration)
0057  */
0058 enum rnbd_access_mode {
0059     RNBD_ACCESS_RO,
0060     RNBD_ACCESS_RW,
0061     RNBD_ACCESS_MIGRATION,
0062 };
0063 
0064 /**
0065  * struct rnbd_msg_sess_info - initial session info from client to server
0066  * @hdr:        message header
0067  * @ver:        RNBD protocol version
0068  */
0069 struct rnbd_msg_sess_info {
0070     struct rnbd_msg_hdr hdr;
0071     u8      ver;
0072     u8      reserved[31];
0073 };
0074 
0075 /**
0076  * struct rnbd_msg_sess_info_rsp - initial session info from server to client
0077  * @hdr:        message header
0078  * @ver:        RNBD protocol version
0079  */
0080 struct rnbd_msg_sess_info_rsp {
0081     struct rnbd_msg_hdr hdr;
0082     u8      ver;
0083     u8      reserved[31];
0084 };
0085 
0086 /**
0087  * struct rnbd_msg_open - request to open a remote device.
0088  * @hdr:        message header
0089  * @access_mode:    the mode to open remote device, valid values see:
0090  *          enum rnbd_access_mode
0091  * @device_name:    device path on remote side
0092  */
0093 struct rnbd_msg_open {
0094     struct rnbd_msg_hdr hdr;
0095     u8      access_mode;
0096     u8      resv1;
0097     s8      dev_name[NAME_MAX];
0098     u8      reserved[3];
0099 };
0100 
0101 /**
0102  * struct rnbd_msg_close - request to close a remote device.
0103  * @hdr:    message header
0104  * @device_id:  device_id on server side to identify the device
0105  */
0106 struct rnbd_msg_close {
0107     struct rnbd_msg_hdr hdr;
0108     __le32      device_id;
0109 };
0110 
0111 enum rnbd_cache_policy {
0112     RNBD_FUA = 1 << 0,
0113     RNBD_WRITEBACK = 1 << 1,
0114 };
0115 
0116 /**
0117  * struct rnbd_msg_open_rsp - response message to RNBD_MSG_OPEN
0118  * @hdr:        message header
0119  * @device_id:      device_id on server side to identify the device
0120  * @nsectors:       number of sectors in the usual 512b unit
0121  * @max_hw_sectors: max hardware sectors in the usual 512b unit
0122  * @max_write_same_sectors: max sectors for WRITE SAME in the 512b unit
0123  * @max_discard_sectors: max. sectors that can be discarded at once in 512b
0124  * unit.
0125  * @discard_granularity: size of the internal discard allocation unit in bytes
0126  * @discard_alignment: offset from internal allocation assignment in bytes
0127  * @physical_block_size: physical block size device supports in bytes
0128  * @logical_block_size: logical block size device supports in bytes
0129  * @max_segments:   max segments hardware support in one transfer
0130  * @secure_discard: supports secure discard
0131  * @obsolete_rotational: obsolete, not in used.
0132  * @cache_policy:   support write-back caching or FUA?
0133  */
0134 struct rnbd_msg_open_rsp {
0135     struct rnbd_msg_hdr hdr;
0136     __le32          device_id;
0137     __le64          nsectors;
0138     __le32          max_hw_sectors;
0139     __le32          max_write_same_sectors;
0140     __le32          max_discard_sectors;
0141     __le32          discard_granularity;
0142     __le32          discard_alignment;
0143     __le16          physical_block_size;
0144     __le16          logical_block_size;
0145     __le16          max_segments;
0146     __le16          secure_discard;
0147     u8          obsolete_rotational;
0148     u8          cache_policy;
0149     u8          reserved[10];
0150 };
0151 
0152 /**
0153  * struct rnbd_msg_io - message for I/O read/write
0154  * @hdr:    message header
0155  * @device_id:  device_id on server side to find the right device
0156  * @sector: bi_sector attribute from struct bio
0157  * @rw:     valid values are defined in enum rnbd_io_flags
0158  * @bi_size:    number of bytes for I/O read/write
0159  * @prio:       priority
0160  */
0161 struct rnbd_msg_io {
0162     struct rnbd_msg_hdr hdr;
0163     __le32      device_id;
0164     __le64      sector;
0165     __le32      rw;
0166     __le32      bi_size;
0167     __le16      prio;
0168 };
0169 
0170 #define RNBD_OP_BITS  8
0171 #define RNBD_OP_MASK  ((1 << RNBD_OP_BITS) - 1)
0172 
0173 /**
0174  * enum rnbd_io_flags - RNBD request types from rq_flag_bits
0175  * @RNBD_OP_READ:        read sectors from the device
0176  * @RNBD_OP_WRITE:       write sectors to the device
0177  * @RNBD_OP_FLUSH:       flush the volatile write cache
0178  * @RNBD_OP_DISCARD:        discard sectors
0179  * @RNBD_OP_SECURE_ERASE:   securely erase sectors
0180  * @RNBD_OP_WRITE_SAME:     write the same sectors many times
0181 
0182  * @RNBD_F_SYNC:         request is sync (sync write or read)
0183  * @RNBD_F_FUA:             forced unit access
0184  */
0185 enum rnbd_io_flags {
0186 
0187     /* Operations */
0188 
0189     RNBD_OP_READ        = 0,
0190     RNBD_OP_WRITE       = 1,
0191     RNBD_OP_FLUSH       = 2,
0192     RNBD_OP_DISCARD = 3,
0193     RNBD_OP_SECURE_ERASE    = 4,
0194     RNBD_OP_WRITE_SAME  = 5,
0195 
0196     RNBD_OP_LAST,
0197 
0198     /* Flags */
0199 
0200     RNBD_F_SYNC  = 1<<(RNBD_OP_BITS + 0),
0201     RNBD_F_FUA   = 1<<(RNBD_OP_BITS + 1),
0202 
0203     RNBD_F_ALL   = (RNBD_F_SYNC | RNBD_F_FUA)
0204 
0205 };
0206 
0207 static inline u32 rnbd_op(u32 flags)
0208 {
0209     return flags & RNBD_OP_MASK;
0210 }
0211 
0212 static inline u32 rnbd_flags(u32 flags)
0213 {
0214     return flags & ~RNBD_OP_MASK;
0215 }
0216 
0217 static inline bool rnbd_flags_supported(u32 flags)
0218 {
0219     u32 op;
0220 
0221     op = rnbd_op(flags);
0222     flags = rnbd_flags(flags);
0223 
0224     if (op >= RNBD_OP_LAST)
0225         return false;
0226     if (flags & ~RNBD_F_ALL)
0227         return false;
0228 
0229     return true;
0230 }
0231 
0232 static inline blk_opf_t rnbd_to_bio_flags(u32 rnbd_opf)
0233 {
0234     blk_opf_t bio_opf;
0235 
0236     switch (rnbd_op(rnbd_opf)) {
0237     case RNBD_OP_READ:
0238         bio_opf = REQ_OP_READ;
0239         break;
0240     case RNBD_OP_WRITE:
0241         bio_opf = REQ_OP_WRITE;
0242         break;
0243     case RNBD_OP_FLUSH:
0244         bio_opf = REQ_OP_FLUSH | REQ_PREFLUSH;
0245         break;
0246     case RNBD_OP_DISCARD:
0247         bio_opf = REQ_OP_DISCARD;
0248         break;
0249     case RNBD_OP_SECURE_ERASE:
0250         bio_opf = REQ_OP_SECURE_ERASE;
0251         break;
0252     default:
0253         WARN(1, "Unknown RNBD type: %d (flags %d)\n",
0254              rnbd_op(rnbd_opf), rnbd_opf);
0255         bio_opf = 0;
0256     }
0257 
0258     if (rnbd_opf & RNBD_F_SYNC)
0259         bio_opf |= REQ_SYNC;
0260 
0261     if (rnbd_opf & RNBD_F_FUA)
0262         bio_opf |= REQ_FUA;
0263 
0264     return bio_opf;
0265 }
0266 
0267 static inline u32 rq_to_rnbd_flags(struct request *rq)
0268 {
0269     u32 rnbd_opf;
0270 
0271     switch (req_op(rq)) {
0272     case REQ_OP_READ:
0273         rnbd_opf = RNBD_OP_READ;
0274         break;
0275     case REQ_OP_WRITE:
0276         rnbd_opf = RNBD_OP_WRITE;
0277         break;
0278     case REQ_OP_DISCARD:
0279         rnbd_opf = RNBD_OP_DISCARD;
0280         break;
0281     case REQ_OP_SECURE_ERASE:
0282         rnbd_opf = RNBD_OP_SECURE_ERASE;
0283         break;
0284     case REQ_OP_FLUSH:
0285         rnbd_opf = RNBD_OP_FLUSH;
0286         break;
0287     default:
0288         WARN(1, "Unknown request type %d (flags %llu)\n",
0289              (__force u32)req_op(rq),
0290              (__force unsigned long long)rq->cmd_flags);
0291         rnbd_opf = 0;
0292     }
0293 
0294     if (op_is_sync(rq->cmd_flags))
0295         rnbd_opf |= RNBD_F_SYNC;
0296 
0297     if (op_is_flush(rq->cmd_flags))
0298         rnbd_opf |= RNBD_F_FUA;
0299 
0300     return rnbd_opf;
0301 }
0302 
0303 const char *rnbd_access_mode_str(enum rnbd_access_mode mode);
0304 
0305 #endif /* RNBD_PROTO_H */