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
, READ
, &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
)
43 struct bio_vec bvec
= {
45 .bv_offset
= page_offset
,
48 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
51 BUG_ON(page_offset
+ length
> PAGE_SIZE
);
52 iov_iter_bvec(&msg
.msg_iter
, READ
, &bvec
, 1, length
);
53 r
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
60 * write something. @more is true if caller will be sending more data
63 static int ceph_tcp_sendmsg(struct socket
*sock
, struct kvec
*iov
,
64 size_t kvlen
, size_t len
, bool more
)
66 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
70 msg
.msg_flags
|= MSG_MORE
;
72 msg
.msg_flags
|= MSG_EOR
; /* superfluous, but what the hell */
74 r
= kernel_sendmsg(sock
, &msg
, iov
, kvlen
, len
);
81 * @more: either or both of MSG_MORE and MSG_SENDPAGE_NOTLAST
83 static int ceph_tcp_sendpage(struct socket
*sock
, struct page
*page
,
84 int offset
, size_t size
, int more
)
86 ssize_t (*sendpage
)(struct socket
*sock
, struct page
*page
,
87 int offset
, size_t size
, int flags
);
88 int flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
| more
;
92 * sendpage cannot properly handle pages with page_count == 0,
93 * we need to fall back to sendmsg if that's the case.
95 * Same goes for slab pages: skb_can_coalesce() allows
96 * coalescing neighboring slab objects into a single frag which
97 * triggers one of hardened usercopy checks.
99 if (sendpage_ok(page
))
100 sendpage
= sock
->ops
->sendpage
;
102 sendpage
= sock_no_sendpage
;
104 ret
= sendpage(sock
, page
, offset
, size
, flags
);
111 static void con_out_kvec_reset(struct ceph_connection
*con
)
113 BUG_ON(con
->v1
.out_skip
);
115 con
->v1
.out_kvec_left
= 0;
116 con
->v1
.out_kvec_bytes
= 0;
117 con
->v1
.out_kvec_cur
= &con
->v1
.out_kvec
[0];
120 static void con_out_kvec_add(struct ceph_connection
*con
,
121 size_t size
, void *data
)
123 int index
= con
->v1
.out_kvec_left
;
125 BUG_ON(con
->v1
.out_skip
);
126 BUG_ON(index
>= ARRAY_SIZE(con
->v1
.out_kvec
));
128 con
->v1
.out_kvec
[index
].iov_len
= size
;
129 con
->v1
.out_kvec
[index
].iov_base
= data
;
130 con
->v1
.out_kvec_left
++;
131 con
->v1
.out_kvec_bytes
+= size
;
135 * Chop off a kvec from the end. Return residual number of bytes for
136 * that kvec, i.e. how many bytes would have been written if the kvec
139 static int con_out_kvec_skip(struct ceph_connection
*con
)
143 if (con
->v1
.out_kvec_bytes
> 0) {
144 skip
= con
->v1
.out_kvec_cur
[con
->v1
.out_kvec_left
- 1].iov_len
;
145 BUG_ON(con
->v1
.out_kvec_bytes
< skip
);
146 BUG_ON(!con
->v1
.out_kvec_left
);
147 con
->v1
.out_kvec_bytes
-= skip
;
148 con
->v1
.out_kvec_left
--;
154 static size_t sizeof_footer(struct ceph_connection
*con
)
156 return (con
->peer_features
& CEPH_FEATURE_MSG_AUTH
) ?
157 sizeof(struct ceph_msg_footer
) :
158 sizeof(struct ceph_msg_footer_old
);
161 static void prepare_message_data(struct ceph_msg
*msg
, u32 data_len
)
163 /* Initialize data cursor */
165 ceph_msg_data_cursor_init(&msg
->cursor
, msg
, data_len
);
169 * Prepare footer for currently outgoing message, and finish things
170 * off. Assumes out_kvec* are already valid.. we just add on to the end.
172 static void prepare_write_message_footer(struct ceph_connection
*con
)
174 struct ceph_msg
*m
= con
->out_msg
;
176 m
->footer
.flags
|= CEPH_MSG_FOOTER_COMPLETE
;
178 dout("prepare_write_message_footer %p\n", con
);
179 con_out_kvec_add(con
, sizeof_footer(con
), &m
->footer
);
180 if (con
->peer_features
& CEPH_FEATURE_MSG_AUTH
) {
181 if (con
->ops
->sign_message
)
182 con
->ops
->sign_message(m
);
186 m
->old_footer
.flags
= m
->footer
.flags
;
188 con
->v1
.out_more
= m
->more_to_follow
;
189 con
->v1
.out_msg_done
= true;
193 * Prepare headers for the next outgoing message.
195 static void prepare_write_message(struct ceph_connection
*con
)
200 con_out_kvec_reset(con
);
201 con
->v1
.out_msg_done
= false;
203 /* Sneak an ack in there first? If we can get it into the same
204 * TCP packet that's a good thing. */
205 if (con
->in_seq
> con
->in_seq_acked
) {
206 con
->in_seq_acked
= con
->in_seq
;
207 con_out_kvec_add(con
, sizeof (tag_ack
), &tag_ack
);
208 con
->v1
.out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
209 con_out_kvec_add(con
, sizeof(con
->v1
.out_temp_ack
),
210 &con
->v1
.out_temp_ack
);
213 ceph_con_get_out_msg(con
);
216 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
217 m
, con
->out_seq
, le16_to_cpu(m
->hdr
.type
),
218 le32_to_cpu(m
->hdr
.front_len
), le32_to_cpu(m
->hdr
.middle_len
),
220 WARN_ON(m
->front
.iov_len
!= le32_to_cpu(m
->hdr
.front_len
));
221 WARN_ON(m
->data_length
!= le32_to_cpu(m
->hdr
.data_len
));
223 /* tag + hdr + front + middle */
224 con_out_kvec_add(con
, sizeof (tag_msg
), &tag_msg
);
225 con_out_kvec_add(con
, sizeof(con
->v1
.out_hdr
), &con
->v1
.out_hdr
);
226 con_out_kvec_add(con
, m
->front
.iov_len
, m
->front
.iov_base
);
229 con_out_kvec_add(con
, m
->middle
->vec
.iov_len
,
230 m
->middle
->vec
.iov_base
);
232 /* fill in hdr crc and finalize hdr */
233 crc
= crc32c(0, &m
->hdr
, offsetof(struct ceph_msg_header
, crc
));
234 con
->out_msg
->hdr
.crc
= cpu_to_le32(crc
);
235 memcpy(&con
->v1
.out_hdr
, &con
->out_msg
->hdr
, sizeof(con
->v1
.out_hdr
));
237 /* fill in front and middle crc, footer */
238 crc
= crc32c(0, m
->front
.iov_base
, m
->front
.iov_len
);
239 con
->out_msg
->footer
.front_crc
= cpu_to_le32(crc
);
241 crc
= crc32c(0, m
->middle
->vec
.iov_base
,
242 m
->middle
->vec
.iov_len
);
243 con
->out_msg
->footer
.middle_crc
= cpu_to_le32(crc
);
245 con
->out_msg
->footer
.middle_crc
= 0;
246 dout("%s front_crc %u middle_crc %u\n", __func__
,
247 le32_to_cpu(con
->out_msg
->footer
.front_crc
),
248 le32_to_cpu(con
->out_msg
->footer
.middle_crc
));
249 con
->out_msg
->footer
.flags
= 0;
251 /* is there a data payload? */
252 con
->out_msg
->footer
.data_crc
= 0;
253 if (m
->data_length
) {
254 prepare_message_data(con
->out_msg
, m
->data_length
);
255 con
->v1
.out_more
= 1; /* data + footer will follow */
257 /* no, queue up footer too and be done */
258 prepare_write_message_footer(con
);
261 ceph_con_flag_set(con
, CEPH_CON_F_WRITE_PENDING
);
267 static void prepare_write_ack(struct ceph_connection
*con
)
269 dout("prepare_write_ack %p %llu -> %llu\n", con
,
270 con
->in_seq_acked
, con
->in_seq
);
271 con
->in_seq_acked
= con
->in_seq
;
273 con_out_kvec_reset(con
);
275 con_out_kvec_add(con
, sizeof (tag_ack
), &tag_ack
);
277 con
->v1
.out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
278 con_out_kvec_add(con
, sizeof(con
->v1
.out_temp_ack
),
279 &con
->v1
.out_temp_ack
);
281 con
->v1
.out_more
= 1; /* more will follow.. eventually.. */
282 ceph_con_flag_set(con
, CEPH_CON_F_WRITE_PENDING
);
286 * Prepare to share the seq during handshake
288 static void prepare_write_seq(struct ceph_connection
*con
)
290 dout("prepare_write_seq %p %llu -> %llu\n", con
,
291 con
->in_seq_acked
, con
->in_seq
);
292 con
->in_seq_acked
= con
->in_seq
;
294 con_out_kvec_reset(con
);
296 con
->v1
.out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
297 con_out_kvec_add(con
, sizeof(con
->v1
.out_temp_ack
),
298 &con
->v1
.out_temp_ack
);
300 ceph_con_flag_set(con
, CEPH_CON_F_WRITE_PENDING
);
304 * Prepare to write keepalive byte.
306 static void prepare_write_keepalive(struct ceph_connection
*con
)
308 dout("prepare_write_keepalive %p\n", con
);
309 con_out_kvec_reset(con
);
310 if (con
->peer_features
& CEPH_FEATURE_MSGR_KEEPALIVE2
) {
311 struct timespec64 now
;
313 ktime_get_real_ts64(&now
);
314 con_out_kvec_add(con
, sizeof(tag_keepalive2
), &tag_keepalive2
);
315 ceph_encode_timespec64(&con
->v1
.out_temp_keepalive2
, &now
);
316 con_out_kvec_add(con
, sizeof(con
->v1
.out_temp_keepalive2
),
317 &con
->v1
.out_temp_keepalive2
);
319 con_out_kvec_add(con
, sizeof(tag_keepalive
), &tag_keepalive
);
321 ceph_con_flag_set(con
, CEPH_CON_F_WRITE_PENDING
);
325 * Connection negotiation.
328 static int get_connect_authorizer(struct ceph_connection
*con
)
330 struct ceph_auth_handshake
*auth
;
333 if (!con
->ops
->get_authorizer
) {
335 con
->v1
.out_connect
.authorizer_protocol
= CEPH_AUTH_UNKNOWN
;
336 con
->v1
.out_connect
.authorizer_len
= 0;
340 auth
= con
->ops
->get_authorizer(con
, &auth_proto
, con
->v1
.auth_retry
);
342 return PTR_ERR(auth
);
345 con
->v1
.out_connect
.authorizer_protocol
= cpu_to_le32(auth_proto
);
346 con
->v1
.out_connect
.authorizer_len
=
347 cpu_to_le32(auth
->authorizer_buf_len
);
352 * We connected to a peer and are saying hello.
354 static void prepare_write_banner(struct ceph_connection
*con
)
356 con_out_kvec_add(con
, strlen(CEPH_BANNER
), CEPH_BANNER
);
357 con_out_kvec_add(con
, sizeof (con
->msgr
->my_enc_addr
),
358 &con
->msgr
->my_enc_addr
);
360 con
->v1
.out_more
= 0;
361 ceph_con_flag_set(con
, CEPH_CON_F_WRITE_PENDING
);
364 static void __prepare_write_connect(struct ceph_connection
*con
)
366 con_out_kvec_add(con
, sizeof(con
->v1
.out_connect
),
367 &con
->v1
.out_connect
);
369 con_out_kvec_add(con
, con
->v1
.auth
->authorizer_buf_len
,
370 con
->v1
.auth
->authorizer_buf
);
372 con
->v1
.out_more
= 0;
373 ceph_con_flag_set(con
, CEPH_CON_F_WRITE_PENDING
);
376 static int prepare_write_connect(struct ceph_connection
*con
)
378 unsigned int global_seq
= ceph_get_global_seq(con
->msgr
, 0);
382 switch (con
->peer_name
.type
) {
383 case CEPH_ENTITY_TYPE_MON
:
384 proto
= CEPH_MONC_PROTOCOL
;
386 case CEPH_ENTITY_TYPE_OSD
:
387 proto
= CEPH_OSDC_PROTOCOL
;
389 case CEPH_ENTITY_TYPE_MDS
:
390 proto
= CEPH_MDSC_PROTOCOL
;
396 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con
,
397 con
->v1
.connect_seq
, global_seq
, proto
);
399 con
->v1
.out_connect
.features
=
400 cpu_to_le64(from_msgr(con
->msgr
)->supported_features
);
401 con
->v1
.out_connect
.host_type
= cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT
);
402 con
->v1
.out_connect
.connect_seq
= cpu_to_le32(con
->v1
.connect_seq
);
403 con
->v1
.out_connect
.global_seq
= cpu_to_le32(global_seq
);
404 con
->v1
.out_connect
.protocol_version
= cpu_to_le32(proto
);
405 con
->v1
.out_connect
.flags
= 0;
407 ret
= get_connect_authorizer(con
);
411 __prepare_write_connect(con
);
416 * write as much of pending kvecs to the socket as we can.
418 * 0 -> socket full, but more to do
421 static int write_partial_kvec(struct ceph_connection
*con
)
425 dout("write_partial_kvec %p %d left\n", con
, con
->v1
.out_kvec_bytes
);
426 while (con
->v1
.out_kvec_bytes
> 0) {
427 ret
= ceph_tcp_sendmsg(con
->sock
, con
->v1
.out_kvec_cur
,
428 con
->v1
.out_kvec_left
,
429 con
->v1
.out_kvec_bytes
,
433 con
->v1
.out_kvec_bytes
-= ret
;
434 if (!con
->v1
.out_kvec_bytes
)
437 /* account for full iov entries consumed */
438 while (ret
>= con
->v1
.out_kvec_cur
->iov_len
) {
439 BUG_ON(!con
->v1
.out_kvec_left
);
440 ret
-= con
->v1
.out_kvec_cur
->iov_len
;
441 con
->v1
.out_kvec_cur
++;
442 con
->v1
.out_kvec_left
--;
444 /* and for a partially-consumed entry */
446 con
->v1
.out_kvec_cur
->iov_len
-= ret
;
447 con
->v1
.out_kvec_cur
->iov_base
+= ret
;
450 con
->v1
.out_kvec_left
= 0;
453 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con
,
454 con
->v1
.out_kvec_bytes
, con
->v1
.out_kvec_left
, ret
);
455 return ret
; /* done! */
459 * Write as much message data payload as we can. If we finish, queue
461 * 1 -> done, footer is now queued in out_kvec[].
462 * 0 -> socket full, but more to do
465 static int write_partial_message_data(struct ceph_connection
*con
)
467 struct ceph_msg
*msg
= con
->out_msg
;
468 struct ceph_msg_data_cursor
*cursor
= &msg
->cursor
;
469 bool do_datacrc
= !ceph_test_opt(from_msgr(con
->msgr
), NOCRC
);
470 int more
= MSG_MORE
| MSG_SENDPAGE_NOTLAST
;
473 dout("%s %p msg %p\n", __func__
, con
, msg
);
475 if (!msg
->num_data_items
)
479 * Iterate through each page that contains data to be
480 * written, and send as much as possible for each.
482 * If we are calculating the data crc (the default), we will
483 * need to map the page. If we have no pages, they have
484 * been revoked, so use the zero page.
486 crc
= do_datacrc
? le32_to_cpu(msg
->footer
.data_crc
) : 0;
487 while (cursor
->total_resid
) {
493 if (!cursor
->resid
) {
494 ceph_msg_data_advance(cursor
, 0);
498 page
= ceph_msg_data_next(cursor
, &page_offset
, &length
, NULL
);
499 if (length
== cursor
->total_resid
)
501 ret
= ceph_tcp_sendpage(con
->sock
, page
, page_offset
, length
,
505 msg
->footer
.data_crc
= cpu_to_le32(crc
);
509 if (do_datacrc
&& cursor
->need_crc
)
510 crc
= ceph_crc32c_page(crc
, page
, page_offset
, length
);
511 ceph_msg_data_advance(cursor
, (size_t)ret
);
514 dout("%s %p msg %p done\n", __func__
, con
, msg
);
516 /* prepare and queue up footer, too */
518 msg
->footer
.data_crc
= cpu_to_le32(crc
);
520 msg
->footer
.flags
|= CEPH_MSG_FOOTER_NOCRC
;
521 con_out_kvec_reset(con
);
522 prepare_write_message_footer(con
);
524 return 1; /* must return > 0 to indicate success */
530 static int write_partial_skip(struct ceph_connection
*con
)
532 int more
= MSG_MORE
| MSG_SENDPAGE_NOTLAST
;
535 dout("%s %p %d left\n", __func__
, con
, con
->v1
.out_skip
);
536 while (con
->v1
.out_skip
> 0) {
537 size_t size
= min(con
->v1
.out_skip
, (int)PAGE_SIZE
);
539 if (size
== con
->v1
.out_skip
)
541 ret
= ceph_tcp_sendpage(con
->sock
, ceph_zero_page
, 0, size
,
545 con
->v1
.out_skip
-= ret
;
553 * Prepare to read connection handshake, or an ack.
555 static void prepare_read_banner(struct ceph_connection
*con
)
557 dout("prepare_read_banner %p\n", con
);
558 con
->v1
.in_base_pos
= 0;
561 static void prepare_read_connect(struct ceph_connection
*con
)
563 dout("prepare_read_connect %p\n", con
);
564 con
->v1
.in_base_pos
= 0;
567 static void prepare_read_ack(struct ceph_connection
*con
)
569 dout("prepare_read_ack %p\n", con
);
570 con
->v1
.in_base_pos
= 0;
573 static void prepare_read_seq(struct ceph_connection
*con
)
575 dout("prepare_read_seq %p\n", con
);
576 con
->v1
.in_base_pos
= 0;
577 con
->v1
.in_tag
= CEPH_MSGR_TAG_SEQ
;
580 static void prepare_read_tag(struct ceph_connection
*con
)
582 dout("prepare_read_tag %p\n", con
);
583 con
->v1
.in_base_pos
= 0;
584 con
->v1
.in_tag
= CEPH_MSGR_TAG_READY
;
587 static void prepare_read_keepalive_ack(struct ceph_connection
*con
)
589 dout("prepare_read_keepalive_ack %p\n", con
);
590 con
->v1
.in_base_pos
= 0;
594 * Prepare to read a message.
596 static int prepare_read_message(struct ceph_connection
*con
)
598 dout("prepare_read_message %p\n", con
);
599 BUG_ON(con
->in_msg
!= NULL
);
600 con
->v1
.in_base_pos
= 0;
601 con
->in_front_crc
= con
->in_middle_crc
= con
->in_data_crc
= 0;
605 static int read_partial(struct ceph_connection
*con
,
606 int end
, int size
, void *object
)
608 while (con
->v1
.in_base_pos
< end
) {
609 int left
= end
- con
->v1
.in_base_pos
;
610 int have
= size
- left
;
611 int ret
= ceph_tcp_recvmsg(con
->sock
, object
+ have
, left
);
614 con
->v1
.in_base_pos
+= ret
;
620 * Read all or part of the connect-side handshake on a new connection
622 static int read_partial_banner(struct ceph_connection
*con
)
628 dout("read_partial_banner %p at %d\n", con
, con
->v1
.in_base_pos
);
631 size
= strlen(CEPH_BANNER
);
633 ret
= read_partial(con
, end
, size
, con
->v1
.in_banner
);
637 size
= sizeof(con
->v1
.actual_peer_addr
);
639 ret
= read_partial(con
, end
, size
, &con
->v1
.actual_peer_addr
);
642 ceph_decode_banner_addr(&con
->v1
.actual_peer_addr
);
644 size
= sizeof(con
->v1
.peer_addr_for_me
);
646 ret
= read_partial(con
, end
, size
, &con
->v1
.peer_addr_for_me
);
649 ceph_decode_banner_addr(&con
->v1
.peer_addr_for_me
);
655 static int read_partial_connect(struct ceph_connection
*con
)
661 dout("read_partial_connect %p at %d\n", con
, con
->v1
.in_base_pos
);
663 size
= sizeof(con
->v1
.in_reply
);
665 ret
= read_partial(con
, end
, size
, &con
->v1
.in_reply
);
670 size
= le32_to_cpu(con
->v1
.in_reply
.authorizer_len
);
671 if (size
> con
->v1
.auth
->authorizer_reply_buf_len
) {
672 pr_err("authorizer reply too big: %d > %zu\n", size
,
673 con
->v1
.auth
->authorizer_reply_buf_len
);
679 ret
= read_partial(con
, end
, size
,
680 con
->v1
.auth
->authorizer_reply_buf
);
685 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
686 con
, con
->v1
.in_reply
.tag
,
687 le32_to_cpu(con
->v1
.in_reply
.connect_seq
),
688 le32_to_cpu(con
->v1
.in_reply
.global_seq
));
694 * Verify the hello banner looks okay.
696 static int verify_hello(struct ceph_connection
*con
)
698 if (memcmp(con
->v1
.in_banner
, CEPH_BANNER
, strlen(CEPH_BANNER
))) {
699 pr_err("connect to %s got bad banner\n",
700 ceph_pr_addr(&con
->peer_addr
));
701 con
->error_msg
= "protocol error, bad banner";
707 static int process_banner(struct ceph_connection
*con
)
709 struct ceph_entity_addr
*my_addr
= &con
->msgr
->inst
.addr
;
711 dout("process_banner on %p\n", con
);
713 if (verify_hello(con
) < 0)
717 * Make sure the other end is who we wanted. note that the other
718 * end may not yet know their ip address, so if it's 0.0.0.0, give
719 * them the benefit of the doubt.
721 if (memcmp(&con
->peer_addr
, &con
->v1
.actual_peer_addr
,
722 sizeof(con
->peer_addr
)) != 0 &&
723 !(ceph_addr_is_blank(&con
->v1
.actual_peer_addr
) &&
724 con
->v1
.actual_peer_addr
.nonce
== con
->peer_addr
.nonce
)) {
725 pr_warn("wrong peer, want %s/%u, got %s/%u\n",
726 ceph_pr_addr(&con
->peer_addr
),
727 le32_to_cpu(con
->peer_addr
.nonce
),
728 ceph_pr_addr(&con
->v1
.actual_peer_addr
),
729 le32_to_cpu(con
->v1
.actual_peer_addr
.nonce
));
730 con
->error_msg
= "wrong peer at address";
735 * did we learn our address?
737 if (ceph_addr_is_blank(my_addr
)) {
738 memcpy(&my_addr
->in_addr
,
739 &con
->v1
.peer_addr_for_me
.in_addr
,
740 sizeof(con
->v1
.peer_addr_for_me
.in_addr
));
741 ceph_addr_set_port(my_addr
, 0);
742 ceph_encode_my_addr(con
->msgr
);
743 dout("process_banner learned my addr is %s\n",
744 ceph_pr_addr(my_addr
));
750 static int process_connect(struct ceph_connection
*con
)
752 u64 sup_feat
= from_msgr(con
->msgr
)->supported_features
;
753 u64 req_feat
= from_msgr(con
->msgr
)->required_features
;
754 u64 server_feat
= le64_to_cpu(con
->v1
.in_reply
.features
);
757 dout("process_connect on %p tag %d\n", con
, con
->v1
.in_tag
);
760 int len
= le32_to_cpu(con
->v1
.in_reply
.authorizer_len
);
763 * Any connection that defines ->get_authorizer()
764 * should also define ->add_authorizer_challenge() and
765 * ->verify_authorizer_reply().
767 * See get_connect_authorizer().
769 if (con
->v1
.in_reply
.tag
==
770 CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER
) {
771 ret
= con
->ops
->add_authorizer_challenge(
772 con
, con
->v1
.auth
->authorizer_reply_buf
, len
);
776 con_out_kvec_reset(con
);
777 __prepare_write_connect(con
);
778 prepare_read_connect(con
);
783 ret
= con
->ops
->verify_authorizer_reply(con
);
785 con
->error_msg
= "bad authorize reply";
791 switch (con
->v1
.in_reply
.tag
) {
792 case CEPH_MSGR_TAG_FEATURES
:
793 pr_err("%s%lld %s feature set mismatch,"
794 " my %llx < server's %llx, missing %llx\n",
795 ENTITY_NAME(con
->peer_name
),
796 ceph_pr_addr(&con
->peer_addr
),
797 sup_feat
, server_feat
, server_feat
& ~sup_feat
);
798 con
->error_msg
= "missing required protocol features";
801 case CEPH_MSGR_TAG_BADPROTOVER
:
802 pr_err("%s%lld %s protocol version mismatch,"
803 " my %d != server's %d\n",
804 ENTITY_NAME(con
->peer_name
),
805 ceph_pr_addr(&con
->peer_addr
),
806 le32_to_cpu(con
->v1
.out_connect
.protocol_version
),
807 le32_to_cpu(con
->v1
.in_reply
.protocol_version
));
808 con
->error_msg
= "protocol version mismatch";
811 case CEPH_MSGR_TAG_BADAUTHORIZER
:
812 con
->v1
.auth_retry
++;
813 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con
,
815 if (con
->v1
.auth_retry
== 2) {
816 con
->error_msg
= "connect authorization failure";
819 con_out_kvec_reset(con
);
820 ret
= prepare_write_connect(con
);
823 prepare_read_connect(con
);
826 case CEPH_MSGR_TAG_RESETSESSION
:
828 * If we connected with a large connect_seq but the peer
829 * has no record of a session with us (no connection, or
830 * connect_seq == 0), they will send RESETSESION to indicate
831 * that they must have reset their session, and may have
834 dout("process_connect got RESET peer seq %u\n",
835 le32_to_cpu(con
->v1
.in_reply
.connect_seq
));
836 pr_info("%s%lld %s session reset\n",
837 ENTITY_NAME(con
->peer_name
),
838 ceph_pr_addr(&con
->peer_addr
));
839 ceph_con_reset_session(con
);
840 con_out_kvec_reset(con
);
841 ret
= prepare_write_connect(con
);
844 prepare_read_connect(con
);
846 /* Tell ceph about it. */
847 mutex_unlock(&con
->mutex
);
848 if (con
->ops
->peer_reset
)
849 con
->ops
->peer_reset(con
);
850 mutex_lock(&con
->mutex
);
851 if (con
->state
!= CEPH_CON_S_V1_CONNECT_MSG
)
855 case CEPH_MSGR_TAG_RETRY_SESSION
:
857 * If we sent a smaller connect_seq than the peer has, try
858 * again with a larger value.
860 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
861 le32_to_cpu(con
->v1
.out_connect
.connect_seq
),
862 le32_to_cpu(con
->v1
.in_reply
.connect_seq
));
863 con
->v1
.connect_seq
= le32_to_cpu(con
->v1
.in_reply
.connect_seq
);
864 con_out_kvec_reset(con
);
865 ret
= prepare_write_connect(con
);
868 prepare_read_connect(con
);
871 case CEPH_MSGR_TAG_RETRY_GLOBAL
:
873 * If we sent a smaller global_seq than the peer has, try
874 * again with a larger value.
876 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
877 con
->v1
.peer_global_seq
,
878 le32_to_cpu(con
->v1
.in_reply
.global_seq
));
879 ceph_get_global_seq(con
->msgr
,
880 le32_to_cpu(con
->v1
.in_reply
.global_seq
));
881 con_out_kvec_reset(con
);
882 ret
= prepare_write_connect(con
);
885 prepare_read_connect(con
);
888 case CEPH_MSGR_TAG_SEQ
:
889 case CEPH_MSGR_TAG_READY
:
890 if (req_feat
& ~server_feat
) {
891 pr_err("%s%lld %s protocol feature mismatch,"
892 " my required %llx > server's %llx, need %llx\n",
893 ENTITY_NAME(con
->peer_name
),
894 ceph_pr_addr(&con
->peer_addr
),
895 req_feat
, server_feat
, req_feat
& ~server_feat
);
896 con
->error_msg
= "missing required protocol features";
900 WARN_ON(con
->state
!= CEPH_CON_S_V1_CONNECT_MSG
);
901 con
->state
= CEPH_CON_S_OPEN
;
902 con
->v1
.auth_retry
= 0; /* we authenticated; clear flag */
903 con
->v1
.peer_global_seq
=
904 le32_to_cpu(con
->v1
.in_reply
.global_seq
);
905 con
->v1
.connect_seq
++;
906 con
->peer_features
= server_feat
;
907 dout("process_connect got READY gseq %d cseq %d (%d)\n",
908 con
->v1
.peer_global_seq
,
909 le32_to_cpu(con
->v1
.in_reply
.connect_seq
),
910 con
->v1
.connect_seq
);
911 WARN_ON(con
->v1
.connect_seq
!=
912 le32_to_cpu(con
->v1
.in_reply
.connect_seq
));
914 if (con
->v1
.in_reply
.flags
& CEPH_MSG_CONNECT_LOSSY
)
915 ceph_con_flag_set(con
, CEPH_CON_F_LOSSYTX
);
917 con
->delay
= 0; /* reset backoff memory */
919 if (con
->v1
.in_reply
.tag
== CEPH_MSGR_TAG_SEQ
) {
920 prepare_write_seq(con
);
921 prepare_read_seq(con
);
923 prepare_read_tag(con
);
927 case CEPH_MSGR_TAG_WAIT
:
929 * If there is a connection race (we are opening
930 * connections to each other), one of us may just have
931 * to WAIT. This shouldn't happen if we are the
934 con
->error_msg
= "protocol error, got WAIT as client";
938 con
->error_msg
= "protocol error, garbage tag during connect";
945 * read (part of) an ack
947 static int read_partial_ack(struct ceph_connection
*con
)
949 int size
= sizeof(con
->v1
.in_temp_ack
);
952 return read_partial(con
, end
, size
, &con
->v1
.in_temp_ack
);
956 * We can finally discard anything that's been acked.
958 static void process_ack(struct ceph_connection
*con
)
960 u64 ack
= le64_to_cpu(con
->v1
.in_temp_ack
);
962 if (con
->v1
.in_tag
== CEPH_MSGR_TAG_ACK
)
963 ceph_con_discard_sent(con
, ack
);
965 ceph_con_discard_requeued(con
, ack
);
967 prepare_read_tag(con
);
970 static int read_partial_message_section(struct ceph_connection
*con
,
971 struct kvec
*section
,
972 unsigned int sec_len
, u32
*crc
)
978 while (section
->iov_len
< sec_len
) {
979 BUG_ON(section
->iov_base
== NULL
);
980 left
= sec_len
- section
->iov_len
;
981 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)section
->iov_base
+
982 section
->iov_len
, left
);
985 section
->iov_len
+= ret
;
987 if (section
->iov_len
== sec_len
)
988 *crc
= crc32c(0, section
->iov_base
, section
->iov_len
);
993 static int read_partial_msg_data(struct ceph_connection
*con
)
995 struct ceph_msg
*msg
= con
->in_msg
;
996 struct ceph_msg_data_cursor
*cursor
= &msg
->cursor
;
997 bool do_datacrc
= !ceph_test_opt(from_msgr(con
->msgr
), NOCRC
);
1004 if (!msg
->num_data_items
)
1008 crc
= con
->in_data_crc
;
1009 while (cursor
->total_resid
) {
1010 if (!cursor
->resid
) {
1011 ceph_msg_data_advance(cursor
, 0);
1015 page
= ceph_msg_data_next(cursor
, &page_offset
, &length
, NULL
);
1016 ret
= ceph_tcp_recvpage(con
->sock
, page
, page_offset
, length
);
1019 con
->in_data_crc
= crc
;
1025 crc
= ceph_crc32c_page(crc
, page
, page_offset
, ret
);
1026 ceph_msg_data_advance(cursor
, (size_t)ret
);
1029 con
->in_data_crc
= crc
;
1031 return 1; /* must return > 0 to indicate success */
1035 * read (part of) a message.
1037 static int read_partial_message(struct ceph_connection
*con
)
1039 struct ceph_msg
*m
= con
->in_msg
;
1043 unsigned int front_len
, middle_len
, data_len
;
1044 bool do_datacrc
= !ceph_test_opt(from_msgr(con
->msgr
), NOCRC
);
1045 bool need_sign
= (con
->peer_features
& CEPH_FEATURE_MSG_AUTH
);
1049 dout("read_partial_message con %p msg %p\n", con
, m
);
1052 size
= sizeof(con
->v1
.in_hdr
);
1054 ret
= read_partial(con
, end
, size
, &con
->v1
.in_hdr
);
1058 crc
= crc32c(0, &con
->v1
.in_hdr
, offsetof(struct ceph_msg_header
, crc
));
1059 if (cpu_to_le32(crc
) != con
->v1
.in_hdr
.crc
) {
1060 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
1061 crc
, con
->v1
.in_hdr
.crc
);
1065 front_len
= le32_to_cpu(con
->v1
.in_hdr
.front_len
);
1066 if (front_len
> CEPH_MSG_MAX_FRONT_LEN
)
1068 middle_len
= le32_to_cpu(con
->v1
.in_hdr
.middle_len
);
1069 if (middle_len
> CEPH_MSG_MAX_MIDDLE_LEN
)
1071 data_len
= le32_to_cpu(con
->v1
.in_hdr
.data_len
);
1072 if (data_len
> CEPH_MSG_MAX_DATA_LEN
)
1076 seq
= le64_to_cpu(con
->v1
.in_hdr
.seq
);
1077 if ((s64
)seq
- (s64
)con
->in_seq
< 1) {
1078 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
1079 ENTITY_NAME(con
->peer_name
),
1080 ceph_pr_addr(&con
->peer_addr
),
1081 seq
, con
->in_seq
+ 1);
1082 con
->v1
.in_base_pos
= -front_len
- middle_len
- data_len
-
1084 con
->v1
.in_tag
= CEPH_MSGR_TAG_READY
;
1086 } else if ((s64
)seq
- (s64
)con
->in_seq
> 1) {
1087 pr_err("read_partial_message bad seq %lld expected %lld\n",
1088 seq
, con
->in_seq
+ 1);
1089 con
->error_msg
= "bad message sequence # for incoming message";
1093 /* allocate message? */
1097 dout("got hdr type %d front %d data %d\n", con
->v1
.in_hdr
.type
,
1098 front_len
, data_len
);
1099 ret
= ceph_con_in_msg_alloc(con
, &con
->v1
.in_hdr
, &skip
);
1103 BUG_ON(!con
->in_msg
^ skip
);
1105 /* skip this message */
1106 dout("alloc_msg said skip message\n");
1107 con
->v1
.in_base_pos
= -front_len
- middle_len
-
1108 data_len
- sizeof_footer(con
);
1109 con
->v1
.in_tag
= CEPH_MSGR_TAG_READY
;
1114 BUG_ON(!con
->in_msg
);
1115 BUG_ON(con
->in_msg
->con
!= con
);
1117 m
->front
.iov_len
= 0; /* haven't read it yet */
1119 m
->middle
->vec
.iov_len
= 0;
1121 /* prepare for data payload, if any */
1124 prepare_message_data(con
->in_msg
, data_len
);
1128 ret
= read_partial_message_section(con
, &m
->front
, front_len
,
1129 &con
->in_front_crc
);
1135 ret
= read_partial_message_section(con
, &m
->middle
->vec
,
1137 &con
->in_middle_crc
);
1144 ret
= read_partial_msg_data(con
);
1150 size
= sizeof_footer(con
);
1152 ret
= read_partial(con
, end
, size
, &m
->footer
);
1157 m
->footer
.flags
= m
->old_footer
.flags
;
1161 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1162 m
, front_len
, m
->footer
.front_crc
, middle_len
,
1163 m
->footer
.middle_crc
, data_len
, m
->footer
.data_crc
);
1166 if (con
->in_front_crc
!= le32_to_cpu(m
->footer
.front_crc
)) {
1167 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1168 m
, con
->in_front_crc
, m
->footer
.front_crc
);
1171 if (con
->in_middle_crc
!= le32_to_cpu(m
->footer
.middle_crc
)) {
1172 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1173 m
, con
->in_middle_crc
, m
->footer
.middle_crc
);
1177 (m
->footer
.flags
& CEPH_MSG_FOOTER_NOCRC
) == 0 &&
1178 con
->in_data_crc
!= le32_to_cpu(m
->footer
.data_crc
)) {
1179 pr_err("read_partial_message %p data crc %u != exp. %u\n", m
,
1180 con
->in_data_crc
, le32_to_cpu(m
->footer
.data_crc
));
1184 if (need_sign
&& con
->ops
->check_message_signature
&&
1185 con
->ops
->check_message_signature(m
)) {
1186 pr_err("read_partial_message %p signature check failed\n", m
);
1190 return 1; /* done! */
1193 static int read_keepalive_ack(struct ceph_connection
*con
)
1195 struct ceph_timespec ceph_ts
;
1196 size_t size
= sizeof(ceph_ts
);
1197 int ret
= read_partial(con
, size
, size
, &ceph_ts
);
1200 ceph_decode_timespec64(&con
->last_keepalive_ack
, &ceph_ts
);
1201 prepare_read_tag(con
);
1206 * Read what we can from the socket.
1208 int ceph_con_v1_try_read(struct ceph_connection
*con
)
1213 dout("try_read start %p state %d\n", con
, con
->state
);
1214 if (con
->state
!= CEPH_CON_S_V1_BANNER
&&
1215 con
->state
!= CEPH_CON_S_V1_CONNECT_MSG
&&
1216 con
->state
!= CEPH_CON_S_OPEN
)
1221 dout("try_read tag %d in_base_pos %d\n", con
->v1
.in_tag
,
1222 con
->v1
.in_base_pos
);
1224 if (con
->state
== CEPH_CON_S_V1_BANNER
) {
1225 ret
= read_partial_banner(con
);
1228 ret
= process_banner(con
);
1232 con
->state
= CEPH_CON_S_V1_CONNECT_MSG
;
1235 * Received banner is good, exchange connection info.
1236 * Do not reset out_kvec, as sending our banner raced
1237 * with receiving peer banner after connect completed.
1239 ret
= prepare_write_connect(con
);
1242 prepare_read_connect(con
);
1244 /* Send connection info before awaiting response */
1248 if (con
->state
== CEPH_CON_S_V1_CONNECT_MSG
) {
1249 ret
= read_partial_connect(con
);
1252 ret
= process_connect(con
);
1258 WARN_ON(con
->state
!= CEPH_CON_S_OPEN
);
1260 if (con
->v1
.in_base_pos
< 0) {
1262 * skipping + discarding content.
1264 ret
= ceph_tcp_recvmsg(con
->sock
, NULL
, -con
->v1
.in_base_pos
);
1267 dout("skipped %d / %d bytes\n", ret
, -con
->v1
.in_base_pos
);
1268 con
->v1
.in_base_pos
+= ret
;
1269 if (con
->v1
.in_base_pos
)
1272 if (con
->v1
.in_tag
== CEPH_MSGR_TAG_READY
) {
1276 ret
= ceph_tcp_recvmsg(con
->sock
, &con
->v1
.in_tag
, 1);
1279 dout("try_read got tag %d\n", con
->v1
.in_tag
);
1280 switch (con
->v1
.in_tag
) {
1281 case CEPH_MSGR_TAG_MSG
:
1282 prepare_read_message(con
);
1284 case CEPH_MSGR_TAG_ACK
:
1285 prepare_read_ack(con
);
1287 case CEPH_MSGR_TAG_KEEPALIVE2_ACK
:
1288 prepare_read_keepalive_ack(con
);
1290 case CEPH_MSGR_TAG_CLOSE
:
1291 ceph_con_close_socket(con
);
1292 con
->state
= CEPH_CON_S_CLOSED
;
1298 if (con
->v1
.in_tag
== CEPH_MSGR_TAG_MSG
) {
1299 ret
= read_partial_message(con
);
1303 con
->error_msg
= "bad crc/signature";
1309 con
->error_msg
= "io error";
1314 if (con
->v1
.in_tag
== CEPH_MSGR_TAG_READY
)
1316 ceph_con_process_message(con
);
1317 if (con
->state
== CEPH_CON_S_OPEN
)
1318 prepare_read_tag(con
);
1321 if (con
->v1
.in_tag
== CEPH_MSGR_TAG_ACK
||
1322 con
->v1
.in_tag
== CEPH_MSGR_TAG_SEQ
) {
1324 * the final handshake seq exchange is semantically
1325 * equivalent to an ACK
1327 ret
= read_partial_ack(con
);
1333 if (con
->v1
.in_tag
== CEPH_MSGR_TAG_KEEPALIVE2_ACK
) {
1334 ret
= read_keepalive_ack(con
);
1341 dout("try_read done on %p ret %d\n", con
, ret
);
1345 pr_err("try_read bad tag %d\n", con
->v1
.in_tag
);
1346 con
->error_msg
= "protocol error, garbage tag";
1352 * Write something to the socket. Called in a worker thread when the
1353 * socket appears to be writeable and we have something ready to send.
1355 int ceph_con_v1_try_write(struct ceph_connection
*con
)
1359 dout("try_write start %p state %d\n", con
, con
->state
);
1360 if (con
->state
!= CEPH_CON_S_PREOPEN
&&
1361 con
->state
!= CEPH_CON_S_V1_BANNER
&&
1362 con
->state
!= CEPH_CON_S_V1_CONNECT_MSG
&&
1363 con
->state
!= CEPH_CON_S_OPEN
)
1366 /* open the socket first? */
1367 if (con
->state
== CEPH_CON_S_PREOPEN
) {
1369 con
->state
= CEPH_CON_S_V1_BANNER
;
1371 con_out_kvec_reset(con
);
1372 prepare_write_banner(con
);
1373 prepare_read_banner(con
);
1375 BUG_ON(con
->in_msg
);
1376 con
->v1
.in_tag
= CEPH_MSGR_TAG_READY
;
1377 dout("try_write initiating connect on %p new state %d\n",
1379 ret
= ceph_tcp_connect(con
);
1381 con
->error_msg
= "connect error";
1387 dout("try_write out_kvec_bytes %d\n", con
->v1
.out_kvec_bytes
);
1390 /* kvec data queued? */
1391 if (con
->v1
.out_kvec_left
) {
1392 ret
= write_partial_kvec(con
);
1396 if (con
->v1
.out_skip
) {
1397 ret
= write_partial_skip(con
);
1404 if (con
->v1
.out_msg_done
) {
1405 ceph_msg_put(con
->out_msg
);
1406 con
->out_msg
= NULL
; /* we're done with this one */
1410 ret
= write_partial_message_data(con
);
1412 goto more
; /* we need to send the footer, too! */
1416 dout("try_write write_partial_message_data err %d\n",
1423 if (con
->state
== CEPH_CON_S_OPEN
) {
1424 if (ceph_con_flag_test_and_clear(con
,
1425 CEPH_CON_F_KEEPALIVE_PENDING
)) {
1426 prepare_write_keepalive(con
);
1429 /* is anything else pending? */
1430 if (!list_empty(&con
->out_queue
)) {
1431 prepare_write_message(con
);
1434 if (con
->in_seq
> con
->in_seq_acked
) {
1435 prepare_write_ack(con
);
1440 /* Nothing to do! */
1441 ceph_con_flag_clear(con
, CEPH_CON_F_WRITE_PENDING
);
1442 dout("try_write nothing else to write.\n");
1445 dout("try_write done on %p ret %d\n", con
, ret
);
1449 void ceph_con_v1_revoke(struct ceph_connection
*con
)
1451 struct ceph_msg
*msg
= con
->out_msg
;
1453 WARN_ON(con
->v1
.out_skip
);
1455 if (con
->v1
.out_msg_done
) {
1456 con
->v1
.out_skip
+= con_out_kvec_skip(con
);
1458 WARN_ON(!msg
->data_length
);
1459 con
->v1
.out_skip
+= sizeof_footer(con
);
1461 /* data, middle, front */
1462 if (msg
->data_length
)
1463 con
->v1
.out_skip
+= msg
->cursor
.total_resid
;
1465 con
->v1
.out_skip
+= con_out_kvec_skip(con
);
1466 con
->v1
.out_skip
+= con_out_kvec_skip(con
);
1468 dout("%s con %p out_kvec_bytes %d out_skip %d\n", __func__
, con
,
1469 con
->v1
.out_kvec_bytes
, con
->v1
.out_skip
);
1472 void ceph_con_v1_revoke_incoming(struct ceph_connection
*con
)
1474 unsigned int front_len
= le32_to_cpu(con
->v1
.in_hdr
.front_len
);
1475 unsigned int middle_len
= le32_to_cpu(con
->v1
.in_hdr
.middle_len
);
1476 unsigned int data_len
= le32_to_cpu(con
->v1
.in_hdr
.data_len
);
1478 /* skip rest of message */
1479 con
->v1
.in_base_pos
= con
->v1
.in_base_pos
-
1480 sizeof(struct ceph_msg_header
) -
1484 sizeof(struct ceph_msg_footer
);
1486 con
->v1
.in_tag
= CEPH_MSGR_TAG_READY
;
1489 dout("%s con %p in_base_pos %d\n", __func__
, con
, con
->v1
.in_base_pos
);
1492 bool ceph_con_v1_opened(struct ceph_connection
*con
)
1494 return con
->v1
.connect_seq
;
1497 void ceph_con_v1_reset_session(struct ceph_connection
*con
)
1499 con
->v1
.connect_seq
= 0;
1500 con
->v1
.peer_global_seq
= 0;
1503 void ceph_con_v1_reset_protocol(struct ceph_connection
*con
)
1505 con
->v1
.out_skip
= 0;