Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 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> |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 12 | #include <linux/bio.h> |
| 13 | #include <linux/blkdev.h> |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 14 | #include <linux/dns_resolver.h> |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 15 | #include <net/tcp.h> |
| 16 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 17 | #include <linux/ceph/libceph.h> |
| 18 | #include <linux/ceph/messenger.h> |
| 19 | #include <linux/ceph/decode.h> |
| 20 | #include <linux/ceph/pagelist.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 21 | #include <linux/export.h> |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Ceph uses the messenger to exchange ceph_msg messages with other |
| 25 | * hosts in the system. The messenger provides ordered and reliable |
| 26 | * delivery. We tolerate TCP disconnects by reconnecting (with |
| 27 | * exponential backoff) in the case of a fault (disconnection, bad |
| 28 | * crc, protocol error). Acks allow sent messages to be discarded by |
| 29 | * the sender. |
| 30 | */ |
| 31 | |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 32 | /* State values for ceph_connection->sock_state; NEW is assumed to be 0 */ |
| 33 | |
| 34 | #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */ |
| 35 | #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */ |
| 36 | #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */ |
| 37 | #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */ |
| 38 | #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */ |
| 39 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 40 | /* static tag bytes (protocol control messages) */ |
| 41 | static char tag_msg = CEPH_MSGR_TAG_MSG; |
| 42 | static char tag_ack = CEPH_MSGR_TAG_ACK; |
| 43 | static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE; |
| 44 | |
Sage Weil | a6a5349 | 2010-04-13 14:07:07 -0700 | [diff] [blame] | 45 | #ifdef CONFIG_LOCKDEP |
| 46 | static struct lock_class_key socket_class; |
| 47 | #endif |
| 48 | |
Alex Elder | 84495f4 | 2012-02-15 07:43:55 -0600 | [diff] [blame] | 49 | /* |
| 50 | * When skipping (ignoring) a block of input we read it into a "skip |
| 51 | * buffer," which is this many bytes in size. |
| 52 | */ |
| 53 | #define SKIP_BUF_SIZE 1024 |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 54 | |
| 55 | static void queue_con(struct ceph_connection *con); |
| 56 | static void con_work(struct work_struct *); |
| 57 | static void ceph_fault(struct ceph_connection *con); |
| 58 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 59 | /* |
Alex Elder | f64a931 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 60 | * Nicely render a sockaddr as a string. An array of formatted |
| 61 | * strings is used, to approximate reentrancy. |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 62 | */ |
Alex Elder | f64a931 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 63 | #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */ |
| 64 | #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG) |
| 65 | #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1) |
| 66 | #define MAX_ADDR_STR_LEN 64 /* 54 is enough */ |
| 67 | |
| 68 | static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN]; |
| 69 | static atomic_t addr_str_seq = ATOMIC_INIT(0); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 70 | |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 71 | static struct page *zero_page; /* used in certain error cases */ |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 72 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 73 | const char *ceph_pr_addr(const struct sockaddr_storage *ss) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 74 | { |
| 75 | int i; |
| 76 | char *s; |
Alex Elder | 99f0f3b | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 77 | struct sockaddr_in *in4 = (struct sockaddr_in *) ss; |
| 78 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 79 | |
Alex Elder | f64a931 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 80 | i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 81 | s = addr_str[i]; |
| 82 | |
| 83 | switch (ss->ss_family) { |
| 84 | case AF_INET: |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 85 | snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr, |
| 86 | ntohs(in4->sin_port)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 87 | break; |
| 88 | |
| 89 | case AF_INET6: |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 90 | snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr, |
| 91 | ntohs(in6->sin6_port)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 92 | break; |
| 93 | |
| 94 | default: |
Alex Elder | d3002b9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 95 | snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)", |
| 96 | ss->ss_family); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return s; |
| 100 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 101 | EXPORT_SYMBOL(ceph_pr_addr); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 102 | |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 103 | static void encode_my_addr(struct ceph_messenger *msgr) |
| 104 | { |
| 105 | memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr)); |
| 106 | ceph_encode_addr(&msgr->my_enc_addr); |
| 107 | } |
| 108 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 109 | /* |
| 110 | * work queue for all reading and writing to/from the socket. |
| 111 | */ |
Alex Elder | e0f43c9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 112 | static struct workqueue_struct *ceph_msgr_wq; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 113 | |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 114 | void _ceph_msgr_exit(void) |
| 115 | { |
Alex Elder | d3002b9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 116 | if (ceph_msgr_wq) { |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 117 | destroy_workqueue(ceph_msgr_wq); |
Alex Elder | d3002b9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 118 | ceph_msgr_wq = NULL; |
| 119 | } |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 120 | |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 121 | BUG_ON(zero_page == NULL); |
| 122 | kunmap(zero_page); |
| 123 | page_cache_release(zero_page); |
| 124 | zero_page = NULL; |
| 125 | } |
| 126 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 127 | int ceph_msgr_init(void) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 128 | { |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 129 | BUG_ON(zero_page != NULL); |
| 130 | zero_page = ZERO_PAGE(0); |
| 131 | page_cache_get(zero_page); |
| 132 | |
Tejun Heo | f363e45fd1 | 2011-01-03 14:49:46 +0100 | [diff] [blame] | 133 | ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0); |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 134 | if (ceph_msgr_wq) |
| 135 | return 0; |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 136 | |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 137 | pr_err("msgr_init failed to create workqueue\n"); |
| 138 | _ceph_msgr_exit(); |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 139 | |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 140 | return -ENOMEM; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 141 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 142 | EXPORT_SYMBOL(ceph_msgr_init); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 143 | |
| 144 | void ceph_msgr_exit(void) |
| 145 | { |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 146 | BUG_ON(ceph_msgr_wq == NULL); |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 147 | |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 148 | _ceph_msgr_exit(); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 149 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 150 | EXPORT_SYMBOL(ceph_msgr_exit); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 151 | |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 152 | void ceph_msgr_flush(void) |
Sage Weil | a922d38 | 2010-05-29 09:41:23 -0700 | [diff] [blame] | 153 | { |
| 154 | flush_workqueue(ceph_msgr_wq); |
| 155 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 156 | EXPORT_SYMBOL(ceph_msgr_flush); |
Sage Weil | a922d38 | 2010-05-29 09:41:23 -0700 | [diff] [blame] | 157 | |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 158 | /* Connection socket state transition functions */ |
| 159 | |
| 160 | static void con_sock_state_init(struct ceph_connection *con) |
| 161 | { |
| 162 | int old_state; |
| 163 | |
| 164 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); |
| 165 | if (WARN_ON(old_state != CON_SOCK_STATE_NEW)) |
| 166 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 167 | } |
| 168 | |
| 169 | static void con_sock_state_connecting(struct ceph_connection *con) |
| 170 | { |
| 171 | int old_state; |
| 172 | |
| 173 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING); |
| 174 | if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED)) |
| 175 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 176 | } |
| 177 | |
| 178 | static void con_sock_state_connected(struct ceph_connection *con) |
| 179 | { |
| 180 | int old_state; |
| 181 | |
| 182 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED); |
| 183 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING)) |
| 184 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 185 | } |
| 186 | |
| 187 | static void con_sock_state_closing(struct ceph_connection *con) |
| 188 | { |
| 189 | int old_state; |
| 190 | |
| 191 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING); |
| 192 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING && |
| 193 | old_state != CON_SOCK_STATE_CONNECTED && |
| 194 | old_state != CON_SOCK_STATE_CLOSING)) |
| 195 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 196 | } |
| 197 | |
| 198 | static void con_sock_state_closed(struct ceph_connection *con) |
| 199 | { |
| 200 | int old_state; |
| 201 | |
| 202 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); |
| 203 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED && |
| 204 | old_state != CON_SOCK_STATE_CLOSING)) |
| 205 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 206 | } |
Sage Weil | a922d38 | 2010-05-29 09:41:23 -0700 | [diff] [blame] | 207 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 208 | /* |
| 209 | * socket callback functions |
| 210 | */ |
| 211 | |
| 212 | /* data available on socket, or listen socket received a connect */ |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 213 | static void ceph_sock_data_ready(struct sock *sk, int count_unused) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 214 | { |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 215 | struct ceph_connection *con = sk->sk_user_data; |
| 216 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 217 | if (sk->sk_state != TCP_CLOSE_WAIT) { |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 218 | dout("%s on %p state = %lu, queueing work\n", __func__, |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 219 | con, con->state); |
| 220 | queue_con(con); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* socket has buffer space for writing */ |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 225 | static void ceph_sock_write_space(struct sock *sk) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 226 | { |
Alex Elder | d3002b9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 227 | struct ceph_connection *con = sk->sk_user_data; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 228 | |
Jim Schutt | 182fac2 | 2012-02-29 08:30:58 -0700 | [diff] [blame] | 229 | /* only queue to workqueue if there is data we want to write, |
| 230 | * and there is sufficient space in the socket buffer to accept |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 231 | * more data. clear SOCK_NOSPACE so that ceph_sock_write_space() |
Jim Schutt | 182fac2 | 2012-02-29 08:30:58 -0700 | [diff] [blame] | 232 | * doesn't get called again until try_write() fills the socket |
| 233 | * buffer. See net/ipv4/tcp_input.c:tcp_check_space() |
| 234 | * and net/core/stream.c:sk_stream_write_space(). |
| 235 | */ |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 236 | if (test_bit(WRITE_PENDING, &con->flags)) { |
Jim Schutt | 182fac2 | 2012-02-29 08:30:58 -0700 | [diff] [blame] | 237 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 238 | dout("%s %p queueing write work\n", __func__, con); |
Jim Schutt | 182fac2 | 2012-02-29 08:30:58 -0700 | [diff] [blame] | 239 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 240 | queue_con(con); |
| 241 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 242 | } else { |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 243 | dout("%s %p nothing to write\n", __func__, con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 244 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* socket's state has changed */ |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 248 | static void ceph_sock_state_change(struct sock *sk) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 249 | { |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 250 | struct ceph_connection *con = sk->sk_user_data; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 251 | |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 252 | dout("%s %p state = %lu sk_state = %u\n", __func__, |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 253 | con, con->state, sk->sk_state); |
| 254 | |
| 255 | if (test_bit(CLOSED, &con->state)) |
| 256 | return; |
| 257 | |
| 258 | switch (sk->sk_state) { |
| 259 | case TCP_CLOSE: |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 260 | dout("%s TCP_CLOSE\n", __func__); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 261 | case TCP_CLOSE_WAIT: |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 262 | dout("%s TCP_CLOSE_WAIT\n", __func__); |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 263 | con_sock_state_closing(con); |
Alex Elder | d65c9e0 | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 264 | set_bit(SOCK_CLOSED, &con->flags); |
| 265 | queue_con(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 266 | break; |
| 267 | case TCP_ESTABLISHED: |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 268 | dout("%s TCP_ESTABLISHED\n", __func__); |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 269 | con_sock_state_connected(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 270 | queue_con(con); |
| 271 | break; |
Alex Elder | d3002b9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 272 | default: /* Everything else is uninteresting */ |
| 273 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * set up socket callbacks |
| 279 | */ |
| 280 | static void set_sock_callbacks(struct socket *sock, |
| 281 | struct ceph_connection *con) |
| 282 | { |
| 283 | struct sock *sk = sock->sk; |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 284 | sk->sk_user_data = con; |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 285 | sk->sk_data_ready = ceph_sock_data_ready; |
| 286 | sk->sk_write_space = ceph_sock_write_space; |
| 287 | sk->sk_state_change = ceph_sock_state_change; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | |
| 291 | /* |
| 292 | * socket helpers |
| 293 | */ |
| 294 | |
| 295 | /* |
| 296 | * initiate connection to a remote socket. |
| 297 | */ |
Alex Elder | 41617d0 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 298 | static int ceph_tcp_connect(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 299 | { |
Sage Weil | f91d347 | 2010-07-01 15:18:31 -0700 | [diff] [blame] | 300 | struct sockaddr_storage *paddr = &con->peer_addr.in_addr; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 301 | struct socket *sock; |
| 302 | int ret; |
| 303 | |
| 304 | BUG_ON(con->sock); |
Sage Weil | f91d347 | 2010-07-01 15:18:31 -0700 | [diff] [blame] | 305 | ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM, |
| 306 | IPPROTO_TCP, &sock); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 307 | if (ret) |
Alex Elder | 41617d0 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 308 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 309 | sock->sk->sk_allocation = GFP_NOFS; |
| 310 | |
Sage Weil | a6a5349 | 2010-04-13 14:07:07 -0700 | [diff] [blame] | 311 | #ifdef CONFIG_LOCKDEP |
| 312 | lockdep_set_class(&sock->sk->sk_lock, &socket_class); |
| 313 | #endif |
| 314 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 315 | set_sock_callbacks(sock, con); |
| 316 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 317 | dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 318 | |
Sage Weil | 89a86be | 2012-06-09 14:19:21 -0700 | [diff] [blame] | 319 | con_sock_state_connecting(con); |
Sage Weil | f91d347 | 2010-07-01 15:18:31 -0700 | [diff] [blame] | 320 | ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr), |
| 321 | O_NONBLOCK); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 322 | if (ret == -EINPROGRESS) { |
| 323 | dout("connect %s EINPROGRESS sk_state = %u\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 324 | ceph_pr_addr(&con->peer_addr.in_addr), |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 325 | sock->sk->sk_state); |
Alex Elder | a5bc312 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 326 | } else if (ret < 0) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 327 | pr_err("connect %s error %d\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 328 | ceph_pr_addr(&con->peer_addr.in_addr), ret); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 329 | sock_release(sock); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 330 | con->error_msg = "connect error"; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 331 | |
Alex Elder | 41617d0 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 332 | return ret; |
Alex Elder | a5bc312 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 333 | } |
| 334 | con->sock = sock; |
Alex Elder | 41617d0 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 335 | return 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len) |
| 339 | { |
| 340 | struct kvec iov = {buf, len}; |
| 341 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 342 | int r; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 343 | |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 344 | r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags); |
| 345 | if (r == -EAGAIN) |
| 346 | r = 0; |
| 347 | return r; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* |
| 351 | * write something. @more is true if caller will be sending more data |
| 352 | * shortly. |
| 353 | */ |
| 354 | static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov, |
| 355 | size_t kvlen, size_t len, int more) |
| 356 | { |
| 357 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 358 | int r; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 359 | |
| 360 | if (more) |
| 361 | msg.msg_flags |= MSG_MORE; |
| 362 | else |
| 363 | msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */ |
| 364 | |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 365 | r = kernel_sendmsg(sock, &msg, iov, kvlen, len); |
| 366 | if (r == -EAGAIN) |
| 367 | r = 0; |
| 368 | return r; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Alex Elder | 3173913 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 371 | static int ceph_tcp_sendpage(struct socket *sock, struct page *page, |
| 372 | int offset, size_t size, int more) |
| 373 | { |
| 374 | int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR); |
| 375 | int ret; |
| 376 | |
| 377 | ret = kernel_sendpage(sock, page, offset, size, flags); |
| 378 | if (ret == -EAGAIN) |
| 379 | ret = 0; |
| 380 | |
| 381 | return ret; |
| 382 | } |
| 383 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * Shutdown/close the socket for the given connection. |
| 387 | */ |
| 388 | static int con_close_socket(struct ceph_connection *con) |
| 389 | { |
| 390 | int rc; |
| 391 | |
| 392 | dout("con_close_socket on %p sock %p\n", con, con->sock); |
| 393 | if (!con->sock) |
| 394 | return 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 395 | rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR); |
| 396 | sock_release(con->sock); |
| 397 | con->sock = NULL; |
Alex Elder | 456ea46 | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 398 | |
| 399 | /* |
| 400 | * Forcibly clear the SOCK_CLOSE flag. It gets set |
| 401 | * independent of the connection mutex, and we could have |
| 402 | * received a socket close event before we had the chance to |
| 403 | * shut the socket down. |
| 404 | */ |
Alex Elder | a8d00e3 | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 405 | clear_bit(SOCK_CLOSED, &con->flags); |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 406 | con_sock_state_closed(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 407 | return rc; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Reset a connection. Discard all incoming and outgoing messages |
| 412 | * and clear *_seq state. |
| 413 | */ |
| 414 | static void ceph_msg_remove(struct ceph_msg *msg) |
| 415 | { |
| 416 | list_del_init(&msg->list_head); |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 417 | BUG_ON(msg->con == NULL); |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 418 | msg->con->ops->put(msg->con); |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 419 | msg->con = NULL; |
| 420 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 421 | ceph_msg_put(msg); |
| 422 | } |
| 423 | static void ceph_msg_remove_list(struct list_head *head) |
| 424 | { |
| 425 | while (!list_empty(head)) { |
| 426 | struct ceph_msg *msg = list_first_entry(head, struct ceph_msg, |
| 427 | list_head); |
| 428 | ceph_msg_remove(msg); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static void reset_connection(struct ceph_connection *con) |
| 433 | { |
| 434 | /* reset connection, out_queue, msg_ and connect_seq */ |
| 435 | /* discard existing out_queue and msg_seq */ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 436 | ceph_msg_remove_list(&con->out_queue); |
| 437 | ceph_msg_remove_list(&con->out_sent); |
| 438 | |
Sage Weil | cf3e5c4 | 2009-12-11 09:48:05 -0800 | [diff] [blame] | 439 | if (con->in_msg) { |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 440 | BUG_ON(con->in_msg->con != con); |
| 441 | con->in_msg->con = NULL; |
Sage Weil | cf3e5c4 | 2009-12-11 09:48:05 -0800 | [diff] [blame] | 442 | ceph_msg_put(con->in_msg); |
| 443 | con->in_msg = NULL; |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 444 | con->ops->put(con); |
Sage Weil | cf3e5c4 | 2009-12-11 09:48:05 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 447 | con->connect_seq = 0; |
| 448 | con->out_seq = 0; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 449 | if (con->out_msg) { |
| 450 | ceph_msg_put(con->out_msg); |
| 451 | con->out_msg = NULL; |
| 452 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 453 | con->in_seq = 0; |
Sage Weil | 0e0d5e0 | 2010-04-02 16:07:19 -0700 | [diff] [blame] | 454 | con->in_seq_acked = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* |
| 458 | * mark a peer down. drop any open connections. |
| 459 | */ |
| 460 | void ceph_con_close(struct ceph_connection *con) |
| 461 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 462 | dout("con_close %p peer %s\n", con, |
| 463 | ceph_pr_addr(&con->peer_addr.in_addr)); |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 464 | clear_bit(NEGOTIATING, &con->state); |
Alex Elder | bb9e6bb | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 465 | clear_bit(CONNECTING, &con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 466 | clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */ |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 467 | set_bit(CLOSED, &con->state); |
| 468 | |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 469 | clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */ |
| 470 | clear_bit(KEEPALIVE_PENDING, &con->flags); |
| 471 | clear_bit(WRITE_PENDING, &con->flags); |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 472 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 473 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 474 | reset_connection(con); |
Sage Weil | 6f2bc3f | 2010-04-02 16:16:34 -0700 | [diff] [blame] | 475 | con->peer_global_seq = 0; |
Sage Weil | 91e45ce3 | 2010-02-15 12:05:09 -0800 | [diff] [blame] | 476 | cancel_delayed_work(&con->work); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 477 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 478 | queue_con(con); |
| 479 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 480 | EXPORT_SYMBOL(ceph_con_close); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 481 | |
| 482 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 483 | * Reopen a closed connection, with a new peer address. |
| 484 | */ |
| 485 | void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr) |
| 486 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 487 | dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 488 | set_bit(OPENING, &con->state); |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 489 | WARN_ON(!test_and_clear_bit(CLOSED, &con->state)); |
| 490 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 491 | memcpy(&con->peer_addr, addr, sizeof(*addr)); |
Sage Weil | 03c677e | 2009-11-20 15:14:15 -0800 | [diff] [blame] | 492 | con->delay = 0; /* reset backoff memory */ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 493 | queue_con(con); |
| 494 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 495 | EXPORT_SYMBOL(ceph_con_open); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 496 | |
| 497 | /* |
Sage Weil | 87b315a | 2010-03-22 14:51:18 -0700 | [diff] [blame] | 498 | * return true if this connection ever successfully opened |
| 499 | */ |
| 500 | bool ceph_con_opened(struct ceph_connection *con) |
| 501 | { |
| 502 | return con->connect_seq > 0; |
| 503 | } |
| 504 | |
| 505 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 506 | * initialize a new connection. |
| 507 | */ |
Alex Elder | 1bfd89f | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 508 | void ceph_con_init(struct ceph_connection *con, void *private, |
| 509 | const struct ceph_connection_operations *ops, |
| 510 | struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 511 | { |
| 512 | dout("con_init %p\n", con); |
| 513 | memset(con, 0, sizeof(*con)); |
Alex Elder | 1bfd89f | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 514 | con->private = private; |
| 515 | con->ops = ops; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 516 | con->msgr = msgr; |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 517 | |
| 518 | con_sock_state_init(con); |
| 519 | |
Alex Elder | 1bfd89f | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 520 | con->peer_name.type = (__u8) entity_type; |
| 521 | con->peer_name.num = cpu_to_le64(entity_num); |
| 522 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 523 | mutex_init(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 524 | INIT_LIST_HEAD(&con->out_queue); |
| 525 | INIT_LIST_HEAD(&con->out_sent); |
| 526 | INIT_DELAYED_WORK(&con->work, con_work); |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 527 | |
| 528 | set_bit(CLOSED, &con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 529 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 530 | EXPORT_SYMBOL(ceph_con_init); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 531 | |
| 532 | |
| 533 | /* |
| 534 | * We maintain a global counter to order connection attempts. Get |
| 535 | * a unique seq greater than @gt. |
| 536 | */ |
| 537 | static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt) |
| 538 | { |
| 539 | u32 ret; |
| 540 | |
| 541 | spin_lock(&msgr->global_seq_lock); |
| 542 | if (msgr->global_seq < gt) |
| 543 | msgr->global_seq = gt; |
| 544 | ret = ++msgr->global_seq; |
| 545 | spin_unlock(&msgr->global_seq_lock); |
| 546 | return ret; |
| 547 | } |
| 548 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 549 | static void con_out_kvec_reset(struct ceph_connection *con) |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 550 | { |
| 551 | con->out_kvec_left = 0; |
| 552 | con->out_kvec_bytes = 0; |
| 553 | con->out_kvec_cur = &con->out_kvec[0]; |
| 554 | } |
| 555 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 556 | static void con_out_kvec_add(struct ceph_connection *con, |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 557 | size_t size, void *data) |
| 558 | { |
| 559 | int index; |
| 560 | |
| 561 | index = con->out_kvec_left; |
| 562 | BUG_ON(index >= ARRAY_SIZE(con->out_kvec)); |
| 563 | |
| 564 | con->out_kvec[index].iov_len = size; |
| 565 | con->out_kvec[index].iov_base = data; |
| 566 | con->out_kvec_left++; |
| 567 | con->out_kvec_bytes += size; |
| 568 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 569 | |
Alex Elder | df6ad1f | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 570 | #ifdef CONFIG_BLOCK |
| 571 | static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg) |
| 572 | { |
| 573 | if (!bio) { |
| 574 | *iter = NULL; |
| 575 | *seg = 0; |
| 576 | return; |
| 577 | } |
| 578 | *iter = bio; |
| 579 | *seg = bio->bi_idx; |
| 580 | } |
| 581 | |
| 582 | static void iter_bio_next(struct bio **bio_iter, int *seg) |
| 583 | { |
| 584 | if (*bio_iter == NULL) |
| 585 | return; |
| 586 | |
| 587 | BUG_ON(*seg >= (*bio_iter)->bi_vcnt); |
| 588 | |
| 589 | (*seg)++; |
| 590 | if (*seg == (*bio_iter)->bi_vcnt) |
| 591 | init_bio_iter((*bio_iter)->bi_next, bio_iter, seg); |
| 592 | } |
| 593 | #endif |
| 594 | |
Alex Elder | 739c905 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 595 | static void prepare_write_message_data(struct ceph_connection *con) |
| 596 | { |
| 597 | struct ceph_msg *msg = con->out_msg; |
| 598 | |
| 599 | BUG_ON(!msg); |
| 600 | BUG_ON(!msg->hdr.data_len); |
| 601 | |
| 602 | /* initialize page iterator */ |
| 603 | con->out_msg_pos.page = 0; |
| 604 | if (msg->pages) |
| 605 | con->out_msg_pos.page_pos = msg->page_alignment; |
| 606 | else |
| 607 | con->out_msg_pos.page_pos = 0; |
Alex Elder | 572c588 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 608 | #ifdef CONFIG_BLOCK |
Alex Elder | abdaa6a | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 609 | if (msg->bio) |
Alex Elder | 572c588 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 610 | init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg); |
| 611 | #endif |
Alex Elder | 739c905 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 612 | con->out_msg_pos.data_pos = 0; |
| 613 | con->out_msg_pos.did_page_crc = false; |
| 614 | con->out_more = 1; /* data + footer will follow */ |
| 615 | } |
| 616 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 617 | /* |
| 618 | * Prepare footer for currently outgoing message, and finish things |
| 619 | * off. Assumes out_kvec* are already valid.. we just add on to the end. |
| 620 | */ |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 621 | static void prepare_write_message_footer(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 622 | { |
| 623 | struct ceph_msg *m = con->out_msg; |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 624 | int v = con->out_kvec_left; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 625 | |
Alex Elder | fd154f3 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 626 | m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE; |
| 627 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 628 | dout("prepare_write_message_footer %p\n", con); |
| 629 | con->out_kvec_is_msg = true; |
| 630 | con->out_kvec[v].iov_base = &m->footer; |
| 631 | con->out_kvec[v].iov_len = sizeof(m->footer); |
| 632 | con->out_kvec_bytes += sizeof(m->footer); |
| 633 | con->out_kvec_left++; |
| 634 | con->out_more = m->more_to_follow; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 635 | con->out_msg_done = true; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Prepare headers for the next outgoing message. |
| 640 | */ |
| 641 | static void prepare_write_message(struct ceph_connection *con) |
| 642 | { |
| 643 | struct ceph_msg *m; |
Alex Elder | a9a0c51 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 644 | u32 crc; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 645 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 646 | con_out_kvec_reset(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 647 | con->out_kvec_is_msg = true; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 648 | con->out_msg_done = false; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 649 | |
| 650 | /* Sneak an ack in there first? If we can get it into the same |
| 651 | * TCP packet that's a good thing. */ |
| 652 | if (con->in_seq > con->in_seq_acked) { |
| 653 | con->in_seq_acked = con->in_seq; |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 654 | con_out_kvec_add(con, sizeof (tag_ack), &tag_ack); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 655 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 656 | con_out_kvec_add(con, sizeof (con->out_temp_ack), |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 657 | &con->out_temp_ack); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 660 | BUG_ON(list_empty(&con->out_queue)); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 661 | m = list_first_entry(&con->out_queue, struct ceph_msg, list_head); |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 662 | con->out_msg = m; |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 663 | BUG_ON(m->con != con); |
Sage Weil | 4cf9d54 | 2011-07-26 11:27:24 -0700 | [diff] [blame] | 664 | |
| 665 | /* put message on sent list */ |
| 666 | ceph_msg_get(m); |
| 667 | list_move_tail(&m->list_head, &con->out_sent); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 668 | |
Sage Weil | e84346b | 2010-05-11 21:20:38 -0700 | [diff] [blame] | 669 | /* |
| 670 | * only assign outgoing seq # if we haven't sent this message |
| 671 | * yet. if it is requeued, resend with it's original seq. |
| 672 | */ |
| 673 | if (m->needs_out_seq) { |
| 674 | m->hdr.seq = cpu_to_le64(++con->out_seq); |
| 675 | m->needs_out_seq = false; |
| 676 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 677 | |
| 678 | dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n", |
| 679 | m, con->out_seq, le16_to_cpu(m->hdr.type), |
| 680 | le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len), |
| 681 | le32_to_cpu(m->hdr.data_len), |
| 682 | m->nr_pages); |
| 683 | BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len); |
| 684 | |
| 685 | /* tag + hdr + front + middle */ |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 686 | con_out_kvec_add(con, sizeof (tag_msg), &tag_msg); |
| 687 | con_out_kvec_add(con, sizeof (m->hdr), &m->hdr); |
| 688 | con_out_kvec_add(con, m->front.iov_len, m->front.iov_base); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 689 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 690 | if (m->middle) |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 691 | con_out_kvec_add(con, m->middle->vec.iov_len, |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 692 | m->middle->vec.iov_base); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 693 | |
| 694 | /* fill in crc (except data pages), footer */ |
Alex Elder | a9a0c51 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 695 | crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc)); |
| 696 | con->out_msg->hdr.crc = cpu_to_le32(crc); |
Alex Elder | fd154f3 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 697 | con->out_msg->footer.flags = 0; |
Alex Elder | a9a0c51 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 698 | |
| 699 | crc = crc32c(0, m->front.iov_base, m->front.iov_len); |
| 700 | con->out_msg->footer.front_crc = cpu_to_le32(crc); |
| 701 | if (m->middle) { |
| 702 | crc = crc32c(0, m->middle->vec.iov_base, |
| 703 | m->middle->vec.iov_len); |
| 704 | con->out_msg->footer.middle_crc = cpu_to_le32(crc); |
| 705 | } else |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 706 | con->out_msg->footer.middle_crc = 0; |
Alex Elder | 739c905 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 707 | dout("%s front_crc %u middle_crc %u\n", __func__, |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 708 | le32_to_cpu(con->out_msg->footer.front_crc), |
| 709 | le32_to_cpu(con->out_msg->footer.middle_crc)); |
| 710 | |
| 711 | /* is there a data payload? */ |
Alex Elder | 739c905 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 712 | con->out_msg->footer.data_crc = 0; |
| 713 | if (m->hdr.data_len) |
| 714 | prepare_write_message_data(con); |
| 715 | else |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 716 | /* no, queue up footer too and be done */ |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 717 | prepare_write_message_footer(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 718 | |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 719 | set_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* |
| 723 | * Prepare an ack. |
| 724 | */ |
| 725 | static void prepare_write_ack(struct ceph_connection *con) |
| 726 | { |
| 727 | dout("prepare_write_ack %p %llu -> %llu\n", con, |
| 728 | con->in_seq_acked, con->in_seq); |
| 729 | con->in_seq_acked = con->in_seq; |
| 730 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 731 | con_out_kvec_reset(con); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 732 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 733 | con_out_kvec_add(con, sizeof (tag_ack), &tag_ack); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 734 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 735 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 736 | con_out_kvec_add(con, sizeof (con->out_temp_ack), |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 737 | &con->out_temp_ack); |
| 738 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 739 | con->out_more = 1; /* more will follow.. eventually.. */ |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 740 | set_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Prepare to write keepalive byte. |
| 745 | */ |
| 746 | static void prepare_write_keepalive(struct ceph_connection *con) |
| 747 | { |
| 748 | dout("prepare_write_keepalive %p\n", con); |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 749 | con_out_kvec_reset(con); |
| 750 | con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive); |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 751 | set_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* |
| 755 | * Connection negotiation. |
| 756 | */ |
| 757 | |
Alex Elder | dac1e71 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 758 | static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con, |
| 759 | int *auth_proto) |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 760 | { |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 761 | struct ceph_auth_handshake *auth; |
Alex Elder | b1c6b98 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 762 | |
| 763 | if (!con->ops->get_authorizer) { |
| 764 | con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN; |
| 765 | con->out_connect.authorizer_len = 0; |
| 766 | |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 767 | return NULL; |
Alex Elder | b1c6b98 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* Can't hold the mutex while getting authorizer */ |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 771 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 772 | mutex_unlock(&con->mutex); |
Alex Elder | b1c6b98 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 773 | |
Alex Elder | dac1e71 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 774 | auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry); |
Alex Elder | 8f43fb5 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 775 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 776 | mutex_lock(&con->mutex); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 777 | |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 778 | if (IS_ERR(auth)) |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 779 | return auth; |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 780 | if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags)) |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 781 | return ERR_PTR(-EAGAIN); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 782 | |
Alex Elder | 8f43fb5 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 783 | con->auth_reply_buf = auth->authorizer_reply_buf; |
| 784 | con->auth_reply_buf_len = auth->authorizer_reply_buf_len; |
| 785 | |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 786 | |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 787 | return auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 788 | } |
| 789 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 790 | /* |
| 791 | * We connected to a peer and are saying hello. |
| 792 | */ |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 793 | static void prepare_write_banner(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 794 | { |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 795 | con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER); |
| 796 | con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr), |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 797 | &con->msgr->my_enc_addr); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 798 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 799 | con->out_more = 0; |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 800 | set_bit(WRITE_PENDING, &con->flags); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 803 | static int prepare_write_connect(struct ceph_connection *con) |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 804 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 805 | unsigned int global_seq = get_global_seq(con->msgr, 0); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 806 | int proto; |
Alex Elder | dac1e71 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 807 | int auth_proto; |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 808 | struct ceph_auth_handshake *auth; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 809 | |
| 810 | switch (con->peer_name.type) { |
| 811 | case CEPH_ENTITY_TYPE_MON: |
| 812 | proto = CEPH_MONC_PROTOCOL; |
| 813 | break; |
| 814 | case CEPH_ENTITY_TYPE_OSD: |
| 815 | proto = CEPH_OSDC_PROTOCOL; |
| 816 | break; |
| 817 | case CEPH_ENTITY_TYPE_MDS: |
| 818 | proto = CEPH_MDSC_PROTOCOL; |
| 819 | break; |
| 820 | default: |
| 821 | BUG(); |
| 822 | } |
| 823 | |
| 824 | dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con, |
| 825 | con->connect_seq, global_seq, proto); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 826 | |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 827 | con->out_connect.features = cpu_to_le64(con->msgr->supported_features); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 828 | con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT); |
| 829 | con->out_connect.connect_seq = cpu_to_le32(con->connect_seq); |
| 830 | con->out_connect.global_seq = cpu_to_le32(global_seq); |
| 831 | con->out_connect.protocol_version = cpu_to_le32(proto); |
| 832 | con->out_connect.flags = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 833 | |
Alex Elder | dac1e71 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 834 | auth_proto = CEPH_AUTH_UNKNOWN; |
| 835 | auth = get_connect_authorizer(con, &auth_proto); |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 836 | if (IS_ERR(auth)) |
| 837 | return PTR_ERR(auth); |
Alex Elder | 3da5477 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 838 | |
Alex Elder | dac1e71 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 839 | con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto); |
Alex Elder | 3da5477 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 840 | con->out_connect.authorizer_len = auth ? |
| 841 | cpu_to_le32(auth->authorizer_buf_len) : 0; |
| 842 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 843 | con_out_kvec_add(con, sizeof (con->out_connect), |
Alex Elder | 3da5477 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 844 | &con->out_connect); |
| 845 | if (auth && auth->authorizer_buf_len) |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 846 | con_out_kvec_add(con, auth->authorizer_buf_len, |
Alex Elder | 3da5477 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 847 | auth->authorizer_buf); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 848 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 849 | con->out_more = 0; |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 850 | set_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 851 | |
Alex Elder | e10c758 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 852 | return 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 855 | /* |
| 856 | * write as much of pending kvecs to the socket as we can. |
| 857 | * 1 -> done |
| 858 | * 0 -> socket full, but more to do |
| 859 | * <0 -> error |
| 860 | */ |
| 861 | static int write_partial_kvec(struct ceph_connection *con) |
| 862 | { |
| 863 | int ret; |
| 864 | |
| 865 | dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes); |
| 866 | while (con->out_kvec_bytes > 0) { |
| 867 | ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur, |
| 868 | con->out_kvec_left, con->out_kvec_bytes, |
| 869 | con->out_more); |
| 870 | if (ret <= 0) |
| 871 | goto out; |
| 872 | con->out_kvec_bytes -= ret; |
| 873 | if (con->out_kvec_bytes == 0) |
| 874 | break; /* done */ |
Alex Elder | f42299e | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 875 | |
| 876 | /* account for full iov entries consumed */ |
| 877 | while (ret >= con->out_kvec_cur->iov_len) { |
| 878 | BUG_ON(!con->out_kvec_left); |
| 879 | ret -= con->out_kvec_cur->iov_len; |
| 880 | con->out_kvec_cur++; |
| 881 | con->out_kvec_left--; |
| 882 | } |
| 883 | /* and for a partially-consumed entry */ |
| 884 | if (ret) { |
| 885 | con->out_kvec_cur->iov_len -= ret; |
| 886 | con->out_kvec_cur->iov_base += ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | con->out_kvec_left = 0; |
| 890 | con->out_kvec_is_msg = false; |
| 891 | ret = 1; |
| 892 | out: |
| 893 | dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con, |
| 894 | con->out_kvec_bytes, con->out_kvec_left, ret); |
| 895 | return ret; /* done! */ |
| 896 | } |
| 897 | |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 898 | static void out_msg_pos_next(struct ceph_connection *con, struct page *page, |
| 899 | size_t len, size_t sent, bool in_trail) |
| 900 | { |
| 901 | struct ceph_msg *msg = con->out_msg; |
| 902 | |
| 903 | BUG_ON(!msg); |
| 904 | BUG_ON(!sent); |
| 905 | |
| 906 | con->out_msg_pos.data_pos += sent; |
| 907 | con->out_msg_pos.page_pos += sent; |
| 908 | if (sent == len) { |
| 909 | con->out_msg_pos.page_pos = 0; |
| 910 | con->out_msg_pos.page++; |
| 911 | con->out_msg_pos.did_page_crc = false; |
| 912 | if (in_trail) |
| 913 | list_move_tail(&page->lru, |
| 914 | &msg->trail->head); |
| 915 | else if (msg->pagelist) |
| 916 | list_move_tail(&page->lru, |
| 917 | &msg->pagelist->head); |
| 918 | #ifdef CONFIG_BLOCK |
| 919 | else if (msg->bio) |
| 920 | iter_bio_next(&msg->bio_iter, &msg->bio_seg); |
| 921 | #endif |
| 922 | } |
| 923 | } |
| 924 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 925 | /* |
| 926 | * Write as much message data payload as we can. If we finish, queue |
| 927 | * up the footer. |
| 928 | * 1 -> done, footer is now queued in out_kvec[]. |
| 929 | * 0 -> socket full, but more to do |
| 930 | * <0 -> error |
| 931 | */ |
| 932 | static int write_partial_msg_pages(struct ceph_connection *con) |
| 933 | { |
| 934 | struct ceph_msg *msg = con->out_msg; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 935 | unsigned int data_len = le32_to_cpu(msg->hdr.data_len); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 936 | size_t len; |
Alex Elder | 37675b0 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 937 | bool do_datacrc = !con->msgr->nocrc; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 938 | int ret; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 939 | int total_max_write; |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 940 | bool in_trail = false; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 941 | size_t trail_len = (msg->trail ? msg->trail->length : 0); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 942 | |
| 943 | dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n", |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 944 | con, msg, con->out_msg_pos.page, msg->nr_pages, |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 945 | con->out_msg_pos.page_pos); |
| 946 | |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 947 | while (data_len > con->out_msg_pos.data_pos) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 948 | struct page *page = NULL; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 949 | int max_write = PAGE_SIZE; |
Alex Elder | 9bd1966 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 950 | int bio_offset = 0; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 951 | |
| 952 | total_max_write = data_len - trail_len - |
| 953 | con->out_msg_pos.data_pos; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 954 | |
| 955 | /* |
| 956 | * if we are calculating the data crc (the default), we need |
| 957 | * to map the page. if our pages[] has been revoked, use the |
| 958 | * zero page. |
| 959 | */ |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 960 | |
| 961 | /* have we reached the trail part of the data? */ |
| 962 | if (con->out_msg_pos.data_pos >= data_len - trail_len) { |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 963 | in_trail = true; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 964 | |
| 965 | total_max_write = data_len - con->out_msg_pos.data_pos; |
| 966 | |
| 967 | page = list_first_entry(&msg->trail->head, |
| 968 | struct page, lru); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 969 | } else if (msg->pages) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 970 | page = msg->pages[con->out_msg_pos.page]; |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 971 | } else if (msg->pagelist) { |
| 972 | page = list_first_entry(&msg->pagelist->head, |
| 973 | struct page, lru); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 974 | #ifdef CONFIG_BLOCK |
| 975 | } else if (msg->bio) { |
| 976 | struct bio_vec *bv; |
| 977 | |
| 978 | bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg); |
| 979 | page = bv->bv_page; |
Alex Elder | 9bd1966 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 980 | bio_offset = bv->bv_offset; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 981 | max_write = bv->bv_len; |
| 982 | #endif |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 983 | } else { |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 984 | page = zero_page; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 985 | } |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 986 | len = min_t(int, max_write - con->out_msg_pos.page_pos, |
| 987 | total_max_write); |
| 988 | |
Alex Elder | 37675b0 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 989 | if (do_datacrc && !con->out_msg_pos.did_page_crc) { |
Alex Elder | 9bd1966 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 990 | void *base; |
Alex Elder | a9a0c51 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 991 | u32 crc; |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 992 | u32 tmpcrc = le32_to_cpu(msg->footer.data_crc); |
Alex Elder | 8d63e31 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 993 | char *kaddr; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 994 | |
Alex Elder | 8d63e31 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 995 | kaddr = kmap(page); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 996 | BUG_ON(kaddr == NULL); |
Alex Elder | 9bd1966 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 997 | base = kaddr + con->out_msg_pos.page_pos + bio_offset; |
Alex Elder | a9a0c51 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 998 | crc = crc32c(tmpcrc, base, len); |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 999 | msg->footer.data_crc = cpu_to_le32(crc); |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1000 | con->out_msg_pos.did_page_crc = true; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1001 | } |
Alex Elder | e36b13c | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1002 | ret = ceph_tcp_sendpage(con->sock, page, |
Alex Elder | 9bd1966 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1003 | con->out_msg_pos.page_pos + bio_offset, |
Alex Elder | e36b13c | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1004 | len, 1); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1005 | |
Alex Elder | 0cdf9e6 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1006 | if (do_datacrc) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1007 | kunmap(page); |
| 1008 | |
| 1009 | if (ret <= 0) |
| 1010 | goto out; |
| 1011 | |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 1012 | out_msg_pos_next(con, page, len, (size_t) ret, in_trail); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | dout("write_partial_msg_pages %p msg %p done\n", con, msg); |
| 1016 | |
| 1017 | /* prepare and queue up footer, too */ |
Alex Elder | 37675b0 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1018 | if (!do_datacrc) |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 1019 | msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC; |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 1020 | con_out_kvec_reset(con); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 1021 | prepare_write_message_footer(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1022 | ret = 1; |
| 1023 | out: |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * write some zeros |
| 1029 | */ |
| 1030 | static int write_partial_skip(struct ceph_connection *con) |
| 1031 | { |
| 1032 | int ret; |
| 1033 | |
| 1034 | while (con->out_skip > 0) { |
Alex Elder | 3173913 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1035 | size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1036 | |
Alex Elder | 3173913 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1037 | ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1038 | if (ret <= 0) |
| 1039 | goto out; |
| 1040 | con->out_skip -= ret; |
| 1041 | } |
| 1042 | ret = 1; |
| 1043 | out: |
| 1044 | return ret; |
| 1045 | } |
| 1046 | |
| 1047 | /* |
| 1048 | * Prepare to read connection handshake, or an ack. |
| 1049 | */ |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1050 | static void prepare_read_banner(struct ceph_connection *con) |
| 1051 | { |
| 1052 | dout("prepare_read_banner %p\n", con); |
| 1053 | con->in_base_pos = 0; |
| 1054 | } |
| 1055 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1056 | static void prepare_read_connect(struct ceph_connection *con) |
| 1057 | { |
| 1058 | dout("prepare_read_connect %p\n", con); |
| 1059 | con->in_base_pos = 0; |
| 1060 | } |
| 1061 | |
| 1062 | static void prepare_read_ack(struct ceph_connection *con) |
| 1063 | { |
| 1064 | dout("prepare_read_ack %p\n", con); |
| 1065 | con->in_base_pos = 0; |
| 1066 | } |
| 1067 | |
| 1068 | static void prepare_read_tag(struct ceph_connection *con) |
| 1069 | { |
| 1070 | dout("prepare_read_tag %p\n", con); |
| 1071 | con->in_base_pos = 0; |
| 1072 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Prepare to read a message. |
| 1077 | */ |
| 1078 | static int prepare_read_message(struct ceph_connection *con) |
| 1079 | { |
| 1080 | dout("prepare_read_message %p\n", con); |
| 1081 | BUG_ON(con->in_msg != NULL); |
| 1082 | con->in_base_pos = 0; |
| 1083 | con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0; |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | |
| 1088 | static int read_partial(struct ceph_connection *con, |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1089 | int end, int size, void *object) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1090 | { |
Alex Elder | e6cee71 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1091 | while (con->in_base_pos < end) { |
| 1092 | int left = end - con->in_base_pos; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1093 | int have = size - left; |
| 1094 | int ret = ceph_tcp_recvmsg(con->sock, object + have, left); |
| 1095 | if (ret <= 0) |
| 1096 | return ret; |
| 1097 | con->in_base_pos += ret; |
| 1098 | } |
| 1099 | return 1; |
| 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | /* |
| 1104 | * Read all or part of the connect-side handshake on a new connection |
| 1105 | */ |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1106 | static int read_partial_banner(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1107 | { |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1108 | int size; |
| 1109 | int end; |
| 1110 | int ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1111 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1112 | 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] | 1113 | |
| 1114 | /* peer's banner */ |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1115 | size = strlen(CEPH_BANNER); |
| 1116 | end = size; |
| 1117 | ret = read_partial(con, end, size, con->in_banner); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1118 | if (ret <= 0) |
| 1119 | goto out; |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1120 | |
| 1121 | size = sizeof (con->actual_peer_addr); |
| 1122 | end += size; |
| 1123 | ret = read_partial(con, end, size, &con->actual_peer_addr); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1124 | if (ret <= 0) |
| 1125 | goto out; |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1126 | |
| 1127 | size = sizeof (con->peer_addr_for_me); |
| 1128 | end += size; |
| 1129 | ret = read_partial(con, end, size, &con->peer_addr_for_me); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1130 | if (ret <= 0) |
| 1131 | goto out; |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1132 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1133 | out: |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | static int read_partial_connect(struct ceph_connection *con) |
| 1138 | { |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1139 | int size; |
| 1140 | int end; |
| 1141 | int ret; |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1142 | |
| 1143 | dout("read_partial_connect %p at %d\n", con, con->in_base_pos); |
| 1144 | |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1145 | size = sizeof (con->in_reply); |
| 1146 | end = size; |
| 1147 | ret = read_partial(con, end, size, &con->in_reply); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1148 | if (ret <= 0) |
| 1149 | goto out; |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1150 | |
| 1151 | size = le32_to_cpu(con->in_reply.authorizer_len); |
| 1152 | end += size; |
| 1153 | ret = read_partial(con, end, size, con->auth_reply_buf); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1154 | if (ret <= 0) |
| 1155 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1156 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1157 | dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n", |
| 1158 | con, (int)con->in_reply.tag, |
| 1159 | le32_to_cpu(con->in_reply.connect_seq), |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1160 | le32_to_cpu(con->in_reply.global_seq)); |
| 1161 | out: |
| 1162 | return ret; |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1163 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | * Verify the hello banner looks okay. |
| 1168 | */ |
| 1169 | static int verify_hello(struct ceph_connection *con) |
| 1170 | { |
| 1171 | if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) { |
Sage Weil | 13e38c8 | 2009-10-09 16:36:34 -0700 | [diff] [blame] | 1172 | pr_err("connect to %s got bad banner\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1173 | ceph_pr_addr(&con->peer_addr.in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1174 | con->error_msg = "protocol error, bad banner"; |
| 1175 | return -1; |
| 1176 | } |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | static bool addr_is_blank(struct sockaddr_storage *ss) |
| 1181 | { |
| 1182 | switch (ss->ss_family) { |
| 1183 | case AF_INET: |
| 1184 | return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0; |
| 1185 | case AF_INET6: |
| 1186 | return |
| 1187 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 && |
| 1188 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 && |
| 1189 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 && |
| 1190 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0; |
| 1191 | } |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
| 1195 | static int addr_port(struct sockaddr_storage *ss) |
| 1196 | { |
| 1197 | switch (ss->ss_family) { |
| 1198 | case AF_INET: |
Sage Weil | f28bcfb | 2009-11-04 11:46:35 -0800 | [diff] [blame] | 1199 | return ntohs(((struct sockaddr_in *)ss)->sin_port); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1200 | case AF_INET6: |
Sage Weil | f28bcfb | 2009-11-04 11:46:35 -0800 | [diff] [blame] | 1201 | return ntohs(((struct sockaddr_in6 *)ss)->sin6_port); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1202 | } |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | static void addr_set_port(struct sockaddr_storage *ss, int p) |
| 1207 | { |
| 1208 | switch (ss->ss_family) { |
| 1209 | case AF_INET: |
| 1210 | ((struct sockaddr_in *)ss)->sin_port = htons(p); |
Sage Weil | a2a7960 | 2011-05-12 15:34:24 -0700 | [diff] [blame] | 1211 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1212 | case AF_INET6: |
| 1213 | ((struct sockaddr_in6 *)ss)->sin6_port = htons(p); |
Sage Weil | a2a7960 | 2011-05-12 15:34:24 -0700 | [diff] [blame] | 1214 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | /* |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1219 | * Unlike other *_pton function semantics, zero indicates success. |
| 1220 | */ |
| 1221 | static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss, |
| 1222 | char delim, const char **ipend) |
| 1223 | { |
Alex Elder | 99f0f3b | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 1224 | struct sockaddr_in *in4 = (struct sockaddr_in *) ss; |
| 1225 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss; |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1226 | |
| 1227 | memset(ss, 0, sizeof(*ss)); |
| 1228 | |
| 1229 | if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) { |
| 1230 | ss->ss_family = AF_INET; |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) { |
| 1235 | ss->ss_family = AF_INET6; |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | return -EINVAL; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | * Extract hostname string and resolve using kernel DNS facility. |
| 1244 | */ |
| 1245 | #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER |
| 1246 | static int ceph_dns_resolve_name(const char *name, size_t namelen, |
| 1247 | struct sockaddr_storage *ss, char delim, const char **ipend) |
| 1248 | { |
| 1249 | const char *end, *delim_p; |
| 1250 | char *colon_p, *ip_addr = NULL; |
| 1251 | int ip_len, ret; |
| 1252 | |
| 1253 | /* |
| 1254 | * The end of the hostname occurs immediately preceding the delimiter or |
| 1255 | * the port marker (':') where the delimiter takes precedence. |
| 1256 | */ |
| 1257 | delim_p = memchr(name, delim, namelen); |
| 1258 | colon_p = memchr(name, ':', namelen); |
| 1259 | |
| 1260 | if (delim_p && colon_p) |
| 1261 | end = delim_p < colon_p ? delim_p : colon_p; |
| 1262 | else if (!delim_p && colon_p) |
| 1263 | end = colon_p; |
| 1264 | else { |
| 1265 | end = delim_p; |
| 1266 | if (!end) /* case: hostname:/ */ |
| 1267 | end = name + namelen; |
| 1268 | } |
| 1269 | |
| 1270 | if (end <= name) |
| 1271 | return -EINVAL; |
| 1272 | |
| 1273 | /* do dns_resolve upcall */ |
| 1274 | ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL); |
| 1275 | if (ip_len > 0) |
| 1276 | ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL); |
| 1277 | else |
| 1278 | ret = -ESRCH; |
| 1279 | |
| 1280 | kfree(ip_addr); |
| 1281 | |
| 1282 | *ipend = end; |
| 1283 | |
| 1284 | pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name, |
| 1285 | ret, ret ? "failed" : ceph_pr_addr(ss)); |
| 1286 | |
| 1287 | return ret; |
| 1288 | } |
| 1289 | #else |
| 1290 | static inline int ceph_dns_resolve_name(const char *name, size_t namelen, |
| 1291 | struct sockaddr_storage *ss, char delim, const char **ipend) |
| 1292 | { |
| 1293 | return -EINVAL; |
| 1294 | } |
| 1295 | #endif |
| 1296 | |
| 1297 | /* |
| 1298 | * Parse a server name (IP or hostname). If a valid IP address is not found |
| 1299 | * then try to extract a hostname to resolve using userspace DNS upcall. |
| 1300 | */ |
| 1301 | static int ceph_parse_server_name(const char *name, size_t namelen, |
| 1302 | struct sockaddr_storage *ss, char delim, const char **ipend) |
| 1303 | { |
| 1304 | int ret; |
| 1305 | |
| 1306 | ret = ceph_pton(name, namelen, ss, delim, ipend); |
| 1307 | if (ret) |
| 1308 | ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend); |
| 1309 | |
| 1310 | return ret; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1314 | * Parse an ip[:port] list into an addr array. Use the default |
| 1315 | * monitor port if a port isn't specified. |
| 1316 | */ |
| 1317 | int ceph_parse_ips(const char *c, const char *end, |
| 1318 | struct ceph_entity_addr *addr, |
| 1319 | int max_count, int *count) |
| 1320 | { |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1321 | int i, ret = -EINVAL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1322 | const char *p = c; |
| 1323 | |
| 1324 | dout("parse_ips on '%.*s'\n", (int)(end-c), c); |
| 1325 | for (i = 0; i < max_count; i++) { |
| 1326 | const char *ipend; |
| 1327 | struct sockaddr_storage *ss = &addr[i].in_addr; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1328 | int port; |
Sage Weil | 39139f6 | 2010-07-08 09:54:52 -0700 | [diff] [blame] | 1329 | char delim = ','; |
| 1330 | |
| 1331 | if (*p == '[') { |
| 1332 | delim = ']'; |
| 1333 | p++; |
| 1334 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1335 | |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1336 | ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend); |
| 1337 | if (ret) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1338 | goto bad; |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1339 | ret = -EINVAL; |
| 1340 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1341 | p = ipend; |
| 1342 | |
Sage Weil | 39139f6 | 2010-07-08 09:54:52 -0700 | [diff] [blame] | 1343 | if (delim == ']') { |
| 1344 | if (*p != ']') { |
| 1345 | dout("missing matching ']'\n"); |
| 1346 | goto bad; |
| 1347 | } |
| 1348 | p++; |
| 1349 | } |
| 1350 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1351 | /* port? */ |
| 1352 | if (p < end && *p == ':') { |
| 1353 | port = 0; |
| 1354 | p++; |
| 1355 | while (p < end && *p >= '0' && *p <= '9') { |
| 1356 | port = (port * 10) + (*p - '0'); |
| 1357 | p++; |
| 1358 | } |
| 1359 | if (port > 65535 || port == 0) |
| 1360 | goto bad; |
| 1361 | } else { |
| 1362 | port = CEPH_MON_PORT; |
| 1363 | } |
| 1364 | |
| 1365 | addr_set_port(ss, port); |
| 1366 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1367 | dout("parse_ips got %s\n", ceph_pr_addr(ss)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1368 | |
| 1369 | if (p == end) |
| 1370 | break; |
| 1371 | if (*p != ',') |
| 1372 | goto bad; |
| 1373 | p++; |
| 1374 | } |
| 1375 | |
| 1376 | if (p != end) |
| 1377 | goto bad; |
| 1378 | |
| 1379 | if (count) |
| 1380 | *count = i + 1; |
| 1381 | return 0; |
| 1382 | |
| 1383 | bad: |
Sage Weil | 39139f6 | 2010-07-08 09:54:52 -0700 | [diff] [blame] | 1384 | pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c); |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1385 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1386 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1387 | EXPORT_SYMBOL(ceph_parse_ips); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1388 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1389 | static int process_banner(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1390 | { |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1391 | dout("process_banner on %p\n", con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1392 | |
| 1393 | if (verify_hello(con) < 0) |
| 1394 | return -1; |
| 1395 | |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 1396 | ceph_decode_addr(&con->actual_peer_addr); |
| 1397 | ceph_decode_addr(&con->peer_addr_for_me); |
| 1398 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1399 | /* |
| 1400 | * Make sure the other end is who we wanted. note that the other |
| 1401 | * end may not yet know their ip address, so if it's 0.0.0.0, give |
| 1402 | * them the benefit of the doubt. |
| 1403 | */ |
Sage Weil | 103e2d3 | 2010-01-07 16:12:36 -0800 | [diff] [blame] | 1404 | if (memcmp(&con->peer_addr, &con->actual_peer_addr, |
| 1405 | sizeof(con->peer_addr)) != 0 && |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1406 | !(addr_is_blank(&con->actual_peer_addr.in_addr) && |
| 1407 | con->actual_peer_addr.nonce == con->peer_addr.nonce)) { |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 1408 | pr_warning("wrong peer, want %s/%d, got %s/%d\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1409 | ceph_pr_addr(&con->peer_addr.in_addr), |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 1410 | (int)le32_to_cpu(con->peer_addr.nonce), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1411 | ceph_pr_addr(&con->actual_peer_addr.in_addr), |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 1412 | (int)le32_to_cpu(con->actual_peer_addr.nonce)); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 1413 | con->error_msg = "wrong peer at address"; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1414 | return -1; |
| 1415 | } |
| 1416 | |
| 1417 | /* |
| 1418 | * did we learn our address? |
| 1419 | */ |
| 1420 | if (addr_is_blank(&con->msgr->inst.addr.in_addr)) { |
| 1421 | int port = addr_port(&con->msgr->inst.addr.in_addr); |
| 1422 | |
| 1423 | memcpy(&con->msgr->inst.addr.in_addr, |
| 1424 | &con->peer_addr_for_me.in_addr, |
| 1425 | sizeof(con->peer_addr_for_me.in_addr)); |
| 1426 | addr_set_port(&con->msgr->inst.addr.in_addr, port); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 1427 | encode_my_addr(con->msgr); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1428 | dout("process_banner learned my addr is %s\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1429 | ceph_pr_addr(&con->msgr->inst.addr.in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1432 | set_bit(NEGOTIATING, &con->state); |
| 1433 | prepare_read_connect(con); |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1437 | static void fail_protocol(struct ceph_connection *con) |
| 1438 | { |
| 1439 | reset_connection(con); |
| 1440 | set_bit(CLOSED, &con->state); /* in case there's queued work */ |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1441 | } |
| 1442 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1443 | static int process_connect(struct ceph_connection *con) |
| 1444 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1445 | u64 sup_feat = con->msgr->supported_features; |
| 1446 | u64 req_feat = con->msgr->required_features; |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1447 | u64 server_feat = le64_to_cpu(con->in_reply.features); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 1448 | int ret; |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1449 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1450 | dout("process_connect on %p tag %d\n", con, (int)con->in_tag); |
| 1451 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1452 | switch (con->in_reply.tag) { |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1453 | case CEPH_MSGR_TAG_FEATURES: |
| 1454 | pr_err("%s%lld %s feature set mismatch," |
| 1455 | " my %llx < server's %llx, missing %llx\n", |
| 1456 | ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1457 | ceph_pr_addr(&con->peer_addr.in_addr), |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1458 | sup_feat, server_feat, server_feat & ~sup_feat); |
| 1459 | con->error_msg = "missing required protocol features"; |
| 1460 | fail_protocol(con); |
| 1461 | return -1; |
| 1462 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1463 | case CEPH_MSGR_TAG_BADPROTOVER: |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1464 | pr_err("%s%lld %s protocol version mismatch," |
| 1465 | " my %d != server's %d\n", |
| 1466 | ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1467 | ceph_pr_addr(&con->peer_addr.in_addr), |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1468 | le32_to_cpu(con->out_connect.protocol_version), |
| 1469 | le32_to_cpu(con->in_reply.protocol_version)); |
| 1470 | con->error_msg = "protocol version mismatch"; |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1471 | fail_protocol(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1472 | return -1; |
| 1473 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1474 | case CEPH_MSGR_TAG_BADAUTHORIZER: |
| 1475 | con->auth_retry++; |
| 1476 | dout("process_connect %p got BADAUTHORIZER attempt %d\n", con, |
| 1477 | con->auth_retry); |
| 1478 | if (con->auth_retry == 2) { |
| 1479 | con->error_msg = "connect authorization failure"; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1480 | return -1; |
| 1481 | } |
| 1482 | con->auth_retry = 1; |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 1483 | con_out_kvec_reset(con); |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 1484 | ret = prepare_write_connect(con); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 1485 | if (ret < 0) |
| 1486 | return ret; |
Sage Weil | 63733a0 | 2010-03-15 15:47:22 -0700 | [diff] [blame] | 1487 | prepare_read_connect(con); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1488 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1489 | |
| 1490 | case CEPH_MSGR_TAG_RESETSESSION: |
| 1491 | /* |
| 1492 | * If we connected with a large connect_seq but the peer |
| 1493 | * has no record of a session with us (no connection, or |
| 1494 | * connect_seq == 0), they will send RESETSESION to indicate |
| 1495 | * that they must have reset their session, and may have |
| 1496 | * dropped messages. |
| 1497 | */ |
| 1498 | dout("process_connect got RESET peer seq %u\n", |
| 1499 | le32_to_cpu(con->in_connect.connect_seq)); |
| 1500 | pr_err("%s%lld %s connection reset\n", |
| 1501 | ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1502 | ceph_pr_addr(&con->peer_addr.in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1503 | reset_connection(con); |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 1504 | con_out_kvec_reset(con); |
Alex Elder | 5a0f8fd | 2012-05-16 21:51:59 -0500 | [diff] [blame] | 1505 | ret = prepare_write_connect(con); |
| 1506 | if (ret < 0) |
| 1507 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1508 | prepare_read_connect(con); |
| 1509 | |
| 1510 | /* Tell ceph about it. */ |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1511 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1512 | pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name)); |
| 1513 | if (con->ops->peer_reset) |
| 1514 | con->ops->peer_reset(con); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1515 | mutex_lock(&con->mutex); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 1516 | if (test_bit(CLOSED, &con->state) || |
| 1517 | test_bit(OPENING, &con->state)) |
| 1518 | return -EAGAIN; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1519 | break; |
| 1520 | |
| 1521 | case CEPH_MSGR_TAG_RETRY_SESSION: |
| 1522 | /* |
| 1523 | * If we sent a smaller connect_seq than the peer has, try |
| 1524 | * again with a larger value. |
| 1525 | */ |
| 1526 | dout("process_connect got RETRY my seq = %u, peer_seq = %u\n", |
| 1527 | le32_to_cpu(con->out_connect.connect_seq), |
| 1528 | le32_to_cpu(con->in_connect.connect_seq)); |
| 1529 | con->connect_seq = le32_to_cpu(con->in_connect.connect_seq); |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 1530 | con_out_kvec_reset(con); |
Alex Elder | 5a0f8fd | 2012-05-16 21:51:59 -0500 | [diff] [blame] | 1531 | ret = prepare_write_connect(con); |
| 1532 | if (ret < 0) |
| 1533 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1534 | prepare_read_connect(con); |
| 1535 | break; |
| 1536 | |
| 1537 | case CEPH_MSGR_TAG_RETRY_GLOBAL: |
| 1538 | /* |
| 1539 | * If we sent a smaller global_seq than the peer has, try |
| 1540 | * again with a larger value. |
| 1541 | */ |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1542 | dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n", |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1543 | con->peer_global_seq, |
| 1544 | le32_to_cpu(con->in_connect.global_seq)); |
| 1545 | get_global_seq(con->msgr, |
| 1546 | le32_to_cpu(con->in_connect.global_seq)); |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 1547 | con_out_kvec_reset(con); |
Alex Elder | 5a0f8fd | 2012-05-16 21:51:59 -0500 | [diff] [blame] | 1548 | ret = prepare_write_connect(con); |
| 1549 | if (ret < 0) |
| 1550 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1551 | prepare_read_connect(con); |
| 1552 | break; |
| 1553 | |
| 1554 | case CEPH_MSGR_TAG_READY: |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1555 | if (req_feat & ~server_feat) { |
| 1556 | pr_err("%s%lld %s protocol feature mismatch," |
| 1557 | " my required %llx > server's %llx, need %llx\n", |
| 1558 | ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1559 | ceph_pr_addr(&con->peer_addr.in_addr), |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1560 | req_feat, server_feat, req_feat & ~server_feat); |
| 1561 | con->error_msg = "missing required protocol features"; |
| 1562 | fail_protocol(con); |
| 1563 | return -1; |
| 1564 | } |
Alex Elder | 3ec50d1 | 2012-05-23 14:35:23 -0500 | [diff] [blame^] | 1565 | clear_bit(NEGOTIATING, &con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1566 | clear_bit(CONNECTING, &con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1567 | con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq); |
| 1568 | con->connect_seq++; |
Sage Weil | aba558e | 2010-05-12 15:23:30 -0700 | [diff] [blame] | 1569 | con->peer_features = server_feat; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1570 | dout("process_connect got READY gseq %d cseq %d (%d)\n", |
| 1571 | con->peer_global_seq, |
| 1572 | le32_to_cpu(con->in_reply.connect_seq), |
| 1573 | con->connect_seq); |
| 1574 | WARN_ON(con->connect_seq != |
| 1575 | le32_to_cpu(con->in_reply.connect_seq)); |
Sage Weil | 92ac41d | 2009-12-14 14:56:56 -0800 | [diff] [blame] | 1576 | |
| 1577 | if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY) |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 1578 | set_bit(LOSSYTX, &con->flags); |
Sage Weil | 92ac41d | 2009-12-14 14:56:56 -0800 | [diff] [blame] | 1579 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1580 | prepare_read_tag(con); |
| 1581 | break; |
| 1582 | |
| 1583 | case CEPH_MSGR_TAG_WAIT: |
| 1584 | /* |
| 1585 | * If there is a connection race (we are opening |
| 1586 | * connections to each other), one of us may just have |
| 1587 | * to WAIT. This shouldn't happen if we are the |
| 1588 | * client. |
| 1589 | */ |
Sage Weil | 0417788 | 2011-05-12 15:33:17 -0700 | [diff] [blame] | 1590 | pr_err("process_connect got WAIT as client\n"); |
| 1591 | con->error_msg = "protocol error, got WAIT as client"; |
| 1592 | return -1; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1593 | |
| 1594 | default: |
| 1595 | pr_err("connect protocol error, will retry\n"); |
| 1596 | con->error_msg = "protocol error, garbage tag during connect"; |
| 1597 | return -1; |
| 1598 | } |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
| 1602 | |
| 1603 | /* |
| 1604 | * read (part of) an ack |
| 1605 | */ |
| 1606 | static int read_partial_ack(struct ceph_connection *con) |
| 1607 | { |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1608 | int size = sizeof (con->in_temp_ack); |
| 1609 | int end = size; |
| 1610 | |
| 1611 | return read_partial(con, end, size, &con->in_temp_ack); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | |
| 1615 | /* |
| 1616 | * We can finally discard anything that's been acked. |
| 1617 | */ |
| 1618 | static void process_ack(struct ceph_connection *con) |
| 1619 | { |
| 1620 | struct ceph_msg *m; |
| 1621 | u64 ack = le64_to_cpu(con->in_temp_ack); |
| 1622 | u64 seq; |
| 1623 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1624 | while (!list_empty(&con->out_sent)) { |
| 1625 | m = list_first_entry(&con->out_sent, struct ceph_msg, |
| 1626 | list_head); |
| 1627 | seq = le64_to_cpu(m->hdr.seq); |
| 1628 | if (seq > ack) |
| 1629 | break; |
| 1630 | dout("got ack for seq %llu type %d at %p\n", seq, |
| 1631 | le16_to_cpu(m->hdr.type), m); |
Sage Weil | 4cf9d54 | 2011-07-26 11:27:24 -0700 | [diff] [blame] | 1632 | m->ack_stamp = jiffies; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1633 | ceph_msg_remove(m); |
| 1634 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1635 | prepare_read_tag(con); |
| 1636 | } |
| 1637 | |
| 1638 | |
| 1639 | |
| 1640 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1641 | static int read_partial_message_section(struct ceph_connection *con, |
Sage Weil | 213c99e | 2010-08-03 10:25:11 -0700 | [diff] [blame] | 1642 | struct kvec *section, |
| 1643 | unsigned int sec_len, u32 *crc) |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1644 | { |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1645 | int ret, left; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1646 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1647 | BUG_ON(!section); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1648 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1649 | while (section->iov_len < sec_len) { |
| 1650 | BUG_ON(section->iov_base == NULL); |
| 1651 | left = sec_len - section->iov_len; |
| 1652 | ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base + |
| 1653 | section->iov_len, left); |
| 1654 | if (ret <= 0) |
| 1655 | return ret; |
| 1656 | section->iov_len += ret; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1657 | } |
Alex Elder | fe3ad59 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1658 | if (section->iov_len == sec_len) |
| 1659 | *crc = crc32c(0, section->iov_base, section->iov_len); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1660 | |
| 1661 | return 1; |
| 1662 | } |
| 1663 | |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 1664 | static bool ceph_con_in_msg_alloc(struct ceph_connection *con, |
| 1665 | struct ceph_msg_header *hdr); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1666 | |
| 1667 | |
| 1668 | static int read_partial_message_pages(struct ceph_connection *con, |
| 1669 | struct page **pages, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1670 | unsigned int data_len, bool do_datacrc) |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1671 | { |
| 1672 | void *p; |
| 1673 | int ret; |
| 1674 | int left; |
| 1675 | |
| 1676 | left = min((int)(data_len - con->in_msg_pos.data_pos), |
| 1677 | (int)(PAGE_SIZE - con->in_msg_pos.page_pos)); |
| 1678 | /* (page) data */ |
| 1679 | BUG_ON(pages == NULL); |
| 1680 | p = kmap(pages[con->in_msg_pos.page]); |
| 1681 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, |
| 1682 | left); |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1683 | if (ret > 0 && do_datacrc) |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1684 | con->in_data_crc = |
| 1685 | crc32c(con->in_data_crc, |
| 1686 | p + con->in_msg_pos.page_pos, ret); |
| 1687 | kunmap(pages[con->in_msg_pos.page]); |
| 1688 | if (ret <= 0) |
| 1689 | return ret; |
| 1690 | con->in_msg_pos.data_pos += ret; |
| 1691 | con->in_msg_pos.page_pos += ret; |
| 1692 | if (con->in_msg_pos.page_pos == PAGE_SIZE) { |
| 1693 | con->in_msg_pos.page_pos = 0; |
| 1694 | con->in_msg_pos.page++; |
| 1695 | } |
| 1696 | |
| 1697 | return ret; |
| 1698 | } |
| 1699 | |
| 1700 | #ifdef CONFIG_BLOCK |
| 1701 | static int read_partial_message_bio(struct ceph_connection *con, |
| 1702 | struct bio **bio_iter, int *bio_seg, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1703 | unsigned int data_len, bool do_datacrc) |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1704 | { |
| 1705 | struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg); |
| 1706 | void *p; |
| 1707 | int ret, left; |
| 1708 | |
| 1709 | if (IS_ERR(bv)) |
| 1710 | return PTR_ERR(bv); |
| 1711 | |
| 1712 | left = min((int)(data_len - con->in_msg_pos.data_pos), |
| 1713 | (int)(bv->bv_len - con->in_msg_pos.page_pos)); |
| 1714 | |
| 1715 | p = kmap(bv->bv_page) + bv->bv_offset; |
| 1716 | |
| 1717 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, |
| 1718 | left); |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1719 | if (ret > 0 && do_datacrc) |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1720 | con->in_data_crc = |
| 1721 | crc32c(con->in_data_crc, |
| 1722 | p + con->in_msg_pos.page_pos, ret); |
| 1723 | kunmap(bv->bv_page); |
| 1724 | if (ret <= 0) |
| 1725 | return ret; |
| 1726 | con->in_msg_pos.data_pos += ret; |
| 1727 | con->in_msg_pos.page_pos += ret; |
| 1728 | if (con->in_msg_pos.page_pos == bv->bv_len) { |
| 1729 | con->in_msg_pos.page_pos = 0; |
| 1730 | iter_bio_next(bio_iter, bio_seg); |
| 1731 | } |
| 1732 | |
| 1733 | return ret; |
| 1734 | } |
| 1735 | #endif |
| 1736 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1737 | /* |
| 1738 | * read (part of) a message. |
| 1739 | */ |
| 1740 | static int read_partial_message(struct ceph_connection *con) |
| 1741 | { |
| 1742 | struct ceph_msg *m = con->in_msg; |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1743 | int size; |
| 1744 | int end; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1745 | int ret; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1746 | unsigned int front_len, middle_len, data_len; |
Alex Elder | 37675b0 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1747 | bool do_datacrc = !con->msgr->nocrc; |
Sage Weil | ae18756 | 2010-04-22 07:47:01 -0700 | [diff] [blame] | 1748 | u64 seq; |
Alex Elder | fe3ad59 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1749 | u32 crc; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1750 | |
| 1751 | dout("read_partial_message con %p msg %p\n", con, m); |
| 1752 | |
| 1753 | /* header */ |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1754 | size = sizeof (con->in_hdr); |
| 1755 | end = size; |
| 1756 | ret = read_partial(con, end, size, &con->in_hdr); |
Alex Elder | 57dac9d | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1757 | if (ret <= 0) |
| 1758 | return ret; |
Alex Elder | fe3ad59 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1759 | |
| 1760 | crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc)); |
| 1761 | if (cpu_to_le32(crc) != con->in_hdr.crc) { |
| 1762 | pr_err("read_partial_message bad hdr " |
| 1763 | " crc %u != expected %u\n", |
| 1764 | crc, con->in_hdr.crc); |
| 1765 | return -EBADMSG; |
| 1766 | } |
| 1767 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1768 | front_len = le32_to_cpu(con->in_hdr.front_len); |
| 1769 | if (front_len > CEPH_MSG_MAX_FRONT_LEN) |
| 1770 | return -EIO; |
| 1771 | middle_len = le32_to_cpu(con->in_hdr.middle_len); |
| 1772 | if (middle_len > CEPH_MSG_MAX_DATA_LEN) |
| 1773 | return -EIO; |
| 1774 | data_len = le32_to_cpu(con->in_hdr.data_len); |
| 1775 | if (data_len > CEPH_MSG_MAX_DATA_LEN) |
| 1776 | return -EIO; |
| 1777 | |
Sage Weil | ae18756 | 2010-04-22 07:47:01 -0700 | [diff] [blame] | 1778 | /* verify seq# */ |
| 1779 | seq = le64_to_cpu(con->in_hdr.seq); |
| 1780 | if ((s64)seq - (s64)con->in_seq < 1) { |
Sage Weil | df9f86f | 2010-11-01 15:49:23 -0700 | [diff] [blame] | 1781 | pr_info("skipping %s%lld %s seq %lld expected %lld\n", |
Sage Weil | ae18756 | 2010-04-22 07:47:01 -0700 | [diff] [blame] | 1782 | ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1783 | ceph_pr_addr(&con->peer_addr.in_addr), |
Sage Weil | ae18756 | 2010-04-22 07:47:01 -0700 | [diff] [blame] | 1784 | seq, con->in_seq + 1); |
| 1785 | con->in_base_pos = -front_len - middle_len - data_len - |
| 1786 | sizeof(m->footer); |
| 1787 | con->in_tag = CEPH_MSGR_TAG_READY; |
Sage Weil | ae18756 | 2010-04-22 07:47:01 -0700 | [diff] [blame] | 1788 | return 0; |
| 1789 | } else if ((s64)seq - (s64)con->in_seq > 1) { |
| 1790 | pr_err("read_partial_message bad seq %lld expected %lld\n", |
| 1791 | seq, con->in_seq + 1); |
| 1792 | con->error_msg = "bad message sequence # for incoming message"; |
| 1793 | return -EBADMSG; |
| 1794 | } |
| 1795 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1796 | /* allocate message? */ |
| 1797 | if (!con->in_msg) { |
| 1798 | dout("got hdr type %d front %d data %d\n", con->in_hdr.type, |
| 1799 | con->in_hdr.front_len, con->in_hdr.data_len); |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 1800 | if (ceph_con_in_msg_alloc(con, &con->in_hdr)) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1801 | /* skip this message */ |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1802 | dout("alloc_msg said skip message\n"); |
Sage Weil | ae32be3 | 2010-06-13 10:30:19 -0700 | [diff] [blame] | 1803 | BUG_ON(con->in_msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1804 | con->in_base_pos = -front_len - middle_len - data_len - |
| 1805 | sizeof(m->footer); |
| 1806 | con->in_tag = CEPH_MSGR_TAG_READY; |
Sage Weil | 684be25 | 2010-04-21 20:45:59 -0700 | [diff] [blame] | 1807 | con->in_seq++; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1808 | return 0; |
| 1809 | } |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1810 | if (!con->in_msg) { |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 1811 | con->error_msg = |
| 1812 | "error allocating memory for incoming message"; |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1813 | return -ENOMEM; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1814 | } |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 1815 | |
| 1816 | BUG_ON(con->in_msg->con != con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1817 | m = con->in_msg; |
| 1818 | m->front.iov_len = 0; /* haven't read it yet */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1819 | if (m->middle) |
| 1820 | m->middle->vec.iov_len = 0; |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 1821 | |
| 1822 | con->in_msg_pos.page = 0; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1823 | if (m->pages) |
Sage Weil | c5c6b19 | 2010-11-09 12:40:00 -0800 | [diff] [blame] | 1824 | con->in_msg_pos.page_pos = m->page_alignment; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1825 | else |
| 1826 | con->in_msg_pos.page_pos = 0; |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 1827 | con->in_msg_pos.data_pos = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | /* front */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1831 | ret = read_partial_message_section(con, &m->front, front_len, |
| 1832 | &con->in_front_crc); |
| 1833 | if (ret <= 0) |
| 1834 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1835 | |
| 1836 | /* middle */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1837 | if (m->middle) { |
Sage Weil | 213c99e | 2010-08-03 10:25:11 -0700 | [diff] [blame] | 1838 | ret = read_partial_message_section(con, &m->middle->vec, |
| 1839 | middle_len, |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1840 | &con->in_middle_crc); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1841 | if (ret <= 0) |
| 1842 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1843 | } |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1844 | #ifdef CONFIG_BLOCK |
| 1845 | if (m->bio && !m->bio_iter) |
| 1846 | init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg); |
| 1847 | #endif |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1848 | |
| 1849 | /* (page) data */ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1850 | while (con->in_msg_pos.data_pos < data_len) { |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1851 | if (m->pages) { |
| 1852 | ret = read_partial_message_pages(con, m->pages, |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1853 | data_len, do_datacrc); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1854 | if (ret <= 0) |
| 1855 | return ret; |
| 1856 | #ifdef CONFIG_BLOCK |
| 1857 | } else if (m->bio) { |
| 1858 | |
| 1859 | ret = read_partial_message_bio(con, |
| 1860 | &m->bio_iter, &m->bio_seg, |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1861 | data_len, do_datacrc); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1862 | if (ret <= 0) |
| 1863 | return ret; |
| 1864 | #endif |
| 1865 | } else { |
| 1866 | BUG_ON(1); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1870 | /* footer */ |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1871 | size = sizeof (m->footer); |
| 1872 | end += size; |
| 1873 | ret = read_partial(con, end, size, &m->footer); |
Alex Elder | 57dac9d | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1874 | if (ret <= 0) |
| 1875 | return ret; |
| 1876 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1877 | dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n", |
| 1878 | m, front_len, m->footer.front_crc, middle_len, |
| 1879 | m->footer.middle_crc, data_len, m->footer.data_crc); |
| 1880 | |
| 1881 | /* crc ok? */ |
| 1882 | if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) { |
| 1883 | pr_err("read_partial_message %p front crc %u != exp. %u\n", |
| 1884 | m, con->in_front_crc, m->footer.front_crc); |
| 1885 | return -EBADMSG; |
| 1886 | } |
| 1887 | if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) { |
| 1888 | pr_err("read_partial_message %p middle crc %u != exp %u\n", |
| 1889 | m, con->in_middle_crc, m->footer.middle_crc); |
| 1890 | return -EBADMSG; |
| 1891 | } |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1892 | if (do_datacrc && |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1893 | (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 && |
| 1894 | con->in_data_crc != le32_to_cpu(m->footer.data_crc)) { |
| 1895 | pr_err("read_partial_message %p data crc %u != exp. %u\n", m, |
| 1896 | con->in_data_crc, le32_to_cpu(m->footer.data_crc)); |
| 1897 | return -EBADMSG; |
| 1898 | } |
| 1899 | |
| 1900 | return 1; /* done! */ |
| 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | * Process message. This happens in the worker thread. The callback should |
| 1905 | * be careful not to do anything that waits on other incoming messages or it |
| 1906 | * may deadlock. |
| 1907 | */ |
| 1908 | static void process_message(struct ceph_connection *con) |
| 1909 | { |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 1910 | struct ceph_msg *msg; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1911 | |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 1912 | BUG_ON(con->in_msg->con != con); |
| 1913 | con->in_msg->con = NULL; |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 1914 | msg = con->in_msg; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1915 | con->in_msg = NULL; |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 1916 | con->ops->put(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1917 | |
| 1918 | /* if first message, set peer_name */ |
| 1919 | if (con->peer_name.type == 0) |
Sage Weil | dbad185 | 2010-03-25 15:45:38 -0700 | [diff] [blame] | 1920 | con->peer_name = msg->hdr.src; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1921 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1922 | con->in_seq++; |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1923 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1924 | |
| 1925 | dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n", |
| 1926 | msg, le64_to_cpu(msg->hdr.seq), |
Sage Weil | dbad185 | 2010-03-25 15:45:38 -0700 | [diff] [blame] | 1927 | ENTITY_NAME(msg->hdr.src), |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1928 | le16_to_cpu(msg->hdr.type), |
| 1929 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), |
| 1930 | le32_to_cpu(msg->hdr.front_len), |
| 1931 | le32_to_cpu(msg->hdr.data_len), |
| 1932 | con->in_front_crc, con->in_middle_crc, con->in_data_crc); |
| 1933 | con->ops->dispatch(con, msg); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1934 | |
| 1935 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1936 | prepare_read_tag(con); |
| 1937 | } |
| 1938 | |
| 1939 | |
| 1940 | /* |
| 1941 | * Write something to the socket. Called in a worker thread when the |
| 1942 | * socket appears to be writeable and we have something ready to send. |
| 1943 | */ |
| 1944 | static int try_write(struct ceph_connection *con) |
| 1945 | { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1946 | int ret = 1; |
| 1947 | |
Sage Weil | d59315c | 2012-06-21 12:49:23 -0700 | [diff] [blame] | 1948 | dout("try_write start %p state %lu\n", con, con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1949 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1950 | more: |
| 1951 | dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes); |
| 1952 | |
| 1953 | /* open the socket first? */ |
| 1954 | if (con->sock == NULL) { |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 1955 | set_bit(CONNECTING, &con->state); |
| 1956 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 1957 | con_out_kvec_reset(con); |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 1958 | prepare_write_banner(con); |
Alex Elder | 5a0f8fd | 2012-05-16 21:51:59 -0500 | [diff] [blame] | 1959 | ret = prepare_write_connect(con); |
| 1960 | if (ret < 0) |
| 1961 | goto out; |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1962 | prepare_read_banner(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1963 | |
Sage Weil | cf3e5c4 | 2009-12-11 09:48:05 -0800 | [diff] [blame] | 1964 | BUG_ON(con->in_msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1965 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 1966 | dout("try_write initiating connect on %p new state %lu\n", |
| 1967 | con, con->state); |
Alex Elder | 41617d0 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 1968 | ret = ceph_tcp_connect(con); |
| 1969 | if (ret < 0) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1970 | con->error_msg = "connect error"; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1971 | goto out; |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | more_kvec: |
| 1976 | /* kvec data queued? */ |
| 1977 | if (con->out_skip) { |
| 1978 | ret = write_partial_skip(con); |
| 1979 | if (ret <= 0) |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 1980 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1981 | } |
| 1982 | if (con->out_kvec_left) { |
| 1983 | ret = write_partial_kvec(con); |
| 1984 | if (ret <= 0) |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 1985 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | /* msg pages? */ |
| 1989 | if (con->out_msg) { |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 1990 | if (con->out_msg_done) { |
| 1991 | ceph_msg_put(con->out_msg); |
| 1992 | con->out_msg = NULL; /* we're done with this one */ |
| 1993 | goto do_next; |
| 1994 | } |
| 1995 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1996 | ret = write_partial_msg_pages(con); |
| 1997 | if (ret == 1) |
| 1998 | goto more_kvec; /* we need to send the footer, too! */ |
| 1999 | if (ret == 0) |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 2000 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2001 | if (ret < 0) { |
| 2002 | dout("try_write write_partial_msg_pages err %d\n", |
| 2003 | ret); |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 2004 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2005 | } |
| 2006 | } |
| 2007 | |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 2008 | do_next: |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2009 | if (!test_bit(CONNECTING, &con->state)) { |
| 2010 | /* is anything else pending? */ |
| 2011 | if (!list_empty(&con->out_queue)) { |
| 2012 | prepare_write_message(con); |
| 2013 | goto more; |
| 2014 | } |
| 2015 | if (con->in_seq > con->in_seq_acked) { |
| 2016 | prepare_write_ack(con); |
| 2017 | goto more; |
| 2018 | } |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2019 | if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2020 | prepare_write_keepalive(con); |
| 2021 | goto more; |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | /* Nothing to do! */ |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2026 | clear_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2027 | dout("try_write nothing else to write.\n"); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2028 | ret = 0; |
| 2029 | out: |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 2030 | dout("try_write done on %p ret %d\n", con, ret); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2031 | return ret; |
| 2032 | } |
| 2033 | |
| 2034 | |
| 2035 | |
| 2036 | /* |
| 2037 | * Read what we can from the socket. |
| 2038 | */ |
| 2039 | static int try_read(struct ceph_connection *con) |
| 2040 | { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2041 | int ret = -1; |
| 2042 | |
| 2043 | if (!con->sock) |
| 2044 | return 0; |
| 2045 | |
| 2046 | if (test_bit(STANDBY, &con->state)) |
| 2047 | return 0; |
| 2048 | |
| 2049 | dout("try_read start on %p\n", con); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2050 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2051 | more: |
| 2052 | dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag, |
| 2053 | con->in_base_pos); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2054 | |
| 2055 | /* |
| 2056 | * process_connect and process_message drop and re-take |
| 2057 | * con->mutex. make sure we handle a racing close or reopen. |
| 2058 | */ |
| 2059 | if (test_bit(CLOSED, &con->state) || |
| 2060 | test_bit(OPENING, &con->state)) { |
| 2061 | ret = -EAGAIN; |
| 2062 | goto out; |
| 2063 | } |
| 2064 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2065 | if (test_bit(CONNECTING, &con->state)) { |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 2066 | if (!test_bit(NEGOTIATING, &con->state)) { |
| 2067 | dout("try_read connecting\n"); |
| 2068 | ret = read_partial_banner(con); |
| 2069 | if (ret <= 0) |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 2070 | goto out; |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2071 | ret = process_banner(con); |
| 2072 | if (ret < 0) |
| 2073 | goto out; |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 2074 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2075 | ret = read_partial_connect(con); |
| 2076 | if (ret <= 0) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2077 | goto out; |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2078 | ret = process_connect(con); |
| 2079 | if (ret < 0) |
| 2080 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2081 | goto more; |
| 2082 | } |
| 2083 | |
| 2084 | if (con->in_base_pos < 0) { |
| 2085 | /* |
| 2086 | * skipping + discarding content. |
| 2087 | * |
| 2088 | * FIXME: there must be a better way to do this! |
| 2089 | */ |
Alex Elder | 84495f4 | 2012-02-15 07:43:55 -0600 | [diff] [blame] | 2090 | static char buf[SKIP_BUF_SIZE]; |
| 2091 | int skip = min((int) sizeof (buf), -con->in_base_pos); |
| 2092 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2093 | dout("skipping %d / %d bytes\n", skip, -con->in_base_pos); |
| 2094 | ret = ceph_tcp_recvmsg(con->sock, buf, skip); |
| 2095 | if (ret <= 0) |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2096 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2097 | con->in_base_pos += ret; |
| 2098 | if (con->in_base_pos) |
| 2099 | goto more; |
| 2100 | } |
| 2101 | if (con->in_tag == CEPH_MSGR_TAG_READY) { |
| 2102 | /* |
| 2103 | * what's next? |
| 2104 | */ |
| 2105 | ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1); |
| 2106 | if (ret <= 0) |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2107 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2108 | dout("try_read got tag %d\n", (int)con->in_tag); |
| 2109 | switch (con->in_tag) { |
| 2110 | case CEPH_MSGR_TAG_MSG: |
| 2111 | prepare_read_message(con); |
| 2112 | break; |
| 2113 | case CEPH_MSGR_TAG_ACK: |
| 2114 | prepare_read_ack(con); |
| 2115 | break; |
| 2116 | case CEPH_MSGR_TAG_CLOSE: |
| 2117 | set_bit(CLOSED, &con->state); /* fixme */ |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2118 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2119 | default: |
| 2120 | goto bad_tag; |
| 2121 | } |
| 2122 | } |
| 2123 | if (con->in_tag == CEPH_MSGR_TAG_MSG) { |
| 2124 | ret = read_partial_message(con); |
| 2125 | if (ret <= 0) { |
| 2126 | switch (ret) { |
| 2127 | case -EBADMSG: |
| 2128 | con->error_msg = "bad crc"; |
| 2129 | ret = -EIO; |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2130 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2131 | case -EIO: |
| 2132 | con->error_msg = "io error"; |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2133 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2134 | } |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2135 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2136 | } |
| 2137 | if (con->in_tag == CEPH_MSGR_TAG_READY) |
| 2138 | goto more; |
| 2139 | process_message(con); |
| 2140 | goto more; |
| 2141 | } |
| 2142 | if (con->in_tag == CEPH_MSGR_TAG_ACK) { |
| 2143 | ret = read_partial_ack(con); |
| 2144 | if (ret <= 0) |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2145 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2146 | process_ack(con); |
| 2147 | goto more; |
| 2148 | } |
| 2149 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2150 | out: |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2151 | dout("try_read done on %p ret %d\n", con, ret); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2152 | return ret; |
| 2153 | |
| 2154 | bad_tag: |
| 2155 | pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag); |
| 2156 | con->error_msg = "protocol error, garbage tag"; |
| 2157 | ret = -1; |
| 2158 | goto out; |
| 2159 | } |
| 2160 | |
| 2161 | |
| 2162 | /* |
| 2163 | * Atomically queue work on a connection. Bump @con reference to |
| 2164 | * avoid races with connection teardown. |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2165 | */ |
| 2166 | static void queue_con(struct ceph_connection *con) |
| 2167 | { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2168 | if (!con->ops->get(con)) { |
| 2169 | dout("queue_con %p ref count 0\n", con); |
| 2170 | return; |
| 2171 | } |
| 2172 | |
Tejun Heo | f363e45fd1 | 2011-01-03 14:49:46 +0100 | [diff] [blame] | 2173 | if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2174 | dout("queue_con %p - already queued\n", con); |
| 2175 | con->ops->put(con); |
| 2176 | } else { |
| 2177 | dout("queue_con %p\n", con); |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | /* |
| 2182 | * Do some work on a connection. Drop a connection ref when we're done. |
| 2183 | */ |
| 2184 | static void con_work(struct work_struct *work) |
| 2185 | { |
| 2186 | struct ceph_connection *con = container_of(work, struct ceph_connection, |
| 2187 | work.work); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2188 | int ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2189 | |
Sage Weil | 9dd4658 | 2010-04-28 13:51:50 -0700 | [diff] [blame] | 2190 | mutex_lock(&con->mutex); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2191 | restart: |
Alex Elder | 188048b | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 2192 | if (test_and_clear_bit(SOCK_CLOSED, &con->flags)) { |
Alex Elder | 3ec50d1 | 2012-05-23 14:35:23 -0500 | [diff] [blame^] | 2193 | if (test_and_clear_bit(CONNECTING, &con->state)) { |
| 2194 | clear_bit(NEGOTIATING, &con->state); |
Alex Elder | 188048b | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 2195 | con->error_msg = "connection failed"; |
Alex Elder | 3ec50d1 | 2012-05-23 14:35:23 -0500 | [diff] [blame^] | 2196 | } else { |
Alex Elder | 188048b | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 2197 | con->error_msg = "socket closed"; |
Alex Elder | 3ec50d1 | 2012-05-23 14:35:23 -0500 | [diff] [blame^] | 2198 | } |
Alex Elder | 188048b | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 2199 | goto fault; |
| 2200 | } |
| 2201 | |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2202 | if (test_and_clear_bit(BACKOFF, &con->flags)) { |
Sage Weil | 60bf8bf | 2011-03-04 12:24:28 -0800 | [diff] [blame] | 2203 | dout("con_work %p backing off\n", con); |
| 2204 | if (queue_delayed_work(ceph_msgr_wq, &con->work, |
| 2205 | round_jiffies_relative(con->delay))) { |
| 2206 | dout("con_work %p backoff %lu\n", con, con->delay); |
| 2207 | mutex_unlock(&con->mutex); |
| 2208 | return; |
| 2209 | } else { |
| 2210 | con->ops->put(con); |
| 2211 | dout("con_work %p FAILED to back off %lu\n", con, |
| 2212 | con->delay); |
| 2213 | } |
| 2214 | } |
Sage Weil | 9dd4658 | 2010-04-28 13:51:50 -0700 | [diff] [blame] | 2215 | |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2216 | if (test_bit(STANDBY, &con->state)) { |
| 2217 | dout("con_work %p STANDBY\n", con); |
| 2218 | goto done; |
| 2219 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2220 | if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */ |
| 2221 | dout("con_work CLOSED\n"); |
| 2222 | con_close_socket(con); |
| 2223 | goto done; |
| 2224 | } |
| 2225 | if (test_and_clear_bit(OPENING, &con->state)) { |
| 2226 | /* reopen w/ new peer */ |
| 2227 | dout("con_work OPENING\n"); |
| 2228 | con_close_socket(con); |
| 2229 | } |
| 2230 | |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2231 | ret = try_read(con); |
| 2232 | if (ret == -EAGAIN) |
| 2233 | goto restart; |
| 2234 | if (ret < 0) |
| 2235 | goto fault; |
| 2236 | |
| 2237 | ret = try_write(con); |
| 2238 | if (ret == -EAGAIN) |
| 2239 | goto restart; |
| 2240 | if (ret < 0) |
| 2241 | goto fault; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2242 | |
| 2243 | done: |
Sage Weil | 9dd4658 | 2010-04-28 13:51:50 -0700 | [diff] [blame] | 2244 | mutex_unlock(&con->mutex); |
Sage Weil | 9dd4658 | 2010-04-28 13:51:50 -0700 | [diff] [blame] | 2245 | done_unlocked: |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2246 | con->ops->put(con); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2247 | return; |
| 2248 | |
| 2249 | fault: |
| 2250 | mutex_unlock(&con->mutex); |
| 2251 | ceph_fault(con); /* error/fault path */ |
| 2252 | goto done_unlocked; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | |
| 2256 | /* |
| 2257 | * Generic error/fault handler. A retry mechanism is used with |
| 2258 | * exponential backoff |
| 2259 | */ |
| 2260 | static void ceph_fault(struct ceph_connection *con) |
| 2261 | { |
| 2262 | pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2263 | ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2264 | dout("fault %p state %lu to peer %s\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2265 | con, con->state, ceph_pr_addr(&con->peer_addr.in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2266 | |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2267 | if (test_bit(LOSSYTX, &con->flags)) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2268 | dout("fault on LOSSYTX channel\n"); |
| 2269 | goto out; |
| 2270 | } |
| 2271 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2272 | mutex_lock(&con->mutex); |
Sage Weil | 91e45ce3 | 2010-02-15 12:05:09 -0800 | [diff] [blame] | 2273 | if (test_bit(CLOSED, &con->state)) |
| 2274 | goto out_unlock; |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2275 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2276 | con_close_socket(con); |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 2277 | |
| 2278 | if (con->in_msg) { |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2279 | BUG_ON(con->in_msg->con != con); |
| 2280 | con->in_msg->con = NULL; |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 2281 | ceph_msg_put(con->in_msg); |
| 2282 | con->in_msg = NULL; |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 2283 | con->ops->put(con); |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 2284 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2285 | |
Sage Weil | e80a52d | 2010-02-25 12:40:45 -0800 | [diff] [blame] | 2286 | /* Requeue anything that hasn't been acked */ |
| 2287 | list_splice_init(&con->out_sent, &con->out_queue); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 2288 | |
Sage Weil | e76661d | 2011-03-03 10:10:15 -0800 | [diff] [blame] | 2289 | /* If there are no messages queued or keepalive pending, place |
| 2290 | * the connection in a STANDBY state */ |
| 2291 | if (list_empty(&con->out_queue) && |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2292 | !test_bit(KEEPALIVE_PENDING, &con->flags)) { |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2293 | dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con); |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2294 | clear_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2295 | set_bit(STANDBY, &con->state); |
Sage Weil | e80a52d | 2010-02-25 12:40:45 -0800 | [diff] [blame] | 2296 | } else { |
| 2297 | /* retry after a delay. */ |
| 2298 | if (con->delay == 0) |
| 2299 | con->delay = BASE_DELAY_INTERVAL; |
| 2300 | else if (con->delay < MAX_DELAY_INTERVAL) |
| 2301 | con->delay *= 2; |
Sage Weil | e80a52d | 2010-02-25 12:40:45 -0800 | [diff] [blame] | 2302 | con->ops->get(con); |
| 2303 | if (queue_delayed_work(ceph_msgr_wq, &con->work, |
Sage Weil | 60bf8bf | 2011-03-04 12:24:28 -0800 | [diff] [blame] | 2304 | round_jiffies_relative(con->delay))) { |
| 2305 | dout("fault queued %p delay %lu\n", con, con->delay); |
| 2306 | } else { |
Sage Weil | e80a52d | 2010-02-25 12:40:45 -0800 | [diff] [blame] | 2307 | con->ops->put(con); |
Sage Weil | 60bf8bf | 2011-03-04 12:24:28 -0800 | [diff] [blame] | 2308 | dout("fault failed to queue %p delay %lu, backoff\n", |
| 2309 | con, con->delay); |
| 2310 | /* |
| 2311 | * In many cases we see a socket state change |
| 2312 | * while con_work is running and end up |
| 2313 | * queuing (non-delayed) work, such that we |
| 2314 | * can't backoff with a delay. Set a flag so |
| 2315 | * that when con_work restarts we schedule the |
| 2316 | * delay then. |
| 2317 | */ |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2318 | set_bit(BACKOFF, &con->flags); |
Sage Weil | 60bf8bf | 2011-03-04 12:24:28 -0800 | [diff] [blame] | 2319 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2320 | } |
| 2321 | |
Sage Weil | 91e45ce3 | 2010-02-15 12:05:09 -0800 | [diff] [blame] | 2322 | out_unlock: |
| 2323 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2324 | out: |
Sage Weil | 161fd65 | 2010-02-25 12:38:57 -0800 | [diff] [blame] | 2325 | /* |
| 2326 | * in case we faulted due to authentication, invalidate our |
| 2327 | * current tickets so that we can get new ones. |
Sage Weil | 213c99e | 2010-08-03 10:25:11 -0700 | [diff] [blame] | 2328 | */ |
Sage Weil | 161fd65 | 2010-02-25 12:38:57 -0800 | [diff] [blame] | 2329 | if (con->auth_retry && con->ops->invalidate_authorizer) { |
| 2330 | dout("calling invalidate_authorizer()\n"); |
| 2331 | con->ops->invalidate_authorizer(con); |
| 2332 | } |
| 2333 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2334 | if (con->ops->fault) |
| 2335 | con->ops->fault(con); |
| 2336 | } |
| 2337 | |
| 2338 | |
| 2339 | |
| 2340 | /* |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 2341 | * initialize a new messenger instance |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2342 | */ |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 2343 | void ceph_messenger_init(struct ceph_messenger *msgr, |
| 2344 | struct ceph_entity_addr *myaddr, |
| 2345 | u32 supported_features, |
| 2346 | u32 required_features, |
| 2347 | bool nocrc) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2348 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2349 | msgr->supported_features = supported_features; |
| 2350 | msgr->required_features = required_features; |
| 2351 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2352 | spin_lock_init(&msgr->global_seq_lock); |
| 2353 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2354 | if (myaddr) |
| 2355 | msgr->inst.addr = *myaddr; |
| 2356 | |
| 2357 | /* select a random nonce */ |
Sage Weil | ac8839d | 2010-01-27 14:28:10 -0800 | [diff] [blame] | 2358 | msgr->inst.addr.type = 0; |
Sage Weil | 103e2d3 | 2010-01-07 16:12:36 -0800 | [diff] [blame] | 2359 | get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce)); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 2360 | encode_my_addr(msgr); |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 2361 | msgr->nocrc = nocrc; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2362 | |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 2363 | dout("%s %p\n", __func__, msgr); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2364 | } |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 2365 | EXPORT_SYMBOL(ceph_messenger_init); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2366 | |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2367 | static void clear_standby(struct ceph_connection *con) |
| 2368 | { |
| 2369 | /* come back from STANDBY? */ |
| 2370 | if (test_and_clear_bit(STANDBY, &con->state)) { |
| 2371 | mutex_lock(&con->mutex); |
| 2372 | dout("clear_standby %p and ++connect_seq\n", con); |
| 2373 | con->connect_seq++; |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2374 | WARN_ON(test_bit(WRITE_PENDING, &con->flags)); |
| 2375 | WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags)); |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2376 | mutex_unlock(&con->mutex); |
| 2377 | } |
| 2378 | } |
| 2379 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2380 | /* |
| 2381 | * Queue up an outgoing message on the given connection. |
| 2382 | */ |
| 2383 | void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg) |
| 2384 | { |
| 2385 | if (test_bit(CLOSED, &con->state)) { |
| 2386 | dout("con_send %p closed, dropping %p\n", con, msg); |
| 2387 | ceph_msg_put(msg); |
| 2388 | return; |
| 2389 | } |
| 2390 | |
| 2391 | /* set src+dst */ |
Sage Weil | dbad185 | 2010-03-25 15:45:38 -0700 | [diff] [blame] | 2392 | msg->hdr.src = con->msgr->inst.name; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2393 | |
Sage Weil | 3ca02ef | 2010-03-01 15:25:00 -0800 | [diff] [blame] | 2394 | BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len)); |
| 2395 | |
Sage Weil | e84346b | 2010-05-11 21:20:38 -0700 | [diff] [blame] | 2396 | msg->needs_out_seq = true; |
| 2397 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2398 | /* queue */ |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2399 | mutex_lock(&con->mutex); |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2400 | |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2401 | BUG_ON(msg->con != NULL); |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 2402 | msg->con = con->ops->get(con); |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2403 | BUG_ON(msg->con == NULL); |
| 2404 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2405 | BUG_ON(!list_empty(&msg->list_head)); |
| 2406 | list_add_tail(&msg->list_head, &con->out_queue); |
| 2407 | dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg, |
| 2408 | ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type), |
| 2409 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), |
| 2410 | le32_to_cpu(msg->hdr.front_len), |
| 2411 | le32_to_cpu(msg->hdr.middle_len), |
| 2412 | le32_to_cpu(msg->hdr.data_len)); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2413 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2414 | |
| 2415 | /* if there wasn't anything waiting to send before, queue |
| 2416 | * new work */ |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2417 | clear_standby(con); |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2418 | if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2419 | queue_con(con); |
| 2420 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2421 | EXPORT_SYMBOL(ceph_con_send); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2422 | |
| 2423 | /* |
| 2424 | * Revoke a message that was previously queued for send |
| 2425 | */ |
Alex Elder | 6740a84 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2426 | void ceph_msg_revoke(struct ceph_msg *msg) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2427 | { |
Alex Elder | 6740a84 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2428 | struct ceph_connection *con = msg->con; |
| 2429 | |
| 2430 | if (!con) |
| 2431 | return; /* Message not in our possession */ |
| 2432 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2433 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2434 | if (!list_empty(&msg->list_head)) { |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2435 | dout("%s %p msg %p - was on queue\n", __func__, con, msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2436 | list_del_init(&msg->list_head); |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2437 | BUG_ON(msg->con == NULL); |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 2438 | msg->con->ops->put(msg->con); |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2439 | msg->con = NULL; |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2440 | msg->hdr.seq = 0; |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2441 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2442 | ceph_msg_put(msg); |
Sage Weil | ed98ada | 2010-07-05 12:15:14 -0700 | [diff] [blame] | 2443 | } |
| 2444 | if (con->out_msg == msg) { |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2445 | dout("%s %p msg %p - was sending\n", __func__, con, msg); |
Sage Weil | ed98ada | 2010-07-05 12:15:14 -0700 | [diff] [blame] | 2446 | con->out_msg = NULL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2447 | if (con->out_kvec_is_msg) { |
| 2448 | con->out_skip = con->out_kvec_bytes; |
| 2449 | con->out_kvec_is_msg = false; |
| 2450 | } |
Sage Weil | ed98ada | 2010-07-05 12:15:14 -0700 | [diff] [blame] | 2451 | msg->hdr.seq = 0; |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2452 | |
| 2453 | ceph_msg_put(msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2454 | } |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2455 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | /* |
Yehuda Sadeh | 0d59ab8 | 2010-01-13 17:03:23 -0800 | [diff] [blame] | 2459 | * Revoke a message that we may be reading data into |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2460 | */ |
Alex Elder | 8921d11 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2461 | void ceph_msg_revoke_incoming(struct ceph_msg *msg) |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2462 | { |
Alex Elder | 8921d11 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2463 | struct ceph_connection *con; |
| 2464 | |
| 2465 | BUG_ON(msg == NULL); |
| 2466 | if (!msg->con) { |
| 2467 | dout("%s msg %p null con\n", __func__, msg); |
| 2468 | |
| 2469 | return; /* Message not in our possession */ |
| 2470 | } |
| 2471 | |
| 2472 | con = msg->con; |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2473 | mutex_lock(&con->mutex); |
Alex Elder | 8921d11 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2474 | if (con->in_msg == msg) { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 2475 | unsigned int front_len = le32_to_cpu(con->in_hdr.front_len); |
| 2476 | unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len); |
| 2477 | unsigned int data_len = le32_to_cpu(con->in_hdr.data_len); |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2478 | |
| 2479 | /* skip rest of message */ |
Alex Elder | 8921d11 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2480 | dout("%s %p msg %p revoked\n", __func__, con, msg); |
| 2481 | con->in_base_pos = con->in_base_pos - |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2482 | sizeof(struct ceph_msg_header) - |
Yehuda Sadeh | 0d59ab8 | 2010-01-13 17:03:23 -0800 | [diff] [blame] | 2483 | front_len - |
| 2484 | middle_len - |
| 2485 | data_len - |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2486 | sizeof(struct ceph_msg_footer); |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2487 | ceph_msg_put(con->in_msg); |
| 2488 | con->in_msg = NULL; |
| 2489 | con->in_tag = CEPH_MSGR_TAG_READY; |
Sage Weil | 684be25 | 2010-04-21 20:45:59 -0700 | [diff] [blame] | 2490 | con->in_seq++; |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2491 | } else { |
Alex Elder | 8921d11 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2492 | dout("%s %p in_msg %p msg %p no-op\n", |
| 2493 | __func__, con, con->in_msg, msg); |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2494 | } |
| 2495 | mutex_unlock(&con->mutex); |
| 2496 | } |
| 2497 | |
| 2498 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2499 | * Queue a keepalive byte to ensure the tcp connection is alive. |
| 2500 | */ |
| 2501 | void ceph_con_keepalive(struct ceph_connection *con) |
| 2502 | { |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2503 | dout("con_keepalive %p\n", con); |
| 2504 | clear_standby(con); |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2505 | if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 && |
| 2506 | test_and_set_bit(WRITE_PENDING, &con->flags) == 0) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2507 | queue_con(con); |
| 2508 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2509 | EXPORT_SYMBOL(ceph_con_keepalive); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2510 | |
| 2511 | |
| 2512 | /* |
| 2513 | * construct a new message with given type, size |
| 2514 | * the new msg has a ref count of 1. |
| 2515 | */ |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 2516 | struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags, |
| 2517 | bool can_fail) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2518 | { |
| 2519 | struct ceph_msg *m; |
| 2520 | |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 2521 | m = kmalloc(sizeof(*m), flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2522 | if (m == NULL) |
| 2523 | goto out; |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2524 | kref_init(&m->kref); |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2525 | |
| 2526 | m->con = NULL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2527 | INIT_LIST_HEAD(&m->list_head); |
| 2528 | |
Sage Weil | 45c6ceb | 2010-05-11 15:01:51 -0700 | [diff] [blame] | 2529 | m->hdr.tid = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2530 | m->hdr.type = cpu_to_le16(type); |
Sage Weil | 45c6ceb | 2010-05-11 15:01:51 -0700 | [diff] [blame] | 2531 | m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); |
| 2532 | m->hdr.version = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2533 | m->hdr.front_len = cpu_to_le32(front_len); |
| 2534 | m->hdr.middle_len = 0; |
Sage Weil | bb25766 | 2010-04-01 16:07:23 -0700 | [diff] [blame] | 2535 | m->hdr.data_len = 0; |
| 2536 | m->hdr.data_off = 0; |
Sage Weil | 45c6ceb | 2010-05-11 15:01:51 -0700 | [diff] [blame] | 2537 | m->hdr.reserved = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2538 | m->footer.front_crc = 0; |
| 2539 | m->footer.middle_crc = 0; |
| 2540 | m->footer.data_crc = 0; |
Sage Weil | 45c6ceb | 2010-05-11 15:01:51 -0700 | [diff] [blame] | 2541 | m->footer.flags = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2542 | m->front_max = front_len; |
| 2543 | m->front_is_vmalloc = false; |
| 2544 | m->more_to_follow = false; |
Jim Schutt | c0d5f9d | 2011-09-16 08:27:31 -0600 | [diff] [blame] | 2545 | m->ack_stamp = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2546 | m->pool = NULL; |
| 2547 | |
Henry C Chang | ca20892 | 2011-05-03 02:29:56 +0000 | [diff] [blame] | 2548 | /* middle */ |
| 2549 | m->middle = NULL; |
| 2550 | |
| 2551 | /* data */ |
| 2552 | m->nr_pages = 0; |
| 2553 | m->page_alignment = 0; |
| 2554 | m->pages = NULL; |
| 2555 | m->pagelist = NULL; |
| 2556 | m->bio = NULL; |
| 2557 | m->bio_iter = NULL; |
| 2558 | m->bio_seg = 0; |
| 2559 | m->trail = NULL; |
| 2560 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2561 | /* front */ |
| 2562 | if (front_len) { |
| 2563 | if (front_len > PAGE_CACHE_SIZE) { |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 2564 | m->front.iov_base = __vmalloc(front_len, flags, |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2565 | PAGE_KERNEL); |
| 2566 | m->front_is_vmalloc = true; |
| 2567 | } else { |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 2568 | m->front.iov_base = kmalloc(front_len, flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2569 | } |
| 2570 | if (m->front.iov_base == NULL) { |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 2571 | dout("ceph_msg_new can't allocate %d bytes\n", |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2572 | front_len); |
| 2573 | goto out2; |
| 2574 | } |
| 2575 | } else { |
| 2576 | m->front.iov_base = NULL; |
| 2577 | } |
| 2578 | m->front.iov_len = front_len; |
| 2579 | |
Sage Weil | bb25766 | 2010-04-01 16:07:23 -0700 | [diff] [blame] | 2580 | dout("ceph_msg_new %p front %d\n", m, front_len); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2581 | return m; |
| 2582 | |
| 2583 | out2: |
| 2584 | ceph_msg_put(m); |
| 2585 | out: |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 2586 | if (!can_fail) { |
| 2587 | pr_err("msg_new can't create type %d front %d\n", type, |
| 2588 | front_len); |
Sage Weil | f0ed1b7 | 2011-08-09 15:05:07 -0700 | [diff] [blame] | 2589 | WARN_ON(1); |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 2590 | } else { |
| 2591 | dout("msg_new can't create type %d front %d\n", type, |
| 2592 | front_len); |
| 2593 | } |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 2594 | return NULL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2595 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2596 | EXPORT_SYMBOL(ceph_msg_new); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2597 | |
| 2598 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2599 | * Allocate "middle" portion of a message, if it is needed and wasn't |
| 2600 | * allocated by alloc_msg. This allows us to read a small fixed-size |
| 2601 | * per-type header in the front and then gracefully fail (i.e., |
| 2602 | * propagate the error to the caller based on info in the front) when |
| 2603 | * the middle is too large. |
| 2604 | */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2605 | 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] | 2606 | { |
| 2607 | int type = le16_to_cpu(msg->hdr.type); |
| 2608 | int middle_len = le32_to_cpu(msg->hdr.middle_len); |
| 2609 | |
| 2610 | dout("alloc_middle %p type %d %s middle_len %d\n", msg, type, |
| 2611 | ceph_msg_type_name(type), middle_len); |
| 2612 | BUG_ON(!middle_len); |
| 2613 | BUG_ON(msg->middle); |
| 2614 | |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame] | 2615 | msg->middle = ceph_buffer_new(middle_len, GFP_NOFS); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2616 | if (!msg->middle) |
| 2617 | return -ENOMEM; |
| 2618 | return 0; |
| 2619 | } |
| 2620 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2621 | /* |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2622 | * Allocate a message for receiving an incoming message on a |
| 2623 | * connection, and save the result in con->in_msg. Uses the |
| 2624 | * connection's private alloc_msg op if available. |
| 2625 | * |
| 2626 | * Returns true if the message should be skipped, false otherwise. |
| 2627 | * If true is returned (skip message), con->in_msg will be NULL. |
| 2628 | * If false is returned, con->in_msg will contain a pointer to the |
| 2629 | * newly-allocated message, or NULL in case of memory exhaustion. |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2630 | */ |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2631 | static bool ceph_con_in_msg_alloc(struct ceph_connection *con, |
| 2632 | struct ceph_msg_header *hdr) |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2633 | { |
| 2634 | int type = le16_to_cpu(hdr->type); |
| 2635 | int front_len = le32_to_cpu(hdr->front_len); |
| 2636 | int middle_len = le32_to_cpu(hdr->middle_len); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2637 | int ret; |
| 2638 | |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2639 | BUG_ON(con->in_msg != NULL); |
| 2640 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2641 | if (con->ops->alloc_msg) { |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2642 | int skip = 0; |
| 2643 | |
Yehuda Sadeh | 0547a9b | 2010-01-11 14:47:13 -0800 | [diff] [blame] | 2644 | mutex_unlock(&con->mutex); |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2645 | con->in_msg = con->ops->alloc_msg(con, hdr, &skip); |
Yehuda Sadeh | 0547a9b | 2010-01-11 14:47:13 -0800 | [diff] [blame] | 2646 | mutex_lock(&con->mutex); |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2647 | if (con->in_msg) { |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 2648 | con->in_msg->con = con->ops->get(con); |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2649 | BUG_ON(con->in_msg->con == NULL); |
| 2650 | } |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2651 | if (skip) |
| 2652 | con->in_msg = NULL; |
| 2653 | |
| 2654 | if (!con->in_msg) |
| 2655 | return skip != 0; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2656 | } |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2657 | if (!con->in_msg) { |
| 2658 | con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false); |
| 2659 | if (!con->in_msg) { |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2660 | pr_err("unable to allocate msg type %d len %d\n", |
| 2661 | type, front_len); |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2662 | return false; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2663 | } |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 2664 | con->in_msg->con = con->ops->get(con); |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2665 | BUG_ON(con->in_msg->con == NULL); |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2666 | con->in_msg->page_alignment = le16_to_cpu(hdr->data_off); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2667 | } |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2668 | memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr)); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2669 | |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2670 | if (middle_len && !con->in_msg->middle) { |
| 2671 | ret = ceph_alloc_middle(con, con->in_msg); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2672 | if (ret < 0) { |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2673 | ceph_msg_put(con->in_msg); |
| 2674 | con->in_msg = NULL; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2675 | } |
| 2676 | } |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 2677 | |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2678 | return false; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2679 | } |
| 2680 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2681 | |
| 2682 | /* |
| 2683 | * Free a generically kmalloc'd message. |
| 2684 | */ |
| 2685 | void ceph_msg_kfree(struct ceph_msg *m) |
| 2686 | { |
| 2687 | dout("msg_kfree %p\n", m); |
| 2688 | if (m->front_is_vmalloc) |
| 2689 | vfree(m->front.iov_base); |
| 2690 | else |
| 2691 | kfree(m->front.iov_base); |
| 2692 | kfree(m); |
| 2693 | } |
| 2694 | |
| 2695 | /* |
| 2696 | * Drop a msg ref. Destroy as needed. |
| 2697 | */ |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2698 | void ceph_msg_last_put(struct kref *kref) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2699 | { |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2700 | struct ceph_msg *m = container_of(kref, struct ceph_msg, kref); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2701 | |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2702 | dout("ceph_msg_put last one on %p\n", m); |
| 2703 | WARN_ON(!list_empty(&m->list_head)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2704 | |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2705 | /* drop middle, data, if any */ |
| 2706 | if (m->middle) { |
| 2707 | ceph_buffer_put(m->middle); |
| 2708 | m->middle = NULL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2709 | } |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2710 | m->nr_pages = 0; |
| 2711 | m->pages = NULL; |
| 2712 | |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 2713 | if (m->pagelist) { |
| 2714 | ceph_pagelist_release(m->pagelist); |
| 2715 | kfree(m->pagelist); |
| 2716 | m->pagelist = NULL; |
| 2717 | } |
| 2718 | |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 2719 | m->trail = NULL; |
| 2720 | |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2721 | if (m->pool) |
| 2722 | ceph_msgpool_put(m->pool, m); |
| 2723 | else |
| 2724 | ceph_msg_kfree(m); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2725 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2726 | EXPORT_SYMBOL(ceph_msg_last_put); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2727 | |
| 2728 | void ceph_msg_dump(struct ceph_msg *msg) |
| 2729 | { |
| 2730 | pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg, |
| 2731 | msg->front_max, msg->nr_pages); |
| 2732 | print_hex_dump(KERN_DEBUG, "header: ", |
| 2733 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2734 | &msg->hdr, sizeof(msg->hdr), true); |
| 2735 | print_hex_dump(KERN_DEBUG, " front: ", |
| 2736 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2737 | msg->front.iov_base, msg->front.iov_len, true); |
| 2738 | if (msg->middle) |
| 2739 | print_hex_dump(KERN_DEBUG, "middle: ", |
| 2740 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2741 | msg->middle->vec.iov_base, |
| 2742 | msg->middle->vec.iov_len, true); |
| 2743 | print_hex_dump(KERN_DEBUG, "footer: ", |
| 2744 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2745 | &msg->footer, sizeof(msg->footer), true); |
| 2746 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2747 | EXPORT_SYMBOL(ceph_msg_dump); |