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