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