1 #include "ceph_debug.h"
3 #include <linux/crc32c.h>
4 #include <linux/ctype.h>
5 #include <linux/highmem.h>
6 #include <linux/inet.h>
7 #include <linux/kthread.h>
9 #include <linux/slab.h>
10 #include <linux/socket.h>
11 #include <linux/string.h>
15 #include "messenger.h"
20 * Ceph uses the messenger to exchange ceph_msg messages with other
21 * hosts in the system. The messenger provides ordered and reliable
22 * delivery. We tolerate TCP disconnects by reconnecting (with
23 * exponential backoff) in the case of a fault (disconnection, bad
24 * crc, protocol error). Acks allow sent messages to be discarded by
28 /* static tag bytes (protocol control messages) */
29 static char tag_msg
= CEPH_MSGR_TAG_MSG
;
30 static char tag_ack
= CEPH_MSGR_TAG_ACK
;
31 static char tag_keepalive
= CEPH_MSGR_TAG_KEEPALIVE
;
34 static struct lock_class_key socket_class
;
38 static void queue_con(struct ceph_connection
*con
);
39 static void con_work(struct work_struct
*);
40 static void ceph_fault(struct ceph_connection
*con
);
43 * nicely render a sockaddr as a string.
45 #define MAX_ADDR_STR 20
46 #define MAX_ADDR_STR_LEN 60
47 static char addr_str
[MAX_ADDR_STR
][MAX_ADDR_STR_LEN
];
48 static DEFINE_SPINLOCK(addr_str_lock
);
49 static int last_addr_str
;
51 const char *pr_addr(const struct sockaddr_storage
*ss
)
55 struct sockaddr_in
*in4
= (void *)ss
;
56 struct sockaddr_in6
*in6
= (void *)ss
;
58 spin_lock(&addr_str_lock
);
60 if (last_addr_str
== MAX_ADDR_STR
)
62 spin_unlock(&addr_str_lock
);
65 switch (ss
->ss_family
) {
67 snprintf(s
, MAX_ADDR_STR_LEN
, "%pI4:%u", &in4
->sin_addr
,
68 (unsigned int)ntohs(in4
->sin_port
));
72 snprintf(s
, MAX_ADDR_STR_LEN
, "[%pI6c]:%u", &in6
->sin6_addr
,
73 (unsigned int)ntohs(in6
->sin6_port
));
77 sprintf(s
, "(unknown sockaddr family %d)", (int)ss
->ss_family
);
83 static void encode_my_addr(struct ceph_messenger
*msgr
)
85 memcpy(&msgr
->my_enc_addr
, &msgr
->inst
.addr
, sizeof(msgr
->my_enc_addr
));
86 ceph_encode_addr(&msgr
->my_enc_addr
);
90 * work queue for all reading and writing to/from the socket.
92 struct workqueue_struct
*ceph_msgr_wq
;
94 int __init
ceph_msgr_init(void)
96 ceph_msgr_wq
= create_workqueue("ceph-msgr");
97 if (IS_ERR(ceph_msgr_wq
)) {
98 int ret
= PTR_ERR(ceph_msgr_wq
);
99 pr_err("msgr_init failed to create workqueue: %d\n", ret
);
106 void ceph_msgr_exit(void)
108 destroy_workqueue(ceph_msgr_wq
);
111 void ceph_msgr_flush(void)
113 flush_workqueue(ceph_msgr_wq
);
118 * socket callback functions
121 /* data available on socket, or listen socket received a connect */
122 static void ceph_data_ready(struct sock
*sk
, int count_unused
)
124 struct ceph_connection
*con
=
125 (struct ceph_connection
*)sk
->sk_user_data
;
126 if (sk
->sk_state
!= TCP_CLOSE_WAIT
) {
127 dout("ceph_data_ready on %p state = %lu, queueing work\n",
133 /* socket has buffer space for writing */
134 static void ceph_write_space(struct sock
*sk
)
136 struct ceph_connection
*con
=
137 (struct ceph_connection
*)sk
->sk_user_data
;
139 /* only queue to workqueue if there is data we want to write. */
140 if (test_bit(WRITE_PENDING
, &con
->state
)) {
141 dout("ceph_write_space %p queueing write work\n", con
);
144 dout("ceph_write_space %p nothing to write\n", con
);
147 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
148 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
151 /* socket's state has changed */
152 static void ceph_state_change(struct sock
*sk
)
154 struct ceph_connection
*con
=
155 (struct ceph_connection
*)sk
->sk_user_data
;
157 dout("ceph_state_change %p state = %lu sk_state = %u\n",
158 con
, con
->state
, sk
->sk_state
);
160 if (test_bit(CLOSED
, &con
->state
))
163 switch (sk
->sk_state
) {
165 dout("ceph_state_change TCP_CLOSE\n");
167 dout("ceph_state_change TCP_CLOSE_WAIT\n");
168 if (test_and_set_bit(SOCK_CLOSED
, &con
->state
) == 0) {
169 if (test_bit(CONNECTING
, &con
->state
))
170 con
->error_msg
= "connection failed";
172 con
->error_msg
= "socket closed";
176 case TCP_ESTABLISHED
:
177 dout("ceph_state_change TCP_ESTABLISHED\n");
184 * set up socket callbacks
186 static void set_sock_callbacks(struct socket
*sock
,
187 struct ceph_connection
*con
)
189 struct sock
*sk
= sock
->sk
;
190 sk
->sk_user_data
= (void *)con
;
191 sk
->sk_data_ready
= ceph_data_ready
;
192 sk
->sk_write_space
= ceph_write_space
;
193 sk
->sk_state_change
= ceph_state_change
;
202 * initiate connection to a remote socket.
204 static struct socket
*ceph_tcp_connect(struct ceph_connection
*con
)
206 struct sockaddr_storage
*paddr
= &con
->peer_addr
.in_addr
;
211 ret
= sock_create_kern(con
->peer_addr
.in_addr
.ss_family
, SOCK_STREAM
,
216 sock
->sk
->sk_allocation
= GFP_NOFS
;
218 #ifdef CONFIG_LOCKDEP
219 lockdep_set_class(&sock
->sk
->sk_lock
, &socket_class
);
222 set_sock_callbacks(sock
, con
);
224 dout("connect %s\n", pr_addr(&con
->peer_addr
.in_addr
));
226 ret
= sock
->ops
->connect(sock
, (struct sockaddr
*)paddr
, sizeof(*paddr
),
228 if (ret
== -EINPROGRESS
) {
229 dout("connect %s EINPROGRESS sk_state = %u\n",
230 pr_addr(&con
->peer_addr
.in_addr
),
235 pr_err("connect %s error %d\n",
236 pr_addr(&con
->peer_addr
.in_addr
), ret
);
239 con
->error_msg
= "connect error";
247 static int ceph_tcp_recvmsg(struct socket
*sock
, void *buf
, size_t len
)
249 struct kvec iov
= {buf
, len
};
250 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
252 return kernel_recvmsg(sock
, &msg
, &iov
, 1, len
, msg
.msg_flags
);
256 * write something. @more is true if caller will be sending more data
259 static int ceph_tcp_sendmsg(struct socket
*sock
, struct kvec
*iov
,
260 size_t kvlen
, size_t len
, int more
)
262 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
265 msg
.msg_flags
|= MSG_MORE
;
267 msg
.msg_flags
|= MSG_EOR
; /* superfluous, but what the hell */
269 return kernel_sendmsg(sock
, &msg
, iov
, kvlen
, len
);
274 * Shutdown/close the socket for the given connection.
276 static int con_close_socket(struct ceph_connection
*con
)
280 dout("con_close_socket on %p sock %p\n", con
, con
->sock
);
283 set_bit(SOCK_CLOSED
, &con
->state
);
284 rc
= con
->sock
->ops
->shutdown(con
->sock
, SHUT_RDWR
);
285 sock_release(con
->sock
);
287 clear_bit(SOCK_CLOSED
, &con
->state
);
292 * Reset a connection. Discard all incoming and outgoing messages
293 * and clear *_seq state.
295 static void ceph_msg_remove(struct ceph_msg
*msg
)
297 list_del_init(&msg
->list_head
);
300 static void ceph_msg_remove_list(struct list_head
*head
)
302 while (!list_empty(head
)) {
303 struct ceph_msg
*msg
= list_first_entry(head
, struct ceph_msg
,
305 ceph_msg_remove(msg
);
309 static void reset_connection(struct ceph_connection
*con
)
311 /* reset connection, out_queue, msg_ and connect_seq */
312 /* discard existing out_queue and msg_seq */
313 ceph_msg_remove_list(&con
->out_queue
);
314 ceph_msg_remove_list(&con
->out_sent
);
317 ceph_msg_put(con
->in_msg
);
321 con
->connect_seq
= 0;
324 ceph_msg_put(con
->out_msg
);
327 con
->out_keepalive_pending
= false;
329 con
->in_seq_acked
= 0;
333 * mark a peer down. drop any open connections.
335 void ceph_con_close(struct ceph_connection
*con
)
337 dout("con_close %p peer %s\n", con
, pr_addr(&con
->peer_addr
.in_addr
));
338 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
339 clear_bit(STANDBY
, &con
->state
); /* avoid connect_seq bump */
340 clear_bit(LOSSYTX
, &con
->state
); /* so we retry next connect */
341 clear_bit(KEEPALIVE_PENDING
, &con
->state
);
342 clear_bit(WRITE_PENDING
, &con
->state
);
343 mutex_lock(&con
->mutex
);
344 reset_connection(con
);
345 con
->peer_global_seq
= 0;
346 cancel_delayed_work(&con
->work
);
347 mutex_unlock(&con
->mutex
);
352 * Reopen a closed connection, with a new peer address.
354 void ceph_con_open(struct ceph_connection
*con
, struct ceph_entity_addr
*addr
)
356 dout("con_open %p %s\n", con
, pr_addr(&addr
->in_addr
));
357 set_bit(OPENING
, &con
->state
);
358 clear_bit(CLOSED
, &con
->state
);
359 memcpy(&con
->peer_addr
, addr
, sizeof(*addr
));
360 con
->delay
= 0; /* reset backoff memory */
365 * return true if this connection ever successfully opened
367 bool ceph_con_opened(struct ceph_connection
*con
)
369 return con
->connect_seq
> 0;
375 struct ceph_connection
*ceph_con_get(struct ceph_connection
*con
)
377 dout("con_get %p nref = %d -> %d\n", con
,
378 atomic_read(&con
->nref
), atomic_read(&con
->nref
) + 1);
379 if (atomic_inc_not_zero(&con
->nref
))
384 void ceph_con_put(struct ceph_connection
*con
)
386 dout("con_put %p nref = %d -> %d\n", con
,
387 atomic_read(&con
->nref
), atomic_read(&con
->nref
) - 1);
388 BUG_ON(atomic_read(&con
->nref
) == 0);
389 if (atomic_dec_and_test(&con
->nref
)) {
396 * initialize a new connection.
398 void ceph_con_init(struct ceph_messenger
*msgr
, struct ceph_connection
*con
)
400 dout("con_init %p\n", con
);
401 memset(con
, 0, sizeof(*con
));
402 atomic_set(&con
->nref
, 1);
404 mutex_init(&con
->mutex
);
405 INIT_LIST_HEAD(&con
->out_queue
);
406 INIT_LIST_HEAD(&con
->out_sent
);
407 INIT_DELAYED_WORK(&con
->work
, con_work
);
412 * We maintain a global counter to order connection attempts. Get
413 * a unique seq greater than @gt.
415 static u32
get_global_seq(struct ceph_messenger
*msgr
, u32 gt
)
419 spin_lock(&msgr
->global_seq_lock
);
420 if (msgr
->global_seq
< gt
)
421 msgr
->global_seq
= gt
;
422 ret
= ++msgr
->global_seq
;
423 spin_unlock(&msgr
->global_seq_lock
);
429 * Prepare footer for currently outgoing message, and finish things
430 * off. Assumes out_kvec* are already valid.. we just add on to the end.
432 static void prepare_write_message_footer(struct ceph_connection
*con
, int v
)
434 struct ceph_msg
*m
= con
->out_msg
;
436 dout("prepare_write_message_footer %p\n", con
);
437 con
->out_kvec_is_msg
= true;
438 con
->out_kvec
[v
].iov_base
= &m
->footer
;
439 con
->out_kvec
[v
].iov_len
= sizeof(m
->footer
);
440 con
->out_kvec_bytes
+= sizeof(m
->footer
);
441 con
->out_kvec_left
++;
442 con
->out_more
= m
->more_to_follow
;
443 con
->out_msg_done
= true;
447 * Prepare headers for the next outgoing message.
449 static void prepare_write_message(struct ceph_connection
*con
)
454 con
->out_kvec_bytes
= 0;
455 con
->out_kvec_is_msg
= true;
456 con
->out_msg_done
= false;
458 /* Sneak an ack in there first? If we can get it into the same
459 * TCP packet that's a good thing. */
460 if (con
->in_seq
> con
->in_seq_acked
) {
461 con
->in_seq_acked
= con
->in_seq
;
462 con
->out_kvec
[v
].iov_base
= &tag_ack
;
463 con
->out_kvec
[v
++].iov_len
= 1;
464 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
465 con
->out_kvec
[v
].iov_base
= &con
->out_temp_ack
;
466 con
->out_kvec
[v
++].iov_len
= sizeof(con
->out_temp_ack
);
467 con
->out_kvec_bytes
= 1 + sizeof(con
->out_temp_ack
);
470 m
= list_first_entry(&con
->out_queue
,
471 struct ceph_msg
, list_head
);
473 if (test_bit(LOSSYTX
, &con
->state
)) {
474 list_del_init(&m
->list_head
);
476 /* put message on sent list */
478 list_move_tail(&m
->list_head
, &con
->out_sent
);
482 * only assign outgoing seq # if we haven't sent this message
483 * yet. if it is requeued, resend with it's original seq.
485 if (m
->needs_out_seq
) {
486 m
->hdr
.seq
= cpu_to_le64(++con
->out_seq
);
487 m
->needs_out_seq
= false;
490 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
491 m
, con
->out_seq
, le16_to_cpu(m
->hdr
.type
),
492 le32_to_cpu(m
->hdr
.front_len
), le32_to_cpu(m
->hdr
.middle_len
),
493 le32_to_cpu(m
->hdr
.data_len
),
495 BUG_ON(le32_to_cpu(m
->hdr
.front_len
) != m
->front
.iov_len
);
497 /* tag + hdr + front + middle */
498 con
->out_kvec
[v
].iov_base
= &tag_msg
;
499 con
->out_kvec
[v
++].iov_len
= 1;
500 con
->out_kvec
[v
].iov_base
= &m
->hdr
;
501 con
->out_kvec
[v
++].iov_len
= sizeof(m
->hdr
);
502 con
->out_kvec
[v
++] = m
->front
;
504 con
->out_kvec
[v
++] = m
->middle
->vec
;
505 con
->out_kvec_left
= v
;
506 con
->out_kvec_bytes
+= 1 + sizeof(m
->hdr
) + m
->front
.iov_len
+
507 (m
->middle
? m
->middle
->vec
.iov_len
: 0);
508 con
->out_kvec_cur
= con
->out_kvec
;
510 /* fill in crc (except data pages), footer */
511 con
->out_msg
->hdr
.crc
=
512 cpu_to_le32(crc32c(0, (void *)&m
->hdr
,
513 sizeof(m
->hdr
) - sizeof(m
->hdr
.crc
)));
514 con
->out_msg
->footer
.flags
= CEPH_MSG_FOOTER_COMPLETE
;
515 con
->out_msg
->footer
.front_crc
=
516 cpu_to_le32(crc32c(0, m
->front
.iov_base
, m
->front
.iov_len
));
518 con
->out_msg
->footer
.middle_crc
=
519 cpu_to_le32(crc32c(0, m
->middle
->vec
.iov_base
,
520 m
->middle
->vec
.iov_len
));
522 con
->out_msg
->footer
.middle_crc
= 0;
523 con
->out_msg
->footer
.data_crc
= 0;
524 dout("prepare_write_message front_crc %u data_crc %u\n",
525 le32_to_cpu(con
->out_msg
->footer
.front_crc
),
526 le32_to_cpu(con
->out_msg
->footer
.middle_crc
));
528 /* is there a data payload? */
529 if (le32_to_cpu(m
->hdr
.data_len
) > 0) {
530 /* initialize page iterator */
531 con
->out_msg_pos
.page
= 0;
532 con
->out_msg_pos
.page_pos
=
533 le16_to_cpu(m
->hdr
.data_off
) & ~PAGE_MASK
;
534 con
->out_msg_pos
.data_pos
= 0;
535 con
->out_msg_pos
.did_page_crc
= 0;
536 con
->out_more
= 1; /* data + footer will follow */
538 /* no, queue up footer too and be done */
539 prepare_write_message_footer(con
, v
);
542 set_bit(WRITE_PENDING
, &con
->state
);
548 static void prepare_write_ack(struct ceph_connection
*con
)
550 dout("prepare_write_ack %p %llu -> %llu\n", con
,
551 con
->in_seq_acked
, con
->in_seq
);
552 con
->in_seq_acked
= con
->in_seq
;
554 con
->out_kvec
[0].iov_base
= &tag_ack
;
555 con
->out_kvec
[0].iov_len
= 1;
556 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
557 con
->out_kvec
[1].iov_base
= &con
->out_temp_ack
;
558 con
->out_kvec
[1].iov_len
= sizeof(con
->out_temp_ack
);
559 con
->out_kvec_left
= 2;
560 con
->out_kvec_bytes
= 1 + sizeof(con
->out_temp_ack
);
561 con
->out_kvec_cur
= con
->out_kvec
;
562 con
->out_more
= 1; /* more will follow.. eventually.. */
563 set_bit(WRITE_PENDING
, &con
->state
);
567 * Prepare to write keepalive byte.
569 static void prepare_write_keepalive(struct ceph_connection
*con
)
571 dout("prepare_write_keepalive %p\n", con
);
572 con
->out_kvec
[0].iov_base
= &tag_keepalive
;
573 con
->out_kvec
[0].iov_len
= 1;
574 con
->out_kvec_left
= 1;
575 con
->out_kvec_bytes
= 1;
576 con
->out_kvec_cur
= con
->out_kvec
;
577 set_bit(WRITE_PENDING
, &con
->state
);
581 * Connection negotiation.
584 static void prepare_connect_authorizer(struct ceph_connection
*con
)
588 int auth_protocol
= 0;
590 mutex_unlock(&con
->mutex
);
591 if (con
->ops
->get_authorizer
)
592 con
->ops
->get_authorizer(con
, &auth_buf
, &auth_len
,
593 &auth_protocol
, &con
->auth_reply_buf
,
594 &con
->auth_reply_buf_len
,
596 mutex_lock(&con
->mutex
);
598 con
->out_connect
.authorizer_protocol
= cpu_to_le32(auth_protocol
);
599 con
->out_connect
.authorizer_len
= cpu_to_le32(auth_len
);
601 con
->out_kvec
[con
->out_kvec_left
].iov_base
= auth_buf
;
602 con
->out_kvec
[con
->out_kvec_left
].iov_len
= auth_len
;
603 con
->out_kvec_left
++;
604 con
->out_kvec_bytes
+= auth_len
;
608 * We connected to a peer and are saying hello.
610 static void prepare_write_banner(struct ceph_messenger
*msgr
,
611 struct ceph_connection
*con
)
613 int len
= strlen(CEPH_BANNER
);
615 con
->out_kvec
[0].iov_base
= CEPH_BANNER
;
616 con
->out_kvec
[0].iov_len
= len
;
617 con
->out_kvec
[1].iov_base
= &msgr
->my_enc_addr
;
618 con
->out_kvec
[1].iov_len
= sizeof(msgr
->my_enc_addr
);
619 con
->out_kvec_left
= 2;
620 con
->out_kvec_bytes
= len
+ sizeof(msgr
->my_enc_addr
);
621 con
->out_kvec_cur
= con
->out_kvec
;
623 set_bit(WRITE_PENDING
, &con
->state
);
626 static void prepare_write_connect(struct ceph_messenger
*msgr
,
627 struct ceph_connection
*con
,
630 unsigned global_seq
= get_global_seq(con
->msgr
, 0);
633 switch (con
->peer_name
.type
) {
634 case CEPH_ENTITY_TYPE_MON
:
635 proto
= CEPH_MONC_PROTOCOL
;
637 case CEPH_ENTITY_TYPE_OSD
:
638 proto
= CEPH_OSDC_PROTOCOL
;
640 case CEPH_ENTITY_TYPE_MDS
:
641 proto
= CEPH_MDSC_PROTOCOL
;
647 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con
,
648 con
->connect_seq
, global_seq
, proto
);
650 con
->out_connect
.features
= cpu_to_le64(CEPH_FEATURE_SUPPORTED
);
651 con
->out_connect
.host_type
= cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT
);
652 con
->out_connect
.connect_seq
= cpu_to_le32(con
->connect_seq
);
653 con
->out_connect
.global_seq
= cpu_to_le32(global_seq
);
654 con
->out_connect
.protocol_version
= cpu_to_le32(proto
);
655 con
->out_connect
.flags
= 0;
658 con
->out_kvec_left
= 0;
659 con
->out_kvec_bytes
= 0;
661 con
->out_kvec
[con
->out_kvec_left
].iov_base
= &con
->out_connect
;
662 con
->out_kvec
[con
->out_kvec_left
].iov_len
= sizeof(con
->out_connect
);
663 con
->out_kvec_left
++;
664 con
->out_kvec_bytes
+= sizeof(con
->out_connect
);
665 con
->out_kvec_cur
= con
->out_kvec
;
667 set_bit(WRITE_PENDING
, &con
->state
);
669 prepare_connect_authorizer(con
);
674 * write as much of pending kvecs to the socket as we can.
676 * 0 -> socket full, but more to do
679 static int write_partial_kvec(struct ceph_connection
*con
)
683 dout("write_partial_kvec %p %d left\n", con
, con
->out_kvec_bytes
);
684 while (con
->out_kvec_bytes
> 0) {
685 ret
= ceph_tcp_sendmsg(con
->sock
, con
->out_kvec_cur
,
686 con
->out_kvec_left
, con
->out_kvec_bytes
,
690 con
->out_kvec_bytes
-= ret
;
691 if (con
->out_kvec_bytes
== 0)
694 if (ret
>= con
->out_kvec_cur
->iov_len
) {
695 ret
-= con
->out_kvec_cur
->iov_len
;
697 con
->out_kvec_left
--;
699 con
->out_kvec_cur
->iov_len
-= ret
;
700 con
->out_kvec_cur
->iov_base
+= ret
;
706 con
->out_kvec_left
= 0;
707 con
->out_kvec_is_msg
= false;
710 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con
,
711 con
->out_kvec_bytes
, con
->out_kvec_left
, ret
);
712 return ret
; /* done! */
716 * Write as much message data payload as we can. If we finish, queue
718 * 1 -> done, footer is now queued in out_kvec[].
719 * 0 -> socket full, but more to do
722 static int write_partial_msg_pages(struct ceph_connection
*con
)
724 struct ceph_msg
*msg
= con
->out_msg
;
725 unsigned data_len
= le32_to_cpu(msg
->hdr
.data_len
);
727 int crc
= con
->msgr
->nocrc
;
730 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
731 con
, con
->out_msg
, con
->out_msg_pos
.page
, con
->out_msg
->nr_pages
,
732 con
->out_msg_pos
.page_pos
);
734 while (con
->out_msg_pos
.page
< con
->out_msg
->nr_pages
) {
735 struct page
*page
= NULL
;
739 * if we are calculating the data crc (the default), we need
740 * to map the page. if our pages[] has been revoked, use the
744 page
= msg
->pages
[con
->out_msg_pos
.page
];
747 } else if (msg
->pagelist
) {
748 page
= list_first_entry(&msg
->pagelist
->head
,
753 page
= con
->msgr
->zero_page
;
755 kaddr
= page_address(con
->msgr
->zero_page
);
757 len
= min((int)(PAGE_SIZE
- con
->out_msg_pos
.page_pos
),
758 (int)(data_len
- con
->out_msg_pos
.data_pos
));
759 if (crc
&& !con
->out_msg_pos
.did_page_crc
) {
760 void *base
= kaddr
+ con
->out_msg_pos
.page_pos
;
761 u32 tmpcrc
= le32_to_cpu(con
->out_msg
->footer
.data_crc
);
763 BUG_ON(kaddr
== NULL
);
764 con
->out_msg
->footer
.data_crc
=
765 cpu_to_le32(crc32c(tmpcrc
, base
, len
));
766 con
->out_msg_pos
.did_page_crc
= 1;
769 ret
= kernel_sendpage(con
->sock
, page
,
770 con
->out_msg_pos
.page_pos
, len
,
771 MSG_DONTWAIT
| MSG_NOSIGNAL
|
774 if (crc
&& (msg
->pages
|| msg
->pagelist
))
780 con
->out_msg_pos
.data_pos
+= ret
;
781 con
->out_msg_pos
.page_pos
+= ret
;
783 con
->out_msg_pos
.page_pos
= 0;
784 con
->out_msg_pos
.page
++;
785 con
->out_msg_pos
.did_page_crc
= 0;
787 list_move_tail(&page
->lru
,
788 &msg
->pagelist
->head
);
792 dout("write_partial_msg_pages %p msg %p done\n", con
, msg
);
794 /* prepare and queue up footer, too */
796 con
->out_msg
->footer
.flags
|= CEPH_MSG_FOOTER_NOCRC
;
797 con
->out_kvec_bytes
= 0;
798 con
->out_kvec_left
= 0;
799 con
->out_kvec_cur
= con
->out_kvec
;
800 prepare_write_message_footer(con
, 0);
809 static int write_partial_skip(struct ceph_connection
*con
)
813 while (con
->out_skip
> 0) {
815 .iov_base
= page_address(con
->msgr
->zero_page
),
816 .iov_len
= min(con
->out_skip
, (int)PAGE_CACHE_SIZE
)
819 ret
= ceph_tcp_sendmsg(con
->sock
, &iov
, 1, iov
.iov_len
, 1);
822 con
->out_skip
-= ret
;
830 * Prepare to read connection handshake, or an ack.
832 static void prepare_read_banner(struct ceph_connection
*con
)
834 dout("prepare_read_banner %p\n", con
);
835 con
->in_base_pos
= 0;
838 static void prepare_read_connect(struct ceph_connection
*con
)
840 dout("prepare_read_connect %p\n", con
);
841 con
->in_base_pos
= 0;
844 static void prepare_read_ack(struct ceph_connection
*con
)
846 dout("prepare_read_ack %p\n", con
);
847 con
->in_base_pos
= 0;
850 static void prepare_read_tag(struct ceph_connection
*con
)
852 dout("prepare_read_tag %p\n", con
);
853 con
->in_base_pos
= 0;
854 con
->in_tag
= CEPH_MSGR_TAG_READY
;
858 * Prepare to read a message.
860 static int prepare_read_message(struct ceph_connection
*con
)
862 dout("prepare_read_message %p\n", con
);
863 BUG_ON(con
->in_msg
!= NULL
);
864 con
->in_base_pos
= 0;
865 con
->in_front_crc
= con
->in_middle_crc
= con
->in_data_crc
= 0;
870 static int read_partial(struct ceph_connection
*con
,
871 int *to
, int size
, void *object
)
874 while (con
->in_base_pos
< *to
) {
875 int left
= *to
- con
->in_base_pos
;
876 int have
= size
- left
;
877 int ret
= ceph_tcp_recvmsg(con
->sock
, object
+ have
, left
);
880 con
->in_base_pos
+= ret
;
887 * Read all or part of the connect-side handshake on a new connection
889 static int read_partial_banner(struct ceph_connection
*con
)
893 dout("read_partial_banner %p at %d\n", con
, con
->in_base_pos
);
896 ret
= read_partial(con
, &to
, strlen(CEPH_BANNER
), con
->in_banner
);
899 ret
= read_partial(con
, &to
, sizeof(con
->actual_peer_addr
),
900 &con
->actual_peer_addr
);
903 ret
= read_partial(con
, &to
, sizeof(con
->peer_addr_for_me
),
904 &con
->peer_addr_for_me
);
911 static int read_partial_connect(struct ceph_connection
*con
)
915 dout("read_partial_connect %p at %d\n", con
, con
->in_base_pos
);
917 ret
= read_partial(con
, &to
, sizeof(con
->in_reply
), &con
->in_reply
);
920 ret
= read_partial(con
, &to
, le32_to_cpu(con
->in_reply
.authorizer_len
),
921 con
->auth_reply_buf
);
925 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
926 con
, (int)con
->in_reply
.tag
,
927 le32_to_cpu(con
->in_reply
.connect_seq
),
928 le32_to_cpu(con
->in_reply
.global_seq
));
935 * Verify the hello banner looks okay.
937 static int verify_hello(struct ceph_connection
*con
)
939 if (memcmp(con
->in_banner
, CEPH_BANNER
, strlen(CEPH_BANNER
))) {
940 pr_err("connect to %s got bad banner\n",
941 pr_addr(&con
->peer_addr
.in_addr
));
942 con
->error_msg
= "protocol error, bad banner";
948 static bool addr_is_blank(struct sockaddr_storage
*ss
)
950 switch (ss
->ss_family
) {
952 return ((struct sockaddr_in
*)ss
)->sin_addr
.s_addr
== 0;
955 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[0] == 0 &&
956 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[1] == 0 &&
957 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[2] == 0 &&
958 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[3] == 0;
963 static int addr_port(struct sockaddr_storage
*ss
)
965 switch (ss
->ss_family
) {
967 return ntohs(((struct sockaddr_in
*)ss
)->sin_port
);
969 return ntohs(((struct sockaddr_in6
*)ss
)->sin6_port
);
974 static void addr_set_port(struct sockaddr_storage
*ss
, int p
)
976 switch (ss
->ss_family
) {
978 ((struct sockaddr_in
*)ss
)->sin_port
= htons(p
);
980 ((struct sockaddr_in6
*)ss
)->sin6_port
= htons(p
);
985 * Parse an ip[:port] list into an addr array. Use the default
986 * monitor port if a port isn't specified.
988 int ceph_parse_ips(const char *c
, const char *end
,
989 struct ceph_entity_addr
*addr
,
990 int max_count
, int *count
)
995 dout("parse_ips on '%.*s'\n", (int)(end
-c
), c
);
996 for (i
= 0; i
< max_count
; i
++) {
998 struct sockaddr_storage
*ss
= &addr
[i
].in_addr
;
999 struct sockaddr_in
*in4
= (void *)ss
;
1000 struct sockaddr_in6
*in6
= (void *)ss
;
1009 memset(ss
, 0, sizeof(*ss
));
1010 if (in4_pton(p
, end
- p
, (u8
*)&in4
->sin_addr
.s_addr
,
1012 ss
->ss_family
= AF_INET
;
1013 else if (in6_pton(p
, end
- p
, (u8
*)&in6
->sin6_addr
.s6_addr
,
1015 ss
->ss_family
= AF_INET6
;
1022 dout("missing matching ']'\n");
1029 if (p
< end
&& *p
== ':') {
1032 while (p
< end
&& *p
>= '0' && *p
<= '9') {
1033 port
= (port
* 10) + (*p
- '0');
1036 if (port
> 65535 || port
== 0)
1039 port
= CEPH_MON_PORT
;
1042 addr_set_port(ss
, port
);
1044 dout("parse_ips got %s\n", pr_addr(ss
));
1061 pr_err("parse_ips bad ip '%.*s'\n", (int)(end
- c
), c
);
1065 static int process_banner(struct ceph_connection
*con
)
1067 dout("process_banner on %p\n", con
);
1069 if (verify_hello(con
) < 0)
1072 ceph_decode_addr(&con
->actual_peer_addr
);
1073 ceph_decode_addr(&con
->peer_addr_for_me
);
1076 * Make sure the other end is who we wanted. note that the other
1077 * end may not yet know their ip address, so if it's 0.0.0.0, give
1078 * them the benefit of the doubt.
1080 if (memcmp(&con
->peer_addr
, &con
->actual_peer_addr
,
1081 sizeof(con
->peer_addr
)) != 0 &&
1082 !(addr_is_blank(&con
->actual_peer_addr
.in_addr
) &&
1083 con
->actual_peer_addr
.nonce
== con
->peer_addr
.nonce
)) {
1084 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
1085 pr_addr(&con
->peer_addr
.in_addr
),
1086 (int)le32_to_cpu(con
->peer_addr
.nonce
),
1087 pr_addr(&con
->actual_peer_addr
.in_addr
),
1088 (int)le32_to_cpu(con
->actual_peer_addr
.nonce
));
1089 con
->error_msg
= "wrong peer at address";
1094 * did we learn our address?
1096 if (addr_is_blank(&con
->msgr
->inst
.addr
.in_addr
)) {
1097 int port
= addr_port(&con
->msgr
->inst
.addr
.in_addr
);
1099 memcpy(&con
->msgr
->inst
.addr
.in_addr
,
1100 &con
->peer_addr_for_me
.in_addr
,
1101 sizeof(con
->peer_addr_for_me
.in_addr
));
1102 addr_set_port(&con
->msgr
->inst
.addr
.in_addr
, port
);
1103 encode_my_addr(con
->msgr
);
1104 dout("process_banner learned my addr is %s\n",
1105 pr_addr(&con
->msgr
->inst
.addr
.in_addr
));
1108 set_bit(NEGOTIATING
, &con
->state
);
1109 prepare_read_connect(con
);
1113 static void fail_protocol(struct ceph_connection
*con
)
1115 reset_connection(con
);
1116 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
1118 mutex_unlock(&con
->mutex
);
1119 if (con
->ops
->bad_proto
)
1120 con
->ops
->bad_proto(con
);
1121 mutex_lock(&con
->mutex
);
1124 static int process_connect(struct ceph_connection
*con
)
1126 u64 sup_feat
= CEPH_FEATURE_SUPPORTED
;
1127 u64 req_feat
= CEPH_FEATURE_REQUIRED
;
1128 u64 server_feat
= le64_to_cpu(con
->in_reply
.features
);
1130 dout("process_connect on %p tag %d\n", con
, (int)con
->in_tag
);
1132 switch (con
->in_reply
.tag
) {
1133 case CEPH_MSGR_TAG_FEATURES
:
1134 pr_err("%s%lld %s feature set mismatch,"
1135 " my %llx < server's %llx, missing %llx\n",
1136 ENTITY_NAME(con
->peer_name
),
1137 pr_addr(&con
->peer_addr
.in_addr
),
1138 sup_feat
, server_feat
, server_feat
& ~sup_feat
);
1139 con
->error_msg
= "missing required protocol features";
1143 case CEPH_MSGR_TAG_BADPROTOVER
:
1144 pr_err("%s%lld %s protocol version mismatch,"
1145 " my %d != server's %d\n",
1146 ENTITY_NAME(con
->peer_name
),
1147 pr_addr(&con
->peer_addr
.in_addr
),
1148 le32_to_cpu(con
->out_connect
.protocol_version
),
1149 le32_to_cpu(con
->in_reply
.protocol_version
));
1150 con
->error_msg
= "protocol version mismatch";
1154 case CEPH_MSGR_TAG_BADAUTHORIZER
:
1156 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con
,
1158 if (con
->auth_retry
== 2) {
1159 con
->error_msg
= "connect authorization failure";
1160 reset_connection(con
);
1161 set_bit(CLOSED
, &con
->state
);
1164 con
->auth_retry
= 1;
1165 prepare_write_connect(con
->msgr
, con
, 0);
1166 prepare_read_connect(con
);
1169 case CEPH_MSGR_TAG_RESETSESSION
:
1171 * If we connected with a large connect_seq but the peer
1172 * has no record of a session with us (no connection, or
1173 * connect_seq == 0), they will send RESETSESION to indicate
1174 * that they must have reset their session, and may have
1177 dout("process_connect got RESET peer seq %u\n",
1178 le32_to_cpu(con
->in_connect
.connect_seq
));
1179 pr_err("%s%lld %s connection reset\n",
1180 ENTITY_NAME(con
->peer_name
),
1181 pr_addr(&con
->peer_addr
.in_addr
));
1182 reset_connection(con
);
1183 prepare_write_connect(con
->msgr
, con
, 0);
1184 prepare_read_connect(con
);
1186 /* Tell ceph about it. */
1187 mutex_unlock(&con
->mutex
);
1188 pr_info("reset on %s%lld\n", ENTITY_NAME(con
->peer_name
));
1189 if (con
->ops
->peer_reset
)
1190 con
->ops
->peer_reset(con
);
1191 mutex_lock(&con
->mutex
);
1194 case CEPH_MSGR_TAG_RETRY_SESSION
:
1196 * If we sent a smaller connect_seq than the peer has, try
1197 * again with a larger value.
1199 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1200 le32_to_cpu(con
->out_connect
.connect_seq
),
1201 le32_to_cpu(con
->in_connect
.connect_seq
));
1202 con
->connect_seq
= le32_to_cpu(con
->in_connect
.connect_seq
);
1203 prepare_write_connect(con
->msgr
, con
, 0);
1204 prepare_read_connect(con
);
1207 case CEPH_MSGR_TAG_RETRY_GLOBAL
:
1209 * If we sent a smaller global_seq than the peer has, try
1210 * again with a larger value.
1212 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
1213 con
->peer_global_seq
,
1214 le32_to_cpu(con
->in_connect
.global_seq
));
1215 get_global_seq(con
->msgr
,
1216 le32_to_cpu(con
->in_connect
.global_seq
));
1217 prepare_write_connect(con
->msgr
, con
, 0);
1218 prepare_read_connect(con
);
1221 case CEPH_MSGR_TAG_READY
:
1222 if (req_feat
& ~server_feat
) {
1223 pr_err("%s%lld %s protocol feature mismatch,"
1224 " my required %llx > server's %llx, need %llx\n",
1225 ENTITY_NAME(con
->peer_name
),
1226 pr_addr(&con
->peer_addr
.in_addr
),
1227 req_feat
, server_feat
, req_feat
& ~server_feat
);
1228 con
->error_msg
= "missing required protocol features";
1232 clear_bit(CONNECTING
, &con
->state
);
1233 con
->peer_global_seq
= le32_to_cpu(con
->in_reply
.global_seq
);
1235 con
->peer_features
= server_feat
;
1236 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1237 con
->peer_global_seq
,
1238 le32_to_cpu(con
->in_reply
.connect_seq
),
1240 WARN_ON(con
->connect_seq
!=
1241 le32_to_cpu(con
->in_reply
.connect_seq
));
1243 if (con
->in_reply
.flags
& CEPH_MSG_CONNECT_LOSSY
)
1244 set_bit(LOSSYTX
, &con
->state
);
1246 prepare_read_tag(con
);
1249 case CEPH_MSGR_TAG_WAIT
:
1251 * If there is a connection race (we are opening
1252 * connections to each other), one of us may just have
1253 * to WAIT. This shouldn't happen if we are the
1256 pr_err("process_connect peer connecting WAIT\n");
1259 pr_err("connect protocol error, will retry\n");
1260 con
->error_msg
= "protocol error, garbage tag during connect";
1268 * read (part of) an ack
1270 static int read_partial_ack(struct ceph_connection
*con
)
1274 return read_partial(con
, &to
, sizeof(con
->in_temp_ack
),
1280 * We can finally discard anything that's been acked.
1282 static void process_ack(struct ceph_connection
*con
)
1285 u64 ack
= le64_to_cpu(con
->in_temp_ack
);
1288 while (!list_empty(&con
->out_sent
)) {
1289 m
= list_first_entry(&con
->out_sent
, struct ceph_msg
,
1291 seq
= le64_to_cpu(m
->hdr
.seq
);
1294 dout("got ack for seq %llu type %d at %p\n", seq
,
1295 le16_to_cpu(m
->hdr
.type
), m
);
1298 prepare_read_tag(con
);
1304 static int read_partial_message_section(struct ceph_connection
*con
,
1305 struct kvec
*section
,
1306 unsigned int sec_len
, u32
*crc
)
1313 while (section
->iov_len
< sec_len
) {
1314 BUG_ON(section
->iov_base
== NULL
);
1315 left
= sec_len
- section
->iov_len
;
1316 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)section
->iov_base
+
1317 section
->iov_len
, left
);
1320 section
->iov_len
+= ret
;
1321 if (section
->iov_len
== sec_len
)
1322 *crc
= crc32c(0, section
->iov_base
,
1329 static struct ceph_msg
*ceph_alloc_msg(struct ceph_connection
*con
,
1330 struct ceph_msg_header
*hdr
,
1333 * read (part of) a message.
1335 static int read_partial_message(struct ceph_connection
*con
)
1337 struct ceph_msg
*m
= con
->in_msg
;
1341 unsigned front_len
, middle_len
, data_len
, data_off
;
1342 int datacrc
= con
->msgr
->nocrc
;
1346 dout("read_partial_message con %p msg %p\n", con
, m
);
1349 while (con
->in_base_pos
< sizeof(con
->in_hdr
)) {
1350 left
= sizeof(con
->in_hdr
) - con
->in_base_pos
;
1351 ret
= ceph_tcp_recvmsg(con
->sock
,
1352 (char *)&con
->in_hdr
+ con
->in_base_pos
,
1356 con
->in_base_pos
+= ret
;
1357 if (con
->in_base_pos
== sizeof(con
->in_hdr
)) {
1358 u32 crc
= crc32c(0, (void *)&con
->in_hdr
,
1359 sizeof(con
->in_hdr
) - sizeof(con
->in_hdr
.crc
));
1360 if (crc
!= le32_to_cpu(con
->in_hdr
.crc
)) {
1361 pr_err("read_partial_message bad hdr "
1362 " crc %u != expected %u\n",
1363 crc
, con
->in_hdr
.crc
);
1368 front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
1369 if (front_len
> CEPH_MSG_MAX_FRONT_LEN
)
1371 middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
1372 if (middle_len
> CEPH_MSG_MAX_DATA_LEN
)
1374 data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
1375 if (data_len
> CEPH_MSG_MAX_DATA_LEN
)
1377 data_off
= le16_to_cpu(con
->in_hdr
.data_off
);
1380 seq
= le64_to_cpu(con
->in_hdr
.seq
);
1381 if ((s64
)seq
- (s64
)con
->in_seq
< 1) {
1382 pr_info("skipping %s%lld %s seq %lld, expected %lld\n",
1383 ENTITY_NAME(con
->peer_name
),
1384 pr_addr(&con
->peer_addr
.in_addr
),
1385 seq
, con
->in_seq
+ 1);
1386 con
->in_base_pos
= -front_len
- middle_len
- data_len
-
1388 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1391 } else if ((s64
)seq
- (s64
)con
->in_seq
> 1) {
1392 pr_err("read_partial_message bad seq %lld expected %lld\n",
1393 seq
, con
->in_seq
+ 1);
1394 con
->error_msg
= "bad message sequence # for incoming message";
1398 /* allocate message? */
1400 dout("got hdr type %d front %d data %d\n", con
->in_hdr
.type
,
1401 con
->in_hdr
.front_len
, con
->in_hdr
.data_len
);
1403 con
->in_msg
= ceph_alloc_msg(con
, &con
->in_hdr
, &skip
);
1405 /* skip this message */
1406 dout("alloc_msg said skip message\n");
1407 BUG_ON(con
->in_msg
);
1408 con
->in_base_pos
= -front_len
- middle_len
- data_len
-
1410 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1416 "error allocating memory for incoming message";
1420 m
->front
.iov_len
= 0; /* haven't read it yet */
1422 m
->middle
->vec
.iov_len
= 0;
1424 con
->in_msg_pos
.page
= 0;
1425 con
->in_msg_pos
.page_pos
= data_off
& ~PAGE_MASK
;
1426 con
->in_msg_pos
.data_pos
= 0;
1430 ret
= read_partial_message_section(con
, &m
->front
, front_len
,
1431 &con
->in_front_crc
);
1437 ret
= read_partial_message_section(con
, &m
->middle
->vec
,
1439 &con
->in_middle_crc
);
1445 while (con
->in_msg_pos
.data_pos
< data_len
) {
1446 left
= min((int)(data_len
- con
->in_msg_pos
.data_pos
),
1447 (int)(PAGE_SIZE
- con
->in_msg_pos
.page_pos
));
1448 BUG_ON(m
->pages
== NULL
);
1449 p
= kmap(m
->pages
[con
->in_msg_pos
.page
]);
1450 ret
= ceph_tcp_recvmsg(con
->sock
, p
+ con
->in_msg_pos
.page_pos
,
1452 if (ret
> 0 && datacrc
)
1454 crc32c(con
->in_data_crc
,
1455 p
+ con
->in_msg_pos
.page_pos
, ret
);
1456 kunmap(m
->pages
[con
->in_msg_pos
.page
]);
1459 con
->in_msg_pos
.data_pos
+= ret
;
1460 con
->in_msg_pos
.page_pos
+= ret
;
1461 if (con
->in_msg_pos
.page_pos
== PAGE_SIZE
) {
1462 con
->in_msg_pos
.page_pos
= 0;
1463 con
->in_msg_pos
.page
++;
1468 to
= sizeof(m
->hdr
) + sizeof(m
->footer
);
1469 while (con
->in_base_pos
< to
) {
1470 left
= to
- con
->in_base_pos
;
1471 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)&m
->footer
+
1472 (con
->in_base_pos
- sizeof(m
->hdr
)),
1476 con
->in_base_pos
+= ret
;
1478 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1479 m
, front_len
, m
->footer
.front_crc
, middle_len
,
1480 m
->footer
.middle_crc
, data_len
, m
->footer
.data_crc
);
1483 if (con
->in_front_crc
!= le32_to_cpu(m
->footer
.front_crc
)) {
1484 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1485 m
, con
->in_front_crc
, m
->footer
.front_crc
);
1488 if (con
->in_middle_crc
!= le32_to_cpu(m
->footer
.middle_crc
)) {
1489 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1490 m
, con
->in_middle_crc
, m
->footer
.middle_crc
);
1494 (m
->footer
.flags
& CEPH_MSG_FOOTER_NOCRC
) == 0 &&
1495 con
->in_data_crc
!= le32_to_cpu(m
->footer
.data_crc
)) {
1496 pr_err("read_partial_message %p data crc %u != exp. %u\n", m
,
1497 con
->in_data_crc
, le32_to_cpu(m
->footer
.data_crc
));
1501 return 1; /* done! */
1505 * Process message. This happens in the worker thread. The callback should
1506 * be careful not to do anything that waits on other incoming messages or it
1509 static void process_message(struct ceph_connection
*con
)
1511 struct ceph_msg
*msg
;
1516 /* if first message, set peer_name */
1517 if (con
->peer_name
.type
== 0)
1518 con
->peer_name
= msg
->hdr
.src
;
1521 mutex_unlock(&con
->mutex
);
1523 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1524 msg
, le64_to_cpu(msg
->hdr
.seq
),
1525 ENTITY_NAME(msg
->hdr
.src
),
1526 le16_to_cpu(msg
->hdr
.type
),
1527 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
1528 le32_to_cpu(msg
->hdr
.front_len
),
1529 le32_to_cpu(msg
->hdr
.data_len
),
1530 con
->in_front_crc
, con
->in_middle_crc
, con
->in_data_crc
);
1531 con
->ops
->dispatch(con
, msg
);
1533 mutex_lock(&con
->mutex
);
1534 prepare_read_tag(con
);
1539 * Write something to the socket. Called in a worker thread when the
1540 * socket appears to be writeable and we have something ready to send.
1542 static int try_write(struct ceph_connection
*con
)
1544 struct ceph_messenger
*msgr
= con
->msgr
;
1547 dout("try_write start %p state %lu nref %d\n", con
, con
->state
,
1548 atomic_read(&con
->nref
));
1551 dout("try_write out_kvec_bytes %d\n", con
->out_kvec_bytes
);
1553 /* open the socket first? */
1554 if (con
->sock
== NULL
) {
1556 * if we were STANDBY and are reconnecting _this_
1557 * connection, bump connect_seq now. Always bump
1560 if (test_and_clear_bit(STANDBY
, &con
->state
))
1563 prepare_write_banner(msgr
, con
);
1564 prepare_write_connect(msgr
, con
, 1);
1565 prepare_read_banner(con
);
1566 set_bit(CONNECTING
, &con
->state
);
1567 clear_bit(NEGOTIATING
, &con
->state
);
1569 BUG_ON(con
->in_msg
);
1570 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1571 dout("try_write initiating connect on %p new state %lu\n",
1573 con
->sock
= ceph_tcp_connect(con
);
1574 if (IS_ERR(con
->sock
)) {
1576 con
->error_msg
= "connect error";
1583 /* kvec data queued? */
1584 if (con
->out_skip
) {
1585 ret
= write_partial_skip(con
);
1589 dout("try_write write_partial_skip err %d\n", ret
);
1593 if (con
->out_kvec_left
) {
1594 ret
= write_partial_kvec(con
);
1601 if (con
->out_msg_done
) {
1602 ceph_msg_put(con
->out_msg
);
1603 con
->out_msg
= NULL
; /* we're done with this one */
1607 ret
= write_partial_msg_pages(con
);
1609 goto more_kvec
; /* we need to send the footer, too! */
1613 dout("try_write write_partial_msg_pages err %d\n",
1620 if (!test_bit(CONNECTING
, &con
->state
)) {
1621 /* is anything else pending? */
1622 if (!list_empty(&con
->out_queue
)) {
1623 prepare_write_message(con
);
1626 if (con
->in_seq
> con
->in_seq_acked
) {
1627 prepare_write_ack(con
);
1630 if (test_and_clear_bit(KEEPALIVE_PENDING
, &con
->state
)) {
1631 prepare_write_keepalive(con
);
1636 /* Nothing to do! */
1637 clear_bit(WRITE_PENDING
, &con
->state
);
1638 dout("try_write nothing else to write.\n");
1642 dout("try_write done on %p\n", con
);
1649 * Read what we can from the socket.
1651 static int try_read(struct ceph_connection
*con
)
1658 if (test_bit(STANDBY
, &con
->state
))
1661 dout("try_read start on %p\n", con
);
1664 dout("try_read tag %d in_base_pos %d\n", (int)con
->in_tag
,
1666 if (test_bit(CONNECTING
, &con
->state
)) {
1667 if (!test_bit(NEGOTIATING
, &con
->state
)) {
1668 dout("try_read connecting\n");
1669 ret
= read_partial_banner(con
);
1672 if (process_banner(con
) < 0) {
1677 ret
= read_partial_connect(con
);
1680 if (process_connect(con
) < 0) {
1687 if (con
->in_base_pos
< 0) {
1689 * skipping + discarding content.
1691 * FIXME: there must be a better way to do this!
1693 static char buf
[1024];
1694 int skip
= min(1024, -con
->in_base_pos
);
1695 dout("skipping %d / %d bytes\n", skip
, -con
->in_base_pos
);
1696 ret
= ceph_tcp_recvmsg(con
->sock
, buf
, skip
);
1699 con
->in_base_pos
+= ret
;
1700 if (con
->in_base_pos
)
1703 if (con
->in_tag
== CEPH_MSGR_TAG_READY
) {
1707 ret
= ceph_tcp_recvmsg(con
->sock
, &con
->in_tag
, 1);
1710 dout("try_read got tag %d\n", (int)con
->in_tag
);
1711 switch (con
->in_tag
) {
1712 case CEPH_MSGR_TAG_MSG
:
1713 prepare_read_message(con
);
1715 case CEPH_MSGR_TAG_ACK
:
1716 prepare_read_ack(con
);
1718 case CEPH_MSGR_TAG_CLOSE
:
1719 set_bit(CLOSED
, &con
->state
); /* fixme */
1725 if (con
->in_tag
== CEPH_MSGR_TAG_MSG
) {
1726 ret
= read_partial_message(con
);
1730 con
->error_msg
= "bad crc";
1734 con
->error_msg
= "io error";
1740 if (con
->in_tag
== CEPH_MSGR_TAG_READY
)
1742 process_message(con
);
1745 if (con
->in_tag
== CEPH_MSGR_TAG_ACK
) {
1746 ret
= read_partial_ack(con
);
1756 dout("try_read done on %p\n", con
);
1760 pr_err("try_read bad con->in_tag = %d\n", (int)con
->in_tag
);
1761 con
->error_msg
= "protocol error, garbage tag";
1768 * Atomically queue work on a connection. Bump @con reference to
1769 * avoid races with connection teardown.
1771 * There is some trickery going on with QUEUED and BUSY because we
1772 * only want a _single_ thread operating on each connection at any
1773 * point in time, but we want to use all available CPUs.
1775 * The worker thread only proceeds if it can atomically set BUSY. It
1776 * clears QUEUED and does it's thing. When it thinks it's done, it
1777 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1778 * (tries again to set BUSY).
1780 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1781 * try to queue work. If that fails (work is already queued, or BUSY)
1782 * we give up (work also already being done or is queued) but leave QUEUED
1783 * set so that the worker thread will loop if necessary.
1785 static void queue_con(struct ceph_connection
*con
)
1787 if (test_bit(DEAD
, &con
->state
)) {
1788 dout("queue_con %p ignoring: DEAD\n",
1793 if (!con
->ops
->get(con
)) {
1794 dout("queue_con %p ref count 0\n", con
);
1798 set_bit(QUEUED
, &con
->state
);
1799 if (test_bit(BUSY
, &con
->state
)) {
1800 dout("queue_con %p - already BUSY\n", con
);
1802 } else if (!queue_work(ceph_msgr_wq
, &con
->work
.work
)) {
1803 dout("queue_con %p - already queued\n", con
);
1806 dout("queue_con %p\n", con
);
1811 * Do some work on a connection. Drop a connection ref when we're done.
1813 static void con_work(struct work_struct
*work
)
1815 struct ceph_connection
*con
= container_of(work
, struct ceph_connection
,
1820 if (test_and_set_bit(BUSY
, &con
->state
) != 0) {
1821 dout("con_work %p BUSY already set\n", con
);
1824 dout("con_work %p start, clearing QUEUED\n", con
);
1825 clear_bit(QUEUED
, &con
->state
);
1827 mutex_lock(&con
->mutex
);
1829 if (test_bit(CLOSED
, &con
->state
)) { /* e.g. if we are replaced */
1830 dout("con_work CLOSED\n");
1831 con_close_socket(con
);
1834 if (test_and_clear_bit(OPENING
, &con
->state
)) {
1835 /* reopen w/ new peer */
1836 dout("con_work OPENING\n");
1837 con_close_socket(con
);
1840 if (test_and_clear_bit(SOCK_CLOSED
, &con
->state
) ||
1841 try_read(con
) < 0 ||
1842 try_write(con
) < 0) {
1843 mutex_unlock(&con
->mutex
);
1845 ceph_fault(con
); /* error/fault path */
1850 mutex_unlock(&con
->mutex
);
1853 clear_bit(BUSY
, &con
->state
);
1854 dout("con->state=%lu\n", con
->state
);
1855 if (test_bit(QUEUED
, &con
->state
)) {
1856 if (!backoff
|| test_bit(OPENING
, &con
->state
)) {
1857 dout("con_work %p QUEUED reset, looping\n", con
);
1860 dout("con_work %p QUEUED reset, but just faulted\n", con
);
1861 clear_bit(QUEUED
, &con
->state
);
1863 dout("con_work %p done\n", con
);
1871 * Generic error/fault handler. A retry mechanism is used with
1872 * exponential backoff
1874 static void ceph_fault(struct ceph_connection
*con
)
1876 pr_err("%s%lld %s %s\n", ENTITY_NAME(con
->peer_name
),
1877 pr_addr(&con
->peer_addr
.in_addr
), con
->error_msg
);
1878 dout("fault %p state %lu to peer %s\n",
1879 con
, con
->state
, pr_addr(&con
->peer_addr
.in_addr
));
1881 if (test_bit(LOSSYTX
, &con
->state
)) {
1882 dout("fault on LOSSYTX channel\n");
1886 mutex_lock(&con
->mutex
);
1887 if (test_bit(CLOSED
, &con
->state
))
1890 con_close_socket(con
);
1893 ceph_msg_put(con
->in_msg
);
1897 /* Requeue anything that hasn't been acked */
1898 list_splice_init(&con
->out_sent
, &con
->out_queue
);
1900 /* If there are no messages in the queue, place the connection
1901 * in a STANDBY state (i.e., don't try to reconnect just yet). */
1902 if (list_empty(&con
->out_queue
) && !con
->out_keepalive_pending
) {
1903 dout("fault setting STANDBY\n");
1904 set_bit(STANDBY
, &con
->state
);
1906 /* retry after a delay. */
1907 if (con
->delay
== 0)
1908 con
->delay
= BASE_DELAY_INTERVAL
;
1909 else if (con
->delay
< MAX_DELAY_INTERVAL
)
1911 dout("fault queueing %p delay %lu\n", con
, con
->delay
);
1913 if (queue_delayed_work(ceph_msgr_wq
, &con
->work
,
1914 round_jiffies_relative(con
->delay
)) == 0)
1919 mutex_unlock(&con
->mutex
);
1922 * in case we faulted due to authentication, invalidate our
1923 * current tickets so that we can get new ones.
1925 if (con
->auth_retry
&& con
->ops
->invalidate_authorizer
) {
1926 dout("calling invalidate_authorizer()\n");
1927 con
->ops
->invalidate_authorizer(con
);
1930 if (con
->ops
->fault
)
1931 con
->ops
->fault(con
);
1937 * create a new messenger instance
1939 struct ceph_messenger
*ceph_messenger_create(struct ceph_entity_addr
*myaddr
)
1941 struct ceph_messenger
*msgr
;
1943 msgr
= kzalloc(sizeof(*msgr
), GFP_KERNEL
);
1945 return ERR_PTR(-ENOMEM
);
1947 spin_lock_init(&msgr
->global_seq_lock
);
1949 /* the zero page is needed if a request is "canceled" while the message
1950 * is being written over the socket */
1951 msgr
->zero_page
= __page_cache_alloc(GFP_KERNEL
| __GFP_ZERO
);
1952 if (!msgr
->zero_page
) {
1954 return ERR_PTR(-ENOMEM
);
1956 kmap(msgr
->zero_page
);
1959 msgr
->inst
.addr
= *myaddr
;
1961 /* select a random nonce */
1962 msgr
->inst
.addr
.type
= 0;
1963 get_random_bytes(&msgr
->inst
.addr
.nonce
, sizeof(msgr
->inst
.addr
.nonce
));
1964 encode_my_addr(msgr
);
1966 dout("messenger_create %p\n", msgr
);
1970 void ceph_messenger_destroy(struct ceph_messenger
*msgr
)
1972 dout("destroy %p\n", msgr
);
1973 kunmap(msgr
->zero_page
);
1974 __free_page(msgr
->zero_page
);
1976 dout("destroyed messenger %p\n", msgr
);
1980 * Queue up an outgoing message on the given connection.
1982 void ceph_con_send(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1984 if (test_bit(CLOSED
, &con
->state
)) {
1985 dout("con_send %p closed, dropping %p\n", con
, msg
);
1991 msg
->hdr
.src
= con
->msgr
->inst
.name
;
1993 BUG_ON(msg
->front
.iov_len
!= le32_to_cpu(msg
->hdr
.front_len
));
1995 msg
->needs_out_seq
= true;
1998 mutex_lock(&con
->mutex
);
1999 BUG_ON(!list_empty(&msg
->list_head
));
2000 list_add_tail(&msg
->list_head
, &con
->out_queue
);
2001 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg
,
2002 ENTITY_NAME(con
->peer_name
), le16_to_cpu(msg
->hdr
.type
),
2003 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
2004 le32_to_cpu(msg
->hdr
.front_len
),
2005 le32_to_cpu(msg
->hdr
.middle_len
),
2006 le32_to_cpu(msg
->hdr
.data_len
));
2007 mutex_unlock(&con
->mutex
);
2009 /* if there wasn't anything waiting to send before, queue
2011 if (test_and_set_bit(WRITE_PENDING
, &con
->state
) == 0)
2016 * Revoke a message that was previously queued for send
2018 void ceph_con_revoke(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2020 mutex_lock(&con
->mutex
);
2021 if (!list_empty(&msg
->list_head
)) {
2022 dout("con_revoke %p msg %p - was on queue\n", con
, msg
);
2023 list_del_init(&msg
->list_head
);
2027 if (con
->out_msg
== msg
) {
2028 dout("con_revoke %p msg %p - was sending\n", con
, msg
);
2029 con
->out_msg
= NULL
;
2030 if (con
->out_kvec_is_msg
) {
2031 con
->out_skip
= con
->out_kvec_bytes
;
2032 con
->out_kvec_is_msg
= false;
2037 mutex_unlock(&con
->mutex
);
2041 * Revoke a message that we may be reading data into
2043 void ceph_con_revoke_message(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2045 mutex_lock(&con
->mutex
);
2046 if (con
->in_msg
&& con
->in_msg
== msg
) {
2047 unsigned front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
2048 unsigned middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
2049 unsigned data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
2051 /* skip rest of message */
2052 dout("con_revoke_pages %p msg %p revoked\n", con
, msg
);
2053 con
->in_base_pos
= con
->in_base_pos
-
2054 sizeof(struct ceph_msg_header
) -
2058 sizeof(struct ceph_msg_footer
);
2059 ceph_msg_put(con
->in_msg
);
2061 con
->in_tag
= CEPH_MSGR_TAG_READY
;
2064 dout("con_revoke_pages %p msg %p pages %p no-op\n",
2065 con
, con
->in_msg
, msg
);
2067 mutex_unlock(&con
->mutex
);
2071 * Queue a keepalive byte to ensure the tcp connection is alive.
2073 void ceph_con_keepalive(struct ceph_connection
*con
)
2075 if (test_and_set_bit(KEEPALIVE_PENDING
, &con
->state
) == 0 &&
2076 test_and_set_bit(WRITE_PENDING
, &con
->state
) == 0)
2082 * construct a new message with given type, size
2083 * the new msg has a ref count of 1.
2085 struct ceph_msg
*ceph_msg_new(int type
, int front_len
, gfp_t flags
)
2089 m
= kmalloc(sizeof(*m
), flags
);
2092 kref_init(&m
->kref
);
2093 INIT_LIST_HEAD(&m
->list_head
);
2096 m
->hdr
.type
= cpu_to_le16(type
);
2097 m
->hdr
.priority
= cpu_to_le16(CEPH_MSG_PRIO_DEFAULT
);
2099 m
->hdr
.front_len
= cpu_to_le32(front_len
);
2100 m
->hdr
.middle_len
= 0;
2101 m
->hdr
.data_len
= 0;
2102 m
->hdr
.data_off
= 0;
2103 m
->hdr
.reserved
= 0;
2104 m
->footer
.front_crc
= 0;
2105 m
->footer
.middle_crc
= 0;
2106 m
->footer
.data_crc
= 0;
2107 m
->footer
.flags
= 0;
2108 m
->front_max
= front_len
;
2109 m
->front_is_vmalloc
= false;
2110 m
->more_to_follow
= false;
2115 if (front_len
> PAGE_CACHE_SIZE
) {
2116 m
->front
.iov_base
= __vmalloc(front_len
, flags
,
2118 m
->front_is_vmalloc
= true;
2120 m
->front
.iov_base
= kmalloc(front_len
, flags
);
2122 if (m
->front
.iov_base
== NULL
) {
2123 pr_err("msg_new can't allocate %d bytes\n",
2128 m
->front
.iov_base
= NULL
;
2130 m
->front
.iov_len
= front_len
;
2140 dout("ceph_msg_new %p front %d\n", m
, front_len
);
2146 pr_err("msg_new can't create type %d front %d\n", type
, front_len
);
2151 * Allocate "middle" portion of a message, if it is needed and wasn't
2152 * allocated by alloc_msg. This allows us to read a small fixed-size
2153 * per-type header in the front and then gracefully fail (i.e.,
2154 * propagate the error to the caller based on info in the front) when
2155 * the middle is too large.
2157 static int ceph_alloc_middle(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2159 int type
= le16_to_cpu(msg
->hdr
.type
);
2160 int middle_len
= le32_to_cpu(msg
->hdr
.middle_len
);
2162 dout("alloc_middle %p type %d %s middle_len %d\n", msg
, type
,
2163 ceph_msg_type_name(type
), middle_len
);
2164 BUG_ON(!middle_len
);
2165 BUG_ON(msg
->middle
);
2167 msg
->middle
= ceph_buffer_new(middle_len
, GFP_NOFS
);
2174 * Generic message allocator, for incoming messages.
2176 static struct ceph_msg
*ceph_alloc_msg(struct ceph_connection
*con
,
2177 struct ceph_msg_header
*hdr
,
2180 int type
= le16_to_cpu(hdr
->type
);
2181 int front_len
= le32_to_cpu(hdr
->front_len
);
2182 int middle_len
= le32_to_cpu(hdr
->middle_len
);
2183 struct ceph_msg
*msg
= NULL
;
2186 if (con
->ops
->alloc_msg
) {
2187 mutex_unlock(&con
->mutex
);
2188 msg
= con
->ops
->alloc_msg(con
, hdr
, skip
);
2189 mutex_lock(&con
->mutex
);
2195 msg
= ceph_msg_new(type
, front_len
, GFP_NOFS
);
2197 pr_err("unable to allocate msg type %d len %d\n",
2202 memcpy(&msg
->hdr
, &con
->in_hdr
, sizeof(con
->in_hdr
));
2204 if (middle_len
&& !msg
->middle
) {
2205 ret
= ceph_alloc_middle(con
, msg
);
2217 * Free a generically kmalloc'd message.
2219 void ceph_msg_kfree(struct ceph_msg
*m
)
2221 dout("msg_kfree %p\n", m
);
2222 if (m
->front_is_vmalloc
)
2223 vfree(m
->front
.iov_base
);
2225 kfree(m
->front
.iov_base
);
2230 * Drop a msg ref. Destroy as needed.
2232 void ceph_msg_last_put(struct kref
*kref
)
2234 struct ceph_msg
*m
= container_of(kref
, struct ceph_msg
, kref
);
2236 dout("ceph_msg_put last one on %p\n", m
);
2237 WARN_ON(!list_empty(&m
->list_head
));
2239 /* drop middle, data, if any */
2241 ceph_buffer_put(m
->middle
);
2248 ceph_pagelist_release(m
->pagelist
);
2254 ceph_msgpool_put(m
->pool
, m
);
2259 void ceph_msg_dump(struct ceph_msg
*msg
)
2261 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg
,
2262 msg
->front_max
, msg
->nr_pages
);
2263 print_hex_dump(KERN_DEBUG
, "header: ",
2264 DUMP_PREFIX_OFFSET
, 16, 1,
2265 &msg
->hdr
, sizeof(msg
->hdr
), true);
2266 print_hex_dump(KERN_DEBUG
, " front: ",
2267 DUMP_PREFIX_OFFSET
, 16, 1,
2268 msg
->front
.iov_base
, msg
->front
.iov_len
, true);
2270 print_hex_dump(KERN_DEBUG
, "middle: ",
2271 DUMP_PREFIX_OFFSET
, 16, 1,
2272 msg
->middle
->vec
.iov_base
,
2273 msg
->middle
->vec
.iov_len
, true);
2274 print_hex_dump(KERN_DEBUG
, "footer: ",
2275 DUMP_PREFIX_OFFSET
, 16, 1,
2276 &msg
->footer
, sizeof(msg
->footer
), true);