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