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