1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/bvec.h>
5 #include <linux/crc32c.h>
7 #include <linux/socket.h>
10 #include <linux/ceph/ceph_features.h>
11 #include <linux/ceph/decode.h>
12 #include <linux/ceph/libceph.h>
13 #include <linux/ceph/messenger.h>
15 /* static tag bytes (protocol control messages) */
16 static char tag_msg = CEPH_MSGR_TAG_MSG;
17 static char tag_ack = CEPH_MSGR_TAG_ACK;
18 static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
19 static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
22 * If @buf is NULL, discard up to @len bytes.
24 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
26 struct kvec iov = {buf, len};
27 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
31 msg.msg_flags |= MSG_TRUNC;
33 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, len);
34 r = sock_recvmsg(sock, &msg, msg.msg_flags);
40 static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
41 int page_offset, size_t length)
44 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
47 BUG_ON(page_offset + length > PAGE_SIZE);
48 bvec_set_page(&bvec, page, length, page_offset);
49 iov_iter_bvec(&msg.msg_iter, ITER_DEST, &bvec, 1, length);
50 r = sock_recvmsg(sock, &msg, msg.msg_flags);
57 * write something. @more is true if caller will be sending more data
60 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
61 size_t kvlen, size_t len, bool more)
63 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
67 msg.msg_flags |= MSG_MORE;
69 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
71 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
78 * @more: MSG_MORE or 0.
80 static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
81 int offset, size_t size, int more)
84 .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL | more,
90 * MSG_SPLICE_PAGES cannot properly handle pages with page_count == 0,
91 * we need to fall back to sendmsg if that's the case.
93 * Same goes for slab pages: skb_can_coalesce() allows
94 * coalescing neighboring slab objects into a single frag which
95 * triggers one of hardened usercopy checks.
97 if (sendpage_ok(page))
98 msg.msg_flags |= MSG_SPLICE_PAGES;
100 bvec_set_page(&bvec, page, size, offset);
101 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size);
103 ret = sock_sendmsg(sock, &msg);
110 static void con_out_kvec_reset(struct ceph_connection *con)
112 BUG_ON(con->v1.out_skip);
114 con->v1.out_kvec_left = 0;
115 con->v1.out_kvec_bytes = 0;
116 con->v1.out_kvec_cur = &con->v1.out_kvec[0];
119 static void con_out_kvec_add(struct ceph_connection *con,
120 size_t size, void *data)
122 int index = con->v1.out_kvec_left;
124 BUG_ON(con->v1.out_skip);
125 BUG_ON(index >= ARRAY_SIZE(con->v1.out_kvec));
127 con->v1.out_kvec[index].iov_len = size;
128 con->v1.out_kvec[index].iov_base = data;
129 con->v1.out_kvec_left++;
130 con->v1.out_kvec_bytes += size;
134 * Chop off a kvec from the end. Return residual number of bytes for
135 * that kvec, i.e. how many bytes would have been written if the kvec
138 static int con_out_kvec_skip(struct ceph_connection *con)
142 if (con->v1.out_kvec_bytes > 0) {
143 skip = con->v1.out_kvec_cur[con->v1.out_kvec_left - 1].iov_len;
144 BUG_ON(con->v1.out_kvec_bytes < skip);
145 BUG_ON(!con->v1.out_kvec_left);
146 con->v1.out_kvec_bytes -= skip;
147 con->v1.out_kvec_left--;
153 static size_t sizeof_footer(struct ceph_connection *con)
155 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
156 sizeof(struct ceph_msg_footer) :
157 sizeof(struct ceph_msg_footer_old);
160 static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
162 /* Initialize data cursor if it's not a sparse read */
163 if (!msg->sparse_read)
164 ceph_msg_data_cursor_init(&msg->cursor, msg, data_len);
168 * Prepare footer for currently outgoing message, and finish things
169 * off. Assumes out_kvec* are already valid.. we just add on to the end.
171 static void prepare_write_message_footer(struct ceph_connection *con)
173 struct ceph_msg *m = con->out_msg;
175 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
177 dout("prepare_write_message_footer %p\n", con);
178 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
179 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
180 if (con->ops->sign_message)
181 con->ops->sign_message(m);
185 m->old_footer.flags = m->footer.flags;
187 con->v1.out_more = m->more_to_follow;
188 con->v1.out_msg_done = true;
192 * Prepare headers for the next outgoing message.
194 static void prepare_write_message(struct ceph_connection *con)
199 con_out_kvec_reset(con);
200 con->v1.out_msg_done = false;
202 /* Sneak an ack in there first? If we can get it into the same
203 * TCP packet that's a good thing. */
204 if (con->in_seq > con->in_seq_acked) {
205 con->in_seq_acked = con->in_seq;
206 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
207 con->v1.out_temp_ack = cpu_to_le64(con->in_seq_acked);
208 con_out_kvec_add(con, sizeof(con->v1.out_temp_ack),
209 &con->v1.out_temp_ack);
212 ceph_con_get_out_msg(con);
215 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
216 m, con->out_seq, le16_to_cpu(m->hdr.type),
217 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
219 WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
220 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
222 /* tag + hdr + front + middle */
223 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
224 con_out_kvec_add(con, sizeof(con->v1.out_hdr), &con->v1.out_hdr);
225 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
228 con_out_kvec_add(con, m->middle->vec.iov_len,
229 m->middle->vec.iov_base);
231 /* fill in hdr crc and finalize hdr */
232 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
233 con->out_msg->hdr.crc = cpu_to_le32(crc);
234 memcpy(&con->v1.out_hdr, &con->out_msg->hdr, sizeof(con->v1.out_hdr));
236 /* fill in front and middle crc, footer */
237 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
238 con->out_msg->footer.front_crc = cpu_to_le32(crc);
240 crc = crc32c(0, m->middle->vec.iov_base,
241 m->middle->vec.iov_len);
242 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
244 con->out_msg->footer.middle_crc = 0;
245 dout("%s front_crc %u middle_crc %u\n", __func__,
246 le32_to_cpu(con->out_msg->footer.front_crc),
247 le32_to_cpu(con->out_msg->footer.middle_crc));
248 con->out_msg->footer.flags = 0;
250 /* is there a data payload? */
251 con->out_msg->footer.data_crc = 0;
252 if (m->data_length) {
253 prepare_message_data(con->out_msg, m->data_length);
254 con->v1.out_more = 1; /* data + footer will follow */
256 /* no, queue up footer too and be done */
257 prepare_write_message_footer(con);
260 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
266 static void prepare_write_ack(struct ceph_connection *con)
268 dout("prepare_write_ack %p %llu -> %llu\n", con,
269 con->in_seq_acked, con->in_seq);
270 con->in_seq_acked = con->in_seq;
272 con_out_kvec_reset(con);
274 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
276 con->v1.out_temp_ack = cpu_to_le64(con->in_seq_acked);
277 con_out_kvec_add(con, sizeof(con->v1.out_temp_ack),
278 &con->v1.out_temp_ack);
280 con->v1.out_more = 1; /* more will follow.. eventually.. */
281 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
285 * Prepare to share the seq during handshake
287 static void prepare_write_seq(struct ceph_connection *con)
289 dout("prepare_write_seq %p %llu -> %llu\n", con,
290 con->in_seq_acked, con->in_seq);
291 con->in_seq_acked = con->in_seq;
293 con_out_kvec_reset(con);
295 con->v1.out_temp_ack = cpu_to_le64(con->in_seq_acked);
296 con_out_kvec_add(con, sizeof(con->v1.out_temp_ack),
297 &con->v1.out_temp_ack);
299 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
303 * Prepare to write keepalive byte.
305 static void prepare_write_keepalive(struct ceph_connection *con)
307 dout("prepare_write_keepalive %p\n", con);
308 con_out_kvec_reset(con);
309 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
310 struct timespec64 now;
312 ktime_get_real_ts64(&now);
313 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
314 ceph_encode_timespec64(&con->v1.out_temp_keepalive2, &now);
315 con_out_kvec_add(con, sizeof(con->v1.out_temp_keepalive2),
316 &con->v1.out_temp_keepalive2);
318 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
320 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
324 * Connection negotiation.
327 static int get_connect_authorizer(struct ceph_connection *con)
329 struct ceph_auth_handshake *auth;
332 if (!con->ops->get_authorizer) {
334 con->v1.out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
335 con->v1.out_connect.authorizer_len = 0;
339 auth = con->ops->get_authorizer(con, &auth_proto, con->v1.auth_retry);
341 return PTR_ERR(auth);
344 con->v1.out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
345 con->v1.out_connect.authorizer_len =
346 cpu_to_le32(auth->authorizer_buf_len);
351 * We connected to a peer and are saying hello.
353 static void prepare_write_banner(struct ceph_connection *con)
355 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
356 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
357 &con->msgr->my_enc_addr);
359 con->v1.out_more = 0;
360 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
363 static void __prepare_write_connect(struct ceph_connection *con)
365 con_out_kvec_add(con, sizeof(con->v1.out_connect),
366 &con->v1.out_connect);
368 con_out_kvec_add(con, con->v1.auth->authorizer_buf_len,
369 con->v1.auth->authorizer_buf);
371 con->v1.out_more = 0;
372 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
375 static int prepare_write_connect(struct ceph_connection *con)
377 unsigned int global_seq = ceph_get_global_seq(con->msgr, 0);
381 switch (con->peer_name.type) {
382 case CEPH_ENTITY_TYPE_MON:
383 proto = CEPH_MONC_PROTOCOL;
385 case CEPH_ENTITY_TYPE_OSD:
386 proto = CEPH_OSDC_PROTOCOL;
388 case CEPH_ENTITY_TYPE_MDS:
389 proto = CEPH_MDSC_PROTOCOL;
395 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
396 con->v1.connect_seq, global_seq, proto);
398 con->v1.out_connect.features =
399 cpu_to_le64(from_msgr(con->msgr)->supported_features);
400 con->v1.out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
401 con->v1.out_connect.connect_seq = cpu_to_le32(con->v1.connect_seq);
402 con->v1.out_connect.global_seq = cpu_to_le32(global_seq);
403 con->v1.out_connect.protocol_version = cpu_to_le32(proto);
404 con->v1.out_connect.flags = 0;
406 ret = get_connect_authorizer(con);
410 __prepare_write_connect(con);
415 * write as much of pending kvecs to the socket as we can.
417 * 0 -> socket full, but more to do
420 static int write_partial_kvec(struct ceph_connection *con)
424 dout("write_partial_kvec %p %d left\n", con, con->v1.out_kvec_bytes);
425 while (con->v1.out_kvec_bytes > 0) {
426 ret = ceph_tcp_sendmsg(con->sock, con->v1.out_kvec_cur,
427 con->v1.out_kvec_left,
428 con->v1.out_kvec_bytes,
432 con->v1.out_kvec_bytes -= ret;
433 if (!con->v1.out_kvec_bytes)
436 /* account for full iov entries consumed */
437 while (ret >= con->v1.out_kvec_cur->iov_len) {
438 BUG_ON(!con->v1.out_kvec_left);
439 ret -= con->v1.out_kvec_cur->iov_len;
440 con->v1.out_kvec_cur++;
441 con->v1.out_kvec_left--;
443 /* and for a partially-consumed entry */
445 con->v1.out_kvec_cur->iov_len -= ret;
446 con->v1.out_kvec_cur->iov_base += ret;
449 con->v1.out_kvec_left = 0;
452 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
453 con->v1.out_kvec_bytes, con->v1.out_kvec_left, ret);
454 return ret; /* done! */
458 * Write as much message data payload as we can. If we finish, queue
460 * 1 -> done, footer is now queued in out_kvec[].
461 * 0 -> socket full, but more to do
464 static int write_partial_message_data(struct ceph_connection *con)
466 struct ceph_msg *msg = con->out_msg;
467 struct ceph_msg_data_cursor *cursor = &msg->cursor;
468 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
471 dout("%s %p msg %p\n", __func__, con, msg);
473 if (!msg->num_data_items)
477 * Iterate through each page that contains data to be
478 * written, and send as much as possible for each.
480 * If we are calculating the data crc (the default), we will
481 * need to map the page. If we have no pages, they have
482 * been revoked, so use the zero page.
484 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
485 while (cursor->total_resid) {
491 if (!cursor->resid) {
492 ceph_msg_data_advance(cursor, 0);
496 page = ceph_msg_data_next(cursor, &page_offset, &length);
497 ret = ceph_tcp_sendpage(con->sock, page, page_offset, length,
501 msg->footer.data_crc = cpu_to_le32(crc);
505 if (do_datacrc && cursor->need_crc)
506 crc = ceph_crc32c_page(crc, page, page_offset, length);
507 ceph_msg_data_advance(cursor, (size_t)ret);
510 dout("%s %p msg %p done\n", __func__, con, msg);
512 /* prepare and queue up footer, too */
514 msg->footer.data_crc = cpu_to_le32(crc);
516 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
517 con_out_kvec_reset(con);
518 prepare_write_message_footer(con);
520 return 1; /* must return > 0 to indicate success */
526 static int write_partial_skip(struct ceph_connection *con)
530 dout("%s %p %d left\n", __func__, con, con->v1.out_skip);
531 while (con->v1.out_skip > 0) {
532 size_t size = min(con->v1.out_skip, (int)PAGE_SIZE);
534 ret = ceph_tcp_sendpage(con->sock, ceph_zero_page, 0, size,
538 con->v1.out_skip -= ret;
546 * Prepare to read connection handshake, or an ack.
548 static void prepare_read_banner(struct ceph_connection *con)
550 dout("prepare_read_banner %p\n", con);
551 con->v1.in_base_pos = 0;
554 static void prepare_read_connect(struct ceph_connection *con)
556 dout("prepare_read_connect %p\n", con);
557 con->v1.in_base_pos = 0;
560 static void prepare_read_ack(struct ceph_connection *con)
562 dout("prepare_read_ack %p\n", con);
563 con->v1.in_base_pos = 0;
566 static void prepare_read_seq(struct ceph_connection *con)
568 dout("prepare_read_seq %p\n", con);
569 con->v1.in_base_pos = 0;
570 con->v1.in_tag = CEPH_MSGR_TAG_SEQ;
573 static void prepare_read_tag(struct ceph_connection *con)
575 dout("prepare_read_tag %p\n", con);
576 con->v1.in_base_pos = 0;
577 con->v1.in_tag = CEPH_MSGR_TAG_READY;
580 static void prepare_read_keepalive_ack(struct ceph_connection *con)
582 dout("prepare_read_keepalive_ack %p\n", con);
583 con->v1.in_base_pos = 0;
587 * Prepare to read a message.
589 static int prepare_read_message(struct ceph_connection *con)
591 dout("prepare_read_message %p\n", con);
592 BUG_ON(con->in_msg != NULL);
593 con->v1.in_base_pos = 0;
594 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
598 static int read_partial(struct ceph_connection *con,
599 int end, int size, void *object)
601 while (con->v1.in_base_pos < end) {
602 int left = end - con->v1.in_base_pos;
603 int have = size - left;
604 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
607 con->v1.in_base_pos += ret;
613 * Read all or part of the connect-side handshake on a new connection
615 static int read_partial_banner(struct ceph_connection *con)
621 dout("read_partial_banner %p at %d\n", con, con->v1.in_base_pos);
624 size = strlen(CEPH_BANNER);
626 ret = read_partial(con, end, size, con->v1.in_banner);
630 size = sizeof(con->v1.actual_peer_addr);
632 ret = read_partial(con, end, size, &con->v1.actual_peer_addr);
635 ceph_decode_banner_addr(&con->v1.actual_peer_addr);
637 size = sizeof(con->v1.peer_addr_for_me);
639 ret = read_partial(con, end, size, &con->v1.peer_addr_for_me);
642 ceph_decode_banner_addr(&con->v1.peer_addr_for_me);
648 static int read_partial_connect(struct ceph_connection *con)
654 dout("read_partial_connect %p at %d\n", con, con->v1.in_base_pos);
656 size = sizeof(con->v1.in_reply);
658 ret = read_partial(con, end, size, &con->v1.in_reply);
663 size = le32_to_cpu(con->v1.in_reply.authorizer_len);
664 if (size > con->v1.auth->authorizer_reply_buf_len) {
665 pr_err("authorizer reply too big: %d > %zu\n", size,
666 con->v1.auth->authorizer_reply_buf_len);
672 ret = read_partial(con, end, size,
673 con->v1.auth->authorizer_reply_buf);
678 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
679 con, con->v1.in_reply.tag,
680 le32_to_cpu(con->v1.in_reply.connect_seq),
681 le32_to_cpu(con->v1.in_reply.global_seq));
687 * Verify the hello banner looks okay.
689 static int verify_hello(struct ceph_connection *con)
691 if (memcmp(con->v1.in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
692 pr_err("connect to %s got bad banner\n",
693 ceph_pr_addr(&con->peer_addr));
694 con->error_msg = "protocol error, bad banner";
700 static int process_banner(struct ceph_connection *con)
702 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
704 dout("process_banner on %p\n", con);
706 if (verify_hello(con) < 0)
710 * Make sure the other end is who we wanted. note that the other
711 * end may not yet know their ip address, so if it's 0.0.0.0, give
712 * them the benefit of the doubt.
714 if (memcmp(&con->peer_addr, &con->v1.actual_peer_addr,
715 sizeof(con->peer_addr)) != 0 &&
716 !(ceph_addr_is_blank(&con->v1.actual_peer_addr) &&
717 con->v1.actual_peer_addr.nonce == con->peer_addr.nonce)) {
718 pr_warn("wrong peer, want %s/%u, got %s/%u\n",
719 ceph_pr_addr(&con->peer_addr),
720 le32_to_cpu(con->peer_addr.nonce),
721 ceph_pr_addr(&con->v1.actual_peer_addr),
722 le32_to_cpu(con->v1.actual_peer_addr.nonce));
723 con->error_msg = "wrong peer at address";
728 * did we learn our address?
730 if (ceph_addr_is_blank(my_addr)) {
731 memcpy(&my_addr->in_addr,
732 &con->v1.peer_addr_for_me.in_addr,
733 sizeof(con->v1.peer_addr_for_me.in_addr));
734 ceph_addr_set_port(my_addr, 0);
735 ceph_encode_my_addr(con->msgr);
736 dout("process_banner learned my addr is %s\n",
737 ceph_pr_addr(my_addr));
743 static int process_connect(struct ceph_connection *con)
745 u64 sup_feat = from_msgr(con->msgr)->supported_features;
746 u64 req_feat = from_msgr(con->msgr)->required_features;
747 u64 server_feat = le64_to_cpu(con->v1.in_reply.features);
750 dout("process_connect on %p tag %d\n", con, con->v1.in_tag);
753 int len = le32_to_cpu(con->v1.in_reply.authorizer_len);
756 * Any connection that defines ->get_authorizer()
757 * should also define ->add_authorizer_challenge() and
758 * ->verify_authorizer_reply().
760 * See get_connect_authorizer().
762 if (con->v1.in_reply.tag ==
763 CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER) {
764 ret = con->ops->add_authorizer_challenge(
765 con, con->v1.auth->authorizer_reply_buf, len);
769 con_out_kvec_reset(con);
770 __prepare_write_connect(con);
771 prepare_read_connect(con);
776 ret = con->ops->verify_authorizer_reply(con);
778 con->error_msg = "bad authorize reply";
784 switch (con->v1.in_reply.tag) {
785 case CEPH_MSGR_TAG_FEATURES:
786 pr_err("%s%lld %s feature set mismatch,"
787 " my %llx < server's %llx, missing %llx\n",
788 ENTITY_NAME(con->peer_name),
789 ceph_pr_addr(&con->peer_addr),
790 sup_feat, server_feat, server_feat & ~sup_feat);
791 con->error_msg = "missing required protocol features";
794 case CEPH_MSGR_TAG_BADPROTOVER:
795 pr_err("%s%lld %s protocol version mismatch,"
796 " my %d != server's %d\n",
797 ENTITY_NAME(con->peer_name),
798 ceph_pr_addr(&con->peer_addr),
799 le32_to_cpu(con->v1.out_connect.protocol_version),
800 le32_to_cpu(con->v1.in_reply.protocol_version));
801 con->error_msg = "protocol version mismatch";
804 case CEPH_MSGR_TAG_BADAUTHORIZER:
805 con->v1.auth_retry++;
806 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
808 if (con->v1.auth_retry == 2) {
809 con->error_msg = "connect authorization failure";
812 con_out_kvec_reset(con);
813 ret = prepare_write_connect(con);
816 prepare_read_connect(con);
819 case CEPH_MSGR_TAG_RESETSESSION:
821 * If we connected with a large connect_seq but the peer
822 * has no record of a session with us (no connection, or
823 * connect_seq == 0), they will send RESETSESION to indicate
824 * that they must have reset their session, and may have
827 dout("process_connect got RESET peer seq %u\n",
828 le32_to_cpu(con->v1.in_reply.connect_seq));
829 pr_info("%s%lld %s session reset\n",
830 ENTITY_NAME(con->peer_name),
831 ceph_pr_addr(&con->peer_addr));
832 ceph_con_reset_session(con);
833 con_out_kvec_reset(con);
834 ret = prepare_write_connect(con);
837 prepare_read_connect(con);
839 /* Tell ceph about it. */
840 mutex_unlock(&con->mutex);
841 if (con->ops->peer_reset)
842 con->ops->peer_reset(con);
843 mutex_lock(&con->mutex);
844 if (con->state != CEPH_CON_S_V1_CONNECT_MSG)
848 case CEPH_MSGR_TAG_RETRY_SESSION:
850 * If we sent a smaller connect_seq than the peer has, try
851 * again with a larger value.
853 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
854 le32_to_cpu(con->v1.out_connect.connect_seq),
855 le32_to_cpu(con->v1.in_reply.connect_seq));
856 con->v1.connect_seq = le32_to_cpu(con->v1.in_reply.connect_seq);
857 con_out_kvec_reset(con);
858 ret = prepare_write_connect(con);
861 prepare_read_connect(con);
864 case CEPH_MSGR_TAG_RETRY_GLOBAL:
866 * If we sent a smaller global_seq than the peer has, try
867 * again with a larger value.
869 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
870 con->v1.peer_global_seq,
871 le32_to_cpu(con->v1.in_reply.global_seq));
872 ceph_get_global_seq(con->msgr,
873 le32_to_cpu(con->v1.in_reply.global_seq));
874 con_out_kvec_reset(con);
875 ret = prepare_write_connect(con);
878 prepare_read_connect(con);
881 case CEPH_MSGR_TAG_SEQ:
882 case CEPH_MSGR_TAG_READY:
883 if (req_feat & ~server_feat) {
884 pr_err("%s%lld %s protocol feature mismatch,"
885 " my required %llx > server's %llx, need %llx\n",
886 ENTITY_NAME(con->peer_name),
887 ceph_pr_addr(&con->peer_addr),
888 req_feat, server_feat, req_feat & ~server_feat);
889 con->error_msg = "missing required protocol features";
893 WARN_ON(con->state != CEPH_CON_S_V1_CONNECT_MSG);
894 con->state = CEPH_CON_S_OPEN;
895 con->v1.auth_retry = 0; /* we authenticated; clear flag */
896 con->v1.peer_global_seq =
897 le32_to_cpu(con->v1.in_reply.global_seq);
898 con->v1.connect_seq++;
899 con->peer_features = server_feat;
900 dout("process_connect got READY gseq %d cseq %d (%d)\n",
901 con->v1.peer_global_seq,
902 le32_to_cpu(con->v1.in_reply.connect_seq),
903 con->v1.connect_seq);
904 WARN_ON(con->v1.connect_seq !=
905 le32_to_cpu(con->v1.in_reply.connect_seq));
907 if (con->v1.in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
908 ceph_con_flag_set(con, CEPH_CON_F_LOSSYTX);
910 con->delay = 0; /* reset backoff memory */
912 if (con->v1.in_reply.tag == CEPH_MSGR_TAG_SEQ) {
913 prepare_write_seq(con);
914 prepare_read_seq(con);
916 prepare_read_tag(con);
920 case CEPH_MSGR_TAG_WAIT:
922 * If there is a connection race (we are opening
923 * connections to each other), one of us may just have
924 * to WAIT. This shouldn't happen if we are the
927 con->error_msg = "protocol error, got WAIT as client";
931 con->error_msg = "protocol error, garbage tag during connect";
938 * read (part of) an ack
940 static int read_partial_ack(struct ceph_connection *con)
942 int size = sizeof(con->v1.in_temp_ack);
945 return read_partial(con, end, size, &con->v1.in_temp_ack);
949 * We can finally discard anything that's been acked.
951 static void process_ack(struct ceph_connection *con)
953 u64 ack = le64_to_cpu(con->v1.in_temp_ack);
955 if (con->v1.in_tag == CEPH_MSGR_TAG_ACK)
956 ceph_con_discard_sent(con, ack);
958 ceph_con_discard_requeued(con, ack);
960 prepare_read_tag(con);
963 static int read_partial_message_chunk(struct ceph_connection *con,
964 struct kvec *section,
965 unsigned int sec_len, u32 *crc)
971 while (section->iov_len < sec_len) {
972 BUG_ON(section->iov_base == NULL);
973 left = sec_len - section->iov_len;
974 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
975 section->iov_len, left);
978 section->iov_len += ret;
980 if (section->iov_len == sec_len)
981 *crc = crc32c(*crc, section->iov_base, section->iov_len);
986 static inline int read_partial_message_section(struct ceph_connection *con,
987 struct kvec *section,
988 unsigned int sec_len, u32 *crc)
991 return read_partial_message_chunk(con, section, sec_len, crc);
994 static int read_sparse_msg_extent(struct ceph_connection *con, u32 *crc)
996 struct ceph_msg_data_cursor *cursor = &con->in_msg->cursor;
997 bool do_bounce = ceph_test_opt(from_msgr(con->msgr), RXBOUNCE);
999 if (do_bounce && unlikely(!con->bounce_page)) {
1000 con->bounce_page = alloc_page(GFP_NOIO);
1001 if (!con->bounce_page) {
1002 pr_err("failed to allocate bounce page\n");
1007 while (cursor->sr_resid > 0) {
1008 struct page *page, *rpage;
1012 page = ceph_msg_data_next(cursor, &off, &len);
1013 rpage = do_bounce ? con->bounce_page : page;
1015 /* clamp to what remains in extent */
1016 len = min_t(int, len, cursor->sr_resid);
1017 ret = ceph_tcp_recvpage(con->sock, rpage, (int)off, len);
1020 *crc = ceph_crc32c_page(*crc, rpage, off, ret);
1021 ceph_msg_data_advance(cursor, (size_t)ret);
1022 cursor->sr_resid -= ret;
1024 memcpy_page(page, off, rpage, off, ret);
1029 static int read_sparse_msg_data(struct ceph_connection *con)
1031 struct ceph_msg_data_cursor *cursor = &con->in_msg->cursor;
1032 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
1037 crc = con->in_data_crc;
1040 if (con->v1.in_sr_kvec.iov_base)
1041 ret = read_partial_message_chunk(con,
1042 &con->v1.in_sr_kvec,
1045 else if (cursor->sr_resid > 0)
1046 ret = read_sparse_msg_extent(con, &crc);
1050 con->in_data_crc = crc;
1054 memset(&con->v1.in_sr_kvec, 0, sizeof(con->v1.in_sr_kvec));
1055 ret = con->ops->sparse_read(con, cursor,
1056 (char **)&con->v1.in_sr_kvec.iov_base);
1057 con->v1.in_sr_len = ret;
1061 con->in_data_crc = crc;
1063 return ret < 0 ? ret : 1; /* must return > 0 to indicate success */
1066 static int read_partial_msg_data(struct ceph_connection *con)
1068 struct ceph_msg_data_cursor *cursor = &con->in_msg->cursor;
1069 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
1077 crc = con->in_data_crc;
1078 while (cursor->total_resid) {
1079 if (!cursor->resid) {
1080 ceph_msg_data_advance(cursor, 0);
1084 page = ceph_msg_data_next(cursor, &page_offset, &length);
1085 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
1088 con->in_data_crc = crc;
1094 crc = ceph_crc32c_page(crc, page, page_offset, ret);
1095 ceph_msg_data_advance(cursor, (size_t)ret);
1098 con->in_data_crc = crc;
1100 return 1; /* must return > 0 to indicate success */
1103 static int read_partial_msg_data_bounce(struct ceph_connection *con)
1105 struct ceph_msg_data_cursor *cursor = &con->in_msg->cursor;
1111 if (unlikely(!con->bounce_page)) {
1112 con->bounce_page = alloc_page(GFP_NOIO);
1113 if (!con->bounce_page) {
1114 pr_err("failed to allocate bounce page\n");
1119 crc = con->in_data_crc;
1120 while (cursor->total_resid) {
1121 if (!cursor->resid) {
1122 ceph_msg_data_advance(cursor, 0);
1126 page = ceph_msg_data_next(cursor, &off, &len);
1127 ret = ceph_tcp_recvpage(con->sock, con->bounce_page, 0, len);
1129 con->in_data_crc = crc;
1133 crc = crc32c(crc, page_address(con->bounce_page), ret);
1134 memcpy_to_page(page, off, page_address(con->bounce_page), ret);
1136 ceph_msg_data_advance(cursor, ret);
1138 con->in_data_crc = crc;
1140 return 1; /* must return > 0 to indicate success */
1144 * read (part of) a message.
1146 static int read_partial_message(struct ceph_connection *con)
1148 struct ceph_msg *m = con->in_msg;
1152 unsigned int front_len, middle_len, data_len;
1153 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
1154 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
1158 dout("read_partial_message con %p msg %p\n", con, m);
1161 size = sizeof(con->v1.in_hdr);
1163 ret = read_partial(con, end, size, &con->v1.in_hdr);
1167 crc = crc32c(0, &con->v1.in_hdr, offsetof(struct ceph_msg_header, crc));
1168 if (cpu_to_le32(crc) != con->v1.in_hdr.crc) {
1169 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
1170 crc, con->v1.in_hdr.crc);
1174 front_len = le32_to_cpu(con->v1.in_hdr.front_len);
1175 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1177 middle_len = le32_to_cpu(con->v1.in_hdr.middle_len);
1178 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
1180 data_len = le32_to_cpu(con->v1.in_hdr.data_len);
1181 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1185 seq = le64_to_cpu(con->v1.in_hdr.seq);
1186 if ((s64)seq - (s64)con->in_seq < 1) {
1187 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
1188 ENTITY_NAME(con->peer_name),
1189 ceph_pr_addr(&con->peer_addr),
1190 seq, con->in_seq + 1);
1191 con->v1.in_base_pos = -front_len - middle_len - data_len -
1193 con->v1.in_tag = CEPH_MSGR_TAG_READY;
1195 } else if ((s64)seq - (s64)con->in_seq > 1) {
1196 pr_err("read_partial_message bad seq %lld expected %lld\n",
1197 seq, con->in_seq + 1);
1198 con->error_msg = "bad message sequence # for incoming message";
1202 /* allocate message? */
1206 dout("got hdr type %d front %d data %d\n", con->v1.in_hdr.type,
1207 front_len, data_len);
1208 ret = ceph_con_in_msg_alloc(con, &con->v1.in_hdr, &skip);
1212 BUG_ON((!con->in_msg) ^ skip);
1214 /* skip this message */
1215 dout("alloc_msg said skip message\n");
1216 con->v1.in_base_pos = -front_len - middle_len -
1217 data_len - sizeof_footer(con);
1218 con->v1.in_tag = CEPH_MSGR_TAG_READY;
1223 BUG_ON(!con->in_msg);
1224 BUG_ON(con->in_msg->con != con);
1226 m->front.iov_len = 0; /* haven't read it yet */
1228 m->middle->vec.iov_len = 0;
1230 /* prepare for data payload, if any */
1233 prepare_message_data(con->in_msg, data_len);
1237 ret = read_partial_message_section(con, &m->front, front_len,
1238 &con->in_front_crc);
1244 ret = read_partial_message_section(con, &m->middle->vec,
1246 &con->in_middle_crc);
1253 if (!m->num_data_items)
1257 ret = read_sparse_msg_data(con);
1258 else if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE))
1259 ret = read_partial_msg_data_bounce(con);
1261 ret = read_partial_msg_data(con);
1267 size = sizeof_footer(con);
1269 ret = read_partial(con, end, size, &m->footer);
1274 m->footer.flags = m->old_footer.flags;
1278 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1279 m, front_len, m->footer.front_crc, middle_len,
1280 m->footer.middle_crc, data_len, m->footer.data_crc);
1283 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1284 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1285 m, con->in_front_crc, m->footer.front_crc);
1288 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1289 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1290 m, con->in_middle_crc, m->footer.middle_crc);
1294 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1295 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1296 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1297 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1301 if (need_sign && con->ops->check_message_signature &&
1302 con->ops->check_message_signature(m)) {
1303 pr_err("read_partial_message %p signature check failed\n", m);
1307 return 1; /* done! */
1310 static int read_keepalive_ack(struct ceph_connection *con)
1312 struct ceph_timespec ceph_ts;
1313 size_t size = sizeof(ceph_ts);
1314 int ret = read_partial(con, size, size, &ceph_ts);
1317 ceph_decode_timespec64(&con->last_keepalive_ack, &ceph_ts);
1318 prepare_read_tag(con);
1323 * Read what we can from the socket.
1325 int ceph_con_v1_try_read(struct ceph_connection *con)
1330 dout("try_read start %p state %d\n", con, con->state);
1331 if (con->state != CEPH_CON_S_V1_BANNER &&
1332 con->state != CEPH_CON_S_V1_CONNECT_MSG &&
1333 con->state != CEPH_CON_S_OPEN)
1338 dout("try_read tag %d in_base_pos %d\n", con->v1.in_tag,
1339 con->v1.in_base_pos);
1341 if (con->state == CEPH_CON_S_V1_BANNER) {
1342 ret = read_partial_banner(con);
1345 ret = process_banner(con);
1349 con->state = CEPH_CON_S_V1_CONNECT_MSG;
1352 * Received banner is good, exchange connection info.
1353 * Do not reset out_kvec, as sending our banner raced
1354 * with receiving peer banner after connect completed.
1356 ret = prepare_write_connect(con);
1359 prepare_read_connect(con);
1361 /* Send connection info before awaiting response */
1365 if (con->state == CEPH_CON_S_V1_CONNECT_MSG) {
1366 ret = read_partial_connect(con);
1369 ret = process_connect(con);
1375 WARN_ON(con->state != CEPH_CON_S_OPEN);
1377 if (con->v1.in_base_pos < 0) {
1379 * skipping + discarding content.
1381 ret = ceph_tcp_recvmsg(con->sock, NULL, -con->v1.in_base_pos);
1384 dout("skipped %d / %d bytes\n", ret, -con->v1.in_base_pos);
1385 con->v1.in_base_pos += ret;
1386 if (con->v1.in_base_pos)
1389 if (con->v1.in_tag == CEPH_MSGR_TAG_READY) {
1393 ret = ceph_tcp_recvmsg(con->sock, &con->v1.in_tag, 1);
1396 dout("try_read got tag %d\n", con->v1.in_tag);
1397 switch (con->v1.in_tag) {
1398 case CEPH_MSGR_TAG_MSG:
1399 prepare_read_message(con);
1401 case CEPH_MSGR_TAG_ACK:
1402 prepare_read_ack(con);
1404 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
1405 prepare_read_keepalive_ack(con);
1407 case CEPH_MSGR_TAG_CLOSE:
1408 ceph_con_close_socket(con);
1409 con->state = CEPH_CON_S_CLOSED;
1415 if (con->v1.in_tag == CEPH_MSGR_TAG_MSG) {
1416 ret = read_partial_message(con);
1420 con->error_msg = "bad crc/signature";
1426 con->error_msg = "io error";
1431 if (con->v1.in_tag == CEPH_MSGR_TAG_READY)
1433 ceph_con_process_message(con);
1434 if (con->state == CEPH_CON_S_OPEN)
1435 prepare_read_tag(con);
1438 if (con->v1.in_tag == CEPH_MSGR_TAG_ACK ||
1439 con->v1.in_tag == CEPH_MSGR_TAG_SEQ) {
1441 * the final handshake seq exchange is semantically
1442 * equivalent to an ACK
1444 ret = read_partial_ack(con);
1450 if (con->v1.in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
1451 ret = read_keepalive_ack(con);
1458 dout("try_read done on %p ret %d\n", con, ret);
1462 pr_err("try_read bad tag %d\n", con->v1.in_tag);
1463 con->error_msg = "protocol error, garbage tag";
1469 * Write something to the socket. Called in a worker thread when the
1470 * socket appears to be writeable and we have something ready to send.
1472 int ceph_con_v1_try_write(struct ceph_connection *con)
1476 dout("try_write start %p state %d\n", con, con->state);
1477 if (con->state != CEPH_CON_S_PREOPEN &&
1478 con->state != CEPH_CON_S_V1_BANNER &&
1479 con->state != CEPH_CON_S_V1_CONNECT_MSG &&
1480 con->state != CEPH_CON_S_OPEN)
1483 /* open the socket first? */
1484 if (con->state == CEPH_CON_S_PREOPEN) {
1486 con->state = CEPH_CON_S_V1_BANNER;
1488 con_out_kvec_reset(con);
1489 prepare_write_banner(con);
1490 prepare_read_banner(con);
1492 BUG_ON(con->in_msg);
1493 con->v1.in_tag = CEPH_MSGR_TAG_READY;
1494 dout("try_write initiating connect on %p new state %d\n",
1496 ret = ceph_tcp_connect(con);
1498 con->error_msg = "connect error";
1504 dout("try_write out_kvec_bytes %d\n", con->v1.out_kvec_bytes);
1507 /* kvec data queued? */
1508 if (con->v1.out_kvec_left) {
1509 ret = write_partial_kvec(con);
1513 if (con->v1.out_skip) {
1514 ret = write_partial_skip(con);
1521 if (con->v1.out_msg_done) {
1522 ceph_msg_put(con->out_msg);
1523 con->out_msg = NULL; /* we're done with this one */
1527 ret = write_partial_message_data(con);
1529 goto more; /* we need to send the footer, too! */
1533 dout("try_write write_partial_message_data err %d\n",
1540 if (con->state == CEPH_CON_S_OPEN) {
1541 if (ceph_con_flag_test_and_clear(con,
1542 CEPH_CON_F_KEEPALIVE_PENDING)) {
1543 prepare_write_keepalive(con);
1546 /* is anything else pending? */
1547 if (!list_empty(&con->out_queue)) {
1548 prepare_write_message(con);
1551 if (con->in_seq > con->in_seq_acked) {
1552 prepare_write_ack(con);
1557 /* Nothing to do! */
1558 ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
1559 dout("try_write nothing else to write.\n");
1562 dout("try_write done on %p ret %d\n", con, ret);
1566 void ceph_con_v1_revoke(struct ceph_connection *con)
1568 struct ceph_msg *msg = con->out_msg;
1570 WARN_ON(con->v1.out_skip);
1572 if (con->v1.out_msg_done) {
1573 con->v1.out_skip += con_out_kvec_skip(con);
1575 WARN_ON(!msg->data_length);
1576 con->v1.out_skip += sizeof_footer(con);
1578 /* data, middle, front */
1579 if (msg->data_length)
1580 con->v1.out_skip += msg->cursor.total_resid;
1582 con->v1.out_skip += con_out_kvec_skip(con);
1583 con->v1.out_skip += con_out_kvec_skip(con);
1585 dout("%s con %p out_kvec_bytes %d out_skip %d\n", __func__, con,
1586 con->v1.out_kvec_bytes, con->v1.out_skip);
1589 void ceph_con_v1_revoke_incoming(struct ceph_connection *con)
1591 unsigned int front_len = le32_to_cpu(con->v1.in_hdr.front_len);
1592 unsigned int middle_len = le32_to_cpu(con->v1.in_hdr.middle_len);
1593 unsigned int data_len = le32_to_cpu(con->v1.in_hdr.data_len);
1595 /* skip rest of message */
1596 con->v1.in_base_pos = con->v1.in_base_pos -
1597 sizeof(struct ceph_msg_header) -
1601 sizeof(struct ceph_msg_footer);
1603 con->v1.in_tag = CEPH_MSGR_TAG_READY;
1606 dout("%s con %p in_base_pos %d\n", __func__, con, con->v1.in_base_pos);
1609 bool ceph_con_v1_opened(struct ceph_connection *con)
1611 return con->v1.connect_seq;
1614 void ceph_con_v1_reset_session(struct ceph_connection *con)
1616 con->v1.connect_seq = 0;
1617 con->v1.peer_global_seq = 0;
1620 void ceph_con_v1_reset_protocol(struct ceph_connection *con)
1622 con->v1.out_skip = 0;