blob: 1360708d7505b8a2b748c71af8683c931ef88b92 [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 Weil31b80062009-10-06 11:31:13 -0700335}
336
337/*
338 * mark a peer down. drop any open connections.
339 */
340void ceph_con_close(struct ceph_connection *con)
341{
342 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
343 set_bit(CLOSED, &con->state); /* in case there's queued work */
344 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Sage Weilec302642009-12-22 10:43:42 -0800345 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700346 reset_connection(con);
Sage Weilec302642009-12-22 10:43:42 -0800347 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700348 queue_con(con);
349}
350
351/*
Sage Weil31b80062009-10-06 11:31:13 -0700352 * Reopen a closed connection, with a new peer address.
353 */
354void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
355{
356 dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
357 set_bit(OPENING, &con->state);
358 clear_bit(CLOSED, &con->state);
359 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800360 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700361 queue_con(con);
362}
363
364/*
365 * generic get/put
366 */
367struct ceph_connection *ceph_con_get(struct ceph_connection *con)
368{
369 dout("con_get %p nref = %d -> %d\n", con,
370 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
371 if (atomic_inc_not_zero(&con->nref))
372 return con;
373 return NULL;
374}
375
376void ceph_con_put(struct ceph_connection *con)
377{
378 dout("con_put %p nref = %d -> %d\n", con,
379 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
380 BUG_ON(atomic_read(&con->nref) == 0);
381 if (atomic_dec_and_test(&con->nref)) {
Sage Weil71ececd2009-11-18 11:27:06 -0800382 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700383 kfree(con);
384 }
385}
386
387/*
388 * initialize a new connection.
389 */
390void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
391{
392 dout("con_init %p\n", con);
393 memset(con, 0, sizeof(*con));
394 atomic_set(&con->nref, 1);
395 con->msgr = msgr;
Sage Weilec302642009-12-22 10:43:42 -0800396 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700397 INIT_LIST_HEAD(&con->out_queue);
398 INIT_LIST_HEAD(&con->out_sent);
399 INIT_DELAYED_WORK(&con->work, con_work);
400}
401
402
403/*
404 * We maintain a global counter to order connection attempts. Get
405 * a unique seq greater than @gt.
406 */
407static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
408{
409 u32 ret;
410
411 spin_lock(&msgr->global_seq_lock);
412 if (msgr->global_seq < gt)
413 msgr->global_seq = gt;
414 ret = ++msgr->global_seq;
415 spin_unlock(&msgr->global_seq_lock);
416 return ret;
417}
418
419
420/*
421 * Prepare footer for currently outgoing message, and finish things
422 * off. Assumes out_kvec* are already valid.. we just add on to the end.
423 */
424static void prepare_write_message_footer(struct ceph_connection *con, int v)
425{
426 struct ceph_msg *m = con->out_msg;
427
428 dout("prepare_write_message_footer %p\n", con);
429 con->out_kvec_is_msg = true;
430 con->out_kvec[v].iov_base = &m->footer;
431 con->out_kvec[v].iov_len = sizeof(m->footer);
432 con->out_kvec_bytes += sizeof(m->footer);
433 con->out_kvec_left++;
434 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800435 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700436}
437
438/*
439 * Prepare headers for the next outgoing message.
440 */
441static void prepare_write_message(struct ceph_connection *con)
442{
443 struct ceph_msg *m;
444 int v = 0;
445
446 con->out_kvec_bytes = 0;
447 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800448 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700449
450 /* Sneak an ack in there first? If we can get it into the same
451 * TCP packet that's a good thing. */
452 if (con->in_seq > con->in_seq_acked) {
453 con->in_seq_acked = con->in_seq;
454 con->out_kvec[v].iov_base = &tag_ack;
455 con->out_kvec[v++].iov_len = 1;
456 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
457 con->out_kvec[v].iov_base = &con->out_temp_ack;
458 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
459 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
460 }
461
Sage Weil31b80062009-10-06 11:31:13 -0700462 m = list_first_entry(&con->out_queue,
463 struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800464 con->out_msg = m;
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800465 if (test_bit(LOSSYTX, &con->state)) {
466 /* put message on sent list */
467 ceph_msg_get(m);
468 list_move_tail(&m->list_head, &con->out_sent);
469 } else {
470 list_del_init(&m->list_head);
471 }
Sage Weil31b80062009-10-06 11:31:13 -0700472
473 m->hdr.seq = cpu_to_le64(++con->out_seq);
474
475 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
476 m, con->out_seq, le16_to_cpu(m->hdr.type),
477 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
478 le32_to_cpu(m->hdr.data_len),
479 m->nr_pages);
480 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
481
482 /* tag + hdr + front + middle */
483 con->out_kvec[v].iov_base = &tag_msg;
484 con->out_kvec[v++].iov_len = 1;
485 con->out_kvec[v].iov_base = &m->hdr;
486 con->out_kvec[v++].iov_len = sizeof(m->hdr);
487 con->out_kvec[v++] = m->front;
488 if (m->middle)
489 con->out_kvec[v++] = m->middle->vec;
490 con->out_kvec_left = v;
491 con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
492 (m->middle ? m->middle->vec.iov_len : 0);
493 con->out_kvec_cur = con->out_kvec;
494
495 /* fill in crc (except data pages), footer */
496 con->out_msg->hdr.crc =
497 cpu_to_le32(crc32c(0, (void *)&m->hdr,
498 sizeof(m->hdr) - sizeof(m->hdr.crc)));
499 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
500 con->out_msg->footer.front_crc =
501 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
502 if (m->middle)
503 con->out_msg->footer.middle_crc =
504 cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
505 m->middle->vec.iov_len));
506 else
507 con->out_msg->footer.middle_crc = 0;
508 con->out_msg->footer.data_crc = 0;
509 dout("prepare_write_message front_crc %u data_crc %u\n",
510 le32_to_cpu(con->out_msg->footer.front_crc),
511 le32_to_cpu(con->out_msg->footer.middle_crc));
512
513 /* is there a data payload? */
514 if (le32_to_cpu(m->hdr.data_len) > 0) {
515 /* initialize page iterator */
516 con->out_msg_pos.page = 0;
517 con->out_msg_pos.page_pos =
518 le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
519 con->out_msg_pos.data_pos = 0;
520 con->out_msg_pos.did_page_crc = 0;
521 con->out_more = 1; /* data + footer will follow */
522 } else {
523 /* no, queue up footer too and be done */
524 prepare_write_message_footer(con, v);
525 }
526
527 set_bit(WRITE_PENDING, &con->state);
528}
529
530/*
531 * Prepare an ack.
532 */
533static void prepare_write_ack(struct ceph_connection *con)
534{
535 dout("prepare_write_ack %p %llu -> %llu\n", con,
536 con->in_seq_acked, con->in_seq);
537 con->in_seq_acked = con->in_seq;
538
539 con->out_kvec[0].iov_base = &tag_ack;
540 con->out_kvec[0].iov_len = 1;
541 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
542 con->out_kvec[1].iov_base = &con->out_temp_ack;
543 con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
544 con->out_kvec_left = 2;
545 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
546 con->out_kvec_cur = con->out_kvec;
547 con->out_more = 1; /* more will follow.. eventually.. */
548 set_bit(WRITE_PENDING, &con->state);
549}
550
551/*
552 * Prepare to write keepalive byte.
553 */
554static void prepare_write_keepalive(struct ceph_connection *con)
555{
556 dout("prepare_write_keepalive %p\n", con);
557 con->out_kvec[0].iov_base = &tag_keepalive;
558 con->out_kvec[0].iov_len = 1;
559 con->out_kvec_left = 1;
560 con->out_kvec_bytes = 1;
561 con->out_kvec_cur = con->out_kvec;
562 set_bit(WRITE_PENDING, &con->state);
563}
564
565/*
566 * Connection negotiation.
567 */
568
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800569static void prepare_connect_authorizer(struct ceph_connection *con)
570{
571 void *auth_buf;
572 int auth_len = 0;
573 int auth_protocol = 0;
574
Sage Weilec302642009-12-22 10:43:42 -0800575 mutex_unlock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800576 if (con->ops->get_authorizer)
577 con->ops->get_authorizer(con, &auth_buf, &auth_len,
578 &auth_protocol, &con->auth_reply_buf,
579 &con->auth_reply_buf_len,
580 con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800581 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800582
583 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
584 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
585
586 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
587 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
588 con->out_kvec_left++;
589 con->out_kvec_bytes += auth_len;
590}
591
Sage Weil31b80062009-10-06 11:31:13 -0700592/*
593 * We connected to a peer and are saying hello.
594 */
Sage Weileed0ef22009-11-10 14:34:36 -0800595static void prepare_write_banner(struct ceph_messenger *msgr,
596 struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700597{
598 int len = strlen(CEPH_BANNER);
Sage Weileed0ef22009-11-10 14:34:36 -0800599
600 con->out_kvec[0].iov_base = CEPH_BANNER;
601 con->out_kvec[0].iov_len = len;
602 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
603 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
604 con->out_kvec_left = 2;
605 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
606 con->out_kvec_cur = con->out_kvec;
607 con->out_more = 0;
608 set_bit(WRITE_PENDING, &con->state);
609}
610
611static void prepare_write_connect(struct ceph_messenger *msgr,
612 struct ceph_connection *con,
613 int after_banner)
614{
Sage Weil31b80062009-10-06 11:31:13 -0700615 unsigned global_seq = get_global_seq(con->msgr, 0);
616 int proto;
617
618 switch (con->peer_name.type) {
619 case CEPH_ENTITY_TYPE_MON:
620 proto = CEPH_MONC_PROTOCOL;
621 break;
622 case CEPH_ENTITY_TYPE_OSD:
623 proto = CEPH_OSDC_PROTOCOL;
624 break;
625 case CEPH_ENTITY_TYPE_MDS:
626 proto = CEPH_MDSC_PROTOCOL;
627 break;
628 default:
629 BUG();
630 }
631
632 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
633 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800634
Sage Weil04a419f2009-12-23 09:30:21 -0800635 con->out_connect.features = CEPH_FEATURE_SUPPORTED;
Sage Weil31b80062009-10-06 11:31:13 -0700636 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
637 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
638 con->out_connect.global_seq = cpu_to_le32(global_seq);
639 con->out_connect.protocol_version = cpu_to_le32(proto);
640 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700641
Sage Weileed0ef22009-11-10 14:34:36 -0800642 if (!after_banner) {
643 con->out_kvec_left = 0;
644 con->out_kvec_bytes = 0;
645 }
646 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
647 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
648 con->out_kvec_left++;
649 con->out_kvec_bytes += sizeof(con->out_connect);
Sage Weil31b80062009-10-06 11:31:13 -0700650 con->out_kvec_cur = con->out_kvec;
651 con->out_more = 0;
652 set_bit(WRITE_PENDING, &con->state);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800653
654 prepare_connect_authorizer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700655}
656
657
658/*
659 * write as much of pending kvecs to the socket as we can.
660 * 1 -> done
661 * 0 -> socket full, but more to do
662 * <0 -> error
663 */
664static int write_partial_kvec(struct ceph_connection *con)
665{
666 int ret;
667
668 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
669 while (con->out_kvec_bytes > 0) {
670 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
671 con->out_kvec_left, con->out_kvec_bytes,
672 con->out_more);
673 if (ret <= 0)
674 goto out;
675 con->out_kvec_bytes -= ret;
676 if (con->out_kvec_bytes == 0)
677 break; /* done */
678 while (ret > 0) {
679 if (ret >= con->out_kvec_cur->iov_len) {
680 ret -= con->out_kvec_cur->iov_len;
681 con->out_kvec_cur++;
682 con->out_kvec_left--;
683 } else {
684 con->out_kvec_cur->iov_len -= ret;
685 con->out_kvec_cur->iov_base += ret;
686 ret = 0;
687 break;
688 }
689 }
690 }
691 con->out_kvec_left = 0;
692 con->out_kvec_is_msg = false;
693 ret = 1;
694out:
695 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
696 con->out_kvec_bytes, con->out_kvec_left, ret);
697 return ret; /* done! */
698}
699
700/*
701 * Write as much message data payload as we can. If we finish, queue
702 * up the footer.
703 * 1 -> done, footer is now queued in out_kvec[].
704 * 0 -> socket full, but more to do
705 * <0 -> error
706 */
707static int write_partial_msg_pages(struct ceph_connection *con)
708{
709 struct ceph_msg *msg = con->out_msg;
710 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
711 size_t len;
712 int crc = con->msgr->nocrc;
713 int ret;
714
715 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
716 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
717 con->out_msg_pos.page_pos);
718
719 while (con->out_msg_pos.page < con->out_msg->nr_pages) {
720 struct page *page = NULL;
721 void *kaddr = NULL;
722
723 /*
724 * if we are calculating the data crc (the default), we need
725 * to map the page. if our pages[] has been revoked, use the
726 * zero page.
727 */
728 if (msg->pages) {
729 page = msg->pages[con->out_msg_pos.page];
730 if (crc)
731 kaddr = kmap(page);
Sage Weil58bb3b32009-12-23 12:12:31 -0800732 } else if (msg->pagelist) {
733 page = list_first_entry(&msg->pagelist->head,
734 struct page, lru);
735 if (crc)
736 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700737 } else {
738 page = con->msgr->zero_page;
739 if (crc)
740 kaddr = page_address(con->msgr->zero_page);
741 }
742 len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
743 (int)(data_len - con->out_msg_pos.data_pos));
744 if (crc && !con->out_msg_pos.did_page_crc) {
745 void *base = kaddr + con->out_msg_pos.page_pos;
746 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
747
748 BUG_ON(kaddr == NULL);
749 con->out_msg->footer.data_crc =
750 cpu_to_le32(crc32c(tmpcrc, base, len));
751 con->out_msg_pos.did_page_crc = 1;
752 }
753
754 ret = kernel_sendpage(con->sock, page,
755 con->out_msg_pos.page_pos, len,
756 MSG_DONTWAIT | MSG_NOSIGNAL |
757 MSG_MORE);
758
Sage Weil58bb3b32009-12-23 12:12:31 -0800759 if (crc && (msg->pages || msg->pagelist))
Sage Weil31b80062009-10-06 11:31:13 -0700760 kunmap(page);
761
762 if (ret <= 0)
763 goto out;
764
765 con->out_msg_pos.data_pos += ret;
766 con->out_msg_pos.page_pos += ret;
767 if (ret == len) {
768 con->out_msg_pos.page_pos = 0;
769 con->out_msg_pos.page++;
770 con->out_msg_pos.did_page_crc = 0;
Sage Weil58bb3b32009-12-23 12:12:31 -0800771 if (msg->pagelist)
772 list_move_tail(&page->lru,
773 &msg->pagelist->head);
Sage Weil31b80062009-10-06 11:31:13 -0700774 }
775 }
776
777 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
778
779 /* prepare and queue up footer, too */
780 if (!crc)
781 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
782 con->out_kvec_bytes = 0;
783 con->out_kvec_left = 0;
784 con->out_kvec_cur = con->out_kvec;
785 prepare_write_message_footer(con, 0);
786 ret = 1;
787out:
788 return ret;
789}
790
791/*
792 * write some zeros
793 */
794static int write_partial_skip(struct ceph_connection *con)
795{
796 int ret;
797
798 while (con->out_skip > 0) {
799 struct kvec iov = {
800 .iov_base = page_address(con->msgr->zero_page),
801 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
802 };
803
804 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
805 if (ret <= 0)
806 goto out;
807 con->out_skip -= ret;
808 }
809 ret = 1;
810out:
811 return ret;
812}
813
814/*
815 * Prepare to read connection handshake, or an ack.
816 */
Sage Weileed0ef22009-11-10 14:34:36 -0800817static void prepare_read_banner(struct ceph_connection *con)
818{
819 dout("prepare_read_banner %p\n", con);
820 con->in_base_pos = 0;
821}
822
Sage Weil31b80062009-10-06 11:31:13 -0700823static void prepare_read_connect(struct ceph_connection *con)
824{
825 dout("prepare_read_connect %p\n", con);
826 con->in_base_pos = 0;
827}
828
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800829static void prepare_read_connect_retry(struct ceph_connection *con)
830{
831 dout("prepare_read_connect_retry %p\n", con);
832 con->in_base_pos = strlen(CEPH_BANNER) + sizeof(con->actual_peer_addr)
833 + sizeof(con->peer_addr_for_me);
834}
835
Sage Weil31b80062009-10-06 11:31:13 -0700836static void prepare_read_ack(struct ceph_connection *con)
837{
838 dout("prepare_read_ack %p\n", con);
839 con->in_base_pos = 0;
840}
841
842static void prepare_read_tag(struct ceph_connection *con)
843{
844 dout("prepare_read_tag %p\n", con);
845 con->in_base_pos = 0;
846 con->in_tag = CEPH_MSGR_TAG_READY;
847}
848
849/*
850 * Prepare to read a message.
851 */
852static int prepare_read_message(struct ceph_connection *con)
853{
854 dout("prepare_read_message %p\n", con);
855 BUG_ON(con->in_msg != NULL);
856 con->in_base_pos = 0;
857 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
858 return 0;
859}
860
861
862static int read_partial(struct ceph_connection *con,
863 int *to, int size, void *object)
864{
865 *to += size;
866 while (con->in_base_pos < *to) {
867 int left = *to - con->in_base_pos;
868 int have = size - left;
869 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
870 if (ret <= 0)
871 return ret;
872 con->in_base_pos += ret;
873 }
874 return 1;
875}
876
877
878/*
879 * Read all or part of the connect-side handshake on a new connection
880 */
Sage Weileed0ef22009-11-10 14:34:36 -0800881static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700882{
883 int ret, to = 0;
884
Sage Weileed0ef22009-11-10 14:34:36 -0800885 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -0700886
887 /* peer's banner */
888 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
889 if (ret <= 0)
890 goto out;
891 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
892 &con->actual_peer_addr);
893 if (ret <= 0)
894 goto out;
895 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
896 &con->peer_addr_for_me);
897 if (ret <= 0)
898 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -0800899out:
900 return ret;
901}
902
903static int read_partial_connect(struct ceph_connection *con)
904{
905 int ret, to = 0;
906
907 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
908
Sage Weil31b80062009-10-06 11:31:13 -0700909 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
910 if (ret <= 0)
911 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800912 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
913 con->auth_reply_buf);
914 if (ret <= 0)
915 goto out;
Sage Weil31b80062009-10-06 11:31:13 -0700916
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800917 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
918 con, (int)con->in_reply.tag,
919 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -0700920 le32_to_cpu(con->in_reply.global_seq));
921out:
922 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -0800923
Sage Weil31b80062009-10-06 11:31:13 -0700924}
925
926/*
927 * Verify the hello banner looks okay.
928 */
929static int verify_hello(struct ceph_connection *con)
930{
931 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -0700932 pr_err("connect to %s got bad banner\n",
Sage Weil31b80062009-10-06 11:31:13 -0700933 pr_addr(&con->peer_addr.in_addr));
934 con->error_msg = "protocol error, bad banner";
935 return -1;
936 }
937 return 0;
938}
939
940static bool addr_is_blank(struct sockaddr_storage *ss)
941{
942 switch (ss->ss_family) {
943 case AF_INET:
944 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
945 case AF_INET6:
946 return
947 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
948 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
949 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
950 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
951 }
952 return false;
953}
954
955static int addr_port(struct sockaddr_storage *ss)
956{
957 switch (ss->ss_family) {
958 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800959 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -0700960 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800961 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -0700962 }
963 return 0;
964}
965
966static void addr_set_port(struct sockaddr_storage *ss, int p)
967{
968 switch (ss->ss_family) {
969 case AF_INET:
970 ((struct sockaddr_in *)ss)->sin_port = htons(p);
971 case AF_INET6:
972 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
973 }
974}
975
976/*
977 * Parse an ip[:port] list into an addr array. Use the default
978 * monitor port if a port isn't specified.
979 */
980int ceph_parse_ips(const char *c, const char *end,
981 struct ceph_entity_addr *addr,
982 int max_count, int *count)
983{
984 int i;
985 const char *p = c;
986
987 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
988 for (i = 0; i < max_count; i++) {
989 const char *ipend;
990 struct sockaddr_storage *ss = &addr[i].in_addr;
991 struct sockaddr_in *in4 = (void *)ss;
992 struct sockaddr_in6 *in6 = (void *)ss;
993 int port;
994
995 memset(ss, 0, sizeof(*ss));
996 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
997 ',', &ipend)) {
998 ss->ss_family = AF_INET;
999 } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
1000 ',', &ipend)) {
1001 ss->ss_family = AF_INET6;
1002 } else {
1003 goto bad;
1004 }
1005 p = ipend;
1006
1007 /* port? */
1008 if (p < end && *p == ':') {
1009 port = 0;
1010 p++;
1011 while (p < end && *p >= '0' && *p <= '9') {
1012 port = (port * 10) + (*p - '0');
1013 p++;
1014 }
1015 if (port > 65535 || port == 0)
1016 goto bad;
1017 } else {
1018 port = CEPH_MON_PORT;
1019 }
1020
1021 addr_set_port(ss, port);
1022
1023 dout("parse_ips got %s\n", pr_addr(ss));
1024
1025 if (p == end)
1026 break;
1027 if (*p != ',')
1028 goto bad;
1029 p++;
1030 }
1031
1032 if (p != end)
1033 goto bad;
1034
1035 if (count)
1036 *count = i + 1;
1037 return 0;
1038
1039bad:
1040 pr_err("parse_ips bad ip '%s'\n", c);
1041 return -EINVAL;
1042}
1043
Sage Weileed0ef22009-11-10 14:34:36 -08001044static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001045{
Sage Weileed0ef22009-11-10 14:34:36 -08001046 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001047
1048 if (verify_hello(con) < 0)
1049 return -1;
1050
Sage Weil63f2d212009-11-03 15:17:56 -08001051 ceph_decode_addr(&con->actual_peer_addr);
1052 ceph_decode_addr(&con->peer_addr_for_me);
1053
Sage Weil31b80062009-10-06 11:31:13 -07001054 /*
1055 * Make sure the other end is who we wanted. note that the other
1056 * end may not yet know their ip address, so if it's 0.0.0.0, give
1057 * them the benefit of the doubt.
1058 */
Sage Weil103e2d32010-01-07 16:12:36 -08001059 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1060 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001061 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1062 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Sage Weil103e2d32010-01-07 16:12:36 -08001063 pr_warning("wrong peer, want %s/%lld, got %s/%lld\n",
1064 pr_addr(&con->peer_addr.in_addr),
1065 le64_to_cpu(con->peer_addr.nonce),
1066 pr_addr(&con->actual_peer_addr.in_addr),
1067 le64_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001068 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001069 return -1;
1070 }
1071
1072 /*
1073 * did we learn our address?
1074 */
1075 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1076 int port = addr_port(&con->msgr->inst.addr.in_addr);
1077
1078 memcpy(&con->msgr->inst.addr.in_addr,
1079 &con->peer_addr_for_me.in_addr,
1080 sizeof(con->peer_addr_for_me.in_addr));
1081 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001082 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001083 dout("process_banner learned my addr is %s\n",
Sage Weil31b80062009-10-06 11:31:13 -07001084 pr_addr(&con->msgr->inst.addr.in_addr));
1085 }
1086
Sage Weileed0ef22009-11-10 14:34:36 -08001087 set_bit(NEGOTIATING, &con->state);
1088 prepare_read_connect(con);
1089 return 0;
1090}
1091
Sage Weil04a419f2009-12-23 09:30:21 -08001092static void fail_protocol(struct ceph_connection *con)
1093{
1094 reset_connection(con);
1095 set_bit(CLOSED, &con->state); /* in case there's queued work */
1096
1097 mutex_unlock(&con->mutex);
1098 if (con->ops->bad_proto)
1099 con->ops->bad_proto(con);
1100 mutex_lock(&con->mutex);
1101}
1102
Sage Weileed0ef22009-11-10 14:34:36 -08001103static int process_connect(struct ceph_connection *con)
1104{
Sage Weil04a419f2009-12-23 09:30:21 -08001105 u64 sup_feat = CEPH_FEATURE_SUPPORTED;
1106 u64 req_feat = CEPH_FEATURE_REQUIRED;
1107 u64 server_feat = le64_to_cpu(con->in_reply.features);
1108
Sage Weileed0ef22009-11-10 14:34:36 -08001109 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1110
Sage Weil31b80062009-10-06 11:31:13 -07001111 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001112 case CEPH_MSGR_TAG_FEATURES:
1113 pr_err("%s%lld %s feature set mismatch,"
1114 " my %llx < server's %llx, missing %llx\n",
1115 ENTITY_NAME(con->peer_name),
1116 pr_addr(&con->peer_addr.in_addr),
1117 sup_feat, server_feat, server_feat & ~sup_feat);
1118 con->error_msg = "missing required protocol features";
1119 fail_protocol(con);
1120 return -1;
1121
Sage Weil31b80062009-10-06 11:31:13 -07001122 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001123 pr_err("%s%lld %s protocol version mismatch,"
1124 " my %d != server's %d\n",
1125 ENTITY_NAME(con->peer_name),
1126 pr_addr(&con->peer_addr.in_addr),
1127 le32_to_cpu(con->out_connect.protocol_version),
1128 le32_to_cpu(con->in_reply.protocol_version));
1129 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001130 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001131 return -1;
1132
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001133 case CEPH_MSGR_TAG_BADAUTHORIZER:
1134 con->auth_retry++;
1135 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1136 con->auth_retry);
1137 if (con->auth_retry == 2) {
1138 con->error_msg = "connect authorization failure";
1139 reset_connection(con);
1140 set_bit(CLOSED, &con->state);
1141 return -1;
1142 }
1143 con->auth_retry = 1;
1144 prepare_write_connect(con->msgr, con, 0);
1145 prepare_read_connect_retry(con);
1146 break;
Sage Weil31b80062009-10-06 11:31:13 -07001147
1148 case CEPH_MSGR_TAG_RESETSESSION:
1149 /*
1150 * If we connected with a large connect_seq but the peer
1151 * has no record of a session with us (no connection, or
1152 * connect_seq == 0), they will send RESETSESION to indicate
1153 * that they must have reset their session, and may have
1154 * dropped messages.
1155 */
1156 dout("process_connect got RESET peer seq %u\n",
1157 le32_to_cpu(con->in_connect.connect_seq));
1158 pr_err("%s%lld %s connection reset\n",
1159 ENTITY_NAME(con->peer_name),
1160 pr_addr(&con->peer_addr.in_addr));
1161 reset_connection(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001162 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001163 prepare_read_connect(con);
1164
1165 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001166 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001167 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1168 if (con->ops->peer_reset)
1169 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001170 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001171 break;
1172
1173 case CEPH_MSGR_TAG_RETRY_SESSION:
1174 /*
1175 * If we sent a smaller connect_seq than the peer has, try
1176 * again with a larger value.
1177 */
1178 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1179 le32_to_cpu(con->out_connect.connect_seq),
1180 le32_to_cpu(con->in_connect.connect_seq));
1181 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Sage Weileed0ef22009-11-10 14:34:36 -08001182 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001183 prepare_read_connect(con);
1184 break;
1185
1186 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1187 /*
1188 * If we sent a smaller global_seq than the peer has, try
1189 * again with a larger value.
1190 */
Sage Weileed0ef22009-11-10 14:34:36 -08001191 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001192 con->peer_global_seq,
1193 le32_to_cpu(con->in_connect.global_seq));
1194 get_global_seq(con->msgr,
1195 le32_to_cpu(con->in_connect.global_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_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001201 if (req_feat & ~server_feat) {
1202 pr_err("%s%lld %s protocol feature mismatch,"
1203 " my required %llx > server's %llx, need %llx\n",
1204 ENTITY_NAME(con->peer_name),
1205 pr_addr(&con->peer_addr.in_addr),
1206 req_feat, server_feat, req_feat & ~server_feat);
1207 con->error_msg = "missing required protocol features";
1208 fail_protocol(con);
1209 return -1;
1210 }
Sage Weil31b80062009-10-06 11:31:13 -07001211 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001212 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1213 con->connect_seq++;
1214 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1215 con->peer_global_seq,
1216 le32_to_cpu(con->in_reply.connect_seq),
1217 con->connect_seq);
1218 WARN_ON(con->connect_seq !=
1219 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001220
1221 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1222 set_bit(LOSSYTX, &con->state);
1223
Sage Weil31b80062009-10-06 11:31:13 -07001224 prepare_read_tag(con);
1225 break;
1226
1227 case CEPH_MSGR_TAG_WAIT:
1228 /*
1229 * If there is a connection race (we are opening
1230 * connections to each other), one of us may just have
1231 * to WAIT. This shouldn't happen if we are the
1232 * client.
1233 */
1234 pr_err("process_connect peer connecting WAIT\n");
1235
1236 default:
1237 pr_err("connect protocol error, will retry\n");
1238 con->error_msg = "protocol error, garbage tag during connect";
1239 return -1;
1240 }
1241 return 0;
1242}
1243
1244
1245/*
1246 * read (part of) an ack
1247 */
1248static int read_partial_ack(struct ceph_connection *con)
1249{
1250 int to = 0;
1251
1252 return read_partial(con, &to, sizeof(con->in_temp_ack),
1253 &con->in_temp_ack);
1254}
1255
1256
1257/*
1258 * We can finally discard anything that's been acked.
1259 */
1260static void process_ack(struct ceph_connection *con)
1261{
1262 struct ceph_msg *m;
1263 u64 ack = le64_to_cpu(con->in_temp_ack);
1264 u64 seq;
1265
Sage Weil31b80062009-10-06 11:31:13 -07001266 while (!list_empty(&con->out_sent)) {
1267 m = list_first_entry(&con->out_sent, struct ceph_msg,
1268 list_head);
1269 seq = le64_to_cpu(m->hdr.seq);
1270 if (seq > ack)
1271 break;
1272 dout("got ack for seq %llu type %d at %p\n", seq,
1273 le16_to_cpu(m->hdr.type), m);
1274 ceph_msg_remove(m);
1275 }
Sage Weil31b80062009-10-06 11:31:13 -07001276 prepare_read_tag(con);
1277}
1278
1279
1280
1281
1282
1283
1284/*
1285 * read (part of) a message.
1286 */
1287static int read_partial_message(struct ceph_connection *con)
1288{
1289 struct ceph_msg *m = con->in_msg;
1290 void *p;
1291 int ret;
1292 int to, want, left;
1293 unsigned front_len, middle_len, data_len, data_off;
1294 int datacrc = con->msgr->nocrc;
1295
1296 dout("read_partial_message con %p msg %p\n", con, m);
1297
1298 /* header */
1299 while (con->in_base_pos < sizeof(con->in_hdr)) {
1300 left = sizeof(con->in_hdr) - con->in_base_pos;
1301 ret = ceph_tcp_recvmsg(con->sock,
1302 (char *)&con->in_hdr + con->in_base_pos,
1303 left);
1304 if (ret <= 0)
1305 return ret;
1306 con->in_base_pos += ret;
1307 if (con->in_base_pos == sizeof(con->in_hdr)) {
1308 u32 crc = crc32c(0, (void *)&con->in_hdr,
1309 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1310 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1311 pr_err("read_partial_message bad hdr "
1312 " crc %u != expected %u\n",
1313 crc, con->in_hdr.crc);
1314 return -EBADMSG;
1315 }
1316 }
1317 }
1318
1319 front_len = le32_to_cpu(con->in_hdr.front_len);
1320 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1321 return -EIO;
1322 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1323 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1324 return -EIO;
1325 data_len = le32_to_cpu(con->in_hdr.data_len);
1326 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1327 return -EIO;
1328
1329 /* allocate message? */
1330 if (!con->in_msg) {
1331 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1332 con->in_hdr.front_len, con->in_hdr.data_len);
1333 con->in_msg = con->ops->alloc_msg(con, &con->in_hdr);
1334 if (!con->in_msg) {
1335 /* skip this message */
Sage Weilcf3e5c42009-12-11 09:48:05 -08001336 pr_err("alloc_msg returned NULL, skipping message\n");
Sage Weil31b80062009-10-06 11:31:13 -07001337 con->in_base_pos = -front_len - middle_len - data_len -
1338 sizeof(m->footer);
1339 con->in_tag = CEPH_MSGR_TAG_READY;
1340 return 0;
1341 }
1342 if (IS_ERR(con->in_msg)) {
1343 ret = PTR_ERR(con->in_msg);
1344 con->in_msg = NULL;
1345 con->error_msg = "out of memory for incoming message";
1346 return ret;
1347 }
1348 m = con->in_msg;
1349 m->front.iov_len = 0; /* haven't read it yet */
1350 memcpy(&m->hdr, &con->in_hdr, sizeof(con->in_hdr));
1351 }
1352
1353 /* front */
1354 while (m->front.iov_len < front_len) {
1355 BUG_ON(m->front.iov_base == NULL);
1356 left = front_len - m->front.iov_len;
1357 ret = ceph_tcp_recvmsg(con->sock, (char *)m->front.iov_base +
1358 m->front.iov_len, left);
1359 if (ret <= 0)
1360 return ret;
1361 m->front.iov_len += ret;
1362 if (m->front.iov_len == front_len)
1363 con->in_front_crc = crc32c(0, m->front.iov_base,
1364 m->front.iov_len);
1365 }
1366
1367 /* middle */
1368 while (middle_len > 0 && (!m->middle ||
1369 m->middle->vec.iov_len < middle_len)) {
1370 if (m->middle == NULL) {
1371 ret = -EOPNOTSUPP;
1372 if (con->ops->alloc_middle)
1373 ret = con->ops->alloc_middle(con, m);
1374 if (ret < 0) {
Sage Weilcf3e5c42009-12-11 09:48:05 -08001375 pr_err("alloc_middle fail skipping payload\n");
Sage Weil31b80062009-10-06 11:31:13 -07001376 con->in_base_pos = -middle_len - data_len
1377 - sizeof(m->footer);
1378 ceph_msg_put(con->in_msg);
1379 con->in_msg = NULL;
1380 con->in_tag = CEPH_MSGR_TAG_READY;
1381 return 0;
1382 }
1383 m->middle->vec.iov_len = 0;
1384 }
1385 left = middle_len - m->middle->vec.iov_len;
1386 ret = ceph_tcp_recvmsg(con->sock,
1387 (char *)m->middle->vec.iov_base +
1388 m->middle->vec.iov_len, left);
1389 if (ret <= 0)
1390 return ret;
1391 m->middle->vec.iov_len += ret;
1392 if (m->middle->vec.iov_len == middle_len)
1393 con->in_middle_crc = crc32c(0, m->middle->vec.iov_base,
1394 m->middle->vec.iov_len);
1395 }
1396
1397 /* (page) data */
1398 data_off = le16_to_cpu(m->hdr.data_off);
1399 if (data_len == 0)
1400 goto no_data;
1401
1402 if (m->nr_pages == 0) {
1403 con->in_msg_pos.page = 0;
1404 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1405 con->in_msg_pos.data_pos = 0;
1406 /* find pages for data payload */
1407 want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1408 ret = -1;
Sage Weilec302642009-12-22 10:43:42 -08001409 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001410 if (con->ops->prepare_pages)
1411 ret = con->ops->prepare_pages(con, m, want);
Sage Weilec302642009-12-22 10:43:42 -08001412 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001413 if (ret < 0) {
1414 dout("%p prepare_pages failed, skipping payload\n", m);
1415 con->in_base_pos = -data_len - sizeof(m->footer);
1416 ceph_msg_put(con->in_msg);
1417 con->in_msg = NULL;
1418 con->in_tag = CEPH_MSGR_TAG_READY;
1419 return 0;
1420 }
1421 BUG_ON(m->nr_pages < want);
1422 }
1423 while (con->in_msg_pos.data_pos < data_len) {
1424 left = min((int)(data_len - con->in_msg_pos.data_pos),
1425 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1426 BUG_ON(m->pages == NULL);
1427 p = kmap(m->pages[con->in_msg_pos.page]);
1428 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1429 left);
1430 if (ret > 0 && datacrc)
1431 con->in_data_crc =
1432 crc32c(con->in_data_crc,
1433 p + con->in_msg_pos.page_pos, ret);
1434 kunmap(m->pages[con->in_msg_pos.page]);
1435 if (ret <= 0)
1436 return ret;
1437 con->in_msg_pos.data_pos += ret;
1438 con->in_msg_pos.page_pos += ret;
1439 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1440 con->in_msg_pos.page_pos = 0;
1441 con->in_msg_pos.page++;
1442 }
1443 }
1444
1445no_data:
1446 /* footer */
1447 to = sizeof(m->hdr) + sizeof(m->footer);
1448 while (con->in_base_pos < to) {
1449 left = to - con->in_base_pos;
1450 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1451 (con->in_base_pos - sizeof(m->hdr)),
1452 left);
1453 if (ret <= 0)
1454 return ret;
1455 con->in_base_pos += ret;
1456 }
1457 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1458 m, front_len, m->footer.front_crc, middle_len,
1459 m->footer.middle_crc, data_len, m->footer.data_crc);
1460
1461 /* crc ok? */
1462 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1463 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1464 m, con->in_front_crc, m->footer.front_crc);
1465 return -EBADMSG;
1466 }
1467 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1468 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1469 m, con->in_middle_crc, m->footer.middle_crc);
1470 return -EBADMSG;
1471 }
1472 if (datacrc &&
1473 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1474 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1475 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1476 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1477 return -EBADMSG;
1478 }
1479
1480 return 1; /* done! */
1481}
1482
1483/*
1484 * Process message. This happens in the worker thread. The callback should
1485 * be careful not to do anything that waits on other incoming messages or it
1486 * may deadlock.
1487 */
1488static void process_message(struct ceph_connection *con)
1489{
Sage Weil5e095e82009-12-14 14:30:34 -08001490 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001491
Sage Weil5e095e82009-12-14 14:30:34 -08001492 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001493 con->in_msg = NULL;
1494
1495 /* if first message, set peer_name */
1496 if (con->peer_name.type == 0)
1497 con->peer_name = msg->hdr.src.name;
1498
Sage Weil31b80062009-10-06 11:31:13 -07001499 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001500 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001501
1502 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1503 msg, le64_to_cpu(msg->hdr.seq),
1504 ENTITY_NAME(msg->hdr.src.name),
1505 le16_to_cpu(msg->hdr.type),
1506 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1507 le32_to_cpu(msg->hdr.front_len),
1508 le32_to_cpu(msg->hdr.data_len),
1509 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1510 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001511
1512 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001513 prepare_read_tag(con);
1514}
1515
1516
1517/*
1518 * Write something to the socket. Called in a worker thread when the
1519 * socket appears to be writeable and we have something ready to send.
1520 */
1521static int try_write(struct ceph_connection *con)
1522{
1523 struct ceph_messenger *msgr = con->msgr;
1524 int ret = 1;
1525
1526 dout("try_write start %p state %lu nref %d\n", con, con->state,
1527 atomic_read(&con->nref));
1528
Sage Weilec302642009-12-22 10:43:42 -08001529 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001530more:
1531 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1532
1533 /* open the socket first? */
1534 if (con->sock == NULL) {
1535 /*
1536 * if we were STANDBY and are reconnecting _this_
1537 * connection, bump connect_seq now. Always bump
1538 * global_seq.
1539 */
1540 if (test_and_clear_bit(STANDBY, &con->state))
1541 con->connect_seq++;
1542
Sage Weileed0ef22009-11-10 14:34:36 -08001543 prepare_write_banner(msgr, con);
1544 prepare_write_connect(msgr, con, 1);
1545 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001546 set_bit(CONNECTING, &con->state);
Sage Weileed0ef22009-11-10 14:34:36 -08001547 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001548
Sage Weilcf3e5c42009-12-11 09:48:05 -08001549 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001550 con->in_tag = CEPH_MSGR_TAG_READY;
1551 dout("try_write initiating connect on %p new state %lu\n",
1552 con, con->state);
1553 con->sock = ceph_tcp_connect(con);
1554 if (IS_ERR(con->sock)) {
1555 con->sock = NULL;
1556 con->error_msg = "connect error";
1557 ret = -1;
1558 goto out;
1559 }
1560 }
1561
1562more_kvec:
1563 /* kvec data queued? */
1564 if (con->out_skip) {
1565 ret = write_partial_skip(con);
1566 if (ret <= 0)
1567 goto done;
1568 if (ret < 0) {
1569 dout("try_write write_partial_skip err %d\n", ret);
1570 goto done;
1571 }
1572 }
1573 if (con->out_kvec_left) {
1574 ret = write_partial_kvec(con);
1575 if (ret <= 0)
1576 goto done;
Sage Weil31b80062009-10-06 11:31:13 -07001577 }
1578
1579 /* msg pages? */
1580 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001581 if (con->out_msg_done) {
1582 ceph_msg_put(con->out_msg);
1583 con->out_msg = NULL; /* we're done with this one */
1584 goto do_next;
1585 }
1586
Sage Weil31b80062009-10-06 11:31:13 -07001587 ret = write_partial_msg_pages(con);
1588 if (ret == 1)
1589 goto more_kvec; /* we need to send the footer, too! */
1590 if (ret == 0)
1591 goto done;
1592 if (ret < 0) {
1593 dout("try_write write_partial_msg_pages err %d\n",
1594 ret);
1595 goto done;
1596 }
1597 }
1598
Sage Weilc86a2932009-12-14 14:04:30 -08001599do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001600 if (!test_bit(CONNECTING, &con->state)) {
1601 /* is anything else pending? */
1602 if (!list_empty(&con->out_queue)) {
1603 prepare_write_message(con);
1604 goto more;
1605 }
1606 if (con->in_seq > con->in_seq_acked) {
1607 prepare_write_ack(con);
1608 goto more;
1609 }
1610 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1611 prepare_write_keepalive(con);
1612 goto more;
1613 }
1614 }
1615
1616 /* Nothing to do! */
1617 clear_bit(WRITE_PENDING, &con->state);
1618 dout("try_write nothing else to write.\n");
1619done:
1620 ret = 0;
1621out:
Sage Weilec302642009-12-22 10:43:42 -08001622 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001623 dout("try_write done on %p\n", con);
1624 return ret;
1625}
1626
1627
1628
1629/*
1630 * Read what we can from the socket.
1631 */
1632static int try_read(struct ceph_connection *con)
1633{
1634 struct ceph_messenger *msgr;
1635 int ret = -1;
1636
1637 if (!con->sock)
1638 return 0;
1639
1640 if (test_bit(STANDBY, &con->state))
1641 return 0;
1642
1643 dout("try_read start on %p\n", con);
1644 msgr = con->msgr;
1645
Sage Weilec302642009-12-22 10:43:42 -08001646 mutex_lock(&con->mutex);
1647
Sage Weil31b80062009-10-06 11:31:13 -07001648more:
1649 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1650 con->in_base_pos);
1651 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08001652 if (!test_bit(NEGOTIATING, &con->state)) {
1653 dout("try_read connecting\n");
1654 ret = read_partial_banner(con);
1655 if (ret <= 0)
1656 goto done;
1657 if (process_banner(con) < 0) {
1658 ret = -1;
1659 goto out;
1660 }
1661 }
Sage Weil31b80062009-10-06 11:31:13 -07001662 ret = read_partial_connect(con);
1663 if (ret <= 0)
1664 goto done;
1665 if (process_connect(con) < 0) {
1666 ret = -1;
1667 goto out;
1668 }
1669 goto more;
1670 }
1671
1672 if (con->in_base_pos < 0) {
1673 /*
1674 * skipping + discarding content.
1675 *
1676 * FIXME: there must be a better way to do this!
1677 */
1678 static char buf[1024];
1679 int skip = min(1024, -con->in_base_pos);
1680 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1681 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1682 if (ret <= 0)
1683 goto done;
1684 con->in_base_pos += ret;
1685 if (con->in_base_pos)
1686 goto more;
1687 }
1688 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1689 /*
1690 * what's next?
1691 */
1692 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1693 if (ret <= 0)
1694 goto done;
1695 dout("try_read got tag %d\n", (int)con->in_tag);
1696 switch (con->in_tag) {
1697 case CEPH_MSGR_TAG_MSG:
1698 prepare_read_message(con);
1699 break;
1700 case CEPH_MSGR_TAG_ACK:
1701 prepare_read_ack(con);
1702 break;
1703 case CEPH_MSGR_TAG_CLOSE:
1704 set_bit(CLOSED, &con->state); /* fixme */
1705 goto done;
1706 default:
1707 goto bad_tag;
1708 }
1709 }
1710 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1711 ret = read_partial_message(con);
1712 if (ret <= 0) {
1713 switch (ret) {
1714 case -EBADMSG:
1715 con->error_msg = "bad crc";
1716 ret = -EIO;
1717 goto out;
1718 case -EIO:
1719 con->error_msg = "io error";
1720 goto out;
1721 default:
1722 goto done;
1723 }
1724 }
1725 if (con->in_tag == CEPH_MSGR_TAG_READY)
1726 goto more;
1727 process_message(con);
1728 goto more;
1729 }
1730 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1731 ret = read_partial_ack(con);
1732 if (ret <= 0)
1733 goto done;
1734 process_ack(con);
1735 goto more;
1736 }
1737
1738done:
1739 ret = 0;
1740out:
Sage Weilec302642009-12-22 10:43:42 -08001741 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001742 dout("try_read done on %p\n", con);
1743 return ret;
1744
1745bad_tag:
1746 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1747 con->error_msg = "protocol error, garbage tag";
1748 ret = -1;
1749 goto out;
1750}
1751
1752
1753/*
1754 * Atomically queue work on a connection. Bump @con reference to
1755 * avoid races with connection teardown.
1756 *
1757 * There is some trickery going on with QUEUED and BUSY because we
1758 * only want a _single_ thread operating on each connection at any
1759 * point in time, but we want to use all available CPUs.
1760 *
1761 * The worker thread only proceeds if it can atomically set BUSY. It
1762 * clears QUEUED and does it's thing. When it thinks it's done, it
1763 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1764 * (tries again to set BUSY).
1765 *
1766 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1767 * try to queue work. If that fails (work is already queued, or BUSY)
1768 * we give up (work also already being done or is queued) but leave QUEUED
1769 * set so that the worker thread will loop if necessary.
1770 */
1771static void queue_con(struct ceph_connection *con)
1772{
1773 if (test_bit(DEAD, &con->state)) {
1774 dout("queue_con %p ignoring: DEAD\n",
1775 con);
1776 return;
1777 }
1778
1779 if (!con->ops->get(con)) {
1780 dout("queue_con %p ref count 0\n", con);
1781 return;
1782 }
1783
1784 set_bit(QUEUED, &con->state);
1785 if (test_bit(BUSY, &con->state)) {
1786 dout("queue_con %p - already BUSY\n", con);
1787 con->ops->put(con);
1788 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1789 dout("queue_con %p - already queued\n", con);
1790 con->ops->put(con);
1791 } else {
1792 dout("queue_con %p\n", con);
1793 }
1794}
1795
1796/*
1797 * Do some work on a connection. Drop a connection ref when we're done.
1798 */
1799static void con_work(struct work_struct *work)
1800{
1801 struct ceph_connection *con = container_of(work, struct ceph_connection,
1802 work.work);
1803 int backoff = 0;
1804
1805more:
1806 if (test_and_set_bit(BUSY, &con->state) != 0) {
1807 dout("con_work %p BUSY already set\n", con);
1808 goto out;
1809 }
1810 dout("con_work %p start, clearing QUEUED\n", con);
1811 clear_bit(QUEUED, &con->state);
1812
1813 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1814 dout("con_work CLOSED\n");
1815 con_close_socket(con);
1816 goto done;
1817 }
1818 if (test_and_clear_bit(OPENING, &con->state)) {
1819 /* reopen w/ new peer */
1820 dout("con_work OPENING\n");
1821 con_close_socket(con);
1822 }
1823
1824 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1825 try_read(con) < 0 ||
1826 try_write(con) < 0) {
1827 backoff = 1;
1828 ceph_fault(con); /* error/fault path */
1829 }
1830
1831done:
1832 clear_bit(BUSY, &con->state);
1833 dout("con->state=%lu\n", con->state);
1834 if (test_bit(QUEUED, &con->state)) {
1835 if (!backoff) {
1836 dout("con_work %p QUEUED reset, looping\n", con);
1837 goto more;
1838 }
1839 dout("con_work %p QUEUED reset, but just faulted\n", con);
1840 clear_bit(QUEUED, &con->state);
1841 }
1842 dout("con_work %p done\n", con);
1843
1844out:
1845 con->ops->put(con);
1846}
1847
1848
1849/*
1850 * Generic error/fault handler. A retry mechanism is used with
1851 * exponential backoff
1852 */
1853static void ceph_fault(struct ceph_connection *con)
1854{
1855 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1856 pr_addr(&con->peer_addr.in_addr), con->error_msg);
1857 dout("fault %p state %lu to peer %s\n",
1858 con, con->state, pr_addr(&con->peer_addr.in_addr));
1859
1860 if (test_bit(LOSSYTX, &con->state)) {
1861 dout("fault on LOSSYTX channel\n");
1862 goto out;
1863 }
1864
1865 clear_bit(BUSY, &con->state); /* to avoid an improbable race */
1866
Sage Weilec302642009-12-22 10:43:42 -08001867 mutex_lock(&con->mutex);
1868
Sage Weil31b80062009-10-06 11:31:13 -07001869 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08001870
1871 if (con->in_msg) {
1872 ceph_msg_put(con->in_msg);
1873 con->in_msg = NULL;
1874 }
Sage Weil31b80062009-10-06 11:31:13 -07001875
1876 /* If there are no messages in the queue, place the connection
1877 * in a STANDBY state (i.e., don't try to reconnect just yet). */
Sage Weil31b80062009-10-06 11:31:13 -07001878 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1879 dout("fault setting STANDBY\n");
1880 set_bit(STANDBY, &con->state);
Sage Weilec302642009-12-22 10:43:42 -08001881 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001882 goto out;
1883 }
1884
1885 /* Requeue anything that hasn't been acked, and retry after a
1886 * delay. */
1887 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil31b80062009-10-06 11:31:13 -07001888
1889 if (con->delay == 0)
1890 con->delay = BASE_DELAY_INTERVAL;
1891 else if (con->delay < MAX_DELAY_INTERVAL)
1892 con->delay *= 2;
1893
Sage Weilec302642009-12-22 10:43:42 -08001894 mutex_unlock(&con->mutex);
1895
Sage Weil31b80062009-10-06 11:31:13 -07001896 /* explicitly schedule work to try to reconnect again later. */
1897 dout("fault queueing %p delay %lu\n", con, con->delay);
1898 con->ops->get(con);
1899 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1900 round_jiffies_relative(con->delay)) == 0)
1901 con->ops->put(con);
1902
1903out:
1904 if (con->ops->fault)
1905 con->ops->fault(con);
1906}
1907
1908
1909
1910/*
1911 * create a new messenger instance
1912 */
1913struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1914{
1915 struct ceph_messenger *msgr;
1916
1917 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1918 if (msgr == NULL)
1919 return ERR_PTR(-ENOMEM);
1920
1921 spin_lock_init(&msgr->global_seq_lock);
1922
1923 /* the zero page is needed if a request is "canceled" while the message
1924 * is being written over the socket */
1925 msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1926 if (!msgr->zero_page) {
1927 kfree(msgr);
1928 return ERR_PTR(-ENOMEM);
1929 }
1930 kmap(msgr->zero_page);
1931
1932 if (myaddr)
1933 msgr->inst.addr = *myaddr;
1934
1935 /* select a random nonce */
Sage Weil103e2d32010-01-07 16:12:36 -08001936 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08001937 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07001938
1939 dout("messenger_create %p\n", msgr);
1940 return msgr;
1941}
1942
1943void ceph_messenger_destroy(struct ceph_messenger *msgr)
1944{
1945 dout("destroy %p\n", msgr);
1946 kunmap(msgr->zero_page);
1947 __free_page(msgr->zero_page);
1948 kfree(msgr);
1949 dout("destroyed messenger %p\n", msgr);
1950}
1951
1952/*
1953 * Queue up an outgoing message on the given connection.
1954 */
1955void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1956{
1957 if (test_bit(CLOSED, &con->state)) {
1958 dout("con_send %p closed, dropping %p\n", con, msg);
1959 ceph_msg_put(msg);
1960 return;
1961 }
1962
1963 /* set src+dst */
Sage Weil63f2d212009-11-03 15:17:56 -08001964 msg->hdr.src.name = con->msgr->inst.name;
1965 msg->hdr.src.addr = con->msgr->my_enc_addr;
1966 msg->hdr.orig_src = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001967
1968 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08001969 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001970 BUG_ON(!list_empty(&msg->list_head));
1971 list_add_tail(&msg->list_head, &con->out_queue);
1972 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
1973 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
1974 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1975 le32_to_cpu(msg->hdr.front_len),
1976 le32_to_cpu(msg->hdr.middle_len),
1977 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08001978 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001979
1980 /* if there wasn't anything waiting to send before, queue
1981 * new work */
1982 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1983 queue_con(con);
1984}
1985
1986/*
1987 * Revoke a message that was previously queued for send
1988 */
1989void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
1990{
Sage Weilec302642009-12-22 10:43:42 -08001991 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001992 if (!list_empty(&msg->list_head)) {
1993 dout("con_revoke %p msg %p\n", con, msg);
1994 list_del_init(&msg->list_head);
1995 ceph_msg_put(msg);
1996 msg->hdr.seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -08001997 if (con->out_msg == msg) {
1998 ceph_msg_put(con->out_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001999 con->out_msg = NULL;
Sage Weilc86a2932009-12-14 14:04:30 -08002000 }
Sage Weil31b80062009-10-06 11:31:13 -07002001 if (con->out_kvec_is_msg) {
2002 con->out_skip = con->out_kvec_bytes;
2003 con->out_kvec_is_msg = false;
2004 }
2005 } else {
2006 dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
2007 }
Sage Weilec302642009-12-22 10:43:42 -08002008 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002009}
2010
2011/*
Sage Weil350b1c32009-12-22 10:45:45 -08002012 * Revoke a page vector that we may be reading data into
2013 */
2014void ceph_con_revoke_pages(struct ceph_connection *con, struct page **pages)
2015{
2016 mutex_lock(&con->mutex);
2017 if (con->in_msg && con->in_msg->pages == pages) {
2018 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2019
2020 /* skip rest of message */
2021 dout("con_revoke_pages %p msg %p pages %p revoked\n", con,
2022 con->in_msg, pages);
2023 if (con->in_msg_pos.data_pos < data_len)
2024 con->in_base_pos = con->in_msg_pos.data_pos - data_len;
2025 else
2026 con->in_base_pos = con->in_base_pos -
2027 sizeof(struct ceph_msg_header) -
2028 sizeof(struct ceph_msg_footer);
2029 con->in_msg->pages = NULL;
2030 ceph_msg_put(con->in_msg);
2031 con->in_msg = NULL;
2032 con->in_tag = CEPH_MSGR_TAG_READY;
2033 } else {
2034 dout("con_revoke_pages %p msg %p pages %p no-op\n",
2035 con, con->in_msg, pages);
2036 }
2037 mutex_unlock(&con->mutex);
2038}
2039
2040/*
Sage Weil31b80062009-10-06 11:31:13 -07002041 * Queue a keepalive byte to ensure the tcp connection is alive.
2042 */
2043void ceph_con_keepalive(struct ceph_connection *con)
2044{
2045 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2046 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2047 queue_con(con);
2048}
2049
2050
2051/*
2052 * construct a new message with given type, size
2053 * the new msg has a ref count of 1.
2054 */
2055struct ceph_msg *ceph_msg_new(int type, int front_len,
2056 int page_len, int page_off, struct page **pages)
2057{
2058 struct ceph_msg *m;
2059
2060 m = kmalloc(sizeof(*m), GFP_NOFS);
2061 if (m == NULL)
2062 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002063 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07002064 INIT_LIST_HEAD(&m->list_head);
2065
2066 m->hdr.type = cpu_to_le16(type);
2067 m->hdr.front_len = cpu_to_le32(front_len);
2068 m->hdr.middle_len = 0;
2069 m->hdr.data_len = cpu_to_le32(page_len);
2070 m->hdr.data_off = cpu_to_le16(page_off);
2071 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2072 m->footer.front_crc = 0;
2073 m->footer.middle_crc = 0;
2074 m->footer.data_crc = 0;
2075 m->front_max = front_len;
2076 m->front_is_vmalloc = false;
2077 m->more_to_follow = false;
2078 m->pool = NULL;
2079
2080 /* front */
2081 if (front_len) {
2082 if (front_len > PAGE_CACHE_SIZE) {
2083 m->front.iov_base = __vmalloc(front_len, GFP_NOFS,
2084 PAGE_KERNEL);
2085 m->front_is_vmalloc = true;
2086 } else {
2087 m->front.iov_base = kmalloc(front_len, GFP_NOFS);
2088 }
2089 if (m->front.iov_base == NULL) {
2090 pr_err("msg_new can't allocate %d bytes\n",
2091 front_len);
2092 goto out2;
2093 }
2094 } else {
2095 m->front.iov_base = NULL;
2096 }
2097 m->front.iov_len = front_len;
2098
2099 /* middle */
2100 m->middle = NULL;
2101
2102 /* data */
2103 m->nr_pages = calc_pages_for(page_off, page_len);
2104 m->pages = pages;
Sage Weil58bb3b32009-12-23 12:12:31 -08002105 m->pagelist = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002106
2107 dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len,
2108 m->nr_pages);
2109 return m;
2110
2111out2:
2112 ceph_msg_put(m);
2113out:
2114 pr_err("msg_new can't create type %d len %d\n", type, front_len);
2115 return ERR_PTR(-ENOMEM);
2116}
2117
2118/*
2119 * Generic message allocator, for incoming messages.
2120 */
2121struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2122 struct ceph_msg_header *hdr)
2123{
2124 int type = le16_to_cpu(hdr->type);
2125 int front_len = le32_to_cpu(hdr->front_len);
2126 struct ceph_msg *msg = ceph_msg_new(type, front_len, 0, 0, NULL);
2127
2128 if (!msg) {
2129 pr_err("unable to allocate msg type %d len %d\n",
2130 type, front_len);
2131 return ERR_PTR(-ENOMEM);
2132 }
2133 return msg;
2134}
2135
2136/*
2137 * Allocate "middle" portion of a message, if it is needed and wasn't
2138 * allocated by alloc_msg. This allows us to read a small fixed-size
2139 * per-type header in the front and then gracefully fail (i.e.,
2140 * propagate the error to the caller based on info in the front) when
2141 * the middle is too large.
2142 */
2143int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
2144{
2145 int type = le16_to_cpu(msg->hdr.type);
2146 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2147
2148 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2149 ceph_msg_type_name(type), middle_len);
2150 BUG_ON(!middle_len);
2151 BUG_ON(msg->middle);
2152
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002153 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002154 if (!msg->middle)
2155 return -ENOMEM;
2156 return 0;
2157}
2158
2159
2160/*
2161 * Free a generically kmalloc'd message.
2162 */
2163void ceph_msg_kfree(struct ceph_msg *m)
2164{
2165 dout("msg_kfree %p\n", m);
2166 if (m->front_is_vmalloc)
2167 vfree(m->front.iov_base);
2168 else
2169 kfree(m->front.iov_base);
2170 kfree(m);
2171}
2172
2173/*
2174 * Drop a msg ref. Destroy as needed.
2175 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002176void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002177{
Sage Weilc2e552e2009-12-07 15:55:05 -08002178 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002179
Sage Weilc2e552e2009-12-07 15:55:05 -08002180 dout("ceph_msg_put last one on %p\n", m);
2181 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002182
Sage Weilc2e552e2009-12-07 15:55:05 -08002183 /* drop middle, data, if any */
2184 if (m->middle) {
2185 ceph_buffer_put(m->middle);
2186 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002187 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002188 m->nr_pages = 0;
2189 m->pages = NULL;
2190
Sage Weil58bb3b32009-12-23 12:12:31 -08002191 if (m->pagelist) {
2192 ceph_pagelist_release(m->pagelist);
2193 kfree(m->pagelist);
2194 m->pagelist = NULL;
2195 }
2196
Sage Weilc2e552e2009-12-07 15:55:05 -08002197 if (m->pool)
2198 ceph_msgpool_put(m->pool, m);
2199 else
2200 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002201}
Sage Weil9ec7cab2009-12-14 15:13:47 -08002202
2203void ceph_msg_dump(struct ceph_msg *msg)
2204{
2205 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2206 msg->front_max, msg->nr_pages);
2207 print_hex_dump(KERN_DEBUG, "header: ",
2208 DUMP_PREFIX_OFFSET, 16, 1,
2209 &msg->hdr, sizeof(msg->hdr), true);
2210 print_hex_dump(KERN_DEBUG, " front: ",
2211 DUMP_PREFIX_OFFSET, 16, 1,
2212 msg->front.iov_base, msg->front.iov_len, true);
2213 if (msg->middle)
2214 print_hex_dump(KERN_DEBUG, "middle: ",
2215 DUMP_PREFIX_OFFSET, 16, 1,
2216 msg->middle->vec.iov_base,
2217 msg->middle->vec.iov_len, true);
2218 print_hex_dump(KERN_DEBUG, "footer: ",
2219 DUMP_PREFIX_OFFSET, 16, 1,
2220 &msg->footer, sizeof(msg->footer), true);
2221}