1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __FS_CEPH_MESSENGER_H
3 #define __FS_CEPH_MESSENGER_H
5 #include <linux/bvec.h>
6 #include <linux/crypto.h>
7 #include <linux/kref.h>
8 #include <linux/mutex.h>
10 #include <linux/radix-tree.h>
11 #include <linux/uio.h>
12 #include <linux/workqueue.h>
13 #include <net/net_namespace.h>
15 #include <linux/ceph/types.h>
16 #include <linux/ceph/buffer.h>
19 struct ceph_connection;
22 * Ceph defines these callbacks for handling connection events.
24 struct ceph_connection_operations {
25 struct ceph_connection *(*get)(struct ceph_connection *);
26 void (*put)(struct ceph_connection *);
28 /* handle an incoming message. */
29 void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
31 /* authorize an outgoing connection */
32 struct ceph_auth_handshake *(*get_authorizer) (
33 struct ceph_connection *con,
34 int *proto, int force_new);
35 int (*add_authorizer_challenge)(struct ceph_connection *con,
37 int challenge_buf_len);
38 int (*verify_authorizer_reply) (struct ceph_connection *con);
39 int (*invalidate_authorizer)(struct ceph_connection *con);
41 /* there was some error on the socket (disconnect, whatever) */
42 void (*fault) (struct ceph_connection *con);
44 /* a remote host as terminated a message exchange session, and messages
45 * we sent (or they tried to send us) may be lost. */
46 void (*peer_reset) (struct ceph_connection *con);
48 struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
49 struct ceph_msg_header *hdr,
52 void (*reencode_message) (struct ceph_msg *msg);
54 int (*sign_message) (struct ceph_msg *msg);
55 int (*check_message_signature) (struct ceph_msg *msg);
57 /* msgr2 authentication exchange */
58 int (*get_auth_request)(struct ceph_connection *con,
59 void *buf, int *buf_len,
60 void **authorizer, int *authorizer_len);
61 int (*handle_auth_reply_more)(struct ceph_connection *con,
62 void *reply, int reply_len,
63 void *buf, int *buf_len,
64 void **authorizer, int *authorizer_len);
65 int (*handle_auth_done)(struct ceph_connection *con,
66 u64 global_id, void *reply, int reply_len,
67 u8 *session_key, int *session_key_len,
68 u8 *con_secret, int *con_secret_len);
69 int (*handle_auth_bad_method)(struct ceph_connection *con,
70 int used_proto, int result,
71 const int *allowed_protos, int proto_cnt,
72 const int *allowed_modes, int mode_cnt);
75 /* use format string %s%lld */
76 #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
78 struct ceph_messenger {
79 struct ceph_entity_inst inst; /* my name+address */
80 struct ceph_entity_addr my_enc_addr;
86 * the global_seq counts connections i (attempt to) initiate
87 * in order to disambiguate certain connect race conditions.
90 spinlock_t global_seq_lock;
93 enum ceph_msg_data_type {
94 CEPH_MSG_DATA_NONE, /* message contains no data payload */
95 CEPH_MSG_DATA_PAGES, /* data source/destination is a page array */
96 CEPH_MSG_DATA_PAGELIST, /* data source/destination is a pagelist */
98 CEPH_MSG_DATA_BIO, /* data source/destination is a bio list */
99 #endif /* CONFIG_BLOCK */
100 CEPH_MSG_DATA_BVECS, /* data source/destination is a bio_vec array */
105 struct ceph_bio_iter {
107 struct bvec_iter iter;
110 #define __ceph_bio_iter_advance_step(it, n, STEP) do { \
111 unsigned int __n = (n), __cur_n; \
114 BUG_ON(!(it)->iter.bi_size); \
115 __cur_n = min((it)->iter.bi_size, __n); \
117 bio_advance_iter((it)->bio, &(it)->iter, __cur_n); \
118 if (!(it)->iter.bi_size && (it)->bio->bi_next) { \
119 dout("__ceph_bio_iter_advance_step next bio\n"); \
120 (it)->bio = (it)->bio->bi_next; \
121 (it)->iter = (it)->bio->bi_iter; \
128 * Advance @it by @n bytes.
130 #define ceph_bio_iter_advance(it, n) \
131 __ceph_bio_iter_advance_step(it, n, 0)
134 * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
136 #define ceph_bio_iter_advance_step(it, n, BVEC_STEP) \
137 __ceph_bio_iter_advance_step(it, n, ({ \
139 struct bvec_iter __cur_iter; \
141 __cur_iter = (it)->iter; \
142 __cur_iter.bi_size = __cur_n; \
143 __bio_for_each_segment(bv, (it)->bio, __cur_iter, __cur_iter) \
147 #endif /* CONFIG_BLOCK */
149 struct ceph_bvec_iter {
150 struct bio_vec *bvecs;
151 struct bvec_iter iter;
154 #define __ceph_bvec_iter_advance_step(it, n, STEP) do { \
155 BUG_ON((n) > (it)->iter.bi_size); \
157 bvec_iter_advance((it)->bvecs, &(it)->iter, (n)); \
161 * Advance @it by @n bytes.
163 #define ceph_bvec_iter_advance(it, n) \
164 __ceph_bvec_iter_advance_step(it, n, 0)
167 * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
169 #define ceph_bvec_iter_advance_step(it, n, BVEC_STEP) \
170 __ceph_bvec_iter_advance_step(it, n, ({ \
172 struct bvec_iter __cur_iter; \
174 __cur_iter = (it)->iter; \
175 __cur_iter.bi_size = (n); \
176 for_each_bvec(bv, (it)->bvecs, __cur_iter, __cur_iter) \
180 #define ceph_bvec_iter_shorten(it, n) do { \
181 BUG_ON((n) > (it)->iter.bi_size); \
182 (it)->iter.bi_size = (n); \
185 struct ceph_msg_data {
186 enum ceph_msg_data_type type;
190 struct ceph_bio_iter bio_pos;
193 #endif /* CONFIG_BLOCK */
194 struct ceph_bvec_iter bvec_pos;
197 size_t length; /* total # bytes */
198 unsigned int alignment; /* first page */
201 struct ceph_pagelist *pagelist;
205 struct ceph_msg_data_cursor {
206 size_t total_resid; /* across all data items */
208 struct ceph_msg_data *data; /* current data item */
209 size_t resid; /* bytes not yet consumed */
210 bool last_piece; /* current is last piece */
211 bool need_crc; /* crc update needed */
214 struct ceph_bio_iter bio_iter;
215 #endif /* CONFIG_BLOCK */
216 struct bvec_iter bvec_iter;
218 unsigned int page_offset; /* offset in page */
219 unsigned short page_index; /* index in array */
220 unsigned short page_count; /* pages in array */
222 struct { /* pagelist */
223 struct page *page; /* page from list */
224 size_t offset; /* bytes from list */
230 * a single message. it contains a header (src, dest, message type, etc.),
231 * footer (crc values, mainly), a "front" message body, and possibly a
232 * data payload (stored in some number of pages).
235 struct ceph_msg_header hdr; /* header */
237 struct ceph_msg_footer footer; /* footer */
238 struct ceph_msg_footer_old old_footer; /* old format footer */
240 struct kvec front; /* unaligned blobs of message */
241 struct ceph_buffer *middle;
244 struct ceph_msg_data *data;
247 struct ceph_msg_data_cursor cursor;
249 struct ceph_connection *con;
250 struct list_head list_head; /* links for connection lists */
257 struct ceph_msgpool *pool;
263 #define CEPH_CON_S_CLOSED 1
264 #define CEPH_CON_S_PREOPEN 2
265 #define CEPH_CON_S_V1_BANNER 3
266 #define CEPH_CON_S_V1_CONNECT_MSG 4
267 #define CEPH_CON_S_V2_BANNER_PREFIX 5
268 #define CEPH_CON_S_V2_BANNER_PAYLOAD 6
269 #define CEPH_CON_S_V2_HELLO 7
270 #define CEPH_CON_S_V2_AUTH 8
271 #define CEPH_CON_S_V2_AUTH_SIGNATURE 9
272 #define CEPH_CON_S_V2_SESSION_CONNECT 10
273 #define CEPH_CON_S_V2_SESSION_RECONNECT 11
274 #define CEPH_CON_S_OPEN 12
275 #define CEPH_CON_S_STANDBY 13
278 * ceph_connection flag bits
280 #define CEPH_CON_F_LOSSYTX 0 /* we can close channel or drop
281 messages on errors */
282 #define CEPH_CON_F_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
283 #define CEPH_CON_F_WRITE_PENDING 2 /* we have data ready to send */
284 #define CEPH_CON_F_SOCK_CLOSED 3 /* socket state changed to closed */
285 #define CEPH_CON_F_BACKOFF 4 /* need to retry queuing delayed
288 /* ceph connection fault delay defaults, for exponential backoff */
289 #define BASE_DELAY_INTERVAL (HZ / 4)
290 #define MAX_DELAY_INTERVAL (15 * HZ)
292 struct ceph_connection_v1_info {
293 struct kvec out_kvec[8], /* sending header/footer data */
295 int out_kvec_left; /* kvec's left in out_kvec */
296 int out_skip; /* skip this many bytes */
297 int out_kvec_bytes; /* total bytes left */
298 bool out_more; /* there is more data after the kvecs */
301 struct ceph_auth_handshake *auth;
302 int auth_retry; /* true if we need a newer authorizer */
304 /* connection negotiation temps */
305 u8 in_banner[CEPH_BANNER_MAX_LEN];
306 struct ceph_entity_addr actual_peer_addr;
307 struct ceph_entity_addr peer_addr_for_me;
308 struct ceph_msg_connect out_connect;
309 struct ceph_msg_connect_reply in_reply;
311 int in_base_pos; /* bytes read */
313 /* message in temps */
314 u8 in_tag; /* protocol control byte */
315 struct ceph_msg_header in_hdr;
316 __le64 in_temp_ack; /* for reading an ack */
318 /* message out temps */
319 struct ceph_msg_header out_hdr;
320 __le64 out_temp_ack; /* for writing an ack */
321 struct ceph_timespec out_temp_keepalive2; /* for writing keepalive2
324 u32 connect_seq; /* identify the most recent connection
325 attempt for this session */
326 u32 peer_global_seq; /* peer's global seq for this connection */
329 #define CEPH_CRC_LEN 4
330 #define CEPH_GCM_KEY_LEN 16
331 #define CEPH_GCM_IV_LEN sizeof(struct ceph_gcm_nonce)
332 #define CEPH_GCM_BLOCK_LEN 16
333 #define CEPH_GCM_TAG_LEN 16
335 #define CEPH_PREAMBLE_LEN 32
336 #define CEPH_PREAMBLE_INLINE_LEN 48
337 #define CEPH_PREAMBLE_PLAIN_LEN CEPH_PREAMBLE_LEN
338 #define CEPH_PREAMBLE_SECURE_LEN (CEPH_PREAMBLE_LEN + \
339 CEPH_PREAMBLE_INLINE_LEN + \
341 #define CEPH_EPILOGUE_PLAIN_LEN (1 + 3 * CEPH_CRC_LEN)
342 #define CEPH_EPILOGUE_SECURE_LEN (CEPH_GCM_BLOCK_LEN + CEPH_GCM_TAG_LEN)
344 #define CEPH_FRAME_MAX_SEGMENT_COUNT 4
346 struct ceph_frame_desc {
347 int fd_tag; /* FRAME_TAG_* */
349 int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT]; /* logical */
350 int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT];
353 struct ceph_gcm_nonce {
355 __le64 counter __packed;
358 struct ceph_connection_v2_info {
359 struct iov_iter in_iter;
360 struct kvec in_kvecs[5]; /* recvmsg */
361 struct bio_vec in_bvec; /* recvmsg (in_cursor) */
363 int in_state; /* IN_S_* */
365 struct iov_iter out_iter;
366 struct kvec out_kvecs[8]; /* sendmsg */
367 struct bio_vec out_bvec; /* sendpage (out_cursor, out_zero),
368 sendmsg (out_enc_pages) */
370 int out_state; /* OUT_S_* */
372 int out_zero; /* # of zero bytes to send */
373 bool out_iter_sendpage; /* use sendpage if possible */
375 struct ceph_frame_desc in_desc;
376 struct ceph_msg_data_cursor in_cursor;
377 struct ceph_msg_data_cursor out_cursor;
379 struct crypto_shash *hmac_tfm; /* post-auth signature */
380 struct crypto_aead *gcm_tfm; /* on-wire encryption */
381 struct aead_request *gcm_req;
382 struct crypto_wait gcm_wait;
383 struct ceph_gcm_nonce in_gcm_nonce;
384 struct ceph_gcm_nonce out_gcm_nonce;
386 struct page **out_enc_pages;
387 int out_enc_page_cnt;
391 int con_mode; /* CEPH_CON_MODE_* */
396 struct kvec in_sign_kvecs[8];
397 struct kvec out_sign_kvecs[8];
398 int in_sign_kvec_cnt;
399 int out_sign_kvec_cnt;
407 u8 in_buf[CEPH_PREAMBLE_SECURE_LEN];
408 u8 out_buf[CEPH_PREAMBLE_SECURE_LEN];
410 u8 late_status; /* FRAME_LATE_STATUS_* */
417 u8 pad[CEPH_GCM_BLOCK_LEN - 1];
423 * A single connection with another host.
425 * We maintain a queue of outgoing messages, and some session state to
426 * ensure that we can preserve the lossless, ordered delivery of
427 * messages in the case of a TCP disconnect.
429 struct ceph_connection {
432 const struct ceph_connection_operations *ops;
434 struct ceph_messenger *msgr;
436 int state; /* CEPH_CON_S_* */
440 unsigned long flags; /* CEPH_CON_F_* */
441 const char *error_msg; /* error message, if any */
443 struct ceph_entity_name peer_name; /* peer name */
444 struct ceph_entity_addr peer_addr; /* peer address */
450 struct list_head out_queue;
451 struct list_head out_sent; /* sending or sent but unacked */
452 u64 out_seq; /* last message queued for send */
454 u64 in_seq, in_seq_acked; /* last message received, acked */
456 struct ceph_msg *in_msg;
457 struct ceph_msg *out_msg; /* sending message (== tail of
460 u32 in_front_crc, in_middle_crc, in_data_crc; /* calculated crc */
462 struct timespec64 last_keepalive_ack; /* keepalive2 ack stamp */
464 struct delayed_work work; /* send|recv work */
465 unsigned long delay; /* current delay interval */
468 struct ceph_connection_v1_info v1;
469 struct ceph_connection_v2_info v2;
473 extern struct page *ceph_zero_page;
475 void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag);
476 void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag);
477 bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag);
478 bool ceph_con_flag_test_and_clear(struct ceph_connection *con,
479 unsigned long con_flag);
480 bool ceph_con_flag_test_and_set(struct ceph_connection *con,
481 unsigned long con_flag);
483 void ceph_encode_my_addr(struct ceph_messenger *msgr);
485 int ceph_tcp_connect(struct ceph_connection *con);
486 int ceph_con_close_socket(struct ceph_connection *con);
487 void ceph_con_reset_session(struct ceph_connection *con);
489 u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt);
490 void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq);
491 void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq);
493 void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
494 struct ceph_msg *msg, size_t length);
495 struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
496 size_t *page_offset, size_t *length,
498 void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes);
500 u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset,
501 unsigned int length);
503 bool ceph_addr_is_blank(const struct ceph_entity_addr *addr);
504 int ceph_addr_port(const struct ceph_entity_addr *addr);
505 void ceph_addr_set_port(struct ceph_entity_addr *addr, int p);
507 void ceph_con_process_message(struct ceph_connection *con);
508 int ceph_con_in_msg_alloc(struct ceph_connection *con,
509 struct ceph_msg_header *hdr, int *skip);
510 void ceph_con_get_out_msg(struct ceph_connection *con);
513 int ceph_con_v1_try_read(struct ceph_connection *con);
514 int ceph_con_v1_try_write(struct ceph_connection *con);
515 void ceph_con_v1_revoke(struct ceph_connection *con);
516 void ceph_con_v1_revoke_incoming(struct ceph_connection *con);
517 bool ceph_con_v1_opened(struct ceph_connection *con);
518 void ceph_con_v1_reset_session(struct ceph_connection *con);
519 void ceph_con_v1_reset_protocol(struct ceph_connection *con);
522 int ceph_con_v2_try_read(struct ceph_connection *con);
523 int ceph_con_v2_try_write(struct ceph_connection *con);
524 void ceph_con_v2_revoke(struct ceph_connection *con);
525 void ceph_con_v2_revoke_incoming(struct ceph_connection *con);
526 bool ceph_con_v2_opened(struct ceph_connection *con);
527 void ceph_con_v2_reset_session(struct ceph_connection *con);
528 void ceph_con_v2_reset_protocol(struct ceph_connection *con);
531 extern const char *ceph_pr_addr(const struct ceph_entity_addr *addr);
533 extern int ceph_parse_ips(const char *c, const char *end,
534 struct ceph_entity_addr *addr,
535 int max_count, int *count);
537 extern int ceph_msgr_init(void);
538 extern void ceph_msgr_exit(void);
539 extern void ceph_msgr_flush(void);
541 extern void ceph_messenger_init(struct ceph_messenger *msgr,
542 struct ceph_entity_addr *myaddr);
543 extern void ceph_messenger_fini(struct ceph_messenger *msgr);
544 extern void ceph_messenger_reset_nonce(struct ceph_messenger *msgr);
546 extern void ceph_con_init(struct ceph_connection *con, void *private,
547 const struct ceph_connection_operations *ops,
548 struct ceph_messenger *msgr);
549 extern void ceph_con_open(struct ceph_connection *con,
550 __u8 entity_type, __u64 entity_num,
551 struct ceph_entity_addr *addr);
552 extern bool ceph_con_opened(struct ceph_connection *con);
553 extern void ceph_con_close(struct ceph_connection *con);
554 extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
556 extern void ceph_msg_revoke(struct ceph_msg *msg);
557 extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
559 extern void ceph_con_keepalive(struct ceph_connection *con);
560 extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
561 unsigned long interval);
563 void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
564 size_t length, size_t alignment, bool own_pages);
565 extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
566 struct ceph_pagelist *pagelist);
568 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
570 #endif /* CONFIG_BLOCK */
571 void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
572 struct ceph_bvec_iter *bvec_pos);
574 struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
575 gfp_t flags, bool can_fail);
576 extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
579 extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
580 extern void ceph_msg_put(struct ceph_msg *msg);
582 extern void ceph_msg_dump(struct ceph_msg *msg);