blob: dcc50e4cd5cd704cb05d9191ff1eed22526d1835 [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 Elderbc18f4b2012-06-20 21:53:53 -050032/*
33 * We track the state of the socket on a given connection using
34 * values defined below. The transition to a new socket state is
35 * handled by a function which verifies we aren't coming from an
36 * unexpected state.
37 *
38 * --------
39 * | NEW* | transient initial state
40 * --------
41 * | con_sock_state_init()
42 * v
43 * ----------
44 * | CLOSED | initialized, but no socket (and no
45 * ---------- TCP connection)
46 * ^ \
47 * | \ con_sock_state_connecting()
48 * | ----------------------
49 * | \
50 * + con_sock_state_closed() \
51 * |\ \
52 * | \ \
53 * | ----------- \
54 * | | CLOSING | socket event; \
55 * | ----------- await close \
56 * | ^ |
57 * | | |
58 * | + con_sock_state_closing() |
59 * | / \ |
60 * | / --------------- |
61 * | / \ v
62 * | / --------------
63 * | / -----------------| CONNECTING | socket created, TCP
64 * | | / -------------- connect initiated
65 * | | | con_sock_state_connected()
66 * | | v
67 * -------------
68 * | CONNECTED | TCP connection established
69 * -------------
70 *
71 * State values for ceph_connection->sock_state; NEW is assumed to be 0.
72 */
Alex Elderce2c8902012-05-22 22:15:49 -050073
74#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
75#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
76#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
77#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
78#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
79
Sage Weil31b80062009-10-06 11:31:13 -070080/* static tag bytes (protocol control messages) */
81static char tag_msg = CEPH_MSGR_TAG_MSG;
82static char tag_ack = CEPH_MSGR_TAG_ACK;
83static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
84
Sage Weila6a53492010-04-13 14:07:07 -070085#ifdef CONFIG_LOCKDEP
86static struct lock_class_key socket_class;
87#endif
88
Alex Elder84495f42012-02-15 07:43:55 -060089/*
90 * When skipping (ignoring) a block of input we read it into a "skip
91 * buffer," which is this many bytes in size.
92 */
93#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -070094
95static void queue_con(struct ceph_connection *con);
96static void con_work(struct work_struct *);
97static void ceph_fault(struct ceph_connection *con);
98
Sage Weil31b80062009-10-06 11:31:13 -070099/*
Alex Elderf64a9312012-01-23 15:49:27 -0600100 * Nicely render a sockaddr as a string. An array of formatted
101 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -0700102 */
Alex Elderf64a9312012-01-23 15:49:27 -0600103#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
104#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
105#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
106#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
107
108static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
109static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -0700110
Alex Elder57666512012-01-23 15:49:27 -0600111static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -0600112
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700113const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -0700114{
115 int i;
116 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -0600117 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
118 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -0700119
Alex Elderf64a9312012-01-23 15:49:27 -0600120 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700121 s = addr_str[i];
122
123 switch (ss->ss_family) {
124 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -0600125 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
126 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700127 break;
128
129 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -0600130 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
131 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700132 break;
133
134 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600135 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
136 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700137 }
138
139 return s;
140}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700141EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700142
Sage Weil63f2d212009-11-03 15:17:56 -0800143static void encode_my_addr(struct ceph_messenger *msgr)
144{
145 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
146 ceph_encode_addr(&msgr->my_enc_addr);
147}
148
Sage Weil31b80062009-10-06 11:31:13 -0700149/*
150 * work queue for all reading and writing to/from the socket.
151 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600152static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700153
Alex Elder6173d1f2012-02-14 14:05:33 -0600154void _ceph_msgr_exit(void)
155{
Alex Elderd3002b92012-02-14 14:05:33 -0600156 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600157 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600158 ceph_msgr_wq = NULL;
159 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600160
Alex Elder6173d1f2012-02-14 14:05:33 -0600161 BUG_ON(zero_page == NULL);
162 kunmap(zero_page);
163 page_cache_release(zero_page);
164 zero_page = NULL;
165}
166
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700167int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700168{
Alex Elder57666512012-01-23 15:49:27 -0600169 BUG_ON(zero_page != NULL);
170 zero_page = ZERO_PAGE(0);
171 page_cache_get(zero_page);
172
Tejun Heof363e45fd12011-01-03 14:49:46 +0100173 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600174 if (ceph_msgr_wq)
175 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600176
Alex Elder6173d1f2012-02-14 14:05:33 -0600177 pr_err("msgr_init failed to create workqueue\n");
178 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600179
Alex Elder6173d1f2012-02-14 14:05:33 -0600180 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700181}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700182EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700183
184void ceph_msgr_exit(void)
185{
Alex Elder57666512012-01-23 15:49:27 -0600186 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600187
Alex Elder6173d1f2012-02-14 14:05:33 -0600188 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700189}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700190EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700191
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700192void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700193{
194 flush_workqueue(ceph_msgr_wq);
195}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700196EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700197
Alex Elderce2c8902012-05-22 22:15:49 -0500198/* Connection socket state transition functions */
199
200static void con_sock_state_init(struct ceph_connection *con)
201{
202 int old_state;
203
204 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
205 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
206 printk("%s: unexpected old state %d\n", __func__, old_state);
207}
208
209static void con_sock_state_connecting(struct ceph_connection *con)
210{
211 int old_state;
212
213 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
214 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
215 printk("%s: unexpected old state %d\n", __func__, old_state);
216}
217
218static void con_sock_state_connected(struct ceph_connection *con)
219{
220 int old_state;
221
222 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
223 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
224 printk("%s: unexpected old state %d\n", __func__, old_state);
225}
226
227static void con_sock_state_closing(struct ceph_connection *con)
228{
229 int old_state;
230
231 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
232 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
233 old_state != CON_SOCK_STATE_CONNECTED &&
234 old_state != CON_SOCK_STATE_CLOSING))
235 printk("%s: unexpected old state %d\n", __func__, old_state);
236}
237
238static void con_sock_state_closed(struct ceph_connection *con)
239{
240 int old_state;
241
242 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
243 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
244 old_state != CON_SOCK_STATE_CLOSING))
245 printk("%s: unexpected old state %d\n", __func__, old_state);
246}
Sage Weila922d382010-05-29 09:41:23 -0700247
Sage Weil31b80062009-10-06 11:31:13 -0700248/*
249 * socket callback functions
250 */
251
252/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500253static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700254{
Alex Elderbd406142012-01-23 15:49:27 -0600255 struct ceph_connection *con = sk->sk_user_data;
256
Sage Weil31b80062009-10-06 11:31:13 -0700257 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500258 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700259 con, con->state);
260 queue_con(con);
261 }
262}
263
264/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500265static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700266{
Alex Elderd3002b92012-02-14 14:05:33 -0600267 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700268
Jim Schutt182fac22012-02-29 08:30:58 -0700269 /* only queue to workqueue if there is data we want to write,
270 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500271 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700272 * doesn't get called again until try_write() fills the socket
273 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
274 * and net/core/stream.c:sk_stream_write_space().
275 */
Alex Elder928443c2012-05-22 11:41:43 -0500276 if (test_bit(WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700277 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500278 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700279 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
280 queue_con(con);
281 }
Sage Weil31b80062009-10-06 11:31:13 -0700282 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500283 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700284 }
Sage Weil31b80062009-10-06 11:31:13 -0700285}
286
287/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500288static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700289{
Alex Elderbd406142012-01-23 15:49:27 -0600290 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700291
Alex Elder327800b2012-05-22 11:41:43 -0500292 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700293 con, con->state, sk->sk_state);
294
295 if (test_bit(CLOSED, &con->state))
296 return;
297
298 switch (sk->sk_state) {
299 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500300 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700301 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500302 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500303 con_sock_state_closing(con);
Alex Elderd65c9e02012-06-20 21:53:53 -0500304 set_bit(SOCK_CLOSED, &con->flags);
305 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700306 break;
307 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500308 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500309 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700310 queue_con(con);
311 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600312 default: /* Everything else is uninteresting */
313 break;
Sage Weil31b80062009-10-06 11:31:13 -0700314 }
315}
316
317/*
318 * set up socket callbacks
319 */
320static void set_sock_callbacks(struct socket *sock,
321 struct ceph_connection *con)
322{
323 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600324 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500325 sk->sk_data_ready = ceph_sock_data_ready;
326 sk->sk_write_space = ceph_sock_write_space;
327 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700328}
329
330
331/*
332 * socket helpers
333 */
334
335/*
336 * initiate connection to a remote socket.
337 */
Alex Elder41617d02012-02-14 14:05:33 -0600338static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700339{
Sage Weilf91d3472010-07-01 15:18:31 -0700340 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700341 struct socket *sock;
342 int ret;
343
344 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700345 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
346 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700347 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600348 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700349 sock->sk->sk_allocation = GFP_NOFS;
350
Sage Weila6a53492010-04-13 14:07:07 -0700351#ifdef CONFIG_LOCKDEP
352 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
353#endif
354
Sage Weil31b80062009-10-06 11:31:13 -0700355 set_sock_callbacks(sock, con);
356
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700357 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700358
Sage Weil89a86be2012-06-09 14:19:21 -0700359 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700360 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
361 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700362 if (ret == -EINPROGRESS) {
363 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700364 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700365 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600366 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700367 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700368 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700369 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700370 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700371
Alex Elder41617d02012-02-14 14:05:33 -0600372 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600373 }
374 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600375 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700376}
377
378static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
379{
380 struct kvec iov = {buf, len};
381 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800382 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700383
Sage Weil98bdb0a2011-01-25 08:17:48 -0800384 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
385 if (r == -EAGAIN)
386 r = 0;
387 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700388}
389
390/*
391 * write something. @more is true if caller will be sending more data
392 * shortly.
393 */
394static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
395 size_t kvlen, size_t len, int more)
396{
397 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800398 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700399
400 if (more)
401 msg.msg_flags |= MSG_MORE;
402 else
403 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
404
Sage Weil42961d22011-01-25 08:19:34 -0800405 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
406 if (r == -EAGAIN)
407 r = 0;
408 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700409}
410
Alex Elder31739132012-03-07 11:40:08 -0600411static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
412 int offset, size_t size, int more)
413{
414 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
415 int ret;
416
417 ret = kernel_sendpage(sock, page, offset, size, flags);
418 if (ret == -EAGAIN)
419 ret = 0;
420
421 return ret;
422}
423
Sage Weil31b80062009-10-06 11:31:13 -0700424
425/*
426 * Shutdown/close the socket for the given connection.
427 */
428static int con_close_socket(struct ceph_connection *con)
429{
430 int rc;
431
432 dout("con_close_socket on %p sock %p\n", con, con->sock);
433 if (!con->sock)
434 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700435 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
436 sock_release(con->sock);
437 con->sock = NULL;
Alex Elder456ea462012-06-20 21:53:53 -0500438
439 /*
440 * Forcibly clear the SOCK_CLOSE flag. It gets set
441 * independent of the connection mutex, and we could have
442 * received a socket close event before we had the chance to
443 * shut the socket down.
444 */
Alex Eldera8d00e32012-06-20 21:53:53 -0500445 clear_bit(SOCK_CLOSED, &con->flags);
Alex Elderce2c8902012-05-22 22:15:49 -0500446 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700447 return rc;
448}
449
450/*
451 * Reset a connection. Discard all incoming and outgoing messages
452 * and clear *_seq state.
453 */
454static void ceph_msg_remove(struct ceph_msg *msg)
455{
456 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500457 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -0700458 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500459 msg->con = NULL;
460
Sage Weil31b80062009-10-06 11:31:13 -0700461 ceph_msg_put(msg);
462}
463static void ceph_msg_remove_list(struct list_head *head)
464{
465 while (!list_empty(head)) {
466 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
467 list_head);
468 ceph_msg_remove(msg);
469 }
470}
471
472static void reset_connection(struct ceph_connection *con)
473{
474 /* reset connection, out_queue, msg_ and connect_seq */
475 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700476 ceph_msg_remove_list(&con->out_queue);
477 ceph_msg_remove_list(&con->out_sent);
478
Sage Weilcf3e5c42009-12-11 09:48:05 -0800479 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500480 BUG_ON(con->in_msg->con != con);
481 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800482 ceph_msg_put(con->in_msg);
483 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -0700484 con->ops->put(con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800485 }
486
Sage Weil31b80062009-10-06 11:31:13 -0700487 con->connect_seq = 0;
488 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800489 if (con->out_msg) {
490 ceph_msg_put(con->out_msg);
491 con->out_msg = NULL;
492 }
Sage Weil31b80062009-10-06 11:31:13 -0700493 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700494 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700495}
496
497/*
498 * mark a peer down. drop any open connections.
499 */
500void ceph_con_close(struct ceph_connection *con)
501{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700502 dout("con_close %p peer %s\n", con,
503 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Eldera5988c42012-05-29 11:04:58 -0500504 clear_bit(NEGOTIATING, &con->state);
Alex Elderbb9e6bb2012-06-20 21:53:53 -0500505 clear_bit(CONNECTING, &con->state);
Alex Eldere27947c2012-05-23 14:35:23 -0500506 clear_bit(CONNECTED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700507 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500508 set_bit(CLOSED, &con->state);
509
Alex Elder928443c2012-05-22 11:41:43 -0500510 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
511 clear_bit(KEEPALIVE_PENDING, &con->flags);
512 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500513
Sage Weilec302642009-12-22 10:43:42 -0800514 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700515 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700516 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800517 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800518 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700519 queue_con(con);
520}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700521EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700522
523/*
Sage Weil31b80062009-10-06 11:31:13 -0700524 * Reopen a closed connection, with a new peer address.
525 */
526void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
527{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700528 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700529 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500530 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
531
Sage Weil31b80062009-10-06 11:31:13 -0700532 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800533 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700534 queue_con(con);
535}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700536EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700537
538/*
Sage Weil87b315a2010-03-22 14:51:18 -0700539 * return true if this connection ever successfully opened
540 */
541bool ceph_con_opened(struct ceph_connection *con)
542{
543 return con->connect_seq > 0;
544}
545
546/*
Sage Weil31b80062009-10-06 11:31:13 -0700547 * initialize a new connection.
548 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500549void ceph_con_init(struct ceph_connection *con, void *private,
550 const struct ceph_connection_operations *ops,
551 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700552{
553 dout("con_init %p\n", con);
554 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500555 con->private = private;
556 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700557 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500558
559 con_sock_state_init(con);
560
Alex Elder1bfd89f2012-05-26 23:26:43 -0500561 con->peer_name.type = (__u8) entity_type;
562 con->peer_name.num = cpu_to_le64(entity_num);
563
Sage Weilec302642009-12-22 10:43:42 -0800564 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700565 INIT_LIST_HEAD(&con->out_queue);
566 INIT_LIST_HEAD(&con->out_sent);
567 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500568
569 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700570}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700571EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700572
573
574/*
575 * We maintain a global counter to order connection attempts. Get
576 * a unique seq greater than @gt.
577 */
578static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
579{
580 u32 ret;
581
582 spin_lock(&msgr->global_seq_lock);
583 if (msgr->global_seq < gt)
584 msgr->global_seq = gt;
585 ret = ++msgr->global_seq;
586 spin_unlock(&msgr->global_seq_lock);
587 return ret;
588}
589
Alex Eldere2200422012-05-23 14:35:23 -0500590static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600591{
592 con->out_kvec_left = 0;
593 con->out_kvec_bytes = 0;
594 con->out_kvec_cur = &con->out_kvec[0];
595}
596
Alex Eldere2200422012-05-23 14:35:23 -0500597static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600598 size_t size, void *data)
599{
600 int index;
601
602 index = con->out_kvec_left;
603 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
604
605 con->out_kvec[index].iov_len = size;
606 con->out_kvec[index].iov_base = data;
607 con->out_kvec_left++;
608 con->out_kvec_bytes += size;
609}
Sage Weil31b80062009-10-06 11:31:13 -0700610
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500611#ifdef CONFIG_BLOCK
612static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
613{
614 if (!bio) {
615 *iter = NULL;
616 *seg = 0;
617 return;
618 }
619 *iter = bio;
620 *seg = bio->bi_idx;
621}
622
623static void iter_bio_next(struct bio **bio_iter, int *seg)
624{
625 if (*bio_iter == NULL)
626 return;
627
628 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
629
630 (*seg)++;
631 if (*seg == (*bio_iter)->bi_vcnt)
632 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
633}
634#endif
635
Alex Elder739c9052012-06-11 14:57:13 -0500636static void prepare_write_message_data(struct ceph_connection *con)
637{
638 struct ceph_msg *msg = con->out_msg;
639
640 BUG_ON(!msg);
641 BUG_ON(!msg->hdr.data_len);
642
643 /* initialize page iterator */
644 con->out_msg_pos.page = 0;
645 if (msg->pages)
646 con->out_msg_pos.page_pos = msg->page_alignment;
647 else
648 con->out_msg_pos.page_pos = 0;
Alex Elder572c5882012-06-11 14:57:13 -0500649#ifdef CONFIG_BLOCK
Alex Elderabdaa6a2012-06-11 14:57:13 -0500650 if (msg->bio)
Alex Elder572c5882012-06-11 14:57:13 -0500651 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
652#endif
Alex Elder739c9052012-06-11 14:57:13 -0500653 con->out_msg_pos.data_pos = 0;
654 con->out_msg_pos.did_page_crc = false;
655 con->out_more = 1; /* data + footer will follow */
656}
657
Sage Weil31b80062009-10-06 11:31:13 -0700658/*
659 * Prepare footer for currently outgoing message, and finish things
660 * off. Assumes out_kvec* are already valid.. we just add on to the end.
661 */
Alex Elder859eb792012-02-14 14:05:33 -0600662static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700663{
664 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600665 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700666
Alex Elderfd154f32012-06-11 14:57:13 -0500667 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
668
Sage Weil31b80062009-10-06 11:31:13 -0700669 dout("prepare_write_message_footer %p\n", con);
670 con->out_kvec_is_msg = true;
671 con->out_kvec[v].iov_base = &m->footer;
672 con->out_kvec[v].iov_len = sizeof(m->footer);
673 con->out_kvec_bytes += sizeof(m->footer);
674 con->out_kvec_left++;
675 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800676 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700677}
678
679/*
680 * Prepare headers for the next outgoing message.
681 */
682static void prepare_write_message(struct ceph_connection *con)
683{
684 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600685 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700686
Alex Eldere2200422012-05-23 14:35:23 -0500687 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700688 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800689 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700690
691 /* Sneak an ack in there first? If we can get it into the same
692 * TCP packet that's a good thing. */
693 if (con->in_seq > con->in_seq_acked) {
694 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500695 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700696 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500697 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600698 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700699 }
700
Alex Elder38941f82012-06-01 14:56:43 -0500701 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600702 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800703 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500704 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700705
706 /* put message on sent list */
707 ceph_msg_get(m);
708 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700709
Sage Weile84346b2010-05-11 21:20:38 -0700710 /*
711 * only assign outgoing seq # if we haven't sent this message
712 * yet. if it is requeued, resend with it's original seq.
713 */
714 if (m->needs_out_seq) {
715 m->hdr.seq = cpu_to_le64(++con->out_seq);
716 m->needs_out_seq = false;
717 }
Sage Weil31b80062009-10-06 11:31:13 -0700718
719 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
720 m, con->out_seq, le16_to_cpu(m->hdr.type),
721 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
722 le32_to_cpu(m->hdr.data_len),
723 m->nr_pages);
724 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
725
726 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500727 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
728 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
729 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600730
Sage Weil31b80062009-10-06 11:31:13 -0700731 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500732 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600733 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700734
735 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600736 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
737 con->out_msg->hdr.crc = cpu_to_le32(crc);
Alex Elderfd154f32012-06-11 14:57:13 -0500738 con->out_msg->footer.flags = 0;
Alex Eldera9a0c512012-02-15 07:43:54 -0600739
740 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
741 con->out_msg->footer.front_crc = cpu_to_le32(crc);
742 if (m->middle) {
743 crc = crc32c(0, m->middle->vec.iov_base,
744 m->middle->vec.iov_len);
745 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
746 } else
Sage Weil31b80062009-10-06 11:31:13 -0700747 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -0500748 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700749 le32_to_cpu(con->out_msg->footer.front_crc),
750 le32_to_cpu(con->out_msg->footer.middle_crc));
751
752 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -0500753 con->out_msg->footer.data_crc = 0;
754 if (m->hdr.data_len)
755 prepare_write_message_data(con);
756 else
Sage Weil31b80062009-10-06 11:31:13 -0700757 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600758 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700759
Alex Elder928443c2012-05-22 11:41:43 -0500760 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700761}
762
763/*
764 * Prepare an ack.
765 */
766static void prepare_write_ack(struct ceph_connection *con)
767{
768 dout("prepare_write_ack %p %llu -> %llu\n", con,
769 con->in_seq_acked, con->in_seq);
770 con->in_seq_acked = con->in_seq;
771
Alex Eldere2200422012-05-23 14:35:23 -0500772 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600773
Alex Eldere2200422012-05-23 14:35:23 -0500774 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600775
Sage Weil31b80062009-10-06 11:31:13 -0700776 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500777 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600778 &con->out_temp_ack);
779
Sage Weil31b80062009-10-06 11:31:13 -0700780 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500781 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700782}
783
784/*
785 * Prepare to write keepalive byte.
786 */
787static void prepare_write_keepalive(struct ceph_connection *con)
788{
789 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500790 con_out_kvec_reset(con);
791 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500792 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700793}
794
795/*
796 * Connection negotiation.
797 */
798
Alex Elderdac1e712012-05-16 15:16:39 -0500799static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
800 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800801{
Alex Eldera3530df2012-05-16 15:16:39 -0500802 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500803
804 if (!con->ops->get_authorizer) {
805 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
806 con->out_connect.authorizer_len = 0;
807
Alex Elder729796b2012-05-16 15:16:39 -0500808 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500809 }
810
811 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800812
Sage Weilec302642009-12-22 10:43:42 -0800813 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500814
Alex Elderdac1e712012-05-16 15:16:39 -0500815 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500816
Sage Weilec302642009-12-22 10:43:42 -0800817 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800818
Alex Eldera3530df2012-05-16 15:16:39 -0500819 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500820 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500821 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500822 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700823
Alex Elder8f43fb52012-05-16 15:16:39 -0500824 con->auth_reply_buf = auth->authorizer_reply_buf;
825 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
826
Alex Elder859eb792012-02-14 14:05:33 -0600827
Alex Elder729796b2012-05-16 15:16:39 -0500828 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800829}
830
Sage Weil31b80062009-10-06 11:31:13 -0700831/*
832 * We connected to a peer and are saying hello.
833 */
Alex Eldere825a662012-05-16 15:16:38 -0500834static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700835{
Alex Eldere2200422012-05-23 14:35:23 -0500836 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
837 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500838 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800839
Sage Weileed0ef22009-11-10 14:34:36 -0800840 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500841 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800842}
843
Alex Eldere825a662012-05-16 15:16:38 -0500844static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800845{
Eric Dumazet95c96172012-04-15 05:58:06 +0000846 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700847 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500848 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500849 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700850
851 switch (con->peer_name.type) {
852 case CEPH_ENTITY_TYPE_MON:
853 proto = CEPH_MONC_PROTOCOL;
854 break;
855 case CEPH_ENTITY_TYPE_OSD:
856 proto = CEPH_OSDC_PROTOCOL;
857 break;
858 case CEPH_ENTITY_TYPE_MDS:
859 proto = CEPH_MDSC_PROTOCOL;
860 break;
861 default:
862 BUG();
863 }
864
865 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
866 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800867
Alex Eldere825a662012-05-16 15:16:38 -0500868 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700869 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
870 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
871 con->out_connect.global_seq = cpu_to_le32(global_seq);
872 con->out_connect.protocol_version = cpu_to_le32(proto);
873 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700874
Alex Elderdac1e712012-05-16 15:16:39 -0500875 auth_proto = CEPH_AUTH_UNKNOWN;
876 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500877 if (IS_ERR(auth))
878 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500879
Alex Elderdac1e712012-05-16 15:16:39 -0500880 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500881 con->out_connect.authorizer_len = auth ?
882 cpu_to_le32(auth->authorizer_buf_len) : 0;
883
Alex Elderab166d52012-05-31 11:37:29 -0500884 con_out_kvec_reset(con);
Alex Eldere2200422012-05-23 14:35:23 -0500885 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500886 &con->out_connect);
887 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500888 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500889 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600890
Sage Weil31b80062009-10-06 11:31:13 -0700891 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500892 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800893
Alex Eldere10c7582012-05-16 15:16:38 -0500894 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700895}
896
Sage Weil31b80062009-10-06 11:31:13 -0700897/*
898 * write as much of pending kvecs to the socket as we can.
899 * 1 -> done
900 * 0 -> socket full, but more to do
901 * <0 -> error
902 */
903static int write_partial_kvec(struct ceph_connection *con)
904{
905 int ret;
906
907 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
908 while (con->out_kvec_bytes > 0) {
909 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
910 con->out_kvec_left, con->out_kvec_bytes,
911 con->out_more);
912 if (ret <= 0)
913 goto out;
914 con->out_kvec_bytes -= ret;
915 if (con->out_kvec_bytes == 0)
916 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600917
918 /* account for full iov entries consumed */
919 while (ret >= con->out_kvec_cur->iov_len) {
920 BUG_ON(!con->out_kvec_left);
921 ret -= con->out_kvec_cur->iov_len;
922 con->out_kvec_cur++;
923 con->out_kvec_left--;
924 }
925 /* and for a partially-consumed entry */
926 if (ret) {
927 con->out_kvec_cur->iov_len -= ret;
928 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700929 }
930 }
931 con->out_kvec_left = 0;
932 con->out_kvec_is_msg = false;
933 ret = 1;
934out:
935 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
936 con->out_kvec_bytes, con->out_kvec_left, ret);
937 return ret; /* done! */
938}
939
Alex Elder84ca8fc2012-06-11 14:57:13 -0500940static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
941 size_t len, size_t sent, bool in_trail)
942{
943 struct ceph_msg *msg = con->out_msg;
944
945 BUG_ON(!msg);
946 BUG_ON(!sent);
947
948 con->out_msg_pos.data_pos += sent;
949 con->out_msg_pos.page_pos += sent;
Alex Elder5821bd82012-06-11 14:57:13 -0500950 if (sent < len)
951 return;
952
953 BUG_ON(sent != len);
954 con->out_msg_pos.page_pos = 0;
955 con->out_msg_pos.page++;
956 con->out_msg_pos.did_page_crc = false;
957 if (in_trail)
958 list_move_tail(&page->lru,
959 &msg->trail->head);
960 else if (msg->pagelist)
961 list_move_tail(&page->lru,
962 &msg->pagelist->head);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500963#ifdef CONFIG_BLOCK
Alex Elder5821bd82012-06-11 14:57:13 -0500964 else if (msg->bio)
965 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500966#endif
Alex Elder84ca8fc2012-06-11 14:57:13 -0500967}
968
Sage Weil31b80062009-10-06 11:31:13 -0700969/*
970 * Write as much message data payload as we can. If we finish, queue
971 * up the footer.
972 * 1 -> done, footer is now queued in out_kvec[].
973 * 0 -> socket full, but more to do
974 * <0 -> error
975 */
976static int write_partial_msg_pages(struct ceph_connection *con)
977{
978 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +0000979 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -0700980 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600981 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700982 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700983 int total_max_write;
Alex Elder84ca8fc2012-06-11 14:57:13 -0500984 bool in_trail = false;
Alex Elder5821bd82012-06-11 14:57:13 -0500985 const size_t trail_len = (msg->trail ? msg->trail->length : 0);
986 const size_t trail_off = data_len - trail_len;
Sage Weil31b80062009-10-06 11:31:13 -0700987
988 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
Alex Elder84ca8fc2012-06-11 14:57:13 -0500989 con, msg, con->out_msg_pos.page, msg->nr_pages,
Sage Weil31b80062009-10-06 11:31:13 -0700990 con->out_msg_pos.page_pos);
991
Alex Elder5821bd82012-06-11 14:57:13 -0500992 /*
993 * Iterate through each page that contains data to be
994 * written, and send as much as possible for each.
995 *
996 * If we are calculating the data crc (the default), we will
997 * need to map the page. If we have no pages, they have
998 * been revoked, so use the zero page.
999 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001000 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -07001001 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001002 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -06001003 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001004
Alex Elder5821bd82012-06-11 14:57:13 -05001005 in_trail = in_trail || con->out_msg_pos.data_pos >= trail_off;
1006 if (!in_trail)
1007 total_max_write = trail_off - con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001008
Alex Elder5821bd82012-06-11 14:57:13 -05001009 if (in_trail) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001010 total_max_write = data_len - con->out_msg_pos.data_pos;
1011
1012 page = list_first_entry(&msg->trail->head,
1013 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001014 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -07001015 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -08001016 } else if (msg->pagelist) {
1017 page = list_first_entry(&msg->pagelist->head,
1018 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001019#ifdef CONFIG_BLOCK
1020 } else if (msg->bio) {
1021 struct bio_vec *bv;
1022
1023 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
1024 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -06001025 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001026 max_write = bv->bv_len;
1027#endif
Sage Weil31b80062009-10-06 11:31:13 -07001028 } else {
Alex Elder57666512012-01-23 15:49:27 -06001029 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -07001030 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001031 len = min_t(int, max_write - con->out_msg_pos.page_pos,
1032 total_max_write);
1033
Alex Elder37675b02012-03-07 11:40:08 -06001034 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -06001035 void *base;
Alex Elder5821bd82012-06-11 14:57:13 -05001036 u32 crc = le32_to_cpu(msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -06001037 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -07001038
Alex Elder8d63e312012-03-07 11:40:08 -06001039 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -07001040 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -06001041 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Elder5821bd82012-06-11 14:57:13 -05001042 crc = crc32c(crc, base, len);
Alex Elder84ca8fc2012-06-11 14:57:13 -05001043 msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -06001044 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -07001045 }
Alex Eldere36b13c2012-03-07 11:40:08 -06001046 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -06001047 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -06001048 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001049
Alex Elder0cdf9e62012-03-07 11:40:08 -06001050 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001051 kunmap(page);
1052
1053 if (ret <= 0)
1054 goto out;
1055
Alex Elder84ca8fc2012-06-11 14:57:13 -05001056 out_msg_pos_next(con, page, len, (size_t) ret, in_trail);
Sage Weil31b80062009-10-06 11:31:13 -07001057 }
1058
1059 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1060
1061 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001062 if (!do_datacrc)
Alex Elder84ca8fc2012-06-11 14:57:13 -05001063 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001064 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001065 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001066 ret = 1;
1067out:
1068 return ret;
1069}
1070
1071/*
1072 * write some zeros
1073 */
1074static int write_partial_skip(struct ceph_connection *con)
1075{
1076 int ret;
1077
1078 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001079 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001080
Alex Elder31739132012-03-07 11:40:08 -06001081 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001082 if (ret <= 0)
1083 goto out;
1084 con->out_skip -= ret;
1085 }
1086 ret = 1;
1087out:
1088 return ret;
1089}
1090
1091/*
1092 * Prepare to read connection handshake, or an ack.
1093 */
Sage Weileed0ef22009-11-10 14:34:36 -08001094static void prepare_read_banner(struct ceph_connection *con)
1095{
1096 dout("prepare_read_banner %p\n", con);
1097 con->in_base_pos = 0;
1098}
1099
Sage Weil31b80062009-10-06 11:31:13 -07001100static void prepare_read_connect(struct ceph_connection *con)
1101{
1102 dout("prepare_read_connect %p\n", con);
1103 con->in_base_pos = 0;
1104}
1105
1106static void prepare_read_ack(struct ceph_connection *con)
1107{
1108 dout("prepare_read_ack %p\n", con);
1109 con->in_base_pos = 0;
1110}
1111
1112static void prepare_read_tag(struct ceph_connection *con)
1113{
1114 dout("prepare_read_tag %p\n", con);
1115 con->in_base_pos = 0;
1116 con->in_tag = CEPH_MSGR_TAG_READY;
1117}
1118
1119/*
1120 * Prepare to read a message.
1121 */
1122static int prepare_read_message(struct ceph_connection *con)
1123{
1124 dout("prepare_read_message %p\n", con);
1125 BUG_ON(con->in_msg != NULL);
1126 con->in_base_pos = 0;
1127 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1128 return 0;
1129}
1130
1131
1132static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001133 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001134{
Alex Eldere6cee712012-05-10 10:29:50 -05001135 while (con->in_base_pos < end) {
1136 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001137 int have = size - left;
1138 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1139 if (ret <= 0)
1140 return ret;
1141 con->in_base_pos += ret;
1142 }
1143 return 1;
1144}
1145
1146
1147/*
1148 * Read all or part of the connect-side handshake on a new connection
1149 */
Sage Weileed0ef22009-11-10 14:34:36 -08001150static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001151{
Alex Elderfd516532012-05-10 10:29:50 -05001152 int size;
1153 int end;
1154 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001155
Sage Weileed0ef22009-11-10 14:34:36 -08001156 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001157
1158 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001159 size = strlen(CEPH_BANNER);
1160 end = size;
1161 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001162 if (ret <= 0)
1163 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001164
1165 size = sizeof (con->actual_peer_addr);
1166 end += size;
1167 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001168 if (ret <= 0)
1169 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001170
1171 size = sizeof (con->peer_addr_for_me);
1172 end += size;
1173 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001174 if (ret <= 0)
1175 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001176
Sage Weileed0ef22009-11-10 14:34:36 -08001177out:
1178 return ret;
1179}
1180
1181static int read_partial_connect(struct ceph_connection *con)
1182{
Alex Elderfd516532012-05-10 10:29:50 -05001183 int size;
1184 int end;
1185 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001186
1187 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1188
Alex Elderfd516532012-05-10 10:29:50 -05001189 size = sizeof (con->in_reply);
1190 end = size;
1191 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001192 if (ret <= 0)
1193 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001194
1195 size = le32_to_cpu(con->in_reply.authorizer_len);
1196 end += size;
1197 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001198 if (ret <= 0)
1199 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001200
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001201 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1202 con, (int)con->in_reply.tag,
1203 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001204 le32_to_cpu(con->in_reply.global_seq));
1205out:
1206 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001207
Sage Weil31b80062009-10-06 11:31:13 -07001208}
1209
1210/*
1211 * Verify the hello banner looks okay.
1212 */
1213static int verify_hello(struct ceph_connection *con)
1214{
1215 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001216 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001217 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001218 con->error_msg = "protocol error, bad banner";
1219 return -1;
1220 }
1221 return 0;
1222}
1223
1224static bool addr_is_blank(struct sockaddr_storage *ss)
1225{
1226 switch (ss->ss_family) {
1227 case AF_INET:
1228 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1229 case AF_INET6:
1230 return
1231 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1232 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1233 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1234 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1235 }
1236 return false;
1237}
1238
1239static int addr_port(struct sockaddr_storage *ss)
1240{
1241 switch (ss->ss_family) {
1242 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001243 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001244 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001245 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001246 }
1247 return 0;
1248}
1249
1250static void addr_set_port(struct sockaddr_storage *ss, int p)
1251{
1252 switch (ss->ss_family) {
1253 case AF_INET:
1254 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001255 break;
Sage Weil31b80062009-10-06 11:31:13 -07001256 case AF_INET6:
1257 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001258 break;
Sage Weil31b80062009-10-06 11:31:13 -07001259 }
1260}
1261
1262/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001263 * Unlike other *_pton function semantics, zero indicates success.
1264 */
1265static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1266 char delim, const char **ipend)
1267{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001268 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1269 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001270
1271 memset(ss, 0, sizeof(*ss));
1272
1273 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1274 ss->ss_family = AF_INET;
1275 return 0;
1276 }
1277
1278 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1279 ss->ss_family = AF_INET6;
1280 return 0;
1281 }
1282
1283 return -EINVAL;
1284}
1285
1286/*
1287 * Extract hostname string and resolve using kernel DNS facility.
1288 */
1289#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1290static int ceph_dns_resolve_name(const char *name, size_t namelen,
1291 struct sockaddr_storage *ss, char delim, const char **ipend)
1292{
1293 const char *end, *delim_p;
1294 char *colon_p, *ip_addr = NULL;
1295 int ip_len, ret;
1296
1297 /*
1298 * The end of the hostname occurs immediately preceding the delimiter or
1299 * the port marker (':') where the delimiter takes precedence.
1300 */
1301 delim_p = memchr(name, delim, namelen);
1302 colon_p = memchr(name, ':', namelen);
1303
1304 if (delim_p && colon_p)
1305 end = delim_p < colon_p ? delim_p : colon_p;
1306 else if (!delim_p && colon_p)
1307 end = colon_p;
1308 else {
1309 end = delim_p;
1310 if (!end) /* case: hostname:/ */
1311 end = name + namelen;
1312 }
1313
1314 if (end <= name)
1315 return -EINVAL;
1316
1317 /* do dns_resolve upcall */
1318 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1319 if (ip_len > 0)
1320 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1321 else
1322 ret = -ESRCH;
1323
1324 kfree(ip_addr);
1325
1326 *ipend = end;
1327
1328 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1329 ret, ret ? "failed" : ceph_pr_addr(ss));
1330
1331 return ret;
1332}
1333#else
1334static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1335 struct sockaddr_storage *ss, char delim, const char **ipend)
1336{
1337 return -EINVAL;
1338}
1339#endif
1340
1341/*
1342 * Parse a server name (IP or hostname). If a valid IP address is not found
1343 * then try to extract a hostname to resolve using userspace DNS upcall.
1344 */
1345static int ceph_parse_server_name(const char *name, size_t namelen,
1346 struct sockaddr_storage *ss, char delim, const char **ipend)
1347{
1348 int ret;
1349
1350 ret = ceph_pton(name, namelen, ss, delim, ipend);
1351 if (ret)
1352 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1353
1354 return ret;
1355}
1356
1357/*
Sage Weil31b80062009-10-06 11:31:13 -07001358 * Parse an ip[:port] list into an addr array. Use the default
1359 * monitor port if a port isn't specified.
1360 */
1361int ceph_parse_ips(const char *c, const char *end,
1362 struct ceph_entity_addr *addr,
1363 int max_count, int *count)
1364{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001365 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001366 const char *p = c;
1367
1368 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1369 for (i = 0; i < max_count; i++) {
1370 const char *ipend;
1371 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001372 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001373 char delim = ',';
1374
1375 if (*p == '[') {
1376 delim = ']';
1377 p++;
1378 }
Sage Weil31b80062009-10-06 11:31:13 -07001379
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001380 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1381 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001382 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001383 ret = -EINVAL;
1384
Sage Weil31b80062009-10-06 11:31:13 -07001385 p = ipend;
1386
Sage Weil39139f62010-07-08 09:54:52 -07001387 if (delim == ']') {
1388 if (*p != ']') {
1389 dout("missing matching ']'\n");
1390 goto bad;
1391 }
1392 p++;
1393 }
1394
Sage Weil31b80062009-10-06 11:31:13 -07001395 /* port? */
1396 if (p < end && *p == ':') {
1397 port = 0;
1398 p++;
1399 while (p < end && *p >= '0' && *p <= '9') {
1400 port = (port * 10) + (*p - '0');
1401 p++;
1402 }
1403 if (port > 65535 || port == 0)
1404 goto bad;
1405 } else {
1406 port = CEPH_MON_PORT;
1407 }
1408
1409 addr_set_port(ss, port);
1410
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001411 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001412
1413 if (p == end)
1414 break;
1415 if (*p != ',')
1416 goto bad;
1417 p++;
1418 }
1419
1420 if (p != end)
1421 goto bad;
1422
1423 if (count)
1424 *count = i + 1;
1425 return 0;
1426
1427bad:
Sage Weil39139f62010-07-08 09:54:52 -07001428 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001429 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001430}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001431EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001432
Sage Weileed0ef22009-11-10 14:34:36 -08001433static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001434{
Sage Weileed0ef22009-11-10 14:34:36 -08001435 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001436
1437 if (verify_hello(con) < 0)
1438 return -1;
1439
Sage Weil63f2d212009-11-03 15:17:56 -08001440 ceph_decode_addr(&con->actual_peer_addr);
1441 ceph_decode_addr(&con->peer_addr_for_me);
1442
Sage Weil31b80062009-10-06 11:31:13 -07001443 /*
1444 * Make sure the other end is who we wanted. note that the other
1445 * end may not yet know their ip address, so if it's 0.0.0.0, give
1446 * them the benefit of the doubt.
1447 */
Sage Weil103e2d32010-01-07 16:12:36 -08001448 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1449 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001450 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1451 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001452 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001453 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001454 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001455 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001456 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001457 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001458 return -1;
1459 }
1460
1461 /*
1462 * did we learn our address?
1463 */
1464 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1465 int port = addr_port(&con->msgr->inst.addr.in_addr);
1466
1467 memcpy(&con->msgr->inst.addr.in_addr,
1468 &con->peer_addr_for_me.in_addr,
1469 sizeof(con->peer_addr_for_me.in_addr));
1470 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001471 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001472 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001473 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001474 }
1475
Sage Weileed0ef22009-11-10 14:34:36 -08001476 return 0;
1477}
1478
Sage Weil04a419f2009-12-23 09:30:21 -08001479static void fail_protocol(struct ceph_connection *con)
1480{
1481 reset_connection(con);
1482 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001483}
1484
Sage Weileed0ef22009-11-10 14:34:36 -08001485static int process_connect(struct ceph_connection *con)
1486{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001487 u64 sup_feat = con->msgr->supported_features;
1488 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001489 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001490 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001491
Sage Weileed0ef22009-11-10 14:34:36 -08001492 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1493
Sage Weil31b80062009-10-06 11:31:13 -07001494 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001495 case CEPH_MSGR_TAG_FEATURES:
1496 pr_err("%s%lld %s feature set mismatch,"
1497 " my %llx < server's %llx, missing %llx\n",
1498 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001499 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001500 sup_feat, server_feat, server_feat & ~sup_feat);
1501 con->error_msg = "missing required protocol features";
1502 fail_protocol(con);
1503 return -1;
1504
Sage Weil31b80062009-10-06 11:31:13 -07001505 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001506 pr_err("%s%lld %s protocol version mismatch,"
1507 " my %d != server's %d\n",
1508 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001509 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001510 le32_to_cpu(con->out_connect.protocol_version),
1511 le32_to_cpu(con->in_reply.protocol_version));
1512 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001513 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001514 return -1;
1515
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001516 case CEPH_MSGR_TAG_BADAUTHORIZER:
1517 con->auth_retry++;
1518 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1519 con->auth_retry);
1520 if (con->auth_retry == 2) {
1521 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001522 return -1;
1523 }
1524 con->auth_retry = 1;
Alex Eldere825a662012-05-16 15:16:38 -05001525 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001526 if (ret < 0)
1527 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001528 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001529 break;
Sage Weil31b80062009-10-06 11:31:13 -07001530
1531 case CEPH_MSGR_TAG_RESETSESSION:
1532 /*
1533 * If we connected with a large connect_seq but the peer
1534 * has no record of a session with us (no connection, or
1535 * connect_seq == 0), they will send RESETSESION to indicate
1536 * that they must have reset their session, and may have
1537 * dropped messages.
1538 */
1539 dout("process_connect got RESET peer seq %u\n",
1540 le32_to_cpu(con->in_connect.connect_seq));
1541 pr_err("%s%lld %s connection reset\n",
1542 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001543 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001544 reset_connection(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001545 ret = prepare_write_connect(con);
1546 if (ret < 0)
1547 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001548 prepare_read_connect(con);
1549
1550 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001551 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001552 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1553 if (con->ops->peer_reset)
1554 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001555 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001556 if (test_bit(CLOSED, &con->state) ||
1557 test_bit(OPENING, &con->state))
1558 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001559 break;
1560
1561 case CEPH_MSGR_TAG_RETRY_SESSION:
1562 /*
1563 * If we sent a smaller connect_seq than the peer has, try
1564 * again with a larger value.
1565 */
1566 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1567 le32_to_cpu(con->out_connect.connect_seq),
1568 le32_to_cpu(con->in_connect.connect_seq));
1569 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001570 ret = prepare_write_connect(con);
1571 if (ret < 0)
1572 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001573 prepare_read_connect(con);
1574 break;
1575
1576 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1577 /*
1578 * If we sent a smaller global_seq than the peer has, try
1579 * again with a larger value.
1580 */
Sage Weileed0ef22009-11-10 14:34:36 -08001581 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001582 con->peer_global_seq,
1583 le32_to_cpu(con->in_connect.global_seq));
1584 get_global_seq(con->msgr,
1585 le32_to_cpu(con->in_connect.global_seq));
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001586 ret = prepare_write_connect(con);
1587 if (ret < 0)
1588 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001589 prepare_read_connect(con);
1590 break;
1591
1592 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001593 if (req_feat & ~server_feat) {
1594 pr_err("%s%lld %s protocol feature mismatch,"
1595 " my required %llx > server's %llx, need %llx\n",
1596 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001597 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001598 req_feat, server_feat, req_feat & ~server_feat);
1599 con->error_msg = "missing required protocol features";
1600 fail_protocol(con);
1601 return -1;
1602 }
Alex Elder3ec50d12012-05-23 14:35:23 -05001603 clear_bit(NEGOTIATING, &con->state);
Alex Eldere27947c2012-05-23 14:35:23 -05001604 set_bit(CONNECTED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001605 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1606 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001607 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001608 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1609 con->peer_global_seq,
1610 le32_to_cpu(con->in_reply.connect_seq),
1611 con->connect_seq);
1612 WARN_ON(con->connect_seq !=
1613 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001614
1615 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001616 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001617
Sage Weil31b80062009-10-06 11:31:13 -07001618 prepare_read_tag(con);
1619 break;
1620
1621 case CEPH_MSGR_TAG_WAIT:
1622 /*
1623 * If there is a connection race (we are opening
1624 * connections to each other), one of us may just have
1625 * to WAIT. This shouldn't happen if we are the
1626 * client.
1627 */
Sage Weil04177882011-05-12 15:33:17 -07001628 pr_err("process_connect got WAIT as client\n");
1629 con->error_msg = "protocol error, got WAIT as client";
1630 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001631
1632 default:
1633 pr_err("connect protocol error, will retry\n");
1634 con->error_msg = "protocol error, garbage tag during connect";
1635 return -1;
1636 }
1637 return 0;
1638}
1639
1640
1641/*
1642 * read (part of) an ack
1643 */
1644static int read_partial_ack(struct ceph_connection *con)
1645{
Alex Elderfd516532012-05-10 10:29:50 -05001646 int size = sizeof (con->in_temp_ack);
1647 int end = size;
1648
1649 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001650}
1651
1652
1653/*
1654 * We can finally discard anything that's been acked.
1655 */
1656static void process_ack(struct ceph_connection *con)
1657{
1658 struct ceph_msg *m;
1659 u64 ack = le64_to_cpu(con->in_temp_ack);
1660 u64 seq;
1661
Sage Weil31b80062009-10-06 11:31:13 -07001662 while (!list_empty(&con->out_sent)) {
1663 m = list_first_entry(&con->out_sent, struct ceph_msg,
1664 list_head);
1665 seq = le64_to_cpu(m->hdr.seq);
1666 if (seq > ack)
1667 break;
1668 dout("got ack for seq %llu type %d at %p\n", seq,
1669 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001670 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001671 ceph_msg_remove(m);
1672 }
Sage Weil31b80062009-10-06 11:31:13 -07001673 prepare_read_tag(con);
1674}
1675
1676
1677
1678
Yehuda Sadeh24504182010-01-08 13:58:34 -08001679static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001680 struct kvec *section,
1681 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001682{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001683 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001684
Yehuda Sadeh24504182010-01-08 13:58:34 -08001685 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001686
Yehuda Sadeh24504182010-01-08 13:58:34 -08001687 while (section->iov_len < sec_len) {
1688 BUG_ON(section->iov_base == NULL);
1689 left = sec_len - section->iov_len;
1690 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1691 section->iov_len, left);
1692 if (ret <= 0)
1693 return ret;
1694 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001695 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001696 if (section->iov_len == sec_len)
1697 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001698
1699 return 1;
1700}
1701
Alex Elder1c20f2d2012-06-04 14:43:32 -05001702static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1703 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001704
1705
1706static int read_partial_message_pages(struct ceph_connection *con,
1707 struct page **pages,
Eric Dumazet95c96172012-04-15 05:58:06 +00001708 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001709{
1710 void *p;
1711 int ret;
1712 int left;
1713
1714 left = min((int)(data_len - con->in_msg_pos.data_pos),
1715 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1716 /* (page) data */
1717 BUG_ON(pages == NULL);
1718 p = kmap(pages[con->in_msg_pos.page]);
1719 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1720 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001721 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001722 con->in_data_crc =
1723 crc32c(con->in_data_crc,
1724 p + con->in_msg_pos.page_pos, ret);
1725 kunmap(pages[con->in_msg_pos.page]);
1726 if (ret <= 0)
1727 return ret;
1728 con->in_msg_pos.data_pos += ret;
1729 con->in_msg_pos.page_pos += ret;
1730 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1731 con->in_msg_pos.page_pos = 0;
1732 con->in_msg_pos.page++;
1733 }
1734
1735 return ret;
1736}
1737
1738#ifdef CONFIG_BLOCK
1739static int read_partial_message_bio(struct ceph_connection *con,
1740 struct bio **bio_iter, int *bio_seg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001741 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001742{
1743 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1744 void *p;
1745 int ret, left;
1746
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001747 left = min((int)(data_len - con->in_msg_pos.data_pos),
1748 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1749
1750 p = kmap(bv->bv_page) + bv->bv_offset;
1751
1752 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1753 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001754 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001755 con->in_data_crc =
1756 crc32c(con->in_data_crc,
1757 p + con->in_msg_pos.page_pos, ret);
1758 kunmap(bv->bv_page);
1759 if (ret <= 0)
1760 return ret;
1761 con->in_msg_pos.data_pos += ret;
1762 con->in_msg_pos.page_pos += ret;
1763 if (con->in_msg_pos.page_pos == bv->bv_len) {
1764 con->in_msg_pos.page_pos = 0;
1765 iter_bio_next(bio_iter, bio_seg);
1766 }
1767
1768 return ret;
1769}
1770#endif
1771
Sage Weil31b80062009-10-06 11:31:13 -07001772/*
1773 * read (part of) a message.
1774 */
1775static int read_partial_message(struct ceph_connection *con)
1776{
1777 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001778 int size;
1779 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001780 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001781 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001782 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001783 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001784 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001785
1786 dout("read_partial_message con %p msg %p\n", con, m);
1787
1788 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001789 size = sizeof (con->in_hdr);
1790 end = size;
1791 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001792 if (ret <= 0)
1793 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001794
1795 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1796 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1797 pr_err("read_partial_message bad hdr "
1798 " crc %u != expected %u\n",
1799 crc, con->in_hdr.crc);
1800 return -EBADMSG;
1801 }
1802
Sage Weil31b80062009-10-06 11:31:13 -07001803 front_len = le32_to_cpu(con->in_hdr.front_len);
1804 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1805 return -EIO;
1806 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1807 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1808 return -EIO;
1809 data_len = le32_to_cpu(con->in_hdr.data_len);
1810 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1811 return -EIO;
1812
Sage Weilae187562010-04-22 07:47:01 -07001813 /* verify seq# */
1814 seq = le64_to_cpu(con->in_hdr.seq);
1815 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001816 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001817 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001818 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001819 seq, con->in_seq + 1);
1820 con->in_base_pos = -front_len - middle_len - data_len -
1821 sizeof(m->footer);
1822 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001823 return 0;
1824 } else if ((s64)seq - (s64)con->in_seq > 1) {
1825 pr_err("read_partial_message bad seq %lld expected %lld\n",
1826 seq, con->in_seq + 1);
1827 con->error_msg = "bad message sequence # for incoming message";
1828 return -EBADMSG;
1829 }
1830
Sage Weil31b80062009-10-06 11:31:13 -07001831 /* allocate message? */
1832 if (!con->in_msg) {
1833 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1834 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001835 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001836 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001837 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001838 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001839 con->in_base_pos = -front_len - middle_len - data_len -
1840 sizeof(m->footer);
1841 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001842 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001843 return 0;
1844 }
Sage Weila79832f2010-04-01 16:06:19 -07001845 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001846 con->error_msg =
1847 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001848 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001849 }
Alex Elder38941f82012-06-01 14:56:43 -05001850
1851 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001852 m = con->in_msg;
1853 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001854 if (m->middle)
1855 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001856
1857 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001858 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001859 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001860 else
1861 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001862 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001863 }
1864
1865 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001866 ret = read_partial_message_section(con, &m->front, front_len,
1867 &con->in_front_crc);
1868 if (ret <= 0)
1869 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001870
1871 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001872 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001873 ret = read_partial_message_section(con, &m->middle->vec,
1874 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001875 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001876 if (ret <= 0)
1877 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001878 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001879#ifdef CONFIG_BLOCK
1880 if (m->bio && !m->bio_iter)
1881 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1882#endif
Sage Weil31b80062009-10-06 11:31:13 -07001883
1884 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001885 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001886 if (m->pages) {
1887 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001888 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001889 if (ret <= 0)
1890 return ret;
1891#ifdef CONFIG_BLOCK
1892 } else if (m->bio) {
1893
1894 ret = read_partial_message_bio(con,
1895 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001896 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001897 if (ret <= 0)
1898 return ret;
1899#endif
1900 } else {
1901 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001902 }
1903 }
1904
Sage Weil31b80062009-10-06 11:31:13 -07001905 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001906 size = sizeof (m->footer);
1907 end += size;
1908 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001909 if (ret <= 0)
1910 return ret;
1911
Sage Weil31b80062009-10-06 11:31:13 -07001912 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1913 m, front_len, m->footer.front_crc, middle_len,
1914 m->footer.middle_crc, data_len, m->footer.data_crc);
1915
1916 /* crc ok? */
1917 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1918 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1919 m, con->in_front_crc, m->footer.front_crc);
1920 return -EBADMSG;
1921 }
1922 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1923 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1924 m, con->in_middle_crc, m->footer.middle_crc);
1925 return -EBADMSG;
1926 }
Alex Elderbca064d2012-02-15 07:43:54 -06001927 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001928 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1929 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1930 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1931 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1932 return -EBADMSG;
1933 }
1934
1935 return 1; /* done! */
1936}
1937
1938/*
1939 * Process message. This happens in the worker thread. The callback should
1940 * be careful not to do anything that waits on other incoming messages or it
1941 * may deadlock.
1942 */
1943static void process_message(struct ceph_connection *con)
1944{
Sage Weil5e095e82009-12-14 14:30:34 -08001945 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001946
Alex Elder38941f82012-06-01 14:56:43 -05001947 BUG_ON(con->in_msg->con != con);
1948 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001949 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001950 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001951 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001952
1953 /* if first message, set peer_name */
1954 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001955 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001956
Sage Weil31b80062009-10-06 11:31:13 -07001957 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001958 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001959
1960 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1961 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001962 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001963 le16_to_cpu(msg->hdr.type),
1964 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1965 le32_to_cpu(msg->hdr.front_len),
1966 le32_to_cpu(msg->hdr.data_len),
1967 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1968 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001969
1970 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001971 prepare_read_tag(con);
1972}
1973
1974
1975/*
1976 * Write something to the socket. Called in a worker thread when the
1977 * socket appears to be writeable and we have something ready to send.
1978 */
1979static int try_write(struct ceph_connection *con)
1980{
Sage Weil31b80062009-10-06 11:31:13 -07001981 int ret = 1;
1982
Sage Weild59315c2012-06-21 12:49:23 -07001983 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001984
Sage Weil31b80062009-10-06 11:31:13 -07001985more:
1986 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1987
1988 /* open the socket first? */
1989 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001990 set_bit(CONNECTING, &con->state);
1991
Alex Eldere2200422012-05-23 14:35:23 -05001992 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001993 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001994 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001995
Sage Weilcf3e5c42009-12-11 09:48:05 -08001996 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001997 con->in_tag = CEPH_MSGR_TAG_READY;
1998 dout("try_write initiating connect on %p new state %lu\n",
1999 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002000 ret = ceph_tcp_connect(con);
2001 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002002 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002003 goto out;
2004 }
2005 }
2006
2007more_kvec:
2008 /* kvec data queued? */
2009 if (con->out_skip) {
2010 ret = write_partial_skip(con);
2011 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002012 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002013 }
2014 if (con->out_kvec_left) {
2015 ret = write_partial_kvec(con);
2016 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002017 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002018 }
2019
2020 /* msg pages? */
2021 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002022 if (con->out_msg_done) {
2023 ceph_msg_put(con->out_msg);
2024 con->out_msg = NULL; /* we're done with this one */
2025 goto do_next;
2026 }
2027
Sage Weil31b80062009-10-06 11:31:13 -07002028 ret = write_partial_msg_pages(con);
2029 if (ret == 1)
2030 goto more_kvec; /* we need to send the footer, too! */
2031 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002032 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002033 if (ret < 0) {
2034 dout("try_write write_partial_msg_pages err %d\n",
2035 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002036 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002037 }
2038 }
2039
Sage Weilc86a2932009-12-14 14:04:30 -08002040do_next:
Alex Elder7593af92012-05-24 11:55:03 -05002041 if (!test_bit(CONNECTING, &con->state) &&
2042 !test_bit(NEGOTIATING, &con->state)) {
Sage Weil31b80062009-10-06 11:31:13 -07002043 /* is anything else pending? */
2044 if (!list_empty(&con->out_queue)) {
2045 prepare_write_message(con);
2046 goto more;
2047 }
2048 if (con->in_seq > con->in_seq_acked) {
2049 prepare_write_ack(con);
2050 goto more;
2051 }
Alex Elder928443c2012-05-22 11:41:43 -05002052 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002053 prepare_write_keepalive(con);
2054 goto more;
2055 }
2056 }
2057
2058 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002059 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002060 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002061 ret = 0;
2062out:
Sage Weil42961d22011-01-25 08:19:34 -08002063 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002064 return ret;
2065}
2066
2067
2068
2069/*
2070 * Read what we can from the socket.
2071 */
2072static int try_read(struct ceph_connection *con)
2073{
Sage Weil31b80062009-10-06 11:31:13 -07002074 int ret = -1;
2075
2076 if (!con->sock)
2077 return 0;
2078
2079 if (test_bit(STANDBY, &con->state))
2080 return 0;
2081
2082 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002083
Sage Weil31b80062009-10-06 11:31:13 -07002084more:
2085 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2086 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002087
2088 /*
2089 * process_connect and process_message drop and re-take
2090 * con->mutex. make sure we handle a racing close or reopen.
2091 */
2092 if (test_bit(CLOSED, &con->state) ||
2093 test_bit(OPENING, &con->state)) {
2094 ret = -EAGAIN;
2095 goto out;
2096 }
2097
Sage Weil31b80062009-10-06 11:31:13 -07002098 if (test_bit(CONNECTING, &con->state)) {
Alex Elder7593af92012-05-24 11:55:03 -05002099 dout("try_read connecting\n");
2100 ret = read_partial_banner(con);
2101 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002102 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002103 ret = process_banner(con);
2104 if (ret < 0)
2105 goto out;
2106
2107 clear_bit(CONNECTING, &con->state);
2108 set_bit(NEGOTIATING, &con->state);
2109
2110 /* Banner is good, exchange connection info */
2111 ret = prepare_write_connect(con);
2112 if (ret < 0)
2113 goto out;
2114 prepare_read_connect(con);
2115
2116 /* Send connection info before awaiting response */
2117 goto out;
2118 }
2119
2120 if (test_bit(NEGOTIATING, &con->state)) {
2121 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002122 ret = read_partial_connect(con);
2123 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002124 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002125 ret = process_connect(con);
2126 if (ret < 0)
2127 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002128 goto more;
2129 }
2130
2131 if (con->in_base_pos < 0) {
2132 /*
2133 * skipping + discarding content.
2134 *
2135 * FIXME: there must be a better way to do this!
2136 */
Alex Elder84495f42012-02-15 07:43:55 -06002137 static char buf[SKIP_BUF_SIZE];
2138 int skip = min((int) sizeof (buf), -con->in_base_pos);
2139
Sage Weil31b80062009-10-06 11:31:13 -07002140 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2141 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2142 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002143 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002144 con->in_base_pos += ret;
2145 if (con->in_base_pos)
2146 goto more;
2147 }
2148 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2149 /*
2150 * what's next?
2151 */
2152 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2153 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002154 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002155 dout("try_read got tag %d\n", (int)con->in_tag);
2156 switch (con->in_tag) {
2157 case CEPH_MSGR_TAG_MSG:
2158 prepare_read_message(con);
2159 break;
2160 case CEPH_MSGR_TAG_ACK:
2161 prepare_read_ack(con);
2162 break;
2163 case CEPH_MSGR_TAG_CLOSE:
Alex Eldere27947c2012-05-23 14:35:23 -05002164 clear_bit(CONNECTED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002165 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002166 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002167 default:
2168 goto bad_tag;
2169 }
2170 }
2171 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2172 ret = read_partial_message(con);
2173 if (ret <= 0) {
2174 switch (ret) {
2175 case -EBADMSG:
2176 con->error_msg = "bad crc";
2177 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002178 break;
Sage Weil31b80062009-10-06 11:31:13 -07002179 case -EIO:
2180 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002181 break;
Sage Weil31b80062009-10-06 11:31:13 -07002182 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002183 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002184 }
2185 if (con->in_tag == CEPH_MSGR_TAG_READY)
2186 goto more;
2187 process_message(con);
2188 goto more;
2189 }
2190 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2191 ret = read_partial_ack(con);
2192 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002193 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002194 process_ack(con);
2195 goto more;
2196 }
2197
Sage Weil31b80062009-10-06 11:31:13 -07002198out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002199 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002200 return ret;
2201
2202bad_tag:
2203 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2204 con->error_msg = "protocol error, garbage tag";
2205 ret = -1;
2206 goto out;
2207}
2208
2209
2210/*
2211 * Atomically queue work on a connection. Bump @con reference to
2212 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002213 */
2214static void queue_con(struct ceph_connection *con)
2215{
Sage Weil31b80062009-10-06 11:31:13 -07002216 if (!con->ops->get(con)) {
2217 dout("queue_con %p ref count 0\n", con);
2218 return;
2219 }
2220
Tejun Heof363e45fd12011-01-03 14:49:46 +01002221 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002222 dout("queue_con %p - already queued\n", con);
2223 con->ops->put(con);
2224 } else {
2225 dout("queue_con %p\n", con);
2226 }
2227}
2228
2229/*
2230 * Do some work on a connection. Drop a connection ref when we're done.
2231 */
2232static void con_work(struct work_struct *work)
2233{
2234 struct ceph_connection *con = container_of(work, struct ceph_connection,
2235 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002236 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002237
Sage Weil9dd46582010-04-28 13:51:50 -07002238 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002239restart:
Alex Elder188048b2012-06-20 21:53:53 -05002240 if (test_and_clear_bit(SOCK_CLOSED, &con->flags)) {
Alex Eldere27947c2012-05-23 14:35:23 -05002241 if (test_and_clear_bit(CONNECTED, &con->state))
2242 con->error_msg = "socket closed";
Alex Elder7593af92012-05-24 11:55:03 -05002243 else if (test_and_clear_bit(NEGOTIATING, &con->state))
2244 con->error_msg = "negotiation failed";
2245 else if (test_and_clear_bit(CONNECTING, &con->state))
Alex Elder188048b2012-06-20 21:53:53 -05002246 con->error_msg = "connection failed";
Alex Elder7593af92012-05-24 11:55:03 -05002247 else
Alex Eldere27947c2012-05-23 14:35:23 -05002248 con->error_msg = "unrecognized con state";
Alex Elder188048b2012-06-20 21:53:53 -05002249 goto fault;
2250 }
2251
Alex Elder928443c2012-05-22 11:41:43 -05002252 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002253 dout("con_work %p backing off\n", con);
2254 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2255 round_jiffies_relative(con->delay))) {
2256 dout("con_work %p backoff %lu\n", con, con->delay);
2257 mutex_unlock(&con->mutex);
2258 return;
2259 } else {
2260 con->ops->put(con);
2261 dout("con_work %p FAILED to back off %lu\n", con,
2262 con->delay);
2263 }
2264 }
Sage Weil9dd46582010-04-28 13:51:50 -07002265
Sage Weile00de342011-03-04 12:25:05 -08002266 if (test_bit(STANDBY, &con->state)) {
2267 dout("con_work %p STANDBY\n", con);
2268 goto done;
2269 }
Sage Weil31b80062009-10-06 11:31:13 -07002270 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2271 dout("con_work CLOSED\n");
2272 con_close_socket(con);
2273 goto done;
2274 }
2275 if (test_and_clear_bit(OPENING, &con->state)) {
2276 /* reopen w/ new peer */
2277 dout("con_work OPENING\n");
2278 con_close_socket(con);
2279 }
2280
Sage Weil0da5d702011-05-19 11:21:05 -07002281 ret = try_read(con);
2282 if (ret == -EAGAIN)
2283 goto restart;
2284 if (ret < 0)
2285 goto fault;
2286
2287 ret = try_write(con);
2288 if (ret == -EAGAIN)
2289 goto restart;
2290 if (ret < 0)
2291 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002292
2293done:
Sage Weil9dd46582010-04-28 13:51:50 -07002294 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002295done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002296 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002297 return;
2298
2299fault:
2300 mutex_unlock(&con->mutex);
2301 ceph_fault(con); /* error/fault path */
2302 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002303}
2304
2305
2306/*
2307 * Generic error/fault handler. A retry mechanism is used with
2308 * exponential backoff
2309 */
2310static void ceph_fault(struct ceph_connection *con)
2311{
2312 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002313 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002314 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002315 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002316
Alex Elder928443c2012-05-22 11:41:43 -05002317 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002318 dout("fault on LOSSYTX channel\n");
2319 goto out;
2320 }
2321
Sage Weilec302642009-12-22 10:43:42 -08002322 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002323 if (test_bit(CLOSED, &con->state))
2324 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002325
Sage Weil31b80062009-10-06 11:31:13 -07002326 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002327
2328 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002329 BUG_ON(con->in_msg->con != con);
2330 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002331 ceph_msg_put(con->in_msg);
2332 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002333 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002334 }
Sage Weil31b80062009-10-06 11:31:13 -07002335
Sage Weile80a52d2010-02-25 12:40:45 -08002336 /* Requeue anything that hasn't been acked */
2337 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002338
Sage Weile76661d2011-03-03 10:10:15 -08002339 /* If there are no messages queued or keepalive pending, place
2340 * the connection in a STANDBY state */
2341 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002342 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002343 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002344 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002345 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002346 } else {
2347 /* retry after a delay. */
2348 if (con->delay == 0)
2349 con->delay = BASE_DELAY_INTERVAL;
2350 else if (con->delay < MAX_DELAY_INTERVAL)
2351 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002352 con->ops->get(con);
2353 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002354 round_jiffies_relative(con->delay))) {
2355 dout("fault queued %p delay %lu\n", con, con->delay);
2356 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002357 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002358 dout("fault failed to queue %p delay %lu, backoff\n",
2359 con, con->delay);
2360 /*
2361 * In many cases we see a socket state change
2362 * while con_work is running and end up
2363 * queuing (non-delayed) work, such that we
2364 * can't backoff with a delay. Set a flag so
2365 * that when con_work restarts we schedule the
2366 * delay then.
2367 */
Alex Elder928443c2012-05-22 11:41:43 -05002368 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002369 }
Sage Weil31b80062009-10-06 11:31:13 -07002370 }
2371
Sage Weil91e45ce32010-02-15 12:05:09 -08002372out_unlock:
2373 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002374out:
Sage Weil161fd652010-02-25 12:38:57 -08002375 /*
2376 * in case we faulted due to authentication, invalidate our
2377 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002378 */
Sage Weil161fd652010-02-25 12:38:57 -08002379 if (con->auth_retry && con->ops->invalidate_authorizer) {
2380 dout("calling invalidate_authorizer()\n");
2381 con->ops->invalidate_authorizer(con);
2382 }
2383
Sage Weil31b80062009-10-06 11:31:13 -07002384 if (con->ops->fault)
2385 con->ops->fault(con);
2386}
2387
2388
2389
2390/*
Alex Elder15d98822012-05-26 23:26:43 -05002391 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002392 */
Alex Elder15d98822012-05-26 23:26:43 -05002393void ceph_messenger_init(struct ceph_messenger *msgr,
2394 struct ceph_entity_addr *myaddr,
2395 u32 supported_features,
2396 u32 required_features,
2397 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002398{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002399 msgr->supported_features = supported_features;
2400 msgr->required_features = required_features;
2401
Sage Weil31b80062009-10-06 11:31:13 -07002402 spin_lock_init(&msgr->global_seq_lock);
2403
Sage Weil31b80062009-10-06 11:31:13 -07002404 if (myaddr)
2405 msgr->inst.addr = *myaddr;
2406
2407 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002408 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002409 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002410 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002411 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002412
Alex Elder15d98822012-05-26 23:26:43 -05002413 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002414}
Alex Elder15d98822012-05-26 23:26:43 -05002415EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002416
Sage Weile00de342011-03-04 12:25:05 -08002417static void clear_standby(struct ceph_connection *con)
2418{
2419 /* come back from STANDBY? */
2420 if (test_and_clear_bit(STANDBY, &con->state)) {
2421 mutex_lock(&con->mutex);
2422 dout("clear_standby %p and ++connect_seq\n", con);
2423 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002424 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2425 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002426 mutex_unlock(&con->mutex);
2427 }
2428}
2429
Sage Weil31b80062009-10-06 11:31:13 -07002430/*
2431 * Queue up an outgoing message on the given connection.
2432 */
2433void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2434{
2435 if (test_bit(CLOSED, &con->state)) {
2436 dout("con_send %p closed, dropping %p\n", con, msg);
2437 ceph_msg_put(msg);
2438 return;
2439 }
2440
2441 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002442 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002443
Sage Weil3ca02ef2010-03-01 15:25:00 -08002444 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2445
Sage Weile84346b2010-05-11 21:20:38 -07002446 msg->needs_out_seq = true;
2447
Sage Weil31b80062009-10-06 11:31:13 -07002448 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002449 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002450
Alex Elder38941f82012-06-01 14:56:43 -05002451 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002452 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002453 BUG_ON(msg->con == NULL);
2454
Sage Weil31b80062009-10-06 11:31:13 -07002455 BUG_ON(!list_empty(&msg->list_head));
2456 list_add_tail(&msg->list_head, &con->out_queue);
2457 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2458 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2459 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2460 le32_to_cpu(msg->hdr.front_len),
2461 le32_to_cpu(msg->hdr.middle_len),
2462 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002463 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002464
2465 /* if there wasn't anything waiting to send before, queue
2466 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002467 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002468 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002469 queue_con(con);
2470}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002471EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002472
2473/*
2474 * Revoke a message that was previously queued for send
2475 */
Alex Elder6740a842012-06-01 14:56:43 -05002476void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002477{
Alex Elder6740a842012-06-01 14:56:43 -05002478 struct ceph_connection *con = msg->con;
2479
2480 if (!con)
2481 return; /* Message not in our possession */
2482
Sage Weilec302642009-12-22 10:43:42 -08002483 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002484 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002485 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002486 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002487 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002488 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002489 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002490 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002491
Sage Weil31b80062009-10-06 11:31:13 -07002492 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002493 }
2494 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002495 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002496 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002497 if (con->out_kvec_is_msg) {
2498 con->out_skip = con->out_kvec_bytes;
2499 con->out_kvec_is_msg = false;
2500 }
Sage Weiled98ada2010-07-05 12:15:14 -07002501 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002502
2503 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002504 }
Sage Weilec302642009-12-22 10:43:42 -08002505 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002506}
2507
2508/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002509 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002510 */
Alex Elder8921d112012-06-01 14:56:43 -05002511void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002512{
Alex Elder8921d112012-06-01 14:56:43 -05002513 struct ceph_connection *con;
2514
2515 BUG_ON(msg == NULL);
2516 if (!msg->con) {
2517 dout("%s msg %p null con\n", __func__, msg);
2518
2519 return; /* Message not in our possession */
2520 }
2521
2522 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002523 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002524 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002525 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2526 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2527 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002528
2529 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002530 dout("%s %p msg %p revoked\n", __func__, con, msg);
2531 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002532 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002533 front_len -
2534 middle_len -
2535 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002536 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002537 ceph_msg_put(con->in_msg);
2538 con->in_msg = NULL;
2539 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002540 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002541 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002542 dout("%s %p in_msg %p msg %p no-op\n",
2543 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002544 }
2545 mutex_unlock(&con->mutex);
2546}
2547
2548/*
Sage Weil31b80062009-10-06 11:31:13 -07002549 * Queue a keepalive byte to ensure the tcp connection is alive.
2550 */
2551void ceph_con_keepalive(struct ceph_connection *con)
2552{
Sage Weile00de342011-03-04 12:25:05 -08002553 dout("con_keepalive %p\n", con);
2554 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002555 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2556 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002557 queue_con(con);
2558}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002559EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002560
2561
2562/*
2563 * construct a new message with given type, size
2564 * the new msg has a ref count of 1.
2565 */
Sage Weilb61c2762011-08-09 15:03:46 -07002566struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2567 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002568{
2569 struct ceph_msg *m;
2570
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002571 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002572 if (m == NULL)
2573 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002574 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002575
2576 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002577 INIT_LIST_HEAD(&m->list_head);
2578
Sage Weil45c6ceb2010-05-11 15:01:51 -07002579 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002580 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002581 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2582 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002583 m->hdr.front_len = cpu_to_le32(front_len);
2584 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002585 m->hdr.data_len = 0;
2586 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002587 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002588 m->footer.front_crc = 0;
2589 m->footer.middle_crc = 0;
2590 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002591 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002592 m->front_max = front_len;
2593 m->front_is_vmalloc = false;
2594 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002595 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002596 m->pool = NULL;
2597
Henry C Changca208922011-05-03 02:29:56 +00002598 /* middle */
2599 m->middle = NULL;
2600
2601 /* data */
2602 m->nr_pages = 0;
2603 m->page_alignment = 0;
2604 m->pages = NULL;
2605 m->pagelist = NULL;
2606 m->bio = NULL;
2607 m->bio_iter = NULL;
2608 m->bio_seg = 0;
2609 m->trail = NULL;
2610
Sage Weil31b80062009-10-06 11:31:13 -07002611 /* front */
2612 if (front_len) {
2613 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002614 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002615 PAGE_KERNEL);
2616 m->front_is_vmalloc = true;
2617 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002618 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002619 }
2620 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002621 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002622 front_len);
2623 goto out2;
2624 }
2625 } else {
2626 m->front.iov_base = NULL;
2627 }
2628 m->front.iov_len = front_len;
2629
Sage Weilbb257662010-04-01 16:07:23 -07002630 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002631 return m;
2632
2633out2:
2634 ceph_msg_put(m);
2635out:
Sage Weilb61c2762011-08-09 15:03:46 -07002636 if (!can_fail) {
2637 pr_err("msg_new can't create type %d front %d\n", type,
2638 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002639 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002640 } else {
2641 dout("msg_new can't create type %d front %d\n", type,
2642 front_len);
2643 }
Sage Weila79832f2010-04-01 16:06:19 -07002644 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002645}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002646EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002647
2648/*
Sage Weil31b80062009-10-06 11:31:13 -07002649 * Allocate "middle" portion of a message, if it is needed and wasn't
2650 * allocated by alloc_msg. This allows us to read a small fixed-size
2651 * per-type header in the front and then gracefully fail (i.e.,
2652 * propagate the error to the caller based on info in the front) when
2653 * the middle is too large.
2654 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002655static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002656{
2657 int type = le16_to_cpu(msg->hdr.type);
2658 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2659
2660 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2661 ceph_msg_type_name(type), middle_len);
2662 BUG_ON(!middle_len);
2663 BUG_ON(msg->middle);
2664
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002665 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002666 if (!msg->middle)
2667 return -ENOMEM;
2668 return 0;
2669}
2670
Yehuda Sadeh24504182010-01-08 13:58:34 -08002671/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002672 * Allocate a message for receiving an incoming message on a
2673 * connection, and save the result in con->in_msg. Uses the
2674 * connection's private alloc_msg op if available.
2675 *
2676 * Returns true if the message should be skipped, false otherwise.
2677 * If true is returned (skip message), con->in_msg will be NULL.
2678 * If false is returned, con->in_msg will contain a pointer to the
2679 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002680 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002681static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2682 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002683{
2684 int type = le16_to_cpu(hdr->type);
2685 int front_len = le32_to_cpu(hdr->front_len);
2686 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002687 int ret;
2688
Alex Elder1c20f2d2012-06-04 14:43:32 -05002689 BUG_ON(con->in_msg != NULL);
2690
Yehuda Sadeh24504182010-01-08 13:58:34 -08002691 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002692 int skip = 0;
2693
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002694 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002695 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002696 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002697 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002698 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002699 BUG_ON(con->in_msg->con == NULL);
2700 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002701 if (skip)
2702 con->in_msg = NULL;
2703
2704 if (!con->in_msg)
2705 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002706 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002707 if (!con->in_msg) {
2708 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2709 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002710 pr_err("unable to allocate msg type %d len %d\n",
2711 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002712 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002713 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002714 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002715 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002716 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002717 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002718 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002719
Alex Elder1c20f2d2012-06-04 14:43:32 -05002720 if (middle_len && !con->in_msg->middle) {
2721 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002722 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002723 ceph_msg_put(con->in_msg);
2724 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002725 }
2726 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002727
Alex Elder1c20f2d2012-06-04 14:43:32 -05002728 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002729}
2730
Sage Weil31b80062009-10-06 11:31:13 -07002731
2732/*
2733 * Free a generically kmalloc'd message.
2734 */
2735void ceph_msg_kfree(struct ceph_msg *m)
2736{
2737 dout("msg_kfree %p\n", m);
2738 if (m->front_is_vmalloc)
2739 vfree(m->front.iov_base);
2740 else
2741 kfree(m->front.iov_base);
2742 kfree(m);
2743}
2744
2745/*
2746 * Drop a msg ref. Destroy as needed.
2747 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002748void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002749{
Sage Weilc2e552e2009-12-07 15:55:05 -08002750 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002751
Sage Weilc2e552e2009-12-07 15:55:05 -08002752 dout("ceph_msg_put last one on %p\n", m);
2753 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002754
Sage Weilc2e552e2009-12-07 15:55:05 -08002755 /* drop middle, data, if any */
2756 if (m->middle) {
2757 ceph_buffer_put(m->middle);
2758 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002759 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002760 m->nr_pages = 0;
2761 m->pages = NULL;
2762
Sage Weil58bb3b32009-12-23 12:12:31 -08002763 if (m->pagelist) {
2764 ceph_pagelist_release(m->pagelist);
2765 kfree(m->pagelist);
2766 m->pagelist = NULL;
2767 }
2768
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002769 m->trail = NULL;
2770
Sage Weilc2e552e2009-12-07 15:55:05 -08002771 if (m->pool)
2772 ceph_msgpool_put(m->pool, m);
2773 else
2774 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002775}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002776EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002777
2778void ceph_msg_dump(struct ceph_msg *msg)
2779{
2780 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2781 msg->front_max, msg->nr_pages);
2782 print_hex_dump(KERN_DEBUG, "header: ",
2783 DUMP_PREFIX_OFFSET, 16, 1,
2784 &msg->hdr, sizeof(msg->hdr), true);
2785 print_hex_dump(KERN_DEBUG, " front: ",
2786 DUMP_PREFIX_OFFSET, 16, 1,
2787 msg->front.iov_base, msg->front.iov_len, true);
2788 if (msg->middle)
2789 print_hex_dump(KERN_DEBUG, "middle: ",
2790 DUMP_PREFIX_OFFSET, 16, 1,
2791 msg->middle->vec.iov_base,
2792 msg->middle->vec.iov_len, true);
2793 print_hex_dump(KERN_DEBUG, "footer: ",
2794 DUMP_PREFIX_OFFSET, 16, 1,
2795 &msg->footer, sizeof(msg->footer), true);
2796}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002797EXPORT_SYMBOL(ceph_msg_dump);