blob: 6f3b5754cc7ec227eecabb0732337dff01fc4b8c [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
Al Viro100803a2015-11-15 00:12:48 -0500523 iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1, len);
524 r = sock_recvmsg(sock, &msg, msg.msg_flags);
Sage Weil98bdb0a2011-01-25 08:17:48 -0800525 if (r == -EAGAIN)
526 r = 0;
527 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700528}
529
Alex Elderafb3d902013-03-08 20:58:59 -0600530static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
531 int page_offset, size_t length)
532{
Al Viro100803a2015-11-15 00:12:48 -0500533 struct bio_vec bvec = {
534 .bv_page = page,
535 .bv_offset = page_offset,
536 .bv_len = length
537 };
538 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
539 int r;
Alex Elderafb3d902013-03-08 20:58:59 -0600540
541 BUG_ON(page_offset + length > PAGE_SIZE);
Al Viro100803a2015-11-15 00:12:48 -0500542 iov_iter_bvec(&msg.msg_iter, READ | ITER_BVEC, &bvec, 1, length);
543 r = sock_recvmsg(sock, &msg, msg.msg_flags);
544 if (r == -EAGAIN)
545 r = 0;
546 return r;
Alex Elderafb3d902013-03-08 20:58:59 -0600547}
548
Sage Weil31b80062009-10-06 11:31:13 -0700549/*
550 * write something. @more is true if caller will be sending more data
551 * shortly.
552 */
553static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
554 size_t kvlen, size_t len, int more)
555{
556 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800557 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700558
559 if (more)
560 msg.msg_flags |= MSG_MORE;
561 else
562 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
563
Sage Weil42961d22011-01-25 08:19:34 -0800564 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
565 if (r == -EAGAIN)
566 r = 0;
567 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700568}
569
Chunwei Chen178eda292014-04-23 12:35:09 +0800570static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
Alex Eldere1dcb122013-03-06 23:39:38 -0600571 int offset, size_t size, bool more)
Alex Elder31739132012-03-07 11:40:08 -0600572{
573 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
574 int ret;
575
576 ret = kernel_sendpage(sock, page, offset, size, flags);
577 if (ret == -EAGAIN)
578 ret = 0;
579
580 return ret;
581}
582
Chunwei Chen178eda292014-04-23 12:35:09 +0800583static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
584 int offset, size_t size, bool more)
585{
Al Viro61ff6e92016-01-09 20:55:45 -0500586 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
587 struct bio_vec bvec;
Chunwei Chen178eda292014-04-23 12:35:09 +0800588 int ret;
Chunwei Chen178eda292014-04-23 12:35:09 +0800589
590 /* sendpage cannot properly handle pages with page_count == 0,
591 * we need to fallback to sendmsg if that's the case */
592 if (page_count(page) >= 1)
593 return __ceph_tcp_sendpage(sock, page, offset, size, more);
594
Al Viro61ff6e92016-01-09 20:55:45 -0500595 bvec.bv_page = page;
596 bvec.bv_offset = offset;
597 bvec.bv_len = size;
598
599 if (more)
600 msg.msg_flags |= MSG_MORE;
601 else
602 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
603
604 iov_iter_bvec(&msg.msg_iter, WRITE | ITER_BVEC, &bvec, 1, size);
605 ret = sock_sendmsg(sock, &msg);
606 if (ret == -EAGAIN)
607 ret = 0;
Chunwei Chen178eda292014-04-23 12:35:09 +0800608
609 return ret;
610}
Sage Weil31b80062009-10-06 11:31:13 -0700611
612/*
613 * Shutdown/close the socket for the given connection.
614 */
615static int con_close_socket(struct ceph_connection *con)
616{
Sage Weil8007b8d2012-07-30 18:16:16 -0700617 int rc = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700618
619 dout("con_close_socket on %p sock %p\n", con, con->sock);
Sage Weil8007b8d2012-07-30 18:16:16 -0700620 if (con->sock) {
621 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
622 sock_release(con->sock);
623 con->sock = NULL;
624 }
Alex Elder456ea462012-06-20 21:53:53 -0500625
626 /*
Sage Weil4a861692012-07-20 17:29:55 -0700627 * Forcibly clear the SOCK_CLOSED flag. It gets set
Alex Elder456ea462012-06-20 21:53:53 -0500628 * independent of the connection mutex, and we could have
629 * received a socket close event before we had the chance to
630 * shut the socket down.
631 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600632 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
Sage Weil8007b8d2012-07-30 18:16:16 -0700633
Alex Elderce2c8902012-05-22 22:15:49 -0500634 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700635 return rc;
636}
637
638/*
639 * Reset a connection. Discard all incoming and outgoing messages
640 * and clear *_seq state.
641 */
642static void ceph_msg_remove(struct ceph_msg *msg)
643{
644 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500645
Sage Weil31b80062009-10-06 11:31:13 -0700646 ceph_msg_put(msg);
647}
648static void ceph_msg_remove_list(struct list_head *head)
649{
650 while (!list_empty(head)) {
651 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
652 list_head);
653 ceph_msg_remove(msg);
654 }
655}
656
657static void reset_connection(struct ceph_connection *con)
658{
659 /* reset connection, out_queue, msg_ and connect_seq */
660 /* discard existing out_queue and msg_seq */
Sage Weil0fa6ebc2012-12-27 20:27:04 -0600661 dout("reset_connection %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -0700662 ceph_msg_remove_list(&con->out_queue);
663 ceph_msg_remove_list(&con->out_sent);
664
Sage Weilcf3e5c42009-12-11 09:48:05 -0800665 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500666 BUG_ON(con->in_msg->con != con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800667 ceph_msg_put(con->in_msg);
668 con->in_msg = NULL;
669 }
670
Sage Weil31b80062009-10-06 11:31:13 -0700671 con->connect_seq = 0;
672 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800673 if (con->out_msg) {
Ilya Dryomov583d0fe2015-11-02 17:13:58 +0100674 BUG_ON(con->out_msg->con != con);
Sage Weilc86a2932009-12-14 14:04:30 -0800675 ceph_msg_put(con->out_msg);
676 con->out_msg = NULL;
677 }
Sage Weil31b80062009-10-06 11:31:13 -0700678 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700679 con->in_seq_acked = 0;
Ilya Dryomov67645d72015-12-28 13:18:34 +0300680
681 con->out_skip = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700682}
683
684/*
685 * mark a peer down. drop any open connections.
686 */
687void ceph_con_close(struct ceph_connection *con)
688{
Sage Weil8c50c812012-07-30 16:24:37 -0700689 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700690 dout("con_close %p peer %s\n", con,
691 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700692 con->state = CON_STATE_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500693
Alex Elderc9ffc772013-02-20 10:25:12 -0600694 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
695 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
696 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
697 con_flag_clear(con, CON_FLAG_BACKOFF);
Alex Eldera5988c42012-05-29 11:04:58 -0500698
Sage Weil31b80062009-10-06 11:31:13 -0700699 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700700 con->peer_global_seq = 0;
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400701 cancel_con(con);
Sage Weilee76e072012-07-20 16:45:49 -0700702 con_close_socket(con);
Sage Weilec302642009-12-22 10:43:42 -0800703 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700704}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700705EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700706
707/*
Sage Weil31b80062009-10-06 11:31:13 -0700708 * Reopen a closed connection, with a new peer address.
709 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700710void ceph_con_open(struct ceph_connection *con,
711 __u8 entity_type, __u64 entity_num,
712 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700713{
Sage Weil54691552012-07-30 16:21:40 -0700714 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700715 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700716
Alex Elder122070a2012-12-26 10:43:57 -0600717 WARN_ON(con->state != CON_STATE_CLOSED);
Sage Weil8dacc7d2012-07-20 17:24:40 -0700718 con->state = CON_STATE_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500719
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700720 con->peer_name.type = (__u8) entity_type;
721 con->peer_name.num = cpu_to_le64(entity_num);
722
Sage Weil31b80062009-10-06 11:31:13 -0700723 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800724 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700725 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700726 queue_con(con);
727}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700728EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700729
730/*
Sage Weil87b315a2010-03-22 14:51:18 -0700731 * return true if this connection ever successfully opened
732 */
733bool ceph_con_opened(struct ceph_connection *con)
734{
735 return con->connect_seq > 0;
736}
737
738/*
Sage Weil31b80062009-10-06 11:31:13 -0700739 * initialize a new connection.
740 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500741void ceph_con_init(struct ceph_connection *con, void *private,
742 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700743 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700744{
745 dout("con_init %p\n", con);
746 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500747 con->private = private;
748 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700749 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500750
751 con_sock_state_init(con);
752
Sage Weilec302642009-12-22 10:43:42 -0800753 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700754 INIT_LIST_HEAD(&con->out_queue);
755 INIT_LIST_HEAD(&con->out_sent);
Ilya Dryomov68931622015-07-03 15:44:41 +0300756 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
Alex Eldera5988c42012-05-29 11:04:58 -0500757
Sage Weil8dacc7d2012-07-20 17:24:40 -0700758 con->state = CON_STATE_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700759}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700760EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700761
762
763/*
764 * We maintain a global counter to order connection attempts. Get
765 * a unique seq greater than @gt.
766 */
767static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
768{
769 u32 ret;
770
771 spin_lock(&msgr->global_seq_lock);
772 if (msgr->global_seq < gt)
773 msgr->global_seq = gt;
774 ret = ++msgr->global_seq;
775 spin_unlock(&msgr->global_seq_lock);
776 return ret;
777}
778
Alex Eldere2200422012-05-23 14:35:23 -0500779static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600780{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300781 BUG_ON(con->out_skip);
782
Alex Elder859eb792012-02-14 14:05:33 -0600783 con->out_kvec_left = 0;
784 con->out_kvec_bytes = 0;
785 con->out_kvec_cur = &con->out_kvec[0];
786}
787
Alex Eldere2200422012-05-23 14:35:23 -0500788static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600789 size_t size, void *data)
790{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300791 int index = con->out_kvec_left;
Alex Elder859eb792012-02-14 14:05:33 -0600792
Ilya Dryomov67645d72015-12-28 13:18:34 +0300793 BUG_ON(con->out_skip);
Alex Elder859eb792012-02-14 14:05:33 -0600794 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
795
796 con->out_kvec[index].iov_len = size;
797 con->out_kvec[index].iov_base = data;
798 con->out_kvec_left++;
799 con->out_kvec_bytes += size;
800}
Sage Weil31b80062009-10-06 11:31:13 -0700801
Ilya Dryomov67645d72015-12-28 13:18:34 +0300802/*
803 * Chop off a kvec from the end. Return residual number of bytes for
804 * that kvec, i.e. how many bytes would have been written if the kvec
805 * hadn't been nuked.
806 */
807static int con_out_kvec_skip(struct ceph_connection *con)
808{
809 int off = con->out_kvec_cur - con->out_kvec;
810 int skip = 0;
811
812 if (con->out_kvec_bytes > 0) {
813 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
814 BUG_ON(con->out_kvec_bytes < skip);
815 BUG_ON(!con->out_kvec_left);
816 con->out_kvec_bytes -= skip;
817 con->out_kvec_left--;
818 }
819
820 return skip;
821}
822
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500823#ifdef CONFIG_BLOCK
Alex Elder6aaa4512013-03-06 23:39:39 -0600824
825/*
826 * For a bio data item, a piece is whatever remains of the next
827 * entry in the current bio iovec, or the first entry in the next
828 * bio in the list.
829 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500830static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500831 size_t length)
Alex Elder6aaa4512013-03-06 23:39:39 -0600832{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500833 struct ceph_msg_data *data = cursor->data;
Alex Elder6aaa4512013-03-06 23:39:39 -0600834 struct bio *bio;
835
836 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
837
838 bio = data->bio;
839 BUG_ON(!bio);
Alex Elder6aaa4512013-03-06 23:39:39 -0600840
Alex Elderca8b3a62013-04-05 14:46:01 -0500841 cursor->resid = min(length, data->bio_length);
Alex Elder6aaa4512013-03-06 23:39:39 -0600842 cursor->bio = bio;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700843 cursor->bvec_iter = bio->bi_iter;
844 cursor->last_piece =
845 cursor->resid <= bio_iter_len(bio, cursor->bvec_iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600846}
847
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500848static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
Alex Elder6aaa4512013-03-06 23:39:39 -0600849 size_t *page_offset,
850 size_t *length)
851{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500852 struct ceph_msg_data *data = cursor->data;
Alex Elder6aaa4512013-03-06 23:39:39 -0600853 struct bio *bio;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700854 struct bio_vec bio_vec;
Alex Elder6aaa4512013-03-06 23:39:39 -0600855
856 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
857
858 bio = cursor->bio;
859 BUG_ON(!bio);
860
Kent Overstreetf38a5182013-08-07 14:30:24 -0700861 bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600862
Kent Overstreetf38a5182013-08-07 14:30:24 -0700863 *page_offset = (size_t) bio_vec.bv_offset;
Alex Elder6aaa4512013-03-06 23:39:39 -0600864 BUG_ON(*page_offset >= PAGE_SIZE);
Alex Elder25aff7c2013-03-11 23:34:22 -0500865 if (cursor->last_piece) /* pagelist offset is always 0 */
866 *length = cursor->resid;
867 else
Kent Overstreetf38a5182013-08-07 14:30:24 -0700868 *length = (size_t) bio_vec.bv_len;
Alex Elder25aff7c2013-03-11 23:34:22 -0500869 BUG_ON(*length > cursor->resid);
Alex Elder5df521b2013-03-30 15:09:59 -0500870 BUG_ON(*page_offset + *length > PAGE_SIZE);
Alex Elder6aaa4512013-03-06 23:39:39 -0600871
Kent Overstreetf38a5182013-08-07 14:30:24 -0700872 return bio_vec.bv_page;
Alex Elder6aaa4512013-03-06 23:39:39 -0600873}
874
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500875static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
876 size_t bytes)
Alex Elder6aaa4512013-03-06 23:39:39 -0600877{
Alex Elder6aaa4512013-03-06 23:39:39 -0600878 struct bio *bio;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700879 struct bio_vec bio_vec;
Alex Elder6aaa4512013-03-06 23:39:39 -0600880
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500881 BUG_ON(cursor->data->type != CEPH_MSG_DATA_BIO);
Alex Elder6aaa4512013-03-06 23:39:39 -0600882
883 bio = cursor->bio;
884 BUG_ON(!bio);
885
Kent Overstreetf38a5182013-08-07 14:30:24 -0700886 bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600887
888 /* Advance the cursor offset */
889
Alex Elder25aff7c2013-03-11 23:34:22 -0500890 BUG_ON(cursor->resid < bytes);
891 cursor->resid -= bytes;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700892
893 bio_advance_iter(bio, &cursor->bvec_iter, bytes);
894
895 if (bytes < bio_vec.bv_len)
Alex Elder6aaa4512013-03-06 23:39:39 -0600896 return false; /* more bytes to process in this segment */
897
898 /* Move on to the next segment, and possibly the next bio */
899
Kent Overstreetf38a5182013-08-07 14:30:24 -0700900 if (!cursor->bvec_iter.bi_size) {
Alex Elder6aaa4512013-03-06 23:39:39 -0600901 bio = bio->bi_next;
Ilya Dryomov0ec1d152014-02-05 15:19:55 +0200902 cursor->bio = bio;
903 if (bio)
904 cursor->bvec_iter = bio->bi_iter;
905 else
906 memset(&cursor->bvec_iter, 0,
907 sizeof(cursor->bvec_iter));
Alex Elder6aaa4512013-03-06 23:39:39 -0600908 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600909
Alex Elder25aff7c2013-03-11 23:34:22 -0500910 if (!cursor->last_piece) {
911 BUG_ON(!cursor->resid);
912 BUG_ON(!bio);
913 /* A short read is OK, so use <= rather than == */
Kent Overstreetf38a5182013-08-07 14:30:24 -0700914 if (cursor->resid <= bio_iter_len(bio, cursor->bvec_iter))
Alex Elder6aaa4512013-03-06 23:39:39 -0600915 cursor->last_piece = true;
Alex Elder25aff7c2013-03-11 23:34:22 -0500916 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600917
918 return true;
919}
Alex Elderea965712013-04-05 14:46:01 -0500920#endif /* CONFIG_BLOCK */
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500921
Alex Elderfe38a2b2013-03-06 23:39:39 -0600922/*
Alex Eldere766d7b2013-03-07 15:38:28 -0600923 * For a page array, a piece comes from the first page in the array
924 * that has not already been fully consumed.
925 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500926static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500927 size_t length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600928{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500929 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600930 int page_count;
931
932 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
933
934 BUG_ON(!data->pages);
935 BUG_ON(!data->length);
936
Alex Elderca8b3a62013-04-05 14:46:01 -0500937 cursor->resid = min(length, data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600938 page_count = calc_pages_for(data->alignment, (u64)data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600939 cursor->page_offset = data->alignment & ~PAGE_MASK;
940 cursor->page_index = 0;
Alex Elder56fc5652013-03-30 23:46:55 -0500941 BUG_ON(page_count > (int)USHRT_MAX);
942 cursor->page_count = (unsigned short)page_count;
943 BUG_ON(length > SIZE_MAX - cursor->page_offset);
Ilya Dryomov5f740d72014-08-08 12:43:39 +0400944 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600945}
946
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500947static struct page *
948ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
949 size_t *page_offset, size_t *length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600950{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500951 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600952
953 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
954
955 BUG_ON(cursor->page_index >= cursor->page_count);
956 BUG_ON(cursor->page_offset >= PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600957
958 *page_offset = cursor->page_offset;
Alex Elder25aff7c2013-03-11 23:34:22 -0500959 if (cursor->last_piece)
Alex Eldere766d7b2013-03-07 15:38:28 -0600960 *length = cursor->resid;
Alex Elder25aff7c2013-03-11 23:34:22 -0500961 else
Alex Eldere766d7b2013-03-07 15:38:28 -0600962 *length = PAGE_SIZE - *page_offset;
Alex Eldere766d7b2013-03-07 15:38:28 -0600963
964 return data->pages[cursor->page_index];
965}
966
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500967static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
Alex Eldere766d7b2013-03-07 15:38:28 -0600968 size_t bytes)
969{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500970 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
Alex Eldere766d7b2013-03-07 15:38:28 -0600971
972 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600973
974 /* Advance the cursor page offset */
975
976 cursor->resid -= bytes;
Alex Elder5df521b2013-03-30 15:09:59 -0500977 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
978 if (!bytes || cursor->page_offset)
Alex Eldere766d7b2013-03-07 15:38:28 -0600979 return false; /* more bytes to process in the current page */
980
Yan, Zhengd90deda2014-03-23 06:50:39 +0800981 if (!cursor->resid)
982 return false; /* no more data */
983
Alex Elder5df521b2013-03-30 15:09:59 -0500984 /* Move on to the next page; offset is already at 0 */
Alex Eldere766d7b2013-03-07 15:38:28 -0600985
986 BUG_ON(cursor->page_index >= cursor->page_count);
Alex Eldere766d7b2013-03-07 15:38:28 -0600987 cursor->page_index++;
Alex Elder25aff7c2013-03-11 23:34:22 -0500988 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600989
990 return true;
991}
992
993/*
Alex Elderdd236fc2013-03-06 23:39:39 -0600994 * For a pagelist, a piece is whatever remains to be consumed in the
995 * first page in the list, or the front of the next page.
Alex Elderfe38a2b2013-03-06 23:39:39 -0600996 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500997static void
998ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500999 size_t length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001000{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001001 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001002 struct ceph_pagelist *pagelist;
1003 struct page *page;
1004
Alex Elderdd236fc2013-03-06 23:39:39 -06001005 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001006
1007 pagelist = data->pagelist;
1008 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001009
1010 if (!length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001011 return; /* pagelist can be assigned but empty */
1012
1013 BUG_ON(list_empty(&pagelist->head));
1014 page = list_first_entry(&pagelist->head, struct page, lru);
1015
Alex Elderca8b3a62013-04-05 14:46:01 -05001016 cursor->resid = min(length, pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001017 cursor->page = page;
1018 cursor->offset = 0;
Alex Eldera51b272e2013-04-19 15:34:49 -05001019 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001020}
1021
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001022static struct page *
1023ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1024 size_t *page_offset, size_t *length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001025{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001026 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001027 struct ceph_pagelist *pagelist;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001028
1029 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1030
1031 pagelist = data->pagelist;
1032 BUG_ON(!pagelist);
1033
1034 BUG_ON(!cursor->page);
Alex Elder25aff7c2013-03-11 23:34:22 -05001035 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001036
Alex Elder5df521b2013-03-30 15:09:59 -05001037 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001038 *page_offset = cursor->offset & ~PAGE_MASK;
Alex Elder5df521b2013-03-30 15:09:59 -05001039 if (cursor->last_piece)
Alex Elder25aff7c2013-03-11 23:34:22 -05001040 *length = cursor->resid;
1041 else
1042 *length = PAGE_SIZE - *page_offset;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001043
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001044 return cursor->page;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001045}
1046
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001047static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
Alex Elderdd236fc2013-03-06 23:39:39 -06001048 size_t bytes)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001049{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001050 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001051 struct ceph_pagelist *pagelist;
1052
1053 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1054
1055 pagelist = data->pagelist;
1056 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001057
1058 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001059 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1060
1061 /* Advance the cursor offset */
1062
Alex Elder25aff7c2013-03-11 23:34:22 -05001063 cursor->resid -= bytes;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001064 cursor->offset += bytes;
Alex Elder5df521b2013-03-30 15:09:59 -05001065 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001066 if (!bytes || cursor->offset & ~PAGE_MASK)
1067 return false; /* more bytes to process in the current page */
1068
Yan, Zhengd90deda2014-03-23 06:50:39 +08001069 if (!cursor->resid)
1070 return false; /* no more data */
1071
Alex Elderfe38a2b2013-03-06 23:39:39 -06001072 /* Move on to the next page */
1073
1074 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001075 cursor->page = list_next_entry(cursor->page, lru);
Alex Elder25aff7c2013-03-11 23:34:22 -05001076 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001077
1078 return true;
1079}
1080
Alex Elderdd236fc2013-03-06 23:39:39 -06001081/*
1082 * Message data is handled (sent or received) in pieces, where each
1083 * piece resides on a single page. The network layer might not
1084 * consume an entire piece at once. A data item's cursor keeps
1085 * track of which piece is next to process and how much remains to
1086 * be processed in that piece. It also tracks whether the current
1087 * piece is the last one in the data item.
1088 */
Alex Elderca8b3a62013-04-05 14:46:01 -05001089static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
Alex Elderdd236fc2013-03-06 23:39:39 -06001090{
Alex Elderca8b3a62013-04-05 14:46:01 -05001091 size_t length = cursor->total_resid;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001092
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001093 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001094 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001095 ceph_msg_data_pagelist_cursor_init(cursor, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001096 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001097 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001098 ceph_msg_data_pages_cursor_init(cursor, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001099 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001100#ifdef CONFIG_BLOCK
1101 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001102 ceph_msg_data_bio_cursor_init(cursor, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001103 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001104#endif /* CONFIG_BLOCK */
Alex Elder6aaa4512013-03-06 23:39:39 -06001105 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001106 default:
1107 /* BUG(); */
1108 break;
1109 }
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001110 cursor->need_crc = true;
Alex Elderdd236fc2013-03-06 23:39:39 -06001111}
1112
Alex Elderca8b3a62013-04-05 14:46:01 -05001113static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1114{
1115 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1116 struct ceph_msg_data *data;
1117
1118 BUG_ON(!length);
1119 BUG_ON(length > msg->data_length);
1120 BUG_ON(list_empty(&msg->data));
1121
Alex Elderca8b3a62013-04-05 14:46:01 -05001122 cursor->data_head = &msg->data;
1123 cursor->total_resid = length;
1124 data = list_first_entry(&msg->data, struct ceph_msg_data, links);
1125 cursor->data = data;
1126
1127 __ceph_msg_data_cursor_init(cursor);
1128}
1129
Alex Elderdd236fc2013-03-06 23:39:39 -06001130/*
1131 * Return the page containing the next piece to process for a given
1132 * data item, and supply the page offset and length of that piece.
1133 * Indicate whether this is the last piece in this data item.
1134 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001135static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1136 size_t *page_offset, size_t *length,
Alex Elderdd236fc2013-03-06 23:39:39 -06001137 bool *last_piece)
1138{
1139 struct page *page;
1140
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001141 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001142 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001143 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001144 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001145 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001146 page = ceph_msg_data_pages_next(cursor, page_offset, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001147 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001148#ifdef CONFIG_BLOCK
1149 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001150 page = ceph_msg_data_bio_next(cursor, page_offset, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001151 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001152#endif /* CONFIG_BLOCK */
Alex Elder6aaa4512013-03-06 23:39:39 -06001153 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001154 default:
1155 page = NULL;
1156 break;
1157 }
1158 BUG_ON(!page);
1159 BUG_ON(*page_offset + *length > PAGE_SIZE);
1160 BUG_ON(!*length);
1161 if (last_piece)
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001162 *last_piece = cursor->last_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001163
1164 return page;
1165}
1166
1167/*
1168 * Returns true if the result moves the cursor on to the next piece
1169 * of the data item.
1170 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001171static bool ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1172 size_t bytes)
Alex Elderdd236fc2013-03-06 23:39:39 -06001173{
1174 bool new_piece;
1175
Alex Elder25aff7c2013-03-11 23:34:22 -05001176 BUG_ON(bytes > cursor->resid);
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001177 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001178 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001179 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
Alex Elderdd236fc2013-03-06 23:39:39 -06001180 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001181 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001182 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
Alex Eldere766d7b2013-03-07 15:38:28 -06001183 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001184#ifdef CONFIG_BLOCK
1185 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001186 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
Alex Elder6aaa4512013-03-06 23:39:39 -06001187 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001188#endif /* CONFIG_BLOCK */
Alex Elder6aaa4512013-03-06 23:39:39 -06001189 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001190 default:
1191 BUG();
1192 break;
1193 }
Alex Elderca8b3a62013-04-05 14:46:01 -05001194 cursor->total_resid -= bytes;
Alex Elderdd236fc2013-03-06 23:39:39 -06001195
Alex Elderca8b3a62013-04-05 14:46:01 -05001196 if (!cursor->resid && cursor->total_resid) {
1197 WARN_ON(!cursor->last_piece);
1198 BUG_ON(list_is_last(&cursor->data->links, cursor->data_head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001199 cursor->data = list_next_entry(cursor->data, links);
Alex Elderca8b3a62013-04-05 14:46:01 -05001200 __ceph_msg_data_cursor_init(cursor);
Alex Eldera51b272e2013-04-19 15:34:49 -05001201 new_piece = true;
Alex Elderca8b3a62013-04-05 14:46:01 -05001202 }
Alex Eldera51b272e2013-04-19 15:34:49 -05001203 cursor->need_crc = new_piece;
Alex Elderca8b3a62013-04-05 14:46:01 -05001204
Alex Elderdd236fc2013-03-06 23:39:39 -06001205 return new_piece;
1206}
1207
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01001208static size_t sizeof_footer(struct ceph_connection *con)
1209{
1210 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1211 sizeof(struct ceph_msg_footer) :
1212 sizeof(struct ceph_msg_footer_old);
1213}
1214
Alex Elder98fa5dd2013-04-02 12:09:50 -05001215static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
Alex Elder739c9052012-06-11 14:57:13 -05001216{
Alex Elder739c9052012-06-11 14:57:13 -05001217 BUG_ON(!msg);
Alex Elder25aff7c2013-03-11 23:34:22 -05001218 BUG_ON(!data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001219
Alex Elder4c59b4a2013-03-11 23:34:23 -05001220 /* Initialize data cursor */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001221
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001222 ceph_msg_data_cursor_init(msg, (size_t)data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001223}
1224
Sage Weil31b80062009-10-06 11:31:13 -07001225/*
1226 * Prepare footer for currently outgoing message, and finish things
1227 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1228 */
Alex Elder859eb792012-02-14 14:05:33 -06001229static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001230{
1231 struct ceph_msg *m = con->out_msg;
1232
Alex Elderfd154f32012-06-11 14:57:13 -05001233 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1234
Sage Weil31b80062009-10-06 11:31:13 -07001235 dout("prepare_write_message_footer %p\n", con);
Ilya Dryomov89f08172016-02-20 15:56:07 +01001236 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
Yan, Zheng33d07332014-11-04 16:33:37 +08001237 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1238 if (con->ops->sign_message)
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01001239 con->ops->sign_message(m);
Yan, Zheng33d07332014-11-04 16:33:37 +08001240 else
1241 m->footer.sig = 0;
Yan, Zheng33d07332014-11-04 16:33:37 +08001242 } else {
1243 m->old_footer.flags = m->footer.flags;
Yan, Zheng33d07332014-11-04 16:33:37 +08001244 }
Sage Weil31b80062009-10-06 11:31:13 -07001245 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -08001246 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -07001247}
1248
1249/*
1250 * Prepare headers for the next outgoing message.
1251 */
1252static void prepare_write_message(struct ceph_connection *con)
1253{
1254 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -06001255 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001256
Alex Eldere2200422012-05-23 14:35:23 -05001257 con_out_kvec_reset(con);
Sage Weilc86a2932009-12-14 14:04:30 -08001258 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -07001259
1260 /* Sneak an ack in there first? If we can get it into the same
1261 * TCP packet that's a good thing. */
1262 if (con->in_seq > con->in_seq_acked) {
1263 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -05001264 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001265 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001266 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001267 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001268 }
1269
Alex Elder38941f82012-06-01 14:56:43 -05001270 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -06001271 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -08001272 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -05001273 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -07001274
1275 /* put message on sent list */
1276 ceph_msg_get(m);
1277 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -07001278
Sage Weile84346b2010-05-11 21:20:38 -07001279 /*
1280 * only assign outgoing seq # if we haven't sent this message
1281 * yet. if it is requeued, resend with it's original seq.
1282 */
1283 if (m->needs_out_seq) {
1284 m->hdr.seq = cpu_to_le64(++con->out_seq);
1285 m->needs_out_seq = false;
1286 }
Alex Elder98fa5dd2013-04-02 12:09:50 -05001287 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
Sage Weil31b80062009-10-06 11:31:13 -07001288
Alex Elder98fa5dd2013-04-02 12:09:50 -05001289 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
Sage Weil31b80062009-10-06 11:31:13 -07001290 m, con->out_seq, le16_to_cpu(m->hdr.type),
1291 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
Alex Elder98fa5dd2013-04-02 12:09:50 -05001292 m->data_length);
Sage Weil31b80062009-10-06 11:31:13 -07001293 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
1294
1295 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -05001296 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001297 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
Alex Eldere2200422012-05-23 14:35:23 -05001298 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -06001299
Sage Weil31b80062009-10-06 11:31:13 -07001300 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -05001301 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -06001302 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -07001303
Ilya Dryomov67645d72015-12-28 13:18:34 +03001304 /* fill in hdr crc and finalize hdr */
Alex Eldera9a0c512012-02-15 07:43:54 -06001305 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1306 con->out_msg->hdr.crc = cpu_to_le32(crc);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001307 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
Alex Eldera9a0c512012-02-15 07:43:54 -06001308
Ilya Dryomov67645d72015-12-28 13:18:34 +03001309 /* fill in front and middle crc, footer */
Alex Eldera9a0c512012-02-15 07:43:54 -06001310 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1311 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1312 if (m->middle) {
1313 crc = crc32c(0, m->middle->vec.iov_base,
1314 m->middle->vec.iov_len);
1315 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1316 } else
Sage Weil31b80062009-10-06 11:31:13 -07001317 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -05001318 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -07001319 le32_to_cpu(con->out_msg->footer.front_crc),
1320 le32_to_cpu(con->out_msg->footer.middle_crc));
Ilya Dryomov67645d72015-12-28 13:18:34 +03001321 con->out_msg->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001322
1323 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -05001324 con->out_msg->footer.data_crc = 0;
Alex Elder98fa5dd2013-04-02 12:09:50 -05001325 if (m->data_length) {
1326 prepare_message_data(con->out_msg, m->data_length);
Alex Elder78625052013-03-06 23:39:39 -06001327 con->out_more = 1; /* data + footer will follow */
1328 } else {
Sage Weil31b80062009-10-06 11:31:13 -07001329 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -06001330 prepare_write_message_footer(con);
Alex Elder78625052013-03-06 23:39:39 -06001331 }
Sage Weil31b80062009-10-06 11:31:13 -07001332
Alex Elderc9ffc772013-02-20 10:25:12 -06001333 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001334}
1335
1336/*
1337 * Prepare an ack.
1338 */
1339static void prepare_write_ack(struct ceph_connection *con)
1340{
1341 dout("prepare_write_ack %p %llu -> %llu\n", con,
1342 con->in_seq_acked, con->in_seq);
1343 con->in_seq_acked = con->in_seq;
1344
Alex Eldere2200422012-05-23 14:35:23 -05001345 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001346
Alex Eldere2200422012-05-23 14:35:23 -05001347 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -06001348
Sage Weil31b80062009-10-06 11:31:13 -07001349 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001350 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001351 &con->out_temp_ack);
1352
Sage Weil31b80062009-10-06 11:31:13 -07001353 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elderc9ffc772013-02-20 10:25:12 -06001354 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001355}
1356
1357/*
Sage Weil3a230832013-03-25 08:47:40 -07001358 * Prepare to share the seq during handshake
1359 */
1360static void prepare_write_seq(struct ceph_connection *con)
1361{
1362 dout("prepare_write_seq %p %llu -> %llu\n", con,
1363 con->in_seq_acked, con->in_seq);
1364 con->in_seq_acked = con->in_seq;
1365
1366 con_out_kvec_reset(con);
1367
1368 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1369 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1370 &con->out_temp_ack);
1371
1372 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1373}
1374
1375/*
Sage Weil31b80062009-10-06 11:31:13 -07001376 * Prepare to write keepalive byte.
1377 */
1378static void prepare_write_keepalive(struct ceph_connection *con)
1379{
1380 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -05001381 con_out_kvec_reset(con);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001382 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001383 struct timespec now = CURRENT_TIME;
1384
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001385 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001386 ceph_encode_timespec(&con->out_temp_keepalive2, &now);
1387 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1388 &con->out_temp_keepalive2);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001389 } else {
1390 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1391 }
Alex Elderc9ffc772013-02-20 10:25:12 -06001392 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001393}
1394
1395/*
1396 * Connection negotiation.
1397 */
1398
Alex Elderdac1e712012-05-16 15:16:39 -05001399static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
1400 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001401{
Alex Eldera3530df2012-05-16 15:16:39 -05001402 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -05001403
1404 if (!con->ops->get_authorizer) {
1405 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1406 con->out_connect.authorizer_len = 0;
Alex Elder729796b2012-05-16 15:16:39 -05001407 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -05001408 }
1409
Alex Elderdac1e712012-05-16 15:16:39 -05001410 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Eldera3530df2012-05-16 15:16:39 -05001411 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -05001412 return auth;
Sage Weil0da5d702011-05-19 11:21:05 -07001413
Alex Elder8f43fb52012-05-16 15:16:39 -05001414 con->auth_reply_buf = auth->authorizer_reply_buf;
1415 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
Alex Elder729796b2012-05-16 15:16:39 -05001416 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001417}
1418
Sage Weil31b80062009-10-06 11:31:13 -07001419/*
1420 * We connected to a peer and are saying hello.
1421 */
Alex Eldere825a662012-05-16 15:16:38 -05001422static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001423{
Alex Eldere2200422012-05-23 14:35:23 -05001424 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1425 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -05001426 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -08001427
Sage Weileed0ef22009-11-10 14:34:36 -08001428 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001429 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weileed0ef22009-11-10 14:34:36 -08001430}
1431
Alex Eldere825a662012-05-16 15:16:38 -05001432static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -08001433{
Eric Dumazet95c96172012-04-15 05:58:06 +00001434 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001435 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -05001436 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -05001437 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -07001438
1439 switch (con->peer_name.type) {
1440 case CEPH_ENTITY_TYPE_MON:
1441 proto = CEPH_MONC_PROTOCOL;
1442 break;
1443 case CEPH_ENTITY_TYPE_OSD:
1444 proto = CEPH_OSDC_PROTOCOL;
1445 break;
1446 case CEPH_ENTITY_TYPE_MDS:
1447 proto = CEPH_MDSC_PROTOCOL;
1448 break;
1449 default:
1450 BUG();
1451 }
1452
1453 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1454 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001455
Ilya Dryomov859bff52015-10-28 23:50:58 +01001456 con->out_connect.features =
1457 cpu_to_le64(from_msgr(con->msgr)->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -07001458 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1459 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1460 con->out_connect.global_seq = cpu_to_le32(global_seq);
1461 con->out_connect.protocol_version = cpu_to_le32(proto);
1462 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001463
Alex Elderdac1e712012-05-16 15:16:39 -05001464 auth_proto = CEPH_AUTH_UNKNOWN;
1465 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -05001466 if (IS_ERR(auth))
1467 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -05001468
Alex Elderdac1e712012-05-16 15:16:39 -05001469 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -05001470 con->out_connect.authorizer_len = auth ?
1471 cpu_to_le32(auth->authorizer_buf_len) : 0;
1472
Alex Eldere2200422012-05-23 14:35:23 -05001473 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -05001474 &con->out_connect);
1475 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -05001476 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -05001477 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -06001478
Sage Weil31b80062009-10-06 11:31:13 -07001479 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001480 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001481
Alex Eldere10c7582012-05-16 15:16:38 -05001482 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07001483}
1484
Sage Weil31b80062009-10-06 11:31:13 -07001485/*
1486 * write as much of pending kvecs to the socket as we can.
1487 * 1 -> done
1488 * 0 -> socket full, but more to do
1489 * <0 -> error
1490 */
1491static int write_partial_kvec(struct ceph_connection *con)
1492{
1493 int ret;
1494
1495 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1496 while (con->out_kvec_bytes > 0) {
1497 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1498 con->out_kvec_left, con->out_kvec_bytes,
1499 con->out_more);
1500 if (ret <= 0)
1501 goto out;
1502 con->out_kvec_bytes -= ret;
1503 if (con->out_kvec_bytes == 0)
1504 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -06001505
1506 /* account for full iov entries consumed */
1507 while (ret >= con->out_kvec_cur->iov_len) {
1508 BUG_ON(!con->out_kvec_left);
1509 ret -= con->out_kvec_cur->iov_len;
1510 con->out_kvec_cur++;
1511 con->out_kvec_left--;
1512 }
1513 /* and for a partially-consumed entry */
1514 if (ret) {
1515 con->out_kvec_cur->iov_len -= ret;
1516 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -07001517 }
1518 }
1519 con->out_kvec_left = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001520 ret = 1;
1521out:
1522 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1523 con->out_kvec_bytes, con->out_kvec_left, ret);
1524 return ret; /* done! */
1525}
1526
Alex Elder35b62802013-03-08 20:59:00 -06001527static u32 ceph_crc32c_page(u32 crc, struct page *page,
1528 unsigned int page_offset,
1529 unsigned int length)
1530{
1531 char *kaddr;
1532
1533 kaddr = kmap(page);
1534 BUG_ON(kaddr == NULL);
1535 crc = crc32c(crc, kaddr + page_offset, length);
1536 kunmap(page);
1537
1538 return crc;
1539}
Sage Weil31b80062009-10-06 11:31:13 -07001540/*
1541 * Write as much message data payload as we can. If we finish, queue
1542 * up the footer.
1543 * 1 -> done, footer is now queued in out_kvec[].
1544 * 0 -> socket full, but more to do
1545 * <0 -> error
1546 */
Alex Elder34d2d202013-03-08 20:58:59 -06001547static int write_partial_message_data(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001548{
1549 struct ceph_msg *msg = con->out_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001550 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01001551 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elderf5db90b2013-03-11 23:34:23 -05001552 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001553
Alex Elder859a35d2013-03-11 23:34:23 -05001554 dout("%s %p msg %p\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001555
Alex Elder5240d9f2013-03-14 14:09:06 -05001556 if (list_empty(&msg->data))
Alex Elder4c59b4a2013-03-11 23:34:23 -05001557 return -EINVAL;
1558
Alex Elder5821bd82012-06-11 14:57:13 -05001559 /*
1560 * Iterate through each page that contains data to be
1561 * written, and send as much as possible for each.
1562 *
1563 * If we are calculating the data crc (the default), we will
1564 * need to map the page. If we have no pages, they have
1565 * been revoked, so use the zero page.
1566 */
Alex Elderf5db90b2013-03-11 23:34:23 -05001567 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
Alex Elder643c68a2013-03-11 23:34:23 -05001568 while (cursor->resid) {
Alex Elder8a166d02013-03-08 13:35:36 -06001569 struct page *page;
Alex Eldere387d522013-03-06 23:39:38 -06001570 size_t page_offset;
1571 size_t length;
Alex Elder8a166d02013-03-08 13:35:36 -06001572 bool last_piece;
Alex Elder8ea299b2013-03-11 23:34:23 -05001573 bool need_crc;
Alex Elderf5db90b2013-03-11 23:34:23 -05001574 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001575
Shraddha Barke343128c2015-10-19 21:59:00 +05301576 page = ceph_msg_data_next(cursor, &page_offset, &length,
1577 &last_piece);
Alex Eldere387d522013-03-06 23:39:38 -06001578 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
Benoît Canetc2cfa192015-06-25 20:32:34 +03001579 length, !last_piece);
Alex Elderf5db90b2013-03-11 23:34:23 -05001580 if (ret <= 0) {
1581 if (do_datacrc)
1582 msg->footer.data_crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -07001583
Alex Elderf5db90b2013-03-11 23:34:23 -05001584 return ret;
1585 }
Alex Elder143334f2013-03-29 11:44:10 -05001586 if (do_datacrc && cursor->need_crc)
1587 crc = ceph_crc32c_page(crc, page, page_offset, length);
Shraddha Barke343128c2015-10-19 21:59:00 +05301588 need_crc = ceph_msg_data_advance(cursor, (size_t)ret);
Sage Weil31b80062009-10-06 11:31:13 -07001589 }
1590
Alex Elder34d2d202013-03-08 20:58:59 -06001591 dout("%s %p msg %p done\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001592
1593 /* prepare and queue up footer, too */
Alex Elderf5db90b2013-03-11 23:34:23 -05001594 if (do_datacrc)
1595 msg->footer.data_crc = cpu_to_le32(crc);
1596 else
Alex Elder84ca8fc2012-06-11 14:57:13 -05001597 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001598 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001599 prepare_write_message_footer(con);
Alex Elderf5db90b2013-03-11 23:34:23 -05001600
1601 return 1; /* must return > 0 to indicate success */
Sage Weil31b80062009-10-06 11:31:13 -07001602}
1603
1604/*
1605 * write some zeros
1606 */
1607static int write_partial_skip(struct ceph_connection *con)
1608{
1609 int ret;
1610
Ilya Dryomov67645d72015-12-28 13:18:34 +03001611 dout("%s %p %d left\n", __func__, con, con->out_skip);
Sage Weil31b80062009-10-06 11:31:13 -07001612 while (con->out_skip > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001613 size_t size = min(con->out_skip, (int) PAGE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001614
Alex Eldere1dcb122013-03-06 23:39:38 -06001615 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
Sage Weil31b80062009-10-06 11:31:13 -07001616 if (ret <= 0)
1617 goto out;
1618 con->out_skip -= ret;
1619 }
1620 ret = 1;
1621out:
1622 return ret;
1623}
1624
1625/*
1626 * Prepare to read connection handshake, or an ack.
1627 */
Sage Weileed0ef22009-11-10 14:34:36 -08001628static void prepare_read_banner(struct ceph_connection *con)
1629{
1630 dout("prepare_read_banner %p\n", con);
1631 con->in_base_pos = 0;
1632}
1633
Sage Weil31b80062009-10-06 11:31:13 -07001634static void prepare_read_connect(struct ceph_connection *con)
1635{
1636 dout("prepare_read_connect %p\n", con);
1637 con->in_base_pos = 0;
1638}
1639
1640static void prepare_read_ack(struct ceph_connection *con)
1641{
1642 dout("prepare_read_ack %p\n", con);
1643 con->in_base_pos = 0;
1644}
1645
Sage Weil3a230832013-03-25 08:47:40 -07001646static void prepare_read_seq(struct ceph_connection *con)
1647{
1648 dout("prepare_read_seq %p\n", con);
1649 con->in_base_pos = 0;
1650 con->in_tag = CEPH_MSGR_TAG_SEQ;
1651}
1652
Sage Weil31b80062009-10-06 11:31:13 -07001653static void prepare_read_tag(struct ceph_connection *con)
1654{
1655 dout("prepare_read_tag %p\n", con);
1656 con->in_base_pos = 0;
1657 con->in_tag = CEPH_MSGR_TAG_READY;
1658}
1659
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001660static void prepare_read_keepalive_ack(struct ceph_connection *con)
1661{
1662 dout("prepare_read_keepalive_ack %p\n", con);
1663 con->in_base_pos = 0;
1664}
1665
Sage Weil31b80062009-10-06 11:31:13 -07001666/*
1667 * Prepare to read a message.
1668 */
1669static int prepare_read_message(struct ceph_connection *con)
1670{
1671 dout("prepare_read_message %p\n", con);
1672 BUG_ON(con->in_msg != NULL);
1673 con->in_base_pos = 0;
1674 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1675 return 0;
1676}
1677
1678
1679static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001680 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001681{
Alex Eldere6cee712012-05-10 10:29:50 -05001682 while (con->in_base_pos < end) {
1683 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001684 int have = size - left;
1685 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1686 if (ret <= 0)
1687 return ret;
1688 con->in_base_pos += ret;
1689 }
1690 return 1;
1691}
1692
1693
1694/*
1695 * Read all or part of the connect-side handshake on a new connection
1696 */
Sage Weileed0ef22009-11-10 14:34:36 -08001697static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001698{
Alex Elderfd516532012-05-10 10:29:50 -05001699 int size;
1700 int end;
1701 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001702
Sage Weileed0ef22009-11-10 14:34:36 -08001703 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001704
1705 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001706 size = strlen(CEPH_BANNER);
1707 end = size;
1708 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001709 if (ret <= 0)
1710 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001711
1712 size = sizeof (con->actual_peer_addr);
1713 end += size;
1714 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001715 if (ret <= 0)
1716 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001717
1718 size = sizeof (con->peer_addr_for_me);
1719 end += size;
1720 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001721 if (ret <= 0)
1722 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001723
Sage Weileed0ef22009-11-10 14:34:36 -08001724out:
1725 return ret;
1726}
1727
1728static int read_partial_connect(struct ceph_connection *con)
1729{
Alex Elderfd516532012-05-10 10:29:50 -05001730 int size;
1731 int end;
1732 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001733
1734 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1735
Alex Elderfd516532012-05-10 10:29:50 -05001736 size = sizeof (con->in_reply);
1737 end = size;
1738 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001739 if (ret <= 0)
1740 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001741
1742 size = le32_to_cpu(con->in_reply.authorizer_len);
1743 end += size;
1744 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001745 if (ret <= 0)
1746 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001747
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001748 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1749 con, (int)con->in_reply.tag,
1750 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001751 le32_to_cpu(con->in_reply.global_seq));
1752out:
1753 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001754
Sage Weil31b80062009-10-06 11:31:13 -07001755}
1756
1757/*
1758 * Verify the hello banner looks okay.
1759 */
1760static int verify_hello(struct ceph_connection *con)
1761{
1762 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001763 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001764 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001765 con->error_msg = "protocol error, bad banner";
1766 return -1;
1767 }
1768 return 0;
1769}
1770
1771static bool addr_is_blank(struct sockaddr_storage *ss)
1772{
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001773 struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1774 struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1775
Sage Weil31b80062009-10-06 11:31:13 -07001776 switch (ss->ss_family) {
1777 case AF_INET:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001778 return addr->s_addr == htonl(INADDR_ANY);
Sage Weil31b80062009-10-06 11:31:13 -07001779 case AF_INET6:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001780 return ipv6_addr_any(addr6);
1781 default:
1782 return true;
Sage Weil31b80062009-10-06 11:31:13 -07001783 }
Sage Weil31b80062009-10-06 11:31:13 -07001784}
1785
1786static int addr_port(struct sockaddr_storage *ss)
1787{
1788 switch (ss->ss_family) {
1789 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001790 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001791 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001792 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001793 }
1794 return 0;
1795}
1796
1797static void addr_set_port(struct sockaddr_storage *ss, int p)
1798{
1799 switch (ss->ss_family) {
1800 case AF_INET:
1801 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001802 break;
Sage Weil31b80062009-10-06 11:31:13 -07001803 case AF_INET6:
1804 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001805 break;
Sage Weil31b80062009-10-06 11:31:13 -07001806 }
1807}
1808
1809/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001810 * Unlike other *_pton function semantics, zero indicates success.
1811 */
1812static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1813 char delim, const char **ipend)
1814{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001815 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1816 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001817
1818 memset(ss, 0, sizeof(*ss));
1819
1820 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1821 ss->ss_family = AF_INET;
1822 return 0;
1823 }
1824
1825 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1826 ss->ss_family = AF_INET6;
1827 return 0;
1828 }
1829
1830 return -EINVAL;
1831}
1832
1833/*
1834 * Extract hostname string and resolve using kernel DNS facility.
1835 */
1836#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1837static int ceph_dns_resolve_name(const char *name, size_t namelen,
1838 struct sockaddr_storage *ss, char delim, const char **ipend)
1839{
1840 const char *end, *delim_p;
1841 char *colon_p, *ip_addr = NULL;
1842 int ip_len, ret;
1843
1844 /*
1845 * The end of the hostname occurs immediately preceding the delimiter or
1846 * the port marker (':') where the delimiter takes precedence.
1847 */
1848 delim_p = memchr(name, delim, namelen);
1849 colon_p = memchr(name, ':', namelen);
1850
1851 if (delim_p && colon_p)
1852 end = delim_p < colon_p ? delim_p : colon_p;
1853 else if (!delim_p && colon_p)
1854 end = colon_p;
1855 else {
1856 end = delim_p;
1857 if (!end) /* case: hostname:/ */
1858 end = name + namelen;
1859 }
1860
1861 if (end <= name)
1862 return -EINVAL;
1863
1864 /* do dns_resolve upcall */
1865 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1866 if (ip_len > 0)
1867 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1868 else
1869 ret = -ESRCH;
1870
1871 kfree(ip_addr);
1872
1873 *ipend = end;
1874
1875 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1876 ret, ret ? "failed" : ceph_pr_addr(ss));
1877
1878 return ret;
1879}
1880#else
1881static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1882 struct sockaddr_storage *ss, char delim, const char **ipend)
1883{
1884 return -EINVAL;
1885}
1886#endif
1887
1888/*
1889 * Parse a server name (IP or hostname). If a valid IP address is not found
1890 * then try to extract a hostname to resolve using userspace DNS upcall.
1891 */
1892static int ceph_parse_server_name(const char *name, size_t namelen,
1893 struct sockaddr_storage *ss, char delim, const char **ipend)
1894{
1895 int ret;
1896
1897 ret = ceph_pton(name, namelen, ss, delim, ipend);
1898 if (ret)
1899 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1900
1901 return ret;
1902}
1903
1904/*
Sage Weil31b80062009-10-06 11:31:13 -07001905 * Parse an ip[:port] list into an addr array. Use the default
1906 * monitor port if a port isn't specified.
1907 */
1908int ceph_parse_ips(const char *c, const char *end,
1909 struct ceph_entity_addr *addr,
1910 int max_count, int *count)
1911{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001912 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001913 const char *p = c;
1914
1915 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1916 for (i = 0; i < max_count; i++) {
1917 const char *ipend;
1918 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001919 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001920 char delim = ',';
1921
1922 if (*p == '[') {
1923 delim = ']';
1924 p++;
1925 }
Sage Weil31b80062009-10-06 11:31:13 -07001926
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001927 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1928 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001929 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001930 ret = -EINVAL;
1931
Sage Weil31b80062009-10-06 11:31:13 -07001932 p = ipend;
1933
Sage Weil39139f62010-07-08 09:54:52 -07001934 if (delim == ']') {
1935 if (*p != ']') {
1936 dout("missing matching ']'\n");
1937 goto bad;
1938 }
1939 p++;
1940 }
1941
Sage Weil31b80062009-10-06 11:31:13 -07001942 /* port? */
1943 if (p < end && *p == ':') {
1944 port = 0;
1945 p++;
1946 while (p < end && *p >= '0' && *p <= '9') {
1947 port = (port * 10) + (*p - '0');
1948 p++;
1949 }
Ilya Dryomovf48db1e2013-12-30 19:21:29 +02001950 if (port == 0)
1951 port = CEPH_MON_PORT;
1952 else if (port > 65535)
Sage Weil31b80062009-10-06 11:31:13 -07001953 goto bad;
1954 } else {
1955 port = CEPH_MON_PORT;
1956 }
1957
1958 addr_set_port(ss, port);
1959
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001960 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001961
1962 if (p == end)
1963 break;
1964 if (*p != ',')
1965 goto bad;
1966 p++;
1967 }
1968
1969 if (p != end)
1970 goto bad;
1971
1972 if (count)
1973 *count = i + 1;
1974 return 0;
1975
1976bad:
Sage Weil39139f62010-07-08 09:54:52 -07001977 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001978 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001979}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001980EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001981
Sage Weileed0ef22009-11-10 14:34:36 -08001982static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001983{
Sage Weileed0ef22009-11-10 14:34:36 -08001984 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001985
1986 if (verify_hello(con) < 0)
1987 return -1;
1988
Sage Weil63f2d212009-11-03 15:17:56 -08001989 ceph_decode_addr(&con->actual_peer_addr);
1990 ceph_decode_addr(&con->peer_addr_for_me);
1991
Sage Weil31b80062009-10-06 11:31:13 -07001992 /*
1993 * Make sure the other end is who we wanted. note that the other
1994 * end may not yet know their ip address, so if it's 0.0.0.0, give
1995 * them the benefit of the doubt.
1996 */
Sage Weil103e2d32010-01-07 16:12:36 -08001997 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1998 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001999 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
2000 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Joe Perchesb9a67892014-09-09 21:17:29 -07002001 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
2002 ceph_pr_addr(&con->peer_addr.in_addr),
2003 (int)le32_to_cpu(con->peer_addr.nonce),
2004 ceph_pr_addr(&con->actual_peer_addr.in_addr),
2005 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08002006 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07002007 return -1;
2008 }
2009
2010 /*
2011 * did we learn our address?
2012 */
2013 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2014 int port = addr_port(&con->msgr->inst.addr.in_addr);
2015
2016 memcpy(&con->msgr->inst.addr.in_addr,
2017 &con->peer_addr_for_me.in_addr,
2018 sizeof(con->peer_addr_for_me.in_addr));
2019 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08002020 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08002021 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002022 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002023 }
2024
Sage Weileed0ef22009-11-10 14:34:36 -08002025 return 0;
2026}
2027
2028static int process_connect(struct ceph_connection *con)
2029{
Ilya Dryomov859bff52015-10-28 23:50:58 +01002030 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2031 u64 req_feat = from_msgr(con->msgr)->required_features;
Ilya Dryomov2b3e0c92013-12-24 21:19:24 +02002032 u64 server_feat = ceph_sanitize_features(
2033 le64_to_cpu(con->in_reply.features));
Sage Weil0da5d702011-05-19 11:21:05 -07002034 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08002035
Sage Weileed0ef22009-11-10 14:34:36 -08002036 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2037
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002038 if (con->auth_reply_buf) {
2039 /*
2040 * Any connection that defines ->get_authorizer()
2041 * should also define ->verify_authorizer_reply().
2042 * See get_connect_authorizer().
2043 */
Ilya Dryomov0dde5842016-12-02 16:35:09 +01002044 ret = con->ops->verify_authorizer_reply(con);
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002045 if (ret < 0) {
2046 con->error_msg = "bad authorize reply";
2047 return ret;
2048 }
2049 }
2050
Sage Weil31b80062009-10-06 11:31:13 -07002051 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08002052 case CEPH_MSGR_TAG_FEATURES:
2053 pr_err("%s%lld %s feature set mismatch,"
2054 " my %llx < server's %llx, missing %llx\n",
2055 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002056 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002057 sup_feat, server_feat, server_feat & ~sup_feat);
2058 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002059 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002060 return -1;
2061
Sage Weil31b80062009-10-06 11:31:13 -07002062 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07002063 pr_err("%s%lld %s protocol version mismatch,"
2064 " my %d != server's %d\n",
2065 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002066 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07002067 le32_to_cpu(con->out_connect.protocol_version),
2068 le32_to_cpu(con->in_reply.protocol_version));
2069 con->error_msg = "protocol version mismatch";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002070 reset_connection(con);
Sage Weil31b80062009-10-06 11:31:13 -07002071 return -1;
2072
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002073 case CEPH_MSGR_TAG_BADAUTHORIZER:
2074 con->auth_retry++;
2075 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2076 con->auth_retry);
2077 if (con->auth_retry == 2) {
2078 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002079 return -1;
2080 }
Jim Schutt6d4221b2012-08-10 10:37:38 -07002081 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002082 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002083 if (ret < 0)
2084 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07002085 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002086 break;
Sage Weil31b80062009-10-06 11:31:13 -07002087
2088 case CEPH_MSGR_TAG_RESETSESSION:
2089 /*
2090 * If we connected with a large connect_seq but the peer
2091 * has no record of a session with us (no connection, or
2092 * connect_seq == 0), they will send RESETSESION to indicate
2093 * that they must have reset their session, and may have
2094 * dropped messages.
2095 */
2096 dout("process_connect got RESET peer seq %u\n",
Sage Weil5bdca4e2012-07-10 11:53:34 -07002097 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002098 pr_err("%s%lld %s connection reset\n",
2099 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002100 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002101 reset_connection(con);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002102 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002103 ret = prepare_write_connect(con);
2104 if (ret < 0)
2105 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002106 prepare_read_connect(con);
2107
2108 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08002109 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002110 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2111 if (con->ops->peer_reset)
2112 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08002113 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002114 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07002115 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07002116 break;
2117
2118 case CEPH_MSGR_TAG_RETRY_SESSION:
2119 /*
2120 * If we sent a smaller connect_seq than the peer has, try
2121 * again with a larger value.
2122 */
Sage Weil5bdca4e2012-07-10 11:53:34 -07002123 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002124 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil5bdca4e2012-07-10 11:53:34 -07002125 le32_to_cpu(con->in_reply.connect_seq));
2126 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002127 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002128 ret = prepare_write_connect(con);
2129 if (ret < 0)
2130 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002131 prepare_read_connect(con);
2132 break;
2133
2134 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2135 /*
2136 * If we sent a smaller global_seq than the peer has, try
2137 * again with a larger value.
2138 */
Sage Weileed0ef22009-11-10 14:34:36 -08002139 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002140 con->peer_global_seq,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002141 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002142 get_global_seq(con->msgr,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002143 le32_to_cpu(con->in_reply.global_seq));
Jim Schutt6d4221b2012-08-10 10:37:38 -07002144 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002145 ret = prepare_write_connect(con);
2146 if (ret < 0)
2147 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002148 prepare_read_connect(con);
2149 break;
2150
Sage Weil3a230832013-03-25 08:47:40 -07002151 case CEPH_MSGR_TAG_SEQ:
Sage Weil31b80062009-10-06 11:31:13 -07002152 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08002153 if (req_feat & ~server_feat) {
2154 pr_err("%s%lld %s protocol feature mismatch,"
2155 " my required %llx > server's %llx, need %llx\n",
2156 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002157 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002158 req_feat, server_feat, req_feat & ~server_feat);
2159 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002160 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002161 return -1;
2162 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002163
Alex Elder122070a2012-12-26 10:43:57 -06002164 WARN_ON(con->state != CON_STATE_NEGOTIATING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002165 con->state = CON_STATE_OPEN;
Sage Weil20e55c42013-03-25 09:30:13 -07002166 con->auth_retry = 0; /* we authenticated; clear flag */
Sage Weil31b80062009-10-06 11:31:13 -07002167 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2168 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07002169 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07002170 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2171 con->peer_global_seq,
2172 le32_to_cpu(con->in_reply.connect_seq),
2173 con->connect_seq);
2174 WARN_ON(con->connect_seq !=
2175 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08002176
2177 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elderc9ffc772013-02-20 10:25:12 -06002178 con_flag_set(con, CON_FLAG_LOSSYTX);
Sage Weil92ac41d2009-12-14 14:56:56 -08002179
Sage Weil85effe12012-07-30 16:22:05 -07002180 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -07002181
Sage Weil3a230832013-03-25 08:47:40 -07002182 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2183 prepare_write_seq(con);
2184 prepare_read_seq(con);
2185 } else {
2186 prepare_read_tag(con);
2187 }
Sage Weil31b80062009-10-06 11:31:13 -07002188 break;
2189
2190 case CEPH_MSGR_TAG_WAIT:
2191 /*
2192 * If there is a connection race (we are opening
2193 * connections to each other), one of us may just have
2194 * to WAIT. This shouldn't happen if we are the
2195 * client.
2196 */
Sage Weil04177882011-05-12 15:33:17 -07002197 con->error_msg = "protocol error, got WAIT as client";
2198 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07002199
2200 default:
Sage Weil31b80062009-10-06 11:31:13 -07002201 con->error_msg = "protocol error, garbage tag during connect";
2202 return -1;
2203 }
2204 return 0;
2205}
2206
2207
2208/*
2209 * read (part of) an ack
2210 */
2211static int read_partial_ack(struct ceph_connection *con)
2212{
Alex Elderfd516532012-05-10 10:29:50 -05002213 int size = sizeof (con->in_temp_ack);
2214 int end = size;
Sage Weil31b80062009-10-06 11:31:13 -07002215
Alex Elderfd516532012-05-10 10:29:50 -05002216 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07002217}
2218
Sage Weil31b80062009-10-06 11:31:13 -07002219/*
2220 * We can finally discard anything that's been acked.
2221 */
2222static void process_ack(struct ceph_connection *con)
2223{
2224 struct ceph_msg *m;
2225 u64 ack = le64_to_cpu(con->in_temp_ack);
2226 u64 seq;
2227
Sage Weil31b80062009-10-06 11:31:13 -07002228 while (!list_empty(&con->out_sent)) {
2229 m = list_first_entry(&con->out_sent, struct ceph_msg,
2230 list_head);
2231 seq = le64_to_cpu(m->hdr.seq);
2232 if (seq > ack)
2233 break;
2234 dout("got ack for seq %llu type %d at %p\n", seq,
2235 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07002236 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07002237 ceph_msg_remove(m);
2238 }
Sage Weil31b80062009-10-06 11:31:13 -07002239 prepare_read_tag(con);
2240}
2241
2242
Yehuda Sadeh24504182010-01-08 13:58:34 -08002243static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002244 struct kvec *section,
2245 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002246{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002247 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07002248
Yehuda Sadeh24504182010-01-08 13:58:34 -08002249 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07002250
Yehuda Sadeh24504182010-01-08 13:58:34 -08002251 while (section->iov_len < sec_len) {
2252 BUG_ON(section->iov_base == NULL);
2253 left = sec_len - section->iov_len;
2254 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2255 section->iov_len, left);
2256 if (ret <= 0)
2257 return ret;
2258 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002259 }
Alex Elderfe3ad592012-02-15 07:43:54 -06002260 if (section->iov_len == sec_len)
2261 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002262
2263 return 1;
2264}
2265
Alex Elder34d2d202013-03-08 20:58:59 -06002266static int read_partial_msg_data(struct ceph_connection *con)
2267{
2268 struct ceph_msg *msg = con->in_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05002269 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002270 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elder686be202013-03-11 23:34:23 -05002271 struct page *page;
2272 size_t page_offset;
2273 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05002274 u32 crc = 0;
Alex Elder34d2d202013-03-08 20:58:59 -06002275 int ret;
2276
2277 BUG_ON(!msg);
Alex Elder5240d9f2013-03-14 14:09:06 -05002278 if (list_empty(&msg->data))
Alex Elder4c59b4a2013-03-11 23:34:23 -05002279 return -EIO;
Alex Elder34d2d202013-03-08 20:58:59 -06002280
Alex Elderf5db90b2013-03-11 23:34:23 -05002281 if (do_datacrc)
2282 crc = con->in_data_crc;
Alex Elder643c68a2013-03-11 23:34:23 -05002283 while (cursor->resid) {
Shraddha Barke343128c2015-10-19 21:59:00 +05302284 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Alex Elder686be202013-03-11 23:34:23 -05002285 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
Alex Elderf5db90b2013-03-11 23:34:23 -05002286 if (ret <= 0) {
2287 if (do_datacrc)
2288 con->in_data_crc = crc;
2289
Alex Elder686be202013-03-11 23:34:23 -05002290 return ret;
Alex Elderf5db90b2013-03-11 23:34:23 -05002291 }
Alex Elder686be202013-03-11 23:34:23 -05002292
2293 if (do_datacrc)
Alex Elderf5db90b2013-03-11 23:34:23 -05002294 crc = ceph_crc32c_page(crc, page, page_offset, ret);
Shraddha Barke343128c2015-10-19 21:59:00 +05302295 (void) ceph_msg_data_advance(cursor, (size_t)ret);
Alex Elder34d2d202013-03-08 20:58:59 -06002296 }
Alex Elderf5db90b2013-03-11 23:34:23 -05002297 if (do_datacrc)
2298 con->in_data_crc = crc;
Alex Elder34d2d202013-03-08 20:58:59 -06002299
2300 return 1; /* must return > 0 to indicate success */
2301}
2302
Sage Weil31b80062009-10-06 11:31:13 -07002303/*
2304 * read (part of) a message.
2305 */
Alex Elder686be202013-03-11 23:34:23 -05002306static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2307
Sage Weil31b80062009-10-06 11:31:13 -07002308static int read_partial_message(struct ceph_connection *con)
2309{
2310 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05002311 int size;
2312 int end;
Sage Weil31b80062009-10-06 11:31:13 -07002313 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00002314 unsigned int front_len, middle_len, data_len;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002315 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Yan, Zheng33d07332014-11-04 16:33:37 +08002316 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
Sage Weilae187562010-04-22 07:47:01 -07002317 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06002318 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07002319
2320 dout("read_partial_message con %p msg %p\n", con, m);
2321
2322 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05002323 size = sizeof (con->in_hdr);
2324 end = size;
2325 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05002326 if (ret <= 0)
2327 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06002328
2329 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2330 if (cpu_to_le32(crc) != con->in_hdr.crc) {
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002331 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
Alex Elderfe3ad592012-02-15 07:43:54 -06002332 crc, con->in_hdr.crc);
2333 return -EBADMSG;
2334 }
2335
Sage Weil31b80062009-10-06 11:31:13 -07002336 front_len = le32_to_cpu(con->in_hdr.front_len);
2337 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2338 return -EIO;
2339 middle_len = le32_to_cpu(con->in_hdr.middle_len);
Alex Elder7b11ba32013-03-08 18:51:03 -06002340 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
Sage Weil31b80062009-10-06 11:31:13 -07002341 return -EIO;
2342 data_len = le32_to_cpu(con->in_hdr.data_len);
2343 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2344 return -EIO;
2345
Sage Weilae187562010-04-22 07:47:01 -07002346 /* verify seq# */
2347 seq = le64_to_cpu(con->in_hdr.seq);
2348 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07002349 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07002350 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002351 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07002352 seq, con->in_seq + 1);
2353 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002354 sizeof_footer(con);
Sage Weilae187562010-04-22 07:47:01 -07002355 con->in_tag = CEPH_MSGR_TAG_READY;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002356 return 1;
Sage Weilae187562010-04-22 07:47:01 -07002357 } else if ((s64)seq - (s64)con->in_seq > 1) {
2358 pr_err("read_partial_message bad seq %lld expected %lld\n",
2359 seq, con->in_seq + 1);
2360 con->error_msg = "bad message sequence # for incoming message";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002361 return -EBADE;
Sage Weilae187562010-04-22 07:47:01 -07002362 }
2363
Sage Weil31b80062009-10-06 11:31:13 -07002364 /* allocate message? */
2365 if (!con->in_msg) {
Sage Weil4740a622012-07-30 18:19:30 -07002366 int skip = 0;
2367
Sage Weil31b80062009-10-06 11:31:13 -07002368 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
Alex Elder6ebc8b32013-03-08 18:51:03 -06002369 front_len, data_len);
Sage Weil4740a622012-07-30 18:19:30 -07002370 ret = ceph_con_in_msg_alloc(con, &skip);
2371 if (ret < 0)
2372 return ret;
Alex Elderf759ebb2013-04-05 14:46:01 -05002373
2374 BUG_ON(!con->in_msg ^ skip);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002375 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07002376 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07002377 dout("alloc_msg said skip message\n");
Sage Weil31b80062009-10-06 11:31:13 -07002378 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002379 sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07002380 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002381 con->in_seq++;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002382 return 1;
Sage Weil31b80062009-10-06 11:31:13 -07002383 }
Alex Elder38941f82012-06-01 14:56:43 -05002384
Sage Weil4740a622012-07-30 18:19:30 -07002385 BUG_ON(!con->in_msg);
Alex Elder38941f82012-06-01 14:56:43 -05002386 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002387 m = con->in_msg;
2388 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002389 if (m->middle)
2390 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002391
Alex Elder78625052013-03-06 23:39:39 -06002392 /* prepare for data payload, if any */
Sage Weila4107022012-07-30 16:20:25 -07002393
Alex Elder78625052013-03-06 23:39:39 -06002394 if (data_len)
Alex Elder98fa5dd2013-04-02 12:09:50 -05002395 prepare_message_data(con->in_msg, data_len);
Sage Weil31b80062009-10-06 11:31:13 -07002396 }
2397
2398 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002399 ret = read_partial_message_section(con, &m->front, front_len,
2400 &con->in_front_crc);
2401 if (ret <= 0)
2402 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002403
2404 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002405 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07002406 ret = read_partial_message_section(con, &m->middle->vec,
2407 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002408 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07002409 if (ret <= 0)
2410 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002411 }
2412
2413 /* (page) data */
Alex Elder34d2d202013-03-08 20:58:59 -06002414 if (data_len) {
2415 ret = read_partial_msg_data(con);
2416 if (ret <= 0)
2417 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002418 }
2419
Sage Weil31b80062009-10-06 11:31:13 -07002420 /* footer */
Ilya Dryomov89f08172016-02-20 15:56:07 +01002421 size = sizeof_footer(con);
Alex Elderfd516532012-05-10 10:29:50 -05002422 end += size;
2423 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05002424 if (ret <= 0)
2425 return ret;
2426
Yan, Zheng33d07332014-11-04 16:33:37 +08002427 if (!need_sign) {
2428 m->footer.flags = m->old_footer.flags;
2429 m->footer.sig = 0;
2430 }
2431
Sage Weil31b80062009-10-06 11:31:13 -07002432 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2433 m, front_len, m->footer.front_crc, middle_len,
2434 m->footer.middle_crc, data_len, m->footer.data_crc);
2435
2436 /* crc ok? */
2437 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2438 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2439 m, con->in_front_crc, m->footer.front_crc);
2440 return -EBADMSG;
2441 }
2442 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2443 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2444 m, con->in_middle_crc, m->footer.middle_crc);
2445 return -EBADMSG;
2446 }
Alex Elderbca064d2012-02-15 07:43:54 -06002447 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07002448 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2449 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2450 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2451 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2452 return -EBADMSG;
2453 }
2454
Yan, Zheng33d07332014-11-04 16:33:37 +08002455 if (need_sign && con->ops->check_message_signature &&
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002456 con->ops->check_message_signature(m)) {
Yan, Zheng33d07332014-11-04 16:33:37 +08002457 pr_err("read_partial_message %p signature check failed\n", m);
2458 return -EBADMSG;
2459 }
2460
Sage Weil31b80062009-10-06 11:31:13 -07002461 return 1; /* done! */
2462}
2463
2464/*
2465 * Process message. This happens in the worker thread. The callback should
2466 * be careful not to do anything that waits on other incoming messages or it
2467 * may deadlock.
2468 */
2469static void process_message(struct ceph_connection *con)
2470{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002471 struct ceph_msg *msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07002472
Alex Elder38941f82012-06-01 14:56:43 -05002473 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002474 con->in_msg = NULL;
2475
2476 /* if first message, set peer_name */
2477 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07002478 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07002479
Sage Weil31b80062009-10-06 11:31:13 -07002480 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08002481 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002482
2483 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2484 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07002485 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07002486 le16_to_cpu(msg->hdr.type),
2487 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2488 le32_to_cpu(msg->hdr.front_len),
2489 le32_to_cpu(msg->hdr.data_len),
2490 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2491 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08002492
2493 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002494}
2495
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002496static int read_keepalive_ack(struct ceph_connection *con)
2497{
2498 struct ceph_timespec ceph_ts;
2499 size_t size = sizeof(ceph_ts);
2500 int ret = read_partial(con, size, size, &ceph_ts);
2501 if (ret <= 0)
2502 return ret;
2503 ceph_decode_timespec(&con->last_keepalive_ack, &ceph_ts);
2504 prepare_read_tag(con);
2505 return 1;
2506}
Sage Weil31b80062009-10-06 11:31:13 -07002507
2508/*
2509 * Write something to the socket. Called in a worker thread when the
2510 * socket appears to be writeable and we have something ready to send.
2511 */
2512static int try_write(struct ceph_connection *con)
2513{
Sage Weil31b80062009-10-06 11:31:13 -07002514 int ret = 1;
2515
Sage Weild59315c2012-06-21 12:49:23 -07002516 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002517
Sage Weil31b80062009-10-06 11:31:13 -07002518more:
2519 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2520
2521 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002522 if (con->state == CON_STATE_PREOPEN) {
2523 BUG_ON(con->sock);
2524 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002525
Alex Eldere2200422012-05-23 14:35:23 -05002526 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002527 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002528 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002529
Sage Weilcf3e5c42009-12-11 09:48:05 -08002530 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002531 con->in_tag = CEPH_MSGR_TAG_READY;
2532 dout("try_write initiating connect on %p new state %lu\n",
2533 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002534 ret = ceph_tcp_connect(con);
2535 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002536 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002537 goto out;
2538 }
2539 }
2540
2541more_kvec:
2542 /* kvec data queued? */
Ilya Dryomov67645d72015-12-28 13:18:34 +03002543 if (con->out_kvec_left) {
2544 ret = write_partial_kvec(con);
Sage Weil31b80062009-10-06 11:31:13 -07002545 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002546 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002547 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03002548 if (con->out_skip) {
2549 ret = write_partial_skip(con);
Sage Weil31b80062009-10-06 11:31:13 -07002550 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002551 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002552 }
2553
2554 /* msg pages? */
2555 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002556 if (con->out_msg_done) {
2557 ceph_msg_put(con->out_msg);
2558 con->out_msg = NULL; /* we're done with this one */
2559 goto do_next;
2560 }
2561
Alex Elder34d2d202013-03-08 20:58:59 -06002562 ret = write_partial_message_data(con);
Sage Weil31b80062009-10-06 11:31:13 -07002563 if (ret == 1)
2564 goto more_kvec; /* we need to send the footer, too! */
2565 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002566 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002567 if (ret < 0) {
Alex Elder34d2d202013-03-08 20:58:59 -06002568 dout("try_write write_partial_message_data err %d\n",
Sage Weil31b80062009-10-06 11:31:13 -07002569 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002570 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002571 }
2572 }
2573
Sage Weilc86a2932009-12-14 14:04:30 -08002574do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002575 if (con->state == CON_STATE_OPEN) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002576 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2577 prepare_write_keepalive(con);
2578 goto more;
2579 }
Sage Weil31b80062009-10-06 11:31:13 -07002580 /* is anything else pending? */
2581 if (!list_empty(&con->out_queue)) {
2582 prepare_write_message(con);
2583 goto more;
2584 }
2585 if (con->in_seq > con->in_seq_acked) {
2586 prepare_write_ack(con);
2587 goto more;
2588 }
Sage Weil31b80062009-10-06 11:31:13 -07002589 }
2590
2591 /* Nothing to do! */
Alex Elderc9ffc772013-02-20 10:25:12 -06002592 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07002593 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002594 ret = 0;
2595out:
Sage Weil42961d22011-01-25 08:19:34 -08002596 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002597 return ret;
2598}
2599
2600
2601
2602/*
2603 * Read what we can from the socket.
2604 */
2605static int try_read(struct ceph_connection *con)
2606{
Sage Weil31b80062009-10-06 11:31:13 -07002607 int ret = -1;
2608
Sage Weil31b80062009-10-06 11:31:13 -07002609more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002610 dout("try_read start on %p state %lu\n", con, con->state);
2611 if (con->state != CON_STATE_CONNECTING &&
2612 con->state != CON_STATE_NEGOTIATING &&
2613 con->state != CON_STATE_OPEN)
2614 return 0;
2615
2616 BUG_ON(!con->sock);
2617
Sage Weil31b80062009-10-06 11:31:13 -07002618 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2619 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002620
Sage Weil8dacc7d2012-07-20 17:24:40 -07002621 if (con->state == CON_STATE_CONNECTING) {
Alex Elder7593af92012-05-24 11:55:03 -05002622 dout("try_read connecting\n");
2623 ret = read_partial_banner(con);
2624 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002625 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002626 ret = process_banner(con);
2627 if (ret < 0)
2628 goto out;
2629
Sage Weil8dacc7d2012-07-20 17:24:40 -07002630 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002631
Jim Schutt6d4221b2012-08-10 10:37:38 -07002632 /*
2633 * Received banner is good, exchange connection info.
2634 * Do not reset out_kvec, as sending our banner raced
2635 * with receiving peer banner after connect completed.
2636 */
Alex Elder7593af92012-05-24 11:55:03 -05002637 ret = prepare_write_connect(con);
2638 if (ret < 0)
2639 goto out;
2640 prepare_read_connect(con);
2641
2642 /* Send connection info before awaiting response */
Sage Weil0da5d702011-05-19 11:21:05 -07002643 goto out;
2644 }
2645
Sage Weil8dacc7d2012-07-20 17:24:40 -07002646 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002647 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002648 ret = read_partial_connect(con);
2649 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002650 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002651 ret = process_connect(con);
2652 if (ret < 0)
2653 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002654 goto more;
2655 }
2656
Alex Elder122070a2012-12-26 10:43:57 -06002657 WARN_ON(con->state != CON_STATE_OPEN);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002658
Sage Weil31b80062009-10-06 11:31:13 -07002659 if (con->in_base_pos < 0) {
2660 /*
2661 * skipping + discarding content.
2662 *
2663 * FIXME: there must be a better way to do this!
2664 */
Alex Elder84495f42012-02-15 07:43:55 -06002665 static char buf[SKIP_BUF_SIZE];
2666 int skip = min((int) sizeof (buf), -con->in_base_pos);
2667
Sage Weil31b80062009-10-06 11:31:13 -07002668 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2669 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2670 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002671 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002672 con->in_base_pos += ret;
2673 if (con->in_base_pos)
2674 goto more;
2675 }
2676 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2677 /*
2678 * what's next?
2679 */
2680 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2681 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002682 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002683 dout("try_read got tag %d\n", (int)con->in_tag);
2684 switch (con->in_tag) {
2685 case CEPH_MSGR_TAG_MSG:
2686 prepare_read_message(con);
2687 break;
2688 case CEPH_MSGR_TAG_ACK:
2689 prepare_read_ack(con);
2690 break;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002691 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2692 prepare_read_keepalive_ack(con);
2693 break;
Sage Weil31b80062009-10-06 11:31:13 -07002694 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002695 con_close_socket(con);
2696 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002697 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002698 default:
2699 goto bad_tag;
2700 }
2701 }
2702 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2703 ret = read_partial_message(con);
2704 if (ret <= 0) {
2705 switch (ret) {
2706 case -EBADMSG:
Ilya Dryomova51983e2015-10-28 23:52:06 +01002707 con->error_msg = "bad crc/signature";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002708 /* fall through */
2709 case -EBADE:
Sage Weil31b80062009-10-06 11:31:13 -07002710 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002711 break;
Sage Weil31b80062009-10-06 11:31:13 -07002712 case -EIO:
2713 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002714 break;
Sage Weil31b80062009-10-06 11:31:13 -07002715 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002716 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002717 }
2718 if (con->in_tag == CEPH_MSGR_TAG_READY)
2719 goto more;
2720 process_message(con);
Sage Weil7b862e02012-07-30 18:16:56 -07002721 if (con->state == CON_STATE_OPEN)
2722 prepare_read_tag(con);
Sage Weil31b80062009-10-06 11:31:13 -07002723 goto more;
2724 }
Sage Weil3a230832013-03-25 08:47:40 -07002725 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2726 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2727 /*
2728 * the final handshake seq exchange is semantically
2729 * equivalent to an ACK
2730 */
Sage Weil31b80062009-10-06 11:31:13 -07002731 ret = read_partial_ack(con);
2732 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002733 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002734 process_ack(con);
2735 goto more;
2736 }
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002737 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2738 ret = read_keepalive_ack(con);
2739 if (ret <= 0)
2740 goto out;
2741 goto more;
2742 }
Sage Weil31b80062009-10-06 11:31:13 -07002743
Sage Weil31b80062009-10-06 11:31:13 -07002744out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002745 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002746 return ret;
2747
2748bad_tag:
2749 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2750 con->error_msg = "protocol error, garbage tag";
2751 ret = -1;
2752 goto out;
2753}
2754
2755
2756/*
Alex Elder802c6d92012-10-08 20:37:30 -07002757 * Atomically queue work on a connection after the specified delay.
2758 * Bump @con reference to avoid races with connection teardown.
2759 * Returns 0 if work was queued, or an error code otherwise.
Sage Weil31b80062009-10-06 11:31:13 -07002760 */
Alex Elder802c6d92012-10-08 20:37:30 -07002761static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
Sage Weil31b80062009-10-06 11:31:13 -07002762{
Sage Weil31b80062009-10-06 11:31:13 -07002763 if (!con->ops->get(con)) {
Alex Elder802c6d92012-10-08 20:37:30 -07002764 dout("%s %p ref count 0\n", __func__, con);
Alex Elder802c6d92012-10-08 20:37:30 -07002765 return -ENOENT;
Sage Weil31b80062009-10-06 11:31:13 -07002766 }
2767
Alex Elder802c6d92012-10-08 20:37:30 -07002768 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2769 dout("%s %p - already queued\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -07002770 con->ops->put(con);
Alex Elder802c6d92012-10-08 20:37:30 -07002771 return -EBUSY;
Sage Weil31b80062009-10-06 11:31:13 -07002772 }
Alex Elder802c6d92012-10-08 20:37:30 -07002773
2774 dout("%s %p %lu\n", __func__, con, delay);
Alex Elder802c6d92012-10-08 20:37:30 -07002775 return 0;
2776}
2777
2778static void queue_con(struct ceph_connection *con)
2779{
2780 (void) queue_con_delay(con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002781}
2782
Ilya Dryomov37ab77a2014-06-24 16:21:45 +04002783static void cancel_con(struct ceph_connection *con)
2784{
2785 if (cancel_delayed_work(&con->work)) {
2786 dout("%s %p\n", __func__, con);
2787 con->ops->put(con);
2788 }
2789}
2790
Alex Elder7bb21d682012-12-07 19:50:07 -06002791static bool con_sock_closed(struct ceph_connection *con)
2792{
Alex Elderc9ffc772013-02-20 10:25:12 -06002793 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
Alex Elder7bb21d682012-12-07 19:50:07 -06002794 return false;
2795
2796#define CASE(x) \
2797 case CON_STATE_ ## x: \
2798 con->error_msg = "socket closed (con state " #x ")"; \
2799 break;
2800
2801 switch (con->state) {
2802 CASE(CLOSED);
2803 CASE(PREOPEN);
2804 CASE(CONNECTING);
2805 CASE(NEGOTIATING);
2806 CASE(OPEN);
2807 CASE(STANDBY);
2808 default:
Joe Perchesb9a67892014-09-09 21:17:29 -07002809 pr_warn("%s con %p unrecognized state %lu\n",
Alex Elder7bb21d682012-12-07 19:50:07 -06002810 __func__, con, con->state);
2811 con->error_msg = "unrecognized con state";
2812 BUG();
2813 break;
2814 }
2815#undef CASE
2816
2817 return true;
2818}
2819
Alex Elderf20a39f2013-02-19 12:25:57 -06002820static bool con_backoff(struct ceph_connection *con)
2821{
2822 int ret;
2823
2824 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2825 return false;
2826
2827 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2828 if (ret) {
2829 dout("%s: con %p FAILED to back off %lu\n", __func__,
2830 con, con->delay);
2831 BUG_ON(ret == -ENOENT);
2832 con_flag_set(con, CON_FLAG_BACKOFF);
2833 }
2834
2835 return true;
2836}
2837
Alex Elder93209262013-02-19 12:25:57 -06002838/* Finish fault handling; con->mutex must *not* be held here */
2839
2840static void con_fault_finish(struct ceph_connection *con)
2841{
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002842 dout("%s %p\n", __func__, con);
2843
Alex Elder93209262013-02-19 12:25:57 -06002844 /*
2845 * in case we faulted due to authentication, invalidate our
2846 * current tickets so that we can get new ones.
2847 */
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002848 if (con->auth_retry) {
2849 dout("auth_retry %d, invalidating\n", con->auth_retry);
2850 if (con->ops->invalidate_authorizer)
2851 con->ops->invalidate_authorizer(con);
2852 con->auth_retry = 0;
Alex Elder93209262013-02-19 12:25:57 -06002853 }
2854
2855 if (con->ops->fault)
2856 con->ops->fault(con);
2857}
2858
Sage Weil31b80062009-10-06 11:31:13 -07002859/*
2860 * Do some work on a connection. Drop a connection ref when we're done.
2861 */
Ilya Dryomov68931622015-07-03 15:44:41 +03002862static void ceph_con_workfn(struct work_struct *work)
Sage Weil31b80062009-10-06 11:31:13 -07002863{
2864 struct ceph_connection *con = container_of(work, struct ceph_connection,
2865 work.work);
Alex Elder49659412013-02-19 12:25:57 -06002866 bool fault;
Sage Weil31b80062009-10-06 11:31:13 -07002867
Sage Weil9dd46582010-04-28 13:51:50 -07002868 mutex_lock(&con->mutex);
Alex Elder49659412013-02-19 12:25:57 -06002869 while (true) {
2870 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002871
Alex Elder49659412013-02-19 12:25:57 -06002872 if ((fault = con_sock_closed(con))) {
2873 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2874 break;
2875 }
2876 if (con_backoff(con)) {
2877 dout("%s: con %p BACKOFF\n", __func__, con);
2878 break;
2879 }
2880 if (con->state == CON_STATE_STANDBY) {
2881 dout("%s: con %p STANDBY\n", __func__, con);
2882 break;
2883 }
2884 if (con->state == CON_STATE_CLOSED) {
2885 dout("%s: con %p CLOSED\n", __func__, con);
2886 BUG_ON(con->sock);
2887 break;
2888 }
2889 if (con->state == CON_STATE_PREOPEN) {
2890 dout("%s: con %p PREOPEN\n", __func__, con);
2891 BUG_ON(con->sock);
2892 }
Sage Weil0da5d702011-05-19 11:21:05 -07002893
Alex Elder49659412013-02-19 12:25:57 -06002894 ret = try_read(con);
2895 if (ret < 0) {
2896 if (ret == -EAGAIN)
2897 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002898 if (!con->error_msg)
2899 con->error_msg = "socket error on read";
Alex Elder49659412013-02-19 12:25:57 -06002900 fault = true;
2901 break;
2902 }
2903
2904 ret = try_write(con);
2905 if (ret < 0) {
2906 if (ret == -EAGAIN)
2907 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002908 if (!con->error_msg)
2909 con->error_msg = "socket error on write";
Alex Elder49659412013-02-19 12:25:57 -06002910 fault = true;
2911 }
2912
2913 break; /* If we make it to here, we're done */
Sage Weil3a140a02012-07-30 16:24:21 -07002914 }
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002915 if (fault)
2916 con_fault(con);
Sage Weil9dd46582010-04-28 13:51:50 -07002917 mutex_unlock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002918
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002919 if (fault)
2920 con_fault_finish(con);
2921
2922 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07002923}
2924
Sage Weil31b80062009-10-06 11:31:13 -07002925/*
2926 * Generic error/fault handler. A retry mechanism is used with
2927 * exponential backoff
2928 */
Alex Elder93209262013-02-19 12:25:57 -06002929static void con_fault(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002930{
Sage Weil31b80062009-10-06 11:31:13 -07002931 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002932 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002933
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002934 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2935 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2936 con->error_msg = NULL;
2937
Alex Elder122070a2012-12-26 10:43:57 -06002938 WARN_ON(con->state != CON_STATE_CONNECTING &&
Sage Weil8dacc7d2012-07-20 17:24:40 -07002939 con->state != CON_STATE_NEGOTIATING &&
2940 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08002941
Sage Weil31b80062009-10-06 11:31:13 -07002942 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002943
Alex Elderc9ffc772013-02-20 10:25:12 -06002944 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002945 dout("fault on LOSSYTX channel, marking CLOSED\n");
2946 con->state = CON_STATE_CLOSED;
Alex Elder93209262013-02-19 12:25:57 -06002947 return;
Sage Weil3b5ede02012-07-20 15:22:53 -07002948 }
2949
Sage Weil5e095e82009-12-14 14:30:34 -08002950 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002951 BUG_ON(con->in_msg->con != con);
Sage Weil5e095e82009-12-14 14:30:34 -08002952 ceph_msg_put(con->in_msg);
2953 con->in_msg = NULL;
2954 }
Sage Weil31b80062009-10-06 11:31:13 -07002955
Sage Weile80a52d2010-02-25 12:40:45 -08002956 /* Requeue anything that hasn't been acked */
2957 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002958
Sage Weile76661d2011-03-03 10:10:15 -08002959 /* If there are no messages queued or keepalive pending, place
2960 * the connection in a STANDBY state */
2961 if (list_empty(&con->out_queue) &&
Alex Elderc9ffc772013-02-20 10:25:12 -06002962 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
Sage Weile00de342011-03-04 12:25:05 -08002963 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elderc9ffc772013-02-20 10:25:12 -06002964 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002965 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08002966 } else {
2967 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002968 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08002969 if (con->delay == 0)
2970 con->delay = BASE_DELAY_INTERVAL;
2971 else if (con->delay < MAX_DELAY_INTERVAL)
2972 con->delay *= 2;
Alex Elderc9ffc772013-02-20 10:25:12 -06002973 con_flag_set(con, CON_FLAG_BACKOFF);
Alex Elder8618e302012-10-08 20:37:30 -07002974 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -07002975 }
Sage Weil31b80062009-10-06 11:31:13 -07002976}
2977
2978
2979
2980/*
Alex Elder15d98822012-05-26 23:26:43 -05002981 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002982 */
Alex Elder15d98822012-05-26 23:26:43 -05002983void ceph_messenger_init(struct ceph_messenger *msgr,
Ilya Dryomov859bff52015-10-28 23:50:58 +01002984 struct ceph_entity_addr *myaddr)
Sage Weil31b80062009-10-06 11:31:13 -07002985{
Sage Weil31b80062009-10-06 11:31:13 -07002986 spin_lock_init(&msgr->global_seq_lock);
2987
Sage Weil31b80062009-10-06 11:31:13 -07002988 if (myaddr)
2989 msgr->inst.addr = *myaddr;
2990
2991 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002992 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002993 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002994 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002995
Guanjun Hea2a32582012-07-08 19:50:33 -07002996 atomic_set(&msgr->stopping, 0);
Ilya Dryomov757856d2015-06-25 17:47:45 +03002997 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
Sage Weil31b80062009-10-06 11:31:13 -07002998
Alex Elder15d98822012-05-26 23:26:43 -05002999 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003000}
Alex Elder15d98822012-05-26 23:26:43 -05003001EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07003002
Ilya Dryomov757856d2015-06-25 17:47:45 +03003003void ceph_messenger_fini(struct ceph_messenger *msgr)
3004{
3005 put_net(read_pnet(&msgr->net));
3006}
3007EXPORT_SYMBOL(ceph_messenger_fini);
3008
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003009static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3010{
3011 if (msg->con)
3012 msg->con->ops->put(msg->con);
3013
3014 msg->con = con ? con->ops->get(con) : NULL;
3015 BUG_ON(msg->con != con);
3016}
3017
Sage Weile00de342011-03-04 12:25:05 -08003018static void clear_standby(struct ceph_connection *con)
3019{
3020 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003021 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08003022 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003023 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08003024 con->connect_seq++;
Alex Elderc9ffc772013-02-20 10:25:12 -06003025 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3026 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
Sage Weile00de342011-03-04 12:25:05 -08003027 }
3028}
3029
Sage Weil31b80062009-10-06 11:31:13 -07003030/*
3031 * Queue up an outgoing message on the given connection.
3032 */
3033void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3034{
Sage Weila59b55a2012-07-20 15:34:04 -07003035 /* set src+dst */
3036 msg->hdr.src = con->msgr->inst.name;
3037 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3038 msg->needs_out_seq = true;
3039
3040 mutex_lock(&con->mutex);
3041
Sage Weil8dacc7d2012-07-20 17:24:40 -07003042 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07003043 dout("con_send %p closed, dropping %p\n", con, msg);
3044 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07003045 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003046 return;
3047 }
3048
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003049 msg_con_set(msg, con);
Sage Weil31b80062009-10-06 11:31:13 -07003050
Sage Weil31b80062009-10-06 11:31:13 -07003051 BUG_ON(!list_empty(&msg->list_head));
3052 list_add_tail(&msg->list_head, &con->out_queue);
3053 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3054 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3055 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3056 le32_to_cpu(msg->hdr.front_len),
3057 le32_to_cpu(msg->hdr.middle_len),
3058 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07003059
3060 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08003061 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003062
3063 /* if there wasn't anything waiting to send before, queue
3064 * new work */
Alex Elderc9ffc772013-02-20 10:25:12 -06003065 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003066 queue_con(con);
3067}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003068EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07003069
3070/*
3071 * Revoke a message that was previously queued for send
3072 */
Alex Elder6740a842012-06-01 14:56:43 -05003073void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003074{
Alex Elder6740a842012-06-01 14:56:43 -05003075 struct ceph_connection *con = msg->con;
3076
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003077 if (!con) {
3078 dout("%s msg %p null con\n", __func__, msg);
Alex Elder6740a842012-06-01 14:56:43 -05003079 return; /* Message not in our possession */
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003080 }
Alex Elder6740a842012-06-01 14:56:43 -05003081
Sage Weilec302642009-12-22 10:43:42 -08003082 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003083 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05003084 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07003085 list_del_init(&msg->list_head);
Sage Weil31b80062009-10-06 11:31:13 -07003086 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05003087
Sage Weil31b80062009-10-06 11:31:13 -07003088 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07003089 }
3090 if (con->out_msg == msg) {
Ilya Dryomov67645d72015-12-28 13:18:34 +03003091 BUG_ON(con->out_skip);
3092 /* footer */
3093 if (con->out_msg_done) {
3094 con->out_skip += con_out_kvec_skip(con);
3095 } else {
3096 BUG_ON(!msg->data_length);
Ilya Dryomov89f08172016-02-20 15:56:07 +01003097 con->out_skip += sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07003098 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003099 /* data, middle, front */
3100 if (msg->data_length)
3101 con->out_skip += msg->cursor.total_resid;
3102 if (msg->middle)
3103 con->out_skip += con_out_kvec_skip(con);
3104 con->out_skip += con_out_kvec_skip(con);
Alex Elder92ce0342012-06-04 14:43:33 -05003105
Ilya Dryomov67645d72015-12-28 13:18:34 +03003106 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3107 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3108 msg->hdr.seq = 0;
3109 con->out_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05003110 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07003111 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003112
Sage Weilec302642009-12-22 10:43:42 -08003113 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003114}
3115
3116/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003117 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08003118 */
Alex Elder8921d112012-06-01 14:56:43 -05003119void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08003120{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003121 struct ceph_connection *con = msg->con;
Alex Elder8921d112012-06-01 14:56:43 -05003122
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003123 if (!con) {
Alex Elder8921d112012-06-01 14:56:43 -05003124 dout("%s msg %p null con\n", __func__, msg);
Alex Elder8921d112012-06-01 14:56:43 -05003125 return; /* Message not in our possession */
3126 }
3127
Sage Weil350b1c32009-12-22 10:45:45 -08003128 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05003129 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00003130 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3131 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3132 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08003133
3134 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05003135 dout("%s %p msg %p revoked\n", __func__, con, msg);
3136 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08003137 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003138 front_len -
3139 middle_len -
3140 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08003141 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08003142 ceph_msg_put(con->in_msg);
3143 con->in_msg = NULL;
3144 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07003145 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08003146 } else {
Alex Elder8921d112012-06-01 14:56:43 -05003147 dout("%s %p in_msg %p msg %p no-op\n",
3148 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08003149 }
3150 mutex_unlock(&con->mutex);
3151}
3152
3153/*
Sage Weil31b80062009-10-06 11:31:13 -07003154 * Queue a keepalive byte to ensure the tcp connection is alive.
3155 */
3156void ceph_con_keepalive(struct ceph_connection *con)
3157{
Sage Weile00de342011-03-04 12:25:05 -08003158 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07003159 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08003160 clear_standby(con);
Sage Weil00650932012-07-20 15:33:04 -07003161 mutex_unlock(&con->mutex);
Alex Elderc9ffc772013-02-20 10:25:12 -06003162 if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
3163 con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003164 queue_con(con);
3165}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003166EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07003167
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003168bool ceph_con_keepalive_expired(struct ceph_connection *con,
3169 unsigned long interval)
3170{
3171 if (interval > 0 &&
3172 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
3173 struct timespec now = CURRENT_TIME;
3174 struct timespec ts;
3175 jiffies_to_timespec(interval, &ts);
3176 ts = timespec_add(con->last_keepalive_ack, ts);
3177 return timespec_compare(&now, &ts) >= 0;
3178 }
3179 return false;
3180}
3181
Alex Elder6644ed72013-03-11 23:34:24 -05003182static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
Alex Elder43794502013-03-01 18:00:16 -06003183{
Alex Elder6644ed72013-03-11 23:34:24 -05003184 struct ceph_msg_data *data;
3185
3186 if (WARN_ON(!ceph_msg_data_type_valid(type)))
3187 return NULL;
3188
Alex Elder81b36be2013-05-01 12:43:04 -05003189 data = kmem_cache_zalloc(ceph_msg_data_cache, GFP_NOFS);
Alex Elder6644ed72013-03-11 23:34:24 -05003190 if (data)
3191 data->type = type;
Alex Elder5240d9f2013-03-14 14:09:06 -05003192 INIT_LIST_HEAD(&data->links);
Alex Elder6644ed72013-03-11 23:34:24 -05003193
3194 return data;
3195}
3196
3197static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3198{
3199 if (!data)
3200 return;
3201
Alex Elder5240d9f2013-03-14 14:09:06 -05003202 WARN_ON(!list_empty(&data->links));
Yan, Zhenge4339d282014-09-16 17:50:45 +08003203 if (data->type == CEPH_MSG_DATA_PAGELIST)
Alex Elder6644ed72013-03-11 23:34:24 -05003204 ceph_pagelist_release(data->pagelist);
Alex Elder81b36be2013-05-01 12:43:04 -05003205 kmem_cache_free(ceph_msg_data_cache, data);
Alex Elder43794502013-03-01 18:00:16 -06003206}
3207
Alex Elder90af3602013-04-05 14:46:01 -05003208void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Alex Elderf1baeb22013-03-07 15:38:26 -06003209 size_t length, size_t alignment)
Alex Elder02afca62013-02-14 12:16:43 -06003210{
Alex Elder6644ed72013-03-11 23:34:24 -05003211 struct ceph_msg_data *data;
3212
Alex Elder07aa1552013-03-04 18:29:06 -06003213 BUG_ON(!pages);
3214 BUG_ON(!length);
Alex Elder02afca62013-02-14 12:16:43 -06003215
Alex Elder6644ed72013-03-11 23:34:24 -05003216 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
3217 BUG_ON(!data);
3218 data->pages = pages;
3219 data->length = length;
3220 data->alignment = alignment & ~PAGE_MASK;
3221
Alex Elder5240d9f2013-03-14 14:09:06 -05003222 list_add_tail(&data->links, &msg->data);
3223 msg->data_length += length;
Alex Elder02afca62013-02-14 12:16:43 -06003224}
Alex Elder90af3602013-04-05 14:46:01 -05003225EXPORT_SYMBOL(ceph_msg_data_add_pages);
Sage Weil31b80062009-10-06 11:31:13 -07003226
Alex Elder90af3602013-04-05 14:46:01 -05003227void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
Alex Elder27fa8382013-02-14 12:16:43 -06003228 struct ceph_pagelist *pagelist)
3229{
Alex Elder6644ed72013-03-11 23:34:24 -05003230 struct ceph_msg_data *data;
3231
Alex Elder07aa1552013-03-04 18:29:06 -06003232 BUG_ON(!pagelist);
3233 BUG_ON(!pagelist->length);
Alex Elder27fa8382013-02-14 12:16:43 -06003234
Alex Elder6644ed72013-03-11 23:34:24 -05003235 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
3236 BUG_ON(!data);
3237 data->pagelist = pagelist;
3238
Alex Elder5240d9f2013-03-14 14:09:06 -05003239 list_add_tail(&data->links, &msg->data);
3240 msg->data_length += pagelist->length;
Alex Elder27fa8382013-02-14 12:16:43 -06003241}
Alex Elder90af3602013-04-05 14:46:01 -05003242EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
Alex Elder27fa8382013-02-14 12:16:43 -06003243
Alex Elderea965712013-04-05 14:46:01 -05003244#ifdef CONFIG_BLOCK
Alex Elder90af3602013-04-05 14:46:01 -05003245void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
Alex Eldera1930802013-03-14 14:09:06 -05003246 size_t length)
Alex Elder27fa8382013-02-14 12:16:43 -06003247{
Alex Elder6644ed72013-03-11 23:34:24 -05003248 struct ceph_msg_data *data;
Alex Elder27fa8382013-02-14 12:16:43 -06003249
Alex Elder6644ed72013-03-11 23:34:24 -05003250 BUG_ON(!bio);
Alex Elder6644ed72013-03-11 23:34:24 -05003251
3252 data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
3253 BUG_ON(!data);
3254 data->bio = bio;
Alex Elderc851c492013-04-05 14:46:01 -05003255 data->bio_length = length;
Alex Elder6644ed72013-03-11 23:34:24 -05003256
Alex Elder5240d9f2013-03-14 14:09:06 -05003257 list_add_tail(&data->links, &msg->data);
3258 msg->data_length += length;
Alex Elder27fa8382013-02-14 12:16:43 -06003259}
Alex Elder90af3602013-04-05 14:46:01 -05003260EXPORT_SYMBOL(ceph_msg_data_add_bio);
Alex Elderea965712013-04-05 14:46:01 -05003261#endif /* CONFIG_BLOCK */
Alex Elder27fa8382013-02-14 12:16:43 -06003262
Sage Weil31b80062009-10-06 11:31:13 -07003263/*
3264 * construct a new message with given type, size
3265 * the new msg has a ref count of 1.
3266 */
Sage Weilb61c2762011-08-09 15:03:46 -07003267struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3268 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07003269{
3270 struct ceph_msg *m;
3271
Alex Eldere3d5d632013-05-01 12:43:04 -05003272 m = kmem_cache_zalloc(ceph_msg_cache, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003273 if (m == NULL)
3274 goto out;
Alex Elder38941f82012-06-01 14:56:43 -05003275
Sage Weil31b80062009-10-06 11:31:13 -07003276 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07003277 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
Sage Weil31b80062009-10-06 11:31:13 -07003278 m->hdr.front_len = cpu_to_le32(front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003279
Alex Elder9516e452013-03-01 18:00:16 -06003280 INIT_LIST_HEAD(&m->list_head);
3281 kref_init(&m->kref);
Alex Elder5240d9f2013-03-14 14:09:06 -05003282 INIT_LIST_HEAD(&m->data);
Henry C Changca208922011-05-03 02:29:56 +00003283
Sage Weil31b80062009-10-06 11:31:13 -07003284 /* front */
3285 if (front_len) {
Ilya Dryomoveeb0bed2014-01-09 20:08:21 +02003286 m->front.iov_base = ceph_kvmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003287 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07003288 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07003289 front_len);
3290 goto out2;
3291 }
3292 } else {
3293 m->front.iov_base = NULL;
3294 }
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02003295 m->front_alloc_len = m->front.iov_len = front_len;
Sage Weil31b80062009-10-06 11:31:13 -07003296
Sage Weilbb257662010-04-01 16:07:23 -07003297 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003298 return m;
3299
3300out2:
3301 ceph_msg_put(m);
3302out:
Sage Weilb61c2762011-08-09 15:03:46 -07003303 if (!can_fail) {
3304 pr_err("msg_new can't create type %d front %d\n", type,
3305 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07003306 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07003307 } else {
3308 dout("msg_new can't create type %d front %d\n", type,
3309 front_len);
3310 }
Sage Weila79832f2010-04-01 16:06:19 -07003311 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003312}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003313EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07003314
3315/*
Sage Weil31b80062009-10-06 11:31:13 -07003316 * Allocate "middle" portion of a message, if it is needed and wasn't
3317 * allocated by alloc_msg. This allows us to read a small fixed-size
3318 * per-type header in the front and then gracefully fail (i.e.,
3319 * propagate the error to the caller based on info in the front) when
3320 * the middle is too large.
3321 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08003322static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003323{
3324 int type = le16_to_cpu(msg->hdr.type);
3325 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3326
3327 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3328 ceph_msg_type_name(type), middle_len);
3329 BUG_ON(!middle_len);
3330 BUG_ON(msg->middle);
3331
Sage Weilb6c1d5b2009-12-07 12:17:17 -08003332 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07003333 if (!msg->middle)
3334 return -ENOMEM;
3335 return 0;
3336}
3337
Yehuda Sadeh24504182010-01-08 13:58:34 -08003338/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05003339 * Allocate a message for receiving an incoming message on a
3340 * connection, and save the result in con->in_msg. Uses the
3341 * connection's private alloc_msg op if available.
3342 *
Sage Weil4740a622012-07-30 18:19:30 -07003343 * Returns 0 on success, or a negative error code.
3344 *
3345 * On success, if we set *skip = 1:
3346 * - the next message should be skipped and ignored.
3347 * - con->in_msg == NULL
3348 * or if we set *skip = 0:
3349 * - con->in_msg is non-null.
3350 * On error (ENOMEM, EAGAIN, ...),
3351 * - con->in_msg == NULL
Yehuda Sadeh24504182010-01-08 13:58:34 -08003352 */
Sage Weil4740a622012-07-30 18:19:30 -07003353static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08003354{
Sage Weil4740a622012-07-30 18:19:30 -07003355 struct ceph_msg_header *hdr = &con->in_hdr;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003356 int middle_len = le32_to_cpu(hdr->middle_len);
Alex Elder1d866d12013-03-01 18:00:14 -06003357 struct ceph_msg *msg;
Sage Weil4740a622012-07-30 18:19:30 -07003358 int ret = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003359
Alex Elder1c20f2d2012-06-04 14:43:32 -05003360 BUG_ON(con->in_msg != NULL);
Alex Elder53ded492013-03-01 18:00:14 -06003361 BUG_ON(!con->ops->alloc_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003362
Alex Elder53ded492013-03-01 18:00:14 -06003363 mutex_unlock(&con->mutex);
3364 msg = con->ops->alloc_msg(con, hdr, skip);
3365 mutex_lock(&con->mutex);
3366 if (con->state != CON_STATE_OPEN) {
3367 if (msg)
Alex Elder1d866d12013-03-01 18:00:14 -06003368 ceph_msg_put(msg);
Alex Elder53ded492013-03-01 18:00:14 -06003369 return -EAGAIN;
3370 }
Alex Elder41375772013-03-05 09:25:10 -06003371 if (msg) {
3372 BUG_ON(*skip);
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003373 msg_con_set(msg, con);
Alex Elder41375772013-03-05 09:25:10 -06003374 con->in_msg = msg;
Alex Elder41375772013-03-05 09:25:10 -06003375 } else {
3376 /*
3377 * Null message pointer means either we should skip
3378 * this message or we couldn't allocate memory. The
3379 * former is not an error.
3380 */
3381 if (*skip)
3382 return 0;
Alex Elder41375772013-03-05 09:25:10 -06003383
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03003384 con->error_msg = "error allocating memory for incoming message";
Alex Elder53ded492013-03-01 18:00:14 -06003385 return -ENOMEM;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003386 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05003387 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08003388
Alex Elder1c20f2d2012-06-04 14:43:32 -05003389 if (middle_len && !con->in_msg->middle) {
3390 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003391 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05003392 ceph_msg_put(con->in_msg);
3393 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003394 }
3395 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08003396
Sage Weil4740a622012-07-30 18:19:30 -07003397 return ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003398}
3399
Sage Weil31b80062009-10-06 11:31:13 -07003400
3401/*
3402 * Free a generically kmalloc'd message.
3403 */
Ilya Dryomov0215e442014-06-20 14:14:41 +04003404static void ceph_msg_free(struct ceph_msg *m)
Sage Weil31b80062009-10-06 11:31:13 -07003405{
Ilya Dryomov0215e442014-06-20 14:14:41 +04003406 dout("%s %p\n", __func__, m);
Ilya Dryomov4965fc32014-10-23 16:32:57 +04003407 kvfree(m->front.iov_base);
Alex Eldere3d5d632013-05-01 12:43:04 -05003408 kmem_cache_free(ceph_msg_cache, m);
Sage Weil31b80062009-10-06 11:31:13 -07003409}
3410
Ilya Dryomov0215e442014-06-20 14:14:41 +04003411static void ceph_msg_release(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07003412{
Sage Weilc2e552e2009-12-07 15:55:05 -08003413 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Geliang Tang10bcee12015-12-18 23:33:30 +08003414 struct ceph_msg_data *data, *next;
Sage Weil31b80062009-10-06 11:31:13 -07003415
Ilya Dryomov0215e442014-06-20 14:14:41 +04003416 dout("%s %p\n", __func__, m);
Sage Weilc2e552e2009-12-07 15:55:05 -08003417 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07003418
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003419 msg_con_set(m, NULL);
3420
Sage Weilc2e552e2009-12-07 15:55:05 -08003421 /* drop middle, data, if any */
3422 if (m->middle) {
3423 ceph_buffer_put(m->middle);
3424 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003425 }
Alex Elder5240d9f2013-03-14 14:09:06 -05003426
Geliang Tang10bcee12015-12-18 23:33:30 +08003427 list_for_each_entry_safe(data, next, &m->data, links) {
3428 list_del_init(&data->links);
Alex Elder5240d9f2013-03-14 14:09:06 -05003429 ceph_msg_data_destroy(data);
3430 }
Alex Eldera1930802013-03-14 14:09:06 -05003431 m->data_length = 0;
Sage Weil58bb3b32009-12-23 12:12:31 -08003432
Sage Weilc2e552e2009-12-07 15:55:05 -08003433 if (m->pool)
3434 ceph_msgpool_put(m->pool, m);
3435 else
Ilya Dryomov0215e442014-06-20 14:14:41 +04003436 ceph_msg_free(m);
Sage Weil31b80062009-10-06 11:31:13 -07003437}
Ilya Dryomov0215e442014-06-20 14:14:41 +04003438
3439struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3440{
3441 dout("%s %p (was %d)\n", __func__, msg,
3442 atomic_read(&msg->kref.refcount));
3443 kref_get(&msg->kref);
3444 return msg;
3445}
3446EXPORT_SYMBOL(ceph_msg_get);
3447
3448void ceph_msg_put(struct ceph_msg *msg)
3449{
3450 dout("%s %p (was %d)\n", __func__, msg,
3451 atomic_read(&msg->kref.refcount));
3452 kref_put(&msg->kref, ceph_msg_release);
3453}
3454EXPORT_SYMBOL(ceph_msg_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003455
3456void ceph_msg_dump(struct ceph_msg *msg)
3457{
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02003458 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3459 msg->front_alloc_len, msg->data_length);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003460 print_hex_dump(KERN_DEBUG, "header: ",
3461 DUMP_PREFIX_OFFSET, 16, 1,
3462 &msg->hdr, sizeof(msg->hdr), true);
3463 print_hex_dump(KERN_DEBUG, " front: ",
3464 DUMP_PREFIX_OFFSET, 16, 1,
3465 msg->front.iov_base, msg->front.iov_len, true);
3466 if (msg->middle)
3467 print_hex_dump(KERN_DEBUG, "middle: ",
3468 DUMP_PREFIX_OFFSET, 16, 1,
3469 msg->middle->vec.iov_base,
3470 msg->middle->vec.iov_len, true);
3471 print_hex_dump(KERN_DEBUG, "footer: ",
3472 DUMP_PREFIX_OFFSET, 16, 1,
3473 &msg->footer, sizeof(msg->footer), true);
3474}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003475EXPORT_SYMBOL(ceph_msg_dump);