1 #include <linux/ceph/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>
12 #include <linux/bio.h>
13 #include <linux/blkdev.h>
14 #include <linux/dns_resolver.h>
17 #include <linux/ceph/libceph.h>
18 #include <linux/ceph/messenger.h>
19 #include <linux/ceph/decode.h>
20 #include <linux/ceph/pagelist.h>
21 #include <linux/export.h>
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
32 /* State values for ceph_connection->sock_state; NEW is assumed to be 0 */
34 #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
35 #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
36 #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
37 #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
38 #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
40 /* static tag bytes (protocol control messages) */
41 static char tag_msg = CEPH_MSGR_TAG_MSG;
42 static char tag_ack = CEPH_MSGR_TAG_ACK;
43 static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
46 static struct lock_class_key socket_class;
50 * When skipping (ignoring) a block of input we read it into a "skip
51 * buffer," which is this many bytes in size.
53 #define SKIP_BUF_SIZE 1024
55 static void queue_con(struct ceph_connection *con);
56 static void con_work(struct work_struct *);
57 static void ceph_fault(struct ceph_connection *con);
60 * Nicely render a sockaddr as a string. An array of formatted
61 * strings is used, to approximate reentrancy.
63 #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
64 #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
65 #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
66 #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
68 static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
69 static atomic_t addr_str_seq = ATOMIC_INIT(0);
71 static struct page *zero_page; /* used in certain error cases */
73 const char *ceph_pr_addr(const struct sockaddr_storage *ss)
77 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
78 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
80 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
83 switch (ss->ss_family) {
85 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
86 ntohs(in4->sin_port));
90 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
91 ntohs(in6->sin6_port));
95 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
101 EXPORT_SYMBOL(ceph_pr_addr);
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 static struct workqueue_struct *ceph_msgr_wq;
114 void _ceph_msgr_exit(void)
117 destroy_workqueue(ceph_msgr_wq);
121 BUG_ON(zero_page == NULL);
123 page_cache_release(zero_page);
127 int ceph_msgr_init(void)
129 BUG_ON(zero_page != NULL);
130 zero_page = ZERO_PAGE(0);
131 page_cache_get(zero_page);
133 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
137 pr_err("msgr_init failed to create workqueue\n");
142 EXPORT_SYMBOL(ceph_msgr_init);
144 void ceph_msgr_exit(void)
146 BUG_ON(ceph_msgr_wq == NULL);
150 EXPORT_SYMBOL(ceph_msgr_exit);
152 void ceph_msgr_flush(void)
154 flush_workqueue(ceph_msgr_wq);
156 EXPORT_SYMBOL(ceph_msgr_flush);
158 /* Connection socket state transition functions */
160 static void con_sock_state_init(struct ceph_connection *con)
164 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
165 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
166 printk("%s: unexpected old state %d\n", __func__, old_state);
169 static void con_sock_state_connecting(struct ceph_connection *con)
173 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
174 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
175 printk("%s: unexpected old state %d\n", __func__, old_state);
178 static void con_sock_state_connected(struct ceph_connection *con)
182 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
183 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
184 printk("%s: unexpected old state %d\n", __func__, old_state);
187 static void con_sock_state_closing(struct ceph_connection *con)
191 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
192 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
193 old_state != CON_SOCK_STATE_CONNECTED &&
194 old_state != CON_SOCK_STATE_CLOSING))
195 printk("%s: unexpected old state %d\n", __func__, old_state);
198 static void con_sock_state_closed(struct ceph_connection *con)
202 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
203 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
204 old_state != CON_SOCK_STATE_CLOSING))
205 printk("%s: unexpected old state %d\n", __func__, old_state);
209 * socket callback functions
212 /* data available on socket, or listen socket received a connect */
213 static void ceph_sock_data_ready(struct sock *sk, int count_unused)
215 struct ceph_connection *con = sk->sk_user_data;
217 if (sk->sk_state != TCP_CLOSE_WAIT) {
218 dout("%s on %p state = %lu, queueing work\n", __func__,
224 /* socket has buffer space for writing */
225 static void ceph_sock_write_space(struct sock *sk)
227 struct ceph_connection *con = sk->sk_user_data;
229 /* only queue to workqueue if there is data we want to write,
230 * and there is sufficient space in the socket buffer to accept
231 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
232 * doesn't get called again until try_write() fills the socket
233 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
234 * and net/core/stream.c:sk_stream_write_space().
236 if (test_bit(WRITE_PENDING, &con->flags)) {
237 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
238 dout("%s %p queueing write work\n", __func__, con);
239 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
243 dout("%s %p nothing to write\n", __func__, con);
247 /* socket's state has changed */
248 static void ceph_sock_state_change(struct sock *sk)
250 struct ceph_connection *con = sk->sk_user_data;
252 dout("%s %p state = %lu sk_state = %u\n", __func__,
253 con, con->state, sk->sk_state);
255 if (test_bit(CLOSED, &con->state))
258 switch (sk->sk_state) {
260 dout("%s TCP_CLOSE\n", __func__);
262 dout("%s TCP_CLOSE_WAIT\n", __func__);
263 con_sock_state_closing(con);
264 if (test_and_set_bit(SOCK_CLOSED, &con->flags) == 0) {
265 if (test_bit(CONNECTING, &con->state))
266 con->error_msg = "connection failed";
268 con->error_msg = "socket closed";
272 case TCP_ESTABLISHED:
273 dout("%s TCP_ESTABLISHED\n", __func__);
274 con_sock_state_connected(con);
277 default: /* Everything else is uninteresting */
283 * set up socket callbacks
285 static void set_sock_callbacks(struct socket *sock,
286 struct ceph_connection *con)
288 struct sock *sk = sock->sk;
289 sk->sk_user_data = con;
290 sk->sk_data_ready = ceph_sock_data_ready;
291 sk->sk_write_space = ceph_sock_write_space;
292 sk->sk_state_change = ceph_sock_state_change;
301 * initiate connection to a remote socket.
303 static int ceph_tcp_connect(struct ceph_connection *con)
305 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
310 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
314 sock->sk->sk_allocation = GFP_NOFS;
316 #ifdef CONFIG_LOCKDEP
317 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
320 set_sock_callbacks(sock, con);
322 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
324 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
326 if (ret == -EINPROGRESS) {
327 dout("connect %s EINPROGRESS sk_state = %u\n",
328 ceph_pr_addr(&con->peer_addr.in_addr),
330 } else if (ret < 0) {
331 pr_err("connect %s error %d\n",
332 ceph_pr_addr(&con->peer_addr.in_addr), ret);
334 con->error_msg = "connect error";
339 con_sock_state_connecting(con);
344 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
346 struct kvec iov = {buf, len};
347 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
350 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
357 * write something. @more is true if caller will be sending more data
360 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
361 size_t kvlen, size_t len, int more)
363 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
367 msg.msg_flags |= MSG_MORE;
369 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
371 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
377 static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
378 int offset, size_t size, int more)
380 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
383 ret = kernel_sendpage(sock, page, offset, size, flags);
392 * Shutdown/close the socket for the given connection.
394 static int con_close_socket(struct ceph_connection *con)
398 dout("con_close_socket on %p sock %p\n", con, con->sock);
401 set_bit(SOCK_CLOSED, &con->state);
402 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
403 sock_release(con->sock);
405 clear_bit(SOCK_CLOSED, &con->state);
406 con_sock_state_closed(con);
411 * Reset a connection. Discard all incoming and outgoing messages
412 * and clear *_seq state.
414 static void ceph_msg_remove(struct ceph_msg *msg)
416 list_del_init(&msg->list_head);
419 static void ceph_msg_remove_list(struct list_head *head)
421 while (!list_empty(head)) {
422 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
424 ceph_msg_remove(msg);
428 static void reset_connection(struct ceph_connection *con)
430 /* reset connection, out_queue, msg_ and connect_seq */
431 /* discard existing out_queue and msg_seq */
432 ceph_msg_remove_list(&con->out_queue);
433 ceph_msg_remove_list(&con->out_sent);
436 ceph_msg_put(con->in_msg);
440 con->connect_seq = 0;
443 ceph_msg_put(con->out_msg);
447 con->in_seq_acked = 0;
451 * mark a peer down. drop any open connections.
453 void ceph_con_close(struct ceph_connection *con)
455 dout("con_close %p peer %s\n", con,
456 ceph_pr_addr(&con->peer_addr.in_addr));
457 clear_bit(NEGOTIATING, &con->state);
458 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
459 set_bit(CLOSED, &con->state);
461 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
462 clear_bit(KEEPALIVE_PENDING, &con->flags);
463 clear_bit(WRITE_PENDING, &con->flags);
465 mutex_lock(&con->mutex);
466 reset_connection(con);
467 con->peer_global_seq = 0;
468 cancel_delayed_work(&con->work);
469 mutex_unlock(&con->mutex);
472 EXPORT_SYMBOL(ceph_con_close);
475 * Reopen a closed connection, with a new peer address.
477 void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
479 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
480 set_bit(OPENING, &con->state);
481 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
483 memcpy(&con->peer_addr, addr, sizeof(*addr));
484 con->delay = 0; /* reset backoff memory */
487 EXPORT_SYMBOL(ceph_con_open);
490 * return true if this connection ever successfully opened
492 bool ceph_con_opened(struct ceph_connection *con)
494 return con->connect_seq > 0;
500 struct ceph_connection *ceph_con_get(struct ceph_connection *con)
502 int nref = __atomic_add_unless(&con->nref, 1, 0);
504 dout("con_get %p nref = %d -> %d\n", con, nref, nref + 1);
506 return nref ? con : NULL;
509 void ceph_con_put(struct ceph_connection *con)
511 int nref = atomic_dec_return(&con->nref);
518 dout("con_put %p nref = %d -> %d\n", con, nref + 1, nref);
522 * initialize a new connection.
524 void ceph_con_init(struct ceph_connection *con, void *private,
525 const struct ceph_connection_operations *ops,
526 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
528 dout("con_init %p\n", con);
529 memset(con, 0, sizeof(*con));
530 con->private = private;
532 atomic_set(&con->nref, 1);
535 con_sock_state_init(con);
537 con->peer_name.type = (__u8) entity_type;
538 con->peer_name.num = cpu_to_le64(entity_num);
540 mutex_init(&con->mutex);
541 INIT_LIST_HEAD(&con->out_queue);
542 INIT_LIST_HEAD(&con->out_sent);
543 INIT_DELAYED_WORK(&con->work, con_work);
545 set_bit(CLOSED, &con->state);
547 EXPORT_SYMBOL(ceph_con_init);
551 * We maintain a global counter to order connection attempts. Get
552 * a unique seq greater than @gt.
554 static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
558 spin_lock(&msgr->global_seq_lock);
559 if (msgr->global_seq < gt)
560 msgr->global_seq = gt;
561 ret = ++msgr->global_seq;
562 spin_unlock(&msgr->global_seq_lock);
566 static void con_out_kvec_reset(struct ceph_connection *con)
568 con->out_kvec_left = 0;
569 con->out_kvec_bytes = 0;
570 con->out_kvec_cur = &con->out_kvec[0];
573 static void con_out_kvec_add(struct ceph_connection *con,
574 size_t size, void *data)
578 index = con->out_kvec_left;
579 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
581 con->out_kvec[index].iov_len = size;
582 con->out_kvec[index].iov_base = data;
583 con->out_kvec_left++;
584 con->out_kvec_bytes += size;
588 * Prepare footer for currently outgoing message, and finish things
589 * off. Assumes out_kvec* are already valid.. we just add on to the end.
591 static void prepare_write_message_footer(struct ceph_connection *con)
593 struct ceph_msg *m = con->out_msg;
594 int v = con->out_kvec_left;
596 dout("prepare_write_message_footer %p\n", con);
597 con->out_kvec_is_msg = true;
598 con->out_kvec[v].iov_base = &m->footer;
599 con->out_kvec[v].iov_len = sizeof(m->footer);
600 con->out_kvec_bytes += sizeof(m->footer);
601 con->out_kvec_left++;
602 con->out_more = m->more_to_follow;
603 con->out_msg_done = true;
607 * Prepare headers for the next outgoing message.
609 static void prepare_write_message(struct ceph_connection *con)
614 con_out_kvec_reset(con);
615 con->out_kvec_is_msg = true;
616 con->out_msg_done = false;
618 /* Sneak an ack in there first? If we can get it into the same
619 * TCP packet that's a good thing. */
620 if (con->in_seq > con->in_seq_acked) {
621 con->in_seq_acked = con->in_seq;
622 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
623 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
624 con_out_kvec_add(con, sizeof (con->out_temp_ack),
628 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
631 /* put message on sent list */
633 list_move_tail(&m->list_head, &con->out_sent);
636 * only assign outgoing seq # if we haven't sent this message
637 * yet. if it is requeued, resend with it's original seq.
639 if (m->needs_out_seq) {
640 m->hdr.seq = cpu_to_le64(++con->out_seq);
641 m->needs_out_seq = false;
644 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
645 m, con->out_seq, le16_to_cpu(m->hdr.type),
646 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
647 le32_to_cpu(m->hdr.data_len),
649 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
651 /* tag + hdr + front + middle */
652 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
653 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
654 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
657 con_out_kvec_add(con, m->middle->vec.iov_len,
658 m->middle->vec.iov_base);
660 /* fill in crc (except data pages), footer */
661 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
662 con->out_msg->hdr.crc = cpu_to_le32(crc);
663 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
665 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
666 con->out_msg->footer.front_crc = cpu_to_le32(crc);
668 crc = crc32c(0, m->middle->vec.iov_base,
669 m->middle->vec.iov_len);
670 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
672 con->out_msg->footer.middle_crc = 0;
673 con->out_msg->footer.data_crc = 0;
674 dout("prepare_write_message front_crc %u data_crc %u\n",
675 le32_to_cpu(con->out_msg->footer.front_crc),
676 le32_to_cpu(con->out_msg->footer.middle_crc));
678 /* is there a data payload? */
679 if (le32_to_cpu(m->hdr.data_len) > 0) {
680 /* initialize page iterator */
681 con->out_msg_pos.page = 0;
683 con->out_msg_pos.page_pos = m->page_alignment;
685 con->out_msg_pos.page_pos = 0;
686 con->out_msg_pos.data_pos = 0;
687 con->out_msg_pos.did_page_crc = false;
688 con->out_more = 1; /* data + footer will follow */
690 /* no, queue up footer too and be done */
691 prepare_write_message_footer(con);
694 set_bit(WRITE_PENDING, &con->flags);
700 static void prepare_write_ack(struct ceph_connection *con)
702 dout("prepare_write_ack %p %llu -> %llu\n", con,
703 con->in_seq_acked, con->in_seq);
704 con->in_seq_acked = con->in_seq;
706 con_out_kvec_reset(con);
708 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
710 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
711 con_out_kvec_add(con, sizeof (con->out_temp_ack),
714 con->out_more = 1; /* more will follow.. eventually.. */
715 set_bit(WRITE_PENDING, &con->flags);
719 * Prepare to write keepalive byte.
721 static void prepare_write_keepalive(struct ceph_connection *con)
723 dout("prepare_write_keepalive %p\n", con);
724 con_out_kvec_reset(con);
725 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
726 set_bit(WRITE_PENDING, &con->flags);
730 * Connection negotiation.
733 static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
736 struct ceph_auth_handshake *auth;
738 if (!con->ops->get_authorizer) {
739 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
740 con->out_connect.authorizer_len = 0;
745 /* Can't hold the mutex while getting authorizer */
747 mutex_unlock(&con->mutex);
749 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
751 mutex_lock(&con->mutex);
755 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
756 return ERR_PTR(-EAGAIN);
758 con->auth_reply_buf = auth->authorizer_reply_buf;
759 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
766 * We connected to a peer and are saying hello.
768 static void prepare_write_banner(struct ceph_connection *con)
770 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
771 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
772 &con->msgr->my_enc_addr);
775 set_bit(WRITE_PENDING, &con->flags);
778 static int prepare_write_connect(struct ceph_connection *con)
780 unsigned global_seq = get_global_seq(con->msgr, 0);
783 struct ceph_auth_handshake *auth;
785 switch (con->peer_name.type) {
786 case CEPH_ENTITY_TYPE_MON:
787 proto = CEPH_MONC_PROTOCOL;
789 case CEPH_ENTITY_TYPE_OSD:
790 proto = CEPH_OSDC_PROTOCOL;
792 case CEPH_ENTITY_TYPE_MDS:
793 proto = CEPH_MDSC_PROTOCOL;
799 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
800 con->connect_seq, global_seq, proto);
802 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
803 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
804 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
805 con->out_connect.global_seq = cpu_to_le32(global_seq);
806 con->out_connect.protocol_version = cpu_to_le32(proto);
807 con->out_connect.flags = 0;
809 auth_proto = CEPH_AUTH_UNKNOWN;
810 auth = get_connect_authorizer(con, &auth_proto);
812 return PTR_ERR(auth);
814 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
815 con->out_connect.authorizer_len = auth ?
816 cpu_to_le32(auth->authorizer_buf_len) : 0;
818 con_out_kvec_add(con, sizeof (con->out_connect),
820 if (auth && auth->authorizer_buf_len)
821 con_out_kvec_add(con, auth->authorizer_buf_len,
822 auth->authorizer_buf);
825 set_bit(WRITE_PENDING, &con->flags);
831 * write as much of pending kvecs to the socket as we can.
833 * 0 -> socket full, but more to do
836 static int write_partial_kvec(struct ceph_connection *con)
840 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
841 while (con->out_kvec_bytes > 0) {
842 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
843 con->out_kvec_left, con->out_kvec_bytes,
847 con->out_kvec_bytes -= ret;
848 if (con->out_kvec_bytes == 0)
851 /* account for full iov entries consumed */
852 while (ret >= con->out_kvec_cur->iov_len) {
853 BUG_ON(!con->out_kvec_left);
854 ret -= con->out_kvec_cur->iov_len;
856 con->out_kvec_left--;
858 /* and for a partially-consumed entry */
860 con->out_kvec_cur->iov_len -= ret;
861 con->out_kvec_cur->iov_base += ret;
864 con->out_kvec_left = 0;
865 con->out_kvec_is_msg = false;
868 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
869 con->out_kvec_bytes, con->out_kvec_left, ret);
870 return ret; /* done! */
874 static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
885 static void iter_bio_next(struct bio **bio_iter, int *seg)
887 if (*bio_iter == NULL)
890 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
893 if (*seg == (*bio_iter)->bi_vcnt)
894 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
899 * Write as much message data payload as we can. If we finish, queue
901 * 1 -> done, footer is now queued in out_kvec[].
902 * 0 -> socket full, but more to do
905 static int write_partial_msg_pages(struct ceph_connection *con)
907 struct ceph_msg *msg = con->out_msg;
908 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
910 bool do_datacrc = !con->msgr->nocrc;
914 size_t trail_len = (msg->trail ? msg->trail->length : 0);
916 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
917 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
918 con->out_msg_pos.page_pos);
921 if (msg->bio && !msg->bio_iter)
922 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
925 while (data_len > con->out_msg_pos.data_pos) {
926 struct page *page = NULL;
927 int max_write = PAGE_SIZE;
930 total_max_write = data_len - trail_len -
931 con->out_msg_pos.data_pos;
934 * if we are calculating the data crc (the default), we need
935 * to map the page. if our pages[] has been revoked, use the
939 /* have we reached the trail part of the data? */
940 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
943 total_max_write = data_len - con->out_msg_pos.data_pos;
945 page = list_first_entry(&msg->trail->head,
947 max_write = PAGE_SIZE;
948 } else if (msg->pages) {
949 page = msg->pages[con->out_msg_pos.page];
950 } else if (msg->pagelist) {
951 page = list_first_entry(&msg->pagelist->head,
954 } else if (msg->bio) {
957 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
959 bio_offset = bv->bv_offset;
960 max_write = bv->bv_len;
965 len = min_t(int, max_write - con->out_msg_pos.page_pos,
968 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
971 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
975 BUG_ON(kaddr == NULL);
976 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
977 crc = crc32c(tmpcrc, base, len);
978 con->out_msg->footer.data_crc = cpu_to_le32(crc);
979 con->out_msg_pos.did_page_crc = true;
981 ret = ceph_tcp_sendpage(con->sock, page,
982 con->out_msg_pos.page_pos + bio_offset,
991 con->out_msg_pos.data_pos += ret;
992 con->out_msg_pos.page_pos += ret;
994 con->out_msg_pos.page_pos = 0;
995 con->out_msg_pos.page++;
996 con->out_msg_pos.did_page_crc = false;
998 list_move_tail(&page->lru,
1000 else if (msg->pagelist)
1001 list_move_tail(&page->lru,
1002 &msg->pagelist->head);
1005 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
1010 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1012 /* prepare and queue up footer, too */
1014 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
1015 con_out_kvec_reset(con);
1016 prepare_write_message_footer(con);
1025 static int write_partial_skip(struct ceph_connection *con)
1029 while (con->out_skip > 0) {
1030 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
1032 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
1035 con->out_skip -= ret;
1043 * Prepare to read connection handshake, or an ack.
1045 static void prepare_read_banner(struct ceph_connection *con)
1047 dout("prepare_read_banner %p\n", con);
1048 con->in_base_pos = 0;
1051 static void prepare_read_connect(struct ceph_connection *con)
1053 dout("prepare_read_connect %p\n", con);
1054 con->in_base_pos = 0;
1057 static void prepare_read_ack(struct ceph_connection *con)
1059 dout("prepare_read_ack %p\n", con);
1060 con->in_base_pos = 0;
1063 static void prepare_read_tag(struct ceph_connection *con)
1065 dout("prepare_read_tag %p\n", con);
1066 con->in_base_pos = 0;
1067 con->in_tag = CEPH_MSGR_TAG_READY;
1071 * Prepare to read a message.
1073 static int prepare_read_message(struct ceph_connection *con)
1075 dout("prepare_read_message %p\n", con);
1076 BUG_ON(con->in_msg != NULL);
1077 con->in_base_pos = 0;
1078 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1083 static int read_partial(struct ceph_connection *con,
1084 int end, int size, void *object)
1086 while (con->in_base_pos < end) {
1087 int left = end - con->in_base_pos;
1088 int have = size - left;
1089 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1092 con->in_base_pos += ret;
1099 * Read all or part of the connect-side handshake on a new connection
1101 static int read_partial_banner(struct ceph_connection *con)
1107 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
1110 size = strlen(CEPH_BANNER);
1112 ret = read_partial(con, end, size, con->in_banner);
1116 size = sizeof (con->actual_peer_addr);
1118 ret = read_partial(con, end, size, &con->actual_peer_addr);
1122 size = sizeof (con->peer_addr_for_me);
1124 ret = read_partial(con, end, size, &con->peer_addr_for_me);
1132 static int read_partial_connect(struct ceph_connection *con)
1138 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1140 size = sizeof (con->in_reply);
1142 ret = read_partial(con, end, size, &con->in_reply);
1146 size = le32_to_cpu(con->in_reply.authorizer_len);
1148 ret = read_partial(con, end, size, con->auth_reply_buf);
1152 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1153 con, (int)con->in_reply.tag,
1154 le32_to_cpu(con->in_reply.connect_seq),
1155 le32_to_cpu(con->in_reply.global_seq));
1162 * Verify the hello banner looks okay.
1164 static int verify_hello(struct ceph_connection *con)
1166 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
1167 pr_err("connect to %s got bad banner\n",
1168 ceph_pr_addr(&con->peer_addr.in_addr));
1169 con->error_msg = "protocol error, bad banner";
1175 static bool addr_is_blank(struct sockaddr_storage *ss)
1177 switch (ss->ss_family) {
1179 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1182 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1183 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1184 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1185 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1190 static int addr_port(struct sockaddr_storage *ss)
1192 switch (ss->ss_family) {
1194 return ntohs(((struct sockaddr_in *)ss)->sin_port);
1196 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
1201 static void addr_set_port(struct sockaddr_storage *ss, int p)
1203 switch (ss->ss_family) {
1205 ((struct sockaddr_in *)ss)->sin_port = htons(p);
1208 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
1214 * Unlike other *_pton function semantics, zero indicates success.
1216 static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1217 char delim, const char **ipend)
1219 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1220 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
1222 memset(ss, 0, sizeof(*ss));
1224 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1225 ss->ss_family = AF_INET;
1229 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1230 ss->ss_family = AF_INET6;
1238 * Extract hostname string and resolve using kernel DNS facility.
1240 #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1241 static int ceph_dns_resolve_name(const char *name, size_t namelen,
1242 struct sockaddr_storage *ss, char delim, const char **ipend)
1244 const char *end, *delim_p;
1245 char *colon_p, *ip_addr = NULL;
1249 * The end of the hostname occurs immediately preceding the delimiter or
1250 * the port marker (':') where the delimiter takes precedence.
1252 delim_p = memchr(name, delim, namelen);
1253 colon_p = memchr(name, ':', namelen);
1255 if (delim_p && colon_p)
1256 end = delim_p < colon_p ? delim_p : colon_p;
1257 else if (!delim_p && colon_p)
1261 if (!end) /* case: hostname:/ */
1262 end = name + namelen;
1268 /* do dns_resolve upcall */
1269 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1271 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1279 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1280 ret, ret ? "failed" : ceph_pr_addr(ss));
1285 static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1286 struct sockaddr_storage *ss, char delim, const char **ipend)
1293 * Parse a server name (IP or hostname). If a valid IP address is not found
1294 * then try to extract a hostname to resolve using userspace DNS upcall.
1296 static int ceph_parse_server_name(const char *name, size_t namelen,
1297 struct sockaddr_storage *ss, char delim, const char **ipend)
1301 ret = ceph_pton(name, namelen, ss, delim, ipend);
1303 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1309 * Parse an ip[:port] list into an addr array. Use the default
1310 * monitor port if a port isn't specified.
1312 int ceph_parse_ips(const char *c, const char *end,
1313 struct ceph_entity_addr *addr,
1314 int max_count, int *count)
1316 int i, ret = -EINVAL;
1319 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1320 for (i = 0; i < max_count; i++) {
1322 struct sockaddr_storage *ss = &addr[i].in_addr;
1331 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1340 dout("missing matching ']'\n");
1347 if (p < end && *p == ':') {
1350 while (p < end && *p >= '0' && *p <= '9') {
1351 port = (port * 10) + (*p - '0');
1354 if (port > 65535 || port == 0)
1357 port = CEPH_MON_PORT;
1360 addr_set_port(ss, port);
1362 dout("parse_ips got %s\n", ceph_pr_addr(ss));
1379 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
1382 EXPORT_SYMBOL(ceph_parse_ips);
1384 static int process_banner(struct ceph_connection *con)
1386 dout("process_banner on %p\n", con);
1388 if (verify_hello(con) < 0)
1391 ceph_decode_addr(&con->actual_peer_addr);
1392 ceph_decode_addr(&con->peer_addr_for_me);
1395 * Make sure the other end is who we wanted. note that the other
1396 * end may not yet know their ip address, so if it's 0.0.0.0, give
1397 * them the benefit of the doubt.
1399 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1400 sizeof(con->peer_addr)) != 0 &&
1401 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1402 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
1403 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
1404 ceph_pr_addr(&con->peer_addr.in_addr),
1405 (int)le32_to_cpu(con->peer_addr.nonce),
1406 ceph_pr_addr(&con->actual_peer_addr.in_addr),
1407 (int)le32_to_cpu(con->actual_peer_addr.nonce));
1408 con->error_msg = "wrong peer at address";
1413 * did we learn our address?
1415 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1416 int port = addr_port(&con->msgr->inst.addr.in_addr);
1418 memcpy(&con->msgr->inst.addr.in_addr,
1419 &con->peer_addr_for_me.in_addr,
1420 sizeof(con->peer_addr_for_me.in_addr));
1421 addr_set_port(&con->msgr->inst.addr.in_addr, port);
1422 encode_my_addr(con->msgr);
1423 dout("process_banner learned my addr is %s\n",
1424 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
1427 set_bit(NEGOTIATING, &con->state);
1428 prepare_read_connect(con);
1432 static void fail_protocol(struct ceph_connection *con)
1434 reset_connection(con);
1435 set_bit(CLOSED, &con->state); /* in case there's queued work */
1438 static int process_connect(struct ceph_connection *con)
1440 u64 sup_feat = con->msgr->supported_features;
1441 u64 req_feat = con->msgr->required_features;
1442 u64 server_feat = le64_to_cpu(con->in_reply.features);
1445 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1447 switch (con->in_reply.tag) {
1448 case CEPH_MSGR_TAG_FEATURES:
1449 pr_err("%s%lld %s feature set mismatch,"
1450 " my %llx < server's %llx, missing %llx\n",
1451 ENTITY_NAME(con->peer_name),
1452 ceph_pr_addr(&con->peer_addr.in_addr),
1453 sup_feat, server_feat, server_feat & ~sup_feat);
1454 con->error_msg = "missing required protocol features";
1458 case CEPH_MSGR_TAG_BADPROTOVER:
1459 pr_err("%s%lld %s protocol version mismatch,"
1460 " my %d != server's %d\n",
1461 ENTITY_NAME(con->peer_name),
1462 ceph_pr_addr(&con->peer_addr.in_addr),
1463 le32_to_cpu(con->out_connect.protocol_version),
1464 le32_to_cpu(con->in_reply.protocol_version));
1465 con->error_msg = "protocol version mismatch";
1469 case CEPH_MSGR_TAG_BADAUTHORIZER:
1471 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1473 if (con->auth_retry == 2) {
1474 con->error_msg = "connect authorization failure";
1477 con->auth_retry = 1;
1478 con_out_kvec_reset(con);
1479 ret = prepare_write_connect(con);
1482 prepare_read_connect(con);
1485 case CEPH_MSGR_TAG_RESETSESSION:
1487 * If we connected with a large connect_seq but the peer
1488 * has no record of a session with us (no connection, or
1489 * connect_seq == 0), they will send RESETSESION to indicate
1490 * that they must have reset their session, and may have
1493 dout("process_connect got RESET peer seq %u\n",
1494 le32_to_cpu(con->in_connect.connect_seq));
1495 pr_err("%s%lld %s connection reset\n",
1496 ENTITY_NAME(con->peer_name),
1497 ceph_pr_addr(&con->peer_addr.in_addr));
1498 reset_connection(con);
1499 con_out_kvec_reset(con);
1500 ret = prepare_write_connect(con);
1503 prepare_read_connect(con);
1505 /* Tell ceph about it. */
1506 mutex_unlock(&con->mutex);
1507 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1508 if (con->ops->peer_reset)
1509 con->ops->peer_reset(con);
1510 mutex_lock(&con->mutex);
1511 if (test_bit(CLOSED, &con->state) ||
1512 test_bit(OPENING, &con->state))
1516 case CEPH_MSGR_TAG_RETRY_SESSION:
1518 * If we sent a smaller connect_seq than the peer has, try
1519 * again with a larger value.
1521 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1522 le32_to_cpu(con->out_connect.connect_seq),
1523 le32_to_cpu(con->in_connect.connect_seq));
1524 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
1525 con_out_kvec_reset(con);
1526 ret = prepare_write_connect(con);
1529 prepare_read_connect(con);
1532 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1534 * If we sent a smaller global_seq than the peer has, try
1535 * again with a larger value.
1537 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
1538 con->peer_global_seq,
1539 le32_to_cpu(con->in_connect.global_seq));
1540 get_global_seq(con->msgr,
1541 le32_to_cpu(con->in_connect.global_seq));
1542 con_out_kvec_reset(con);
1543 ret = prepare_write_connect(con);
1546 prepare_read_connect(con);
1549 case CEPH_MSGR_TAG_READY:
1550 if (req_feat & ~server_feat) {
1551 pr_err("%s%lld %s protocol feature mismatch,"
1552 " my required %llx > server's %llx, need %llx\n",
1553 ENTITY_NAME(con->peer_name),
1554 ceph_pr_addr(&con->peer_addr.in_addr),
1555 req_feat, server_feat, req_feat & ~server_feat);
1556 con->error_msg = "missing required protocol features";
1560 clear_bit(CONNECTING, &con->state);
1561 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1563 con->peer_features = server_feat;
1564 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1565 con->peer_global_seq,
1566 le32_to_cpu(con->in_reply.connect_seq),
1568 WARN_ON(con->connect_seq !=
1569 le32_to_cpu(con->in_reply.connect_seq));
1571 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1572 set_bit(LOSSYTX, &con->flags);
1574 prepare_read_tag(con);
1577 case CEPH_MSGR_TAG_WAIT:
1579 * If there is a connection race (we are opening
1580 * connections to each other), one of us may just have
1581 * to WAIT. This shouldn't happen if we are the
1584 pr_err("process_connect got WAIT as client\n");
1585 con->error_msg = "protocol error, got WAIT as client";
1589 pr_err("connect protocol error, will retry\n");
1590 con->error_msg = "protocol error, garbage tag during connect";
1598 * read (part of) an ack
1600 static int read_partial_ack(struct ceph_connection *con)
1602 int size = sizeof (con->in_temp_ack);
1605 return read_partial(con, end, size, &con->in_temp_ack);
1610 * We can finally discard anything that's been acked.
1612 static void process_ack(struct ceph_connection *con)
1615 u64 ack = le64_to_cpu(con->in_temp_ack);
1618 while (!list_empty(&con->out_sent)) {
1619 m = list_first_entry(&con->out_sent, struct ceph_msg,
1621 seq = le64_to_cpu(m->hdr.seq);
1624 dout("got ack for seq %llu type %d at %p\n", seq,
1625 le16_to_cpu(m->hdr.type), m);
1626 m->ack_stamp = jiffies;
1629 prepare_read_tag(con);
1635 static int read_partial_message_section(struct ceph_connection *con,
1636 struct kvec *section,
1637 unsigned int sec_len, u32 *crc)
1643 while (section->iov_len < sec_len) {
1644 BUG_ON(section->iov_base == NULL);
1645 left = sec_len - section->iov_len;
1646 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1647 section->iov_len, left);
1650 section->iov_len += ret;
1652 if (section->iov_len == sec_len)
1653 *crc = crc32c(0, section->iov_base, section->iov_len);
1658 static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1659 struct ceph_msg_header *hdr);
1662 static int read_partial_message_pages(struct ceph_connection *con,
1663 struct page **pages,
1664 unsigned data_len, bool do_datacrc)
1670 left = min((int)(data_len - con->in_msg_pos.data_pos),
1671 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1673 BUG_ON(pages == NULL);
1674 p = kmap(pages[con->in_msg_pos.page]);
1675 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1677 if (ret > 0 && do_datacrc)
1679 crc32c(con->in_data_crc,
1680 p + con->in_msg_pos.page_pos, ret);
1681 kunmap(pages[con->in_msg_pos.page]);
1684 con->in_msg_pos.data_pos += ret;
1685 con->in_msg_pos.page_pos += ret;
1686 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1687 con->in_msg_pos.page_pos = 0;
1688 con->in_msg_pos.page++;
1695 static int read_partial_message_bio(struct ceph_connection *con,
1696 struct bio **bio_iter, int *bio_seg,
1697 unsigned data_len, bool do_datacrc)
1699 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1706 left = min((int)(data_len - con->in_msg_pos.data_pos),
1707 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1709 p = kmap(bv->bv_page) + bv->bv_offset;
1711 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1713 if (ret > 0 && do_datacrc)
1715 crc32c(con->in_data_crc,
1716 p + con->in_msg_pos.page_pos, ret);
1717 kunmap(bv->bv_page);
1720 con->in_msg_pos.data_pos += ret;
1721 con->in_msg_pos.page_pos += ret;
1722 if (con->in_msg_pos.page_pos == bv->bv_len) {
1723 con->in_msg_pos.page_pos = 0;
1724 iter_bio_next(bio_iter, bio_seg);
1732 * read (part of) a message.
1734 static int read_partial_message(struct ceph_connection *con)
1736 struct ceph_msg *m = con->in_msg;
1740 unsigned front_len, middle_len, data_len;
1741 bool do_datacrc = !con->msgr->nocrc;
1745 dout("read_partial_message con %p msg %p\n", con, m);
1748 size = sizeof (con->in_hdr);
1750 ret = read_partial(con, end, size, &con->in_hdr);
1754 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1755 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1756 pr_err("read_partial_message bad hdr "
1757 " crc %u != expected %u\n",
1758 crc, con->in_hdr.crc);
1762 front_len = le32_to_cpu(con->in_hdr.front_len);
1763 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1765 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1766 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1768 data_len = le32_to_cpu(con->in_hdr.data_len);
1769 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1773 seq = le64_to_cpu(con->in_hdr.seq);
1774 if ((s64)seq - (s64)con->in_seq < 1) {
1775 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
1776 ENTITY_NAME(con->peer_name),
1777 ceph_pr_addr(&con->peer_addr.in_addr),
1778 seq, con->in_seq + 1);
1779 con->in_base_pos = -front_len - middle_len - data_len -
1781 con->in_tag = CEPH_MSGR_TAG_READY;
1783 } else if ((s64)seq - (s64)con->in_seq > 1) {
1784 pr_err("read_partial_message bad seq %lld expected %lld\n",
1785 seq, con->in_seq + 1);
1786 con->error_msg = "bad message sequence # for incoming message";
1790 /* allocate message? */
1792 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1793 con->in_hdr.front_len, con->in_hdr.data_len);
1794 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
1795 /* skip this message */
1796 dout("alloc_msg said skip message\n");
1797 BUG_ON(con->in_msg);
1798 con->in_base_pos = -front_len - middle_len - data_len -
1800 con->in_tag = CEPH_MSGR_TAG_READY;
1806 "error allocating memory for incoming message";
1810 m->front.iov_len = 0; /* haven't read it yet */
1812 m->middle->vec.iov_len = 0;
1814 con->in_msg_pos.page = 0;
1816 con->in_msg_pos.page_pos = m->page_alignment;
1818 con->in_msg_pos.page_pos = 0;
1819 con->in_msg_pos.data_pos = 0;
1823 ret = read_partial_message_section(con, &m->front, front_len,
1824 &con->in_front_crc);
1830 ret = read_partial_message_section(con, &m->middle->vec,
1832 &con->in_middle_crc);
1837 if (m->bio && !m->bio_iter)
1838 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1842 while (con->in_msg_pos.data_pos < data_len) {
1844 ret = read_partial_message_pages(con, m->pages,
1845 data_len, do_datacrc);
1849 } else if (m->bio) {
1851 ret = read_partial_message_bio(con,
1852 &m->bio_iter, &m->bio_seg,
1853 data_len, do_datacrc);
1863 size = sizeof (m->footer);
1865 ret = read_partial(con, end, size, &m->footer);
1869 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1870 m, front_len, m->footer.front_crc, middle_len,
1871 m->footer.middle_crc, data_len, m->footer.data_crc);
1874 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1875 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1876 m, con->in_front_crc, m->footer.front_crc);
1879 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1880 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1881 m, con->in_middle_crc, m->footer.middle_crc);
1885 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1886 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1887 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1888 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1892 return 1; /* done! */
1896 * Process message. This happens in the worker thread. The callback should
1897 * be careful not to do anything that waits on other incoming messages or it
1900 static void process_message(struct ceph_connection *con)
1902 struct ceph_msg *msg;
1907 /* if first message, set peer_name */
1908 if (con->peer_name.type == 0)
1909 con->peer_name = msg->hdr.src;
1912 mutex_unlock(&con->mutex);
1914 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1915 msg, le64_to_cpu(msg->hdr.seq),
1916 ENTITY_NAME(msg->hdr.src),
1917 le16_to_cpu(msg->hdr.type),
1918 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1919 le32_to_cpu(msg->hdr.front_len),
1920 le32_to_cpu(msg->hdr.data_len),
1921 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1922 con->ops->dispatch(con, msg);
1924 mutex_lock(&con->mutex);
1925 prepare_read_tag(con);
1930 * Write something to the socket. Called in a worker thread when the
1931 * socket appears to be writeable and we have something ready to send.
1933 static int try_write(struct ceph_connection *con)
1937 dout("try_write start %p state %lu nref %d\n", con, con->state,
1938 atomic_read(&con->nref));
1941 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1943 /* open the socket first? */
1944 if (con->sock == NULL) {
1945 clear_bit(NEGOTIATING, &con->state);
1946 set_bit(CONNECTING, &con->state);
1948 con_out_kvec_reset(con);
1949 prepare_write_banner(con);
1950 ret = prepare_write_connect(con);
1953 prepare_read_banner(con);
1955 BUG_ON(con->in_msg);
1956 con->in_tag = CEPH_MSGR_TAG_READY;
1957 dout("try_write initiating connect on %p new state %lu\n",
1959 ret = ceph_tcp_connect(con);
1961 con->error_msg = "connect error";
1967 /* kvec data queued? */
1968 if (con->out_skip) {
1969 ret = write_partial_skip(con);
1973 if (con->out_kvec_left) {
1974 ret = write_partial_kvec(con);
1981 if (con->out_msg_done) {
1982 ceph_msg_put(con->out_msg);
1983 con->out_msg = NULL; /* we're done with this one */
1987 ret = write_partial_msg_pages(con);
1989 goto more_kvec; /* we need to send the footer, too! */
1993 dout("try_write write_partial_msg_pages err %d\n",
2000 if (!test_bit(CONNECTING, &con->state)) {
2001 /* is anything else pending? */
2002 if (!list_empty(&con->out_queue)) {
2003 prepare_write_message(con);
2006 if (con->in_seq > con->in_seq_acked) {
2007 prepare_write_ack(con);
2010 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
2011 prepare_write_keepalive(con);
2016 /* Nothing to do! */
2017 clear_bit(WRITE_PENDING, &con->flags);
2018 dout("try_write nothing else to write.\n");
2021 dout("try_write done on %p ret %d\n", con, ret);
2028 * Read what we can from the socket.
2030 static int try_read(struct ceph_connection *con)
2037 if (test_bit(STANDBY, &con->state))
2040 dout("try_read start on %p\n", con);
2043 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2047 * process_connect and process_message drop and re-take
2048 * con->mutex. make sure we handle a racing close or reopen.
2050 if (test_bit(CLOSED, &con->state) ||
2051 test_bit(OPENING, &con->state)) {
2056 if (test_bit(CONNECTING, &con->state)) {
2057 if (!test_bit(NEGOTIATING, &con->state)) {
2058 dout("try_read connecting\n");
2059 ret = read_partial_banner(con);
2062 ret = process_banner(con);
2066 ret = read_partial_connect(con);
2069 ret = process_connect(con);
2075 if (con->in_base_pos < 0) {
2077 * skipping + discarding content.
2079 * FIXME: there must be a better way to do this!
2081 static char buf[SKIP_BUF_SIZE];
2082 int skip = min((int) sizeof (buf), -con->in_base_pos);
2084 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2085 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2088 con->in_base_pos += ret;
2089 if (con->in_base_pos)
2092 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2096 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2099 dout("try_read got tag %d\n", (int)con->in_tag);
2100 switch (con->in_tag) {
2101 case CEPH_MSGR_TAG_MSG:
2102 prepare_read_message(con);
2104 case CEPH_MSGR_TAG_ACK:
2105 prepare_read_ack(con);
2107 case CEPH_MSGR_TAG_CLOSE:
2108 set_bit(CLOSED, &con->state); /* fixme */
2114 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2115 ret = read_partial_message(con);
2119 con->error_msg = "bad crc";
2123 con->error_msg = "io error";
2128 if (con->in_tag == CEPH_MSGR_TAG_READY)
2130 process_message(con);
2133 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2134 ret = read_partial_ack(con);
2142 dout("try_read done on %p ret %d\n", con, ret);
2146 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2147 con->error_msg = "protocol error, garbage tag";
2154 * Atomically queue work on a connection. Bump @con reference to
2155 * avoid races with connection teardown.
2157 static void queue_con(struct ceph_connection *con)
2159 if (!con->ops->get(con)) {
2160 dout("queue_con %p ref count 0\n", con);
2164 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
2165 dout("queue_con %p - already queued\n", con);
2168 dout("queue_con %p\n", con);
2173 * Do some work on a connection. Drop a connection ref when we're done.
2175 static void con_work(struct work_struct *work)
2177 struct ceph_connection *con = container_of(work, struct ceph_connection,
2181 mutex_lock(&con->mutex);
2183 if (test_and_clear_bit(BACKOFF, &con->flags)) {
2184 dout("con_work %p backing off\n", con);
2185 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2186 round_jiffies_relative(con->delay))) {
2187 dout("con_work %p backoff %lu\n", con, con->delay);
2188 mutex_unlock(&con->mutex);
2192 dout("con_work %p FAILED to back off %lu\n", con,
2197 if (test_bit(STANDBY, &con->state)) {
2198 dout("con_work %p STANDBY\n", con);
2201 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2202 dout("con_work CLOSED\n");
2203 con_close_socket(con);
2206 if (test_and_clear_bit(OPENING, &con->state)) {
2207 /* reopen w/ new peer */
2208 dout("con_work OPENING\n");
2209 con_close_socket(con);
2212 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
2215 ret = try_read(con);
2221 ret = try_write(con);
2228 mutex_unlock(&con->mutex);
2234 mutex_unlock(&con->mutex);
2235 ceph_fault(con); /* error/fault path */
2241 * Generic error/fault handler. A retry mechanism is used with
2242 * exponential backoff
2244 static void ceph_fault(struct ceph_connection *con)
2246 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2247 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2248 dout("fault %p state %lu to peer %s\n",
2249 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
2251 if (test_bit(LOSSYTX, &con->flags)) {
2252 dout("fault on LOSSYTX channel\n");
2256 mutex_lock(&con->mutex);
2257 if (test_bit(CLOSED, &con->state))
2260 con_close_socket(con);
2263 ceph_msg_put(con->in_msg);
2267 /* Requeue anything that hasn't been acked */
2268 list_splice_init(&con->out_sent, &con->out_queue);
2270 /* If there are no messages queued or keepalive pending, place
2271 * the connection in a STANDBY state */
2272 if (list_empty(&con->out_queue) &&
2273 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
2274 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
2275 clear_bit(WRITE_PENDING, &con->flags);
2276 set_bit(STANDBY, &con->state);
2278 /* retry after a delay. */
2279 if (con->delay == 0)
2280 con->delay = BASE_DELAY_INTERVAL;
2281 else if (con->delay < MAX_DELAY_INTERVAL)
2284 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2285 round_jiffies_relative(con->delay))) {
2286 dout("fault queued %p delay %lu\n", con, con->delay);
2289 dout("fault failed to queue %p delay %lu, backoff\n",
2292 * In many cases we see a socket state change
2293 * while con_work is running and end up
2294 * queuing (non-delayed) work, such that we
2295 * can't backoff with a delay. Set a flag so
2296 * that when con_work restarts we schedule the
2299 set_bit(BACKOFF, &con->flags);
2304 mutex_unlock(&con->mutex);
2307 * in case we faulted due to authentication, invalidate our
2308 * current tickets so that we can get new ones.
2310 if (con->auth_retry && con->ops->invalidate_authorizer) {
2311 dout("calling invalidate_authorizer()\n");
2312 con->ops->invalidate_authorizer(con);
2315 if (con->ops->fault)
2316 con->ops->fault(con);
2322 * initialize a new messenger instance
2324 void ceph_messenger_init(struct ceph_messenger *msgr,
2325 struct ceph_entity_addr *myaddr,
2326 u32 supported_features,
2327 u32 required_features,
2330 msgr->supported_features = supported_features;
2331 msgr->required_features = required_features;
2333 spin_lock_init(&msgr->global_seq_lock);
2336 msgr->inst.addr = *myaddr;
2338 /* select a random nonce */
2339 msgr->inst.addr.type = 0;
2340 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
2341 encode_my_addr(msgr);
2342 msgr->nocrc = nocrc;
2344 dout("%s %p\n", __func__, msgr);
2346 EXPORT_SYMBOL(ceph_messenger_init);
2348 static void clear_standby(struct ceph_connection *con)
2350 /* come back from STANDBY? */
2351 if (test_and_clear_bit(STANDBY, &con->state)) {
2352 mutex_lock(&con->mutex);
2353 dout("clear_standby %p and ++connect_seq\n", con);
2355 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2356 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
2357 mutex_unlock(&con->mutex);
2362 * Queue up an outgoing message on the given connection.
2364 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2366 if (test_bit(CLOSED, &con->state)) {
2367 dout("con_send %p closed, dropping %p\n", con, msg);
2373 msg->hdr.src = con->msgr->inst.name;
2375 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2377 msg->needs_out_seq = true;
2380 mutex_lock(&con->mutex);
2381 BUG_ON(!list_empty(&msg->list_head));
2382 list_add_tail(&msg->list_head, &con->out_queue);
2383 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2384 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2385 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2386 le32_to_cpu(msg->hdr.front_len),
2387 le32_to_cpu(msg->hdr.middle_len),
2388 le32_to_cpu(msg->hdr.data_len));
2389 mutex_unlock(&con->mutex);
2391 /* if there wasn't anything waiting to send before, queue
2394 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
2397 EXPORT_SYMBOL(ceph_con_send);
2400 * Revoke a message that was previously queued for send
2402 void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2404 mutex_lock(&con->mutex);
2405 if (!list_empty(&msg->list_head)) {
2406 dout("con_revoke %p msg %p - was on queue\n", con, msg);
2407 list_del_init(&msg->list_head);
2411 if (con->out_msg == msg) {
2412 dout("con_revoke %p msg %p - was sending\n", con, msg);
2413 con->out_msg = NULL;
2414 if (con->out_kvec_is_msg) {
2415 con->out_skip = con->out_kvec_bytes;
2416 con->out_kvec_is_msg = false;
2421 mutex_unlock(&con->mutex);
2425 * Revoke a message that we may be reading data into
2427 void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
2429 mutex_lock(&con->mutex);
2430 if (con->in_msg && con->in_msg == msg) {
2431 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2432 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
2433 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2435 /* skip rest of message */
2436 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
2437 con->in_base_pos = con->in_base_pos -
2438 sizeof(struct ceph_msg_header) -
2442 sizeof(struct ceph_msg_footer);
2443 ceph_msg_put(con->in_msg);
2445 con->in_tag = CEPH_MSGR_TAG_READY;
2448 dout("con_revoke_pages %p msg %p pages %p no-op\n",
2449 con, con->in_msg, msg);
2451 mutex_unlock(&con->mutex);
2455 * Queue a keepalive byte to ensure the tcp connection is alive.
2457 void ceph_con_keepalive(struct ceph_connection *con)
2459 dout("con_keepalive %p\n", con);
2461 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2462 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
2465 EXPORT_SYMBOL(ceph_con_keepalive);
2469 * construct a new message with given type, size
2470 * the new msg has a ref count of 1.
2472 struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2477 m = kmalloc(sizeof(*m), flags);
2480 kref_init(&m->kref);
2481 INIT_LIST_HEAD(&m->list_head);
2484 m->hdr.type = cpu_to_le16(type);
2485 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2487 m->hdr.front_len = cpu_to_le32(front_len);
2488 m->hdr.middle_len = 0;
2489 m->hdr.data_len = 0;
2490 m->hdr.data_off = 0;
2491 m->hdr.reserved = 0;
2492 m->footer.front_crc = 0;
2493 m->footer.middle_crc = 0;
2494 m->footer.data_crc = 0;
2495 m->footer.flags = 0;
2496 m->front_max = front_len;
2497 m->front_is_vmalloc = false;
2498 m->more_to_follow = false;
2507 m->page_alignment = 0;
2517 if (front_len > PAGE_CACHE_SIZE) {
2518 m->front.iov_base = __vmalloc(front_len, flags,
2520 m->front_is_vmalloc = true;
2522 m->front.iov_base = kmalloc(front_len, flags);
2524 if (m->front.iov_base == NULL) {
2525 dout("ceph_msg_new can't allocate %d bytes\n",
2530 m->front.iov_base = NULL;
2532 m->front.iov_len = front_len;
2534 dout("ceph_msg_new %p front %d\n", m, front_len);
2541 pr_err("msg_new can't create type %d front %d\n", type,
2545 dout("msg_new can't create type %d front %d\n", type,
2550 EXPORT_SYMBOL(ceph_msg_new);
2553 * Allocate "middle" portion of a message, if it is needed and wasn't
2554 * allocated by alloc_msg. This allows us to read a small fixed-size
2555 * per-type header in the front and then gracefully fail (i.e.,
2556 * propagate the error to the caller based on info in the front) when
2557 * the middle is too large.
2559 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
2561 int type = le16_to_cpu(msg->hdr.type);
2562 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2564 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2565 ceph_msg_type_name(type), middle_len);
2566 BUG_ON(!middle_len);
2567 BUG_ON(msg->middle);
2569 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
2576 * Allocate a message for receiving an incoming message on a
2577 * connection, and save the result in con->in_msg. Uses the
2578 * connection's private alloc_msg op if available.
2580 * Returns true if the message should be skipped, false otherwise.
2581 * If true is returned (skip message), con->in_msg will be NULL.
2582 * If false is returned, con->in_msg will contain a pointer to the
2583 * newly-allocated message, or NULL in case of memory exhaustion.
2585 static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2586 struct ceph_msg_header *hdr)
2588 int type = le16_to_cpu(hdr->type);
2589 int front_len = le32_to_cpu(hdr->front_len);
2590 int middle_len = le32_to_cpu(hdr->middle_len);
2593 BUG_ON(con->in_msg != NULL);
2595 if (con->ops->alloc_msg) {
2598 mutex_unlock(&con->mutex);
2599 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
2600 mutex_lock(&con->mutex);
2608 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2610 pr_err("unable to allocate msg type %d len %d\n",
2614 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
2616 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
2618 if (middle_len && !con->in_msg->middle) {
2619 ret = ceph_alloc_middle(con, con->in_msg);
2621 ceph_msg_put(con->in_msg);
2631 * Free a generically kmalloc'd message.
2633 void ceph_msg_kfree(struct ceph_msg *m)
2635 dout("msg_kfree %p\n", m);
2636 if (m->front_is_vmalloc)
2637 vfree(m->front.iov_base);
2639 kfree(m->front.iov_base);
2644 * Drop a msg ref. Destroy as needed.
2646 void ceph_msg_last_put(struct kref *kref)
2648 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
2650 dout("ceph_msg_put last one on %p\n", m);
2651 WARN_ON(!list_empty(&m->list_head));
2653 /* drop middle, data, if any */
2655 ceph_buffer_put(m->middle);
2662 ceph_pagelist_release(m->pagelist);
2670 ceph_msgpool_put(m->pool, m);
2674 EXPORT_SYMBOL(ceph_msg_last_put);
2676 void ceph_msg_dump(struct ceph_msg *msg)
2678 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2679 msg->front_max, msg->nr_pages);
2680 print_hex_dump(KERN_DEBUG, "header: ",
2681 DUMP_PREFIX_OFFSET, 16, 1,
2682 &msg->hdr, sizeof(msg->hdr), true);
2683 print_hex_dump(KERN_DEBUG, " front: ",
2684 DUMP_PREFIX_OFFSET, 16, 1,
2685 msg->front.iov_base, msg->front.iov_len, true);
2687 print_hex_dump(KERN_DEBUG, "middle: ",
2688 DUMP_PREFIX_OFFSET, 16, 1,
2689 msg->middle->vec.iov_base,
2690 msg->middle->vec.iov_len, true);
2691 print_hex_dump(KERN_DEBUG, "footer: ",
2692 DUMP_PREFIX_OFFSET, 16, 1,
2693 &msg->footer, sizeof(msg->footer), true);
2695 EXPORT_SYMBOL(ceph_msg_dump);