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/slab.h>
10 #include <linux/socket.h>
11 #include <linux/string.h>
15 #include "messenger.h"
20 * Ceph uses the messenger to exchange ceph_msg messages with other
21 * hosts in the system. The messenger provides ordered and reliable
22 * delivery. We tolerate TCP disconnects by reconnecting (with
23 * exponential backoff) in the case of a fault (disconnection, bad
24 * crc, protocol error). Acks allow sent messages to be discarded by
28 /* static tag bytes (protocol control messages) */
29 static char tag_msg = CEPH_MSGR_TAG_MSG;
30 static char tag_ack = CEPH_MSGR_TAG_ACK;
31 static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
34 static struct lock_class_key socket_class;
38 static void queue_con(struct ceph_connection *con);
39 static void con_work(struct work_struct *);
40 static void ceph_fault(struct ceph_connection *con);
43 * nicely render a sockaddr as a string.
45 #define MAX_ADDR_STR 20
46 #define MAX_ADDR_STR_LEN 60
47 static char addr_str[MAX_ADDR_STR][MAX_ADDR_STR_LEN];
48 static DEFINE_SPINLOCK(addr_str_lock);
49 static int last_addr_str;
51 const char *pr_addr(const struct sockaddr_storage *ss)
55 struct sockaddr_in *in4 = (void *)ss;
56 struct sockaddr_in6 *in6 = (void *)ss;
58 spin_lock(&addr_str_lock);
60 if (last_addr_str == MAX_ADDR_STR)
62 spin_unlock(&addr_str_lock);
65 switch (ss->ss_family) {
67 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%u", &in4->sin_addr,
68 (unsigned int)ntohs(in4->sin_port));
72 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%u", &in6->sin6_addr,
73 (unsigned int)ntohs(in6->sin6_port));
77 sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family);
83 static void encode_my_addr(struct ceph_messenger *msgr)
85 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
86 ceph_encode_addr(&msgr->my_enc_addr);
90 * work queue for all reading and writing to/from the socket.
92 struct workqueue_struct *ceph_msgr_wq;
94 int __init ceph_msgr_init(void)
96 ceph_msgr_wq = create_workqueue("ceph-msgr");
97 if (IS_ERR(ceph_msgr_wq)) {
98 int ret = PTR_ERR(ceph_msgr_wq);
99 pr_err("msgr_init failed to create workqueue: %d\n", ret);
106 void ceph_msgr_exit(void)
108 destroy_workqueue(ceph_msgr_wq);
111 void ceph_msgr_flush()
113 flush_workqueue(ceph_msgr_wq);
118 * socket callback functions
121 /* data available on socket, or listen socket received a connect */
122 static void ceph_data_ready(struct sock *sk, int count_unused)
124 struct ceph_connection *con =
125 (struct ceph_connection *)sk->sk_user_data;
126 if (sk->sk_state != TCP_CLOSE_WAIT) {
127 dout("ceph_data_ready on %p state = %lu, queueing work\n",
133 /* socket has buffer space for writing */
134 static void ceph_write_space(struct sock *sk)
136 struct ceph_connection *con =
137 (struct ceph_connection *)sk->sk_user_data;
139 /* only queue to workqueue if there is data we want to write. */
140 if (test_bit(WRITE_PENDING, &con->state)) {
141 dout("ceph_write_space %p queueing write work\n", con);
144 dout("ceph_write_space %p nothing to write\n", con);
147 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
148 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
151 /* socket's state has changed */
152 static void ceph_state_change(struct sock *sk)
154 struct ceph_connection *con =
155 (struct ceph_connection *)sk->sk_user_data;
157 dout("ceph_state_change %p state = %lu sk_state = %u\n",
158 con, con->state, sk->sk_state);
160 if (test_bit(CLOSED, &con->state))
163 switch (sk->sk_state) {
165 dout("ceph_state_change TCP_CLOSE\n");
167 dout("ceph_state_change TCP_CLOSE_WAIT\n");
168 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
169 if (test_bit(CONNECTING, &con->state))
170 con->error_msg = "connection failed";
172 con->error_msg = "socket closed";
176 case TCP_ESTABLISHED:
177 dout("ceph_state_change TCP_ESTABLISHED\n");
184 * set up socket callbacks
186 static void set_sock_callbacks(struct socket *sock,
187 struct ceph_connection *con)
189 struct sock *sk = sock->sk;
190 sk->sk_user_data = (void *)con;
191 sk->sk_data_ready = ceph_data_ready;
192 sk->sk_write_space = ceph_write_space;
193 sk->sk_state_change = ceph_state_change;
202 * initiate connection to a remote socket.
204 static struct socket *ceph_tcp_connect(struct ceph_connection *con)
206 struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr;
211 ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
215 sock->sk->sk_allocation = GFP_NOFS;
217 #ifdef CONFIG_LOCKDEP
218 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
221 set_sock_callbacks(sock, con);
223 dout("connect %s\n", pr_addr(&con->peer_addr.in_addr));
225 ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK);
226 if (ret == -EINPROGRESS) {
227 dout("connect %s EINPROGRESS sk_state = %u\n",
228 pr_addr(&con->peer_addr.in_addr),
233 pr_err("connect %s error %d\n",
234 pr_addr(&con->peer_addr.in_addr), ret);
237 con->error_msg = "connect error";
245 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
247 struct kvec iov = {buf, len};
248 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
250 return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
254 * write something. @more is true if caller will be sending more data
257 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
258 size_t kvlen, size_t len, int more)
260 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
263 msg.msg_flags |= MSG_MORE;
265 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
267 return kernel_sendmsg(sock, &msg, iov, kvlen, len);
272 * Shutdown/close the socket for the given connection.
274 static int con_close_socket(struct ceph_connection *con)
278 dout("con_close_socket on %p sock %p\n", con, con->sock);
281 set_bit(SOCK_CLOSED, &con->state);
282 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
283 sock_release(con->sock);
285 clear_bit(SOCK_CLOSED, &con->state);
290 * Reset a connection. Discard all incoming and outgoing messages
291 * and clear *_seq state.
293 static void ceph_msg_remove(struct ceph_msg *msg)
295 list_del_init(&msg->list_head);
298 static void ceph_msg_remove_list(struct list_head *head)
300 while (!list_empty(head)) {
301 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
303 ceph_msg_remove(msg);
307 static void reset_connection(struct ceph_connection *con)
309 /* reset connection, out_queue, msg_ and connect_seq */
310 /* discard existing out_queue and msg_seq */
311 ceph_msg_remove_list(&con->out_queue);
312 ceph_msg_remove_list(&con->out_sent);
315 ceph_msg_put(con->in_msg);
319 con->connect_seq = 0;
322 ceph_msg_put(con->out_msg);
325 con->out_keepalive_pending = false;
327 con->in_seq_acked = 0;
331 * mark a peer down. drop any open connections.
333 void ceph_con_close(struct ceph_connection *con)
335 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
336 set_bit(CLOSED, &con->state); /* in case there's queued work */
337 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
338 clear_bit(LOSSYTX, &con->state); /* so we retry next connect */
339 clear_bit(KEEPALIVE_PENDING, &con->state);
340 clear_bit(WRITE_PENDING, &con->state);
341 mutex_lock(&con->mutex);
342 reset_connection(con);
343 con->peer_global_seq = 0;
344 cancel_delayed_work(&con->work);
345 mutex_unlock(&con->mutex);
350 * Reopen a closed connection, with a new peer address.
352 void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
354 dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
355 set_bit(OPENING, &con->state);
356 clear_bit(CLOSED, &con->state);
357 memcpy(&con->peer_addr, addr, sizeof(*addr));
358 con->delay = 0; /* reset backoff memory */
363 * return true if this connection ever successfully opened
365 bool ceph_con_opened(struct ceph_connection *con)
367 return con->connect_seq > 0;
373 struct ceph_connection *ceph_con_get(struct ceph_connection *con)
375 dout("con_get %p nref = %d -> %d\n", con,
376 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
377 if (atomic_inc_not_zero(&con->nref))
382 void ceph_con_put(struct ceph_connection *con)
384 dout("con_put %p nref = %d -> %d\n", con,
385 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
386 BUG_ON(atomic_read(&con->nref) == 0);
387 if (atomic_dec_and_test(&con->nref)) {
394 * initialize a new connection.
396 void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
398 dout("con_init %p\n", con);
399 memset(con, 0, sizeof(*con));
400 atomic_set(&con->nref, 1);
402 mutex_init(&con->mutex);
403 INIT_LIST_HEAD(&con->out_queue);
404 INIT_LIST_HEAD(&con->out_sent);
405 INIT_DELAYED_WORK(&con->work, con_work);
410 * We maintain a global counter to order connection attempts. Get
411 * a unique seq greater than @gt.
413 static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
417 spin_lock(&msgr->global_seq_lock);
418 if (msgr->global_seq < gt)
419 msgr->global_seq = gt;
420 ret = ++msgr->global_seq;
421 spin_unlock(&msgr->global_seq_lock);
427 * Prepare footer for currently outgoing message, and finish things
428 * off. Assumes out_kvec* are already valid.. we just add on to the end.
430 static void prepare_write_message_footer(struct ceph_connection *con, int v)
432 struct ceph_msg *m = con->out_msg;
434 dout("prepare_write_message_footer %p\n", con);
435 con->out_kvec_is_msg = true;
436 con->out_kvec[v].iov_base = &m->footer;
437 con->out_kvec[v].iov_len = sizeof(m->footer);
438 con->out_kvec_bytes += sizeof(m->footer);
439 con->out_kvec_left++;
440 con->out_more = m->more_to_follow;
441 con->out_msg_done = true;
445 * Prepare headers for the next outgoing message.
447 static void prepare_write_message(struct ceph_connection *con)
452 con->out_kvec_bytes = 0;
453 con->out_kvec_is_msg = true;
454 con->out_msg_done = false;
456 /* Sneak an ack in there first? If we can get it into the same
457 * TCP packet that's a good thing. */
458 if (con->in_seq > con->in_seq_acked) {
459 con->in_seq_acked = con->in_seq;
460 con->out_kvec[v].iov_base = &tag_ack;
461 con->out_kvec[v++].iov_len = 1;
462 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
463 con->out_kvec[v].iov_base = &con->out_temp_ack;
464 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
465 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
468 m = list_first_entry(&con->out_queue,
469 struct ceph_msg, list_head);
471 if (test_bit(LOSSYTX, &con->state)) {
472 list_del_init(&m->list_head);
474 /* put message on sent list */
476 list_move_tail(&m->list_head, &con->out_sent);
480 * only assign outgoing seq # if we haven't sent this message
481 * yet. if it is requeued, resend with it's original seq.
483 if (m->needs_out_seq) {
484 m->hdr.seq = cpu_to_le64(++con->out_seq);
485 m->needs_out_seq = false;
488 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
489 m, con->out_seq, le16_to_cpu(m->hdr.type),
490 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
491 le32_to_cpu(m->hdr.data_len),
493 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
495 /* tag + hdr + front + middle */
496 con->out_kvec[v].iov_base = &tag_msg;
497 con->out_kvec[v++].iov_len = 1;
498 con->out_kvec[v].iov_base = &m->hdr;
499 con->out_kvec[v++].iov_len = sizeof(m->hdr);
500 con->out_kvec[v++] = m->front;
502 con->out_kvec[v++] = m->middle->vec;
503 con->out_kvec_left = v;
504 con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
505 (m->middle ? m->middle->vec.iov_len : 0);
506 con->out_kvec_cur = con->out_kvec;
508 /* fill in crc (except data pages), footer */
509 con->out_msg->hdr.crc =
510 cpu_to_le32(crc32c(0, (void *)&m->hdr,
511 sizeof(m->hdr) - sizeof(m->hdr.crc)));
512 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
513 con->out_msg->footer.front_crc =
514 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
516 con->out_msg->footer.middle_crc =
517 cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
518 m->middle->vec.iov_len));
520 con->out_msg->footer.middle_crc = 0;
521 con->out_msg->footer.data_crc = 0;
522 dout("prepare_write_message front_crc %u data_crc %u\n",
523 le32_to_cpu(con->out_msg->footer.front_crc),
524 le32_to_cpu(con->out_msg->footer.middle_crc));
526 /* is there a data payload? */
527 if (le32_to_cpu(m->hdr.data_len) > 0) {
528 /* initialize page iterator */
529 con->out_msg_pos.page = 0;
530 con->out_msg_pos.page_pos =
531 le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
532 con->out_msg_pos.data_pos = 0;
533 con->out_msg_pos.did_page_crc = 0;
534 con->out_more = 1; /* data + footer will follow */
536 /* no, queue up footer too and be done */
537 prepare_write_message_footer(con, v);
540 set_bit(WRITE_PENDING, &con->state);
546 static void prepare_write_ack(struct ceph_connection *con)
548 dout("prepare_write_ack %p %llu -> %llu\n", con,
549 con->in_seq_acked, con->in_seq);
550 con->in_seq_acked = con->in_seq;
552 con->out_kvec[0].iov_base = &tag_ack;
553 con->out_kvec[0].iov_len = 1;
554 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
555 con->out_kvec[1].iov_base = &con->out_temp_ack;
556 con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
557 con->out_kvec_left = 2;
558 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
559 con->out_kvec_cur = con->out_kvec;
560 con->out_more = 1; /* more will follow.. eventually.. */
561 set_bit(WRITE_PENDING, &con->state);
565 * Prepare to write keepalive byte.
567 static void prepare_write_keepalive(struct ceph_connection *con)
569 dout("prepare_write_keepalive %p\n", con);
570 con->out_kvec[0].iov_base = &tag_keepalive;
571 con->out_kvec[0].iov_len = 1;
572 con->out_kvec_left = 1;
573 con->out_kvec_bytes = 1;
574 con->out_kvec_cur = con->out_kvec;
575 set_bit(WRITE_PENDING, &con->state);
579 * Connection negotiation.
582 static void prepare_connect_authorizer(struct ceph_connection *con)
586 int auth_protocol = 0;
588 mutex_unlock(&con->mutex);
589 if (con->ops->get_authorizer)
590 con->ops->get_authorizer(con, &auth_buf, &auth_len,
591 &auth_protocol, &con->auth_reply_buf,
592 &con->auth_reply_buf_len,
594 mutex_lock(&con->mutex);
596 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
597 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
599 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
600 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
601 con->out_kvec_left++;
602 con->out_kvec_bytes += auth_len;
606 * We connected to a peer and are saying hello.
608 static void prepare_write_banner(struct ceph_messenger *msgr,
609 struct ceph_connection *con)
611 int len = strlen(CEPH_BANNER);
613 con->out_kvec[0].iov_base = CEPH_BANNER;
614 con->out_kvec[0].iov_len = len;
615 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
616 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
617 con->out_kvec_left = 2;
618 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
619 con->out_kvec_cur = con->out_kvec;
621 set_bit(WRITE_PENDING, &con->state);
624 static void prepare_write_connect(struct ceph_messenger *msgr,
625 struct ceph_connection *con,
628 unsigned global_seq = get_global_seq(con->msgr, 0);
631 switch (con->peer_name.type) {
632 case CEPH_ENTITY_TYPE_MON:
633 proto = CEPH_MONC_PROTOCOL;
635 case CEPH_ENTITY_TYPE_OSD:
636 proto = CEPH_OSDC_PROTOCOL;
638 case CEPH_ENTITY_TYPE_MDS:
639 proto = CEPH_MDSC_PROTOCOL;
645 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
646 con->connect_seq, global_seq, proto);
648 con->out_connect.features = cpu_to_le64(CEPH_FEATURE_SUPPORTED_CLIENT);
649 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
650 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
651 con->out_connect.global_seq = cpu_to_le32(global_seq);
652 con->out_connect.protocol_version = cpu_to_le32(proto);
653 con->out_connect.flags = 0;
656 con->out_kvec_left = 0;
657 con->out_kvec_bytes = 0;
659 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
660 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
661 con->out_kvec_left++;
662 con->out_kvec_bytes += sizeof(con->out_connect);
663 con->out_kvec_cur = con->out_kvec;
665 set_bit(WRITE_PENDING, &con->state);
667 prepare_connect_authorizer(con);
672 * write as much of pending kvecs to the socket as we can.
674 * 0 -> socket full, but more to do
677 static int write_partial_kvec(struct ceph_connection *con)
681 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
682 while (con->out_kvec_bytes > 0) {
683 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
684 con->out_kvec_left, con->out_kvec_bytes,
688 con->out_kvec_bytes -= ret;
689 if (con->out_kvec_bytes == 0)
692 if (ret >= con->out_kvec_cur->iov_len) {
693 ret -= con->out_kvec_cur->iov_len;
695 con->out_kvec_left--;
697 con->out_kvec_cur->iov_len -= ret;
698 con->out_kvec_cur->iov_base += ret;
704 con->out_kvec_left = 0;
705 con->out_kvec_is_msg = false;
708 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
709 con->out_kvec_bytes, con->out_kvec_left, ret);
710 return ret; /* done! */
714 * Write as much message data payload as we can. If we finish, queue
716 * 1 -> done, footer is now queued in out_kvec[].
717 * 0 -> socket full, but more to do
720 static int write_partial_msg_pages(struct ceph_connection *con)
722 struct ceph_msg *msg = con->out_msg;
723 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
725 int crc = con->msgr->nocrc;
728 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
729 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
730 con->out_msg_pos.page_pos);
732 while (con->out_msg_pos.page < con->out_msg->nr_pages) {
733 struct page *page = NULL;
737 * if we are calculating the data crc (the default), we need
738 * to map the page. if our pages[] has been revoked, use the
742 page = msg->pages[con->out_msg_pos.page];
745 } else if (msg->pagelist) {
746 page = list_first_entry(&msg->pagelist->head,
751 page = con->msgr->zero_page;
753 kaddr = page_address(con->msgr->zero_page);
755 len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
756 (int)(data_len - con->out_msg_pos.data_pos));
757 if (crc && !con->out_msg_pos.did_page_crc) {
758 void *base = kaddr + con->out_msg_pos.page_pos;
759 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
761 BUG_ON(kaddr == NULL);
762 con->out_msg->footer.data_crc =
763 cpu_to_le32(crc32c(tmpcrc, base, len));
764 con->out_msg_pos.did_page_crc = 1;
767 ret = kernel_sendpage(con->sock, page,
768 con->out_msg_pos.page_pos, len,
769 MSG_DONTWAIT | MSG_NOSIGNAL |
772 if (crc && (msg->pages || msg->pagelist))
778 con->out_msg_pos.data_pos += ret;
779 con->out_msg_pos.page_pos += ret;
781 con->out_msg_pos.page_pos = 0;
782 con->out_msg_pos.page++;
783 con->out_msg_pos.did_page_crc = 0;
785 list_move_tail(&page->lru,
786 &msg->pagelist->head);
790 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
792 /* prepare and queue up footer, too */
794 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
795 con->out_kvec_bytes = 0;
796 con->out_kvec_left = 0;
797 con->out_kvec_cur = con->out_kvec;
798 prepare_write_message_footer(con, 0);
807 static int write_partial_skip(struct ceph_connection *con)
811 while (con->out_skip > 0) {
813 .iov_base = page_address(con->msgr->zero_page),
814 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
817 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
820 con->out_skip -= ret;
828 * Prepare to read connection handshake, or an ack.
830 static void prepare_read_banner(struct ceph_connection *con)
832 dout("prepare_read_banner %p\n", con);
833 con->in_base_pos = 0;
836 static void prepare_read_connect(struct ceph_connection *con)
838 dout("prepare_read_connect %p\n", con);
839 con->in_base_pos = 0;
842 static void prepare_read_ack(struct ceph_connection *con)
844 dout("prepare_read_ack %p\n", con);
845 con->in_base_pos = 0;
848 static void prepare_read_tag(struct ceph_connection *con)
850 dout("prepare_read_tag %p\n", con);
851 con->in_base_pos = 0;
852 con->in_tag = CEPH_MSGR_TAG_READY;
856 * Prepare to read a message.
858 static int prepare_read_message(struct ceph_connection *con)
860 dout("prepare_read_message %p\n", con);
861 BUG_ON(con->in_msg != NULL);
862 con->in_base_pos = 0;
863 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
868 static int read_partial(struct ceph_connection *con,
869 int *to, int size, void *object)
872 while (con->in_base_pos < *to) {
873 int left = *to - con->in_base_pos;
874 int have = size - left;
875 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
878 con->in_base_pos += ret;
885 * Read all or part of the connect-side handshake on a new connection
887 static int read_partial_banner(struct ceph_connection *con)
891 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
894 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
897 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
898 &con->actual_peer_addr);
901 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
902 &con->peer_addr_for_me);
909 static int read_partial_connect(struct ceph_connection *con)
913 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
915 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
918 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
919 con->auth_reply_buf);
923 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
924 con, (int)con->in_reply.tag,
925 le32_to_cpu(con->in_reply.connect_seq),
926 le32_to_cpu(con->in_reply.global_seq));
933 * Verify the hello banner looks okay.
935 static int verify_hello(struct ceph_connection *con)
937 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
938 pr_err("connect to %s got bad banner\n",
939 pr_addr(&con->peer_addr.in_addr));
940 con->error_msg = "protocol error, bad banner";
946 static bool addr_is_blank(struct sockaddr_storage *ss)
948 switch (ss->ss_family) {
950 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
953 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
954 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
955 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
956 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
961 static int addr_port(struct sockaddr_storage *ss)
963 switch (ss->ss_family) {
965 return ntohs(((struct sockaddr_in *)ss)->sin_port);
967 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
972 static void addr_set_port(struct sockaddr_storage *ss, int p)
974 switch (ss->ss_family) {
976 ((struct sockaddr_in *)ss)->sin_port = htons(p);
978 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
983 * Parse an ip[:port] list into an addr array. Use the default
984 * monitor port if a port isn't specified.
986 int ceph_parse_ips(const char *c, const char *end,
987 struct ceph_entity_addr *addr,
988 int max_count, int *count)
993 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
994 for (i = 0; i < max_count; i++) {
996 struct sockaddr_storage *ss = &addr[i].in_addr;
997 struct sockaddr_in *in4 = (void *)ss;
998 struct sockaddr_in6 *in6 = (void *)ss;
1001 memset(ss, 0, sizeof(*ss));
1002 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
1004 ss->ss_family = AF_INET;
1005 } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
1007 ss->ss_family = AF_INET6;
1014 if (p < end && *p == ':') {
1017 while (p < end && *p >= '0' && *p <= '9') {
1018 port = (port * 10) + (*p - '0');
1021 if (port > 65535 || port == 0)
1024 port = CEPH_MON_PORT;
1027 addr_set_port(ss, port);
1029 dout("parse_ips got %s\n", pr_addr(ss));
1046 pr_err("parse_ips bad ip '%s'\n", c);
1050 static int process_banner(struct ceph_connection *con)
1052 dout("process_banner on %p\n", con);
1054 if (verify_hello(con) < 0)
1057 ceph_decode_addr(&con->actual_peer_addr);
1058 ceph_decode_addr(&con->peer_addr_for_me);
1061 * Make sure the other end is who we wanted. note that the other
1062 * end may not yet know their ip address, so if it's 0.0.0.0, give
1063 * them the benefit of the doubt.
1065 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1066 sizeof(con->peer_addr)) != 0 &&
1067 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1068 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
1069 pr_warning("wrong peer, want %s/%lld, got %s/%lld\n",
1070 pr_addr(&con->peer_addr.in_addr),
1071 le64_to_cpu(con->peer_addr.nonce),
1072 pr_addr(&con->actual_peer_addr.in_addr),
1073 le64_to_cpu(con->actual_peer_addr.nonce));
1074 con->error_msg = "wrong peer at address";
1079 * did we learn our address?
1081 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1082 int port = addr_port(&con->msgr->inst.addr.in_addr);
1084 memcpy(&con->msgr->inst.addr.in_addr,
1085 &con->peer_addr_for_me.in_addr,
1086 sizeof(con->peer_addr_for_me.in_addr));
1087 addr_set_port(&con->msgr->inst.addr.in_addr, port);
1088 encode_my_addr(con->msgr);
1089 dout("process_banner learned my addr is %s\n",
1090 pr_addr(&con->msgr->inst.addr.in_addr));
1093 set_bit(NEGOTIATING, &con->state);
1094 prepare_read_connect(con);
1098 static void fail_protocol(struct ceph_connection *con)
1100 reset_connection(con);
1101 set_bit(CLOSED, &con->state); /* in case there's queued work */
1103 mutex_unlock(&con->mutex);
1104 if (con->ops->bad_proto)
1105 con->ops->bad_proto(con);
1106 mutex_lock(&con->mutex);
1109 static int process_connect(struct ceph_connection *con)
1111 u64 sup_feat = CEPH_FEATURE_SUPPORTED_CLIENT;
1112 u64 req_feat = CEPH_FEATURE_REQUIRED_CLIENT;
1113 u64 server_feat = le64_to_cpu(con->in_reply.features);
1115 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1117 switch (con->in_reply.tag) {
1118 case CEPH_MSGR_TAG_FEATURES:
1119 pr_err("%s%lld %s feature set mismatch,"
1120 " my %llx < server's %llx, missing %llx\n",
1121 ENTITY_NAME(con->peer_name),
1122 pr_addr(&con->peer_addr.in_addr),
1123 sup_feat, server_feat, server_feat & ~sup_feat);
1124 con->error_msg = "missing required protocol features";
1128 case CEPH_MSGR_TAG_BADPROTOVER:
1129 pr_err("%s%lld %s protocol version mismatch,"
1130 " my %d != server's %d\n",
1131 ENTITY_NAME(con->peer_name),
1132 pr_addr(&con->peer_addr.in_addr),
1133 le32_to_cpu(con->out_connect.protocol_version),
1134 le32_to_cpu(con->in_reply.protocol_version));
1135 con->error_msg = "protocol version mismatch";
1139 case CEPH_MSGR_TAG_BADAUTHORIZER:
1141 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1143 if (con->auth_retry == 2) {
1144 con->error_msg = "connect authorization failure";
1145 reset_connection(con);
1146 set_bit(CLOSED, &con->state);
1149 con->auth_retry = 1;
1150 prepare_write_connect(con->msgr, con, 0);
1151 prepare_read_connect(con);
1154 case CEPH_MSGR_TAG_RESETSESSION:
1156 * If we connected with a large connect_seq but the peer
1157 * has no record of a session with us (no connection, or
1158 * connect_seq == 0), they will send RESETSESION to indicate
1159 * that they must have reset their session, and may have
1162 dout("process_connect got RESET peer seq %u\n",
1163 le32_to_cpu(con->in_connect.connect_seq));
1164 pr_err("%s%lld %s connection reset\n",
1165 ENTITY_NAME(con->peer_name),
1166 pr_addr(&con->peer_addr.in_addr));
1167 reset_connection(con);
1168 prepare_write_connect(con->msgr, con, 0);
1169 prepare_read_connect(con);
1171 /* Tell ceph about it. */
1172 mutex_unlock(&con->mutex);
1173 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1174 if (con->ops->peer_reset)
1175 con->ops->peer_reset(con);
1176 mutex_lock(&con->mutex);
1179 case CEPH_MSGR_TAG_RETRY_SESSION:
1181 * If we sent a smaller connect_seq than the peer has, try
1182 * again with a larger value.
1184 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1185 le32_to_cpu(con->out_connect.connect_seq),
1186 le32_to_cpu(con->in_connect.connect_seq));
1187 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
1188 prepare_write_connect(con->msgr, con, 0);
1189 prepare_read_connect(con);
1192 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1194 * If we sent a smaller global_seq than the peer has, try
1195 * again with a larger value.
1197 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
1198 con->peer_global_seq,
1199 le32_to_cpu(con->in_connect.global_seq));
1200 get_global_seq(con->msgr,
1201 le32_to_cpu(con->in_connect.global_seq));
1202 prepare_write_connect(con->msgr, con, 0);
1203 prepare_read_connect(con);
1206 case CEPH_MSGR_TAG_READY:
1207 if (req_feat & ~server_feat) {
1208 pr_err("%s%lld %s protocol feature mismatch,"
1209 " my required %llx > server's %llx, need %llx\n",
1210 ENTITY_NAME(con->peer_name),
1211 pr_addr(&con->peer_addr.in_addr),
1212 req_feat, server_feat, req_feat & ~server_feat);
1213 con->error_msg = "missing required protocol features";
1217 clear_bit(CONNECTING, &con->state);
1218 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1220 con->peer_features = server_feat;
1221 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1222 con->peer_global_seq,
1223 le32_to_cpu(con->in_reply.connect_seq),
1225 WARN_ON(con->connect_seq !=
1226 le32_to_cpu(con->in_reply.connect_seq));
1228 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1229 set_bit(LOSSYTX, &con->state);
1231 prepare_read_tag(con);
1234 case CEPH_MSGR_TAG_WAIT:
1236 * If there is a connection race (we are opening
1237 * connections to each other), one of us may just have
1238 * to WAIT. This shouldn't happen if we are the
1241 pr_err("process_connect peer connecting WAIT\n");
1244 pr_err("connect protocol error, will retry\n");
1245 con->error_msg = "protocol error, garbage tag during connect";
1253 * read (part of) an ack
1255 static int read_partial_ack(struct ceph_connection *con)
1259 return read_partial(con, &to, sizeof(con->in_temp_ack),
1265 * We can finally discard anything that's been acked.
1267 static void process_ack(struct ceph_connection *con)
1270 u64 ack = le64_to_cpu(con->in_temp_ack);
1273 while (!list_empty(&con->out_sent)) {
1274 m = list_first_entry(&con->out_sent, struct ceph_msg,
1276 seq = le64_to_cpu(m->hdr.seq);
1279 dout("got ack for seq %llu type %d at %p\n", seq,
1280 le16_to_cpu(m->hdr.type), m);
1283 prepare_read_tag(con);
1289 static int read_partial_message_section(struct ceph_connection *con,
1290 struct kvec *section, unsigned int sec_len,
1298 while (section->iov_len < sec_len) {
1299 BUG_ON(section->iov_base == NULL);
1300 left = sec_len - section->iov_len;
1301 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1302 section->iov_len, left);
1305 section->iov_len += ret;
1306 if (section->iov_len == sec_len)
1307 *crc = crc32c(0, section->iov_base,
1314 static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1315 struct ceph_msg_header *hdr,
1318 * read (part of) a message.
1320 static int read_partial_message(struct ceph_connection *con)
1322 struct ceph_msg *m = con->in_msg;
1326 unsigned front_len, middle_len, data_len, data_off;
1327 int datacrc = con->msgr->nocrc;
1331 dout("read_partial_message con %p msg %p\n", con, m);
1334 while (con->in_base_pos < sizeof(con->in_hdr)) {
1335 left = sizeof(con->in_hdr) - con->in_base_pos;
1336 ret = ceph_tcp_recvmsg(con->sock,
1337 (char *)&con->in_hdr + con->in_base_pos,
1341 con->in_base_pos += ret;
1342 if (con->in_base_pos == sizeof(con->in_hdr)) {
1343 u32 crc = crc32c(0, (void *)&con->in_hdr,
1344 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1345 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1346 pr_err("read_partial_message bad hdr "
1347 " crc %u != expected %u\n",
1348 crc, con->in_hdr.crc);
1353 front_len = le32_to_cpu(con->in_hdr.front_len);
1354 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1356 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1357 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1359 data_len = le32_to_cpu(con->in_hdr.data_len);
1360 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1362 data_off = le16_to_cpu(con->in_hdr.data_off);
1365 seq = le64_to_cpu(con->in_hdr.seq);
1366 if ((s64)seq - (s64)con->in_seq < 1) {
1367 pr_info("skipping %s%lld %s seq %lld, expected %lld\n",
1368 ENTITY_NAME(con->peer_name),
1369 pr_addr(&con->peer_addr.in_addr),
1370 seq, con->in_seq + 1);
1371 con->in_base_pos = -front_len - middle_len - data_len -
1373 con->in_tag = CEPH_MSGR_TAG_READY;
1376 } else if ((s64)seq - (s64)con->in_seq > 1) {
1377 pr_err("read_partial_message bad seq %lld expected %lld\n",
1378 seq, con->in_seq + 1);
1379 con->error_msg = "bad message sequence # for incoming message";
1383 /* allocate message? */
1385 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1386 con->in_hdr.front_len, con->in_hdr.data_len);
1388 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1390 /* skip this message */
1391 dout("alloc_msg said skip message\n");
1392 BUG_ON(con->in_msg);
1393 con->in_base_pos = -front_len - middle_len - data_len -
1395 con->in_tag = CEPH_MSGR_TAG_READY;
1401 "error allocating memory for incoming message";
1405 m->front.iov_len = 0; /* haven't read it yet */
1407 m->middle->vec.iov_len = 0;
1409 con->in_msg_pos.page = 0;
1410 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1411 con->in_msg_pos.data_pos = 0;
1415 ret = read_partial_message_section(con, &m->front, front_len,
1416 &con->in_front_crc);
1422 ret = read_partial_message_section(con, &m->middle->vec, middle_len,
1423 &con->in_middle_crc);
1429 while (con->in_msg_pos.data_pos < data_len) {
1430 left = min((int)(data_len - con->in_msg_pos.data_pos),
1431 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1432 BUG_ON(m->pages == NULL);
1433 p = kmap(m->pages[con->in_msg_pos.page]);
1434 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1436 if (ret > 0 && datacrc)
1438 crc32c(con->in_data_crc,
1439 p + con->in_msg_pos.page_pos, ret);
1440 kunmap(m->pages[con->in_msg_pos.page]);
1443 con->in_msg_pos.data_pos += ret;
1444 con->in_msg_pos.page_pos += ret;
1445 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1446 con->in_msg_pos.page_pos = 0;
1447 con->in_msg_pos.page++;
1452 to = sizeof(m->hdr) + sizeof(m->footer);
1453 while (con->in_base_pos < to) {
1454 left = to - con->in_base_pos;
1455 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1456 (con->in_base_pos - sizeof(m->hdr)),
1460 con->in_base_pos += ret;
1462 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1463 m, front_len, m->footer.front_crc, middle_len,
1464 m->footer.middle_crc, data_len, m->footer.data_crc);
1467 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1468 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1469 m, con->in_front_crc, m->footer.front_crc);
1472 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1473 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1474 m, con->in_middle_crc, m->footer.middle_crc);
1478 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1479 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1480 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1481 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1485 return 1; /* done! */
1489 * Process message. This happens in the worker thread. The callback should
1490 * be careful not to do anything that waits on other incoming messages or it
1493 static void process_message(struct ceph_connection *con)
1495 struct ceph_msg *msg;
1500 /* if first message, set peer_name */
1501 if (con->peer_name.type == 0)
1502 con->peer_name = msg->hdr.src;
1505 mutex_unlock(&con->mutex);
1507 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1508 msg, le64_to_cpu(msg->hdr.seq),
1509 ENTITY_NAME(msg->hdr.src),
1510 le16_to_cpu(msg->hdr.type),
1511 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1512 le32_to_cpu(msg->hdr.front_len),
1513 le32_to_cpu(msg->hdr.data_len),
1514 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1515 con->ops->dispatch(con, msg);
1517 mutex_lock(&con->mutex);
1518 prepare_read_tag(con);
1523 * Write something to the socket. Called in a worker thread when the
1524 * socket appears to be writeable and we have something ready to send.
1526 static int try_write(struct ceph_connection *con)
1528 struct ceph_messenger *msgr = con->msgr;
1531 dout("try_write start %p state %lu nref %d\n", con, con->state,
1532 atomic_read(&con->nref));
1535 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1537 /* open the socket first? */
1538 if (con->sock == NULL) {
1540 * if we were STANDBY and are reconnecting _this_
1541 * connection, bump connect_seq now. Always bump
1544 if (test_and_clear_bit(STANDBY, &con->state))
1547 prepare_write_banner(msgr, con);
1548 prepare_write_connect(msgr, con, 1);
1549 prepare_read_banner(con);
1550 set_bit(CONNECTING, &con->state);
1551 clear_bit(NEGOTIATING, &con->state);
1553 BUG_ON(con->in_msg);
1554 con->in_tag = CEPH_MSGR_TAG_READY;
1555 dout("try_write initiating connect on %p new state %lu\n",
1557 con->sock = ceph_tcp_connect(con);
1558 if (IS_ERR(con->sock)) {
1560 con->error_msg = "connect error";
1567 /* kvec data queued? */
1568 if (con->out_skip) {
1569 ret = write_partial_skip(con);
1573 dout("try_write write_partial_skip err %d\n", ret);
1577 if (con->out_kvec_left) {
1578 ret = write_partial_kvec(con);
1585 if (con->out_msg_done) {
1586 ceph_msg_put(con->out_msg);
1587 con->out_msg = NULL; /* we're done with this one */
1591 ret = write_partial_msg_pages(con);
1593 goto more_kvec; /* we need to send the footer, too! */
1597 dout("try_write write_partial_msg_pages err %d\n",
1604 if (!test_bit(CONNECTING, &con->state)) {
1605 /* is anything else pending? */
1606 if (!list_empty(&con->out_queue)) {
1607 prepare_write_message(con);
1610 if (con->in_seq > con->in_seq_acked) {
1611 prepare_write_ack(con);
1614 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1615 prepare_write_keepalive(con);
1620 /* Nothing to do! */
1621 clear_bit(WRITE_PENDING, &con->state);
1622 dout("try_write nothing else to write.\n");
1626 dout("try_write done on %p\n", con);
1633 * Read what we can from the socket.
1635 static int try_read(struct ceph_connection *con)
1642 if (test_bit(STANDBY, &con->state))
1645 dout("try_read start on %p\n", con);
1648 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1650 if (test_bit(CONNECTING, &con->state)) {
1651 if (!test_bit(NEGOTIATING, &con->state)) {
1652 dout("try_read connecting\n");
1653 ret = read_partial_banner(con);
1656 if (process_banner(con) < 0) {
1661 ret = read_partial_connect(con);
1664 if (process_connect(con) < 0) {
1671 if (con->in_base_pos < 0) {
1673 * skipping + discarding content.
1675 * FIXME: there must be a better way to do this!
1677 static char buf[1024];
1678 int skip = min(1024, -con->in_base_pos);
1679 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1680 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1683 con->in_base_pos += ret;
1684 if (con->in_base_pos)
1687 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1691 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1694 dout("try_read got tag %d\n", (int)con->in_tag);
1695 switch (con->in_tag) {
1696 case CEPH_MSGR_TAG_MSG:
1697 prepare_read_message(con);
1699 case CEPH_MSGR_TAG_ACK:
1700 prepare_read_ack(con);
1702 case CEPH_MSGR_TAG_CLOSE:
1703 set_bit(CLOSED, &con->state); /* fixme */
1709 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1710 ret = read_partial_message(con);
1714 con->error_msg = "bad crc";
1718 con->error_msg = "io error";
1724 if (con->in_tag == CEPH_MSGR_TAG_READY)
1726 process_message(con);
1729 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1730 ret = read_partial_ack(con);
1740 dout("try_read done on %p\n", con);
1744 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1745 con->error_msg = "protocol error, garbage tag";
1752 * Atomically queue work on a connection. Bump @con reference to
1753 * avoid races with connection teardown.
1755 * There is some trickery going on with QUEUED and BUSY because we
1756 * only want a _single_ thread operating on each connection at any
1757 * point in time, but we want to use all available CPUs.
1759 * The worker thread only proceeds if it can atomically set BUSY. It
1760 * clears QUEUED and does it's thing. When it thinks it's done, it
1761 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1762 * (tries again to set BUSY).
1764 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1765 * try to queue work. If that fails (work is already queued, or BUSY)
1766 * we give up (work also already being done or is queued) but leave QUEUED
1767 * set so that the worker thread will loop if necessary.
1769 static void queue_con(struct ceph_connection *con)
1771 if (test_bit(DEAD, &con->state)) {
1772 dout("queue_con %p ignoring: DEAD\n",
1777 if (!con->ops->get(con)) {
1778 dout("queue_con %p ref count 0\n", con);
1782 set_bit(QUEUED, &con->state);
1783 if (test_bit(BUSY, &con->state)) {
1784 dout("queue_con %p - already BUSY\n", con);
1786 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1787 dout("queue_con %p - already queued\n", con);
1790 dout("queue_con %p\n", con);
1795 * Do some work on a connection. Drop a connection ref when we're done.
1797 static void con_work(struct work_struct *work)
1799 struct ceph_connection *con = container_of(work, struct ceph_connection,
1804 if (test_and_set_bit(BUSY, &con->state) != 0) {
1805 dout("con_work %p BUSY already set\n", con);
1808 dout("con_work %p start, clearing QUEUED\n", con);
1809 clear_bit(QUEUED, &con->state);
1811 mutex_lock(&con->mutex);
1813 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1814 dout("con_work CLOSED\n");
1815 con_close_socket(con);
1818 if (test_and_clear_bit(OPENING, &con->state)) {
1819 /* reopen w/ new peer */
1820 dout("con_work OPENING\n");
1821 con_close_socket(con);
1824 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1825 try_read(con) < 0 ||
1826 try_write(con) < 0) {
1827 mutex_unlock(&con->mutex);
1829 ceph_fault(con); /* error/fault path */
1834 mutex_unlock(&con->mutex);
1837 clear_bit(BUSY, &con->state);
1838 dout("con->state=%lu\n", con->state);
1839 if (test_bit(QUEUED, &con->state)) {
1840 if (!backoff || test_bit(OPENING, &con->state)) {
1841 dout("con_work %p QUEUED reset, looping\n", con);
1844 dout("con_work %p QUEUED reset, but just faulted\n", con);
1845 clear_bit(QUEUED, &con->state);
1847 dout("con_work %p done\n", con);
1855 * Generic error/fault handler. A retry mechanism is used with
1856 * exponential backoff
1858 static void ceph_fault(struct ceph_connection *con)
1860 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1861 pr_addr(&con->peer_addr.in_addr), con->error_msg);
1862 dout("fault %p state %lu to peer %s\n",
1863 con, con->state, pr_addr(&con->peer_addr.in_addr));
1865 if (test_bit(LOSSYTX, &con->state)) {
1866 dout("fault on LOSSYTX channel\n");
1870 mutex_lock(&con->mutex);
1871 if (test_bit(CLOSED, &con->state))
1874 con_close_socket(con);
1877 ceph_msg_put(con->in_msg);
1881 /* Requeue anything that hasn't been acked */
1882 list_splice_init(&con->out_sent, &con->out_queue);
1884 /* If there are no messages in the queue, place the connection
1885 * in a STANDBY state (i.e., don't try to reconnect just yet). */
1886 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1887 dout("fault setting STANDBY\n");
1888 set_bit(STANDBY, &con->state);
1890 /* retry after a delay. */
1891 if (con->delay == 0)
1892 con->delay = BASE_DELAY_INTERVAL;
1893 else if (con->delay < MAX_DELAY_INTERVAL)
1895 dout("fault queueing %p delay %lu\n", con, con->delay);
1897 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1898 round_jiffies_relative(con->delay)) == 0)
1903 mutex_unlock(&con->mutex);
1906 * in case we faulted due to authentication, invalidate our
1907 * current tickets so that we can get new ones.
1909 if (con->auth_retry && con->ops->invalidate_authorizer) {
1910 dout("calling invalidate_authorizer()\n");
1911 con->ops->invalidate_authorizer(con);
1914 if (con->ops->fault)
1915 con->ops->fault(con);
1921 * create a new messenger instance
1923 struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1925 struct ceph_messenger *msgr;
1927 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1929 return ERR_PTR(-ENOMEM);
1931 spin_lock_init(&msgr->global_seq_lock);
1933 /* the zero page is needed if a request is "canceled" while the message
1934 * is being written over the socket */
1935 msgr->zero_page = __page_cache_alloc(GFP_KERNEL | __GFP_ZERO);
1936 if (!msgr->zero_page) {
1938 return ERR_PTR(-ENOMEM);
1940 kmap(msgr->zero_page);
1943 msgr->inst.addr = *myaddr;
1945 /* select a random nonce */
1946 msgr->inst.addr.type = 0;
1947 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
1948 encode_my_addr(msgr);
1950 dout("messenger_create %p\n", msgr);
1954 void ceph_messenger_destroy(struct ceph_messenger *msgr)
1956 dout("destroy %p\n", msgr);
1957 kunmap(msgr->zero_page);
1958 __free_page(msgr->zero_page);
1960 dout("destroyed messenger %p\n", msgr);
1964 * Queue up an outgoing message on the given connection.
1966 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1968 if (test_bit(CLOSED, &con->state)) {
1969 dout("con_send %p closed, dropping %p\n", con, msg);
1975 msg->hdr.src = con->msgr->inst.name;
1977 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
1979 msg->needs_out_seq = true;
1982 mutex_lock(&con->mutex);
1983 BUG_ON(!list_empty(&msg->list_head));
1984 list_add_tail(&msg->list_head, &con->out_queue);
1985 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
1986 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
1987 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1988 le32_to_cpu(msg->hdr.front_len),
1989 le32_to_cpu(msg->hdr.middle_len),
1990 le32_to_cpu(msg->hdr.data_len));
1991 mutex_unlock(&con->mutex);
1993 /* if there wasn't anything waiting to send before, queue
1995 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2000 * Revoke a message that was previously queued for send
2002 void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2004 mutex_lock(&con->mutex);
2005 if (!list_empty(&msg->list_head)) {
2006 dout("con_revoke %p msg %p - was on queue\n", con, msg);
2007 list_del_init(&msg->list_head);
2011 if (con->out_msg == msg) {
2012 dout("con_revoke %p msg %p - was sending\n", con, msg);
2013 con->out_msg = NULL;
2014 if (con->out_kvec_is_msg) {
2015 con->out_skip = con->out_kvec_bytes;
2016 con->out_kvec_is_msg = false;
2021 mutex_unlock(&con->mutex);
2025 * Revoke a message that we may be reading data into
2027 void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
2029 mutex_lock(&con->mutex);
2030 if (con->in_msg && con->in_msg == msg) {
2031 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2032 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
2033 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2035 /* skip rest of message */
2036 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
2037 con->in_base_pos = con->in_base_pos -
2038 sizeof(struct ceph_msg_header) -
2042 sizeof(struct ceph_msg_footer);
2043 ceph_msg_put(con->in_msg);
2045 con->in_tag = CEPH_MSGR_TAG_READY;
2048 dout("con_revoke_pages %p msg %p pages %p no-op\n",
2049 con, con->in_msg, msg);
2051 mutex_unlock(&con->mutex);
2055 * Queue a keepalive byte to ensure the tcp connection is alive.
2057 void ceph_con_keepalive(struct ceph_connection *con)
2059 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2060 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2066 * construct a new message with given type, size
2067 * the new msg has a ref count of 1.
2069 struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags)
2073 m = kmalloc(sizeof(*m), flags);
2076 kref_init(&m->kref);
2077 INIT_LIST_HEAD(&m->list_head);
2080 m->hdr.type = cpu_to_le16(type);
2081 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2083 m->hdr.front_len = cpu_to_le32(front_len);
2084 m->hdr.middle_len = 0;
2085 m->hdr.data_len = 0;
2086 m->hdr.data_off = 0;
2087 m->hdr.reserved = 0;
2088 m->footer.front_crc = 0;
2089 m->footer.middle_crc = 0;
2090 m->footer.data_crc = 0;
2091 m->footer.flags = 0;
2092 m->front_max = front_len;
2093 m->front_is_vmalloc = false;
2094 m->more_to_follow = false;
2099 if (front_len > PAGE_CACHE_SIZE) {
2100 m->front.iov_base = __vmalloc(front_len, flags,
2102 m->front_is_vmalloc = true;
2104 m->front.iov_base = kmalloc(front_len, flags);
2106 if (m->front.iov_base == NULL) {
2107 pr_err("msg_new can't allocate %d bytes\n",
2112 m->front.iov_base = NULL;
2114 m->front.iov_len = front_len;
2124 dout("ceph_msg_new %p front %d\n", m, front_len);
2130 pr_err("msg_new can't create type %d front %d\n", type, front_len);
2135 * Allocate "middle" portion of a message, if it is needed and wasn't
2136 * allocated by alloc_msg. This allows us to read a small fixed-size
2137 * per-type header in the front and then gracefully fail (i.e.,
2138 * propagate the error to the caller based on info in the front) when
2139 * the middle is too large.
2141 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
2143 int type = le16_to_cpu(msg->hdr.type);
2144 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2146 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2147 ceph_msg_type_name(type), middle_len);
2148 BUG_ON(!middle_len);
2149 BUG_ON(msg->middle);
2151 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
2158 * Generic message allocator, for incoming messages.
2160 static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2161 struct ceph_msg_header *hdr,
2164 int type = le16_to_cpu(hdr->type);
2165 int front_len = le32_to_cpu(hdr->front_len);
2166 int middle_len = le32_to_cpu(hdr->middle_len);
2167 struct ceph_msg *msg = NULL;
2170 if (con->ops->alloc_msg) {
2171 mutex_unlock(&con->mutex);
2172 msg = con->ops->alloc_msg(con, hdr, skip);
2173 mutex_lock(&con->mutex);
2179 msg = ceph_msg_new(type, front_len, GFP_NOFS);
2181 pr_err("unable to allocate msg type %d len %d\n",
2186 memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
2188 if (middle_len && !msg->middle) {
2189 ret = ceph_alloc_middle(con, msg);
2201 * Free a generically kmalloc'd message.
2203 void ceph_msg_kfree(struct ceph_msg *m)
2205 dout("msg_kfree %p\n", m);
2206 if (m->front_is_vmalloc)
2207 vfree(m->front.iov_base);
2209 kfree(m->front.iov_base);
2214 * Drop a msg ref. Destroy as needed.
2216 void ceph_msg_last_put(struct kref *kref)
2218 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
2220 dout("ceph_msg_put last one on %p\n", m);
2221 WARN_ON(!list_empty(&m->list_head));
2223 /* drop middle, data, if any */
2225 ceph_buffer_put(m->middle);
2232 ceph_pagelist_release(m->pagelist);
2238 ceph_msgpool_put(m->pool, m);
2243 void ceph_msg_dump(struct ceph_msg *msg)
2245 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2246 msg->front_max, msg->nr_pages);
2247 print_hex_dump(KERN_DEBUG, "header: ",
2248 DUMP_PREFIX_OFFSET, 16, 1,
2249 &msg->hdr, sizeof(msg->hdr), true);
2250 print_hex_dump(KERN_DEBUG, " front: ",
2251 DUMP_PREFIX_OFFSET, 16, 1,
2252 msg->front.iov_base, msg->front.iov_len, true);
2254 print_hex_dump(KERN_DEBUG, "middle: ",
2255 DUMP_PREFIX_OFFSET, 16, 1,
2256 msg->middle->vec.iov_base,
2257 msg->middle->vec.iov_len, true);
2258 print_hex_dump(KERN_DEBUG, "footer: ",
2259 DUMP_PREFIX_OFFSET, 16, 1,
2260 &msg->footer, sizeof(msg->footer), true);