xref: /linux-6.15/include/linux/ceph/messenger.h (revision 00a62703)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __FS_CEPH_MESSENGER_H
3 #define __FS_CEPH_MESSENGER_H
4 
5 #include <linux/bvec.h>
6 #include <linux/kref.h>
7 #include <linux/mutex.h>
8 #include <linux/net.h>
9 #include <linux/radix-tree.h>
10 #include <linux/uio.h>
11 #include <linux/workqueue.h>
12 #include <net/net_namespace.h>
13 
14 #include <linux/ceph/types.h>
15 #include <linux/ceph/buffer.h>
16 
17 struct ceph_msg;
18 struct ceph_connection;
19 
20 /*
21  * Ceph defines these callbacks for handling connection events.
22  */
23 struct ceph_connection_operations {
24 	struct ceph_connection *(*get)(struct ceph_connection *);
25 	void (*put)(struct ceph_connection *);
26 
27 	/* handle an incoming message. */
28 	void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
29 
30 	/* authorize an outgoing connection */
31 	struct ceph_auth_handshake *(*get_authorizer) (
32 				struct ceph_connection *con,
33 			       int *proto, int force_new);
34 	int (*verify_authorizer_reply) (struct ceph_connection *con);
35 	int (*invalidate_authorizer)(struct ceph_connection *con);
36 
37 	/* there was some error on the socket (disconnect, whatever) */
38 	void (*fault) (struct ceph_connection *con);
39 
40 	/* a remote host as terminated a message exchange session, and messages
41 	 * we sent (or they tried to send us) may be lost. */
42 	void (*peer_reset) (struct ceph_connection *con);
43 
44 	struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
45 					struct ceph_msg_header *hdr,
46 					int *skip);
47 
48 	void (*reencode_message) (struct ceph_msg *msg);
49 
50 	int (*sign_message) (struct ceph_msg *msg);
51 	int (*check_message_signature) (struct ceph_msg *msg);
52 };
53 
54 /* use format string %s%d */
55 #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
56 
57 struct ceph_messenger {
58 	struct ceph_entity_inst inst;    /* my name+address */
59 	struct ceph_entity_addr my_enc_addr;
60 
61 	atomic_t stopping;
62 	possible_net_t net;
63 
64 	/*
65 	 * the global_seq counts connections i (attempt to) initiate
66 	 * in order to disambiguate certain connect race conditions.
67 	 */
68 	u32 global_seq;
69 	spinlock_t global_seq_lock;
70 };
71 
72 enum ceph_msg_data_type {
73 	CEPH_MSG_DATA_NONE,	/* message contains no data payload */
74 	CEPH_MSG_DATA_PAGES,	/* data source/destination is a page array */
75 	CEPH_MSG_DATA_PAGELIST,	/* data source/destination is a pagelist */
76 #ifdef CONFIG_BLOCK
77 	CEPH_MSG_DATA_BIO,	/* data source/destination is a bio list */
78 #endif /* CONFIG_BLOCK */
79 	CEPH_MSG_DATA_BVECS,	/* data source/destination is a bio_vec array */
80 };
81 
82 static __inline__ bool ceph_msg_data_type_valid(enum ceph_msg_data_type type)
83 {
84 	switch (type) {
85 	case CEPH_MSG_DATA_NONE:
86 	case CEPH_MSG_DATA_PAGES:
87 	case CEPH_MSG_DATA_PAGELIST:
88 #ifdef CONFIG_BLOCK
89 	case CEPH_MSG_DATA_BIO:
90 #endif /* CONFIG_BLOCK */
91 	case CEPH_MSG_DATA_BVECS:
92 		return true;
93 	default:
94 		return false;
95 	}
96 }
97 
98 #ifdef CONFIG_BLOCK
99 
100 struct ceph_bio_iter {
101 	struct bio *bio;
102 	struct bvec_iter iter;
103 };
104 
105 #define __ceph_bio_iter_advance_step(it, n, STEP) do {			      \
106 	unsigned int __n = (n), __cur_n;				      \
107 									      \
108 	while (__n) {							      \
109 		BUG_ON(!(it)->iter.bi_size);				      \
110 		__cur_n = min((it)->iter.bi_size, __n);			      \
111 		(void)(STEP);						      \
112 		bio_advance_iter((it)->bio, &(it)->iter, __cur_n);	      \
113 		if (!(it)->iter.bi_size && (it)->bio->bi_next) {	      \
114 			dout("__ceph_bio_iter_advance_step next bio\n");      \
115 			(it)->bio = (it)->bio->bi_next;			      \
116 			(it)->iter = (it)->bio->bi_iter;		      \
117 		}							      \
118 		__n -= __cur_n;						      \
119 	}								      \
120 } while (0)
121 
122 /*
123  * Advance @it by @n bytes.
124  */
125 #define ceph_bio_iter_advance(it, n)					      \
126 	__ceph_bio_iter_advance_step(it, n, 0)
127 
128 /*
129  * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
130  */
131 #define ceph_bio_iter_advance_step(it, n, BVEC_STEP)			      \
132 	__ceph_bio_iter_advance_step(it, n, ({				      \
133 		struct bio_vec bv;					      \
134 		struct bvec_iter __cur_iter;				      \
135 									      \
136 		__cur_iter = (it)->iter;				      \
137 		__cur_iter.bi_size = __cur_n;				      \
138 		__bio_for_each_segment(bv, (it)->bio, __cur_iter, __cur_iter) \
139 			(void)(BVEC_STEP);				      \
140 	}))
141 
142 #endif /* CONFIG_BLOCK */
143 
144 struct ceph_bvec_iter {
145 	struct bio_vec *bvecs;
146 	struct bvec_iter iter;
147 };
148 
149 #define __ceph_bvec_iter_advance_step(it, n, STEP) do {			      \
150 	BUG_ON((n) > (it)->iter.bi_size);				      \
151 	(void)(STEP);							      \
152 	bvec_iter_advance((it)->bvecs, &(it)->iter, (n));		      \
153 } while (0)
154 
155 /*
156  * Advance @it by @n bytes.
157  */
158 #define ceph_bvec_iter_advance(it, n)					      \
159 	__ceph_bvec_iter_advance_step(it, n, 0)
160 
161 /*
162  * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
163  */
164 #define ceph_bvec_iter_advance_step(it, n, BVEC_STEP)			      \
165 	__ceph_bvec_iter_advance_step(it, n, ({				      \
166 		struct bio_vec bv;					      \
167 		struct bvec_iter __cur_iter;				      \
168 									      \
169 		__cur_iter = (it)->iter;				      \
170 		__cur_iter.bi_size = (n);				      \
171 		for_each_bvec(bv, (it)->bvecs, __cur_iter, __cur_iter)	      \
172 			(void)(BVEC_STEP);				      \
173 	}))
174 
175 #define ceph_bvec_iter_shorten(it, n) do {				      \
176 	BUG_ON((n) > (it)->iter.bi_size);				      \
177 	(it)->iter.bi_size = (n);					      \
178 } while (0)
179 
180 struct ceph_msg_data {
181 	struct list_head		links;	/* ceph_msg->data */
182 	enum ceph_msg_data_type		type;
183 	union {
184 #ifdef CONFIG_BLOCK
185 		struct {
186 			struct ceph_bio_iter	bio_pos;
187 			u32			bio_length;
188 		};
189 #endif /* CONFIG_BLOCK */
190 		struct ceph_bvec_iter	bvec_pos;
191 		struct {
192 			struct page	**pages;	/* NOT OWNER. */
193 			size_t		length;		/* total # bytes */
194 			unsigned int	alignment;	/* first page */
195 		};
196 		struct ceph_pagelist	*pagelist;
197 	};
198 };
199 
200 struct ceph_msg_data_cursor {
201 	size_t			total_resid;	/* across all data items */
202 	struct list_head	*data_head;	/* = &ceph_msg->data */
203 
204 	struct ceph_msg_data	*data;		/* current data item */
205 	size_t			resid;		/* bytes not yet consumed */
206 	bool			last_piece;	/* current is last piece */
207 	bool			need_crc;	/* crc update needed */
208 	union {
209 #ifdef CONFIG_BLOCK
210 		struct ceph_bio_iter	bio_iter;
211 #endif /* CONFIG_BLOCK */
212 		struct bvec_iter	bvec_iter;
213 		struct {				/* pages */
214 			unsigned int	page_offset;	/* offset in page */
215 			unsigned short	page_index;	/* index in array */
216 			unsigned short	page_count;	/* pages in array */
217 		};
218 		struct {				/* pagelist */
219 			struct page	*page;		/* page from list */
220 			size_t		offset;		/* bytes from list */
221 		};
222 	};
223 };
224 
225 /*
226  * a single message.  it contains a header (src, dest, message type, etc.),
227  * footer (crc values, mainly), a "front" message body, and possibly a
228  * data payload (stored in some number of pages).
229  */
230 struct ceph_msg {
231 	struct ceph_msg_header hdr;	/* header */
232 	union {
233 		struct ceph_msg_footer footer;		/* footer */
234 		struct ceph_msg_footer_old old_footer;	/* old format footer */
235 	};
236 	struct kvec front;              /* unaligned blobs of message */
237 	struct ceph_buffer *middle;
238 
239 	size_t				data_length;
240 	struct list_head		data;
241 	struct ceph_msg_data_cursor	cursor;
242 
243 	struct ceph_connection *con;
244 	struct list_head list_head;	/* links for connection lists */
245 
246 	struct kref kref;
247 	bool more_to_follow;
248 	bool needs_out_seq;
249 	int front_alloc_len;
250 	unsigned long ack_stamp;        /* tx: when we were acked */
251 
252 	struct ceph_msgpool *pool;
253 };
254 
255 /* ceph connection fault delay defaults, for exponential backoff */
256 #define BASE_DELAY_INTERVAL	(HZ/2)
257 #define MAX_DELAY_INTERVAL	(5 * 60 * HZ)
258 
259 /*
260  * A single connection with another host.
261  *
262  * We maintain a queue of outgoing messages, and some session state to
263  * ensure that we can preserve the lossless, ordered delivery of
264  * messages in the case of a TCP disconnect.
265  */
266 struct ceph_connection {
267 	void *private;
268 
269 	const struct ceph_connection_operations *ops;
270 
271 	struct ceph_messenger *msgr;
272 
273 	atomic_t sock_state;
274 	struct socket *sock;
275 	struct ceph_entity_addr peer_addr; /* peer address */
276 	struct ceph_entity_addr peer_addr_for_me;
277 
278 	unsigned long flags;
279 	unsigned long state;
280 	const char *error_msg;  /* error message, if any */
281 
282 	struct ceph_entity_name peer_name; /* peer name */
283 
284 	u64 peer_features;
285 	u32 connect_seq;      /* identify the most recent connection
286 				 attempt for this connection, client */
287 	u32 peer_global_seq;  /* peer's global seq for this connection */
288 
289 	int auth_retry;       /* true if we need a newer authorizer */
290 	void *auth_reply_buf;   /* where to put the authorizer reply */
291 	int auth_reply_buf_len;
292 
293 	struct mutex mutex;
294 
295 	/* out queue */
296 	struct list_head out_queue;
297 	struct list_head out_sent;   /* sending or sent but unacked */
298 	u64 out_seq;		     /* last message queued for send */
299 
300 	u64 in_seq, in_seq_acked;  /* last message received, acked */
301 
302 	/* connection negotiation temps */
303 	char in_banner[CEPH_BANNER_MAX_LEN];
304 	struct ceph_msg_connect out_connect;
305 	struct ceph_msg_connect_reply in_reply;
306 	struct ceph_entity_addr actual_peer_addr;
307 
308 	/* message out temps */
309 	struct ceph_msg_header out_hdr;
310 	struct ceph_msg *out_msg;        /* sending message (== tail of
311 					    out_sent) */
312 	bool out_msg_done;
313 
314 	struct kvec out_kvec[8],         /* sending header/footer data */
315 		*out_kvec_cur;
316 	int out_kvec_left;   /* kvec's left in out_kvec */
317 	int out_skip;        /* skip this many bytes */
318 	int out_kvec_bytes;  /* total bytes left */
319 	int out_more;        /* there is more data after the kvecs */
320 	__le64 out_temp_ack; /* for writing an ack */
321 	struct ceph_timespec out_temp_keepalive2; /* for writing keepalive2
322 						     stamp */
323 
324 	/* message in temps */
325 	struct ceph_msg_header in_hdr;
326 	struct ceph_msg *in_msg;
327 	u32 in_front_crc, in_middle_crc, in_data_crc;  /* calculated crc */
328 
329 	char in_tag;         /* protocol control byte */
330 	int in_base_pos;     /* bytes read */
331 	__le64 in_temp_ack;  /* for reading an ack */
332 
333 	struct timespec last_keepalive_ack; /* keepalive2 ack stamp */
334 
335 	struct delayed_work work;	    /* send|recv work */
336 	unsigned long       delay;          /* current delay interval */
337 };
338 
339 
340 extern const char *ceph_pr_addr(const struct sockaddr_storage *ss);
341 extern int ceph_parse_ips(const char *c, const char *end,
342 			  struct ceph_entity_addr *addr,
343 			  int max_count, int *count);
344 
345 
346 extern int ceph_msgr_init(void);
347 extern void ceph_msgr_exit(void);
348 extern void ceph_msgr_flush(void);
349 
350 extern void ceph_messenger_init(struct ceph_messenger *msgr,
351 				struct ceph_entity_addr *myaddr);
352 extern void ceph_messenger_fini(struct ceph_messenger *msgr);
353 
354 extern void ceph_con_init(struct ceph_connection *con, void *private,
355 			const struct ceph_connection_operations *ops,
356 			struct ceph_messenger *msgr);
357 extern void ceph_con_open(struct ceph_connection *con,
358 			  __u8 entity_type, __u64 entity_num,
359 			  struct ceph_entity_addr *addr);
360 extern bool ceph_con_opened(struct ceph_connection *con);
361 extern void ceph_con_close(struct ceph_connection *con);
362 extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
363 
364 extern void ceph_msg_revoke(struct ceph_msg *msg);
365 extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
366 
367 extern void ceph_con_keepalive(struct ceph_connection *con);
368 extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
369 				       unsigned long interval);
370 
371 extern void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
372 				size_t length, size_t alignment);
373 extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
374 				struct ceph_pagelist *pagelist);
375 #ifdef CONFIG_BLOCK
376 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
377 			   u32 length);
378 #endif /* CONFIG_BLOCK */
379 void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
380 			     struct ceph_bvec_iter *bvec_pos);
381 
382 extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
383 				     bool can_fail);
384 
385 extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
386 extern void ceph_msg_put(struct ceph_msg *msg);
387 
388 extern void ceph_msg_dump(struct ceph_msg *msg);
389 
390 #endif
391