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