Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1 | #include "ceph_debug.h" |
| 2 | |
| 3 | #include <linux/crc32c.h> |
| 4 | #include <linux/ctype.h> |
| 5 | #include <linux/highmem.h> |
| 6 | #include <linux/inet.h> |
| 7 | #include <linux/kthread.h> |
| 8 | #include <linux/net.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 10 | #include <linux/socket.h> |
| 11 | #include <linux/string.h> |
| 12 | #include <net/tcp.h> |
| 13 | |
| 14 | #include "super.h" |
| 15 | #include "messenger.h" |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 16 | #include "decode.h" |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 17 | #include "pagelist.h" |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Ceph uses the messenger to exchange ceph_msg messages with other |
| 21 | * hosts in the system. The messenger provides ordered and reliable |
| 22 | * delivery. We tolerate TCP disconnects by reconnecting (with |
| 23 | * exponential backoff) in the case of a fault (disconnection, bad |
| 24 | * crc, protocol error). Acks allow sent messages to be discarded by |
| 25 | * the sender. |
| 26 | */ |
| 27 | |
| 28 | /* static tag bytes (protocol control messages) */ |
| 29 | static char tag_msg = CEPH_MSGR_TAG_MSG; |
| 30 | static char tag_ack = CEPH_MSGR_TAG_ACK; |
| 31 | static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE; |
| 32 | |
Sage Weil | a6a5349 | 2010-04-13 14:07:07 -0700 | [diff] [blame] | 33 | #ifdef CONFIG_LOCKDEP |
| 34 | static struct lock_class_key socket_class; |
| 35 | #endif |
| 36 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 37 | |
| 38 | static void queue_con(struct ceph_connection *con); |
| 39 | static void con_work(struct work_struct *); |
| 40 | static void ceph_fault(struct ceph_connection *con); |
| 41 | |
| 42 | const char *ceph_name_type_str(int t) |
| 43 | { |
| 44 | switch (t) { |
| 45 | case CEPH_ENTITY_TYPE_MON: return "mon"; |
| 46 | case CEPH_ENTITY_TYPE_MDS: return "mds"; |
| 47 | case CEPH_ENTITY_TYPE_OSD: return "osd"; |
| 48 | case CEPH_ENTITY_TYPE_CLIENT: return "client"; |
| 49 | case CEPH_ENTITY_TYPE_ADMIN: return "admin"; |
| 50 | default: return "???"; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * nicely render a sockaddr as a string. |
| 56 | */ |
| 57 | #define MAX_ADDR_STR 20 |
| 58 | static char addr_str[MAX_ADDR_STR][40]; |
| 59 | static DEFINE_SPINLOCK(addr_str_lock); |
| 60 | static int last_addr_str; |
| 61 | |
| 62 | const char *pr_addr(const struct sockaddr_storage *ss) |
| 63 | { |
| 64 | int i; |
| 65 | char *s; |
| 66 | struct sockaddr_in *in4 = (void *)ss; |
| 67 | unsigned char *quad = (void *)&in4->sin_addr.s_addr; |
| 68 | struct sockaddr_in6 *in6 = (void *)ss; |
| 69 | |
| 70 | spin_lock(&addr_str_lock); |
| 71 | i = last_addr_str++; |
| 72 | if (last_addr_str == MAX_ADDR_STR) |
| 73 | last_addr_str = 0; |
| 74 | spin_unlock(&addr_str_lock); |
| 75 | s = addr_str[i]; |
| 76 | |
| 77 | switch (ss->ss_family) { |
| 78 | case AF_INET: |
| 79 | sprintf(s, "%u.%u.%u.%u:%u", |
| 80 | (unsigned int)quad[0], |
| 81 | (unsigned int)quad[1], |
| 82 | (unsigned int)quad[2], |
| 83 | (unsigned int)quad[3], |
| 84 | (unsigned int)ntohs(in4->sin_port)); |
| 85 | break; |
| 86 | |
| 87 | case AF_INET6: |
| 88 | sprintf(s, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u", |
| 89 | in6->sin6_addr.s6_addr16[0], |
| 90 | in6->sin6_addr.s6_addr16[1], |
| 91 | in6->sin6_addr.s6_addr16[2], |
| 92 | in6->sin6_addr.s6_addr16[3], |
| 93 | in6->sin6_addr.s6_addr16[4], |
| 94 | in6->sin6_addr.s6_addr16[5], |
| 95 | in6->sin6_addr.s6_addr16[6], |
| 96 | in6->sin6_addr.s6_addr16[7], |
| 97 | (unsigned int)ntohs(in6->sin6_port)); |
| 98 | break; |
| 99 | |
| 100 | default: |
| 101 | sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family); |
| 102 | } |
| 103 | |
| 104 | return s; |
| 105 | } |
| 106 | |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 107 | static void encode_my_addr(struct ceph_messenger *msgr) |
| 108 | { |
| 109 | memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr)); |
| 110 | ceph_encode_addr(&msgr->my_enc_addr); |
| 111 | } |
| 112 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 113 | /* |
| 114 | * work queue for all reading and writing to/from the socket. |
| 115 | */ |
| 116 | struct workqueue_struct *ceph_msgr_wq; |
| 117 | |
| 118 | int __init ceph_msgr_init(void) |
| 119 | { |
| 120 | ceph_msgr_wq = create_workqueue("ceph-msgr"); |
| 121 | if (IS_ERR(ceph_msgr_wq)) { |
| 122 | int ret = PTR_ERR(ceph_msgr_wq); |
| 123 | pr_err("msgr_init failed to create workqueue: %d\n", ret); |
| 124 | ceph_msgr_wq = NULL; |
| 125 | return ret; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | void ceph_msgr_exit(void) |
| 131 | { |
| 132 | destroy_workqueue(ceph_msgr_wq); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * socket callback functions |
| 137 | */ |
| 138 | |
| 139 | /* data available on socket, or listen socket received a connect */ |
| 140 | static void ceph_data_ready(struct sock *sk, int count_unused) |
| 141 | { |
| 142 | struct ceph_connection *con = |
| 143 | (struct ceph_connection *)sk->sk_user_data; |
| 144 | if (sk->sk_state != TCP_CLOSE_WAIT) { |
| 145 | dout("ceph_data_ready on %p state = %lu, queueing work\n", |
| 146 | con, con->state); |
| 147 | queue_con(con); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* socket has buffer space for writing */ |
| 152 | static void ceph_write_space(struct sock *sk) |
| 153 | { |
| 154 | struct ceph_connection *con = |
| 155 | (struct ceph_connection *)sk->sk_user_data; |
| 156 | |
| 157 | /* only queue to workqueue if there is data we want to write. */ |
| 158 | if (test_bit(WRITE_PENDING, &con->state)) { |
| 159 | dout("ceph_write_space %p queueing write work\n", con); |
| 160 | queue_con(con); |
| 161 | } else { |
| 162 | dout("ceph_write_space %p nothing to write\n", con); |
| 163 | } |
| 164 | |
| 165 | /* since we have our own write_space, clear the SOCK_NOSPACE flag */ |
| 166 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 167 | } |
| 168 | |
| 169 | /* socket's state has changed */ |
| 170 | static void ceph_state_change(struct sock *sk) |
| 171 | { |
| 172 | struct ceph_connection *con = |
| 173 | (struct ceph_connection *)sk->sk_user_data; |
| 174 | |
| 175 | dout("ceph_state_change %p state = %lu sk_state = %u\n", |
| 176 | con, con->state, sk->sk_state); |
| 177 | |
| 178 | if (test_bit(CLOSED, &con->state)) |
| 179 | return; |
| 180 | |
| 181 | switch (sk->sk_state) { |
| 182 | case TCP_CLOSE: |
| 183 | dout("ceph_state_change TCP_CLOSE\n"); |
| 184 | case TCP_CLOSE_WAIT: |
| 185 | dout("ceph_state_change TCP_CLOSE_WAIT\n"); |
| 186 | if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) { |
| 187 | if (test_bit(CONNECTING, &con->state)) |
| 188 | con->error_msg = "connection failed"; |
| 189 | else |
| 190 | con->error_msg = "socket closed"; |
| 191 | queue_con(con); |
| 192 | } |
| 193 | break; |
| 194 | case TCP_ESTABLISHED: |
| 195 | dout("ceph_state_change TCP_ESTABLISHED\n"); |
| 196 | queue_con(con); |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * set up socket callbacks |
| 203 | */ |
| 204 | static void set_sock_callbacks(struct socket *sock, |
| 205 | struct ceph_connection *con) |
| 206 | { |
| 207 | struct sock *sk = sock->sk; |
| 208 | sk->sk_user_data = (void *)con; |
| 209 | sk->sk_data_ready = ceph_data_ready; |
| 210 | sk->sk_write_space = ceph_write_space; |
| 211 | sk->sk_state_change = ceph_state_change; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /* |
| 216 | * socket helpers |
| 217 | */ |
| 218 | |
| 219 | /* |
| 220 | * initiate connection to a remote socket. |
| 221 | */ |
| 222 | static struct socket *ceph_tcp_connect(struct ceph_connection *con) |
| 223 | { |
| 224 | struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr; |
| 225 | struct socket *sock; |
| 226 | int ret; |
| 227 | |
| 228 | BUG_ON(con->sock); |
| 229 | ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); |
| 230 | if (ret) |
| 231 | return ERR_PTR(ret); |
| 232 | con->sock = sock; |
| 233 | sock->sk->sk_allocation = GFP_NOFS; |
| 234 | |
Sage Weil | a6a5349 | 2010-04-13 14:07:07 -0700 | [diff] [blame] | 235 | #ifdef CONFIG_LOCKDEP |
| 236 | lockdep_set_class(&sock->sk->sk_lock, &socket_class); |
| 237 | #endif |
| 238 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 239 | set_sock_callbacks(sock, con); |
| 240 | |
| 241 | dout("connect %s\n", pr_addr(&con->peer_addr.in_addr)); |
| 242 | |
| 243 | ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK); |
| 244 | if (ret == -EINPROGRESS) { |
| 245 | dout("connect %s EINPROGRESS sk_state = %u\n", |
| 246 | pr_addr(&con->peer_addr.in_addr), |
| 247 | sock->sk->sk_state); |
| 248 | ret = 0; |
| 249 | } |
| 250 | if (ret < 0) { |
| 251 | pr_err("connect %s error %d\n", |
| 252 | pr_addr(&con->peer_addr.in_addr), ret); |
| 253 | sock_release(sock); |
| 254 | con->sock = NULL; |
| 255 | con->error_msg = "connect error"; |
| 256 | } |
| 257 | |
| 258 | if (ret < 0) |
| 259 | return ERR_PTR(ret); |
| 260 | return sock; |
| 261 | } |
| 262 | |
| 263 | static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len) |
| 264 | { |
| 265 | struct kvec iov = {buf, len}; |
| 266 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; |
| 267 | |
| 268 | return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * write something. @more is true if caller will be sending more data |
| 273 | * shortly. |
| 274 | */ |
| 275 | static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov, |
| 276 | size_t kvlen, size_t len, int more) |
| 277 | { |
| 278 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; |
| 279 | |
| 280 | if (more) |
| 281 | msg.msg_flags |= MSG_MORE; |
| 282 | else |
| 283 | msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */ |
| 284 | |
| 285 | return kernel_sendmsg(sock, &msg, iov, kvlen, len); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /* |
| 290 | * Shutdown/close the socket for the given connection. |
| 291 | */ |
| 292 | static int con_close_socket(struct ceph_connection *con) |
| 293 | { |
| 294 | int rc; |
| 295 | |
| 296 | dout("con_close_socket on %p sock %p\n", con, con->sock); |
| 297 | if (!con->sock) |
| 298 | return 0; |
| 299 | set_bit(SOCK_CLOSED, &con->state); |
| 300 | rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR); |
| 301 | sock_release(con->sock); |
| 302 | con->sock = NULL; |
| 303 | clear_bit(SOCK_CLOSED, &con->state); |
| 304 | return rc; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Reset a connection. Discard all incoming and outgoing messages |
| 309 | * and clear *_seq state. |
| 310 | */ |
| 311 | static void ceph_msg_remove(struct ceph_msg *msg) |
| 312 | { |
| 313 | list_del_init(&msg->list_head); |
| 314 | ceph_msg_put(msg); |
| 315 | } |
| 316 | static void ceph_msg_remove_list(struct list_head *head) |
| 317 | { |
| 318 | while (!list_empty(head)) { |
| 319 | struct ceph_msg *msg = list_first_entry(head, struct ceph_msg, |
| 320 | list_head); |
| 321 | ceph_msg_remove(msg); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static void reset_connection(struct ceph_connection *con) |
| 326 | { |
| 327 | /* reset connection, out_queue, msg_ and connect_seq */ |
| 328 | /* discard existing out_queue and msg_seq */ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 329 | ceph_msg_remove_list(&con->out_queue); |
| 330 | ceph_msg_remove_list(&con->out_sent); |
| 331 | |
Sage Weil | cf3e5c4 | 2009-12-11 09:48:05 -0800 | [diff] [blame] | 332 | if (con->in_msg) { |
| 333 | ceph_msg_put(con->in_msg); |
| 334 | con->in_msg = NULL; |
| 335 | } |
| 336 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 337 | con->connect_seq = 0; |
| 338 | con->out_seq = 0; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 339 | if (con->out_msg) { |
| 340 | ceph_msg_put(con->out_msg); |
| 341 | con->out_msg = NULL; |
| 342 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 343 | con->in_seq = 0; |
Sage Weil | 0e0d5e0 | 2010-04-02 16:07:19 -0700 | [diff] [blame] | 344 | con->in_seq_acked = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* |
| 348 | * mark a peer down. drop any open connections. |
| 349 | */ |
| 350 | void ceph_con_close(struct ceph_connection *con) |
| 351 | { |
| 352 | dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr)); |
| 353 | set_bit(CLOSED, &con->state); /* in case there's queued work */ |
| 354 | clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */ |
Sage Weil | 1679f87 | 2010-02-26 13:55:51 -0800 | [diff] [blame] | 355 | clear_bit(LOSSYTX, &con->state); /* so we retry next connect */ |
| 356 | clear_bit(KEEPALIVE_PENDING, &con->state); |
| 357 | clear_bit(WRITE_PENDING, &con->state); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 358 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 359 | reset_connection(con); |
Sage Weil | 91e45ce | 2010-02-15 12:05:09 -0800 | [diff] [blame] | 360 | cancel_delayed_work(&con->work); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 361 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 362 | queue_con(con); |
| 363 | } |
| 364 | |
| 365 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 366 | * Reopen a closed connection, with a new peer address. |
| 367 | */ |
| 368 | void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr) |
| 369 | { |
| 370 | dout("con_open %p %s\n", con, pr_addr(&addr->in_addr)); |
| 371 | set_bit(OPENING, &con->state); |
| 372 | clear_bit(CLOSED, &con->state); |
| 373 | memcpy(&con->peer_addr, addr, sizeof(*addr)); |
Sage Weil | 03c677e | 2009-11-20 15:14:15 -0800 | [diff] [blame] | 374 | con->delay = 0; /* reset backoff memory */ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 375 | queue_con(con); |
| 376 | } |
| 377 | |
| 378 | /* |
Sage Weil | 87b315a | 2010-03-22 14:51:18 -0700 | [diff] [blame] | 379 | * return true if this connection ever successfully opened |
| 380 | */ |
| 381 | bool ceph_con_opened(struct ceph_connection *con) |
| 382 | { |
| 383 | return con->connect_seq > 0; |
| 384 | } |
| 385 | |
| 386 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 387 | * generic get/put |
| 388 | */ |
| 389 | struct ceph_connection *ceph_con_get(struct ceph_connection *con) |
| 390 | { |
| 391 | dout("con_get %p nref = %d -> %d\n", con, |
| 392 | atomic_read(&con->nref), atomic_read(&con->nref) + 1); |
| 393 | if (atomic_inc_not_zero(&con->nref)) |
| 394 | return con; |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | void ceph_con_put(struct ceph_connection *con) |
| 399 | { |
| 400 | dout("con_put %p nref = %d -> %d\n", con, |
| 401 | atomic_read(&con->nref), atomic_read(&con->nref) - 1); |
| 402 | BUG_ON(atomic_read(&con->nref) == 0); |
| 403 | if (atomic_dec_and_test(&con->nref)) { |
Sage Weil | 71ececd | 2009-11-18 11:27:06 -0800 | [diff] [blame] | 404 | BUG_ON(con->sock); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 405 | kfree(con); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * initialize a new connection. |
| 411 | */ |
| 412 | void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con) |
| 413 | { |
| 414 | dout("con_init %p\n", con); |
| 415 | memset(con, 0, sizeof(*con)); |
| 416 | atomic_set(&con->nref, 1); |
| 417 | con->msgr = msgr; |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 418 | mutex_init(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 419 | INIT_LIST_HEAD(&con->out_queue); |
| 420 | INIT_LIST_HEAD(&con->out_sent); |
| 421 | INIT_DELAYED_WORK(&con->work, con_work); |
| 422 | } |
| 423 | |
| 424 | |
| 425 | /* |
| 426 | * We maintain a global counter to order connection attempts. Get |
| 427 | * a unique seq greater than @gt. |
| 428 | */ |
| 429 | static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt) |
| 430 | { |
| 431 | u32 ret; |
| 432 | |
| 433 | spin_lock(&msgr->global_seq_lock); |
| 434 | if (msgr->global_seq < gt) |
| 435 | msgr->global_seq = gt; |
| 436 | ret = ++msgr->global_seq; |
| 437 | spin_unlock(&msgr->global_seq_lock); |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | /* |
| 443 | * Prepare footer for currently outgoing message, and finish things |
| 444 | * off. Assumes out_kvec* are already valid.. we just add on to the end. |
| 445 | */ |
| 446 | static void prepare_write_message_footer(struct ceph_connection *con, int v) |
| 447 | { |
| 448 | struct ceph_msg *m = con->out_msg; |
| 449 | |
| 450 | dout("prepare_write_message_footer %p\n", con); |
| 451 | con->out_kvec_is_msg = true; |
| 452 | con->out_kvec[v].iov_base = &m->footer; |
| 453 | con->out_kvec[v].iov_len = sizeof(m->footer); |
| 454 | con->out_kvec_bytes += sizeof(m->footer); |
| 455 | con->out_kvec_left++; |
| 456 | con->out_more = m->more_to_follow; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 457 | con->out_msg_done = true; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Prepare headers for the next outgoing message. |
| 462 | */ |
| 463 | static void prepare_write_message(struct ceph_connection *con) |
| 464 | { |
| 465 | struct ceph_msg *m; |
| 466 | int v = 0; |
| 467 | |
| 468 | con->out_kvec_bytes = 0; |
| 469 | con->out_kvec_is_msg = true; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 470 | con->out_msg_done = false; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 471 | |
| 472 | /* Sneak an ack in there first? If we can get it into the same |
| 473 | * TCP packet that's a good thing. */ |
| 474 | if (con->in_seq > con->in_seq_acked) { |
| 475 | con->in_seq_acked = con->in_seq; |
| 476 | con->out_kvec[v].iov_base = &tag_ack; |
| 477 | con->out_kvec[v++].iov_len = 1; |
| 478 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
| 479 | con->out_kvec[v].iov_base = &con->out_temp_ack; |
| 480 | con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack); |
| 481 | con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack); |
| 482 | } |
| 483 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 484 | m = list_first_entry(&con->out_queue, |
| 485 | struct ceph_msg, list_head); |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 486 | con->out_msg = m; |
Sage Weil | b3d1dbb | 2009-12-14 14:58:11 -0800 | [diff] [blame] | 487 | if (test_bit(LOSSYTX, &con->state)) { |
Sage Weil | 6c5d1a4 | 2010-02-13 20:29:31 -0800 | [diff] [blame] | 488 | list_del_init(&m->list_head); |
| 489 | } else { |
Sage Weil | b3d1dbb | 2009-12-14 14:58:11 -0800 | [diff] [blame] | 490 | /* put message on sent list */ |
| 491 | ceph_msg_get(m); |
| 492 | list_move_tail(&m->list_head, &con->out_sent); |
Sage Weil | b3d1dbb | 2009-12-14 14:58:11 -0800 | [diff] [blame] | 493 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 494 | |
| 495 | m->hdr.seq = cpu_to_le64(++con->out_seq); |
| 496 | |
| 497 | dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n", |
| 498 | m, con->out_seq, le16_to_cpu(m->hdr.type), |
| 499 | le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len), |
| 500 | le32_to_cpu(m->hdr.data_len), |
| 501 | m->nr_pages); |
| 502 | BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len); |
| 503 | |
| 504 | /* tag + hdr + front + middle */ |
| 505 | con->out_kvec[v].iov_base = &tag_msg; |
| 506 | con->out_kvec[v++].iov_len = 1; |
| 507 | con->out_kvec[v].iov_base = &m->hdr; |
| 508 | con->out_kvec[v++].iov_len = sizeof(m->hdr); |
| 509 | con->out_kvec[v++] = m->front; |
| 510 | if (m->middle) |
| 511 | con->out_kvec[v++] = m->middle->vec; |
| 512 | con->out_kvec_left = v; |
| 513 | con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len + |
| 514 | (m->middle ? m->middle->vec.iov_len : 0); |
| 515 | con->out_kvec_cur = con->out_kvec; |
| 516 | |
| 517 | /* fill in crc (except data pages), footer */ |
| 518 | con->out_msg->hdr.crc = |
| 519 | cpu_to_le32(crc32c(0, (void *)&m->hdr, |
| 520 | sizeof(m->hdr) - sizeof(m->hdr.crc))); |
| 521 | con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE; |
| 522 | con->out_msg->footer.front_crc = |
| 523 | cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len)); |
| 524 | if (m->middle) |
| 525 | con->out_msg->footer.middle_crc = |
| 526 | cpu_to_le32(crc32c(0, m->middle->vec.iov_base, |
| 527 | m->middle->vec.iov_len)); |
| 528 | else |
| 529 | con->out_msg->footer.middle_crc = 0; |
| 530 | con->out_msg->footer.data_crc = 0; |
| 531 | dout("prepare_write_message front_crc %u data_crc %u\n", |
| 532 | le32_to_cpu(con->out_msg->footer.front_crc), |
| 533 | le32_to_cpu(con->out_msg->footer.middle_crc)); |
| 534 | |
| 535 | /* is there a data payload? */ |
| 536 | if (le32_to_cpu(m->hdr.data_len) > 0) { |
| 537 | /* initialize page iterator */ |
| 538 | con->out_msg_pos.page = 0; |
| 539 | con->out_msg_pos.page_pos = |
| 540 | le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK; |
| 541 | con->out_msg_pos.data_pos = 0; |
| 542 | con->out_msg_pos.did_page_crc = 0; |
| 543 | con->out_more = 1; /* data + footer will follow */ |
| 544 | } else { |
| 545 | /* no, queue up footer too and be done */ |
| 546 | prepare_write_message_footer(con, v); |
| 547 | } |
| 548 | |
| 549 | set_bit(WRITE_PENDING, &con->state); |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Prepare an ack. |
| 554 | */ |
| 555 | static void prepare_write_ack(struct ceph_connection *con) |
| 556 | { |
| 557 | dout("prepare_write_ack %p %llu -> %llu\n", con, |
| 558 | con->in_seq_acked, con->in_seq); |
| 559 | con->in_seq_acked = con->in_seq; |
| 560 | |
| 561 | con->out_kvec[0].iov_base = &tag_ack; |
| 562 | con->out_kvec[0].iov_len = 1; |
| 563 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
| 564 | con->out_kvec[1].iov_base = &con->out_temp_ack; |
| 565 | con->out_kvec[1].iov_len = sizeof(con->out_temp_ack); |
| 566 | con->out_kvec_left = 2; |
| 567 | con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack); |
| 568 | con->out_kvec_cur = con->out_kvec; |
| 569 | con->out_more = 1; /* more will follow.. eventually.. */ |
| 570 | set_bit(WRITE_PENDING, &con->state); |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Prepare to write keepalive byte. |
| 575 | */ |
| 576 | static void prepare_write_keepalive(struct ceph_connection *con) |
| 577 | { |
| 578 | dout("prepare_write_keepalive %p\n", con); |
| 579 | con->out_kvec[0].iov_base = &tag_keepalive; |
| 580 | con->out_kvec[0].iov_len = 1; |
| 581 | con->out_kvec_left = 1; |
| 582 | con->out_kvec_bytes = 1; |
| 583 | con->out_kvec_cur = con->out_kvec; |
| 584 | set_bit(WRITE_PENDING, &con->state); |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * Connection negotiation. |
| 589 | */ |
| 590 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 591 | static void prepare_connect_authorizer(struct ceph_connection *con) |
| 592 | { |
| 593 | void *auth_buf; |
| 594 | int auth_len = 0; |
| 595 | int auth_protocol = 0; |
| 596 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 597 | mutex_unlock(&con->mutex); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 598 | if (con->ops->get_authorizer) |
| 599 | con->ops->get_authorizer(con, &auth_buf, &auth_len, |
| 600 | &auth_protocol, &con->auth_reply_buf, |
| 601 | &con->auth_reply_buf_len, |
| 602 | con->auth_retry); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 603 | mutex_lock(&con->mutex); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 604 | |
| 605 | con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol); |
| 606 | con->out_connect.authorizer_len = cpu_to_le32(auth_len); |
| 607 | |
| 608 | con->out_kvec[con->out_kvec_left].iov_base = auth_buf; |
| 609 | con->out_kvec[con->out_kvec_left].iov_len = auth_len; |
| 610 | con->out_kvec_left++; |
| 611 | con->out_kvec_bytes += auth_len; |
| 612 | } |
| 613 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 614 | /* |
| 615 | * We connected to a peer and are saying hello. |
| 616 | */ |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 617 | static void prepare_write_banner(struct ceph_messenger *msgr, |
| 618 | struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 619 | { |
| 620 | int len = strlen(CEPH_BANNER); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 621 | |
| 622 | con->out_kvec[0].iov_base = CEPH_BANNER; |
| 623 | con->out_kvec[0].iov_len = len; |
| 624 | con->out_kvec[1].iov_base = &msgr->my_enc_addr; |
| 625 | con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr); |
| 626 | con->out_kvec_left = 2; |
| 627 | con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr); |
| 628 | con->out_kvec_cur = con->out_kvec; |
| 629 | con->out_more = 0; |
| 630 | set_bit(WRITE_PENDING, &con->state); |
| 631 | } |
| 632 | |
| 633 | static void prepare_write_connect(struct ceph_messenger *msgr, |
| 634 | struct ceph_connection *con, |
| 635 | int after_banner) |
| 636 | { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 637 | unsigned global_seq = get_global_seq(con->msgr, 0); |
| 638 | int proto; |
| 639 | |
| 640 | switch (con->peer_name.type) { |
| 641 | case CEPH_ENTITY_TYPE_MON: |
| 642 | proto = CEPH_MONC_PROTOCOL; |
| 643 | break; |
| 644 | case CEPH_ENTITY_TYPE_OSD: |
| 645 | proto = CEPH_OSDC_PROTOCOL; |
| 646 | break; |
| 647 | case CEPH_ENTITY_TYPE_MDS: |
| 648 | proto = CEPH_MDSC_PROTOCOL; |
| 649 | break; |
| 650 | default: |
| 651 | BUG(); |
| 652 | } |
| 653 | |
| 654 | dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con, |
| 655 | con->connect_seq, global_seq, proto); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 656 | |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 657 | con->out_connect.features = CEPH_FEATURE_SUPPORTED; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 658 | con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT); |
| 659 | con->out_connect.connect_seq = cpu_to_le32(con->connect_seq); |
| 660 | con->out_connect.global_seq = cpu_to_le32(global_seq); |
| 661 | con->out_connect.protocol_version = cpu_to_le32(proto); |
| 662 | con->out_connect.flags = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 663 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 664 | if (!after_banner) { |
| 665 | con->out_kvec_left = 0; |
| 666 | con->out_kvec_bytes = 0; |
| 667 | } |
| 668 | con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect; |
| 669 | con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect); |
| 670 | con->out_kvec_left++; |
| 671 | con->out_kvec_bytes += sizeof(con->out_connect); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 672 | con->out_kvec_cur = con->out_kvec; |
| 673 | con->out_more = 0; |
| 674 | set_bit(WRITE_PENDING, &con->state); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 675 | |
| 676 | prepare_connect_authorizer(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | |
| 680 | /* |
| 681 | * write as much of pending kvecs to the socket as we can. |
| 682 | * 1 -> done |
| 683 | * 0 -> socket full, but more to do |
| 684 | * <0 -> error |
| 685 | */ |
| 686 | static int write_partial_kvec(struct ceph_connection *con) |
| 687 | { |
| 688 | int ret; |
| 689 | |
| 690 | dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes); |
| 691 | while (con->out_kvec_bytes > 0) { |
| 692 | ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur, |
| 693 | con->out_kvec_left, con->out_kvec_bytes, |
| 694 | con->out_more); |
| 695 | if (ret <= 0) |
| 696 | goto out; |
| 697 | con->out_kvec_bytes -= ret; |
| 698 | if (con->out_kvec_bytes == 0) |
| 699 | break; /* done */ |
| 700 | while (ret > 0) { |
| 701 | if (ret >= con->out_kvec_cur->iov_len) { |
| 702 | ret -= con->out_kvec_cur->iov_len; |
| 703 | con->out_kvec_cur++; |
| 704 | con->out_kvec_left--; |
| 705 | } else { |
| 706 | con->out_kvec_cur->iov_len -= ret; |
| 707 | con->out_kvec_cur->iov_base += ret; |
| 708 | ret = 0; |
| 709 | break; |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | con->out_kvec_left = 0; |
| 714 | con->out_kvec_is_msg = false; |
| 715 | ret = 1; |
| 716 | out: |
| 717 | dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con, |
| 718 | con->out_kvec_bytes, con->out_kvec_left, ret); |
| 719 | return ret; /* done! */ |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * Write as much message data payload as we can. If we finish, queue |
| 724 | * up the footer. |
| 725 | * 1 -> done, footer is now queued in out_kvec[]. |
| 726 | * 0 -> socket full, but more to do |
| 727 | * <0 -> error |
| 728 | */ |
| 729 | static int write_partial_msg_pages(struct ceph_connection *con) |
| 730 | { |
| 731 | struct ceph_msg *msg = con->out_msg; |
| 732 | unsigned data_len = le32_to_cpu(msg->hdr.data_len); |
| 733 | size_t len; |
| 734 | int crc = con->msgr->nocrc; |
| 735 | int ret; |
| 736 | |
| 737 | dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n", |
| 738 | con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages, |
| 739 | con->out_msg_pos.page_pos); |
| 740 | |
| 741 | while (con->out_msg_pos.page < con->out_msg->nr_pages) { |
| 742 | struct page *page = NULL; |
| 743 | void *kaddr = NULL; |
| 744 | |
| 745 | /* |
| 746 | * if we are calculating the data crc (the default), we need |
| 747 | * to map the page. if our pages[] has been revoked, use the |
| 748 | * zero page. |
| 749 | */ |
| 750 | if (msg->pages) { |
| 751 | page = msg->pages[con->out_msg_pos.page]; |
| 752 | if (crc) |
| 753 | kaddr = kmap(page); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 754 | } else if (msg->pagelist) { |
| 755 | page = list_first_entry(&msg->pagelist->head, |
| 756 | struct page, lru); |
| 757 | if (crc) |
| 758 | kaddr = kmap(page); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 759 | } else { |
| 760 | page = con->msgr->zero_page; |
| 761 | if (crc) |
| 762 | kaddr = page_address(con->msgr->zero_page); |
| 763 | } |
| 764 | len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos), |
| 765 | (int)(data_len - con->out_msg_pos.data_pos)); |
| 766 | if (crc && !con->out_msg_pos.did_page_crc) { |
| 767 | void *base = kaddr + con->out_msg_pos.page_pos; |
| 768 | u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc); |
| 769 | |
| 770 | BUG_ON(kaddr == NULL); |
| 771 | con->out_msg->footer.data_crc = |
| 772 | cpu_to_le32(crc32c(tmpcrc, base, len)); |
| 773 | con->out_msg_pos.did_page_crc = 1; |
| 774 | } |
| 775 | |
| 776 | ret = kernel_sendpage(con->sock, page, |
| 777 | con->out_msg_pos.page_pos, len, |
| 778 | MSG_DONTWAIT | MSG_NOSIGNAL | |
| 779 | MSG_MORE); |
| 780 | |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 781 | if (crc && (msg->pages || msg->pagelist)) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 782 | kunmap(page); |
| 783 | |
| 784 | if (ret <= 0) |
| 785 | goto out; |
| 786 | |
| 787 | con->out_msg_pos.data_pos += ret; |
| 788 | con->out_msg_pos.page_pos += ret; |
| 789 | if (ret == len) { |
| 790 | con->out_msg_pos.page_pos = 0; |
| 791 | con->out_msg_pos.page++; |
| 792 | con->out_msg_pos.did_page_crc = 0; |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 793 | if (msg->pagelist) |
| 794 | list_move_tail(&page->lru, |
| 795 | &msg->pagelist->head); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
| 799 | dout("write_partial_msg_pages %p msg %p done\n", con, msg); |
| 800 | |
| 801 | /* prepare and queue up footer, too */ |
| 802 | if (!crc) |
| 803 | con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC; |
| 804 | con->out_kvec_bytes = 0; |
| 805 | con->out_kvec_left = 0; |
| 806 | con->out_kvec_cur = con->out_kvec; |
| 807 | prepare_write_message_footer(con, 0); |
| 808 | ret = 1; |
| 809 | out: |
| 810 | return ret; |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * write some zeros |
| 815 | */ |
| 816 | static int write_partial_skip(struct ceph_connection *con) |
| 817 | { |
| 818 | int ret; |
| 819 | |
| 820 | while (con->out_skip > 0) { |
| 821 | struct kvec iov = { |
| 822 | .iov_base = page_address(con->msgr->zero_page), |
| 823 | .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE) |
| 824 | }; |
| 825 | |
| 826 | ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1); |
| 827 | if (ret <= 0) |
| 828 | goto out; |
| 829 | con->out_skip -= ret; |
| 830 | } |
| 831 | ret = 1; |
| 832 | out: |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Prepare to read connection handshake, or an ack. |
| 838 | */ |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 839 | static void prepare_read_banner(struct ceph_connection *con) |
| 840 | { |
| 841 | dout("prepare_read_banner %p\n", con); |
| 842 | con->in_base_pos = 0; |
| 843 | } |
| 844 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 845 | static void prepare_read_connect(struct ceph_connection *con) |
| 846 | { |
| 847 | dout("prepare_read_connect %p\n", con); |
| 848 | con->in_base_pos = 0; |
| 849 | } |
| 850 | |
| 851 | static void prepare_read_ack(struct ceph_connection *con) |
| 852 | { |
| 853 | dout("prepare_read_ack %p\n", con); |
| 854 | con->in_base_pos = 0; |
| 855 | } |
| 856 | |
| 857 | static void prepare_read_tag(struct ceph_connection *con) |
| 858 | { |
| 859 | dout("prepare_read_tag %p\n", con); |
| 860 | con->in_base_pos = 0; |
| 861 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | * Prepare to read a message. |
| 866 | */ |
| 867 | static int prepare_read_message(struct ceph_connection *con) |
| 868 | { |
| 869 | dout("prepare_read_message %p\n", con); |
| 870 | BUG_ON(con->in_msg != NULL); |
| 871 | con->in_base_pos = 0; |
| 872 | con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0; |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | |
| 877 | static int read_partial(struct ceph_connection *con, |
| 878 | int *to, int size, void *object) |
| 879 | { |
| 880 | *to += size; |
| 881 | while (con->in_base_pos < *to) { |
| 882 | int left = *to - con->in_base_pos; |
| 883 | int have = size - left; |
| 884 | int ret = ceph_tcp_recvmsg(con->sock, object + have, left); |
| 885 | if (ret <= 0) |
| 886 | return ret; |
| 887 | con->in_base_pos += ret; |
| 888 | } |
| 889 | return 1; |
| 890 | } |
| 891 | |
| 892 | |
| 893 | /* |
| 894 | * Read all or part of the connect-side handshake on a new connection |
| 895 | */ |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 896 | static int read_partial_banner(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 897 | { |
| 898 | int ret, to = 0; |
| 899 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 900 | dout("read_partial_banner %p at %d\n", con, con->in_base_pos); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 901 | |
| 902 | /* peer's banner */ |
| 903 | ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner); |
| 904 | if (ret <= 0) |
| 905 | goto out; |
| 906 | ret = read_partial(con, &to, sizeof(con->actual_peer_addr), |
| 907 | &con->actual_peer_addr); |
| 908 | if (ret <= 0) |
| 909 | goto out; |
| 910 | ret = read_partial(con, &to, sizeof(con->peer_addr_for_me), |
| 911 | &con->peer_addr_for_me); |
| 912 | if (ret <= 0) |
| 913 | goto out; |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 914 | out: |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | static int read_partial_connect(struct ceph_connection *con) |
| 919 | { |
| 920 | int ret, to = 0; |
| 921 | |
| 922 | dout("read_partial_connect %p at %d\n", con, con->in_base_pos); |
| 923 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 924 | ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply); |
| 925 | if (ret <= 0) |
| 926 | goto out; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 927 | ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len), |
| 928 | con->auth_reply_buf); |
| 929 | if (ret <= 0) |
| 930 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 931 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 932 | dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n", |
| 933 | con, (int)con->in_reply.tag, |
| 934 | le32_to_cpu(con->in_reply.connect_seq), |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 935 | le32_to_cpu(con->in_reply.global_seq)); |
| 936 | out: |
| 937 | return ret; |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 938 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* |
| 942 | * Verify the hello banner looks okay. |
| 943 | */ |
| 944 | static int verify_hello(struct ceph_connection *con) |
| 945 | { |
| 946 | if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) { |
Sage Weil | 13e38c8 | 2009-10-09 16:36:34 -0700 | [diff] [blame] | 947 | pr_err("connect to %s got bad banner\n", |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 948 | pr_addr(&con->peer_addr.in_addr)); |
| 949 | con->error_msg = "protocol error, bad banner"; |
| 950 | return -1; |
| 951 | } |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | static bool addr_is_blank(struct sockaddr_storage *ss) |
| 956 | { |
| 957 | switch (ss->ss_family) { |
| 958 | case AF_INET: |
| 959 | return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0; |
| 960 | case AF_INET6: |
| 961 | return |
| 962 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 && |
| 963 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 && |
| 964 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 && |
| 965 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0; |
| 966 | } |
| 967 | return false; |
| 968 | } |
| 969 | |
| 970 | static int addr_port(struct sockaddr_storage *ss) |
| 971 | { |
| 972 | switch (ss->ss_family) { |
| 973 | case AF_INET: |
Sage Weil | f28bcfb | 2009-11-04 11:46:35 -0800 | [diff] [blame] | 974 | return ntohs(((struct sockaddr_in *)ss)->sin_port); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 975 | case AF_INET6: |
Sage Weil | f28bcfb | 2009-11-04 11:46:35 -0800 | [diff] [blame] | 976 | return ntohs(((struct sockaddr_in6 *)ss)->sin6_port); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 977 | } |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | static void addr_set_port(struct sockaddr_storage *ss, int p) |
| 982 | { |
| 983 | switch (ss->ss_family) { |
| 984 | case AF_INET: |
| 985 | ((struct sockaddr_in *)ss)->sin_port = htons(p); |
| 986 | case AF_INET6: |
| 987 | ((struct sockaddr_in6 *)ss)->sin6_port = htons(p); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Parse an ip[:port] list into an addr array. Use the default |
| 993 | * monitor port if a port isn't specified. |
| 994 | */ |
| 995 | int ceph_parse_ips(const char *c, const char *end, |
| 996 | struct ceph_entity_addr *addr, |
| 997 | int max_count, int *count) |
| 998 | { |
| 999 | int i; |
| 1000 | const char *p = c; |
| 1001 | |
| 1002 | dout("parse_ips on '%.*s'\n", (int)(end-c), c); |
| 1003 | for (i = 0; i < max_count; i++) { |
| 1004 | const char *ipend; |
| 1005 | struct sockaddr_storage *ss = &addr[i].in_addr; |
| 1006 | struct sockaddr_in *in4 = (void *)ss; |
| 1007 | struct sockaddr_in6 *in6 = (void *)ss; |
| 1008 | int port; |
| 1009 | |
| 1010 | memset(ss, 0, sizeof(*ss)); |
| 1011 | if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr, |
| 1012 | ',', &ipend)) { |
| 1013 | ss->ss_family = AF_INET; |
| 1014 | } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr, |
| 1015 | ',', &ipend)) { |
| 1016 | ss->ss_family = AF_INET6; |
| 1017 | } else { |
| 1018 | goto bad; |
| 1019 | } |
| 1020 | p = ipend; |
| 1021 | |
| 1022 | /* port? */ |
| 1023 | if (p < end && *p == ':') { |
| 1024 | port = 0; |
| 1025 | p++; |
| 1026 | while (p < end && *p >= '0' && *p <= '9') { |
| 1027 | port = (port * 10) + (*p - '0'); |
| 1028 | p++; |
| 1029 | } |
| 1030 | if (port > 65535 || port == 0) |
| 1031 | goto bad; |
| 1032 | } else { |
| 1033 | port = CEPH_MON_PORT; |
| 1034 | } |
| 1035 | |
| 1036 | addr_set_port(ss, port); |
| 1037 | |
| 1038 | dout("parse_ips got %s\n", pr_addr(ss)); |
| 1039 | |
| 1040 | if (p == end) |
| 1041 | break; |
| 1042 | if (*p != ',') |
| 1043 | goto bad; |
| 1044 | p++; |
| 1045 | } |
| 1046 | |
| 1047 | if (p != end) |
| 1048 | goto bad; |
| 1049 | |
| 1050 | if (count) |
| 1051 | *count = i + 1; |
| 1052 | return 0; |
| 1053 | |
| 1054 | bad: |
| 1055 | pr_err("parse_ips bad ip '%s'\n", c); |
| 1056 | return -EINVAL; |
| 1057 | } |
| 1058 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1059 | static int process_banner(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1060 | { |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1061 | dout("process_banner on %p\n", con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1062 | |
| 1063 | if (verify_hello(con) < 0) |
| 1064 | return -1; |
| 1065 | |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 1066 | ceph_decode_addr(&con->actual_peer_addr); |
| 1067 | ceph_decode_addr(&con->peer_addr_for_me); |
| 1068 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1069 | /* |
| 1070 | * Make sure the other end is who we wanted. note that the other |
| 1071 | * end may not yet know their ip address, so if it's 0.0.0.0, give |
| 1072 | * them the benefit of the doubt. |
| 1073 | */ |
Sage Weil | 103e2d3 | 2010-01-07 16:12:36 -0800 | [diff] [blame] | 1074 | if (memcmp(&con->peer_addr, &con->actual_peer_addr, |
| 1075 | sizeof(con->peer_addr)) != 0 && |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1076 | !(addr_is_blank(&con->actual_peer_addr.in_addr) && |
| 1077 | con->actual_peer_addr.nonce == con->peer_addr.nonce)) { |
Sage Weil | 103e2d3 | 2010-01-07 16:12:36 -0800 | [diff] [blame] | 1078 | pr_warning("wrong peer, want %s/%lld, got %s/%lld\n", |
| 1079 | pr_addr(&con->peer_addr.in_addr), |
| 1080 | le64_to_cpu(con->peer_addr.nonce), |
| 1081 | pr_addr(&con->actual_peer_addr.in_addr), |
| 1082 | le64_to_cpu(con->actual_peer_addr.nonce)); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 1083 | con->error_msg = "wrong peer at address"; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1084 | return -1; |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * did we learn our address? |
| 1089 | */ |
| 1090 | if (addr_is_blank(&con->msgr->inst.addr.in_addr)) { |
| 1091 | int port = addr_port(&con->msgr->inst.addr.in_addr); |
| 1092 | |
| 1093 | memcpy(&con->msgr->inst.addr.in_addr, |
| 1094 | &con->peer_addr_for_me.in_addr, |
| 1095 | sizeof(con->peer_addr_for_me.in_addr)); |
| 1096 | addr_set_port(&con->msgr->inst.addr.in_addr, port); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 1097 | encode_my_addr(con->msgr); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1098 | dout("process_banner learned my addr is %s\n", |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1099 | pr_addr(&con->msgr->inst.addr.in_addr)); |
| 1100 | } |
| 1101 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1102 | set_bit(NEGOTIATING, &con->state); |
| 1103 | prepare_read_connect(con); |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1107 | static void fail_protocol(struct ceph_connection *con) |
| 1108 | { |
| 1109 | reset_connection(con); |
| 1110 | set_bit(CLOSED, &con->state); /* in case there's queued work */ |
| 1111 | |
| 1112 | mutex_unlock(&con->mutex); |
| 1113 | if (con->ops->bad_proto) |
| 1114 | con->ops->bad_proto(con); |
| 1115 | mutex_lock(&con->mutex); |
| 1116 | } |
| 1117 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1118 | static int process_connect(struct ceph_connection *con) |
| 1119 | { |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1120 | u64 sup_feat = CEPH_FEATURE_SUPPORTED; |
| 1121 | u64 req_feat = CEPH_FEATURE_REQUIRED; |
| 1122 | u64 server_feat = le64_to_cpu(con->in_reply.features); |
| 1123 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1124 | dout("process_connect on %p tag %d\n", con, (int)con->in_tag); |
| 1125 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1126 | switch (con->in_reply.tag) { |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1127 | case CEPH_MSGR_TAG_FEATURES: |
| 1128 | pr_err("%s%lld %s feature set mismatch," |
| 1129 | " my %llx < server's %llx, missing %llx\n", |
| 1130 | ENTITY_NAME(con->peer_name), |
| 1131 | pr_addr(&con->peer_addr.in_addr), |
| 1132 | sup_feat, server_feat, server_feat & ~sup_feat); |
| 1133 | con->error_msg = "missing required protocol features"; |
| 1134 | fail_protocol(con); |
| 1135 | return -1; |
| 1136 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1137 | case CEPH_MSGR_TAG_BADPROTOVER: |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1138 | pr_err("%s%lld %s protocol version mismatch," |
| 1139 | " my %d != server's %d\n", |
| 1140 | ENTITY_NAME(con->peer_name), |
| 1141 | pr_addr(&con->peer_addr.in_addr), |
| 1142 | le32_to_cpu(con->out_connect.protocol_version), |
| 1143 | le32_to_cpu(con->in_reply.protocol_version)); |
| 1144 | con->error_msg = "protocol version mismatch"; |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1145 | fail_protocol(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1146 | return -1; |
| 1147 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1148 | case CEPH_MSGR_TAG_BADAUTHORIZER: |
| 1149 | con->auth_retry++; |
| 1150 | dout("process_connect %p got BADAUTHORIZER attempt %d\n", con, |
| 1151 | con->auth_retry); |
| 1152 | if (con->auth_retry == 2) { |
| 1153 | con->error_msg = "connect authorization failure"; |
| 1154 | reset_connection(con); |
| 1155 | set_bit(CLOSED, &con->state); |
| 1156 | return -1; |
| 1157 | } |
| 1158 | con->auth_retry = 1; |
| 1159 | prepare_write_connect(con->msgr, con, 0); |
Sage Weil | 63733a0 | 2010-03-15 15:47:22 -0700 | [diff] [blame] | 1160 | prepare_read_connect(con); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1161 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1162 | |
| 1163 | case CEPH_MSGR_TAG_RESETSESSION: |
| 1164 | /* |
| 1165 | * If we connected with a large connect_seq but the peer |
| 1166 | * has no record of a session with us (no connection, or |
| 1167 | * connect_seq == 0), they will send RESETSESION to indicate |
| 1168 | * that they must have reset their session, and may have |
| 1169 | * dropped messages. |
| 1170 | */ |
| 1171 | dout("process_connect got RESET peer seq %u\n", |
| 1172 | le32_to_cpu(con->in_connect.connect_seq)); |
| 1173 | pr_err("%s%lld %s connection reset\n", |
| 1174 | ENTITY_NAME(con->peer_name), |
| 1175 | pr_addr(&con->peer_addr.in_addr)); |
| 1176 | reset_connection(con); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1177 | prepare_write_connect(con->msgr, con, 0); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1178 | prepare_read_connect(con); |
| 1179 | |
| 1180 | /* Tell ceph about it. */ |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1181 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1182 | pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name)); |
| 1183 | if (con->ops->peer_reset) |
| 1184 | con->ops->peer_reset(con); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1185 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1186 | break; |
| 1187 | |
| 1188 | case CEPH_MSGR_TAG_RETRY_SESSION: |
| 1189 | /* |
| 1190 | * If we sent a smaller connect_seq than the peer has, try |
| 1191 | * again with a larger value. |
| 1192 | */ |
| 1193 | dout("process_connect got RETRY my seq = %u, peer_seq = %u\n", |
| 1194 | le32_to_cpu(con->out_connect.connect_seq), |
| 1195 | le32_to_cpu(con->in_connect.connect_seq)); |
| 1196 | con->connect_seq = le32_to_cpu(con->in_connect.connect_seq); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1197 | prepare_write_connect(con->msgr, con, 0); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1198 | prepare_read_connect(con); |
| 1199 | break; |
| 1200 | |
| 1201 | case CEPH_MSGR_TAG_RETRY_GLOBAL: |
| 1202 | /* |
| 1203 | * If we sent a smaller global_seq than the peer has, try |
| 1204 | * again with a larger value. |
| 1205 | */ |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1206 | dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n", |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1207 | con->peer_global_seq, |
| 1208 | le32_to_cpu(con->in_connect.global_seq)); |
| 1209 | get_global_seq(con->msgr, |
| 1210 | le32_to_cpu(con->in_connect.global_seq)); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1211 | prepare_write_connect(con->msgr, con, 0); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1212 | prepare_read_connect(con); |
| 1213 | break; |
| 1214 | |
| 1215 | case CEPH_MSGR_TAG_READY: |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1216 | if (req_feat & ~server_feat) { |
| 1217 | pr_err("%s%lld %s protocol feature mismatch," |
| 1218 | " my required %llx > server's %llx, need %llx\n", |
| 1219 | ENTITY_NAME(con->peer_name), |
| 1220 | pr_addr(&con->peer_addr.in_addr), |
| 1221 | req_feat, server_feat, req_feat & ~server_feat); |
| 1222 | con->error_msg = "missing required protocol features"; |
| 1223 | fail_protocol(con); |
| 1224 | return -1; |
| 1225 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1226 | clear_bit(CONNECTING, &con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1227 | con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq); |
| 1228 | con->connect_seq++; |
| 1229 | dout("process_connect got READY gseq %d cseq %d (%d)\n", |
| 1230 | con->peer_global_seq, |
| 1231 | le32_to_cpu(con->in_reply.connect_seq), |
| 1232 | con->connect_seq); |
| 1233 | WARN_ON(con->connect_seq != |
| 1234 | le32_to_cpu(con->in_reply.connect_seq)); |
Sage Weil | 92ac41d | 2009-12-14 14:56:56 -0800 | [diff] [blame] | 1235 | |
| 1236 | if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY) |
| 1237 | set_bit(LOSSYTX, &con->state); |
| 1238 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1239 | prepare_read_tag(con); |
| 1240 | break; |
| 1241 | |
| 1242 | case CEPH_MSGR_TAG_WAIT: |
| 1243 | /* |
| 1244 | * If there is a connection race (we are opening |
| 1245 | * connections to each other), one of us may just have |
| 1246 | * to WAIT. This shouldn't happen if we are the |
| 1247 | * client. |
| 1248 | */ |
| 1249 | pr_err("process_connect peer connecting WAIT\n"); |
| 1250 | |
| 1251 | default: |
| 1252 | pr_err("connect protocol error, will retry\n"); |
| 1253 | con->error_msg = "protocol error, garbage tag during connect"; |
| 1254 | return -1; |
| 1255 | } |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | |
| 1260 | /* |
| 1261 | * read (part of) an ack |
| 1262 | */ |
| 1263 | static int read_partial_ack(struct ceph_connection *con) |
| 1264 | { |
| 1265 | int to = 0; |
| 1266 | |
| 1267 | return read_partial(con, &to, sizeof(con->in_temp_ack), |
| 1268 | &con->in_temp_ack); |
| 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | /* |
| 1273 | * We can finally discard anything that's been acked. |
| 1274 | */ |
| 1275 | static void process_ack(struct ceph_connection *con) |
| 1276 | { |
| 1277 | struct ceph_msg *m; |
| 1278 | u64 ack = le64_to_cpu(con->in_temp_ack); |
| 1279 | u64 seq; |
| 1280 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1281 | while (!list_empty(&con->out_sent)) { |
| 1282 | m = list_first_entry(&con->out_sent, struct ceph_msg, |
| 1283 | list_head); |
| 1284 | seq = le64_to_cpu(m->hdr.seq); |
| 1285 | if (seq > ack) |
| 1286 | break; |
| 1287 | dout("got ack for seq %llu type %d at %p\n", seq, |
| 1288 | le16_to_cpu(m->hdr.type), m); |
| 1289 | ceph_msg_remove(m); |
| 1290 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1291 | prepare_read_tag(con); |
| 1292 | } |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1297 | static int read_partial_message_section(struct ceph_connection *con, |
| 1298 | struct kvec *section, unsigned int sec_len, |
| 1299 | u32 *crc) |
| 1300 | { |
| 1301 | int left; |
| 1302 | int ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1303 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1304 | BUG_ON(!section); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1305 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1306 | while (section->iov_len < sec_len) { |
| 1307 | BUG_ON(section->iov_base == NULL); |
| 1308 | left = sec_len - section->iov_len; |
| 1309 | ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base + |
| 1310 | section->iov_len, left); |
| 1311 | if (ret <= 0) |
| 1312 | return ret; |
| 1313 | section->iov_len += ret; |
| 1314 | if (section->iov_len == sec_len) |
| 1315 | *crc = crc32c(0, section->iov_base, |
| 1316 | section->iov_len); |
| 1317 | } |
| 1318 | |
| 1319 | return 1; |
| 1320 | } |
| 1321 | |
| 1322 | static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con, |
| 1323 | struct ceph_msg_header *hdr, |
| 1324 | int *skip); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1325 | /* |
| 1326 | * read (part of) a message. |
| 1327 | */ |
| 1328 | static int read_partial_message(struct ceph_connection *con) |
| 1329 | { |
| 1330 | struct ceph_msg *m = con->in_msg; |
| 1331 | void *p; |
| 1332 | int ret; |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 1333 | int to, left; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1334 | unsigned front_len, middle_len, data_len, data_off; |
| 1335 | int datacrc = con->msgr->nocrc; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1336 | int skip; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1337 | |
| 1338 | dout("read_partial_message con %p msg %p\n", con, m); |
| 1339 | |
| 1340 | /* header */ |
| 1341 | while (con->in_base_pos < sizeof(con->in_hdr)) { |
| 1342 | left = sizeof(con->in_hdr) - con->in_base_pos; |
| 1343 | ret = ceph_tcp_recvmsg(con->sock, |
| 1344 | (char *)&con->in_hdr + con->in_base_pos, |
| 1345 | left); |
| 1346 | if (ret <= 0) |
| 1347 | return ret; |
| 1348 | con->in_base_pos += ret; |
| 1349 | if (con->in_base_pos == sizeof(con->in_hdr)) { |
| 1350 | u32 crc = crc32c(0, (void *)&con->in_hdr, |
| 1351 | sizeof(con->in_hdr) - sizeof(con->in_hdr.crc)); |
| 1352 | if (crc != le32_to_cpu(con->in_hdr.crc)) { |
| 1353 | pr_err("read_partial_message bad hdr " |
| 1354 | " crc %u != expected %u\n", |
| 1355 | crc, con->in_hdr.crc); |
| 1356 | return -EBADMSG; |
| 1357 | } |
| 1358 | } |
| 1359 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1360 | front_len = le32_to_cpu(con->in_hdr.front_len); |
| 1361 | if (front_len > CEPH_MSG_MAX_FRONT_LEN) |
| 1362 | return -EIO; |
| 1363 | middle_len = le32_to_cpu(con->in_hdr.middle_len); |
| 1364 | if (middle_len > CEPH_MSG_MAX_DATA_LEN) |
| 1365 | return -EIO; |
| 1366 | data_len = le32_to_cpu(con->in_hdr.data_len); |
| 1367 | if (data_len > CEPH_MSG_MAX_DATA_LEN) |
| 1368 | return -EIO; |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 1369 | data_off = le16_to_cpu(con->in_hdr.data_off); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1370 | |
| 1371 | /* allocate message? */ |
| 1372 | if (!con->in_msg) { |
| 1373 | dout("got hdr type %d front %d data %d\n", con->in_hdr.type, |
| 1374 | con->in_hdr.front_len, con->in_hdr.data_len); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1375 | con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip); |
| 1376 | if (skip) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1377 | /* skip this message */ |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 1378 | dout("alloc_msg returned NULL, skipping message\n"); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1379 | con->in_base_pos = -front_len - middle_len - data_len - |
| 1380 | sizeof(m->footer); |
| 1381 | con->in_tag = CEPH_MSGR_TAG_READY; |
Sage Weil | 684be25 | 2010-04-21 20:45:59 -0700 | [diff] [blame^] | 1382 | con->in_seq++; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1383 | return 0; |
| 1384 | } |
| 1385 | if (IS_ERR(con->in_msg)) { |
| 1386 | ret = PTR_ERR(con->in_msg); |
| 1387 | con->in_msg = NULL; |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 1388 | con->error_msg = |
| 1389 | "error allocating memory for incoming message"; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1390 | return ret; |
| 1391 | } |
| 1392 | m = con->in_msg; |
| 1393 | m->front.iov_len = 0; /* haven't read it yet */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1394 | if (m->middle) |
| 1395 | m->middle->vec.iov_len = 0; |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 1396 | |
| 1397 | con->in_msg_pos.page = 0; |
| 1398 | con->in_msg_pos.page_pos = data_off & ~PAGE_MASK; |
| 1399 | con->in_msg_pos.data_pos = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | /* front */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1403 | ret = read_partial_message_section(con, &m->front, front_len, |
| 1404 | &con->in_front_crc); |
| 1405 | if (ret <= 0) |
| 1406 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1407 | |
| 1408 | /* middle */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1409 | if (m->middle) { |
| 1410 | ret = read_partial_message_section(con, &m->middle->vec, middle_len, |
| 1411 | &con->in_middle_crc); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1412 | if (ret <= 0) |
| 1413 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | /* (page) data */ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1417 | while (con->in_msg_pos.data_pos < data_len) { |
| 1418 | left = min((int)(data_len - con->in_msg_pos.data_pos), |
| 1419 | (int)(PAGE_SIZE - con->in_msg_pos.page_pos)); |
| 1420 | BUG_ON(m->pages == NULL); |
| 1421 | p = kmap(m->pages[con->in_msg_pos.page]); |
| 1422 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, |
| 1423 | left); |
| 1424 | if (ret > 0 && datacrc) |
| 1425 | con->in_data_crc = |
| 1426 | crc32c(con->in_data_crc, |
| 1427 | p + con->in_msg_pos.page_pos, ret); |
| 1428 | kunmap(m->pages[con->in_msg_pos.page]); |
| 1429 | if (ret <= 0) |
| 1430 | return ret; |
| 1431 | con->in_msg_pos.data_pos += ret; |
| 1432 | con->in_msg_pos.page_pos += ret; |
| 1433 | if (con->in_msg_pos.page_pos == PAGE_SIZE) { |
| 1434 | con->in_msg_pos.page_pos = 0; |
| 1435 | con->in_msg_pos.page++; |
| 1436 | } |
| 1437 | } |
| 1438 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1439 | /* footer */ |
| 1440 | to = sizeof(m->hdr) + sizeof(m->footer); |
| 1441 | while (con->in_base_pos < to) { |
| 1442 | left = to - con->in_base_pos; |
| 1443 | ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer + |
| 1444 | (con->in_base_pos - sizeof(m->hdr)), |
| 1445 | left); |
| 1446 | if (ret <= 0) |
| 1447 | return ret; |
| 1448 | con->in_base_pos += ret; |
| 1449 | } |
| 1450 | dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n", |
| 1451 | m, front_len, m->footer.front_crc, middle_len, |
| 1452 | m->footer.middle_crc, data_len, m->footer.data_crc); |
| 1453 | |
| 1454 | /* crc ok? */ |
| 1455 | if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) { |
| 1456 | pr_err("read_partial_message %p front crc %u != exp. %u\n", |
| 1457 | m, con->in_front_crc, m->footer.front_crc); |
| 1458 | return -EBADMSG; |
| 1459 | } |
| 1460 | if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) { |
| 1461 | pr_err("read_partial_message %p middle crc %u != exp %u\n", |
| 1462 | m, con->in_middle_crc, m->footer.middle_crc); |
| 1463 | return -EBADMSG; |
| 1464 | } |
| 1465 | if (datacrc && |
| 1466 | (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 && |
| 1467 | con->in_data_crc != le32_to_cpu(m->footer.data_crc)) { |
| 1468 | pr_err("read_partial_message %p data crc %u != exp. %u\n", m, |
| 1469 | con->in_data_crc, le32_to_cpu(m->footer.data_crc)); |
| 1470 | return -EBADMSG; |
| 1471 | } |
| 1472 | |
| 1473 | return 1; /* done! */ |
| 1474 | } |
| 1475 | |
| 1476 | /* |
| 1477 | * Process message. This happens in the worker thread. The callback should |
| 1478 | * be careful not to do anything that waits on other incoming messages or it |
| 1479 | * may deadlock. |
| 1480 | */ |
| 1481 | static void process_message(struct ceph_connection *con) |
| 1482 | { |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 1483 | struct ceph_msg *msg; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1484 | |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 1485 | msg = con->in_msg; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1486 | con->in_msg = NULL; |
| 1487 | |
| 1488 | /* if first message, set peer_name */ |
| 1489 | if (con->peer_name.type == 0) |
| 1490 | con->peer_name = msg->hdr.src.name; |
| 1491 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1492 | con->in_seq++; |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1493 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1494 | |
| 1495 | dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n", |
| 1496 | msg, le64_to_cpu(msg->hdr.seq), |
| 1497 | ENTITY_NAME(msg->hdr.src.name), |
| 1498 | le16_to_cpu(msg->hdr.type), |
| 1499 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), |
| 1500 | le32_to_cpu(msg->hdr.front_len), |
| 1501 | le32_to_cpu(msg->hdr.data_len), |
| 1502 | con->in_front_crc, con->in_middle_crc, con->in_data_crc); |
| 1503 | con->ops->dispatch(con, msg); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1504 | |
| 1505 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1506 | prepare_read_tag(con); |
| 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | /* |
| 1511 | * Write something to the socket. Called in a worker thread when the |
| 1512 | * socket appears to be writeable and we have something ready to send. |
| 1513 | */ |
| 1514 | static int try_write(struct ceph_connection *con) |
| 1515 | { |
| 1516 | struct ceph_messenger *msgr = con->msgr; |
| 1517 | int ret = 1; |
| 1518 | |
| 1519 | dout("try_write start %p state %lu nref %d\n", con, con->state, |
| 1520 | atomic_read(&con->nref)); |
| 1521 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1522 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1523 | more: |
| 1524 | dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes); |
| 1525 | |
| 1526 | /* open the socket first? */ |
| 1527 | if (con->sock == NULL) { |
| 1528 | /* |
| 1529 | * if we were STANDBY and are reconnecting _this_ |
| 1530 | * connection, bump connect_seq now. Always bump |
| 1531 | * global_seq. |
| 1532 | */ |
| 1533 | if (test_and_clear_bit(STANDBY, &con->state)) |
| 1534 | con->connect_seq++; |
| 1535 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1536 | prepare_write_banner(msgr, con); |
| 1537 | prepare_write_connect(msgr, con, 1); |
| 1538 | prepare_read_banner(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1539 | set_bit(CONNECTING, &con->state); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1540 | clear_bit(NEGOTIATING, &con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1541 | |
Sage Weil | cf3e5c4 | 2009-12-11 09:48:05 -0800 | [diff] [blame] | 1542 | BUG_ON(con->in_msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1543 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 1544 | dout("try_write initiating connect on %p new state %lu\n", |
| 1545 | con, con->state); |
| 1546 | con->sock = ceph_tcp_connect(con); |
| 1547 | if (IS_ERR(con->sock)) { |
| 1548 | con->sock = NULL; |
| 1549 | con->error_msg = "connect error"; |
| 1550 | ret = -1; |
| 1551 | goto out; |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | more_kvec: |
| 1556 | /* kvec data queued? */ |
| 1557 | if (con->out_skip) { |
| 1558 | ret = write_partial_skip(con); |
| 1559 | if (ret <= 0) |
| 1560 | goto done; |
| 1561 | if (ret < 0) { |
| 1562 | dout("try_write write_partial_skip err %d\n", ret); |
| 1563 | goto done; |
| 1564 | } |
| 1565 | } |
| 1566 | if (con->out_kvec_left) { |
| 1567 | ret = write_partial_kvec(con); |
| 1568 | if (ret <= 0) |
| 1569 | goto done; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | /* msg pages? */ |
| 1573 | if (con->out_msg) { |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 1574 | if (con->out_msg_done) { |
| 1575 | ceph_msg_put(con->out_msg); |
| 1576 | con->out_msg = NULL; /* we're done with this one */ |
| 1577 | goto do_next; |
| 1578 | } |
| 1579 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1580 | ret = write_partial_msg_pages(con); |
| 1581 | if (ret == 1) |
| 1582 | goto more_kvec; /* we need to send the footer, too! */ |
| 1583 | if (ret == 0) |
| 1584 | goto done; |
| 1585 | if (ret < 0) { |
| 1586 | dout("try_write write_partial_msg_pages err %d\n", |
| 1587 | ret); |
| 1588 | goto done; |
| 1589 | } |
| 1590 | } |
| 1591 | |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 1592 | do_next: |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1593 | if (!test_bit(CONNECTING, &con->state)) { |
| 1594 | /* is anything else pending? */ |
| 1595 | if (!list_empty(&con->out_queue)) { |
| 1596 | prepare_write_message(con); |
| 1597 | goto more; |
| 1598 | } |
| 1599 | if (con->in_seq > con->in_seq_acked) { |
| 1600 | prepare_write_ack(con); |
| 1601 | goto more; |
| 1602 | } |
| 1603 | if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) { |
| 1604 | prepare_write_keepalive(con); |
| 1605 | goto more; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | /* Nothing to do! */ |
| 1610 | clear_bit(WRITE_PENDING, &con->state); |
| 1611 | dout("try_write nothing else to write.\n"); |
| 1612 | done: |
| 1613 | ret = 0; |
| 1614 | out: |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1615 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1616 | dout("try_write done on %p\n", con); |
| 1617 | return ret; |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | /* |
| 1623 | * Read what we can from the socket. |
| 1624 | */ |
| 1625 | static int try_read(struct ceph_connection *con) |
| 1626 | { |
| 1627 | struct ceph_messenger *msgr; |
| 1628 | int ret = -1; |
| 1629 | |
| 1630 | if (!con->sock) |
| 1631 | return 0; |
| 1632 | |
| 1633 | if (test_bit(STANDBY, &con->state)) |
| 1634 | return 0; |
| 1635 | |
| 1636 | dout("try_read start on %p\n", con); |
| 1637 | msgr = con->msgr; |
| 1638 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1639 | mutex_lock(&con->mutex); |
| 1640 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1641 | more: |
| 1642 | dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag, |
| 1643 | con->in_base_pos); |
| 1644 | if (test_bit(CONNECTING, &con->state)) { |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1645 | if (!test_bit(NEGOTIATING, &con->state)) { |
| 1646 | dout("try_read connecting\n"); |
| 1647 | ret = read_partial_banner(con); |
| 1648 | if (ret <= 0) |
| 1649 | goto done; |
| 1650 | if (process_banner(con) < 0) { |
| 1651 | ret = -1; |
| 1652 | goto out; |
| 1653 | } |
| 1654 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1655 | ret = read_partial_connect(con); |
| 1656 | if (ret <= 0) |
| 1657 | goto done; |
| 1658 | if (process_connect(con) < 0) { |
| 1659 | ret = -1; |
| 1660 | goto out; |
| 1661 | } |
| 1662 | goto more; |
| 1663 | } |
| 1664 | |
| 1665 | if (con->in_base_pos < 0) { |
| 1666 | /* |
| 1667 | * skipping + discarding content. |
| 1668 | * |
| 1669 | * FIXME: there must be a better way to do this! |
| 1670 | */ |
| 1671 | static char buf[1024]; |
| 1672 | int skip = min(1024, -con->in_base_pos); |
| 1673 | dout("skipping %d / %d bytes\n", skip, -con->in_base_pos); |
| 1674 | ret = ceph_tcp_recvmsg(con->sock, buf, skip); |
| 1675 | if (ret <= 0) |
| 1676 | goto done; |
| 1677 | con->in_base_pos += ret; |
| 1678 | if (con->in_base_pos) |
| 1679 | goto more; |
| 1680 | } |
| 1681 | if (con->in_tag == CEPH_MSGR_TAG_READY) { |
| 1682 | /* |
| 1683 | * what's next? |
| 1684 | */ |
| 1685 | ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1); |
| 1686 | if (ret <= 0) |
| 1687 | goto done; |
| 1688 | dout("try_read got tag %d\n", (int)con->in_tag); |
| 1689 | switch (con->in_tag) { |
| 1690 | case CEPH_MSGR_TAG_MSG: |
| 1691 | prepare_read_message(con); |
| 1692 | break; |
| 1693 | case CEPH_MSGR_TAG_ACK: |
| 1694 | prepare_read_ack(con); |
| 1695 | break; |
| 1696 | case CEPH_MSGR_TAG_CLOSE: |
| 1697 | set_bit(CLOSED, &con->state); /* fixme */ |
| 1698 | goto done; |
| 1699 | default: |
| 1700 | goto bad_tag; |
| 1701 | } |
| 1702 | } |
| 1703 | if (con->in_tag == CEPH_MSGR_TAG_MSG) { |
| 1704 | ret = read_partial_message(con); |
| 1705 | if (ret <= 0) { |
| 1706 | switch (ret) { |
| 1707 | case -EBADMSG: |
| 1708 | con->error_msg = "bad crc"; |
| 1709 | ret = -EIO; |
| 1710 | goto out; |
| 1711 | case -EIO: |
| 1712 | con->error_msg = "io error"; |
| 1713 | goto out; |
| 1714 | default: |
| 1715 | goto done; |
| 1716 | } |
| 1717 | } |
| 1718 | if (con->in_tag == CEPH_MSGR_TAG_READY) |
| 1719 | goto more; |
| 1720 | process_message(con); |
| 1721 | goto more; |
| 1722 | } |
| 1723 | if (con->in_tag == CEPH_MSGR_TAG_ACK) { |
| 1724 | ret = read_partial_ack(con); |
| 1725 | if (ret <= 0) |
| 1726 | goto done; |
| 1727 | process_ack(con); |
| 1728 | goto more; |
| 1729 | } |
| 1730 | |
| 1731 | done: |
| 1732 | ret = 0; |
| 1733 | out: |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1734 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1735 | dout("try_read done on %p\n", con); |
| 1736 | return ret; |
| 1737 | |
| 1738 | bad_tag: |
| 1739 | pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag); |
| 1740 | con->error_msg = "protocol error, garbage tag"; |
| 1741 | ret = -1; |
| 1742 | goto out; |
| 1743 | } |
| 1744 | |
| 1745 | |
| 1746 | /* |
| 1747 | * Atomically queue work on a connection. Bump @con reference to |
| 1748 | * avoid races with connection teardown. |
| 1749 | * |
| 1750 | * There is some trickery going on with QUEUED and BUSY because we |
| 1751 | * only want a _single_ thread operating on each connection at any |
| 1752 | * point in time, but we want to use all available CPUs. |
| 1753 | * |
| 1754 | * The worker thread only proceeds if it can atomically set BUSY. It |
| 1755 | * clears QUEUED and does it's thing. When it thinks it's done, it |
| 1756 | * clears BUSY, then rechecks QUEUED.. if it's set again, it loops |
| 1757 | * (tries again to set BUSY). |
| 1758 | * |
| 1759 | * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we |
| 1760 | * try to queue work. If that fails (work is already queued, or BUSY) |
| 1761 | * we give up (work also already being done or is queued) but leave QUEUED |
| 1762 | * set so that the worker thread will loop if necessary. |
| 1763 | */ |
| 1764 | static void queue_con(struct ceph_connection *con) |
| 1765 | { |
| 1766 | if (test_bit(DEAD, &con->state)) { |
| 1767 | dout("queue_con %p ignoring: DEAD\n", |
| 1768 | con); |
| 1769 | return; |
| 1770 | } |
| 1771 | |
| 1772 | if (!con->ops->get(con)) { |
| 1773 | dout("queue_con %p ref count 0\n", con); |
| 1774 | return; |
| 1775 | } |
| 1776 | |
| 1777 | set_bit(QUEUED, &con->state); |
| 1778 | if (test_bit(BUSY, &con->state)) { |
| 1779 | dout("queue_con %p - already BUSY\n", con); |
| 1780 | con->ops->put(con); |
| 1781 | } else if (!queue_work(ceph_msgr_wq, &con->work.work)) { |
| 1782 | dout("queue_con %p - already queued\n", con); |
| 1783 | con->ops->put(con); |
| 1784 | } else { |
| 1785 | dout("queue_con %p\n", con); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * Do some work on a connection. Drop a connection ref when we're done. |
| 1791 | */ |
| 1792 | static void con_work(struct work_struct *work) |
| 1793 | { |
| 1794 | struct ceph_connection *con = container_of(work, struct ceph_connection, |
| 1795 | work.work); |
| 1796 | int backoff = 0; |
| 1797 | |
| 1798 | more: |
| 1799 | if (test_and_set_bit(BUSY, &con->state) != 0) { |
| 1800 | dout("con_work %p BUSY already set\n", con); |
| 1801 | goto out; |
| 1802 | } |
| 1803 | dout("con_work %p start, clearing QUEUED\n", con); |
| 1804 | clear_bit(QUEUED, &con->state); |
| 1805 | |
| 1806 | if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */ |
| 1807 | dout("con_work CLOSED\n"); |
| 1808 | con_close_socket(con); |
| 1809 | goto done; |
| 1810 | } |
| 1811 | if (test_and_clear_bit(OPENING, &con->state)) { |
| 1812 | /* reopen w/ new peer */ |
| 1813 | dout("con_work OPENING\n"); |
| 1814 | con_close_socket(con); |
| 1815 | } |
| 1816 | |
| 1817 | if (test_and_clear_bit(SOCK_CLOSED, &con->state) || |
| 1818 | try_read(con) < 0 || |
| 1819 | try_write(con) < 0) { |
| 1820 | backoff = 1; |
| 1821 | ceph_fault(con); /* error/fault path */ |
| 1822 | } |
| 1823 | |
| 1824 | done: |
| 1825 | clear_bit(BUSY, &con->state); |
| 1826 | dout("con->state=%lu\n", con->state); |
| 1827 | if (test_bit(QUEUED, &con->state)) { |
Sage Weil | e2663ab | 2010-02-16 22:01:03 -0800 | [diff] [blame] | 1828 | if (!backoff || test_bit(OPENING, &con->state)) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1829 | dout("con_work %p QUEUED reset, looping\n", con); |
| 1830 | goto more; |
| 1831 | } |
| 1832 | dout("con_work %p QUEUED reset, but just faulted\n", con); |
| 1833 | clear_bit(QUEUED, &con->state); |
| 1834 | } |
| 1835 | dout("con_work %p done\n", con); |
| 1836 | |
| 1837 | out: |
| 1838 | con->ops->put(con); |
| 1839 | } |
| 1840 | |
| 1841 | |
| 1842 | /* |
| 1843 | * Generic error/fault handler. A retry mechanism is used with |
| 1844 | * exponential backoff |
| 1845 | */ |
| 1846 | static void ceph_fault(struct ceph_connection *con) |
| 1847 | { |
| 1848 | pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name), |
| 1849 | pr_addr(&con->peer_addr.in_addr), con->error_msg); |
| 1850 | dout("fault %p state %lu to peer %s\n", |
| 1851 | con, con->state, pr_addr(&con->peer_addr.in_addr)); |
| 1852 | |
| 1853 | if (test_bit(LOSSYTX, &con->state)) { |
| 1854 | dout("fault on LOSSYTX channel\n"); |
| 1855 | goto out; |
| 1856 | } |
| 1857 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1858 | mutex_lock(&con->mutex); |
Sage Weil | 91e45ce | 2010-02-15 12:05:09 -0800 | [diff] [blame] | 1859 | if (test_bit(CLOSED, &con->state)) |
| 1860 | goto out_unlock; |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1861 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1862 | con_close_socket(con); |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 1863 | |
| 1864 | if (con->in_msg) { |
| 1865 | ceph_msg_put(con->in_msg); |
| 1866 | con->in_msg = NULL; |
| 1867 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1868 | |
Sage Weil | e80a52d | 2010-02-25 12:40:45 -0800 | [diff] [blame] | 1869 | /* Requeue anything that hasn't been acked */ |
| 1870 | list_splice_init(&con->out_sent, &con->out_queue); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 1871 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1872 | /* If there are no messages in the queue, place the connection |
| 1873 | * in a STANDBY state (i.e., don't try to reconnect just yet). */ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1874 | if (list_empty(&con->out_queue) && !con->out_keepalive_pending) { |
| 1875 | dout("fault setting STANDBY\n"); |
| 1876 | set_bit(STANDBY, &con->state); |
Sage Weil | e80a52d | 2010-02-25 12:40:45 -0800 | [diff] [blame] | 1877 | } else { |
| 1878 | /* retry after a delay. */ |
| 1879 | if (con->delay == 0) |
| 1880 | con->delay = BASE_DELAY_INTERVAL; |
| 1881 | else if (con->delay < MAX_DELAY_INTERVAL) |
| 1882 | con->delay *= 2; |
| 1883 | dout("fault queueing %p delay %lu\n", con, con->delay); |
| 1884 | con->ops->get(con); |
| 1885 | if (queue_delayed_work(ceph_msgr_wq, &con->work, |
| 1886 | round_jiffies_relative(con->delay)) == 0) |
| 1887 | con->ops->put(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
Sage Weil | 91e45ce | 2010-02-15 12:05:09 -0800 | [diff] [blame] | 1890 | out_unlock: |
| 1891 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1892 | out: |
Sage Weil | 161fd65 | 2010-02-25 12:38:57 -0800 | [diff] [blame] | 1893 | /* |
| 1894 | * in case we faulted due to authentication, invalidate our |
| 1895 | * current tickets so that we can get new ones. |
| 1896 | */ |
| 1897 | if (con->auth_retry && con->ops->invalidate_authorizer) { |
| 1898 | dout("calling invalidate_authorizer()\n"); |
| 1899 | con->ops->invalidate_authorizer(con); |
| 1900 | } |
| 1901 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1902 | if (con->ops->fault) |
| 1903 | con->ops->fault(con); |
| 1904 | } |
| 1905 | |
| 1906 | |
| 1907 | |
| 1908 | /* |
| 1909 | * create a new messenger instance |
| 1910 | */ |
| 1911 | struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr) |
| 1912 | { |
| 1913 | struct ceph_messenger *msgr; |
| 1914 | |
| 1915 | msgr = kzalloc(sizeof(*msgr), GFP_KERNEL); |
| 1916 | if (msgr == NULL) |
| 1917 | return ERR_PTR(-ENOMEM); |
| 1918 | |
| 1919 | spin_lock_init(&msgr->global_seq_lock); |
| 1920 | |
| 1921 | /* the zero page is needed if a request is "canceled" while the message |
| 1922 | * is being written over the socket */ |
| 1923 | msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 1924 | if (!msgr->zero_page) { |
| 1925 | kfree(msgr); |
| 1926 | return ERR_PTR(-ENOMEM); |
| 1927 | } |
| 1928 | kmap(msgr->zero_page); |
| 1929 | |
| 1930 | if (myaddr) |
| 1931 | msgr->inst.addr = *myaddr; |
| 1932 | |
| 1933 | /* select a random nonce */ |
Sage Weil | ac8839d | 2010-01-27 14:28:10 -0800 | [diff] [blame] | 1934 | msgr->inst.addr.type = 0; |
Sage Weil | 103e2d3 | 2010-01-07 16:12:36 -0800 | [diff] [blame] | 1935 | get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce)); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 1936 | encode_my_addr(msgr); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1937 | |
| 1938 | dout("messenger_create %p\n", msgr); |
| 1939 | return msgr; |
| 1940 | } |
| 1941 | |
| 1942 | void ceph_messenger_destroy(struct ceph_messenger *msgr) |
| 1943 | { |
| 1944 | dout("destroy %p\n", msgr); |
| 1945 | kunmap(msgr->zero_page); |
| 1946 | __free_page(msgr->zero_page); |
| 1947 | kfree(msgr); |
| 1948 | dout("destroyed messenger %p\n", msgr); |
| 1949 | } |
| 1950 | |
| 1951 | /* |
| 1952 | * Queue up an outgoing message on the given connection. |
| 1953 | */ |
| 1954 | void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg) |
| 1955 | { |
| 1956 | if (test_bit(CLOSED, &con->state)) { |
| 1957 | dout("con_send %p closed, dropping %p\n", con, msg); |
| 1958 | ceph_msg_put(msg); |
| 1959 | return; |
| 1960 | } |
| 1961 | |
| 1962 | /* set src+dst */ |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 1963 | msg->hdr.src.name = con->msgr->inst.name; |
| 1964 | msg->hdr.src.addr = con->msgr->my_enc_addr; |
| 1965 | msg->hdr.orig_src = msg->hdr.src; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1966 | |
Sage Weil | 3ca02ef | 2010-03-01 15:25:00 -0800 | [diff] [blame] | 1967 | BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len)); |
| 1968 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1969 | /* queue */ |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1970 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1971 | BUG_ON(!list_empty(&msg->list_head)); |
| 1972 | list_add_tail(&msg->list_head, &con->out_queue); |
| 1973 | dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg, |
| 1974 | ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type), |
| 1975 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), |
| 1976 | le32_to_cpu(msg->hdr.front_len), |
| 1977 | le32_to_cpu(msg->hdr.middle_len), |
| 1978 | le32_to_cpu(msg->hdr.data_len)); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1979 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1980 | |
| 1981 | /* if there wasn't anything waiting to send before, queue |
| 1982 | * new work */ |
| 1983 | if (test_and_set_bit(WRITE_PENDING, &con->state) == 0) |
| 1984 | queue_con(con); |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * Revoke a message that was previously queued for send |
| 1989 | */ |
| 1990 | void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg) |
| 1991 | { |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1992 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1993 | if (!list_empty(&msg->list_head)) { |
| 1994 | dout("con_revoke %p msg %p\n", con, msg); |
| 1995 | list_del_init(&msg->list_head); |
| 1996 | ceph_msg_put(msg); |
| 1997 | msg->hdr.seq = 0; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 1998 | if (con->out_msg == msg) { |
| 1999 | ceph_msg_put(con->out_msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2000 | con->out_msg = NULL; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 2001 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2002 | if (con->out_kvec_is_msg) { |
| 2003 | con->out_skip = con->out_kvec_bytes; |
| 2004 | con->out_kvec_is_msg = false; |
| 2005 | } |
| 2006 | } else { |
| 2007 | dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg); |
| 2008 | } |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2009 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | /* |
Yehuda Sadeh | 0d59ab8 | 2010-01-13 17:03:23 -0800 | [diff] [blame] | 2013 | * Revoke a message that we may be reading data into |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2014 | */ |
Yehuda Sadeh | 0d59ab8 | 2010-01-13 17:03:23 -0800 | [diff] [blame] | 2015 | void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg) |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2016 | { |
| 2017 | mutex_lock(&con->mutex); |
Yehuda Sadeh | 0d59ab8 | 2010-01-13 17:03:23 -0800 | [diff] [blame] | 2018 | if (con->in_msg && con->in_msg == msg) { |
| 2019 | unsigned front_len = le32_to_cpu(con->in_hdr.front_len); |
| 2020 | unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len); |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2021 | unsigned data_len = le32_to_cpu(con->in_hdr.data_len); |
| 2022 | |
| 2023 | /* skip rest of message */ |
Yehuda Sadeh | 0d59ab8 | 2010-01-13 17:03:23 -0800 | [diff] [blame] | 2024 | dout("con_revoke_pages %p msg %p revoked\n", con, msg); |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2025 | con->in_base_pos = con->in_base_pos - |
| 2026 | sizeof(struct ceph_msg_header) - |
Yehuda Sadeh | 0d59ab8 | 2010-01-13 17:03:23 -0800 | [diff] [blame] | 2027 | front_len - |
| 2028 | middle_len - |
| 2029 | data_len - |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2030 | sizeof(struct ceph_msg_footer); |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2031 | ceph_msg_put(con->in_msg); |
| 2032 | con->in_msg = NULL; |
| 2033 | con->in_tag = CEPH_MSGR_TAG_READY; |
Sage Weil | 684be25 | 2010-04-21 20:45:59 -0700 | [diff] [blame^] | 2034 | con->in_seq++; |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2035 | } else { |
| 2036 | dout("con_revoke_pages %p msg %p pages %p no-op\n", |
Yehuda Sadeh | 0d59ab8 | 2010-01-13 17:03:23 -0800 | [diff] [blame] | 2037 | con, con->in_msg, msg); |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2038 | } |
| 2039 | mutex_unlock(&con->mutex); |
| 2040 | } |
| 2041 | |
| 2042 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2043 | * Queue a keepalive byte to ensure the tcp connection is alive. |
| 2044 | */ |
| 2045 | void ceph_con_keepalive(struct ceph_connection *con) |
| 2046 | { |
| 2047 | if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 && |
| 2048 | test_and_set_bit(WRITE_PENDING, &con->state) == 0) |
| 2049 | queue_con(con); |
| 2050 | } |
| 2051 | |
| 2052 | |
| 2053 | /* |
| 2054 | * construct a new message with given type, size |
| 2055 | * the new msg has a ref count of 1. |
| 2056 | */ |
| 2057 | struct ceph_msg *ceph_msg_new(int type, int front_len, |
| 2058 | int page_len, int page_off, struct page **pages) |
| 2059 | { |
| 2060 | struct ceph_msg *m; |
| 2061 | |
| 2062 | m = kmalloc(sizeof(*m), GFP_NOFS); |
| 2063 | if (m == NULL) |
| 2064 | goto out; |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2065 | kref_init(&m->kref); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2066 | INIT_LIST_HEAD(&m->list_head); |
| 2067 | |
| 2068 | m->hdr.type = cpu_to_le16(type); |
| 2069 | m->hdr.front_len = cpu_to_le32(front_len); |
| 2070 | m->hdr.middle_len = 0; |
| 2071 | m->hdr.data_len = cpu_to_le32(page_len); |
| 2072 | m->hdr.data_off = cpu_to_le16(page_off); |
| 2073 | m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); |
| 2074 | m->footer.front_crc = 0; |
| 2075 | m->footer.middle_crc = 0; |
| 2076 | m->footer.data_crc = 0; |
| 2077 | m->front_max = front_len; |
| 2078 | m->front_is_vmalloc = false; |
| 2079 | m->more_to_follow = false; |
| 2080 | m->pool = NULL; |
| 2081 | |
| 2082 | /* front */ |
| 2083 | if (front_len) { |
| 2084 | if (front_len > PAGE_CACHE_SIZE) { |
| 2085 | m->front.iov_base = __vmalloc(front_len, GFP_NOFS, |
| 2086 | PAGE_KERNEL); |
| 2087 | m->front_is_vmalloc = true; |
| 2088 | } else { |
| 2089 | m->front.iov_base = kmalloc(front_len, GFP_NOFS); |
| 2090 | } |
| 2091 | if (m->front.iov_base == NULL) { |
| 2092 | pr_err("msg_new can't allocate %d bytes\n", |
| 2093 | front_len); |
| 2094 | goto out2; |
| 2095 | } |
| 2096 | } else { |
| 2097 | m->front.iov_base = NULL; |
| 2098 | } |
| 2099 | m->front.iov_len = front_len; |
| 2100 | |
| 2101 | /* middle */ |
| 2102 | m->middle = NULL; |
| 2103 | |
| 2104 | /* data */ |
| 2105 | m->nr_pages = calc_pages_for(page_off, page_len); |
| 2106 | m->pages = pages; |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 2107 | m->pagelist = NULL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2108 | |
| 2109 | dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len, |
| 2110 | m->nr_pages); |
| 2111 | return m; |
| 2112 | |
| 2113 | out2: |
| 2114 | ceph_msg_put(m); |
| 2115 | out: |
| 2116 | pr_err("msg_new can't create type %d len %d\n", type, front_len); |
| 2117 | return ERR_PTR(-ENOMEM); |
| 2118 | } |
| 2119 | |
| 2120 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2121 | * Allocate "middle" portion of a message, if it is needed and wasn't |
| 2122 | * allocated by alloc_msg. This allows us to read a small fixed-size |
| 2123 | * per-type header in the front and then gracefully fail (i.e., |
| 2124 | * propagate the error to the caller based on info in the front) when |
| 2125 | * the middle is too large. |
| 2126 | */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2127 | static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2128 | { |
| 2129 | int type = le16_to_cpu(msg->hdr.type); |
| 2130 | int middle_len = le32_to_cpu(msg->hdr.middle_len); |
| 2131 | |
| 2132 | dout("alloc_middle %p type %d %s middle_len %d\n", msg, type, |
| 2133 | ceph_msg_type_name(type), middle_len); |
| 2134 | BUG_ON(!middle_len); |
| 2135 | BUG_ON(msg->middle); |
| 2136 | |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame] | 2137 | msg->middle = ceph_buffer_new(middle_len, GFP_NOFS); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2138 | if (!msg->middle) |
| 2139 | return -ENOMEM; |
| 2140 | return 0; |
| 2141 | } |
| 2142 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2143 | /* |
| 2144 | * Generic message allocator, for incoming messages. |
| 2145 | */ |
| 2146 | static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con, |
| 2147 | struct ceph_msg_header *hdr, |
| 2148 | int *skip) |
| 2149 | { |
| 2150 | int type = le16_to_cpu(hdr->type); |
| 2151 | int front_len = le32_to_cpu(hdr->front_len); |
| 2152 | int middle_len = le32_to_cpu(hdr->middle_len); |
| 2153 | struct ceph_msg *msg = NULL; |
| 2154 | int ret; |
| 2155 | |
| 2156 | if (con->ops->alloc_msg) { |
Yehuda Sadeh | 0547a9b | 2010-01-11 14:47:13 -0800 | [diff] [blame] | 2157 | mutex_unlock(&con->mutex); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2158 | msg = con->ops->alloc_msg(con, hdr, skip); |
Yehuda Sadeh | 0547a9b | 2010-01-11 14:47:13 -0800 | [diff] [blame] | 2159 | mutex_lock(&con->mutex); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2160 | if (IS_ERR(msg)) |
| 2161 | return msg; |
| 2162 | |
| 2163 | if (*skip) |
| 2164 | return NULL; |
| 2165 | } |
| 2166 | if (!msg) { |
| 2167 | *skip = 0; |
| 2168 | msg = ceph_msg_new(type, front_len, 0, 0, NULL); |
| 2169 | if (!msg) { |
| 2170 | pr_err("unable to allocate msg type %d len %d\n", |
| 2171 | type, front_len); |
| 2172 | return ERR_PTR(-ENOMEM); |
| 2173 | } |
| 2174 | } |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 2175 | memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr)); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2176 | |
| 2177 | if (middle_len) { |
| 2178 | ret = ceph_alloc_middle(con, msg); |
| 2179 | |
| 2180 | if (ret < 0) { |
| 2181 | ceph_msg_put(msg); |
| 2182 | return msg; |
| 2183 | } |
| 2184 | } |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 2185 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2186 | return msg; |
| 2187 | } |
| 2188 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2189 | |
| 2190 | /* |
| 2191 | * Free a generically kmalloc'd message. |
| 2192 | */ |
| 2193 | void ceph_msg_kfree(struct ceph_msg *m) |
| 2194 | { |
| 2195 | dout("msg_kfree %p\n", m); |
| 2196 | if (m->front_is_vmalloc) |
| 2197 | vfree(m->front.iov_base); |
| 2198 | else |
| 2199 | kfree(m->front.iov_base); |
| 2200 | kfree(m); |
| 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * Drop a msg ref. Destroy as needed. |
| 2205 | */ |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2206 | void ceph_msg_last_put(struct kref *kref) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2207 | { |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2208 | struct ceph_msg *m = container_of(kref, struct ceph_msg, kref); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2209 | |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2210 | dout("ceph_msg_put last one on %p\n", m); |
| 2211 | WARN_ON(!list_empty(&m->list_head)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2212 | |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2213 | /* drop middle, data, if any */ |
| 2214 | if (m->middle) { |
| 2215 | ceph_buffer_put(m->middle); |
| 2216 | m->middle = NULL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2217 | } |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2218 | m->nr_pages = 0; |
| 2219 | m->pages = NULL; |
| 2220 | |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 2221 | if (m->pagelist) { |
| 2222 | ceph_pagelist_release(m->pagelist); |
| 2223 | kfree(m->pagelist); |
| 2224 | m->pagelist = NULL; |
| 2225 | } |
| 2226 | |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2227 | if (m->pool) |
| 2228 | ceph_msgpool_put(m->pool, m); |
| 2229 | else |
| 2230 | ceph_msg_kfree(m); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2231 | } |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2232 | |
| 2233 | void ceph_msg_dump(struct ceph_msg *msg) |
| 2234 | { |
| 2235 | pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg, |
| 2236 | msg->front_max, msg->nr_pages); |
| 2237 | print_hex_dump(KERN_DEBUG, "header: ", |
| 2238 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2239 | &msg->hdr, sizeof(msg->hdr), true); |
| 2240 | print_hex_dump(KERN_DEBUG, " front: ", |
| 2241 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2242 | msg->front.iov_base, msg->front.iov_len, true); |
| 2243 | if (msg->middle) |
| 2244 | print_hex_dump(KERN_DEBUG, "middle: ", |
| 2245 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2246 | msg->middle->vec.iov_base, |
| 2247 | msg->middle->vec.iov_len, true); |
| 2248 | print_hex_dump(KERN_DEBUG, "footer: ", |
| 2249 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2250 | &msg->footer, sizeof(msg->footer), true); |
| 2251 | } |