blob: 8dbd7879fdc6b74719d9cb65b85d8c60f589bde6 [file] [log] [blame]
Sage Weil31b80062009-10-06 11:31:13 -07001#ifndef __FS_CEPH_MESSENGER_H
2#define __FS_CEPH_MESSENGER_H
3
Kent Overstreetf38a5182013-08-07 14:30:24 -07004#include <linux/blk_types.h>
Sage Weilc2e552e2009-12-07 15:55:05 -08005#include <linux/kref.h>
Sage Weil31b80062009-10-06 11:31:13 -07006#include <linux/mutex.h>
7#include <linux/net.h>
8#include <linux/radix-tree.h>
9#include <linux/uio.h>
Sage Weil31b80062009-10-06 11:31:13 -070010#include <linux/workqueue.h>
Ilya Dryomov757856d2015-06-25 17:47:45 +030011#include <net/net_namespace.h>
Sage Weil31b80062009-10-06 11:31:13 -070012
David Howellsa1ce3922012-10-02 18:01:25 +010013#include <linux/ceph/types.h>
14#include <linux/ceph/buffer.h>
Sage Weil31b80062009-10-06 11:31:13 -070015
16struct ceph_msg;
17struct ceph_connection;
18
Sage Weil31b80062009-10-06 11:31:13 -070019/*
20 * Ceph defines these callbacks for handling connection events.
21 */
22struct ceph_connection_operations {
23 struct ceph_connection *(*get)(struct ceph_connection *);
24 void (*put)(struct ceph_connection *);
25
26 /* handle an incoming message. */
27 void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
28
Sage Weil4e7a5dc2009-11-18 16:19:57 -080029 /* authorize an outgoing connection */
Alex Eldera3530df2012-05-16 15:16:39 -050030 struct ceph_auth_handshake *(*get_authorizer) (
31 struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -050032 int *proto, int force_new);
Sage Weil4e7a5dc2009-11-18 16:19:57 -080033 int (*verify_authorizer_reply) (struct ceph_connection *con, int len);
Sage Weil9bd2e6f2010-02-02 16:21:06 -080034 int (*invalidate_authorizer)(struct ceph_connection *con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -080035
Sage Weil31b80062009-10-06 11:31:13 -070036 /* there was some error on the socket (disconnect, whatever) */
37 void (*fault) (struct ceph_connection *con);
38
39 /* a remote host as terminated a message exchange session, and messages
40 * we sent (or they tried to send us) may be lost. */
41 void (*peer_reset) (struct ceph_connection *con);
42
43 struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -080044 struct ceph_msg_header *hdr,
45 int *skip);
Yan, Zheng33d07332014-11-04 16:33:37 +080046
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +010047 int (*sign_message) (struct ceph_msg *msg);
48 int (*check_message_signature) (struct ceph_msg *msg);
Sage Weil31b80062009-10-06 11:31:13 -070049};
50
Sage Weil31b80062009-10-06 11:31:13 -070051/* use format string %s%d */
Sage Weilca9d93a2010-05-12 14:48:20 -070052#define ENTITY_NAME(n) ceph_entity_type_name((n).type), le64_to_cpu((n).num)
Sage Weil31b80062009-10-06 11:31:13 -070053
54struct ceph_messenger {
55 struct ceph_entity_inst inst; /* my name+address */
Sage Weil63f2d212009-11-03 15:17:56 -080056 struct ceph_entity_addr my_enc_addr;
Sage Weil31b80062009-10-06 11:31:13 -070057
Guanjun Hea2a32582012-07-08 19:50:33 -070058 atomic_t stopping;
Ilya Dryomov757856d2015-06-25 17:47:45 +030059 possible_net_t net;
Sage Weil31b80062009-10-06 11:31:13 -070060
61 /*
62 * the global_seq counts connections i (attempt to) initiate
63 * in order to disambiguate certain connect race conditions.
64 */
65 u32 global_seq;
66 spinlock_t global_seq_lock;
67};
68
Alex Elder43794502013-03-01 18:00:16 -060069enum ceph_msg_data_type {
70 CEPH_MSG_DATA_NONE, /* message contains no data payload */
71 CEPH_MSG_DATA_PAGES, /* data source/destination is a page array */
72 CEPH_MSG_DATA_PAGELIST, /* data source/destination is a pagelist */
73#ifdef CONFIG_BLOCK
74 CEPH_MSG_DATA_BIO, /* data source/destination is a bio list */
75#endif /* CONFIG_BLOCK */
76};
77
78static __inline__ bool ceph_msg_data_type_valid(enum ceph_msg_data_type type)
79{
80 switch (type) {
81 case CEPH_MSG_DATA_NONE:
82 case CEPH_MSG_DATA_PAGES:
83 case CEPH_MSG_DATA_PAGELIST:
84#ifdef CONFIG_BLOCK
85 case CEPH_MSG_DATA_BIO:
86#endif /* CONFIG_BLOCK */
87 return true;
88 default:
89 return false;
90 }
91}
92
Alex Elder36153ec2013-03-14 14:09:06 -050093struct ceph_msg_data {
Alex Elder5240d9f2013-03-14 14:09:06 -050094 struct list_head links; /* ceph_msg->data */
Alex Elder36153ec2013-03-14 14:09:06 -050095 enum ceph_msg_data_type type;
96 union {
97#ifdef CONFIG_BLOCK
98 struct {
99 struct bio *bio;
100 size_t bio_length;
101 };
102#endif /* CONFIG_BLOCK */
103 struct {
104 struct page **pages; /* NOT OWNER. */
105 size_t length; /* total # bytes */
106 unsigned int alignment; /* first page */
107 };
108 struct ceph_pagelist *pagelist;
109 };
Alex Elder36153ec2013-03-14 14:09:06 -0500110};
111
Alex Elderfe38a2b2013-03-06 23:39:39 -0600112struct ceph_msg_data_cursor {
Alex Elderca8b3a62013-04-05 14:46:01 -0500113 size_t total_resid; /* across all data items */
114 struct list_head *data_head; /* = &ceph_msg->data */
115
116 struct ceph_msg_data *data; /* current data item */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500117 size_t resid; /* bytes not yet consumed */
118 bool last_piece; /* current is last piece */
119 bool need_crc; /* crc update needed */
Alex Elderdd236fc2013-03-06 23:39:39 -0600120 union {
Alex Elder6aaa4512013-03-06 23:39:39 -0600121#ifdef CONFIG_BLOCK
122 struct { /* bio */
123 struct bio *bio; /* bio from list */
Kent Overstreetf38a5182013-08-07 14:30:24 -0700124 struct bvec_iter bvec_iter;
Alex Elder6aaa4512013-03-06 23:39:39 -0600125 };
126#endif /* CONFIG_BLOCK */
Alex Eldere766d7b2013-03-07 15:38:28 -0600127 struct { /* pages */
Alex Eldere766d7b2013-03-07 15:38:28 -0600128 unsigned int page_offset; /* offset in page */
129 unsigned short page_index; /* index in array */
130 unsigned short page_count; /* pages in array */
131 };
Alex Elderdd236fc2013-03-06 23:39:39 -0600132 struct { /* pagelist */
133 struct page *page; /* page from list */
134 size_t offset; /* bytes from list */
135 };
136 };
Alex Elderfe38a2b2013-03-06 23:39:39 -0600137};
138
Sage Weil31b80062009-10-06 11:31:13 -0700139/*
140 * a single message. it contains a header (src, dest, message type, etc.),
141 * footer (crc values, mainly), a "front" message body, and possibly a
142 * data payload (stored in some number of pages).
143 */
144struct ceph_msg {
145 struct ceph_msg_header hdr; /* header */
Yan, Zheng33d07332014-11-04 16:33:37 +0800146 union {
147 struct ceph_msg_footer footer; /* footer */
148 struct ceph_msg_footer_old old_footer; /* old format footer */
149 };
Sage Weil31b80062009-10-06 11:31:13 -0700150 struct kvec front; /* unaligned blobs of message */
151 struct ceph_buffer *middle;
Alex Elder02afca62013-02-14 12:16:43 -0600152
Alex Elder36153ec2013-03-14 14:09:06 -0500153 size_t data_length;
Alex Elder5240d9f2013-03-14 14:09:06 -0500154 struct list_head data;
Alex Elder36153ec2013-03-14 14:09:06 -0500155 struct ceph_msg_data_cursor cursor;
Alex Elder02afca62013-02-14 12:16:43 -0600156
157 struct ceph_connection *con;
158 struct list_head list_head; /* links for connection lists */
159
160 struct kref kref;
Sage Weil31b80062009-10-06 11:31:13 -0700161 bool more_to_follow;
Sage Weile84346b2010-05-11 21:20:38 -0700162 bool needs_out_seq;
Ilya Dryomov3cea4c32014-01-09 20:08:21 +0200163 int front_alloc_len;
Sage Weil4cf9d542011-07-26 11:27:24 -0700164 unsigned long ack_stamp; /* tx: when we were acked */
Sage Weil31b80062009-10-06 11:31:13 -0700165
166 struct ceph_msgpool *pool;
167};
168
Sage Weil31b80062009-10-06 11:31:13 -0700169/* ceph connection fault delay defaults, for exponential backoff */
170#define BASE_DELAY_INTERVAL (HZ/2)
171#define MAX_DELAY_INTERVAL (5 * 60 * HZ)
172
173/*
Sage Weil31b80062009-10-06 11:31:13 -0700174 * A single connection with another host.
175 *
176 * We maintain a queue of outgoing messages, and some session state to
177 * ensure that we can preserve the lossless, ordered delivery of
178 * messages in the case of a TCP disconnect.
179 */
180struct ceph_connection {
181 void *private;
Sage Weil31b80062009-10-06 11:31:13 -0700182
183 const struct ceph_connection_operations *ops;
184
185 struct ceph_messenger *msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500186
187 atomic_t sock_state;
Sage Weil31b80062009-10-06 11:31:13 -0700188 struct socket *sock;
Alex Elderce2c8902012-05-22 22:15:49 -0500189 struct ceph_entity_addr peer_addr; /* peer address */
190 struct ceph_entity_addr peer_addr_for_me;
191
Alex Elder928443c2012-05-22 11:41:43 -0500192 unsigned long flags;
193 unsigned long state;
Sage Weil31b80062009-10-06 11:31:13 -0700194 const char *error_msg; /* error message, if any */
195
Sage Weil31b80062009-10-06 11:31:13 -0700196 struct ceph_entity_name peer_name; /* peer name */
Alex Elderce2c8902012-05-22 22:15:49 -0500197
Ilya Dryomov12b46292013-12-24 21:19:23 +0200198 u64 peer_features;
Sage Weil31b80062009-10-06 11:31:13 -0700199 u32 connect_seq; /* identify the most recent connection
200 attempt for this connection, client */
201 u32 peer_global_seq; /* peer's global seq for this connection */
202
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800203 int auth_retry; /* true if we need a newer authorizer */
204 void *auth_reply_buf; /* where to put the authorizer reply */
205 int auth_reply_buf_len;
206
Sage Weilec302642009-12-22 10:43:42 -0800207 struct mutex mutex;
208
Sage Weil31b80062009-10-06 11:31:13 -0700209 /* out queue */
Sage Weil31b80062009-10-06 11:31:13 -0700210 struct list_head out_queue;
211 struct list_head out_sent; /* sending or sent but unacked */
212 u64 out_seq; /* last message queued for send */
Sage Weil31b80062009-10-06 11:31:13 -0700213
214 u64 in_seq, in_seq_acked; /* last message received, acked */
215
216 /* connection negotiation temps */
217 char in_banner[CEPH_BANNER_MAX_LEN];
Sage Weila16cb1f2012-07-10 11:53:34 -0700218 struct ceph_msg_connect out_connect;
219 struct ceph_msg_connect_reply in_reply;
Sage Weil31b80062009-10-06 11:31:13 -0700220 struct ceph_entity_addr actual_peer_addr;
221
222 /* message out temps */
Ilya Dryomov67645d72015-12-28 13:18:34 +0300223 struct ceph_msg_header out_hdr;
Sage Weil31b80062009-10-06 11:31:13 -0700224 struct ceph_msg *out_msg; /* sending message (== tail of
225 out_sent) */
Sage Weilc86a2932009-12-14 14:04:30 -0800226 bool out_msg_done;
Sage Weil31b80062009-10-06 11:31:13 -0700227
228 struct kvec out_kvec[8], /* sending header/footer data */
229 *out_kvec_cur;
230 int out_kvec_left; /* kvec's left in out_kvec */
231 int out_skip; /* skip this many bytes */
232 int out_kvec_bytes; /* total bytes left */
Sage Weil31b80062009-10-06 11:31:13 -0700233 int out_more; /* there is more data after the kvecs */
234 __le64 out_temp_ack; /* for writing an ack */
Ilya Dryomov7f61f542015-09-14 16:01:05 +0300235 struct ceph_timespec out_temp_keepalive2; /* for writing keepalive2
236 stamp */
Sage Weil31b80062009-10-06 11:31:13 -0700237
238 /* message in temps */
239 struct ceph_msg_header in_hdr;
240 struct ceph_msg *in_msg;
Sage Weil31b80062009-10-06 11:31:13 -0700241 u32 in_front_crc, in_middle_crc, in_data_crc; /* calculated crc */
242
243 char in_tag; /* protocol control byte */
244 int in_base_pos; /* bytes read */
245 __le64 in_temp_ack; /* for reading an ack */
246
Ilya Dryomov7f61f542015-09-14 16:01:05 +0300247 struct timespec last_keepalive_ack; /* keepalive2 ack stamp */
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800248
Sage Weil31b80062009-10-06 11:31:13 -0700249 struct delayed_work work; /* send|recv work */
250 unsigned long delay; /* current delay interval */
251};
252
253
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700254extern const char *ceph_pr_addr(const struct sockaddr_storage *ss);
Sage Weil31b80062009-10-06 11:31:13 -0700255extern int ceph_parse_ips(const char *c, const char *end,
256 struct ceph_entity_addr *addr,
257 int max_count, int *count);
258
259
260extern int ceph_msgr_init(void);
261extern void ceph_msgr_exit(void);
Sage Weila922d382010-05-29 09:41:23 -0700262extern void ceph_msgr_flush(void);
Sage Weil31b80062009-10-06 11:31:13 -0700263
Alex Elder15d98822012-05-26 23:26:43 -0500264extern void ceph_messenger_init(struct ceph_messenger *msgr,
Ilya Dryomov859bff52015-10-28 23:50:58 +0100265 struct ceph_entity_addr *myaddr);
Ilya Dryomov757856d2015-06-25 17:47:45 +0300266extern void ceph_messenger_fini(struct ceph_messenger *msgr);
Sage Weil31b80062009-10-06 11:31:13 -0700267
Alex Elder1bfd89f2012-05-26 23:26:43 -0500268extern void ceph_con_init(struct ceph_connection *con, void *private,
269 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700270 struct ceph_messenger *msgr);
Sage Weil31b80062009-10-06 11:31:13 -0700271extern void ceph_con_open(struct ceph_connection *con,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700272 __u8 entity_type, __u64 entity_num,
Sage Weil31b80062009-10-06 11:31:13 -0700273 struct ceph_entity_addr *addr);
Sage Weil87b315a2010-03-22 14:51:18 -0700274extern bool ceph_con_opened(struct ceph_connection *con);
Sage Weil31b80062009-10-06 11:31:13 -0700275extern void ceph_con_close(struct ceph_connection *con);
276extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
Alex Elder6740a842012-06-01 14:56:43 -0500277
278extern void ceph_msg_revoke(struct ceph_msg *msg);
Alex Elder8921d112012-06-01 14:56:43 -0500279extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
280
Sage Weil31b80062009-10-06 11:31:13 -0700281extern void ceph_con_keepalive(struct ceph_connection *con);
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800282extern bool ceph_con_keepalive_expired(struct ceph_connection *con,
283 unsigned long interval);
Sage Weil31b80062009-10-06 11:31:13 -0700284
Alex Elder90af3602013-04-05 14:46:01 -0500285extern void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Alex Elderf1baeb22013-03-07 15:38:26 -0600286 size_t length, size_t alignment);
Alex Elder90af3602013-04-05 14:46:01 -0500287extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
Alex Elder27fa8382013-02-14 12:16:43 -0600288 struct ceph_pagelist *pagelist);
Alex Elderea965712013-04-05 14:46:01 -0500289#ifdef CONFIG_BLOCK
Alex Elder90af3602013-04-05 14:46:01 -0500290extern void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
Alex Eldera1930802013-03-14 14:09:06 -0500291 size_t length);
Alex Elderea965712013-04-05 14:46:01 -0500292#endif /* CONFIG_BLOCK */
Alex Elder02afca62013-02-14 12:16:43 -0600293
Sage Weilb61c2762011-08-09 15:03:46 -0700294extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
295 bool can_fail);
Sage Weil31b80062009-10-06 11:31:13 -0700296
Ilya Dryomov0215e442014-06-20 14:14:41 +0400297extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
298extern void ceph_msg_put(struct ceph_msg *msg);
Sage Weil31b80062009-10-06 11:31:13 -0700299
Sage Weil9ec7cab2009-12-14 15:13:47 -0800300extern void ceph_msg_dump(struct ceph_msg *msg);
301
Sage Weil31b80062009-10-06 11:31:13 -0700302#endif