blob: 98519bd33f04f751dd36a8af6f37f21a8836a398 [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
323 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 Weil31b80062009-10-06 11:31:13 -0700329 con->in_seq = 0;
330 mutex_unlock(&con->out_mutex);
331}
332
333/*
334 * mark a peer down. drop any open connections.
335 */
336void ceph_con_close(struct ceph_connection *con)
337{
338 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
339 set_bit(CLOSED, &con->state); /* in case there's queued work */
340 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
341 reset_connection(con);
342 queue_con(con);
343}
344
345/*
Sage Weil31b80062009-10-06 11:31:13 -0700346 * Reopen a closed connection, with a new peer address.
347 */
348void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
349{
350 dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
351 set_bit(OPENING, &con->state);
352 clear_bit(CLOSED, &con->state);
353 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800354 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700355 queue_con(con);
356}
357
358/*
359 * generic get/put
360 */
361struct ceph_connection *ceph_con_get(struct ceph_connection *con)
362{
363 dout("con_get %p nref = %d -> %d\n", con,
364 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
365 if (atomic_inc_not_zero(&con->nref))
366 return con;
367 return NULL;
368}
369
370void ceph_con_put(struct ceph_connection *con)
371{
372 dout("con_put %p nref = %d -> %d\n", con,
373 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
374 BUG_ON(atomic_read(&con->nref) == 0);
375 if (atomic_dec_and_test(&con->nref)) {
Sage Weil71ececd2009-11-18 11:27:06 -0800376 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700377 kfree(con);
378 }
379}
380
381/*
382 * initialize a new connection.
383 */
384void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
385{
386 dout("con_init %p\n", con);
387 memset(con, 0, sizeof(*con));
388 atomic_set(&con->nref, 1);
389 con->msgr = msgr;
390 mutex_init(&con->out_mutex);
391 INIT_LIST_HEAD(&con->out_queue);
392 INIT_LIST_HEAD(&con->out_sent);
393 INIT_DELAYED_WORK(&con->work, con_work);
394}
395
396
397/*
398 * We maintain a global counter to order connection attempts. Get
399 * a unique seq greater than @gt.
400 */
401static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
402{
403 u32 ret;
404
405 spin_lock(&msgr->global_seq_lock);
406 if (msgr->global_seq < gt)
407 msgr->global_seq = gt;
408 ret = ++msgr->global_seq;
409 spin_unlock(&msgr->global_seq_lock);
410 return ret;
411}
412
413
414/*
415 * Prepare footer for currently outgoing message, and finish things
416 * off. Assumes out_kvec* are already valid.. we just add on to the end.
417 */
418static void prepare_write_message_footer(struct ceph_connection *con, int v)
419{
420 struct ceph_msg *m = con->out_msg;
421
422 dout("prepare_write_message_footer %p\n", con);
423 con->out_kvec_is_msg = true;
424 con->out_kvec[v].iov_base = &m->footer;
425 con->out_kvec[v].iov_len = sizeof(m->footer);
426 con->out_kvec_bytes += sizeof(m->footer);
427 con->out_kvec_left++;
428 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800429 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700430}
431
432/*
433 * Prepare headers for the next outgoing message.
434 */
435static void prepare_write_message(struct ceph_connection *con)
436{
437 struct ceph_msg *m;
438 int v = 0;
439
440 con->out_kvec_bytes = 0;
441 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800442 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700443
444 /* Sneak an ack in there first? If we can get it into the same
445 * TCP packet that's a good thing. */
446 if (con->in_seq > con->in_seq_acked) {
447 con->in_seq_acked = con->in_seq;
448 con->out_kvec[v].iov_base = &tag_ack;
449 con->out_kvec[v++].iov_len = 1;
450 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
451 con->out_kvec[v].iov_base = &con->out_temp_ack;
452 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
453 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
454 }
455
456 /* move message to sending/sent list */
457 m = list_first_entry(&con->out_queue,
458 struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800459 con->out_msg = m;
460 ceph_msg_get(m);
Sage Weil31b80062009-10-06 11:31:13 -0700461 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700462
463 m->hdr.seq = cpu_to_le64(++con->out_seq);
464
465 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
466 m, con->out_seq, le16_to_cpu(m->hdr.type),
467 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
468 le32_to_cpu(m->hdr.data_len),
469 m->nr_pages);
470 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
471
472 /* tag + hdr + front + middle */
473 con->out_kvec[v].iov_base = &tag_msg;
474 con->out_kvec[v++].iov_len = 1;
475 con->out_kvec[v].iov_base = &m->hdr;
476 con->out_kvec[v++].iov_len = sizeof(m->hdr);
477 con->out_kvec[v++] = m->front;
478 if (m->middle)
479 con->out_kvec[v++] = m->middle->vec;
480 con->out_kvec_left = v;
481 con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
482 (m->middle ? m->middle->vec.iov_len : 0);
483 con->out_kvec_cur = con->out_kvec;
484
485 /* fill in crc (except data pages), footer */
486 con->out_msg->hdr.crc =
487 cpu_to_le32(crc32c(0, (void *)&m->hdr,
488 sizeof(m->hdr) - sizeof(m->hdr.crc)));
489 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
490 con->out_msg->footer.front_crc =
491 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
492 if (m->middle)
493 con->out_msg->footer.middle_crc =
494 cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
495 m->middle->vec.iov_len));
496 else
497 con->out_msg->footer.middle_crc = 0;
498 con->out_msg->footer.data_crc = 0;
499 dout("prepare_write_message front_crc %u data_crc %u\n",
500 le32_to_cpu(con->out_msg->footer.front_crc),
501 le32_to_cpu(con->out_msg->footer.middle_crc));
502
503 /* is there a data payload? */
504 if (le32_to_cpu(m->hdr.data_len) > 0) {
505 /* initialize page iterator */
506 con->out_msg_pos.page = 0;
507 con->out_msg_pos.page_pos =
508 le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
509 con->out_msg_pos.data_pos = 0;
510 con->out_msg_pos.did_page_crc = 0;
511 con->out_more = 1; /* data + footer will follow */
512 } else {
513 /* no, queue up footer too and be done */
514 prepare_write_message_footer(con, v);
515 }
516
517 set_bit(WRITE_PENDING, &con->state);
518}
519
520/*
521 * Prepare an ack.
522 */
523static void prepare_write_ack(struct ceph_connection *con)
524{
525 dout("prepare_write_ack %p %llu -> %llu\n", con,
526 con->in_seq_acked, con->in_seq);
527 con->in_seq_acked = con->in_seq;
528
529 con->out_kvec[0].iov_base = &tag_ack;
530 con->out_kvec[0].iov_len = 1;
531 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
532 con->out_kvec[1].iov_base = &con->out_temp_ack;
533 con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
534 con->out_kvec_left = 2;
535 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
536 con->out_kvec_cur = con->out_kvec;
537 con->out_more = 1; /* more will follow.. eventually.. */
538 set_bit(WRITE_PENDING, &con->state);
539}
540
541/*
542 * Prepare to write keepalive byte.
543 */
544static void prepare_write_keepalive(struct ceph_connection *con)
545{
546 dout("prepare_write_keepalive %p\n", con);
547 con->out_kvec[0].iov_base = &tag_keepalive;
548 con->out_kvec[0].iov_len = 1;
549 con->out_kvec_left = 1;
550 con->out_kvec_bytes = 1;
551 con->out_kvec_cur = con->out_kvec;
552 set_bit(WRITE_PENDING, &con->state);
553}
554
555/*
556 * Connection negotiation.
557 */
558
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800559static void prepare_connect_authorizer(struct ceph_connection *con)
560{
561 void *auth_buf;
562 int auth_len = 0;
563 int auth_protocol = 0;
564
565 if (con->ops->get_authorizer)
566 con->ops->get_authorizer(con, &auth_buf, &auth_len,
567 &auth_protocol, &con->auth_reply_buf,
568 &con->auth_reply_buf_len,
569 con->auth_retry);
570
571 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
572 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
573
574 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
575 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
576 con->out_kvec_left++;
577 con->out_kvec_bytes += auth_len;
578}
579
Sage Weil31b80062009-10-06 11:31:13 -0700580/*
581 * We connected to a peer and are saying hello.
582 */
Sage Weileed0ef22009-11-10 14:34:36 -0800583static void prepare_write_banner(struct ceph_messenger *msgr,
584 struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700585{
586 int len = strlen(CEPH_BANNER);
Sage Weileed0ef22009-11-10 14:34:36 -0800587
588 con->out_kvec[0].iov_base = CEPH_BANNER;
589 con->out_kvec[0].iov_len = len;
590 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
591 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
592 con->out_kvec_left = 2;
593 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
594 con->out_kvec_cur = con->out_kvec;
595 con->out_more = 0;
596 set_bit(WRITE_PENDING, &con->state);
597}
598
599static void prepare_write_connect(struct ceph_messenger *msgr,
600 struct ceph_connection *con,
601 int after_banner)
602{
Sage Weil31b80062009-10-06 11:31:13 -0700603 unsigned global_seq = get_global_seq(con->msgr, 0);
604 int proto;
605
606 switch (con->peer_name.type) {
607 case CEPH_ENTITY_TYPE_MON:
608 proto = CEPH_MONC_PROTOCOL;
609 break;
610 case CEPH_ENTITY_TYPE_OSD:
611 proto = CEPH_OSDC_PROTOCOL;
612 break;
613 case CEPH_ENTITY_TYPE_MDS:
614 proto = CEPH_MDSC_PROTOCOL;
615 break;
616 default:
617 BUG();
618 }
619
620 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
621 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800622
Sage Weil31b80062009-10-06 11:31:13 -0700623 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
624 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
625 con->out_connect.global_seq = cpu_to_le32(global_seq);
626 con->out_connect.protocol_version = cpu_to_le32(proto);
627 con->out_connect.flags = 0;
628 if (test_bit(LOSSYTX, &con->state))
629 con->out_connect.flags = CEPH_MSG_CONNECT_LOSSY;
630
Sage Weileed0ef22009-11-10 14:34:36 -0800631 if (!after_banner) {
632 con->out_kvec_left = 0;
633 con->out_kvec_bytes = 0;
634 }
635 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
636 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
637 con->out_kvec_left++;
638 con->out_kvec_bytes += sizeof(con->out_connect);
Sage Weil31b80062009-10-06 11:31:13 -0700639 con->out_kvec_cur = con->out_kvec;
640 con->out_more = 0;
641 set_bit(WRITE_PENDING, &con->state);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800642
643 prepare_connect_authorizer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700644}
645
646
647/*
648 * write as much of pending kvecs to the socket as we can.
649 * 1 -> done
650 * 0 -> socket full, but more to do
651 * <0 -> error
652 */
653static int write_partial_kvec(struct ceph_connection *con)
654{
655 int ret;
656
657 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
658 while (con->out_kvec_bytes > 0) {
659 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
660 con->out_kvec_left, con->out_kvec_bytes,
661 con->out_more);
662 if (ret <= 0)
663 goto out;
664 con->out_kvec_bytes -= ret;
665 if (con->out_kvec_bytes == 0)
666 break; /* done */
667 while (ret > 0) {
668 if (ret >= con->out_kvec_cur->iov_len) {
669 ret -= con->out_kvec_cur->iov_len;
670 con->out_kvec_cur++;
671 con->out_kvec_left--;
672 } else {
673 con->out_kvec_cur->iov_len -= ret;
674 con->out_kvec_cur->iov_base += ret;
675 ret = 0;
676 break;
677 }
678 }
679 }
680 con->out_kvec_left = 0;
681 con->out_kvec_is_msg = false;
682 ret = 1;
683out:
684 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
685 con->out_kvec_bytes, con->out_kvec_left, ret);
686 return ret; /* done! */
687}
688
689/*
690 * Write as much message data payload as we can. If we finish, queue
691 * up the footer.
692 * 1 -> done, footer is now queued in out_kvec[].
693 * 0 -> socket full, but more to do
694 * <0 -> error
695 */
696static int write_partial_msg_pages(struct ceph_connection *con)
697{
698 struct ceph_msg *msg = con->out_msg;
699 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
700 size_t len;
701 int crc = con->msgr->nocrc;
702 int ret;
703
704 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
705 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
706 con->out_msg_pos.page_pos);
707
708 while (con->out_msg_pos.page < con->out_msg->nr_pages) {
709 struct page *page = NULL;
710 void *kaddr = NULL;
711
712 /*
713 * if we are calculating the data crc (the default), we need
714 * to map the page. if our pages[] has been revoked, use the
715 * zero page.
716 */
717 if (msg->pages) {
718 page = msg->pages[con->out_msg_pos.page];
719 if (crc)
720 kaddr = kmap(page);
721 } else {
722 page = con->msgr->zero_page;
723 if (crc)
724 kaddr = page_address(con->msgr->zero_page);
725 }
726 len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
727 (int)(data_len - con->out_msg_pos.data_pos));
728 if (crc && !con->out_msg_pos.did_page_crc) {
729 void *base = kaddr + con->out_msg_pos.page_pos;
730 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
731
732 BUG_ON(kaddr == NULL);
733 con->out_msg->footer.data_crc =
734 cpu_to_le32(crc32c(tmpcrc, base, len));
735 con->out_msg_pos.did_page_crc = 1;
736 }
737
738 ret = kernel_sendpage(con->sock, page,
739 con->out_msg_pos.page_pos, len,
740 MSG_DONTWAIT | MSG_NOSIGNAL |
741 MSG_MORE);
742
743 if (crc && msg->pages)
744 kunmap(page);
745
746 if (ret <= 0)
747 goto out;
748
749 con->out_msg_pos.data_pos += ret;
750 con->out_msg_pos.page_pos += ret;
751 if (ret == len) {
752 con->out_msg_pos.page_pos = 0;
753 con->out_msg_pos.page++;
754 con->out_msg_pos.did_page_crc = 0;
755 }
756 }
757
758 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
759
760 /* prepare and queue up footer, too */
761 if (!crc)
762 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
763 con->out_kvec_bytes = 0;
764 con->out_kvec_left = 0;
765 con->out_kvec_cur = con->out_kvec;
766 prepare_write_message_footer(con, 0);
767 ret = 1;
768out:
769 return ret;
770}
771
772/*
773 * write some zeros
774 */
775static int write_partial_skip(struct ceph_connection *con)
776{
777 int ret;
778
779 while (con->out_skip > 0) {
780 struct kvec iov = {
781 .iov_base = page_address(con->msgr->zero_page),
782 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
783 };
784
785 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
786 if (ret <= 0)
787 goto out;
788 con->out_skip -= ret;
789 }
790 ret = 1;
791out:
792 return ret;
793}
794
795/*
796 * Prepare to read connection handshake, or an ack.
797 */
Sage Weileed0ef22009-11-10 14:34:36 -0800798static void prepare_read_banner(struct ceph_connection *con)
799{
800 dout("prepare_read_banner %p\n", con);
801 con->in_base_pos = 0;
802}
803
Sage Weil31b80062009-10-06 11:31:13 -0700804static void prepare_read_connect(struct ceph_connection *con)
805{
806 dout("prepare_read_connect %p\n", con);
807 con->in_base_pos = 0;
808}
809
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800810static void prepare_read_connect_retry(struct ceph_connection *con)
811{
812 dout("prepare_read_connect_retry %p\n", con);
813 con->in_base_pos = strlen(CEPH_BANNER) + sizeof(con->actual_peer_addr)
814 + sizeof(con->peer_addr_for_me);
815}
816
Sage Weil31b80062009-10-06 11:31:13 -0700817static void prepare_read_ack(struct ceph_connection *con)
818{
819 dout("prepare_read_ack %p\n", con);
820 con->in_base_pos = 0;
821}
822
823static void prepare_read_tag(struct ceph_connection *con)
824{
825 dout("prepare_read_tag %p\n", con);
826 con->in_base_pos = 0;
827 con->in_tag = CEPH_MSGR_TAG_READY;
828}
829
830/*
831 * Prepare to read a message.
832 */
833static int prepare_read_message(struct ceph_connection *con)
834{
835 dout("prepare_read_message %p\n", con);
836 BUG_ON(con->in_msg != NULL);
837 con->in_base_pos = 0;
838 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
839 return 0;
840}
841
842
843static int read_partial(struct ceph_connection *con,
844 int *to, int size, void *object)
845{
846 *to += size;
847 while (con->in_base_pos < *to) {
848 int left = *to - con->in_base_pos;
849 int have = size - left;
850 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
851 if (ret <= 0)
852 return ret;
853 con->in_base_pos += ret;
854 }
855 return 1;
856}
857
858
859/*
860 * Read all or part of the connect-side handshake on a new connection
861 */
Sage Weileed0ef22009-11-10 14:34:36 -0800862static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700863{
864 int ret, to = 0;
865
Sage Weileed0ef22009-11-10 14:34:36 -0800866 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -0700867
868 /* peer's banner */
869 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
870 if (ret <= 0)
871 goto out;
872 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
873 &con->actual_peer_addr);
874 if (ret <= 0)
875 goto out;
876 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
877 &con->peer_addr_for_me);
878 if (ret <= 0)
879 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -0800880out:
881 return ret;
882}
883
884static int read_partial_connect(struct ceph_connection *con)
885{
886 int ret, to = 0;
887
888 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
889
Sage Weil31b80062009-10-06 11:31:13 -0700890 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
891 if (ret <= 0)
892 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800893 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
894 con->auth_reply_buf);
895 if (ret <= 0)
896 goto out;
Sage Weil31b80062009-10-06 11:31:13 -0700897
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800898 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
899 con, (int)con->in_reply.tag,
900 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -0700901 le32_to_cpu(con->in_reply.global_seq));
902out:
903 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -0800904
Sage Weil31b80062009-10-06 11:31:13 -0700905}
906
907/*
908 * Verify the hello banner looks okay.
909 */
910static int verify_hello(struct ceph_connection *con)
911{
912 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -0700913 pr_err("connect to %s got bad banner\n",
Sage Weil31b80062009-10-06 11:31:13 -0700914 pr_addr(&con->peer_addr.in_addr));
915 con->error_msg = "protocol error, bad banner";
916 return -1;
917 }
918 return 0;
919}
920
921static bool addr_is_blank(struct sockaddr_storage *ss)
922{
923 switch (ss->ss_family) {
924 case AF_INET:
925 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
926 case AF_INET6:
927 return
928 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
929 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
930 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
931 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
932 }
933 return false;
934}
935
936static int addr_port(struct sockaddr_storage *ss)
937{
938 switch (ss->ss_family) {
939 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800940 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -0700941 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800942 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -0700943 }
944 return 0;
945}
946
947static void addr_set_port(struct sockaddr_storage *ss, int p)
948{
949 switch (ss->ss_family) {
950 case AF_INET:
951 ((struct sockaddr_in *)ss)->sin_port = htons(p);
952 case AF_INET6:
953 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
954 }
955}
956
957/*
958 * Parse an ip[:port] list into an addr array. Use the default
959 * monitor port if a port isn't specified.
960 */
961int ceph_parse_ips(const char *c, const char *end,
962 struct ceph_entity_addr *addr,
963 int max_count, int *count)
964{
965 int i;
966 const char *p = c;
967
968 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
969 for (i = 0; i < max_count; i++) {
970 const char *ipend;
971 struct sockaddr_storage *ss = &addr[i].in_addr;
972 struct sockaddr_in *in4 = (void *)ss;
973 struct sockaddr_in6 *in6 = (void *)ss;
974 int port;
975
976 memset(ss, 0, sizeof(*ss));
977 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
978 ',', &ipend)) {
979 ss->ss_family = AF_INET;
980 } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
981 ',', &ipend)) {
982 ss->ss_family = AF_INET6;
983 } else {
984 goto bad;
985 }
986 p = ipend;
987
988 /* port? */
989 if (p < end && *p == ':') {
990 port = 0;
991 p++;
992 while (p < end && *p >= '0' && *p <= '9') {
993 port = (port * 10) + (*p - '0');
994 p++;
995 }
996 if (port > 65535 || port == 0)
997 goto bad;
998 } else {
999 port = CEPH_MON_PORT;
1000 }
1001
1002 addr_set_port(ss, port);
1003
1004 dout("parse_ips got %s\n", pr_addr(ss));
1005
1006 if (p == end)
1007 break;
1008 if (*p != ',')
1009 goto bad;
1010 p++;
1011 }
1012
1013 if (p != end)
1014 goto bad;
1015
1016 if (count)
1017 *count = i + 1;
1018 return 0;
1019
1020bad:
1021 pr_err("parse_ips bad ip '%s'\n", c);
1022 return -EINVAL;
1023}
1024
Sage Weileed0ef22009-11-10 14:34:36 -08001025static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001026{
Sage Weileed0ef22009-11-10 14:34:36 -08001027 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001028
1029 if (verify_hello(con) < 0)
1030 return -1;
1031
Sage Weil63f2d212009-11-03 15:17:56 -08001032 ceph_decode_addr(&con->actual_peer_addr);
1033 ceph_decode_addr(&con->peer_addr_for_me);
1034
Sage Weil31b80062009-10-06 11:31:13 -07001035 /*
1036 * Make sure the other end is who we wanted. note that the other
1037 * end may not yet know their ip address, so if it's 0.0.0.0, give
1038 * them the benefit of the doubt.
1039 */
1040 if (!ceph_entity_addr_is_local(&con->peer_addr,
1041 &con->actual_peer_addr) &&
1042 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1043 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
1044 pr_err("wrong peer, want %s/%d, "
1045 "got %s/%d, wtf\n",
1046 pr_addr(&con->peer_addr.in_addr),
1047 con->peer_addr.nonce,
1048 pr_addr(&con->actual_peer_addr.in_addr),
1049 con->actual_peer_addr.nonce);
1050 con->error_msg = "protocol error, wrong peer";
1051 return -1;
1052 }
1053
1054 /*
1055 * did we learn our address?
1056 */
1057 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1058 int port = addr_port(&con->msgr->inst.addr.in_addr);
1059
1060 memcpy(&con->msgr->inst.addr.in_addr,
1061 &con->peer_addr_for_me.in_addr,
1062 sizeof(con->peer_addr_for_me.in_addr));
1063 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001064 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001065 dout("process_banner learned my addr is %s\n",
Sage Weil31b80062009-10-06 11:31:13 -07001066 pr_addr(&con->msgr->inst.addr.in_addr));
1067 }
1068
Sage Weileed0ef22009-11-10 14:34:36 -08001069 set_bit(NEGOTIATING, &con->state);
1070 prepare_read_connect(con);
1071 return 0;
1072}
1073
1074static int process_connect(struct ceph_connection *con)
1075{
1076 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1077
Sage Weil31b80062009-10-06 11:31:13 -07001078 switch (con->in_reply.tag) {
1079 case CEPH_MSGR_TAG_BADPROTOVER:
1080 dout("process_connect got BADPROTOVER my %d != their %d\n",
1081 le32_to_cpu(con->out_connect.protocol_version),
1082 le32_to_cpu(con->in_reply.protocol_version));
1083 pr_err("%s%lld %s protocol version mismatch,"
1084 " my %d != server's %d\n",
1085 ENTITY_NAME(con->peer_name),
1086 pr_addr(&con->peer_addr.in_addr),
1087 le32_to_cpu(con->out_connect.protocol_version),
1088 le32_to_cpu(con->in_reply.protocol_version));
1089 con->error_msg = "protocol version mismatch";
1090 if (con->ops->bad_proto)
1091 con->ops->bad_proto(con);
1092 reset_connection(con);
1093 set_bit(CLOSED, &con->state); /* in case there's queued work */
1094 return -1;
1095
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001096 case CEPH_MSGR_TAG_BADAUTHORIZER:
1097 con->auth_retry++;
1098 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1099 con->auth_retry);
1100 if (con->auth_retry == 2) {
1101 con->error_msg = "connect authorization failure";
1102 reset_connection(con);
1103 set_bit(CLOSED, &con->state);
1104 return -1;
1105 }
1106 con->auth_retry = 1;
1107 prepare_write_connect(con->msgr, con, 0);
1108 prepare_read_connect_retry(con);
1109 break;
Sage Weil31b80062009-10-06 11:31:13 -07001110
1111 case CEPH_MSGR_TAG_RESETSESSION:
1112 /*
1113 * If we connected with a large connect_seq but the peer
1114 * has no record of a session with us (no connection, or
1115 * connect_seq == 0), they will send RESETSESION to indicate
1116 * that they must have reset their session, and may have
1117 * dropped messages.
1118 */
1119 dout("process_connect got RESET peer seq %u\n",
1120 le32_to_cpu(con->in_connect.connect_seq));
1121 pr_err("%s%lld %s connection reset\n",
1122 ENTITY_NAME(con->peer_name),
1123 pr_addr(&con->peer_addr.in_addr));
1124 reset_connection(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001125 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001126 prepare_read_connect(con);
1127
1128 /* Tell ceph about it. */
1129 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1130 if (con->ops->peer_reset)
1131 con->ops->peer_reset(con);
1132 break;
1133
1134 case CEPH_MSGR_TAG_RETRY_SESSION:
1135 /*
1136 * If we sent a smaller connect_seq than the peer has, try
1137 * again with a larger value.
1138 */
1139 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1140 le32_to_cpu(con->out_connect.connect_seq),
1141 le32_to_cpu(con->in_connect.connect_seq));
1142 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Sage Weileed0ef22009-11-10 14:34:36 -08001143 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001144 prepare_read_connect(con);
1145 break;
1146
1147 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1148 /*
1149 * If we sent a smaller global_seq than the peer has, try
1150 * again with a larger value.
1151 */
Sage Weileed0ef22009-11-10 14:34:36 -08001152 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001153 con->peer_global_seq,
1154 le32_to_cpu(con->in_connect.global_seq));
1155 get_global_seq(con->msgr,
1156 le32_to_cpu(con->in_connect.global_seq));
Sage Weileed0ef22009-11-10 14:34:36 -08001157 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001158 prepare_read_connect(con);
1159 break;
1160
1161 case CEPH_MSGR_TAG_READY:
1162 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001163 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1164 con->connect_seq++;
1165 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1166 con->peer_global_seq,
1167 le32_to_cpu(con->in_reply.connect_seq),
1168 con->connect_seq);
1169 WARN_ON(con->connect_seq !=
1170 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07001171 prepare_read_tag(con);
1172 break;
1173
1174 case CEPH_MSGR_TAG_WAIT:
1175 /*
1176 * If there is a connection race (we are opening
1177 * connections to each other), one of us may just have
1178 * to WAIT. This shouldn't happen if we are the
1179 * client.
1180 */
1181 pr_err("process_connect peer connecting WAIT\n");
1182
1183 default:
1184 pr_err("connect protocol error, will retry\n");
1185 con->error_msg = "protocol error, garbage tag during connect";
1186 return -1;
1187 }
1188 return 0;
1189}
1190
1191
1192/*
1193 * read (part of) an ack
1194 */
1195static int read_partial_ack(struct ceph_connection *con)
1196{
1197 int to = 0;
1198
1199 return read_partial(con, &to, sizeof(con->in_temp_ack),
1200 &con->in_temp_ack);
1201}
1202
1203
1204/*
1205 * We can finally discard anything that's been acked.
1206 */
1207static void process_ack(struct ceph_connection *con)
1208{
1209 struct ceph_msg *m;
1210 u64 ack = le64_to_cpu(con->in_temp_ack);
1211 u64 seq;
1212
1213 mutex_lock(&con->out_mutex);
1214 while (!list_empty(&con->out_sent)) {
1215 m = list_first_entry(&con->out_sent, struct ceph_msg,
1216 list_head);
1217 seq = le64_to_cpu(m->hdr.seq);
1218 if (seq > ack)
1219 break;
1220 dout("got ack for seq %llu type %d at %p\n", seq,
1221 le16_to_cpu(m->hdr.type), m);
1222 ceph_msg_remove(m);
1223 }
1224 mutex_unlock(&con->out_mutex);
1225 prepare_read_tag(con);
1226}
1227
1228
1229
1230
1231
1232
1233/*
1234 * read (part of) a message.
1235 */
1236static int read_partial_message(struct ceph_connection *con)
1237{
1238 struct ceph_msg *m = con->in_msg;
1239 void *p;
1240 int ret;
1241 int to, want, left;
1242 unsigned front_len, middle_len, data_len, data_off;
1243 int datacrc = con->msgr->nocrc;
1244
1245 dout("read_partial_message con %p msg %p\n", con, m);
1246
1247 /* header */
1248 while (con->in_base_pos < sizeof(con->in_hdr)) {
1249 left = sizeof(con->in_hdr) - con->in_base_pos;
1250 ret = ceph_tcp_recvmsg(con->sock,
1251 (char *)&con->in_hdr + con->in_base_pos,
1252 left);
1253 if (ret <= 0)
1254 return ret;
1255 con->in_base_pos += ret;
1256 if (con->in_base_pos == sizeof(con->in_hdr)) {
1257 u32 crc = crc32c(0, (void *)&con->in_hdr,
1258 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1259 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1260 pr_err("read_partial_message bad hdr "
1261 " crc %u != expected %u\n",
1262 crc, con->in_hdr.crc);
1263 return -EBADMSG;
1264 }
1265 }
1266 }
1267
1268 front_len = le32_to_cpu(con->in_hdr.front_len);
1269 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1270 return -EIO;
1271 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1272 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1273 return -EIO;
1274 data_len = le32_to_cpu(con->in_hdr.data_len);
1275 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1276 return -EIO;
1277
1278 /* allocate message? */
1279 if (!con->in_msg) {
1280 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1281 con->in_hdr.front_len, con->in_hdr.data_len);
1282 con->in_msg = con->ops->alloc_msg(con, &con->in_hdr);
1283 if (!con->in_msg) {
1284 /* skip this message */
1285 dout("alloc_msg returned NULL, skipping message\n");
1286 con->in_base_pos = -front_len - middle_len - data_len -
1287 sizeof(m->footer);
1288 con->in_tag = CEPH_MSGR_TAG_READY;
1289 return 0;
1290 }
1291 if (IS_ERR(con->in_msg)) {
1292 ret = PTR_ERR(con->in_msg);
1293 con->in_msg = NULL;
1294 con->error_msg = "out of memory for incoming message";
1295 return ret;
1296 }
1297 m = con->in_msg;
1298 m->front.iov_len = 0; /* haven't read it yet */
1299 memcpy(&m->hdr, &con->in_hdr, sizeof(con->in_hdr));
1300 }
1301
1302 /* front */
1303 while (m->front.iov_len < front_len) {
1304 BUG_ON(m->front.iov_base == NULL);
1305 left = front_len - m->front.iov_len;
1306 ret = ceph_tcp_recvmsg(con->sock, (char *)m->front.iov_base +
1307 m->front.iov_len, left);
1308 if (ret <= 0)
1309 return ret;
1310 m->front.iov_len += ret;
1311 if (m->front.iov_len == front_len)
1312 con->in_front_crc = crc32c(0, m->front.iov_base,
1313 m->front.iov_len);
1314 }
1315
1316 /* middle */
1317 while (middle_len > 0 && (!m->middle ||
1318 m->middle->vec.iov_len < middle_len)) {
1319 if (m->middle == NULL) {
1320 ret = -EOPNOTSUPP;
1321 if (con->ops->alloc_middle)
1322 ret = con->ops->alloc_middle(con, m);
1323 if (ret < 0) {
1324 dout("alloc_middle failed, skipping payload\n");
1325 con->in_base_pos = -middle_len - data_len
1326 - sizeof(m->footer);
1327 ceph_msg_put(con->in_msg);
1328 con->in_msg = NULL;
1329 con->in_tag = CEPH_MSGR_TAG_READY;
1330 return 0;
1331 }
1332 m->middle->vec.iov_len = 0;
1333 }
1334 left = middle_len - m->middle->vec.iov_len;
1335 ret = ceph_tcp_recvmsg(con->sock,
1336 (char *)m->middle->vec.iov_base +
1337 m->middle->vec.iov_len, left);
1338 if (ret <= 0)
1339 return ret;
1340 m->middle->vec.iov_len += ret;
1341 if (m->middle->vec.iov_len == middle_len)
1342 con->in_middle_crc = crc32c(0, m->middle->vec.iov_base,
1343 m->middle->vec.iov_len);
1344 }
1345
1346 /* (page) data */
1347 data_off = le16_to_cpu(m->hdr.data_off);
1348 if (data_len == 0)
1349 goto no_data;
1350
1351 if (m->nr_pages == 0) {
1352 con->in_msg_pos.page = 0;
1353 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1354 con->in_msg_pos.data_pos = 0;
1355 /* find pages for data payload */
1356 want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1357 ret = -1;
1358 if (con->ops->prepare_pages)
1359 ret = con->ops->prepare_pages(con, m, want);
1360 if (ret < 0) {
1361 dout("%p prepare_pages failed, skipping payload\n", m);
1362 con->in_base_pos = -data_len - sizeof(m->footer);
1363 ceph_msg_put(con->in_msg);
1364 con->in_msg = NULL;
1365 con->in_tag = CEPH_MSGR_TAG_READY;
1366 return 0;
1367 }
1368 BUG_ON(m->nr_pages < want);
1369 }
1370 while (con->in_msg_pos.data_pos < data_len) {
1371 left = min((int)(data_len - con->in_msg_pos.data_pos),
1372 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1373 BUG_ON(m->pages == NULL);
1374 p = kmap(m->pages[con->in_msg_pos.page]);
1375 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1376 left);
1377 if (ret > 0 && datacrc)
1378 con->in_data_crc =
1379 crc32c(con->in_data_crc,
1380 p + con->in_msg_pos.page_pos, ret);
1381 kunmap(m->pages[con->in_msg_pos.page]);
1382 if (ret <= 0)
1383 return ret;
1384 con->in_msg_pos.data_pos += ret;
1385 con->in_msg_pos.page_pos += ret;
1386 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1387 con->in_msg_pos.page_pos = 0;
1388 con->in_msg_pos.page++;
1389 }
1390 }
1391
1392no_data:
1393 /* footer */
1394 to = sizeof(m->hdr) + sizeof(m->footer);
1395 while (con->in_base_pos < to) {
1396 left = to - con->in_base_pos;
1397 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1398 (con->in_base_pos - sizeof(m->hdr)),
1399 left);
1400 if (ret <= 0)
1401 return ret;
1402 con->in_base_pos += ret;
1403 }
1404 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1405 m, front_len, m->footer.front_crc, middle_len,
1406 m->footer.middle_crc, data_len, m->footer.data_crc);
1407
1408 /* crc ok? */
1409 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1410 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1411 m, con->in_front_crc, m->footer.front_crc);
1412 return -EBADMSG;
1413 }
1414 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1415 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1416 m, con->in_middle_crc, m->footer.middle_crc);
1417 return -EBADMSG;
1418 }
1419 if (datacrc &&
1420 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1421 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1422 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1423 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1424 return -EBADMSG;
1425 }
1426
1427 return 1; /* done! */
1428}
1429
1430/*
1431 * Process message. This happens in the worker thread. The callback should
1432 * be careful not to do anything that waits on other incoming messages or it
1433 * may deadlock.
1434 */
1435static void process_message(struct ceph_connection *con)
1436{
Sage Weil5e095e82009-12-14 14:30:34 -08001437 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001438
Sage Weil5e095e82009-12-14 14:30:34 -08001439 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001440 con->in_msg = NULL;
1441
1442 /* if first message, set peer_name */
1443 if (con->peer_name.type == 0)
1444 con->peer_name = msg->hdr.src.name;
1445
1446 mutex_lock(&con->out_mutex);
1447 con->in_seq++;
1448 mutex_unlock(&con->out_mutex);
1449
1450 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1451 msg, le64_to_cpu(msg->hdr.seq),
1452 ENTITY_NAME(msg->hdr.src.name),
1453 le16_to_cpu(msg->hdr.type),
1454 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1455 le32_to_cpu(msg->hdr.front_len),
1456 le32_to_cpu(msg->hdr.data_len),
1457 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1458 con->ops->dispatch(con, msg);
1459 prepare_read_tag(con);
1460}
1461
1462
1463/*
1464 * Write something to the socket. Called in a worker thread when the
1465 * socket appears to be writeable and we have something ready to send.
1466 */
1467static int try_write(struct ceph_connection *con)
1468{
1469 struct ceph_messenger *msgr = con->msgr;
1470 int ret = 1;
1471
1472 dout("try_write start %p state %lu nref %d\n", con, con->state,
1473 atomic_read(&con->nref));
1474
1475 mutex_lock(&con->out_mutex);
1476more:
1477 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1478
1479 /* open the socket first? */
1480 if (con->sock == NULL) {
1481 /*
1482 * if we were STANDBY and are reconnecting _this_
1483 * connection, bump connect_seq now. Always bump
1484 * global_seq.
1485 */
1486 if (test_and_clear_bit(STANDBY, &con->state))
1487 con->connect_seq++;
1488
Sage Weileed0ef22009-11-10 14:34:36 -08001489 prepare_write_banner(msgr, con);
1490 prepare_write_connect(msgr, con, 1);
1491 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001492 set_bit(CONNECTING, &con->state);
Sage Weileed0ef22009-11-10 14:34:36 -08001493 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001494
1495 con->in_tag = CEPH_MSGR_TAG_READY;
1496 dout("try_write initiating connect on %p new state %lu\n",
1497 con, con->state);
1498 con->sock = ceph_tcp_connect(con);
1499 if (IS_ERR(con->sock)) {
1500 con->sock = NULL;
1501 con->error_msg = "connect error";
1502 ret = -1;
1503 goto out;
1504 }
1505 }
1506
1507more_kvec:
1508 /* kvec data queued? */
1509 if (con->out_skip) {
1510 ret = write_partial_skip(con);
1511 if (ret <= 0)
1512 goto done;
1513 if (ret < 0) {
1514 dout("try_write write_partial_skip err %d\n", ret);
1515 goto done;
1516 }
1517 }
1518 if (con->out_kvec_left) {
1519 ret = write_partial_kvec(con);
1520 if (ret <= 0)
1521 goto done;
1522 if (ret < 0) {
1523 dout("try_write write_partial_kvec err %d\n", ret);
1524 goto done;
1525 }
1526 }
1527
1528 /* msg pages? */
1529 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001530 if (con->out_msg_done) {
1531 ceph_msg_put(con->out_msg);
1532 con->out_msg = NULL; /* we're done with this one */
1533 goto do_next;
1534 }
1535
Sage Weil31b80062009-10-06 11:31:13 -07001536 ret = write_partial_msg_pages(con);
1537 if (ret == 1)
1538 goto more_kvec; /* we need to send the footer, too! */
1539 if (ret == 0)
1540 goto done;
1541 if (ret < 0) {
1542 dout("try_write write_partial_msg_pages err %d\n",
1543 ret);
1544 goto done;
1545 }
1546 }
1547
Sage Weilc86a2932009-12-14 14:04:30 -08001548do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001549 if (!test_bit(CONNECTING, &con->state)) {
1550 /* is anything else pending? */
1551 if (!list_empty(&con->out_queue)) {
1552 prepare_write_message(con);
1553 goto more;
1554 }
1555 if (con->in_seq > con->in_seq_acked) {
1556 prepare_write_ack(con);
1557 goto more;
1558 }
1559 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1560 prepare_write_keepalive(con);
1561 goto more;
1562 }
1563 }
1564
1565 /* Nothing to do! */
1566 clear_bit(WRITE_PENDING, &con->state);
1567 dout("try_write nothing else to write.\n");
1568done:
1569 ret = 0;
1570out:
1571 mutex_unlock(&con->out_mutex);
1572 dout("try_write done on %p\n", con);
1573 return ret;
1574}
1575
1576
1577
1578/*
1579 * Read what we can from the socket.
1580 */
1581static int try_read(struct ceph_connection *con)
1582{
1583 struct ceph_messenger *msgr;
1584 int ret = -1;
1585
1586 if (!con->sock)
1587 return 0;
1588
1589 if (test_bit(STANDBY, &con->state))
1590 return 0;
1591
1592 dout("try_read start on %p\n", con);
1593 msgr = con->msgr;
1594
1595more:
1596 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1597 con->in_base_pos);
1598 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08001599 if (!test_bit(NEGOTIATING, &con->state)) {
1600 dout("try_read connecting\n");
1601 ret = read_partial_banner(con);
1602 if (ret <= 0)
1603 goto done;
1604 if (process_banner(con) < 0) {
1605 ret = -1;
1606 goto out;
1607 }
1608 }
Sage Weil31b80062009-10-06 11:31:13 -07001609 ret = read_partial_connect(con);
1610 if (ret <= 0)
1611 goto done;
1612 if (process_connect(con) < 0) {
1613 ret = -1;
1614 goto out;
1615 }
1616 goto more;
1617 }
1618
1619 if (con->in_base_pos < 0) {
1620 /*
1621 * skipping + discarding content.
1622 *
1623 * FIXME: there must be a better way to do this!
1624 */
1625 static char buf[1024];
1626 int skip = min(1024, -con->in_base_pos);
1627 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1628 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1629 if (ret <= 0)
1630 goto done;
1631 con->in_base_pos += ret;
1632 if (con->in_base_pos)
1633 goto more;
1634 }
1635 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1636 /*
1637 * what's next?
1638 */
1639 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1640 if (ret <= 0)
1641 goto done;
1642 dout("try_read got tag %d\n", (int)con->in_tag);
1643 switch (con->in_tag) {
1644 case CEPH_MSGR_TAG_MSG:
1645 prepare_read_message(con);
1646 break;
1647 case CEPH_MSGR_TAG_ACK:
1648 prepare_read_ack(con);
1649 break;
1650 case CEPH_MSGR_TAG_CLOSE:
1651 set_bit(CLOSED, &con->state); /* fixme */
1652 goto done;
1653 default:
1654 goto bad_tag;
1655 }
1656 }
1657 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1658 ret = read_partial_message(con);
1659 if (ret <= 0) {
1660 switch (ret) {
1661 case -EBADMSG:
1662 con->error_msg = "bad crc";
1663 ret = -EIO;
1664 goto out;
1665 case -EIO:
1666 con->error_msg = "io error";
1667 goto out;
1668 default:
1669 goto done;
1670 }
1671 }
1672 if (con->in_tag == CEPH_MSGR_TAG_READY)
1673 goto more;
1674 process_message(con);
1675 goto more;
1676 }
1677 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1678 ret = read_partial_ack(con);
1679 if (ret <= 0)
1680 goto done;
1681 process_ack(con);
1682 goto more;
1683 }
1684
1685done:
1686 ret = 0;
1687out:
1688 dout("try_read done on %p\n", con);
1689 return ret;
1690
1691bad_tag:
1692 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1693 con->error_msg = "protocol error, garbage tag";
1694 ret = -1;
1695 goto out;
1696}
1697
1698
1699/*
1700 * Atomically queue work on a connection. Bump @con reference to
1701 * avoid races with connection teardown.
1702 *
1703 * There is some trickery going on with QUEUED and BUSY because we
1704 * only want a _single_ thread operating on each connection at any
1705 * point in time, but we want to use all available CPUs.
1706 *
1707 * The worker thread only proceeds if it can atomically set BUSY. It
1708 * clears QUEUED and does it's thing. When it thinks it's done, it
1709 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1710 * (tries again to set BUSY).
1711 *
1712 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1713 * try to queue work. If that fails (work is already queued, or BUSY)
1714 * we give up (work also already being done or is queued) but leave QUEUED
1715 * set so that the worker thread will loop if necessary.
1716 */
1717static void queue_con(struct ceph_connection *con)
1718{
1719 if (test_bit(DEAD, &con->state)) {
1720 dout("queue_con %p ignoring: DEAD\n",
1721 con);
1722 return;
1723 }
1724
1725 if (!con->ops->get(con)) {
1726 dout("queue_con %p ref count 0\n", con);
1727 return;
1728 }
1729
1730 set_bit(QUEUED, &con->state);
1731 if (test_bit(BUSY, &con->state)) {
1732 dout("queue_con %p - already BUSY\n", con);
1733 con->ops->put(con);
1734 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1735 dout("queue_con %p - already queued\n", con);
1736 con->ops->put(con);
1737 } else {
1738 dout("queue_con %p\n", con);
1739 }
1740}
1741
1742/*
1743 * Do some work on a connection. Drop a connection ref when we're done.
1744 */
1745static void con_work(struct work_struct *work)
1746{
1747 struct ceph_connection *con = container_of(work, struct ceph_connection,
1748 work.work);
1749 int backoff = 0;
1750
1751more:
1752 if (test_and_set_bit(BUSY, &con->state) != 0) {
1753 dout("con_work %p BUSY already set\n", con);
1754 goto out;
1755 }
1756 dout("con_work %p start, clearing QUEUED\n", con);
1757 clear_bit(QUEUED, &con->state);
1758
1759 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1760 dout("con_work CLOSED\n");
1761 con_close_socket(con);
1762 goto done;
1763 }
1764 if (test_and_clear_bit(OPENING, &con->state)) {
1765 /* reopen w/ new peer */
1766 dout("con_work OPENING\n");
1767 con_close_socket(con);
1768 }
1769
1770 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1771 try_read(con) < 0 ||
1772 try_write(con) < 0) {
1773 backoff = 1;
1774 ceph_fault(con); /* error/fault path */
1775 }
1776
1777done:
1778 clear_bit(BUSY, &con->state);
1779 dout("con->state=%lu\n", con->state);
1780 if (test_bit(QUEUED, &con->state)) {
1781 if (!backoff) {
1782 dout("con_work %p QUEUED reset, looping\n", con);
1783 goto more;
1784 }
1785 dout("con_work %p QUEUED reset, but just faulted\n", con);
1786 clear_bit(QUEUED, &con->state);
1787 }
1788 dout("con_work %p done\n", con);
1789
1790out:
1791 con->ops->put(con);
1792}
1793
1794
1795/*
1796 * Generic error/fault handler. A retry mechanism is used with
1797 * exponential backoff
1798 */
1799static void ceph_fault(struct ceph_connection *con)
1800{
1801 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1802 pr_addr(&con->peer_addr.in_addr), con->error_msg);
1803 dout("fault %p state %lu to peer %s\n",
1804 con, con->state, pr_addr(&con->peer_addr.in_addr));
1805
1806 if (test_bit(LOSSYTX, &con->state)) {
1807 dout("fault on LOSSYTX channel\n");
1808 goto out;
1809 }
1810
1811 clear_bit(BUSY, &con->state); /* to avoid an improbable race */
1812
1813 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08001814
1815 if (con->in_msg) {
1816 ceph_msg_put(con->in_msg);
1817 con->in_msg = NULL;
1818 }
Sage Weil31b80062009-10-06 11:31:13 -07001819
1820 /* If there are no messages in the queue, place the connection
1821 * in a STANDBY state (i.e., don't try to reconnect just yet). */
1822 mutex_lock(&con->out_mutex);
1823 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1824 dout("fault setting STANDBY\n");
1825 set_bit(STANDBY, &con->state);
1826 mutex_unlock(&con->out_mutex);
1827 goto out;
1828 }
1829
1830 /* Requeue anything that hasn't been acked, and retry after a
1831 * delay. */
1832 list_splice_init(&con->out_sent, &con->out_queue);
1833 mutex_unlock(&con->out_mutex);
1834
1835 if (con->delay == 0)
1836 con->delay = BASE_DELAY_INTERVAL;
1837 else if (con->delay < MAX_DELAY_INTERVAL)
1838 con->delay *= 2;
1839
1840 /* explicitly schedule work to try to reconnect again later. */
1841 dout("fault queueing %p delay %lu\n", con, con->delay);
1842 con->ops->get(con);
1843 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1844 round_jiffies_relative(con->delay)) == 0)
1845 con->ops->put(con);
1846
1847out:
1848 if (con->ops->fault)
1849 con->ops->fault(con);
1850}
1851
1852
1853
1854/*
1855 * create a new messenger instance
1856 */
1857struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1858{
1859 struct ceph_messenger *msgr;
1860
1861 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1862 if (msgr == NULL)
1863 return ERR_PTR(-ENOMEM);
1864
1865 spin_lock_init(&msgr->global_seq_lock);
1866
1867 /* the zero page is needed if a request is "canceled" while the message
1868 * is being written over the socket */
1869 msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1870 if (!msgr->zero_page) {
1871 kfree(msgr);
1872 return ERR_PTR(-ENOMEM);
1873 }
1874 kmap(msgr->zero_page);
1875
1876 if (myaddr)
1877 msgr->inst.addr = *myaddr;
1878
1879 /* select a random nonce */
1880 get_random_bytes(&msgr->inst.addr.nonce,
1881 sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08001882 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07001883
1884 dout("messenger_create %p\n", msgr);
1885 return msgr;
1886}
1887
1888void ceph_messenger_destroy(struct ceph_messenger *msgr)
1889{
1890 dout("destroy %p\n", msgr);
1891 kunmap(msgr->zero_page);
1892 __free_page(msgr->zero_page);
1893 kfree(msgr);
1894 dout("destroyed messenger %p\n", msgr);
1895}
1896
1897/*
1898 * Queue up an outgoing message on the given connection.
1899 */
1900void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1901{
1902 if (test_bit(CLOSED, &con->state)) {
1903 dout("con_send %p closed, dropping %p\n", con, msg);
1904 ceph_msg_put(msg);
1905 return;
1906 }
1907
1908 /* set src+dst */
Sage Weil63f2d212009-11-03 15:17:56 -08001909 msg->hdr.src.name = con->msgr->inst.name;
1910 msg->hdr.src.addr = con->msgr->my_enc_addr;
1911 msg->hdr.orig_src = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001912 msg->hdr.dst_erank = con->peer_addr.erank;
1913
1914 /* queue */
1915 mutex_lock(&con->out_mutex);
1916 BUG_ON(!list_empty(&msg->list_head));
1917 list_add_tail(&msg->list_head, &con->out_queue);
1918 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
1919 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
1920 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1921 le32_to_cpu(msg->hdr.front_len),
1922 le32_to_cpu(msg->hdr.middle_len),
1923 le32_to_cpu(msg->hdr.data_len));
1924 mutex_unlock(&con->out_mutex);
1925
1926 /* if there wasn't anything waiting to send before, queue
1927 * new work */
1928 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1929 queue_con(con);
1930}
1931
1932/*
1933 * Revoke a message that was previously queued for send
1934 */
1935void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
1936{
1937 mutex_lock(&con->out_mutex);
1938 if (!list_empty(&msg->list_head)) {
1939 dout("con_revoke %p msg %p\n", con, msg);
1940 list_del_init(&msg->list_head);
1941 ceph_msg_put(msg);
1942 msg->hdr.seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -08001943 if (con->out_msg == msg) {
1944 ceph_msg_put(con->out_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001945 con->out_msg = NULL;
Sage Weilc86a2932009-12-14 14:04:30 -08001946 }
Sage Weil31b80062009-10-06 11:31:13 -07001947 if (con->out_kvec_is_msg) {
1948 con->out_skip = con->out_kvec_bytes;
1949 con->out_kvec_is_msg = false;
1950 }
1951 } else {
1952 dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
1953 }
1954 mutex_unlock(&con->out_mutex);
1955}
1956
1957/*
1958 * Queue a keepalive byte to ensure the tcp connection is alive.
1959 */
1960void ceph_con_keepalive(struct ceph_connection *con)
1961{
1962 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
1963 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1964 queue_con(con);
1965}
1966
1967
1968/*
1969 * construct a new message with given type, size
1970 * the new msg has a ref count of 1.
1971 */
1972struct ceph_msg *ceph_msg_new(int type, int front_len,
1973 int page_len, int page_off, struct page **pages)
1974{
1975 struct ceph_msg *m;
1976
1977 m = kmalloc(sizeof(*m), GFP_NOFS);
1978 if (m == NULL)
1979 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08001980 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07001981 INIT_LIST_HEAD(&m->list_head);
1982
1983 m->hdr.type = cpu_to_le16(type);
1984 m->hdr.front_len = cpu_to_le32(front_len);
1985 m->hdr.middle_len = 0;
1986 m->hdr.data_len = cpu_to_le32(page_len);
1987 m->hdr.data_off = cpu_to_le16(page_off);
1988 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
1989 m->footer.front_crc = 0;
1990 m->footer.middle_crc = 0;
1991 m->footer.data_crc = 0;
1992 m->front_max = front_len;
1993 m->front_is_vmalloc = false;
1994 m->more_to_follow = false;
1995 m->pool = NULL;
1996
1997 /* front */
1998 if (front_len) {
1999 if (front_len > PAGE_CACHE_SIZE) {
2000 m->front.iov_base = __vmalloc(front_len, GFP_NOFS,
2001 PAGE_KERNEL);
2002 m->front_is_vmalloc = true;
2003 } else {
2004 m->front.iov_base = kmalloc(front_len, GFP_NOFS);
2005 }
2006 if (m->front.iov_base == NULL) {
2007 pr_err("msg_new can't allocate %d bytes\n",
2008 front_len);
2009 goto out2;
2010 }
2011 } else {
2012 m->front.iov_base = NULL;
2013 }
2014 m->front.iov_len = front_len;
2015
2016 /* middle */
2017 m->middle = NULL;
2018
2019 /* data */
2020 m->nr_pages = calc_pages_for(page_off, page_len);
2021 m->pages = pages;
2022
2023 dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len,
2024 m->nr_pages);
2025 return m;
2026
2027out2:
2028 ceph_msg_put(m);
2029out:
2030 pr_err("msg_new can't create type %d len %d\n", type, front_len);
2031 return ERR_PTR(-ENOMEM);
2032}
2033
2034/*
2035 * Generic message allocator, for incoming messages.
2036 */
2037struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2038 struct ceph_msg_header *hdr)
2039{
2040 int type = le16_to_cpu(hdr->type);
2041 int front_len = le32_to_cpu(hdr->front_len);
2042 struct ceph_msg *msg = ceph_msg_new(type, front_len, 0, 0, NULL);
2043
2044 if (!msg) {
2045 pr_err("unable to allocate msg type %d len %d\n",
2046 type, front_len);
2047 return ERR_PTR(-ENOMEM);
2048 }
2049 return msg;
2050}
2051
2052/*
2053 * Allocate "middle" portion of a message, if it is needed and wasn't
2054 * allocated by alloc_msg. This allows us to read a small fixed-size
2055 * per-type header in the front and then gracefully fail (i.e.,
2056 * propagate the error to the caller based on info in the front) when
2057 * the middle is too large.
2058 */
2059int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
2060{
2061 int type = le16_to_cpu(msg->hdr.type);
2062 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2063
2064 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2065 ceph_msg_type_name(type), middle_len);
2066 BUG_ON(!middle_len);
2067 BUG_ON(msg->middle);
2068
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002069 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002070 if (!msg->middle)
2071 return -ENOMEM;
2072 return 0;
2073}
2074
2075
2076/*
2077 * Free a generically kmalloc'd message.
2078 */
2079void ceph_msg_kfree(struct ceph_msg *m)
2080{
2081 dout("msg_kfree %p\n", m);
2082 if (m->front_is_vmalloc)
2083 vfree(m->front.iov_base);
2084 else
2085 kfree(m->front.iov_base);
2086 kfree(m);
2087}
2088
2089/*
2090 * Drop a msg ref. Destroy as needed.
2091 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002092void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002093{
Sage Weilc2e552e2009-12-07 15:55:05 -08002094 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002095
Sage Weilc2e552e2009-12-07 15:55:05 -08002096 dout("ceph_msg_put last one on %p\n", m);
2097 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002098
Sage Weilc2e552e2009-12-07 15:55:05 -08002099 /* drop middle, data, if any */
2100 if (m->middle) {
2101 ceph_buffer_put(m->middle);
2102 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002103 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002104 m->nr_pages = 0;
2105 m->pages = NULL;
2106
2107 if (m->pool)
2108 ceph_msgpool_put(m->pool, m);
2109 else
2110 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002111}