1
2#ifndef __FS_CEPH_MESSENGER_H
3#define __FS_CEPH_MESSENGER_H
4
5#include <linux/bvec.h>
6#include <linux/crypto.h>
7#include <linux/kref.h>
8#include <linux/mutex.h>
9#include <linux/net.h>
10#include <linux/radix-tree.h>
11#include <linux/uio.h>
12#include <linux/workqueue.h>
13#include <net/net_namespace.h>
14
15#include <linux/ceph/types.h>
16#include <linux/ceph/buffer.h>
17
18struct ceph_msg;
19struct ceph_connection;
20
21
22
23
24struct ceph_connection_operations {
25 struct ceph_connection *(*get)(struct ceph_connection *);
26 void (*put)(struct ceph_connection *);
27
28
29 void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
30
31
32 struct ceph_auth_handshake *(*get_authorizer) (
33 struct ceph_connection *con,
34 int *proto, int force_new);
35 int (*add_authorizer_challenge)(struct ceph_connection *con,
36 void *challenge_buf,
37 int challenge_buf_len);
38 int (*verify_authorizer_reply) (struct ceph_connection *con);
39 int (*invalidate_authorizer)(struct ceph_connection *con);
40
41
42 void (*fault) (struct ceph_connection *con);
43
44
45
46 void (*peer_reset) (struct ceph_connection *con);
47
48 struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
49 struct ceph_msg_header *hdr,
50 int *skip);
51
52 void (*reencode_message) (struct ceph_msg *msg);
53
54 int (*sign_message) (struct ceph_msg *msg);
55 int (*check_message_signature) (struct ceph_msg *msg);
56
57
58 int (*get_auth_request)(struct ceph_connection *con,
59 void *buf, int *buf_len,
60 void **authorizer, int *authorizer_len);
61 int (*handle_auth_reply_more)(struct ceph_connection *con,
62 void *reply, int reply_len,
63 void *buf, int *buf_len,
64 void **authorizer, int *authorizer_len);
65 int (*handle_auth_done)(struct ceph_connection *con,
66 u64 global_id, void *reply, int reply_len,
67 u8 *session_key, int *session_key_len,
68 u8 *con_secret, int *con_secret_len);
69 int (*handle_auth_bad_method)(struct ceph_connection *con,
70 int used_proto, int result,
71 const int *allowed_protos, int proto_cnt,
72 const int *allowed_modes, int mode_cnt);
73};
74
75
76#define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
77
78struct ceph_messenger {
79 struct ceph_entity_inst inst;
80 struct ceph_entity_addr my_enc_addr;
81
82 atomic_t stopping;
83 possible_net_t net;
84
85
86
87
88
89 u32 global_seq;
90 spinlock_t global_seq_lock;
91};
92
93enum ceph_msg_data_type {
94 CEPH_MSG_DATA_NONE,
95 CEPH_MSG_DATA_PAGES,
96 CEPH_MSG_DATA_PAGELIST,
97#ifdef CONFIG_BLOCK
98 CEPH_MSG_DATA_BIO,
99#endif
100 CEPH_MSG_DATA_BVECS,
101};
102
103#ifdef CONFIG_BLOCK
104
105struct ceph_bio_iter {
106 struct bio *bio;
107 struct bvec_iter iter;
108};
109
110#define __ceph_bio_iter_advance_step(it, n, STEP) do { \
111 unsigned int __n = (n), __cur_n; \
112 \
113 while (__n) { \
114 BUG_ON(!(it)->iter.bi_size); \
115 __cur_n = min((it)->iter.bi_size, __n); \
116 (void)(STEP); \
117 bio_advance_iter((it)->bio, &(it)->iter, __cur_n); \
118 if (!(it)->iter.bi_size && (it)->bio->bi_next) { \
119 dout("__ceph_bio_iter_advance_step next bio\n"); \
120 (it)->bio = (it)->bio->bi_next; \
121 (it)->iter = (it)->bio->bi_iter; \
122 } \
123 __n -= __cur_n; \
124 } \
125} while (0)
126
127
128
129
130#define ceph_bio_iter_advance(it, n) \
131 __ceph_bio_iter_advance_step(it, n, 0)
132
133
134
135
136#define ceph_bio_iter_advance_step(it, n, BVEC_STEP) \
137 __ceph_bio_iter_advance_step(it, n, ({ \
138 struct bio_vec bv; \
139 struct bvec_iter __cur_iter; \
140 \
141 __cur_iter = (it)->iter; \
142 __cur_iter.bi_size = __cur_n; \
143 __bio_for_each_segment(bv, (it)->bio, __cur_iter, __cur_iter) \
144 (void)(BVEC_STEP); \
145 }))
146
147#endif
148
149struct ceph_bvec_iter {
150 struct bio_vec *bvecs;
151 struct bvec_iter iter;
152};
153
154#define __ceph_bvec_iter_advance_step(it, n, STEP) do { \
155 BUG_ON((n) > (it)->iter.bi_size); \
156 (void)(STEP); \
157 bvec_iter_advance((it)->bvecs, &(it)->iter, (n)); \
158} while (0)
159
160
161
162
163#define ceph_bvec_iter_advance(it, n) \
164 __ceph_bvec_iter_advance_step(it, n, 0)
165
166
167
168
169#define ceph_bvec_iter_advance_step(it, n, BVEC_STEP) \
170 __ceph_bvec_iter_advance_step(it, n, ({ \
171 struct bio_vec bv; \
172 struct bvec_iter __cur_iter; \
173 \
174 __cur_iter = (it)->iter; \
175 __cur_iter.bi_size = (n); \
176 for_each_bvec(bv, (it)->bvecs, __cur_iter, __cur_iter) \
177 (void)(BVEC_STEP); \
178 }))
179
180#define ceph_bvec_iter_shorten(it, n) do { \
181 BUG_ON((n) > (it)->iter.bi_size); \
182 (it)->iter.bi_size = (n); \
183} while (0)
184
185struct ceph_msg_data {
186 enum ceph_msg_data_type type;
187 union {
188#ifdef CONFIG_BLOCK
189 struct {
190 struct ceph_bio_iter bio_pos;
191 u32 bio_length;
192 };
193#endif
194 struct ceph_bvec_iter bvec_pos;
195 struct {
196 struct page **pages;
197 size_t length;
198 unsigned int alignment;
199 bool own_pages;
200 };
201 struct ceph_pagelist *pagelist;
202 };
203};
204
205struct ceph_msg_data_cursor {
206 size_t total_resid;
207
208 struct ceph_msg_data *data;
209 size_t resid;
210 bool last_piece;
211 bool need_crc;
212 union {
213#ifdef CONFIG_BLOCK
214 struct ceph_bio_iter bio_iter;
215#endif
216 struct bvec_iter bvec_iter;
217 struct {
218 unsigned int page_offset;
219 unsigned short page_index;
220 unsigned short page_count;
221 };
222 struct {
223 struct page *page;
224 size_t offset;
225 };
226 };
227};
228
229
230
231
232
233
234struct ceph_msg {
235 struct ceph_msg_header hdr;
236 union {
237 struct ceph_msg_footer footer;
238 struct ceph_msg_footer_old old_footer;
239 };
240 struct kvec front;
241 struct ceph_buffer *middle;
242
243 size_t data_length;
244 struct ceph_msg_data *data;
245 int num_data_items;
246 int max_data_items;
247 struct ceph_msg_data_cursor cursor;
248
249 struct ceph_connection *con;
250 struct list_head list_head;
251
252 struct kref kref;
253 bool more_to_follow;
254 bool needs_out_seq;
255 int front_alloc_len;
256
257 struct ceph_msgpool *pool;
258};
259
260
261
262
263#define CEPH_CON_S_CLOSED 1
264#define CEPH_CON_S_PREOPEN 2
265#define CEPH_CON_S_V1_BANNER 3
266#define CEPH_CON_S_V1_CONNECT_MSG 4
267#define CEPH_CON_S_V2_BANNER_PREFIX 5
268#define CEPH_CON_S_V2_BANNER_PAYLOAD 6
269#define CEPH_CON_S_V2_HELLO 7
270#define CEPH_CON_S_V2_AUTH 8
271#define CEPH_CON_S_V2_AUTH_SIGNATURE 9
272#define CEPH_CON_S_V2_SESSION_CONNECT 10
273#define CEPH_CON_S_V2_SESSION_RECONNECT 11
274#define CEPH_CON_S_OPEN 12
275#define CEPH_CON_S_STANDBY 13
276
277
278
279
280#define CEPH_CON_F_LOSSYTX 0
281
282#define CEPH_CON_F_KEEPALIVE_PENDING 1
283#define CEPH_CON_F_WRITE_PENDING 2
284#define CEPH_CON_F_SOCK_CLOSED 3
285#define CEPH_CON_F_BACKOFF 4
286
287
288
289#define BASE_DELAY_INTERVAL (HZ / 4)
290#define MAX_DELAY_INTERVAL (15 * HZ)
291
292struct ceph_connection_v1_info {
293 struct kvec out_kvec[8],
294 *out_kvec_cur;
295 int out_kvec_left;
296 int out_skip;
297 int out_kvec_bytes;
298 bool out_more;
299 bool out_msg_done;
300
301 struct ceph_auth_handshake *auth;
302 int auth_retry;
303
304
305 u8 in_banner[CEPH_BANNER_MAX_LEN];
306 struct ceph_entity_addr actual_peer_addr;
307 struct ceph_entity_addr peer_addr_for_me;
308 struct ceph_msg_connect out_connect;
309 struct ceph_msg_connect_reply in_reply;
310
311 int in_base_pos;
312
313
314 u8 in_tag;
315 struct ceph_msg_header in_hdr;
316 __le64 in_temp_ack;
317
318
319 struct ceph_msg_header out_hdr;
320 __le64 out_temp_ack;
321 struct ceph_timespec out_temp_keepalive2;
322
323
324 u32 connect_seq;
325
326 u32 peer_global_seq;
327};
328
329#define CEPH_CRC_LEN 4
330#define CEPH_GCM_KEY_LEN 16
331#define CEPH_GCM_IV_LEN sizeof(struct ceph_gcm_nonce)
332#define CEPH_GCM_BLOCK_LEN 16
333#define CEPH_GCM_TAG_LEN 16
334
335#define CEPH_PREAMBLE_LEN 32
336#define CEPH_PREAMBLE_INLINE_LEN 48
337#define CEPH_PREAMBLE_PLAIN_LEN CEPH_PREAMBLE_LEN
338#define CEPH_PREAMBLE_SECURE_LEN (CEPH_PREAMBLE_LEN + \
339 CEPH_PREAMBLE_INLINE_LEN + \
340 CEPH_GCM_TAG_LEN)
341#define CEPH_EPILOGUE_PLAIN_LEN (1 + 3 * CEPH_CRC_LEN)
342#define CEPH_EPILOGUE_SECURE_LEN (CEPH_GCM_BLOCK_LEN + CEPH_GCM_TAG_LEN)
343
344#define CEPH_FRAME_MAX_SEGMENT_COUNT 4
345
346struct ceph_frame_desc {
347 int fd_tag;
348 int fd_seg_cnt;
349 int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT];
350 int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT];
351};
352
353struct ceph_gcm_nonce {
354 __le32 fixed;
355 __le64 counter __packed;
356};
357
358struct ceph_connection_v2_info {
359 struct iov_iter in_iter;
360 struct kvec in_kvecs[5];
361 struct bio_vec in_bvec;
362 int in_kvec_cnt;
363 int in_state;
364
365 struct iov_iter out_iter;
366 struct kvec out_kvecs[8];
367 struct bio_vec out_bvec;
368
369 int out_kvec_cnt;
370 int out_state;
371
372 int out_zero;
373 bool out_iter_sendpage;
374
375 struct ceph_frame_desc in_desc;
376 struct ceph_msg_data_cursor in_cursor;
377 struct ceph_msg_data_cursor out_cursor;
378
379 struct crypto_shash *hmac_tfm;
380 struct crypto_aead *gcm_tfm;
381 struct aead_request *gcm_req;
382 struct crypto_wait gcm_wait;
383 struct ceph_gcm_nonce in_gcm_nonce;
384 struct ceph_gcm_nonce out_gcm_nonce;
385
386 struct page **out_enc_pages;
387 int out_enc_page_cnt;
388 int out_enc_resid;
389 int out_enc_i;
390
391 int con_mode;
392
393 void *conn_bufs[16];
394 int conn_buf_cnt;
395
396 struct kvec in_sign_kvecs[8];
397 struct kvec out_sign_kvecs[8];
398 int in_sign_kvec_cnt;
399 int out_sign_kvec_cnt;
400
401 u64 client_cookie;
402 u64 server_cookie;
403 u64 global_seq;
404 u64 connect_seq;
405 u64 peer_global_seq;
406
407 u8 in_buf[CEPH_PREAMBLE_SECURE_LEN];
408 u8 out_buf[CEPH_PREAMBLE_SECURE_LEN];
409 struct {
410 u8 late_status;
411 union {
412 struct {
413 u32 front_crc;
414 u32 middle_crc;
415 u32 data_crc;
416 } __packed;
417 u8 pad[CEPH_GCM_BLOCK_LEN - 1];
418 };
419 } out_epil;
420};
421
422
423
424
425
426
427
428
429struct ceph_connection {
430 void *private;
431
432 const struct ceph_connection_operations *ops;
433
434 struct ceph_messenger *msgr;
435
436 int state;
437 atomic_t sock_state;
438 struct socket *sock;
439
440 unsigned long flags;
441 const char *error_msg;
442
443 struct ceph_entity_name peer_name;
444 struct ceph_entity_addr peer_addr;
445 u64 peer_features;
446
447 struct mutex mutex;
448
449
450 struct list_head out_queue;
451 struct list_head out_sent;
452 u64 out_seq;
453
454 u64 in_seq, in_seq_acked;
455
456 struct ceph_msg *in_msg;
457 struct ceph_msg *out_msg;
458
459
460 u32 in_front_crc, in_middle_crc, in_data_crc;
461
462 struct timespec64 last_keepalive_ack;
463
464 struct delayed_work work;
465 unsigned long delay;
466
467 union {
468 struct ceph_connection_v1_info v1;
469 struct ceph_connection_v2_info v2;
470 };
471};
472
473extern struct page *ceph_zero_page;
474
475void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag);
476void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag);
477bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag);
478bool ceph_con_flag_test_and_clear(struct ceph_connection *con,
479 unsigned long con_flag);
480bool ceph_con_flag_test_and_set(struct ceph_connection *con,
481 unsigned long con_flag);
482
483void ceph_encode_my_addr(struct ceph_messenger *msgr);
484
485int ceph_tcp_connect(struct ceph_connection *con);
486int ceph_con_close_socket(struct ceph_connection *con);
487void ceph_con_reset_session(struct ceph_connection *con);
488
489u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt);
490void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq);
491void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq);
492
493void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
494 struct ceph_msg *msg, size_t length);
495struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
496 size_t *page_offset, size_t *length,
497 bool *last_piece);
498void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes);
499
500u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset,
501 unsigned int length);
502
503bool ceph_addr_is_blank(const struct ceph_entity_addr *addr);
504int ceph_addr_port(const struct ceph_entity_addr *addr);
505void ceph_addr_set_port(struct ceph_entity_addr *addr, int p);
506
507void ceph_con_process_message(struct ceph_connection *con);
508int ceph_con_in_msg_alloc(struct ceph_connection *con,
509 struct ceph_msg_header *hdr, int *skip);
510void ceph_con_get_out_msg(struct ceph_connection *con);
511
512
513int ceph_con_v1_try_read(struct ceph_connection *con);
514int ceph_con_v1_try_write(struct ceph_connection *con);
515void ceph_con_v1_revoke(struct ceph_connection *con);
516void ceph_con_v1_revoke_incoming(struct ceph_connection *con);
517bool ceph_con_v1_opened(struct ceph_connection *con);
518void ceph_con_v1_reset_session(struct ceph_connection *con);
519void ceph_con_v1_reset_protocol(struct ceph_connection *con);
520
521
522int ceph_con_v2_try_read(struct ceph_connection *con);
523int ceph_con_v2_try_write(struct ceph_connection *con);
524void ceph_con_v2_revoke(struct ceph_connection *con);
525void ceph_con_v2_revoke_incoming(struct ceph_connection *con);
526bool ceph_con_v2_opened(struct ceph_connection *con);
527void ceph_con_v2_reset_session(struct ceph_connection *con);
528void ceph_con_v2_reset_protocol(struct ceph_connection *con);
529
530
531extern const char *ceph_pr_addr(const struct ceph_entity_addr *addr);
532
533extern int ceph_parse_ips(const char *c, const char *end,
534 struct ceph_entity_addr *addr,
535 int max_count, int *count);
536
537extern int ceph_msgr_init(void);
538extern void ceph_msgr_exit(void);
539extern void ceph_msgr_flush(void);
540
541extern void ceph_messenger_init(struct ceph_messenger *msgr,
542 struct ceph_entity_addr *myaddr);
543extern void ceph_messenger_fini(struct ceph_messenger *msgr);
544extern void ceph_messenger_reset_nonce(struct ceph_messenger *msgr);
545
546extern void ceph_con_init(struct ceph_connection *con, void *private,
547 const struct ceph_connection_operations *ops,
548 struct ceph_messenger *msgr);
549extern void ceph_con_open(struct ceph_connection *con,
550 __u8 entity_type, __u64 entity_num,
551 struct ceph_entity_addr *addr);
552extern bool ceph_con_opened(struct ceph_connection *con);
553extern void ceph_con_close(struct ceph_connection *con);
554extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
555
556extern void ceph_msg_revoke(struct ceph_msg *msg);
557extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
558
559extern void ceph_con_keepalive(struct ceph_connection *con);
560extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
561 unsigned long interval);
562
563void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
564 size_t length, size_t alignment, bool own_pages);
565extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
566 struct ceph_pagelist *pagelist);
567#ifdef CONFIG_BLOCK
568void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
569 u32 length);
570#endif
571void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
572 struct ceph_bvec_iter *bvec_pos);
573
574struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
575 gfp_t flags, bool can_fail);
576extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
577 bool can_fail);
578
579extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
580extern void ceph_msg_put(struct ceph_msg *msg);
581
582extern void ceph_msg_dump(struct ceph_msg *msg);
583
584#endif
585