blob: 5c75d5d32b27c525be580166edda112366d215cf [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
Sage Weila6a53492010-04-13 14:07:07 -070032#ifdef CONFIG_LOCKDEP
33static struct lock_class_key socket_class;
34#endif
35
Sage Weil31b80062009-10-06 11:31:13 -070036
37static void queue_con(struct ceph_connection *con);
38static void con_work(struct work_struct *);
39static void ceph_fault(struct ceph_connection *con);
40
41const char *ceph_name_type_str(int t)
42{
43 switch (t) {
44 case CEPH_ENTITY_TYPE_MON: return "mon";
45 case CEPH_ENTITY_TYPE_MDS: return "mds";
46 case CEPH_ENTITY_TYPE_OSD: return "osd";
47 case CEPH_ENTITY_TYPE_CLIENT: return "client";
48 case CEPH_ENTITY_TYPE_ADMIN: return "admin";
49 default: return "???";
50 }
51}
52
53/*
54 * nicely render a sockaddr as a string.
55 */
56#define MAX_ADDR_STR 20
57static char addr_str[MAX_ADDR_STR][40];
58static DEFINE_SPINLOCK(addr_str_lock);
59static int last_addr_str;
60
61const char *pr_addr(const struct sockaddr_storage *ss)
62{
63 int i;
64 char *s;
65 struct sockaddr_in *in4 = (void *)ss;
66 unsigned char *quad = (void *)&in4->sin_addr.s_addr;
67 struct sockaddr_in6 *in6 = (void *)ss;
68
69 spin_lock(&addr_str_lock);
70 i = last_addr_str++;
71 if (last_addr_str == MAX_ADDR_STR)
72 last_addr_str = 0;
73 spin_unlock(&addr_str_lock);
74 s = addr_str[i];
75
76 switch (ss->ss_family) {
77 case AF_INET:
78 sprintf(s, "%u.%u.%u.%u:%u",
79 (unsigned int)quad[0],
80 (unsigned int)quad[1],
81 (unsigned int)quad[2],
82 (unsigned int)quad[3],
83 (unsigned int)ntohs(in4->sin_port));
84 break;
85
86 case AF_INET6:
87 sprintf(s, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u",
88 in6->sin6_addr.s6_addr16[0],
89 in6->sin6_addr.s6_addr16[1],
90 in6->sin6_addr.s6_addr16[2],
91 in6->sin6_addr.s6_addr16[3],
92 in6->sin6_addr.s6_addr16[4],
93 in6->sin6_addr.s6_addr16[5],
94 in6->sin6_addr.s6_addr16[6],
95 in6->sin6_addr.s6_addr16[7],
96 (unsigned int)ntohs(in6->sin6_port));
97 break;
98
99 default:
100 sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family);
101 }
102
103 return s;
104}
105
Sage Weil63f2d212009-11-03 15:17:56 -0800106static void encode_my_addr(struct ceph_messenger *msgr)
107{
108 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
109 ceph_encode_addr(&msgr->my_enc_addr);
110}
111
Sage Weil31b80062009-10-06 11:31:13 -0700112/*
113 * work queue for all reading and writing to/from the socket.
114 */
115struct workqueue_struct *ceph_msgr_wq;
116
117int __init ceph_msgr_init(void)
118{
119 ceph_msgr_wq = create_workqueue("ceph-msgr");
120 if (IS_ERR(ceph_msgr_wq)) {
121 int ret = PTR_ERR(ceph_msgr_wq);
122 pr_err("msgr_init failed to create workqueue: %d\n", ret);
123 ceph_msgr_wq = NULL;
124 return ret;
125 }
126 return 0;
127}
128
129void ceph_msgr_exit(void)
130{
131 destroy_workqueue(ceph_msgr_wq);
132}
133
134/*
135 * socket callback functions
136 */
137
138/* data available on socket, or listen socket received a connect */
139static void ceph_data_ready(struct sock *sk, int count_unused)
140{
141 struct ceph_connection *con =
142 (struct ceph_connection *)sk->sk_user_data;
143 if (sk->sk_state != TCP_CLOSE_WAIT) {
144 dout("ceph_data_ready on %p state = %lu, queueing work\n",
145 con, con->state);
146 queue_con(con);
147 }
148}
149
150/* socket has buffer space for writing */
151static void ceph_write_space(struct sock *sk)
152{
153 struct ceph_connection *con =
154 (struct ceph_connection *)sk->sk_user_data;
155
156 /* only queue to workqueue if there is data we want to write. */
157 if (test_bit(WRITE_PENDING, &con->state)) {
158 dout("ceph_write_space %p queueing write work\n", con);
159 queue_con(con);
160 } else {
161 dout("ceph_write_space %p nothing to write\n", con);
162 }
163
164 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
165 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
166}
167
168/* socket's state has changed */
169static void ceph_state_change(struct sock *sk)
170{
171 struct ceph_connection *con =
172 (struct ceph_connection *)sk->sk_user_data;
173
174 dout("ceph_state_change %p state = %lu sk_state = %u\n",
175 con, con->state, sk->sk_state);
176
177 if (test_bit(CLOSED, &con->state))
178 return;
179
180 switch (sk->sk_state) {
181 case TCP_CLOSE:
182 dout("ceph_state_change TCP_CLOSE\n");
183 case TCP_CLOSE_WAIT:
184 dout("ceph_state_change TCP_CLOSE_WAIT\n");
185 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
186 if (test_bit(CONNECTING, &con->state))
187 con->error_msg = "connection failed";
188 else
189 con->error_msg = "socket closed";
190 queue_con(con);
191 }
192 break;
193 case TCP_ESTABLISHED:
194 dout("ceph_state_change TCP_ESTABLISHED\n");
195 queue_con(con);
196 break;
197 }
198}
199
200/*
201 * set up socket callbacks
202 */
203static void set_sock_callbacks(struct socket *sock,
204 struct ceph_connection *con)
205{
206 struct sock *sk = sock->sk;
207 sk->sk_user_data = (void *)con;
208 sk->sk_data_ready = ceph_data_ready;
209 sk->sk_write_space = ceph_write_space;
210 sk->sk_state_change = ceph_state_change;
211}
212
213
214/*
215 * socket helpers
216 */
217
218/*
219 * initiate connection to a remote socket.
220 */
221static struct socket *ceph_tcp_connect(struct ceph_connection *con)
222{
223 struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr;
224 struct socket *sock;
225 int ret;
226
227 BUG_ON(con->sock);
228 ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
229 if (ret)
230 return ERR_PTR(ret);
231 con->sock = sock;
232 sock->sk->sk_allocation = GFP_NOFS;
233
Sage Weila6a53492010-04-13 14:07:07 -0700234#ifdef CONFIG_LOCKDEP
235 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
236#endif
237
Sage Weil31b80062009-10-06 11:31:13 -0700238 set_sock_callbacks(sock, con);
239
240 dout("connect %s\n", pr_addr(&con->peer_addr.in_addr));
241
242 ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK);
243 if (ret == -EINPROGRESS) {
244 dout("connect %s EINPROGRESS sk_state = %u\n",
245 pr_addr(&con->peer_addr.in_addr),
246 sock->sk->sk_state);
247 ret = 0;
248 }
249 if (ret < 0) {
250 pr_err("connect %s error %d\n",
251 pr_addr(&con->peer_addr.in_addr), ret);
252 sock_release(sock);
253 con->sock = NULL;
254 con->error_msg = "connect error";
255 }
256
257 if (ret < 0)
258 return ERR_PTR(ret);
259 return sock;
260}
261
262static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
263{
264 struct kvec iov = {buf, len};
265 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
266
267 return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
268}
269
270/*
271 * write something. @more is true if caller will be sending more data
272 * shortly.
273 */
274static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
275 size_t kvlen, size_t len, int more)
276{
277 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
278
279 if (more)
280 msg.msg_flags |= MSG_MORE;
281 else
282 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
283
284 return kernel_sendmsg(sock, &msg, iov, kvlen, len);
285}
286
287
288/*
289 * Shutdown/close the socket for the given connection.
290 */
291static int con_close_socket(struct ceph_connection *con)
292{
293 int rc;
294
295 dout("con_close_socket on %p sock %p\n", con, con->sock);
296 if (!con->sock)
297 return 0;
298 set_bit(SOCK_CLOSED, &con->state);
299 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
300 sock_release(con->sock);
301 con->sock = NULL;
302 clear_bit(SOCK_CLOSED, &con->state);
303 return rc;
304}
305
306/*
307 * Reset a connection. Discard all incoming and outgoing messages
308 * and clear *_seq state.
309 */
310static void ceph_msg_remove(struct ceph_msg *msg)
311{
312 list_del_init(&msg->list_head);
313 ceph_msg_put(msg);
314}
315static void ceph_msg_remove_list(struct list_head *head)
316{
317 while (!list_empty(head)) {
318 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
319 list_head);
320 ceph_msg_remove(msg);
321 }
322}
323
324static void reset_connection(struct ceph_connection *con)
325{
326 /* reset connection, out_queue, msg_ and connect_seq */
327 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700328 ceph_msg_remove_list(&con->out_queue);
329 ceph_msg_remove_list(&con->out_sent);
330
Sage Weilcf3e5c42009-12-11 09:48:05 -0800331 if (con->in_msg) {
332 ceph_msg_put(con->in_msg);
333 con->in_msg = NULL;
334 }
335
Sage Weil31b80062009-10-06 11:31:13 -0700336 con->connect_seq = 0;
337 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800338 if (con->out_msg) {
339 ceph_msg_put(con->out_msg);
340 con->out_msg = NULL;
341 }
Sage Weil31b80062009-10-06 11:31:13 -0700342 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700343 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700344}
345
346/*
347 * mark a peer down. drop any open connections.
348 */
349void ceph_con_close(struct ceph_connection *con)
350{
351 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
352 set_bit(CLOSED, &con->state); /* in case there's queued work */
353 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Sage Weil1679f872010-02-26 13:55:51 -0800354 clear_bit(LOSSYTX, &con->state); /* so we retry next connect */
355 clear_bit(KEEPALIVE_PENDING, &con->state);
356 clear_bit(WRITE_PENDING, &con->state);
Sage Weilec302642009-12-22 10:43:42 -0800357 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700358 reset_connection(con);
Sage Weil91e45ce32010-02-15 12:05:09 -0800359 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800360 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700361 queue_con(con);
362}
363
364/*
Sage Weil31b80062009-10-06 11:31:13 -0700365 * Reopen a closed connection, with a new peer address.
366 */
367void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
368{
369 dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
370 set_bit(OPENING, &con->state);
371 clear_bit(CLOSED, &con->state);
372 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800373 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700374 queue_con(con);
375}
376
377/*
Sage Weil87b315a2010-03-22 14:51:18 -0700378 * return true if this connection ever successfully opened
379 */
380bool ceph_con_opened(struct ceph_connection *con)
381{
382 return con->connect_seq > 0;
383}
384
385/*
Sage Weil31b80062009-10-06 11:31:13 -0700386 * generic get/put
387 */
388struct ceph_connection *ceph_con_get(struct ceph_connection *con)
389{
390 dout("con_get %p nref = %d -> %d\n", con,
391 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
392 if (atomic_inc_not_zero(&con->nref))
393 return con;
394 return NULL;
395}
396
397void ceph_con_put(struct ceph_connection *con)
398{
399 dout("con_put %p nref = %d -> %d\n", con,
400 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
401 BUG_ON(atomic_read(&con->nref) == 0);
402 if (atomic_dec_and_test(&con->nref)) {
Sage Weil71ececd2009-11-18 11:27:06 -0800403 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700404 kfree(con);
405 }
406}
407
408/*
409 * initialize a new connection.
410 */
411void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
412{
413 dout("con_init %p\n", con);
414 memset(con, 0, sizeof(*con));
415 atomic_set(&con->nref, 1);
416 con->msgr = msgr;
Sage Weilec302642009-12-22 10:43:42 -0800417 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700418 INIT_LIST_HEAD(&con->out_queue);
419 INIT_LIST_HEAD(&con->out_sent);
420 INIT_DELAYED_WORK(&con->work, con_work);
421}
422
423
424/*
425 * We maintain a global counter to order connection attempts. Get
426 * a unique seq greater than @gt.
427 */
428static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
429{
430 u32 ret;
431
432 spin_lock(&msgr->global_seq_lock);
433 if (msgr->global_seq < gt)
434 msgr->global_seq = gt;
435 ret = ++msgr->global_seq;
436 spin_unlock(&msgr->global_seq_lock);
437 return ret;
438}
439
440
441/*
442 * Prepare footer for currently outgoing message, and finish things
443 * off. Assumes out_kvec* are already valid.. we just add on to the end.
444 */
445static void prepare_write_message_footer(struct ceph_connection *con, int v)
446{
447 struct ceph_msg *m = con->out_msg;
448
449 dout("prepare_write_message_footer %p\n", con);
450 con->out_kvec_is_msg = true;
451 con->out_kvec[v].iov_base = &m->footer;
452 con->out_kvec[v].iov_len = sizeof(m->footer);
453 con->out_kvec_bytes += sizeof(m->footer);
454 con->out_kvec_left++;
455 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800456 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700457}
458
459/*
460 * Prepare headers for the next outgoing message.
461 */
462static void prepare_write_message(struct ceph_connection *con)
463{
464 struct ceph_msg *m;
465 int v = 0;
466
467 con->out_kvec_bytes = 0;
468 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800469 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700470
471 /* Sneak an ack in there first? If we can get it into the same
472 * TCP packet that's a good thing. */
473 if (con->in_seq > con->in_seq_acked) {
474 con->in_seq_acked = con->in_seq;
475 con->out_kvec[v].iov_base = &tag_ack;
476 con->out_kvec[v++].iov_len = 1;
477 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
478 con->out_kvec[v].iov_base = &con->out_temp_ack;
479 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
480 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
481 }
482
Sage Weil31b80062009-10-06 11:31:13 -0700483 m = list_first_entry(&con->out_queue,
484 struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800485 con->out_msg = m;
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800486 if (test_bit(LOSSYTX, &con->state)) {
Sage Weil6c5d1a42010-02-13 20:29:31 -0800487 list_del_init(&m->list_head);
488 } else {
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800489 /* put message on sent list */
490 ceph_msg_get(m);
491 list_move_tail(&m->list_head, &con->out_sent);
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800492 }
Sage Weil31b80062009-10-06 11:31:13 -0700493
494 m->hdr.seq = cpu_to_le64(++con->out_seq);
495
496 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
497 m, con->out_seq, le16_to_cpu(m->hdr.type),
498 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
499 le32_to_cpu(m->hdr.data_len),
500 m->nr_pages);
501 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
502
503 /* tag + hdr + front + middle */
504 con->out_kvec[v].iov_base = &tag_msg;
505 con->out_kvec[v++].iov_len = 1;
506 con->out_kvec[v].iov_base = &m->hdr;
507 con->out_kvec[v++].iov_len = sizeof(m->hdr);
508 con->out_kvec[v++] = m->front;
509 if (m->middle)
510 con->out_kvec[v++] = m->middle->vec;
511 con->out_kvec_left = v;
512 con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
513 (m->middle ? m->middle->vec.iov_len : 0);
514 con->out_kvec_cur = con->out_kvec;
515
516 /* fill in crc (except data pages), footer */
517 con->out_msg->hdr.crc =
518 cpu_to_le32(crc32c(0, (void *)&m->hdr,
519 sizeof(m->hdr) - sizeof(m->hdr.crc)));
520 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
521 con->out_msg->footer.front_crc =
522 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
523 if (m->middle)
524 con->out_msg->footer.middle_crc =
525 cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
526 m->middle->vec.iov_len));
527 else
528 con->out_msg->footer.middle_crc = 0;
529 con->out_msg->footer.data_crc = 0;
530 dout("prepare_write_message front_crc %u data_crc %u\n",
531 le32_to_cpu(con->out_msg->footer.front_crc),
532 le32_to_cpu(con->out_msg->footer.middle_crc));
533
534 /* is there a data payload? */
535 if (le32_to_cpu(m->hdr.data_len) > 0) {
536 /* initialize page iterator */
537 con->out_msg_pos.page = 0;
538 con->out_msg_pos.page_pos =
539 le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
540 con->out_msg_pos.data_pos = 0;
541 con->out_msg_pos.did_page_crc = 0;
542 con->out_more = 1; /* data + footer will follow */
543 } else {
544 /* no, queue up footer too and be done */
545 prepare_write_message_footer(con, v);
546 }
547
548 set_bit(WRITE_PENDING, &con->state);
549}
550
551/*
552 * Prepare an ack.
553 */
554static void prepare_write_ack(struct ceph_connection *con)
555{
556 dout("prepare_write_ack %p %llu -> %llu\n", con,
557 con->in_seq_acked, con->in_seq);
558 con->in_seq_acked = con->in_seq;
559
560 con->out_kvec[0].iov_base = &tag_ack;
561 con->out_kvec[0].iov_len = 1;
562 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
563 con->out_kvec[1].iov_base = &con->out_temp_ack;
564 con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
565 con->out_kvec_left = 2;
566 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
567 con->out_kvec_cur = con->out_kvec;
568 con->out_more = 1; /* more will follow.. eventually.. */
569 set_bit(WRITE_PENDING, &con->state);
570}
571
572/*
573 * Prepare to write keepalive byte.
574 */
575static void prepare_write_keepalive(struct ceph_connection *con)
576{
577 dout("prepare_write_keepalive %p\n", con);
578 con->out_kvec[0].iov_base = &tag_keepalive;
579 con->out_kvec[0].iov_len = 1;
580 con->out_kvec_left = 1;
581 con->out_kvec_bytes = 1;
582 con->out_kvec_cur = con->out_kvec;
583 set_bit(WRITE_PENDING, &con->state);
584}
585
586/*
587 * Connection negotiation.
588 */
589
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800590static void prepare_connect_authorizer(struct ceph_connection *con)
591{
592 void *auth_buf;
593 int auth_len = 0;
594 int auth_protocol = 0;
595
Sage Weilec302642009-12-22 10:43:42 -0800596 mutex_unlock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800597 if (con->ops->get_authorizer)
598 con->ops->get_authorizer(con, &auth_buf, &auth_len,
599 &auth_protocol, &con->auth_reply_buf,
600 &con->auth_reply_buf_len,
601 con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800602 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800603
604 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
605 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
606
607 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
608 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
609 con->out_kvec_left++;
610 con->out_kvec_bytes += auth_len;
611}
612
Sage Weil31b80062009-10-06 11:31:13 -0700613/*
614 * We connected to a peer and are saying hello.
615 */
Sage Weileed0ef22009-11-10 14:34:36 -0800616static void prepare_write_banner(struct ceph_messenger *msgr,
617 struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700618{
619 int len = strlen(CEPH_BANNER);
Sage Weileed0ef22009-11-10 14:34:36 -0800620
621 con->out_kvec[0].iov_base = CEPH_BANNER;
622 con->out_kvec[0].iov_len = len;
623 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
624 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
625 con->out_kvec_left = 2;
626 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
627 con->out_kvec_cur = con->out_kvec;
628 con->out_more = 0;
629 set_bit(WRITE_PENDING, &con->state);
630}
631
632static void prepare_write_connect(struct ceph_messenger *msgr,
633 struct ceph_connection *con,
634 int after_banner)
635{
Sage Weil31b80062009-10-06 11:31:13 -0700636 unsigned global_seq = get_global_seq(con->msgr, 0);
637 int proto;
638
639 switch (con->peer_name.type) {
640 case CEPH_ENTITY_TYPE_MON:
641 proto = CEPH_MONC_PROTOCOL;
642 break;
643 case CEPH_ENTITY_TYPE_OSD:
644 proto = CEPH_OSDC_PROTOCOL;
645 break;
646 case CEPH_ENTITY_TYPE_MDS:
647 proto = CEPH_MDSC_PROTOCOL;
648 break;
649 default:
650 BUG();
651 }
652
653 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
654 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800655
Sage Weil04a419f2009-12-23 09:30:21 -0800656 con->out_connect.features = CEPH_FEATURE_SUPPORTED;
Sage Weil31b80062009-10-06 11:31:13 -0700657 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
658 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
659 con->out_connect.global_seq = cpu_to_le32(global_seq);
660 con->out_connect.protocol_version = cpu_to_le32(proto);
661 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700662
Sage Weileed0ef22009-11-10 14:34:36 -0800663 if (!after_banner) {
664 con->out_kvec_left = 0;
665 con->out_kvec_bytes = 0;
666 }
667 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
668 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
669 con->out_kvec_left++;
670 con->out_kvec_bytes += sizeof(con->out_connect);
Sage Weil31b80062009-10-06 11:31:13 -0700671 con->out_kvec_cur = con->out_kvec;
672 con->out_more = 0;
673 set_bit(WRITE_PENDING, &con->state);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800674
675 prepare_connect_authorizer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700676}
677
678
679/*
680 * write as much of pending kvecs to the socket as we can.
681 * 1 -> done
682 * 0 -> socket full, but more to do
683 * <0 -> error
684 */
685static int write_partial_kvec(struct ceph_connection *con)
686{
687 int ret;
688
689 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
690 while (con->out_kvec_bytes > 0) {
691 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
692 con->out_kvec_left, con->out_kvec_bytes,
693 con->out_more);
694 if (ret <= 0)
695 goto out;
696 con->out_kvec_bytes -= ret;
697 if (con->out_kvec_bytes == 0)
698 break; /* done */
699 while (ret > 0) {
700 if (ret >= con->out_kvec_cur->iov_len) {
701 ret -= con->out_kvec_cur->iov_len;
702 con->out_kvec_cur++;
703 con->out_kvec_left--;
704 } else {
705 con->out_kvec_cur->iov_len -= ret;
706 con->out_kvec_cur->iov_base += ret;
707 ret = 0;
708 break;
709 }
710 }
711 }
712 con->out_kvec_left = 0;
713 con->out_kvec_is_msg = false;
714 ret = 1;
715out:
716 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
717 con->out_kvec_bytes, con->out_kvec_left, ret);
718 return ret; /* done! */
719}
720
721/*
722 * Write as much message data payload as we can. If we finish, queue
723 * up the footer.
724 * 1 -> done, footer is now queued in out_kvec[].
725 * 0 -> socket full, but more to do
726 * <0 -> error
727 */
728static int write_partial_msg_pages(struct ceph_connection *con)
729{
730 struct ceph_msg *msg = con->out_msg;
731 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
732 size_t len;
733 int crc = con->msgr->nocrc;
734 int ret;
735
736 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
737 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
738 con->out_msg_pos.page_pos);
739
740 while (con->out_msg_pos.page < con->out_msg->nr_pages) {
741 struct page *page = NULL;
742 void *kaddr = NULL;
743
744 /*
745 * if we are calculating the data crc (the default), we need
746 * to map the page. if our pages[] has been revoked, use the
747 * zero page.
748 */
749 if (msg->pages) {
750 page = msg->pages[con->out_msg_pos.page];
751 if (crc)
752 kaddr = kmap(page);
Sage Weil58bb3b32009-12-23 12:12:31 -0800753 } else if (msg->pagelist) {
754 page = list_first_entry(&msg->pagelist->head,
755 struct page, lru);
756 if (crc)
757 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700758 } else {
759 page = con->msgr->zero_page;
760 if (crc)
761 kaddr = page_address(con->msgr->zero_page);
762 }
763 len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
764 (int)(data_len - con->out_msg_pos.data_pos));
765 if (crc && !con->out_msg_pos.did_page_crc) {
766 void *base = kaddr + con->out_msg_pos.page_pos;
767 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
768
769 BUG_ON(kaddr == NULL);
770 con->out_msg->footer.data_crc =
771 cpu_to_le32(crc32c(tmpcrc, base, len));
772 con->out_msg_pos.did_page_crc = 1;
773 }
774
775 ret = kernel_sendpage(con->sock, page,
776 con->out_msg_pos.page_pos, len,
777 MSG_DONTWAIT | MSG_NOSIGNAL |
778 MSG_MORE);
779
Sage Weil58bb3b32009-12-23 12:12:31 -0800780 if (crc && (msg->pages || msg->pagelist))
Sage Weil31b80062009-10-06 11:31:13 -0700781 kunmap(page);
782
783 if (ret <= 0)
784 goto out;
785
786 con->out_msg_pos.data_pos += ret;
787 con->out_msg_pos.page_pos += ret;
788 if (ret == len) {
789 con->out_msg_pos.page_pos = 0;
790 con->out_msg_pos.page++;
791 con->out_msg_pos.did_page_crc = 0;
Sage Weil58bb3b32009-12-23 12:12:31 -0800792 if (msg->pagelist)
793 list_move_tail(&page->lru,
794 &msg->pagelist->head);
Sage Weil31b80062009-10-06 11:31:13 -0700795 }
796 }
797
798 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
799
800 /* prepare and queue up footer, too */
801 if (!crc)
802 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
803 con->out_kvec_bytes = 0;
804 con->out_kvec_left = 0;
805 con->out_kvec_cur = con->out_kvec;
806 prepare_write_message_footer(con, 0);
807 ret = 1;
808out:
809 return ret;
810}
811
812/*
813 * write some zeros
814 */
815static int write_partial_skip(struct ceph_connection *con)
816{
817 int ret;
818
819 while (con->out_skip > 0) {
820 struct kvec iov = {
821 .iov_base = page_address(con->msgr->zero_page),
822 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
823 };
824
825 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
826 if (ret <= 0)
827 goto out;
828 con->out_skip -= ret;
829 }
830 ret = 1;
831out:
832 return ret;
833}
834
835/*
836 * Prepare to read connection handshake, or an ack.
837 */
Sage Weileed0ef22009-11-10 14:34:36 -0800838static void prepare_read_banner(struct ceph_connection *con)
839{
840 dout("prepare_read_banner %p\n", con);
841 con->in_base_pos = 0;
842}
843
Sage Weil31b80062009-10-06 11:31:13 -0700844static void prepare_read_connect(struct ceph_connection *con)
845{
846 dout("prepare_read_connect %p\n", con);
847 con->in_base_pos = 0;
848}
849
850static void prepare_read_ack(struct ceph_connection *con)
851{
852 dout("prepare_read_ack %p\n", con);
853 con->in_base_pos = 0;
854}
855
856static void prepare_read_tag(struct ceph_connection *con)
857{
858 dout("prepare_read_tag %p\n", con);
859 con->in_base_pos = 0;
860 con->in_tag = CEPH_MSGR_TAG_READY;
861}
862
863/*
864 * Prepare to read a message.
865 */
866static int prepare_read_message(struct ceph_connection *con)
867{
868 dout("prepare_read_message %p\n", con);
869 BUG_ON(con->in_msg != NULL);
870 con->in_base_pos = 0;
871 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
872 return 0;
873}
874
875
876static int read_partial(struct ceph_connection *con,
877 int *to, int size, void *object)
878{
879 *to += size;
880 while (con->in_base_pos < *to) {
881 int left = *to - con->in_base_pos;
882 int have = size - left;
883 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
884 if (ret <= 0)
885 return ret;
886 con->in_base_pos += ret;
887 }
888 return 1;
889}
890
891
892/*
893 * Read all or part of the connect-side handshake on a new connection
894 */
Sage Weileed0ef22009-11-10 14:34:36 -0800895static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700896{
897 int ret, to = 0;
898
Sage Weileed0ef22009-11-10 14:34:36 -0800899 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -0700900
901 /* peer's banner */
902 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
903 if (ret <= 0)
904 goto out;
905 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
906 &con->actual_peer_addr);
907 if (ret <= 0)
908 goto out;
909 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
910 &con->peer_addr_for_me);
911 if (ret <= 0)
912 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -0800913out:
914 return ret;
915}
916
917static int read_partial_connect(struct ceph_connection *con)
918{
919 int ret, to = 0;
920
921 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
922
Sage Weil31b80062009-10-06 11:31:13 -0700923 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
924 if (ret <= 0)
925 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800926 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
927 con->auth_reply_buf);
928 if (ret <= 0)
929 goto out;
Sage Weil31b80062009-10-06 11:31:13 -0700930
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800931 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
932 con, (int)con->in_reply.tag,
933 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -0700934 le32_to_cpu(con->in_reply.global_seq));
935out:
936 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -0800937
Sage Weil31b80062009-10-06 11:31:13 -0700938}
939
940/*
941 * Verify the hello banner looks okay.
942 */
943static int verify_hello(struct ceph_connection *con)
944{
945 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -0700946 pr_err("connect to %s got bad banner\n",
Sage Weil31b80062009-10-06 11:31:13 -0700947 pr_addr(&con->peer_addr.in_addr));
948 con->error_msg = "protocol error, bad banner";
949 return -1;
950 }
951 return 0;
952}
953
954static bool addr_is_blank(struct sockaddr_storage *ss)
955{
956 switch (ss->ss_family) {
957 case AF_INET:
958 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
959 case AF_INET6:
960 return
961 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
962 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
963 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
964 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
965 }
966 return false;
967}
968
969static int addr_port(struct sockaddr_storage *ss)
970{
971 switch (ss->ss_family) {
972 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800973 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -0700974 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800975 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -0700976 }
977 return 0;
978}
979
980static void addr_set_port(struct sockaddr_storage *ss, int p)
981{
982 switch (ss->ss_family) {
983 case AF_INET:
984 ((struct sockaddr_in *)ss)->sin_port = htons(p);
985 case AF_INET6:
986 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
987 }
988}
989
990/*
991 * Parse an ip[:port] list into an addr array. Use the default
992 * monitor port if a port isn't specified.
993 */
994int ceph_parse_ips(const char *c, const char *end,
995 struct ceph_entity_addr *addr,
996 int max_count, int *count)
997{
998 int i;
999 const char *p = c;
1000
1001 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1002 for (i = 0; i < max_count; i++) {
1003 const char *ipend;
1004 struct sockaddr_storage *ss = &addr[i].in_addr;
1005 struct sockaddr_in *in4 = (void *)ss;
1006 struct sockaddr_in6 *in6 = (void *)ss;
1007 int port;
1008
1009 memset(ss, 0, sizeof(*ss));
1010 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
1011 ',', &ipend)) {
1012 ss->ss_family = AF_INET;
1013 } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
1014 ',', &ipend)) {
1015 ss->ss_family = AF_INET6;
1016 } else {
1017 goto bad;
1018 }
1019 p = ipend;
1020
1021 /* port? */
1022 if (p < end && *p == ':') {
1023 port = 0;
1024 p++;
1025 while (p < end && *p >= '0' && *p <= '9') {
1026 port = (port * 10) + (*p - '0');
1027 p++;
1028 }
1029 if (port > 65535 || port == 0)
1030 goto bad;
1031 } else {
1032 port = CEPH_MON_PORT;
1033 }
1034
1035 addr_set_port(ss, port);
1036
1037 dout("parse_ips got %s\n", pr_addr(ss));
1038
1039 if (p == end)
1040 break;
1041 if (*p != ',')
1042 goto bad;
1043 p++;
1044 }
1045
1046 if (p != end)
1047 goto bad;
1048
1049 if (count)
1050 *count = i + 1;
1051 return 0;
1052
1053bad:
1054 pr_err("parse_ips bad ip '%s'\n", c);
1055 return -EINVAL;
1056}
1057
Sage Weileed0ef22009-11-10 14:34:36 -08001058static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001059{
Sage Weileed0ef22009-11-10 14:34:36 -08001060 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001061
1062 if (verify_hello(con) < 0)
1063 return -1;
1064
Sage Weil63f2d212009-11-03 15:17:56 -08001065 ceph_decode_addr(&con->actual_peer_addr);
1066 ceph_decode_addr(&con->peer_addr_for_me);
1067
Sage Weil31b80062009-10-06 11:31:13 -07001068 /*
1069 * Make sure the other end is who we wanted. note that the other
1070 * end may not yet know their ip address, so if it's 0.0.0.0, give
1071 * them the benefit of the doubt.
1072 */
Sage Weil103e2d32010-01-07 16:12:36 -08001073 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1074 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001075 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1076 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Sage Weil103e2d32010-01-07 16:12:36 -08001077 pr_warning("wrong peer, want %s/%lld, got %s/%lld\n",
1078 pr_addr(&con->peer_addr.in_addr),
1079 le64_to_cpu(con->peer_addr.nonce),
1080 pr_addr(&con->actual_peer_addr.in_addr),
1081 le64_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001082 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001083 return -1;
1084 }
1085
1086 /*
1087 * did we learn our address?
1088 */
1089 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1090 int port = addr_port(&con->msgr->inst.addr.in_addr);
1091
1092 memcpy(&con->msgr->inst.addr.in_addr,
1093 &con->peer_addr_for_me.in_addr,
1094 sizeof(con->peer_addr_for_me.in_addr));
1095 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001096 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001097 dout("process_banner learned my addr is %s\n",
Sage Weil31b80062009-10-06 11:31:13 -07001098 pr_addr(&con->msgr->inst.addr.in_addr));
1099 }
1100
Sage Weileed0ef22009-11-10 14:34:36 -08001101 set_bit(NEGOTIATING, &con->state);
1102 prepare_read_connect(con);
1103 return 0;
1104}
1105
Sage Weil04a419f2009-12-23 09:30:21 -08001106static void fail_protocol(struct ceph_connection *con)
1107{
1108 reset_connection(con);
1109 set_bit(CLOSED, &con->state); /* in case there's queued work */
1110
1111 mutex_unlock(&con->mutex);
1112 if (con->ops->bad_proto)
1113 con->ops->bad_proto(con);
1114 mutex_lock(&con->mutex);
1115}
1116
Sage Weileed0ef22009-11-10 14:34:36 -08001117static int process_connect(struct ceph_connection *con)
1118{
Sage Weil04a419f2009-12-23 09:30:21 -08001119 u64 sup_feat = CEPH_FEATURE_SUPPORTED;
1120 u64 req_feat = CEPH_FEATURE_REQUIRED;
1121 u64 server_feat = le64_to_cpu(con->in_reply.features);
1122
Sage Weileed0ef22009-11-10 14:34:36 -08001123 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1124
Sage Weil31b80062009-10-06 11:31:13 -07001125 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001126 case CEPH_MSGR_TAG_FEATURES:
1127 pr_err("%s%lld %s feature set mismatch,"
1128 " my %llx < server's %llx, missing %llx\n",
1129 ENTITY_NAME(con->peer_name),
1130 pr_addr(&con->peer_addr.in_addr),
1131 sup_feat, server_feat, server_feat & ~sup_feat);
1132 con->error_msg = "missing required protocol features";
1133 fail_protocol(con);
1134 return -1;
1135
Sage Weil31b80062009-10-06 11:31:13 -07001136 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001137 pr_err("%s%lld %s protocol version mismatch,"
1138 " my %d != server's %d\n",
1139 ENTITY_NAME(con->peer_name),
1140 pr_addr(&con->peer_addr.in_addr),
1141 le32_to_cpu(con->out_connect.protocol_version),
1142 le32_to_cpu(con->in_reply.protocol_version));
1143 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001144 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001145 return -1;
1146
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001147 case CEPH_MSGR_TAG_BADAUTHORIZER:
1148 con->auth_retry++;
1149 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1150 con->auth_retry);
1151 if (con->auth_retry == 2) {
1152 con->error_msg = "connect authorization failure";
1153 reset_connection(con);
1154 set_bit(CLOSED, &con->state);
1155 return -1;
1156 }
1157 con->auth_retry = 1;
1158 prepare_write_connect(con->msgr, con, 0);
Sage Weil63733a02010-03-15 15:47:22 -07001159 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001160 break;
Sage Weil31b80062009-10-06 11:31:13 -07001161
1162 case CEPH_MSGR_TAG_RESETSESSION:
1163 /*
1164 * If we connected with a large connect_seq but the peer
1165 * has no record of a session with us (no connection, or
1166 * connect_seq == 0), they will send RESETSESION to indicate
1167 * that they must have reset their session, and may have
1168 * dropped messages.
1169 */
1170 dout("process_connect got RESET peer seq %u\n",
1171 le32_to_cpu(con->in_connect.connect_seq));
1172 pr_err("%s%lld %s connection reset\n",
1173 ENTITY_NAME(con->peer_name),
1174 pr_addr(&con->peer_addr.in_addr));
1175 reset_connection(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001176 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001177 prepare_read_connect(con);
1178
1179 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001180 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001181 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1182 if (con->ops->peer_reset)
1183 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001184 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001185 break;
1186
1187 case CEPH_MSGR_TAG_RETRY_SESSION:
1188 /*
1189 * If we sent a smaller connect_seq than the peer has, try
1190 * again with a larger value.
1191 */
1192 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1193 le32_to_cpu(con->out_connect.connect_seq),
1194 le32_to_cpu(con->in_connect.connect_seq));
1195 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Sage Weileed0ef22009-11-10 14:34:36 -08001196 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001197 prepare_read_connect(con);
1198 break;
1199
1200 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1201 /*
1202 * If we sent a smaller global_seq than the peer has, try
1203 * again with a larger value.
1204 */
Sage Weileed0ef22009-11-10 14:34:36 -08001205 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001206 con->peer_global_seq,
1207 le32_to_cpu(con->in_connect.global_seq));
1208 get_global_seq(con->msgr,
1209 le32_to_cpu(con->in_connect.global_seq));
Sage Weileed0ef22009-11-10 14:34:36 -08001210 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001211 prepare_read_connect(con);
1212 break;
1213
1214 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001215 if (req_feat & ~server_feat) {
1216 pr_err("%s%lld %s protocol feature mismatch,"
1217 " my required %llx > server's %llx, need %llx\n",
1218 ENTITY_NAME(con->peer_name),
1219 pr_addr(&con->peer_addr.in_addr),
1220 req_feat, server_feat, req_feat & ~server_feat);
1221 con->error_msg = "missing required protocol features";
1222 fail_protocol(con);
1223 return -1;
1224 }
Sage Weil31b80062009-10-06 11:31:13 -07001225 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001226 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1227 con->connect_seq++;
1228 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1229 con->peer_global_seq,
1230 le32_to_cpu(con->in_reply.connect_seq),
1231 con->connect_seq);
1232 WARN_ON(con->connect_seq !=
1233 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001234
1235 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1236 set_bit(LOSSYTX, &con->state);
1237
Sage Weil31b80062009-10-06 11:31:13 -07001238 prepare_read_tag(con);
1239 break;
1240
1241 case CEPH_MSGR_TAG_WAIT:
1242 /*
1243 * If there is a connection race (we are opening
1244 * connections to each other), one of us may just have
1245 * to WAIT. This shouldn't happen if we are the
1246 * client.
1247 */
1248 pr_err("process_connect peer connecting WAIT\n");
1249
1250 default:
1251 pr_err("connect protocol error, will retry\n");
1252 con->error_msg = "protocol error, garbage tag during connect";
1253 return -1;
1254 }
1255 return 0;
1256}
1257
1258
1259/*
1260 * read (part of) an ack
1261 */
1262static int read_partial_ack(struct ceph_connection *con)
1263{
1264 int to = 0;
1265
1266 return read_partial(con, &to, sizeof(con->in_temp_ack),
1267 &con->in_temp_ack);
1268}
1269
1270
1271/*
1272 * We can finally discard anything that's been acked.
1273 */
1274static void process_ack(struct ceph_connection *con)
1275{
1276 struct ceph_msg *m;
1277 u64 ack = le64_to_cpu(con->in_temp_ack);
1278 u64 seq;
1279
Sage Weil31b80062009-10-06 11:31:13 -07001280 while (!list_empty(&con->out_sent)) {
1281 m = list_first_entry(&con->out_sent, struct ceph_msg,
1282 list_head);
1283 seq = le64_to_cpu(m->hdr.seq);
1284 if (seq > ack)
1285 break;
1286 dout("got ack for seq %llu type %d at %p\n", seq,
1287 le16_to_cpu(m->hdr.type), m);
1288 ceph_msg_remove(m);
1289 }
Sage Weil31b80062009-10-06 11:31:13 -07001290 prepare_read_tag(con);
1291}
1292
1293
1294
1295
Yehuda Sadeh24504182010-01-08 13:58:34 -08001296static int read_partial_message_section(struct ceph_connection *con,
1297 struct kvec *section, unsigned int sec_len,
1298 u32 *crc)
1299{
1300 int left;
1301 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001302
Yehuda Sadeh24504182010-01-08 13:58:34 -08001303 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001304
Yehuda Sadeh24504182010-01-08 13:58:34 -08001305 while (section->iov_len < sec_len) {
1306 BUG_ON(section->iov_base == NULL);
1307 left = sec_len - section->iov_len;
1308 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1309 section->iov_len, left);
1310 if (ret <= 0)
1311 return ret;
1312 section->iov_len += ret;
1313 if (section->iov_len == sec_len)
1314 *crc = crc32c(0, section->iov_base,
1315 section->iov_len);
1316 }
1317
1318 return 1;
1319}
1320
1321static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1322 struct ceph_msg_header *hdr,
1323 int *skip);
Sage Weil31b80062009-10-06 11:31:13 -07001324/*
1325 * read (part of) a message.
1326 */
1327static int read_partial_message(struct ceph_connection *con)
1328{
1329 struct ceph_msg *m = con->in_msg;
1330 void *p;
1331 int ret;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001332 int to, left;
Sage Weil31b80062009-10-06 11:31:13 -07001333 unsigned front_len, middle_len, data_len, data_off;
1334 int datacrc = con->msgr->nocrc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001335 int skip;
Sage Weil31b80062009-10-06 11:31:13 -07001336
1337 dout("read_partial_message con %p msg %p\n", con, m);
1338
1339 /* header */
1340 while (con->in_base_pos < sizeof(con->in_hdr)) {
1341 left = sizeof(con->in_hdr) - con->in_base_pos;
1342 ret = ceph_tcp_recvmsg(con->sock,
1343 (char *)&con->in_hdr + con->in_base_pos,
1344 left);
1345 if (ret <= 0)
1346 return ret;
1347 con->in_base_pos += ret;
1348 if (con->in_base_pos == sizeof(con->in_hdr)) {
1349 u32 crc = crc32c(0, (void *)&con->in_hdr,
1350 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1351 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1352 pr_err("read_partial_message bad hdr "
1353 " crc %u != expected %u\n",
1354 crc, con->in_hdr.crc);
1355 return -EBADMSG;
1356 }
1357 }
1358 }
Sage Weil31b80062009-10-06 11:31:13 -07001359 front_len = le32_to_cpu(con->in_hdr.front_len);
1360 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1361 return -EIO;
1362 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1363 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1364 return -EIO;
1365 data_len = le32_to_cpu(con->in_hdr.data_len);
1366 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1367 return -EIO;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001368 data_off = le16_to_cpu(con->in_hdr.data_off);
Sage Weil31b80062009-10-06 11:31:13 -07001369
1370 /* allocate message? */
1371 if (!con->in_msg) {
1372 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1373 con->in_hdr.front_len, con->in_hdr.data_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001374 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1375 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07001376 /* skip this message */
Sage Weil5b3a4db2010-02-19 21:43:23 -08001377 dout("alloc_msg returned NULL, skipping message\n");
Sage Weil31b80062009-10-06 11:31:13 -07001378 con->in_base_pos = -front_len - middle_len - data_len -
1379 sizeof(m->footer);
1380 con->in_tag = CEPH_MSGR_TAG_READY;
1381 return 0;
1382 }
1383 if (IS_ERR(con->in_msg)) {
1384 ret = PTR_ERR(con->in_msg);
1385 con->in_msg = NULL;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001386 con->error_msg =
1387 "error allocating memory for incoming message";
Sage Weil31b80062009-10-06 11:31:13 -07001388 return ret;
1389 }
1390 m = con->in_msg;
1391 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001392 if (m->middle)
1393 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001394
1395 con->in_msg_pos.page = 0;
1396 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1397 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001398 }
1399
1400 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001401 ret = read_partial_message_section(con, &m->front, front_len,
1402 &con->in_front_crc);
1403 if (ret <= 0)
1404 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001405
1406 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001407 if (m->middle) {
1408 ret = read_partial_message_section(con, &m->middle->vec, middle_len,
1409 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001410 if (ret <= 0)
1411 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001412 }
1413
1414 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001415 while (con->in_msg_pos.data_pos < data_len) {
1416 left = min((int)(data_len - con->in_msg_pos.data_pos),
1417 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1418 BUG_ON(m->pages == NULL);
1419 p = kmap(m->pages[con->in_msg_pos.page]);
1420 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1421 left);
1422 if (ret > 0 && datacrc)
1423 con->in_data_crc =
1424 crc32c(con->in_data_crc,
1425 p + con->in_msg_pos.page_pos, ret);
1426 kunmap(m->pages[con->in_msg_pos.page]);
1427 if (ret <= 0)
1428 return ret;
1429 con->in_msg_pos.data_pos += ret;
1430 con->in_msg_pos.page_pos += ret;
1431 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1432 con->in_msg_pos.page_pos = 0;
1433 con->in_msg_pos.page++;
1434 }
1435 }
1436
Sage Weil31b80062009-10-06 11:31:13 -07001437 /* footer */
1438 to = sizeof(m->hdr) + sizeof(m->footer);
1439 while (con->in_base_pos < to) {
1440 left = to - con->in_base_pos;
1441 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1442 (con->in_base_pos - sizeof(m->hdr)),
1443 left);
1444 if (ret <= 0)
1445 return ret;
1446 con->in_base_pos += ret;
1447 }
1448 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1449 m, front_len, m->footer.front_crc, middle_len,
1450 m->footer.middle_crc, data_len, m->footer.data_crc);
1451
1452 /* crc ok? */
1453 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1454 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1455 m, con->in_front_crc, m->footer.front_crc);
1456 return -EBADMSG;
1457 }
1458 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1459 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1460 m, con->in_middle_crc, m->footer.middle_crc);
1461 return -EBADMSG;
1462 }
1463 if (datacrc &&
1464 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1465 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1466 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1467 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1468 return -EBADMSG;
1469 }
1470
1471 return 1; /* done! */
1472}
1473
1474/*
1475 * Process message. This happens in the worker thread. The callback should
1476 * be careful not to do anything that waits on other incoming messages or it
1477 * may deadlock.
1478 */
1479static void process_message(struct ceph_connection *con)
1480{
Sage Weil5e095e82009-12-14 14:30:34 -08001481 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001482
Sage Weil5e095e82009-12-14 14:30:34 -08001483 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001484 con->in_msg = NULL;
1485
1486 /* if first message, set peer_name */
1487 if (con->peer_name.type == 0)
1488 con->peer_name = msg->hdr.src.name;
1489
Sage Weil31b80062009-10-06 11:31:13 -07001490 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001491 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001492
1493 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1494 msg, le64_to_cpu(msg->hdr.seq),
1495 ENTITY_NAME(msg->hdr.src.name),
1496 le16_to_cpu(msg->hdr.type),
1497 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1498 le32_to_cpu(msg->hdr.front_len),
1499 le32_to_cpu(msg->hdr.data_len),
1500 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1501 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001502
1503 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001504 prepare_read_tag(con);
1505}
1506
1507
1508/*
1509 * Write something to the socket. Called in a worker thread when the
1510 * socket appears to be writeable and we have something ready to send.
1511 */
1512static int try_write(struct ceph_connection *con)
1513{
1514 struct ceph_messenger *msgr = con->msgr;
1515 int ret = 1;
1516
1517 dout("try_write start %p state %lu nref %d\n", con, con->state,
1518 atomic_read(&con->nref));
1519
Sage Weilec302642009-12-22 10:43:42 -08001520 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001521more:
1522 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1523
1524 /* open the socket first? */
1525 if (con->sock == NULL) {
1526 /*
1527 * if we were STANDBY and are reconnecting _this_
1528 * connection, bump connect_seq now. Always bump
1529 * global_seq.
1530 */
1531 if (test_and_clear_bit(STANDBY, &con->state))
1532 con->connect_seq++;
1533
Sage Weileed0ef22009-11-10 14:34:36 -08001534 prepare_write_banner(msgr, con);
1535 prepare_write_connect(msgr, con, 1);
1536 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001537 set_bit(CONNECTING, &con->state);
Sage Weileed0ef22009-11-10 14:34:36 -08001538 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001539
Sage Weilcf3e5c42009-12-11 09:48:05 -08001540 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001541 con->in_tag = CEPH_MSGR_TAG_READY;
1542 dout("try_write initiating connect on %p new state %lu\n",
1543 con, con->state);
1544 con->sock = ceph_tcp_connect(con);
1545 if (IS_ERR(con->sock)) {
1546 con->sock = NULL;
1547 con->error_msg = "connect error";
1548 ret = -1;
1549 goto out;
1550 }
1551 }
1552
1553more_kvec:
1554 /* kvec data queued? */
1555 if (con->out_skip) {
1556 ret = write_partial_skip(con);
1557 if (ret <= 0)
1558 goto done;
1559 if (ret < 0) {
1560 dout("try_write write_partial_skip err %d\n", ret);
1561 goto done;
1562 }
1563 }
1564 if (con->out_kvec_left) {
1565 ret = write_partial_kvec(con);
1566 if (ret <= 0)
1567 goto done;
Sage Weil31b80062009-10-06 11:31:13 -07001568 }
1569
1570 /* msg pages? */
1571 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001572 if (con->out_msg_done) {
1573 ceph_msg_put(con->out_msg);
1574 con->out_msg = NULL; /* we're done with this one */
1575 goto do_next;
1576 }
1577
Sage Weil31b80062009-10-06 11:31:13 -07001578 ret = write_partial_msg_pages(con);
1579 if (ret == 1)
1580 goto more_kvec; /* we need to send the footer, too! */
1581 if (ret == 0)
1582 goto done;
1583 if (ret < 0) {
1584 dout("try_write write_partial_msg_pages err %d\n",
1585 ret);
1586 goto done;
1587 }
1588 }
1589
Sage Weilc86a2932009-12-14 14:04:30 -08001590do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001591 if (!test_bit(CONNECTING, &con->state)) {
1592 /* is anything else pending? */
1593 if (!list_empty(&con->out_queue)) {
1594 prepare_write_message(con);
1595 goto more;
1596 }
1597 if (con->in_seq > con->in_seq_acked) {
1598 prepare_write_ack(con);
1599 goto more;
1600 }
1601 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1602 prepare_write_keepalive(con);
1603 goto more;
1604 }
1605 }
1606
1607 /* Nothing to do! */
1608 clear_bit(WRITE_PENDING, &con->state);
1609 dout("try_write nothing else to write.\n");
1610done:
1611 ret = 0;
1612out:
Sage Weilec302642009-12-22 10:43:42 -08001613 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001614 dout("try_write done on %p\n", con);
1615 return ret;
1616}
1617
1618
1619
1620/*
1621 * Read what we can from the socket.
1622 */
1623static int try_read(struct ceph_connection *con)
1624{
1625 struct ceph_messenger *msgr;
1626 int ret = -1;
1627
1628 if (!con->sock)
1629 return 0;
1630
1631 if (test_bit(STANDBY, &con->state))
1632 return 0;
1633
1634 dout("try_read start on %p\n", con);
1635 msgr = con->msgr;
1636
Sage Weilec302642009-12-22 10:43:42 -08001637 mutex_lock(&con->mutex);
1638
Sage Weil31b80062009-10-06 11:31:13 -07001639more:
1640 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1641 con->in_base_pos);
1642 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08001643 if (!test_bit(NEGOTIATING, &con->state)) {
1644 dout("try_read connecting\n");
1645 ret = read_partial_banner(con);
1646 if (ret <= 0)
1647 goto done;
1648 if (process_banner(con) < 0) {
1649 ret = -1;
1650 goto out;
1651 }
1652 }
Sage Weil31b80062009-10-06 11:31:13 -07001653 ret = read_partial_connect(con);
1654 if (ret <= 0)
1655 goto done;
1656 if (process_connect(con) < 0) {
1657 ret = -1;
1658 goto out;
1659 }
1660 goto more;
1661 }
1662
1663 if (con->in_base_pos < 0) {
1664 /*
1665 * skipping + discarding content.
1666 *
1667 * FIXME: there must be a better way to do this!
1668 */
1669 static char buf[1024];
1670 int skip = min(1024, -con->in_base_pos);
1671 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1672 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1673 if (ret <= 0)
1674 goto done;
1675 con->in_base_pos += ret;
1676 if (con->in_base_pos)
1677 goto more;
1678 }
1679 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1680 /*
1681 * what's next?
1682 */
1683 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1684 if (ret <= 0)
1685 goto done;
1686 dout("try_read got tag %d\n", (int)con->in_tag);
1687 switch (con->in_tag) {
1688 case CEPH_MSGR_TAG_MSG:
1689 prepare_read_message(con);
1690 break;
1691 case CEPH_MSGR_TAG_ACK:
1692 prepare_read_ack(con);
1693 break;
1694 case CEPH_MSGR_TAG_CLOSE:
1695 set_bit(CLOSED, &con->state); /* fixme */
1696 goto done;
1697 default:
1698 goto bad_tag;
1699 }
1700 }
1701 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1702 ret = read_partial_message(con);
1703 if (ret <= 0) {
1704 switch (ret) {
1705 case -EBADMSG:
1706 con->error_msg = "bad crc";
1707 ret = -EIO;
1708 goto out;
1709 case -EIO:
1710 con->error_msg = "io error";
1711 goto out;
1712 default:
1713 goto done;
1714 }
1715 }
1716 if (con->in_tag == CEPH_MSGR_TAG_READY)
1717 goto more;
1718 process_message(con);
1719 goto more;
1720 }
1721 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1722 ret = read_partial_ack(con);
1723 if (ret <= 0)
1724 goto done;
1725 process_ack(con);
1726 goto more;
1727 }
1728
1729done:
1730 ret = 0;
1731out:
Sage Weilec302642009-12-22 10:43:42 -08001732 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001733 dout("try_read done on %p\n", con);
1734 return ret;
1735
1736bad_tag:
1737 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1738 con->error_msg = "protocol error, garbage tag";
1739 ret = -1;
1740 goto out;
1741}
1742
1743
1744/*
1745 * Atomically queue work on a connection. Bump @con reference to
1746 * avoid races with connection teardown.
1747 *
1748 * There is some trickery going on with QUEUED and BUSY because we
1749 * only want a _single_ thread operating on each connection at any
1750 * point in time, but we want to use all available CPUs.
1751 *
1752 * The worker thread only proceeds if it can atomically set BUSY. It
1753 * clears QUEUED and does it's thing. When it thinks it's done, it
1754 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1755 * (tries again to set BUSY).
1756 *
1757 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1758 * try to queue work. If that fails (work is already queued, or BUSY)
1759 * we give up (work also already being done or is queued) but leave QUEUED
1760 * set so that the worker thread will loop if necessary.
1761 */
1762static void queue_con(struct ceph_connection *con)
1763{
1764 if (test_bit(DEAD, &con->state)) {
1765 dout("queue_con %p ignoring: DEAD\n",
1766 con);
1767 return;
1768 }
1769
1770 if (!con->ops->get(con)) {
1771 dout("queue_con %p ref count 0\n", con);
1772 return;
1773 }
1774
1775 set_bit(QUEUED, &con->state);
1776 if (test_bit(BUSY, &con->state)) {
1777 dout("queue_con %p - already BUSY\n", con);
1778 con->ops->put(con);
1779 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1780 dout("queue_con %p - already queued\n", con);
1781 con->ops->put(con);
1782 } else {
1783 dout("queue_con %p\n", con);
1784 }
1785}
1786
1787/*
1788 * Do some work on a connection. Drop a connection ref when we're done.
1789 */
1790static void con_work(struct work_struct *work)
1791{
1792 struct ceph_connection *con = container_of(work, struct ceph_connection,
1793 work.work);
1794 int backoff = 0;
1795
1796more:
1797 if (test_and_set_bit(BUSY, &con->state) != 0) {
1798 dout("con_work %p BUSY already set\n", con);
1799 goto out;
1800 }
1801 dout("con_work %p start, clearing QUEUED\n", con);
1802 clear_bit(QUEUED, &con->state);
1803
1804 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1805 dout("con_work CLOSED\n");
1806 con_close_socket(con);
1807 goto done;
1808 }
1809 if (test_and_clear_bit(OPENING, &con->state)) {
1810 /* reopen w/ new peer */
1811 dout("con_work OPENING\n");
1812 con_close_socket(con);
1813 }
1814
1815 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1816 try_read(con) < 0 ||
1817 try_write(con) < 0) {
1818 backoff = 1;
1819 ceph_fault(con); /* error/fault path */
1820 }
1821
1822done:
1823 clear_bit(BUSY, &con->state);
1824 dout("con->state=%lu\n", con->state);
1825 if (test_bit(QUEUED, &con->state)) {
Sage Weile2663ab2010-02-16 22:01:03 -08001826 if (!backoff || test_bit(OPENING, &con->state)) {
Sage Weil31b80062009-10-06 11:31:13 -07001827 dout("con_work %p QUEUED reset, looping\n", con);
1828 goto more;
1829 }
1830 dout("con_work %p QUEUED reset, but just faulted\n", con);
1831 clear_bit(QUEUED, &con->state);
1832 }
1833 dout("con_work %p done\n", con);
1834
1835out:
1836 con->ops->put(con);
1837}
1838
1839
1840/*
1841 * Generic error/fault handler. A retry mechanism is used with
1842 * exponential backoff
1843 */
1844static void ceph_fault(struct ceph_connection *con)
1845{
1846 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1847 pr_addr(&con->peer_addr.in_addr), con->error_msg);
1848 dout("fault %p state %lu to peer %s\n",
1849 con, con->state, pr_addr(&con->peer_addr.in_addr));
1850
1851 if (test_bit(LOSSYTX, &con->state)) {
1852 dout("fault on LOSSYTX channel\n");
1853 goto out;
1854 }
1855
Sage Weilec302642009-12-22 10:43:42 -08001856 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08001857 if (test_bit(CLOSED, &con->state))
1858 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08001859
Sage Weil31b80062009-10-06 11:31:13 -07001860 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08001861
1862 if (con->in_msg) {
1863 ceph_msg_put(con->in_msg);
1864 con->in_msg = NULL;
1865 }
Sage Weil31b80062009-10-06 11:31:13 -07001866
Sage Weile80a52d2010-02-25 12:40:45 -08001867 /* Requeue anything that hasn't been acked */
1868 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001869
Sage Weil31b80062009-10-06 11:31:13 -07001870 /* If there are no messages in the queue, place the connection
1871 * in a STANDBY state (i.e., don't try to reconnect just yet). */
Sage Weil31b80062009-10-06 11:31:13 -07001872 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1873 dout("fault setting STANDBY\n");
1874 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08001875 } else {
1876 /* retry after a delay. */
1877 if (con->delay == 0)
1878 con->delay = BASE_DELAY_INTERVAL;
1879 else if (con->delay < MAX_DELAY_INTERVAL)
1880 con->delay *= 2;
1881 dout("fault queueing %p delay %lu\n", con, con->delay);
1882 con->ops->get(con);
1883 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1884 round_jiffies_relative(con->delay)) == 0)
1885 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001886 }
1887
Sage Weil91e45ce32010-02-15 12:05:09 -08001888out_unlock:
1889 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001890out:
Sage Weil161fd652010-02-25 12:38:57 -08001891 /*
1892 * in case we faulted due to authentication, invalidate our
1893 * current tickets so that we can get new ones.
1894 */
1895 if (con->auth_retry && con->ops->invalidate_authorizer) {
1896 dout("calling invalidate_authorizer()\n");
1897 con->ops->invalidate_authorizer(con);
1898 }
1899
Sage Weil31b80062009-10-06 11:31:13 -07001900 if (con->ops->fault)
1901 con->ops->fault(con);
1902}
1903
1904
1905
1906/*
1907 * create a new messenger instance
1908 */
1909struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1910{
1911 struct ceph_messenger *msgr;
1912
1913 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1914 if (msgr == NULL)
1915 return ERR_PTR(-ENOMEM);
1916
1917 spin_lock_init(&msgr->global_seq_lock);
1918
1919 /* the zero page is needed if a request is "canceled" while the message
1920 * is being written over the socket */
1921 msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1922 if (!msgr->zero_page) {
1923 kfree(msgr);
1924 return ERR_PTR(-ENOMEM);
1925 }
1926 kmap(msgr->zero_page);
1927
1928 if (myaddr)
1929 msgr->inst.addr = *myaddr;
1930
1931 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08001932 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08001933 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08001934 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07001935
1936 dout("messenger_create %p\n", msgr);
1937 return msgr;
1938}
1939
1940void ceph_messenger_destroy(struct ceph_messenger *msgr)
1941{
1942 dout("destroy %p\n", msgr);
1943 kunmap(msgr->zero_page);
1944 __free_page(msgr->zero_page);
1945 kfree(msgr);
1946 dout("destroyed messenger %p\n", msgr);
1947}
1948
1949/*
1950 * Queue up an outgoing message on the given connection.
1951 */
1952void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1953{
1954 if (test_bit(CLOSED, &con->state)) {
1955 dout("con_send %p closed, dropping %p\n", con, msg);
1956 ceph_msg_put(msg);
1957 return;
1958 }
1959
1960 /* set src+dst */
Sage Weil63f2d212009-11-03 15:17:56 -08001961 msg->hdr.src.name = con->msgr->inst.name;
1962 msg->hdr.src.addr = con->msgr->my_enc_addr;
1963 msg->hdr.orig_src = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001964
Sage Weil3ca02ef2010-03-01 15:25:00 -08001965 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
1966
Sage Weil31b80062009-10-06 11:31:13 -07001967 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08001968 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001969 BUG_ON(!list_empty(&msg->list_head));
1970 list_add_tail(&msg->list_head, &con->out_queue);
1971 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
1972 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
1973 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1974 le32_to_cpu(msg->hdr.front_len),
1975 le32_to_cpu(msg->hdr.middle_len),
1976 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08001977 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001978
1979 /* if there wasn't anything waiting to send before, queue
1980 * new work */
1981 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1982 queue_con(con);
1983}
1984
1985/*
1986 * Revoke a message that was previously queued for send
1987 */
1988void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
1989{
Sage Weilec302642009-12-22 10:43:42 -08001990 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001991 if (!list_empty(&msg->list_head)) {
1992 dout("con_revoke %p msg %p\n", con, msg);
1993 list_del_init(&msg->list_head);
1994 ceph_msg_put(msg);
1995 msg->hdr.seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -08001996 if (con->out_msg == msg) {
1997 ceph_msg_put(con->out_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001998 con->out_msg = NULL;
Sage Weilc86a2932009-12-14 14:04:30 -08001999 }
Sage Weil31b80062009-10-06 11:31:13 -07002000 if (con->out_kvec_is_msg) {
2001 con->out_skip = con->out_kvec_bytes;
2002 con->out_kvec_is_msg = false;
2003 }
2004 } else {
2005 dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
2006 }
Sage Weilec302642009-12-22 10:43:42 -08002007 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002008}
2009
2010/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002011 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002012 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002013void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002014{
2015 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002016 if (con->in_msg && con->in_msg == msg) {
2017 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2018 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002019 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2020
2021 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002022 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002023 con->in_base_pos = con->in_base_pos -
2024 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002025 front_len -
2026 middle_len -
2027 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002028 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002029 ceph_msg_put(con->in_msg);
2030 con->in_msg = NULL;
2031 con->in_tag = CEPH_MSGR_TAG_READY;
2032 } else {
2033 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002034 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002035 }
2036 mutex_unlock(&con->mutex);
2037}
2038
2039/*
Sage Weil31b80062009-10-06 11:31:13 -07002040 * Queue a keepalive byte to ensure the tcp connection is alive.
2041 */
2042void ceph_con_keepalive(struct ceph_connection *con)
2043{
2044 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2045 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2046 queue_con(con);
2047}
2048
2049
2050/*
2051 * construct a new message with given type, size
2052 * the new msg has a ref count of 1.
2053 */
2054struct ceph_msg *ceph_msg_new(int type, int front_len,
2055 int page_len, int page_off, struct page **pages)
2056{
2057 struct ceph_msg *m;
2058
2059 m = kmalloc(sizeof(*m), GFP_NOFS);
2060 if (m == NULL)
2061 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002062 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07002063 INIT_LIST_HEAD(&m->list_head);
2064
2065 m->hdr.type = cpu_to_le16(type);
2066 m->hdr.front_len = cpu_to_le32(front_len);
2067 m->hdr.middle_len = 0;
2068 m->hdr.data_len = cpu_to_le32(page_len);
2069 m->hdr.data_off = cpu_to_le16(page_off);
2070 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2071 m->footer.front_crc = 0;
2072 m->footer.middle_crc = 0;
2073 m->footer.data_crc = 0;
2074 m->front_max = front_len;
2075 m->front_is_vmalloc = false;
2076 m->more_to_follow = false;
2077 m->pool = NULL;
2078
2079 /* front */
2080 if (front_len) {
2081 if (front_len > PAGE_CACHE_SIZE) {
2082 m->front.iov_base = __vmalloc(front_len, GFP_NOFS,
2083 PAGE_KERNEL);
2084 m->front_is_vmalloc = true;
2085 } else {
2086 m->front.iov_base = kmalloc(front_len, GFP_NOFS);
2087 }
2088 if (m->front.iov_base == NULL) {
2089 pr_err("msg_new can't allocate %d bytes\n",
2090 front_len);
2091 goto out2;
2092 }
2093 } else {
2094 m->front.iov_base = NULL;
2095 }
2096 m->front.iov_len = front_len;
2097
2098 /* middle */
2099 m->middle = NULL;
2100
2101 /* data */
2102 m->nr_pages = calc_pages_for(page_off, page_len);
2103 m->pages = pages;
Sage Weil58bb3b32009-12-23 12:12:31 -08002104 m->pagelist = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002105
2106 dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len,
2107 m->nr_pages);
2108 return m;
2109
2110out2:
2111 ceph_msg_put(m);
2112out:
2113 pr_err("msg_new can't create type %d len %d\n", type, front_len);
2114 return ERR_PTR(-ENOMEM);
2115}
2116
2117/*
Sage Weil31b80062009-10-06 11:31:13 -07002118 * Allocate "middle" portion of a message, if it is needed and wasn't
2119 * allocated by alloc_msg. This allows us to read a small fixed-size
2120 * per-type header in the front and then gracefully fail (i.e.,
2121 * propagate the error to the caller based on info in the front) when
2122 * the middle is too large.
2123 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002124static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002125{
2126 int type = le16_to_cpu(msg->hdr.type);
2127 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2128
2129 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2130 ceph_msg_type_name(type), middle_len);
2131 BUG_ON(!middle_len);
2132 BUG_ON(msg->middle);
2133
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002134 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002135 if (!msg->middle)
2136 return -ENOMEM;
2137 return 0;
2138}
2139
Yehuda Sadeh24504182010-01-08 13:58:34 -08002140/*
2141 * Generic message allocator, for incoming messages.
2142 */
2143static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2144 struct ceph_msg_header *hdr,
2145 int *skip)
2146{
2147 int type = le16_to_cpu(hdr->type);
2148 int front_len = le32_to_cpu(hdr->front_len);
2149 int middle_len = le32_to_cpu(hdr->middle_len);
2150 struct ceph_msg *msg = NULL;
2151 int ret;
2152
2153 if (con->ops->alloc_msg) {
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002154 mutex_unlock(&con->mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002155 msg = con->ops->alloc_msg(con, hdr, skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002156 mutex_lock(&con->mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002157 if (IS_ERR(msg))
2158 return msg;
2159
2160 if (*skip)
2161 return NULL;
2162 }
2163 if (!msg) {
2164 *skip = 0;
2165 msg = ceph_msg_new(type, front_len, 0, 0, NULL);
2166 if (!msg) {
2167 pr_err("unable to allocate msg type %d len %d\n",
2168 type, front_len);
2169 return ERR_PTR(-ENOMEM);
2170 }
2171 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002172 memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002173
2174 if (middle_len) {
2175 ret = ceph_alloc_middle(con, msg);
2176
2177 if (ret < 0) {
2178 ceph_msg_put(msg);
2179 return msg;
2180 }
2181 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002182
Yehuda Sadeh24504182010-01-08 13:58:34 -08002183 return msg;
2184}
2185
Sage Weil31b80062009-10-06 11:31:13 -07002186
2187/*
2188 * Free a generically kmalloc'd message.
2189 */
2190void ceph_msg_kfree(struct ceph_msg *m)
2191{
2192 dout("msg_kfree %p\n", m);
2193 if (m->front_is_vmalloc)
2194 vfree(m->front.iov_base);
2195 else
2196 kfree(m->front.iov_base);
2197 kfree(m);
2198}
2199
2200/*
2201 * Drop a msg ref. Destroy as needed.
2202 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002203void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002204{
Sage Weilc2e552e2009-12-07 15:55:05 -08002205 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002206
Sage Weilc2e552e2009-12-07 15:55:05 -08002207 dout("ceph_msg_put last one on %p\n", m);
2208 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002209
Sage Weilc2e552e2009-12-07 15:55:05 -08002210 /* drop middle, data, if any */
2211 if (m->middle) {
2212 ceph_buffer_put(m->middle);
2213 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002214 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002215 m->nr_pages = 0;
2216 m->pages = NULL;
2217
Sage Weil58bb3b32009-12-23 12:12:31 -08002218 if (m->pagelist) {
2219 ceph_pagelist_release(m->pagelist);
2220 kfree(m->pagelist);
2221 m->pagelist = NULL;
2222 }
2223
Sage Weilc2e552e2009-12-07 15:55:05 -08002224 if (m->pool)
2225 ceph_msgpool_put(m->pool, m);
2226 else
2227 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002228}
Sage Weil9ec7cab2009-12-14 15:13:47 -08002229
2230void ceph_msg_dump(struct ceph_msg *msg)
2231{
2232 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2233 msg->front_max, msg->nr_pages);
2234 print_hex_dump(KERN_DEBUG, "header: ",
2235 DUMP_PREFIX_OFFSET, 16, 1,
2236 &msg->hdr, sizeof(msg->hdr), true);
2237 print_hex_dump(KERN_DEBUG, " front: ",
2238 DUMP_PREFIX_OFFSET, 16, 1,
2239 msg->front.iov_base, msg->front.iov_len, true);
2240 if (msg->middle)
2241 print_hex_dump(KERN_DEBUG, "middle: ",
2242 DUMP_PREFIX_OFFSET, 16, 1,
2243 msg->middle->vec.iov_base,
2244 msg->middle->vec.iov_len, true);
2245 print_hex_dump(KERN_DEBUG, "footer: ",
2246 DUMP_PREFIX_OFFSET, 16, 1,
2247 &msg->footer, sizeof(msg->footer), true);
2248}