GNU Linux-libre 4.14.302-gnu1
[releases.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%d */
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 };
83
84 static __inline__ bool ceph_msg_data_type_valid(enum ceph_msg_data_type type)
85 {
86         switch (type) {
87         case CEPH_MSG_DATA_NONE:
88         case CEPH_MSG_DATA_PAGES:
89         case CEPH_MSG_DATA_PAGELIST:
90 #ifdef CONFIG_BLOCK
91         case CEPH_MSG_DATA_BIO:
92 #endif /* CONFIG_BLOCK */
93                 return true;
94         default:
95                 return false;
96         }
97 }
98
99 struct ceph_msg_data {
100         struct list_head                links;  /* ceph_msg->data */
101         enum ceph_msg_data_type         type;
102         union {
103 #ifdef CONFIG_BLOCK
104                 struct {
105                         struct bio      *bio;
106                         size_t          bio_length;
107                 };
108 #endif /* CONFIG_BLOCK */
109                 struct {
110                         struct page     **pages;        /* NOT OWNER. */
111                         size_t          length;         /* total # bytes */
112                         unsigned int    alignment;      /* first page */
113                 };
114                 struct ceph_pagelist    *pagelist;
115         };
116 };
117
118 struct ceph_msg_data_cursor {
119         size_t                  total_resid;    /* across all data items */
120         struct list_head        *data_head;     /* = &ceph_msg->data */
121
122         struct ceph_msg_data    *data;          /* current data item */
123         size_t                  resid;          /* bytes not yet consumed */
124         bool                    last_piece;     /* current is last piece */
125         bool                    need_crc;       /* crc update needed */
126         union {
127 #ifdef CONFIG_BLOCK
128                 struct {                                /* bio */
129                         struct bio      *bio;           /* bio from list */
130                         struct bvec_iter bvec_iter;
131                 };
132 #endif /* CONFIG_BLOCK */
133                 struct {                                /* pages */
134                         unsigned int    page_offset;    /* offset in page */
135                         unsigned short  page_index;     /* index in array */
136                         unsigned short  page_count;     /* pages in array */
137                 };
138                 struct {                                /* pagelist */
139                         struct page     *page;          /* page from list */
140                         size_t          offset;         /* bytes from list */
141                 };
142         };
143 };
144
145 /*
146  * a single message.  it contains a header (src, dest, message type, etc.),
147  * footer (crc values, mainly), a "front" message body, and possibly a
148  * data payload (stored in some number of pages).
149  */
150 struct ceph_msg {
151         struct ceph_msg_header hdr;     /* header */
152         union {
153                 struct ceph_msg_footer footer;          /* footer */
154                 struct ceph_msg_footer_old old_footer;  /* old format footer */
155         };
156         struct kvec front;              /* unaligned blobs of message */
157         struct ceph_buffer *middle;
158
159         size_t                          data_length;
160         struct list_head                data;
161         struct ceph_msg_data_cursor     cursor;
162
163         struct ceph_connection *con;
164         struct list_head list_head;     /* links for connection lists */
165
166         struct kref kref;
167         bool more_to_follow;
168         bool needs_out_seq;
169         int front_alloc_len;
170         unsigned long ack_stamp;        /* tx: when we were acked */
171
172         struct ceph_msgpool *pool;
173 };
174
175 /* ceph connection fault delay defaults, for exponential backoff */
176 #define BASE_DELAY_INTERVAL     (HZ/2)
177 #define MAX_DELAY_INTERVAL      (5 * 60 * HZ)
178
179 /*
180  * A single connection with another host.
181  *
182  * We maintain a queue of outgoing messages, and some session state to
183  * ensure that we can preserve the lossless, ordered delivery of
184  * messages in the case of a TCP disconnect.
185  */
186 struct ceph_connection {
187         void *private;
188
189         const struct ceph_connection_operations *ops;
190
191         struct ceph_messenger *msgr;
192
193         atomic_t sock_state;
194         struct socket *sock;
195         struct ceph_entity_addr peer_addr; /* peer address */
196         struct ceph_entity_addr peer_addr_for_me;
197
198         unsigned long flags;
199         unsigned long state;
200         const char *error_msg;  /* error message, if any */
201
202         struct ceph_entity_name peer_name; /* peer name */
203
204         u64 peer_features;
205         u32 connect_seq;      /* identify the most recent connection
206                                  attempt for this connection, client */
207         u32 peer_global_seq;  /* peer's global seq for this connection */
208
209         struct ceph_auth_handshake *auth;
210         int auth_retry;       /* true if we need a newer authorizer */
211
212         struct mutex mutex;
213
214         /* out queue */
215         struct list_head out_queue;
216         struct list_head out_sent;   /* sending or sent but unacked */
217         u64 out_seq;                 /* last message queued for send */
218
219         u64 in_seq, in_seq_acked;  /* last message received, acked */
220
221         /* connection negotiation temps */
222         char in_banner[CEPH_BANNER_MAX_LEN];
223         struct ceph_msg_connect out_connect;
224         struct ceph_msg_connect_reply in_reply;
225         struct ceph_entity_addr actual_peer_addr;
226
227         /* message out temps */
228         struct ceph_msg_header out_hdr;
229         struct ceph_msg *out_msg;        /* sending message (== tail of
230                                             out_sent) */
231         bool out_msg_done;
232
233         struct kvec out_kvec[8],         /* sending header/footer data */
234                 *out_kvec_cur;
235         int out_kvec_left;   /* kvec's left in out_kvec */
236         int out_skip;        /* skip this many bytes */
237         int out_kvec_bytes;  /* total bytes left */
238         int out_more;        /* there is more data after the kvecs */
239         __le64 out_temp_ack; /* for writing an ack */
240         struct ceph_timespec out_temp_keepalive2; /* for writing keepalive2
241                                                      stamp */
242
243         /* message in temps */
244         struct ceph_msg_header in_hdr;
245         struct ceph_msg *in_msg;
246         u32 in_front_crc, in_middle_crc, in_data_crc;  /* calculated crc */
247
248         char in_tag;         /* protocol control byte */
249         int in_base_pos;     /* bytes read */
250         __le64 in_temp_ack;  /* for reading an ack */
251
252         struct timespec last_keepalive_ack; /* keepalive2 ack stamp */
253
254         struct delayed_work work;           /* send|recv work */
255         unsigned long       delay;          /* current delay interval */
256 };
257
258
259 extern const char *ceph_pr_addr(const struct sockaddr_storage *ss);
260 extern int ceph_parse_ips(const char *c, const char *end,
261                           struct ceph_entity_addr *addr,
262                           int max_count, int *count);
263
264
265 extern int ceph_msgr_init(void);
266 extern void ceph_msgr_exit(void);
267 extern void ceph_msgr_flush(void);
268
269 extern void ceph_messenger_init(struct ceph_messenger *msgr,
270                                 struct ceph_entity_addr *myaddr);
271 extern void ceph_messenger_fini(struct ceph_messenger *msgr);
272
273 extern void ceph_con_init(struct ceph_connection *con, void *private,
274                         const struct ceph_connection_operations *ops,
275                         struct ceph_messenger *msgr);
276 extern void ceph_con_open(struct ceph_connection *con,
277                           __u8 entity_type, __u64 entity_num,
278                           struct ceph_entity_addr *addr);
279 extern bool ceph_con_opened(struct ceph_connection *con);
280 extern void ceph_con_close(struct ceph_connection *con);
281 extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
282
283 extern void ceph_msg_revoke(struct ceph_msg *msg);
284 extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
285
286 extern void ceph_con_keepalive(struct ceph_connection *con);
287 extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
288                                        unsigned long interval);
289
290 extern void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
291                                 size_t length, size_t alignment);
292 extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
293                                 struct ceph_pagelist *pagelist);
294 #ifdef CONFIG_BLOCK
295 extern void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
296                                 size_t length);
297 #endif /* CONFIG_BLOCK */
298
299 extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
300                                      bool can_fail);
301
302 extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
303 extern void ceph_msg_put(struct ceph_msg *msg);
304
305 extern void ceph_msg_dump(struct ceph_msg *msg);
306
307 #endif