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