xref: /linux-6.15/include/linux/ceph/messenger.h (revision 038b8d1d)
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;
203d14c5d2SYehuda Sadeh 
213d14c5d2SYehuda Sadeh /*
223d14c5d2SYehuda Sadeh  * Ceph defines these callbacks for handling connection events.
233d14c5d2SYehuda Sadeh  */
243d14c5d2SYehuda Sadeh struct ceph_connection_operations {
253d14c5d2SYehuda Sadeh 	struct ceph_connection *(*get)(struct ceph_connection *);
263d14c5d2SYehuda Sadeh 	void (*put)(struct ceph_connection *);
273d14c5d2SYehuda Sadeh 
283d14c5d2SYehuda Sadeh 	/* handle an incoming message. */
293d14c5d2SYehuda Sadeh 	void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
303d14c5d2SYehuda Sadeh 
313d14c5d2SYehuda Sadeh 	/* authorize an outgoing connection */
32a3530df3SAlex Elder 	struct ceph_auth_handshake *(*get_authorizer) (
33a3530df3SAlex Elder 				struct ceph_connection *con,
348f43fb53SAlex Elder 			       int *proto, int force_new);
356daca13dSIlya Dryomov 	int (*add_authorizer_challenge)(struct ceph_connection *con,
366daca13dSIlya Dryomov 					void *challenge_buf,
376daca13dSIlya Dryomov 					int challenge_buf_len);
380dde5848SIlya Dryomov 	int (*verify_authorizer_reply) (struct ceph_connection *con);
393d14c5d2SYehuda Sadeh 	int (*invalidate_authorizer)(struct ceph_connection *con);
403d14c5d2SYehuda Sadeh 
413d14c5d2SYehuda Sadeh 	/* there was some error on the socket (disconnect, whatever) */
423d14c5d2SYehuda Sadeh 	void (*fault) (struct ceph_connection *con);
433d14c5d2SYehuda Sadeh 
443d14c5d2SYehuda Sadeh 	/* a remote host as terminated a message exchange session, and messages
453d14c5d2SYehuda Sadeh 	 * we sent (or they tried to send us) may be lost. */
463d14c5d2SYehuda Sadeh 	void (*peer_reset) (struct ceph_connection *con);
473d14c5d2SYehuda Sadeh 
483d14c5d2SYehuda Sadeh 	struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
493d14c5d2SYehuda Sadeh 					struct ceph_msg_header *hdr,
503d14c5d2SYehuda Sadeh 					int *skip);
5133d07337SYan, Zheng 
5298ad5ebdSIlya Dryomov 	void (*reencode_message) (struct ceph_msg *msg);
5398ad5ebdSIlya Dryomov 
5479dbd1baSIlya Dryomov 	int (*sign_message) (struct ceph_msg *msg);
5579dbd1baSIlya Dryomov 	int (*check_message_signature) (struct ceph_msg *msg);
56cd1a677cSIlya Dryomov 
57cd1a677cSIlya Dryomov 	/* msgr2 authentication exchange */
58cd1a677cSIlya Dryomov 	int (*get_auth_request)(struct ceph_connection *con,
59cd1a677cSIlya Dryomov 				void *buf, int *buf_len,
60cd1a677cSIlya Dryomov 				void **authorizer, int *authorizer_len);
61cd1a677cSIlya Dryomov 	int (*handle_auth_reply_more)(struct ceph_connection *con,
62cd1a677cSIlya Dryomov 				      void *reply, int reply_len,
63cd1a677cSIlya Dryomov 				      void *buf, int *buf_len,
64cd1a677cSIlya Dryomov 				      void **authorizer, int *authorizer_len);
65cd1a677cSIlya Dryomov 	int (*handle_auth_done)(struct ceph_connection *con,
66cd1a677cSIlya Dryomov 				u64 global_id, void *reply, int reply_len,
67cd1a677cSIlya Dryomov 				u8 *session_key, int *session_key_len,
68cd1a677cSIlya Dryomov 				u8 *con_secret, int *con_secret_len);
69cd1a677cSIlya Dryomov 	int (*handle_auth_bad_method)(struct ceph_connection *con,
70cd1a677cSIlya Dryomov 				      int used_proto, int result,
71cd1a677cSIlya Dryomov 				      const int *allowed_protos, int proto_cnt,
72cd1a677cSIlya Dryomov 				      const int *allowed_modes, int mode_cnt);
733d14c5d2SYehuda Sadeh };
743d14c5d2SYehuda Sadeh 
75b07720d0SIlya Dryomov /* use format string %s%lld */
763d14c5d2SYehuda Sadeh #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
773d14c5d2SYehuda Sadeh 
783d14c5d2SYehuda Sadeh struct ceph_messenger {
793d14c5d2SYehuda Sadeh 	struct ceph_entity_inst inst;    /* my name+address */
803d14c5d2SYehuda Sadeh 	struct ceph_entity_addr my_enc_addr;
813d14c5d2SYehuda Sadeh 
82a2a32584SGuanjun He 	atomic_t stopping;
83757856d2SIlya Dryomov 	possible_net_t net;
843d14c5d2SYehuda Sadeh 
853d14c5d2SYehuda Sadeh 	/*
863d14c5d2SYehuda Sadeh 	 * the global_seq counts connections i (attempt to) initiate
873d14c5d2SYehuda Sadeh 	 * in order to disambiguate certain connect race conditions.
883d14c5d2SYehuda Sadeh 	 */
893d14c5d2SYehuda Sadeh 	u32 global_seq;
903d14c5d2SYehuda Sadeh 	spinlock_t global_seq_lock;
913d14c5d2SYehuda Sadeh };
923d14c5d2SYehuda Sadeh 
9343794509SAlex Elder enum ceph_msg_data_type {
9443794509SAlex Elder 	CEPH_MSG_DATA_NONE,	/* message contains no data payload */
9543794509SAlex Elder 	CEPH_MSG_DATA_PAGES,	/* data source/destination is a page array */
9643794509SAlex Elder 	CEPH_MSG_DATA_PAGELIST,	/* data source/destination is a pagelist */
9743794509SAlex Elder #ifdef CONFIG_BLOCK
9843794509SAlex Elder 	CEPH_MSG_DATA_BIO,	/* data source/destination is a bio list */
9943794509SAlex Elder #endif /* CONFIG_BLOCK */
100b9e281c2SIlya Dryomov 	CEPH_MSG_DATA_BVECS,	/* data source/destination is a bio_vec array */
10143794509SAlex Elder };
10243794509SAlex Elder 
1035359a17dSIlya Dryomov #ifdef CONFIG_BLOCK
1045359a17dSIlya Dryomov 
1055359a17dSIlya Dryomov struct ceph_bio_iter {
1065359a17dSIlya Dryomov 	struct bio *bio;
1075359a17dSIlya Dryomov 	struct bvec_iter iter;
1085359a17dSIlya Dryomov };
1095359a17dSIlya Dryomov 
1105359a17dSIlya Dryomov #define __ceph_bio_iter_advance_step(it, n, STEP) do {			      \
1115359a17dSIlya Dryomov 	unsigned int __n = (n), __cur_n;				      \
1125359a17dSIlya Dryomov 									      \
1135359a17dSIlya Dryomov 	while (__n) {							      \
1145359a17dSIlya Dryomov 		BUG_ON(!(it)->iter.bi_size);				      \
1155359a17dSIlya Dryomov 		__cur_n = min((it)->iter.bi_size, __n);			      \
1165359a17dSIlya Dryomov 		(void)(STEP);						      \
1175359a17dSIlya Dryomov 		bio_advance_iter((it)->bio, &(it)->iter, __cur_n);	      \
1185359a17dSIlya Dryomov 		if (!(it)->iter.bi_size && (it)->bio->bi_next) {	      \
1195359a17dSIlya Dryomov 			dout("__ceph_bio_iter_advance_step next bio\n");      \
1205359a17dSIlya Dryomov 			(it)->bio = (it)->bio->bi_next;			      \
1215359a17dSIlya Dryomov 			(it)->iter = (it)->bio->bi_iter;		      \
1225359a17dSIlya Dryomov 		}							      \
1235359a17dSIlya Dryomov 		__n -= __cur_n;						      \
1245359a17dSIlya Dryomov 	}								      \
1255359a17dSIlya Dryomov } while (0)
1265359a17dSIlya Dryomov 
1275359a17dSIlya Dryomov /*
1285359a17dSIlya Dryomov  * Advance @it by @n bytes.
1295359a17dSIlya Dryomov  */
1305359a17dSIlya Dryomov #define ceph_bio_iter_advance(it, n)					      \
1315359a17dSIlya Dryomov 	__ceph_bio_iter_advance_step(it, n, 0)
1325359a17dSIlya Dryomov 
1335359a17dSIlya Dryomov /*
1345359a17dSIlya Dryomov  * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
1355359a17dSIlya Dryomov  */
1365359a17dSIlya Dryomov #define ceph_bio_iter_advance_step(it, n, BVEC_STEP)			      \
1375359a17dSIlya Dryomov 	__ceph_bio_iter_advance_step(it, n, ({				      \
1385359a17dSIlya Dryomov 		struct bio_vec bv;					      \
1395359a17dSIlya Dryomov 		struct bvec_iter __cur_iter;				      \
1405359a17dSIlya Dryomov 									      \
1415359a17dSIlya Dryomov 		__cur_iter = (it)->iter;				      \
1425359a17dSIlya Dryomov 		__cur_iter.bi_size = __cur_n;				      \
1435359a17dSIlya Dryomov 		__bio_for_each_segment(bv, (it)->bio, __cur_iter, __cur_iter) \
1445359a17dSIlya Dryomov 			(void)(BVEC_STEP);				      \
1455359a17dSIlya Dryomov 	}))
1465359a17dSIlya Dryomov 
1475359a17dSIlya Dryomov #endif /* CONFIG_BLOCK */
1485359a17dSIlya Dryomov 
149b9e281c2SIlya Dryomov struct ceph_bvec_iter {
150b9e281c2SIlya Dryomov 	struct bio_vec *bvecs;
151b9e281c2SIlya Dryomov 	struct bvec_iter iter;
152b9e281c2SIlya Dryomov };
153b9e281c2SIlya Dryomov 
154b9e281c2SIlya Dryomov #define __ceph_bvec_iter_advance_step(it, n, STEP) do {			      \
155b9e281c2SIlya Dryomov 	BUG_ON((n) > (it)->iter.bi_size);				      \
156b9e281c2SIlya Dryomov 	(void)(STEP);							      \
157b9e281c2SIlya Dryomov 	bvec_iter_advance((it)->bvecs, &(it)->iter, (n));		      \
158b9e281c2SIlya Dryomov } while (0)
159b9e281c2SIlya Dryomov 
160b9e281c2SIlya Dryomov /*
161b9e281c2SIlya Dryomov  * Advance @it by @n bytes.
162b9e281c2SIlya Dryomov  */
163b9e281c2SIlya Dryomov #define ceph_bvec_iter_advance(it, n)					      \
164b9e281c2SIlya Dryomov 	__ceph_bvec_iter_advance_step(it, n, 0)
165b9e281c2SIlya Dryomov 
166b9e281c2SIlya Dryomov /*
167b9e281c2SIlya Dryomov  * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
168b9e281c2SIlya Dryomov  */
169b9e281c2SIlya Dryomov #define ceph_bvec_iter_advance_step(it, n, BVEC_STEP)			      \
170b9e281c2SIlya Dryomov 	__ceph_bvec_iter_advance_step(it, n, ({				      \
171b9e281c2SIlya Dryomov 		struct bio_vec bv;					      \
172b9e281c2SIlya Dryomov 		struct bvec_iter __cur_iter;				      \
173b9e281c2SIlya Dryomov 									      \
174b9e281c2SIlya Dryomov 		__cur_iter = (it)->iter;				      \
175b9e281c2SIlya Dryomov 		__cur_iter.bi_size = (n);				      \
176b9e281c2SIlya Dryomov 		for_each_bvec(bv, (it)->bvecs, __cur_iter, __cur_iter)	      \
177b9e281c2SIlya Dryomov 			(void)(BVEC_STEP);				      \
178b9e281c2SIlya Dryomov 	}))
179b9e281c2SIlya Dryomov 
180b9e281c2SIlya Dryomov #define ceph_bvec_iter_shorten(it, n) do {				      \
181b9e281c2SIlya Dryomov 	BUG_ON((n) > (it)->iter.bi_size);				      \
182b9e281c2SIlya Dryomov 	(it)->iter.bi_size = (n);					      \
183b9e281c2SIlya Dryomov } while (0)
184b9e281c2SIlya Dryomov 
18536153ec9SAlex Elder struct ceph_msg_data {
18636153ec9SAlex Elder 	enum ceph_msg_data_type		type;
18736153ec9SAlex Elder 	union {
18836153ec9SAlex Elder #ifdef CONFIG_BLOCK
18936153ec9SAlex Elder 		struct {
1905359a17dSIlya Dryomov 			struct ceph_bio_iter	bio_pos;
1915359a17dSIlya Dryomov 			u32			bio_length;
19236153ec9SAlex Elder 		};
19336153ec9SAlex Elder #endif /* CONFIG_BLOCK */
194b9e281c2SIlya Dryomov 		struct ceph_bvec_iter	bvec_pos;
19536153ec9SAlex Elder 		struct {
196e8862740SIlya Dryomov 			struct page	**pages;
19736153ec9SAlex Elder 			size_t		length;		/* total # bytes */
19836153ec9SAlex Elder 			unsigned int	alignment;	/* first page */
199e8862740SIlya Dryomov 			bool		own_pages;
20036153ec9SAlex Elder 		};
20136153ec9SAlex Elder 		struct ceph_pagelist	*pagelist;
20236153ec9SAlex Elder 	};
20336153ec9SAlex Elder };
20436153ec9SAlex Elder 
205fe38a2b6SAlex Elder struct ceph_msg_data_cursor {
206ca8b3a69SAlex Elder 	size_t			total_resid;	/* across all data items */
207ca8b3a69SAlex Elder 
208ca8b3a69SAlex Elder 	struct ceph_msg_data	*data;		/* current data item */
20925aff7c5SAlex Elder 	size_t			resid;		/* bytes not yet consumed */
2108ae4f4f5SAlex Elder 	bool			last_piece;	/* current is last piece */
2118ae4f4f5SAlex Elder 	bool			need_crc;	/* crc update needed */
212dd236fcbSAlex Elder 	union {
2136aaa4511SAlex Elder #ifdef CONFIG_BLOCK
2145359a17dSIlya Dryomov 		struct ceph_bio_iter	bio_iter;
2156aaa4511SAlex Elder #endif /* CONFIG_BLOCK */
216b9e281c2SIlya Dryomov 		struct bvec_iter	bvec_iter;
217e766d7b5SAlex Elder 		struct {				/* pages */
218e766d7b5SAlex Elder 			unsigned int	page_offset;	/* offset in page */
219e766d7b5SAlex Elder 			unsigned short	page_index;	/* index in array */
220e766d7b5SAlex Elder 			unsigned short	page_count;	/* pages in array */
221e766d7b5SAlex Elder 		};
222dd236fcbSAlex Elder 		struct {				/* pagelist */
223dd236fcbSAlex Elder 			struct page	*page;		/* page from list */
224dd236fcbSAlex Elder 			size_t		offset;		/* bytes from list */
225dd236fcbSAlex Elder 		};
226dd236fcbSAlex Elder 	};
227fe38a2b6SAlex Elder };
228fe38a2b6SAlex Elder 
2293d14c5d2SYehuda Sadeh /*
2303d14c5d2SYehuda Sadeh  * a single message.  it contains a header (src, dest, message type, etc.),
2313d14c5d2SYehuda Sadeh  * footer (crc values, mainly), a "front" message body, and possibly a
2323d14c5d2SYehuda Sadeh  * data payload (stored in some number of pages).
2333d14c5d2SYehuda Sadeh  */
2343d14c5d2SYehuda Sadeh struct ceph_msg {
2353d14c5d2SYehuda Sadeh 	struct ceph_msg_header hdr;	/* header */
23633d07337SYan, Zheng 	union {
2373d14c5d2SYehuda Sadeh 		struct ceph_msg_footer footer;		/* footer */
23833d07337SYan, Zheng 		struct ceph_msg_footer_old old_footer;	/* old format footer */
23933d07337SYan, Zheng 	};
2403d14c5d2SYehuda Sadeh 	struct kvec front;              /* unaligned blobs of message */
2413d14c5d2SYehuda Sadeh 	struct ceph_buffer *middle;
24202afca6cSAlex Elder 
243a1930804SAlex Elder 	size_t				data_length;
2440d9c1ab3SIlya Dryomov 	struct ceph_msg_data		*data;
2450d9c1ab3SIlya Dryomov 	int				num_data_items;
2460d9c1ab3SIlya Dryomov 	int				max_data_items;
24736153ec9SAlex Elder 	struct ceph_msg_data_cursor	cursor;
24802afca6cSAlex Elder 
24902afca6cSAlex Elder 	struct ceph_connection *con;
25002afca6cSAlex Elder 	struct list_head list_head;	/* links for connection lists */
25102afca6cSAlex Elder 
25202afca6cSAlex Elder 	struct kref kref;
2533d14c5d2SYehuda Sadeh 	bool more_to_follow;
2543d14c5d2SYehuda Sadeh 	bool needs_out_seq;
2553cea4c30SIlya Dryomov 	int front_alloc_len;
2563d14c5d2SYehuda Sadeh 
2573d14c5d2SYehuda Sadeh 	struct ceph_msgpool *pool;
2583d14c5d2SYehuda Sadeh };
2593d14c5d2SYehuda Sadeh 
2606d7f62bfSIlya Dryomov /*
2616d7f62bfSIlya Dryomov  * connection states
2626d7f62bfSIlya Dryomov  */
2636d7f62bfSIlya Dryomov #define CEPH_CON_S_CLOSED		1
2646d7f62bfSIlya Dryomov #define CEPH_CON_S_PREOPEN		2
2656d7f62bfSIlya Dryomov #define CEPH_CON_S_V1_BANNER		3
2666d7f62bfSIlya Dryomov #define CEPH_CON_S_V1_CONNECT_MSG	4
267cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_BANNER_PREFIX	5
268cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_BANNER_PAYLOAD	6
269cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_HELLO		7
270cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_AUTH		8
271cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_AUTH_SIGNATURE	9
272cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_SESSION_CONNECT	10
273cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_SESSION_RECONNECT	11
274cd1a677cSIlya Dryomov #define CEPH_CON_S_OPEN			12
275cd1a677cSIlya Dryomov #define CEPH_CON_S_STANDBY		13
2766d7f62bfSIlya Dryomov 
2773fefd43eSIlya Dryomov /*
2783fefd43eSIlya Dryomov  * ceph_connection flag bits
2793fefd43eSIlya Dryomov  */
2803fefd43eSIlya Dryomov #define CEPH_CON_F_LOSSYTX		0  /* we can close channel or drop
2813fefd43eSIlya Dryomov 					      messages on errors */
2823fefd43eSIlya Dryomov #define CEPH_CON_F_KEEPALIVE_PENDING	1  /* we need to send a keepalive */
2833fefd43eSIlya Dryomov #define CEPH_CON_F_WRITE_PENDING	2  /* we have data ready to send */
2843fefd43eSIlya Dryomov #define CEPH_CON_F_SOCK_CLOSED		3  /* socket state changed to closed */
2853fefd43eSIlya Dryomov #define CEPH_CON_F_BACKOFF		4  /* need to retry queuing delayed
2863fefd43eSIlya Dryomov 					      work */
2873fefd43eSIlya Dryomov 
2883d14c5d2SYehuda Sadeh /* ceph connection fault delay defaults, for exponential backoff */
289418af5b3SIlya Dryomov #define BASE_DELAY_INTERVAL	(HZ / 4)
290418af5b3SIlya Dryomov #define MAX_DELAY_INTERVAL	(15 * HZ)
2913d14c5d2SYehuda Sadeh 
292a56dd9bfSIlya Dryomov struct ceph_connection_v1_info {
293a56dd9bfSIlya Dryomov 	struct kvec out_kvec[8],         /* sending header/footer data */
294a56dd9bfSIlya Dryomov 		*out_kvec_cur;
295a56dd9bfSIlya Dryomov 	int out_kvec_left;   /* kvec's left in out_kvec */
296a56dd9bfSIlya Dryomov 	int out_skip;        /* skip this many bytes */
297a56dd9bfSIlya Dryomov 	int out_kvec_bytes;  /* total bytes left */
298a56dd9bfSIlya Dryomov 	bool out_more;       /* there is more data after the kvecs */
299a56dd9bfSIlya Dryomov 	bool out_msg_done;
300a56dd9bfSIlya Dryomov 
301a56dd9bfSIlya Dryomov 	struct ceph_auth_handshake *auth;
302a56dd9bfSIlya Dryomov 	int auth_retry;       /* true if we need a newer authorizer */
303a56dd9bfSIlya Dryomov 
304a56dd9bfSIlya Dryomov 	/* connection negotiation temps */
305a56dd9bfSIlya Dryomov 	u8 in_banner[CEPH_BANNER_MAX_LEN];
306a56dd9bfSIlya Dryomov 	struct ceph_entity_addr actual_peer_addr;
307a56dd9bfSIlya Dryomov 	struct ceph_entity_addr peer_addr_for_me;
308a56dd9bfSIlya Dryomov 	struct ceph_msg_connect out_connect;
309a56dd9bfSIlya Dryomov 	struct ceph_msg_connect_reply in_reply;
310a56dd9bfSIlya Dryomov 
311a56dd9bfSIlya Dryomov 	int in_base_pos;     /* bytes read */
312a56dd9bfSIlya Dryomov 
313a56dd9bfSIlya Dryomov 	/* message in temps */
314a56dd9bfSIlya Dryomov 	u8 in_tag;           /* protocol control byte */
315a56dd9bfSIlya Dryomov 	struct ceph_msg_header in_hdr;
316a56dd9bfSIlya Dryomov 	__le64 in_temp_ack;  /* for reading an ack */
317a56dd9bfSIlya Dryomov 
318a56dd9bfSIlya Dryomov 	/* message out temps */
319a56dd9bfSIlya Dryomov 	struct ceph_msg_header out_hdr;
320a56dd9bfSIlya Dryomov 	__le64 out_temp_ack;  /* for writing an ack */
321a56dd9bfSIlya Dryomov 	struct ceph_timespec out_temp_keepalive2;  /* for writing keepalive2
322a56dd9bfSIlya Dryomov 						      stamp */
323a56dd9bfSIlya Dryomov 
324a56dd9bfSIlya Dryomov 	u32 connect_seq;      /* identify the most recent connection
325a56dd9bfSIlya Dryomov 				 attempt for this session */
326a56dd9bfSIlya Dryomov 	u32 peer_global_seq;  /* peer's global seq for this connection */
327a56dd9bfSIlya Dryomov };
328a56dd9bfSIlya Dryomov 
329cd1a677cSIlya Dryomov #define CEPH_CRC_LEN			4
330cd1a677cSIlya Dryomov #define CEPH_GCM_KEY_LEN		16
331cd1a677cSIlya Dryomov #define CEPH_GCM_IV_LEN			sizeof(struct ceph_gcm_nonce)
332cd1a677cSIlya Dryomov #define CEPH_GCM_BLOCK_LEN		16
333cd1a677cSIlya Dryomov #define CEPH_GCM_TAG_LEN		16
334cd1a677cSIlya Dryomov 
335cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_LEN		32
336cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_INLINE_LEN	48
337cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_PLAIN_LEN		CEPH_PREAMBLE_LEN
338cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_SECURE_LEN	(CEPH_PREAMBLE_LEN +		\
339cd1a677cSIlya Dryomov 					 CEPH_PREAMBLE_INLINE_LEN +	\
340cd1a677cSIlya Dryomov 					 CEPH_GCM_TAG_LEN)
341cd1a677cSIlya Dryomov #define CEPH_EPILOGUE_PLAIN_LEN		(1 + 3 * CEPH_CRC_LEN)
342cd1a677cSIlya Dryomov #define CEPH_EPILOGUE_SECURE_LEN	(CEPH_GCM_BLOCK_LEN + CEPH_GCM_TAG_LEN)
343cd1a677cSIlya Dryomov 
344cd1a677cSIlya Dryomov #define CEPH_FRAME_MAX_SEGMENT_COUNT	4
345cd1a677cSIlya Dryomov 
346cd1a677cSIlya Dryomov struct ceph_frame_desc {
347cd1a677cSIlya Dryomov 	int fd_tag;  /* FRAME_TAG_* */
348cd1a677cSIlya Dryomov 	int fd_seg_cnt;
349cd1a677cSIlya Dryomov 	int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT];  /* logical */
350cd1a677cSIlya Dryomov 	int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT];
351cd1a677cSIlya Dryomov };
352cd1a677cSIlya Dryomov 
353cd1a677cSIlya Dryomov struct ceph_gcm_nonce {
354cd1a677cSIlya Dryomov 	__le32 fixed;
355cd1a677cSIlya Dryomov 	__le64 counter __packed;
356cd1a677cSIlya Dryomov };
357cd1a677cSIlya Dryomov 
358cd1a677cSIlya Dryomov struct ceph_connection_v2_info {
359cd1a677cSIlya Dryomov 	struct iov_iter in_iter;
360cd1a677cSIlya Dryomov 	struct kvec in_kvecs[5];  /* recvmsg */
361cd1a677cSIlya Dryomov 	struct bio_vec in_bvec;  /* recvmsg (in_cursor) */
362cd1a677cSIlya Dryomov 	int in_kvec_cnt;
363cd1a677cSIlya Dryomov 	int in_state;  /* IN_S_* */
364cd1a677cSIlya Dryomov 
365cd1a677cSIlya Dryomov 	struct iov_iter out_iter;
366cd1a677cSIlya Dryomov 	struct kvec out_kvecs[8];  /* sendmsg */
367cd1a677cSIlya Dryomov 	struct bio_vec out_bvec;  /* sendpage (out_cursor, out_zero),
368cd1a677cSIlya Dryomov 				     sendmsg (out_enc_pages) */
369cd1a677cSIlya Dryomov 	int out_kvec_cnt;
370cd1a677cSIlya Dryomov 	int out_state;  /* OUT_S_* */
371cd1a677cSIlya Dryomov 
372cd1a677cSIlya Dryomov 	int out_zero;  /* # of zero bytes to send */
373cd1a677cSIlya Dryomov 	bool out_iter_sendpage;  /* use sendpage if possible */
374cd1a677cSIlya Dryomov 
375cd1a677cSIlya Dryomov 	struct ceph_frame_desc in_desc;
376cd1a677cSIlya Dryomov 	struct ceph_msg_data_cursor in_cursor;
377cd1a677cSIlya Dryomov 	struct ceph_msg_data_cursor out_cursor;
378cd1a677cSIlya Dryomov 
379cd1a677cSIlya Dryomov 	struct crypto_shash *hmac_tfm;  /* post-auth signature */
380cd1a677cSIlya Dryomov 	struct crypto_aead *gcm_tfm;  /* on-wire encryption */
381cd1a677cSIlya Dryomov 	struct aead_request *gcm_req;
382cd1a677cSIlya Dryomov 	struct crypto_wait gcm_wait;
383cd1a677cSIlya Dryomov 	struct ceph_gcm_nonce in_gcm_nonce;
384cd1a677cSIlya Dryomov 	struct ceph_gcm_nonce out_gcm_nonce;
385cd1a677cSIlya Dryomov 
3862ea88716SIlya Dryomov 	struct page **in_enc_pages;
3872ea88716SIlya Dryomov 	int in_enc_page_cnt;
3882ea88716SIlya Dryomov 	int in_enc_resid;
3892ea88716SIlya Dryomov 	int in_enc_i;
390cd1a677cSIlya Dryomov 	struct page **out_enc_pages;
391cd1a677cSIlya Dryomov 	int out_enc_page_cnt;
392cd1a677cSIlya Dryomov 	int out_enc_resid;
393cd1a677cSIlya Dryomov 	int out_enc_i;
394cd1a677cSIlya Dryomov 
395cd1a677cSIlya Dryomov 	int con_mode;  /* CEPH_CON_MODE_* */
396cd1a677cSIlya Dryomov 
397cd1a677cSIlya Dryomov 	void *conn_bufs[16];
398cd1a677cSIlya Dryomov 	int conn_buf_cnt;
399cd1a677cSIlya Dryomov 
400cd1a677cSIlya Dryomov 	struct kvec in_sign_kvecs[8];
401cd1a677cSIlya Dryomov 	struct kvec out_sign_kvecs[8];
402cd1a677cSIlya Dryomov 	int in_sign_kvec_cnt;
403cd1a677cSIlya Dryomov 	int out_sign_kvec_cnt;
404cd1a677cSIlya Dryomov 
405cd1a677cSIlya Dryomov 	u64 client_cookie;
406cd1a677cSIlya Dryomov 	u64 server_cookie;
407cd1a677cSIlya Dryomov 	u64 global_seq;
408cd1a677cSIlya Dryomov 	u64 connect_seq;
409cd1a677cSIlya Dryomov 	u64 peer_global_seq;
410cd1a677cSIlya Dryomov 
411cd1a677cSIlya Dryomov 	u8 in_buf[CEPH_PREAMBLE_SECURE_LEN];
412cd1a677cSIlya Dryomov 	u8 out_buf[CEPH_PREAMBLE_SECURE_LEN];
413cd1a677cSIlya Dryomov 	struct {
414cd1a677cSIlya Dryomov 		u8 late_status;  /* FRAME_LATE_STATUS_* */
415cd1a677cSIlya Dryomov 		union {
416cd1a677cSIlya Dryomov 			struct {
417cd1a677cSIlya Dryomov 				u32 front_crc;
418cd1a677cSIlya Dryomov 				u32 middle_crc;
419cd1a677cSIlya Dryomov 				u32 data_crc;
420cd1a677cSIlya Dryomov 			} __packed;
421cd1a677cSIlya Dryomov 			u8 pad[CEPH_GCM_BLOCK_LEN - 1];
422cd1a677cSIlya Dryomov 		};
423cd1a677cSIlya Dryomov 	} out_epil;
424cd1a677cSIlya Dryomov };
425cd1a677cSIlya Dryomov 
4263d14c5d2SYehuda Sadeh /*
4273d14c5d2SYehuda Sadeh  * A single connection with another host.
4283d14c5d2SYehuda Sadeh  *
4293d14c5d2SYehuda Sadeh  * We maintain a queue of outgoing messages, and some session state to
4303d14c5d2SYehuda Sadeh  * ensure that we can preserve the lossless, ordered delivery of
4313d14c5d2SYehuda Sadeh  * messages in the case of a TCP disconnect.
4323d14c5d2SYehuda Sadeh  */
4333d14c5d2SYehuda Sadeh struct ceph_connection {
4343d14c5d2SYehuda Sadeh 	void *private;
4353d14c5d2SYehuda Sadeh 
4363d14c5d2SYehuda Sadeh 	const struct ceph_connection_operations *ops;
4373d14c5d2SYehuda Sadeh 
4383d14c5d2SYehuda Sadeh 	struct ceph_messenger *msgr;
439ce2c8903SAlex Elder 
4406d7f62bfSIlya Dryomov 	int state;  /* CEPH_CON_S_* */
441ce2c8903SAlex Elder 	atomic_t sock_state;
4423d14c5d2SYehuda Sadeh 	struct socket *sock;
443ce2c8903SAlex Elder 
4443fefd43eSIlya Dryomov 	unsigned long flags;  /* CEPH_CON_F_* */
4453d14c5d2SYehuda Sadeh 	const char *error_msg;  /* error message, if any */
4463d14c5d2SYehuda Sadeh 
4473d14c5d2SYehuda Sadeh 	struct ceph_entity_name peer_name; /* peer name */
448a56dd9bfSIlya Dryomov 	struct ceph_entity_addr peer_addr; /* peer address */
44912b4629aSIlya Dryomov 	u64 peer_features;
4503d14c5d2SYehuda Sadeh 
4513d14c5d2SYehuda Sadeh 	struct mutex mutex;
4523d14c5d2SYehuda Sadeh 
4533d14c5d2SYehuda Sadeh 	/* out queue */
4543d14c5d2SYehuda Sadeh 	struct list_head out_queue;
4553d14c5d2SYehuda Sadeh 	struct list_head out_sent;   /* sending or sent but unacked */
4563d14c5d2SYehuda Sadeh 	u64 out_seq;		     /* last message queued for send */
4573d14c5d2SYehuda Sadeh 
4583d14c5d2SYehuda Sadeh 	u64 in_seq, in_seq_acked;  /* last message received, acked */
4593d14c5d2SYehuda Sadeh 
460a56dd9bfSIlya Dryomov 	struct ceph_msg *in_msg;
4613d14c5d2SYehuda Sadeh 	struct ceph_msg *out_msg;        /* sending message (== tail of
4623d14c5d2SYehuda Sadeh 					    out_sent) */
4633d14c5d2SYehuda Sadeh 
464*038b8d1dSIlya Dryomov 	struct page *bounce_page;
4653d14c5d2SYehuda Sadeh 	u32 in_front_crc, in_middle_crc, in_data_crc;  /* calculated crc */
4663d14c5d2SYehuda Sadeh 
467473bd2d7SArnd Bergmann 	struct timespec64 last_keepalive_ack; /* keepalive2 ack stamp */
4688b9558aaSYan, Zheng 
4693d14c5d2SYehuda Sadeh 	struct delayed_work work;	    /* send|recv work */
4703d14c5d2SYehuda Sadeh 	unsigned long       delay;          /* current delay interval */
471a56dd9bfSIlya Dryomov 
472cd1a677cSIlya Dryomov 	union {
473a56dd9bfSIlya Dryomov 		struct ceph_connection_v1_info v1;
474cd1a677cSIlya Dryomov 		struct ceph_connection_v2_info v2;
475cd1a677cSIlya Dryomov 	};
4763d14c5d2SYehuda Sadeh };
4773d14c5d2SYehuda Sadeh 
478699921d9SIlya Dryomov extern struct page *ceph_zero_page;
4793d14c5d2SYehuda Sadeh 
4806503e0b6SIlya Dryomov void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag);
4816503e0b6SIlya Dryomov void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag);
4826503e0b6SIlya Dryomov bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag);
4836503e0b6SIlya Dryomov bool ceph_con_flag_test_and_clear(struct ceph_connection *con,
4846503e0b6SIlya Dryomov 				  unsigned long con_flag);
4856503e0b6SIlya Dryomov bool ceph_con_flag_test_and_set(struct ceph_connection *con,
4866503e0b6SIlya Dryomov 				unsigned long con_flag);
4876503e0b6SIlya Dryomov 
4886503e0b6SIlya Dryomov void ceph_encode_my_addr(struct ceph_messenger *msgr);
4896503e0b6SIlya Dryomov 
4906503e0b6SIlya Dryomov int ceph_tcp_connect(struct ceph_connection *con);
4916503e0b6SIlya Dryomov int ceph_con_close_socket(struct ceph_connection *con);
4926503e0b6SIlya Dryomov void ceph_con_reset_session(struct ceph_connection *con);
4936503e0b6SIlya Dryomov 
4946503e0b6SIlya Dryomov u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt);
4956503e0b6SIlya Dryomov void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq);
4966503e0b6SIlya Dryomov void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq);
4976503e0b6SIlya Dryomov 
4986503e0b6SIlya Dryomov void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
4996503e0b6SIlya Dryomov 			       struct ceph_msg *msg, size_t length);
5006503e0b6SIlya Dryomov struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
5016503e0b6SIlya Dryomov 				size_t *page_offset, size_t *length,
5026503e0b6SIlya Dryomov 				bool *last_piece);
5036503e0b6SIlya Dryomov void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes);
5046503e0b6SIlya Dryomov 
5056503e0b6SIlya Dryomov u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset,
5066503e0b6SIlya Dryomov 		     unsigned int length);
5076503e0b6SIlya Dryomov 
5086503e0b6SIlya Dryomov bool ceph_addr_is_blank(const struct ceph_entity_addr *addr);
5096503e0b6SIlya Dryomov int ceph_addr_port(const struct ceph_entity_addr *addr);
5106503e0b6SIlya Dryomov void ceph_addr_set_port(struct ceph_entity_addr *addr, int p);
5116503e0b6SIlya Dryomov 
5126503e0b6SIlya Dryomov void ceph_con_process_message(struct ceph_connection *con);
5136503e0b6SIlya Dryomov int ceph_con_in_msg_alloc(struct ceph_connection *con,
5146503e0b6SIlya Dryomov 			  struct ceph_msg_header *hdr, int *skip);
5156503e0b6SIlya Dryomov void ceph_con_get_out_msg(struct ceph_connection *con);
5166503e0b6SIlya Dryomov 
5172f713615SIlya Dryomov /* messenger_v1.c */
518566050e1SIlya Dryomov int ceph_con_v1_try_read(struct ceph_connection *con);
519566050e1SIlya Dryomov int ceph_con_v1_try_write(struct ceph_connection *con);
520566050e1SIlya Dryomov void ceph_con_v1_revoke(struct ceph_connection *con);
521566050e1SIlya Dryomov void ceph_con_v1_revoke_incoming(struct ceph_connection *con);
522566050e1SIlya Dryomov bool ceph_con_v1_opened(struct ceph_connection *con);
523566050e1SIlya Dryomov void ceph_con_v1_reset_session(struct ceph_connection *con);
524566050e1SIlya Dryomov void ceph_con_v1_reset_protocol(struct ceph_connection *con);
525566050e1SIlya Dryomov 
526cd1a677cSIlya Dryomov /* messenger_v2.c */
527cd1a677cSIlya Dryomov int ceph_con_v2_try_read(struct ceph_connection *con);
528cd1a677cSIlya Dryomov int ceph_con_v2_try_write(struct ceph_connection *con);
529cd1a677cSIlya Dryomov void ceph_con_v2_revoke(struct ceph_connection *con);
530cd1a677cSIlya Dryomov void ceph_con_v2_revoke_incoming(struct ceph_connection *con);
531cd1a677cSIlya Dryomov bool ceph_con_v2_opened(struct ceph_connection *con);
532cd1a677cSIlya Dryomov void ceph_con_v2_reset_session(struct ceph_connection *con);
533cd1a677cSIlya Dryomov void ceph_con_v2_reset_protocol(struct ceph_connection *con);
534cd1a677cSIlya Dryomov 
5356503e0b6SIlya Dryomov 
536b726ec97SJeff Layton extern const char *ceph_pr_addr(const struct ceph_entity_addr *addr);
537b726ec97SJeff Layton 
5383d14c5d2SYehuda Sadeh extern int ceph_parse_ips(const char *c, const char *end,
5393d14c5d2SYehuda Sadeh 			  struct ceph_entity_addr *addr,
5402d7c86a8SVenky Shankar 			  int max_count, int *count, char delim);
5413d14c5d2SYehuda Sadeh 
5423d14c5d2SYehuda Sadeh extern int ceph_msgr_init(void);
5433d14c5d2SYehuda Sadeh extern void ceph_msgr_exit(void);
5443d14c5d2SYehuda Sadeh extern void ceph_msgr_flush(void);
5453d14c5d2SYehuda Sadeh 
54615d9882cSAlex Elder extern void ceph_messenger_init(struct ceph_messenger *msgr,
547859bff51SIlya Dryomov 				struct ceph_entity_addr *myaddr);
548757856d2SIlya Dryomov extern void ceph_messenger_fini(struct ceph_messenger *msgr);
549120a75eaSYan, Zheng extern void ceph_messenger_reset_nonce(struct ceph_messenger *msgr);
5503d14c5d2SYehuda Sadeh 
5511bfd89f4SAlex Elder extern void ceph_con_init(struct ceph_connection *con, void *private,
5521bfd89f4SAlex Elder 			const struct ceph_connection_operations *ops,
553b7a9e5ddSSage Weil 			struct ceph_messenger *msgr);
5543d14c5d2SYehuda Sadeh extern void ceph_con_open(struct ceph_connection *con,
555b7a9e5ddSSage Weil 			  __u8 entity_type, __u64 entity_num,
5563d14c5d2SYehuda Sadeh 			  struct ceph_entity_addr *addr);
5573d14c5d2SYehuda Sadeh extern bool ceph_con_opened(struct ceph_connection *con);
5583d14c5d2SYehuda Sadeh extern void ceph_con_close(struct ceph_connection *con);
5593d14c5d2SYehuda Sadeh extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
5606740a845SAlex Elder 
5616740a845SAlex Elder extern void ceph_msg_revoke(struct ceph_msg *msg);
5628921d114SAlex Elder extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
5638921d114SAlex Elder 
5643d14c5d2SYehuda Sadeh extern void ceph_con_keepalive(struct ceph_connection *con);
5658b9558aaSYan, Zheng extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
5668b9558aaSYan, Zheng 				       unsigned long interval);
5673d14c5d2SYehuda Sadeh 
568e8862740SIlya Dryomov void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
569e8862740SIlya Dryomov 			     size_t length, size_t alignment, bool own_pages);
57090af3602SAlex Elder extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
57127fa8385SAlex Elder 				struct ceph_pagelist *pagelist);
572ea96571fSAlex Elder #ifdef CONFIG_BLOCK
5735359a17dSIlya Dryomov void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
5745359a17dSIlya Dryomov 			   u32 length);
575ea96571fSAlex Elder #endif /* CONFIG_BLOCK */
576b9e281c2SIlya Dryomov void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
577b9e281c2SIlya Dryomov 			     struct ceph_bvec_iter *bvec_pos);
57802afca6cSAlex Elder 
5790d9c1ab3SIlya Dryomov struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
5800d9c1ab3SIlya Dryomov 			       gfp_t flags, bool can_fail);
581b61c2763SSage Weil extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
582b61c2763SSage Weil 				     bool can_fail);
5833d14c5d2SYehuda Sadeh 
5840215e44bSIlya Dryomov extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
5850215e44bSIlya Dryomov extern void ceph_msg_put(struct ceph_msg *msg);
5863d14c5d2SYehuda Sadeh 
5873d14c5d2SYehuda Sadeh extern void ceph_msg_dump(struct ceph_msg *msg);
5883d14c5d2SYehuda Sadeh 
5893d14c5d2SYehuda Sadeh #endif
590