xref: /linux-6.15/include/linux/ceph/messenger.h (revision d396f89d)
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 */
12643794509SAlex Elder };
12743794509SAlex Elder 
1285359a17dSIlya Dryomov #ifdef CONFIG_BLOCK
1295359a17dSIlya Dryomov 
1305359a17dSIlya Dryomov struct ceph_bio_iter {
1315359a17dSIlya Dryomov 	struct bio *bio;
1325359a17dSIlya Dryomov 	struct bvec_iter iter;
1335359a17dSIlya Dryomov };
1345359a17dSIlya Dryomov 
1355359a17dSIlya Dryomov #define __ceph_bio_iter_advance_step(it, n, STEP) do {			      \
1365359a17dSIlya Dryomov 	unsigned int __n = (n), __cur_n;				      \
1375359a17dSIlya Dryomov 									      \
1385359a17dSIlya Dryomov 	while (__n) {							      \
1395359a17dSIlya Dryomov 		BUG_ON(!(it)->iter.bi_size);				      \
1405359a17dSIlya Dryomov 		__cur_n = min((it)->iter.bi_size, __n);			      \
1415359a17dSIlya Dryomov 		(void)(STEP);						      \
1425359a17dSIlya Dryomov 		bio_advance_iter((it)->bio, &(it)->iter, __cur_n);	      \
1435359a17dSIlya Dryomov 		if (!(it)->iter.bi_size && (it)->bio->bi_next) {	      \
1445359a17dSIlya Dryomov 			dout("__ceph_bio_iter_advance_step next bio\n");      \
1455359a17dSIlya Dryomov 			(it)->bio = (it)->bio->bi_next;			      \
1465359a17dSIlya Dryomov 			(it)->iter = (it)->bio->bi_iter;		      \
1475359a17dSIlya Dryomov 		}							      \
1485359a17dSIlya Dryomov 		__n -= __cur_n;						      \
1495359a17dSIlya Dryomov 	}								      \
1505359a17dSIlya Dryomov } while (0)
1515359a17dSIlya Dryomov 
1525359a17dSIlya Dryomov /*
1535359a17dSIlya Dryomov  * Advance @it by @n bytes.
1545359a17dSIlya Dryomov  */
1555359a17dSIlya Dryomov #define ceph_bio_iter_advance(it, n)					      \
1565359a17dSIlya Dryomov 	__ceph_bio_iter_advance_step(it, n, 0)
1575359a17dSIlya Dryomov 
1585359a17dSIlya Dryomov /*
1595359a17dSIlya Dryomov  * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
1605359a17dSIlya Dryomov  */
1615359a17dSIlya Dryomov #define ceph_bio_iter_advance_step(it, n, BVEC_STEP)			      \
1625359a17dSIlya Dryomov 	__ceph_bio_iter_advance_step(it, n, ({				      \
1635359a17dSIlya Dryomov 		struct bio_vec bv;					      \
1645359a17dSIlya Dryomov 		struct bvec_iter __cur_iter;				      \
1655359a17dSIlya Dryomov 									      \
1665359a17dSIlya Dryomov 		__cur_iter = (it)->iter;				      \
1675359a17dSIlya Dryomov 		__cur_iter.bi_size = __cur_n;				      \
1685359a17dSIlya Dryomov 		__bio_for_each_segment(bv, (it)->bio, __cur_iter, __cur_iter) \
1695359a17dSIlya Dryomov 			(void)(BVEC_STEP);				      \
1705359a17dSIlya Dryomov 	}))
1715359a17dSIlya Dryomov 
1725359a17dSIlya Dryomov #endif /* CONFIG_BLOCK */
1735359a17dSIlya Dryomov 
174b9e281c2SIlya Dryomov struct ceph_bvec_iter {
175b9e281c2SIlya Dryomov 	struct bio_vec *bvecs;
176b9e281c2SIlya Dryomov 	struct bvec_iter iter;
177b9e281c2SIlya Dryomov };
178b9e281c2SIlya Dryomov 
179b9e281c2SIlya Dryomov #define __ceph_bvec_iter_advance_step(it, n, STEP) do {			      \
180b9e281c2SIlya Dryomov 	BUG_ON((n) > (it)->iter.bi_size);				      \
181b9e281c2SIlya Dryomov 	(void)(STEP);							      \
182b9e281c2SIlya Dryomov 	bvec_iter_advance((it)->bvecs, &(it)->iter, (n));		      \
183b9e281c2SIlya Dryomov } while (0)
184b9e281c2SIlya Dryomov 
185b9e281c2SIlya Dryomov /*
186b9e281c2SIlya Dryomov  * Advance @it by @n bytes.
187b9e281c2SIlya Dryomov  */
188b9e281c2SIlya Dryomov #define ceph_bvec_iter_advance(it, n)					      \
189b9e281c2SIlya Dryomov 	__ceph_bvec_iter_advance_step(it, n, 0)
190b9e281c2SIlya Dryomov 
191b9e281c2SIlya Dryomov /*
192b9e281c2SIlya Dryomov  * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
193b9e281c2SIlya Dryomov  */
194b9e281c2SIlya Dryomov #define ceph_bvec_iter_advance_step(it, n, BVEC_STEP)			      \
195b9e281c2SIlya Dryomov 	__ceph_bvec_iter_advance_step(it, n, ({				      \
196b9e281c2SIlya Dryomov 		struct bio_vec bv;					      \
197b9e281c2SIlya Dryomov 		struct bvec_iter __cur_iter;				      \
198b9e281c2SIlya Dryomov 									      \
199b9e281c2SIlya Dryomov 		__cur_iter = (it)->iter;				      \
200b9e281c2SIlya Dryomov 		__cur_iter.bi_size = (n);				      \
201b9e281c2SIlya Dryomov 		for_each_bvec(bv, (it)->bvecs, __cur_iter, __cur_iter)	      \
202b9e281c2SIlya Dryomov 			(void)(BVEC_STEP);				      \
203b9e281c2SIlya Dryomov 	}))
204b9e281c2SIlya Dryomov 
205b9e281c2SIlya Dryomov #define ceph_bvec_iter_shorten(it, n) do {				      \
206b9e281c2SIlya Dryomov 	BUG_ON((n) > (it)->iter.bi_size);				      \
207b9e281c2SIlya Dryomov 	(it)->iter.bi_size = (n);					      \
208b9e281c2SIlya Dryomov } while (0)
209b9e281c2SIlya Dryomov 
21036153ec9SAlex Elder struct ceph_msg_data {
21136153ec9SAlex Elder 	enum ceph_msg_data_type		type;
21236153ec9SAlex Elder 	union {
21336153ec9SAlex Elder #ifdef CONFIG_BLOCK
21436153ec9SAlex Elder 		struct {
2155359a17dSIlya Dryomov 			struct ceph_bio_iter	bio_pos;
2165359a17dSIlya Dryomov 			u32			bio_length;
21736153ec9SAlex Elder 		};
21836153ec9SAlex Elder #endif /* CONFIG_BLOCK */
219b9e281c2SIlya Dryomov 		struct ceph_bvec_iter	bvec_pos;
22036153ec9SAlex Elder 		struct {
221e8862740SIlya Dryomov 			struct page	**pages;
22236153ec9SAlex Elder 			size_t		length;		/* total # bytes */
22336153ec9SAlex Elder 			unsigned int	alignment;	/* first page */
224e8862740SIlya Dryomov 			bool		own_pages;
22536153ec9SAlex Elder 		};
22636153ec9SAlex Elder 		struct ceph_pagelist	*pagelist;
22736153ec9SAlex Elder 	};
22836153ec9SAlex Elder };
22936153ec9SAlex Elder 
230fe38a2b6SAlex Elder struct ceph_msg_data_cursor {
231ca8b3a69SAlex Elder 	size_t			total_resid;	/* across all data items */
232ca8b3a69SAlex Elder 
233ca8b3a69SAlex Elder 	struct ceph_msg_data	*data;		/* current data item */
23425aff7c5SAlex Elder 	size_t			resid;		/* bytes not yet consumed */
235ec3bc567SJeff Layton 	int			sr_resid;	/* residual sparse_read len */
2368ae4f4f5SAlex Elder 	bool			need_crc;	/* crc update needed */
237dd236fcbSAlex Elder 	union {
2386aaa4511SAlex Elder #ifdef CONFIG_BLOCK
2395359a17dSIlya Dryomov 		struct ceph_bio_iter	bio_iter;
2406aaa4511SAlex Elder #endif /* CONFIG_BLOCK */
241b9e281c2SIlya Dryomov 		struct bvec_iter	bvec_iter;
242e766d7b5SAlex Elder 		struct {				/* pages */
243e766d7b5SAlex Elder 			unsigned int	page_offset;	/* offset in page */
244e766d7b5SAlex Elder 			unsigned short	page_index;	/* index in array */
245e766d7b5SAlex Elder 			unsigned short	page_count;	/* pages in array */
246e766d7b5SAlex Elder 		};
247dd236fcbSAlex Elder 		struct {				/* pagelist */
248dd236fcbSAlex Elder 			struct page	*page;		/* page from list */
249dd236fcbSAlex Elder 			size_t		offset;		/* bytes from list */
250dd236fcbSAlex Elder 		};
251dd236fcbSAlex Elder 	};
252fe38a2b6SAlex Elder };
253fe38a2b6SAlex Elder 
2543d14c5d2SYehuda Sadeh /*
2553d14c5d2SYehuda Sadeh  * a single message.  it contains a header (src, dest, message type, etc.),
2563d14c5d2SYehuda Sadeh  * footer (crc values, mainly), a "front" message body, and possibly a
2573d14c5d2SYehuda Sadeh  * data payload (stored in some number of pages).
2583d14c5d2SYehuda Sadeh  */
2593d14c5d2SYehuda Sadeh struct ceph_msg {
2603d14c5d2SYehuda Sadeh 	struct ceph_msg_header hdr;	/* header */
26133d07337SYan, Zheng 	union {
2623d14c5d2SYehuda Sadeh 		struct ceph_msg_footer footer;		/* footer */
26333d07337SYan, Zheng 		struct ceph_msg_footer_old old_footer;	/* old format footer */
26433d07337SYan, Zheng 	};
2653d14c5d2SYehuda Sadeh 	struct kvec front;              /* unaligned blobs of message */
2663d14c5d2SYehuda Sadeh 	struct ceph_buffer *middle;
26702afca6cSAlex Elder 
268a1930804SAlex Elder 	size_t				data_length;
2690d9c1ab3SIlya Dryomov 	struct ceph_msg_data		*data;
2700d9c1ab3SIlya Dryomov 	int				num_data_items;
2710d9c1ab3SIlya Dryomov 	int				max_data_items;
27236153ec9SAlex Elder 	struct ceph_msg_data_cursor	cursor;
27302afca6cSAlex Elder 
27402afca6cSAlex Elder 	struct ceph_connection *con;
27502afca6cSAlex Elder 	struct list_head list_head;	/* links for connection lists */
27602afca6cSAlex Elder 
27702afca6cSAlex Elder 	struct kref kref;
2783d14c5d2SYehuda Sadeh 	bool more_to_follow;
2793d14c5d2SYehuda Sadeh 	bool needs_out_seq;
280ec3bc567SJeff Layton 	bool sparse_read;
2813cea4c30SIlya Dryomov 	int front_alloc_len;
2823d14c5d2SYehuda Sadeh 
2833d14c5d2SYehuda Sadeh 	struct ceph_msgpool *pool;
2843d14c5d2SYehuda Sadeh };
2853d14c5d2SYehuda Sadeh 
2866d7f62bfSIlya Dryomov /*
2876d7f62bfSIlya Dryomov  * connection states
2886d7f62bfSIlya Dryomov  */
2896d7f62bfSIlya Dryomov #define CEPH_CON_S_CLOSED		1
2906d7f62bfSIlya Dryomov #define CEPH_CON_S_PREOPEN		2
2916d7f62bfSIlya Dryomov #define CEPH_CON_S_V1_BANNER		3
2926d7f62bfSIlya Dryomov #define CEPH_CON_S_V1_CONNECT_MSG	4
293cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_BANNER_PREFIX	5
294cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_BANNER_PAYLOAD	6
295cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_HELLO		7
296cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_AUTH		8
297cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_AUTH_SIGNATURE	9
298cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_SESSION_CONNECT	10
299cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_SESSION_RECONNECT	11
300cd1a677cSIlya Dryomov #define CEPH_CON_S_OPEN			12
301cd1a677cSIlya Dryomov #define CEPH_CON_S_STANDBY		13
3026d7f62bfSIlya Dryomov 
3033fefd43eSIlya Dryomov /*
3043fefd43eSIlya Dryomov  * ceph_connection flag bits
3053fefd43eSIlya Dryomov  */
3063fefd43eSIlya Dryomov #define CEPH_CON_F_LOSSYTX		0  /* we can close channel or drop
3073fefd43eSIlya Dryomov 					      messages on errors */
3083fefd43eSIlya Dryomov #define CEPH_CON_F_KEEPALIVE_PENDING	1  /* we need to send a keepalive */
3093fefd43eSIlya Dryomov #define CEPH_CON_F_WRITE_PENDING	2  /* we have data ready to send */
3103fefd43eSIlya Dryomov #define CEPH_CON_F_SOCK_CLOSED		3  /* socket state changed to closed */
3113fefd43eSIlya Dryomov #define CEPH_CON_F_BACKOFF		4  /* need to retry queuing delayed
3123fefd43eSIlya Dryomov 					      work */
3133fefd43eSIlya Dryomov 
3143d14c5d2SYehuda Sadeh /* ceph connection fault delay defaults, for exponential backoff */
315418af5b3SIlya Dryomov #define BASE_DELAY_INTERVAL	(HZ / 4)
316418af5b3SIlya Dryomov #define MAX_DELAY_INTERVAL	(15 * HZ)
3173d14c5d2SYehuda Sadeh 
318a56dd9bfSIlya Dryomov struct ceph_connection_v1_info {
319a56dd9bfSIlya Dryomov 	struct kvec out_kvec[8],         /* sending header/footer data */
320a56dd9bfSIlya Dryomov 		*out_kvec_cur;
321a56dd9bfSIlya Dryomov 	int out_kvec_left;   /* kvec's left in out_kvec */
322a56dd9bfSIlya Dryomov 	int out_skip;        /* skip this many bytes */
323a56dd9bfSIlya Dryomov 	int out_kvec_bytes;  /* total bytes left */
324a56dd9bfSIlya Dryomov 	bool out_more;       /* there is more data after the kvecs */
325a56dd9bfSIlya Dryomov 	bool out_msg_done;
326a56dd9bfSIlya Dryomov 
327a56dd9bfSIlya Dryomov 	struct ceph_auth_handshake *auth;
328a56dd9bfSIlya Dryomov 	int auth_retry;       /* true if we need a newer authorizer */
329a56dd9bfSIlya Dryomov 
330a56dd9bfSIlya Dryomov 	/* connection negotiation temps */
331a56dd9bfSIlya Dryomov 	u8 in_banner[CEPH_BANNER_MAX_LEN];
332a56dd9bfSIlya Dryomov 	struct ceph_entity_addr actual_peer_addr;
333a56dd9bfSIlya Dryomov 	struct ceph_entity_addr peer_addr_for_me;
334a56dd9bfSIlya Dryomov 	struct ceph_msg_connect out_connect;
335a56dd9bfSIlya Dryomov 	struct ceph_msg_connect_reply in_reply;
336a56dd9bfSIlya Dryomov 
337a56dd9bfSIlya Dryomov 	int in_base_pos;     /* bytes read */
338a56dd9bfSIlya Dryomov 
339*d396f89dSJeff Layton 	/* sparse reads */
340*d396f89dSJeff Layton 	struct kvec in_sr_kvec; /* current location to receive into */
341*d396f89dSJeff Layton 	u64 in_sr_len;		/* amount of data in this extent */
342*d396f89dSJeff Layton 
343a56dd9bfSIlya Dryomov 	/* message in temps */
344a56dd9bfSIlya Dryomov 	u8 in_tag;           /* protocol control byte */
345a56dd9bfSIlya Dryomov 	struct ceph_msg_header in_hdr;
346a56dd9bfSIlya Dryomov 	__le64 in_temp_ack;  /* for reading an ack */
347a56dd9bfSIlya Dryomov 
348a56dd9bfSIlya Dryomov 	/* message out temps */
349a56dd9bfSIlya Dryomov 	struct ceph_msg_header out_hdr;
350a56dd9bfSIlya Dryomov 	__le64 out_temp_ack;  /* for writing an ack */
351a56dd9bfSIlya Dryomov 	struct ceph_timespec out_temp_keepalive2;  /* for writing keepalive2
352a56dd9bfSIlya Dryomov 						      stamp */
353a56dd9bfSIlya Dryomov 
354a56dd9bfSIlya Dryomov 	u32 connect_seq;      /* identify the most recent connection
355a56dd9bfSIlya Dryomov 				 attempt for this session */
356a56dd9bfSIlya Dryomov 	u32 peer_global_seq;  /* peer's global seq for this connection */
357a56dd9bfSIlya Dryomov };
358a56dd9bfSIlya Dryomov 
359cd1a677cSIlya Dryomov #define CEPH_CRC_LEN			4
360cd1a677cSIlya Dryomov #define CEPH_GCM_KEY_LEN		16
361cd1a677cSIlya Dryomov #define CEPH_GCM_IV_LEN			sizeof(struct ceph_gcm_nonce)
362cd1a677cSIlya Dryomov #define CEPH_GCM_BLOCK_LEN		16
363cd1a677cSIlya Dryomov #define CEPH_GCM_TAG_LEN		16
364cd1a677cSIlya Dryomov 
365cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_LEN		32
366cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_INLINE_LEN	48
367cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_PLAIN_LEN		CEPH_PREAMBLE_LEN
368cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_SECURE_LEN	(CEPH_PREAMBLE_LEN +		\
369cd1a677cSIlya Dryomov 					 CEPH_PREAMBLE_INLINE_LEN +	\
370cd1a677cSIlya Dryomov 					 CEPH_GCM_TAG_LEN)
371cd1a677cSIlya Dryomov #define CEPH_EPILOGUE_PLAIN_LEN		(1 + 3 * CEPH_CRC_LEN)
372cd1a677cSIlya Dryomov #define CEPH_EPILOGUE_SECURE_LEN	(CEPH_GCM_BLOCK_LEN + CEPH_GCM_TAG_LEN)
373cd1a677cSIlya Dryomov 
374cd1a677cSIlya Dryomov #define CEPH_FRAME_MAX_SEGMENT_COUNT	4
375cd1a677cSIlya Dryomov 
376cd1a677cSIlya Dryomov struct ceph_frame_desc {
377cd1a677cSIlya Dryomov 	int fd_tag;  /* FRAME_TAG_* */
378cd1a677cSIlya Dryomov 	int fd_seg_cnt;
379cd1a677cSIlya Dryomov 	int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT];  /* logical */
380cd1a677cSIlya Dryomov 	int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT];
381cd1a677cSIlya Dryomov };
382cd1a677cSIlya Dryomov 
383cd1a677cSIlya Dryomov struct ceph_gcm_nonce {
384cd1a677cSIlya Dryomov 	__le32 fixed;
385cd1a677cSIlya Dryomov 	__le64 counter __packed;
386cd1a677cSIlya Dryomov };
387cd1a677cSIlya Dryomov 
388cd1a677cSIlya Dryomov struct ceph_connection_v2_info {
389cd1a677cSIlya Dryomov 	struct iov_iter in_iter;
390cd1a677cSIlya Dryomov 	struct kvec in_kvecs[5];  /* recvmsg */
391cd1a677cSIlya Dryomov 	struct bio_vec in_bvec;  /* recvmsg (in_cursor) */
392cd1a677cSIlya Dryomov 	int in_kvec_cnt;
393cd1a677cSIlya Dryomov 	int in_state;  /* IN_S_* */
394cd1a677cSIlya Dryomov 
395cd1a677cSIlya Dryomov 	struct iov_iter out_iter;
396cd1a677cSIlya Dryomov 	struct kvec out_kvecs[8];  /* sendmsg */
397cd1a677cSIlya Dryomov 	struct bio_vec out_bvec;  /* sendpage (out_cursor, out_zero),
398cd1a677cSIlya Dryomov 				     sendmsg (out_enc_pages) */
399cd1a677cSIlya Dryomov 	int out_kvec_cnt;
400cd1a677cSIlya Dryomov 	int out_state;  /* OUT_S_* */
401cd1a677cSIlya Dryomov 
402cd1a677cSIlya Dryomov 	int out_zero;  /* # of zero bytes to send */
403cd1a677cSIlya Dryomov 	bool out_iter_sendpage;  /* use sendpage if possible */
404cd1a677cSIlya Dryomov 
405cd1a677cSIlya Dryomov 	struct ceph_frame_desc in_desc;
406cd1a677cSIlya Dryomov 	struct ceph_msg_data_cursor in_cursor;
407cd1a677cSIlya Dryomov 	struct ceph_msg_data_cursor out_cursor;
408cd1a677cSIlya Dryomov 
409cd1a677cSIlya Dryomov 	struct crypto_shash *hmac_tfm;  /* post-auth signature */
410cd1a677cSIlya Dryomov 	struct crypto_aead *gcm_tfm;  /* on-wire encryption */
411cd1a677cSIlya Dryomov 	struct aead_request *gcm_req;
412cd1a677cSIlya Dryomov 	struct crypto_wait gcm_wait;
413cd1a677cSIlya Dryomov 	struct ceph_gcm_nonce in_gcm_nonce;
414cd1a677cSIlya Dryomov 	struct ceph_gcm_nonce out_gcm_nonce;
415cd1a677cSIlya Dryomov 
4162ea88716SIlya Dryomov 	struct page **in_enc_pages;
4172ea88716SIlya Dryomov 	int in_enc_page_cnt;
4182ea88716SIlya Dryomov 	int in_enc_resid;
4192ea88716SIlya Dryomov 	int in_enc_i;
420cd1a677cSIlya Dryomov 	struct page **out_enc_pages;
421cd1a677cSIlya Dryomov 	int out_enc_page_cnt;
422cd1a677cSIlya Dryomov 	int out_enc_resid;
423cd1a677cSIlya Dryomov 	int out_enc_i;
424cd1a677cSIlya Dryomov 
425cd1a677cSIlya Dryomov 	int con_mode;  /* CEPH_CON_MODE_* */
426cd1a677cSIlya Dryomov 
427cd1a677cSIlya Dryomov 	void *conn_bufs[16];
428cd1a677cSIlya Dryomov 	int conn_buf_cnt;
429ec3bc567SJeff Layton 	int data_len_remain;
430cd1a677cSIlya Dryomov 
431cd1a677cSIlya Dryomov 	struct kvec in_sign_kvecs[8];
432cd1a677cSIlya Dryomov 	struct kvec out_sign_kvecs[8];
433cd1a677cSIlya Dryomov 	int in_sign_kvec_cnt;
434cd1a677cSIlya Dryomov 	int out_sign_kvec_cnt;
435cd1a677cSIlya Dryomov 
436cd1a677cSIlya Dryomov 	u64 client_cookie;
437cd1a677cSIlya Dryomov 	u64 server_cookie;
438cd1a677cSIlya Dryomov 	u64 global_seq;
439cd1a677cSIlya Dryomov 	u64 connect_seq;
440cd1a677cSIlya Dryomov 	u64 peer_global_seq;
441cd1a677cSIlya Dryomov 
442cd1a677cSIlya Dryomov 	u8 in_buf[CEPH_PREAMBLE_SECURE_LEN];
443cd1a677cSIlya Dryomov 	u8 out_buf[CEPH_PREAMBLE_SECURE_LEN];
444cd1a677cSIlya Dryomov 	struct {
445cd1a677cSIlya Dryomov 		u8 late_status;  /* FRAME_LATE_STATUS_* */
446cd1a677cSIlya Dryomov 		union {
447cd1a677cSIlya Dryomov 			struct {
448cd1a677cSIlya Dryomov 				u32 front_crc;
449cd1a677cSIlya Dryomov 				u32 middle_crc;
450cd1a677cSIlya Dryomov 				u32 data_crc;
451cd1a677cSIlya Dryomov 			} __packed;
452cd1a677cSIlya Dryomov 			u8 pad[CEPH_GCM_BLOCK_LEN - 1];
453cd1a677cSIlya Dryomov 		};
454cd1a677cSIlya Dryomov 	} out_epil;
455cd1a677cSIlya Dryomov };
456cd1a677cSIlya Dryomov 
4573d14c5d2SYehuda Sadeh /*
4583d14c5d2SYehuda Sadeh  * A single connection with another host.
4593d14c5d2SYehuda Sadeh  *
4603d14c5d2SYehuda Sadeh  * We maintain a queue of outgoing messages, and some session state to
4613d14c5d2SYehuda Sadeh  * ensure that we can preserve the lossless, ordered delivery of
4623d14c5d2SYehuda Sadeh  * messages in the case of a TCP disconnect.
4633d14c5d2SYehuda Sadeh  */
4643d14c5d2SYehuda Sadeh struct ceph_connection {
4653d14c5d2SYehuda Sadeh 	void *private;
4663d14c5d2SYehuda Sadeh 
4673d14c5d2SYehuda Sadeh 	const struct ceph_connection_operations *ops;
4683d14c5d2SYehuda Sadeh 
4693d14c5d2SYehuda Sadeh 	struct ceph_messenger *msgr;
470ce2c8903SAlex Elder 
4716d7f62bfSIlya Dryomov 	int state;  /* CEPH_CON_S_* */
472ce2c8903SAlex Elder 	atomic_t sock_state;
4733d14c5d2SYehuda Sadeh 	struct socket *sock;
474ce2c8903SAlex Elder 
4753fefd43eSIlya Dryomov 	unsigned long flags;  /* CEPH_CON_F_* */
4763d14c5d2SYehuda Sadeh 	const char *error_msg;  /* error message, if any */
4773d14c5d2SYehuda Sadeh 
4783d14c5d2SYehuda Sadeh 	struct ceph_entity_name peer_name; /* peer name */
479a56dd9bfSIlya Dryomov 	struct ceph_entity_addr peer_addr; /* peer address */
48012b4629aSIlya Dryomov 	u64 peer_features;
4813d14c5d2SYehuda Sadeh 
4823d14c5d2SYehuda Sadeh 	struct mutex mutex;
4833d14c5d2SYehuda Sadeh 
4843d14c5d2SYehuda Sadeh 	/* out queue */
4853d14c5d2SYehuda Sadeh 	struct list_head out_queue;
4863d14c5d2SYehuda Sadeh 	struct list_head out_sent;   /* sending or sent but unacked */
4873d14c5d2SYehuda Sadeh 	u64 out_seq;		     /* last message queued for send */
4883d14c5d2SYehuda Sadeh 
4893d14c5d2SYehuda Sadeh 	u64 in_seq, in_seq_acked;  /* last message received, acked */
4903d14c5d2SYehuda Sadeh 
491a56dd9bfSIlya Dryomov 	struct ceph_msg *in_msg;
4923d14c5d2SYehuda Sadeh 	struct ceph_msg *out_msg;        /* sending message (== tail of
4933d14c5d2SYehuda Sadeh 					    out_sent) */
4943d14c5d2SYehuda Sadeh 
495038b8d1dSIlya Dryomov 	struct page *bounce_page;
4963d14c5d2SYehuda Sadeh 	u32 in_front_crc, in_middle_crc, in_data_crc;  /* calculated crc */
4973d14c5d2SYehuda Sadeh 
498473bd2d7SArnd Bergmann 	struct timespec64 last_keepalive_ack; /* keepalive2 ack stamp */
4998b9558aaSYan, Zheng 
5003d14c5d2SYehuda Sadeh 	struct delayed_work work;	    /* send|recv work */
5013d14c5d2SYehuda Sadeh 	unsigned long       delay;          /* current delay interval */
502a56dd9bfSIlya Dryomov 
503cd1a677cSIlya Dryomov 	union {
504a56dd9bfSIlya Dryomov 		struct ceph_connection_v1_info v1;
505cd1a677cSIlya Dryomov 		struct ceph_connection_v2_info v2;
506cd1a677cSIlya Dryomov 	};
5073d14c5d2SYehuda Sadeh };
5083d14c5d2SYehuda Sadeh 
509699921d9SIlya Dryomov extern struct page *ceph_zero_page;
5103d14c5d2SYehuda Sadeh 
5116503e0b6SIlya Dryomov void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag);
5126503e0b6SIlya Dryomov void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag);
5136503e0b6SIlya Dryomov bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag);
5146503e0b6SIlya Dryomov bool ceph_con_flag_test_and_clear(struct ceph_connection *con,
5156503e0b6SIlya Dryomov 				  unsigned long con_flag);
5166503e0b6SIlya Dryomov bool ceph_con_flag_test_and_set(struct ceph_connection *con,
5176503e0b6SIlya Dryomov 				unsigned long con_flag);
5186503e0b6SIlya Dryomov 
5196503e0b6SIlya Dryomov void ceph_encode_my_addr(struct ceph_messenger *msgr);
5206503e0b6SIlya Dryomov 
5216503e0b6SIlya Dryomov int ceph_tcp_connect(struct ceph_connection *con);
5226503e0b6SIlya Dryomov int ceph_con_close_socket(struct ceph_connection *con);
5236503e0b6SIlya Dryomov void ceph_con_reset_session(struct ceph_connection *con);
5246503e0b6SIlya Dryomov 
5256503e0b6SIlya Dryomov u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt);
5266503e0b6SIlya Dryomov void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq);
5276503e0b6SIlya Dryomov void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq);
5286503e0b6SIlya Dryomov 
5296503e0b6SIlya Dryomov void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
5306503e0b6SIlya Dryomov 			       struct ceph_msg *msg, size_t length);
5316503e0b6SIlya Dryomov struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
532da4ab869SJeff Layton 				size_t *page_offset, size_t *length);
5336503e0b6SIlya Dryomov void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes);
5346503e0b6SIlya Dryomov 
5356503e0b6SIlya Dryomov u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset,
5366503e0b6SIlya Dryomov 		     unsigned int length);
5376503e0b6SIlya Dryomov 
5386503e0b6SIlya Dryomov bool ceph_addr_is_blank(const struct ceph_entity_addr *addr);
5396503e0b6SIlya Dryomov int ceph_addr_port(const struct ceph_entity_addr *addr);
5406503e0b6SIlya Dryomov void ceph_addr_set_port(struct ceph_entity_addr *addr, int p);
5416503e0b6SIlya Dryomov 
5426503e0b6SIlya Dryomov void ceph_con_process_message(struct ceph_connection *con);
5436503e0b6SIlya Dryomov int ceph_con_in_msg_alloc(struct ceph_connection *con,
5446503e0b6SIlya Dryomov 			  struct ceph_msg_header *hdr, int *skip);
5456503e0b6SIlya Dryomov void ceph_con_get_out_msg(struct ceph_connection *con);
5466503e0b6SIlya Dryomov 
5472f713615SIlya Dryomov /* messenger_v1.c */
548566050e1SIlya Dryomov int ceph_con_v1_try_read(struct ceph_connection *con);
549566050e1SIlya Dryomov int ceph_con_v1_try_write(struct ceph_connection *con);
550566050e1SIlya Dryomov void ceph_con_v1_revoke(struct ceph_connection *con);
551566050e1SIlya Dryomov void ceph_con_v1_revoke_incoming(struct ceph_connection *con);
552566050e1SIlya Dryomov bool ceph_con_v1_opened(struct ceph_connection *con);
553566050e1SIlya Dryomov void ceph_con_v1_reset_session(struct ceph_connection *con);
554566050e1SIlya Dryomov void ceph_con_v1_reset_protocol(struct ceph_connection *con);
555566050e1SIlya Dryomov 
556cd1a677cSIlya Dryomov /* messenger_v2.c */
557cd1a677cSIlya Dryomov int ceph_con_v2_try_read(struct ceph_connection *con);
558cd1a677cSIlya Dryomov int ceph_con_v2_try_write(struct ceph_connection *con);
559cd1a677cSIlya Dryomov void ceph_con_v2_revoke(struct ceph_connection *con);
560cd1a677cSIlya Dryomov void ceph_con_v2_revoke_incoming(struct ceph_connection *con);
561cd1a677cSIlya Dryomov bool ceph_con_v2_opened(struct ceph_connection *con);
562cd1a677cSIlya Dryomov void ceph_con_v2_reset_session(struct ceph_connection *con);
563cd1a677cSIlya Dryomov void ceph_con_v2_reset_protocol(struct ceph_connection *con);
564cd1a677cSIlya Dryomov 
5656503e0b6SIlya Dryomov 
566b726ec97SJeff Layton extern const char *ceph_pr_addr(const struct ceph_entity_addr *addr);
567b726ec97SJeff Layton 
5683d14c5d2SYehuda Sadeh extern int ceph_parse_ips(const char *c, const char *end,
5693d14c5d2SYehuda Sadeh 			  struct ceph_entity_addr *addr,
5702d7c86a8SVenky Shankar 			  int max_count, int *count, char delim);
5713d14c5d2SYehuda Sadeh 
5723d14c5d2SYehuda Sadeh extern int ceph_msgr_init(void);
5733d14c5d2SYehuda Sadeh extern void ceph_msgr_exit(void);
5743d14c5d2SYehuda Sadeh extern void ceph_msgr_flush(void);
5753d14c5d2SYehuda Sadeh 
57615d9882cSAlex Elder extern void ceph_messenger_init(struct ceph_messenger *msgr,
577859bff51SIlya Dryomov 				struct ceph_entity_addr *myaddr);
578757856d2SIlya Dryomov extern void ceph_messenger_fini(struct ceph_messenger *msgr);
579120a75eaSYan, Zheng extern void ceph_messenger_reset_nonce(struct ceph_messenger *msgr);
5803d14c5d2SYehuda Sadeh 
5811bfd89f4SAlex Elder extern void ceph_con_init(struct ceph_connection *con, void *private,
5821bfd89f4SAlex Elder 			const struct ceph_connection_operations *ops,
583b7a9e5ddSSage Weil 			struct ceph_messenger *msgr);
5843d14c5d2SYehuda Sadeh extern void ceph_con_open(struct ceph_connection *con,
585b7a9e5ddSSage Weil 			  __u8 entity_type, __u64 entity_num,
5863d14c5d2SYehuda Sadeh 			  struct ceph_entity_addr *addr);
5873d14c5d2SYehuda Sadeh extern bool ceph_con_opened(struct ceph_connection *con);
5883d14c5d2SYehuda Sadeh extern void ceph_con_close(struct ceph_connection *con);
5893d14c5d2SYehuda Sadeh extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
5906740a845SAlex Elder 
5916740a845SAlex Elder extern void ceph_msg_revoke(struct ceph_msg *msg);
5928921d114SAlex Elder extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
5938921d114SAlex Elder 
5943d14c5d2SYehuda Sadeh extern void ceph_con_keepalive(struct ceph_connection *con);
5958b9558aaSYan, Zheng extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
5968b9558aaSYan, Zheng 				       unsigned long interval);
5973d14c5d2SYehuda Sadeh 
598e8862740SIlya Dryomov void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
599e8862740SIlya Dryomov 			     size_t length, size_t alignment, bool own_pages);
60090af3602SAlex Elder extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
60127fa8385SAlex Elder 				struct ceph_pagelist *pagelist);
602ea96571fSAlex Elder #ifdef CONFIG_BLOCK
6035359a17dSIlya Dryomov void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
6045359a17dSIlya Dryomov 			   u32 length);
605ea96571fSAlex Elder #endif /* CONFIG_BLOCK */
606b9e281c2SIlya Dryomov void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
607b9e281c2SIlya Dryomov 			     struct ceph_bvec_iter *bvec_pos);
60802afca6cSAlex Elder 
6090d9c1ab3SIlya Dryomov struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
6100d9c1ab3SIlya Dryomov 			       gfp_t flags, bool can_fail);
611b61c2763SSage Weil extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
612b61c2763SSage Weil 				     bool can_fail);
6133d14c5d2SYehuda Sadeh 
6140215e44bSIlya Dryomov extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
6150215e44bSIlya Dryomov extern void ceph_msg_put(struct ceph_msg *msg);
6163d14c5d2SYehuda Sadeh 
6173d14c5d2SYehuda Sadeh extern void ceph_msg_dump(struct ceph_msg *msg);
6183d14c5d2SYehuda Sadeh 
6193d14c5d2SYehuda Sadeh #endif
620