blob: 85d20372f923cf4a52f39c83737e648b8e012b6e [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
Alex Elderc9ffc772013-02-20 10:25:12 -060085static bool con_flag_valid(unsigned long con_flag)
86{
87 switch (con_flag) {
Ilya Dryomov3fefd432020-11-09 14:56:36 +010088 case CEPH_CON_F_LOSSYTX:
89 case CEPH_CON_F_KEEPALIVE_PENDING:
90 case CEPH_CON_F_WRITE_PENDING:
91 case CEPH_CON_F_SOCK_CLOSED:
92 case CEPH_CON_F_BACKOFF:
Alex Elderc9ffc772013-02-20 10:25:12 -060093 return true;
94 default:
95 return false;
96 }
97}
98
Ilya Dryomov6503e0b2020-11-09 16:29:47 +010099void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
Alex Elderc9ffc772013-02-20 10:25:12 -0600100{
101 BUG_ON(!con_flag_valid(con_flag));
102
103 clear_bit(con_flag, &con->flags);
104}
105
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100106void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag)
Alex Elderc9ffc772013-02-20 10:25:12 -0600107{
108 BUG_ON(!con_flag_valid(con_flag));
109
110 set_bit(con_flag, &con->flags);
111}
112
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100113bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag)
Alex Elderc9ffc772013-02-20 10:25:12 -0600114{
115 BUG_ON(!con_flag_valid(con_flag));
116
117 return test_bit(con_flag, &con->flags);
118}
119
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100120bool ceph_con_flag_test_and_clear(struct ceph_connection *con,
121 unsigned long con_flag)
Alex Elderc9ffc772013-02-20 10:25:12 -0600122{
123 BUG_ON(!con_flag_valid(con_flag));
124
125 return test_and_clear_bit(con_flag, &con->flags);
126}
127
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100128bool ceph_con_flag_test_and_set(struct ceph_connection *con,
129 unsigned long con_flag)
Alex Elderc9ffc772013-02-20 10:25:12 -0600130{
131 BUG_ON(!con_flag_valid(con_flag));
132
133 return test_and_set_bit(con_flag, &con->flags);
134}
135
Alex Eldere3d5d632013-05-01 12:43:04 -0500136/* Slab caches for frequently-allocated structures */
137
138static struct kmem_cache *ceph_msg_cache;
139
Sage Weil31b80062009-10-06 11:31:13 -0700140/* static tag bytes (protocol control messages) */
141static char tag_msg = CEPH_MSGR_TAG_MSG;
142static char tag_ack = CEPH_MSGR_TAG_ACK;
143static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800144static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
Sage Weil31b80062009-10-06 11:31:13 -0700145
Sage Weila6a53492010-04-13 14:07:07 -0700146#ifdef CONFIG_LOCKDEP
147static struct lock_class_key socket_class;
148#endif
149
Sage Weil31b80062009-10-06 11:31:13 -0700150static void queue_con(struct ceph_connection *con);
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400151static void cancel_con(struct ceph_connection *con);
Ilya Dryomov68931622015-07-03 15:44:41 +0300152static void ceph_con_workfn(struct work_struct *);
Alex Elder93209262013-02-19 12:25:57 -0600153static void con_fault(struct ceph_connection *con);
Sage Weil31b80062009-10-06 11:31:13 -0700154
Sage Weil31b80062009-10-06 11:31:13 -0700155/*
Alex Elderf64a9312012-01-23 15:49:27 -0600156 * Nicely render a sockaddr as a string. An array of formatted
157 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -0700158 */
Alex Elderf64a9312012-01-23 15:49:27 -0600159#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
160#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
161#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
162#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
163
164static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
165static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -0700166
Ilya Dryomov699921d2020-11-09 14:37:06 +0100167struct page *ceph_zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -0600168
Jeff Laytonb726ec92019-05-06 09:38:47 -0400169const char *ceph_pr_addr(const struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700170{
171 int i;
172 char *s;
Jeff Laytonb726ec92019-05-06 09:38:47 -0400173 struct sockaddr_storage ss = addr->in_addr; /* align */
174 struct sockaddr_in *in4 = (struct sockaddr_in *)&ss;
175 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&ss;
Sage Weil31b80062009-10-06 11:31:13 -0700176
Alex Elderf64a9312012-01-23 15:49:27 -0600177 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700178 s = addr_str[i];
179
Jeff Laytonb726ec92019-05-06 09:38:47 -0400180 switch (ss.ss_family) {
Sage Weil31b80062009-10-06 11:31:13 -0700181 case AF_INET:
Jeff Laytond3c3c0a2019-06-17 06:57:25 -0400182 snprintf(s, MAX_ADDR_STR_LEN, "(%d)%pI4:%hu",
183 le32_to_cpu(addr->type), &in4->sin_addr,
Alex Elderbd406142012-01-23 15:49:27 -0600184 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700185 break;
186
187 case AF_INET6:
Jeff Laytond3c3c0a2019-06-17 06:57:25 -0400188 snprintf(s, MAX_ADDR_STR_LEN, "(%d)[%pI6c]:%hu",
189 le32_to_cpu(addr->type), &in6->sin6_addr,
Alex Elderbd406142012-01-23 15:49:27 -0600190 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700191 break;
192
193 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600194 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
Jeff Laytonb726ec92019-05-06 09:38:47 -0400195 ss.ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700196 }
197
198 return s;
199}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700200EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700201
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100202void ceph_encode_my_addr(struct ceph_messenger *msgr)
Sage Weil63f2d212009-11-03 15:17:56 -0800203{
204 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
Jeff Layton2c66de52019-06-17 09:24:31 -0400205 ceph_encode_banner_addr(&msgr->my_enc_addr);
Sage Weil63f2d212009-11-03 15:17:56 -0800206}
207
Sage Weil31b80062009-10-06 11:31:13 -0700208/*
209 * work queue for all reading and writing to/from the socket.
210 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600211static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700212
Alex Eldere3d5d632013-05-01 12:43:04 -0500213static int ceph_msgr_slab_init(void)
214{
215 BUG_ON(ceph_msg_cache);
Geliang Tang5ee61e92016-03-13 15:18:39 +0800216 ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
Alex Elder81b36be2013-05-01 12:43:04 -0500217 if (!ceph_msg_cache)
218 return -ENOMEM;
219
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200220 return 0;
Alex Eldere3d5d632013-05-01 12:43:04 -0500221}
222
223static void ceph_msgr_slab_exit(void)
224{
225 BUG_ON(!ceph_msg_cache);
226 kmem_cache_destroy(ceph_msg_cache);
227 ceph_msg_cache = NULL;
228}
229
Alex Elder15417162013-02-19 12:25:56 -0600230static void _ceph_msgr_exit(void)
Alex Elder6173d1f2012-02-14 14:05:33 -0600231{
Alex Elderd3002b92012-02-14 14:05:33 -0600232 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600233 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600234 ceph_msgr_wq = NULL;
235 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600236
Ilya Dryomov699921d2020-11-09 14:37:06 +0100237 BUG_ON(!ceph_zero_page);
238 put_page(ceph_zero_page);
239 ceph_zero_page = NULL;
Benoît Canetd920ff62015-06-25 21:02:57 +0200240
241 ceph_msgr_slab_exit();
Alex Elder6173d1f2012-02-14 14:05:33 -0600242}
243
Chengguang Xu57a35df2018-03-10 20:32:05 +0800244int __init ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700245{
Benoît Canetd920ff62015-06-25 21:02:57 +0200246 if (ceph_msgr_slab_init())
247 return -ENOMEM;
248
Ilya Dryomov699921d2020-11-09 14:37:06 +0100249 BUG_ON(ceph_zero_page);
250 ceph_zero_page = ZERO_PAGE(0);
251 get_page(ceph_zero_page);
Alex Elder57666512012-01-23 15:49:27 -0600252
Ilya Dryomovf9865f02014-10-10 16:39:05 +0400253 /*
254 * The number of active work items is limited by the number of
255 * connections, so leave @max_active at default.
256 */
257 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600258 if (ceph_msgr_wq)
259 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600260
Alex Elder6173d1f2012-02-14 14:05:33 -0600261 pr_err("msgr_init failed to create workqueue\n");
262 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600263
Alex Elder6173d1f2012-02-14 14:05:33 -0600264 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700265}
266
267void ceph_msgr_exit(void)
268{
Alex Elder57666512012-01-23 15:49:27 -0600269 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600270
Alex Elder6173d1f2012-02-14 14:05:33 -0600271 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700272}
273
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700274void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700275{
276 flush_workqueue(ceph_msgr_wq);
277}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700278EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700279
Alex Elderce2c8902012-05-22 22:15:49 -0500280/* Connection socket state transition functions */
281
282static void con_sock_state_init(struct ceph_connection *con)
283{
284 int old_state;
285
286 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
287 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
288 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700289 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
290 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500291}
292
293static void con_sock_state_connecting(struct ceph_connection *con)
294{
295 int old_state;
296
297 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
298 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
299 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700300 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
301 CON_SOCK_STATE_CONNECTING);
Alex Elderce2c8902012-05-22 22:15:49 -0500302}
303
304static void con_sock_state_connected(struct ceph_connection *con)
305{
306 int old_state;
307
308 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
309 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
310 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700311 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
312 CON_SOCK_STATE_CONNECTED);
Alex Elderce2c8902012-05-22 22:15:49 -0500313}
314
315static void con_sock_state_closing(struct ceph_connection *con)
316{
317 int old_state;
318
319 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
320 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
321 old_state != CON_SOCK_STATE_CONNECTED &&
322 old_state != CON_SOCK_STATE_CLOSING))
323 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700324 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
325 CON_SOCK_STATE_CLOSING);
Alex Elderce2c8902012-05-22 22:15:49 -0500326}
327
328static void con_sock_state_closed(struct ceph_connection *con)
329{
330 int old_state;
331
332 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
333 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
Sage Weilfbb85a42012-06-27 12:31:02 -0700334 old_state != CON_SOCK_STATE_CLOSING &&
Sage Weil8007b8d2012-07-30 18:16:16 -0700335 old_state != CON_SOCK_STATE_CONNECTING &&
336 old_state != CON_SOCK_STATE_CLOSED))
Alex Elderce2c8902012-05-22 22:15:49 -0500337 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700338 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
339 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500340}
Sage Weila922d382010-05-29 09:41:23 -0700341
Sage Weil31b80062009-10-06 11:31:13 -0700342/*
343 * socket callback functions
344 */
345
346/* data available on socket, or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400347static void ceph_sock_data_ready(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700348{
Alex Elderbd406142012-01-23 15:49:27 -0600349 struct ceph_connection *con = sk->sk_user_data;
Guanjun Hea2a32582012-07-08 19:50:33 -0700350 if (atomic_read(&con->msgr->stopping)) {
351 return;
352 }
Alex Elderbd406142012-01-23 15:49:27 -0600353
Sage Weil31b80062009-10-06 11:31:13 -0700354 if (sk->sk_state != TCP_CLOSE_WAIT) {
Ilya Dryomov30be7802020-11-09 14:11:26 +0100355 dout("%s %p state = %d, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700356 con, con->state);
357 queue_con(con);
358 }
359}
360
361/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500362static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700363{
Alex Elderd3002b92012-02-14 14:05:33 -0600364 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700365
Jim Schutt182fac22012-02-29 08:30:58 -0700366 /* only queue to workqueue if there is data we want to write,
367 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500368 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700369 * doesn't get called again until try_write() fills the socket
370 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
371 * and net/core/stream.c:sk_stream_write_space().
372 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100373 if (ceph_con_flag_test(con, CEPH_CON_F_WRITE_PENDING)) {
Eric Dumazet64dc6132013-07-22 20:26:31 -0700374 if (sk_stream_is_writeable(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500375 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700376 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
377 queue_con(con);
378 }
Sage Weil31b80062009-10-06 11:31:13 -0700379 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500380 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700381 }
Sage Weil31b80062009-10-06 11:31:13 -0700382}
383
384/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500385static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700386{
Alex Elderbd406142012-01-23 15:49:27 -0600387 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700388
Ilya Dryomov30be7802020-11-09 14:11:26 +0100389 dout("%s %p state = %d sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700390 con, con->state, sk->sk_state);
391
Sage Weil31b80062009-10-06 11:31:13 -0700392 switch (sk->sk_state) {
393 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500394 dout("%s TCP_CLOSE\n", __func__);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500395 fallthrough;
Sage Weil31b80062009-10-06 11:31:13 -0700396 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500397 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500398 con_sock_state_closing(con);
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100399 ceph_con_flag_set(con, CEPH_CON_F_SOCK_CLOSED);
Alex Elderd65c9e02012-06-20 21:53:53 -0500400 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700401 break;
402 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500403 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500404 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700405 queue_con(con);
406 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600407 default: /* Everything else is uninteresting */
408 break;
Sage Weil31b80062009-10-06 11:31:13 -0700409 }
410}
411
412/*
413 * set up socket callbacks
414 */
415static void set_sock_callbacks(struct socket *sock,
416 struct ceph_connection *con)
417{
418 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600419 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500420 sk->sk_data_ready = ceph_sock_data_ready;
421 sk->sk_write_space = ceph_sock_write_space;
422 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700423}
424
425
426/*
427 * socket helpers
428 */
429
430/*
431 * initiate connection to a remote socket.
432 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100433int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700434{
Jeff Laytoncede1852019-05-06 09:38:46 -0400435 struct sockaddr_storage ss = con->peer_addr.in_addr; /* align */
Sage Weil31b80062009-10-06 11:31:13 -0700436 struct socket *sock;
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100437 unsigned int noio_flag;
Sage Weil31b80062009-10-06 11:31:13 -0700438 int ret;
439
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100440 dout("%s con %p peer_addr %s\n", __func__, con,
441 ceph_pr_addr(&con->peer_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700442 BUG_ON(con->sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100443
444 /* sock_create_kern() allocates with GFP_KERNEL */
445 noio_flag = memalloc_noio_save();
Jeff Laytoncede1852019-05-06 09:38:46 -0400446 ret = sock_create_kern(read_pnet(&con->msgr->net), ss.ss_family,
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -0500447 SOCK_STREAM, IPPROTO_TCP, &sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100448 memalloc_noio_restore(noio_flag);
Sage Weil31b80062009-10-06 11:31:13 -0700449 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600450 return ret;
Ilya Dryomov6d7fdb02015-04-02 14:40:58 +0300451 sock->sk->sk_allocation = GFP_NOFS;
Sage Weil31b80062009-10-06 11:31:13 -0700452
Sage Weila6a53492010-04-13 14:07:07 -0700453#ifdef CONFIG_LOCKDEP
454 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
455#endif
456
Sage Weil31b80062009-10-06 11:31:13 -0700457 set_sock_callbacks(sock, con);
458
Sage Weil89a86be2012-06-09 14:19:21 -0700459 con_sock_state_connecting(con);
Jeff Laytoncede1852019-05-06 09:38:46 -0400460 ret = sock->ops->connect(sock, (struct sockaddr *)&ss, sizeof(ss),
Sage Weilf91d3472010-07-01 15:18:31 -0700461 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700462 if (ret == -EINPROGRESS) {
463 dout("connect %s EINPROGRESS sk_state = %u\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -0400464 ceph_pr_addr(&con->peer_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700465 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600466 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700467 pr_err("connect %s error %d\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -0400468 ceph_pr_addr(&con->peer_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700469 sock_release(sock);
Alex Elder41617d02012-02-14 14:05:33 -0600470 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600471 }
Mike Christie89baaa52014-10-16 01:50:19 -0500472
Christoph Hellwig12abc5e2020-05-28 07:12:19 +0200473 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY))
474 tcp_sock_set_nodelay(sock->sk);
Chaitanya Huilgolba988f82015-01-23 16:41:25 +0530475
Alex Eldera5bc3122012-01-23 15:49:27 -0600476 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600477 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700478}
479
Ilya Dryomove5c93882018-04-27 18:58:47 +0200480/*
481 * If @buf is NULL, discard up to @len bytes.
482 */
Sage Weil31b80062009-10-06 11:31:13 -0700483static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
484{
485 struct kvec iov = {buf, len};
486 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800487 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700488
Ilya Dryomove5c93882018-04-27 18:58:47 +0200489 if (!buf)
490 msg.msg_flags |= MSG_TRUNC;
491
David Howellsaa563d72018-10-20 00:57:56 +0100492 iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, len);
Al Viro100803a2015-11-15 00:12:48 -0500493 r = sock_recvmsg(sock, &msg, msg.msg_flags);
Sage Weil98bdb0a2011-01-25 08:17:48 -0800494 if (r == -EAGAIN)
495 r = 0;
496 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700497}
498
Alex Elderafb3d902013-03-08 20:58:59 -0600499static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
500 int page_offset, size_t length)
501{
Al Viro100803a2015-11-15 00:12:48 -0500502 struct bio_vec bvec = {
503 .bv_page = page,
504 .bv_offset = page_offset,
505 .bv_len = length
506 };
507 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
508 int r;
Alex Elderafb3d902013-03-08 20:58:59 -0600509
510 BUG_ON(page_offset + length > PAGE_SIZE);
David Howellsaa563d72018-10-20 00:57:56 +0100511 iov_iter_bvec(&msg.msg_iter, READ, &bvec, 1, length);
Al Viro100803a2015-11-15 00:12:48 -0500512 r = sock_recvmsg(sock, &msg, msg.msg_flags);
513 if (r == -EAGAIN)
514 r = 0;
515 return r;
Alex Elderafb3d902013-03-08 20:58:59 -0600516}
517
Sage Weil31b80062009-10-06 11:31:13 -0700518/*
519 * write something. @more is true if caller will be sending more data
520 * shortly.
521 */
522static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
Ilya Dryomov87349cd2018-11-21 18:56:40 +0100523 size_t kvlen, size_t len, bool more)
Sage Weil31b80062009-10-06 11:31:13 -0700524{
525 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800526 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700527
528 if (more)
529 msg.msg_flags |= MSG_MORE;
530 else
531 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
532
Sage Weil42961d22011-01-25 08:19:34 -0800533 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
534 if (r == -EAGAIN)
535 r = 0;
536 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700537}
538
Ilya Dryomov433b0a12018-11-20 15:44:00 +0100539/*
540 * @more: either or both of MSG_MORE and MSG_SENDPAGE_NOTLAST
541 */
Chunwei Chen178eda292014-04-23 12:35:09 +0800542static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
Ilya Dryomov433b0a12018-11-20 15:44:00 +0100543 int offset, size_t size, int more)
Chunwei Chen178eda292014-04-23 12:35:09 +0800544{
Ilya Dryomov3239eb52018-11-16 11:58:19 +0100545 ssize_t (*sendpage)(struct socket *sock, struct page *page,
546 int offset, size_t size, int flags);
Ilya Dryomov433b0a12018-11-20 15:44:00 +0100547 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | more;
Chunwei Chen178eda292014-04-23 12:35:09 +0800548 int ret;
Chunwei Chen178eda292014-04-23 12:35:09 +0800549
Ilya Dryomov7e241f62018-11-08 15:55:37 +0100550 /*
551 * sendpage cannot properly handle pages with page_count == 0,
552 * we need to fall back to sendmsg if that's the case.
553 *
554 * Same goes for slab pages: skb_can_coalesce() allows
555 * coalescing neighboring slab objects into a single frag which
556 * triggers one of hardened usercopy checks.
557 */
Coly Li40efc4d2020-10-02 16:27:34 +0800558 if (sendpage_ok(page))
Ilya Dryomov3239eb52018-11-16 11:58:19 +0100559 sendpage = sock->ops->sendpage;
Al Viro61ff6e92016-01-09 20:55:45 -0500560 else
Ilya Dryomov3239eb52018-11-16 11:58:19 +0100561 sendpage = sock_no_sendpage;
Al Viro61ff6e92016-01-09 20:55:45 -0500562
Ilya Dryomov3239eb52018-11-16 11:58:19 +0100563 ret = sendpage(sock, page, offset, size, flags);
Al Viro61ff6e92016-01-09 20:55:45 -0500564 if (ret == -EAGAIN)
565 ret = 0;
Chunwei Chen178eda292014-04-23 12:35:09 +0800566
567 return ret;
568}
Sage Weil31b80062009-10-06 11:31:13 -0700569
570/*
571 * Shutdown/close the socket for the given connection.
572 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100573int ceph_con_close_socket(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700574{
Sage Weil8007b8d2012-07-30 18:16:16 -0700575 int rc = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700576
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100577 dout("%s con %p sock %p\n", __func__, con, con->sock);
Sage Weil8007b8d2012-07-30 18:16:16 -0700578 if (con->sock) {
579 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
580 sock_release(con->sock);
581 con->sock = NULL;
582 }
Alex Elder456ea462012-06-20 21:53:53 -0500583
584 /*
Sage Weil4a861692012-07-20 17:29:55 -0700585 * Forcibly clear the SOCK_CLOSED flag. It gets set
Alex Elder456ea462012-06-20 21:53:53 -0500586 * independent of the connection mutex, and we could have
587 * received a socket close event before we had the chance to
588 * shut the socket down.
589 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100590 ceph_con_flag_clear(con, CEPH_CON_F_SOCK_CLOSED);
Sage Weil8007b8d2012-07-30 18:16:16 -0700591
Alex Elderce2c8902012-05-22 22:15:49 -0500592 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700593 return rc;
594}
595
Ilya Dryomov3596f4c2020-11-06 17:07:07 +0100596static void ceph_con_reset_protocol(struct ceph_connection *con)
597{
598 dout("%s con %p\n", __func__, con);
599
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100600 ceph_con_close_socket(con);
Ilya Dryomov3596f4c2020-11-06 17:07:07 +0100601 if (con->in_msg) {
602 WARN_ON(con->in_msg->con != con);
603 ceph_msg_put(con->in_msg);
604 con->in_msg = NULL;
605 }
606 if (con->out_msg) {
607 WARN_ON(con->out_msg->con != con);
608 ceph_msg_put(con->out_msg);
609 con->out_msg = NULL;
610 }
611
612 con->out_skip = 0;
613}
614
Sage Weil31b80062009-10-06 11:31:13 -0700615/*
616 * Reset a connection. Discard all incoming and outgoing messages
617 * and clear *_seq state.
618 */
619static void ceph_msg_remove(struct ceph_msg *msg)
620{
621 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500622
Sage Weil31b80062009-10-06 11:31:13 -0700623 ceph_msg_put(msg);
624}
625static void ceph_msg_remove_list(struct list_head *head)
626{
627 while (!list_empty(head)) {
628 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
629 list_head);
630 ceph_msg_remove(msg);
631 }
632}
633
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100634void ceph_con_reset_session(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700635{
Ilya Dryomov5963c3d2020-11-06 19:04:30 +0100636 dout("%s con %p\n", __func__, con);
Ilya Dryomov3596f4c2020-11-06 17:07:07 +0100637
638 WARN_ON(con->in_msg);
639 WARN_ON(con->out_msg);
Sage Weil31b80062009-10-06 11:31:13 -0700640 ceph_msg_remove_list(&con->out_queue);
641 ceph_msg_remove_list(&con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700642 con->out_seq = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700643 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700644 con->in_seq_acked = 0;
Ilya Dryomova3da0572020-11-11 14:08:59 +0100645
646 con->connect_seq = 0;
647 con->peer_global_seq = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700648}
649
650/*
651 * mark a peer down. drop any open connections.
652 */
653void ceph_con_close(struct ceph_connection *con)
654{
Sage Weil8c50c812012-07-30 16:24:37 -0700655 mutex_lock(&con->mutex);
Jeff Laytonb726ec92019-05-06 09:38:47 -0400656 dout("con_close %p peer %s\n", con, ceph_pr_addr(&con->peer_addr));
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +0100657 con->state = CEPH_CON_S_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500658
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100659 ceph_con_flag_clear(con, CEPH_CON_F_LOSSYTX); /* so we retry next
660 connect */
661 ceph_con_flag_clear(con, CEPH_CON_F_KEEPALIVE_PENDING);
662 ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
663 ceph_con_flag_clear(con, CEPH_CON_F_BACKOFF);
Alex Eldera5988c42012-05-29 11:04:58 -0500664
Ilya Dryomov3596f4c2020-11-06 17:07:07 +0100665 ceph_con_reset_protocol(con);
Ilya Dryomov5963c3d2020-11-06 19:04:30 +0100666 ceph_con_reset_session(con);
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400667 cancel_con(con);
Sage Weilec302642009-12-22 10:43:42 -0800668 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700669}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700670EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700671
672/*
Sage Weil31b80062009-10-06 11:31:13 -0700673 * Reopen a closed connection, with a new peer address.
674 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700675void ceph_con_open(struct ceph_connection *con,
676 __u8 entity_type, __u64 entity_num,
677 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700678{
Sage Weil54691552012-07-30 16:21:40 -0700679 mutex_lock(&con->mutex);
Jeff Laytonb726ec92019-05-06 09:38:47 -0400680 dout("con_open %p %s\n", con, ceph_pr_addr(addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700681
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +0100682 WARN_ON(con->state != CEPH_CON_S_CLOSED);
683 con->state = CEPH_CON_S_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500684
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700685 con->peer_name.type = (__u8) entity_type;
686 con->peer_name.num = cpu_to_le64(entity_num);
687
Sage Weil31b80062009-10-06 11:31:13 -0700688 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800689 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700690 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700691 queue_con(con);
692}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700693EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700694
695/*
Sage Weil87b315a2010-03-22 14:51:18 -0700696 * return true if this connection ever successfully opened
697 */
698bool ceph_con_opened(struct ceph_connection *con)
699{
700 return con->connect_seq > 0;
701}
702
703/*
Sage Weil31b80062009-10-06 11:31:13 -0700704 * initialize a new connection.
705 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500706void ceph_con_init(struct ceph_connection *con, void *private,
707 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700708 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700709{
710 dout("con_init %p\n", con);
711 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500712 con->private = private;
713 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700714 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500715
716 con_sock_state_init(con);
717
Sage Weilec302642009-12-22 10:43:42 -0800718 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700719 INIT_LIST_HEAD(&con->out_queue);
720 INIT_LIST_HEAD(&con->out_sent);
Ilya Dryomov68931622015-07-03 15:44:41 +0300721 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
Alex Eldera5988c42012-05-29 11:04:58 -0500722
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +0100723 con->state = CEPH_CON_S_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700724}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700725EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700726
727
728/*
729 * We maintain a global counter to order connection attempts. Get
730 * a unique seq greater than @gt.
731 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100732u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt)
Sage Weil31b80062009-10-06 11:31:13 -0700733{
734 u32 ret;
735
736 spin_lock(&msgr->global_seq_lock);
737 if (msgr->global_seq < gt)
738 msgr->global_seq = gt;
739 ret = ++msgr->global_seq;
740 spin_unlock(&msgr->global_seq_lock);
741 return ret;
742}
743
Ilya Dryomov02471922020-10-13 17:38:53 +0200744/*
745 * Discard messages that have been acked by the server.
746 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100747void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq)
Ilya Dryomov02471922020-10-13 17:38:53 +0200748{
749 struct ceph_msg *msg;
750 u64 seq;
751
752 dout("%s con %p ack_seq %llu\n", __func__, con, ack_seq);
753 while (!list_empty(&con->out_sent)) {
754 msg = list_first_entry(&con->out_sent, struct ceph_msg,
755 list_head);
756 WARN_ON(msg->needs_out_seq);
757 seq = le64_to_cpu(msg->hdr.seq);
758 if (seq > ack_seq)
759 break;
760
761 dout("%s con %p discarding msg %p seq %llu\n", __func__, con,
762 msg, seq);
763 ceph_msg_remove(msg);
764 }
765}
766
767/*
768 * Discard messages that have been requeued in con_fault(), up to
769 * reconnect_seq. This avoids gratuitously resending messages that
770 * the server had received and handled prior to reconnect.
771 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +0100772void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq)
Ilya Dryomov02471922020-10-13 17:38:53 +0200773{
774 struct ceph_msg *msg;
775 u64 seq;
776
777 dout("%s con %p reconnect_seq %llu\n", __func__, con, reconnect_seq);
778 while (!list_empty(&con->out_queue)) {
779 msg = list_first_entry(&con->out_queue, struct ceph_msg,
780 list_head);
781 if (msg->needs_out_seq)
782 break;
783 seq = le64_to_cpu(msg->hdr.seq);
784 if (seq > reconnect_seq)
785 break;
786
787 dout("%s con %p discarding msg %p seq %llu\n", __func__, con,
788 msg, seq);
789 ceph_msg_remove(msg);
790 }
791}
792
Alex Eldere2200422012-05-23 14:35:23 -0500793static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600794{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300795 BUG_ON(con->out_skip);
796
Alex Elder859eb792012-02-14 14:05:33 -0600797 con->out_kvec_left = 0;
798 con->out_kvec_bytes = 0;
799 con->out_kvec_cur = &con->out_kvec[0];
800}
801
Alex Eldere2200422012-05-23 14:35:23 -0500802static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600803 size_t size, void *data)
804{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300805 int index = con->out_kvec_left;
Alex Elder859eb792012-02-14 14:05:33 -0600806
Ilya Dryomov67645d72015-12-28 13:18:34 +0300807 BUG_ON(con->out_skip);
Alex Elder859eb792012-02-14 14:05:33 -0600808 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
809
810 con->out_kvec[index].iov_len = size;
811 con->out_kvec[index].iov_base = data;
812 con->out_kvec_left++;
813 con->out_kvec_bytes += size;
814}
Sage Weil31b80062009-10-06 11:31:13 -0700815
Ilya Dryomov67645d72015-12-28 13:18:34 +0300816/*
817 * Chop off a kvec from the end. Return residual number of bytes for
818 * that kvec, i.e. how many bytes would have been written if the kvec
819 * hadn't been nuked.
820 */
821static int con_out_kvec_skip(struct ceph_connection *con)
822{
823 int off = con->out_kvec_cur - con->out_kvec;
824 int skip = 0;
825
826 if (con->out_kvec_bytes > 0) {
827 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
828 BUG_ON(con->out_kvec_bytes < skip);
829 BUG_ON(!con->out_kvec_left);
830 con->out_kvec_bytes -= skip;
831 con->out_kvec_left--;
832 }
833
834 return skip;
835}
836
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500837#ifdef CONFIG_BLOCK
Alex Elder6aaa4512013-03-06 23:39:39 -0600838
839/*
840 * For a bio data item, a piece is whatever remains of the next
841 * entry in the current bio iovec, or the first entry in the next
842 * bio in the list.
843 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500844static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500845 size_t length)
Alex Elder6aaa4512013-03-06 23:39:39 -0600846{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500847 struct ceph_msg_data *data = cursor->data;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100848 struct ceph_bio_iter *it = &cursor->bio_iter;
Alex Elder6aaa4512013-03-06 23:39:39 -0600849
Ilya Dryomov5359a172018-01-20 10:30:10 +0100850 cursor->resid = min_t(size_t, length, data->bio_length);
851 *it = data->bio_pos;
852 if (cursor->resid < it->iter.bi_size)
853 it->iter.bi_size = cursor->resid;
Alex Elder6aaa4512013-03-06 23:39:39 -0600854
Ilya Dryomov5359a172018-01-20 10:30:10 +0100855 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
856 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600857}
858
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500859static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
Alex Elder6aaa4512013-03-06 23:39:39 -0600860 size_t *page_offset,
861 size_t *length)
862{
Ilya Dryomov5359a172018-01-20 10:30:10 +0100863 struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio,
864 cursor->bio_iter.iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600865
Ilya Dryomov5359a172018-01-20 10:30:10 +0100866 *page_offset = bv.bv_offset;
867 *length = bv.bv_len;
868 return bv.bv_page;
Alex Elder6aaa4512013-03-06 23:39:39 -0600869}
870
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500871static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
872 size_t bytes)
Alex Elder6aaa4512013-03-06 23:39:39 -0600873{
Ilya Dryomov5359a172018-01-20 10:30:10 +0100874 struct ceph_bio_iter *it = &cursor->bio_iter;
Ilya Dryomov187df762019-03-22 22:14:19 +0100875 struct page *page = bio_iter_page(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600876
Ilya Dryomov5359a172018-01-20 10:30:10 +0100877 BUG_ON(bytes > cursor->resid);
878 BUG_ON(bytes > bio_iter_len(it->bio, it->iter));
Alex Elder25aff7c2013-03-11 23:34:22 -0500879 cursor->resid -= bytes;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100880 bio_advance_iter(it->bio, &it->iter, bytes);
Kent Overstreetf38a5182013-08-07 14:30:24 -0700881
Ilya Dryomov5359a172018-01-20 10:30:10 +0100882 if (!cursor->resid) {
883 BUG_ON(!cursor->last_piece);
884 return false; /* no more data */
885 }
Kent Overstreetf38a5182013-08-07 14:30:24 -0700886
Ilya Dryomov187df762019-03-22 22:14:19 +0100887 if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done &&
888 page == bio_iter_page(it->bio, it->iter)))
Alex Elder6aaa4512013-03-06 23:39:39 -0600889 return false; /* more bytes to process in this segment */
890
Ilya Dryomov5359a172018-01-20 10:30:10 +0100891 if (!it->iter.bi_size) {
892 it->bio = it->bio->bi_next;
893 it->iter = it->bio->bi_iter;
894 if (cursor->resid < it->iter.bi_size)
895 it->iter.bi_size = cursor->resid;
Alex Elder6aaa4512013-03-06 23:39:39 -0600896 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600897
Ilya Dryomov5359a172018-01-20 10:30:10 +0100898 BUG_ON(cursor->last_piece);
899 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
900 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600901 return true;
902}
Alex Elderea965712013-04-05 14:46:01 -0500903#endif /* CONFIG_BLOCK */
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500904
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100905static void ceph_msg_data_bvecs_cursor_init(struct ceph_msg_data_cursor *cursor,
906 size_t length)
907{
908 struct ceph_msg_data *data = cursor->data;
909 struct bio_vec *bvecs = data->bvec_pos.bvecs;
910
911 cursor->resid = min_t(size_t, length, data->bvec_pos.iter.bi_size);
912 cursor->bvec_iter = data->bvec_pos.iter;
913 cursor->bvec_iter.bi_size = cursor->resid;
914
915 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
916 cursor->last_piece =
917 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
918}
919
920static struct page *ceph_msg_data_bvecs_next(struct ceph_msg_data_cursor *cursor,
921 size_t *page_offset,
922 size_t *length)
923{
924 struct bio_vec bv = bvec_iter_bvec(cursor->data->bvec_pos.bvecs,
925 cursor->bvec_iter);
926
927 *page_offset = bv.bv_offset;
928 *length = bv.bv_len;
929 return bv.bv_page;
930}
931
932static bool ceph_msg_data_bvecs_advance(struct ceph_msg_data_cursor *cursor,
933 size_t bytes)
934{
935 struct bio_vec *bvecs = cursor->data->bvec_pos.bvecs;
Ilya Dryomov187df762019-03-22 22:14:19 +0100936 struct page *page = bvec_iter_page(bvecs, cursor->bvec_iter);
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100937
938 BUG_ON(bytes > cursor->resid);
939 BUG_ON(bytes > bvec_iter_len(bvecs, cursor->bvec_iter));
940 cursor->resid -= bytes;
941 bvec_iter_advance(bvecs, &cursor->bvec_iter, bytes);
942
943 if (!cursor->resid) {
944 BUG_ON(!cursor->last_piece);
945 return false; /* no more data */
946 }
947
Ilya Dryomov187df762019-03-22 22:14:19 +0100948 if (!bytes || (cursor->bvec_iter.bi_bvec_done &&
949 page == bvec_iter_page(bvecs, cursor->bvec_iter)))
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100950 return false; /* more bytes to process in this segment */
951
952 BUG_ON(cursor->last_piece);
953 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
954 cursor->last_piece =
955 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
956 return true;
957}
958
Alex Elderfe38a2b2013-03-06 23:39:39 -0600959/*
Alex Eldere766d7b2013-03-07 15:38:28 -0600960 * For a page array, a piece comes from the first page in the array
961 * that has not already been fully consumed.
962 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500963static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500964 size_t length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600965{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500966 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600967 int page_count;
968
969 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
970
971 BUG_ON(!data->pages);
972 BUG_ON(!data->length);
973
Alex Elderca8b3a62013-04-05 14:46:01 -0500974 cursor->resid = min(length, data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600975 page_count = calc_pages_for(data->alignment, (u64)data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600976 cursor->page_offset = data->alignment & ~PAGE_MASK;
977 cursor->page_index = 0;
Alex Elder56fc5652013-03-30 23:46:55 -0500978 BUG_ON(page_count > (int)USHRT_MAX);
979 cursor->page_count = (unsigned short)page_count;
980 BUG_ON(length > SIZE_MAX - cursor->page_offset);
Ilya Dryomov5f740d72014-08-08 12:43:39 +0400981 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600982}
983
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500984static struct page *
985ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
986 size_t *page_offset, size_t *length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600987{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500988 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600989
990 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
991
992 BUG_ON(cursor->page_index >= cursor->page_count);
993 BUG_ON(cursor->page_offset >= PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600994
995 *page_offset = cursor->page_offset;
Alex Elder25aff7c2013-03-11 23:34:22 -0500996 if (cursor->last_piece)
Alex Eldere766d7b2013-03-07 15:38:28 -0600997 *length = cursor->resid;
Alex Elder25aff7c2013-03-11 23:34:22 -0500998 else
Alex Eldere766d7b2013-03-07 15:38:28 -0600999 *length = PAGE_SIZE - *page_offset;
Alex Eldere766d7b2013-03-07 15:38:28 -06001000
1001 return data->pages[cursor->page_index];
1002}
1003
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001004static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
Alex Eldere766d7b2013-03-07 15:38:28 -06001005 size_t bytes)
1006{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001007 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
Alex Eldere766d7b2013-03-07 15:38:28 -06001008
1009 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -06001010
1011 /* Advance the cursor page offset */
1012
1013 cursor->resid -= bytes;
Alex Elder5df521b2013-03-30 15:09:59 -05001014 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
1015 if (!bytes || cursor->page_offset)
Alex Eldere766d7b2013-03-07 15:38:28 -06001016 return false; /* more bytes to process in the current page */
1017
Yan, Zhengd90deda2014-03-23 06:50:39 +08001018 if (!cursor->resid)
1019 return false; /* no more data */
1020
Alex Elder5df521b2013-03-30 15:09:59 -05001021 /* Move on to the next page; offset is already at 0 */
Alex Eldere766d7b2013-03-07 15:38:28 -06001022
1023 BUG_ON(cursor->page_index >= cursor->page_count);
Alex Eldere766d7b2013-03-07 15:38:28 -06001024 cursor->page_index++;
Alex Elder25aff7c2013-03-11 23:34:22 -05001025 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -06001026
1027 return true;
1028}
1029
1030/*
Alex Elderdd236fc2013-03-06 23:39:39 -06001031 * For a pagelist, a piece is whatever remains to be consumed in the
1032 * first page in the list, or the front of the next page.
Alex Elderfe38a2b2013-03-06 23:39:39 -06001033 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001034static void
1035ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -05001036 size_t length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001037{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001038 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001039 struct ceph_pagelist *pagelist;
1040 struct page *page;
1041
Alex Elderdd236fc2013-03-06 23:39:39 -06001042 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001043
1044 pagelist = data->pagelist;
1045 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001046
1047 if (!length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001048 return; /* pagelist can be assigned but empty */
1049
1050 BUG_ON(list_empty(&pagelist->head));
1051 page = list_first_entry(&pagelist->head, struct page, lru);
1052
Alex Elderca8b3a62013-04-05 14:46:01 -05001053 cursor->resid = min(length, pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001054 cursor->page = page;
1055 cursor->offset = 0;
Alex Eldera51b272e2013-04-19 15:34:49 -05001056 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001057}
1058
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001059static struct page *
1060ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1061 size_t *page_offset, size_t *length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001062{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001063 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001064 struct ceph_pagelist *pagelist;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001065
1066 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1067
1068 pagelist = data->pagelist;
1069 BUG_ON(!pagelist);
1070
1071 BUG_ON(!cursor->page);
Alex Elder25aff7c2013-03-11 23:34:22 -05001072 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001073
Alex Elder5df521b2013-03-30 15:09:59 -05001074 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001075 *page_offset = cursor->offset & ~PAGE_MASK;
Alex Elder5df521b2013-03-30 15:09:59 -05001076 if (cursor->last_piece)
Alex Elder25aff7c2013-03-11 23:34:22 -05001077 *length = cursor->resid;
1078 else
1079 *length = PAGE_SIZE - *page_offset;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001080
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001081 return cursor->page;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001082}
1083
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001084static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
Alex Elderdd236fc2013-03-06 23:39:39 -06001085 size_t bytes)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001086{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001087 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001088 struct ceph_pagelist *pagelist;
1089
1090 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1091
1092 pagelist = data->pagelist;
1093 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001094
1095 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001096 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1097
1098 /* Advance the cursor offset */
1099
Alex Elder25aff7c2013-03-11 23:34:22 -05001100 cursor->resid -= bytes;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001101 cursor->offset += bytes;
Alex Elder5df521b2013-03-30 15:09:59 -05001102 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001103 if (!bytes || cursor->offset & ~PAGE_MASK)
1104 return false; /* more bytes to process in the current page */
1105
Yan, Zhengd90deda2014-03-23 06:50:39 +08001106 if (!cursor->resid)
1107 return false; /* no more data */
1108
Alex Elderfe38a2b2013-03-06 23:39:39 -06001109 /* Move on to the next page */
1110
1111 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001112 cursor->page = list_next_entry(cursor->page, lru);
Alex Elder25aff7c2013-03-11 23:34:22 -05001113 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001114
1115 return true;
1116}
1117
Alex Elderdd236fc2013-03-06 23:39:39 -06001118/*
1119 * Message data is handled (sent or received) in pieces, where each
1120 * piece resides on a single page. The network layer might not
1121 * consume an entire piece at once. A data item's cursor keeps
1122 * track of which piece is next to process and how much remains to
1123 * be processed in that piece. It also tracks whether the current
1124 * piece is the last one in the data item.
1125 */
Alex Elderca8b3a62013-04-05 14:46:01 -05001126static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
Alex Elderdd236fc2013-03-06 23:39:39 -06001127{
Alex Elderca8b3a62013-04-05 14:46:01 -05001128 size_t length = cursor->total_resid;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001129
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001130 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001131 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001132 ceph_msg_data_pagelist_cursor_init(cursor, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001133 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001134 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001135 ceph_msg_data_pages_cursor_init(cursor, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001136 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001137#ifdef CONFIG_BLOCK
1138 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001139 ceph_msg_data_bio_cursor_init(cursor, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001140 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001141#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001142 case CEPH_MSG_DATA_BVECS:
1143 ceph_msg_data_bvecs_cursor_init(cursor, length);
1144 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001145 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001146 default:
1147 /* BUG(); */
1148 break;
1149 }
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001150 cursor->need_crc = true;
Alex Elderdd236fc2013-03-06 23:39:39 -06001151}
1152
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001153void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
1154 struct ceph_msg *msg, size_t length)
Alex Elderca8b3a62013-04-05 14:46:01 -05001155{
Alex Elderca8b3a62013-04-05 14:46:01 -05001156 BUG_ON(!length);
1157 BUG_ON(length > msg->data_length);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001158 BUG_ON(!msg->num_data_items);
Alex Elderca8b3a62013-04-05 14:46:01 -05001159
Alex Elderca8b3a62013-04-05 14:46:01 -05001160 cursor->total_resid = length;
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001161 cursor->data = msg->data;
Alex Elderca8b3a62013-04-05 14:46:01 -05001162
1163 __ceph_msg_data_cursor_init(cursor);
1164}
1165
Alex Elderdd236fc2013-03-06 23:39:39 -06001166/*
1167 * Return the page containing the next piece to process for a given
1168 * data item, and supply the page offset and length of that piece.
1169 * Indicate whether this is the last piece in this data item.
1170 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001171struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1172 size_t *page_offset, size_t *length,
1173 bool *last_piece)
Alex Elderdd236fc2013-03-06 23:39:39 -06001174{
1175 struct page *page;
1176
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001177 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001178 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001179 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001180 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001181 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001182 page = ceph_msg_data_pages_next(cursor, page_offset, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001183 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001184#ifdef CONFIG_BLOCK
1185 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001186 page = ceph_msg_data_bio_next(cursor, page_offset, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001187 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001188#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001189 case CEPH_MSG_DATA_BVECS:
1190 page = ceph_msg_data_bvecs_next(cursor, page_offset, length);
1191 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001192 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001193 default:
1194 page = NULL;
1195 break;
1196 }
Ilya Dryomov5359a172018-01-20 10:30:10 +01001197
Alex Elderdd236fc2013-03-06 23:39:39 -06001198 BUG_ON(!page);
1199 BUG_ON(*page_offset + *length > PAGE_SIZE);
1200 BUG_ON(!*length);
Ilya Dryomov5359a172018-01-20 10:30:10 +01001201 BUG_ON(*length > cursor->resid);
Alex Elderdd236fc2013-03-06 23:39:39 -06001202 if (last_piece)
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001203 *last_piece = cursor->last_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001204
1205 return page;
1206}
1207
1208/*
1209 * Returns true if the result moves the cursor on to the next piece
1210 * of the data item.
1211 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001212void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes)
Alex Elderdd236fc2013-03-06 23:39:39 -06001213{
1214 bool new_piece;
1215
Alex Elder25aff7c2013-03-11 23:34:22 -05001216 BUG_ON(bytes > cursor->resid);
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001217 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001218 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001219 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
Alex Elderdd236fc2013-03-06 23:39:39 -06001220 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001221 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001222 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
Alex Eldere766d7b2013-03-07 15:38:28 -06001223 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001224#ifdef CONFIG_BLOCK
1225 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001226 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
Alex Elder6aaa4512013-03-06 23:39:39 -06001227 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001228#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001229 case CEPH_MSG_DATA_BVECS:
1230 new_piece = ceph_msg_data_bvecs_advance(cursor, bytes);
1231 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001232 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001233 default:
1234 BUG();
1235 break;
1236 }
Alex Elderca8b3a62013-04-05 14:46:01 -05001237 cursor->total_resid -= bytes;
Alex Elderdd236fc2013-03-06 23:39:39 -06001238
Alex Elderca8b3a62013-04-05 14:46:01 -05001239 if (!cursor->resid && cursor->total_resid) {
1240 WARN_ON(!cursor->last_piece);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001241 cursor->data++;
Alex Elderca8b3a62013-04-05 14:46:01 -05001242 __ceph_msg_data_cursor_init(cursor);
Alex Eldera51b272e2013-04-19 15:34:49 -05001243 new_piece = true;
Alex Elderca8b3a62013-04-05 14:46:01 -05001244 }
Alex Eldera51b272e2013-04-19 15:34:49 -05001245 cursor->need_crc = new_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001246}
1247
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01001248static size_t sizeof_footer(struct ceph_connection *con)
1249{
1250 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1251 sizeof(struct ceph_msg_footer) :
1252 sizeof(struct ceph_msg_footer_old);
1253}
1254
Alex Elder98fa5dd2013-04-02 12:09:50 -05001255static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
Alex Elder739c9052012-06-11 14:57:13 -05001256{
Alex Elder4c59b4a2013-03-11 23:34:23 -05001257 /* Initialize data cursor */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001258
Ilya Dryomov8ee8abf2020-11-04 10:26:39 +01001259 ceph_msg_data_cursor_init(&msg->cursor, msg, data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001260}
1261
Sage Weil31b80062009-10-06 11:31:13 -07001262/*
1263 * Prepare footer for currently outgoing message, and finish things
1264 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1265 */
Alex Elder859eb792012-02-14 14:05:33 -06001266static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001267{
1268 struct ceph_msg *m = con->out_msg;
1269
Alex Elderfd154f32012-06-11 14:57:13 -05001270 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1271
Sage Weil31b80062009-10-06 11:31:13 -07001272 dout("prepare_write_message_footer %p\n", con);
Ilya Dryomov89f08172016-02-20 15:56:07 +01001273 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
Yan, Zheng33d07332014-11-04 16:33:37 +08001274 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1275 if (con->ops->sign_message)
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01001276 con->ops->sign_message(m);
Yan, Zheng33d07332014-11-04 16:33:37 +08001277 else
1278 m->footer.sig = 0;
Yan, Zheng33d07332014-11-04 16:33:37 +08001279 } else {
1280 m->old_footer.flags = m->footer.flags;
Yan, Zheng33d07332014-11-04 16:33:37 +08001281 }
Sage Weil31b80062009-10-06 11:31:13 -07001282 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -08001283 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -07001284}
1285
1286/*
1287 * Prepare headers for the next outgoing message.
1288 */
1289static void prepare_write_message(struct ceph_connection *con)
1290{
1291 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -06001292 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001293
Alex Eldere2200422012-05-23 14:35:23 -05001294 con_out_kvec_reset(con);
Sage Weilc86a2932009-12-14 14:04:30 -08001295 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -07001296
1297 /* Sneak an ack in there first? If we can get it into the same
1298 * TCP packet that's a good thing. */
1299 if (con->in_seq > con->in_seq_acked) {
1300 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -05001301 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001302 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001303 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001304 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001305 }
1306
Ilya Dryomov771294f2020-11-18 16:37:14 +01001307 ceph_con_get_out_msg(con);
1308 m = con->out_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001309
Alex Elder98fa5dd2013-04-02 12:09:50 -05001310 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
Sage Weil31b80062009-10-06 11:31:13 -07001311 m, con->out_seq, le16_to_cpu(m->hdr.type),
1312 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
Alex Elder98fa5dd2013-04-02 12:09:50 -05001313 m->data_length);
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001314 WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
1315 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
Sage Weil31b80062009-10-06 11:31:13 -07001316
1317 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -05001318 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001319 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
Alex Eldere2200422012-05-23 14:35:23 -05001320 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -06001321
Sage Weil31b80062009-10-06 11:31:13 -07001322 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -05001323 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -06001324 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -07001325
Ilya Dryomov67645d72015-12-28 13:18:34 +03001326 /* fill in hdr crc and finalize hdr */
Alex Eldera9a0c512012-02-15 07:43:54 -06001327 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1328 con->out_msg->hdr.crc = cpu_to_le32(crc);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001329 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
Alex Eldera9a0c512012-02-15 07:43:54 -06001330
Ilya Dryomov67645d72015-12-28 13:18:34 +03001331 /* fill in front and middle crc, footer */
Alex Eldera9a0c512012-02-15 07:43:54 -06001332 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1333 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1334 if (m->middle) {
1335 crc = crc32c(0, m->middle->vec.iov_base,
1336 m->middle->vec.iov_len);
1337 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1338 } else
Sage Weil31b80062009-10-06 11:31:13 -07001339 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -05001340 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -07001341 le32_to_cpu(con->out_msg->footer.front_crc),
1342 le32_to_cpu(con->out_msg->footer.middle_crc));
Ilya Dryomov67645d72015-12-28 13:18:34 +03001343 con->out_msg->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001344
1345 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -05001346 con->out_msg->footer.data_crc = 0;
Alex Elder98fa5dd2013-04-02 12:09:50 -05001347 if (m->data_length) {
1348 prepare_message_data(con->out_msg, m->data_length);
Alex Elder78625052013-03-06 23:39:39 -06001349 con->out_more = 1; /* data + footer will follow */
1350 } else {
Sage Weil31b80062009-10-06 11:31:13 -07001351 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -06001352 prepare_write_message_footer(con);
Alex Elder78625052013-03-06 23:39:39 -06001353 }
Sage Weil31b80062009-10-06 11:31:13 -07001354
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001355 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001356}
1357
1358/*
1359 * Prepare an ack.
1360 */
1361static void prepare_write_ack(struct ceph_connection *con)
1362{
1363 dout("prepare_write_ack %p %llu -> %llu\n", con,
1364 con->in_seq_acked, con->in_seq);
1365 con->in_seq_acked = con->in_seq;
1366
Alex Eldere2200422012-05-23 14:35:23 -05001367 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001368
Alex Eldere2200422012-05-23 14:35:23 -05001369 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -06001370
Sage Weil31b80062009-10-06 11:31:13 -07001371 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001372 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001373 &con->out_temp_ack);
1374
Sage Weil31b80062009-10-06 11:31:13 -07001375 con->out_more = 1; /* more will follow.. eventually.. */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001376 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001377}
1378
1379/*
Sage Weil3a230832013-03-25 08:47:40 -07001380 * Prepare to share the seq during handshake
1381 */
1382static void prepare_write_seq(struct ceph_connection *con)
1383{
1384 dout("prepare_write_seq %p %llu -> %llu\n", con,
1385 con->in_seq_acked, con->in_seq);
1386 con->in_seq_acked = con->in_seq;
1387
1388 con_out_kvec_reset(con);
1389
1390 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1391 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1392 &con->out_temp_ack);
1393
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001394 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
Sage Weil3a230832013-03-25 08:47:40 -07001395}
1396
1397/*
Sage Weil31b80062009-10-06 11:31:13 -07001398 * Prepare to write keepalive byte.
1399 */
1400static void prepare_write_keepalive(struct ceph_connection *con)
1401{
1402 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -05001403 con_out_kvec_reset(con);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001404 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001405 struct timespec64 now;
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001406
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001407 ktime_get_real_ts64(&now);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001408 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001409 ceph_encode_timespec64(&con->out_temp_keepalive2, &now);
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001410 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1411 &con->out_temp_keepalive2);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001412 } else {
1413 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1414 }
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001415 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001416}
1417
1418/*
1419 * Connection negotiation.
1420 */
1421
Ilya Dryomov262614c2018-07-26 15:17:46 +02001422static int get_connect_authorizer(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001423{
Alex Eldera3530df2012-05-16 15:16:39 -05001424 struct ceph_auth_handshake *auth;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001425 int auth_proto;
Alex Elderb1c6b982012-05-16 15:16:38 -05001426
1427 if (!con->ops->get_authorizer) {
Ilya Dryomov262614c2018-07-26 15:17:46 +02001428 con->auth = NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -05001429 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1430 con->out_connect.authorizer_len = 0;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001431 return 0;
Alex Elderb1c6b982012-05-16 15:16:38 -05001432 }
1433
Ilya Dryomov262614c2018-07-26 15:17:46 +02001434 auth = con->ops->get_authorizer(con, &auth_proto, con->auth_retry);
Alex Eldera3530df2012-05-16 15:16:39 -05001435 if (IS_ERR(auth))
Ilya Dryomov262614c2018-07-26 15:17:46 +02001436 return PTR_ERR(auth);
Sage Weil0da5d702011-05-19 11:21:05 -07001437
Ilya Dryomov262614c2018-07-26 15:17:46 +02001438 con->auth = auth;
1439 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
1440 con->out_connect.authorizer_len = cpu_to_le32(auth->authorizer_buf_len);
1441 return 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001442}
1443
Sage Weil31b80062009-10-06 11:31:13 -07001444/*
1445 * We connected to a peer and are saying hello.
1446 */
Alex Eldere825a662012-05-16 15:16:38 -05001447static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001448{
Alex Eldere2200422012-05-23 14:35:23 -05001449 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1450 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -05001451 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -08001452
Sage Weileed0ef22009-11-10 14:34:36 -08001453 con->out_more = 0;
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001454 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
Sage Weileed0ef22009-11-10 14:34:36 -08001455}
1456
Ilya Dryomovc0f56b42018-07-26 17:43:47 +02001457static void __prepare_write_connect(struct ceph_connection *con)
1458{
1459 con_out_kvec_add(con, sizeof(con->out_connect), &con->out_connect);
1460 if (con->auth)
1461 con_out_kvec_add(con, con->auth->authorizer_buf_len,
1462 con->auth->authorizer_buf);
1463
1464 con->out_more = 0;
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001465 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
Ilya Dryomovc0f56b42018-07-26 17:43:47 +02001466}
1467
Alex Eldere825a662012-05-16 15:16:38 -05001468static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -08001469{
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001470 unsigned int global_seq = ceph_get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001471 int proto;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001472 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001473
1474 switch (con->peer_name.type) {
1475 case CEPH_ENTITY_TYPE_MON:
1476 proto = CEPH_MONC_PROTOCOL;
1477 break;
1478 case CEPH_ENTITY_TYPE_OSD:
1479 proto = CEPH_OSDC_PROTOCOL;
1480 break;
1481 case CEPH_ENTITY_TYPE_MDS:
1482 proto = CEPH_MDSC_PROTOCOL;
1483 break;
1484 default:
1485 BUG();
1486 }
1487
1488 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1489 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001490
Ilya Dryomov859bff52015-10-28 23:50:58 +01001491 con->out_connect.features =
1492 cpu_to_le64(from_msgr(con->msgr)->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -07001493 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1494 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1495 con->out_connect.global_seq = cpu_to_le32(global_seq);
1496 con->out_connect.protocol_version = cpu_to_le32(proto);
1497 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001498
Ilya Dryomov262614c2018-07-26 15:17:46 +02001499 ret = get_connect_authorizer(con);
1500 if (ret)
1501 return ret;
Alex Elder3da54772012-05-16 15:16:39 -05001502
Ilya Dryomovc0f56b42018-07-26 17:43:47 +02001503 __prepare_write_connect(con);
Alex Eldere10c7582012-05-16 15:16:38 -05001504 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07001505}
1506
Sage Weil31b80062009-10-06 11:31:13 -07001507/*
1508 * write as much of pending kvecs to the socket as we can.
1509 * 1 -> done
1510 * 0 -> socket full, but more to do
1511 * <0 -> error
1512 */
1513static int write_partial_kvec(struct ceph_connection *con)
1514{
1515 int ret;
1516
1517 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1518 while (con->out_kvec_bytes > 0) {
1519 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1520 con->out_kvec_left, con->out_kvec_bytes,
1521 con->out_more);
1522 if (ret <= 0)
1523 goto out;
1524 con->out_kvec_bytes -= ret;
1525 if (con->out_kvec_bytes == 0)
1526 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -06001527
1528 /* account for full iov entries consumed */
1529 while (ret >= con->out_kvec_cur->iov_len) {
1530 BUG_ON(!con->out_kvec_left);
1531 ret -= con->out_kvec_cur->iov_len;
1532 con->out_kvec_cur++;
1533 con->out_kvec_left--;
1534 }
1535 /* and for a partially-consumed entry */
1536 if (ret) {
1537 con->out_kvec_cur->iov_len -= ret;
1538 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -07001539 }
1540 }
1541 con->out_kvec_left = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001542 ret = 1;
1543out:
1544 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1545 con->out_kvec_bytes, con->out_kvec_left, ret);
1546 return ret; /* done! */
1547}
1548
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001549u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset,
1550 unsigned int length)
Alex Elder35b62802013-03-08 20:59:00 -06001551{
1552 char *kaddr;
1553
1554 kaddr = kmap(page);
1555 BUG_ON(kaddr == NULL);
1556 crc = crc32c(crc, kaddr + page_offset, length);
1557 kunmap(page);
1558
1559 return crc;
1560}
Sage Weil31b80062009-10-06 11:31:13 -07001561/*
1562 * Write as much message data payload as we can. If we finish, queue
1563 * up the footer.
1564 * 1 -> done, footer is now queued in out_kvec[].
1565 * 0 -> socket full, but more to do
1566 * <0 -> error
1567 */
Alex Elder34d2d202013-03-08 20:58:59 -06001568static int write_partial_message_data(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001569{
1570 struct ceph_msg *msg = con->out_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001571 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01001572 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Ilya Dryomov433b0a12018-11-20 15:44:00 +01001573 int more = MSG_MORE | MSG_SENDPAGE_NOTLAST;
Alex Elderf5db90b2013-03-11 23:34:23 -05001574 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001575
Alex Elder859a35d2013-03-11 23:34:23 -05001576 dout("%s %p msg %p\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001577
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001578 if (!msg->num_data_items)
Alex Elder4c59b4a2013-03-11 23:34:23 -05001579 return -EINVAL;
1580
Alex Elder5821bd82012-06-11 14:57:13 -05001581 /*
1582 * Iterate through each page that contains data to be
1583 * written, and send as much as possible for each.
1584 *
1585 * If we are calculating the data crc (the default), we will
1586 * need to map the page. If we have no pages, they have
1587 * been revoked, so use the zero page.
1588 */
Alex Elderf5db90b2013-03-11 23:34:23 -05001589 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
Ilya Dryomov45a267d2018-01-22 15:20:15 +01001590 while (cursor->total_resid) {
Alex Elder8a166d02013-03-08 13:35:36 -06001591 struct page *page;
Alex Eldere387d522013-03-06 23:39:38 -06001592 size_t page_offset;
1593 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05001594 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001595
Ilya Dryomov45a267d2018-01-22 15:20:15 +01001596 if (!cursor->resid) {
1597 ceph_msg_data_advance(cursor, 0);
1598 continue;
1599 }
1600
Ilya Dryomov1f6b8212018-11-14 12:24:01 +01001601 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Ilya Dryomov433b0a12018-11-20 15:44:00 +01001602 if (length == cursor->total_resid)
1603 more = MSG_MORE;
Ilya Dryomov1f6b8212018-11-14 12:24:01 +01001604 ret = ceph_tcp_sendpage(con->sock, page, page_offset, length,
Ilya Dryomov433b0a12018-11-20 15:44:00 +01001605 more);
Alex Elderf5db90b2013-03-11 23:34:23 -05001606 if (ret <= 0) {
1607 if (do_datacrc)
1608 msg->footer.data_crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -07001609
Alex Elderf5db90b2013-03-11 23:34:23 -05001610 return ret;
1611 }
Alex Elder143334f2013-03-29 11:44:10 -05001612 if (do_datacrc && cursor->need_crc)
1613 crc = ceph_crc32c_page(crc, page, page_offset, length);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001614 ceph_msg_data_advance(cursor, (size_t)ret);
Sage Weil31b80062009-10-06 11:31:13 -07001615 }
1616
Alex Elder34d2d202013-03-08 20:58:59 -06001617 dout("%s %p msg %p done\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001618
1619 /* prepare and queue up footer, too */
Alex Elderf5db90b2013-03-11 23:34:23 -05001620 if (do_datacrc)
1621 msg->footer.data_crc = cpu_to_le32(crc);
1622 else
Alex Elder84ca8fc2012-06-11 14:57:13 -05001623 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001624 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001625 prepare_write_message_footer(con);
Alex Elderf5db90b2013-03-11 23:34:23 -05001626
1627 return 1; /* must return > 0 to indicate success */
Sage Weil31b80062009-10-06 11:31:13 -07001628}
1629
1630/*
1631 * write some zeros
1632 */
1633static int write_partial_skip(struct ceph_connection *con)
1634{
Ilya Dryomov433b0a12018-11-20 15:44:00 +01001635 int more = MSG_MORE | MSG_SENDPAGE_NOTLAST;
Sage Weil31b80062009-10-06 11:31:13 -07001636 int ret;
1637
Ilya Dryomov67645d72015-12-28 13:18:34 +03001638 dout("%s %p %d left\n", __func__, con, con->out_skip);
Sage Weil31b80062009-10-06 11:31:13 -07001639 while (con->out_skip > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001640 size_t size = min(con->out_skip, (int) PAGE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001641
Ilya Dryomov433b0a12018-11-20 15:44:00 +01001642 if (size == con->out_skip)
1643 more = MSG_MORE;
Ilya Dryomov699921d2020-11-09 14:37:06 +01001644 ret = ceph_tcp_sendpage(con->sock, ceph_zero_page, 0, size,
1645 more);
Sage Weil31b80062009-10-06 11:31:13 -07001646 if (ret <= 0)
1647 goto out;
1648 con->out_skip -= ret;
1649 }
1650 ret = 1;
1651out:
1652 return ret;
1653}
1654
1655/*
1656 * Prepare to read connection handshake, or an ack.
1657 */
Sage Weileed0ef22009-11-10 14:34:36 -08001658static void prepare_read_banner(struct ceph_connection *con)
1659{
1660 dout("prepare_read_banner %p\n", con);
1661 con->in_base_pos = 0;
1662}
1663
Sage Weil31b80062009-10-06 11:31:13 -07001664static void prepare_read_connect(struct ceph_connection *con)
1665{
1666 dout("prepare_read_connect %p\n", con);
1667 con->in_base_pos = 0;
1668}
1669
1670static void prepare_read_ack(struct ceph_connection *con)
1671{
1672 dout("prepare_read_ack %p\n", con);
1673 con->in_base_pos = 0;
1674}
1675
Sage Weil3a230832013-03-25 08:47:40 -07001676static void prepare_read_seq(struct ceph_connection *con)
1677{
1678 dout("prepare_read_seq %p\n", con);
1679 con->in_base_pos = 0;
1680 con->in_tag = CEPH_MSGR_TAG_SEQ;
1681}
1682
Sage Weil31b80062009-10-06 11:31:13 -07001683static void prepare_read_tag(struct ceph_connection *con)
1684{
1685 dout("prepare_read_tag %p\n", con);
1686 con->in_base_pos = 0;
1687 con->in_tag = CEPH_MSGR_TAG_READY;
1688}
1689
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001690static void prepare_read_keepalive_ack(struct ceph_connection *con)
1691{
1692 dout("prepare_read_keepalive_ack %p\n", con);
1693 con->in_base_pos = 0;
1694}
1695
Sage Weil31b80062009-10-06 11:31:13 -07001696/*
1697 * Prepare to read a message.
1698 */
1699static int prepare_read_message(struct ceph_connection *con)
1700{
1701 dout("prepare_read_message %p\n", con);
1702 BUG_ON(con->in_msg != NULL);
1703 con->in_base_pos = 0;
1704 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1705 return 0;
1706}
1707
1708
1709static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001710 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001711{
Alex Eldere6cee712012-05-10 10:29:50 -05001712 while (con->in_base_pos < end) {
1713 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001714 int have = size - left;
1715 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1716 if (ret <= 0)
1717 return ret;
1718 con->in_base_pos += ret;
1719 }
1720 return 1;
1721}
1722
1723
1724/*
1725 * Read all or part of the connect-side handshake on a new connection
1726 */
Sage Weileed0ef22009-11-10 14:34:36 -08001727static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001728{
Alex Elderfd516532012-05-10 10:29:50 -05001729 int size;
1730 int end;
1731 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001732
Sage Weileed0ef22009-11-10 14:34:36 -08001733 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001734
1735 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001736 size = strlen(CEPH_BANNER);
1737 end = size;
1738 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001739 if (ret <= 0)
1740 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001741
1742 size = sizeof (con->actual_peer_addr);
1743 end += size;
1744 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001745 if (ret <= 0)
1746 goto out;
Jeff Layton2c66de52019-06-17 09:24:31 -04001747 ceph_decode_banner_addr(&con->actual_peer_addr);
Alex Elderfd516532012-05-10 10:29:50 -05001748
1749 size = sizeof (con->peer_addr_for_me);
1750 end += size;
1751 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001752 if (ret <= 0)
1753 goto out;
Jeff Layton2c66de52019-06-17 09:24:31 -04001754 ceph_decode_banner_addr(&con->peer_addr_for_me);
Alex Elderfd516532012-05-10 10:29:50 -05001755
Sage Weileed0ef22009-11-10 14:34:36 -08001756out:
1757 return ret;
1758}
1759
1760static int read_partial_connect(struct ceph_connection *con)
1761{
Alex Elderfd516532012-05-10 10:29:50 -05001762 int size;
1763 int end;
1764 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001765
1766 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1767
Alex Elderfd516532012-05-10 10:29:50 -05001768 size = sizeof (con->in_reply);
1769 end = size;
1770 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001771 if (ret <= 0)
1772 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001773
Ilya Dryomov262614c2018-07-26 15:17:46 +02001774 if (con->auth) {
1775 size = le32_to_cpu(con->in_reply.authorizer_len);
Ilya Dryomov130f52f2018-07-27 19:40:30 +02001776 if (size > con->auth->authorizer_reply_buf_len) {
1777 pr_err("authorizer reply too big: %d > %zu\n", size,
1778 con->auth->authorizer_reply_buf_len);
1779 ret = -EINVAL;
1780 goto out;
1781 }
1782
Ilya Dryomov262614c2018-07-26 15:17:46 +02001783 end += size;
1784 ret = read_partial(con, end, size,
1785 con->auth->authorizer_reply_buf);
1786 if (ret <= 0)
1787 goto out;
1788 }
Sage Weil31b80062009-10-06 11:31:13 -07001789
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001790 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1791 con, (int)con->in_reply.tag,
1792 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001793 le32_to_cpu(con->in_reply.global_seq));
1794out:
1795 return ret;
1796}
1797
1798/*
1799 * Verify the hello banner looks okay.
1800 */
1801static int verify_hello(struct ceph_connection *con)
1802{
1803 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001804 pr_err("connect to %s got bad banner\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -04001805 ceph_pr_addr(&con->peer_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001806 con->error_msg = "protocol error, bad banner";
1807 return -1;
1808 }
1809 return 0;
1810}
1811
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001812bool ceph_addr_is_blank(const struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -07001813{
Jeff Laytoncede1852019-05-06 09:38:46 -04001814 struct sockaddr_storage ss = addr->in_addr; /* align */
1815 struct in_addr *addr4 = &((struct sockaddr_in *)&ss)->sin_addr;
1816 struct in6_addr *addr6 = &((struct sockaddr_in6 *)&ss)->sin6_addr;
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001817
Jeff Laytoncede1852019-05-06 09:38:46 -04001818 switch (ss.ss_family) {
Sage Weil31b80062009-10-06 11:31:13 -07001819 case AF_INET:
Jeff Laytoncede1852019-05-06 09:38:46 -04001820 return addr4->s_addr == htonl(INADDR_ANY);
Sage Weil31b80062009-10-06 11:31:13 -07001821 case AF_INET6:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001822 return ipv6_addr_any(addr6);
1823 default:
1824 return true;
Sage Weil31b80062009-10-06 11:31:13 -07001825 }
Sage Weil31b80062009-10-06 11:31:13 -07001826}
1827
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001828int ceph_addr_port(const struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -07001829{
Jeff Laytoncede1852019-05-06 09:38:46 -04001830 switch (get_unaligned(&addr->in_addr.ss_family)) {
Sage Weil31b80062009-10-06 11:31:13 -07001831 case AF_INET:
Jeff Laytoncede1852019-05-06 09:38:46 -04001832 return ntohs(get_unaligned(&((struct sockaddr_in *)&addr->in_addr)->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -07001833 case AF_INET6:
Jeff Laytoncede1852019-05-06 09:38:46 -04001834 return ntohs(get_unaligned(&((struct sockaddr_in6 *)&addr->in_addr)->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -07001835 }
1836 return 0;
1837}
1838
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001839void ceph_addr_set_port(struct ceph_entity_addr *addr, int p)
Sage Weil31b80062009-10-06 11:31:13 -07001840{
Jeff Laytoncede1852019-05-06 09:38:46 -04001841 switch (get_unaligned(&addr->in_addr.ss_family)) {
Sage Weil31b80062009-10-06 11:31:13 -07001842 case AF_INET:
Jeff Laytoncede1852019-05-06 09:38:46 -04001843 put_unaligned(htons(p), &((struct sockaddr_in *)&addr->in_addr)->sin_port);
Sage Weila2a79602011-05-12 15:34:24 -07001844 break;
Sage Weil31b80062009-10-06 11:31:13 -07001845 case AF_INET6:
Jeff Laytoncede1852019-05-06 09:38:46 -04001846 put_unaligned(htons(p), &((struct sockaddr_in6 *)&addr->in_addr)->sin6_port);
Sage Weila2a79602011-05-12 15:34:24 -07001847 break;
Sage Weil31b80062009-10-06 11:31:13 -07001848 }
1849}
1850
1851/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001852 * Unlike other *_pton function semantics, zero indicates success.
1853 */
Jeff Laytoncede1852019-05-06 09:38:46 -04001854static int ceph_pton(const char *str, size_t len, struct ceph_entity_addr *addr,
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001855 char delim, const char **ipend)
1856{
Jeff Laytoncede1852019-05-06 09:38:46 -04001857 memset(&addr->in_addr, 0, sizeof(addr->in_addr));
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001858
Jeff Laytoncede1852019-05-06 09:38:46 -04001859 if (in4_pton(str, len, (u8 *)&((struct sockaddr_in *)&addr->in_addr)->sin_addr.s_addr, delim, ipend)) {
1860 put_unaligned(AF_INET, &addr->in_addr.ss_family);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001861 return 0;
1862 }
1863
Jeff Laytoncede1852019-05-06 09:38:46 -04001864 if (in6_pton(str, len, (u8 *)&((struct sockaddr_in6 *)&addr->in_addr)->sin6_addr.s6_addr, delim, ipend)) {
1865 put_unaligned(AF_INET6, &addr->in_addr.ss_family);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001866 return 0;
1867 }
1868
1869 return -EINVAL;
1870}
1871
1872/*
1873 * Extract hostname string and resolve using kernel DNS facility.
1874 */
1875#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1876static int ceph_dns_resolve_name(const char *name, size_t namelen,
Jeff Laytoncede1852019-05-06 09:38:46 -04001877 struct ceph_entity_addr *addr, char delim, const char **ipend)
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001878{
1879 const char *end, *delim_p;
1880 char *colon_p, *ip_addr = NULL;
1881 int ip_len, ret;
1882
1883 /*
1884 * The end of the hostname occurs immediately preceding the delimiter or
1885 * the port marker (':') where the delimiter takes precedence.
1886 */
1887 delim_p = memchr(name, delim, namelen);
1888 colon_p = memchr(name, ':', namelen);
1889
1890 if (delim_p && colon_p)
1891 end = delim_p < colon_p ? delim_p : colon_p;
1892 else if (!delim_p && colon_p)
1893 end = colon_p;
1894 else {
1895 end = delim_p;
1896 if (!end) /* case: hostname:/ */
1897 end = name + namelen;
1898 }
1899
1900 if (end <= name)
1901 return -EINVAL;
1902
1903 /* do dns_resolve upcall */
David Howellsa58946c2019-06-26 21:02:33 +01001904 ip_len = dns_query(current->nsproxy->net_ns,
1905 NULL, name, end - name, NULL, &ip_addr, NULL, false);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001906 if (ip_len > 0)
Jeff Laytoncede1852019-05-06 09:38:46 -04001907 ret = ceph_pton(ip_addr, ip_len, addr, -1, NULL);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001908 else
1909 ret = -ESRCH;
1910
1911 kfree(ip_addr);
1912
1913 *ipend = end;
1914
1915 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
Jeff Laytonb726ec92019-05-06 09:38:47 -04001916 ret, ret ? "failed" : ceph_pr_addr(addr));
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001917
1918 return ret;
1919}
1920#else
1921static inline int ceph_dns_resolve_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 return -EINVAL;
1925}
1926#endif
1927
1928/*
1929 * Parse a server name (IP or hostname). If a valid IP address is not found
1930 * then try to extract a hostname to resolve using userspace DNS upcall.
1931 */
1932static int ceph_parse_server_name(const char *name, size_t namelen,
Jeff Laytoncede1852019-05-06 09:38:46 -04001933 struct ceph_entity_addr *addr, char delim, const char **ipend)
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001934{
1935 int ret;
1936
Jeff Laytoncede1852019-05-06 09:38:46 -04001937 ret = ceph_pton(name, namelen, addr, delim, ipend);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001938 if (ret)
Jeff Laytoncede1852019-05-06 09:38:46 -04001939 ret = ceph_dns_resolve_name(name, namelen, addr, delim, ipend);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001940
1941 return ret;
1942}
1943
1944/*
Sage Weil31b80062009-10-06 11:31:13 -07001945 * Parse an ip[:port] list into an addr array. Use the default
1946 * monitor port if a port isn't specified.
1947 */
1948int ceph_parse_ips(const char *c, const char *end,
1949 struct ceph_entity_addr *addr,
1950 int max_count, int *count)
1951{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001952 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001953 const char *p = c;
1954
1955 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1956 for (i = 0; i < max_count; i++) {
1957 const char *ipend;
Sage Weil31b80062009-10-06 11:31:13 -07001958 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001959 char delim = ',';
1960
1961 if (*p == '[') {
1962 delim = ']';
1963 p++;
1964 }
Sage Weil31b80062009-10-06 11:31:13 -07001965
Jeff Laytoncede1852019-05-06 09:38:46 -04001966 ret = ceph_parse_server_name(p, end - p, &addr[i], delim, &ipend);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001967 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001968 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001969 ret = -EINVAL;
1970
Sage Weil31b80062009-10-06 11:31:13 -07001971 p = ipend;
1972
Sage Weil39139f62010-07-08 09:54:52 -07001973 if (delim == ']') {
1974 if (*p != ']') {
1975 dout("missing matching ']'\n");
1976 goto bad;
1977 }
1978 p++;
1979 }
1980
Sage Weil31b80062009-10-06 11:31:13 -07001981 /* port? */
1982 if (p < end && *p == ':') {
1983 port = 0;
1984 p++;
1985 while (p < end && *p >= '0' && *p <= '9') {
1986 port = (port * 10) + (*p - '0');
1987 p++;
1988 }
Ilya Dryomovf48db1e2013-12-30 19:21:29 +02001989 if (port == 0)
1990 port = CEPH_MON_PORT;
1991 else if (port > 65535)
Sage Weil31b80062009-10-06 11:31:13 -07001992 goto bad;
1993 } else {
1994 port = CEPH_MON_PORT;
1995 }
1996
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01001997 ceph_addr_set_port(&addr[i], port);
Jeff Laytond3c3c0a2019-06-17 06:57:25 -04001998 addr[i].type = CEPH_ENTITY_ADDR_TYPE_LEGACY;
Sage Weil31b80062009-10-06 11:31:13 -07001999
Jeff Laytonb726ec92019-05-06 09:38:47 -04002000 dout("parse_ips got %s\n", ceph_pr_addr(&addr[i]));
Sage Weil31b80062009-10-06 11:31:13 -07002001
2002 if (p == end)
2003 break;
2004 if (*p != ',')
2005 goto bad;
2006 p++;
2007 }
2008
2009 if (p != end)
2010 goto bad;
2011
2012 if (count)
2013 *count = i + 1;
2014 return 0;
2015
2016bad:
Noah Watkinsee3b56f2011-09-23 11:48:42 -07002017 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002018}
2019
Sage Weileed0ef22009-11-10 14:34:36 -08002020static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002021{
Ilya Dryomovfd1a1542020-11-05 18:48:04 +01002022 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
2023
Sage Weileed0ef22009-11-10 14:34:36 -08002024 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07002025
2026 if (verify_hello(con) < 0)
2027 return -1;
2028
2029 /*
2030 * Make sure the other end is who we wanted. note that the other
2031 * end may not yet know their ip address, so if it's 0.0.0.0, give
2032 * them the benefit of the doubt.
2033 */
Sage Weil103e2d32010-01-07 16:12:36 -08002034 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2035 sizeof(con->peer_addr)) != 0 &&
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002036 !(ceph_addr_is_blank(&con->actual_peer_addr) &&
Sage Weil31b80062009-10-06 11:31:13 -07002037 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Ilya Dryomova9dfe312020-10-03 11:52:15 +02002038 pr_warn("wrong peer, want %s/%u, got %s/%u\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -04002039 ceph_pr_addr(&con->peer_addr),
Ilya Dryomova9dfe312020-10-03 11:52:15 +02002040 le32_to_cpu(con->peer_addr.nonce),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002041 ceph_pr_addr(&con->actual_peer_addr),
Ilya Dryomova9dfe312020-10-03 11:52:15 +02002042 le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08002043 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07002044 return -1;
2045 }
2046
2047 /*
2048 * did we learn our address?
2049 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002050 if (ceph_addr_is_blank(my_addr)) {
Ilya Dryomovfd1a1542020-11-05 18:48:04 +01002051 memcpy(&my_addr->in_addr,
Sage Weil31b80062009-10-06 11:31:13 -07002052 &con->peer_addr_for_me.in_addr,
2053 sizeof(con->peer_addr_for_me.in_addr));
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002054 ceph_addr_set_port(my_addr, 0);
2055 ceph_encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08002056 dout("process_banner learned my addr is %s\n",
Ilya Dryomovfd1a1542020-11-05 18:48:04 +01002057 ceph_pr_addr(my_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002058 }
2059
Sage Weileed0ef22009-11-10 14:34:36 -08002060 return 0;
2061}
2062
2063static int process_connect(struct ceph_connection *con)
2064{
Ilya Dryomov859bff52015-10-28 23:50:58 +01002065 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2066 u64 req_feat = from_msgr(con->msgr)->required_features;
Ilya Dryomovdcbbd972017-06-05 14:44:59 +02002067 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07002068 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08002069
Sage Weileed0ef22009-11-10 14:34:36 -08002070 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2071
Ilya Dryomov262614c2018-07-26 15:17:46 +02002072 if (con->auth) {
Ilya Dryomov0fd3fd02019-02-05 20:30:27 +01002073 int len = le32_to_cpu(con->in_reply.authorizer_len);
2074
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002075 /*
2076 * Any connection that defines ->get_authorizer()
Ilya Dryomov6daca132018-07-27 19:18:34 +02002077 * should also define ->add_authorizer_challenge() and
2078 * ->verify_authorizer_reply().
2079 *
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002080 * See get_connect_authorizer().
2081 */
Ilya Dryomov6daca132018-07-27 19:18:34 +02002082 if (con->in_reply.tag == CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER) {
2083 ret = con->ops->add_authorizer_challenge(
Ilya Dryomov0fd3fd02019-02-05 20:30:27 +01002084 con, con->auth->authorizer_reply_buf, len);
Ilya Dryomov6daca132018-07-27 19:18:34 +02002085 if (ret < 0)
2086 return ret;
2087
2088 con_out_kvec_reset(con);
2089 __prepare_write_connect(con);
2090 prepare_read_connect(con);
2091 return 0;
2092 }
2093
Ilya Dryomov0fd3fd02019-02-05 20:30:27 +01002094 if (len) {
2095 ret = con->ops->verify_authorizer_reply(con);
2096 if (ret < 0) {
2097 con->error_msg = "bad authorize reply";
2098 return ret;
2099 }
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002100 }
2101 }
2102
Sage Weil31b80062009-10-06 11:31:13 -07002103 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08002104 case CEPH_MSGR_TAG_FEATURES:
2105 pr_err("%s%lld %s feature set mismatch,"
2106 " my %llx < server's %llx, missing %llx\n",
2107 ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002108 ceph_pr_addr(&con->peer_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002109 sup_feat, server_feat, server_feat & ~sup_feat);
2110 con->error_msg = "missing required protocol features";
Sage Weil04a419f2009-12-23 09:30:21 -08002111 return -1;
2112
Sage Weil31b80062009-10-06 11:31:13 -07002113 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07002114 pr_err("%s%lld %s protocol version mismatch,"
2115 " my %d != server's %d\n",
2116 ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002117 ceph_pr_addr(&con->peer_addr),
Sage Weil31b80062009-10-06 11:31:13 -07002118 le32_to_cpu(con->out_connect.protocol_version),
2119 le32_to_cpu(con->in_reply.protocol_version));
2120 con->error_msg = "protocol version mismatch";
Sage Weil31b80062009-10-06 11:31:13 -07002121 return -1;
2122
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002123 case CEPH_MSGR_TAG_BADAUTHORIZER:
2124 con->auth_retry++;
2125 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2126 con->auth_retry);
2127 if (con->auth_retry == 2) {
2128 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002129 return -1;
2130 }
Jim Schutt6d4221b2012-08-10 10:37:38 -07002131 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002132 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002133 if (ret < 0)
2134 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07002135 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002136 break;
Sage Weil31b80062009-10-06 11:31:13 -07002137
2138 case CEPH_MSGR_TAG_RESETSESSION:
2139 /*
2140 * If we connected with a large connect_seq but the peer
2141 * has no record of a session with us (no connection, or
2142 * connect_seq == 0), they will send RESETSESION to indicate
2143 * that they must have reset their session, and may have
2144 * dropped messages.
2145 */
2146 dout("process_connect got RESET peer seq %u\n",
Sage Weil5bdca4e2012-07-10 11:53:34 -07002147 le32_to_cpu(con->in_reply.connect_seq));
Ilya Dryomovd3c12482020-11-11 14:16:45 +01002148 pr_info("%s%lld %s session reset\n",
2149 ENTITY_NAME(con->peer_name),
2150 ceph_pr_addr(&con->peer_addr));
Ilya Dryomov5963c3d2020-11-06 19:04:30 +01002151 ceph_con_reset_session(con);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002152 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002153 ret = prepare_write_connect(con);
2154 if (ret < 0)
2155 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002156 prepare_read_connect(con);
2157
2158 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08002159 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002160 if (con->ops->peer_reset)
2161 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08002162 mutex_lock(&con->mutex);
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002163 if (con->state != CEPH_CON_S_V1_CONNECT_MSG)
Sage Weil0da5d702011-05-19 11:21:05 -07002164 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07002165 break;
2166
2167 case CEPH_MSGR_TAG_RETRY_SESSION:
2168 /*
2169 * If we sent a smaller connect_seq than the peer has, try
2170 * again with a larger value.
2171 */
Sage Weil5bdca4e2012-07-10 11:53:34 -07002172 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002173 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil5bdca4e2012-07-10 11:53:34 -07002174 le32_to_cpu(con->in_reply.connect_seq));
2175 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002176 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002177 ret = prepare_write_connect(con);
2178 if (ret < 0)
2179 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002180 prepare_read_connect(con);
2181 break;
2182
2183 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2184 /*
2185 * If we sent a smaller global_seq than the peer has, try
2186 * again with a larger value.
2187 */
Sage Weileed0ef22009-11-10 14:34:36 -08002188 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002189 con->peer_global_seq,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002190 le32_to_cpu(con->in_reply.global_seq));
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002191 ceph_get_global_seq(con->msgr,
2192 le32_to_cpu(con->in_reply.global_seq));
Jim Schutt6d4221b2012-08-10 10:37:38 -07002193 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002194 ret = prepare_write_connect(con);
2195 if (ret < 0)
2196 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002197 prepare_read_connect(con);
2198 break;
2199
Sage Weil3a230832013-03-25 08:47:40 -07002200 case CEPH_MSGR_TAG_SEQ:
Sage Weil31b80062009-10-06 11:31:13 -07002201 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08002202 if (req_feat & ~server_feat) {
2203 pr_err("%s%lld %s protocol feature mismatch,"
2204 " my required %llx > server's %llx, need %llx\n",
2205 ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002206 ceph_pr_addr(&con->peer_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002207 req_feat, server_feat, req_feat & ~server_feat);
2208 con->error_msg = "missing required protocol features";
Sage Weil04a419f2009-12-23 09:30:21 -08002209 return -1;
2210 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002211
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002212 WARN_ON(con->state != CEPH_CON_S_V1_CONNECT_MSG);
2213 con->state = CEPH_CON_S_OPEN;
Sage Weil20e55c42013-03-25 09:30:13 -07002214 con->auth_retry = 0; /* we authenticated; clear flag */
Sage Weil31b80062009-10-06 11:31:13 -07002215 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2216 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07002217 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07002218 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2219 con->peer_global_seq,
2220 le32_to_cpu(con->in_reply.connect_seq),
2221 con->connect_seq);
2222 WARN_ON(con->connect_seq !=
2223 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08002224
2225 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002226 ceph_con_flag_set(con, CEPH_CON_F_LOSSYTX);
Sage Weil92ac41d2009-12-14 14:56:56 -08002227
Sage Weil85effe12012-07-30 16:22:05 -07002228 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -07002229
Sage Weil3a230832013-03-25 08:47:40 -07002230 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2231 prepare_write_seq(con);
2232 prepare_read_seq(con);
2233 } else {
2234 prepare_read_tag(con);
2235 }
Sage Weil31b80062009-10-06 11:31:13 -07002236 break;
2237
2238 case CEPH_MSGR_TAG_WAIT:
2239 /*
2240 * If there is a connection race (we are opening
2241 * connections to each other), one of us may just have
2242 * to WAIT. This shouldn't happen if we are the
2243 * client.
2244 */
Sage Weil04177882011-05-12 15:33:17 -07002245 con->error_msg = "protocol error, got WAIT as client";
2246 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07002247
2248 default:
Sage Weil31b80062009-10-06 11:31:13 -07002249 con->error_msg = "protocol error, garbage tag during connect";
2250 return -1;
2251 }
2252 return 0;
2253}
2254
2255
2256/*
2257 * read (part of) an ack
2258 */
2259static int read_partial_ack(struct ceph_connection *con)
2260{
Alex Elderfd516532012-05-10 10:29:50 -05002261 int size = sizeof (con->in_temp_ack);
2262 int end = size;
Sage Weil31b80062009-10-06 11:31:13 -07002263
Alex Elderfd516532012-05-10 10:29:50 -05002264 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07002265}
2266
Sage Weil31b80062009-10-06 11:31:13 -07002267/*
2268 * We can finally discard anything that's been acked.
2269 */
2270static void process_ack(struct ceph_connection *con)
2271{
Sage Weil31b80062009-10-06 11:31:13 -07002272 u64 ack = le64_to_cpu(con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07002273
Ilya Dryomov02471922020-10-13 17:38:53 +02002274 if (con->in_tag == CEPH_MSGR_TAG_ACK)
2275 ceph_con_discard_sent(con, ack);
2276 else
2277 ceph_con_discard_requeued(con, ack);
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002278
Sage Weil31b80062009-10-06 11:31:13 -07002279 prepare_read_tag(con);
2280}
2281
2282
Yehuda Sadeh24504182010-01-08 13:58:34 -08002283static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002284 struct kvec *section,
2285 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002286{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002287 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07002288
Yehuda Sadeh24504182010-01-08 13:58:34 -08002289 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07002290
Yehuda Sadeh24504182010-01-08 13:58:34 -08002291 while (section->iov_len < sec_len) {
2292 BUG_ON(section->iov_base == NULL);
2293 left = sec_len - section->iov_len;
2294 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2295 section->iov_len, left);
2296 if (ret <= 0)
2297 return ret;
2298 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002299 }
Alex Elderfe3ad592012-02-15 07:43:54 -06002300 if (section->iov_len == sec_len)
2301 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002302
2303 return 1;
2304}
2305
Alex Elder34d2d202013-03-08 20:58:59 -06002306static int read_partial_msg_data(struct ceph_connection *con)
2307{
2308 struct ceph_msg *msg = con->in_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05002309 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002310 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elder686be202013-03-11 23:34:23 -05002311 struct page *page;
2312 size_t page_offset;
2313 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05002314 u32 crc = 0;
Alex Elder34d2d202013-03-08 20:58:59 -06002315 int ret;
2316
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002317 if (!msg->num_data_items)
Alex Elder4c59b4a2013-03-11 23:34:23 -05002318 return -EIO;
Alex Elder34d2d202013-03-08 20:58:59 -06002319
Alex Elderf5db90b2013-03-11 23:34:23 -05002320 if (do_datacrc)
2321 crc = con->in_data_crc;
Ilya Dryomov45a267d2018-01-22 15:20:15 +01002322 while (cursor->total_resid) {
2323 if (!cursor->resid) {
2324 ceph_msg_data_advance(cursor, 0);
2325 continue;
2326 }
2327
Shraddha Barke343128c2015-10-19 21:59:00 +05302328 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Alex Elder686be202013-03-11 23:34:23 -05002329 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
Alex Elderf5db90b2013-03-11 23:34:23 -05002330 if (ret <= 0) {
2331 if (do_datacrc)
2332 con->in_data_crc = crc;
2333
Alex Elder686be202013-03-11 23:34:23 -05002334 return ret;
Alex Elderf5db90b2013-03-11 23:34:23 -05002335 }
Alex Elder686be202013-03-11 23:34:23 -05002336
2337 if (do_datacrc)
Alex Elderf5db90b2013-03-11 23:34:23 -05002338 crc = ceph_crc32c_page(crc, page, page_offset, ret);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02002339 ceph_msg_data_advance(cursor, (size_t)ret);
Alex Elder34d2d202013-03-08 20:58:59 -06002340 }
Alex Elderf5db90b2013-03-11 23:34:23 -05002341 if (do_datacrc)
2342 con->in_data_crc = crc;
Alex Elder34d2d202013-03-08 20:58:59 -06002343
2344 return 1; /* must return > 0 to indicate success */
2345}
2346
Sage Weil31b80062009-10-06 11:31:13 -07002347/*
2348 * read (part of) a message.
2349 */
2350static int read_partial_message(struct ceph_connection *con)
2351{
2352 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05002353 int size;
2354 int end;
Sage Weil31b80062009-10-06 11:31:13 -07002355 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00002356 unsigned int front_len, middle_len, data_len;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002357 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Yan, Zheng33d07332014-11-04 16:33:37 +08002358 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
Sage Weilae187562010-04-22 07:47:01 -07002359 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06002360 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07002361
2362 dout("read_partial_message con %p msg %p\n", con, m);
2363
2364 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05002365 size = sizeof (con->in_hdr);
2366 end = size;
2367 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05002368 if (ret <= 0)
2369 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06002370
2371 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2372 if (cpu_to_le32(crc) != con->in_hdr.crc) {
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002373 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
Alex Elderfe3ad592012-02-15 07:43:54 -06002374 crc, con->in_hdr.crc);
2375 return -EBADMSG;
2376 }
2377
Sage Weil31b80062009-10-06 11:31:13 -07002378 front_len = le32_to_cpu(con->in_hdr.front_len);
2379 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2380 return -EIO;
2381 middle_len = le32_to_cpu(con->in_hdr.middle_len);
Alex Elder7b11ba32013-03-08 18:51:03 -06002382 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
Sage Weil31b80062009-10-06 11:31:13 -07002383 return -EIO;
2384 data_len = le32_to_cpu(con->in_hdr.data_len);
2385 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2386 return -EIO;
2387
Sage Weilae187562010-04-22 07:47:01 -07002388 /* verify seq# */
2389 seq = le64_to_cpu(con->in_hdr.seq);
2390 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07002391 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07002392 ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002393 ceph_pr_addr(&con->peer_addr),
Sage Weilae187562010-04-22 07:47:01 -07002394 seq, con->in_seq + 1);
2395 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002396 sizeof_footer(con);
Sage Weilae187562010-04-22 07:47:01 -07002397 con->in_tag = CEPH_MSGR_TAG_READY;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002398 return 1;
Sage Weilae187562010-04-22 07:47:01 -07002399 } else if ((s64)seq - (s64)con->in_seq > 1) {
2400 pr_err("read_partial_message bad seq %lld expected %lld\n",
2401 seq, con->in_seq + 1);
2402 con->error_msg = "bad message sequence # for incoming message";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002403 return -EBADE;
Sage Weilae187562010-04-22 07:47:01 -07002404 }
2405
Sage Weil31b80062009-10-06 11:31:13 -07002406 /* allocate message? */
2407 if (!con->in_msg) {
Sage Weil4740a622012-07-30 18:19:30 -07002408 int skip = 0;
2409
Sage Weil31b80062009-10-06 11:31:13 -07002410 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
Alex Elder6ebc8b32013-03-08 18:51:03 -06002411 front_len, data_len);
Ilya Dryomovfc4c1282020-11-16 17:27:50 +01002412 ret = ceph_con_in_msg_alloc(con, &con->in_hdr, &skip);
Sage Weil4740a622012-07-30 18:19:30 -07002413 if (ret < 0)
2414 return ret;
Alex Elderf759ebb2013-04-05 14:46:01 -05002415
2416 BUG_ON(!con->in_msg ^ skip);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002417 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07002418 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07002419 dout("alloc_msg said skip message\n");
Sage Weil31b80062009-10-06 11:31:13 -07002420 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002421 sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07002422 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002423 con->in_seq++;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002424 return 1;
Sage Weil31b80062009-10-06 11:31:13 -07002425 }
Alex Elder38941f82012-06-01 14:56:43 -05002426
Sage Weil4740a622012-07-30 18:19:30 -07002427 BUG_ON(!con->in_msg);
Alex Elder38941f82012-06-01 14:56:43 -05002428 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002429 m = con->in_msg;
2430 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002431 if (m->middle)
2432 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002433
Alex Elder78625052013-03-06 23:39:39 -06002434 /* prepare for data payload, if any */
Sage Weila4107022012-07-30 16:20:25 -07002435
Alex Elder78625052013-03-06 23:39:39 -06002436 if (data_len)
Alex Elder98fa5dd2013-04-02 12:09:50 -05002437 prepare_message_data(con->in_msg, data_len);
Sage Weil31b80062009-10-06 11:31:13 -07002438 }
2439
2440 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002441 ret = read_partial_message_section(con, &m->front, front_len,
2442 &con->in_front_crc);
2443 if (ret <= 0)
2444 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002445
2446 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002447 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07002448 ret = read_partial_message_section(con, &m->middle->vec,
2449 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002450 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07002451 if (ret <= 0)
2452 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002453 }
2454
2455 /* (page) data */
Alex Elder34d2d202013-03-08 20:58:59 -06002456 if (data_len) {
2457 ret = read_partial_msg_data(con);
2458 if (ret <= 0)
2459 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002460 }
2461
Sage Weil31b80062009-10-06 11:31:13 -07002462 /* footer */
Ilya Dryomov89f08172016-02-20 15:56:07 +01002463 size = sizeof_footer(con);
Alex Elderfd516532012-05-10 10:29:50 -05002464 end += size;
2465 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05002466 if (ret <= 0)
2467 return ret;
2468
Yan, Zheng33d07332014-11-04 16:33:37 +08002469 if (!need_sign) {
2470 m->footer.flags = m->old_footer.flags;
2471 m->footer.sig = 0;
2472 }
2473
Sage Weil31b80062009-10-06 11:31:13 -07002474 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2475 m, front_len, m->footer.front_crc, middle_len,
2476 m->footer.middle_crc, data_len, m->footer.data_crc);
2477
2478 /* crc ok? */
2479 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2480 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2481 m, con->in_front_crc, m->footer.front_crc);
2482 return -EBADMSG;
2483 }
2484 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2485 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2486 m, con->in_middle_crc, m->footer.middle_crc);
2487 return -EBADMSG;
2488 }
Alex Elderbca064d2012-02-15 07:43:54 -06002489 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07002490 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2491 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2492 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2493 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2494 return -EBADMSG;
2495 }
2496
Yan, Zheng33d07332014-11-04 16:33:37 +08002497 if (need_sign && con->ops->check_message_signature &&
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002498 con->ops->check_message_signature(m)) {
Yan, Zheng33d07332014-11-04 16:33:37 +08002499 pr_err("read_partial_message %p signature check failed\n", m);
2500 return -EBADMSG;
2501 }
2502
Sage Weil31b80062009-10-06 11:31:13 -07002503 return 1; /* done! */
2504}
2505
2506/*
2507 * Process message. This happens in the worker thread. The callback should
2508 * be careful not to do anything that waits on other incoming messages or it
2509 * may deadlock.
2510 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002511void ceph_con_process_message(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002512{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002513 struct ceph_msg *msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07002514
Alex Elder38941f82012-06-01 14:56:43 -05002515 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002516 con->in_msg = NULL;
2517
2518 /* if first message, set peer_name */
2519 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07002520 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07002521
Sage Weil31b80062009-10-06 11:31:13 -07002522 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08002523 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002524
Ilya Dryomovb77f8f0e2020-11-05 15:01:53 +01002525 dout("===== %p %llu from %s%lld %d=%s len %d+%d+%d (%u %u %u) =====\n",
Sage Weil31b80062009-10-06 11:31:13 -07002526 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07002527 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07002528 le16_to_cpu(msg->hdr.type),
2529 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2530 le32_to_cpu(msg->hdr.front_len),
Ilya Dryomovb77f8f0e2020-11-05 15:01:53 +01002531 le32_to_cpu(msg->hdr.middle_len),
Sage Weil31b80062009-10-06 11:31:13 -07002532 le32_to_cpu(msg->hdr.data_len),
2533 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2534 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08002535
2536 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002537}
2538
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002539static int read_keepalive_ack(struct ceph_connection *con)
2540{
2541 struct ceph_timespec ceph_ts;
2542 size_t size = sizeof(ceph_ts);
2543 int ret = read_partial(con, size, size, &ceph_ts);
2544 if (ret <= 0)
2545 return ret;
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02002546 ceph_decode_timespec64(&con->last_keepalive_ack, &ceph_ts);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002547 prepare_read_tag(con);
2548 return 1;
2549}
Sage Weil31b80062009-10-06 11:31:13 -07002550
2551/*
2552 * Write something to the socket. Called in a worker thread when the
2553 * socket appears to be writeable and we have something ready to send.
2554 */
2555static int try_write(struct ceph_connection *con)
2556{
Sage Weil31b80062009-10-06 11:31:13 -07002557 int ret = 1;
2558
Ilya Dryomov30be7802020-11-09 14:11:26 +01002559 dout("try_write start %p state %d\n", con, con->state);
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002560 if (con->state != CEPH_CON_S_PREOPEN &&
2561 con->state != CEPH_CON_S_V1_BANNER &&
2562 con->state != CEPH_CON_S_V1_CONNECT_MSG &&
2563 con->state != CEPH_CON_S_OPEN)
Ilya Dryomov9c55ad12018-04-24 19:10:55 +02002564 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07002565
Sage Weil31b80062009-10-06 11:31:13 -07002566 /* open the socket first? */
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002567 if (con->state == CEPH_CON_S_PREOPEN) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002568 BUG_ON(con->sock);
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002569 con->state = CEPH_CON_S_V1_BANNER;
Alex Eldera5988c42012-05-29 11:04:58 -05002570
Alex Eldere2200422012-05-23 14:35:23 -05002571 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002572 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002573 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002574
Sage Weilcf3e5c42009-12-11 09:48:05 -08002575 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002576 con->in_tag = CEPH_MSGR_TAG_READY;
Ilya Dryomov30be7802020-11-09 14:11:26 +01002577 dout("try_write initiating connect on %p new state %d\n",
Sage Weil31b80062009-10-06 11:31:13 -07002578 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002579 ret = ceph_tcp_connect(con);
2580 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002581 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002582 goto out;
2583 }
2584 }
2585
Ilya Dryomovd2935d62018-04-25 12:17:13 +02002586more:
2587 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
Ilya Dryomov9c55ad12018-04-24 19:10:55 +02002588 BUG_ON(!con->sock);
2589
Sage Weil31b80062009-10-06 11:31:13 -07002590 /* kvec data queued? */
Ilya Dryomov67645d72015-12-28 13:18:34 +03002591 if (con->out_kvec_left) {
2592 ret = write_partial_kvec(con);
Sage Weil31b80062009-10-06 11:31:13 -07002593 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002594 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002595 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03002596 if (con->out_skip) {
2597 ret = write_partial_skip(con);
Sage Weil31b80062009-10-06 11:31:13 -07002598 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002599 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002600 }
2601
2602 /* msg pages? */
2603 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002604 if (con->out_msg_done) {
2605 ceph_msg_put(con->out_msg);
2606 con->out_msg = NULL; /* we're done with this one */
2607 goto do_next;
2608 }
2609
Alex Elder34d2d202013-03-08 20:58:59 -06002610 ret = write_partial_message_data(con);
Sage Weil31b80062009-10-06 11:31:13 -07002611 if (ret == 1)
Ilya Dryomovd2935d62018-04-25 12:17:13 +02002612 goto more; /* we need to send the footer, too! */
Sage Weil31b80062009-10-06 11:31:13 -07002613 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002614 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002615 if (ret < 0) {
Alex Elder34d2d202013-03-08 20:58:59 -06002616 dout("try_write write_partial_message_data err %d\n",
Sage Weil31b80062009-10-06 11:31:13 -07002617 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002618 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002619 }
2620 }
2621
Sage Weilc86a2932009-12-14 14:04:30 -08002622do_next:
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002623 if (con->state == CEPH_CON_S_OPEN) {
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002624 if (ceph_con_flag_test_and_clear(con,
Ilya Dryomov3fefd432020-11-09 14:56:36 +01002625 CEPH_CON_F_KEEPALIVE_PENDING)) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002626 prepare_write_keepalive(con);
2627 goto more;
2628 }
Sage Weil31b80062009-10-06 11:31:13 -07002629 /* is anything else pending? */
2630 if (!list_empty(&con->out_queue)) {
2631 prepare_write_message(con);
2632 goto more;
2633 }
2634 if (con->in_seq > con->in_seq_acked) {
2635 prepare_write_ack(con);
2636 goto more;
2637 }
Sage Weil31b80062009-10-06 11:31:13 -07002638 }
2639
2640 /* Nothing to do! */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002641 ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07002642 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002643 ret = 0;
2644out:
Sage Weil42961d22011-01-25 08:19:34 -08002645 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002646 return ret;
2647}
2648
Sage Weil31b80062009-10-06 11:31:13 -07002649/*
2650 * Read what we can from the socket.
2651 */
2652static int try_read(struct ceph_connection *con)
2653{
Sage Weil31b80062009-10-06 11:31:13 -07002654 int ret = -1;
2655
Sage Weil31b80062009-10-06 11:31:13 -07002656more:
Ilya Dryomov30be7802020-11-09 14:11:26 +01002657 dout("try_read start %p state %d\n", con, con->state);
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002658 if (con->state != CEPH_CON_S_V1_BANNER &&
2659 con->state != CEPH_CON_S_V1_CONNECT_MSG &&
2660 con->state != CEPH_CON_S_OPEN)
Sage Weil8dacc7d2012-07-20 17:24:40 -07002661 return 0;
2662
2663 BUG_ON(!con->sock);
2664
Sage Weil31b80062009-10-06 11:31:13 -07002665 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2666 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002667
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002668 if (con->state == CEPH_CON_S_V1_BANNER) {
Alex Elder7593af92012-05-24 11:55:03 -05002669 ret = read_partial_banner(con);
2670 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002671 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002672 ret = process_banner(con);
2673 if (ret < 0)
2674 goto out;
2675
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002676 con->state = CEPH_CON_S_V1_CONNECT_MSG;
Alex Elder7593af92012-05-24 11:55:03 -05002677
Jim Schutt6d4221b2012-08-10 10:37:38 -07002678 /*
2679 * Received banner is good, exchange connection info.
2680 * Do not reset out_kvec, as sending our banner raced
2681 * with receiving peer banner after connect completed.
2682 */
Alex Elder7593af92012-05-24 11:55:03 -05002683 ret = prepare_write_connect(con);
2684 if (ret < 0)
2685 goto out;
2686 prepare_read_connect(con);
2687
2688 /* Send connection info before awaiting response */
Sage Weil0da5d702011-05-19 11:21:05 -07002689 goto out;
2690 }
2691
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002692 if (con->state == CEPH_CON_S_V1_CONNECT_MSG) {
Sage Weil31b80062009-10-06 11:31:13 -07002693 ret = read_partial_connect(con);
2694 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002695 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002696 ret = process_connect(con);
2697 if (ret < 0)
2698 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002699 goto more;
2700 }
2701
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002702 WARN_ON(con->state != CEPH_CON_S_OPEN);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002703
Sage Weil31b80062009-10-06 11:31:13 -07002704 if (con->in_base_pos < 0) {
2705 /*
2706 * skipping + discarding content.
Sage Weil31b80062009-10-06 11:31:13 -07002707 */
Ilya Dryomove5c93882018-04-27 18:58:47 +02002708 ret = ceph_tcp_recvmsg(con->sock, NULL, -con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07002709 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002710 goto out;
Ilya Dryomove5c93882018-04-27 18:58:47 +02002711 dout("skipped %d / %d bytes\n", ret, -con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07002712 con->in_base_pos += ret;
2713 if (con->in_base_pos)
2714 goto more;
2715 }
2716 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2717 /*
2718 * what's next?
2719 */
2720 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2721 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002722 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002723 dout("try_read got tag %d\n", (int)con->in_tag);
2724 switch (con->in_tag) {
2725 case CEPH_MSGR_TAG_MSG:
2726 prepare_read_message(con);
2727 break;
2728 case CEPH_MSGR_TAG_ACK:
2729 prepare_read_ack(con);
2730 break;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002731 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2732 prepare_read_keepalive_ack(con);
2733 break;
Sage Weil31b80062009-10-06 11:31:13 -07002734 case CEPH_MSGR_TAG_CLOSE:
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002735 ceph_con_close_socket(con);
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002736 con->state = CEPH_CON_S_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002737 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002738 default:
2739 goto bad_tag;
2740 }
2741 }
2742 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2743 ret = read_partial_message(con);
2744 if (ret <= 0) {
2745 switch (ret) {
2746 case -EBADMSG:
Ilya Dryomova51983e2015-10-28 23:52:06 +01002747 con->error_msg = "bad crc/signature";
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002748 fallthrough;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002749 case -EBADE:
Sage Weil31b80062009-10-06 11:31:13 -07002750 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002751 break;
Sage Weil31b80062009-10-06 11:31:13 -07002752 case -EIO:
2753 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002754 break;
Sage Weil31b80062009-10-06 11:31:13 -07002755 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002756 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002757 }
2758 if (con->in_tag == CEPH_MSGR_TAG_READY)
2759 goto more;
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002760 ceph_con_process_message(con);
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002761 if (con->state == CEPH_CON_S_OPEN)
Sage Weil7b862e02012-07-30 18:16:56 -07002762 prepare_read_tag(con);
Sage Weil31b80062009-10-06 11:31:13 -07002763 goto more;
2764 }
Sage Weil3a230832013-03-25 08:47:40 -07002765 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2766 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2767 /*
2768 * the final handshake seq exchange is semantically
2769 * equivalent to an ACK
2770 */
Sage Weil31b80062009-10-06 11:31:13 -07002771 ret = read_partial_ack(con);
2772 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002773 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002774 process_ack(con);
2775 goto more;
2776 }
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002777 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2778 ret = read_keepalive_ack(con);
2779 if (ret <= 0)
2780 goto out;
2781 goto more;
2782 }
Sage Weil31b80062009-10-06 11:31:13 -07002783
Sage Weil31b80062009-10-06 11:31:13 -07002784out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002785 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002786 return ret;
2787
2788bad_tag:
2789 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2790 con->error_msg = "protocol error, garbage tag";
2791 ret = -1;
2792 goto out;
2793}
2794
2795
2796/*
Alex Elder802c6d92012-10-08 20:37:30 -07002797 * Atomically queue work on a connection after the specified delay.
2798 * Bump @con reference to avoid races with connection teardown.
2799 * Returns 0 if work was queued, or an error code otherwise.
Sage Weil31b80062009-10-06 11:31:13 -07002800 */
Alex Elder802c6d92012-10-08 20:37:30 -07002801static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
Sage Weil31b80062009-10-06 11:31:13 -07002802{
Sage Weil31b80062009-10-06 11:31:13 -07002803 if (!con->ops->get(con)) {
Alex Elder802c6d92012-10-08 20:37:30 -07002804 dout("%s %p ref count 0\n", __func__, con);
Alex Elder802c6d92012-10-08 20:37:30 -07002805 return -ENOENT;
Sage Weil31b80062009-10-06 11:31:13 -07002806 }
2807
Ilya Dryomov418af5b2020-10-29 14:49:10 +01002808 if (delay >= HZ)
2809 delay = round_jiffies_relative(delay);
2810
Ilya Dryomov5a5036c2020-10-02 14:15:59 +02002811 dout("%s %p %lu\n", __func__, con, delay);
Alex Elder802c6d92012-10-08 20:37:30 -07002812 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2813 dout("%s %p - already queued\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -07002814 con->ops->put(con);
Alex Elder802c6d92012-10-08 20:37:30 -07002815 return -EBUSY;
Sage Weil31b80062009-10-06 11:31:13 -07002816 }
Alex Elder802c6d92012-10-08 20:37:30 -07002817
Alex Elder802c6d92012-10-08 20:37:30 -07002818 return 0;
2819}
2820
2821static void queue_con(struct ceph_connection *con)
2822{
2823 (void) queue_con_delay(con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002824}
2825
Ilya Dryomov37ab77a2014-06-24 16:21:45 +04002826static void cancel_con(struct ceph_connection *con)
2827{
2828 if (cancel_delayed_work(&con->work)) {
2829 dout("%s %p\n", __func__, con);
2830 con->ops->put(con);
2831 }
2832}
2833
Alex Elder7bb21d682012-12-07 19:50:07 -06002834static bool con_sock_closed(struct ceph_connection *con)
2835{
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002836 if (!ceph_con_flag_test_and_clear(con, CEPH_CON_F_SOCK_CLOSED))
Alex Elder7bb21d682012-12-07 19:50:07 -06002837 return false;
2838
2839#define CASE(x) \
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002840 case CEPH_CON_S_ ## x: \
Alex Elder7bb21d682012-12-07 19:50:07 -06002841 con->error_msg = "socket closed (con state " #x ")"; \
2842 break;
2843
2844 switch (con->state) {
2845 CASE(CLOSED);
2846 CASE(PREOPEN);
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002847 CASE(V1_BANNER);
2848 CASE(V1_CONNECT_MSG);
Alex Elder7bb21d682012-12-07 19:50:07 -06002849 CASE(OPEN);
2850 CASE(STANDBY);
2851 default:
Alex Elder7bb21d682012-12-07 19:50:07 -06002852 BUG();
Alex Elder7bb21d682012-12-07 19:50:07 -06002853 }
2854#undef CASE
2855
2856 return true;
2857}
2858
Alex Elderf20a39f2013-02-19 12:25:57 -06002859static bool con_backoff(struct ceph_connection *con)
2860{
2861 int ret;
2862
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002863 if (!ceph_con_flag_test_and_clear(con, CEPH_CON_F_BACKOFF))
Alex Elderf20a39f2013-02-19 12:25:57 -06002864 return false;
2865
Ilya Dryomov418af5b2020-10-29 14:49:10 +01002866 ret = queue_con_delay(con, con->delay);
Alex Elderf20a39f2013-02-19 12:25:57 -06002867 if (ret) {
2868 dout("%s: con %p FAILED to back off %lu\n", __func__,
2869 con, con->delay);
2870 BUG_ON(ret == -ENOENT);
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002871 ceph_con_flag_set(con, CEPH_CON_F_BACKOFF);
Alex Elderf20a39f2013-02-19 12:25:57 -06002872 }
2873
2874 return true;
2875}
2876
Alex Elder93209262013-02-19 12:25:57 -06002877/* Finish fault handling; con->mutex must *not* be held here */
2878
2879static void con_fault_finish(struct ceph_connection *con)
2880{
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002881 dout("%s %p\n", __func__, con);
2882
Alex Elder93209262013-02-19 12:25:57 -06002883 /*
2884 * in case we faulted due to authentication, invalidate our
2885 * current tickets so that we can get new ones.
2886 */
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002887 if (con->auth_retry) {
2888 dout("auth_retry %d, invalidating\n", con->auth_retry);
2889 if (con->ops->invalidate_authorizer)
2890 con->ops->invalidate_authorizer(con);
2891 con->auth_retry = 0;
Alex Elder93209262013-02-19 12:25:57 -06002892 }
2893
2894 if (con->ops->fault)
2895 con->ops->fault(con);
2896}
2897
Sage Weil31b80062009-10-06 11:31:13 -07002898/*
2899 * Do some work on a connection. Drop a connection ref when we're done.
2900 */
Ilya Dryomov68931622015-07-03 15:44:41 +03002901static void ceph_con_workfn(struct work_struct *work)
Sage Weil31b80062009-10-06 11:31:13 -07002902{
2903 struct ceph_connection *con = container_of(work, struct ceph_connection,
2904 work.work);
Alex Elder49659412013-02-19 12:25:57 -06002905 bool fault;
Sage Weil31b80062009-10-06 11:31:13 -07002906
Sage Weil9dd46582010-04-28 13:51:50 -07002907 mutex_lock(&con->mutex);
Alex Elder49659412013-02-19 12:25:57 -06002908 while (true) {
2909 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002910
Alex Elder49659412013-02-19 12:25:57 -06002911 if ((fault = con_sock_closed(con))) {
2912 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2913 break;
2914 }
2915 if (con_backoff(con)) {
2916 dout("%s: con %p BACKOFF\n", __func__, con);
2917 break;
2918 }
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002919 if (con->state == CEPH_CON_S_STANDBY) {
Alex Elder49659412013-02-19 12:25:57 -06002920 dout("%s: con %p STANDBY\n", __func__, con);
2921 break;
2922 }
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002923 if (con->state == CEPH_CON_S_CLOSED) {
Alex Elder49659412013-02-19 12:25:57 -06002924 dout("%s: con %p CLOSED\n", __func__, con);
2925 BUG_ON(con->sock);
2926 break;
2927 }
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002928 if (con->state == CEPH_CON_S_PREOPEN) {
Alex Elder49659412013-02-19 12:25:57 -06002929 dout("%s: con %p PREOPEN\n", __func__, con);
2930 BUG_ON(con->sock);
2931 }
Sage Weil0da5d702011-05-19 11:21:05 -07002932
Alex Elder49659412013-02-19 12:25:57 -06002933 ret = try_read(con);
2934 if (ret < 0) {
2935 if (ret == -EAGAIN)
2936 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002937 if (!con->error_msg)
2938 con->error_msg = "socket error on read";
Alex Elder49659412013-02-19 12:25:57 -06002939 fault = true;
2940 break;
2941 }
2942
2943 ret = try_write(con);
2944 if (ret < 0) {
2945 if (ret == -EAGAIN)
2946 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002947 if (!con->error_msg)
2948 con->error_msg = "socket error on write";
Alex Elder49659412013-02-19 12:25:57 -06002949 fault = true;
2950 }
2951
2952 break; /* If we make it to here, we're done */
Sage Weil3a140a02012-07-30 16:24:21 -07002953 }
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002954 if (fault)
2955 con_fault(con);
Sage Weil9dd46582010-04-28 13:51:50 -07002956 mutex_unlock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002957
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002958 if (fault)
2959 con_fault_finish(con);
2960
2961 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07002962}
2963
Sage Weil31b80062009-10-06 11:31:13 -07002964/*
2965 * Generic error/fault handler. A retry mechanism is used with
2966 * exponential backoff
2967 */
Alex Elder93209262013-02-19 12:25:57 -06002968static void con_fault(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002969{
Ilya Dryomov30be7802020-11-09 14:11:26 +01002970 dout("fault %p state %d to peer %s\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -04002971 con, con->state, ceph_pr_addr(&con->peer_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002972
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002973 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Jeff Laytonb726ec92019-05-06 09:38:47 -04002974 ceph_pr_addr(&con->peer_addr), con->error_msg);
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002975 con->error_msg = NULL;
2976
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002977 WARN_ON(con->state != CEPH_CON_S_V1_BANNER &&
2978 con->state != CEPH_CON_S_V1_CONNECT_MSG &&
2979 con->state != CEPH_CON_S_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08002980
Ilya Dryomov3596f4c2020-11-06 17:07:07 +01002981 ceph_con_reset_protocol(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002982
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002983 if (ceph_con_flag_test(con, CEPH_CON_F_LOSSYTX)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002984 dout("fault on LOSSYTX channel, marking CLOSED\n");
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002985 con->state = CEPH_CON_S_CLOSED;
Alex Elder93209262013-02-19 12:25:57 -06002986 return;
Sage Weil3b5ede02012-07-20 15:22:53 -07002987 }
2988
Sage Weile80a52d2010-02-25 12:40:45 -08002989 /* Requeue anything that hasn't been acked */
2990 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002991
Sage Weile76661d2011-03-03 10:10:15 -08002992 /* If there are no messages queued or keepalive pending, place
2993 * the connection in a STANDBY state */
2994 if (list_empty(&con->out_queue) &&
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002995 !ceph_con_flag_test(con, CEPH_CON_F_KEEPALIVE_PENDING)) {
Sage Weile00de342011-03-04 12:25:05 -08002996 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01002997 ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01002998 con->state = CEPH_CON_S_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08002999 } else {
3000 /* retry after a delay. */
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01003001 con->state = CEPH_CON_S_PREOPEN;
Ilya Dryomov418af5b2020-10-29 14:49:10 +01003002 if (!con->delay) {
Sage Weile80a52d2010-02-25 12:40:45 -08003003 con->delay = BASE_DELAY_INTERVAL;
Ilya Dryomov418af5b2020-10-29 14:49:10 +01003004 } else if (con->delay < MAX_DELAY_INTERVAL) {
Sage Weile80a52d2010-02-25 12:40:45 -08003005 con->delay *= 2;
Ilya Dryomov418af5b2020-10-29 14:49:10 +01003006 if (con->delay > MAX_DELAY_INTERVAL)
3007 con->delay = MAX_DELAY_INTERVAL;
3008 }
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01003009 ceph_con_flag_set(con, CEPH_CON_F_BACKOFF);
Alex Elder8618e302012-10-08 20:37:30 -07003010 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -07003011 }
Sage Weil31b80062009-10-06 11:31:13 -07003012}
3013
3014
Yan, Zheng120a75e2019-07-25 20:16:39 +08003015void ceph_messenger_reset_nonce(struct ceph_messenger *msgr)
3016{
3017 u32 nonce = le32_to_cpu(msgr->inst.addr.nonce) + 1000000;
3018 msgr->inst.addr.nonce = cpu_to_le32(nonce);
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01003019 ceph_encode_my_addr(msgr);
Yan, Zheng120a75e2019-07-25 20:16:39 +08003020}
Sage Weil31b80062009-10-06 11:31:13 -07003021
3022/*
Alex Elder15d98822012-05-26 23:26:43 -05003023 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07003024 */
Alex Elder15d98822012-05-26 23:26:43 -05003025void ceph_messenger_init(struct ceph_messenger *msgr,
Ilya Dryomov859bff52015-10-28 23:50:58 +01003026 struct ceph_entity_addr *myaddr)
Sage Weil31b80062009-10-06 11:31:13 -07003027{
Sage Weil31b80062009-10-06 11:31:13 -07003028 spin_lock_init(&msgr->global_seq_lock);
3029
Ilya Dryomovfd1a1542020-11-05 18:48:04 +01003030 if (myaddr) {
3031 memcpy(&msgr->inst.addr.in_addr, &myaddr->in_addr,
3032 sizeof(msgr->inst.addr.in_addr));
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01003033 ceph_addr_set_port(&msgr->inst.addr, 0);
Ilya Dryomovfd1a1542020-11-05 18:48:04 +01003034 }
Sage Weil31b80062009-10-06 11:31:13 -07003035
Sage Weilac8839d2010-01-27 14:28:10 -08003036 msgr->inst.addr.type = 0;
Ilya Dryomovfd1a1542020-11-05 18:48:04 +01003037
3038 /* generate a random non-zero nonce */
3039 do {
3040 get_random_bytes(&msgr->inst.addr.nonce,
3041 sizeof(msgr->inst.addr.nonce));
3042 } while (!msgr->inst.addr.nonce);
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01003043 ceph_encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003044
Guanjun Hea2a32582012-07-08 19:50:33 -07003045 atomic_set(&msgr->stopping, 0);
Ilya Dryomov757856d2015-06-25 17:47:45 +03003046 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
Sage Weil31b80062009-10-06 11:31:13 -07003047
Alex Elder15d98822012-05-26 23:26:43 -05003048 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003049}
3050
Ilya Dryomov757856d2015-06-25 17:47:45 +03003051void ceph_messenger_fini(struct ceph_messenger *msgr)
3052{
3053 put_net(read_pnet(&msgr->net));
3054}
Ilya Dryomov757856d2015-06-25 17:47:45 +03003055
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003056static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3057{
3058 if (msg->con)
3059 msg->con->ops->put(msg->con);
3060
3061 msg->con = con ? con->ops->get(con) : NULL;
3062 BUG_ON(msg->con != con);
3063}
3064
Sage Weile00de342011-03-04 12:25:05 -08003065static void clear_standby(struct ceph_connection *con)
3066{
3067 /* come back from STANDBY? */
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01003068 if (con->state == CEPH_CON_S_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08003069 dout("clear_standby %p and ++connect_seq\n", con);
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01003070 con->state = CEPH_CON_S_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08003071 con->connect_seq++;
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01003072 WARN_ON(ceph_con_flag_test(con, CEPH_CON_F_WRITE_PENDING));
3073 WARN_ON(ceph_con_flag_test(con, CEPH_CON_F_KEEPALIVE_PENDING));
Sage Weile00de342011-03-04 12:25:05 -08003074 }
3075}
3076
Sage Weil31b80062009-10-06 11:31:13 -07003077/*
3078 * Queue up an outgoing message on the given connection.
Ilya Dryomov771294f2020-11-18 16:37:14 +01003079 *
3080 * Consumes a ref on @msg.
Sage Weil31b80062009-10-06 11:31:13 -07003081 */
3082void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3083{
Sage Weila59b55a2012-07-20 15:34:04 -07003084 /* set src+dst */
3085 msg->hdr.src = con->msgr->inst.name;
3086 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3087 msg->needs_out_seq = true;
3088
3089 mutex_lock(&con->mutex);
3090
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01003091 if (con->state == CEPH_CON_S_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07003092 dout("con_send %p closed, dropping %p\n", con, msg);
3093 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07003094 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003095 return;
3096 }
3097
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003098 msg_con_set(msg, con);
Sage Weil31b80062009-10-06 11:31:13 -07003099
Sage Weil31b80062009-10-06 11:31:13 -07003100 BUG_ON(!list_empty(&msg->list_head));
3101 list_add_tail(&msg->list_head, &con->out_queue);
3102 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3103 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3104 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3105 le32_to_cpu(msg->hdr.front_len),
3106 le32_to_cpu(msg->hdr.middle_len),
3107 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07003108
3109 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08003110 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003111
3112 /* if there wasn't anything waiting to send before, queue
3113 * new work */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01003114 if (!ceph_con_flag_test_and_set(con, CEPH_CON_F_WRITE_PENDING))
Sage Weil31b80062009-10-06 11:31:13 -07003115 queue_con(con);
3116}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003117EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07003118
3119/*
3120 * Revoke a message that was previously queued for send
3121 */
Alex Elder6740a842012-06-01 14:56:43 -05003122void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003123{
Alex Elder6740a842012-06-01 14:56:43 -05003124 struct ceph_connection *con = msg->con;
3125
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003126 if (!con) {
3127 dout("%s msg %p null con\n", __func__, msg);
Alex Elder6740a842012-06-01 14:56:43 -05003128 return; /* Message not in our possession */
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003129 }
Alex Elder6740a842012-06-01 14:56:43 -05003130
Sage Weilec302642009-12-22 10:43:42 -08003131 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003132 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05003133 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07003134 list_del_init(&msg->list_head);
Sage Weil31b80062009-10-06 11:31:13 -07003135 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05003136
Sage Weil31b80062009-10-06 11:31:13 -07003137 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07003138 }
3139 if (con->out_msg == msg) {
Ilya Dryomov67645d72015-12-28 13:18:34 +03003140 BUG_ON(con->out_skip);
3141 /* footer */
3142 if (con->out_msg_done) {
3143 con->out_skip += con_out_kvec_skip(con);
3144 } else {
3145 BUG_ON(!msg->data_length);
Ilya Dryomov89f08172016-02-20 15:56:07 +01003146 con->out_skip += sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07003147 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003148 /* data, middle, front */
3149 if (msg->data_length)
3150 con->out_skip += msg->cursor.total_resid;
3151 if (msg->middle)
3152 con->out_skip += con_out_kvec_skip(con);
3153 con->out_skip += con_out_kvec_skip(con);
Alex Elder92ce0342012-06-04 14:43:33 -05003154
Ilya Dryomov67645d72015-12-28 13:18:34 +03003155 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3156 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3157 msg->hdr.seq = 0;
3158 con->out_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05003159 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07003160 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003161
Sage Weilec302642009-12-22 10:43:42 -08003162 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003163}
3164
3165/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003166 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08003167 */
Alex Elder8921d112012-06-01 14:56:43 -05003168void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08003169{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003170 struct ceph_connection *con = msg->con;
Alex Elder8921d112012-06-01 14:56:43 -05003171
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003172 if (!con) {
Alex Elder8921d112012-06-01 14:56:43 -05003173 dout("%s msg %p null con\n", __func__, msg);
Alex Elder8921d112012-06-01 14:56:43 -05003174 return; /* Message not in our possession */
3175 }
3176
Sage Weil350b1c32009-12-22 10:45:45 -08003177 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05003178 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00003179 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3180 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3181 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08003182
3183 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05003184 dout("%s %p msg %p revoked\n", __func__, con, msg);
3185 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08003186 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003187 front_len -
3188 middle_len -
3189 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08003190 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08003191 ceph_msg_put(con->in_msg);
3192 con->in_msg = NULL;
3193 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07003194 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08003195 } else {
Alex Elder8921d112012-06-01 14:56:43 -05003196 dout("%s %p in_msg %p msg %p no-op\n",
3197 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08003198 }
3199 mutex_unlock(&con->mutex);
3200}
3201
3202/*
Sage Weil31b80062009-10-06 11:31:13 -07003203 * Queue a keepalive byte to ensure the tcp connection is alive.
3204 */
3205void ceph_con_keepalive(struct ceph_connection *con)
3206{
Sage Weile00de342011-03-04 12:25:05 -08003207 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07003208 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08003209 clear_standby(con);
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01003210 ceph_con_flag_set(con, CEPH_CON_F_KEEPALIVE_PENDING);
Sage Weil00650932012-07-20 15:33:04 -07003211 mutex_unlock(&con->mutex);
Ilya Dryomov4aac9222019-01-14 21:13:10 +01003212
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01003213 if (!ceph_con_flag_test_and_set(con, CEPH_CON_F_WRITE_PENDING))
Sage Weil31b80062009-10-06 11:31:13 -07003214 queue_con(con);
3215}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003216EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07003217
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003218bool ceph_con_keepalive_expired(struct ceph_connection *con,
3219 unsigned long interval)
3220{
3221 if (interval > 0 &&
3222 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02003223 struct timespec64 now;
3224 struct timespec64 ts;
3225 ktime_get_real_ts64(&now);
3226 jiffies_to_timespec64(interval, &ts);
3227 ts = timespec64_add(con->last_keepalive_ack, ts);
3228 return timespec64_compare(&now, &ts) >= 0;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003229 }
3230 return false;
3231}
3232
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003233static struct ceph_msg_data *ceph_msg_data_add(struct ceph_msg *msg)
Alex Elder43794502013-03-01 18:00:16 -06003234{
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003235 BUG_ON(msg->num_data_items >= msg->max_data_items);
3236 return &msg->data[msg->num_data_items++];
Alex Elder6644ed72013-03-11 23:34:24 -05003237}
3238
3239static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3240{
Ilya Dryomove8862742020-03-10 16:19:01 +01003241 if (data->type == CEPH_MSG_DATA_PAGES && data->own_pages) {
3242 int num_pages = calc_pages_for(data->alignment, data->length);
3243 ceph_release_page_vector(data->pages, num_pages);
3244 } else if (data->type == CEPH_MSG_DATA_PAGELIST) {
Alex Elder6644ed72013-03-11 23:34:24 -05003245 ceph_pagelist_release(data->pagelist);
Ilya Dryomove8862742020-03-10 16:19:01 +01003246 }
Alex Elder43794502013-03-01 18:00:16 -06003247}
3248
Alex Elder90af3602013-04-05 14:46:01 -05003249void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Ilya Dryomove8862742020-03-10 16:19:01 +01003250 size_t length, size_t alignment, bool own_pages)
Alex Elder02afca62013-02-14 12:16:43 -06003251{
Alex Elder6644ed72013-03-11 23:34:24 -05003252 struct ceph_msg_data *data;
3253
Alex Elder07aa1552013-03-04 18:29:06 -06003254 BUG_ON(!pages);
3255 BUG_ON(!length);
Alex Elder02afca62013-02-14 12:16:43 -06003256
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003257 data = ceph_msg_data_add(msg);
3258 data->type = CEPH_MSG_DATA_PAGES;
Alex Elder6644ed72013-03-11 23:34:24 -05003259 data->pages = pages;
3260 data->length = length;
3261 data->alignment = alignment & ~PAGE_MASK;
Ilya Dryomove8862742020-03-10 16:19:01 +01003262 data->own_pages = own_pages;
Alex Elder6644ed72013-03-11 23:34:24 -05003263
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 */
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01003419int ceph_con_in_msg_alloc(struct ceph_connection *con,
3420 struct ceph_msg_header *hdr, int *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08003421{
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);
Ilya Dryomov6d7f62b2020-11-09 14:59:02 +01003432 if (con->state != CEPH_CON_S_OPEN) {
Alex Elder53ded492013-03-01 18:00:14 -06003433 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 }
Ilya Dryomovfc4c1282020-11-16 17:27:50 +01003453 memcpy(&con->in_msg->hdr, hdr, sizeof(*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
Ilya Dryomov6503e0b2020-11-09 16:29:47 +01003466void ceph_con_get_out_msg(struct ceph_connection *con)
Ilya Dryomov771294f2020-11-18 16:37:14 +01003467{
3468 struct ceph_msg *msg;
3469
3470 BUG_ON(list_empty(&con->out_queue));
3471 msg = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
3472 WARN_ON(msg->con != con);
3473
3474 /*
3475 * Put the message on "sent" list using a ref from ceph_con_send().
3476 * It is put when the message is acked or revoked.
3477 */
3478 list_move_tail(&msg->list_head, &con->out_sent);
3479
3480 /*
3481 * Only assign outgoing seq # if we haven't sent this message
3482 * yet. If it is requeued, resend with it's original seq.
3483 */
3484 if (msg->needs_out_seq) {
3485 msg->hdr.seq = cpu_to_le64(++con->out_seq);
3486 msg->needs_out_seq = false;
3487
3488 if (con->ops->reencode_message)
3489 con->ops->reencode_message(msg);
3490 }
3491
3492 /*
3493 * Get a ref for out_msg. It is put when we are done sending the
3494 * message or in case of a fault.
3495 */
3496 WARN_ON(con->out_msg);
3497 con->out_msg = ceph_msg_get(msg);
3498}
Sage Weil31b80062009-10-06 11:31:13 -07003499
3500/*
3501 * Free a generically kmalloc'd message.
3502 */
Ilya Dryomov0215e442014-06-20 14:14:41 +04003503static void ceph_msg_free(struct ceph_msg *m)
Sage Weil31b80062009-10-06 11:31:13 -07003504{
Ilya Dryomov0215e442014-06-20 14:14:41 +04003505 dout("%s %p\n", __func__, m);
Ilya Dryomov4965fc32014-10-23 16:32:57 +04003506 kvfree(m->front.iov_base);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003507 kfree(m->data);
Alex Eldere3d5d632013-05-01 12:43:04 -05003508 kmem_cache_free(ceph_msg_cache, m);
Sage Weil31b80062009-10-06 11:31:13 -07003509}
3510
Ilya Dryomov0215e442014-06-20 14:14:41 +04003511static void ceph_msg_release(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07003512{
Sage Weilc2e552e2009-12-07 15:55:05 -08003513 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003514 int i;
Sage Weil31b80062009-10-06 11:31:13 -07003515
Ilya Dryomov0215e442014-06-20 14:14:41 +04003516 dout("%s %p\n", __func__, m);
Sage Weilc2e552e2009-12-07 15:55:05 -08003517 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07003518
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003519 msg_con_set(m, NULL);
3520
Sage Weilc2e552e2009-12-07 15:55:05 -08003521 /* drop middle, data, if any */
3522 if (m->middle) {
3523 ceph_buffer_put(m->middle);
3524 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003525 }
Alex Elder5240d9f2013-03-14 14:09:06 -05003526
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003527 for (i = 0; i < m->num_data_items; i++)
3528 ceph_msg_data_destroy(&m->data[i]);
Sage Weil58bb3b32009-12-23 12:12:31 -08003529
Sage Weilc2e552e2009-12-07 15:55:05 -08003530 if (m->pool)
3531 ceph_msgpool_put(m->pool, m);
3532 else
Ilya Dryomov0215e442014-06-20 14:14:41 +04003533 ceph_msg_free(m);
Sage Weil31b80062009-10-06 11:31:13 -07003534}
Ilya Dryomov0215e442014-06-20 14:14:41 +04003535
3536struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3537{
3538 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003539 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003540 kref_get(&msg->kref);
3541 return msg;
3542}
3543EXPORT_SYMBOL(ceph_msg_get);
3544
3545void ceph_msg_put(struct ceph_msg *msg)
3546{
3547 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003548 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003549 kref_put(&msg->kref, ceph_msg_release);
3550}
3551EXPORT_SYMBOL(ceph_msg_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003552
3553void ceph_msg_dump(struct ceph_msg *msg)
3554{
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02003555 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3556 msg->front_alloc_len, msg->data_length);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003557 print_hex_dump(KERN_DEBUG, "header: ",
3558 DUMP_PREFIX_OFFSET, 16, 1,
3559 &msg->hdr, sizeof(msg->hdr), true);
3560 print_hex_dump(KERN_DEBUG, " front: ",
3561 DUMP_PREFIX_OFFSET, 16, 1,
3562 msg->front.iov_base, msg->front.iov_len, true);
3563 if (msg->middle)
3564 print_hex_dump(KERN_DEBUG, "middle: ",
3565 DUMP_PREFIX_OFFSET, 16, 1,
3566 msg->middle->vec.iov_base,
3567 msg->middle->vec.iov_len, true);
3568 print_hex_dump(KERN_DEBUG, "footer: ",
3569 DUMP_PREFIX_OFFSET, 16, 1,
3570 &msg->footer, sizeof(msg->footer), true);
3571}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003572EXPORT_SYMBOL(ceph_msg_dump);