blob: 0a3ef33cf7ac2a62debd9f42d0680e01d9ad57f4 [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
Jeff Laytonb726ec92019-05-06 09:38:47 -0400189const char *ceph_pr_addr(const struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700190{
191 int i;
192 char *s;
Jeff Laytonb726ec92019-05-06 09:38:47 -0400193 struct sockaddr_storage ss = addr->in_addr; /* align */
194 struct sockaddr_in *in4 = (struct sockaddr_in *)&ss;
195 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&ss;
Sage Weil31b80062009-10-06 11:31:13 -0700196
Alex Elderf64a9312012-01-23 15:49:27 -0600197 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700198 s = addr_str[i];
199
Jeff Laytonb726ec92019-05-06 09:38:47 -0400200 switch (ss.ss_family) {
Sage Weil31b80062009-10-06 11:31:13 -0700201 case AF_INET:
Jeff Laytond3c3c0a2019-06-17 06:57:25 -0400202 snprintf(s, MAX_ADDR_STR_LEN, "(%d)%pI4:%hu",
203 le32_to_cpu(addr->type), &in4->sin_addr,
Alex Elderbd406142012-01-23 15:49:27 -0600204 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700205 break;
206
207 case AF_INET6:
Jeff Laytond3c3c0a2019-06-17 06:57:25 -0400208 snprintf(s, MAX_ADDR_STR_LEN, "(%d)[%pI6c]:%hu",
209 le32_to_cpu(addr->type), &in6->sin6_addr,
Alex Elderbd406142012-01-23 15:49:27 -0600210 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700211 break;
212
213 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600214 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
Jeff Laytonb726ec92019-05-06 09:38:47 -0400215 ss.ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700216 }
217
218 return s;
219}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700220EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700221
Sage Weil63f2d212009-11-03 15:17:56 -0800222static void encode_my_addr(struct ceph_messenger *msgr)
223{
224 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
225 ceph_encode_addr(&msgr->my_enc_addr);
226}
227
Sage Weil31b80062009-10-06 11:31:13 -0700228/*
229 * work queue for all reading and writing to/from the socket.
230 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600231static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700232
Alex Eldere3d5d632013-05-01 12:43:04 -0500233static int ceph_msgr_slab_init(void)
234{
235 BUG_ON(ceph_msg_cache);
Geliang Tang5ee61e92016-03-13 15:18:39 +0800236 ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
Alex Elder81b36be2013-05-01 12:43:04 -0500237 if (!ceph_msg_cache)
238 return -ENOMEM;
239
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200240 return 0;
Alex Eldere3d5d632013-05-01 12:43:04 -0500241}
242
243static void ceph_msgr_slab_exit(void)
244{
245 BUG_ON(!ceph_msg_cache);
246 kmem_cache_destroy(ceph_msg_cache);
247 ceph_msg_cache = NULL;
248}
249
Alex Elder15417162013-02-19 12:25:56 -0600250static void _ceph_msgr_exit(void)
Alex Elder6173d1f2012-02-14 14:05:33 -0600251{
Alex Elderd3002b92012-02-14 14:05:33 -0600252 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600253 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600254 ceph_msgr_wq = NULL;
255 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600256
Alex Elder6173d1f2012-02-14 14:05:33 -0600257 BUG_ON(zero_page == NULL);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300258 put_page(zero_page);
Alex Elder6173d1f2012-02-14 14:05:33 -0600259 zero_page = NULL;
Benoît Canetd920ff62015-06-25 21:02:57 +0200260
261 ceph_msgr_slab_exit();
Alex Elder6173d1f2012-02-14 14:05:33 -0600262}
263
Chengguang Xu57a35df2018-03-10 20:32:05 +0800264int __init ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700265{
Benoît Canetd920ff62015-06-25 21:02:57 +0200266 if (ceph_msgr_slab_init())
267 return -ENOMEM;
268
Alex Elder57666512012-01-23 15:49:27 -0600269 BUG_ON(zero_page != NULL);
270 zero_page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300271 get_page(zero_page);
Alex Elder57666512012-01-23 15:49:27 -0600272
Ilya Dryomovf9865f02014-10-10 16:39:05 +0400273 /*
274 * The number of active work items is limited by the number of
275 * connections, so leave @max_active at default.
276 */
277 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600278 if (ceph_msgr_wq)
279 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600280
Alex Elder6173d1f2012-02-14 14:05:33 -0600281 pr_err("msgr_init failed to create workqueue\n");
282 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600283
Alex Elder6173d1f2012-02-14 14:05:33 -0600284 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700285}
286
287void ceph_msgr_exit(void)
288{
Alex Elder57666512012-01-23 15:49:27 -0600289 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600290
Alex Elder6173d1f2012-02-14 14:05:33 -0600291 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700292}
293
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700294void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700295{
296 flush_workqueue(ceph_msgr_wq);
297}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700298EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700299
Alex Elderce2c8902012-05-22 22:15:49 -0500300/* Connection socket state transition functions */
301
302static void con_sock_state_init(struct ceph_connection *con)
303{
304 int old_state;
305
306 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
307 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
308 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700309 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
310 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500311}
312
313static void con_sock_state_connecting(struct ceph_connection *con)
314{
315 int old_state;
316
317 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
318 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
319 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700320 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
321 CON_SOCK_STATE_CONNECTING);
Alex Elderce2c8902012-05-22 22:15:49 -0500322}
323
324static void con_sock_state_connected(struct ceph_connection *con)
325{
326 int old_state;
327
328 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
329 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
330 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700331 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
332 CON_SOCK_STATE_CONNECTED);
Alex Elderce2c8902012-05-22 22:15:49 -0500333}
334
335static void con_sock_state_closing(struct ceph_connection *con)
336{
337 int old_state;
338
339 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
340 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
341 old_state != CON_SOCK_STATE_CONNECTED &&
342 old_state != CON_SOCK_STATE_CLOSING))
343 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700344 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
345 CON_SOCK_STATE_CLOSING);
Alex Elderce2c8902012-05-22 22:15:49 -0500346}
347
348static void con_sock_state_closed(struct ceph_connection *con)
349{
350 int old_state;
351
352 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
353 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
Sage Weilfbb85a42012-06-27 12:31:02 -0700354 old_state != CON_SOCK_STATE_CLOSING &&
Sage Weil8007b8d2012-07-30 18:16:16 -0700355 old_state != CON_SOCK_STATE_CONNECTING &&
356 old_state != CON_SOCK_STATE_CLOSED))
Alex Elderce2c8902012-05-22 22:15:49 -0500357 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700358 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
359 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500360}
Sage Weila922d382010-05-29 09:41:23 -0700361
Sage Weil31b80062009-10-06 11:31:13 -0700362/*
363 * socket callback functions
364 */
365
366/* data available on socket, or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400367static void ceph_sock_data_ready(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700368{
Alex Elderbd406142012-01-23 15:49:27 -0600369 struct ceph_connection *con = sk->sk_user_data;
Guanjun Hea2a32582012-07-08 19:50:33 -0700370 if (atomic_read(&con->msgr->stopping)) {
371 return;
372 }
Alex Elderbd406142012-01-23 15:49:27 -0600373
Sage Weil31b80062009-10-06 11:31:13 -0700374 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500375 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700376 con, con->state);
377 queue_con(con);
378 }
379}
380
381/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500382static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700383{
Alex Elderd3002b92012-02-14 14:05:33 -0600384 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700385
Jim Schutt182fac22012-02-29 08:30:58 -0700386 /* only queue to workqueue if there is data we want to write,
387 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500388 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700389 * doesn't get called again until try_write() fills the socket
390 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
391 * and net/core/stream.c:sk_stream_write_space().
392 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600393 if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
Eric Dumazet64dc6132013-07-22 20:26:31 -0700394 if (sk_stream_is_writeable(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500395 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700396 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
397 queue_con(con);
398 }
Sage Weil31b80062009-10-06 11:31:13 -0700399 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500400 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700401 }
Sage Weil31b80062009-10-06 11:31:13 -0700402}
403
404/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500405static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700406{
Alex Elderbd406142012-01-23 15:49:27 -0600407 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700408
Alex Elder327800b2012-05-22 11:41:43 -0500409 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700410 con, con->state, sk->sk_state);
411
Sage Weil31b80062009-10-06 11:31:13 -0700412 switch (sk->sk_state) {
413 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500414 dout("%s TCP_CLOSE\n", __func__);
Gustavo A. R. Silva18370b32017-10-15 12:55:23 -0500415 /* fall through */
Sage Weil31b80062009-10-06 11:31:13 -0700416 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500417 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500418 con_sock_state_closing(con);
Alex Elderc9ffc772013-02-20 10:25:12 -0600419 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
Alex Elderd65c9e02012-06-20 21:53:53 -0500420 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700421 break;
422 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500423 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500424 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700425 queue_con(con);
426 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600427 default: /* Everything else is uninteresting */
428 break;
Sage Weil31b80062009-10-06 11:31:13 -0700429 }
430}
431
432/*
433 * set up socket callbacks
434 */
435static void set_sock_callbacks(struct socket *sock,
436 struct ceph_connection *con)
437{
438 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600439 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500440 sk->sk_data_ready = ceph_sock_data_ready;
441 sk->sk_write_space = ceph_sock_write_space;
442 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700443}
444
445
446/*
447 * socket helpers
448 */
449
450/*
451 * initiate connection to a remote socket.
452 */
Alex Elder41617d02012-02-14 14:05:33 -0600453static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700454{
Jeff Laytoncede1852019-05-06 09:38:46 -0400455 struct sockaddr_storage ss = con->peer_addr.in_addr; /* align */
Sage Weil31b80062009-10-06 11:31:13 -0700456 struct socket *sock;
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100457 unsigned int noio_flag;
Sage Weil31b80062009-10-06 11:31:13 -0700458 int ret;
459
460 BUG_ON(con->sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100461
462 /* sock_create_kern() allocates with GFP_KERNEL */
463 noio_flag = memalloc_noio_save();
Jeff Laytoncede1852019-05-06 09:38:46 -0400464 ret = sock_create_kern(read_pnet(&con->msgr->net), ss.ss_family,
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -0500465 SOCK_STREAM, IPPROTO_TCP, &sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100466 memalloc_noio_restore(noio_flag);
Sage Weil31b80062009-10-06 11:31:13 -0700467 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600468 return ret;
Ilya Dryomov6d7fdb02015-04-02 14:40:58 +0300469 sock->sk->sk_allocation = GFP_NOFS;
Sage Weil31b80062009-10-06 11:31:13 -0700470
Sage Weila6a53492010-04-13 14:07:07 -0700471#ifdef CONFIG_LOCKDEP
472 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
473#endif
474
Sage Weil31b80062009-10-06 11:31:13 -0700475 set_sock_callbacks(sock, con);
476
Jeff Laytonb726ec92019-05-06 09:38:47 -0400477 dout("connect %s\n", ceph_pr_addr(&con->peer_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700478
Sage Weil89a86be2012-06-09 14:19:21 -0700479 con_sock_state_connecting(con);
Jeff Laytoncede1852019-05-06 09:38:46 -0400480 ret = sock->ops->connect(sock, (struct sockaddr *)&ss, sizeof(ss),
Sage Weilf91d3472010-07-01 15:18:31 -0700481 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700482 if (ret == -EINPROGRESS) {
483 dout("connect %s EINPROGRESS sk_state = %u\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -0400484 ceph_pr_addr(&con->peer_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700485 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600486 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700487 pr_err("connect %s error %d\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -0400488 ceph_pr_addr(&con->peer_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700489 sock_release(sock);
Alex Elder41617d02012-02-14 14:05:33 -0600490 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600491 }
Mike Christie89baaa52014-10-16 01:50:19 -0500492
Ilya Dryomov859bff52015-10-28 23:50:58 +0100493 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
Chaitanya Huilgolba988f82015-01-23 16:41:25 +0530494 int optval = 1;
495
496 ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
497 (char *)&optval, sizeof(optval));
498 if (ret)
499 pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
500 ret);
501 }
502
Alex Eldera5bc3122012-01-23 15:49:27 -0600503 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600504 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700505}
506
Ilya Dryomove5c93882018-04-27 18:58:47 +0200507/*
508 * If @buf is NULL, discard up to @len bytes.
509 */
Sage Weil31b80062009-10-06 11:31:13 -0700510static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
511{
512 struct kvec iov = {buf, len};
513 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800514 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700515
Ilya Dryomove5c93882018-04-27 18:58:47 +0200516 if (!buf)
517 msg.msg_flags |= MSG_TRUNC;
518
David Howellsaa563d72018-10-20 00:57:56 +0100519 iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, len);
Al Viro100803a2015-11-15 00:12:48 -0500520 r = sock_recvmsg(sock, &msg, msg.msg_flags);
Sage Weil98bdb0a2011-01-25 08:17:48 -0800521 if (r == -EAGAIN)
522 r = 0;
523 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700524}
525
Alex Elderafb3d902013-03-08 20:58:59 -0600526static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
527 int page_offset, size_t length)
528{
Al Viro100803a2015-11-15 00:12:48 -0500529 struct bio_vec bvec = {
530 .bv_page = page,
531 .bv_offset = page_offset,
532 .bv_len = length
533 };
534 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
535 int r;
Alex Elderafb3d902013-03-08 20:58:59 -0600536
537 BUG_ON(page_offset + length > PAGE_SIZE);
David Howellsaa563d72018-10-20 00:57:56 +0100538 iov_iter_bvec(&msg.msg_iter, READ, &bvec, 1, length);
Al Viro100803a2015-11-15 00:12:48 -0500539 r = sock_recvmsg(sock, &msg, msg.msg_flags);
540 if (r == -EAGAIN)
541 r = 0;
542 return r;
Alex Elderafb3d902013-03-08 20:58:59 -0600543}
544
Sage Weil31b80062009-10-06 11:31:13 -0700545/*
546 * write something. @more is true if caller will be sending more data
547 * shortly.
548 */
549static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
Ilya Dryomov87349cd2018-11-21 18:56:40 +0100550 size_t kvlen, size_t len, bool more)
Sage Weil31b80062009-10-06 11:31:13 -0700551{
552 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800553 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700554
555 if (more)
556 msg.msg_flags |= MSG_MORE;
557 else
558 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
559
Sage Weil42961d22011-01-25 08:19:34 -0800560 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
561 if (r == -EAGAIN)
562 r = 0;
563 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700564}
565
Ilya Dryomov433b0a12018-11-20 15:44:00 +0100566/*
567 * @more: either or both of MSG_MORE and MSG_SENDPAGE_NOTLAST
568 */
Chunwei Chen178eda292014-04-23 12:35:09 +0800569static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
Ilya Dryomov433b0a12018-11-20 15:44:00 +0100570 int offset, size_t size, int more)
Chunwei Chen178eda292014-04-23 12:35:09 +0800571{
Ilya Dryomov3239eb52018-11-16 11:58:19 +0100572 ssize_t (*sendpage)(struct socket *sock, struct page *page,
573 int offset, size_t size, int flags);
Ilya Dryomov433b0a12018-11-20 15:44:00 +0100574 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | more;
Chunwei Chen178eda292014-04-23 12:35:09 +0800575 int ret;
Chunwei Chen178eda292014-04-23 12:35:09 +0800576
Ilya Dryomov7e241f62018-11-08 15:55:37 +0100577 /*
578 * sendpage cannot properly handle pages with page_count == 0,
579 * we need to fall back to sendmsg if that's the case.
580 *
581 * Same goes for slab pages: skb_can_coalesce() allows
582 * coalescing neighboring slab objects into a single frag which
583 * triggers one of hardened usercopy checks.
584 */
585 if (page_count(page) >= 1 && !PageSlab(page))
Ilya Dryomov3239eb52018-11-16 11:58:19 +0100586 sendpage = sock->ops->sendpage;
Al Viro61ff6e92016-01-09 20:55:45 -0500587 else
Ilya Dryomov3239eb52018-11-16 11:58:19 +0100588 sendpage = sock_no_sendpage;
Al Viro61ff6e92016-01-09 20:55:45 -0500589
Ilya Dryomov3239eb52018-11-16 11:58:19 +0100590 ret = sendpage(sock, page, offset, size, flags);
Al Viro61ff6e92016-01-09 20:55:45 -0500591 if (ret == -EAGAIN)
592 ret = 0;
Chunwei Chen178eda292014-04-23 12:35:09 +0800593
594 return ret;
595}
Sage Weil31b80062009-10-06 11:31:13 -0700596
597/*
598 * Shutdown/close the socket for the given connection.
599 */
600static int con_close_socket(struct ceph_connection *con)
601{
Sage Weil8007b8d2012-07-30 18:16:16 -0700602 int rc = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700603
604 dout("con_close_socket on %p sock %p\n", con, con->sock);
Sage Weil8007b8d2012-07-30 18:16:16 -0700605 if (con->sock) {
606 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
607 sock_release(con->sock);
608 con->sock = NULL;
609 }
Alex Elder456ea462012-06-20 21:53:53 -0500610
611 /*
Sage Weil4a861692012-07-20 17:29:55 -0700612 * Forcibly clear the SOCK_CLOSED flag. It gets set
Alex Elder456ea462012-06-20 21:53:53 -0500613 * independent of the connection mutex, and we could have
614 * received a socket close event before we had the chance to
615 * shut the socket down.
616 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600617 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
Sage Weil8007b8d2012-07-30 18:16:16 -0700618
Alex Elderce2c8902012-05-22 22:15:49 -0500619 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700620 return rc;
621}
622
623/*
624 * Reset a connection. Discard all incoming and outgoing messages
625 * and clear *_seq state.
626 */
627static void ceph_msg_remove(struct ceph_msg *msg)
628{
629 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500630
Sage Weil31b80062009-10-06 11:31:13 -0700631 ceph_msg_put(msg);
632}
633static void ceph_msg_remove_list(struct list_head *head)
634{
635 while (!list_empty(head)) {
636 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
637 list_head);
638 ceph_msg_remove(msg);
639 }
640}
641
642static void reset_connection(struct ceph_connection *con)
643{
644 /* reset connection, out_queue, msg_ and connect_seq */
645 /* discard existing out_queue and msg_seq */
Sage Weil0fa6ebc2012-12-27 20:27:04 -0600646 dout("reset_connection %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -0700647 ceph_msg_remove_list(&con->out_queue);
648 ceph_msg_remove_list(&con->out_sent);
649
Sage Weilcf3e5c42009-12-11 09:48:05 -0800650 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500651 BUG_ON(con->in_msg->con != con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800652 ceph_msg_put(con->in_msg);
653 con->in_msg = NULL;
654 }
655
Sage Weil31b80062009-10-06 11:31:13 -0700656 con->connect_seq = 0;
657 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800658 if (con->out_msg) {
Ilya Dryomov583d0fe2015-11-02 17:13:58 +0100659 BUG_ON(con->out_msg->con != con);
Sage Weilc86a2932009-12-14 14:04:30 -0800660 ceph_msg_put(con->out_msg);
661 con->out_msg = NULL;
662 }
Sage Weil31b80062009-10-06 11:31:13 -0700663 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700664 con->in_seq_acked = 0;
Ilya Dryomov67645d72015-12-28 13:18:34 +0300665
666 con->out_skip = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700667}
668
669/*
670 * mark a peer down. drop any open connections.
671 */
672void ceph_con_close(struct ceph_connection *con)
673{
Sage Weil8c50c812012-07-30 16:24:37 -0700674 mutex_lock(&con->mutex);
Jeff Laytonb726ec92019-05-06 09:38:47 -0400675 dout("con_close %p peer %s\n", con, ceph_pr_addr(&con->peer_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700676 con->state = CON_STATE_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500677
Alex Elderc9ffc772013-02-20 10:25:12 -0600678 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
679 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
680 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
681 con_flag_clear(con, CON_FLAG_BACKOFF);
Alex Eldera5988c42012-05-29 11:04:58 -0500682
Sage Weil31b80062009-10-06 11:31:13 -0700683 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700684 con->peer_global_seq = 0;
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400685 cancel_con(con);
Sage Weilee76e072012-07-20 16:45:49 -0700686 con_close_socket(con);
Sage Weilec302642009-12-22 10:43:42 -0800687 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700688}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700689EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700690
691/*
Sage Weil31b80062009-10-06 11:31:13 -0700692 * Reopen a closed connection, with a new peer address.
693 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700694void ceph_con_open(struct ceph_connection *con,
695 __u8 entity_type, __u64 entity_num,
696 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700697{
Sage Weil54691552012-07-30 16:21:40 -0700698 mutex_lock(&con->mutex);
Jeff Laytonb726ec92019-05-06 09:38:47 -0400699 dout("con_open %p %s\n", con, ceph_pr_addr(addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700700
Alex Elder122070a2012-12-26 10:43:57 -0600701 WARN_ON(con->state != CON_STATE_CLOSED);
Sage Weil8dacc7d2012-07-20 17:24:40 -0700702 con->state = CON_STATE_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500703
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700704 con->peer_name.type = (__u8) entity_type;
705 con->peer_name.num = cpu_to_le64(entity_num);
706
Sage Weil31b80062009-10-06 11:31:13 -0700707 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800708 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700709 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700710 queue_con(con);
711}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700712EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700713
714/*
Sage Weil87b315a2010-03-22 14:51:18 -0700715 * return true if this connection ever successfully opened
716 */
717bool ceph_con_opened(struct ceph_connection *con)
718{
719 return con->connect_seq > 0;
720}
721
722/*
Sage Weil31b80062009-10-06 11:31:13 -0700723 * initialize a new connection.
724 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500725void ceph_con_init(struct ceph_connection *con, void *private,
726 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700727 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700728{
729 dout("con_init %p\n", con);
730 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500731 con->private = private;
732 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700733 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500734
735 con_sock_state_init(con);
736
Sage Weilec302642009-12-22 10:43:42 -0800737 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700738 INIT_LIST_HEAD(&con->out_queue);
739 INIT_LIST_HEAD(&con->out_sent);
Ilya Dryomov68931622015-07-03 15:44:41 +0300740 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
Alex Eldera5988c42012-05-29 11:04:58 -0500741
Sage Weil8dacc7d2012-07-20 17:24:40 -0700742 con->state = CON_STATE_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700743}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700744EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700745
746
747/*
748 * We maintain a global counter to order connection attempts. Get
749 * a unique seq greater than @gt.
750 */
751static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
752{
753 u32 ret;
754
755 spin_lock(&msgr->global_seq_lock);
756 if (msgr->global_seq < gt)
757 msgr->global_seq = gt;
758 ret = ++msgr->global_seq;
759 spin_unlock(&msgr->global_seq_lock);
760 return ret;
761}
762
Alex Eldere2200422012-05-23 14:35:23 -0500763static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600764{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300765 BUG_ON(con->out_skip);
766
Alex Elder859eb792012-02-14 14:05:33 -0600767 con->out_kvec_left = 0;
768 con->out_kvec_bytes = 0;
769 con->out_kvec_cur = &con->out_kvec[0];
770}
771
Alex Eldere2200422012-05-23 14:35:23 -0500772static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600773 size_t size, void *data)
774{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300775 int index = con->out_kvec_left;
Alex Elder859eb792012-02-14 14:05:33 -0600776
Ilya Dryomov67645d72015-12-28 13:18:34 +0300777 BUG_ON(con->out_skip);
Alex Elder859eb792012-02-14 14:05:33 -0600778 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
779
780 con->out_kvec[index].iov_len = size;
781 con->out_kvec[index].iov_base = data;
782 con->out_kvec_left++;
783 con->out_kvec_bytes += size;
784}
Sage Weil31b80062009-10-06 11:31:13 -0700785
Ilya Dryomov67645d72015-12-28 13:18:34 +0300786/*
787 * Chop off a kvec from the end. Return residual number of bytes for
788 * that kvec, i.e. how many bytes would have been written if the kvec
789 * hadn't been nuked.
790 */
791static int con_out_kvec_skip(struct ceph_connection *con)
792{
793 int off = con->out_kvec_cur - con->out_kvec;
794 int skip = 0;
795
796 if (con->out_kvec_bytes > 0) {
797 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
798 BUG_ON(con->out_kvec_bytes < skip);
799 BUG_ON(!con->out_kvec_left);
800 con->out_kvec_bytes -= skip;
801 con->out_kvec_left--;
802 }
803
804 return skip;
805}
806
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500807#ifdef CONFIG_BLOCK
Alex Elder6aaa4512013-03-06 23:39:39 -0600808
809/*
810 * For a bio data item, a piece is whatever remains of the next
811 * entry in the current bio iovec, or the first entry in the next
812 * bio in the list.
813 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500814static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500815 size_t length)
Alex Elder6aaa4512013-03-06 23:39:39 -0600816{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500817 struct ceph_msg_data *data = cursor->data;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100818 struct ceph_bio_iter *it = &cursor->bio_iter;
Alex Elder6aaa4512013-03-06 23:39:39 -0600819
Ilya Dryomov5359a172018-01-20 10:30:10 +0100820 cursor->resid = min_t(size_t, length, data->bio_length);
821 *it = data->bio_pos;
822 if (cursor->resid < it->iter.bi_size)
823 it->iter.bi_size = cursor->resid;
Alex Elder6aaa4512013-03-06 23:39:39 -0600824
Ilya Dryomov5359a172018-01-20 10:30:10 +0100825 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
826 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600827}
828
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500829static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
Alex Elder6aaa4512013-03-06 23:39:39 -0600830 size_t *page_offset,
831 size_t *length)
832{
Ilya Dryomov5359a172018-01-20 10:30:10 +0100833 struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio,
834 cursor->bio_iter.iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600835
Ilya Dryomov5359a172018-01-20 10:30:10 +0100836 *page_offset = bv.bv_offset;
837 *length = bv.bv_len;
838 return bv.bv_page;
Alex Elder6aaa4512013-03-06 23:39:39 -0600839}
840
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500841static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
842 size_t bytes)
Alex Elder6aaa4512013-03-06 23:39:39 -0600843{
Ilya Dryomov5359a172018-01-20 10:30:10 +0100844 struct ceph_bio_iter *it = &cursor->bio_iter;
Ilya Dryomov187df762019-03-22 22:14:19 +0100845 struct page *page = bio_iter_page(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600846
Ilya Dryomov5359a172018-01-20 10:30:10 +0100847 BUG_ON(bytes > cursor->resid);
848 BUG_ON(bytes > bio_iter_len(it->bio, it->iter));
Alex Elder25aff7c2013-03-11 23:34:22 -0500849 cursor->resid -= bytes;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100850 bio_advance_iter(it->bio, &it->iter, bytes);
Kent Overstreetf38a5182013-08-07 14:30:24 -0700851
Ilya Dryomov5359a172018-01-20 10:30:10 +0100852 if (!cursor->resid) {
853 BUG_ON(!cursor->last_piece);
854 return false; /* no more data */
855 }
Kent Overstreetf38a5182013-08-07 14:30:24 -0700856
Ilya Dryomov187df762019-03-22 22:14:19 +0100857 if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done &&
858 page == bio_iter_page(it->bio, it->iter)))
Alex Elder6aaa4512013-03-06 23:39:39 -0600859 return false; /* more bytes to process in this segment */
860
Ilya Dryomov5359a172018-01-20 10:30:10 +0100861 if (!it->iter.bi_size) {
862 it->bio = it->bio->bi_next;
863 it->iter = it->bio->bi_iter;
864 if (cursor->resid < it->iter.bi_size)
865 it->iter.bi_size = cursor->resid;
Alex Elder6aaa4512013-03-06 23:39:39 -0600866 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600867
Ilya Dryomov5359a172018-01-20 10:30:10 +0100868 BUG_ON(cursor->last_piece);
869 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
870 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600871 return true;
872}
Alex Elderea965712013-04-05 14:46:01 -0500873#endif /* CONFIG_BLOCK */
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500874
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100875static void ceph_msg_data_bvecs_cursor_init(struct ceph_msg_data_cursor *cursor,
876 size_t length)
877{
878 struct ceph_msg_data *data = cursor->data;
879 struct bio_vec *bvecs = data->bvec_pos.bvecs;
880
881 cursor->resid = min_t(size_t, length, data->bvec_pos.iter.bi_size);
882 cursor->bvec_iter = data->bvec_pos.iter;
883 cursor->bvec_iter.bi_size = cursor->resid;
884
885 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
886 cursor->last_piece =
887 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
888}
889
890static struct page *ceph_msg_data_bvecs_next(struct ceph_msg_data_cursor *cursor,
891 size_t *page_offset,
892 size_t *length)
893{
894 struct bio_vec bv = bvec_iter_bvec(cursor->data->bvec_pos.bvecs,
895 cursor->bvec_iter);
896
897 *page_offset = bv.bv_offset;
898 *length = bv.bv_len;
899 return bv.bv_page;
900}
901
902static bool ceph_msg_data_bvecs_advance(struct ceph_msg_data_cursor *cursor,
903 size_t bytes)
904{
905 struct bio_vec *bvecs = cursor->data->bvec_pos.bvecs;
Ilya Dryomov187df762019-03-22 22:14:19 +0100906 struct page *page = bvec_iter_page(bvecs, cursor->bvec_iter);
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100907
908 BUG_ON(bytes > cursor->resid);
909 BUG_ON(bytes > bvec_iter_len(bvecs, cursor->bvec_iter));
910 cursor->resid -= bytes;
911 bvec_iter_advance(bvecs, &cursor->bvec_iter, bytes);
912
913 if (!cursor->resid) {
914 BUG_ON(!cursor->last_piece);
915 return false; /* no more data */
916 }
917
Ilya Dryomov187df762019-03-22 22:14:19 +0100918 if (!bytes || (cursor->bvec_iter.bi_bvec_done &&
919 page == bvec_iter_page(bvecs, cursor->bvec_iter)))
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100920 return false; /* more bytes to process in this segment */
921
922 BUG_ON(cursor->last_piece);
923 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
924 cursor->last_piece =
925 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
926 return true;
927}
928
Alex Elderfe38a2b2013-03-06 23:39:39 -0600929/*
Alex Eldere766d7b2013-03-07 15:38:28 -0600930 * For a page array, a piece comes from the first page in the array
931 * that has not already been fully consumed.
932 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500933static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500934 size_t length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600935{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500936 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600937 int page_count;
938
939 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
940
941 BUG_ON(!data->pages);
942 BUG_ON(!data->length);
943
Alex Elderca8b3a62013-04-05 14:46:01 -0500944 cursor->resid = min(length, data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600945 page_count = calc_pages_for(data->alignment, (u64)data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600946 cursor->page_offset = data->alignment & ~PAGE_MASK;
947 cursor->page_index = 0;
Alex Elder56fc5652013-03-30 23:46:55 -0500948 BUG_ON(page_count > (int)USHRT_MAX);
949 cursor->page_count = (unsigned short)page_count;
950 BUG_ON(length > SIZE_MAX - cursor->page_offset);
Ilya Dryomov5f740d72014-08-08 12:43:39 +0400951 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600952}
953
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500954static struct page *
955ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
956 size_t *page_offset, size_t *length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600957{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500958 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600959
960 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
961
962 BUG_ON(cursor->page_index >= cursor->page_count);
963 BUG_ON(cursor->page_offset >= PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600964
965 *page_offset = cursor->page_offset;
Alex Elder25aff7c2013-03-11 23:34:22 -0500966 if (cursor->last_piece)
Alex Eldere766d7b2013-03-07 15:38:28 -0600967 *length = cursor->resid;
Alex Elder25aff7c2013-03-11 23:34:22 -0500968 else
Alex Eldere766d7b2013-03-07 15:38:28 -0600969 *length = PAGE_SIZE - *page_offset;
Alex Eldere766d7b2013-03-07 15:38:28 -0600970
971 return data->pages[cursor->page_index];
972}
973
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500974static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
Alex Eldere766d7b2013-03-07 15:38:28 -0600975 size_t bytes)
976{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500977 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
Alex Eldere766d7b2013-03-07 15:38:28 -0600978
979 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600980
981 /* Advance the cursor page offset */
982
983 cursor->resid -= bytes;
Alex Elder5df521b2013-03-30 15:09:59 -0500984 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
985 if (!bytes || cursor->page_offset)
Alex Eldere766d7b2013-03-07 15:38:28 -0600986 return false; /* more bytes to process in the current page */
987
Yan, Zhengd90deda2014-03-23 06:50:39 +0800988 if (!cursor->resid)
989 return false; /* no more data */
990
Alex Elder5df521b2013-03-30 15:09:59 -0500991 /* Move on to the next page; offset is already at 0 */
Alex Eldere766d7b2013-03-07 15:38:28 -0600992
993 BUG_ON(cursor->page_index >= cursor->page_count);
Alex Eldere766d7b2013-03-07 15:38:28 -0600994 cursor->page_index++;
Alex Elder25aff7c2013-03-11 23:34:22 -0500995 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600996
997 return true;
998}
999
1000/*
Alex Elderdd236fc2013-03-06 23:39:39 -06001001 * For a pagelist, a piece is whatever remains to be consumed in the
1002 * first page in the list, or the front of the next page.
Alex Elderfe38a2b2013-03-06 23:39:39 -06001003 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001004static void
1005ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -05001006 size_t length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001007{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001008 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001009 struct ceph_pagelist *pagelist;
1010 struct page *page;
1011
Alex Elderdd236fc2013-03-06 23:39:39 -06001012 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001013
1014 pagelist = data->pagelist;
1015 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001016
1017 if (!length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001018 return; /* pagelist can be assigned but empty */
1019
1020 BUG_ON(list_empty(&pagelist->head));
1021 page = list_first_entry(&pagelist->head, struct page, lru);
1022
Alex Elderca8b3a62013-04-05 14:46:01 -05001023 cursor->resid = min(length, pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001024 cursor->page = page;
1025 cursor->offset = 0;
Alex Eldera51b272e2013-04-19 15:34:49 -05001026 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001027}
1028
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001029static struct page *
1030ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1031 size_t *page_offset, size_t *length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001032{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001033 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001034 struct ceph_pagelist *pagelist;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001035
1036 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1037
1038 pagelist = data->pagelist;
1039 BUG_ON(!pagelist);
1040
1041 BUG_ON(!cursor->page);
Alex Elder25aff7c2013-03-11 23:34:22 -05001042 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001043
Alex Elder5df521b2013-03-30 15:09:59 -05001044 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001045 *page_offset = cursor->offset & ~PAGE_MASK;
Alex Elder5df521b2013-03-30 15:09:59 -05001046 if (cursor->last_piece)
Alex Elder25aff7c2013-03-11 23:34:22 -05001047 *length = cursor->resid;
1048 else
1049 *length = PAGE_SIZE - *page_offset;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001050
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001051 return cursor->page;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001052}
1053
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001054static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
Alex Elderdd236fc2013-03-06 23:39:39 -06001055 size_t bytes)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001056{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001057 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001058 struct ceph_pagelist *pagelist;
1059
1060 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1061
1062 pagelist = data->pagelist;
1063 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001064
1065 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001066 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1067
1068 /* Advance the cursor offset */
1069
Alex Elder25aff7c2013-03-11 23:34:22 -05001070 cursor->resid -= bytes;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001071 cursor->offset += bytes;
Alex Elder5df521b2013-03-30 15:09:59 -05001072 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001073 if (!bytes || cursor->offset & ~PAGE_MASK)
1074 return false; /* more bytes to process in the current page */
1075
Yan, Zhengd90deda2014-03-23 06:50:39 +08001076 if (!cursor->resid)
1077 return false; /* no more data */
1078
Alex Elderfe38a2b2013-03-06 23:39:39 -06001079 /* Move on to the next page */
1080
1081 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001082 cursor->page = list_next_entry(cursor->page, lru);
Alex Elder25aff7c2013-03-11 23:34:22 -05001083 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001084
1085 return true;
1086}
1087
Alex Elderdd236fc2013-03-06 23:39:39 -06001088/*
1089 * Message data is handled (sent or received) in pieces, where each
1090 * piece resides on a single page. The network layer might not
1091 * consume an entire piece at once. A data item's cursor keeps
1092 * track of which piece is next to process and how much remains to
1093 * be processed in that piece. It also tracks whether the current
1094 * piece is the last one in the data item.
1095 */
Alex Elderca8b3a62013-04-05 14:46:01 -05001096static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
Alex Elderdd236fc2013-03-06 23:39:39 -06001097{
Alex Elderca8b3a62013-04-05 14:46:01 -05001098 size_t length = cursor->total_resid;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001099
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001100 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001101 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001102 ceph_msg_data_pagelist_cursor_init(cursor, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001103 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001104 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001105 ceph_msg_data_pages_cursor_init(cursor, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001106 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001107#ifdef CONFIG_BLOCK
1108 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001109 ceph_msg_data_bio_cursor_init(cursor, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001110 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001111#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001112 case CEPH_MSG_DATA_BVECS:
1113 ceph_msg_data_bvecs_cursor_init(cursor, length);
1114 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001115 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001116 default:
1117 /* BUG(); */
1118 break;
1119 }
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001120 cursor->need_crc = true;
Alex Elderdd236fc2013-03-06 23:39:39 -06001121}
1122
Alex Elderca8b3a62013-04-05 14:46:01 -05001123static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1124{
1125 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Alex Elderca8b3a62013-04-05 14:46:01 -05001126
1127 BUG_ON(!length);
1128 BUG_ON(length > msg->data_length);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001129 BUG_ON(!msg->num_data_items);
Alex Elderca8b3a62013-04-05 14:46:01 -05001130
Alex Elderca8b3a62013-04-05 14:46:01 -05001131 cursor->total_resid = length;
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001132 cursor->data = msg->data;
Alex Elderca8b3a62013-04-05 14:46:01 -05001133
1134 __ceph_msg_data_cursor_init(cursor);
1135}
1136
Alex Elderdd236fc2013-03-06 23:39:39 -06001137/*
1138 * Return the page containing the next piece to process for a given
1139 * data item, and supply the page offset and length of that piece.
1140 * Indicate whether this is the last piece in this data item.
1141 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001142static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1143 size_t *page_offset, size_t *length,
Alex Elderdd236fc2013-03-06 23:39:39 -06001144 bool *last_piece)
1145{
1146 struct page *page;
1147
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001148 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001149 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001150 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001151 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001152 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001153 page = ceph_msg_data_pages_next(cursor, page_offset, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001154 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001155#ifdef CONFIG_BLOCK
1156 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001157 page = ceph_msg_data_bio_next(cursor, page_offset, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001158 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001159#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001160 case CEPH_MSG_DATA_BVECS:
1161 page = ceph_msg_data_bvecs_next(cursor, page_offset, length);
1162 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001163 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001164 default:
1165 page = NULL;
1166 break;
1167 }
Ilya Dryomov5359a172018-01-20 10:30:10 +01001168
Alex Elderdd236fc2013-03-06 23:39:39 -06001169 BUG_ON(!page);
1170 BUG_ON(*page_offset + *length > PAGE_SIZE);
1171 BUG_ON(!*length);
Ilya Dryomov5359a172018-01-20 10:30:10 +01001172 BUG_ON(*length > cursor->resid);
Alex Elderdd236fc2013-03-06 23:39:39 -06001173 if (last_piece)
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001174 *last_piece = cursor->last_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001175
1176 return page;
1177}
1178
1179/*
1180 * Returns true if the result moves the cursor on to the next piece
1181 * of the data item.
1182 */
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001183static void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1184 size_t bytes)
Alex Elderdd236fc2013-03-06 23:39:39 -06001185{
1186 bool new_piece;
1187
Alex Elder25aff7c2013-03-11 23:34:22 -05001188 BUG_ON(bytes > cursor->resid);
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001189 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001190 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001191 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
Alex Elderdd236fc2013-03-06 23:39:39 -06001192 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001193 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001194 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
Alex Eldere766d7b2013-03-07 15:38:28 -06001195 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001196#ifdef CONFIG_BLOCK
1197 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001198 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
Alex Elder6aaa4512013-03-06 23:39:39 -06001199 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001200#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001201 case CEPH_MSG_DATA_BVECS:
1202 new_piece = ceph_msg_data_bvecs_advance(cursor, bytes);
1203 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001204 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001205 default:
1206 BUG();
1207 break;
1208 }
Alex Elderca8b3a62013-04-05 14:46:01 -05001209 cursor->total_resid -= bytes;
Alex Elderdd236fc2013-03-06 23:39:39 -06001210
Alex Elderca8b3a62013-04-05 14:46:01 -05001211 if (!cursor->resid && cursor->total_resid) {
1212 WARN_ON(!cursor->last_piece);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001213 cursor->data++;
Alex Elderca8b3a62013-04-05 14:46:01 -05001214 __ceph_msg_data_cursor_init(cursor);
Alex Eldera51b272e2013-04-19 15:34:49 -05001215 new_piece = true;
Alex Elderca8b3a62013-04-05 14:46:01 -05001216 }
Alex Eldera51b272e2013-04-19 15:34:49 -05001217 cursor->need_crc = new_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001218}
1219
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01001220static size_t sizeof_footer(struct ceph_connection *con)
1221{
1222 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1223 sizeof(struct ceph_msg_footer) :
1224 sizeof(struct ceph_msg_footer_old);
1225}
1226
Alex Elder98fa5dd2013-04-02 12:09:50 -05001227static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
Alex Elder739c9052012-06-11 14:57:13 -05001228{
Alex Elder4c59b4a2013-03-11 23:34:23 -05001229 /* Initialize data cursor */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001230
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001231 ceph_msg_data_cursor_init(msg, (size_t)data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001232}
1233
Sage Weil31b80062009-10-06 11:31:13 -07001234/*
1235 * Prepare footer for currently outgoing message, and finish things
1236 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1237 */
Alex Elder859eb792012-02-14 14:05:33 -06001238static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001239{
1240 struct ceph_msg *m = con->out_msg;
1241
Alex Elderfd154f32012-06-11 14:57:13 -05001242 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1243
Sage Weil31b80062009-10-06 11:31:13 -07001244 dout("prepare_write_message_footer %p\n", con);
Ilya Dryomov89f08172016-02-20 15:56:07 +01001245 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
Yan, Zheng33d07332014-11-04 16:33:37 +08001246 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1247 if (con->ops->sign_message)
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01001248 con->ops->sign_message(m);
Yan, Zheng33d07332014-11-04 16:33:37 +08001249 else
1250 m->footer.sig = 0;
Yan, Zheng33d07332014-11-04 16:33:37 +08001251 } else {
1252 m->old_footer.flags = m->footer.flags;
Yan, Zheng33d07332014-11-04 16:33:37 +08001253 }
Sage Weil31b80062009-10-06 11:31:13 -07001254 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -08001255 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -07001256}
1257
1258/*
1259 * Prepare headers for the next outgoing message.
1260 */
1261static void prepare_write_message(struct ceph_connection *con)
1262{
1263 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -06001264 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001265
Alex Eldere2200422012-05-23 14:35:23 -05001266 con_out_kvec_reset(con);
Sage Weilc86a2932009-12-14 14:04:30 -08001267 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -07001268
1269 /* Sneak an ack in there first? If we can get it into the same
1270 * TCP packet that's a good thing. */
1271 if (con->in_seq > con->in_seq_acked) {
1272 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -05001273 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001274 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001275 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001276 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001277 }
1278
Alex Elder38941f82012-06-01 14:56:43 -05001279 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -06001280 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -08001281 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -05001282 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -07001283
1284 /* put message on sent list */
1285 ceph_msg_get(m);
1286 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -07001287
Sage Weile84346b2010-05-11 21:20:38 -07001288 /*
1289 * only assign outgoing seq # if we haven't sent this message
1290 * yet. if it is requeued, resend with it's original seq.
1291 */
1292 if (m->needs_out_seq) {
1293 m->hdr.seq = cpu_to_le64(++con->out_seq);
1294 m->needs_out_seq = false;
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001295
Ilya Dryomov4690faf2017-07-26 09:59:15 +02001296 if (con->ops->reencode_message)
1297 con->ops->reencode_message(m);
1298 }
Sage Weil31b80062009-10-06 11:31:13 -07001299
Alex Elder98fa5dd2013-04-02 12:09:50 -05001300 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
Sage Weil31b80062009-10-06 11:31:13 -07001301 m, con->out_seq, le16_to_cpu(m->hdr.type),
1302 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
Alex Elder98fa5dd2013-04-02 12:09:50 -05001303 m->data_length);
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001304 WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
1305 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
Sage Weil31b80062009-10-06 11:31:13 -07001306
1307 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -05001308 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001309 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
Alex Eldere2200422012-05-23 14:35:23 -05001310 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -06001311
Sage Weil31b80062009-10-06 11:31:13 -07001312 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -05001313 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -06001314 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -07001315
Ilya Dryomov67645d72015-12-28 13:18:34 +03001316 /* fill in hdr crc and finalize hdr */
Alex Eldera9a0c512012-02-15 07:43:54 -06001317 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1318 con->out_msg->hdr.crc = cpu_to_le32(crc);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001319 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
Alex Eldera9a0c512012-02-15 07:43:54 -06001320
Ilya Dryomov67645d72015-12-28 13:18:34 +03001321 /* fill in front and middle crc, footer */
Alex Eldera9a0c512012-02-15 07:43:54 -06001322 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1323 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1324 if (m->middle) {
1325 crc = crc32c(0, m->middle->vec.iov_base,
1326 m->middle->vec.iov_len);
1327 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1328 } else
Sage Weil31b80062009-10-06 11:31:13 -07001329 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -05001330 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -07001331 le32_to_cpu(con->out_msg->footer.front_crc),
1332 le32_to_cpu(con->out_msg->footer.middle_crc));
Ilya Dryomov67645d72015-12-28 13:18:34 +03001333 con->out_msg->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001334
1335 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -05001336 con->out_msg->footer.data_crc = 0;
Alex Elder98fa5dd2013-04-02 12:09:50 -05001337 if (m->data_length) {
1338 prepare_message_data(con->out_msg, m->data_length);
Alex Elder78625052013-03-06 23:39:39 -06001339 con->out_more = 1; /* data + footer will follow */
1340 } else {
Sage Weil31b80062009-10-06 11:31:13 -07001341 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -06001342 prepare_write_message_footer(con);
Alex Elder78625052013-03-06 23:39:39 -06001343 }
Sage Weil31b80062009-10-06 11:31:13 -07001344
Alex Elderc9ffc772013-02-20 10:25:12 -06001345 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001346}
1347
1348/*
1349 * Prepare an ack.
1350 */
1351static void prepare_write_ack(struct ceph_connection *con)
1352{
1353 dout("prepare_write_ack %p %llu -> %llu\n", con,
1354 con->in_seq_acked, con->in_seq);
1355 con->in_seq_acked = con->in_seq;
1356
Alex Eldere2200422012-05-23 14:35:23 -05001357 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001358
Alex Eldere2200422012-05-23 14:35:23 -05001359 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -06001360
Sage Weil31b80062009-10-06 11:31:13 -07001361 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001362 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001363 &con->out_temp_ack);
1364
Sage Weil31b80062009-10-06 11:31:13 -07001365 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elderc9ffc772013-02-20 10:25:12 -06001366 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001367}
1368
1369/*
Sage Weil3a230832013-03-25 08:47:40 -07001370 * Prepare to share the seq during handshake
1371 */
1372static void prepare_write_seq(struct ceph_connection *con)
1373{
1374 dout("prepare_write_seq %p %llu -> %llu\n", con,
1375 con->in_seq_acked, con->in_seq);
1376 con->in_seq_acked = con->in_seq;
1377
1378 con_out_kvec_reset(con);
1379
1380 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1381 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1382 &con->out_temp_ack);
1383
1384 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1385}
1386
1387/*
Sage Weil31b80062009-10-06 11:31:13 -07001388 * Prepare to write keepalive byte.
1389 */
1390static void prepare_write_keepalive(struct ceph_connection *con)
1391{
1392 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -05001393 con_out_kvec_reset(con);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001394 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001395 struct timespec64 now;
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001396
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001397 ktime_get_real_ts64(&now);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001398 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001399 ceph_encode_timespec64(&con->out_temp_keepalive2, &now);
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001400 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1401 &con->out_temp_keepalive2);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001402 } else {
1403 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1404 }
Alex Elderc9ffc772013-02-20 10:25:12 -06001405 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001406}
1407
1408/*
1409 * Connection negotiation.
1410 */
1411
Ilya Dryomov262614c2018-07-26 15:17:46 +02001412static int get_connect_authorizer(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001413{
Alex Eldera3530df2012-05-16 15:16:39 -05001414 struct ceph_auth_handshake *auth;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001415 int auth_proto;
Alex Elderb1c6b982012-05-16 15:16:38 -05001416
1417 if (!con->ops->get_authorizer) {
Ilya Dryomov262614c2018-07-26 15:17:46 +02001418 con->auth = NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -05001419 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1420 con->out_connect.authorizer_len = 0;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001421 return 0;
Alex Elderb1c6b982012-05-16 15:16:38 -05001422 }
1423
Ilya Dryomov262614c2018-07-26 15:17:46 +02001424 auth = con->ops->get_authorizer(con, &auth_proto, con->auth_retry);
Alex Eldera3530df2012-05-16 15:16:39 -05001425 if (IS_ERR(auth))
Ilya Dryomov262614c2018-07-26 15:17:46 +02001426 return PTR_ERR(auth);
Sage Weil0da5d702011-05-19 11:21:05 -07001427
Ilya Dryomov262614c2018-07-26 15:17:46 +02001428 con->auth = auth;
1429 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
1430 con->out_connect.authorizer_len = cpu_to_le32(auth->authorizer_buf_len);
1431 return 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001432}
1433
Sage Weil31b80062009-10-06 11:31:13 -07001434/*
1435 * We connected to a peer and are saying hello.
1436 */
Alex Eldere825a662012-05-16 15:16:38 -05001437static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001438{
Alex Eldere2200422012-05-23 14:35:23 -05001439 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1440 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -05001441 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -08001442
Sage Weileed0ef22009-11-10 14:34:36 -08001443 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001444 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weileed0ef22009-11-10 14:34:36 -08001445}
1446
Ilya Dryomovc0f56b42018-07-26 17:43:47 +02001447static void __prepare_write_connect(struct ceph_connection *con)
1448{
1449 con_out_kvec_add(con, sizeof(con->out_connect), &con->out_connect);
1450 if (con->auth)
1451 con_out_kvec_add(con, con->auth->authorizer_buf_len,
1452 con->auth->authorizer_buf);
1453
1454 con->out_more = 0;
1455 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1456}
1457
Alex Eldere825a662012-05-16 15:16:38 -05001458static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -08001459{
Eric Dumazet95c96172012-04-15 05:58:06 +00001460 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001461 int proto;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001462 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001463
1464 switch (con->peer_name.type) {
1465 case CEPH_ENTITY_TYPE_MON:
1466 proto = CEPH_MONC_PROTOCOL;
1467 break;
1468 case CEPH_ENTITY_TYPE_OSD:
1469 proto = CEPH_OSDC_PROTOCOL;
1470 break;
1471 case CEPH_ENTITY_TYPE_MDS:
1472 proto = CEPH_MDSC_PROTOCOL;
1473 break;
1474 default:
1475 BUG();
1476 }
1477
1478 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1479 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001480
Ilya Dryomov859bff52015-10-28 23:50:58 +01001481 con->out_connect.features =
1482 cpu_to_le64(from_msgr(con->msgr)->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -07001483 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1484 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1485 con->out_connect.global_seq = cpu_to_le32(global_seq);
1486 con->out_connect.protocol_version = cpu_to_le32(proto);
1487 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001488
Ilya Dryomov262614c2018-07-26 15:17:46 +02001489 ret = get_connect_authorizer(con);
1490 if (ret)
1491 return ret;
Alex Elder3da54772012-05-16 15:16:39 -05001492
Ilya Dryomovc0f56b42018-07-26 17:43:47 +02001493 __prepare_write_connect(con);
Alex Eldere10c7582012-05-16 15:16:38 -05001494 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07001495}
1496
Sage Weil31b80062009-10-06 11:31:13 -07001497/*
1498 * write as much of pending kvecs to the socket as we can.
1499 * 1 -> done
1500 * 0 -> socket full, but more to do
1501 * <0 -> error
1502 */
1503static int write_partial_kvec(struct ceph_connection *con)
1504{
1505 int ret;
1506
1507 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1508 while (con->out_kvec_bytes > 0) {
1509 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1510 con->out_kvec_left, con->out_kvec_bytes,
1511 con->out_more);
1512 if (ret <= 0)
1513 goto out;
1514 con->out_kvec_bytes -= ret;
1515 if (con->out_kvec_bytes == 0)
1516 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -06001517
1518 /* account for full iov entries consumed */
1519 while (ret >= con->out_kvec_cur->iov_len) {
1520 BUG_ON(!con->out_kvec_left);
1521 ret -= con->out_kvec_cur->iov_len;
1522 con->out_kvec_cur++;
1523 con->out_kvec_left--;
1524 }
1525 /* and for a partially-consumed entry */
1526 if (ret) {
1527 con->out_kvec_cur->iov_len -= ret;
1528 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -07001529 }
1530 }
1531 con->out_kvec_left = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001532 ret = 1;
1533out:
1534 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1535 con->out_kvec_bytes, con->out_kvec_left, ret);
1536 return ret; /* done! */
1537}
1538
Alex Elder35b62802013-03-08 20:59:00 -06001539static u32 ceph_crc32c_page(u32 crc, struct page *page,
1540 unsigned int page_offset,
1541 unsigned int length)
1542{
1543 char *kaddr;
1544
1545 kaddr = kmap(page);
1546 BUG_ON(kaddr == NULL);
1547 crc = crc32c(crc, kaddr + page_offset, length);
1548 kunmap(page);
1549
1550 return crc;
1551}
Sage Weil31b80062009-10-06 11:31:13 -07001552/*
1553 * Write as much message data payload as we can. If we finish, queue
1554 * up the footer.
1555 * 1 -> done, footer is now queued in out_kvec[].
1556 * 0 -> socket full, but more to do
1557 * <0 -> error
1558 */
Alex Elder34d2d202013-03-08 20:58:59 -06001559static int write_partial_message_data(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001560{
1561 struct ceph_msg *msg = con->out_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001562 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01001563 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Ilya Dryomov433b0a12018-11-20 15:44:00 +01001564 int more = MSG_MORE | MSG_SENDPAGE_NOTLAST;
Alex Elderf5db90b2013-03-11 23:34:23 -05001565 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001566
Alex Elder859a35d2013-03-11 23:34:23 -05001567 dout("%s %p msg %p\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001568
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001569 if (!msg->num_data_items)
Alex Elder4c59b4a2013-03-11 23:34:23 -05001570 return -EINVAL;
1571
Alex Elder5821bd82012-06-11 14:57:13 -05001572 /*
1573 * Iterate through each page that contains data to be
1574 * written, and send as much as possible for each.
1575 *
1576 * If we are calculating the data crc (the default), we will
1577 * need to map the page. If we have no pages, they have
1578 * been revoked, so use the zero page.
1579 */
Alex Elderf5db90b2013-03-11 23:34:23 -05001580 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
Ilya Dryomov45a267d2018-01-22 15:20:15 +01001581 while (cursor->total_resid) {
Alex Elder8a166d02013-03-08 13:35:36 -06001582 struct page *page;
Alex Eldere387d522013-03-06 23:39:38 -06001583 size_t page_offset;
1584 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05001585 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001586
Ilya Dryomov45a267d2018-01-22 15:20:15 +01001587 if (!cursor->resid) {
1588 ceph_msg_data_advance(cursor, 0);
1589 continue;
1590 }
1591
Ilya Dryomov1f6b8212018-11-14 12:24:01 +01001592 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Ilya Dryomov433b0a12018-11-20 15:44:00 +01001593 if (length == cursor->total_resid)
1594 more = MSG_MORE;
Ilya Dryomov1f6b8212018-11-14 12:24:01 +01001595 ret = ceph_tcp_sendpage(con->sock, page, page_offset, length,
Ilya Dryomov433b0a12018-11-20 15:44:00 +01001596 more);
Alex Elderf5db90b2013-03-11 23:34:23 -05001597 if (ret <= 0) {
1598 if (do_datacrc)
1599 msg->footer.data_crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -07001600
Alex Elderf5db90b2013-03-11 23:34:23 -05001601 return ret;
1602 }
Alex Elder143334f2013-03-29 11:44:10 -05001603 if (do_datacrc && cursor->need_crc)
1604 crc = ceph_crc32c_page(crc, page, page_offset, length);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001605 ceph_msg_data_advance(cursor, (size_t)ret);
Sage Weil31b80062009-10-06 11:31:13 -07001606 }
1607
Alex Elder34d2d202013-03-08 20:58:59 -06001608 dout("%s %p msg %p done\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001609
1610 /* prepare and queue up footer, too */
Alex Elderf5db90b2013-03-11 23:34:23 -05001611 if (do_datacrc)
1612 msg->footer.data_crc = cpu_to_le32(crc);
1613 else
Alex Elder84ca8fc2012-06-11 14:57:13 -05001614 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001615 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001616 prepare_write_message_footer(con);
Alex Elderf5db90b2013-03-11 23:34:23 -05001617
1618 return 1; /* must return > 0 to indicate success */
Sage Weil31b80062009-10-06 11:31:13 -07001619}
1620
1621/*
1622 * write some zeros
1623 */
1624static int write_partial_skip(struct ceph_connection *con)
1625{
Ilya Dryomov433b0a12018-11-20 15:44:00 +01001626 int more = MSG_MORE | MSG_SENDPAGE_NOTLAST;
Sage Weil31b80062009-10-06 11:31:13 -07001627 int ret;
1628
Ilya Dryomov67645d72015-12-28 13:18:34 +03001629 dout("%s %p %d left\n", __func__, con, con->out_skip);
Sage Weil31b80062009-10-06 11:31:13 -07001630 while (con->out_skip > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001631 size_t size = min(con->out_skip, (int) PAGE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001632
Ilya Dryomov433b0a12018-11-20 15:44:00 +01001633 if (size == con->out_skip)
1634 more = MSG_MORE;
1635 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, more);
Sage Weil31b80062009-10-06 11:31:13 -07001636 if (ret <= 0)
1637 goto out;
1638 con->out_skip -= ret;
1639 }
1640 ret = 1;
1641out:
1642 return ret;
1643}
1644
1645/*
1646 * Prepare to read connection handshake, or an ack.
1647 */
Sage Weileed0ef22009-11-10 14:34:36 -08001648static void prepare_read_banner(struct ceph_connection *con)
1649{
1650 dout("prepare_read_banner %p\n", con);
1651 con->in_base_pos = 0;
1652}
1653
Sage Weil31b80062009-10-06 11:31:13 -07001654static void prepare_read_connect(struct ceph_connection *con)
1655{
1656 dout("prepare_read_connect %p\n", con);
1657 con->in_base_pos = 0;
1658}
1659
1660static void prepare_read_ack(struct ceph_connection *con)
1661{
1662 dout("prepare_read_ack %p\n", con);
1663 con->in_base_pos = 0;
1664}
1665
Sage Weil3a230832013-03-25 08:47:40 -07001666static void prepare_read_seq(struct ceph_connection *con)
1667{
1668 dout("prepare_read_seq %p\n", con);
1669 con->in_base_pos = 0;
1670 con->in_tag = CEPH_MSGR_TAG_SEQ;
1671}
1672
Sage Weil31b80062009-10-06 11:31:13 -07001673static void prepare_read_tag(struct ceph_connection *con)
1674{
1675 dout("prepare_read_tag %p\n", con);
1676 con->in_base_pos = 0;
1677 con->in_tag = CEPH_MSGR_TAG_READY;
1678}
1679
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001680static void prepare_read_keepalive_ack(struct ceph_connection *con)
1681{
1682 dout("prepare_read_keepalive_ack %p\n", con);
1683 con->in_base_pos = 0;
1684}
1685
Sage Weil31b80062009-10-06 11:31:13 -07001686/*
1687 * Prepare to read a message.
1688 */
1689static int prepare_read_message(struct ceph_connection *con)
1690{
1691 dout("prepare_read_message %p\n", con);
1692 BUG_ON(con->in_msg != NULL);
1693 con->in_base_pos = 0;
1694 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1695 return 0;
1696}
1697
1698
1699static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001700 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001701{
Alex Eldere6cee712012-05-10 10:29:50 -05001702 while (con->in_base_pos < end) {
1703 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001704 int have = size - left;
1705 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1706 if (ret <= 0)
1707 return ret;
1708 con->in_base_pos += ret;
1709 }
1710 return 1;
1711}
1712
1713
1714/*
1715 * Read all or part of the connect-side handshake on a new connection
1716 */
Sage Weileed0ef22009-11-10 14:34:36 -08001717static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001718{
Alex Elderfd516532012-05-10 10:29:50 -05001719 int size;
1720 int end;
1721 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001722
Sage Weileed0ef22009-11-10 14:34:36 -08001723 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001724
1725 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001726 size = strlen(CEPH_BANNER);
1727 end = size;
1728 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001729 if (ret <= 0)
1730 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001731
1732 size = sizeof (con->actual_peer_addr);
1733 end += size;
1734 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001735 if (ret <= 0)
1736 goto out;
Jeff Laytonbc07532c2019-06-04 13:13:48 -04001737 ceph_decode_addr(&con->actual_peer_addr);
Alex Elderfd516532012-05-10 10:29:50 -05001738
1739 size = sizeof (con->peer_addr_for_me);
1740 end += size;
1741 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001742 if (ret <= 0)
1743 goto out;
Jeff Laytonbc07532c2019-06-04 13:13:48 -04001744 ceph_decode_addr(&con->peer_addr_for_me);
Alex Elderfd516532012-05-10 10:29:50 -05001745
Sage Weileed0ef22009-11-10 14:34:36 -08001746out:
1747 return ret;
1748}
1749
1750static int read_partial_connect(struct ceph_connection *con)
1751{
Alex Elderfd516532012-05-10 10:29:50 -05001752 int size;
1753 int end;
1754 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001755
1756 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1757
Alex Elderfd516532012-05-10 10:29:50 -05001758 size = sizeof (con->in_reply);
1759 end = size;
1760 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001761 if (ret <= 0)
1762 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001763
Ilya Dryomov262614c2018-07-26 15:17:46 +02001764 if (con->auth) {
1765 size = le32_to_cpu(con->in_reply.authorizer_len);
Ilya Dryomov130f52f2018-07-27 19:40:30 +02001766 if (size > con->auth->authorizer_reply_buf_len) {
1767 pr_err("authorizer reply too big: %d > %zu\n", size,
1768 con->auth->authorizer_reply_buf_len);
1769 ret = -EINVAL;
1770 goto out;
1771 }
1772
Ilya Dryomov262614c2018-07-26 15:17:46 +02001773 end += size;
1774 ret = read_partial(con, end, size,
1775 con->auth->authorizer_reply_buf);
1776 if (ret <= 0)
1777 goto out;
1778 }
Sage Weil31b80062009-10-06 11:31:13 -07001779
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001780 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1781 con, (int)con->in_reply.tag,
1782 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001783 le32_to_cpu(con->in_reply.global_seq));
1784out:
1785 return ret;
1786}
1787
1788/*
1789 * Verify the hello banner looks okay.
1790 */
1791static int verify_hello(struct ceph_connection *con)
1792{
1793 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001794 pr_err("connect to %s got bad banner\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -04001795 ceph_pr_addr(&con->peer_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001796 con->error_msg = "protocol error, bad banner";
1797 return -1;
1798 }
1799 return 0;
1800}
1801
Jeff Laytoncede1852019-05-06 09:38:46 -04001802static bool addr_is_blank(struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -07001803{
Jeff Laytoncede1852019-05-06 09:38:46 -04001804 struct sockaddr_storage ss = addr->in_addr; /* align */
1805 struct in_addr *addr4 = &((struct sockaddr_in *)&ss)->sin_addr;
1806 struct in6_addr *addr6 = &((struct sockaddr_in6 *)&ss)->sin6_addr;
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001807
Jeff Laytoncede1852019-05-06 09:38:46 -04001808 switch (ss.ss_family) {
Sage Weil31b80062009-10-06 11:31:13 -07001809 case AF_INET:
Jeff Laytoncede1852019-05-06 09:38:46 -04001810 return addr4->s_addr == htonl(INADDR_ANY);
Sage Weil31b80062009-10-06 11:31:13 -07001811 case AF_INET6:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001812 return ipv6_addr_any(addr6);
1813 default:
1814 return true;
Sage Weil31b80062009-10-06 11:31:13 -07001815 }
Sage Weil31b80062009-10-06 11:31:13 -07001816}
1817
Jeff Laytoncede1852019-05-06 09:38:46 -04001818static int addr_port(struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -07001819{
Jeff Laytoncede1852019-05-06 09:38:46 -04001820 switch (get_unaligned(&addr->in_addr.ss_family)) {
Sage Weil31b80062009-10-06 11:31:13 -07001821 case AF_INET:
Jeff Laytoncede1852019-05-06 09:38:46 -04001822 return ntohs(get_unaligned(&((struct sockaddr_in *)&addr->in_addr)->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -07001823 case AF_INET6:
Jeff Laytoncede1852019-05-06 09:38:46 -04001824 return ntohs(get_unaligned(&((struct sockaddr_in6 *)&addr->in_addr)->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -07001825 }
1826 return 0;
1827}
1828
Jeff Laytoncede1852019-05-06 09:38:46 -04001829static void addr_set_port(struct ceph_entity_addr *addr, int p)
Sage Weil31b80062009-10-06 11:31:13 -07001830{
Jeff Laytoncede1852019-05-06 09:38:46 -04001831 switch (get_unaligned(&addr->in_addr.ss_family)) {
Sage Weil31b80062009-10-06 11:31:13 -07001832 case AF_INET:
Jeff Laytoncede1852019-05-06 09:38:46 -04001833 put_unaligned(htons(p), &((struct sockaddr_in *)&addr->in_addr)->sin_port);
Sage Weila2a79602011-05-12 15:34:24 -07001834 break;
Sage Weil31b80062009-10-06 11:31:13 -07001835 case AF_INET6:
Jeff Laytoncede1852019-05-06 09:38:46 -04001836 put_unaligned(htons(p), &((struct sockaddr_in6 *)&addr->in_addr)->sin6_port);
Sage Weila2a79602011-05-12 15:34:24 -07001837 break;
Sage Weil31b80062009-10-06 11:31:13 -07001838 }
1839}
1840
1841/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001842 * Unlike other *_pton function semantics, zero indicates success.
1843 */
Jeff Laytoncede1852019-05-06 09:38:46 -04001844static int ceph_pton(const char *str, size_t len, struct ceph_entity_addr *addr,
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001845 char delim, const char **ipend)
1846{
Jeff Laytoncede1852019-05-06 09:38:46 -04001847 memset(&addr->in_addr, 0, sizeof(addr->in_addr));
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001848
Jeff Laytoncede1852019-05-06 09:38:46 -04001849 if (in4_pton(str, len, (u8 *)&((struct sockaddr_in *)&addr->in_addr)->sin_addr.s_addr, delim, ipend)) {
1850 put_unaligned(AF_INET, &addr->in_addr.ss_family);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001851 return 0;
1852 }
1853
Jeff Laytoncede1852019-05-06 09:38:46 -04001854 if (in6_pton(str, len, (u8 *)&((struct sockaddr_in6 *)&addr->in_addr)->sin6_addr.s6_addr, delim, ipend)) {
1855 put_unaligned(AF_INET6, &addr->in_addr.ss_family);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001856 return 0;
1857 }
1858
1859 return -EINVAL;
1860}
1861
1862/*
1863 * Extract hostname string and resolve using kernel DNS facility.
1864 */
1865#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1866static int ceph_dns_resolve_name(const char *name, size_t namelen,
Jeff Laytoncede1852019-05-06 09:38:46 -04001867 struct ceph_entity_addr *addr, char delim, const char **ipend)
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001868{
1869 const char *end, *delim_p;
1870 char *colon_p, *ip_addr = NULL;
1871 int ip_len, ret;
1872
1873 /*
1874 * The end of the hostname occurs immediately preceding the delimiter or
1875 * the port marker (':') where the delimiter takes precedence.
1876 */
1877 delim_p = memchr(name, delim, namelen);
1878 colon_p = memchr(name, ':', namelen);
1879
1880 if (delim_p && colon_p)
1881 end = delim_p < colon_p ? delim_p : colon_p;
1882 else if (!delim_p && colon_p)
1883 end = colon_p;
1884 else {
1885 end = delim_p;
1886 if (!end) /* case: hostname:/ */
1887 end = name + namelen;
1888 }
1889
1890 if (end <= name)
1891 return -EINVAL;
1892
1893 /* do dns_resolve upcall */
David Howellsd0660f02019-05-03 18:26:55 +01001894 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL, false);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001895 if (ip_len > 0)
Jeff Laytoncede1852019-05-06 09:38:46 -04001896 ret = ceph_pton(ip_addr, ip_len, addr, -1, NULL);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001897 else
1898 ret = -ESRCH;
1899
1900 kfree(ip_addr);
1901
1902 *ipend = end;
1903
1904 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
Jeff Laytonb726ec92019-05-06 09:38:47 -04001905 ret, ret ? "failed" : ceph_pr_addr(addr));
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001906
1907 return ret;
1908}
1909#else
1910static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
Jeff Laytoncede1852019-05-06 09:38:46 -04001911 struct ceph_entity_addr *addr, char delim, const char **ipend)
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001912{
1913 return -EINVAL;
1914}
1915#endif
1916
1917/*
1918 * Parse a server name (IP or hostname). If a valid IP address is not found
1919 * then try to extract a hostname to resolve using userspace DNS upcall.
1920 */
1921static int ceph_parse_server_name(const char *name, size_t namelen,
Jeff Laytoncede1852019-05-06 09:38:46 -04001922 struct ceph_entity_addr *addr, char delim, const char **ipend)
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001923{
1924 int ret;
1925
Jeff Laytoncede1852019-05-06 09:38:46 -04001926 ret = ceph_pton(name, namelen, addr, delim, ipend);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001927 if (ret)
Jeff Laytoncede1852019-05-06 09:38:46 -04001928 ret = ceph_dns_resolve_name(name, namelen, addr, delim, ipend);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001929
1930 return ret;
1931}
1932
1933/*
Sage Weil31b80062009-10-06 11:31:13 -07001934 * Parse an ip[:port] list into an addr array. Use the default
1935 * monitor port if a port isn't specified.
1936 */
1937int ceph_parse_ips(const char *c, const char *end,
1938 struct ceph_entity_addr *addr,
1939 int max_count, int *count)
1940{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001941 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001942 const char *p = c;
1943
1944 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1945 for (i = 0; i < max_count; i++) {
1946 const char *ipend;
Sage Weil31b80062009-10-06 11:31:13 -07001947 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001948 char delim = ',';
1949
1950 if (*p == '[') {
1951 delim = ']';
1952 p++;
1953 }
Sage Weil31b80062009-10-06 11:31:13 -07001954
Jeff Laytoncede1852019-05-06 09:38:46 -04001955 ret = ceph_parse_server_name(p, end - p, &addr[i], delim, &ipend);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001956 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001957 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001958 ret = -EINVAL;
1959
Sage Weil31b80062009-10-06 11:31:13 -07001960 p = ipend;
1961
Sage Weil39139f62010-07-08 09:54:52 -07001962 if (delim == ']') {
1963 if (*p != ']') {
1964 dout("missing matching ']'\n");
1965 goto bad;
1966 }
1967 p++;
1968 }
1969
Sage Weil31b80062009-10-06 11:31:13 -07001970 /* port? */
1971 if (p < end && *p == ':') {
1972 port = 0;
1973 p++;
1974 while (p < end && *p >= '0' && *p <= '9') {
1975 port = (port * 10) + (*p - '0');
1976 p++;
1977 }
Ilya Dryomovf48db1e2013-12-30 19:21:29 +02001978 if (port == 0)
1979 port = CEPH_MON_PORT;
1980 else if (port > 65535)
Sage Weil31b80062009-10-06 11:31:13 -07001981 goto bad;
1982 } else {
1983 port = CEPH_MON_PORT;
1984 }
1985
Jeff Laytoncede1852019-05-06 09:38:46 -04001986 addr_set_port(&addr[i], port);
Jeff Laytond3c3c0a2019-06-17 06:57:25 -04001987 addr[i].type = CEPH_ENTITY_ADDR_TYPE_LEGACY;
Sage Weil31b80062009-10-06 11:31:13 -07001988
Jeff Laytonb726ec92019-05-06 09:38:47 -04001989 dout("parse_ips got %s\n", ceph_pr_addr(&addr[i]));
Sage Weil31b80062009-10-06 11:31:13 -07001990
1991 if (p == end)
1992 break;
1993 if (*p != ',')
1994 goto bad;
1995 p++;
1996 }
1997
1998 if (p != end)
1999 goto bad;
2000
2001 if (count)
2002 *count = i + 1;
2003 return 0;
2004
2005bad:
Sage Weil39139f62010-07-08 09:54:52 -07002006 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07002007 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002008}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002009EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07002010
Sage Weileed0ef22009-11-10 14:34:36 -08002011static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002012{
Sage Weileed0ef22009-11-10 14:34:36 -08002013 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07002014
2015 if (verify_hello(con) < 0)
2016 return -1;
2017
2018 /*
2019 * Make sure the other end is who we wanted. note that the other
2020 * end may not yet know their ip address, so if it's 0.0.0.0, give
2021 * them the benefit of the doubt.
2022 */
Sage Weil103e2d32010-01-07 16:12:36 -08002023 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2024 sizeof(con->peer_addr)) != 0 &&
Jeff Laytoncede1852019-05-06 09:38:46 -04002025 !(addr_is_blank(&con->actual_peer_addr) &&
Sage Weil31b80062009-10-06 11:31:13 -07002026 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Joe Perchesb9a67892014-09-09 21:17:29 -07002027 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -04002028 ceph_pr_addr(&con->peer_addr),
Joe Perchesb9a67892014-09-09 21:17:29 -07002029 (int)le32_to_cpu(con->peer_addr.nonce),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002030 ceph_pr_addr(&con->actual_peer_addr),
Joe Perchesb9a67892014-09-09 21:17:29 -07002031 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08002032 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07002033 return -1;
2034 }
2035
2036 /*
2037 * did we learn our address?
2038 */
Jeff Laytoncede1852019-05-06 09:38:46 -04002039 if (addr_is_blank(&con->msgr->inst.addr)) {
2040 int port = addr_port(&con->msgr->inst.addr);
Sage Weil31b80062009-10-06 11:31:13 -07002041
2042 memcpy(&con->msgr->inst.addr.in_addr,
2043 &con->peer_addr_for_me.in_addr,
2044 sizeof(con->peer_addr_for_me.in_addr));
Jeff Laytoncede1852019-05-06 09:38:46 -04002045 addr_set_port(&con->msgr->inst.addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08002046 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08002047 dout("process_banner learned my addr is %s\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -04002048 ceph_pr_addr(&con->msgr->inst.addr));
Sage Weil31b80062009-10-06 11:31:13 -07002049 }
2050
Sage Weileed0ef22009-11-10 14:34:36 -08002051 return 0;
2052}
2053
2054static int process_connect(struct ceph_connection *con)
2055{
Ilya Dryomov859bff52015-10-28 23:50:58 +01002056 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2057 u64 req_feat = from_msgr(con->msgr)->required_features;
Ilya Dryomovdcbbd972017-06-05 14:44:59 +02002058 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07002059 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08002060
Sage Weileed0ef22009-11-10 14:34:36 -08002061 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2062
Ilya Dryomov262614c2018-07-26 15:17:46 +02002063 if (con->auth) {
Ilya Dryomov0fd3fd02019-02-05 20:30:27 +01002064 int len = le32_to_cpu(con->in_reply.authorizer_len);
2065
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002066 /*
2067 * Any connection that defines ->get_authorizer()
Ilya Dryomov6daca132018-07-27 19:18:34 +02002068 * should also define ->add_authorizer_challenge() and
2069 * ->verify_authorizer_reply().
2070 *
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002071 * See get_connect_authorizer().
2072 */
Ilya Dryomov6daca132018-07-27 19:18:34 +02002073 if (con->in_reply.tag == CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER) {
2074 ret = con->ops->add_authorizer_challenge(
Ilya Dryomov0fd3fd02019-02-05 20:30:27 +01002075 con, con->auth->authorizer_reply_buf, len);
Ilya Dryomov6daca132018-07-27 19:18:34 +02002076 if (ret < 0)
2077 return ret;
2078
2079 con_out_kvec_reset(con);
2080 __prepare_write_connect(con);
2081 prepare_read_connect(con);
2082 return 0;
2083 }
2084
Ilya Dryomov0fd3fd02019-02-05 20:30:27 +01002085 if (len) {
2086 ret = con->ops->verify_authorizer_reply(con);
2087 if (ret < 0) {
2088 con->error_msg = "bad authorize reply";
2089 return ret;
2090 }
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002091 }
2092 }
2093
Sage Weil31b80062009-10-06 11:31:13 -07002094 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08002095 case CEPH_MSGR_TAG_FEATURES:
2096 pr_err("%s%lld %s feature set mismatch,"
2097 " my %llx < server's %llx, missing %llx\n",
2098 ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002099 ceph_pr_addr(&con->peer_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002100 sup_feat, server_feat, server_feat & ~sup_feat);
2101 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002102 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002103 return -1;
2104
Sage Weil31b80062009-10-06 11:31:13 -07002105 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07002106 pr_err("%s%lld %s protocol version mismatch,"
2107 " my %d != server's %d\n",
2108 ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002109 ceph_pr_addr(&con->peer_addr),
Sage Weil31b80062009-10-06 11:31:13 -07002110 le32_to_cpu(con->out_connect.protocol_version),
2111 le32_to_cpu(con->in_reply.protocol_version));
2112 con->error_msg = "protocol version mismatch";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002113 reset_connection(con);
Sage Weil31b80062009-10-06 11:31:13 -07002114 return -1;
2115
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002116 case CEPH_MSGR_TAG_BADAUTHORIZER:
2117 con->auth_retry++;
2118 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2119 con->auth_retry);
2120 if (con->auth_retry == 2) {
2121 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002122 return -1;
2123 }
Jim Schutt6d4221b2012-08-10 10:37:38 -07002124 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002125 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002126 if (ret < 0)
2127 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07002128 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002129 break;
Sage Weil31b80062009-10-06 11:31:13 -07002130
2131 case CEPH_MSGR_TAG_RESETSESSION:
2132 /*
2133 * If we connected with a large connect_seq but the peer
2134 * has no record of a session with us (no connection, or
2135 * connect_seq == 0), they will send RESETSESION to indicate
2136 * that they must have reset their session, and may have
2137 * dropped messages.
2138 */
2139 dout("process_connect got RESET peer seq %u\n",
Sage Weil5bdca4e2012-07-10 11:53:34 -07002140 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002141 pr_err("%s%lld %s connection reset\n",
2142 ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002143 ceph_pr_addr(&con->peer_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002144 reset_connection(con);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002145 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002146 ret = prepare_write_connect(con);
2147 if (ret < 0)
2148 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002149 prepare_read_connect(con);
2150
2151 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08002152 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002153 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2154 if (con->ops->peer_reset)
2155 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08002156 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002157 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07002158 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07002159 break;
2160
2161 case CEPH_MSGR_TAG_RETRY_SESSION:
2162 /*
2163 * If we sent a smaller connect_seq than the peer has, try
2164 * again with a larger value.
2165 */
Sage Weil5bdca4e2012-07-10 11:53:34 -07002166 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002167 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil5bdca4e2012-07-10 11:53:34 -07002168 le32_to_cpu(con->in_reply.connect_seq));
2169 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002170 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002171 ret = prepare_write_connect(con);
2172 if (ret < 0)
2173 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002174 prepare_read_connect(con);
2175 break;
2176
2177 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2178 /*
2179 * If we sent a smaller global_seq than the peer has, try
2180 * again with a larger value.
2181 */
Sage Weileed0ef22009-11-10 14:34:36 -08002182 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002183 con->peer_global_seq,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002184 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002185 get_global_seq(con->msgr,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002186 le32_to_cpu(con->in_reply.global_seq));
Jim Schutt6d4221b2012-08-10 10:37:38 -07002187 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002188 ret = prepare_write_connect(con);
2189 if (ret < 0)
2190 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002191 prepare_read_connect(con);
2192 break;
2193
Sage Weil3a230832013-03-25 08:47:40 -07002194 case CEPH_MSGR_TAG_SEQ:
Sage Weil31b80062009-10-06 11:31:13 -07002195 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08002196 if (req_feat & ~server_feat) {
2197 pr_err("%s%lld %s protocol feature mismatch,"
2198 " my required %llx > server's %llx, need %llx\n",
2199 ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002200 ceph_pr_addr(&con->peer_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002201 req_feat, server_feat, req_feat & ~server_feat);
2202 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002203 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002204 return -1;
2205 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002206
Alex Elder122070a2012-12-26 10:43:57 -06002207 WARN_ON(con->state != CON_STATE_NEGOTIATING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002208 con->state = CON_STATE_OPEN;
Sage Weil20e55c42013-03-25 09:30:13 -07002209 con->auth_retry = 0; /* we authenticated; clear flag */
Sage Weil31b80062009-10-06 11:31:13 -07002210 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2211 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07002212 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07002213 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2214 con->peer_global_seq,
2215 le32_to_cpu(con->in_reply.connect_seq),
2216 con->connect_seq);
2217 WARN_ON(con->connect_seq !=
2218 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08002219
2220 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elderc9ffc772013-02-20 10:25:12 -06002221 con_flag_set(con, CON_FLAG_LOSSYTX);
Sage Weil92ac41d2009-12-14 14:56:56 -08002222
Sage Weil85effe12012-07-30 16:22:05 -07002223 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -07002224
Sage Weil3a230832013-03-25 08:47:40 -07002225 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2226 prepare_write_seq(con);
2227 prepare_read_seq(con);
2228 } else {
2229 prepare_read_tag(con);
2230 }
Sage Weil31b80062009-10-06 11:31:13 -07002231 break;
2232
2233 case CEPH_MSGR_TAG_WAIT:
2234 /*
2235 * If there is a connection race (we are opening
2236 * connections to each other), one of us may just have
2237 * to WAIT. This shouldn't happen if we are the
2238 * client.
2239 */
Sage Weil04177882011-05-12 15:33:17 -07002240 con->error_msg = "protocol error, got WAIT as client";
2241 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07002242
2243 default:
Sage Weil31b80062009-10-06 11:31:13 -07002244 con->error_msg = "protocol error, garbage tag during connect";
2245 return -1;
2246 }
2247 return 0;
2248}
2249
2250
2251/*
2252 * read (part of) an ack
2253 */
2254static int read_partial_ack(struct ceph_connection *con)
2255{
Alex Elderfd516532012-05-10 10:29:50 -05002256 int size = sizeof (con->in_temp_ack);
2257 int end = size;
Sage Weil31b80062009-10-06 11:31:13 -07002258
Alex Elderfd516532012-05-10 10:29:50 -05002259 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07002260}
2261
Sage Weil31b80062009-10-06 11:31:13 -07002262/*
2263 * We can finally discard anything that's been acked.
2264 */
2265static void process_ack(struct ceph_connection *con)
2266{
2267 struct ceph_msg *m;
2268 u64 ack = le64_to_cpu(con->in_temp_ack);
2269 u64 seq;
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002270 bool reconnect = (con->in_tag == CEPH_MSGR_TAG_SEQ);
2271 struct list_head *list = reconnect ? &con->out_queue : &con->out_sent;
Sage Weil31b80062009-10-06 11:31:13 -07002272
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002273 /*
2274 * In the reconnect case, con_fault() has requeued messages
2275 * in out_sent. We should cleanup old messages according to
2276 * the reconnect seq.
2277 */
2278 while (!list_empty(list)) {
2279 m = list_first_entry(list, struct ceph_msg, list_head);
2280 if (reconnect && m->needs_out_seq)
2281 break;
Sage Weil31b80062009-10-06 11:31:13 -07002282 seq = le64_to_cpu(m->hdr.seq);
2283 if (seq > ack)
2284 break;
2285 dout("got ack for seq %llu type %d at %p\n", seq,
2286 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07002287 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07002288 ceph_msg_remove(m);
2289 }
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002290
Sage Weil31b80062009-10-06 11:31:13 -07002291 prepare_read_tag(con);
2292}
2293
2294
Yehuda Sadeh24504182010-01-08 13:58:34 -08002295static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002296 struct kvec *section,
2297 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002298{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002299 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07002300
Yehuda Sadeh24504182010-01-08 13:58:34 -08002301 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07002302
Yehuda Sadeh24504182010-01-08 13:58:34 -08002303 while (section->iov_len < sec_len) {
2304 BUG_ON(section->iov_base == NULL);
2305 left = sec_len - section->iov_len;
2306 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2307 section->iov_len, left);
2308 if (ret <= 0)
2309 return ret;
2310 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002311 }
Alex Elderfe3ad592012-02-15 07:43:54 -06002312 if (section->iov_len == sec_len)
2313 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002314
2315 return 1;
2316}
2317
Alex Elder34d2d202013-03-08 20:58:59 -06002318static int read_partial_msg_data(struct ceph_connection *con)
2319{
2320 struct ceph_msg *msg = con->in_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05002321 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002322 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elder686be202013-03-11 23:34:23 -05002323 struct page *page;
2324 size_t page_offset;
2325 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05002326 u32 crc = 0;
Alex Elder34d2d202013-03-08 20:58:59 -06002327 int ret;
2328
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002329 if (!msg->num_data_items)
Alex Elder4c59b4a2013-03-11 23:34:23 -05002330 return -EIO;
Alex Elder34d2d202013-03-08 20:58:59 -06002331
Alex Elderf5db90b2013-03-11 23:34:23 -05002332 if (do_datacrc)
2333 crc = con->in_data_crc;
Ilya Dryomov45a267d2018-01-22 15:20:15 +01002334 while (cursor->total_resid) {
2335 if (!cursor->resid) {
2336 ceph_msg_data_advance(cursor, 0);
2337 continue;
2338 }
2339
Shraddha Barke343128c2015-10-19 21:59:00 +05302340 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Alex Elder686be202013-03-11 23:34:23 -05002341 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
Alex Elderf5db90b2013-03-11 23:34:23 -05002342 if (ret <= 0) {
2343 if (do_datacrc)
2344 con->in_data_crc = crc;
2345
Alex Elder686be202013-03-11 23:34:23 -05002346 return ret;
Alex Elderf5db90b2013-03-11 23:34:23 -05002347 }
Alex Elder686be202013-03-11 23:34:23 -05002348
2349 if (do_datacrc)
Alex Elderf5db90b2013-03-11 23:34:23 -05002350 crc = ceph_crc32c_page(crc, page, page_offset, ret);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02002351 ceph_msg_data_advance(cursor, (size_t)ret);
Alex Elder34d2d202013-03-08 20:58:59 -06002352 }
Alex Elderf5db90b2013-03-11 23:34:23 -05002353 if (do_datacrc)
2354 con->in_data_crc = crc;
Alex Elder34d2d202013-03-08 20:58:59 -06002355
2356 return 1; /* must return > 0 to indicate success */
2357}
2358
Sage Weil31b80062009-10-06 11:31:13 -07002359/*
2360 * read (part of) a message.
2361 */
Alex Elder686be202013-03-11 23:34:23 -05002362static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2363
Sage Weil31b80062009-10-06 11:31:13 -07002364static int read_partial_message(struct ceph_connection *con)
2365{
2366 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05002367 int size;
2368 int end;
Sage Weil31b80062009-10-06 11:31:13 -07002369 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00002370 unsigned int front_len, middle_len, data_len;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002371 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Yan, Zheng33d07332014-11-04 16:33:37 +08002372 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
Sage Weilae187562010-04-22 07:47:01 -07002373 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06002374 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07002375
2376 dout("read_partial_message con %p msg %p\n", con, m);
2377
2378 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05002379 size = sizeof (con->in_hdr);
2380 end = size;
2381 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05002382 if (ret <= 0)
2383 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06002384
2385 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2386 if (cpu_to_le32(crc) != con->in_hdr.crc) {
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002387 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
Alex Elderfe3ad592012-02-15 07:43:54 -06002388 crc, con->in_hdr.crc);
2389 return -EBADMSG;
2390 }
2391
Sage Weil31b80062009-10-06 11:31:13 -07002392 front_len = le32_to_cpu(con->in_hdr.front_len);
2393 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2394 return -EIO;
2395 middle_len = le32_to_cpu(con->in_hdr.middle_len);
Alex Elder7b11ba32013-03-08 18:51:03 -06002396 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
Sage Weil31b80062009-10-06 11:31:13 -07002397 return -EIO;
2398 data_len = le32_to_cpu(con->in_hdr.data_len);
2399 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2400 return -EIO;
2401
Sage Weilae187562010-04-22 07:47:01 -07002402 /* verify seq# */
2403 seq = le64_to_cpu(con->in_hdr.seq);
2404 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07002405 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07002406 ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002407 ceph_pr_addr(&con->peer_addr),
Sage Weilae187562010-04-22 07:47:01 -07002408 seq, con->in_seq + 1);
2409 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002410 sizeof_footer(con);
Sage Weilae187562010-04-22 07:47:01 -07002411 con->in_tag = CEPH_MSGR_TAG_READY;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002412 return 1;
Sage Weilae187562010-04-22 07:47:01 -07002413 } else if ((s64)seq - (s64)con->in_seq > 1) {
2414 pr_err("read_partial_message bad seq %lld expected %lld\n",
2415 seq, con->in_seq + 1);
2416 con->error_msg = "bad message sequence # for incoming message";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002417 return -EBADE;
Sage Weilae187562010-04-22 07:47:01 -07002418 }
2419
Sage Weil31b80062009-10-06 11:31:13 -07002420 /* allocate message? */
2421 if (!con->in_msg) {
Sage Weil4740a622012-07-30 18:19:30 -07002422 int skip = 0;
2423
Sage Weil31b80062009-10-06 11:31:13 -07002424 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
Alex Elder6ebc8b32013-03-08 18:51:03 -06002425 front_len, data_len);
Sage Weil4740a622012-07-30 18:19:30 -07002426 ret = ceph_con_in_msg_alloc(con, &skip);
2427 if (ret < 0)
2428 return ret;
Alex Elderf759ebb2013-04-05 14:46:01 -05002429
2430 BUG_ON(!con->in_msg ^ skip);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002431 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07002432 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07002433 dout("alloc_msg said skip message\n");
Sage Weil31b80062009-10-06 11:31:13 -07002434 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002435 sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07002436 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002437 con->in_seq++;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002438 return 1;
Sage Weil31b80062009-10-06 11:31:13 -07002439 }
Alex Elder38941f82012-06-01 14:56:43 -05002440
Sage Weil4740a622012-07-30 18:19:30 -07002441 BUG_ON(!con->in_msg);
Alex Elder38941f82012-06-01 14:56:43 -05002442 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002443 m = con->in_msg;
2444 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002445 if (m->middle)
2446 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002447
Alex Elder78625052013-03-06 23:39:39 -06002448 /* prepare for data payload, if any */
Sage Weila4107022012-07-30 16:20:25 -07002449
Alex Elder78625052013-03-06 23:39:39 -06002450 if (data_len)
Alex Elder98fa5dd2013-04-02 12:09:50 -05002451 prepare_message_data(con->in_msg, data_len);
Sage Weil31b80062009-10-06 11:31:13 -07002452 }
2453
2454 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002455 ret = read_partial_message_section(con, &m->front, front_len,
2456 &con->in_front_crc);
2457 if (ret <= 0)
2458 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002459
2460 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002461 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07002462 ret = read_partial_message_section(con, &m->middle->vec,
2463 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002464 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07002465 if (ret <= 0)
2466 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002467 }
2468
2469 /* (page) data */
Alex Elder34d2d202013-03-08 20:58:59 -06002470 if (data_len) {
2471 ret = read_partial_msg_data(con);
2472 if (ret <= 0)
2473 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002474 }
2475
Sage Weil31b80062009-10-06 11:31:13 -07002476 /* footer */
Ilya Dryomov89f08172016-02-20 15:56:07 +01002477 size = sizeof_footer(con);
Alex Elderfd516532012-05-10 10:29:50 -05002478 end += size;
2479 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05002480 if (ret <= 0)
2481 return ret;
2482
Yan, Zheng33d07332014-11-04 16:33:37 +08002483 if (!need_sign) {
2484 m->footer.flags = m->old_footer.flags;
2485 m->footer.sig = 0;
2486 }
2487
Sage Weil31b80062009-10-06 11:31:13 -07002488 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2489 m, front_len, m->footer.front_crc, middle_len,
2490 m->footer.middle_crc, data_len, m->footer.data_crc);
2491
2492 /* crc ok? */
2493 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2494 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2495 m, con->in_front_crc, m->footer.front_crc);
2496 return -EBADMSG;
2497 }
2498 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2499 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2500 m, con->in_middle_crc, m->footer.middle_crc);
2501 return -EBADMSG;
2502 }
Alex Elderbca064d2012-02-15 07:43:54 -06002503 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07002504 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2505 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2506 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2507 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2508 return -EBADMSG;
2509 }
2510
Yan, Zheng33d07332014-11-04 16:33:37 +08002511 if (need_sign && con->ops->check_message_signature &&
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002512 con->ops->check_message_signature(m)) {
Yan, Zheng33d07332014-11-04 16:33:37 +08002513 pr_err("read_partial_message %p signature check failed\n", m);
2514 return -EBADMSG;
2515 }
2516
Sage Weil31b80062009-10-06 11:31:13 -07002517 return 1; /* done! */
2518}
2519
2520/*
2521 * Process message. This happens in the worker thread. The callback should
2522 * be careful not to do anything that waits on other incoming messages or it
2523 * may deadlock.
2524 */
2525static void process_message(struct ceph_connection *con)
2526{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002527 struct ceph_msg *msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07002528
Alex Elder38941f82012-06-01 14:56:43 -05002529 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002530 con->in_msg = NULL;
2531
2532 /* if first message, set peer_name */
2533 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07002534 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07002535
Sage Weil31b80062009-10-06 11:31:13 -07002536 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08002537 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002538
2539 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2540 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07002541 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07002542 le16_to_cpu(msg->hdr.type),
2543 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2544 le32_to_cpu(msg->hdr.front_len),
2545 le32_to_cpu(msg->hdr.data_len),
2546 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2547 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08002548
2549 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002550}
2551
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002552static int read_keepalive_ack(struct ceph_connection *con)
2553{
2554 struct ceph_timespec ceph_ts;
2555 size_t size = sizeof(ceph_ts);
2556 int ret = read_partial(con, size, size, &ceph_ts);
2557 if (ret <= 0)
2558 return ret;
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02002559 ceph_decode_timespec64(&con->last_keepalive_ack, &ceph_ts);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002560 prepare_read_tag(con);
2561 return 1;
2562}
Sage Weil31b80062009-10-06 11:31:13 -07002563
2564/*
2565 * Write something to the socket. Called in a worker thread when the
2566 * socket appears to be writeable and we have something ready to send.
2567 */
2568static int try_write(struct ceph_connection *con)
2569{
Sage Weil31b80062009-10-06 11:31:13 -07002570 int ret = 1;
2571
Sage Weild59315c2012-06-21 12:49:23 -07002572 dout("try_write start %p state %lu\n", con, con->state);
Ilya Dryomov9c55ad12018-04-24 19:10:55 +02002573 if (con->state != CON_STATE_PREOPEN &&
2574 con->state != CON_STATE_CONNECTING &&
2575 con->state != CON_STATE_NEGOTIATING &&
2576 con->state != CON_STATE_OPEN)
2577 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07002578
Sage Weil31b80062009-10-06 11:31:13 -07002579 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002580 if (con->state == CON_STATE_PREOPEN) {
2581 BUG_ON(con->sock);
2582 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002583
Alex Eldere2200422012-05-23 14:35:23 -05002584 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002585 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002586 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002587
Sage Weilcf3e5c42009-12-11 09:48:05 -08002588 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002589 con->in_tag = CEPH_MSGR_TAG_READY;
2590 dout("try_write initiating connect on %p new state %lu\n",
2591 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002592 ret = ceph_tcp_connect(con);
2593 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002594 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002595 goto out;
2596 }
2597 }
2598
Ilya Dryomovd2935d62018-04-25 12:17:13 +02002599more:
2600 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
Ilya Dryomov9c55ad12018-04-24 19:10:55 +02002601 BUG_ON(!con->sock);
2602
Sage Weil31b80062009-10-06 11:31:13 -07002603 /* kvec data queued? */
Ilya Dryomov67645d72015-12-28 13:18:34 +03002604 if (con->out_kvec_left) {
2605 ret = write_partial_kvec(con);
Sage Weil31b80062009-10-06 11:31:13 -07002606 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002607 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002608 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03002609 if (con->out_skip) {
2610 ret = write_partial_skip(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 }
2614
2615 /* msg pages? */
2616 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002617 if (con->out_msg_done) {
2618 ceph_msg_put(con->out_msg);
2619 con->out_msg = NULL; /* we're done with this one */
2620 goto do_next;
2621 }
2622
Alex Elder34d2d202013-03-08 20:58:59 -06002623 ret = write_partial_message_data(con);
Sage Weil31b80062009-10-06 11:31:13 -07002624 if (ret == 1)
Ilya Dryomovd2935d62018-04-25 12:17:13 +02002625 goto more; /* we need to send the footer, too! */
Sage Weil31b80062009-10-06 11:31:13 -07002626 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002627 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002628 if (ret < 0) {
Alex Elder34d2d202013-03-08 20:58:59 -06002629 dout("try_write write_partial_message_data err %d\n",
Sage Weil31b80062009-10-06 11:31:13 -07002630 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002631 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002632 }
2633 }
2634
Sage Weilc86a2932009-12-14 14:04:30 -08002635do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002636 if (con->state == CON_STATE_OPEN) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002637 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2638 prepare_write_keepalive(con);
2639 goto more;
2640 }
Sage Weil31b80062009-10-06 11:31:13 -07002641 /* is anything else pending? */
2642 if (!list_empty(&con->out_queue)) {
2643 prepare_write_message(con);
2644 goto more;
2645 }
2646 if (con->in_seq > con->in_seq_acked) {
2647 prepare_write_ack(con);
2648 goto more;
2649 }
Sage Weil31b80062009-10-06 11:31:13 -07002650 }
2651
2652 /* Nothing to do! */
Alex Elderc9ffc772013-02-20 10:25:12 -06002653 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07002654 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002655 ret = 0;
2656out:
Sage Weil42961d22011-01-25 08:19:34 -08002657 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002658 return ret;
2659}
2660
Sage Weil31b80062009-10-06 11:31:13 -07002661/*
2662 * Read what we can from the socket.
2663 */
2664static int try_read(struct ceph_connection *con)
2665{
Sage Weil31b80062009-10-06 11:31:13 -07002666 int ret = -1;
2667
Sage Weil31b80062009-10-06 11:31:13 -07002668more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002669 dout("try_read start on %p state %lu\n", con, con->state);
2670 if (con->state != CON_STATE_CONNECTING &&
2671 con->state != CON_STATE_NEGOTIATING &&
2672 con->state != CON_STATE_OPEN)
2673 return 0;
2674
2675 BUG_ON(!con->sock);
2676
Sage Weil31b80062009-10-06 11:31:13 -07002677 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2678 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002679
Sage Weil8dacc7d2012-07-20 17:24:40 -07002680 if (con->state == CON_STATE_CONNECTING) {
Alex Elder7593af92012-05-24 11:55:03 -05002681 dout("try_read connecting\n");
2682 ret = read_partial_banner(con);
2683 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002684 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002685 ret = process_banner(con);
2686 if (ret < 0)
2687 goto out;
2688
Sage Weil8dacc7d2012-07-20 17:24:40 -07002689 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002690
Jim Schutt6d4221b2012-08-10 10:37:38 -07002691 /*
2692 * Received banner is good, exchange connection info.
2693 * Do not reset out_kvec, as sending our banner raced
2694 * with receiving peer banner after connect completed.
2695 */
Alex Elder7593af92012-05-24 11:55:03 -05002696 ret = prepare_write_connect(con);
2697 if (ret < 0)
2698 goto out;
2699 prepare_read_connect(con);
2700
2701 /* Send connection info before awaiting response */
Sage Weil0da5d702011-05-19 11:21:05 -07002702 goto out;
2703 }
2704
Sage Weil8dacc7d2012-07-20 17:24:40 -07002705 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002706 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002707 ret = read_partial_connect(con);
2708 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002709 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002710 ret = process_connect(con);
2711 if (ret < 0)
2712 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002713 goto more;
2714 }
2715
Alex Elder122070a2012-12-26 10:43:57 -06002716 WARN_ON(con->state != CON_STATE_OPEN);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002717
Sage Weil31b80062009-10-06 11:31:13 -07002718 if (con->in_base_pos < 0) {
2719 /*
2720 * skipping + discarding content.
Sage Weil31b80062009-10-06 11:31:13 -07002721 */
Ilya Dryomove5c93882018-04-27 18:58:47 +02002722 ret = ceph_tcp_recvmsg(con->sock, NULL, -con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07002723 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002724 goto out;
Ilya Dryomove5c93882018-04-27 18:58:47 +02002725 dout("skipped %d / %d bytes\n", ret, -con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07002726 con->in_base_pos += ret;
2727 if (con->in_base_pos)
2728 goto more;
2729 }
2730 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2731 /*
2732 * what's next?
2733 */
2734 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2735 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002736 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002737 dout("try_read got tag %d\n", (int)con->in_tag);
2738 switch (con->in_tag) {
2739 case CEPH_MSGR_TAG_MSG:
2740 prepare_read_message(con);
2741 break;
2742 case CEPH_MSGR_TAG_ACK:
2743 prepare_read_ack(con);
2744 break;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002745 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2746 prepare_read_keepalive_ack(con);
2747 break;
Sage Weil31b80062009-10-06 11:31:13 -07002748 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002749 con_close_socket(con);
2750 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002751 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002752 default:
2753 goto bad_tag;
2754 }
2755 }
2756 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2757 ret = read_partial_message(con);
2758 if (ret <= 0) {
2759 switch (ret) {
2760 case -EBADMSG:
Ilya Dryomova51983e2015-10-28 23:52:06 +01002761 con->error_msg = "bad crc/signature";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002762 /* fall through */
2763 case -EBADE:
Sage Weil31b80062009-10-06 11:31:13 -07002764 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002765 break;
Sage Weil31b80062009-10-06 11:31:13 -07002766 case -EIO:
2767 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002768 break;
Sage Weil31b80062009-10-06 11:31:13 -07002769 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002770 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002771 }
2772 if (con->in_tag == CEPH_MSGR_TAG_READY)
2773 goto more;
2774 process_message(con);
Sage Weil7b862e02012-07-30 18:16:56 -07002775 if (con->state == CON_STATE_OPEN)
2776 prepare_read_tag(con);
Sage Weil31b80062009-10-06 11:31:13 -07002777 goto more;
2778 }
Sage Weil3a230832013-03-25 08:47:40 -07002779 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2780 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2781 /*
2782 * the final handshake seq exchange is semantically
2783 * equivalent to an ACK
2784 */
Sage Weil31b80062009-10-06 11:31:13 -07002785 ret = read_partial_ack(con);
2786 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002787 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002788 process_ack(con);
2789 goto more;
2790 }
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002791 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2792 ret = read_keepalive_ack(con);
2793 if (ret <= 0)
2794 goto out;
2795 goto more;
2796 }
Sage Weil31b80062009-10-06 11:31:13 -07002797
Sage Weil31b80062009-10-06 11:31:13 -07002798out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002799 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002800 return ret;
2801
2802bad_tag:
2803 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2804 con->error_msg = "protocol error, garbage tag";
2805 ret = -1;
2806 goto out;
2807}
2808
2809
2810/*
Alex Elder802c6d92012-10-08 20:37:30 -07002811 * Atomically queue work on a connection after the specified delay.
2812 * Bump @con reference to avoid races with connection teardown.
2813 * Returns 0 if work was queued, or an error code otherwise.
Sage Weil31b80062009-10-06 11:31:13 -07002814 */
Alex Elder802c6d92012-10-08 20:37:30 -07002815static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
Sage Weil31b80062009-10-06 11:31:13 -07002816{
Sage Weil31b80062009-10-06 11:31:13 -07002817 if (!con->ops->get(con)) {
Alex Elder802c6d92012-10-08 20:37:30 -07002818 dout("%s %p ref count 0\n", __func__, con);
Alex Elder802c6d92012-10-08 20:37:30 -07002819 return -ENOENT;
Sage Weil31b80062009-10-06 11:31:13 -07002820 }
2821
Alex Elder802c6d92012-10-08 20:37:30 -07002822 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2823 dout("%s %p - already queued\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -07002824 con->ops->put(con);
Alex Elder802c6d92012-10-08 20:37:30 -07002825 return -EBUSY;
Sage Weil31b80062009-10-06 11:31:13 -07002826 }
Alex Elder802c6d92012-10-08 20:37:30 -07002827
2828 dout("%s %p %lu\n", __func__, con, delay);
Alex Elder802c6d92012-10-08 20:37:30 -07002829 return 0;
2830}
2831
2832static void queue_con(struct ceph_connection *con)
2833{
2834 (void) queue_con_delay(con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002835}
2836
Ilya Dryomov37ab77a2014-06-24 16:21:45 +04002837static void cancel_con(struct ceph_connection *con)
2838{
2839 if (cancel_delayed_work(&con->work)) {
2840 dout("%s %p\n", __func__, con);
2841 con->ops->put(con);
2842 }
2843}
2844
Alex Elder7bb21d682012-12-07 19:50:07 -06002845static bool con_sock_closed(struct ceph_connection *con)
2846{
Alex Elderc9ffc772013-02-20 10:25:12 -06002847 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
Alex Elder7bb21d682012-12-07 19:50:07 -06002848 return false;
2849
2850#define CASE(x) \
2851 case CON_STATE_ ## x: \
2852 con->error_msg = "socket closed (con state " #x ")"; \
2853 break;
2854
2855 switch (con->state) {
2856 CASE(CLOSED);
2857 CASE(PREOPEN);
2858 CASE(CONNECTING);
2859 CASE(NEGOTIATING);
2860 CASE(OPEN);
2861 CASE(STANDBY);
2862 default:
Joe Perchesb9a67892014-09-09 21:17:29 -07002863 pr_warn("%s con %p unrecognized state %lu\n",
Alex Elder7bb21d682012-12-07 19:50:07 -06002864 __func__, con, con->state);
2865 con->error_msg = "unrecognized con state";
2866 BUG();
2867 break;
2868 }
2869#undef CASE
2870
2871 return true;
2872}
2873
Alex Elderf20a39f2013-02-19 12:25:57 -06002874static bool con_backoff(struct ceph_connection *con)
2875{
2876 int ret;
2877
2878 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2879 return false;
2880
2881 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2882 if (ret) {
2883 dout("%s: con %p FAILED to back off %lu\n", __func__,
2884 con, con->delay);
2885 BUG_ON(ret == -ENOENT);
2886 con_flag_set(con, CON_FLAG_BACKOFF);
2887 }
2888
2889 return true;
2890}
2891
Alex Elder93209262013-02-19 12:25:57 -06002892/* Finish fault handling; con->mutex must *not* be held here */
2893
2894static void con_fault_finish(struct ceph_connection *con)
2895{
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002896 dout("%s %p\n", __func__, con);
2897
Alex Elder93209262013-02-19 12:25:57 -06002898 /*
2899 * in case we faulted due to authentication, invalidate our
2900 * current tickets so that we can get new ones.
2901 */
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002902 if (con->auth_retry) {
2903 dout("auth_retry %d, invalidating\n", con->auth_retry);
2904 if (con->ops->invalidate_authorizer)
2905 con->ops->invalidate_authorizer(con);
2906 con->auth_retry = 0;
Alex Elder93209262013-02-19 12:25:57 -06002907 }
2908
2909 if (con->ops->fault)
2910 con->ops->fault(con);
2911}
2912
Sage Weil31b80062009-10-06 11:31:13 -07002913/*
2914 * Do some work on a connection. Drop a connection ref when we're done.
2915 */
Ilya Dryomov68931622015-07-03 15:44:41 +03002916static void ceph_con_workfn(struct work_struct *work)
Sage Weil31b80062009-10-06 11:31:13 -07002917{
2918 struct ceph_connection *con = container_of(work, struct ceph_connection,
2919 work.work);
Alex Elder49659412013-02-19 12:25:57 -06002920 bool fault;
Sage Weil31b80062009-10-06 11:31:13 -07002921
Sage Weil9dd46582010-04-28 13:51:50 -07002922 mutex_lock(&con->mutex);
Alex Elder49659412013-02-19 12:25:57 -06002923 while (true) {
2924 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002925
Alex Elder49659412013-02-19 12:25:57 -06002926 if ((fault = con_sock_closed(con))) {
2927 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2928 break;
2929 }
2930 if (con_backoff(con)) {
2931 dout("%s: con %p BACKOFF\n", __func__, con);
2932 break;
2933 }
2934 if (con->state == CON_STATE_STANDBY) {
2935 dout("%s: con %p STANDBY\n", __func__, con);
2936 break;
2937 }
2938 if (con->state == CON_STATE_CLOSED) {
2939 dout("%s: con %p CLOSED\n", __func__, con);
2940 BUG_ON(con->sock);
2941 break;
2942 }
2943 if (con->state == CON_STATE_PREOPEN) {
2944 dout("%s: con %p PREOPEN\n", __func__, con);
2945 BUG_ON(con->sock);
2946 }
Sage Weil0da5d702011-05-19 11:21:05 -07002947
Alex Elder49659412013-02-19 12:25:57 -06002948 ret = try_read(con);
2949 if (ret < 0) {
2950 if (ret == -EAGAIN)
2951 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002952 if (!con->error_msg)
2953 con->error_msg = "socket error on read";
Alex Elder49659412013-02-19 12:25:57 -06002954 fault = true;
2955 break;
2956 }
2957
2958 ret = try_write(con);
2959 if (ret < 0) {
2960 if (ret == -EAGAIN)
2961 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002962 if (!con->error_msg)
2963 con->error_msg = "socket error on write";
Alex Elder49659412013-02-19 12:25:57 -06002964 fault = true;
2965 }
2966
2967 break; /* If we make it to here, we're done */
Sage Weil3a140a02012-07-30 16:24:21 -07002968 }
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002969 if (fault)
2970 con_fault(con);
Sage Weil9dd46582010-04-28 13:51:50 -07002971 mutex_unlock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002972
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002973 if (fault)
2974 con_fault_finish(con);
2975
2976 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07002977}
2978
Sage Weil31b80062009-10-06 11:31:13 -07002979/*
2980 * Generic error/fault handler. A retry mechanism is used with
2981 * exponential backoff
2982 */
Alex Elder93209262013-02-19 12:25:57 -06002983static void con_fault(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002984{
Sage Weil31b80062009-10-06 11:31:13 -07002985 dout("fault %p state %lu to peer %s\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -04002986 con, con->state, ceph_pr_addr(&con->peer_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002987
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002988 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002989 ceph_pr_addr(&con->peer_addr), con->error_msg);
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002990 con->error_msg = NULL;
2991
Alex Elder122070a2012-12-26 10:43:57 -06002992 WARN_ON(con->state != CON_STATE_CONNECTING &&
Sage Weil8dacc7d2012-07-20 17:24:40 -07002993 con->state != CON_STATE_NEGOTIATING &&
2994 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08002995
Sage Weil31b80062009-10-06 11:31:13 -07002996 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002997
Alex Elderc9ffc772013-02-20 10:25:12 -06002998 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002999 dout("fault on LOSSYTX channel, marking CLOSED\n");
3000 con->state = CON_STATE_CLOSED;
Alex Elder93209262013-02-19 12:25:57 -06003001 return;
Sage Weil3b5ede02012-07-20 15:22:53 -07003002 }
3003
Sage Weil5e095e82009-12-14 14:30:34 -08003004 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05003005 BUG_ON(con->in_msg->con != con);
Sage Weil5e095e82009-12-14 14:30:34 -08003006 ceph_msg_put(con->in_msg);
3007 con->in_msg = NULL;
3008 }
Sage Weil31b80062009-10-06 11:31:13 -07003009
Sage Weile80a52d2010-02-25 12:40:45 -08003010 /* Requeue anything that hasn't been acked */
3011 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003012
Sage Weile76661d2011-03-03 10:10:15 -08003013 /* If there are no messages queued or keepalive pending, place
3014 * the connection in a STANDBY state */
3015 if (list_empty(&con->out_queue) &&
Alex Elderc9ffc772013-02-20 10:25:12 -06003016 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
Sage Weile00de342011-03-04 12:25:05 -08003017 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elderc9ffc772013-02-20 10:25:12 -06003018 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003019 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08003020 } else {
3021 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003022 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08003023 if (con->delay == 0)
3024 con->delay = BASE_DELAY_INTERVAL;
3025 else if (con->delay < MAX_DELAY_INTERVAL)
3026 con->delay *= 2;
Alex Elderc9ffc772013-02-20 10:25:12 -06003027 con_flag_set(con, CON_FLAG_BACKOFF);
Alex Elder8618e302012-10-08 20:37:30 -07003028 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -07003029 }
Sage Weil31b80062009-10-06 11:31:13 -07003030}
3031
3032
3033
3034/*
Alex Elder15d98822012-05-26 23:26:43 -05003035 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07003036 */
Alex Elder15d98822012-05-26 23:26:43 -05003037void ceph_messenger_init(struct ceph_messenger *msgr,
Ilya Dryomov859bff52015-10-28 23:50:58 +01003038 struct ceph_entity_addr *myaddr)
Sage Weil31b80062009-10-06 11:31:13 -07003039{
Sage Weil31b80062009-10-06 11:31:13 -07003040 spin_lock_init(&msgr->global_seq_lock);
3041
Sage Weil31b80062009-10-06 11:31:13 -07003042 if (myaddr)
3043 msgr->inst.addr = *myaddr;
3044
3045 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08003046 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08003047 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08003048 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003049
Guanjun Hea2a32582012-07-08 19:50:33 -07003050 atomic_set(&msgr->stopping, 0);
Ilya Dryomov757856d2015-06-25 17:47:45 +03003051 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
Sage Weil31b80062009-10-06 11:31:13 -07003052
Alex Elder15d98822012-05-26 23:26:43 -05003053 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003054}
Alex Elder15d98822012-05-26 23:26:43 -05003055EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07003056
Ilya Dryomov757856d2015-06-25 17:47:45 +03003057void ceph_messenger_fini(struct ceph_messenger *msgr)
3058{
3059 put_net(read_pnet(&msgr->net));
3060}
3061EXPORT_SYMBOL(ceph_messenger_fini);
3062
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003063static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3064{
3065 if (msg->con)
3066 msg->con->ops->put(msg->con);
3067
3068 msg->con = con ? con->ops->get(con) : NULL;
3069 BUG_ON(msg->con != con);
3070}
3071
Sage Weile00de342011-03-04 12:25:05 -08003072static void clear_standby(struct ceph_connection *con)
3073{
3074 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003075 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08003076 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003077 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08003078 con->connect_seq++;
Alex Elderc9ffc772013-02-20 10:25:12 -06003079 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3080 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
Sage Weile00de342011-03-04 12:25:05 -08003081 }
3082}
3083
Sage Weil31b80062009-10-06 11:31:13 -07003084/*
3085 * Queue up an outgoing message on the given connection.
3086 */
3087void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3088{
Sage Weila59b55a2012-07-20 15:34:04 -07003089 /* set src+dst */
3090 msg->hdr.src = con->msgr->inst.name;
3091 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3092 msg->needs_out_seq = true;
3093
3094 mutex_lock(&con->mutex);
3095
Sage Weil8dacc7d2012-07-20 17:24:40 -07003096 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07003097 dout("con_send %p closed, dropping %p\n", con, msg);
3098 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07003099 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003100 return;
3101 }
3102
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003103 msg_con_set(msg, con);
Sage Weil31b80062009-10-06 11:31:13 -07003104
Sage Weil31b80062009-10-06 11:31:13 -07003105 BUG_ON(!list_empty(&msg->list_head));
3106 list_add_tail(&msg->list_head, &con->out_queue);
3107 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3108 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3109 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3110 le32_to_cpu(msg->hdr.front_len),
3111 le32_to_cpu(msg->hdr.middle_len),
3112 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07003113
3114 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08003115 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003116
3117 /* if there wasn't anything waiting to send before, queue
3118 * new work */
Alex Elderc9ffc772013-02-20 10:25:12 -06003119 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003120 queue_con(con);
3121}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003122EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07003123
3124/*
3125 * Revoke a message that was previously queued for send
3126 */
Alex Elder6740a842012-06-01 14:56:43 -05003127void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003128{
Alex Elder6740a842012-06-01 14:56:43 -05003129 struct ceph_connection *con = msg->con;
3130
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003131 if (!con) {
3132 dout("%s msg %p null con\n", __func__, msg);
Alex Elder6740a842012-06-01 14:56:43 -05003133 return; /* Message not in our possession */
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003134 }
Alex Elder6740a842012-06-01 14:56:43 -05003135
Sage Weilec302642009-12-22 10:43:42 -08003136 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003137 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05003138 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07003139 list_del_init(&msg->list_head);
Sage Weil31b80062009-10-06 11:31:13 -07003140 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05003141
Sage Weil31b80062009-10-06 11:31:13 -07003142 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07003143 }
3144 if (con->out_msg == msg) {
Ilya Dryomov67645d72015-12-28 13:18:34 +03003145 BUG_ON(con->out_skip);
3146 /* footer */
3147 if (con->out_msg_done) {
3148 con->out_skip += con_out_kvec_skip(con);
3149 } else {
3150 BUG_ON(!msg->data_length);
Ilya Dryomov89f08172016-02-20 15:56:07 +01003151 con->out_skip += sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07003152 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003153 /* data, middle, front */
3154 if (msg->data_length)
3155 con->out_skip += msg->cursor.total_resid;
3156 if (msg->middle)
3157 con->out_skip += con_out_kvec_skip(con);
3158 con->out_skip += con_out_kvec_skip(con);
Alex Elder92ce0342012-06-04 14:43:33 -05003159
Ilya Dryomov67645d72015-12-28 13:18:34 +03003160 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3161 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3162 msg->hdr.seq = 0;
3163 con->out_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05003164 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07003165 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003166
Sage Weilec302642009-12-22 10:43:42 -08003167 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003168}
3169
3170/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003171 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08003172 */
Alex Elder8921d112012-06-01 14:56:43 -05003173void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08003174{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003175 struct ceph_connection *con = msg->con;
Alex Elder8921d112012-06-01 14:56:43 -05003176
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003177 if (!con) {
Alex Elder8921d112012-06-01 14:56:43 -05003178 dout("%s msg %p null con\n", __func__, msg);
Alex Elder8921d112012-06-01 14:56:43 -05003179 return; /* Message not in our possession */
3180 }
3181
Sage Weil350b1c32009-12-22 10:45:45 -08003182 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05003183 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00003184 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3185 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3186 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08003187
3188 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05003189 dout("%s %p msg %p revoked\n", __func__, con, msg);
3190 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08003191 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003192 front_len -
3193 middle_len -
3194 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08003195 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08003196 ceph_msg_put(con->in_msg);
3197 con->in_msg = NULL;
3198 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07003199 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08003200 } else {
Alex Elder8921d112012-06-01 14:56:43 -05003201 dout("%s %p in_msg %p msg %p no-op\n",
3202 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08003203 }
3204 mutex_unlock(&con->mutex);
3205}
3206
3207/*
Sage Weil31b80062009-10-06 11:31:13 -07003208 * Queue a keepalive byte to ensure the tcp connection is alive.
3209 */
3210void ceph_con_keepalive(struct ceph_connection *con)
3211{
Sage Weile00de342011-03-04 12:25:05 -08003212 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07003213 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08003214 clear_standby(con);
Ilya Dryomov4aac9222019-01-14 21:13:10 +01003215 con_flag_set(con, CON_FLAG_KEEPALIVE_PENDING);
Sage Weil00650932012-07-20 15:33:04 -07003216 mutex_unlock(&con->mutex);
Ilya Dryomov4aac9222019-01-14 21:13:10 +01003217
3218 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003219 queue_con(con);
3220}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003221EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07003222
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003223bool ceph_con_keepalive_expired(struct ceph_connection *con,
3224 unsigned long interval)
3225{
3226 if (interval > 0 &&
3227 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02003228 struct timespec64 now;
3229 struct timespec64 ts;
3230 ktime_get_real_ts64(&now);
3231 jiffies_to_timespec64(interval, &ts);
3232 ts = timespec64_add(con->last_keepalive_ack, ts);
3233 return timespec64_compare(&now, &ts) >= 0;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003234 }
3235 return false;
3236}
3237
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003238static struct ceph_msg_data *ceph_msg_data_add(struct ceph_msg *msg)
Alex Elder43794502013-03-01 18:00:16 -06003239{
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003240 BUG_ON(msg->num_data_items >= msg->max_data_items);
3241 return &msg->data[msg->num_data_items++];
Alex Elder6644ed72013-03-11 23:34:24 -05003242}
3243
3244static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3245{
Yan, Zhenge4339d282014-09-16 17:50:45 +08003246 if (data->type == CEPH_MSG_DATA_PAGELIST)
Alex Elder6644ed72013-03-11 23:34:24 -05003247 ceph_pagelist_release(data->pagelist);
Alex Elder43794502013-03-01 18:00:16 -06003248}
3249
Alex Elder90af3602013-04-05 14:46:01 -05003250void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Alex Elderf1baeb22013-03-07 15:38:26 -06003251 size_t length, size_t alignment)
Alex Elder02afca62013-02-14 12:16:43 -06003252{
Alex Elder6644ed72013-03-11 23:34:24 -05003253 struct ceph_msg_data *data;
3254
Alex Elder07aa1552013-03-04 18:29:06 -06003255 BUG_ON(!pages);
3256 BUG_ON(!length);
Alex Elder02afca62013-02-14 12:16:43 -06003257
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003258 data = ceph_msg_data_add(msg);
3259 data->type = CEPH_MSG_DATA_PAGES;
Alex Elder6644ed72013-03-11 23:34:24 -05003260 data->pages = pages;
3261 data->length = length;
3262 data->alignment = alignment & ~PAGE_MASK;
3263
Alex Elder5240d9f2013-03-14 14:09:06 -05003264 msg->data_length += length;
Alex Elder02afca62013-02-14 12:16:43 -06003265}
Alex Elder90af3602013-04-05 14:46:01 -05003266EXPORT_SYMBOL(ceph_msg_data_add_pages);
Sage Weil31b80062009-10-06 11:31:13 -07003267
Alex Elder90af3602013-04-05 14:46:01 -05003268void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
Alex Elder27fa8382013-02-14 12:16:43 -06003269 struct ceph_pagelist *pagelist)
3270{
Alex Elder6644ed72013-03-11 23:34:24 -05003271 struct ceph_msg_data *data;
3272
Alex Elder07aa1552013-03-04 18:29:06 -06003273 BUG_ON(!pagelist);
3274 BUG_ON(!pagelist->length);
Alex Elder27fa8382013-02-14 12:16:43 -06003275
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003276 data = ceph_msg_data_add(msg);
3277 data->type = CEPH_MSG_DATA_PAGELIST;
Ilya Dryomov894868332018-09-28 16:02:53 +02003278 refcount_inc(&pagelist->refcnt);
Alex Elder6644ed72013-03-11 23:34:24 -05003279 data->pagelist = pagelist;
3280
Alex Elder5240d9f2013-03-14 14:09:06 -05003281 msg->data_length += pagelist->length;
Alex Elder27fa8382013-02-14 12:16:43 -06003282}
Alex Elder90af3602013-04-05 14:46:01 -05003283EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
Alex Elder27fa8382013-02-14 12:16:43 -06003284
Alex Elderea965712013-04-05 14:46:01 -05003285#ifdef CONFIG_BLOCK
Ilya Dryomov5359a172018-01-20 10:30:10 +01003286void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
3287 u32 length)
Alex Elder27fa8382013-02-14 12:16:43 -06003288{
Alex Elder6644ed72013-03-11 23:34:24 -05003289 struct ceph_msg_data *data;
Alex Elder27fa8382013-02-14 12:16:43 -06003290
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003291 data = ceph_msg_data_add(msg);
3292 data->type = CEPH_MSG_DATA_BIO;
Ilya Dryomov5359a172018-01-20 10:30:10 +01003293 data->bio_pos = *bio_pos;
Alex Elderc851c492013-04-05 14:46:01 -05003294 data->bio_length = length;
Alex Elder6644ed72013-03-11 23:34:24 -05003295
Alex Elder5240d9f2013-03-14 14:09:06 -05003296 msg->data_length += length;
Alex Elder27fa8382013-02-14 12:16:43 -06003297}
Alex Elder90af3602013-04-05 14:46:01 -05003298EXPORT_SYMBOL(ceph_msg_data_add_bio);
Alex Elderea965712013-04-05 14:46:01 -05003299#endif /* CONFIG_BLOCK */
Alex Elder27fa8382013-02-14 12:16:43 -06003300
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01003301void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
3302 struct ceph_bvec_iter *bvec_pos)
3303{
3304 struct ceph_msg_data *data;
3305
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003306 data = ceph_msg_data_add(msg);
3307 data->type = CEPH_MSG_DATA_BVECS;
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01003308 data->bvec_pos = *bvec_pos;
3309
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01003310 msg->data_length += bvec_pos->iter.bi_size;
3311}
3312EXPORT_SYMBOL(ceph_msg_data_add_bvecs);
3313
Sage Weil31b80062009-10-06 11:31:13 -07003314/*
3315 * construct a new message with given type, size
3316 * the new msg has a ref count of 1.
3317 */
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003318struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
3319 gfp_t flags, bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07003320{
3321 struct ceph_msg *m;
3322
Alex Eldere3d5d632013-05-01 12:43:04 -05003323 m = kmem_cache_zalloc(ceph_msg_cache, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003324 if (m == NULL)
3325 goto out;
Alex Elder38941f82012-06-01 14:56:43 -05003326
Sage Weil31b80062009-10-06 11:31:13 -07003327 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07003328 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
Sage Weil31b80062009-10-06 11:31:13 -07003329 m->hdr.front_len = cpu_to_le32(front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003330
Alex Elder9516e452013-03-01 18:00:16 -06003331 INIT_LIST_HEAD(&m->list_head);
3332 kref_init(&m->kref);
Henry C Changca208922011-05-03 02:29:56 +00003333
Sage Weil31b80062009-10-06 11:31:13 -07003334 /* front */
3335 if (front_len) {
Ilya Dryomoveeb0bed2014-01-09 20:08:21 +02003336 m->front.iov_base = ceph_kvmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003337 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07003338 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07003339 front_len);
3340 goto out2;
3341 }
3342 } else {
3343 m->front.iov_base = NULL;
3344 }
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02003345 m->front_alloc_len = m->front.iov_len = front_len;
Sage Weil31b80062009-10-06 11:31:13 -07003346
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003347 if (max_data_items) {
3348 m->data = kmalloc_array(max_data_items, sizeof(*m->data),
3349 flags);
3350 if (!m->data)
3351 goto out2;
3352
3353 m->max_data_items = max_data_items;
3354 }
3355
Sage Weilbb257662010-04-01 16:07:23 -07003356 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003357 return m;
3358
3359out2:
3360 ceph_msg_put(m);
3361out:
Sage Weilb61c2762011-08-09 15:03:46 -07003362 if (!can_fail) {
3363 pr_err("msg_new can't create type %d front %d\n", type,
3364 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07003365 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07003366 } else {
3367 dout("msg_new can't create type %d front %d\n", type,
3368 front_len);
3369 }
Sage Weila79832f2010-04-01 16:06:19 -07003370 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003371}
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003372EXPORT_SYMBOL(ceph_msg_new2);
3373
3374struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3375 bool can_fail)
3376{
3377 return ceph_msg_new2(type, front_len, 0, flags, can_fail);
3378}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003379EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07003380
3381/*
Sage Weil31b80062009-10-06 11:31:13 -07003382 * Allocate "middle" portion of a message, if it is needed and wasn't
3383 * allocated by alloc_msg. This allows us to read a small fixed-size
3384 * per-type header in the front and then gracefully fail (i.e.,
3385 * propagate the error to the caller based on info in the front) when
3386 * the middle is too large.
3387 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08003388static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003389{
3390 int type = le16_to_cpu(msg->hdr.type);
3391 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3392
3393 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3394 ceph_msg_type_name(type), middle_len);
3395 BUG_ON(!middle_len);
3396 BUG_ON(msg->middle);
3397
Sage Weilb6c1d5b2009-12-07 12:17:17 -08003398 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07003399 if (!msg->middle)
3400 return -ENOMEM;
3401 return 0;
3402}
3403
Yehuda Sadeh24504182010-01-08 13:58:34 -08003404/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05003405 * Allocate a message for receiving an incoming message on a
3406 * connection, and save the result in con->in_msg. Uses the
3407 * connection's private alloc_msg op if available.
3408 *
Sage Weil4740a622012-07-30 18:19:30 -07003409 * Returns 0 on success, or a negative error code.
3410 *
3411 * On success, if we set *skip = 1:
3412 * - the next message should be skipped and ignored.
3413 * - con->in_msg == NULL
3414 * or if we set *skip = 0:
3415 * - con->in_msg is non-null.
3416 * On error (ENOMEM, EAGAIN, ...),
3417 * - con->in_msg == NULL
Yehuda Sadeh24504182010-01-08 13:58:34 -08003418 */
Sage Weil4740a622012-07-30 18:19:30 -07003419static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08003420{
Sage Weil4740a622012-07-30 18:19:30 -07003421 struct ceph_msg_header *hdr = &con->in_hdr;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003422 int middle_len = le32_to_cpu(hdr->middle_len);
Alex Elder1d866d12013-03-01 18:00:14 -06003423 struct ceph_msg *msg;
Sage Weil4740a622012-07-30 18:19:30 -07003424 int ret = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003425
Alex Elder1c20f2d2012-06-04 14:43:32 -05003426 BUG_ON(con->in_msg != NULL);
Alex Elder53ded492013-03-01 18:00:14 -06003427 BUG_ON(!con->ops->alloc_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003428
Alex Elder53ded492013-03-01 18:00:14 -06003429 mutex_unlock(&con->mutex);
3430 msg = con->ops->alloc_msg(con, hdr, skip);
3431 mutex_lock(&con->mutex);
3432 if (con->state != CON_STATE_OPEN) {
3433 if (msg)
Alex Elder1d866d12013-03-01 18:00:14 -06003434 ceph_msg_put(msg);
Alex Elder53ded492013-03-01 18:00:14 -06003435 return -EAGAIN;
3436 }
Alex Elder41375772013-03-05 09:25:10 -06003437 if (msg) {
3438 BUG_ON(*skip);
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003439 msg_con_set(msg, con);
Alex Elder41375772013-03-05 09:25:10 -06003440 con->in_msg = msg;
Alex Elder41375772013-03-05 09:25:10 -06003441 } else {
3442 /*
3443 * Null message pointer means either we should skip
3444 * this message or we couldn't allocate memory. The
3445 * former is not an error.
3446 */
3447 if (*skip)
3448 return 0;
Alex Elder41375772013-03-05 09:25:10 -06003449
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03003450 con->error_msg = "error allocating memory for incoming message";
Alex Elder53ded492013-03-01 18:00:14 -06003451 return -ENOMEM;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003452 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05003453 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08003454
Alex Elder1c20f2d2012-06-04 14:43:32 -05003455 if (middle_len && !con->in_msg->middle) {
3456 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003457 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05003458 ceph_msg_put(con->in_msg);
3459 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003460 }
3461 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08003462
Sage Weil4740a622012-07-30 18:19:30 -07003463 return ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003464}
3465
Sage Weil31b80062009-10-06 11:31:13 -07003466
3467/*
3468 * Free a generically kmalloc'd message.
3469 */
Ilya Dryomov0215e442014-06-20 14:14:41 +04003470static void ceph_msg_free(struct ceph_msg *m)
Sage Weil31b80062009-10-06 11:31:13 -07003471{
Ilya Dryomov0215e442014-06-20 14:14:41 +04003472 dout("%s %p\n", __func__, m);
Ilya Dryomov4965fc32014-10-23 16:32:57 +04003473 kvfree(m->front.iov_base);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003474 kfree(m->data);
Alex Eldere3d5d632013-05-01 12:43:04 -05003475 kmem_cache_free(ceph_msg_cache, m);
Sage Weil31b80062009-10-06 11:31:13 -07003476}
3477
Ilya Dryomov0215e442014-06-20 14:14:41 +04003478static void ceph_msg_release(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07003479{
Sage Weilc2e552e2009-12-07 15:55:05 -08003480 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003481 int i;
Sage Weil31b80062009-10-06 11:31:13 -07003482
Ilya Dryomov0215e442014-06-20 14:14:41 +04003483 dout("%s %p\n", __func__, m);
Sage Weilc2e552e2009-12-07 15:55:05 -08003484 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07003485
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003486 msg_con_set(m, NULL);
3487
Sage Weilc2e552e2009-12-07 15:55:05 -08003488 /* drop middle, data, if any */
3489 if (m->middle) {
3490 ceph_buffer_put(m->middle);
3491 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003492 }
Alex Elder5240d9f2013-03-14 14:09:06 -05003493
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003494 for (i = 0; i < m->num_data_items; i++)
3495 ceph_msg_data_destroy(&m->data[i]);
Sage Weil58bb3b32009-12-23 12:12:31 -08003496
Sage Weilc2e552e2009-12-07 15:55:05 -08003497 if (m->pool)
3498 ceph_msgpool_put(m->pool, m);
3499 else
Ilya Dryomov0215e442014-06-20 14:14:41 +04003500 ceph_msg_free(m);
Sage Weil31b80062009-10-06 11:31:13 -07003501}
Ilya Dryomov0215e442014-06-20 14:14:41 +04003502
3503struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3504{
3505 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003506 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003507 kref_get(&msg->kref);
3508 return msg;
3509}
3510EXPORT_SYMBOL(ceph_msg_get);
3511
3512void ceph_msg_put(struct ceph_msg *msg)
3513{
3514 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003515 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003516 kref_put(&msg->kref, ceph_msg_release);
3517}
3518EXPORT_SYMBOL(ceph_msg_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003519
3520void ceph_msg_dump(struct ceph_msg *msg)
3521{
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02003522 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3523 msg->front_alloc_len, msg->data_length);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003524 print_hex_dump(KERN_DEBUG, "header: ",
3525 DUMP_PREFIX_OFFSET, 16, 1,
3526 &msg->hdr, sizeof(msg->hdr), true);
3527 print_hex_dump(KERN_DEBUG, " front: ",
3528 DUMP_PREFIX_OFFSET, 16, 1,
3529 msg->front.iov_base, msg->front.iov_len, true);
3530 if (msg->middle)
3531 print_hex_dump(KERN_DEBUG, "middle: ",
3532 DUMP_PREFIX_OFFSET, 16, 1,
3533 msg->middle->vec.iov_base,
3534 msg->middle->vec.iov_len, true);
3535 print_hex_dump(KERN_DEBUG, "footer: ",
3536 DUMP_PREFIX_OFFSET, 16, 1,
3537 &msg->footer, sizeof(msg->footer), true);
3538}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003539EXPORT_SYMBOL(ceph_msg_dump);