1 #include "ceph_debug.h"
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>
9 #include <linux/socket.h>
10 #include <linux/string.h>
14 #include "messenger.h"
18 * Ceph uses the messenger to exchange ceph_msg messages with other
19 * hosts in the system. The messenger provides ordered and reliable
20 * delivery. We tolerate TCP disconnects by reconnecting (with
21 * exponential backoff) in the case of a fault (disconnection, bad
22 * crc, protocol error). Acks allow sent messages to be discarded by
26 /* static tag bytes (protocol control messages) */
27 static char tag_msg
= CEPH_MSGR_TAG_MSG
;
28 static char tag_ack
= CEPH_MSGR_TAG_ACK
;
29 static char tag_keepalive
= CEPH_MSGR_TAG_KEEPALIVE
;
32 static void queue_con(struct ceph_connection
*con
);
33 static void con_work(struct work_struct
*);
34 static void ceph_fault(struct ceph_connection
*con
);
36 const char *ceph_name_type_str(int t
)
39 case CEPH_ENTITY_TYPE_MON
: return "mon";
40 case CEPH_ENTITY_TYPE_MDS
: return "mds";
41 case CEPH_ENTITY_TYPE_OSD
: return "osd";
42 case CEPH_ENTITY_TYPE_CLIENT
: return "client";
43 case CEPH_ENTITY_TYPE_ADMIN
: return "admin";
44 default: return "???";
49 * nicely render a sockaddr as a string.
51 #define MAX_ADDR_STR 20
52 static char addr_str
[MAX_ADDR_STR
][40];
53 static DEFINE_SPINLOCK(addr_str_lock
);
54 static int last_addr_str
;
56 const char *pr_addr(const struct sockaddr_storage
*ss
)
60 struct sockaddr_in
*in4
= (void *)ss
;
61 unsigned char *quad
= (void *)&in4
->sin_addr
.s_addr
;
62 struct sockaddr_in6
*in6
= (void *)ss
;
64 spin_lock(&addr_str_lock
);
66 if (last_addr_str
== MAX_ADDR_STR
)
68 spin_unlock(&addr_str_lock
);
71 switch (ss
->ss_family
) {
73 sprintf(s
, "%u.%u.%u.%u:%u",
74 (unsigned int)quad
[0],
75 (unsigned int)quad
[1],
76 (unsigned int)quad
[2],
77 (unsigned int)quad
[3],
78 (unsigned int)ntohs(in4
->sin_port
));
82 sprintf(s
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u",
83 in6
->sin6_addr
.s6_addr16
[0],
84 in6
->sin6_addr
.s6_addr16
[1],
85 in6
->sin6_addr
.s6_addr16
[2],
86 in6
->sin6_addr
.s6_addr16
[3],
87 in6
->sin6_addr
.s6_addr16
[4],
88 in6
->sin6_addr
.s6_addr16
[5],
89 in6
->sin6_addr
.s6_addr16
[6],
90 in6
->sin6_addr
.s6_addr16
[7],
91 (unsigned int)ntohs(in6
->sin6_port
));
95 sprintf(s
, "(unknown sockaddr family %d)", (int)ss
->ss_family
);
101 static void encode_my_addr(struct ceph_messenger
*msgr
)
103 memcpy(&msgr
->my_enc_addr
, &msgr
->inst
.addr
, sizeof(msgr
->my_enc_addr
));
104 ceph_encode_addr(&msgr
->my_enc_addr
);
108 * work queue for all reading and writing to/from the socket.
110 struct workqueue_struct
*ceph_msgr_wq
;
112 int __init
ceph_msgr_init(void)
114 ceph_msgr_wq
= create_workqueue("ceph-msgr");
115 if (IS_ERR(ceph_msgr_wq
)) {
116 int ret
= PTR_ERR(ceph_msgr_wq
);
117 pr_err("msgr_init failed to create workqueue: %d\n", ret
);
124 void ceph_msgr_exit(void)
126 destroy_workqueue(ceph_msgr_wq
);
130 * socket callback functions
133 /* data available on socket, or listen socket received a connect */
134 static void ceph_data_ready(struct sock
*sk
, int count_unused
)
136 struct ceph_connection
*con
=
137 (struct ceph_connection
*)sk
->sk_user_data
;
138 if (sk
->sk_state
!= TCP_CLOSE_WAIT
) {
139 dout("ceph_data_ready on %p state = %lu, queueing work\n",
145 /* socket has buffer space for writing */
146 static void ceph_write_space(struct sock
*sk
)
148 struct ceph_connection
*con
=
149 (struct ceph_connection
*)sk
->sk_user_data
;
151 /* only queue to workqueue if there is data we want to write. */
152 if (test_bit(WRITE_PENDING
, &con
->state
)) {
153 dout("ceph_write_space %p queueing write work\n", con
);
156 dout("ceph_write_space %p nothing to write\n", con
);
159 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
160 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
163 /* socket's state has changed */
164 static void ceph_state_change(struct sock
*sk
)
166 struct ceph_connection
*con
=
167 (struct ceph_connection
*)sk
->sk_user_data
;
169 dout("ceph_state_change %p state = %lu sk_state = %u\n",
170 con
, con
->state
, sk
->sk_state
);
172 if (test_bit(CLOSED
, &con
->state
))
175 switch (sk
->sk_state
) {
177 dout("ceph_state_change TCP_CLOSE\n");
179 dout("ceph_state_change TCP_CLOSE_WAIT\n");
180 if (test_and_set_bit(SOCK_CLOSED
, &con
->state
) == 0) {
181 if (test_bit(CONNECTING
, &con
->state
))
182 con
->error_msg
= "connection failed";
184 con
->error_msg
= "socket closed";
188 case TCP_ESTABLISHED
:
189 dout("ceph_state_change TCP_ESTABLISHED\n");
196 * set up socket callbacks
198 static void set_sock_callbacks(struct socket
*sock
,
199 struct ceph_connection
*con
)
201 struct sock
*sk
= sock
->sk
;
202 sk
->sk_user_data
= (void *)con
;
203 sk
->sk_data_ready
= ceph_data_ready
;
204 sk
->sk_write_space
= ceph_write_space
;
205 sk
->sk_state_change
= ceph_state_change
;
214 * initiate connection to a remote socket.
216 static struct socket
*ceph_tcp_connect(struct ceph_connection
*con
)
218 struct sockaddr
*paddr
= (struct sockaddr
*)&con
->peer_addr
.in_addr
;
223 ret
= sock_create_kern(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
227 sock
->sk
->sk_allocation
= GFP_NOFS
;
229 set_sock_callbacks(sock
, con
);
231 dout("connect %s\n", pr_addr(&con
->peer_addr
.in_addr
));
233 ret
= sock
->ops
->connect(sock
, paddr
, sizeof(*paddr
), O_NONBLOCK
);
234 if (ret
== -EINPROGRESS
) {
235 dout("connect %s EINPROGRESS sk_state = %u\n",
236 pr_addr(&con
->peer_addr
.in_addr
),
241 pr_err("connect %s error %d\n",
242 pr_addr(&con
->peer_addr
.in_addr
), ret
);
245 con
->error_msg
= "connect error";
253 static int ceph_tcp_recvmsg(struct socket
*sock
, void *buf
, size_t len
)
255 struct kvec iov
= {buf
, len
};
256 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
258 return kernel_recvmsg(sock
, &msg
, &iov
, 1, len
, msg
.msg_flags
);
262 * write something. @more is true if caller will be sending more data
265 static int ceph_tcp_sendmsg(struct socket
*sock
, struct kvec
*iov
,
266 size_t kvlen
, size_t len
, int more
)
268 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
271 msg
.msg_flags
|= MSG_MORE
;
273 msg
.msg_flags
|= MSG_EOR
; /* superfluous, but what the hell */
275 return kernel_sendmsg(sock
, &msg
, iov
, kvlen
, len
);
280 * Shutdown/close the socket for the given connection.
282 static int con_close_socket(struct ceph_connection
*con
)
286 dout("con_close_socket on %p sock %p\n", con
, con
->sock
);
289 set_bit(SOCK_CLOSED
, &con
->state
);
290 rc
= con
->sock
->ops
->shutdown(con
->sock
, SHUT_RDWR
);
291 sock_release(con
->sock
);
293 clear_bit(SOCK_CLOSED
, &con
->state
);
298 * Reset a connection. Discard all incoming and outgoing messages
299 * and clear *_seq state.
301 static void ceph_msg_remove(struct ceph_msg
*msg
)
303 list_del_init(&msg
->list_head
);
306 static void ceph_msg_remove_list(struct list_head
*head
)
308 while (!list_empty(head
)) {
309 struct ceph_msg
*msg
= list_first_entry(head
, struct ceph_msg
,
311 ceph_msg_remove(msg
);
315 static void reset_connection(struct ceph_connection
*con
)
317 /* reset connection, out_queue, msg_ and connect_seq */
318 /* discard existing out_queue and msg_seq */
319 ceph_msg_remove_list(&con
->out_queue
);
320 ceph_msg_remove_list(&con
->out_sent
);
323 ceph_msg_put(con
->in_msg
);
327 con
->connect_seq
= 0;
330 ceph_msg_put(con
->out_msg
);
337 * mark a peer down. drop any open connections.
339 void ceph_con_close(struct ceph_connection
*con
)
341 dout("con_close %p peer %s\n", con
, pr_addr(&con
->peer_addr
.in_addr
));
342 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
343 clear_bit(STANDBY
, &con
->state
); /* avoid connect_seq bump */
344 mutex_lock(&con
->mutex
);
345 reset_connection(con
);
346 mutex_unlock(&con
->mutex
);
351 * Reopen a closed connection, with a new peer address.
353 void ceph_con_open(struct ceph_connection
*con
, struct ceph_entity_addr
*addr
)
355 dout("con_open %p %s\n", con
, pr_addr(&addr
->in_addr
));
356 set_bit(OPENING
, &con
->state
);
357 clear_bit(CLOSED
, &con
->state
);
358 memcpy(&con
->peer_addr
, addr
, sizeof(*addr
));
359 con
->delay
= 0; /* reset backoff memory */
366 struct ceph_connection
*ceph_con_get(struct ceph_connection
*con
)
368 dout("con_get %p nref = %d -> %d\n", con
,
369 atomic_read(&con
->nref
), atomic_read(&con
->nref
) + 1);
370 if (atomic_inc_not_zero(&con
->nref
))
375 void ceph_con_put(struct ceph_connection
*con
)
377 dout("con_put %p nref = %d -> %d\n", con
,
378 atomic_read(&con
->nref
), atomic_read(&con
->nref
) - 1);
379 BUG_ON(atomic_read(&con
->nref
) == 0);
380 if (atomic_dec_and_test(&con
->nref
)) {
387 * initialize a new connection.
389 void ceph_con_init(struct ceph_messenger
*msgr
, struct ceph_connection
*con
)
391 dout("con_init %p\n", con
);
392 memset(con
, 0, sizeof(*con
));
393 atomic_set(&con
->nref
, 1);
395 mutex_init(&con
->mutex
);
396 INIT_LIST_HEAD(&con
->out_queue
);
397 INIT_LIST_HEAD(&con
->out_sent
);
398 INIT_DELAYED_WORK(&con
->work
, con_work
);
403 * We maintain a global counter to order connection attempts. Get
404 * a unique seq greater than @gt.
406 static u32
get_global_seq(struct ceph_messenger
*msgr
, u32 gt
)
410 spin_lock(&msgr
->global_seq_lock
);
411 if (msgr
->global_seq
< gt
)
412 msgr
->global_seq
= gt
;
413 ret
= ++msgr
->global_seq
;
414 spin_unlock(&msgr
->global_seq_lock
);
420 * Prepare footer for currently outgoing message, and finish things
421 * off. Assumes out_kvec* are already valid.. we just add on to the end.
423 static void prepare_write_message_footer(struct ceph_connection
*con
, int v
)
425 struct ceph_msg
*m
= con
->out_msg
;
427 dout("prepare_write_message_footer %p\n", con
);
428 con
->out_kvec_is_msg
= true;
429 con
->out_kvec
[v
].iov_base
= &m
->footer
;
430 con
->out_kvec
[v
].iov_len
= sizeof(m
->footer
);
431 con
->out_kvec_bytes
+= sizeof(m
->footer
);
432 con
->out_kvec_left
++;
433 con
->out_more
= m
->more_to_follow
;
434 con
->out_msg_done
= true;
438 * Prepare headers for the next outgoing message.
440 static void prepare_write_message(struct ceph_connection
*con
)
445 con
->out_kvec_bytes
= 0;
446 con
->out_kvec_is_msg
= true;
447 con
->out_msg_done
= false;
449 /* Sneak an ack in there first? If we can get it into the same
450 * TCP packet that's a good thing. */
451 if (con
->in_seq
> con
->in_seq_acked
) {
452 con
->in_seq_acked
= con
->in_seq
;
453 con
->out_kvec
[v
].iov_base
= &tag_ack
;
454 con
->out_kvec
[v
++].iov_len
= 1;
455 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
456 con
->out_kvec
[v
].iov_base
= &con
->out_temp_ack
;
457 con
->out_kvec
[v
++].iov_len
= sizeof(con
->out_temp_ack
);
458 con
->out_kvec_bytes
= 1 + sizeof(con
->out_temp_ack
);
461 m
= list_first_entry(&con
->out_queue
,
462 struct ceph_msg
, list_head
);
464 if (test_bit(LOSSYTX
, &con
->state
)) {
465 /* put message on sent list */
467 list_move_tail(&m
->list_head
, &con
->out_sent
);
469 list_del_init(&m
->list_head
);
472 m
->hdr
.seq
= cpu_to_le64(++con
->out_seq
);
474 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
475 m
, con
->out_seq
, le16_to_cpu(m
->hdr
.type
),
476 le32_to_cpu(m
->hdr
.front_len
), le32_to_cpu(m
->hdr
.middle_len
),
477 le32_to_cpu(m
->hdr
.data_len
),
479 BUG_ON(le32_to_cpu(m
->hdr
.front_len
) != m
->front
.iov_len
);
481 /* tag + hdr + front + middle */
482 con
->out_kvec
[v
].iov_base
= &tag_msg
;
483 con
->out_kvec
[v
++].iov_len
= 1;
484 con
->out_kvec
[v
].iov_base
= &m
->hdr
;
485 con
->out_kvec
[v
++].iov_len
= sizeof(m
->hdr
);
486 con
->out_kvec
[v
++] = m
->front
;
488 con
->out_kvec
[v
++] = m
->middle
->vec
;
489 con
->out_kvec_left
= v
;
490 con
->out_kvec_bytes
+= 1 + sizeof(m
->hdr
) + m
->front
.iov_len
+
491 (m
->middle
? m
->middle
->vec
.iov_len
: 0);
492 con
->out_kvec_cur
= con
->out_kvec
;
494 /* fill in crc (except data pages), footer */
495 con
->out_msg
->hdr
.crc
=
496 cpu_to_le32(crc32c(0, (void *)&m
->hdr
,
497 sizeof(m
->hdr
) - sizeof(m
->hdr
.crc
)));
498 con
->out_msg
->footer
.flags
= CEPH_MSG_FOOTER_COMPLETE
;
499 con
->out_msg
->footer
.front_crc
=
500 cpu_to_le32(crc32c(0, m
->front
.iov_base
, m
->front
.iov_len
));
502 con
->out_msg
->footer
.middle_crc
=
503 cpu_to_le32(crc32c(0, m
->middle
->vec
.iov_base
,
504 m
->middle
->vec
.iov_len
));
506 con
->out_msg
->footer
.middle_crc
= 0;
507 con
->out_msg
->footer
.data_crc
= 0;
508 dout("prepare_write_message front_crc %u data_crc %u\n",
509 le32_to_cpu(con
->out_msg
->footer
.front_crc
),
510 le32_to_cpu(con
->out_msg
->footer
.middle_crc
));
512 /* is there a data payload? */
513 if (le32_to_cpu(m
->hdr
.data_len
) > 0) {
514 /* initialize page iterator */
515 con
->out_msg_pos
.page
= 0;
516 con
->out_msg_pos
.page_pos
=
517 le16_to_cpu(m
->hdr
.data_off
) & ~PAGE_MASK
;
518 con
->out_msg_pos
.data_pos
= 0;
519 con
->out_msg_pos
.did_page_crc
= 0;
520 con
->out_more
= 1; /* data + footer will follow */
522 /* no, queue up footer too and be done */
523 prepare_write_message_footer(con
, v
);
526 set_bit(WRITE_PENDING
, &con
->state
);
532 static void prepare_write_ack(struct ceph_connection
*con
)
534 dout("prepare_write_ack %p %llu -> %llu\n", con
,
535 con
->in_seq_acked
, con
->in_seq
);
536 con
->in_seq_acked
= con
->in_seq
;
538 con
->out_kvec
[0].iov_base
= &tag_ack
;
539 con
->out_kvec
[0].iov_len
= 1;
540 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
541 con
->out_kvec
[1].iov_base
= &con
->out_temp_ack
;
542 con
->out_kvec
[1].iov_len
= sizeof(con
->out_temp_ack
);
543 con
->out_kvec_left
= 2;
544 con
->out_kvec_bytes
= 1 + sizeof(con
->out_temp_ack
);
545 con
->out_kvec_cur
= con
->out_kvec
;
546 con
->out_more
= 1; /* more will follow.. eventually.. */
547 set_bit(WRITE_PENDING
, &con
->state
);
551 * Prepare to write keepalive byte.
553 static void prepare_write_keepalive(struct ceph_connection
*con
)
555 dout("prepare_write_keepalive %p\n", con
);
556 con
->out_kvec
[0].iov_base
= &tag_keepalive
;
557 con
->out_kvec
[0].iov_len
= 1;
558 con
->out_kvec_left
= 1;
559 con
->out_kvec_bytes
= 1;
560 con
->out_kvec_cur
= con
->out_kvec
;
561 set_bit(WRITE_PENDING
, &con
->state
);
565 * Connection negotiation.
568 static void prepare_connect_authorizer(struct ceph_connection
*con
)
572 int auth_protocol
= 0;
574 mutex_unlock(&con
->mutex
);
575 if (con
->ops
->get_authorizer
)
576 con
->ops
->get_authorizer(con
, &auth_buf
, &auth_len
,
577 &auth_protocol
, &con
->auth_reply_buf
,
578 &con
->auth_reply_buf_len
,
580 mutex_lock(&con
->mutex
);
582 con
->out_connect
.authorizer_protocol
= cpu_to_le32(auth_protocol
);
583 con
->out_connect
.authorizer_len
= cpu_to_le32(auth_len
);
585 con
->out_kvec
[con
->out_kvec_left
].iov_base
= auth_buf
;
586 con
->out_kvec
[con
->out_kvec_left
].iov_len
= auth_len
;
587 con
->out_kvec_left
++;
588 con
->out_kvec_bytes
+= auth_len
;
592 * We connected to a peer and are saying hello.
594 static void prepare_write_banner(struct ceph_messenger
*msgr
,
595 struct ceph_connection
*con
)
597 int len
= strlen(CEPH_BANNER
);
599 con
->out_kvec
[0].iov_base
= CEPH_BANNER
;
600 con
->out_kvec
[0].iov_len
= len
;
601 con
->out_kvec
[1].iov_base
= &msgr
->my_enc_addr
;
602 con
->out_kvec
[1].iov_len
= sizeof(msgr
->my_enc_addr
);
603 con
->out_kvec_left
= 2;
604 con
->out_kvec_bytes
= len
+ sizeof(msgr
->my_enc_addr
);
605 con
->out_kvec_cur
= con
->out_kvec
;
607 set_bit(WRITE_PENDING
, &con
->state
);
610 static void prepare_write_connect(struct ceph_messenger
*msgr
,
611 struct ceph_connection
*con
,
614 unsigned global_seq
= get_global_seq(con
->msgr
, 0);
617 switch (con
->peer_name
.type
) {
618 case CEPH_ENTITY_TYPE_MON
:
619 proto
= CEPH_MONC_PROTOCOL
;
621 case CEPH_ENTITY_TYPE_OSD
:
622 proto
= CEPH_OSDC_PROTOCOL
;
624 case CEPH_ENTITY_TYPE_MDS
:
625 proto
= CEPH_MDSC_PROTOCOL
;
631 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con
,
632 con
->connect_seq
, global_seq
, proto
);
634 con
->out_connect
.features
= CEPH_FEATURE_SUPPORTED
;
635 con
->out_connect
.host_type
= cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT
);
636 con
->out_connect
.connect_seq
= cpu_to_le32(con
->connect_seq
);
637 con
->out_connect
.global_seq
= cpu_to_le32(global_seq
);
638 con
->out_connect
.protocol_version
= cpu_to_le32(proto
);
639 con
->out_connect
.flags
= 0;
642 con
->out_kvec_left
= 0;
643 con
->out_kvec_bytes
= 0;
645 con
->out_kvec
[con
->out_kvec_left
].iov_base
= &con
->out_connect
;
646 con
->out_kvec
[con
->out_kvec_left
].iov_len
= sizeof(con
->out_connect
);
647 con
->out_kvec_left
++;
648 con
->out_kvec_bytes
+= sizeof(con
->out_connect
);
649 con
->out_kvec_cur
= con
->out_kvec
;
651 set_bit(WRITE_PENDING
, &con
->state
);
653 prepare_connect_authorizer(con
);
658 * write as much of pending kvecs to the socket as we can.
660 * 0 -> socket full, but more to do
663 static int write_partial_kvec(struct ceph_connection
*con
)
667 dout("write_partial_kvec %p %d left\n", con
, con
->out_kvec_bytes
);
668 while (con
->out_kvec_bytes
> 0) {
669 ret
= ceph_tcp_sendmsg(con
->sock
, con
->out_kvec_cur
,
670 con
->out_kvec_left
, con
->out_kvec_bytes
,
674 con
->out_kvec_bytes
-= ret
;
675 if (con
->out_kvec_bytes
== 0)
678 if (ret
>= con
->out_kvec_cur
->iov_len
) {
679 ret
-= con
->out_kvec_cur
->iov_len
;
681 con
->out_kvec_left
--;
683 con
->out_kvec_cur
->iov_len
-= ret
;
684 con
->out_kvec_cur
->iov_base
+= ret
;
690 con
->out_kvec_left
= 0;
691 con
->out_kvec_is_msg
= false;
694 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con
,
695 con
->out_kvec_bytes
, con
->out_kvec_left
, ret
);
696 return ret
; /* done! */
700 * Write as much message data payload as we can. If we finish, queue
702 * 1 -> done, footer is now queued in out_kvec[].
703 * 0 -> socket full, but more to do
706 static int write_partial_msg_pages(struct ceph_connection
*con
)
708 struct ceph_msg
*msg
= con
->out_msg
;
709 unsigned data_len
= le32_to_cpu(msg
->hdr
.data_len
);
711 int crc
= con
->msgr
->nocrc
;
714 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
715 con
, con
->out_msg
, con
->out_msg_pos
.page
, con
->out_msg
->nr_pages
,
716 con
->out_msg_pos
.page_pos
);
718 while (con
->out_msg_pos
.page
< con
->out_msg
->nr_pages
) {
719 struct page
*page
= NULL
;
723 * if we are calculating the data crc (the default), we need
724 * to map the page. if our pages[] has been revoked, use the
728 page
= msg
->pages
[con
->out_msg_pos
.page
];
732 page
= con
->msgr
->zero_page
;
734 kaddr
= page_address(con
->msgr
->zero_page
);
736 len
= min((int)(PAGE_SIZE
- con
->out_msg_pos
.page_pos
),
737 (int)(data_len
- con
->out_msg_pos
.data_pos
));
738 if (crc
&& !con
->out_msg_pos
.did_page_crc
) {
739 void *base
= kaddr
+ con
->out_msg_pos
.page_pos
;
740 u32 tmpcrc
= le32_to_cpu(con
->out_msg
->footer
.data_crc
);
742 BUG_ON(kaddr
== NULL
);
743 con
->out_msg
->footer
.data_crc
=
744 cpu_to_le32(crc32c(tmpcrc
, base
, len
));
745 con
->out_msg_pos
.did_page_crc
= 1;
748 ret
= kernel_sendpage(con
->sock
, page
,
749 con
->out_msg_pos
.page_pos
, len
,
750 MSG_DONTWAIT
| MSG_NOSIGNAL
|
753 if (crc
&& msg
->pages
)
759 con
->out_msg_pos
.data_pos
+= ret
;
760 con
->out_msg_pos
.page_pos
+= ret
;
762 con
->out_msg_pos
.page_pos
= 0;
763 con
->out_msg_pos
.page
++;
764 con
->out_msg_pos
.did_page_crc
= 0;
768 dout("write_partial_msg_pages %p msg %p done\n", con
, msg
);
770 /* prepare and queue up footer, too */
772 con
->out_msg
->footer
.flags
|= CEPH_MSG_FOOTER_NOCRC
;
773 con
->out_kvec_bytes
= 0;
774 con
->out_kvec_left
= 0;
775 con
->out_kvec_cur
= con
->out_kvec
;
776 prepare_write_message_footer(con
, 0);
785 static int write_partial_skip(struct ceph_connection
*con
)
789 while (con
->out_skip
> 0) {
791 .iov_base
= page_address(con
->msgr
->zero_page
),
792 .iov_len
= min(con
->out_skip
, (int)PAGE_CACHE_SIZE
)
795 ret
= ceph_tcp_sendmsg(con
->sock
, &iov
, 1, iov
.iov_len
, 1);
798 con
->out_skip
-= ret
;
806 * Prepare to read connection handshake, or an ack.
808 static void prepare_read_banner(struct ceph_connection
*con
)
810 dout("prepare_read_banner %p\n", con
);
811 con
->in_base_pos
= 0;
814 static void prepare_read_connect(struct ceph_connection
*con
)
816 dout("prepare_read_connect %p\n", con
);
817 con
->in_base_pos
= 0;
820 static void prepare_read_connect_retry(struct ceph_connection
*con
)
822 dout("prepare_read_connect_retry %p\n", con
);
823 con
->in_base_pos
= strlen(CEPH_BANNER
) + sizeof(con
->actual_peer_addr
)
824 + sizeof(con
->peer_addr_for_me
);
827 static void prepare_read_ack(struct ceph_connection
*con
)
829 dout("prepare_read_ack %p\n", con
);
830 con
->in_base_pos
= 0;
833 static void prepare_read_tag(struct ceph_connection
*con
)
835 dout("prepare_read_tag %p\n", con
);
836 con
->in_base_pos
= 0;
837 con
->in_tag
= CEPH_MSGR_TAG_READY
;
841 * Prepare to read a message.
843 static int prepare_read_message(struct ceph_connection
*con
)
845 dout("prepare_read_message %p\n", con
);
846 BUG_ON(con
->in_msg
!= NULL
);
847 con
->in_base_pos
= 0;
848 con
->in_front_crc
= con
->in_middle_crc
= con
->in_data_crc
= 0;
853 static int read_partial(struct ceph_connection
*con
,
854 int *to
, int size
, void *object
)
857 while (con
->in_base_pos
< *to
) {
858 int left
= *to
- con
->in_base_pos
;
859 int have
= size
- left
;
860 int ret
= ceph_tcp_recvmsg(con
->sock
, object
+ have
, left
);
863 con
->in_base_pos
+= ret
;
870 * Read all or part of the connect-side handshake on a new connection
872 static int read_partial_banner(struct ceph_connection
*con
)
876 dout("read_partial_banner %p at %d\n", con
, con
->in_base_pos
);
879 ret
= read_partial(con
, &to
, strlen(CEPH_BANNER
), con
->in_banner
);
882 ret
= read_partial(con
, &to
, sizeof(con
->actual_peer_addr
),
883 &con
->actual_peer_addr
);
886 ret
= read_partial(con
, &to
, sizeof(con
->peer_addr_for_me
),
887 &con
->peer_addr_for_me
);
894 static int read_partial_connect(struct ceph_connection
*con
)
898 dout("read_partial_connect %p at %d\n", con
, con
->in_base_pos
);
900 ret
= read_partial(con
, &to
, sizeof(con
->in_reply
), &con
->in_reply
);
903 ret
= read_partial(con
, &to
, le32_to_cpu(con
->in_reply
.authorizer_len
),
904 con
->auth_reply_buf
);
908 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
909 con
, (int)con
->in_reply
.tag
,
910 le32_to_cpu(con
->in_reply
.connect_seq
),
911 le32_to_cpu(con
->in_reply
.global_seq
));
918 * Verify the hello banner looks okay.
920 static int verify_hello(struct ceph_connection
*con
)
922 if (memcmp(con
->in_banner
, CEPH_BANNER
, strlen(CEPH_BANNER
))) {
923 pr_err("connect to %s got bad banner\n",
924 pr_addr(&con
->peer_addr
.in_addr
));
925 con
->error_msg
= "protocol error, bad banner";
931 static bool addr_is_blank(struct sockaddr_storage
*ss
)
933 switch (ss
->ss_family
) {
935 return ((struct sockaddr_in
*)ss
)->sin_addr
.s_addr
== 0;
938 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[0] == 0 &&
939 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[1] == 0 &&
940 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[2] == 0 &&
941 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[3] == 0;
946 static int addr_port(struct sockaddr_storage
*ss
)
948 switch (ss
->ss_family
) {
950 return ntohs(((struct sockaddr_in
*)ss
)->sin_port
);
952 return ntohs(((struct sockaddr_in6
*)ss
)->sin6_port
);
957 static void addr_set_port(struct sockaddr_storage
*ss
, int p
)
959 switch (ss
->ss_family
) {
961 ((struct sockaddr_in
*)ss
)->sin_port
= htons(p
);
963 ((struct sockaddr_in6
*)ss
)->sin6_port
= htons(p
);
968 * Parse an ip[:port] list into an addr array. Use the default
969 * monitor port if a port isn't specified.
971 int ceph_parse_ips(const char *c
, const char *end
,
972 struct ceph_entity_addr
*addr
,
973 int max_count
, int *count
)
978 dout("parse_ips on '%.*s'\n", (int)(end
-c
), c
);
979 for (i
= 0; i
< max_count
; i
++) {
981 struct sockaddr_storage
*ss
= &addr
[i
].in_addr
;
982 struct sockaddr_in
*in4
= (void *)ss
;
983 struct sockaddr_in6
*in6
= (void *)ss
;
986 memset(ss
, 0, sizeof(*ss
));
987 if (in4_pton(p
, end
- p
, (u8
*)&in4
->sin_addr
.s_addr
,
989 ss
->ss_family
= AF_INET
;
990 } else if (in6_pton(p
, end
- p
, (u8
*)&in6
->sin6_addr
.s6_addr
,
992 ss
->ss_family
= AF_INET6
;
999 if (p
< end
&& *p
== ':') {
1002 while (p
< end
&& *p
>= '0' && *p
<= '9') {
1003 port
= (port
* 10) + (*p
- '0');
1006 if (port
> 65535 || port
== 0)
1009 port
= CEPH_MON_PORT
;
1012 addr_set_port(ss
, port
);
1014 dout("parse_ips got %s\n", pr_addr(ss
));
1031 pr_err("parse_ips bad ip '%s'\n", c
);
1035 static int process_banner(struct ceph_connection
*con
)
1037 dout("process_banner on %p\n", con
);
1039 if (verify_hello(con
) < 0)
1042 ceph_decode_addr(&con
->actual_peer_addr
);
1043 ceph_decode_addr(&con
->peer_addr_for_me
);
1046 * Make sure the other end is who we wanted. note that the other
1047 * end may not yet know their ip address, so if it's 0.0.0.0, give
1048 * them the benefit of the doubt.
1050 if (!ceph_entity_addr_is_local(&con
->peer_addr
,
1051 &con
->actual_peer_addr
) &&
1052 !(addr_is_blank(&con
->actual_peer_addr
.in_addr
) &&
1053 con
->actual_peer_addr
.nonce
== con
->peer_addr
.nonce
)) {
1054 pr_err("wrong peer, want %s/%d, "
1056 pr_addr(&con
->peer_addr
.in_addr
),
1057 con
->peer_addr
.nonce
,
1058 pr_addr(&con
->actual_peer_addr
.in_addr
),
1059 con
->actual_peer_addr
.nonce
);
1060 con
->error_msg
= "protocol error, wrong peer";
1065 * did we learn our address?
1067 if (addr_is_blank(&con
->msgr
->inst
.addr
.in_addr
)) {
1068 int port
= addr_port(&con
->msgr
->inst
.addr
.in_addr
);
1070 memcpy(&con
->msgr
->inst
.addr
.in_addr
,
1071 &con
->peer_addr_for_me
.in_addr
,
1072 sizeof(con
->peer_addr_for_me
.in_addr
));
1073 addr_set_port(&con
->msgr
->inst
.addr
.in_addr
, port
);
1074 encode_my_addr(con
->msgr
);
1075 dout("process_banner learned my addr is %s\n",
1076 pr_addr(&con
->msgr
->inst
.addr
.in_addr
));
1079 set_bit(NEGOTIATING
, &con
->state
);
1080 prepare_read_connect(con
);
1084 static void fail_protocol(struct ceph_connection
*con
)
1086 reset_connection(con
);
1087 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
1089 mutex_unlock(&con
->mutex
);
1090 if (con
->ops
->bad_proto
)
1091 con
->ops
->bad_proto(con
);
1092 mutex_lock(&con
->mutex
);
1095 static int process_connect(struct ceph_connection
*con
)
1097 u64 sup_feat
= CEPH_FEATURE_SUPPORTED
;
1098 u64 req_feat
= CEPH_FEATURE_REQUIRED
;
1099 u64 server_feat
= le64_to_cpu(con
->in_reply
.features
);
1101 dout("process_connect on %p tag %d\n", con
, (int)con
->in_tag
);
1103 switch (con
->in_reply
.tag
) {
1104 case CEPH_MSGR_TAG_FEATURES
:
1105 pr_err("%s%lld %s feature set mismatch,"
1106 " my %llx < server's %llx, missing %llx\n",
1107 ENTITY_NAME(con
->peer_name
),
1108 pr_addr(&con
->peer_addr
.in_addr
),
1109 sup_feat
, server_feat
, server_feat
& ~sup_feat
);
1110 con
->error_msg
= "missing required protocol features";
1114 case CEPH_MSGR_TAG_BADPROTOVER
:
1115 pr_err("%s%lld %s protocol version mismatch,"
1116 " my %d != server's %d\n",
1117 ENTITY_NAME(con
->peer_name
),
1118 pr_addr(&con
->peer_addr
.in_addr
),
1119 le32_to_cpu(con
->out_connect
.protocol_version
),
1120 le32_to_cpu(con
->in_reply
.protocol_version
));
1121 con
->error_msg
= "protocol version mismatch";
1125 case CEPH_MSGR_TAG_BADAUTHORIZER
:
1127 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con
,
1129 if (con
->auth_retry
== 2) {
1130 con
->error_msg
= "connect authorization failure";
1131 reset_connection(con
);
1132 set_bit(CLOSED
, &con
->state
);
1135 con
->auth_retry
= 1;
1136 prepare_write_connect(con
->msgr
, con
, 0);
1137 prepare_read_connect_retry(con
);
1140 case CEPH_MSGR_TAG_RESETSESSION
:
1142 * If we connected with a large connect_seq but the peer
1143 * has no record of a session with us (no connection, or
1144 * connect_seq == 0), they will send RESETSESION to indicate
1145 * that they must have reset their session, and may have
1148 dout("process_connect got RESET peer seq %u\n",
1149 le32_to_cpu(con
->in_connect
.connect_seq
));
1150 pr_err("%s%lld %s connection reset\n",
1151 ENTITY_NAME(con
->peer_name
),
1152 pr_addr(&con
->peer_addr
.in_addr
));
1153 reset_connection(con
);
1154 prepare_write_connect(con
->msgr
, con
, 0);
1155 prepare_read_connect(con
);
1157 /* Tell ceph about it. */
1158 mutex_unlock(&con
->mutex
);
1159 pr_info("reset on %s%lld\n", ENTITY_NAME(con
->peer_name
));
1160 if (con
->ops
->peer_reset
)
1161 con
->ops
->peer_reset(con
);
1162 mutex_lock(&con
->mutex
);
1165 case CEPH_MSGR_TAG_RETRY_SESSION
:
1167 * If we sent a smaller connect_seq than the peer has, try
1168 * again with a larger value.
1170 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1171 le32_to_cpu(con
->out_connect
.connect_seq
),
1172 le32_to_cpu(con
->in_connect
.connect_seq
));
1173 con
->connect_seq
= le32_to_cpu(con
->in_connect
.connect_seq
);
1174 prepare_write_connect(con
->msgr
, con
, 0);
1175 prepare_read_connect(con
);
1178 case CEPH_MSGR_TAG_RETRY_GLOBAL
:
1180 * If we sent a smaller global_seq than the peer has, try
1181 * again with a larger value.
1183 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
1184 con
->peer_global_seq
,
1185 le32_to_cpu(con
->in_connect
.global_seq
));
1186 get_global_seq(con
->msgr
,
1187 le32_to_cpu(con
->in_connect
.global_seq
));
1188 prepare_write_connect(con
->msgr
, con
, 0);
1189 prepare_read_connect(con
);
1192 case CEPH_MSGR_TAG_READY
:
1193 if (req_feat
& ~server_feat
) {
1194 pr_err("%s%lld %s protocol feature mismatch,"
1195 " my required %llx > server's %llx, need %llx\n",
1196 ENTITY_NAME(con
->peer_name
),
1197 pr_addr(&con
->peer_addr
.in_addr
),
1198 req_feat
, server_feat
, req_feat
& ~server_feat
);
1199 con
->error_msg
= "missing required protocol features";
1203 clear_bit(CONNECTING
, &con
->state
);
1204 con
->peer_global_seq
= le32_to_cpu(con
->in_reply
.global_seq
);
1206 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1207 con
->peer_global_seq
,
1208 le32_to_cpu(con
->in_reply
.connect_seq
),
1210 WARN_ON(con
->connect_seq
!=
1211 le32_to_cpu(con
->in_reply
.connect_seq
));
1213 if (con
->in_reply
.flags
& CEPH_MSG_CONNECT_LOSSY
)
1214 set_bit(LOSSYTX
, &con
->state
);
1216 prepare_read_tag(con
);
1219 case CEPH_MSGR_TAG_WAIT
:
1221 * If there is a connection race (we are opening
1222 * connections to each other), one of us may just have
1223 * to WAIT. This shouldn't happen if we are the
1226 pr_err("process_connect peer connecting WAIT\n");
1229 pr_err("connect protocol error, will retry\n");
1230 con
->error_msg
= "protocol error, garbage tag during connect";
1238 * read (part of) an ack
1240 static int read_partial_ack(struct ceph_connection
*con
)
1244 return read_partial(con
, &to
, sizeof(con
->in_temp_ack
),
1250 * We can finally discard anything that's been acked.
1252 static void process_ack(struct ceph_connection
*con
)
1255 u64 ack
= le64_to_cpu(con
->in_temp_ack
);
1258 while (!list_empty(&con
->out_sent
)) {
1259 m
= list_first_entry(&con
->out_sent
, struct ceph_msg
,
1261 seq
= le64_to_cpu(m
->hdr
.seq
);
1264 dout("got ack for seq %llu type %d at %p\n", seq
,
1265 le16_to_cpu(m
->hdr
.type
), m
);
1268 prepare_read_tag(con
);
1277 * read (part of) a message.
1279 static int read_partial_message(struct ceph_connection
*con
)
1281 struct ceph_msg
*m
= con
->in_msg
;
1285 unsigned front_len
, middle_len
, data_len
, data_off
;
1286 int datacrc
= con
->msgr
->nocrc
;
1288 dout("read_partial_message con %p msg %p\n", con
, m
);
1291 while (con
->in_base_pos
< sizeof(con
->in_hdr
)) {
1292 left
= sizeof(con
->in_hdr
) - con
->in_base_pos
;
1293 ret
= ceph_tcp_recvmsg(con
->sock
,
1294 (char *)&con
->in_hdr
+ con
->in_base_pos
,
1298 con
->in_base_pos
+= ret
;
1299 if (con
->in_base_pos
== sizeof(con
->in_hdr
)) {
1300 u32 crc
= crc32c(0, (void *)&con
->in_hdr
,
1301 sizeof(con
->in_hdr
) - sizeof(con
->in_hdr
.crc
));
1302 if (crc
!= le32_to_cpu(con
->in_hdr
.crc
)) {
1303 pr_err("read_partial_message bad hdr "
1304 " crc %u != expected %u\n",
1305 crc
, con
->in_hdr
.crc
);
1311 front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
1312 if (front_len
> CEPH_MSG_MAX_FRONT_LEN
)
1314 middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
1315 if (middle_len
> CEPH_MSG_MAX_DATA_LEN
)
1317 data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
1318 if (data_len
> CEPH_MSG_MAX_DATA_LEN
)
1321 /* allocate message? */
1323 dout("got hdr type %d front %d data %d\n", con
->in_hdr
.type
,
1324 con
->in_hdr
.front_len
, con
->in_hdr
.data_len
);
1325 con
->in_msg
= con
->ops
->alloc_msg(con
, &con
->in_hdr
);
1327 /* skip this message */
1328 pr_err("alloc_msg returned NULL, skipping message\n");
1329 con
->in_base_pos
= -front_len
- middle_len
- data_len
-
1331 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1334 if (IS_ERR(con
->in_msg
)) {
1335 ret
= PTR_ERR(con
->in_msg
);
1337 con
->error_msg
= "out of memory for incoming message";
1341 m
->front
.iov_len
= 0; /* haven't read it yet */
1342 memcpy(&m
->hdr
, &con
->in_hdr
, sizeof(con
->in_hdr
));
1346 while (m
->front
.iov_len
< front_len
) {
1347 BUG_ON(m
->front
.iov_base
== NULL
);
1348 left
= front_len
- m
->front
.iov_len
;
1349 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)m
->front
.iov_base
+
1350 m
->front
.iov_len
, left
);
1353 m
->front
.iov_len
+= ret
;
1354 if (m
->front
.iov_len
== front_len
)
1355 con
->in_front_crc
= crc32c(0, m
->front
.iov_base
,
1360 while (middle_len
> 0 && (!m
->middle
||
1361 m
->middle
->vec
.iov_len
< middle_len
)) {
1362 if (m
->middle
== NULL
) {
1364 if (con
->ops
->alloc_middle
)
1365 ret
= con
->ops
->alloc_middle(con
, m
);
1367 pr_err("alloc_middle fail skipping payload\n");
1368 con
->in_base_pos
= -middle_len
- data_len
1369 - sizeof(m
->footer
);
1370 ceph_msg_put(con
->in_msg
);
1372 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1375 m
->middle
->vec
.iov_len
= 0;
1377 left
= middle_len
- m
->middle
->vec
.iov_len
;
1378 ret
= ceph_tcp_recvmsg(con
->sock
,
1379 (char *)m
->middle
->vec
.iov_base
+
1380 m
->middle
->vec
.iov_len
, left
);
1383 m
->middle
->vec
.iov_len
+= ret
;
1384 if (m
->middle
->vec
.iov_len
== middle_len
)
1385 con
->in_middle_crc
= crc32c(0, m
->middle
->vec
.iov_base
,
1386 m
->middle
->vec
.iov_len
);
1390 data_off
= le16_to_cpu(m
->hdr
.data_off
);
1394 if (m
->nr_pages
== 0) {
1395 con
->in_msg_pos
.page
= 0;
1396 con
->in_msg_pos
.page_pos
= data_off
& ~PAGE_MASK
;
1397 con
->in_msg_pos
.data_pos
= 0;
1398 /* find pages for data payload */
1399 want
= calc_pages_for(data_off
& ~PAGE_MASK
, data_len
);
1401 mutex_unlock(&con
->mutex
);
1402 if (con
->ops
->prepare_pages
)
1403 ret
= con
->ops
->prepare_pages(con
, m
, want
);
1404 mutex_lock(&con
->mutex
);
1406 dout("%p prepare_pages failed, skipping payload\n", m
);
1407 con
->in_base_pos
= -data_len
- sizeof(m
->footer
);
1408 ceph_msg_put(con
->in_msg
);
1410 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1413 BUG_ON(m
->nr_pages
< want
);
1415 while (con
->in_msg_pos
.data_pos
< data_len
) {
1416 left
= min((int)(data_len
- con
->in_msg_pos
.data_pos
),
1417 (int)(PAGE_SIZE
- con
->in_msg_pos
.page_pos
));
1418 BUG_ON(m
->pages
== NULL
);
1419 p
= kmap(m
->pages
[con
->in_msg_pos
.page
]);
1420 ret
= ceph_tcp_recvmsg(con
->sock
, p
+ con
->in_msg_pos
.page_pos
,
1422 if (ret
> 0 && datacrc
)
1424 crc32c(con
->in_data_crc
,
1425 p
+ con
->in_msg_pos
.page_pos
, ret
);
1426 kunmap(m
->pages
[con
->in_msg_pos
.page
]);
1429 con
->in_msg_pos
.data_pos
+= ret
;
1430 con
->in_msg_pos
.page_pos
+= ret
;
1431 if (con
->in_msg_pos
.page_pos
== PAGE_SIZE
) {
1432 con
->in_msg_pos
.page_pos
= 0;
1433 con
->in_msg_pos
.page
++;
1439 to
= sizeof(m
->hdr
) + sizeof(m
->footer
);
1440 while (con
->in_base_pos
< to
) {
1441 left
= to
- con
->in_base_pos
;
1442 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)&m
->footer
+
1443 (con
->in_base_pos
- sizeof(m
->hdr
)),
1447 con
->in_base_pos
+= ret
;
1449 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1450 m
, front_len
, m
->footer
.front_crc
, middle_len
,
1451 m
->footer
.middle_crc
, data_len
, m
->footer
.data_crc
);
1454 if (con
->in_front_crc
!= le32_to_cpu(m
->footer
.front_crc
)) {
1455 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1456 m
, con
->in_front_crc
, m
->footer
.front_crc
);
1459 if (con
->in_middle_crc
!= le32_to_cpu(m
->footer
.middle_crc
)) {
1460 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1461 m
, con
->in_middle_crc
, m
->footer
.middle_crc
);
1465 (m
->footer
.flags
& CEPH_MSG_FOOTER_NOCRC
) == 0 &&
1466 con
->in_data_crc
!= le32_to_cpu(m
->footer
.data_crc
)) {
1467 pr_err("read_partial_message %p data crc %u != exp. %u\n", m
,
1468 con
->in_data_crc
, le32_to_cpu(m
->footer
.data_crc
));
1472 return 1; /* done! */
1476 * Process message. This happens in the worker thread. The callback should
1477 * be careful not to do anything that waits on other incoming messages or it
1480 static void process_message(struct ceph_connection
*con
)
1482 struct ceph_msg
*msg
;
1487 /* if first message, set peer_name */
1488 if (con
->peer_name
.type
== 0)
1489 con
->peer_name
= msg
->hdr
.src
.name
;
1492 mutex_unlock(&con
->mutex
);
1494 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1495 msg
, le64_to_cpu(msg
->hdr
.seq
),
1496 ENTITY_NAME(msg
->hdr
.src
.name
),
1497 le16_to_cpu(msg
->hdr
.type
),
1498 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
1499 le32_to_cpu(msg
->hdr
.front_len
),
1500 le32_to_cpu(msg
->hdr
.data_len
),
1501 con
->in_front_crc
, con
->in_middle_crc
, con
->in_data_crc
);
1502 con
->ops
->dispatch(con
, msg
);
1504 mutex_lock(&con
->mutex
);
1505 prepare_read_tag(con
);
1510 * Write something to the socket. Called in a worker thread when the
1511 * socket appears to be writeable and we have something ready to send.
1513 static int try_write(struct ceph_connection
*con
)
1515 struct ceph_messenger
*msgr
= con
->msgr
;
1518 dout("try_write start %p state %lu nref %d\n", con
, con
->state
,
1519 atomic_read(&con
->nref
));
1521 mutex_lock(&con
->mutex
);
1523 dout("try_write out_kvec_bytes %d\n", con
->out_kvec_bytes
);
1525 /* open the socket first? */
1526 if (con
->sock
== NULL
) {
1528 * if we were STANDBY and are reconnecting _this_
1529 * connection, bump connect_seq now. Always bump
1532 if (test_and_clear_bit(STANDBY
, &con
->state
))
1535 prepare_write_banner(msgr
, con
);
1536 prepare_write_connect(msgr
, con
, 1);
1537 prepare_read_banner(con
);
1538 set_bit(CONNECTING
, &con
->state
);
1539 clear_bit(NEGOTIATING
, &con
->state
);
1541 BUG_ON(con
->in_msg
);
1542 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1543 dout("try_write initiating connect on %p new state %lu\n",
1545 con
->sock
= ceph_tcp_connect(con
);
1546 if (IS_ERR(con
->sock
)) {
1548 con
->error_msg
= "connect error";
1555 /* kvec data queued? */
1556 if (con
->out_skip
) {
1557 ret
= write_partial_skip(con
);
1561 dout("try_write write_partial_skip err %d\n", ret
);
1565 if (con
->out_kvec_left
) {
1566 ret
= write_partial_kvec(con
);
1573 if (con
->out_msg_done
) {
1574 ceph_msg_put(con
->out_msg
);
1575 con
->out_msg
= NULL
; /* we're done with this one */
1579 ret
= write_partial_msg_pages(con
);
1581 goto more_kvec
; /* we need to send the footer, too! */
1585 dout("try_write write_partial_msg_pages err %d\n",
1592 if (!test_bit(CONNECTING
, &con
->state
)) {
1593 /* is anything else pending? */
1594 if (!list_empty(&con
->out_queue
)) {
1595 prepare_write_message(con
);
1598 if (con
->in_seq
> con
->in_seq_acked
) {
1599 prepare_write_ack(con
);
1602 if (test_and_clear_bit(KEEPALIVE_PENDING
, &con
->state
)) {
1603 prepare_write_keepalive(con
);
1608 /* Nothing to do! */
1609 clear_bit(WRITE_PENDING
, &con
->state
);
1610 dout("try_write nothing else to write.\n");
1614 mutex_unlock(&con
->mutex
);
1615 dout("try_write done on %p\n", con
);
1622 * Read what we can from the socket.
1624 static int try_read(struct ceph_connection
*con
)
1626 struct ceph_messenger
*msgr
;
1632 if (test_bit(STANDBY
, &con
->state
))
1635 dout("try_read start on %p\n", con
);
1638 mutex_lock(&con
->mutex
);
1641 dout("try_read tag %d in_base_pos %d\n", (int)con
->in_tag
,
1643 if (test_bit(CONNECTING
, &con
->state
)) {
1644 if (!test_bit(NEGOTIATING
, &con
->state
)) {
1645 dout("try_read connecting\n");
1646 ret
= read_partial_banner(con
);
1649 if (process_banner(con
) < 0) {
1654 ret
= read_partial_connect(con
);
1657 if (process_connect(con
) < 0) {
1664 if (con
->in_base_pos
< 0) {
1666 * skipping + discarding content.
1668 * FIXME: there must be a better way to do this!
1670 static char buf
[1024];
1671 int skip
= min(1024, -con
->in_base_pos
);
1672 dout("skipping %d / %d bytes\n", skip
, -con
->in_base_pos
);
1673 ret
= ceph_tcp_recvmsg(con
->sock
, buf
, skip
);
1676 con
->in_base_pos
+= ret
;
1677 if (con
->in_base_pos
)
1680 if (con
->in_tag
== CEPH_MSGR_TAG_READY
) {
1684 ret
= ceph_tcp_recvmsg(con
->sock
, &con
->in_tag
, 1);
1687 dout("try_read got tag %d\n", (int)con
->in_tag
);
1688 switch (con
->in_tag
) {
1689 case CEPH_MSGR_TAG_MSG
:
1690 prepare_read_message(con
);
1692 case CEPH_MSGR_TAG_ACK
:
1693 prepare_read_ack(con
);
1695 case CEPH_MSGR_TAG_CLOSE
:
1696 set_bit(CLOSED
, &con
->state
); /* fixme */
1702 if (con
->in_tag
== CEPH_MSGR_TAG_MSG
) {
1703 ret
= read_partial_message(con
);
1707 con
->error_msg
= "bad crc";
1711 con
->error_msg
= "io error";
1717 if (con
->in_tag
== CEPH_MSGR_TAG_READY
)
1719 process_message(con
);
1722 if (con
->in_tag
== CEPH_MSGR_TAG_ACK
) {
1723 ret
= read_partial_ack(con
);
1733 mutex_unlock(&con
->mutex
);
1734 dout("try_read done on %p\n", con
);
1738 pr_err("try_read bad con->in_tag = %d\n", (int)con
->in_tag
);
1739 con
->error_msg
= "protocol error, garbage tag";
1746 * Atomically queue work on a connection. Bump @con reference to
1747 * avoid races with connection teardown.
1749 * There is some trickery going on with QUEUED and BUSY because we
1750 * only want a _single_ thread operating on each connection at any
1751 * point in time, but we want to use all available CPUs.
1753 * The worker thread only proceeds if it can atomically set BUSY. It
1754 * clears QUEUED and does it's thing. When it thinks it's done, it
1755 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1756 * (tries again to set BUSY).
1758 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1759 * try to queue work. If that fails (work is already queued, or BUSY)
1760 * we give up (work also already being done or is queued) but leave QUEUED
1761 * set so that the worker thread will loop if necessary.
1763 static void queue_con(struct ceph_connection
*con
)
1765 if (test_bit(DEAD
, &con
->state
)) {
1766 dout("queue_con %p ignoring: DEAD\n",
1771 if (!con
->ops
->get(con
)) {
1772 dout("queue_con %p ref count 0\n", con
);
1776 set_bit(QUEUED
, &con
->state
);
1777 if (test_bit(BUSY
, &con
->state
)) {
1778 dout("queue_con %p - already BUSY\n", con
);
1780 } else if (!queue_work(ceph_msgr_wq
, &con
->work
.work
)) {
1781 dout("queue_con %p - already queued\n", con
);
1784 dout("queue_con %p\n", con
);
1789 * Do some work on a connection. Drop a connection ref when we're done.
1791 static void con_work(struct work_struct
*work
)
1793 struct ceph_connection
*con
= container_of(work
, struct ceph_connection
,
1798 if (test_and_set_bit(BUSY
, &con
->state
) != 0) {
1799 dout("con_work %p BUSY already set\n", con
);
1802 dout("con_work %p start, clearing QUEUED\n", con
);
1803 clear_bit(QUEUED
, &con
->state
);
1805 if (test_bit(CLOSED
, &con
->state
)) { /* e.g. if we are replaced */
1806 dout("con_work CLOSED\n");
1807 con_close_socket(con
);
1810 if (test_and_clear_bit(OPENING
, &con
->state
)) {
1811 /* reopen w/ new peer */
1812 dout("con_work OPENING\n");
1813 con_close_socket(con
);
1816 if (test_and_clear_bit(SOCK_CLOSED
, &con
->state
) ||
1817 try_read(con
) < 0 ||
1818 try_write(con
) < 0) {
1820 ceph_fault(con
); /* error/fault path */
1824 clear_bit(BUSY
, &con
->state
);
1825 dout("con->state=%lu\n", con
->state
);
1826 if (test_bit(QUEUED
, &con
->state
)) {
1828 dout("con_work %p QUEUED reset, looping\n", con
);
1831 dout("con_work %p QUEUED reset, but just faulted\n", con
);
1832 clear_bit(QUEUED
, &con
->state
);
1834 dout("con_work %p done\n", con
);
1842 * Generic error/fault handler. A retry mechanism is used with
1843 * exponential backoff
1845 static void ceph_fault(struct ceph_connection
*con
)
1847 pr_err("%s%lld %s %s\n", ENTITY_NAME(con
->peer_name
),
1848 pr_addr(&con
->peer_addr
.in_addr
), con
->error_msg
);
1849 dout("fault %p state %lu to peer %s\n",
1850 con
, con
->state
, pr_addr(&con
->peer_addr
.in_addr
));
1852 if (test_bit(LOSSYTX
, &con
->state
)) {
1853 dout("fault on LOSSYTX channel\n");
1857 clear_bit(BUSY
, &con
->state
); /* to avoid an improbable race */
1859 mutex_lock(&con
->mutex
);
1861 con_close_socket(con
);
1864 ceph_msg_put(con
->in_msg
);
1868 /* If there are no messages in the queue, place the connection
1869 * in a STANDBY state (i.e., don't try to reconnect just yet). */
1870 if (list_empty(&con
->out_queue
) && !con
->out_keepalive_pending
) {
1871 dout("fault setting STANDBY\n");
1872 set_bit(STANDBY
, &con
->state
);
1873 mutex_unlock(&con
->mutex
);
1877 /* Requeue anything that hasn't been acked, and retry after a
1879 list_splice_init(&con
->out_sent
, &con
->out_queue
);
1881 if (con
->delay
== 0)
1882 con
->delay
= BASE_DELAY_INTERVAL
;
1883 else if (con
->delay
< MAX_DELAY_INTERVAL
)
1886 mutex_unlock(&con
->mutex
);
1888 /* explicitly schedule work to try to reconnect again later. */
1889 dout("fault queueing %p delay %lu\n", con
, con
->delay
);
1891 if (queue_delayed_work(ceph_msgr_wq
, &con
->work
,
1892 round_jiffies_relative(con
->delay
)) == 0)
1896 if (con
->ops
->fault
)
1897 con
->ops
->fault(con
);
1903 * create a new messenger instance
1905 struct ceph_messenger
*ceph_messenger_create(struct ceph_entity_addr
*myaddr
)
1907 struct ceph_messenger
*msgr
;
1909 msgr
= kzalloc(sizeof(*msgr
), GFP_KERNEL
);
1911 return ERR_PTR(-ENOMEM
);
1913 spin_lock_init(&msgr
->global_seq_lock
);
1915 /* the zero page is needed if a request is "canceled" while the message
1916 * is being written over the socket */
1917 msgr
->zero_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
1918 if (!msgr
->zero_page
) {
1920 return ERR_PTR(-ENOMEM
);
1922 kmap(msgr
->zero_page
);
1925 msgr
->inst
.addr
= *myaddr
;
1927 /* select a random nonce */
1928 get_random_bytes(&msgr
->inst
.addr
.nonce
,
1929 sizeof(msgr
->inst
.addr
.nonce
));
1930 encode_my_addr(msgr
);
1932 dout("messenger_create %p\n", msgr
);
1936 void ceph_messenger_destroy(struct ceph_messenger
*msgr
)
1938 dout("destroy %p\n", msgr
);
1939 kunmap(msgr
->zero_page
);
1940 __free_page(msgr
->zero_page
);
1942 dout("destroyed messenger %p\n", msgr
);
1946 * Queue up an outgoing message on the given connection.
1948 void ceph_con_send(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1950 if (test_bit(CLOSED
, &con
->state
)) {
1951 dout("con_send %p closed, dropping %p\n", con
, msg
);
1957 msg
->hdr
.src
.name
= con
->msgr
->inst
.name
;
1958 msg
->hdr
.src
.addr
= con
->msgr
->my_enc_addr
;
1959 msg
->hdr
.orig_src
= msg
->hdr
.src
;
1960 msg
->hdr
.dst_erank
= con
->peer_addr
.erank
;
1963 mutex_lock(&con
->mutex
);
1964 BUG_ON(!list_empty(&msg
->list_head
));
1965 list_add_tail(&msg
->list_head
, &con
->out_queue
);
1966 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg
,
1967 ENTITY_NAME(con
->peer_name
), le16_to_cpu(msg
->hdr
.type
),
1968 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
1969 le32_to_cpu(msg
->hdr
.front_len
),
1970 le32_to_cpu(msg
->hdr
.middle_len
),
1971 le32_to_cpu(msg
->hdr
.data_len
));
1972 mutex_unlock(&con
->mutex
);
1974 /* if there wasn't anything waiting to send before, queue
1976 if (test_and_set_bit(WRITE_PENDING
, &con
->state
) == 0)
1981 * Revoke a message that was previously queued for send
1983 void ceph_con_revoke(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1985 mutex_lock(&con
->mutex
);
1986 if (!list_empty(&msg
->list_head
)) {
1987 dout("con_revoke %p msg %p\n", con
, msg
);
1988 list_del_init(&msg
->list_head
);
1991 if (con
->out_msg
== msg
) {
1992 ceph_msg_put(con
->out_msg
);
1993 con
->out_msg
= NULL
;
1995 if (con
->out_kvec_is_msg
) {
1996 con
->out_skip
= con
->out_kvec_bytes
;
1997 con
->out_kvec_is_msg
= false;
2000 dout("con_revoke %p msg %p - not queued (sent?)\n", con
, msg
);
2002 mutex_unlock(&con
->mutex
);
2006 * Revoke a page vector that we may be reading data into
2008 void ceph_con_revoke_pages(struct ceph_connection
*con
, struct page
**pages
)
2010 mutex_lock(&con
->mutex
);
2011 if (con
->in_msg
&& con
->in_msg
->pages
== pages
) {
2012 unsigned data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
2014 /* skip rest of message */
2015 dout("con_revoke_pages %p msg %p pages %p revoked\n", con
,
2016 con
->in_msg
, pages
);
2017 if (con
->in_msg_pos
.data_pos
< data_len
)
2018 con
->in_base_pos
= con
->in_msg_pos
.data_pos
- data_len
;
2020 con
->in_base_pos
= con
->in_base_pos
-
2021 sizeof(struct ceph_msg_header
) -
2022 sizeof(struct ceph_msg_footer
);
2023 con
->in_msg
->pages
= NULL
;
2024 ceph_msg_put(con
->in_msg
);
2026 con
->in_tag
= CEPH_MSGR_TAG_READY
;
2028 dout("con_revoke_pages %p msg %p pages %p no-op\n",
2029 con
, con
->in_msg
, pages
);
2031 mutex_unlock(&con
->mutex
);
2035 * Queue a keepalive byte to ensure the tcp connection is alive.
2037 void ceph_con_keepalive(struct ceph_connection
*con
)
2039 if (test_and_set_bit(KEEPALIVE_PENDING
, &con
->state
) == 0 &&
2040 test_and_set_bit(WRITE_PENDING
, &con
->state
) == 0)
2046 * construct a new message with given type, size
2047 * the new msg has a ref count of 1.
2049 struct ceph_msg
*ceph_msg_new(int type
, int front_len
,
2050 int page_len
, int page_off
, struct page
**pages
)
2054 m
= kmalloc(sizeof(*m
), GFP_NOFS
);
2057 kref_init(&m
->kref
);
2058 INIT_LIST_HEAD(&m
->list_head
);
2060 m
->hdr
.type
= cpu_to_le16(type
);
2061 m
->hdr
.front_len
= cpu_to_le32(front_len
);
2062 m
->hdr
.middle_len
= 0;
2063 m
->hdr
.data_len
= cpu_to_le32(page_len
);
2064 m
->hdr
.data_off
= cpu_to_le16(page_off
);
2065 m
->hdr
.priority
= cpu_to_le16(CEPH_MSG_PRIO_DEFAULT
);
2066 m
->footer
.front_crc
= 0;
2067 m
->footer
.middle_crc
= 0;
2068 m
->footer
.data_crc
= 0;
2069 m
->front_max
= front_len
;
2070 m
->front_is_vmalloc
= false;
2071 m
->more_to_follow
= false;
2076 if (front_len
> PAGE_CACHE_SIZE
) {
2077 m
->front
.iov_base
= __vmalloc(front_len
, GFP_NOFS
,
2079 m
->front_is_vmalloc
= true;
2081 m
->front
.iov_base
= kmalloc(front_len
, GFP_NOFS
);
2083 if (m
->front
.iov_base
== NULL
) {
2084 pr_err("msg_new can't allocate %d bytes\n",
2089 m
->front
.iov_base
= NULL
;
2091 m
->front
.iov_len
= front_len
;
2097 m
->nr_pages
= calc_pages_for(page_off
, page_len
);
2100 dout("ceph_msg_new %p page %d~%d -> %d\n", m
, page_off
, page_len
,
2107 pr_err("msg_new can't create type %d len %d\n", type
, front_len
);
2108 return ERR_PTR(-ENOMEM
);
2112 * Generic message allocator, for incoming messages.
2114 struct ceph_msg
*ceph_alloc_msg(struct ceph_connection
*con
,
2115 struct ceph_msg_header
*hdr
)
2117 int type
= le16_to_cpu(hdr
->type
);
2118 int front_len
= le32_to_cpu(hdr
->front_len
);
2119 struct ceph_msg
*msg
= ceph_msg_new(type
, front_len
, 0, 0, NULL
);
2122 pr_err("unable to allocate msg type %d len %d\n",
2124 return ERR_PTR(-ENOMEM
);
2130 * Allocate "middle" portion of a message, if it is needed and wasn't
2131 * allocated by alloc_msg. This allows us to read a small fixed-size
2132 * per-type header in the front and then gracefully fail (i.e.,
2133 * propagate the error to the caller based on info in the front) when
2134 * the middle is too large.
2136 int ceph_alloc_middle(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2138 int type
= le16_to_cpu(msg
->hdr
.type
);
2139 int middle_len
= le32_to_cpu(msg
->hdr
.middle_len
);
2141 dout("alloc_middle %p type %d %s middle_len %d\n", msg
, type
,
2142 ceph_msg_type_name(type
), middle_len
);
2143 BUG_ON(!middle_len
);
2144 BUG_ON(msg
->middle
);
2146 msg
->middle
= ceph_buffer_new(middle_len
, GFP_NOFS
);
2154 * Free a generically kmalloc'd message.
2156 void ceph_msg_kfree(struct ceph_msg
*m
)
2158 dout("msg_kfree %p\n", m
);
2159 if (m
->front_is_vmalloc
)
2160 vfree(m
->front
.iov_base
);
2162 kfree(m
->front
.iov_base
);
2167 * Drop a msg ref. Destroy as needed.
2169 void ceph_msg_last_put(struct kref
*kref
)
2171 struct ceph_msg
*m
= container_of(kref
, struct ceph_msg
, kref
);
2173 dout("ceph_msg_put last one on %p\n", m
);
2174 WARN_ON(!list_empty(&m
->list_head
));
2176 /* drop middle, data, if any */
2178 ceph_buffer_put(m
->middle
);
2185 ceph_msgpool_put(m
->pool
, m
);
2190 void ceph_msg_dump(struct ceph_msg
*msg
)
2192 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg
,
2193 msg
->front_max
, msg
->nr_pages
);
2194 print_hex_dump(KERN_DEBUG
, "header: ",
2195 DUMP_PREFIX_OFFSET
, 16, 1,
2196 &msg
->hdr
, sizeof(msg
->hdr
), true);
2197 print_hex_dump(KERN_DEBUG
, " front: ",
2198 DUMP_PREFIX_OFFSET
, 16, 1,
2199 msg
->front
.iov_base
, msg
->front
.iov_len
, true);
2201 print_hex_dump(KERN_DEBUG
, "middle: ",
2202 DUMP_PREFIX_OFFSET
, 16, 1,
2203 msg
->middle
->vec
.iov_base
,
2204 msg
->middle
->vec
.iov_len
, true);
2205 print_hex_dump(KERN_DEBUG
, "footer: ",
2206 DUMP_PREFIX_OFFSET
, 16, 1,
2207 &msg
->footer
, sizeof(msg
->footer
), true);