1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/crc32c.h>
4 #include <linux/ctype.h>
5 #include <linux/highmem.h>
6 #include <linux/inet.h>
7 #include <linux/kthread.h>
9 #include <linux/slab.h>
10 #include <linux/socket.h>
11 #include <linux/string.h>
12 #include <linux/bio.h>
13 #include <linux/blkdev.h>
14 #include <linux/dns_resolver.h>
17 #include <linux/ceph/libceph.h>
18 #include <linux/ceph/messenger.h>
19 #include <linux/ceph/decode.h>
20 #include <linux/ceph/pagelist.h>
21 #include <linux/export.h>
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
32 /* State values for ceph_connection->sock_state; NEW is assumed to be 0 */
34 #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
35 #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
36 #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
37 #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
38 #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
40 /* static tag bytes (protocol control messages) */
41 static char tag_msg
= CEPH_MSGR_TAG_MSG
;
42 static char tag_ack
= CEPH_MSGR_TAG_ACK
;
43 static char tag_keepalive
= CEPH_MSGR_TAG_KEEPALIVE
;
46 static struct lock_class_key socket_class
;
50 * When skipping (ignoring) a block of input we read it into a "skip
51 * buffer," which is this many bytes in size.
53 #define SKIP_BUF_SIZE 1024
55 static void queue_con(struct ceph_connection
*con
);
56 static void con_work(struct work_struct
*);
57 static void ceph_fault(struct ceph_connection
*con
);
60 * Nicely render a sockaddr as a string. An array of formatted
61 * strings is used, to approximate reentrancy.
63 #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
64 #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
65 #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
66 #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
68 static char addr_str
[ADDR_STR_COUNT
][MAX_ADDR_STR_LEN
];
69 static atomic_t addr_str_seq
= ATOMIC_INIT(0);
71 static struct page
*zero_page
; /* used in certain error cases */
73 const char *ceph_pr_addr(const struct sockaddr_storage
*ss
)
77 struct sockaddr_in
*in4
= (struct sockaddr_in
*) ss
;
78 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*) ss
;
80 i
= atomic_inc_return(&addr_str_seq
) & ADDR_STR_COUNT_MASK
;
83 switch (ss
->ss_family
) {
85 snprintf(s
, MAX_ADDR_STR_LEN
, "%pI4:%hu", &in4
->sin_addr
,
86 ntohs(in4
->sin_port
));
90 snprintf(s
, MAX_ADDR_STR_LEN
, "[%pI6c]:%hu", &in6
->sin6_addr
,
91 ntohs(in6
->sin6_port
));
95 snprintf(s
, MAX_ADDR_STR_LEN
, "(unknown sockaddr family %hu)",
101 EXPORT_SYMBOL(ceph_pr_addr
);
103 static void encode_my_addr(struct ceph_messenger
*msgr
)
105 memcpy(&msgr
->my_enc_addr
, &msgr
->inst
.addr
, sizeof(msgr
->my_enc_addr
));
106 ceph_encode_addr(&msgr
->my_enc_addr
);
110 * work queue for all reading and writing to/from the socket.
112 static struct workqueue_struct
*ceph_msgr_wq
;
114 void _ceph_msgr_exit(void)
117 destroy_workqueue(ceph_msgr_wq
);
121 BUG_ON(zero_page
== NULL
);
123 page_cache_release(zero_page
);
127 int ceph_msgr_init(void)
129 BUG_ON(zero_page
!= NULL
);
130 zero_page
= ZERO_PAGE(0);
131 page_cache_get(zero_page
);
133 ceph_msgr_wq
= alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT
, 0);
137 pr_err("msgr_init failed to create workqueue\n");
142 EXPORT_SYMBOL(ceph_msgr_init
);
144 void ceph_msgr_exit(void)
146 BUG_ON(ceph_msgr_wq
== NULL
);
150 EXPORT_SYMBOL(ceph_msgr_exit
);
152 void ceph_msgr_flush(void)
154 flush_workqueue(ceph_msgr_wq
);
156 EXPORT_SYMBOL(ceph_msgr_flush
);
158 /* Connection socket state transition functions */
160 static void con_sock_state_init(struct ceph_connection
*con
)
164 old_state
= atomic_xchg(&con
->sock_state
, CON_SOCK_STATE_CLOSED
);
165 if (WARN_ON(old_state
!= CON_SOCK_STATE_NEW
))
166 printk("%s: unexpected old state %d\n", __func__
, old_state
);
169 static void con_sock_state_connecting(struct ceph_connection
*con
)
173 old_state
= atomic_xchg(&con
->sock_state
, CON_SOCK_STATE_CONNECTING
);
174 if (WARN_ON(old_state
!= CON_SOCK_STATE_CLOSED
))
175 printk("%s: unexpected old state %d\n", __func__
, old_state
);
178 static void con_sock_state_connected(struct ceph_connection
*con
)
182 old_state
= atomic_xchg(&con
->sock_state
, CON_SOCK_STATE_CONNECTED
);
183 if (WARN_ON(old_state
!= CON_SOCK_STATE_CONNECTING
))
184 printk("%s: unexpected old state %d\n", __func__
, old_state
);
187 static void con_sock_state_closing(struct ceph_connection
*con
)
191 old_state
= atomic_xchg(&con
->sock_state
, CON_SOCK_STATE_CLOSING
);
192 if (WARN_ON(old_state
!= CON_SOCK_STATE_CONNECTING
&&
193 old_state
!= CON_SOCK_STATE_CONNECTED
&&
194 old_state
!= CON_SOCK_STATE_CLOSING
))
195 printk("%s: unexpected old state %d\n", __func__
, old_state
);
198 static void con_sock_state_closed(struct ceph_connection
*con
)
202 old_state
= atomic_xchg(&con
->sock_state
, CON_SOCK_STATE_CLOSED
);
203 if (WARN_ON(old_state
!= CON_SOCK_STATE_CONNECTED
&&
204 old_state
!= CON_SOCK_STATE_CLOSING
))
205 printk("%s: unexpected old state %d\n", __func__
, old_state
);
209 * socket callback functions
212 /* data available on socket, or listen socket received a connect */
213 static void ceph_sock_data_ready(struct sock
*sk
, int count_unused
)
215 struct ceph_connection
*con
= sk
->sk_user_data
;
217 if (sk
->sk_state
!= TCP_CLOSE_WAIT
) {
218 dout("%s on %p state = %lu, queueing work\n", __func__
,
224 /* socket has buffer space for writing */
225 static void ceph_sock_write_space(struct sock
*sk
)
227 struct ceph_connection
*con
= sk
->sk_user_data
;
229 /* only queue to workqueue if there is data we want to write,
230 * and there is sufficient space in the socket buffer to accept
231 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
232 * doesn't get called again until try_write() fills the socket
233 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
234 * and net/core/stream.c:sk_stream_write_space().
236 if (test_bit(WRITE_PENDING
, &con
->flags
)) {
237 if (sk_stream_wspace(sk
) >= sk_stream_min_wspace(sk
)) {
238 dout("%s %p queueing write work\n", __func__
, con
);
239 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
243 dout("%s %p nothing to write\n", __func__
, con
);
247 /* socket's state has changed */
248 static void ceph_sock_state_change(struct sock
*sk
)
250 struct ceph_connection
*con
= sk
->sk_user_data
;
252 dout("%s %p state = %lu sk_state = %u\n", __func__
,
253 con
, con
->state
, sk
->sk_state
);
255 if (test_bit(CLOSED
, &con
->state
))
258 switch (sk
->sk_state
) {
260 dout("%s TCP_CLOSE\n", __func__
);
262 dout("%s TCP_CLOSE_WAIT\n", __func__
);
263 con_sock_state_closing(con
);
264 if (test_and_set_bit(SOCK_CLOSED
, &con
->flags
) == 0) {
265 if (test_bit(CONNECTING
, &con
->state
))
266 con
->error_msg
= "connection failed";
268 con
->error_msg
= "socket closed";
272 case TCP_ESTABLISHED
:
273 dout("%s TCP_ESTABLISHED\n", __func__
);
274 con_sock_state_connected(con
);
277 default: /* Everything else is uninteresting */
283 * set up socket callbacks
285 static void set_sock_callbacks(struct socket
*sock
,
286 struct ceph_connection
*con
)
288 struct sock
*sk
= sock
->sk
;
289 sk
->sk_user_data
= con
;
290 sk
->sk_data_ready
= ceph_sock_data_ready
;
291 sk
->sk_write_space
= ceph_sock_write_space
;
292 sk
->sk_state_change
= ceph_sock_state_change
;
301 * initiate connection to a remote socket.
303 static int ceph_tcp_connect(struct ceph_connection
*con
)
305 struct sockaddr_storage
*paddr
= &con
->peer_addr
.in_addr
;
310 ret
= sock_create_kern(con
->peer_addr
.in_addr
.ss_family
, SOCK_STREAM
,
314 sock
->sk
->sk_allocation
= GFP_NOFS
;
316 #ifdef CONFIG_LOCKDEP
317 lockdep_set_class(&sock
->sk
->sk_lock
, &socket_class
);
320 set_sock_callbacks(sock
, con
);
322 dout("connect %s\n", ceph_pr_addr(&con
->peer_addr
.in_addr
));
324 con_sock_state_connecting(con
);
325 ret
= sock
->ops
->connect(sock
, (struct sockaddr
*)paddr
, sizeof(*paddr
),
327 if (ret
== -EINPROGRESS
) {
328 dout("connect %s EINPROGRESS sk_state = %u\n",
329 ceph_pr_addr(&con
->peer_addr
.in_addr
),
331 } else if (ret
< 0) {
332 pr_err("connect %s error %d\n",
333 ceph_pr_addr(&con
->peer_addr
.in_addr
), ret
);
335 con
->error_msg
= "connect error";
343 static int ceph_tcp_recvmsg(struct socket
*sock
, void *buf
, size_t len
)
345 struct kvec iov
= {buf
, len
};
346 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
349 r
= kernel_recvmsg(sock
, &msg
, &iov
, 1, len
, msg
.msg_flags
);
356 * write something. @more is true if caller will be sending more data
359 static int ceph_tcp_sendmsg(struct socket
*sock
, struct kvec
*iov
,
360 size_t kvlen
, size_t len
, int more
)
362 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
366 msg
.msg_flags
|= MSG_MORE
;
368 msg
.msg_flags
|= MSG_EOR
; /* superfluous, but what the hell */
370 r
= kernel_sendmsg(sock
, &msg
, iov
, kvlen
, len
);
376 static int ceph_tcp_sendpage(struct socket
*sock
, struct page
*page
,
377 int offset
, size_t size
, int more
)
379 int flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
| (more
? MSG_MORE
: MSG_EOR
);
382 ret
= kernel_sendpage(sock
, page
, offset
, size
, flags
);
391 * Shutdown/close the socket for the given connection.
393 static int con_close_socket(struct ceph_connection
*con
)
397 dout("con_close_socket on %p sock %p\n", con
, con
->sock
);
400 set_bit(SOCK_CLOSED
, &con
->state
);
401 rc
= con
->sock
->ops
->shutdown(con
->sock
, SHUT_RDWR
);
402 sock_release(con
->sock
);
404 clear_bit(SOCK_CLOSED
, &con
->state
);
405 con_sock_state_closed(con
);
410 * Reset a connection. Discard all incoming and outgoing messages
411 * and clear *_seq state.
413 static void ceph_msg_remove(struct ceph_msg
*msg
)
415 list_del_init(&msg
->list_head
);
416 BUG_ON(msg
->con
== NULL
);
417 msg
->con
->ops
->put(msg
->con
);
422 static void ceph_msg_remove_list(struct list_head
*head
)
424 while (!list_empty(head
)) {
425 struct ceph_msg
*msg
= list_first_entry(head
, struct ceph_msg
,
427 ceph_msg_remove(msg
);
431 static void reset_connection(struct ceph_connection
*con
)
433 /* reset connection, out_queue, msg_ and connect_seq */
434 /* discard existing out_queue and msg_seq */
435 ceph_msg_remove_list(&con
->out_queue
);
436 ceph_msg_remove_list(&con
->out_sent
);
439 BUG_ON(con
->in_msg
->con
!= con
);
440 con
->in_msg
->con
= NULL
;
441 ceph_msg_put(con
->in_msg
);
446 con
->connect_seq
= 0;
449 ceph_msg_put(con
->out_msg
);
453 con
->in_seq_acked
= 0;
457 * mark a peer down. drop any open connections.
459 void ceph_con_close(struct ceph_connection
*con
)
461 dout("con_close %p peer %s\n", con
,
462 ceph_pr_addr(&con
->peer_addr
.in_addr
));
463 clear_bit(NEGOTIATING
, &con
->state
);
464 clear_bit(STANDBY
, &con
->state
); /* avoid connect_seq bump */
465 set_bit(CLOSED
, &con
->state
);
467 clear_bit(LOSSYTX
, &con
->flags
); /* so we retry next connect */
468 clear_bit(KEEPALIVE_PENDING
, &con
->flags
);
469 clear_bit(WRITE_PENDING
, &con
->flags
);
471 mutex_lock(&con
->mutex
);
472 reset_connection(con
);
473 con
->peer_global_seq
= 0;
474 cancel_delayed_work(&con
->work
);
475 mutex_unlock(&con
->mutex
);
478 EXPORT_SYMBOL(ceph_con_close
);
481 * Reopen a closed connection, with a new peer address.
483 void ceph_con_open(struct ceph_connection
*con
, struct ceph_entity_addr
*addr
)
485 dout("con_open %p %s\n", con
, ceph_pr_addr(&addr
->in_addr
));
486 set_bit(OPENING
, &con
->state
);
487 WARN_ON(!test_and_clear_bit(CLOSED
, &con
->state
));
489 memcpy(&con
->peer_addr
, addr
, sizeof(*addr
));
490 con
->delay
= 0; /* reset backoff memory */
493 EXPORT_SYMBOL(ceph_con_open
);
496 * return true if this connection ever successfully opened
498 bool ceph_con_opened(struct ceph_connection
*con
)
500 return con
->connect_seq
> 0;
504 * initialize a new connection.
506 void ceph_con_init(struct ceph_connection
*con
, void *private,
507 const struct ceph_connection_operations
*ops
,
508 struct ceph_messenger
*msgr
, __u8 entity_type
, __u64 entity_num
)
510 dout("con_init %p\n", con
);
511 memset(con
, 0, sizeof(*con
));
512 con
->private = private;
516 con_sock_state_init(con
);
518 con
->peer_name
.type
= (__u8
) entity_type
;
519 con
->peer_name
.num
= cpu_to_le64(entity_num
);
521 mutex_init(&con
->mutex
);
522 INIT_LIST_HEAD(&con
->out_queue
);
523 INIT_LIST_HEAD(&con
->out_sent
);
524 INIT_DELAYED_WORK(&con
->work
, con_work
);
526 set_bit(CLOSED
, &con
->state
);
528 EXPORT_SYMBOL(ceph_con_init
);
532 * We maintain a global counter to order connection attempts. Get
533 * a unique seq greater than @gt.
535 static u32
get_global_seq(struct ceph_messenger
*msgr
, u32 gt
)
539 spin_lock(&msgr
->global_seq_lock
);
540 if (msgr
->global_seq
< gt
)
541 msgr
->global_seq
= gt
;
542 ret
= ++msgr
->global_seq
;
543 spin_unlock(&msgr
->global_seq_lock
);
547 static void con_out_kvec_reset(struct ceph_connection
*con
)
549 con
->out_kvec_left
= 0;
550 con
->out_kvec_bytes
= 0;
551 con
->out_kvec_cur
= &con
->out_kvec
[0];
554 static void con_out_kvec_add(struct ceph_connection
*con
,
555 size_t size
, void *data
)
559 index
= con
->out_kvec_left
;
560 BUG_ON(index
>= ARRAY_SIZE(con
->out_kvec
));
562 con
->out_kvec
[index
].iov_len
= size
;
563 con
->out_kvec
[index
].iov_base
= data
;
564 con
->out_kvec_left
++;
565 con
->out_kvec_bytes
+= size
;
568 static void prepare_write_message_data(struct ceph_connection
*con
)
570 struct ceph_msg
*msg
= con
->out_msg
;
573 BUG_ON(!msg
->hdr
.data_len
);
575 /* initialize page iterator */
576 con
->out_msg_pos
.page
= 0;
578 con
->out_msg_pos
.page_pos
= msg
->page_alignment
;
580 con
->out_msg_pos
.page_pos
= 0;
581 con
->out_msg_pos
.data_pos
= 0;
582 con
->out_msg_pos
.did_page_crc
= false;
583 con
->out_more
= 1; /* data + footer will follow */
587 * Prepare footer for currently outgoing message, and finish things
588 * off. Assumes out_kvec* are already valid.. we just add on to the end.
590 static void prepare_write_message_footer(struct ceph_connection
*con
)
592 struct ceph_msg
*m
= con
->out_msg
;
593 int v
= con
->out_kvec_left
;
595 dout("prepare_write_message_footer %p\n", con
);
596 con
->out_kvec_is_msg
= true;
597 con
->out_kvec
[v
].iov_base
= &m
->footer
;
598 con
->out_kvec
[v
].iov_len
= sizeof(m
->footer
);
599 con
->out_kvec_bytes
+= sizeof(m
->footer
);
600 con
->out_kvec_left
++;
601 con
->out_more
= m
->more_to_follow
;
602 con
->out_msg_done
= true;
606 * Prepare headers for the next outgoing message.
608 static void prepare_write_message(struct ceph_connection
*con
)
613 con_out_kvec_reset(con
);
614 con
->out_kvec_is_msg
= true;
615 con
->out_msg_done
= false;
617 /* Sneak an ack in there first? If we can get it into the same
618 * TCP packet that's a good thing. */
619 if (con
->in_seq
> con
->in_seq_acked
) {
620 con
->in_seq_acked
= con
->in_seq
;
621 con_out_kvec_add(con
, sizeof (tag_ack
), &tag_ack
);
622 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
623 con_out_kvec_add(con
, sizeof (con
->out_temp_ack
),
627 BUG_ON(list_empty(&con
->out_queue
));
628 m
= list_first_entry(&con
->out_queue
, struct ceph_msg
, list_head
);
630 BUG_ON(m
->con
!= con
);
632 /* put message on sent list */
634 list_move_tail(&m
->list_head
, &con
->out_sent
);
637 * only assign outgoing seq # if we haven't sent this message
638 * yet. if it is requeued, resend with it's original seq.
640 if (m
->needs_out_seq
) {
641 m
->hdr
.seq
= cpu_to_le64(++con
->out_seq
);
642 m
->needs_out_seq
= false;
649 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
650 m
, con
->out_seq
, le16_to_cpu(m
->hdr
.type
),
651 le32_to_cpu(m
->hdr
.front_len
), le32_to_cpu(m
->hdr
.middle_len
),
652 le32_to_cpu(m
->hdr
.data_len
),
654 BUG_ON(le32_to_cpu(m
->hdr
.front_len
) != m
->front
.iov_len
);
656 /* tag + hdr + front + middle */
657 con_out_kvec_add(con
, sizeof (tag_msg
), &tag_msg
);
658 con_out_kvec_add(con
, sizeof (m
->hdr
), &m
->hdr
);
659 con_out_kvec_add(con
, m
->front
.iov_len
, m
->front
.iov_base
);
662 con_out_kvec_add(con
, m
->middle
->vec
.iov_len
,
663 m
->middle
->vec
.iov_base
);
665 /* fill in crc (except data pages), footer */
666 crc
= crc32c(0, &m
->hdr
, offsetof(struct ceph_msg_header
, crc
));
667 con
->out_msg
->hdr
.crc
= cpu_to_le32(crc
);
668 con
->out_msg
->footer
.flags
= CEPH_MSG_FOOTER_COMPLETE
;
670 crc
= crc32c(0, m
->front
.iov_base
, m
->front
.iov_len
);
671 con
->out_msg
->footer
.front_crc
= cpu_to_le32(crc
);
673 crc
= crc32c(0, m
->middle
->vec
.iov_base
,
674 m
->middle
->vec
.iov_len
);
675 con
->out_msg
->footer
.middle_crc
= cpu_to_le32(crc
);
677 con
->out_msg
->footer
.middle_crc
= 0;
678 dout("%s front_crc %u middle_crc %u\n", __func__
,
679 le32_to_cpu(con
->out_msg
->footer
.front_crc
),
680 le32_to_cpu(con
->out_msg
->footer
.middle_crc
));
682 /* is there a data payload? */
683 con
->out_msg
->footer
.data_crc
= 0;
685 prepare_write_message_data(con
);
687 /* no, queue up footer too and be done */
688 prepare_write_message_footer(con
);
690 set_bit(WRITE_PENDING
, &con
->flags
);
696 static void prepare_write_ack(struct ceph_connection
*con
)
698 dout("prepare_write_ack %p %llu -> %llu\n", con
,
699 con
->in_seq_acked
, con
->in_seq
);
700 con
->in_seq_acked
= con
->in_seq
;
702 con_out_kvec_reset(con
);
704 con_out_kvec_add(con
, sizeof (tag_ack
), &tag_ack
);
706 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
707 con_out_kvec_add(con
, sizeof (con
->out_temp_ack
),
710 con
->out_more
= 1; /* more will follow.. eventually.. */
711 set_bit(WRITE_PENDING
, &con
->flags
);
715 * Prepare to write keepalive byte.
717 static void prepare_write_keepalive(struct ceph_connection
*con
)
719 dout("prepare_write_keepalive %p\n", con
);
720 con_out_kvec_reset(con
);
721 con_out_kvec_add(con
, sizeof (tag_keepalive
), &tag_keepalive
);
722 set_bit(WRITE_PENDING
, &con
->flags
);
726 * Connection negotiation.
729 static struct ceph_auth_handshake
*get_connect_authorizer(struct ceph_connection
*con
,
732 struct ceph_auth_handshake
*auth
;
734 if (!con
->ops
->get_authorizer
) {
735 con
->out_connect
.authorizer_protocol
= CEPH_AUTH_UNKNOWN
;
736 con
->out_connect
.authorizer_len
= 0;
741 /* Can't hold the mutex while getting authorizer */
743 mutex_unlock(&con
->mutex
);
745 auth
= con
->ops
->get_authorizer(con
, auth_proto
, con
->auth_retry
);
747 mutex_lock(&con
->mutex
);
751 if (test_bit(CLOSED
, &con
->state
) || test_bit(OPENING
, &con
->flags
))
752 return ERR_PTR(-EAGAIN
);
754 con
->auth_reply_buf
= auth
->authorizer_reply_buf
;
755 con
->auth_reply_buf_len
= auth
->authorizer_reply_buf_len
;
762 * We connected to a peer and are saying hello.
764 static void prepare_write_banner(struct ceph_connection
*con
)
766 con_out_kvec_add(con
, strlen(CEPH_BANNER
), CEPH_BANNER
);
767 con_out_kvec_add(con
, sizeof (con
->msgr
->my_enc_addr
),
768 &con
->msgr
->my_enc_addr
);
771 set_bit(WRITE_PENDING
, &con
->flags
);
774 static int prepare_write_connect(struct ceph_connection
*con
)
776 unsigned int global_seq
= get_global_seq(con
->msgr
, 0);
779 struct ceph_auth_handshake
*auth
;
781 switch (con
->peer_name
.type
) {
782 case CEPH_ENTITY_TYPE_MON
:
783 proto
= CEPH_MONC_PROTOCOL
;
785 case CEPH_ENTITY_TYPE_OSD
:
786 proto
= CEPH_OSDC_PROTOCOL
;
788 case CEPH_ENTITY_TYPE_MDS
:
789 proto
= CEPH_MDSC_PROTOCOL
;
795 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con
,
796 con
->connect_seq
, global_seq
, proto
);
798 con
->out_connect
.features
= cpu_to_le64(con
->msgr
->supported_features
);
799 con
->out_connect
.host_type
= cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT
);
800 con
->out_connect
.connect_seq
= cpu_to_le32(con
->connect_seq
);
801 con
->out_connect
.global_seq
= cpu_to_le32(global_seq
);
802 con
->out_connect
.protocol_version
= cpu_to_le32(proto
);
803 con
->out_connect
.flags
= 0;
805 auth_proto
= CEPH_AUTH_UNKNOWN
;
806 auth
= get_connect_authorizer(con
, &auth_proto
);
808 return PTR_ERR(auth
);
810 con
->out_connect
.authorizer_protocol
= cpu_to_le32(auth_proto
);
811 con
->out_connect
.authorizer_len
= auth
?
812 cpu_to_le32(auth
->authorizer_buf_len
) : 0;
814 con_out_kvec_add(con
, sizeof (con
->out_connect
),
816 if (auth
&& auth
->authorizer_buf_len
)
817 con_out_kvec_add(con
, auth
->authorizer_buf_len
,
818 auth
->authorizer_buf
);
821 set_bit(WRITE_PENDING
, &con
->flags
);
827 * write as much of pending kvecs to the socket as we can.
829 * 0 -> socket full, but more to do
832 static int write_partial_kvec(struct ceph_connection
*con
)
836 dout("write_partial_kvec %p %d left\n", con
, con
->out_kvec_bytes
);
837 while (con
->out_kvec_bytes
> 0) {
838 ret
= ceph_tcp_sendmsg(con
->sock
, con
->out_kvec_cur
,
839 con
->out_kvec_left
, con
->out_kvec_bytes
,
843 con
->out_kvec_bytes
-= ret
;
844 if (con
->out_kvec_bytes
== 0)
847 /* account for full iov entries consumed */
848 while (ret
>= con
->out_kvec_cur
->iov_len
) {
849 BUG_ON(!con
->out_kvec_left
);
850 ret
-= con
->out_kvec_cur
->iov_len
;
852 con
->out_kvec_left
--;
854 /* and for a partially-consumed entry */
856 con
->out_kvec_cur
->iov_len
-= ret
;
857 con
->out_kvec_cur
->iov_base
+= ret
;
860 con
->out_kvec_left
= 0;
861 con
->out_kvec_is_msg
= false;
864 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con
,
865 con
->out_kvec_bytes
, con
->out_kvec_left
, ret
);
866 return ret
; /* done! */
870 static void init_bio_iter(struct bio
*bio
, struct bio
**iter
, int *seg
)
881 static void iter_bio_next(struct bio
**bio_iter
, int *seg
)
883 if (*bio_iter
== NULL
)
886 BUG_ON(*seg
>= (*bio_iter
)->bi_vcnt
);
889 if (*seg
== (*bio_iter
)->bi_vcnt
)
890 init_bio_iter((*bio_iter
)->bi_next
, bio_iter
, seg
);
895 * Write as much message data payload as we can. If we finish, queue
897 * 1 -> done, footer is now queued in out_kvec[].
898 * 0 -> socket full, but more to do
901 static int write_partial_msg_pages(struct ceph_connection
*con
)
903 struct ceph_msg
*msg
= con
->out_msg
;
904 unsigned int data_len
= le32_to_cpu(msg
->hdr
.data_len
);
906 bool do_datacrc
= !con
->msgr
->nocrc
;
910 size_t trail_len
= (msg
->trail
? msg
->trail
->length
: 0);
912 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
913 con
, con
->out_msg
, con
->out_msg_pos
.page
, con
->out_msg
->nr_pages
,
914 con
->out_msg_pos
.page_pos
);
917 if (msg
->bio
&& !msg
->bio_iter
)
918 init_bio_iter(msg
->bio
, &msg
->bio_iter
, &msg
->bio_seg
);
921 while (data_len
> con
->out_msg_pos
.data_pos
) {
922 struct page
*page
= NULL
;
923 int max_write
= PAGE_SIZE
;
926 total_max_write
= data_len
- trail_len
-
927 con
->out_msg_pos
.data_pos
;
930 * if we are calculating the data crc (the default), we need
931 * to map the page. if our pages[] has been revoked, use the
935 /* have we reached the trail part of the data? */
936 if (con
->out_msg_pos
.data_pos
>= data_len
- trail_len
) {
939 total_max_write
= data_len
- con
->out_msg_pos
.data_pos
;
941 page
= list_first_entry(&msg
->trail
->head
,
943 max_write
= PAGE_SIZE
;
944 } else if (msg
->pages
) {
945 page
= msg
->pages
[con
->out_msg_pos
.page
];
946 } else if (msg
->pagelist
) {
947 page
= list_first_entry(&msg
->pagelist
->head
,
950 } else if (msg
->bio
) {
953 bv
= bio_iovec_idx(msg
->bio_iter
, msg
->bio_seg
);
955 bio_offset
= bv
->bv_offset
;
956 max_write
= bv
->bv_len
;
961 len
= min_t(int, max_write
- con
->out_msg_pos
.page_pos
,
964 if (do_datacrc
&& !con
->out_msg_pos
.did_page_crc
) {
967 u32 tmpcrc
= le32_to_cpu(con
->out_msg
->footer
.data_crc
);
971 BUG_ON(kaddr
== NULL
);
972 base
= kaddr
+ con
->out_msg_pos
.page_pos
+ bio_offset
;
973 crc
= crc32c(tmpcrc
, base
, len
);
974 con
->out_msg
->footer
.data_crc
= cpu_to_le32(crc
);
975 con
->out_msg_pos
.did_page_crc
= true;
977 ret
= ceph_tcp_sendpage(con
->sock
, page
,
978 con
->out_msg_pos
.page_pos
+ bio_offset
,
987 con
->out_msg_pos
.data_pos
+= ret
;
988 con
->out_msg_pos
.page_pos
+= ret
;
990 con
->out_msg_pos
.page_pos
= 0;
991 con
->out_msg_pos
.page
++;
992 con
->out_msg_pos
.did_page_crc
= false;
994 list_move_tail(&page
->lru
,
996 else if (msg
->pagelist
)
997 list_move_tail(&page
->lru
,
998 &msg
->pagelist
->head
);
1001 iter_bio_next(&msg
->bio_iter
, &msg
->bio_seg
);
1006 dout("write_partial_msg_pages %p msg %p done\n", con
, msg
);
1008 /* prepare and queue up footer, too */
1010 con
->out_msg
->footer
.flags
|= CEPH_MSG_FOOTER_NOCRC
;
1011 con_out_kvec_reset(con
);
1012 prepare_write_message_footer(con
);
1021 static int write_partial_skip(struct ceph_connection
*con
)
1025 while (con
->out_skip
> 0) {
1026 size_t size
= min(con
->out_skip
, (int) PAGE_CACHE_SIZE
);
1028 ret
= ceph_tcp_sendpage(con
->sock
, zero_page
, 0, size
, 1);
1031 con
->out_skip
-= ret
;
1039 * Prepare to read connection handshake, or an ack.
1041 static void prepare_read_banner(struct ceph_connection
*con
)
1043 dout("prepare_read_banner %p\n", con
);
1044 con
->in_base_pos
= 0;
1047 static void prepare_read_connect(struct ceph_connection
*con
)
1049 dout("prepare_read_connect %p\n", con
);
1050 con
->in_base_pos
= 0;
1053 static void prepare_read_ack(struct ceph_connection
*con
)
1055 dout("prepare_read_ack %p\n", con
);
1056 con
->in_base_pos
= 0;
1059 static void prepare_read_tag(struct ceph_connection
*con
)
1061 dout("prepare_read_tag %p\n", con
);
1062 con
->in_base_pos
= 0;
1063 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1067 * Prepare to read a message.
1069 static int prepare_read_message(struct ceph_connection
*con
)
1071 dout("prepare_read_message %p\n", con
);
1072 BUG_ON(con
->in_msg
!= NULL
);
1073 con
->in_base_pos
= 0;
1074 con
->in_front_crc
= con
->in_middle_crc
= con
->in_data_crc
= 0;
1079 static int read_partial(struct ceph_connection
*con
,
1080 int end
, int size
, void *object
)
1082 while (con
->in_base_pos
< end
) {
1083 int left
= end
- con
->in_base_pos
;
1084 int have
= size
- left
;
1085 int ret
= ceph_tcp_recvmsg(con
->sock
, object
+ have
, left
);
1088 con
->in_base_pos
+= ret
;
1095 * Read all or part of the connect-side handshake on a new connection
1097 static int read_partial_banner(struct ceph_connection
*con
)
1103 dout("read_partial_banner %p at %d\n", con
, con
->in_base_pos
);
1106 size
= strlen(CEPH_BANNER
);
1108 ret
= read_partial(con
, end
, size
, con
->in_banner
);
1112 size
= sizeof (con
->actual_peer_addr
);
1114 ret
= read_partial(con
, end
, size
, &con
->actual_peer_addr
);
1118 size
= sizeof (con
->peer_addr_for_me
);
1120 ret
= read_partial(con
, end
, size
, &con
->peer_addr_for_me
);
1128 static int read_partial_connect(struct ceph_connection
*con
)
1134 dout("read_partial_connect %p at %d\n", con
, con
->in_base_pos
);
1136 size
= sizeof (con
->in_reply
);
1138 ret
= read_partial(con
, end
, size
, &con
->in_reply
);
1142 size
= le32_to_cpu(con
->in_reply
.authorizer_len
);
1144 ret
= read_partial(con
, end
, size
, con
->auth_reply_buf
);
1148 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1149 con
, (int)con
->in_reply
.tag
,
1150 le32_to_cpu(con
->in_reply
.connect_seq
),
1151 le32_to_cpu(con
->in_reply
.global_seq
));
1158 * Verify the hello banner looks okay.
1160 static int verify_hello(struct ceph_connection
*con
)
1162 if (memcmp(con
->in_banner
, CEPH_BANNER
, strlen(CEPH_BANNER
))) {
1163 pr_err("connect to %s got bad banner\n",
1164 ceph_pr_addr(&con
->peer_addr
.in_addr
));
1165 con
->error_msg
= "protocol error, bad banner";
1171 static bool addr_is_blank(struct sockaddr_storage
*ss
)
1173 switch (ss
->ss_family
) {
1175 return ((struct sockaddr_in
*)ss
)->sin_addr
.s_addr
== 0;
1178 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[0] == 0 &&
1179 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[1] == 0 &&
1180 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[2] == 0 &&
1181 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[3] == 0;
1186 static int addr_port(struct sockaddr_storage
*ss
)
1188 switch (ss
->ss_family
) {
1190 return ntohs(((struct sockaddr_in
*)ss
)->sin_port
);
1192 return ntohs(((struct sockaddr_in6
*)ss
)->sin6_port
);
1197 static void addr_set_port(struct sockaddr_storage
*ss
, int p
)
1199 switch (ss
->ss_family
) {
1201 ((struct sockaddr_in
*)ss
)->sin_port
= htons(p
);
1204 ((struct sockaddr_in6
*)ss
)->sin6_port
= htons(p
);
1210 * Unlike other *_pton function semantics, zero indicates success.
1212 static int ceph_pton(const char *str
, size_t len
, struct sockaddr_storage
*ss
,
1213 char delim
, const char **ipend
)
1215 struct sockaddr_in
*in4
= (struct sockaddr_in
*) ss
;
1216 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*) ss
;
1218 memset(ss
, 0, sizeof(*ss
));
1220 if (in4_pton(str
, len
, (u8
*)&in4
->sin_addr
.s_addr
, delim
, ipend
)) {
1221 ss
->ss_family
= AF_INET
;
1225 if (in6_pton(str
, len
, (u8
*)&in6
->sin6_addr
.s6_addr
, delim
, ipend
)) {
1226 ss
->ss_family
= AF_INET6
;
1234 * Extract hostname string and resolve using kernel DNS facility.
1236 #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1237 static int ceph_dns_resolve_name(const char *name
, size_t namelen
,
1238 struct sockaddr_storage
*ss
, char delim
, const char **ipend
)
1240 const char *end
, *delim_p
;
1241 char *colon_p
, *ip_addr
= NULL
;
1245 * The end of the hostname occurs immediately preceding the delimiter or
1246 * the port marker (':') where the delimiter takes precedence.
1248 delim_p
= memchr(name
, delim
, namelen
);
1249 colon_p
= memchr(name
, ':', namelen
);
1251 if (delim_p
&& colon_p
)
1252 end
= delim_p
< colon_p
? delim_p
: colon_p
;
1253 else if (!delim_p
&& colon_p
)
1257 if (!end
) /* case: hostname:/ */
1258 end
= name
+ namelen
;
1264 /* do dns_resolve upcall */
1265 ip_len
= dns_query(NULL
, name
, end
- name
, NULL
, &ip_addr
, NULL
);
1267 ret
= ceph_pton(ip_addr
, ip_len
, ss
, -1, NULL
);
1275 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end
- name
), name
,
1276 ret
, ret
? "failed" : ceph_pr_addr(ss
));
1281 static inline int ceph_dns_resolve_name(const char *name
, size_t namelen
,
1282 struct sockaddr_storage
*ss
, char delim
, const char **ipend
)
1289 * Parse a server name (IP or hostname). If a valid IP address is not found
1290 * then try to extract a hostname to resolve using userspace DNS upcall.
1292 static int ceph_parse_server_name(const char *name
, size_t namelen
,
1293 struct sockaddr_storage
*ss
, char delim
, const char **ipend
)
1297 ret
= ceph_pton(name
, namelen
, ss
, delim
, ipend
);
1299 ret
= ceph_dns_resolve_name(name
, namelen
, ss
, delim
, ipend
);
1305 * Parse an ip[:port] list into an addr array. Use the default
1306 * monitor port if a port isn't specified.
1308 int ceph_parse_ips(const char *c
, const char *end
,
1309 struct ceph_entity_addr
*addr
,
1310 int max_count
, int *count
)
1312 int i
, ret
= -EINVAL
;
1315 dout("parse_ips on '%.*s'\n", (int)(end
-c
), c
);
1316 for (i
= 0; i
< max_count
; i
++) {
1318 struct sockaddr_storage
*ss
= &addr
[i
].in_addr
;
1327 ret
= ceph_parse_server_name(p
, end
- p
, ss
, delim
, &ipend
);
1336 dout("missing matching ']'\n");
1343 if (p
< end
&& *p
== ':') {
1346 while (p
< end
&& *p
>= '0' && *p
<= '9') {
1347 port
= (port
* 10) + (*p
- '0');
1350 if (port
> 65535 || port
== 0)
1353 port
= CEPH_MON_PORT
;
1356 addr_set_port(ss
, port
);
1358 dout("parse_ips got %s\n", ceph_pr_addr(ss
));
1375 pr_err("parse_ips bad ip '%.*s'\n", (int)(end
- c
), c
);
1378 EXPORT_SYMBOL(ceph_parse_ips
);
1380 static int process_banner(struct ceph_connection
*con
)
1382 dout("process_banner on %p\n", con
);
1384 if (verify_hello(con
) < 0)
1387 ceph_decode_addr(&con
->actual_peer_addr
);
1388 ceph_decode_addr(&con
->peer_addr_for_me
);
1391 * Make sure the other end is who we wanted. note that the other
1392 * end may not yet know their ip address, so if it's 0.0.0.0, give
1393 * them the benefit of the doubt.
1395 if (memcmp(&con
->peer_addr
, &con
->actual_peer_addr
,
1396 sizeof(con
->peer_addr
)) != 0 &&
1397 !(addr_is_blank(&con
->actual_peer_addr
.in_addr
) &&
1398 con
->actual_peer_addr
.nonce
== con
->peer_addr
.nonce
)) {
1399 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
1400 ceph_pr_addr(&con
->peer_addr
.in_addr
),
1401 (int)le32_to_cpu(con
->peer_addr
.nonce
),
1402 ceph_pr_addr(&con
->actual_peer_addr
.in_addr
),
1403 (int)le32_to_cpu(con
->actual_peer_addr
.nonce
));
1404 con
->error_msg
= "wrong peer at address";
1409 * did we learn our address?
1411 if (addr_is_blank(&con
->msgr
->inst
.addr
.in_addr
)) {
1412 int port
= addr_port(&con
->msgr
->inst
.addr
.in_addr
);
1414 memcpy(&con
->msgr
->inst
.addr
.in_addr
,
1415 &con
->peer_addr_for_me
.in_addr
,
1416 sizeof(con
->peer_addr_for_me
.in_addr
));
1417 addr_set_port(&con
->msgr
->inst
.addr
.in_addr
, port
);
1418 encode_my_addr(con
->msgr
);
1419 dout("process_banner learned my addr is %s\n",
1420 ceph_pr_addr(&con
->msgr
->inst
.addr
.in_addr
));
1423 set_bit(NEGOTIATING
, &con
->state
);
1424 prepare_read_connect(con
);
1428 static void fail_protocol(struct ceph_connection
*con
)
1430 reset_connection(con
);
1431 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
1434 static int process_connect(struct ceph_connection
*con
)
1436 u64 sup_feat
= con
->msgr
->supported_features
;
1437 u64 req_feat
= con
->msgr
->required_features
;
1438 u64 server_feat
= le64_to_cpu(con
->in_reply
.features
);
1441 dout("process_connect on %p tag %d\n", con
, (int)con
->in_tag
);
1443 switch (con
->in_reply
.tag
) {
1444 case CEPH_MSGR_TAG_FEATURES
:
1445 pr_err("%s%lld %s feature set mismatch,"
1446 " my %llx < server's %llx, missing %llx\n",
1447 ENTITY_NAME(con
->peer_name
),
1448 ceph_pr_addr(&con
->peer_addr
.in_addr
),
1449 sup_feat
, server_feat
, server_feat
& ~sup_feat
);
1450 con
->error_msg
= "missing required protocol features";
1454 case CEPH_MSGR_TAG_BADPROTOVER
:
1455 pr_err("%s%lld %s protocol version mismatch,"
1456 " my %d != server's %d\n",
1457 ENTITY_NAME(con
->peer_name
),
1458 ceph_pr_addr(&con
->peer_addr
.in_addr
),
1459 le32_to_cpu(con
->out_connect
.protocol_version
),
1460 le32_to_cpu(con
->in_reply
.protocol_version
));
1461 con
->error_msg
= "protocol version mismatch";
1465 case CEPH_MSGR_TAG_BADAUTHORIZER
:
1467 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con
,
1469 if (con
->auth_retry
== 2) {
1470 con
->error_msg
= "connect authorization failure";
1473 con
->auth_retry
= 1;
1474 con_out_kvec_reset(con
);
1475 ret
= prepare_write_connect(con
);
1478 prepare_read_connect(con
);
1481 case CEPH_MSGR_TAG_RESETSESSION
:
1483 * If we connected with a large connect_seq but the peer
1484 * has no record of a session with us (no connection, or
1485 * connect_seq == 0), they will send RESETSESION to indicate
1486 * that they must have reset their session, and may have
1489 dout("process_connect got RESET peer seq %u\n",
1490 le32_to_cpu(con
->in_connect
.connect_seq
));
1491 pr_err("%s%lld %s connection reset\n",
1492 ENTITY_NAME(con
->peer_name
),
1493 ceph_pr_addr(&con
->peer_addr
.in_addr
));
1494 reset_connection(con
);
1495 con_out_kvec_reset(con
);
1496 ret
= prepare_write_connect(con
);
1499 prepare_read_connect(con
);
1501 /* Tell ceph about it. */
1502 mutex_unlock(&con
->mutex
);
1503 pr_info("reset on %s%lld\n", ENTITY_NAME(con
->peer_name
));
1504 if (con
->ops
->peer_reset
)
1505 con
->ops
->peer_reset(con
);
1506 mutex_lock(&con
->mutex
);
1507 if (test_bit(CLOSED
, &con
->state
) ||
1508 test_bit(OPENING
, &con
->state
))
1512 case CEPH_MSGR_TAG_RETRY_SESSION
:
1514 * If we sent a smaller connect_seq than the peer has, try
1515 * again with a larger value.
1517 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1518 le32_to_cpu(con
->out_connect
.connect_seq
),
1519 le32_to_cpu(con
->in_connect
.connect_seq
));
1520 con
->connect_seq
= le32_to_cpu(con
->in_connect
.connect_seq
);
1521 con_out_kvec_reset(con
);
1522 ret
= prepare_write_connect(con
);
1525 prepare_read_connect(con
);
1528 case CEPH_MSGR_TAG_RETRY_GLOBAL
:
1530 * If we sent a smaller global_seq than the peer has, try
1531 * again with a larger value.
1533 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
1534 con
->peer_global_seq
,
1535 le32_to_cpu(con
->in_connect
.global_seq
));
1536 get_global_seq(con
->msgr
,
1537 le32_to_cpu(con
->in_connect
.global_seq
));
1538 con_out_kvec_reset(con
);
1539 ret
= prepare_write_connect(con
);
1542 prepare_read_connect(con
);
1545 case CEPH_MSGR_TAG_READY
:
1546 if (req_feat
& ~server_feat
) {
1547 pr_err("%s%lld %s protocol feature mismatch,"
1548 " my required %llx > server's %llx, need %llx\n",
1549 ENTITY_NAME(con
->peer_name
),
1550 ceph_pr_addr(&con
->peer_addr
.in_addr
),
1551 req_feat
, server_feat
, req_feat
& ~server_feat
);
1552 con
->error_msg
= "missing required protocol features";
1556 clear_bit(CONNECTING
, &con
->state
);
1557 con
->peer_global_seq
= le32_to_cpu(con
->in_reply
.global_seq
);
1559 con
->peer_features
= server_feat
;
1560 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1561 con
->peer_global_seq
,
1562 le32_to_cpu(con
->in_reply
.connect_seq
),
1564 WARN_ON(con
->connect_seq
!=
1565 le32_to_cpu(con
->in_reply
.connect_seq
));
1567 if (con
->in_reply
.flags
& CEPH_MSG_CONNECT_LOSSY
)
1568 set_bit(LOSSYTX
, &con
->flags
);
1570 prepare_read_tag(con
);
1573 case CEPH_MSGR_TAG_WAIT
:
1575 * If there is a connection race (we are opening
1576 * connections to each other), one of us may just have
1577 * to WAIT. This shouldn't happen if we are the
1580 pr_err("process_connect got WAIT as client\n");
1581 con
->error_msg
= "protocol error, got WAIT as client";
1585 pr_err("connect protocol error, will retry\n");
1586 con
->error_msg
= "protocol error, garbage tag during connect";
1594 * read (part of) an ack
1596 static int read_partial_ack(struct ceph_connection
*con
)
1598 int size
= sizeof (con
->in_temp_ack
);
1601 return read_partial(con
, end
, size
, &con
->in_temp_ack
);
1606 * We can finally discard anything that's been acked.
1608 static void process_ack(struct ceph_connection
*con
)
1611 u64 ack
= le64_to_cpu(con
->in_temp_ack
);
1614 while (!list_empty(&con
->out_sent
)) {
1615 m
= list_first_entry(&con
->out_sent
, struct ceph_msg
,
1617 seq
= le64_to_cpu(m
->hdr
.seq
);
1620 dout("got ack for seq %llu type %d at %p\n", seq
,
1621 le16_to_cpu(m
->hdr
.type
), m
);
1622 m
->ack_stamp
= jiffies
;
1625 prepare_read_tag(con
);
1631 static int read_partial_message_section(struct ceph_connection
*con
,
1632 struct kvec
*section
,
1633 unsigned int sec_len
, u32
*crc
)
1639 while (section
->iov_len
< sec_len
) {
1640 BUG_ON(section
->iov_base
== NULL
);
1641 left
= sec_len
- section
->iov_len
;
1642 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)section
->iov_base
+
1643 section
->iov_len
, left
);
1646 section
->iov_len
+= ret
;
1648 if (section
->iov_len
== sec_len
)
1649 *crc
= crc32c(0, section
->iov_base
, section
->iov_len
);
1654 static bool ceph_con_in_msg_alloc(struct ceph_connection
*con
,
1655 struct ceph_msg_header
*hdr
);
1658 static int read_partial_message_pages(struct ceph_connection
*con
,
1659 struct page
**pages
,
1660 unsigned int data_len
, bool do_datacrc
)
1666 left
= min((int)(data_len
- con
->in_msg_pos
.data_pos
),
1667 (int)(PAGE_SIZE
- con
->in_msg_pos
.page_pos
));
1669 BUG_ON(pages
== NULL
);
1670 p
= kmap(pages
[con
->in_msg_pos
.page
]);
1671 ret
= ceph_tcp_recvmsg(con
->sock
, p
+ con
->in_msg_pos
.page_pos
,
1673 if (ret
> 0 && do_datacrc
)
1675 crc32c(con
->in_data_crc
,
1676 p
+ con
->in_msg_pos
.page_pos
, ret
);
1677 kunmap(pages
[con
->in_msg_pos
.page
]);
1680 con
->in_msg_pos
.data_pos
+= ret
;
1681 con
->in_msg_pos
.page_pos
+= ret
;
1682 if (con
->in_msg_pos
.page_pos
== PAGE_SIZE
) {
1683 con
->in_msg_pos
.page_pos
= 0;
1684 con
->in_msg_pos
.page
++;
1691 static int read_partial_message_bio(struct ceph_connection
*con
,
1692 struct bio
**bio_iter
, int *bio_seg
,
1693 unsigned int data_len
, bool do_datacrc
)
1695 struct bio_vec
*bv
= bio_iovec_idx(*bio_iter
, *bio_seg
);
1702 left
= min((int)(data_len
- con
->in_msg_pos
.data_pos
),
1703 (int)(bv
->bv_len
- con
->in_msg_pos
.page_pos
));
1705 p
= kmap(bv
->bv_page
) + bv
->bv_offset
;
1707 ret
= ceph_tcp_recvmsg(con
->sock
, p
+ con
->in_msg_pos
.page_pos
,
1709 if (ret
> 0 && do_datacrc
)
1711 crc32c(con
->in_data_crc
,
1712 p
+ con
->in_msg_pos
.page_pos
, ret
);
1713 kunmap(bv
->bv_page
);
1716 con
->in_msg_pos
.data_pos
+= ret
;
1717 con
->in_msg_pos
.page_pos
+= ret
;
1718 if (con
->in_msg_pos
.page_pos
== bv
->bv_len
) {
1719 con
->in_msg_pos
.page_pos
= 0;
1720 iter_bio_next(bio_iter
, bio_seg
);
1728 * read (part of) a message.
1730 static int read_partial_message(struct ceph_connection
*con
)
1732 struct ceph_msg
*m
= con
->in_msg
;
1736 unsigned int front_len
, middle_len
, data_len
;
1737 bool do_datacrc
= !con
->msgr
->nocrc
;
1741 dout("read_partial_message con %p msg %p\n", con
, m
);
1744 size
= sizeof (con
->in_hdr
);
1746 ret
= read_partial(con
, end
, size
, &con
->in_hdr
);
1750 crc
= crc32c(0, &con
->in_hdr
, offsetof(struct ceph_msg_header
, crc
));
1751 if (cpu_to_le32(crc
) != con
->in_hdr
.crc
) {
1752 pr_err("read_partial_message bad hdr "
1753 " crc %u != expected %u\n",
1754 crc
, con
->in_hdr
.crc
);
1758 front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
1759 if (front_len
> CEPH_MSG_MAX_FRONT_LEN
)
1761 middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
1762 if (middle_len
> CEPH_MSG_MAX_DATA_LEN
)
1764 data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
1765 if (data_len
> CEPH_MSG_MAX_DATA_LEN
)
1769 seq
= le64_to_cpu(con
->in_hdr
.seq
);
1770 if ((s64
)seq
- (s64
)con
->in_seq
< 1) {
1771 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
1772 ENTITY_NAME(con
->peer_name
),
1773 ceph_pr_addr(&con
->peer_addr
.in_addr
),
1774 seq
, con
->in_seq
+ 1);
1775 con
->in_base_pos
= -front_len
- middle_len
- data_len
-
1777 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1779 } else if ((s64
)seq
- (s64
)con
->in_seq
> 1) {
1780 pr_err("read_partial_message bad seq %lld expected %lld\n",
1781 seq
, con
->in_seq
+ 1);
1782 con
->error_msg
= "bad message sequence # for incoming message";
1786 /* allocate message? */
1788 dout("got hdr type %d front %d data %d\n", con
->in_hdr
.type
,
1789 con
->in_hdr
.front_len
, con
->in_hdr
.data_len
);
1790 if (ceph_con_in_msg_alloc(con
, &con
->in_hdr
)) {
1791 /* skip this message */
1792 dout("alloc_msg said skip message\n");
1793 BUG_ON(con
->in_msg
);
1794 con
->in_base_pos
= -front_len
- middle_len
- data_len
-
1796 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1802 "error allocating memory for incoming message";
1806 BUG_ON(con
->in_msg
->con
!= con
);
1808 m
->front
.iov_len
= 0; /* haven't read it yet */
1810 m
->middle
->vec
.iov_len
= 0;
1812 con
->in_msg_pos
.page
= 0;
1814 con
->in_msg_pos
.page_pos
= m
->page_alignment
;
1816 con
->in_msg_pos
.page_pos
= 0;
1817 con
->in_msg_pos
.data_pos
= 0;
1821 ret
= read_partial_message_section(con
, &m
->front
, front_len
,
1822 &con
->in_front_crc
);
1828 ret
= read_partial_message_section(con
, &m
->middle
->vec
,
1830 &con
->in_middle_crc
);
1835 if (m
->bio
&& !m
->bio_iter
)
1836 init_bio_iter(m
->bio
, &m
->bio_iter
, &m
->bio_seg
);
1840 while (con
->in_msg_pos
.data_pos
< data_len
) {
1842 ret
= read_partial_message_pages(con
, m
->pages
,
1843 data_len
, do_datacrc
);
1847 } else if (m
->bio
) {
1849 ret
= read_partial_message_bio(con
,
1850 &m
->bio_iter
, &m
->bio_seg
,
1851 data_len
, do_datacrc
);
1861 size
= sizeof (m
->footer
);
1863 ret
= read_partial(con
, end
, size
, &m
->footer
);
1867 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1868 m
, front_len
, m
->footer
.front_crc
, middle_len
,
1869 m
->footer
.middle_crc
, data_len
, m
->footer
.data_crc
);
1872 if (con
->in_front_crc
!= le32_to_cpu(m
->footer
.front_crc
)) {
1873 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1874 m
, con
->in_front_crc
, m
->footer
.front_crc
);
1877 if (con
->in_middle_crc
!= le32_to_cpu(m
->footer
.middle_crc
)) {
1878 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1879 m
, con
->in_middle_crc
, m
->footer
.middle_crc
);
1883 (m
->footer
.flags
& CEPH_MSG_FOOTER_NOCRC
) == 0 &&
1884 con
->in_data_crc
!= le32_to_cpu(m
->footer
.data_crc
)) {
1885 pr_err("read_partial_message %p data crc %u != exp. %u\n", m
,
1886 con
->in_data_crc
, le32_to_cpu(m
->footer
.data_crc
));
1890 return 1; /* done! */
1894 * Process message. This happens in the worker thread. The callback should
1895 * be careful not to do anything that waits on other incoming messages or it
1898 static void process_message(struct ceph_connection
*con
)
1900 struct ceph_msg
*msg
;
1902 BUG_ON(con
->in_msg
->con
!= con
);
1903 con
->in_msg
->con
= NULL
;
1908 /* if first message, set peer_name */
1909 if (con
->peer_name
.type
== 0)
1910 con
->peer_name
= msg
->hdr
.src
;
1913 mutex_unlock(&con
->mutex
);
1915 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1916 msg
, le64_to_cpu(msg
->hdr
.seq
),
1917 ENTITY_NAME(msg
->hdr
.src
),
1918 le16_to_cpu(msg
->hdr
.type
),
1919 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
1920 le32_to_cpu(msg
->hdr
.front_len
),
1921 le32_to_cpu(msg
->hdr
.data_len
),
1922 con
->in_front_crc
, con
->in_middle_crc
, con
->in_data_crc
);
1923 con
->ops
->dispatch(con
, msg
);
1925 mutex_lock(&con
->mutex
);
1926 prepare_read_tag(con
);
1931 * Write something to the socket. Called in a worker thread when the
1932 * socket appears to be writeable and we have something ready to send.
1934 static int try_write(struct ceph_connection
*con
)
1938 dout("try_write start %p state %lu\n", con
, con
->state
);
1941 dout("try_write out_kvec_bytes %d\n", con
->out_kvec_bytes
);
1943 /* open the socket first? */
1944 if (con
->sock
== NULL
) {
1945 clear_bit(NEGOTIATING
, &con
->state
);
1946 set_bit(CONNECTING
, &con
->state
);
1948 con_out_kvec_reset(con
);
1949 prepare_write_banner(con
);
1950 ret
= prepare_write_connect(con
);
1953 prepare_read_banner(con
);
1955 BUG_ON(con
->in_msg
);
1956 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1957 dout("try_write initiating connect on %p new state %lu\n",
1959 ret
= ceph_tcp_connect(con
);
1961 con
->error_msg
= "connect error";
1967 /* kvec data queued? */
1968 if (con
->out_skip
) {
1969 ret
= write_partial_skip(con
);
1973 if (con
->out_kvec_left
) {
1974 ret
= write_partial_kvec(con
);
1981 if (con
->out_msg_done
) {
1982 ceph_msg_put(con
->out_msg
);
1983 con
->out_msg
= NULL
; /* we're done with this one */
1987 ret
= write_partial_msg_pages(con
);
1989 goto more_kvec
; /* we need to send the footer, too! */
1993 dout("try_write write_partial_msg_pages err %d\n",
2000 if (!test_bit(CONNECTING
, &con
->state
)) {
2001 /* is anything else pending? */
2002 if (!list_empty(&con
->out_queue
)) {
2003 prepare_write_message(con
);
2006 if (con
->in_seq
> con
->in_seq_acked
) {
2007 prepare_write_ack(con
);
2010 if (test_and_clear_bit(KEEPALIVE_PENDING
, &con
->flags
)) {
2011 prepare_write_keepalive(con
);
2016 /* Nothing to do! */
2017 clear_bit(WRITE_PENDING
, &con
->flags
);
2018 dout("try_write nothing else to write.\n");
2021 dout("try_write done on %p ret %d\n", con
, ret
);
2028 * Read what we can from the socket.
2030 static int try_read(struct ceph_connection
*con
)
2037 if (test_bit(STANDBY
, &con
->state
))
2040 dout("try_read start on %p\n", con
);
2043 dout("try_read tag %d in_base_pos %d\n", (int)con
->in_tag
,
2047 * process_connect and process_message drop and re-take
2048 * con->mutex. make sure we handle a racing close or reopen.
2050 if (test_bit(CLOSED
, &con
->state
) ||
2051 test_bit(OPENING
, &con
->state
)) {
2056 if (test_bit(CONNECTING
, &con
->state
)) {
2057 if (!test_bit(NEGOTIATING
, &con
->state
)) {
2058 dout("try_read connecting\n");
2059 ret
= read_partial_banner(con
);
2062 ret
= process_banner(con
);
2066 ret
= read_partial_connect(con
);
2069 ret
= process_connect(con
);
2075 if (con
->in_base_pos
< 0) {
2077 * skipping + discarding content.
2079 * FIXME: there must be a better way to do this!
2081 static char buf
[SKIP_BUF_SIZE
];
2082 int skip
= min((int) sizeof (buf
), -con
->in_base_pos
);
2084 dout("skipping %d / %d bytes\n", skip
, -con
->in_base_pos
);
2085 ret
= ceph_tcp_recvmsg(con
->sock
, buf
, skip
);
2088 con
->in_base_pos
+= ret
;
2089 if (con
->in_base_pos
)
2092 if (con
->in_tag
== CEPH_MSGR_TAG_READY
) {
2096 ret
= ceph_tcp_recvmsg(con
->sock
, &con
->in_tag
, 1);
2099 dout("try_read got tag %d\n", (int)con
->in_tag
);
2100 switch (con
->in_tag
) {
2101 case CEPH_MSGR_TAG_MSG
:
2102 prepare_read_message(con
);
2104 case CEPH_MSGR_TAG_ACK
:
2105 prepare_read_ack(con
);
2107 case CEPH_MSGR_TAG_CLOSE
:
2108 set_bit(CLOSED
, &con
->state
); /* fixme */
2114 if (con
->in_tag
== CEPH_MSGR_TAG_MSG
) {
2115 ret
= read_partial_message(con
);
2119 con
->error_msg
= "bad crc";
2123 con
->error_msg
= "io error";
2128 if (con
->in_tag
== CEPH_MSGR_TAG_READY
)
2130 process_message(con
);
2133 if (con
->in_tag
== CEPH_MSGR_TAG_ACK
) {
2134 ret
= read_partial_ack(con
);
2142 dout("try_read done on %p ret %d\n", con
, ret
);
2146 pr_err("try_read bad con->in_tag = %d\n", (int)con
->in_tag
);
2147 con
->error_msg
= "protocol error, garbage tag";
2154 * Atomically queue work on a connection. Bump @con reference to
2155 * avoid races with connection teardown.
2157 static void queue_con(struct ceph_connection
*con
)
2159 if (!con
->ops
->get(con
)) {
2160 dout("queue_con %p ref count 0\n", con
);
2164 if (!queue_delayed_work(ceph_msgr_wq
, &con
->work
, 0)) {
2165 dout("queue_con %p - already queued\n", con
);
2168 dout("queue_con %p\n", con
);
2173 * Do some work on a connection. Drop a connection ref when we're done.
2175 static void con_work(struct work_struct
*work
)
2177 struct ceph_connection
*con
= container_of(work
, struct ceph_connection
,
2181 mutex_lock(&con
->mutex
);
2183 if (test_and_clear_bit(BACKOFF
, &con
->flags
)) {
2184 dout("con_work %p backing off\n", con
);
2185 if (queue_delayed_work(ceph_msgr_wq
, &con
->work
,
2186 round_jiffies_relative(con
->delay
))) {
2187 dout("con_work %p backoff %lu\n", con
, con
->delay
);
2188 mutex_unlock(&con
->mutex
);
2192 dout("con_work %p FAILED to back off %lu\n", con
,
2197 if (test_bit(STANDBY
, &con
->state
)) {
2198 dout("con_work %p STANDBY\n", con
);
2201 if (test_bit(CLOSED
, &con
->state
)) { /* e.g. if we are replaced */
2202 dout("con_work CLOSED\n");
2203 con_close_socket(con
);
2206 if (test_and_clear_bit(OPENING
, &con
->state
)) {
2207 /* reopen w/ new peer */
2208 dout("con_work OPENING\n");
2209 con_close_socket(con
);
2212 if (test_and_clear_bit(SOCK_CLOSED
, &con
->flags
))
2215 ret
= try_read(con
);
2221 ret
= try_write(con
);
2228 mutex_unlock(&con
->mutex
);
2234 mutex_unlock(&con
->mutex
);
2235 ceph_fault(con
); /* error/fault path */
2241 * Generic error/fault handler. A retry mechanism is used with
2242 * exponential backoff
2244 static void ceph_fault(struct ceph_connection
*con
)
2246 pr_err("%s%lld %s %s\n", ENTITY_NAME(con
->peer_name
),
2247 ceph_pr_addr(&con
->peer_addr
.in_addr
), con
->error_msg
);
2248 dout("fault %p state %lu to peer %s\n",
2249 con
, con
->state
, ceph_pr_addr(&con
->peer_addr
.in_addr
));
2251 if (test_bit(LOSSYTX
, &con
->flags
)) {
2252 dout("fault on LOSSYTX channel\n");
2256 mutex_lock(&con
->mutex
);
2257 if (test_bit(CLOSED
, &con
->state
))
2260 con_close_socket(con
);
2263 BUG_ON(con
->in_msg
->con
!= con
);
2264 con
->in_msg
->con
= NULL
;
2265 ceph_msg_put(con
->in_msg
);
2270 /* Requeue anything that hasn't been acked */
2271 list_splice_init(&con
->out_sent
, &con
->out_queue
);
2273 /* If there are no messages queued or keepalive pending, place
2274 * the connection in a STANDBY state */
2275 if (list_empty(&con
->out_queue
) &&
2276 !test_bit(KEEPALIVE_PENDING
, &con
->flags
)) {
2277 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con
);
2278 clear_bit(WRITE_PENDING
, &con
->flags
);
2279 set_bit(STANDBY
, &con
->state
);
2281 /* retry after a delay. */
2282 if (con
->delay
== 0)
2283 con
->delay
= BASE_DELAY_INTERVAL
;
2284 else if (con
->delay
< MAX_DELAY_INTERVAL
)
2287 if (queue_delayed_work(ceph_msgr_wq
, &con
->work
,
2288 round_jiffies_relative(con
->delay
))) {
2289 dout("fault queued %p delay %lu\n", con
, con
->delay
);
2292 dout("fault failed to queue %p delay %lu, backoff\n",
2295 * In many cases we see a socket state change
2296 * while con_work is running and end up
2297 * queuing (non-delayed) work, such that we
2298 * can't backoff with a delay. Set a flag so
2299 * that when con_work restarts we schedule the
2302 set_bit(BACKOFF
, &con
->flags
);
2307 mutex_unlock(&con
->mutex
);
2310 * in case we faulted due to authentication, invalidate our
2311 * current tickets so that we can get new ones.
2313 if (con
->auth_retry
&& con
->ops
->invalidate_authorizer
) {
2314 dout("calling invalidate_authorizer()\n");
2315 con
->ops
->invalidate_authorizer(con
);
2318 if (con
->ops
->fault
)
2319 con
->ops
->fault(con
);
2325 * initialize a new messenger instance
2327 void ceph_messenger_init(struct ceph_messenger
*msgr
,
2328 struct ceph_entity_addr
*myaddr
,
2329 u32 supported_features
,
2330 u32 required_features
,
2333 msgr
->supported_features
= supported_features
;
2334 msgr
->required_features
= required_features
;
2336 spin_lock_init(&msgr
->global_seq_lock
);
2339 msgr
->inst
.addr
= *myaddr
;
2341 /* select a random nonce */
2342 msgr
->inst
.addr
.type
= 0;
2343 get_random_bytes(&msgr
->inst
.addr
.nonce
, sizeof(msgr
->inst
.addr
.nonce
));
2344 encode_my_addr(msgr
);
2345 msgr
->nocrc
= nocrc
;
2347 dout("%s %p\n", __func__
, msgr
);
2349 EXPORT_SYMBOL(ceph_messenger_init
);
2351 static void clear_standby(struct ceph_connection
*con
)
2353 /* come back from STANDBY? */
2354 if (test_and_clear_bit(STANDBY
, &con
->state
)) {
2355 mutex_lock(&con
->mutex
);
2356 dout("clear_standby %p and ++connect_seq\n", con
);
2358 WARN_ON(test_bit(WRITE_PENDING
, &con
->flags
));
2359 WARN_ON(test_bit(KEEPALIVE_PENDING
, &con
->flags
));
2360 mutex_unlock(&con
->mutex
);
2365 * Queue up an outgoing message on the given connection.
2367 void ceph_con_send(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2369 if (test_bit(CLOSED
, &con
->state
)) {
2370 dout("con_send %p closed, dropping %p\n", con
, msg
);
2376 msg
->hdr
.src
= con
->msgr
->inst
.name
;
2378 BUG_ON(msg
->front
.iov_len
!= le32_to_cpu(msg
->hdr
.front_len
));
2380 msg
->needs_out_seq
= true;
2383 mutex_lock(&con
->mutex
);
2385 BUG_ON(msg
->con
!= NULL
);
2386 msg
->con
= con
->ops
->get(con
);
2387 BUG_ON(msg
->con
== NULL
);
2389 BUG_ON(!list_empty(&msg
->list_head
));
2390 list_add_tail(&msg
->list_head
, &con
->out_queue
);
2391 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg
,
2392 ENTITY_NAME(con
->peer_name
), le16_to_cpu(msg
->hdr
.type
),
2393 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
2394 le32_to_cpu(msg
->hdr
.front_len
),
2395 le32_to_cpu(msg
->hdr
.middle_len
),
2396 le32_to_cpu(msg
->hdr
.data_len
));
2397 mutex_unlock(&con
->mutex
);
2399 /* if there wasn't anything waiting to send before, queue
2402 if (test_and_set_bit(WRITE_PENDING
, &con
->flags
) == 0)
2405 EXPORT_SYMBOL(ceph_con_send
);
2408 * Revoke a message that was previously queued for send
2410 void ceph_msg_revoke(struct ceph_msg
*msg
)
2412 struct ceph_connection
*con
= msg
->con
;
2415 return; /* Message not in our possession */
2417 mutex_lock(&con
->mutex
);
2418 if (!list_empty(&msg
->list_head
)) {
2419 dout("%s %p msg %p - was on queue\n", __func__
, con
, msg
);
2420 list_del_init(&msg
->list_head
);
2421 BUG_ON(msg
->con
== NULL
);
2422 msg
->con
->ops
->put(msg
->con
);
2428 if (con
->out_msg
== msg
) {
2429 dout("%s %p msg %p - was sending\n", __func__
, con
, msg
);
2430 con
->out_msg
= NULL
;
2431 if (con
->out_kvec_is_msg
) {
2432 con
->out_skip
= con
->out_kvec_bytes
;
2433 con
->out_kvec_is_msg
= false;
2439 mutex_unlock(&con
->mutex
);
2443 * Revoke a message that we may be reading data into
2445 void ceph_msg_revoke_incoming(struct ceph_msg
*msg
)
2447 struct ceph_connection
*con
;
2449 BUG_ON(msg
== NULL
);
2451 dout("%s msg %p null con\n", __func__
, msg
);
2453 return; /* Message not in our possession */
2457 mutex_lock(&con
->mutex
);
2458 if (con
->in_msg
== msg
) {
2459 unsigned int front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
2460 unsigned int middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
2461 unsigned int data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
2463 /* skip rest of message */
2464 dout("%s %p msg %p revoked\n", __func__
, con
, msg
);
2465 con
->in_base_pos
= con
->in_base_pos
-
2466 sizeof(struct ceph_msg_header
) -
2470 sizeof(struct ceph_msg_footer
);
2471 ceph_msg_put(con
->in_msg
);
2473 con
->in_tag
= CEPH_MSGR_TAG_READY
;
2476 dout("%s %p in_msg %p msg %p no-op\n",
2477 __func__
, con
, con
->in_msg
, msg
);
2479 mutex_unlock(&con
->mutex
);
2483 * Queue a keepalive byte to ensure the tcp connection is alive.
2485 void ceph_con_keepalive(struct ceph_connection
*con
)
2487 dout("con_keepalive %p\n", con
);
2489 if (test_and_set_bit(KEEPALIVE_PENDING
, &con
->flags
) == 0 &&
2490 test_and_set_bit(WRITE_PENDING
, &con
->flags
) == 0)
2493 EXPORT_SYMBOL(ceph_con_keepalive
);
2497 * construct a new message with given type, size
2498 * the new msg has a ref count of 1.
2500 struct ceph_msg
*ceph_msg_new(int type
, int front_len
, gfp_t flags
,
2505 m
= kmalloc(sizeof(*m
), flags
);
2508 kref_init(&m
->kref
);
2511 INIT_LIST_HEAD(&m
->list_head
);
2514 m
->hdr
.type
= cpu_to_le16(type
);
2515 m
->hdr
.priority
= cpu_to_le16(CEPH_MSG_PRIO_DEFAULT
);
2517 m
->hdr
.front_len
= cpu_to_le32(front_len
);
2518 m
->hdr
.middle_len
= 0;
2519 m
->hdr
.data_len
= 0;
2520 m
->hdr
.data_off
= 0;
2521 m
->hdr
.reserved
= 0;
2522 m
->footer
.front_crc
= 0;
2523 m
->footer
.middle_crc
= 0;
2524 m
->footer
.data_crc
= 0;
2525 m
->footer
.flags
= 0;
2526 m
->front_max
= front_len
;
2527 m
->front_is_vmalloc
= false;
2528 m
->more_to_follow
= false;
2537 m
->page_alignment
= 0;
2547 if (front_len
> PAGE_CACHE_SIZE
) {
2548 m
->front
.iov_base
= __vmalloc(front_len
, flags
,
2550 m
->front_is_vmalloc
= true;
2552 m
->front
.iov_base
= kmalloc(front_len
, flags
);
2554 if (m
->front
.iov_base
== NULL
) {
2555 dout("ceph_msg_new can't allocate %d bytes\n",
2560 m
->front
.iov_base
= NULL
;
2562 m
->front
.iov_len
= front_len
;
2564 dout("ceph_msg_new %p front %d\n", m
, front_len
);
2571 pr_err("msg_new can't create type %d front %d\n", type
,
2575 dout("msg_new can't create type %d front %d\n", type
,
2580 EXPORT_SYMBOL(ceph_msg_new
);
2583 * Allocate "middle" portion of a message, if it is needed and wasn't
2584 * allocated by alloc_msg. This allows us to read a small fixed-size
2585 * per-type header in the front and then gracefully fail (i.e.,
2586 * propagate the error to the caller based on info in the front) when
2587 * the middle is too large.
2589 static int ceph_alloc_middle(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2591 int type
= le16_to_cpu(msg
->hdr
.type
);
2592 int middle_len
= le32_to_cpu(msg
->hdr
.middle_len
);
2594 dout("alloc_middle %p type %d %s middle_len %d\n", msg
, type
,
2595 ceph_msg_type_name(type
), middle_len
);
2596 BUG_ON(!middle_len
);
2597 BUG_ON(msg
->middle
);
2599 msg
->middle
= ceph_buffer_new(middle_len
, GFP_NOFS
);
2606 * Allocate a message for receiving an incoming message on a
2607 * connection, and save the result in con->in_msg. Uses the
2608 * connection's private alloc_msg op if available.
2610 * Returns true if the message should be skipped, false otherwise.
2611 * If true is returned (skip message), con->in_msg will be NULL.
2612 * If false is returned, con->in_msg will contain a pointer to the
2613 * newly-allocated message, or NULL in case of memory exhaustion.
2615 static bool ceph_con_in_msg_alloc(struct ceph_connection
*con
,
2616 struct ceph_msg_header
*hdr
)
2618 int type
= le16_to_cpu(hdr
->type
);
2619 int front_len
= le32_to_cpu(hdr
->front_len
);
2620 int middle_len
= le32_to_cpu(hdr
->middle_len
);
2623 BUG_ON(con
->in_msg
!= NULL
);
2625 if (con
->ops
->alloc_msg
) {
2628 mutex_unlock(&con
->mutex
);
2629 con
->in_msg
= con
->ops
->alloc_msg(con
, hdr
, &skip
);
2630 mutex_lock(&con
->mutex
);
2632 con
->in_msg
->con
= con
->ops
->get(con
);
2633 BUG_ON(con
->in_msg
->con
== NULL
);
2642 con
->in_msg
= ceph_msg_new(type
, front_len
, GFP_NOFS
, false);
2644 pr_err("unable to allocate msg type %d len %d\n",
2648 con
->in_msg
->con
= con
->ops
->get(con
);
2649 BUG_ON(con
->in_msg
->con
== NULL
);
2650 con
->in_msg
->page_alignment
= le16_to_cpu(hdr
->data_off
);
2652 memcpy(&con
->in_msg
->hdr
, &con
->in_hdr
, sizeof(con
->in_hdr
));
2654 if (middle_len
&& !con
->in_msg
->middle
) {
2655 ret
= ceph_alloc_middle(con
, con
->in_msg
);
2657 ceph_msg_put(con
->in_msg
);
2667 * Free a generically kmalloc'd message.
2669 void ceph_msg_kfree(struct ceph_msg
*m
)
2671 dout("msg_kfree %p\n", m
);
2672 if (m
->front_is_vmalloc
)
2673 vfree(m
->front
.iov_base
);
2675 kfree(m
->front
.iov_base
);
2680 * Drop a msg ref. Destroy as needed.
2682 void ceph_msg_last_put(struct kref
*kref
)
2684 struct ceph_msg
*m
= container_of(kref
, struct ceph_msg
, kref
);
2686 dout("ceph_msg_put last one on %p\n", m
);
2687 WARN_ON(!list_empty(&m
->list_head
));
2689 /* drop middle, data, if any */
2691 ceph_buffer_put(m
->middle
);
2698 ceph_pagelist_release(m
->pagelist
);
2706 ceph_msgpool_put(m
->pool
, m
);
2710 EXPORT_SYMBOL(ceph_msg_last_put
);
2712 void ceph_msg_dump(struct ceph_msg
*msg
)
2714 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg
,
2715 msg
->front_max
, msg
->nr_pages
);
2716 print_hex_dump(KERN_DEBUG
, "header: ",
2717 DUMP_PREFIX_OFFSET
, 16, 1,
2718 &msg
->hdr
, sizeof(msg
->hdr
), true);
2719 print_hex_dump(KERN_DEBUG
, " front: ",
2720 DUMP_PREFIX_OFFSET
, 16, 1,
2721 msg
->front
.iov_base
, msg
->front
.iov_len
, true);
2723 print_hex_dump(KERN_DEBUG
, "middle: ",
2724 DUMP_PREFIX_OFFSET
, 16, 1,
2725 msg
->middle
->vec
.iov_base
,
2726 msg
->middle
->vec
.iov_len
, true);
2727 print_hex_dump(KERN_DEBUG
, "footer: ",
2728 DUMP_PREFIX_OFFSET
, 16, 1,
2729 &msg
->footer
, sizeof(msg
->footer
), true);
2731 EXPORT_SYMBOL(ceph_msg_dump
);