BUG_ON(page_offset + length > PAGE_SIZE);
bvec_set_page(&bvec, page, length, page_offset);
iov_iter_bvec(&msg.msg_iter, ITER_DEST, &bvec, 1, length);
r = sock_recvmsg(sock, &msg, msg.msg_flags); if (r == -EAGAIN)
r = 0; return r;
}
/* * write something. @more is true if caller will be sending more data * shortly.
*/ staticint ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
size_t kvlen, size_t len, bool more)
{ struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; int r;
if (more)
msg.msg_flags |= MSG_MORE; else
msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
r = kernel_sendmsg(sock, &msg, iov, kvlen, len); if (r == -EAGAIN)
r = 0; return r;
}
/* * @more: MSG_MORE or 0.
*/ staticint ceph_tcp_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int more)
{ struct msghdr msg = {
.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL | more,
}; struct bio_vec bvec; int ret;
/* * MSG_SPLICE_PAGES cannot properly handle pages with page_count == 0, * we need to fall back to sendmsg if that's the case. * * Same goes for slab pages: skb_can_coalesce() allows * coalescing neighboring slab objects into a single frag which * triggers one of hardened usercopy checks.
*/ if (sendpage_ok(page))
msg.msg_flags |= MSG_SPLICE_PAGES;
/* * Chop off a kvec from the end. Return residual number of bytes for * that kvec, i.e. how many bytes would have been written if the kvec * hadn't been nuked.
*/ staticint con_out_kvec_skip(struct ceph_connection *con)
{ int skip = 0;
staticvoid prepare_message_data(struct ceph_msg *msg, u32 data_len)
{ /* Initialize data cursor if it's not a sparse read */
u64 len = msg->sparse_read_total ? : data_len;
/* * Prepare footer for currently outgoing message, and finish things * off. Assumes out_kvec* are already valid.. we just add on to the end.
*/ staticvoid prepare_write_message_footer(struct ceph_connection *con)
{ struct ceph_msg *m = con->out_msg;
/* Sneak an ack in there first? If we can get it into the same
* TCP packet that's a good thing. */ if (con->in_seq > con->in_seq_acked) {
con->in_seq_acked = con->in_seq;
con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
con->v1.out_temp_ack = cpu_to_le64(con->in_seq_acked);
con_out_kvec_add(con, sizeof(con->v1.out_temp_ack),
&con->v1.out_temp_ack);
}
ceph_con_get_out_msg(con);
m = con->out_msg;
dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
m, con->out_seq, le16_to_cpu(m->hdr.type),
le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
m->data_length);
WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
/* tag + hdr + front + middle */
con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
con_out_kvec_add(con, sizeof(con->v1.out_hdr), &con->v1.out_hdr);
con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
if (m->middle)
con_out_kvec_add(con, m->middle->vec.iov_len,
m->middle->vec.iov_base);
/* fill in hdr crc and finalize hdr */
crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
con->out_msg->hdr.crc = cpu_to_le32(crc);
memcpy(&con->v1.out_hdr, &con->out_msg->hdr, sizeof(con->v1.out_hdr));
/* fill in front and middle crc, footer */
crc = crc32c(0, m->front.iov_base, m->front.iov_len);
con->out_msg->footer.front_crc = cpu_to_le32(crc); if (m->middle) {
crc = crc32c(0, m->middle->vec.iov_base,
m->middle->vec.iov_len);
con->out_msg->footer.middle_crc = cpu_to_le32(crc);
} else
con->out_msg->footer.middle_crc = 0;
dout("%s front_crc %u middle_crc %u\n", __func__,
le32_to_cpu(con->out_msg->footer.front_crc),
le32_to_cpu(con->out_msg->footer.middle_crc));
con->out_msg->footer.flags = 0;
/* is there a data payload? */
con->out_msg->footer.data_crc = 0; if (m->data_length) {
prepare_message_data(con->out_msg, m->data_length);
con->v1.out_more = 1; /* data + footer will follow */
} else { /* no, queue up footer too and be done */
prepare_write_message_footer(con);
}
/* * We connected to a peer and are saying hello.
*/ staticvoid prepare_write_banner(struct ceph_connection *con)
{
con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
&con->msgr->my_enc_addr);
staticint prepare_write_connect(struct ceph_connection *con)
{ unsignedint global_seq = ceph_get_global_seq(con->msgr, 0); int proto; int ret;
switch (con->peer_name.type) { case CEPH_ENTITY_TYPE_MON:
proto = CEPH_MONC_PROTOCOL; break; case CEPH_ENTITY_TYPE_OSD:
proto = CEPH_OSDC_PROTOCOL; break; case CEPH_ENTITY_TYPE_MDS:
proto = CEPH_MDSC_PROTOCOL; break; default:
BUG();
}
ret = get_connect_authorizer(con); if (ret) return ret;
__prepare_write_connect(con); return 0;
}
/* * write as much of pending kvecs to the socket as we can. * 1 -> done * 0 -> socket full, but more to do * <0 -> error
*/ staticint write_partial_kvec(struct ceph_connection *con)
{ int ret;
dout("write_partial_kvec %p %d left\n", con, con->v1.out_kvec_bytes); while (con->v1.out_kvec_bytes > 0) {
ret = ceph_tcp_sendmsg(con->sock, con->v1.out_kvec_cur,
con->v1.out_kvec_left,
con->v1.out_kvec_bytes,
con->v1.out_more); if (ret <= 0) goto out;
con->v1.out_kvec_bytes -= ret; if (!con->v1.out_kvec_bytes) break; /* done */
/* account for full iov entries consumed */ while (ret >= con->v1.out_kvec_cur->iov_len) {
BUG_ON(!con->v1.out_kvec_left);
ret -= con->v1.out_kvec_cur->iov_len;
con->v1.out_kvec_cur++;
con->v1.out_kvec_left--;
} /* and for a partially-consumed entry */ if (ret) {
con->v1.out_kvec_cur->iov_len -= ret;
con->v1.out_kvec_cur->iov_base += ret;
}
}
con->v1.out_kvec_left = 0;
ret = 1;
out:
dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
con->v1.out_kvec_bytes, con->v1.out_kvec_left, ret); return ret; /* done! */
}
/* * Write as much message data payload as we can. If we finish, queue * up the footer. * 1 -> done, footer is now queued in out_kvec[]. * 0 -> socket full, but more to do * <0 -> error
*/ staticint write_partial_message_data(struct ceph_connection *con)
{ struct ceph_msg *msg = con->out_msg; struct ceph_msg_data_cursor *cursor = &msg->cursor; bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
u32 crc;
dout("%s %p msg %p\n", __func__, con, msg);
if (!msg->num_data_items) return -EINVAL;
/* * Iterate through each page that contains data to be * written, and send as much as possible for each. * * If we are calculating the data crc (the default), we will * need to map the page. If we have no pages, they have * been revoked, so use the zero page.
*/
crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0; while (cursor->total_resid) { struct page *page;
size_t page_offset;
size_t length; int ret;
if (!cursor->resid) {
ceph_msg_data_advance(cursor, 0); continue;
}
page = ceph_msg_data_next(cursor, &page_offset, &length);
ret = ceph_tcp_sendpage(con->sock, page, page_offset, length,
MSG_MORE); if (ret <= 0) { if (do_datacrc)
msg->footer.data_crc = cpu_to_le32(crc);
/* prepare and queue up footer, too */ if (do_datacrc)
msg->footer.data_crc = cpu_to_le32(crc); else
msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
con_out_kvec_reset(con);
prepare_write_message_footer(con);
return 1; /* must return > 0 to indicate success */
}
/* * write some zeros
*/ staticint write_partial_skip(struct ceph_connection *con)
{ int ret;
staticint read_partial(struct ceph_connection *con, int end, int size, void *object)
{ while (con->v1.in_base_pos < end) { int left = end - con->v1.in_base_pos; int have = size - left; int ret = ceph_tcp_recvmsg(con->sock, object + have, left); if (ret <= 0) return ret;
con->v1.in_base_pos += ret;
} return 1;
}
/* * Read all or part of the connect-side handshake on a new connection
*/ staticint read_partial_banner(struct ceph_connection *con)
{ int size; int end; int ret;
dout("read_partial_banner %p at %d\n", con, con->v1.in_base_pos);
/* peer's banner */
size = strlen(CEPH_BANNER);
end = size;
ret = read_partial(con, end, size, con->v1.in_banner); if (ret <= 0) goto out;
size = sizeof(con->v1.actual_peer_addr);
end += size;
ret = read_partial(con, end, size, &con->v1.actual_peer_addr); if (ret <= 0) goto out;
ceph_decode_banner_addr(&con->v1.actual_peer_addr);
size = sizeof(con->v1.peer_addr_for_me);
end += size;
ret = read_partial(con, end, size, &con->v1.peer_addr_for_me); if (ret <= 0) goto out;
ceph_decode_banner_addr(&con->v1.peer_addr_for_me);
out: return ret;
}
staticint read_partial_connect(struct ceph_connection *con)
{ int size; int end; int ret;
dout("read_partial_connect %p at %d\n", con, con->v1.in_base_pos);
size = sizeof(con->v1.in_reply);
end = size;
ret = read_partial(con, end, size, &con->v1.in_reply); if (ret <= 0) goto out;
if (con->v1.auth) {
size = le32_to_cpu(con->v1.in_reply.authorizer_len); if (size > con->v1.auth->authorizer_reply_buf_len) {
pr_err("authorizer reply too big: %d > %zu\n", size,
con->v1.auth->authorizer_reply_buf_len);
ret = -EINVAL; goto out;
}
end += size;
ret = read_partial(con, end, size,
con->v1.auth->authorizer_reply_buf); if (ret <= 0) goto out;
}
/* * Make sure the other end is who we wanted. note that the other * end may not yet know their ip address, so if it's 0.0.0.0, give * them the benefit of the doubt.
*/ if (memcmp(&con->peer_addr, &con->v1.actual_peer_addr, sizeof(con->peer_addr)) != 0 &&
!(ceph_addr_is_blank(&con->v1.actual_peer_addr) &&
con->v1.actual_peer_addr.nonce == con->peer_addr.nonce)) {
pr_warn("wrong peer, want %s/%u, got %s/%u\n",
ceph_pr_addr(&con->peer_addr),
le32_to_cpu(con->peer_addr.nonce),
ceph_pr_addr(&con->v1.actual_peer_addr),
le32_to_cpu(con->v1.actual_peer_addr.nonce));
con->error_msg = "wrong peer at address"; return -1;
}
/* * did we learn our address?
*/ if (ceph_addr_is_blank(my_addr)) {
memcpy(&my_addr->in_addr,
&con->v1.peer_addr_for_me.in_addr, sizeof(con->v1.peer_addr_for_me.in_addr));
ceph_addr_set_port(my_addr, 0);
ceph_encode_my_addr(con->msgr);
dout("process_banner learned my addr is %s\n",
ceph_pr_addr(my_addr));
}
dout("process_connect on %p tag %d\n", con, con->v1.in_tag);
if (con->v1.auth) { int len = le32_to_cpu(con->v1.in_reply.authorizer_len);
/* * Any connection that defines ->get_authorizer() * should also define ->add_authorizer_challenge() and * ->verify_authorizer_reply(). * * See get_connect_authorizer().
*/ if (con->v1.in_reply.tag ==
CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER) {
ret = con->ops->add_authorizer_challenge(
con, con->v1.auth->authorizer_reply_buf, len); if (ret < 0) return ret;
case CEPH_MSGR_TAG_BADPROTOVER:
pr_err("%s%lld %s protocol version mismatch," " my %d != server's %d\n",
ENTITY_NAME(con->peer_name),
ceph_pr_addr(&con->peer_addr),
le32_to_cpu(con->v1.out_connect.protocol_version),
le32_to_cpu(con->v1.in_reply.protocol_version));
con->error_msg = "protocol version mismatch"; return -1;
case CEPH_MSGR_TAG_BADAUTHORIZER:
con->v1.auth_retry++;
dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
con->v1.auth_retry); if (con->v1.auth_retry == 2) {
con->error_msg = "connect authorization failure"; return -1;
}
con_out_kvec_reset(con);
ret = prepare_write_connect(con); if (ret < 0) return ret;
prepare_read_connect(con); break;
case CEPH_MSGR_TAG_RESETSESSION: /* * If we connected with a large connect_seq but the peer * has no record of a session with us (no connection, or * connect_seq == 0), they will send RESETSESION to indicate * that they must have reset their session, and may have * dropped messages.
*/
dout("process_connect got RESET peer seq %u\n",
le32_to_cpu(con->v1.in_reply.connect_seq));
pr_info("%s%lld %s session reset\n",
ENTITY_NAME(con->peer_name),
ceph_pr_addr(&con->peer_addr));
ceph_con_reset_session(con);
con_out_kvec_reset(con);
ret = prepare_write_connect(con); if (ret < 0) return ret;
prepare_read_connect(con);
/* Tell ceph about it. */
mutex_unlock(&con->mutex); if (con->ops->peer_reset)
con->ops->peer_reset(con);
mutex_lock(&con->mutex); if (con->state != CEPH_CON_S_V1_CONNECT_MSG) return -EAGAIN; break;
case CEPH_MSGR_TAG_RETRY_SESSION: /* * If we sent a smaller connect_seq than the peer has, try * again with a larger value.
*/
dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
le32_to_cpu(con->v1.out_connect.connect_seq),
le32_to_cpu(con->v1.in_reply.connect_seq));
con->v1.connect_seq = le32_to_cpu(con->v1.in_reply.connect_seq);
con_out_kvec_reset(con);
ret = prepare_write_connect(con); if (ret < 0) return ret;
prepare_read_connect(con); break;
case CEPH_MSGR_TAG_RETRY_GLOBAL: /* * If we sent a smaller global_seq than the peer has, try * again with a larger value.
*/
dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
con->v1.peer_global_seq,
le32_to_cpu(con->v1.in_reply.global_seq));
ceph_get_global_seq(con->msgr,
le32_to_cpu(con->v1.in_reply.global_seq));
con_out_kvec_reset(con);
ret = prepare_write_connect(con); if (ret < 0) return ret;
prepare_read_connect(con); break;
case CEPH_MSGR_TAG_SEQ: case CEPH_MSGR_TAG_READY: if (req_feat & ~server_feat) {
pr_err("%s%lld %s protocol feature mismatch," " my required %llx > server's %llx, need %llx\n",
ENTITY_NAME(con->peer_name),
ceph_pr_addr(&con->peer_addr),
req_feat, server_feat, req_feat & ~server_feat);
con->error_msg = "missing required protocol features"; return -1;
}
case CEPH_MSGR_TAG_WAIT: /* * If there is a connection race (we are opening * connections to each other), one of us may just have * to WAIT. This shouldn't happen if we are the * client.
*/
con->error_msg = "protocol error, got WAIT as client"; return -1;
default:
con->error_msg = "protocol error, garbage tag during connect"; return -1;
} return 0;
}
/* * read (part of) an ack
*/ staticint read_partial_ack(struct ceph_connection *con)
{ int size = sizeof(con->v1.in_temp_ack); int end = size;
while (cursor->total_resid) { if (con->v1.in_sr_kvec.iov_base)
ret = read_partial_message_chunk(con,
&con->v1.in_sr_kvec,
con->v1.in_sr_len,
&crc); elseif (cursor->sr_resid > 0)
ret = read_partial_sparse_msg_extent(con, &crc); if (ret <= 0) break;
memset(&con->v1.in_sr_kvec, 0, sizeof(con->v1.in_sr_kvec));
ret = con->ops->sparse_read(con, cursor,
(char **)&con->v1.in_sr_kvec.iov_base); if (ret <= 0) {
ret = ret ? ret : 1; /* must return > 0 to indicate success */ break;
}
con->v1.in_sr_len = ret;
}
/* allocate message? */ if (!con->in_msg) { int skip = 0;
dout("got hdr type %d front %d data %d\n", con->v1.in_hdr.type,
front_len, data_len);
ret = ceph_con_in_msg_alloc(con, &con->v1.in_hdr, &skip); if (ret < 0) return ret;
BUG_ON(!con->in_msg);
BUG_ON(con->in_msg->con != con);
m = con->in_msg;
m->front.iov_len = 0; /* haven't read it yet */ if (m->middle)
m->middle->vec.iov_len = 0;
/* prepare for data payload, if any */
if (data_len)
prepare_message_data(con->in_msg, data_len);
}
/* front */
ret = read_partial_message_section(con, &m->front, front_len,
&con->in_front_crc); if (ret <= 0) return ret;
/* middle */ if (m->middle) {
ret = read_partial_message_section(con, &m->middle->vec,
middle_len,
&con->in_middle_crc); if (ret <= 0) return ret;
}
/* (page) data */ if (data_len) { if (!m->num_data_items) return -EIO;
if (m->sparse_read_total)
ret = read_partial_sparse_msg_data(con); elseif (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE))
ret = read_partial_msg_data_bounce(con); else
ret = read_partial_msg_data(con); if (ret <= 0) return ret;
}
/* footer */
size = sizeof_footer(con);
end += size;
ret = read_partial(con, end, size, &m->footer); if (ret <= 0) return ret;
if (!need_sign) {
m->footer.flags = m->old_footer.flags;
m->footer.sig = 0;
}
dout("try_read tag %d in_base_pos %d\n", con->v1.in_tag,
con->v1.in_base_pos);
if (con->state == CEPH_CON_S_V1_BANNER) {
ret = read_partial_banner(con); if (ret <= 0) goto out;
ret = process_banner(con); if (ret < 0) goto out;
con->state = CEPH_CON_S_V1_CONNECT_MSG;
/* * Received banner is good, exchange connection info. * Do not reset out_kvec, as sending our banner raced * with receiving peer banner after connect completed.
*/
ret = prepare_write_connect(con); if (ret < 0) goto out;
prepare_read_connect(con);
/* Send connection info before awaiting response */ goto out;
}
if (con->state == CEPH_CON_S_V1_CONNECT_MSG) {
ret = read_partial_connect(con); if (ret <= 0) goto out;
ret = process_connect(con); if (ret < 0) goto out; goto more;
}
WARN_ON(con->state != CEPH_CON_S_OPEN);
if (con->v1.in_base_pos < 0) { /* * skipping + discarding content.
*/
ret = ceph_tcp_recvmsg(con->sock, NULL, -con->v1.in_base_pos); if (ret <= 0) goto out;
dout("skipped %d / %d bytes\n", ret, -con->v1.in_base_pos);
con->v1.in_base_pos += ret; if (con->v1.in_base_pos) goto more;
} if (con->v1.in_tag == CEPH_MSGR_TAG_READY) { /* * what's next?
*/
ret = ceph_tcp_recvmsg(con->sock, &con->v1.in_tag, 1); if (ret <= 0) goto out;
dout("try_read got tag %d\n", con->v1.in_tag); switch (con->v1.in_tag) { case CEPH_MSGR_TAG_MSG:
prepare_read_message(con); break; case CEPH_MSGR_TAG_ACK:
prepare_read_ack(con); break; case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
prepare_read_keepalive_ack(con); break; case CEPH_MSGR_TAG_CLOSE:
ceph_con_close_socket(con);
con->state = CEPH_CON_S_CLOSED; goto out; default: goto bad_tag;
}
} if (con->v1.in_tag == CEPH_MSGR_TAG_MSG) {
ret = read_partial_message(con); if (ret <= 0) { switch (ret) { case -EBADMSG:
con->error_msg = "bad crc/signature";
fallthrough; case -EBADE:
ret = -EIO; break; case -EIO:
con->error_msg = "io error"; break;
} goto out;
} if (con->v1.in_tag == CEPH_MSGR_TAG_READY) goto more;
ceph_con_process_message(con); if (con->state == CEPH_CON_S_OPEN)
prepare_read_tag(con); goto more;
} if (con->v1.in_tag == CEPH_MSGR_TAG_ACK ||
con->v1.in_tag == CEPH_MSGR_TAG_SEQ) { /* * the final handshake seq exchange is semantically * equivalent to an ACK
*/
ret = read_partial_ack(con); if (ret <= 0) goto out;
process_ack(con); goto more;
} if (con->v1.in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
ret = read_keepalive_ack(con); if (ret <= 0) goto out; goto more;
}
out:
dout("try_read done on %p ret %d\n", con, ret); return ret;
bad_tag:
pr_err("try_read bad tag %d\n", con->v1.in_tag);
con->error_msg = "protocol error, garbage tag";
ret = -1; goto out;
}
/* * Write something to the socket. Called in a worker thread when the * socket appears to be writeable and we have something ready to send.
*/ int ceph_con_v1_try_write(struct ceph_connection *con)
{ int ret = 1;
/* kvec data queued? */ if (con->v1.out_kvec_left) {
ret = write_partial_kvec(con); if (ret <= 0) goto out;
} if (con->v1.out_skip) {
ret = write_partial_skip(con); if (ret <= 0) goto out;
}
/* msg pages? */ if (con->out_msg) { if (con->v1.out_msg_done) {
ceph_msg_put(con->out_msg);
con->out_msg = NULL; /* we're done with this one */ goto do_next;
}
ret = write_partial_message_data(con); if (ret == 1) goto more; /* we need to send the footer, too! */ if (ret == 0) goto out; if (ret < 0) {
dout("try_write write_partial_message_data err %d\n",
ret); goto out;
}
}
do_next: if (con->state == CEPH_CON_S_OPEN) { if (ceph_con_flag_test_and_clear(con,
CEPH_CON_F_KEEPALIVE_PENDING)) {
prepare_write_keepalive(con); goto more;
} /* is anything else pending? */ if (!list_empty(&con->out_queue)) {
prepare_write_message(con); goto more;
} if (con->in_seq > con->in_seq_acked) {
prepare_write_ack(con); goto more;
}
}
/* Nothing to do! */
ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
dout("try_write nothing else to write.\n");
ret = 0;
out:
dout("try_write done on %p ret %d\n", con, ret); return ret;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.