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