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 void queue_con(struct ceph_connection *con);
35 static void con_work(struct work_struct *);
36 static void ceph_fault(struct ceph_connection *con);
38 const char *ceph_name_type_str(int t)
41 case CEPH_ENTITY_TYPE_MON: return "mon";
42 case CEPH_ENTITY_TYPE_MDS: return "mds";
43 case CEPH_ENTITY_TYPE_OSD: return "osd";
44 case CEPH_ENTITY_TYPE_CLIENT: return "client";
45 case CEPH_ENTITY_TYPE_ADMIN: return "admin";
46 default: return "???";
51 * nicely render a sockaddr as a string.
53 #define MAX_ADDR_STR 20
54 static char addr_str[MAX_ADDR_STR][40];
55 static DEFINE_SPINLOCK(addr_str_lock);
56 static int last_addr_str;
58 const char *pr_addr(const struct sockaddr_storage *ss)
62 struct sockaddr_in *in4 = (void *)ss;
63 unsigned char *quad = (void *)&in4->sin_addr.s_addr;
64 struct sockaddr_in6 *in6 = (void *)ss;
66 spin_lock(&addr_str_lock);
68 if (last_addr_str == MAX_ADDR_STR)
70 spin_unlock(&addr_str_lock);
73 switch (ss->ss_family) {
75 sprintf(s, "%u.%u.%u.%u:%u",
76 (unsigned int)quad[0],
77 (unsigned int)quad[1],
78 (unsigned int)quad[2],
79 (unsigned int)quad[3],
80 (unsigned int)ntohs(in4->sin_port));
84 sprintf(s, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u",
85 in6->sin6_addr.s6_addr16[0],
86 in6->sin6_addr.s6_addr16[1],
87 in6->sin6_addr.s6_addr16[2],
88 in6->sin6_addr.s6_addr16[3],
89 in6->sin6_addr.s6_addr16[4],
90 in6->sin6_addr.s6_addr16[5],
91 in6->sin6_addr.s6_addr16[6],
92 in6->sin6_addr.s6_addr16[7],
93 (unsigned int)ntohs(in6->sin6_port));
97 sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family);
103 static void encode_my_addr(struct ceph_messenger *msgr)
105 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
106 ceph_encode_addr(&msgr->my_enc_addr);
110 * work queue for all reading and writing to/from the socket.
112 struct workqueue_struct *ceph_msgr_wq;
114 int __init ceph_msgr_init(void)
116 ceph_msgr_wq = create_workqueue("ceph-msgr");
117 if (IS_ERR(ceph_msgr_wq)) {
118 int ret = PTR_ERR(ceph_msgr_wq);
119 pr_err("msgr_init failed to create workqueue: %d\n", ret);
126 void ceph_msgr_exit(void)
128 destroy_workqueue(ceph_msgr_wq);
132 * socket callback functions
135 /* data available on socket, or listen socket received a connect */
136 static void ceph_data_ready(struct sock *sk, int count_unused)
138 struct ceph_connection *con =
139 (struct ceph_connection *)sk->sk_user_data;
140 if (sk->sk_state != TCP_CLOSE_WAIT) {
141 dout("ceph_data_ready on %p state = %lu, queueing work\n",
147 /* socket has buffer space for writing */
148 static void ceph_write_space(struct sock *sk)
150 struct ceph_connection *con =
151 (struct ceph_connection *)sk->sk_user_data;
153 /* only queue to workqueue if there is data we want to write. */
154 if (test_bit(WRITE_PENDING, &con->state)) {
155 dout("ceph_write_space %p queueing write work\n", con);
158 dout("ceph_write_space %p nothing to write\n", con);
161 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
162 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
165 /* socket's state has changed */
166 static void ceph_state_change(struct sock *sk)
168 struct ceph_connection *con =
169 (struct ceph_connection *)sk->sk_user_data;
171 dout("ceph_state_change %p state = %lu sk_state = %u\n",
172 con, con->state, sk->sk_state);
174 if (test_bit(CLOSED, &con->state))
177 switch (sk->sk_state) {
179 dout("ceph_state_change TCP_CLOSE\n");
181 dout("ceph_state_change TCP_CLOSE_WAIT\n");
182 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
183 if (test_bit(CONNECTING, &con->state))
184 con->error_msg = "connection failed";
186 con->error_msg = "socket closed";
190 case TCP_ESTABLISHED:
191 dout("ceph_state_change TCP_ESTABLISHED\n");
198 * set up socket callbacks
200 static void set_sock_callbacks(struct socket *sock,
201 struct ceph_connection *con)
203 struct sock *sk = sock->sk;
204 sk->sk_user_data = (void *)con;
205 sk->sk_data_ready = ceph_data_ready;
206 sk->sk_write_space = ceph_write_space;
207 sk->sk_state_change = ceph_state_change;
216 * initiate connection to a remote socket.
218 static struct socket *ceph_tcp_connect(struct ceph_connection *con)
220 struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr;
225 ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
229 sock->sk->sk_allocation = GFP_NOFS;
231 set_sock_callbacks(sock, con);
233 dout("connect %s\n", pr_addr(&con->peer_addr.in_addr));
235 ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK);
236 if (ret == -EINPROGRESS) {
237 dout("connect %s EINPROGRESS sk_state = %u\n",
238 pr_addr(&con->peer_addr.in_addr),
243 pr_err("connect %s error %d\n",
244 pr_addr(&con->peer_addr.in_addr), ret);
247 con->error_msg = "connect error";
255 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
257 struct kvec iov = {buf, len};
258 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
260 return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
264 * write something. @more is true if caller will be sending more data
267 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
268 size_t kvlen, size_t len, int more)
270 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
273 msg.msg_flags |= MSG_MORE;
275 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
277 return kernel_sendmsg(sock, &msg, iov, kvlen, len);
282 * Shutdown/close the socket for the given connection.
284 static int con_close_socket(struct ceph_connection *con)
288 dout("con_close_socket on %p sock %p\n", con, con->sock);
291 set_bit(SOCK_CLOSED, &con->state);
292 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
293 sock_release(con->sock);
295 clear_bit(SOCK_CLOSED, &con->state);
300 * Reset a connection. Discard all incoming and outgoing messages
301 * and clear *_seq state.
303 static void ceph_msg_remove(struct ceph_msg *msg)
305 list_del_init(&msg->list_head);
308 static void ceph_msg_remove_list(struct list_head *head)
310 while (!list_empty(head)) {
311 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
313 ceph_msg_remove(msg);
317 static void reset_connection(struct ceph_connection *con)
319 /* reset connection, out_queue, msg_ and connect_seq */
320 /* discard existing out_queue and msg_seq */
321 ceph_msg_remove_list(&con->out_queue);
322 ceph_msg_remove_list(&con->out_sent);
325 ceph_msg_put(con->in_msg);
329 con->connect_seq = 0;
332 ceph_msg_put(con->out_msg);
339 * mark a peer down. drop any open connections.
341 void ceph_con_close(struct ceph_connection *con)
343 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
344 set_bit(CLOSED, &con->state); /* in case there's queued work */
345 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
346 clear_bit(LOSSYTX, &con->state); /* so we retry next connect */
347 clear_bit(KEEPALIVE_PENDING, &con->state);
348 clear_bit(WRITE_PENDING, &con->state);
349 mutex_lock(&con->mutex);
350 reset_connection(con);
351 cancel_delayed_work(&con->work);
352 mutex_unlock(&con->mutex);
357 * Reopen a closed connection, with a new peer address.
359 void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
361 dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
362 set_bit(OPENING, &con->state);
363 clear_bit(CLOSED, &con->state);
364 memcpy(&con->peer_addr, addr, sizeof(*addr));
365 con->delay = 0; /* reset backoff memory */
370 * return true if this connection ever successfully opened
372 bool ceph_con_opened(struct ceph_connection *con)
374 return con->connect_seq > 0;
380 struct ceph_connection *ceph_con_get(struct ceph_connection *con)
382 dout("con_get %p nref = %d -> %d\n", con,
383 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
384 if (atomic_inc_not_zero(&con->nref))
389 void ceph_con_put(struct ceph_connection *con)
391 dout("con_put %p nref = %d -> %d\n", con,
392 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
393 BUG_ON(atomic_read(&con->nref) == 0);
394 if (atomic_dec_and_test(&con->nref)) {
401 * initialize a new connection.
403 void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
405 dout("con_init %p\n", con);
406 memset(con, 0, sizeof(*con));
407 atomic_set(&con->nref, 1);
409 mutex_init(&con->mutex);
410 INIT_LIST_HEAD(&con->out_queue);
411 INIT_LIST_HEAD(&con->out_sent);
412 INIT_DELAYED_WORK(&con->work, con_work);
417 * We maintain a global counter to order connection attempts. Get
418 * a unique seq greater than @gt.
420 static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
424 spin_lock(&msgr->global_seq_lock);
425 if (msgr->global_seq < gt)
426 msgr->global_seq = gt;
427 ret = ++msgr->global_seq;
428 spin_unlock(&msgr->global_seq_lock);
434 * Prepare footer for currently outgoing message, and finish things
435 * off. Assumes out_kvec* are already valid.. we just add on to the end.
437 static void prepare_write_message_footer(struct ceph_connection *con, int v)
439 struct ceph_msg *m = con->out_msg;
441 dout("prepare_write_message_footer %p\n", con);
442 con->out_kvec_is_msg = true;
443 con->out_kvec[v].iov_base = &m->footer;
444 con->out_kvec[v].iov_len = sizeof(m->footer);
445 con->out_kvec_bytes += sizeof(m->footer);
446 con->out_kvec_left++;
447 con->out_more = m->more_to_follow;
448 con->out_msg_done = true;
452 * Prepare headers for the next outgoing message.
454 static void prepare_write_message(struct ceph_connection *con)
459 con->out_kvec_bytes = 0;
460 con->out_kvec_is_msg = true;
461 con->out_msg_done = false;
463 /* Sneak an ack in there first? If we can get it into the same
464 * TCP packet that's a good thing. */
465 if (con->in_seq > con->in_seq_acked) {
466 con->in_seq_acked = con->in_seq;
467 con->out_kvec[v].iov_base = &tag_ack;
468 con->out_kvec[v++].iov_len = 1;
469 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
470 con->out_kvec[v].iov_base = &con->out_temp_ack;
471 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
472 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
475 m = list_first_entry(&con->out_queue,
476 struct ceph_msg, list_head);
478 if (test_bit(LOSSYTX, &con->state)) {
479 list_del_init(&m->list_head);
481 /* put message on sent list */
483 list_move_tail(&m->list_head, &con->out_sent);
486 m->hdr.seq = cpu_to_le64(++con->out_seq);
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 = CEPH_FEATURE_SUPPORTED;
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;
1112 u64 req_feat = CEPH_FEATURE_REQUIRED;
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 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1221 con->peer_global_seq,
1222 le32_to_cpu(con->in_reply.connect_seq),
1224 WARN_ON(con->connect_seq !=
1225 le32_to_cpu(con->in_reply.connect_seq));
1227 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1228 set_bit(LOSSYTX, &con->state);
1230 prepare_read_tag(con);
1233 case CEPH_MSGR_TAG_WAIT:
1235 * If there is a connection race (we are opening
1236 * connections to each other), one of us may just have
1237 * to WAIT. This shouldn't happen if we are the
1240 pr_err("process_connect peer connecting WAIT\n");
1243 pr_err("connect protocol error, will retry\n");
1244 con->error_msg = "protocol error, garbage tag during connect";
1252 * read (part of) an ack
1254 static int read_partial_ack(struct ceph_connection *con)
1258 return read_partial(con, &to, sizeof(con->in_temp_ack),
1264 * We can finally discard anything that's been acked.
1266 static void process_ack(struct ceph_connection *con)
1269 u64 ack = le64_to_cpu(con->in_temp_ack);
1272 while (!list_empty(&con->out_sent)) {
1273 m = list_first_entry(&con->out_sent, struct ceph_msg,
1275 seq = le64_to_cpu(m->hdr.seq);
1278 dout("got ack for seq %llu type %d at %p\n", seq,
1279 le16_to_cpu(m->hdr.type), m);
1282 prepare_read_tag(con);
1288 static int read_partial_message_section(struct ceph_connection *con,
1289 struct kvec *section, unsigned int sec_len,
1297 while (section->iov_len < sec_len) {
1298 BUG_ON(section->iov_base == NULL);
1299 left = sec_len - section->iov_len;
1300 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1301 section->iov_len, left);
1304 section->iov_len += ret;
1305 if (section->iov_len == sec_len)
1306 *crc = crc32c(0, section->iov_base,
1313 static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1314 struct ceph_msg_header *hdr,
1317 * read (part of) a message.
1319 static int read_partial_message(struct ceph_connection *con)
1321 struct ceph_msg *m = con->in_msg;
1325 unsigned front_len, middle_len, data_len, data_off;
1326 int datacrc = con->msgr->nocrc;
1329 dout("read_partial_message con %p msg %p\n", con, m);
1332 while (con->in_base_pos < sizeof(con->in_hdr)) {
1333 left = sizeof(con->in_hdr) - con->in_base_pos;
1334 ret = ceph_tcp_recvmsg(con->sock,
1335 (char *)&con->in_hdr + con->in_base_pos,
1339 con->in_base_pos += ret;
1340 if (con->in_base_pos == sizeof(con->in_hdr)) {
1341 u32 crc = crc32c(0, (void *)&con->in_hdr,
1342 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1343 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1344 pr_err("read_partial_message bad hdr "
1345 " crc %u != expected %u\n",
1346 crc, con->in_hdr.crc);
1351 front_len = le32_to_cpu(con->in_hdr.front_len);
1352 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1354 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1355 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1357 data_len = le32_to_cpu(con->in_hdr.data_len);
1358 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1360 data_off = le16_to_cpu(con->in_hdr.data_off);
1362 /* allocate message? */
1364 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1365 con->in_hdr.front_len, con->in_hdr.data_len);
1366 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1368 /* skip this message */
1369 dout("alloc_msg returned NULL, skipping message\n");
1370 con->in_base_pos = -front_len - middle_len - data_len -
1372 con->in_tag = CEPH_MSGR_TAG_READY;
1375 if (IS_ERR(con->in_msg)) {
1376 ret = PTR_ERR(con->in_msg);
1379 "error allocating memory for incoming message";
1383 m->front.iov_len = 0; /* haven't read it yet */
1385 m->middle->vec.iov_len = 0;
1387 con->in_msg_pos.page = 0;
1388 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1389 con->in_msg_pos.data_pos = 0;
1393 ret = read_partial_message_section(con, &m->front, front_len,
1394 &con->in_front_crc);
1400 ret = read_partial_message_section(con, &m->middle->vec, middle_len,
1401 &con->in_middle_crc);
1407 while (con->in_msg_pos.data_pos < data_len) {
1408 left = min((int)(data_len - con->in_msg_pos.data_pos),
1409 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1410 BUG_ON(m->pages == NULL);
1411 p = kmap(m->pages[con->in_msg_pos.page]);
1412 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1414 if (ret > 0 && datacrc)
1416 crc32c(con->in_data_crc,
1417 p + con->in_msg_pos.page_pos, ret);
1418 kunmap(m->pages[con->in_msg_pos.page]);
1421 con->in_msg_pos.data_pos += ret;
1422 con->in_msg_pos.page_pos += ret;
1423 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1424 con->in_msg_pos.page_pos = 0;
1425 con->in_msg_pos.page++;
1430 to = sizeof(m->hdr) + sizeof(m->footer);
1431 while (con->in_base_pos < to) {
1432 left = to - con->in_base_pos;
1433 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1434 (con->in_base_pos - sizeof(m->hdr)),
1438 con->in_base_pos += ret;
1440 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1441 m, front_len, m->footer.front_crc, middle_len,
1442 m->footer.middle_crc, data_len, m->footer.data_crc);
1445 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1446 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1447 m, con->in_front_crc, m->footer.front_crc);
1450 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1451 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1452 m, con->in_middle_crc, m->footer.middle_crc);
1456 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1457 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1458 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1459 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1463 return 1; /* done! */
1467 * Process message. This happens in the worker thread. The callback should
1468 * be careful not to do anything that waits on other incoming messages or it
1471 static void process_message(struct ceph_connection *con)
1473 struct ceph_msg *msg;
1478 /* if first message, set peer_name */
1479 if (con->peer_name.type == 0)
1480 con->peer_name = msg->hdr.src.name;
1483 mutex_unlock(&con->mutex);
1485 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1486 msg, le64_to_cpu(msg->hdr.seq),
1487 ENTITY_NAME(msg->hdr.src.name),
1488 le16_to_cpu(msg->hdr.type),
1489 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1490 le32_to_cpu(msg->hdr.front_len),
1491 le32_to_cpu(msg->hdr.data_len),
1492 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1493 con->ops->dispatch(con, msg);
1495 mutex_lock(&con->mutex);
1496 prepare_read_tag(con);
1501 * Write something to the socket. Called in a worker thread when the
1502 * socket appears to be writeable and we have something ready to send.
1504 static int try_write(struct ceph_connection *con)
1506 struct ceph_messenger *msgr = con->msgr;
1509 dout("try_write start %p state %lu nref %d\n", con, con->state,
1510 atomic_read(&con->nref));
1512 mutex_lock(&con->mutex);
1514 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1516 /* open the socket first? */
1517 if (con->sock == NULL) {
1519 * if we were STANDBY and are reconnecting _this_
1520 * connection, bump connect_seq now. Always bump
1523 if (test_and_clear_bit(STANDBY, &con->state))
1526 prepare_write_banner(msgr, con);
1527 prepare_write_connect(msgr, con, 1);
1528 prepare_read_banner(con);
1529 set_bit(CONNECTING, &con->state);
1530 clear_bit(NEGOTIATING, &con->state);
1532 BUG_ON(con->in_msg);
1533 con->in_tag = CEPH_MSGR_TAG_READY;
1534 dout("try_write initiating connect on %p new state %lu\n",
1536 con->sock = ceph_tcp_connect(con);
1537 if (IS_ERR(con->sock)) {
1539 con->error_msg = "connect error";
1546 /* kvec data queued? */
1547 if (con->out_skip) {
1548 ret = write_partial_skip(con);
1552 dout("try_write write_partial_skip err %d\n", ret);
1556 if (con->out_kvec_left) {
1557 ret = write_partial_kvec(con);
1564 if (con->out_msg_done) {
1565 ceph_msg_put(con->out_msg);
1566 con->out_msg = NULL; /* we're done with this one */
1570 ret = write_partial_msg_pages(con);
1572 goto more_kvec; /* we need to send the footer, too! */
1576 dout("try_write write_partial_msg_pages err %d\n",
1583 if (!test_bit(CONNECTING, &con->state)) {
1584 /* is anything else pending? */
1585 if (!list_empty(&con->out_queue)) {
1586 prepare_write_message(con);
1589 if (con->in_seq > con->in_seq_acked) {
1590 prepare_write_ack(con);
1593 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1594 prepare_write_keepalive(con);
1599 /* Nothing to do! */
1600 clear_bit(WRITE_PENDING, &con->state);
1601 dout("try_write nothing else to write.\n");
1605 mutex_unlock(&con->mutex);
1606 dout("try_write done on %p\n", con);
1613 * Read what we can from the socket.
1615 static int try_read(struct ceph_connection *con)
1617 struct ceph_messenger *msgr;
1623 if (test_bit(STANDBY, &con->state))
1626 dout("try_read start on %p\n", con);
1629 mutex_lock(&con->mutex);
1632 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1634 if (test_bit(CONNECTING, &con->state)) {
1635 if (!test_bit(NEGOTIATING, &con->state)) {
1636 dout("try_read connecting\n");
1637 ret = read_partial_banner(con);
1640 if (process_banner(con) < 0) {
1645 ret = read_partial_connect(con);
1648 if (process_connect(con) < 0) {
1655 if (con->in_base_pos < 0) {
1657 * skipping + discarding content.
1659 * FIXME: there must be a better way to do this!
1661 static char buf[1024];
1662 int skip = min(1024, -con->in_base_pos);
1663 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1664 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1667 con->in_base_pos += ret;
1668 if (con->in_base_pos)
1671 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1675 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1678 dout("try_read got tag %d\n", (int)con->in_tag);
1679 switch (con->in_tag) {
1680 case CEPH_MSGR_TAG_MSG:
1681 prepare_read_message(con);
1683 case CEPH_MSGR_TAG_ACK:
1684 prepare_read_ack(con);
1686 case CEPH_MSGR_TAG_CLOSE:
1687 set_bit(CLOSED, &con->state); /* fixme */
1693 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1694 ret = read_partial_message(con);
1698 con->error_msg = "bad crc";
1702 con->error_msg = "io error";
1708 if (con->in_tag == CEPH_MSGR_TAG_READY)
1710 process_message(con);
1713 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1714 ret = read_partial_ack(con);
1724 mutex_unlock(&con->mutex);
1725 dout("try_read done on %p\n", con);
1729 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1730 con->error_msg = "protocol error, garbage tag";
1737 * Atomically queue work on a connection. Bump @con reference to
1738 * avoid races with connection teardown.
1740 * There is some trickery going on with QUEUED and BUSY because we
1741 * only want a _single_ thread operating on each connection at any
1742 * point in time, but we want to use all available CPUs.
1744 * The worker thread only proceeds if it can atomically set BUSY. It
1745 * clears QUEUED and does it's thing. When it thinks it's done, it
1746 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1747 * (tries again to set BUSY).
1749 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1750 * try to queue work. If that fails (work is already queued, or BUSY)
1751 * we give up (work also already being done or is queued) but leave QUEUED
1752 * set so that the worker thread will loop if necessary.
1754 static void queue_con(struct ceph_connection *con)
1756 if (test_bit(DEAD, &con->state)) {
1757 dout("queue_con %p ignoring: DEAD\n",
1762 if (!con->ops->get(con)) {
1763 dout("queue_con %p ref count 0\n", con);
1767 set_bit(QUEUED, &con->state);
1768 if (test_bit(BUSY, &con->state)) {
1769 dout("queue_con %p - already BUSY\n", con);
1771 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1772 dout("queue_con %p - already queued\n", con);
1775 dout("queue_con %p\n", con);
1780 * Do some work on a connection. Drop a connection ref when we're done.
1782 static void con_work(struct work_struct *work)
1784 struct ceph_connection *con = container_of(work, struct ceph_connection,
1789 if (test_and_set_bit(BUSY, &con->state) != 0) {
1790 dout("con_work %p BUSY already set\n", con);
1793 dout("con_work %p start, clearing QUEUED\n", con);
1794 clear_bit(QUEUED, &con->state);
1796 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1797 dout("con_work CLOSED\n");
1798 con_close_socket(con);
1801 if (test_and_clear_bit(OPENING, &con->state)) {
1802 /* reopen w/ new peer */
1803 dout("con_work OPENING\n");
1804 con_close_socket(con);
1807 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1808 try_read(con) < 0 ||
1809 try_write(con) < 0) {
1811 ceph_fault(con); /* error/fault path */
1815 clear_bit(BUSY, &con->state);
1816 dout("con->state=%lu\n", con->state);
1817 if (test_bit(QUEUED, &con->state)) {
1818 if (!backoff || test_bit(OPENING, &con->state)) {
1819 dout("con_work %p QUEUED reset, looping\n", con);
1822 dout("con_work %p QUEUED reset, but just faulted\n", con);
1823 clear_bit(QUEUED, &con->state);
1825 dout("con_work %p done\n", con);
1833 * Generic error/fault handler. A retry mechanism is used with
1834 * exponential backoff
1836 static void ceph_fault(struct ceph_connection *con)
1838 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1839 pr_addr(&con->peer_addr.in_addr), con->error_msg);
1840 dout("fault %p state %lu to peer %s\n",
1841 con, con->state, pr_addr(&con->peer_addr.in_addr));
1843 if (test_bit(LOSSYTX, &con->state)) {
1844 dout("fault on LOSSYTX channel\n");
1848 mutex_lock(&con->mutex);
1849 if (test_bit(CLOSED, &con->state))
1852 con_close_socket(con);
1855 ceph_msg_put(con->in_msg);
1859 /* Requeue anything that hasn't been acked */
1860 list_splice_init(&con->out_sent, &con->out_queue);
1862 /* If there are no messages in the queue, place the connection
1863 * in a STANDBY state (i.e., don't try to reconnect just yet). */
1864 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1865 dout("fault setting STANDBY\n");
1866 set_bit(STANDBY, &con->state);
1868 /* retry after a delay. */
1869 if (con->delay == 0)
1870 con->delay = BASE_DELAY_INTERVAL;
1871 else if (con->delay < MAX_DELAY_INTERVAL)
1873 dout("fault queueing %p delay %lu\n", con, con->delay);
1875 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1876 round_jiffies_relative(con->delay)) == 0)
1881 mutex_unlock(&con->mutex);
1884 * in case we faulted due to authentication, invalidate our
1885 * current tickets so that we can get new ones.
1887 if (con->auth_retry && con->ops->invalidate_authorizer) {
1888 dout("calling invalidate_authorizer()\n");
1889 con->ops->invalidate_authorizer(con);
1892 if (con->ops->fault)
1893 con->ops->fault(con);
1899 * create a new messenger instance
1901 struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1903 struct ceph_messenger *msgr;
1905 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1907 return ERR_PTR(-ENOMEM);
1909 spin_lock_init(&msgr->global_seq_lock);
1911 /* the zero page is needed if a request is "canceled" while the message
1912 * is being written over the socket */
1913 msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1914 if (!msgr->zero_page) {
1916 return ERR_PTR(-ENOMEM);
1918 kmap(msgr->zero_page);
1921 msgr->inst.addr = *myaddr;
1923 /* select a random nonce */
1924 msgr->inst.addr.type = 0;
1925 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
1926 encode_my_addr(msgr);
1928 dout("messenger_create %p\n", msgr);
1932 void ceph_messenger_destroy(struct ceph_messenger *msgr)
1934 dout("destroy %p\n", msgr);
1935 kunmap(msgr->zero_page);
1936 __free_page(msgr->zero_page);
1938 dout("destroyed messenger %p\n", msgr);
1942 * Queue up an outgoing message on the given connection.
1944 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1946 if (test_bit(CLOSED, &con->state)) {
1947 dout("con_send %p closed, dropping %p\n", con, msg);
1953 msg->hdr.src.name = con->msgr->inst.name;
1954 msg->hdr.src.addr = con->msgr->my_enc_addr;
1955 msg->hdr.orig_src = msg->hdr.src;
1957 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
1960 mutex_lock(&con->mutex);
1961 BUG_ON(!list_empty(&msg->list_head));
1962 list_add_tail(&msg->list_head, &con->out_queue);
1963 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
1964 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
1965 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1966 le32_to_cpu(msg->hdr.front_len),
1967 le32_to_cpu(msg->hdr.middle_len),
1968 le32_to_cpu(msg->hdr.data_len));
1969 mutex_unlock(&con->mutex);
1971 /* if there wasn't anything waiting to send before, queue
1973 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1978 * Revoke a message that was previously queued for send
1980 void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
1982 mutex_lock(&con->mutex);
1983 if (!list_empty(&msg->list_head)) {
1984 dout("con_revoke %p msg %p\n", con, msg);
1985 list_del_init(&msg->list_head);
1988 if (con->out_msg == msg) {
1989 ceph_msg_put(con->out_msg);
1990 con->out_msg = NULL;
1992 if (con->out_kvec_is_msg) {
1993 con->out_skip = con->out_kvec_bytes;
1994 con->out_kvec_is_msg = false;
1997 dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
1999 mutex_unlock(&con->mutex);
2003 * Revoke a message that we may be reading data into
2005 void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
2007 mutex_lock(&con->mutex);
2008 if (con->in_msg && con->in_msg == msg) {
2009 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2010 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
2011 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2013 /* skip rest of message */
2014 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
2015 con->in_base_pos = con->in_base_pos -
2016 sizeof(struct ceph_msg_header) -
2020 sizeof(struct ceph_msg_footer);
2021 ceph_msg_put(con->in_msg);
2023 con->in_tag = CEPH_MSGR_TAG_READY;
2025 dout("con_revoke_pages %p msg %p pages %p no-op\n",
2026 con, con->in_msg, msg);
2028 mutex_unlock(&con->mutex);
2032 * Queue a keepalive byte to ensure the tcp connection is alive.
2034 void ceph_con_keepalive(struct ceph_connection *con)
2036 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2037 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2043 * construct a new message with given type, size
2044 * the new msg has a ref count of 1.
2046 struct ceph_msg *ceph_msg_new(int type, int front_len,
2047 int page_len, int page_off, struct page **pages)
2051 m = kmalloc(sizeof(*m), GFP_NOFS);
2054 kref_init(&m->kref);
2055 INIT_LIST_HEAD(&m->list_head);
2057 m->hdr.type = cpu_to_le16(type);
2058 m->hdr.front_len = cpu_to_le32(front_len);
2059 m->hdr.middle_len = 0;
2060 m->hdr.data_len = cpu_to_le32(page_len);
2061 m->hdr.data_off = cpu_to_le16(page_off);
2062 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2063 m->footer.front_crc = 0;
2064 m->footer.middle_crc = 0;
2065 m->footer.data_crc = 0;
2066 m->front_max = front_len;
2067 m->front_is_vmalloc = false;
2068 m->more_to_follow = false;
2073 if (front_len > PAGE_CACHE_SIZE) {
2074 m->front.iov_base = __vmalloc(front_len, GFP_NOFS,
2076 m->front_is_vmalloc = true;
2078 m->front.iov_base = kmalloc(front_len, GFP_NOFS);
2080 if (m->front.iov_base == NULL) {
2081 pr_err("msg_new can't allocate %d bytes\n",
2086 m->front.iov_base = NULL;
2088 m->front.iov_len = front_len;
2094 m->nr_pages = calc_pages_for(page_off, page_len);
2098 dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len,
2105 pr_err("msg_new can't create type %d len %d\n", type, front_len);
2106 return ERR_PTR(-ENOMEM);
2110 * Allocate "middle" portion of a message, if it is needed and wasn't
2111 * allocated by alloc_msg. This allows us to read a small fixed-size
2112 * per-type header in the front and then gracefully fail (i.e.,
2113 * propagate the error to the caller based on info in the front) when
2114 * the middle is too large.
2116 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
2118 int type = le16_to_cpu(msg->hdr.type);
2119 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2121 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2122 ceph_msg_type_name(type), middle_len);
2123 BUG_ON(!middle_len);
2124 BUG_ON(msg->middle);
2126 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
2133 * Generic message allocator, for incoming messages.
2135 static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2136 struct ceph_msg_header *hdr,
2139 int type = le16_to_cpu(hdr->type);
2140 int front_len = le32_to_cpu(hdr->front_len);
2141 int middle_len = le32_to_cpu(hdr->middle_len);
2142 struct ceph_msg *msg = NULL;
2145 if (con->ops->alloc_msg) {
2146 mutex_unlock(&con->mutex);
2147 msg = con->ops->alloc_msg(con, hdr, skip);
2148 mutex_lock(&con->mutex);
2157 msg = ceph_msg_new(type, front_len, 0, 0, NULL);
2159 pr_err("unable to allocate msg type %d len %d\n",
2161 return ERR_PTR(-ENOMEM);
2164 memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
2167 ret = ceph_alloc_middle(con, msg);
2180 * Free a generically kmalloc'd message.
2182 void ceph_msg_kfree(struct ceph_msg *m)
2184 dout("msg_kfree %p\n", m);
2185 if (m->front_is_vmalloc)
2186 vfree(m->front.iov_base);
2188 kfree(m->front.iov_base);
2193 * Drop a msg ref. Destroy as needed.
2195 void ceph_msg_last_put(struct kref *kref)
2197 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
2199 dout("ceph_msg_put last one on %p\n", m);
2200 WARN_ON(!list_empty(&m->list_head));
2202 /* drop middle, data, if any */
2204 ceph_buffer_put(m->middle);
2211 ceph_pagelist_release(m->pagelist);
2217 ceph_msgpool_put(m->pool, m);
2222 void ceph_msg_dump(struct ceph_msg *msg)
2224 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2225 msg->front_max, msg->nr_pages);
2226 print_hex_dump(KERN_DEBUG, "header: ",
2227 DUMP_PREFIX_OFFSET, 16, 1,
2228 &msg->hdr, sizeof(msg->hdr), true);
2229 print_hex_dump(KERN_DEBUG, " front: ",
2230 DUMP_PREFIX_OFFSET, 16, 1,
2231 msg->front.iov_base, msg->front.iov_len, true);
2233 print_hex_dump(KERN_DEBUG, "middle: ",
2234 DUMP_PREFIX_OFFSET, 16, 1,
2235 msg->middle->vec.iov_base,
2236 msg->middle->vec.iov_len, true);
2237 print_hex_dump(KERN_DEBUG, "footer: ",
2238 DUMP_PREFIX_OFFSET, 16, 1,
2239 &msg->footer, sizeof(msg->footer), true);