blob: 36b440a00cc2854ebc5e130b8fe041beaa5dd475 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07002
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 Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070010#include <linux/socket.h>
11#include <linux/string.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070012#include <linux/bio.h>
13#include <linux/blkdev.h>
Noah Watkinsee3b56f2011-09-23 11:48:42 -070014#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070015#include <net/tcp.h>
16
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#include <linux/ceph/libceph.h>
18#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070022
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 Elderce2c8902012-05-22 22:15:49 -050032/* 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 Weil31b80062009-10-06 11:31:13 -070040/* static tag bytes (protocol control messages) */
41static char tag_msg = CEPH_MSGR_TAG_MSG;
42static char tag_ack = CEPH_MSGR_TAG_ACK;
43static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
44
Sage Weila6a53492010-04-13 14:07:07 -070045#ifdef CONFIG_LOCKDEP
46static struct lock_class_key socket_class;
47#endif
48
Alex Elder84495f42012-02-15 07:43:55 -060049/*
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 Weil31b80062009-10-06 11:31:13 -070054
55static void queue_con(struct ceph_connection *con);
56static void con_work(struct work_struct *);
57static void ceph_fault(struct ceph_connection *con);
58
Sage Weil31b80062009-10-06 11:31:13 -070059/*
Alex Elderf64a9312012-01-23 15:49:27 -060060 * Nicely render a sockaddr as a string. An array of formatted
61 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -070062 */
Alex Elderf64a9312012-01-23 15:49:27 -060063#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
68static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
69static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -070070
Alex Elder57666512012-01-23 15:49:27 -060071static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -060072
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070073const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -070074{
75 int i;
76 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -060077 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
78 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -070079
Alex Elderf64a9312012-01-23 15:49:27 -060080 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -070081 s = addr_str[i];
82
83 switch (ss->ss_family) {
84 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -060085 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
86 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -070087 break;
88
89 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -060090 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
91 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -070092 break;
93
94 default:
Alex Elderd3002b92012-02-14 14:05:33 -060095 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
96 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -070097 }
98
99 return s;
100}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700101EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700102
Sage Weil63f2d212009-11-03 15:17:56 -0800103static 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 Weil31b80062009-10-06 11:31:13 -0700109/*
110 * work queue for all reading and writing to/from the socket.
111 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600112static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700113
Alex Elder6173d1f2012-02-14 14:05:33 -0600114void _ceph_msgr_exit(void)
115{
Alex Elderd3002b92012-02-14 14:05:33 -0600116 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600117 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600118 ceph_msgr_wq = NULL;
119 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600120
Alex Elder6173d1f2012-02-14 14:05:33 -0600121 BUG_ON(zero_page == NULL);
122 kunmap(zero_page);
123 page_cache_release(zero_page);
124 zero_page = NULL;
125}
126
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700127int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700128{
Alex Elder57666512012-01-23 15:49:27 -0600129 BUG_ON(zero_page != NULL);
130 zero_page = ZERO_PAGE(0);
131 page_cache_get(zero_page);
132
Tejun Heof363e452011-01-03 14:49:46 +0100133 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600134 if (ceph_msgr_wq)
135 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600136
Alex Elder6173d1f2012-02-14 14:05:33 -0600137 pr_err("msgr_init failed to create workqueue\n");
138 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600139
Alex Elder6173d1f2012-02-14 14:05:33 -0600140 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700141}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700142EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700143
144void ceph_msgr_exit(void)
145{
Alex Elder57666512012-01-23 15:49:27 -0600146 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600147
Alex Elder6173d1f2012-02-14 14:05:33 -0600148 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700149}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700150EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700151
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700152void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700153{
154 flush_workqueue(ceph_msgr_wq);
155}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700156EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700157
Alex Elderce2c8902012-05-22 22:15:49 -0500158/* Connection socket state transition functions */
159
160static 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
169static 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
178static 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
187static 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
198static 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 Weila922d382010-05-29 09:41:23 -0700207
Sage Weil31b80062009-10-06 11:31:13 -0700208/*
209 * socket callback functions
210 */
211
212/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500213static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700214{
Alex Elderbd406142012-01-23 15:49:27 -0600215 struct ceph_connection *con = sk->sk_user_data;
216
Sage Weil31b80062009-10-06 11:31:13 -0700217 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500218 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700219 con, con->state);
220 queue_con(con);
221 }
222}
223
224/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500225static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700226{
Alex Elderd3002b92012-02-14 14:05:33 -0600227 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700228
Jim Schutt182fac22012-02-29 08:30:58 -0700229 /* 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 Elder327800b2012-05-22 11:41:43 -0500231 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700232 * 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 Elder928443c2012-05-22 11:41:43 -0500236 if (test_bit(WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700237 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500238 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700239 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
240 queue_con(con);
241 }
Sage Weil31b80062009-10-06 11:31:13 -0700242 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500243 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700244 }
Sage Weil31b80062009-10-06 11:31:13 -0700245}
246
247/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500248static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700249{
Alex Elderbd406142012-01-23 15:49:27 -0600250 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700251
Alex Elder327800b2012-05-22 11:41:43 -0500252 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700253 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 Elder327800b2012-05-22 11:41:43 -0500260 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700261 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500262 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500263 con_sock_state_closing(con);
Alex Elder928443c2012-05-22 11:41:43 -0500264 if (test_and_set_bit(SOCK_CLOSED, &con->flags) == 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700265 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 Elder327800b2012-05-22 11:41:43 -0500273 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500274 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700275 queue_con(con);
276 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600277 default: /* Everything else is uninteresting */
278 break;
Sage Weil31b80062009-10-06 11:31:13 -0700279 }
280}
281
282/*
283 * set up socket callbacks
284 */
285static void set_sock_callbacks(struct socket *sock,
286 struct ceph_connection *con)
287{
288 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600289 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500290 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 Weil31b80062009-10-06 11:31:13 -0700293}
294
295
296/*
297 * socket helpers
298 */
299
300/*
301 * initiate connection to a remote socket.
302 */
Alex Elder41617d02012-02-14 14:05:33 -0600303static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700304{
Sage Weilf91d3472010-07-01 15:18:31 -0700305 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700306 struct socket *sock;
307 int ret;
308
309 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700310 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
311 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700312 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600313 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700314 sock->sk->sk_allocation = GFP_NOFS;
315
Sage Weila6a53492010-04-13 14:07:07 -0700316#ifdef CONFIG_LOCKDEP
317 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
318#endif
319
Sage Weil31b80062009-10-06 11:31:13 -0700320 set_sock_callbacks(sock, con);
321
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700322 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700323
Sage Weilf91d3472010-07-01 15:18:31 -0700324 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
325 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700326 if (ret == -EINPROGRESS) {
327 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700328 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700329 sock->sk->sk_state);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600330 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700331 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700332 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700333 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700334 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700335
Alex Elder41617d02012-02-14 14:05:33 -0600336 return ret;
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600337 }
338 con->sock = sock;
Alex Elderce2c8902012-05-22 22:15:49 -0500339 con_sock_state_connecting(con);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600340
Alex Elder41617d02012-02-14 14:05:33 -0600341 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700342}
343
344static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
345{
346 struct kvec iov = {buf, len};
347 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800348 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700349
Sage Weil98bdb0a2011-01-25 08:17:48 -0800350 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
351 if (r == -EAGAIN)
352 r = 0;
353 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700354}
355
356/*
357 * write something. @more is true if caller will be sending more data
358 * shortly.
359 */
360static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
361 size_t kvlen, size_t len, int more)
362{
363 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800364 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700365
366 if (more)
367 msg.msg_flags |= MSG_MORE;
368 else
369 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
370
Sage Weil42961d22011-01-25 08:19:34 -0800371 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
372 if (r == -EAGAIN)
373 r = 0;
374 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700375}
376
Alex Elder31739132012-03-07 11:40:08 -0600377static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
378 int offset, size_t size, int more)
379{
380 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
381 int ret;
382
383 ret = kernel_sendpage(sock, page, offset, size, flags);
384 if (ret == -EAGAIN)
385 ret = 0;
386
387 return ret;
388}
389
Sage Weil31b80062009-10-06 11:31:13 -0700390
391/*
392 * Shutdown/close the socket for the given connection.
393 */
394static int con_close_socket(struct ceph_connection *con)
395{
396 int rc;
397
398 dout("con_close_socket on %p sock %p\n", con, con->sock);
399 if (!con->sock)
400 return 0;
401 set_bit(SOCK_CLOSED, &con->state);
402 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
403 sock_release(con->sock);
404 con->sock = NULL;
405 clear_bit(SOCK_CLOSED, &con->state);
Alex Elderce2c8902012-05-22 22:15:49 -0500406 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700407 return rc;
408}
409
410/*
411 * Reset a connection. Discard all incoming and outgoing messages
412 * and clear *_seq state.
413 */
414static void ceph_msg_remove(struct ceph_msg *msg)
415{
416 list_del_init(&msg->list_head);
417 ceph_msg_put(msg);
418}
419static void ceph_msg_remove_list(struct list_head *head)
420{
421 while (!list_empty(head)) {
422 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
423 list_head);
424 ceph_msg_remove(msg);
425 }
426}
427
428static void reset_connection(struct ceph_connection *con)
429{
430 /* reset connection, out_queue, msg_ and connect_seq */
431 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700432 ceph_msg_remove_list(&con->out_queue);
433 ceph_msg_remove_list(&con->out_sent);
434
Sage Weilcf3e5c42009-12-11 09:48:05 -0800435 if (con->in_msg) {
436 ceph_msg_put(con->in_msg);
437 con->in_msg = NULL;
438 }
439
Sage Weil31b80062009-10-06 11:31:13 -0700440 con->connect_seq = 0;
441 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800442 if (con->out_msg) {
443 ceph_msg_put(con->out_msg);
444 con->out_msg = NULL;
445 }
Sage Weil31b80062009-10-06 11:31:13 -0700446 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700447 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700448}
449
450/*
451 * mark a peer down. drop any open connections.
452 */
453void ceph_con_close(struct ceph_connection *con)
454{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700455 dout("con_close %p peer %s\n", con,
456 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Eldera5988c42012-05-29 11:04:58 -0500457 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700458 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500459 set_bit(CLOSED, &con->state);
460
Alex Elder928443c2012-05-22 11:41:43 -0500461 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
462 clear_bit(KEEPALIVE_PENDING, &con->flags);
463 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500464
Sage Weilec302642009-12-22 10:43:42 -0800465 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700466 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700467 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800468 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800469 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700470 queue_con(con);
471}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700472EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700473
474/*
Sage Weil31b80062009-10-06 11:31:13 -0700475 * Reopen a closed connection, with a new peer address.
476 */
477void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
478{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700479 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700480 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500481 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
482
Sage Weil31b80062009-10-06 11:31:13 -0700483 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800484 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700485 queue_con(con);
486}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700487EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700488
489/*
Sage Weil87b315a2010-03-22 14:51:18 -0700490 * return true if this connection ever successfully opened
491 */
492bool ceph_con_opened(struct ceph_connection *con)
493{
494 return con->connect_seq > 0;
495}
496
497/*
Sage Weil31b80062009-10-06 11:31:13 -0700498 * generic get/put
499 */
500struct ceph_connection *ceph_con_get(struct ceph_connection *con)
501{
Alex Elderd3002b92012-02-14 14:05:33 -0600502 int nref = __atomic_add_unless(&con->nref, 1, 0);
503
504 dout("con_get %p nref = %d -> %d\n", con, nref, nref + 1);
505
506 return nref ? con : NULL;
Sage Weil31b80062009-10-06 11:31:13 -0700507}
508
509void ceph_con_put(struct ceph_connection *con)
510{
Alex Elderd3002b92012-02-14 14:05:33 -0600511 int nref = atomic_dec_return(&con->nref);
512
513 BUG_ON(nref < 0);
514 if (nref == 0) {
Sage Weil71ececd2009-11-18 11:27:06 -0800515 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700516 kfree(con);
517 }
Alex Elderd3002b92012-02-14 14:05:33 -0600518 dout("con_put %p nref = %d -> %d\n", con, nref + 1, nref);
Sage Weil31b80062009-10-06 11:31:13 -0700519}
520
521/*
522 * initialize a new connection.
523 */
524void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
525{
526 dout("con_init %p\n", con);
527 memset(con, 0, sizeof(*con));
528 atomic_set(&con->nref, 1);
529 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500530
531 con_sock_state_init(con);
532
Sage Weilec302642009-12-22 10:43:42 -0800533 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700534 INIT_LIST_HEAD(&con->out_queue);
535 INIT_LIST_HEAD(&con->out_sent);
536 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500537
538 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700539}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700540EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700541
542
543/*
544 * We maintain a global counter to order connection attempts. Get
545 * a unique seq greater than @gt.
546 */
547static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
548{
549 u32 ret;
550
551 spin_lock(&msgr->global_seq_lock);
552 if (msgr->global_seq < gt)
553 msgr->global_seq = gt;
554 ret = ++msgr->global_seq;
555 spin_unlock(&msgr->global_seq_lock);
556 return ret;
557}
558
Alex Eldere2200422012-05-23 14:35:23 -0500559static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600560{
561 con->out_kvec_left = 0;
562 con->out_kvec_bytes = 0;
563 con->out_kvec_cur = &con->out_kvec[0];
564}
565
Alex Eldere2200422012-05-23 14:35:23 -0500566static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600567 size_t size, void *data)
568{
569 int index;
570
571 index = con->out_kvec_left;
572 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
573
574 con->out_kvec[index].iov_len = size;
575 con->out_kvec[index].iov_base = data;
576 con->out_kvec_left++;
577 con->out_kvec_bytes += size;
578}
Sage Weil31b80062009-10-06 11:31:13 -0700579
580/*
581 * Prepare footer for currently outgoing message, and finish things
582 * off. Assumes out_kvec* are already valid.. we just add on to the end.
583 */
Alex Elder859eb792012-02-14 14:05:33 -0600584static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700585{
586 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600587 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700588
589 dout("prepare_write_message_footer %p\n", con);
590 con->out_kvec_is_msg = true;
591 con->out_kvec[v].iov_base = &m->footer;
592 con->out_kvec[v].iov_len = sizeof(m->footer);
593 con->out_kvec_bytes += sizeof(m->footer);
594 con->out_kvec_left++;
595 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800596 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700597}
598
599/*
600 * Prepare headers for the next outgoing message.
601 */
602static void prepare_write_message(struct ceph_connection *con)
603{
604 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600605 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700606
Alex Eldere2200422012-05-23 14:35:23 -0500607 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700608 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800609 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700610
611 /* Sneak an ack in there first? If we can get it into the same
612 * TCP packet that's a good thing. */
613 if (con->in_seq > con->in_seq_acked) {
614 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500615 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700616 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500617 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600618 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700619 }
620
Alex Elder859eb792012-02-14 14:05:33 -0600621 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800622 con->out_msg = m;
Sage Weil4cf9d542011-07-26 11:27:24 -0700623
624 /* put message on sent list */
625 ceph_msg_get(m);
626 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700627
Sage Weile84346b2010-05-11 21:20:38 -0700628 /*
629 * only assign outgoing seq # if we haven't sent this message
630 * yet. if it is requeued, resend with it's original seq.
631 */
632 if (m->needs_out_seq) {
633 m->hdr.seq = cpu_to_le64(++con->out_seq);
634 m->needs_out_seq = false;
635 }
Sage Weil31b80062009-10-06 11:31:13 -0700636
637 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
638 m, con->out_seq, le16_to_cpu(m->hdr.type),
639 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
640 le32_to_cpu(m->hdr.data_len),
641 m->nr_pages);
642 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
643
644 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500645 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
646 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
647 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600648
Sage Weil31b80062009-10-06 11:31:13 -0700649 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500650 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600651 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700652
653 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600654 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
655 con->out_msg->hdr.crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -0700656 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
Alex Eldera9a0c512012-02-15 07:43:54 -0600657
658 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
659 con->out_msg->footer.front_crc = cpu_to_le32(crc);
660 if (m->middle) {
661 crc = crc32c(0, m->middle->vec.iov_base,
662 m->middle->vec.iov_len);
663 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
664 } else
Sage Weil31b80062009-10-06 11:31:13 -0700665 con->out_msg->footer.middle_crc = 0;
666 con->out_msg->footer.data_crc = 0;
667 dout("prepare_write_message front_crc %u data_crc %u\n",
668 le32_to_cpu(con->out_msg->footer.front_crc),
669 le32_to_cpu(con->out_msg->footer.middle_crc));
670
671 /* is there a data payload? */
672 if (le32_to_cpu(m->hdr.data_len) > 0) {
673 /* initialize page iterator */
674 con->out_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700675 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -0800676 con->out_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700677 else
678 con->out_msg_pos.page_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700679 con->out_msg_pos.data_pos = 0;
Alex Elderbca064d2012-02-15 07:43:54 -0600680 con->out_msg_pos.did_page_crc = false;
Sage Weil31b80062009-10-06 11:31:13 -0700681 con->out_more = 1; /* data + footer will follow */
682 } else {
683 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600684 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700685 }
686
Alex Elder928443c2012-05-22 11:41:43 -0500687 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700688}
689
690/*
691 * Prepare an ack.
692 */
693static void prepare_write_ack(struct ceph_connection *con)
694{
695 dout("prepare_write_ack %p %llu -> %llu\n", con,
696 con->in_seq_acked, con->in_seq);
697 con->in_seq_acked = con->in_seq;
698
Alex Eldere2200422012-05-23 14:35:23 -0500699 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600700
Alex Eldere2200422012-05-23 14:35:23 -0500701 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600702
Sage Weil31b80062009-10-06 11:31:13 -0700703 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500704 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600705 &con->out_temp_ack);
706
Sage Weil31b80062009-10-06 11:31:13 -0700707 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500708 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700709}
710
711/*
712 * Prepare to write keepalive byte.
713 */
714static void prepare_write_keepalive(struct ceph_connection *con)
715{
716 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500717 con_out_kvec_reset(con);
718 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500719 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700720}
721
722/*
723 * Connection negotiation.
724 */
725
Alex Elderdac1e712012-05-16 15:16:39 -0500726static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
727 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800728{
Alex Eldera3530df2012-05-16 15:16:39 -0500729 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500730
731 if (!con->ops->get_authorizer) {
732 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
733 con->out_connect.authorizer_len = 0;
734
Alex Elder729796b2012-05-16 15:16:39 -0500735 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500736 }
737
738 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800739
Sage Weilec302642009-12-22 10:43:42 -0800740 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500741
Alex Elderdac1e712012-05-16 15:16:39 -0500742 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500743
Sage Weilec302642009-12-22 10:43:42 -0800744 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800745
Alex Eldera3530df2012-05-16 15:16:39 -0500746 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500747 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500748 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500749 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700750
Alex Elder8f43fb52012-05-16 15:16:39 -0500751 con->auth_reply_buf = auth->authorizer_reply_buf;
752 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
753
Alex Elder859eb792012-02-14 14:05:33 -0600754
Alex Elder729796b2012-05-16 15:16:39 -0500755 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800756}
757
Sage Weil31b80062009-10-06 11:31:13 -0700758/*
759 * We connected to a peer and are saying hello.
760 */
Alex Eldere825a662012-05-16 15:16:38 -0500761static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700762{
Alex Eldere2200422012-05-23 14:35:23 -0500763 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
764 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500765 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800766
Sage Weileed0ef22009-11-10 14:34:36 -0800767 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500768 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800769}
770
Alex Eldere825a662012-05-16 15:16:38 -0500771static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800772{
Sage Weil31b80062009-10-06 11:31:13 -0700773 unsigned global_seq = get_global_seq(con->msgr, 0);
774 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500775 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500776 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700777
778 switch (con->peer_name.type) {
779 case CEPH_ENTITY_TYPE_MON:
780 proto = CEPH_MONC_PROTOCOL;
781 break;
782 case CEPH_ENTITY_TYPE_OSD:
783 proto = CEPH_OSDC_PROTOCOL;
784 break;
785 case CEPH_ENTITY_TYPE_MDS:
786 proto = CEPH_MDSC_PROTOCOL;
787 break;
788 default:
789 BUG();
790 }
791
792 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
793 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800794
Alex Eldere825a662012-05-16 15:16:38 -0500795 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700796 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
797 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
798 con->out_connect.global_seq = cpu_to_le32(global_seq);
799 con->out_connect.protocol_version = cpu_to_le32(proto);
800 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700801
Alex Elderdac1e712012-05-16 15:16:39 -0500802 auth_proto = CEPH_AUTH_UNKNOWN;
803 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500804 if (IS_ERR(auth))
805 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500806
Alex Elderdac1e712012-05-16 15:16:39 -0500807 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500808 con->out_connect.authorizer_len = auth ?
809 cpu_to_le32(auth->authorizer_buf_len) : 0;
810
Alex Eldere2200422012-05-23 14:35:23 -0500811 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500812 &con->out_connect);
813 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500814 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500815 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600816
Sage Weil31b80062009-10-06 11:31:13 -0700817 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500818 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800819
Alex Eldere10c7582012-05-16 15:16:38 -0500820 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700821}
822
Sage Weil31b80062009-10-06 11:31:13 -0700823/*
824 * write as much of pending kvecs to the socket as we can.
825 * 1 -> done
826 * 0 -> socket full, but more to do
827 * <0 -> error
828 */
829static int write_partial_kvec(struct ceph_connection *con)
830{
831 int ret;
832
833 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
834 while (con->out_kvec_bytes > 0) {
835 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
836 con->out_kvec_left, con->out_kvec_bytes,
837 con->out_more);
838 if (ret <= 0)
839 goto out;
840 con->out_kvec_bytes -= ret;
841 if (con->out_kvec_bytes == 0)
842 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600843
844 /* account for full iov entries consumed */
845 while (ret >= con->out_kvec_cur->iov_len) {
846 BUG_ON(!con->out_kvec_left);
847 ret -= con->out_kvec_cur->iov_len;
848 con->out_kvec_cur++;
849 con->out_kvec_left--;
850 }
851 /* and for a partially-consumed entry */
852 if (ret) {
853 con->out_kvec_cur->iov_len -= ret;
854 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700855 }
856 }
857 con->out_kvec_left = 0;
858 con->out_kvec_is_msg = false;
859 ret = 1;
860out:
861 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
862 con->out_kvec_bytes, con->out_kvec_left, ret);
863 return ret; /* done! */
864}
865
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700866#ifdef CONFIG_BLOCK
867static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
868{
869 if (!bio) {
870 *iter = NULL;
871 *seg = 0;
872 return;
873 }
874 *iter = bio;
875 *seg = bio->bi_idx;
876}
877
878static void iter_bio_next(struct bio **bio_iter, int *seg)
879{
880 if (*bio_iter == NULL)
881 return;
882
883 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
884
885 (*seg)++;
886 if (*seg == (*bio_iter)->bi_vcnt)
887 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
888}
889#endif
890
Sage Weil31b80062009-10-06 11:31:13 -0700891/*
892 * Write as much message data payload as we can. If we finish, queue
893 * up the footer.
894 * 1 -> done, footer is now queued in out_kvec[].
895 * 0 -> socket full, but more to do
896 * <0 -> error
897 */
898static int write_partial_msg_pages(struct ceph_connection *con)
899{
900 struct ceph_msg *msg = con->out_msg;
901 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
902 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600903 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700904 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700905 int total_max_write;
906 int in_trail = 0;
907 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700908
909 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
910 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
911 con->out_msg_pos.page_pos);
912
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700913#ifdef CONFIG_BLOCK
914 if (msg->bio && !msg->bio_iter)
915 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
916#endif
917
918 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700919 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700920 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600921 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700922
923 total_max_write = data_len - trail_len -
924 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700925
926 /*
927 * if we are calculating the data crc (the default), we need
928 * to map the page. if our pages[] has been revoked, use the
929 * zero page.
930 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700931
932 /* have we reached the trail part of the data? */
933 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
934 in_trail = 1;
935
936 total_max_write = data_len - con->out_msg_pos.data_pos;
937
938 page = list_first_entry(&msg->trail->head,
939 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700940 max_write = PAGE_SIZE;
941 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700942 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800943 } else if (msg->pagelist) {
944 page = list_first_entry(&msg->pagelist->head,
945 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700946#ifdef CONFIG_BLOCK
947 } else if (msg->bio) {
948 struct bio_vec *bv;
949
950 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
951 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600952 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700953 max_write = bv->bv_len;
954#endif
Sage Weil31b80062009-10-06 11:31:13 -0700955 } else {
Alex Elder57666512012-01-23 15:49:27 -0600956 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700957 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700958 len = min_t(int, max_write - con->out_msg_pos.page_pos,
959 total_max_write);
960
Alex Elder37675b02012-03-07 11:40:08 -0600961 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600962 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600963 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700964 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600965 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700966
Alex Elder8d63e312012-03-07 11:40:08 -0600967 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700968 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600969 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600970 crc = crc32c(tmpcrc, base, len);
971 con->out_msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600972 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -0700973 }
Alex Eldere36b13c2012-03-07 11:40:08 -0600974 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -0600975 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -0600976 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700977
Alex Elder0cdf9e62012-03-07 11:40:08 -0600978 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -0700979 kunmap(page);
980
981 if (ret <= 0)
982 goto out;
983
984 con->out_msg_pos.data_pos += ret;
985 con->out_msg_pos.page_pos += ret;
986 if (ret == len) {
987 con->out_msg_pos.page_pos = 0;
988 con->out_msg_pos.page++;
Alex Elderbca064d2012-02-15 07:43:54 -0600989 con->out_msg_pos.did_page_crc = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700990 if (in_trail)
991 list_move_tail(&page->lru,
992 &msg->trail->head);
993 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -0800994 list_move_tail(&page->lru,
995 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700996#ifdef CONFIG_BLOCK
997 else if (msg->bio)
998 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
999#endif
Sage Weil31b80062009-10-06 11:31:13 -07001000 }
1001 }
1002
1003 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1004
1005 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001006 if (!do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001007 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001008 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001009 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001010 ret = 1;
1011out:
1012 return ret;
1013}
1014
1015/*
1016 * write some zeros
1017 */
1018static int write_partial_skip(struct ceph_connection *con)
1019{
1020 int ret;
1021
1022 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001023 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001024
Alex Elder31739132012-03-07 11:40:08 -06001025 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001026 if (ret <= 0)
1027 goto out;
1028 con->out_skip -= ret;
1029 }
1030 ret = 1;
1031out:
1032 return ret;
1033}
1034
1035/*
1036 * Prepare to read connection handshake, or an ack.
1037 */
Sage Weileed0ef22009-11-10 14:34:36 -08001038static void prepare_read_banner(struct ceph_connection *con)
1039{
1040 dout("prepare_read_banner %p\n", con);
1041 con->in_base_pos = 0;
1042}
1043
Sage Weil31b80062009-10-06 11:31:13 -07001044static void prepare_read_connect(struct ceph_connection *con)
1045{
1046 dout("prepare_read_connect %p\n", con);
1047 con->in_base_pos = 0;
1048}
1049
1050static void prepare_read_ack(struct ceph_connection *con)
1051{
1052 dout("prepare_read_ack %p\n", con);
1053 con->in_base_pos = 0;
1054}
1055
1056static void prepare_read_tag(struct ceph_connection *con)
1057{
1058 dout("prepare_read_tag %p\n", con);
1059 con->in_base_pos = 0;
1060 con->in_tag = CEPH_MSGR_TAG_READY;
1061}
1062
1063/*
1064 * Prepare to read a message.
1065 */
1066static int prepare_read_message(struct ceph_connection *con)
1067{
1068 dout("prepare_read_message %p\n", con);
1069 BUG_ON(con->in_msg != NULL);
1070 con->in_base_pos = 0;
1071 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1072 return 0;
1073}
1074
1075
1076static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001077 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001078{
Alex Eldere6cee712012-05-10 10:29:50 -05001079 while (con->in_base_pos < end) {
1080 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001081 int have = size - left;
1082 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1083 if (ret <= 0)
1084 return ret;
1085 con->in_base_pos += ret;
1086 }
1087 return 1;
1088}
1089
1090
1091/*
1092 * Read all or part of the connect-side handshake on a new connection
1093 */
Sage Weileed0ef22009-11-10 14:34:36 -08001094static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001095{
Alex Elderfd516532012-05-10 10:29:50 -05001096 int size;
1097 int end;
1098 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001099
Sage Weileed0ef22009-11-10 14:34:36 -08001100 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001101
1102 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001103 size = strlen(CEPH_BANNER);
1104 end = size;
1105 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001106 if (ret <= 0)
1107 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001108
1109 size = sizeof (con->actual_peer_addr);
1110 end += size;
1111 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001112 if (ret <= 0)
1113 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001114
1115 size = sizeof (con->peer_addr_for_me);
1116 end += size;
1117 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001118 if (ret <= 0)
1119 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001120
Sage Weileed0ef22009-11-10 14:34:36 -08001121out:
1122 return ret;
1123}
1124
1125static int read_partial_connect(struct ceph_connection *con)
1126{
Alex Elderfd516532012-05-10 10:29:50 -05001127 int size;
1128 int end;
1129 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001130
1131 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1132
Alex Elderfd516532012-05-10 10:29:50 -05001133 size = sizeof (con->in_reply);
1134 end = size;
1135 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001136 if (ret <= 0)
1137 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001138
1139 size = le32_to_cpu(con->in_reply.authorizer_len);
1140 end += size;
1141 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001142 if (ret <= 0)
1143 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001144
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001145 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1146 con, (int)con->in_reply.tag,
1147 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001148 le32_to_cpu(con->in_reply.global_seq));
1149out:
1150 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001151
Sage Weil31b80062009-10-06 11:31:13 -07001152}
1153
1154/*
1155 * Verify the hello banner looks okay.
1156 */
1157static int verify_hello(struct ceph_connection *con)
1158{
1159 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001160 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001161 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001162 con->error_msg = "protocol error, bad banner";
1163 return -1;
1164 }
1165 return 0;
1166}
1167
1168static bool addr_is_blank(struct sockaddr_storage *ss)
1169{
1170 switch (ss->ss_family) {
1171 case AF_INET:
1172 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1173 case AF_INET6:
1174 return
1175 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1176 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1177 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1178 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1179 }
1180 return false;
1181}
1182
1183static int addr_port(struct sockaddr_storage *ss)
1184{
1185 switch (ss->ss_family) {
1186 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001187 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001188 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001189 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001190 }
1191 return 0;
1192}
1193
1194static void addr_set_port(struct sockaddr_storage *ss, int p)
1195{
1196 switch (ss->ss_family) {
1197 case AF_INET:
1198 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001199 break;
Sage Weil31b80062009-10-06 11:31:13 -07001200 case AF_INET6:
1201 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001202 break;
Sage Weil31b80062009-10-06 11:31:13 -07001203 }
1204}
1205
1206/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001207 * Unlike other *_pton function semantics, zero indicates success.
1208 */
1209static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1210 char delim, const char **ipend)
1211{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001212 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1213 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001214
1215 memset(ss, 0, sizeof(*ss));
1216
1217 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1218 ss->ss_family = AF_INET;
1219 return 0;
1220 }
1221
1222 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1223 ss->ss_family = AF_INET6;
1224 return 0;
1225 }
1226
1227 return -EINVAL;
1228}
1229
1230/*
1231 * Extract hostname string and resolve using kernel DNS facility.
1232 */
1233#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1234static int ceph_dns_resolve_name(const char *name, size_t namelen,
1235 struct sockaddr_storage *ss, char delim, const char **ipend)
1236{
1237 const char *end, *delim_p;
1238 char *colon_p, *ip_addr = NULL;
1239 int ip_len, ret;
1240
1241 /*
1242 * The end of the hostname occurs immediately preceding the delimiter or
1243 * the port marker (':') where the delimiter takes precedence.
1244 */
1245 delim_p = memchr(name, delim, namelen);
1246 colon_p = memchr(name, ':', namelen);
1247
1248 if (delim_p && colon_p)
1249 end = delim_p < colon_p ? delim_p : colon_p;
1250 else if (!delim_p && colon_p)
1251 end = colon_p;
1252 else {
1253 end = delim_p;
1254 if (!end) /* case: hostname:/ */
1255 end = name + namelen;
1256 }
1257
1258 if (end <= name)
1259 return -EINVAL;
1260
1261 /* do dns_resolve upcall */
1262 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1263 if (ip_len > 0)
1264 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1265 else
1266 ret = -ESRCH;
1267
1268 kfree(ip_addr);
1269
1270 *ipend = end;
1271
1272 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1273 ret, ret ? "failed" : ceph_pr_addr(ss));
1274
1275 return ret;
1276}
1277#else
1278static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1279 struct sockaddr_storage *ss, char delim, const char **ipend)
1280{
1281 return -EINVAL;
1282}
1283#endif
1284
1285/*
1286 * Parse a server name (IP or hostname). If a valid IP address is not found
1287 * then try to extract a hostname to resolve using userspace DNS upcall.
1288 */
1289static int ceph_parse_server_name(const char *name, size_t namelen,
1290 struct sockaddr_storage *ss, char delim, const char **ipend)
1291{
1292 int ret;
1293
1294 ret = ceph_pton(name, namelen, ss, delim, ipend);
1295 if (ret)
1296 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1297
1298 return ret;
1299}
1300
1301/*
Sage Weil31b80062009-10-06 11:31:13 -07001302 * Parse an ip[:port] list into an addr array. Use the default
1303 * monitor port if a port isn't specified.
1304 */
1305int ceph_parse_ips(const char *c, const char *end,
1306 struct ceph_entity_addr *addr,
1307 int max_count, int *count)
1308{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001309 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001310 const char *p = c;
1311
1312 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1313 for (i = 0; i < max_count; i++) {
1314 const char *ipend;
1315 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001316 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001317 char delim = ',';
1318
1319 if (*p == '[') {
1320 delim = ']';
1321 p++;
1322 }
Sage Weil31b80062009-10-06 11:31:13 -07001323
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001324 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1325 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001326 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001327 ret = -EINVAL;
1328
Sage Weil31b80062009-10-06 11:31:13 -07001329 p = ipend;
1330
Sage Weil39139f62010-07-08 09:54:52 -07001331 if (delim == ']') {
1332 if (*p != ']') {
1333 dout("missing matching ']'\n");
1334 goto bad;
1335 }
1336 p++;
1337 }
1338
Sage Weil31b80062009-10-06 11:31:13 -07001339 /* port? */
1340 if (p < end && *p == ':') {
1341 port = 0;
1342 p++;
1343 while (p < end && *p >= '0' && *p <= '9') {
1344 port = (port * 10) + (*p - '0');
1345 p++;
1346 }
1347 if (port > 65535 || port == 0)
1348 goto bad;
1349 } else {
1350 port = CEPH_MON_PORT;
1351 }
1352
1353 addr_set_port(ss, port);
1354
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001355 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001356
1357 if (p == end)
1358 break;
1359 if (*p != ',')
1360 goto bad;
1361 p++;
1362 }
1363
1364 if (p != end)
1365 goto bad;
1366
1367 if (count)
1368 *count = i + 1;
1369 return 0;
1370
1371bad:
Sage Weil39139f62010-07-08 09:54:52 -07001372 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001373 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001374}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001375EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001376
Sage Weileed0ef22009-11-10 14:34:36 -08001377static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001378{
Sage Weileed0ef22009-11-10 14:34:36 -08001379 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001380
1381 if (verify_hello(con) < 0)
1382 return -1;
1383
Sage Weil63f2d212009-11-03 15:17:56 -08001384 ceph_decode_addr(&con->actual_peer_addr);
1385 ceph_decode_addr(&con->peer_addr_for_me);
1386
Sage Weil31b80062009-10-06 11:31:13 -07001387 /*
1388 * Make sure the other end is who we wanted. note that the other
1389 * end may not yet know their ip address, so if it's 0.0.0.0, give
1390 * them the benefit of the doubt.
1391 */
Sage Weil103e2d32010-01-07 16:12:36 -08001392 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1393 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001394 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1395 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001396 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001397 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001398 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001399 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001400 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001401 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001402 return -1;
1403 }
1404
1405 /*
1406 * did we learn our address?
1407 */
1408 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1409 int port = addr_port(&con->msgr->inst.addr.in_addr);
1410
1411 memcpy(&con->msgr->inst.addr.in_addr,
1412 &con->peer_addr_for_me.in_addr,
1413 sizeof(con->peer_addr_for_me.in_addr));
1414 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001415 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001416 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001417 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001418 }
1419
Sage Weileed0ef22009-11-10 14:34:36 -08001420 set_bit(NEGOTIATING, &con->state);
1421 prepare_read_connect(con);
1422 return 0;
1423}
1424
Sage Weil04a419f2009-12-23 09:30:21 -08001425static void fail_protocol(struct ceph_connection *con)
1426{
1427 reset_connection(con);
1428 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001429}
1430
Sage Weileed0ef22009-11-10 14:34:36 -08001431static int process_connect(struct ceph_connection *con)
1432{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001433 u64 sup_feat = con->msgr->supported_features;
1434 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001435 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001436 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001437
Sage Weileed0ef22009-11-10 14:34:36 -08001438 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1439
Sage Weil31b80062009-10-06 11:31:13 -07001440 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001441 case CEPH_MSGR_TAG_FEATURES:
1442 pr_err("%s%lld %s feature set mismatch,"
1443 " my %llx < server's %llx, missing %llx\n",
1444 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001445 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001446 sup_feat, server_feat, server_feat & ~sup_feat);
1447 con->error_msg = "missing required protocol features";
1448 fail_protocol(con);
1449 return -1;
1450
Sage Weil31b80062009-10-06 11:31:13 -07001451 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001452 pr_err("%s%lld %s protocol version mismatch,"
1453 " my %d != server's %d\n",
1454 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001455 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001456 le32_to_cpu(con->out_connect.protocol_version),
1457 le32_to_cpu(con->in_reply.protocol_version));
1458 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001459 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001460 return -1;
1461
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001462 case CEPH_MSGR_TAG_BADAUTHORIZER:
1463 con->auth_retry++;
1464 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1465 con->auth_retry);
1466 if (con->auth_retry == 2) {
1467 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001468 return -1;
1469 }
1470 con->auth_retry = 1;
Alex Eldere2200422012-05-23 14:35:23 -05001471 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001472 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001473 if (ret < 0)
1474 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001475 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001476 break;
Sage Weil31b80062009-10-06 11:31:13 -07001477
1478 case CEPH_MSGR_TAG_RESETSESSION:
1479 /*
1480 * If we connected with a large connect_seq but the peer
1481 * has no record of a session with us (no connection, or
1482 * connect_seq == 0), they will send RESETSESION to indicate
1483 * that they must have reset their session, and may have
1484 * dropped messages.
1485 */
1486 dout("process_connect got RESET peer seq %u\n",
1487 le32_to_cpu(con->in_connect.connect_seq));
1488 pr_err("%s%lld %s connection reset\n",
1489 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001490 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001491 reset_connection(con);
Alex Eldere2200422012-05-23 14:35:23 -05001492 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001493 ret = prepare_write_connect(con);
1494 if (ret < 0)
1495 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001496 prepare_read_connect(con);
1497
1498 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001499 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001500 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1501 if (con->ops->peer_reset)
1502 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001503 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001504 if (test_bit(CLOSED, &con->state) ||
1505 test_bit(OPENING, &con->state))
1506 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001507 break;
1508
1509 case CEPH_MSGR_TAG_RETRY_SESSION:
1510 /*
1511 * If we sent a smaller connect_seq than the peer has, try
1512 * again with a larger value.
1513 */
1514 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1515 le32_to_cpu(con->out_connect.connect_seq),
1516 le32_to_cpu(con->in_connect.connect_seq));
1517 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Eldere2200422012-05-23 14:35:23 -05001518 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001519 ret = prepare_write_connect(con);
1520 if (ret < 0)
1521 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001522 prepare_read_connect(con);
1523 break;
1524
1525 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1526 /*
1527 * If we sent a smaller global_seq than the peer has, try
1528 * again with a larger value.
1529 */
Sage Weileed0ef22009-11-10 14:34:36 -08001530 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001531 con->peer_global_seq,
1532 le32_to_cpu(con->in_connect.global_seq));
1533 get_global_seq(con->msgr,
1534 le32_to_cpu(con->in_connect.global_seq));
Alex Eldere2200422012-05-23 14:35:23 -05001535 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001536 ret = prepare_write_connect(con);
1537 if (ret < 0)
1538 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001539 prepare_read_connect(con);
1540 break;
1541
1542 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001543 if (req_feat & ~server_feat) {
1544 pr_err("%s%lld %s protocol feature mismatch,"
1545 " my required %llx > server's %llx, need %llx\n",
1546 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001547 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001548 req_feat, server_feat, req_feat & ~server_feat);
1549 con->error_msg = "missing required protocol features";
1550 fail_protocol(con);
1551 return -1;
1552 }
Sage Weil31b80062009-10-06 11:31:13 -07001553 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001554 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1555 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001556 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001557 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1558 con->peer_global_seq,
1559 le32_to_cpu(con->in_reply.connect_seq),
1560 con->connect_seq);
1561 WARN_ON(con->connect_seq !=
1562 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001563
1564 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001565 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001566
Sage Weil31b80062009-10-06 11:31:13 -07001567 prepare_read_tag(con);
1568 break;
1569
1570 case CEPH_MSGR_TAG_WAIT:
1571 /*
1572 * If there is a connection race (we are opening
1573 * connections to each other), one of us may just have
1574 * to WAIT. This shouldn't happen if we are the
1575 * client.
1576 */
Sage Weil04177882011-05-12 15:33:17 -07001577 pr_err("process_connect got WAIT as client\n");
1578 con->error_msg = "protocol error, got WAIT as client";
1579 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001580
1581 default:
1582 pr_err("connect protocol error, will retry\n");
1583 con->error_msg = "protocol error, garbage tag during connect";
1584 return -1;
1585 }
1586 return 0;
1587}
1588
1589
1590/*
1591 * read (part of) an ack
1592 */
1593static int read_partial_ack(struct ceph_connection *con)
1594{
Alex Elderfd516532012-05-10 10:29:50 -05001595 int size = sizeof (con->in_temp_ack);
1596 int end = size;
1597
1598 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001599}
1600
1601
1602/*
1603 * We can finally discard anything that's been acked.
1604 */
1605static void process_ack(struct ceph_connection *con)
1606{
1607 struct ceph_msg *m;
1608 u64 ack = le64_to_cpu(con->in_temp_ack);
1609 u64 seq;
1610
Sage Weil31b80062009-10-06 11:31:13 -07001611 while (!list_empty(&con->out_sent)) {
1612 m = list_first_entry(&con->out_sent, struct ceph_msg,
1613 list_head);
1614 seq = le64_to_cpu(m->hdr.seq);
1615 if (seq > ack)
1616 break;
1617 dout("got ack for seq %llu type %d at %p\n", seq,
1618 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001619 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001620 ceph_msg_remove(m);
1621 }
Sage Weil31b80062009-10-06 11:31:13 -07001622 prepare_read_tag(con);
1623}
1624
1625
1626
1627
Yehuda Sadeh24504182010-01-08 13:58:34 -08001628static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001629 struct kvec *section,
1630 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001631{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001632 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001633
Yehuda Sadeh24504182010-01-08 13:58:34 -08001634 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001635
Yehuda Sadeh24504182010-01-08 13:58:34 -08001636 while (section->iov_len < sec_len) {
1637 BUG_ON(section->iov_base == NULL);
1638 left = sec_len - section->iov_len;
1639 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1640 section->iov_len, left);
1641 if (ret <= 0)
1642 return ret;
1643 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001644 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001645 if (section->iov_len == sec_len)
1646 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001647
1648 return 1;
1649}
1650
1651static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1652 struct ceph_msg_header *hdr,
1653 int *skip);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001654
1655
1656static int read_partial_message_pages(struct ceph_connection *con,
1657 struct page **pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001658 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001659{
1660 void *p;
1661 int ret;
1662 int left;
1663
1664 left = min((int)(data_len - con->in_msg_pos.data_pos),
1665 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1666 /* (page) data */
1667 BUG_ON(pages == NULL);
1668 p = kmap(pages[con->in_msg_pos.page]);
1669 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1670 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001671 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001672 con->in_data_crc =
1673 crc32c(con->in_data_crc,
1674 p + con->in_msg_pos.page_pos, ret);
1675 kunmap(pages[con->in_msg_pos.page]);
1676 if (ret <= 0)
1677 return ret;
1678 con->in_msg_pos.data_pos += ret;
1679 con->in_msg_pos.page_pos += ret;
1680 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1681 con->in_msg_pos.page_pos = 0;
1682 con->in_msg_pos.page++;
1683 }
1684
1685 return ret;
1686}
1687
1688#ifdef CONFIG_BLOCK
1689static int read_partial_message_bio(struct ceph_connection *con,
1690 struct bio **bio_iter, int *bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001691 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001692{
1693 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1694 void *p;
1695 int ret, left;
1696
1697 if (IS_ERR(bv))
1698 return PTR_ERR(bv);
1699
1700 left = min((int)(data_len - con->in_msg_pos.data_pos),
1701 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1702
1703 p = kmap(bv->bv_page) + bv->bv_offset;
1704
1705 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1706 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001707 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001708 con->in_data_crc =
1709 crc32c(con->in_data_crc,
1710 p + con->in_msg_pos.page_pos, ret);
1711 kunmap(bv->bv_page);
1712 if (ret <= 0)
1713 return ret;
1714 con->in_msg_pos.data_pos += ret;
1715 con->in_msg_pos.page_pos += ret;
1716 if (con->in_msg_pos.page_pos == bv->bv_len) {
1717 con->in_msg_pos.page_pos = 0;
1718 iter_bio_next(bio_iter, bio_seg);
1719 }
1720
1721 return ret;
1722}
1723#endif
1724
Sage Weil31b80062009-10-06 11:31:13 -07001725/*
1726 * read (part of) a message.
1727 */
1728static int read_partial_message(struct ceph_connection *con)
1729{
1730 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001731 int size;
1732 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001733 int ret;
Sage Weilc5c6b192010-11-09 12:40:00 -08001734 unsigned front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001735 bool do_datacrc = !con->msgr->nocrc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001736 int skip;
Sage Weilae187562010-04-22 07:47:01 -07001737 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001738 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001739
1740 dout("read_partial_message con %p msg %p\n", con, m);
1741
1742 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001743 size = sizeof (con->in_hdr);
1744 end = size;
1745 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001746 if (ret <= 0)
1747 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001748
1749 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1750 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1751 pr_err("read_partial_message bad hdr "
1752 " crc %u != expected %u\n",
1753 crc, con->in_hdr.crc);
1754 return -EBADMSG;
1755 }
1756
Sage Weil31b80062009-10-06 11:31:13 -07001757 front_len = le32_to_cpu(con->in_hdr.front_len);
1758 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1759 return -EIO;
1760 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1761 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1762 return -EIO;
1763 data_len = le32_to_cpu(con->in_hdr.data_len);
1764 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1765 return -EIO;
1766
Sage Weilae187562010-04-22 07:47:01 -07001767 /* verify seq# */
1768 seq = le64_to_cpu(con->in_hdr.seq);
1769 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001770 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001771 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001772 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001773 seq, con->in_seq + 1);
1774 con->in_base_pos = -front_len - middle_len - data_len -
1775 sizeof(m->footer);
1776 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001777 return 0;
1778 } else if ((s64)seq - (s64)con->in_seq > 1) {
1779 pr_err("read_partial_message bad seq %lld expected %lld\n",
1780 seq, con->in_seq + 1);
1781 con->error_msg = "bad message sequence # for incoming message";
1782 return -EBADMSG;
1783 }
1784
Sage Weil31b80062009-10-06 11:31:13 -07001785 /* allocate message? */
1786 if (!con->in_msg) {
1787 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1788 con->in_hdr.front_len, con->in_hdr.data_len);
Sage Weilae32be32010-06-13 10:30:19 -07001789 skip = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001790 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1791 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07001792 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001793 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001794 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001795 con->in_base_pos = -front_len - middle_len - data_len -
1796 sizeof(m->footer);
1797 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001798 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001799 return 0;
1800 }
Sage Weila79832f2010-04-01 16:06:19 -07001801 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001802 con->error_msg =
1803 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001804 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001805 }
1806 m = con->in_msg;
1807 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001808 if (m->middle)
1809 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001810
1811 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001812 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001813 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001814 else
1815 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001816 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001817 }
1818
1819 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001820 ret = read_partial_message_section(con, &m->front, front_len,
1821 &con->in_front_crc);
1822 if (ret <= 0)
1823 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001824
1825 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001826 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001827 ret = read_partial_message_section(con, &m->middle->vec,
1828 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001829 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001830 if (ret <= 0)
1831 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001832 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001833#ifdef CONFIG_BLOCK
1834 if (m->bio && !m->bio_iter)
1835 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1836#endif
Sage Weil31b80062009-10-06 11:31:13 -07001837
1838 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001839 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001840 if (m->pages) {
1841 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001842 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001843 if (ret <= 0)
1844 return ret;
1845#ifdef CONFIG_BLOCK
1846 } else if (m->bio) {
1847
1848 ret = read_partial_message_bio(con,
1849 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001850 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001851 if (ret <= 0)
1852 return ret;
1853#endif
1854 } else {
1855 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001856 }
1857 }
1858
Sage Weil31b80062009-10-06 11:31:13 -07001859 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001860 size = sizeof (m->footer);
1861 end += size;
1862 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001863 if (ret <= 0)
1864 return ret;
1865
Sage Weil31b80062009-10-06 11:31:13 -07001866 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1867 m, front_len, m->footer.front_crc, middle_len,
1868 m->footer.middle_crc, data_len, m->footer.data_crc);
1869
1870 /* crc ok? */
1871 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1872 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1873 m, con->in_front_crc, m->footer.front_crc);
1874 return -EBADMSG;
1875 }
1876 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1877 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1878 m, con->in_middle_crc, m->footer.middle_crc);
1879 return -EBADMSG;
1880 }
Alex Elderbca064d2012-02-15 07:43:54 -06001881 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001882 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1883 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1884 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1885 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1886 return -EBADMSG;
1887 }
1888
1889 return 1; /* done! */
1890}
1891
1892/*
1893 * Process message. This happens in the worker thread. The callback should
1894 * be careful not to do anything that waits on other incoming messages or it
1895 * may deadlock.
1896 */
1897static void process_message(struct ceph_connection *con)
1898{
Sage Weil5e095e82009-12-14 14:30:34 -08001899 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001900
Sage Weil5e095e82009-12-14 14:30:34 -08001901 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001902 con->in_msg = NULL;
1903
1904 /* if first message, set peer_name */
1905 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001906 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001907
Sage Weil31b80062009-10-06 11:31:13 -07001908 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001909 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001910
1911 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1912 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001913 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001914 le16_to_cpu(msg->hdr.type),
1915 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1916 le32_to_cpu(msg->hdr.front_len),
1917 le32_to_cpu(msg->hdr.data_len),
1918 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1919 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001920
1921 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001922 prepare_read_tag(con);
1923}
1924
1925
1926/*
1927 * Write something to the socket. Called in a worker thread when the
1928 * socket appears to be writeable and we have something ready to send.
1929 */
1930static int try_write(struct ceph_connection *con)
1931{
Sage Weil31b80062009-10-06 11:31:13 -07001932 int ret = 1;
1933
1934 dout("try_write start %p state %lu nref %d\n", con, con->state,
1935 atomic_read(&con->nref));
1936
Sage Weil31b80062009-10-06 11:31:13 -07001937more:
1938 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1939
1940 /* open the socket first? */
1941 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001942 clear_bit(NEGOTIATING, &con->state);
1943 set_bit(CONNECTING, &con->state);
1944
Alex Eldere2200422012-05-23 14:35:23 -05001945 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001946 prepare_write_banner(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001947 ret = prepare_write_connect(con);
1948 if (ret < 0)
1949 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001950 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001951
Sage Weilcf3e5c42009-12-11 09:48:05 -08001952 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001953 con->in_tag = CEPH_MSGR_TAG_READY;
1954 dout("try_write initiating connect on %p new state %lu\n",
1955 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001956 ret = ceph_tcp_connect(con);
1957 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001958 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001959 goto out;
1960 }
1961 }
1962
1963more_kvec:
1964 /* kvec data queued? */
1965 if (con->out_skip) {
1966 ret = write_partial_skip(con);
1967 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001968 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001969 }
1970 if (con->out_kvec_left) {
1971 ret = write_partial_kvec(con);
1972 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001973 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001974 }
1975
1976 /* msg pages? */
1977 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001978 if (con->out_msg_done) {
1979 ceph_msg_put(con->out_msg);
1980 con->out_msg = NULL; /* we're done with this one */
1981 goto do_next;
1982 }
1983
Sage Weil31b80062009-10-06 11:31:13 -07001984 ret = write_partial_msg_pages(con);
1985 if (ret == 1)
1986 goto more_kvec; /* we need to send the footer, too! */
1987 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08001988 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001989 if (ret < 0) {
1990 dout("try_write write_partial_msg_pages err %d\n",
1991 ret);
Sage Weil42961d22011-01-25 08:19:34 -08001992 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001993 }
1994 }
1995
Sage Weilc86a2932009-12-14 14:04:30 -08001996do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001997 if (!test_bit(CONNECTING, &con->state)) {
1998 /* is anything else pending? */
1999 if (!list_empty(&con->out_queue)) {
2000 prepare_write_message(con);
2001 goto more;
2002 }
2003 if (con->in_seq > con->in_seq_acked) {
2004 prepare_write_ack(con);
2005 goto more;
2006 }
Alex Elder928443c2012-05-22 11:41:43 -05002007 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002008 prepare_write_keepalive(con);
2009 goto more;
2010 }
2011 }
2012
2013 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002014 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002015 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002016 ret = 0;
2017out:
Sage Weil42961d22011-01-25 08:19:34 -08002018 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002019 return ret;
2020}
2021
2022
2023
2024/*
2025 * Read what we can from the socket.
2026 */
2027static int try_read(struct ceph_connection *con)
2028{
Sage Weil31b80062009-10-06 11:31:13 -07002029 int ret = -1;
2030
2031 if (!con->sock)
2032 return 0;
2033
2034 if (test_bit(STANDBY, &con->state))
2035 return 0;
2036
2037 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002038
Sage Weil31b80062009-10-06 11:31:13 -07002039more:
2040 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2041 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002042
2043 /*
2044 * process_connect and process_message drop and re-take
2045 * con->mutex. make sure we handle a racing close or reopen.
2046 */
2047 if (test_bit(CLOSED, &con->state) ||
2048 test_bit(OPENING, &con->state)) {
2049 ret = -EAGAIN;
2050 goto out;
2051 }
2052
Sage Weil31b80062009-10-06 11:31:13 -07002053 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002054 if (!test_bit(NEGOTIATING, &con->state)) {
2055 dout("try_read connecting\n");
2056 ret = read_partial_banner(con);
2057 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002058 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002059 ret = process_banner(con);
2060 if (ret < 0)
2061 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002062 }
Sage Weil31b80062009-10-06 11:31:13 -07002063 ret = read_partial_connect(con);
2064 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002065 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002066 ret = process_connect(con);
2067 if (ret < 0)
2068 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002069 goto more;
2070 }
2071
2072 if (con->in_base_pos < 0) {
2073 /*
2074 * skipping + discarding content.
2075 *
2076 * FIXME: there must be a better way to do this!
2077 */
Alex Elder84495f42012-02-15 07:43:55 -06002078 static char buf[SKIP_BUF_SIZE];
2079 int skip = min((int) sizeof (buf), -con->in_base_pos);
2080
Sage Weil31b80062009-10-06 11:31:13 -07002081 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2082 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2083 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002084 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002085 con->in_base_pos += ret;
2086 if (con->in_base_pos)
2087 goto more;
2088 }
2089 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2090 /*
2091 * what's next?
2092 */
2093 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2094 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002095 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002096 dout("try_read got tag %d\n", (int)con->in_tag);
2097 switch (con->in_tag) {
2098 case CEPH_MSGR_TAG_MSG:
2099 prepare_read_message(con);
2100 break;
2101 case CEPH_MSGR_TAG_ACK:
2102 prepare_read_ack(con);
2103 break;
2104 case CEPH_MSGR_TAG_CLOSE:
2105 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002106 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002107 default:
2108 goto bad_tag;
2109 }
2110 }
2111 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2112 ret = read_partial_message(con);
2113 if (ret <= 0) {
2114 switch (ret) {
2115 case -EBADMSG:
2116 con->error_msg = "bad crc";
2117 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002118 break;
Sage Weil31b80062009-10-06 11:31:13 -07002119 case -EIO:
2120 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002121 break;
Sage Weil31b80062009-10-06 11:31:13 -07002122 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002123 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002124 }
2125 if (con->in_tag == CEPH_MSGR_TAG_READY)
2126 goto more;
2127 process_message(con);
2128 goto more;
2129 }
2130 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2131 ret = read_partial_ack(con);
2132 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002133 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002134 process_ack(con);
2135 goto more;
2136 }
2137
Sage Weil31b80062009-10-06 11:31:13 -07002138out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002139 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002140 return ret;
2141
2142bad_tag:
2143 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2144 con->error_msg = "protocol error, garbage tag";
2145 ret = -1;
2146 goto out;
2147}
2148
2149
2150/*
2151 * Atomically queue work on a connection. Bump @con reference to
2152 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002153 */
2154static void queue_con(struct ceph_connection *con)
2155{
Sage Weil31b80062009-10-06 11:31:13 -07002156 if (!con->ops->get(con)) {
2157 dout("queue_con %p ref count 0\n", con);
2158 return;
2159 }
2160
Tejun Heof363e452011-01-03 14:49:46 +01002161 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002162 dout("queue_con %p - already queued\n", con);
2163 con->ops->put(con);
2164 } else {
2165 dout("queue_con %p\n", con);
2166 }
2167}
2168
2169/*
2170 * Do some work on a connection. Drop a connection ref when we're done.
2171 */
2172static void con_work(struct work_struct *work)
2173{
2174 struct ceph_connection *con = container_of(work, struct ceph_connection,
2175 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002176 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002177
Sage Weil9dd46582010-04-28 13:51:50 -07002178 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002179restart:
Alex Elder928443c2012-05-22 11:41:43 -05002180 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002181 dout("con_work %p backing off\n", con);
2182 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2183 round_jiffies_relative(con->delay))) {
2184 dout("con_work %p backoff %lu\n", con, con->delay);
2185 mutex_unlock(&con->mutex);
2186 return;
2187 } else {
2188 con->ops->put(con);
2189 dout("con_work %p FAILED to back off %lu\n", con,
2190 con->delay);
2191 }
2192 }
Sage Weil9dd46582010-04-28 13:51:50 -07002193
Sage Weile00de342011-03-04 12:25:05 -08002194 if (test_bit(STANDBY, &con->state)) {
2195 dout("con_work %p STANDBY\n", con);
2196 goto done;
2197 }
Sage Weil31b80062009-10-06 11:31:13 -07002198 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2199 dout("con_work CLOSED\n");
2200 con_close_socket(con);
2201 goto done;
2202 }
2203 if (test_and_clear_bit(OPENING, &con->state)) {
2204 /* reopen w/ new peer */
2205 dout("con_work OPENING\n");
2206 con_close_socket(con);
2207 }
2208
Alex Elder928443c2012-05-22 11:41:43 -05002209 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
Sage Weil0da5d702011-05-19 11:21:05 -07002210 goto fault;
2211
2212 ret = try_read(con);
2213 if (ret == -EAGAIN)
2214 goto restart;
2215 if (ret < 0)
2216 goto fault;
2217
2218 ret = try_write(con);
2219 if (ret == -EAGAIN)
2220 goto restart;
2221 if (ret < 0)
2222 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002223
2224done:
Sage Weil9dd46582010-04-28 13:51:50 -07002225 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002226done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002227 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002228 return;
2229
2230fault:
2231 mutex_unlock(&con->mutex);
2232 ceph_fault(con); /* error/fault path */
2233 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002234}
2235
2236
2237/*
2238 * Generic error/fault handler. A retry mechanism is used with
2239 * exponential backoff
2240 */
2241static void ceph_fault(struct ceph_connection *con)
2242{
2243 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002244 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002245 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002246 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002247
Alex Elder928443c2012-05-22 11:41:43 -05002248 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002249 dout("fault on LOSSYTX channel\n");
2250 goto out;
2251 }
2252
Sage Weilec302642009-12-22 10:43:42 -08002253 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002254 if (test_bit(CLOSED, &con->state))
2255 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002256
Sage Weil31b80062009-10-06 11:31:13 -07002257 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002258
2259 if (con->in_msg) {
2260 ceph_msg_put(con->in_msg);
2261 con->in_msg = NULL;
2262 }
Sage Weil31b80062009-10-06 11:31:13 -07002263
Sage Weile80a52d2010-02-25 12:40:45 -08002264 /* Requeue anything that hasn't been acked */
2265 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002266
Sage Weile76661d2011-03-03 10:10:15 -08002267 /* If there are no messages queued or keepalive pending, place
2268 * the connection in a STANDBY state */
2269 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002270 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002271 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002272 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002273 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002274 } else {
2275 /* retry after a delay. */
2276 if (con->delay == 0)
2277 con->delay = BASE_DELAY_INTERVAL;
2278 else if (con->delay < MAX_DELAY_INTERVAL)
2279 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002280 con->ops->get(con);
2281 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002282 round_jiffies_relative(con->delay))) {
2283 dout("fault queued %p delay %lu\n", con, con->delay);
2284 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002285 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002286 dout("fault failed to queue %p delay %lu, backoff\n",
2287 con, con->delay);
2288 /*
2289 * In many cases we see a socket state change
2290 * while con_work is running and end up
2291 * queuing (non-delayed) work, such that we
2292 * can't backoff with a delay. Set a flag so
2293 * that when con_work restarts we schedule the
2294 * delay then.
2295 */
Alex Elder928443c2012-05-22 11:41:43 -05002296 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002297 }
Sage Weil31b80062009-10-06 11:31:13 -07002298 }
2299
Sage Weil91e45ce32010-02-15 12:05:09 -08002300out_unlock:
2301 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002302out:
Sage Weil161fd652010-02-25 12:38:57 -08002303 /*
2304 * in case we faulted due to authentication, invalidate our
2305 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002306 */
Sage Weil161fd652010-02-25 12:38:57 -08002307 if (con->auth_retry && con->ops->invalidate_authorizer) {
2308 dout("calling invalidate_authorizer()\n");
2309 con->ops->invalidate_authorizer(con);
2310 }
2311
Sage Weil31b80062009-10-06 11:31:13 -07002312 if (con->ops->fault)
2313 con->ops->fault(con);
2314}
2315
2316
2317
2318/*
Alex Elder15d98822012-05-26 23:26:43 -05002319 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002320 */
Alex Elder15d98822012-05-26 23:26:43 -05002321void ceph_messenger_init(struct ceph_messenger *msgr,
2322 struct ceph_entity_addr *myaddr,
2323 u32 supported_features,
2324 u32 required_features,
2325 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002326{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002327 msgr->supported_features = supported_features;
2328 msgr->required_features = required_features;
2329
Sage Weil31b80062009-10-06 11:31:13 -07002330 spin_lock_init(&msgr->global_seq_lock);
2331
Sage Weil31b80062009-10-06 11:31:13 -07002332 if (myaddr)
2333 msgr->inst.addr = *myaddr;
2334
2335 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002336 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002337 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002338 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002339 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002340
Alex Elder15d98822012-05-26 23:26:43 -05002341 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002342}
Alex Elder15d98822012-05-26 23:26:43 -05002343EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002344
Sage Weile00de342011-03-04 12:25:05 -08002345static void clear_standby(struct ceph_connection *con)
2346{
2347 /* come back from STANDBY? */
2348 if (test_and_clear_bit(STANDBY, &con->state)) {
2349 mutex_lock(&con->mutex);
2350 dout("clear_standby %p and ++connect_seq\n", con);
2351 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002352 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2353 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002354 mutex_unlock(&con->mutex);
2355 }
2356}
2357
Sage Weil31b80062009-10-06 11:31:13 -07002358/*
2359 * Queue up an outgoing message on the given connection.
2360 */
2361void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2362{
2363 if (test_bit(CLOSED, &con->state)) {
2364 dout("con_send %p closed, dropping %p\n", con, msg);
2365 ceph_msg_put(msg);
2366 return;
2367 }
2368
2369 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002370 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002371
Sage Weil3ca02ef2010-03-01 15:25:00 -08002372 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2373
Sage Weile84346b2010-05-11 21:20:38 -07002374 msg->needs_out_seq = true;
2375
Sage Weil31b80062009-10-06 11:31:13 -07002376 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002377 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002378 BUG_ON(!list_empty(&msg->list_head));
2379 list_add_tail(&msg->list_head, &con->out_queue);
2380 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2381 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2382 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2383 le32_to_cpu(msg->hdr.front_len),
2384 le32_to_cpu(msg->hdr.middle_len),
2385 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002386 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002387
2388 /* if there wasn't anything waiting to send before, queue
2389 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002390 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002391 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002392 queue_con(con);
2393}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002394EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002395
2396/*
2397 * Revoke a message that was previously queued for send
2398 */
2399void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2400{
Sage Weilec302642009-12-22 10:43:42 -08002401 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002402 if (!list_empty(&msg->list_head)) {
Sage Weiled98ada2010-07-05 12:15:14 -07002403 dout("con_revoke %p msg %p - was on queue\n", con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002404 list_del_init(&msg->list_head);
2405 ceph_msg_put(msg);
2406 msg->hdr.seq = 0;
Sage Weiled98ada2010-07-05 12:15:14 -07002407 }
2408 if (con->out_msg == msg) {
2409 dout("con_revoke %p msg %p - was sending\n", con, msg);
2410 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002411 if (con->out_kvec_is_msg) {
2412 con->out_skip = con->out_kvec_bytes;
2413 con->out_kvec_is_msg = false;
2414 }
Sage Weiled98ada2010-07-05 12:15:14 -07002415 ceph_msg_put(msg);
2416 msg->hdr.seq = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002417 }
Sage Weilec302642009-12-22 10:43:42 -08002418 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002419}
2420
2421/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002422 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002423 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002424void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002425{
2426 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002427 if (con->in_msg && con->in_msg == msg) {
2428 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2429 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002430 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2431
2432 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002433 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002434 con->in_base_pos = con->in_base_pos -
2435 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002436 front_len -
2437 middle_len -
2438 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002439 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002440 ceph_msg_put(con->in_msg);
2441 con->in_msg = NULL;
2442 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002443 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002444 } else {
2445 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002446 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002447 }
2448 mutex_unlock(&con->mutex);
2449}
2450
2451/*
Sage Weil31b80062009-10-06 11:31:13 -07002452 * Queue a keepalive byte to ensure the tcp connection is alive.
2453 */
2454void ceph_con_keepalive(struct ceph_connection *con)
2455{
Sage Weile00de342011-03-04 12:25:05 -08002456 dout("con_keepalive %p\n", con);
2457 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002458 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2459 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002460 queue_con(con);
2461}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002462EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002463
2464
2465/*
2466 * construct a new message with given type, size
2467 * the new msg has a ref count of 1.
2468 */
Sage Weilb61c2762011-08-09 15:03:46 -07002469struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2470 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002471{
2472 struct ceph_msg *m;
2473
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002474 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002475 if (m == NULL)
2476 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002477 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07002478 INIT_LIST_HEAD(&m->list_head);
2479
Sage Weil45c6ceb2010-05-11 15:01:51 -07002480 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002481 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002482 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2483 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002484 m->hdr.front_len = cpu_to_le32(front_len);
2485 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002486 m->hdr.data_len = 0;
2487 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002488 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002489 m->footer.front_crc = 0;
2490 m->footer.middle_crc = 0;
2491 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002492 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002493 m->front_max = front_len;
2494 m->front_is_vmalloc = false;
2495 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002496 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002497 m->pool = NULL;
2498
Henry C Changca208922011-05-03 02:29:56 +00002499 /* middle */
2500 m->middle = NULL;
2501
2502 /* data */
2503 m->nr_pages = 0;
2504 m->page_alignment = 0;
2505 m->pages = NULL;
2506 m->pagelist = NULL;
2507 m->bio = NULL;
2508 m->bio_iter = NULL;
2509 m->bio_seg = 0;
2510 m->trail = NULL;
2511
Sage Weil31b80062009-10-06 11:31:13 -07002512 /* front */
2513 if (front_len) {
2514 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002515 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002516 PAGE_KERNEL);
2517 m->front_is_vmalloc = true;
2518 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002519 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002520 }
2521 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002522 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002523 front_len);
2524 goto out2;
2525 }
2526 } else {
2527 m->front.iov_base = NULL;
2528 }
2529 m->front.iov_len = front_len;
2530
Sage Weilbb257662010-04-01 16:07:23 -07002531 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002532 return m;
2533
2534out2:
2535 ceph_msg_put(m);
2536out:
Sage Weilb61c2762011-08-09 15:03:46 -07002537 if (!can_fail) {
2538 pr_err("msg_new can't create type %d front %d\n", type,
2539 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002540 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002541 } else {
2542 dout("msg_new can't create type %d front %d\n", type,
2543 front_len);
2544 }
Sage Weila79832f2010-04-01 16:06:19 -07002545 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002546}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002547EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002548
2549/*
Sage Weil31b80062009-10-06 11:31:13 -07002550 * Allocate "middle" portion of a message, if it is needed and wasn't
2551 * allocated by alloc_msg. This allows us to read a small fixed-size
2552 * per-type header in the front and then gracefully fail (i.e.,
2553 * propagate the error to the caller based on info in the front) when
2554 * the middle is too large.
2555 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002556static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002557{
2558 int type = le16_to_cpu(msg->hdr.type);
2559 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2560
2561 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2562 ceph_msg_type_name(type), middle_len);
2563 BUG_ON(!middle_len);
2564 BUG_ON(msg->middle);
2565
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002566 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002567 if (!msg->middle)
2568 return -ENOMEM;
2569 return 0;
2570}
2571
Yehuda Sadeh24504182010-01-08 13:58:34 -08002572/*
2573 * Generic message allocator, for incoming messages.
2574 */
2575static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2576 struct ceph_msg_header *hdr,
2577 int *skip)
2578{
2579 int type = le16_to_cpu(hdr->type);
2580 int front_len = le32_to_cpu(hdr->front_len);
2581 int middle_len = le32_to_cpu(hdr->middle_len);
2582 struct ceph_msg *msg = NULL;
2583 int ret;
2584
2585 if (con->ops->alloc_msg) {
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002586 mutex_unlock(&con->mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002587 msg = con->ops->alloc_msg(con, hdr, skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002588 mutex_lock(&con->mutex);
Sage Weila79832f2010-04-01 16:06:19 -07002589 if (!msg || *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002590 return NULL;
2591 }
2592 if (!msg) {
2593 *skip = 0;
Sage Weilb61c2762011-08-09 15:03:46 -07002594 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002595 if (!msg) {
2596 pr_err("unable to allocate msg type %d len %d\n",
2597 type, front_len);
Sage Weila79832f2010-04-01 16:06:19 -07002598 return NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002599 }
Sage Weilc5c6b192010-11-09 12:40:00 -08002600 msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002601 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002602 memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002603
Sage Weilbb257662010-04-01 16:07:23 -07002604 if (middle_len && !msg->middle) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002605 ret = ceph_alloc_middle(con, msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002606 if (ret < 0) {
2607 ceph_msg_put(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002608 return NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002609 }
2610 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002611
Yehuda Sadeh24504182010-01-08 13:58:34 -08002612 return msg;
2613}
2614
Sage Weil31b80062009-10-06 11:31:13 -07002615
2616/*
2617 * Free a generically kmalloc'd message.
2618 */
2619void ceph_msg_kfree(struct ceph_msg *m)
2620{
2621 dout("msg_kfree %p\n", m);
2622 if (m->front_is_vmalloc)
2623 vfree(m->front.iov_base);
2624 else
2625 kfree(m->front.iov_base);
2626 kfree(m);
2627}
2628
2629/*
2630 * Drop a msg ref. Destroy as needed.
2631 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002632void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002633{
Sage Weilc2e552e2009-12-07 15:55:05 -08002634 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002635
Sage Weilc2e552e2009-12-07 15:55:05 -08002636 dout("ceph_msg_put last one on %p\n", m);
2637 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002638
Sage Weilc2e552e2009-12-07 15:55:05 -08002639 /* drop middle, data, if any */
2640 if (m->middle) {
2641 ceph_buffer_put(m->middle);
2642 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002643 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002644 m->nr_pages = 0;
2645 m->pages = NULL;
2646
Sage Weil58bb3b32009-12-23 12:12:31 -08002647 if (m->pagelist) {
2648 ceph_pagelist_release(m->pagelist);
2649 kfree(m->pagelist);
2650 m->pagelist = NULL;
2651 }
2652
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002653 m->trail = NULL;
2654
Sage Weilc2e552e2009-12-07 15:55:05 -08002655 if (m->pool)
2656 ceph_msgpool_put(m->pool, m);
2657 else
2658 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002659}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002660EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002661
2662void ceph_msg_dump(struct ceph_msg *msg)
2663{
2664 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2665 msg->front_max, msg->nr_pages);
2666 print_hex_dump(KERN_DEBUG, "header: ",
2667 DUMP_PREFIX_OFFSET, 16, 1,
2668 &msg->hdr, sizeof(msg->hdr), true);
2669 print_hex_dump(KERN_DEBUG, " front: ",
2670 DUMP_PREFIX_OFFSET, 16, 1,
2671 msg->front.iov_base, msg->front.iov_len, true);
2672 if (msg->middle)
2673 print_hex_dump(KERN_DEBUG, "middle: ",
2674 DUMP_PREFIX_OFFSET, 16, 1,
2675 msg->middle->vec.iov_base,
2676 msg->middle->vec.iov_len, true);
2677 print_hex_dump(KERN_DEBUG, "footer: ",
2678 DUMP_PREFIX_OFFSET, 16, 1,
2679 &msg->footer, sizeof(msg->footer), true);
2680}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002681EXPORT_SYMBOL(ceph_msg_dump);