Commit | Line | Data |
---|---|---|
31b8006e SW |
1 | #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" | |
63f2d211 | 15 | #include "decode.h" |
31b8006e SW |
16 | |
17 | /* | |
18 | * Ceph uses the messenger to exchange ceph_msg messages with other | |
19 | * hosts in the system. The messenger provides ordered and reliable | |
20 | * delivery. We tolerate TCP disconnects by reconnecting (with | |
21 | * exponential backoff) in the case of a fault (disconnection, bad | |
22 | * crc, protocol error). Acks allow sent messages to be discarded by | |
23 | * the sender. | |
24 | */ | |
25 | ||
26 | /* static tag bytes (protocol control messages) */ | |
27 | static char tag_msg = CEPH_MSGR_TAG_MSG; | |
28 | static char tag_ack = CEPH_MSGR_TAG_ACK; | |
29 | static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE; | |
30 | ||
31 | ||
32 | static void queue_con(struct ceph_connection *con); | |
33 | static void con_work(struct work_struct *); | |
34 | static void ceph_fault(struct ceph_connection *con); | |
35 | ||
36 | const char *ceph_name_type_str(int t) | |
37 | { | |
38 | switch (t) { | |
39 | case CEPH_ENTITY_TYPE_MON: return "mon"; | |
40 | case CEPH_ENTITY_TYPE_MDS: return "mds"; | |
41 | case CEPH_ENTITY_TYPE_OSD: return "osd"; | |
42 | case CEPH_ENTITY_TYPE_CLIENT: return "client"; | |
43 | case CEPH_ENTITY_TYPE_ADMIN: return "admin"; | |
44 | default: return "???"; | |
45 | } | |
46 | } | |
47 | ||
48 | /* | |
49 | * nicely render a sockaddr as a string. | |
50 | */ | |
51 | #define MAX_ADDR_STR 20 | |
52 | static char addr_str[MAX_ADDR_STR][40]; | |
53 | static DEFINE_SPINLOCK(addr_str_lock); | |
54 | static int last_addr_str; | |
55 | ||
56 | const char *pr_addr(const struct sockaddr_storage *ss) | |
57 | { | |
58 | int i; | |
59 | char *s; | |
60 | struct sockaddr_in *in4 = (void *)ss; | |
61 | unsigned char *quad = (void *)&in4->sin_addr.s_addr; | |
62 | struct sockaddr_in6 *in6 = (void *)ss; | |
63 | ||
64 | spin_lock(&addr_str_lock); | |
65 | i = last_addr_str++; | |
66 | if (last_addr_str == MAX_ADDR_STR) | |
67 | last_addr_str = 0; | |
68 | spin_unlock(&addr_str_lock); | |
69 | s = addr_str[i]; | |
70 | ||
71 | switch (ss->ss_family) { | |
72 | case AF_INET: | |
73 | sprintf(s, "%u.%u.%u.%u:%u", | |
74 | (unsigned int)quad[0], | |
75 | (unsigned int)quad[1], | |
76 | (unsigned int)quad[2], | |
77 | (unsigned int)quad[3], | |
78 | (unsigned int)ntohs(in4->sin_port)); | |
79 | break; | |
80 | ||
81 | case AF_INET6: | |
82 | sprintf(s, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u", | |
83 | in6->sin6_addr.s6_addr16[0], | |
84 | in6->sin6_addr.s6_addr16[1], | |
85 | in6->sin6_addr.s6_addr16[2], | |
86 | in6->sin6_addr.s6_addr16[3], | |
87 | in6->sin6_addr.s6_addr16[4], | |
88 | in6->sin6_addr.s6_addr16[5], | |
89 | in6->sin6_addr.s6_addr16[6], | |
90 | in6->sin6_addr.s6_addr16[7], | |
91 | (unsigned int)ntohs(in6->sin6_port)); | |
92 | break; | |
93 | ||
94 | default: | |
95 | sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family); | |
96 | } | |
97 | ||
98 | return s; | |
99 | } | |
100 | ||
63f2d211 SW |
101 | static void encode_my_addr(struct ceph_messenger *msgr) |
102 | { | |
103 | memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr)); | |
104 | ceph_encode_addr(&msgr->my_enc_addr); | |
105 | } | |
106 | ||
31b8006e SW |
107 | /* |
108 | * work queue for all reading and writing to/from the socket. | |
109 | */ | |
110 | struct workqueue_struct *ceph_msgr_wq; | |
111 | ||
112 | int __init ceph_msgr_init(void) | |
113 | { | |
114 | ceph_msgr_wq = create_workqueue("ceph-msgr"); | |
115 | if (IS_ERR(ceph_msgr_wq)) { | |
116 | int ret = PTR_ERR(ceph_msgr_wq); | |
117 | pr_err("msgr_init failed to create workqueue: %d\n", ret); | |
118 | ceph_msgr_wq = NULL; | |
119 | return ret; | |
120 | } | |
121 | return 0; | |
122 | } | |
123 | ||
124 | void ceph_msgr_exit(void) | |
125 | { | |
126 | destroy_workqueue(ceph_msgr_wq); | |
127 | } | |
128 | ||
129 | /* | |
130 | * socket callback functions | |
131 | */ | |
132 | ||
133 | /* data available on socket, or listen socket received a connect */ | |
134 | static void ceph_data_ready(struct sock *sk, int count_unused) | |
135 | { | |
136 | struct ceph_connection *con = | |
137 | (struct ceph_connection *)sk->sk_user_data; | |
138 | if (sk->sk_state != TCP_CLOSE_WAIT) { | |
139 | dout("ceph_data_ready on %p state = %lu, queueing work\n", | |
140 | con, con->state); | |
141 | queue_con(con); | |
142 | } | |
143 | } | |
144 | ||
145 | /* socket has buffer space for writing */ | |
146 | static void ceph_write_space(struct sock *sk) | |
147 | { | |
148 | struct ceph_connection *con = | |
149 | (struct ceph_connection *)sk->sk_user_data; | |
150 | ||
151 | /* only queue to workqueue if there is data we want to write. */ | |
152 | if (test_bit(WRITE_PENDING, &con->state)) { | |
153 | dout("ceph_write_space %p queueing write work\n", con); | |
154 | queue_con(con); | |
155 | } else { | |
156 | dout("ceph_write_space %p nothing to write\n", con); | |
157 | } | |
158 | ||
159 | /* since we have our own write_space, clear the SOCK_NOSPACE flag */ | |
160 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); | |
161 | } | |
162 | ||
163 | /* socket's state has changed */ | |
164 | static void ceph_state_change(struct sock *sk) | |
165 | { | |
166 | struct ceph_connection *con = | |
167 | (struct ceph_connection *)sk->sk_user_data; | |
168 | ||
169 | dout("ceph_state_change %p state = %lu sk_state = %u\n", | |
170 | con, con->state, sk->sk_state); | |
171 | ||
172 | if (test_bit(CLOSED, &con->state)) | |
173 | return; | |
174 | ||
175 | switch (sk->sk_state) { | |
176 | case TCP_CLOSE: | |
177 | dout("ceph_state_change TCP_CLOSE\n"); | |
178 | case TCP_CLOSE_WAIT: | |
179 | dout("ceph_state_change TCP_CLOSE_WAIT\n"); | |
180 | if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) { | |
181 | if (test_bit(CONNECTING, &con->state)) | |
182 | con->error_msg = "connection failed"; | |
183 | else | |
184 | con->error_msg = "socket closed"; | |
185 | queue_con(con); | |
186 | } | |
187 | break; | |
188 | case TCP_ESTABLISHED: | |
189 | dout("ceph_state_change TCP_ESTABLISHED\n"); | |
190 | queue_con(con); | |
191 | break; | |
192 | } | |
193 | } | |
194 | ||
195 | /* | |
196 | * set up socket callbacks | |
197 | */ | |
198 | static void set_sock_callbacks(struct socket *sock, | |
199 | struct ceph_connection *con) | |
200 | { | |
201 | struct sock *sk = sock->sk; | |
202 | sk->sk_user_data = (void *)con; | |
203 | sk->sk_data_ready = ceph_data_ready; | |
204 | sk->sk_write_space = ceph_write_space; | |
205 | sk->sk_state_change = ceph_state_change; | |
206 | } | |
207 | ||
208 | ||
209 | /* | |
210 | * socket helpers | |
211 | */ | |
212 | ||
213 | /* | |
214 | * initiate connection to a remote socket. | |
215 | */ | |
216 | static struct socket *ceph_tcp_connect(struct ceph_connection *con) | |
217 | { | |
218 | struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr; | |
219 | struct socket *sock; | |
220 | int ret; | |
221 | ||
222 | BUG_ON(con->sock); | |
223 | ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); | |
224 | if (ret) | |
225 | return ERR_PTR(ret); | |
226 | con->sock = sock; | |
227 | sock->sk->sk_allocation = GFP_NOFS; | |
228 | ||
229 | set_sock_callbacks(sock, con); | |
230 | ||
231 | dout("connect %s\n", pr_addr(&con->peer_addr.in_addr)); | |
232 | ||
233 | ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK); | |
234 | if (ret == -EINPROGRESS) { | |
235 | dout("connect %s EINPROGRESS sk_state = %u\n", | |
236 | pr_addr(&con->peer_addr.in_addr), | |
237 | sock->sk->sk_state); | |
238 | ret = 0; | |
239 | } | |
240 | if (ret < 0) { | |
241 | pr_err("connect %s error %d\n", | |
242 | pr_addr(&con->peer_addr.in_addr), ret); | |
243 | sock_release(sock); | |
244 | con->sock = NULL; | |
245 | con->error_msg = "connect error"; | |
246 | } | |
247 | ||
248 | if (ret < 0) | |
249 | return ERR_PTR(ret); | |
250 | return sock; | |
251 | } | |
252 | ||
253 | static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len) | |
254 | { | |
255 | struct kvec iov = {buf, len}; | |
256 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; | |
257 | ||
258 | return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags); | |
259 | } | |
260 | ||
261 | /* | |
262 | * write something. @more is true if caller will be sending more data | |
263 | * shortly. | |
264 | */ | |
265 | static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov, | |
266 | size_t kvlen, size_t len, int more) | |
267 | { | |
268 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; | |
269 | ||
270 | if (more) | |
271 | msg.msg_flags |= MSG_MORE; | |
272 | else | |
273 | msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */ | |
274 | ||
275 | return kernel_sendmsg(sock, &msg, iov, kvlen, len); | |
276 | } | |
277 | ||
278 | ||
279 | /* | |
280 | * Shutdown/close the socket for the given connection. | |
281 | */ | |
282 | static int con_close_socket(struct ceph_connection *con) | |
283 | { | |
284 | int rc; | |
285 | ||
286 | dout("con_close_socket on %p sock %p\n", con, con->sock); | |
287 | if (!con->sock) | |
288 | return 0; | |
289 | set_bit(SOCK_CLOSED, &con->state); | |
290 | rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR); | |
291 | sock_release(con->sock); | |
292 | con->sock = NULL; | |
293 | clear_bit(SOCK_CLOSED, &con->state); | |
294 | return rc; | |
295 | } | |
296 | ||
297 | /* | |
298 | * Reset a connection. Discard all incoming and outgoing messages | |
299 | * and clear *_seq state. | |
300 | */ | |
301 | static void ceph_msg_remove(struct ceph_msg *msg) | |
302 | { | |
303 | list_del_init(&msg->list_head); | |
304 | ceph_msg_put(msg); | |
305 | } | |
306 | static void ceph_msg_remove_list(struct list_head *head) | |
307 | { | |
308 | while (!list_empty(head)) { | |
309 | struct ceph_msg *msg = list_first_entry(head, struct ceph_msg, | |
310 | list_head); | |
311 | ceph_msg_remove(msg); | |
312 | } | |
313 | } | |
314 | ||
315 | static void reset_connection(struct ceph_connection *con) | |
316 | { | |
317 | /* reset connection, out_queue, msg_ and connect_seq */ | |
318 | /* discard existing out_queue and msg_seq */ | |
319 | mutex_lock(&con->out_mutex); | |
320 | ceph_msg_remove_list(&con->out_queue); | |
321 | ceph_msg_remove_list(&con->out_sent); | |
322 | ||
323 | con->connect_seq = 0; | |
324 | con->out_seq = 0; | |
325 | con->out_msg = NULL; | |
326 | con->in_seq = 0; | |
327 | mutex_unlock(&con->out_mutex); | |
328 | } | |
329 | ||
330 | /* | |
331 | * mark a peer down. drop any open connections. | |
332 | */ | |
333 | void ceph_con_close(struct ceph_connection *con) | |
334 | { | |
335 | dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr)); | |
336 | set_bit(CLOSED, &con->state); /* in case there's queued work */ | |
337 | clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */ | |
338 | reset_connection(con); | |
339 | queue_con(con); | |
340 | } | |
341 | ||
31b8006e SW |
342 | /* |
343 | * Reopen a closed connection, with a new peer address. | |
344 | */ | |
345 | void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr) | |
346 | { | |
347 | dout("con_open %p %s\n", con, pr_addr(&addr->in_addr)); | |
348 | set_bit(OPENING, &con->state); | |
349 | clear_bit(CLOSED, &con->state); | |
350 | memcpy(&con->peer_addr, addr, sizeof(*addr)); | |
351 | queue_con(con); | |
352 | } | |
353 | ||
354 | /* | |
355 | * generic get/put | |
356 | */ | |
357 | struct ceph_connection *ceph_con_get(struct ceph_connection *con) | |
358 | { | |
359 | dout("con_get %p nref = %d -> %d\n", con, | |
360 | atomic_read(&con->nref), atomic_read(&con->nref) + 1); | |
361 | if (atomic_inc_not_zero(&con->nref)) | |
362 | return con; | |
363 | return NULL; | |
364 | } | |
365 | ||
366 | void ceph_con_put(struct ceph_connection *con) | |
367 | { | |
368 | dout("con_put %p nref = %d -> %d\n", con, | |
369 | atomic_read(&con->nref), atomic_read(&con->nref) - 1); | |
370 | BUG_ON(atomic_read(&con->nref) == 0); | |
371 | if (atomic_dec_and_test(&con->nref)) { | |
71ececda | 372 | BUG_ON(con->sock); |
31b8006e SW |
373 | kfree(con); |
374 | } | |
375 | } | |
376 | ||
377 | /* | |
378 | * initialize a new connection. | |
379 | */ | |
380 | void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con) | |
381 | { | |
382 | dout("con_init %p\n", con); | |
383 | memset(con, 0, sizeof(*con)); | |
384 | atomic_set(&con->nref, 1); | |
385 | con->msgr = msgr; | |
386 | mutex_init(&con->out_mutex); | |
387 | INIT_LIST_HEAD(&con->out_queue); | |
388 | INIT_LIST_HEAD(&con->out_sent); | |
389 | INIT_DELAYED_WORK(&con->work, con_work); | |
390 | } | |
391 | ||
392 | ||
393 | /* | |
394 | * We maintain a global counter to order connection attempts. Get | |
395 | * a unique seq greater than @gt. | |
396 | */ | |
397 | static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt) | |
398 | { | |
399 | u32 ret; | |
400 | ||
401 | spin_lock(&msgr->global_seq_lock); | |
402 | if (msgr->global_seq < gt) | |
403 | msgr->global_seq = gt; | |
404 | ret = ++msgr->global_seq; | |
405 | spin_unlock(&msgr->global_seq_lock); | |
406 | return ret; | |
407 | } | |
408 | ||
409 | ||
410 | /* | |
411 | * Prepare footer for currently outgoing message, and finish things | |
412 | * off. Assumes out_kvec* are already valid.. we just add on to the end. | |
413 | */ | |
414 | static void prepare_write_message_footer(struct ceph_connection *con, int v) | |
415 | { | |
416 | struct ceph_msg *m = con->out_msg; | |
417 | ||
418 | dout("prepare_write_message_footer %p\n", con); | |
419 | con->out_kvec_is_msg = true; | |
420 | con->out_kvec[v].iov_base = &m->footer; | |
421 | con->out_kvec[v].iov_len = sizeof(m->footer); | |
422 | con->out_kvec_bytes += sizeof(m->footer); | |
423 | con->out_kvec_left++; | |
424 | con->out_more = m->more_to_follow; | |
425 | con->out_msg = NULL; /* we're done with this one */ | |
426 | } | |
427 | ||
428 | /* | |
429 | * Prepare headers for the next outgoing message. | |
430 | */ | |
431 | static void prepare_write_message(struct ceph_connection *con) | |
432 | { | |
433 | struct ceph_msg *m; | |
434 | int v = 0; | |
435 | ||
436 | con->out_kvec_bytes = 0; | |
437 | con->out_kvec_is_msg = true; | |
438 | ||
439 | /* Sneak an ack in there first? If we can get it into the same | |
440 | * TCP packet that's a good thing. */ | |
441 | if (con->in_seq > con->in_seq_acked) { | |
442 | con->in_seq_acked = con->in_seq; | |
443 | con->out_kvec[v].iov_base = &tag_ack; | |
444 | con->out_kvec[v++].iov_len = 1; | |
445 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); | |
446 | con->out_kvec[v].iov_base = &con->out_temp_ack; | |
447 | con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack); | |
448 | con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack); | |
449 | } | |
450 | ||
451 | /* move message to sending/sent list */ | |
452 | m = list_first_entry(&con->out_queue, | |
453 | struct ceph_msg, list_head); | |
454 | list_move_tail(&m->list_head, &con->out_sent); | |
455 | con->out_msg = m; /* we don't bother taking a reference here. */ | |
456 | ||
457 | m->hdr.seq = cpu_to_le64(++con->out_seq); | |
458 | ||
459 | dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n", | |
460 | m, con->out_seq, le16_to_cpu(m->hdr.type), | |
461 | le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len), | |
462 | le32_to_cpu(m->hdr.data_len), | |
463 | m->nr_pages); | |
464 | BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len); | |
465 | ||
466 | /* tag + hdr + front + middle */ | |
467 | con->out_kvec[v].iov_base = &tag_msg; | |
468 | con->out_kvec[v++].iov_len = 1; | |
469 | con->out_kvec[v].iov_base = &m->hdr; | |
470 | con->out_kvec[v++].iov_len = sizeof(m->hdr); | |
471 | con->out_kvec[v++] = m->front; | |
472 | if (m->middle) | |
473 | con->out_kvec[v++] = m->middle->vec; | |
474 | con->out_kvec_left = v; | |
475 | con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len + | |
476 | (m->middle ? m->middle->vec.iov_len : 0); | |
477 | con->out_kvec_cur = con->out_kvec; | |
478 | ||
479 | /* fill in crc (except data pages), footer */ | |
480 | con->out_msg->hdr.crc = | |
481 | cpu_to_le32(crc32c(0, (void *)&m->hdr, | |
482 | sizeof(m->hdr) - sizeof(m->hdr.crc))); | |
483 | con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE; | |
484 | con->out_msg->footer.front_crc = | |
485 | cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len)); | |
486 | if (m->middle) | |
487 | con->out_msg->footer.middle_crc = | |
488 | cpu_to_le32(crc32c(0, m->middle->vec.iov_base, | |
489 | m->middle->vec.iov_len)); | |
490 | else | |
491 | con->out_msg->footer.middle_crc = 0; | |
492 | con->out_msg->footer.data_crc = 0; | |
493 | dout("prepare_write_message front_crc %u data_crc %u\n", | |
494 | le32_to_cpu(con->out_msg->footer.front_crc), | |
495 | le32_to_cpu(con->out_msg->footer.middle_crc)); | |
496 | ||
497 | /* is there a data payload? */ | |
498 | if (le32_to_cpu(m->hdr.data_len) > 0) { | |
499 | /* initialize page iterator */ | |
500 | con->out_msg_pos.page = 0; | |
501 | con->out_msg_pos.page_pos = | |
502 | le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK; | |
503 | con->out_msg_pos.data_pos = 0; | |
504 | con->out_msg_pos.did_page_crc = 0; | |
505 | con->out_more = 1; /* data + footer will follow */ | |
506 | } else { | |
507 | /* no, queue up footer too and be done */ | |
508 | prepare_write_message_footer(con, v); | |
509 | } | |
510 | ||
511 | set_bit(WRITE_PENDING, &con->state); | |
512 | } | |
513 | ||
514 | /* | |
515 | * Prepare an ack. | |
516 | */ | |
517 | static void prepare_write_ack(struct ceph_connection *con) | |
518 | { | |
519 | dout("prepare_write_ack %p %llu -> %llu\n", con, | |
520 | con->in_seq_acked, con->in_seq); | |
521 | con->in_seq_acked = con->in_seq; | |
522 | ||
523 | con->out_kvec[0].iov_base = &tag_ack; | |
524 | con->out_kvec[0].iov_len = 1; | |
525 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); | |
526 | con->out_kvec[1].iov_base = &con->out_temp_ack; | |
527 | con->out_kvec[1].iov_len = sizeof(con->out_temp_ack); | |
528 | con->out_kvec_left = 2; | |
529 | con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack); | |
530 | con->out_kvec_cur = con->out_kvec; | |
531 | con->out_more = 1; /* more will follow.. eventually.. */ | |
532 | set_bit(WRITE_PENDING, &con->state); | |
533 | } | |
534 | ||
535 | /* | |
536 | * Prepare to write keepalive byte. | |
537 | */ | |
538 | static void prepare_write_keepalive(struct ceph_connection *con) | |
539 | { | |
540 | dout("prepare_write_keepalive %p\n", con); | |
541 | con->out_kvec[0].iov_base = &tag_keepalive; | |
542 | con->out_kvec[0].iov_len = 1; | |
543 | con->out_kvec_left = 1; | |
544 | con->out_kvec_bytes = 1; | |
545 | con->out_kvec_cur = con->out_kvec; | |
546 | set_bit(WRITE_PENDING, &con->state); | |
547 | } | |
548 | ||
549 | /* | |
550 | * Connection negotiation. | |
551 | */ | |
552 | ||
553 | /* | |
554 | * We connected to a peer and are saying hello. | |
555 | */ | |
eed0ef2c SW |
556 | static void prepare_write_banner(struct ceph_messenger *msgr, |
557 | struct ceph_connection *con) | |
31b8006e SW |
558 | { |
559 | int len = strlen(CEPH_BANNER); | |
eed0ef2c SW |
560 | |
561 | con->out_kvec[0].iov_base = CEPH_BANNER; | |
562 | con->out_kvec[0].iov_len = len; | |
563 | con->out_kvec[1].iov_base = &msgr->my_enc_addr; | |
564 | con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr); | |
565 | con->out_kvec_left = 2; | |
566 | con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr); | |
567 | con->out_kvec_cur = con->out_kvec; | |
568 | con->out_more = 0; | |
569 | set_bit(WRITE_PENDING, &con->state); | |
570 | } | |
571 | ||
572 | static void prepare_write_connect(struct ceph_messenger *msgr, | |
573 | struct ceph_connection *con, | |
574 | int after_banner) | |
575 | { | |
31b8006e SW |
576 | unsigned global_seq = get_global_seq(con->msgr, 0); |
577 | int proto; | |
578 | ||
579 | switch (con->peer_name.type) { | |
580 | case CEPH_ENTITY_TYPE_MON: | |
581 | proto = CEPH_MONC_PROTOCOL; | |
582 | break; | |
583 | case CEPH_ENTITY_TYPE_OSD: | |
584 | proto = CEPH_OSDC_PROTOCOL; | |
585 | break; | |
586 | case CEPH_ENTITY_TYPE_MDS: | |
587 | proto = CEPH_MDSC_PROTOCOL; | |
588 | break; | |
589 | default: | |
590 | BUG(); | |
591 | } | |
592 | ||
593 | dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con, | |
594 | con->connect_seq, global_seq, proto); | |
595 | con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT); | |
596 | con->out_connect.connect_seq = cpu_to_le32(con->connect_seq); | |
597 | con->out_connect.global_seq = cpu_to_le32(global_seq); | |
598 | con->out_connect.protocol_version = cpu_to_le32(proto); | |
599 | con->out_connect.flags = 0; | |
600 | if (test_bit(LOSSYTX, &con->state)) | |
601 | con->out_connect.flags = CEPH_MSG_CONNECT_LOSSY; | |
602 | ||
eed0ef2c SW |
603 | if (!after_banner) { |
604 | con->out_kvec_left = 0; | |
605 | con->out_kvec_bytes = 0; | |
606 | } | |
607 | con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect; | |
608 | con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect); | |
609 | con->out_kvec_left++; | |
610 | con->out_kvec_bytes += sizeof(con->out_connect); | |
31b8006e SW |
611 | con->out_kvec_cur = con->out_kvec; |
612 | con->out_more = 0; | |
613 | set_bit(WRITE_PENDING, &con->state); | |
614 | } | |
615 | ||
616 | ||
617 | /* | |
618 | * write as much of pending kvecs to the socket as we can. | |
619 | * 1 -> done | |
620 | * 0 -> socket full, but more to do | |
621 | * <0 -> error | |
622 | */ | |
623 | static int write_partial_kvec(struct ceph_connection *con) | |
624 | { | |
625 | int ret; | |
626 | ||
627 | dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes); | |
628 | while (con->out_kvec_bytes > 0) { | |
629 | ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur, | |
630 | con->out_kvec_left, con->out_kvec_bytes, | |
631 | con->out_more); | |
632 | if (ret <= 0) | |
633 | goto out; | |
634 | con->out_kvec_bytes -= ret; | |
635 | if (con->out_kvec_bytes == 0) | |
636 | break; /* done */ | |
637 | while (ret > 0) { | |
638 | if (ret >= con->out_kvec_cur->iov_len) { | |
639 | ret -= con->out_kvec_cur->iov_len; | |
640 | con->out_kvec_cur++; | |
641 | con->out_kvec_left--; | |
642 | } else { | |
643 | con->out_kvec_cur->iov_len -= ret; | |
644 | con->out_kvec_cur->iov_base += ret; | |
645 | ret = 0; | |
646 | break; | |
647 | } | |
648 | } | |
649 | } | |
650 | con->out_kvec_left = 0; | |
651 | con->out_kvec_is_msg = false; | |
652 | ret = 1; | |
653 | out: | |
654 | dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con, | |
655 | con->out_kvec_bytes, con->out_kvec_left, ret); | |
656 | return ret; /* done! */ | |
657 | } | |
658 | ||
659 | /* | |
660 | * Write as much message data payload as we can. If we finish, queue | |
661 | * up the footer. | |
662 | * 1 -> done, footer is now queued in out_kvec[]. | |
663 | * 0 -> socket full, but more to do | |
664 | * <0 -> error | |
665 | */ | |
666 | static int write_partial_msg_pages(struct ceph_connection *con) | |
667 | { | |
668 | struct ceph_msg *msg = con->out_msg; | |
669 | unsigned data_len = le32_to_cpu(msg->hdr.data_len); | |
670 | size_t len; | |
671 | int crc = con->msgr->nocrc; | |
672 | int ret; | |
673 | ||
674 | dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n", | |
675 | con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages, | |
676 | con->out_msg_pos.page_pos); | |
677 | ||
678 | while (con->out_msg_pos.page < con->out_msg->nr_pages) { | |
679 | struct page *page = NULL; | |
680 | void *kaddr = NULL; | |
681 | ||
682 | /* | |
683 | * if we are calculating the data crc (the default), we need | |
684 | * to map the page. if our pages[] has been revoked, use the | |
685 | * zero page. | |
686 | */ | |
687 | if (msg->pages) { | |
688 | page = msg->pages[con->out_msg_pos.page]; | |
689 | if (crc) | |
690 | kaddr = kmap(page); | |
691 | } else { | |
692 | page = con->msgr->zero_page; | |
693 | if (crc) | |
694 | kaddr = page_address(con->msgr->zero_page); | |
695 | } | |
696 | len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos), | |
697 | (int)(data_len - con->out_msg_pos.data_pos)); | |
698 | if (crc && !con->out_msg_pos.did_page_crc) { | |
699 | void *base = kaddr + con->out_msg_pos.page_pos; | |
700 | u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc); | |
701 | ||
702 | BUG_ON(kaddr == NULL); | |
703 | con->out_msg->footer.data_crc = | |
704 | cpu_to_le32(crc32c(tmpcrc, base, len)); | |
705 | con->out_msg_pos.did_page_crc = 1; | |
706 | } | |
707 | ||
708 | ret = kernel_sendpage(con->sock, page, | |
709 | con->out_msg_pos.page_pos, len, | |
710 | MSG_DONTWAIT | MSG_NOSIGNAL | | |
711 | MSG_MORE); | |
712 | ||
713 | if (crc && msg->pages) | |
714 | kunmap(page); | |
715 | ||
716 | if (ret <= 0) | |
717 | goto out; | |
718 | ||
719 | con->out_msg_pos.data_pos += ret; | |
720 | con->out_msg_pos.page_pos += ret; | |
721 | if (ret == len) { | |
722 | con->out_msg_pos.page_pos = 0; | |
723 | con->out_msg_pos.page++; | |
724 | con->out_msg_pos.did_page_crc = 0; | |
725 | } | |
726 | } | |
727 | ||
728 | dout("write_partial_msg_pages %p msg %p done\n", con, msg); | |
729 | ||
730 | /* prepare and queue up footer, too */ | |
731 | if (!crc) | |
732 | con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC; | |
733 | con->out_kvec_bytes = 0; | |
734 | con->out_kvec_left = 0; | |
735 | con->out_kvec_cur = con->out_kvec; | |
736 | prepare_write_message_footer(con, 0); | |
737 | ret = 1; | |
738 | out: | |
739 | return ret; | |
740 | } | |
741 | ||
742 | /* | |
743 | * write some zeros | |
744 | */ | |
745 | static int write_partial_skip(struct ceph_connection *con) | |
746 | { | |
747 | int ret; | |
748 | ||
749 | while (con->out_skip > 0) { | |
750 | struct kvec iov = { | |
751 | .iov_base = page_address(con->msgr->zero_page), | |
752 | .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE) | |
753 | }; | |
754 | ||
755 | ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1); | |
756 | if (ret <= 0) | |
757 | goto out; | |
758 | con->out_skip -= ret; | |
759 | } | |
760 | ret = 1; | |
761 | out: | |
762 | return ret; | |
763 | } | |
764 | ||
765 | /* | |
766 | * Prepare to read connection handshake, or an ack. | |
767 | */ | |
eed0ef2c SW |
768 | static void prepare_read_banner(struct ceph_connection *con) |
769 | { | |
770 | dout("prepare_read_banner %p\n", con); | |
771 | con->in_base_pos = 0; | |
772 | } | |
773 | ||
31b8006e SW |
774 | static void prepare_read_connect(struct ceph_connection *con) |
775 | { | |
776 | dout("prepare_read_connect %p\n", con); | |
777 | con->in_base_pos = 0; | |
778 | } | |
779 | ||
780 | static void prepare_read_ack(struct ceph_connection *con) | |
781 | { | |
782 | dout("prepare_read_ack %p\n", con); | |
783 | con->in_base_pos = 0; | |
784 | } | |
785 | ||
786 | static void prepare_read_tag(struct ceph_connection *con) | |
787 | { | |
788 | dout("prepare_read_tag %p\n", con); | |
789 | con->in_base_pos = 0; | |
790 | con->in_tag = CEPH_MSGR_TAG_READY; | |
791 | } | |
792 | ||
793 | /* | |
794 | * Prepare to read a message. | |
795 | */ | |
796 | static int prepare_read_message(struct ceph_connection *con) | |
797 | { | |
798 | dout("prepare_read_message %p\n", con); | |
799 | BUG_ON(con->in_msg != NULL); | |
800 | con->in_base_pos = 0; | |
801 | con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0; | |
802 | return 0; | |
803 | } | |
804 | ||
805 | ||
806 | static int read_partial(struct ceph_connection *con, | |
807 | int *to, int size, void *object) | |
808 | { | |
809 | *to += size; | |
810 | while (con->in_base_pos < *to) { | |
811 | int left = *to - con->in_base_pos; | |
812 | int have = size - left; | |
813 | int ret = ceph_tcp_recvmsg(con->sock, object + have, left); | |
814 | if (ret <= 0) | |
815 | return ret; | |
816 | con->in_base_pos += ret; | |
817 | } | |
818 | return 1; | |
819 | } | |
820 | ||
821 | ||
822 | /* | |
823 | * Read all or part of the connect-side handshake on a new connection | |
824 | */ | |
eed0ef2c | 825 | static int read_partial_banner(struct ceph_connection *con) |
31b8006e SW |
826 | { |
827 | int ret, to = 0; | |
828 | ||
eed0ef2c | 829 | dout("read_partial_banner %p at %d\n", con, con->in_base_pos); |
31b8006e SW |
830 | |
831 | /* peer's banner */ | |
832 | ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner); | |
833 | if (ret <= 0) | |
834 | goto out; | |
835 | ret = read_partial(con, &to, sizeof(con->actual_peer_addr), | |
836 | &con->actual_peer_addr); | |
837 | if (ret <= 0) | |
838 | goto out; | |
839 | ret = read_partial(con, &to, sizeof(con->peer_addr_for_me), | |
840 | &con->peer_addr_for_me); | |
841 | if (ret <= 0) | |
842 | goto out; | |
eed0ef2c SW |
843 | out: |
844 | return ret; | |
845 | } | |
846 | ||
847 | static int read_partial_connect(struct ceph_connection *con) | |
848 | { | |
849 | int ret, to = 0; | |
850 | ||
851 | dout("read_partial_connect %p at %d\n", con, con->in_base_pos); | |
852 | ||
31b8006e SW |
853 | ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply); |
854 | if (ret <= 0) | |
855 | goto out; | |
856 | ||
857 | dout("read_partial_connect %p connect_seq = %u, global_seq = %u\n", | |
858 | con, le32_to_cpu(con->in_reply.connect_seq), | |
859 | le32_to_cpu(con->in_reply.global_seq)); | |
860 | out: | |
861 | return ret; | |
eed0ef2c | 862 | |
31b8006e SW |
863 | } |
864 | ||
865 | /* | |
866 | * Verify the hello banner looks okay. | |
867 | */ | |
868 | static int verify_hello(struct ceph_connection *con) | |
869 | { | |
870 | if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) { | |
13e38c8a | 871 | pr_err("connect to %s got bad banner\n", |
31b8006e SW |
872 | pr_addr(&con->peer_addr.in_addr)); |
873 | con->error_msg = "protocol error, bad banner"; | |
874 | return -1; | |
875 | } | |
876 | return 0; | |
877 | } | |
878 | ||
879 | static bool addr_is_blank(struct sockaddr_storage *ss) | |
880 | { | |
881 | switch (ss->ss_family) { | |
882 | case AF_INET: | |
883 | return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0; | |
884 | case AF_INET6: | |
885 | return | |
886 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 && | |
887 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 && | |
888 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 && | |
889 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0; | |
890 | } | |
891 | return false; | |
892 | } | |
893 | ||
894 | static int addr_port(struct sockaddr_storage *ss) | |
895 | { | |
896 | switch (ss->ss_family) { | |
897 | case AF_INET: | |
f28bcfbe | 898 | return ntohs(((struct sockaddr_in *)ss)->sin_port); |
31b8006e | 899 | case AF_INET6: |
f28bcfbe | 900 | return ntohs(((struct sockaddr_in6 *)ss)->sin6_port); |
31b8006e SW |
901 | } |
902 | return 0; | |
903 | } | |
904 | ||
905 | static void addr_set_port(struct sockaddr_storage *ss, int p) | |
906 | { | |
907 | switch (ss->ss_family) { | |
908 | case AF_INET: | |
909 | ((struct sockaddr_in *)ss)->sin_port = htons(p); | |
910 | case AF_INET6: | |
911 | ((struct sockaddr_in6 *)ss)->sin6_port = htons(p); | |
912 | } | |
913 | } | |
914 | ||
915 | /* | |
916 | * Parse an ip[:port] list into an addr array. Use the default | |
917 | * monitor port if a port isn't specified. | |
918 | */ | |
919 | int ceph_parse_ips(const char *c, const char *end, | |
920 | struct ceph_entity_addr *addr, | |
921 | int max_count, int *count) | |
922 | { | |
923 | int i; | |
924 | const char *p = c; | |
925 | ||
926 | dout("parse_ips on '%.*s'\n", (int)(end-c), c); | |
927 | for (i = 0; i < max_count; i++) { | |
928 | const char *ipend; | |
929 | struct sockaddr_storage *ss = &addr[i].in_addr; | |
930 | struct sockaddr_in *in4 = (void *)ss; | |
931 | struct sockaddr_in6 *in6 = (void *)ss; | |
932 | int port; | |
933 | ||
934 | memset(ss, 0, sizeof(*ss)); | |
935 | if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr, | |
936 | ',', &ipend)) { | |
937 | ss->ss_family = AF_INET; | |
938 | } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr, | |
939 | ',', &ipend)) { | |
940 | ss->ss_family = AF_INET6; | |
941 | } else { | |
942 | goto bad; | |
943 | } | |
944 | p = ipend; | |
945 | ||
946 | /* port? */ | |
947 | if (p < end && *p == ':') { | |
948 | port = 0; | |
949 | p++; | |
950 | while (p < end && *p >= '0' && *p <= '9') { | |
951 | port = (port * 10) + (*p - '0'); | |
952 | p++; | |
953 | } | |
954 | if (port > 65535 || port == 0) | |
955 | goto bad; | |
956 | } else { | |
957 | port = CEPH_MON_PORT; | |
958 | } | |
959 | ||
960 | addr_set_port(ss, port); | |
961 | ||
962 | dout("parse_ips got %s\n", pr_addr(ss)); | |
963 | ||
964 | if (p == end) | |
965 | break; | |
966 | if (*p != ',') | |
967 | goto bad; | |
968 | p++; | |
969 | } | |
970 | ||
971 | if (p != end) | |
972 | goto bad; | |
973 | ||
974 | if (count) | |
975 | *count = i + 1; | |
976 | return 0; | |
977 | ||
978 | bad: | |
979 | pr_err("parse_ips bad ip '%s'\n", c); | |
980 | return -EINVAL; | |
981 | } | |
982 | ||
eed0ef2c | 983 | static int process_banner(struct ceph_connection *con) |
31b8006e | 984 | { |
eed0ef2c | 985 | dout("process_banner on %p\n", con); |
31b8006e SW |
986 | |
987 | if (verify_hello(con) < 0) | |
988 | return -1; | |
989 | ||
63f2d211 SW |
990 | ceph_decode_addr(&con->actual_peer_addr); |
991 | ceph_decode_addr(&con->peer_addr_for_me); | |
992 | ||
31b8006e SW |
993 | /* |
994 | * Make sure the other end is who we wanted. note that the other | |
995 | * end may not yet know their ip address, so if it's 0.0.0.0, give | |
996 | * them the benefit of the doubt. | |
997 | */ | |
998 | if (!ceph_entity_addr_is_local(&con->peer_addr, | |
999 | &con->actual_peer_addr) && | |
1000 | !(addr_is_blank(&con->actual_peer_addr.in_addr) && | |
1001 | con->actual_peer_addr.nonce == con->peer_addr.nonce)) { | |
1002 | pr_err("wrong peer, want %s/%d, " | |
1003 | "got %s/%d, wtf\n", | |
1004 | pr_addr(&con->peer_addr.in_addr), | |
1005 | con->peer_addr.nonce, | |
1006 | pr_addr(&con->actual_peer_addr.in_addr), | |
1007 | con->actual_peer_addr.nonce); | |
1008 | con->error_msg = "protocol error, wrong peer"; | |
1009 | return -1; | |
1010 | } | |
1011 | ||
1012 | /* | |
1013 | * did we learn our address? | |
1014 | */ | |
1015 | if (addr_is_blank(&con->msgr->inst.addr.in_addr)) { | |
1016 | int port = addr_port(&con->msgr->inst.addr.in_addr); | |
1017 | ||
1018 | memcpy(&con->msgr->inst.addr.in_addr, | |
1019 | &con->peer_addr_for_me.in_addr, | |
1020 | sizeof(con->peer_addr_for_me.in_addr)); | |
1021 | addr_set_port(&con->msgr->inst.addr.in_addr, port); | |
63f2d211 | 1022 | encode_my_addr(con->msgr); |
eed0ef2c | 1023 | dout("process_banner learned my addr is %s\n", |
31b8006e SW |
1024 | pr_addr(&con->msgr->inst.addr.in_addr)); |
1025 | } | |
1026 | ||
eed0ef2c SW |
1027 | set_bit(NEGOTIATING, &con->state); |
1028 | prepare_read_connect(con); | |
1029 | return 0; | |
1030 | } | |
1031 | ||
1032 | static int process_connect(struct ceph_connection *con) | |
1033 | { | |
1034 | dout("process_connect on %p tag %d\n", con, (int)con->in_tag); | |
1035 | ||
31b8006e SW |
1036 | switch (con->in_reply.tag) { |
1037 | case CEPH_MSGR_TAG_BADPROTOVER: | |
1038 | dout("process_connect got BADPROTOVER my %d != their %d\n", | |
1039 | le32_to_cpu(con->out_connect.protocol_version), | |
1040 | le32_to_cpu(con->in_reply.protocol_version)); | |
1041 | pr_err("%s%lld %s protocol version mismatch," | |
1042 | " my %d != server's %d\n", | |
1043 | ENTITY_NAME(con->peer_name), | |
1044 | pr_addr(&con->peer_addr.in_addr), | |
1045 | le32_to_cpu(con->out_connect.protocol_version), | |
1046 | le32_to_cpu(con->in_reply.protocol_version)); | |
1047 | con->error_msg = "protocol version mismatch"; | |
1048 | if (con->ops->bad_proto) | |
1049 | con->ops->bad_proto(con); | |
1050 | reset_connection(con); | |
1051 | set_bit(CLOSED, &con->state); /* in case there's queued work */ | |
1052 | return -1; | |
1053 | ||
1054 | ||
1055 | case CEPH_MSGR_TAG_RESETSESSION: | |
1056 | /* | |
1057 | * If we connected with a large connect_seq but the peer | |
1058 | * has no record of a session with us (no connection, or | |
1059 | * connect_seq == 0), they will send RESETSESION to indicate | |
1060 | * that they must have reset their session, and may have | |
1061 | * dropped messages. | |
1062 | */ | |
1063 | dout("process_connect got RESET peer seq %u\n", | |
1064 | le32_to_cpu(con->in_connect.connect_seq)); | |
1065 | pr_err("%s%lld %s connection reset\n", | |
1066 | ENTITY_NAME(con->peer_name), | |
1067 | pr_addr(&con->peer_addr.in_addr)); | |
1068 | reset_connection(con); | |
eed0ef2c | 1069 | prepare_write_connect(con->msgr, con, 0); |
31b8006e SW |
1070 | prepare_read_connect(con); |
1071 | ||
1072 | /* Tell ceph about it. */ | |
1073 | pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name)); | |
1074 | if (con->ops->peer_reset) | |
1075 | con->ops->peer_reset(con); | |
1076 | break; | |
1077 | ||
1078 | case CEPH_MSGR_TAG_RETRY_SESSION: | |
1079 | /* | |
1080 | * If we sent a smaller connect_seq than the peer has, try | |
1081 | * again with a larger value. | |
1082 | */ | |
1083 | dout("process_connect got RETRY my seq = %u, peer_seq = %u\n", | |
1084 | le32_to_cpu(con->out_connect.connect_seq), | |
1085 | le32_to_cpu(con->in_connect.connect_seq)); | |
1086 | con->connect_seq = le32_to_cpu(con->in_connect.connect_seq); | |
eed0ef2c | 1087 | prepare_write_connect(con->msgr, con, 0); |
31b8006e SW |
1088 | prepare_read_connect(con); |
1089 | break; | |
1090 | ||
1091 | case CEPH_MSGR_TAG_RETRY_GLOBAL: | |
1092 | /* | |
1093 | * If we sent a smaller global_seq than the peer has, try | |
1094 | * again with a larger value. | |
1095 | */ | |
eed0ef2c | 1096 | dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n", |
31b8006e SW |
1097 | con->peer_global_seq, |
1098 | le32_to_cpu(con->in_connect.global_seq)); | |
1099 | get_global_seq(con->msgr, | |
1100 | le32_to_cpu(con->in_connect.global_seq)); | |
eed0ef2c | 1101 | prepare_write_connect(con->msgr, con, 0); |
31b8006e SW |
1102 | prepare_read_connect(con); |
1103 | break; | |
1104 | ||
1105 | case CEPH_MSGR_TAG_READY: | |
1106 | clear_bit(CONNECTING, &con->state); | |
31b8006e SW |
1107 | con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq); |
1108 | con->connect_seq++; | |
1109 | dout("process_connect got READY gseq %d cseq %d (%d)\n", | |
1110 | con->peer_global_seq, | |
1111 | le32_to_cpu(con->in_reply.connect_seq), | |
1112 | con->connect_seq); | |
1113 | WARN_ON(con->connect_seq != | |
1114 | le32_to_cpu(con->in_reply.connect_seq)); | |
1115 | ||
1116 | con->delay = 0; /* reset backoff memory */ | |
1117 | prepare_read_tag(con); | |
1118 | break; | |
1119 | ||
1120 | case CEPH_MSGR_TAG_WAIT: | |
1121 | /* | |
1122 | * If there is a connection race (we are opening | |
1123 | * connections to each other), one of us may just have | |
1124 | * to WAIT. This shouldn't happen if we are the | |
1125 | * client. | |
1126 | */ | |
1127 | pr_err("process_connect peer connecting WAIT\n"); | |
1128 | ||
1129 | default: | |
1130 | pr_err("connect protocol error, will retry\n"); | |
1131 | con->error_msg = "protocol error, garbage tag during connect"; | |
1132 | return -1; | |
1133 | } | |
1134 | return 0; | |
1135 | } | |
1136 | ||
1137 | ||
1138 | /* | |
1139 | * read (part of) an ack | |
1140 | */ | |
1141 | static int read_partial_ack(struct ceph_connection *con) | |
1142 | { | |
1143 | int to = 0; | |
1144 | ||
1145 | return read_partial(con, &to, sizeof(con->in_temp_ack), | |
1146 | &con->in_temp_ack); | |
1147 | } | |
1148 | ||
1149 | ||
1150 | /* | |
1151 | * We can finally discard anything that's been acked. | |
1152 | */ | |
1153 | static void process_ack(struct ceph_connection *con) | |
1154 | { | |
1155 | struct ceph_msg *m; | |
1156 | u64 ack = le64_to_cpu(con->in_temp_ack); | |
1157 | u64 seq; | |
1158 | ||
1159 | mutex_lock(&con->out_mutex); | |
1160 | while (!list_empty(&con->out_sent)) { | |
1161 | m = list_first_entry(&con->out_sent, struct ceph_msg, | |
1162 | list_head); | |
1163 | seq = le64_to_cpu(m->hdr.seq); | |
1164 | if (seq > ack) | |
1165 | break; | |
1166 | dout("got ack for seq %llu type %d at %p\n", seq, | |
1167 | le16_to_cpu(m->hdr.type), m); | |
1168 | ceph_msg_remove(m); | |
1169 | } | |
1170 | mutex_unlock(&con->out_mutex); | |
1171 | prepare_read_tag(con); | |
1172 | } | |
1173 | ||
1174 | ||
1175 | ||
1176 | ||
1177 | ||
1178 | ||
1179 | /* | |
1180 | * read (part of) a message. | |
1181 | */ | |
1182 | static int read_partial_message(struct ceph_connection *con) | |
1183 | { | |
1184 | struct ceph_msg *m = con->in_msg; | |
1185 | void *p; | |
1186 | int ret; | |
1187 | int to, want, left; | |
1188 | unsigned front_len, middle_len, data_len, data_off; | |
1189 | int datacrc = con->msgr->nocrc; | |
1190 | ||
1191 | dout("read_partial_message con %p msg %p\n", con, m); | |
1192 | ||
1193 | /* header */ | |
1194 | while (con->in_base_pos < sizeof(con->in_hdr)) { | |
1195 | left = sizeof(con->in_hdr) - con->in_base_pos; | |
1196 | ret = ceph_tcp_recvmsg(con->sock, | |
1197 | (char *)&con->in_hdr + con->in_base_pos, | |
1198 | left); | |
1199 | if (ret <= 0) | |
1200 | return ret; | |
1201 | con->in_base_pos += ret; | |
1202 | if (con->in_base_pos == sizeof(con->in_hdr)) { | |
1203 | u32 crc = crc32c(0, (void *)&con->in_hdr, | |
1204 | sizeof(con->in_hdr) - sizeof(con->in_hdr.crc)); | |
1205 | if (crc != le32_to_cpu(con->in_hdr.crc)) { | |
1206 | pr_err("read_partial_message bad hdr " | |
1207 | " crc %u != expected %u\n", | |
1208 | crc, con->in_hdr.crc); | |
1209 | return -EBADMSG; | |
1210 | } | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | front_len = le32_to_cpu(con->in_hdr.front_len); | |
1215 | if (front_len > CEPH_MSG_MAX_FRONT_LEN) | |
1216 | return -EIO; | |
1217 | middle_len = le32_to_cpu(con->in_hdr.middle_len); | |
1218 | if (middle_len > CEPH_MSG_MAX_DATA_LEN) | |
1219 | return -EIO; | |
1220 | data_len = le32_to_cpu(con->in_hdr.data_len); | |
1221 | if (data_len > CEPH_MSG_MAX_DATA_LEN) | |
1222 | return -EIO; | |
1223 | ||
1224 | /* allocate message? */ | |
1225 | if (!con->in_msg) { | |
1226 | dout("got hdr type %d front %d data %d\n", con->in_hdr.type, | |
1227 | con->in_hdr.front_len, con->in_hdr.data_len); | |
1228 | con->in_msg = con->ops->alloc_msg(con, &con->in_hdr); | |
1229 | if (!con->in_msg) { | |
1230 | /* skip this message */ | |
1231 | dout("alloc_msg returned NULL, skipping message\n"); | |
1232 | con->in_base_pos = -front_len - middle_len - data_len - | |
1233 | sizeof(m->footer); | |
1234 | con->in_tag = CEPH_MSGR_TAG_READY; | |
1235 | return 0; | |
1236 | } | |
1237 | if (IS_ERR(con->in_msg)) { | |
1238 | ret = PTR_ERR(con->in_msg); | |
1239 | con->in_msg = NULL; | |
1240 | con->error_msg = "out of memory for incoming message"; | |
1241 | return ret; | |
1242 | } | |
1243 | m = con->in_msg; | |
1244 | m->front.iov_len = 0; /* haven't read it yet */ | |
1245 | memcpy(&m->hdr, &con->in_hdr, sizeof(con->in_hdr)); | |
1246 | } | |
1247 | ||
1248 | /* front */ | |
1249 | while (m->front.iov_len < front_len) { | |
1250 | BUG_ON(m->front.iov_base == NULL); | |
1251 | left = front_len - m->front.iov_len; | |
1252 | ret = ceph_tcp_recvmsg(con->sock, (char *)m->front.iov_base + | |
1253 | m->front.iov_len, left); | |
1254 | if (ret <= 0) | |
1255 | return ret; | |
1256 | m->front.iov_len += ret; | |
1257 | if (m->front.iov_len == front_len) | |
1258 | con->in_front_crc = crc32c(0, m->front.iov_base, | |
1259 | m->front.iov_len); | |
1260 | } | |
1261 | ||
1262 | /* middle */ | |
1263 | while (middle_len > 0 && (!m->middle || | |
1264 | m->middle->vec.iov_len < middle_len)) { | |
1265 | if (m->middle == NULL) { | |
1266 | ret = -EOPNOTSUPP; | |
1267 | if (con->ops->alloc_middle) | |
1268 | ret = con->ops->alloc_middle(con, m); | |
1269 | if (ret < 0) { | |
1270 | dout("alloc_middle failed, skipping payload\n"); | |
1271 | con->in_base_pos = -middle_len - data_len | |
1272 | - sizeof(m->footer); | |
1273 | ceph_msg_put(con->in_msg); | |
1274 | con->in_msg = NULL; | |
1275 | con->in_tag = CEPH_MSGR_TAG_READY; | |
1276 | return 0; | |
1277 | } | |
1278 | m->middle->vec.iov_len = 0; | |
1279 | } | |
1280 | left = middle_len - m->middle->vec.iov_len; | |
1281 | ret = ceph_tcp_recvmsg(con->sock, | |
1282 | (char *)m->middle->vec.iov_base + | |
1283 | m->middle->vec.iov_len, left); | |
1284 | if (ret <= 0) | |
1285 | return ret; | |
1286 | m->middle->vec.iov_len += ret; | |
1287 | if (m->middle->vec.iov_len == middle_len) | |
1288 | con->in_middle_crc = crc32c(0, m->middle->vec.iov_base, | |
1289 | m->middle->vec.iov_len); | |
1290 | } | |
1291 | ||
1292 | /* (page) data */ | |
1293 | data_off = le16_to_cpu(m->hdr.data_off); | |
1294 | if (data_len == 0) | |
1295 | goto no_data; | |
1296 | ||
1297 | if (m->nr_pages == 0) { | |
1298 | con->in_msg_pos.page = 0; | |
1299 | con->in_msg_pos.page_pos = data_off & ~PAGE_MASK; | |
1300 | con->in_msg_pos.data_pos = 0; | |
1301 | /* find pages for data payload */ | |
1302 | want = calc_pages_for(data_off & ~PAGE_MASK, data_len); | |
1303 | ret = -1; | |
1304 | if (con->ops->prepare_pages) | |
1305 | ret = con->ops->prepare_pages(con, m, want); | |
1306 | if (ret < 0) { | |
1307 | dout("%p prepare_pages failed, skipping payload\n", m); | |
1308 | con->in_base_pos = -data_len - sizeof(m->footer); | |
1309 | ceph_msg_put(con->in_msg); | |
1310 | con->in_msg = NULL; | |
1311 | con->in_tag = CEPH_MSGR_TAG_READY; | |
1312 | return 0; | |
1313 | } | |
1314 | BUG_ON(m->nr_pages < want); | |
1315 | } | |
1316 | while (con->in_msg_pos.data_pos < data_len) { | |
1317 | left = min((int)(data_len - con->in_msg_pos.data_pos), | |
1318 | (int)(PAGE_SIZE - con->in_msg_pos.page_pos)); | |
1319 | BUG_ON(m->pages == NULL); | |
1320 | p = kmap(m->pages[con->in_msg_pos.page]); | |
1321 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, | |
1322 | left); | |
1323 | if (ret > 0 && datacrc) | |
1324 | con->in_data_crc = | |
1325 | crc32c(con->in_data_crc, | |
1326 | p + con->in_msg_pos.page_pos, ret); | |
1327 | kunmap(m->pages[con->in_msg_pos.page]); | |
1328 | if (ret <= 0) | |
1329 | return ret; | |
1330 | con->in_msg_pos.data_pos += ret; | |
1331 | con->in_msg_pos.page_pos += ret; | |
1332 | if (con->in_msg_pos.page_pos == PAGE_SIZE) { | |
1333 | con->in_msg_pos.page_pos = 0; | |
1334 | con->in_msg_pos.page++; | |
1335 | } | |
1336 | } | |
1337 | ||
1338 | no_data: | |
1339 | /* footer */ | |
1340 | to = sizeof(m->hdr) + sizeof(m->footer); | |
1341 | while (con->in_base_pos < to) { | |
1342 | left = to - con->in_base_pos; | |
1343 | ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer + | |
1344 | (con->in_base_pos - sizeof(m->hdr)), | |
1345 | left); | |
1346 | if (ret <= 0) | |
1347 | return ret; | |
1348 | con->in_base_pos += ret; | |
1349 | } | |
1350 | dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n", | |
1351 | m, front_len, m->footer.front_crc, middle_len, | |
1352 | m->footer.middle_crc, data_len, m->footer.data_crc); | |
1353 | ||
1354 | /* crc ok? */ | |
1355 | if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) { | |
1356 | pr_err("read_partial_message %p front crc %u != exp. %u\n", | |
1357 | m, con->in_front_crc, m->footer.front_crc); | |
1358 | return -EBADMSG; | |
1359 | } | |
1360 | if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) { | |
1361 | pr_err("read_partial_message %p middle crc %u != exp %u\n", | |
1362 | m, con->in_middle_crc, m->footer.middle_crc); | |
1363 | return -EBADMSG; | |
1364 | } | |
1365 | if (datacrc && | |
1366 | (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 && | |
1367 | con->in_data_crc != le32_to_cpu(m->footer.data_crc)) { | |
1368 | pr_err("read_partial_message %p data crc %u != exp. %u\n", m, | |
1369 | con->in_data_crc, le32_to_cpu(m->footer.data_crc)); | |
1370 | return -EBADMSG; | |
1371 | } | |
1372 | ||
1373 | return 1; /* done! */ | |
1374 | } | |
1375 | ||
1376 | /* | |
1377 | * Process message. This happens in the worker thread. The callback should | |
1378 | * be careful not to do anything that waits on other incoming messages or it | |
1379 | * may deadlock. | |
1380 | */ | |
1381 | static void process_message(struct ceph_connection *con) | |
1382 | { | |
1383 | struct ceph_msg *msg = con->in_msg; | |
1384 | ||
1385 | con->in_msg = NULL; | |
1386 | ||
1387 | /* if first message, set peer_name */ | |
1388 | if (con->peer_name.type == 0) | |
1389 | con->peer_name = msg->hdr.src.name; | |
1390 | ||
1391 | mutex_lock(&con->out_mutex); | |
1392 | con->in_seq++; | |
1393 | mutex_unlock(&con->out_mutex); | |
1394 | ||
1395 | dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n", | |
1396 | msg, le64_to_cpu(msg->hdr.seq), | |
1397 | ENTITY_NAME(msg->hdr.src.name), | |
1398 | le16_to_cpu(msg->hdr.type), | |
1399 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), | |
1400 | le32_to_cpu(msg->hdr.front_len), | |
1401 | le32_to_cpu(msg->hdr.data_len), | |
1402 | con->in_front_crc, con->in_middle_crc, con->in_data_crc); | |
1403 | con->ops->dispatch(con, msg); | |
1404 | prepare_read_tag(con); | |
1405 | } | |
1406 | ||
1407 | ||
1408 | /* | |
1409 | * Write something to the socket. Called in a worker thread when the | |
1410 | * socket appears to be writeable and we have something ready to send. | |
1411 | */ | |
1412 | static int try_write(struct ceph_connection *con) | |
1413 | { | |
1414 | struct ceph_messenger *msgr = con->msgr; | |
1415 | int ret = 1; | |
1416 | ||
1417 | dout("try_write start %p state %lu nref %d\n", con, con->state, | |
1418 | atomic_read(&con->nref)); | |
1419 | ||
1420 | mutex_lock(&con->out_mutex); | |
1421 | more: | |
1422 | dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes); | |
1423 | ||
1424 | /* open the socket first? */ | |
1425 | if (con->sock == NULL) { | |
1426 | /* | |
1427 | * if we were STANDBY and are reconnecting _this_ | |
1428 | * connection, bump connect_seq now. Always bump | |
1429 | * global_seq. | |
1430 | */ | |
1431 | if (test_and_clear_bit(STANDBY, &con->state)) | |
1432 | con->connect_seq++; | |
1433 | ||
eed0ef2c SW |
1434 | prepare_write_banner(msgr, con); |
1435 | prepare_write_connect(msgr, con, 1); | |
1436 | prepare_read_banner(con); | |
31b8006e | 1437 | set_bit(CONNECTING, &con->state); |
eed0ef2c | 1438 | clear_bit(NEGOTIATING, &con->state); |
31b8006e SW |
1439 | |
1440 | con->in_tag = CEPH_MSGR_TAG_READY; | |
1441 | dout("try_write initiating connect on %p new state %lu\n", | |
1442 | con, con->state); | |
1443 | con->sock = ceph_tcp_connect(con); | |
1444 | if (IS_ERR(con->sock)) { | |
1445 | con->sock = NULL; | |
1446 | con->error_msg = "connect error"; | |
1447 | ret = -1; | |
1448 | goto out; | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | more_kvec: | |
1453 | /* kvec data queued? */ | |
1454 | if (con->out_skip) { | |
1455 | ret = write_partial_skip(con); | |
1456 | if (ret <= 0) | |
1457 | goto done; | |
1458 | if (ret < 0) { | |
1459 | dout("try_write write_partial_skip err %d\n", ret); | |
1460 | goto done; | |
1461 | } | |
1462 | } | |
1463 | if (con->out_kvec_left) { | |
1464 | ret = write_partial_kvec(con); | |
1465 | if (ret <= 0) | |
1466 | goto done; | |
1467 | if (ret < 0) { | |
1468 | dout("try_write write_partial_kvec err %d\n", ret); | |
1469 | goto done; | |
1470 | } | |
1471 | } | |
1472 | ||
1473 | /* msg pages? */ | |
1474 | if (con->out_msg) { | |
1475 | ret = write_partial_msg_pages(con); | |
1476 | if (ret == 1) | |
1477 | goto more_kvec; /* we need to send the footer, too! */ | |
1478 | if (ret == 0) | |
1479 | goto done; | |
1480 | if (ret < 0) { | |
1481 | dout("try_write write_partial_msg_pages err %d\n", | |
1482 | ret); | |
1483 | goto done; | |
1484 | } | |
1485 | } | |
1486 | ||
1487 | if (!test_bit(CONNECTING, &con->state)) { | |
1488 | /* is anything else pending? */ | |
1489 | if (!list_empty(&con->out_queue)) { | |
1490 | prepare_write_message(con); | |
1491 | goto more; | |
1492 | } | |
1493 | if (con->in_seq > con->in_seq_acked) { | |
1494 | prepare_write_ack(con); | |
1495 | goto more; | |
1496 | } | |
1497 | if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) { | |
1498 | prepare_write_keepalive(con); | |
1499 | goto more; | |
1500 | } | |
1501 | } | |
1502 | ||
1503 | /* Nothing to do! */ | |
1504 | clear_bit(WRITE_PENDING, &con->state); | |
1505 | dout("try_write nothing else to write.\n"); | |
1506 | done: | |
1507 | ret = 0; | |
1508 | out: | |
1509 | mutex_unlock(&con->out_mutex); | |
1510 | dout("try_write done on %p\n", con); | |
1511 | return ret; | |
1512 | } | |
1513 | ||
1514 | ||
1515 | ||
1516 | /* | |
1517 | * Read what we can from the socket. | |
1518 | */ | |
1519 | static int try_read(struct ceph_connection *con) | |
1520 | { | |
1521 | struct ceph_messenger *msgr; | |
1522 | int ret = -1; | |
1523 | ||
1524 | if (!con->sock) | |
1525 | return 0; | |
1526 | ||
1527 | if (test_bit(STANDBY, &con->state)) | |
1528 | return 0; | |
1529 | ||
1530 | dout("try_read start on %p\n", con); | |
1531 | msgr = con->msgr; | |
1532 | ||
1533 | more: | |
1534 | dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag, | |
1535 | con->in_base_pos); | |
1536 | if (test_bit(CONNECTING, &con->state)) { | |
eed0ef2c SW |
1537 | if (!test_bit(NEGOTIATING, &con->state)) { |
1538 | dout("try_read connecting\n"); | |
1539 | ret = read_partial_banner(con); | |
1540 | if (ret <= 0) | |
1541 | goto done; | |
1542 | if (process_banner(con) < 0) { | |
1543 | ret = -1; | |
1544 | goto out; | |
1545 | } | |
1546 | } | |
31b8006e SW |
1547 | ret = read_partial_connect(con); |
1548 | if (ret <= 0) | |
1549 | goto done; | |
1550 | if (process_connect(con) < 0) { | |
1551 | ret = -1; | |
1552 | goto out; | |
1553 | } | |
1554 | goto more; | |
1555 | } | |
1556 | ||
1557 | if (con->in_base_pos < 0) { | |
1558 | /* | |
1559 | * skipping + discarding content. | |
1560 | * | |
1561 | * FIXME: there must be a better way to do this! | |
1562 | */ | |
1563 | static char buf[1024]; | |
1564 | int skip = min(1024, -con->in_base_pos); | |
1565 | dout("skipping %d / %d bytes\n", skip, -con->in_base_pos); | |
1566 | ret = ceph_tcp_recvmsg(con->sock, buf, skip); | |
1567 | if (ret <= 0) | |
1568 | goto done; | |
1569 | con->in_base_pos += ret; | |
1570 | if (con->in_base_pos) | |
1571 | goto more; | |
1572 | } | |
1573 | if (con->in_tag == CEPH_MSGR_TAG_READY) { | |
1574 | /* | |
1575 | * what's next? | |
1576 | */ | |
1577 | ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1); | |
1578 | if (ret <= 0) | |
1579 | goto done; | |
1580 | dout("try_read got tag %d\n", (int)con->in_tag); | |
1581 | switch (con->in_tag) { | |
1582 | case CEPH_MSGR_TAG_MSG: | |
1583 | prepare_read_message(con); | |
1584 | break; | |
1585 | case CEPH_MSGR_TAG_ACK: | |
1586 | prepare_read_ack(con); | |
1587 | break; | |
1588 | case CEPH_MSGR_TAG_CLOSE: | |
1589 | set_bit(CLOSED, &con->state); /* fixme */ | |
1590 | goto done; | |
1591 | default: | |
1592 | goto bad_tag; | |
1593 | } | |
1594 | } | |
1595 | if (con->in_tag == CEPH_MSGR_TAG_MSG) { | |
1596 | ret = read_partial_message(con); | |
1597 | if (ret <= 0) { | |
1598 | switch (ret) { | |
1599 | case -EBADMSG: | |
1600 | con->error_msg = "bad crc"; | |
1601 | ret = -EIO; | |
1602 | goto out; | |
1603 | case -EIO: | |
1604 | con->error_msg = "io error"; | |
1605 | goto out; | |
1606 | default: | |
1607 | goto done; | |
1608 | } | |
1609 | } | |
1610 | if (con->in_tag == CEPH_MSGR_TAG_READY) | |
1611 | goto more; | |
1612 | process_message(con); | |
1613 | goto more; | |
1614 | } | |
1615 | if (con->in_tag == CEPH_MSGR_TAG_ACK) { | |
1616 | ret = read_partial_ack(con); | |
1617 | if (ret <= 0) | |
1618 | goto done; | |
1619 | process_ack(con); | |
1620 | goto more; | |
1621 | } | |
1622 | ||
1623 | done: | |
1624 | ret = 0; | |
1625 | out: | |
1626 | dout("try_read done on %p\n", con); | |
1627 | return ret; | |
1628 | ||
1629 | bad_tag: | |
1630 | pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag); | |
1631 | con->error_msg = "protocol error, garbage tag"; | |
1632 | ret = -1; | |
1633 | goto out; | |
1634 | } | |
1635 | ||
1636 | ||
1637 | /* | |
1638 | * Atomically queue work on a connection. Bump @con reference to | |
1639 | * avoid races with connection teardown. | |
1640 | * | |
1641 | * There is some trickery going on with QUEUED and BUSY because we | |
1642 | * only want a _single_ thread operating on each connection at any | |
1643 | * point in time, but we want to use all available CPUs. | |
1644 | * | |
1645 | * The worker thread only proceeds if it can atomically set BUSY. It | |
1646 | * clears QUEUED and does it's thing. When it thinks it's done, it | |
1647 | * clears BUSY, then rechecks QUEUED.. if it's set again, it loops | |
1648 | * (tries again to set BUSY). | |
1649 | * | |
1650 | * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we | |
1651 | * try to queue work. If that fails (work is already queued, or BUSY) | |
1652 | * we give up (work also already being done or is queued) but leave QUEUED | |
1653 | * set so that the worker thread will loop if necessary. | |
1654 | */ | |
1655 | static void queue_con(struct ceph_connection *con) | |
1656 | { | |
1657 | if (test_bit(DEAD, &con->state)) { | |
1658 | dout("queue_con %p ignoring: DEAD\n", | |
1659 | con); | |
1660 | return; | |
1661 | } | |
1662 | ||
1663 | if (!con->ops->get(con)) { | |
1664 | dout("queue_con %p ref count 0\n", con); | |
1665 | return; | |
1666 | } | |
1667 | ||
1668 | set_bit(QUEUED, &con->state); | |
1669 | if (test_bit(BUSY, &con->state)) { | |
1670 | dout("queue_con %p - already BUSY\n", con); | |
1671 | con->ops->put(con); | |
1672 | } else if (!queue_work(ceph_msgr_wq, &con->work.work)) { | |
1673 | dout("queue_con %p - already queued\n", con); | |
1674 | con->ops->put(con); | |
1675 | } else { | |
1676 | dout("queue_con %p\n", con); | |
1677 | } | |
1678 | } | |
1679 | ||
1680 | /* | |
1681 | * Do some work on a connection. Drop a connection ref when we're done. | |
1682 | */ | |
1683 | static void con_work(struct work_struct *work) | |
1684 | { | |
1685 | struct ceph_connection *con = container_of(work, struct ceph_connection, | |
1686 | work.work); | |
1687 | int backoff = 0; | |
1688 | ||
1689 | more: | |
1690 | if (test_and_set_bit(BUSY, &con->state) != 0) { | |
1691 | dout("con_work %p BUSY already set\n", con); | |
1692 | goto out; | |
1693 | } | |
1694 | dout("con_work %p start, clearing QUEUED\n", con); | |
1695 | clear_bit(QUEUED, &con->state); | |
1696 | ||
1697 | if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */ | |
1698 | dout("con_work CLOSED\n"); | |
1699 | con_close_socket(con); | |
1700 | goto done; | |
1701 | } | |
1702 | if (test_and_clear_bit(OPENING, &con->state)) { | |
1703 | /* reopen w/ new peer */ | |
1704 | dout("con_work OPENING\n"); | |
1705 | con_close_socket(con); | |
1706 | } | |
1707 | ||
1708 | if (test_and_clear_bit(SOCK_CLOSED, &con->state) || | |
1709 | try_read(con) < 0 || | |
1710 | try_write(con) < 0) { | |
1711 | backoff = 1; | |
1712 | ceph_fault(con); /* error/fault path */ | |
1713 | } | |
1714 | ||
1715 | done: | |
1716 | clear_bit(BUSY, &con->state); | |
1717 | dout("con->state=%lu\n", con->state); | |
1718 | if (test_bit(QUEUED, &con->state)) { | |
1719 | if (!backoff) { | |
1720 | dout("con_work %p QUEUED reset, looping\n", con); | |
1721 | goto more; | |
1722 | } | |
1723 | dout("con_work %p QUEUED reset, but just faulted\n", con); | |
1724 | clear_bit(QUEUED, &con->state); | |
1725 | } | |
1726 | dout("con_work %p done\n", con); | |
1727 | ||
1728 | out: | |
1729 | con->ops->put(con); | |
1730 | } | |
1731 | ||
1732 | ||
1733 | /* | |
1734 | * Generic error/fault handler. A retry mechanism is used with | |
1735 | * exponential backoff | |
1736 | */ | |
1737 | static void ceph_fault(struct ceph_connection *con) | |
1738 | { | |
1739 | pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name), | |
1740 | pr_addr(&con->peer_addr.in_addr), con->error_msg); | |
1741 | dout("fault %p state %lu to peer %s\n", | |
1742 | con, con->state, pr_addr(&con->peer_addr.in_addr)); | |
1743 | ||
1744 | if (test_bit(LOSSYTX, &con->state)) { | |
1745 | dout("fault on LOSSYTX channel\n"); | |
1746 | goto out; | |
1747 | } | |
1748 | ||
1749 | clear_bit(BUSY, &con->state); /* to avoid an improbable race */ | |
1750 | ||
1751 | con_close_socket(con); | |
1752 | con->in_msg = NULL; | |
1753 | ||
1754 | /* If there are no messages in the queue, place the connection | |
1755 | * in a STANDBY state (i.e., don't try to reconnect just yet). */ | |
1756 | mutex_lock(&con->out_mutex); | |
1757 | if (list_empty(&con->out_queue) && !con->out_keepalive_pending) { | |
1758 | dout("fault setting STANDBY\n"); | |
1759 | set_bit(STANDBY, &con->state); | |
1760 | mutex_unlock(&con->out_mutex); | |
1761 | goto out; | |
1762 | } | |
1763 | ||
1764 | /* Requeue anything that hasn't been acked, and retry after a | |
1765 | * delay. */ | |
1766 | list_splice_init(&con->out_sent, &con->out_queue); | |
1767 | mutex_unlock(&con->out_mutex); | |
1768 | ||
1769 | if (con->delay == 0) | |
1770 | con->delay = BASE_DELAY_INTERVAL; | |
1771 | else if (con->delay < MAX_DELAY_INTERVAL) | |
1772 | con->delay *= 2; | |
1773 | ||
1774 | /* explicitly schedule work to try to reconnect again later. */ | |
1775 | dout("fault queueing %p delay %lu\n", con, con->delay); | |
1776 | con->ops->get(con); | |
1777 | if (queue_delayed_work(ceph_msgr_wq, &con->work, | |
1778 | round_jiffies_relative(con->delay)) == 0) | |
1779 | con->ops->put(con); | |
1780 | ||
1781 | out: | |
1782 | if (con->ops->fault) | |
1783 | con->ops->fault(con); | |
1784 | } | |
1785 | ||
1786 | ||
1787 | ||
1788 | /* | |
1789 | * create a new messenger instance | |
1790 | */ | |
1791 | struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr) | |
1792 | { | |
1793 | struct ceph_messenger *msgr; | |
1794 | ||
1795 | msgr = kzalloc(sizeof(*msgr), GFP_KERNEL); | |
1796 | if (msgr == NULL) | |
1797 | return ERR_PTR(-ENOMEM); | |
1798 | ||
1799 | spin_lock_init(&msgr->global_seq_lock); | |
1800 | ||
1801 | /* the zero page is needed if a request is "canceled" while the message | |
1802 | * is being written over the socket */ | |
1803 | msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
1804 | if (!msgr->zero_page) { | |
1805 | kfree(msgr); | |
1806 | return ERR_PTR(-ENOMEM); | |
1807 | } | |
1808 | kmap(msgr->zero_page); | |
1809 | ||
1810 | if (myaddr) | |
1811 | msgr->inst.addr = *myaddr; | |
1812 | ||
1813 | /* select a random nonce */ | |
1814 | get_random_bytes(&msgr->inst.addr.nonce, | |
1815 | sizeof(msgr->inst.addr.nonce)); | |
63f2d211 | 1816 | encode_my_addr(msgr); |
31b8006e SW |
1817 | |
1818 | dout("messenger_create %p\n", msgr); | |
1819 | return msgr; | |
1820 | } | |
1821 | ||
1822 | void ceph_messenger_destroy(struct ceph_messenger *msgr) | |
1823 | { | |
1824 | dout("destroy %p\n", msgr); | |
1825 | kunmap(msgr->zero_page); | |
1826 | __free_page(msgr->zero_page); | |
1827 | kfree(msgr); | |
1828 | dout("destroyed messenger %p\n", msgr); | |
1829 | } | |
1830 | ||
1831 | /* | |
1832 | * Queue up an outgoing message on the given connection. | |
1833 | */ | |
1834 | void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg) | |
1835 | { | |
1836 | if (test_bit(CLOSED, &con->state)) { | |
1837 | dout("con_send %p closed, dropping %p\n", con, msg); | |
1838 | ceph_msg_put(msg); | |
1839 | return; | |
1840 | } | |
1841 | ||
1842 | /* set src+dst */ | |
63f2d211 SW |
1843 | msg->hdr.src.name = con->msgr->inst.name; |
1844 | msg->hdr.src.addr = con->msgr->my_enc_addr; | |
1845 | msg->hdr.orig_src = msg->hdr.src; | |
31b8006e SW |
1846 | msg->hdr.dst_erank = con->peer_addr.erank; |
1847 | ||
1848 | /* queue */ | |
1849 | mutex_lock(&con->out_mutex); | |
1850 | BUG_ON(!list_empty(&msg->list_head)); | |
1851 | list_add_tail(&msg->list_head, &con->out_queue); | |
1852 | dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg, | |
1853 | ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type), | |
1854 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), | |
1855 | le32_to_cpu(msg->hdr.front_len), | |
1856 | le32_to_cpu(msg->hdr.middle_len), | |
1857 | le32_to_cpu(msg->hdr.data_len)); | |
1858 | mutex_unlock(&con->out_mutex); | |
1859 | ||
1860 | /* if there wasn't anything waiting to send before, queue | |
1861 | * new work */ | |
1862 | if (test_and_set_bit(WRITE_PENDING, &con->state) == 0) | |
1863 | queue_con(con); | |
1864 | } | |
1865 | ||
1866 | /* | |
1867 | * Revoke a message that was previously queued for send | |
1868 | */ | |
1869 | void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg) | |
1870 | { | |
1871 | mutex_lock(&con->out_mutex); | |
1872 | if (!list_empty(&msg->list_head)) { | |
1873 | dout("con_revoke %p msg %p\n", con, msg); | |
1874 | list_del_init(&msg->list_head); | |
1875 | ceph_msg_put(msg); | |
1876 | msg->hdr.seq = 0; | |
1877 | if (con->out_msg == msg) | |
1878 | con->out_msg = NULL; | |
1879 | if (con->out_kvec_is_msg) { | |
1880 | con->out_skip = con->out_kvec_bytes; | |
1881 | con->out_kvec_is_msg = false; | |
1882 | } | |
1883 | } else { | |
1884 | dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg); | |
1885 | } | |
1886 | mutex_unlock(&con->out_mutex); | |
1887 | } | |
1888 | ||
1889 | /* | |
1890 | * Queue a keepalive byte to ensure the tcp connection is alive. | |
1891 | */ | |
1892 | void ceph_con_keepalive(struct ceph_connection *con) | |
1893 | { | |
1894 | if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 && | |
1895 | test_and_set_bit(WRITE_PENDING, &con->state) == 0) | |
1896 | queue_con(con); | |
1897 | } | |
1898 | ||
1899 | ||
1900 | /* | |
1901 | * construct a new message with given type, size | |
1902 | * the new msg has a ref count of 1. | |
1903 | */ | |
1904 | struct ceph_msg *ceph_msg_new(int type, int front_len, | |
1905 | int page_len, int page_off, struct page **pages) | |
1906 | { | |
1907 | struct ceph_msg *m; | |
1908 | ||
1909 | m = kmalloc(sizeof(*m), GFP_NOFS); | |
1910 | if (m == NULL) | |
1911 | goto out; | |
1912 | atomic_set(&m->nref, 1); | |
1913 | INIT_LIST_HEAD(&m->list_head); | |
1914 | ||
1915 | m->hdr.type = cpu_to_le16(type); | |
1916 | m->hdr.front_len = cpu_to_le32(front_len); | |
1917 | m->hdr.middle_len = 0; | |
1918 | m->hdr.data_len = cpu_to_le32(page_len); | |
1919 | m->hdr.data_off = cpu_to_le16(page_off); | |
1920 | m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); | |
1921 | m->footer.front_crc = 0; | |
1922 | m->footer.middle_crc = 0; | |
1923 | m->footer.data_crc = 0; | |
1924 | m->front_max = front_len; | |
1925 | m->front_is_vmalloc = false; | |
1926 | m->more_to_follow = false; | |
1927 | m->pool = NULL; | |
1928 | ||
1929 | /* front */ | |
1930 | if (front_len) { | |
1931 | if (front_len > PAGE_CACHE_SIZE) { | |
1932 | m->front.iov_base = __vmalloc(front_len, GFP_NOFS, | |
1933 | PAGE_KERNEL); | |
1934 | m->front_is_vmalloc = true; | |
1935 | } else { | |
1936 | m->front.iov_base = kmalloc(front_len, GFP_NOFS); | |
1937 | } | |
1938 | if (m->front.iov_base == NULL) { | |
1939 | pr_err("msg_new can't allocate %d bytes\n", | |
1940 | front_len); | |
1941 | goto out2; | |
1942 | } | |
1943 | } else { | |
1944 | m->front.iov_base = NULL; | |
1945 | } | |
1946 | m->front.iov_len = front_len; | |
1947 | ||
1948 | /* middle */ | |
1949 | m->middle = NULL; | |
1950 | ||
1951 | /* data */ | |
1952 | m->nr_pages = calc_pages_for(page_off, page_len); | |
1953 | m->pages = pages; | |
1954 | ||
1955 | dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len, | |
1956 | m->nr_pages); | |
1957 | return m; | |
1958 | ||
1959 | out2: | |
1960 | ceph_msg_put(m); | |
1961 | out: | |
1962 | pr_err("msg_new can't create type %d len %d\n", type, front_len); | |
1963 | return ERR_PTR(-ENOMEM); | |
1964 | } | |
1965 | ||
1966 | /* | |
1967 | * Generic message allocator, for incoming messages. | |
1968 | */ | |
1969 | struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con, | |
1970 | struct ceph_msg_header *hdr) | |
1971 | { | |
1972 | int type = le16_to_cpu(hdr->type); | |
1973 | int front_len = le32_to_cpu(hdr->front_len); | |
1974 | struct ceph_msg *msg = ceph_msg_new(type, front_len, 0, 0, NULL); | |
1975 | ||
1976 | if (!msg) { | |
1977 | pr_err("unable to allocate msg type %d len %d\n", | |
1978 | type, front_len); | |
1979 | return ERR_PTR(-ENOMEM); | |
1980 | } | |
1981 | return msg; | |
1982 | } | |
1983 | ||
1984 | /* | |
1985 | * Allocate "middle" portion of a message, if it is needed and wasn't | |
1986 | * allocated by alloc_msg. This allows us to read a small fixed-size | |
1987 | * per-type header in the front and then gracefully fail (i.e., | |
1988 | * propagate the error to the caller based on info in the front) when | |
1989 | * the middle is too large. | |
1990 | */ | |
1991 | int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg) | |
1992 | { | |
1993 | int type = le16_to_cpu(msg->hdr.type); | |
1994 | int middle_len = le32_to_cpu(msg->hdr.middle_len); | |
1995 | ||
1996 | dout("alloc_middle %p type %d %s middle_len %d\n", msg, type, | |
1997 | ceph_msg_type_name(type), middle_len); | |
1998 | BUG_ON(!middle_len); | |
1999 | BUG_ON(msg->middle); | |
2000 | ||
2001 | msg->middle = ceph_buffer_new_alloc(middle_len, GFP_NOFS); | |
2002 | if (!msg->middle) | |
2003 | return -ENOMEM; | |
2004 | return 0; | |
2005 | } | |
2006 | ||
2007 | ||
2008 | /* | |
2009 | * Free a generically kmalloc'd message. | |
2010 | */ | |
2011 | void ceph_msg_kfree(struct ceph_msg *m) | |
2012 | { | |
2013 | dout("msg_kfree %p\n", m); | |
2014 | if (m->front_is_vmalloc) | |
2015 | vfree(m->front.iov_base); | |
2016 | else | |
2017 | kfree(m->front.iov_base); | |
2018 | kfree(m); | |
2019 | } | |
2020 | ||
2021 | /* | |
2022 | * Drop a msg ref. Destroy as needed. | |
2023 | */ | |
2024 | void ceph_msg_put(struct ceph_msg *m) | |
2025 | { | |
2026 | dout("ceph_msg_put %p %d -> %d\n", m, atomic_read(&m->nref), | |
2027 | atomic_read(&m->nref)-1); | |
2028 | if (atomic_read(&m->nref) <= 0) { | |
2029 | pr_err("bad ceph_msg_put on %p %llu %d=%s %d+%d\n", | |
2030 | m, le64_to_cpu(m->hdr.seq), | |
2031 | le16_to_cpu(m->hdr.type), | |
2032 | ceph_msg_type_name(le16_to_cpu(m->hdr.type)), | |
2033 | le32_to_cpu(m->hdr.front_len), | |
2034 | le32_to_cpu(m->hdr.data_len)); | |
2035 | WARN_ON(1); | |
2036 | } | |
2037 | if (atomic_dec_and_test(&m->nref)) { | |
2038 | dout("ceph_msg_put last one on %p\n", m); | |
2039 | WARN_ON(!list_empty(&m->list_head)); | |
2040 | ||
2041 | /* drop middle, data, if any */ | |
2042 | if (m->middle) { | |
2043 | ceph_buffer_put(m->middle); | |
2044 | m->middle = NULL; | |
2045 | } | |
2046 | m->nr_pages = 0; | |
2047 | m->pages = NULL; | |
2048 | ||
2049 | if (m->pool) | |
2050 | ceph_msgpool_put(m->pool, m); | |
2051 | else | |
2052 | ceph_msg_kfree(m); | |
2053 | } | |
2054 | } |