libceph, ceph: implement msgr2.1 protocol (crc and secure modes)
[linux-2.6-microblaze.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/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
18 struct ceph_msg;
19 struct ceph_connection;
20
21 /*
22  * Ceph defines these callbacks for handling connection events.
23  */
24 struct ceph_connection_operations {
25         struct ceph_connection *(*get)(struct ceph_connection *);
26         void (*put)(struct ceph_connection *);
27
28         /* handle an incoming message. */
29         void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
30
31         /* authorize an outgoing connection */
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         /* there was some error on the socket (disconnect, whatever) */
42         void (*fault) (struct ceph_connection *con);
43
44         /* a remote host as terminated a message exchange session, and messages
45          * we sent (or they tried to send us) may be lost. */
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         /* msgr2 authentication exchange */
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 /* use format string %s%lld */
76 #define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
77
78 struct ceph_messenger {
79         struct ceph_entity_inst inst;    /* my name+address */
80         struct ceph_entity_addr my_enc_addr;
81
82         atomic_t stopping;
83         possible_net_t net;
84
85         /*
86          * the global_seq counts connections i (attempt to) initiate
87          * in order to disambiguate certain connect race conditions.
88          */
89         u32 global_seq;
90         spinlock_t global_seq_lock;
91 };
92
93 enum ceph_msg_data_type {
94         CEPH_MSG_DATA_NONE,     /* message contains no data payload */
95         CEPH_MSG_DATA_PAGES,    /* data source/destination is a page array */
96         CEPH_MSG_DATA_PAGELIST, /* data source/destination is a pagelist */
97 #ifdef CONFIG_BLOCK
98         CEPH_MSG_DATA_BIO,      /* data source/destination is a bio list */
99 #endif /* CONFIG_BLOCK */
100         CEPH_MSG_DATA_BVECS,    /* data source/destination is a bio_vec array */
101 };
102
103 #ifdef CONFIG_BLOCK
104
105 struct 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  * Advance @it by @n bytes.
129  */
130 #define ceph_bio_iter_advance(it, n)                                          \
131         __ceph_bio_iter_advance_step(it, n, 0)
132
133 /*
134  * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
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 /* CONFIG_BLOCK */
148
149 struct 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  * Advance @it by @n bytes.
162  */
163 #define ceph_bvec_iter_advance(it, n)                                         \
164         __ceph_bvec_iter_advance_step(it, n, 0)
165
166 /*
167  * Advance @it by @n bytes, executing BVEC_STEP for each bio_vec.
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
185 struct 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 /* CONFIG_BLOCK */
194                 struct ceph_bvec_iter   bvec_pos;
195                 struct {
196                         struct page     **pages;
197                         size_t          length;         /* total # bytes */
198                         unsigned int    alignment;      /* first page */
199                         bool            own_pages;
200                 };
201                 struct ceph_pagelist    *pagelist;
202         };
203 };
204
205 struct ceph_msg_data_cursor {
206         size_t                  total_resid;    /* across all data items */
207
208         struct ceph_msg_data    *data;          /* current data item */
209         size_t                  resid;          /* bytes not yet consumed */
210         bool                    last_piece;     /* current is last piece */
211         bool                    need_crc;       /* crc update needed */
212         union {
213 #ifdef CONFIG_BLOCK
214                 struct ceph_bio_iter    bio_iter;
215 #endif /* CONFIG_BLOCK */
216                 struct bvec_iter        bvec_iter;
217                 struct {                                /* pages */
218                         unsigned int    page_offset;    /* offset in page */
219                         unsigned short  page_index;     /* index in array */
220                         unsigned short  page_count;     /* pages in array */
221                 };
222                 struct {                                /* pagelist */
223                         struct page     *page;          /* page from list */
224                         size_t          offset;         /* bytes from list */
225                 };
226         };
227 };
228
229 /*
230  * a single message.  it contains a header (src, dest, message type, etc.),
231  * footer (crc values, mainly), a "front" message body, and possibly a
232  * data payload (stored in some number of pages).
233  */
234 struct ceph_msg {
235         struct ceph_msg_header hdr;     /* header */
236         union {
237                 struct ceph_msg_footer footer;          /* footer */
238                 struct ceph_msg_footer_old old_footer;  /* old format footer */
239         };
240         struct kvec front;              /* unaligned blobs of message */
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;     /* links for connection lists */
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  * connection states
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  * ceph_connection flag bits
279  */
280 #define CEPH_CON_F_LOSSYTX              0  /* we can close channel or drop
281                                               messages on errors */
282 #define CEPH_CON_F_KEEPALIVE_PENDING    1  /* we need to send a keepalive */
283 #define CEPH_CON_F_WRITE_PENDING        2  /* we have data ready to send */
284 #define CEPH_CON_F_SOCK_CLOSED          3  /* socket state changed to closed */
285 #define CEPH_CON_F_BACKOFF              4  /* need to retry queuing delayed
286                                               work */
287
288 /* ceph connection fault delay defaults, for exponential backoff */
289 #define BASE_DELAY_INTERVAL     (HZ / 4)
290 #define MAX_DELAY_INTERVAL      (15 * HZ)
291
292 struct ceph_connection_v1_info {
293         struct kvec out_kvec[8],         /* sending header/footer data */
294                 *out_kvec_cur;
295         int out_kvec_left;   /* kvec's left in out_kvec */
296         int out_skip;        /* skip this many bytes */
297         int out_kvec_bytes;  /* total bytes left */
298         bool out_more;       /* there is more data after the kvecs */
299         bool out_msg_done;
300
301         struct ceph_auth_handshake *auth;
302         int auth_retry;       /* true if we need a newer authorizer */
303
304         /* connection negotiation temps */
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;     /* bytes read */
312
313         /* message in temps */
314         u8 in_tag;           /* protocol control byte */
315         struct ceph_msg_header in_hdr;
316         __le64 in_temp_ack;  /* for reading an ack */
317
318         /* message out temps */
319         struct ceph_msg_header out_hdr;
320         __le64 out_temp_ack;  /* for writing an ack */
321         struct ceph_timespec out_temp_keepalive2;  /* for writing keepalive2
322                                                       stamp */
323
324         u32 connect_seq;      /* identify the most recent connection
325                                  attempt for this session */
326         u32 peer_global_seq;  /* peer's global seq for this connection */
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
346 struct ceph_frame_desc {
347         int fd_tag;  /* FRAME_TAG_* */
348         int fd_seg_cnt;
349         int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT];  /* logical */
350         int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT];
351 };
352
353 struct ceph_gcm_nonce {
354         __le32 fixed;
355         __le64 counter __packed;
356 };
357
358 struct ceph_connection_v2_info {
359         struct iov_iter in_iter;
360         struct kvec in_kvecs[5];  /* recvmsg */
361         struct bio_vec in_bvec;  /* recvmsg (in_cursor) */
362         int in_kvec_cnt;
363         int in_state;  /* IN_S_* */
364
365         struct iov_iter out_iter;
366         struct kvec out_kvecs[8];  /* sendmsg */
367         struct bio_vec out_bvec;  /* sendpage (out_cursor, out_zero),
368                                      sendmsg (out_enc_pages) */
369         int out_kvec_cnt;
370         int out_state;  /* OUT_S_* */
371
372         int out_zero;  /* # of zero bytes to send */
373         bool out_iter_sendpage;  /* use sendpage if possible */
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;  /* post-auth signature */
380         struct crypto_aead *gcm_tfm;  /* on-wire encryption */
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;  /* CEPH_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;  /* FRAME_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  * A single connection with another host.
424  *
425  * We maintain a queue of outgoing messages, and some session state to
426  * ensure that we can preserve the lossless, ordered delivery of
427  * messages in the case of a TCP disconnect.
428  */
429 struct ceph_connection {
430         void *private;
431
432         const struct ceph_connection_operations *ops;
433
434         struct ceph_messenger *msgr;
435
436         int state;  /* CEPH_CON_S_* */
437         atomic_t sock_state;
438         struct socket *sock;
439
440         unsigned long flags;  /* CEPH_CON_F_* */
441         const char *error_msg;  /* error message, if any */
442
443         struct ceph_entity_name peer_name; /* peer name */
444         struct ceph_entity_addr peer_addr; /* peer address */
445         u64 peer_features;
446
447         struct mutex mutex;
448
449         /* out queue */
450         struct list_head out_queue;
451         struct list_head out_sent;   /* sending or sent but unacked */
452         u64 out_seq;                 /* last message queued for send */
453
454         u64 in_seq, in_seq_acked;  /* last message received, acked */
455
456         struct ceph_msg *in_msg;
457         struct ceph_msg *out_msg;        /* sending message (== tail of
458                                             out_sent) */
459
460         u32 in_front_crc, in_middle_crc, in_data_crc;  /* calculated crc */
461
462         struct timespec64 last_keepalive_ack; /* keepalive2 ack stamp */
463
464         struct delayed_work work;           /* send|recv work */
465         unsigned long       delay;          /* current delay interval */
466
467         union {
468                 struct ceph_connection_v1_info v1;
469                 struct ceph_connection_v2_info v2;
470         };
471 };
472
473 extern struct page *ceph_zero_page;
474
475 void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag);
476 void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag);
477 bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag);
478 bool ceph_con_flag_test_and_clear(struct ceph_connection *con,
479                                   unsigned long con_flag);
480 bool ceph_con_flag_test_and_set(struct ceph_connection *con,
481                                 unsigned long con_flag);
482
483 void ceph_encode_my_addr(struct ceph_messenger *msgr);
484
485 int ceph_tcp_connect(struct ceph_connection *con);
486 int ceph_con_close_socket(struct ceph_connection *con);
487 void ceph_con_reset_session(struct ceph_connection *con);
488
489 u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt);
490 void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq);
491 void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq);
492
493 void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
494                                struct ceph_msg *msg, size_t length);
495 struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
496                                 size_t *page_offset, size_t *length,
497                                 bool *last_piece);
498 void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes);
499
500 u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset,
501                      unsigned int length);
502
503 bool ceph_addr_is_blank(const struct ceph_entity_addr *addr);
504 int ceph_addr_port(const struct ceph_entity_addr *addr);
505 void ceph_addr_set_port(struct ceph_entity_addr *addr, int p);
506
507 void ceph_con_process_message(struct ceph_connection *con);
508 int ceph_con_in_msg_alloc(struct ceph_connection *con,
509                           struct ceph_msg_header *hdr, int *skip);
510 void ceph_con_get_out_msg(struct ceph_connection *con);
511
512 /* messenger_v1.c */
513 int ceph_con_v1_try_read(struct ceph_connection *con);
514 int ceph_con_v1_try_write(struct ceph_connection *con);
515 void ceph_con_v1_revoke(struct ceph_connection *con);
516 void ceph_con_v1_revoke_incoming(struct ceph_connection *con);
517 bool ceph_con_v1_opened(struct ceph_connection *con);
518 void ceph_con_v1_reset_session(struct ceph_connection *con);
519 void ceph_con_v1_reset_protocol(struct ceph_connection *con);
520
521 /* messenger_v2.c */
522 int ceph_con_v2_try_read(struct ceph_connection *con);
523 int ceph_con_v2_try_write(struct ceph_connection *con);
524 void ceph_con_v2_revoke(struct ceph_connection *con);
525 void ceph_con_v2_revoke_incoming(struct ceph_connection *con);
526 bool ceph_con_v2_opened(struct ceph_connection *con);
527 void ceph_con_v2_reset_session(struct ceph_connection *con);
528 void ceph_con_v2_reset_protocol(struct ceph_connection *con);
529
530
531 extern const char *ceph_pr_addr(const struct ceph_entity_addr *addr);
532
533 extern int ceph_parse_ips(const char *c, const char *end,
534                           struct ceph_entity_addr *addr,
535                           int max_count, int *count);
536
537 extern int ceph_msgr_init(void);
538 extern void ceph_msgr_exit(void);
539 extern void ceph_msgr_flush(void);
540
541 extern void ceph_messenger_init(struct ceph_messenger *msgr,
542                                 struct ceph_entity_addr *myaddr);
543 extern void ceph_messenger_fini(struct ceph_messenger *msgr);
544 extern void ceph_messenger_reset_nonce(struct ceph_messenger *msgr);
545
546 extern void ceph_con_init(struct ceph_connection *con, void *private,
547                         const struct ceph_connection_operations *ops,
548                         struct ceph_messenger *msgr);
549 extern void ceph_con_open(struct ceph_connection *con,
550                           __u8 entity_type, __u64 entity_num,
551                           struct ceph_entity_addr *addr);
552 extern bool ceph_con_opened(struct ceph_connection *con);
553 extern void ceph_con_close(struct ceph_connection *con);
554 extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
555
556 extern void ceph_msg_revoke(struct ceph_msg *msg);
557 extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
558
559 extern void ceph_con_keepalive(struct ceph_connection *con);
560 extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
561                                        unsigned long interval);
562
563 void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
564                              size_t length, size_t alignment, bool own_pages);
565 extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
566                                 struct ceph_pagelist *pagelist);
567 #ifdef CONFIG_BLOCK
568 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
569                            u32 length);
570 #endif /* CONFIG_BLOCK */
571 void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
572                              struct ceph_bvec_iter *bvec_pos);
573
574 struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
575                                gfp_t flags, bool can_fail);
576 extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
577                                      bool can_fail);
578
579 extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
580 extern void ceph_msg_put(struct ceph_msg *msg);
581
582 extern void ceph_msg_dump(struct ceph_msg *msg);
583
584 #endif