xref: /linux-6.15/include/linux/ceph/messenger.h (revision cd1a677c)
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>
6*cd1a677cSIlya 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);
56*cd1a677cSIlya Dryomov 
57*cd1a677cSIlya Dryomov 	/* msgr2 authentication exchange */
58*cd1a677cSIlya Dryomov 	int (*get_auth_request)(struct ceph_connection *con,
59*cd1a677cSIlya Dryomov 				void *buf, int *buf_len,
60*cd1a677cSIlya Dryomov 				void **authorizer, int *authorizer_len);
61*cd1a677cSIlya Dryomov 	int (*handle_auth_reply_more)(struct ceph_connection *con,
62*cd1a677cSIlya Dryomov 				      void *reply, int reply_len,
63*cd1a677cSIlya Dryomov 				      void *buf, int *buf_len,
64*cd1a677cSIlya Dryomov 				      void **authorizer, int *authorizer_len);
65*cd1a677cSIlya Dryomov 	int (*handle_auth_done)(struct ceph_connection *con,
66*cd1a677cSIlya Dryomov 				u64 global_id, void *reply, int reply_len,
67*cd1a677cSIlya Dryomov 				u8 *session_key, int *session_key_len,
68*cd1a677cSIlya Dryomov 				u8 *con_secret, int *con_secret_len);
69*cd1a677cSIlya Dryomov 	int (*handle_auth_bad_method)(struct ceph_connection *con,
70*cd1a677cSIlya Dryomov 				      int used_proto, int result,
71*cd1a677cSIlya Dryomov 				      const int *allowed_protos, int proto_cnt,
72*cd1a677cSIlya 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
267*cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_BANNER_PREFIX	5
268*cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_BANNER_PAYLOAD	6
269*cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_HELLO		7
270*cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_AUTH		8
271*cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_AUTH_SIGNATURE	9
272*cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_SESSION_CONNECT	10
273*cd1a677cSIlya Dryomov #define CEPH_CON_S_V2_SESSION_RECONNECT	11
274*cd1a677cSIlya Dryomov #define CEPH_CON_S_OPEN			12
275*cd1a677cSIlya 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 
329*cd1a677cSIlya Dryomov #define CEPH_CRC_LEN			4
330*cd1a677cSIlya Dryomov #define CEPH_GCM_KEY_LEN		16
331*cd1a677cSIlya Dryomov #define CEPH_GCM_IV_LEN			sizeof(struct ceph_gcm_nonce)
332*cd1a677cSIlya Dryomov #define CEPH_GCM_BLOCK_LEN		16
333*cd1a677cSIlya Dryomov #define CEPH_GCM_TAG_LEN		16
334*cd1a677cSIlya Dryomov 
335*cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_LEN		32
336*cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_INLINE_LEN	48
337*cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_PLAIN_LEN		CEPH_PREAMBLE_LEN
338*cd1a677cSIlya Dryomov #define CEPH_PREAMBLE_SECURE_LEN	(CEPH_PREAMBLE_LEN +		\
339*cd1a677cSIlya Dryomov 					 CEPH_PREAMBLE_INLINE_LEN +	\
340*cd1a677cSIlya Dryomov 					 CEPH_GCM_TAG_LEN)
341*cd1a677cSIlya Dryomov #define CEPH_EPILOGUE_PLAIN_LEN		(1 + 3 * CEPH_CRC_LEN)
342*cd1a677cSIlya Dryomov #define CEPH_EPILOGUE_SECURE_LEN	(CEPH_GCM_BLOCK_LEN + CEPH_GCM_TAG_LEN)
343*cd1a677cSIlya Dryomov 
344*cd1a677cSIlya Dryomov #define CEPH_FRAME_MAX_SEGMENT_COUNT	4
345*cd1a677cSIlya Dryomov 
346*cd1a677cSIlya Dryomov struct ceph_frame_desc {
347*cd1a677cSIlya Dryomov 	int fd_tag;  /* FRAME_TAG_* */
348*cd1a677cSIlya Dryomov 	int fd_seg_cnt;
349*cd1a677cSIlya Dryomov 	int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT];  /* logical */
350*cd1a677cSIlya Dryomov 	int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT];
351*cd1a677cSIlya Dryomov };
352*cd1a677cSIlya Dryomov 
353*cd1a677cSIlya Dryomov struct ceph_gcm_nonce {
354*cd1a677cSIlya Dryomov 	__le32 fixed;
355*cd1a677cSIlya Dryomov 	__le64 counter __packed;
356*cd1a677cSIlya Dryomov };
357*cd1a677cSIlya Dryomov 
358*cd1a677cSIlya Dryomov struct ceph_connection_v2_info {
359*cd1a677cSIlya Dryomov 	struct iov_iter in_iter;
360*cd1a677cSIlya Dryomov 	struct kvec in_kvecs[5];  /* recvmsg */
361*cd1a677cSIlya Dryomov 	struct bio_vec in_bvec;  /* recvmsg (in_cursor) */
362*cd1a677cSIlya Dryomov 	int in_kvec_cnt;
363*cd1a677cSIlya Dryomov 	int in_state;  /* IN_S_* */
364*cd1a677cSIlya Dryomov 
365*cd1a677cSIlya Dryomov 	struct iov_iter out_iter;
366*cd1a677cSIlya Dryomov 	struct kvec out_kvecs[8];  /* sendmsg */
367*cd1a677cSIlya Dryomov 	struct bio_vec out_bvec;  /* sendpage (out_cursor, out_zero),
368*cd1a677cSIlya Dryomov 				     sendmsg (out_enc_pages) */
369*cd1a677cSIlya Dryomov 	int out_kvec_cnt;
370*cd1a677cSIlya Dryomov 	int out_state;  /* OUT_S_* */
371*cd1a677cSIlya Dryomov 
372*cd1a677cSIlya Dryomov 	int out_zero;  /* # of zero bytes to send */
373*cd1a677cSIlya Dryomov 	bool out_iter_sendpage;  /* use sendpage if possible */
374*cd1a677cSIlya Dryomov 
375*cd1a677cSIlya Dryomov 	struct ceph_frame_desc in_desc;
376*cd1a677cSIlya Dryomov 	struct ceph_msg_data_cursor in_cursor;
377*cd1a677cSIlya Dryomov 	struct ceph_msg_data_cursor out_cursor;
378*cd1a677cSIlya Dryomov 
379*cd1a677cSIlya Dryomov 	struct crypto_shash *hmac_tfm;  /* post-auth signature */
380*cd1a677cSIlya Dryomov 	struct crypto_aead *gcm_tfm;  /* on-wire encryption */
381*cd1a677cSIlya Dryomov 	struct aead_request *gcm_req;
382*cd1a677cSIlya Dryomov 	struct crypto_wait gcm_wait;
383*cd1a677cSIlya Dryomov 	struct ceph_gcm_nonce in_gcm_nonce;
384*cd1a677cSIlya Dryomov 	struct ceph_gcm_nonce out_gcm_nonce;
385*cd1a677cSIlya Dryomov 
386*cd1a677cSIlya Dryomov 	struct page **out_enc_pages;
387*cd1a677cSIlya Dryomov 	int out_enc_page_cnt;
388*cd1a677cSIlya Dryomov 	int out_enc_resid;
389*cd1a677cSIlya Dryomov 	int out_enc_i;
390*cd1a677cSIlya Dryomov 
391*cd1a677cSIlya Dryomov 	int con_mode;  /* CEPH_CON_MODE_* */
392*cd1a677cSIlya Dryomov 
393*cd1a677cSIlya Dryomov 	void *conn_bufs[16];
394*cd1a677cSIlya Dryomov 	int conn_buf_cnt;
395*cd1a677cSIlya Dryomov 
396*cd1a677cSIlya Dryomov 	struct kvec in_sign_kvecs[8];
397*cd1a677cSIlya Dryomov 	struct kvec out_sign_kvecs[8];
398*cd1a677cSIlya Dryomov 	int in_sign_kvec_cnt;
399*cd1a677cSIlya Dryomov 	int out_sign_kvec_cnt;
400*cd1a677cSIlya Dryomov 
401*cd1a677cSIlya Dryomov 	u64 client_cookie;
402*cd1a677cSIlya Dryomov 	u64 server_cookie;
403*cd1a677cSIlya Dryomov 	u64 global_seq;
404*cd1a677cSIlya Dryomov 	u64 connect_seq;
405*cd1a677cSIlya Dryomov 	u64 peer_global_seq;
406*cd1a677cSIlya Dryomov 
407*cd1a677cSIlya Dryomov 	u8 in_buf[CEPH_PREAMBLE_SECURE_LEN];
408*cd1a677cSIlya Dryomov 	u8 out_buf[CEPH_PREAMBLE_SECURE_LEN];
409*cd1a677cSIlya Dryomov 	struct {
410*cd1a677cSIlya Dryomov 		u8 late_status;  /* FRAME_LATE_STATUS_* */
411*cd1a677cSIlya Dryomov 		union {
412*cd1a677cSIlya Dryomov 			struct {
413*cd1a677cSIlya Dryomov 				u32 front_crc;
414*cd1a677cSIlya Dryomov 				u32 middle_crc;
415*cd1a677cSIlya Dryomov 				u32 data_crc;
416*cd1a677cSIlya Dryomov 			} __packed;
417*cd1a677cSIlya Dryomov 			u8 pad[CEPH_GCM_BLOCK_LEN - 1];
418*cd1a677cSIlya Dryomov 		};
419*cd1a677cSIlya Dryomov 	} out_epil;
420*cd1a677cSIlya Dryomov };
421*cd1a677cSIlya Dryomov 
4223d14c5d2SYehuda Sadeh /*
4233d14c5d2SYehuda Sadeh  * A single connection with another host.
4243d14c5d2SYehuda Sadeh  *
4253d14c5d2SYehuda Sadeh  * We maintain a queue of outgoing messages, and some session state to
4263d14c5d2SYehuda Sadeh  * ensure that we can preserve the lossless, ordered delivery of
4273d14c5d2SYehuda Sadeh  * messages in the case of a TCP disconnect.
4283d14c5d2SYehuda Sadeh  */
4293d14c5d2SYehuda Sadeh struct ceph_connection {
4303d14c5d2SYehuda Sadeh 	void *private;
4313d14c5d2SYehuda Sadeh 
4323d14c5d2SYehuda Sadeh 	const struct ceph_connection_operations *ops;
4333d14c5d2SYehuda Sadeh 
4343d14c5d2SYehuda Sadeh 	struct ceph_messenger *msgr;
435ce2c8903SAlex Elder 
4366d7f62bfSIlya Dryomov 	int state;  /* CEPH_CON_S_* */
437ce2c8903SAlex Elder 	atomic_t sock_state;
4383d14c5d2SYehuda Sadeh 	struct socket *sock;
439ce2c8903SAlex Elder 
4403fefd43eSIlya Dryomov 	unsigned long flags;  /* CEPH_CON_F_* */
4413d14c5d2SYehuda Sadeh 	const char *error_msg;  /* error message, if any */
4423d14c5d2SYehuda Sadeh 
4433d14c5d2SYehuda Sadeh 	struct ceph_entity_name peer_name; /* peer name */
444a56dd9bfSIlya Dryomov 	struct ceph_entity_addr peer_addr; /* peer address */
44512b4629aSIlya Dryomov 	u64 peer_features;
4463d14c5d2SYehuda Sadeh 
4473d14c5d2SYehuda Sadeh 	struct mutex mutex;
4483d14c5d2SYehuda Sadeh 
4493d14c5d2SYehuda Sadeh 	/* out queue */
4503d14c5d2SYehuda Sadeh 	struct list_head out_queue;
4513d14c5d2SYehuda Sadeh 	struct list_head out_sent;   /* sending or sent but unacked */
4523d14c5d2SYehuda Sadeh 	u64 out_seq;		     /* last message queued for send */
4533d14c5d2SYehuda Sadeh 
4543d14c5d2SYehuda Sadeh 	u64 in_seq, in_seq_acked;  /* last message received, acked */
4553d14c5d2SYehuda Sadeh 
456a56dd9bfSIlya Dryomov 	struct ceph_msg *in_msg;
4573d14c5d2SYehuda Sadeh 	struct ceph_msg *out_msg;        /* sending message (== tail of
4583d14c5d2SYehuda Sadeh 					    out_sent) */
4593d14c5d2SYehuda Sadeh 
4603d14c5d2SYehuda Sadeh 	u32 in_front_crc, in_middle_crc, in_data_crc;  /* calculated crc */
4613d14c5d2SYehuda Sadeh 
462473bd2d7SArnd Bergmann 	struct timespec64 last_keepalive_ack; /* keepalive2 ack stamp */
4638b9558aaSYan, Zheng 
4643d14c5d2SYehuda Sadeh 	struct delayed_work work;	    /* send|recv work */
4653d14c5d2SYehuda Sadeh 	unsigned long       delay;          /* current delay interval */
466a56dd9bfSIlya Dryomov 
467*cd1a677cSIlya Dryomov 	union {
468a56dd9bfSIlya Dryomov 		struct ceph_connection_v1_info v1;
469*cd1a677cSIlya Dryomov 		struct ceph_connection_v2_info v2;
470*cd1a677cSIlya Dryomov 	};
4713d14c5d2SYehuda Sadeh };
4723d14c5d2SYehuda Sadeh 
473699921d9SIlya Dryomov extern struct page *ceph_zero_page;
4743d14c5d2SYehuda Sadeh 
4756503e0b6SIlya Dryomov void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag);
4766503e0b6SIlya Dryomov void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag);
4776503e0b6SIlya Dryomov bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag);
4786503e0b6SIlya Dryomov bool ceph_con_flag_test_and_clear(struct ceph_connection *con,
4796503e0b6SIlya Dryomov 				  unsigned long con_flag);
4806503e0b6SIlya Dryomov bool ceph_con_flag_test_and_set(struct ceph_connection *con,
4816503e0b6SIlya Dryomov 				unsigned long con_flag);
4826503e0b6SIlya Dryomov 
4836503e0b6SIlya Dryomov void ceph_encode_my_addr(struct ceph_messenger *msgr);
4846503e0b6SIlya Dryomov 
4856503e0b6SIlya Dryomov int ceph_tcp_connect(struct ceph_connection *con);
4866503e0b6SIlya Dryomov int ceph_con_close_socket(struct ceph_connection *con);
4876503e0b6SIlya Dryomov void ceph_con_reset_session(struct ceph_connection *con);
4886503e0b6SIlya Dryomov 
4896503e0b6SIlya Dryomov u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt);
4906503e0b6SIlya Dryomov void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq);
4916503e0b6SIlya Dryomov void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq);
4926503e0b6SIlya Dryomov 
4936503e0b6SIlya Dryomov void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
4946503e0b6SIlya Dryomov 			       struct ceph_msg *msg, size_t length);
4956503e0b6SIlya Dryomov struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
4966503e0b6SIlya Dryomov 				size_t *page_offset, size_t *length,
4976503e0b6SIlya Dryomov 				bool *last_piece);
4986503e0b6SIlya Dryomov void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes);
4996503e0b6SIlya Dryomov 
5006503e0b6SIlya Dryomov u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset,
5016503e0b6SIlya Dryomov 		     unsigned int length);
5026503e0b6SIlya Dryomov 
5036503e0b6SIlya Dryomov bool ceph_addr_is_blank(const struct ceph_entity_addr *addr);
5046503e0b6SIlya Dryomov int ceph_addr_port(const struct ceph_entity_addr *addr);
5056503e0b6SIlya Dryomov void ceph_addr_set_port(struct ceph_entity_addr *addr, int p);
5066503e0b6SIlya Dryomov 
5076503e0b6SIlya Dryomov void ceph_con_process_message(struct ceph_connection *con);
5086503e0b6SIlya Dryomov int ceph_con_in_msg_alloc(struct ceph_connection *con,
5096503e0b6SIlya Dryomov 			  struct ceph_msg_header *hdr, int *skip);
5106503e0b6SIlya Dryomov void ceph_con_get_out_msg(struct ceph_connection *con);
5116503e0b6SIlya Dryomov 
5122f713615SIlya Dryomov /* messenger_v1.c */
513566050e1SIlya Dryomov int ceph_con_v1_try_read(struct ceph_connection *con);
514566050e1SIlya Dryomov int ceph_con_v1_try_write(struct ceph_connection *con);
515566050e1SIlya Dryomov void ceph_con_v1_revoke(struct ceph_connection *con);
516566050e1SIlya Dryomov void ceph_con_v1_revoke_incoming(struct ceph_connection *con);
517566050e1SIlya Dryomov bool ceph_con_v1_opened(struct ceph_connection *con);
518566050e1SIlya Dryomov void ceph_con_v1_reset_session(struct ceph_connection *con);
519566050e1SIlya Dryomov void ceph_con_v1_reset_protocol(struct ceph_connection *con);
520566050e1SIlya Dryomov 
521*cd1a677cSIlya Dryomov /* messenger_v2.c */
522*cd1a677cSIlya Dryomov int ceph_con_v2_try_read(struct ceph_connection *con);
523*cd1a677cSIlya Dryomov int ceph_con_v2_try_write(struct ceph_connection *con);
524*cd1a677cSIlya Dryomov void ceph_con_v2_revoke(struct ceph_connection *con);
525*cd1a677cSIlya Dryomov void ceph_con_v2_revoke_incoming(struct ceph_connection *con);
526*cd1a677cSIlya Dryomov bool ceph_con_v2_opened(struct ceph_connection *con);
527*cd1a677cSIlya Dryomov void ceph_con_v2_reset_session(struct ceph_connection *con);
528*cd1a677cSIlya Dryomov void ceph_con_v2_reset_protocol(struct ceph_connection *con);
529*cd1a677cSIlya Dryomov 
5306503e0b6SIlya Dryomov 
531b726ec97SJeff Layton extern const char *ceph_pr_addr(const struct ceph_entity_addr *addr);
532b726ec97SJeff Layton 
5333d14c5d2SYehuda Sadeh extern int ceph_parse_ips(const char *c, const char *end,
5343d14c5d2SYehuda Sadeh 			  struct ceph_entity_addr *addr,
5353d14c5d2SYehuda Sadeh 			  int max_count, int *count);
5363d14c5d2SYehuda Sadeh 
5373d14c5d2SYehuda Sadeh extern int ceph_msgr_init(void);
5383d14c5d2SYehuda Sadeh extern void ceph_msgr_exit(void);
5393d14c5d2SYehuda Sadeh extern void ceph_msgr_flush(void);
5403d14c5d2SYehuda Sadeh 
54115d9882cSAlex Elder extern void ceph_messenger_init(struct ceph_messenger *msgr,
542859bff51SIlya Dryomov 				struct ceph_entity_addr *myaddr);
543757856d2SIlya Dryomov extern void ceph_messenger_fini(struct ceph_messenger *msgr);
544120a75eaSYan, Zheng extern void ceph_messenger_reset_nonce(struct ceph_messenger *msgr);
5453d14c5d2SYehuda Sadeh 
5461bfd89f4SAlex Elder extern void ceph_con_init(struct ceph_connection *con, void *private,
5471bfd89f4SAlex Elder 			const struct ceph_connection_operations *ops,
548b7a9e5ddSSage Weil 			struct ceph_messenger *msgr);
5493d14c5d2SYehuda Sadeh extern void ceph_con_open(struct ceph_connection *con,
550b7a9e5ddSSage Weil 			  __u8 entity_type, __u64 entity_num,
5513d14c5d2SYehuda Sadeh 			  struct ceph_entity_addr *addr);
5523d14c5d2SYehuda Sadeh extern bool ceph_con_opened(struct ceph_connection *con);
5533d14c5d2SYehuda Sadeh extern void ceph_con_close(struct ceph_connection *con);
5543d14c5d2SYehuda Sadeh extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
5556740a845SAlex Elder 
5566740a845SAlex Elder extern void ceph_msg_revoke(struct ceph_msg *msg);
5578921d114SAlex Elder extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
5588921d114SAlex Elder 
5593d14c5d2SYehuda Sadeh extern void ceph_con_keepalive(struct ceph_connection *con);
5608b9558aaSYan, Zheng extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
5618b9558aaSYan, Zheng 				       unsigned long interval);
5623d14c5d2SYehuda Sadeh 
563e8862740SIlya Dryomov void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
564e8862740SIlya Dryomov 			     size_t length, size_t alignment, bool own_pages);
56590af3602SAlex Elder extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
56627fa8385SAlex Elder 				struct ceph_pagelist *pagelist);
567ea96571fSAlex Elder #ifdef CONFIG_BLOCK
5685359a17dSIlya Dryomov void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
5695359a17dSIlya Dryomov 			   u32 length);
570ea96571fSAlex Elder #endif /* CONFIG_BLOCK */
571b9e281c2SIlya Dryomov void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
572b9e281c2SIlya Dryomov 			     struct ceph_bvec_iter *bvec_pos);
57302afca6cSAlex Elder 
5740d9c1ab3SIlya Dryomov struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
5750d9c1ab3SIlya Dryomov 			       gfp_t flags, bool can_fail);
576b61c2763SSage Weil extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
577b61c2763SSage Weil 				     bool can_fail);
5783d14c5d2SYehuda Sadeh 
5790215e44bSIlya Dryomov extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
5800215e44bSIlya Dryomov extern void ceph_msg_put(struct ceph_msg *msg);
5813d14c5d2SYehuda Sadeh 
5823d14c5d2SYehuda Sadeh extern void ceph_msg_dump(struct ceph_msg *msg);
5833d14c5d2SYehuda Sadeh 
5843d14c5d2SYehuda Sadeh #endif
585