blob: b872db5c498941020c05c6c77a9729b70cace120 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07002
3#include <linux/crc32c.h>
4#include <linux/ctype.h>
5#include <linux/highmem.h>
6#include <linux/inet.h>
7#include <linux/kthread.h>
8#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070010#include <linux/socket.h>
11#include <linux/string.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070012#include <linux/bio.h>
13#include <linux/blkdev.h>
Noah Watkinsee3b56f2011-09-23 11:48:42 -070014#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070015#include <net/tcp.h>
16
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#include <linux/ceph/libceph.h>
18#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070022
23/*
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
29 * the sender.
30 */
31
Alex Elderbc18f4b2012-06-20 21:53:53 -050032/*
33 * We track the state of the socket on a given connection using
34 * values defined below. The transition to a new socket state is
35 * handled by a function which verifies we aren't coming from an
36 * unexpected state.
37 *
38 * --------
39 * | NEW* | transient initial state
40 * --------
41 * | con_sock_state_init()
42 * v
43 * ----------
44 * | CLOSED | initialized, but no socket (and no
45 * ---------- TCP connection)
46 * ^ \
47 * | \ con_sock_state_connecting()
48 * | ----------------------
49 * | \
50 * + con_sock_state_closed() \
Sage Weilfbb85a42012-06-27 12:31:02 -070051 * |+--------------------------- \
52 * | \ \ \
53 * | ----------- \ \
54 * | | CLOSING | socket event; \ \
55 * | ----------- await close \ \
56 * | ^ \ |
57 * | | \ |
58 * | + con_sock_state_closing() \ |
59 * | / \ | |
60 * | / --------------- | |
61 * | / \ v v
Alex Elderbc18f4b2012-06-20 21:53:53 -050062 * | / --------------
63 * | / -----------------| CONNECTING | socket created, TCP
64 * | | / -------------- connect initiated
65 * | | | con_sock_state_connected()
66 * | | v
67 * -------------
68 * | CONNECTED | TCP connection established
69 * -------------
70 *
71 * State values for ceph_connection->sock_state; NEW is assumed to be 0.
72 */
Alex Elderce2c8902012-05-22 22:15:49 -050073
74#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
75#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
76#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
77#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
78#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
79
Sage Weil8dacc7d2012-07-20 17:24:40 -070080/*
81 * connection states
82 */
83#define CON_STATE_CLOSED 1 /* -> PREOPEN */
84#define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
85#define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
86#define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
87#define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
88#define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
89
Sage Weil4a861692012-07-20 17:29:55 -070090/*
91 * ceph_connection flag bits
92 */
93#define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
94 * messages on errors */
95#define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
96#define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
97#define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
98#define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
Sage Weil8dacc7d2012-07-20 17:24:40 -070099
Sage Weil31b80062009-10-06 11:31:13 -0700100/* static tag bytes (protocol control messages) */
101static char tag_msg = CEPH_MSGR_TAG_MSG;
102static char tag_ack = CEPH_MSGR_TAG_ACK;
103static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
104
Sage Weila6a53492010-04-13 14:07:07 -0700105#ifdef CONFIG_LOCKDEP
106static struct lock_class_key socket_class;
107#endif
108
Alex Elder84495f42012-02-15 07:43:55 -0600109/*
110 * When skipping (ignoring) a block of input we read it into a "skip
111 * buffer," which is this many bytes in size.
112 */
113#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -0700114
115static void queue_con(struct ceph_connection *con);
116static void con_work(struct work_struct *);
117static void ceph_fault(struct ceph_connection *con);
118
Sage Weil31b80062009-10-06 11:31:13 -0700119/*
Alex Elderf64a9312012-01-23 15:49:27 -0600120 * Nicely render a sockaddr as a string. An array of formatted
121 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -0700122 */
Alex Elderf64a9312012-01-23 15:49:27 -0600123#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
124#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
125#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
126#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
127
128static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
129static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -0700130
Alex Elder57666512012-01-23 15:49:27 -0600131static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -0600132
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700133const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -0700134{
135 int i;
136 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -0600137 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
138 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -0700139
Alex Elderf64a9312012-01-23 15:49:27 -0600140 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700141 s = addr_str[i];
142
143 switch (ss->ss_family) {
144 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -0600145 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
146 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700147 break;
148
149 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -0600150 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
151 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700152 break;
153
154 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600155 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
156 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700157 }
158
159 return s;
160}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700161EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700162
Sage Weil63f2d212009-11-03 15:17:56 -0800163static void encode_my_addr(struct ceph_messenger *msgr)
164{
165 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
166 ceph_encode_addr(&msgr->my_enc_addr);
167}
168
Sage Weil31b80062009-10-06 11:31:13 -0700169/*
170 * work queue for all reading and writing to/from the socket.
171 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600172static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700173
Alex Elder6173d1f2012-02-14 14:05:33 -0600174void _ceph_msgr_exit(void)
175{
Alex Elderd3002b92012-02-14 14:05:33 -0600176 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600177 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600178 ceph_msgr_wq = NULL;
179 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600180
Alex Elder6173d1f2012-02-14 14:05:33 -0600181 BUG_ON(zero_page == NULL);
182 kunmap(zero_page);
183 page_cache_release(zero_page);
184 zero_page = NULL;
185}
186
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700187int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700188{
Alex Elder57666512012-01-23 15:49:27 -0600189 BUG_ON(zero_page != NULL);
190 zero_page = ZERO_PAGE(0);
191 page_cache_get(zero_page);
192
Tejun Heof363e452011-01-03 14:49:46 +0100193 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600194 if (ceph_msgr_wq)
195 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600196
Alex Elder6173d1f2012-02-14 14:05:33 -0600197 pr_err("msgr_init failed to create workqueue\n");
198 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600199
Alex Elder6173d1f2012-02-14 14:05:33 -0600200 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700201}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700202EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700203
204void ceph_msgr_exit(void)
205{
Alex Elder57666512012-01-23 15:49:27 -0600206 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600207
Alex Elder6173d1f2012-02-14 14:05:33 -0600208 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700209}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700210EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700211
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700212void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700213{
214 flush_workqueue(ceph_msgr_wq);
215}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700216EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700217
Alex Elderce2c8902012-05-22 22:15:49 -0500218/* Connection socket state transition functions */
219
220static void con_sock_state_init(struct ceph_connection *con)
221{
222 int old_state;
223
224 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
225 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
226 printk("%s: unexpected old state %d\n", __func__, old_state);
227}
228
229static void con_sock_state_connecting(struct ceph_connection *con)
230{
231 int old_state;
232
233 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
234 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
235 printk("%s: unexpected old state %d\n", __func__, old_state);
236}
237
238static void con_sock_state_connected(struct ceph_connection *con)
239{
240 int old_state;
241
242 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
243 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
244 printk("%s: unexpected old state %d\n", __func__, old_state);
245}
246
247static void con_sock_state_closing(struct ceph_connection *con)
248{
249 int old_state;
250
251 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
252 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
253 old_state != CON_SOCK_STATE_CONNECTED &&
254 old_state != CON_SOCK_STATE_CLOSING))
255 printk("%s: unexpected old state %d\n", __func__, old_state);
256}
257
258static void con_sock_state_closed(struct ceph_connection *con)
259{
260 int old_state;
261
262 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
263 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
Sage Weilfbb85a42012-06-27 12:31:02 -0700264 old_state != CON_SOCK_STATE_CLOSING &&
265 old_state != CON_SOCK_STATE_CONNECTING))
Alex Elderce2c8902012-05-22 22:15:49 -0500266 printk("%s: unexpected old state %d\n", __func__, old_state);
267}
Sage Weila922d382010-05-29 09:41:23 -0700268
Sage Weil31b80062009-10-06 11:31:13 -0700269/*
270 * socket callback functions
271 */
272
273/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500274static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700275{
Alex Elderbd406142012-01-23 15:49:27 -0600276 struct ceph_connection *con = sk->sk_user_data;
Guanjun Hea2a32582012-07-08 19:50:33 -0700277 if (atomic_read(&con->msgr->stopping)) {
278 return;
279 }
Alex Elderbd406142012-01-23 15:49:27 -0600280
Sage Weil31b80062009-10-06 11:31:13 -0700281 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500282 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700283 con, con->state);
284 queue_con(con);
285 }
286}
287
288/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500289static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700290{
Alex Elderd3002b92012-02-14 14:05:33 -0600291 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700292
Jim Schutt182fac22012-02-29 08:30:58 -0700293 /* only queue to workqueue if there is data we want to write,
294 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500295 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700296 * doesn't get called again until try_write() fills the socket
297 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
298 * and net/core/stream.c:sk_stream_write_space().
299 */
Sage Weil4a861692012-07-20 17:29:55 -0700300 if (test_bit(CON_FLAG_WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700301 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500302 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700303 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
304 queue_con(con);
305 }
Sage Weil31b80062009-10-06 11:31:13 -0700306 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500307 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700308 }
Sage Weil31b80062009-10-06 11:31:13 -0700309}
310
311/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500312static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700313{
Alex Elderbd406142012-01-23 15:49:27 -0600314 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700315
Alex Elder327800b2012-05-22 11:41:43 -0500316 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700317 con, con->state, sk->sk_state);
318
Sage Weil31b80062009-10-06 11:31:13 -0700319 switch (sk->sk_state) {
320 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500321 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700322 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500323 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500324 con_sock_state_closing(con);
Sage Weil4a861692012-07-20 17:29:55 -0700325 set_bit(CON_FLAG_SOCK_CLOSED, &con->flags);
Alex Elderd65c9e02012-06-20 21:53:53 -0500326 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700327 break;
328 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500329 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500330 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700331 queue_con(con);
332 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600333 default: /* Everything else is uninteresting */
334 break;
Sage Weil31b80062009-10-06 11:31:13 -0700335 }
336}
337
338/*
339 * set up socket callbacks
340 */
341static void set_sock_callbacks(struct socket *sock,
342 struct ceph_connection *con)
343{
344 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600345 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500346 sk->sk_data_ready = ceph_sock_data_ready;
347 sk->sk_write_space = ceph_sock_write_space;
348 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700349}
350
351
352/*
353 * socket helpers
354 */
355
356/*
357 * initiate connection to a remote socket.
358 */
Alex Elder41617d02012-02-14 14:05:33 -0600359static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700360{
Sage Weilf91d3472010-07-01 15:18:31 -0700361 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700362 struct socket *sock;
363 int ret;
364
365 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700366 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
367 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700368 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600369 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700370 sock->sk->sk_allocation = GFP_NOFS;
371
Sage Weila6a53492010-04-13 14:07:07 -0700372#ifdef CONFIG_LOCKDEP
373 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
374#endif
375
Sage Weil31b80062009-10-06 11:31:13 -0700376 set_sock_callbacks(sock, con);
377
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700378 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700379
Sage Weil89a86be2012-06-09 14:19:21 -0700380 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700381 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
382 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700383 if (ret == -EINPROGRESS) {
384 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700385 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700386 sock->sk->sk_state);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600387 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700388 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700389 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700390 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700391 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700392
Alex Elder41617d02012-02-14 14:05:33 -0600393 return ret;
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600394 }
395 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600396 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700397}
398
399static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
400{
401 struct kvec iov = {buf, len};
402 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800403 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700404
Sage Weil98bdb0a2011-01-25 08:17:48 -0800405 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
406 if (r == -EAGAIN)
407 r = 0;
408 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700409}
410
411/*
412 * write something. @more is true if caller will be sending more data
413 * shortly.
414 */
415static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
416 size_t kvlen, size_t len, int more)
417{
418 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800419 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700420
421 if (more)
422 msg.msg_flags |= MSG_MORE;
423 else
424 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
425
Sage Weil42961d22011-01-25 08:19:34 -0800426 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
427 if (r == -EAGAIN)
428 r = 0;
429 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700430}
431
Alex Elder31739132012-03-07 11:40:08 -0600432static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
433 int offset, size_t size, int more)
434{
435 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
436 int ret;
437
438 ret = kernel_sendpage(sock, page, offset, size, flags);
439 if (ret == -EAGAIN)
440 ret = 0;
441
442 return ret;
443}
444
Sage Weil31b80062009-10-06 11:31:13 -0700445
446/*
447 * Shutdown/close the socket for the given connection.
448 */
449static int con_close_socket(struct ceph_connection *con)
450{
451 int rc;
452
453 dout("con_close_socket on %p sock %p\n", con, con->sock);
454 if (!con->sock)
455 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700456 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
457 sock_release(con->sock);
458 con->sock = NULL;
Alex Elder456ea462012-06-20 21:53:53 -0500459
460 /*
Sage Weil4a861692012-07-20 17:29:55 -0700461 * Forcibly clear the SOCK_CLOSED flag. It gets set
Alex Elder456ea462012-06-20 21:53:53 -0500462 * independent of the connection mutex, and we could have
463 * received a socket close event before we had the chance to
464 * shut the socket down.
465 */
Sage Weil4a861692012-07-20 17:29:55 -0700466 clear_bit(CON_FLAG_SOCK_CLOSED, &con->flags);
Alex Elderce2c8902012-05-22 22:15:49 -0500467 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700468 return rc;
469}
470
471/*
472 * Reset a connection. Discard all incoming and outgoing messages
473 * and clear *_seq state.
474 */
475static void ceph_msg_remove(struct ceph_msg *msg)
476{
477 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500478 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -0700479 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500480 msg->con = NULL;
481
Sage Weil31b80062009-10-06 11:31:13 -0700482 ceph_msg_put(msg);
483}
484static void ceph_msg_remove_list(struct list_head *head)
485{
486 while (!list_empty(head)) {
487 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
488 list_head);
489 ceph_msg_remove(msg);
490 }
491}
492
493static void reset_connection(struct ceph_connection *con)
494{
495 /* reset connection, out_queue, msg_ and connect_seq */
496 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700497 ceph_msg_remove_list(&con->out_queue);
498 ceph_msg_remove_list(&con->out_sent);
499
Sage Weilcf3e5c42009-12-11 09:48:05 -0800500 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500501 BUG_ON(con->in_msg->con != con);
502 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800503 ceph_msg_put(con->in_msg);
504 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -0700505 con->ops->put(con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800506 }
507
Sage Weil31b80062009-10-06 11:31:13 -0700508 con->connect_seq = 0;
509 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800510 if (con->out_msg) {
511 ceph_msg_put(con->out_msg);
512 con->out_msg = NULL;
513 }
Sage Weil31b80062009-10-06 11:31:13 -0700514 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700515 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700516}
517
518/*
519 * mark a peer down. drop any open connections.
520 */
521void ceph_con_close(struct ceph_connection *con)
522{
Sage Weil8c50c812012-07-30 16:24:37 -0700523 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700524 dout("con_close %p peer %s\n", con,
525 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700526 con->state = CON_STATE_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500527
Sage Weil4a861692012-07-20 17:29:55 -0700528 clear_bit(CON_FLAG_LOSSYTX, &con->flags); /* so we retry next connect */
529 clear_bit(CON_FLAG_KEEPALIVE_PENDING, &con->flags);
530 clear_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500531
Sage Weil31b80062009-10-06 11:31:13 -0700532 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700533 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800534 cancel_delayed_work(&con->work);
Sage Weilee76e072012-07-20 16:45:49 -0700535 con_close_socket(con);
Sage Weilec302642009-12-22 10:43:42 -0800536 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700537}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700538EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700539
540/*
Sage Weil31b80062009-10-06 11:31:13 -0700541 * Reopen a closed connection, with a new peer address.
542 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700543void ceph_con_open(struct ceph_connection *con,
544 __u8 entity_type, __u64 entity_num,
545 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700546{
Sage Weil54691552012-07-30 16:21:40 -0700547 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700548 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700549
550 BUG_ON(con->state != CON_STATE_CLOSED);
551 con->state = CON_STATE_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500552
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700553 con->peer_name.type = (__u8) entity_type;
554 con->peer_name.num = cpu_to_le64(entity_num);
555
Sage Weil31b80062009-10-06 11:31:13 -0700556 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800557 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700558 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700559 queue_con(con);
560}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700561EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700562
563/*
Sage Weil87b315a2010-03-22 14:51:18 -0700564 * return true if this connection ever successfully opened
565 */
566bool ceph_con_opened(struct ceph_connection *con)
567{
568 return con->connect_seq > 0;
569}
570
571/*
Sage Weil31b80062009-10-06 11:31:13 -0700572 * initialize a new connection.
573 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500574void ceph_con_init(struct ceph_connection *con, void *private,
575 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700576 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700577{
578 dout("con_init %p\n", con);
579 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500580 con->private = private;
581 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700582 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500583
584 con_sock_state_init(con);
585
Sage Weilec302642009-12-22 10:43:42 -0800586 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700587 INIT_LIST_HEAD(&con->out_queue);
588 INIT_LIST_HEAD(&con->out_sent);
589 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500590
Sage Weil8dacc7d2012-07-20 17:24:40 -0700591 con->state = CON_STATE_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700592}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700593EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700594
595
596/*
597 * We maintain a global counter to order connection attempts. Get
598 * a unique seq greater than @gt.
599 */
600static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
601{
602 u32 ret;
603
604 spin_lock(&msgr->global_seq_lock);
605 if (msgr->global_seq < gt)
606 msgr->global_seq = gt;
607 ret = ++msgr->global_seq;
608 spin_unlock(&msgr->global_seq_lock);
609 return ret;
610}
611
Alex Eldere2200422012-05-23 14:35:23 -0500612static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600613{
614 con->out_kvec_left = 0;
615 con->out_kvec_bytes = 0;
616 con->out_kvec_cur = &con->out_kvec[0];
617}
618
Alex Eldere2200422012-05-23 14:35:23 -0500619static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600620 size_t size, void *data)
621{
622 int index;
623
624 index = con->out_kvec_left;
625 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
626
627 con->out_kvec[index].iov_len = size;
628 con->out_kvec[index].iov_base = data;
629 con->out_kvec_left++;
630 con->out_kvec_bytes += size;
631}
Sage Weil31b80062009-10-06 11:31:13 -0700632
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500633#ifdef CONFIG_BLOCK
634static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
635{
636 if (!bio) {
637 *iter = NULL;
638 *seg = 0;
639 return;
640 }
641 *iter = bio;
642 *seg = bio->bi_idx;
643}
644
645static void iter_bio_next(struct bio **bio_iter, int *seg)
646{
647 if (*bio_iter == NULL)
648 return;
649
650 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
651
652 (*seg)++;
653 if (*seg == (*bio_iter)->bi_vcnt)
654 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
655}
656#endif
657
Alex Elder739c9052012-06-11 14:57:13 -0500658static void prepare_write_message_data(struct ceph_connection *con)
659{
660 struct ceph_msg *msg = con->out_msg;
661
662 BUG_ON(!msg);
663 BUG_ON(!msg->hdr.data_len);
664
665 /* initialize page iterator */
666 con->out_msg_pos.page = 0;
667 if (msg->pages)
668 con->out_msg_pos.page_pos = msg->page_alignment;
669 else
670 con->out_msg_pos.page_pos = 0;
Alex Elder572c5882012-06-11 14:57:13 -0500671#ifdef CONFIG_BLOCK
Alex Elderabdaa6a2012-06-11 14:57:13 -0500672 if (msg->bio)
Alex Elder572c5882012-06-11 14:57:13 -0500673 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
674#endif
Alex Elder739c9052012-06-11 14:57:13 -0500675 con->out_msg_pos.data_pos = 0;
676 con->out_msg_pos.did_page_crc = false;
677 con->out_more = 1; /* data + footer will follow */
678}
679
Sage Weil31b80062009-10-06 11:31:13 -0700680/*
681 * Prepare footer for currently outgoing message, and finish things
682 * off. Assumes out_kvec* are already valid.. we just add on to the end.
683 */
Alex Elder859eb792012-02-14 14:05:33 -0600684static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700685{
686 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600687 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700688
Alex Elderfd154f32012-06-11 14:57:13 -0500689 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
690
Sage Weil31b80062009-10-06 11:31:13 -0700691 dout("prepare_write_message_footer %p\n", con);
692 con->out_kvec_is_msg = true;
693 con->out_kvec[v].iov_base = &m->footer;
694 con->out_kvec[v].iov_len = sizeof(m->footer);
695 con->out_kvec_bytes += sizeof(m->footer);
696 con->out_kvec_left++;
697 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800698 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700699}
700
701/*
702 * Prepare headers for the next outgoing message.
703 */
704static void prepare_write_message(struct ceph_connection *con)
705{
706 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600707 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700708
Alex Eldere2200422012-05-23 14:35:23 -0500709 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700710 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800711 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700712
713 /* Sneak an ack in there first? If we can get it into the same
714 * TCP packet that's a good thing. */
715 if (con->in_seq > con->in_seq_acked) {
716 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500717 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700718 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500719 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600720 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700721 }
722
Alex Elder38941f82012-06-01 14:56:43 -0500723 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600724 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800725 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500726 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700727
728 /* put message on sent list */
729 ceph_msg_get(m);
730 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700731
Sage Weile84346b2010-05-11 21:20:38 -0700732 /*
733 * only assign outgoing seq # if we haven't sent this message
734 * yet. if it is requeued, resend with it's original seq.
735 */
736 if (m->needs_out_seq) {
737 m->hdr.seq = cpu_to_le64(++con->out_seq);
738 m->needs_out_seq = false;
739 }
Sage Weil31b80062009-10-06 11:31:13 -0700740
741 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
742 m, con->out_seq, le16_to_cpu(m->hdr.type),
743 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
744 le32_to_cpu(m->hdr.data_len),
745 m->nr_pages);
746 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
747
748 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500749 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
750 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
751 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600752
Sage Weil31b80062009-10-06 11:31:13 -0700753 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500754 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600755 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700756
757 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600758 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
759 con->out_msg->hdr.crc = cpu_to_le32(crc);
Alex Elderfd154f32012-06-11 14:57:13 -0500760 con->out_msg->footer.flags = 0;
Alex Eldera9a0c512012-02-15 07:43:54 -0600761
762 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
763 con->out_msg->footer.front_crc = cpu_to_le32(crc);
764 if (m->middle) {
765 crc = crc32c(0, m->middle->vec.iov_base,
766 m->middle->vec.iov_len);
767 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
768 } else
Sage Weil31b80062009-10-06 11:31:13 -0700769 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -0500770 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700771 le32_to_cpu(con->out_msg->footer.front_crc),
772 le32_to_cpu(con->out_msg->footer.middle_crc));
773
774 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -0500775 con->out_msg->footer.data_crc = 0;
776 if (m->hdr.data_len)
777 prepare_write_message_data(con);
778 else
Sage Weil31b80062009-10-06 11:31:13 -0700779 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600780 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700781
Sage Weil4a861692012-07-20 17:29:55 -0700782 set_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700783}
784
785/*
786 * Prepare an ack.
787 */
788static void prepare_write_ack(struct ceph_connection *con)
789{
790 dout("prepare_write_ack %p %llu -> %llu\n", con,
791 con->in_seq_acked, con->in_seq);
792 con->in_seq_acked = con->in_seq;
793
Alex Eldere2200422012-05-23 14:35:23 -0500794 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600795
Alex Eldere2200422012-05-23 14:35:23 -0500796 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600797
Sage Weil31b80062009-10-06 11:31:13 -0700798 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500799 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600800 &con->out_temp_ack);
801
Sage Weil31b80062009-10-06 11:31:13 -0700802 con->out_more = 1; /* more will follow.. eventually.. */
Sage Weil4a861692012-07-20 17:29:55 -0700803 set_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700804}
805
806/*
807 * Prepare to write keepalive byte.
808 */
809static void prepare_write_keepalive(struct ceph_connection *con)
810{
811 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500812 con_out_kvec_reset(con);
813 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Sage Weil4a861692012-07-20 17:29:55 -0700814 set_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700815}
816
817/*
818 * Connection negotiation.
819 */
820
Alex Elderdac1e712012-05-16 15:16:39 -0500821static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
822 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800823{
Alex Eldera3530df2012-05-16 15:16:39 -0500824 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500825
826 if (!con->ops->get_authorizer) {
827 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
828 con->out_connect.authorizer_len = 0;
Alex Elder729796b2012-05-16 15:16:39 -0500829 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500830 }
831
832 /* Can't hold the mutex while getting authorizer */
Sage Weilec302642009-12-22 10:43:42 -0800833 mutex_unlock(&con->mutex);
Alex Elderdac1e712012-05-16 15:16:39 -0500834 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800835 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800836
Alex Eldera3530df2012-05-16 15:16:39 -0500837 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500838 return auth;
Sage Weil8dacc7d2012-07-20 17:24:40 -0700839 if (con->state != CON_STATE_NEGOTIATING)
Alex Elder729796b2012-05-16 15:16:39 -0500840 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700841
Alex Elder8f43fb52012-05-16 15:16:39 -0500842 con->auth_reply_buf = auth->authorizer_reply_buf;
843 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
Alex Elder729796b2012-05-16 15:16:39 -0500844 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800845}
846
Sage Weil31b80062009-10-06 11:31:13 -0700847/*
848 * We connected to a peer and are saying hello.
849 */
Alex Eldere825a662012-05-16 15:16:38 -0500850static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700851{
Alex Eldere2200422012-05-23 14:35:23 -0500852 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
853 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500854 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800855
Sage Weileed0ef22009-11-10 14:34:36 -0800856 con->out_more = 0;
Sage Weil4a861692012-07-20 17:29:55 -0700857 set_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800858}
859
Alex Eldere825a662012-05-16 15:16:38 -0500860static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800861{
Eric Dumazet95c96172012-04-15 05:58:06 +0000862 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700863 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500864 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500865 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700866
867 switch (con->peer_name.type) {
868 case CEPH_ENTITY_TYPE_MON:
869 proto = CEPH_MONC_PROTOCOL;
870 break;
871 case CEPH_ENTITY_TYPE_OSD:
872 proto = CEPH_OSDC_PROTOCOL;
873 break;
874 case CEPH_ENTITY_TYPE_MDS:
875 proto = CEPH_MDSC_PROTOCOL;
876 break;
877 default:
878 BUG();
879 }
880
881 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
882 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800883
Alex Eldere825a662012-05-16 15:16:38 -0500884 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700885 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
886 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
887 con->out_connect.global_seq = cpu_to_le32(global_seq);
888 con->out_connect.protocol_version = cpu_to_le32(proto);
889 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700890
Alex Elderdac1e712012-05-16 15:16:39 -0500891 auth_proto = CEPH_AUTH_UNKNOWN;
892 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500893 if (IS_ERR(auth))
894 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500895
Alex Elderdac1e712012-05-16 15:16:39 -0500896 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500897 con->out_connect.authorizer_len = auth ?
898 cpu_to_le32(auth->authorizer_buf_len) : 0;
899
Alex Elderab166d52012-05-31 11:37:29 -0500900 con_out_kvec_reset(con);
Alex Eldere2200422012-05-23 14:35:23 -0500901 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500902 &con->out_connect);
903 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500904 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500905 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600906
Sage Weil31b80062009-10-06 11:31:13 -0700907 con->out_more = 0;
Sage Weil4a861692012-07-20 17:29:55 -0700908 set_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800909
Alex Eldere10c7582012-05-16 15:16:38 -0500910 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700911}
912
Sage Weil31b80062009-10-06 11:31:13 -0700913/*
914 * write as much of pending kvecs to the socket as we can.
915 * 1 -> done
916 * 0 -> socket full, but more to do
917 * <0 -> error
918 */
919static int write_partial_kvec(struct ceph_connection *con)
920{
921 int ret;
922
923 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
924 while (con->out_kvec_bytes > 0) {
925 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
926 con->out_kvec_left, con->out_kvec_bytes,
927 con->out_more);
928 if (ret <= 0)
929 goto out;
930 con->out_kvec_bytes -= ret;
931 if (con->out_kvec_bytes == 0)
932 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600933
934 /* account for full iov entries consumed */
935 while (ret >= con->out_kvec_cur->iov_len) {
936 BUG_ON(!con->out_kvec_left);
937 ret -= con->out_kvec_cur->iov_len;
938 con->out_kvec_cur++;
939 con->out_kvec_left--;
940 }
941 /* and for a partially-consumed entry */
942 if (ret) {
943 con->out_kvec_cur->iov_len -= ret;
944 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700945 }
946 }
947 con->out_kvec_left = 0;
948 con->out_kvec_is_msg = false;
949 ret = 1;
950out:
951 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
952 con->out_kvec_bytes, con->out_kvec_left, ret);
953 return ret; /* done! */
954}
955
Alex Elder84ca8fc2012-06-11 14:57:13 -0500956static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
957 size_t len, size_t sent, bool in_trail)
958{
959 struct ceph_msg *msg = con->out_msg;
960
961 BUG_ON(!msg);
962 BUG_ON(!sent);
963
964 con->out_msg_pos.data_pos += sent;
965 con->out_msg_pos.page_pos += sent;
Alex Elder5821bd82012-06-11 14:57:13 -0500966 if (sent < len)
967 return;
968
969 BUG_ON(sent != len);
970 con->out_msg_pos.page_pos = 0;
971 con->out_msg_pos.page++;
972 con->out_msg_pos.did_page_crc = false;
973 if (in_trail)
974 list_move_tail(&page->lru,
975 &msg->trail->head);
976 else if (msg->pagelist)
977 list_move_tail(&page->lru,
978 &msg->pagelist->head);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500979#ifdef CONFIG_BLOCK
Alex Elder5821bd82012-06-11 14:57:13 -0500980 else if (msg->bio)
981 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500982#endif
Alex Elder84ca8fc2012-06-11 14:57:13 -0500983}
984
Sage Weil31b80062009-10-06 11:31:13 -0700985/*
986 * Write as much message data payload as we can. If we finish, queue
987 * up the footer.
988 * 1 -> done, footer is now queued in out_kvec[].
989 * 0 -> socket full, but more to do
990 * <0 -> error
991 */
992static int write_partial_msg_pages(struct ceph_connection *con)
993{
994 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +0000995 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -0700996 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600997 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700998 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700999 int total_max_write;
Alex Elder84ca8fc2012-06-11 14:57:13 -05001000 bool in_trail = false;
Alex Elder5821bd82012-06-11 14:57:13 -05001001 const size_t trail_len = (msg->trail ? msg->trail->length : 0);
1002 const size_t trail_off = data_len - trail_len;
Sage Weil31b80062009-10-06 11:31:13 -07001003
1004 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
Alex Elder84ca8fc2012-06-11 14:57:13 -05001005 con, msg, con->out_msg_pos.page, msg->nr_pages,
Sage Weil31b80062009-10-06 11:31:13 -07001006 con->out_msg_pos.page_pos);
1007
Alex Elder5821bd82012-06-11 14:57:13 -05001008 /*
1009 * Iterate through each page that contains data to be
1010 * written, and send as much as possible for each.
1011 *
1012 * If we are calculating the data crc (the default), we will
1013 * need to map the page. If we have no pages, they have
1014 * been revoked, so use the zero page.
1015 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001016 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -07001017 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001018 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -06001019 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001020
Alex Elder5821bd82012-06-11 14:57:13 -05001021 in_trail = in_trail || con->out_msg_pos.data_pos >= trail_off;
1022 if (!in_trail)
1023 total_max_write = trail_off - con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001024
Alex Elder5821bd82012-06-11 14:57:13 -05001025 if (in_trail) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001026 total_max_write = data_len - con->out_msg_pos.data_pos;
1027
1028 page = list_first_entry(&msg->trail->head,
1029 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001030 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -07001031 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -08001032 } else if (msg->pagelist) {
1033 page = list_first_entry(&msg->pagelist->head,
1034 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001035#ifdef CONFIG_BLOCK
1036 } else if (msg->bio) {
1037 struct bio_vec *bv;
1038
1039 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
1040 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -06001041 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001042 max_write = bv->bv_len;
1043#endif
Sage Weil31b80062009-10-06 11:31:13 -07001044 } else {
Alex Elder57666512012-01-23 15:49:27 -06001045 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -07001046 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001047 len = min_t(int, max_write - con->out_msg_pos.page_pos,
1048 total_max_write);
1049
Alex Elder37675b02012-03-07 11:40:08 -06001050 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -06001051 void *base;
Alex Elder5821bd82012-06-11 14:57:13 -05001052 u32 crc = le32_to_cpu(msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -06001053 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -07001054
Alex Elder8d63e312012-03-07 11:40:08 -06001055 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -07001056 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -06001057 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Elder5821bd82012-06-11 14:57:13 -05001058 crc = crc32c(crc, base, len);
Alex Elder84ca8fc2012-06-11 14:57:13 -05001059 msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -06001060 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -07001061 }
Alex Eldere36b13c2012-03-07 11:40:08 -06001062 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -06001063 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -06001064 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001065
Alex Elder0cdf9e62012-03-07 11:40:08 -06001066 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001067 kunmap(page);
1068
1069 if (ret <= 0)
1070 goto out;
1071
Alex Elder84ca8fc2012-06-11 14:57:13 -05001072 out_msg_pos_next(con, page, len, (size_t) ret, in_trail);
Sage Weil31b80062009-10-06 11:31:13 -07001073 }
1074
1075 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1076
1077 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001078 if (!do_datacrc)
Alex Elder84ca8fc2012-06-11 14:57:13 -05001079 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001080 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001081 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001082 ret = 1;
1083out:
1084 return ret;
1085}
1086
1087/*
1088 * write some zeros
1089 */
1090static int write_partial_skip(struct ceph_connection *con)
1091{
1092 int ret;
1093
1094 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001095 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001096
Alex Elder31739132012-03-07 11:40:08 -06001097 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001098 if (ret <= 0)
1099 goto out;
1100 con->out_skip -= ret;
1101 }
1102 ret = 1;
1103out:
1104 return ret;
1105}
1106
1107/*
1108 * Prepare to read connection handshake, or an ack.
1109 */
Sage Weileed0ef22009-11-10 14:34:36 -08001110static void prepare_read_banner(struct ceph_connection *con)
1111{
1112 dout("prepare_read_banner %p\n", con);
1113 con->in_base_pos = 0;
1114}
1115
Sage Weil31b80062009-10-06 11:31:13 -07001116static void prepare_read_connect(struct ceph_connection *con)
1117{
1118 dout("prepare_read_connect %p\n", con);
1119 con->in_base_pos = 0;
1120}
1121
1122static void prepare_read_ack(struct ceph_connection *con)
1123{
1124 dout("prepare_read_ack %p\n", con);
1125 con->in_base_pos = 0;
1126}
1127
1128static void prepare_read_tag(struct ceph_connection *con)
1129{
1130 dout("prepare_read_tag %p\n", con);
1131 con->in_base_pos = 0;
1132 con->in_tag = CEPH_MSGR_TAG_READY;
1133}
1134
1135/*
1136 * Prepare to read a message.
1137 */
1138static int prepare_read_message(struct ceph_connection *con)
1139{
1140 dout("prepare_read_message %p\n", con);
1141 BUG_ON(con->in_msg != NULL);
1142 con->in_base_pos = 0;
1143 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1144 return 0;
1145}
1146
1147
1148static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001149 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001150{
Alex Eldere6cee712012-05-10 10:29:50 -05001151 while (con->in_base_pos < end) {
1152 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001153 int have = size - left;
1154 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1155 if (ret <= 0)
1156 return ret;
1157 con->in_base_pos += ret;
1158 }
1159 return 1;
1160}
1161
1162
1163/*
1164 * Read all or part of the connect-side handshake on a new connection
1165 */
Sage Weileed0ef22009-11-10 14:34:36 -08001166static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001167{
Alex Elderfd516532012-05-10 10:29:50 -05001168 int size;
1169 int end;
1170 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001171
Sage Weileed0ef22009-11-10 14:34:36 -08001172 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001173
1174 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001175 size = strlen(CEPH_BANNER);
1176 end = size;
1177 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001178 if (ret <= 0)
1179 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001180
1181 size = sizeof (con->actual_peer_addr);
1182 end += size;
1183 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001184 if (ret <= 0)
1185 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001186
1187 size = sizeof (con->peer_addr_for_me);
1188 end += size;
1189 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001190 if (ret <= 0)
1191 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001192
Sage Weileed0ef22009-11-10 14:34:36 -08001193out:
1194 return ret;
1195}
1196
1197static int read_partial_connect(struct ceph_connection *con)
1198{
Alex Elderfd516532012-05-10 10:29:50 -05001199 int size;
1200 int end;
1201 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001202
1203 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1204
Alex Elderfd516532012-05-10 10:29:50 -05001205 size = sizeof (con->in_reply);
1206 end = size;
1207 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001208 if (ret <= 0)
1209 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001210
1211 size = le32_to_cpu(con->in_reply.authorizer_len);
1212 end += size;
1213 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001214 if (ret <= 0)
1215 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001216
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001217 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1218 con, (int)con->in_reply.tag,
1219 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001220 le32_to_cpu(con->in_reply.global_seq));
1221out:
1222 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001223
Sage Weil31b80062009-10-06 11:31:13 -07001224}
1225
1226/*
1227 * Verify the hello banner looks okay.
1228 */
1229static int verify_hello(struct ceph_connection *con)
1230{
1231 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001232 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001233 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001234 con->error_msg = "protocol error, bad banner";
1235 return -1;
1236 }
1237 return 0;
1238}
1239
1240static bool addr_is_blank(struct sockaddr_storage *ss)
1241{
1242 switch (ss->ss_family) {
1243 case AF_INET:
1244 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1245 case AF_INET6:
1246 return
1247 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1248 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1249 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1250 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1251 }
1252 return false;
1253}
1254
1255static int addr_port(struct sockaddr_storage *ss)
1256{
1257 switch (ss->ss_family) {
1258 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001259 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001260 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001261 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001262 }
1263 return 0;
1264}
1265
1266static void addr_set_port(struct sockaddr_storage *ss, int p)
1267{
1268 switch (ss->ss_family) {
1269 case AF_INET:
1270 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001271 break;
Sage Weil31b80062009-10-06 11:31:13 -07001272 case AF_INET6:
1273 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001274 break;
Sage Weil31b80062009-10-06 11:31:13 -07001275 }
1276}
1277
1278/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001279 * Unlike other *_pton function semantics, zero indicates success.
1280 */
1281static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1282 char delim, const char **ipend)
1283{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001284 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1285 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001286
1287 memset(ss, 0, sizeof(*ss));
1288
1289 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1290 ss->ss_family = AF_INET;
1291 return 0;
1292 }
1293
1294 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1295 ss->ss_family = AF_INET6;
1296 return 0;
1297 }
1298
1299 return -EINVAL;
1300}
1301
1302/*
1303 * Extract hostname string and resolve using kernel DNS facility.
1304 */
1305#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1306static int ceph_dns_resolve_name(const char *name, size_t namelen,
1307 struct sockaddr_storage *ss, char delim, const char **ipend)
1308{
1309 const char *end, *delim_p;
1310 char *colon_p, *ip_addr = NULL;
1311 int ip_len, ret;
1312
1313 /*
1314 * The end of the hostname occurs immediately preceding the delimiter or
1315 * the port marker (':') where the delimiter takes precedence.
1316 */
1317 delim_p = memchr(name, delim, namelen);
1318 colon_p = memchr(name, ':', namelen);
1319
1320 if (delim_p && colon_p)
1321 end = delim_p < colon_p ? delim_p : colon_p;
1322 else if (!delim_p && colon_p)
1323 end = colon_p;
1324 else {
1325 end = delim_p;
1326 if (!end) /* case: hostname:/ */
1327 end = name + namelen;
1328 }
1329
1330 if (end <= name)
1331 return -EINVAL;
1332
1333 /* do dns_resolve upcall */
1334 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1335 if (ip_len > 0)
1336 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1337 else
1338 ret = -ESRCH;
1339
1340 kfree(ip_addr);
1341
1342 *ipend = end;
1343
1344 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1345 ret, ret ? "failed" : ceph_pr_addr(ss));
1346
1347 return ret;
1348}
1349#else
1350static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1351 struct sockaddr_storage *ss, char delim, const char **ipend)
1352{
1353 return -EINVAL;
1354}
1355#endif
1356
1357/*
1358 * Parse a server name (IP or hostname). If a valid IP address is not found
1359 * then try to extract a hostname to resolve using userspace DNS upcall.
1360 */
1361static int ceph_parse_server_name(const char *name, size_t namelen,
1362 struct sockaddr_storage *ss, char delim, const char **ipend)
1363{
1364 int ret;
1365
1366 ret = ceph_pton(name, namelen, ss, delim, ipend);
1367 if (ret)
1368 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1369
1370 return ret;
1371}
1372
1373/*
Sage Weil31b80062009-10-06 11:31:13 -07001374 * Parse an ip[:port] list into an addr array. Use the default
1375 * monitor port if a port isn't specified.
1376 */
1377int ceph_parse_ips(const char *c, const char *end,
1378 struct ceph_entity_addr *addr,
1379 int max_count, int *count)
1380{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001381 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001382 const char *p = c;
1383
1384 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1385 for (i = 0; i < max_count; i++) {
1386 const char *ipend;
1387 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001388 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001389 char delim = ',';
1390
1391 if (*p == '[') {
1392 delim = ']';
1393 p++;
1394 }
Sage Weil31b80062009-10-06 11:31:13 -07001395
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001396 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1397 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001398 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001399 ret = -EINVAL;
1400
Sage Weil31b80062009-10-06 11:31:13 -07001401 p = ipend;
1402
Sage Weil39139f62010-07-08 09:54:52 -07001403 if (delim == ']') {
1404 if (*p != ']') {
1405 dout("missing matching ']'\n");
1406 goto bad;
1407 }
1408 p++;
1409 }
1410
Sage Weil31b80062009-10-06 11:31:13 -07001411 /* port? */
1412 if (p < end && *p == ':') {
1413 port = 0;
1414 p++;
1415 while (p < end && *p >= '0' && *p <= '9') {
1416 port = (port * 10) + (*p - '0');
1417 p++;
1418 }
1419 if (port > 65535 || port == 0)
1420 goto bad;
1421 } else {
1422 port = CEPH_MON_PORT;
1423 }
1424
1425 addr_set_port(ss, port);
1426
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001427 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001428
1429 if (p == end)
1430 break;
1431 if (*p != ',')
1432 goto bad;
1433 p++;
1434 }
1435
1436 if (p != end)
1437 goto bad;
1438
1439 if (count)
1440 *count = i + 1;
1441 return 0;
1442
1443bad:
Sage Weil39139f62010-07-08 09:54:52 -07001444 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001445 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001446}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001447EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001448
Sage Weileed0ef22009-11-10 14:34:36 -08001449static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001450{
Sage Weileed0ef22009-11-10 14:34:36 -08001451 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001452
1453 if (verify_hello(con) < 0)
1454 return -1;
1455
Sage Weil63f2d212009-11-03 15:17:56 -08001456 ceph_decode_addr(&con->actual_peer_addr);
1457 ceph_decode_addr(&con->peer_addr_for_me);
1458
Sage Weil31b80062009-10-06 11:31:13 -07001459 /*
1460 * Make sure the other end is who we wanted. note that the other
1461 * end may not yet know their ip address, so if it's 0.0.0.0, give
1462 * them the benefit of the doubt.
1463 */
Sage Weil103e2d32010-01-07 16:12:36 -08001464 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1465 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001466 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1467 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001468 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001469 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001470 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001471 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001472 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001473 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001474 return -1;
1475 }
1476
1477 /*
1478 * did we learn our address?
1479 */
1480 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1481 int port = addr_port(&con->msgr->inst.addr.in_addr);
1482
1483 memcpy(&con->msgr->inst.addr.in_addr,
1484 &con->peer_addr_for_me.in_addr,
1485 sizeof(con->peer_addr_for_me.in_addr));
1486 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001487 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001488 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001489 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001490 }
1491
Sage Weileed0ef22009-11-10 14:34:36 -08001492 return 0;
1493}
1494
Sage Weil04a419f2009-12-23 09:30:21 -08001495static void fail_protocol(struct ceph_connection *con)
1496{
1497 reset_connection(con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07001498 BUG_ON(con->state != CON_STATE_NEGOTIATING);
1499 con->state = CON_STATE_CLOSED;
Sage Weil04a419f2009-12-23 09:30:21 -08001500}
1501
Sage Weileed0ef22009-11-10 14:34:36 -08001502static int process_connect(struct ceph_connection *con)
1503{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001504 u64 sup_feat = con->msgr->supported_features;
1505 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001506 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001507 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001508
Sage Weileed0ef22009-11-10 14:34:36 -08001509 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1510
Sage Weil31b80062009-10-06 11:31:13 -07001511 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001512 case CEPH_MSGR_TAG_FEATURES:
1513 pr_err("%s%lld %s feature set mismatch,"
1514 " my %llx < server's %llx, missing %llx\n",
1515 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001516 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001517 sup_feat, server_feat, server_feat & ~sup_feat);
1518 con->error_msg = "missing required protocol features";
1519 fail_protocol(con);
1520 return -1;
1521
Sage Weil31b80062009-10-06 11:31:13 -07001522 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001523 pr_err("%s%lld %s protocol version mismatch,"
1524 " my %d != server's %d\n",
1525 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001526 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001527 le32_to_cpu(con->out_connect.protocol_version),
1528 le32_to_cpu(con->in_reply.protocol_version));
1529 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001530 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001531 return -1;
1532
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001533 case CEPH_MSGR_TAG_BADAUTHORIZER:
1534 con->auth_retry++;
1535 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1536 con->auth_retry);
1537 if (con->auth_retry == 2) {
1538 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001539 return -1;
1540 }
1541 con->auth_retry = 1;
Alex Eldere825a662012-05-16 15:16:38 -05001542 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001543 if (ret < 0)
1544 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001545 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001546 break;
Sage Weil31b80062009-10-06 11:31:13 -07001547
1548 case CEPH_MSGR_TAG_RESETSESSION:
1549 /*
1550 * If we connected with a large connect_seq but the peer
1551 * has no record of a session with us (no connection, or
1552 * connect_seq == 0), they will send RESETSESION to indicate
1553 * that they must have reset their session, and may have
1554 * dropped messages.
1555 */
1556 dout("process_connect got RESET peer seq %u\n",
Sage Weila16cb1f2012-07-10 11:53:34 -07001557 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07001558 pr_err("%s%lld %s connection reset\n",
1559 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001560 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001561 reset_connection(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001562 ret = prepare_write_connect(con);
1563 if (ret < 0)
1564 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001565 prepare_read_connect(con);
1566
1567 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001568 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001569 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1570 if (con->ops->peer_reset)
1571 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001572 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07001573 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07001574 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001575 break;
1576
1577 case CEPH_MSGR_TAG_RETRY_SESSION:
1578 /*
1579 * If we sent a smaller connect_seq than the peer has, try
1580 * again with a larger value.
1581 */
Sage Weila16cb1f2012-07-10 11:53:34 -07001582 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001583 le32_to_cpu(con->out_connect.connect_seq),
Sage Weila16cb1f2012-07-10 11:53:34 -07001584 le32_to_cpu(con->in_reply.connect_seq));
1585 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001586 ret = prepare_write_connect(con);
1587 if (ret < 0)
1588 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001589 prepare_read_connect(con);
1590 break;
1591
1592 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1593 /*
1594 * If we sent a smaller global_seq than the peer has, try
1595 * again with a larger value.
1596 */
Sage Weileed0ef22009-11-10 14:34:36 -08001597 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001598 con->peer_global_seq,
Sage Weila16cb1f2012-07-10 11:53:34 -07001599 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07001600 get_global_seq(con->msgr,
Sage Weila16cb1f2012-07-10 11:53:34 -07001601 le32_to_cpu(con->in_reply.global_seq));
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001602 ret = prepare_write_connect(con);
1603 if (ret < 0)
1604 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001605 prepare_read_connect(con);
1606 break;
1607
1608 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001609 if (req_feat & ~server_feat) {
1610 pr_err("%s%lld %s protocol feature mismatch,"
1611 " my required %llx > server's %llx, need %llx\n",
1612 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001613 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001614 req_feat, server_feat, req_feat & ~server_feat);
1615 con->error_msg = "missing required protocol features";
1616 fail_protocol(con);
1617 return -1;
1618 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07001619
1620 BUG_ON(con->state != CON_STATE_NEGOTIATING);
1621 con->state = CON_STATE_OPEN;
1622
Sage Weil31b80062009-10-06 11:31:13 -07001623 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1624 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001625 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001626 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1627 con->peer_global_seq,
1628 le32_to_cpu(con->in_reply.connect_seq),
1629 con->connect_seq);
1630 WARN_ON(con->connect_seq !=
1631 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001632
1633 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Sage Weil4a861692012-07-20 17:29:55 -07001634 set_bit(CON_FLAG_LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001635
Sage Weil85effe12012-07-30 16:22:05 -07001636 con->delay = 0; /* reset backoff memory */
1637
Sage Weil31b80062009-10-06 11:31:13 -07001638 prepare_read_tag(con);
1639 break;
1640
1641 case CEPH_MSGR_TAG_WAIT:
1642 /*
1643 * If there is a connection race (we are opening
1644 * connections to each other), one of us may just have
1645 * to WAIT. This shouldn't happen if we are the
1646 * client.
1647 */
Sage Weil04177882011-05-12 15:33:17 -07001648 pr_err("process_connect got WAIT as client\n");
1649 con->error_msg = "protocol error, got WAIT as client";
1650 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001651
1652 default:
1653 pr_err("connect protocol error, will retry\n");
1654 con->error_msg = "protocol error, garbage tag during connect";
1655 return -1;
1656 }
1657 return 0;
1658}
1659
1660
1661/*
1662 * read (part of) an ack
1663 */
1664static int read_partial_ack(struct ceph_connection *con)
1665{
Alex Elderfd516532012-05-10 10:29:50 -05001666 int size = sizeof (con->in_temp_ack);
1667 int end = size;
1668
1669 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001670}
1671
1672
1673/*
1674 * We can finally discard anything that's been acked.
1675 */
1676static void process_ack(struct ceph_connection *con)
1677{
1678 struct ceph_msg *m;
1679 u64 ack = le64_to_cpu(con->in_temp_ack);
1680 u64 seq;
1681
Sage Weil31b80062009-10-06 11:31:13 -07001682 while (!list_empty(&con->out_sent)) {
1683 m = list_first_entry(&con->out_sent, struct ceph_msg,
1684 list_head);
1685 seq = le64_to_cpu(m->hdr.seq);
1686 if (seq > ack)
1687 break;
1688 dout("got ack for seq %llu type %d at %p\n", seq,
1689 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001690 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001691 ceph_msg_remove(m);
1692 }
Sage Weil31b80062009-10-06 11:31:13 -07001693 prepare_read_tag(con);
1694}
1695
1696
1697
1698
Yehuda Sadeh24504182010-01-08 13:58:34 -08001699static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001700 struct kvec *section,
1701 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001702{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001703 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001704
Yehuda Sadeh24504182010-01-08 13:58:34 -08001705 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001706
Yehuda Sadeh24504182010-01-08 13:58:34 -08001707 while (section->iov_len < sec_len) {
1708 BUG_ON(section->iov_base == NULL);
1709 left = sec_len - section->iov_len;
1710 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1711 section->iov_len, left);
1712 if (ret <= 0)
1713 return ret;
1714 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001715 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001716 if (section->iov_len == sec_len)
1717 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001718
1719 return 1;
1720}
1721
Alex Elder1c20f2d2012-06-04 14:43:32 -05001722static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1723 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001724
1725
1726static int read_partial_message_pages(struct ceph_connection *con,
1727 struct page **pages,
Eric Dumazet95c96172012-04-15 05:58:06 +00001728 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001729{
1730 void *p;
1731 int ret;
1732 int left;
1733
1734 left = min((int)(data_len - con->in_msg_pos.data_pos),
1735 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1736 /* (page) data */
1737 BUG_ON(pages == NULL);
1738 p = kmap(pages[con->in_msg_pos.page]);
1739 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1740 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001741 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001742 con->in_data_crc =
1743 crc32c(con->in_data_crc,
1744 p + con->in_msg_pos.page_pos, ret);
1745 kunmap(pages[con->in_msg_pos.page]);
1746 if (ret <= 0)
1747 return ret;
1748 con->in_msg_pos.data_pos += ret;
1749 con->in_msg_pos.page_pos += ret;
1750 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1751 con->in_msg_pos.page_pos = 0;
1752 con->in_msg_pos.page++;
1753 }
1754
1755 return ret;
1756}
1757
1758#ifdef CONFIG_BLOCK
1759static int read_partial_message_bio(struct ceph_connection *con,
1760 struct bio **bio_iter, int *bio_seg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001761 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001762{
1763 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1764 void *p;
1765 int ret, left;
1766
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001767 left = min((int)(data_len - con->in_msg_pos.data_pos),
1768 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1769
1770 p = kmap(bv->bv_page) + bv->bv_offset;
1771
1772 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1773 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001774 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001775 con->in_data_crc =
1776 crc32c(con->in_data_crc,
1777 p + con->in_msg_pos.page_pos, ret);
1778 kunmap(bv->bv_page);
1779 if (ret <= 0)
1780 return ret;
1781 con->in_msg_pos.data_pos += ret;
1782 con->in_msg_pos.page_pos += ret;
1783 if (con->in_msg_pos.page_pos == bv->bv_len) {
1784 con->in_msg_pos.page_pos = 0;
1785 iter_bio_next(bio_iter, bio_seg);
1786 }
1787
1788 return ret;
1789}
1790#endif
1791
Sage Weil31b80062009-10-06 11:31:13 -07001792/*
1793 * read (part of) a message.
1794 */
1795static int read_partial_message(struct ceph_connection *con)
1796{
1797 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001798 int size;
1799 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001800 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001801 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001802 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001803 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001804 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001805
1806 dout("read_partial_message con %p msg %p\n", con, m);
1807
1808 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001809 size = sizeof (con->in_hdr);
1810 end = size;
1811 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001812 if (ret <= 0)
1813 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001814
1815 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1816 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1817 pr_err("read_partial_message bad hdr "
1818 " crc %u != expected %u\n",
1819 crc, con->in_hdr.crc);
1820 return -EBADMSG;
1821 }
1822
Sage Weil31b80062009-10-06 11:31:13 -07001823 front_len = le32_to_cpu(con->in_hdr.front_len);
1824 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1825 return -EIO;
1826 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1827 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1828 return -EIO;
1829 data_len = le32_to_cpu(con->in_hdr.data_len);
1830 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1831 return -EIO;
1832
Sage Weilae187562010-04-22 07:47:01 -07001833 /* verify seq# */
1834 seq = le64_to_cpu(con->in_hdr.seq);
1835 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001836 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001837 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001838 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001839 seq, con->in_seq + 1);
1840 con->in_base_pos = -front_len - middle_len - data_len -
1841 sizeof(m->footer);
1842 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001843 return 0;
1844 } else if ((s64)seq - (s64)con->in_seq > 1) {
1845 pr_err("read_partial_message bad seq %lld expected %lld\n",
1846 seq, con->in_seq + 1);
1847 con->error_msg = "bad message sequence # for incoming message";
1848 return -EBADMSG;
1849 }
1850
Sage Weil31b80062009-10-06 11:31:13 -07001851 /* allocate message? */
1852 if (!con->in_msg) {
1853 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1854 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001855 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001856 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001857 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001858 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001859 con->in_base_pos = -front_len - middle_len - data_len -
1860 sizeof(m->footer);
1861 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001862 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001863 return 0;
1864 }
Sage Weila79832f2010-04-01 16:06:19 -07001865 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001866 con->error_msg =
1867 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001868 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001869 }
Alex Elder38941f82012-06-01 14:56:43 -05001870
1871 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001872 m = con->in_msg;
1873 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001874 if (m->middle)
1875 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001876
1877 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001878 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001879 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001880 else
1881 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001882 con->in_msg_pos.data_pos = 0;
Sage Weila4107022012-07-30 16:20:25 -07001883
1884#ifdef CONFIG_BLOCK
1885 if (m->bio)
1886 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1887#endif
Sage Weil31b80062009-10-06 11:31:13 -07001888 }
1889
1890 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001891 ret = read_partial_message_section(con, &m->front, front_len,
1892 &con->in_front_crc);
1893 if (ret <= 0)
1894 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001895
1896 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001897 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001898 ret = read_partial_message_section(con, &m->middle->vec,
1899 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001900 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001901 if (ret <= 0)
1902 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001903 }
1904
1905 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001906 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001907 if (m->pages) {
1908 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001909 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001910 if (ret <= 0)
1911 return ret;
1912#ifdef CONFIG_BLOCK
1913 } else if (m->bio) {
Sage Weila4107022012-07-30 16:20:25 -07001914 BUG_ON(!m->bio_iter);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001915 ret = read_partial_message_bio(con,
1916 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001917 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001918 if (ret <= 0)
1919 return ret;
1920#endif
1921 } else {
1922 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001923 }
1924 }
1925
Sage Weil31b80062009-10-06 11:31:13 -07001926 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001927 size = sizeof (m->footer);
1928 end += size;
1929 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001930 if (ret <= 0)
1931 return ret;
1932
Sage Weil31b80062009-10-06 11:31:13 -07001933 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1934 m, front_len, m->footer.front_crc, middle_len,
1935 m->footer.middle_crc, data_len, m->footer.data_crc);
1936
1937 /* crc ok? */
1938 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1939 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1940 m, con->in_front_crc, m->footer.front_crc);
1941 return -EBADMSG;
1942 }
1943 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1944 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1945 m, con->in_middle_crc, m->footer.middle_crc);
1946 return -EBADMSG;
1947 }
Alex Elderbca064d2012-02-15 07:43:54 -06001948 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001949 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1950 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1951 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1952 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1953 return -EBADMSG;
1954 }
1955
1956 return 1; /* done! */
1957}
1958
1959/*
1960 * Process message. This happens in the worker thread. The callback should
1961 * be careful not to do anything that waits on other incoming messages or it
1962 * may deadlock.
1963 */
1964static void process_message(struct ceph_connection *con)
1965{
Sage Weil5e095e82009-12-14 14:30:34 -08001966 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001967
Alex Elder38941f82012-06-01 14:56:43 -05001968 BUG_ON(con->in_msg->con != con);
1969 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001970 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001971 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001972 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001973
1974 /* if first message, set peer_name */
1975 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001976 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001977
Sage Weil31b80062009-10-06 11:31:13 -07001978 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001979 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001980
1981 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1982 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001983 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001984 le16_to_cpu(msg->hdr.type),
1985 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1986 le32_to_cpu(msg->hdr.front_len),
1987 le32_to_cpu(msg->hdr.data_len),
1988 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1989 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001990
1991 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001992 prepare_read_tag(con);
1993}
1994
1995
1996/*
1997 * Write something to the socket. Called in a worker thread when the
1998 * socket appears to be writeable and we have something ready to send.
1999 */
2000static int try_write(struct ceph_connection *con)
2001{
Sage Weil31b80062009-10-06 11:31:13 -07002002 int ret = 1;
2003
Sage Weild59315c2012-06-21 12:49:23 -07002004 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002005
Sage Weil31b80062009-10-06 11:31:13 -07002006more:
2007 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2008
2009 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002010 if (con->state == CON_STATE_PREOPEN) {
2011 BUG_ON(con->sock);
2012 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002013
Alex Eldere2200422012-05-23 14:35:23 -05002014 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002015 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002016 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002017
Sage Weilcf3e5c42009-12-11 09:48:05 -08002018 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002019 con->in_tag = CEPH_MSGR_TAG_READY;
2020 dout("try_write initiating connect on %p new state %lu\n",
2021 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002022 ret = ceph_tcp_connect(con);
2023 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002024 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002025 goto out;
2026 }
2027 }
2028
2029more_kvec:
2030 /* kvec data queued? */
2031 if (con->out_skip) {
2032 ret = write_partial_skip(con);
2033 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002034 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002035 }
2036 if (con->out_kvec_left) {
2037 ret = write_partial_kvec(con);
2038 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002039 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002040 }
2041
2042 /* msg pages? */
2043 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002044 if (con->out_msg_done) {
2045 ceph_msg_put(con->out_msg);
2046 con->out_msg = NULL; /* we're done with this one */
2047 goto do_next;
2048 }
2049
Sage Weil31b80062009-10-06 11:31:13 -07002050 ret = write_partial_msg_pages(con);
2051 if (ret == 1)
2052 goto more_kvec; /* we need to send the footer, too! */
2053 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002054 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002055 if (ret < 0) {
2056 dout("try_write write_partial_msg_pages err %d\n",
2057 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002058 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002059 }
2060 }
2061
Sage Weilc86a2932009-12-14 14:04:30 -08002062do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002063 if (con->state == CON_STATE_OPEN) {
Sage Weil31b80062009-10-06 11:31:13 -07002064 /* is anything else pending? */
2065 if (!list_empty(&con->out_queue)) {
2066 prepare_write_message(con);
2067 goto more;
2068 }
2069 if (con->in_seq > con->in_seq_acked) {
2070 prepare_write_ack(con);
2071 goto more;
2072 }
Sage Weil4a861692012-07-20 17:29:55 -07002073 if (test_and_clear_bit(CON_FLAG_KEEPALIVE_PENDING,
2074 &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002075 prepare_write_keepalive(con);
2076 goto more;
2077 }
2078 }
2079
2080 /* Nothing to do! */
Sage Weil4a861692012-07-20 17:29:55 -07002081 clear_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002082 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002083 ret = 0;
2084out:
Sage Weil42961d22011-01-25 08:19:34 -08002085 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002086 return ret;
2087}
2088
2089
2090
2091/*
2092 * Read what we can from the socket.
2093 */
2094static int try_read(struct ceph_connection *con)
2095{
Sage Weil31b80062009-10-06 11:31:13 -07002096 int ret = -1;
2097
Sage Weil31b80062009-10-06 11:31:13 -07002098more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002099 dout("try_read start on %p state %lu\n", con, con->state);
2100 if (con->state != CON_STATE_CONNECTING &&
2101 con->state != CON_STATE_NEGOTIATING &&
2102 con->state != CON_STATE_OPEN)
2103 return 0;
2104
2105 BUG_ON(!con->sock);
2106
Sage Weil31b80062009-10-06 11:31:13 -07002107 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2108 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002109
Sage Weil8dacc7d2012-07-20 17:24:40 -07002110 if (con->state == CON_STATE_CONNECTING) {
Alex Elder7593af92012-05-24 11:55:03 -05002111 dout("try_read connecting\n");
2112 ret = read_partial_banner(con);
2113 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002114 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002115 ret = process_banner(con);
2116 if (ret < 0)
2117 goto out;
2118
Sage Weil8dacc7d2012-07-20 17:24:40 -07002119 BUG_ON(con->state != CON_STATE_CONNECTING);
2120 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002121
2122 /* Banner is good, exchange connection info */
2123 ret = prepare_write_connect(con);
2124 if (ret < 0)
2125 goto out;
2126 prepare_read_connect(con);
2127
2128 /* Send connection info before awaiting response */
2129 goto out;
2130 }
2131
Sage Weil8dacc7d2012-07-20 17:24:40 -07002132 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002133 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002134 ret = read_partial_connect(con);
2135 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002136 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002137 ret = process_connect(con);
2138 if (ret < 0)
2139 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002140 goto more;
2141 }
2142
Sage Weil8dacc7d2012-07-20 17:24:40 -07002143 BUG_ON(con->state != CON_STATE_OPEN);
2144
Sage Weil31b80062009-10-06 11:31:13 -07002145 if (con->in_base_pos < 0) {
2146 /*
2147 * skipping + discarding content.
2148 *
2149 * FIXME: there must be a better way to do this!
2150 */
Alex Elder84495f42012-02-15 07:43:55 -06002151 static char buf[SKIP_BUF_SIZE];
2152 int skip = min((int) sizeof (buf), -con->in_base_pos);
2153
Sage Weil31b80062009-10-06 11:31:13 -07002154 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2155 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2156 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002157 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002158 con->in_base_pos += ret;
2159 if (con->in_base_pos)
2160 goto more;
2161 }
2162 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2163 /*
2164 * what's next?
2165 */
2166 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2167 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002168 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002169 dout("try_read got tag %d\n", (int)con->in_tag);
2170 switch (con->in_tag) {
2171 case CEPH_MSGR_TAG_MSG:
2172 prepare_read_message(con);
2173 break;
2174 case CEPH_MSGR_TAG_ACK:
2175 prepare_read_ack(con);
2176 break;
2177 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002178 con_close_socket(con);
2179 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002180 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002181 default:
2182 goto bad_tag;
2183 }
2184 }
2185 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2186 ret = read_partial_message(con);
2187 if (ret <= 0) {
2188 switch (ret) {
2189 case -EBADMSG:
2190 con->error_msg = "bad crc";
2191 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002192 break;
Sage Weil31b80062009-10-06 11:31:13 -07002193 case -EIO:
2194 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002195 break;
Sage Weil31b80062009-10-06 11:31:13 -07002196 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002197 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002198 }
2199 if (con->in_tag == CEPH_MSGR_TAG_READY)
2200 goto more;
2201 process_message(con);
2202 goto more;
2203 }
2204 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2205 ret = read_partial_ack(con);
2206 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002207 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002208 process_ack(con);
2209 goto more;
2210 }
2211
Sage Weil31b80062009-10-06 11:31:13 -07002212out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002213 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002214 return ret;
2215
2216bad_tag:
2217 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2218 con->error_msg = "protocol error, garbage tag";
2219 ret = -1;
2220 goto out;
2221}
2222
2223
2224/*
2225 * Atomically queue work on a connection. Bump @con reference to
2226 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002227 */
2228static void queue_con(struct ceph_connection *con)
2229{
Sage Weil31b80062009-10-06 11:31:13 -07002230 if (!con->ops->get(con)) {
2231 dout("queue_con %p ref count 0\n", con);
2232 return;
2233 }
2234
Tejun Heof363e452011-01-03 14:49:46 +01002235 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002236 dout("queue_con %p - already queued\n", con);
2237 con->ops->put(con);
2238 } else {
2239 dout("queue_con %p\n", con);
2240 }
2241}
2242
2243/*
2244 * Do some work on a connection. Drop a connection ref when we're done.
2245 */
2246static void con_work(struct work_struct *work)
2247{
2248 struct ceph_connection *con = container_of(work, struct ceph_connection,
2249 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002250 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002251
Sage Weil9dd46582010-04-28 13:51:50 -07002252 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002253restart:
Sage Weil4a861692012-07-20 17:29:55 -07002254 if (test_and_clear_bit(CON_FLAG_SOCK_CLOSED, &con->flags)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002255 switch (con->state) {
2256 case CON_STATE_CONNECTING:
Alex Elder188048b2012-06-20 21:53:53 -05002257 con->error_msg = "connection failed";
Sage Weil8dacc7d2012-07-20 17:24:40 -07002258 break;
2259 case CON_STATE_NEGOTIATING:
2260 con->error_msg = "negotiation failed";
2261 break;
2262 case CON_STATE_OPEN:
2263 con->error_msg = "socket closed";
2264 break;
2265 default:
2266 dout("unrecognized con state %d\n", (int)con->state);
Alex Eldere27947c2012-05-23 14:35:23 -05002267 con->error_msg = "unrecognized con state";
Sage Weil8dacc7d2012-07-20 17:24:40 -07002268 BUG();
2269 }
Alex Elder188048b2012-06-20 21:53:53 -05002270 goto fault;
2271 }
2272
Sage Weil4a861692012-07-20 17:29:55 -07002273 if (test_and_clear_bit(CON_FLAG_BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002274 dout("con_work %p backing off\n", con);
2275 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2276 round_jiffies_relative(con->delay))) {
2277 dout("con_work %p backoff %lu\n", con, con->delay);
2278 mutex_unlock(&con->mutex);
2279 return;
2280 } else {
2281 con->ops->put(con);
2282 dout("con_work %p FAILED to back off %lu\n", con,
2283 con->delay);
2284 }
2285 }
Sage Weil9dd46582010-04-28 13:51:50 -07002286
Sage Weil8dacc7d2012-07-20 17:24:40 -07002287 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08002288 dout("con_work %p STANDBY\n", con);
2289 goto done;
2290 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002291 if (con->state == CON_STATE_CLOSED) {
Sage Weil2e8cb102012-07-20 15:40:04 -07002292 dout("con_work %p CLOSED\n", con);
2293 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -07002294 goto done;
2295 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002296 if (con->state == CON_STATE_PREOPEN) {
Sage Weil31b80062009-10-06 11:31:13 -07002297 dout("con_work OPENING\n");
Sage Weil2e8cb102012-07-20 15:40:04 -07002298 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -07002299 }
2300
Sage Weil0da5d702011-05-19 11:21:05 -07002301 ret = try_read(con);
2302 if (ret == -EAGAIN)
2303 goto restart;
Sage Weil3a140a02012-07-30 16:24:21 -07002304 if (ret < 0) {
2305 con->error_msg = "socket error on read";
Sage Weil0da5d702011-05-19 11:21:05 -07002306 goto fault;
Sage Weil3a140a02012-07-30 16:24:21 -07002307 }
Sage Weil0da5d702011-05-19 11:21:05 -07002308
2309 ret = try_write(con);
2310 if (ret == -EAGAIN)
2311 goto restart;
Sage Weil3a140a02012-07-30 16:24:21 -07002312 if (ret < 0) {
2313 con->error_msg = "socket error on write";
Sage Weil0da5d702011-05-19 11:21:05 -07002314 goto fault;
Sage Weil3a140a02012-07-30 16:24:21 -07002315 }
Sage Weil31b80062009-10-06 11:31:13 -07002316
2317done:
Sage Weil9dd46582010-04-28 13:51:50 -07002318 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002319done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002320 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002321 return;
2322
2323fault:
2324 mutex_unlock(&con->mutex);
2325 ceph_fault(con); /* error/fault path */
2326 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002327}
2328
2329
2330/*
2331 * Generic error/fault handler. A retry mechanism is used with
2332 * exponential backoff
2333 */
2334static void ceph_fault(struct ceph_connection *con)
2335{
Sage Weil3b5ede02012-07-20 15:22:53 -07002336 mutex_lock(&con->mutex);
2337
Sage Weil31b80062009-10-06 11:31:13 -07002338 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002339 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002340 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002341 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002342
Sage Weil8dacc7d2012-07-20 17:24:40 -07002343 BUG_ON(con->state != CON_STATE_CONNECTING &&
2344 con->state != CON_STATE_NEGOTIATING &&
2345 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08002346
Sage Weil31b80062009-10-06 11:31:13 -07002347 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002348
Sage Weil4a861692012-07-20 17:29:55 -07002349 if (test_bit(CON_FLAG_LOSSYTX, &con->flags)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002350 dout("fault on LOSSYTX channel, marking CLOSED\n");
2351 con->state = CON_STATE_CLOSED;
Sage Weil3b5ede02012-07-20 15:22:53 -07002352 goto out_unlock;
2353 }
2354
Sage Weil5e095e82009-12-14 14:30:34 -08002355 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002356 BUG_ON(con->in_msg->con != con);
2357 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002358 ceph_msg_put(con->in_msg);
2359 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002360 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002361 }
Sage Weil31b80062009-10-06 11:31:13 -07002362
Sage Weile80a52d2010-02-25 12:40:45 -08002363 /* Requeue anything that hasn't been acked */
2364 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002365
Sage Weile76661d2011-03-03 10:10:15 -08002366 /* If there are no messages queued or keepalive pending, place
2367 * the connection in a STANDBY state */
2368 if (list_empty(&con->out_queue) &&
Sage Weil4a861692012-07-20 17:29:55 -07002369 !test_bit(CON_FLAG_KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002370 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Sage Weil4a861692012-07-20 17:29:55 -07002371 clear_bit(CON_FLAG_WRITE_PENDING, &con->flags);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002372 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08002373 } else {
2374 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002375 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08002376 if (con->delay == 0)
2377 con->delay = BASE_DELAY_INTERVAL;
2378 else if (con->delay < MAX_DELAY_INTERVAL)
2379 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002380 con->ops->get(con);
2381 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002382 round_jiffies_relative(con->delay))) {
2383 dout("fault queued %p delay %lu\n", con, con->delay);
2384 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002385 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002386 dout("fault failed to queue %p delay %lu, backoff\n",
2387 con, con->delay);
2388 /*
2389 * In many cases we see a socket state change
2390 * while con_work is running and end up
2391 * queuing (non-delayed) work, such that we
2392 * can't backoff with a delay. Set a flag so
2393 * that when con_work restarts we schedule the
2394 * delay then.
2395 */
Sage Weil4a861692012-07-20 17:29:55 -07002396 set_bit(CON_FLAG_BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002397 }
Sage Weil31b80062009-10-06 11:31:13 -07002398 }
2399
Sage Weil91e45ce32010-02-15 12:05:09 -08002400out_unlock:
2401 mutex_unlock(&con->mutex);
Sage Weil161fd652010-02-25 12:38:57 -08002402 /*
2403 * in case we faulted due to authentication, invalidate our
2404 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002405 */
Sage Weil161fd652010-02-25 12:38:57 -08002406 if (con->auth_retry && con->ops->invalidate_authorizer) {
2407 dout("calling invalidate_authorizer()\n");
2408 con->ops->invalidate_authorizer(con);
2409 }
2410
Sage Weil31b80062009-10-06 11:31:13 -07002411 if (con->ops->fault)
2412 con->ops->fault(con);
2413}
2414
2415
2416
2417/*
Alex Elder15d98822012-05-26 23:26:43 -05002418 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002419 */
Alex Elder15d98822012-05-26 23:26:43 -05002420void ceph_messenger_init(struct ceph_messenger *msgr,
2421 struct ceph_entity_addr *myaddr,
2422 u32 supported_features,
2423 u32 required_features,
2424 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002425{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002426 msgr->supported_features = supported_features;
2427 msgr->required_features = required_features;
2428
Sage Weil31b80062009-10-06 11:31:13 -07002429 spin_lock_init(&msgr->global_seq_lock);
2430
Sage Weil31b80062009-10-06 11:31:13 -07002431 if (myaddr)
2432 msgr->inst.addr = *myaddr;
2433
2434 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002435 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002436 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002437 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002438 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002439
Guanjun Hea2a32582012-07-08 19:50:33 -07002440 atomic_set(&msgr->stopping, 0);
2441
Alex Elder15d98822012-05-26 23:26:43 -05002442 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002443}
Alex Elder15d98822012-05-26 23:26:43 -05002444EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002445
Sage Weile00de342011-03-04 12:25:05 -08002446static void clear_standby(struct ceph_connection *con)
2447{
2448 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002449 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08002450 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002451 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08002452 con->connect_seq++;
Sage Weil4a861692012-07-20 17:29:55 -07002453 WARN_ON(test_bit(CON_FLAG_WRITE_PENDING, &con->flags));
2454 WARN_ON(test_bit(CON_FLAG_KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002455 }
2456}
2457
Sage Weil31b80062009-10-06 11:31:13 -07002458/*
2459 * Queue up an outgoing message on the given connection.
2460 */
2461void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2462{
Sage Weila59b55a2012-07-20 15:34:04 -07002463 /* set src+dst */
2464 msg->hdr.src = con->msgr->inst.name;
2465 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2466 msg->needs_out_seq = true;
2467
2468 mutex_lock(&con->mutex);
2469
Sage Weil8dacc7d2012-07-20 17:24:40 -07002470 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07002471 dout("con_send %p closed, dropping %p\n", con, msg);
2472 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07002473 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002474 return;
2475 }
2476
Alex Elder38941f82012-06-01 14:56:43 -05002477 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002478 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002479 BUG_ON(msg->con == NULL);
2480
Sage Weil31b80062009-10-06 11:31:13 -07002481 BUG_ON(!list_empty(&msg->list_head));
2482 list_add_tail(&msg->list_head, &con->out_queue);
2483 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2484 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2485 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2486 le32_to_cpu(msg->hdr.front_len),
2487 le32_to_cpu(msg->hdr.middle_len),
2488 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07002489
2490 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08002491 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002492
2493 /* if there wasn't anything waiting to send before, queue
2494 * new work */
Sage Weil4a861692012-07-20 17:29:55 -07002495 if (test_and_set_bit(CON_FLAG_WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002496 queue_con(con);
2497}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002498EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002499
2500/*
2501 * Revoke a message that was previously queued for send
2502 */
Alex Elder6740a842012-06-01 14:56:43 -05002503void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002504{
Alex Elder6740a842012-06-01 14:56:43 -05002505 struct ceph_connection *con = msg->con;
2506
2507 if (!con)
2508 return; /* Message not in our possession */
2509
Sage Weilec302642009-12-22 10:43:42 -08002510 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002511 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002512 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002513 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002514 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002515 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002516 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002517 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002518
Sage Weil31b80062009-10-06 11:31:13 -07002519 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002520 }
2521 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002522 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002523 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002524 if (con->out_kvec_is_msg) {
2525 con->out_skip = con->out_kvec_bytes;
2526 con->out_kvec_is_msg = false;
2527 }
Sage Weiled98ada2010-07-05 12:15:14 -07002528 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002529
2530 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002531 }
Sage Weilec302642009-12-22 10:43:42 -08002532 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002533}
2534
2535/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002536 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002537 */
Alex Elder8921d112012-06-01 14:56:43 -05002538void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002539{
Alex Elder8921d112012-06-01 14:56:43 -05002540 struct ceph_connection *con;
2541
2542 BUG_ON(msg == NULL);
2543 if (!msg->con) {
2544 dout("%s msg %p null con\n", __func__, msg);
2545
2546 return; /* Message not in our possession */
2547 }
2548
2549 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002550 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002551 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002552 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2553 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2554 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002555
2556 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002557 dout("%s %p msg %p revoked\n", __func__, con, msg);
2558 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002559 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002560 front_len -
2561 middle_len -
2562 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002563 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002564 ceph_msg_put(con->in_msg);
2565 con->in_msg = NULL;
2566 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002567 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002568 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002569 dout("%s %p in_msg %p msg %p no-op\n",
2570 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002571 }
2572 mutex_unlock(&con->mutex);
2573}
2574
2575/*
Sage Weil31b80062009-10-06 11:31:13 -07002576 * Queue a keepalive byte to ensure the tcp connection is alive.
2577 */
2578void ceph_con_keepalive(struct ceph_connection *con)
2579{
Sage Weile00de342011-03-04 12:25:05 -08002580 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07002581 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08002582 clear_standby(con);
Sage Weil00650932012-07-20 15:33:04 -07002583 mutex_unlock(&con->mutex);
Sage Weil4a861692012-07-20 17:29:55 -07002584 if (test_and_set_bit(CON_FLAG_KEEPALIVE_PENDING, &con->flags) == 0 &&
2585 test_and_set_bit(CON_FLAG_WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002586 queue_con(con);
2587}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002588EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002589
2590
2591/*
2592 * construct a new message with given type, size
2593 * the new msg has a ref count of 1.
2594 */
Sage Weilb61c2762011-08-09 15:03:46 -07002595struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2596 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002597{
2598 struct ceph_msg *m;
2599
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002600 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002601 if (m == NULL)
2602 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002603 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002604
2605 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002606 INIT_LIST_HEAD(&m->list_head);
2607
Sage Weil45c6ceb2010-05-11 15:01:51 -07002608 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002609 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002610 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2611 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002612 m->hdr.front_len = cpu_to_le32(front_len);
2613 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002614 m->hdr.data_len = 0;
2615 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002616 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002617 m->footer.front_crc = 0;
2618 m->footer.middle_crc = 0;
2619 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002620 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002621 m->front_max = front_len;
2622 m->front_is_vmalloc = false;
2623 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002624 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002625 m->pool = NULL;
2626
Henry C Changca208922011-05-03 02:29:56 +00002627 /* middle */
2628 m->middle = NULL;
2629
2630 /* data */
2631 m->nr_pages = 0;
2632 m->page_alignment = 0;
2633 m->pages = NULL;
2634 m->pagelist = NULL;
2635 m->bio = NULL;
2636 m->bio_iter = NULL;
2637 m->bio_seg = 0;
2638 m->trail = NULL;
2639
Sage Weil31b80062009-10-06 11:31:13 -07002640 /* front */
2641 if (front_len) {
2642 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002643 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002644 PAGE_KERNEL);
2645 m->front_is_vmalloc = true;
2646 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002647 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002648 }
2649 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002650 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002651 front_len);
2652 goto out2;
2653 }
2654 } else {
2655 m->front.iov_base = NULL;
2656 }
2657 m->front.iov_len = front_len;
2658
Sage Weilbb257662010-04-01 16:07:23 -07002659 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002660 return m;
2661
2662out2:
2663 ceph_msg_put(m);
2664out:
Sage Weilb61c2762011-08-09 15:03:46 -07002665 if (!can_fail) {
2666 pr_err("msg_new can't create type %d front %d\n", type,
2667 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002668 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002669 } else {
2670 dout("msg_new can't create type %d front %d\n", type,
2671 front_len);
2672 }
Sage Weila79832f2010-04-01 16:06:19 -07002673 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002674}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002675EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002676
2677/*
Sage Weil31b80062009-10-06 11:31:13 -07002678 * Allocate "middle" portion of a message, if it is needed and wasn't
2679 * allocated by alloc_msg. This allows us to read a small fixed-size
2680 * per-type header in the front and then gracefully fail (i.e.,
2681 * propagate the error to the caller based on info in the front) when
2682 * the middle is too large.
2683 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002684static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002685{
2686 int type = le16_to_cpu(msg->hdr.type);
2687 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2688
2689 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2690 ceph_msg_type_name(type), middle_len);
2691 BUG_ON(!middle_len);
2692 BUG_ON(msg->middle);
2693
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002694 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002695 if (!msg->middle)
2696 return -ENOMEM;
2697 return 0;
2698}
2699
Yehuda Sadeh24504182010-01-08 13:58:34 -08002700/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002701 * Allocate a message for receiving an incoming message on a
2702 * connection, and save the result in con->in_msg. Uses the
2703 * connection's private alloc_msg op if available.
2704 *
2705 * Returns true if the message should be skipped, false otherwise.
2706 * If true is returned (skip message), con->in_msg will be NULL.
2707 * If false is returned, con->in_msg will contain a pointer to the
2708 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002709 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002710static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2711 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002712{
2713 int type = le16_to_cpu(hdr->type);
2714 int front_len = le32_to_cpu(hdr->front_len);
2715 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002716 int ret;
2717
Alex Elder1c20f2d2012-06-04 14:43:32 -05002718 BUG_ON(con->in_msg != NULL);
2719
Yehuda Sadeh24504182010-01-08 13:58:34 -08002720 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002721 int skip = 0;
2722
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002723 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002724 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002725 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002726 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002727 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002728 BUG_ON(con->in_msg->con == NULL);
2729 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002730 if (skip)
2731 con->in_msg = NULL;
2732
2733 if (!con->in_msg)
2734 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002735 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002736 if (!con->in_msg) {
2737 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2738 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002739 pr_err("unable to allocate msg type %d len %d\n",
2740 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002741 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002742 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002743 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002744 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002745 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002746 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002747 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002748
Alex Elder1c20f2d2012-06-04 14:43:32 -05002749 if (middle_len && !con->in_msg->middle) {
2750 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002751 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002752 ceph_msg_put(con->in_msg);
2753 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002754 }
2755 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002756
Alex Elder1c20f2d2012-06-04 14:43:32 -05002757 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002758}
2759
Sage Weil31b80062009-10-06 11:31:13 -07002760
2761/*
2762 * Free a generically kmalloc'd message.
2763 */
2764void ceph_msg_kfree(struct ceph_msg *m)
2765{
2766 dout("msg_kfree %p\n", m);
2767 if (m->front_is_vmalloc)
2768 vfree(m->front.iov_base);
2769 else
2770 kfree(m->front.iov_base);
2771 kfree(m);
2772}
2773
2774/*
2775 * Drop a msg ref. Destroy as needed.
2776 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002777void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002778{
Sage Weilc2e552e2009-12-07 15:55:05 -08002779 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002780
Sage Weilc2e552e2009-12-07 15:55:05 -08002781 dout("ceph_msg_put last one on %p\n", m);
2782 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002783
Sage Weilc2e552e2009-12-07 15:55:05 -08002784 /* drop middle, data, if any */
2785 if (m->middle) {
2786 ceph_buffer_put(m->middle);
2787 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002788 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002789 m->nr_pages = 0;
2790 m->pages = NULL;
2791
Sage Weil58bb3b32009-12-23 12:12:31 -08002792 if (m->pagelist) {
2793 ceph_pagelist_release(m->pagelist);
2794 kfree(m->pagelist);
2795 m->pagelist = NULL;
2796 }
2797
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002798 m->trail = NULL;
2799
Sage Weilc2e552e2009-12-07 15:55:05 -08002800 if (m->pool)
2801 ceph_msgpool_put(m->pool, m);
2802 else
2803 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002804}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002805EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002806
2807void ceph_msg_dump(struct ceph_msg *msg)
2808{
2809 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2810 msg->front_max, msg->nr_pages);
2811 print_hex_dump(KERN_DEBUG, "header: ",
2812 DUMP_PREFIX_OFFSET, 16, 1,
2813 &msg->hdr, sizeof(msg->hdr), true);
2814 print_hex_dump(KERN_DEBUG, " front: ",
2815 DUMP_PREFIX_OFFSET, 16, 1,
2816 msg->front.iov_base, msg->front.iov_len, true);
2817 if (msg->middle)
2818 print_hex_dump(KERN_DEBUG, "middle: ",
2819 DUMP_PREFIX_OFFSET, 16, 1,
2820 msg->middle->vec.iov_base,
2821 msg->middle->vec.iov_len, true);
2822 print_hex_dump(KERN_DEBUG, "footer: ",
2823 DUMP_PREFIX_OFFSET, 16, 1,
2824 &msg->footer, sizeof(msg->footer), true);
2825}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002826EXPORT_SYMBOL(ceph_msg_dump);