blob: 8a62a559a2aaade97cd9315c76ce63e9062391e6 [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>
Alex Elder3ebc21f2013-01-31 16:02:01 -060012#ifdef CONFIG_BLOCK
Yehuda Sadeh68b44762010-04-06 15:01:27 -070013#include <linux/bio.h>
Alex Elder3ebc21f2013-01-31 16:02:01 -060014#endif /* CONFIG_BLOCK */
Noah Watkinsee3b56f2011-09-23 11:48:42 -070015#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070016#include <net/tcp.h>
17
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070018#include <linux/ceph/libceph.h>
19#include <linux/ceph/messenger.h>
20#include <linux/ceph/decode.h>
21#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040022#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070023
24/*
25 * Ceph uses the messenger to exchange ceph_msg messages with other
26 * hosts in the system. The messenger provides ordered and reliable
27 * delivery. We tolerate TCP disconnects by reconnecting (with
28 * exponential backoff) in the case of a fault (disconnection, bad
29 * crc, protocol error). Acks allow sent messages to be discarded by
30 * the sender.
31 */
32
Alex Elderbc18f4b2012-06-20 21:53:53 -050033/*
34 * We track the state of the socket on a given connection using
35 * values defined below. The transition to a new socket state is
36 * handled by a function which verifies we aren't coming from an
37 * unexpected state.
38 *
39 * --------
40 * | NEW* | transient initial state
41 * --------
42 * | con_sock_state_init()
43 * v
44 * ----------
45 * | CLOSED | initialized, but no socket (and no
46 * ---------- TCP connection)
47 * ^ \
48 * | \ con_sock_state_connecting()
49 * | ----------------------
50 * | \
51 * + con_sock_state_closed() \
Sage Weilfbb85a42012-06-27 12:31:02 -070052 * |+--------------------------- \
53 * | \ \ \
54 * | ----------- \ \
55 * | | CLOSING | socket event; \ \
56 * | ----------- await close \ \
57 * | ^ \ |
58 * | | \ |
59 * | + con_sock_state_closing() \ |
60 * | / \ | |
61 * | / --------------- | |
62 * | / \ v v
Alex Elderbc18f4b2012-06-20 21:53:53 -050063 * | / --------------
64 * | / -----------------| CONNECTING | socket created, TCP
65 * | | / -------------- connect initiated
66 * | | | con_sock_state_connected()
67 * | | v
68 * -------------
69 * | CONNECTED | TCP connection established
70 * -------------
71 *
72 * State values for ceph_connection->sock_state; NEW is assumed to be 0.
73 */
Alex Elderce2c8902012-05-22 22:15:49 -050074
75#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
76#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
77#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
78#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
79#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
80
Sage Weil8dacc7d2012-07-20 17:24:40 -070081/*
82 * connection states
83 */
84#define CON_STATE_CLOSED 1 /* -> PREOPEN */
85#define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
86#define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
87#define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
88#define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
89#define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
90
Sage Weil4a861692012-07-20 17:29:55 -070091/*
92 * ceph_connection flag bits
93 */
94#define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
95 * messages on errors */
96#define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
97#define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
98#define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
99#define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
Sage Weil8dacc7d2012-07-20 17:24:40 -0700100
Sage Weil31b80062009-10-06 11:31:13 -0700101/* static tag bytes (protocol control messages) */
102static char tag_msg = CEPH_MSGR_TAG_MSG;
103static char tag_ack = CEPH_MSGR_TAG_ACK;
104static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
105
Sage Weila6a53492010-04-13 14:07:07 -0700106#ifdef CONFIG_LOCKDEP
107static struct lock_class_key socket_class;
108#endif
109
Alex Elder84495f42012-02-15 07:43:55 -0600110/*
111 * When skipping (ignoring) a block of input we read it into a "skip
112 * buffer," which is this many bytes in size.
113 */
114#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -0700115
116static void queue_con(struct ceph_connection *con);
117static void con_work(struct work_struct *);
118static void ceph_fault(struct ceph_connection *con);
119
Sage Weil31b80062009-10-06 11:31:13 -0700120/*
Alex Elderf64a9312012-01-23 15:49:27 -0600121 * Nicely render a sockaddr as a string. An array of formatted
122 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -0700123 */
Alex Elderf64a9312012-01-23 15:49:27 -0600124#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
125#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
126#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
127#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
128
129static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
130static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -0700131
Alex Elder57666512012-01-23 15:49:27 -0600132static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -0600133
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700134const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -0700135{
136 int i;
137 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -0600138 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
139 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -0700140
Alex Elderf64a9312012-01-23 15:49:27 -0600141 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700142 s = addr_str[i];
143
144 switch (ss->ss_family) {
145 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -0600146 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
147 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700148 break;
149
150 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -0600151 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
152 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700153 break;
154
155 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600156 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
157 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700158 }
159
160 return s;
161}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700162EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700163
Sage Weil63f2d212009-11-03 15:17:56 -0800164static void encode_my_addr(struct ceph_messenger *msgr)
165{
166 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
167 ceph_encode_addr(&msgr->my_enc_addr);
168}
169
Sage Weil31b80062009-10-06 11:31:13 -0700170/*
171 * work queue for all reading and writing to/from the socket.
172 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600173static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700174
Alex Elder6173d1f2012-02-14 14:05:33 -0600175void _ceph_msgr_exit(void)
176{
Alex Elderd3002b92012-02-14 14:05:33 -0600177 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600178 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600179 ceph_msgr_wq = NULL;
180 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600181
Alex Elder6173d1f2012-02-14 14:05:33 -0600182 BUG_ON(zero_page == NULL);
183 kunmap(zero_page);
184 page_cache_release(zero_page);
185 zero_page = NULL;
186}
187
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700188int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700189{
Alex Elder57666512012-01-23 15:49:27 -0600190 BUG_ON(zero_page != NULL);
191 zero_page = ZERO_PAGE(0);
192 page_cache_get(zero_page);
193
Tejun Heof363e452011-01-03 14:49:46 +0100194 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600195 if (ceph_msgr_wq)
196 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600197
Alex Elder6173d1f2012-02-14 14:05:33 -0600198 pr_err("msgr_init failed to create workqueue\n");
199 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600200
Alex Elder6173d1f2012-02-14 14:05:33 -0600201 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700202}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700203EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700204
205void ceph_msgr_exit(void)
206{
Alex Elder57666512012-01-23 15:49:27 -0600207 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600208
Alex Elder6173d1f2012-02-14 14:05:33 -0600209 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700210}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700211EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700212
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700213void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700214{
215 flush_workqueue(ceph_msgr_wq);
216}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700217EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700218
Alex Elderce2c8902012-05-22 22:15:49 -0500219/* Connection socket state transition functions */
220
221static void con_sock_state_init(struct ceph_connection *con)
222{
223 int old_state;
224
225 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
226 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
227 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700228 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
229 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500230}
231
232static void con_sock_state_connecting(struct ceph_connection *con)
233{
234 int old_state;
235
236 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
237 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
238 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700239 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
240 CON_SOCK_STATE_CONNECTING);
Alex Elderce2c8902012-05-22 22:15:49 -0500241}
242
243static void con_sock_state_connected(struct ceph_connection *con)
244{
245 int old_state;
246
247 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
248 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
249 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700250 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
251 CON_SOCK_STATE_CONNECTED);
Alex Elderce2c8902012-05-22 22:15:49 -0500252}
253
254static void con_sock_state_closing(struct ceph_connection *con)
255{
256 int old_state;
257
258 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
259 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
260 old_state != CON_SOCK_STATE_CONNECTED &&
261 old_state != CON_SOCK_STATE_CLOSING))
262 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700263 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
264 CON_SOCK_STATE_CLOSING);
Alex Elderce2c8902012-05-22 22:15:49 -0500265}
266
267static void con_sock_state_closed(struct ceph_connection *con)
268{
269 int old_state;
270
271 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
272 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
Sage Weilfbb85a42012-06-27 12:31:02 -0700273 old_state != CON_SOCK_STATE_CLOSING &&
Sage Weil8007b8d2012-07-30 18:16:16 -0700274 old_state != CON_SOCK_STATE_CONNECTING &&
275 old_state != CON_SOCK_STATE_CLOSED))
Alex Elderce2c8902012-05-22 22:15:49 -0500276 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700277 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
278 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500279}
Sage Weila922d382010-05-29 09:41:23 -0700280
Sage Weil31b80062009-10-06 11:31:13 -0700281/*
282 * socket callback functions
283 */
284
285/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500286static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700287{
Alex Elderbd406142012-01-23 15:49:27 -0600288 struct ceph_connection *con = sk->sk_user_data;
Guanjun Hea2a32582012-07-08 19:50:33 -0700289 if (atomic_read(&con->msgr->stopping)) {
290 return;
291 }
Alex Elderbd406142012-01-23 15:49:27 -0600292
Sage Weil31b80062009-10-06 11:31:13 -0700293 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500294 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700295 con, con->state);
296 queue_con(con);
297 }
298}
299
300/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500301static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700302{
Alex Elderd3002b92012-02-14 14:05:33 -0600303 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700304
Jim Schutt182fac22012-02-29 08:30:58 -0700305 /* only queue to workqueue if there is data we want to write,
306 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500307 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700308 * doesn't get called again until try_write() fills the socket
309 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
310 * and net/core/stream.c:sk_stream_write_space().
311 */
Sage Weil4a861692012-07-20 17:29:55 -0700312 if (test_bit(CON_FLAG_WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700313 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500314 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700315 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
316 queue_con(con);
317 }
Sage Weil31b80062009-10-06 11:31:13 -0700318 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500319 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700320 }
Sage Weil31b80062009-10-06 11:31:13 -0700321}
322
323/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500324static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700325{
Alex Elderbd406142012-01-23 15:49:27 -0600326 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700327
Alex Elder327800b2012-05-22 11:41:43 -0500328 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700329 con, con->state, sk->sk_state);
330
Sage Weil31b80062009-10-06 11:31:13 -0700331 switch (sk->sk_state) {
332 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500333 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700334 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500335 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500336 con_sock_state_closing(con);
Sage Weil4a861692012-07-20 17:29:55 -0700337 set_bit(CON_FLAG_SOCK_CLOSED, &con->flags);
Alex Elderd65c9e02012-06-20 21:53:53 -0500338 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700339 break;
340 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500341 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500342 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700343 queue_con(con);
344 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600345 default: /* Everything else is uninteresting */
346 break;
Sage Weil31b80062009-10-06 11:31:13 -0700347 }
348}
349
350/*
351 * set up socket callbacks
352 */
353static void set_sock_callbacks(struct socket *sock,
354 struct ceph_connection *con)
355{
356 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600357 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500358 sk->sk_data_ready = ceph_sock_data_ready;
359 sk->sk_write_space = ceph_sock_write_space;
360 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700361}
362
363
364/*
365 * socket helpers
366 */
367
368/*
369 * initiate connection to a remote socket.
370 */
Alex Elder41617d02012-02-14 14:05:33 -0600371static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700372{
Sage Weilf91d3472010-07-01 15:18:31 -0700373 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700374 struct socket *sock;
375 int ret;
376
377 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700378 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
379 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700380 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600381 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700382 sock->sk->sk_allocation = GFP_NOFS;
383
Sage Weila6a53492010-04-13 14:07:07 -0700384#ifdef CONFIG_LOCKDEP
385 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
386#endif
387
Sage Weil31b80062009-10-06 11:31:13 -0700388 set_sock_callbacks(sock, con);
389
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700390 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700391
Sage Weil89a86be2012-06-09 14:19:21 -0700392 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700393 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
394 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700395 if (ret == -EINPROGRESS) {
396 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700397 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700398 sock->sk->sk_state);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600399 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700400 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700401 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700402 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700403 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700404
Alex Elder41617d02012-02-14 14:05:33 -0600405 return ret;
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600406 }
407 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600408 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700409}
410
411static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
412{
413 struct kvec iov = {buf, len};
414 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800415 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700416
Sage Weil98bdb0a2011-01-25 08:17:48 -0800417 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
418 if (r == -EAGAIN)
419 r = 0;
420 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700421}
422
423/*
424 * write something. @more is true if caller will be sending more data
425 * shortly.
426 */
427static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
428 size_t kvlen, size_t len, int more)
429{
430 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800431 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700432
433 if (more)
434 msg.msg_flags |= MSG_MORE;
435 else
436 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
437
Sage Weil42961d22011-01-25 08:19:34 -0800438 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
439 if (r == -EAGAIN)
440 r = 0;
441 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700442}
443
Alex Elder31739132012-03-07 11:40:08 -0600444static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
445 int offset, size_t size, int more)
446{
447 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
448 int ret;
449
450 ret = kernel_sendpage(sock, page, offset, size, flags);
451 if (ret == -EAGAIN)
452 ret = 0;
453
454 return ret;
455}
456
Sage Weil31b80062009-10-06 11:31:13 -0700457
458/*
459 * Shutdown/close the socket for the given connection.
460 */
461static int con_close_socket(struct ceph_connection *con)
462{
Sage Weil8007b8d2012-07-30 18:16:16 -0700463 int rc = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700464
465 dout("con_close_socket on %p sock %p\n", con, con->sock);
Sage Weil8007b8d2012-07-30 18:16:16 -0700466 if (con->sock) {
467 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
468 sock_release(con->sock);
469 con->sock = NULL;
470 }
Alex Elder456ea462012-06-20 21:53:53 -0500471
472 /*
Sage Weil4a861692012-07-20 17:29:55 -0700473 * Forcibly clear the SOCK_CLOSED flag. It gets set
Alex Elder456ea462012-06-20 21:53:53 -0500474 * independent of the connection mutex, and we could have
475 * received a socket close event before we had the chance to
476 * shut the socket down.
477 */
Sage Weil4a861692012-07-20 17:29:55 -0700478 clear_bit(CON_FLAG_SOCK_CLOSED, &con->flags);
Sage Weil8007b8d2012-07-30 18:16:16 -0700479
Alex Elderce2c8902012-05-22 22:15:49 -0500480 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700481 return rc;
482}
483
484/*
485 * Reset a connection. Discard all incoming and outgoing messages
486 * and clear *_seq state.
487 */
488static void ceph_msg_remove(struct ceph_msg *msg)
489{
490 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500491 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -0700492 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500493 msg->con = NULL;
494
Sage Weil31b80062009-10-06 11:31:13 -0700495 ceph_msg_put(msg);
496}
497static void ceph_msg_remove_list(struct list_head *head)
498{
499 while (!list_empty(head)) {
500 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
501 list_head);
502 ceph_msg_remove(msg);
503 }
504}
505
506static void reset_connection(struct ceph_connection *con)
507{
508 /* reset connection, out_queue, msg_ and connect_seq */
509 /* discard existing out_queue and msg_seq */
Sage Weil0fa6ebc2012-12-27 20:27:04 -0600510 dout("reset_connection %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -0700511 ceph_msg_remove_list(&con->out_queue);
512 ceph_msg_remove_list(&con->out_sent);
513
Sage Weilcf3e5c42009-12-11 09:48:05 -0800514 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500515 BUG_ON(con->in_msg->con != con);
516 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800517 ceph_msg_put(con->in_msg);
518 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -0700519 con->ops->put(con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800520 }
521
Sage Weil31b80062009-10-06 11:31:13 -0700522 con->connect_seq = 0;
523 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800524 if (con->out_msg) {
525 ceph_msg_put(con->out_msg);
526 con->out_msg = NULL;
527 }
Sage Weil31b80062009-10-06 11:31:13 -0700528 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700529 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700530}
531
532/*
533 * mark a peer down. drop any open connections.
534 */
535void ceph_con_close(struct ceph_connection *con)
536{
Sage Weil8c50c812012-07-30 16:24:37 -0700537 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700538 dout("con_close %p peer %s\n", con,
539 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700540 con->state = CON_STATE_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500541
Sage Weil4a861692012-07-20 17:29:55 -0700542 clear_bit(CON_FLAG_LOSSYTX, &con->flags); /* so we retry next connect */
543 clear_bit(CON_FLAG_KEEPALIVE_PENDING, &con->flags);
544 clear_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil43c74272012-07-20 17:30:40 -0700545 clear_bit(CON_FLAG_KEEPALIVE_PENDING, &con->flags);
546 clear_bit(CON_FLAG_BACKOFF, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500547
Sage Weil31b80062009-10-06 11:31:13 -0700548 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700549 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800550 cancel_delayed_work(&con->work);
Sage Weilee76e072012-07-20 16:45:49 -0700551 con_close_socket(con);
Sage Weilec302642009-12-22 10:43:42 -0800552 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700553}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700554EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700555
556/*
Sage Weil31b80062009-10-06 11:31:13 -0700557 * Reopen a closed connection, with a new peer address.
558 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700559void ceph_con_open(struct ceph_connection *con,
560 __u8 entity_type, __u64 entity_num,
561 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700562{
Sage Weil54691552012-07-30 16:21:40 -0700563 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700564 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700565
Alex Elder122070a2012-12-26 10:43:57 -0600566 WARN_ON(con->state != CON_STATE_CLOSED);
Sage Weil8dacc7d2012-07-20 17:24:40 -0700567 con->state = CON_STATE_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500568
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700569 con->peer_name.type = (__u8) entity_type;
570 con->peer_name.num = cpu_to_le64(entity_num);
571
Sage Weil31b80062009-10-06 11:31:13 -0700572 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800573 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700574 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700575 queue_con(con);
576}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700577EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700578
579/*
Sage Weil87b315a2010-03-22 14:51:18 -0700580 * return true if this connection ever successfully opened
581 */
582bool ceph_con_opened(struct ceph_connection *con)
583{
584 return con->connect_seq > 0;
585}
586
587/*
Sage Weil31b80062009-10-06 11:31:13 -0700588 * initialize a new connection.
589 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500590void ceph_con_init(struct ceph_connection *con, void *private,
591 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700592 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700593{
594 dout("con_init %p\n", con);
595 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500596 con->private = private;
597 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700598 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500599
600 con_sock_state_init(con);
601
Sage Weilec302642009-12-22 10:43:42 -0800602 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700603 INIT_LIST_HEAD(&con->out_queue);
604 INIT_LIST_HEAD(&con->out_sent);
605 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500606
Sage Weil8dacc7d2012-07-20 17:24:40 -0700607 con->state = CON_STATE_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700608}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700609EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700610
611
612/*
613 * We maintain a global counter to order connection attempts. Get
614 * a unique seq greater than @gt.
615 */
616static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
617{
618 u32 ret;
619
620 spin_lock(&msgr->global_seq_lock);
621 if (msgr->global_seq < gt)
622 msgr->global_seq = gt;
623 ret = ++msgr->global_seq;
624 spin_unlock(&msgr->global_seq_lock);
625 return ret;
626}
627
Alex Eldere2200422012-05-23 14:35:23 -0500628static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600629{
630 con->out_kvec_left = 0;
631 con->out_kvec_bytes = 0;
632 con->out_kvec_cur = &con->out_kvec[0];
633}
634
Alex Eldere2200422012-05-23 14:35:23 -0500635static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600636 size_t size, void *data)
637{
638 int index;
639
640 index = con->out_kvec_left;
641 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
642
643 con->out_kvec[index].iov_len = size;
644 con->out_kvec[index].iov_base = data;
645 con->out_kvec_left++;
646 con->out_kvec_bytes += size;
647}
Sage Weil31b80062009-10-06 11:31:13 -0700648
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500649#ifdef CONFIG_BLOCK
650static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
651{
652 if (!bio) {
653 *iter = NULL;
654 *seg = 0;
655 return;
656 }
657 *iter = bio;
658 *seg = bio->bi_idx;
659}
660
661static void iter_bio_next(struct bio **bio_iter, int *seg)
662{
663 if (*bio_iter == NULL)
664 return;
665
666 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
667
668 (*seg)++;
669 if (*seg == (*bio_iter)->bi_vcnt)
670 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
671}
672#endif
673
Alex Elder739c9052012-06-11 14:57:13 -0500674static void prepare_write_message_data(struct ceph_connection *con)
675{
676 struct ceph_msg *msg = con->out_msg;
677
678 BUG_ON(!msg);
679 BUG_ON(!msg->hdr.data_len);
680
681 /* initialize page iterator */
682 con->out_msg_pos.page = 0;
683 if (msg->pages)
684 con->out_msg_pos.page_pos = msg->page_alignment;
685 else
686 con->out_msg_pos.page_pos = 0;
Alex Elder572c5882012-06-11 14:57:13 -0500687#ifdef CONFIG_BLOCK
Alex Elderabdaa6a2012-06-11 14:57:13 -0500688 if (msg->bio)
Alex Elder572c5882012-06-11 14:57:13 -0500689 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
690#endif
Alex Elder739c9052012-06-11 14:57:13 -0500691 con->out_msg_pos.data_pos = 0;
692 con->out_msg_pos.did_page_crc = false;
693 con->out_more = 1; /* data + footer will follow */
694}
695
Sage Weil31b80062009-10-06 11:31:13 -0700696/*
697 * Prepare footer for currently outgoing message, and finish things
698 * off. Assumes out_kvec* are already valid.. we just add on to the end.
699 */
Alex Elder859eb792012-02-14 14:05:33 -0600700static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700701{
702 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600703 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700704
Alex Elderfd154f32012-06-11 14:57:13 -0500705 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
706
Sage Weil31b80062009-10-06 11:31:13 -0700707 dout("prepare_write_message_footer %p\n", con);
708 con->out_kvec_is_msg = true;
709 con->out_kvec[v].iov_base = &m->footer;
710 con->out_kvec[v].iov_len = sizeof(m->footer);
711 con->out_kvec_bytes += sizeof(m->footer);
712 con->out_kvec_left++;
713 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800714 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700715}
716
717/*
718 * Prepare headers for the next outgoing message.
719 */
720static void prepare_write_message(struct ceph_connection *con)
721{
722 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600723 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700724
Alex Eldere2200422012-05-23 14:35:23 -0500725 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700726 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800727 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700728
729 /* Sneak an ack in there first? If we can get it into the same
730 * TCP packet that's a good thing. */
731 if (con->in_seq > con->in_seq_acked) {
732 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500733 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700734 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500735 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600736 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700737 }
738
Alex Elder38941f82012-06-01 14:56:43 -0500739 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600740 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800741 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500742 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700743
744 /* put message on sent list */
745 ceph_msg_get(m);
746 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700747
Sage Weile84346b2010-05-11 21:20:38 -0700748 /*
749 * only assign outgoing seq # if we haven't sent this message
750 * yet. if it is requeued, resend with it's original seq.
751 */
752 if (m->needs_out_seq) {
753 m->hdr.seq = cpu_to_le64(++con->out_seq);
754 m->needs_out_seq = false;
755 }
Yan, Zhengb132cf42012-06-06 19:35:55 -0500756#ifdef CONFIG_BLOCK
757 else
758 m->bio_iter = NULL;
759#endif
Sage Weil31b80062009-10-06 11:31:13 -0700760
761 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
762 m, con->out_seq, le16_to_cpu(m->hdr.type),
763 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
764 le32_to_cpu(m->hdr.data_len),
765 m->nr_pages);
766 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
767
768 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500769 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
770 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
771 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600772
Sage Weil31b80062009-10-06 11:31:13 -0700773 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500774 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600775 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700776
777 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600778 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
779 con->out_msg->hdr.crc = cpu_to_le32(crc);
Alex Elderfd154f32012-06-11 14:57:13 -0500780 con->out_msg->footer.flags = 0;
Alex Eldera9a0c512012-02-15 07:43:54 -0600781
782 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
783 con->out_msg->footer.front_crc = cpu_to_le32(crc);
784 if (m->middle) {
785 crc = crc32c(0, m->middle->vec.iov_base,
786 m->middle->vec.iov_len);
787 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
788 } else
Sage Weil31b80062009-10-06 11:31:13 -0700789 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -0500790 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700791 le32_to_cpu(con->out_msg->footer.front_crc),
792 le32_to_cpu(con->out_msg->footer.middle_crc));
793
794 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -0500795 con->out_msg->footer.data_crc = 0;
796 if (m->hdr.data_len)
797 prepare_write_message_data(con);
798 else
Sage Weil31b80062009-10-06 11:31:13 -0700799 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600800 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700801
Sage Weil4a861692012-07-20 17:29:55 -0700802 set_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700803}
804
805/*
806 * Prepare an ack.
807 */
808static void prepare_write_ack(struct ceph_connection *con)
809{
810 dout("prepare_write_ack %p %llu -> %llu\n", con,
811 con->in_seq_acked, con->in_seq);
812 con->in_seq_acked = con->in_seq;
813
Alex Eldere2200422012-05-23 14:35:23 -0500814 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600815
Alex Eldere2200422012-05-23 14:35:23 -0500816 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600817
Sage Weil31b80062009-10-06 11:31:13 -0700818 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500819 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600820 &con->out_temp_ack);
821
Sage Weil31b80062009-10-06 11:31:13 -0700822 con->out_more = 1; /* more will follow.. eventually.. */
Sage Weil4a861692012-07-20 17:29:55 -0700823 set_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700824}
825
826/*
827 * Prepare to write keepalive byte.
828 */
829static void prepare_write_keepalive(struct ceph_connection *con)
830{
831 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500832 con_out_kvec_reset(con);
833 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Sage Weil4a861692012-07-20 17:29:55 -0700834 set_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700835}
836
837/*
838 * Connection negotiation.
839 */
840
Alex Elderdac1e712012-05-16 15:16:39 -0500841static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
842 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800843{
Alex Eldera3530df2012-05-16 15:16:39 -0500844 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500845
846 if (!con->ops->get_authorizer) {
847 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
848 con->out_connect.authorizer_len = 0;
Alex Elder729796b2012-05-16 15:16:39 -0500849 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500850 }
851
852 /* Can't hold the mutex while getting authorizer */
Sage Weilec302642009-12-22 10:43:42 -0800853 mutex_unlock(&con->mutex);
Alex Elderdac1e712012-05-16 15:16:39 -0500854 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800855 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800856
Alex Eldera3530df2012-05-16 15:16:39 -0500857 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500858 return auth;
Sage Weil8dacc7d2012-07-20 17:24:40 -0700859 if (con->state != CON_STATE_NEGOTIATING)
Alex Elder729796b2012-05-16 15:16:39 -0500860 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700861
Alex Elder8f43fb52012-05-16 15:16:39 -0500862 con->auth_reply_buf = auth->authorizer_reply_buf;
863 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
Alex Elder729796b2012-05-16 15:16:39 -0500864 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800865}
866
Sage Weil31b80062009-10-06 11:31:13 -0700867/*
868 * We connected to a peer and are saying hello.
869 */
Alex Eldere825a662012-05-16 15:16:38 -0500870static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700871{
Alex Eldere2200422012-05-23 14:35:23 -0500872 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
873 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500874 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800875
Sage Weileed0ef22009-11-10 14:34:36 -0800876 con->out_more = 0;
Sage Weil4a861692012-07-20 17:29:55 -0700877 set_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800878}
879
Alex Eldere825a662012-05-16 15:16:38 -0500880static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800881{
Eric Dumazet95c96172012-04-15 05:58:06 +0000882 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700883 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500884 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500885 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700886
887 switch (con->peer_name.type) {
888 case CEPH_ENTITY_TYPE_MON:
889 proto = CEPH_MONC_PROTOCOL;
890 break;
891 case CEPH_ENTITY_TYPE_OSD:
892 proto = CEPH_OSDC_PROTOCOL;
893 break;
894 case CEPH_ENTITY_TYPE_MDS:
895 proto = CEPH_MDSC_PROTOCOL;
896 break;
897 default:
898 BUG();
899 }
900
901 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
902 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800903
Alex Eldere825a662012-05-16 15:16:38 -0500904 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700905 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
906 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
907 con->out_connect.global_seq = cpu_to_le32(global_seq);
908 con->out_connect.protocol_version = cpu_to_le32(proto);
909 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700910
Alex Elderdac1e712012-05-16 15:16:39 -0500911 auth_proto = CEPH_AUTH_UNKNOWN;
912 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500913 if (IS_ERR(auth))
914 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500915
Alex Elderdac1e712012-05-16 15:16:39 -0500916 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500917 con->out_connect.authorizer_len = auth ?
918 cpu_to_le32(auth->authorizer_buf_len) : 0;
919
Alex Eldere2200422012-05-23 14:35:23 -0500920 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500921 &con->out_connect);
922 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500923 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500924 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600925
Sage Weil31b80062009-10-06 11:31:13 -0700926 con->out_more = 0;
Sage Weil4a861692012-07-20 17:29:55 -0700927 set_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800928
Alex Eldere10c7582012-05-16 15:16:38 -0500929 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700930}
931
Sage Weil31b80062009-10-06 11:31:13 -0700932/*
933 * write as much of pending kvecs to the socket as we can.
934 * 1 -> done
935 * 0 -> socket full, but more to do
936 * <0 -> error
937 */
938static int write_partial_kvec(struct ceph_connection *con)
939{
940 int ret;
941
942 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
943 while (con->out_kvec_bytes > 0) {
944 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
945 con->out_kvec_left, con->out_kvec_bytes,
946 con->out_more);
947 if (ret <= 0)
948 goto out;
949 con->out_kvec_bytes -= ret;
950 if (con->out_kvec_bytes == 0)
951 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600952
953 /* account for full iov entries consumed */
954 while (ret >= con->out_kvec_cur->iov_len) {
955 BUG_ON(!con->out_kvec_left);
956 ret -= con->out_kvec_cur->iov_len;
957 con->out_kvec_cur++;
958 con->out_kvec_left--;
959 }
960 /* and for a partially-consumed entry */
961 if (ret) {
962 con->out_kvec_cur->iov_len -= ret;
963 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700964 }
965 }
966 con->out_kvec_left = 0;
967 con->out_kvec_is_msg = false;
968 ret = 1;
969out:
970 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
971 con->out_kvec_bytes, con->out_kvec_left, ret);
972 return ret; /* done! */
973}
974
Alex Elder84ca8fc2012-06-11 14:57:13 -0500975static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
976 size_t len, size_t sent, bool in_trail)
977{
978 struct ceph_msg *msg = con->out_msg;
979
980 BUG_ON(!msg);
981 BUG_ON(!sent);
982
983 con->out_msg_pos.data_pos += sent;
984 con->out_msg_pos.page_pos += sent;
Alex Elder5821bd82012-06-11 14:57:13 -0500985 if (sent < len)
986 return;
987
988 BUG_ON(sent != len);
989 con->out_msg_pos.page_pos = 0;
990 con->out_msg_pos.page++;
991 con->out_msg_pos.did_page_crc = false;
992 if (in_trail)
993 list_move_tail(&page->lru,
994 &msg->trail->head);
995 else if (msg->pagelist)
996 list_move_tail(&page->lru,
997 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700998#ifdef CONFIG_BLOCK
Alex Elder5821bd82012-06-11 14:57:13 -0500999 else if (msg->bio)
1000 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001001#endif
Alex Elder84ca8fc2012-06-11 14:57:13 -05001002}
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001003
Sage Weil31b80062009-10-06 11:31:13 -07001004/*
1005 * Write as much message data payload as we can. If we finish, queue
1006 * up the footer.
1007 * 1 -> done, footer is now queued in out_kvec[].
1008 * 0 -> socket full, but more to do
1009 * <0 -> error
1010 */
1011static int write_partial_msg_pages(struct ceph_connection *con)
1012{
1013 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +00001014 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -07001015 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -06001016 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07001017 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001018 int total_max_write;
Alex Elder84ca8fc2012-06-11 14:57:13 -05001019 bool in_trail = false;
Alex Elder5821bd82012-06-11 14:57:13 -05001020 const size_t trail_len = (msg->trail ? msg->trail->length : 0);
1021 const size_t trail_off = data_len - trail_len;
Sage Weil31b80062009-10-06 11:31:13 -07001022
1023 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
Alex Elder84ca8fc2012-06-11 14:57:13 -05001024 con, msg, con->out_msg_pos.page, msg->nr_pages,
Sage Weil31b80062009-10-06 11:31:13 -07001025 con->out_msg_pos.page_pos);
1026
Alex Elder5821bd82012-06-11 14:57:13 -05001027 /*
1028 * Iterate through each page that contains data to be
1029 * written, and send as much as possible for each.
1030 *
1031 * If we are calculating the data crc (the default), we will
1032 * need to map the page. If we have no pages, they have
1033 * been revoked, so use the zero page.
1034 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001035 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -07001036 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001037 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -06001038 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001039
Alex Elder5821bd82012-06-11 14:57:13 -05001040 in_trail = in_trail || con->out_msg_pos.data_pos >= trail_off;
1041 if (!in_trail)
1042 total_max_write = trail_off - con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001043
Alex Elder5821bd82012-06-11 14:57:13 -05001044 if (in_trail) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001045 total_max_write = data_len - con->out_msg_pos.data_pos;
1046
1047 page = list_first_entry(&msg->trail->head,
1048 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001049 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -07001050 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -08001051 } else if (msg->pagelist) {
1052 page = list_first_entry(&msg->pagelist->head,
1053 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001054#ifdef CONFIG_BLOCK
1055 } else if (msg->bio) {
1056 struct bio_vec *bv;
1057
1058 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
1059 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -06001060 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001061 max_write = bv->bv_len;
1062#endif
Sage Weil31b80062009-10-06 11:31:13 -07001063 } else {
Alex Elder57666512012-01-23 15:49:27 -06001064 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -07001065 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001066 len = min_t(int, max_write - con->out_msg_pos.page_pos,
1067 total_max_write);
1068
Alex Elder37675b02012-03-07 11:40:08 -06001069 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -06001070 void *base;
Alex Elder5821bd82012-06-11 14:57:13 -05001071 u32 crc = le32_to_cpu(msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -06001072 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -07001073
Alex Elder8d63e312012-03-07 11:40:08 -06001074 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -07001075 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -06001076 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Elder5821bd82012-06-11 14:57:13 -05001077 crc = crc32c(crc, base, len);
Alex Elder5ce765a2012-09-21 17:59:58 -05001078 kunmap(page);
Alex Elder84ca8fc2012-06-11 14:57:13 -05001079 msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -06001080 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -07001081 }
Alex Eldere36b13c2012-03-07 11:40:08 -06001082 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -06001083 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -06001084 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001085 if (ret <= 0)
1086 goto out;
1087
Alex Elder84ca8fc2012-06-11 14:57:13 -05001088 out_msg_pos_next(con, page, len, (size_t) ret, in_trail);
Sage Weil31b80062009-10-06 11:31:13 -07001089 }
1090
1091 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1092
1093 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001094 if (!do_datacrc)
Alex Elder84ca8fc2012-06-11 14:57:13 -05001095 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001096 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001097 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001098 ret = 1;
1099out:
1100 return ret;
1101}
1102
1103/*
1104 * write some zeros
1105 */
1106static int write_partial_skip(struct ceph_connection *con)
1107{
1108 int ret;
1109
1110 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001111 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001112
Alex Elder31739132012-03-07 11:40:08 -06001113 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001114 if (ret <= 0)
1115 goto out;
1116 con->out_skip -= ret;
1117 }
1118 ret = 1;
1119out:
1120 return ret;
1121}
1122
1123/*
1124 * Prepare to read connection handshake, or an ack.
1125 */
Sage Weileed0ef22009-11-10 14:34:36 -08001126static void prepare_read_banner(struct ceph_connection *con)
1127{
1128 dout("prepare_read_banner %p\n", con);
1129 con->in_base_pos = 0;
1130}
1131
Sage Weil31b80062009-10-06 11:31:13 -07001132static void prepare_read_connect(struct ceph_connection *con)
1133{
1134 dout("prepare_read_connect %p\n", con);
1135 con->in_base_pos = 0;
1136}
1137
1138static void prepare_read_ack(struct ceph_connection *con)
1139{
1140 dout("prepare_read_ack %p\n", con);
1141 con->in_base_pos = 0;
1142}
1143
1144static void prepare_read_tag(struct ceph_connection *con)
1145{
1146 dout("prepare_read_tag %p\n", con);
1147 con->in_base_pos = 0;
1148 con->in_tag = CEPH_MSGR_TAG_READY;
1149}
1150
1151/*
1152 * Prepare to read a message.
1153 */
1154static int prepare_read_message(struct ceph_connection *con)
1155{
1156 dout("prepare_read_message %p\n", con);
1157 BUG_ON(con->in_msg != NULL);
1158 con->in_base_pos = 0;
1159 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1160 return 0;
1161}
1162
1163
1164static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001165 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001166{
Alex Eldere6cee712012-05-10 10:29:50 -05001167 while (con->in_base_pos < end) {
1168 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001169 int have = size - left;
1170 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1171 if (ret <= 0)
1172 return ret;
1173 con->in_base_pos += ret;
1174 }
1175 return 1;
1176}
1177
1178
1179/*
1180 * Read all or part of the connect-side handshake on a new connection
1181 */
Sage Weileed0ef22009-11-10 14:34:36 -08001182static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001183{
Alex Elderfd516532012-05-10 10:29:50 -05001184 int size;
1185 int end;
1186 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001187
Sage Weileed0ef22009-11-10 14:34:36 -08001188 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001189
1190 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001191 size = strlen(CEPH_BANNER);
1192 end = size;
1193 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001194 if (ret <= 0)
1195 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001196
1197 size = sizeof (con->actual_peer_addr);
1198 end += size;
1199 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001200 if (ret <= 0)
1201 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001202
1203 size = sizeof (con->peer_addr_for_me);
1204 end += size;
1205 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001206 if (ret <= 0)
1207 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001208
Sage Weileed0ef22009-11-10 14:34:36 -08001209out:
1210 return ret;
1211}
1212
1213static int read_partial_connect(struct ceph_connection *con)
1214{
Alex Elderfd516532012-05-10 10:29:50 -05001215 int size;
1216 int end;
1217 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001218
1219 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1220
Alex Elderfd516532012-05-10 10:29:50 -05001221 size = sizeof (con->in_reply);
1222 end = size;
1223 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001224 if (ret <= 0)
1225 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001226
1227 size = le32_to_cpu(con->in_reply.authorizer_len);
1228 end += size;
1229 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001230 if (ret <= 0)
1231 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001232
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001233 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1234 con, (int)con->in_reply.tag,
1235 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001236 le32_to_cpu(con->in_reply.global_seq));
1237out:
1238 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001239
Sage Weil31b80062009-10-06 11:31:13 -07001240}
1241
1242/*
1243 * Verify the hello banner looks okay.
1244 */
1245static int verify_hello(struct ceph_connection *con)
1246{
1247 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001248 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001249 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001250 con->error_msg = "protocol error, bad banner";
1251 return -1;
1252 }
1253 return 0;
1254}
1255
1256static bool addr_is_blank(struct sockaddr_storage *ss)
1257{
1258 switch (ss->ss_family) {
1259 case AF_INET:
1260 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1261 case AF_INET6:
1262 return
1263 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1264 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1265 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1266 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1267 }
1268 return false;
1269}
1270
1271static int addr_port(struct sockaddr_storage *ss)
1272{
1273 switch (ss->ss_family) {
1274 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001275 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001276 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001277 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001278 }
1279 return 0;
1280}
1281
1282static void addr_set_port(struct sockaddr_storage *ss, int p)
1283{
1284 switch (ss->ss_family) {
1285 case AF_INET:
1286 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001287 break;
Sage Weil31b80062009-10-06 11:31:13 -07001288 case AF_INET6:
1289 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001290 break;
Sage Weil31b80062009-10-06 11:31:13 -07001291 }
1292}
1293
1294/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001295 * Unlike other *_pton function semantics, zero indicates success.
1296 */
1297static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1298 char delim, const char **ipend)
1299{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001300 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1301 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001302
1303 memset(ss, 0, sizeof(*ss));
1304
1305 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1306 ss->ss_family = AF_INET;
1307 return 0;
1308 }
1309
1310 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1311 ss->ss_family = AF_INET6;
1312 return 0;
1313 }
1314
1315 return -EINVAL;
1316}
1317
1318/*
1319 * Extract hostname string and resolve using kernel DNS facility.
1320 */
1321#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1322static int ceph_dns_resolve_name(const char *name, size_t namelen,
1323 struct sockaddr_storage *ss, char delim, const char **ipend)
1324{
1325 const char *end, *delim_p;
1326 char *colon_p, *ip_addr = NULL;
1327 int ip_len, ret;
1328
1329 /*
1330 * The end of the hostname occurs immediately preceding the delimiter or
1331 * the port marker (':') where the delimiter takes precedence.
1332 */
1333 delim_p = memchr(name, delim, namelen);
1334 colon_p = memchr(name, ':', namelen);
1335
1336 if (delim_p && colon_p)
1337 end = delim_p < colon_p ? delim_p : colon_p;
1338 else if (!delim_p && colon_p)
1339 end = colon_p;
1340 else {
1341 end = delim_p;
1342 if (!end) /* case: hostname:/ */
1343 end = name + namelen;
1344 }
1345
1346 if (end <= name)
1347 return -EINVAL;
1348
1349 /* do dns_resolve upcall */
1350 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1351 if (ip_len > 0)
1352 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1353 else
1354 ret = -ESRCH;
1355
1356 kfree(ip_addr);
1357
1358 *ipend = end;
1359
1360 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1361 ret, ret ? "failed" : ceph_pr_addr(ss));
1362
1363 return ret;
1364}
1365#else
1366static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1367 struct sockaddr_storage *ss, char delim, const char **ipend)
1368{
1369 return -EINVAL;
1370}
1371#endif
1372
1373/*
1374 * Parse a server name (IP or hostname). If a valid IP address is not found
1375 * then try to extract a hostname to resolve using userspace DNS upcall.
1376 */
1377static int ceph_parse_server_name(const char *name, size_t namelen,
1378 struct sockaddr_storage *ss, char delim, const char **ipend)
1379{
1380 int ret;
1381
1382 ret = ceph_pton(name, namelen, ss, delim, ipend);
1383 if (ret)
1384 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1385
1386 return ret;
1387}
1388
1389/*
Sage Weil31b80062009-10-06 11:31:13 -07001390 * Parse an ip[:port] list into an addr array. Use the default
1391 * monitor port if a port isn't specified.
1392 */
1393int ceph_parse_ips(const char *c, const char *end,
1394 struct ceph_entity_addr *addr,
1395 int max_count, int *count)
1396{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001397 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001398 const char *p = c;
1399
1400 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1401 for (i = 0; i < max_count; i++) {
1402 const char *ipend;
1403 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001404 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001405 char delim = ',';
1406
1407 if (*p == '[') {
1408 delim = ']';
1409 p++;
1410 }
Sage Weil31b80062009-10-06 11:31:13 -07001411
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001412 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1413 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001414 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001415 ret = -EINVAL;
1416
Sage Weil31b80062009-10-06 11:31:13 -07001417 p = ipend;
1418
Sage Weil39139f62010-07-08 09:54:52 -07001419 if (delim == ']') {
1420 if (*p != ']') {
1421 dout("missing matching ']'\n");
1422 goto bad;
1423 }
1424 p++;
1425 }
1426
Sage Weil31b80062009-10-06 11:31:13 -07001427 /* port? */
1428 if (p < end && *p == ':') {
1429 port = 0;
1430 p++;
1431 while (p < end && *p >= '0' && *p <= '9') {
1432 port = (port * 10) + (*p - '0');
1433 p++;
1434 }
1435 if (port > 65535 || port == 0)
1436 goto bad;
1437 } else {
1438 port = CEPH_MON_PORT;
1439 }
1440
1441 addr_set_port(ss, port);
1442
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001443 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001444
1445 if (p == end)
1446 break;
1447 if (*p != ',')
1448 goto bad;
1449 p++;
1450 }
1451
1452 if (p != end)
1453 goto bad;
1454
1455 if (count)
1456 *count = i + 1;
1457 return 0;
1458
1459bad:
Sage Weil39139f62010-07-08 09:54:52 -07001460 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001461 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001462}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001463EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001464
Sage Weileed0ef22009-11-10 14:34:36 -08001465static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001466{
Sage Weileed0ef22009-11-10 14:34:36 -08001467 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001468
1469 if (verify_hello(con) < 0)
1470 return -1;
1471
Sage Weil63f2d212009-11-03 15:17:56 -08001472 ceph_decode_addr(&con->actual_peer_addr);
1473 ceph_decode_addr(&con->peer_addr_for_me);
1474
Sage Weil31b80062009-10-06 11:31:13 -07001475 /*
1476 * Make sure the other end is who we wanted. note that the other
1477 * end may not yet know their ip address, so if it's 0.0.0.0, give
1478 * them the benefit of the doubt.
1479 */
Sage Weil103e2d32010-01-07 16:12:36 -08001480 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1481 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001482 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1483 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001484 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001485 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001486 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001487 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001488 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001489 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001490 return -1;
1491 }
1492
1493 /*
1494 * did we learn our address?
1495 */
1496 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1497 int port = addr_port(&con->msgr->inst.addr.in_addr);
1498
1499 memcpy(&con->msgr->inst.addr.in_addr,
1500 &con->peer_addr_for_me.in_addr,
1501 sizeof(con->peer_addr_for_me.in_addr));
1502 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001503 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001504 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001505 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001506 }
1507
Sage Weileed0ef22009-11-10 14:34:36 -08001508 return 0;
1509}
1510
1511static int process_connect(struct ceph_connection *con)
1512{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001513 u64 sup_feat = con->msgr->supported_features;
1514 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001515 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001516 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001517
Sage Weileed0ef22009-11-10 14:34:36 -08001518 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1519
Sage Weil31b80062009-10-06 11:31:13 -07001520 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001521 case CEPH_MSGR_TAG_FEATURES:
1522 pr_err("%s%lld %s feature set mismatch,"
1523 " my %llx < server's %llx, missing %llx\n",
1524 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001525 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001526 sup_feat, server_feat, server_feat & ~sup_feat);
1527 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06001528 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08001529 return -1;
1530
Sage Weil31b80062009-10-06 11:31:13 -07001531 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001532 pr_err("%s%lld %s protocol version mismatch,"
1533 " my %d != server's %d\n",
1534 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001535 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001536 le32_to_cpu(con->out_connect.protocol_version),
1537 le32_to_cpu(con->in_reply.protocol_version));
1538 con->error_msg = "protocol version mismatch";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06001539 reset_connection(con);
Sage Weil31b80062009-10-06 11:31:13 -07001540 return -1;
1541
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001542 case CEPH_MSGR_TAG_BADAUTHORIZER:
1543 con->auth_retry++;
1544 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1545 con->auth_retry);
1546 if (con->auth_retry == 2) {
1547 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001548 return -1;
1549 }
1550 con->auth_retry = 1;
Jim Schutt6d4221b2012-08-10 10:37:38 -07001551 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001552 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001553 if (ret < 0)
1554 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001555 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001556 break;
Sage Weil31b80062009-10-06 11:31:13 -07001557
1558 case CEPH_MSGR_TAG_RESETSESSION:
1559 /*
1560 * If we connected with a large connect_seq but the peer
1561 * has no record of a session with us (no connection, or
1562 * connect_seq == 0), they will send RESETSESION to indicate
1563 * that they must have reset their session, and may have
1564 * dropped messages.
1565 */
1566 dout("process_connect got RESET peer seq %u\n",
Sage Weil5bdca4e2012-07-10 11:53:34 -07001567 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07001568 pr_err("%s%lld %s connection reset\n",
1569 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001570 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001571 reset_connection(con);
Jim Schutt6d4221b2012-08-10 10:37:38 -07001572 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001573 ret = prepare_write_connect(con);
1574 if (ret < 0)
1575 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001576 prepare_read_connect(con);
1577
1578 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001579 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001580 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1581 if (con->ops->peer_reset)
1582 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001583 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07001584 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07001585 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001586 break;
1587
1588 case CEPH_MSGR_TAG_RETRY_SESSION:
1589 /*
1590 * If we sent a smaller connect_seq than the peer has, try
1591 * again with a larger value.
1592 */
Sage Weil5bdca4e2012-07-10 11:53:34 -07001593 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001594 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil5bdca4e2012-07-10 11:53:34 -07001595 le32_to_cpu(con->in_reply.connect_seq));
1596 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Jim Schutt6d4221b2012-08-10 10:37:38 -07001597 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001598 ret = prepare_write_connect(con);
1599 if (ret < 0)
1600 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001601 prepare_read_connect(con);
1602 break;
1603
1604 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1605 /*
1606 * If we sent a smaller global_seq than the peer has, try
1607 * again with a larger value.
1608 */
Sage Weileed0ef22009-11-10 14:34:36 -08001609 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001610 con->peer_global_seq,
Sage Weil5bdca4e2012-07-10 11:53:34 -07001611 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07001612 get_global_seq(con->msgr,
Sage Weil5bdca4e2012-07-10 11:53:34 -07001613 le32_to_cpu(con->in_reply.global_seq));
Jim Schutt6d4221b2012-08-10 10:37:38 -07001614 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001615 ret = prepare_write_connect(con);
1616 if (ret < 0)
1617 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001618 prepare_read_connect(con);
1619 break;
1620
1621 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001622 if (req_feat & ~server_feat) {
1623 pr_err("%s%lld %s protocol feature mismatch,"
1624 " my required %llx > server's %llx, need %llx\n",
1625 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001626 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001627 req_feat, server_feat, req_feat & ~server_feat);
1628 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06001629 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08001630 return -1;
1631 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07001632
Alex Elder122070a2012-12-26 10:43:57 -06001633 WARN_ON(con->state != CON_STATE_NEGOTIATING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07001634 con->state = CON_STATE_OPEN;
1635
Sage Weil31b80062009-10-06 11:31:13 -07001636 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1637 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001638 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001639 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1640 con->peer_global_seq,
1641 le32_to_cpu(con->in_reply.connect_seq),
1642 con->connect_seq);
1643 WARN_ON(con->connect_seq !=
1644 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001645
1646 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Sage Weil4a861692012-07-20 17:29:55 -07001647 set_bit(CON_FLAG_LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001648
Sage Weil85effe12012-07-30 16:22:05 -07001649 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -07001650
1651 prepare_read_tag(con);
1652 break;
1653
1654 case CEPH_MSGR_TAG_WAIT:
1655 /*
1656 * If there is a connection race (we are opening
1657 * connections to each other), one of us may just have
1658 * to WAIT. This shouldn't happen if we are the
1659 * client.
1660 */
Sage Weil04177882011-05-12 15:33:17 -07001661 pr_err("process_connect got WAIT as client\n");
1662 con->error_msg = "protocol error, got WAIT as client";
1663 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001664
1665 default:
1666 pr_err("connect protocol error, will retry\n");
1667 con->error_msg = "protocol error, garbage tag during connect";
1668 return -1;
1669 }
1670 return 0;
1671}
1672
1673
1674/*
1675 * read (part of) an ack
1676 */
1677static int read_partial_ack(struct ceph_connection *con)
1678{
Alex Elderfd516532012-05-10 10:29:50 -05001679 int size = sizeof (con->in_temp_ack);
1680 int end = size;
Sage Weil31b80062009-10-06 11:31:13 -07001681
Alex Elderfd516532012-05-10 10:29:50 -05001682 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001683}
1684
1685
1686/*
1687 * We can finally discard anything that's been acked.
1688 */
1689static void process_ack(struct ceph_connection *con)
1690{
1691 struct ceph_msg *m;
1692 u64 ack = le64_to_cpu(con->in_temp_ack);
1693 u64 seq;
1694
Sage Weil31b80062009-10-06 11:31:13 -07001695 while (!list_empty(&con->out_sent)) {
1696 m = list_first_entry(&con->out_sent, struct ceph_msg,
1697 list_head);
1698 seq = le64_to_cpu(m->hdr.seq);
1699 if (seq > ack)
1700 break;
1701 dout("got ack for seq %llu type %d at %p\n", seq,
1702 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001703 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001704 ceph_msg_remove(m);
1705 }
Sage Weil31b80062009-10-06 11:31:13 -07001706 prepare_read_tag(con);
1707}
1708
1709
1710
1711
Yehuda Sadeh24504182010-01-08 13:58:34 -08001712static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001713 struct kvec *section,
1714 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001715{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001716 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001717
Yehuda Sadeh24504182010-01-08 13:58:34 -08001718 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001719
Yehuda Sadeh24504182010-01-08 13:58:34 -08001720 while (section->iov_len < sec_len) {
1721 BUG_ON(section->iov_base == NULL);
1722 left = sec_len - section->iov_len;
1723 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1724 section->iov_len, left);
1725 if (ret <= 0)
1726 return ret;
1727 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001728 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001729 if (section->iov_len == sec_len)
1730 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001731
1732 return 1;
1733}
1734
Sage Weil4740a622012-07-30 18:19:30 -07001735static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001736
1737static int read_partial_message_pages(struct ceph_connection *con,
1738 struct page **pages,
Eric Dumazet95c96172012-04-15 05:58:06 +00001739 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001740{
1741 void *p;
1742 int ret;
1743 int left;
1744
1745 left = min((int)(data_len - con->in_msg_pos.data_pos),
1746 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1747 /* (page) data */
1748 BUG_ON(pages == NULL);
1749 p = kmap(pages[con->in_msg_pos.page]);
1750 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1751 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001752 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001753 con->in_data_crc =
1754 crc32c(con->in_data_crc,
1755 p + con->in_msg_pos.page_pos, ret);
1756 kunmap(pages[con->in_msg_pos.page]);
1757 if (ret <= 0)
1758 return ret;
1759 con->in_msg_pos.data_pos += ret;
1760 con->in_msg_pos.page_pos += ret;
1761 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1762 con->in_msg_pos.page_pos = 0;
1763 con->in_msg_pos.page++;
1764 }
1765
1766 return ret;
1767}
1768
1769#ifdef CONFIG_BLOCK
1770static int read_partial_message_bio(struct ceph_connection *con,
1771 struct bio **bio_iter, int *bio_seg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001772 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001773{
1774 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1775 void *p;
1776 int ret, left;
1777
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001778 left = min((int)(data_len - con->in_msg_pos.data_pos),
1779 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1780
1781 p = kmap(bv->bv_page) + bv->bv_offset;
1782
1783 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1784 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001785 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001786 con->in_data_crc =
1787 crc32c(con->in_data_crc,
1788 p + con->in_msg_pos.page_pos, ret);
1789 kunmap(bv->bv_page);
1790 if (ret <= 0)
1791 return ret;
1792 con->in_msg_pos.data_pos += ret;
1793 con->in_msg_pos.page_pos += ret;
1794 if (con->in_msg_pos.page_pos == bv->bv_len) {
1795 con->in_msg_pos.page_pos = 0;
1796 iter_bio_next(bio_iter, bio_seg);
1797 }
1798
1799 return ret;
1800}
1801#endif
1802
Sage Weil31b80062009-10-06 11:31:13 -07001803/*
1804 * read (part of) a message.
1805 */
1806static int read_partial_message(struct ceph_connection *con)
1807{
1808 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001809 int size;
1810 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001811 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001812 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001813 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001814 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001815 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001816
1817 dout("read_partial_message con %p msg %p\n", con, m);
1818
1819 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001820 size = sizeof (con->in_hdr);
1821 end = size;
1822 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001823 if (ret <= 0)
1824 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001825
1826 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1827 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1828 pr_err("read_partial_message bad hdr "
1829 " crc %u != expected %u\n",
1830 crc, con->in_hdr.crc);
1831 return -EBADMSG;
1832 }
1833
Sage Weil31b80062009-10-06 11:31:13 -07001834 front_len = le32_to_cpu(con->in_hdr.front_len);
1835 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1836 return -EIO;
1837 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1838 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1839 return -EIO;
1840 data_len = le32_to_cpu(con->in_hdr.data_len);
1841 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1842 return -EIO;
1843
Sage Weilae187562010-04-22 07:47:01 -07001844 /* verify seq# */
1845 seq = le64_to_cpu(con->in_hdr.seq);
1846 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001847 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001848 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001849 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001850 seq, con->in_seq + 1);
1851 con->in_base_pos = -front_len - middle_len - data_len -
1852 sizeof(m->footer);
1853 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001854 return 0;
1855 } else if ((s64)seq - (s64)con->in_seq > 1) {
1856 pr_err("read_partial_message bad seq %lld expected %lld\n",
1857 seq, con->in_seq + 1);
1858 con->error_msg = "bad message sequence # for incoming message";
1859 return -EBADMSG;
1860 }
1861
Sage Weil31b80062009-10-06 11:31:13 -07001862 /* allocate message? */
1863 if (!con->in_msg) {
Sage Weil4740a622012-07-30 18:19:30 -07001864 int skip = 0;
1865
Sage Weil31b80062009-10-06 11:31:13 -07001866 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1867 con->in_hdr.front_len, con->in_hdr.data_len);
Sage Weil4740a622012-07-30 18:19:30 -07001868 ret = ceph_con_in_msg_alloc(con, &skip);
1869 if (ret < 0)
1870 return ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001871 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07001872 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001873 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001874 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001875 con->in_base_pos = -front_len - middle_len - data_len -
1876 sizeof(m->footer);
1877 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001878 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001879 return 0;
1880 }
Alex Elder38941f82012-06-01 14:56:43 -05001881
Sage Weil4740a622012-07-30 18:19:30 -07001882 BUG_ON(!con->in_msg);
Alex Elder38941f82012-06-01 14:56:43 -05001883 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001884 m = con->in_msg;
1885 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001886 if (m->middle)
1887 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001888
1889 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001890 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001891 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001892 else
1893 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001894 con->in_msg_pos.data_pos = 0;
Sage Weila4107022012-07-30 16:20:25 -07001895
1896#ifdef CONFIG_BLOCK
1897 if (m->bio)
1898 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1899#endif
Sage Weil31b80062009-10-06 11:31:13 -07001900 }
1901
1902 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001903 ret = read_partial_message_section(con, &m->front, front_len,
1904 &con->in_front_crc);
1905 if (ret <= 0)
1906 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001907
1908 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001909 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001910 ret = read_partial_message_section(con, &m->middle->vec,
1911 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001912 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001913 if (ret <= 0)
1914 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001915 }
1916
1917 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001918 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001919 if (m->pages) {
1920 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001921 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001922 if (ret <= 0)
1923 return ret;
1924#ifdef CONFIG_BLOCK
1925 } else if (m->bio) {
Sage Weila4107022012-07-30 16:20:25 -07001926 BUG_ON(!m->bio_iter);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001927 ret = read_partial_message_bio(con,
1928 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001929 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001930 if (ret <= 0)
1931 return ret;
1932#endif
1933 } else {
1934 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001935 }
1936 }
1937
Sage Weil31b80062009-10-06 11:31:13 -07001938 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001939 size = sizeof (m->footer);
1940 end += size;
1941 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001942 if (ret <= 0)
1943 return ret;
1944
Sage Weil31b80062009-10-06 11:31:13 -07001945 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1946 m, front_len, m->footer.front_crc, middle_len,
1947 m->footer.middle_crc, data_len, m->footer.data_crc);
1948
1949 /* crc ok? */
1950 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1951 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1952 m, con->in_front_crc, m->footer.front_crc);
1953 return -EBADMSG;
1954 }
1955 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1956 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1957 m, con->in_middle_crc, m->footer.middle_crc);
1958 return -EBADMSG;
1959 }
Alex Elderbca064d2012-02-15 07:43:54 -06001960 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001961 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1962 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1963 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1964 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1965 return -EBADMSG;
1966 }
1967
1968 return 1; /* done! */
1969}
1970
1971/*
1972 * Process message. This happens in the worker thread. The callback should
1973 * be careful not to do anything that waits on other incoming messages or it
1974 * may deadlock.
1975 */
1976static void process_message(struct ceph_connection *con)
1977{
Sage Weil5e095e82009-12-14 14:30:34 -08001978 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001979
Alex Elder38941f82012-06-01 14:56:43 -05001980 BUG_ON(con->in_msg->con != con);
1981 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001982 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001983 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001984 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001985
1986 /* if first message, set peer_name */
1987 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001988 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001989
Sage Weil31b80062009-10-06 11:31:13 -07001990 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001991 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001992
1993 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1994 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001995 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001996 le16_to_cpu(msg->hdr.type),
1997 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1998 le32_to_cpu(msg->hdr.front_len),
1999 le32_to_cpu(msg->hdr.data_len),
2000 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2001 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08002002
2003 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002004}
2005
2006
2007/*
2008 * Write something to the socket. Called in a worker thread when the
2009 * socket appears to be writeable and we have something ready to send.
2010 */
2011static int try_write(struct ceph_connection *con)
2012{
Sage Weil31b80062009-10-06 11:31:13 -07002013 int ret = 1;
2014
Sage Weild59315c2012-06-21 12:49:23 -07002015 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002016
Sage Weil31b80062009-10-06 11:31:13 -07002017more:
2018 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2019
2020 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002021 if (con->state == CON_STATE_PREOPEN) {
2022 BUG_ON(con->sock);
2023 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002024
Alex Eldere2200422012-05-23 14:35:23 -05002025 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002026 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002027 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002028
Sage Weilcf3e5c42009-12-11 09:48:05 -08002029 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002030 con->in_tag = CEPH_MSGR_TAG_READY;
2031 dout("try_write initiating connect on %p new state %lu\n",
2032 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002033 ret = ceph_tcp_connect(con);
2034 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002035 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002036 goto out;
2037 }
2038 }
2039
2040more_kvec:
2041 /* kvec data queued? */
2042 if (con->out_skip) {
2043 ret = write_partial_skip(con);
2044 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002045 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002046 }
2047 if (con->out_kvec_left) {
2048 ret = write_partial_kvec(con);
2049 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002050 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002051 }
2052
2053 /* msg pages? */
2054 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002055 if (con->out_msg_done) {
2056 ceph_msg_put(con->out_msg);
2057 con->out_msg = NULL; /* we're done with this one */
2058 goto do_next;
2059 }
2060
Sage Weil31b80062009-10-06 11:31:13 -07002061 ret = write_partial_msg_pages(con);
2062 if (ret == 1)
2063 goto more_kvec; /* we need to send the footer, too! */
2064 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002065 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002066 if (ret < 0) {
2067 dout("try_write write_partial_msg_pages err %d\n",
2068 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002069 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002070 }
2071 }
2072
Sage Weilc86a2932009-12-14 14:04:30 -08002073do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002074 if (con->state == CON_STATE_OPEN) {
Sage Weil31b80062009-10-06 11:31:13 -07002075 /* is anything else pending? */
2076 if (!list_empty(&con->out_queue)) {
2077 prepare_write_message(con);
2078 goto more;
2079 }
2080 if (con->in_seq > con->in_seq_acked) {
2081 prepare_write_ack(con);
2082 goto more;
2083 }
Sage Weil4a861692012-07-20 17:29:55 -07002084 if (test_and_clear_bit(CON_FLAG_KEEPALIVE_PENDING,
2085 &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002086 prepare_write_keepalive(con);
2087 goto more;
2088 }
2089 }
2090
2091 /* Nothing to do! */
Sage Weil4a861692012-07-20 17:29:55 -07002092 clear_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002093 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002094 ret = 0;
2095out:
Sage Weil42961d22011-01-25 08:19:34 -08002096 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002097 return ret;
2098}
2099
2100
2101
2102/*
2103 * Read what we can from the socket.
2104 */
2105static int try_read(struct ceph_connection *con)
2106{
Sage Weil31b80062009-10-06 11:31:13 -07002107 int ret = -1;
2108
Sage Weil31b80062009-10-06 11:31:13 -07002109more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002110 dout("try_read start on %p state %lu\n", con, con->state);
2111 if (con->state != CON_STATE_CONNECTING &&
2112 con->state != CON_STATE_NEGOTIATING &&
2113 con->state != CON_STATE_OPEN)
2114 return 0;
2115
2116 BUG_ON(!con->sock);
2117
Sage Weil31b80062009-10-06 11:31:13 -07002118 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2119 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002120
Sage Weil8dacc7d2012-07-20 17:24:40 -07002121 if (con->state == CON_STATE_CONNECTING) {
Alex Elder7593af92012-05-24 11:55:03 -05002122 dout("try_read connecting\n");
2123 ret = read_partial_banner(con);
2124 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002125 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002126 ret = process_banner(con);
2127 if (ret < 0)
2128 goto out;
2129
Sage Weil8dacc7d2012-07-20 17:24:40 -07002130 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002131
Jim Schutt6d4221b2012-08-10 10:37:38 -07002132 /*
2133 * Received banner is good, exchange connection info.
2134 * Do not reset out_kvec, as sending our banner raced
2135 * with receiving peer banner after connect completed.
2136 */
Alex Elder7593af92012-05-24 11:55:03 -05002137 ret = prepare_write_connect(con);
2138 if (ret < 0)
2139 goto out;
2140 prepare_read_connect(con);
2141
2142 /* Send connection info before awaiting response */
Sage Weil0da5d702011-05-19 11:21:05 -07002143 goto out;
2144 }
2145
Sage Weil8dacc7d2012-07-20 17:24:40 -07002146 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002147 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002148 ret = read_partial_connect(con);
2149 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002150 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002151 ret = process_connect(con);
2152 if (ret < 0)
2153 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002154 goto more;
2155 }
2156
Alex Elder122070a2012-12-26 10:43:57 -06002157 WARN_ON(con->state != CON_STATE_OPEN);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002158
Sage Weil31b80062009-10-06 11:31:13 -07002159 if (con->in_base_pos < 0) {
2160 /*
2161 * skipping + discarding content.
2162 *
2163 * FIXME: there must be a better way to do this!
2164 */
Alex Elder84495f42012-02-15 07:43:55 -06002165 static char buf[SKIP_BUF_SIZE];
2166 int skip = min((int) sizeof (buf), -con->in_base_pos);
2167
Sage Weil31b80062009-10-06 11:31:13 -07002168 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2169 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2170 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002171 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002172 con->in_base_pos += ret;
2173 if (con->in_base_pos)
2174 goto more;
2175 }
2176 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2177 /*
2178 * what's next?
2179 */
2180 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2181 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002182 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002183 dout("try_read got tag %d\n", (int)con->in_tag);
2184 switch (con->in_tag) {
2185 case CEPH_MSGR_TAG_MSG:
2186 prepare_read_message(con);
2187 break;
2188 case CEPH_MSGR_TAG_ACK:
2189 prepare_read_ack(con);
2190 break;
2191 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002192 con_close_socket(con);
2193 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002194 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002195 default:
2196 goto bad_tag;
2197 }
2198 }
2199 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2200 ret = read_partial_message(con);
2201 if (ret <= 0) {
2202 switch (ret) {
2203 case -EBADMSG:
2204 con->error_msg = "bad crc";
2205 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002206 break;
Sage Weil31b80062009-10-06 11:31:13 -07002207 case -EIO:
2208 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002209 break;
Sage Weil31b80062009-10-06 11:31:13 -07002210 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002211 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002212 }
2213 if (con->in_tag == CEPH_MSGR_TAG_READY)
2214 goto more;
2215 process_message(con);
Sage Weil7b862e02012-07-30 18:16:56 -07002216 if (con->state == CON_STATE_OPEN)
2217 prepare_read_tag(con);
Sage Weil31b80062009-10-06 11:31:13 -07002218 goto more;
2219 }
2220 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2221 ret = read_partial_ack(con);
2222 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002223 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002224 process_ack(con);
2225 goto more;
2226 }
2227
Sage Weil31b80062009-10-06 11:31:13 -07002228out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002229 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002230 return ret;
2231
2232bad_tag:
2233 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2234 con->error_msg = "protocol error, garbage tag";
2235 ret = -1;
2236 goto out;
2237}
2238
2239
2240/*
Alex Elder802c6d92012-10-08 20:37:30 -07002241 * Atomically queue work on a connection after the specified delay.
2242 * Bump @con reference to avoid races with connection teardown.
2243 * Returns 0 if work was queued, or an error code otherwise.
Sage Weil31b80062009-10-06 11:31:13 -07002244 */
Alex Elder802c6d92012-10-08 20:37:30 -07002245static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
Sage Weil31b80062009-10-06 11:31:13 -07002246{
Sage Weil31b80062009-10-06 11:31:13 -07002247 if (!con->ops->get(con)) {
Alex Elder802c6d92012-10-08 20:37:30 -07002248 dout("%s %p ref count 0\n", __func__, con);
2249
2250 return -ENOENT;
Sage Weil31b80062009-10-06 11:31:13 -07002251 }
2252
Alex Elder802c6d92012-10-08 20:37:30 -07002253 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2254 dout("%s %p - already queued\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -07002255 con->ops->put(con);
Alex Elder802c6d92012-10-08 20:37:30 -07002256
2257 return -EBUSY;
Sage Weil31b80062009-10-06 11:31:13 -07002258 }
Alex Elder802c6d92012-10-08 20:37:30 -07002259
2260 dout("%s %p %lu\n", __func__, con, delay);
2261
2262 return 0;
2263}
2264
2265static void queue_con(struct ceph_connection *con)
2266{
2267 (void) queue_con_delay(con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002268}
2269
Alex Elder7bb21d682012-12-07 19:50:07 -06002270static bool con_sock_closed(struct ceph_connection *con)
2271{
2272 if (!test_and_clear_bit(CON_FLAG_SOCK_CLOSED, &con->flags))
2273 return false;
2274
2275#define CASE(x) \
2276 case CON_STATE_ ## x: \
2277 con->error_msg = "socket closed (con state " #x ")"; \
2278 break;
2279
2280 switch (con->state) {
2281 CASE(CLOSED);
2282 CASE(PREOPEN);
2283 CASE(CONNECTING);
2284 CASE(NEGOTIATING);
2285 CASE(OPEN);
2286 CASE(STANDBY);
2287 default:
2288 pr_warning("%s con %p unrecognized state %lu\n",
2289 __func__, con, con->state);
2290 con->error_msg = "unrecognized con state";
2291 BUG();
2292 break;
2293 }
2294#undef CASE
2295
2296 return true;
2297}
2298
Sage Weil31b80062009-10-06 11:31:13 -07002299/*
2300 * Do some work on a connection. Drop a connection ref when we're done.
2301 */
2302static void con_work(struct work_struct *work)
2303{
2304 struct ceph_connection *con = container_of(work, struct ceph_connection,
2305 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002306 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002307
Sage Weil9dd46582010-04-28 13:51:50 -07002308 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002309restart:
Alex Elder7bb21d682012-12-07 19:50:07 -06002310 if (con_sock_closed(con))
Alex Elder188048b2012-06-20 21:53:53 -05002311 goto fault;
Alex Elder188048b2012-06-20 21:53:53 -05002312
Sage Weil4a861692012-07-20 17:29:55 -07002313 if (test_and_clear_bit(CON_FLAG_BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002314 dout("con_work %p backing off\n", con);
Alex Elder802c6d92012-10-08 20:37:30 -07002315 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2316 if (ret) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002317 dout("con_work %p FAILED to back off %lu\n", con,
2318 con->delay);
Alex Elder802c6d92012-10-08 20:37:30 -07002319 BUG_ON(ret == -ENOENT);
Alex Elder588377d2012-10-08 20:37:30 -07002320 set_bit(CON_FLAG_BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002321 }
Alex Elder588377d2012-10-08 20:37:30 -07002322 goto done;
Sage Weil60bf8bf2011-03-04 12:24:28 -08002323 }
Sage Weil9dd46582010-04-28 13:51:50 -07002324
Sage Weil8dacc7d2012-07-20 17:24:40 -07002325 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08002326 dout("con_work %p STANDBY\n", con);
2327 goto done;
2328 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002329 if (con->state == CON_STATE_CLOSED) {
Sage Weil2e8cb102012-07-20 15:40:04 -07002330 dout("con_work %p CLOSED\n", con);
2331 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -07002332 goto done;
2333 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002334 if (con->state == CON_STATE_PREOPEN) {
Sage Weil31b80062009-10-06 11:31:13 -07002335 dout("con_work OPENING\n");
Sage Weil2e8cb102012-07-20 15:40:04 -07002336 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -07002337 }
2338
Sage Weil0da5d702011-05-19 11:21:05 -07002339 ret = try_read(con);
2340 if (ret == -EAGAIN)
2341 goto restart;
Sage Weil3a140a02012-07-30 16:24:21 -07002342 if (ret < 0) {
2343 con->error_msg = "socket error on read";
Sage Weil0da5d702011-05-19 11:21:05 -07002344 goto fault;
Sage Weil3a140a02012-07-30 16:24:21 -07002345 }
Sage Weil0da5d702011-05-19 11:21:05 -07002346
2347 ret = try_write(con);
2348 if (ret == -EAGAIN)
2349 goto restart;
Sage Weil3a140a02012-07-30 16:24:21 -07002350 if (ret < 0) {
2351 con->error_msg = "socket error on write";
Sage Weil0da5d702011-05-19 11:21:05 -07002352 goto fault;
Sage Weil3a140a02012-07-30 16:24:21 -07002353 }
Sage Weil31b80062009-10-06 11:31:13 -07002354
2355done:
Sage Weil9dd46582010-04-28 13:51:50 -07002356 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002357done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002358 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002359 return;
2360
2361fault:
Sage Weil0da5d702011-05-19 11:21:05 -07002362 ceph_fault(con); /* error/fault path */
2363 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002364}
2365
2366
2367/*
2368 * Generic error/fault handler. A retry mechanism is used with
2369 * exponential backoff
2370 */
2371static void ceph_fault(struct ceph_connection *con)
Sage Weil8636ea62012-07-30 18:17:13 -07002372 __releases(con->mutex)
Sage Weil31b80062009-10-06 11:31:13 -07002373{
Alex Elder28362982012-12-14 16:47:41 -06002374 pr_warning("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002375 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002376 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002377 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002378
Alex Elder122070a2012-12-26 10:43:57 -06002379 WARN_ON(con->state != CON_STATE_CONNECTING &&
Sage Weil8dacc7d2012-07-20 17:24:40 -07002380 con->state != CON_STATE_NEGOTIATING &&
2381 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08002382
Sage Weil31b80062009-10-06 11:31:13 -07002383 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002384
Sage Weil4a861692012-07-20 17:29:55 -07002385 if (test_bit(CON_FLAG_LOSSYTX, &con->flags)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002386 dout("fault on LOSSYTX channel, marking CLOSED\n");
2387 con->state = CON_STATE_CLOSED;
Sage Weil3b5ede02012-07-20 15:22:53 -07002388 goto out_unlock;
2389 }
2390
Sage Weil5e095e82009-12-14 14:30:34 -08002391 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002392 BUG_ON(con->in_msg->con != con);
2393 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002394 ceph_msg_put(con->in_msg);
2395 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002396 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002397 }
Sage Weil31b80062009-10-06 11:31:13 -07002398
Sage Weile80a52d2010-02-25 12:40:45 -08002399 /* Requeue anything that hasn't been acked */
2400 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002401
Sage Weile76661d2011-03-03 10:10:15 -08002402 /* If there are no messages queued or keepalive pending, place
2403 * the connection in a STANDBY state */
2404 if (list_empty(&con->out_queue) &&
Sage Weil4a861692012-07-20 17:29:55 -07002405 !test_bit(CON_FLAG_KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002406 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Sage Weil4a861692012-07-20 17:29:55 -07002407 clear_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002408 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08002409 } else {
2410 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002411 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08002412 if (con->delay == 0)
2413 con->delay = BASE_DELAY_INTERVAL;
2414 else if (con->delay < MAX_DELAY_INTERVAL)
2415 con->delay *= 2;
Alex Elder8618e302012-10-08 20:37:30 -07002416 set_bit(CON_FLAG_BACKOFF, &con->flags);
2417 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -07002418 }
2419
Sage Weil91e45ce32010-02-15 12:05:09 -08002420out_unlock:
2421 mutex_unlock(&con->mutex);
Sage Weil161fd652010-02-25 12:38:57 -08002422 /*
2423 * in case we faulted due to authentication, invalidate our
2424 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002425 */
Sage Weil161fd652010-02-25 12:38:57 -08002426 if (con->auth_retry && con->ops->invalidate_authorizer) {
2427 dout("calling invalidate_authorizer()\n");
2428 con->ops->invalidate_authorizer(con);
2429 }
2430
Sage Weil31b80062009-10-06 11:31:13 -07002431 if (con->ops->fault)
2432 con->ops->fault(con);
2433}
2434
2435
2436
2437/*
Alex Elder15d98822012-05-26 23:26:43 -05002438 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002439 */
Alex Elder15d98822012-05-26 23:26:43 -05002440void ceph_messenger_init(struct ceph_messenger *msgr,
2441 struct ceph_entity_addr *myaddr,
2442 u32 supported_features,
2443 u32 required_features,
2444 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002445{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002446 msgr->supported_features = supported_features;
2447 msgr->required_features = required_features;
2448
Sage Weil31b80062009-10-06 11:31:13 -07002449 spin_lock_init(&msgr->global_seq_lock);
2450
Sage Weil31b80062009-10-06 11:31:13 -07002451 if (myaddr)
2452 msgr->inst.addr = *myaddr;
2453
2454 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002455 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002456 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002457 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002458 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002459
Guanjun Hea2a32582012-07-08 19:50:33 -07002460 atomic_set(&msgr->stopping, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002461
Alex Elder15d98822012-05-26 23:26:43 -05002462 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002463}
Alex Elder15d98822012-05-26 23:26:43 -05002464EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002465
Sage Weile00de342011-03-04 12:25:05 -08002466static void clear_standby(struct ceph_connection *con)
2467{
2468 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002469 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08002470 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002471 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08002472 con->connect_seq++;
Sage Weil4a861692012-07-20 17:29:55 -07002473 WARN_ON(test_bit(CON_FLAG_WRITE_PENDING, &con->flags));
2474 WARN_ON(test_bit(CON_FLAG_KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002475 }
2476}
2477
Sage Weil31b80062009-10-06 11:31:13 -07002478/*
2479 * Queue up an outgoing message on the given connection.
2480 */
2481void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2482{
Sage Weila59b55a2012-07-20 15:34:04 -07002483 /* set src+dst */
2484 msg->hdr.src = con->msgr->inst.name;
2485 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2486 msg->needs_out_seq = true;
2487
2488 mutex_lock(&con->mutex);
2489
Sage Weil8dacc7d2012-07-20 17:24:40 -07002490 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07002491 dout("con_send %p closed, dropping %p\n", con, msg);
2492 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07002493 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002494 return;
2495 }
2496
Alex Elder38941f82012-06-01 14:56:43 -05002497 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002498 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002499 BUG_ON(msg->con == NULL);
Sage Weil31b80062009-10-06 11:31:13 -07002500
Sage Weil31b80062009-10-06 11:31:13 -07002501 BUG_ON(!list_empty(&msg->list_head));
2502 list_add_tail(&msg->list_head, &con->out_queue);
2503 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2504 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2505 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2506 le32_to_cpu(msg->hdr.front_len),
2507 le32_to_cpu(msg->hdr.middle_len),
2508 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07002509
2510 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08002511 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002512
2513 /* if there wasn't anything waiting to send before, queue
2514 * new work */
Sage Weil4a861692012-07-20 17:29:55 -07002515 if (test_and_set_bit(CON_FLAG_WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002516 queue_con(con);
2517}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002518EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002519
2520/*
2521 * Revoke a message that was previously queued for send
2522 */
Alex Elder6740a842012-06-01 14:56:43 -05002523void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002524{
Alex Elder6740a842012-06-01 14:56:43 -05002525 struct ceph_connection *con = msg->con;
2526
2527 if (!con)
2528 return; /* Message not in our possession */
2529
Sage Weilec302642009-12-22 10:43:42 -08002530 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002531 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002532 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002533 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002534 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002535 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002536 msg->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002537 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002538
Sage Weil31b80062009-10-06 11:31:13 -07002539 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002540 }
2541 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002542 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002543 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002544 if (con->out_kvec_is_msg) {
2545 con->out_skip = con->out_kvec_bytes;
2546 con->out_kvec_is_msg = false;
2547 }
Sage Weiled98ada2010-07-05 12:15:14 -07002548 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002549
2550 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002551 }
Sage Weilec302642009-12-22 10:43:42 -08002552 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002553}
2554
2555/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002556 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002557 */
Alex Elder8921d112012-06-01 14:56:43 -05002558void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002559{
Alex Elder8921d112012-06-01 14:56:43 -05002560 struct ceph_connection *con;
2561
2562 BUG_ON(msg == NULL);
2563 if (!msg->con) {
2564 dout("%s msg %p null con\n", __func__, msg);
2565
2566 return; /* Message not in our possession */
2567 }
2568
2569 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002570 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002571 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002572 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2573 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2574 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002575
2576 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002577 dout("%s %p msg %p revoked\n", __func__, con, msg);
2578 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002579 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002580 front_len -
2581 middle_len -
2582 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002583 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002584 ceph_msg_put(con->in_msg);
2585 con->in_msg = NULL;
2586 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002587 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002588 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002589 dout("%s %p in_msg %p msg %p no-op\n",
2590 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002591 }
2592 mutex_unlock(&con->mutex);
2593}
2594
2595/*
Sage Weil31b80062009-10-06 11:31:13 -07002596 * Queue a keepalive byte to ensure the tcp connection is alive.
2597 */
2598void ceph_con_keepalive(struct ceph_connection *con)
2599{
Sage Weile00de342011-03-04 12:25:05 -08002600 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07002601 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08002602 clear_standby(con);
Sage Weil00650932012-07-20 15:33:04 -07002603 mutex_unlock(&con->mutex);
Sage Weil4a861692012-07-20 17:29:55 -07002604 if (test_and_set_bit(CON_FLAG_KEEPALIVE_PENDING, &con->flags) == 0 &&
2605 test_and_set_bit(CON_FLAG_WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002606 queue_con(con);
2607}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002608EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002609
2610
2611/*
2612 * construct a new message with given type, size
2613 * the new msg has a ref count of 1.
2614 */
Sage Weilb61c2762011-08-09 15:03:46 -07002615struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2616 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002617{
2618 struct ceph_msg *m;
2619
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002620 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002621 if (m == NULL)
2622 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002623 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002624
2625 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002626 INIT_LIST_HEAD(&m->list_head);
2627
Sage Weil45c6ceb2010-05-11 15:01:51 -07002628 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002629 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002630 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2631 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002632 m->hdr.front_len = cpu_to_le32(front_len);
2633 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002634 m->hdr.data_len = 0;
2635 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002636 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002637 m->footer.front_crc = 0;
2638 m->footer.middle_crc = 0;
2639 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002640 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002641 m->front_max = front_len;
2642 m->front_is_vmalloc = false;
2643 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002644 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002645 m->pool = NULL;
2646
Henry C Changca208922011-05-03 02:29:56 +00002647 /* middle */
2648 m->middle = NULL;
2649
2650 /* data */
2651 m->nr_pages = 0;
2652 m->page_alignment = 0;
2653 m->pages = NULL;
2654 m->pagelist = NULL;
Alex Elder3ebc21f2013-01-31 16:02:01 -06002655#ifdef CONFIG_BLOCK
Henry C Changca208922011-05-03 02:29:56 +00002656 m->bio = NULL;
2657 m->bio_iter = NULL;
2658 m->bio_seg = 0;
Alex Elder3ebc21f2013-01-31 16:02:01 -06002659#endif /* CONFIG_BLOCK */
Henry C Changca208922011-05-03 02:29:56 +00002660 m->trail = NULL;
2661
Sage Weil31b80062009-10-06 11:31:13 -07002662 /* front */
2663 if (front_len) {
2664 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002665 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002666 PAGE_KERNEL);
2667 m->front_is_vmalloc = true;
2668 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002669 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002670 }
2671 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002672 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002673 front_len);
2674 goto out2;
2675 }
2676 } else {
2677 m->front.iov_base = NULL;
2678 }
2679 m->front.iov_len = front_len;
2680
Sage Weilbb257662010-04-01 16:07:23 -07002681 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002682 return m;
2683
2684out2:
2685 ceph_msg_put(m);
2686out:
Sage Weilb61c2762011-08-09 15:03:46 -07002687 if (!can_fail) {
2688 pr_err("msg_new can't create type %d front %d\n", type,
2689 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002690 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002691 } else {
2692 dout("msg_new can't create type %d front %d\n", type,
2693 front_len);
2694 }
Sage Weila79832f2010-04-01 16:06:19 -07002695 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002696}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002697EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002698
2699/*
Sage Weil31b80062009-10-06 11:31:13 -07002700 * Allocate "middle" portion of a message, if it is needed and wasn't
2701 * allocated by alloc_msg. This allows us to read a small fixed-size
2702 * per-type header in the front and then gracefully fail (i.e.,
2703 * propagate the error to the caller based on info in the front) when
2704 * the middle is too large.
2705 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002706static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002707{
2708 int type = le16_to_cpu(msg->hdr.type);
2709 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2710
2711 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2712 ceph_msg_type_name(type), middle_len);
2713 BUG_ON(!middle_len);
2714 BUG_ON(msg->middle);
2715
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002716 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002717 if (!msg->middle)
2718 return -ENOMEM;
2719 return 0;
2720}
2721
Yehuda Sadeh24504182010-01-08 13:58:34 -08002722/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002723 * Allocate a message for receiving an incoming message on a
2724 * connection, and save the result in con->in_msg. Uses the
2725 * connection's private alloc_msg op if available.
2726 *
Sage Weil4740a622012-07-30 18:19:30 -07002727 * Returns 0 on success, or a negative error code.
2728 *
2729 * On success, if we set *skip = 1:
2730 * - the next message should be skipped and ignored.
2731 * - con->in_msg == NULL
2732 * or if we set *skip = 0:
2733 * - con->in_msg is non-null.
2734 * On error (ENOMEM, EAGAIN, ...),
2735 * - con->in_msg == NULL
Yehuda Sadeh24504182010-01-08 13:58:34 -08002736 */
Sage Weil4740a622012-07-30 18:19:30 -07002737static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002738{
Sage Weil4740a622012-07-30 18:19:30 -07002739 struct ceph_msg_header *hdr = &con->in_hdr;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002740 int type = le16_to_cpu(hdr->type);
2741 int front_len = le32_to_cpu(hdr->front_len);
2742 int middle_len = le32_to_cpu(hdr->middle_len);
Sage Weil4740a622012-07-30 18:19:30 -07002743 int ret = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002744
Alex Elder1c20f2d2012-06-04 14:43:32 -05002745 BUG_ON(con->in_msg != NULL);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002746
2747 if (con->ops->alloc_msg) {
Sage Weil61399192012-07-30 18:19:45 -07002748 struct ceph_msg *msg;
2749
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002750 mutex_unlock(&con->mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002751 msg = con->ops->alloc_msg(con, hdr, skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002752 mutex_lock(&con->mutex);
Sage Weil61399192012-07-30 18:19:45 -07002753 if (con->state != CON_STATE_OPEN) {
Sage Weil72462402012-10-25 08:49:41 -07002754 if (msg)
2755 ceph_msg_put(msg);
Sage Weil61399192012-07-30 18:19:45 -07002756 return -EAGAIN;
2757 }
2758 con->in_msg = msg;
Alex Elder92ce0342012-06-04 14:43:33 -05002759 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002760 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002761 BUG_ON(con->in_msg->con == NULL);
2762 }
Sage Weil4740a622012-07-30 18:19:30 -07002763 if (*skip) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002764 con->in_msg = NULL;
Sage Weil4740a622012-07-30 18:19:30 -07002765 return 0;
2766 }
2767 if (!con->in_msg) {
2768 con->error_msg =
2769 "error allocating memory for incoming message";
2770 return -ENOMEM;
2771 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002772 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002773 if (!con->in_msg) {
2774 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2775 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002776 pr_err("unable to allocate msg type %d len %d\n",
2777 type, front_len);
Sage Weil4740a622012-07-30 18:19:30 -07002778 return -ENOMEM;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002779 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002780 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002781 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002782 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002783 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002784 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002785
Alex Elder1c20f2d2012-06-04 14:43:32 -05002786 if (middle_len && !con->in_msg->middle) {
2787 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002788 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002789 ceph_msg_put(con->in_msg);
2790 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002791 }
2792 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002793
Sage Weil4740a622012-07-30 18:19:30 -07002794 return ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002795}
2796
Sage Weil31b80062009-10-06 11:31:13 -07002797
2798/*
2799 * Free a generically kmalloc'd message.
2800 */
2801void ceph_msg_kfree(struct ceph_msg *m)
2802{
2803 dout("msg_kfree %p\n", m);
2804 if (m->front_is_vmalloc)
2805 vfree(m->front.iov_base);
2806 else
2807 kfree(m->front.iov_base);
2808 kfree(m);
2809}
2810
2811/*
2812 * Drop a msg ref. Destroy as needed.
2813 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002814void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002815{
Sage Weilc2e552e2009-12-07 15:55:05 -08002816 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002817
Sage Weilc2e552e2009-12-07 15:55:05 -08002818 dout("ceph_msg_put last one on %p\n", m);
2819 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002820
Sage Weilc2e552e2009-12-07 15:55:05 -08002821 /* drop middle, data, if any */
2822 if (m->middle) {
2823 ceph_buffer_put(m->middle);
2824 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002825 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002826 m->nr_pages = 0;
2827 m->pages = NULL;
2828
Sage Weil58bb3b32009-12-23 12:12:31 -08002829 if (m->pagelist) {
2830 ceph_pagelist_release(m->pagelist);
2831 kfree(m->pagelist);
2832 m->pagelist = NULL;
2833 }
2834
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002835 m->trail = NULL;
2836
Sage Weilc2e552e2009-12-07 15:55:05 -08002837 if (m->pool)
2838 ceph_msgpool_put(m->pool, m);
2839 else
2840 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002841}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002842EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002843
2844void ceph_msg_dump(struct ceph_msg *msg)
2845{
2846 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2847 msg->front_max, msg->nr_pages);
2848 print_hex_dump(KERN_DEBUG, "header: ",
2849 DUMP_PREFIX_OFFSET, 16, 1,
2850 &msg->hdr, sizeof(msg->hdr), true);
2851 print_hex_dump(KERN_DEBUG, " front: ",
2852 DUMP_PREFIX_OFFSET, 16, 1,
2853 msg->front.iov_base, msg->front.iov_len, true);
2854 if (msg->middle)
2855 print_hex_dump(KERN_DEBUG, "middle: ",
2856 DUMP_PREFIX_OFFSET, 16, 1,
2857 msg->middle->vec.iov_base,
2858 msg->middle->vec.iov_len, true);
2859 print_hex_dump(KERN_DEBUG, "footer: ",
2860 DUMP_PREFIX_OFFSET, 16, 1,
2861 &msg->footer, sizeof(msg->footer), true);
2862}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002863EXPORT_SYMBOL(ceph_msg_dump);