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