blob: c03b4185c143c3deb74a24baacced90ee760ae80 [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 */
Sage Weil31b80062009-10-06 11:31:13 -0700319 ceph_msg_remove_list(&con->out_queue);
320 ceph_msg_remove_list(&con->out_sent);
321
Sage Weilcf3e5c42009-12-11 09:48:05 -0800322 if (con->in_msg) {
323 ceph_msg_put(con->in_msg);
324 con->in_msg = NULL;
325 }
326
Sage Weil31b80062009-10-06 11:31:13 -0700327 con->connect_seq = 0;
328 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800329 if (con->out_msg) {
330 ceph_msg_put(con->out_msg);
331 con->out_msg = NULL;
332 }
Sage Weil31b80062009-10-06 11:31:13 -0700333 con->in_seq = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700334}
335
336/*
337 * mark a peer down. drop any open connections.
338 */
339void ceph_con_close(struct ceph_connection *con)
340{
341 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
342 set_bit(CLOSED, &con->state); /* in case there's queued work */
343 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Sage Weilec302642009-12-22 10:43:42 -0800344 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700345 reset_connection(con);
Sage Weilec302642009-12-22 10:43:42 -0800346 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700347 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;
Sage Weilec302642009-12-22 10:43:42 -0800395 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700396 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
Sage Weilec302642009-12-22 10:43:42 -0800574 mutex_unlock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800575 if (con->ops->get_authorizer)
576 con->ops->get_authorizer(con, &auth_buf, &auth_len,
577 &auth_protocol, &con->auth_reply_buf,
578 &con->auth_reply_buf_len,
579 con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800580 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800581
582 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
583 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
584
585 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
586 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
587 con->out_kvec_left++;
588 con->out_kvec_bytes += auth_len;
589}
590
Sage Weil31b80062009-10-06 11:31:13 -0700591/*
592 * We connected to a peer and are saying hello.
593 */
Sage Weileed0ef22009-11-10 14:34:36 -0800594static void prepare_write_banner(struct ceph_messenger *msgr,
595 struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700596{
597 int len = strlen(CEPH_BANNER);
Sage Weileed0ef22009-11-10 14:34:36 -0800598
599 con->out_kvec[0].iov_base = CEPH_BANNER;
600 con->out_kvec[0].iov_len = len;
601 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
602 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
603 con->out_kvec_left = 2;
604 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
605 con->out_kvec_cur = con->out_kvec;
606 con->out_more = 0;
607 set_bit(WRITE_PENDING, &con->state);
608}
609
610static void prepare_write_connect(struct ceph_messenger *msgr,
611 struct ceph_connection *con,
612 int after_banner)
613{
Sage Weil31b80062009-10-06 11:31:13 -0700614 unsigned global_seq = get_global_seq(con->msgr, 0);
615 int proto;
616
617 switch (con->peer_name.type) {
618 case CEPH_ENTITY_TYPE_MON:
619 proto = CEPH_MONC_PROTOCOL;
620 break;
621 case CEPH_ENTITY_TYPE_OSD:
622 proto = CEPH_OSDC_PROTOCOL;
623 break;
624 case CEPH_ENTITY_TYPE_MDS:
625 proto = CEPH_MDSC_PROTOCOL;
626 break;
627 default:
628 BUG();
629 }
630
631 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
632 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800633
Sage Weil31b80062009-10-06 11:31:13 -0700634 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
635 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
636 con->out_connect.global_seq = cpu_to_le32(global_seq);
637 con->out_connect.protocol_version = cpu_to_le32(proto);
638 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700639
Sage Weileed0ef22009-11-10 14:34:36 -0800640 if (!after_banner) {
641 con->out_kvec_left = 0;
642 con->out_kvec_bytes = 0;
643 }
644 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
645 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
646 con->out_kvec_left++;
647 con->out_kvec_bytes += sizeof(con->out_connect);
Sage Weil31b80062009-10-06 11:31:13 -0700648 con->out_kvec_cur = con->out_kvec;
649 con->out_more = 0;
650 set_bit(WRITE_PENDING, &con->state);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800651
652 prepare_connect_authorizer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700653}
654
655
656/*
657 * write as much of pending kvecs to the socket as we can.
658 * 1 -> done
659 * 0 -> socket full, but more to do
660 * <0 -> error
661 */
662static int write_partial_kvec(struct ceph_connection *con)
663{
664 int ret;
665
666 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
667 while (con->out_kvec_bytes > 0) {
668 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
669 con->out_kvec_left, con->out_kvec_bytes,
670 con->out_more);
671 if (ret <= 0)
672 goto out;
673 con->out_kvec_bytes -= ret;
674 if (con->out_kvec_bytes == 0)
675 break; /* done */
676 while (ret > 0) {
677 if (ret >= con->out_kvec_cur->iov_len) {
678 ret -= con->out_kvec_cur->iov_len;
679 con->out_kvec_cur++;
680 con->out_kvec_left--;
681 } else {
682 con->out_kvec_cur->iov_len -= ret;
683 con->out_kvec_cur->iov_base += ret;
684 ret = 0;
685 break;
686 }
687 }
688 }
689 con->out_kvec_left = 0;
690 con->out_kvec_is_msg = false;
691 ret = 1;
692out:
693 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
694 con->out_kvec_bytes, con->out_kvec_left, ret);
695 return ret; /* done! */
696}
697
698/*
699 * Write as much message data payload as we can. If we finish, queue
700 * up the footer.
701 * 1 -> done, footer is now queued in out_kvec[].
702 * 0 -> socket full, but more to do
703 * <0 -> error
704 */
705static int write_partial_msg_pages(struct ceph_connection *con)
706{
707 struct ceph_msg *msg = con->out_msg;
708 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
709 size_t len;
710 int crc = con->msgr->nocrc;
711 int ret;
712
713 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
714 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
715 con->out_msg_pos.page_pos);
716
717 while (con->out_msg_pos.page < con->out_msg->nr_pages) {
718 struct page *page = NULL;
719 void *kaddr = NULL;
720
721 /*
722 * if we are calculating the data crc (the default), we need
723 * to map the page. if our pages[] has been revoked, use the
724 * zero page.
725 */
726 if (msg->pages) {
727 page = msg->pages[con->out_msg_pos.page];
728 if (crc)
729 kaddr = kmap(page);
730 } else {
731 page = con->msgr->zero_page;
732 if (crc)
733 kaddr = page_address(con->msgr->zero_page);
734 }
735 len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
736 (int)(data_len - con->out_msg_pos.data_pos));
737 if (crc && !con->out_msg_pos.did_page_crc) {
738 void *base = kaddr + con->out_msg_pos.page_pos;
739 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
740
741 BUG_ON(kaddr == NULL);
742 con->out_msg->footer.data_crc =
743 cpu_to_le32(crc32c(tmpcrc, base, len));
744 con->out_msg_pos.did_page_crc = 1;
745 }
746
747 ret = kernel_sendpage(con->sock, page,
748 con->out_msg_pos.page_pos, len,
749 MSG_DONTWAIT | MSG_NOSIGNAL |
750 MSG_MORE);
751
752 if (crc && msg->pages)
753 kunmap(page);
754
755 if (ret <= 0)
756 goto out;
757
758 con->out_msg_pos.data_pos += ret;
759 con->out_msg_pos.page_pos += ret;
760 if (ret == len) {
761 con->out_msg_pos.page_pos = 0;
762 con->out_msg_pos.page++;
763 con->out_msg_pos.did_page_crc = 0;
764 }
765 }
766
767 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
768
769 /* prepare and queue up footer, too */
770 if (!crc)
771 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
772 con->out_kvec_bytes = 0;
773 con->out_kvec_left = 0;
774 con->out_kvec_cur = con->out_kvec;
775 prepare_write_message_footer(con, 0);
776 ret = 1;
777out:
778 return ret;
779}
780
781/*
782 * write some zeros
783 */
784static int write_partial_skip(struct ceph_connection *con)
785{
786 int ret;
787
788 while (con->out_skip > 0) {
789 struct kvec iov = {
790 .iov_base = page_address(con->msgr->zero_page),
791 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
792 };
793
794 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
795 if (ret <= 0)
796 goto out;
797 con->out_skip -= ret;
798 }
799 ret = 1;
800out:
801 return ret;
802}
803
804/*
805 * Prepare to read connection handshake, or an ack.
806 */
Sage Weileed0ef22009-11-10 14:34:36 -0800807static void prepare_read_banner(struct ceph_connection *con)
808{
809 dout("prepare_read_banner %p\n", con);
810 con->in_base_pos = 0;
811}
812
Sage Weil31b80062009-10-06 11:31:13 -0700813static void prepare_read_connect(struct ceph_connection *con)
814{
815 dout("prepare_read_connect %p\n", con);
816 con->in_base_pos = 0;
817}
818
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800819static void prepare_read_connect_retry(struct ceph_connection *con)
820{
821 dout("prepare_read_connect_retry %p\n", con);
822 con->in_base_pos = strlen(CEPH_BANNER) + sizeof(con->actual_peer_addr)
823 + sizeof(con->peer_addr_for_me);
824}
825
Sage Weil31b80062009-10-06 11:31:13 -0700826static void prepare_read_ack(struct ceph_connection *con)
827{
828 dout("prepare_read_ack %p\n", con);
829 con->in_base_pos = 0;
830}
831
832static void prepare_read_tag(struct ceph_connection *con)
833{
834 dout("prepare_read_tag %p\n", con);
835 con->in_base_pos = 0;
836 con->in_tag = CEPH_MSGR_TAG_READY;
837}
838
839/*
840 * Prepare to read a message.
841 */
842static int prepare_read_message(struct ceph_connection *con)
843{
844 dout("prepare_read_message %p\n", con);
845 BUG_ON(con->in_msg != NULL);
846 con->in_base_pos = 0;
847 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
848 return 0;
849}
850
851
852static int read_partial(struct ceph_connection *con,
853 int *to, int size, void *object)
854{
855 *to += size;
856 while (con->in_base_pos < *to) {
857 int left = *to - con->in_base_pos;
858 int have = size - left;
859 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
860 if (ret <= 0)
861 return ret;
862 con->in_base_pos += ret;
863 }
864 return 1;
865}
866
867
868/*
869 * Read all or part of the connect-side handshake on a new connection
870 */
Sage Weileed0ef22009-11-10 14:34:36 -0800871static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700872{
873 int ret, to = 0;
874
Sage Weileed0ef22009-11-10 14:34:36 -0800875 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -0700876
877 /* peer's banner */
878 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
879 if (ret <= 0)
880 goto out;
881 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
882 &con->actual_peer_addr);
883 if (ret <= 0)
884 goto out;
885 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
886 &con->peer_addr_for_me);
887 if (ret <= 0)
888 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -0800889out:
890 return ret;
891}
892
893static int read_partial_connect(struct ceph_connection *con)
894{
895 int ret, to = 0;
896
897 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
898
Sage Weil31b80062009-10-06 11:31:13 -0700899 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
900 if (ret <= 0)
901 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800902 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
903 con->auth_reply_buf);
904 if (ret <= 0)
905 goto out;
Sage Weil31b80062009-10-06 11:31:13 -0700906
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800907 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
908 con, (int)con->in_reply.tag,
909 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -0700910 le32_to_cpu(con->in_reply.global_seq));
911out:
912 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -0800913
Sage Weil31b80062009-10-06 11:31:13 -0700914}
915
916/*
917 * Verify the hello banner looks okay.
918 */
919static int verify_hello(struct ceph_connection *con)
920{
921 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -0700922 pr_err("connect to %s got bad banner\n",
Sage Weil31b80062009-10-06 11:31:13 -0700923 pr_addr(&con->peer_addr.in_addr));
924 con->error_msg = "protocol error, bad banner";
925 return -1;
926 }
927 return 0;
928}
929
930static bool addr_is_blank(struct sockaddr_storage *ss)
931{
932 switch (ss->ss_family) {
933 case AF_INET:
934 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
935 case AF_INET6:
936 return
937 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
938 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
939 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
940 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
941 }
942 return false;
943}
944
945static int addr_port(struct sockaddr_storage *ss)
946{
947 switch (ss->ss_family) {
948 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800949 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -0700950 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800951 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -0700952 }
953 return 0;
954}
955
956static void addr_set_port(struct sockaddr_storage *ss, int p)
957{
958 switch (ss->ss_family) {
959 case AF_INET:
960 ((struct sockaddr_in *)ss)->sin_port = htons(p);
961 case AF_INET6:
962 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
963 }
964}
965
966/*
967 * Parse an ip[:port] list into an addr array. Use the default
968 * monitor port if a port isn't specified.
969 */
970int ceph_parse_ips(const char *c, const char *end,
971 struct ceph_entity_addr *addr,
972 int max_count, int *count)
973{
974 int i;
975 const char *p = c;
976
977 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
978 for (i = 0; i < max_count; i++) {
979 const char *ipend;
980 struct sockaddr_storage *ss = &addr[i].in_addr;
981 struct sockaddr_in *in4 = (void *)ss;
982 struct sockaddr_in6 *in6 = (void *)ss;
983 int port;
984
985 memset(ss, 0, sizeof(*ss));
986 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
987 ',', &ipend)) {
988 ss->ss_family = AF_INET;
989 } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
990 ',', &ipend)) {
991 ss->ss_family = AF_INET6;
992 } else {
993 goto bad;
994 }
995 p = ipend;
996
997 /* port? */
998 if (p < end && *p == ':') {
999 port = 0;
1000 p++;
1001 while (p < end && *p >= '0' && *p <= '9') {
1002 port = (port * 10) + (*p - '0');
1003 p++;
1004 }
1005 if (port > 65535 || port == 0)
1006 goto bad;
1007 } else {
1008 port = CEPH_MON_PORT;
1009 }
1010
1011 addr_set_port(ss, port);
1012
1013 dout("parse_ips got %s\n", pr_addr(ss));
1014
1015 if (p == end)
1016 break;
1017 if (*p != ',')
1018 goto bad;
1019 p++;
1020 }
1021
1022 if (p != end)
1023 goto bad;
1024
1025 if (count)
1026 *count = i + 1;
1027 return 0;
1028
1029bad:
1030 pr_err("parse_ips bad ip '%s'\n", c);
1031 return -EINVAL;
1032}
1033
Sage Weileed0ef22009-11-10 14:34:36 -08001034static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001035{
Sage Weileed0ef22009-11-10 14:34:36 -08001036 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001037
1038 if (verify_hello(con) < 0)
1039 return -1;
1040
Sage Weil63f2d212009-11-03 15:17:56 -08001041 ceph_decode_addr(&con->actual_peer_addr);
1042 ceph_decode_addr(&con->peer_addr_for_me);
1043
Sage Weil31b80062009-10-06 11:31:13 -07001044 /*
1045 * Make sure the other end is who we wanted. note that the other
1046 * end may not yet know their ip address, so if it's 0.0.0.0, give
1047 * them the benefit of the doubt.
1048 */
1049 if (!ceph_entity_addr_is_local(&con->peer_addr,
1050 &con->actual_peer_addr) &&
1051 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1052 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
1053 pr_err("wrong peer, want %s/%d, "
1054 "got %s/%d, wtf\n",
1055 pr_addr(&con->peer_addr.in_addr),
1056 con->peer_addr.nonce,
1057 pr_addr(&con->actual_peer_addr.in_addr),
1058 con->actual_peer_addr.nonce);
1059 con->error_msg = "protocol error, wrong peer";
1060 return -1;
1061 }
1062
1063 /*
1064 * did we learn our address?
1065 */
1066 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1067 int port = addr_port(&con->msgr->inst.addr.in_addr);
1068
1069 memcpy(&con->msgr->inst.addr.in_addr,
1070 &con->peer_addr_for_me.in_addr,
1071 sizeof(con->peer_addr_for_me.in_addr));
1072 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001073 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001074 dout("process_banner learned my addr is %s\n",
Sage Weil31b80062009-10-06 11:31:13 -07001075 pr_addr(&con->msgr->inst.addr.in_addr));
1076 }
1077
Sage Weileed0ef22009-11-10 14:34:36 -08001078 set_bit(NEGOTIATING, &con->state);
1079 prepare_read_connect(con);
1080 return 0;
1081}
1082
1083static int process_connect(struct ceph_connection *con)
1084{
1085 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1086
Sage Weil31b80062009-10-06 11:31:13 -07001087 switch (con->in_reply.tag) {
1088 case CEPH_MSGR_TAG_BADPROTOVER:
1089 dout("process_connect got BADPROTOVER my %d != their %d\n",
1090 le32_to_cpu(con->out_connect.protocol_version),
1091 le32_to_cpu(con->in_reply.protocol_version));
1092 pr_err("%s%lld %s protocol version mismatch,"
1093 " my %d != server's %d\n",
1094 ENTITY_NAME(con->peer_name),
1095 pr_addr(&con->peer_addr.in_addr),
1096 le32_to_cpu(con->out_connect.protocol_version),
1097 le32_to_cpu(con->in_reply.protocol_version));
1098 con->error_msg = "protocol version mismatch";
Sage Weil31b80062009-10-06 11:31:13 -07001099 reset_connection(con);
1100 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weilec302642009-12-22 10:43:42 -08001101
1102 mutex_unlock(&con->mutex);
1103 if (con->ops->bad_proto)
1104 con->ops->bad_proto(con);
1105 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001106 return -1;
1107
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001108 case CEPH_MSGR_TAG_BADAUTHORIZER:
1109 con->auth_retry++;
1110 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1111 con->auth_retry);
1112 if (con->auth_retry == 2) {
1113 con->error_msg = "connect authorization failure";
1114 reset_connection(con);
1115 set_bit(CLOSED, &con->state);
1116 return -1;
1117 }
1118 con->auth_retry = 1;
1119 prepare_write_connect(con->msgr, con, 0);
1120 prepare_read_connect_retry(con);
1121 break;
Sage Weil31b80062009-10-06 11:31:13 -07001122
1123 case CEPH_MSGR_TAG_RESETSESSION:
1124 /*
1125 * If we connected with a large connect_seq but the peer
1126 * has no record of a session with us (no connection, or
1127 * connect_seq == 0), they will send RESETSESION to indicate
1128 * that they must have reset their session, and may have
1129 * dropped messages.
1130 */
1131 dout("process_connect got RESET peer seq %u\n",
1132 le32_to_cpu(con->in_connect.connect_seq));
1133 pr_err("%s%lld %s connection reset\n",
1134 ENTITY_NAME(con->peer_name),
1135 pr_addr(&con->peer_addr.in_addr));
1136 reset_connection(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001137 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001138 prepare_read_connect(con);
1139
1140 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001141 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001142 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1143 if (con->ops->peer_reset)
1144 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001145 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001146 break;
1147
1148 case CEPH_MSGR_TAG_RETRY_SESSION:
1149 /*
1150 * If we sent a smaller connect_seq than the peer has, try
1151 * again with a larger value.
1152 */
1153 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1154 le32_to_cpu(con->out_connect.connect_seq),
1155 le32_to_cpu(con->in_connect.connect_seq));
1156 con->connect_seq = le32_to_cpu(con->in_connect.connect_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_RETRY_GLOBAL:
1162 /*
1163 * If we sent a smaller global_seq than the peer has, try
1164 * again with a larger value.
1165 */
Sage Weileed0ef22009-11-10 14:34:36 -08001166 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001167 con->peer_global_seq,
1168 le32_to_cpu(con->in_connect.global_seq));
1169 get_global_seq(con->msgr,
1170 le32_to_cpu(con->in_connect.global_seq));
Sage Weileed0ef22009-11-10 14:34:36 -08001171 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001172 prepare_read_connect(con);
1173 break;
1174
1175 case CEPH_MSGR_TAG_READY:
1176 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001177 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1178 con->connect_seq++;
1179 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1180 con->peer_global_seq,
1181 le32_to_cpu(con->in_reply.connect_seq),
1182 con->connect_seq);
1183 WARN_ON(con->connect_seq !=
1184 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001185
1186 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1187 set_bit(LOSSYTX, &con->state);
1188
Sage Weil31b80062009-10-06 11:31:13 -07001189 prepare_read_tag(con);
1190 break;
1191
1192 case CEPH_MSGR_TAG_WAIT:
1193 /*
1194 * If there is a connection race (we are opening
1195 * connections to each other), one of us may just have
1196 * to WAIT. This shouldn't happen if we are the
1197 * client.
1198 */
1199 pr_err("process_connect peer connecting WAIT\n");
1200
1201 default:
1202 pr_err("connect protocol error, will retry\n");
1203 con->error_msg = "protocol error, garbage tag during connect";
1204 return -1;
1205 }
1206 return 0;
1207}
1208
1209
1210/*
1211 * read (part of) an ack
1212 */
1213static int read_partial_ack(struct ceph_connection *con)
1214{
1215 int to = 0;
1216
1217 return read_partial(con, &to, sizeof(con->in_temp_ack),
1218 &con->in_temp_ack);
1219}
1220
1221
1222/*
1223 * We can finally discard anything that's been acked.
1224 */
1225static void process_ack(struct ceph_connection *con)
1226{
1227 struct ceph_msg *m;
1228 u64 ack = le64_to_cpu(con->in_temp_ack);
1229 u64 seq;
1230
Sage Weil31b80062009-10-06 11:31:13 -07001231 while (!list_empty(&con->out_sent)) {
1232 m = list_first_entry(&con->out_sent, struct ceph_msg,
1233 list_head);
1234 seq = le64_to_cpu(m->hdr.seq);
1235 if (seq > ack)
1236 break;
1237 dout("got ack for seq %llu type %d at %p\n", seq,
1238 le16_to_cpu(m->hdr.type), m);
1239 ceph_msg_remove(m);
1240 }
Sage Weil31b80062009-10-06 11:31:13 -07001241 prepare_read_tag(con);
1242}
1243
1244
1245
1246
1247
1248
1249/*
1250 * read (part of) a message.
1251 */
1252static int read_partial_message(struct ceph_connection *con)
1253{
1254 struct ceph_msg *m = con->in_msg;
1255 void *p;
1256 int ret;
1257 int to, want, left;
1258 unsigned front_len, middle_len, data_len, data_off;
1259 int datacrc = con->msgr->nocrc;
1260
1261 dout("read_partial_message con %p msg %p\n", con, m);
1262
1263 /* header */
1264 while (con->in_base_pos < sizeof(con->in_hdr)) {
1265 left = sizeof(con->in_hdr) - con->in_base_pos;
1266 ret = ceph_tcp_recvmsg(con->sock,
1267 (char *)&con->in_hdr + con->in_base_pos,
1268 left);
1269 if (ret <= 0)
1270 return ret;
1271 con->in_base_pos += ret;
1272 if (con->in_base_pos == sizeof(con->in_hdr)) {
1273 u32 crc = crc32c(0, (void *)&con->in_hdr,
1274 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1275 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1276 pr_err("read_partial_message bad hdr "
1277 " crc %u != expected %u\n",
1278 crc, con->in_hdr.crc);
1279 return -EBADMSG;
1280 }
1281 }
1282 }
1283
1284 front_len = le32_to_cpu(con->in_hdr.front_len);
1285 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1286 return -EIO;
1287 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1288 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1289 return -EIO;
1290 data_len = le32_to_cpu(con->in_hdr.data_len);
1291 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1292 return -EIO;
1293
1294 /* allocate message? */
1295 if (!con->in_msg) {
1296 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1297 con->in_hdr.front_len, con->in_hdr.data_len);
1298 con->in_msg = con->ops->alloc_msg(con, &con->in_hdr);
1299 if (!con->in_msg) {
1300 /* skip this message */
Sage Weilcf3e5c42009-12-11 09:48:05 -08001301 pr_err("alloc_msg returned NULL, skipping message\n");
Sage Weil31b80062009-10-06 11:31:13 -07001302 con->in_base_pos = -front_len - middle_len - data_len -
1303 sizeof(m->footer);
1304 con->in_tag = CEPH_MSGR_TAG_READY;
1305 return 0;
1306 }
1307 if (IS_ERR(con->in_msg)) {
1308 ret = PTR_ERR(con->in_msg);
1309 con->in_msg = NULL;
1310 con->error_msg = "out of memory for incoming message";
1311 return ret;
1312 }
1313 m = con->in_msg;
1314 m->front.iov_len = 0; /* haven't read it yet */
1315 memcpy(&m->hdr, &con->in_hdr, sizeof(con->in_hdr));
1316 }
1317
1318 /* front */
1319 while (m->front.iov_len < front_len) {
1320 BUG_ON(m->front.iov_base == NULL);
1321 left = front_len - m->front.iov_len;
1322 ret = ceph_tcp_recvmsg(con->sock, (char *)m->front.iov_base +
1323 m->front.iov_len, left);
1324 if (ret <= 0)
1325 return ret;
1326 m->front.iov_len += ret;
1327 if (m->front.iov_len == front_len)
1328 con->in_front_crc = crc32c(0, m->front.iov_base,
1329 m->front.iov_len);
1330 }
1331
1332 /* middle */
1333 while (middle_len > 0 && (!m->middle ||
1334 m->middle->vec.iov_len < middle_len)) {
1335 if (m->middle == NULL) {
1336 ret = -EOPNOTSUPP;
1337 if (con->ops->alloc_middle)
1338 ret = con->ops->alloc_middle(con, m);
1339 if (ret < 0) {
Sage Weilcf3e5c42009-12-11 09:48:05 -08001340 pr_err("alloc_middle fail skipping payload\n");
Sage Weil31b80062009-10-06 11:31:13 -07001341 con->in_base_pos = -middle_len - data_len
1342 - sizeof(m->footer);
1343 ceph_msg_put(con->in_msg);
1344 con->in_msg = NULL;
1345 con->in_tag = CEPH_MSGR_TAG_READY;
1346 return 0;
1347 }
1348 m->middle->vec.iov_len = 0;
1349 }
1350 left = middle_len - m->middle->vec.iov_len;
1351 ret = ceph_tcp_recvmsg(con->sock,
1352 (char *)m->middle->vec.iov_base +
1353 m->middle->vec.iov_len, left);
1354 if (ret <= 0)
1355 return ret;
1356 m->middle->vec.iov_len += ret;
1357 if (m->middle->vec.iov_len == middle_len)
1358 con->in_middle_crc = crc32c(0, m->middle->vec.iov_base,
1359 m->middle->vec.iov_len);
1360 }
1361
1362 /* (page) data */
1363 data_off = le16_to_cpu(m->hdr.data_off);
1364 if (data_len == 0)
1365 goto no_data;
1366
1367 if (m->nr_pages == 0) {
1368 con->in_msg_pos.page = 0;
1369 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1370 con->in_msg_pos.data_pos = 0;
1371 /* find pages for data payload */
1372 want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1373 ret = -1;
Sage Weilec302642009-12-22 10:43:42 -08001374 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001375 if (con->ops->prepare_pages)
1376 ret = con->ops->prepare_pages(con, m, want);
Sage Weilec302642009-12-22 10:43:42 -08001377 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001378 if (ret < 0) {
1379 dout("%p prepare_pages failed, skipping payload\n", m);
1380 con->in_base_pos = -data_len - sizeof(m->footer);
1381 ceph_msg_put(con->in_msg);
1382 con->in_msg = NULL;
1383 con->in_tag = CEPH_MSGR_TAG_READY;
1384 return 0;
1385 }
1386 BUG_ON(m->nr_pages < want);
1387 }
1388 while (con->in_msg_pos.data_pos < data_len) {
1389 left = min((int)(data_len - con->in_msg_pos.data_pos),
1390 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1391 BUG_ON(m->pages == NULL);
1392 p = kmap(m->pages[con->in_msg_pos.page]);
1393 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1394 left);
1395 if (ret > 0 && datacrc)
1396 con->in_data_crc =
1397 crc32c(con->in_data_crc,
1398 p + con->in_msg_pos.page_pos, ret);
1399 kunmap(m->pages[con->in_msg_pos.page]);
1400 if (ret <= 0)
1401 return ret;
1402 con->in_msg_pos.data_pos += ret;
1403 con->in_msg_pos.page_pos += ret;
1404 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1405 con->in_msg_pos.page_pos = 0;
1406 con->in_msg_pos.page++;
1407 }
1408 }
1409
1410no_data:
1411 /* footer */
1412 to = sizeof(m->hdr) + sizeof(m->footer);
1413 while (con->in_base_pos < to) {
1414 left = to - con->in_base_pos;
1415 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1416 (con->in_base_pos - sizeof(m->hdr)),
1417 left);
1418 if (ret <= 0)
1419 return ret;
1420 con->in_base_pos += ret;
1421 }
1422 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1423 m, front_len, m->footer.front_crc, middle_len,
1424 m->footer.middle_crc, data_len, m->footer.data_crc);
1425
1426 /* crc ok? */
1427 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1428 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1429 m, con->in_front_crc, m->footer.front_crc);
1430 return -EBADMSG;
1431 }
1432 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1433 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1434 m, con->in_middle_crc, m->footer.middle_crc);
1435 return -EBADMSG;
1436 }
1437 if (datacrc &&
1438 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1439 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1440 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1441 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1442 return -EBADMSG;
1443 }
1444
1445 return 1; /* done! */
1446}
1447
1448/*
1449 * Process message. This happens in the worker thread. The callback should
1450 * be careful not to do anything that waits on other incoming messages or it
1451 * may deadlock.
1452 */
1453static void process_message(struct ceph_connection *con)
1454{
Sage Weil5e095e82009-12-14 14:30:34 -08001455 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001456
Sage Weil5e095e82009-12-14 14:30:34 -08001457 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001458 con->in_msg = NULL;
1459
1460 /* if first message, set peer_name */
1461 if (con->peer_name.type == 0)
1462 con->peer_name = msg->hdr.src.name;
1463
Sage Weil31b80062009-10-06 11:31:13 -07001464 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001465 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001466
1467 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1468 msg, le64_to_cpu(msg->hdr.seq),
1469 ENTITY_NAME(msg->hdr.src.name),
1470 le16_to_cpu(msg->hdr.type),
1471 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1472 le32_to_cpu(msg->hdr.front_len),
1473 le32_to_cpu(msg->hdr.data_len),
1474 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1475 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001476
1477 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001478 prepare_read_tag(con);
1479}
1480
1481
1482/*
1483 * Write something to the socket. Called in a worker thread when the
1484 * socket appears to be writeable and we have something ready to send.
1485 */
1486static int try_write(struct ceph_connection *con)
1487{
1488 struct ceph_messenger *msgr = con->msgr;
1489 int ret = 1;
1490
1491 dout("try_write start %p state %lu nref %d\n", con, con->state,
1492 atomic_read(&con->nref));
1493
Sage Weilec302642009-12-22 10:43:42 -08001494 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001495more:
1496 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1497
1498 /* open the socket first? */
1499 if (con->sock == NULL) {
1500 /*
1501 * if we were STANDBY and are reconnecting _this_
1502 * connection, bump connect_seq now. Always bump
1503 * global_seq.
1504 */
1505 if (test_and_clear_bit(STANDBY, &con->state))
1506 con->connect_seq++;
1507
Sage Weileed0ef22009-11-10 14:34:36 -08001508 prepare_write_banner(msgr, con);
1509 prepare_write_connect(msgr, con, 1);
1510 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001511 set_bit(CONNECTING, &con->state);
Sage Weileed0ef22009-11-10 14:34:36 -08001512 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001513
Sage Weilcf3e5c42009-12-11 09:48:05 -08001514 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001515 con->in_tag = CEPH_MSGR_TAG_READY;
1516 dout("try_write initiating connect on %p new state %lu\n",
1517 con, con->state);
1518 con->sock = ceph_tcp_connect(con);
1519 if (IS_ERR(con->sock)) {
1520 con->sock = NULL;
1521 con->error_msg = "connect error";
1522 ret = -1;
1523 goto out;
1524 }
1525 }
1526
1527more_kvec:
1528 /* kvec data queued? */
1529 if (con->out_skip) {
1530 ret = write_partial_skip(con);
1531 if (ret <= 0)
1532 goto done;
1533 if (ret < 0) {
1534 dout("try_write write_partial_skip err %d\n", ret);
1535 goto done;
1536 }
1537 }
1538 if (con->out_kvec_left) {
1539 ret = write_partial_kvec(con);
1540 if (ret <= 0)
1541 goto done;
Sage Weil31b80062009-10-06 11:31:13 -07001542 }
1543
1544 /* msg pages? */
1545 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001546 if (con->out_msg_done) {
1547 ceph_msg_put(con->out_msg);
1548 con->out_msg = NULL; /* we're done with this one */
1549 goto do_next;
1550 }
1551
Sage Weil31b80062009-10-06 11:31:13 -07001552 ret = write_partial_msg_pages(con);
1553 if (ret == 1)
1554 goto more_kvec; /* we need to send the footer, too! */
1555 if (ret == 0)
1556 goto done;
1557 if (ret < 0) {
1558 dout("try_write write_partial_msg_pages err %d\n",
1559 ret);
1560 goto done;
1561 }
1562 }
1563
Sage Weilc86a2932009-12-14 14:04:30 -08001564do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001565 if (!test_bit(CONNECTING, &con->state)) {
1566 /* is anything else pending? */
1567 if (!list_empty(&con->out_queue)) {
1568 prepare_write_message(con);
1569 goto more;
1570 }
1571 if (con->in_seq > con->in_seq_acked) {
1572 prepare_write_ack(con);
1573 goto more;
1574 }
1575 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1576 prepare_write_keepalive(con);
1577 goto more;
1578 }
1579 }
1580
1581 /* Nothing to do! */
1582 clear_bit(WRITE_PENDING, &con->state);
1583 dout("try_write nothing else to write.\n");
1584done:
1585 ret = 0;
1586out:
Sage Weilec302642009-12-22 10:43:42 -08001587 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001588 dout("try_write done on %p\n", con);
1589 return ret;
1590}
1591
1592
1593
1594/*
1595 * Read what we can from the socket.
1596 */
1597static int try_read(struct ceph_connection *con)
1598{
1599 struct ceph_messenger *msgr;
1600 int ret = -1;
1601
1602 if (!con->sock)
1603 return 0;
1604
1605 if (test_bit(STANDBY, &con->state))
1606 return 0;
1607
1608 dout("try_read start on %p\n", con);
1609 msgr = con->msgr;
1610
Sage Weilec302642009-12-22 10:43:42 -08001611 mutex_lock(&con->mutex);
1612
Sage Weil31b80062009-10-06 11:31:13 -07001613more:
1614 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1615 con->in_base_pos);
1616 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08001617 if (!test_bit(NEGOTIATING, &con->state)) {
1618 dout("try_read connecting\n");
1619 ret = read_partial_banner(con);
1620 if (ret <= 0)
1621 goto done;
1622 if (process_banner(con) < 0) {
1623 ret = -1;
1624 goto out;
1625 }
1626 }
Sage Weil31b80062009-10-06 11:31:13 -07001627 ret = read_partial_connect(con);
1628 if (ret <= 0)
1629 goto done;
1630 if (process_connect(con) < 0) {
1631 ret = -1;
1632 goto out;
1633 }
1634 goto more;
1635 }
1636
1637 if (con->in_base_pos < 0) {
1638 /*
1639 * skipping + discarding content.
1640 *
1641 * FIXME: there must be a better way to do this!
1642 */
1643 static char buf[1024];
1644 int skip = min(1024, -con->in_base_pos);
1645 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1646 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1647 if (ret <= 0)
1648 goto done;
1649 con->in_base_pos += ret;
1650 if (con->in_base_pos)
1651 goto more;
1652 }
1653 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1654 /*
1655 * what's next?
1656 */
1657 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1658 if (ret <= 0)
1659 goto done;
1660 dout("try_read got tag %d\n", (int)con->in_tag);
1661 switch (con->in_tag) {
1662 case CEPH_MSGR_TAG_MSG:
1663 prepare_read_message(con);
1664 break;
1665 case CEPH_MSGR_TAG_ACK:
1666 prepare_read_ack(con);
1667 break;
1668 case CEPH_MSGR_TAG_CLOSE:
1669 set_bit(CLOSED, &con->state); /* fixme */
1670 goto done;
1671 default:
1672 goto bad_tag;
1673 }
1674 }
1675 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1676 ret = read_partial_message(con);
1677 if (ret <= 0) {
1678 switch (ret) {
1679 case -EBADMSG:
1680 con->error_msg = "bad crc";
1681 ret = -EIO;
1682 goto out;
1683 case -EIO:
1684 con->error_msg = "io error";
1685 goto out;
1686 default:
1687 goto done;
1688 }
1689 }
1690 if (con->in_tag == CEPH_MSGR_TAG_READY)
1691 goto more;
1692 process_message(con);
1693 goto more;
1694 }
1695 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1696 ret = read_partial_ack(con);
1697 if (ret <= 0)
1698 goto done;
1699 process_ack(con);
1700 goto more;
1701 }
1702
1703done:
1704 ret = 0;
1705out:
Sage Weilec302642009-12-22 10:43:42 -08001706 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001707 dout("try_read done on %p\n", con);
1708 return ret;
1709
1710bad_tag:
1711 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1712 con->error_msg = "protocol error, garbage tag";
1713 ret = -1;
1714 goto out;
1715}
1716
1717
1718/*
1719 * Atomically queue work on a connection. Bump @con reference to
1720 * avoid races with connection teardown.
1721 *
1722 * There is some trickery going on with QUEUED and BUSY because we
1723 * only want a _single_ thread operating on each connection at any
1724 * point in time, but we want to use all available CPUs.
1725 *
1726 * The worker thread only proceeds if it can atomically set BUSY. It
1727 * clears QUEUED and does it's thing. When it thinks it's done, it
1728 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1729 * (tries again to set BUSY).
1730 *
1731 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1732 * try to queue work. If that fails (work is already queued, or BUSY)
1733 * we give up (work also already being done or is queued) but leave QUEUED
1734 * set so that the worker thread will loop if necessary.
1735 */
1736static void queue_con(struct ceph_connection *con)
1737{
1738 if (test_bit(DEAD, &con->state)) {
1739 dout("queue_con %p ignoring: DEAD\n",
1740 con);
1741 return;
1742 }
1743
1744 if (!con->ops->get(con)) {
1745 dout("queue_con %p ref count 0\n", con);
1746 return;
1747 }
1748
1749 set_bit(QUEUED, &con->state);
1750 if (test_bit(BUSY, &con->state)) {
1751 dout("queue_con %p - already BUSY\n", con);
1752 con->ops->put(con);
1753 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1754 dout("queue_con %p - already queued\n", con);
1755 con->ops->put(con);
1756 } else {
1757 dout("queue_con %p\n", con);
1758 }
1759}
1760
1761/*
1762 * Do some work on a connection. Drop a connection ref when we're done.
1763 */
1764static void con_work(struct work_struct *work)
1765{
1766 struct ceph_connection *con = container_of(work, struct ceph_connection,
1767 work.work);
1768 int backoff = 0;
1769
1770more:
1771 if (test_and_set_bit(BUSY, &con->state) != 0) {
1772 dout("con_work %p BUSY already set\n", con);
1773 goto out;
1774 }
1775 dout("con_work %p start, clearing QUEUED\n", con);
1776 clear_bit(QUEUED, &con->state);
1777
1778 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1779 dout("con_work CLOSED\n");
1780 con_close_socket(con);
1781 goto done;
1782 }
1783 if (test_and_clear_bit(OPENING, &con->state)) {
1784 /* reopen w/ new peer */
1785 dout("con_work OPENING\n");
1786 con_close_socket(con);
1787 }
1788
1789 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1790 try_read(con) < 0 ||
1791 try_write(con) < 0) {
1792 backoff = 1;
1793 ceph_fault(con); /* error/fault path */
1794 }
1795
1796done:
1797 clear_bit(BUSY, &con->state);
1798 dout("con->state=%lu\n", con->state);
1799 if (test_bit(QUEUED, &con->state)) {
1800 if (!backoff) {
1801 dout("con_work %p QUEUED reset, looping\n", con);
1802 goto more;
1803 }
1804 dout("con_work %p QUEUED reset, but just faulted\n", con);
1805 clear_bit(QUEUED, &con->state);
1806 }
1807 dout("con_work %p done\n", con);
1808
1809out:
1810 con->ops->put(con);
1811}
1812
1813
1814/*
1815 * Generic error/fault handler. A retry mechanism is used with
1816 * exponential backoff
1817 */
1818static void ceph_fault(struct ceph_connection *con)
1819{
1820 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1821 pr_addr(&con->peer_addr.in_addr), con->error_msg);
1822 dout("fault %p state %lu to peer %s\n",
1823 con, con->state, pr_addr(&con->peer_addr.in_addr));
1824
1825 if (test_bit(LOSSYTX, &con->state)) {
1826 dout("fault on LOSSYTX channel\n");
1827 goto out;
1828 }
1829
1830 clear_bit(BUSY, &con->state); /* to avoid an improbable race */
1831
Sage Weilec302642009-12-22 10:43:42 -08001832 mutex_lock(&con->mutex);
1833
Sage Weil31b80062009-10-06 11:31:13 -07001834 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08001835
1836 if (con->in_msg) {
1837 ceph_msg_put(con->in_msg);
1838 con->in_msg = NULL;
1839 }
Sage Weil31b80062009-10-06 11:31:13 -07001840
1841 /* If there are no messages in the queue, place the connection
1842 * in a STANDBY state (i.e., don't try to reconnect just yet). */
Sage Weil31b80062009-10-06 11:31:13 -07001843 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1844 dout("fault setting STANDBY\n");
1845 set_bit(STANDBY, &con->state);
Sage Weilec302642009-12-22 10:43:42 -08001846 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001847 goto out;
1848 }
1849
1850 /* Requeue anything that hasn't been acked, and retry after a
1851 * delay. */
1852 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil31b80062009-10-06 11:31:13 -07001853
1854 if (con->delay == 0)
1855 con->delay = BASE_DELAY_INTERVAL;
1856 else if (con->delay < MAX_DELAY_INTERVAL)
1857 con->delay *= 2;
1858
Sage Weilec302642009-12-22 10:43:42 -08001859 mutex_unlock(&con->mutex);
1860
Sage Weil31b80062009-10-06 11:31:13 -07001861 /* explicitly schedule work to try to reconnect again later. */
1862 dout("fault queueing %p delay %lu\n", con, con->delay);
1863 con->ops->get(con);
1864 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1865 round_jiffies_relative(con->delay)) == 0)
1866 con->ops->put(con);
1867
1868out:
1869 if (con->ops->fault)
1870 con->ops->fault(con);
1871}
1872
1873
1874
1875/*
1876 * create a new messenger instance
1877 */
1878struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1879{
1880 struct ceph_messenger *msgr;
1881
1882 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1883 if (msgr == NULL)
1884 return ERR_PTR(-ENOMEM);
1885
1886 spin_lock_init(&msgr->global_seq_lock);
1887
1888 /* the zero page is needed if a request is "canceled" while the message
1889 * is being written over the socket */
1890 msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1891 if (!msgr->zero_page) {
1892 kfree(msgr);
1893 return ERR_PTR(-ENOMEM);
1894 }
1895 kmap(msgr->zero_page);
1896
1897 if (myaddr)
1898 msgr->inst.addr = *myaddr;
1899
1900 /* select a random nonce */
1901 get_random_bytes(&msgr->inst.addr.nonce,
1902 sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08001903 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07001904
1905 dout("messenger_create %p\n", msgr);
1906 return msgr;
1907}
1908
1909void ceph_messenger_destroy(struct ceph_messenger *msgr)
1910{
1911 dout("destroy %p\n", msgr);
1912 kunmap(msgr->zero_page);
1913 __free_page(msgr->zero_page);
1914 kfree(msgr);
1915 dout("destroyed messenger %p\n", msgr);
1916}
1917
1918/*
1919 * Queue up an outgoing message on the given connection.
1920 */
1921void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1922{
1923 if (test_bit(CLOSED, &con->state)) {
1924 dout("con_send %p closed, dropping %p\n", con, msg);
1925 ceph_msg_put(msg);
1926 return;
1927 }
1928
1929 /* set src+dst */
Sage Weil63f2d212009-11-03 15:17:56 -08001930 msg->hdr.src.name = con->msgr->inst.name;
1931 msg->hdr.src.addr = con->msgr->my_enc_addr;
1932 msg->hdr.orig_src = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001933 msg->hdr.dst_erank = con->peer_addr.erank;
1934
1935 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08001936 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001937 BUG_ON(!list_empty(&msg->list_head));
1938 list_add_tail(&msg->list_head, &con->out_queue);
1939 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
1940 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
1941 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1942 le32_to_cpu(msg->hdr.front_len),
1943 le32_to_cpu(msg->hdr.middle_len),
1944 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08001945 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001946
1947 /* if there wasn't anything waiting to send before, queue
1948 * new work */
1949 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1950 queue_con(con);
1951}
1952
1953/*
1954 * Revoke a message that was previously queued for send
1955 */
1956void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
1957{
Sage Weilec302642009-12-22 10:43:42 -08001958 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001959 if (!list_empty(&msg->list_head)) {
1960 dout("con_revoke %p msg %p\n", con, msg);
1961 list_del_init(&msg->list_head);
1962 ceph_msg_put(msg);
1963 msg->hdr.seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -08001964 if (con->out_msg == msg) {
1965 ceph_msg_put(con->out_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001966 con->out_msg = NULL;
Sage Weilc86a2932009-12-14 14:04:30 -08001967 }
Sage Weil31b80062009-10-06 11:31:13 -07001968 if (con->out_kvec_is_msg) {
1969 con->out_skip = con->out_kvec_bytes;
1970 con->out_kvec_is_msg = false;
1971 }
1972 } else {
1973 dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
1974 }
Sage Weilec302642009-12-22 10:43:42 -08001975 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001976}
1977
1978/*
1979 * Queue a keepalive byte to ensure the tcp connection is alive.
1980 */
1981void ceph_con_keepalive(struct ceph_connection *con)
1982{
1983 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
1984 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1985 queue_con(con);
1986}
1987
1988
1989/*
1990 * construct a new message with given type, size
1991 * the new msg has a ref count of 1.
1992 */
1993struct ceph_msg *ceph_msg_new(int type, int front_len,
1994 int page_len, int page_off, struct page **pages)
1995{
1996 struct ceph_msg *m;
1997
1998 m = kmalloc(sizeof(*m), GFP_NOFS);
1999 if (m == NULL)
2000 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002001 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07002002 INIT_LIST_HEAD(&m->list_head);
2003
2004 m->hdr.type = cpu_to_le16(type);
2005 m->hdr.front_len = cpu_to_le32(front_len);
2006 m->hdr.middle_len = 0;
2007 m->hdr.data_len = cpu_to_le32(page_len);
2008 m->hdr.data_off = cpu_to_le16(page_off);
2009 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2010 m->footer.front_crc = 0;
2011 m->footer.middle_crc = 0;
2012 m->footer.data_crc = 0;
2013 m->front_max = front_len;
2014 m->front_is_vmalloc = false;
2015 m->more_to_follow = false;
2016 m->pool = NULL;
2017
2018 /* front */
2019 if (front_len) {
2020 if (front_len > PAGE_CACHE_SIZE) {
2021 m->front.iov_base = __vmalloc(front_len, GFP_NOFS,
2022 PAGE_KERNEL);
2023 m->front_is_vmalloc = true;
2024 } else {
2025 m->front.iov_base = kmalloc(front_len, GFP_NOFS);
2026 }
2027 if (m->front.iov_base == NULL) {
2028 pr_err("msg_new can't allocate %d bytes\n",
2029 front_len);
2030 goto out2;
2031 }
2032 } else {
2033 m->front.iov_base = NULL;
2034 }
2035 m->front.iov_len = front_len;
2036
2037 /* middle */
2038 m->middle = NULL;
2039
2040 /* data */
2041 m->nr_pages = calc_pages_for(page_off, page_len);
2042 m->pages = pages;
2043
2044 dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len,
2045 m->nr_pages);
2046 return m;
2047
2048out2:
2049 ceph_msg_put(m);
2050out:
2051 pr_err("msg_new can't create type %d len %d\n", type, front_len);
2052 return ERR_PTR(-ENOMEM);
2053}
2054
2055/*
2056 * Generic message allocator, for incoming messages.
2057 */
2058struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2059 struct ceph_msg_header *hdr)
2060{
2061 int type = le16_to_cpu(hdr->type);
2062 int front_len = le32_to_cpu(hdr->front_len);
2063 struct ceph_msg *msg = ceph_msg_new(type, front_len, 0, 0, NULL);
2064
2065 if (!msg) {
2066 pr_err("unable to allocate msg type %d len %d\n",
2067 type, front_len);
2068 return ERR_PTR(-ENOMEM);
2069 }
2070 return msg;
2071}
2072
2073/*
2074 * Allocate "middle" portion of a message, if it is needed and wasn't
2075 * allocated by alloc_msg. This allows us to read a small fixed-size
2076 * per-type header in the front and then gracefully fail (i.e.,
2077 * propagate the error to the caller based on info in the front) when
2078 * the middle is too large.
2079 */
2080int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
2081{
2082 int type = le16_to_cpu(msg->hdr.type);
2083 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2084
2085 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2086 ceph_msg_type_name(type), middle_len);
2087 BUG_ON(!middle_len);
2088 BUG_ON(msg->middle);
2089
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002090 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002091 if (!msg->middle)
2092 return -ENOMEM;
2093 return 0;
2094}
2095
2096
2097/*
2098 * Free a generically kmalloc'd message.
2099 */
2100void ceph_msg_kfree(struct ceph_msg *m)
2101{
2102 dout("msg_kfree %p\n", m);
2103 if (m->front_is_vmalloc)
2104 vfree(m->front.iov_base);
2105 else
2106 kfree(m->front.iov_base);
2107 kfree(m);
2108}
2109
2110/*
2111 * Drop a msg ref. Destroy as needed.
2112 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002113void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002114{
Sage Weilc2e552e2009-12-07 15:55:05 -08002115 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002116
Sage Weilc2e552e2009-12-07 15:55:05 -08002117 dout("ceph_msg_put last one on %p\n", m);
2118 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002119
Sage Weilc2e552e2009-12-07 15:55:05 -08002120 /* drop middle, data, if any */
2121 if (m->middle) {
2122 ceph_buffer_put(m->middle);
2123 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002124 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002125 m->nr_pages = 0;
2126 m->pages = NULL;
2127
2128 if (m->pool)
2129 ceph_msgpool_put(m->pool, m);
2130 else
2131 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002132}
Sage Weil9ec7cab2009-12-14 15:13:47 -08002133
2134void ceph_msg_dump(struct ceph_msg *msg)
2135{
2136 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2137 msg->front_max, msg->nr_pages);
2138 print_hex_dump(KERN_DEBUG, "header: ",
2139 DUMP_PREFIX_OFFSET, 16, 1,
2140 &msg->hdr, sizeof(msg->hdr), true);
2141 print_hex_dump(KERN_DEBUG, " front: ",
2142 DUMP_PREFIX_OFFSET, 16, 1,
2143 msg->front.iov_base, msg->front.iov_len, true);
2144 if (msg->middle)
2145 print_hex_dump(KERN_DEBUG, "middle: ",
2146 DUMP_PREFIX_OFFSET, 16, 1,
2147 msg->middle->vec.iov_base,
2148 msg->middle->vec.iov_len, true);
2149 print_hex_dump(KERN_DEBUG, "footer: ",
2150 DUMP_PREFIX_OFFSET, 16, 1,
2151 &msg->footer, sizeof(msg->footer), true);
2152}