0001
0002 #ifndef __FS_CEPH_MESSENGER_H
0003 #define __FS_CEPH_MESSENGER_H
0004
0005 #include <linux/bvec.h>
0006 #include <linux/crypto.h>
0007 #include <linux/kref.h>
0008 #include <linux/mutex.h>
0009 #include <linux/net.h>
0010 #include <linux/radix-tree.h>
0011 #include <linux/uio.h>
0012 #include <linux/workqueue.h>
0013 #include <net/net_namespace.h>
0014
0015 #include <linux/ceph/types.h>
0016 #include <linux/ceph/buffer.h>
0017
0018 struct ceph_msg;
0019 struct ceph_connection;
0020
0021
0022
0023
0024 struct ceph_connection_operations {
0025 struct ceph_connection *(*get)(struct ceph_connection *);
0026 void (*put)(struct ceph_connection *);
0027
0028
0029 void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
0030
0031
0032 struct ceph_auth_handshake *(*get_authorizer) (
0033 struct ceph_connection *con,
0034 int *proto, int force_new);
0035 int (*add_authorizer_challenge)(struct ceph_connection *con,
0036 void *challenge_buf,
0037 int challenge_buf_len);
0038 int (*verify_authorizer_reply) (struct ceph_connection *con);
0039 int (*invalidate_authorizer)(struct ceph_connection *con);
0040
0041
0042 void (*fault) (struct ceph_connection *con);
0043
0044
0045
0046 void (*peer_reset) (struct ceph_connection *con);
0047
0048 struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
0049 struct ceph_msg_header *hdr,
0050 int *skip);
0051
0052 void (*reencode_message) (struct ceph_msg *msg);
0053
0054 int (*sign_message) (struct ceph_msg *msg);
0055 int (*check_message_signature) (struct ceph_msg *msg);
0056
0057
0058 int (*get_auth_request)(struct ceph_connection *con,
0059 void *buf, int *buf_len,
0060 void **authorizer, int *authorizer_len);
0061 int (*handle_auth_reply_more)(struct ceph_connection *con,
0062 void *reply, int reply_len,
0063 void *buf, int *buf_len,
0064 void **authorizer, int *authorizer_len);
0065 int (*handle_auth_done)(struct ceph_connection *con,
0066 u64 global_id, void *reply, int reply_len,
0067 u8 *session_key, int *session_key_len,
0068 u8 *con_secret, int *con_secret_len);
0069 int (*handle_auth_bad_method)(struct ceph_connection *con,
0070 int used_proto, int result,
0071 const int *allowed_protos, int proto_cnt,
0072 const int *allowed_modes, int mode_cnt);
0073 };
0074
0075
0076 #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
0077
0078 struct ceph_messenger {
0079 struct ceph_entity_inst inst;
0080 struct ceph_entity_addr my_enc_addr;
0081
0082 atomic_t stopping;
0083 possible_net_t net;
0084
0085
0086
0087
0088
0089 u32 global_seq;
0090 spinlock_t global_seq_lock;
0091 };
0092
0093 enum ceph_msg_data_type {
0094 CEPH_MSG_DATA_NONE,
0095 CEPH_MSG_DATA_PAGES,
0096 CEPH_MSG_DATA_PAGELIST,
0097 #ifdef CONFIG_BLOCK
0098 CEPH_MSG_DATA_BIO,
0099 #endif
0100 CEPH_MSG_DATA_BVECS,
0101 };
0102
0103 #ifdef CONFIG_BLOCK
0104
0105 struct ceph_bio_iter {
0106 struct bio *bio;
0107 struct bvec_iter iter;
0108 };
0109
0110 #define __ceph_bio_iter_advance_step(it, n, STEP) do { \
0111 unsigned int __n = (n), __cur_n; \
0112 \
0113 while (__n) { \
0114 BUG_ON(!(it)->iter.bi_size); \
0115 __cur_n = min((it)->iter.bi_size, __n); \
0116 (void)(STEP); \
0117 bio_advance_iter((it)->bio, &(it)->iter, __cur_n); \
0118 if (!(it)->iter.bi_size && (it)->bio->bi_next) { \
0119 dout("__ceph_bio_iter_advance_step next bio\n"); \
0120 (it)->bio = (it)->bio->bi_next; \
0121 (it)->iter = (it)->bio->bi_iter; \
0122 } \
0123 __n -= __cur_n; \
0124 } \
0125 } while (0)
0126
0127
0128
0129
0130 #define ceph_bio_iter_advance(it, n) \
0131 __ceph_bio_iter_advance_step(it, n, 0)
0132
0133
0134
0135
0136 #define ceph_bio_iter_advance_step(it, n, BVEC_STEP) \
0137 __ceph_bio_iter_advance_step(it, n, ({ \
0138 struct bio_vec bv; \
0139 struct bvec_iter __cur_iter; \
0140 \
0141 __cur_iter = (it)->iter; \
0142 __cur_iter.bi_size = __cur_n; \
0143 __bio_for_each_segment(bv, (it)->bio, __cur_iter, __cur_iter) \
0144 (void)(BVEC_STEP); \
0145 }))
0146
0147 #endif
0148
0149 struct ceph_bvec_iter {
0150 struct bio_vec *bvecs;
0151 struct bvec_iter iter;
0152 };
0153
0154 #define __ceph_bvec_iter_advance_step(it, n, STEP) do { \
0155 BUG_ON((n) > (it)->iter.bi_size); \
0156 (void)(STEP); \
0157 bvec_iter_advance((it)->bvecs, &(it)->iter, (n)); \
0158 } while (0)
0159
0160
0161
0162
0163 #define ceph_bvec_iter_advance(it, n) \
0164 __ceph_bvec_iter_advance_step(it, n, 0)
0165
0166
0167
0168
0169 #define ceph_bvec_iter_advance_step(it, n, BVEC_STEP) \
0170 __ceph_bvec_iter_advance_step(it, n, ({ \
0171 struct bio_vec bv; \
0172 struct bvec_iter __cur_iter; \
0173 \
0174 __cur_iter = (it)->iter; \
0175 __cur_iter.bi_size = (n); \
0176 for_each_bvec(bv, (it)->bvecs, __cur_iter, __cur_iter) \
0177 (void)(BVEC_STEP); \
0178 }))
0179
0180 #define ceph_bvec_iter_shorten(it, n) do { \
0181 BUG_ON((n) > (it)->iter.bi_size); \
0182 (it)->iter.bi_size = (n); \
0183 } while (0)
0184
0185 struct ceph_msg_data {
0186 enum ceph_msg_data_type type;
0187 union {
0188 #ifdef CONFIG_BLOCK
0189 struct {
0190 struct ceph_bio_iter bio_pos;
0191 u32 bio_length;
0192 };
0193 #endif
0194 struct ceph_bvec_iter bvec_pos;
0195 struct {
0196 struct page **pages;
0197 size_t length;
0198 unsigned int alignment;
0199 bool own_pages;
0200 };
0201 struct ceph_pagelist *pagelist;
0202 };
0203 };
0204
0205 struct ceph_msg_data_cursor {
0206 size_t total_resid;
0207
0208 struct ceph_msg_data *data;
0209 size_t resid;
0210 bool last_piece;
0211 bool need_crc;
0212 union {
0213 #ifdef CONFIG_BLOCK
0214 struct ceph_bio_iter bio_iter;
0215 #endif
0216 struct bvec_iter bvec_iter;
0217 struct {
0218 unsigned int page_offset;
0219 unsigned short page_index;
0220 unsigned short page_count;
0221 };
0222 struct {
0223 struct page *page;
0224 size_t offset;
0225 };
0226 };
0227 };
0228
0229
0230
0231
0232
0233
0234 struct ceph_msg {
0235 struct ceph_msg_header hdr;
0236 union {
0237 struct ceph_msg_footer footer;
0238 struct ceph_msg_footer_old old_footer;
0239 };
0240 struct kvec front;
0241 struct ceph_buffer *middle;
0242
0243 size_t data_length;
0244 struct ceph_msg_data *data;
0245 int num_data_items;
0246 int max_data_items;
0247 struct ceph_msg_data_cursor cursor;
0248
0249 struct ceph_connection *con;
0250 struct list_head list_head;
0251
0252 struct kref kref;
0253 bool more_to_follow;
0254 bool needs_out_seq;
0255 int front_alloc_len;
0256
0257 struct ceph_msgpool *pool;
0258 };
0259
0260
0261
0262
0263 #define CEPH_CON_S_CLOSED 1
0264 #define CEPH_CON_S_PREOPEN 2
0265 #define CEPH_CON_S_V1_BANNER 3
0266 #define CEPH_CON_S_V1_CONNECT_MSG 4
0267 #define CEPH_CON_S_V2_BANNER_PREFIX 5
0268 #define CEPH_CON_S_V2_BANNER_PAYLOAD 6
0269 #define CEPH_CON_S_V2_HELLO 7
0270 #define CEPH_CON_S_V2_AUTH 8
0271 #define CEPH_CON_S_V2_AUTH_SIGNATURE 9
0272 #define CEPH_CON_S_V2_SESSION_CONNECT 10
0273 #define CEPH_CON_S_V2_SESSION_RECONNECT 11
0274 #define CEPH_CON_S_OPEN 12
0275 #define CEPH_CON_S_STANDBY 13
0276
0277
0278
0279
0280 #define CEPH_CON_F_LOSSYTX 0
0281
0282 #define CEPH_CON_F_KEEPALIVE_PENDING 1
0283 #define CEPH_CON_F_WRITE_PENDING 2
0284 #define CEPH_CON_F_SOCK_CLOSED 3
0285 #define CEPH_CON_F_BACKOFF 4
0286
0287
0288
0289 #define BASE_DELAY_INTERVAL (HZ / 4)
0290 #define MAX_DELAY_INTERVAL (15 * HZ)
0291
0292 struct ceph_connection_v1_info {
0293 struct kvec out_kvec[8],
0294 *out_kvec_cur;
0295 int out_kvec_left;
0296 int out_skip;
0297 int out_kvec_bytes;
0298 bool out_more;
0299 bool out_msg_done;
0300
0301 struct ceph_auth_handshake *auth;
0302 int auth_retry;
0303
0304
0305 u8 in_banner[CEPH_BANNER_MAX_LEN];
0306 struct ceph_entity_addr actual_peer_addr;
0307 struct ceph_entity_addr peer_addr_for_me;
0308 struct ceph_msg_connect out_connect;
0309 struct ceph_msg_connect_reply in_reply;
0310
0311 int in_base_pos;
0312
0313
0314 u8 in_tag;
0315 struct ceph_msg_header in_hdr;
0316 __le64 in_temp_ack;
0317
0318
0319 struct ceph_msg_header out_hdr;
0320 __le64 out_temp_ack;
0321 struct ceph_timespec out_temp_keepalive2;
0322
0323
0324 u32 connect_seq;
0325
0326 u32 peer_global_seq;
0327 };
0328
0329 #define CEPH_CRC_LEN 4
0330 #define CEPH_GCM_KEY_LEN 16
0331 #define CEPH_GCM_IV_LEN sizeof(struct ceph_gcm_nonce)
0332 #define CEPH_GCM_BLOCK_LEN 16
0333 #define CEPH_GCM_TAG_LEN 16
0334
0335 #define CEPH_PREAMBLE_LEN 32
0336 #define CEPH_PREAMBLE_INLINE_LEN 48
0337 #define CEPH_PREAMBLE_PLAIN_LEN CEPH_PREAMBLE_LEN
0338 #define CEPH_PREAMBLE_SECURE_LEN (CEPH_PREAMBLE_LEN + \
0339 CEPH_PREAMBLE_INLINE_LEN + \
0340 CEPH_GCM_TAG_LEN)
0341 #define CEPH_EPILOGUE_PLAIN_LEN (1 + 3 * CEPH_CRC_LEN)
0342 #define CEPH_EPILOGUE_SECURE_LEN (CEPH_GCM_BLOCK_LEN + CEPH_GCM_TAG_LEN)
0343
0344 #define CEPH_FRAME_MAX_SEGMENT_COUNT 4
0345
0346 struct ceph_frame_desc {
0347 int fd_tag;
0348 int fd_seg_cnt;
0349 int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT];
0350 int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT];
0351 };
0352
0353 struct ceph_gcm_nonce {
0354 __le32 fixed;
0355 __le64 counter __packed;
0356 };
0357
0358 struct ceph_connection_v2_info {
0359 struct iov_iter in_iter;
0360 struct kvec in_kvecs[5];
0361 struct bio_vec in_bvec;
0362 int in_kvec_cnt;
0363 int in_state;
0364
0365 struct iov_iter out_iter;
0366 struct kvec out_kvecs[8];
0367 struct bio_vec out_bvec;
0368
0369 int out_kvec_cnt;
0370 int out_state;
0371
0372 int out_zero;
0373 bool out_iter_sendpage;
0374
0375 struct ceph_frame_desc in_desc;
0376 struct ceph_msg_data_cursor in_cursor;
0377 struct ceph_msg_data_cursor out_cursor;
0378
0379 struct crypto_shash *hmac_tfm;
0380 struct crypto_aead *gcm_tfm;
0381 struct aead_request *gcm_req;
0382 struct crypto_wait gcm_wait;
0383 struct ceph_gcm_nonce in_gcm_nonce;
0384 struct ceph_gcm_nonce out_gcm_nonce;
0385
0386 struct page **in_enc_pages;
0387 int in_enc_page_cnt;
0388 int in_enc_resid;
0389 int in_enc_i;
0390 struct page **out_enc_pages;
0391 int out_enc_page_cnt;
0392 int out_enc_resid;
0393 int out_enc_i;
0394
0395 int con_mode;
0396
0397 void *conn_bufs[16];
0398 int conn_buf_cnt;
0399
0400 struct kvec in_sign_kvecs[8];
0401 struct kvec out_sign_kvecs[8];
0402 int in_sign_kvec_cnt;
0403 int out_sign_kvec_cnt;
0404
0405 u64 client_cookie;
0406 u64 server_cookie;
0407 u64 global_seq;
0408 u64 connect_seq;
0409 u64 peer_global_seq;
0410
0411 u8 in_buf[CEPH_PREAMBLE_SECURE_LEN];
0412 u8 out_buf[CEPH_PREAMBLE_SECURE_LEN];
0413 struct {
0414 u8 late_status;
0415 union {
0416 struct {
0417 u32 front_crc;
0418 u32 middle_crc;
0419 u32 data_crc;
0420 } __packed;
0421 u8 pad[CEPH_GCM_BLOCK_LEN - 1];
0422 };
0423 } out_epil;
0424 };
0425
0426
0427
0428
0429
0430
0431
0432
0433 struct ceph_connection {
0434 void *private;
0435
0436 const struct ceph_connection_operations *ops;
0437
0438 struct ceph_messenger *msgr;
0439
0440 int state;
0441 atomic_t sock_state;
0442 struct socket *sock;
0443
0444 unsigned long flags;
0445 const char *error_msg;
0446
0447 struct ceph_entity_name peer_name;
0448 struct ceph_entity_addr peer_addr;
0449 u64 peer_features;
0450
0451 struct mutex mutex;
0452
0453
0454 struct list_head out_queue;
0455 struct list_head out_sent;
0456 u64 out_seq;
0457
0458 u64 in_seq, in_seq_acked;
0459
0460 struct ceph_msg *in_msg;
0461 struct ceph_msg *out_msg;
0462
0463
0464 struct page *bounce_page;
0465 u32 in_front_crc, in_middle_crc, in_data_crc;
0466
0467 struct timespec64 last_keepalive_ack;
0468
0469 struct delayed_work work;
0470 unsigned long delay;
0471
0472 union {
0473 struct ceph_connection_v1_info v1;
0474 struct ceph_connection_v2_info v2;
0475 };
0476 };
0477
0478 extern struct page *ceph_zero_page;
0479
0480 void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag);
0481 void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag);
0482 bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag);
0483 bool ceph_con_flag_test_and_clear(struct ceph_connection *con,
0484 unsigned long con_flag);
0485 bool ceph_con_flag_test_and_set(struct ceph_connection *con,
0486 unsigned long con_flag);
0487
0488 void ceph_encode_my_addr(struct ceph_messenger *msgr);
0489
0490 int ceph_tcp_connect(struct ceph_connection *con);
0491 int ceph_con_close_socket(struct ceph_connection *con);
0492 void ceph_con_reset_session(struct ceph_connection *con);
0493
0494 u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt);
0495 void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq);
0496 void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq);
0497
0498 void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
0499 struct ceph_msg *msg, size_t length);
0500 struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
0501 size_t *page_offset, size_t *length,
0502 bool *last_piece);
0503 void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes);
0504
0505 u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset,
0506 unsigned int length);
0507
0508 bool ceph_addr_is_blank(const struct ceph_entity_addr *addr);
0509 int ceph_addr_port(const struct ceph_entity_addr *addr);
0510 void ceph_addr_set_port(struct ceph_entity_addr *addr, int p);
0511
0512 void ceph_con_process_message(struct ceph_connection *con);
0513 int ceph_con_in_msg_alloc(struct ceph_connection *con,
0514 struct ceph_msg_header *hdr, int *skip);
0515 void ceph_con_get_out_msg(struct ceph_connection *con);
0516
0517
0518 int ceph_con_v1_try_read(struct ceph_connection *con);
0519 int ceph_con_v1_try_write(struct ceph_connection *con);
0520 void ceph_con_v1_revoke(struct ceph_connection *con);
0521 void ceph_con_v1_revoke_incoming(struct ceph_connection *con);
0522 bool ceph_con_v1_opened(struct ceph_connection *con);
0523 void ceph_con_v1_reset_session(struct ceph_connection *con);
0524 void ceph_con_v1_reset_protocol(struct ceph_connection *con);
0525
0526
0527 int ceph_con_v2_try_read(struct ceph_connection *con);
0528 int ceph_con_v2_try_write(struct ceph_connection *con);
0529 void ceph_con_v2_revoke(struct ceph_connection *con);
0530 void ceph_con_v2_revoke_incoming(struct ceph_connection *con);
0531 bool ceph_con_v2_opened(struct ceph_connection *con);
0532 void ceph_con_v2_reset_session(struct ceph_connection *con);
0533 void ceph_con_v2_reset_protocol(struct ceph_connection *con);
0534
0535
0536 extern const char *ceph_pr_addr(const struct ceph_entity_addr *addr);
0537
0538 extern int ceph_parse_ips(const char *c, const char *end,
0539 struct ceph_entity_addr *addr,
0540 int max_count, int *count, char delim);
0541
0542 extern int ceph_msgr_init(void);
0543 extern void ceph_msgr_exit(void);
0544 extern void ceph_msgr_flush(void);
0545
0546 extern void ceph_messenger_init(struct ceph_messenger *msgr,
0547 struct ceph_entity_addr *myaddr);
0548 extern void ceph_messenger_fini(struct ceph_messenger *msgr);
0549 extern void ceph_messenger_reset_nonce(struct ceph_messenger *msgr);
0550
0551 extern void ceph_con_init(struct ceph_connection *con, void *private,
0552 const struct ceph_connection_operations *ops,
0553 struct ceph_messenger *msgr);
0554 extern void ceph_con_open(struct ceph_connection *con,
0555 __u8 entity_type, __u64 entity_num,
0556 struct ceph_entity_addr *addr);
0557 extern bool ceph_con_opened(struct ceph_connection *con);
0558 extern void ceph_con_close(struct ceph_connection *con);
0559 extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
0560
0561 extern void ceph_msg_revoke(struct ceph_msg *msg);
0562 extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
0563
0564 extern void ceph_con_keepalive(struct ceph_connection *con);
0565 extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
0566 unsigned long interval);
0567
0568 void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
0569 size_t length, size_t alignment, bool own_pages);
0570 extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
0571 struct ceph_pagelist *pagelist);
0572 #ifdef CONFIG_BLOCK
0573 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
0574 u32 length);
0575 #endif
0576 void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
0577 struct ceph_bvec_iter *bvec_pos);
0578
0579 struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
0580 gfp_t flags, bool can_fail);
0581 extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
0582 bool can_fail);
0583
0584 extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
0585 extern void ceph_msg_put(struct ceph_msg *msg);
0586
0587 extern void ceph_msg_dump(struct ceph_msg *msg);
0588
0589 #endif