xref: /linux-6.15/include/linux/ceph/messenger.h (revision 8e46a2d0)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
23d14c5d2SYehuda Sadeh #ifndef __FS_CEPH_MESSENGER_H
33d14c5d2SYehuda Sadeh #define __FS_CEPH_MESSENGER_H
43d14c5d2SYehuda Sadeh 
59f082171SChristoph Hellwig #include <linux/bvec.h>
6cd1a677cSIlya Dryomov #include <linux/crypto.h>
73d14c5d2SYehuda Sadeh #include <linux/kref.h>
83d14c5d2SYehuda Sadeh #include <linux/mutex.h>
93d14c5d2SYehuda Sadeh #include <linux/net.h>
103d14c5d2SYehuda Sadeh #include <linux/radix-tree.h>
113d14c5d2SYehuda Sadeh #include <linux/uio.h>
123d14c5d2SYehuda Sadeh #include <linux/workqueue.h>
13757856d2SIlya Dryomov #include <net/net_namespace.h>
143d14c5d2SYehuda Sadeh 
15a1ce3928SDavid Howells #include <linux/ceph/types.h>
16a1ce3928SDavid Howells #include <linux/ceph/buffer.h>
173d14c5d2SYehuda Sadeh 
183d14c5d2SYehuda Sadeh struct ceph_msg;
193d14c5d2SYehuda Sadeh struct ceph_connection;
20ec3bc567SJeff Layton struct ceph_msg_data_cursor;
213d14c5d2SYehuda Sadeh 
223d14c5d2SYehuda Sadeh /*
233d14c5d2SYehuda Sadeh  * Ceph defines these callbacks for handling connection events.
243d14c5d2SYehuda Sadeh  */
253d14c5d2SYehuda Sadeh struct ceph_connection_operations {
263d14c5d2SYehuda Sadeh 	struct ceph_connection *(*get)(struct ceph_connection *);
273d14c5d2SYehuda Sadeh 	void (*put)(struct ceph_connection *);
283d14c5d2SYehuda Sadeh 
293d14c5d2SYehuda Sadeh 	/* handle an incoming message. */
303d14c5d2SYehuda Sadeh 	void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
313d14c5d2SYehuda Sadeh 
323d14c5d2SYehuda Sadeh 	/* authorize an outgoing connection */
33a3530df3SAlex Elder 	struct ceph_auth_handshake *(*get_authorizer) (
34a3530df3SAlex Elder 				struct ceph_connection *con,
358f43fb53SAlex Elder 			       int *proto, int force_new);
366daca13dSIlya Dryomov 	int (*add_authorizer_challenge)(struct ceph_connection *con,
376daca13dSIlya Dryomov 					void *challenge_buf,
386daca13dSIlya Dryomov 					int challenge_buf_len);
390dde5848SIlya Dryomov 	int (*verify_authorizer_reply) (struct ceph_connection *con);
403d14c5d2SYehuda Sadeh 	int (*invalidate_authorizer)(struct ceph_connection *con);
413d14c5d2SYehuda Sadeh 
423d14c5d2SYehuda Sadeh 	/* there was some error on the socket (disconnect, whatever) */
433d14c5d2SYehuda Sadeh 	void (*fault) (struct ceph_connection *con);
443d14c5d2SYehuda Sadeh 
453d14c5d2SYehuda Sadeh 	/* a remote host as terminated a message exchange session, and messages
463d14c5d2SYehuda Sadeh 	 * we sent (or they tried to send us) may be lost. */
473d14c5d2SYehuda Sadeh 	void (*peer_reset) (struct ceph_connection *con);
483d14c5d2SYehuda Sadeh 
493d14c5d2SYehuda Sadeh 	struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
503d14c5d2SYehuda Sadeh 					struct ceph_msg_header *hdr,
513d14c5d2SYehuda Sadeh 					int *skip);
5233d07337SYan, Zheng 
5398ad5ebdSIlya Dryomov 	void (*reencode_message) (struct ceph_msg *msg);
5498ad5ebdSIlya Dryomov 
5579dbd1baSIlya Dryomov 	int (*sign_message) (struct ceph_msg *msg);
5679dbd1baSIlya Dryomov 	int (*check_message_signature) (struct ceph_msg *msg);
57cd1a677cSIlya Dryomov 
58cd1a677cSIlya Dryomov 	/* msgr2 authentication exchange */
59cd1a677cSIlya Dryomov 	int (*get_auth_request)(struct ceph_connection *con,
60cd1a677cSIlya Dryomov 				void *buf, int *buf_len,
61cd1a677cSIlya Dryomov 				void **authorizer, int *authorizer_len);
62cd1a677cSIlya Dryomov 	int (*handle_auth_reply_more)(struct ceph_connection *con,
63cd1a677cSIlya Dryomov 				      void *reply, int reply_len,
64cd1a677cSIlya Dryomov 				      void *buf, int *buf_len,
65cd1a677cSIlya Dryomov 				      void **authorizer, int *authorizer_len);
66cd1a677cSIlya Dryomov 	int (*handle_auth_done)(struct ceph_connection *con,
67cd1a677cSIlya Dryomov 				u64 global_id, void *reply, int reply_len,
68cd1a677cSIlya Dryomov 				u8 *session_key, int *session_key_len,
69cd1a677cSIlya Dryomov 				u8 *con_secret, int *con_secret_len);
70cd1a677cSIlya Dryomov 	int (*handle_auth_bad_method)(struct ceph_connection *con,
71cd1a677cSIlya Dryomov 				      int used_proto, int result,
72cd1a677cSIlya Dryomov 				      const int *allowed_protos, int proto_cnt,
73cd1a677cSIlya Dryomov 				      const int *allowed_modes, int mode_cnt);
74ec3bc567SJeff Layton 
75ec3bc567SJeff Layton 	/**
76ec3bc567SJeff Layton 	 * sparse_read: read sparse data
77ec3bc567SJeff Layton 	 * @con: connection we're reading from
78ec3bc567SJeff Layton 	 * @cursor: data cursor for reading extents
79ec3bc567SJeff Layton 	 * @buf: optional buffer to read into
80ec3bc567SJeff Layton 	 *
81ec3bc567SJeff Layton 	 * This should be called more than once, each time setting up to
82ec3bc567SJeff Layton 	 * receive an extent into the current cursor position, and zeroing
83ec3bc567SJeff Layton 	 * the holes between them.
84ec3bc567SJeff Layton 	 *
85ec3bc567SJeff Layton 	 * Returns amount of data to be read (in bytes), 0 if reading is
86ec3bc567SJeff Layton 	 * complete, or -errno if there was an error.
87ec3bc567SJeff Layton 	 *
88ec3bc567SJeff Layton 	 * If @buf is set on a >0 return, then the data should be read into
89ec3bc567SJeff Layton 	 * the provided buffer. Otherwise, it should be read into the cursor.
90ec3bc567SJeff Layton 	 *
91ec3bc567SJeff Layton 	 * The sparse read operation is expected to initialize the cursor
92ec3bc567SJeff Layton 	 * with a length covering up to the end of the last extent.
93ec3bc567SJeff Layton 	 */
94ec3bc567SJeff Layton 	int (*sparse_read)(struct ceph_connection *con,
95ec3bc567SJeff Layton 			   struct ceph_msg_data_cursor *cursor,
96ec3bc567SJeff Layton 			   char **buf);
97ec3bc567SJeff Layton 
983d14c5d2SYehuda Sadeh };
993d14c5d2SYehuda Sadeh 
100b07720d0SIlya Dryomov /* use format string %s%lld */
1013d14c5d2SYehuda Sadeh #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
1023d14c5d2SYehuda Sadeh 
1033d14c5d2SYehuda Sadeh struct ceph_messenger {
1043d14c5d2SYehuda Sadeh 	struct ceph_entity_inst inst;    /* my name+address */
1053d14c5d2SYehuda Sadeh 	struct ceph_entity_addr my_enc_addr;
1063d14c5d2SYehuda Sadeh 
107a2a32584SGuanjun He 	atomic_t stopping;
108757856d2SIlya Dryomov 	possible_net_t net;
1093d14c5d2SYehuda Sadeh 
1103d14c5d2SYehuda Sadeh 	/*
1113d14c5d2SYehuda Sadeh 	 * the global_seq counts connections i (attempt to) initiate
1123d14c5d2SYehuda Sadeh 	 * in order to disambiguate certain connect race conditions.
1133d14c5d2SYehuda Sadeh 	 */
1143d14c5d2SYehuda Sadeh 	u32 global_seq;
1153d14c5d2SYehuda Sadeh 	spinlock_t global_seq_lock;
1163d14c5d2SYehuda Sadeh };
1173d14c5d2SYehuda Sadeh 
11843794509SAlex Elder enum ceph_msg_data_type {
11943794509SAlex Elder 	CEPH_MSG_DATA_NONE,	/* message contains no data payload */
12043794509SAlex Elder 	CEPH_MSG_DATA_PAGES,	/* data source/destination is a page array */
12143794509SAlex Elder 	CEPH_MSG_DATA_PAGELIST,	/* data source/destination is a pagelist */
12243794509SAlex Elder #ifdef CONFIG_BLOCK
12343794509SAlex Elder 	CEPH_MSG_DATA_BIO,	/* data source/destination is a bio list */
12443794509SAlex Elder #endif /* CONFIG_BLOCK */
125b9e281c2SIlya Dryomov 	CEPH_MSG_DATA_BVECS,	/* data source/destination is a bio_vec array */
126dee0c5f8SJeff Layton 	CEPH_MSG_DATA_ITER,	/* data source/destination is an iov_iter */
12743794509SAlex Elder };
12843794509SAlex Elder 
1295359a17dSIlya Dryomov #ifdef CONFIG_BLOCK
1305359a17dSIlya Dryomov 
1315359a17dSIlya Dryomov struct ceph_bio_iter {
1325359a17dSIlya Dryomov 	struct bio *bio;
1335359a17dSIlya Dryomov 	struct bvec_iter iter;
1345359a17dSIlya Dryomov };
1355359a17dSIlya Dryomov 
1365359a17dSIlya Dryomov #define __ceph_bio_iter_advance_step(it, n, STEP) do {			      \
1375359a17dSIlya Dryomov 	unsigned int __n = (n), __cur_n;				      \
1385359a17dSIlya Dryomov 									      \
1395359a17dSIlya Dryomov 	while (__n) {							      \
1405359a17dSIlya Dryomov 		BUG_ON(!(it)->iter.bi_size);				      \
1415359a17dSIlya Dryomov 		__cur_n = min((it)->iter.bi_size, __n);			      \
1425359a17dSIlya Dryomov 		(void)(STEP);						      \
1435359a17dSIlya Dryomov 		bio_advance_iter((it)->bio, &(it)->iter, __cur_n);	      \
1445359a17dSIlya Dryomov 		if (!(it)->iter.bi_size && (it)->bio->bi_next) {	      \
1455359a17dSIlya Dryomov 			dout("__ceph_bio_iter_advance_step next bio\n");      \
1465359a17dSIlya Dryomov 			(it)->bio = (it)->bio->bi_next;			      \
1475359a17dSIlya Dryomov 			(it)->iter = (it)->bio->bi_iter;		      \
1485359a17dSIlya Dryomov 		}							      \
1495359a17dSIlya Dryomov 		__n -= __cur_n;						      \
1505359a17dSIlya Dryomov 	}								      \
1515359a17dSIlya Dryomov } while (0)
1525359a17dSIlya Dryomov 
1535359a17dSIlya Dryomov /*
1545359a17dSIlya Dryomov  * Advance @it by @n bytes.
1555359a17dSIlya Dryomov  */
1565359a17dSIlya Dryomov #define ceph_bio_iter_advance(it, n)					      \
1575359a17dSIlya Dryomov 	__ceph_bio_iter_advance_step(it, n, 0)
1585359a17dSIlya Dryomov 
1595359a17dSIlya Dryomov /*
1605359a17dSIlya Dryomov  * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
1615359a17dSIlya Dryomov  */
1625359a17dSIlya Dryomov #define ceph_bio_iter_advance_step(it, n, BVEC_STEP)			      \
1635359a17dSIlya Dryomov 	__ceph_bio_iter_advance_step(it, n, ({				      \
1645359a17dSIlya Dryomov 		struct bio_vec bv;					      \
1655359a17dSIlya Dryomov 		struct bvec_iter __cur_iter;				      \
1665359a17dSIlya Dryomov 									      \
1675359a17dSIlya Dryomov 		__cur_iter = (it)->iter;				      \
1685359a17dSIlya Dryomov 		__cur_iter.bi_size = __cur_n;				      \
1695359a17dSIlya Dryomov 		__bio_for_each_segment(bv, (it)->bio, __cur_iter, __cur_iter) \
1705359a17dSIlya Dryomov 			(void)(BVEC_STEP);				      \
1715359a17dSIlya Dryomov 	}))
1725359a17dSIlya Dryomov 
1735359a17dSIlya Dryomov #endif /* CONFIG_BLOCK */
1745359a17dSIlya Dryomov 
175b9e281c2SIlya Dryomov struct ceph_bvec_iter {
176b9e281c2SIlya Dryomov 	struct bio_vec *bvecs;
177b9e281c2SIlya Dryomov 	struct bvec_iter iter;
178b9e281c2SIlya Dryomov };
179b9e281c2SIlya Dryomov 
180b9e281c2SIlya Dryomov #define __ceph_bvec_iter_advance_step(it, n, STEP) do {			      \
181b9e281c2SIlya Dryomov 	BUG_ON((n) > (it)->iter.bi_size);				      \
182b9e281c2SIlya Dryomov 	(void)(STEP);							      \
183b9e281c2SIlya Dryomov 	bvec_iter_advance((it)->bvecs, &(it)->iter, (n));		      \
184b9e281c2SIlya Dryomov } while (0)
185b9e281c2SIlya Dryomov 
186b9e281c2SIlya Dryomov /*
187b9e281c2SIlya Dryomov  * Advance @it by @n bytes.
188b9e281c2SIlya Dryomov  */
189b9e281c2SIlya Dryomov #define ceph_bvec_iter_advance(it, n)					      \
190b9e281c2SIlya Dryomov 	__ceph_bvec_iter_advance_step(it, n, 0)
191b9e281c2SIlya Dryomov 
192b9e281c2SIlya Dryomov /*
193b9e281c2SIlya Dryomov  * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
194b9e281c2SIlya Dryomov  */
195b9e281c2SIlya Dryomov #define ceph_bvec_iter_advance_step(it, n, BVEC_STEP)			      \
196b9e281c2SIlya Dryomov 	__ceph_bvec_iter_advance_step(it, n, ({				      \
197b9e281c2SIlya Dryomov 		struct bio_vec bv;					      \
198b9e281c2SIlya Dryomov 		struct bvec_iter __cur_iter;				      \
199b9e281c2SIlya Dryomov 									      \
200b9e281c2SIlya Dryomov 		__cur_iter = (it)->iter;				      \
201b9e281c2SIlya Dryomov 		__cur_iter.bi_size = (n);				      \
202b9e281c2SIlya Dryomov 		for_each_bvec(bv, (it)->bvecs, __cur_iter, __cur_iter)	      \
203b9e281c2SIlya Dryomov 			(void)(BVEC_STEP);				      \
204b9e281c2SIlya Dryomov 	}))
205b9e281c2SIlya Dryomov 
206b9e281c2SIlya Dryomov #define ceph_bvec_iter_shorten(it, n) do {				      \
207b9e281c2SIlya Dryomov 	BUG_ON((n) > (it)->iter.bi_size);				      \
208b9e281c2SIlya Dryomov 	(it)->iter.bi_size = (n);					      \
209b9e281c2SIlya Dryomov } while (0)
210b9e281c2SIlya Dryomov 
21136153ec9SAlex Elder struct ceph_msg_data {
21236153ec9SAlex Elder 	enum ceph_msg_data_type		type;
21336153ec9SAlex Elder 	union {
21436153ec9SAlex Elder #ifdef CONFIG_BLOCK
21536153ec9SAlex Elder 		struct {
2165359a17dSIlya Dryomov 			struct ceph_bio_iter	bio_pos;
2175359a17dSIlya Dryomov 			u32			bio_length;
21836153ec9SAlex Elder 		};
21936153ec9SAlex Elder #endif /* CONFIG_BLOCK */
220b9e281c2SIlya Dryomov 		struct ceph_bvec_iter	bvec_pos;
22136153ec9SAlex Elder 		struct {
222e8862740SIlya Dryomov 			struct page	**pages;
22336153ec9SAlex Elder 			size_t		length;		/* total # bytes */
22436153ec9SAlex Elder 			unsigned int	alignment;	/* first page */
225e8862740SIlya Dryomov 			bool		own_pages;
22636153ec9SAlex Elder 		};
22736153ec9SAlex Elder 		struct ceph_pagelist	*pagelist;
228dee0c5f8SJeff Layton 		struct iov_iter		iter;
22936153ec9SAlex Elder 	};
23036153ec9SAlex Elder };
23136153ec9SAlex Elder 
232fe38a2b6SAlex Elder struct ceph_msg_data_cursor {
233ca8b3a69SAlex Elder 	size_t			total_resid;	/* across all data items */
234ca8b3a69SAlex Elder 
235ca8b3a69SAlex Elder 	struct ceph_msg_data	*data;		/* current data item */
23625aff7c5SAlex Elder 	size_t			resid;		/* bytes not yet consumed */
237ec3bc567SJeff Layton 	int			sr_resid;	/* residual sparse_read len */
2388ae4f4f5SAlex Elder 	bool			need_crc;	/* crc update needed */
239dd236fcbSAlex Elder 	union {
2406aaa4511SAlex Elder #ifdef CONFIG_BLOCK
2415359a17dSIlya Dryomov 		struct ceph_bio_iter	bio_iter;
2426aaa4511SAlex Elder #endif /* CONFIG_BLOCK */
243b9e281c2SIlya Dryomov 		struct bvec_iter	bvec_iter;
244e766d7b5SAlex Elder 		struct {				/* pages */
245e766d7b5SAlex Elder 			unsigned int	page_offset;	/* offset in page */
246e766d7b5SAlex Elder 			unsigned short	page_index;	/* index in array */
247e766d7b5SAlex Elder 			unsigned short	page_count;	/* pages in array */
248e766d7b5SAlex Elder 		};
249dd236fcbSAlex Elder 		struct {				/* pagelist */
250dd236fcbSAlex Elder 			struct page	*page;		/* page from list */
251dd236fcbSAlex Elder 			size_t		offset;		/* bytes from list */
252dd236fcbSAlex Elder 		};
253dee0c5f8SJeff Layton 		struct {
254dee0c5f8SJeff Layton 			struct iov_iter		iov_iter;
255dee0c5f8SJeff Layton 			unsigned int		lastlen;
256dee0c5f8SJeff Layton 		};
257dd236fcbSAlex Elder 	};
258fe38a2b6SAlex Elder };
259fe38a2b6SAlex Elder 
2603d14c5d2SYehuda Sadeh /*
2613d14c5d2SYehuda Sadeh  * a single message.  it contains a header (src, dest, message type, etc.),
2623d14c5d2SYehuda Sadeh  * footer (crc values, mainly), a "front" message body, and possibly a
2633d14c5d2SYehuda Sadeh  * data payload (stored in some number of pages).
2643d14c5d2SYehuda Sadeh  */
2653d14c5d2SYehuda Sadeh struct ceph_msg {
2663d14c5d2SYehuda Sadeh 	struct ceph_msg_header hdr;	/* header */
26733d07337SYan, Zheng 	union {
2683d14c5d2SYehuda Sadeh 		struct ceph_msg_footer footer;		/* footer */
26933d07337SYan, Zheng 		struct ceph_msg_footer_old old_footer;	/* old format footer */
27033d07337SYan, Zheng 	};
2713d14c5d2SYehuda Sadeh 	struct kvec front;              /* unaligned blobs of message */
2723d14c5d2SYehuda Sadeh 	struct ceph_buffer *middle;
27302afca6cSAlex Elder 
274a1930804SAlex Elder 	size_t				data_length;
2750d9c1ab3SIlya Dryomov 	struct ceph_msg_data		*data;
2760d9c1ab3SIlya Dryomov 	int				num_data_items;
2770d9c1ab3SIlya Dryomov 	int				max_data_items;
27836153ec9SAlex Elder 	struct ceph_msg_data_cursor	cursor;
27902afca6cSAlex Elder 
28002afca6cSAlex Elder 	struct ceph_connection *con;
28102afca6cSAlex Elder 	struct list_head list_head;	/* links for connection lists */
28202afca6cSAlex Elder 
28302afca6cSAlex Elder 	struct kref kref;
2843d14c5d2SYehuda Sadeh 	bool more_to_follow;
2853d14c5d2SYehuda Sadeh 	bool needs_out_seq;
286*8e46a2d0SXiubo Li 	u64 sparse_read_total;
2873cea4c30SIlya Dryomov 	int front_alloc_len;
2883d14c5d2SYehuda Sadeh 
2893d14c5d2SYehuda Sadeh 	struct ceph_msgpool *pool;
2903d14c5d2SYehuda Sadeh };
2913d14c5d2SYehuda Sadeh 
2926d7f62bfSIlya Dryomov /*
2936d7f62bfSIlya Dryomov  * connection states
2946d7f62bfSIlya Dryomov  */
2956d7f62bfSIlya Dryomov #define CEPH_CON_S_CLOSED		1
2966d7f62bfSIlya Dryomov #define CEPH_CON_S_PREOPEN		2
2976d7f62bfSIlya Dryomov #define CEPH_CON_S_V1_BANNER		3
2986d7f62bfSIlya Dryomov #define CEPH_CON_S_V1_CONNECT_MSG	4
299cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_BANNER_PREFIX	5
300cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_BANNER_PAYLOAD	6
301cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_HELLO		7
302cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_AUTH		8
303cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_AUTH_SIGNATURE	9
304cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_SESSION_CONNECT	10
305cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_SESSION_RECONNECT	11
306cd1a677cSIlya Dryomov #define CEPH_CON_S_OPEN			12
307cd1a677cSIlya Dryomov #define CEPH_CON_S_STANDBY		13
3086d7f62bfSIlya Dryomov 
3093fefd43eSIlya Dryomov /*
3103fefd43eSIlya Dryomov  * ceph_connection flag bits
3113fefd43eSIlya Dryomov  */
3123fefd43eSIlya Dryomov #define CEPH_CON_F_LOSSYTX		0  /* we can close channel or drop
3133fefd43eSIlya Dryomov 					      messages on errors */
3143fefd43eSIlya Dryomov #define CEPH_CON_F_KEEPALIVE_PENDING	1  /* we need to send a keepalive */
3153fefd43eSIlya Dryomov #define CEPH_CON_F_WRITE_PENDING	2  /* we have data ready to send */
3163fefd43eSIlya Dryomov #define CEPH_CON_F_SOCK_CLOSED		3  /* socket state changed to closed */
3173fefd43eSIlya Dryomov #define CEPH_CON_F_BACKOFF		4  /* need to retry queuing delayed
3183fefd43eSIlya Dryomov 					      work */
3193fefd43eSIlya Dryomov 
3203d14c5d2SYehuda Sadeh /* ceph connection fault delay defaults, for exponential backoff */
321418af5b3SIlya Dryomov #define BASE_DELAY_INTERVAL	(HZ / 4)
322418af5b3SIlya Dryomov #define MAX_DELAY_INTERVAL	(15 * HZ)
3233d14c5d2SYehuda Sadeh 
324a56dd9bfSIlya Dryomov struct ceph_connection_v1_info {
325a56dd9bfSIlya Dryomov 	struct kvec out_kvec[8],         /* sending header/footer data */
326a56dd9bfSIlya Dryomov 		*out_kvec_cur;
327a56dd9bfSIlya Dryomov 	int out_kvec_left;   /* kvec's left in out_kvec */
328a56dd9bfSIlya Dryomov 	int out_skip;        /* skip this many bytes */
329a56dd9bfSIlya Dryomov 	int out_kvec_bytes;  /* total bytes left */
330a56dd9bfSIlya Dryomov 	bool out_more;       /* there is more data after the kvecs */
331a56dd9bfSIlya Dryomov 	bool out_msg_done;
332a56dd9bfSIlya Dryomov 
333a56dd9bfSIlya Dryomov 	struct ceph_auth_handshake *auth;
334a56dd9bfSIlya Dryomov 	int auth_retry;       /* true if we need a newer authorizer */
335a56dd9bfSIlya Dryomov 
336a56dd9bfSIlya Dryomov 	/* connection negotiation temps */
337a56dd9bfSIlya Dryomov 	u8 in_banner[CEPH_BANNER_MAX_LEN];
338a56dd9bfSIlya Dryomov 	struct ceph_entity_addr actual_peer_addr;
339a56dd9bfSIlya Dryomov 	struct ceph_entity_addr peer_addr_for_me;
340a56dd9bfSIlya Dryomov 	struct ceph_msg_connect out_connect;
341a56dd9bfSIlya Dryomov 	struct ceph_msg_connect_reply in_reply;
342a56dd9bfSIlya Dryomov 
343a56dd9bfSIlya Dryomov 	int in_base_pos;     /* bytes read */
344a56dd9bfSIlya Dryomov 
345d396f89dSJeff Layton 	/* sparse reads */
346d396f89dSJeff Layton 	struct kvec in_sr_kvec; /* current location to receive into */
347d396f89dSJeff Layton 	u64 in_sr_len;		/* amount of data in this extent */
348d396f89dSJeff Layton 
349a56dd9bfSIlya Dryomov 	/* message in temps */
350a56dd9bfSIlya Dryomov 	u8 in_tag;           /* protocol control byte */
351a56dd9bfSIlya Dryomov 	struct ceph_msg_header in_hdr;
352a56dd9bfSIlya Dryomov 	__le64 in_temp_ack;  /* for reading an ack */
353a56dd9bfSIlya Dryomov 
354a56dd9bfSIlya Dryomov 	/* message out temps */
355a56dd9bfSIlya Dryomov 	struct ceph_msg_header out_hdr;
356a56dd9bfSIlya Dryomov 	__le64 out_temp_ack;  /* for writing an ack */
357a56dd9bfSIlya Dryomov 	struct ceph_timespec out_temp_keepalive2;  /* for writing keepalive2
358a56dd9bfSIlya Dryomov 						      stamp */
359a56dd9bfSIlya Dryomov 
360a56dd9bfSIlya Dryomov 	u32 connect_seq;      /* identify the most recent connection
361a56dd9bfSIlya Dryomov 				 attempt for this session */
362a56dd9bfSIlya Dryomov 	u32 peer_global_seq;  /* peer's global seq for this connection */
363a56dd9bfSIlya Dryomov };
364a56dd9bfSIlya Dryomov 
365cd1a677cSIlya Dryomov #define CEPH_CRC_LEN			4
366cd1a677cSIlya Dryomov #define CEPH_GCM_KEY_LEN		16
367cd1a677cSIlya Dryomov #define CEPH_GCM_IV_LEN			sizeof(struct ceph_gcm_nonce)
368cd1a677cSIlya Dryomov #define CEPH_GCM_BLOCK_LEN		16
369cd1a677cSIlya Dryomov #define CEPH_GCM_TAG_LEN		16
370cd1a677cSIlya Dryomov 
371cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_LEN		32
372cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_INLINE_LEN	48
373cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_PLAIN_LEN		CEPH_PREAMBLE_LEN
374cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_SECURE_LEN	(CEPH_PREAMBLE_LEN +		\
375cd1a677cSIlya Dryomov 					 CEPH_PREAMBLE_INLINE_LEN +	\
376cd1a677cSIlya Dryomov 					 CEPH_GCM_TAG_LEN)
377cd1a677cSIlya Dryomov #define CEPH_EPILOGUE_PLAIN_LEN		(1 + 3 * CEPH_CRC_LEN)
378cd1a677cSIlya Dryomov #define CEPH_EPILOGUE_SECURE_LEN	(CEPH_GCM_BLOCK_LEN + CEPH_GCM_TAG_LEN)
379cd1a677cSIlya Dryomov 
380cd1a677cSIlya Dryomov #define CEPH_FRAME_MAX_SEGMENT_COUNT	4
381cd1a677cSIlya Dryomov 
382cd1a677cSIlya Dryomov struct ceph_frame_desc {
383cd1a677cSIlya Dryomov 	int fd_tag;  /* FRAME_TAG_* */
384cd1a677cSIlya Dryomov 	int fd_seg_cnt;
385cd1a677cSIlya Dryomov 	int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT];  /* logical */
386cd1a677cSIlya Dryomov 	int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT];
387cd1a677cSIlya Dryomov };
388cd1a677cSIlya Dryomov 
389cd1a677cSIlya Dryomov struct ceph_gcm_nonce {
390cd1a677cSIlya Dryomov 	__le32 fixed;
391cd1a677cSIlya Dryomov 	__le64 counter __packed;
392cd1a677cSIlya Dryomov };
393cd1a677cSIlya Dryomov 
394cd1a677cSIlya Dryomov struct ceph_connection_v2_info {
395cd1a677cSIlya Dryomov 	struct iov_iter in_iter;
396cd1a677cSIlya Dryomov 	struct kvec in_kvecs[5];  /* recvmsg */
397cd1a677cSIlya Dryomov 	struct bio_vec in_bvec;  /* recvmsg (in_cursor) */
398cd1a677cSIlya Dryomov 	int in_kvec_cnt;
399cd1a677cSIlya Dryomov 	int in_state;  /* IN_S_* */
400cd1a677cSIlya Dryomov 
401cd1a677cSIlya Dryomov 	struct iov_iter out_iter;
402cd1a677cSIlya Dryomov 	struct kvec out_kvecs[8];  /* sendmsg */
403cd1a677cSIlya Dryomov 	struct bio_vec out_bvec;  /* sendpage (out_cursor, out_zero),
404cd1a677cSIlya Dryomov 				     sendmsg (out_enc_pages) */
405cd1a677cSIlya Dryomov 	int out_kvec_cnt;
406cd1a677cSIlya Dryomov 	int out_state;  /* OUT_S_* */
407cd1a677cSIlya Dryomov 
408cd1a677cSIlya Dryomov 	int out_zero;  /* # of zero bytes to send */
409cd1a677cSIlya Dryomov 	bool out_iter_sendpage;  /* use sendpage if possible */
410cd1a677cSIlya Dryomov 
411cd1a677cSIlya Dryomov 	struct ceph_frame_desc in_desc;
412cd1a677cSIlya Dryomov 	struct ceph_msg_data_cursor in_cursor;
413cd1a677cSIlya Dryomov 	struct ceph_msg_data_cursor out_cursor;
414cd1a677cSIlya Dryomov 
415cd1a677cSIlya Dryomov 	struct crypto_shash *hmac_tfm;  /* post-auth signature */
416cd1a677cSIlya Dryomov 	struct crypto_aead *gcm_tfm;  /* on-wire encryption */
417cd1a677cSIlya Dryomov 	struct aead_request *gcm_req;
418cd1a677cSIlya Dryomov 	struct crypto_wait gcm_wait;
419cd1a677cSIlya Dryomov 	struct ceph_gcm_nonce in_gcm_nonce;
420cd1a677cSIlya Dryomov 	struct ceph_gcm_nonce out_gcm_nonce;
421cd1a677cSIlya Dryomov 
4222ea88716SIlya Dryomov 	struct page **in_enc_pages;
4232ea88716SIlya Dryomov 	int in_enc_page_cnt;
4242ea88716SIlya Dryomov 	int in_enc_resid;
4252ea88716SIlya Dryomov 	int in_enc_i;
426cd1a677cSIlya Dryomov 	struct page **out_enc_pages;
427cd1a677cSIlya Dryomov 	int out_enc_page_cnt;
428cd1a677cSIlya Dryomov 	int out_enc_resid;
429cd1a677cSIlya Dryomov 	int out_enc_i;
430cd1a677cSIlya Dryomov 
431cd1a677cSIlya Dryomov 	int con_mode;  /* CEPH_CON_MODE_* */
432cd1a677cSIlya Dryomov 
433cd1a677cSIlya Dryomov 	void *conn_bufs[16];
434cd1a677cSIlya Dryomov 	int conn_buf_cnt;
435ec3bc567SJeff Layton 	int data_len_remain;
436cd1a677cSIlya Dryomov 
437cd1a677cSIlya Dryomov 	struct kvec in_sign_kvecs[8];
438cd1a677cSIlya Dryomov 	struct kvec out_sign_kvecs[8];
439cd1a677cSIlya Dryomov 	int in_sign_kvec_cnt;
440cd1a677cSIlya Dryomov 	int out_sign_kvec_cnt;
441cd1a677cSIlya Dryomov 
442cd1a677cSIlya Dryomov 	u64 client_cookie;
443cd1a677cSIlya Dryomov 	u64 server_cookie;
444cd1a677cSIlya Dryomov 	u64 global_seq;
445cd1a677cSIlya Dryomov 	u64 connect_seq;
446cd1a677cSIlya Dryomov 	u64 peer_global_seq;
447cd1a677cSIlya Dryomov 
448cd1a677cSIlya Dryomov 	u8 in_buf[CEPH_PREAMBLE_SECURE_LEN];
449cd1a677cSIlya Dryomov 	u8 out_buf[CEPH_PREAMBLE_SECURE_LEN];
450cd1a677cSIlya Dryomov 	struct {
451cd1a677cSIlya Dryomov 		u8 late_status;  /* FRAME_LATE_STATUS_* */
452cd1a677cSIlya Dryomov 		union {
453cd1a677cSIlya Dryomov 			struct {
454cd1a677cSIlya Dryomov 				u32 front_crc;
455cd1a677cSIlya Dryomov 				u32 middle_crc;
456cd1a677cSIlya Dryomov 				u32 data_crc;
457cd1a677cSIlya Dryomov 			} __packed;
458cd1a677cSIlya Dryomov 			u8 pad[CEPH_GCM_BLOCK_LEN - 1];
459cd1a677cSIlya Dryomov 		};
460cd1a677cSIlya Dryomov 	} out_epil;
461cd1a677cSIlya Dryomov };
462cd1a677cSIlya Dryomov 
4633d14c5d2SYehuda Sadeh /*
4643d14c5d2SYehuda Sadeh  * A single connection with another host.
4653d14c5d2SYehuda Sadeh  *
4663d14c5d2SYehuda Sadeh  * We maintain a queue of outgoing messages, and some session state to
4673d14c5d2SYehuda Sadeh  * ensure that we can preserve the lossless, ordered delivery of
4683d14c5d2SYehuda Sadeh  * messages in the case of a TCP disconnect.
4693d14c5d2SYehuda Sadeh  */
4703d14c5d2SYehuda Sadeh struct ceph_connection {
4713d14c5d2SYehuda Sadeh 	void *private;
4723d14c5d2SYehuda Sadeh 
4733d14c5d2SYehuda Sadeh 	const struct ceph_connection_operations *ops;
4743d14c5d2SYehuda Sadeh 
4753d14c5d2SYehuda Sadeh 	struct ceph_messenger *msgr;
476ce2c8903SAlex Elder 
4776d7f62bfSIlya Dryomov 	int state;  /* CEPH_CON_S_* */
478ce2c8903SAlex Elder 	atomic_t sock_state;
4793d14c5d2SYehuda Sadeh 	struct socket *sock;
480ce2c8903SAlex Elder 
4813fefd43eSIlya Dryomov 	unsigned long flags;  /* CEPH_CON_F_* */
4823d14c5d2SYehuda Sadeh 	const char *error_msg;  /* error message, if any */
4833d14c5d2SYehuda Sadeh 
4843d14c5d2SYehuda Sadeh 	struct ceph_entity_name peer_name; /* peer name */
485a56dd9bfSIlya Dryomov 	struct ceph_entity_addr peer_addr; /* peer address */
48612b4629aSIlya Dryomov 	u64 peer_features;
4873d14c5d2SYehuda Sadeh 
4883d14c5d2SYehuda Sadeh 	struct mutex mutex;
4893d14c5d2SYehuda Sadeh 
4903d14c5d2SYehuda Sadeh 	/* out queue */
4913d14c5d2SYehuda Sadeh 	struct list_head out_queue;
4923d14c5d2SYehuda Sadeh 	struct list_head out_sent;   /* sending or sent but unacked */
4933d14c5d2SYehuda Sadeh 	u64 out_seq;		     /* last message queued for send */
4943d14c5d2SYehuda Sadeh 
4953d14c5d2SYehuda Sadeh 	u64 in_seq, in_seq_acked;  /* last message received, acked */
4963d14c5d2SYehuda Sadeh 
497a56dd9bfSIlya Dryomov 	struct ceph_msg *in_msg;
4983d14c5d2SYehuda Sadeh 	struct ceph_msg *out_msg;        /* sending message (== tail of
4993d14c5d2SYehuda Sadeh 					    out_sent) */
5003d14c5d2SYehuda Sadeh 
501038b8d1dSIlya Dryomov 	struct page *bounce_page;
5023d14c5d2SYehuda Sadeh 	u32 in_front_crc, in_middle_crc, in_data_crc;  /* calculated crc */
5033d14c5d2SYehuda Sadeh 
504473bd2d7SArnd Bergmann 	struct timespec64 last_keepalive_ack; /* keepalive2 ack stamp */
5058b9558aaSYan, Zheng 
5063d14c5d2SYehuda Sadeh 	struct delayed_work work;	    /* send|recv work */
5073d14c5d2SYehuda Sadeh 	unsigned long       delay;          /* current delay interval */
508a56dd9bfSIlya Dryomov 
509cd1a677cSIlya Dryomov 	union {
510a56dd9bfSIlya Dryomov 		struct ceph_connection_v1_info v1;
511cd1a677cSIlya Dryomov 		struct ceph_connection_v2_info v2;
512cd1a677cSIlya Dryomov 	};
5133d14c5d2SYehuda Sadeh };
5143d14c5d2SYehuda Sadeh 
515699921d9SIlya Dryomov extern struct page *ceph_zero_page;
5163d14c5d2SYehuda Sadeh 
5176503e0b6SIlya Dryomov void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag);
5186503e0b6SIlya Dryomov void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag);
5196503e0b6SIlya Dryomov bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag);
5206503e0b6SIlya Dryomov bool ceph_con_flag_test_and_clear(struct ceph_connection *con,
5216503e0b6SIlya Dryomov 				  unsigned long con_flag);
5226503e0b6SIlya Dryomov bool ceph_con_flag_test_and_set(struct ceph_connection *con,
5236503e0b6SIlya Dryomov 				unsigned long con_flag);
5246503e0b6SIlya Dryomov 
5256503e0b6SIlya Dryomov void ceph_encode_my_addr(struct ceph_messenger *msgr);
5266503e0b6SIlya Dryomov 
5276503e0b6SIlya Dryomov int ceph_tcp_connect(struct ceph_connection *con);
5286503e0b6SIlya Dryomov int ceph_con_close_socket(struct ceph_connection *con);
5296503e0b6SIlya Dryomov void ceph_con_reset_session(struct ceph_connection *con);
5306503e0b6SIlya Dryomov 
5316503e0b6SIlya Dryomov u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt);
5326503e0b6SIlya Dryomov void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq);
5336503e0b6SIlya Dryomov void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq);
5346503e0b6SIlya Dryomov 
5356503e0b6SIlya Dryomov void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
5366503e0b6SIlya Dryomov 			       struct ceph_msg *msg, size_t length);
5376503e0b6SIlya Dryomov struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
538da4ab869SJeff Layton 				size_t *page_offset, size_t *length);
5396503e0b6SIlya Dryomov void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes);
5406503e0b6SIlya Dryomov 
5416503e0b6SIlya Dryomov u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset,
5426503e0b6SIlya Dryomov 		     unsigned int length);
5436503e0b6SIlya Dryomov 
5446503e0b6SIlya Dryomov bool ceph_addr_is_blank(const struct ceph_entity_addr *addr);
5456503e0b6SIlya Dryomov int ceph_addr_port(const struct ceph_entity_addr *addr);
5466503e0b6SIlya Dryomov void ceph_addr_set_port(struct ceph_entity_addr *addr, int p);
5476503e0b6SIlya Dryomov 
5486503e0b6SIlya Dryomov void ceph_con_process_message(struct ceph_connection *con);
5496503e0b6SIlya Dryomov int ceph_con_in_msg_alloc(struct ceph_connection *con,
5506503e0b6SIlya Dryomov 			  struct ceph_msg_header *hdr, int *skip);
5516503e0b6SIlya Dryomov void ceph_con_get_out_msg(struct ceph_connection *con);
5526503e0b6SIlya Dryomov 
5532f713615SIlya Dryomov /* messenger_v1.c */
554566050e1SIlya Dryomov int ceph_con_v1_try_read(struct ceph_connection *con);
555566050e1SIlya Dryomov int ceph_con_v1_try_write(struct ceph_connection *con);
556566050e1SIlya Dryomov void ceph_con_v1_revoke(struct ceph_connection *con);
557566050e1SIlya Dryomov void ceph_con_v1_revoke_incoming(struct ceph_connection *con);
558566050e1SIlya Dryomov bool ceph_con_v1_opened(struct ceph_connection *con);
559566050e1SIlya Dryomov void ceph_con_v1_reset_session(struct ceph_connection *con);
560566050e1SIlya Dryomov void ceph_con_v1_reset_protocol(struct ceph_connection *con);
561566050e1SIlya Dryomov 
562cd1a677cSIlya Dryomov /* messenger_v2.c */
563cd1a677cSIlya Dryomov int ceph_con_v2_try_read(struct ceph_connection *con);
564cd1a677cSIlya Dryomov int ceph_con_v2_try_write(struct ceph_connection *con);
565cd1a677cSIlya Dryomov void ceph_con_v2_revoke(struct ceph_connection *con);
566cd1a677cSIlya Dryomov void ceph_con_v2_revoke_incoming(struct ceph_connection *con);
567cd1a677cSIlya Dryomov bool ceph_con_v2_opened(struct ceph_connection *con);
568cd1a677cSIlya Dryomov void ceph_con_v2_reset_session(struct ceph_connection *con);
569cd1a677cSIlya Dryomov void ceph_con_v2_reset_protocol(struct ceph_connection *con);
570cd1a677cSIlya Dryomov 
5716503e0b6SIlya Dryomov 
572b726ec97SJeff Layton extern const char *ceph_pr_addr(const struct ceph_entity_addr *addr);
573b726ec97SJeff Layton 
5743d14c5d2SYehuda Sadeh extern int ceph_parse_ips(const char *c, const char *end,
5753d14c5d2SYehuda Sadeh 			  struct ceph_entity_addr *addr,
5762d7c86a8SVenky Shankar 			  int max_count, int *count, char delim);
5773d14c5d2SYehuda Sadeh 
5783d14c5d2SYehuda Sadeh extern int ceph_msgr_init(void);
5793d14c5d2SYehuda Sadeh extern void ceph_msgr_exit(void);
5803d14c5d2SYehuda Sadeh extern void ceph_msgr_flush(void);
5813d14c5d2SYehuda Sadeh 
58215d9882cSAlex Elder extern void ceph_messenger_init(struct ceph_messenger *msgr,
583859bff51SIlya Dryomov 				struct ceph_entity_addr *myaddr);
584757856d2SIlya Dryomov extern void ceph_messenger_fini(struct ceph_messenger *msgr);
585120a75eaSYan, Zheng extern void ceph_messenger_reset_nonce(struct ceph_messenger *msgr);
5863d14c5d2SYehuda Sadeh 
5871bfd89f4SAlex Elder extern void ceph_con_init(struct ceph_connection *con, void *private,
5881bfd89f4SAlex Elder 			const struct ceph_connection_operations *ops,
589b7a9e5ddSSage Weil 			struct ceph_messenger *msgr);
5903d14c5d2SYehuda Sadeh extern void ceph_con_open(struct ceph_connection *con,
591b7a9e5ddSSage Weil 			  __u8 entity_type, __u64 entity_num,
5923d14c5d2SYehuda Sadeh 			  struct ceph_entity_addr *addr);
5933d14c5d2SYehuda Sadeh extern bool ceph_con_opened(struct ceph_connection *con);
5943d14c5d2SYehuda Sadeh extern void ceph_con_close(struct ceph_connection *con);
5953d14c5d2SYehuda Sadeh extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
5966740a845SAlex Elder 
5976740a845SAlex Elder extern void ceph_msg_revoke(struct ceph_msg *msg);
5988921d114SAlex Elder extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
5998921d114SAlex Elder 
6003d14c5d2SYehuda Sadeh extern void ceph_con_keepalive(struct ceph_connection *con);
6018b9558aaSYan, Zheng extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
6028b9558aaSYan, Zheng 				       unsigned long interval);
6033d14c5d2SYehuda Sadeh 
604e8862740SIlya Dryomov void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
605e8862740SIlya Dryomov 			     size_t length, size_t alignment, bool own_pages);
60690af3602SAlex Elder extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
60727fa8385SAlex Elder 				struct ceph_pagelist *pagelist);
608ea96571fSAlex Elder #ifdef CONFIG_BLOCK
6095359a17dSIlya Dryomov void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
6105359a17dSIlya Dryomov 			   u32 length);
611ea96571fSAlex Elder #endif /* CONFIG_BLOCK */
612b9e281c2SIlya Dryomov void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
613b9e281c2SIlya Dryomov 			     struct ceph_bvec_iter *bvec_pos);
614dee0c5f8SJeff Layton void ceph_msg_data_add_iter(struct ceph_msg *msg,
615dee0c5f8SJeff Layton 			    struct iov_iter *iter);
61602afca6cSAlex Elder 
6170d9c1ab3SIlya Dryomov struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
6180d9c1ab3SIlya Dryomov 			       gfp_t flags, bool can_fail);
619b61c2763SSage Weil extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
620b61c2763SSage Weil 				     bool can_fail);
6213d14c5d2SYehuda Sadeh 
6220215e44bSIlya Dryomov extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
6230215e44bSIlya Dryomov extern void ceph_msg_put(struct ceph_msg *msg);
6243d14c5d2SYehuda Sadeh 
6253d14c5d2SYehuda Sadeh extern void ceph_msg_dump(struct ceph_msg *msg);
6263d14c5d2SYehuda Sadeh 
6273d14c5d2SYehuda Sadeh #endif
628