1 #include "ceph_debug.h"
3 #include <linux/crc32c.h>
4 #include <linux/ctype.h>
5 #include <linux/highmem.h>
6 #include <linux/inet.h>
7 #include <linux/kthread.h>
9 #include <linux/socket.h>
10 #include <linux/string.h>
14 #include "messenger.h"
19 * Ceph uses the messenger to exchange ceph_msg messages with other
20 * hosts in the system. The messenger provides ordered and reliable
21 * delivery. We tolerate TCP disconnects by reconnecting (with
22 * exponential backoff) in the case of a fault (disconnection, bad
23 * crc, protocol error). Acks allow sent messages to be discarded by
27 /* static tag bytes (protocol control messages) */
28 static char tag_msg
= CEPH_MSGR_TAG_MSG
;
29 static char tag_ack
= CEPH_MSGR_TAG_ACK
;
30 static char tag_keepalive
= CEPH_MSGR_TAG_KEEPALIVE
;
33 static void queue_con(struct ceph_connection
*con
);
34 static void con_work(struct work_struct
*);
35 static void ceph_fault(struct ceph_connection
*con
);
37 const char *ceph_name_type_str(int t
)
40 case CEPH_ENTITY_TYPE_MON
: return "mon";
41 case CEPH_ENTITY_TYPE_MDS
: return "mds";
42 case CEPH_ENTITY_TYPE_OSD
: return "osd";
43 case CEPH_ENTITY_TYPE_CLIENT
: return "client";
44 case CEPH_ENTITY_TYPE_ADMIN
: return "admin";
45 default: return "???";
50 * nicely render a sockaddr as a string.
52 #define MAX_ADDR_STR 20
53 static char addr_str
[MAX_ADDR_STR
][40];
54 static DEFINE_SPINLOCK(addr_str_lock
);
55 static int last_addr_str
;
57 const char *pr_addr(const struct sockaddr_storage
*ss
)
61 struct sockaddr_in
*in4
= (void *)ss
;
62 unsigned char *quad
= (void *)&in4
->sin_addr
.s_addr
;
63 struct sockaddr_in6
*in6
= (void *)ss
;
65 spin_lock(&addr_str_lock
);
67 if (last_addr_str
== MAX_ADDR_STR
)
69 spin_unlock(&addr_str_lock
);
72 switch (ss
->ss_family
) {
74 sprintf(s
, "%u.%u.%u.%u:%u",
75 (unsigned int)quad
[0],
76 (unsigned int)quad
[1],
77 (unsigned int)quad
[2],
78 (unsigned int)quad
[3],
79 (unsigned int)ntohs(in4
->sin_port
));
83 sprintf(s
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u",
84 in6
->sin6_addr
.s6_addr16
[0],
85 in6
->sin6_addr
.s6_addr16
[1],
86 in6
->sin6_addr
.s6_addr16
[2],
87 in6
->sin6_addr
.s6_addr16
[3],
88 in6
->sin6_addr
.s6_addr16
[4],
89 in6
->sin6_addr
.s6_addr16
[5],
90 in6
->sin6_addr
.s6_addr16
[6],
91 in6
->sin6_addr
.s6_addr16
[7],
92 (unsigned int)ntohs(in6
->sin6_port
));
96 sprintf(s
, "(unknown sockaddr family %d)", (int)ss
->ss_family
);
102 static void encode_my_addr(struct ceph_messenger
*msgr
)
104 memcpy(&msgr
->my_enc_addr
, &msgr
->inst
.addr
, sizeof(msgr
->my_enc_addr
));
105 ceph_encode_addr(&msgr
->my_enc_addr
);
109 * work queue for all reading and writing to/from the socket.
111 struct workqueue_struct
*ceph_msgr_wq
;
113 int __init
ceph_msgr_init(void)
115 ceph_msgr_wq
= create_workqueue("ceph-msgr");
116 if (IS_ERR(ceph_msgr_wq
)) {
117 int ret
= PTR_ERR(ceph_msgr_wq
);
118 pr_err("msgr_init failed to create workqueue: %d\n", ret
);
125 void ceph_msgr_exit(void)
127 destroy_workqueue(ceph_msgr_wq
);
131 * socket callback functions
134 /* data available on socket, or listen socket received a connect */
135 static void ceph_data_ready(struct sock
*sk
, int count_unused
)
137 struct ceph_connection
*con
=
138 (struct ceph_connection
*)sk
->sk_user_data
;
139 if (sk
->sk_state
!= TCP_CLOSE_WAIT
) {
140 dout("ceph_data_ready on %p state = %lu, queueing work\n",
146 /* socket has buffer space for writing */
147 static void ceph_write_space(struct sock
*sk
)
149 struct ceph_connection
*con
=
150 (struct ceph_connection
*)sk
->sk_user_data
;
152 /* only queue to workqueue if there is data we want to write. */
153 if (test_bit(WRITE_PENDING
, &con
->state
)) {
154 dout("ceph_write_space %p queueing write work\n", con
);
157 dout("ceph_write_space %p nothing to write\n", con
);
160 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
161 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
164 /* socket's state has changed */
165 static void ceph_state_change(struct sock
*sk
)
167 struct ceph_connection
*con
=
168 (struct ceph_connection
*)sk
->sk_user_data
;
170 dout("ceph_state_change %p state = %lu sk_state = %u\n",
171 con
, con
->state
, sk
->sk_state
);
173 if (test_bit(CLOSED
, &con
->state
))
176 switch (sk
->sk_state
) {
178 dout("ceph_state_change TCP_CLOSE\n");
180 dout("ceph_state_change TCP_CLOSE_WAIT\n");
181 if (test_and_set_bit(SOCK_CLOSED
, &con
->state
) == 0) {
182 if (test_bit(CONNECTING
, &con
->state
))
183 con
->error_msg
= "connection failed";
185 con
->error_msg
= "socket closed";
189 case TCP_ESTABLISHED
:
190 dout("ceph_state_change TCP_ESTABLISHED\n");
197 * set up socket callbacks
199 static void set_sock_callbacks(struct socket
*sock
,
200 struct ceph_connection
*con
)
202 struct sock
*sk
= sock
->sk
;
203 sk
->sk_user_data
= (void *)con
;
204 sk
->sk_data_ready
= ceph_data_ready
;
205 sk
->sk_write_space
= ceph_write_space
;
206 sk
->sk_state_change
= ceph_state_change
;
215 * initiate connection to a remote socket.
217 static struct socket
*ceph_tcp_connect(struct ceph_connection
*con
)
219 struct sockaddr
*paddr
= (struct sockaddr
*)&con
->peer_addr
.in_addr
;
224 ret
= sock_create_kern(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
228 sock
->sk
->sk_allocation
= GFP_NOFS
;
230 set_sock_callbacks(sock
, con
);
232 dout("connect %s\n", pr_addr(&con
->peer_addr
.in_addr
));
234 ret
= sock
->ops
->connect(sock
, paddr
, sizeof(*paddr
), O_NONBLOCK
);
235 if (ret
== -EINPROGRESS
) {
236 dout("connect %s EINPROGRESS sk_state = %u\n",
237 pr_addr(&con
->peer_addr
.in_addr
),
242 pr_err("connect %s error %d\n",
243 pr_addr(&con
->peer_addr
.in_addr
), ret
);
246 con
->error_msg
= "connect error";
254 static int ceph_tcp_recvmsg(struct socket
*sock
, void *buf
, size_t len
)
256 struct kvec iov
= {buf
, len
};
257 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
259 return kernel_recvmsg(sock
, &msg
, &iov
, 1, len
, msg
.msg_flags
);
263 * write something. @more is true if caller will be sending more data
266 static int ceph_tcp_sendmsg(struct socket
*sock
, struct kvec
*iov
,
267 size_t kvlen
, size_t len
, int more
)
269 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
272 msg
.msg_flags
|= MSG_MORE
;
274 msg
.msg_flags
|= MSG_EOR
; /* superfluous, but what the hell */
276 return kernel_sendmsg(sock
, &msg
, iov
, kvlen
, len
);
281 * Shutdown/close the socket for the given connection.
283 static int con_close_socket(struct ceph_connection
*con
)
287 dout("con_close_socket on %p sock %p\n", con
, con
->sock
);
290 set_bit(SOCK_CLOSED
, &con
->state
);
291 rc
= con
->sock
->ops
->shutdown(con
->sock
, SHUT_RDWR
);
292 sock_release(con
->sock
);
294 clear_bit(SOCK_CLOSED
, &con
->state
);
299 * Reset a connection. Discard all incoming and outgoing messages
300 * and clear *_seq state.
302 static void ceph_msg_remove(struct ceph_msg
*msg
)
304 list_del_init(&msg
->list_head
);
307 static void ceph_msg_remove_list(struct list_head
*head
)
309 while (!list_empty(head
)) {
310 struct ceph_msg
*msg
= list_first_entry(head
, struct ceph_msg
,
312 ceph_msg_remove(msg
);
316 static void reset_connection(struct ceph_connection
*con
)
318 /* reset connection, out_queue, msg_ and connect_seq */
319 /* discard existing out_queue and msg_seq */
320 ceph_msg_remove_list(&con
->out_queue
);
321 ceph_msg_remove_list(&con
->out_sent
);
324 ceph_msg_put(con
->in_msg
);
328 con
->connect_seq
= 0;
331 ceph_msg_put(con
->out_msg
);
338 * mark a peer down. drop any open connections.
340 void ceph_con_close(struct ceph_connection
*con
)
342 dout("con_close %p peer %s\n", con
, pr_addr(&con
->peer_addr
.in_addr
));
343 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
344 clear_bit(STANDBY
, &con
->state
); /* avoid connect_seq bump */
345 mutex_lock(&con
->mutex
);
346 reset_connection(con
);
347 cancel_delayed_work(&con
->work
);
348 mutex_unlock(&con
->mutex
);
353 * Reopen a closed connection, with a new peer address.
355 void ceph_con_open(struct ceph_connection
*con
, struct ceph_entity_addr
*addr
)
357 dout("con_open %p %s\n", con
, pr_addr(&addr
->in_addr
));
358 set_bit(OPENING
, &con
->state
);
359 clear_bit(CLOSED
, &con
->state
);
360 memcpy(&con
->peer_addr
, addr
, sizeof(*addr
));
361 con
->delay
= 0; /* reset backoff memory */
368 struct ceph_connection
*ceph_con_get(struct ceph_connection
*con
)
370 dout("con_get %p nref = %d -> %d\n", con
,
371 atomic_read(&con
->nref
), atomic_read(&con
->nref
) + 1);
372 if (atomic_inc_not_zero(&con
->nref
))
377 void ceph_con_put(struct ceph_connection
*con
)
379 dout("con_put %p nref = %d -> %d\n", con
,
380 atomic_read(&con
->nref
), atomic_read(&con
->nref
) - 1);
381 BUG_ON(atomic_read(&con
->nref
) == 0);
382 if (atomic_dec_and_test(&con
->nref
)) {
389 * initialize a new connection.
391 void ceph_con_init(struct ceph_messenger
*msgr
, struct ceph_connection
*con
)
393 dout("con_init %p\n", con
);
394 memset(con
, 0, sizeof(*con
));
395 atomic_set(&con
->nref
, 1);
397 mutex_init(&con
->mutex
);
398 INIT_LIST_HEAD(&con
->out_queue
);
399 INIT_LIST_HEAD(&con
->out_sent
);
400 INIT_DELAYED_WORK(&con
->work
, con_work
);
405 * We maintain a global counter to order connection attempts. Get
406 * a unique seq greater than @gt.
408 static u32
get_global_seq(struct ceph_messenger
*msgr
, u32 gt
)
412 spin_lock(&msgr
->global_seq_lock
);
413 if (msgr
->global_seq
< gt
)
414 msgr
->global_seq
= gt
;
415 ret
= ++msgr
->global_seq
;
416 spin_unlock(&msgr
->global_seq_lock
);
422 * Prepare footer for currently outgoing message, and finish things
423 * off. Assumes out_kvec* are already valid.. we just add on to the end.
425 static void prepare_write_message_footer(struct ceph_connection
*con
, int v
)
427 struct ceph_msg
*m
= con
->out_msg
;
429 dout("prepare_write_message_footer %p\n", con
);
430 con
->out_kvec_is_msg
= true;
431 con
->out_kvec
[v
].iov_base
= &m
->footer
;
432 con
->out_kvec
[v
].iov_len
= sizeof(m
->footer
);
433 con
->out_kvec_bytes
+= sizeof(m
->footer
);
434 con
->out_kvec_left
++;
435 con
->out_more
= m
->more_to_follow
;
436 con
->out_msg_done
= true;
440 * Prepare headers for the next outgoing message.
442 static void prepare_write_message(struct ceph_connection
*con
)
447 con
->out_kvec_bytes
= 0;
448 con
->out_kvec_is_msg
= true;
449 con
->out_msg_done
= false;
451 /* Sneak an ack in there first? If we can get it into the same
452 * TCP packet that's a good thing. */
453 if (con
->in_seq
> con
->in_seq_acked
) {
454 con
->in_seq_acked
= con
->in_seq
;
455 con
->out_kvec
[v
].iov_base
= &tag_ack
;
456 con
->out_kvec
[v
++].iov_len
= 1;
457 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
458 con
->out_kvec
[v
].iov_base
= &con
->out_temp_ack
;
459 con
->out_kvec
[v
++].iov_len
= sizeof(con
->out_temp_ack
);
460 con
->out_kvec_bytes
= 1 + sizeof(con
->out_temp_ack
);
463 m
= list_first_entry(&con
->out_queue
,
464 struct ceph_msg
, list_head
);
466 if (test_bit(LOSSYTX
, &con
->state
)) {
467 list_del_init(&m
->list_head
);
469 /* put message on sent list */
471 list_move_tail(&m
->list_head
, &con
->out_sent
);
474 m
->hdr
.seq
= cpu_to_le64(++con
->out_seq
);
476 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
477 m
, con
->out_seq
, le16_to_cpu(m
->hdr
.type
),
478 le32_to_cpu(m
->hdr
.front_len
), le32_to_cpu(m
->hdr
.middle_len
),
479 le32_to_cpu(m
->hdr
.data_len
),
481 BUG_ON(le32_to_cpu(m
->hdr
.front_len
) != m
->front
.iov_len
);
483 /* tag + hdr + front + middle */
484 con
->out_kvec
[v
].iov_base
= &tag_msg
;
485 con
->out_kvec
[v
++].iov_len
= 1;
486 con
->out_kvec
[v
].iov_base
= &m
->hdr
;
487 con
->out_kvec
[v
++].iov_len
= sizeof(m
->hdr
);
488 con
->out_kvec
[v
++] = m
->front
;
490 con
->out_kvec
[v
++] = m
->middle
->vec
;
491 con
->out_kvec_left
= v
;
492 con
->out_kvec_bytes
+= 1 + sizeof(m
->hdr
) + m
->front
.iov_len
+
493 (m
->middle
? m
->middle
->vec
.iov_len
: 0);
494 con
->out_kvec_cur
= con
->out_kvec
;
496 /* fill in crc (except data pages), footer */
497 con
->out_msg
->hdr
.crc
=
498 cpu_to_le32(crc32c(0, (void *)&m
->hdr
,
499 sizeof(m
->hdr
) - sizeof(m
->hdr
.crc
)));
500 con
->out_msg
->footer
.flags
= CEPH_MSG_FOOTER_COMPLETE
;
501 con
->out_msg
->footer
.front_crc
=
502 cpu_to_le32(crc32c(0, m
->front
.iov_base
, m
->front
.iov_len
));
504 con
->out_msg
->footer
.middle_crc
=
505 cpu_to_le32(crc32c(0, m
->middle
->vec
.iov_base
,
506 m
->middle
->vec
.iov_len
));
508 con
->out_msg
->footer
.middle_crc
= 0;
509 con
->out_msg
->footer
.data_crc
= 0;
510 dout("prepare_write_message front_crc %u data_crc %u\n",
511 le32_to_cpu(con
->out_msg
->footer
.front_crc
),
512 le32_to_cpu(con
->out_msg
->footer
.middle_crc
));
514 /* is there a data payload? */
515 if (le32_to_cpu(m
->hdr
.data_len
) > 0) {
516 /* initialize page iterator */
517 con
->out_msg_pos
.page
= 0;
518 con
->out_msg_pos
.page_pos
=
519 le16_to_cpu(m
->hdr
.data_off
) & ~PAGE_MASK
;
520 con
->out_msg_pos
.data_pos
= 0;
521 con
->out_msg_pos
.did_page_crc
= 0;
522 con
->out_more
= 1; /* data + footer will follow */
524 /* no, queue up footer too and be done */
525 prepare_write_message_footer(con
, v
);
528 set_bit(WRITE_PENDING
, &con
->state
);
534 static void prepare_write_ack(struct ceph_connection
*con
)
536 dout("prepare_write_ack %p %llu -> %llu\n", con
,
537 con
->in_seq_acked
, con
->in_seq
);
538 con
->in_seq_acked
= con
->in_seq
;
540 con
->out_kvec
[0].iov_base
= &tag_ack
;
541 con
->out_kvec
[0].iov_len
= 1;
542 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
543 con
->out_kvec
[1].iov_base
= &con
->out_temp_ack
;
544 con
->out_kvec
[1].iov_len
= sizeof(con
->out_temp_ack
);
545 con
->out_kvec_left
= 2;
546 con
->out_kvec_bytes
= 1 + sizeof(con
->out_temp_ack
);
547 con
->out_kvec_cur
= con
->out_kvec
;
548 con
->out_more
= 1; /* more will follow.. eventually.. */
549 set_bit(WRITE_PENDING
, &con
->state
);
553 * Prepare to write keepalive byte.
555 static void prepare_write_keepalive(struct ceph_connection
*con
)
557 dout("prepare_write_keepalive %p\n", con
);
558 con
->out_kvec
[0].iov_base
= &tag_keepalive
;
559 con
->out_kvec
[0].iov_len
= 1;
560 con
->out_kvec_left
= 1;
561 con
->out_kvec_bytes
= 1;
562 con
->out_kvec_cur
= con
->out_kvec
;
563 set_bit(WRITE_PENDING
, &con
->state
);
567 * Connection negotiation.
570 static void prepare_connect_authorizer(struct ceph_connection
*con
)
574 int auth_protocol
= 0;
576 mutex_unlock(&con
->mutex
);
577 if (con
->ops
->get_authorizer
)
578 con
->ops
->get_authorizer(con
, &auth_buf
, &auth_len
,
579 &auth_protocol
, &con
->auth_reply_buf
,
580 &con
->auth_reply_buf_len
,
582 mutex_lock(&con
->mutex
);
584 con
->out_connect
.authorizer_protocol
= cpu_to_le32(auth_protocol
);
585 con
->out_connect
.authorizer_len
= cpu_to_le32(auth_len
);
587 con
->out_kvec
[con
->out_kvec_left
].iov_base
= auth_buf
;
588 con
->out_kvec
[con
->out_kvec_left
].iov_len
= auth_len
;
589 con
->out_kvec_left
++;
590 con
->out_kvec_bytes
+= auth_len
;
594 * We connected to a peer and are saying hello.
596 static void prepare_write_banner(struct ceph_messenger
*msgr
,
597 struct ceph_connection
*con
)
599 int len
= strlen(CEPH_BANNER
);
601 con
->out_kvec
[0].iov_base
= CEPH_BANNER
;
602 con
->out_kvec
[0].iov_len
= len
;
603 con
->out_kvec
[1].iov_base
= &msgr
->my_enc_addr
;
604 con
->out_kvec
[1].iov_len
= sizeof(msgr
->my_enc_addr
);
605 con
->out_kvec_left
= 2;
606 con
->out_kvec_bytes
= len
+ sizeof(msgr
->my_enc_addr
);
607 con
->out_kvec_cur
= con
->out_kvec
;
609 set_bit(WRITE_PENDING
, &con
->state
);
612 static void prepare_write_connect(struct ceph_messenger
*msgr
,
613 struct ceph_connection
*con
,
616 unsigned global_seq
= get_global_seq(con
->msgr
, 0);
619 switch (con
->peer_name
.type
) {
620 case CEPH_ENTITY_TYPE_MON
:
621 proto
= CEPH_MONC_PROTOCOL
;
623 case CEPH_ENTITY_TYPE_OSD
:
624 proto
= CEPH_OSDC_PROTOCOL
;
626 case CEPH_ENTITY_TYPE_MDS
:
627 proto
= CEPH_MDSC_PROTOCOL
;
633 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con
,
634 con
->connect_seq
, global_seq
, proto
);
636 con
->out_connect
.features
= CEPH_FEATURE_SUPPORTED
;
637 con
->out_connect
.host_type
= cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT
);
638 con
->out_connect
.connect_seq
= cpu_to_le32(con
->connect_seq
);
639 con
->out_connect
.global_seq
= cpu_to_le32(global_seq
);
640 con
->out_connect
.protocol_version
= cpu_to_le32(proto
);
641 con
->out_connect
.flags
= 0;
644 con
->out_kvec_left
= 0;
645 con
->out_kvec_bytes
= 0;
647 con
->out_kvec
[con
->out_kvec_left
].iov_base
= &con
->out_connect
;
648 con
->out_kvec
[con
->out_kvec_left
].iov_len
= sizeof(con
->out_connect
);
649 con
->out_kvec_left
++;
650 con
->out_kvec_bytes
+= sizeof(con
->out_connect
);
651 con
->out_kvec_cur
= con
->out_kvec
;
653 set_bit(WRITE_PENDING
, &con
->state
);
655 prepare_connect_authorizer(con
);
660 * write as much of pending kvecs to the socket as we can.
662 * 0 -> socket full, but more to do
665 static int write_partial_kvec(struct ceph_connection
*con
)
669 dout("write_partial_kvec %p %d left\n", con
, con
->out_kvec_bytes
);
670 while (con
->out_kvec_bytes
> 0) {
671 ret
= ceph_tcp_sendmsg(con
->sock
, con
->out_kvec_cur
,
672 con
->out_kvec_left
, con
->out_kvec_bytes
,
676 con
->out_kvec_bytes
-= ret
;
677 if (con
->out_kvec_bytes
== 0)
680 if (ret
>= con
->out_kvec_cur
->iov_len
) {
681 ret
-= con
->out_kvec_cur
->iov_len
;
683 con
->out_kvec_left
--;
685 con
->out_kvec_cur
->iov_len
-= ret
;
686 con
->out_kvec_cur
->iov_base
+= ret
;
692 con
->out_kvec_left
= 0;
693 con
->out_kvec_is_msg
= false;
696 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con
,
697 con
->out_kvec_bytes
, con
->out_kvec_left
, ret
);
698 return ret
; /* done! */
702 * Write as much message data payload as we can. If we finish, queue
704 * 1 -> done, footer is now queued in out_kvec[].
705 * 0 -> socket full, but more to do
708 static int write_partial_msg_pages(struct ceph_connection
*con
)
710 struct ceph_msg
*msg
= con
->out_msg
;
711 unsigned data_len
= le32_to_cpu(msg
->hdr
.data_len
);
713 int crc
= con
->msgr
->nocrc
;
716 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
717 con
, con
->out_msg
, con
->out_msg_pos
.page
, con
->out_msg
->nr_pages
,
718 con
->out_msg_pos
.page_pos
);
720 while (con
->out_msg_pos
.page
< con
->out_msg
->nr_pages
) {
721 struct page
*page
= NULL
;
725 * if we are calculating the data crc (the default), we need
726 * to map the page. if our pages[] has been revoked, use the
730 page
= msg
->pages
[con
->out_msg_pos
.page
];
733 } else if (msg
->pagelist
) {
734 page
= list_first_entry(&msg
->pagelist
->head
,
739 page
= con
->msgr
->zero_page
;
741 kaddr
= page_address(con
->msgr
->zero_page
);
743 len
= min((int)(PAGE_SIZE
- con
->out_msg_pos
.page_pos
),
744 (int)(data_len
- con
->out_msg_pos
.data_pos
));
745 if (crc
&& !con
->out_msg_pos
.did_page_crc
) {
746 void *base
= kaddr
+ con
->out_msg_pos
.page_pos
;
747 u32 tmpcrc
= le32_to_cpu(con
->out_msg
->footer
.data_crc
);
749 BUG_ON(kaddr
== NULL
);
750 con
->out_msg
->footer
.data_crc
=
751 cpu_to_le32(crc32c(tmpcrc
, base
, len
));
752 con
->out_msg_pos
.did_page_crc
= 1;
755 ret
= kernel_sendpage(con
->sock
, page
,
756 con
->out_msg_pos
.page_pos
, len
,
757 MSG_DONTWAIT
| MSG_NOSIGNAL
|
760 if (crc
&& (msg
->pages
|| msg
->pagelist
))
766 con
->out_msg_pos
.data_pos
+= ret
;
767 con
->out_msg_pos
.page_pos
+= ret
;
769 con
->out_msg_pos
.page_pos
= 0;
770 con
->out_msg_pos
.page
++;
771 con
->out_msg_pos
.did_page_crc
= 0;
773 list_move_tail(&page
->lru
,
774 &msg
->pagelist
->head
);
778 dout("write_partial_msg_pages %p msg %p done\n", con
, msg
);
780 /* prepare and queue up footer, too */
782 con
->out_msg
->footer
.flags
|= CEPH_MSG_FOOTER_NOCRC
;
783 con
->out_kvec_bytes
= 0;
784 con
->out_kvec_left
= 0;
785 con
->out_kvec_cur
= con
->out_kvec
;
786 prepare_write_message_footer(con
, 0);
795 static int write_partial_skip(struct ceph_connection
*con
)
799 while (con
->out_skip
> 0) {
801 .iov_base
= page_address(con
->msgr
->zero_page
),
802 .iov_len
= min(con
->out_skip
, (int)PAGE_CACHE_SIZE
)
805 ret
= ceph_tcp_sendmsg(con
->sock
, &iov
, 1, iov
.iov_len
, 1);
808 con
->out_skip
-= ret
;
816 * Prepare to read connection handshake, or an ack.
818 static void prepare_read_banner(struct ceph_connection
*con
)
820 dout("prepare_read_banner %p\n", con
);
821 con
->in_base_pos
= 0;
824 static void prepare_read_connect(struct ceph_connection
*con
)
826 dout("prepare_read_connect %p\n", con
);
827 con
->in_base_pos
= 0;
830 static void prepare_read_connect_retry(struct ceph_connection
*con
)
832 dout("prepare_read_connect_retry %p\n", con
);
833 con
->in_base_pos
= strlen(CEPH_BANNER
) + sizeof(con
->actual_peer_addr
)
834 + sizeof(con
->peer_addr_for_me
);
837 static void prepare_read_ack(struct ceph_connection
*con
)
839 dout("prepare_read_ack %p\n", con
);
840 con
->in_base_pos
= 0;
843 static void prepare_read_tag(struct ceph_connection
*con
)
845 dout("prepare_read_tag %p\n", con
);
846 con
->in_base_pos
= 0;
847 con
->in_tag
= CEPH_MSGR_TAG_READY
;
851 * Prepare to read a message.
853 static int prepare_read_message(struct ceph_connection
*con
)
855 dout("prepare_read_message %p\n", con
);
856 BUG_ON(con
->in_msg
!= NULL
);
857 con
->in_base_pos
= 0;
858 con
->in_front_crc
= con
->in_middle_crc
= con
->in_data_crc
= 0;
863 static int read_partial(struct ceph_connection
*con
,
864 int *to
, int size
, void *object
)
867 while (con
->in_base_pos
< *to
) {
868 int left
= *to
- con
->in_base_pos
;
869 int have
= size
- left
;
870 int ret
= ceph_tcp_recvmsg(con
->sock
, object
+ have
, left
);
873 con
->in_base_pos
+= ret
;
880 * Read all or part of the connect-side handshake on a new connection
882 static int read_partial_banner(struct ceph_connection
*con
)
886 dout("read_partial_banner %p at %d\n", con
, con
->in_base_pos
);
889 ret
= read_partial(con
, &to
, strlen(CEPH_BANNER
), con
->in_banner
);
892 ret
= read_partial(con
, &to
, sizeof(con
->actual_peer_addr
),
893 &con
->actual_peer_addr
);
896 ret
= read_partial(con
, &to
, sizeof(con
->peer_addr_for_me
),
897 &con
->peer_addr_for_me
);
904 static int read_partial_connect(struct ceph_connection
*con
)
908 dout("read_partial_connect %p at %d\n", con
, con
->in_base_pos
);
910 ret
= read_partial(con
, &to
, sizeof(con
->in_reply
), &con
->in_reply
);
913 ret
= read_partial(con
, &to
, le32_to_cpu(con
->in_reply
.authorizer_len
),
914 con
->auth_reply_buf
);
918 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
919 con
, (int)con
->in_reply
.tag
,
920 le32_to_cpu(con
->in_reply
.connect_seq
),
921 le32_to_cpu(con
->in_reply
.global_seq
));
928 * Verify the hello banner looks okay.
930 static int verify_hello(struct ceph_connection
*con
)
932 if (memcmp(con
->in_banner
, CEPH_BANNER
, strlen(CEPH_BANNER
))) {
933 pr_err("connect to %s got bad banner\n",
934 pr_addr(&con
->peer_addr
.in_addr
));
935 con
->error_msg
= "protocol error, bad banner";
941 static bool addr_is_blank(struct sockaddr_storage
*ss
)
943 switch (ss
->ss_family
) {
945 return ((struct sockaddr_in
*)ss
)->sin_addr
.s_addr
== 0;
948 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[0] == 0 &&
949 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[1] == 0 &&
950 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[2] == 0 &&
951 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[3] == 0;
956 static int addr_port(struct sockaddr_storage
*ss
)
958 switch (ss
->ss_family
) {
960 return ntohs(((struct sockaddr_in
*)ss
)->sin_port
);
962 return ntohs(((struct sockaddr_in6
*)ss
)->sin6_port
);
967 static void addr_set_port(struct sockaddr_storage
*ss
, int p
)
969 switch (ss
->ss_family
) {
971 ((struct sockaddr_in
*)ss
)->sin_port
= htons(p
);
973 ((struct sockaddr_in6
*)ss
)->sin6_port
= htons(p
);
978 * Parse an ip[:port] list into an addr array. Use the default
979 * monitor port if a port isn't specified.
981 int ceph_parse_ips(const char *c
, const char *end
,
982 struct ceph_entity_addr
*addr
,
983 int max_count
, int *count
)
988 dout("parse_ips on '%.*s'\n", (int)(end
-c
), c
);
989 for (i
= 0; i
< max_count
; i
++) {
991 struct sockaddr_storage
*ss
= &addr
[i
].in_addr
;
992 struct sockaddr_in
*in4
= (void *)ss
;
993 struct sockaddr_in6
*in6
= (void *)ss
;
996 memset(ss
, 0, sizeof(*ss
));
997 if (in4_pton(p
, end
- p
, (u8
*)&in4
->sin_addr
.s_addr
,
999 ss
->ss_family
= AF_INET
;
1000 } else if (in6_pton(p
, end
- p
, (u8
*)&in6
->sin6_addr
.s6_addr
,
1002 ss
->ss_family
= AF_INET6
;
1009 if (p
< end
&& *p
== ':') {
1012 while (p
< end
&& *p
>= '0' && *p
<= '9') {
1013 port
= (port
* 10) + (*p
- '0');
1016 if (port
> 65535 || port
== 0)
1019 port
= CEPH_MON_PORT
;
1022 addr_set_port(ss
, port
);
1024 dout("parse_ips got %s\n", pr_addr(ss
));
1041 pr_err("parse_ips bad ip '%s'\n", c
);
1045 static int process_banner(struct ceph_connection
*con
)
1047 dout("process_banner on %p\n", con
);
1049 if (verify_hello(con
) < 0)
1052 ceph_decode_addr(&con
->actual_peer_addr
);
1053 ceph_decode_addr(&con
->peer_addr_for_me
);
1056 * Make sure the other end is who we wanted. note that the other
1057 * end may not yet know their ip address, so if it's 0.0.0.0, give
1058 * them the benefit of the doubt.
1060 if (memcmp(&con
->peer_addr
, &con
->actual_peer_addr
,
1061 sizeof(con
->peer_addr
)) != 0 &&
1062 !(addr_is_blank(&con
->actual_peer_addr
.in_addr
) &&
1063 con
->actual_peer_addr
.nonce
== con
->peer_addr
.nonce
)) {
1064 pr_warning("wrong peer, want %s/%lld, got %s/%lld\n",
1065 pr_addr(&con
->peer_addr
.in_addr
),
1066 le64_to_cpu(con
->peer_addr
.nonce
),
1067 pr_addr(&con
->actual_peer_addr
.in_addr
),
1068 le64_to_cpu(con
->actual_peer_addr
.nonce
));
1069 con
->error_msg
= "wrong peer at address";
1074 * did we learn our address?
1076 if (addr_is_blank(&con
->msgr
->inst
.addr
.in_addr
)) {
1077 int port
= addr_port(&con
->msgr
->inst
.addr
.in_addr
);
1079 memcpy(&con
->msgr
->inst
.addr
.in_addr
,
1080 &con
->peer_addr_for_me
.in_addr
,
1081 sizeof(con
->peer_addr_for_me
.in_addr
));
1082 addr_set_port(&con
->msgr
->inst
.addr
.in_addr
, port
);
1083 encode_my_addr(con
->msgr
);
1084 dout("process_banner learned my addr is %s\n",
1085 pr_addr(&con
->msgr
->inst
.addr
.in_addr
));
1088 set_bit(NEGOTIATING
, &con
->state
);
1089 prepare_read_connect(con
);
1093 static void fail_protocol(struct ceph_connection
*con
)
1095 reset_connection(con
);
1096 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
1098 mutex_unlock(&con
->mutex
);
1099 if (con
->ops
->bad_proto
)
1100 con
->ops
->bad_proto(con
);
1101 mutex_lock(&con
->mutex
);
1104 static int process_connect(struct ceph_connection
*con
)
1106 u64 sup_feat
= CEPH_FEATURE_SUPPORTED
;
1107 u64 req_feat
= CEPH_FEATURE_REQUIRED
;
1108 u64 server_feat
= le64_to_cpu(con
->in_reply
.features
);
1110 dout("process_connect on %p tag %d\n", con
, (int)con
->in_tag
);
1112 switch (con
->in_reply
.tag
) {
1113 case CEPH_MSGR_TAG_FEATURES
:
1114 pr_err("%s%lld %s feature set mismatch,"
1115 " my %llx < server's %llx, missing %llx\n",
1116 ENTITY_NAME(con
->peer_name
),
1117 pr_addr(&con
->peer_addr
.in_addr
),
1118 sup_feat
, server_feat
, server_feat
& ~sup_feat
);
1119 con
->error_msg
= "missing required protocol features";
1123 case CEPH_MSGR_TAG_BADPROTOVER
:
1124 pr_err("%s%lld %s protocol version mismatch,"
1125 " my %d != server's %d\n",
1126 ENTITY_NAME(con
->peer_name
),
1127 pr_addr(&con
->peer_addr
.in_addr
),
1128 le32_to_cpu(con
->out_connect
.protocol_version
),
1129 le32_to_cpu(con
->in_reply
.protocol_version
));
1130 con
->error_msg
= "protocol version mismatch";
1134 case CEPH_MSGR_TAG_BADAUTHORIZER
:
1136 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con
,
1138 if (con
->auth_retry
== 2) {
1139 con
->error_msg
= "connect authorization failure";
1140 reset_connection(con
);
1141 set_bit(CLOSED
, &con
->state
);
1144 con
->auth_retry
= 1;
1145 prepare_write_connect(con
->msgr
, con
, 0);
1146 prepare_read_connect_retry(con
);
1149 case CEPH_MSGR_TAG_RESETSESSION
:
1151 * If we connected with a large connect_seq but the peer
1152 * has no record of a session with us (no connection, or
1153 * connect_seq == 0), they will send RESETSESION to indicate
1154 * that they must have reset their session, and may have
1157 dout("process_connect got RESET peer seq %u\n",
1158 le32_to_cpu(con
->in_connect
.connect_seq
));
1159 pr_err("%s%lld %s connection reset\n",
1160 ENTITY_NAME(con
->peer_name
),
1161 pr_addr(&con
->peer_addr
.in_addr
));
1162 reset_connection(con
);
1163 prepare_write_connect(con
->msgr
, con
, 0);
1164 prepare_read_connect(con
);
1166 /* Tell ceph about it. */
1167 mutex_unlock(&con
->mutex
);
1168 pr_info("reset on %s%lld\n", ENTITY_NAME(con
->peer_name
));
1169 if (con
->ops
->peer_reset
)
1170 con
->ops
->peer_reset(con
);
1171 mutex_lock(&con
->mutex
);
1174 case CEPH_MSGR_TAG_RETRY_SESSION
:
1176 * If we sent a smaller connect_seq than the peer has, try
1177 * again with a larger value.
1179 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1180 le32_to_cpu(con
->out_connect
.connect_seq
),
1181 le32_to_cpu(con
->in_connect
.connect_seq
));
1182 con
->connect_seq
= le32_to_cpu(con
->in_connect
.connect_seq
);
1183 prepare_write_connect(con
->msgr
, con
, 0);
1184 prepare_read_connect(con
);
1187 case CEPH_MSGR_TAG_RETRY_GLOBAL
:
1189 * If we sent a smaller global_seq than the peer has, try
1190 * again with a larger value.
1192 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
1193 con
->peer_global_seq
,
1194 le32_to_cpu(con
->in_connect
.global_seq
));
1195 get_global_seq(con
->msgr
,
1196 le32_to_cpu(con
->in_connect
.global_seq
));
1197 prepare_write_connect(con
->msgr
, con
, 0);
1198 prepare_read_connect(con
);
1201 case CEPH_MSGR_TAG_READY
:
1202 if (req_feat
& ~server_feat
) {
1203 pr_err("%s%lld %s protocol feature mismatch,"
1204 " my required %llx > server's %llx, need %llx\n",
1205 ENTITY_NAME(con
->peer_name
),
1206 pr_addr(&con
->peer_addr
.in_addr
),
1207 req_feat
, server_feat
, req_feat
& ~server_feat
);
1208 con
->error_msg
= "missing required protocol features";
1212 clear_bit(CONNECTING
, &con
->state
);
1213 con
->peer_global_seq
= le32_to_cpu(con
->in_reply
.global_seq
);
1215 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1216 con
->peer_global_seq
,
1217 le32_to_cpu(con
->in_reply
.connect_seq
),
1219 WARN_ON(con
->connect_seq
!=
1220 le32_to_cpu(con
->in_reply
.connect_seq
));
1222 if (con
->in_reply
.flags
& CEPH_MSG_CONNECT_LOSSY
)
1223 set_bit(LOSSYTX
, &con
->state
);
1225 prepare_read_tag(con
);
1228 case CEPH_MSGR_TAG_WAIT
:
1230 * If there is a connection race (we are opening
1231 * connections to each other), one of us may just have
1232 * to WAIT. This shouldn't happen if we are the
1235 pr_err("process_connect peer connecting WAIT\n");
1238 pr_err("connect protocol error, will retry\n");
1239 con
->error_msg
= "protocol error, garbage tag during connect";
1247 * read (part of) an ack
1249 static int read_partial_ack(struct ceph_connection
*con
)
1253 return read_partial(con
, &to
, sizeof(con
->in_temp_ack
),
1259 * We can finally discard anything that's been acked.
1261 static void process_ack(struct ceph_connection
*con
)
1264 u64 ack
= le64_to_cpu(con
->in_temp_ack
);
1267 while (!list_empty(&con
->out_sent
)) {
1268 m
= list_first_entry(&con
->out_sent
, struct ceph_msg
,
1270 seq
= le64_to_cpu(m
->hdr
.seq
);
1273 dout("got ack for seq %llu type %d at %p\n", seq
,
1274 le16_to_cpu(m
->hdr
.type
), m
);
1277 prepare_read_tag(con
);
1283 static int read_partial_message_section(struct ceph_connection
*con
,
1284 struct kvec
*section
, unsigned int sec_len
,
1292 while (section
->iov_len
< sec_len
) {
1293 BUG_ON(section
->iov_base
== NULL
);
1294 left
= sec_len
- section
->iov_len
;
1295 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)section
->iov_base
+
1296 section
->iov_len
, left
);
1299 section
->iov_len
+= ret
;
1300 if (section
->iov_len
== sec_len
)
1301 *crc
= crc32c(0, section
->iov_base
,
1308 static struct ceph_msg
*ceph_alloc_msg(struct ceph_connection
*con
,
1309 struct ceph_msg_header
*hdr
,
1312 * read (part of) a message.
1314 static int read_partial_message(struct ceph_connection
*con
)
1316 struct ceph_msg
*m
= con
->in_msg
;
1320 unsigned front_len
, middle_len
, data_len
, data_off
;
1321 int datacrc
= con
->msgr
->nocrc
;
1324 dout("read_partial_message con %p msg %p\n", con
, m
);
1327 while (con
->in_base_pos
< sizeof(con
->in_hdr
)) {
1328 left
= sizeof(con
->in_hdr
) - con
->in_base_pos
;
1329 ret
= ceph_tcp_recvmsg(con
->sock
,
1330 (char *)&con
->in_hdr
+ con
->in_base_pos
,
1334 con
->in_base_pos
+= ret
;
1335 if (con
->in_base_pos
== sizeof(con
->in_hdr
)) {
1336 u32 crc
= crc32c(0, (void *)&con
->in_hdr
,
1337 sizeof(con
->in_hdr
) - sizeof(con
->in_hdr
.crc
));
1338 if (crc
!= le32_to_cpu(con
->in_hdr
.crc
)) {
1339 pr_err("read_partial_message bad hdr "
1340 " crc %u != expected %u\n",
1341 crc
, con
->in_hdr
.crc
);
1346 front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
1347 if (front_len
> CEPH_MSG_MAX_FRONT_LEN
)
1349 middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
1350 if (middle_len
> CEPH_MSG_MAX_DATA_LEN
)
1352 data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
1353 if (data_len
> CEPH_MSG_MAX_DATA_LEN
)
1355 data_off
= le16_to_cpu(con
->in_hdr
.data_off
);
1357 /* allocate message? */
1359 dout("got hdr type %d front %d data %d\n", con
->in_hdr
.type
,
1360 con
->in_hdr
.front_len
, con
->in_hdr
.data_len
);
1361 con
->in_msg
= ceph_alloc_msg(con
, &con
->in_hdr
, &skip
);
1363 /* skip this message */
1364 dout("alloc_msg returned NULL, skipping message\n");
1365 con
->in_base_pos
= -front_len
- middle_len
- data_len
-
1367 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1370 if (IS_ERR(con
->in_msg
)) {
1371 ret
= PTR_ERR(con
->in_msg
);
1374 "error allocating memory for incoming message";
1378 m
->front
.iov_len
= 0; /* haven't read it yet */
1380 m
->middle
->vec
.iov_len
= 0;
1382 con
->in_msg_pos
.page
= 0;
1383 con
->in_msg_pos
.page_pos
= data_off
& ~PAGE_MASK
;
1384 con
->in_msg_pos
.data_pos
= 0;
1388 ret
= read_partial_message_section(con
, &m
->front
, front_len
,
1389 &con
->in_front_crc
);
1395 ret
= read_partial_message_section(con
, &m
->middle
->vec
, middle_len
,
1396 &con
->in_middle_crc
);
1402 while (con
->in_msg_pos
.data_pos
< data_len
) {
1403 left
= min((int)(data_len
- con
->in_msg_pos
.data_pos
),
1404 (int)(PAGE_SIZE
- con
->in_msg_pos
.page_pos
));
1405 BUG_ON(m
->pages
== NULL
);
1406 p
= kmap(m
->pages
[con
->in_msg_pos
.page
]);
1407 ret
= ceph_tcp_recvmsg(con
->sock
, p
+ con
->in_msg_pos
.page_pos
,
1409 if (ret
> 0 && datacrc
)
1411 crc32c(con
->in_data_crc
,
1412 p
+ con
->in_msg_pos
.page_pos
, ret
);
1413 kunmap(m
->pages
[con
->in_msg_pos
.page
]);
1416 con
->in_msg_pos
.data_pos
+= ret
;
1417 con
->in_msg_pos
.page_pos
+= ret
;
1418 if (con
->in_msg_pos
.page_pos
== PAGE_SIZE
) {
1419 con
->in_msg_pos
.page_pos
= 0;
1420 con
->in_msg_pos
.page
++;
1425 to
= sizeof(m
->hdr
) + sizeof(m
->footer
);
1426 while (con
->in_base_pos
< to
) {
1427 left
= to
- con
->in_base_pos
;
1428 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)&m
->footer
+
1429 (con
->in_base_pos
- sizeof(m
->hdr
)),
1433 con
->in_base_pos
+= ret
;
1435 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1436 m
, front_len
, m
->footer
.front_crc
, middle_len
,
1437 m
->footer
.middle_crc
, data_len
, m
->footer
.data_crc
);
1440 if (con
->in_front_crc
!= le32_to_cpu(m
->footer
.front_crc
)) {
1441 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1442 m
, con
->in_front_crc
, m
->footer
.front_crc
);
1445 if (con
->in_middle_crc
!= le32_to_cpu(m
->footer
.middle_crc
)) {
1446 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1447 m
, con
->in_middle_crc
, m
->footer
.middle_crc
);
1451 (m
->footer
.flags
& CEPH_MSG_FOOTER_NOCRC
) == 0 &&
1452 con
->in_data_crc
!= le32_to_cpu(m
->footer
.data_crc
)) {
1453 pr_err("read_partial_message %p data crc %u != exp. %u\n", m
,
1454 con
->in_data_crc
, le32_to_cpu(m
->footer
.data_crc
));
1458 return 1; /* done! */
1462 * Process message. This happens in the worker thread. The callback should
1463 * be careful not to do anything that waits on other incoming messages or it
1466 static void process_message(struct ceph_connection
*con
)
1468 struct ceph_msg
*msg
;
1473 /* if first message, set peer_name */
1474 if (con
->peer_name
.type
== 0)
1475 con
->peer_name
= msg
->hdr
.src
.name
;
1478 mutex_unlock(&con
->mutex
);
1480 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1481 msg
, le64_to_cpu(msg
->hdr
.seq
),
1482 ENTITY_NAME(msg
->hdr
.src
.name
),
1483 le16_to_cpu(msg
->hdr
.type
),
1484 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
1485 le32_to_cpu(msg
->hdr
.front_len
),
1486 le32_to_cpu(msg
->hdr
.data_len
),
1487 con
->in_front_crc
, con
->in_middle_crc
, con
->in_data_crc
);
1488 con
->ops
->dispatch(con
, msg
);
1490 mutex_lock(&con
->mutex
);
1491 prepare_read_tag(con
);
1496 * Write something to the socket. Called in a worker thread when the
1497 * socket appears to be writeable and we have something ready to send.
1499 static int try_write(struct ceph_connection
*con
)
1501 struct ceph_messenger
*msgr
= con
->msgr
;
1504 dout("try_write start %p state %lu nref %d\n", con
, con
->state
,
1505 atomic_read(&con
->nref
));
1507 mutex_lock(&con
->mutex
);
1509 dout("try_write out_kvec_bytes %d\n", con
->out_kvec_bytes
);
1511 /* open the socket first? */
1512 if (con
->sock
== NULL
) {
1514 * if we were STANDBY and are reconnecting _this_
1515 * connection, bump connect_seq now. Always bump
1518 if (test_and_clear_bit(STANDBY
, &con
->state
))
1521 prepare_write_banner(msgr
, con
);
1522 prepare_write_connect(msgr
, con
, 1);
1523 prepare_read_banner(con
);
1524 set_bit(CONNECTING
, &con
->state
);
1525 clear_bit(NEGOTIATING
, &con
->state
);
1527 BUG_ON(con
->in_msg
);
1528 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1529 dout("try_write initiating connect on %p new state %lu\n",
1531 con
->sock
= ceph_tcp_connect(con
);
1532 if (IS_ERR(con
->sock
)) {
1534 con
->error_msg
= "connect error";
1541 /* kvec data queued? */
1542 if (con
->out_skip
) {
1543 ret
= write_partial_skip(con
);
1547 dout("try_write write_partial_skip err %d\n", ret
);
1551 if (con
->out_kvec_left
) {
1552 ret
= write_partial_kvec(con
);
1559 if (con
->out_msg_done
) {
1560 ceph_msg_put(con
->out_msg
);
1561 con
->out_msg
= NULL
; /* we're done with this one */
1565 ret
= write_partial_msg_pages(con
);
1567 goto more_kvec
; /* we need to send the footer, too! */
1571 dout("try_write write_partial_msg_pages err %d\n",
1578 if (!test_bit(CONNECTING
, &con
->state
)) {
1579 /* is anything else pending? */
1580 if (!list_empty(&con
->out_queue
)) {
1581 prepare_write_message(con
);
1584 if (con
->in_seq
> con
->in_seq_acked
) {
1585 prepare_write_ack(con
);
1588 if (test_and_clear_bit(KEEPALIVE_PENDING
, &con
->state
)) {
1589 prepare_write_keepalive(con
);
1594 /* Nothing to do! */
1595 clear_bit(WRITE_PENDING
, &con
->state
);
1596 dout("try_write nothing else to write.\n");
1600 mutex_unlock(&con
->mutex
);
1601 dout("try_write done on %p\n", con
);
1608 * Read what we can from the socket.
1610 static int try_read(struct ceph_connection
*con
)
1612 struct ceph_messenger
*msgr
;
1618 if (test_bit(STANDBY
, &con
->state
))
1621 dout("try_read start on %p\n", con
);
1624 mutex_lock(&con
->mutex
);
1627 dout("try_read tag %d in_base_pos %d\n", (int)con
->in_tag
,
1629 if (test_bit(CONNECTING
, &con
->state
)) {
1630 if (!test_bit(NEGOTIATING
, &con
->state
)) {
1631 dout("try_read connecting\n");
1632 ret
= read_partial_banner(con
);
1635 if (process_banner(con
) < 0) {
1640 ret
= read_partial_connect(con
);
1643 if (process_connect(con
) < 0) {
1650 if (con
->in_base_pos
< 0) {
1652 * skipping + discarding content.
1654 * FIXME: there must be a better way to do this!
1656 static char buf
[1024];
1657 int skip
= min(1024, -con
->in_base_pos
);
1658 dout("skipping %d / %d bytes\n", skip
, -con
->in_base_pos
);
1659 ret
= ceph_tcp_recvmsg(con
->sock
, buf
, skip
);
1662 con
->in_base_pos
+= ret
;
1663 if (con
->in_base_pos
)
1666 if (con
->in_tag
== CEPH_MSGR_TAG_READY
) {
1670 ret
= ceph_tcp_recvmsg(con
->sock
, &con
->in_tag
, 1);
1673 dout("try_read got tag %d\n", (int)con
->in_tag
);
1674 switch (con
->in_tag
) {
1675 case CEPH_MSGR_TAG_MSG
:
1676 prepare_read_message(con
);
1678 case CEPH_MSGR_TAG_ACK
:
1679 prepare_read_ack(con
);
1681 case CEPH_MSGR_TAG_CLOSE
:
1682 set_bit(CLOSED
, &con
->state
); /* fixme */
1688 if (con
->in_tag
== CEPH_MSGR_TAG_MSG
) {
1689 ret
= read_partial_message(con
);
1693 con
->error_msg
= "bad crc";
1697 con
->error_msg
= "io error";
1703 if (con
->in_tag
== CEPH_MSGR_TAG_READY
)
1705 process_message(con
);
1708 if (con
->in_tag
== CEPH_MSGR_TAG_ACK
) {
1709 ret
= read_partial_ack(con
);
1719 mutex_unlock(&con
->mutex
);
1720 dout("try_read done on %p\n", con
);
1724 pr_err("try_read bad con->in_tag = %d\n", (int)con
->in_tag
);
1725 con
->error_msg
= "protocol error, garbage tag";
1732 * Atomically queue work on a connection. Bump @con reference to
1733 * avoid races with connection teardown.
1735 * There is some trickery going on with QUEUED and BUSY because we
1736 * only want a _single_ thread operating on each connection at any
1737 * point in time, but we want to use all available CPUs.
1739 * The worker thread only proceeds if it can atomically set BUSY. It
1740 * clears QUEUED and does it's thing. When it thinks it's done, it
1741 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1742 * (tries again to set BUSY).
1744 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1745 * try to queue work. If that fails (work is already queued, or BUSY)
1746 * we give up (work also already being done or is queued) but leave QUEUED
1747 * set so that the worker thread will loop if necessary.
1749 static void queue_con(struct ceph_connection
*con
)
1751 if (test_bit(DEAD
, &con
->state
)) {
1752 dout("queue_con %p ignoring: DEAD\n",
1757 if (!con
->ops
->get(con
)) {
1758 dout("queue_con %p ref count 0\n", con
);
1762 set_bit(QUEUED
, &con
->state
);
1763 if (test_bit(BUSY
, &con
->state
)) {
1764 dout("queue_con %p - already BUSY\n", con
);
1766 } else if (!queue_work(ceph_msgr_wq
, &con
->work
.work
)) {
1767 dout("queue_con %p - already queued\n", con
);
1770 dout("queue_con %p\n", con
);
1775 * Do some work on a connection. Drop a connection ref when we're done.
1777 static void con_work(struct work_struct
*work
)
1779 struct ceph_connection
*con
= container_of(work
, struct ceph_connection
,
1784 if (test_and_set_bit(BUSY
, &con
->state
) != 0) {
1785 dout("con_work %p BUSY already set\n", con
);
1788 dout("con_work %p start, clearing QUEUED\n", con
);
1789 clear_bit(QUEUED
, &con
->state
);
1791 if (test_bit(CLOSED
, &con
->state
)) { /* e.g. if we are replaced */
1792 dout("con_work CLOSED\n");
1793 con_close_socket(con
);
1796 if (test_and_clear_bit(OPENING
, &con
->state
)) {
1797 /* reopen w/ new peer */
1798 dout("con_work OPENING\n");
1799 con_close_socket(con
);
1802 if (test_and_clear_bit(SOCK_CLOSED
, &con
->state
) ||
1803 try_read(con
) < 0 ||
1804 try_write(con
) < 0) {
1806 ceph_fault(con
); /* error/fault path */
1810 clear_bit(BUSY
, &con
->state
);
1811 dout("con->state=%lu\n", con
->state
);
1812 if (test_bit(QUEUED
, &con
->state
)) {
1813 if (!backoff
|| test_bit(OPENING
, &con
->state
)) {
1814 dout("con_work %p QUEUED reset, looping\n", con
);
1817 dout("con_work %p QUEUED reset, but just faulted\n", con
);
1818 clear_bit(QUEUED
, &con
->state
);
1820 dout("con_work %p done\n", con
);
1828 * Generic error/fault handler. A retry mechanism is used with
1829 * exponential backoff
1831 static void ceph_fault(struct ceph_connection
*con
)
1833 pr_err("%s%lld %s %s\n", ENTITY_NAME(con
->peer_name
),
1834 pr_addr(&con
->peer_addr
.in_addr
), con
->error_msg
);
1835 dout("fault %p state %lu to peer %s\n",
1836 con
, con
->state
, pr_addr(&con
->peer_addr
.in_addr
));
1838 if (test_bit(LOSSYTX
, &con
->state
)) {
1839 dout("fault on LOSSYTX channel\n");
1843 clear_bit(BUSY
, &con
->state
); /* to avoid an improbable race */
1845 mutex_lock(&con
->mutex
);
1846 if (test_bit(CLOSED
, &con
->state
))
1849 con_close_socket(con
);
1852 ceph_msg_put(con
->in_msg
);
1857 * in case we faulted due to authentication, invalidate our
1858 * current tickets so that we can get new ones.
1860 if (con
->auth_retry
&& con
->ops
->invalidate_authorizer
) {
1861 dout("calling invalidate_authorizer()\n");
1862 con
->ops
->invalidate_authorizer(con
);
1865 /* If there are no messages in the queue, place the connection
1866 * in a STANDBY state (i.e., don't try to reconnect just yet). */
1867 if (list_empty(&con
->out_queue
) && !con
->out_keepalive_pending
) {
1868 dout("fault setting STANDBY\n");
1869 set_bit(STANDBY
, &con
->state
);
1870 mutex_unlock(&con
->mutex
);
1874 /* Requeue anything that hasn't been acked, and retry after a
1876 list_splice_init(&con
->out_sent
, &con
->out_queue
);
1878 if (con
->delay
== 0)
1879 con
->delay
= BASE_DELAY_INTERVAL
;
1880 else if (con
->delay
< MAX_DELAY_INTERVAL
)
1883 /* explicitly schedule work to try to reconnect again later. */
1884 dout("fault queueing %p delay %lu\n", con
, con
->delay
);
1886 if (queue_delayed_work(ceph_msgr_wq
, &con
->work
,
1887 round_jiffies_relative(con
->delay
)) == 0)
1891 mutex_unlock(&con
->mutex
);
1893 if (con
->ops
->fault
)
1894 con
->ops
->fault(con
);
1900 * create a new messenger instance
1902 struct ceph_messenger
*ceph_messenger_create(struct ceph_entity_addr
*myaddr
)
1904 struct ceph_messenger
*msgr
;
1906 msgr
= kzalloc(sizeof(*msgr
), GFP_KERNEL
);
1908 return ERR_PTR(-ENOMEM
);
1910 spin_lock_init(&msgr
->global_seq_lock
);
1912 /* the zero page is needed if a request is "canceled" while the message
1913 * is being written over the socket */
1914 msgr
->zero_page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
1915 if (!msgr
->zero_page
) {
1917 return ERR_PTR(-ENOMEM
);
1919 kmap(msgr
->zero_page
);
1922 msgr
->inst
.addr
= *myaddr
;
1924 /* select a random nonce */
1925 msgr
->inst
.addr
.type
= 0;
1926 get_random_bytes(&msgr
->inst
.addr
.nonce
, sizeof(msgr
->inst
.addr
.nonce
));
1927 encode_my_addr(msgr
);
1929 dout("messenger_create %p\n", msgr
);
1933 void ceph_messenger_destroy(struct ceph_messenger
*msgr
)
1935 dout("destroy %p\n", msgr
);
1936 kunmap(msgr
->zero_page
);
1937 __free_page(msgr
->zero_page
);
1939 dout("destroyed messenger %p\n", msgr
);
1943 * Queue up an outgoing message on the given connection.
1945 void ceph_con_send(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1947 if (test_bit(CLOSED
, &con
->state
)) {
1948 dout("con_send %p closed, dropping %p\n", con
, msg
);
1954 msg
->hdr
.src
.name
= con
->msgr
->inst
.name
;
1955 msg
->hdr
.src
.addr
= con
->msgr
->my_enc_addr
;
1956 msg
->hdr
.orig_src
= msg
->hdr
.src
;
1959 mutex_lock(&con
->mutex
);
1960 BUG_ON(!list_empty(&msg
->list_head
));
1961 list_add_tail(&msg
->list_head
, &con
->out_queue
);
1962 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg
,
1963 ENTITY_NAME(con
->peer_name
), le16_to_cpu(msg
->hdr
.type
),
1964 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
1965 le32_to_cpu(msg
->hdr
.front_len
),
1966 le32_to_cpu(msg
->hdr
.middle_len
),
1967 le32_to_cpu(msg
->hdr
.data_len
));
1968 mutex_unlock(&con
->mutex
);
1970 /* if there wasn't anything waiting to send before, queue
1972 if (test_and_set_bit(WRITE_PENDING
, &con
->state
) == 0)
1977 * Revoke a message that was previously queued for send
1979 void ceph_con_revoke(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1981 mutex_lock(&con
->mutex
);
1982 if (!list_empty(&msg
->list_head
)) {
1983 dout("con_revoke %p msg %p\n", con
, msg
);
1984 list_del_init(&msg
->list_head
);
1987 if (con
->out_msg
== msg
) {
1988 ceph_msg_put(con
->out_msg
);
1989 con
->out_msg
= NULL
;
1991 if (con
->out_kvec_is_msg
) {
1992 con
->out_skip
= con
->out_kvec_bytes
;
1993 con
->out_kvec_is_msg
= false;
1996 dout("con_revoke %p msg %p - not queued (sent?)\n", con
, msg
);
1998 mutex_unlock(&con
->mutex
);
2002 * Revoke a message that we may be reading data into
2004 void ceph_con_revoke_message(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2006 mutex_lock(&con
->mutex
);
2007 if (con
->in_msg
&& con
->in_msg
== msg
) {
2008 unsigned front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
2009 unsigned middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
2010 unsigned data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
2012 /* skip rest of message */
2013 dout("con_revoke_pages %p msg %p revoked\n", con
, msg
);
2014 con
->in_base_pos
= con
->in_base_pos
-
2015 sizeof(struct ceph_msg_header
) -
2019 sizeof(struct ceph_msg_footer
);
2020 ceph_msg_put(con
->in_msg
);
2022 con
->in_tag
= CEPH_MSGR_TAG_READY
;
2024 dout("con_revoke_pages %p msg %p pages %p no-op\n",
2025 con
, con
->in_msg
, msg
);
2027 mutex_unlock(&con
->mutex
);
2031 * Queue a keepalive byte to ensure the tcp connection is alive.
2033 void ceph_con_keepalive(struct ceph_connection
*con
)
2035 if (test_and_set_bit(KEEPALIVE_PENDING
, &con
->state
) == 0 &&
2036 test_and_set_bit(WRITE_PENDING
, &con
->state
) == 0)
2042 * construct a new message with given type, size
2043 * the new msg has a ref count of 1.
2045 struct ceph_msg
*ceph_msg_new(int type
, int front_len
,
2046 int page_len
, int page_off
, struct page
**pages
)
2050 m
= kmalloc(sizeof(*m
), GFP_NOFS
);
2053 kref_init(&m
->kref
);
2054 INIT_LIST_HEAD(&m
->list_head
);
2056 m
->hdr
.type
= cpu_to_le16(type
);
2057 m
->hdr
.front_len
= cpu_to_le32(front_len
);
2058 m
->hdr
.middle_len
= 0;
2059 m
->hdr
.data_len
= cpu_to_le32(page_len
);
2060 m
->hdr
.data_off
= cpu_to_le16(page_off
);
2061 m
->hdr
.priority
= cpu_to_le16(CEPH_MSG_PRIO_DEFAULT
);
2062 m
->footer
.front_crc
= 0;
2063 m
->footer
.middle_crc
= 0;
2064 m
->footer
.data_crc
= 0;
2065 m
->front_max
= front_len
;
2066 m
->front_is_vmalloc
= false;
2067 m
->more_to_follow
= false;
2072 if (front_len
> PAGE_CACHE_SIZE
) {
2073 m
->front
.iov_base
= __vmalloc(front_len
, GFP_NOFS
,
2075 m
->front_is_vmalloc
= true;
2077 m
->front
.iov_base
= kmalloc(front_len
, GFP_NOFS
);
2079 if (m
->front
.iov_base
== NULL
) {
2080 pr_err("msg_new can't allocate %d bytes\n",
2085 m
->front
.iov_base
= NULL
;
2087 m
->front
.iov_len
= front_len
;
2093 m
->nr_pages
= calc_pages_for(page_off
, page_len
);
2097 dout("ceph_msg_new %p page %d~%d -> %d\n", m
, page_off
, page_len
,
2104 pr_err("msg_new can't create type %d len %d\n", type
, front_len
);
2105 return ERR_PTR(-ENOMEM
);
2109 * Allocate "middle" portion of a message, if it is needed and wasn't
2110 * allocated by alloc_msg. This allows us to read a small fixed-size
2111 * per-type header in the front and then gracefully fail (i.e.,
2112 * propagate the error to the caller based on info in the front) when
2113 * the middle is too large.
2115 static int ceph_alloc_middle(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2117 int type
= le16_to_cpu(msg
->hdr
.type
);
2118 int middle_len
= le32_to_cpu(msg
->hdr
.middle_len
);
2120 dout("alloc_middle %p type %d %s middle_len %d\n", msg
, type
,
2121 ceph_msg_type_name(type
), middle_len
);
2122 BUG_ON(!middle_len
);
2123 BUG_ON(msg
->middle
);
2125 msg
->middle
= ceph_buffer_new(middle_len
, GFP_NOFS
);
2132 * Generic message allocator, for incoming messages.
2134 static struct ceph_msg
*ceph_alloc_msg(struct ceph_connection
*con
,
2135 struct ceph_msg_header
*hdr
,
2138 int type
= le16_to_cpu(hdr
->type
);
2139 int front_len
= le32_to_cpu(hdr
->front_len
);
2140 int middle_len
= le32_to_cpu(hdr
->middle_len
);
2141 struct ceph_msg
*msg
= NULL
;
2144 if (con
->ops
->alloc_msg
) {
2145 mutex_unlock(&con
->mutex
);
2146 msg
= con
->ops
->alloc_msg(con
, hdr
, skip
);
2147 mutex_lock(&con
->mutex
);
2156 msg
= ceph_msg_new(type
, front_len
, 0, 0, NULL
);
2158 pr_err("unable to allocate msg type %d len %d\n",
2160 return ERR_PTR(-ENOMEM
);
2163 memcpy(&msg
->hdr
, &con
->in_hdr
, sizeof(con
->in_hdr
));
2166 ret
= ceph_alloc_middle(con
, msg
);
2179 * Free a generically kmalloc'd message.
2181 void ceph_msg_kfree(struct ceph_msg
*m
)
2183 dout("msg_kfree %p\n", m
);
2184 if (m
->front_is_vmalloc
)
2185 vfree(m
->front
.iov_base
);
2187 kfree(m
->front
.iov_base
);
2192 * Drop a msg ref. Destroy as needed.
2194 void ceph_msg_last_put(struct kref
*kref
)
2196 struct ceph_msg
*m
= container_of(kref
, struct ceph_msg
, kref
);
2198 dout("ceph_msg_put last one on %p\n", m
);
2199 WARN_ON(!list_empty(&m
->list_head
));
2201 /* drop middle, data, if any */
2203 ceph_buffer_put(m
->middle
);
2210 ceph_pagelist_release(m
->pagelist
);
2216 ceph_msgpool_put(m
->pool
, m
);
2221 void ceph_msg_dump(struct ceph_msg
*msg
)
2223 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg
,
2224 msg
->front_max
, msg
->nr_pages
);
2225 print_hex_dump(KERN_DEBUG
, "header: ",
2226 DUMP_PREFIX_OFFSET
, 16, 1,
2227 &msg
->hdr
, sizeof(msg
->hdr
), true);
2228 print_hex_dump(KERN_DEBUG
, " front: ",
2229 DUMP_PREFIX_OFFSET
, 16, 1,
2230 msg
->front
.iov_base
, msg
->front
.iov_len
, true);
2232 print_hex_dump(KERN_DEBUG
, "middle: ",
2233 DUMP_PREFIX_OFFSET
, 16, 1,
2234 msg
->middle
->vec
.iov_base
,
2235 msg
->middle
->vec
.iov_len
, true);
2236 print_hex_dump(KERN_DEBUG
, "footer: ",
2237 DUMP_PREFIX_OFFSET
, 16, 1,
2238 &msg
->footer
, sizeof(msg
->footer
), true);