blob: 17a09b32a591a08254e4cf9f6d660cd8a9399252 [file] [log] [blame]
Sage Weil31b80062009-10-06 11:31:13 -07001#include "ceph_debug.h"
2
3#include <linux/crc32c.h>
4#include <linux/ctype.h>
5#include <linux/highmem.h>
6#include <linux/inet.h>
7#include <linux/kthread.h>
8#include <linux/net.h>
Tejun 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>
Sage Weil31b80062009-10-06 11:31:13 -070014#include <net/tcp.h>
15
16#include "super.h"
17#include "messenger.h"
Sage Weil63f2d212009-11-03 15:17:56 -080018#include "decode.h"
Sage Weil58bb3b32009-12-23 12:12:31 -080019#include "pagelist.h"
Sage Weil31b80062009-10-06 11:31:13 -070020
21/*
22 * Ceph uses the messenger to exchange ceph_msg messages with other
23 * hosts in the system. The messenger provides ordered and reliable
24 * delivery. We tolerate TCP disconnects by reconnecting (with
25 * exponential backoff) in the case of a fault (disconnection, bad
26 * crc, protocol error). Acks allow sent messages to be discarded by
27 * the sender.
28 */
29
30/* static tag bytes (protocol control messages) */
31static char tag_msg = CEPH_MSGR_TAG_MSG;
32static char tag_ack = CEPH_MSGR_TAG_ACK;
33static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
34
Sage Weila6a53492010-04-13 14:07:07 -070035#ifdef CONFIG_LOCKDEP
36static struct lock_class_key socket_class;
37#endif
38
Sage Weil31b80062009-10-06 11:31:13 -070039
40static void queue_con(struct ceph_connection *con);
41static void con_work(struct work_struct *);
42static void ceph_fault(struct ceph_connection *con);
43
Sage Weil31b80062009-10-06 11:31:13 -070044/*
45 * nicely render a sockaddr as a string.
46 */
47#define MAX_ADDR_STR 20
Sage Weild06dbaf2010-07-08 10:47:16 -070048#define MAX_ADDR_STR_LEN 60
49static char addr_str[MAX_ADDR_STR][MAX_ADDR_STR_LEN];
Sage Weil31b80062009-10-06 11:31:13 -070050static DEFINE_SPINLOCK(addr_str_lock);
51static int last_addr_str;
52
53const char *pr_addr(const struct sockaddr_storage *ss)
54{
55 int i;
56 char *s;
57 struct sockaddr_in *in4 = (void *)ss;
Sage Weil31b80062009-10-06 11:31:13 -070058 struct sockaddr_in6 *in6 = (void *)ss;
59
60 spin_lock(&addr_str_lock);
61 i = last_addr_str++;
62 if (last_addr_str == MAX_ADDR_STR)
63 last_addr_str = 0;
64 spin_unlock(&addr_str_lock);
65 s = addr_str[i];
66
67 switch (ss->ss_family) {
68 case AF_INET:
Sage Weild06dbaf2010-07-08 10:47:16 -070069 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%u", &in4->sin_addr,
70 (unsigned int)ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -070071 break;
72
73 case AF_INET6:
Sage Weild06dbaf2010-07-08 10:47:16 -070074 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%u", &in6->sin6_addr,
75 (unsigned int)ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -070076 break;
77
78 default:
79 sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family);
80 }
81
82 return s;
83}
84
Sage Weil63f2d212009-11-03 15:17:56 -080085static void encode_my_addr(struct ceph_messenger *msgr)
86{
87 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
88 ceph_encode_addr(&msgr->my_enc_addr);
89}
90
Sage Weil31b80062009-10-06 11:31:13 -070091/*
92 * work queue for all reading and writing to/from the socket.
93 */
94struct workqueue_struct *ceph_msgr_wq;
95
96int __init ceph_msgr_init(void)
97{
98 ceph_msgr_wq = create_workqueue("ceph-msgr");
99 if (IS_ERR(ceph_msgr_wq)) {
100 int ret = PTR_ERR(ceph_msgr_wq);
101 pr_err("msgr_init failed to create workqueue: %d\n", ret);
102 ceph_msgr_wq = NULL;
103 return ret;
104 }
105 return 0;
106}
107
108void ceph_msgr_exit(void)
109{
110 destroy_workqueue(ceph_msgr_wq);
111}
112
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700113void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700114{
115 flush_workqueue(ceph_msgr_wq);
116}
117
118
Sage Weil31b80062009-10-06 11:31:13 -0700119/*
120 * socket callback functions
121 */
122
123/* data available on socket, or listen socket received a connect */
124static void ceph_data_ready(struct sock *sk, int count_unused)
125{
126 struct ceph_connection *con =
127 (struct ceph_connection *)sk->sk_user_data;
128 if (sk->sk_state != TCP_CLOSE_WAIT) {
129 dout("ceph_data_ready on %p state = %lu, queueing work\n",
130 con, con->state);
131 queue_con(con);
132 }
133}
134
135/* socket has buffer space for writing */
136static void ceph_write_space(struct sock *sk)
137{
138 struct ceph_connection *con =
139 (struct ceph_connection *)sk->sk_user_data;
140
141 /* only queue to workqueue if there is data we want to write. */
142 if (test_bit(WRITE_PENDING, &con->state)) {
143 dout("ceph_write_space %p queueing write work\n", con);
144 queue_con(con);
145 } else {
146 dout("ceph_write_space %p nothing to write\n", con);
147 }
148
149 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
150 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
151}
152
153/* socket's state has changed */
154static void ceph_state_change(struct sock *sk)
155{
156 struct ceph_connection *con =
157 (struct ceph_connection *)sk->sk_user_data;
158
159 dout("ceph_state_change %p state = %lu sk_state = %u\n",
160 con, con->state, sk->sk_state);
161
162 if (test_bit(CLOSED, &con->state))
163 return;
164
165 switch (sk->sk_state) {
166 case TCP_CLOSE:
167 dout("ceph_state_change TCP_CLOSE\n");
168 case TCP_CLOSE_WAIT:
169 dout("ceph_state_change TCP_CLOSE_WAIT\n");
170 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
171 if (test_bit(CONNECTING, &con->state))
172 con->error_msg = "connection failed";
173 else
174 con->error_msg = "socket closed";
175 queue_con(con);
176 }
177 break;
178 case TCP_ESTABLISHED:
179 dout("ceph_state_change TCP_ESTABLISHED\n");
180 queue_con(con);
181 break;
182 }
183}
184
185/*
186 * set up socket callbacks
187 */
188static void set_sock_callbacks(struct socket *sock,
189 struct ceph_connection *con)
190{
191 struct sock *sk = sock->sk;
192 sk->sk_user_data = (void *)con;
193 sk->sk_data_ready = ceph_data_ready;
194 sk->sk_write_space = ceph_write_space;
195 sk->sk_state_change = ceph_state_change;
196}
197
198
199/*
200 * socket helpers
201 */
202
203/*
204 * initiate connection to a remote socket.
205 */
206static struct socket *ceph_tcp_connect(struct ceph_connection *con)
207{
Sage Weilf91d3472010-07-01 15:18:31 -0700208 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700209 struct socket *sock;
210 int ret;
211
212 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700213 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
214 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700215 if (ret)
216 return ERR_PTR(ret);
217 con->sock = sock;
218 sock->sk->sk_allocation = GFP_NOFS;
219
Sage Weila6a53492010-04-13 14:07:07 -0700220#ifdef CONFIG_LOCKDEP
221 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
222#endif
223
Sage Weil31b80062009-10-06 11:31:13 -0700224 set_sock_callbacks(sock, con);
225
226 dout("connect %s\n", pr_addr(&con->peer_addr.in_addr));
227
Sage Weilf91d3472010-07-01 15:18:31 -0700228 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
229 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700230 if (ret == -EINPROGRESS) {
231 dout("connect %s EINPROGRESS sk_state = %u\n",
232 pr_addr(&con->peer_addr.in_addr),
233 sock->sk->sk_state);
234 ret = 0;
235 }
236 if (ret < 0) {
237 pr_err("connect %s error %d\n",
238 pr_addr(&con->peer_addr.in_addr), ret);
239 sock_release(sock);
240 con->sock = NULL;
241 con->error_msg = "connect error";
242 }
243
244 if (ret < 0)
245 return ERR_PTR(ret);
246 return sock;
247}
248
249static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
250{
251 struct kvec iov = {buf, len};
252 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
253
254 return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
255}
256
257/*
258 * write something. @more is true if caller will be sending more data
259 * shortly.
260 */
261static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
262 size_t kvlen, size_t len, int more)
263{
264 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
265
266 if (more)
267 msg.msg_flags |= MSG_MORE;
268 else
269 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
270
271 return kernel_sendmsg(sock, &msg, iov, kvlen, len);
272}
273
274
275/*
276 * Shutdown/close the socket for the given connection.
277 */
278static int con_close_socket(struct ceph_connection *con)
279{
280 int rc;
281
282 dout("con_close_socket on %p sock %p\n", con, con->sock);
283 if (!con->sock)
284 return 0;
285 set_bit(SOCK_CLOSED, &con->state);
286 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
287 sock_release(con->sock);
288 con->sock = NULL;
289 clear_bit(SOCK_CLOSED, &con->state);
290 return rc;
291}
292
293/*
294 * Reset a connection. Discard all incoming and outgoing messages
295 * and clear *_seq state.
296 */
297static void ceph_msg_remove(struct ceph_msg *msg)
298{
299 list_del_init(&msg->list_head);
300 ceph_msg_put(msg);
301}
302static void ceph_msg_remove_list(struct list_head *head)
303{
304 while (!list_empty(head)) {
305 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
306 list_head);
307 ceph_msg_remove(msg);
308 }
309}
310
311static void reset_connection(struct ceph_connection *con)
312{
313 /* reset connection, out_queue, msg_ and connect_seq */
314 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700315 ceph_msg_remove_list(&con->out_queue);
316 ceph_msg_remove_list(&con->out_sent);
317
Sage Weilcf3e5c42009-12-11 09:48:05 -0800318 if (con->in_msg) {
319 ceph_msg_put(con->in_msg);
320 con->in_msg = NULL;
321 }
322
Sage Weil31b80062009-10-06 11:31:13 -0700323 con->connect_seq = 0;
324 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800325 if (con->out_msg) {
326 ceph_msg_put(con->out_msg);
327 con->out_msg = NULL;
328 }
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700329 con->out_keepalive_pending = false;
Sage Weil31b80062009-10-06 11:31:13 -0700330 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700331 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700332}
333
334/*
335 * mark a peer down. drop any open connections.
336 */
337void ceph_con_close(struct ceph_connection *con)
338{
339 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
340 set_bit(CLOSED, &con->state); /* in case there's queued work */
341 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Sage Weil1679f872010-02-26 13:55:51 -0800342 clear_bit(LOSSYTX, &con->state); /* so we retry next connect */
343 clear_bit(KEEPALIVE_PENDING, &con->state);
344 clear_bit(WRITE_PENDING, &con->state);
Sage Weilec302642009-12-22 10:43:42 -0800345 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700346 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700347 con->peer_global_seq = 0;
Sage Weil91e45ce2010-02-15 12:05:09 -0800348 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800349 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700350 queue_con(con);
351}
352
353/*
Sage Weil31b80062009-10-06 11:31:13 -0700354 * Reopen a closed connection, with a new peer address.
355 */
356void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
357{
358 dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
359 set_bit(OPENING, &con->state);
360 clear_bit(CLOSED, &con->state);
361 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800362 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700363 queue_con(con);
364}
365
366/*
Sage Weil87b315a2010-03-22 14:51:18 -0700367 * return true if this connection ever successfully opened
368 */
369bool ceph_con_opened(struct ceph_connection *con)
370{
371 return con->connect_seq > 0;
372}
373
374/*
Sage Weil31b80062009-10-06 11:31:13 -0700375 * generic get/put
376 */
377struct ceph_connection *ceph_con_get(struct ceph_connection *con)
378{
379 dout("con_get %p nref = %d -> %d\n", con,
380 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
381 if (atomic_inc_not_zero(&con->nref))
382 return con;
383 return NULL;
384}
385
386void ceph_con_put(struct ceph_connection *con)
387{
388 dout("con_put %p nref = %d -> %d\n", con,
389 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
390 BUG_ON(atomic_read(&con->nref) == 0);
391 if (atomic_dec_and_test(&con->nref)) {
Sage Weil71ececd2009-11-18 11:27:06 -0800392 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700393 kfree(con);
394 }
395}
396
397/*
398 * initialize a new connection.
399 */
400void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
401{
402 dout("con_init %p\n", con);
403 memset(con, 0, sizeof(*con));
404 atomic_set(&con->nref, 1);
405 con->msgr = msgr;
Sage Weilec302642009-12-22 10:43:42 -0800406 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700407 INIT_LIST_HEAD(&con->out_queue);
408 INIT_LIST_HEAD(&con->out_sent);
409 INIT_DELAYED_WORK(&con->work, con_work);
410}
411
412
413/*
414 * We maintain a global counter to order connection attempts. Get
415 * a unique seq greater than @gt.
416 */
417static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
418{
419 u32 ret;
420
421 spin_lock(&msgr->global_seq_lock);
422 if (msgr->global_seq < gt)
423 msgr->global_seq = gt;
424 ret = ++msgr->global_seq;
425 spin_unlock(&msgr->global_seq_lock);
426 return ret;
427}
428
429
430/*
431 * Prepare footer for currently outgoing message, and finish things
432 * off. Assumes out_kvec* are already valid.. we just add on to the end.
433 */
434static void prepare_write_message_footer(struct ceph_connection *con, int v)
435{
436 struct ceph_msg *m = con->out_msg;
437
438 dout("prepare_write_message_footer %p\n", con);
439 con->out_kvec_is_msg = true;
440 con->out_kvec[v].iov_base = &m->footer;
441 con->out_kvec[v].iov_len = sizeof(m->footer);
442 con->out_kvec_bytes += sizeof(m->footer);
443 con->out_kvec_left++;
444 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800445 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700446}
447
448/*
449 * Prepare headers for the next outgoing message.
450 */
451static void prepare_write_message(struct ceph_connection *con)
452{
453 struct ceph_msg *m;
454 int v = 0;
455
456 con->out_kvec_bytes = 0;
457 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800458 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700459
460 /* Sneak an ack in there first? If we can get it into the same
461 * TCP packet that's a good thing. */
462 if (con->in_seq > con->in_seq_acked) {
463 con->in_seq_acked = con->in_seq;
464 con->out_kvec[v].iov_base = &tag_ack;
465 con->out_kvec[v++].iov_len = 1;
466 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
467 con->out_kvec[v].iov_base = &con->out_temp_ack;
468 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
469 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
470 }
471
Sage Weil31b80062009-10-06 11:31:13 -0700472 m = list_first_entry(&con->out_queue,
473 struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800474 con->out_msg = m;
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800475 if (test_bit(LOSSYTX, &con->state)) {
Sage Weil6c5d1a42010-02-13 20:29:31 -0800476 list_del_init(&m->list_head);
477 } else {
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800478 /* put message on sent list */
479 ceph_msg_get(m);
480 list_move_tail(&m->list_head, &con->out_sent);
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800481 }
Sage Weil31b80062009-10-06 11:31:13 -0700482
Sage Weile84346b2010-05-11 21:20:38 -0700483 /*
484 * only assign outgoing seq # if we haven't sent this message
485 * yet. if it is requeued, resend with it's original seq.
486 */
487 if (m->needs_out_seq) {
488 m->hdr.seq = cpu_to_le64(++con->out_seq);
489 m->needs_out_seq = false;
490 }
Sage Weil31b80062009-10-06 11:31:13 -0700491
492 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
493 m, con->out_seq, le16_to_cpu(m->hdr.type),
494 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
495 le32_to_cpu(m->hdr.data_len),
496 m->nr_pages);
497 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
498
499 /* tag + hdr + front + middle */
500 con->out_kvec[v].iov_base = &tag_msg;
501 con->out_kvec[v++].iov_len = 1;
502 con->out_kvec[v].iov_base = &m->hdr;
503 con->out_kvec[v++].iov_len = sizeof(m->hdr);
504 con->out_kvec[v++] = m->front;
505 if (m->middle)
506 con->out_kvec[v++] = m->middle->vec;
507 con->out_kvec_left = v;
508 con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
509 (m->middle ? m->middle->vec.iov_len : 0);
510 con->out_kvec_cur = con->out_kvec;
511
512 /* fill in crc (except data pages), footer */
513 con->out_msg->hdr.crc =
514 cpu_to_le32(crc32c(0, (void *)&m->hdr,
515 sizeof(m->hdr) - sizeof(m->hdr.crc)));
516 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
517 con->out_msg->footer.front_crc =
518 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
519 if (m->middle)
520 con->out_msg->footer.middle_crc =
521 cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
522 m->middle->vec.iov_len));
523 else
524 con->out_msg->footer.middle_crc = 0;
525 con->out_msg->footer.data_crc = 0;
526 dout("prepare_write_message front_crc %u data_crc %u\n",
527 le32_to_cpu(con->out_msg->footer.front_crc),
528 le32_to_cpu(con->out_msg->footer.middle_crc));
529
530 /* is there a data payload? */
531 if (le32_to_cpu(m->hdr.data_len) > 0) {
532 /* initialize page iterator */
533 con->out_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700534 if (m->pages)
535 con->out_msg_pos.page_pos =
536 le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
537 else
538 con->out_msg_pos.page_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700539 con->out_msg_pos.data_pos = 0;
540 con->out_msg_pos.did_page_crc = 0;
541 con->out_more = 1; /* data + footer will follow */
542 } else {
543 /* no, queue up footer too and be done */
544 prepare_write_message_footer(con, v);
545 }
546
547 set_bit(WRITE_PENDING, &con->state);
548}
549
550/*
551 * Prepare an ack.
552 */
553static void prepare_write_ack(struct ceph_connection *con)
554{
555 dout("prepare_write_ack %p %llu -> %llu\n", con,
556 con->in_seq_acked, con->in_seq);
557 con->in_seq_acked = con->in_seq;
558
559 con->out_kvec[0].iov_base = &tag_ack;
560 con->out_kvec[0].iov_len = 1;
561 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
562 con->out_kvec[1].iov_base = &con->out_temp_ack;
563 con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
564 con->out_kvec_left = 2;
565 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
566 con->out_kvec_cur = con->out_kvec;
567 con->out_more = 1; /* more will follow.. eventually.. */
568 set_bit(WRITE_PENDING, &con->state);
569}
570
571/*
572 * Prepare to write keepalive byte.
573 */
574static void prepare_write_keepalive(struct ceph_connection *con)
575{
576 dout("prepare_write_keepalive %p\n", con);
577 con->out_kvec[0].iov_base = &tag_keepalive;
578 con->out_kvec[0].iov_len = 1;
579 con->out_kvec_left = 1;
580 con->out_kvec_bytes = 1;
581 con->out_kvec_cur = con->out_kvec;
582 set_bit(WRITE_PENDING, &con->state);
583}
584
585/*
586 * Connection negotiation.
587 */
588
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800589static void prepare_connect_authorizer(struct ceph_connection *con)
590{
591 void *auth_buf;
592 int auth_len = 0;
593 int auth_protocol = 0;
594
Sage Weilec302642009-12-22 10:43:42 -0800595 mutex_unlock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800596 if (con->ops->get_authorizer)
597 con->ops->get_authorizer(con, &auth_buf, &auth_len,
598 &auth_protocol, &con->auth_reply_buf,
599 &con->auth_reply_buf_len,
600 con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800601 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800602
603 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
604 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
605
606 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
607 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
608 con->out_kvec_left++;
609 con->out_kvec_bytes += auth_len;
610}
611
Sage Weil31b80062009-10-06 11:31:13 -0700612/*
613 * We connected to a peer and are saying hello.
614 */
Sage Weileed0ef22009-11-10 14:34:36 -0800615static void prepare_write_banner(struct ceph_messenger *msgr,
616 struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700617{
618 int len = strlen(CEPH_BANNER);
Sage Weileed0ef22009-11-10 14:34:36 -0800619
620 con->out_kvec[0].iov_base = CEPH_BANNER;
621 con->out_kvec[0].iov_len = len;
622 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
623 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
624 con->out_kvec_left = 2;
625 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
626 con->out_kvec_cur = con->out_kvec;
627 con->out_more = 0;
628 set_bit(WRITE_PENDING, &con->state);
629}
630
631static void prepare_write_connect(struct ceph_messenger *msgr,
632 struct ceph_connection *con,
633 int after_banner)
634{
Sage Weil31b80062009-10-06 11:31:13 -0700635 unsigned global_seq = get_global_seq(con->msgr, 0);
636 int proto;
637
638 switch (con->peer_name.type) {
639 case CEPH_ENTITY_TYPE_MON:
640 proto = CEPH_MONC_PROTOCOL;
641 break;
642 case CEPH_ENTITY_TYPE_OSD:
643 proto = CEPH_OSDC_PROTOCOL;
644 break;
645 case CEPH_ENTITY_TYPE_MDS:
646 proto = CEPH_MDSC_PROTOCOL;
647 break;
648 default:
649 BUG();
650 }
651
652 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
653 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800654
Sage Weil6a259382010-07-07 09:06:08 -0700655 con->out_connect.features = cpu_to_le64(CEPH_FEATURE_SUPPORTED);
Sage Weil31b80062009-10-06 11:31:13 -0700656 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
657 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
658 con->out_connect.global_seq = cpu_to_le32(global_seq);
659 con->out_connect.protocol_version = cpu_to_le32(proto);
660 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700661
Sage Weileed0ef22009-11-10 14:34:36 -0800662 if (!after_banner) {
663 con->out_kvec_left = 0;
664 con->out_kvec_bytes = 0;
665 }
666 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
667 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
668 con->out_kvec_left++;
669 con->out_kvec_bytes += sizeof(con->out_connect);
Sage Weil31b80062009-10-06 11:31:13 -0700670 con->out_kvec_cur = con->out_kvec;
671 con->out_more = 0;
672 set_bit(WRITE_PENDING, &con->state);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800673
674 prepare_connect_authorizer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700675}
676
677
678/*
679 * write as much of pending kvecs to the socket as we can.
680 * 1 -> done
681 * 0 -> socket full, but more to do
682 * <0 -> error
683 */
684static int write_partial_kvec(struct ceph_connection *con)
685{
686 int ret;
687
688 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
689 while (con->out_kvec_bytes > 0) {
690 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
691 con->out_kvec_left, con->out_kvec_bytes,
692 con->out_more);
693 if (ret <= 0)
694 goto out;
695 con->out_kvec_bytes -= ret;
696 if (con->out_kvec_bytes == 0)
697 break; /* done */
698 while (ret > 0) {
699 if (ret >= con->out_kvec_cur->iov_len) {
700 ret -= con->out_kvec_cur->iov_len;
701 con->out_kvec_cur++;
702 con->out_kvec_left--;
703 } else {
704 con->out_kvec_cur->iov_len -= ret;
705 con->out_kvec_cur->iov_base += ret;
706 ret = 0;
707 break;
708 }
709 }
710 }
711 con->out_kvec_left = 0;
712 con->out_kvec_is_msg = false;
713 ret = 1;
714out:
715 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
716 con->out_kvec_bytes, con->out_kvec_left, ret);
717 return ret; /* done! */
718}
719
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700720#ifdef CONFIG_BLOCK
721static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
722{
723 if (!bio) {
724 *iter = NULL;
725 *seg = 0;
726 return;
727 }
728 *iter = bio;
729 *seg = bio->bi_idx;
730}
731
732static void iter_bio_next(struct bio **bio_iter, int *seg)
733{
734 if (*bio_iter == NULL)
735 return;
736
737 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
738
739 (*seg)++;
740 if (*seg == (*bio_iter)->bi_vcnt)
741 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
742}
743#endif
744
Sage Weil31b80062009-10-06 11:31:13 -0700745/*
746 * Write as much message data payload as we can. If we finish, queue
747 * up the footer.
748 * 1 -> done, footer is now queued in out_kvec[].
749 * 0 -> socket full, but more to do
750 * <0 -> error
751 */
752static int write_partial_msg_pages(struct ceph_connection *con)
753{
754 struct ceph_msg *msg = con->out_msg;
755 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
756 size_t len;
757 int crc = con->msgr->nocrc;
758 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700759 int total_max_write;
760 int in_trail = 0;
761 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700762
763 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
764 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
765 con->out_msg_pos.page_pos);
766
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700767#ifdef CONFIG_BLOCK
768 if (msg->bio && !msg->bio_iter)
769 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
770#endif
771
772 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700773 struct page *page = NULL;
774 void *kaddr = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700775 int max_write = PAGE_SIZE;
776 int page_shift = 0;
777
778 total_max_write = data_len - trail_len -
779 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700780
781 /*
782 * if we are calculating the data crc (the default), we need
783 * to map the page. if our pages[] has been revoked, use the
784 * zero page.
785 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700786
787 /* have we reached the trail part of the data? */
788 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
789 in_trail = 1;
790
791 total_max_write = data_len - con->out_msg_pos.data_pos;
792
793 page = list_first_entry(&msg->trail->head,
794 struct page, lru);
795 if (crc)
796 kaddr = kmap(page);
797 max_write = PAGE_SIZE;
798 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700799 page = msg->pages[con->out_msg_pos.page];
800 if (crc)
801 kaddr = kmap(page);
Sage Weil58bb3b32009-12-23 12:12:31 -0800802 } else if (msg->pagelist) {
803 page = list_first_entry(&msg->pagelist->head,
804 struct page, lru);
805 if (crc)
806 kaddr = kmap(page);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700807#ifdef CONFIG_BLOCK
808 } else if (msg->bio) {
809 struct bio_vec *bv;
810
811 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
812 page = bv->bv_page;
813 page_shift = bv->bv_offset;
814 if (crc)
815 kaddr = kmap(page) + page_shift;
816 max_write = bv->bv_len;
817#endif
Sage Weil31b80062009-10-06 11:31:13 -0700818 } else {
819 page = con->msgr->zero_page;
820 if (crc)
821 kaddr = page_address(con->msgr->zero_page);
822 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700823 len = min_t(int, max_write - con->out_msg_pos.page_pos,
824 total_max_write);
825
Sage Weil31b80062009-10-06 11:31:13 -0700826 if (crc && !con->out_msg_pos.did_page_crc) {
827 void *base = kaddr + con->out_msg_pos.page_pos;
828 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
829
830 BUG_ON(kaddr == NULL);
831 con->out_msg->footer.data_crc =
832 cpu_to_le32(crc32c(tmpcrc, base, len));
833 con->out_msg_pos.did_page_crc = 1;
834 }
Sage Weil31b80062009-10-06 11:31:13 -0700835 ret = kernel_sendpage(con->sock, page,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700836 con->out_msg_pos.page_pos + page_shift,
837 len,
Sage Weil31b80062009-10-06 11:31:13 -0700838 MSG_DONTWAIT | MSG_NOSIGNAL |
839 MSG_MORE);
840
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700841 if (crc &&
842 (msg->pages || msg->pagelist || msg->bio || in_trail))
Sage Weil31b80062009-10-06 11:31:13 -0700843 kunmap(page);
844
845 if (ret <= 0)
846 goto out;
847
848 con->out_msg_pos.data_pos += ret;
849 con->out_msg_pos.page_pos += ret;
850 if (ret == len) {
851 con->out_msg_pos.page_pos = 0;
852 con->out_msg_pos.page++;
853 con->out_msg_pos.did_page_crc = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700854 if (in_trail)
855 list_move_tail(&page->lru,
856 &msg->trail->head);
857 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -0800858 list_move_tail(&page->lru,
859 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700860#ifdef CONFIG_BLOCK
861 else if (msg->bio)
862 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
863#endif
Sage Weil31b80062009-10-06 11:31:13 -0700864 }
865 }
866
867 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
868
869 /* prepare and queue up footer, too */
870 if (!crc)
871 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
872 con->out_kvec_bytes = 0;
873 con->out_kvec_left = 0;
874 con->out_kvec_cur = con->out_kvec;
875 prepare_write_message_footer(con, 0);
876 ret = 1;
877out:
878 return ret;
879}
880
881/*
882 * write some zeros
883 */
884static int write_partial_skip(struct ceph_connection *con)
885{
886 int ret;
887
888 while (con->out_skip > 0) {
889 struct kvec iov = {
890 .iov_base = page_address(con->msgr->zero_page),
891 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
892 };
893
894 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
895 if (ret <= 0)
896 goto out;
897 con->out_skip -= ret;
898 }
899 ret = 1;
900out:
901 return ret;
902}
903
904/*
905 * Prepare to read connection handshake, or an ack.
906 */
Sage Weileed0ef22009-11-10 14:34:36 -0800907static void prepare_read_banner(struct ceph_connection *con)
908{
909 dout("prepare_read_banner %p\n", con);
910 con->in_base_pos = 0;
911}
912
Sage Weil31b80062009-10-06 11:31:13 -0700913static void prepare_read_connect(struct ceph_connection *con)
914{
915 dout("prepare_read_connect %p\n", con);
916 con->in_base_pos = 0;
917}
918
919static void prepare_read_ack(struct ceph_connection *con)
920{
921 dout("prepare_read_ack %p\n", con);
922 con->in_base_pos = 0;
923}
924
925static void prepare_read_tag(struct ceph_connection *con)
926{
927 dout("prepare_read_tag %p\n", con);
928 con->in_base_pos = 0;
929 con->in_tag = CEPH_MSGR_TAG_READY;
930}
931
932/*
933 * Prepare to read a message.
934 */
935static int prepare_read_message(struct ceph_connection *con)
936{
937 dout("prepare_read_message %p\n", con);
938 BUG_ON(con->in_msg != NULL);
939 con->in_base_pos = 0;
940 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
941 return 0;
942}
943
944
945static int read_partial(struct ceph_connection *con,
946 int *to, int size, void *object)
947{
948 *to += size;
949 while (con->in_base_pos < *to) {
950 int left = *to - con->in_base_pos;
951 int have = size - left;
952 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
953 if (ret <= 0)
954 return ret;
955 con->in_base_pos += ret;
956 }
957 return 1;
958}
959
960
961/*
962 * Read all or part of the connect-side handshake on a new connection
963 */
Sage Weileed0ef22009-11-10 14:34:36 -0800964static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700965{
966 int ret, to = 0;
967
Sage Weileed0ef22009-11-10 14:34:36 -0800968 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -0700969
970 /* peer's banner */
971 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
972 if (ret <= 0)
973 goto out;
974 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
975 &con->actual_peer_addr);
976 if (ret <= 0)
977 goto out;
978 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
979 &con->peer_addr_for_me);
980 if (ret <= 0)
981 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -0800982out:
983 return ret;
984}
985
986static int read_partial_connect(struct ceph_connection *con)
987{
988 int ret, to = 0;
989
990 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
991
Sage Weil31b80062009-10-06 11:31:13 -0700992 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
993 if (ret <= 0)
994 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800995 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
996 con->auth_reply_buf);
997 if (ret <= 0)
998 goto out;
Sage Weil31b80062009-10-06 11:31:13 -0700999
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001000 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1001 con, (int)con->in_reply.tag,
1002 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001003 le32_to_cpu(con->in_reply.global_seq));
1004out:
1005 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001006
Sage Weil31b80062009-10-06 11:31:13 -07001007}
1008
1009/*
1010 * Verify the hello banner looks okay.
1011 */
1012static int verify_hello(struct ceph_connection *con)
1013{
1014 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001015 pr_err("connect to %s got bad banner\n",
Sage Weil31b80062009-10-06 11:31:13 -07001016 pr_addr(&con->peer_addr.in_addr));
1017 con->error_msg = "protocol error, bad banner";
1018 return -1;
1019 }
1020 return 0;
1021}
1022
1023static bool addr_is_blank(struct sockaddr_storage *ss)
1024{
1025 switch (ss->ss_family) {
1026 case AF_INET:
1027 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1028 case AF_INET6:
1029 return
1030 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1031 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1032 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1033 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1034 }
1035 return false;
1036}
1037
1038static int addr_port(struct sockaddr_storage *ss)
1039{
1040 switch (ss->ss_family) {
1041 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001042 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001043 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001044 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001045 }
1046 return 0;
1047}
1048
1049static void addr_set_port(struct sockaddr_storage *ss, int p)
1050{
1051 switch (ss->ss_family) {
1052 case AF_INET:
1053 ((struct sockaddr_in *)ss)->sin_port = htons(p);
1054 case AF_INET6:
1055 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
1056 }
1057}
1058
1059/*
1060 * Parse an ip[:port] list into an addr array. Use the default
1061 * monitor port if a port isn't specified.
1062 */
1063int ceph_parse_ips(const char *c, const char *end,
1064 struct ceph_entity_addr *addr,
1065 int max_count, int *count)
1066{
1067 int i;
1068 const char *p = c;
1069
1070 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1071 for (i = 0; i < max_count; i++) {
1072 const char *ipend;
1073 struct sockaddr_storage *ss = &addr[i].in_addr;
1074 struct sockaddr_in *in4 = (void *)ss;
1075 struct sockaddr_in6 *in6 = (void *)ss;
1076 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001077 char delim = ',';
1078
1079 if (*p == '[') {
1080 delim = ']';
1081 p++;
1082 }
Sage Weil31b80062009-10-06 11:31:13 -07001083
1084 memset(ss, 0, sizeof(*ss));
1085 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
Sage Weil39139f62010-07-08 09:54:52 -07001086 delim, &ipend))
Sage Weil31b80062009-10-06 11:31:13 -07001087 ss->ss_family = AF_INET;
Sage Weil39139f62010-07-08 09:54:52 -07001088 else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
1089 delim, &ipend))
Sage Weil31b80062009-10-06 11:31:13 -07001090 ss->ss_family = AF_INET6;
Sage Weil39139f62010-07-08 09:54:52 -07001091 else
Sage Weil31b80062009-10-06 11:31:13 -07001092 goto bad;
Sage Weil31b80062009-10-06 11:31:13 -07001093 p = ipend;
1094
Sage Weil39139f62010-07-08 09:54:52 -07001095 if (delim == ']') {
1096 if (*p != ']') {
1097 dout("missing matching ']'\n");
1098 goto bad;
1099 }
1100 p++;
1101 }
1102
Sage Weil31b80062009-10-06 11:31:13 -07001103 /* port? */
1104 if (p < end && *p == ':') {
1105 port = 0;
1106 p++;
1107 while (p < end && *p >= '0' && *p <= '9') {
1108 port = (port * 10) + (*p - '0');
1109 p++;
1110 }
1111 if (port > 65535 || port == 0)
1112 goto bad;
1113 } else {
1114 port = CEPH_MON_PORT;
1115 }
1116
1117 addr_set_port(ss, port);
1118
1119 dout("parse_ips got %s\n", pr_addr(ss));
1120
1121 if (p == end)
1122 break;
1123 if (*p != ',')
1124 goto bad;
1125 p++;
1126 }
1127
1128 if (p != end)
1129 goto bad;
1130
1131 if (count)
1132 *count = i + 1;
1133 return 0;
1134
1135bad:
Sage Weil39139f62010-07-08 09:54:52 -07001136 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Sage Weil31b80062009-10-06 11:31:13 -07001137 return -EINVAL;
1138}
1139
Sage Weileed0ef22009-11-10 14:34:36 -08001140static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001141{
Sage Weileed0ef22009-11-10 14:34:36 -08001142 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001143
1144 if (verify_hello(con) < 0)
1145 return -1;
1146
Sage Weil63f2d212009-11-03 15:17:56 -08001147 ceph_decode_addr(&con->actual_peer_addr);
1148 ceph_decode_addr(&con->peer_addr_for_me);
1149
Sage Weil31b80062009-10-06 11:31:13 -07001150 /*
1151 * Make sure the other end is who we wanted. note that the other
1152 * end may not yet know their ip address, so if it's 0.0.0.0, give
1153 * them the benefit of the doubt.
1154 */
Sage Weil103e2d32010-01-07 16:12:36 -08001155 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1156 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001157 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1158 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001159 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Sage Weil103e2d32010-01-07 16:12:36 -08001160 pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001161 (int)le32_to_cpu(con->peer_addr.nonce),
Sage Weil103e2d32010-01-07 16:12:36 -08001162 pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001163 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001164 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001165 return -1;
1166 }
1167
1168 /*
1169 * did we learn our address?
1170 */
1171 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1172 int port = addr_port(&con->msgr->inst.addr.in_addr);
1173
1174 memcpy(&con->msgr->inst.addr.in_addr,
1175 &con->peer_addr_for_me.in_addr,
1176 sizeof(con->peer_addr_for_me.in_addr));
1177 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001178 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001179 dout("process_banner learned my addr is %s\n",
Sage Weil31b80062009-10-06 11:31:13 -07001180 pr_addr(&con->msgr->inst.addr.in_addr));
1181 }
1182
Sage Weileed0ef22009-11-10 14:34:36 -08001183 set_bit(NEGOTIATING, &con->state);
1184 prepare_read_connect(con);
1185 return 0;
1186}
1187
Sage Weil04a419f2009-12-23 09:30:21 -08001188static void fail_protocol(struct ceph_connection *con)
1189{
1190 reset_connection(con);
1191 set_bit(CLOSED, &con->state); /* in case there's queued work */
1192
1193 mutex_unlock(&con->mutex);
1194 if (con->ops->bad_proto)
1195 con->ops->bad_proto(con);
1196 mutex_lock(&con->mutex);
1197}
1198
Sage Weileed0ef22009-11-10 14:34:36 -08001199static int process_connect(struct ceph_connection *con)
1200{
Sage Weil6a259382010-07-07 09:06:08 -07001201 u64 sup_feat = CEPH_FEATURE_SUPPORTED;
1202 u64 req_feat = CEPH_FEATURE_REQUIRED;
Sage Weil04a419f2009-12-23 09:30:21 -08001203 u64 server_feat = le64_to_cpu(con->in_reply.features);
1204
Sage Weileed0ef22009-11-10 14:34:36 -08001205 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1206
Sage Weil31b80062009-10-06 11:31:13 -07001207 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001208 case CEPH_MSGR_TAG_FEATURES:
1209 pr_err("%s%lld %s feature set mismatch,"
1210 " my %llx < server's %llx, missing %llx\n",
1211 ENTITY_NAME(con->peer_name),
1212 pr_addr(&con->peer_addr.in_addr),
1213 sup_feat, server_feat, server_feat & ~sup_feat);
1214 con->error_msg = "missing required protocol features";
1215 fail_protocol(con);
1216 return -1;
1217
Sage Weil31b80062009-10-06 11:31:13 -07001218 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001219 pr_err("%s%lld %s protocol version mismatch,"
1220 " my %d != server's %d\n",
1221 ENTITY_NAME(con->peer_name),
1222 pr_addr(&con->peer_addr.in_addr),
1223 le32_to_cpu(con->out_connect.protocol_version),
1224 le32_to_cpu(con->in_reply.protocol_version));
1225 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001226 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001227 return -1;
1228
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001229 case CEPH_MSGR_TAG_BADAUTHORIZER:
1230 con->auth_retry++;
1231 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1232 con->auth_retry);
1233 if (con->auth_retry == 2) {
1234 con->error_msg = "connect authorization failure";
1235 reset_connection(con);
1236 set_bit(CLOSED, &con->state);
1237 return -1;
1238 }
1239 con->auth_retry = 1;
1240 prepare_write_connect(con->msgr, con, 0);
Sage Weil63733a02010-03-15 15:47:22 -07001241 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001242 break;
Sage Weil31b80062009-10-06 11:31:13 -07001243
1244 case CEPH_MSGR_TAG_RESETSESSION:
1245 /*
1246 * If we connected with a large connect_seq but the peer
1247 * has no record of a session with us (no connection, or
1248 * connect_seq == 0), they will send RESETSESION to indicate
1249 * that they must have reset their session, and may have
1250 * dropped messages.
1251 */
1252 dout("process_connect got RESET peer seq %u\n",
1253 le32_to_cpu(con->in_connect.connect_seq));
1254 pr_err("%s%lld %s connection reset\n",
1255 ENTITY_NAME(con->peer_name),
1256 pr_addr(&con->peer_addr.in_addr));
1257 reset_connection(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001258 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001259 prepare_read_connect(con);
1260
1261 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001262 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001263 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1264 if (con->ops->peer_reset)
1265 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001266 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001267 break;
1268
1269 case CEPH_MSGR_TAG_RETRY_SESSION:
1270 /*
1271 * If we sent a smaller connect_seq than the peer has, try
1272 * again with a larger value.
1273 */
1274 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1275 le32_to_cpu(con->out_connect.connect_seq),
1276 le32_to_cpu(con->in_connect.connect_seq));
1277 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Sage Weileed0ef22009-11-10 14:34:36 -08001278 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001279 prepare_read_connect(con);
1280 break;
1281
1282 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1283 /*
1284 * If we sent a smaller global_seq than the peer has, try
1285 * again with a larger value.
1286 */
Sage Weileed0ef22009-11-10 14:34:36 -08001287 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001288 con->peer_global_seq,
1289 le32_to_cpu(con->in_connect.global_seq));
1290 get_global_seq(con->msgr,
1291 le32_to_cpu(con->in_connect.global_seq));
Sage Weileed0ef22009-11-10 14:34:36 -08001292 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001293 prepare_read_connect(con);
1294 break;
1295
1296 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001297 if (req_feat & ~server_feat) {
1298 pr_err("%s%lld %s protocol feature mismatch,"
1299 " my required %llx > server's %llx, need %llx\n",
1300 ENTITY_NAME(con->peer_name),
1301 pr_addr(&con->peer_addr.in_addr),
1302 req_feat, server_feat, req_feat & ~server_feat);
1303 con->error_msg = "missing required protocol features";
1304 fail_protocol(con);
1305 return -1;
1306 }
Sage Weil31b80062009-10-06 11:31:13 -07001307 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001308 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1309 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001310 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001311 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1312 con->peer_global_seq,
1313 le32_to_cpu(con->in_reply.connect_seq),
1314 con->connect_seq);
1315 WARN_ON(con->connect_seq !=
1316 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001317
1318 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1319 set_bit(LOSSYTX, &con->state);
1320
Sage Weil31b80062009-10-06 11:31:13 -07001321 prepare_read_tag(con);
1322 break;
1323
1324 case CEPH_MSGR_TAG_WAIT:
1325 /*
1326 * If there is a connection race (we are opening
1327 * connections to each other), one of us may just have
1328 * to WAIT. This shouldn't happen if we are the
1329 * client.
1330 */
1331 pr_err("process_connect peer connecting WAIT\n");
1332
1333 default:
1334 pr_err("connect protocol error, will retry\n");
1335 con->error_msg = "protocol error, garbage tag during connect";
1336 return -1;
1337 }
1338 return 0;
1339}
1340
1341
1342/*
1343 * read (part of) an ack
1344 */
1345static int read_partial_ack(struct ceph_connection *con)
1346{
1347 int to = 0;
1348
1349 return read_partial(con, &to, sizeof(con->in_temp_ack),
1350 &con->in_temp_ack);
1351}
1352
1353
1354/*
1355 * We can finally discard anything that's been acked.
1356 */
1357static void process_ack(struct ceph_connection *con)
1358{
1359 struct ceph_msg *m;
1360 u64 ack = le64_to_cpu(con->in_temp_ack);
1361 u64 seq;
1362
Sage Weil31b80062009-10-06 11:31:13 -07001363 while (!list_empty(&con->out_sent)) {
1364 m = list_first_entry(&con->out_sent, struct ceph_msg,
1365 list_head);
1366 seq = le64_to_cpu(m->hdr.seq);
1367 if (seq > ack)
1368 break;
1369 dout("got ack for seq %llu type %d at %p\n", seq,
1370 le16_to_cpu(m->hdr.type), m);
1371 ceph_msg_remove(m);
1372 }
Sage Weil31b80062009-10-06 11:31:13 -07001373 prepare_read_tag(con);
1374}
1375
1376
1377
1378
Yehuda Sadeh24504182010-01-08 13:58:34 -08001379static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001380 struct kvec *section,
1381 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001382{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001383 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001384
Yehuda Sadeh24504182010-01-08 13:58:34 -08001385 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001386
Yehuda Sadeh24504182010-01-08 13:58:34 -08001387 while (section->iov_len < sec_len) {
1388 BUG_ON(section->iov_base == NULL);
1389 left = sec_len - section->iov_len;
1390 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1391 section->iov_len, left);
1392 if (ret <= 0)
1393 return ret;
1394 section->iov_len += ret;
1395 if (section->iov_len == sec_len)
1396 *crc = crc32c(0, section->iov_base,
1397 section->iov_len);
1398 }
1399
1400 return 1;
1401}
1402
1403static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1404 struct ceph_msg_header *hdr,
1405 int *skip);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001406
1407
1408static int read_partial_message_pages(struct ceph_connection *con,
1409 struct page **pages,
1410 unsigned data_len, int datacrc)
1411{
1412 void *p;
1413 int ret;
1414 int left;
1415
1416 left = min((int)(data_len - con->in_msg_pos.data_pos),
1417 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1418 /* (page) data */
1419 BUG_ON(pages == NULL);
1420 p = kmap(pages[con->in_msg_pos.page]);
1421 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1422 left);
1423 if (ret > 0 && datacrc)
1424 con->in_data_crc =
1425 crc32c(con->in_data_crc,
1426 p + con->in_msg_pos.page_pos, ret);
1427 kunmap(pages[con->in_msg_pos.page]);
1428 if (ret <= 0)
1429 return ret;
1430 con->in_msg_pos.data_pos += ret;
1431 con->in_msg_pos.page_pos += ret;
1432 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1433 con->in_msg_pos.page_pos = 0;
1434 con->in_msg_pos.page++;
1435 }
1436
1437 return ret;
1438}
1439
1440#ifdef CONFIG_BLOCK
1441static int read_partial_message_bio(struct ceph_connection *con,
1442 struct bio **bio_iter, int *bio_seg,
1443 unsigned data_len, int datacrc)
1444{
1445 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1446 void *p;
1447 int ret, left;
1448
1449 if (IS_ERR(bv))
1450 return PTR_ERR(bv);
1451
1452 left = min((int)(data_len - con->in_msg_pos.data_pos),
1453 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1454
1455 p = kmap(bv->bv_page) + bv->bv_offset;
1456
1457 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1458 left);
1459 if (ret > 0 && datacrc)
1460 con->in_data_crc =
1461 crc32c(con->in_data_crc,
1462 p + con->in_msg_pos.page_pos, ret);
1463 kunmap(bv->bv_page);
1464 if (ret <= 0)
1465 return ret;
1466 con->in_msg_pos.data_pos += ret;
1467 con->in_msg_pos.page_pos += ret;
1468 if (con->in_msg_pos.page_pos == bv->bv_len) {
1469 con->in_msg_pos.page_pos = 0;
1470 iter_bio_next(bio_iter, bio_seg);
1471 }
1472
1473 return ret;
1474}
1475#endif
1476
Sage Weil31b80062009-10-06 11:31:13 -07001477/*
1478 * read (part of) a message.
1479 */
1480static int read_partial_message(struct ceph_connection *con)
1481{
1482 struct ceph_msg *m = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001483 int ret;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001484 int to, left;
Sage Weil31b80062009-10-06 11:31:13 -07001485 unsigned front_len, middle_len, data_len, data_off;
1486 int datacrc = con->msgr->nocrc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001487 int skip;
Sage Weilae187562010-04-22 07:47:01 -07001488 u64 seq;
Sage Weil31b80062009-10-06 11:31:13 -07001489
1490 dout("read_partial_message con %p msg %p\n", con, m);
1491
1492 /* header */
1493 while (con->in_base_pos < sizeof(con->in_hdr)) {
1494 left = sizeof(con->in_hdr) - con->in_base_pos;
1495 ret = ceph_tcp_recvmsg(con->sock,
1496 (char *)&con->in_hdr + con->in_base_pos,
1497 left);
1498 if (ret <= 0)
1499 return ret;
1500 con->in_base_pos += ret;
1501 if (con->in_base_pos == sizeof(con->in_hdr)) {
1502 u32 crc = crc32c(0, (void *)&con->in_hdr,
1503 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1504 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1505 pr_err("read_partial_message bad hdr "
1506 " crc %u != expected %u\n",
1507 crc, con->in_hdr.crc);
1508 return -EBADMSG;
1509 }
1510 }
1511 }
Sage Weil31b80062009-10-06 11:31:13 -07001512 front_len = le32_to_cpu(con->in_hdr.front_len);
1513 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1514 return -EIO;
1515 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1516 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1517 return -EIO;
1518 data_len = le32_to_cpu(con->in_hdr.data_len);
1519 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1520 return -EIO;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001521 data_off = le16_to_cpu(con->in_hdr.data_off);
Sage Weil31b80062009-10-06 11:31:13 -07001522
Sage Weilae187562010-04-22 07:47:01 -07001523 /* verify seq# */
1524 seq = le64_to_cpu(con->in_hdr.seq);
1525 if ((s64)seq - (s64)con->in_seq < 1) {
1526 pr_info("skipping %s%lld %s seq %lld, expected %lld\n",
1527 ENTITY_NAME(con->peer_name),
1528 pr_addr(&con->peer_addr.in_addr),
1529 seq, con->in_seq + 1);
1530 con->in_base_pos = -front_len - middle_len - data_len -
1531 sizeof(m->footer);
1532 con->in_tag = CEPH_MSGR_TAG_READY;
1533 con->in_seq++;
1534 return 0;
1535 } else if ((s64)seq - (s64)con->in_seq > 1) {
1536 pr_err("read_partial_message bad seq %lld expected %lld\n",
1537 seq, con->in_seq + 1);
1538 con->error_msg = "bad message sequence # for incoming message";
1539 return -EBADMSG;
1540 }
1541
Sage Weil31b80062009-10-06 11:31:13 -07001542 /* allocate message? */
1543 if (!con->in_msg) {
1544 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1545 con->in_hdr.front_len, con->in_hdr.data_len);
Sage Weilae32be32010-06-13 10:30:19 -07001546 skip = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001547 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1548 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07001549 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001550 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001551 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001552 con->in_base_pos = -front_len - middle_len - data_len -
1553 sizeof(m->footer);
1554 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001555 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001556 return 0;
1557 }
Sage Weila79832f2010-04-01 16:06:19 -07001558 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001559 con->error_msg =
1560 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001561 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001562 }
1563 m = con->in_msg;
1564 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001565 if (m->middle)
1566 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001567
1568 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001569 if (m->pages)
1570 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1571 else
1572 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001573 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001574 }
1575
1576 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001577 ret = read_partial_message_section(con, &m->front, front_len,
1578 &con->in_front_crc);
1579 if (ret <= 0)
1580 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001581
1582 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001583 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001584 ret = read_partial_message_section(con, &m->middle->vec,
1585 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001586 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001587 if (ret <= 0)
1588 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001589 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001590#ifdef CONFIG_BLOCK
1591 if (m->bio && !m->bio_iter)
1592 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1593#endif
Sage Weil31b80062009-10-06 11:31:13 -07001594
1595 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001596 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001597 if (m->pages) {
1598 ret = read_partial_message_pages(con, m->pages,
1599 data_len, datacrc);
1600 if (ret <= 0)
1601 return ret;
1602#ifdef CONFIG_BLOCK
1603 } else if (m->bio) {
1604
1605 ret = read_partial_message_bio(con,
1606 &m->bio_iter, &m->bio_seg,
1607 data_len, datacrc);
1608 if (ret <= 0)
1609 return ret;
1610#endif
1611 } else {
1612 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001613 }
1614 }
1615
Sage Weil31b80062009-10-06 11:31:13 -07001616 /* footer */
1617 to = sizeof(m->hdr) + sizeof(m->footer);
1618 while (con->in_base_pos < to) {
1619 left = to - con->in_base_pos;
1620 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1621 (con->in_base_pos - sizeof(m->hdr)),
1622 left);
1623 if (ret <= 0)
1624 return ret;
1625 con->in_base_pos += ret;
1626 }
1627 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1628 m, front_len, m->footer.front_crc, middle_len,
1629 m->footer.middle_crc, data_len, m->footer.data_crc);
1630
1631 /* crc ok? */
1632 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1633 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1634 m, con->in_front_crc, m->footer.front_crc);
1635 return -EBADMSG;
1636 }
1637 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1638 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1639 m, con->in_middle_crc, m->footer.middle_crc);
1640 return -EBADMSG;
1641 }
1642 if (datacrc &&
1643 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1644 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1645 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1646 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1647 return -EBADMSG;
1648 }
1649
1650 return 1; /* done! */
1651}
1652
1653/*
1654 * Process message. This happens in the worker thread. The callback should
1655 * be careful not to do anything that waits on other incoming messages or it
1656 * may deadlock.
1657 */
1658static void process_message(struct ceph_connection *con)
1659{
Sage Weil5e095e82009-12-14 14:30:34 -08001660 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001661
Sage Weil5e095e82009-12-14 14:30:34 -08001662 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001663 con->in_msg = NULL;
1664
1665 /* if first message, set peer_name */
1666 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001667 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001668
Sage Weil31b80062009-10-06 11:31:13 -07001669 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001670 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001671
1672 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1673 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001674 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001675 le16_to_cpu(msg->hdr.type),
1676 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1677 le32_to_cpu(msg->hdr.front_len),
1678 le32_to_cpu(msg->hdr.data_len),
1679 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1680 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001681
1682 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001683 prepare_read_tag(con);
1684}
1685
1686
1687/*
1688 * Write something to the socket. Called in a worker thread when the
1689 * socket appears to be writeable and we have something ready to send.
1690 */
1691static int try_write(struct ceph_connection *con)
1692{
1693 struct ceph_messenger *msgr = con->msgr;
1694 int ret = 1;
1695
1696 dout("try_write start %p state %lu nref %d\n", con, con->state,
1697 atomic_read(&con->nref));
1698
Sage Weil31b80062009-10-06 11:31:13 -07001699more:
1700 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1701
1702 /* open the socket first? */
1703 if (con->sock == NULL) {
1704 /*
1705 * if we were STANDBY and are reconnecting _this_
1706 * connection, bump connect_seq now. Always bump
1707 * global_seq.
1708 */
1709 if (test_and_clear_bit(STANDBY, &con->state))
1710 con->connect_seq++;
1711
Sage Weileed0ef22009-11-10 14:34:36 -08001712 prepare_write_banner(msgr, con);
1713 prepare_write_connect(msgr, con, 1);
1714 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001715 set_bit(CONNECTING, &con->state);
Sage Weileed0ef22009-11-10 14:34:36 -08001716 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001717
Sage Weilcf3e5c42009-12-11 09:48:05 -08001718 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001719 con->in_tag = CEPH_MSGR_TAG_READY;
1720 dout("try_write initiating connect on %p new state %lu\n",
1721 con, con->state);
1722 con->sock = ceph_tcp_connect(con);
1723 if (IS_ERR(con->sock)) {
1724 con->sock = NULL;
1725 con->error_msg = "connect error";
1726 ret = -1;
1727 goto out;
1728 }
1729 }
1730
1731more_kvec:
1732 /* kvec data queued? */
1733 if (con->out_skip) {
1734 ret = write_partial_skip(con);
1735 if (ret <= 0)
1736 goto done;
1737 if (ret < 0) {
1738 dout("try_write write_partial_skip err %d\n", ret);
1739 goto done;
1740 }
1741 }
1742 if (con->out_kvec_left) {
1743 ret = write_partial_kvec(con);
1744 if (ret <= 0)
1745 goto done;
Sage Weil31b80062009-10-06 11:31:13 -07001746 }
1747
1748 /* msg pages? */
1749 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001750 if (con->out_msg_done) {
1751 ceph_msg_put(con->out_msg);
1752 con->out_msg = NULL; /* we're done with this one */
1753 goto do_next;
1754 }
1755
Sage Weil31b80062009-10-06 11:31:13 -07001756 ret = write_partial_msg_pages(con);
1757 if (ret == 1)
1758 goto more_kvec; /* we need to send the footer, too! */
1759 if (ret == 0)
1760 goto done;
1761 if (ret < 0) {
1762 dout("try_write write_partial_msg_pages err %d\n",
1763 ret);
1764 goto done;
1765 }
1766 }
1767
Sage Weilc86a2932009-12-14 14:04:30 -08001768do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001769 if (!test_bit(CONNECTING, &con->state)) {
1770 /* is anything else pending? */
1771 if (!list_empty(&con->out_queue)) {
1772 prepare_write_message(con);
1773 goto more;
1774 }
1775 if (con->in_seq > con->in_seq_acked) {
1776 prepare_write_ack(con);
1777 goto more;
1778 }
1779 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1780 prepare_write_keepalive(con);
1781 goto more;
1782 }
1783 }
1784
1785 /* Nothing to do! */
1786 clear_bit(WRITE_PENDING, &con->state);
1787 dout("try_write nothing else to write.\n");
1788done:
1789 ret = 0;
1790out:
Sage Weil31b80062009-10-06 11:31:13 -07001791 dout("try_write done on %p\n", con);
1792 return ret;
1793}
1794
1795
1796
1797/*
1798 * Read what we can from the socket.
1799 */
1800static int try_read(struct ceph_connection *con)
1801{
Sage Weil31b80062009-10-06 11:31:13 -07001802 int ret = -1;
1803
1804 if (!con->sock)
1805 return 0;
1806
1807 if (test_bit(STANDBY, &con->state))
1808 return 0;
1809
1810 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08001811
Sage Weil31b80062009-10-06 11:31:13 -07001812more:
1813 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1814 con->in_base_pos);
1815 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08001816 if (!test_bit(NEGOTIATING, &con->state)) {
1817 dout("try_read connecting\n");
1818 ret = read_partial_banner(con);
1819 if (ret <= 0)
1820 goto done;
1821 if (process_banner(con) < 0) {
1822 ret = -1;
1823 goto out;
1824 }
1825 }
Sage Weil31b80062009-10-06 11:31:13 -07001826 ret = read_partial_connect(con);
1827 if (ret <= 0)
1828 goto done;
1829 if (process_connect(con) < 0) {
1830 ret = -1;
1831 goto out;
1832 }
1833 goto more;
1834 }
1835
1836 if (con->in_base_pos < 0) {
1837 /*
1838 * skipping + discarding content.
1839 *
1840 * FIXME: there must be a better way to do this!
1841 */
1842 static char buf[1024];
1843 int skip = min(1024, -con->in_base_pos);
1844 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1845 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1846 if (ret <= 0)
1847 goto done;
1848 con->in_base_pos += ret;
1849 if (con->in_base_pos)
1850 goto more;
1851 }
1852 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1853 /*
1854 * what's next?
1855 */
1856 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1857 if (ret <= 0)
1858 goto done;
1859 dout("try_read got tag %d\n", (int)con->in_tag);
1860 switch (con->in_tag) {
1861 case CEPH_MSGR_TAG_MSG:
1862 prepare_read_message(con);
1863 break;
1864 case CEPH_MSGR_TAG_ACK:
1865 prepare_read_ack(con);
1866 break;
1867 case CEPH_MSGR_TAG_CLOSE:
1868 set_bit(CLOSED, &con->state); /* fixme */
1869 goto done;
1870 default:
1871 goto bad_tag;
1872 }
1873 }
1874 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1875 ret = read_partial_message(con);
1876 if (ret <= 0) {
1877 switch (ret) {
1878 case -EBADMSG:
1879 con->error_msg = "bad crc";
1880 ret = -EIO;
1881 goto out;
1882 case -EIO:
1883 con->error_msg = "io error";
1884 goto out;
1885 default:
1886 goto done;
1887 }
1888 }
1889 if (con->in_tag == CEPH_MSGR_TAG_READY)
1890 goto more;
1891 process_message(con);
1892 goto more;
1893 }
1894 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1895 ret = read_partial_ack(con);
1896 if (ret <= 0)
1897 goto done;
1898 process_ack(con);
1899 goto more;
1900 }
1901
1902done:
1903 ret = 0;
1904out:
1905 dout("try_read done on %p\n", con);
1906 return ret;
1907
1908bad_tag:
1909 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1910 con->error_msg = "protocol error, garbage tag";
1911 ret = -1;
1912 goto out;
1913}
1914
1915
1916/*
1917 * Atomically queue work on a connection. Bump @con reference to
1918 * avoid races with connection teardown.
1919 *
1920 * There is some trickery going on with QUEUED and BUSY because we
1921 * only want a _single_ thread operating on each connection at any
1922 * point in time, but we want to use all available CPUs.
1923 *
1924 * The worker thread only proceeds if it can atomically set BUSY. It
1925 * clears QUEUED and does it's thing. When it thinks it's done, it
1926 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1927 * (tries again to set BUSY).
1928 *
1929 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1930 * try to queue work. If that fails (work is already queued, or BUSY)
1931 * we give up (work also already being done or is queued) but leave QUEUED
1932 * set so that the worker thread will loop if necessary.
1933 */
1934static void queue_con(struct ceph_connection *con)
1935{
1936 if (test_bit(DEAD, &con->state)) {
1937 dout("queue_con %p ignoring: DEAD\n",
1938 con);
1939 return;
1940 }
1941
1942 if (!con->ops->get(con)) {
1943 dout("queue_con %p ref count 0\n", con);
1944 return;
1945 }
1946
1947 set_bit(QUEUED, &con->state);
1948 if (test_bit(BUSY, &con->state)) {
1949 dout("queue_con %p - already BUSY\n", con);
1950 con->ops->put(con);
1951 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1952 dout("queue_con %p - already queued\n", con);
1953 con->ops->put(con);
1954 } else {
1955 dout("queue_con %p\n", con);
1956 }
1957}
1958
1959/*
1960 * Do some work on a connection. Drop a connection ref when we're done.
1961 */
1962static void con_work(struct work_struct *work)
1963{
1964 struct ceph_connection *con = container_of(work, struct ceph_connection,
1965 work.work);
1966 int backoff = 0;
1967
1968more:
1969 if (test_and_set_bit(BUSY, &con->state) != 0) {
1970 dout("con_work %p BUSY already set\n", con);
1971 goto out;
1972 }
1973 dout("con_work %p start, clearing QUEUED\n", con);
1974 clear_bit(QUEUED, &con->state);
1975
Sage Weil9dd46582010-04-28 13:51:50 -07001976 mutex_lock(&con->mutex);
1977
Sage Weil31b80062009-10-06 11:31:13 -07001978 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1979 dout("con_work CLOSED\n");
1980 con_close_socket(con);
1981 goto done;
1982 }
1983 if (test_and_clear_bit(OPENING, &con->state)) {
1984 /* reopen w/ new peer */
1985 dout("con_work OPENING\n");
1986 con_close_socket(con);
1987 }
1988
1989 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1990 try_read(con) < 0 ||
1991 try_write(con) < 0) {
Sage Weil9dd46582010-04-28 13:51:50 -07001992 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001993 backoff = 1;
1994 ceph_fault(con); /* error/fault path */
Sage Weil9dd46582010-04-28 13:51:50 -07001995 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07001996 }
1997
1998done:
Sage Weil9dd46582010-04-28 13:51:50 -07001999 mutex_unlock(&con->mutex);
2000
2001done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002002 clear_bit(BUSY, &con->state);
2003 dout("con->state=%lu\n", con->state);
2004 if (test_bit(QUEUED, &con->state)) {
Sage Weile2663ab2010-02-16 22:01:03 -08002005 if (!backoff || test_bit(OPENING, &con->state)) {
Sage Weil31b80062009-10-06 11:31:13 -07002006 dout("con_work %p QUEUED reset, looping\n", con);
2007 goto more;
2008 }
2009 dout("con_work %p QUEUED reset, but just faulted\n", con);
2010 clear_bit(QUEUED, &con->state);
2011 }
2012 dout("con_work %p done\n", con);
2013
2014out:
2015 con->ops->put(con);
2016}
2017
2018
2019/*
2020 * Generic error/fault handler. A retry mechanism is used with
2021 * exponential backoff
2022 */
2023static void ceph_fault(struct ceph_connection *con)
2024{
2025 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2026 pr_addr(&con->peer_addr.in_addr), con->error_msg);
2027 dout("fault %p state %lu to peer %s\n",
2028 con, con->state, pr_addr(&con->peer_addr.in_addr));
2029
2030 if (test_bit(LOSSYTX, &con->state)) {
2031 dout("fault on LOSSYTX channel\n");
2032 goto out;
2033 }
2034
Sage Weilec302642009-12-22 10:43:42 -08002035 mutex_lock(&con->mutex);
Sage Weil91e45ce2010-02-15 12:05:09 -08002036 if (test_bit(CLOSED, &con->state))
2037 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002038
Sage Weil31b80062009-10-06 11:31:13 -07002039 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002040
2041 if (con->in_msg) {
2042 ceph_msg_put(con->in_msg);
2043 con->in_msg = NULL;
2044 }
Sage Weil31b80062009-10-06 11:31:13 -07002045
Sage Weile80a52d2010-02-25 12:40:45 -08002046 /* Requeue anything that hasn't been acked */
2047 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002048
Sage Weil31b80062009-10-06 11:31:13 -07002049 /* If there are no messages in the queue, place the connection
2050 * in a STANDBY state (i.e., don't try to reconnect just yet). */
Sage Weil31b80062009-10-06 11:31:13 -07002051 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
2052 dout("fault setting STANDBY\n");
2053 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002054 } else {
2055 /* retry after a delay. */
2056 if (con->delay == 0)
2057 con->delay = BASE_DELAY_INTERVAL;
2058 else if (con->delay < MAX_DELAY_INTERVAL)
2059 con->delay *= 2;
2060 dout("fault queueing %p delay %lu\n", con, con->delay);
2061 con->ops->get(con);
2062 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2063 round_jiffies_relative(con->delay)) == 0)
2064 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07002065 }
2066
Sage Weil91e45ce2010-02-15 12:05:09 -08002067out_unlock:
2068 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002069out:
Sage Weil161fd652010-02-25 12:38:57 -08002070 /*
2071 * in case we faulted due to authentication, invalidate our
2072 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002073 */
Sage Weil161fd652010-02-25 12:38:57 -08002074 if (con->auth_retry && con->ops->invalidate_authorizer) {
2075 dout("calling invalidate_authorizer()\n");
2076 con->ops->invalidate_authorizer(con);
2077 }
2078
Sage Weil31b80062009-10-06 11:31:13 -07002079 if (con->ops->fault)
2080 con->ops->fault(con);
2081}
2082
2083
2084
2085/*
2086 * create a new messenger instance
2087 */
2088struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
2089{
2090 struct ceph_messenger *msgr;
2091
2092 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
2093 if (msgr == NULL)
2094 return ERR_PTR(-ENOMEM);
2095
2096 spin_lock_init(&msgr->global_seq_lock);
2097
2098 /* the zero page is needed if a request is "canceled" while the message
2099 * is being written over the socket */
Yehuda Sadeh31459fe2010-03-17 13:54:02 -07002100 msgr->zero_page = __page_cache_alloc(GFP_KERNEL | __GFP_ZERO);
Sage Weil31b80062009-10-06 11:31:13 -07002101 if (!msgr->zero_page) {
2102 kfree(msgr);
2103 return ERR_PTR(-ENOMEM);
2104 }
2105 kmap(msgr->zero_page);
2106
2107 if (myaddr)
2108 msgr->inst.addr = *myaddr;
2109
2110 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002111 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002112 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002113 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002114
2115 dout("messenger_create %p\n", msgr);
2116 return msgr;
2117}
2118
2119void ceph_messenger_destroy(struct ceph_messenger *msgr)
2120{
2121 dout("destroy %p\n", msgr);
2122 kunmap(msgr->zero_page);
2123 __free_page(msgr->zero_page);
2124 kfree(msgr);
2125 dout("destroyed messenger %p\n", msgr);
2126}
2127
2128/*
2129 * Queue up an outgoing message on the given connection.
2130 */
2131void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2132{
2133 if (test_bit(CLOSED, &con->state)) {
2134 dout("con_send %p closed, dropping %p\n", con, msg);
2135 ceph_msg_put(msg);
2136 return;
2137 }
2138
2139 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002140 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002141
Sage Weil3ca02ef2010-03-01 15:25:00 -08002142 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2143
Sage Weile84346b2010-05-11 21:20:38 -07002144 msg->needs_out_seq = true;
2145
Sage Weil31b80062009-10-06 11:31:13 -07002146 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002147 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002148 BUG_ON(!list_empty(&msg->list_head));
2149 list_add_tail(&msg->list_head, &con->out_queue);
2150 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2151 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2152 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2153 le32_to_cpu(msg->hdr.front_len),
2154 le32_to_cpu(msg->hdr.middle_len),
2155 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002156 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002157
2158 /* if there wasn't anything waiting to send before, queue
2159 * new work */
2160 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2161 queue_con(con);
2162}
2163
2164/*
2165 * Revoke a message that was previously queued for send
2166 */
2167void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2168{
Sage Weilec302642009-12-22 10:43:42 -08002169 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002170 if (!list_empty(&msg->list_head)) {
Sage Weiled98ada2010-07-05 12:15:14 -07002171 dout("con_revoke %p msg %p - was on queue\n", con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002172 list_del_init(&msg->list_head);
2173 ceph_msg_put(msg);
2174 msg->hdr.seq = 0;
Sage Weiled98ada2010-07-05 12:15:14 -07002175 }
2176 if (con->out_msg == msg) {
2177 dout("con_revoke %p msg %p - was sending\n", con, msg);
2178 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002179 if (con->out_kvec_is_msg) {
2180 con->out_skip = con->out_kvec_bytes;
2181 con->out_kvec_is_msg = false;
2182 }
Sage Weiled98ada2010-07-05 12:15:14 -07002183 ceph_msg_put(msg);
2184 msg->hdr.seq = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002185 }
Sage Weilec302642009-12-22 10:43:42 -08002186 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002187}
2188
2189/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002190 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002191 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002192void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002193{
2194 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002195 if (con->in_msg && con->in_msg == msg) {
2196 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2197 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002198 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2199
2200 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002201 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002202 con->in_base_pos = con->in_base_pos -
2203 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002204 front_len -
2205 middle_len -
2206 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002207 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002208 ceph_msg_put(con->in_msg);
2209 con->in_msg = NULL;
2210 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002211 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002212 } else {
2213 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002214 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002215 }
2216 mutex_unlock(&con->mutex);
2217}
2218
2219/*
Sage Weil31b80062009-10-06 11:31:13 -07002220 * Queue a keepalive byte to ensure the tcp connection is alive.
2221 */
2222void ceph_con_keepalive(struct ceph_connection *con)
2223{
2224 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2225 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2226 queue_con(con);
2227}
2228
2229
2230/*
2231 * construct a new message with given type, size
2232 * the new msg has a ref count of 1.
2233 */
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002234struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags)
Sage Weil31b80062009-10-06 11:31:13 -07002235{
2236 struct ceph_msg *m;
2237
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002238 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002239 if (m == NULL)
2240 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002241 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07002242 INIT_LIST_HEAD(&m->list_head);
2243
Sage Weil45c6ceb2010-05-11 15:01:51 -07002244 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002245 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002246 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2247 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002248 m->hdr.front_len = cpu_to_le32(front_len);
2249 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002250 m->hdr.data_len = 0;
2251 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002252 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002253 m->footer.front_crc = 0;
2254 m->footer.middle_crc = 0;
2255 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002256 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002257 m->front_max = front_len;
2258 m->front_is_vmalloc = false;
2259 m->more_to_follow = false;
2260 m->pool = NULL;
2261
2262 /* front */
2263 if (front_len) {
2264 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002265 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002266 PAGE_KERNEL);
2267 m->front_is_vmalloc = true;
2268 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002269 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002270 }
2271 if (m->front.iov_base == NULL) {
2272 pr_err("msg_new can't allocate %d bytes\n",
2273 front_len);
2274 goto out2;
2275 }
2276 } else {
2277 m->front.iov_base = NULL;
2278 }
2279 m->front.iov_len = front_len;
2280
2281 /* middle */
2282 m->middle = NULL;
2283
2284 /* data */
Sage Weilbb257662010-04-01 16:07:23 -07002285 m->nr_pages = 0;
2286 m->pages = NULL;
Sage Weil58bb3b32009-12-23 12:12:31 -08002287 m->pagelist = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002288 m->bio = NULL;
2289 m->bio_iter = NULL;
2290 m->bio_seg = 0;
2291 m->trail = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002292
Sage Weilbb257662010-04-01 16:07:23 -07002293 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002294 return m;
2295
2296out2:
2297 ceph_msg_put(m);
2298out:
Sage Weilbb257662010-04-01 16:07:23 -07002299 pr_err("msg_new can't create type %d front %d\n", type, front_len);
Sage Weila79832f2010-04-01 16:06:19 -07002300 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002301}
2302
2303/*
Sage Weil31b80062009-10-06 11:31:13 -07002304 * Allocate "middle" portion of a message, if it is needed and wasn't
2305 * allocated by alloc_msg. This allows us to read a small fixed-size
2306 * per-type header in the front and then gracefully fail (i.e.,
2307 * propagate the error to the caller based on info in the front) when
2308 * the middle is too large.
2309 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002310static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002311{
2312 int type = le16_to_cpu(msg->hdr.type);
2313 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2314
2315 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2316 ceph_msg_type_name(type), middle_len);
2317 BUG_ON(!middle_len);
2318 BUG_ON(msg->middle);
2319
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002320 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002321 if (!msg->middle)
2322 return -ENOMEM;
2323 return 0;
2324}
2325
Yehuda Sadeh24504182010-01-08 13:58:34 -08002326/*
2327 * Generic message allocator, for incoming messages.
2328 */
2329static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2330 struct ceph_msg_header *hdr,
2331 int *skip)
2332{
2333 int type = le16_to_cpu(hdr->type);
2334 int front_len = le32_to_cpu(hdr->front_len);
2335 int middle_len = le32_to_cpu(hdr->middle_len);
2336 struct ceph_msg *msg = NULL;
2337 int ret;
2338
2339 if (con->ops->alloc_msg) {
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002340 mutex_unlock(&con->mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002341 msg = con->ops->alloc_msg(con, hdr, skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002342 mutex_lock(&con->mutex);
Sage Weila79832f2010-04-01 16:06:19 -07002343 if (!msg || *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002344 return NULL;
2345 }
2346 if (!msg) {
2347 *skip = 0;
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002348 msg = ceph_msg_new(type, front_len, GFP_NOFS);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002349 if (!msg) {
2350 pr_err("unable to allocate msg type %d len %d\n",
2351 type, front_len);
Sage Weila79832f2010-04-01 16:06:19 -07002352 return NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002353 }
2354 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002355 memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002356
Sage Weilbb257662010-04-01 16:07:23 -07002357 if (middle_len && !msg->middle) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002358 ret = ceph_alloc_middle(con, msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002359 if (ret < 0) {
2360 ceph_msg_put(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002361 return NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002362 }
2363 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002364
Yehuda Sadeh24504182010-01-08 13:58:34 -08002365 return msg;
2366}
2367
Sage Weil31b80062009-10-06 11:31:13 -07002368
2369/*
2370 * Free a generically kmalloc'd message.
2371 */
2372void ceph_msg_kfree(struct ceph_msg *m)
2373{
2374 dout("msg_kfree %p\n", m);
2375 if (m->front_is_vmalloc)
2376 vfree(m->front.iov_base);
2377 else
2378 kfree(m->front.iov_base);
2379 kfree(m);
2380}
2381
2382/*
2383 * Drop a msg ref. Destroy as needed.
2384 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002385void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002386{
Sage Weilc2e552e2009-12-07 15:55:05 -08002387 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002388
Sage Weilc2e552e2009-12-07 15:55:05 -08002389 dout("ceph_msg_put last one on %p\n", m);
2390 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002391
Sage Weilc2e552e2009-12-07 15:55:05 -08002392 /* drop middle, data, if any */
2393 if (m->middle) {
2394 ceph_buffer_put(m->middle);
2395 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002396 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002397 m->nr_pages = 0;
2398 m->pages = NULL;
2399
Sage Weil58bb3b32009-12-23 12:12:31 -08002400 if (m->pagelist) {
2401 ceph_pagelist_release(m->pagelist);
2402 kfree(m->pagelist);
2403 m->pagelist = NULL;
2404 }
2405
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002406 m->trail = NULL;
2407
Sage Weilc2e552e2009-12-07 15:55:05 -08002408 if (m->pool)
2409 ceph_msgpool_put(m->pool, m);
2410 else
2411 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002412}
Sage Weil9ec7cab2009-12-14 15:13:47 -08002413
2414void ceph_msg_dump(struct ceph_msg *msg)
2415{
2416 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2417 msg->front_max, msg->nr_pages);
2418 print_hex_dump(KERN_DEBUG, "header: ",
2419 DUMP_PREFIX_OFFSET, 16, 1,
2420 &msg->hdr, sizeof(msg->hdr), true);
2421 print_hex_dump(KERN_DEBUG, " front: ",
2422 DUMP_PREFIX_OFFSET, 16, 1,
2423 msg->front.iov_base, msg->front.iov_len, true);
2424 if (msg->middle)
2425 print_hex_dump(KERN_DEBUG, "middle: ",
2426 DUMP_PREFIX_OFFSET, 16, 1,
2427 msg->middle->vec.iov_base,
2428 msg->middle->vec.iov_len, true);
2429 print_hex_dump(KERN_DEBUG, "footer: ",
2430 DUMP_PREFIX_OFFSET, 16, 1,
2431 &msg->footer, sizeof(msg->footer), true);
2432}