blob: cca96d32ac6413f9a5c8668d9ed9dc3442e25bda [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07003
4#include <linux/crc32c.h>
5#include <linux/ctype.h>
6#include <linux/highmem.h>
7#include <linux/inet.h>
8#include <linux/kthread.h>
9#include <linux/net.h>
Ilya Dryomov757856d2015-06-25 17:47:45 +030010#include <linux/nsproxy.h>
Ilya Dryomov633ee4072017-03-21 13:44:28 +010011#include <linux/sched/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070013#include <linux/socket.h>
14#include <linux/string.h>
Alex Elder3ebc21f2013-01-31 16:02:01 -060015#ifdef CONFIG_BLOCK
Yehuda Sadeh68b44762010-04-06 15:01:27 -070016#include <linux/bio.h>
Alex Elder3ebc21f2013-01-31 16:02:01 -060017#endif /* CONFIG_BLOCK */
Noah Watkinsee3b56f2011-09-23 11:48:42 -070018#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070019#include <net/tcp.h>
20
Ilya Dryomov2b3e0c92013-12-24 21:19:24 +020021#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070022#include <linux/ceph/libceph.h>
23#include <linux/ceph/messenger.h>
24#include <linux/ceph/decode.h>
25#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040026#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070027
28/*
29 * Ceph uses the messenger to exchange ceph_msg messages with other
30 * hosts in the system. The messenger provides ordered and reliable
31 * delivery. We tolerate TCP disconnects by reconnecting (with
32 * exponential backoff) in the case of a fault (disconnection, bad
33 * crc, protocol error). Acks allow sent messages to be discarded by
34 * the sender.
35 */
36
Alex Elderbc18f4b2012-06-20 21:53:53 -050037/*
38 * We track the state of the socket on a given connection using
39 * values defined below. The transition to a new socket state is
40 * handled by a function which verifies we aren't coming from an
41 * unexpected state.
42 *
43 * --------
44 * | NEW* | transient initial state
45 * --------
46 * | con_sock_state_init()
47 * v
48 * ----------
49 * | CLOSED | initialized, but no socket (and no
50 * ---------- TCP connection)
51 * ^ \
52 * | \ con_sock_state_connecting()
53 * | ----------------------
54 * | \
55 * + con_sock_state_closed() \
Sage Weilfbb85a42012-06-27 12:31:02 -070056 * |+--------------------------- \
57 * | \ \ \
58 * | ----------- \ \
59 * | | CLOSING | socket event; \ \
60 * | ----------- await close \ \
61 * | ^ \ |
62 * | | \ |
63 * | + con_sock_state_closing() \ |
64 * | / \ | |
65 * | / --------------- | |
66 * | / \ v v
Alex Elderbc18f4b2012-06-20 21:53:53 -050067 * | / --------------
68 * | / -----------------| CONNECTING | socket created, TCP
69 * | | / -------------- connect initiated
70 * | | | con_sock_state_connected()
71 * | | v
72 * -------------
73 * | CONNECTED | TCP connection established
74 * -------------
75 *
76 * State values for ceph_connection->sock_state; NEW is assumed to be 0.
77 */
Alex Elderce2c8902012-05-22 22:15:49 -050078
79#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
80#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
81#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
82#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
83#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
84
Sage Weil8dacc7d2012-07-20 17:24:40 -070085/*
86 * connection states
87 */
88#define CON_STATE_CLOSED 1 /* -> PREOPEN */
89#define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
90#define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
91#define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
92#define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
93#define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
94
Sage Weil4a861692012-07-20 17:29:55 -070095/*
96 * ceph_connection flag bits
97 */
98#define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
99 * messages on errors */
100#define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
101#define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
102#define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
103#define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
Sage Weil8dacc7d2012-07-20 17:24:40 -0700104
Alex Elderc9ffc772013-02-20 10:25:12 -0600105static bool con_flag_valid(unsigned long con_flag)
106{
107 switch (con_flag) {
108 case CON_FLAG_LOSSYTX:
109 case CON_FLAG_KEEPALIVE_PENDING:
110 case CON_FLAG_WRITE_PENDING:
111 case CON_FLAG_SOCK_CLOSED:
112 case CON_FLAG_BACKOFF:
113 return true;
114 default:
115 return false;
116 }
117}
118
119static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
120{
121 BUG_ON(!con_flag_valid(con_flag));
122
123 clear_bit(con_flag, &con->flags);
124}
125
126static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
127{
128 BUG_ON(!con_flag_valid(con_flag));
129
130 set_bit(con_flag, &con->flags);
131}
132
133static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
134{
135 BUG_ON(!con_flag_valid(con_flag));
136
137 return test_bit(con_flag, &con->flags);
138}
139
140static bool con_flag_test_and_clear(struct ceph_connection *con,
141 unsigned long con_flag)
142{
143 BUG_ON(!con_flag_valid(con_flag));
144
145 return test_and_clear_bit(con_flag, &con->flags);
146}
147
148static bool con_flag_test_and_set(struct ceph_connection *con,
149 unsigned long con_flag)
150{
151 BUG_ON(!con_flag_valid(con_flag));
152
153 return test_and_set_bit(con_flag, &con->flags);
154}
155
Alex Eldere3d5d632013-05-01 12:43:04 -0500156/* Slab caches for frequently-allocated structures */
157
158static struct kmem_cache *ceph_msg_cache;
159
Sage Weil31b80062009-10-06 11:31:13 -0700160/* static tag bytes (protocol control messages) */
161static char tag_msg = CEPH_MSGR_TAG_MSG;
162static char tag_ack = CEPH_MSGR_TAG_ACK;
163static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800164static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
Sage Weil31b80062009-10-06 11:31:13 -0700165
Sage Weila6a53492010-04-13 14:07:07 -0700166#ifdef CONFIG_LOCKDEP
167static struct lock_class_key socket_class;
168#endif
169
Sage Weil31b80062009-10-06 11:31:13 -0700170static void queue_con(struct ceph_connection *con);
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400171static void cancel_con(struct ceph_connection *con);
Ilya Dryomov68931622015-07-03 15:44:41 +0300172static void ceph_con_workfn(struct work_struct *);
Alex Elder93209262013-02-19 12:25:57 -0600173static void con_fault(struct ceph_connection *con);
Sage Weil31b80062009-10-06 11:31:13 -0700174
Sage Weil31b80062009-10-06 11:31:13 -0700175/*
Alex Elderf64a9312012-01-23 15:49:27 -0600176 * Nicely render a sockaddr as a string. An array of formatted
177 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -0700178 */
Alex Elderf64a9312012-01-23 15:49:27 -0600179#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
180#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
181#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
182#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
183
184static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
185static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -0700186
Alex Elder57666512012-01-23 15:49:27 -0600187static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -0600188
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700189const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -0700190{
191 int i;
192 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -0600193 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
194 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -0700195
Alex Elderf64a9312012-01-23 15:49:27 -0600196 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700197 s = addr_str[i];
198
199 switch (ss->ss_family) {
200 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -0600201 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
202 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700203 break;
204
205 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -0600206 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
207 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700208 break;
209
210 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600211 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
212 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700213 }
214
215 return s;
216}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700217EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700218
Sage Weil63f2d212009-11-03 15:17:56 -0800219static void encode_my_addr(struct ceph_messenger *msgr)
220{
221 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
222 ceph_encode_addr(&msgr->my_enc_addr);
223}
224
Sage Weil31b80062009-10-06 11:31:13 -0700225/*
226 * work queue for all reading and writing to/from the socket.
227 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600228static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700229
Alex Eldere3d5d632013-05-01 12:43:04 -0500230static int ceph_msgr_slab_init(void)
231{
232 BUG_ON(ceph_msg_cache);
Geliang Tang5ee61e92016-03-13 15:18:39 +0800233 ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
Alex Elder81b36be2013-05-01 12:43:04 -0500234 if (!ceph_msg_cache)
235 return -ENOMEM;
236
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200237 return 0;
Alex Eldere3d5d632013-05-01 12:43:04 -0500238}
239
240static void ceph_msgr_slab_exit(void)
241{
242 BUG_ON(!ceph_msg_cache);
243 kmem_cache_destroy(ceph_msg_cache);
244 ceph_msg_cache = NULL;
245}
246
Alex Elder15417162013-02-19 12:25:56 -0600247static void _ceph_msgr_exit(void)
Alex Elder6173d1f2012-02-14 14:05:33 -0600248{
Alex Elderd3002b92012-02-14 14:05:33 -0600249 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600250 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600251 ceph_msgr_wq = NULL;
252 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600253
Alex Elder6173d1f2012-02-14 14:05:33 -0600254 BUG_ON(zero_page == NULL);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300255 put_page(zero_page);
Alex Elder6173d1f2012-02-14 14:05:33 -0600256 zero_page = NULL;
Benoît Canetd920ff62015-06-25 21:02:57 +0200257
258 ceph_msgr_slab_exit();
Alex Elder6173d1f2012-02-14 14:05:33 -0600259}
260
Chengguang Xu57a35df2018-03-10 20:32:05 +0800261int __init ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700262{
Benoît Canetd920ff62015-06-25 21:02:57 +0200263 if (ceph_msgr_slab_init())
264 return -ENOMEM;
265
Alex Elder57666512012-01-23 15:49:27 -0600266 BUG_ON(zero_page != NULL);
267 zero_page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300268 get_page(zero_page);
Alex Elder57666512012-01-23 15:49:27 -0600269
Ilya Dryomovf9865f02014-10-10 16:39:05 +0400270 /*
271 * The number of active work items is limited by the number of
272 * connections, so leave @max_active at default.
273 */
274 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600275 if (ceph_msgr_wq)
276 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600277
Alex Elder6173d1f2012-02-14 14:05:33 -0600278 pr_err("msgr_init failed to create workqueue\n");
279 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600280
Alex Elder6173d1f2012-02-14 14:05:33 -0600281 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700282}
283
284void ceph_msgr_exit(void)
285{
Alex Elder57666512012-01-23 15:49:27 -0600286 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600287
Alex Elder6173d1f2012-02-14 14:05:33 -0600288 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700289}
290
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700291void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700292{
293 flush_workqueue(ceph_msgr_wq);
294}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700295EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700296
Alex Elderce2c8902012-05-22 22:15:49 -0500297/* Connection socket state transition functions */
298
299static void con_sock_state_init(struct ceph_connection *con)
300{
301 int old_state;
302
303 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
304 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
305 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700306 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
307 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500308}
309
310static void con_sock_state_connecting(struct ceph_connection *con)
311{
312 int old_state;
313
314 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
315 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
316 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700317 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
318 CON_SOCK_STATE_CONNECTING);
Alex Elderce2c8902012-05-22 22:15:49 -0500319}
320
321static void con_sock_state_connected(struct ceph_connection *con)
322{
323 int old_state;
324
325 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
326 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
327 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700328 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
329 CON_SOCK_STATE_CONNECTED);
Alex Elderce2c8902012-05-22 22:15:49 -0500330}
331
332static void con_sock_state_closing(struct ceph_connection *con)
333{
334 int old_state;
335
336 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
337 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
338 old_state != CON_SOCK_STATE_CONNECTED &&
339 old_state != CON_SOCK_STATE_CLOSING))
340 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700341 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
342 CON_SOCK_STATE_CLOSING);
Alex Elderce2c8902012-05-22 22:15:49 -0500343}
344
345static void con_sock_state_closed(struct ceph_connection *con)
346{
347 int old_state;
348
349 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
350 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
Sage Weilfbb85a42012-06-27 12:31:02 -0700351 old_state != CON_SOCK_STATE_CLOSING &&
Sage Weil8007b8d2012-07-30 18:16:16 -0700352 old_state != CON_SOCK_STATE_CONNECTING &&
353 old_state != CON_SOCK_STATE_CLOSED))
Alex Elderce2c8902012-05-22 22:15:49 -0500354 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700355 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
356 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500357}
Sage Weila922d382010-05-29 09:41:23 -0700358
Sage Weil31b80062009-10-06 11:31:13 -0700359/*
360 * socket callback functions
361 */
362
363/* data available on socket, or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400364static void ceph_sock_data_ready(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700365{
Alex Elderbd406142012-01-23 15:49:27 -0600366 struct ceph_connection *con = sk->sk_user_data;
Guanjun Hea2a32582012-07-08 19:50:33 -0700367 if (atomic_read(&con->msgr->stopping)) {
368 return;
369 }
Alex Elderbd406142012-01-23 15:49:27 -0600370
Sage Weil31b80062009-10-06 11:31:13 -0700371 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500372 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700373 con, con->state);
374 queue_con(con);
375 }
376}
377
378/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500379static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700380{
Alex Elderd3002b92012-02-14 14:05:33 -0600381 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700382
Jim Schutt182fac22012-02-29 08:30:58 -0700383 /* only queue to workqueue if there is data we want to write,
384 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500385 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700386 * doesn't get called again until try_write() fills the socket
387 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
388 * and net/core/stream.c:sk_stream_write_space().
389 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600390 if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
Eric Dumazet64dc6132013-07-22 20:26:31 -0700391 if (sk_stream_is_writeable(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500392 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700393 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
394 queue_con(con);
395 }
Sage Weil31b80062009-10-06 11:31:13 -0700396 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500397 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700398 }
Sage Weil31b80062009-10-06 11:31:13 -0700399}
400
401/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500402static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700403{
Alex Elderbd406142012-01-23 15:49:27 -0600404 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700405
Alex Elder327800b2012-05-22 11:41:43 -0500406 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700407 con, con->state, sk->sk_state);
408
Sage Weil31b80062009-10-06 11:31:13 -0700409 switch (sk->sk_state) {
410 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500411 dout("%s TCP_CLOSE\n", __func__);
Gustavo A. R. Silva18370b32017-10-15 12:55:23 -0500412 /* fall through */
Sage Weil31b80062009-10-06 11:31:13 -0700413 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500414 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500415 con_sock_state_closing(con);
Alex Elderc9ffc772013-02-20 10:25:12 -0600416 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
Alex Elderd65c9e02012-06-20 21:53:53 -0500417 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700418 break;
419 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500420 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500421 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700422 queue_con(con);
423 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600424 default: /* Everything else is uninteresting */
425 break;
Sage Weil31b80062009-10-06 11:31:13 -0700426 }
427}
428
429/*
430 * set up socket callbacks
431 */
432static void set_sock_callbacks(struct socket *sock,
433 struct ceph_connection *con)
434{
435 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600436 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500437 sk->sk_data_ready = ceph_sock_data_ready;
438 sk->sk_write_space = ceph_sock_write_space;
439 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700440}
441
442
443/*
444 * socket helpers
445 */
446
447/*
448 * initiate connection to a remote socket.
449 */
Alex Elder41617d02012-02-14 14:05:33 -0600450static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700451{
Sage Weilf91d3472010-07-01 15:18:31 -0700452 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700453 struct socket *sock;
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100454 unsigned int noio_flag;
Sage Weil31b80062009-10-06 11:31:13 -0700455 int ret;
456
457 BUG_ON(con->sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100458
459 /* sock_create_kern() allocates with GFP_KERNEL */
460 noio_flag = memalloc_noio_save();
Ilya Dryomov757856d2015-06-25 17:47:45 +0300461 ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -0500462 SOCK_STREAM, IPPROTO_TCP, &sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100463 memalloc_noio_restore(noio_flag);
Sage Weil31b80062009-10-06 11:31:13 -0700464 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600465 return ret;
Ilya Dryomov6d7fdb02015-04-02 14:40:58 +0300466 sock->sk->sk_allocation = GFP_NOFS;
Sage Weil31b80062009-10-06 11:31:13 -0700467
Sage Weila6a53492010-04-13 14:07:07 -0700468#ifdef CONFIG_LOCKDEP
469 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
470#endif
471
Sage Weil31b80062009-10-06 11:31:13 -0700472 set_sock_callbacks(sock, con);
473
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700474 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700475
Sage Weil89a86be2012-06-09 14:19:21 -0700476 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700477 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
478 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700479 if (ret == -EINPROGRESS) {
480 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700481 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700482 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600483 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700484 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700485 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700486 sock_release(sock);
Alex Elder41617d02012-02-14 14:05:33 -0600487 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600488 }
Mike Christie89baaa52014-10-16 01:50:19 -0500489
Ilya Dryomov859bff52015-10-28 23:50:58 +0100490 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
Chaitanya Huilgolba988f82015-01-23 16:41:25 +0530491 int optval = 1;
492
493 ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
494 (char *)&optval, sizeof(optval));
495 if (ret)
496 pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
497 ret);
498 }
499
Alex Eldera5bc3122012-01-23 15:49:27 -0600500 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600501 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700502}
503
Ilya Dryomove5c93882018-04-27 18:58:47 +0200504/*
505 * If @buf is NULL, discard up to @len bytes.
506 */
Sage Weil31b80062009-10-06 11:31:13 -0700507static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
508{
509 struct kvec iov = {buf, len};
510 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800511 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700512
Ilya Dryomove5c93882018-04-27 18:58:47 +0200513 if (!buf)
514 msg.msg_flags |= MSG_TRUNC;
515
David Howellsaa563d72018-10-20 00:57:56 +0100516 iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, len);
Al Viro100803a2015-11-15 00:12:48 -0500517 r = sock_recvmsg(sock, &msg, msg.msg_flags);
Sage Weil98bdb0a2011-01-25 08:17:48 -0800518 if (r == -EAGAIN)
519 r = 0;
520 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700521}
522
Alex Elderafb3d902013-03-08 20:58:59 -0600523static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
524 int page_offset, size_t length)
525{
Al Viro100803a2015-11-15 00:12:48 -0500526 struct bio_vec bvec = {
527 .bv_page = page,
528 .bv_offset = page_offset,
529 .bv_len = length
530 };
531 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
532 int r;
Alex Elderafb3d902013-03-08 20:58:59 -0600533
534 BUG_ON(page_offset + length > PAGE_SIZE);
David Howellsaa563d72018-10-20 00:57:56 +0100535 iov_iter_bvec(&msg.msg_iter, READ, &bvec, 1, length);
Al Viro100803a2015-11-15 00:12:48 -0500536 r = sock_recvmsg(sock, &msg, msg.msg_flags);
537 if (r == -EAGAIN)
538 r = 0;
539 return r;
Alex Elderafb3d902013-03-08 20:58:59 -0600540}
541
Sage Weil31b80062009-10-06 11:31:13 -0700542/*
543 * write something. @more is true if caller will be sending more data
544 * shortly.
545 */
546static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
547 size_t kvlen, size_t len, int more)
548{
549 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800550 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700551
552 if (more)
553 msg.msg_flags |= MSG_MORE;
554 else
555 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
556
Sage Weil42961d22011-01-25 08:19:34 -0800557 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
558 if (r == -EAGAIN)
559 r = 0;
560 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700561}
562
Chunwei Chen178eda292014-04-23 12:35:09 +0800563static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
Alex Eldere1dcb122013-03-06 23:39:38 -0600564 int offset, size_t size, bool more)
Alex Elder31739132012-03-07 11:40:08 -0600565{
566 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
567 int ret;
568
569 ret = kernel_sendpage(sock, page, offset, size, flags);
570 if (ret == -EAGAIN)
571 ret = 0;
572
573 return ret;
574}
575
Chunwei Chen178eda292014-04-23 12:35:09 +0800576static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
577 int offset, size_t size, bool more)
578{
Al Viro61ff6e92016-01-09 20:55:45 -0500579 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
580 struct bio_vec bvec;
Chunwei Chen178eda292014-04-23 12:35:09 +0800581 int ret;
Chunwei Chen178eda292014-04-23 12:35:09 +0800582
Ilya Dryomov7e241f62018-11-08 15:55:37 +0100583 /*
584 * sendpage cannot properly handle pages with page_count == 0,
585 * we need to fall back to sendmsg if that's the case.
586 *
587 * Same goes for slab pages: skb_can_coalesce() allows
588 * coalescing neighboring slab objects into a single frag which
589 * triggers one of hardened usercopy checks.
590 */
591 if (page_count(page) >= 1 && !PageSlab(page))
Chunwei Chen178eda292014-04-23 12:35:09 +0800592 return __ceph_tcp_sendpage(sock, page, offset, size, more);
593
Al Viro61ff6e92016-01-09 20:55:45 -0500594 bvec.bv_page = page;
595 bvec.bv_offset = offset;
596 bvec.bv_len = size;
597
598 if (more)
599 msg.msg_flags |= MSG_MORE;
600 else
601 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
602
David Howellsaa563d72018-10-20 00:57:56 +0100603 iov_iter_bvec(&msg.msg_iter, WRITE, &bvec, 1, size);
Al Viro61ff6e92016-01-09 20:55:45 -0500604 ret = sock_sendmsg(sock, &msg);
605 if (ret == -EAGAIN)
606 ret = 0;
Chunwei Chen178eda292014-04-23 12:35:09 +0800607
608 return ret;
609}
Sage Weil31b80062009-10-06 11:31:13 -0700610
611/*
612 * Shutdown/close the socket for the given connection.
613 */
614static int con_close_socket(struct ceph_connection *con)
615{
Sage Weil8007b8d2012-07-30 18:16:16 -0700616 int rc = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700617
618 dout("con_close_socket on %p sock %p\n", con, con->sock);
Sage Weil8007b8d2012-07-30 18:16:16 -0700619 if (con->sock) {
620 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
621 sock_release(con->sock);
622 con->sock = NULL;
623 }
Alex Elder456ea462012-06-20 21:53:53 -0500624
625 /*
Sage Weil4a861692012-07-20 17:29:55 -0700626 * Forcibly clear the SOCK_CLOSED flag. It gets set
Alex Elder456ea462012-06-20 21:53:53 -0500627 * independent of the connection mutex, and we could have
628 * received a socket close event before we had the chance to
629 * shut the socket down.
630 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600631 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
Sage Weil8007b8d2012-07-30 18:16:16 -0700632
Alex Elderce2c8902012-05-22 22:15:49 -0500633 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700634 return rc;
635}
636
637/*
638 * Reset a connection. Discard all incoming and outgoing messages
639 * and clear *_seq state.
640 */
641static void ceph_msg_remove(struct ceph_msg *msg)
642{
643 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500644
Sage Weil31b80062009-10-06 11:31:13 -0700645 ceph_msg_put(msg);
646}
647static void ceph_msg_remove_list(struct list_head *head)
648{
649 while (!list_empty(head)) {
650 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
651 list_head);
652 ceph_msg_remove(msg);
653 }
654}
655
656static void reset_connection(struct ceph_connection *con)
657{
658 /* reset connection, out_queue, msg_ and connect_seq */
659 /* discard existing out_queue and msg_seq */
Sage Weil0fa6ebc2012-12-27 20:27:04 -0600660 dout("reset_connection %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -0700661 ceph_msg_remove_list(&con->out_queue);
662 ceph_msg_remove_list(&con->out_sent);
663
Sage Weilcf3e5c42009-12-11 09:48:05 -0800664 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500665 BUG_ON(con->in_msg->con != con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800666 ceph_msg_put(con->in_msg);
667 con->in_msg = NULL;
668 }
669
Sage Weil31b80062009-10-06 11:31:13 -0700670 con->connect_seq = 0;
671 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800672 if (con->out_msg) {
Ilya Dryomov583d0fe2015-11-02 17:13:58 +0100673 BUG_ON(con->out_msg->con != con);
Sage Weilc86a2932009-12-14 14:04:30 -0800674 ceph_msg_put(con->out_msg);
675 con->out_msg = NULL;
676 }
Sage Weil31b80062009-10-06 11:31:13 -0700677 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700678 con->in_seq_acked = 0;
Ilya Dryomov67645d72015-12-28 13:18:34 +0300679
680 con->out_skip = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700681}
682
683/*
684 * mark a peer down. drop any open connections.
685 */
686void ceph_con_close(struct ceph_connection *con)
687{
Sage Weil8c50c812012-07-30 16:24:37 -0700688 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700689 dout("con_close %p peer %s\n", con,
690 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700691 con->state = CON_STATE_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500692
Alex Elderc9ffc772013-02-20 10:25:12 -0600693 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
694 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
695 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
696 con_flag_clear(con, CON_FLAG_BACKOFF);
Alex Eldera5988c42012-05-29 11:04:58 -0500697
Sage Weil31b80062009-10-06 11:31:13 -0700698 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700699 con->peer_global_seq = 0;
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400700 cancel_con(con);
Sage Weilee76e072012-07-20 16:45:49 -0700701 con_close_socket(con);
Sage Weilec302642009-12-22 10:43:42 -0800702 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700703}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700704EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700705
706/*
Sage Weil31b80062009-10-06 11:31:13 -0700707 * Reopen a closed connection, with a new peer address.
708 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700709void ceph_con_open(struct ceph_connection *con,
710 __u8 entity_type, __u64 entity_num,
711 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700712{
Sage Weil54691552012-07-30 16:21:40 -0700713 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700714 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700715
Alex Elder122070a2012-12-26 10:43:57 -0600716 WARN_ON(con->state != CON_STATE_CLOSED);
Sage Weil8dacc7d2012-07-20 17:24:40 -0700717 con->state = CON_STATE_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500718
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700719 con->peer_name.type = (__u8) entity_type;
720 con->peer_name.num = cpu_to_le64(entity_num);
721
Sage Weil31b80062009-10-06 11:31:13 -0700722 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800723 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700724 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700725 queue_con(con);
726}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700727EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700728
729/*
Sage Weil87b315a2010-03-22 14:51:18 -0700730 * return true if this connection ever successfully opened
731 */
732bool ceph_con_opened(struct ceph_connection *con)
733{
734 return con->connect_seq > 0;
735}
736
737/*
Sage Weil31b80062009-10-06 11:31:13 -0700738 * initialize a new connection.
739 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500740void ceph_con_init(struct ceph_connection *con, void *private,
741 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700742 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700743{
744 dout("con_init %p\n", con);
745 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500746 con->private = private;
747 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700748 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500749
750 con_sock_state_init(con);
751
Sage Weilec302642009-12-22 10:43:42 -0800752 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700753 INIT_LIST_HEAD(&con->out_queue);
754 INIT_LIST_HEAD(&con->out_sent);
Ilya Dryomov68931622015-07-03 15:44:41 +0300755 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
Alex Eldera5988c42012-05-29 11:04:58 -0500756
Sage Weil8dacc7d2012-07-20 17:24:40 -0700757 con->state = CON_STATE_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700758}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700759EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700760
761
762/*
763 * We maintain a global counter to order connection attempts. Get
764 * a unique seq greater than @gt.
765 */
766static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
767{
768 u32 ret;
769
770 spin_lock(&msgr->global_seq_lock);
771 if (msgr->global_seq < gt)
772 msgr->global_seq = gt;
773 ret = ++msgr->global_seq;
774 spin_unlock(&msgr->global_seq_lock);
775 return ret;
776}
777
Alex Eldere2200422012-05-23 14:35:23 -0500778static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600779{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300780 BUG_ON(con->out_skip);
781
Alex Elder859eb792012-02-14 14:05:33 -0600782 con->out_kvec_left = 0;
783 con->out_kvec_bytes = 0;
784 con->out_kvec_cur = &con->out_kvec[0];
785}
786
Alex Eldere2200422012-05-23 14:35:23 -0500787static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600788 size_t size, void *data)
789{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300790 int index = con->out_kvec_left;
Alex Elder859eb792012-02-14 14:05:33 -0600791
Ilya Dryomov67645d72015-12-28 13:18:34 +0300792 BUG_ON(con->out_skip);
Alex Elder859eb792012-02-14 14:05:33 -0600793 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
794
795 con->out_kvec[index].iov_len = size;
796 con->out_kvec[index].iov_base = data;
797 con->out_kvec_left++;
798 con->out_kvec_bytes += size;
799}
Sage Weil31b80062009-10-06 11:31:13 -0700800
Ilya Dryomov67645d72015-12-28 13:18:34 +0300801/*
802 * Chop off a kvec from the end. Return residual number of bytes for
803 * that kvec, i.e. how many bytes would have been written if the kvec
804 * hadn't been nuked.
805 */
806static int con_out_kvec_skip(struct ceph_connection *con)
807{
808 int off = con->out_kvec_cur - con->out_kvec;
809 int skip = 0;
810
811 if (con->out_kvec_bytes > 0) {
812 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
813 BUG_ON(con->out_kvec_bytes < skip);
814 BUG_ON(!con->out_kvec_left);
815 con->out_kvec_bytes -= skip;
816 con->out_kvec_left--;
817 }
818
819 return skip;
820}
821
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500822#ifdef CONFIG_BLOCK
Alex Elder6aaa4512013-03-06 23:39:39 -0600823
824/*
825 * For a bio data item, a piece is whatever remains of the next
826 * entry in the current bio iovec, or the first entry in the next
827 * bio in the list.
828 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500829static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500830 size_t length)
Alex Elder6aaa4512013-03-06 23:39:39 -0600831{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500832 struct ceph_msg_data *data = cursor->data;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100833 struct ceph_bio_iter *it = &cursor->bio_iter;
Alex Elder6aaa4512013-03-06 23:39:39 -0600834
Ilya Dryomov5359a172018-01-20 10:30:10 +0100835 cursor->resid = min_t(size_t, length, data->bio_length);
836 *it = data->bio_pos;
837 if (cursor->resid < it->iter.bi_size)
838 it->iter.bi_size = cursor->resid;
Alex Elder6aaa4512013-03-06 23:39:39 -0600839
Ilya Dryomov5359a172018-01-20 10:30:10 +0100840 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
841 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600842}
843
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500844static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
Alex Elder6aaa4512013-03-06 23:39:39 -0600845 size_t *page_offset,
846 size_t *length)
847{
Ilya Dryomov5359a172018-01-20 10:30:10 +0100848 struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio,
849 cursor->bio_iter.iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600850
Ilya Dryomov5359a172018-01-20 10:30:10 +0100851 *page_offset = bv.bv_offset;
852 *length = bv.bv_len;
853 return bv.bv_page;
Alex Elder6aaa4512013-03-06 23:39:39 -0600854}
855
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500856static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
857 size_t bytes)
Alex Elder6aaa4512013-03-06 23:39:39 -0600858{
Ilya Dryomov5359a172018-01-20 10:30:10 +0100859 struct ceph_bio_iter *it = &cursor->bio_iter;
Alex Elder6aaa4512013-03-06 23:39:39 -0600860
Ilya Dryomov5359a172018-01-20 10:30:10 +0100861 BUG_ON(bytes > cursor->resid);
862 BUG_ON(bytes > bio_iter_len(it->bio, it->iter));
Alex Elder25aff7c2013-03-11 23:34:22 -0500863 cursor->resid -= bytes;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100864 bio_advance_iter(it->bio, &it->iter, bytes);
Kent Overstreetf38a5182013-08-07 14:30:24 -0700865
Ilya Dryomov5359a172018-01-20 10:30:10 +0100866 if (!cursor->resid) {
867 BUG_ON(!cursor->last_piece);
868 return false; /* no more data */
869 }
Kent Overstreetf38a5182013-08-07 14:30:24 -0700870
Ilya Dryomov5359a172018-01-20 10:30:10 +0100871 if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done))
Alex Elder6aaa4512013-03-06 23:39:39 -0600872 return false; /* more bytes to process in this segment */
873
Ilya Dryomov5359a172018-01-20 10:30:10 +0100874 if (!it->iter.bi_size) {
875 it->bio = it->bio->bi_next;
876 it->iter = it->bio->bi_iter;
877 if (cursor->resid < it->iter.bi_size)
878 it->iter.bi_size = cursor->resid;
Alex Elder6aaa4512013-03-06 23:39:39 -0600879 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600880
Ilya Dryomov5359a172018-01-20 10:30:10 +0100881 BUG_ON(cursor->last_piece);
882 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
883 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600884 return true;
885}
Alex Elderea965712013-04-05 14:46:01 -0500886#endif /* CONFIG_BLOCK */
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500887
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100888static void ceph_msg_data_bvecs_cursor_init(struct ceph_msg_data_cursor *cursor,
889 size_t length)
890{
891 struct ceph_msg_data *data = cursor->data;
892 struct bio_vec *bvecs = data->bvec_pos.bvecs;
893
894 cursor->resid = min_t(size_t, length, data->bvec_pos.iter.bi_size);
895 cursor->bvec_iter = data->bvec_pos.iter;
896 cursor->bvec_iter.bi_size = cursor->resid;
897
898 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
899 cursor->last_piece =
900 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
901}
902
903static struct page *ceph_msg_data_bvecs_next(struct ceph_msg_data_cursor *cursor,
904 size_t *page_offset,
905 size_t *length)
906{
907 struct bio_vec bv = bvec_iter_bvec(cursor->data->bvec_pos.bvecs,
908 cursor->bvec_iter);
909
910 *page_offset = bv.bv_offset;
911 *length = bv.bv_len;
912 return bv.bv_page;
913}
914
915static bool ceph_msg_data_bvecs_advance(struct ceph_msg_data_cursor *cursor,
916 size_t bytes)
917{
918 struct bio_vec *bvecs = cursor->data->bvec_pos.bvecs;
919
920 BUG_ON(bytes > cursor->resid);
921 BUG_ON(bytes > bvec_iter_len(bvecs, cursor->bvec_iter));
922 cursor->resid -= bytes;
923 bvec_iter_advance(bvecs, &cursor->bvec_iter, bytes);
924
925 if (!cursor->resid) {
926 BUG_ON(!cursor->last_piece);
927 return false; /* no more data */
928 }
929
930 if (!bytes || cursor->bvec_iter.bi_bvec_done)
931 return false; /* more bytes to process in this segment */
932
933 BUG_ON(cursor->last_piece);
934 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
935 cursor->last_piece =
936 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
937 return true;
938}
939
Alex Elderfe38a2b2013-03-06 23:39:39 -0600940/*
Alex Eldere766d7b2013-03-07 15:38:28 -0600941 * For a page array, a piece comes from the first page in the array
942 * that has not already been fully consumed.
943 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500944static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500945 size_t length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600946{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500947 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600948 int page_count;
949
950 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
951
952 BUG_ON(!data->pages);
953 BUG_ON(!data->length);
954
Alex Elderca8b3a62013-04-05 14:46:01 -0500955 cursor->resid = min(length, data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600956 page_count = calc_pages_for(data->alignment, (u64)data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600957 cursor->page_offset = data->alignment & ~PAGE_MASK;
958 cursor->page_index = 0;
Alex Elder56fc5652013-03-30 23:46:55 -0500959 BUG_ON(page_count > (int)USHRT_MAX);
960 cursor->page_count = (unsigned short)page_count;
961 BUG_ON(length > SIZE_MAX - cursor->page_offset);
Ilya Dryomov5f740d72014-08-08 12:43:39 +0400962 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600963}
964
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500965static struct page *
966ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
967 size_t *page_offset, size_t *length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600968{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500969 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600970
971 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
972
973 BUG_ON(cursor->page_index >= cursor->page_count);
974 BUG_ON(cursor->page_offset >= PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600975
976 *page_offset = cursor->page_offset;
Alex Elder25aff7c2013-03-11 23:34:22 -0500977 if (cursor->last_piece)
Alex Eldere766d7b2013-03-07 15:38:28 -0600978 *length = cursor->resid;
Alex Elder25aff7c2013-03-11 23:34:22 -0500979 else
Alex Eldere766d7b2013-03-07 15:38:28 -0600980 *length = PAGE_SIZE - *page_offset;
Alex Eldere766d7b2013-03-07 15:38:28 -0600981
982 return data->pages[cursor->page_index];
983}
984
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500985static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
Alex Eldere766d7b2013-03-07 15:38:28 -0600986 size_t bytes)
987{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500988 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
Alex Eldere766d7b2013-03-07 15:38:28 -0600989
990 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600991
992 /* Advance the cursor page offset */
993
994 cursor->resid -= bytes;
Alex Elder5df521b2013-03-30 15:09:59 -0500995 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
996 if (!bytes || cursor->page_offset)
Alex Eldere766d7b2013-03-07 15:38:28 -0600997 return false; /* more bytes to process in the current page */
998
Yan, Zhengd90deda2014-03-23 06:50:39 +0800999 if (!cursor->resid)
1000 return false; /* no more data */
1001
Alex Elder5df521b2013-03-30 15:09:59 -05001002 /* Move on to the next page; offset is already at 0 */
Alex Eldere766d7b2013-03-07 15:38:28 -06001003
1004 BUG_ON(cursor->page_index >= cursor->page_count);
Alex Eldere766d7b2013-03-07 15:38:28 -06001005 cursor->page_index++;
Alex Elder25aff7c2013-03-11 23:34:22 -05001006 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -06001007
1008 return true;
1009}
1010
1011/*
Alex Elderdd236fc2013-03-06 23:39:39 -06001012 * For a pagelist, a piece is whatever remains to be consumed in the
1013 * first page in the list, or the front of the next page.
Alex Elderfe38a2b2013-03-06 23:39:39 -06001014 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001015static void
1016ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -05001017 size_t length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001018{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001019 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001020 struct ceph_pagelist *pagelist;
1021 struct page *page;
1022
Alex Elderdd236fc2013-03-06 23:39:39 -06001023 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001024
1025 pagelist = data->pagelist;
1026 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001027
1028 if (!length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001029 return; /* pagelist can be assigned but empty */
1030
1031 BUG_ON(list_empty(&pagelist->head));
1032 page = list_first_entry(&pagelist->head, struct page, lru);
1033
Alex Elderca8b3a62013-04-05 14:46:01 -05001034 cursor->resid = min(length, pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001035 cursor->page = page;
1036 cursor->offset = 0;
Alex Eldera51b272e2013-04-19 15:34:49 -05001037 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001038}
1039
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001040static struct page *
1041ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1042 size_t *page_offset, size_t *length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001043{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001044 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001045 struct ceph_pagelist *pagelist;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001046
1047 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1048
1049 pagelist = data->pagelist;
1050 BUG_ON(!pagelist);
1051
1052 BUG_ON(!cursor->page);
Alex Elder25aff7c2013-03-11 23:34:22 -05001053 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001054
Alex Elder5df521b2013-03-30 15:09:59 -05001055 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001056 *page_offset = cursor->offset & ~PAGE_MASK;
Alex Elder5df521b2013-03-30 15:09:59 -05001057 if (cursor->last_piece)
Alex Elder25aff7c2013-03-11 23:34:22 -05001058 *length = cursor->resid;
1059 else
1060 *length = PAGE_SIZE - *page_offset;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001061
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001062 return cursor->page;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001063}
1064
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001065static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
Alex Elderdd236fc2013-03-06 23:39:39 -06001066 size_t bytes)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001067{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001068 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001069 struct ceph_pagelist *pagelist;
1070
1071 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1072
1073 pagelist = data->pagelist;
1074 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001075
1076 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001077 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1078
1079 /* Advance the cursor offset */
1080
Alex Elder25aff7c2013-03-11 23:34:22 -05001081 cursor->resid -= bytes;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001082 cursor->offset += bytes;
Alex Elder5df521b2013-03-30 15:09:59 -05001083 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001084 if (!bytes || cursor->offset & ~PAGE_MASK)
1085 return false; /* more bytes to process in the current page */
1086
Yan, Zhengd90deda2014-03-23 06:50:39 +08001087 if (!cursor->resid)
1088 return false; /* no more data */
1089
Alex Elderfe38a2b2013-03-06 23:39:39 -06001090 /* Move on to the next page */
1091
1092 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001093 cursor->page = list_next_entry(cursor->page, lru);
Alex Elder25aff7c2013-03-11 23:34:22 -05001094 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001095
1096 return true;
1097}
1098
Alex Elderdd236fc2013-03-06 23:39:39 -06001099/*
1100 * Message data is handled (sent or received) in pieces, where each
1101 * piece resides on a single page. The network layer might not
1102 * consume an entire piece at once. A data item's cursor keeps
1103 * track of which piece is next to process and how much remains to
1104 * be processed in that piece. It also tracks whether the current
1105 * piece is the last one in the data item.
1106 */
Alex Elderca8b3a62013-04-05 14:46:01 -05001107static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
Alex Elderdd236fc2013-03-06 23:39:39 -06001108{
Alex Elderca8b3a62013-04-05 14:46:01 -05001109 size_t length = cursor->total_resid;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001110
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001111 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001112 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001113 ceph_msg_data_pagelist_cursor_init(cursor, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001114 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001115 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001116 ceph_msg_data_pages_cursor_init(cursor, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001117 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001118#ifdef CONFIG_BLOCK
1119 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001120 ceph_msg_data_bio_cursor_init(cursor, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001121 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001122#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001123 case CEPH_MSG_DATA_BVECS:
1124 ceph_msg_data_bvecs_cursor_init(cursor, length);
1125 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001126 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001127 default:
1128 /* BUG(); */
1129 break;
1130 }
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001131 cursor->need_crc = true;
Alex Elderdd236fc2013-03-06 23:39:39 -06001132}
1133
Alex Elderca8b3a62013-04-05 14:46:01 -05001134static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1135{
1136 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Alex Elderca8b3a62013-04-05 14:46:01 -05001137
1138 BUG_ON(!length);
1139 BUG_ON(length > msg->data_length);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001140 BUG_ON(!msg->num_data_items);
Alex Elderca8b3a62013-04-05 14:46:01 -05001141
Alex Elderca8b3a62013-04-05 14:46:01 -05001142 cursor->total_resid = length;
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001143 cursor->data = msg->data;
Alex Elderca8b3a62013-04-05 14:46:01 -05001144
1145 __ceph_msg_data_cursor_init(cursor);
1146}
1147
Alex Elderdd236fc2013-03-06 23:39:39 -06001148/*
1149 * Return the page containing the next piece to process for a given
1150 * data item, and supply the page offset and length of that piece.
1151 * Indicate whether this is the last piece in this data item.
1152 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001153static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1154 size_t *page_offset, size_t *length,
Alex Elderdd236fc2013-03-06 23:39:39 -06001155 bool *last_piece)
1156{
1157 struct page *page;
1158
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001159 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001160 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001161 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001162 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001163 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001164 page = ceph_msg_data_pages_next(cursor, page_offset, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001165 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001166#ifdef CONFIG_BLOCK
1167 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001168 page = ceph_msg_data_bio_next(cursor, page_offset, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001169 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001170#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001171 case CEPH_MSG_DATA_BVECS:
1172 page = ceph_msg_data_bvecs_next(cursor, page_offset, length);
1173 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001174 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001175 default:
1176 page = NULL;
1177 break;
1178 }
Ilya Dryomov5359a172018-01-20 10:30:10 +01001179
Alex Elderdd236fc2013-03-06 23:39:39 -06001180 BUG_ON(!page);
1181 BUG_ON(*page_offset + *length > PAGE_SIZE);
1182 BUG_ON(!*length);
Ilya Dryomov5359a172018-01-20 10:30:10 +01001183 BUG_ON(*length > cursor->resid);
Alex Elderdd236fc2013-03-06 23:39:39 -06001184 if (last_piece)
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001185 *last_piece = cursor->last_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001186
1187 return page;
1188}
1189
1190/*
1191 * Returns true if the result moves the cursor on to the next piece
1192 * of the data item.
1193 */
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001194static void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1195 size_t bytes)
Alex Elderdd236fc2013-03-06 23:39:39 -06001196{
1197 bool new_piece;
1198
Alex Elder25aff7c2013-03-11 23:34:22 -05001199 BUG_ON(bytes > cursor->resid);
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001200 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001201 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001202 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
Alex Elderdd236fc2013-03-06 23:39:39 -06001203 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001204 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001205 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
Alex Eldere766d7b2013-03-07 15:38:28 -06001206 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001207#ifdef CONFIG_BLOCK
1208 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001209 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
Alex Elder6aaa4512013-03-06 23:39:39 -06001210 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001211#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001212 case CEPH_MSG_DATA_BVECS:
1213 new_piece = ceph_msg_data_bvecs_advance(cursor, bytes);
1214 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001215 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001216 default:
1217 BUG();
1218 break;
1219 }
Alex Elderca8b3a62013-04-05 14:46:01 -05001220 cursor->total_resid -= bytes;
Alex Elderdd236fc2013-03-06 23:39:39 -06001221
Alex Elderca8b3a62013-04-05 14:46:01 -05001222 if (!cursor->resid && cursor->total_resid) {
1223 WARN_ON(!cursor->last_piece);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001224 cursor->data++;
Alex Elderca8b3a62013-04-05 14:46:01 -05001225 __ceph_msg_data_cursor_init(cursor);
Alex Eldera51b272e2013-04-19 15:34:49 -05001226 new_piece = true;
Alex Elderca8b3a62013-04-05 14:46:01 -05001227 }
Alex Eldera51b272e2013-04-19 15:34:49 -05001228 cursor->need_crc = new_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001229}
1230
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01001231static size_t sizeof_footer(struct ceph_connection *con)
1232{
1233 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1234 sizeof(struct ceph_msg_footer) :
1235 sizeof(struct ceph_msg_footer_old);
1236}
1237
Alex Elder98fa5dd2013-04-02 12:09:50 -05001238static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
Alex Elder739c9052012-06-11 14:57:13 -05001239{
Alex Elder4c59b4a2013-03-11 23:34:23 -05001240 /* Initialize data cursor */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001241
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001242 ceph_msg_data_cursor_init(msg, (size_t)data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001243}
1244
Sage Weil31b80062009-10-06 11:31:13 -07001245/*
1246 * Prepare footer for currently outgoing message, and finish things
1247 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1248 */
Alex Elder859eb792012-02-14 14:05:33 -06001249static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001250{
1251 struct ceph_msg *m = con->out_msg;
1252
Alex Elderfd154f32012-06-11 14:57:13 -05001253 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1254
Sage Weil31b80062009-10-06 11:31:13 -07001255 dout("prepare_write_message_footer %p\n", con);
Ilya Dryomov89f08172016-02-20 15:56:07 +01001256 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
Yan, Zheng33d07332014-11-04 16:33:37 +08001257 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1258 if (con->ops->sign_message)
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01001259 con->ops->sign_message(m);
Yan, Zheng33d07332014-11-04 16:33:37 +08001260 else
1261 m->footer.sig = 0;
Yan, Zheng33d07332014-11-04 16:33:37 +08001262 } else {
1263 m->old_footer.flags = m->footer.flags;
Yan, Zheng33d07332014-11-04 16:33:37 +08001264 }
Sage Weil31b80062009-10-06 11:31:13 -07001265 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -08001266 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -07001267}
1268
1269/*
1270 * Prepare headers for the next outgoing message.
1271 */
1272static void prepare_write_message(struct ceph_connection *con)
1273{
1274 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -06001275 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001276
Alex Eldere2200422012-05-23 14:35:23 -05001277 con_out_kvec_reset(con);
Sage Weilc86a2932009-12-14 14:04:30 -08001278 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -07001279
1280 /* Sneak an ack in there first? If we can get it into the same
1281 * TCP packet that's a good thing. */
1282 if (con->in_seq > con->in_seq_acked) {
1283 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -05001284 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001285 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001286 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001287 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001288 }
1289
Alex Elder38941f82012-06-01 14:56:43 -05001290 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -06001291 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -08001292 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -05001293 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -07001294
1295 /* put message on sent list */
1296 ceph_msg_get(m);
1297 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -07001298
Sage Weile84346b2010-05-11 21:20:38 -07001299 /*
1300 * only assign outgoing seq # if we haven't sent this message
1301 * yet. if it is requeued, resend with it's original seq.
1302 */
1303 if (m->needs_out_seq) {
1304 m->hdr.seq = cpu_to_le64(++con->out_seq);
1305 m->needs_out_seq = false;
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001306
Ilya Dryomov4690faf2017-07-26 09:59:15 +02001307 if (con->ops->reencode_message)
1308 con->ops->reencode_message(m);
1309 }
Sage Weil31b80062009-10-06 11:31:13 -07001310
Alex Elder98fa5dd2013-04-02 12:09:50 -05001311 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
Sage Weil31b80062009-10-06 11:31:13 -07001312 m, con->out_seq, le16_to_cpu(m->hdr.type),
1313 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
Alex Elder98fa5dd2013-04-02 12:09:50 -05001314 m->data_length);
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001315 WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
1316 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
Sage Weil31b80062009-10-06 11:31:13 -07001317
1318 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -05001319 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001320 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
Alex Eldere2200422012-05-23 14:35:23 -05001321 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -06001322
Sage Weil31b80062009-10-06 11:31:13 -07001323 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -05001324 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -06001325 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -07001326
Ilya Dryomov67645d72015-12-28 13:18:34 +03001327 /* fill in hdr crc and finalize hdr */
Alex Eldera9a0c512012-02-15 07:43:54 -06001328 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1329 con->out_msg->hdr.crc = cpu_to_le32(crc);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001330 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
Alex Eldera9a0c512012-02-15 07:43:54 -06001331
Ilya Dryomov67645d72015-12-28 13:18:34 +03001332 /* fill in front and middle crc, footer */
Alex Eldera9a0c512012-02-15 07:43:54 -06001333 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1334 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1335 if (m->middle) {
1336 crc = crc32c(0, m->middle->vec.iov_base,
1337 m->middle->vec.iov_len);
1338 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1339 } else
Sage Weil31b80062009-10-06 11:31:13 -07001340 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -05001341 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -07001342 le32_to_cpu(con->out_msg->footer.front_crc),
1343 le32_to_cpu(con->out_msg->footer.middle_crc));
Ilya Dryomov67645d72015-12-28 13:18:34 +03001344 con->out_msg->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001345
1346 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -05001347 con->out_msg->footer.data_crc = 0;
Alex Elder98fa5dd2013-04-02 12:09:50 -05001348 if (m->data_length) {
1349 prepare_message_data(con->out_msg, m->data_length);
Alex Elder78625052013-03-06 23:39:39 -06001350 con->out_more = 1; /* data + footer will follow */
1351 } else {
Sage Weil31b80062009-10-06 11:31:13 -07001352 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -06001353 prepare_write_message_footer(con);
Alex Elder78625052013-03-06 23:39:39 -06001354 }
Sage Weil31b80062009-10-06 11:31:13 -07001355
Alex Elderc9ffc772013-02-20 10:25:12 -06001356 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001357}
1358
1359/*
1360 * Prepare an ack.
1361 */
1362static void prepare_write_ack(struct ceph_connection *con)
1363{
1364 dout("prepare_write_ack %p %llu -> %llu\n", con,
1365 con->in_seq_acked, con->in_seq);
1366 con->in_seq_acked = con->in_seq;
1367
Alex Eldere2200422012-05-23 14:35:23 -05001368 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001369
Alex Eldere2200422012-05-23 14:35:23 -05001370 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -06001371
Sage Weil31b80062009-10-06 11:31:13 -07001372 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001373 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001374 &con->out_temp_ack);
1375
Sage Weil31b80062009-10-06 11:31:13 -07001376 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elderc9ffc772013-02-20 10:25:12 -06001377 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001378}
1379
1380/*
Sage Weil3a230832013-03-25 08:47:40 -07001381 * Prepare to share the seq during handshake
1382 */
1383static void prepare_write_seq(struct ceph_connection *con)
1384{
1385 dout("prepare_write_seq %p %llu -> %llu\n", con,
1386 con->in_seq_acked, con->in_seq);
1387 con->in_seq_acked = con->in_seq;
1388
1389 con_out_kvec_reset(con);
1390
1391 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1392 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1393 &con->out_temp_ack);
1394
1395 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1396}
1397
1398/*
Sage Weil31b80062009-10-06 11:31:13 -07001399 * Prepare to write keepalive byte.
1400 */
1401static void prepare_write_keepalive(struct ceph_connection *con)
1402{
1403 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -05001404 con_out_kvec_reset(con);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001405 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001406 struct timespec64 now;
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001407
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001408 ktime_get_real_ts64(&now);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001409 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001410 ceph_encode_timespec64(&con->out_temp_keepalive2, &now);
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001411 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1412 &con->out_temp_keepalive2);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001413 } else {
1414 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1415 }
Alex Elderc9ffc772013-02-20 10:25:12 -06001416 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001417}
1418
1419/*
1420 * Connection negotiation.
1421 */
1422
Ilya Dryomov262614c2018-07-26 15:17:46 +02001423static int get_connect_authorizer(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001424{
Alex Eldera3530df2012-05-16 15:16:39 -05001425 struct ceph_auth_handshake *auth;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001426 int auth_proto;
Alex Elderb1c6b982012-05-16 15:16:38 -05001427
1428 if (!con->ops->get_authorizer) {
Ilya Dryomov262614c2018-07-26 15:17:46 +02001429 con->auth = NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -05001430 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1431 con->out_connect.authorizer_len = 0;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001432 return 0;
Alex Elderb1c6b982012-05-16 15:16:38 -05001433 }
1434
Ilya Dryomov262614c2018-07-26 15:17:46 +02001435 auth = con->ops->get_authorizer(con, &auth_proto, con->auth_retry);
Alex Eldera3530df2012-05-16 15:16:39 -05001436 if (IS_ERR(auth))
Ilya Dryomov262614c2018-07-26 15:17:46 +02001437 return PTR_ERR(auth);
Sage Weil0da5d702011-05-19 11:21:05 -07001438
Ilya Dryomov262614c2018-07-26 15:17:46 +02001439 con->auth = auth;
1440 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
1441 con->out_connect.authorizer_len = cpu_to_le32(auth->authorizer_buf_len);
1442 return 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001443}
1444
Sage Weil31b80062009-10-06 11:31:13 -07001445/*
1446 * We connected to a peer and are saying hello.
1447 */
Alex Eldere825a662012-05-16 15:16:38 -05001448static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001449{
Alex Eldere2200422012-05-23 14:35:23 -05001450 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1451 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -05001452 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -08001453
Sage Weileed0ef22009-11-10 14:34:36 -08001454 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001455 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weileed0ef22009-11-10 14:34:36 -08001456}
1457
Ilya Dryomovc0f56b42018-07-26 17:43:47 +02001458static void __prepare_write_connect(struct ceph_connection *con)
1459{
1460 con_out_kvec_add(con, sizeof(con->out_connect), &con->out_connect);
1461 if (con->auth)
1462 con_out_kvec_add(con, con->auth->authorizer_buf_len,
1463 con->auth->authorizer_buf);
1464
1465 con->out_more = 0;
1466 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1467}
1468
Alex Eldere825a662012-05-16 15:16:38 -05001469static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -08001470{
Eric Dumazet95c96172012-04-15 05:58:06 +00001471 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001472 int proto;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001473 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001474
1475 switch (con->peer_name.type) {
1476 case CEPH_ENTITY_TYPE_MON:
1477 proto = CEPH_MONC_PROTOCOL;
1478 break;
1479 case CEPH_ENTITY_TYPE_OSD:
1480 proto = CEPH_OSDC_PROTOCOL;
1481 break;
1482 case CEPH_ENTITY_TYPE_MDS:
1483 proto = CEPH_MDSC_PROTOCOL;
1484 break;
1485 default:
1486 BUG();
1487 }
1488
1489 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1490 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001491
Ilya Dryomov859bff52015-10-28 23:50:58 +01001492 con->out_connect.features =
1493 cpu_to_le64(from_msgr(con->msgr)->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -07001494 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1495 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1496 con->out_connect.global_seq = cpu_to_le32(global_seq);
1497 con->out_connect.protocol_version = cpu_to_le32(proto);
1498 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001499
Ilya Dryomov262614c2018-07-26 15:17:46 +02001500 ret = get_connect_authorizer(con);
1501 if (ret)
1502 return ret;
Alex Elder3da54772012-05-16 15:16:39 -05001503
Ilya Dryomovc0f56b42018-07-26 17:43:47 +02001504 __prepare_write_connect(con);
Alex Eldere10c7582012-05-16 15:16:38 -05001505 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07001506}
1507
Sage Weil31b80062009-10-06 11:31:13 -07001508/*
1509 * write as much of pending kvecs to the socket as we can.
1510 * 1 -> done
1511 * 0 -> socket full, but more to do
1512 * <0 -> error
1513 */
1514static int write_partial_kvec(struct ceph_connection *con)
1515{
1516 int ret;
1517
1518 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1519 while (con->out_kvec_bytes > 0) {
1520 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1521 con->out_kvec_left, con->out_kvec_bytes,
1522 con->out_more);
1523 if (ret <= 0)
1524 goto out;
1525 con->out_kvec_bytes -= ret;
1526 if (con->out_kvec_bytes == 0)
1527 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -06001528
1529 /* account for full iov entries consumed */
1530 while (ret >= con->out_kvec_cur->iov_len) {
1531 BUG_ON(!con->out_kvec_left);
1532 ret -= con->out_kvec_cur->iov_len;
1533 con->out_kvec_cur++;
1534 con->out_kvec_left--;
1535 }
1536 /* and for a partially-consumed entry */
1537 if (ret) {
1538 con->out_kvec_cur->iov_len -= ret;
1539 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -07001540 }
1541 }
1542 con->out_kvec_left = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001543 ret = 1;
1544out:
1545 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1546 con->out_kvec_bytes, con->out_kvec_left, ret);
1547 return ret; /* done! */
1548}
1549
Alex Elder35b62802013-03-08 20:59:00 -06001550static u32 ceph_crc32c_page(u32 crc, struct page *page,
1551 unsigned int page_offset,
1552 unsigned int length)
1553{
1554 char *kaddr;
1555
1556 kaddr = kmap(page);
1557 BUG_ON(kaddr == NULL);
1558 crc = crc32c(crc, kaddr + page_offset, length);
1559 kunmap(page);
1560
1561 return crc;
1562}
Sage Weil31b80062009-10-06 11:31:13 -07001563/*
1564 * Write as much message data payload as we can. If we finish, queue
1565 * up the footer.
1566 * 1 -> done, footer is now queued in out_kvec[].
1567 * 0 -> socket full, but more to do
1568 * <0 -> error
1569 */
Alex Elder34d2d202013-03-08 20:58:59 -06001570static int write_partial_message_data(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001571{
1572 struct ceph_msg *msg = con->out_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001573 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01001574 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elderf5db90b2013-03-11 23:34:23 -05001575 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001576
Alex Elder859a35d2013-03-11 23:34:23 -05001577 dout("%s %p msg %p\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001578
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001579 if (!msg->num_data_items)
Alex Elder4c59b4a2013-03-11 23:34:23 -05001580 return -EINVAL;
1581
Alex Elder5821bd82012-06-11 14:57:13 -05001582 /*
1583 * Iterate through each page that contains data to be
1584 * written, and send as much as possible for each.
1585 *
1586 * If we are calculating the data crc (the default), we will
1587 * need to map the page. If we have no pages, they have
1588 * been revoked, so use the zero page.
1589 */
Alex Elderf5db90b2013-03-11 23:34:23 -05001590 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
Ilya Dryomov45a267d2018-01-22 15:20:15 +01001591 while (cursor->total_resid) {
Alex Elder8a166d02013-03-08 13:35:36 -06001592 struct page *page;
Alex Eldere387d522013-03-06 23:39:38 -06001593 size_t page_offset;
1594 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05001595 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001596
Ilya Dryomov45a267d2018-01-22 15:20:15 +01001597 if (!cursor->resid) {
1598 ceph_msg_data_advance(cursor, 0);
1599 continue;
1600 }
1601
Ilya Dryomov1f6b8212018-11-14 12:24:01 +01001602 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
1603 ret = ceph_tcp_sendpage(con->sock, page, page_offset, length,
1604 true);
Alex Elderf5db90b2013-03-11 23:34:23 -05001605 if (ret <= 0) {
1606 if (do_datacrc)
1607 msg->footer.data_crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -07001608
Alex Elderf5db90b2013-03-11 23:34:23 -05001609 return ret;
1610 }
Alex Elder143334f2013-03-29 11:44:10 -05001611 if (do_datacrc && cursor->need_crc)
1612 crc = ceph_crc32c_page(crc, page, page_offset, length);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001613 ceph_msg_data_advance(cursor, (size_t)ret);
Sage Weil31b80062009-10-06 11:31:13 -07001614 }
1615
Alex Elder34d2d202013-03-08 20:58:59 -06001616 dout("%s %p msg %p done\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001617
1618 /* prepare and queue up footer, too */
Alex Elderf5db90b2013-03-11 23:34:23 -05001619 if (do_datacrc)
1620 msg->footer.data_crc = cpu_to_le32(crc);
1621 else
Alex Elder84ca8fc2012-06-11 14:57:13 -05001622 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001623 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001624 prepare_write_message_footer(con);
Alex Elderf5db90b2013-03-11 23:34:23 -05001625
1626 return 1; /* must return > 0 to indicate success */
Sage Weil31b80062009-10-06 11:31:13 -07001627}
1628
1629/*
1630 * write some zeros
1631 */
1632static int write_partial_skip(struct ceph_connection *con)
1633{
1634 int ret;
1635
Ilya Dryomov67645d72015-12-28 13:18:34 +03001636 dout("%s %p %d left\n", __func__, con, con->out_skip);
Sage Weil31b80062009-10-06 11:31:13 -07001637 while (con->out_skip > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001638 size_t size = min(con->out_skip, (int) PAGE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001639
Alex Eldere1dcb122013-03-06 23:39:38 -06001640 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
Sage Weil31b80062009-10-06 11:31:13 -07001641 if (ret <= 0)
1642 goto out;
1643 con->out_skip -= ret;
1644 }
1645 ret = 1;
1646out:
1647 return ret;
1648}
1649
1650/*
1651 * Prepare to read connection handshake, or an ack.
1652 */
Sage Weileed0ef22009-11-10 14:34:36 -08001653static void prepare_read_banner(struct ceph_connection *con)
1654{
1655 dout("prepare_read_banner %p\n", con);
1656 con->in_base_pos = 0;
1657}
1658
Sage Weil31b80062009-10-06 11:31:13 -07001659static void prepare_read_connect(struct ceph_connection *con)
1660{
1661 dout("prepare_read_connect %p\n", con);
1662 con->in_base_pos = 0;
1663}
1664
1665static void prepare_read_ack(struct ceph_connection *con)
1666{
1667 dout("prepare_read_ack %p\n", con);
1668 con->in_base_pos = 0;
1669}
1670
Sage Weil3a230832013-03-25 08:47:40 -07001671static void prepare_read_seq(struct ceph_connection *con)
1672{
1673 dout("prepare_read_seq %p\n", con);
1674 con->in_base_pos = 0;
1675 con->in_tag = CEPH_MSGR_TAG_SEQ;
1676}
1677
Sage Weil31b80062009-10-06 11:31:13 -07001678static void prepare_read_tag(struct ceph_connection *con)
1679{
1680 dout("prepare_read_tag %p\n", con);
1681 con->in_base_pos = 0;
1682 con->in_tag = CEPH_MSGR_TAG_READY;
1683}
1684
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001685static void prepare_read_keepalive_ack(struct ceph_connection *con)
1686{
1687 dout("prepare_read_keepalive_ack %p\n", con);
1688 con->in_base_pos = 0;
1689}
1690
Sage Weil31b80062009-10-06 11:31:13 -07001691/*
1692 * Prepare to read a message.
1693 */
1694static int prepare_read_message(struct ceph_connection *con)
1695{
1696 dout("prepare_read_message %p\n", con);
1697 BUG_ON(con->in_msg != NULL);
1698 con->in_base_pos = 0;
1699 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1700 return 0;
1701}
1702
1703
1704static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001705 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001706{
Alex Eldere6cee712012-05-10 10:29:50 -05001707 while (con->in_base_pos < end) {
1708 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001709 int have = size - left;
1710 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1711 if (ret <= 0)
1712 return ret;
1713 con->in_base_pos += ret;
1714 }
1715 return 1;
1716}
1717
1718
1719/*
1720 * Read all or part of the connect-side handshake on a new connection
1721 */
Sage Weileed0ef22009-11-10 14:34:36 -08001722static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001723{
Alex Elderfd516532012-05-10 10:29:50 -05001724 int size;
1725 int end;
1726 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001727
Sage Weileed0ef22009-11-10 14:34:36 -08001728 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001729
1730 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001731 size = strlen(CEPH_BANNER);
1732 end = size;
1733 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001734 if (ret <= 0)
1735 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001736
1737 size = sizeof (con->actual_peer_addr);
1738 end += size;
1739 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001740 if (ret <= 0)
1741 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001742
1743 size = sizeof (con->peer_addr_for_me);
1744 end += size;
1745 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001746 if (ret <= 0)
1747 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001748
Sage Weileed0ef22009-11-10 14:34:36 -08001749out:
1750 return ret;
1751}
1752
1753static int read_partial_connect(struct ceph_connection *con)
1754{
Alex Elderfd516532012-05-10 10:29:50 -05001755 int size;
1756 int end;
1757 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001758
1759 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1760
Alex Elderfd516532012-05-10 10:29:50 -05001761 size = sizeof (con->in_reply);
1762 end = size;
1763 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001764 if (ret <= 0)
1765 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001766
Ilya Dryomov262614c2018-07-26 15:17:46 +02001767 if (con->auth) {
1768 size = le32_to_cpu(con->in_reply.authorizer_len);
Ilya Dryomov130f52f2018-07-27 19:40:30 +02001769 if (size > con->auth->authorizer_reply_buf_len) {
1770 pr_err("authorizer reply too big: %d > %zu\n", size,
1771 con->auth->authorizer_reply_buf_len);
1772 ret = -EINVAL;
1773 goto out;
1774 }
1775
Ilya Dryomov262614c2018-07-26 15:17:46 +02001776 end += size;
1777 ret = read_partial(con, end, size,
1778 con->auth->authorizer_reply_buf);
1779 if (ret <= 0)
1780 goto out;
1781 }
Sage Weil31b80062009-10-06 11:31:13 -07001782
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001783 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1784 con, (int)con->in_reply.tag,
1785 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001786 le32_to_cpu(con->in_reply.global_seq));
1787out:
1788 return ret;
1789}
1790
1791/*
1792 * Verify the hello banner looks okay.
1793 */
1794static int verify_hello(struct ceph_connection *con)
1795{
1796 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001797 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001798 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001799 con->error_msg = "protocol error, bad banner";
1800 return -1;
1801 }
1802 return 0;
1803}
1804
1805static bool addr_is_blank(struct sockaddr_storage *ss)
1806{
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001807 struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1808 struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1809
Sage Weil31b80062009-10-06 11:31:13 -07001810 switch (ss->ss_family) {
1811 case AF_INET:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001812 return addr->s_addr == htonl(INADDR_ANY);
Sage Weil31b80062009-10-06 11:31:13 -07001813 case AF_INET6:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001814 return ipv6_addr_any(addr6);
1815 default:
1816 return true;
Sage Weil31b80062009-10-06 11:31:13 -07001817 }
Sage Weil31b80062009-10-06 11:31:13 -07001818}
1819
1820static int addr_port(struct sockaddr_storage *ss)
1821{
1822 switch (ss->ss_family) {
1823 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001824 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001825 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001826 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001827 }
1828 return 0;
1829}
1830
1831static void addr_set_port(struct sockaddr_storage *ss, int p)
1832{
1833 switch (ss->ss_family) {
1834 case AF_INET:
1835 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001836 break;
Sage Weil31b80062009-10-06 11:31:13 -07001837 case AF_INET6:
1838 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001839 break;
Sage Weil31b80062009-10-06 11:31:13 -07001840 }
1841}
1842
1843/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001844 * Unlike other *_pton function semantics, zero indicates success.
1845 */
1846static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1847 char delim, const char **ipend)
1848{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001849 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1850 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001851
1852 memset(ss, 0, sizeof(*ss));
1853
1854 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1855 ss->ss_family = AF_INET;
1856 return 0;
1857 }
1858
1859 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1860 ss->ss_family = AF_INET6;
1861 return 0;
1862 }
1863
1864 return -EINVAL;
1865}
1866
1867/*
1868 * Extract hostname string and resolve using kernel DNS facility.
1869 */
1870#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1871static int ceph_dns_resolve_name(const char *name, size_t namelen,
1872 struct sockaddr_storage *ss, char delim, const char **ipend)
1873{
1874 const char *end, *delim_p;
1875 char *colon_p, *ip_addr = NULL;
1876 int ip_len, ret;
1877
1878 /*
1879 * The end of the hostname occurs immediately preceding the delimiter or
1880 * the port marker (':') where the delimiter takes precedence.
1881 */
1882 delim_p = memchr(name, delim, namelen);
1883 colon_p = memchr(name, ':', namelen);
1884
1885 if (delim_p && colon_p)
1886 end = delim_p < colon_p ? delim_p : colon_p;
1887 else if (!delim_p && colon_p)
1888 end = colon_p;
1889 else {
1890 end = delim_p;
1891 if (!end) /* case: hostname:/ */
1892 end = name + namelen;
1893 }
1894
1895 if (end <= name)
1896 return -EINVAL;
1897
1898 /* do dns_resolve upcall */
1899 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1900 if (ip_len > 0)
1901 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1902 else
1903 ret = -ESRCH;
1904
1905 kfree(ip_addr);
1906
1907 *ipend = end;
1908
1909 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1910 ret, ret ? "failed" : ceph_pr_addr(ss));
1911
1912 return ret;
1913}
1914#else
1915static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1916 struct sockaddr_storage *ss, char delim, const char **ipend)
1917{
1918 return -EINVAL;
1919}
1920#endif
1921
1922/*
1923 * Parse a server name (IP or hostname). If a valid IP address is not found
1924 * then try to extract a hostname to resolve using userspace DNS upcall.
1925 */
1926static int ceph_parse_server_name(const char *name, size_t namelen,
1927 struct sockaddr_storage *ss, char delim, const char **ipend)
1928{
1929 int ret;
1930
1931 ret = ceph_pton(name, namelen, ss, delim, ipend);
1932 if (ret)
1933 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1934
1935 return ret;
1936}
1937
1938/*
Sage Weil31b80062009-10-06 11:31:13 -07001939 * Parse an ip[:port] list into an addr array. Use the default
1940 * monitor port if a port isn't specified.
1941 */
1942int ceph_parse_ips(const char *c, const char *end,
1943 struct ceph_entity_addr *addr,
1944 int max_count, int *count)
1945{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001946 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001947 const char *p = c;
1948
1949 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1950 for (i = 0; i < max_count; i++) {
1951 const char *ipend;
1952 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001953 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001954 char delim = ',';
1955
1956 if (*p == '[') {
1957 delim = ']';
1958 p++;
1959 }
Sage Weil31b80062009-10-06 11:31:13 -07001960
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001961 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1962 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001963 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001964 ret = -EINVAL;
1965
Sage Weil31b80062009-10-06 11:31:13 -07001966 p = ipend;
1967
Sage Weil39139f62010-07-08 09:54:52 -07001968 if (delim == ']') {
1969 if (*p != ']') {
1970 dout("missing matching ']'\n");
1971 goto bad;
1972 }
1973 p++;
1974 }
1975
Sage Weil31b80062009-10-06 11:31:13 -07001976 /* port? */
1977 if (p < end && *p == ':') {
1978 port = 0;
1979 p++;
1980 while (p < end && *p >= '0' && *p <= '9') {
1981 port = (port * 10) + (*p - '0');
1982 p++;
1983 }
Ilya Dryomovf48db1e2013-12-30 19:21:29 +02001984 if (port == 0)
1985 port = CEPH_MON_PORT;
1986 else if (port > 65535)
Sage Weil31b80062009-10-06 11:31:13 -07001987 goto bad;
1988 } else {
1989 port = CEPH_MON_PORT;
1990 }
1991
1992 addr_set_port(ss, port);
1993
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001994 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001995
1996 if (p == end)
1997 break;
1998 if (*p != ',')
1999 goto bad;
2000 p++;
2001 }
2002
2003 if (p != end)
2004 goto bad;
2005
2006 if (count)
2007 *count = i + 1;
2008 return 0;
2009
2010bad:
Sage Weil39139f62010-07-08 09:54:52 -07002011 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07002012 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002013}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002014EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07002015
Sage Weileed0ef22009-11-10 14:34:36 -08002016static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002017{
Sage Weileed0ef22009-11-10 14:34:36 -08002018 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07002019
2020 if (verify_hello(con) < 0)
2021 return -1;
2022
Sage Weil63f2d212009-11-03 15:17:56 -08002023 ceph_decode_addr(&con->actual_peer_addr);
2024 ceph_decode_addr(&con->peer_addr_for_me);
2025
Sage Weil31b80062009-10-06 11:31:13 -07002026 /*
2027 * Make sure the other end is who we wanted. note that the other
2028 * end may not yet know their ip address, so if it's 0.0.0.0, give
2029 * them the benefit of the doubt.
2030 */
Sage Weil103e2d32010-01-07 16:12:36 -08002031 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2032 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07002033 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
2034 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Joe Perchesb9a67892014-09-09 21:17:29 -07002035 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
2036 ceph_pr_addr(&con->peer_addr.in_addr),
2037 (int)le32_to_cpu(con->peer_addr.nonce),
2038 ceph_pr_addr(&con->actual_peer_addr.in_addr),
2039 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08002040 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07002041 return -1;
2042 }
2043
2044 /*
2045 * did we learn our address?
2046 */
2047 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2048 int port = addr_port(&con->msgr->inst.addr.in_addr);
2049
2050 memcpy(&con->msgr->inst.addr.in_addr,
2051 &con->peer_addr_for_me.in_addr,
2052 sizeof(con->peer_addr_for_me.in_addr));
2053 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08002054 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08002055 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002056 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002057 }
2058
Sage Weileed0ef22009-11-10 14:34:36 -08002059 return 0;
2060}
2061
2062static int process_connect(struct ceph_connection *con)
2063{
Ilya Dryomov859bff52015-10-28 23:50:58 +01002064 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2065 u64 req_feat = from_msgr(con->msgr)->required_features;
Ilya Dryomovdcbbd972017-06-05 14:44:59 +02002066 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07002067 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08002068
Sage Weileed0ef22009-11-10 14:34:36 -08002069 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2070
Ilya Dryomov262614c2018-07-26 15:17:46 +02002071 if (con->auth) {
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002072 /*
2073 * Any connection that defines ->get_authorizer()
Ilya Dryomov6daca132018-07-27 19:18:34 +02002074 * should also define ->add_authorizer_challenge() and
2075 * ->verify_authorizer_reply().
2076 *
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002077 * See get_connect_authorizer().
2078 */
Ilya Dryomov6daca132018-07-27 19:18:34 +02002079 if (con->in_reply.tag == CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER) {
2080 ret = con->ops->add_authorizer_challenge(
2081 con, con->auth->authorizer_reply_buf,
2082 le32_to_cpu(con->in_reply.authorizer_len));
2083 if (ret < 0)
2084 return ret;
2085
2086 con_out_kvec_reset(con);
2087 __prepare_write_connect(con);
2088 prepare_read_connect(con);
2089 return 0;
2090 }
2091
Ilya Dryomov0dde5842016-12-02 16:35:09 +01002092 ret = con->ops->verify_authorizer_reply(con);
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002093 if (ret < 0) {
2094 con->error_msg = "bad authorize reply";
2095 return ret;
2096 }
2097 }
2098
Sage Weil31b80062009-10-06 11:31:13 -07002099 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08002100 case CEPH_MSGR_TAG_FEATURES:
2101 pr_err("%s%lld %s feature set mismatch,"
2102 " my %llx < server's %llx, missing %llx\n",
2103 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002104 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002105 sup_feat, server_feat, server_feat & ~sup_feat);
2106 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002107 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002108 return -1;
2109
Sage Weil31b80062009-10-06 11:31:13 -07002110 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07002111 pr_err("%s%lld %s protocol version mismatch,"
2112 " my %d != server's %d\n",
2113 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002114 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07002115 le32_to_cpu(con->out_connect.protocol_version),
2116 le32_to_cpu(con->in_reply.protocol_version));
2117 con->error_msg = "protocol version mismatch";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002118 reset_connection(con);
Sage Weil31b80062009-10-06 11:31:13 -07002119 return -1;
2120
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002121 case CEPH_MSGR_TAG_BADAUTHORIZER:
2122 con->auth_retry++;
2123 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2124 con->auth_retry);
2125 if (con->auth_retry == 2) {
2126 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002127 return -1;
2128 }
Jim Schutt6d4221b2012-08-10 10:37:38 -07002129 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002130 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002131 if (ret < 0)
2132 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07002133 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002134 break;
Sage Weil31b80062009-10-06 11:31:13 -07002135
2136 case CEPH_MSGR_TAG_RESETSESSION:
2137 /*
2138 * If we connected with a large connect_seq but the peer
2139 * has no record of a session with us (no connection, or
2140 * connect_seq == 0), they will send RESETSESION to indicate
2141 * that they must have reset their session, and may have
2142 * dropped messages.
2143 */
2144 dout("process_connect got RESET peer seq %u\n",
Sage Weil5bdca4e2012-07-10 11:53:34 -07002145 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002146 pr_err("%s%lld %s connection reset\n",
2147 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002148 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002149 reset_connection(con);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002150 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002151 ret = prepare_write_connect(con);
2152 if (ret < 0)
2153 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002154 prepare_read_connect(con);
2155
2156 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08002157 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002158 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2159 if (con->ops->peer_reset)
2160 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08002161 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002162 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07002163 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07002164 break;
2165
2166 case CEPH_MSGR_TAG_RETRY_SESSION:
2167 /*
2168 * If we sent a smaller connect_seq than the peer has, try
2169 * again with a larger value.
2170 */
Sage Weil5bdca4e2012-07-10 11:53:34 -07002171 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002172 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil5bdca4e2012-07-10 11:53:34 -07002173 le32_to_cpu(con->in_reply.connect_seq));
2174 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002175 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002176 ret = prepare_write_connect(con);
2177 if (ret < 0)
2178 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002179 prepare_read_connect(con);
2180 break;
2181
2182 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2183 /*
2184 * If we sent a smaller global_seq than the peer has, try
2185 * again with a larger value.
2186 */
Sage Weileed0ef22009-11-10 14:34:36 -08002187 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002188 con->peer_global_seq,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002189 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002190 get_global_seq(con->msgr,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002191 le32_to_cpu(con->in_reply.global_seq));
Jim Schutt6d4221b2012-08-10 10:37:38 -07002192 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002193 ret = prepare_write_connect(con);
2194 if (ret < 0)
2195 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002196 prepare_read_connect(con);
2197 break;
2198
Sage Weil3a230832013-03-25 08:47:40 -07002199 case CEPH_MSGR_TAG_SEQ:
Sage Weil31b80062009-10-06 11:31:13 -07002200 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08002201 if (req_feat & ~server_feat) {
2202 pr_err("%s%lld %s protocol feature mismatch,"
2203 " my required %llx > server's %llx, need %llx\n",
2204 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002205 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002206 req_feat, server_feat, req_feat & ~server_feat);
2207 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002208 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002209 return -1;
2210 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002211
Alex Elder122070a2012-12-26 10:43:57 -06002212 WARN_ON(con->state != CON_STATE_NEGOTIATING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002213 con->state = CON_STATE_OPEN;
Sage Weil20e55c42013-03-25 09:30:13 -07002214 con->auth_retry = 0; /* we authenticated; clear flag */
Sage Weil31b80062009-10-06 11:31:13 -07002215 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2216 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07002217 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07002218 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2219 con->peer_global_seq,
2220 le32_to_cpu(con->in_reply.connect_seq),
2221 con->connect_seq);
2222 WARN_ON(con->connect_seq !=
2223 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08002224
2225 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elderc9ffc772013-02-20 10:25:12 -06002226 con_flag_set(con, CON_FLAG_LOSSYTX);
Sage Weil92ac41d2009-12-14 14:56:56 -08002227
Sage Weil85effe12012-07-30 16:22:05 -07002228 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -07002229
Sage Weil3a230832013-03-25 08:47:40 -07002230 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2231 prepare_write_seq(con);
2232 prepare_read_seq(con);
2233 } else {
2234 prepare_read_tag(con);
2235 }
Sage Weil31b80062009-10-06 11:31:13 -07002236 break;
2237
2238 case CEPH_MSGR_TAG_WAIT:
2239 /*
2240 * If there is a connection race (we are opening
2241 * connections to each other), one of us may just have
2242 * to WAIT. This shouldn't happen if we are the
2243 * client.
2244 */
Sage Weil04177882011-05-12 15:33:17 -07002245 con->error_msg = "protocol error, got WAIT as client";
2246 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07002247
2248 default:
Sage Weil31b80062009-10-06 11:31:13 -07002249 con->error_msg = "protocol error, garbage tag during connect";
2250 return -1;
2251 }
2252 return 0;
2253}
2254
2255
2256/*
2257 * read (part of) an ack
2258 */
2259static int read_partial_ack(struct ceph_connection *con)
2260{
Alex Elderfd516532012-05-10 10:29:50 -05002261 int size = sizeof (con->in_temp_ack);
2262 int end = size;
Sage Weil31b80062009-10-06 11:31:13 -07002263
Alex Elderfd516532012-05-10 10:29:50 -05002264 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07002265}
2266
Sage Weil31b80062009-10-06 11:31:13 -07002267/*
2268 * We can finally discard anything that's been acked.
2269 */
2270static void process_ack(struct ceph_connection *con)
2271{
2272 struct ceph_msg *m;
2273 u64 ack = le64_to_cpu(con->in_temp_ack);
2274 u64 seq;
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002275 bool reconnect = (con->in_tag == CEPH_MSGR_TAG_SEQ);
2276 struct list_head *list = reconnect ? &con->out_queue : &con->out_sent;
Sage Weil31b80062009-10-06 11:31:13 -07002277
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002278 /*
2279 * In the reconnect case, con_fault() has requeued messages
2280 * in out_sent. We should cleanup old messages according to
2281 * the reconnect seq.
2282 */
2283 while (!list_empty(list)) {
2284 m = list_first_entry(list, struct ceph_msg, list_head);
2285 if (reconnect && m->needs_out_seq)
2286 break;
Sage Weil31b80062009-10-06 11:31:13 -07002287 seq = le64_to_cpu(m->hdr.seq);
2288 if (seq > ack)
2289 break;
2290 dout("got ack for seq %llu type %d at %p\n", seq,
2291 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07002292 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07002293 ceph_msg_remove(m);
2294 }
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002295
Sage Weil31b80062009-10-06 11:31:13 -07002296 prepare_read_tag(con);
2297}
2298
2299
Yehuda Sadeh24504182010-01-08 13:58:34 -08002300static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002301 struct kvec *section,
2302 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002303{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002304 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07002305
Yehuda Sadeh24504182010-01-08 13:58:34 -08002306 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07002307
Yehuda Sadeh24504182010-01-08 13:58:34 -08002308 while (section->iov_len < sec_len) {
2309 BUG_ON(section->iov_base == NULL);
2310 left = sec_len - section->iov_len;
2311 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2312 section->iov_len, left);
2313 if (ret <= 0)
2314 return ret;
2315 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002316 }
Alex Elderfe3ad592012-02-15 07:43:54 -06002317 if (section->iov_len == sec_len)
2318 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002319
2320 return 1;
2321}
2322
Alex Elder34d2d202013-03-08 20:58:59 -06002323static int read_partial_msg_data(struct ceph_connection *con)
2324{
2325 struct ceph_msg *msg = con->in_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05002326 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002327 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elder686be202013-03-11 23:34:23 -05002328 struct page *page;
2329 size_t page_offset;
2330 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05002331 u32 crc = 0;
Alex Elder34d2d202013-03-08 20:58:59 -06002332 int ret;
2333
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002334 if (!msg->num_data_items)
Alex Elder4c59b4a2013-03-11 23:34:23 -05002335 return -EIO;
Alex Elder34d2d202013-03-08 20:58:59 -06002336
Alex Elderf5db90b2013-03-11 23:34:23 -05002337 if (do_datacrc)
2338 crc = con->in_data_crc;
Ilya Dryomov45a267d2018-01-22 15:20:15 +01002339 while (cursor->total_resid) {
2340 if (!cursor->resid) {
2341 ceph_msg_data_advance(cursor, 0);
2342 continue;
2343 }
2344
Shraddha Barke343128c2015-10-19 21:59:00 +05302345 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Alex Elder686be202013-03-11 23:34:23 -05002346 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
Alex Elderf5db90b2013-03-11 23:34:23 -05002347 if (ret <= 0) {
2348 if (do_datacrc)
2349 con->in_data_crc = crc;
2350
Alex Elder686be202013-03-11 23:34:23 -05002351 return ret;
Alex Elderf5db90b2013-03-11 23:34:23 -05002352 }
Alex Elder686be202013-03-11 23:34:23 -05002353
2354 if (do_datacrc)
Alex Elderf5db90b2013-03-11 23:34:23 -05002355 crc = ceph_crc32c_page(crc, page, page_offset, ret);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02002356 ceph_msg_data_advance(cursor, (size_t)ret);
Alex Elder34d2d202013-03-08 20:58:59 -06002357 }
Alex Elderf5db90b2013-03-11 23:34:23 -05002358 if (do_datacrc)
2359 con->in_data_crc = crc;
Alex Elder34d2d202013-03-08 20:58:59 -06002360
2361 return 1; /* must return > 0 to indicate success */
2362}
2363
Sage Weil31b80062009-10-06 11:31:13 -07002364/*
2365 * read (part of) a message.
2366 */
Alex Elder686be202013-03-11 23:34:23 -05002367static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2368
Sage Weil31b80062009-10-06 11:31:13 -07002369static int read_partial_message(struct ceph_connection *con)
2370{
2371 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05002372 int size;
2373 int end;
Sage Weil31b80062009-10-06 11:31:13 -07002374 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00002375 unsigned int front_len, middle_len, data_len;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002376 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Yan, Zheng33d07332014-11-04 16:33:37 +08002377 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
Sage Weilae187562010-04-22 07:47:01 -07002378 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06002379 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07002380
2381 dout("read_partial_message con %p msg %p\n", con, m);
2382
2383 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05002384 size = sizeof (con->in_hdr);
2385 end = size;
2386 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05002387 if (ret <= 0)
2388 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06002389
2390 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2391 if (cpu_to_le32(crc) != con->in_hdr.crc) {
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002392 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
Alex Elderfe3ad592012-02-15 07:43:54 -06002393 crc, con->in_hdr.crc);
2394 return -EBADMSG;
2395 }
2396
Sage Weil31b80062009-10-06 11:31:13 -07002397 front_len = le32_to_cpu(con->in_hdr.front_len);
2398 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2399 return -EIO;
2400 middle_len = le32_to_cpu(con->in_hdr.middle_len);
Alex Elder7b11ba32013-03-08 18:51:03 -06002401 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
Sage Weil31b80062009-10-06 11:31:13 -07002402 return -EIO;
2403 data_len = le32_to_cpu(con->in_hdr.data_len);
2404 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2405 return -EIO;
2406
Sage Weilae187562010-04-22 07:47:01 -07002407 /* verify seq# */
2408 seq = le64_to_cpu(con->in_hdr.seq);
2409 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07002410 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07002411 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002412 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07002413 seq, con->in_seq + 1);
2414 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002415 sizeof_footer(con);
Sage Weilae187562010-04-22 07:47:01 -07002416 con->in_tag = CEPH_MSGR_TAG_READY;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002417 return 1;
Sage Weilae187562010-04-22 07:47:01 -07002418 } else if ((s64)seq - (s64)con->in_seq > 1) {
2419 pr_err("read_partial_message bad seq %lld expected %lld\n",
2420 seq, con->in_seq + 1);
2421 con->error_msg = "bad message sequence # for incoming message";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002422 return -EBADE;
Sage Weilae187562010-04-22 07:47:01 -07002423 }
2424
Sage Weil31b80062009-10-06 11:31:13 -07002425 /* allocate message? */
2426 if (!con->in_msg) {
Sage Weil4740a622012-07-30 18:19:30 -07002427 int skip = 0;
2428
Sage Weil31b80062009-10-06 11:31:13 -07002429 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
Alex Elder6ebc8b32013-03-08 18:51:03 -06002430 front_len, data_len);
Sage Weil4740a622012-07-30 18:19:30 -07002431 ret = ceph_con_in_msg_alloc(con, &skip);
2432 if (ret < 0)
2433 return ret;
Alex Elderf759ebb2013-04-05 14:46:01 -05002434
2435 BUG_ON(!con->in_msg ^ skip);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002436 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07002437 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07002438 dout("alloc_msg said skip message\n");
Sage Weil31b80062009-10-06 11:31:13 -07002439 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002440 sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07002441 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002442 con->in_seq++;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002443 return 1;
Sage Weil31b80062009-10-06 11:31:13 -07002444 }
Alex Elder38941f82012-06-01 14:56:43 -05002445
Sage Weil4740a622012-07-30 18:19:30 -07002446 BUG_ON(!con->in_msg);
Alex Elder38941f82012-06-01 14:56:43 -05002447 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002448 m = con->in_msg;
2449 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002450 if (m->middle)
2451 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002452
Alex Elder78625052013-03-06 23:39:39 -06002453 /* prepare for data payload, if any */
Sage Weila4107022012-07-30 16:20:25 -07002454
Alex Elder78625052013-03-06 23:39:39 -06002455 if (data_len)
Alex Elder98fa5dd2013-04-02 12:09:50 -05002456 prepare_message_data(con->in_msg, data_len);
Sage Weil31b80062009-10-06 11:31:13 -07002457 }
2458
2459 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002460 ret = read_partial_message_section(con, &m->front, front_len,
2461 &con->in_front_crc);
2462 if (ret <= 0)
2463 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002464
2465 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002466 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07002467 ret = read_partial_message_section(con, &m->middle->vec,
2468 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002469 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07002470 if (ret <= 0)
2471 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002472 }
2473
2474 /* (page) data */
Alex Elder34d2d202013-03-08 20:58:59 -06002475 if (data_len) {
2476 ret = read_partial_msg_data(con);
2477 if (ret <= 0)
2478 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002479 }
2480
Sage Weil31b80062009-10-06 11:31:13 -07002481 /* footer */
Ilya Dryomov89f08172016-02-20 15:56:07 +01002482 size = sizeof_footer(con);
Alex Elderfd516532012-05-10 10:29:50 -05002483 end += size;
2484 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05002485 if (ret <= 0)
2486 return ret;
2487
Yan, Zheng33d07332014-11-04 16:33:37 +08002488 if (!need_sign) {
2489 m->footer.flags = m->old_footer.flags;
2490 m->footer.sig = 0;
2491 }
2492
Sage Weil31b80062009-10-06 11:31:13 -07002493 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2494 m, front_len, m->footer.front_crc, middle_len,
2495 m->footer.middle_crc, data_len, m->footer.data_crc);
2496
2497 /* crc ok? */
2498 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2499 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2500 m, con->in_front_crc, m->footer.front_crc);
2501 return -EBADMSG;
2502 }
2503 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2504 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2505 m, con->in_middle_crc, m->footer.middle_crc);
2506 return -EBADMSG;
2507 }
Alex Elderbca064d2012-02-15 07:43:54 -06002508 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07002509 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2510 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2511 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2512 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2513 return -EBADMSG;
2514 }
2515
Yan, Zheng33d07332014-11-04 16:33:37 +08002516 if (need_sign && con->ops->check_message_signature &&
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002517 con->ops->check_message_signature(m)) {
Yan, Zheng33d07332014-11-04 16:33:37 +08002518 pr_err("read_partial_message %p signature check failed\n", m);
2519 return -EBADMSG;
2520 }
2521
Sage Weil31b80062009-10-06 11:31:13 -07002522 return 1; /* done! */
2523}
2524
2525/*
2526 * Process message. This happens in the worker thread. The callback should
2527 * be careful not to do anything that waits on other incoming messages or it
2528 * may deadlock.
2529 */
2530static void process_message(struct ceph_connection *con)
2531{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002532 struct ceph_msg *msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07002533
Alex Elder38941f82012-06-01 14:56:43 -05002534 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002535 con->in_msg = NULL;
2536
2537 /* if first message, set peer_name */
2538 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07002539 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07002540
Sage Weil31b80062009-10-06 11:31:13 -07002541 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08002542 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002543
2544 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2545 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07002546 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07002547 le16_to_cpu(msg->hdr.type),
2548 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2549 le32_to_cpu(msg->hdr.front_len),
2550 le32_to_cpu(msg->hdr.data_len),
2551 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2552 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08002553
2554 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002555}
2556
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002557static int read_keepalive_ack(struct ceph_connection *con)
2558{
2559 struct ceph_timespec ceph_ts;
2560 size_t size = sizeof(ceph_ts);
2561 int ret = read_partial(con, size, size, &ceph_ts);
2562 if (ret <= 0)
2563 return ret;
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02002564 ceph_decode_timespec64(&con->last_keepalive_ack, &ceph_ts);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002565 prepare_read_tag(con);
2566 return 1;
2567}
Sage Weil31b80062009-10-06 11:31:13 -07002568
2569/*
2570 * Write something to the socket. Called in a worker thread when the
2571 * socket appears to be writeable and we have something ready to send.
2572 */
2573static int try_write(struct ceph_connection *con)
2574{
Sage Weil31b80062009-10-06 11:31:13 -07002575 int ret = 1;
2576
Sage Weild59315c2012-06-21 12:49:23 -07002577 dout("try_write start %p state %lu\n", con, con->state);
Ilya Dryomov9c55ad12018-04-24 19:10:55 +02002578 if (con->state != CON_STATE_PREOPEN &&
2579 con->state != CON_STATE_CONNECTING &&
2580 con->state != CON_STATE_NEGOTIATING &&
2581 con->state != CON_STATE_OPEN)
2582 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07002583
Sage Weil31b80062009-10-06 11:31:13 -07002584 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002585 if (con->state == CON_STATE_PREOPEN) {
2586 BUG_ON(con->sock);
2587 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002588
Alex Eldere2200422012-05-23 14:35:23 -05002589 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002590 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002591 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002592
Sage Weilcf3e5c42009-12-11 09:48:05 -08002593 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002594 con->in_tag = CEPH_MSGR_TAG_READY;
2595 dout("try_write initiating connect on %p new state %lu\n",
2596 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002597 ret = ceph_tcp_connect(con);
2598 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002599 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002600 goto out;
2601 }
2602 }
2603
Ilya Dryomovd2935d62018-04-25 12:17:13 +02002604more:
2605 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
Ilya Dryomov9c55ad12018-04-24 19:10:55 +02002606 BUG_ON(!con->sock);
2607
Sage Weil31b80062009-10-06 11:31:13 -07002608 /* kvec data queued? */
Ilya Dryomov67645d72015-12-28 13:18:34 +03002609 if (con->out_kvec_left) {
2610 ret = write_partial_kvec(con);
Sage Weil31b80062009-10-06 11:31:13 -07002611 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002612 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002613 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03002614 if (con->out_skip) {
2615 ret = write_partial_skip(con);
Sage Weil31b80062009-10-06 11:31:13 -07002616 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002617 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002618 }
2619
2620 /* msg pages? */
2621 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002622 if (con->out_msg_done) {
2623 ceph_msg_put(con->out_msg);
2624 con->out_msg = NULL; /* we're done with this one */
2625 goto do_next;
2626 }
2627
Alex Elder34d2d202013-03-08 20:58:59 -06002628 ret = write_partial_message_data(con);
Sage Weil31b80062009-10-06 11:31:13 -07002629 if (ret == 1)
Ilya Dryomovd2935d62018-04-25 12:17:13 +02002630 goto more; /* we need to send the footer, too! */
Sage Weil31b80062009-10-06 11:31:13 -07002631 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002632 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002633 if (ret < 0) {
Alex Elder34d2d202013-03-08 20:58:59 -06002634 dout("try_write write_partial_message_data err %d\n",
Sage Weil31b80062009-10-06 11:31:13 -07002635 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002636 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002637 }
2638 }
2639
Sage Weilc86a2932009-12-14 14:04:30 -08002640do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002641 if (con->state == CON_STATE_OPEN) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002642 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2643 prepare_write_keepalive(con);
2644 goto more;
2645 }
Sage Weil31b80062009-10-06 11:31:13 -07002646 /* is anything else pending? */
2647 if (!list_empty(&con->out_queue)) {
2648 prepare_write_message(con);
2649 goto more;
2650 }
2651 if (con->in_seq > con->in_seq_acked) {
2652 prepare_write_ack(con);
2653 goto more;
2654 }
Sage Weil31b80062009-10-06 11:31:13 -07002655 }
2656
2657 /* Nothing to do! */
Alex Elderc9ffc772013-02-20 10:25:12 -06002658 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07002659 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002660 ret = 0;
2661out:
Sage Weil42961d22011-01-25 08:19:34 -08002662 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002663 return ret;
2664}
2665
Sage Weil31b80062009-10-06 11:31:13 -07002666/*
2667 * Read what we can from the socket.
2668 */
2669static int try_read(struct ceph_connection *con)
2670{
Sage Weil31b80062009-10-06 11:31:13 -07002671 int ret = -1;
2672
Sage Weil31b80062009-10-06 11:31:13 -07002673more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002674 dout("try_read start on %p state %lu\n", con, con->state);
2675 if (con->state != CON_STATE_CONNECTING &&
2676 con->state != CON_STATE_NEGOTIATING &&
2677 con->state != CON_STATE_OPEN)
2678 return 0;
2679
2680 BUG_ON(!con->sock);
2681
Sage Weil31b80062009-10-06 11:31:13 -07002682 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2683 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002684
Sage Weil8dacc7d2012-07-20 17:24:40 -07002685 if (con->state == CON_STATE_CONNECTING) {
Alex Elder7593af92012-05-24 11:55:03 -05002686 dout("try_read connecting\n");
2687 ret = read_partial_banner(con);
2688 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002689 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002690 ret = process_banner(con);
2691 if (ret < 0)
2692 goto out;
2693
Sage Weil8dacc7d2012-07-20 17:24:40 -07002694 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002695
Jim Schutt6d4221b2012-08-10 10:37:38 -07002696 /*
2697 * Received banner is good, exchange connection info.
2698 * Do not reset out_kvec, as sending our banner raced
2699 * with receiving peer banner after connect completed.
2700 */
Alex Elder7593af92012-05-24 11:55:03 -05002701 ret = prepare_write_connect(con);
2702 if (ret < 0)
2703 goto out;
2704 prepare_read_connect(con);
2705
2706 /* Send connection info before awaiting response */
Sage Weil0da5d702011-05-19 11:21:05 -07002707 goto out;
2708 }
2709
Sage Weil8dacc7d2012-07-20 17:24:40 -07002710 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002711 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002712 ret = read_partial_connect(con);
2713 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002714 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002715 ret = process_connect(con);
2716 if (ret < 0)
2717 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002718 goto more;
2719 }
2720
Alex Elder122070a2012-12-26 10:43:57 -06002721 WARN_ON(con->state != CON_STATE_OPEN);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002722
Sage Weil31b80062009-10-06 11:31:13 -07002723 if (con->in_base_pos < 0) {
2724 /*
2725 * skipping + discarding content.
Sage Weil31b80062009-10-06 11:31:13 -07002726 */
Ilya Dryomove5c93882018-04-27 18:58:47 +02002727 ret = ceph_tcp_recvmsg(con->sock, NULL, -con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07002728 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002729 goto out;
Ilya Dryomove5c93882018-04-27 18:58:47 +02002730 dout("skipped %d / %d bytes\n", ret, -con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07002731 con->in_base_pos += ret;
2732 if (con->in_base_pos)
2733 goto more;
2734 }
2735 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2736 /*
2737 * what's next?
2738 */
2739 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2740 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002741 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002742 dout("try_read got tag %d\n", (int)con->in_tag);
2743 switch (con->in_tag) {
2744 case CEPH_MSGR_TAG_MSG:
2745 prepare_read_message(con);
2746 break;
2747 case CEPH_MSGR_TAG_ACK:
2748 prepare_read_ack(con);
2749 break;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002750 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2751 prepare_read_keepalive_ack(con);
2752 break;
Sage Weil31b80062009-10-06 11:31:13 -07002753 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002754 con_close_socket(con);
2755 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002756 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002757 default:
2758 goto bad_tag;
2759 }
2760 }
2761 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2762 ret = read_partial_message(con);
2763 if (ret <= 0) {
2764 switch (ret) {
2765 case -EBADMSG:
Ilya Dryomova51983e2015-10-28 23:52:06 +01002766 con->error_msg = "bad crc/signature";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002767 /* fall through */
2768 case -EBADE:
Sage Weil31b80062009-10-06 11:31:13 -07002769 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002770 break;
Sage Weil31b80062009-10-06 11:31:13 -07002771 case -EIO:
2772 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002773 break;
Sage Weil31b80062009-10-06 11:31:13 -07002774 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002775 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002776 }
2777 if (con->in_tag == CEPH_MSGR_TAG_READY)
2778 goto more;
2779 process_message(con);
Sage Weil7b862e02012-07-30 18:16:56 -07002780 if (con->state == CON_STATE_OPEN)
2781 prepare_read_tag(con);
Sage Weil31b80062009-10-06 11:31:13 -07002782 goto more;
2783 }
Sage Weil3a230832013-03-25 08:47:40 -07002784 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2785 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2786 /*
2787 * the final handshake seq exchange is semantically
2788 * equivalent to an ACK
2789 */
Sage Weil31b80062009-10-06 11:31:13 -07002790 ret = read_partial_ack(con);
2791 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002792 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002793 process_ack(con);
2794 goto more;
2795 }
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002796 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2797 ret = read_keepalive_ack(con);
2798 if (ret <= 0)
2799 goto out;
2800 goto more;
2801 }
Sage Weil31b80062009-10-06 11:31:13 -07002802
Sage Weil31b80062009-10-06 11:31:13 -07002803out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002804 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002805 return ret;
2806
2807bad_tag:
2808 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2809 con->error_msg = "protocol error, garbage tag";
2810 ret = -1;
2811 goto out;
2812}
2813
2814
2815/*
Alex Elder802c6d92012-10-08 20:37:30 -07002816 * Atomically queue work on a connection after the specified delay.
2817 * Bump @con reference to avoid races with connection teardown.
2818 * Returns 0 if work was queued, or an error code otherwise.
Sage Weil31b80062009-10-06 11:31:13 -07002819 */
Alex Elder802c6d92012-10-08 20:37:30 -07002820static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
Sage Weil31b80062009-10-06 11:31:13 -07002821{
Sage Weil31b80062009-10-06 11:31:13 -07002822 if (!con->ops->get(con)) {
Alex Elder802c6d92012-10-08 20:37:30 -07002823 dout("%s %p ref count 0\n", __func__, con);
Alex Elder802c6d92012-10-08 20:37:30 -07002824 return -ENOENT;
Sage Weil31b80062009-10-06 11:31:13 -07002825 }
2826
Alex Elder802c6d92012-10-08 20:37:30 -07002827 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2828 dout("%s %p - already queued\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -07002829 con->ops->put(con);
Alex Elder802c6d92012-10-08 20:37:30 -07002830 return -EBUSY;
Sage Weil31b80062009-10-06 11:31:13 -07002831 }
Alex Elder802c6d92012-10-08 20:37:30 -07002832
2833 dout("%s %p %lu\n", __func__, con, delay);
Alex Elder802c6d92012-10-08 20:37:30 -07002834 return 0;
2835}
2836
2837static void queue_con(struct ceph_connection *con)
2838{
2839 (void) queue_con_delay(con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002840}
2841
Ilya Dryomov37ab77a2014-06-24 16:21:45 +04002842static void cancel_con(struct ceph_connection *con)
2843{
2844 if (cancel_delayed_work(&con->work)) {
2845 dout("%s %p\n", __func__, con);
2846 con->ops->put(con);
2847 }
2848}
2849
Alex Elder7bb21d682012-12-07 19:50:07 -06002850static bool con_sock_closed(struct ceph_connection *con)
2851{
Alex Elderc9ffc772013-02-20 10:25:12 -06002852 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
Alex Elder7bb21d682012-12-07 19:50:07 -06002853 return false;
2854
2855#define CASE(x) \
2856 case CON_STATE_ ## x: \
2857 con->error_msg = "socket closed (con state " #x ")"; \
2858 break;
2859
2860 switch (con->state) {
2861 CASE(CLOSED);
2862 CASE(PREOPEN);
2863 CASE(CONNECTING);
2864 CASE(NEGOTIATING);
2865 CASE(OPEN);
2866 CASE(STANDBY);
2867 default:
Joe Perchesb9a67892014-09-09 21:17:29 -07002868 pr_warn("%s con %p unrecognized state %lu\n",
Alex Elder7bb21d682012-12-07 19:50:07 -06002869 __func__, con, con->state);
2870 con->error_msg = "unrecognized con state";
2871 BUG();
2872 break;
2873 }
2874#undef CASE
2875
2876 return true;
2877}
2878
Alex Elderf20a39f2013-02-19 12:25:57 -06002879static bool con_backoff(struct ceph_connection *con)
2880{
2881 int ret;
2882
2883 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2884 return false;
2885
2886 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2887 if (ret) {
2888 dout("%s: con %p FAILED to back off %lu\n", __func__,
2889 con, con->delay);
2890 BUG_ON(ret == -ENOENT);
2891 con_flag_set(con, CON_FLAG_BACKOFF);
2892 }
2893
2894 return true;
2895}
2896
Alex Elder93209262013-02-19 12:25:57 -06002897/* Finish fault handling; con->mutex must *not* be held here */
2898
2899static void con_fault_finish(struct ceph_connection *con)
2900{
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002901 dout("%s %p\n", __func__, con);
2902
Alex Elder93209262013-02-19 12:25:57 -06002903 /*
2904 * in case we faulted due to authentication, invalidate our
2905 * current tickets so that we can get new ones.
2906 */
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002907 if (con->auth_retry) {
2908 dout("auth_retry %d, invalidating\n", con->auth_retry);
2909 if (con->ops->invalidate_authorizer)
2910 con->ops->invalidate_authorizer(con);
2911 con->auth_retry = 0;
Alex Elder93209262013-02-19 12:25:57 -06002912 }
2913
2914 if (con->ops->fault)
2915 con->ops->fault(con);
2916}
2917
Sage Weil31b80062009-10-06 11:31:13 -07002918/*
2919 * Do some work on a connection. Drop a connection ref when we're done.
2920 */
Ilya Dryomov68931622015-07-03 15:44:41 +03002921static void ceph_con_workfn(struct work_struct *work)
Sage Weil31b80062009-10-06 11:31:13 -07002922{
2923 struct ceph_connection *con = container_of(work, struct ceph_connection,
2924 work.work);
Alex Elder49659412013-02-19 12:25:57 -06002925 bool fault;
Sage Weil31b80062009-10-06 11:31:13 -07002926
Sage Weil9dd46582010-04-28 13:51:50 -07002927 mutex_lock(&con->mutex);
Alex Elder49659412013-02-19 12:25:57 -06002928 while (true) {
2929 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002930
Alex Elder49659412013-02-19 12:25:57 -06002931 if ((fault = con_sock_closed(con))) {
2932 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2933 break;
2934 }
2935 if (con_backoff(con)) {
2936 dout("%s: con %p BACKOFF\n", __func__, con);
2937 break;
2938 }
2939 if (con->state == CON_STATE_STANDBY) {
2940 dout("%s: con %p STANDBY\n", __func__, con);
2941 break;
2942 }
2943 if (con->state == CON_STATE_CLOSED) {
2944 dout("%s: con %p CLOSED\n", __func__, con);
2945 BUG_ON(con->sock);
2946 break;
2947 }
2948 if (con->state == CON_STATE_PREOPEN) {
2949 dout("%s: con %p PREOPEN\n", __func__, con);
2950 BUG_ON(con->sock);
2951 }
Sage Weil0da5d702011-05-19 11:21:05 -07002952
Alex Elder49659412013-02-19 12:25:57 -06002953 ret = try_read(con);
2954 if (ret < 0) {
2955 if (ret == -EAGAIN)
2956 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002957 if (!con->error_msg)
2958 con->error_msg = "socket error on read";
Alex Elder49659412013-02-19 12:25:57 -06002959 fault = true;
2960 break;
2961 }
2962
2963 ret = try_write(con);
2964 if (ret < 0) {
2965 if (ret == -EAGAIN)
2966 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002967 if (!con->error_msg)
2968 con->error_msg = "socket error on write";
Alex Elder49659412013-02-19 12:25:57 -06002969 fault = true;
2970 }
2971
2972 break; /* If we make it to here, we're done */
Sage Weil3a140a02012-07-30 16:24:21 -07002973 }
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002974 if (fault)
2975 con_fault(con);
Sage Weil9dd46582010-04-28 13:51:50 -07002976 mutex_unlock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002977
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002978 if (fault)
2979 con_fault_finish(con);
2980
2981 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07002982}
2983
Sage Weil31b80062009-10-06 11:31:13 -07002984/*
2985 * Generic error/fault handler. A retry mechanism is used with
2986 * exponential backoff
2987 */
Alex Elder93209262013-02-19 12:25:57 -06002988static void con_fault(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002989{
Sage Weil31b80062009-10-06 11:31:13 -07002990 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002991 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002992
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002993 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2994 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2995 con->error_msg = NULL;
2996
Alex Elder122070a2012-12-26 10:43:57 -06002997 WARN_ON(con->state != CON_STATE_CONNECTING &&
Sage Weil8dacc7d2012-07-20 17:24:40 -07002998 con->state != CON_STATE_NEGOTIATING &&
2999 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08003000
Sage Weil31b80062009-10-06 11:31:13 -07003001 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08003002
Alex Elderc9ffc772013-02-20 10:25:12 -06003003 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07003004 dout("fault on LOSSYTX channel, marking CLOSED\n");
3005 con->state = CON_STATE_CLOSED;
Alex Elder93209262013-02-19 12:25:57 -06003006 return;
Sage Weil3b5ede02012-07-20 15:22:53 -07003007 }
3008
Sage Weil5e095e82009-12-14 14:30:34 -08003009 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05003010 BUG_ON(con->in_msg->con != con);
Sage Weil5e095e82009-12-14 14:30:34 -08003011 ceph_msg_put(con->in_msg);
3012 con->in_msg = NULL;
3013 }
Sage Weil31b80062009-10-06 11:31:13 -07003014
Sage Weile80a52d2010-02-25 12:40:45 -08003015 /* Requeue anything that hasn't been acked */
3016 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003017
Sage Weile76661d2011-03-03 10:10:15 -08003018 /* If there are no messages queued or keepalive pending, place
3019 * the connection in a STANDBY state */
3020 if (list_empty(&con->out_queue) &&
Alex Elderc9ffc772013-02-20 10:25:12 -06003021 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
Sage Weile00de342011-03-04 12:25:05 -08003022 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elderc9ffc772013-02-20 10:25:12 -06003023 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003024 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08003025 } else {
3026 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003027 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08003028 if (con->delay == 0)
3029 con->delay = BASE_DELAY_INTERVAL;
3030 else if (con->delay < MAX_DELAY_INTERVAL)
3031 con->delay *= 2;
Alex Elderc9ffc772013-02-20 10:25:12 -06003032 con_flag_set(con, CON_FLAG_BACKOFF);
Alex Elder8618e302012-10-08 20:37:30 -07003033 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -07003034 }
Sage Weil31b80062009-10-06 11:31:13 -07003035}
3036
3037
3038
3039/*
Alex Elder15d98822012-05-26 23:26:43 -05003040 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07003041 */
Alex Elder15d98822012-05-26 23:26:43 -05003042void ceph_messenger_init(struct ceph_messenger *msgr,
Ilya Dryomov859bff52015-10-28 23:50:58 +01003043 struct ceph_entity_addr *myaddr)
Sage Weil31b80062009-10-06 11:31:13 -07003044{
Sage Weil31b80062009-10-06 11:31:13 -07003045 spin_lock_init(&msgr->global_seq_lock);
3046
Sage Weil31b80062009-10-06 11:31:13 -07003047 if (myaddr)
3048 msgr->inst.addr = *myaddr;
3049
3050 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08003051 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08003052 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08003053 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003054
Guanjun Hea2a32582012-07-08 19:50:33 -07003055 atomic_set(&msgr->stopping, 0);
Ilya Dryomov757856d2015-06-25 17:47:45 +03003056 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
Sage Weil31b80062009-10-06 11:31:13 -07003057
Alex Elder15d98822012-05-26 23:26:43 -05003058 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003059}
Alex Elder15d98822012-05-26 23:26:43 -05003060EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07003061
Ilya Dryomov757856d2015-06-25 17:47:45 +03003062void ceph_messenger_fini(struct ceph_messenger *msgr)
3063{
3064 put_net(read_pnet(&msgr->net));
3065}
3066EXPORT_SYMBOL(ceph_messenger_fini);
3067
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003068static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3069{
3070 if (msg->con)
3071 msg->con->ops->put(msg->con);
3072
3073 msg->con = con ? con->ops->get(con) : NULL;
3074 BUG_ON(msg->con != con);
3075}
3076
Sage Weile00de342011-03-04 12:25:05 -08003077static void clear_standby(struct ceph_connection *con)
3078{
3079 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003080 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08003081 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003082 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08003083 con->connect_seq++;
Alex Elderc9ffc772013-02-20 10:25:12 -06003084 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3085 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
Sage Weile00de342011-03-04 12:25:05 -08003086 }
3087}
3088
Sage Weil31b80062009-10-06 11:31:13 -07003089/*
3090 * Queue up an outgoing message on the given connection.
3091 */
3092void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3093{
Sage Weila59b55a2012-07-20 15:34:04 -07003094 /* set src+dst */
3095 msg->hdr.src = con->msgr->inst.name;
3096 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3097 msg->needs_out_seq = true;
3098
3099 mutex_lock(&con->mutex);
3100
Sage Weil8dacc7d2012-07-20 17:24:40 -07003101 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07003102 dout("con_send %p closed, dropping %p\n", con, msg);
3103 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07003104 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003105 return;
3106 }
3107
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003108 msg_con_set(msg, con);
Sage Weil31b80062009-10-06 11:31:13 -07003109
Sage Weil31b80062009-10-06 11:31:13 -07003110 BUG_ON(!list_empty(&msg->list_head));
3111 list_add_tail(&msg->list_head, &con->out_queue);
3112 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3113 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3114 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3115 le32_to_cpu(msg->hdr.front_len),
3116 le32_to_cpu(msg->hdr.middle_len),
3117 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07003118
3119 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08003120 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003121
3122 /* if there wasn't anything waiting to send before, queue
3123 * new work */
Alex Elderc9ffc772013-02-20 10:25:12 -06003124 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003125 queue_con(con);
3126}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003127EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07003128
3129/*
3130 * Revoke a message that was previously queued for send
3131 */
Alex Elder6740a842012-06-01 14:56:43 -05003132void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003133{
Alex Elder6740a842012-06-01 14:56:43 -05003134 struct ceph_connection *con = msg->con;
3135
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003136 if (!con) {
3137 dout("%s msg %p null con\n", __func__, msg);
Alex Elder6740a842012-06-01 14:56:43 -05003138 return; /* Message not in our possession */
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003139 }
Alex Elder6740a842012-06-01 14:56:43 -05003140
Sage Weilec302642009-12-22 10:43:42 -08003141 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003142 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05003143 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07003144 list_del_init(&msg->list_head);
Sage Weil31b80062009-10-06 11:31:13 -07003145 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05003146
Sage Weil31b80062009-10-06 11:31:13 -07003147 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07003148 }
3149 if (con->out_msg == msg) {
Ilya Dryomov67645d72015-12-28 13:18:34 +03003150 BUG_ON(con->out_skip);
3151 /* footer */
3152 if (con->out_msg_done) {
3153 con->out_skip += con_out_kvec_skip(con);
3154 } else {
3155 BUG_ON(!msg->data_length);
Ilya Dryomov89f08172016-02-20 15:56:07 +01003156 con->out_skip += sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07003157 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003158 /* data, middle, front */
3159 if (msg->data_length)
3160 con->out_skip += msg->cursor.total_resid;
3161 if (msg->middle)
3162 con->out_skip += con_out_kvec_skip(con);
3163 con->out_skip += con_out_kvec_skip(con);
Alex Elder92ce0342012-06-04 14:43:33 -05003164
Ilya Dryomov67645d72015-12-28 13:18:34 +03003165 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3166 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3167 msg->hdr.seq = 0;
3168 con->out_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05003169 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07003170 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003171
Sage Weilec302642009-12-22 10:43:42 -08003172 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003173}
3174
3175/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003176 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08003177 */
Alex Elder8921d112012-06-01 14:56:43 -05003178void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08003179{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003180 struct ceph_connection *con = msg->con;
Alex Elder8921d112012-06-01 14:56:43 -05003181
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003182 if (!con) {
Alex Elder8921d112012-06-01 14:56:43 -05003183 dout("%s msg %p null con\n", __func__, msg);
Alex Elder8921d112012-06-01 14:56:43 -05003184 return; /* Message not in our possession */
3185 }
3186
Sage Weil350b1c32009-12-22 10:45:45 -08003187 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05003188 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00003189 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3190 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3191 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08003192
3193 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05003194 dout("%s %p msg %p revoked\n", __func__, con, msg);
3195 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08003196 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003197 front_len -
3198 middle_len -
3199 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08003200 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08003201 ceph_msg_put(con->in_msg);
3202 con->in_msg = NULL;
3203 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07003204 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08003205 } else {
Alex Elder8921d112012-06-01 14:56:43 -05003206 dout("%s %p in_msg %p msg %p no-op\n",
3207 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08003208 }
3209 mutex_unlock(&con->mutex);
3210}
3211
3212/*
Sage Weil31b80062009-10-06 11:31:13 -07003213 * Queue a keepalive byte to ensure the tcp connection is alive.
3214 */
3215void ceph_con_keepalive(struct ceph_connection *con)
3216{
Sage Weile00de342011-03-04 12:25:05 -08003217 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07003218 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08003219 clear_standby(con);
Sage Weil00650932012-07-20 15:33:04 -07003220 mutex_unlock(&con->mutex);
Alex Elderc9ffc772013-02-20 10:25:12 -06003221 if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
3222 con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003223 queue_con(con);
3224}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003225EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07003226
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003227bool ceph_con_keepalive_expired(struct ceph_connection *con,
3228 unsigned long interval)
3229{
3230 if (interval > 0 &&
3231 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02003232 struct timespec64 now;
3233 struct timespec64 ts;
3234 ktime_get_real_ts64(&now);
3235 jiffies_to_timespec64(interval, &ts);
3236 ts = timespec64_add(con->last_keepalive_ack, ts);
3237 return timespec64_compare(&now, &ts) >= 0;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003238 }
3239 return false;
3240}
3241
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003242static struct ceph_msg_data *ceph_msg_data_add(struct ceph_msg *msg)
Alex Elder43794502013-03-01 18:00:16 -06003243{
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003244 BUG_ON(msg->num_data_items >= msg->max_data_items);
3245 return &msg->data[msg->num_data_items++];
Alex Elder6644ed72013-03-11 23:34:24 -05003246}
3247
3248static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3249{
Yan, Zhenge4339d282014-09-16 17:50:45 +08003250 if (data->type == CEPH_MSG_DATA_PAGELIST)
Alex Elder6644ed72013-03-11 23:34:24 -05003251 ceph_pagelist_release(data->pagelist);
Alex Elder43794502013-03-01 18:00:16 -06003252}
3253
Alex Elder90af3602013-04-05 14:46:01 -05003254void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Alex Elderf1baeb22013-03-07 15:38:26 -06003255 size_t length, size_t alignment)
Alex Elder02afca62013-02-14 12:16:43 -06003256{
Alex Elder6644ed72013-03-11 23:34:24 -05003257 struct ceph_msg_data *data;
3258
Alex Elder07aa1552013-03-04 18:29:06 -06003259 BUG_ON(!pages);
3260 BUG_ON(!length);
Alex Elder02afca62013-02-14 12:16:43 -06003261
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003262 data = ceph_msg_data_add(msg);
3263 data->type = CEPH_MSG_DATA_PAGES;
Alex Elder6644ed72013-03-11 23:34:24 -05003264 data->pages = pages;
3265 data->length = length;
3266 data->alignment = alignment & ~PAGE_MASK;
3267
Alex Elder5240d9f2013-03-14 14:09:06 -05003268 msg->data_length += length;
Alex Elder02afca62013-02-14 12:16:43 -06003269}
Alex Elder90af3602013-04-05 14:46:01 -05003270EXPORT_SYMBOL(ceph_msg_data_add_pages);
Sage Weil31b80062009-10-06 11:31:13 -07003271
Alex Elder90af3602013-04-05 14:46:01 -05003272void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
Alex Elder27fa8382013-02-14 12:16:43 -06003273 struct ceph_pagelist *pagelist)
3274{
Alex Elder6644ed72013-03-11 23:34:24 -05003275 struct ceph_msg_data *data;
3276
Alex Elder07aa1552013-03-04 18:29:06 -06003277 BUG_ON(!pagelist);
3278 BUG_ON(!pagelist->length);
Alex Elder27fa8382013-02-14 12:16:43 -06003279
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003280 data = ceph_msg_data_add(msg);
3281 data->type = CEPH_MSG_DATA_PAGELIST;
Ilya Dryomov894868332018-09-28 16:02:53 +02003282 refcount_inc(&pagelist->refcnt);
Alex Elder6644ed72013-03-11 23:34:24 -05003283 data->pagelist = pagelist;
3284
Alex Elder5240d9f2013-03-14 14:09:06 -05003285 msg->data_length += pagelist->length;
Alex Elder27fa8382013-02-14 12:16:43 -06003286}
Alex Elder90af3602013-04-05 14:46:01 -05003287EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
Alex Elder27fa8382013-02-14 12:16:43 -06003288
Alex Elderea965712013-04-05 14:46:01 -05003289#ifdef CONFIG_BLOCK
Ilya Dryomov5359a172018-01-20 10:30:10 +01003290void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
3291 u32 length)
Alex Elder27fa8382013-02-14 12:16:43 -06003292{
Alex Elder6644ed72013-03-11 23:34:24 -05003293 struct ceph_msg_data *data;
Alex Elder27fa8382013-02-14 12:16:43 -06003294
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003295 data = ceph_msg_data_add(msg);
3296 data->type = CEPH_MSG_DATA_BIO;
Ilya Dryomov5359a172018-01-20 10:30:10 +01003297 data->bio_pos = *bio_pos;
Alex Elderc851c492013-04-05 14:46:01 -05003298 data->bio_length = length;
Alex Elder6644ed72013-03-11 23:34:24 -05003299
Alex Elder5240d9f2013-03-14 14:09:06 -05003300 msg->data_length += length;
Alex Elder27fa8382013-02-14 12:16:43 -06003301}
Alex Elder90af3602013-04-05 14:46:01 -05003302EXPORT_SYMBOL(ceph_msg_data_add_bio);
Alex Elderea965712013-04-05 14:46:01 -05003303#endif /* CONFIG_BLOCK */
Alex Elder27fa8382013-02-14 12:16:43 -06003304
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01003305void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
3306 struct ceph_bvec_iter *bvec_pos)
3307{
3308 struct ceph_msg_data *data;
3309
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003310 data = ceph_msg_data_add(msg);
3311 data->type = CEPH_MSG_DATA_BVECS;
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01003312 data->bvec_pos = *bvec_pos;
3313
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01003314 msg->data_length += bvec_pos->iter.bi_size;
3315}
3316EXPORT_SYMBOL(ceph_msg_data_add_bvecs);
3317
Sage Weil31b80062009-10-06 11:31:13 -07003318/*
3319 * construct a new message with given type, size
3320 * the new msg has a ref count of 1.
3321 */
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003322struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
3323 gfp_t flags, bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07003324{
3325 struct ceph_msg *m;
3326
Alex Eldere3d5d632013-05-01 12:43:04 -05003327 m = kmem_cache_zalloc(ceph_msg_cache, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003328 if (m == NULL)
3329 goto out;
Alex Elder38941f82012-06-01 14:56:43 -05003330
Sage Weil31b80062009-10-06 11:31:13 -07003331 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07003332 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
Sage Weil31b80062009-10-06 11:31:13 -07003333 m->hdr.front_len = cpu_to_le32(front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003334
Alex Elder9516e452013-03-01 18:00:16 -06003335 INIT_LIST_HEAD(&m->list_head);
3336 kref_init(&m->kref);
Henry C Changca208922011-05-03 02:29:56 +00003337
Sage Weil31b80062009-10-06 11:31:13 -07003338 /* front */
3339 if (front_len) {
Ilya Dryomoveeb0bed2014-01-09 20:08:21 +02003340 m->front.iov_base = ceph_kvmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003341 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07003342 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07003343 front_len);
3344 goto out2;
3345 }
3346 } else {
3347 m->front.iov_base = NULL;
3348 }
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02003349 m->front_alloc_len = m->front.iov_len = front_len;
Sage Weil31b80062009-10-06 11:31:13 -07003350
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003351 if (max_data_items) {
3352 m->data = kmalloc_array(max_data_items, sizeof(*m->data),
3353 flags);
3354 if (!m->data)
3355 goto out2;
3356
3357 m->max_data_items = max_data_items;
3358 }
3359
Sage Weilbb257662010-04-01 16:07:23 -07003360 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003361 return m;
3362
3363out2:
3364 ceph_msg_put(m);
3365out:
Sage Weilb61c2762011-08-09 15:03:46 -07003366 if (!can_fail) {
3367 pr_err("msg_new can't create type %d front %d\n", type,
3368 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07003369 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07003370 } else {
3371 dout("msg_new can't create type %d front %d\n", type,
3372 front_len);
3373 }
Sage Weila79832f2010-04-01 16:06:19 -07003374 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003375}
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003376EXPORT_SYMBOL(ceph_msg_new2);
3377
3378struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3379 bool can_fail)
3380{
3381 return ceph_msg_new2(type, front_len, 0, flags, can_fail);
3382}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003383EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07003384
3385/*
Sage Weil31b80062009-10-06 11:31:13 -07003386 * Allocate "middle" portion of a message, if it is needed and wasn't
3387 * allocated by alloc_msg. This allows us to read a small fixed-size
3388 * per-type header in the front and then gracefully fail (i.e.,
3389 * propagate the error to the caller based on info in the front) when
3390 * the middle is too large.
3391 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08003392static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003393{
3394 int type = le16_to_cpu(msg->hdr.type);
3395 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3396
3397 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3398 ceph_msg_type_name(type), middle_len);
3399 BUG_ON(!middle_len);
3400 BUG_ON(msg->middle);
3401
Sage Weilb6c1d5b2009-12-07 12:17:17 -08003402 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07003403 if (!msg->middle)
3404 return -ENOMEM;
3405 return 0;
3406}
3407
Yehuda Sadeh24504182010-01-08 13:58:34 -08003408/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05003409 * Allocate a message for receiving an incoming message on a
3410 * connection, and save the result in con->in_msg. Uses the
3411 * connection's private alloc_msg op if available.
3412 *
Sage Weil4740a622012-07-30 18:19:30 -07003413 * Returns 0 on success, or a negative error code.
3414 *
3415 * On success, if we set *skip = 1:
3416 * - the next message should be skipped and ignored.
3417 * - con->in_msg == NULL
3418 * or if we set *skip = 0:
3419 * - con->in_msg is non-null.
3420 * On error (ENOMEM, EAGAIN, ...),
3421 * - con->in_msg == NULL
Yehuda Sadeh24504182010-01-08 13:58:34 -08003422 */
Sage Weil4740a622012-07-30 18:19:30 -07003423static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08003424{
Sage Weil4740a622012-07-30 18:19:30 -07003425 struct ceph_msg_header *hdr = &con->in_hdr;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003426 int middle_len = le32_to_cpu(hdr->middle_len);
Alex Elder1d866d12013-03-01 18:00:14 -06003427 struct ceph_msg *msg;
Sage Weil4740a622012-07-30 18:19:30 -07003428 int ret = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003429
Alex Elder1c20f2d2012-06-04 14:43:32 -05003430 BUG_ON(con->in_msg != NULL);
Alex Elder53ded492013-03-01 18:00:14 -06003431 BUG_ON(!con->ops->alloc_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003432
Alex Elder53ded492013-03-01 18:00:14 -06003433 mutex_unlock(&con->mutex);
3434 msg = con->ops->alloc_msg(con, hdr, skip);
3435 mutex_lock(&con->mutex);
3436 if (con->state != CON_STATE_OPEN) {
3437 if (msg)
Alex Elder1d866d12013-03-01 18:00:14 -06003438 ceph_msg_put(msg);
Alex Elder53ded492013-03-01 18:00:14 -06003439 return -EAGAIN;
3440 }
Alex Elder41375772013-03-05 09:25:10 -06003441 if (msg) {
3442 BUG_ON(*skip);
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003443 msg_con_set(msg, con);
Alex Elder41375772013-03-05 09:25:10 -06003444 con->in_msg = msg;
Alex Elder41375772013-03-05 09:25:10 -06003445 } else {
3446 /*
3447 * Null message pointer means either we should skip
3448 * this message or we couldn't allocate memory. The
3449 * former is not an error.
3450 */
3451 if (*skip)
3452 return 0;
Alex Elder41375772013-03-05 09:25:10 -06003453
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03003454 con->error_msg = "error allocating memory for incoming message";
Alex Elder53ded492013-03-01 18:00:14 -06003455 return -ENOMEM;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003456 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05003457 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08003458
Alex Elder1c20f2d2012-06-04 14:43:32 -05003459 if (middle_len && !con->in_msg->middle) {
3460 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003461 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05003462 ceph_msg_put(con->in_msg);
3463 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003464 }
3465 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08003466
Sage Weil4740a622012-07-30 18:19:30 -07003467 return ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003468}
3469
Sage Weil31b80062009-10-06 11:31:13 -07003470
3471/*
3472 * Free a generically kmalloc'd message.
3473 */
Ilya Dryomov0215e442014-06-20 14:14:41 +04003474static void ceph_msg_free(struct ceph_msg *m)
Sage Weil31b80062009-10-06 11:31:13 -07003475{
Ilya Dryomov0215e442014-06-20 14:14:41 +04003476 dout("%s %p\n", __func__, m);
Ilya Dryomov4965fc32014-10-23 16:32:57 +04003477 kvfree(m->front.iov_base);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003478 kfree(m->data);
Alex Eldere3d5d632013-05-01 12:43:04 -05003479 kmem_cache_free(ceph_msg_cache, m);
Sage Weil31b80062009-10-06 11:31:13 -07003480}
3481
Ilya Dryomov0215e442014-06-20 14:14:41 +04003482static void ceph_msg_release(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07003483{
Sage Weilc2e552e2009-12-07 15:55:05 -08003484 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003485 int i;
Sage Weil31b80062009-10-06 11:31:13 -07003486
Ilya Dryomov0215e442014-06-20 14:14:41 +04003487 dout("%s %p\n", __func__, m);
Sage Weilc2e552e2009-12-07 15:55:05 -08003488 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07003489
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003490 msg_con_set(m, NULL);
3491
Sage Weilc2e552e2009-12-07 15:55:05 -08003492 /* drop middle, data, if any */
3493 if (m->middle) {
3494 ceph_buffer_put(m->middle);
3495 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003496 }
Alex Elder5240d9f2013-03-14 14:09:06 -05003497
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003498 for (i = 0; i < m->num_data_items; i++)
3499 ceph_msg_data_destroy(&m->data[i]);
Sage Weil58bb3b32009-12-23 12:12:31 -08003500
Sage Weilc2e552e2009-12-07 15:55:05 -08003501 if (m->pool)
3502 ceph_msgpool_put(m->pool, m);
3503 else
Ilya Dryomov0215e442014-06-20 14:14:41 +04003504 ceph_msg_free(m);
Sage Weil31b80062009-10-06 11:31:13 -07003505}
Ilya Dryomov0215e442014-06-20 14:14:41 +04003506
3507struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3508{
3509 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003510 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003511 kref_get(&msg->kref);
3512 return msg;
3513}
3514EXPORT_SYMBOL(ceph_msg_get);
3515
3516void ceph_msg_put(struct ceph_msg *msg)
3517{
3518 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003519 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003520 kref_put(&msg->kref, ceph_msg_release);
3521}
3522EXPORT_SYMBOL(ceph_msg_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003523
3524void ceph_msg_dump(struct ceph_msg *msg)
3525{
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02003526 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3527 msg->front_alloc_len, msg->data_length);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003528 print_hex_dump(KERN_DEBUG, "header: ",
3529 DUMP_PREFIX_OFFSET, 16, 1,
3530 &msg->hdr, sizeof(msg->hdr), true);
3531 print_hex_dump(KERN_DEBUG, " front: ",
3532 DUMP_PREFIX_OFFSET, 16, 1,
3533 msg->front.iov_base, msg->front.iov_len, true);
3534 if (msg->middle)
3535 print_hex_dump(KERN_DEBUG, "middle: ",
3536 DUMP_PREFIX_OFFSET, 16, 1,
3537 msg->middle->vec.iov_base,
3538 msg->middle->vec.iov_len, true);
3539 print_hex_dump(KERN_DEBUG, "footer: ",
3540 DUMP_PREFIX_OFFSET, 16, 1,
3541 &msg->footer, sizeof(msg->footer), true);
3542}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003543EXPORT_SYMBOL(ceph_msg_dump);