blob: f35b4945a9c3a322090c427d118a13e4dd9d1a8a [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 Weil58bb3b32009-12-23 12:12:31 -080016#include "pagelist.h"
Sage Weil31b80062009-10-06 11:31:13 -070017
18/*
19 * Ceph uses the messenger to exchange ceph_msg messages with other
20 * hosts in the system. The messenger provides ordered and reliable
21 * delivery. We tolerate TCP disconnects by reconnecting (with
22 * exponential backoff) in the case of a fault (disconnection, bad
23 * crc, protocol error). Acks allow sent messages to be discarded by
24 * the sender.
25 */
26
27/* static tag bytes (protocol control messages) */
28static char tag_msg = CEPH_MSGR_TAG_MSG;
29static char tag_ack = CEPH_MSGR_TAG_ACK;
30static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
31
32
33static void queue_con(struct ceph_connection *con);
34static void con_work(struct work_struct *);
35static void ceph_fault(struct ceph_connection *con);
36
37const char *ceph_name_type_str(int t)
38{
39 switch (t) {
40 case CEPH_ENTITY_TYPE_MON: return "mon";
41 case CEPH_ENTITY_TYPE_MDS: return "mds";
42 case CEPH_ENTITY_TYPE_OSD: return "osd";
43 case CEPH_ENTITY_TYPE_CLIENT: return "client";
44 case CEPH_ENTITY_TYPE_ADMIN: return "admin";
45 default: return "???";
46 }
47}
48
49/*
50 * nicely render a sockaddr as a string.
51 */
52#define MAX_ADDR_STR 20
53static char addr_str[MAX_ADDR_STR][40];
54static DEFINE_SPINLOCK(addr_str_lock);
55static int last_addr_str;
56
57const char *pr_addr(const struct sockaddr_storage *ss)
58{
59 int i;
60 char *s;
61 struct sockaddr_in *in4 = (void *)ss;
62 unsigned char *quad = (void *)&in4->sin_addr.s_addr;
63 struct sockaddr_in6 *in6 = (void *)ss;
64
65 spin_lock(&addr_str_lock);
66 i = last_addr_str++;
67 if (last_addr_str == MAX_ADDR_STR)
68 last_addr_str = 0;
69 spin_unlock(&addr_str_lock);
70 s = addr_str[i];
71
72 switch (ss->ss_family) {
73 case AF_INET:
74 sprintf(s, "%u.%u.%u.%u:%u",
75 (unsigned int)quad[0],
76 (unsigned int)quad[1],
77 (unsigned int)quad[2],
78 (unsigned int)quad[3],
79 (unsigned int)ntohs(in4->sin_port));
80 break;
81
82 case AF_INET6:
83 sprintf(s, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u",
84 in6->sin6_addr.s6_addr16[0],
85 in6->sin6_addr.s6_addr16[1],
86 in6->sin6_addr.s6_addr16[2],
87 in6->sin6_addr.s6_addr16[3],
88 in6->sin6_addr.s6_addr16[4],
89 in6->sin6_addr.s6_addr16[5],
90 in6->sin6_addr.s6_addr16[6],
91 in6->sin6_addr.s6_addr16[7],
92 (unsigned int)ntohs(in6->sin6_port));
93 break;
94
95 default:
96 sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family);
97 }
98
99 return s;
100}
101
Sage Weil63f2d212009-11-03 15:17:56 -0800102static void encode_my_addr(struct ceph_messenger *msgr)
103{
104 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
105 ceph_encode_addr(&msgr->my_enc_addr);
106}
107
Sage Weil31b80062009-10-06 11:31:13 -0700108/*
109 * work queue for all reading and writing to/from the socket.
110 */
111struct workqueue_struct *ceph_msgr_wq;
112
113int __init ceph_msgr_init(void)
114{
115 ceph_msgr_wq = create_workqueue("ceph-msgr");
116 if (IS_ERR(ceph_msgr_wq)) {
117 int ret = PTR_ERR(ceph_msgr_wq);
118 pr_err("msgr_init failed to create workqueue: %d\n", ret);
119 ceph_msgr_wq = NULL;
120 return ret;
121 }
122 return 0;
123}
124
125void ceph_msgr_exit(void)
126{
127 destroy_workqueue(ceph_msgr_wq);
128}
129
130/*
131 * socket callback functions
132 */
133
134/* data available on socket, or listen socket received a connect */
135static void ceph_data_ready(struct sock *sk, int count_unused)
136{
137 struct ceph_connection *con =
138 (struct ceph_connection *)sk->sk_user_data;
139 if (sk->sk_state != TCP_CLOSE_WAIT) {
140 dout("ceph_data_ready on %p state = %lu, queueing work\n",
141 con, con->state);
142 queue_con(con);
143 }
144}
145
146/* socket has buffer space for writing */
147static void ceph_write_space(struct sock *sk)
148{
149 struct ceph_connection *con =
150 (struct ceph_connection *)sk->sk_user_data;
151
152 /* only queue to workqueue if there is data we want to write. */
153 if (test_bit(WRITE_PENDING, &con->state)) {
154 dout("ceph_write_space %p queueing write work\n", con);
155 queue_con(con);
156 } else {
157 dout("ceph_write_space %p nothing to write\n", con);
158 }
159
160 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
161 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
162}
163
164/* socket's state has changed */
165static void ceph_state_change(struct sock *sk)
166{
167 struct ceph_connection *con =
168 (struct ceph_connection *)sk->sk_user_data;
169
170 dout("ceph_state_change %p state = %lu sk_state = %u\n",
171 con, con->state, sk->sk_state);
172
173 if (test_bit(CLOSED, &con->state))
174 return;
175
176 switch (sk->sk_state) {
177 case TCP_CLOSE:
178 dout("ceph_state_change TCP_CLOSE\n");
179 case TCP_CLOSE_WAIT:
180 dout("ceph_state_change TCP_CLOSE_WAIT\n");
181 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
182 if (test_bit(CONNECTING, &con->state))
183 con->error_msg = "connection failed";
184 else
185 con->error_msg = "socket closed";
186 queue_con(con);
187 }
188 break;
189 case TCP_ESTABLISHED:
190 dout("ceph_state_change TCP_ESTABLISHED\n");
191 queue_con(con);
192 break;
193 }
194}
195
196/*
197 * set up socket callbacks
198 */
199static void set_sock_callbacks(struct socket *sock,
200 struct ceph_connection *con)
201{
202 struct sock *sk = sock->sk;
203 sk->sk_user_data = (void *)con;
204 sk->sk_data_ready = ceph_data_ready;
205 sk->sk_write_space = ceph_write_space;
206 sk->sk_state_change = ceph_state_change;
207}
208
209
210/*
211 * socket helpers
212 */
213
214/*
215 * initiate connection to a remote socket.
216 */
217static struct socket *ceph_tcp_connect(struct ceph_connection *con)
218{
219 struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr;
220 struct socket *sock;
221 int ret;
222
223 BUG_ON(con->sock);
224 ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
225 if (ret)
226 return ERR_PTR(ret);
227 con->sock = sock;
228 sock->sk->sk_allocation = GFP_NOFS;
229
230 set_sock_callbacks(sock, con);
231
232 dout("connect %s\n", pr_addr(&con->peer_addr.in_addr));
233
234 ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK);
235 if (ret == -EINPROGRESS) {
236 dout("connect %s EINPROGRESS sk_state = %u\n",
237 pr_addr(&con->peer_addr.in_addr),
238 sock->sk->sk_state);
239 ret = 0;
240 }
241 if (ret < 0) {
242 pr_err("connect %s error %d\n",
243 pr_addr(&con->peer_addr.in_addr), ret);
244 sock_release(sock);
245 con->sock = NULL;
246 con->error_msg = "connect error";
247 }
248
249 if (ret < 0)
250 return ERR_PTR(ret);
251 return sock;
252}
253
254static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
255{
256 struct kvec iov = {buf, len};
257 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
258
259 return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
260}
261
262/*
263 * write something. @more is true if caller will be sending more data
264 * shortly.
265 */
266static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
267 size_t kvlen, size_t len, int more)
268{
269 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
270
271 if (more)
272 msg.msg_flags |= MSG_MORE;
273 else
274 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
275
276 return kernel_sendmsg(sock, &msg, iov, kvlen, len);
277}
278
279
280/*
281 * Shutdown/close the socket for the given connection.
282 */
283static int con_close_socket(struct ceph_connection *con)
284{
285 int rc;
286
287 dout("con_close_socket on %p sock %p\n", con, con->sock);
288 if (!con->sock)
289 return 0;
290 set_bit(SOCK_CLOSED, &con->state);
291 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
292 sock_release(con->sock);
293 con->sock = NULL;
294 clear_bit(SOCK_CLOSED, &con->state);
295 return rc;
296}
297
298/*
299 * Reset a connection. Discard all incoming and outgoing messages
300 * and clear *_seq state.
301 */
302static void ceph_msg_remove(struct ceph_msg *msg)
303{
304 list_del_init(&msg->list_head);
305 ceph_msg_put(msg);
306}
307static void ceph_msg_remove_list(struct list_head *head)
308{
309 while (!list_empty(head)) {
310 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
311 list_head);
312 ceph_msg_remove(msg);
313 }
314}
315
316static void reset_connection(struct ceph_connection *con)
317{
318 /* reset connection, out_queue, msg_ and connect_seq */
319 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700320 ceph_msg_remove_list(&con->out_queue);
321 ceph_msg_remove_list(&con->out_sent);
322
Sage Weilcf3e5c42009-12-11 09:48:05 -0800323 if (con->in_msg) {
324 ceph_msg_put(con->in_msg);
325 con->in_msg = NULL;
326 }
327
Sage Weil31b80062009-10-06 11:31:13 -0700328 con->connect_seq = 0;
329 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800330 if (con->out_msg) {
331 ceph_msg_put(con->out_msg);
332 con->out_msg = NULL;
333 }
Sage Weil31b80062009-10-06 11:31:13 -0700334 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700335 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700336}
337
338/*
339 * mark a peer down. drop any open connections.
340 */
341void ceph_con_close(struct ceph_connection *con)
342{
343 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
344 set_bit(CLOSED, &con->state); /* in case there's queued work */
345 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Sage Weil1679f872010-02-26 13:55:51 -0800346 clear_bit(LOSSYTX, &con->state); /* so we retry next connect */
347 clear_bit(KEEPALIVE_PENDING, &con->state);
348 clear_bit(WRITE_PENDING, &con->state);
Sage Weilec302642009-12-22 10:43:42 -0800349 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700350 reset_connection(con);
Sage Weil91e45ce2010-02-15 12:05:09 -0800351 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800352 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700353 queue_con(con);
354}
355
356/*
Sage Weil31b80062009-10-06 11:31:13 -0700357 * Reopen a closed connection, with a new peer address.
358 */
359void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
360{
361 dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
362 set_bit(OPENING, &con->state);
363 clear_bit(CLOSED, &con->state);
364 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800365 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700366 queue_con(con);
367}
368
369/*
Sage Weil87b315a2010-03-22 14:51:18 -0700370 * return true if this connection ever successfully opened
371 */
372bool ceph_con_opened(struct ceph_connection *con)
373{
374 return con->connect_seq > 0;
375}
376
377/*
Sage Weil31b80062009-10-06 11:31:13 -0700378 * generic get/put
379 */
380struct ceph_connection *ceph_con_get(struct ceph_connection *con)
381{
382 dout("con_get %p nref = %d -> %d\n", con,
383 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
384 if (atomic_inc_not_zero(&con->nref))
385 return con;
386 return NULL;
387}
388
389void ceph_con_put(struct ceph_connection *con)
390{
391 dout("con_put %p nref = %d -> %d\n", con,
392 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
393 BUG_ON(atomic_read(&con->nref) == 0);
394 if (atomic_dec_and_test(&con->nref)) {
Sage Weil71ececd2009-11-18 11:27:06 -0800395 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700396 kfree(con);
397 }
398}
399
400/*
401 * initialize a new connection.
402 */
403void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
404{
405 dout("con_init %p\n", con);
406 memset(con, 0, sizeof(*con));
407 atomic_set(&con->nref, 1);
408 con->msgr = msgr;
Sage Weilec302642009-12-22 10:43:42 -0800409 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700410 INIT_LIST_HEAD(&con->out_queue);
411 INIT_LIST_HEAD(&con->out_sent);
412 INIT_DELAYED_WORK(&con->work, con_work);
413}
414
415
416/*
417 * We maintain a global counter to order connection attempts. Get
418 * a unique seq greater than @gt.
419 */
420static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
421{
422 u32 ret;
423
424 spin_lock(&msgr->global_seq_lock);
425 if (msgr->global_seq < gt)
426 msgr->global_seq = gt;
427 ret = ++msgr->global_seq;
428 spin_unlock(&msgr->global_seq_lock);
429 return ret;
430}
431
432
433/*
434 * Prepare footer for currently outgoing message, and finish things
435 * off. Assumes out_kvec* are already valid.. we just add on to the end.
436 */
437static void prepare_write_message_footer(struct ceph_connection *con, int v)
438{
439 struct ceph_msg *m = con->out_msg;
440
441 dout("prepare_write_message_footer %p\n", con);
442 con->out_kvec_is_msg = true;
443 con->out_kvec[v].iov_base = &m->footer;
444 con->out_kvec[v].iov_len = sizeof(m->footer);
445 con->out_kvec_bytes += sizeof(m->footer);
446 con->out_kvec_left++;
447 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800448 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700449}
450
451/*
452 * Prepare headers for the next outgoing message.
453 */
454static void prepare_write_message(struct ceph_connection *con)
455{
456 struct ceph_msg *m;
457 int v = 0;
458
459 con->out_kvec_bytes = 0;
460 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800461 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700462
463 /* Sneak an ack in there first? If we can get it into the same
464 * TCP packet that's a good thing. */
465 if (con->in_seq > con->in_seq_acked) {
466 con->in_seq_acked = con->in_seq;
467 con->out_kvec[v].iov_base = &tag_ack;
468 con->out_kvec[v++].iov_len = 1;
469 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
470 con->out_kvec[v].iov_base = &con->out_temp_ack;
471 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
472 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
473 }
474
Sage Weil31b80062009-10-06 11:31:13 -0700475 m = list_first_entry(&con->out_queue,
476 struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800477 con->out_msg = m;
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800478 if (test_bit(LOSSYTX, &con->state)) {
Sage Weil6c5d1a42010-02-13 20:29:31 -0800479 list_del_init(&m->list_head);
480 } else {
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800481 /* put message on sent list */
482 ceph_msg_get(m);
483 list_move_tail(&m->list_head, &con->out_sent);
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800484 }
Sage Weil31b80062009-10-06 11:31:13 -0700485
486 m->hdr.seq = cpu_to_le64(++con->out_seq);
487
488 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
489 m, con->out_seq, le16_to_cpu(m->hdr.type),
490 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
491 le32_to_cpu(m->hdr.data_len),
492 m->nr_pages);
493 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
494
495 /* tag + hdr + front + middle */
496 con->out_kvec[v].iov_base = &tag_msg;
497 con->out_kvec[v++].iov_len = 1;
498 con->out_kvec[v].iov_base = &m->hdr;
499 con->out_kvec[v++].iov_len = sizeof(m->hdr);
500 con->out_kvec[v++] = m->front;
501 if (m->middle)
502 con->out_kvec[v++] = m->middle->vec;
503 con->out_kvec_left = v;
504 con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
505 (m->middle ? m->middle->vec.iov_len : 0);
506 con->out_kvec_cur = con->out_kvec;
507
508 /* fill in crc (except data pages), footer */
509 con->out_msg->hdr.crc =
510 cpu_to_le32(crc32c(0, (void *)&m->hdr,
511 sizeof(m->hdr) - sizeof(m->hdr.crc)));
512 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
513 con->out_msg->footer.front_crc =
514 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
515 if (m->middle)
516 con->out_msg->footer.middle_crc =
517 cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
518 m->middle->vec.iov_len));
519 else
520 con->out_msg->footer.middle_crc = 0;
521 con->out_msg->footer.data_crc = 0;
522 dout("prepare_write_message front_crc %u data_crc %u\n",
523 le32_to_cpu(con->out_msg->footer.front_crc),
524 le32_to_cpu(con->out_msg->footer.middle_crc));
525
526 /* is there a data payload? */
527 if (le32_to_cpu(m->hdr.data_len) > 0) {
528 /* initialize page iterator */
529 con->out_msg_pos.page = 0;
530 con->out_msg_pos.page_pos =
531 le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
532 con->out_msg_pos.data_pos = 0;
533 con->out_msg_pos.did_page_crc = 0;
534 con->out_more = 1; /* data + footer will follow */
535 } else {
536 /* no, queue up footer too and be done */
537 prepare_write_message_footer(con, v);
538 }
539
540 set_bit(WRITE_PENDING, &con->state);
541}
542
543/*
544 * Prepare an ack.
545 */
546static void prepare_write_ack(struct ceph_connection *con)
547{
548 dout("prepare_write_ack %p %llu -> %llu\n", con,
549 con->in_seq_acked, con->in_seq);
550 con->in_seq_acked = con->in_seq;
551
552 con->out_kvec[0].iov_base = &tag_ack;
553 con->out_kvec[0].iov_len = 1;
554 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
555 con->out_kvec[1].iov_base = &con->out_temp_ack;
556 con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
557 con->out_kvec_left = 2;
558 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
559 con->out_kvec_cur = con->out_kvec;
560 con->out_more = 1; /* more will follow.. eventually.. */
561 set_bit(WRITE_PENDING, &con->state);
562}
563
564/*
565 * Prepare to write keepalive byte.
566 */
567static void prepare_write_keepalive(struct ceph_connection *con)
568{
569 dout("prepare_write_keepalive %p\n", con);
570 con->out_kvec[0].iov_base = &tag_keepalive;
571 con->out_kvec[0].iov_len = 1;
572 con->out_kvec_left = 1;
573 con->out_kvec_bytes = 1;
574 con->out_kvec_cur = con->out_kvec;
575 set_bit(WRITE_PENDING, &con->state);
576}
577
578/*
579 * Connection negotiation.
580 */
581
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800582static void prepare_connect_authorizer(struct ceph_connection *con)
583{
584 void *auth_buf;
585 int auth_len = 0;
586 int auth_protocol = 0;
587
Sage Weilec302642009-12-22 10:43:42 -0800588 mutex_unlock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800589 if (con->ops->get_authorizer)
590 con->ops->get_authorizer(con, &auth_buf, &auth_len,
591 &auth_protocol, &con->auth_reply_buf,
592 &con->auth_reply_buf_len,
593 con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800594 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800595
596 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
597 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
598
599 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
600 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
601 con->out_kvec_left++;
602 con->out_kvec_bytes += auth_len;
603}
604
Sage Weil31b80062009-10-06 11:31:13 -0700605/*
606 * We connected to a peer and are saying hello.
607 */
Sage Weileed0ef22009-11-10 14:34:36 -0800608static void prepare_write_banner(struct ceph_messenger *msgr,
609 struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700610{
611 int len = strlen(CEPH_BANNER);
Sage Weileed0ef22009-11-10 14:34:36 -0800612
613 con->out_kvec[0].iov_base = CEPH_BANNER;
614 con->out_kvec[0].iov_len = len;
615 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
616 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
617 con->out_kvec_left = 2;
618 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
619 con->out_kvec_cur = con->out_kvec;
620 con->out_more = 0;
621 set_bit(WRITE_PENDING, &con->state);
622}
623
624static void prepare_write_connect(struct ceph_messenger *msgr,
625 struct ceph_connection *con,
626 int after_banner)
627{
Sage Weil31b80062009-10-06 11:31:13 -0700628 unsigned global_seq = get_global_seq(con->msgr, 0);
629 int proto;
630
631 switch (con->peer_name.type) {
632 case CEPH_ENTITY_TYPE_MON:
633 proto = CEPH_MONC_PROTOCOL;
634 break;
635 case CEPH_ENTITY_TYPE_OSD:
636 proto = CEPH_OSDC_PROTOCOL;
637 break;
638 case CEPH_ENTITY_TYPE_MDS:
639 proto = CEPH_MDSC_PROTOCOL;
640 break;
641 default:
642 BUG();
643 }
644
645 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
646 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800647
Sage Weil04a419f2009-12-23 09:30:21 -0800648 con->out_connect.features = CEPH_FEATURE_SUPPORTED;
Sage Weil31b80062009-10-06 11:31:13 -0700649 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
650 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
651 con->out_connect.global_seq = cpu_to_le32(global_seq);
652 con->out_connect.protocol_version = cpu_to_le32(proto);
653 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700654
Sage Weileed0ef22009-11-10 14:34:36 -0800655 if (!after_banner) {
656 con->out_kvec_left = 0;
657 con->out_kvec_bytes = 0;
658 }
659 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
660 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
661 con->out_kvec_left++;
662 con->out_kvec_bytes += sizeof(con->out_connect);
Sage Weil31b80062009-10-06 11:31:13 -0700663 con->out_kvec_cur = con->out_kvec;
664 con->out_more = 0;
665 set_bit(WRITE_PENDING, &con->state);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800666
667 prepare_connect_authorizer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700668}
669
670
671/*
672 * write as much of pending kvecs to the socket as we can.
673 * 1 -> done
674 * 0 -> socket full, but more to do
675 * <0 -> error
676 */
677static int write_partial_kvec(struct ceph_connection *con)
678{
679 int ret;
680
681 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
682 while (con->out_kvec_bytes > 0) {
683 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
684 con->out_kvec_left, con->out_kvec_bytes,
685 con->out_more);
686 if (ret <= 0)
687 goto out;
688 con->out_kvec_bytes -= ret;
689 if (con->out_kvec_bytes == 0)
690 break; /* done */
691 while (ret > 0) {
692 if (ret >= con->out_kvec_cur->iov_len) {
693 ret -= con->out_kvec_cur->iov_len;
694 con->out_kvec_cur++;
695 con->out_kvec_left--;
696 } else {
697 con->out_kvec_cur->iov_len -= ret;
698 con->out_kvec_cur->iov_base += ret;
699 ret = 0;
700 break;
701 }
702 }
703 }
704 con->out_kvec_left = 0;
705 con->out_kvec_is_msg = false;
706 ret = 1;
707out:
708 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
709 con->out_kvec_bytes, con->out_kvec_left, ret);
710 return ret; /* done! */
711}
712
713/*
714 * Write as much message data payload as we can. If we finish, queue
715 * up the footer.
716 * 1 -> done, footer is now queued in out_kvec[].
717 * 0 -> socket full, but more to do
718 * <0 -> error
719 */
720static int write_partial_msg_pages(struct ceph_connection *con)
721{
722 struct ceph_msg *msg = con->out_msg;
723 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
724 size_t len;
725 int crc = con->msgr->nocrc;
726 int ret;
727
728 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
729 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
730 con->out_msg_pos.page_pos);
731
732 while (con->out_msg_pos.page < con->out_msg->nr_pages) {
733 struct page *page = NULL;
734 void *kaddr = NULL;
735
736 /*
737 * if we are calculating the data crc (the default), we need
738 * to map the page. if our pages[] has been revoked, use the
739 * zero page.
740 */
741 if (msg->pages) {
742 page = msg->pages[con->out_msg_pos.page];
743 if (crc)
744 kaddr = kmap(page);
Sage Weil58bb3b32009-12-23 12:12:31 -0800745 } else if (msg->pagelist) {
746 page = list_first_entry(&msg->pagelist->head,
747 struct page, lru);
748 if (crc)
749 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700750 } else {
751 page = con->msgr->zero_page;
752 if (crc)
753 kaddr = page_address(con->msgr->zero_page);
754 }
755 len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
756 (int)(data_len - con->out_msg_pos.data_pos));
757 if (crc && !con->out_msg_pos.did_page_crc) {
758 void *base = kaddr + con->out_msg_pos.page_pos;
759 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
760
761 BUG_ON(kaddr == NULL);
762 con->out_msg->footer.data_crc =
763 cpu_to_le32(crc32c(tmpcrc, base, len));
764 con->out_msg_pos.did_page_crc = 1;
765 }
766
767 ret = kernel_sendpage(con->sock, page,
768 con->out_msg_pos.page_pos, len,
769 MSG_DONTWAIT | MSG_NOSIGNAL |
770 MSG_MORE);
771
Sage Weil58bb3b32009-12-23 12:12:31 -0800772 if (crc && (msg->pages || msg->pagelist))
Sage Weil31b80062009-10-06 11:31:13 -0700773 kunmap(page);
774
775 if (ret <= 0)
776 goto out;
777
778 con->out_msg_pos.data_pos += ret;
779 con->out_msg_pos.page_pos += ret;
780 if (ret == len) {
781 con->out_msg_pos.page_pos = 0;
782 con->out_msg_pos.page++;
783 con->out_msg_pos.did_page_crc = 0;
Sage Weil58bb3b32009-12-23 12:12:31 -0800784 if (msg->pagelist)
785 list_move_tail(&page->lru,
786 &msg->pagelist->head);
Sage Weil31b80062009-10-06 11:31:13 -0700787 }
788 }
789
790 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
791
792 /* prepare and queue up footer, too */
793 if (!crc)
794 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
795 con->out_kvec_bytes = 0;
796 con->out_kvec_left = 0;
797 con->out_kvec_cur = con->out_kvec;
798 prepare_write_message_footer(con, 0);
799 ret = 1;
800out:
801 return ret;
802}
803
804/*
805 * write some zeros
806 */
807static int write_partial_skip(struct ceph_connection *con)
808{
809 int ret;
810
811 while (con->out_skip > 0) {
812 struct kvec iov = {
813 .iov_base = page_address(con->msgr->zero_page),
814 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
815 };
816
817 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
818 if (ret <= 0)
819 goto out;
820 con->out_skip -= ret;
821 }
822 ret = 1;
823out:
824 return ret;
825}
826
827/*
828 * Prepare to read connection handshake, or an ack.
829 */
Sage Weileed0ef22009-11-10 14:34:36 -0800830static void prepare_read_banner(struct ceph_connection *con)
831{
832 dout("prepare_read_banner %p\n", con);
833 con->in_base_pos = 0;
834}
835
Sage Weil31b80062009-10-06 11:31:13 -0700836static void prepare_read_connect(struct ceph_connection *con)
837{
838 dout("prepare_read_connect %p\n", con);
839 con->in_base_pos = 0;
840}
841
842static void prepare_read_ack(struct ceph_connection *con)
843{
844 dout("prepare_read_ack %p\n", con);
845 con->in_base_pos = 0;
846}
847
848static void prepare_read_tag(struct ceph_connection *con)
849{
850 dout("prepare_read_tag %p\n", con);
851 con->in_base_pos = 0;
852 con->in_tag = CEPH_MSGR_TAG_READY;
853}
854
855/*
856 * Prepare to read a message.
857 */
858static int prepare_read_message(struct ceph_connection *con)
859{
860 dout("prepare_read_message %p\n", con);
861 BUG_ON(con->in_msg != NULL);
862 con->in_base_pos = 0;
863 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
864 return 0;
865}
866
867
868static int read_partial(struct ceph_connection *con,
869 int *to, int size, void *object)
870{
871 *to += size;
872 while (con->in_base_pos < *to) {
873 int left = *to - con->in_base_pos;
874 int have = size - left;
875 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
876 if (ret <= 0)
877 return ret;
878 con->in_base_pos += ret;
879 }
880 return 1;
881}
882
883
884/*
885 * Read all or part of the connect-side handshake on a new connection
886 */
Sage Weileed0ef22009-11-10 14:34:36 -0800887static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700888{
889 int ret, to = 0;
890
Sage Weileed0ef22009-11-10 14:34:36 -0800891 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -0700892
893 /* peer's banner */
894 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
895 if (ret <= 0)
896 goto out;
897 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
898 &con->actual_peer_addr);
899 if (ret <= 0)
900 goto out;
901 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
902 &con->peer_addr_for_me);
903 if (ret <= 0)
904 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -0800905out:
906 return ret;
907}
908
909static int read_partial_connect(struct ceph_connection *con)
910{
911 int ret, to = 0;
912
913 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
914
Sage Weil31b80062009-10-06 11:31:13 -0700915 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
916 if (ret <= 0)
917 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800918 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
919 con->auth_reply_buf);
920 if (ret <= 0)
921 goto out;
Sage Weil31b80062009-10-06 11:31:13 -0700922
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800923 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
924 con, (int)con->in_reply.tag,
925 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -0700926 le32_to_cpu(con->in_reply.global_seq));
927out:
928 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -0800929
Sage Weil31b80062009-10-06 11:31:13 -0700930}
931
932/*
933 * Verify the hello banner looks okay.
934 */
935static int verify_hello(struct ceph_connection *con)
936{
937 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -0700938 pr_err("connect to %s got bad banner\n",
Sage Weil31b80062009-10-06 11:31:13 -0700939 pr_addr(&con->peer_addr.in_addr));
940 con->error_msg = "protocol error, bad banner";
941 return -1;
942 }
943 return 0;
944}
945
946static bool addr_is_blank(struct sockaddr_storage *ss)
947{
948 switch (ss->ss_family) {
949 case AF_INET:
950 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
951 case AF_INET6:
952 return
953 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
954 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
955 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
956 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
957 }
958 return false;
959}
960
961static int addr_port(struct sockaddr_storage *ss)
962{
963 switch (ss->ss_family) {
964 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800965 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -0700966 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800967 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -0700968 }
969 return 0;
970}
971
972static void addr_set_port(struct sockaddr_storage *ss, int p)
973{
974 switch (ss->ss_family) {
975 case AF_INET:
976 ((struct sockaddr_in *)ss)->sin_port = htons(p);
977 case AF_INET6:
978 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
979 }
980}
981
982/*
983 * Parse an ip[:port] list into an addr array. Use the default
984 * monitor port if a port isn't specified.
985 */
986int ceph_parse_ips(const char *c, const char *end,
987 struct ceph_entity_addr *addr,
988 int max_count, int *count)
989{
990 int i;
991 const char *p = c;
992
993 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
994 for (i = 0; i < max_count; i++) {
995 const char *ipend;
996 struct sockaddr_storage *ss = &addr[i].in_addr;
997 struct sockaddr_in *in4 = (void *)ss;
998 struct sockaddr_in6 *in6 = (void *)ss;
999 int port;
1000
1001 memset(ss, 0, sizeof(*ss));
1002 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
1003 ',', &ipend)) {
1004 ss->ss_family = AF_INET;
1005 } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
1006 ',', &ipend)) {
1007 ss->ss_family = AF_INET6;
1008 } else {
1009 goto bad;
1010 }
1011 p = ipend;
1012
1013 /* port? */
1014 if (p < end && *p == ':') {
1015 port = 0;
1016 p++;
1017 while (p < end && *p >= '0' && *p <= '9') {
1018 port = (port * 10) + (*p - '0');
1019 p++;
1020 }
1021 if (port > 65535 || port == 0)
1022 goto bad;
1023 } else {
1024 port = CEPH_MON_PORT;
1025 }
1026
1027 addr_set_port(ss, port);
1028
1029 dout("parse_ips got %s\n", pr_addr(ss));
1030
1031 if (p == end)
1032 break;
1033 if (*p != ',')
1034 goto bad;
1035 p++;
1036 }
1037
1038 if (p != end)
1039 goto bad;
1040
1041 if (count)
1042 *count = i + 1;
1043 return 0;
1044
1045bad:
1046 pr_err("parse_ips bad ip '%s'\n", c);
1047 return -EINVAL;
1048}
1049
Sage Weileed0ef22009-11-10 14:34:36 -08001050static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001051{
Sage Weileed0ef22009-11-10 14:34:36 -08001052 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001053
1054 if (verify_hello(con) < 0)
1055 return -1;
1056
Sage Weil63f2d212009-11-03 15:17:56 -08001057 ceph_decode_addr(&con->actual_peer_addr);
1058 ceph_decode_addr(&con->peer_addr_for_me);
1059
Sage Weil31b80062009-10-06 11:31:13 -07001060 /*
1061 * Make sure the other end is who we wanted. note that the other
1062 * end may not yet know their ip address, so if it's 0.0.0.0, give
1063 * them the benefit of the doubt.
1064 */
Sage Weil103e2d32010-01-07 16:12:36 -08001065 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1066 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001067 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1068 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Sage Weil103e2d32010-01-07 16:12:36 -08001069 pr_warning("wrong peer, want %s/%lld, got %s/%lld\n",
1070 pr_addr(&con->peer_addr.in_addr),
1071 le64_to_cpu(con->peer_addr.nonce),
1072 pr_addr(&con->actual_peer_addr.in_addr),
1073 le64_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001074 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001075 return -1;
1076 }
1077
1078 /*
1079 * did we learn our address?
1080 */
1081 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1082 int port = addr_port(&con->msgr->inst.addr.in_addr);
1083
1084 memcpy(&con->msgr->inst.addr.in_addr,
1085 &con->peer_addr_for_me.in_addr,
1086 sizeof(con->peer_addr_for_me.in_addr));
1087 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001088 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001089 dout("process_banner learned my addr is %s\n",
Sage Weil31b80062009-10-06 11:31:13 -07001090 pr_addr(&con->msgr->inst.addr.in_addr));
1091 }
1092
Sage Weileed0ef22009-11-10 14:34:36 -08001093 set_bit(NEGOTIATING, &con->state);
1094 prepare_read_connect(con);
1095 return 0;
1096}
1097
Sage Weil04a419f2009-12-23 09:30:21 -08001098static void fail_protocol(struct ceph_connection *con)
1099{
1100 reset_connection(con);
1101 set_bit(CLOSED, &con->state); /* in case there's queued work */
1102
1103 mutex_unlock(&con->mutex);
1104 if (con->ops->bad_proto)
1105 con->ops->bad_proto(con);
1106 mutex_lock(&con->mutex);
1107}
1108
Sage Weileed0ef22009-11-10 14:34:36 -08001109static int process_connect(struct ceph_connection *con)
1110{
Sage Weil04a419f2009-12-23 09:30:21 -08001111 u64 sup_feat = CEPH_FEATURE_SUPPORTED;
1112 u64 req_feat = CEPH_FEATURE_REQUIRED;
1113 u64 server_feat = le64_to_cpu(con->in_reply.features);
1114
Sage Weileed0ef22009-11-10 14:34:36 -08001115 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1116
Sage Weil31b80062009-10-06 11:31:13 -07001117 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001118 case CEPH_MSGR_TAG_FEATURES:
1119 pr_err("%s%lld %s feature set mismatch,"
1120 " my %llx < server's %llx, missing %llx\n",
1121 ENTITY_NAME(con->peer_name),
1122 pr_addr(&con->peer_addr.in_addr),
1123 sup_feat, server_feat, server_feat & ~sup_feat);
1124 con->error_msg = "missing required protocol features";
1125 fail_protocol(con);
1126 return -1;
1127
Sage Weil31b80062009-10-06 11:31:13 -07001128 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001129 pr_err("%s%lld %s protocol version mismatch,"
1130 " my %d != server's %d\n",
1131 ENTITY_NAME(con->peer_name),
1132 pr_addr(&con->peer_addr.in_addr),
1133 le32_to_cpu(con->out_connect.protocol_version),
1134 le32_to_cpu(con->in_reply.protocol_version));
1135 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001136 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001137 return -1;
1138
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001139 case CEPH_MSGR_TAG_BADAUTHORIZER:
1140 con->auth_retry++;
1141 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1142 con->auth_retry);
1143 if (con->auth_retry == 2) {
1144 con->error_msg = "connect authorization failure";
1145 reset_connection(con);
1146 set_bit(CLOSED, &con->state);
1147 return -1;
1148 }
1149 con->auth_retry = 1;
1150 prepare_write_connect(con->msgr, con, 0);
Sage Weil63733a02010-03-15 15:47:22 -07001151 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001152 break;
Sage Weil31b80062009-10-06 11:31:13 -07001153
1154 case CEPH_MSGR_TAG_RESETSESSION:
1155 /*
1156 * If we connected with a large connect_seq but the peer
1157 * has no record of a session with us (no connection, or
1158 * connect_seq == 0), they will send RESETSESION to indicate
1159 * that they must have reset their session, and may have
1160 * dropped messages.
1161 */
1162 dout("process_connect got RESET peer seq %u\n",
1163 le32_to_cpu(con->in_connect.connect_seq));
1164 pr_err("%s%lld %s connection reset\n",
1165 ENTITY_NAME(con->peer_name),
1166 pr_addr(&con->peer_addr.in_addr));
1167 reset_connection(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001168 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001169 prepare_read_connect(con);
1170
1171 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001172 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001173 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1174 if (con->ops->peer_reset)
1175 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001176 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001177 break;
1178
1179 case CEPH_MSGR_TAG_RETRY_SESSION:
1180 /*
1181 * If we sent a smaller connect_seq than the peer has, try
1182 * again with a larger value.
1183 */
1184 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1185 le32_to_cpu(con->out_connect.connect_seq),
1186 le32_to_cpu(con->in_connect.connect_seq));
1187 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Sage Weileed0ef22009-11-10 14:34:36 -08001188 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001189 prepare_read_connect(con);
1190 break;
1191
1192 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1193 /*
1194 * If we sent a smaller global_seq than the peer has, try
1195 * again with a larger value.
1196 */
Sage Weileed0ef22009-11-10 14:34:36 -08001197 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001198 con->peer_global_seq,
1199 le32_to_cpu(con->in_connect.global_seq));
1200 get_global_seq(con->msgr,
1201 le32_to_cpu(con->in_connect.global_seq));
Sage Weileed0ef22009-11-10 14:34:36 -08001202 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001203 prepare_read_connect(con);
1204 break;
1205
1206 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001207 if (req_feat & ~server_feat) {
1208 pr_err("%s%lld %s protocol feature mismatch,"
1209 " my required %llx > server's %llx, need %llx\n",
1210 ENTITY_NAME(con->peer_name),
1211 pr_addr(&con->peer_addr.in_addr),
1212 req_feat, server_feat, req_feat & ~server_feat);
1213 con->error_msg = "missing required protocol features";
1214 fail_protocol(con);
1215 return -1;
1216 }
Sage Weil31b80062009-10-06 11:31:13 -07001217 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001218 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1219 con->connect_seq++;
1220 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1221 con->peer_global_seq,
1222 le32_to_cpu(con->in_reply.connect_seq),
1223 con->connect_seq);
1224 WARN_ON(con->connect_seq !=
1225 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001226
1227 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1228 set_bit(LOSSYTX, &con->state);
1229
Sage Weil31b80062009-10-06 11:31:13 -07001230 prepare_read_tag(con);
1231 break;
1232
1233 case CEPH_MSGR_TAG_WAIT:
1234 /*
1235 * If there is a connection race (we are opening
1236 * connections to each other), one of us may just have
1237 * to WAIT. This shouldn't happen if we are the
1238 * client.
1239 */
1240 pr_err("process_connect peer connecting WAIT\n");
1241
1242 default:
1243 pr_err("connect protocol error, will retry\n");
1244 con->error_msg = "protocol error, garbage tag during connect";
1245 return -1;
1246 }
1247 return 0;
1248}
1249
1250
1251/*
1252 * read (part of) an ack
1253 */
1254static int read_partial_ack(struct ceph_connection *con)
1255{
1256 int to = 0;
1257
1258 return read_partial(con, &to, sizeof(con->in_temp_ack),
1259 &con->in_temp_ack);
1260}
1261
1262
1263/*
1264 * We can finally discard anything that's been acked.
1265 */
1266static void process_ack(struct ceph_connection *con)
1267{
1268 struct ceph_msg *m;
1269 u64 ack = le64_to_cpu(con->in_temp_ack);
1270 u64 seq;
1271
Sage Weil31b80062009-10-06 11:31:13 -07001272 while (!list_empty(&con->out_sent)) {
1273 m = list_first_entry(&con->out_sent, struct ceph_msg,
1274 list_head);
1275 seq = le64_to_cpu(m->hdr.seq);
1276 if (seq > ack)
1277 break;
1278 dout("got ack for seq %llu type %d at %p\n", seq,
1279 le16_to_cpu(m->hdr.type), m);
1280 ceph_msg_remove(m);
1281 }
Sage Weil31b80062009-10-06 11:31:13 -07001282 prepare_read_tag(con);
1283}
1284
1285
1286
1287
Yehuda Sadeh24504182010-01-08 13:58:34 -08001288static int read_partial_message_section(struct ceph_connection *con,
1289 struct kvec *section, unsigned int sec_len,
1290 u32 *crc)
1291{
1292 int left;
1293 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001294
Yehuda Sadeh24504182010-01-08 13:58:34 -08001295 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001296
Yehuda Sadeh24504182010-01-08 13:58:34 -08001297 while (section->iov_len < sec_len) {
1298 BUG_ON(section->iov_base == NULL);
1299 left = sec_len - section->iov_len;
1300 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1301 section->iov_len, left);
1302 if (ret <= 0)
1303 return ret;
1304 section->iov_len += ret;
1305 if (section->iov_len == sec_len)
1306 *crc = crc32c(0, section->iov_base,
1307 section->iov_len);
1308 }
1309
1310 return 1;
1311}
1312
1313static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1314 struct ceph_msg_header *hdr,
1315 int *skip);
Sage Weil31b80062009-10-06 11:31:13 -07001316/*
1317 * read (part of) a message.
1318 */
1319static int read_partial_message(struct ceph_connection *con)
1320{
1321 struct ceph_msg *m = con->in_msg;
1322 void *p;
1323 int ret;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001324 int to, left;
Sage Weil31b80062009-10-06 11:31:13 -07001325 unsigned front_len, middle_len, data_len, data_off;
1326 int datacrc = con->msgr->nocrc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001327 int skip;
Sage Weil31b80062009-10-06 11:31:13 -07001328
1329 dout("read_partial_message con %p msg %p\n", con, m);
1330
1331 /* header */
1332 while (con->in_base_pos < sizeof(con->in_hdr)) {
1333 left = sizeof(con->in_hdr) - con->in_base_pos;
1334 ret = ceph_tcp_recvmsg(con->sock,
1335 (char *)&con->in_hdr + con->in_base_pos,
1336 left);
1337 if (ret <= 0)
1338 return ret;
1339 con->in_base_pos += ret;
1340 if (con->in_base_pos == sizeof(con->in_hdr)) {
1341 u32 crc = crc32c(0, (void *)&con->in_hdr,
1342 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1343 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1344 pr_err("read_partial_message bad hdr "
1345 " crc %u != expected %u\n",
1346 crc, con->in_hdr.crc);
1347 return -EBADMSG;
1348 }
1349 }
1350 }
Sage Weil31b80062009-10-06 11:31:13 -07001351 front_len = le32_to_cpu(con->in_hdr.front_len);
1352 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1353 return -EIO;
1354 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1355 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1356 return -EIO;
1357 data_len = le32_to_cpu(con->in_hdr.data_len);
1358 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1359 return -EIO;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001360 data_off = le16_to_cpu(con->in_hdr.data_off);
Sage Weil31b80062009-10-06 11:31:13 -07001361
1362 /* allocate message? */
1363 if (!con->in_msg) {
1364 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1365 con->in_hdr.front_len, con->in_hdr.data_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001366 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1367 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07001368 /* skip this message */
Sage Weil5b3a4db2010-02-19 21:43:23 -08001369 dout("alloc_msg returned NULL, skipping message\n");
Sage Weil31b80062009-10-06 11:31:13 -07001370 con->in_base_pos = -front_len - middle_len - data_len -
1371 sizeof(m->footer);
1372 con->in_tag = CEPH_MSGR_TAG_READY;
1373 return 0;
1374 }
1375 if (IS_ERR(con->in_msg)) {
1376 ret = PTR_ERR(con->in_msg);
1377 con->in_msg = NULL;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001378 con->error_msg =
1379 "error allocating memory for incoming message";
Sage Weil31b80062009-10-06 11:31:13 -07001380 return ret;
1381 }
1382 m = con->in_msg;
1383 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001384 if (m->middle)
1385 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001386
1387 con->in_msg_pos.page = 0;
1388 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1389 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001390 }
1391
1392 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001393 ret = read_partial_message_section(con, &m->front, front_len,
1394 &con->in_front_crc);
1395 if (ret <= 0)
1396 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001397
1398 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001399 if (m->middle) {
1400 ret = read_partial_message_section(con, &m->middle->vec, middle_len,
1401 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001402 if (ret <= 0)
1403 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001404 }
1405
1406 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001407 while (con->in_msg_pos.data_pos < data_len) {
1408 left = min((int)(data_len - con->in_msg_pos.data_pos),
1409 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1410 BUG_ON(m->pages == NULL);
1411 p = kmap(m->pages[con->in_msg_pos.page]);
1412 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1413 left);
1414 if (ret > 0 && datacrc)
1415 con->in_data_crc =
1416 crc32c(con->in_data_crc,
1417 p + con->in_msg_pos.page_pos, ret);
1418 kunmap(m->pages[con->in_msg_pos.page]);
1419 if (ret <= 0)
1420 return ret;
1421 con->in_msg_pos.data_pos += ret;
1422 con->in_msg_pos.page_pos += ret;
1423 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1424 con->in_msg_pos.page_pos = 0;
1425 con->in_msg_pos.page++;
1426 }
1427 }
1428
Sage Weil31b80062009-10-06 11:31:13 -07001429 /* footer */
1430 to = sizeof(m->hdr) + sizeof(m->footer);
1431 while (con->in_base_pos < to) {
1432 left = to - con->in_base_pos;
1433 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1434 (con->in_base_pos - sizeof(m->hdr)),
1435 left);
1436 if (ret <= 0)
1437 return ret;
1438 con->in_base_pos += ret;
1439 }
1440 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1441 m, front_len, m->footer.front_crc, middle_len,
1442 m->footer.middle_crc, data_len, m->footer.data_crc);
1443
1444 /* crc ok? */
1445 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1446 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1447 m, con->in_front_crc, m->footer.front_crc);
1448 return -EBADMSG;
1449 }
1450 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1451 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1452 m, con->in_middle_crc, m->footer.middle_crc);
1453 return -EBADMSG;
1454 }
1455 if (datacrc &&
1456 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1457 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1458 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1459 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1460 return -EBADMSG;
1461 }
1462
1463 return 1; /* done! */
1464}
1465
1466/*
1467 * Process message. This happens in the worker thread. The callback should
1468 * be careful not to do anything that waits on other incoming messages or it
1469 * may deadlock.
1470 */
1471static void process_message(struct ceph_connection *con)
1472{
Sage Weil5e095e82009-12-14 14:30:34 -08001473 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001474
Sage Weil5e095e82009-12-14 14:30:34 -08001475 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001476 con->in_msg = NULL;
1477
1478 /* if first message, set peer_name */
1479 if (con->peer_name.type == 0)
1480 con->peer_name = msg->hdr.src.name;
1481
Sage Weil31b80062009-10-06 11:31:13 -07001482 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001483 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001484
1485 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1486 msg, le64_to_cpu(msg->hdr.seq),
1487 ENTITY_NAME(msg->hdr.src.name),
1488 le16_to_cpu(msg->hdr.type),
1489 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1490 le32_to_cpu(msg->hdr.front_len),
1491 le32_to_cpu(msg->hdr.data_len),
1492 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1493 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001494
1495 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001496 prepare_read_tag(con);
1497}
1498
1499
1500/*
1501 * Write something to the socket. Called in a worker thread when the
1502 * socket appears to be writeable and we have something ready to send.
1503 */
1504static int try_write(struct ceph_connection *con)
1505{
1506 struct ceph_messenger *msgr = con->msgr;
1507 int ret = 1;
1508
1509 dout("try_write start %p state %lu nref %d\n", con, con->state,
1510 atomic_read(&con->nref));
1511
Sage Weilec302642009-12-22 10:43:42 -08001512 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001513more:
1514 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1515
1516 /* open the socket first? */
1517 if (con->sock == NULL) {
1518 /*
1519 * if we were STANDBY and are reconnecting _this_
1520 * connection, bump connect_seq now. Always bump
1521 * global_seq.
1522 */
1523 if (test_and_clear_bit(STANDBY, &con->state))
1524 con->connect_seq++;
1525
Sage Weileed0ef22009-11-10 14:34:36 -08001526 prepare_write_banner(msgr, con);
1527 prepare_write_connect(msgr, con, 1);
1528 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001529 set_bit(CONNECTING, &con->state);
Sage Weileed0ef22009-11-10 14:34:36 -08001530 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001531
Sage Weilcf3e5c42009-12-11 09:48:05 -08001532 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001533 con->in_tag = CEPH_MSGR_TAG_READY;
1534 dout("try_write initiating connect on %p new state %lu\n",
1535 con, con->state);
1536 con->sock = ceph_tcp_connect(con);
1537 if (IS_ERR(con->sock)) {
1538 con->sock = NULL;
1539 con->error_msg = "connect error";
1540 ret = -1;
1541 goto out;
1542 }
1543 }
1544
1545more_kvec:
1546 /* kvec data queued? */
1547 if (con->out_skip) {
1548 ret = write_partial_skip(con);
1549 if (ret <= 0)
1550 goto done;
1551 if (ret < 0) {
1552 dout("try_write write_partial_skip err %d\n", ret);
1553 goto done;
1554 }
1555 }
1556 if (con->out_kvec_left) {
1557 ret = write_partial_kvec(con);
1558 if (ret <= 0)
1559 goto done;
Sage Weil31b80062009-10-06 11:31:13 -07001560 }
1561
1562 /* msg pages? */
1563 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001564 if (con->out_msg_done) {
1565 ceph_msg_put(con->out_msg);
1566 con->out_msg = NULL; /* we're done with this one */
1567 goto do_next;
1568 }
1569
Sage Weil31b80062009-10-06 11:31:13 -07001570 ret = write_partial_msg_pages(con);
1571 if (ret == 1)
1572 goto more_kvec; /* we need to send the footer, too! */
1573 if (ret == 0)
1574 goto done;
1575 if (ret < 0) {
1576 dout("try_write write_partial_msg_pages err %d\n",
1577 ret);
1578 goto done;
1579 }
1580 }
1581
Sage Weilc86a2932009-12-14 14:04:30 -08001582do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001583 if (!test_bit(CONNECTING, &con->state)) {
1584 /* is anything else pending? */
1585 if (!list_empty(&con->out_queue)) {
1586 prepare_write_message(con);
1587 goto more;
1588 }
1589 if (con->in_seq > con->in_seq_acked) {
1590 prepare_write_ack(con);
1591 goto more;
1592 }
1593 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1594 prepare_write_keepalive(con);
1595 goto more;
1596 }
1597 }
1598
1599 /* Nothing to do! */
1600 clear_bit(WRITE_PENDING, &con->state);
1601 dout("try_write nothing else to write.\n");
1602done:
1603 ret = 0;
1604out:
Sage Weilec302642009-12-22 10:43:42 -08001605 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001606 dout("try_write done on %p\n", con);
1607 return ret;
1608}
1609
1610
1611
1612/*
1613 * Read what we can from the socket.
1614 */
1615static int try_read(struct ceph_connection *con)
1616{
1617 struct ceph_messenger *msgr;
1618 int ret = -1;
1619
1620 if (!con->sock)
1621 return 0;
1622
1623 if (test_bit(STANDBY, &con->state))
1624 return 0;
1625
1626 dout("try_read start on %p\n", con);
1627 msgr = con->msgr;
1628
Sage Weilec302642009-12-22 10:43:42 -08001629 mutex_lock(&con->mutex);
1630
Sage Weil31b80062009-10-06 11:31:13 -07001631more:
1632 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1633 con->in_base_pos);
1634 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08001635 if (!test_bit(NEGOTIATING, &con->state)) {
1636 dout("try_read connecting\n");
1637 ret = read_partial_banner(con);
1638 if (ret <= 0)
1639 goto done;
1640 if (process_banner(con) < 0) {
1641 ret = -1;
1642 goto out;
1643 }
1644 }
Sage Weil31b80062009-10-06 11:31:13 -07001645 ret = read_partial_connect(con);
1646 if (ret <= 0)
1647 goto done;
1648 if (process_connect(con) < 0) {
1649 ret = -1;
1650 goto out;
1651 }
1652 goto more;
1653 }
1654
1655 if (con->in_base_pos < 0) {
1656 /*
1657 * skipping + discarding content.
1658 *
1659 * FIXME: there must be a better way to do this!
1660 */
1661 static char buf[1024];
1662 int skip = min(1024, -con->in_base_pos);
1663 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1664 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1665 if (ret <= 0)
1666 goto done;
1667 con->in_base_pos += ret;
1668 if (con->in_base_pos)
1669 goto more;
1670 }
1671 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1672 /*
1673 * what's next?
1674 */
1675 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1676 if (ret <= 0)
1677 goto done;
1678 dout("try_read got tag %d\n", (int)con->in_tag);
1679 switch (con->in_tag) {
1680 case CEPH_MSGR_TAG_MSG:
1681 prepare_read_message(con);
1682 break;
1683 case CEPH_MSGR_TAG_ACK:
1684 prepare_read_ack(con);
1685 break;
1686 case CEPH_MSGR_TAG_CLOSE:
1687 set_bit(CLOSED, &con->state); /* fixme */
1688 goto done;
1689 default:
1690 goto bad_tag;
1691 }
1692 }
1693 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1694 ret = read_partial_message(con);
1695 if (ret <= 0) {
1696 switch (ret) {
1697 case -EBADMSG:
1698 con->error_msg = "bad crc";
1699 ret = -EIO;
1700 goto out;
1701 case -EIO:
1702 con->error_msg = "io error";
1703 goto out;
1704 default:
1705 goto done;
1706 }
1707 }
1708 if (con->in_tag == CEPH_MSGR_TAG_READY)
1709 goto more;
1710 process_message(con);
1711 goto more;
1712 }
1713 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1714 ret = read_partial_ack(con);
1715 if (ret <= 0)
1716 goto done;
1717 process_ack(con);
1718 goto more;
1719 }
1720
1721done:
1722 ret = 0;
1723out:
Sage Weilec302642009-12-22 10:43:42 -08001724 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001725 dout("try_read done on %p\n", con);
1726 return ret;
1727
1728bad_tag:
1729 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1730 con->error_msg = "protocol error, garbage tag";
1731 ret = -1;
1732 goto out;
1733}
1734
1735
1736/*
1737 * Atomically queue work on a connection. Bump @con reference to
1738 * avoid races with connection teardown.
1739 *
1740 * There is some trickery going on with QUEUED and BUSY because we
1741 * only want a _single_ thread operating on each connection at any
1742 * point in time, but we want to use all available CPUs.
1743 *
1744 * The worker thread only proceeds if it can atomically set BUSY. It
1745 * clears QUEUED and does it's thing. When it thinks it's done, it
1746 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1747 * (tries again to set BUSY).
1748 *
1749 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1750 * try to queue work. If that fails (work is already queued, or BUSY)
1751 * we give up (work also already being done or is queued) but leave QUEUED
1752 * set so that the worker thread will loop if necessary.
1753 */
1754static void queue_con(struct ceph_connection *con)
1755{
1756 if (test_bit(DEAD, &con->state)) {
1757 dout("queue_con %p ignoring: DEAD\n",
1758 con);
1759 return;
1760 }
1761
1762 if (!con->ops->get(con)) {
1763 dout("queue_con %p ref count 0\n", con);
1764 return;
1765 }
1766
1767 set_bit(QUEUED, &con->state);
1768 if (test_bit(BUSY, &con->state)) {
1769 dout("queue_con %p - already BUSY\n", con);
1770 con->ops->put(con);
1771 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1772 dout("queue_con %p - already queued\n", con);
1773 con->ops->put(con);
1774 } else {
1775 dout("queue_con %p\n", con);
1776 }
1777}
1778
1779/*
1780 * Do some work on a connection. Drop a connection ref when we're done.
1781 */
1782static void con_work(struct work_struct *work)
1783{
1784 struct ceph_connection *con = container_of(work, struct ceph_connection,
1785 work.work);
1786 int backoff = 0;
1787
1788more:
1789 if (test_and_set_bit(BUSY, &con->state) != 0) {
1790 dout("con_work %p BUSY already set\n", con);
1791 goto out;
1792 }
1793 dout("con_work %p start, clearing QUEUED\n", con);
1794 clear_bit(QUEUED, &con->state);
1795
1796 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1797 dout("con_work CLOSED\n");
1798 con_close_socket(con);
1799 goto done;
1800 }
1801 if (test_and_clear_bit(OPENING, &con->state)) {
1802 /* reopen w/ new peer */
1803 dout("con_work OPENING\n");
1804 con_close_socket(con);
1805 }
1806
1807 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1808 try_read(con) < 0 ||
1809 try_write(con) < 0) {
1810 backoff = 1;
1811 ceph_fault(con); /* error/fault path */
1812 }
1813
1814done:
1815 clear_bit(BUSY, &con->state);
1816 dout("con->state=%lu\n", con->state);
1817 if (test_bit(QUEUED, &con->state)) {
Sage Weile2663ab2010-02-16 22:01:03 -08001818 if (!backoff || test_bit(OPENING, &con->state)) {
Sage Weil31b80062009-10-06 11:31:13 -07001819 dout("con_work %p QUEUED reset, looping\n", con);
1820 goto more;
1821 }
1822 dout("con_work %p QUEUED reset, but just faulted\n", con);
1823 clear_bit(QUEUED, &con->state);
1824 }
1825 dout("con_work %p done\n", con);
1826
1827out:
1828 con->ops->put(con);
1829}
1830
1831
1832/*
1833 * Generic error/fault handler. A retry mechanism is used with
1834 * exponential backoff
1835 */
1836static void ceph_fault(struct ceph_connection *con)
1837{
1838 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1839 pr_addr(&con->peer_addr.in_addr), con->error_msg);
1840 dout("fault %p state %lu to peer %s\n",
1841 con, con->state, pr_addr(&con->peer_addr.in_addr));
1842
1843 if (test_bit(LOSSYTX, &con->state)) {
1844 dout("fault on LOSSYTX channel\n");
1845 goto out;
1846 }
1847
Sage Weilec302642009-12-22 10:43:42 -08001848 mutex_lock(&con->mutex);
Sage Weil91e45ce2010-02-15 12:05:09 -08001849 if (test_bit(CLOSED, &con->state))
1850 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08001851
Sage Weil31b80062009-10-06 11:31:13 -07001852 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08001853
1854 if (con->in_msg) {
1855 ceph_msg_put(con->in_msg);
1856 con->in_msg = NULL;
1857 }
Sage Weil31b80062009-10-06 11:31:13 -07001858
Sage Weile80a52d2010-02-25 12:40:45 -08001859 /* Requeue anything that hasn't been acked */
1860 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001861
Sage Weil31b80062009-10-06 11:31:13 -07001862 /* If there are no messages in the queue, place the connection
1863 * in a STANDBY state (i.e., don't try to reconnect just yet). */
Sage Weil31b80062009-10-06 11:31:13 -07001864 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1865 dout("fault setting STANDBY\n");
1866 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08001867 } else {
1868 /* retry after a delay. */
1869 if (con->delay == 0)
1870 con->delay = BASE_DELAY_INTERVAL;
1871 else if (con->delay < MAX_DELAY_INTERVAL)
1872 con->delay *= 2;
1873 dout("fault queueing %p delay %lu\n", con, con->delay);
1874 con->ops->get(con);
1875 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1876 round_jiffies_relative(con->delay)) == 0)
1877 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001878 }
1879
Sage Weil91e45ce2010-02-15 12:05:09 -08001880out_unlock:
1881 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001882out:
Sage Weil161fd652010-02-25 12:38:57 -08001883 /*
1884 * in case we faulted due to authentication, invalidate our
1885 * current tickets so that we can get new ones.
1886 */
1887 if (con->auth_retry && con->ops->invalidate_authorizer) {
1888 dout("calling invalidate_authorizer()\n");
1889 con->ops->invalidate_authorizer(con);
1890 }
1891
Sage Weil31b80062009-10-06 11:31:13 -07001892 if (con->ops->fault)
1893 con->ops->fault(con);
1894}
1895
1896
1897
1898/*
1899 * create a new messenger instance
1900 */
1901struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1902{
1903 struct ceph_messenger *msgr;
1904
1905 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1906 if (msgr == NULL)
1907 return ERR_PTR(-ENOMEM);
1908
1909 spin_lock_init(&msgr->global_seq_lock);
1910
1911 /* the zero page is needed if a request is "canceled" while the message
1912 * is being written over the socket */
1913 msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1914 if (!msgr->zero_page) {
1915 kfree(msgr);
1916 return ERR_PTR(-ENOMEM);
1917 }
1918 kmap(msgr->zero_page);
1919
1920 if (myaddr)
1921 msgr->inst.addr = *myaddr;
1922
1923 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08001924 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08001925 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08001926 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07001927
1928 dout("messenger_create %p\n", msgr);
1929 return msgr;
1930}
1931
1932void ceph_messenger_destroy(struct ceph_messenger *msgr)
1933{
1934 dout("destroy %p\n", msgr);
1935 kunmap(msgr->zero_page);
1936 __free_page(msgr->zero_page);
1937 kfree(msgr);
1938 dout("destroyed messenger %p\n", msgr);
1939}
1940
1941/*
1942 * Queue up an outgoing message on the given connection.
1943 */
1944void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1945{
1946 if (test_bit(CLOSED, &con->state)) {
1947 dout("con_send %p closed, dropping %p\n", con, msg);
1948 ceph_msg_put(msg);
1949 return;
1950 }
1951
1952 /* set src+dst */
Sage Weil63f2d212009-11-03 15:17:56 -08001953 msg->hdr.src.name = con->msgr->inst.name;
1954 msg->hdr.src.addr = con->msgr->my_enc_addr;
1955 msg->hdr.orig_src = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001956
Sage Weil3ca02ef2010-03-01 15:25:00 -08001957 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
1958
Sage Weil31b80062009-10-06 11:31:13 -07001959 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08001960 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001961 BUG_ON(!list_empty(&msg->list_head));
1962 list_add_tail(&msg->list_head, &con->out_queue);
1963 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
1964 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
1965 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1966 le32_to_cpu(msg->hdr.front_len),
1967 le32_to_cpu(msg->hdr.middle_len),
1968 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08001969 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001970
1971 /* if there wasn't anything waiting to send before, queue
1972 * new work */
1973 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1974 queue_con(con);
1975}
1976
1977/*
1978 * Revoke a message that was previously queued for send
1979 */
1980void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
1981{
Sage Weilec302642009-12-22 10:43:42 -08001982 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001983 if (!list_empty(&msg->list_head)) {
1984 dout("con_revoke %p msg %p\n", con, msg);
1985 list_del_init(&msg->list_head);
1986 ceph_msg_put(msg);
1987 msg->hdr.seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -08001988 if (con->out_msg == msg) {
1989 ceph_msg_put(con->out_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001990 con->out_msg = NULL;
Sage Weilc86a2932009-12-14 14:04:30 -08001991 }
Sage Weil31b80062009-10-06 11:31:13 -07001992 if (con->out_kvec_is_msg) {
1993 con->out_skip = con->out_kvec_bytes;
1994 con->out_kvec_is_msg = false;
1995 }
1996 } else {
1997 dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
1998 }
Sage Weilec302642009-12-22 10:43:42 -08001999 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002000}
2001
2002/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002003 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002004 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002005void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002006{
2007 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002008 if (con->in_msg && con->in_msg == msg) {
2009 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2010 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002011 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2012
2013 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002014 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002015 con->in_base_pos = con->in_base_pos -
2016 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002017 front_len -
2018 middle_len -
2019 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002020 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002021 ceph_msg_put(con->in_msg);
2022 con->in_msg = NULL;
2023 con->in_tag = CEPH_MSGR_TAG_READY;
2024 } else {
2025 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002026 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002027 }
2028 mutex_unlock(&con->mutex);
2029}
2030
2031/*
Sage Weil31b80062009-10-06 11:31:13 -07002032 * Queue a keepalive byte to ensure the tcp connection is alive.
2033 */
2034void ceph_con_keepalive(struct ceph_connection *con)
2035{
2036 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2037 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2038 queue_con(con);
2039}
2040
2041
2042/*
2043 * construct a new message with given type, size
2044 * the new msg has a ref count of 1.
2045 */
2046struct ceph_msg *ceph_msg_new(int type, int front_len,
2047 int page_len, int page_off, struct page **pages)
2048{
2049 struct ceph_msg *m;
2050
2051 m = kmalloc(sizeof(*m), GFP_NOFS);
2052 if (m == NULL)
2053 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002054 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07002055 INIT_LIST_HEAD(&m->list_head);
2056
2057 m->hdr.type = cpu_to_le16(type);
2058 m->hdr.front_len = cpu_to_le32(front_len);
2059 m->hdr.middle_len = 0;
2060 m->hdr.data_len = cpu_to_le32(page_len);
2061 m->hdr.data_off = cpu_to_le16(page_off);
2062 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2063 m->footer.front_crc = 0;
2064 m->footer.middle_crc = 0;
2065 m->footer.data_crc = 0;
2066 m->front_max = front_len;
2067 m->front_is_vmalloc = false;
2068 m->more_to_follow = false;
2069 m->pool = NULL;
2070
2071 /* front */
2072 if (front_len) {
2073 if (front_len > PAGE_CACHE_SIZE) {
2074 m->front.iov_base = __vmalloc(front_len, GFP_NOFS,
2075 PAGE_KERNEL);
2076 m->front_is_vmalloc = true;
2077 } else {
2078 m->front.iov_base = kmalloc(front_len, GFP_NOFS);
2079 }
2080 if (m->front.iov_base == NULL) {
2081 pr_err("msg_new can't allocate %d bytes\n",
2082 front_len);
2083 goto out2;
2084 }
2085 } else {
2086 m->front.iov_base = NULL;
2087 }
2088 m->front.iov_len = front_len;
2089
2090 /* middle */
2091 m->middle = NULL;
2092
2093 /* data */
2094 m->nr_pages = calc_pages_for(page_off, page_len);
2095 m->pages = pages;
Sage Weil58bb3b32009-12-23 12:12:31 -08002096 m->pagelist = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002097
2098 dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len,
2099 m->nr_pages);
2100 return m;
2101
2102out2:
2103 ceph_msg_put(m);
2104out:
2105 pr_err("msg_new can't create type %d len %d\n", type, front_len);
2106 return ERR_PTR(-ENOMEM);
2107}
2108
2109/*
Sage Weil31b80062009-10-06 11:31:13 -07002110 * Allocate "middle" portion of a message, if it is needed and wasn't
2111 * allocated by alloc_msg. This allows us to read a small fixed-size
2112 * per-type header in the front and then gracefully fail (i.e.,
2113 * propagate the error to the caller based on info in the front) when
2114 * the middle is too large.
2115 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002116static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002117{
2118 int type = le16_to_cpu(msg->hdr.type);
2119 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2120
2121 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2122 ceph_msg_type_name(type), middle_len);
2123 BUG_ON(!middle_len);
2124 BUG_ON(msg->middle);
2125
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002126 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002127 if (!msg->middle)
2128 return -ENOMEM;
2129 return 0;
2130}
2131
Yehuda Sadeh24504182010-01-08 13:58:34 -08002132/*
2133 * Generic message allocator, for incoming messages.
2134 */
2135static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2136 struct ceph_msg_header *hdr,
2137 int *skip)
2138{
2139 int type = le16_to_cpu(hdr->type);
2140 int front_len = le32_to_cpu(hdr->front_len);
2141 int middle_len = le32_to_cpu(hdr->middle_len);
2142 struct ceph_msg *msg = NULL;
2143 int ret;
2144
2145 if (con->ops->alloc_msg) {
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002146 mutex_unlock(&con->mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002147 msg = con->ops->alloc_msg(con, hdr, skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002148 mutex_lock(&con->mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002149 if (IS_ERR(msg))
2150 return msg;
2151
2152 if (*skip)
2153 return NULL;
2154 }
2155 if (!msg) {
2156 *skip = 0;
2157 msg = ceph_msg_new(type, front_len, 0, 0, NULL);
2158 if (!msg) {
2159 pr_err("unable to allocate msg type %d len %d\n",
2160 type, front_len);
2161 return ERR_PTR(-ENOMEM);
2162 }
2163 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002164 memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002165
2166 if (middle_len) {
2167 ret = ceph_alloc_middle(con, msg);
2168
2169 if (ret < 0) {
2170 ceph_msg_put(msg);
2171 return msg;
2172 }
2173 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002174
Yehuda Sadeh24504182010-01-08 13:58:34 -08002175 return msg;
2176}
2177
Sage Weil31b80062009-10-06 11:31:13 -07002178
2179/*
2180 * Free a generically kmalloc'd message.
2181 */
2182void ceph_msg_kfree(struct ceph_msg *m)
2183{
2184 dout("msg_kfree %p\n", m);
2185 if (m->front_is_vmalloc)
2186 vfree(m->front.iov_base);
2187 else
2188 kfree(m->front.iov_base);
2189 kfree(m);
2190}
2191
2192/*
2193 * Drop a msg ref. Destroy as needed.
2194 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002195void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002196{
Sage Weilc2e552e2009-12-07 15:55:05 -08002197 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002198
Sage Weilc2e552e2009-12-07 15:55:05 -08002199 dout("ceph_msg_put last one on %p\n", m);
2200 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002201
Sage Weilc2e552e2009-12-07 15:55:05 -08002202 /* drop middle, data, if any */
2203 if (m->middle) {
2204 ceph_buffer_put(m->middle);
2205 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002206 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002207 m->nr_pages = 0;
2208 m->pages = NULL;
2209
Sage Weil58bb3b32009-12-23 12:12:31 -08002210 if (m->pagelist) {
2211 ceph_pagelist_release(m->pagelist);
2212 kfree(m->pagelist);
2213 m->pagelist = NULL;
2214 }
2215
Sage Weilc2e552e2009-12-07 15:55:05 -08002216 if (m->pool)
2217 ceph_msgpool_put(m->pool, m);
2218 else
2219 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002220}
Sage Weil9ec7cab2009-12-14 15:13:47 -08002221
2222void ceph_msg_dump(struct ceph_msg *msg)
2223{
2224 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2225 msg->front_max, msg->nr_pages);
2226 print_hex_dump(KERN_DEBUG, "header: ",
2227 DUMP_PREFIX_OFFSET, 16, 1,
2228 &msg->hdr, sizeof(msg->hdr), true);
2229 print_hex_dump(KERN_DEBUG, " front: ",
2230 DUMP_PREFIX_OFFSET, 16, 1,
2231 msg->front.iov_base, msg->front.iov_len, true);
2232 if (msg->middle)
2233 print_hex_dump(KERN_DEBUG, "middle: ",
2234 DUMP_PREFIX_OFFSET, 16, 1,
2235 msg->middle->vec.iov_base,
2236 msg->middle->vec.iov_len, true);
2237 print_hex_dump(KERN_DEBUG, "footer: ",
2238 DUMP_PREFIX_OFFSET, 16, 1,
2239 &msg->footer, sizeof(msg->footer), true);
2240}