1 #include "ceph_debug.h"
3 #include <linux/crc32c.h>
4 #include <linux/ctype.h>
5 #include <linux/highmem.h>
6 #include <linux/inet.h>
7 #include <linux/kthread.h>
9 #include <linux/slab.h>
10 #include <linux/socket.h>
11 #include <linux/string.h>
15 #include "messenger.h"
20 * Ceph uses the messenger to exchange ceph_msg messages with other
21 * hosts in the system. The messenger provides ordered and reliable
22 * delivery. We tolerate TCP disconnects by reconnecting (with
23 * exponential backoff) in the case of a fault (disconnection, bad
24 * crc, protocol error). Acks allow sent messages to be discarded by
28 /* static tag bytes (protocol control messages) */
29 static char tag_msg
= CEPH_MSGR_TAG_MSG
;
30 static char tag_ack
= CEPH_MSGR_TAG_ACK
;
31 static char tag_keepalive
= CEPH_MSGR_TAG_KEEPALIVE
;
34 static struct lock_class_key socket_class
;
38 static void queue_con(struct ceph_connection
*con
);
39 static void con_work(struct work_struct
*);
40 static void ceph_fault(struct ceph_connection
*con
);
43 * nicely render a sockaddr as a string.
45 #define MAX_ADDR_STR 20
46 static char addr_str
[MAX_ADDR_STR
][40];
47 static DEFINE_SPINLOCK(addr_str_lock
);
48 static int last_addr_str
;
50 const char *pr_addr(const struct sockaddr_storage
*ss
)
54 struct sockaddr_in
*in4
= (void *)ss
;
55 unsigned char *quad
= (void *)&in4
->sin_addr
.s_addr
;
56 struct sockaddr_in6
*in6
= (void *)ss
;
58 spin_lock(&addr_str_lock
);
60 if (last_addr_str
== MAX_ADDR_STR
)
62 spin_unlock(&addr_str_lock
);
65 switch (ss
->ss_family
) {
67 sprintf(s
, "%u.%u.%u.%u:%u",
68 (unsigned int)quad
[0],
69 (unsigned int)quad
[1],
70 (unsigned int)quad
[2],
71 (unsigned int)quad
[3],
72 (unsigned int)ntohs(in4
->sin_port
));
76 sprintf(s
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u",
77 in6
->sin6_addr
.s6_addr16
[0],
78 in6
->sin6_addr
.s6_addr16
[1],
79 in6
->sin6_addr
.s6_addr16
[2],
80 in6
->sin6_addr
.s6_addr16
[3],
81 in6
->sin6_addr
.s6_addr16
[4],
82 in6
->sin6_addr
.s6_addr16
[5],
83 in6
->sin6_addr
.s6_addr16
[6],
84 in6
->sin6_addr
.s6_addr16
[7],
85 (unsigned int)ntohs(in6
->sin6_port
));
89 sprintf(s
, "(unknown sockaddr family %d)", (int)ss
->ss_family
);
95 static void encode_my_addr(struct ceph_messenger
*msgr
)
97 memcpy(&msgr
->my_enc_addr
, &msgr
->inst
.addr
, sizeof(msgr
->my_enc_addr
));
98 ceph_encode_addr(&msgr
->my_enc_addr
);
102 * work queue for all reading and writing to/from the socket.
104 struct workqueue_struct
*ceph_msgr_wq
;
106 int __init
ceph_msgr_init(void)
108 ceph_msgr_wq
= create_workqueue("ceph-msgr");
109 if (IS_ERR(ceph_msgr_wq
)) {
110 int ret
= PTR_ERR(ceph_msgr_wq
);
111 pr_err("msgr_init failed to create workqueue: %d\n", ret
);
118 void ceph_msgr_exit(void)
120 destroy_workqueue(ceph_msgr_wq
);
124 * socket callback functions
127 /* data available on socket, or listen socket received a connect */
128 static void ceph_data_ready(struct sock
*sk
, int count_unused
)
130 struct ceph_connection
*con
=
131 (struct ceph_connection
*)sk
->sk_user_data
;
132 if (sk
->sk_state
!= TCP_CLOSE_WAIT
) {
133 dout("ceph_data_ready on %p state = %lu, queueing work\n",
139 /* socket has buffer space for writing */
140 static void ceph_write_space(struct sock
*sk
)
142 struct ceph_connection
*con
=
143 (struct ceph_connection
*)sk
->sk_user_data
;
145 /* only queue to workqueue if there is data we want to write. */
146 if (test_bit(WRITE_PENDING
, &con
->state
)) {
147 dout("ceph_write_space %p queueing write work\n", con
);
150 dout("ceph_write_space %p nothing to write\n", con
);
153 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
154 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
157 /* socket's state has changed */
158 static void ceph_state_change(struct sock
*sk
)
160 struct ceph_connection
*con
=
161 (struct ceph_connection
*)sk
->sk_user_data
;
163 dout("ceph_state_change %p state = %lu sk_state = %u\n",
164 con
, con
->state
, sk
->sk_state
);
166 if (test_bit(CLOSED
, &con
->state
))
169 switch (sk
->sk_state
) {
171 dout("ceph_state_change TCP_CLOSE\n");
173 dout("ceph_state_change TCP_CLOSE_WAIT\n");
174 if (test_and_set_bit(SOCK_CLOSED
, &con
->state
) == 0) {
175 if (test_bit(CONNECTING
, &con
->state
))
176 con
->error_msg
= "connection failed";
178 con
->error_msg
= "socket closed";
182 case TCP_ESTABLISHED
:
183 dout("ceph_state_change TCP_ESTABLISHED\n");
190 * set up socket callbacks
192 static void set_sock_callbacks(struct socket
*sock
,
193 struct ceph_connection
*con
)
195 struct sock
*sk
= sock
->sk
;
196 sk
->sk_user_data
= (void *)con
;
197 sk
->sk_data_ready
= ceph_data_ready
;
198 sk
->sk_write_space
= ceph_write_space
;
199 sk
->sk_state_change
= ceph_state_change
;
208 * initiate connection to a remote socket.
210 static struct socket
*ceph_tcp_connect(struct ceph_connection
*con
)
212 struct sockaddr
*paddr
= (struct sockaddr
*)&con
->peer_addr
.in_addr
;
217 ret
= sock_create_kern(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
221 sock
->sk
->sk_allocation
= GFP_NOFS
;
223 #ifdef CONFIG_LOCKDEP
224 lockdep_set_class(&sock
->sk
->sk_lock
, &socket_class
);
227 set_sock_callbacks(sock
, con
);
229 dout("connect %s\n", pr_addr(&con
->peer_addr
.in_addr
));
231 ret
= sock
->ops
->connect(sock
, paddr
, sizeof(*paddr
), O_NONBLOCK
);
232 if (ret
== -EINPROGRESS
) {
233 dout("connect %s EINPROGRESS sk_state = %u\n",
234 pr_addr(&con
->peer_addr
.in_addr
),
239 pr_err("connect %s error %d\n",
240 pr_addr(&con
->peer_addr
.in_addr
), ret
);
243 con
->error_msg
= "connect error";
251 static int ceph_tcp_recvmsg(struct socket
*sock
, void *buf
, size_t len
)
253 struct kvec iov
= {buf
, len
};
254 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
256 return kernel_recvmsg(sock
, &msg
, &iov
, 1, len
, msg
.msg_flags
);
260 * write something. @more is true if caller will be sending more data
263 static int ceph_tcp_sendmsg(struct socket
*sock
, struct kvec
*iov
,
264 size_t kvlen
, size_t len
, int more
)
266 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
269 msg
.msg_flags
|= MSG_MORE
;
271 msg
.msg_flags
|= MSG_EOR
; /* superfluous, but what the hell */
273 return kernel_sendmsg(sock
, &msg
, iov
, kvlen
, len
);
278 * Shutdown/close the socket for the given connection.
280 static int con_close_socket(struct ceph_connection
*con
)
284 dout("con_close_socket on %p sock %p\n", con
, con
->sock
);
287 set_bit(SOCK_CLOSED
, &con
->state
);
288 rc
= con
->sock
->ops
->shutdown(con
->sock
, SHUT_RDWR
);
289 sock_release(con
->sock
);
291 clear_bit(SOCK_CLOSED
, &con
->state
);
296 * Reset a connection. Discard all incoming and outgoing messages
297 * and clear *_seq state.
299 static void ceph_msg_remove(struct ceph_msg
*msg
)
301 list_del_init(&msg
->list_head
);
304 static void ceph_msg_remove_list(struct list_head
*head
)
306 while (!list_empty(head
)) {
307 struct ceph_msg
*msg
= list_first_entry(head
, struct ceph_msg
,
309 ceph_msg_remove(msg
);
313 static void reset_connection(struct ceph_connection
*con
)
315 /* reset connection, out_queue, msg_ and connect_seq */
316 /* discard existing out_queue and msg_seq */
317 ceph_msg_remove_list(&con
->out_queue
);
318 ceph_msg_remove_list(&con
->out_sent
);
321 ceph_msg_put(con
->in_msg
);
325 con
->connect_seq
= 0;
328 ceph_msg_put(con
->out_msg
);
331 con
->out_keepalive_pending
= false;
333 con
->in_seq_acked
= 0;
337 * mark a peer down. drop any open connections.
339 void ceph_con_close(struct ceph_connection
*con
)
341 dout("con_close %p peer %s\n", con
, pr_addr(&con
->peer_addr
.in_addr
));
342 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
343 clear_bit(STANDBY
, &con
->state
); /* avoid connect_seq bump */
344 clear_bit(LOSSYTX
, &con
->state
); /* so we retry next connect */
345 clear_bit(KEEPALIVE_PENDING
, &con
->state
);
346 clear_bit(WRITE_PENDING
, &con
->state
);
347 mutex_lock(&con
->mutex
);
348 reset_connection(con
);
349 con
->peer_global_seq
= 0;
350 cancel_delayed_work(&con
->work
);
351 mutex_unlock(&con
->mutex
);
356 * Reopen a closed connection, with a new peer address.
358 void ceph_con_open(struct ceph_connection
*con
, struct ceph_entity_addr
*addr
)
360 dout("con_open %p %s\n", con
, pr_addr(&addr
->in_addr
));
361 set_bit(OPENING
, &con
->state
);
362 clear_bit(CLOSED
, &con
->state
);
363 memcpy(&con
->peer_addr
, addr
, sizeof(*addr
));
364 con
->delay
= 0; /* reset backoff memory */
369 * return true if this connection ever successfully opened
371 bool ceph_con_opened(struct ceph_connection
*con
)
373 return con
->connect_seq
> 0;
379 struct ceph_connection
*ceph_con_get(struct ceph_connection
*con
)
381 dout("con_get %p nref = %d -> %d\n", con
,
382 atomic_read(&con
->nref
), atomic_read(&con
->nref
) + 1);
383 if (atomic_inc_not_zero(&con
->nref
))
388 void ceph_con_put(struct ceph_connection
*con
)
390 dout("con_put %p nref = %d -> %d\n", con
,
391 atomic_read(&con
->nref
), atomic_read(&con
->nref
) - 1);
392 BUG_ON(atomic_read(&con
->nref
) == 0);
393 if (atomic_dec_and_test(&con
->nref
)) {
400 * initialize a new connection.
402 void ceph_con_init(struct ceph_messenger
*msgr
, struct ceph_connection
*con
)
404 dout("con_init %p\n", con
);
405 memset(con
, 0, sizeof(*con
));
406 atomic_set(&con
->nref
, 1);
408 mutex_init(&con
->mutex
);
409 INIT_LIST_HEAD(&con
->out_queue
);
410 INIT_LIST_HEAD(&con
->out_sent
);
411 INIT_DELAYED_WORK(&con
->work
, con_work
);
416 * We maintain a global counter to order connection attempts. Get
417 * a unique seq greater than @gt.
419 static u32
get_global_seq(struct ceph_messenger
*msgr
, u32 gt
)
423 spin_lock(&msgr
->global_seq_lock
);
424 if (msgr
->global_seq
< gt
)
425 msgr
->global_seq
= gt
;
426 ret
= ++msgr
->global_seq
;
427 spin_unlock(&msgr
->global_seq_lock
);
433 * Prepare footer for currently outgoing message, and finish things
434 * off. Assumes out_kvec* are already valid.. we just add on to the end.
436 static void prepare_write_message_footer(struct ceph_connection
*con
, int v
)
438 struct ceph_msg
*m
= con
->out_msg
;
440 dout("prepare_write_message_footer %p\n", con
);
441 con
->out_kvec_is_msg
= true;
442 con
->out_kvec
[v
].iov_base
= &m
->footer
;
443 con
->out_kvec
[v
].iov_len
= sizeof(m
->footer
);
444 con
->out_kvec_bytes
+= sizeof(m
->footer
);
445 con
->out_kvec_left
++;
446 con
->out_more
= m
->more_to_follow
;
447 con
->out_msg_done
= true;
451 * Prepare headers for the next outgoing message.
453 static void prepare_write_message(struct ceph_connection
*con
)
458 con
->out_kvec_bytes
= 0;
459 con
->out_kvec_is_msg
= true;
460 con
->out_msg_done
= false;
462 /* Sneak an ack in there first? If we can get it into the same
463 * TCP packet that's a good thing. */
464 if (con
->in_seq
> con
->in_seq_acked
) {
465 con
->in_seq_acked
= con
->in_seq
;
466 con
->out_kvec
[v
].iov_base
= &tag_ack
;
467 con
->out_kvec
[v
++].iov_len
= 1;
468 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
469 con
->out_kvec
[v
].iov_base
= &con
->out_temp_ack
;
470 con
->out_kvec
[v
++].iov_len
= sizeof(con
->out_temp_ack
);
471 con
->out_kvec_bytes
= 1 + sizeof(con
->out_temp_ack
);
474 m
= list_first_entry(&con
->out_queue
,
475 struct ceph_msg
, list_head
);
477 if (test_bit(LOSSYTX
, &con
->state
)) {
478 list_del_init(&m
->list_head
);
480 /* put message on sent list */
482 list_move_tail(&m
->list_head
, &con
->out_sent
);
486 * only assign outgoing seq # if we haven't sent this message
487 * yet. if it is requeued, resend with it's original seq.
489 if (m
->needs_out_seq
) {
490 m
->hdr
.seq
= cpu_to_le64(++con
->out_seq
);
491 m
->needs_out_seq
= false;
494 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
495 m
, con
->out_seq
, le16_to_cpu(m
->hdr
.type
),
496 le32_to_cpu(m
->hdr
.front_len
), le32_to_cpu(m
->hdr
.middle_len
),
497 le32_to_cpu(m
->hdr
.data_len
),
499 BUG_ON(le32_to_cpu(m
->hdr
.front_len
) != m
->front
.iov_len
);
501 /* tag + hdr + front + middle */
502 con
->out_kvec
[v
].iov_base
= &tag_msg
;
503 con
->out_kvec
[v
++].iov_len
= 1;
504 con
->out_kvec
[v
].iov_base
= &m
->hdr
;
505 con
->out_kvec
[v
++].iov_len
= sizeof(m
->hdr
);
506 con
->out_kvec
[v
++] = m
->front
;
508 con
->out_kvec
[v
++] = m
->middle
->vec
;
509 con
->out_kvec_left
= v
;
510 con
->out_kvec_bytes
+= 1 + sizeof(m
->hdr
) + m
->front
.iov_len
+
511 (m
->middle
? m
->middle
->vec
.iov_len
: 0);
512 con
->out_kvec_cur
= con
->out_kvec
;
514 /* fill in crc (except data pages), footer */
515 con
->out_msg
->hdr
.crc
=
516 cpu_to_le32(crc32c(0, (void *)&m
->hdr
,
517 sizeof(m
->hdr
) - sizeof(m
->hdr
.crc
)));
518 con
->out_msg
->footer
.flags
= CEPH_MSG_FOOTER_COMPLETE
;
519 con
->out_msg
->footer
.front_crc
=
520 cpu_to_le32(crc32c(0, m
->front
.iov_base
, m
->front
.iov_len
));
522 con
->out_msg
->footer
.middle_crc
=
523 cpu_to_le32(crc32c(0, m
->middle
->vec
.iov_base
,
524 m
->middle
->vec
.iov_len
));
526 con
->out_msg
->footer
.middle_crc
= 0;
527 con
->out_msg
->footer
.data_crc
= 0;
528 dout("prepare_write_message front_crc %u data_crc %u\n",
529 le32_to_cpu(con
->out_msg
->footer
.front_crc
),
530 le32_to_cpu(con
->out_msg
->footer
.middle_crc
));
532 /* is there a data payload? */
533 if (le32_to_cpu(m
->hdr
.data_len
) > 0) {
534 /* initialize page iterator */
535 con
->out_msg_pos
.page
= 0;
536 con
->out_msg_pos
.page_pos
=
537 le16_to_cpu(m
->hdr
.data_off
) & ~PAGE_MASK
;
538 con
->out_msg_pos
.data_pos
= 0;
539 con
->out_msg_pos
.did_page_crc
= 0;
540 con
->out_more
= 1; /* data + footer will follow */
542 /* no, queue up footer too and be done */
543 prepare_write_message_footer(con
, v
);
546 set_bit(WRITE_PENDING
, &con
->state
);
552 static void prepare_write_ack(struct ceph_connection
*con
)
554 dout("prepare_write_ack %p %llu -> %llu\n", con
,
555 con
->in_seq_acked
, con
->in_seq
);
556 con
->in_seq_acked
= con
->in_seq
;
558 con
->out_kvec
[0].iov_base
= &tag_ack
;
559 con
->out_kvec
[0].iov_len
= 1;
560 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
561 con
->out_kvec
[1].iov_base
= &con
->out_temp_ack
;
562 con
->out_kvec
[1].iov_len
= sizeof(con
->out_temp_ack
);
563 con
->out_kvec_left
= 2;
564 con
->out_kvec_bytes
= 1 + sizeof(con
->out_temp_ack
);
565 con
->out_kvec_cur
= con
->out_kvec
;
566 con
->out_more
= 1; /* more will follow.. eventually.. */
567 set_bit(WRITE_PENDING
, &con
->state
);
571 * Prepare to write keepalive byte.
573 static void prepare_write_keepalive(struct ceph_connection
*con
)
575 dout("prepare_write_keepalive %p\n", con
);
576 con
->out_kvec
[0].iov_base
= &tag_keepalive
;
577 con
->out_kvec
[0].iov_len
= 1;
578 con
->out_kvec_left
= 1;
579 con
->out_kvec_bytes
= 1;
580 con
->out_kvec_cur
= con
->out_kvec
;
581 set_bit(WRITE_PENDING
, &con
->state
);
585 * Connection negotiation.
588 static void prepare_connect_authorizer(struct ceph_connection
*con
)
592 int auth_protocol
= 0;
594 mutex_unlock(&con
->mutex
);
595 if (con
->ops
->get_authorizer
)
596 con
->ops
->get_authorizer(con
, &auth_buf
, &auth_len
,
597 &auth_protocol
, &con
->auth_reply_buf
,
598 &con
->auth_reply_buf_len
,
600 mutex_lock(&con
->mutex
);
602 con
->out_connect
.authorizer_protocol
= cpu_to_le32(auth_protocol
);
603 con
->out_connect
.authorizer_len
= cpu_to_le32(auth_len
);
605 con
->out_kvec
[con
->out_kvec_left
].iov_base
= auth_buf
;
606 con
->out_kvec
[con
->out_kvec_left
].iov_len
= auth_len
;
607 con
->out_kvec_left
++;
608 con
->out_kvec_bytes
+= auth_len
;
612 * We connected to a peer and are saying hello.
614 static void prepare_write_banner(struct ceph_messenger
*msgr
,
615 struct ceph_connection
*con
)
617 int len
= strlen(CEPH_BANNER
);
619 con
->out_kvec
[0].iov_base
= CEPH_BANNER
;
620 con
->out_kvec
[0].iov_len
= len
;
621 con
->out_kvec
[1].iov_base
= &msgr
->my_enc_addr
;
622 con
->out_kvec
[1].iov_len
= sizeof(msgr
->my_enc_addr
);
623 con
->out_kvec_left
= 2;
624 con
->out_kvec_bytes
= len
+ sizeof(msgr
->my_enc_addr
);
625 con
->out_kvec_cur
= con
->out_kvec
;
627 set_bit(WRITE_PENDING
, &con
->state
);
630 static void prepare_write_connect(struct ceph_messenger
*msgr
,
631 struct ceph_connection
*con
,
634 unsigned global_seq
= get_global_seq(con
->msgr
, 0);
637 switch (con
->peer_name
.type
) {
638 case CEPH_ENTITY_TYPE_MON
:
639 proto
= CEPH_MONC_PROTOCOL
;
641 case CEPH_ENTITY_TYPE_OSD
:
642 proto
= CEPH_OSDC_PROTOCOL
;
644 case CEPH_ENTITY_TYPE_MDS
:
645 proto
= CEPH_MDSC_PROTOCOL
;
651 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con
,
652 con
->connect_seq
, global_seq
, proto
);
654 con
->out_connect
.features
= CEPH_FEATURE_SUPPORTED_CLIENT
;
655 con
->out_connect
.host_type
= cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT
);
656 con
->out_connect
.connect_seq
= cpu_to_le32(con
->connect_seq
);
657 con
->out_connect
.global_seq
= cpu_to_le32(global_seq
);
658 con
->out_connect
.protocol_version
= cpu_to_le32(proto
);
659 con
->out_connect
.flags
= 0;
662 con
->out_kvec_left
= 0;
663 con
->out_kvec_bytes
= 0;
665 con
->out_kvec
[con
->out_kvec_left
].iov_base
= &con
->out_connect
;
666 con
->out_kvec
[con
->out_kvec_left
].iov_len
= sizeof(con
->out_connect
);
667 con
->out_kvec_left
++;
668 con
->out_kvec_bytes
+= sizeof(con
->out_connect
);
669 con
->out_kvec_cur
= con
->out_kvec
;
671 set_bit(WRITE_PENDING
, &con
->state
);
673 prepare_connect_authorizer(con
);
678 * write as much of pending kvecs to the socket as we can.
680 * 0 -> socket full, but more to do
683 static int write_partial_kvec(struct ceph_connection
*con
)
687 dout("write_partial_kvec %p %d left\n", con
, con
->out_kvec_bytes
);
688 while (con
->out_kvec_bytes
> 0) {
689 ret
= ceph_tcp_sendmsg(con
->sock
, con
->out_kvec_cur
,
690 con
->out_kvec_left
, con
->out_kvec_bytes
,
694 con
->out_kvec_bytes
-= ret
;
695 if (con
->out_kvec_bytes
== 0)
698 if (ret
>= con
->out_kvec_cur
->iov_len
) {
699 ret
-= con
->out_kvec_cur
->iov_len
;
701 con
->out_kvec_left
--;
703 con
->out_kvec_cur
->iov_len
-= ret
;
704 con
->out_kvec_cur
->iov_base
+= ret
;
710 con
->out_kvec_left
= 0;
711 con
->out_kvec_is_msg
= false;
714 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con
,
715 con
->out_kvec_bytes
, con
->out_kvec_left
, ret
);
716 return ret
; /* done! */
720 * Write as much message data payload as we can. If we finish, queue
722 * 1 -> done, footer is now queued in out_kvec[].
723 * 0 -> socket full, but more to do
726 static int write_partial_msg_pages(struct ceph_connection
*con
)
728 struct ceph_msg
*msg
= con
->out_msg
;
729 unsigned data_len
= le32_to_cpu(msg
->hdr
.data_len
);
731 int crc
= con
->msgr
->nocrc
;
734 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
735 con
, con
->out_msg
, con
->out_msg_pos
.page
, con
->out_msg
->nr_pages
,
736 con
->out_msg_pos
.page_pos
);
738 while (con
->out_msg_pos
.page
< con
->out_msg
->nr_pages
) {
739 struct page
*page
= NULL
;
743 * if we are calculating the data crc (the default), we need
744 * to map the page. if our pages[] has been revoked, use the
748 page
= msg
->pages
[con
->out_msg_pos
.page
];
751 } else if (msg
->pagelist
) {
752 page
= list_first_entry(&msg
->pagelist
->head
,
757 page
= con
->msgr
->zero_page
;
759 kaddr
= page_address(con
->msgr
->zero_page
);
761 len
= min((int)(PAGE_SIZE
- con
->out_msg_pos
.page_pos
),
762 (int)(data_len
- con
->out_msg_pos
.data_pos
));
763 if (crc
&& !con
->out_msg_pos
.did_page_crc
) {
764 void *base
= kaddr
+ con
->out_msg_pos
.page_pos
;
765 u32 tmpcrc
= le32_to_cpu(con
->out_msg
->footer
.data_crc
);
767 BUG_ON(kaddr
== NULL
);
768 con
->out_msg
->footer
.data_crc
=
769 cpu_to_le32(crc32c(tmpcrc
, base
, len
));
770 con
->out_msg_pos
.did_page_crc
= 1;
773 ret
= kernel_sendpage(con
->sock
, page
,
774 con
->out_msg_pos
.page_pos
, len
,
775 MSG_DONTWAIT
| MSG_NOSIGNAL
|
778 if (crc
&& (msg
->pages
|| msg
->pagelist
))
784 con
->out_msg_pos
.data_pos
+= ret
;
785 con
->out_msg_pos
.page_pos
+= ret
;
787 con
->out_msg_pos
.page_pos
= 0;
788 con
->out_msg_pos
.page
++;
789 con
->out_msg_pos
.did_page_crc
= 0;
791 list_move_tail(&page
->lru
,
792 &msg
->pagelist
->head
);
796 dout("write_partial_msg_pages %p msg %p done\n", con
, msg
);
798 /* prepare and queue up footer, too */
800 con
->out_msg
->footer
.flags
|= CEPH_MSG_FOOTER_NOCRC
;
801 con
->out_kvec_bytes
= 0;
802 con
->out_kvec_left
= 0;
803 con
->out_kvec_cur
= con
->out_kvec
;
804 prepare_write_message_footer(con
, 0);
813 static int write_partial_skip(struct ceph_connection
*con
)
817 while (con
->out_skip
> 0) {
819 .iov_base
= page_address(con
->msgr
->zero_page
),
820 .iov_len
= min(con
->out_skip
, (int)PAGE_CACHE_SIZE
)
823 ret
= ceph_tcp_sendmsg(con
->sock
, &iov
, 1, iov
.iov_len
, 1);
826 con
->out_skip
-= ret
;
834 * Prepare to read connection handshake, or an ack.
836 static void prepare_read_banner(struct ceph_connection
*con
)
838 dout("prepare_read_banner %p\n", con
);
839 con
->in_base_pos
= 0;
842 static void prepare_read_connect(struct ceph_connection
*con
)
844 dout("prepare_read_connect %p\n", con
);
845 con
->in_base_pos
= 0;
848 static void prepare_read_ack(struct ceph_connection
*con
)
850 dout("prepare_read_ack %p\n", con
);
851 con
->in_base_pos
= 0;
854 static void prepare_read_tag(struct ceph_connection
*con
)
856 dout("prepare_read_tag %p\n", con
);
857 con
->in_base_pos
= 0;
858 con
->in_tag
= CEPH_MSGR_TAG_READY
;
862 * Prepare to read a message.
864 static int prepare_read_message(struct ceph_connection
*con
)
866 dout("prepare_read_message %p\n", con
);
867 BUG_ON(con
->in_msg
!= NULL
);
868 con
->in_base_pos
= 0;
869 con
->in_front_crc
= con
->in_middle_crc
= con
->in_data_crc
= 0;
874 static int read_partial(struct ceph_connection
*con
,
875 int *to
, int size
, void *object
)
878 while (con
->in_base_pos
< *to
) {
879 int left
= *to
- con
->in_base_pos
;
880 int have
= size
- left
;
881 int ret
= ceph_tcp_recvmsg(con
->sock
, object
+ have
, left
);
884 con
->in_base_pos
+= ret
;
891 * Read all or part of the connect-side handshake on a new connection
893 static int read_partial_banner(struct ceph_connection
*con
)
897 dout("read_partial_banner %p at %d\n", con
, con
->in_base_pos
);
900 ret
= read_partial(con
, &to
, strlen(CEPH_BANNER
), con
->in_banner
);
903 ret
= read_partial(con
, &to
, sizeof(con
->actual_peer_addr
),
904 &con
->actual_peer_addr
);
907 ret
= read_partial(con
, &to
, sizeof(con
->peer_addr_for_me
),
908 &con
->peer_addr_for_me
);
915 static int read_partial_connect(struct ceph_connection
*con
)
919 dout("read_partial_connect %p at %d\n", con
, con
->in_base_pos
);
921 ret
= read_partial(con
, &to
, sizeof(con
->in_reply
), &con
->in_reply
);
924 ret
= read_partial(con
, &to
, le32_to_cpu(con
->in_reply
.authorizer_len
),
925 con
->auth_reply_buf
);
929 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
930 con
, (int)con
->in_reply
.tag
,
931 le32_to_cpu(con
->in_reply
.connect_seq
),
932 le32_to_cpu(con
->in_reply
.global_seq
));
939 * Verify the hello banner looks okay.
941 static int verify_hello(struct ceph_connection
*con
)
943 if (memcmp(con
->in_banner
, CEPH_BANNER
, strlen(CEPH_BANNER
))) {
944 pr_err("connect to %s got bad banner\n",
945 pr_addr(&con
->peer_addr
.in_addr
));
946 con
->error_msg
= "protocol error, bad banner";
952 static bool addr_is_blank(struct sockaddr_storage
*ss
)
954 switch (ss
->ss_family
) {
956 return ((struct sockaddr_in
*)ss
)->sin_addr
.s_addr
== 0;
959 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[0] == 0 &&
960 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[1] == 0 &&
961 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[2] == 0 &&
962 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[3] == 0;
967 static int addr_port(struct sockaddr_storage
*ss
)
969 switch (ss
->ss_family
) {
971 return ntohs(((struct sockaddr_in
*)ss
)->sin_port
);
973 return ntohs(((struct sockaddr_in6
*)ss
)->sin6_port
);
978 static void addr_set_port(struct sockaddr_storage
*ss
, int p
)
980 switch (ss
->ss_family
) {
982 ((struct sockaddr_in
*)ss
)->sin_port
= htons(p
);
984 ((struct sockaddr_in6
*)ss
)->sin6_port
= htons(p
);
989 * Parse an ip[:port] list into an addr array. Use the default
990 * monitor port if a port isn't specified.
992 int ceph_parse_ips(const char *c
, const char *end
,
993 struct ceph_entity_addr
*addr
,
994 int max_count
, int *count
)
999 dout("parse_ips on '%.*s'\n", (int)(end
-c
), c
);
1000 for (i
= 0; i
< max_count
; i
++) {
1002 struct sockaddr_storage
*ss
= &addr
[i
].in_addr
;
1003 struct sockaddr_in
*in4
= (void *)ss
;
1004 struct sockaddr_in6
*in6
= (void *)ss
;
1007 memset(ss
, 0, sizeof(*ss
));
1008 if (in4_pton(p
, end
- p
, (u8
*)&in4
->sin_addr
.s_addr
,
1010 ss
->ss_family
= AF_INET
;
1011 } else if (in6_pton(p
, end
- p
, (u8
*)&in6
->sin6_addr
.s6_addr
,
1013 ss
->ss_family
= AF_INET6
;
1020 if (p
< end
&& *p
== ':') {
1023 while (p
< end
&& *p
>= '0' && *p
<= '9') {
1024 port
= (port
* 10) + (*p
- '0');
1027 if (port
> 65535 || port
== 0)
1030 port
= CEPH_MON_PORT
;
1033 addr_set_port(ss
, port
);
1035 dout("parse_ips got %s\n", pr_addr(ss
));
1052 pr_err("parse_ips bad ip '%s'\n", c
);
1056 static int process_banner(struct ceph_connection
*con
)
1058 dout("process_banner on %p\n", con
);
1060 if (verify_hello(con
) < 0)
1063 ceph_decode_addr(&con
->actual_peer_addr
);
1064 ceph_decode_addr(&con
->peer_addr_for_me
);
1067 * Make sure the other end is who we wanted. note that the other
1068 * end may not yet know their ip address, so if it's 0.0.0.0, give
1069 * them the benefit of the doubt.
1071 if (memcmp(&con
->peer_addr
, &con
->actual_peer_addr
,
1072 sizeof(con
->peer_addr
)) != 0 &&
1073 !(addr_is_blank(&con
->actual_peer_addr
.in_addr
) &&
1074 con
->actual_peer_addr
.nonce
== con
->peer_addr
.nonce
)) {
1075 pr_warning("wrong peer, want %s/%lld, got %s/%lld\n",
1076 pr_addr(&con
->peer_addr
.in_addr
),
1077 le64_to_cpu(con
->peer_addr
.nonce
),
1078 pr_addr(&con
->actual_peer_addr
.in_addr
),
1079 le64_to_cpu(con
->actual_peer_addr
.nonce
));
1080 con
->error_msg
= "wrong peer at address";
1085 * did we learn our address?
1087 if (addr_is_blank(&con
->msgr
->inst
.addr
.in_addr
)) {
1088 int port
= addr_port(&con
->msgr
->inst
.addr
.in_addr
);
1090 memcpy(&con
->msgr
->inst
.addr
.in_addr
,
1091 &con
->peer_addr_for_me
.in_addr
,
1092 sizeof(con
->peer_addr_for_me
.in_addr
));
1093 addr_set_port(&con
->msgr
->inst
.addr
.in_addr
, port
);
1094 encode_my_addr(con
->msgr
);
1095 dout("process_banner learned my addr is %s\n",
1096 pr_addr(&con
->msgr
->inst
.addr
.in_addr
));
1099 set_bit(NEGOTIATING
, &con
->state
);
1100 prepare_read_connect(con
);
1104 static void fail_protocol(struct ceph_connection
*con
)
1106 reset_connection(con
);
1107 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
1109 mutex_unlock(&con
->mutex
);
1110 if (con
->ops
->bad_proto
)
1111 con
->ops
->bad_proto(con
);
1112 mutex_lock(&con
->mutex
);
1115 static int process_connect(struct ceph_connection
*con
)
1117 u64 sup_feat
= CEPH_FEATURE_SUPPORTED_CLIENT
;
1118 u64 req_feat
= CEPH_FEATURE_REQUIRED_CLIENT
;
1119 u64 server_feat
= le64_to_cpu(con
->in_reply
.features
);
1121 dout("process_connect on %p tag %d\n", con
, (int)con
->in_tag
);
1123 switch (con
->in_reply
.tag
) {
1124 case CEPH_MSGR_TAG_FEATURES
:
1125 pr_err("%s%lld %s feature set mismatch,"
1126 " my %llx < server's %llx, missing %llx\n",
1127 ENTITY_NAME(con
->peer_name
),
1128 pr_addr(&con
->peer_addr
.in_addr
),
1129 sup_feat
, server_feat
, server_feat
& ~sup_feat
);
1130 con
->error_msg
= "missing required protocol features";
1134 case CEPH_MSGR_TAG_BADPROTOVER
:
1135 pr_err("%s%lld %s protocol version mismatch,"
1136 " my %d != server's %d\n",
1137 ENTITY_NAME(con
->peer_name
),
1138 pr_addr(&con
->peer_addr
.in_addr
),
1139 le32_to_cpu(con
->out_connect
.protocol_version
),
1140 le32_to_cpu(con
->in_reply
.protocol_version
));
1141 con
->error_msg
= "protocol version mismatch";
1145 case CEPH_MSGR_TAG_BADAUTHORIZER
:
1147 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con
,
1149 if (con
->auth_retry
== 2) {
1150 con
->error_msg
= "connect authorization failure";
1151 reset_connection(con
);
1152 set_bit(CLOSED
, &con
->state
);
1155 con
->auth_retry
= 1;
1156 prepare_write_connect(con
->msgr
, con
, 0);
1157 prepare_read_connect(con
);
1160 case CEPH_MSGR_TAG_RESETSESSION
:
1162 * If we connected with a large connect_seq but the peer
1163 * has no record of a session with us (no connection, or
1164 * connect_seq == 0), they will send RESETSESION to indicate
1165 * that they must have reset their session, and may have
1168 dout("process_connect got RESET peer seq %u\n",
1169 le32_to_cpu(con
->in_connect
.connect_seq
));
1170 pr_err("%s%lld %s connection reset\n",
1171 ENTITY_NAME(con
->peer_name
),
1172 pr_addr(&con
->peer_addr
.in_addr
));
1173 reset_connection(con
);
1174 prepare_write_connect(con
->msgr
, con
, 0);
1175 prepare_read_connect(con
);
1177 /* Tell ceph about it. */
1178 mutex_unlock(&con
->mutex
);
1179 pr_info("reset on %s%lld\n", ENTITY_NAME(con
->peer_name
));
1180 if (con
->ops
->peer_reset
)
1181 con
->ops
->peer_reset(con
);
1182 mutex_lock(&con
->mutex
);
1185 case CEPH_MSGR_TAG_RETRY_SESSION
:
1187 * If we sent a smaller connect_seq than the peer has, try
1188 * again with a larger value.
1190 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1191 le32_to_cpu(con
->out_connect
.connect_seq
),
1192 le32_to_cpu(con
->in_connect
.connect_seq
));
1193 con
->connect_seq
= le32_to_cpu(con
->in_connect
.connect_seq
);
1194 prepare_write_connect(con
->msgr
, con
, 0);
1195 prepare_read_connect(con
);
1198 case CEPH_MSGR_TAG_RETRY_GLOBAL
:
1200 * If we sent a smaller global_seq than the peer has, try
1201 * again with a larger value.
1203 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
1204 con
->peer_global_seq
,
1205 le32_to_cpu(con
->in_connect
.global_seq
));
1206 get_global_seq(con
->msgr
,
1207 le32_to_cpu(con
->in_connect
.global_seq
));
1208 prepare_write_connect(con
->msgr
, con
, 0);
1209 prepare_read_connect(con
);
1212 case CEPH_MSGR_TAG_READY
:
1213 if (req_feat
& ~server_feat
) {
1214 pr_err("%s%lld %s protocol feature mismatch,"
1215 " my required %llx > server's %llx, need %llx\n",
1216 ENTITY_NAME(con
->peer_name
),
1217 pr_addr(&con
->peer_addr
.in_addr
),
1218 req_feat
, server_feat
, req_feat
& ~server_feat
);
1219 con
->error_msg
= "missing required protocol features";
1223 clear_bit(CONNECTING
, &con
->state
);
1224 con
->peer_global_seq
= le32_to_cpu(con
->in_reply
.global_seq
);
1226 con
->peer_features
= server_feat
;
1227 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1228 con
->peer_global_seq
,
1229 le32_to_cpu(con
->in_reply
.connect_seq
),
1231 WARN_ON(con
->connect_seq
!=
1232 le32_to_cpu(con
->in_reply
.connect_seq
));
1234 if (con
->in_reply
.flags
& CEPH_MSG_CONNECT_LOSSY
)
1235 set_bit(LOSSYTX
, &con
->state
);
1237 prepare_read_tag(con
);
1240 case CEPH_MSGR_TAG_WAIT
:
1242 * If there is a connection race (we are opening
1243 * connections to each other), one of us may just have
1244 * to WAIT. This shouldn't happen if we are the
1247 pr_err("process_connect peer connecting WAIT\n");
1250 pr_err("connect protocol error, will retry\n");
1251 con
->error_msg
= "protocol error, garbage tag during connect";
1259 * read (part of) an ack
1261 static int read_partial_ack(struct ceph_connection
*con
)
1265 return read_partial(con
, &to
, sizeof(con
->in_temp_ack
),
1271 * We can finally discard anything that's been acked.
1273 static void process_ack(struct ceph_connection
*con
)
1276 u64 ack
= le64_to_cpu(con
->in_temp_ack
);
1279 while (!list_empty(&con
->out_sent
)) {
1280 m
= list_first_entry(&con
->out_sent
, struct ceph_msg
,
1282 seq
= le64_to_cpu(m
->hdr
.seq
);
1285 dout("got ack for seq %llu type %d at %p\n", seq
,
1286 le16_to_cpu(m
->hdr
.type
), m
);
1289 prepare_read_tag(con
);
1295 static int read_partial_message_section(struct ceph_connection
*con
,
1296 struct kvec
*section
, unsigned int sec_len
,
1304 while (section
->iov_len
< sec_len
) {
1305 BUG_ON(section
->iov_base
== NULL
);
1306 left
= sec_len
- section
->iov_len
;
1307 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)section
->iov_base
+
1308 section
->iov_len
, left
);
1311 section
->iov_len
+= ret
;
1312 if (section
->iov_len
== sec_len
)
1313 *crc
= crc32c(0, section
->iov_base
,
1320 static struct ceph_msg
*ceph_alloc_msg(struct ceph_connection
*con
,
1321 struct ceph_msg_header
*hdr
,
1324 * read (part of) a message.
1326 static int read_partial_message(struct ceph_connection
*con
)
1328 struct ceph_msg
*m
= con
->in_msg
;
1332 unsigned front_len
, middle_len
, data_len
, data_off
;
1333 int datacrc
= con
->msgr
->nocrc
;
1337 dout("read_partial_message con %p msg %p\n", con
, m
);
1340 while (con
->in_base_pos
< sizeof(con
->in_hdr
)) {
1341 left
= sizeof(con
->in_hdr
) - con
->in_base_pos
;
1342 ret
= ceph_tcp_recvmsg(con
->sock
,
1343 (char *)&con
->in_hdr
+ con
->in_base_pos
,
1347 con
->in_base_pos
+= ret
;
1348 if (con
->in_base_pos
== sizeof(con
->in_hdr
)) {
1349 u32 crc
= crc32c(0, (void *)&con
->in_hdr
,
1350 sizeof(con
->in_hdr
) - sizeof(con
->in_hdr
.crc
));
1351 if (crc
!= le32_to_cpu(con
->in_hdr
.crc
)) {
1352 pr_err("read_partial_message bad hdr "
1353 " crc %u != expected %u\n",
1354 crc
, con
->in_hdr
.crc
);
1359 front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
1360 if (front_len
> CEPH_MSG_MAX_FRONT_LEN
)
1362 middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
1363 if (middle_len
> CEPH_MSG_MAX_DATA_LEN
)
1365 data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
1366 if (data_len
> CEPH_MSG_MAX_DATA_LEN
)
1368 data_off
= le16_to_cpu(con
->in_hdr
.data_off
);
1371 seq
= le64_to_cpu(con
->in_hdr
.seq
);
1372 if ((s64
)seq
- (s64
)con
->in_seq
< 1) {
1373 pr_info("skipping %s%lld %s seq %lld, expected %lld\n",
1374 ENTITY_NAME(con
->peer_name
),
1375 pr_addr(&con
->peer_addr
.in_addr
),
1376 seq
, con
->in_seq
+ 1);
1377 con
->in_base_pos
= -front_len
- middle_len
- data_len
-
1379 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1382 } else if ((s64
)seq
- (s64
)con
->in_seq
> 1) {
1383 pr_err("read_partial_message bad seq %lld expected %lld\n",
1384 seq
, con
->in_seq
+ 1);
1385 con
->error_msg
= "bad message sequence # for incoming message";
1389 /* allocate message? */
1391 dout("got hdr type %d front %d data %d\n", con
->in_hdr
.type
,
1392 con
->in_hdr
.front_len
, con
->in_hdr
.data_len
);
1393 con
->in_msg
= ceph_alloc_msg(con
, &con
->in_hdr
, &skip
);
1395 /* skip this message */
1396 dout("alloc_msg said skip message\n");
1397 con
->in_base_pos
= -front_len
- middle_len
- data_len
-
1399 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1405 "error allocating memory for incoming message";
1409 m
->front
.iov_len
= 0; /* haven't read it yet */
1411 m
->middle
->vec
.iov_len
= 0;
1413 con
->in_msg_pos
.page
= 0;
1414 con
->in_msg_pos
.page_pos
= data_off
& ~PAGE_MASK
;
1415 con
->in_msg_pos
.data_pos
= 0;
1419 ret
= read_partial_message_section(con
, &m
->front
, front_len
,
1420 &con
->in_front_crc
);
1426 ret
= read_partial_message_section(con
, &m
->middle
->vec
, middle_len
,
1427 &con
->in_middle_crc
);
1433 while (con
->in_msg_pos
.data_pos
< data_len
) {
1434 left
= min((int)(data_len
- con
->in_msg_pos
.data_pos
),
1435 (int)(PAGE_SIZE
- con
->in_msg_pos
.page_pos
));
1436 BUG_ON(m
->pages
== NULL
);
1437 p
= kmap(m
->pages
[con
->in_msg_pos
.page
]);
1438 ret
= ceph_tcp_recvmsg(con
->sock
, p
+ con
->in_msg_pos
.page_pos
,
1440 if (ret
> 0 && datacrc
)
1442 crc32c(con
->in_data_crc
,
1443 p
+ con
->in_msg_pos
.page_pos
, ret
);
1444 kunmap(m
->pages
[con
->in_msg_pos
.page
]);
1447 con
->in_msg_pos
.data_pos
+= ret
;
1448 con
->in_msg_pos
.page_pos
+= ret
;
1449 if (con
->in_msg_pos
.page_pos
== PAGE_SIZE
) {
1450 con
->in_msg_pos
.page_pos
= 0;
1451 con
->in_msg_pos
.page
++;
1456 to
= sizeof(m
->hdr
) + sizeof(m
->footer
);
1457 while (con
->in_base_pos
< to
) {
1458 left
= to
- con
->in_base_pos
;
1459 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)&m
->footer
+
1460 (con
->in_base_pos
- sizeof(m
->hdr
)),
1464 con
->in_base_pos
+= ret
;
1466 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1467 m
, front_len
, m
->footer
.front_crc
, middle_len
,
1468 m
->footer
.middle_crc
, data_len
, m
->footer
.data_crc
);
1471 if (con
->in_front_crc
!= le32_to_cpu(m
->footer
.front_crc
)) {
1472 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1473 m
, con
->in_front_crc
, m
->footer
.front_crc
);
1476 if (con
->in_middle_crc
!= le32_to_cpu(m
->footer
.middle_crc
)) {
1477 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1478 m
, con
->in_middle_crc
, m
->footer
.middle_crc
);
1482 (m
->footer
.flags
& CEPH_MSG_FOOTER_NOCRC
) == 0 &&
1483 con
->in_data_crc
!= le32_to_cpu(m
->footer
.data_crc
)) {
1484 pr_err("read_partial_message %p data crc %u != exp. %u\n", m
,
1485 con
->in_data_crc
, le32_to_cpu(m
->footer
.data_crc
));
1489 return 1; /* done! */
1493 * Process message. This happens in the worker thread. The callback should
1494 * be careful not to do anything that waits on other incoming messages or it
1497 static void process_message(struct ceph_connection
*con
)
1499 struct ceph_msg
*msg
;
1504 /* if first message, set peer_name */
1505 if (con
->peer_name
.type
== 0)
1506 con
->peer_name
= msg
->hdr
.src
;
1509 mutex_unlock(&con
->mutex
);
1511 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1512 msg
, le64_to_cpu(msg
->hdr
.seq
),
1513 ENTITY_NAME(msg
->hdr
.src
),
1514 le16_to_cpu(msg
->hdr
.type
),
1515 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
1516 le32_to_cpu(msg
->hdr
.front_len
),
1517 le32_to_cpu(msg
->hdr
.data_len
),
1518 con
->in_front_crc
, con
->in_middle_crc
, con
->in_data_crc
);
1519 con
->ops
->dispatch(con
, msg
);
1521 mutex_lock(&con
->mutex
);
1522 prepare_read_tag(con
);
1527 * Write something to the socket. Called in a worker thread when the
1528 * socket appears to be writeable and we have something ready to send.
1530 static int try_write(struct ceph_connection
*con
)
1532 struct ceph_messenger
*msgr
= con
->msgr
;
1535 dout("try_write start %p state %lu nref %d\n", con
, con
->state
,
1536 atomic_read(&con
->nref
));
1539 dout("try_write out_kvec_bytes %d\n", con
->out_kvec_bytes
);
1541 /* open the socket first? */
1542 if (con
->sock
== NULL
) {
1544 * if we were STANDBY and are reconnecting _this_
1545 * connection, bump connect_seq now. Always bump
1548 if (test_and_clear_bit(STANDBY
, &con
->state
))
1551 prepare_write_banner(msgr
, con
);
1552 prepare_write_connect(msgr
, con
, 1);
1553 prepare_read_banner(con
);
1554 set_bit(CONNECTING
, &con
->state
);
1555 clear_bit(NEGOTIATING
, &con
->state
);
1557 BUG_ON(con
->in_msg
);
1558 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1559 dout("try_write initiating connect on %p new state %lu\n",
1561 con
->sock
= ceph_tcp_connect(con
);
1562 if (IS_ERR(con
->sock
)) {
1564 con
->error_msg
= "connect error";
1571 /* kvec data queued? */
1572 if (con
->out_skip
) {
1573 ret
= write_partial_skip(con
);
1577 dout("try_write write_partial_skip err %d\n", ret
);
1581 if (con
->out_kvec_left
) {
1582 ret
= write_partial_kvec(con
);
1589 if (con
->out_msg_done
) {
1590 ceph_msg_put(con
->out_msg
);
1591 con
->out_msg
= NULL
; /* we're done with this one */
1595 ret
= write_partial_msg_pages(con
);
1597 goto more_kvec
; /* we need to send the footer, too! */
1601 dout("try_write write_partial_msg_pages err %d\n",
1608 if (!test_bit(CONNECTING
, &con
->state
)) {
1609 /* is anything else pending? */
1610 if (!list_empty(&con
->out_queue
)) {
1611 prepare_write_message(con
);
1614 if (con
->in_seq
> con
->in_seq_acked
) {
1615 prepare_write_ack(con
);
1618 if (test_and_clear_bit(KEEPALIVE_PENDING
, &con
->state
)) {
1619 prepare_write_keepalive(con
);
1624 /* Nothing to do! */
1625 clear_bit(WRITE_PENDING
, &con
->state
);
1626 dout("try_write nothing else to write.\n");
1630 dout("try_write done on %p\n", con
);
1637 * Read what we can from the socket.
1639 static int try_read(struct ceph_connection
*con
)
1646 if (test_bit(STANDBY
, &con
->state
))
1649 dout("try_read start on %p\n", con
);
1652 dout("try_read tag %d in_base_pos %d\n", (int)con
->in_tag
,
1654 if (test_bit(CONNECTING
, &con
->state
)) {
1655 if (!test_bit(NEGOTIATING
, &con
->state
)) {
1656 dout("try_read connecting\n");
1657 ret
= read_partial_banner(con
);
1660 if (process_banner(con
) < 0) {
1665 ret
= read_partial_connect(con
);
1668 if (process_connect(con
) < 0) {
1675 if (con
->in_base_pos
< 0) {
1677 * skipping + discarding content.
1679 * FIXME: there must be a better way to do this!
1681 static char buf
[1024];
1682 int skip
= min(1024, -con
->in_base_pos
);
1683 dout("skipping %d / %d bytes\n", skip
, -con
->in_base_pos
);
1684 ret
= ceph_tcp_recvmsg(con
->sock
, buf
, skip
);
1687 con
->in_base_pos
+= ret
;
1688 if (con
->in_base_pos
)
1691 if (con
->in_tag
== CEPH_MSGR_TAG_READY
) {
1695 ret
= ceph_tcp_recvmsg(con
->sock
, &con
->in_tag
, 1);
1698 dout("try_read got tag %d\n", (int)con
->in_tag
);
1699 switch (con
->in_tag
) {
1700 case CEPH_MSGR_TAG_MSG
:
1701 prepare_read_message(con
);
1703 case CEPH_MSGR_TAG_ACK
:
1704 prepare_read_ack(con
);
1706 case CEPH_MSGR_TAG_CLOSE
:
1707 set_bit(CLOSED
, &con
->state
); /* fixme */
1713 if (con
->in_tag
== CEPH_MSGR_TAG_MSG
) {
1714 ret
= read_partial_message(con
);
1718 con
->error_msg
= "bad crc";
1722 con
->error_msg
= "io error";
1728 if (con
->in_tag
== CEPH_MSGR_TAG_READY
)
1730 process_message(con
);
1733 if (con
->in_tag
== CEPH_MSGR_TAG_ACK
) {
1734 ret
= read_partial_ack(con
);
1744 dout("try_read done on %p\n", con
);
1748 pr_err("try_read bad con->in_tag = %d\n", (int)con
->in_tag
);
1749 con
->error_msg
= "protocol error, garbage tag";
1756 * Atomically queue work on a connection. Bump @con reference to
1757 * avoid races with connection teardown.
1759 * There is some trickery going on with QUEUED and BUSY because we
1760 * only want a _single_ thread operating on each connection at any
1761 * point in time, but we want to use all available CPUs.
1763 * The worker thread only proceeds if it can atomically set BUSY. It
1764 * clears QUEUED and does it's thing. When it thinks it's done, it
1765 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1766 * (tries again to set BUSY).
1768 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1769 * try to queue work. If that fails (work is already queued, or BUSY)
1770 * we give up (work also already being done or is queued) but leave QUEUED
1771 * set so that the worker thread will loop if necessary.
1773 static void queue_con(struct ceph_connection
*con
)
1775 if (test_bit(DEAD
, &con
->state
)) {
1776 dout("queue_con %p ignoring: DEAD\n",
1781 if (!con
->ops
->get(con
)) {
1782 dout("queue_con %p ref count 0\n", con
);
1786 set_bit(QUEUED
, &con
->state
);
1787 if (test_bit(BUSY
, &con
->state
)) {
1788 dout("queue_con %p - already BUSY\n", con
);
1790 } else if (!queue_work(ceph_msgr_wq
, &con
->work
.work
)) {
1791 dout("queue_con %p - already queued\n", con
);
1794 dout("queue_con %p\n", con
);
1799 * Do some work on a connection. Drop a connection ref when we're done.
1801 static void con_work(struct work_struct
*work
)
1803 struct ceph_connection
*con
= container_of(work
, struct ceph_connection
,
1808 if (test_and_set_bit(BUSY
, &con
->state
) != 0) {
1809 dout("con_work %p BUSY already set\n", con
);
1812 dout("con_work %p start, clearing QUEUED\n", con
);
1813 clear_bit(QUEUED
, &con
->state
);
1815 mutex_lock(&con
->mutex
);
1817 if (test_bit(CLOSED
, &con
->state
)) { /* e.g. if we are replaced */
1818 dout("con_work CLOSED\n");
1819 con_close_socket(con
);
1822 if (test_and_clear_bit(OPENING
, &con
->state
)) {
1823 /* reopen w/ new peer */
1824 dout("con_work OPENING\n");
1825 con_close_socket(con
);
1828 if (test_and_clear_bit(SOCK_CLOSED
, &con
->state
) ||
1829 try_read(con
) < 0 ||
1830 try_write(con
) < 0) {
1831 mutex_unlock(&con
->mutex
);
1833 ceph_fault(con
); /* error/fault path */
1838 mutex_unlock(&con
->mutex
);
1841 clear_bit(BUSY
, &con
->state
);
1842 dout("con->state=%lu\n", con
->state
);
1843 if (test_bit(QUEUED
, &con
->state
)) {
1844 if (!backoff
|| test_bit(OPENING
, &con
->state
)) {
1845 dout("con_work %p QUEUED reset, looping\n", con
);
1848 dout("con_work %p QUEUED reset, but just faulted\n", con
);
1849 clear_bit(QUEUED
, &con
->state
);
1851 dout("con_work %p done\n", con
);
1859 * Generic error/fault handler. A retry mechanism is used with
1860 * exponential backoff
1862 static void ceph_fault(struct ceph_connection
*con
)
1864 pr_err("%s%lld %s %s\n", ENTITY_NAME(con
->peer_name
),
1865 pr_addr(&con
->peer_addr
.in_addr
), con
->error_msg
);
1866 dout("fault %p state %lu to peer %s\n",
1867 con
, con
->state
, pr_addr(&con
->peer_addr
.in_addr
));
1869 if (test_bit(LOSSYTX
, &con
->state
)) {
1870 dout("fault on LOSSYTX channel\n");
1874 mutex_lock(&con
->mutex
);
1875 if (test_bit(CLOSED
, &con
->state
))
1878 con_close_socket(con
);
1881 ceph_msg_put(con
->in_msg
);
1885 /* Requeue anything that hasn't been acked */
1886 list_splice_init(&con
->out_sent
, &con
->out_queue
);
1888 /* If there are no messages in the queue, place the connection
1889 * in a STANDBY state (i.e., don't try to reconnect just yet). */
1890 if (list_empty(&con
->out_queue
) && !con
->out_keepalive_pending
) {
1891 dout("fault setting STANDBY\n");
1892 set_bit(STANDBY
, &con
->state
);
1894 /* retry after a delay. */
1895 if (con
->delay
== 0)
1896 con
->delay
= BASE_DELAY_INTERVAL
;
1897 else if (con
->delay
< MAX_DELAY_INTERVAL
)
1899 dout("fault queueing %p delay %lu\n", con
, con
->delay
);
1901 if (queue_delayed_work(ceph_msgr_wq
, &con
->work
,
1902 round_jiffies_relative(con
->delay
)) == 0)
1907 mutex_unlock(&con
->mutex
);
1910 * in case we faulted due to authentication, invalidate our
1911 * current tickets so that we can get new ones.
1913 if (con
->auth_retry
&& con
->ops
->invalidate_authorizer
) {
1914 dout("calling invalidate_authorizer()\n");
1915 con
->ops
->invalidate_authorizer(con
);
1918 if (con
->ops
->fault
)
1919 con
->ops
->fault(con
);
1925 * create a new messenger instance
1927 struct ceph_messenger
*ceph_messenger_create(struct ceph_entity_addr
*myaddr
)
1929 struct ceph_messenger
*msgr
;
1931 msgr
= kzalloc(sizeof(*msgr
), GFP_KERNEL
);
1933 return ERR_PTR(-ENOMEM
);
1935 spin_lock_init(&msgr
->global_seq_lock
);
1937 /* the zero page is needed if a request is "canceled" while the message
1938 * is being written over the socket */
1939 msgr
->zero_page
= __page_cache_alloc(GFP_KERNEL
| __GFP_ZERO
);
1940 if (!msgr
->zero_page
) {
1942 return ERR_PTR(-ENOMEM
);
1944 kmap(msgr
->zero_page
);
1947 msgr
->inst
.addr
= *myaddr
;
1949 /* select a random nonce */
1950 msgr
->inst
.addr
.type
= 0;
1951 get_random_bytes(&msgr
->inst
.addr
.nonce
, sizeof(msgr
->inst
.addr
.nonce
));
1952 encode_my_addr(msgr
);
1954 dout("messenger_create %p\n", msgr
);
1958 void ceph_messenger_destroy(struct ceph_messenger
*msgr
)
1960 dout("destroy %p\n", msgr
);
1961 kunmap(msgr
->zero_page
);
1962 __free_page(msgr
->zero_page
);
1964 dout("destroyed messenger %p\n", msgr
);
1968 * Queue up an outgoing message on the given connection.
1970 void ceph_con_send(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1972 if (test_bit(CLOSED
, &con
->state
)) {
1973 dout("con_send %p closed, dropping %p\n", con
, msg
);
1979 msg
->hdr
.src
= con
->msgr
->inst
.name
;
1981 BUG_ON(msg
->front
.iov_len
!= le32_to_cpu(msg
->hdr
.front_len
));
1983 msg
->needs_out_seq
= true;
1986 mutex_lock(&con
->mutex
);
1987 BUG_ON(!list_empty(&msg
->list_head
));
1988 list_add_tail(&msg
->list_head
, &con
->out_queue
);
1989 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg
,
1990 ENTITY_NAME(con
->peer_name
), le16_to_cpu(msg
->hdr
.type
),
1991 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
1992 le32_to_cpu(msg
->hdr
.front_len
),
1993 le32_to_cpu(msg
->hdr
.middle_len
),
1994 le32_to_cpu(msg
->hdr
.data_len
));
1995 mutex_unlock(&con
->mutex
);
1997 /* if there wasn't anything waiting to send before, queue
1999 if (test_and_set_bit(WRITE_PENDING
, &con
->state
) == 0)
2004 * Revoke a message that was previously queued for send
2006 void ceph_con_revoke(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2008 mutex_lock(&con
->mutex
);
2009 if (!list_empty(&msg
->list_head
)) {
2010 dout("con_revoke %p msg %p\n", con
, msg
);
2011 list_del_init(&msg
->list_head
);
2014 if (con
->out_msg
== msg
) {
2015 ceph_msg_put(con
->out_msg
);
2016 con
->out_msg
= NULL
;
2018 if (con
->out_kvec_is_msg
) {
2019 con
->out_skip
= con
->out_kvec_bytes
;
2020 con
->out_kvec_is_msg
= false;
2023 dout("con_revoke %p msg %p - not queued (sent?)\n", con
, msg
);
2025 mutex_unlock(&con
->mutex
);
2029 * Revoke a message that we may be reading data into
2031 void ceph_con_revoke_message(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2033 mutex_lock(&con
->mutex
);
2034 if (con
->in_msg
&& con
->in_msg
== msg
) {
2035 unsigned front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
2036 unsigned middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
2037 unsigned data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
2039 /* skip rest of message */
2040 dout("con_revoke_pages %p msg %p revoked\n", con
, msg
);
2041 con
->in_base_pos
= con
->in_base_pos
-
2042 sizeof(struct ceph_msg_header
) -
2046 sizeof(struct ceph_msg_footer
);
2047 ceph_msg_put(con
->in_msg
);
2049 con
->in_tag
= CEPH_MSGR_TAG_READY
;
2052 dout("con_revoke_pages %p msg %p pages %p no-op\n",
2053 con
, con
->in_msg
, msg
);
2055 mutex_unlock(&con
->mutex
);
2059 * Queue a keepalive byte to ensure the tcp connection is alive.
2061 void ceph_con_keepalive(struct ceph_connection
*con
)
2063 if (test_and_set_bit(KEEPALIVE_PENDING
, &con
->state
) == 0 &&
2064 test_and_set_bit(WRITE_PENDING
, &con
->state
) == 0)
2070 * construct a new message with given type, size
2071 * the new msg has a ref count of 1.
2073 struct ceph_msg
*ceph_msg_new(int type
, int front_len
, gfp_t flags
)
2077 m
= kmalloc(sizeof(*m
), flags
);
2080 kref_init(&m
->kref
);
2081 INIT_LIST_HEAD(&m
->list_head
);
2084 m
->hdr
.type
= cpu_to_le16(type
);
2085 m
->hdr
.priority
= cpu_to_le16(CEPH_MSG_PRIO_DEFAULT
);
2087 m
->hdr
.front_len
= cpu_to_le32(front_len
);
2088 m
->hdr
.middle_len
= 0;
2089 m
->hdr
.data_len
= 0;
2090 m
->hdr
.data_off
= 0;
2091 m
->hdr
.reserved
= 0;
2092 m
->footer
.front_crc
= 0;
2093 m
->footer
.middle_crc
= 0;
2094 m
->footer
.data_crc
= 0;
2095 m
->footer
.flags
= 0;
2096 m
->front_max
= front_len
;
2097 m
->front_is_vmalloc
= false;
2098 m
->more_to_follow
= false;
2103 if (front_len
> PAGE_CACHE_SIZE
) {
2104 m
->front
.iov_base
= __vmalloc(front_len
, flags
,
2106 m
->front_is_vmalloc
= true;
2108 m
->front
.iov_base
= kmalloc(front_len
, flags
);
2110 if (m
->front
.iov_base
== NULL
) {
2111 pr_err("msg_new can't allocate %d bytes\n",
2116 m
->front
.iov_base
= NULL
;
2118 m
->front
.iov_len
= front_len
;
2128 dout("ceph_msg_new %p front %d\n", m
, front_len
);
2134 pr_err("msg_new can't create type %d front %d\n", type
, front_len
);
2139 * Allocate "middle" portion of a message, if it is needed and wasn't
2140 * allocated by alloc_msg. This allows us to read a small fixed-size
2141 * per-type header in the front and then gracefully fail (i.e.,
2142 * propagate the error to the caller based on info in the front) when
2143 * the middle is too large.
2145 static int ceph_alloc_middle(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2147 int type
= le16_to_cpu(msg
->hdr
.type
);
2148 int middle_len
= le32_to_cpu(msg
->hdr
.middle_len
);
2150 dout("alloc_middle %p type %d %s middle_len %d\n", msg
, type
,
2151 ceph_msg_type_name(type
), middle_len
);
2152 BUG_ON(!middle_len
);
2153 BUG_ON(msg
->middle
);
2155 msg
->middle
= ceph_buffer_new(middle_len
, GFP_NOFS
);
2162 * Generic message allocator, for incoming messages.
2164 static struct ceph_msg
*ceph_alloc_msg(struct ceph_connection
*con
,
2165 struct ceph_msg_header
*hdr
,
2168 int type
= le16_to_cpu(hdr
->type
);
2169 int front_len
= le32_to_cpu(hdr
->front_len
);
2170 int middle_len
= le32_to_cpu(hdr
->middle_len
);
2171 struct ceph_msg
*msg
= NULL
;
2174 if (con
->ops
->alloc_msg
) {
2175 mutex_unlock(&con
->mutex
);
2176 msg
= con
->ops
->alloc_msg(con
, hdr
, skip
);
2177 mutex_lock(&con
->mutex
);
2183 msg
= ceph_msg_new(type
, front_len
, GFP_NOFS
);
2185 pr_err("unable to allocate msg type %d len %d\n",
2190 memcpy(&msg
->hdr
, &con
->in_hdr
, sizeof(con
->in_hdr
));
2192 if (middle_len
&& !msg
->middle
) {
2193 ret
= ceph_alloc_middle(con
, msg
);
2205 * Free a generically kmalloc'd message.
2207 void ceph_msg_kfree(struct ceph_msg
*m
)
2209 dout("msg_kfree %p\n", m
);
2210 if (m
->front_is_vmalloc
)
2211 vfree(m
->front
.iov_base
);
2213 kfree(m
->front
.iov_base
);
2218 * Drop a msg ref. Destroy as needed.
2220 void ceph_msg_last_put(struct kref
*kref
)
2222 struct ceph_msg
*m
= container_of(kref
, struct ceph_msg
, kref
);
2224 dout("ceph_msg_put last one on %p\n", m
);
2225 WARN_ON(!list_empty(&m
->list_head
));
2227 /* drop middle, data, if any */
2229 ceph_buffer_put(m
->middle
);
2236 ceph_pagelist_release(m
->pagelist
);
2242 ceph_msgpool_put(m
->pool
, m
);
2247 void ceph_msg_dump(struct ceph_msg
*msg
)
2249 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg
,
2250 msg
->front_max
, msg
->nr_pages
);
2251 print_hex_dump(KERN_DEBUG
, "header: ",
2252 DUMP_PREFIX_OFFSET
, 16, 1,
2253 &msg
->hdr
, sizeof(msg
->hdr
), true);
2254 print_hex_dump(KERN_DEBUG
, " front: ",
2255 DUMP_PREFIX_OFFSET
, 16, 1,
2256 msg
->front
.iov_base
, msg
->front
.iov_len
, true);
2258 print_hex_dump(KERN_DEBUG
, "middle: ",
2259 DUMP_PREFIX_OFFSET
, 16, 1,
2260 msg
->middle
->vec
.iov_base
,
2261 msg
->middle
->vec
.iov_len
, true);
2262 print_hex_dump(KERN_DEBUG
, "footer: ",
2263 DUMP_PREFIX_OFFSET
, 16, 1,
2264 &msg
->footer
, sizeof(msg
->footer
), true);