blob: 770c52701efa3e08cec85c756fd7b8125f63ac91 [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);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300272 put_page(zero_page);
Alex Elder6173d1f2012-02-14 14:05:33 -0600273 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);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300285 get_page(zero_page);
Alex Elder57666512012-01-23 15:49:27 -0600286
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
Alex Elderdac1e712012-05-16 15:16:39 -05001396 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Eldera3530df2012-05-16 15:16:39 -05001397 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -05001398 return auth;
Sage Weil0da5d702011-05-19 11:21:05 -07001399
Alex Elder8f43fb52012-05-16 15:16:39 -05001400 con->auth_reply_buf = auth->authorizer_reply_buf;
1401 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
Alex Elder729796b2012-05-16 15:16:39 -05001402 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001403}
1404
Sage Weil31b80062009-10-06 11:31:13 -07001405/*
1406 * We connected to a peer and are saying hello.
1407 */
Alex Eldere825a662012-05-16 15:16:38 -05001408static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001409{
Alex Eldere2200422012-05-23 14:35:23 -05001410 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1411 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -05001412 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -08001413
Sage Weileed0ef22009-11-10 14:34:36 -08001414 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001415 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weileed0ef22009-11-10 14:34:36 -08001416}
1417
Alex Eldere825a662012-05-16 15:16:38 -05001418static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -08001419{
Eric Dumazet95c96172012-04-15 05:58:06 +00001420 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001421 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -05001422 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -05001423 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -07001424
1425 switch (con->peer_name.type) {
1426 case CEPH_ENTITY_TYPE_MON:
1427 proto = CEPH_MONC_PROTOCOL;
1428 break;
1429 case CEPH_ENTITY_TYPE_OSD:
1430 proto = CEPH_OSDC_PROTOCOL;
1431 break;
1432 case CEPH_ENTITY_TYPE_MDS:
1433 proto = CEPH_MDSC_PROTOCOL;
1434 break;
1435 default:
1436 BUG();
1437 }
1438
1439 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1440 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001441
Ilya Dryomov859bff52015-10-28 23:50:58 +01001442 con->out_connect.features =
1443 cpu_to_le64(from_msgr(con->msgr)->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -07001444 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1445 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1446 con->out_connect.global_seq = cpu_to_le32(global_seq);
1447 con->out_connect.protocol_version = cpu_to_le32(proto);
1448 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001449
Alex Elderdac1e712012-05-16 15:16:39 -05001450 auth_proto = CEPH_AUTH_UNKNOWN;
1451 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -05001452 if (IS_ERR(auth))
1453 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -05001454
Alex Elderdac1e712012-05-16 15:16:39 -05001455 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -05001456 con->out_connect.authorizer_len = auth ?
1457 cpu_to_le32(auth->authorizer_buf_len) : 0;
1458
Alex Eldere2200422012-05-23 14:35:23 -05001459 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -05001460 &con->out_connect);
1461 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -05001462 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -05001463 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -06001464
Sage Weil31b80062009-10-06 11:31:13 -07001465 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001466 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001467
Alex Eldere10c7582012-05-16 15:16:38 -05001468 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07001469}
1470
Sage Weil31b80062009-10-06 11:31:13 -07001471/*
1472 * write as much of pending kvecs to the socket as we can.
1473 * 1 -> done
1474 * 0 -> socket full, but more to do
1475 * <0 -> error
1476 */
1477static int write_partial_kvec(struct ceph_connection *con)
1478{
1479 int ret;
1480
1481 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1482 while (con->out_kvec_bytes > 0) {
1483 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1484 con->out_kvec_left, con->out_kvec_bytes,
1485 con->out_more);
1486 if (ret <= 0)
1487 goto out;
1488 con->out_kvec_bytes -= ret;
1489 if (con->out_kvec_bytes == 0)
1490 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -06001491
1492 /* account for full iov entries consumed */
1493 while (ret >= con->out_kvec_cur->iov_len) {
1494 BUG_ON(!con->out_kvec_left);
1495 ret -= con->out_kvec_cur->iov_len;
1496 con->out_kvec_cur++;
1497 con->out_kvec_left--;
1498 }
1499 /* and for a partially-consumed entry */
1500 if (ret) {
1501 con->out_kvec_cur->iov_len -= ret;
1502 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -07001503 }
1504 }
1505 con->out_kvec_left = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001506 ret = 1;
1507out:
1508 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1509 con->out_kvec_bytes, con->out_kvec_left, ret);
1510 return ret; /* done! */
1511}
1512
Alex Elder35b62802013-03-08 20:59:00 -06001513static u32 ceph_crc32c_page(u32 crc, struct page *page,
1514 unsigned int page_offset,
1515 unsigned int length)
1516{
1517 char *kaddr;
1518
1519 kaddr = kmap(page);
1520 BUG_ON(kaddr == NULL);
1521 crc = crc32c(crc, kaddr + page_offset, length);
1522 kunmap(page);
1523
1524 return crc;
1525}
Sage Weil31b80062009-10-06 11:31:13 -07001526/*
1527 * Write as much message data payload as we can. If we finish, queue
1528 * up the footer.
1529 * 1 -> done, footer is now queued in out_kvec[].
1530 * 0 -> socket full, but more to do
1531 * <0 -> error
1532 */
Alex Elder34d2d202013-03-08 20:58:59 -06001533static int write_partial_message_data(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001534{
1535 struct ceph_msg *msg = con->out_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001536 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01001537 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elderf5db90b2013-03-11 23:34:23 -05001538 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001539
Alex Elder859a35d2013-03-11 23:34:23 -05001540 dout("%s %p msg %p\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001541
Alex Elder5240d9f2013-03-14 14:09:06 -05001542 if (list_empty(&msg->data))
Alex Elder4c59b4a2013-03-11 23:34:23 -05001543 return -EINVAL;
1544
Alex Elder5821bd82012-06-11 14:57:13 -05001545 /*
1546 * Iterate through each page that contains data to be
1547 * written, and send as much as possible for each.
1548 *
1549 * If we are calculating the data crc (the default), we will
1550 * need to map the page. If we have no pages, they have
1551 * been revoked, so use the zero page.
1552 */
Alex Elderf5db90b2013-03-11 23:34:23 -05001553 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
Alex Elder643c68a2013-03-11 23:34:23 -05001554 while (cursor->resid) {
Alex Elder8a166d02013-03-08 13:35:36 -06001555 struct page *page;
Alex Eldere387d522013-03-06 23:39:38 -06001556 size_t page_offset;
1557 size_t length;
Alex Elder8a166d02013-03-08 13:35:36 -06001558 bool last_piece;
Alex Elder8ea299b2013-03-11 23:34:23 -05001559 bool need_crc;
Alex Elderf5db90b2013-03-11 23:34:23 -05001560 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001561
Shraddha Barke343128c2015-10-19 21:59:00 +05301562 page = ceph_msg_data_next(cursor, &page_offset, &length,
1563 &last_piece);
Alex Eldere387d522013-03-06 23:39:38 -06001564 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
Benoît Canetc2cfa192015-06-25 20:32:34 +03001565 length, !last_piece);
Alex Elderf5db90b2013-03-11 23:34:23 -05001566 if (ret <= 0) {
1567 if (do_datacrc)
1568 msg->footer.data_crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -07001569
Alex Elderf5db90b2013-03-11 23:34:23 -05001570 return ret;
1571 }
Alex Elder143334f2013-03-29 11:44:10 -05001572 if (do_datacrc && cursor->need_crc)
1573 crc = ceph_crc32c_page(crc, page, page_offset, length);
Shraddha Barke343128c2015-10-19 21:59:00 +05301574 need_crc = ceph_msg_data_advance(cursor, (size_t)ret);
Sage Weil31b80062009-10-06 11:31:13 -07001575 }
1576
Alex Elder34d2d202013-03-08 20:58:59 -06001577 dout("%s %p msg %p done\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001578
1579 /* prepare and queue up footer, too */
Alex Elderf5db90b2013-03-11 23:34:23 -05001580 if (do_datacrc)
1581 msg->footer.data_crc = cpu_to_le32(crc);
1582 else
Alex Elder84ca8fc2012-06-11 14:57:13 -05001583 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001584 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001585 prepare_write_message_footer(con);
Alex Elderf5db90b2013-03-11 23:34:23 -05001586
1587 return 1; /* must return > 0 to indicate success */
Sage Weil31b80062009-10-06 11:31:13 -07001588}
1589
1590/*
1591 * write some zeros
1592 */
1593static int write_partial_skip(struct ceph_connection *con)
1594{
1595 int ret;
1596
Ilya Dryomov67645d72015-12-28 13:18:34 +03001597 dout("%s %p %d left\n", __func__, con, con->out_skip);
Sage Weil31b80062009-10-06 11:31:13 -07001598 while (con->out_skip > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001599 size_t size = min(con->out_skip, (int) PAGE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001600
Alex Eldere1dcb122013-03-06 23:39:38 -06001601 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
Sage Weil31b80062009-10-06 11:31:13 -07001602 if (ret <= 0)
1603 goto out;
1604 con->out_skip -= ret;
1605 }
1606 ret = 1;
1607out:
1608 return ret;
1609}
1610
1611/*
1612 * Prepare to read connection handshake, or an ack.
1613 */
Sage Weileed0ef22009-11-10 14:34:36 -08001614static void prepare_read_banner(struct ceph_connection *con)
1615{
1616 dout("prepare_read_banner %p\n", con);
1617 con->in_base_pos = 0;
1618}
1619
Sage Weil31b80062009-10-06 11:31:13 -07001620static void prepare_read_connect(struct ceph_connection *con)
1621{
1622 dout("prepare_read_connect %p\n", con);
1623 con->in_base_pos = 0;
1624}
1625
1626static void prepare_read_ack(struct ceph_connection *con)
1627{
1628 dout("prepare_read_ack %p\n", con);
1629 con->in_base_pos = 0;
1630}
1631
Sage Weil3a230832013-03-25 08:47:40 -07001632static void prepare_read_seq(struct ceph_connection *con)
1633{
1634 dout("prepare_read_seq %p\n", con);
1635 con->in_base_pos = 0;
1636 con->in_tag = CEPH_MSGR_TAG_SEQ;
1637}
1638
Sage Weil31b80062009-10-06 11:31:13 -07001639static void prepare_read_tag(struct ceph_connection *con)
1640{
1641 dout("prepare_read_tag %p\n", con);
1642 con->in_base_pos = 0;
1643 con->in_tag = CEPH_MSGR_TAG_READY;
1644}
1645
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001646static void prepare_read_keepalive_ack(struct ceph_connection *con)
1647{
1648 dout("prepare_read_keepalive_ack %p\n", con);
1649 con->in_base_pos = 0;
1650}
1651
Sage Weil31b80062009-10-06 11:31:13 -07001652/*
1653 * Prepare to read a message.
1654 */
1655static int prepare_read_message(struct ceph_connection *con)
1656{
1657 dout("prepare_read_message %p\n", con);
1658 BUG_ON(con->in_msg != NULL);
1659 con->in_base_pos = 0;
1660 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1661 return 0;
1662}
1663
1664
1665static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001666 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001667{
Alex Eldere6cee712012-05-10 10:29:50 -05001668 while (con->in_base_pos < end) {
1669 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001670 int have = size - left;
1671 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1672 if (ret <= 0)
1673 return ret;
1674 con->in_base_pos += ret;
1675 }
1676 return 1;
1677}
1678
1679
1680/*
1681 * Read all or part of the connect-side handshake on a new connection
1682 */
Sage Weileed0ef22009-11-10 14:34:36 -08001683static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001684{
Alex Elderfd516532012-05-10 10:29:50 -05001685 int size;
1686 int end;
1687 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001688
Sage Weileed0ef22009-11-10 14:34:36 -08001689 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001690
1691 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001692 size = strlen(CEPH_BANNER);
1693 end = size;
1694 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001695 if (ret <= 0)
1696 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001697
1698 size = sizeof (con->actual_peer_addr);
1699 end += size;
1700 ret = read_partial(con, end, size, &con->actual_peer_addr);
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->peer_addr_for_me);
1705 end += size;
1706 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001707 if (ret <= 0)
1708 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001709
Sage Weileed0ef22009-11-10 14:34:36 -08001710out:
1711 return ret;
1712}
1713
1714static int read_partial_connect(struct ceph_connection *con)
1715{
Alex Elderfd516532012-05-10 10:29:50 -05001716 int size;
1717 int end;
1718 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001719
1720 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1721
Alex Elderfd516532012-05-10 10:29:50 -05001722 size = sizeof (con->in_reply);
1723 end = size;
1724 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001725 if (ret <= 0)
1726 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001727
1728 size = le32_to_cpu(con->in_reply.authorizer_len);
1729 end += size;
1730 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001731 if (ret <= 0)
1732 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001733
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001734 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1735 con, (int)con->in_reply.tag,
1736 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001737 le32_to_cpu(con->in_reply.global_seq));
1738out:
1739 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001740
Sage Weil31b80062009-10-06 11:31:13 -07001741}
1742
1743/*
1744 * Verify the hello banner looks okay.
1745 */
1746static int verify_hello(struct ceph_connection *con)
1747{
1748 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001749 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001750 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001751 con->error_msg = "protocol error, bad banner";
1752 return -1;
1753 }
1754 return 0;
1755}
1756
1757static bool addr_is_blank(struct sockaddr_storage *ss)
1758{
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001759 struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1760 struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1761
Sage Weil31b80062009-10-06 11:31:13 -07001762 switch (ss->ss_family) {
1763 case AF_INET:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001764 return addr->s_addr == htonl(INADDR_ANY);
Sage Weil31b80062009-10-06 11:31:13 -07001765 case AF_INET6:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001766 return ipv6_addr_any(addr6);
1767 default:
1768 return true;
Sage Weil31b80062009-10-06 11:31:13 -07001769 }
Sage Weil31b80062009-10-06 11:31:13 -07001770}
1771
1772static int addr_port(struct sockaddr_storage *ss)
1773{
1774 switch (ss->ss_family) {
1775 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001776 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001777 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001778 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001779 }
1780 return 0;
1781}
1782
1783static void addr_set_port(struct sockaddr_storage *ss, int p)
1784{
1785 switch (ss->ss_family) {
1786 case AF_INET:
1787 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001788 break;
Sage Weil31b80062009-10-06 11:31:13 -07001789 case AF_INET6:
1790 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001791 break;
Sage Weil31b80062009-10-06 11:31:13 -07001792 }
1793}
1794
1795/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001796 * Unlike other *_pton function semantics, zero indicates success.
1797 */
1798static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1799 char delim, const char **ipend)
1800{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001801 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1802 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001803
1804 memset(ss, 0, sizeof(*ss));
1805
1806 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1807 ss->ss_family = AF_INET;
1808 return 0;
1809 }
1810
1811 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1812 ss->ss_family = AF_INET6;
1813 return 0;
1814 }
1815
1816 return -EINVAL;
1817}
1818
1819/*
1820 * Extract hostname string and resolve using kernel DNS facility.
1821 */
1822#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1823static int ceph_dns_resolve_name(const char *name, size_t namelen,
1824 struct sockaddr_storage *ss, char delim, const char **ipend)
1825{
1826 const char *end, *delim_p;
1827 char *colon_p, *ip_addr = NULL;
1828 int ip_len, ret;
1829
1830 /*
1831 * The end of the hostname occurs immediately preceding the delimiter or
1832 * the port marker (':') where the delimiter takes precedence.
1833 */
1834 delim_p = memchr(name, delim, namelen);
1835 colon_p = memchr(name, ':', namelen);
1836
1837 if (delim_p && colon_p)
1838 end = delim_p < colon_p ? delim_p : colon_p;
1839 else if (!delim_p && colon_p)
1840 end = colon_p;
1841 else {
1842 end = delim_p;
1843 if (!end) /* case: hostname:/ */
1844 end = name + namelen;
1845 }
1846
1847 if (end <= name)
1848 return -EINVAL;
1849
1850 /* do dns_resolve upcall */
1851 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1852 if (ip_len > 0)
1853 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1854 else
1855 ret = -ESRCH;
1856
1857 kfree(ip_addr);
1858
1859 *ipend = end;
1860
1861 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1862 ret, ret ? "failed" : ceph_pr_addr(ss));
1863
1864 return ret;
1865}
1866#else
1867static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1868 struct sockaddr_storage *ss, char delim, const char **ipend)
1869{
1870 return -EINVAL;
1871}
1872#endif
1873
1874/*
1875 * Parse a server name (IP or hostname). If a valid IP address is not found
1876 * then try to extract a hostname to resolve using userspace DNS upcall.
1877 */
1878static int ceph_parse_server_name(const char *name, size_t namelen,
1879 struct sockaddr_storage *ss, char delim, const char **ipend)
1880{
1881 int ret;
1882
1883 ret = ceph_pton(name, namelen, ss, delim, ipend);
1884 if (ret)
1885 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1886
1887 return ret;
1888}
1889
1890/*
Sage Weil31b80062009-10-06 11:31:13 -07001891 * Parse an ip[:port] list into an addr array. Use the default
1892 * monitor port if a port isn't specified.
1893 */
1894int ceph_parse_ips(const char *c, const char *end,
1895 struct ceph_entity_addr *addr,
1896 int max_count, int *count)
1897{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001898 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001899 const char *p = c;
1900
1901 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1902 for (i = 0; i < max_count; i++) {
1903 const char *ipend;
1904 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001905 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001906 char delim = ',';
1907
1908 if (*p == '[') {
1909 delim = ']';
1910 p++;
1911 }
Sage Weil31b80062009-10-06 11:31:13 -07001912
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001913 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1914 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001915 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001916 ret = -EINVAL;
1917
Sage Weil31b80062009-10-06 11:31:13 -07001918 p = ipend;
1919
Sage Weil39139f62010-07-08 09:54:52 -07001920 if (delim == ']') {
1921 if (*p != ']') {
1922 dout("missing matching ']'\n");
1923 goto bad;
1924 }
1925 p++;
1926 }
1927
Sage Weil31b80062009-10-06 11:31:13 -07001928 /* port? */
1929 if (p < end && *p == ':') {
1930 port = 0;
1931 p++;
1932 while (p < end && *p >= '0' && *p <= '9') {
1933 port = (port * 10) + (*p - '0');
1934 p++;
1935 }
Ilya Dryomovf48db1e2013-12-30 19:21:29 +02001936 if (port == 0)
1937 port = CEPH_MON_PORT;
1938 else if (port > 65535)
Sage Weil31b80062009-10-06 11:31:13 -07001939 goto bad;
1940 } else {
1941 port = CEPH_MON_PORT;
1942 }
1943
1944 addr_set_port(ss, port);
1945
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001946 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001947
1948 if (p == end)
1949 break;
1950 if (*p != ',')
1951 goto bad;
1952 p++;
1953 }
1954
1955 if (p != end)
1956 goto bad;
1957
1958 if (count)
1959 *count = i + 1;
1960 return 0;
1961
1962bad:
Sage Weil39139f62010-07-08 09:54:52 -07001963 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001964 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001965}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001966EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001967
Sage Weileed0ef22009-11-10 14:34:36 -08001968static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001969{
Sage Weileed0ef22009-11-10 14:34:36 -08001970 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001971
1972 if (verify_hello(con) < 0)
1973 return -1;
1974
Sage Weil63f2d212009-11-03 15:17:56 -08001975 ceph_decode_addr(&con->actual_peer_addr);
1976 ceph_decode_addr(&con->peer_addr_for_me);
1977
Sage Weil31b80062009-10-06 11:31:13 -07001978 /*
1979 * Make sure the other end is who we wanted. note that the other
1980 * end may not yet know their ip address, so if it's 0.0.0.0, give
1981 * them the benefit of the doubt.
1982 */
Sage Weil103e2d32010-01-07 16:12:36 -08001983 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1984 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001985 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1986 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001987 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
1988 ceph_pr_addr(&con->peer_addr.in_addr),
1989 (int)le32_to_cpu(con->peer_addr.nonce),
1990 ceph_pr_addr(&con->actual_peer_addr.in_addr),
1991 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001992 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001993 return -1;
1994 }
1995
1996 /*
1997 * did we learn our address?
1998 */
1999 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2000 int port = addr_port(&con->msgr->inst.addr.in_addr);
2001
2002 memcpy(&con->msgr->inst.addr.in_addr,
2003 &con->peer_addr_for_me.in_addr,
2004 sizeof(con->peer_addr_for_me.in_addr));
2005 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08002006 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08002007 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002008 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002009 }
2010
Sage Weileed0ef22009-11-10 14:34:36 -08002011 return 0;
2012}
2013
2014static int process_connect(struct ceph_connection *con)
2015{
Ilya Dryomov859bff52015-10-28 23:50:58 +01002016 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2017 u64 req_feat = from_msgr(con->msgr)->required_features;
Ilya Dryomov2b3e0c92013-12-24 21:19:24 +02002018 u64 server_feat = ceph_sanitize_features(
2019 le64_to_cpu(con->in_reply.features));
Sage Weil0da5d702011-05-19 11:21:05 -07002020 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08002021
Sage Weileed0ef22009-11-10 14:34:36 -08002022 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2023
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002024 if (con->auth_reply_buf) {
2025 /*
2026 * Any connection that defines ->get_authorizer()
2027 * should also define ->verify_authorizer_reply().
2028 * See get_connect_authorizer().
2029 */
Ilya Dryomov0dde5842016-12-02 16:35:09 +01002030 ret = con->ops->verify_authorizer_reply(con);
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002031 if (ret < 0) {
2032 con->error_msg = "bad authorize reply";
2033 return ret;
2034 }
2035 }
2036
Sage Weil31b80062009-10-06 11:31:13 -07002037 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08002038 case CEPH_MSGR_TAG_FEATURES:
2039 pr_err("%s%lld %s feature set mismatch,"
2040 " my %llx < server's %llx, missing %llx\n",
2041 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002042 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002043 sup_feat, server_feat, server_feat & ~sup_feat);
2044 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002045 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002046 return -1;
2047
Sage Weil31b80062009-10-06 11:31:13 -07002048 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07002049 pr_err("%s%lld %s protocol version mismatch,"
2050 " my %d != server's %d\n",
2051 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002052 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07002053 le32_to_cpu(con->out_connect.protocol_version),
2054 le32_to_cpu(con->in_reply.protocol_version));
2055 con->error_msg = "protocol version mismatch";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002056 reset_connection(con);
Sage Weil31b80062009-10-06 11:31:13 -07002057 return -1;
2058
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002059 case CEPH_MSGR_TAG_BADAUTHORIZER:
2060 con->auth_retry++;
2061 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2062 con->auth_retry);
2063 if (con->auth_retry == 2) {
2064 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002065 return -1;
2066 }
Jim Schutt6d4221b2012-08-10 10:37:38 -07002067 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002068 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002069 if (ret < 0)
2070 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07002071 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002072 break;
Sage Weil31b80062009-10-06 11:31:13 -07002073
2074 case CEPH_MSGR_TAG_RESETSESSION:
2075 /*
2076 * If we connected with a large connect_seq but the peer
2077 * has no record of a session with us (no connection, or
2078 * connect_seq == 0), they will send RESETSESION to indicate
2079 * that they must have reset their session, and may have
2080 * dropped messages.
2081 */
2082 dout("process_connect got RESET peer seq %u\n",
Sage Weil5bdca4e2012-07-10 11:53:34 -07002083 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002084 pr_err("%s%lld %s connection reset\n",
2085 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002086 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002087 reset_connection(con);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002088 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002089 ret = prepare_write_connect(con);
2090 if (ret < 0)
2091 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002092 prepare_read_connect(con);
2093
2094 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08002095 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002096 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2097 if (con->ops->peer_reset)
2098 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08002099 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002100 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07002101 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07002102 break;
2103
2104 case CEPH_MSGR_TAG_RETRY_SESSION:
2105 /*
2106 * If we sent a smaller connect_seq than the peer has, try
2107 * again with a larger value.
2108 */
Sage Weil5bdca4e2012-07-10 11:53:34 -07002109 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002110 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil5bdca4e2012-07-10 11:53:34 -07002111 le32_to_cpu(con->in_reply.connect_seq));
2112 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002113 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002114 ret = prepare_write_connect(con);
2115 if (ret < 0)
2116 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002117 prepare_read_connect(con);
2118 break;
2119
2120 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2121 /*
2122 * If we sent a smaller global_seq than the peer has, try
2123 * again with a larger value.
2124 */
Sage Weileed0ef22009-11-10 14:34:36 -08002125 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002126 con->peer_global_seq,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002127 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002128 get_global_seq(con->msgr,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002129 le32_to_cpu(con->in_reply.global_seq));
Jim Schutt6d4221b2012-08-10 10:37:38 -07002130 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002131 ret = prepare_write_connect(con);
2132 if (ret < 0)
2133 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002134 prepare_read_connect(con);
2135 break;
2136
Sage Weil3a230832013-03-25 08:47:40 -07002137 case CEPH_MSGR_TAG_SEQ:
Sage Weil31b80062009-10-06 11:31:13 -07002138 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08002139 if (req_feat & ~server_feat) {
2140 pr_err("%s%lld %s protocol feature mismatch,"
2141 " my required %llx > server's %llx, need %llx\n",
2142 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002143 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002144 req_feat, server_feat, req_feat & ~server_feat);
2145 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002146 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002147 return -1;
2148 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002149
Alex Elder122070a2012-12-26 10:43:57 -06002150 WARN_ON(con->state != CON_STATE_NEGOTIATING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002151 con->state = CON_STATE_OPEN;
Sage Weil20e55c42013-03-25 09:30:13 -07002152 con->auth_retry = 0; /* we authenticated; clear flag */
Sage Weil31b80062009-10-06 11:31:13 -07002153 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2154 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07002155 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07002156 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2157 con->peer_global_seq,
2158 le32_to_cpu(con->in_reply.connect_seq),
2159 con->connect_seq);
2160 WARN_ON(con->connect_seq !=
2161 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08002162
2163 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elderc9ffc772013-02-20 10:25:12 -06002164 con_flag_set(con, CON_FLAG_LOSSYTX);
Sage Weil92ac41d2009-12-14 14:56:56 -08002165
Sage Weil85effe12012-07-30 16:22:05 -07002166 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -07002167
Sage Weil3a230832013-03-25 08:47:40 -07002168 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2169 prepare_write_seq(con);
2170 prepare_read_seq(con);
2171 } else {
2172 prepare_read_tag(con);
2173 }
Sage Weil31b80062009-10-06 11:31:13 -07002174 break;
2175
2176 case CEPH_MSGR_TAG_WAIT:
2177 /*
2178 * If there is a connection race (we are opening
2179 * connections to each other), one of us may just have
2180 * to WAIT. This shouldn't happen if we are the
2181 * client.
2182 */
Sage Weil04177882011-05-12 15:33:17 -07002183 con->error_msg = "protocol error, got WAIT as client";
2184 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07002185
2186 default:
Sage Weil31b80062009-10-06 11:31:13 -07002187 con->error_msg = "protocol error, garbage tag during connect";
2188 return -1;
2189 }
2190 return 0;
2191}
2192
2193
2194/*
2195 * read (part of) an ack
2196 */
2197static int read_partial_ack(struct ceph_connection *con)
2198{
Alex Elderfd516532012-05-10 10:29:50 -05002199 int size = sizeof (con->in_temp_ack);
2200 int end = size;
Sage Weil31b80062009-10-06 11:31:13 -07002201
Alex Elderfd516532012-05-10 10:29:50 -05002202 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07002203}
2204
Sage Weil31b80062009-10-06 11:31:13 -07002205/*
2206 * We can finally discard anything that's been acked.
2207 */
2208static void process_ack(struct ceph_connection *con)
2209{
2210 struct ceph_msg *m;
2211 u64 ack = le64_to_cpu(con->in_temp_ack);
2212 u64 seq;
2213
Sage Weil31b80062009-10-06 11:31:13 -07002214 while (!list_empty(&con->out_sent)) {
2215 m = list_first_entry(&con->out_sent, struct ceph_msg,
2216 list_head);
2217 seq = le64_to_cpu(m->hdr.seq);
2218 if (seq > ack)
2219 break;
2220 dout("got ack for seq %llu type %d at %p\n", seq,
2221 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07002222 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07002223 ceph_msg_remove(m);
2224 }
Sage Weil31b80062009-10-06 11:31:13 -07002225 prepare_read_tag(con);
2226}
2227
2228
Yehuda Sadeh24504182010-01-08 13:58:34 -08002229static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002230 struct kvec *section,
2231 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002232{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002233 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07002234
Yehuda Sadeh24504182010-01-08 13:58:34 -08002235 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07002236
Yehuda Sadeh24504182010-01-08 13:58:34 -08002237 while (section->iov_len < sec_len) {
2238 BUG_ON(section->iov_base == NULL);
2239 left = sec_len - section->iov_len;
2240 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2241 section->iov_len, left);
2242 if (ret <= 0)
2243 return ret;
2244 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002245 }
Alex Elderfe3ad592012-02-15 07:43:54 -06002246 if (section->iov_len == sec_len)
2247 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002248
2249 return 1;
2250}
2251
Alex Elder34d2d202013-03-08 20:58:59 -06002252static int read_partial_msg_data(struct ceph_connection *con)
2253{
2254 struct ceph_msg *msg = con->in_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05002255 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002256 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elder686be202013-03-11 23:34:23 -05002257 struct page *page;
2258 size_t page_offset;
2259 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05002260 u32 crc = 0;
Alex Elder34d2d202013-03-08 20:58:59 -06002261 int ret;
2262
2263 BUG_ON(!msg);
Alex Elder5240d9f2013-03-14 14:09:06 -05002264 if (list_empty(&msg->data))
Alex Elder4c59b4a2013-03-11 23:34:23 -05002265 return -EIO;
Alex Elder34d2d202013-03-08 20:58:59 -06002266
Alex Elderf5db90b2013-03-11 23:34:23 -05002267 if (do_datacrc)
2268 crc = con->in_data_crc;
Alex Elder643c68a2013-03-11 23:34:23 -05002269 while (cursor->resid) {
Shraddha Barke343128c2015-10-19 21:59:00 +05302270 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Alex Elder686be202013-03-11 23:34:23 -05002271 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
Alex Elderf5db90b2013-03-11 23:34:23 -05002272 if (ret <= 0) {
2273 if (do_datacrc)
2274 con->in_data_crc = crc;
2275
Alex Elder686be202013-03-11 23:34:23 -05002276 return ret;
Alex Elderf5db90b2013-03-11 23:34:23 -05002277 }
Alex Elder686be202013-03-11 23:34:23 -05002278
2279 if (do_datacrc)
Alex Elderf5db90b2013-03-11 23:34:23 -05002280 crc = ceph_crc32c_page(crc, page, page_offset, ret);
Shraddha Barke343128c2015-10-19 21:59:00 +05302281 (void) ceph_msg_data_advance(cursor, (size_t)ret);
Alex Elder34d2d202013-03-08 20:58:59 -06002282 }
Alex Elderf5db90b2013-03-11 23:34:23 -05002283 if (do_datacrc)
2284 con->in_data_crc = crc;
Alex Elder34d2d202013-03-08 20:58:59 -06002285
2286 return 1; /* must return > 0 to indicate success */
2287}
2288
Sage Weil31b80062009-10-06 11:31:13 -07002289/*
2290 * read (part of) a message.
2291 */
Alex Elder686be202013-03-11 23:34:23 -05002292static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2293
Sage Weil31b80062009-10-06 11:31:13 -07002294static int read_partial_message(struct ceph_connection *con)
2295{
2296 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05002297 int size;
2298 int end;
Sage Weil31b80062009-10-06 11:31:13 -07002299 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00002300 unsigned int front_len, middle_len, data_len;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002301 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Yan, Zheng33d07332014-11-04 16:33:37 +08002302 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
Sage Weilae187562010-04-22 07:47:01 -07002303 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06002304 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07002305
2306 dout("read_partial_message con %p msg %p\n", con, m);
2307
2308 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05002309 size = sizeof (con->in_hdr);
2310 end = size;
2311 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05002312 if (ret <= 0)
2313 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06002314
2315 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2316 if (cpu_to_le32(crc) != con->in_hdr.crc) {
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002317 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
Alex Elderfe3ad592012-02-15 07:43:54 -06002318 crc, con->in_hdr.crc);
2319 return -EBADMSG;
2320 }
2321
Sage Weil31b80062009-10-06 11:31:13 -07002322 front_len = le32_to_cpu(con->in_hdr.front_len);
2323 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2324 return -EIO;
2325 middle_len = le32_to_cpu(con->in_hdr.middle_len);
Alex Elder7b11ba32013-03-08 18:51:03 -06002326 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
Sage Weil31b80062009-10-06 11:31:13 -07002327 return -EIO;
2328 data_len = le32_to_cpu(con->in_hdr.data_len);
2329 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2330 return -EIO;
2331
Sage Weilae187562010-04-22 07:47:01 -07002332 /* verify seq# */
2333 seq = le64_to_cpu(con->in_hdr.seq);
2334 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07002335 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07002336 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002337 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07002338 seq, con->in_seq + 1);
2339 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002340 sizeof_footer(con);
Sage Weilae187562010-04-22 07:47:01 -07002341 con->in_tag = CEPH_MSGR_TAG_READY;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002342 return 1;
Sage Weilae187562010-04-22 07:47:01 -07002343 } else if ((s64)seq - (s64)con->in_seq > 1) {
2344 pr_err("read_partial_message bad seq %lld expected %lld\n",
2345 seq, con->in_seq + 1);
2346 con->error_msg = "bad message sequence # for incoming message";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002347 return -EBADE;
Sage Weilae187562010-04-22 07:47:01 -07002348 }
2349
Sage Weil31b80062009-10-06 11:31:13 -07002350 /* allocate message? */
2351 if (!con->in_msg) {
Sage Weil4740a622012-07-30 18:19:30 -07002352 int skip = 0;
2353
Sage Weil31b80062009-10-06 11:31:13 -07002354 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
Alex Elder6ebc8b32013-03-08 18:51:03 -06002355 front_len, data_len);
Sage Weil4740a622012-07-30 18:19:30 -07002356 ret = ceph_con_in_msg_alloc(con, &skip);
2357 if (ret < 0)
2358 return ret;
Alex Elderf759ebb2013-04-05 14:46:01 -05002359
2360 BUG_ON(!con->in_msg ^ skip);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002361 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07002362 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07002363 dout("alloc_msg said skip message\n");
Sage Weil31b80062009-10-06 11:31:13 -07002364 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002365 sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07002366 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002367 con->in_seq++;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002368 return 1;
Sage Weil31b80062009-10-06 11:31:13 -07002369 }
Alex Elder38941f82012-06-01 14:56:43 -05002370
Sage Weil4740a622012-07-30 18:19:30 -07002371 BUG_ON(!con->in_msg);
Alex Elder38941f82012-06-01 14:56:43 -05002372 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002373 m = con->in_msg;
2374 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002375 if (m->middle)
2376 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002377
Alex Elder78625052013-03-06 23:39:39 -06002378 /* prepare for data payload, if any */
Sage Weila4107022012-07-30 16:20:25 -07002379
Alex Elder78625052013-03-06 23:39:39 -06002380 if (data_len)
Alex Elder98fa5dd2013-04-02 12:09:50 -05002381 prepare_message_data(con->in_msg, data_len);
Sage Weil31b80062009-10-06 11:31:13 -07002382 }
2383
2384 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002385 ret = read_partial_message_section(con, &m->front, front_len,
2386 &con->in_front_crc);
2387 if (ret <= 0)
2388 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002389
2390 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002391 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07002392 ret = read_partial_message_section(con, &m->middle->vec,
2393 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002394 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07002395 if (ret <= 0)
2396 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002397 }
2398
2399 /* (page) data */
Alex Elder34d2d202013-03-08 20:58:59 -06002400 if (data_len) {
2401 ret = read_partial_msg_data(con);
2402 if (ret <= 0)
2403 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002404 }
2405
Sage Weil31b80062009-10-06 11:31:13 -07002406 /* footer */
Ilya Dryomov89f08172016-02-20 15:56:07 +01002407 size = sizeof_footer(con);
Alex Elderfd516532012-05-10 10:29:50 -05002408 end += size;
2409 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05002410 if (ret <= 0)
2411 return ret;
2412
Yan, Zheng33d07332014-11-04 16:33:37 +08002413 if (!need_sign) {
2414 m->footer.flags = m->old_footer.flags;
2415 m->footer.sig = 0;
2416 }
2417
Sage Weil31b80062009-10-06 11:31:13 -07002418 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2419 m, front_len, m->footer.front_crc, middle_len,
2420 m->footer.middle_crc, data_len, m->footer.data_crc);
2421
2422 /* crc ok? */
2423 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2424 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2425 m, con->in_front_crc, m->footer.front_crc);
2426 return -EBADMSG;
2427 }
2428 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2429 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2430 m, con->in_middle_crc, m->footer.middle_crc);
2431 return -EBADMSG;
2432 }
Alex Elderbca064d2012-02-15 07:43:54 -06002433 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07002434 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2435 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2436 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2437 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2438 return -EBADMSG;
2439 }
2440
Yan, Zheng33d07332014-11-04 16:33:37 +08002441 if (need_sign && con->ops->check_message_signature &&
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002442 con->ops->check_message_signature(m)) {
Yan, Zheng33d07332014-11-04 16:33:37 +08002443 pr_err("read_partial_message %p signature check failed\n", m);
2444 return -EBADMSG;
2445 }
2446
Sage Weil31b80062009-10-06 11:31:13 -07002447 return 1; /* done! */
2448}
2449
2450/*
2451 * Process message. This happens in the worker thread. The callback should
2452 * be careful not to do anything that waits on other incoming messages or it
2453 * may deadlock.
2454 */
2455static void process_message(struct ceph_connection *con)
2456{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002457 struct ceph_msg *msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07002458
Alex Elder38941f82012-06-01 14:56:43 -05002459 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002460 con->in_msg = NULL;
2461
2462 /* if first message, set peer_name */
2463 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07002464 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07002465
Sage Weil31b80062009-10-06 11:31:13 -07002466 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08002467 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002468
2469 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2470 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07002471 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07002472 le16_to_cpu(msg->hdr.type),
2473 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2474 le32_to_cpu(msg->hdr.front_len),
2475 le32_to_cpu(msg->hdr.data_len),
2476 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2477 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08002478
2479 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002480}
2481
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002482static int read_keepalive_ack(struct ceph_connection *con)
2483{
2484 struct ceph_timespec ceph_ts;
2485 size_t size = sizeof(ceph_ts);
2486 int ret = read_partial(con, size, size, &ceph_ts);
2487 if (ret <= 0)
2488 return ret;
2489 ceph_decode_timespec(&con->last_keepalive_ack, &ceph_ts);
2490 prepare_read_tag(con);
2491 return 1;
2492}
Sage Weil31b80062009-10-06 11:31:13 -07002493
2494/*
2495 * Write something to the socket. Called in a worker thread when the
2496 * socket appears to be writeable and we have something ready to send.
2497 */
2498static int try_write(struct ceph_connection *con)
2499{
Sage Weil31b80062009-10-06 11:31:13 -07002500 int ret = 1;
2501
Sage Weild59315c2012-06-21 12:49:23 -07002502 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002503
Sage Weil31b80062009-10-06 11:31:13 -07002504more:
2505 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2506
2507 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002508 if (con->state == CON_STATE_PREOPEN) {
2509 BUG_ON(con->sock);
2510 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002511
Alex Eldere2200422012-05-23 14:35:23 -05002512 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002513 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002514 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002515
Sage Weilcf3e5c42009-12-11 09:48:05 -08002516 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002517 con->in_tag = CEPH_MSGR_TAG_READY;
2518 dout("try_write initiating connect on %p new state %lu\n",
2519 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002520 ret = ceph_tcp_connect(con);
2521 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002522 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002523 goto out;
2524 }
2525 }
2526
2527more_kvec:
2528 /* kvec data queued? */
Ilya Dryomov67645d72015-12-28 13:18:34 +03002529 if (con->out_kvec_left) {
2530 ret = write_partial_kvec(con);
Sage Weil31b80062009-10-06 11:31:13 -07002531 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002532 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002533 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03002534 if (con->out_skip) {
2535 ret = write_partial_skip(con);
Sage Weil31b80062009-10-06 11:31:13 -07002536 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002537 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002538 }
2539
2540 /* msg pages? */
2541 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002542 if (con->out_msg_done) {
2543 ceph_msg_put(con->out_msg);
2544 con->out_msg = NULL; /* we're done with this one */
2545 goto do_next;
2546 }
2547
Alex Elder34d2d202013-03-08 20:58:59 -06002548 ret = write_partial_message_data(con);
Sage Weil31b80062009-10-06 11:31:13 -07002549 if (ret == 1)
2550 goto more_kvec; /* we need to send the footer, too! */
2551 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002552 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002553 if (ret < 0) {
Alex Elder34d2d202013-03-08 20:58:59 -06002554 dout("try_write write_partial_message_data err %d\n",
Sage Weil31b80062009-10-06 11:31:13 -07002555 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002556 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002557 }
2558 }
2559
Sage Weilc86a2932009-12-14 14:04:30 -08002560do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002561 if (con->state == CON_STATE_OPEN) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002562 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2563 prepare_write_keepalive(con);
2564 goto more;
2565 }
Sage Weil31b80062009-10-06 11:31:13 -07002566 /* is anything else pending? */
2567 if (!list_empty(&con->out_queue)) {
2568 prepare_write_message(con);
2569 goto more;
2570 }
2571 if (con->in_seq > con->in_seq_acked) {
2572 prepare_write_ack(con);
2573 goto more;
2574 }
Sage Weil31b80062009-10-06 11:31:13 -07002575 }
2576
2577 /* Nothing to do! */
Alex Elderc9ffc772013-02-20 10:25:12 -06002578 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07002579 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002580 ret = 0;
2581out:
Sage Weil42961d22011-01-25 08:19:34 -08002582 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002583 return ret;
2584}
2585
2586
2587
2588/*
2589 * Read what we can from the socket.
2590 */
2591static int try_read(struct ceph_connection *con)
2592{
Sage Weil31b80062009-10-06 11:31:13 -07002593 int ret = -1;
2594
Sage Weil31b80062009-10-06 11:31:13 -07002595more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002596 dout("try_read start on %p state %lu\n", con, con->state);
2597 if (con->state != CON_STATE_CONNECTING &&
2598 con->state != CON_STATE_NEGOTIATING &&
2599 con->state != CON_STATE_OPEN)
2600 return 0;
2601
2602 BUG_ON(!con->sock);
2603
Sage Weil31b80062009-10-06 11:31:13 -07002604 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2605 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002606
Sage Weil8dacc7d2012-07-20 17:24:40 -07002607 if (con->state == CON_STATE_CONNECTING) {
Alex Elder7593af92012-05-24 11:55:03 -05002608 dout("try_read connecting\n");
2609 ret = read_partial_banner(con);
2610 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002611 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002612 ret = process_banner(con);
2613 if (ret < 0)
2614 goto out;
2615
Sage Weil8dacc7d2012-07-20 17:24:40 -07002616 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002617
Jim Schutt6d4221b2012-08-10 10:37:38 -07002618 /*
2619 * Received banner is good, exchange connection info.
2620 * Do not reset out_kvec, as sending our banner raced
2621 * with receiving peer banner after connect completed.
2622 */
Alex Elder7593af92012-05-24 11:55:03 -05002623 ret = prepare_write_connect(con);
2624 if (ret < 0)
2625 goto out;
2626 prepare_read_connect(con);
2627
2628 /* Send connection info before awaiting response */
Sage Weil0da5d702011-05-19 11:21:05 -07002629 goto out;
2630 }
2631
Sage Weil8dacc7d2012-07-20 17:24:40 -07002632 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002633 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002634 ret = read_partial_connect(con);
2635 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002636 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002637 ret = process_connect(con);
2638 if (ret < 0)
2639 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002640 goto more;
2641 }
2642
Alex Elder122070a2012-12-26 10:43:57 -06002643 WARN_ON(con->state != CON_STATE_OPEN);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002644
Sage Weil31b80062009-10-06 11:31:13 -07002645 if (con->in_base_pos < 0) {
2646 /*
2647 * skipping + discarding content.
2648 *
2649 * FIXME: there must be a better way to do this!
2650 */
Alex Elder84495f42012-02-15 07:43:55 -06002651 static char buf[SKIP_BUF_SIZE];
2652 int skip = min((int) sizeof (buf), -con->in_base_pos);
2653
Sage Weil31b80062009-10-06 11:31:13 -07002654 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2655 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2656 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002657 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002658 con->in_base_pos += ret;
2659 if (con->in_base_pos)
2660 goto more;
2661 }
2662 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2663 /*
2664 * what's next?
2665 */
2666 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2667 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002668 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002669 dout("try_read got tag %d\n", (int)con->in_tag);
2670 switch (con->in_tag) {
2671 case CEPH_MSGR_TAG_MSG:
2672 prepare_read_message(con);
2673 break;
2674 case CEPH_MSGR_TAG_ACK:
2675 prepare_read_ack(con);
2676 break;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002677 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2678 prepare_read_keepalive_ack(con);
2679 break;
Sage Weil31b80062009-10-06 11:31:13 -07002680 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002681 con_close_socket(con);
2682 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002683 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002684 default:
2685 goto bad_tag;
2686 }
2687 }
2688 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2689 ret = read_partial_message(con);
2690 if (ret <= 0) {
2691 switch (ret) {
2692 case -EBADMSG:
Ilya Dryomova51983e2015-10-28 23:52:06 +01002693 con->error_msg = "bad crc/signature";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002694 /* fall through */
2695 case -EBADE:
Sage Weil31b80062009-10-06 11:31:13 -07002696 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002697 break;
Sage Weil31b80062009-10-06 11:31:13 -07002698 case -EIO:
2699 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002700 break;
Sage Weil31b80062009-10-06 11:31:13 -07002701 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002702 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002703 }
2704 if (con->in_tag == CEPH_MSGR_TAG_READY)
2705 goto more;
2706 process_message(con);
Sage Weil7b862e02012-07-30 18:16:56 -07002707 if (con->state == CON_STATE_OPEN)
2708 prepare_read_tag(con);
Sage Weil31b80062009-10-06 11:31:13 -07002709 goto more;
2710 }
Sage Weil3a230832013-03-25 08:47:40 -07002711 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2712 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2713 /*
2714 * the final handshake seq exchange is semantically
2715 * equivalent to an ACK
2716 */
Sage Weil31b80062009-10-06 11:31:13 -07002717 ret = read_partial_ack(con);
2718 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002719 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002720 process_ack(con);
2721 goto more;
2722 }
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002723 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2724 ret = read_keepalive_ack(con);
2725 if (ret <= 0)
2726 goto out;
2727 goto more;
2728 }
Sage Weil31b80062009-10-06 11:31:13 -07002729
Sage Weil31b80062009-10-06 11:31:13 -07002730out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002731 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002732 return ret;
2733
2734bad_tag:
2735 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2736 con->error_msg = "protocol error, garbage tag";
2737 ret = -1;
2738 goto out;
2739}
2740
2741
2742/*
Alex Elder802c6d92012-10-08 20:37:30 -07002743 * Atomically queue work on a connection after the specified delay.
2744 * Bump @con reference to avoid races with connection teardown.
2745 * Returns 0 if work was queued, or an error code otherwise.
Sage Weil31b80062009-10-06 11:31:13 -07002746 */
Alex Elder802c6d92012-10-08 20:37:30 -07002747static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
Sage Weil31b80062009-10-06 11:31:13 -07002748{
Sage Weil31b80062009-10-06 11:31:13 -07002749 if (!con->ops->get(con)) {
Alex Elder802c6d92012-10-08 20:37:30 -07002750 dout("%s %p ref count 0\n", __func__, con);
Alex Elder802c6d92012-10-08 20:37:30 -07002751 return -ENOENT;
Sage Weil31b80062009-10-06 11:31:13 -07002752 }
2753
Alex Elder802c6d92012-10-08 20:37:30 -07002754 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2755 dout("%s %p - already queued\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -07002756 con->ops->put(con);
Alex Elder802c6d92012-10-08 20:37:30 -07002757 return -EBUSY;
Sage Weil31b80062009-10-06 11:31:13 -07002758 }
Alex Elder802c6d92012-10-08 20:37:30 -07002759
2760 dout("%s %p %lu\n", __func__, con, delay);
Alex Elder802c6d92012-10-08 20:37:30 -07002761 return 0;
2762}
2763
2764static void queue_con(struct ceph_connection *con)
2765{
2766 (void) queue_con_delay(con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002767}
2768
Ilya Dryomov37ab77a2014-06-24 16:21:45 +04002769static void cancel_con(struct ceph_connection *con)
2770{
2771 if (cancel_delayed_work(&con->work)) {
2772 dout("%s %p\n", __func__, con);
2773 con->ops->put(con);
2774 }
2775}
2776
Alex Elder7bb21d682012-12-07 19:50:07 -06002777static bool con_sock_closed(struct ceph_connection *con)
2778{
Alex Elderc9ffc772013-02-20 10:25:12 -06002779 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
Alex Elder7bb21d682012-12-07 19:50:07 -06002780 return false;
2781
2782#define CASE(x) \
2783 case CON_STATE_ ## x: \
2784 con->error_msg = "socket closed (con state " #x ")"; \
2785 break;
2786
2787 switch (con->state) {
2788 CASE(CLOSED);
2789 CASE(PREOPEN);
2790 CASE(CONNECTING);
2791 CASE(NEGOTIATING);
2792 CASE(OPEN);
2793 CASE(STANDBY);
2794 default:
Joe Perchesb9a67892014-09-09 21:17:29 -07002795 pr_warn("%s con %p unrecognized state %lu\n",
Alex Elder7bb21d682012-12-07 19:50:07 -06002796 __func__, con, con->state);
2797 con->error_msg = "unrecognized con state";
2798 BUG();
2799 break;
2800 }
2801#undef CASE
2802
2803 return true;
2804}
2805
Alex Elderf20a39f2013-02-19 12:25:57 -06002806static bool con_backoff(struct ceph_connection *con)
2807{
2808 int ret;
2809
2810 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2811 return false;
2812
2813 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2814 if (ret) {
2815 dout("%s: con %p FAILED to back off %lu\n", __func__,
2816 con, con->delay);
2817 BUG_ON(ret == -ENOENT);
2818 con_flag_set(con, CON_FLAG_BACKOFF);
2819 }
2820
2821 return true;
2822}
2823
Alex Elder93209262013-02-19 12:25:57 -06002824/* Finish fault handling; con->mutex must *not* be held here */
2825
2826static void con_fault_finish(struct ceph_connection *con)
2827{
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002828 dout("%s %p\n", __func__, con);
2829
Alex Elder93209262013-02-19 12:25:57 -06002830 /*
2831 * in case we faulted due to authentication, invalidate our
2832 * current tickets so that we can get new ones.
2833 */
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002834 if (con->auth_retry) {
2835 dout("auth_retry %d, invalidating\n", con->auth_retry);
2836 if (con->ops->invalidate_authorizer)
2837 con->ops->invalidate_authorizer(con);
2838 con->auth_retry = 0;
Alex Elder93209262013-02-19 12:25:57 -06002839 }
2840
2841 if (con->ops->fault)
2842 con->ops->fault(con);
2843}
2844
Sage Weil31b80062009-10-06 11:31:13 -07002845/*
2846 * Do some work on a connection. Drop a connection ref when we're done.
2847 */
Ilya Dryomov68931622015-07-03 15:44:41 +03002848static void ceph_con_workfn(struct work_struct *work)
Sage Weil31b80062009-10-06 11:31:13 -07002849{
2850 struct ceph_connection *con = container_of(work, struct ceph_connection,
2851 work.work);
Alex Elder49659412013-02-19 12:25:57 -06002852 bool fault;
Sage Weil31b80062009-10-06 11:31:13 -07002853
Sage Weil9dd46582010-04-28 13:51:50 -07002854 mutex_lock(&con->mutex);
Alex Elder49659412013-02-19 12:25:57 -06002855 while (true) {
2856 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002857
Alex Elder49659412013-02-19 12:25:57 -06002858 if ((fault = con_sock_closed(con))) {
2859 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2860 break;
2861 }
2862 if (con_backoff(con)) {
2863 dout("%s: con %p BACKOFF\n", __func__, con);
2864 break;
2865 }
2866 if (con->state == CON_STATE_STANDBY) {
2867 dout("%s: con %p STANDBY\n", __func__, con);
2868 break;
2869 }
2870 if (con->state == CON_STATE_CLOSED) {
2871 dout("%s: con %p CLOSED\n", __func__, con);
2872 BUG_ON(con->sock);
2873 break;
2874 }
2875 if (con->state == CON_STATE_PREOPEN) {
2876 dout("%s: con %p PREOPEN\n", __func__, con);
2877 BUG_ON(con->sock);
2878 }
Sage Weil0da5d702011-05-19 11:21:05 -07002879
Alex Elder49659412013-02-19 12:25:57 -06002880 ret = try_read(con);
2881 if (ret < 0) {
2882 if (ret == -EAGAIN)
2883 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002884 if (!con->error_msg)
2885 con->error_msg = "socket error on read";
Alex Elder49659412013-02-19 12:25:57 -06002886 fault = true;
2887 break;
2888 }
2889
2890 ret = try_write(con);
2891 if (ret < 0) {
2892 if (ret == -EAGAIN)
2893 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002894 if (!con->error_msg)
2895 con->error_msg = "socket error on write";
Alex Elder49659412013-02-19 12:25:57 -06002896 fault = true;
2897 }
2898
2899 break; /* If we make it to here, we're done */
Sage Weil3a140a02012-07-30 16:24:21 -07002900 }
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002901 if (fault)
2902 con_fault(con);
Sage Weil9dd46582010-04-28 13:51:50 -07002903 mutex_unlock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002904
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002905 if (fault)
2906 con_fault_finish(con);
2907
2908 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07002909}
2910
Sage Weil31b80062009-10-06 11:31:13 -07002911/*
2912 * Generic error/fault handler. A retry mechanism is used with
2913 * exponential backoff
2914 */
Alex Elder93209262013-02-19 12:25:57 -06002915static void con_fault(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002916{
Sage Weil31b80062009-10-06 11:31:13 -07002917 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002918 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002919
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002920 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2921 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2922 con->error_msg = NULL;
2923
Alex Elder122070a2012-12-26 10:43:57 -06002924 WARN_ON(con->state != CON_STATE_CONNECTING &&
Sage Weil8dacc7d2012-07-20 17:24:40 -07002925 con->state != CON_STATE_NEGOTIATING &&
2926 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08002927
Sage Weil31b80062009-10-06 11:31:13 -07002928 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002929
Alex Elderc9ffc772013-02-20 10:25:12 -06002930 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002931 dout("fault on LOSSYTX channel, marking CLOSED\n");
2932 con->state = CON_STATE_CLOSED;
Alex Elder93209262013-02-19 12:25:57 -06002933 return;
Sage Weil3b5ede02012-07-20 15:22:53 -07002934 }
2935
Sage Weil5e095e82009-12-14 14:30:34 -08002936 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002937 BUG_ON(con->in_msg->con != con);
Sage Weil5e095e82009-12-14 14:30:34 -08002938 ceph_msg_put(con->in_msg);
2939 con->in_msg = NULL;
2940 }
Sage Weil31b80062009-10-06 11:31:13 -07002941
Sage Weile80a52d2010-02-25 12:40:45 -08002942 /* Requeue anything that hasn't been acked */
2943 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002944
Sage Weile76661d2011-03-03 10:10:15 -08002945 /* If there are no messages queued or keepalive pending, place
2946 * the connection in a STANDBY state */
2947 if (list_empty(&con->out_queue) &&
Alex Elderc9ffc772013-02-20 10:25:12 -06002948 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
Sage Weile00de342011-03-04 12:25:05 -08002949 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elderc9ffc772013-02-20 10:25:12 -06002950 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002951 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08002952 } else {
2953 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002954 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08002955 if (con->delay == 0)
2956 con->delay = BASE_DELAY_INTERVAL;
2957 else if (con->delay < MAX_DELAY_INTERVAL)
2958 con->delay *= 2;
Alex Elderc9ffc772013-02-20 10:25:12 -06002959 con_flag_set(con, CON_FLAG_BACKOFF);
Alex Elder8618e302012-10-08 20:37:30 -07002960 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -07002961 }
Sage Weil31b80062009-10-06 11:31:13 -07002962}
2963
2964
2965
2966/*
Alex Elder15d98822012-05-26 23:26:43 -05002967 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002968 */
Alex Elder15d98822012-05-26 23:26:43 -05002969void ceph_messenger_init(struct ceph_messenger *msgr,
Ilya Dryomov859bff52015-10-28 23:50:58 +01002970 struct ceph_entity_addr *myaddr)
Sage Weil31b80062009-10-06 11:31:13 -07002971{
Sage Weil31b80062009-10-06 11:31:13 -07002972 spin_lock_init(&msgr->global_seq_lock);
2973
Sage Weil31b80062009-10-06 11:31:13 -07002974 if (myaddr)
2975 msgr->inst.addr = *myaddr;
2976
2977 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002978 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002979 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002980 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002981
Guanjun Hea2a32582012-07-08 19:50:33 -07002982 atomic_set(&msgr->stopping, 0);
Ilya Dryomov757856d2015-06-25 17:47:45 +03002983 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
Sage Weil31b80062009-10-06 11:31:13 -07002984
Alex Elder15d98822012-05-26 23:26:43 -05002985 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002986}
Alex Elder15d98822012-05-26 23:26:43 -05002987EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002988
Ilya Dryomov757856d2015-06-25 17:47:45 +03002989void ceph_messenger_fini(struct ceph_messenger *msgr)
2990{
2991 put_net(read_pnet(&msgr->net));
2992}
2993EXPORT_SYMBOL(ceph_messenger_fini);
2994
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002995static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
2996{
2997 if (msg->con)
2998 msg->con->ops->put(msg->con);
2999
3000 msg->con = con ? con->ops->get(con) : NULL;
3001 BUG_ON(msg->con != con);
3002}
3003
Sage Weile00de342011-03-04 12:25:05 -08003004static void clear_standby(struct ceph_connection *con)
3005{
3006 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003007 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08003008 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003009 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08003010 con->connect_seq++;
Alex Elderc9ffc772013-02-20 10:25:12 -06003011 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3012 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
Sage Weile00de342011-03-04 12:25:05 -08003013 }
3014}
3015
Sage Weil31b80062009-10-06 11:31:13 -07003016/*
3017 * Queue up an outgoing message on the given connection.
3018 */
3019void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3020{
Sage Weila59b55a2012-07-20 15:34:04 -07003021 /* set src+dst */
3022 msg->hdr.src = con->msgr->inst.name;
3023 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3024 msg->needs_out_seq = true;
3025
3026 mutex_lock(&con->mutex);
3027
Sage Weil8dacc7d2012-07-20 17:24:40 -07003028 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07003029 dout("con_send %p closed, dropping %p\n", con, msg);
3030 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07003031 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003032 return;
3033 }
3034
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003035 msg_con_set(msg, con);
Sage Weil31b80062009-10-06 11:31:13 -07003036
Sage Weil31b80062009-10-06 11:31:13 -07003037 BUG_ON(!list_empty(&msg->list_head));
3038 list_add_tail(&msg->list_head, &con->out_queue);
3039 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3040 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3041 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3042 le32_to_cpu(msg->hdr.front_len),
3043 le32_to_cpu(msg->hdr.middle_len),
3044 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07003045
3046 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08003047 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003048
3049 /* if there wasn't anything waiting to send before, queue
3050 * new work */
Alex Elderc9ffc772013-02-20 10:25:12 -06003051 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003052 queue_con(con);
3053}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003054EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07003055
3056/*
3057 * Revoke a message that was previously queued for send
3058 */
Alex Elder6740a842012-06-01 14:56:43 -05003059void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003060{
Alex Elder6740a842012-06-01 14:56:43 -05003061 struct ceph_connection *con = msg->con;
3062
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003063 if (!con) {
3064 dout("%s msg %p null con\n", __func__, msg);
Alex Elder6740a842012-06-01 14:56:43 -05003065 return; /* Message not in our possession */
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003066 }
Alex Elder6740a842012-06-01 14:56:43 -05003067
Sage Weilec302642009-12-22 10:43:42 -08003068 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003069 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05003070 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07003071 list_del_init(&msg->list_head);
Sage Weil31b80062009-10-06 11:31:13 -07003072 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05003073
Sage Weil31b80062009-10-06 11:31:13 -07003074 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07003075 }
3076 if (con->out_msg == msg) {
Ilya Dryomov67645d72015-12-28 13:18:34 +03003077 BUG_ON(con->out_skip);
3078 /* footer */
3079 if (con->out_msg_done) {
3080 con->out_skip += con_out_kvec_skip(con);
3081 } else {
3082 BUG_ON(!msg->data_length);
Ilya Dryomov89f08172016-02-20 15:56:07 +01003083 con->out_skip += sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07003084 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003085 /* data, middle, front */
3086 if (msg->data_length)
3087 con->out_skip += msg->cursor.total_resid;
3088 if (msg->middle)
3089 con->out_skip += con_out_kvec_skip(con);
3090 con->out_skip += con_out_kvec_skip(con);
Alex Elder92ce0342012-06-04 14:43:33 -05003091
Ilya Dryomov67645d72015-12-28 13:18:34 +03003092 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3093 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3094 msg->hdr.seq = 0;
3095 con->out_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05003096 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07003097 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003098
Sage Weilec302642009-12-22 10:43:42 -08003099 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003100}
3101
3102/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003103 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08003104 */
Alex Elder8921d112012-06-01 14:56:43 -05003105void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08003106{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003107 struct ceph_connection *con = msg->con;
Alex Elder8921d112012-06-01 14:56:43 -05003108
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003109 if (!con) {
Alex Elder8921d112012-06-01 14:56:43 -05003110 dout("%s msg %p null con\n", __func__, msg);
Alex Elder8921d112012-06-01 14:56:43 -05003111 return; /* Message not in our possession */
3112 }
3113
Sage Weil350b1c32009-12-22 10:45:45 -08003114 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05003115 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00003116 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3117 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3118 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08003119
3120 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05003121 dout("%s %p msg %p revoked\n", __func__, con, msg);
3122 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08003123 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003124 front_len -
3125 middle_len -
3126 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08003127 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08003128 ceph_msg_put(con->in_msg);
3129 con->in_msg = NULL;
3130 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07003131 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08003132 } else {
Alex Elder8921d112012-06-01 14:56:43 -05003133 dout("%s %p in_msg %p msg %p no-op\n",
3134 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08003135 }
3136 mutex_unlock(&con->mutex);
3137}
3138
3139/*
Sage Weil31b80062009-10-06 11:31:13 -07003140 * Queue a keepalive byte to ensure the tcp connection is alive.
3141 */
3142void ceph_con_keepalive(struct ceph_connection *con)
3143{
Sage Weile00de342011-03-04 12:25:05 -08003144 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07003145 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08003146 clear_standby(con);
Sage Weil00650932012-07-20 15:33:04 -07003147 mutex_unlock(&con->mutex);
Alex Elderc9ffc772013-02-20 10:25:12 -06003148 if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
3149 con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003150 queue_con(con);
3151}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003152EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07003153
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003154bool ceph_con_keepalive_expired(struct ceph_connection *con,
3155 unsigned long interval)
3156{
3157 if (interval > 0 &&
3158 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
3159 struct timespec now = CURRENT_TIME;
3160 struct timespec ts;
3161 jiffies_to_timespec(interval, &ts);
3162 ts = timespec_add(con->last_keepalive_ack, ts);
3163 return timespec_compare(&now, &ts) >= 0;
3164 }
3165 return false;
3166}
3167
Alex Elder6644ed72013-03-11 23:34:24 -05003168static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
Alex Elder43794502013-03-01 18:00:16 -06003169{
Alex Elder6644ed72013-03-11 23:34:24 -05003170 struct ceph_msg_data *data;
3171
3172 if (WARN_ON(!ceph_msg_data_type_valid(type)))
3173 return NULL;
3174
Alex Elder81b36be2013-05-01 12:43:04 -05003175 data = kmem_cache_zalloc(ceph_msg_data_cache, GFP_NOFS);
Alex Elder6644ed72013-03-11 23:34:24 -05003176 if (data)
3177 data->type = type;
Alex Elder5240d9f2013-03-14 14:09:06 -05003178 INIT_LIST_HEAD(&data->links);
Alex Elder6644ed72013-03-11 23:34:24 -05003179
3180 return data;
3181}
3182
3183static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3184{
3185 if (!data)
3186 return;
3187
Alex Elder5240d9f2013-03-14 14:09:06 -05003188 WARN_ON(!list_empty(&data->links));
Yan, Zhenge4339d282014-09-16 17:50:45 +08003189 if (data->type == CEPH_MSG_DATA_PAGELIST)
Alex Elder6644ed72013-03-11 23:34:24 -05003190 ceph_pagelist_release(data->pagelist);
Alex Elder81b36be2013-05-01 12:43:04 -05003191 kmem_cache_free(ceph_msg_data_cache, data);
Alex Elder43794502013-03-01 18:00:16 -06003192}
3193
Alex Elder90af3602013-04-05 14:46:01 -05003194void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Alex Elderf1baeb22013-03-07 15:38:26 -06003195 size_t length, size_t alignment)
Alex Elder02afca62013-02-14 12:16:43 -06003196{
Alex Elder6644ed72013-03-11 23:34:24 -05003197 struct ceph_msg_data *data;
3198
Alex Elder07aa1552013-03-04 18:29:06 -06003199 BUG_ON(!pages);
3200 BUG_ON(!length);
Alex Elder02afca62013-02-14 12:16:43 -06003201
Alex Elder6644ed72013-03-11 23:34:24 -05003202 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
3203 BUG_ON(!data);
3204 data->pages = pages;
3205 data->length = length;
3206 data->alignment = alignment & ~PAGE_MASK;
3207
Alex Elder5240d9f2013-03-14 14:09:06 -05003208 list_add_tail(&data->links, &msg->data);
3209 msg->data_length += length;
Alex Elder02afca62013-02-14 12:16:43 -06003210}
Alex Elder90af3602013-04-05 14:46:01 -05003211EXPORT_SYMBOL(ceph_msg_data_add_pages);
Sage Weil31b80062009-10-06 11:31:13 -07003212
Alex Elder90af3602013-04-05 14:46:01 -05003213void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
Alex Elder27fa8382013-02-14 12:16:43 -06003214 struct ceph_pagelist *pagelist)
3215{
Alex Elder6644ed72013-03-11 23:34:24 -05003216 struct ceph_msg_data *data;
3217
Alex Elder07aa1552013-03-04 18:29:06 -06003218 BUG_ON(!pagelist);
3219 BUG_ON(!pagelist->length);
Alex Elder27fa8382013-02-14 12:16:43 -06003220
Alex Elder6644ed72013-03-11 23:34:24 -05003221 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
3222 BUG_ON(!data);
3223 data->pagelist = pagelist;
3224
Alex Elder5240d9f2013-03-14 14:09:06 -05003225 list_add_tail(&data->links, &msg->data);
3226 msg->data_length += pagelist->length;
Alex Elder27fa8382013-02-14 12:16:43 -06003227}
Alex Elder90af3602013-04-05 14:46:01 -05003228EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
Alex Elder27fa8382013-02-14 12:16:43 -06003229
Alex Elderea965712013-04-05 14:46:01 -05003230#ifdef CONFIG_BLOCK
Alex Elder90af3602013-04-05 14:46:01 -05003231void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
Alex Eldera1930802013-03-14 14:09:06 -05003232 size_t length)
Alex Elder27fa8382013-02-14 12:16:43 -06003233{
Alex Elder6644ed72013-03-11 23:34:24 -05003234 struct ceph_msg_data *data;
Alex Elder27fa8382013-02-14 12:16:43 -06003235
Alex Elder6644ed72013-03-11 23:34:24 -05003236 BUG_ON(!bio);
Alex Elder6644ed72013-03-11 23:34:24 -05003237
3238 data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
3239 BUG_ON(!data);
3240 data->bio = bio;
Alex Elderc851c492013-04-05 14:46:01 -05003241 data->bio_length = length;
Alex Elder6644ed72013-03-11 23:34:24 -05003242
Alex Elder5240d9f2013-03-14 14:09:06 -05003243 list_add_tail(&data->links, &msg->data);
3244 msg->data_length += length;
Alex Elder27fa8382013-02-14 12:16:43 -06003245}
Alex Elder90af3602013-04-05 14:46:01 -05003246EXPORT_SYMBOL(ceph_msg_data_add_bio);
Alex Elderea965712013-04-05 14:46:01 -05003247#endif /* CONFIG_BLOCK */
Alex Elder27fa8382013-02-14 12:16:43 -06003248
Sage Weil31b80062009-10-06 11:31:13 -07003249/*
3250 * construct a new message with given type, size
3251 * the new msg has a ref count of 1.
3252 */
Sage Weilb61c2762011-08-09 15:03:46 -07003253struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3254 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07003255{
3256 struct ceph_msg *m;
3257
Alex Eldere3d5d632013-05-01 12:43:04 -05003258 m = kmem_cache_zalloc(ceph_msg_cache, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003259 if (m == NULL)
3260 goto out;
Alex Elder38941f82012-06-01 14:56:43 -05003261
Sage Weil31b80062009-10-06 11:31:13 -07003262 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07003263 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
Sage Weil31b80062009-10-06 11:31:13 -07003264 m->hdr.front_len = cpu_to_le32(front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003265
Alex Elder9516e452013-03-01 18:00:16 -06003266 INIT_LIST_HEAD(&m->list_head);
3267 kref_init(&m->kref);
Alex Elder5240d9f2013-03-14 14:09:06 -05003268 INIT_LIST_HEAD(&m->data);
Henry C Changca208922011-05-03 02:29:56 +00003269
Sage Weil31b80062009-10-06 11:31:13 -07003270 /* front */
3271 if (front_len) {
Ilya Dryomoveeb0bed2014-01-09 20:08:21 +02003272 m->front.iov_base = ceph_kvmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003273 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07003274 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07003275 front_len);
3276 goto out2;
3277 }
3278 } else {
3279 m->front.iov_base = NULL;
3280 }
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02003281 m->front_alloc_len = m->front.iov_len = front_len;
Sage Weil31b80062009-10-06 11:31:13 -07003282
Sage Weilbb257662010-04-01 16:07:23 -07003283 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003284 return m;
3285
3286out2:
3287 ceph_msg_put(m);
3288out:
Sage Weilb61c2762011-08-09 15:03:46 -07003289 if (!can_fail) {
3290 pr_err("msg_new can't create type %d front %d\n", type,
3291 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07003292 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07003293 } else {
3294 dout("msg_new can't create type %d front %d\n", type,
3295 front_len);
3296 }
Sage Weila79832f2010-04-01 16:06:19 -07003297 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003298}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003299EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07003300
3301/*
Sage Weil31b80062009-10-06 11:31:13 -07003302 * Allocate "middle" portion of a message, if it is needed and wasn't
3303 * allocated by alloc_msg. This allows us to read a small fixed-size
3304 * per-type header in the front and then gracefully fail (i.e.,
3305 * propagate the error to the caller based on info in the front) when
3306 * the middle is too large.
3307 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08003308static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003309{
3310 int type = le16_to_cpu(msg->hdr.type);
3311 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3312
3313 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3314 ceph_msg_type_name(type), middle_len);
3315 BUG_ON(!middle_len);
3316 BUG_ON(msg->middle);
3317
Sage Weilb6c1d5b2009-12-07 12:17:17 -08003318 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07003319 if (!msg->middle)
3320 return -ENOMEM;
3321 return 0;
3322}
3323
Yehuda Sadeh24504182010-01-08 13:58:34 -08003324/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05003325 * Allocate a message for receiving an incoming message on a
3326 * connection, and save the result in con->in_msg. Uses the
3327 * connection's private alloc_msg op if available.
3328 *
Sage Weil4740a622012-07-30 18:19:30 -07003329 * Returns 0 on success, or a negative error code.
3330 *
3331 * On success, if we set *skip = 1:
3332 * - the next message should be skipped and ignored.
3333 * - con->in_msg == NULL
3334 * or if we set *skip = 0:
3335 * - con->in_msg is non-null.
3336 * On error (ENOMEM, EAGAIN, ...),
3337 * - con->in_msg == NULL
Yehuda Sadeh24504182010-01-08 13:58:34 -08003338 */
Sage Weil4740a622012-07-30 18:19:30 -07003339static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08003340{
Sage Weil4740a622012-07-30 18:19:30 -07003341 struct ceph_msg_header *hdr = &con->in_hdr;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003342 int middle_len = le32_to_cpu(hdr->middle_len);
Alex Elder1d866d12013-03-01 18:00:14 -06003343 struct ceph_msg *msg;
Sage Weil4740a622012-07-30 18:19:30 -07003344 int ret = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003345
Alex Elder1c20f2d2012-06-04 14:43:32 -05003346 BUG_ON(con->in_msg != NULL);
Alex Elder53ded492013-03-01 18:00:14 -06003347 BUG_ON(!con->ops->alloc_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003348
Alex Elder53ded492013-03-01 18:00:14 -06003349 mutex_unlock(&con->mutex);
3350 msg = con->ops->alloc_msg(con, hdr, skip);
3351 mutex_lock(&con->mutex);
3352 if (con->state != CON_STATE_OPEN) {
3353 if (msg)
Alex Elder1d866d12013-03-01 18:00:14 -06003354 ceph_msg_put(msg);
Alex Elder53ded492013-03-01 18:00:14 -06003355 return -EAGAIN;
3356 }
Alex Elder41375772013-03-05 09:25:10 -06003357 if (msg) {
3358 BUG_ON(*skip);
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003359 msg_con_set(msg, con);
Alex Elder41375772013-03-05 09:25:10 -06003360 con->in_msg = msg;
Alex Elder41375772013-03-05 09:25:10 -06003361 } else {
3362 /*
3363 * Null message pointer means either we should skip
3364 * this message or we couldn't allocate memory. The
3365 * former is not an error.
3366 */
3367 if (*skip)
3368 return 0;
Alex Elder41375772013-03-05 09:25:10 -06003369
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03003370 con->error_msg = "error allocating memory for incoming message";
Alex Elder53ded492013-03-01 18:00:14 -06003371 return -ENOMEM;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003372 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05003373 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08003374
Alex Elder1c20f2d2012-06-04 14:43:32 -05003375 if (middle_len && !con->in_msg->middle) {
3376 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003377 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05003378 ceph_msg_put(con->in_msg);
3379 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003380 }
3381 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08003382
Sage Weil4740a622012-07-30 18:19:30 -07003383 return ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003384}
3385
Sage Weil31b80062009-10-06 11:31:13 -07003386
3387/*
3388 * Free a generically kmalloc'd message.
3389 */
Ilya Dryomov0215e442014-06-20 14:14:41 +04003390static void ceph_msg_free(struct ceph_msg *m)
Sage Weil31b80062009-10-06 11:31:13 -07003391{
Ilya Dryomov0215e442014-06-20 14:14:41 +04003392 dout("%s %p\n", __func__, m);
Ilya Dryomov4965fc32014-10-23 16:32:57 +04003393 kvfree(m->front.iov_base);
Alex Eldere3d5d632013-05-01 12:43:04 -05003394 kmem_cache_free(ceph_msg_cache, m);
Sage Weil31b80062009-10-06 11:31:13 -07003395}
3396
Ilya Dryomov0215e442014-06-20 14:14:41 +04003397static void ceph_msg_release(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07003398{
Sage Weilc2e552e2009-12-07 15:55:05 -08003399 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Geliang Tang10bcee12015-12-18 23:33:30 +08003400 struct ceph_msg_data *data, *next;
Sage Weil31b80062009-10-06 11:31:13 -07003401
Ilya Dryomov0215e442014-06-20 14:14:41 +04003402 dout("%s %p\n", __func__, m);
Sage Weilc2e552e2009-12-07 15:55:05 -08003403 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07003404
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003405 msg_con_set(m, NULL);
3406
Sage Weilc2e552e2009-12-07 15:55:05 -08003407 /* drop middle, data, if any */
3408 if (m->middle) {
3409 ceph_buffer_put(m->middle);
3410 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003411 }
Alex Elder5240d9f2013-03-14 14:09:06 -05003412
Geliang Tang10bcee12015-12-18 23:33:30 +08003413 list_for_each_entry_safe(data, next, &m->data, links) {
3414 list_del_init(&data->links);
Alex Elder5240d9f2013-03-14 14:09:06 -05003415 ceph_msg_data_destroy(data);
3416 }
Alex Eldera1930802013-03-14 14:09:06 -05003417 m->data_length = 0;
Sage Weil58bb3b32009-12-23 12:12:31 -08003418
Sage Weilc2e552e2009-12-07 15:55:05 -08003419 if (m->pool)
3420 ceph_msgpool_put(m->pool, m);
3421 else
Ilya Dryomov0215e442014-06-20 14:14:41 +04003422 ceph_msg_free(m);
Sage Weil31b80062009-10-06 11:31:13 -07003423}
Ilya Dryomov0215e442014-06-20 14:14:41 +04003424
3425struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3426{
3427 dout("%s %p (was %d)\n", __func__, msg,
3428 atomic_read(&msg->kref.refcount));
3429 kref_get(&msg->kref);
3430 return msg;
3431}
3432EXPORT_SYMBOL(ceph_msg_get);
3433
3434void ceph_msg_put(struct ceph_msg *msg)
3435{
3436 dout("%s %p (was %d)\n", __func__, msg,
3437 atomic_read(&msg->kref.refcount));
3438 kref_put(&msg->kref, ceph_msg_release);
3439}
3440EXPORT_SYMBOL(ceph_msg_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003441
3442void ceph_msg_dump(struct ceph_msg *msg)
3443{
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02003444 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3445 msg->front_alloc_len, msg->data_length);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003446 print_hex_dump(KERN_DEBUG, "header: ",
3447 DUMP_PREFIX_OFFSET, 16, 1,
3448 &msg->hdr, sizeof(msg->hdr), true);
3449 print_hex_dump(KERN_DEBUG, " front: ",
3450 DUMP_PREFIX_OFFSET, 16, 1,
3451 msg->front.iov_base, msg->front.iov_len, true);
3452 if (msg->middle)
3453 print_hex_dump(KERN_DEBUG, "middle: ",
3454 DUMP_PREFIX_OFFSET, 16, 1,
3455 msg->middle->vec.iov_base,
3456 msg->middle->vec.iov_len, true);
3457 print_hex_dump(KERN_DEBUG, "footer: ",
3458 DUMP_PREFIX_OFFSET, 16, 1,
3459 &msg->footer, sizeof(msg->footer), true);
3460}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003461EXPORT_SYMBOL(ceph_msg_dump);