blob: e15499422fdcc7686942ce88389d686d9078cd79 [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>
11
David Howellsa1ce3922012-10-02 18:01:25 +010012#include <linux/ceph/types.h>
13#include <linux/ceph/buffer.h>
Sage Weil31b80062009-10-06 11:31:13 -070014
15struct ceph_msg;
16struct ceph_connection;
17
Sage Weil31b80062009-10-06 11:31:13 -070018/*
19 * Ceph defines these callbacks for handling connection events.
20 */
21struct ceph_connection_operations {
22 struct ceph_connection *(*get)(struct ceph_connection *);
23 void (*put)(struct ceph_connection *);
24
25 /* handle an incoming message. */
26 void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
27
Sage Weil4e7a5dc2009-11-18 16:19:57 -080028 /* authorize an outgoing connection */
Alex Eldera3530df2012-05-16 15:16:39 -050029 struct ceph_auth_handshake *(*get_authorizer) (
30 struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -050031 int *proto, int force_new);
Sage Weil4e7a5dc2009-11-18 16:19:57 -080032 int (*verify_authorizer_reply) (struct ceph_connection *con, int len);
Sage Weil9bd2e6f2010-02-02 16:21:06 -080033 int (*invalidate_authorizer)(struct ceph_connection *con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -080034
Sage Weil31b80062009-10-06 11:31:13 -070035 /* there was some error on the socket (disconnect, whatever) */
36 void (*fault) (struct ceph_connection *con);
37
38 /* a remote host as terminated a message exchange session, and messages
39 * we sent (or they tried to send us) may be lost. */
40 void (*peer_reset) (struct ceph_connection *con);
41
42 struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -080043 struct ceph_msg_header *hdr,
44 int *skip);
Yan, Zheng33d07332014-11-04 16:33:37 +080045 int (*sign_message) (struct ceph_connection *con, struct ceph_msg *msg);
46
47 int (*check_message_signature) (struct ceph_connection *con,
48 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;
Sage Weil31b80062009-10-06 11:31:13 -070059 bool nocrc;
Chaitanya Huilgolba988f82015-01-23 16:41:25 +053060 bool tcp_nodelay;
Sage Weil31b80062009-10-06 11:31:13 -070061
62 /*
63 * the global_seq counts connections i (attempt to) initiate
64 * in order to disambiguate certain connect race conditions.
65 */
66 u32 global_seq;
67 spinlock_t global_seq_lock;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070068
Ilya Dryomov12b46292013-12-24 21:19:23 +020069 u64 supported_features;
70 u64 required_features;
Sage Weil31b80062009-10-06 11:31:13 -070071};
72
Alex Elder43794502013-03-01 18:00:16 -060073enum ceph_msg_data_type {
74 CEPH_MSG_DATA_NONE, /* message contains no data payload */
75 CEPH_MSG_DATA_PAGES, /* data source/destination is a page array */
76 CEPH_MSG_DATA_PAGELIST, /* data source/destination is a pagelist */
77#ifdef CONFIG_BLOCK
78 CEPH_MSG_DATA_BIO, /* data source/destination is a bio list */
79#endif /* CONFIG_BLOCK */
80};
81
82static __inline__ bool ceph_msg_data_type_valid(enum ceph_msg_data_type type)
83{
84 switch (type) {
85 case CEPH_MSG_DATA_NONE:
86 case CEPH_MSG_DATA_PAGES:
87 case CEPH_MSG_DATA_PAGELIST:
88#ifdef CONFIG_BLOCK
89 case CEPH_MSG_DATA_BIO:
90#endif /* CONFIG_BLOCK */
91 return true;
92 default:
93 return false;
94 }
95}
96
Alex Elder36153ec2013-03-14 14:09:06 -050097struct ceph_msg_data {
Alex Elder5240d9f2013-03-14 14:09:06 -050098 struct list_head links; /* ceph_msg->data */
Alex Elder36153ec2013-03-14 14:09:06 -050099 enum ceph_msg_data_type type;
100 union {
101#ifdef CONFIG_BLOCK
102 struct {
103 struct bio *bio;
104 size_t bio_length;
105 };
106#endif /* CONFIG_BLOCK */
107 struct {
108 struct page **pages; /* NOT OWNER. */
109 size_t length; /* total # bytes */
110 unsigned int alignment; /* first page */
111 };
112 struct ceph_pagelist *pagelist;
113 };
Alex Elder36153ec2013-03-14 14:09:06 -0500114};
115
Alex Elderfe38a2b2013-03-06 23:39:39 -0600116struct ceph_msg_data_cursor {
Alex Elderca8b3a62013-04-05 14:46:01 -0500117 size_t total_resid; /* across all data items */
118 struct list_head *data_head; /* = &ceph_msg->data */
119
120 struct ceph_msg_data *data; /* current data item */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500121 size_t resid; /* bytes not yet consumed */
122 bool last_piece; /* current is last piece */
123 bool need_crc; /* crc update needed */
Alex Elderdd236fc2013-03-06 23:39:39 -0600124 union {
Alex Elder6aaa4512013-03-06 23:39:39 -0600125#ifdef CONFIG_BLOCK
126 struct { /* bio */
127 struct bio *bio; /* bio from list */
Kent Overstreetf38a5182013-08-07 14:30:24 -0700128 struct bvec_iter bvec_iter;
Alex Elder6aaa4512013-03-06 23:39:39 -0600129 };
130#endif /* CONFIG_BLOCK */
Alex Eldere766d7b2013-03-07 15:38:28 -0600131 struct { /* pages */
Alex Eldere766d7b2013-03-07 15:38:28 -0600132 unsigned int page_offset; /* offset in page */
133 unsigned short page_index; /* index in array */
134 unsigned short page_count; /* pages in array */
135 };
Alex Elderdd236fc2013-03-06 23:39:39 -0600136 struct { /* pagelist */
137 struct page *page; /* page from list */
138 size_t offset; /* bytes from list */
139 };
140 };
Alex Elderfe38a2b2013-03-06 23:39:39 -0600141};
142
Sage Weil31b80062009-10-06 11:31:13 -0700143/*
144 * a single message. it contains a header (src, dest, message type, etc.),
145 * footer (crc values, mainly), a "front" message body, and possibly a
146 * data payload (stored in some number of pages).
147 */
148struct ceph_msg {
149 struct ceph_msg_header hdr; /* header */
Yan, Zheng33d07332014-11-04 16:33:37 +0800150 union {
151 struct ceph_msg_footer footer; /* footer */
152 struct ceph_msg_footer_old old_footer; /* old format footer */
153 };
Sage Weil31b80062009-10-06 11:31:13 -0700154 struct kvec front; /* unaligned blobs of message */
155 struct ceph_buffer *middle;
Alex Elder02afca62013-02-14 12:16:43 -0600156
Alex Elder36153ec2013-03-14 14:09:06 -0500157 size_t data_length;
Alex Elder5240d9f2013-03-14 14:09:06 -0500158 struct list_head data;
Alex Elder36153ec2013-03-14 14:09:06 -0500159 struct ceph_msg_data_cursor cursor;
Alex Elder02afca62013-02-14 12:16:43 -0600160
161 struct ceph_connection *con;
162 struct list_head list_head; /* links for connection lists */
163
164 struct kref kref;
Sage Weil31b80062009-10-06 11:31:13 -0700165 bool more_to_follow;
Sage Weile84346b2010-05-11 21:20:38 -0700166 bool needs_out_seq;
Ilya Dryomov3cea4c32014-01-09 20:08:21 +0200167 int front_alloc_len;
Sage Weil4cf9d542011-07-26 11:27:24 -0700168 unsigned long ack_stamp; /* tx: when we were acked */
Sage Weil31b80062009-10-06 11:31:13 -0700169
170 struct ceph_msgpool *pool;
171};
172
Sage Weil31b80062009-10-06 11:31:13 -0700173/* ceph connection fault delay defaults, for exponential backoff */
174#define BASE_DELAY_INTERVAL (HZ/2)
175#define MAX_DELAY_INTERVAL (5 * 60 * HZ)
176
177/*
Sage Weil31b80062009-10-06 11:31:13 -0700178 * A single connection with another host.
179 *
180 * We maintain a queue of outgoing messages, and some session state to
181 * ensure that we can preserve the lossless, ordered delivery of
182 * messages in the case of a TCP disconnect.
183 */
184struct ceph_connection {
185 void *private;
Sage Weil31b80062009-10-06 11:31:13 -0700186
187 const struct ceph_connection_operations *ops;
188
189 struct ceph_messenger *msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500190
191 atomic_t sock_state;
Sage Weil31b80062009-10-06 11:31:13 -0700192 struct socket *sock;
Alex Elderce2c8902012-05-22 22:15:49 -0500193 struct ceph_entity_addr peer_addr; /* peer address */
194 struct ceph_entity_addr peer_addr_for_me;
195
Alex Elder928443c2012-05-22 11:41:43 -0500196 unsigned long flags;
197 unsigned long state;
Sage Weil31b80062009-10-06 11:31:13 -0700198 const char *error_msg; /* error message, if any */
199
Sage Weil31b80062009-10-06 11:31:13 -0700200 struct ceph_entity_name peer_name; /* peer name */
Alex Elderce2c8902012-05-22 22:15:49 -0500201
Ilya Dryomov12b46292013-12-24 21:19:23 +0200202 u64 peer_features;
Sage Weil31b80062009-10-06 11:31:13 -0700203 u32 connect_seq; /* identify the most recent connection
204 attempt for this connection, client */
205 u32 peer_global_seq; /* peer's global seq for this connection */
206
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800207 int auth_retry; /* true if we need a newer authorizer */
208 void *auth_reply_buf; /* where to put the authorizer reply */
209 int auth_reply_buf_len;
210
Sage Weilec302642009-12-22 10:43:42 -0800211 struct mutex mutex;
212
Sage Weil31b80062009-10-06 11:31:13 -0700213 /* out queue */
Sage Weil31b80062009-10-06 11:31:13 -0700214 struct list_head out_queue;
215 struct list_head out_sent; /* sending or sent but unacked */
216 u64 out_seq; /* last message queued for send */
Sage Weil31b80062009-10-06 11:31:13 -0700217
218 u64 in_seq, in_seq_acked; /* last message received, acked */
219
220 /* connection negotiation temps */
221 char in_banner[CEPH_BANNER_MAX_LEN];
Sage Weila16cb1f2012-07-10 11:53:34 -0700222 struct ceph_msg_connect out_connect;
223 struct ceph_msg_connect_reply in_reply;
Sage Weil31b80062009-10-06 11:31:13 -0700224 struct ceph_entity_addr actual_peer_addr;
225
226 /* message out temps */
227 struct ceph_msg *out_msg; /* sending message (== tail of
228 out_sent) */
Sage Weilc86a2932009-12-14 14:04:30 -0800229 bool out_msg_done;
Sage Weil31b80062009-10-06 11:31:13 -0700230
231 struct kvec out_kvec[8], /* sending header/footer data */
232 *out_kvec_cur;
233 int out_kvec_left; /* kvec's left in out_kvec */
234 int out_skip; /* skip this many bytes */
235 int out_kvec_bytes; /* total bytes left */
236 bool out_kvec_is_msg; /* kvec refers to out_msg */
237 int out_more; /* there is more data after the kvecs */
238 __le64 out_temp_ack; /* for writing an ack */
239
240 /* message in temps */
241 struct ceph_msg_header in_hdr;
242 struct ceph_msg *in_msg;
Sage Weil31b80062009-10-06 11:31:13 -0700243 u32 in_front_crc, in_middle_crc, in_data_crc; /* calculated crc */
244
245 char in_tag; /* protocol control byte */
246 int in_base_pos; /* bytes read */
247 __le64 in_temp_ack; /* for reading an ack */
248
249 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,
265 struct ceph_entity_addr *myaddr,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200266 u64 supported_features,
267 u64 required_features,
Chaitanya Huilgolba988f82015-01-23 16:41:25 +0530268 bool nocrc,
269 bool tcp_nodelay);
Sage Weil31b80062009-10-06 11:31:13 -0700270
Alex Elder1bfd89f2012-05-26 23:26:43 -0500271extern void ceph_con_init(struct ceph_connection *con, void *private,
272 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700273 struct ceph_messenger *msgr);
Sage Weil31b80062009-10-06 11:31:13 -0700274extern void ceph_con_open(struct ceph_connection *con,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700275 __u8 entity_type, __u64 entity_num,
Sage Weil31b80062009-10-06 11:31:13 -0700276 struct ceph_entity_addr *addr);
Sage Weil87b315a2010-03-22 14:51:18 -0700277extern bool ceph_con_opened(struct ceph_connection *con);
Sage Weil31b80062009-10-06 11:31:13 -0700278extern void ceph_con_close(struct ceph_connection *con);
279extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
Alex Elder6740a842012-06-01 14:56:43 -0500280
281extern void ceph_msg_revoke(struct ceph_msg *msg);
Alex Elder8921d112012-06-01 14:56:43 -0500282extern void ceph_msg_revoke_incoming(struct ceph_msg *msg);
283
Sage Weil31b80062009-10-06 11:31:13 -0700284extern void ceph_con_keepalive(struct ceph_connection *con);
Sage Weil31b80062009-10-06 11:31:13 -0700285
Alex Elder90af3602013-04-05 14:46:01 -0500286extern void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Alex Elderf1baeb22013-03-07 15:38:26 -0600287 size_t length, size_t alignment);
Alex Elder90af3602013-04-05 14:46:01 -0500288extern void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
Alex Elder27fa8382013-02-14 12:16:43 -0600289 struct ceph_pagelist *pagelist);
Alex Elderea965712013-04-05 14:46:01 -0500290#ifdef CONFIG_BLOCK
Alex Elder90af3602013-04-05 14:46:01 -0500291extern void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
Alex Eldera1930802013-03-14 14:09:06 -0500292 size_t length);
Alex Elderea965712013-04-05 14:46:01 -0500293#endif /* CONFIG_BLOCK */
Alex Elder02afca62013-02-14 12:16:43 -0600294
Sage Weilb61c2762011-08-09 15:03:46 -0700295extern struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
296 bool can_fail);
Sage Weil31b80062009-10-06 11:31:13 -0700297
Ilya Dryomov0215e442014-06-20 14:14:41 +0400298extern struct ceph_msg *ceph_msg_get(struct ceph_msg *msg);
299extern void ceph_msg_put(struct ceph_msg *msg);
Sage Weil31b80062009-10-06 11:31:13 -0700300
Sage Weil9ec7cab2009-12-14 15:13:47 -0800301extern void ceph_msg_dump(struct ceph_msg *msg);
302
Sage Weil31b80062009-10-06 11:31:13 -0700303#endif