blob: 1831f63536225bbe120dbc5215b26d2d6518f3fb [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07002
3#include <linux/crc32c.h>
4#include <linux/ctype.h>
5#include <linux/highmem.h>
6#include <linux/inet.h>
7#include <linux/kthread.h>
8#include <linux/net.h>
Ilya Dryomov757856d2015-06-25 17:47:45 +03009#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070011#include <linux/socket.h>
12#include <linux/string.h>
Alex Elder3ebc21f2013-01-31 16:02:01 -060013#ifdef CONFIG_BLOCK
Yehuda Sadeh68b44762010-04-06 15:01:27 -070014#include <linux/bio.h>
Alex Elder3ebc21f2013-01-31 16:02:01 -060015#endif /* CONFIG_BLOCK */
Noah Watkinsee3b56f2011-09-23 11:48:42 -070016#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070017#include <net/tcp.h>
18
Ilya Dryomov2b3e0c92013-12-24 21:19:24 +020019#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070020#include <linux/ceph/libceph.h>
21#include <linux/ceph/messenger.h>
22#include <linux/ceph/decode.h>
23#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070025
26/*
27 * Ceph uses the messenger to exchange ceph_msg messages with other
28 * hosts in the system. The messenger provides ordered and reliable
29 * delivery. We tolerate TCP disconnects by reconnecting (with
30 * exponential backoff) in the case of a fault (disconnection, bad
31 * crc, protocol error). Acks allow sent messages to be discarded by
32 * the sender.
33 */
34
Alex Elderbc18f4b2012-06-20 21:53:53 -050035/*
36 * We track the state of the socket on a given connection using
37 * values defined below. The transition to a new socket state is
38 * handled by a function which verifies we aren't coming from an
39 * unexpected state.
40 *
41 * --------
42 * | NEW* | transient initial state
43 * --------
44 * | con_sock_state_init()
45 * v
46 * ----------
47 * | CLOSED | initialized, but no socket (and no
48 * ---------- TCP connection)
49 * ^ \
50 * | \ con_sock_state_connecting()
51 * | ----------------------
52 * | \
53 * + con_sock_state_closed() \
Sage Weilfbb85a42012-06-27 12:31:02 -070054 * |+--------------------------- \
55 * | \ \ \
56 * | ----------- \ \
57 * | | CLOSING | socket event; \ \
58 * | ----------- await close \ \
59 * | ^ \ |
60 * | | \ |
61 * | + con_sock_state_closing() \ |
62 * | / \ | |
63 * | / --------------- | |
64 * | / \ v v
Alex Elderbc18f4b2012-06-20 21:53:53 -050065 * | / --------------
66 * | / -----------------| CONNECTING | socket created, TCP
67 * | | / -------------- connect initiated
68 * | | | con_sock_state_connected()
69 * | | v
70 * -------------
71 * | CONNECTED | TCP connection established
72 * -------------
73 *
74 * State values for ceph_connection->sock_state; NEW is assumed to be 0.
75 */
Alex Elderce2c8902012-05-22 22:15:49 -050076
77#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
78#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
79#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
80#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
81#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
82
Sage Weil8dacc7d2012-07-20 17:24:40 -070083/*
84 * connection states
85 */
86#define CON_STATE_CLOSED 1 /* -> PREOPEN */
87#define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
88#define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
89#define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
90#define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
91#define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
92
Sage Weil4a861692012-07-20 17:29:55 -070093/*
94 * ceph_connection flag bits
95 */
96#define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
97 * messages on errors */
98#define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
99#define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
100#define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
101#define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
Sage Weil8dacc7d2012-07-20 17:24:40 -0700102
Alex Elderc9ffc772013-02-20 10:25:12 -0600103static bool con_flag_valid(unsigned long con_flag)
104{
105 switch (con_flag) {
106 case CON_FLAG_LOSSYTX:
107 case CON_FLAG_KEEPALIVE_PENDING:
108 case CON_FLAG_WRITE_PENDING:
109 case CON_FLAG_SOCK_CLOSED:
110 case CON_FLAG_BACKOFF:
111 return true;
112 default:
113 return false;
114 }
115}
116
117static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
118{
119 BUG_ON(!con_flag_valid(con_flag));
120
121 clear_bit(con_flag, &con->flags);
122}
123
124static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
125{
126 BUG_ON(!con_flag_valid(con_flag));
127
128 set_bit(con_flag, &con->flags);
129}
130
131static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
132{
133 BUG_ON(!con_flag_valid(con_flag));
134
135 return test_bit(con_flag, &con->flags);
136}
137
138static bool con_flag_test_and_clear(struct ceph_connection *con,
139 unsigned long con_flag)
140{
141 BUG_ON(!con_flag_valid(con_flag));
142
143 return test_and_clear_bit(con_flag, &con->flags);
144}
145
146static bool con_flag_test_and_set(struct ceph_connection *con,
147 unsigned long con_flag)
148{
149 BUG_ON(!con_flag_valid(con_flag));
150
151 return test_and_set_bit(con_flag, &con->flags);
152}
153
Alex Eldere3d5d632013-05-01 12:43:04 -0500154/* Slab caches for frequently-allocated structures */
155
156static struct kmem_cache *ceph_msg_cache;
Alex Elder81b36be2013-05-01 12:43:04 -0500157static struct kmem_cache *ceph_msg_data_cache;
Alex Eldere3d5d632013-05-01 12:43:04 -0500158
Sage Weil31b80062009-10-06 11:31:13 -0700159/* static tag bytes (protocol control messages) */
160static char tag_msg = CEPH_MSGR_TAG_MSG;
161static char tag_ack = CEPH_MSGR_TAG_ACK;
162static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800163static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
Sage Weil31b80062009-10-06 11:31:13 -0700164
Sage Weila6a53492010-04-13 14:07:07 -0700165#ifdef CONFIG_LOCKDEP
166static struct lock_class_key socket_class;
167#endif
168
Alex Elder84495f42012-02-15 07:43:55 -0600169/*
170 * When skipping (ignoring) a block of input we read it into a "skip
171 * buffer," which is this many bytes in size.
172 */
173#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -0700174
175static void queue_con(struct ceph_connection *con);
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400176static void cancel_con(struct ceph_connection *con);
Ilya Dryomov68931622015-07-03 15:44:41 +0300177static void ceph_con_workfn(struct work_struct *);
Alex Elder93209262013-02-19 12:25:57 -0600178static void con_fault(struct ceph_connection *con);
Sage Weil31b80062009-10-06 11:31:13 -0700179
Sage Weil31b80062009-10-06 11:31:13 -0700180/*
Alex Elderf64a9312012-01-23 15:49:27 -0600181 * Nicely render a sockaddr as a string. An array of formatted
182 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -0700183 */
Alex Elderf64a9312012-01-23 15:49:27 -0600184#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
185#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
186#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
187#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
188
189static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
190static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -0700191
Alex Elder57666512012-01-23 15:49:27 -0600192static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -0600193
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700194const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -0700195{
196 int i;
197 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -0600198 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
199 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -0700200
Alex Elderf64a9312012-01-23 15:49:27 -0600201 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700202 s = addr_str[i];
203
204 switch (ss->ss_family) {
205 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -0600206 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
207 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700208 break;
209
210 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -0600211 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
212 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700213 break;
214
215 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600216 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
217 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700218 }
219
220 return s;
221}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700222EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700223
Sage Weil63f2d212009-11-03 15:17:56 -0800224static void encode_my_addr(struct ceph_messenger *msgr)
225{
226 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
227 ceph_encode_addr(&msgr->my_enc_addr);
228}
229
Sage Weil31b80062009-10-06 11:31:13 -0700230/*
231 * work queue for all reading and writing to/from the socket.
232 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600233static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700234
Alex Eldere3d5d632013-05-01 12:43:04 -0500235static int ceph_msgr_slab_init(void)
236{
237 BUG_ON(ceph_msg_cache);
Geliang Tang5ee61e92016-03-13 15:18:39 +0800238 ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
Alex Elder81b36be2013-05-01 12:43:04 -0500239 if (!ceph_msg_cache)
240 return -ENOMEM;
241
242 BUG_ON(ceph_msg_data_cache);
Geliang Tang5ee61e92016-03-13 15:18:39 +0800243 ceph_msg_data_cache = KMEM_CACHE(ceph_msg_data, 0);
Alex Elder81b36be2013-05-01 12:43:04 -0500244 if (ceph_msg_data_cache)
245 return 0;
246
247 kmem_cache_destroy(ceph_msg_cache);
248 ceph_msg_cache = NULL;
249
250 return -ENOMEM;
Alex Eldere3d5d632013-05-01 12:43:04 -0500251}
252
253static void ceph_msgr_slab_exit(void)
254{
Alex Elder81b36be2013-05-01 12:43:04 -0500255 BUG_ON(!ceph_msg_data_cache);
256 kmem_cache_destroy(ceph_msg_data_cache);
257 ceph_msg_data_cache = NULL;
258
Alex Eldere3d5d632013-05-01 12:43:04 -0500259 BUG_ON(!ceph_msg_cache);
260 kmem_cache_destroy(ceph_msg_cache);
261 ceph_msg_cache = NULL;
262}
263
Alex Elder15417162013-02-19 12:25:56 -0600264static void _ceph_msgr_exit(void)
Alex Elder6173d1f2012-02-14 14:05:33 -0600265{
Alex Elderd3002b92012-02-14 14:05:33 -0600266 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600267 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600268 ceph_msgr_wq = NULL;
269 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600270
Alex Elder6173d1f2012-02-14 14:05:33 -0600271 BUG_ON(zero_page == NULL);
Alex Elder6173d1f2012-02-14 14:05:33 -0600272 page_cache_release(zero_page);
273 zero_page = NULL;
Benoît Canetd920ff62015-06-25 21:02:57 +0200274
275 ceph_msgr_slab_exit();
Alex Elder6173d1f2012-02-14 14:05:33 -0600276}
277
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700278int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700279{
Benoît Canetd920ff62015-06-25 21:02:57 +0200280 if (ceph_msgr_slab_init())
281 return -ENOMEM;
282
Alex Elder57666512012-01-23 15:49:27 -0600283 BUG_ON(zero_page != NULL);
284 zero_page = ZERO_PAGE(0);
285 page_cache_get(zero_page);
286
Ilya Dryomovf9865f02014-10-10 16:39:05 +0400287 /*
288 * The number of active work items is limited by the number of
289 * connections, so leave @max_active at default.
290 */
291 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600292 if (ceph_msgr_wq)
293 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600294
Alex Elder6173d1f2012-02-14 14:05:33 -0600295 pr_err("msgr_init failed to create workqueue\n");
296 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600297
Alex Elder6173d1f2012-02-14 14:05:33 -0600298 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700299}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700300EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700301
302void ceph_msgr_exit(void)
303{
Alex Elder57666512012-01-23 15:49:27 -0600304 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600305
Alex Elder6173d1f2012-02-14 14:05:33 -0600306 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700307}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700308EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700309
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700310void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700311{
312 flush_workqueue(ceph_msgr_wq);
313}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700314EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700315
Alex Elderce2c8902012-05-22 22:15:49 -0500316/* Connection socket state transition functions */
317
318static void con_sock_state_init(struct ceph_connection *con)
319{
320 int old_state;
321
322 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
323 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
324 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700325 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
326 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500327}
328
329static void con_sock_state_connecting(struct ceph_connection *con)
330{
331 int old_state;
332
333 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
334 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
335 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700336 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
337 CON_SOCK_STATE_CONNECTING);
Alex Elderce2c8902012-05-22 22:15:49 -0500338}
339
340static void con_sock_state_connected(struct ceph_connection *con)
341{
342 int old_state;
343
344 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
345 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
346 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700347 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
348 CON_SOCK_STATE_CONNECTED);
Alex Elderce2c8902012-05-22 22:15:49 -0500349}
350
351static void con_sock_state_closing(struct ceph_connection *con)
352{
353 int old_state;
354
355 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
356 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
357 old_state != CON_SOCK_STATE_CONNECTED &&
358 old_state != CON_SOCK_STATE_CLOSING))
359 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700360 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
361 CON_SOCK_STATE_CLOSING);
Alex Elderce2c8902012-05-22 22:15:49 -0500362}
363
364static void con_sock_state_closed(struct ceph_connection *con)
365{
366 int old_state;
367
368 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
369 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
Sage Weilfbb85a42012-06-27 12:31:02 -0700370 old_state != CON_SOCK_STATE_CLOSING &&
Sage Weil8007b8d2012-07-30 18:16:16 -0700371 old_state != CON_SOCK_STATE_CONNECTING &&
372 old_state != CON_SOCK_STATE_CLOSED))
Alex Elderce2c8902012-05-22 22:15:49 -0500373 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700374 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
375 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500376}
Sage Weila922d382010-05-29 09:41:23 -0700377
Sage Weil31b80062009-10-06 11:31:13 -0700378/*
379 * socket callback functions
380 */
381
382/* data available on socket, or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400383static void ceph_sock_data_ready(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700384{
Alex Elderbd406142012-01-23 15:49:27 -0600385 struct ceph_connection *con = sk->sk_user_data;
Guanjun Hea2a32582012-07-08 19:50:33 -0700386 if (atomic_read(&con->msgr->stopping)) {
387 return;
388 }
Alex Elderbd406142012-01-23 15:49:27 -0600389
Sage Weil31b80062009-10-06 11:31:13 -0700390 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500391 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700392 con, con->state);
393 queue_con(con);
394 }
395}
396
397/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500398static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700399{
Alex Elderd3002b92012-02-14 14:05:33 -0600400 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700401
Jim Schutt182fac22012-02-29 08:30:58 -0700402 /* only queue to workqueue if there is data we want to write,
403 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500404 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700405 * doesn't get called again until try_write() fills the socket
406 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
407 * and net/core/stream.c:sk_stream_write_space().
408 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600409 if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
Eric Dumazet64dc6132013-07-22 20:26:31 -0700410 if (sk_stream_is_writeable(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500411 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700412 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
413 queue_con(con);
414 }
Sage Weil31b80062009-10-06 11:31:13 -0700415 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500416 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700417 }
Sage Weil31b80062009-10-06 11:31:13 -0700418}
419
420/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500421static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700422{
Alex Elderbd406142012-01-23 15:49:27 -0600423 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700424
Alex Elder327800b2012-05-22 11:41:43 -0500425 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700426 con, con->state, sk->sk_state);
427
Sage Weil31b80062009-10-06 11:31:13 -0700428 switch (sk->sk_state) {
429 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500430 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700431 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500432 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500433 con_sock_state_closing(con);
Alex Elderc9ffc772013-02-20 10:25:12 -0600434 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
Alex Elderd65c9e02012-06-20 21:53:53 -0500435 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700436 break;
437 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500438 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500439 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700440 queue_con(con);
441 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600442 default: /* Everything else is uninteresting */
443 break;
Sage Weil31b80062009-10-06 11:31:13 -0700444 }
445}
446
447/*
448 * set up socket callbacks
449 */
450static void set_sock_callbacks(struct socket *sock,
451 struct ceph_connection *con)
452{
453 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600454 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500455 sk->sk_data_ready = ceph_sock_data_ready;
456 sk->sk_write_space = ceph_sock_write_space;
457 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700458}
459
460
461/*
462 * socket helpers
463 */
464
465/*
466 * initiate connection to a remote socket.
467 */
Alex Elder41617d02012-02-14 14:05:33 -0600468static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700469{
Sage Weilf91d3472010-07-01 15:18:31 -0700470 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700471 struct socket *sock;
472 int ret;
473
474 BUG_ON(con->sock);
Ilya Dryomov757856d2015-06-25 17:47:45 +0300475 ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -0500476 SOCK_STREAM, IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700477 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600478 return ret;
Ilya Dryomov6d7fdb02015-04-02 14:40:58 +0300479 sock->sk->sk_allocation = GFP_NOFS;
Sage Weil31b80062009-10-06 11:31:13 -0700480
Sage Weila6a53492010-04-13 14:07:07 -0700481#ifdef CONFIG_LOCKDEP
482 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
483#endif
484
Sage Weil31b80062009-10-06 11:31:13 -0700485 set_sock_callbacks(sock, con);
486
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700487 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700488
Sage Weil89a86be2012-06-09 14:19:21 -0700489 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700490 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
491 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700492 if (ret == -EINPROGRESS) {
493 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700494 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700495 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600496 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700497 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700498 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700499 sock_release(sock);
Alex Elder41617d02012-02-14 14:05:33 -0600500 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600501 }
Mike Christie89baaa52014-10-16 01:50:19 -0500502
Ilya Dryomov859bff52015-10-28 23:50:58 +0100503 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
Chaitanya Huilgolba988f82015-01-23 16:41:25 +0530504 int optval = 1;
505
506 ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
507 (char *)&optval, sizeof(optval));
508 if (ret)
509 pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
510 ret);
511 }
512
Alex Eldera5bc3122012-01-23 15:49:27 -0600513 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600514 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700515}
516
517static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
518{
519 struct kvec iov = {buf, len};
520 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800521 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700522
Sage Weil98bdb0a2011-01-25 08:17:48 -0800523 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
524 if (r == -EAGAIN)
525 r = 0;
526 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700527}
528
Alex Elderafb3d902013-03-08 20:58:59 -0600529static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
530 int page_offset, size_t length)
531{
532 void *kaddr;
533 int ret;
534
535 BUG_ON(page_offset + length > PAGE_SIZE);
536
537 kaddr = kmap(page);
538 BUG_ON(!kaddr);
539 ret = ceph_tcp_recvmsg(sock, kaddr + page_offset, length);
540 kunmap(page);
541
542 return ret;
543}
544
Sage Weil31b80062009-10-06 11:31:13 -0700545/*
546 * write something. @more is true if caller will be sending more data
547 * shortly.
548 */
549static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
550 size_t kvlen, size_t len, int more)
551{
552 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800553 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700554
555 if (more)
556 msg.msg_flags |= MSG_MORE;
557 else
558 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
559
Sage Weil42961d22011-01-25 08:19:34 -0800560 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
561 if (r == -EAGAIN)
562 r = 0;
563 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700564}
565
Chunwei Chen178eda22014-04-23 12:35:09 +0800566static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
Alex Eldere1dcb122013-03-06 23:39:38 -0600567 int offset, size_t size, bool more)
Alex Elder31739132012-03-07 11:40:08 -0600568{
569 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
570 int ret;
571
572 ret = kernel_sendpage(sock, page, offset, size, flags);
573 if (ret == -EAGAIN)
574 ret = 0;
575
576 return ret;
577}
578
Chunwei Chen178eda22014-04-23 12:35:09 +0800579static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
580 int offset, size_t size, bool more)
581{
582 int ret;
583 struct kvec iov;
584
585 /* sendpage cannot properly handle pages with page_count == 0,
586 * we need to fallback to sendmsg if that's the case */
587 if (page_count(page) >= 1)
588 return __ceph_tcp_sendpage(sock, page, offset, size, more);
589
590 iov.iov_base = kmap(page) + offset;
591 iov.iov_len = size;
592 ret = ceph_tcp_sendmsg(sock, &iov, 1, size, more);
593 kunmap(page);
594
595 return ret;
596}
Sage Weil31b80062009-10-06 11:31:13 -0700597
598/*
599 * Shutdown/close the socket for the given connection.
600 */
601static int con_close_socket(struct ceph_connection *con)
602{
Sage Weil8007b8d2012-07-30 18:16:16 -0700603 int rc = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700604
605 dout("con_close_socket on %p sock %p\n", con, con->sock);
Sage Weil8007b8d2012-07-30 18:16:16 -0700606 if (con->sock) {
607 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
608 sock_release(con->sock);
609 con->sock = NULL;
610 }
Alex Elder456ea462012-06-20 21:53:53 -0500611
612 /*
Sage Weil4a861692012-07-20 17:29:55 -0700613 * Forcibly clear the SOCK_CLOSED flag. It gets set
Alex Elder456ea462012-06-20 21:53:53 -0500614 * independent of the connection mutex, and we could have
615 * received a socket close event before we had the chance to
616 * shut the socket down.
617 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600618 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
Sage Weil8007b8d2012-07-30 18:16:16 -0700619
Alex Elderce2c8902012-05-22 22:15:49 -0500620 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700621 return rc;
622}
623
624/*
625 * Reset a connection. Discard all incoming and outgoing messages
626 * and clear *_seq state.
627 */
628static void ceph_msg_remove(struct ceph_msg *msg)
629{
630 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500631
Sage Weil31b80062009-10-06 11:31:13 -0700632 ceph_msg_put(msg);
633}
634static void ceph_msg_remove_list(struct list_head *head)
635{
636 while (!list_empty(head)) {
637 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
638 list_head);
639 ceph_msg_remove(msg);
640 }
641}
642
643static void reset_connection(struct ceph_connection *con)
644{
645 /* reset connection, out_queue, msg_ and connect_seq */
646 /* discard existing out_queue and msg_seq */
Sage Weil0fa6ebc2012-12-27 20:27:04 -0600647 dout("reset_connection %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -0700648 ceph_msg_remove_list(&con->out_queue);
649 ceph_msg_remove_list(&con->out_sent);
650
Sage Weilcf3e5c42009-12-11 09:48:05 -0800651 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500652 BUG_ON(con->in_msg->con != con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800653 ceph_msg_put(con->in_msg);
654 con->in_msg = NULL;
655 }
656
Sage Weil31b80062009-10-06 11:31:13 -0700657 con->connect_seq = 0;
658 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800659 if (con->out_msg) {
Ilya Dryomov583d0fe2015-11-02 17:13:58 +0100660 BUG_ON(con->out_msg->con != con);
Sage Weilc86a2932009-12-14 14:04:30 -0800661 ceph_msg_put(con->out_msg);
662 con->out_msg = NULL;
663 }
Sage Weil31b80062009-10-06 11:31:13 -0700664 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700665 con->in_seq_acked = 0;
Ilya Dryomov67645d72015-12-28 13:18:34 +0300666
667 con->out_skip = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700668}
669
670/*
671 * mark a peer down. drop any open connections.
672 */
673void ceph_con_close(struct ceph_connection *con)
674{
Sage Weil8c50c812012-07-30 16:24:37 -0700675 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700676 dout("con_close %p peer %s\n", con,
677 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700678 con->state = CON_STATE_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500679
Alex Elderc9ffc772013-02-20 10:25:12 -0600680 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
681 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
682 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
683 con_flag_clear(con, CON_FLAG_BACKOFF);
Alex Eldera5988c42012-05-29 11:04:58 -0500684
Sage Weil31b80062009-10-06 11:31:13 -0700685 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700686 con->peer_global_seq = 0;
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400687 cancel_con(con);
Sage Weilee76e072012-07-20 16:45:49 -0700688 con_close_socket(con);
Sage Weilec302642009-12-22 10:43:42 -0800689 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700690}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700691EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700692
693/*
Sage Weil31b80062009-10-06 11:31:13 -0700694 * Reopen a closed connection, with a new peer address.
695 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700696void ceph_con_open(struct ceph_connection *con,
697 __u8 entity_type, __u64 entity_num,
698 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700699{
Sage Weil54691552012-07-30 16:21:40 -0700700 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700701 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700702
Alex Elder122070a2012-12-26 10:43:57 -0600703 WARN_ON(con->state != CON_STATE_CLOSED);
Sage Weil8dacc7d2012-07-20 17:24:40 -0700704 con->state = CON_STATE_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500705
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700706 con->peer_name.type = (__u8) entity_type;
707 con->peer_name.num = cpu_to_le64(entity_num);
708
Sage Weil31b80062009-10-06 11:31:13 -0700709 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800710 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700711 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700712 queue_con(con);
713}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700714EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700715
716/*
Sage Weil87b315a2010-03-22 14:51:18 -0700717 * return true if this connection ever successfully opened
718 */
719bool ceph_con_opened(struct ceph_connection *con)
720{
721 return con->connect_seq > 0;
722}
723
724/*
Sage Weil31b80062009-10-06 11:31:13 -0700725 * initialize a new connection.
726 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500727void ceph_con_init(struct ceph_connection *con, void *private,
728 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700729 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700730{
731 dout("con_init %p\n", con);
732 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500733 con->private = private;
734 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700735 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500736
737 con_sock_state_init(con);
738
Sage Weilec302642009-12-22 10:43:42 -0800739 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700740 INIT_LIST_HEAD(&con->out_queue);
741 INIT_LIST_HEAD(&con->out_sent);
Ilya Dryomov68931622015-07-03 15:44:41 +0300742 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
Alex Eldera5988c42012-05-29 11:04:58 -0500743
Sage Weil8dacc7d2012-07-20 17:24:40 -0700744 con->state = CON_STATE_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700745}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700746EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700747
748
749/*
750 * We maintain a global counter to order connection attempts. Get
751 * a unique seq greater than @gt.
752 */
753static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
754{
755 u32 ret;
756
757 spin_lock(&msgr->global_seq_lock);
758 if (msgr->global_seq < gt)
759 msgr->global_seq = gt;
760 ret = ++msgr->global_seq;
761 spin_unlock(&msgr->global_seq_lock);
762 return ret;
763}
764
Alex Eldere2200422012-05-23 14:35:23 -0500765static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600766{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300767 BUG_ON(con->out_skip);
768
Alex Elder859eb792012-02-14 14:05:33 -0600769 con->out_kvec_left = 0;
770 con->out_kvec_bytes = 0;
771 con->out_kvec_cur = &con->out_kvec[0];
772}
773
Alex Eldere2200422012-05-23 14:35:23 -0500774static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600775 size_t size, void *data)
776{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300777 int index = con->out_kvec_left;
Alex Elder859eb792012-02-14 14:05:33 -0600778
Ilya Dryomov67645d72015-12-28 13:18:34 +0300779 BUG_ON(con->out_skip);
Alex Elder859eb792012-02-14 14:05:33 -0600780 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
781
782 con->out_kvec[index].iov_len = size;
783 con->out_kvec[index].iov_base = data;
784 con->out_kvec_left++;
785 con->out_kvec_bytes += size;
786}
Sage Weil31b80062009-10-06 11:31:13 -0700787
Ilya Dryomov67645d72015-12-28 13:18:34 +0300788/*
789 * Chop off a kvec from the end. Return residual number of bytes for
790 * that kvec, i.e. how many bytes would have been written if the kvec
791 * hadn't been nuked.
792 */
793static int con_out_kvec_skip(struct ceph_connection *con)
794{
795 int off = con->out_kvec_cur - con->out_kvec;
796 int skip = 0;
797
798 if (con->out_kvec_bytes > 0) {
799 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
800 BUG_ON(con->out_kvec_bytes < skip);
801 BUG_ON(!con->out_kvec_left);
802 con->out_kvec_bytes -= skip;
803 con->out_kvec_left--;
804 }
805
806 return skip;
807}
808
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500809#ifdef CONFIG_BLOCK
Alex Elder6aaa4512013-03-06 23:39:39 -0600810
811/*
812 * For a bio data item, a piece is whatever remains of the next
813 * entry in the current bio iovec, or the first entry in the next
814 * bio in the list.
815 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500816static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500817 size_t length)
Alex Elder6aaa4512013-03-06 23:39:39 -0600818{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500819 struct ceph_msg_data *data = cursor->data;
Alex Elder6aaa4512013-03-06 23:39:39 -0600820 struct bio *bio;
821
822 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
823
824 bio = data->bio;
825 BUG_ON(!bio);
Alex Elder6aaa4512013-03-06 23:39:39 -0600826
Alex Elderca8b3a62013-04-05 14:46:01 -0500827 cursor->resid = min(length, data->bio_length);
Alex Elder6aaa4512013-03-06 23:39:39 -0600828 cursor->bio = bio;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700829 cursor->bvec_iter = bio->bi_iter;
830 cursor->last_piece =
831 cursor->resid <= bio_iter_len(bio, cursor->bvec_iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600832}
833
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500834static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
Alex Elder6aaa4512013-03-06 23:39:39 -0600835 size_t *page_offset,
836 size_t *length)
837{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500838 struct ceph_msg_data *data = cursor->data;
Alex Elder6aaa4512013-03-06 23:39:39 -0600839 struct bio *bio;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700840 struct bio_vec bio_vec;
Alex Elder6aaa4512013-03-06 23:39:39 -0600841
842 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
843
844 bio = cursor->bio;
845 BUG_ON(!bio);
846
Kent Overstreetf38a5182013-08-07 14:30:24 -0700847 bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600848
Kent Overstreetf38a5182013-08-07 14:30:24 -0700849 *page_offset = (size_t) bio_vec.bv_offset;
Alex Elder6aaa4512013-03-06 23:39:39 -0600850 BUG_ON(*page_offset >= PAGE_SIZE);
Alex Elder25aff7c2013-03-11 23:34:22 -0500851 if (cursor->last_piece) /* pagelist offset is always 0 */
852 *length = cursor->resid;
853 else
Kent Overstreetf38a5182013-08-07 14:30:24 -0700854 *length = (size_t) bio_vec.bv_len;
Alex Elder25aff7c2013-03-11 23:34:22 -0500855 BUG_ON(*length > cursor->resid);
Alex Elder5df521b2013-03-30 15:09:59 -0500856 BUG_ON(*page_offset + *length > PAGE_SIZE);
Alex Elder6aaa4512013-03-06 23:39:39 -0600857
Kent Overstreetf38a5182013-08-07 14:30:24 -0700858 return bio_vec.bv_page;
Alex Elder6aaa4512013-03-06 23:39:39 -0600859}
860
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500861static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
862 size_t bytes)
Alex Elder6aaa4512013-03-06 23:39:39 -0600863{
Alex Elder6aaa4512013-03-06 23:39:39 -0600864 struct bio *bio;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700865 struct bio_vec bio_vec;
Alex Elder6aaa4512013-03-06 23:39:39 -0600866
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500867 BUG_ON(cursor->data->type != CEPH_MSG_DATA_BIO);
Alex Elder6aaa4512013-03-06 23:39:39 -0600868
869 bio = cursor->bio;
870 BUG_ON(!bio);
871
Kent Overstreetf38a5182013-08-07 14:30:24 -0700872 bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600873
874 /* Advance the cursor offset */
875
Alex Elder25aff7c2013-03-11 23:34:22 -0500876 BUG_ON(cursor->resid < bytes);
877 cursor->resid -= bytes;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700878
879 bio_advance_iter(bio, &cursor->bvec_iter, bytes);
880
881 if (bytes < bio_vec.bv_len)
Alex Elder6aaa4512013-03-06 23:39:39 -0600882 return false; /* more bytes to process in this segment */
883
884 /* Move on to the next segment, and possibly the next bio */
885
Kent Overstreetf38a5182013-08-07 14:30:24 -0700886 if (!cursor->bvec_iter.bi_size) {
Alex Elder6aaa4512013-03-06 23:39:39 -0600887 bio = bio->bi_next;
Ilya Dryomov0ec1d152014-02-05 15:19:55 +0200888 cursor->bio = bio;
889 if (bio)
890 cursor->bvec_iter = bio->bi_iter;
891 else
892 memset(&cursor->bvec_iter, 0,
893 sizeof(cursor->bvec_iter));
Alex Elder6aaa4512013-03-06 23:39:39 -0600894 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600895
Alex Elder25aff7c2013-03-11 23:34:22 -0500896 if (!cursor->last_piece) {
897 BUG_ON(!cursor->resid);
898 BUG_ON(!bio);
899 /* A short read is OK, so use <= rather than == */
Kent Overstreetf38a5182013-08-07 14:30:24 -0700900 if (cursor->resid <= bio_iter_len(bio, cursor->bvec_iter))
Alex Elder6aaa4512013-03-06 23:39:39 -0600901 cursor->last_piece = true;
Alex Elder25aff7c2013-03-11 23:34:22 -0500902 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600903
904 return true;
905}
Alex Elderea965712013-04-05 14:46:01 -0500906#endif /* CONFIG_BLOCK */
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500907
Alex Elderfe38a2b2013-03-06 23:39:39 -0600908/*
Alex Eldere766d7b2013-03-07 15:38:28 -0600909 * For a page array, a piece comes from the first page in the array
910 * that has not already been fully consumed.
911 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500912static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500913 size_t length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600914{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500915 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600916 int page_count;
917
918 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
919
920 BUG_ON(!data->pages);
921 BUG_ON(!data->length);
922
Alex Elderca8b3a62013-04-05 14:46:01 -0500923 cursor->resid = min(length, data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600924 page_count = calc_pages_for(data->alignment, (u64)data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600925 cursor->page_offset = data->alignment & ~PAGE_MASK;
926 cursor->page_index = 0;
Alex Elder56fc5652013-03-30 23:46:55 -0500927 BUG_ON(page_count > (int)USHRT_MAX);
928 cursor->page_count = (unsigned short)page_count;
929 BUG_ON(length > SIZE_MAX - cursor->page_offset);
Ilya Dryomov5f740d72014-08-08 12:43:39 +0400930 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600931}
932
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500933static struct page *
934ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
935 size_t *page_offset, size_t *length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600936{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500937 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600938
939 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
940
941 BUG_ON(cursor->page_index >= cursor->page_count);
942 BUG_ON(cursor->page_offset >= PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600943
944 *page_offset = cursor->page_offset;
Alex Elder25aff7c2013-03-11 23:34:22 -0500945 if (cursor->last_piece)
Alex Eldere766d7b2013-03-07 15:38:28 -0600946 *length = cursor->resid;
Alex Elder25aff7c2013-03-11 23:34:22 -0500947 else
Alex Eldere766d7b2013-03-07 15:38:28 -0600948 *length = PAGE_SIZE - *page_offset;
Alex Eldere766d7b2013-03-07 15:38:28 -0600949
950 return data->pages[cursor->page_index];
951}
952
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500953static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
Alex Eldere766d7b2013-03-07 15:38:28 -0600954 size_t bytes)
955{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500956 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
Alex Eldere766d7b2013-03-07 15:38:28 -0600957
958 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600959
960 /* Advance the cursor page offset */
961
962 cursor->resid -= bytes;
Alex Elder5df521b2013-03-30 15:09:59 -0500963 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
964 if (!bytes || cursor->page_offset)
Alex Eldere766d7b2013-03-07 15:38:28 -0600965 return false; /* more bytes to process in the current page */
966
Yan, Zhengd90deda2014-03-23 06:50:39 +0800967 if (!cursor->resid)
968 return false; /* no more data */
969
Alex Elder5df521b2013-03-30 15:09:59 -0500970 /* Move on to the next page; offset is already at 0 */
Alex Eldere766d7b2013-03-07 15:38:28 -0600971
972 BUG_ON(cursor->page_index >= cursor->page_count);
Alex Eldere766d7b2013-03-07 15:38:28 -0600973 cursor->page_index++;
Alex Elder25aff7c2013-03-11 23:34:22 -0500974 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600975
976 return true;
977}
978
979/*
Alex Elderdd236fc2013-03-06 23:39:39 -0600980 * For a pagelist, a piece is whatever remains to be consumed in the
981 * first page in the list, or the front of the next page.
Alex Elderfe38a2b2013-03-06 23:39:39 -0600982 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500983static void
984ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500985 size_t length)
Alex Elderfe38a2b2013-03-06 23:39:39 -0600986{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500987 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -0600988 struct ceph_pagelist *pagelist;
989 struct page *page;
990
Alex Elderdd236fc2013-03-06 23:39:39 -0600991 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
Alex Elderfe38a2b2013-03-06 23:39:39 -0600992
993 pagelist = data->pagelist;
994 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -0500995
996 if (!length)
Alex Elderfe38a2b2013-03-06 23:39:39 -0600997 return; /* pagelist can be assigned but empty */
998
999 BUG_ON(list_empty(&pagelist->head));
1000 page = list_first_entry(&pagelist->head, struct page, lru);
1001
Alex Elderca8b3a62013-04-05 14:46:01 -05001002 cursor->resid = min(length, pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001003 cursor->page = page;
1004 cursor->offset = 0;
Alex Eldera51b272e2013-04-19 15:34:49 -05001005 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001006}
1007
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001008static struct page *
1009ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1010 size_t *page_offset, size_t *length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001011{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001012 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001013 struct ceph_pagelist *pagelist;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001014
1015 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1016
1017 pagelist = data->pagelist;
1018 BUG_ON(!pagelist);
1019
1020 BUG_ON(!cursor->page);
Alex Elder25aff7c2013-03-11 23:34:22 -05001021 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001022
Alex Elder5df521b2013-03-30 15:09:59 -05001023 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001024 *page_offset = cursor->offset & ~PAGE_MASK;
Alex Elder5df521b2013-03-30 15:09:59 -05001025 if (cursor->last_piece)
Alex Elder25aff7c2013-03-11 23:34:22 -05001026 *length = cursor->resid;
1027 else
1028 *length = PAGE_SIZE - *page_offset;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001029
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001030 return cursor->page;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001031}
1032
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001033static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
Alex Elderdd236fc2013-03-06 23:39:39 -06001034 size_t bytes)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001035{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001036 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001037 struct ceph_pagelist *pagelist;
1038
1039 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1040
1041 pagelist = data->pagelist;
1042 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001043
1044 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001045 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1046
1047 /* Advance the cursor offset */
1048
Alex Elder25aff7c2013-03-11 23:34:22 -05001049 cursor->resid -= bytes;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001050 cursor->offset += bytes;
Alex Elder5df521b2013-03-30 15:09:59 -05001051 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001052 if (!bytes || cursor->offset & ~PAGE_MASK)
1053 return false; /* more bytes to process in the current page */
1054
Yan, Zhengd90deda2014-03-23 06:50:39 +08001055 if (!cursor->resid)
1056 return false; /* no more data */
1057
Alex Elderfe38a2b2013-03-06 23:39:39 -06001058 /* Move on to the next page */
1059
1060 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001061 cursor->page = list_next_entry(cursor->page, lru);
Alex Elder25aff7c2013-03-11 23:34:22 -05001062 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001063
1064 return true;
1065}
1066
Alex Elderdd236fc2013-03-06 23:39:39 -06001067/*
1068 * Message data is handled (sent or received) in pieces, where each
1069 * piece resides on a single page. The network layer might not
1070 * consume an entire piece at once. A data item's cursor keeps
1071 * track of which piece is next to process and how much remains to
1072 * be processed in that piece. It also tracks whether the current
1073 * piece is the last one in the data item.
1074 */
Alex Elderca8b3a62013-04-05 14:46:01 -05001075static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
Alex Elderdd236fc2013-03-06 23:39:39 -06001076{
Alex Elderca8b3a62013-04-05 14:46:01 -05001077 size_t length = cursor->total_resid;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001078
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001079 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001080 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001081 ceph_msg_data_pagelist_cursor_init(cursor, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001082 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001083 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001084 ceph_msg_data_pages_cursor_init(cursor, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001085 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001086#ifdef CONFIG_BLOCK
1087 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001088 ceph_msg_data_bio_cursor_init(cursor, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001089 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001090#endif /* CONFIG_BLOCK */
Alex Elder6aaa4512013-03-06 23:39:39 -06001091 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001092 default:
1093 /* BUG(); */
1094 break;
1095 }
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001096 cursor->need_crc = true;
Alex Elderdd236fc2013-03-06 23:39:39 -06001097}
1098
Alex Elderca8b3a62013-04-05 14:46:01 -05001099static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1100{
1101 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1102 struct ceph_msg_data *data;
1103
1104 BUG_ON(!length);
1105 BUG_ON(length > msg->data_length);
1106 BUG_ON(list_empty(&msg->data));
1107
Alex Elderca8b3a62013-04-05 14:46:01 -05001108 cursor->data_head = &msg->data;
1109 cursor->total_resid = length;
1110 data = list_first_entry(&msg->data, struct ceph_msg_data, links);
1111 cursor->data = data;
1112
1113 __ceph_msg_data_cursor_init(cursor);
1114}
1115
Alex Elderdd236fc2013-03-06 23:39:39 -06001116/*
1117 * Return the page containing the next piece to process for a given
1118 * data item, and supply the page offset and length of that piece.
1119 * Indicate whether this is the last piece in this data item.
1120 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001121static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1122 size_t *page_offset, size_t *length,
Alex Elderdd236fc2013-03-06 23:39:39 -06001123 bool *last_piece)
1124{
1125 struct page *page;
1126
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001127 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001128 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001129 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001130 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001131 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001132 page = ceph_msg_data_pages_next(cursor, page_offset, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001133 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001134#ifdef CONFIG_BLOCK
1135 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001136 page = ceph_msg_data_bio_next(cursor, page_offset, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001137 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001138#endif /* CONFIG_BLOCK */
Alex Elder6aaa4512013-03-06 23:39:39 -06001139 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001140 default:
1141 page = NULL;
1142 break;
1143 }
1144 BUG_ON(!page);
1145 BUG_ON(*page_offset + *length > PAGE_SIZE);
1146 BUG_ON(!*length);
1147 if (last_piece)
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001148 *last_piece = cursor->last_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001149
1150 return page;
1151}
1152
1153/*
1154 * Returns true if the result moves the cursor on to the next piece
1155 * of the data item.
1156 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001157static bool ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1158 size_t bytes)
Alex Elderdd236fc2013-03-06 23:39:39 -06001159{
1160 bool new_piece;
1161
Alex Elder25aff7c2013-03-11 23:34:22 -05001162 BUG_ON(bytes > cursor->resid);
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001163 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001164 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001165 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
Alex Elderdd236fc2013-03-06 23:39:39 -06001166 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001167 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001168 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
Alex Eldere766d7b2013-03-07 15:38:28 -06001169 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001170#ifdef CONFIG_BLOCK
1171 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001172 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
Alex Elder6aaa4512013-03-06 23:39:39 -06001173 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001174#endif /* CONFIG_BLOCK */
Alex Elder6aaa4512013-03-06 23:39:39 -06001175 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001176 default:
1177 BUG();
1178 break;
1179 }
Alex Elderca8b3a62013-04-05 14:46:01 -05001180 cursor->total_resid -= bytes;
Alex Elderdd236fc2013-03-06 23:39:39 -06001181
Alex Elderca8b3a62013-04-05 14:46:01 -05001182 if (!cursor->resid && cursor->total_resid) {
1183 WARN_ON(!cursor->last_piece);
1184 BUG_ON(list_is_last(&cursor->data->links, cursor->data_head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001185 cursor->data = list_next_entry(cursor->data, links);
Alex Elderca8b3a62013-04-05 14:46:01 -05001186 __ceph_msg_data_cursor_init(cursor);
Alex Eldera51b272e2013-04-19 15:34:49 -05001187 new_piece = true;
Alex Elderca8b3a62013-04-05 14:46:01 -05001188 }
Alex Eldera51b272e2013-04-19 15:34:49 -05001189 cursor->need_crc = new_piece;
Alex Elderca8b3a62013-04-05 14:46:01 -05001190
Alex Elderdd236fc2013-03-06 23:39:39 -06001191 return new_piece;
1192}
1193
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01001194static size_t sizeof_footer(struct ceph_connection *con)
1195{
1196 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1197 sizeof(struct ceph_msg_footer) :
1198 sizeof(struct ceph_msg_footer_old);
1199}
1200
Alex Elder98fa5dd2013-04-02 12:09:50 -05001201static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
Alex Elder739c9052012-06-11 14:57:13 -05001202{
Alex Elder739c9052012-06-11 14:57:13 -05001203 BUG_ON(!msg);
Alex Elder25aff7c2013-03-11 23:34:22 -05001204 BUG_ON(!data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001205
Alex Elder4c59b4a2013-03-11 23:34:23 -05001206 /* Initialize data cursor */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001207
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001208 ceph_msg_data_cursor_init(msg, (size_t)data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001209}
1210
Sage Weil31b80062009-10-06 11:31:13 -07001211/*
1212 * Prepare footer for currently outgoing message, and finish things
1213 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1214 */
Alex Elder859eb792012-02-14 14:05:33 -06001215static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001216{
1217 struct ceph_msg *m = con->out_msg;
1218
Alex Elderfd154f32012-06-11 14:57:13 -05001219 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1220
Sage Weil31b80062009-10-06 11:31:13 -07001221 dout("prepare_write_message_footer %p\n", con);
Ilya Dryomov89f08172016-02-20 15:56:07 +01001222 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
Yan, Zheng33d07332014-11-04 16:33:37 +08001223 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1224 if (con->ops->sign_message)
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01001225 con->ops->sign_message(m);
Yan, Zheng33d07332014-11-04 16:33:37 +08001226 else
1227 m->footer.sig = 0;
Yan, Zheng33d07332014-11-04 16:33:37 +08001228 } else {
1229 m->old_footer.flags = m->footer.flags;
Yan, Zheng33d07332014-11-04 16:33:37 +08001230 }
Sage Weil31b80062009-10-06 11:31:13 -07001231 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -08001232 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -07001233}
1234
1235/*
1236 * Prepare headers for the next outgoing message.
1237 */
1238static void prepare_write_message(struct ceph_connection *con)
1239{
1240 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -06001241 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001242
Alex Eldere2200422012-05-23 14:35:23 -05001243 con_out_kvec_reset(con);
Sage Weilc86a2932009-12-14 14:04:30 -08001244 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -07001245
1246 /* Sneak an ack in there first? If we can get it into the same
1247 * TCP packet that's a good thing. */
1248 if (con->in_seq > con->in_seq_acked) {
1249 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -05001250 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001251 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001252 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001253 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001254 }
1255
Alex Elder38941f82012-06-01 14:56:43 -05001256 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -06001257 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -08001258 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -05001259 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -07001260
1261 /* put message on sent list */
1262 ceph_msg_get(m);
1263 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -07001264
Sage Weile84346b2010-05-11 21:20:38 -07001265 /*
1266 * only assign outgoing seq # if we haven't sent this message
1267 * yet. if it is requeued, resend with it's original seq.
1268 */
1269 if (m->needs_out_seq) {
1270 m->hdr.seq = cpu_to_le64(++con->out_seq);
1271 m->needs_out_seq = false;
1272 }
Alex Elder98fa5dd2013-04-02 12:09:50 -05001273 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
Sage Weil31b80062009-10-06 11:31:13 -07001274
Alex Elder98fa5dd2013-04-02 12:09:50 -05001275 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
Sage Weil31b80062009-10-06 11:31:13 -07001276 m, con->out_seq, le16_to_cpu(m->hdr.type),
1277 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
Alex Elder98fa5dd2013-04-02 12:09:50 -05001278 m->data_length);
Sage Weil31b80062009-10-06 11:31:13 -07001279 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
1280
1281 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -05001282 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001283 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
Alex Eldere2200422012-05-23 14:35:23 -05001284 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -06001285
Sage Weil31b80062009-10-06 11:31:13 -07001286 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -05001287 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -06001288 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -07001289
Ilya Dryomov67645d72015-12-28 13:18:34 +03001290 /* fill in hdr crc and finalize hdr */
Alex Eldera9a0c512012-02-15 07:43:54 -06001291 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1292 con->out_msg->hdr.crc = cpu_to_le32(crc);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001293 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
Alex Eldera9a0c512012-02-15 07:43:54 -06001294
Ilya Dryomov67645d72015-12-28 13:18:34 +03001295 /* fill in front and middle crc, footer */
Alex Eldera9a0c512012-02-15 07:43:54 -06001296 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1297 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1298 if (m->middle) {
1299 crc = crc32c(0, m->middle->vec.iov_base,
1300 m->middle->vec.iov_len);
1301 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1302 } else
Sage Weil31b80062009-10-06 11:31:13 -07001303 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -05001304 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -07001305 le32_to_cpu(con->out_msg->footer.front_crc),
1306 le32_to_cpu(con->out_msg->footer.middle_crc));
Ilya Dryomov67645d72015-12-28 13:18:34 +03001307 con->out_msg->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001308
1309 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -05001310 con->out_msg->footer.data_crc = 0;
Alex Elder98fa5dd2013-04-02 12:09:50 -05001311 if (m->data_length) {
1312 prepare_message_data(con->out_msg, m->data_length);
Alex Elder78625052013-03-06 23:39:39 -06001313 con->out_more = 1; /* data + footer will follow */
1314 } else {
Sage Weil31b80062009-10-06 11:31:13 -07001315 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -06001316 prepare_write_message_footer(con);
Alex Elder78625052013-03-06 23:39:39 -06001317 }
Sage Weil31b80062009-10-06 11:31:13 -07001318
Alex Elderc9ffc772013-02-20 10:25:12 -06001319 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001320}
1321
1322/*
1323 * Prepare an ack.
1324 */
1325static void prepare_write_ack(struct ceph_connection *con)
1326{
1327 dout("prepare_write_ack %p %llu -> %llu\n", con,
1328 con->in_seq_acked, con->in_seq);
1329 con->in_seq_acked = con->in_seq;
1330
Alex Eldere2200422012-05-23 14:35:23 -05001331 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001332
Alex Eldere2200422012-05-23 14:35:23 -05001333 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -06001334
Sage Weil31b80062009-10-06 11:31:13 -07001335 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001336 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001337 &con->out_temp_ack);
1338
Sage Weil31b80062009-10-06 11:31:13 -07001339 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elderc9ffc772013-02-20 10:25:12 -06001340 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001341}
1342
1343/*
Sage Weil3a230832013-03-25 08:47:40 -07001344 * Prepare to share the seq during handshake
1345 */
1346static void prepare_write_seq(struct ceph_connection *con)
1347{
1348 dout("prepare_write_seq %p %llu -> %llu\n", con,
1349 con->in_seq_acked, con->in_seq);
1350 con->in_seq_acked = con->in_seq;
1351
1352 con_out_kvec_reset(con);
1353
1354 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1355 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1356 &con->out_temp_ack);
1357
1358 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1359}
1360
1361/*
Sage Weil31b80062009-10-06 11:31:13 -07001362 * Prepare to write keepalive byte.
1363 */
1364static void prepare_write_keepalive(struct ceph_connection *con)
1365{
1366 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -05001367 con_out_kvec_reset(con);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001368 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001369 struct timespec now = CURRENT_TIME;
1370
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001371 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001372 ceph_encode_timespec(&con->out_temp_keepalive2, &now);
1373 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1374 &con->out_temp_keepalive2);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001375 } else {
1376 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1377 }
Alex Elderc9ffc772013-02-20 10:25:12 -06001378 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001379}
1380
1381/*
1382 * Connection negotiation.
1383 */
1384
Alex Elderdac1e712012-05-16 15:16:39 -05001385static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
1386 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001387{
Alex Eldera3530df2012-05-16 15:16:39 -05001388 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -05001389
1390 if (!con->ops->get_authorizer) {
1391 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1392 con->out_connect.authorizer_len = 0;
Alex Elder729796b2012-05-16 15:16:39 -05001393 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -05001394 }
1395
1396 /* Can't hold the mutex while getting authorizer */
Sage Weilec302642009-12-22 10:43:42 -08001397 mutex_unlock(&con->mutex);
Alex Elderdac1e712012-05-16 15:16:39 -05001398 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -08001399 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001400
Alex Eldera3530df2012-05-16 15:16:39 -05001401 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -05001402 return auth;
Sage Weil8dacc7d2012-07-20 17:24:40 -07001403 if (con->state != CON_STATE_NEGOTIATING)
Alex Elder729796b2012-05-16 15:16:39 -05001404 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -07001405
Alex Elder8f43fb52012-05-16 15:16:39 -05001406 con->auth_reply_buf = auth->authorizer_reply_buf;
1407 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
Alex Elder729796b2012-05-16 15:16:39 -05001408 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001409}
1410
Sage Weil31b80062009-10-06 11:31:13 -07001411/*
1412 * We connected to a peer and are saying hello.
1413 */
Alex Eldere825a662012-05-16 15:16:38 -05001414static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001415{
Alex Eldere2200422012-05-23 14:35:23 -05001416 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1417 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -05001418 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -08001419
Sage Weileed0ef22009-11-10 14:34:36 -08001420 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001421 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weileed0ef22009-11-10 14:34:36 -08001422}
1423
Alex Eldere825a662012-05-16 15:16:38 -05001424static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -08001425{
Eric Dumazet95c96172012-04-15 05:58:06 +00001426 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001427 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -05001428 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -05001429 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -07001430
1431 switch (con->peer_name.type) {
1432 case CEPH_ENTITY_TYPE_MON:
1433 proto = CEPH_MONC_PROTOCOL;
1434 break;
1435 case CEPH_ENTITY_TYPE_OSD:
1436 proto = CEPH_OSDC_PROTOCOL;
1437 break;
1438 case CEPH_ENTITY_TYPE_MDS:
1439 proto = CEPH_MDSC_PROTOCOL;
1440 break;
1441 default:
1442 BUG();
1443 }
1444
1445 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1446 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001447
Ilya Dryomov859bff52015-10-28 23:50:58 +01001448 con->out_connect.features =
1449 cpu_to_le64(from_msgr(con->msgr)->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -07001450 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1451 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1452 con->out_connect.global_seq = cpu_to_le32(global_seq);
1453 con->out_connect.protocol_version = cpu_to_le32(proto);
1454 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001455
Alex Elderdac1e712012-05-16 15:16:39 -05001456 auth_proto = CEPH_AUTH_UNKNOWN;
1457 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -05001458 if (IS_ERR(auth))
1459 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -05001460
Alex Elderdac1e712012-05-16 15:16:39 -05001461 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -05001462 con->out_connect.authorizer_len = auth ?
1463 cpu_to_le32(auth->authorizer_buf_len) : 0;
1464
Alex Eldere2200422012-05-23 14:35:23 -05001465 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -05001466 &con->out_connect);
1467 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -05001468 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -05001469 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -06001470
Sage Weil31b80062009-10-06 11:31:13 -07001471 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001472 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001473
Alex Eldere10c7582012-05-16 15:16:38 -05001474 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07001475}
1476
Sage Weil31b80062009-10-06 11:31:13 -07001477/*
1478 * write as much of pending kvecs to the socket as we can.
1479 * 1 -> done
1480 * 0 -> socket full, but more to do
1481 * <0 -> error
1482 */
1483static int write_partial_kvec(struct ceph_connection *con)
1484{
1485 int ret;
1486
1487 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1488 while (con->out_kvec_bytes > 0) {
1489 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1490 con->out_kvec_left, con->out_kvec_bytes,
1491 con->out_more);
1492 if (ret <= 0)
1493 goto out;
1494 con->out_kvec_bytes -= ret;
1495 if (con->out_kvec_bytes == 0)
1496 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -06001497
1498 /* account for full iov entries consumed */
1499 while (ret >= con->out_kvec_cur->iov_len) {
1500 BUG_ON(!con->out_kvec_left);
1501 ret -= con->out_kvec_cur->iov_len;
1502 con->out_kvec_cur++;
1503 con->out_kvec_left--;
1504 }
1505 /* and for a partially-consumed entry */
1506 if (ret) {
1507 con->out_kvec_cur->iov_len -= ret;
1508 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -07001509 }
1510 }
1511 con->out_kvec_left = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001512 ret = 1;
1513out:
1514 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1515 con->out_kvec_bytes, con->out_kvec_left, ret);
1516 return ret; /* done! */
1517}
1518
Alex Elder35b62802013-03-08 20:59:00 -06001519static u32 ceph_crc32c_page(u32 crc, struct page *page,
1520 unsigned int page_offset,
1521 unsigned int length)
1522{
1523 char *kaddr;
1524
1525 kaddr = kmap(page);
1526 BUG_ON(kaddr == NULL);
1527 crc = crc32c(crc, kaddr + page_offset, length);
1528 kunmap(page);
1529
1530 return crc;
1531}
Sage Weil31b80062009-10-06 11:31:13 -07001532/*
1533 * Write as much message data payload as we can. If we finish, queue
1534 * up the footer.
1535 * 1 -> done, footer is now queued in out_kvec[].
1536 * 0 -> socket full, but more to do
1537 * <0 -> error
1538 */
Alex Elder34d2d202013-03-08 20:58:59 -06001539static int write_partial_message_data(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001540{
1541 struct ceph_msg *msg = con->out_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001542 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01001543 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elderf5db90b2013-03-11 23:34:23 -05001544 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001545
Alex Elder859a35d2013-03-11 23:34:23 -05001546 dout("%s %p msg %p\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001547
Alex Elder5240d9f2013-03-14 14:09:06 -05001548 if (list_empty(&msg->data))
Alex Elder4c59b4a2013-03-11 23:34:23 -05001549 return -EINVAL;
1550
Alex Elder5821bd82012-06-11 14:57:13 -05001551 /*
1552 * Iterate through each page that contains data to be
1553 * written, and send as much as possible for each.
1554 *
1555 * If we are calculating the data crc (the default), we will
1556 * need to map the page. If we have no pages, they have
1557 * been revoked, so use the zero page.
1558 */
Alex Elderf5db90b2013-03-11 23:34:23 -05001559 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
Alex Elder643c68a2013-03-11 23:34:23 -05001560 while (cursor->resid) {
Alex Elder8a166d02013-03-08 13:35:36 -06001561 struct page *page;
Alex Eldere387d522013-03-06 23:39:38 -06001562 size_t page_offset;
1563 size_t length;
Alex Elder8a166d02013-03-08 13:35:36 -06001564 bool last_piece;
Alex Elder8ea299b2013-03-11 23:34:23 -05001565 bool need_crc;
Alex Elderf5db90b2013-03-11 23:34:23 -05001566 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001567
Shraddha Barke343128c2015-10-19 21:59:00 +05301568 page = ceph_msg_data_next(cursor, &page_offset, &length,
1569 &last_piece);
Alex Eldere387d522013-03-06 23:39:38 -06001570 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
Benoît Canetc2cfa192015-06-25 20:32:34 +03001571 length, !last_piece);
Alex Elderf5db90b2013-03-11 23:34:23 -05001572 if (ret <= 0) {
1573 if (do_datacrc)
1574 msg->footer.data_crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -07001575
Alex Elderf5db90b2013-03-11 23:34:23 -05001576 return ret;
1577 }
Alex Elder143334f2013-03-29 11:44:10 -05001578 if (do_datacrc && cursor->need_crc)
1579 crc = ceph_crc32c_page(crc, page, page_offset, length);
Shraddha Barke343128c2015-10-19 21:59:00 +05301580 need_crc = ceph_msg_data_advance(cursor, (size_t)ret);
Sage Weil31b80062009-10-06 11:31:13 -07001581 }
1582
Alex Elder34d2d202013-03-08 20:58:59 -06001583 dout("%s %p msg %p done\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001584
1585 /* prepare and queue up footer, too */
Alex Elderf5db90b2013-03-11 23:34:23 -05001586 if (do_datacrc)
1587 msg->footer.data_crc = cpu_to_le32(crc);
1588 else
Alex Elder84ca8fc2012-06-11 14:57:13 -05001589 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001590 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001591 prepare_write_message_footer(con);
Alex Elderf5db90b2013-03-11 23:34:23 -05001592
1593 return 1; /* must return > 0 to indicate success */
Sage Weil31b80062009-10-06 11:31:13 -07001594}
1595
1596/*
1597 * write some zeros
1598 */
1599static int write_partial_skip(struct ceph_connection *con)
1600{
1601 int ret;
1602
Ilya Dryomov67645d72015-12-28 13:18:34 +03001603 dout("%s %p %d left\n", __func__, con, con->out_skip);
Sage Weil31b80062009-10-06 11:31:13 -07001604 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001605 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001606
Alex Eldere1dcb122013-03-06 23:39:38 -06001607 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
Sage Weil31b80062009-10-06 11:31:13 -07001608 if (ret <= 0)
1609 goto out;
1610 con->out_skip -= ret;
1611 }
1612 ret = 1;
1613out:
1614 return ret;
1615}
1616
1617/*
1618 * Prepare to read connection handshake, or an ack.
1619 */
Sage Weileed0ef22009-11-10 14:34:36 -08001620static void prepare_read_banner(struct ceph_connection *con)
1621{
1622 dout("prepare_read_banner %p\n", con);
1623 con->in_base_pos = 0;
1624}
1625
Sage Weil31b80062009-10-06 11:31:13 -07001626static void prepare_read_connect(struct ceph_connection *con)
1627{
1628 dout("prepare_read_connect %p\n", con);
1629 con->in_base_pos = 0;
1630}
1631
1632static void prepare_read_ack(struct ceph_connection *con)
1633{
1634 dout("prepare_read_ack %p\n", con);
1635 con->in_base_pos = 0;
1636}
1637
Sage Weil3a230832013-03-25 08:47:40 -07001638static void prepare_read_seq(struct ceph_connection *con)
1639{
1640 dout("prepare_read_seq %p\n", con);
1641 con->in_base_pos = 0;
1642 con->in_tag = CEPH_MSGR_TAG_SEQ;
1643}
1644
Sage Weil31b80062009-10-06 11:31:13 -07001645static void prepare_read_tag(struct ceph_connection *con)
1646{
1647 dout("prepare_read_tag %p\n", con);
1648 con->in_base_pos = 0;
1649 con->in_tag = CEPH_MSGR_TAG_READY;
1650}
1651
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001652static void prepare_read_keepalive_ack(struct ceph_connection *con)
1653{
1654 dout("prepare_read_keepalive_ack %p\n", con);
1655 con->in_base_pos = 0;
1656}
1657
Sage Weil31b80062009-10-06 11:31:13 -07001658/*
1659 * Prepare to read a message.
1660 */
1661static int prepare_read_message(struct ceph_connection *con)
1662{
1663 dout("prepare_read_message %p\n", con);
1664 BUG_ON(con->in_msg != NULL);
1665 con->in_base_pos = 0;
1666 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1667 return 0;
1668}
1669
1670
1671static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001672 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001673{
Alex Eldere6cee712012-05-10 10:29:50 -05001674 while (con->in_base_pos < end) {
1675 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001676 int have = size - left;
1677 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1678 if (ret <= 0)
1679 return ret;
1680 con->in_base_pos += ret;
1681 }
1682 return 1;
1683}
1684
1685
1686/*
1687 * Read all or part of the connect-side handshake on a new connection
1688 */
Sage Weileed0ef22009-11-10 14:34:36 -08001689static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001690{
Alex Elderfd516532012-05-10 10:29:50 -05001691 int size;
1692 int end;
1693 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001694
Sage Weileed0ef22009-11-10 14:34:36 -08001695 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001696
1697 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001698 size = strlen(CEPH_BANNER);
1699 end = size;
1700 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001701 if (ret <= 0)
1702 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001703
1704 size = sizeof (con->actual_peer_addr);
1705 end += size;
1706 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001707 if (ret <= 0)
1708 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001709
1710 size = sizeof (con->peer_addr_for_me);
1711 end += size;
1712 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001713 if (ret <= 0)
1714 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001715
Sage Weileed0ef22009-11-10 14:34:36 -08001716out:
1717 return ret;
1718}
1719
1720static int read_partial_connect(struct ceph_connection *con)
1721{
Alex Elderfd516532012-05-10 10:29:50 -05001722 int size;
1723 int end;
1724 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001725
1726 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1727
Alex Elderfd516532012-05-10 10:29:50 -05001728 size = sizeof (con->in_reply);
1729 end = size;
1730 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001731 if (ret <= 0)
1732 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001733
1734 size = le32_to_cpu(con->in_reply.authorizer_len);
1735 end += size;
1736 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001737 if (ret <= 0)
1738 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001739
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001740 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1741 con, (int)con->in_reply.tag,
1742 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001743 le32_to_cpu(con->in_reply.global_seq));
1744out:
1745 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001746
Sage Weil31b80062009-10-06 11:31:13 -07001747}
1748
1749/*
1750 * Verify the hello banner looks okay.
1751 */
1752static int verify_hello(struct ceph_connection *con)
1753{
1754 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001755 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001756 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001757 con->error_msg = "protocol error, bad banner";
1758 return -1;
1759 }
1760 return 0;
1761}
1762
1763static bool addr_is_blank(struct sockaddr_storage *ss)
1764{
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001765 struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1766 struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1767
Sage Weil31b80062009-10-06 11:31:13 -07001768 switch (ss->ss_family) {
1769 case AF_INET:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001770 return addr->s_addr == htonl(INADDR_ANY);
Sage Weil31b80062009-10-06 11:31:13 -07001771 case AF_INET6:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001772 return ipv6_addr_any(addr6);
1773 default:
1774 return true;
Sage Weil31b80062009-10-06 11:31:13 -07001775 }
Sage Weil31b80062009-10-06 11:31:13 -07001776}
1777
1778static int addr_port(struct sockaddr_storage *ss)
1779{
1780 switch (ss->ss_family) {
1781 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001782 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001783 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001784 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001785 }
1786 return 0;
1787}
1788
1789static void addr_set_port(struct sockaddr_storage *ss, int p)
1790{
1791 switch (ss->ss_family) {
1792 case AF_INET:
1793 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001794 break;
Sage Weil31b80062009-10-06 11:31:13 -07001795 case AF_INET6:
1796 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001797 break;
Sage Weil31b80062009-10-06 11:31:13 -07001798 }
1799}
1800
1801/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001802 * Unlike other *_pton function semantics, zero indicates success.
1803 */
1804static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1805 char delim, const char **ipend)
1806{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001807 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1808 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001809
1810 memset(ss, 0, sizeof(*ss));
1811
1812 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1813 ss->ss_family = AF_INET;
1814 return 0;
1815 }
1816
1817 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1818 ss->ss_family = AF_INET6;
1819 return 0;
1820 }
1821
1822 return -EINVAL;
1823}
1824
1825/*
1826 * Extract hostname string and resolve using kernel DNS facility.
1827 */
1828#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1829static int ceph_dns_resolve_name(const char *name, size_t namelen,
1830 struct sockaddr_storage *ss, char delim, const char **ipend)
1831{
1832 const char *end, *delim_p;
1833 char *colon_p, *ip_addr = NULL;
1834 int ip_len, ret;
1835
1836 /*
1837 * The end of the hostname occurs immediately preceding the delimiter or
1838 * the port marker (':') where the delimiter takes precedence.
1839 */
1840 delim_p = memchr(name, delim, namelen);
1841 colon_p = memchr(name, ':', namelen);
1842
1843 if (delim_p && colon_p)
1844 end = delim_p < colon_p ? delim_p : colon_p;
1845 else if (!delim_p && colon_p)
1846 end = colon_p;
1847 else {
1848 end = delim_p;
1849 if (!end) /* case: hostname:/ */
1850 end = name + namelen;
1851 }
1852
1853 if (end <= name)
1854 return -EINVAL;
1855
1856 /* do dns_resolve upcall */
1857 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1858 if (ip_len > 0)
1859 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1860 else
1861 ret = -ESRCH;
1862
1863 kfree(ip_addr);
1864
1865 *ipend = end;
1866
1867 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1868 ret, ret ? "failed" : ceph_pr_addr(ss));
1869
1870 return ret;
1871}
1872#else
1873static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1874 struct sockaddr_storage *ss, char delim, const char **ipend)
1875{
1876 return -EINVAL;
1877}
1878#endif
1879
1880/*
1881 * Parse a server name (IP or hostname). If a valid IP address is not found
1882 * then try to extract a hostname to resolve using userspace DNS upcall.
1883 */
1884static int ceph_parse_server_name(const char *name, size_t namelen,
1885 struct sockaddr_storage *ss, char delim, const char **ipend)
1886{
1887 int ret;
1888
1889 ret = ceph_pton(name, namelen, ss, delim, ipend);
1890 if (ret)
1891 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1892
1893 return ret;
1894}
1895
1896/*
Sage Weil31b80062009-10-06 11:31:13 -07001897 * Parse an ip[:port] list into an addr array. Use the default
1898 * monitor port if a port isn't specified.
1899 */
1900int ceph_parse_ips(const char *c, const char *end,
1901 struct ceph_entity_addr *addr,
1902 int max_count, int *count)
1903{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001904 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001905 const char *p = c;
1906
1907 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1908 for (i = 0; i < max_count; i++) {
1909 const char *ipend;
1910 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001911 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001912 char delim = ',';
1913
1914 if (*p == '[') {
1915 delim = ']';
1916 p++;
1917 }
Sage Weil31b80062009-10-06 11:31:13 -07001918
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001919 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1920 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001921 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001922 ret = -EINVAL;
1923
Sage Weil31b80062009-10-06 11:31:13 -07001924 p = ipend;
1925
Sage Weil39139f62010-07-08 09:54:52 -07001926 if (delim == ']') {
1927 if (*p != ']') {
1928 dout("missing matching ']'\n");
1929 goto bad;
1930 }
1931 p++;
1932 }
1933
Sage Weil31b80062009-10-06 11:31:13 -07001934 /* port? */
1935 if (p < end && *p == ':') {
1936 port = 0;
1937 p++;
1938 while (p < end && *p >= '0' && *p <= '9') {
1939 port = (port * 10) + (*p - '0');
1940 p++;
1941 }
Ilya Dryomovf48db1e2013-12-30 19:21:29 +02001942 if (port == 0)
1943 port = CEPH_MON_PORT;
1944 else if (port > 65535)
Sage Weil31b80062009-10-06 11:31:13 -07001945 goto bad;
1946 } else {
1947 port = CEPH_MON_PORT;
1948 }
1949
1950 addr_set_port(ss, port);
1951
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001952 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001953
1954 if (p == end)
1955 break;
1956 if (*p != ',')
1957 goto bad;
1958 p++;
1959 }
1960
1961 if (p != end)
1962 goto bad;
1963
1964 if (count)
1965 *count = i + 1;
1966 return 0;
1967
1968bad:
Sage Weil39139f62010-07-08 09:54:52 -07001969 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001970 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001971}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001972EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001973
Sage Weileed0ef22009-11-10 14:34:36 -08001974static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001975{
Sage Weileed0ef22009-11-10 14:34:36 -08001976 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001977
1978 if (verify_hello(con) < 0)
1979 return -1;
1980
Sage Weil63f2d212009-11-03 15:17:56 -08001981 ceph_decode_addr(&con->actual_peer_addr);
1982 ceph_decode_addr(&con->peer_addr_for_me);
1983
Sage Weil31b80062009-10-06 11:31:13 -07001984 /*
1985 * Make sure the other end is who we wanted. note that the other
1986 * end may not yet know their ip address, so if it's 0.0.0.0, give
1987 * them the benefit of the doubt.
1988 */
Sage Weil103e2d32010-01-07 16:12:36 -08001989 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1990 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001991 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1992 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001993 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
1994 ceph_pr_addr(&con->peer_addr.in_addr),
1995 (int)le32_to_cpu(con->peer_addr.nonce),
1996 ceph_pr_addr(&con->actual_peer_addr.in_addr),
1997 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001998 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001999 return -1;
2000 }
2001
2002 /*
2003 * did we learn our address?
2004 */
2005 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2006 int port = addr_port(&con->msgr->inst.addr.in_addr);
2007
2008 memcpy(&con->msgr->inst.addr.in_addr,
2009 &con->peer_addr_for_me.in_addr,
2010 sizeof(con->peer_addr_for_me.in_addr));
2011 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08002012 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08002013 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002014 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002015 }
2016
Sage Weileed0ef22009-11-10 14:34:36 -08002017 return 0;
2018}
2019
2020static int process_connect(struct ceph_connection *con)
2021{
Ilya Dryomov859bff52015-10-28 23:50:58 +01002022 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2023 u64 req_feat = from_msgr(con->msgr)->required_features;
Ilya Dryomov2b3e0c92013-12-24 21:19:24 +02002024 u64 server_feat = ceph_sanitize_features(
2025 le64_to_cpu(con->in_reply.features));
Sage Weil0da5d702011-05-19 11:21:05 -07002026 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08002027
Sage Weileed0ef22009-11-10 14:34:36 -08002028 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2029
Sage Weil31b80062009-10-06 11:31:13 -07002030 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08002031 case CEPH_MSGR_TAG_FEATURES:
2032 pr_err("%s%lld %s feature set mismatch,"
2033 " my %llx < server's %llx, missing %llx\n",
2034 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002035 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002036 sup_feat, server_feat, server_feat & ~sup_feat);
2037 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002038 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002039 return -1;
2040
Sage Weil31b80062009-10-06 11:31:13 -07002041 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07002042 pr_err("%s%lld %s protocol version mismatch,"
2043 " my %d != server's %d\n",
2044 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002045 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07002046 le32_to_cpu(con->out_connect.protocol_version),
2047 le32_to_cpu(con->in_reply.protocol_version));
2048 con->error_msg = "protocol version mismatch";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002049 reset_connection(con);
Sage Weil31b80062009-10-06 11:31:13 -07002050 return -1;
2051
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002052 case CEPH_MSGR_TAG_BADAUTHORIZER:
2053 con->auth_retry++;
2054 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2055 con->auth_retry);
2056 if (con->auth_retry == 2) {
2057 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002058 return -1;
2059 }
Jim Schutt6d4221b2012-08-10 10:37:38 -07002060 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002061 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002062 if (ret < 0)
2063 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07002064 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002065 break;
Sage Weil31b80062009-10-06 11:31:13 -07002066
2067 case CEPH_MSGR_TAG_RESETSESSION:
2068 /*
2069 * If we connected with a large connect_seq but the peer
2070 * has no record of a session with us (no connection, or
2071 * connect_seq == 0), they will send RESETSESION to indicate
2072 * that they must have reset their session, and may have
2073 * dropped messages.
2074 */
2075 dout("process_connect got RESET peer seq %u\n",
Sage Weil5bdca4e2012-07-10 11:53:34 -07002076 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002077 pr_err("%s%lld %s connection reset\n",
2078 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002079 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002080 reset_connection(con);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002081 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002082 ret = prepare_write_connect(con);
2083 if (ret < 0)
2084 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002085 prepare_read_connect(con);
2086
2087 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08002088 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002089 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2090 if (con->ops->peer_reset)
2091 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08002092 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002093 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07002094 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07002095 break;
2096
2097 case CEPH_MSGR_TAG_RETRY_SESSION:
2098 /*
2099 * If we sent a smaller connect_seq than the peer has, try
2100 * again with a larger value.
2101 */
Sage Weil5bdca4e2012-07-10 11:53:34 -07002102 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002103 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil5bdca4e2012-07-10 11:53:34 -07002104 le32_to_cpu(con->in_reply.connect_seq));
2105 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002106 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002107 ret = prepare_write_connect(con);
2108 if (ret < 0)
2109 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002110 prepare_read_connect(con);
2111 break;
2112
2113 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2114 /*
2115 * If we sent a smaller global_seq than the peer has, try
2116 * again with a larger value.
2117 */
Sage Weileed0ef22009-11-10 14:34:36 -08002118 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002119 con->peer_global_seq,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002120 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002121 get_global_seq(con->msgr,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002122 le32_to_cpu(con->in_reply.global_seq));
Jim Schutt6d4221b2012-08-10 10:37:38 -07002123 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002124 ret = prepare_write_connect(con);
2125 if (ret < 0)
2126 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002127 prepare_read_connect(con);
2128 break;
2129
Sage Weil3a230832013-03-25 08:47:40 -07002130 case CEPH_MSGR_TAG_SEQ:
Sage Weil31b80062009-10-06 11:31:13 -07002131 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08002132 if (req_feat & ~server_feat) {
2133 pr_err("%s%lld %s protocol feature mismatch,"
2134 " my required %llx > server's %llx, need %llx\n",
2135 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002136 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002137 req_feat, server_feat, req_feat & ~server_feat);
2138 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002139 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002140 return -1;
2141 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002142
Alex Elder122070a2012-12-26 10:43:57 -06002143 WARN_ON(con->state != CON_STATE_NEGOTIATING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002144 con->state = CON_STATE_OPEN;
Sage Weil20e55c42013-03-25 09:30:13 -07002145 con->auth_retry = 0; /* we authenticated; clear flag */
Sage Weil31b80062009-10-06 11:31:13 -07002146 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2147 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07002148 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07002149 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2150 con->peer_global_seq,
2151 le32_to_cpu(con->in_reply.connect_seq),
2152 con->connect_seq);
2153 WARN_ON(con->connect_seq !=
2154 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08002155
2156 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elderc9ffc772013-02-20 10:25:12 -06002157 con_flag_set(con, CON_FLAG_LOSSYTX);
Sage Weil92ac41d2009-12-14 14:56:56 -08002158
Sage Weil85effe12012-07-30 16:22:05 -07002159 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -07002160
Sage Weil3a230832013-03-25 08:47:40 -07002161 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2162 prepare_write_seq(con);
2163 prepare_read_seq(con);
2164 } else {
2165 prepare_read_tag(con);
2166 }
Sage Weil31b80062009-10-06 11:31:13 -07002167 break;
2168
2169 case CEPH_MSGR_TAG_WAIT:
2170 /*
2171 * If there is a connection race (we are opening
2172 * connections to each other), one of us may just have
2173 * to WAIT. This shouldn't happen if we are the
2174 * client.
2175 */
Sage Weil04177882011-05-12 15:33:17 -07002176 con->error_msg = "protocol error, got WAIT as client";
2177 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07002178
2179 default:
Sage Weil31b80062009-10-06 11:31:13 -07002180 con->error_msg = "protocol error, garbage tag during connect";
2181 return -1;
2182 }
2183 return 0;
2184}
2185
2186
2187/*
2188 * read (part of) an ack
2189 */
2190static int read_partial_ack(struct ceph_connection *con)
2191{
Alex Elderfd516532012-05-10 10:29:50 -05002192 int size = sizeof (con->in_temp_ack);
2193 int end = size;
Sage Weil31b80062009-10-06 11:31:13 -07002194
Alex Elderfd516532012-05-10 10:29:50 -05002195 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07002196}
2197
Sage Weil31b80062009-10-06 11:31:13 -07002198/*
2199 * We can finally discard anything that's been acked.
2200 */
2201static void process_ack(struct ceph_connection *con)
2202{
2203 struct ceph_msg *m;
2204 u64 ack = le64_to_cpu(con->in_temp_ack);
2205 u64 seq;
2206
Sage Weil31b80062009-10-06 11:31:13 -07002207 while (!list_empty(&con->out_sent)) {
2208 m = list_first_entry(&con->out_sent, struct ceph_msg,
2209 list_head);
2210 seq = le64_to_cpu(m->hdr.seq);
2211 if (seq > ack)
2212 break;
2213 dout("got ack for seq %llu type %d at %p\n", seq,
2214 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07002215 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07002216 ceph_msg_remove(m);
2217 }
Sage Weil31b80062009-10-06 11:31:13 -07002218 prepare_read_tag(con);
2219}
2220
2221
Yehuda Sadeh24504182010-01-08 13:58:34 -08002222static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002223 struct kvec *section,
2224 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002225{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002226 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07002227
Yehuda Sadeh24504182010-01-08 13:58:34 -08002228 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07002229
Yehuda Sadeh24504182010-01-08 13:58:34 -08002230 while (section->iov_len < sec_len) {
2231 BUG_ON(section->iov_base == NULL);
2232 left = sec_len - section->iov_len;
2233 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2234 section->iov_len, left);
2235 if (ret <= 0)
2236 return ret;
2237 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002238 }
Alex Elderfe3ad592012-02-15 07:43:54 -06002239 if (section->iov_len == sec_len)
2240 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002241
2242 return 1;
2243}
2244
Alex Elder34d2d202013-03-08 20:58:59 -06002245static int read_partial_msg_data(struct ceph_connection *con)
2246{
2247 struct ceph_msg *msg = con->in_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05002248 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002249 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elder686be202013-03-11 23:34:23 -05002250 struct page *page;
2251 size_t page_offset;
2252 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05002253 u32 crc = 0;
Alex Elder34d2d202013-03-08 20:58:59 -06002254 int ret;
2255
2256 BUG_ON(!msg);
Alex Elder5240d9f2013-03-14 14:09:06 -05002257 if (list_empty(&msg->data))
Alex Elder4c59b4a2013-03-11 23:34:23 -05002258 return -EIO;
Alex Elder34d2d202013-03-08 20:58:59 -06002259
Alex Elderf5db90b2013-03-11 23:34:23 -05002260 if (do_datacrc)
2261 crc = con->in_data_crc;
Alex Elder643c68a2013-03-11 23:34:23 -05002262 while (cursor->resid) {
Shraddha Barke343128c2015-10-19 21:59:00 +05302263 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Alex Elder686be202013-03-11 23:34:23 -05002264 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
Alex Elderf5db90b2013-03-11 23:34:23 -05002265 if (ret <= 0) {
2266 if (do_datacrc)
2267 con->in_data_crc = crc;
2268
Alex Elder686be202013-03-11 23:34:23 -05002269 return ret;
Alex Elderf5db90b2013-03-11 23:34:23 -05002270 }
Alex Elder686be202013-03-11 23:34:23 -05002271
2272 if (do_datacrc)
Alex Elderf5db90b2013-03-11 23:34:23 -05002273 crc = ceph_crc32c_page(crc, page, page_offset, ret);
Shraddha Barke343128c2015-10-19 21:59:00 +05302274 (void) ceph_msg_data_advance(cursor, (size_t)ret);
Alex Elder34d2d202013-03-08 20:58:59 -06002275 }
Alex Elderf5db90b2013-03-11 23:34:23 -05002276 if (do_datacrc)
2277 con->in_data_crc = crc;
Alex Elder34d2d202013-03-08 20:58:59 -06002278
2279 return 1; /* must return > 0 to indicate success */
2280}
2281
Sage Weil31b80062009-10-06 11:31:13 -07002282/*
2283 * read (part of) a message.
2284 */
Alex Elder686be202013-03-11 23:34:23 -05002285static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2286
Sage Weil31b80062009-10-06 11:31:13 -07002287static int read_partial_message(struct ceph_connection *con)
2288{
2289 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05002290 int size;
2291 int end;
Sage Weil31b80062009-10-06 11:31:13 -07002292 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00002293 unsigned int front_len, middle_len, data_len;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002294 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Yan, Zheng33d07332014-11-04 16:33:37 +08002295 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
Sage Weilae187562010-04-22 07:47:01 -07002296 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06002297 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07002298
2299 dout("read_partial_message con %p msg %p\n", con, m);
2300
2301 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05002302 size = sizeof (con->in_hdr);
2303 end = size;
2304 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05002305 if (ret <= 0)
2306 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06002307
2308 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2309 if (cpu_to_le32(crc) != con->in_hdr.crc) {
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002310 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
Alex Elderfe3ad592012-02-15 07:43:54 -06002311 crc, con->in_hdr.crc);
2312 return -EBADMSG;
2313 }
2314
Sage Weil31b80062009-10-06 11:31:13 -07002315 front_len = le32_to_cpu(con->in_hdr.front_len);
2316 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2317 return -EIO;
2318 middle_len = le32_to_cpu(con->in_hdr.middle_len);
Alex Elder7b11ba32013-03-08 18:51:03 -06002319 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
Sage Weil31b80062009-10-06 11:31:13 -07002320 return -EIO;
2321 data_len = le32_to_cpu(con->in_hdr.data_len);
2322 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2323 return -EIO;
2324
Sage Weilae187562010-04-22 07:47:01 -07002325 /* verify seq# */
2326 seq = le64_to_cpu(con->in_hdr.seq);
2327 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07002328 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07002329 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002330 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07002331 seq, con->in_seq + 1);
2332 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002333 sizeof_footer(con);
Sage Weilae187562010-04-22 07:47:01 -07002334 con->in_tag = CEPH_MSGR_TAG_READY;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002335 return 1;
Sage Weilae187562010-04-22 07:47:01 -07002336 } else if ((s64)seq - (s64)con->in_seq > 1) {
2337 pr_err("read_partial_message bad seq %lld expected %lld\n",
2338 seq, con->in_seq + 1);
2339 con->error_msg = "bad message sequence # for incoming message";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002340 return -EBADE;
Sage Weilae187562010-04-22 07:47:01 -07002341 }
2342
Sage Weil31b80062009-10-06 11:31:13 -07002343 /* allocate message? */
2344 if (!con->in_msg) {
Sage Weil4740a622012-07-30 18:19:30 -07002345 int skip = 0;
2346
Sage Weil31b80062009-10-06 11:31:13 -07002347 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
Alex Elder6ebc8b32013-03-08 18:51:03 -06002348 front_len, data_len);
Sage Weil4740a622012-07-30 18:19:30 -07002349 ret = ceph_con_in_msg_alloc(con, &skip);
2350 if (ret < 0)
2351 return ret;
Alex Elderf759ebb2013-04-05 14:46:01 -05002352
2353 BUG_ON(!con->in_msg ^ skip);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002354 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07002355 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07002356 dout("alloc_msg said skip message\n");
Sage Weil31b80062009-10-06 11:31:13 -07002357 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002358 sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07002359 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002360 con->in_seq++;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002361 return 1;
Sage Weil31b80062009-10-06 11:31:13 -07002362 }
Alex Elder38941f82012-06-01 14:56:43 -05002363
Sage Weil4740a622012-07-30 18:19:30 -07002364 BUG_ON(!con->in_msg);
Alex Elder38941f82012-06-01 14:56:43 -05002365 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002366 m = con->in_msg;
2367 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002368 if (m->middle)
2369 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002370
Alex Elder78625052013-03-06 23:39:39 -06002371 /* prepare for data payload, if any */
Sage Weila4107022012-07-30 16:20:25 -07002372
Alex Elder78625052013-03-06 23:39:39 -06002373 if (data_len)
Alex Elder98fa5dd2013-04-02 12:09:50 -05002374 prepare_message_data(con->in_msg, data_len);
Sage Weil31b80062009-10-06 11:31:13 -07002375 }
2376
2377 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002378 ret = read_partial_message_section(con, &m->front, front_len,
2379 &con->in_front_crc);
2380 if (ret <= 0)
2381 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002382
2383 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002384 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07002385 ret = read_partial_message_section(con, &m->middle->vec,
2386 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002387 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07002388 if (ret <= 0)
2389 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002390 }
2391
2392 /* (page) data */
Alex Elder34d2d202013-03-08 20:58:59 -06002393 if (data_len) {
2394 ret = read_partial_msg_data(con);
2395 if (ret <= 0)
2396 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002397 }
2398
Sage Weil31b80062009-10-06 11:31:13 -07002399 /* footer */
Ilya Dryomov89f08172016-02-20 15:56:07 +01002400 size = sizeof_footer(con);
Alex Elderfd516532012-05-10 10:29:50 -05002401 end += size;
2402 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05002403 if (ret <= 0)
2404 return ret;
2405
Yan, Zheng33d07332014-11-04 16:33:37 +08002406 if (!need_sign) {
2407 m->footer.flags = m->old_footer.flags;
2408 m->footer.sig = 0;
2409 }
2410
Sage Weil31b80062009-10-06 11:31:13 -07002411 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2412 m, front_len, m->footer.front_crc, middle_len,
2413 m->footer.middle_crc, data_len, m->footer.data_crc);
2414
2415 /* crc ok? */
2416 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2417 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2418 m, con->in_front_crc, m->footer.front_crc);
2419 return -EBADMSG;
2420 }
2421 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2422 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2423 m, con->in_middle_crc, m->footer.middle_crc);
2424 return -EBADMSG;
2425 }
Alex Elderbca064d2012-02-15 07:43:54 -06002426 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07002427 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2428 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2429 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2430 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2431 return -EBADMSG;
2432 }
2433
Yan, Zheng33d07332014-11-04 16:33:37 +08002434 if (need_sign && con->ops->check_message_signature &&
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002435 con->ops->check_message_signature(m)) {
Yan, Zheng33d07332014-11-04 16:33:37 +08002436 pr_err("read_partial_message %p signature check failed\n", m);
2437 return -EBADMSG;
2438 }
2439
Sage Weil31b80062009-10-06 11:31:13 -07002440 return 1; /* done! */
2441}
2442
2443/*
2444 * Process message. This happens in the worker thread. The callback should
2445 * be careful not to do anything that waits on other incoming messages or it
2446 * may deadlock.
2447 */
2448static void process_message(struct ceph_connection *con)
2449{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002450 struct ceph_msg *msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07002451
Alex Elder38941f82012-06-01 14:56:43 -05002452 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002453 con->in_msg = NULL;
2454
2455 /* if first message, set peer_name */
2456 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07002457 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07002458
Sage Weil31b80062009-10-06 11:31:13 -07002459 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08002460 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002461
2462 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2463 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07002464 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07002465 le16_to_cpu(msg->hdr.type),
2466 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2467 le32_to_cpu(msg->hdr.front_len),
2468 le32_to_cpu(msg->hdr.data_len),
2469 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2470 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08002471
2472 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002473}
2474
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002475static int read_keepalive_ack(struct ceph_connection *con)
2476{
2477 struct ceph_timespec ceph_ts;
2478 size_t size = sizeof(ceph_ts);
2479 int ret = read_partial(con, size, size, &ceph_ts);
2480 if (ret <= 0)
2481 return ret;
2482 ceph_decode_timespec(&con->last_keepalive_ack, &ceph_ts);
2483 prepare_read_tag(con);
2484 return 1;
2485}
Sage Weil31b80062009-10-06 11:31:13 -07002486
2487/*
2488 * Write something to the socket. Called in a worker thread when the
2489 * socket appears to be writeable and we have something ready to send.
2490 */
2491static int try_write(struct ceph_connection *con)
2492{
Sage Weil31b80062009-10-06 11:31:13 -07002493 int ret = 1;
2494
Sage Weild59315c2012-06-21 12:49:23 -07002495 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002496
Sage Weil31b80062009-10-06 11:31:13 -07002497more:
2498 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2499
2500 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002501 if (con->state == CON_STATE_PREOPEN) {
2502 BUG_ON(con->sock);
2503 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002504
Alex Eldere2200422012-05-23 14:35:23 -05002505 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002506 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002507 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002508
Sage Weilcf3e5c42009-12-11 09:48:05 -08002509 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002510 con->in_tag = CEPH_MSGR_TAG_READY;
2511 dout("try_write initiating connect on %p new state %lu\n",
2512 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002513 ret = ceph_tcp_connect(con);
2514 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002515 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002516 goto out;
2517 }
2518 }
2519
2520more_kvec:
2521 /* kvec data queued? */
Ilya Dryomov67645d72015-12-28 13:18:34 +03002522 if (con->out_kvec_left) {
2523 ret = write_partial_kvec(con);
Sage Weil31b80062009-10-06 11:31:13 -07002524 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002525 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002526 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03002527 if (con->out_skip) {
2528 ret = write_partial_skip(con);
Sage Weil31b80062009-10-06 11:31:13 -07002529 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002530 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002531 }
2532
2533 /* msg pages? */
2534 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002535 if (con->out_msg_done) {
2536 ceph_msg_put(con->out_msg);
2537 con->out_msg = NULL; /* we're done with this one */
2538 goto do_next;
2539 }
2540
Alex Elder34d2d202013-03-08 20:58:59 -06002541 ret = write_partial_message_data(con);
Sage Weil31b80062009-10-06 11:31:13 -07002542 if (ret == 1)
2543 goto more_kvec; /* we need to send the footer, too! */
2544 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002545 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002546 if (ret < 0) {
Alex Elder34d2d202013-03-08 20:58:59 -06002547 dout("try_write write_partial_message_data err %d\n",
Sage Weil31b80062009-10-06 11:31:13 -07002548 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002549 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002550 }
2551 }
2552
Sage Weilc86a2932009-12-14 14:04:30 -08002553do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002554 if (con->state == CON_STATE_OPEN) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002555 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2556 prepare_write_keepalive(con);
2557 goto more;
2558 }
Sage Weil31b80062009-10-06 11:31:13 -07002559 /* is anything else pending? */
2560 if (!list_empty(&con->out_queue)) {
2561 prepare_write_message(con);
2562 goto more;
2563 }
2564 if (con->in_seq > con->in_seq_acked) {
2565 prepare_write_ack(con);
2566 goto more;
2567 }
Sage Weil31b80062009-10-06 11:31:13 -07002568 }
2569
2570 /* Nothing to do! */
Alex Elderc9ffc772013-02-20 10:25:12 -06002571 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07002572 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002573 ret = 0;
2574out:
Sage Weil42961d22011-01-25 08:19:34 -08002575 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002576 return ret;
2577}
2578
2579
2580
2581/*
2582 * Read what we can from the socket.
2583 */
2584static int try_read(struct ceph_connection *con)
2585{
Sage Weil31b80062009-10-06 11:31:13 -07002586 int ret = -1;
2587
Sage Weil31b80062009-10-06 11:31:13 -07002588more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002589 dout("try_read start on %p state %lu\n", con, con->state);
2590 if (con->state != CON_STATE_CONNECTING &&
2591 con->state != CON_STATE_NEGOTIATING &&
2592 con->state != CON_STATE_OPEN)
2593 return 0;
2594
2595 BUG_ON(!con->sock);
2596
Sage Weil31b80062009-10-06 11:31:13 -07002597 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2598 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002599
Sage Weil8dacc7d2012-07-20 17:24:40 -07002600 if (con->state == CON_STATE_CONNECTING) {
Alex Elder7593af92012-05-24 11:55:03 -05002601 dout("try_read connecting\n");
2602 ret = read_partial_banner(con);
2603 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002604 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002605 ret = process_banner(con);
2606 if (ret < 0)
2607 goto out;
2608
Sage Weil8dacc7d2012-07-20 17:24:40 -07002609 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002610
Jim Schutt6d4221b2012-08-10 10:37:38 -07002611 /*
2612 * Received banner is good, exchange connection info.
2613 * Do not reset out_kvec, as sending our banner raced
2614 * with receiving peer banner after connect completed.
2615 */
Alex Elder7593af92012-05-24 11:55:03 -05002616 ret = prepare_write_connect(con);
2617 if (ret < 0)
2618 goto out;
2619 prepare_read_connect(con);
2620
2621 /* Send connection info before awaiting response */
Sage Weil0da5d702011-05-19 11:21:05 -07002622 goto out;
2623 }
2624
Sage Weil8dacc7d2012-07-20 17:24:40 -07002625 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002626 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002627 ret = read_partial_connect(con);
2628 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002629 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002630 ret = process_connect(con);
2631 if (ret < 0)
2632 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002633 goto more;
2634 }
2635
Alex Elder122070a2012-12-26 10:43:57 -06002636 WARN_ON(con->state != CON_STATE_OPEN);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002637
Sage Weil31b80062009-10-06 11:31:13 -07002638 if (con->in_base_pos < 0) {
2639 /*
2640 * skipping + discarding content.
2641 *
2642 * FIXME: there must be a better way to do this!
2643 */
Alex Elder84495f42012-02-15 07:43:55 -06002644 static char buf[SKIP_BUF_SIZE];
2645 int skip = min((int) sizeof (buf), -con->in_base_pos);
2646
Sage Weil31b80062009-10-06 11:31:13 -07002647 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2648 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2649 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002650 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002651 con->in_base_pos += ret;
2652 if (con->in_base_pos)
2653 goto more;
2654 }
2655 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2656 /*
2657 * what's next?
2658 */
2659 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2660 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002661 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002662 dout("try_read got tag %d\n", (int)con->in_tag);
2663 switch (con->in_tag) {
2664 case CEPH_MSGR_TAG_MSG:
2665 prepare_read_message(con);
2666 break;
2667 case CEPH_MSGR_TAG_ACK:
2668 prepare_read_ack(con);
2669 break;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002670 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2671 prepare_read_keepalive_ack(con);
2672 break;
Sage Weil31b80062009-10-06 11:31:13 -07002673 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002674 con_close_socket(con);
2675 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002676 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002677 default:
2678 goto bad_tag;
2679 }
2680 }
2681 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2682 ret = read_partial_message(con);
2683 if (ret <= 0) {
2684 switch (ret) {
2685 case -EBADMSG:
Ilya Dryomova51983e2015-10-28 23:52:06 +01002686 con->error_msg = "bad crc/signature";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002687 /* fall through */
2688 case -EBADE:
Sage Weil31b80062009-10-06 11:31:13 -07002689 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002690 break;
Sage Weil31b80062009-10-06 11:31:13 -07002691 case -EIO:
2692 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002693 break;
Sage Weil31b80062009-10-06 11:31:13 -07002694 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002695 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002696 }
2697 if (con->in_tag == CEPH_MSGR_TAG_READY)
2698 goto more;
2699 process_message(con);
Sage Weil7b862e02012-07-30 18:16:56 -07002700 if (con->state == CON_STATE_OPEN)
2701 prepare_read_tag(con);
Sage Weil31b80062009-10-06 11:31:13 -07002702 goto more;
2703 }
Sage Weil3a230832013-03-25 08:47:40 -07002704 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2705 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2706 /*
2707 * the final handshake seq exchange is semantically
2708 * equivalent to an ACK
2709 */
Sage Weil31b80062009-10-06 11:31:13 -07002710 ret = read_partial_ack(con);
2711 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002712 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002713 process_ack(con);
2714 goto more;
2715 }
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002716 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2717 ret = read_keepalive_ack(con);
2718 if (ret <= 0)
2719 goto out;
2720 goto more;
2721 }
Sage Weil31b80062009-10-06 11:31:13 -07002722
Sage Weil31b80062009-10-06 11:31:13 -07002723out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002724 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002725 return ret;
2726
2727bad_tag:
2728 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2729 con->error_msg = "protocol error, garbage tag";
2730 ret = -1;
2731 goto out;
2732}
2733
2734
2735/*
Alex Elder802c6d92012-10-08 20:37:30 -07002736 * Atomically queue work on a connection after the specified delay.
2737 * Bump @con reference to avoid races with connection teardown.
2738 * Returns 0 if work was queued, or an error code otherwise.
Sage Weil31b80062009-10-06 11:31:13 -07002739 */
Alex Elder802c6d92012-10-08 20:37:30 -07002740static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
Sage Weil31b80062009-10-06 11:31:13 -07002741{
Sage Weil31b80062009-10-06 11:31:13 -07002742 if (!con->ops->get(con)) {
Alex Elder802c6d92012-10-08 20:37:30 -07002743 dout("%s %p ref count 0\n", __func__, con);
Alex Elder802c6d92012-10-08 20:37:30 -07002744 return -ENOENT;
Sage Weil31b80062009-10-06 11:31:13 -07002745 }
2746
Alex Elder802c6d92012-10-08 20:37:30 -07002747 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2748 dout("%s %p - already queued\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -07002749 con->ops->put(con);
Alex Elder802c6d92012-10-08 20:37:30 -07002750 return -EBUSY;
Sage Weil31b80062009-10-06 11:31:13 -07002751 }
Alex Elder802c6d92012-10-08 20:37:30 -07002752
2753 dout("%s %p %lu\n", __func__, con, delay);
Alex Elder802c6d92012-10-08 20:37:30 -07002754 return 0;
2755}
2756
2757static void queue_con(struct ceph_connection *con)
2758{
2759 (void) queue_con_delay(con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002760}
2761
Ilya Dryomov37ab77a2014-06-24 16:21:45 +04002762static void cancel_con(struct ceph_connection *con)
2763{
2764 if (cancel_delayed_work(&con->work)) {
2765 dout("%s %p\n", __func__, con);
2766 con->ops->put(con);
2767 }
2768}
2769
Alex Elder7bb21d682012-12-07 19:50:07 -06002770static bool con_sock_closed(struct ceph_connection *con)
2771{
Alex Elderc9ffc772013-02-20 10:25:12 -06002772 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
Alex Elder7bb21d682012-12-07 19:50:07 -06002773 return false;
2774
2775#define CASE(x) \
2776 case CON_STATE_ ## x: \
2777 con->error_msg = "socket closed (con state " #x ")"; \
2778 break;
2779
2780 switch (con->state) {
2781 CASE(CLOSED);
2782 CASE(PREOPEN);
2783 CASE(CONNECTING);
2784 CASE(NEGOTIATING);
2785 CASE(OPEN);
2786 CASE(STANDBY);
2787 default:
Joe Perchesb9a67892014-09-09 21:17:29 -07002788 pr_warn("%s con %p unrecognized state %lu\n",
Alex Elder7bb21d682012-12-07 19:50:07 -06002789 __func__, con, con->state);
2790 con->error_msg = "unrecognized con state";
2791 BUG();
2792 break;
2793 }
2794#undef CASE
2795
2796 return true;
2797}
2798
Alex Elderf20a39f2013-02-19 12:25:57 -06002799static bool con_backoff(struct ceph_connection *con)
2800{
2801 int ret;
2802
2803 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2804 return false;
2805
2806 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2807 if (ret) {
2808 dout("%s: con %p FAILED to back off %lu\n", __func__,
2809 con, con->delay);
2810 BUG_ON(ret == -ENOENT);
2811 con_flag_set(con, CON_FLAG_BACKOFF);
2812 }
2813
2814 return true;
2815}
2816
Alex Elder93209262013-02-19 12:25:57 -06002817/* Finish fault handling; con->mutex must *not* be held here */
2818
2819static void con_fault_finish(struct ceph_connection *con)
2820{
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002821 dout("%s %p\n", __func__, con);
2822
Alex Elder93209262013-02-19 12:25:57 -06002823 /*
2824 * in case we faulted due to authentication, invalidate our
2825 * current tickets so that we can get new ones.
2826 */
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002827 if (con->auth_retry) {
2828 dout("auth_retry %d, invalidating\n", con->auth_retry);
2829 if (con->ops->invalidate_authorizer)
2830 con->ops->invalidate_authorizer(con);
2831 con->auth_retry = 0;
Alex Elder93209262013-02-19 12:25:57 -06002832 }
2833
2834 if (con->ops->fault)
2835 con->ops->fault(con);
2836}
2837
Sage Weil31b80062009-10-06 11:31:13 -07002838/*
2839 * Do some work on a connection. Drop a connection ref when we're done.
2840 */
Ilya Dryomov68931622015-07-03 15:44:41 +03002841static void ceph_con_workfn(struct work_struct *work)
Sage Weil31b80062009-10-06 11:31:13 -07002842{
2843 struct ceph_connection *con = container_of(work, struct ceph_connection,
2844 work.work);
Alex Elder49659412013-02-19 12:25:57 -06002845 bool fault;
Sage Weil31b80062009-10-06 11:31:13 -07002846
Sage Weil9dd46582010-04-28 13:51:50 -07002847 mutex_lock(&con->mutex);
Alex Elder49659412013-02-19 12:25:57 -06002848 while (true) {
2849 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002850
Alex Elder49659412013-02-19 12:25:57 -06002851 if ((fault = con_sock_closed(con))) {
2852 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2853 break;
2854 }
2855 if (con_backoff(con)) {
2856 dout("%s: con %p BACKOFF\n", __func__, con);
2857 break;
2858 }
2859 if (con->state == CON_STATE_STANDBY) {
2860 dout("%s: con %p STANDBY\n", __func__, con);
2861 break;
2862 }
2863 if (con->state == CON_STATE_CLOSED) {
2864 dout("%s: con %p CLOSED\n", __func__, con);
2865 BUG_ON(con->sock);
2866 break;
2867 }
2868 if (con->state == CON_STATE_PREOPEN) {
2869 dout("%s: con %p PREOPEN\n", __func__, con);
2870 BUG_ON(con->sock);
2871 }
Sage Weil0da5d702011-05-19 11:21:05 -07002872
Alex Elder49659412013-02-19 12:25:57 -06002873 ret = try_read(con);
2874 if (ret < 0) {
2875 if (ret == -EAGAIN)
2876 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002877 if (!con->error_msg)
2878 con->error_msg = "socket error on read";
Alex Elder49659412013-02-19 12:25:57 -06002879 fault = true;
2880 break;
2881 }
2882
2883 ret = try_write(con);
2884 if (ret < 0) {
2885 if (ret == -EAGAIN)
2886 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002887 if (!con->error_msg)
2888 con->error_msg = "socket error on write";
Alex Elder49659412013-02-19 12:25:57 -06002889 fault = true;
2890 }
2891
2892 break; /* If we make it to here, we're done */
Sage Weil3a140a02012-07-30 16:24:21 -07002893 }
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002894 if (fault)
2895 con_fault(con);
Sage Weil9dd46582010-04-28 13:51:50 -07002896 mutex_unlock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002897
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002898 if (fault)
2899 con_fault_finish(con);
2900
2901 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07002902}
2903
Sage Weil31b80062009-10-06 11:31:13 -07002904/*
2905 * Generic error/fault handler. A retry mechanism is used with
2906 * exponential backoff
2907 */
Alex Elder93209262013-02-19 12:25:57 -06002908static void con_fault(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002909{
Sage Weil31b80062009-10-06 11:31:13 -07002910 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002911 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002912
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002913 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2914 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2915 con->error_msg = NULL;
2916
Alex Elder122070a2012-12-26 10:43:57 -06002917 WARN_ON(con->state != CON_STATE_CONNECTING &&
Sage Weil8dacc7d2012-07-20 17:24:40 -07002918 con->state != CON_STATE_NEGOTIATING &&
2919 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08002920
Sage Weil31b80062009-10-06 11:31:13 -07002921 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002922
Alex Elderc9ffc772013-02-20 10:25:12 -06002923 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002924 dout("fault on LOSSYTX channel, marking CLOSED\n");
2925 con->state = CON_STATE_CLOSED;
Alex Elder93209262013-02-19 12:25:57 -06002926 return;
Sage Weil3b5ede02012-07-20 15:22:53 -07002927 }
2928
Sage Weil5e095e82009-12-14 14:30:34 -08002929 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002930 BUG_ON(con->in_msg->con != con);
Sage Weil5e095e82009-12-14 14:30:34 -08002931 ceph_msg_put(con->in_msg);
2932 con->in_msg = NULL;
2933 }
Sage Weil31b80062009-10-06 11:31:13 -07002934
Sage Weile80a52d2010-02-25 12:40:45 -08002935 /* Requeue anything that hasn't been acked */
2936 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002937
Sage Weile76661d2011-03-03 10:10:15 -08002938 /* If there are no messages queued or keepalive pending, place
2939 * the connection in a STANDBY state */
2940 if (list_empty(&con->out_queue) &&
Alex Elderc9ffc772013-02-20 10:25:12 -06002941 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
Sage Weile00de342011-03-04 12:25:05 -08002942 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elderc9ffc772013-02-20 10:25:12 -06002943 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002944 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08002945 } else {
2946 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002947 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08002948 if (con->delay == 0)
2949 con->delay = BASE_DELAY_INTERVAL;
2950 else if (con->delay < MAX_DELAY_INTERVAL)
2951 con->delay *= 2;
Alex Elderc9ffc772013-02-20 10:25:12 -06002952 con_flag_set(con, CON_FLAG_BACKOFF);
Alex Elder8618e302012-10-08 20:37:30 -07002953 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -07002954 }
Sage Weil31b80062009-10-06 11:31:13 -07002955}
2956
2957
2958
2959/*
Alex Elder15d98822012-05-26 23:26:43 -05002960 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002961 */
Alex Elder15d98822012-05-26 23:26:43 -05002962void ceph_messenger_init(struct ceph_messenger *msgr,
Ilya Dryomov859bff52015-10-28 23:50:58 +01002963 struct ceph_entity_addr *myaddr)
Sage Weil31b80062009-10-06 11:31:13 -07002964{
Sage Weil31b80062009-10-06 11:31:13 -07002965 spin_lock_init(&msgr->global_seq_lock);
2966
Sage Weil31b80062009-10-06 11:31:13 -07002967 if (myaddr)
2968 msgr->inst.addr = *myaddr;
2969
2970 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002971 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002972 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002973 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002974
Guanjun Hea2a32582012-07-08 19:50:33 -07002975 atomic_set(&msgr->stopping, 0);
Ilya Dryomov757856d2015-06-25 17:47:45 +03002976 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
Sage Weil31b80062009-10-06 11:31:13 -07002977
Alex Elder15d98822012-05-26 23:26:43 -05002978 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002979}
Alex Elder15d98822012-05-26 23:26:43 -05002980EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002981
Ilya Dryomov757856d2015-06-25 17:47:45 +03002982void ceph_messenger_fini(struct ceph_messenger *msgr)
2983{
2984 put_net(read_pnet(&msgr->net));
2985}
2986EXPORT_SYMBOL(ceph_messenger_fini);
2987
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002988static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
2989{
2990 if (msg->con)
2991 msg->con->ops->put(msg->con);
2992
2993 msg->con = con ? con->ops->get(con) : NULL;
2994 BUG_ON(msg->con != con);
2995}
2996
Sage Weile00de342011-03-04 12:25:05 -08002997static void clear_standby(struct ceph_connection *con)
2998{
2999 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003000 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08003001 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003002 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08003003 con->connect_seq++;
Alex Elderc9ffc772013-02-20 10:25:12 -06003004 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3005 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
Sage Weile00de342011-03-04 12:25:05 -08003006 }
3007}
3008
Sage Weil31b80062009-10-06 11:31:13 -07003009/*
3010 * Queue up an outgoing message on the given connection.
3011 */
3012void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3013{
Sage Weila59b55a2012-07-20 15:34:04 -07003014 /* set src+dst */
3015 msg->hdr.src = con->msgr->inst.name;
3016 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3017 msg->needs_out_seq = true;
3018
3019 mutex_lock(&con->mutex);
3020
Sage Weil8dacc7d2012-07-20 17:24:40 -07003021 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07003022 dout("con_send %p closed, dropping %p\n", con, msg);
3023 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07003024 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003025 return;
3026 }
3027
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003028 msg_con_set(msg, con);
Sage Weil31b80062009-10-06 11:31:13 -07003029
Sage Weil31b80062009-10-06 11:31:13 -07003030 BUG_ON(!list_empty(&msg->list_head));
3031 list_add_tail(&msg->list_head, &con->out_queue);
3032 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3033 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3034 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3035 le32_to_cpu(msg->hdr.front_len),
3036 le32_to_cpu(msg->hdr.middle_len),
3037 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07003038
3039 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08003040 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003041
3042 /* if there wasn't anything waiting to send before, queue
3043 * new work */
Alex Elderc9ffc772013-02-20 10:25:12 -06003044 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003045 queue_con(con);
3046}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003047EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07003048
3049/*
3050 * Revoke a message that was previously queued for send
3051 */
Alex Elder6740a842012-06-01 14:56:43 -05003052void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003053{
Alex Elder6740a842012-06-01 14:56:43 -05003054 struct ceph_connection *con = msg->con;
3055
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003056 if (!con) {
3057 dout("%s msg %p null con\n", __func__, msg);
Alex Elder6740a842012-06-01 14:56:43 -05003058 return; /* Message not in our possession */
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003059 }
Alex Elder6740a842012-06-01 14:56:43 -05003060
Sage Weilec302642009-12-22 10:43:42 -08003061 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003062 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05003063 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07003064 list_del_init(&msg->list_head);
Sage Weil31b80062009-10-06 11:31:13 -07003065 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05003066
Sage Weil31b80062009-10-06 11:31:13 -07003067 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07003068 }
3069 if (con->out_msg == msg) {
Ilya Dryomov67645d72015-12-28 13:18:34 +03003070 BUG_ON(con->out_skip);
3071 /* footer */
3072 if (con->out_msg_done) {
3073 con->out_skip += con_out_kvec_skip(con);
3074 } else {
3075 BUG_ON(!msg->data_length);
Ilya Dryomov89f08172016-02-20 15:56:07 +01003076 con->out_skip += sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07003077 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003078 /* data, middle, front */
3079 if (msg->data_length)
3080 con->out_skip += msg->cursor.total_resid;
3081 if (msg->middle)
3082 con->out_skip += con_out_kvec_skip(con);
3083 con->out_skip += con_out_kvec_skip(con);
Alex Elder92ce0342012-06-04 14:43:33 -05003084
Ilya Dryomov67645d72015-12-28 13:18:34 +03003085 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3086 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3087 msg->hdr.seq = 0;
3088 con->out_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05003089 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07003090 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003091
Sage Weilec302642009-12-22 10:43:42 -08003092 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003093}
3094
3095/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003096 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08003097 */
Alex Elder8921d112012-06-01 14:56:43 -05003098void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08003099{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003100 struct ceph_connection *con = msg->con;
Alex Elder8921d112012-06-01 14:56:43 -05003101
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003102 if (!con) {
Alex Elder8921d112012-06-01 14:56:43 -05003103 dout("%s msg %p null con\n", __func__, msg);
Alex Elder8921d112012-06-01 14:56:43 -05003104 return; /* Message not in our possession */
3105 }
3106
Sage Weil350b1c32009-12-22 10:45:45 -08003107 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05003108 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00003109 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3110 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3111 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08003112
3113 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05003114 dout("%s %p msg %p revoked\n", __func__, con, msg);
3115 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08003116 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003117 front_len -
3118 middle_len -
3119 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08003120 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08003121 ceph_msg_put(con->in_msg);
3122 con->in_msg = NULL;
3123 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07003124 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08003125 } else {
Alex Elder8921d112012-06-01 14:56:43 -05003126 dout("%s %p in_msg %p msg %p no-op\n",
3127 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08003128 }
3129 mutex_unlock(&con->mutex);
3130}
3131
3132/*
Sage Weil31b80062009-10-06 11:31:13 -07003133 * Queue a keepalive byte to ensure the tcp connection is alive.
3134 */
3135void ceph_con_keepalive(struct ceph_connection *con)
3136{
Sage Weile00de342011-03-04 12:25:05 -08003137 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07003138 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08003139 clear_standby(con);
Sage Weil00650932012-07-20 15:33:04 -07003140 mutex_unlock(&con->mutex);
Alex Elderc9ffc772013-02-20 10:25:12 -06003141 if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
3142 con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003143 queue_con(con);
3144}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003145EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07003146
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003147bool ceph_con_keepalive_expired(struct ceph_connection *con,
3148 unsigned long interval)
3149{
3150 if (interval > 0 &&
3151 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
3152 struct timespec now = CURRENT_TIME;
3153 struct timespec ts;
3154 jiffies_to_timespec(interval, &ts);
3155 ts = timespec_add(con->last_keepalive_ack, ts);
3156 return timespec_compare(&now, &ts) >= 0;
3157 }
3158 return false;
3159}
3160
Alex Elder6644ed72013-03-11 23:34:24 -05003161static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
Alex Elder43794502013-03-01 18:00:16 -06003162{
Alex Elder6644ed72013-03-11 23:34:24 -05003163 struct ceph_msg_data *data;
3164
3165 if (WARN_ON(!ceph_msg_data_type_valid(type)))
3166 return NULL;
3167
Alex Elder81b36be2013-05-01 12:43:04 -05003168 data = kmem_cache_zalloc(ceph_msg_data_cache, GFP_NOFS);
Alex Elder6644ed72013-03-11 23:34:24 -05003169 if (data)
3170 data->type = type;
Alex Elder5240d9f2013-03-14 14:09:06 -05003171 INIT_LIST_HEAD(&data->links);
Alex Elder6644ed72013-03-11 23:34:24 -05003172
3173 return data;
3174}
3175
3176static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3177{
3178 if (!data)
3179 return;
3180
Alex Elder5240d9f2013-03-14 14:09:06 -05003181 WARN_ON(!list_empty(&data->links));
Yan, Zhenge4339d282014-09-16 17:50:45 +08003182 if (data->type == CEPH_MSG_DATA_PAGELIST)
Alex Elder6644ed72013-03-11 23:34:24 -05003183 ceph_pagelist_release(data->pagelist);
Alex Elder81b36be2013-05-01 12:43:04 -05003184 kmem_cache_free(ceph_msg_data_cache, data);
Alex Elder43794502013-03-01 18:00:16 -06003185}
3186
Alex Elder90af3602013-04-05 14:46:01 -05003187void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Alex Elderf1baeb22013-03-07 15:38:26 -06003188 size_t length, size_t alignment)
Alex Elder02afca62013-02-14 12:16:43 -06003189{
Alex Elder6644ed72013-03-11 23:34:24 -05003190 struct ceph_msg_data *data;
3191
Alex Elder07aa1552013-03-04 18:29:06 -06003192 BUG_ON(!pages);
3193 BUG_ON(!length);
Alex Elder02afca62013-02-14 12:16:43 -06003194
Alex Elder6644ed72013-03-11 23:34:24 -05003195 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
3196 BUG_ON(!data);
3197 data->pages = pages;
3198 data->length = length;
3199 data->alignment = alignment & ~PAGE_MASK;
3200
Alex Elder5240d9f2013-03-14 14:09:06 -05003201 list_add_tail(&data->links, &msg->data);
3202 msg->data_length += length;
Alex Elder02afca62013-02-14 12:16:43 -06003203}
Alex Elder90af3602013-04-05 14:46:01 -05003204EXPORT_SYMBOL(ceph_msg_data_add_pages);
Sage Weil31b80062009-10-06 11:31:13 -07003205
Alex Elder90af3602013-04-05 14:46:01 -05003206void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
Alex Elder27fa8382013-02-14 12:16:43 -06003207 struct ceph_pagelist *pagelist)
3208{
Alex Elder6644ed72013-03-11 23:34:24 -05003209 struct ceph_msg_data *data;
3210
Alex Elder07aa1552013-03-04 18:29:06 -06003211 BUG_ON(!pagelist);
3212 BUG_ON(!pagelist->length);
Alex Elder27fa8382013-02-14 12:16:43 -06003213
Alex Elder6644ed72013-03-11 23:34:24 -05003214 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
3215 BUG_ON(!data);
3216 data->pagelist = pagelist;
3217
Alex Elder5240d9f2013-03-14 14:09:06 -05003218 list_add_tail(&data->links, &msg->data);
3219 msg->data_length += pagelist->length;
Alex Elder27fa8382013-02-14 12:16:43 -06003220}
Alex Elder90af3602013-04-05 14:46:01 -05003221EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
Alex Elder27fa8382013-02-14 12:16:43 -06003222
Alex Elderea965712013-04-05 14:46:01 -05003223#ifdef CONFIG_BLOCK
Alex Elder90af3602013-04-05 14:46:01 -05003224void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
Alex Eldera1930802013-03-14 14:09:06 -05003225 size_t length)
Alex Elder27fa8382013-02-14 12:16:43 -06003226{
Alex Elder6644ed72013-03-11 23:34:24 -05003227 struct ceph_msg_data *data;
Alex Elder27fa8382013-02-14 12:16:43 -06003228
Alex Elder6644ed72013-03-11 23:34:24 -05003229 BUG_ON(!bio);
Alex Elder6644ed72013-03-11 23:34:24 -05003230
3231 data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
3232 BUG_ON(!data);
3233 data->bio = bio;
Alex Elderc851c492013-04-05 14:46:01 -05003234 data->bio_length = length;
Alex Elder6644ed72013-03-11 23:34:24 -05003235
Alex Elder5240d9f2013-03-14 14:09:06 -05003236 list_add_tail(&data->links, &msg->data);
3237 msg->data_length += length;
Alex Elder27fa8382013-02-14 12:16:43 -06003238}
Alex Elder90af3602013-04-05 14:46:01 -05003239EXPORT_SYMBOL(ceph_msg_data_add_bio);
Alex Elderea965712013-04-05 14:46:01 -05003240#endif /* CONFIG_BLOCK */
Alex Elder27fa8382013-02-14 12:16:43 -06003241
Sage Weil31b80062009-10-06 11:31:13 -07003242/*
3243 * construct a new message with given type, size
3244 * the new msg has a ref count of 1.
3245 */
Sage Weilb61c2762011-08-09 15:03:46 -07003246struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3247 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07003248{
3249 struct ceph_msg *m;
3250
Alex Eldere3d5d632013-05-01 12:43:04 -05003251 m = kmem_cache_zalloc(ceph_msg_cache, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003252 if (m == NULL)
3253 goto out;
Alex Elder38941f82012-06-01 14:56:43 -05003254
Sage Weil31b80062009-10-06 11:31:13 -07003255 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07003256 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
Sage Weil31b80062009-10-06 11:31:13 -07003257 m->hdr.front_len = cpu_to_le32(front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003258
Alex Elder9516e452013-03-01 18:00:16 -06003259 INIT_LIST_HEAD(&m->list_head);
3260 kref_init(&m->kref);
Alex Elder5240d9f2013-03-14 14:09:06 -05003261 INIT_LIST_HEAD(&m->data);
Henry C Changca208922011-05-03 02:29:56 +00003262
Sage Weil31b80062009-10-06 11:31:13 -07003263 /* front */
3264 if (front_len) {
Ilya Dryomoveeb0bed2014-01-09 20:08:21 +02003265 m->front.iov_base = ceph_kvmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003266 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07003267 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07003268 front_len);
3269 goto out2;
3270 }
3271 } else {
3272 m->front.iov_base = NULL;
3273 }
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02003274 m->front_alloc_len = m->front.iov_len = front_len;
Sage Weil31b80062009-10-06 11:31:13 -07003275
Sage Weilbb257662010-04-01 16:07:23 -07003276 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003277 return m;
3278
3279out2:
3280 ceph_msg_put(m);
3281out:
Sage Weilb61c2762011-08-09 15:03:46 -07003282 if (!can_fail) {
3283 pr_err("msg_new can't create type %d front %d\n", type,
3284 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07003285 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07003286 } else {
3287 dout("msg_new can't create type %d front %d\n", type,
3288 front_len);
3289 }
Sage Weila79832f2010-04-01 16:06:19 -07003290 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003291}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003292EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07003293
3294/*
Sage Weil31b80062009-10-06 11:31:13 -07003295 * Allocate "middle" portion of a message, if it is needed and wasn't
3296 * allocated by alloc_msg. This allows us to read a small fixed-size
3297 * per-type header in the front and then gracefully fail (i.e.,
3298 * propagate the error to the caller based on info in the front) when
3299 * the middle is too large.
3300 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08003301static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003302{
3303 int type = le16_to_cpu(msg->hdr.type);
3304 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3305
3306 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3307 ceph_msg_type_name(type), middle_len);
3308 BUG_ON(!middle_len);
3309 BUG_ON(msg->middle);
3310
Sage Weilb6c1d5b2009-12-07 12:17:17 -08003311 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07003312 if (!msg->middle)
3313 return -ENOMEM;
3314 return 0;
3315}
3316
Yehuda Sadeh24504182010-01-08 13:58:34 -08003317/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05003318 * Allocate a message for receiving an incoming message on a
3319 * connection, and save the result in con->in_msg. Uses the
3320 * connection's private alloc_msg op if available.
3321 *
Sage Weil4740a622012-07-30 18:19:30 -07003322 * Returns 0 on success, or a negative error code.
3323 *
3324 * On success, if we set *skip = 1:
3325 * - the next message should be skipped and ignored.
3326 * - con->in_msg == NULL
3327 * or if we set *skip = 0:
3328 * - con->in_msg is non-null.
3329 * On error (ENOMEM, EAGAIN, ...),
3330 * - con->in_msg == NULL
Yehuda Sadeh24504182010-01-08 13:58:34 -08003331 */
Sage Weil4740a622012-07-30 18:19:30 -07003332static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08003333{
Sage Weil4740a622012-07-30 18:19:30 -07003334 struct ceph_msg_header *hdr = &con->in_hdr;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003335 int middle_len = le32_to_cpu(hdr->middle_len);
Alex Elder1d866d12013-03-01 18:00:14 -06003336 struct ceph_msg *msg;
Sage Weil4740a622012-07-30 18:19:30 -07003337 int ret = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003338
Alex Elder1c20f2d2012-06-04 14:43:32 -05003339 BUG_ON(con->in_msg != NULL);
Alex Elder53ded492013-03-01 18:00:14 -06003340 BUG_ON(!con->ops->alloc_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003341
Alex Elder53ded492013-03-01 18:00:14 -06003342 mutex_unlock(&con->mutex);
3343 msg = con->ops->alloc_msg(con, hdr, skip);
3344 mutex_lock(&con->mutex);
3345 if (con->state != CON_STATE_OPEN) {
3346 if (msg)
Alex Elder1d866d12013-03-01 18:00:14 -06003347 ceph_msg_put(msg);
Alex Elder53ded492013-03-01 18:00:14 -06003348 return -EAGAIN;
3349 }
Alex Elder41375772013-03-05 09:25:10 -06003350 if (msg) {
3351 BUG_ON(*skip);
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003352 msg_con_set(msg, con);
Alex Elder41375772013-03-05 09:25:10 -06003353 con->in_msg = msg;
Alex Elder41375772013-03-05 09:25:10 -06003354 } else {
3355 /*
3356 * Null message pointer means either we should skip
3357 * this message or we couldn't allocate memory. The
3358 * former is not an error.
3359 */
3360 if (*skip)
3361 return 0;
Alex Elder41375772013-03-05 09:25:10 -06003362
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03003363 con->error_msg = "error allocating memory for incoming message";
Alex Elder53ded492013-03-01 18:00:14 -06003364 return -ENOMEM;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003365 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05003366 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08003367
Alex Elder1c20f2d2012-06-04 14:43:32 -05003368 if (middle_len && !con->in_msg->middle) {
3369 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003370 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05003371 ceph_msg_put(con->in_msg);
3372 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003373 }
3374 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08003375
Sage Weil4740a622012-07-30 18:19:30 -07003376 return ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003377}
3378
Sage Weil31b80062009-10-06 11:31:13 -07003379
3380/*
3381 * Free a generically kmalloc'd message.
3382 */
Ilya Dryomov0215e442014-06-20 14:14:41 +04003383static void ceph_msg_free(struct ceph_msg *m)
Sage Weil31b80062009-10-06 11:31:13 -07003384{
Ilya Dryomov0215e442014-06-20 14:14:41 +04003385 dout("%s %p\n", __func__, m);
Ilya Dryomov4965fc32014-10-23 16:32:57 +04003386 kvfree(m->front.iov_base);
Alex Eldere3d5d632013-05-01 12:43:04 -05003387 kmem_cache_free(ceph_msg_cache, m);
Sage Weil31b80062009-10-06 11:31:13 -07003388}
3389
Ilya Dryomov0215e442014-06-20 14:14:41 +04003390static void ceph_msg_release(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07003391{
Sage Weilc2e552e2009-12-07 15:55:05 -08003392 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Geliang Tang10bcee12015-12-18 23:33:30 +08003393 struct ceph_msg_data *data, *next;
Sage Weil31b80062009-10-06 11:31:13 -07003394
Ilya Dryomov0215e442014-06-20 14:14:41 +04003395 dout("%s %p\n", __func__, m);
Sage Weilc2e552e2009-12-07 15:55:05 -08003396 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07003397
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003398 msg_con_set(m, NULL);
3399
Sage Weilc2e552e2009-12-07 15:55:05 -08003400 /* drop middle, data, if any */
3401 if (m->middle) {
3402 ceph_buffer_put(m->middle);
3403 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003404 }
Alex Elder5240d9f2013-03-14 14:09:06 -05003405
Geliang Tang10bcee12015-12-18 23:33:30 +08003406 list_for_each_entry_safe(data, next, &m->data, links) {
3407 list_del_init(&data->links);
Alex Elder5240d9f2013-03-14 14:09:06 -05003408 ceph_msg_data_destroy(data);
3409 }
Alex Eldera1930802013-03-14 14:09:06 -05003410 m->data_length = 0;
Sage Weil58bb3b32009-12-23 12:12:31 -08003411
Sage Weilc2e552e2009-12-07 15:55:05 -08003412 if (m->pool)
3413 ceph_msgpool_put(m->pool, m);
3414 else
Ilya Dryomov0215e442014-06-20 14:14:41 +04003415 ceph_msg_free(m);
Sage Weil31b80062009-10-06 11:31:13 -07003416}
Ilya Dryomov0215e442014-06-20 14:14:41 +04003417
3418struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3419{
3420 dout("%s %p (was %d)\n", __func__, msg,
3421 atomic_read(&msg->kref.refcount));
3422 kref_get(&msg->kref);
3423 return msg;
3424}
3425EXPORT_SYMBOL(ceph_msg_get);
3426
3427void ceph_msg_put(struct ceph_msg *msg)
3428{
3429 dout("%s %p (was %d)\n", __func__, msg,
3430 atomic_read(&msg->kref.refcount));
3431 kref_put(&msg->kref, ceph_msg_release);
3432}
3433EXPORT_SYMBOL(ceph_msg_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003434
3435void ceph_msg_dump(struct ceph_msg *msg)
3436{
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02003437 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3438 msg->front_alloc_len, msg->data_length);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003439 print_hex_dump(KERN_DEBUG, "header: ",
3440 DUMP_PREFIX_OFFSET, 16, 1,
3441 &msg->hdr, sizeof(msg->hdr), true);
3442 print_hex_dump(KERN_DEBUG, " front: ",
3443 DUMP_PREFIX_OFFSET, 16, 1,
3444 msg->front.iov_base, msg->front.iov_len, true);
3445 if (msg->middle)
3446 print_hex_dump(KERN_DEBUG, "middle: ",
3447 DUMP_PREFIX_OFFSET, 16, 1,
3448 msg->middle->vec.iov_base,
3449 msg->middle->vec.iov_len, true);
3450 print_hex_dump(KERN_DEBUG, "footer: ",
3451 DUMP_PREFIX_OFFSET, 16, 1,
3452 &msg->footer, sizeof(msg->footer), true);
3453}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003454EXPORT_SYMBOL(ceph_msg_dump);