1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __FS_CEPH_MESSENGER_H
3 #define __FS_CEPH_MESSENGER_H
5 #include <linux/bvec.h>
6 #include <linux/kref.h>
7 #include <linux/mutex.h>
9 #include <linux/radix-tree.h>
10 #include <linux/uio.h>
11 #include <linux/workqueue.h>
12 #include <net/net_namespace.h>
14 #include <linux/ceph/types.h>
15 #include <linux/ceph/buffer.h>
18 struct ceph_connection;
21 * Ceph defines these callbacks for handling connection events.
23 struct ceph_connection_operations {
24 struct ceph_connection *(*get)(struct ceph_connection *);
25 void (*put)(struct ceph_connection *);
27 /* handle an incoming message. */
28 void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
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);
37 /* there was some error on the socket (disconnect, whatever) */
38 void (*fault) (struct ceph_connection *con);
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);
44 struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
45 struct ceph_msg_header *hdr,
48 void (*reencode_message) (struct ceph_msg *msg);
50 int (*sign_message) (struct ceph_msg *msg);
51 int (*check_message_signature) (struct ceph_msg *msg);
54 /* use format string %s%d */
55 #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
57 struct ceph_messenger {
58 struct ceph_entity_inst inst; /* my name+address */
59 struct ceph_entity_addr my_enc_addr;
65 * the global_seq counts connections i (attempt to) initiate
66 * in order to disambiguate certain connect race conditions.
69 spinlock_t global_seq_lock;
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 */
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 */
82 static __inline__ bool ceph_msg_data_type_valid(enum ceph_msg_data_type type)
85 case CEPH_MSG_DATA_NONE:
86 case CEPH_MSG_DATA_PAGES:
87 case CEPH_MSG_DATA_PAGELIST:
89 case CEPH_MSG_DATA_BIO:
90 #endif /* CONFIG_BLOCK */
91 case CEPH_MSG_DATA_BVECS:
100 struct ceph_bio_iter {
102 struct bvec_iter iter;
105 #define __ceph_bio_iter_advance_step(it, n, STEP) do { \
106 unsigned int __n = (n), __cur_n; \
109 BUG_ON(!(it)->iter.bi_size); \
110 __cur_n = min((it)->iter.bi_size, __n); \
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; \
123 * Advance @it by @n bytes.
125 #define ceph_bio_iter_advance(it, n) \
126 __ceph_bio_iter_advance_step(it, n, 0)
129 * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
131 #define ceph_bio_iter_advance_step(it, n, BVEC_STEP) \
132 __ceph_bio_iter_advance_step(it, n, ({ \
134 struct bvec_iter __cur_iter; \
136 __cur_iter = (it)->iter; \
137 __cur_iter.bi_size = __cur_n; \
138 __bio_for_each_segment(bv, (it)->bio, __cur_iter, __cur_iter) \
142 #endif /* CONFIG_BLOCK */
144 struct ceph_bvec_iter {
145 struct bio_vec *bvecs;
146 struct bvec_iter iter;
149 #define __ceph_bvec_iter_advance_step(it, n, STEP) do { \
150 BUG_ON((n) > (it)->iter.bi_size); \
152 bvec_iter_advance((it)->bvecs, &(it)->iter, (n)); \
156 * Advance @it by @n bytes.
158 #define ceph_bvec_iter_advance(it, n) \
159 __ceph_bvec_iter_advance_step(it, n, 0)
162 * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
164 #define ceph_bvec_iter_advance_step(it, n, BVEC_STEP) \
165 __ceph_bvec_iter_advance_step(it, n, ({ \
167 struct bvec_iter __cur_iter; \
169 __cur_iter = (it)->iter; \
170 __cur_iter.bi_size = (n); \
171 for_each_bvec(bv, (it)->bvecs, __cur_iter, __cur_iter) \
175 #define ceph_bvec_iter_shorten(it, n) do { \
176 BUG_ON((n) > (it)->iter.bi_size); \
177 (it)->iter.bi_size = (n); \
180 struct ceph_msg_data {
181 struct list_head links; /* ceph_msg->data */
182 enum ceph_msg_data_type type;
186 struct ceph_bio_iter bio_pos;
189 #endif /* CONFIG_BLOCK */
190 struct ceph_bvec_iter bvec_pos;
192 struct page **pages; /* NOT OWNER. */
193 size_t length; /* total # bytes */
194 unsigned int alignment; /* first page */
196 struct ceph_pagelist *pagelist;
200 struct ceph_msg_data_cursor {
201 size_t total_resid; /* across all data items */
202 struct list_head *data_head; /* = &ceph_msg->data */
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 */
210 struct ceph_bio_iter bio_iter;
211 #endif /* CONFIG_BLOCK */
212 struct bvec_iter bvec_iter;
214 unsigned int page_offset; /* offset in page */
215 unsigned short page_index; /* index in array */
216 unsigned short page_count; /* pages in array */
218 struct { /* pagelist */
219 struct page *page; /* page from list */
220 size_t offset; /* bytes from list */
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).
231 struct ceph_msg_header hdr; /* header */
233 struct ceph_msg_footer footer; /* footer */
234 struct ceph_msg_footer_old old_footer; /* old format footer */
236 struct kvec front; /* unaligned blobs of message */
237 struct ceph_buffer *middle;
240 struct list_head data;
241 struct ceph_msg_data_cursor cursor;
243 struct ceph_connection *con;
244 struct list_head list_head; /* links for connection lists */
250 unsigned long ack_stamp; /* tx: when we were acked */
252 struct ceph_msgpool *pool;
255 /* ceph connection fault delay defaults, for exponential backoff */
256 #define BASE_DELAY_INTERVAL (HZ/2)
257 #define MAX_DELAY_INTERVAL (5 * 60 * HZ)
260 * A single connection with another host.
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.
266 struct ceph_connection {
269 const struct ceph_connection_operations *ops;
271 struct ceph_messenger *msgr;
275 struct ceph_entity_addr peer_addr; /* peer address */
276 struct ceph_entity_addr peer_addr_for_me;
280 const char *error_msg; /* error message, if any */
282 struct ceph_entity_name peer_name; /* peer name */
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 */
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;
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 */
300 u64 in_seq, in_seq_acked; /* last message received, acked */
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;
308 /* message out temps */
309 struct ceph_msg_header out_hdr;
310 struct ceph_msg *out_msg; /* sending message (== tail of
314 struct kvec out_kvec[8], /* sending header/footer data */
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
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 */
329 char in_tag; /* protocol control byte */
330 int in_base_pos; /* bytes read */
331 __le64 in_temp_ack; /* for reading an ack */
333 struct timespec last_keepalive_ack; /* keepalive2 ack stamp */
335 struct delayed_work work; /* send|recv work */
336 unsigned long delay; /* current delay interval */
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);
346 extern int ceph_msgr_init(void);
347 extern void ceph_msgr_exit(void);
348 extern void ceph_msgr_flush(void);
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);
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);
364 extern void ceph_msg_revoke(struct ceph_msg *msg);
365 extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
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);
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);
376 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
378 #endif /* CONFIG_BLOCK */
379 void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
380 struct ceph_bvec_iter *bvec_pos);
382 extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
385 extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
386 extern void ceph_msg_put(struct ceph_msg *msg);
388 extern void ceph_msg_dump(struct ceph_msg *msg);