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 */
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_section(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(0, section->iov_base, section->iov_len);
986 static int read_partial_msg_data(struct ceph_connection *con)
988 struct ceph_msg_data_cursor *cursor = &con->in_msg->cursor;
989 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
997 crc = con->in_data_crc;
998 while (cursor->total_resid) {
999 if (!cursor->resid) {
1000 ceph_msg_data_advance(cursor, 0);
1004 page = ceph_msg_data_next(cursor, &page_offset, &length);
1005 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
1008 con->in_data_crc = crc;
1014 crc = ceph_crc32c_page(crc, page, page_offset, ret);
1015 ceph_msg_data_advance(cursor, (size_t)ret);
1018 con->in_data_crc = crc;
1020 return 1; /* must return > 0 to indicate success */
1023 static int read_partial_msg_data_bounce(struct ceph_connection *con)
1025 struct ceph_msg_data_cursor *cursor = &con->in_msg->cursor;
1031 if (unlikely(!con->bounce_page)) {
1032 con->bounce_page = alloc_page(GFP_NOIO);
1033 if (!con->bounce_page) {
1034 pr_err("failed to allocate bounce page\n");
1039 crc = con->in_data_crc;
1040 while (cursor->total_resid) {
1041 if (!cursor->resid) {
1042 ceph_msg_data_advance(cursor, 0);
1046 page = ceph_msg_data_next(cursor, &off, &len);
1047 ret = ceph_tcp_recvpage(con->sock, con->bounce_page, 0, len);
1049 con->in_data_crc = crc;
1053 crc = crc32c(crc, page_address(con->bounce_page), ret);
1054 memcpy_to_page(page, off, page_address(con->bounce_page), ret);
1056 ceph_msg_data_advance(cursor, ret);
1058 con->in_data_crc = crc;
1060 return 1; /* must return > 0 to indicate success */
1064 * read (part of) a message.
1066 static int read_partial_message(struct ceph_connection *con)
1068 struct ceph_msg *m = con->in_msg;
1072 unsigned int front_len, middle_len, data_len;
1073 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
1074 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
1078 dout("read_partial_message con %p msg %p\n", con, m);
1081 size = sizeof(con->v1.in_hdr);
1083 ret = read_partial(con, end, size, &con->v1.in_hdr);
1087 crc = crc32c(0, &con->v1.in_hdr, offsetof(struct ceph_msg_header, crc));
1088 if (cpu_to_le32(crc) != con->v1.in_hdr.crc) {
1089 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
1090 crc, con->v1.in_hdr.crc);
1094 front_len = le32_to_cpu(con->v1.in_hdr.front_len);
1095 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1097 middle_len = le32_to_cpu(con->v1.in_hdr.middle_len);
1098 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
1100 data_len = le32_to_cpu(con->v1.in_hdr.data_len);
1101 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1105 seq = le64_to_cpu(con->v1.in_hdr.seq);
1106 if ((s64)seq - (s64)con->in_seq < 1) {
1107 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
1108 ENTITY_NAME(con->peer_name),
1109 ceph_pr_addr(&con->peer_addr),
1110 seq, con->in_seq + 1);
1111 con->v1.in_base_pos = -front_len - middle_len - data_len -
1113 con->v1.in_tag = CEPH_MSGR_TAG_READY;
1115 } else if ((s64)seq - (s64)con->in_seq > 1) {
1116 pr_err("read_partial_message bad seq %lld expected %lld\n",
1117 seq, con->in_seq + 1);
1118 con->error_msg = "bad message sequence # for incoming message";
1122 /* allocate message? */
1126 dout("got hdr type %d front %d data %d\n", con->v1.in_hdr.type,
1127 front_len, data_len);
1128 ret = ceph_con_in_msg_alloc(con, &con->v1.in_hdr, &skip);
1132 BUG_ON((!con->in_msg) ^ skip);
1134 /* skip this message */
1135 dout("alloc_msg said skip message\n");
1136 con->v1.in_base_pos = -front_len - middle_len -
1137 data_len - sizeof_footer(con);
1138 con->v1.in_tag = CEPH_MSGR_TAG_READY;
1143 BUG_ON(!con->in_msg);
1144 BUG_ON(con->in_msg->con != con);
1146 m->front.iov_len = 0; /* haven't read it yet */
1148 m->middle->vec.iov_len = 0;
1150 /* prepare for data payload, if any */
1153 prepare_message_data(con->in_msg, data_len);
1157 ret = read_partial_message_section(con, &m->front, front_len,
1158 &con->in_front_crc);
1164 ret = read_partial_message_section(con, &m->middle->vec,
1166 &con->in_middle_crc);
1173 if (!m->num_data_items)
1176 if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE))
1177 ret = read_partial_msg_data_bounce(con);
1179 ret = read_partial_msg_data(con);
1185 size = sizeof_footer(con);
1187 ret = read_partial(con, end, size, &m->footer);
1192 m->footer.flags = m->old_footer.flags;
1196 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1197 m, front_len, m->footer.front_crc, middle_len,
1198 m->footer.middle_crc, data_len, m->footer.data_crc);
1201 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1202 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1203 m, con->in_front_crc, m->footer.front_crc);
1206 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1207 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1208 m, con->in_middle_crc, m->footer.middle_crc);
1212 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1213 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1214 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1215 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1219 if (need_sign && con->ops->check_message_signature &&
1220 con->ops->check_message_signature(m)) {
1221 pr_err("read_partial_message %p signature check failed\n", m);
1225 return 1; /* done! */
1228 static int read_keepalive_ack(struct ceph_connection *con)
1230 struct ceph_timespec ceph_ts;
1231 size_t size = sizeof(ceph_ts);
1232 int ret = read_partial(con, size, size, &ceph_ts);
1235 ceph_decode_timespec64(&con->last_keepalive_ack, &ceph_ts);
1236 prepare_read_tag(con);
1241 * Read what we can from the socket.
1243 int ceph_con_v1_try_read(struct ceph_connection *con)
1248 dout("try_read start %p state %d\n", con, con->state);
1249 if (con->state != CEPH_CON_S_V1_BANNER &&
1250 con->state != CEPH_CON_S_V1_CONNECT_MSG &&
1251 con->state != CEPH_CON_S_OPEN)
1256 dout("try_read tag %d in_base_pos %d\n", con->v1.in_tag,
1257 con->v1.in_base_pos);
1259 if (con->state == CEPH_CON_S_V1_BANNER) {
1260 ret = read_partial_banner(con);
1263 ret = process_banner(con);
1267 con->state = CEPH_CON_S_V1_CONNECT_MSG;
1270 * Received banner is good, exchange connection info.
1271 * Do not reset out_kvec, as sending our banner raced
1272 * with receiving peer banner after connect completed.
1274 ret = prepare_write_connect(con);
1277 prepare_read_connect(con);
1279 /* Send connection info before awaiting response */
1283 if (con->state == CEPH_CON_S_V1_CONNECT_MSG) {
1284 ret = read_partial_connect(con);
1287 ret = process_connect(con);
1293 WARN_ON(con->state != CEPH_CON_S_OPEN);
1295 if (con->v1.in_base_pos < 0) {
1297 * skipping + discarding content.
1299 ret = ceph_tcp_recvmsg(con->sock, NULL, -con->v1.in_base_pos);
1302 dout("skipped %d / %d bytes\n", ret, -con->v1.in_base_pos);
1303 con->v1.in_base_pos += ret;
1304 if (con->v1.in_base_pos)
1307 if (con->v1.in_tag == CEPH_MSGR_TAG_READY) {
1311 ret = ceph_tcp_recvmsg(con->sock, &con->v1.in_tag, 1);
1314 dout("try_read got tag %d\n", con->v1.in_tag);
1315 switch (con->v1.in_tag) {
1316 case CEPH_MSGR_TAG_MSG:
1317 prepare_read_message(con);
1319 case CEPH_MSGR_TAG_ACK:
1320 prepare_read_ack(con);
1322 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
1323 prepare_read_keepalive_ack(con);
1325 case CEPH_MSGR_TAG_CLOSE:
1326 ceph_con_close_socket(con);
1327 con->state = CEPH_CON_S_CLOSED;
1333 if (con->v1.in_tag == CEPH_MSGR_TAG_MSG) {
1334 ret = read_partial_message(con);
1338 con->error_msg = "bad crc/signature";
1344 con->error_msg = "io error";
1349 if (con->v1.in_tag == CEPH_MSGR_TAG_READY)
1351 ceph_con_process_message(con);
1352 if (con->state == CEPH_CON_S_OPEN)
1353 prepare_read_tag(con);
1356 if (con->v1.in_tag == CEPH_MSGR_TAG_ACK ||
1357 con->v1.in_tag == CEPH_MSGR_TAG_SEQ) {
1359 * the final handshake seq exchange is semantically
1360 * equivalent to an ACK
1362 ret = read_partial_ack(con);
1368 if (con->v1.in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
1369 ret = read_keepalive_ack(con);
1376 dout("try_read done on %p ret %d\n", con, ret);
1380 pr_err("try_read bad tag %d\n", con->v1.in_tag);
1381 con->error_msg = "protocol error, garbage tag";
1387 * Write something to the socket. Called in a worker thread when the
1388 * socket appears to be writeable and we have something ready to send.
1390 int ceph_con_v1_try_write(struct ceph_connection *con)
1394 dout("try_write start %p state %d\n", con, con->state);
1395 if (con->state != CEPH_CON_S_PREOPEN &&
1396 con->state != CEPH_CON_S_V1_BANNER &&
1397 con->state != CEPH_CON_S_V1_CONNECT_MSG &&
1398 con->state != CEPH_CON_S_OPEN)
1401 /* open the socket first? */
1402 if (con->state == CEPH_CON_S_PREOPEN) {
1404 con->state = CEPH_CON_S_V1_BANNER;
1406 con_out_kvec_reset(con);
1407 prepare_write_banner(con);
1408 prepare_read_banner(con);
1410 BUG_ON(con->in_msg);
1411 con->v1.in_tag = CEPH_MSGR_TAG_READY;
1412 dout("try_write initiating connect on %p new state %d\n",
1414 ret = ceph_tcp_connect(con);
1416 con->error_msg = "connect error";
1422 dout("try_write out_kvec_bytes %d\n", con->v1.out_kvec_bytes);
1425 /* kvec data queued? */
1426 if (con->v1.out_kvec_left) {
1427 ret = write_partial_kvec(con);
1431 if (con->v1.out_skip) {
1432 ret = write_partial_skip(con);
1439 if (con->v1.out_msg_done) {
1440 ceph_msg_put(con->out_msg);
1441 con->out_msg = NULL; /* we're done with this one */
1445 ret = write_partial_message_data(con);
1447 goto more; /* we need to send the footer, too! */
1451 dout("try_write write_partial_message_data err %d\n",
1458 if (con->state == CEPH_CON_S_OPEN) {
1459 if (ceph_con_flag_test_and_clear(con,
1460 CEPH_CON_F_KEEPALIVE_PENDING)) {
1461 prepare_write_keepalive(con);
1464 /* is anything else pending? */
1465 if (!list_empty(&con->out_queue)) {
1466 prepare_write_message(con);
1469 if (con->in_seq > con->in_seq_acked) {
1470 prepare_write_ack(con);
1475 /* Nothing to do! */
1476 ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
1477 dout("try_write nothing else to write.\n");
1480 dout("try_write done on %p ret %d\n", con, ret);
1484 void ceph_con_v1_revoke(struct ceph_connection *con)
1486 struct ceph_msg *msg = con->out_msg;
1488 WARN_ON(con->v1.out_skip);
1490 if (con->v1.out_msg_done) {
1491 con->v1.out_skip += con_out_kvec_skip(con);
1493 WARN_ON(!msg->data_length);
1494 con->v1.out_skip += sizeof_footer(con);
1496 /* data, middle, front */
1497 if (msg->data_length)
1498 con->v1.out_skip += msg->cursor.total_resid;
1500 con->v1.out_skip += con_out_kvec_skip(con);
1501 con->v1.out_skip += con_out_kvec_skip(con);
1503 dout("%s con %p out_kvec_bytes %d out_skip %d\n", __func__, con,
1504 con->v1.out_kvec_bytes, con->v1.out_skip);
1507 void ceph_con_v1_revoke_incoming(struct ceph_connection *con)
1509 unsigned int front_len = le32_to_cpu(con->v1.in_hdr.front_len);
1510 unsigned int middle_len = le32_to_cpu(con->v1.in_hdr.middle_len);
1511 unsigned int data_len = le32_to_cpu(con->v1.in_hdr.data_len);
1513 /* skip rest of message */
1514 con->v1.in_base_pos = con->v1.in_base_pos -
1515 sizeof(struct ceph_msg_header) -
1519 sizeof(struct ceph_msg_footer);
1521 con->v1.in_tag = CEPH_MSGR_TAG_READY;
1524 dout("%s con %p in_base_pos %d\n", __func__, con, con->v1.in_base_pos);
1527 bool ceph_con_v1_opened(struct ceph_connection *con)
1529 return con->v1.connect_seq;
1532 void ceph_con_v1_reset_session(struct ceph_connection *con)
1534 con->v1.connect_seq = 0;
1535 con->v1.peer_global_seq = 0;
1538 void ceph_con_v1_reset_protocol(struct ceph_connection *con)
1540 con->v1.out_skip = 0;