blob: b12604ef184667e3fac17ee595495af2b6db49ac [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>
9#include <linux/socket.h>
10#include <linux/string.h>
11#include <net/tcp.h>
12
13#include "super.h"
14#include "messenger.h"
Sage Weil63f2d212009-11-03 15:17:56 -080015#include "decode.h"
Sage Weil31b80062009-10-06 11:31:13 -070016
17/*
18 * Ceph uses the messenger to exchange ceph_msg messages with other
19 * hosts in the system. The messenger provides ordered and reliable
20 * delivery. We tolerate TCP disconnects by reconnecting (with
21 * exponential backoff) in the case of a fault (disconnection, bad
22 * crc, protocol error). Acks allow sent messages to be discarded by
23 * the sender.
24 */
25
26/* static tag bytes (protocol control messages) */
27static char tag_msg = CEPH_MSGR_TAG_MSG;
28static char tag_ack = CEPH_MSGR_TAG_ACK;
29static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
30
31
32static void queue_con(struct ceph_connection *con);
33static void con_work(struct work_struct *);
34static void ceph_fault(struct ceph_connection *con);
35
36const char *ceph_name_type_str(int t)
37{
38 switch (t) {
39 case CEPH_ENTITY_TYPE_MON: return "mon";
40 case CEPH_ENTITY_TYPE_MDS: return "mds";
41 case CEPH_ENTITY_TYPE_OSD: return "osd";
42 case CEPH_ENTITY_TYPE_CLIENT: return "client";
43 case CEPH_ENTITY_TYPE_ADMIN: return "admin";
44 default: return "???";
45 }
46}
47
48/*
49 * nicely render a sockaddr as a string.
50 */
51#define MAX_ADDR_STR 20
52static char addr_str[MAX_ADDR_STR][40];
53static DEFINE_SPINLOCK(addr_str_lock);
54static int last_addr_str;
55
56const char *pr_addr(const struct sockaddr_storage *ss)
57{
58 int i;
59 char *s;
60 struct sockaddr_in *in4 = (void *)ss;
61 unsigned char *quad = (void *)&in4->sin_addr.s_addr;
62 struct sockaddr_in6 *in6 = (void *)ss;
63
64 spin_lock(&addr_str_lock);
65 i = last_addr_str++;
66 if (last_addr_str == MAX_ADDR_STR)
67 last_addr_str = 0;
68 spin_unlock(&addr_str_lock);
69 s = addr_str[i];
70
71 switch (ss->ss_family) {
72 case AF_INET:
73 sprintf(s, "%u.%u.%u.%u:%u",
74 (unsigned int)quad[0],
75 (unsigned int)quad[1],
76 (unsigned int)quad[2],
77 (unsigned int)quad[3],
78 (unsigned int)ntohs(in4->sin_port));
79 break;
80
81 case AF_INET6:
82 sprintf(s, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u",
83 in6->sin6_addr.s6_addr16[0],
84 in6->sin6_addr.s6_addr16[1],
85 in6->sin6_addr.s6_addr16[2],
86 in6->sin6_addr.s6_addr16[3],
87 in6->sin6_addr.s6_addr16[4],
88 in6->sin6_addr.s6_addr16[5],
89 in6->sin6_addr.s6_addr16[6],
90 in6->sin6_addr.s6_addr16[7],
91 (unsigned int)ntohs(in6->sin6_port));
92 break;
93
94 default:
95 sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family);
96 }
97
98 return s;
99}
100
Sage Weil63f2d212009-11-03 15:17:56 -0800101static void encode_my_addr(struct ceph_messenger *msgr)
102{
103 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
104 ceph_encode_addr(&msgr->my_enc_addr);
105}
106
Sage Weil31b80062009-10-06 11:31:13 -0700107/*
108 * work queue for all reading and writing to/from the socket.
109 */
110struct workqueue_struct *ceph_msgr_wq;
111
112int __init ceph_msgr_init(void)
113{
114 ceph_msgr_wq = create_workqueue("ceph-msgr");
115 if (IS_ERR(ceph_msgr_wq)) {
116 int ret = PTR_ERR(ceph_msgr_wq);
117 pr_err("msgr_init failed to create workqueue: %d\n", ret);
118 ceph_msgr_wq = NULL;
119 return ret;
120 }
121 return 0;
122}
123
124void ceph_msgr_exit(void)
125{
126 destroy_workqueue(ceph_msgr_wq);
127}
128
129/*
130 * socket callback functions
131 */
132
133/* data available on socket, or listen socket received a connect */
134static void ceph_data_ready(struct sock *sk, int count_unused)
135{
136 struct ceph_connection *con =
137 (struct ceph_connection *)sk->sk_user_data;
138 if (sk->sk_state != TCP_CLOSE_WAIT) {
139 dout("ceph_data_ready on %p state = %lu, queueing work\n",
140 con, con->state);
141 queue_con(con);
142 }
143}
144
145/* socket has buffer space for writing */
146static void ceph_write_space(struct sock *sk)
147{
148 struct ceph_connection *con =
149 (struct ceph_connection *)sk->sk_user_data;
150
151 /* only queue to workqueue if there is data we want to write. */
152 if (test_bit(WRITE_PENDING, &con->state)) {
153 dout("ceph_write_space %p queueing write work\n", con);
154 queue_con(con);
155 } else {
156 dout("ceph_write_space %p nothing to write\n", con);
157 }
158
159 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
160 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
161}
162
163/* socket's state has changed */
164static void ceph_state_change(struct sock *sk)
165{
166 struct ceph_connection *con =
167 (struct ceph_connection *)sk->sk_user_data;
168
169 dout("ceph_state_change %p state = %lu sk_state = %u\n",
170 con, con->state, sk->sk_state);
171
172 if (test_bit(CLOSED, &con->state))
173 return;
174
175 switch (sk->sk_state) {
176 case TCP_CLOSE:
177 dout("ceph_state_change TCP_CLOSE\n");
178 case TCP_CLOSE_WAIT:
179 dout("ceph_state_change TCP_CLOSE_WAIT\n");
180 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
181 if (test_bit(CONNECTING, &con->state))
182 con->error_msg = "connection failed";
183 else
184 con->error_msg = "socket closed";
185 queue_con(con);
186 }
187 break;
188 case TCP_ESTABLISHED:
189 dout("ceph_state_change TCP_ESTABLISHED\n");
190 queue_con(con);
191 break;
192 }
193}
194
195/*
196 * set up socket callbacks
197 */
198static void set_sock_callbacks(struct socket *sock,
199 struct ceph_connection *con)
200{
201 struct sock *sk = sock->sk;
202 sk->sk_user_data = (void *)con;
203 sk->sk_data_ready = ceph_data_ready;
204 sk->sk_write_space = ceph_write_space;
205 sk->sk_state_change = ceph_state_change;
206}
207
208
209/*
210 * socket helpers
211 */
212
213/*
214 * initiate connection to a remote socket.
215 */
216static struct socket *ceph_tcp_connect(struct ceph_connection *con)
217{
218 struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr;
219 struct socket *sock;
220 int ret;
221
222 BUG_ON(con->sock);
223 ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
224 if (ret)
225 return ERR_PTR(ret);
226 con->sock = sock;
227 sock->sk->sk_allocation = GFP_NOFS;
228
229 set_sock_callbacks(sock, con);
230
231 dout("connect %s\n", pr_addr(&con->peer_addr.in_addr));
232
233 ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK);
234 if (ret == -EINPROGRESS) {
235 dout("connect %s EINPROGRESS sk_state = %u\n",
236 pr_addr(&con->peer_addr.in_addr),
237 sock->sk->sk_state);
238 ret = 0;
239 }
240 if (ret < 0) {
241 pr_err("connect %s error %d\n",
242 pr_addr(&con->peer_addr.in_addr), ret);
243 sock_release(sock);
244 con->sock = NULL;
245 con->error_msg = "connect error";
246 }
247
248 if (ret < 0)
249 return ERR_PTR(ret);
250 return sock;
251}
252
253static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
254{
255 struct kvec iov = {buf, len};
256 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
257
258 return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
259}
260
261/*
262 * write something. @more is true if caller will be sending more data
263 * shortly.
264 */
265static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
266 size_t kvlen, size_t len, int more)
267{
268 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
269
270 if (more)
271 msg.msg_flags |= MSG_MORE;
272 else
273 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
274
275 return kernel_sendmsg(sock, &msg, iov, kvlen, len);
276}
277
278
279/*
280 * Shutdown/close the socket for the given connection.
281 */
282static int con_close_socket(struct ceph_connection *con)
283{
284 int rc;
285
286 dout("con_close_socket on %p sock %p\n", con, con->sock);
287 if (!con->sock)
288 return 0;
289 set_bit(SOCK_CLOSED, &con->state);
290 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
291 sock_release(con->sock);
292 con->sock = NULL;
293 clear_bit(SOCK_CLOSED, &con->state);
294 return rc;
295}
296
297/*
298 * Reset a connection. Discard all incoming and outgoing messages
299 * and clear *_seq state.
300 */
301static void ceph_msg_remove(struct ceph_msg *msg)
302{
303 list_del_init(&msg->list_head);
304 ceph_msg_put(msg);
305}
306static void ceph_msg_remove_list(struct list_head *head)
307{
308 while (!list_empty(head)) {
309 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
310 list_head);
311 ceph_msg_remove(msg);
312 }
313}
314
315static void reset_connection(struct ceph_connection *con)
316{
317 /* reset connection, out_queue, msg_ and connect_seq */
318 /* discard existing out_queue and msg_seq */
319 mutex_lock(&con->out_mutex);
320 ceph_msg_remove_list(&con->out_queue);
321 ceph_msg_remove_list(&con->out_sent);
322
Sage Weilcf3e5c42009-12-11 09:48:05 -0800323 if (con->in_msg) {
324 ceph_msg_put(con->in_msg);
325 con->in_msg = NULL;
326 }
327
Sage Weil31b80062009-10-06 11:31:13 -0700328 con->connect_seq = 0;
329 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800330 if (con->out_msg) {
331 ceph_msg_put(con->out_msg);
332 con->out_msg = NULL;
333 }
Sage Weil31b80062009-10-06 11:31:13 -0700334 con->in_seq = 0;
335 mutex_unlock(&con->out_mutex);
336}
337
338/*
339 * mark a peer down. drop any open connections.
340 */
341void ceph_con_close(struct ceph_connection *con)
342{
343 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
344 set_bit(CLOSED, &con->state); /* in case there's queued work */
345 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
346 reset_connection(con);
347 queue_con(con);
348}
349
350/*
Sage Weil31b80062009-10-06 11:31:13 -0700351 * Reopen a closed connection, with a new peer address.
352 */
353void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
354{
355 dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
356 set_bit(OPENING, &con->state);
357 clear_bit(CLOSED, &con->state);
358 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800359 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700360 queue_con(con);
361}
362
363/*
364 * generic get/put
365 */
366struct ceph_connection *ceph_con_get(struct ceph_connection *con)
367{
368 dout("con_get %p nref = %d -> %d\n", con,
369 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
370 if (atomic_inc_not_zero(&con->nref))
371 return con;
372 return NULL;
373}
374
375void ceph_con_put(struct ceph_connection *con)
376{
377 dout("con_put %p nref = %d -> %d\n", con,
378 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
379 BUG_ON(atomic_read(&con->nref) == 0);
380 if (atomic_dec_and_test(&con->nref)) {
Sage Weil71ececd2009-11-18 11:27:06 -0800381 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700382 kfree(con);
383 }
384}
385
386/*
387 * initialize a new connection.
388 */
389void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
390{
391 dout("con_init %p\n", con);
392 memset(con, 0, sizeof(*con));
393 atomic_set(&con->nref, 1);
394 con->msgr = msgr;
395 mutex_init(&con->out_mutex);
396 INIT_LIST_HEAD(&con->out_queue);
397 INIT_LIST_HEAD(&con->out_sent);
398 INIT_DELAYED_WORK(&con->work, con_work);
399}
400
401
402/*
403 * We maintain a global counter to order connection attempts. Get
404 * a unique seq greater than @gt.
405 */
406static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
407{
408 u32 ret;
409
410 spin_lock(&msgr->global_seq_lock);
411 if (msgr->global_seq < gt)
412 msgr->global_seq = gt;
413 ret = ++msgr->global_seq;
414 spin_unlock(&msgr->global_seq_lock);
415 return ret;
416}
417
418
419/*
420 * Prepare footer for currently outgoing message, and finish things
421 * off. Assumes out_kvec* are already valid.. we just add on to the end.
422 */
423static void prepare_write_message_footer(struct ceph_connection *con, int v)
424{
425 struct ceph_msg *m = con->out_msg;
426
427 dout("prepare_write_message_footer %p\n", con);
428 con->out_kvec_is_msg = true;
429 con->out_kvec[v].iov_base = &m->footer;
430 con->out_kvec[v].iov_len = sizeof(m->footer);
431 con->out_kvec_bytes += sizeof(m->footer);
432 con->out_kvec_left++;
433 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800434 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700435}
436
437/*
438 * Prepare headers for the next outgoing message.
439 */
440static void prepare_write_message(struct ceph_connection *con)
441{
442 struct ceph_msg *m;
443 int v = 0;
444
445 con->out_kvec_bytes = 0;
446 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800447 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700448
449 /* Sneak an ack in there first? If we can get it into the same
450 * TCP packet that's a good thing. */
451 if (con->in_seq > con->in_seq_acked) {
452 con->in_seq_acked = con->in_seq;
453 con->out_kvec[v].iov_base = &tag_ack;
454 con->out_kvec[v++].iov_len = 1;
455 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
456 con->out_kvec[v].iov_base = &con->out_temp_ack;
457 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
458 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
459 }
460
Sage Weil31b80062009-10-06 11:31:13 -0700461 m = list_first_entry(&con->out_queue,
462 struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800463 con->out_msg = m;
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800464 if (test_bit(LOSSYTX, &con->state)) {
465 /* put message on sent list */
466 ceph_msg_get(m);
467 list_move_tail(&m->list_head, &con->out_sent);
468 } else {
469 list_del_init(&m->list_head);
470 }
Sage Weil31b80062009-10-06 11:31:13 -0700471
472 m->hdr.seq = cpu_to_le64(++con->out_seq);
473
474 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
475 m, con->out_seq, le16_to_cpu(m->hdr.type),
476 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
477 le32_to_cpu(m->hdr.data_len),
478 m->nr_pages);
479 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
480
481 /* tag + hdr + front + middle */
482 con->out_kvec[v].iov_base = &tag_msg;
483 con->out_kvec[v++].iov_len = 1;
484 con->out_kvec[v].iov_base = &m->hdr;
485 con->out_kvec[v++].iov_len = sizeof(m->hdr);
486 con->out_kvec[v++] = m->front;
487 if (m->middle)
488 con->out_kvec[v++] = m->middle->vec;
489 con->out_kvec_left = v;
490 con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
491 (m->middle ? m->middle->vec.iov_len : 0);
492 con->out_kvec_cur = con->out_kvec;
493
494 /* fill in crc (except data pages), footer */
495 con->out_msg->hdr.crc =
496 cpu_to_le32(crc32c(0, (void *)&m->hdr,
497 sizeof(m->hdr) - sizeof(m->hdr.crc)));
498 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
499 con->out_msg->footer.front_crc =
500 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
501 if (m->middle)
502 con->out_msg->footer.middle_crc =
503 cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
504 m->middle->vec.iov_len));
505 else
506 con->out_msg->footer.middle_crc = 0;
507 con->out_msg->footer.data_crc = 0;
508 dout("prepare_write_message front_crc %u data_crc %u\n",
509 le32_to_cpu(con->out_msg->footer.front_crc),
510 le32_to_cpu(con->out_msg->footer.middle_crc));
511
512 /* is there a data payload? */
513 if (le32_to_cpu(m->hdr.data_len) > 0) {
514 /* initialize page iterator */
515 con->out_msg_pos.page = 0;
516 con->out_msg_pos.page_pos =
517 le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
518 con->out_msg_pos.data_pos = 0;
519 con->out_msg_pos.did_page_crc = 0;
520 con->out_more = 1; /* data + footer will follow */
521 } else {
522 /* no, queue up footer too and be done */
523 prepare_write_message_footer(con, v);
524 }
525
526 set_bit(WRITE_PENDING, &con->state);
527}
528
529/*
530 * Prepare an ack.
531 */
532static void prepare_write_ack(struct ceph_connection *con)
533{
534 dout("prepare_write_ack %p %llu -> %llu\n", con,
535 con->in_seq_acked, con->in_seq);
536 con->in_seq_acked = con->in_seq;
537
538 con->out_kvec[0].iov_base = &tag_ack;
539 con->out_kvec[0].iov_len = 1;
540 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
541 con->out_kvec[1].iov_base = &con->out_temp_ack;
542 con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
543 con->out_kvec_left = 2;
544 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
545 con->out_kvec_cur = con->out_kvec;
546 con->out_more = 1; /* more will follow.. eventually.. */
547 set_bit(WRITE_PENDING, &con->state);
548}
549
550/*
551 * Prepare to write keepalive byte.
552 */
553static void prepare_write_keepalive(struct ceph_connection *con)
554{
555 dout("prepare_write_keepalive %p\n", con);
556 con->out_kvec[0].iov_base = &tag_keepalive;
557 con->out_kvec[0].iov_len = 1;
558 con->out_kvec_left = 1;
559 con->out_kvec_bytes = 1;
560 con->out_kvec_cur = con->out_kvec;
561 set_bit(WRITE_PENDING, &con->state);
562}
563
564/*
565 * Connection negotiation.
566 */
567
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800568static void prepare_connect_authorizer(struct ceph_connection *con)
569{
570 void *auth_buf;
571 int auth_len = 0;
572 int auth_protocol = 0;
573
574 if (con->ops->get_authorizer)
575 con->ops->get_authorizer(con, &auth_buf, &auth_len,
576 &auth_protocol, &con->auth_reply_buf,
577 &con->auth_reply_buf_len,
578 con->auth_retry);
579
580 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
581 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
582
583 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
584 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
585 con->out_kvec_left++;
586 con->out_kvec_bytes += auth_len;
587}
588
Sage Weil31b80062009-10-06 11:31:13 -0700589/*
590 * We connected to a peer and are saying hello.
591 */
Sage Weileed0ef22009-11-10 14:34:36 -0800592static void prepare_write_banner(struct ceph_messenger *msgr,
593 struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700594{
595 int len = strlen(CEPH_BANNER);
Sage Weileed0ef22009-11-10 14:34:36 -0800596
597 con->out_kvec[0].iov_base = CEPH_BANNER;
598 con->out_kvec[0].iov_len = len;
599 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
600 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
601 con->out_kvec_left = 2;
602 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
603 con->out_kvec_cur = con->out_kvec;
604 con->out_more = 0;
605 set_bit(WRITE_PENDING, &con->state);
606}
607
608static void prepare_write_connect(struct ceph_messenger *msgr,
609 struct ceph_connection *con,
610 int after_banner)
611{
Sage Weil31b80062009-10-06 11:31:13 -0700612 unsigned global_seq = get_global_seq(con->msgr, 0);
613 int proto;
614
615 switch (con->peer_name.type) {
616 case CEPH_ENTITY_TYPE_MON:
617 proto = CEPH_MONC_PROTOCOL;
618 break;
619 case CEPH_ENTITY_TYPE_OSD:
620 proto = CEPH_OSDC_PROTOCOL;
621 break;
622 case CEPH_ENTITY_TYPE_MDS:
623 proto = CEPH_MDSC_PROTOCOL;
624 break;
625 default:
626 BUG();
627 }
628
629 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
630 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800631
Sage Weil31b80062009-10-06 11:31:13 -0700632 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
633 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
634 con->out_connect.global_seq = cpu_to_le32(global_seq);
635 con->out_connect.protocol_version = cpu_to_le32(proto);
636 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700637
Sage Weileed0ef22009-11-10 14:34:36 -0800638 if (!after_banner) {
639 con->out_kvec_left = 0;
640 con->out_kvec_bytes = 0;
641 }
642 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
643 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
644 con->out_kvec_left++;
645 con->out_kvec_bytes += sizeof(con->out_connect);
Sage Weil31b80062009-10-06 11:31:13 -0700646 con->out_kvec_cur = con->out_kvec;
647 con->out_more = 0;
648 set_bit(WRITE_PENDING, &con->state);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800649
650 prepare_connect_authorizer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700651}
652
653
654/*
655 * write as much of pending kvecs to the socket as we can.
656 * 1 -> done
657 * 0 -> socket full, but more to do
658 * <0 -> error
659 */
660static int write_partial_kvec(struct ceph_connection *con)
661{
662 int ret;
663
664 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
665 while (con->out_kvec_bytes > 0) {
666 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
667 con->out_kvec_left, con->out_kvec_bytes,
668 con->out_more);
669 if (ret <= 0)
670 goto out;
671 con->out_kvec_bytes -= ret;
672 if (con->out_kvec_bytes == 0)
673 break; /* done */
674 while (ret > 0) {
675 if (ret >= con->out_kvec_cur->iov_len) {
676 ret -= con->out_kvec_cur->iov_len;
677 con->out_kvec_cur++;
678 con->out_kvec_left--;
679 } else {
680 con->out_kvec_cur->iov_len -= ret;
681 con->out_kvec_cur->iov_base += ret;
682 ret = 0;
683 break;
684 }
685 }
686 }
687 con->out_kvec_left = 0;
688 con->out_kvec_is_msg = false;
689 ret = 1;
690out:
691 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
692 con->out_kvec_bytes, con->out_kvec_left, ret);
693 return ret; /* done! */
694}
695
696/*
697 * Write as much message data payload as we can. If we finish, queue
698 * up the footer.
699 * 1 -> done, footer is now queued in out_kvec[].
700 * 0 -> socket full, but more to do
701 * <0 -> error
702 */
703static int write_partial_msg_pages(struct ceph_connection *con)
704{
705 struct ceph_msg *msg = con->out_msg;
706 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
707 size_t len;
708 int crc = con->msgr->nocrc;
709 int ret;
710
711 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
712 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
713 con->out_msg_pos.page_pos);
714
715 while (con->out_msg_pos.page < con->out_msg->nr_pages) {
716 struct page *page = NULL;
717 void *kaddr = NULL;
718
719 /*
720 * if we are calculating the data crc (the default), we need
721 * to map the page. if our pages[] has been revoked, use the
722 * zero page.
723 */
724 if (msg->pages) {
725 page = msg->pages[con->out_msg_pos.page];
726 if (crc)
727 kaddr = kmap(page);
728 } else {
729 page = con->msgr->zero_page;
730 if (crc)
731 kaddr = page_address(con->msgr->zero_page);
732 }
733 len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
734 (int)(data_len - con->out_msg_pos.data_pos));
735 if (crc && !con->out_msg_pos.did_page_crc) {
736 void *base = kaddr + con->out_msg_pos.page_pos;
737 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
738
739 BUG_ON(kaddr == NULL);
740 con->out_msg->footer.data_crc =
741 cpu_to_le32(crc32c(tmpcrc, base, len));
742 con->out_msg_pos.did_page_crc = 1;
743 }
744
745 ret = kernel_sendpage(con->sock, page,
746 con->out_msg_pos.page_pos, len,
747 MSG_DONTWAIT | MSG_NOSIGNAL |
748 MSG_MORE);
749
750 if (crc && msg->pages)
751 kunmap(page);
752
753 if (ret <= 0)
754 goto out;
755
756 con->out_msg_pos.data_pos += ret;
757 con->out_msg_pos.page_pos += ret;
758 if (ret == len) {
759 con->out_msg_pos.page_pos = 0;
760 con->out_msg_pos.page++;
761 con->out_msg_pos.did_page_crc = 0;
762 }
763 }
764
765 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
766
767 /* prepare and queue up footer, too */
768 if (!crc)
769 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
770 con->out_kvec_bytes = 0;
771 con->out_kvec_left = 0;
772 con->out_kvec_cur = con->out_kvec;
773 prepare_write_message_footer(con, 0);
774 ret = 1;
775out:
776 return ret;
777}
778
779/*
780 * write some zeros
781 */
782static int write_partial_skip(struct ceph_connection *con)
783{
784 int ret;
785
786 while (con->out_skip > 0) {
787 struct kvec iov = {
788 .iov_base = page_address(con->msgr->zero_page),
789 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
790 };
791
792 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
793 if (ret <= 0)
794 goto out;
795 con->out_skip -= ret;
796 }
797 ret = 1;
798out:
799 return ret;
800}
801
802/*
803 * Prepare to read connection handshake, or an ack.
804 */
Sage Weileed0ef22009-11-10 14:34:36 -0800805static void prepare_read_banner(struct ceph_connection *con)
806{
807 dout("prepare_read_banner %p\n", con);
808 con->in_base_pos = 0;
809}
810
Sage Weil31b80062009-10-06 11:31:13 -0700811static void prepare_read_connect(struct ceph_connection *con)
812{
813 dout("prepare_read_connect %p\n", con);
814 con->in_base_pos = 0;
815}
816
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800817static void prepare_read_connect_retry(struct ceph_connection *con)
818{
819 dout("prepare_read_connect_retry %p\n", con);
820 con->in_base_pos = strlen(CEPH_BANNER) + sizeof(con->actual_peer_addr)
821 + sizeof(con->peer_addr_for_me);
822}
823
Sage Weil31b80062009-10-06 11:31:13 -0700824static void prepare_read_ack(struct ceph_connection *con)
825{
826 dout("prepare_read_ack %p\n", con);
827 con->in_base_pos = 0;
828}
829
830static void prepare_read_tag(struct ceph_connection *con)
831{
832 dout("prepare_read_tag %p\n", con);
833 con->in_base_pos = 0;
834 con->in_tag = CEPH_MSGR_TAG_READY;
835}
836
837/*
838 * Prepare to read a message.
839 */
840static int prepare_read_message(struct ceph_connection *con)
841{
842 dout("prepare_read_message %p\n", con);
843 BUG_ON(con->in_msg != NULL);
844 con->in_base_pos = 0;
845 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
846 return 0;
847}
848
849
850static int read_partial(struct ceph_connection *con,
851 int *to, int size, void *object)
852{
853 *to += size;
854 while (con->in_base_pos < *to) {
855 int left = *to - con->in_base_pos;
856 int have = size - left;
857 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
858 if (ret <= 0)
859 return ret;
860 con->in_base_pos += ret;
861 }
862 return 1;
863}
864
865
866/*
867 * Read all or part of the connect-side handshake on a new connection
868 */
Sage Weileed0ef22009-11-10 14:34:36 -0800869static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700870{
871 int ret, to = 0;
872
Sage Weileed0ef22009-11-10 14:34:36 -0800873 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -0700874
875 /* peer's banner */
876 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
877 if (ret <= 0)
878 goto out;
879 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
880 &con->actual_peer_addr);
881 if (ret <= 0)
882 goto out;
883 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
884 &con->peer_addr_for_me);
885 if (ret <= 0)
886 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -0800887out:
888 return ret;
889}
890
891static int read_partial_connect(struct ceph_connection *con)
892{
893 int ret, to = 0;
894
895 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
896
Sage Weil31b80062009-10-06 11:31:13 -0700897 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
898 if (ret <= 0)
899 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800900 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
901 con->auth_reply_buf);
902 if (ret <= 0)
903 goto out;
Sage Weil31b80062009-10-06 11:31:13 -0700904
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800905 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
906 con, (int)con->in_reply.tag,
907 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -0700908 le32_to_cpu(con->in_reply.global_seq));
909out:
910 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -0800911
Sage Weil31b80062009-10-06 11:31:13 -0700912}
913
914/*
915 * Verify the hello banner looks okay.
916 */
917static int verify_hello(struct ceph_connection *con)
918{
919 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -0700920 pr_err("connect to %s got bad banner\n",
Sage Weil31b80062009-10-06 11:31:13 -0700921 pr_addr(&con->peer_addr.in_addr));
922 con->error_msg = "protocol error, bad banner";
923 return -1;
924 }
925 return 0;
926}
927
928static bool addr_is_blank(struct sockaddr_storage *ss)
929{
930 switch (ss->ss_family) {
931 case AF_INET:
932 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
933 case AF_INET6:
934 return
935 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
936 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
937 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
938 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
939 }
940 return false;
941}
942
943static int addr_port(struct sockaddr_storage *ss)
944{
945 switch (ss->ss_family) {
946 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800947 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -0700948 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800949 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -0700950 }
951 return 0;
952}
953
954static void addr_set_port(struct sockaddr_storage *ss, int p)
955{
956 switch (ss->ss_family) {
957 case AF_INET:
958 ((struct sockaddr_in *)ss)->sin_port = htons(p);
959 case AF_INET6:
960 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
961 }
962}
963
964/*
965 * Parse an ip[:port] list into an addr array. Use the default
966 * monitor port if a port isn't specified.
967 */
968int ceph_parse_ips(const char *c, const char *end,
969 struct ceph_entity_addr *addr,
970 int max_count, int *count)
971{
972 int i;
973 const char *p = c;
974
975 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
976 for (i = 0; i < max_count; i++) {
977 const char *ipend;
978 struct sockaddr_storage *ss = &addr[i].in_addr;
979 struct sockaddr_in *in4 = (void *)ss;
980 struct sockaddr_in6 *in6 = (void *)ss;
981 int port;
982
983 memset(ss, 0, sizeof(*ss));
984 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
985 ',', &ipend)) {
986 ss->ss_family = AF_INET;
987 } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
988 ',', &ipend)) {
989 ss->ss_family = AF_INET6;
990 } else {
991 goto bad;
992 }
993 p = ipend;
994
995 /* port? */
996 if (p < end && *p == ':') {
997 port = 0;
998 p++;
999 while (p < end && *p >= '0' && *p <= '9') {
1000 port = (port * 10) + (*p - '0');
1001 p++;
1002 }
1003 if (port > 65535 || port == 0)
1004 goto bad;
1005 } else {
1006 port = CEPH_MON_PORT;
1007 }
1008
1009 addr_set_port(ss, port);
1010
1011 dout("parse_ips got %s\n", pr_addr(ss));
1012
1013 if (p == end)
1014 break;
1015 if (*p != ',')
1016 goto bad;
1017 p++;
1018 }
1019
1020 if (p != end)
1021 goto bad;
1022
1023 if (count)
1024 *count = i + 1;
1025 return 0;
1026
1027bad:
1028 pr_err("parse_ips bad ip '%s'\n", c);
1029 return -EINVAL;
1030}
1031
Sage Weileed0ef22009-11-10 14:34:36 -08001032static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001033{
Sage Weileed0ef22009-11-10 14:34:36 -08001034 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001035
1036 if (verify_hello(con) < 0)
1037 return -1;
1038
Sage Weil63f2d212009-11-03 15:17:56 -08001039 ceph_decode_addr(&con->actual_peer_addr);
1040 ceph_decode_addr(&con->peer_addr_for_me);
1041
Sage Weil31b80062009-10-06 11:31:13 -07001042 /*
1043 * Make sure the other end is who we wanted. note that the other
1044 * end may not yet know their ip address, so if it's 0.0.0.0, give
1045 * them the benefit of the doubt.
1046 */
1047 if (!ceph_entity_addr_is_local(&con->peer_addr,
1048 &con->actual_peer_addr) &&
1049 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1050 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
1051 pr_err("wrong peer, want %s/%d, "
1052 "got %s/%d, wtf\n",
1053 pr_addr(&con->peer_addr.in_addr),
1054 con->peer_addr.nonce,
1055 pr_addr(&con->actual_peer_addr.in_addr),
1056 con->actual_peer_addr.nonce);
1057 con->error_msg = "protocol error, wrong peer";
1058 return -1;
1059 }
1060
1061 /*
1062 * did we learn our address?
1063 */
1064 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1065 int port = addr_port(&con->msgr->inst.addr.in_addr);
1066
1067 memcpy(&con->msgr->inst.addr.in_addr,
1068 &con->peer_addr_for_me.in_addr,
1069 sizeof(con->peer_addr_for_me.in_addr));
1070 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001071 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001072 dout("process_banner learned my addr is %s\n",
Sage Weil31b80062009-10-06 11:31:13 -07001073 pr_addr(&con->msgr->inst.addr.in_addr));
1074 }
1075
Sage Weileed0ef22009-11-10 14:34:36 -08001076 set_bit(NEGOTIATING, &con->state);
1077 prepare_read_connect(con);
1078 return 0;
1079}
1080
1081static int process_connect(struct ceph_connection *con)
1082{
1083 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1084
Sage Weil31b80062009-10-06 11:31:13 -07001085 switch (con->in_reply.tag) {
1086 case CEPH_MSGR_TAG_BADPROTOVER:
1087 dout("process_connect got BADPROTOVER my %d != their %d\n",
1088 le32_to_cpu(con->out_connect.protocol_version),
1089 le32_to_cpu(con->in_reply.protocol_version));
1090 pr_err("%s%lld %s protocol version mismatch,"
1091 " my %d != server's %d\n",
1092 ENTITY_NAME(con->peer_name),
1093 pr_addr(&con->peer_addr.in_addr),
1094 le32_to_cpu(con->out_connect.protocol_version),
1095 le32_to_cpu(con->in_reply.protocol_version));
1096 con->error_msg = "protocol version mismatch";
1097 if (con->ops->bad_proto)
1098 con->ops->bad_proto(con);
1099 reset_connection(con);
1100 set_bit(CLOSED, &con->state); /* in case there's queued work */
1101 return -1;
1102
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001103 case CEPH_MSGR_TAG_BADAUTHORIZER:
1104 con->auth_retry++;
1105 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1106 con->auth_retry);
1107 if (con->auth_retry == 2) {
1108 con->error_msg = "connect authorization failure";
1109 reset_connection(con);
1110 set_bit(CLOSED, &con->state);
1111 return -1;
1112 }
1113 con->auth_retry = 1;
1114 prepare_write_connect(con->msgr, con, 0);
1115 prepare_read_connect_retry(con);
1116 break;
Sage Weil31b80062009-10-06 11:31:13 -07001117
1118 case CEPH_MSGR_TAG_RESETSESSION:
1119 /*
1120 * If we connected with a large connect_seq but the peer
1121 * has no record of a session with us (no connection, or
1122 * connect_seq == 0), they will send RESETSESION to indicate
1123 * that they must have reset their session, and may have
1124 * dropped messages.
1125 */
1126 dout("process_connect got RESET peer seq %u\n",
1127 le32_to_cpu(con->in_connect.connect_seq));
1128 pr_err("%s%lld %s connection reset\n",
1129 ENTITY_NAME(con->peer_name),
1130 pr_addr(&con->peer_addr.in_addr));
1131 reset_connection(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001132 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001133 prepare_read_connect(con);
1134
1135 /* Tell ceph about it. */
1136 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1137 if (con->ops->peer_reset)
1138 con->ops->peer_reset(con);
1139 break;
1140
1141 case CEPH_MSGR_TAG_RETRY_SESSION:
1142 /*
1143 * If we sent a smaller connect_seq than the peer has, try
1144 * again with a larger value.
1145 */
1146 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1147 le32_to_cpu(con->out_connect.connect_seq),
1148 le32_to_cpu(con->in_connect.connect_seq));
1149 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Sage Weileed0ef22009-11-10 14:34:36 -08001150 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001151 prepare_read_connect(con);
1152 break;
1153
1154 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1155 /*
1156 * If we sent a smaller global_seq than the peer has, try
1157 * again with a larger value.
1158 */
Sage Weileed0ef22009-11-10 14:34:36 -08001159 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001160 con->peer_global_seq,
1161 le32_to_cpu(con->in_connect.global_seq));
1162 get_global_seq(con->msgr,
1163 le32_to_cpu(con->in_connect.global_seq));
Sage Weileed0ef22009-11-10 14:34:36 -08001164 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001165 prepare_read_connect(con);
1166 break;
1167
1168 case CEPH_MSGR_TAG_READY:
1169 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001170 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1171 con->connect_seq++;
1172 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1173 con->peer_global_seq,
1174 le32_to_cpu(con->in_reply.connect_seq),
1175 con->connect_seq);
1176 WARN_ON(con->connect_seq !=
1177 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001178
1179 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1180 set_bit(LOSSYTX, &con->state);
1181
Sage Weil31b80062009-10-06 11:31:13 -07001182 prepare_read_tag(con);
1183 break;
1184
1185 case CEPH_MSGR_TAG_WAIT:
1186 /*
1187 * If there is a connection race (we are opening
1188 * connections to each other), one of us may just have
1189 * to WAIT. This shouldn't happen if we are the
1190 * client.
1191 */
1192 pr_err("process_connect peer connecting WAIT\n");
1193
1194 default:
1195 pr_err("connect protocol error, will retry\n");
1196 con->error_msg = "protocol error, garbage tag during connect";
1197 return -1;
1198 }
1199 return 0;
1200}
1201
1202
1203/*
1204 * read (part of) an ack
1205 */
1206static int read_partial_ack(struct ceph_connection *con)
1207{
1208 int to = 0;
1209
1210 return read_partial(con, &to, sizeof(con->in_temp_ack),
1211 &con->in_temp_ack);
1212}
1213
1214
1215/*
1216 * We can finally discard anything that's been acked.
1217 */
1218static void process_ack(struct ceph_connection *con)
1219{
1220 struct ceph_msg *m;
1221 u64 ack = le64_to_cpu(con->in_temp_ack);
1222 u64 seq;
1223
1224 mutex_lock(&con->out_mutex);
1225 while (!list_empty(&con->out_sent)) {
1226 m = list_first_entry(&con->out_sent, struct ceph_msg,
1227 list_head);
1228 seq = le64_to_cpu(m->hdr.seq);
1229 if (seq > ack)
1230 break;
1231 dout("got ack for seq %llu type %d at %p\n", seq,
1232 le16_to_cpu(m->hdr.type), m);
1233 ceph_msg_remove(m);
1234 }
1235 mutex_unlock(&con->out_mutex);
1236 prepare_read_tag(con);
1237}
1238
1239
1240
1241
1242
1243
1244/*
1245 * read (part of) a message.
1246 */
1247static int read_partial_message(struct ceph_connection *con)
1248{
1249 struct ceph_msg *m = con->in_msg;
1250 void *p;
1251 int ret;
1252 int to, want, left;
1253 unsigned front_len, middle_len, data_len, data_off;
1254 int datacrc = con->msgr->nocrc;
1255
1256 dout("read_partial_message con %p msg %p\n", con, m);
1257
1258 /* header */
1259 while (con->in_base_pos < sizeof(con->in_hdr)) {
1260 left = sizeof(con->in_hdr) - con->in_base_pos;
1261 ret = ceph_tcp_recvmsg(con->sock,
1262 (char *)&con->in_hdr + con->in_base_pos,
1263 left);
1264 if (ret <= 0)
1265 return ret;
1266 con->in_base_pos += ret;
1267 if (con->in_base_pos == sizeof(con->in_hdr)) {
1268 u32 crc = crc32c(0, (void *)&con->in_hdr,
1269 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1270 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1271 pr_err("read_partial_message bad hdr "
1272 " crc %u != expected %u\n",
1273 crc, con->in_hdr.crc);
1274 return -EBADMSG;
1275 }
1276 }
1277 }
1278
1279 front_len = le32_to_cpu(con->in_hdr.front_len);
1280 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1281 return -EIO;
1282 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1283 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1284 return -EIO;
1285 data_len = le32_to_cpu(con->in_hdr.data_len);
1286 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1287 return -EIO;
1288
1289 /* allocate message? */
1290 if (!con->in_msg) {
1291 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1292 con->in_hdr.front_len, con->in_hdr.data_len);
1293 con->in_msg = con->ops->alloc_msg(con, &con->in_hdr);
1294 if (!con->in_msg) {
1295 /* skip this message */
Sage Weilcf3e5c42009-12-11 09:48:05 -08001296 pr_err("alloc_msg returned NULL, skipping message\n");
Sage Weil31b80062009-10-06 11:31:13 -07001297 con->in_base_pos = -front_len - middle_len - data_len -
1298 sizeof(m->footer);
1299 con->in_tag = CEPH_MSGR_TAG_READY;
1300 return 0;
1301 }
1302 if (IS_ERR(con->in_msg)) {
1303 ret = PTR_ERR(con->in_msg);
1304 con->in_msg = NULL;
1305 con->error_msg = "out of memory for incoming message";
1306 return ret;
1307 }
1308 m = con->in_msg;
1309 m->front.iov_len = 0; /* haven't read it yet */
1310 memcpy(&m->hdr, &con->in_hdr, sizeof(con->in_hdr));
1311 }
1312
1313 /* front */
1314 while (m->front.iov_len < front_len) {
1315 BUG_ON(m->front.iov_base == NULL);
1316 left = front_len - m->front.iov_len;
1317 ret = ceph_tcp_recvmsg(con->sock, (char *)m->front.iov_base +
1318 m->front.iov_len, left);
1319 if (ret <= 0)
1320 return ret;
1321 m->front.iov_len += ret;
1322 if (m->front.iov_len == front_len)
1323 con->in_front_crc = crc32c(0, m->front.iov_base,
1324 m->front.iov_len);
1325 }
1326
1327 /* middle */
1328 while (middle_len > 0 && (!m->middle ||
1329 m->middle->vec.iov_len < middle_len)) {
1330 if (m->middle == NULL) {
1331 ret = -EOPNOTSUPP;
1332 if (con->ops->alloc_middle)
1333 ret = con->ops->alloc_middle(con, m);
1334 if (ret < 0) {
Sage Weilcf3e5c42009-12-11 09:48:05 -08001335 pr_err("alloc_middle fail skipping payload\n");
Sage Weil31b80062009-10-06 11:31:13 -07001336 con->in_base_pos = -middle_len - data_len
1337 - sizeof(m->footer);
1338 ceph_msg_put(con->in_msg);
1339 con->in_msg = NULL;
1340 con->in_tag = CEPH_MSGR_TAG_READY;
1341 return 0;
1342 }
1343 m->middle->vec.iov_len = 0;
1344 }
1345 left = middle_len - m->middle->vec.iov_len;
1346 ret = ceph_tcp_recvmsg(con->sock,
1347 (char *)m->middle->vec.iov_base +
1348 m->middle->vec.iov_len, left);
1349 if (ret <= 0)
1350 return ret;
1351 m->middle->vec.iov_len += ret;
1352 if (m->middle->vec.iov_len == middle_len)
1353 con->in_middle_crc = crc32c(0, m->middle->vec.iov_base,
1354 m->middle->vec.iov_len);
1355 }
1356
1357 /* (page) data */
1358 data_off = le16_to_cpu(m->hdr.data_off);
1359 if (data_len == 0)
1360 goto no_data;
1361
1362 if (m->nr_pages == 0) {
1363 con->in_msg_pos.page = 0;
1364 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1365 con->in_msg_pos.data_pos = 0;
1366 /* find pages for data payload */
1367 want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1368 ret = -1;
1369 if (con->ops->prepare_pages)
1370 ret = con->ops->prepare_pages(con, m, want);
1371 if (ret < 0) {
1372 dout("%p prepare_pages failed, skipping payload\n", m);
1373 con->in_base_pos = -data_len - sizeof(m->footer);
1374 ceph_msg_put(con->in_msg);
1375 con->in_msg = NULL;
1376 con->in_tag = CEPH_MSGR_TAG_READY;
1377 return 0;
1378 }
1379 BUG_ON(m->nr_pages < want);
1380 }
1381 while (con->in_msg_pos.data_pos < data_len) {
1382 left = min((int)(data_len - con->in_msg_pos.data_pos),
1383 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1384 BUG_ON(m->pages == NULL);
1385 p = kmap(m->pages[con->in_msg_pos.page]);
1386 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1387 left);
1388 if (ret > 0 && datacrc)
1389 con->in_data_crc =
1390 crc32c(con->in_data_crc,
1391 p + con->in_msg_pos.page_pos, ret);
1392 kunmap(m->pages[con->in_msg_pos.page]);
1393 if (ret <= 0)
1394 return ret;
1395 con->in_msg_pos.data_pos += ret;
1396 con->in_msg_pos.page_pos += ret;
1397 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1398 con->in_msg_pos.page_pos = 0;
1399 con->in_msg_pos.page++;
1400 }
1401 }
1402
1403no_data:
1404 /* footer */
1405 to = sizeof(m->hdr) + sizeof(m->footer);
1406 while (con->in_base_pos < to) {
1407 left = to - con->in_base_pos;
1408 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1409 (con->in_base_pos - sizeof(m->hdr)),
1410 left);
1411 if (ret <= 0)
1412 return ret;
1413 con->in_base_pos += ret;
1414 }
1415 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1416 m, front_len, m->footer.front_crc, middle_len,
1417 m->footer.middle_crc, data_len, m->footer.data_crc);
1418
1419 /* crc ok? */
1420 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1421 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1422 m, con->in_front_crc, m->footer.front_crc);
1423 return -EBADMSG;
1424 }
1425 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1426 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1427 m, con->in_middle_crc, m->footer.middle_crc);
1428 return -EBADMSG;
1429 }
1430 if (datacrc &&
1431 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1432 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1433 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1434 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1435 return -EBADMSG;
1436 }
1437
1438 return 1; /* done! */
1439}
1440
1441/*
1442 * Process message. This happens in the worker thread. The callback should
1443 * be careful not to do anything that waits on other incoming messages or it
1444 * may deadlock.
1445 */
1446static void process_message(struct ceph_connection *con)
1447{
Sage Weil5e095e82009-12-14 14:30:34 -08001448 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001449
Sage Weil5e095e82009-12-14 14:30:34 -08001450 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001451 con->in_msg = NULL;
1452
1453 /* if first message, set peer_name */
1454 if (con->peer_name.type == 0)
1455 con->peer_name = msg->hdr.src.name;
1456
1457 mutex_lock(&con->out_mutex);
1458 con->in_seq++;
1459 mutex_unlock(&con->out_mutex);
1460
1461 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1462 msg, le64_to_cpu(msg->hdr.seq),
1463 ENTITY_NAME(msg->hdr.src.name),
1464 le16_to_cpu(msg->hdr.type),
1465 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1466 le32_to_cpu(msg->hdr.front_len),
1467 le32_to_cpu(msg->hdr.data_len),
1468 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1469 con->ops->dispatch(con, msg);
1470 prepare_read_tag(con);
1471}
1472
1473
1474/*
1475 * Write something to the socket. Called in a worker thread when the
1476 * socket appears to be writeable and we have something ready to send.
1477 */
1478static int try_write(struct ceph_connection *con)
1479{
1480 struct ceph_messenger *msgr = con->msgr;
1481 int ret = 1;
1482
1483 dout("try_write start %p state %lu nref %d\n", con, con->state,
1484 atomic_read(&con->nref));
1485
1486 mutex_lock(&con->out_mutex);
1487more:
1488 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1489
1490 /* open the socket first? */
1491 if (con->sock == NULL) {
1492 /*
1493 * if we were STANDBY and are reconnecting _this_
1494 * connection, bump connect_seq now. Always bump
1495 * global_seq.
1496 */
1497 if (test_and_clear_bit(STANDBY, &con->state))
1498 con->connect_seq++;
1499
Sage Weileed0ef22009-11-10 14:34:36 -08001500 prepare_write_banner(msgr, con);
1501 prepare_write_connect(msgr, con, 1);
1502 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001503 set_bit(CONNECTING, &con->state);
Sage Weileed0ef22009-11-10 14:34:36 -08001504 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001505
Sage Weilcf3e5c42009-12-11 09:48:05 -08001506 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001507 con->in_tag = CEPH_MSGR_TAG_READY;
1508 dout("try_write initiating connect on %p new state %lu\n",
1509 con, con->state);
1510 con->sock = ceph_tcp_connect(con);
1511 if (IS_ERR(con->sock)) {
1512 con->sock = NULL;
1513 con->error_msg = "connect error";
1514 ret = -1;
1515 goto out;
1516 }
1517 }
1518
1519more_kvec:
1520 /* kvec data queued? */
1521 if (con->out_skip) {
1522 ret = write_partial_skip(con);
1523 if (ret <= 0)
1524 goto done;
1525 if (ret < 0) {
1526 dout("try_write write_partial_skip err %d\n", ret);
1527 goto done;
1528 }
1529 }
1530 if (con->out_kvec_left) {
1531 ret = write_partial_kvec(con);
1532 if (ret <= 0)
1533 goto done;
1534 if (ret < 0) {
1535 dout("try_write write_partial_kvec err %d\n", ret);
1536 goto done;
1537 }
1538 }
1539
1540 /* msg pages? */
1541 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001542 if (con->out_msg_done) {
1543 ceph_msg_put(con->out_msg);
1544 con->out_msg = NULL; /* we're done with this one */
1545 goto do_next;
1546 }
1547
Sage Weil31b80062009-10-06 11:31:13 -07001548 ret = write_partial_msg_pages(con);
1549 if (ret == 1)
1550 goto more_kvec; /* we need to send the footer, too! */
1551 if (ret == 0)
1552 goto done;
1553 if (ret < 0) {
1554 dout("try_write write_partial_msg_pages err %d\n",
1555 ret);
1556 goto done;
1557 }
1558 }
1559
Sage Weilc86a2932009-12-14 14:04:30 -08001560do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001561 if (!test_bit(CONNECTING, &con->state)) {
1562 /* is anything else pending? */
1563 if (!list_empty(&con->out_queue)) {
1564 prepare_write_message(con);
1565 goto more;
1566 }
1567 if (con->in_seq > con->in_seq_acked) {
1568 prepare_write_ack(con);
1569 goto more;
1570 }
1571 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1572 prepare_write_keepalive(con);
1573 goto more;
1574 }
1575 }
1576
1577 /* Nothing to do! */
1578 clear_bit(WRITE_PENDING, &con->state);
1579 dout("try_write nothing else to write.\n");
1580done:
1581 ret = 0;
1582out:
1583 mutex_unlock(&con->out_mutex);
1584 dout("try_write done on %p\n", con);
1585 return ret;
1586}
1587
1588
1589
1590/*
1591 * Read what we can from the socket.
1592 */
1593static int try_read(struct ceph_connection *con)
1594{
1595 struct ceph_messenger *msgr;
1596 int ret = -1;
1597
1598 if (!con->sock)
1599 return 0;
1600
1601 if (test_bit(STANDBY, &con->state))
1602 return 0;
1603
1604 dout("try_read start on %p\n", con);
1605 msgr = con->msgr;
1606
1607more:
1608 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1609 con->in_base_pos);
1610 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08001611 if (!test_bit(NEGOTIATING, &con->state)) {
1612 dout("try_read connecting\n");
1613 ret = read_partial_banner(con);
1614 if (ret <= 0)
1615 goto done;
1616 if (process_banner(con) < 0) {
1617 ret = -1;
1618 goto out;
1619 }
1620 }
Sage Weil31b80062009-10-06 11:31:13 -07001621 ret = read_partial_connect(con);
1622 if (ret <= 0)
1623 goto done;
1624 if (process_connect(con) < 0) {
1625 ret = -1;
1626 goto out;
1627 }
1628 goto more;
1629 }
1630
1631 if (con->in_base_pos < 0) {
1632 /*
1633 * skipping + discarding content.
1634 *
1635 * FIXME: there must be a better way to do this!
1636 */
1637 static char buf[1024];
1638 int skip = min(1024, -con->in_base_pos);
1639 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1640 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1641 if (ret <= 0)
1642 goto done;
1643 con->in_base_pos += ret;
1644 if (con->in_base_pos)
1645 goto more;
1646 }
1647 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1648 /*
1649 * what's next?
1650 */
1651 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1652 if (ret <= 0)
1653 goto done;
1654 dout("try_read got tag %d\n", (int)con->in_tag);
1655 switch (con->in_tag) {
1656 case CEPH_MSGR_TAG_MSG:
1657 prepare_read_message(con);
1658 break;
1659 case CEPH_MSGR_TAG_ACK:
1660 prepare_read_ack(con);
1661 break;
1662 case CEPH_MSGR_TAG_CLOSE:
1663 set_bit(CLOSED, &con->state); /* fixme */
1664 goto done;
1665 default:
1666 goto bad_tag;
1667 }
1668 }
1669 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1670 ret = read_partial_message(con);
1671 if (ret <= 0) {
1672 switch (ret) {
1673 case -EBADMSG:
1674 con->error_msg = "bad crc";
1675 ret = -EIO;
1676 goto out;
1677 case -EIO:
1678 con->error_msg = "io error";
1679 goto out;
1680 default:
1681 goto done;
1682 }
1683 }
1684 if (con->in_tag == CEPH_MSGR_TAG_READY)
1685 goto more;
1686 process_message(con);
1687 goto more;
1688 }
1689 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1690 ret = read_partial_ack(con);
1691 if (ret <= 0)
1692 goto done;
1693 process_ack(con);
1694 goto more;
1695 }
1696
1697done:
1698 ret = 0;
1699out:
1700 dout("try_read done on %p\n", con);
1701 return ret;
1702
1703bad_tag:
1704 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1705 con->error_msg = "protocol error, garbage tag";
1706 ret = -1;
1707 goto out;
1708}
1709
1710
1711/*
1712 * Atomically queue work on a connection. Bump @con reference to
1713 * avoid races with connection teardown.
1714 *
1715 * There is some trickery going on with QUEUED and BUSY because we
1716 * only want a _single_ thread operating on each connection at any
1717 * point in time, but we want to use all available CPUs.
1718 *
1719 * The worker thread only proceeds if it can atomically set BUSY. It
1720 * clears QUEUED and does it's thing. When it thinks it's done, it
1721 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1722 * (tries again to set BUSY).
1723 *
1724 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1725 * try to queue work. If that fails (work is already queued, or BUSY)
1726 * we give up (work also already being done or is queued) but leave QUEUED
1727 * set so that the worker thread will loop if necessary.
1728 */
1729static void queue_con(struct ceph_connection *con)
1730{
1731 if (test_bit(DEAD, &con->state)) {
1732 dout("queue_con %p ignoring: DEAD\n",
1733 con);
1734 return;
1735 }
1736
1737 if (!con->ops->get(con)) {
1738 dout("queue_con %p ref count 0\n", con);
1739 return;
1740 }
1741
1742 set_bit(QUEUED, &con->state);
1743 if (test_bit(BUSY, &con->state)) {
1744 dout("queue_con %p - already BUSY\n", con);
1745 con->ops->put(con);
1746 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1747 dout("queue_con %p - already queued\n", con);
1748 con->ops->put(con);
1749 } else {
1750 dout("queue_con %p\n", con);
1751 }
1752}
1753
1754/*
1755 * Do some work on a connection. Drop a connection ref when we're done.
1756 */
1757static void con_work(struct work_struct *work)
1758{
1759 struct ceph_connection *con = container_of(work, struct ceph_connection,
1760 work.work);
1761 int backoff = 0;
1762
1763more:
1764 if (test_and_set_bit(BUSY, &con->state) != 0) {
1765 dout("con_work %p BUSY already set\n", con);
1766 goto out;
1767 }
1768 dout("con_work %p start, clearing QUEUED\n", con);
1769 clear_bit(QUEUED, &con->state);
1770
1771 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1772 dout("con_work CLOSED\n");
1773 con_close_socket(con);
1774 goto done;
1775 }
1776 if (test_and_clear_bit(OPENING, &con->state)) {
1777 /* reopen w/ new peer */
1778 dout("con_work OPENING\n");
1779 con_close_socket(con);
1780 }
1781
1782 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1783 try_read(con) < 0 ||
1784 try_write(con) < 0) {
1785 backoff = 1;
1786 ceph_fault(con); /* error/fault path */
1787 }
1788
1789done:
1790 clear_bit(BUSY, &con->state);
1791 dout("con->state=%lu\n", con->state);
1792 if (test_bit(QUEUED, &con->state)) {
1793 if (!backoff) {
1794 dout("con_work %p QUEUED reset, looping\n", con);
1795 goto more;
1796 }
1797 dout("con_work %p QUEUED reset, but just faulted\n", con);
1798 clear_bit(QUEUED, &con->state);
1799 }
1800 dout("con_work %p done\n", con);
1801
1802out:
1803 con->ops->put(con);
1804}
1805
1806
1807/*
1808 * Generic error/fault handler. A retry mechanism is used with
1809 * exponential backoff
1810 */
1811static void ceph_fault(struct ceph_connection *con)
1812{
1813 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1814 pr_addr(&con->peer_addr.in_addr), con->error_msg);
1815 dout("fault %p state %lu to peer %s\n",
1816 con, con->state, pr_addr(&con->peer_addr.in_addr));
1817
1818 if (test_bit(LOSSYTX, &con->state)) {
1819 dout("fault on LOSSYTX channel\n");
1820 goto out;
1821 }
1822
1823 clear_bit(BUSY, &con->state); /* to avoid an improbable race */
1824
1825 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08001826
1827 if (con->in_msg) {
1828 ceph_msg_put(con->in_msg);
1829 con->in_msg = NULL;
1830 }
Sage Weil31b80062009-10-06 11:31:13 -07001831
1832 /* If there are no messages in the queue, place the connection
1833 * in a STANDBY state (i.e., don't try to reconnect just yet). */
1834 mutex_lock(&con->out_mutex);
1835 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1836 dout("fault setting STANDBY\n");
1837 set_bit(STANDBY, &con->state);
1838 mutex_unlock(&con->out_mutex);
1839 goto out;
1840 }
1841
1842 /* Requeue anything that hasn't been acked, and retry after a
1843 * delay. */
1844 list_splice_init(&con->out_sent, &con->out_queue);
1845 mutex_unlock(&con->out_mutex);
1846
1847 if (con->delay == 0)
1848 con->delay = BASE_DELAY_INTERVAL;
1849 else if (con->delay < MAX_DELAY_INTERVAL)
1850 con->delay *= 2;
1851
1852 /* explicitly schedule work to try to reconnect again later. */
1853 dout("fault queueing %p delay %lu\n", con, con->delay);
1854 con->ops->get(con);
1855 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1856 round_jiffies_relative(con->delay)) == 0)
1857 con->ops->put(con);
1858
1859out:
1860 if (con->ops->fault)
1861 con->ops->fault(con);
1862}
1863
1864
1865
1866/*
1867 * create a new messenger instance
1868 */
1869struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1870{
1871 struct ceph_messenger *msgr;
1872
1873 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1874 if (msgr == NULL)
1875 return ERR_PTR(-ENOMEM);
1876
1877 spin_lock_init(&msgr->global_seq_lock);
1878
1879 /* the zero page is needed if a request is "canceled" while the message
1880 * is being written over the socket */
1881 msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1882 if (!msgr->zero_page) {
1883 kfree(msgr);
1884 return ERR_PTR(-ENOMEM);
1885 }
1886 kmap(msgr->zero_page);
1887
1888 if (myaddr)
1889 msgr->inst.addr = *myaddr;
1890
1891 /* select a random nonce */
1892 get_random_bytes(&msgr->inst.addr.nonce,
1893 sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08001894 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07001895
1896 dout("messenger_create %p\n", msgr);
1897 return msgr;
1898}
1899
1900void ceph_messenger_destroy(struct ceph_messenger *msgr)
1901{
1902 dout("destroy %p\n", msgr);
1903 kunmap(msgr->zero_page);
1904 __free_page(msgr->zero_page);
1905 kfree(msgr);
1906 dout("destroyed messenger %p\n", msgr);
1907}
1908
1909/*
1910 * Queue up an outgoing message on the given connection.
1911 */
1912void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1913{
1914 if (test_bit(CLOSED, &con->state)) {
1915 dout("con_send %p closed, dropping %p\n", con, msg);
1916 ceph_msg_put(msg);
1917 return;
1918 }
1919
1920 /* set src+dst */
Sage Weil63f2d212009-11-03 15:17:56 -08001921 msg->hdr.src.name = con->msgr->inst.name;
1922 msg->hdr.src.addr = con->msgr->my_enc_addr;
1923 msg->hdr.orig_src = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001924 msg->hdr.dst_erank = con->peer_addr.erank;
1925
1926 /* queue */
1927 mutex_lock(&con->out_mutex);
1928 BUG_ON(!list_empty(&msg->list_head));
1929 list_add_tail(&msg->list_head, &con->out_queue);
1930 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
1931 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
1932 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1933 le32_to_cpu(msg->hdr.front_len),
1934 le32_to_cpu(msg->hdr.middle_len),
1935 le32_to_cpu(msg->hdr.data_len));
1936 mutex_unlock(&con->out_mutex);
1937
1938 /* if there wasn't anything waiting to send before, queue
1939 * new work */
1940 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1941 queue_con(con);
1942}
1943
1944/*
1945 * Revoke a message that was previously queued for send
1946 */
1947void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
1948{
1949 mutex_lock(&con->out_mutex);
1950 if (!list_empty(&msg->list_head)) {
1951 dout("con_revoke %p msg %p\n", con, msg);
1952 list_del_init(&msg->list_head);
1953 ceph_msg_put(msg);
1954 msg->hdr.seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -08001955 if (con->out_msg == msg) {
1956 ceph_msg_put(con->out_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001957 con->out_msg = NULL;
Sage Weilc86a2932009-12-14 14:04:30 -08001958 }
Sage Weil31b80062009-10-06 11:31:13 -07001959 if (con->out_kvec_is_msg) {
1960 con->out_skip = con->out_kvec_bytes;
1961 con->out_kvec_is_msg = false;
1962 }
1963 } else {
1964 dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
1965 }
1966 mutex_unlock(&con->out_mutex);
1967}
1968
1969/*
1970 * Queue a keepalive byte to ensure the tcp connection is alive.
1971 */
1972void ceph_con_keepalive(struct ceph_connection *con)
1973{
1974 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
1975 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1976 queue_con(con);
1977}
1978
1979
1980/*
1981 * construct a new message with given type, size
1982 * the new msg has a ref count of 1.
1983 */
1984struct ceph_msg *ceph_msg_new(int type, int front_len,
1985 int page_len, int page_off, struct page **pages)
1986{
1987 struct ceph_msg *m;
1988
1989 m = kmalloc(sizeof(*m), GFP_NOFS);
1990 if (m == NULL)
1991 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08001992 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07001993 INIT_LIST_HEAD(&m->list_head);
1994
1995 m->hdr.type = cpu_to_le16(type);
1996 m->hdr.front_len = cpu_to_le32(front_len);
1997 m->hdr.middle_len = 0;
1998 m->hdr.data_len = cpu_to_le32(page_len);
1999 m->hdr.data_off = cpu_to_le16(page_off);
2000 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2001 m->footer.front_crc = 0;
2002 m->footer.middle_crc = 0;
2003 m->footer.data_crc = 0;
2004 m->front_max = front_len;
2005 m->front_is_vmalloc = false;
2006 m->more_to_follow = false;
2007 m->pool = NULL;
2008
2009 /* front */
2010 if (front_len) {
2011 if (front_len > PAGE_CACHE_SIZE) {
2012 m->front.iov_base = __vmalloc(front_len, GFP_NOFS,
2013 PAGE_KERNEL);
2014 m->front_is_vmalloc = true;
2015 } else {
2016 m->front.iov_base = kmalloc(front_len, GFP_NOFS);
2017 }
2018 if (m->front.iov_base == NULL) {
2019 pr_err("msg_new can't allocate %d bytes\n",
2020 front_len);
2021 goto out2;
2022 }
2023 } else {
2024 m->front.iov_base = NULL;
2025 }
2026 m->front.iov_len = front_len;
2027
2028 /* middle */
2029 m->middle = NULL;
2030
2031 /* data */
2032 m->nr_pages = calc_pages_for(page_off, page_len);
2033 m->pages = pages;
2034
2035 dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len,
2036 m->nr_pages);
2037 return m;
2038
2039out2:
2040 ceph_msg_put(m);
2041out:
2042 pr_err("msg_new can't create type %d len %d\n", type, front_len);
2043 return ERR_PTR(-ENOMEM);
2044}
2045
2046/*
2047 * Generic message allocator, for incoming messages.
2048 */
2049struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2050 struct ceph_msg_header *hdr)
2051{
2052 int type = le16_to_cpu(hdr->type);
2053 int front_len = le32_to_cpu(hdr->front_len);
2054 struct ceph_msg *msg = ceph_msg_new(type, front_len, 0, 0, NULL);
2055
2056 if (!msg) {
2057 pr_err("unable to allocate msg type %d len %d\n",
2058 type, front_len);
2059 return ERR_PTR(-ENOMEM);
2060 }
2061 return msg;
2062}
2063
2064/*
2065 * Allocate "middle" portion of a message, if it is needed and wasn't
2066 * allocated by alloc_msg. This allows us to read a small fixed-size
2067 * per-type header in the front and then gracefully fail (i.e.,
2068 * propagate the error to the caller based on info in the front) when
2069 * the middle is too large.
2070 */
2071int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
2072{
2073 int type = le16_to_cpu(msg->hdr.type);
2074 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2075
2076 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2077 ceph_msg_type_name(type), middle_len);
2078 BUG_ON(!middle_len);
2079 BUG_ON(msg->middle);
2080
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002081 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002082 if (!msg->middle)
2083 return -ENOMEM;
2084 return 0;
2085}
2086
2087
2088/*
2089 * Free a generically kmalloc'd message.
2090 */
2091void ceph_msg_kfree(struct ceph_msg *m)
2092{
2093 dout("msg_kfree %p\n", m);
2094 if (m->front_is_vmalloc)
2095 vfree(m->front.iov_base);
2096 else
2097 kfree(m->front.iov_base);
2098 kfree(m);
2099}
2100
2101/*
2102 * Drop a msg ref. Destroy as needed.
2103 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002104void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002105{
Sage Weilc2e552e2009-12-07 15:55:05 -08002106 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002107
Sage Weilc2e552e2009-12-07 15:55:05 -08002108 dout("ceph_msg_put last one on %p\n", m);
2109 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002110
Sage Weilc2e552e2009-12-07 15:55:05 -08002111 /* drop middle, data, if any */
2112 if (m->middle) {
2113 ceph_buffer_put(m->middle);
2114 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002115 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002116 m->nr_pages = 0;
2117 m->pages = NULL;
2118
2119 if (m->pool)
2120 ceph_msgpool_put(m->pool, m);
2121 else
2122 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002123}
Sage Weil9ec7cab2009-12-14 15:13:47 -08002124
2125void ceph_msg_dump(struct ceph_msg *msg)
2126{
2127 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2128 msg->front_max, msg->nr_pages);
2129 print_hex_dump(KERN_DEBUG, "header: ",
2130 DUMP_PREFIX_OFFSET, 16, 1,
2131 &msg->hdr, sizeof(msg->hdr), true);
2132 print_hex_dump(KERN_DEBUG, " front: ",
2133 DUMP_PREFIX_OFFSET, 16, 1,
2134 msg->front.iov_base, msg->front.iov_len, true);
2135 if (msg->middle)
2136 print_hex_dump(KERN_DEBUG, "middle: ",
2137 DUMP_PREFIX_OFFSET, 16, 1,
2138 msg->middle->vec.iov_base,
2139 msg->middle->vec.iov_len, true);
2140 print_hex_dump(KERN_DEBUG, "footer: ",
2141 DUMP_PREFIX_OFFSET, 16, 1,
2142 &msg->footer, sizeof(msg->footer), true);
2143}