]> git.ipfire.org Git - people/ms/linux.git/blame - net/ceph/messenger.c
libceph: allocate ceph messages with a slab allocator
[people/ms/linux.git] / net / ceph / messenger.c
CommitLineData
3d14c5d2 1#include <linux/ceph/ceph_debug.h>
31b8006e
SW
2
3#include <linux/crc32c.h>
4#include <linux/ctype.h>
5#include <linux/highmem.h>
6#include <linux/inet.h>
7#include <linux/kthread.h>
8#include <linux/net.h>
5a0e3ad6 9#include <linux/slab.h>
31b8006e
SW
10#include <linux/socket.h>
11#include <linux/string.h>
3ebc21f7 12#ifdef CONFIG_BLOCK
68b4476b 13#include <linux/bio.h>
3ebc21f7 14#endif /* CONFIG_BLOCK */
ee3b56f2 15#include <linux/dns_resolver.h>
31b8006e
SW
16#include <net/tcp.h>
17
3d14c5d2
YS
18#include <linux/ceph/libceph.h>
19#include <linux/ceph/messenger.h>
20#include <linux/ceph/decode.h>
21#include <linux/ceph/pagelist.h>
bc3b2d7f 22#include <linux/export.h>
31b8006e 23
fe38a2b6
AE
24#define list_entry_next(pos, member) \
25 list_entry(pos->member.next, typeof(*pos), member)
26
31b8006e
SW
27/*
28 * Ceph uses the messenger to exchange ceph_msg messages with other
29 * hosts in the system. The messenger provides ordered and reliable
30 * delivery. We tolerate TCP disconnects by reconnecting (with
31 * exponential backoff) in the case of a fault (disconnection, bad
32 * crc, protocol error). Acks allow sent messages to be discarded by
33 * the sender.
34 */
35
bc18f4b1
AE
36/*
37 * We track the state of the socket on a given connection using
38 * values defined below. The transition to a new socket state is
39 * handled by a function which verifies we aren't coming from an
40 * unexpected state.
41 *
42 * --------
43 * | NEW* | transient initial state
44 * --------
45 * | con_sock_state_init()
46 * v
47 * ----------
48 * | CLOSED | initialized, but no socket (and no
49 * ---------- TCP connection)
50 * ^ \
51 * | \ con_sock_state_connecting()
52 * | ----------------------
53 * | \
54 * + con_sock_state_closed() \
fbb85a47
SW
55 * |+--------------------------- \
56 * | \ \ \
57 * | ----------- \ \
58 * | | CLOSING | socket event; \ \
59 * | ----------- await close \ \
60 * | ^ \ |
61 * | | \ |
62 * | + con_sock_state_closing() \ |
63 * | / \ | |
64 * | / --------------- | |
65 * | / \ v v
bc18f4b1
AE
66 * | / --------------
67 * | / -----------------| CONNECTING | socket created, TCP
68 * | | / -------------- connect initiated
69 * | | | con_sock_state_connected()
70 * | | v
71 * -------------
72 * | CONNECTED | TCP connection established
73 * -------------
74 *
75 * State values for ceph_connection->sock_state; NEW is assumed to be 0.
76 */
ce2c8903
AE
77
78#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
79#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
80#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
81#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
82#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
83
8dacc7da
SW
84/*
85 * connection states
86 */
87#define CON_STATE_CLOSED 1 /* -> PREOPEN */
88#define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
89#define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
90#define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
91#define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
92#define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
93
4a861692
SW
94/*
95 * ceph_connection flag bits
96 */
97#define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
98 * messages on errors */
99#define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
100#define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
101#define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
102#define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
8dacc7da 103
c9ffc77a
AE
104static bool con_flag_valid(unsigned long con_flag)
105{
106 switch (con_flag) {
107 case CON_FLAG_LOSSYTX:
108 case CON_FLAG_KEEPALIVE_PENDING:
109 case CON_FLAG_WRITE_PENDING:
110 case CON_FLAG_SOCK_CLOSED:
111 case CON_FLAG_BACKOFF:
112 return true;
113 default:
114 return false;
115 }
116}
117
118static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
119{
120 BUG_ON(!con_flag_valid(con_flag));
121
122 clear_bit(con_flag, &con->flags);
123}
124
125static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
126{
127 BUG_ON(!con_flag_valid(con_flag));
128
129 set_bit(con_flag, &con->flags);
130}
131
132static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
133{
134 BUG_ON(!con_flag_valid(con_flag));
135
136 return test_bit(con_flag, &con->flags);
137}
138
139static bool con_flag_test_and_clear(struct ceph_connection *con,
140 unsigned long con_flag)
141{
142 BUG_ON(!con_flag_valid(con_flag));
143
144 return test_and_clear_bit(con_flag, &con->flags);
145}
146
147static bool con_flag_test_and_set(struct ceph_connection *con,
148 unsigned long con_flag)
149{
150 BUG_ON(!con_flag_valid(con_flag));
151
152 return test_and_set_bit(con_flag, &con->flags);
153}
154
e3d5d638
AE
155/* Slab caches for frequently-allocated structures */
156
157static struct kmem_cache *ceph_msg_cache;
158
31b8006e
SW
159/* static tag bytes (protocol control messages) */
160static char tag_msg = CEPH_MSGR_TAG_MSG;
161static char tag_ack = CEPH_MSGR_TAG_ACK;
162static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
163
a6a5349d
SW
164#ifdef CONFIG_LOCKDEP
165static struct lock_class_key socket_class;
166#endif
167
84495f49
AE
168/*
169 * When skipping (ignoring) a block of input we read it into a "skip
170 * buffer," which is this many bytes in size.
171 */
172#define SKIP_BUF_SIZE 1024
31b8006e
SW
173
174static void queue_con(struct ceph_connection *con);
175static void con_work(struct work_struct *);
93209264 176static void con_fault(struct ceph_connection *con);
31b8006e 177
31b8006e 178/*
f64a9317
AE
179 * Nicely render a sockaddr as a string. An array of formatted
180 * strings is used, to approximate reentrancy.
31b8006e 181 */
f64a9317
AE
182#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
183#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
184#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
185#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
186
187static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
188static atomic_t addr_str_seq = ATOMIC_INIT(0);
31b8006e 189
57666519 190static struct page *zero_page; /* used in certain error cases */
57666519 191
3d14c5d2 192const char *ceph_pr_addr(const struct sockaddr_storage *ss)
31b8006e
SW
193{
194 int i;
195 char *s;
99f0f3b2
AE
196 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
197 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
31b8006e 198
f64a9317 199 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
31b8006e
SW
200 s = addr_str[i];
201
202 switch (ss->ss_family) {
203 case AF_INET:
bd406145
AE
204 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
205 ntohs(in4->sin_port));
31b8006e
SW
206 break;
207
208 case AF_INET6:
bd406145
AE
209 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
210 ntohs(in6->sin6_port));
31b8006e
SW
211 break;
212
213 default:
d3002b97
AE
214 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
215 ss->ss_family);
31b8006e
SW
216 }
217
218 return s;
219}
3d14c5d2 220EXPORT_SYMBOL(ceph_pr_addr);
31b8006e 221
63f2d211
SW
222static void encode_my_addr(struct ceph_messenger *msgr)
223{
224 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
225 ceph_encode_addr(&msgr->my_enc_addr);
226}
227
31b8006e
SW
228/*
229 * work queue for all reading and writing to/from the socket.
230 */
e0f43c94 231static struct workqueue_struct *ceph_msgr_wq;
31b8006e 232
e3d5d638
AE
233static int ceph_msgr_slab_init(void)
234{
235 BUG_ON(ceph_msg_cache);
236 ceph_msg_cache = kmem_cache_create("ceph_msg",
237 sizeof (struct ceph_msg),
238 __alignof__(struct ceph_msg), 0, NULL);
239 return ceph_msg_cache ? 0 : -ENOMEM;
240}
241
242static void ceph_msgr_slab_exit(void)
243{
244 BUG_ON(!ceph_msg_cache);
245 kmem_cache_destroy(ceph_msg_cache);
246 ceph_msg_cache = NULL;
247}
248
15417167 249static void _ceph_msgr_exit(void)
6173d1f0 250{
d3002b97 251 if (ceph_msgr_wq) {
6173d1f0 252 destroy_workqueue(ceph_msgr_wq);
d3002b97
AE
253 ceph_msgr_wq = NULL;
254 }
6173d1f0 255
e3d5d638
AE
256 ceph_msgr_slab_exit();
257
6173d1f0
AE
258 BUG_ON(zero_page == NULL);
259 kunmap(zero_page);
260 page_cache_release(zero_page);
261 zero_page = NULL;
262}
263
3d14c5d2 264int ceph_msgr_init(void)
31b8006e 265{
57666519
AE
266 BUG_ON(zero_page != NULL);
267 zero_page = ZERO_PAGE(0);
268 page_cache_get(zero_page);
269
e3d5d638
AE
270 if (ceph_msgr_slab_init())
271 return -ENOMEM;
272
f363e45f 273 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
6173d1f0
AE
274 if (ceph_msgr_wq)
275 return 0;
57666519 276
6173d1f0
AE
277 pr_err("msgr_init failed to create workqueue\n");
278 _ceph_msgr_exit();
57666519 279
6173d1f0 280 return -ENOMEM;
31b8006e 281}
3d14c5d2 282EXPORT_SYMBOL(ceph_msgr_init);
31b8006e
SW
283
284void ceph_msgr_exit(void)
285{
57666519 286 BUG_ON(ceph_msgr_wq == NULL);
57666519 287
6173d1f0 288 _ceph_msgr_exit();
31b8006e 289}
3d14c5d2 290EXPORT_SYMBOL(ceph_msgr_exit);
31b8006e 291
cd84db6e 292void ceph_msgr_flush(void)
a922d38f
SW
293{
294 flush_workqueue(ceph_msgr_wq);
295}
3d14c5d2 296EXPORT_SYMBOL(ceph_msgr_flush);
a922d38f 297
ce2c8903
AE
298/* Connection socket state transition functions */
299
300static void con_sock_state_init(struct ceph_connection *con)
301{
302 int old_state;
303
304 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
305 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
306 printk("%s: unexpected old state %d\n", __func__, old_state);
8007b8d6
SW
307 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
308 CON_SOCK_STATE_CLOSED);
ce2c8903
AE
309}
310
311static void con_sock_state_connecting(struct ceph_connection *con)
312{
313 int old_state;
314
315 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
316 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
317 printk("%s: unexpected old state %d\n", __func__, old_state);
8007b8d6
SW
318 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
319 CON_SOCK_STATE_CONNECTING);
ce2c8903
AE
320}
321
322static void con_sock_state_connected(struct ceph_connection *con)
323{
324 int old_state;
325
326 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
327 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
328 printk("%s: unexpected old state %d\n", __func__, old_state);
8007b8d6
SW
329 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
330 CON_SOCK_STATE_CONNECTED);
ce2c8903
AE
331}
332
333static void con_sock_state_closing(struct ceph_connection *con)
334{
335 int old_state;
336
337 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
338 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
339 old_state != CON_SOCK_STATE_CONNECTED &&
340 old_state != CON_SOCK_STATE_CLOSING))
341 printk("%s: unexpected old state %d\n", __func__, old_state);
8007b8d6
SW
342 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
343 CON_SOCK_STATE_CLOSING);
ce2c8903
AE
344}
345
346static void con_sock_state_closed(struct ceph_connection *con)
347{
348 int old_state;
349
350 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
351 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
fbb85a47 352 old_state != CON_SOCK_STATE_CLOSING &&
8007b8d6
SW
353 old_state != CON_SOCK_STATE_CONNECTING &&
354 old_state != CON_SOCK_STATE_CLOSED))
ce2c8903 355 printk("%s: unexpected old state %d\n", __func__, old_state);
8007b8d6
SW
356 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
357 CON_SOCK_STATE_CLOSED);
ce2c8903 358}
a922d38f 359
31b8006e
SW
360/*
361 * socket callback functions
362 */
363
364/* data available on socket, or listen socket received a connect */
327800bd 365static void ceph_sock_data_ready(struct sock *sk, int count_unused)
31b8006e 366{
bd406145 367 struct ceph_connection *con = sk->sk_user_data;
a2a32584
GH
368 if (atomic_read(&con->msgr->stopping)) {
369 return;
370 }
bd406145 371
31b8006e 372 if (sk->sk_state != TCP_CLOSE_WAIT) {
327800bd 373 dout("%s on %p state = %lu, queueing work\n", __func__,
31b8006e
SW
374 con, con->state);
375 queue_con(con);
376 }
377}
378
379/* socket has buffer space for writing */
327800bd 380static void ceph_sock_write_space(struct sock *sk)
31b8006e 381{
d3002b97 382 struct ceph_connection *con = sk->sk_user_data;
31b8006e 383
182fac26
JS
384 /* only queue to workqueue if there is data we want to write,
385 * and there is sufficient space in the socket buffer to accept
327800bd 386 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
182fac26
JS
387 * doesn't get called again until try_write() fills the socket
388 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
389 * and net/core/stream.c:sk_stream_write_space().
390 */
c9ffc77a 391 if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
182fac26 392 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
327800bd 393 dout("%s %p queueing write work\n", __func__, con);
182fac26
JS
394 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
395 queue_con(con);
396 }
31b8006e 397 } else {
327800bd 398 dout("%s %p nothing to write\n", __func__, con);
31b8006e 399 }
31b8006e
SW
400}
401
402/* socket's state has changed */
327800bd 403static void ceph_sock_state_change(struct sock *sk)
31b8006e 404{
bd406145 405 struct ceph_connection *con = sk->sk_user_data;
31b8006e 406
327800bd 407 dout("%s %p state = %lu sk_state = %u\n", __func__,
31b8006e
SW
408 con, con->state, sk->sk_state);
409
31b8006e
SW
410 switch (sk->sk_state) {
411 case TCP_CLOSE:
327800bd 412 dout("%s TCP_CLOSE\n", __func__);
31b8006e 413 case TCP_CLOSE_WAIT:
327800bd 414 dout("%s TCP_CLOSE_WAIT\n", __func__);
ce2c8903 415 con_sock_state_closing(con);
c9ffc77a 416 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
d65c9e0b 417 queue_con(con);
31b8006e
SW
418 break;
419 case TCP_ESTABLISHED:
327800bd 420 dout("%s TCP_ESTABLISHED\n", __func__);
ce2c8903 421 con_sock_state_connected(con);
31b8006e
SW
422 queue_con(con);
423 break;
d3002b97
AE
424 default: /* Everything else is uninteresting */
425 break;
31b8006e
SW
426 }
427}
428
429/*
430 * set up socket callbacks
431 */
432static void set_sock_callbacks(struct socket *sock,
433 struct ceph_connection *con)
434{
435 struct sock *sk = sock->sk;
bd406145 436 sk->sk_user_data = con;
327800bd
AE
437 sk->sk_data_ready = ceph_sock_data_ready;
438 sk->sk_write_space = ceph_sock_write_space;
439 sk->sk_state_change = ceph_sock_state_change;
31b8006e
SW
440}
441
442
443/*
444 * socket helpers
445 */
446
447/*
448 * initiate connection to a remote socket.
449 */
41617d0c 450static int ceph_tcp_connect(struct ceph_connection *con)
31b8006e 451{
f91d3471 452 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
31b8006e
SW
453 struct socket *sock;
454 int ret;
455
456 BUG_ON(con->sock);
f91d3471
SW
457 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
458 IPPROTO_TCP, &sock);
31b8006e 459 if (ret)
41617d0c 460 return ret;
31b8006e
SW
461 sock->sk->sk_allocation = GFP_NOFS;
462
a6a5349d
SW
463#ifdef CONFIG_LOCKDEP
464 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
465#endif
466
31b8006e
SW
467 set_sock_callbacks(sock, con);
468
3d14c5d2 469 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
31b8006e 470
89a86be0 471 con_sock_state_connecting(con);
f91d3471
SW
472 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
473 O_NONBLOCK);
31b8006e
SW
474 if (ret == -EINPROGRESS) {
475 dout("connect %s EINPROGRESS sk_state = %u\n",
3d14c5d2 476 ceph_pr_addr(&con->peer_addr.in_addr),
31b8006e 477 sock->sk->sk_state);
a5bc3129 478 } else if (ret < 0) {
31b8006e 479 pr_err("connect %s error %d\n",
3d14c5d2 480 ceph_pr_addr(&con->peer_addr.in_addr), ret);
31b8006e 481 sock_release(sock);
31b8006e 482 con->error_msg = "connect error";
31b8006e 483
41617d0c 484 return ret;
a5bc3129
AE
485 }
486 con->sock = sock;
41617d0c 487 return 0;
31b8006e
SW
488}
489
490static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
491{
492 struct kvec iov = {buf, len};
493 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
98bdb0aa 494 int r;
31b8006e 495
98bdb0aa
SW
496 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
497 if (r == -EAGAIN)
498 r = 0;
499 return r;
31b8006e
SW
500}
501
afb3d90e
AE
502static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
503 int page_offset, size_t length)
504{
505 void *kaddr;
506 int ret;
507
508 BUG_ON(page_offset + length > PAGE_SIZE);
509
510 kaddr = kmap(page);
511 BUG_ON(!kaddr);
512 ret = ceph_tcp_recvmsg(sock, kaddr + page_offset, length);
513 kunmap(page);
514
515 return ret;
516}
517
31b8006e
SW
518/*
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,
523 size_t kvlen, size_t len, int more)
524{
525 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
42961d23 526 int r;
31b8006e
SW
527
528 if (more)
529 msg.msg_flags |= MSG_MORE;
530 else
531 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
532
42961d23
SW
533 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
534 if (r == -EAGAIN)
535 r = 0;
536 return r;
31b8006e
SW
537}
538
31739139 539static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
e1dcb128 540 int offset, size_t size, bool more)
31739139
AE
541{
542 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
543 int ret;
544
545 ret = kernel_sendpage(sock, page, offset, size, flags);
546 if (ret == -EAGAIN)
547 ret = 0;
548
549 return ret;
550}
551
31b8006e
SW
552
553/*
554 * Shutdown/close the socket for the given connection.
555 */
556static int con_close_socket(struct ceph_connection *con)
557{
8007b8d6 558 int rc = 0;
31b8006e
SW
559
560 dout("con_close_socket on %p sock %p\n", con, con->sock);
8007b8d6
SW
561 if (con->sock) {
562 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
563 sock_release(con->sock);
564 con->sock = NULL;
565 }
456ea468
AE
566
567 /*
4a861692 568 * Forcibly clear the SOCK_CLOSED flag. It gets set
456ea468
AE
569 * independent of the connection mutex, and we could have
570 * received a socket close event before we had the chance to
571 * shut the socket down.
572 */
c9ffc77a 573 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
8007b8d6 574
ce2c8903 575 con_sock_state_closed(con);
31b8006e
SW
576 return rc;
577}
578
579/*
580 * Reset a connection. Discard all incoming and outgoing messages
581 * and clear *_seq state.
582 */
583static void ceph_msg_remove(struct ceph_msg *msg)
584{
585 list_del_init(&msg->list_head);
38941f80 586 BUG_ON(msg->con == NULL);
36eb71aa 587 msg->con->ops->put(msg->con);
38941f80
AE
588 msg->con = NULL;
589
31b8006e
SW
590 ceph_msg_put(msg);
591}
592static void ceph_msg_remove_list(struct list_head *head)
593{
594 while (!list_empty(head)) {
595 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
596 list_head);
597 ceph_msg_remove(msg);
598 }
599}
600
601static void reset_connection(struct ceph_connection *con)
602{
603 /* reset connection, out_queue, msg_ and connect_seq */
604 /* discard existing out_queue and msg_seq */
0fa6ebc6 605 dout("reset_connection %p\n", con);
31b8006e
SW
606 ceph_msg_remove_list(&con->out_queue);
607 ceph_msg_remove_list(&con->out_sent);
608
cf3e5c40 609 if (con->in_msg) {
38941f80
AE
610 BUG_ON(con->in_msg->con != con);
611 con->in_msg->con = NULL;
cf3e5c40
SW
612 ceph_msg_put(con->in_msg);
613 con->in_msg = NULL;
36eb71aa 614 con->ops->put(con);
cf3e5c40
SW
615 }
616
31b8006e
SW
617 con->connect_seq = 0;
618 con->out_seq = 0;
c86a2930
SW
619 if (con->out_msg) {
620 ceph_msg_put(con->out_msg);
621 con->out_msg = NULL;
622 }
31b8006e 623 con->in_seq = 0;
0e0d5e0c 624 con->in_seq_acked = 0;
31b8006e
SW
625}
626
627/*
628 * mark a peer down. drop any open connections.
629 */
630void ceph_con_close(struct ceph_connection *con)
631{
8c50c817 632 mutex_lock(&con->mutex);
3d14c5d2
YS
633 dout("con_close %p peer %s\n", con,
634 ceph_pr_addr(&con->peer_addr.in_addr));
8dacc7da 635 con->state = CON_STATE_CLOSED;
a5988c49 636
c9ffc77a
AE
637 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
638 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
639 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
640 con_flag_clear(con, CON_FLAG_BACKOFF);
a5988c49 641
31b8006e 642 reset_connection(con);
6f2bc3ff 643 con->peer_global_seq = 0;
91e45ce3 644 cancel_delayed_work(&con->work);
ee76e073 645 con_close_socket(con);
ec302645 646 mutex_unlock(&con->mutex);
31b8006e 647}
3d14c5d2 648EXPORT_SYMBOL(ceph_con_close);
31b8006e 649
31b8006e
SW
650/*
651 * Reopen a closed connection, with a new peer address.
652 */
b7a9e5dd
SW
653void ceph_con_open(struct ceph_connection *con,
654 __u8 entity_type, __u64 entity_num,
655 struct ceph_entity_addr *addr)
31b8006e 656{
5469155f 657 mutex_lock(&con->mutex);
3d14c5d2 658 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
8dacc7da 659
122070a2 660 WARN_ON(con->state != CON_STATE_CLOSED);
8dacc7da 661 con->state = CON_STATE_PREOPEN;
a5988c49 662
b7a9e5dd
SW
663 con->peer_name.type = (__u8) entity_type;
664 con->peer_name.num = cpu_to_le64(entity_num);
665
31b8006e 666 memcpy(&con->peer_addr, addr, sizeof(*addr));
03c677e1 667 con->delay = 0; /* reset backoff memory */
5469155f 668 mutex_unlock(&con->mutex);
31b8006e
SW
669 queue_con(con);
670}
3d14c5d2 671EXPORT_SYMBOL(ceph_con_open);
31b8006e 672
87b315a5
SW
673/*
674 * return true if this connection ever successfully opened
675 */
676bool ceph_con_opened(struct ceph_connection *con)
677{
678 return con->connect_seq > 0;
679}
680
31b8006e
SW
681/*
682 * initialize a new connection.
683 */
1bfd89f4
AE
684void ceph_con_init(struct ceph_connection *con, void *private,
685 const struct ceph_connection_operations *ops,
b7a9e5dd 686 struct ceph_messenger *msgr)
31b8006e
SW
687{
688 dout("con_init %p\n", con);
689 memset(con, 0, sizeof(*con));
1bfd89f4
AE
690 con->private = private;
691 con->ops = ops;
31b8006e 692 con->msgr = msgr;
ce2c8903
AE
693
694 con_sock_state_init(con);
695
ec302645 696 mutex_init(&con->mutex);
31b8006e
SW
697 INIT_LIST_HEAD(&con->out_queue);
698 INIT_LIST_HEAD(&con->out_sent);
699 INIT_DELAYED_WORK(&con->work, con_work);
a5988c49 700
8dacc7da 701 con->state = CON_STATE_CLOSED;
31b8006e 702}
3d14c5d2 703EXPORT_SYMBOL(ceph_con_init);
31b8006e
SW
704
705
706/*
707 * We maintain a global counter to order connection attempts. Get
708 * a unique seq greater than @gt.
709 */
710static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
711{
712 u32 ret;
713
714 spin_lock(&msgr->global_seq_lock);
715 if (msgr->global_seq < gt)
716 msgr->global_seq = gt;
717 ret = ++msgr->global_seq;
718 spin_unlock(&msgr->global_seq_lock);
719 return ret;
720}
721
e2200423 722static void con_out_kvec_reset(struct ceph_connection *con)
859eb799
AE
723{
724 con->out_kvec_left = 0;
725 con->out_kvec_bytes = 0;
726 con->out_kvec_cur = &con->out_kvec[0];
727}
728
e2200423 729static void con_out_kvec_add(struct ceph_connection *con,
859eb799
AE
730 size_t size, void *data)
731{
732 int index;
733
734 index = con->out_kvec_left;
735 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
736
737 con->out_kvec[index].iov_len = size;
738 con->out_kvec[index].iov_base = data;
739 con->out_kvec_left++;
740 con->out_kvec_bytes += size;
741}
31b8006e 742
df6ad1f9 743#ifdef CONFIG_BLOCK
6aaa4511
AE
744
745/*
746 * For a bio data item, a piece is whatever remains of the next
747 * entry in the current bio iovec, or the first entry in the next
748 * bio in the list.
749 */
8ae4f4f5 750static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
25aff7c5 751 size_t length)
6aaa4511 752{
8ae4f4f5 753 struct ceph_msg_data *data = cursor->data;
6aaa4511
AE
754 struct bio *bio;
755
756 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
757
758 bio = data->bio;
759 BUG_ON(!bio);
760 BUG_ON(!bio->bi_vcnt);
6aaa4511 761
ca8b3a69 762 cursor->resid = min(length, data->bio_length);
6aaa4511
AE
763 cursor->bio = bio;
764 cursor->vector_index = 0;
765 cursor->vector_offset = 0;
25aff7c5 766 cursor->last_piece = length <= bio->bi_io_vec[0].bv_len;
6aaa4511
AE
767}
768
8ae4f4f5 769static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
6aaa4511
AE
770 size_t *page_offset,
771 size_t *length)
772{
8ae4f4f5 773 struct ceph_msg_data *data = cursor->data;
6aaa4511
AE
774 struct bio *bio;
775 struct bio_vec *bio_vec;
776 unsigned int index;
777
778 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
779
780 bio = cursor->bio;
781 BUG_ON(!bio);
782
783 index = cursor->vector_index;
784 BUG_ON(index >= (unsigned int) bio->bi_vcnt);
785
786 bio_vec = &bio->bi_io_vec[index];
787 BUG_ON(cursor->vector_offset >= bio_vec->bv_len);
788 *page_offset = (size_t) (bio_vec->bv_offset + cursor->vector_offset);
789 BUG_ON(*page_offset >= PAGE_SIZE);
25aff7c5
AE
790 if (cursor->last_piece) /* pagelist offset is always 0 */
791 *length = cursor->resid;
792 else
793 *length = (size_t) (bio_vec->bv_len - cursor->vector_offset);
25aff7c5 794 BUG_ON(*length > cursor->resid);
5df521b1 795 BUG_ON(*page_offset + *length > PAGE_SIZE);
6aaa4511
AE
796
797 return bio_vec->bv_page;
798}
799
8ae4f4f5
AE
800static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
801 size_t bytes)
6aaa4511 802{
6aaa4511
AE
803 struct bio *bio;
804 struct bio_vec *bio_vec;
805 unsigned int index;
806
8ae4f4f5 807 BUG_ON(cursor->data->type != CEPH_MSG_DATA_BIO);
6aaa4511
AE
808
809 bio = cursor->bio;
810 BUG_ON(!bio);
811
812 index = cursor->vector_index;
813 BUG_ON(index >= (unsigned int) bio->bi_vcnt);
814 bio_vec = &bio->bi_io_vec[index];
6aaa4511
AE
815
816 /* Advance the cursor offset */
817
25aff7c5
AE
818 BUG_ON(cursor->resid < bytes);
819 cursor->resid -= bytes;
6aaa4511
AE
820 cursor->vector_offset += bytes;
821 if (cursor->vector_offset < bio_vec->bv_len)
822 return false; /* more bytes to process in this segment */
25aff7c5 823 BUG_ON(cursor->vector_offset != bio_vec->bv_len);
6aaa4511
AE
824
825 /* Move on to the next segment, and possibly the next bio */
826
25aff7c5 827 if (++index == (unsigned int) bio->bi_vcnt) {
6aaa4511 828 bio = bio->bi_next;
25aff7c5 829 index = 0;
6aaa4511 830 }
25aff7c5
AE
831 cursor->bio = bio;
832 cursor->vector_index = index;
6aaa4511
AE
833 cursor->vector_offset = 0;
834
25aff7c5
AE
835 if (!cursor->last_piece) {
836 BUG_ON(!cursor->resid);
837 BUG_ON(!bio);
838 /* A short read is OK, so use <= rather than == */
839 if (cursor->resid <= bio->bi_io_vec[index].bv_len)
6aaa4511 840 cursor->last_piece = true;
25aff7c5 841 }
6aaa4511
AE
842
843 return true;
844}
ea96571f 845#endif /* CONFIG_BLOCK */
df6ad1f9 846
e766d7b5
AE
847/*
848 * For a page array, a piece comes from the first page in the array
849 * that has not already been fully consumed.
850 */
8ae4f4f5 851static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
25aff7c5 852 size_t length)
e766d7b5 853{
8ae4f4f5 854 struct ceph_msg_data *data = cursor->data;
e766d7b5
AE
855 int page_count;
856
857 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
858
859 BUG_ON(!data->pages);
860 BUG_ON(!data->length);
861
ca8b3a69 862 cursor->resid = min(length, data->length);
e766d7b5 863 page_count = calc_pages_for(data->alignment, (u64)data->length);
e766d7b5
AE
864 cursor->page_offset = data->alignment & ~PAGE_MASK;
865 cursor->page_index = 0;
56fc5659
AE
866 BUG_ON(page_count > (int)USHRT_MAX);
867 cursor->page_count = (unsigned short)page_count;
868 BUG_ON(length > SIZE_MAX - cursor->page_offset);
869 cursor->last_piece = (size_t)cursor->page_offset + length <= PAGE_SIZE;
e766d7b5
AE
870}
871
8ae4f4f5
AE
872static struct page *
873ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
874 size_t *page_offset, size_t *length)
e766d7b5 875{
8ae4f4f5 876 struct ceph_msg_data *data = cursor->data;
e766d7b5
AE
877
878 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
879
880 BUG_ON(cursor->page_index >= cursor->page_count);
881 BUG_ON(cursor->page_offset >= PAGE_SIZE);
e766d7b5
AE
882
883 *page_offset = cursor->page_offset;
25aff7c5 884 if (cursor->last_piece)
e766d7b5 885 *length = cursor->resid;
25aff7c5 886 else
e766d7b5 887 *length = PAGE_SIZE - *page_offset;
e766d7b5
AE
888
889 return data->pages[cursor->page_index];
890}
891
8ae4f4f5 892static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
e766d7b5
AE
893 size_t bytes)
894{
8ae4f4f5 895 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
e766d7b5
AE
896
897 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
e766d7b5
AE
898
899 /* Advance the cursor page offset */
900
901 cursor->resid -= bytes;
5df521b1
AE
902 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
903 if (!bytes || cursor->page_offset)
e766d7b5
AE
904 return false; /* more bytes to process in the current page */
905
5df521b1 906 /* Move on to the next page; offset is already at 0 */
e766d7b5
AE
907
908 BUG_ON(cursor->page_index >= cursor->page_count);
e766d7b5 909 cursor->page_index++;
25aff7c5 910 cursor->last_piece = cursor->resid <= PAGE_SIZE;
e766d7b5
AE
911
912 return true;
913}
914
fe38a2b6 915/*
dd236fcb
AE
916 * For a pagelist, a piece is whatever remains to be consumed in the
917 * first page in the list, or the front of the next page.
fe38a2b6 918 */
8ae4f4f5
AE
919static void
920ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
25aff7c5 921 size_t length)
fe38a2b6 922{
8ae4f4f5 923 struct ceph_msg_data *data = cursor->data;
fe38a2b6
AE
924 struct ceph_pagelist *pagelist;
925 struct page *page;
926
dd236fcb 927 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
fe38a2b6
AE
928
929 pagelist = data->pagelist;
930 BUG_ON(!pagelist);
25aff7c5
AE
931
932 if (!length)
fe38a2b6
AE
933 return; /* pagelist can be assigned but empty */
934
935 BUG_ON(list_empty(&pagelist->head));
936 page = list_first_entry(&pagelist->head, struct page, lru);
937
ca8b3a69 938 cursor->resid = min(length, pagelist->length);
fe38a2b6
AE
939 cursor->page = page;
940 cursor->offset = 0;
a51b272e 941 cursor->last_piece = cursor->resid <= PAGE_SIZE;
fe38a2b6
AE
942}
943
8ae4f4f5
AE
944static struct page *
945ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
946 size_t *page_offset, size_t *length)
fe38a2b6 947{
8ae4f4f5 948 struct ceph_msg_data *data = cursor->data;
fe38a2b6 949 struct ceph_pagelist *pagelist;
fe38a2b6
AE
950
951 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
952
953 pagelist = data->pagelist;
954 BUG_ON(!pagelist);
955
956 BUG_ON(!cursor->page);
25aff7c5 957 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
fe38a2b6 958
5df521b1 959 /* offset of first page in pagelist is always 0 */
fe38a2b6 960 *page_offset = cursor->offset & ~PAGE_MASK;
5df521b1 961 if (cursor->last_piece)
25aff7c5
AE
962 *length = cursor->resid;
963 else
964 *length = PAGE_SIZE - *page_offset;
fe38a2b6 965
8ae4f4f5 966 return cursor->page;
fe38a2b6
AE
967}
968
8ae4f4f5 969static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
dd236fcb 970 size_t bytes)
fe38a2b6 971{
8ae4f4f5 972 struct ceph_msg_data *data = cursor->data;
fe38a2b6
AE
973 struct ceph_pagelist *pagelist;
974
975 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
976
977 pagelist = data->pagelist;
978 BUG_ON(!pagelist);
25aff7c5
AE
979
980 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
fe38a2b6
AE
981 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
982
983 /* Advance the cursor offset */
984
25aff7c5 985 cursor->resid -= bytes;
fe38a2b6 986 cursor->offset += bytes;
5df521b1 987 /* offset of first page in pagelist is always 0 */
fe38a2b6
AE
988 if (!bytes || cursor->offset & ~PAGE_MASK)
989 return false; /* more bytes to process in the current page */
990
991 /* Move on to the next page */
992
993 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
994 cursor->page = list_entry_next(cursor->page, lru);
25aff7c5 995 cursor->last_piece = cursor->resid <= PAGE_SIZE;
fe38a2b6
AE
996
997 return true;
998}
999
dd236fcb
AE
1000/*
1001 * Message data is handled (sent or received) in pieces, where each
1002 * piece resides on a single page. The network layer might not
1003 * consume an entire piece at once. A data item's cursor keeps
1004 * track of which piece is next to process and how much remains to
1005 * be processed in that piece. It also tracks whether the current
1006 * piece is the last one in the data item.
1007 */
ca8b3a69 1008static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
dd236fcb 1009{
ca8b3a69 1010 size_t length = cursor->total_resid;
8ae4f4f5 1011
8ae4f4f5 1012 switch (cursor->data->type) {
dd236fcb 1013 case CEPH_MSG_DATA_PAGELIST:
8ae4f4f5 1014 ceph_msg_data_pagelist_cursor_init(cursor, length);
dd236fcb 1015 break;
e766d7b5 1016 case CEPH_MSG_DATA_PAGES:
8ae4f4f5 1017 ceph_msg_data_pages_cursor_init(cursor, length);
e766d7b5 1018 break;
dd236fcb
AE
1019#ifdef CONFIG_BLOCK
1020 case CEPH_MSG_DATA_BIO:
8ae4f4f5 1021 ceph_msg_data_bio_cursor_init(cursor, length);
6aaa4511 1022 break;
dd236fcb 1023#endif /* CONFIG_BLOCK */
6aaa4511 1024 case CEPH_MSG_DATA_NONE:
dd236fcb
AE
1025 default:
1026 /* BUG(); */
1027 break;
1028 }
8ae4f4f5 1029 cursor->need_crc = true;
dd236fcb
AE
1030}
1031
ca8b3a69
AE
1032static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1033{
1034 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1035 struct ceph_msg_data *data;
1036
1037 BUG_ON(!length);
1038 BUG_ON(length > msg->data_length);
1039 BUG_ON(list_empty(&msg->data));
1040
ca8b3a69
AE
1041 cursor->data_head = &msg->data;
1042 cursor->total_resid = length;
1043 data = list_first_entry(&msg->data, struct ceph_msg_data, links);
1044 cursor->data = data;
1045
1046 __ceph_msg_data_cursor_init(cursor);
1047}
1048
dd236fcb
AE
1049/*
1050 * Return the page containing the next piece to process for a given
1051 * data item, and supply the page offset and length of that piece.
1052 * Indicate whether this is the last piece in this data item.
1053 */
8ae4f4f5
AE
1054static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1055 size_t *page_offset, size_t *length,
dd236fcb
AE
1056 bool *last_piece)
1057{
1058 struct page *page;
1059
8ae4f4f5 1060 switch (cursor->data->type) {
dd236fcb 1061 case CEPH_MSG_DATA_PAGELIST:
8ae4f4f5 1062 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
dd236fcb 1063 break;
e766d7b5 1064 case CEPH_MSG_DATA_PAGES:
8ae4f4f5 1065 page = ceph_msg_data_pages_next(cursor, page_offset, length);
e766d7b5 1066 break;
dd236fcb
AE
1067#ifdef CONFIG_BLOCK
1068 case CEPH_MSG_DATA_BIO:
8ae4f4f5 1069 page = ceph_msg_data_bio_next(cursor, page_offset, length);
6aaa4511 1070 break;
dd236fcb 1071#endif /* CONFIG_BLOCK */
6aaa4511 1072 case CEPH_MSG_DATA_NONE:
dd236fcb
AE
1073 default:
1074 page = NULL;
1075 break;
1076 }
1077 BUG_ON(!page);
1078 BUG_ON(*page_offset + *length > PAGE_SIZE);
1079 BUG_ON(!*length);
1080 if (last_piece)
8ae4f4f5 1081 *last_piece = cursor->last_piece;
dd236fcb
AE
1082
1083 return page;
1084}
1085
1086/*
1087 * Returns true if the result moves the cursor on to the next piece
1088 * of the data item.
1089 */
8ae4f4f5
AE
1090static bool ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1091 size_t bytes)
dd236fcb
AE
1092{
1093 bool new_piece;
1094
25aff7c5 1095 BUG_ON(bytes > cursor->resid);
8ae4f4f5 1096 switch (cursor->data->type) {
dd236fcb 1097 case CEPH_MSG_DATA_PAGELIST:
8ae4f4f5 1098 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
dd236fcb 1099 break;
e766d7b5 1100 case CEPH_MSG_DATA_PAGES:
8ae4f4f5 1101 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
e766d7b5 1102 break;
dd236fcb
AE
1103#ifdef CONFIG_BLOCK
1104 case CEPH_MSG_DATA_BIO:
8ae4f4f5 1105 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
6aaa4511 1106 break;
dd236fcb 1107#endif /* CONFIG_BLOCK */
6aaa4511 1108 case CEPH_MSG_DATA_NONE:
dd236fcb
AE
1109 default:
1110 BUG();
1111 break;
1112 }
ca8b3a69 1113 cursor->total_resid -= bytes;
dd236fcb 1114
ca8b3a69
AE
1115 if (!cursor->resid && cursor->total_resid) {
1116 WARN_ON(!cursor->last_piece);
1117 BUG_ON(list_is_last(&cursor->data->links, cursor->data_head));
1118 cursor->data = list_entry_next(cursor->data, links);
1119 __ceph_msg_data_cursor_init(cursor);
a51b272e 1120 new_piece = true;
ca8b3a69 1121 }
a51b272e 1122 cursor->need_crc = new_piece;
ca8b3a69 1123
dd236fcb
AE
1124 return new_piece;
1125}
1126
98fa5dd8 1127static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
739c905b 1128{
739c905b 1129 BUG_ON(!msg);
25aff7c5 1130 BUG_ON(!data_len);
739c905b 1131
4c59b4a2 1132 /* Initialize data cursor */
fe38a2b6 1133
8ae4f4f5 1134 ceph_msg_data_cursor_init(msg, (size_t)data_len);
739c905b
AE
1135}
1136
31b8006e
SW
1137/*
1138 * Prepare footer for currently outgoing message, and finish things
1139 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1140 */
859eb799 1141static void prepare_write_message_footer(struct ceph_connection *con)
31b8006e
SW
1142{
1143 struct ceph_msg *m = con->out_msg;
859eb799 1144 int v = con->out_kvec_left;
31b8006e 1145
fd154f3c
AE
1146 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1147
31b8006e
SW
1148 dout("prepare_write_message_footer %p\n", con);
1149 con->out_kvec_is_msg = true;
1150 con->out_kvec[v].iov_base = &m->footer;
1151 con->out_kvec[v].iov_len = sizeof(m->footer);
1152 con->out_kvec_bytes += sizeof(m->footer);
1153 con->out_kvec_left++;
1154 con->out_more = m->more_to_follow;
c86a2930 1155 con->out_msg_done = true;
31b8006e
SW
1156}
1157
1158/*
1159 * Prepare headers for the next outgoing message.
1160 */
1161static void prepare_write_message(struct ceph_connection *con)
1162{
1163 struct ceph_msg *m;
a9a0c51a 1164 u32 crc;
31b8006e 1165
e2200423 1166 con_out_kvec_reset(con);
31b8006e 1167 con->out_kvec_is_msg = true;
c86a2930 1168 con->out_msg_done = false;
31b8006e
SW
1169
1170 /* Sneak an ack in there first? If we can get it into the same
1171 * TCP packet that's a good thing. */
1172 if (con->in_seq > con->in_seq_acked) {
1173 con->in_seq_acked = con->in_seq;
e2200423 1174 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
31b8006e 1175 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
e2200423 1176 con_out_kvec_add(con, sizeof (con->out_temp_ack),
859eb799 1177 &con->out_temp_ack);
31b8006e
SW
1178 }
1179
38941f80 1180 BUG_ON(list_empty(&con->out_queue));
859eb799 1181 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
c86a2930 1182 con->out_msg = m;
38941f80 1183 BUG_ON(m->con != con);
4cf9d544
SW
1184
1185 /* put message on sent list */
1186 ceph_msg_get(m);
1187 list_move_tail(&m->list_head, &con->out_sent);
31b8006e 1188
e84346b7
SW
1189 /*
1190 * only assign outgoing seq # if we haven't sent this message
1191 * yet. if it is requeued, resend with it's original seq.
1192 */
1193 if (m->needs_out_seq) {
1194 m->hdr.seq = cpu_to_le64(++con->out_seq);
1195 m->needs_out_seq = false;
1196 }
98fa5dd8 1197 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
31b8006e 1198
98fa5dd8 1199 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
31b8006e
SW
1200 m, con->out_seq, le16_to_cpu(m->hdr.type),
1201 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
98fa5dd8 1202 m->data_length);
31b8006e
SW
1203 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
1204
1205 /* tag + hdr + front + middle */
e2200423
AE
1206 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
1207 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
1208 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
859eb799 1209
31b8006e 1210 if (m->middle)
e2200423 1211 con_out_kvec_add(con, m->middle->vec.iov_len,
859eb799 1212 m->middle->vec.iov_base);
31b8006e
SW
1213
1214 /* fill in crc (except data pages), footer */
a9a0c51a
AE
1215 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1216 con->out_msg->hdr.crc = cpu_to_le32(crc);
fd154f3c 1217 con->out_msg->footer.flags = 0;
a9a0c51a
AE
1218
1219 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1220 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1221 if (m->middle) {
1222 crc = crc32c(0, m->middle->vec.iov_base,
1223 m->middle->vec.iov_len);
1224 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1225 } else
31b8006e 1226 con->out_msg->footer.middle_crc = 0;
739c905b 1227 dout("%s front_crc %u middle_crc %u\n", __func__,
31b8006e
SW
1228 le32_to_cpu(con->out_msg->footer.front_crc),
1229 le32_to_cpu(con->out_msg->footer.middle_crc));
1230
1231 /* is there a data payload? */
739c905b 1232 con->out_msg->footer.data_crc = 0;
98fa5dd8
AE
1233 if (m->data_length) {
1234 prepare_message_data(con->out_msg, m->data_length);
78625051
AE
1235 con->out_more = 1; /* data + footer will follow */
1236 } else {
31b8006e 1237 /* no, queue up footer too and be done */
859eb799 1238 prepare_write_message_footer(con);
78625051 1239 }
31b8006e 1240
c9ffc77a 1241 con_flag_set(con, CON_FLAG_WRITE_PENDING);
31b8006e
SW
1242}
1243
1244/*
1245 * Prepare an ack.
1246 */
1247static void prepare_write_ack(struct ceph_connection *con)
1248{
1249 dout("prepare_write_ack %p %llu -> %llu\n", con,
1250 con->in_seq_acked, con->in_seq);
1251 con->in_seq_acked = con->in_seq;
1252
e2200423 1253 con_out_kvec_reset(con);
859eb799 1254
e2200423 1255 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
859eb799 1256
31b8006e 1257 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
e2200423 1258 con_out_kvec_add(con, sizeof (con->out_temp_ack),
859eb799
AE
1259 &con->out_temp_ack);
1260
31b8006e 1261 con->out_more = 1; /* more will follow.. eventually.. */
c9ffc77a 1262 con_flag_set(con, CON_FLAG_WRITE_PENDING);
31b8006e
SW
1263}
1264
3a23083b
SW
1265/*
1266 * Prepare to share the seq during handshake
1267 */
1268static void prepare_write_seq(struct ceph_connection *con)
1269{
1270 dout("prepare_write_seq %p %llu -> %llu\n", con,
1271 con->in_seq_acked, con->in_seq);
1272 con->in_seq_acked = con->in_seq;
1273
1274 con_out_kvec_reset(con);
1275
1276 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1277 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1278 &con->out_temp_ack);
1279
1280 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1281}
1282
31b8006e
SW
1283/*
1284 * Prepare to write keepalive byte.
1285 */
1286static void prepare_write_keepalive(struct ceph_connection *con)
1287{
1288 dout("prepare_write_keepalive %p\n", con);
e2200423
AE
1289 con_out_kvec_reset(con);
1290 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
c9ffc77a 1291 con_flag_set(con, CON_FLAG_WRITE_PENDING);
31b8006e
SW
1292}
1293
1294/*
1295 * Connection negotiation.
1296 */
1297
dac1e716
AE
1298static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
1299 int *auth_proto)
4e7a5dcd 1300{
a3530df3 1301 struct ceph_auth_handshake *auth;
b1c6b980
AE
1302
1303 if (!con->ops->get_authorizer) {
1304 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1305 con->out_connect.authorizer_len = 0;
729796be 1306 return NULL;
b1c6b980
AE
1307 }
1308
1309 /* Can't hold the mutex while getting authorizer */
ec302645 1310 mutex_unlock(&con->mutex);
dac1e716 1311 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
ec302645 1312 mutex_lock(&con->mutex);
4e7a5dcd 1313
a3530df3 1314 if (IS_ERR(auth))
729796be 1315 return auth;
8dacc7da 1316 if (con->state != CON_STATE_NEGOTIATING)
729796be 1317 return ERR_PTR(-EAGAIN);
0da5d703 1318
8f43fb53
AE
1319 con->auth_reply_buf = auth->authorizer_reply_buf;
1320 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
729796be 1321 return auth;
4e7a5dcd
SW
1322}
1323
31b8006e
SW
1324/*
1325 * We connected to a peer and are saying hello.
1326 */
e825a66d 1327static void prepare_write_banner(struct ceph_connection *con)
31b8006e 1328{
e2200423
AE
1329 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1330 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
e825a66d 1331 &con->msgr->my_enc_addr);
eed0ef2c 1332
eed0ef2c 1333 con->out_more = 0;
c9ffc77a 1334 con_flag_set(con, CON_FLAG_WRITE_PENDING);
eed0ef2c
SW
1335}
1336
e825a66d 1337static int prepare_write_connect(struct ceph_connection *con)
eed0ef2c 1338{
95c96174 1339 unsigned int global_seq = get_global_seq(con->msgr, 0);
31b8006e 1340 int proto;
dac1e716 1341 int auth_proto;
729796be 1342 struct ceph_auth_handshake *auth;
31b8006e
SW
1343
1344 switch (con->peer_name.type) {
1345 case CEPH_ENTITY_TYPE_MON:
1346 proto = CEPH_MONC_PROTOCOL;
1347 break;
1348 case CEPH_ENTITY_TYPE_OSD:
1349 proto = CEPH_OSDC_PROTOCOL;
1350 break;
1351 case CEPH_ENTITY_TYPE_MDS:
1352 proto = CEPH_MDSC_PROTOCOL;
1353 break;
1354 default:
1355 BUG();
1356 }
1357
1358 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1359 con->connect_seq, global_seq, proto);
4e7a5dcd 1360
e825a66d 1361 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
31b8006e
SW
1362 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1363 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1364 con->out_connect.global_seq = cpu_to_le32(global_seq);
1365 con->out_connect.protocol_version = cpu_to_le32(proto);
1366 con->out_connect.flags = 0;
31b8006e 1367
dac1e716
AE
1368 auth_proto = CEPH_AUTH_UNKNOWN;
1369 auth = get_connect_authorizer(con, &auth_proto);
729796be
AE
1370 if (IS_ERR(auth))
1371 return PTR_ERR(auth);
3da54776 1372
dac1e716 1373 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
3da54776
AE
1374 con->out_connect.authorizer_len = auth ?
1375 cpu_to_le32(auth->authorizer_buf_len) : 0;
1376
e2200423 1377 con_out_kvec_add(con, sizeof (con->out_connect),
3da54776
AE
1378 &con->out_connect);
1379 if (auth && auth->authorizer_buf_len)
e2200423 1380 con_out_kvec_add(con, auth->authorizer_buf_len,
3da54776 1381 auth->authorizer_buf);
859eb799 1382
31b8006e 1383 con->out_more = 0;
c9ffc77a 1384 con_flag_set(con, CON_FLAG_WRITE_PENDING);
4e7a5dcd 1385
e10c758e 1386 return 0;
31b8006e
SW
1387}
1388
31b8006e
SW
1389/*
1390 * write as much of pending kvecs to the socket as we can.
1391 * 1 -> done
1392 * 0 -> socket full, but more to do
1393 * <0 -> error
1394 */
1395static int write_partial_kvec(struct ceph_connection *con)
1396{
1397 int ret;
1398
1399 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1400 while (con->out_kvec_bytes > 0) {
1401 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1402 con->out_kvec_left, con->out_kvec_bytes,
1403 con->out_more);
1404 if (ret <= 0)
1405 goto out;
1406 con->out_kvec_bytes -= ret;
1407 if (con->out_kvec_bytes == 0)
1408 break; /* done */
f42299e6
AE
1409
1410 /* account for full iov entries consumed */
1411 while (ret >= con->out_kvec_cur->iov_len) {
1412 BUG_ON(!con->out_kvec_left);
1413 ret -= con->out_kvec_cur->iov_len;
1414 con->out_kvec_cur++;
1415 con->out_kvec_left--;
1416 }
1417 /* and for a partially-consumed entry */
1418 if (ret) {
1419 con->out_kvec_cur->iov_len -= ret;
1420 con->out_kvec_cur->iov_base += ret;
31b8006e
SW
1421 }
1422 }
1423 con->out_kvec_left = 0;
1424 con->out_kvec_is_msg = false;
1425 ret = 1;
1426out:
1427 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1428 con->out_kvec_bytes, con->out_kvec_left, ret);
1429 return ret; /* done! */
1430}
1431
35b62808
AE
1432static u32 ceph_crc32c_page(u32 crc, struct page *page,
1433 unsigned int page_offset,
1434 unsigned int length)
1435{
1436 char *kaddr;
1437
1438 kaddr = kmap(page);
1439 BUG_ON(kaddr == NULL);
1440 crc = crc32c(crc, kaddr + page_offset, length);
1441 kunmap(page);
1442
1443 return crc;
1444}
31b8006e
SW
1445/*
1446 * Write as much message data payload as we can. If we finish, queue
1447 * up the footer.
1448 * 1 -> done, footer is now queued in out_kvec[].
1449 * 0 -> socket full, but more to do
1450 * <0 -> error
1451 */
34d2d200 1452static int write_partial_message_data(struct ceph_connection *con)
31b8006e
SW
1453{
1454 struct ceph_msg *msg = con->out_msg;
8ae4f4f5 1455 struct ceph_msg_data_cursor *cursor = &msg->cursor;
37675b0f 1456 bool do_datacrc = !con->msgr->nocrc;
f5db90bc 1457 u32 crc;
31b8006e 1458
859a35d5 1459 dout("%s %p msg %p\n", __func__, con, msg);
31b8006e 1460
5240d9f9 1461 if (list_empty(&msg->data))
4c59b4a2
AE
1462 return -EINVAL;
1463
5821bd8c
AE
1464 /*
1465 * Iterate through each page that contains data to be
1466 * written, and send as much as possible for each.
1467 *
1468 * If we are calculating the data crc (the default), we will
1469 * need to map the page. If we have no pages, they have
1470 * been revoked, so use the zero page.
1471 */
f5db90bc 1472 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
643c68a4 1473 while (cursor->resid) {
8a166d05 1474 struct page *page;
e387d525
AE
1475 size_t page_offset;
1476 size_t length;
8a166d05 1477 bool last_piece;
8ea299bc 1478 bool need_crc;
f5db90bc 1479 int ret;
68b4476b 1480
8ae4f4f5 1481 page = ceph_msg_data_next(&msg->cursor, &page_offset, &length,
4c59b4a2 1482 &last_piece);
e387d525 1483 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
fe38a2b6 1484 length, last_piece);
f5db90bc
AE
1485 if (ret <= 0) {
1486 if (do_datacrc)
1487 msg->footer.data_crc = cpu_to_le32(crc);
31b8006e 1488
f5db90bc
AE
1489 return ret;
1490 }
143334ff
AE
1491 if (do_datacrc && cursor->need_crc)
1492 crc = ceph_crc32c_page(crc, page, page_offset, length);
8ae4f4f5 1493 need_crc = ceph_msg_data_advance(&msg->cursor, (size_t)ret);
31b8006e
SW
1494 }
1495
34d2d200 1496 dout("%s %p msg %p done\n", __func__, con, msg);
31b8006e
SW
1497
1498 /* prepare and queue up footer, too */
f5db90bc
AE
1499 if (do_datacrc)
1500 msg->footer.data_crc = cpu_to_le32(crc);
1501 else
84ca8fc8 1502 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
e2200423 1503 con_out_kvec_reset(con);
859eb799 1504 prepare_write_message_footer(con);
f5db90bc
AE
1505
1506 return 1; /* must return > 0 to indicate success */
31b8006e
SW
1507}
1508
1509/*
1510 * write some zeros
1511 */
1512static int write_partial_skip(struct ceph_connection *con)
1513{
1514 int ret;
1515
1516 while (con->out_skip > 0) {
31739139 1517 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
31b8006e 1518
e1dcb128 1519 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
31b8006e
SW
1520 if (ret <= 0)
1521 goto out;
1522 con->out_skip -= ret;
1523 }
1524 ret = 1;
1525out:
1526 return ret;
1527}
1528
1529/*
1530 * Prepare to read connection handshake, or an ack.
1531 */
eed0ef2c
SW
1532static void prepare_read_banner(struct ceph_connection *con)
1533{
1534 dout("prepare_read_banner %p\n", con);
1535 con->in_base_pos = 0;
1536}
1537
31b8006e
SW
1538static void prepare_read_connect(struct ceph_connection *con)
1539{
1540 dout("prepare_read_connect %p\n", con);
1541 con->in_base_pos = 0;
1542}
1543
1544static void prepare_read_ack(struct ceph_connection *con)
1545{
1546 dout("prepare_read_ack %p\n", con);
1547 con->in_base_pos = 0;
1548}
1549
3a23083b
SW
1550static void prepare_read_seq(struct ceph_connection *con)
1551{
1552 dout("prepare_read_seq %p\n", con);
1553 con->in_base_pos = 0;
1554 con->in_tag = CEPH_MSGR_TAG_SEQ;
1555}
1556
31b8006e
SW
1557static void prepare_read_tag(struct ceph_connection *con)
1558{
1559 dout("prepare_read_tag %p\n", con);
1560 con->in_base_pos = 0;
1561 con->in_tag = CEPH_MSGR_TAG_READY;
1562}
1563
1564/*
1565 * Prepare to read a message.
1566 */
1567static int prepare_read_message(struct ceph_connection *con)
1568{
1569 dout("prepare_read_message %p\n", con);
1570 BUG_ON(con->in_msg != NULL);
1571 con->in_base_pos = 0;
1572 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1573 return 0;
1574}
1575
1576
1577static int read_partial(struct ceph_connection *con,
fd51653f 1578 int end, int size, void *object)
31b8006e 1579{
e6cee71f
AE
1580 while (con->in_base_pos < end) {
1581 int left = end - con->in_base_pos;
31b8006e
SW
1582 int have = size - left;
1583 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1584 if (ret <= 0)
1585 return ret;
1586 con->in_base_pos += ret;
1587 }
1588 return 1;
1589}
1590
1591
1592/*
1593 * Read all or part of the connect-side handshake on a new connection
1594 */
eed0ef2c 1595static int read_partial_banner(struct ceph_connection *con)
31b8006e 1596{
fd51653f
AE
1597 int size;
1598 int end;
1599 int ret;
31b8006e 1600
eed0ef2c 1601 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
31b8006e
SW
1602
1603 /* peer's banner */
fd51653f
AE
1604 size = strlen(CEPH_BANNER);
1605 end = size;
1606 ret = read_partial(con, end, size, con->in_banner);
31b8006e
SW
1607 if (ret <= 0)
1608 goto out;
fd51653f
AE
1609
1610 size = sizeof (con->actual_peer_addr);
1611 end += size;
1612 ret = read_partial(con, end, size, &con->actual_peer_addr);
31b8006e
SW
1613 if (ret <= 0)
1614 goto out;
fd51653f
AE
1615
1616 size = sizeof (con->peer_addr_for_me);
1617 end += size;
1618 ret = read_partial(con, end, size, &con->peer_addr_for_me);
31b8006e
SW
1619 if (ret <= 0)
1620 goto out;
fd51653f 1621
eed0ef2c
SW
1622out:
1623 return ret;
1624}
1625
1626static int read_partial_connect(struct ceph_connection *con)
1627{
fd51653f
AE
1628 int size;
1629 int end;
1630 int ret;
eed0ef2c
SW
1631
1632 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1633
fd51653f
AE
1634 size = sizeof (con->in_reply);
1635 end = size;
1636 ret = read_partial(con, end, size, &con->in_reply);
31b8006e
SW
1637 if (ret <= 0)
1638 goto out;
fd51653f
AE
1639
1640 size = le32_to_cpu(con->in_reply.authorizer_len);
1641 end += size;
1642 ret = read_partial(con, end, size, con->auth_reply_buf);
4e7a5dcd
SW
1643 if (ret <= 0)
1644 goto out;
31b8006e 1645
4e7a5dcd
SW
1646 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1647 con, (int)con->in_reply.tag,
1648 le32_to_cpu(con->in_reply.connect_seq),
31b8006e
SW
1649 le32_to_cpu(con->in_reply.global_seq));
1650out:
1651 return ret;
eed0ef2c 1652
31b8006e
SW
1653}
1654
1655/*
1656 * Verify the hello banner looks okay.
1657 */
1658static int verify_hello(struct ceph_connection *con)
1659{
1660 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
13e38c8a 1661 pr_err("connect to %s got bad banner\n",
3d14c5d2 1662 ceph_pr_addr(&con->peer_addr.in_addr));
31b8006e
SW
1663 con->error_msg = "protocol error, bad banner";
1664 return -1;
1665 }
1666 return 0;
1667}
1668
1669static bool addr_is_blank(struct sockaddr_storage *ss)
1670{
1671 switch (ss->ss_family) {
1672 case AF_INET:
1673 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1674 case AF_INET6:
1675 return
1676 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1677 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1678 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1679 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1680 }
1681 return false;
1682}
1683
1684static int addr_port(struct sockaddr_storage *ss)
1685{
1686 switch (ss->ss_family) {
1687 case AF_INET:
f28bcfbe 1688 return ntohs(((struct sockaddr_in *)ss)->sin_port);
31b8006e 1689 case AF_INET6:
f28bcfbe 1690 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
31b8006e
SW
1691 }
1692 return 0;
1693}
1694
1695static void addr_set_port(struct sockaddr_storage *ss, int p)
1696{
1697 switch (ss->ss_family) {
1698 case AF_INET:
1699 ((struct sockaddr_in *)ss)->sin_port = htons(p);
a2a79609 1700 break;
31b8006e
SW
1701 case AF_INET6:
1702 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
a2a79609 1703 break;
31b8006e
SW
1704 }
1705}
1706
ee3b56f2
NW
1707/*
1708 * Unlike other *_pton function semantics, zero indicates success.
1709 */
1710static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1711 char delim, const char **ipend)
1712{
99f0f3b2
AE
1713 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1714 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
ee3b56f2
NW
1715
1716 memset(ss, 0, sizeof(*ss));
1717
1718 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1719 ss->ss_family = AF_INET;
1720 return 0;
1721 }
1722
1723 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1724 ss->ss_family = AF_INET6;
1725 return 0;
1726 }
1727
1728 return -EINVAL;
1729}
1730
1731/*
1732 * Extract hostname string and resolve using kernel DNS facility.
1733 */
1734#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1735static int ceph_dns_resolve_name(const char *name, size_t namelen,
1736 struct sockaddr_storage *ss, char delim, const char **ipend)
1737{
1738 const char *end, *delim_p;
1739 char *colon_p, *ip_addr = NULL;
1740 int ip_len, ret;
1741
1742 /*
1743 * The end of the hostname occurs immediately preceding the delimiter or
1744 * the port marker (':') where the delimiter takes precedence.
1745 */
1746 delim_p = memchr(name, delim, namelen);
1747 colon_p = memchr(name, ':', namelen);
1748
1749 if (delim_p && colon_p)
1750 end = delim_p < colon_p ? delim_p : colon_p;
1751 else if (!delim_p && colon_p)
1752 end = colon_p;
1753 else {
1754 end = delim_p;
1755 if (!end) /* case: hostname:/ */
1756 end = name + namelen;
1757 }
1758
1759 if (end <= name)
1760 return -EINVAL;
1761
1762 /* do dns_resolve upcall */
1763 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1764 if (ip_len > 0)
1765 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1766 else
1767 ret = -ESRCH;
1768
1769 kfree(ip_addr);
1770
1771 *ipend = end;
1772
1773 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1774 ret, ret ? "failed" : ceph_pr_addr(ss));
1775
1776 return ret;
1777}
1778#else
1779static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1780 struct sockaddr_storage *ss, char delim, const char **ipend)
1781{
1782 return -EINVAL;
1783}
1784#endif
1785
1786/*
1787 * Parse a server name (IP or hostname). If a valid IP address is not found
1788 * then try to extract a hostname to resolve using userspace DNS upcall.
1789 */
1790static int ceph_parse_server_name(const char *name, size_t namelen,
1791 struct sockaddr_storage *ss, char delim, const char **ipend)
1792{
1793 int ret;
1794
1795 ret = ceph_pton(name, namelen, ss, delim, ipend);
1796 if (ret)
1797 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1798
1799 return ret;
1800}
1801
31b8006e
SW
1802/*
1803 * Parse an ip[:port] list into an addr array. Use the default
1804 * monitor port if a port isn't specified.
1805 */
1806int ceph_parse_ips(const char *c, const char *end,
1807 struct ceph_entity_addr *addr,
1808 int max_count, int *count)
1809{
ee3b56f2 1810 int i, ret = -EINVAL;
31b8006e
SW
1811 const char *p = c;
1812
1813 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1814 for (i = 0; i < max_count; i++) {
1815 const char *ipend;
1816 struct sockaddr_storage *ss = &addr[i].in_addr;
31b8006e 1817 int port;
39139f64
SW
1818 char delim = ',';
1819
1820 if (*p == '[') {
1821 delim = ']';
1822 p++;
1823 }
31b8006e 1824
ee3b56f2
NW
1825 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1826 if (ret)
31b8006e 1827 goto bad;
ee3b56f2
NW
1828 ret = -EINVAL;
1829
31b8006e
SW
1830 p = ipend;
1831
39139f64
SW
1832 if (delim == ']') {
1833 if (*p != ']') {
1834 dout("missing matching ']'\n");
1835 goto bad;
1836 }
1837 p++;
1838 }
1839
31b8006e
SW
1840 /* port? */
1841 if (p < end && *p == ':') {
1842 port = 0;
1843 p++;
1844 while (p < end && *p >= '0' && *p <= '9') {
1845 port = (port * 10) + (*p - '0');
1846 p++;
1847 }
1848 if (port > 65535 || port == 0)
1849 goto bad;
1850 } else {
1851 port = CEPH_MON_PORT;
1852 }
1853
1854 addr_set_port(ss, port);
1855
3d14c5d2 1856 dout("parse_ips got %s\n", ceph_pr_addr(ss));
31b8006e
SW
1857
1858 if (p == end)
1859 break;
1860 if (*p != ',')
1861 goto bad;
1862 p++;
1863 }
1864
1865 if (p != end)
1866 goto bad;
1867
1868 if (count)
1869 *count = i + 1;
1870 return 0;
1871
1872bad:
39139f64 1873 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
ee3b56f2 1874 return ret;
31b8006e 1875}
3d14c5d2 1876EXPORT_SYMBOL(ceph_parse_ips);
31b8006e 1877
eed0ef2c 1878static int process_banner(struct ceph_connection *con)
31b8006e 1879{
eed0ef2c 1880 dout("process_banner on %p\n", con);
31b8006e
SW
1881
1882 if (verify_hello(con) < 0)
1883 return -1;
1884
63f2d211
SW
1885 ceph_decode_addr(&con->actual_peer_addr);
1886 ceph_decode_addr(&con->peer_addr_for_me);
1887
31b8006e
SW
1888 /*
1889 * Make sure the other end is who we wanted. note that the other
1890 * end may not yet know their ip address, so if it's 0.0.0.0, give
1891 * them the benefit of the doubt.
1892 */
103e2d3a
SW
1893 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1894 sizeof(con->peer_addr)) != 0 &&
31b8006e
SW
1895 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1896 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
cd84db6e 1897 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
3d14c5d2 1898 ceph_pr_addr(&con->peer_addr.in_addr),
cd84db6e 1899 (int)le32_to_cpu(con->peer_addr.nonce),
3d14c5d2 1900 ceph_pr_addr(&con->actual_peer_addr.in_addr),
cd84db6e 1901 (int)le32_to_cpu(con->actual_peer_addr.nonce));
58bb3b37 1902 con->error_msg = "wrong peer at address";
31b8006e
SW
1903 return -1;
1904 }
1905
1906 /*
1907 * did we learn our address?
1908 */
1909 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1910 int port = addr_port(&con->msgr->inst.addr.in_addr);
1911
1912 memcpy(&con->msgr->inst.addr.in_addr,
1913 &con->peer_addr_for_me.in_addr,
1914 sizeof(con->peer_addr_for_me.in_addr));
1915 addr_set_port(&con->msgr->inst.addr.in_addr, port);
63f2d211 1916 encode_my_addr(con->msgr);
eed0ef2c 1917 dout("process_banner learned my addr is %s\n",
3d14c5d2 1918 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
31b8006e
SW
1919 }
1920
eed0ef2c
SW
1921 return 0;
1922}
1923
1924static int process_connect(struct ceph_connection *con)
1925{
3d14c5d2
YS
1926 u64 sup_feat = con->msgr->supported_features;
1927 u64 req_feat = con->msgr->required_features;
04a419f9 1928 u64 server_feat = le64_to_cpu(con->in_reply.features);
0da5d703 1929 int ret;
04a419f9 1930
eed0ef2c
SW
1931 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1932
31b8006e 1933 switch (con->in_reply.tag) {
04a419f9
SW
1934 case CEPH_MSGR_TAG_FEATURES:
1935 pr_err("%s%lld %s feature set mismatch,"
1936 " my %llx < server's %llx, missing %llx\n",
1937 ENTITY_NAME(con->peer_name),
3d14c5d2 1938 ceph_pr_addr(&con->peer_addr.in_addr),
04a419f9
SW
1939 sup_feat, server_feat, server_feat & ~sup_feat);
1940 con->error_msg = "missing required protocol features";
0fa6ebc6 1941 reset_connection(con);
04a419f9
SW
1942 return -1;
1943
31b8006e 1944 case CEPH_MSGR_TAG_BADPROTOVER:
31b8006e
SW
1945 pr_err("%s%lld %s protocol version mismatch,"
1946 " my %d != server's %d\n",
1947 ENTITY_NAME(con->peer_name),
3d14c5d2 1948 ceph_pr_addr(&con->peer_addr.in_addr),
31b8006e
SW
1949 le32_to_cpu(con->out_connect.protocol_version),
1950 le32_to_cpu(con->in_reply.protocol_version));
1951 con->error_msg = "protocol version mismatch";
0fa6ebc6 1952 reset_connection(con);
31b8006e
SW
1953 return -1;
1954
4e7a5dcd
SW
1955 case CEPH_MSGR_TAG_BADAUTHORIZER:
1956 con->auth_retry++;
1957 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1958 con->auth_retry);
1959 if (con->auth_retry == 2) {
1960 con->error_msg = "connect authorization failure";
4e7a5dcd
SW
1961 return -1;
1962 }
6d4221b5 1963 con_out_kvec_reset(con);
e825a66d 1964 ret = prepare_write_connect(con);
0da5d703
SW
1965 if (ret < 0)
1966 return ret;
63733a0f 1967 prepare_read_connect(con);
4e7a5dcd 1968 break;
31b8006e
SW
1969
1970 case CEPH_MSGR_TAG_RESETSESSION:
1971 /*
1972 * If we connected with a large connect_seq but the peer
1973 * has no record of a session with us (no connection, or
1974 * connect_seq == 0), they will send RESETSESION to indicate
1975 * that they must have reset their session, and may have
1976 * dropped messages.
1977 */
1978 dout("process_connect got RESET peer seq %u\n",
5bdca4e0 1979 le32_to_cpu(con->in_reply.connect_seq));
31b8006e
SW
1980 pr_err("%s%lld %s connection reset\n",
1981 ENTITY_NAME(con->peer_name),
3d14c5d2 1982 ceph_pr_addr(&con->peer_addr.in_addr));
31b8006e 1983 reset_connection(con);
6d4221b5 1984 con_out_kvec_reset(con);
5a0f8fdd
AE
1985 ret = prepare_write_connect(con);
1986 if (ret < 0)
1987 return ret;
31b8006e
SW
1988 prepare_read_connect(con);
1989
1990 /* Tell ceph about it. */
ec302645 1991 mutex_unlock(&con->mutex);
31b8006e
SW
1992 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1993 if (con->ops->peer_reset)
1994 con->ops->peer_reset(con);
ec302645 1995 mutex_lock(&con->mutex);
8dacc7da 1996 if (con->state != CON_STATE_NEGOTIATING)
0da5d703 1997 return -EAGAIN;
31b8006e
SW
1998 break;
1999
2000 case CEPH_MSGR_TAG_RETRY_SESSION:
2001 /*
2002 * If we sent a smaller connect_seq than the peer has, try
2003 * again with a larger value.
2004 */
5bdca4e0 2005 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
31b8006e 2006 le32_to_cpu(con->out_connect.connect_seq),
5bdca4e0
SW
2007 le32_to_cpu(con->in_reply.connect_seq));
2008 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
6d4221b5 2009 con_out_kvec_reset(con);
5a0f8fdd
AE
2010 ret = prepare_write_connect(con);
2011 if (ret < 0)
2012 return ret;
31b8006e
SW
2013 prepare_read_connect(con);
2014 break;
2015
2016 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2017 /*
2018 * If we sent a smaller global_seq than the peer has, try
2019 * again with a larger value.
2020 */
eed0ef2c 2021 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
31b8006e 2022 con->peer_global_seq,
5bdca4e0 2023 le32_to_cpu(con->in_reply.global_seq));
31b8006e 2024 get_global_seq(con->msgr,
5bdca4e0 2025 le32_to_cpu(con->in_reply.global_seq));
6d4221b5 2026 con_out_kvec_reset(con);
5a0f8fdd
AE
2027 ret = prepare_write_connect(con);
2028 if (ret < 0)
2029 return ret;
31b8006e
SW
2030 prepare_read_connect(con);
2031 break;
2032
3a23083b 2033 case CEPH_MSGR_TAG_SEQ:
31b8006e 2034 case CEPH_MSGR_TAG_READY:
04a419f9
SW
2035 if (req_feat & ~server_feat) {
2036 pr_err("%s%lld %s protocol feature mismatch,"
2037 " my required %llx > server's %llx, need %llx\n",
2038 ENTITY_NAME(con->peer_name),
3d14c5d2 2039 ceph_pr_addr(&con->peer_addr.in_addr),
04a419f9
SW
2040 req_feat, server_feat, req_feat & ~server_feat);
2041 con->error_msg = "missing required protocol features";
0fa6ebc6 2042 reset_connection(con);
04a419f9
SW
2043 return -1;
2044 }
8dacc7da 2045
122070a2 2046 WARN_ON(con->state != CON_STATE_NEGOTIATING);
8dacc7da 2047 con->state = CON_STATE_OPEN;
20e55c4c 2048 con->auth_retry = 0; /* we authenticated; clear flag */
31b8006e
SW
2049 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2050 con->connect_seq++;
aba558e2 2051 con->peer_features = server_feat;
31b8006e
SW
2052 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2053 con->peer_global_seq,
2054 le32_to_cpu(con->in_reply.connect_seq),
2055 con->connect_seq);
2056 WARN_ON(con->connect_seq !=
2057 le32_to_cpu(con->in_reply.connect_seq));
92ac41d0
SW
2058
2059 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
c9ffc77a 2060 con_flag_set(con, CON_FLAG_LOSSYTX);
92ac41d0 2061
85effe18 2062 con->delay = 0; /* reset backoff memory */
92ac41d0 2063
3a23083b
SW
2064 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2065 prepare_write_seq(con);
2066 prepare_read_seq(con);
2067 } else {
2068 prepare_read_tag(con);
2069 }
31b8006e
SW
2070 break;
2071
2072 case CEPH_MSGR_TAG_WAIT:
2073 /*
2074 * If there is a connection race (we are opening
2075 * connections to each other), one of us may just have
2076 * to WAIT. This shouldn't happen if we are the
2077 * client.
2078 */
04177882
SW
2079 pr_err("process_connect got WAIT as client\n");
2080 con->error_msg = "protocol error, got WAIT as client";
2081 return -1;
31b8006e
SW
2082
2083 default:
2084 pr_err("connect protocol error, will retry\n");
2085 con->error_msg = "protocol error, garbage tag during connect";
2086 return -1;
2087 }
2088 return 0;
2089}
2090
2091
2092/*
2093 * read (part of) an ack
2094 */
2095static int read_partial_ack(struct ceph_connection *con)
2096{
fd51653f
AE
2097 int size = sizeof (con->in_temp_ack);
2098 int end = size;
31b8006e 2099
fd51653f 2100 return read_partial(con, end, size, &con->in_temp_ack);
31b8006e
SW
2101}
2102
31b8006e
SW
2103/*
2104 * We can finally discard anything that's been acked.
2105 */
2106static void process_ack(struct ceph_connection *con)
2107{
2108 struct ceph_msg *m;
2109 u64 ack = le64_to_cpu(con->in_temp_ack);
2110 u64 seq;
2111
31b8006e
SW
2112 while (!list_empty(&con->out_sent)) {
2113 m = list_first_entry(&con->out_sent, struct ceph_msg,
2114 list_head);
2115 seq = le64_to_cpu(m->hdr.seq);
2116 if (seq > ack)
2117 break;
2118 dout("got ack for seq %llu type %d at %p\n", seq,
2119 le16_to_cpu(m->hdr.type), m);
4cf9d544 2120 m->ack_stamp = jiffies;
31b8006e
SW
2121 ceph_msg_remove(m);
2122 }
31b8006e
SW
2123 prepare_read_tag(con);
2124}
2125
2126
2450418c 2127static int read_partial_message_section(struct ceph_connection *con,
213c99ee
SW
2128 struct kvec *section,
2129 unsigned int sec_len, u32 *crc)
2450418c 2130{
68b4476b 2131 int ret, left;
2450418c
YS
2132
2133 BUG_ON(!section);
2134
2135 while (section->iov_len < sec_len) {
2136 BUG_ON(section->iov_base == NULL);
2137 left = sec_len - section->iov_len;
2138 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2139 section->iov_len, left);
2140 if (ret <= 0)
2141 return ret;
2142 section->iov_len += ret;
2450418c 2143 }
fe3ad593
AE
2144 if (section->iov_len == sec_len)
2145 *crc = crc32c(0, section->iov_base, section->iov_len);
31b8006e 2146
2450418c
YS
2147 return 1;
2148}
31b8006e 2149
34d2d200
AE
2150static int read_partial_msg_data(struct ceph_connection *con)
2151{
2152 struct ceph_msg *msg = con->in_msg;
8ae4f4f5 2153 struct ceph_msg_data_cursor *cursor = &msg->cursor;
34d2d200 2154 const bool do_datacrc = !con->msgr->nocrc;
686be208
AE
2155 struct page *page;
2156 size_t page_offset;
2157 size_t length;
f5db90bc 2158 u32 crc = 0;
34d2d200
AE
2159 int ret;
2160
2161 BUG_ON(!msg);
5240d9f9 2162 if (list_empty(&msg->data))
4c59b4a2 2163 return -EIO;
34d2d200 2164
f5db90bc
AE
2165 if (do_datacrc)
2166 crc = con->in_data_crc;
643c68a4 2167 while (cursor->resid) {
8ae4f4f5 2168 page = ceph_msg_data_next(&msg->cursor, &page_offset, &length,
4c59b4a2 2169 NULL);
686be208 2170 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
f5db90bc
AE
2171 if (ret <= 0) {
2172 if (do_datacrc)
2173 con->in_data_crc = crc;
2174
686be208 2175 return ret;
f5db90bc 2176 }
686be208
AE
2177
2178 if (do_datacrc)
f5db90bc 2179 crc = ceph_crc32c_page(crc, page, page_offset, ret);
8ae4f4f5 2180 (void) ceph_msg_data_advance(&msg->cursor, (size_t)ret);
34d2d200 2181 }
f5db90bc
AE
2182 if (do_datacrc)
2183 con->in_data_crc = crc;
34d2d200
AE
2184
2185 return 1; /* must return > 0 to indicate success */
2186}
2187
31b8006e
SW
2188/*
2189 * read (part of) a message.
2190 */
686be208
AE
2191static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2192
31b8006e
SW
2193static int read_partial_message(struct ceph_connection *con)
2194{
2195 struct ceph_msg *m = con->in_msg;
fd51653f
AE
2196 int size;
2197 int end;
31b8006e 2198 int ret;
95c96174 2199 unsigned int front_len, middle_len, data_len;
37675b0f 2200 bool do_datacrc = !con->msgr->nocrc;
ae18756b 2201 u64 seq;
fe3ad593 2202 u32 crc;
31b8006e
SW
2203
2204 dout("read_partial_message con %p msg %p\n", con, m);
2205
2206 /* header */
fd51653f
AE
2207 size = sizeof (con->in_hdr);
2208 end = size;
2209 ret = read_partial(con, end, size, &con->in_hdr);
57dac9d1
AE
2210 if (ret <= 0)
2211 return ret;
fe3ad593
AE
2212
2213 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2214 if (cpu_to_le32(crc) != con->in_hdr.crc) {
2215 pr_err("read_partial_message bad hdr "
2216 " crc %u != expected %u\n",
2217 crc, con->in_hdr.crc);
2218 return -EBADMSG;
2219 }
2220
31b8006e
SW
2221 front_len = le32_to_cpu(con->in_hdr.front_len);
2222 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2223 return -EIO;
2224 middle_len = le32_to_cpu(con->in_hdr.middle_len);
7b11ba37 2225 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
31b8006e
SW
2226 return -EIO;
2227 data_len = le32_to_cpu(con->in_hdr.data_len);
2228 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2229 return -EIO;
2230
ae18756b
SW
2231 /* verify seq# */
2232 seq = le64_to_cpu(con->in_hdr.seq);
2233 if ((s64)seq - (s64)con->in_seq < 1) {
df9f86fa 2234 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
ae18756b 2235 ENTITY_NAME(con->peer_name),
3d14c5d2 2236 ceph_pr_addr(&con->peer_addr.in_addr),
ae18756b
SW
2237 seq, con->in_seq + 1);
2238 con->in_base_pos = -front_len - middle_len - data_len -
2239 sizeof(m->footer);
2240 con->in_tag = CEPH_MSGR_TAG_READY;
ae18756b
SW
2241 return 0;
2242 } else if ((s64)seq - (s64)con->in_seq > 1) {
2243 pr_err("read_partial_message bad seq %lld expected %lld\n",
2244 seq, con->in_seq + 1);
2245 con->error_msg = "bad message sequence # for incoming message";
2246 return -EBADMSG;
2247 }
2248
31b8006e
SW
2249 /* allocate message? */
2250 if (!con->in_msg) {
4740a623
SW
2251 int skip = 0;
2252
31b8006e 2253 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
6ebc8b32 2254 front_len, data_len);
4740a623
SW
2255 ret = ceph_con_in_msg_alloc(con, &skip);
2256 if (ret < 0)
2257 return ret;
f759ebb9
AE
2258
2259 BUG_ON(!con->in_msg ^ skip);
2260 if (con->in_msg && data_len > con->in_msg->data_length) {
2261 pr_warning("%s skipping long message (%u > %zd)\n",
2262 __func__, data_len, con->in_msg->data_length);
2263 ceph_msg_put(con->in_msg);
2264 con->in_msg = NULL;
2265 skip = 1;
2266 }
2450418c 2267 if (skip) {
31b8006e 2268 /* skip this message */
a79832f2 2269 dout("alloc_msg said skip message\n");
31b8006e
SW
2270 con->in_base_pos = -front_len - middle_len - data_len -
2271 sizeof(m->footer);
2272 con->in_tag = CEPH_MSGR_TAG_READY;
684be25c 2273 con->in_seq++;
31b8006e
SW
2274 return 0;
2275 }
38941f80 2276
4740a623 2277 BUG_ON(!con->in_msg);
38941f80 2278 BUG_ON(con->in_msg->con != con);
31b8006e
SW
2279 m = con->in_msg;
2280 m->front.iov_len = 0; /* haven't read it yet */
2450418c
YS
2281 if (m->middle)
2282 m->middle->vec.iov_len = 0;
9d7f0f13 2283
78625051 2284 /* prepare for data payload, if any */
a4107026 2285
78625051 2286 if (data_len)
98fa5dd8 2287 prepare_message_data(con->in_msg, data_len);
31b8006e
SW
2288 }
2289
2290 /* front */
2450418c
YS
2291 ret = read_partial_message_section(con, &m->front, front_len,
2292 &con->in_front_crc);
2293 if (ret <= 0)
2294 return ret;
31b8006e
SW
2295
2296 /* middle */
2450418c 2297 if (m->middle) {
213c99ee
SW
2298 ret = read_partial_message_section(con, &m->middle->vec,
2299 middle_len,
2450418c 2300 &con->in_middle_crc);
31b8006e
SW
2301 if (ret <= 0)
2302 return ret;
31b8006e
SW
2303 }
2304
2305 /* (page) data */
34d2d200
AE
2306 if (data_len) {
2307 ret = read_partial_msg_data(con);
2308 if (ret <= 0)
2309 return ret;
31b8006e
SW
2310 }
2311
31b8006e 2312 /* footer */
fd51653f
AE
2313 size = sizeof (m->footer);
2314 end += size;
2315 ret = read_partial(con, end, size, &m->footer);
57dac9d1
AE
2316 if (ret <= 0)
2317 return ret;
2318
31b8006e
SW
2319 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2320 m, front_len, m->footer.front_crc, middle_len,
2321 m->footer.middle_crc, data_len, m->footer.data_crc);
2322
2323 /* crc ok? */
2324 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2325 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2326 m, con->in_front_crc, m->footer.front_crc);
2327 return -EBADMSG;
2328 }
2329 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2330 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2331 m, con->in_middle_crc, m->footer.middle_crc);
2332 return -EBADMSG;
2333 }
bca064d2 2334 if (do_datacrc &&
31b8006e
SW
2335 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2336 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2337 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2338 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2339 return -EBADMSG;
2340 }
2341
2342 return 1; /* done! */
2343}
2344
2345/*
2346 * Process message. This happens in the worker thread. The callback should
2347 * be careful not to do anything that waits on other incoming messages or it
2348 * may deadlock.
2349 */
2350static void process_message(struct ceph_connection *con)
2351{
5e095e8b 2352 struct ceph_msg *msg;
31b8006e 2353
38941f80
AE
2354 BUG_ON(con->in_msg->con != con);
2355 con->in_msg->con = NULL;
5e095e8b 2356 msg = con->in_msg;
31b8006e 2357 con->in_msg = NULL;
36eb71aa 2358 con->ops->put(con);
31b8006e
SW
2359
2360 /* if first message, set peer_name */
2361 if (con->peer_name.type == 0)
dbad185d 2362 con->peer_name = msg->hdr.src;
31b8006e 2363
31b8006e 2364 con->in_seq++;
ec302645 2365 mutex_unlock(&con->mutex);
31b8006e
SW
2366
2367 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2368 msg, le64_to_cpu(msg->hdr.seq),
dbad185d 2369 ENTITY_NAME(msg->hdr.src),
31b8006e
SW
2370 le16_to_cpu(msg->hdr.type),
2371 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2372 le32_to_cpu(msg->hdr.front_len),
2373 le32_to_cpu(msg->hdr.data_len),
2374 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2375 con->ops->dispatch(con, msg);
ec302645
SW
2376
2377 mutex_lock(&con->mutex);
31b8006e
SW
2378}
2379
2380
2381/*
2382 * Write something to the socket. Called in a worker thread when the
2383 * socket appears to be writeable and we have something ready to send.
2384 */
2385static int try_write(struct ceph_connection *con)
2386{
31b8006e
SW
2387 int ret = 1;
2388
d59315ca 2389 dout("try_write start %p state %lu\n", con, con->state);
31b8006e 2390
31b8006e
SW
2391more:
2392 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2393
2394 /* open the socket first? */
8dacc7da
SW
2395 if (con->state == CON_STATE_PREOPEN) {
2396 BUG_ON(con->sock);
2397 con->state = CON_STATE_CONNECTING;
a5988c49 2398
e2200423 2399 con_out_kvec_reset(con);
e825a66d 2400 prepare_write_banner(con);
eed0ef2c 2401 prepare_read_banner(con);
31b8006e 2402
cf3e5c40 2403 BUG_ON(con->in_msg);
31b8006e
SW
2404 con->in_tag = CEPH_MSGR_TAG_READY;
2405 dout("try_write initiating connect on %p new state %lu\n",
2406 con, con->state);
41617d0c
AE
2407 ret = ceph_tcp_connect(con);
2408 if (ret < 0) {
31b8006e 2409 con->error_msg = "connect error";
31b8006e
SW
2410 goto out;
2411 }
2412 }
2413
2414more_kvec:
2415 /* kvec data queued? */
2416 if (con->out_skip) {
2417 ret = write_partial_skip(con);
2418 if (ret <= 0)
42961d23 2419 goto out;
31b8006e
SW
2420 }
2421 if (con->out_kvec_left) {
2422 ret = write_partial_kvec(con);
2423 if (ret <= 0)
42961d23 2424 goto out;
31b8006e
SW
2425 }
2426
2427 /* msg pages? */
2428 if (con->out_msg) {
c86a2930
SW
2429 if (con->out_msg_done) {
2430 ceph_msg_put(con->out_msg);
2431 con->out_msg = NULL; /* we're done with this one */
2432 goto do_next;
2433 }
2434
34d2d200 2435 ret = write_partial_message_data(con);
31b8006e
SW
2436 if (ret == 1)
2437 goto more_kvec; /* we need to send the footer, too! */
2438 if (ret == 0)
42961d23 2439 goto out;
31b8006e 2440 if (ret < 0) {
34d2d200 2441 dout("try_write write_partial_message_data err %d\n",
31b8006e 2442 ret);
42961d23 2443 goto out;
31b8006e
SW
2444 }
2445 }
2446
c86a2930 2447do_next:
8dacc7da 2448 if (con->state == CON_STATE_OPEN) {
31b8006e
SW
2449 /* is anything else pending? */
2450 if (!list_empty(&con->out_queue)) {
2451 prepare_write_message(con);
2452 goto more;
2453 }
2454 if (con->in_seq > con->in_seq_acked) {
2455 prepare_write_ack(con);
2456 goto more;
2457 }
c9ffc77a 2458 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
31b8006e
SW
2459 prepare_write_keepalive(con);
2460 goto more;
2461 }
2462 }
2463
2464 /* Nothing to do! */
c9ffc77a 2465 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
31b8006e 2466 dout("try_write nothing else to write.\n");
31b8006e
SW
2467 ret = 0;
2468out:
42961d23 2469 dout("try_write done on %p ret %d\n", con, ret);
31b8006e
SW
2470 return ret;
2471}
2472
2473
2474
2475/*
2476 * Read what we can from the socket.
2477 */
2478static int try_read(struct ceph_connection *con)
2479{
31b8006e
SW
2480 int ret = -1;
2481
8dacc7da
SW
2482more:
2483 dout("try_read start on %p state %lu\n", con, con->state);
2484 if (con->state != CON_STATE_CONNECTING &&
2485 con->state != CON_STATE_NEGOTIATING &&
2486 con->state != CON_STATE_OPEN)
31b8006e
SW
2487 return 0;
2488
8dacc7da 2489 BUG_ON(!con->sock);
ec302645 2490
31b8006e
SW
2491 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2492 con->in_base_pos);
0da5d703 2493
8dacc7da 2494 if (con->state == CON_STATE_CONNECTING) {
7593af92
AE
2495 dout("try_read connecting\n");
2496 ret = read_partial_banner(con);
2497 if (ret <= 0)
ab166d5a 2498 goto out;
7593af92
AE
2499 ret = process_banner(con);
2500 if (ret < 0)
2501 goto out;
2502
8dacc7da 2503 con->state = CON_STATE_NEGOTIATING;
7593af92 2504
6d4221b5
JS
2505 /*
2506 * Received banner is good, exchange connection info.
2507 * Do not reset out_kvec, as sending our banner raced
2508 * with receiving peer banner after connect completed.
2509 */
7593af92
AE
2510 ret = prepare_write_connect(con);
2511 if (ret < 0)
2512 goto out;
2513 prepare_read_connect(con);
2514
2515 /* Send connection info before awaiting response */
0da5d703
SW
2516 goto out;
2517 }
2518
8dacc7da 2519 if (con->state == CON_STATE_NEGOTIATING) {
7593af92 2520 dout("try_read negotiating\n");
31b8006e
SW
2521 ret = read_partial_connect(con);
2522 if (ret <= 0)
31b8006e 2523 goto out;
98bdb0aa
SW
2524 ret = process_connect(con);
2525 if (ret < 0)
2526 goto out;
31b8006e
SW
2527 goto more;
2528 }
2529
122070a2 2530 WARN_ON(con->state != CON_STATE_OPEN);
8dacc7da 2531
31b8006e
SW
2532 if (con->in_base_pos < 0) {
2533 /*
2534 * skipping + discarding content.
2535 *
2536 * FIXME: there must be a better way to do this!
2537 */
84495f49
AE
2538 static char buf[SKIP_BUF_SIZE];
2539 int skip = min((int) sizeof (buf), -con->in_base_pos);
2540
31b8006e
SW
2541 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2542 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2543 if (ret <= 0)
98bdb0aa 2544 goto out;
31b8006e
SW
2545 con->in_base_pos += ret;
2546 if (con->in_base_pos)
2547 goto more;
2548 }
2549 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2550 /*
2551 * what's next?
2552 */
2553 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2554 if (ret <= 0)
98bdb0aa 2555 goto out;
31b8006e
SW
2556 dout("try_read got tag %d\n", (int)con->in_tag);
2557 switch (con->in_tag) {
2558 case CEPH_MSGR_TAG_MSG:
2559 prepare_read_message(con);
2560 break;
2561 case CEPH_MSGR_TAG_ACK:
2562 prepare_read_ack(con);
2563 break;
2564 case CEPH_MSGR_TAG_CLOSE:
8dacc7da
SW
2565 con_close_socket(con);
2566 con->state = CON_STATE_CLOSED;
98bdb0aa 2567 goto out;
31b8006e
SW
2568 default:
2569 goto bad_tag;
2570 }
2571 }
2572 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2573 ret = read_partial_message(con);
2574 if (ret <= 0) {
2575 switch (ret) {
2576 case -EBADMSG:
2577 con->error_msg = "bad crc";
2578 ret = -EIO;
98bdb0aa 2579 break;
31b8006e
SW
2580 case -EIO:
2581 con->error_msg = "io error";
98bdb0aa 2582 break;
31b8006e 2583 }
98bdb0aa 2584 goto out;
31b8006e
SW
2585 }
2586 if (con->in_tag == CEPH_MSGR_TAG_READY)
2587 goto more;
2588 process_message(con);
7b862e07
SW
2589 if (con->state == CON_STATE_OPEN)
2590 prepare_read_tag(con);
31b8006e
SW
2591 goto more;
2592 }
3a23083b
SW
2593 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2594 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2595 /*
2596 * the final handshake seq exchange is semantically
2597 * equivalent to an ACK
2598 */
31b8006e
SW
2599 ret = read_partial_ack(con);
2600 if (ret <= 0)
98bdb0aa 2601 goto out;
31b8006e
SW
2602 process_ack(con);
2603 goto more;
2604 }
2605
31b8006e 2606out:
98bdb0aa 2607 dout("try_read done on %p ret %d\n", con, ret);
31b8006e
SW
2608 return ret;
2609
2610bad_tag:
2611 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2612 con->error_msg = "protocol error, garbage tag";
2613 ret = -1;
2614 goto out;
2615}
2616
2617
2618/*
802c6d96
AE
2619 * Atomically queue work on a connection after the specified delay.
2620 * Bump @con reference to avoid races with connection teardown.
2621 * Returns 0 if work was queued, or an error code otherwise.
31b8006e 2622 */
802c6d96 2623static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
31b8006e 2624{
31b8006e 2625 if (!con->ops->get(con)) {
802c6d96
AE
2626 dout("%s %p ref count 0\n", __func__, con);
2627
2628 return -ENOENT;
31b8006e
SW
2629 }
2630
802c6d96
AE
2631 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2632 dout("%s %p - already queued\n", __func__, con);
31b8006e 2633 con->ops->put(con);
802c6d96
AE
2634
2635 return -EBUSY;
31b8006e 2636 }
802c6d96
AE
2637
2638 dout("%s %p %lu\n", __func__, con, delay);
2639
2640 return 0;
2641}
2642
2643static void queue_con(struct ceph_connection *con)
2644{
2645 (void) queue_con_delay(con, 0);
31b8006e
SW
2646}
2647
7bb21d68
AE
2648static bool con_sock_closed(struct ceph_connection *con)
2649{
c9ffc77a 2650 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
7bb21d68
AE
2651 return false;
2652
2653#define CASE(x) \
2654 case CON_STATE_ ## x: \
2655 con->error_msg = "socket closed (con state " #x ")"; \
2656 break;
2657
2658 switch (con->state) {
2659 CASE(CLOSED);
2660 CASE(PREOPEN);
2661 CASE(CONNECTING);
2662 CASE(NEGOTIATING);
2663 CASE(OPEN);
2664 CASE(STANDBY);
2665 default:
2666 pr_warning("%s con %p unrecognized state %lu\n",
2667 __func__, con, con->state);
2668 con->error_msg = "unrecognized con state";
2669 BUG();
2670 break;
2671 }
2672#undef CASE
2673
2674 return true;
2675}
2676
f20a39fd
AE
2677static bool con_backoff(struct ceph_connection *con)
2678{
2679 int ret;
2680
2681 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2682 return false;
2683
2684 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2685 if (ret) {
2686 dout("%s: con %p FAILED to back off %lu\n", __func__,
2687 con, con->delay);
2688 BUG_ON(ret == -ENOENT);
2689 con_flag_set(con, CON_FLAG_BACKOFF);
2690 }
2691
2692 return true;
2693}
2694
93209264
AE
2695/* Finish fault handling; con->mutex must *not* be held here */
2696
2697static void con_fault_finish(struct ceph_connection *con)
2698{
2699 /*
2700 * in case we faulted due to authentication, invalidate our
2701 * current tickets so that we can get new ones.
2702 */
2703 if (con->auth_retry && con->ops->invalidate_authorizer) {
2704 dout("calling invalidate_authorizer()\n");
2705 con->ops->invalidate_authorizer(con);
2706 }
2707
2708 if (con->ops->fault)
2709 con->ops->fault(con);
2710}
2711
31b8006e
SW
2712/*
2713 * Do some work on a connection. Drop a connection ref when we're done.
2714 */
2715static void con_work(struct work_struct *work)
2716{
2717 struct ceph_connection *con = container_of(work, struct ceph_connection,
2718 work.work);
49659416 2719 bool fault;
31b8006e 2720
9dd4658d 2721 mutex_lock(&con->mutex);
49659416
AE
2722 while (true) {
2723 int ret;
31b8006e 2724
49659416
AE
2725 if ((fault = con_sock_closed(con))) {
2726 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2727 break;
2728 }
2729 if (con_backoff(con)) {
2730 dout("%s: con %p BACKOFF\n", __func__, con);
2731 break;
2732 }
2733 if (con->state == CON_STATE_STANDBY) {
2734 dout("%s: con %p STANDBY\n", __func__, con);
2735 break;
2736 }
2737 if (con->state == CON_STATE_CLOSED) {
2738 dout("%s: con %p CLOSED\n", __func__, con);
2739 BUG_ON(con->sock);
2740 break;
2741 }
2742 if (con->state == CON_STATE_PREOPEN) {
2743 dout("%s: con %p PREOPEN\n", __func__, con);
2744 BUG_ON(con->sock);
2745 }
0da5d703 2746
49659416
AE
2747 ret = try_read(con);
2748 if (ret < 0) {
2749 if (ret == -EAGAIN)
2750 continue;
2751 con->error_msg = "socket error on read";
2752 fault = true;
2753 break;
2754 }
2755
2756 ret = try_write(con);
2757 if (ret < 0) {
2758 if (ret == -EAGAIN)
2759 continue;
2760 con->error_msg = "socket error on write";
2761 fault = true;
2762 }
2763
2764 break; /* If we make it to here, we're done */
3a140a0d 2765 }
b6e7b6a1
AE
2766 if (fault)
2767 con_fault(con);
9dd4658d 2768 mutex_unlock(&con->mutex);
0da5d703 2769
b6e7b6a1
AE
2770 if (fault)
2771 con_fault_finish(con);
2772
2773 con->ops->put(con);
31b8006e
SW
2774}
2775
31b8006e
SW
2776/*
2777 * Generic error/fault handler. A retry mechanism is used with
2778 * exponential backoff
2779 */
93209264 2780static void con_fault(struct ceph_connection *con)
31b8006e 2781{
28362986 2782 pr_warning("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
3d14c5d2 2783 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
31b8006e 2784 dout("fault %p state %lu to peer %s\n",
3d14c5d2 2785 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
31b8006e 2786
122070a2 2787 WARN_ON(con->state != CON_STATE_CONNECTING &&
8dacc7da
SW
2788 con->state != CON_STATE_NEGOTIATING &&
2789 con->state != CON_STATE_OPEN);
ec302645 2790
31b8006e 2791 con_close_socket(con);
5e095e8b 2792
c9ffc77a 2793 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
8dacc7da
SW
2794 dout("fault on LOSSYTX channel, marking CLOSED\n");
2795 con->state = CON_STATE_CLOSED;
93209264 2796 return;
3b5ede07
SW
2797 }
2798
5e095e8b 2799 if (con->in_msg) {
38941f80
AE
2800 BUG_ON(con->in_msg->con != con);
2801 con->in_msg->con = NULL;
5e095e8b
SW
2802 ceph_msg_put(con->in_msg);
2803 con->in_msg = NULL;
36eb71aa 2804 con->ops->put(con);
5e095e8b 2805 }
31b8006e 2806
e80a52d1
SW
2807 /* Requeue anything that hasn't been acked */
2808 list_splice_init(&con->out_sent, &con->out_queue);
9bd2e6f8 2809
e76661d0
SW
2810 /* If there are no messages queued or keepalive pending, place
2811 * the connection in a STANDBY state */
2812 if (list_empty(&con->out_queue) &&
c9ffc77a 2813 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
e00de341 2814 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
c9ffc77a 2815 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
8dacc7da 2816 con->state = CON_STATE_STANDBY;
e80a52d1
SW
2817 } else {
2818 /* retry after a delay. */
8dacc7da 2819 con->state = CON_STATE_PREOPEN;
e80a52d1
SW
2820 if (con->delay == 0)
2821 con->delay = BASE_DELAY_INTERVAL;
2822 else if (con->delay < MAX_DELAY_INTERVAL)
2823 con->delay *= 2;
c9ffc77a 2824 con_flag_set(con, CON_FLAG_BACKOFF);
8618e30b 2825 queue_con(con);
31b8006e 2826 }
31b8006e
SW
2827}
2828
2829
2830
2831/*
15d9882c 2832 * initialize a new messenger instance
31b8006e 2833 */
15d9882c
AE
2834void ceph_messenger_init(struct ceph_messenger *msgr,
2835 struct ceph_entity_addr *myaddr,
2836 u32 supported_features,
2837 u32 required_features,
2838 bool nocrc)
31b8006e 2839{
3d14c5d2
YS
2840 msgr->supported_features = supported_features;
2841 msgr->required_features = required_features;
2842
31b8006e
SW
2843 spin_lock_init(&msgr->global_seq_lock);
2844
31b8006e
SW
2845 if (myaddr)
2846 msgr->inst.addr = *myaddr;
2847
2848 /* select a random nonce */
ac8839d7 2849 msgr->inst.addr.type = 0;
103e2d3a 2850 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
63f2d211 2851 encode_my_addr(msgr);
15d9882c 2852 msgr->nocrc = nocrc;
31b8006e 2853
a2a32584 2854 atomic_set(&msgr->stopping, 0);
31b8006e 2855
15d9882c 2856 dout("%s %p\n", __func__, msgr);
31b8006e 2857}
15d9882c 2858EXPORT_SYMBOL(ceph_messenger_init);
31b8006e 2859
e00de341
SW
2860static void clear_standby(struct ceph_connection *con)
2861{
2862 /* come back from STANDBY? */
8dacc7da 2863 if (con->state == CON_STATE_STANDBY) {
e00de341 2864 dout("clear_standby %p and ++connect_seq\n", con);
8dacc7da 2865 con->state = CON_STATE_PREOPEN;
e00de341 2866 con->connect_seq++;
c9ffc77a
AE
2867 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
2868 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
e00de341
SW
2869 }
2870}
2871
31b8006e
SW
2872/*
2873 * Queue up an outgoing message on the given connection.
2874 */
2875void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2876{
31b8006e 2877 /* set src+dst */
dbad185d 2878 msg->hdr.src = con->msgr->inst.name;
3ca02ef9 2879 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
e84346b7
SW
2880 msg->needs_out_seq = true;
2881
ec302645 2882 mutex_lock(&con->mutex);
92ce034b 2883
8dacc7da 2884 if (con->state == CON_STATE_CLOSED) {
a59b55a6
SW
2885 dout("con_send %p closed, dropping %p\n", con, msg);
2886 ceph_msg_put(msg);
2887 mutex_unlock(&con->mutex);
2888 return;
2889 }
2890
38941f80 2891 BUG_ON(msg->con != NULL);
36eb71aa 2892 msg->con = con->ops->get(con);
92ce034b
AE
2893 BUG_ON(msg->con == NULL);
2894
31b8006e
SW
2895 BUG_ON(!list_empty(&msg->list_head));
2896 list_add_tail(&msg->list_head, &con->out_queue);
2897 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2898 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2899 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2900 le32_to_cpu(msg->hdr.front_len),
2901 le32_to_cpu(msg->hdr.middle_len),
2902 le32_to_cpu(msg->hdr.data_len));
00650931
SW
2903
2904 clear_standby(con);
ec302645 2905 mutex_unlock(&con->mutex);
31b8006e
SW
2906
2907 /* if there wasn't anything waiting to send before, queue
2908 * new work */
c9ffc77a 2909 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
31b8006e
SW
2910 queue_con(con);
2911}
3d14c5d2 2912EXPORT_SYMBOL(ceph_con_send);
31b8006e
SW
2913
2914/*
2915 * Revoke a message that was previously queued for send
2916 */
6740a845 2917void ceph_msg_revoke(struct ceph_msg *msg)
31b8006e 2918{
6740a845
AE
2919 struct ceph_connection *con = msg->con;
2920
2921 if (!con)
2922 return; /* Message not in our possession */
2923
ec302645 2924 mutex_lock(&con->mutex);
31b8006e 2925 if (!list_empty(&msg->list_head)) {
38941f80 2926 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
31b8006e 2927 list_del_init(&msg->list_head);
38941f80 2928 BUG_ON(msg->con == NULL);
36eb71aa 2929 msg->con->ops->put(msg->con);
38941f80 2930 msg->con = NULL;
31b8006e 2931 msg->hdr.seq = 0;
38941f80 2932
31b8006e 2933 ceph_msg_put(msg);
ed98adad
SW
2934 }
2935 if (con->out_msg == msg) {
38941f80 2936 dout("%s %p msg %p - was sending\n", __func__, con, msg);
ed98adad 2937 con->out_msg = NULL;
31b8006e
SW
2938 if (con->out_kvec_is_msg) {
2939 con->out_skip = con->out_kvec_bytes;
2940 con->out_kvec_is_msg = false;
2941 }
ed98adad 2942 msg->hdr.seq = 0;
92ce034b
AE
2943
2944 ceph_msg_put(msg);
31b8006e 2945 }
ec302645 2946 mutex_unlock(&con->mutex);
31b8006e
SW
2947}
2948
350b1c32 2949/*
0d59ab81 2950 * Revoke a message that we may be reading data into
350b1c32 2951 */
8921d114 2952void ceph_msg_revoke_incoming(struct ceph_msg *msg)
350b1c32 2953{
8921d114
AE
2954 struct ceph_connection *con;
2955
2956 BUG_ON(msg == NULL);
2957 if (!msg->con) {
2958 dout("%s msg %p null con\n", __func__, msg);
2959
2960 return; /* Message not in our possession */
2961 }
2962
2963 con = msg->con;
350b1c32 2964 mutex_lock(&con->mutex);
8921d114 2965 if (con->in_msg == msg) {
95c96174
ED
2966 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2967 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2968 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
350b1c32
SW
2969
2970 /* skip rest of message */
8921d114
AE
2971 dout("%s %p msg %p revoked\n", __func__, con, msg);
2972 con->in_base_pos = con->in_base_pos -
350b1c32 2973 sizeof(struct ceph_msg_header) -
0d59ab81
YS
2974 front_len -
2975 middle_len -
2976 data_len -
350b1c32 2977 sizeof(struct ceph_msg_footer);
350b1c32
SW
2978 ceph_msg_put(con->in_msg);
2979 con->in_msg = NULL;
2980 con->in_tag = CEPH_MSGR_TAG_READY;
684be25c 2981 con->in_seq++;
350b1c32 2982 } else {
8921d114
AE
2983 dout("%s %p in_msg %p msg %p no-op\n",
2984 __func__, con, con->in_msg, msg);
350b1c32
SW
2985 }
2986 mutex_unlock(&con->mutex);
2987}
2988
31b8006e
SW
2989/*
2990 * Queue a keepalive byte to ensure the tcp connection is alive.
2991 */
2992void ceph_con_keepalive(struct ceph_connection *con)
2993{
e00de341 2994 dout("con_keepalive %p\n", con);
00650931 2995 mutex_lock(&con->mutex);
e00de341 2996 clear_standby(con);
00650931 2997 mutex_unlock(&con->mutex);
c9ffc77a
AE
2998 if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
2999 con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
31b8006e
SW
3000 queue_con(con);
3001}
3d14c5d2 3002EXPORT_SYMBOL(ceph_con_keepalive);
31b8006e 3003
6644ed7b 3004static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
43794509 3005{
6644ed7b
AE
3006 struct ceph_msg_data *data;
3007
3008 if (WARN_ON(!ceph_msg_data_type_valid(type)))
3009 return NULL;
3010
3011 data = kzalloc(sizeof (*data), GFP_NOFS);
3012 if (data)
3013 data->type = type;
5240d9f9 3014 INIT_LIST_HEAD(&data->links);
6644ed7b
AE
3015
3016 return data;
3017}
3018
3019static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3020{
3021 if (!data)
3022 return;
3023
5240d9f9 3024 WARN_ON(!list_empty(&data->links));
6644ed7b
AE
3025 if (data->type == CEPH_MSG_DATA_PAGELIST) {
3026 ceph_pagelist_release(data->pagelist);
3027 kfree(data->pagelist);
3028 }
3029 kfree(data);
43794509
AE
3030}
3031
90af3602 3032void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
f1baeb2b 3033 size_t length, size_t alignment)
02afca6c 3034{
6644ed7b
AE
3035 struct ceph_msg_data *data;
3036
07aa1558
AE
3037 BUG_ON(!pages);
3038 BUG_ON(!length);
6644ed7b
AE
3039
3040 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
3041 BUG_ON(!data);
3042 data->pages = pages;
3043 data->length = length;
3044 data->alignment = alignment & ~PAGE_MASK;
02afca6c 3045
5240d9f9
AE
3046 list_add_tail(&data->links, &msg->data);
3047 msg->data_length += length;
02afca6c 3048}
90af3602 3049EXPORT_SYMBOL(ceph_msg_data_add_pages);
31b8006e 3050
90af3602 3051void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
27fa8385
AE
3052 struct ceph_pagelist *pagelist)
3053{
6644ed7b
AE
3054 struct ceph_msg_data *data;
3055
07aa1558
AE
3056 BUG_ON(!pagelist);
3057 BUG_ON(!pagelist->length);
27fa8385 3058
6644ed7b
AE
3059 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
3060 BUG_ON(!data);
3061 data->pagelist = pagelist;
3062
5240d9f9
AE
3063 list_add_tail(&data->links, &msg->data);
3064 msg->data_length += pagelist->length;
27fa8385 3065}
90af3602 3066EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
27fa8385 3067
ea96571f 3068#ifdef CONFIG_BLOCK
90af3602 3069void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
a1930804 3070 size_t length)
27fa8385 3071{
6644ed7b
AE
3072 struct ceph_msg_data *data;
3073
07aa1558 3074 BUG_ON(!bio);
27fa8385 3075
6644ed7b
AE
3076 data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
3077 BUG_ON(!data);
3078 data->bio = bio;
c851c495 3079 data->bio_length = length;
6644ed7b 3080
5240d9f9
AE
3081 list_add_tail(&data->links, &msg->data);
3082 msg->data_length += length;
27fa8385 3083}
90af3602 3084EXPORT_SYMBOL(ceph_msg_data_add_bio);
ea96571f 3085#endif /* CONFIG_BLOCK */
27fa8385 3086
31b8006e
SW
3087/*
3088 * construct a new message with given type, size
3089 * the new msg has a ref count of 1.
3090 */
b61c2763
SW
3091struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3092 bool can_fail)
31b8006e
SW
3093{
3094 struct ceph_msg *m;
3095
e3d5d638 3096 m = kmem_cache_zalloc(ceph_msg_cache, flags);
31b8006e
SW
3097 if (m == NULL)
3098 goto out;
31b8006e
SW
3099
3100 m->hdr.type = cpu_to_le16(type);
45c6ceb5 3101 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
31b8006e 3102 m->hdr.front_len = cpu_to_le32(front_len);
ca20892d 3103
9516e45b
AE
3104 INIT_LIST_HEAD(&m->list_head);
3105 kref_init(&m->kref);
5240d9f9 3106 INIT_LIST_HEAD(&m->data);
ca20892d 3107
31b8006e 3108 /* front */
9516e45b 3109 m->front_max = front_len;
31b8006e
SW
3110 if (front_len) {
3111 if (front_len > PAGE_CACHE_SIZE) {
34d23762 3112 m->front.iov_base = __vmalloc(front_len, flags,
31b8006e
SW
3113 PAGE_KERNEL);
3114 m->front_is_vmalloc = true;
3115 } else {
34d23762 3116 m->front.iov_base = kmalloc(front_len, flags);
31b8006e
SW
3117 }
3118 if (m->front.iov_base == NULL) {
b61c2763 3119 dout("ceph_msg_new can't allocate %d bytes\n",
31b8006e
SW
3120 front_len);
3121 goto out2;
3122 }
3123 } else {
3124 m->front.iov_base = NULL;
3125 }
3126 m->front.iov_len = front_len;
3127
bb257664 3128 dout("ceph_msg_new %p front %d\n", m, front_len);
31b8006e
SW
3129 return m;
3130
3131out2:
3132 ceph_msg_put(m);
3133out:
b61c2763
SW
3134 if (!can_fail) {
3135 pr_err("msg_new can't create type %d front %d\n", type,
3136 front_len);
f0ed1b7c 3137 WARN_ON(1);
b61c2763
SW
3138 } else {
3139 dout("msg_new can't create type %d front %d\n", type,
3140 front_len);
3141 }
a79832f2 3142 return NULL;
31b8006e 3143}
3d14c5d2 3144EXPORT_SYMBOL(ceph_msg_new);
31b8006e 3145
31b8006e
SW
3146/*
3147 * Allocate "middle" portion of a message, if it is needed and wasn't
3148 * allocated by alloc_msg. This allows us to read a small fixed-size
3149 * per-type header in the front and then gracefully fail (i.e.,
3150 * propagate the error to the caller based on info in the front) when
3151 * the middle is too large.
3152 */
2450418c 3153static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
31b8006e
SW
3154{
3155 int type = le16_to_cpu(msg->hdr.type);
3156 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3157
3158 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3159 ceph_msg_type_name(type), middle_len);
3160 BUG_ON(!middle_len);
3161 BUG_ON(msg->middle);
3162
b6c1d5b8 3163 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
31b8006e
SW
3164 if (!msg->middle)
3165 return -ENOMEM;
3166 return 0;
3167}
3168
2450418c 3169/*
1c20f2d2
AE
3170 * Allocate a message for receiving an incoming message on a
3171 * connection, and save the result in con->in_msg. Uses the
3172 * connection's private alloc_msg op if available.
3173 *
4740a623
SW
3174 * Returns 0 on success, or a negative error code.
3175 *
3176 * On success, if we set *skip = 1:
3177 * - the next message should be skipped and ignored.
3178 * - con->in_msg == NULL
3179 * or if we set *skip = 0:
3180 * - con->in_msg is non-null.
3181 * On error (ENOMEM, EAGAIN, ...),
3182 * - con->in_msg == NULL
2450418c 3183 */
4740a623 3184static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
2450418c 3185{
4740a623 3186 struct ceph_msg_header *hdr = &con->in_hdr;
2450418c 3187 int middle_len = le32_to_cpu(hdr->middle_len);
1d866d1c 3188 struct ceph_msg *msg;
4740a623 3189 int ret = 0;
2450418c 3190
1c20f2d2 3191 BUG_ON(con->in_msg != NULL);
53ded495 3192 BUG_ON(!con->ops->alloc_msg);
2450418c 3193
53ded495
AE
3194 mutex_unlock(&con->mutex);
3195 msg = con->ops->alloc_msg(con, hdr, skip);
3196 mutex_lock(&con->mutex);
3197 if (con->state != CON_STATE_OPEN) {
3198 if (msg)
1d866d1c 3199 ceph_msg_put(msg);
53ded495
AE
3200 return -EAGAIN;
3201 }
4137577a
AE
3202 if (msg) {
3203 BUG_ON(*skip);
3204 con->in_msg = msg;
36eb71aa 3205 con->in_msg->con = con->ops->get(con);
92ce034b 3206 BUG_ON(con->in_msg->con == NULL);
4137577a
AE
3207 } else {
3208 /*
3209 * Null message pointer means either we should skip
3210 * this message or we couldn't allocate memory. The
3211 * former is not an error.
3212 */
3213 if (*skip)
3214 return 0;
3215 con->error_msg = "error allocating memory for incoming message";
3216
53ded495 3217 return -ENOMEM;
2450418c 3218 }
1c20f2d2 3219 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
2450418c 3220
1c20f2d2
AE
3221 if (middle_len && !con->in_msg->middle) {
3222 ret = ceph_alloc_middle(con, con->in_msg);
2450418c 3223 if (ret < 0) {
1c20f2d2
AE
3224 ceph_msg_put(con->in_msg);
3225 con->in_msg = NULL;
2450418c
YS
3226 }
3227 }
9d7f0f13 3228
4740a623 3229 return ret;
2450418c
YS
3230}
3231
31b8006e
SW
3232
3233/*
3234 * Free a generically kmalloc'd message.
3235 */
3236void ceph_msg_kfree(struct ceph_msg *m)
3237{
3238 dout("msg_kfree %p\n", m);
3239 if (m->front_is_vmalloc)
3240 vfree(m->front.iov_base);
3241 else
3242 kfree(m->front.iov_base);
e3d5d638 3243 kmem_cache_free(ceph_msg_cache, m);
31b8006e
SW
3244}
3245
3246/*
3247 * Drop a msg ref. Destroy as needed.
3248 */
c2e552e7
SW
3249void ceph_msg_last_put(struct kref *kref)
3250{
3251 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
5240d9f9
AE
3252 LIST_HEAD(data);
3253 struct list_head *links;
3254 struct list_head *next;
31b8006e 3255
c2e552e7
SW
3256 dout("ceph_msg_put last one on %p\n", m);
3257 WARN_ON(!list_empty(&m->list_head));
3258
3259 /* drop middle, data, if any */
3260 if (m->middle) {
3261 ceph_buffer_put(m->middle);
3262 m->middle = NULL;
31b8006e 3263 }
5240d9f9
AE
3264
3265 list_splice_init(&m->data, &data);
3266 list_for_each_safe(links, next, &data) {
3267 struct ceph_msg_data *data;
3268
3269 data = list_entry(links, struct ceph_msg_data, links);
3270 list_del_init(links);
3271 ceph_msg_data_destroy(data);
3272 }
a1930804 3273 m->data_length = 0;
58bb3b37 3274
c2e552e7
SW
3275 if (m->pool)
3276 ceph_msgpool_put(m->pool, m);
3277 else
3278 ceph_msg_kfree(m);
31b8006e 3279}
3d14c5d2 3280EXPORT_SYMBOL(ceph_msg_last_put);
9ec7cab1
SW
3281
3282void ceph_msg_dump(struct ceph_msg *msg)
3283{
4a73ef27 3284 pr_debug("msg_dump %p (front_max %d length %zd)\n", msg,
5240d9f9 3285 msg->front_max, msg->data_length);
9ec7cab1
SW
3286 print_hex_dump(KERN_DEBUG, "header: ",
3287 DUMP_PREFIX_OFFSET, 16, 1,
3288 &msg->hdr, sizeof(msg->hdr), true);
3289 print_hex_dump(KERN_DEBUG, " front: ",
3290 DUMP_PREFIX_OFFSET, 16, 1,
3291 msg->front.iov_base, msg->front.iov_len, true);
3292 if (msg->middle)
3293 print_hex_dump(KERN_DEBUG, "middle: ",
3294 DUMP_PREFIX_OFFSET, 16, 1,
3295 msg->middle->vec.iov_base,
3296 msg->middle->vec.iov_len, true);
3297 print_hex_dump(KERN_DEBUG, "footer: ",
3298 DUMP_PREFIX_OFFSET, 16, 1,
3299 &msg->footer, sizeof(msg->footer), true);
3300}
3d14c5d2 3301EXPORT_SYMBOL(ceph_msg_dump);