xref: /linux-6.15/include/linux/ceph/messenger.h (revision b64eedb8)
1 #ifndef __FS_CEPH_MESSENGER_H
2 #define __FS_CEPH_MESSENGER_H
3 
4 #include <linux/blk_types.h>
5 #include <linux/kref.h>
6 #include <linux/mutex.h>
7 #include <linux/net.h>
8 #include <linux/radix-tree.h>
9 #include <linux/uio.h>
10 #include <linux/workqueue.h>
11 
12 #include <linux/ceph/types.h>
13 #include <linux/ceph/buffer.h>
14 
15 struct ceph_msg;
16 struct ceph_connection;
17 
18 /*
19  * Ceph defines these callbacks for handling connection events.
20  */
21 struct ceph_connection_operations {
22 	struct ceph_connection *(*get)(struct ceph_connection *);
23 	void (*put)(struct ceph_connection *);
24 
25 	/* handle an incoming message. */
26 	void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
27 
28 	/* authorize an outgoing connection */
29 	struct ceph_auth_handshake *(*get_authorizer) (
30 				struct ceph_connection *con,
31 			       int *proto, int force_new);
32 	int (*verify_authorizer_reply) (struct ceph_connection *con, int len);
33 	int (*invalidate_authorizer)(struct ceph_connection *con);
34 
35 	/* there was some error on the socket (disconnect, whatever) */
36 	void (*fault) (struct ceph_connection *con);
37 
38 	/* a remote host as terminated a message exchange session, and messages
39 	 * we sent (or they tried to send us) may be lost. */
40 	void (*peer_reset) (struct ceph_connection *con);
41 
42 	struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
43 					struct ceph_msg_header *hdr,
44 					int *skip);
45 	int (*sign_message) (struct ceph_connection *con, struct ceph_msg *msg);
46 
47 	int (*check_message_signature) (struct ceph_connection *con,
48 					struct ceph_msg *msg);
49 };
50 
51 /* use format string %s%d */
52 #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
53 
54 struct ceph_messenger {
55 	struct ceph_entity_inst inst;    /* my name+address */
56 	struct ceph_entity_addr my_enc_addr;
57 
58 	atomic_t stopping;
59 	bool nocrc;
60 
61 	/*
62 	 * the global_seq counts connections i (attempt to) initiate
63 	 * in order to disambiguate certain connect race conditions.
64 	 */
65 	u32 global_seq;
66 	spinlock_t global_seq_lock;
67 
68 	u64 supported_features;
69 	u64 required_features;
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 };
80 
81 static __inline__ bool ceph_msg_data_type_valid(enum ceph_msg_data_type type)
82 {
83 	switch (type) {
84 	case CEPH_MSG_DATA_NONE:
85 	case CEPH_MSG_DATA_PAGES:
86 	case CEPH_MSG_DATA_PAGELIST:
87 #ifdef CONFIG_BLOCK
88 	case CEPH_MSG_DATA_BIO:
89 #endif /* CONFIG_BLOCK */
90 		return true;
91 	default:
92 		return false;
93 	}
94 }
95 
96 struct ceph_msg_data {
97 	struct list_head		links;	/* ceph_msg->data */
98 	enum ceph_msg_data_type		type;
99 	union {
100 #ifdef CONFIG_BLOCK
101 		struct {
102 			struct bio	*bio;
103 			size_t		bio_length;
104 		};
105 #endif /* CONFIG_BLOCK */
106 		struct {
107 			struct page	**pages;	/* NOT OWNER. */
108 			size_t		length;		/* total # bytes */
109 			unsigned int	alignment;	/* first page */
110 		};
111 		struct ceph_pagelist	*pagelist;
112 	};
113 };
114 
115 struct ceph_msg_data_cursor {
116 	size_t			total_resid;	/* across all data items */
117 	struct list_head	*data_head;	/* = &ceph_msg->data */
118 
119 	struct ceph_msg_data	*data;		/* current data item */
120 	size_t			resid;		/* bytes not yet consumed */
121 	bool			last_piece;	/* current is last piece */
122 	bool			need_crc;	/* crc update needed */
123 	union {
124 #ifdef CONFIG_BLOCK
125 		struct {				/* bio */
126 			struct bio	*bio;		/* bio from list */
127 			struct bvec_iter bvec_iter;
128 		};
129 #endif /* CONFIG_BLOCK */
130 		struct {				/* pages */
131 			unsigned int	page_offset;	/* offset in page */
132 			unsigned short	page_index;	/* index in array */
133 			unsigned short	page_count;	/* pages in array */
134 		};
135 		struct {				/* pagelist */
136 			struct page	*page;		/* page from list */
137 			size_t		offset;		/* bytes from list */
138 		};
139 	};
140 };
141 
142 /*
143  * a single message.  it contains a header (src, dest, message type, etc.),
144  * footer (crc values, mainly), a "front" message body, and possibly a
145  * data payload (stored in some number of pages).
146  */
147 struct ceph_msg {
148 	struct ceph_msg_header hdr;	/* header */
149 	union {
150 		struct ceph_msg_footer footer;		/* footer */
151 		struct ceph_msg_footer_old old_footer;	/* old format footer */
152 	};
153 	struct kvec front;              /* unaligned blobs of message */
154 	struct ceph_buffer *middle;
155 
156 	size_t				data_length;
157 	struct list_head		data;
158 	struct ceph_msg_data_cursor	cursor;
159 
160 	struct ceph_connection *con;
161 	struct list_head list_head;	/* links for connection lists */
162 
163 	struct kref kref;
164 	bool more_to_follow;
165 	bool needs_out_seq;
166 	int front_alloc_len;
167 	unsigned long ack_stamp;        /* tx: when we were acked */
168 
169 	struct ceph_msgpool *pool;
170 };
171 
172 /* ceph connection fault delay defaults, for exponential backoff */
173 #define BASE_DELAY_INTERVAL	(HZ/2)
174 #define MAX_DELAY_INTERVAL	(5 * 60 * HZ)
175 
176 /*
177  * A single connection with another host.
178  *
179  * We maintain a queue of outgoing messages, and some session state to
180  * ensure that we can preserve the lossless, ordered delivery of
181  * messages in the case of a TCP disconnect.
182  */
183 struct ceph_connection {
184 	void *private;
185 
186 	const struct ceph_connection_operations *ops;
187 
188 	struct ceph_messenger *msgr;
189 
190 	atomic_t sock_state;
191 	struct socket *sock;
192 	struct ceph_entity_addr peer_addr; /* peer address */
193 	struct ceph_entity_addr peer_addr_for_me;
194 
195 	unsigned long flags;
196 	unsigned long state;
197 	const char *error_msg;  /* error message, if any */
198 
199 	struct ceph_entity_name peer_name; /* peer name */
200 
201 	u64 peer_features;
202 	u32 connect_seq;      /* identify the most recent connection
203 				 attempt for this connection, client */
204 	u32 peer_global_seq;  /* peer's global seq for this connection */
205 
206 	int auth_retry;       /* true if we need a newer authorizer */
207 	void *auth_reply_buf;   /* where to put the authorizer reply */
208 	int auth_reply_buf_len;
209 
210 	struct mutex mutex;
211 
212 	/* out queue */
213 	struct list_head out_queue;
214 	struct list_head out_sent;   /* sending or sent but unacked */
215 	u64 out_seq;		     /* last message queued for send */
216 
217 	u64 in_seq, in_seq_acked;  /* last message received, acked */
218 
219 	/* connection negotiation temps */
220 	char in_banner[CEPH_BANNER_MAX_LEN];
221 	struct ceph_msg_connect out_connect;
222 	struct ceph_msg_connect_reply in_reply;
223 	struct ceph_entity_addr actual_peer_addr;
224 
225 	/* message out temps */
226 	struct ceph_msg *out_msg;        /* sending message (== tail of
227 					    out_sent) */
228 	bool out_msg_done;
229 
230 	struct kvec out_kvec[8],         /* sending header/footer data */
231 		*out_kvec_cur;
232 	int out_kvec_left;   /* kvec's left in out_kvec */
233 	int out_skip;        /* skip this many bytes */
234 	int out_kvec_bytes;  /* total bytes left */
235 	bool out_kvec_is_msg; /* kvec refers to out_msg */
236 	int out_more;        /* there is more data after the kvecs */
237 	__le64 out_temp_ack; /* for writing an ack */
238 
239 	/* message in temps */
240 	struct ceph_msg_header in_hdr;
241 	struct ceph_msg *in_msg;
242 	u32 in_front_crc, in_middle_crc, in_data_crc;  /* calculated crc */
243 
244 	char in_tag;         /* protocol control byte */
245 	int in_base_pos;     /* bytes read */
246 	__le64 in_temp_ack;  /* for reading an ack */
247 
248 	struct delayed_work work;	    /* send|recv work */
249 	unsigned long       delay;          /* current delay interval */
250 };
251 
252 
253 extern const char *ceph_pr_addr(const struct sockaddr_storage *ss);
254 extern int ceph_parse_ips(const char *c, const char *end,
255 			  struct ceph_entity_addr *addr,
256 			  int max_count, int *count);
257 
258 
259 extern int ceph_msgr_init(void);
260 extern void ceph_msgr_exit(void);
261 extern void ceph_msgr_flush(void);
262 
263 extern void ceph_messenger_init(struct ceph_messenger *msgr,
264 			struct ceph_entity_addr *myaddr,
265 			u64 supported_features,
266 			u64 required_features,
267 			bool nocrc);
268 
269 extern void ceph_con_init(struct ceph_connection *con, void *private,
270 			const struct ceph_connection_operations *ops,
271 			struct ceph_messenger *msgr);
272 extern void ceph_con_open(struct ceph_connection *con,
273 			  __u8 entity_type, __u64 entity_num,
274 			  struct ceph_entity_addr *addr);
275 extern bool ceph_con_opened(struct ceph_connection *con);
276 extern void ceph_con_close(struct ceph_connection *con);
277 extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
278 
279 extern void ceph_msg_revoke(struct ceph_msg *msg);
280 extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
281 
282 extern void ceph_con_keepalive(struct ceph_connection *con);
283 
284 extern void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
285 				size_t length, size_t alignment);
286 extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
287 				struct ceph_pagelist *pagelist);
288 #ifdef CONFIG_BLOCK
289 extern void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
290 				size_t length);
291 #endif /* CONFIG_BLOCK */
292 
293 extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
294 				     bool can_fail);
295 
296 extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
297 extern void ceph_msg_put(struct ceph_msg *msg);
298 
299 extern void ceph_msg_dump(struct ceph_msg *msg);
300 
301 #endif
302