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