]>
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> | |
ee3b56f2 | 14 | #include <linux/dns_resolver.h> |
31b8006e SW |
15 | #include <net/tcp.h> |
16 | ||
3d14c5d2 YS |
17 | #include <linux/ceph/libceph.h> |
18 | #include <linux/ceph/messenger.h> | |
19 | #include <linux/ceph/decode.h> | |
20 | #include <linux/ceph/pagelist.h> | |
bc3b2d7f | 21 | #include <linux/export.h> |
31b8006e SW |
22 | |
23 | /* | |
24 | * Ceph uses the messenger to exchange ceph_msg messages with other | |
25 | * hosts in the system. The messenger provides ordered and reliable | |
26 | * delivery. We tolerate TCP disconnects by reconnecting (with | |
27 | * exponential backoff) in the case of a fault (disconnection, bad | |
28 | * crc, protocol error). Acks allow sent messages to be discarded by | |
29 | * the sender. | |
30 | */ | |
31 | ||
bc18f4b1 AE |
32 | /* |
33 | * We track the state of the socket on a given connection using | |
34 | * values defined below. The transition to a new socket state is | |
35 | * handled by a function which verifies we aren't coming from an | |
36 | * unexpected state. | |
37 | * | |
38 | * -------- | |
39 | * | NEW* | transient initial state | |
40 | * -------- | |
41 | * | con_sock_state_init() | |
42 | * v | |
43 | * ---------- | |
44 | * | CLOSED | initialized, but no socket (and no | |
45 | * ---------- TCP connection) | |
46 | * ^ \ | |
47 | * | \ con_sock_state_connecting() | |
48 | * | ---------------------- | |
49 | * | \ | |
50 | * + con_sock_state_closed() \ | |
fbb85a47 SW |
51 | * |+--------------------------- \ |
52 | * | \ \ \ | |
53 | * | ----------- \ \ | |
54 | * | | CLOSING | socket event; \ \ | |
55 | * | ----------- await close \ \ | |
56 | * | ^ \ | | |
57 | * | | \ | | |
58 | * | + con_sock_state_closing() \ | | |
59 | * | / \ | | | |
60 | * | / --------------- | | | |
61 | * | / \ v v | |
bc18f4b1 AE |
62 | * | / -------------- |
63 | * | / -----------------| CONNECTING | socket created, TCP | |
64 | * | | / -------------- connect initiated | |
65 | * | | | con_sock_state_connected() | |
66 | * | | v | |
67 | * ------------- | |
68 | * | CONNECTED | TCP connection established | |
69 | * ------------- | |
70 | * | |
71 | * State values for ceph_connection->sock_state; NEW is assumed to be 0. | |
72 | */ | |
ce2c8903 AE |
73 | |
74 | #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */ | |
75 | #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */ | |
76 | #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */ | |
77 | #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */ | |
78 | #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */ | |
79 | ||
8dacc7da SW |
80 | /* |
81 | * connection states | |
82 | */ | |
83 | #define CON_STATE_CLOSED 1 /* -> PREOPEN */ | |
84 | #define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */ | |
85 | #define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */ | |
86 | #define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */ | |
87 | #define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */ | |
88 | #define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */ | |
89 | ||
90 | ||
31b8006e SW |
91 | /* static tag bytes (protocol control messages) */ |
92 | static char tag_msg = CEPH_MSGR_TAG_MSG; | |
93 | static char tag_ack = CEPH_MSGR_TAG_ACK; | |
94 | static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE; | |
95 | ||
a6a5349d SW |
96 | #ifdef CONFIG_LOCKDEP |
97 | static struct lock_class_key socket_class; | |
98 | #endif | |
99 | ||
84495f49 AE |
100 | /* |
101 | * When skipping (ignoring) a block of input we read it into a "skip | |
102 | * buffer," which is this many bytes in size. | |
103 | */ | |
104 | #define SKIP_BUF_SIZE 1024 | |
31b8006e SW |
105 | |
106 | static void queue_con(struct ceph_connection *con); | |
107 | static void con_work(struct work_struct *); | |
108 | static void ceph_fault(struct ceph_connection *con); | |
109 | ||
31b8006e | 110 | /* |
f64a9317 AE |
111 | * Nicely render a sockaddr as a string. An array of formatted |
112 | * strings is used, to approximate reentrancy. | |
31b8006e | 113 | */ |
f64a9317 AE |
114 | #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */ |
115 | #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG) | |
116 | #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1) | |
117 | #define MAX_ADDR_STR_LEN 64 /* 54 is enough */ | |
118 | ||
119 | static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN]; | |
120 | static atomic_t addr_str_seq = ATOMIC_INIT(0); | |
31b8006e | 121 | |
57666519 | 122 | static struct page *zero_page; /* used in certain error cases */ |
57666519 | 123 | |
3d14c5d2 | 124 | const char *ceph_pr_addr(const struct sockaddr_storage *ss) |
31b8006e SW |
125 | { |
126 | int i; | |
127 | char *s; | |
99f0f3b2 AE |
128 | struct sockaddr_in *in4 = (struct sockaddr_in *) ss; |
129 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss; | |
31b8006e | 130 | |
f64a9317 | 131 | i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK; |
31b8006e SW |
132 | s = addr_str[i]; |
133 | ||
134 | switch (ss->ss_family) { | |
135 | case AF_INET: | |
bd406145 AE |
136 | snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr, |
137 | ntohs(in4->sin_port)); | |
31b8006e SW |
138 | break; |
139 | ||
140 | case AF_INET6: | |
bd406145 AE |
141 | snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr, |
142 | ntohs(in6->sin6_port)); | |
31b8006e SW |
143 | break; |
144 | ||
145 | default: | |
d3002b97 AE |
146 | snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)", |
147 | ss->ss_family); | |
31b8006e SW |
148 | } |
149 | ||
150 | return s; | |
151 | } | |
3d14c5d2 | 152 | EXPORT_SYMBOL(ceph_pr_addr); |
31b8006e | 153 | |
63f2d211 SW |
154 | static void encode_my_addr(struct ceph_messenger *msgr) |
155 | { | |
156 | memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr)); | |
157 | ceph_encode_addr(&msgr->my_enc_addr); | |
158 | } | |
159 | ||
31b8006e SW |
160 | /* |
161 | * work queue for all reading and writing to/from the socket. | |
162 | */ | |
e0f43c94 | 163 | static struct workqueue_struct *ceph_msgr_wq; |
31b8006e | 164 | |
6173d1f0 AE |
165 | void _ceph_msgr_exit(void) |
166 | { | |
d3002b97 | 167 | if (ceph_msgr_wq) { |
6173d1f0 | 168 | destroy_workqueue(ceph_msgr_wq); |
d3002b97 AE |
169 | ceph_msgr_wq = NULL; |
170 | } | |
6173d1f0 | 171 | |
6173d1f0 AE |
172 | BUG_ON(zero_page == NULL); |
173 | kunmap(zero_page); | |
174 | page_cache_release(zero_page); | |
175 | zero_page = NULL; | |
176 | } | |
177 | ||
3d14c5d2 | 178 | int ceph_msgr_init(void) |
31b8006e | 179 | { |
57666519 AE |
180 | BUG_ON(zero_page != NULL); |
181 | zero_page = ZERO_PAGE(0); | |
182 | page_cache_get(zero_page); | |
183 | ||
f363e45f | 184 | ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0); |
6173d1f0 AE |
185 | if (ceph_msgr_wq) |
186 | return 0; | |
57666519 | 187 | |
6173d1f0 AE |
188 | pr_err("msgr_init failed to create workqueue\n"); |
189 | _ceph_msgr_exit(); | |
57666519 | 190 | |
6173d1f0 | 191 | return -ENOMEM; |
31b8006e | 192 | } |
3d14c5d2 | 193 | EXPORT_SYMBOL(ceph_msgr_init); |
31b8006e SW |
194 | |
195 | void ceph_msgr_exit(void) | |
196 | { | |
57666519 | 197 | BUG_ON(ceph_msgr_wq == NULL); |
57666519 | 198 | |
6173d1f0 | 199 | _ceph_msgr_exit(); |
31b8006e | 200 | } |
3d14c5d2 | 201 | EXPORT_SYMBOL(ceph_msgr_exit); |
31b8006e | 202 | |
cd84db6e | 203 | void ceph_msgr_flush(void) |
a922d38f SW |
204 | { |
205 | flush_workqueue(ceph_msgr_wq); | |
206 | } | |
3d14c5d2 | 207 | EXPORT_SYMBOL(ceph_msgr_flush); |
a922d38f | 208 | |
ce2c8903 AE |
209 | /* Connection socket state transition functions */ |
210 | ||
211 | static void con_sock_state_init(struct ceph_connection *con) | |
212 | { | |
213 | int old_state; | |
214 | ||
215 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); | |
216 | if (WARN_ON(old_state != CON_SOCK_STATE_NEW)) | |
217 | printk("%s: unexpected old state %d\n", __func__, old_state); | |
218 | } | |
219 | ||
220 | static void con_sock_state_connecting(struct ceph_connection *con) | |
221 | { | |
222 | int old_state; | |
223 | ||
224 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING); | |
225 | if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED)) | |
226 | printk("%s: unexpected old state %d\n", __func__, old_state); | |
227 | } | |
228 | ||
229 | static void con_sock_state_connected(struct ceph_connection *con) | |
230 | { | |
231 | int old_state; | |
232 | ||
233 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED); | |
234 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING)) | |
235 | printk("%s: unexpected old state %d\n", __func__, old_state); | |
236 | } | |
237 | ||
238 | static void con_sock_state_closing(struct ceph_connection *con) | |
239 | { | |
240 | int old_state; | |
241 | ||
242 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING); | |
243 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING && | |
244 | old_state != CON_SOCK_STATE_CONNECTED && | |
245 | old_state != CON_SOCK_STATE_CLOSING)) | |
246 | printk("%s: unexpected old state %d\n", __func__, old_state); | |
247 | } | |
248 | ||
249 | static void con_sock_state_closed(struct ceph_connection *con) | |
250 | { | |
251 | int old_state; | |
252 | ||
253 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); | |
254 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED && | |
fbb85a47 SW |
255 | old_state != CON_SOCK_STATE_CLOSING && |
256 | old_state != CON_SOCK_STATE_CONNECTING)) | |
ce2c8903 AE |
257 | printk("%s: unexpected old state %d\n", __func__, old_state); |
258 | } | |
a922d38f | 259 | |
31b8006e SW |
260 | /* |
261 | * socket callback functions | |
262 | */ | |
263 | ||
264 | /* data available on socket, or listen socket received a connect */ | |
327800bd | 265 | static void ceph_sock_data_ready(struct sock *sk, int count_unused) |
31b8006e | 266 | { |
bd406145 | 267 | struct ceph_connection *con = sk->sk_user_data; |
a2a32584 GH |
268 | if (atomic_read(&con->msgr->stopping)) { |
269 | return; | |
270 | } | |
bd406145 | 271 | |
31b8006e | 272 | if (sk->sk_state != TCP_CLOSE_WAIT) { |
327800bd | 273 | dout("%s on %p state = %lu, queueing work\n", __func__, |
31b8006e SW |
274 | con, con->state); |
275 | queue_con(con); | |
276 | } | |
277 | } | |
278 | ||
279 | /* socket has buffer space for writing */ | |
327800bd | 280 | static void ceph_sock_write_space(struct sock *sk) |
31b8006e | 281 | { |
d3002b97 | 282 | struct ceph_connection *con = sk->sk_user_data; |
31b8006e | 283 | |
182fac26 JS |
284 | /* only queue to workqueue if there is data we want to write, |
285 | * and there is sufficient space in the socket buffer to accept | |
327800bd | 286 | * more data. clear SOCK_NOSPACE so that ceph_sock_write_space() |
182fac26 JS |
287 | * doesn't get called again until try_write() fills the socket |
288 | * buffer. See net/ipv4/tcp_input.c:tcp_check_space() | |
289 | * and net/core/stream.c:sk_stream_write_space(). | |
290 | */ | |
928443cd | 291 | if (test_bit(WRITE_PENDING, &con->flags)) { |
182fac26 | 292 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { |
327800bd | 293 | dout("%s %p queueing write work\n", __func__, con); |
182fac26 JS |
294 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
295 | queue_con(con); | |
296 | } | |
31b8006e | 297 | } else { |
327800bd | 298 | dout("%s %p nothing to write\n", __func__, con); |
31b8006e | 299 | } |
31b8006e SW |
300 | } |
301 | ||
302 | /* socket's state has changed */ | |
327800bd | 303 | static void ceph_sock_state_change(struct sock *sk) |
31b8006e | 304 | { |
bd406145 | 305 | struct ceph_connection *con = sk->sk_user_data; |
31b8006e | 306 | |
327800bd | 307 | dout("%s %p state = %lu sk_state = %u\n", __func__, |
31b8006e SW |
308 | con, con->state, sk->sk_state); |
309 | ||
31b8006e SW |
310 | switch (sk->sk_state) { |
311 | case TCP_CLOSE: | |
327800bd | 312 | dout("%s TCP_CLOSE\n", __func__); |
31b8006e | 313 | case TCP_CLOSE_WAIT: |
327800bd | 314 | dout("%s TCP_CLOSE_WAIT\n", __func__); |
ce2c8903 | 315 | con_sock_state_closing(con); |
d65c9e0b AE |
316 | set_bit(SOCK_CLOSED, &con->flags); |
317 | queue_con(con); | |
31b8006e SW |
318 | break; |
319 | case TCP_ESTABLISHED: | |
327800bd | 320 | dout("%s TCP_ESTABLISHED\n", __func__); |
ce2c8903 | 321 | con_sock_state_connected(con); |
31b8006e SW |
322 | queue_con(con); |
323 | break; | |
d3002b97 AE |
324 | default: /* Everything else is uninteresting */ |
325 | break; | |
31b8006e SW |
326 | } |
327 | } | |
328 | ||
329 | /* | |
330 | * set up socket callbacks | |
331 | */ | |
332 | static void set_sock_callbacks(struct socket *sock, | |
333 | struct ceph_connection *con) | |
334 | { | |
335 | struct sock *sk = sock->sk; | |
bd406145 | 336 | sk->sk_user_data = con; |
327800bd AE |
337 | sk->sk_data_ready = ceph_sock_data_ready; |
338 | sk->sk_write_space = ceph_sock_write_space; | |
339 | sk->sk_state_change = ceph_sock_state_change; | |
31b8006e SW |
340 | } |
341 | ||
342 | ||
343 | /* | |
344 | * socket helpers | |
345 | */ | |
346 | ||
347 | /* | |
348 | * initiate connection to a remote socket. | |
349 | */ | |
41617d0c | 350 | static int ceph_tcp_connect(struct ceph_connection *con) |
31b8006e | 351 | { |
f91d3471 | 352 | struct sockaddr_storage *paddr = &con->peer_addr.in_addr; |
31b8006e SW |
353 | struct socket *sock; |
354 | int ret; | |
355 | ||
356 | BUG_ON(con->sock); | |
f91d3471 SW |
357 | ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM, |
358 | IPPROTO_TCP, &sock); | |
31b8006e | 359 | if (ret) |
41617d0c | 360 | return ret; |
31b8006e SW |
361 | sock->sk->sk_allocation = GFP_NOFS; |
362 | ||
a6a5349d SW |
363 | #ifdef CONFIG_LOCKDEP |
364 | lockdep_set_class(&sock->sk->sk_lock, &socket_class); | |
365 | #endif | |
366 | ||
31b8006e SW |
367 | set_sock_callbacks(sock, con); |
368 | ||
3d14c5d2 | 369 | dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr)); |
31b8006e | 370 | |
89a86be0 | 371 | con_sock_state_connecting(con); |
f91d3471 SW |
372 | ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr), |
373 | O_NONBLOCK); | |
31b8006e SW |
374 | if (ret == -EINPROGRESS) { |
375 | dout("connect %s EINPROGRESS sk_state = %u\n", | |
3d14c5d2 | 376 | ceph_pr_addr(&con->peer_addr.in_addr), |
31b8006e | 377 | sock->sk->sk_state); |
a5bc3129 | 378 | } else if (ret < 0) { |
31b8006e | 379 | pr_err("connect %s error %d\n", |
3d14c5d2 | 380 | ceph_pr_addr(&con->peer_addr.in_addr), ret); |
31b8006e | 381 | sock_release(sock); |
31b8006e | 382 | con->error_msg = "connect error"; |
31b8006e | 383 | |
41617d0c | 384 | return ret; |
a5bc3129 AE |
385 | } |
386 | con->sock = sock; | |
41617d0c | 387 | return 0; |
31b8006e SW |
388 | } |
389 | ||
390 | static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len) | |
391 | { | |
392 | struct kvec iov = {buf, len}; | |
393 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; | |
98bdb0aa | 394 | int r; |
31b8006e | 395 | |
98bdb0aa SW |
396 | r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags); |
397 | if (r == -EAGAIN) | |
398 | r = 0; | |
399 | return r; | |
31b8006e SW |
400 | } |
401 | ||
402 | /* | |
403 | * write something. @more is true if caller will be sending more data | |
404 | * shortly. | |
405 | */ | |
406 | static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov, | |
407 | size_t kvlen, size_t len, int more) | |
408 | { | |
409 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; | |
42961d23 | 410 | int r; |
31b8006e SW |
411 | |
412 | if (more) | |
413 | msg.msg_flags |= MSG_MORE; | |
414 | else | |
415 | msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */ | |
416 | ||
42961d23 SW |
417 | r = kernel_sendmsg(sock, &msg, iov, kvlen, len); |
418 | if (r == -EAGAIN) | |
419 | r = 0; | |
420 | return r; | |
31b8006e SW |
421 | } |
422 | ||
31739139 AE |
423 | static int ceph_tcp_sendpage(struct socket *sock, struct page *page, |
424 | int offset, size_t size, int more) | |
425 | { | |
426 | int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR); | |
427 | int ret; | |
428 | ||
429 | ret = kernel_sendpage(sock, page, offset, size, flags); | |
430 | if (ret == -EAGAIN) | |
431 | ret = 0; | |
432 | ||
433 | return ret; | |
434 | } | |
435 | ||
31b8006e SW |
436 | |
437 | /* | |
438 | * Shutdown/close the socket for the given connection. | |
439 | */ | |
440 | static int con_close_socket(struct ceph_connection *con) | |
441 | { | |
442 | int rc; | |
443 | ||
444 | dout("con_close_socket on %p sock %p\n", con, con->sock); | |
445 | if (!con->sock) | |
446 | return 0; | |
31b8006e SW |
447 | rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR); |
448 | sock_release(con->sock); | |
449 | con->sock = NULL; | |
456ea468 AE |
450 | |
451 | /* | |
452 | * Forcibly clear the SOCK_CLOSE flag. It gets set | |
453 | * independent of the connection mutex, and we could have | |
454 | * received a socket close event before we had the chance to | |
455 | * shut the socket down. | |
456 | */ | |
a8d00e3c | 457 | clear_bit(SOCK_CLOSED, &con->flags); |
ce2c8903 | 458 | con_sock_state_closed(con); |
31b8006e SW |
459 | return rc; |
460 | } | |
461 | ||
462 | /* | |
463 | * Reset a connection. Discard all incoming and outgoing messages | |
464 | * and clear *_seq state. | |
465 | */ | |
466 | static void ceph_msg_remove(struct ceph_msg *msg) | |
467 | { | |
468 | list_del_init(&msg->list_head); | |
38941f80 | 469 | BUG_ON(msg->con == NULL); |
36eb71aa | 470 | msg->con->ops->put(msg->con); |
38941f80 AE |
471 | msg->con = NULL; |
472 | ||
31b8006e SW |
473 | ceph_msg_put(msg); |
474 | } | |
475 | static void ceph_msg_remove_list(struct list_head *head) | |
476 | { | |
477 | while (!list_empty(head)) { | |
478 | struct ceph_msg *msg = list_first_entry(head, struct ceph_msg, | |
479 | list_head); | |
480 | ceph_msg_remove(msg); | |
481 | } | |
482 | } | |
483 | ||
484 | static void reset_connection(struct ceph_connection *con) | |
485 | { | |
486 | /* reset connection, out_queue, msg_ and connect_seq */ | |
487 | /* discard existing out_queue and msg_seq */ | |
31b8006e SW |
488 | ceph_msg_remove_list(&con->out_queue); |
489 | ceph_msg_remove_list(&con->out_sent); | |
490 | ||
cf3e5c40 | 491 | if (con->in_msg) { |
38941f80 AE |
492 | BUG_ON(con->in_msg->con != con); |
493 | con->in_msg->con = NULL; | |
cf3e5c40 SW |
494 | ceph_msg_put(con->in_msg); |
495 | con->in_msg = NULL; | |
36eb71aa | 496 | con->ops->put(con); |
cf3e5c40 SW |
497 | } |
498 | ||
31b8006e SW |
499 | con->connect_seq = 0; |
500 | con->out_seq = 0; | |
c86a2930 SW |
501 | if (con->out_msg) { |
502 | ceph_msg_put(con->out_msg); | |
503 | con->out_msg = NULL; | |
504 | } | |
31b8006e | 505 | con->in_seq = 0; |
0e0d5e0c | 506 | con->in_seq_acked = 0; |
31b8006e SW |
507 | } |
508 | ||
509 | /* | |
510 | * mark a peer down. drop any open connections. | |
511 | */ | |
512 | void ceph_con_close(struct ceph_connection *con) | |
513 | { | |
8c50c817 | 514 | mutex_lock(&con->mutex); |
3d14c5d2 YS |
515 | dout("con_close %p peer %s\n", con, |
516 | ceph_pr_addr(&con->peer_addr.in_addr)); | |
8dacc7da | 517 | con->state = CON_STATE_CLOSED; |
a5988c49 | 518 | |
928443cd AE |
519 | clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */ |
520 | clear_bit(KEEPALIVE_PENDING, &con->flags); | |
521 | clear_bit(WRITE_PENDING, &con->flags); | |
a5988c49 | 522 | |
31b8006e | 523 | reset_connection(con); |
6f2bc3ff | 524 | con->peer_global_seq = 0; |
91e45ce3 | 525 | cancel_delayed_work(&con->work); |
ee76e073 | 526 | con_close_socket(con); |
ec302645 | 527 | mutex_unlock(&con->mutex); |
31b8006e | 528 | } |
3d14c5d2 | 529 | EXPORT_SYMBOL(ceph_con_close); |
31b8006e | 530 | |
31b8006e SW |
531 | /* |
532 | * Reopen a closed connection, with a new peer address. | |
533 | */ | |
b7a9e5dd SW |
534 | void ceph_con_open(struct ceph_connection *con, |
535 | __u8 entity_type, __u64 entity_num, | |
536 | struct ceph_entity_addr *addr) | |
31b8006e | 537 | { |
5469155f | 538 | mutex_lock(&con->mutex); |
3d14c5d2 | 539 | dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr)); |
8dacc7da SW |
540 | |
541 | BUG_ON(con->state != CON_STATE_CLOSED); | |
542 | con->state = CON_STATE_PREOPEN; | |
a5988c49 | 543 | |
b7a9e5dd SW |
544 | con->peer_name.type = (__u8) entity_type; |
545 | con->peer_name.num = cpu_to_le64(entity_num); | |
546 | ||
31b8006e | 547 | memcpy(&con->peer_addr, addr, sizeof(*addr)); |
03c677e1 | 548 | con->delay = 0; /* reset backoff memory */ |
5469155f | 549 | mutex_unlock(&con->mutex); |
31b8006e SW |
550 | queue_con(con); |
551 | } | |
3d14c5d2 | 552 | EXPORT_SYMBOL(ceph_con_open); |
31b8006e | 553 | |
87b315a5 SW |
554 | /* |
555 | * return true if this connection ever successfully opened | |
556 | */ | |
557 | bool ceph_con_opened(struct ceph_connection *con) | |
558 | { | |
559 | return con->connect_seq > 0; | |
560 | } | |
561 | ||
31b8006e SW |
562 | /* |
563 | * initialize a new connection. | |
564 | */ | |
1bfd89f4 AE |
565 | void ceph_con_init(struct ceph_connection *con, void *private, |
566 | const struct ceph_connection_operations *ops, | |
b7a9e5dd | 567 | struct ceph_messenger *msgr) |
31b8006e SW |
568 | { |
569 | dout("con_init %p\n", con); | |
570 | memset(con, 0, sizeof(*con)); | |
1bfd89f4 AE |
571 | con->private = private; |
572 | con->ops = ops; | |
31b8006e | 573 | con->msgr = msgr; |
ce2c8903 AE |
574 | |
575 | con_sock_state_init(con); | |
576 | ||
ec302645 | 577 | mutex_init(&con->mutex); |
31b8006e SW |
578 | INIT_LIST_HEAD(&con->out_queue); |
579 | INIT_LIST_HEAD(&con->out_sent); | |
580 | INIT_DELAYED_WORK(&con->work, con_work); | |
a5988c49 | 581 | |
8dacc7da | 582 | con->state = CON_STATE_CLOSED; |
31b8006e | 583 | } |
3d14c5d2 | 584 | EXPORT_SYMBOL(ceph_con_init); |
31b8006e SW |
585 | |
586 | ||
587 | /* | |
588 | * We maintain a global counter to order connection attempts. Get | |
589 | * a unique seq greater than @gt. | |
590 | */ | |
591 | static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt) | |
592 | { | |
593 | u32 ret; | |
594 | ||
595 | spin_lock(&msgr->global_seq_lock); | |
596 | if (msgr->global_seq < gt) | |
597 | msgr->global_seq = gt; | |
598 | ret = ++msgr->global_seq; | |
599 | spin_unlock(&msgr->global_seq_lock); | |
600 | return ret; | |
601 | } | |
602 | ||
e2200423 | 603 | static void con_out_kvec_reset(struct ceph_connection *con) |
859eb799 AE |
604 | { |
605 | con->out_kvec_left = 0; | |
606 | con->out_kvec_bytes = 0; | |
607 | con->out_kvec_cur = &con->out_kvec[0]; | |
608 | } | |
609 | ||
e2200423 | 610 | static void con_out_kvec_add(struct ceph_connection *con, |
859eb799 AE |
611 | size_t size, void *data) |
612 | { | |
613 | int index; | |
614 | ||
615 | index = con->out_kvec_left; | |
616 | BUG_ON(index >= ARRAY_SIZE(con->out_kvec)); | |
617 | ||
618 | con->out_kvec[index].iov_len = size; | |
619 | con->out_kvec[index].iov_base = data; | |
620 | con->out_kvec_left++; | |
621 | con->out_kvec_bytes += size; | |
622 | } | |
31b8006e | 623 | |
df6ad1f9 AE |
624 | #ifdef CONFIG_BLOCK |
625 | static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg) | |
626 | { | |
627 | if (!bio) { | |
628 | *iter = NULL; | |
629 | *seg = 0; | |
630 | return; | |
631 | } | |
632 | *iter = bio; | |
633 | *seg = bio->bi_idx; | |
634 | } | |
635 | ||
636 | static void iter_bio_next(struct bio **bio_iter, int *seg) | |
637 | { | |
638 | if (*bio_iter == NULL) | |
639 | return; | |
640 | ||
641 | BUG_ON(*seg >= (*bio_iter)->bi_vcnt); | |
642 | ||
643 | (*seg)++; | |
644 | if (*seg == (*bio_iter)->bi_vcnt) | |
645 | init_bio_iter((*bio_iter)->bi_next, bio_iter, seg); | |
646 | } | |
647 | #endif | |
648 | ||
739c905b AE |
649 | static void prepare_write_message_data(struct ceph_connection *con) |
650 | { | |
651 | struct ceph_msg *msg = con->out_msg; | |
652 | ||
653 | BUG_ON(!msg); | |
654 | BUG_ON(!msg->hdr.data_len); | |
655 | ||
656 | /* initialize page iterator */ | |
657 | con->out_msg_pos.page = 0; | |
658 | if (msg->pages) | |
659 | con->out_msg_pos.page_pos = msg->page_alignment; | |
660 | else | |
661 | con->out_msg_pos.page_pos = 0; | |
572c588e | 662 | #ifdef CONFIG_BLOCK |
abdaa6a8 | 663 | if (msg->bio) |
572c588e AE |
664 | init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg); |
665 | #endif | |
739c905b AE |
666 | con->out_msg_pos.data_pos = 0; |
667 | con->out_msg_pos.did_page_crc = false; | |
668 | con->out_more = 1; /* data + footer will follow */ | |
669 | } | |
670 | ||
31b8006e SW |
671 | /* |
672 | * Prepare footer for currently outgoing message, and finish things | |
673 | * off. Assumes out_kvec* are already valid.. we just add on to the end. | |
674 | */ | |
859eb799 | 675 | static void prepare_write_message_footer(struct ceph_connection *con) |
31b8006e SW |
676 | { |
677 | struct ceph_msg *m = con->out_msg; | |
859eb799 | 678 | int v = con->out_kvec_left; |
31b8006e | 679 | |
fd154f3c AE |
680 | m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE; |
681 | ||
31b8006e SW |
682 | dout("prepare_write_message_footer %p\n", con); |
683 | con->out_kvec_is_msg = true; | |
684 | con->out_kvec[v].iov_base = &m->footer; | |
685 | con->out_kvec[v].iov_len = sizeof(m->footer); | |
686 | con->out_kvec_bytes += sizeof(m->footer); | |
687 | con->out_kvec_left++; | |
688 | con->out_more = m->more_to_follow; | |
c86a2930 | 689 | con->out_msg_done = true; |
31b8006e SW |
690 | } |
691 | ||
692 | /* | |
693 | * Prepare headers for the next outgoing message. | |
694 | */ | |
695 | static void prepare_write_message(struct ceph_connection *con) | |
696 | { | |
697 | struct ceph_msg *m; | |
a9a0c51a | 698 | u32 crc; |
31b8006e | 699 | |
e2200423 | 700 | con_out_kvec_reset(con); |
31b8006e | 701 | con->out_kvec_is_msg = true; |
c86a2930 | 702 | con->out_msg_done = false; |
31b8006e SW |
703 | |
704 | /* Sneak an ack in there first? If we can get it into the same | |
705 | * TCP packet that's a good thing. */ | |
706 | if (con->in_seq > con->in_seq_acked) { | |
707 | con->in_seq_acked = con->in_seq; | |
e2200423 | 708 | con_out_kvec_add(con, sizeof (tag_ack), &tag_ack); |
31b8006e | 709 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
e2200423 | 710 | con_out_kvec_add(con, sizeof (con->out_temp_ack), |
859eb799 | 711 | &con->out_temp_ack); |
31b8006e SW |
712 | } |
713 | ||
38941f80 | 714 | BUG_ON(list_empty(&con->out_queue)); |
859eb799 | 715 | m = list_first_entry(&con->out_queue, struct ceph_msg, list_head); |
c86a2930 | 716 | con->out_msg = m; |
38941f80 | 717 | BUG_ON(m->con != con); |
4cf9d544 SW |
718 | |
719 | /* put message on sent list */ | |
720 | ceph_msg_get(m); | |
721 | list_move_tail(&m->list_head, &con->out_sent); | |
31b8006e | 722 | |
e84346b7 SW |
723 | /* |
724 | * only assign outgoing seq # if we haven't sent this message | |
725 | * yet. if it is requeued, resend with it's original seq. | |
726 | */ | |
727 | if (m->needs_out_seq) { | |
728 | m->hdr.seq = cpu_to_le64(++con->out_seq); | |
729 | m->needs_out_seq = false; | |
730 | } | |
31b8006e SW |
731 | |
732 | dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n", | |
733 | m, con->out_seq, le16_to_cpu(m->hdr.type), | |
734 | le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len), | |
735 | le32_to_cpu(m->hdr.data_len), | |
736 | m->nr_pages); | |
737 | BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len); | |
738 | ||
739 | /* tag + hdr + front + middle */ | |
e2200423 AE |
740 | con_out_kvec_add(con, sizeof (tag_msg), &tag_msg); |
741 | con_out_kvec_add(con, sizeof (m->hdr), &m->hdr); | |
742 | con_out_kvec_add(con, m->front.iov_len, m->front.iov_base); | |
859eb799 | 743 | |
31b8006e | 744 | if (m->middle) |
e2200423 | 745 | con_out_kvec_add(con, m->middle->vec.iov_len, |
859eb799 | 746 | m->middle->vec.iov_base); |
31b8006e SW |
747 | |
748 | /* fill in crc (except data pages), footer */ | |
a9a0c51a AE |
749 | crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc)); |
750 | con->out_msg->hdr.crc = cpu_to_le32(crc); | |
fd154f3c | 751 | con->out_msg->footer.flags = 0; |
a9a0c51a AE |
752 | |
753 | crc = crc32c(0, m->front.iov_base, m->front.iov_len); | |
754 | con->out_msg->footer.front_crc = cpu_to_le32(crc); | |
755 | if (m->middle) { | |
756 | crc = crc32c(0, m->middle->vec.iov_base, | |
757 | m->middle->vec.iov_len); | |
758 | con->out_msg->footer.middle_crc = cpu_to_le32(crc); | |
759 | } else | |
31b8006e | 760 | con->out_msg->footer.middle_crc = 0; |
739c905b | 761 | dout("%s front_crc %u middle_crc %u\n", __func__, |
31b8006e SW |
762 | le32_to_cpu(con->out_msg->footer.front_crc), |
763 | le32_to_cpu(con->out_msg->footer.middle_crc)); | |
764 | ||
765 | /* is there a data payload? */ | |
739c905b AE |
766 | con->out_msg->footer.data_crc = 0; |
767 | if (m->hdr.data_len) | |
768 | prepare_write_message_data(con); | |
769 | else | |
31b8006e | 770 | /* no, queue up footer too and be done */ |
859eb799 | 771 | prepare_write_message_footer(con); |
31b8006e | 772 | |
928443cd | 773 | set_bit(WRITE_PENDING, &con->flags); |
31b8006e SW |
774 | } |
775 | ||
776 | /* | |
777 | * Prepare an ack. | |
778 | */ | |
779 | static void prepare_write_ack(struct ceph_connection *con) | |
780 | { | |
781 | dout("prepare_write_ack %p %llu -> %llu\n", con, | |
782 | con->in_seq_acked, con->in_seq); | |
783 | con->in_seq_acked = con->in_seq; | |
784 | ||
e2200423 | 785 | con_out_kvec_reset(con); |
859eb799 | 786 | |
e2200423 | 787 | con_out_kvec_add(con, sizeof (tag_ack), &tag_ack); |
859eb799 | 788 | |
31b8006e | 789 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
e2200423 | 790 | con_out_kvec_add(con, sizeof (con->out_temp_ack), |
859eb799 AE |
791 | &con->out_temp_ack); |
792 | ||
31b8006e | 793 | con->out_more = 1; /* more will follow.. eventually.. */ |
928443cd | 794 | set_bit(WRITE_PENDING, &con->flags); |
31b8006e SW |
795 | } |
796 | ||
797 | /* | |
798 | * Prepare to write keepalive byte. | |
799 | */ | |
800 | static void prepare_write_keepalive(struct ceph_connection *con) | |
801 | { | |
802 | dout("prepare_write_keepalive %p\n", con); | |
e2200423 AE |
803 | con_out_kvec_reset(con); |
804 | con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive); | |
928443cd | 805 | set_bit(WRITE_PENDING, &con->flags); |
31b8006e SW |
806 | } |
807 | ||
808 | /* | |
809 | * Connection negotiation. | |
810 | */ | |
811 | ||
dac1e716 AE |
812 | static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con, |
813 | int *auth_proto) | |
4e7a5dcd | 814 | { |
a3530df3 | 815 | struct ceph_auth_handshake *auth; |
b1c6b980 AE |
816 | |
817 | if (!con->ops->get_authorizer) { | |
818 | con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN; | |
819 | con->out_connect.authorizer_len = 0; | |
729796be | 820 | return NULL; |
b1c6b980 AE |
821 | } |
822 | ||
823 | /* Can't hold the mutex while getting authorizer */ | |
ec302645 | 824 | mutex_unlock(&con->mutex); |
dac1e716 | 825 | auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry); |
ec302645 | 826 | mutex_lock(&con->mutex); |
4e7a5dcd | 827 | |
a3530df3 | 828 | if (IS_ERR(auth)) |
729796be | 829 | return auth; |
8dacc7da | 830 | if (con->state != CON_STATE_NEGOTIATING) |
729796be | 831 | return ERR_PTR(-EAGAIN); |
0da5d703 | 832 | |
8f43fb53 AE |
833 | con->auth_reply_buf = auth->authorizer_reply_buf; |
834 | con->auth_reply_buf_len = auth->authorizer_reply_buf_len; | |
729796be | 835 | return auth; |
4e7a5dcd SW |
836 | } |
837 | ||
31b8006e SW |
838 | /* |
839 | * We connected to a peer and are saying hello. | |
840 | */ | |
e825a66d | 841 | static void prepare_write_banner(struct ceph_connection *con) |
31b8006e | 842 | { |
e2200423 AE |
843 | con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER); |
844 | con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr), | |
e825a66d | 845 | &con->msgr->my_enc_addr); |
eed0ef2c | 846 | |
eed0ef2c | 847 | con->out_more = 0; |
928443cd | 848 | set_bit(WRITE_PENDING, &con->flags); |
eed0ef2c SW |
849 | } |
850 | ||
e825a66d | 851 | static int prepare_write_connect(struct ceph_connection *con) |
eed0ef2c | 852 | { |
95c96174 | 853 | unsigned int global_seq = get_global_seq(con->msgr, 0); |
31b8006e | 854 | int proto; |
dac1e716 | 855 | int auth_proto; |
729796be | 856 | struct ceph_auth_handshake *auth; |
31b8006e SW |
857 | |
858 | switch (con->peer_name.type) { | |
859 | case CEPH_ENTITY_TYPE_MON: | |
860 | proto = CEPH_MONC_PROTOCOL; | |
861 | break; | |
862 | case CEPH_ENTITY_TYPE_OSD: | |
863 | proto = CEPH_OSDC_PROTOCOL; | |
864 | break; | |
865 | case CEPH_ENTITY_TYPE_MDS: | |
866 | proto = CEPH_MDSC_PROTOCOL; | |
867 | break; | |
868 | default: | |
869 | BUG(); | |
870 | } | |
871 | ||
872 | dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con, | |
873 | con->connect_seq, global_seq, proto); | |
4e7a5dcd | 874 | |
e825a66d | 875 | con->out_connect.features = cpu_to_le64(con->msgr->supported_features); |
31b8006e SW |
876 | con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT); |
877 | con->out_connect.connect_seq = cpu_to_le32(con->connect_seq); | |
878 | con->out_connect.global_seq = cpu_to_le32(global_seq); | |
879 | con->out_connect.protocol_version = cpu_to_le32(proto); | |
880 | con->out_connect.flags = 0; | |
31b8006e | 881 | |
dac1e716 AE |
882 | auth_proto = CEPH_AUTH_UNKNOWN; |
883 | auth = get_connect_authorizer(con, &auth_proto); | |
729796be AE |
884 | if (IS_ERR(auth)) |
885 | return PTR_ERR(auth); | |
3da54776 | 886 | |
dac1e716 | 887 | con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto); |
3da54776 AE |
888 | con->out_connect.authorizer_len = auth ? |
889 | cpu_to_le32(auth->authorizer_buf_len) : 0; | |
890 | ||
ab166d5a | 891 | con_out_kvec_reset(con); |
e2200423 | 892 | con_out_kvec_add(con, sizeof (con->out_connect), |
3da54776 AE |
893 | &con->out_connect); |
894 | if (auth && auth->authorizer_buf_len) | |
e2200423 | 895 | con_out_kvec_add(con, auth->authorizer_buf_len, |
3da54776 | 896 | auth->authorizer_buf); |
859eb799 | 897 | |
31b8006e | 898 | con->out_more = 0; |
928443cd | 899 | set_bit(WRITE_PENDING, &con->flags); |
4e7a5dcd | 900 | |
e10c758e | 901 | return 0; |
31b8006e SW |
902 | } |
903 | ||
31b8006e SW |
904 | /* |
905 | * write as much of pending kvecs to the socket as we can. | |
906 | * 1 -> done | |
907 | * 0 -> socket full, but more to do | |
908 | * <0 -> error | |
909 | */ | |
910 | static int write_partial_kvec(struct ceph_connection *con) | |
911 | { | |
912 | int ret; | |
913 | ||
914 | dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes); | |
915 | while (con->out_kvec_bytes > 0) { | |
916 | ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur, | |
917 | con->out_kvec_left, con->out_kvec_bytes, | |
918 | con->out_more); | |
919 | if (ret <= 0) | |
920 | goto out; | |
921 | con->out_kvec_bytes -= ret; | |
922 | if (con->out_kvec_bytes == 0) | |
923 | break; /* done */ | |
f42299e6 AE |
924 | |
925 | /* account for full iov entries consumed */ | |
926 | while (ret >= con->out_kvec_cur->iov_len) { | |
927 | BUG_ON(!con->out_kvec_left); | |
928 | ret -= con->out_kvec_cur->iov_len; | |
929 | con->out_kvec_cur++; | |
930 | con->out_kvec_left--; | |
931 | } | |
932 | /* and for a partially-consumed entry */ | |
933 | if (ret) { | |
934 | con->out_kvec_cur->iov_len -= ret; | |
935 | con->out_kvec_cur->iov_base += ret; | |
31b8006e SW |
936 | } |
937 | } | |
938 | con->out_kvec_left = 0; | |
939 | con->out_kvec_is_msg = false; | |
940 | ret = 1; | |
941 | out: | |
942 | dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con, | |
943 | con->out_kvec_bytes, con->out_kvec_left, ret); | |
944 | return ret; /* done! */ | |
945 | } | |
946 | ||
84ca8fc8 AE |
947 | static void out_msg_pos_next(struct ceph_connection *con, struct page *page, |
948 | size_t len, size_t sent, bool in_trail) | |
949 | { | |
950 | struct ceph_msg *msg = con->out_msg; | |
951 | ||
952 | BUG_ON(!msg); | |
953 | BUG_ON(!sent); | |
954 | ||
955 | con->out_msg_pos.data_pos += sent; | |
956 | con->out_msg_pos.page_pos += sent; | |
5821bd8c AE |
957 | if (sent < len) |
958 | return; | |
959 | ||
960 | BUG_ON(sent != len); | |
961 | con->out_msg_pos.page_pos = 0; | |
962 | con->out_msg_pos.page++; | |
963 | con->out_msg_pos.did_page_crc = false; | |
964 | if (in_trail) | |
965 | list_move_tail(&page->lru, | |
966 | &msg->trail->head); | |
967 | else if (msg->pagelist) | |
968 | list_move_tail(&page->lru, | |
969 | &msg->pagelist->head); | |
84ca8fc8 | 970 | #ifdef CONFIG_BLOCK |
5821bd8c AE |
971 | else if (msg->bio) |
972 | iter_bio_next(&msg->bio_iter, &msg->bio_seg); | |
84ca8fc8 | 973 | #endif |
84ca8fc8 AE |
974 | } |
975 | ||
31b8006e SW |
976 | /* |
977 | * Write as much message data payload as we can. If we finish, queue | |
978 | * up the footer. | |
979 | * 1 -> done, footer is now queued in out_kvec[]. | |
980 | * 0 -> socket full, but more to do | |
981 | * <0 -> error | |
982 | */ | |
983 | static int write_partial_msg_pages(struct ceph_connection *con) | |
984 | { | |
985 | struct ceph_msg *msg = con->out_msg; | |
95c96174 | 986 | unsigned int data_len = le32_to_cpu(msg->hdr.data_len); |
31b8006e | 987 | size_t len; |
37675b0f | 988 | bool do_datacrc = !con->msgr->nocrc; |
31b8006e | 989 | int ret; |
68b4476b | 990 | int total_max_write; |
84ca8fc8 | 991 | bool in_trail = false; |
5821bd8c AE |
992 | const size_t trail_len = (msg->trail ? msg->trail->length : 0); |
993 | const size_t trail_off = data_len - trail_len; | |
31b8006e SW |
994 | |
995 | dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n", | |
84ca8fc8 | 996 | con, msg, con->out_msg_pos.page, msg->nr_pages, |
31b8006e SW |
997 | con->out_msg_pos.page_pos); |
998 | ||
5821bd8c AE |
999 | /* |
1000 | * Iterate through each page that contains data to be | |
1001 | * written, and send as much as possible for each. | |
1002 | * | |
1003 | * If we are calculating the data crc (the default), we will | |
1004 | * need to map the page. If we have no pages, they have | |
1005 | * been revoked, so use the zero page. | |
1006 | */ | |
68b4476b | 1007 | while (data_len > con->out_msg_pos.data_pos) { |
31b8006e | 1008 | struct page *page = NULL; |
68b4476b | 1009 | int max_write = PAGE_SIZE; |
9bd19663 | 1010 | int bio_offset = 0; |
68b4476b | 1011 | |
5821bd8c AE |
1012 | in_trail = in_trail || con->out_msg_pos.data_pos >= trail_off; |
1013 | if (!in_trail) | |
1014 | total_max_write = trail_off - con->out_msg_pos.data_pos; | |
68b4476b | 1015 | |
5821bd8c | 1016 | if (in_trail) { |
68b4476b YS |
1017 | total_max_write = data_len - con->out_msg_pos.data_pos; |
1018 | ||
1019 | page = list_first_entry(&msg->trail->head, | |
1020 | struct page, lru); | |
68b4476b | 1021 | } else if (msg->pages) { |
31b8006e | 1022 | page = msg->pages[con->out_msg_pos.page]; |
58bb3b37 SW |
1023 | } else if (msg->pagelist) { |
1024 | page = list_first_entry(&msg->pagelist->head, | |
1025 | struct page, lru); | |
68b4476b YS |
1026 | #ifdef CONFIG_BLOCK |
1027 | } else if (msg->bio) { | |
1028 | struct bio_vec *bv; | |
1029 | ||
1030 | bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg); | |
1031 | page = bv->bv_page; | |
9bd19663 | 1032 | bio_offset = bv->bv_offset; |
68b4476b YS |
1033 | max_write = bv->bv_len; |
1034 | #endif | |
31b8006e | 1035 | } else { |
57666519 | 1036 | page = zero_page; |
31b8006e | 1037 | } |
68b4476b YS |
1038 | len = min_t(int, max_write - con->out_msg_pos.page_pos, |
1039 | total_max_write); | |
1040 | ||
37675b0f | 1041 | if (do_datacrc && !con->out_msg_pos.did_page_crc) { |
9bd19663 | 1042 | void *base; |
5821bd8c | 1043 | u32 crc = le32_to_cpu(msg->footer.data_crc); |
8d63e318 | 1044 | char *kaddr; |
31b8006e | 1045 | |
8d63e318 | 1046 | kaddr = kmap(page); |
31b8006e | 1047 | BUG_ON(kaddr == NULL); |
9bd19663 | 1048 | base = kaddr + con->out_msg_pos.page_pos + bio_offset; |
5821bd8c | 1049 | crc = crc32c(crc, base, len); |
84ca8fc8 | 1050 | msg->footer.data_crc = cpu_to_le32(crc); |
bca064d2 | 1051 | con->out_msg_pos.did_page_crc = true; |
31b8006e | 1052 | } |
e36b13cc | 1053 | ret = ceph_tcp_sendpage(con->sock, page, |
9bd19663 | 1054 | con->out_msg_pos.page_pos + bio_offset, |
e36b13cc | 1055 | len, 1); |
31b8006e | 1056 | |
0cdf9e60 | 1057 | if (do_datacrc) |
31b8006e SW |
1058 | kunmap(page); |
1059 | ||
1060 | if (ret <= 0) | |
1061 | goto out; | |
1062 | ||
84ca8fc8 | 1063 | out_msg_pos_next(con, page, len, (size_t) ret, in_trail); |
31b8006e SW |
1064 | } |
1065 | ||
1066 | dout("write_partial_msg_pages %p msg %p done\n", con, msg); | |
1067 | ||
1068 | /* prepare and queue up footer, too */ | |
37675b0f | 1069 | if (!do_datacrc) |
84ca8fc8 | 1070 | msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC; |
e2200423 | 1071 | con_out_kvec_reset(con); |
859eb799 | 1072 | prepare_write_message_footer(con); |
31b8006e SW |
1073 | ret = 1; |
1074 | out: | |
1075 | return ret; | |
1076 | } | |
1077 | ||
1078 | /* | |
1079 | * write some zeros | |
1080 | */ | |
1081 | static int write_partial_skip(struct ceph_connection *con) | |
1082 | { | |
1083 | int ret; | |
1084 | ||
1085 | while (con->out_skip > 0) { | |
31739139 | 1086 | size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE); |
31b8006e | 1087 | |
31739139 | 1088 | ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1); |
31b8006e SW |
1089 | if (ret <= 0) |
1090 | goto out; | |
1091 | con->out_skip -= ret; | |
1092 | } | |
1093 | ret = 1; | |
1094 | out: | |
1095 | return ret; | |
1096 | } | |
1097 | ||
1098 | /* | |
1099 | * Prepare to read connection handshake, or an ack. | |
1100 | */ | |
eed0ef2c SW |
1101 | static void prepare_read_banner(struct ceph_connection *con) |
1102 | { | |
1103 | dout("prepare_read_banner %p\n", con); | |
1104 | con->in_base_pos = 0; | |
1105 | } | |
1106 | ||
31b8006e SW |
1107 | static void prepare_read_connect(struct ceph_connection *con) |
1108 | { | |
1109 | dout("prepare_read_connect %p\n", con); | |
1110 | con->in_base_pos = 0; | |
1111 | } | |
1112 | ||
1113 | static void prepare_read_ack(struct ceph_connection *con) | |
1114 | { | |
1115 | dout("prepare_read_ack %p\n", con); | |
1116 | con->in_base_pos = 0; | |
1117 | } | |
1118 | ||
1119 | static void prepare_read_tag(struct ceph_connection *con) | |
1120 | { | |
1121 | dout("prepare_read_tag %p\n", con); | |
1122 | con->in_base_pos = 0; | |
1123 | con->in_tag = CEPH_MSGR_TAG_READY; | |
1124 | } | |
1125 | ||
1126 | /* | |
1127 | * Prepare to read a message. | |
1128 | */ | |
1129 | static int prepare_read_message(struct ceph_connection *con) | |
1130 | { | |
1131 | dout("prepare_read_message %p\n", con); | |
1132 | BUG_ON(con->in_msg != NULL); | |
1133 | con->in_base_pos = 0; | |
1134 | con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0; | |
1135 | return 0; | |
1136 | } | |
1137 | ||
1138 | ||
1139 | static int read_partial(struct ceph_connection *con, | |
fd51653f | 1140 | int end, int size, void *object) |
31b8006e | 1141 | { |
e6cee71f AE |
1142 | while (con->in_base_pos < end) { |
1143 | int left = end - con->in_base_pos; | |
31b8006e SW |
1144 | int have = size - left; |
1145 | int ret = ceph_tcp_recvmsg(con->sock, object + have, left); | |
1146 | if (ret <= 0) | |
1147 | return ret; | |
1148 | con->in_base_pos += ret; | |
1149 | } | |
1150 | return 1; | |
1151 | } | |
1152 | ||
1153 | ||
1154 | /* | |
1155 | * Read all or part of the connect-side handshake on a new connection | |
1156 | */ | |
eed0ef2c | 1157 | static int read_partial_banner(struct ceph_connection *con) |
31b8006e | 1158 | { |
fd51653f AE |
1159 | int size; |
1160 | int end; | |
1161 | int ret; | |
31b8006e | 1162 | |
eed0ef2c | 1163 | dout("read_partial_banner %p at %d\n", con, con->in_base_pos); |
31b8006e SW |
1164 | |
1165 | /* peer's banner */ | |
fd51653f AE |
1166 | size = strlen(CEPH_BANNER); |
1167 | end = size; | |
1168 | ret = read_partial(con, end, size, con->in_banner); | |
31b8006e SW |
1169 | if (ret <= 0) |
1170 | goto out; | |
fd51653f AE |
1171 | |
1172 | size = sizeof (con->actual_peer_addr); | |
1173 | end += size; | |
1174 | ret = read_partial(con, end, size, &con->actual_peer_addr); | |
31b8006e SW |
1175 | if (ret <= 0) |
1176 | goto out; | |
fd51653f AE |
1177 | |
1178 | size = sizeof (con->peer_addr_for_me); | |
1179 | end += size; | |
1180 | ret = read_partial(con, end, size, &con->peer_addr_for_me); | |
31b8006e SW |
1181 | if (ret <= 0) |
1182 | goto out; | |
fd51653f | 1183 | |
eed0ef2c SW |
1184 | out: |
1185 | return ret; | |
1186 | } | |
1187 | ||
1188 | static int read_partial_connect(struct ceph_connection *con) | |
1189 | { | |
fd51653f AE |
1190 | int size; |
1191 | int end; | |
1192 | int ret; | |
eed0ef2c SW |
1193 | |
1194 | dout("read_partial_connect %p at %d\n", con, con->in_base_pos); | |
1195 | ||
fd51653f AE |
1196 | size = sizeof (con->in_reply); |
1197 | end = size; | |
1198 | ret = read_partial(con, end, size, &con->in_reply); | |
31b8006e SW |
1199 | if (ret <= 0) |
1200 | goto out; | |
fd51653f AE |
1201 | |
1202 | size = le32_to_cpu(con->in_reply.authorizer_len); | |
1203 | end += size; | |
1204 | ret = read_partial(con, end, size, con->auth_reply_buf); | |
4e7a5dcd SW |
1205 | if (ret <= 0) |
1206 | goto out; | |
31b8006e | 1207 | |
4e7a5dcd SW |
1208 | dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n", |
1209 | con, (int)con->in_reply.tag, | |
1210 | le32_to_cpu(con->in_reply.connect_seq), | |
31b8006e SW |
1211 | le32_to_cpu(con->in_reply.global_seq)); |
1212 | out: | |
1213 | return ret; | |
eed0ef2c | 1214 | |
31b8006e SW |
1215 | } |
1216 | ||
1217 | /* | |
1218 | * Verify the hello banner looks okay. | |
1219 | */ | |
1220 | static int verify_hello(struct ceph_connection *con) | |
1221 | { | |
1222 | if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) { | |
13e38c8a | 1223 | pr_err("connect to %s got bad banner\n", |
3d14c5d2 | 1224 | ceph_pr_addr(&con->peer_addr.in_addr)); |
31b8006e SW |
1225 | con->error_msg = "protocol error, bad banner"; |
1226 | return -1; | |
1227 | } | |
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | static bool addr_is_blank(struct sockaddr_storage *ss) | |
1232 | { | |
1233 | switch (ss->ss_family) { | |
1234 | case AF_INET: | |
1235 | return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0; | |
1236 | case AF_INET6: | |
1237 | return | |
1238 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 && | |
1239 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 && | |
1240 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 && | |
1241 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0; | |
1242 | } | |
1243 | return false; | |
1244 | } | |
1245 | ||
1246 | static int addr_port(struct sockaddr_storage *ss) | |
1247 | { | |
1248 | switch (ss->ss_family) { | |
1249 | case AF_INET: | |
f28bcfbe | 1250 | return ntohs(((struct sockaddr_in *)ss)->sin_port); |
31b8006e | 1251 | case AF_INET6: |
f28bcfbe | 1252 | return ntohs(((struct sockaddr_in6 *)ss)->sin6_port); |
31b8006e SW |
1253 | } |
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | static void addr_set_port(struct sockaddr_storage *ss, int p) | |
1258 | { | |
1259 | switch (ss->ss_family) { | |
1260 | case AF_INET: | |
1261 | ((struct sockaddr_in *)ss)->sin_port = htons(p); | |
a2a79609 | 1262 | break; |
31b8006e SW |
1263 | case AF_INET6: |
1264 | ((struct sockaddr_in6 *)ss)->sin6_port = htons(p); | |
a2a79609 | 1265 | break; |
31b8006e SW |
1266 | } |
1267 | } | |
1268 | ||
ee3b56f2 NW |
1269 | /* |
1270 | * Unlike other *_pton function semantics, zero indicates success. | |
1271 | */ | |
1272 | static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss, | |
1273 | char delim, const char **ipend) | |
1274 | { | |
99f0f3b2 AE |
1275 | struct sockaddr_in *in4 = (struct sockaddr_in *) ss; |
1276 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss; | |
ee3b56f2 NW |
1277 | |
1278 | memset(ss, 0, sizeof(*ss)); | |
1279 | ||
1280 | if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) { | |
1281 | ss->ss_family = AF_INET; | |
1282 | return 0; | |
1283 | } | |
1284 | ||
1285 | if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) { | |
1286 | ss->ss_family = AF_INET6; | |
1287 | return 0; | |
1288 | } | |
1289 | ||
1290 | return -EINVAL; | |
1291 | } | |
1292 | ||
1293 | /* | |
1294 | * Extract hostname string and resolve using kernel DNS facility. | |
1295 | */ | |
1296 | #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER | |
1297 | static int ceph_dns_resolve_name(const char *name, size_t namelen, | |
1298 | struct sockaddr_storage *ss, char delim, const char **ipend) | |
1299 | { | |
1300 | const char *end, *delim_p; | |
1301 | char *colon_p, *ip_addr = NULL; | |
1302 | int ip_len, ret; | |
1303 | ||
1304 | /* | |
1305 | * The end of the hostname occurs immediately preceding the delimiter or | |
1306 | * the port marker (':') where the delimiter takes precedence. | |
1307 | */ | |
1308 | delim_p = memchr(name, delim, namelen); | |
1309 | colon_p = memchr(name, ':', namelen); | |
1310 | ||
1311 | if (delim_p && colon_p) | |
1312 | end = delim_p < colon_p ? delim_p : colon_p; | |
1313 | else if (!delim_p && colon_p) | |
1314 | end = colon_p; | |
1315 | else { | |
1316 | end = delim_p; | |
1317 | if (!end) /* case: hostname:/ */ | |
1318 | end = name + namelen; | |
1319 | } | |
1320 | ||
1321 | if (end <= name) | |
1322 | return -EINVAL; | |
1323 | ||
1324 | /* do dns_resolve upcall */ | |
1325 | ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL); | |
1326 | if (ip_len > 0) | |
1327 | ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL); | |
1328 | else | |
1329 | ret = -ESRCH; | |
1330 | ||
1331 | kfree(ip_addr); | |
1332 | ||
1333 | *ipend = end; | |
1334 | ||
1335 | pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name, | |
1336 | ret, ret ? "failed" : ceph_pr_addr(ss)); | |
1337 | ||
1338 | return ret; | |
1339 | } | |
1340 | #else | |
1341 | static inline int ceph_dns_resolve_name(const char *name, size_t namelen, | |
1342 | struct sockaddr_storage *ss, char delim, const char **ipend) | |
1343 | { | |
1344 | return -EINVAL; | |
1345 | } | |
1346 | #endif | |
1347 | ||
1348 | /* | |
1349 | * Parse a server name (IP or hostname). If a valid IP address is not found | |
1350 | * then try to extract a hostname to resolve using userspace DNS upcall. | |
1351 | */ | |
1352 | static int ceph_parse_server_name(const char *name, size_t namelen, | |
1353 | struct sockaddr_storage *ss, char delim, const char **ipend) | |
1354 | { | |
1355 | int ret; | |
1356 | ||
1357 | ret = ceph_pton(name, namelen, ss, delim, ipend); | |
1358 | if (ret) | |
1359 | ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend); | |
1360 | ||
1361 | return ret; | |
1362 | } | |
1363 | ||
31b8006e SW |
1364 | /* |
1365 | * Parse an ip[:port] list into an addr array. Use the default | |
1366 | * monitor port if a port isn't specified. | |
1367 | */ | |
1368 | int ceph_parse_ips(const char *c, const char *end, | |
1369 | struct ceph_entity_addr *addr, | |
1370 | int max_count, int *count) | |
1371 | { | |
ee3b56f2 | 1372 | int i, ret = -EINVAL; |
31b8006e SW |
1373 | const char *p = c; |
1374 | ||
1375 | dout("parse_ips on '%.*s'\n", (int)(end-c), c); | |
1376 | for (i = 0; i < max_count; i++) { | |
1377 | const char *ipend; | |
1378 | struct sockaddr_storage *ss = &addr[i].in_addr; | |
31b8006e | 1379 | int port; |
39139f64 SW |
1380 | char delim = ','; |
1381 | ||
1382 | if (*p == '[') { | |
1383 | delim = ']'; | |
1384 | p++; | |
1385 | } | |
31b8006e | 1386 | |
ee3b56f2 NW |
1387 | ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend); |
1388 | if (ret) | |
31b8006e | 1389 | goto bad; |
ee3b56f2 NW |
1390 | ret = -EINVAL; |
1391 | ||
31b8006e SW |
1392 | p = ipend; |
1393 | ||
39139f64 SW |
1394 | if (delim == ']') { |
1395 | if (*p != ']') { | |
1396 | dout("missing matching ']'\n"); | |
1397 | goto bad; | |
1398 | } | |
1399 | p++; | |
1400 | } | |
1401 | ||
31b8006e SW |
1402 | /* port? */ |
1403 | if (p < end && *p == ':') { | |
1404 | port = 0; | |
1405 | p++; | |
1406 | while (p < end && *p >= '0' && *p <= '9') { | |
1407 | port = (port * 10) + (*p - '0'); | |
1408 | p++; | |
1409 | } | |
1410 | if (port > 65535 || port == 0) | |
1411 | goto bad; | |
1412 | } else { | |
1413 | port = CEPH_MON_PORT; | |
1414 | } | |
1415 | ||
1416 | addr_set_port(ss, port); | |
1417 | ||
3d14c5d2 | 1418 | dout("parse_ips got %s\n", ceph_pr_addr(ss)); |
31b8006e SW |
1419 | |
1420 | if (p == end) | |
1421 | break; | |
1422 | if (*p != ',') | |
1423 | goto bad; | |
1424 | p++; | |
1425 | } | |
1426 | ||
1427 | if (p != end) | |
1428 | goto bad; | |
1429 | ||
1430 | if (count) | |
1431 | *count = i + 1; | |
1432 | return 0; | |
1433 | ||
1434 | bad: | |
39139f64 | 1435 | pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c); |
ee3b56f2 | 1436 | return ret; |
31b8006e | 1437 | } |
3d14c5d2 | 1438 | EXPORT_SYMBOL(ceph_parse_ips); |
31b8006e | 1439 | |
eed0ef2c | 1440 | static int process_banner(struct ceph_connection *con) |
31b8006e | 1441 | { |
eed0ef2c | 1442 | dout("process_banner on %p\n", con); |
31b8006e SW |
1443 | |
1444 | if (verify_hello(con) < 0) | |
1445 | return -1; | |
1446 | ||
63f2d211 SW |
1447 | ceph_decode_addr(&con->actual_peer_addr); |
1448 | ceph_decode_addr(&con->peer_addr_for_me); | |
1449 | ||
31b8006e SW |
1450 | /* |
1451 | * Make sure the other end is who we wanted. note that the other | |
1452 | * end may not yet know their ip address, so if it's 0.0.0.0, give | |
1453 | * them the benefit of the doubt. | |
1454 | */ | |
103e2d3a SW |
1455 | if (memcmp(&con->peer_addr, &con->actual_peer_addr, |
1456 | sizeof(con->peer_addr)) != 0 && | |
31b8006e SW |
1457 | !(addr_is_blank(&con->actual_peer_addr.in_addr) && |
1458 | con->actual_peer_addr.nonce == con->peer_addr.nonce)) { | |
cd84db6e | 1459 | pr_warning("wrong peer, want %s/%d, got %s/%d\n", |
3d14c5d2 | 1460 | ceph_pr_addr(&con->peer_addr.in_addr), |
cd84db6e | 1461 | (int)le32_to_cpu(con->peer_addr.nonce), |
3d14c5d2 | 1462 | ceph_pr_addr(&con->actual_peer_addr.in_addr), |
cd84db6e | 1463 | (int)le32_to_cpu(con->actual_peer_addr.nonce)); |
58bb3b37 | 1464 | con->error_msg = "wrong peer at address"; |
31b8006e SW |
1465 | return -1; |
1466 | } | |
1467 | ||
1468 | /* | |
1469 | * did we learn our address? | |
1470 | */ | |
1471 | if (addr_is_blank(&con->msgr->inst.addr.in_addr)) { | |
1472 | int port = addr_port(&con->msgr->inst.addr.in_addr); | |
1473 | ||
1474 | memcpy(&con->msgr->inst.addr.in_addr, | |
1475 | &con->peer_addr_for_me.in_addr, | |
1476 | sizeof(con->peer_addr_for_me.in_addr)); | |
1477 | addr_set_port(&con->msgr->inst.addr.in_addr, port); | |
63f2d211 | 1478 | encode_my_addr(con->msgr); |
eed0ef2c | 1479 | dout("process_banner learned my addr is %s\n", |
3d14c5d2 | 1480 | ceph_pr_addr(&con->msgr->inst.addr.in_addr)); |
31b8006e SW |
1481 | } |
1482 | ||
eed0ef2c SW |
1483 | return 0; |
1484 | } | |
1485 | ||
04a419f9 SW |
1486 | static void fail_protocol(struct ceph_connection *con) |
1487 | { | |
1488 | reset_connection(con); | |
8dacc7da SW |
1489 | BUG_ON(con->state != CON_STATE_NEGOTIATING); |
1490 | con->state = CON_STATE_CLOSED; | |
04a419f9 SW |
1491 | } |
1492 | ||
eed0ef2c SW |
1493 | static int process_connect(struct ceph_connection *con) |
1494 | { | |
3d14c5d2 YS |
1495 | u64 sup_feat = con->msgr->supported_features; |
1496 | u64 req_feat = con->msgr->required_features; | |
04a419f9 | 1497 | u64 server_feat = le64_to_cpu(con->in_reply.features); |
0da5d703 | 1498 | int ret; |
04a419f9 | 1499 | |
eed0ef2c SW |
1500 | dout("process_connect on %p tag %d\n", con, (int)con->in_tag); |
1501 | ||
31b8006e | 1502 | switch (con->in_reply.tag) { |
04a419f9 SW |
1503 | case CEPH_MSGR_TAG_FEATURES: |
1504 | pr_err("%s%lld %s feature set mismatch," | |
1505 | " my %llx < server's %llx, missing %llx\n", | |
1506 | ENTITY_NAME(con->peer_name), | |
3d14c5d2 | 1507 | ceph_pr_addr(&con->peer_addr.in_addr), |
04a419f9 SW |
1508 | sup_feat, server_feat, server_feat & ~sup_feat); |
1509 | con->error_msg = "missing required protocol features"; | |
1510 | fail_protocol(con); | |
1511 | return -1; | |
1512 | ||
31b8006e | 1513 | case CEPH_MSGR_TAG_BADPROTOVER: |
31b8006e SW |
1514 | pr_err("%s%lld %s protocol version mismatch," |
1515 | " my %d != server's %d\n", | |
1516 | ENTITY_NAME(con->peer_name), | |
3d14c5d2 | 1517 | ceph_pr_addr(&con->peer_addr.in_addr), |
31b8006e SW |
1518 | le32_to_cpu(con->out_connect.protocol_version), |
1519 | le32_to_cpu(con->in_reply.protocol_version)); | |
1520 | con->error_msg = "protocol version mismatch"; | |
04a419f9 | 1521 | fail_protocol(con); |
31b8006e SW |
1522 | return -1; |
1523 | ||
4e7a5dcd SW |
1524 | case CEPH_MSGR_TAG_BADAUTHORIZER: |
1525 | con->auth_retry++; | |
1526 | dout("process_connect %p got BADAUTHORIZER attempt %d\n", con, | |
1527 | con->auth_retry); | |
1528 | if (con->auth_retry == 2) { | |
1529 | con->error_msg = "connect authorization failure"; | |
4e7a5dcd SW |
1530 | return -1; |
1531 | } | |
1532 | con->auth_retry = 1; | |
e825a66d | 1533 | ret = prepare_write_connect(con); |
0da5d703 SW |
1534 | if (ret < 0) |
1535 | return ret; | |
63733a0f | 1536 | prepare_read_connect(con); |
4e7a5dcd | 1537 | break; |
31b8006e SW |
1538 | |
1539 | case CEPH_MSGR_TAG_RESETSESSION: | |
1540 | /* | |
1541 | * If we connected with a large connect_seq but the peer | |
1542 | * has no record of a session with us (no connection, or | |
1543 | * connect_seq == 0), they will send RESETSESION to indicate | |
1544 | * that they must have reset their session, and may have | |
1545 | * dropped messages. | |
1546 | */ | |
1547 | dout("process_connect got RESET peer seq %u\n", | |
a16cb1f7 | 1548 | le32_to_cpu(con->in_reply.connect_seq)); |
31b8006e SW |
1549 | pr_err("%s%lld %s connection reset\n", |
1550 | ENTITY_NAME(con->peer_name), | |
3d14c5d2 | 1551 | ceph_pr_addr(&con->peer_addr.in_addr)); |
31b8006e | 1552 | reset_connection(con); |
5a0f8fdd AE |
1553 | ret = prepare_write_connect(con); |
1554 | if (ret < 0) | |
1555 | return ret; | |
31b8006e SW |
1556 | prepare_read_connect(con); |
1557 | ||
1558 | /* Tell ceph about it. */ | |
ec302645 | 1559 | mutex_unlock(&con->mutex); |
31b8006e SW |
1560 | pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name)); |
1561 | if (con->ops->peer_reset) | |
1562 | con->ops->peer_reset(con); | |
ec302645 | 1563 | mutex_lock(&con->mutex); |
8dacc7da | 1564 | if (con->state != CON_STATE_NEGOTIATING) |
0da5d703 | 1565 | return -EAGAIN; |
31b8006e SW |
1566 | break; |
1567 | ||
1568 | case CEPH_MSGR_TAG_RETRY_SESSION: | |
1569 | /* | |
1570 | * If we sent a smaller connect_seq than the peer has, try | |
1571 | * again with a larger value. | |
1572 | */ | |
a16cb1f7 | 1573 | dout("process_connect got RETRY_SESSION my seq %u, peer %u\n", |
31b8006e | 1574 | le32_to_cpu(con->out_connect.connect_seq), |
a16cb1f7 SW |
1575 | le32_to_cpu(con->in_reply.connect_seq)); |
1576 | con->connect_seq = le32_to_cpu(con->in_reply.connect_seq); | |
5a0f8fdd AE |
1577 | ret = prepare_write_connect(con); |
1578 | if (ret < 0) | |
1579 | return ret; | |
31b8006e SW |
1580 | prepare_read_connect(con); |
1581 | break; | |
1582 | ||
1583 | case CEPH_MSGR_TAG_RETRY_GLOBAL: | |
1584 | /* | |
1585 | * If we sent a smaller global_seq than the peer has, try | |
1586 | * again with a larger value. | |
1587 | */ | |
eed0ef2c | 1588 | dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n", |
31b8006e | 1589 | con->peer_global_seq, |
a16cb1f7 | 1590 | le32_to_cpu(con->in_reply.global_seq)); |
31b8006e | 1591 | get_global_seq(con->msgr, |
a16cb1f7 | 1592 | le32_to_cpu(con->in_reply.global_seq)); |
5a0f8fdd AE |
1593 | ret = prepare_write_connect(con); |
1594 | if (ret < 0) | |
1595 | return ret; | |
31b8006e SW |
1596 | prepare_read_connect(con); |
1597 | break; | |
1598 | ||
1599 | case CEPH_MSGR_TAG_READY: | |
04a419f9 SW |
1600 | if (req_feat & ~server_feat) { |
1601 | pr_err("%s%lld %s protocol feature mismatch," | |
1602 | " my required %llx > server's %llx, need %llx\n", | |
1603 | ENTITY_NAME(con->peer_name), | |
3d14c5d2 | 1604 | ceph_pr_addr(&con->peer_addr.in_addr), |
04a419f9 SW |
1605 | req_feat, server_feat, req_feat & ~server_feat); |
1606 | con->error_msg = "missing required protocol features"; | |
1607 | fail_protocol(con); | |
1608 | return -1; | |
1609 | } | |
8dacc7da SW |
1610 | |
1611 | BUG_ON(con->state != CON_STATE_NEGOTIATING); | |
1612 | con->state = CON_STATE_OPEN; | |
1613 | ||
31b8006e SW |
1614 | con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq); |
1615 | con->connect_seq++; | |
aba558e2 | 1616 | con->peer_features = server_feat; |
31b8006e SW |
1617 | dout("process_connect got READY gseq %d cseq %d (%d)\n", |
1618 | con->peer_global_seq, | |
1619 | le32_to_cpu(con->in_reply.connect_seq), | |
1620 | con->connect_seq); | |
1621 | WARN_ON(con->connect_seq != | |
1622 | le32_to_cpu(con->in_reply.connect_seq)); | |
92ac41d0 SW |
1623 | |
1624 | if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY) | |
928443cd | 1625 | set_bit(LOSSYTX, &con->flags); |
92ac41d0 | 1626 | |
85effe18 SW |
1627 | con->delay = 0; /* reset backoff memory */ |
1628 | ||
31b8006e SW |
1629 | prepare_read_tag(con); |
1630 | break; | |
1631 | ||
1632 | case CEPH_MSGR_TAG_WAIT: | |
1633 | /* | |
1634 | * If there is a connection race (we are opening | |
1635 | * connections to each other), one of us may just have | |
1636 | * to WAIT. This shouldn't happen if we are the | |
1637 | * client. | |
1638 | */ | |
04177882 SW |
1639 | pr_err("process_connect got WAIT as client\n"); |
1640 | con->error_msg = "protocol error, got WAIT as client"; | |
1641 | return -1; | |
31b8006e SW |
1642 | |
1643 | default: | |
1644 | pr_err("connect protocol error, will retry\n"); | |
1645 | con->error_msg = "protocol error, garbage tag during connect"; | |
1646 | return -1; | |
1647 | } | |
1648 | return 0; | |
1649 | } | |
1650 | ||
1651 | ||
1652 | /* | |
1653 | * read (part of) an ack | |
1654 | */ | |
1655 | static int read_partial_ack(struct ceph_connection *con) | |
1656 | { | |
fd51653f AE |
1657 | int size = sizeof (con->in_temp_ack); |
1658 | int end = size; | |
1659 | ||
1660 | return read_partial(con, end, size, &con->in_temp_ack); | |
31b8006e SW |
1661 | } |
1662 | ||
1663 | ||
1664 | /* | |
1665 | * We can finally discard anything that's been acked. | |
1666 | */ | |
1667 | static void process_ack(struct ceph_connection *con) | |
1668 | { | |
1669 | struct ceph_msg *m; | |
1670 | u64 ack = le64_to_cpu(con->in_temp_ack); | |
1671 | u64 seq; | |
1672 | ||
31b8006e SW |
1673 | while (!list_empty(&con->out_sent)) { |
1674 | m = list_first_entry(&con->out_sent, struct ceph_msg, | |
1675 | list_head); | |
1676 | seq = le64_to_cpu(m->hdr.seq); | |
1677 | if (seq > ack) | |
1678 | break; | |
1679 | dout("got ack for seq %llu type %d at %p\n", seq, | |
1680 | le16_to_cpu(m->hdr.type), m); | |
4cf9d544 | 1681 | m->ack_stamp = jiffies; |
31b8006e SW |
1682 | ceph_msg_remove(m); |
1683 | } | |
31b8006e SW |
1684 | prepare_read_tag(con); |
1685 | } | |
1686 | ||
1687 | ||
1688 | ||
1689 | ||
2450418c | 1690 | static int read_partial_message_section(struct ceph_connection *con, |
213c99ee SW |
1691 | struct kvec *section, |
1692 | unsigned int sec_len, u32 *crc) | |
2450418c | 1693 | { |
68b4476b | 1694 | int ret, left; |
2450418c YS |
1695 | |
1696 | BUG_ON(!section); | |
1697 | ||
1698 | while (section->iov_len < sec_len) { | |
1699 | BUG_ON(section->iov_base == NULL); | |
1700 | left = sec_len - section->iov_len; | |
1701 | ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base + | |
1702 | section->iov_len, left); | |
1703 | if (ret <= 0) | |
1704 | return ret; | |
1705 | section->iov_len += ret; | |
2450418c | 1706 | } |
fe3ad593 AE |
1707 | if (section->iov_len == sec_len) |
1708 | *crc = crc32c(0, section->iov_base, section->iov_len); | |
31b8006e | 1709 | |
2450418c YS |
1710 | return 1; |
1711 | } | |
31b8006e | 1712 | |
1c20f2d2 AE |
1713 | static bool ceph_con_in_msg_alloc(struct ceph_connection *con, |
1714 | struct ceph_msg_header *hdr); | |
68b4476b YS |
1715 | |
1716 | ||
1717 | static int read_partial_message_pages(struct ceph_connection *con, | |
1718 | struct page **pages, | |
95c96174 | 1719 | unsigned int data_len, bool do_datacrc) |
68b4476b YS |
1720 | { |
1721 | void *p; | |
1722 | int ret; | |
1723 | int left; | |
1724 | ||
1725 | left = min((int)(data_len - con->in_msg_pos.data_pos), | |
1726 | (int)(PAGE_SIZE - con->in_msg_pos.page_pos)); | |
1727 | /* (page) data */ | |
1728 | BUG_ON(pages == NULL); | |
1729 | p = kmap(pages[con->in_msg_pos.page]); | |
1730 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, | |
1731 | left); | |
bca064d2 | 1732 | if (ret > 0 && do_datacrc) |
68b4476b YS |
1733 | con->in_data_crc = |
1734 | crc32c(con->in_data_crc, | |
1735 | p + con->in_msg_pos.page_pos, ret); | |
1736 | kunmap(pages[con->in_msg_pos.page]); | |
1737 | if (ret <= 0) | |
1738 | return ret; | |
1739 | con->in_msg_pos.data_pos += ret; | |
1740 | con->in_msg_pos.page_pos += ret; | |
1741 | if (con->in_msg_pos.page_pos == PAGE_SIZE) { | |
1742 | con->in_msg_pos.page_pos = 0; | |
1743 | con->in_msg_pos.page++; | |
1744 | } | |
1745 | ||
1746 | return ret; | |
1747 | } | |
1748 | ||
1749 | #ifdef CONFIG_BLOCK | |
1750 | static int read_partial_message_bio(struct ceph_connection *con, | |
1751 | struct bio **bio_iter, int *bio_seg, | |
95c96174 | 1752 | unsigned int data_len, bool do_datacrc) |
68b4476b YS |
1753 | { |
1754 | struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg); | |
1755 | void *p; | |
1756 | int ret, left; | |
1757 | ||
68b4476b YS |
1758 | left = min((int)(data_len - con->in_msg_pos.data_pos), |
1759 | (int)(bv->bv_len - con->in_msg_pos.page_pos)); | |
1760 | ||
1761 | p = kmap(bv->bv_page) + bv->bv_offset; | |
1762 | ||
1763 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, | |
1764 | left); | |
bca064d2 | 1765 | if (ret > 0 && do_datacrc) |
68b4476b YS |
1766 | con->in_data_crc = |
1767 | crc32c(con->in_data_crc, | |
1768 | p + con->in_msg_pos.page_pos, ret); | |
1769 | kunmap(bv->bv_page); | |
1770 | if (ret <= 0) | |
1771 | return ret; | |
1772 | con->in_msg_pos.data_pos += ret; | |
1773 | con->in_msg_pos.page_pos += ret; | |
1774 | if (con->in_msg_pos.page_pos == bv->bv_len) { | |
1775 | con->in_msg_pos.page_pos = 0; | |
1776 | iter_bio_next(bio_iter, bio_seg); | |
1777 | } | |
1778 | ||
1779 | return ret; | |
1780 | } | |
1781 | #endif | |
1782 | ||
31b8006e SW |
1783 | /* |
1784 | * read (part of) a message. | |
1785 | */ | |
1786 | static int read_partial_message(struct ceph_connection *con) | |
1787 | { | |
1788 | struct ceph_msg *m = con->in_msg; | |
fd51653f AE |
1789 | int size; |
1790 | int end; | |
31b8006e | 1791 | int ret; |
95c96174 | 1792 | unsigned int front_len, middle_len, data_len; |
37675b0f | 1793 | bool do_datacrc = !con->msgr->nocrc; |
ae18756b | 1794 | u64 seq; |
fe3ad593 | 1795 | u32 crc; |
31b8006e SW |
1796 | |
1797 | dout("read_partial_message con %p msg %p\n", con, m); | |
1798 | ||
1799 | /* header */ | |
fd51653f AE |
1800 | size = sizeof (con->in_hdr); |
1801 | end = size; | |
1802 | ret = read_partial(con, end, size, &con->in_hdr); | |
57dac9d1 AE |
1803 | if (ret <= 0) |
1804 | return ret; | |
fe3ad593 AE |
1805 | |
1806 | crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc)); | |
1807 | if (cpu_to_le32(crc) != con->in_hdr.crc) { | |
1808 | pr_err("read_partial_message bad hdr " | |
1809 | " crc %u != expected %u\n", | |
1810 | crc, con->in_hdr.crc); | |
1811 | return -EBADMSG; | |
1812 | } | |
1813 | ||
31b8006e SW |
1814 | front_len = le32_to_cpu(con->in_hdr.front_len); |
1815 | if (front_len > CEPH_MSG_MAX_FRONT_LEN) | |
1816 | return -EIO; | |
1817 | middle_len = le32_to_cpu(con->in_hdr.middle_len); | |
1818 | if (middle_len > CEPH_MSG_MAX_DATA_LEN) | |
1819 | return -EIO; | |
1820 | data_len = le32_to_cpu(con->in_hdr.data_len); | |
1821 | if (data_len > CEPH_MSG_MAX_DATA_LEN) | |
1822 | return -EIO; | |
1823 | ||
ae18756b SW |
1824 | /* verify seq# */ |
1825 | seq = le64_to_cpu(con->in_hdr.seq); | |
1826 | if ((s64)seq - (s64)con->in_seq < 1) { | |
df9f86fa | 1827 | pr_info("skipping %s%lld %s seq %lld expected %lld\n", |
ae18756b | 1828 | ENTITY_NAME(con->peer_name), |
3d14c5d2 | 1829 | ceph_pr_addr(&con->peer_addr.in_addr), |
ae18756b SW |
1830 | seq, con->in_seq + 1); |
1831 | con->in_base_pos = -front_len - middle_len - data_len - | |
1832 | sizeof(m->footer); | |
1833 | con->in_tag = CEPH_MSGR_TAG_READY; | |
ae18756b SW |
1834 | return 0; |
1835 | } else if ((s64)seq - (s64)con->in_seq > 1) { | |
1836 | pr_err("read_partial_message bad seq %lld expected %lld\n", | |
1837 | seq, con->in_seq + 1); | |
1838 | con->error_msg = "bad message sequence # for incoming message"; | |
1839 | return -EBADMSG; | |
1840 | } | |
1841 | ||
31b8006e SW |
1842 | /* allocate message? */ |
1843 | if (!con->in_msg) { | |
1844 | dout("got hdr type %d front %d data %d\n", con->in_hdr.type, | |
1845 | con->in_hdr.front_len, con->in_hdr.data_len); | |
1c20f2d2 | 1846 | if (ceph_con_in_msg_alloc(con, &con->in_hdr)) { |
31b8006e | 1847 | /* skip this message */ |
a79832f2 | 1848 | dout("alloc_msg said skip message\n"); |
ae32be31 | 1849 | BUG_ON(con->in_msg); |
31b8006e SW |
1850 | con->in_base_pos = -front_len - middle_len - data_len - |
1851 | sizeof(m->footer); | |
1852 | con->in_tag = CEPH_MSGR_TAG_READY; | |
684be25c | 1853 | con->in_seq++; |
31b8006e SW |
1854 | return 0; |
1855 | } | |
a79832f2 | 1856 | if (!con->in_msg) { |
5b3a4db3 SW |
1857 | con->error_msg = |
1858 | "error allocating memory for incoming message"; | |
a79832f2 | 1859 | return -ENOMEM; |
31b8006e | 1860 | } |
38941f80 AE |
1861 | |
1862 | BUG_ON(con->in_msg->con != con); | |
31b8006e SW |
1863 | m = con->in_msg; |
1864 | m->front.iov_len = 0; /* haven't read it yet */ | |
2450418c YS |
1865 | if (m->middle) |
1866 | m->middle->vec.iov_len = 0; | |
9d7f0f13 YS |
1867 | |
1868 | con->in_msg_pos.page = 0; | |
68b4476b | 1869 | if (m->pages) |
c5c6b19d | 1870 | con->in_msg_pos.page_pos = m->page_alignment; |
68b4476b YS |
1871 | else |
1872 | con->in_msg_pos.page_pos = 0; | |
9d7f0f13 | 1873 | con->in_msg_pos.data_pos = 0; |
a4107026 SW |
1874 | |
1875 | #ifdef CONFIG_BLOCK | |
1876 | if (m->bio) | |
1877 | init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg); | |
1878 | #endif | |
31b8006e SW |
1879 | } |
1880 | ||
1881 | /* front */ | |
2450418c YS |
1882 | ret = read_partial_message_section(con, &m->front, front_len, |
1883 | &con->in_front_crc); | |
1884 | if (ret <= 0) | |
1885 | return ret; | |
31b8006e SW |
1886 | |
1887 | /* middle */ | |
2450418c | 1888 | if (m->middle) { |
213c99ee SW |
1889 | ret = read_partial_message_section(con, &m->middle->vec, |
1890 | middle_len, | |
2450418c | 1891 | &con->in_middle_crc); |
31b8006e SW |
1892 | if (ret <= 0) |
1893 | return ret; | |
31b8006e SW |
1894 | } |
1895 | ||
1896 | /* (page) data */ | |
31b8006e | 1897 | while (con->in_msg_pos.data_pos < data_len) { |
68b4476b YS |
1898 | if (m->pages) { |
1899 | ret = read_partial_message_pages(con, m->pages, | |
bca064d2 | 1900 | data_len, do_datacrc); |
68b4476b YS |
1901 | if (ret <= 0) |
1902 | return ret; | |
1903 | #ifdef CONFIG_BLOCK | |
1904 | } else if (m->bio) { | |
a4107026 | 1905 | BUG_ON(!m->bio_iter); |
68b4476b YS |
1906 | ret = read_partial_message_bio(con, |
1907 | &m->bio_iter, &m->bio_seg, | |
bca064d2 | 1908 | data_len, do_datacrc); |
68b4476b YS |
1909 | if (ret <= 0) |
1910 | return ret; | |
1911 | #endif | |
1912 | } else { | |
1913 | BUG_ON(1); | |
31b8006e SW |
1914 | } |
1915 | } | |
1916 | ||
31b8006e | 1917 | /* footer */ |
fd51653f AE |
1918 | size = sizeof (m->footer); |
1919 | end += size; | |
1920 | ret = read_partial(con, end, size, &m->footer); | |
57dac9d1 AE |
1921 | if (ret <= 0) |
1922 | return ret; | |
1923 | ||
31b8006e SW |
1924 | dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n", |
1925 | m, front_len, m->footer.front_crc, middle_len, | |
1926 | m->footer.middle_crc, data_len, m->footer.data_crc); | |
1927 | ||
1928 | /* crc ok? */ | |
1929 | if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) { | |
1930 | pr_err("read_partial_message %p front crc %u != exp. %u\n", | |
1931 | m, con->in_front_crc, m->footer.front_crc); | |
1932 | return -EBADMSG; | |
1933 | } | |
1934 | if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) { | |
1935 | pr_err("read_partial_message %p middle crc %u != exp %u\n", | |
1936 | m, con->in_middle_crc, m->footer.middle_crc); | |
1937 | return -EBADMSG; | |
1938 | } | |
bca064d2 | 1939 | if (do_datacrc && |
31b8006e SW |
1940 | (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 && |
1941 | con->in_data_crc != le32_to_cpu(m->footer.data_crc)) { | |
1942 | pr_err("read_partial_message %p data crc %u != exp. %u\n", m, | |
1943 | con->in_data_crc, le32_to_cpu(m->footer.data_crc)); | |
1944 | return -EBADMSG; | |
1945 | } | |
1946 | ||
1947 | return 1; /* done! */ | |
1948 | } | |
1949 | ||
1950 | /* | |
1951 | * Process message. This happens in the worker thread. The callback should | |
1952 | * be careful not to do anything that waits on other incoming messages or it | |
1953 | * may deadlock. | |
1954 | */ | |
1955 | static void process_message(struct ceph_connection *con) | |
1956 | { | |
5e095e8b | 1957 | struct ceph_msg *msg; |
31b8006e | 1958 | |
38941f80 AE |
1959 | BUG_ON(con->in_msg->con != con); |
1960 | con->in_msg->con = NULL; | |
5e095e8b | 1961 | msg = con->in_msg; |
31b8006e | 1962 | con->in_msg = NULL; |
36eb71aa | 1963 | con->ops->put(con); |
31b8006e SW |
1964 | |
1965 | /* if first message, set peer_name */ | |
1966 | if (con->peer_name.type == 0) | |
dbad185d | 1967 | con->peer_name = msg->hdr.src; |
31b8006e | 1968 | |
31b8006e | 1969 | con->in_seq++; |
ec302645 | 1970 | mutex_unlock(&con->mutex); |
31b8006e SW |
1971 | |
1972 | dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n", | |
1973 | msg, le64_to_cpu(msg->hdr.seq), | |
dbad185d | 1974 | ENTITY_NAME(msg->hdr.src), |
31b8006e SW |
1975 | le16_to_cpu(msg->hdr.type), |
1976 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), | |
1977 | le32_to_cpu(msg->hdr.front_len), | |
1978 | le32_to_cpu(msg->hdr.data_len), | |
1979 | con->in_front_crc, con->in_middle_crc, con->in_data_crc); | |
1980 | con->ops->dispatch(con, msg); | |
ec302645 SW |
1981 | |
1982 | mutex_lock(&con->mutex); | |
31b8006e SW |
1983 | prepare_read_tag(con); |
1984 | } | |
1985 | ||
1986 | ||
1987 | /* | |
1988 | * Write something to the socket. Called in a worker thread when the | |
1989 | * socket appears to be writeable and we have something ready to send. | |
1990 | */ | |
1991 | static int try_write(struct ceph_connection *con) | |
1992 | { | |
31b8006e SW |
1993 | int ret = 1; |
1994 | ||
d59315ca | 1995 | dout("try_write start %p state %lu\n", con, con->state); |
31b8006e | 1996 | |
31b8006e SW |
1997 | more: |
1998 | dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes); | |
1999 | ||
2000 | /* open the socket first? */ | |
8dacc7da SW |
2001 | if (con->state == CON_STATE_PREOPEN) { |
2002 | BUG_ON(con->sock); | |
2003 | con->state = CON_STATE_CONNECTING; | |
a5988c49 | 2004 | |
e2200423 | 2005 | con_out_kvec_reset(con); |
e825a66d | 2006 | prepare_write_banner(con); |
eed0ef2c | 2007 | prepare_read_banner(con); |
31b8006e | 2008 | |
cf3e5c40 | 2009 | BUG_ON(con->in_msg); |
31b8006e SW |
2010 | con->in_tag = CEPH_MSGR_TAG_READY; |
2011 | dout("try_write initiating connect on %p new state %lu\n", | |
2012 | con, con->state); | |
41617d0c AE |
2013 | ret = ceph_tcp_connect(con); |
2014 | if (ret < 0) { | |
31b8006e | 2015 | con->error_msg = "connect error"; |
31b8006e SW |
2016 | goto out; |
2017 | } | |
2018 | } | |
2019 | ||
2020 | more_kvec: | |
2021 | /* kvec data queued? */ | |
2022 | if (con->out_skip) { | |
2023 | ret = write_partial_skip(con); | |
2024 | if (ret <= 0) | |
42961d23 | 2025 | goto out; |
31b8006e SW |
2026 | } |
2027 | if (con->out_kvec_left) { | |
2028 | ret = write_partial_kvec(con); | |
2029 | if (ret <= 0) | |
42961d23 | 2030 | goto out; |
31b8006e SW |
2031 | } |
2032 | ||
2033 | /* msg pages? */ | |
2034 | if (con->out_msg) { | |
c86a2930 SW |
2035 | if (con->out_msg_done) { |
2036 | ceph_msg_put(con->out_msg); | |
2037 | con->out_msg = NULL; /* we're done with this one */ | |
2038 | goto do_next; | |
2039 | } | |
2040 | ||
31b8006e SW |
2041 | ret = write_partial_msg_pages(con); |
2042 | if (ret == 1) | |
2043 | goto more_kvec; /* we need to send the footer, too! */ | |
2044 | if (ret == 0) | |
42961d23 | 2045 | goto out; |
31b8006e SW |
2046 | if (ret < 0) { |
2047 | dout("try_write write_partial_msg_pages err %d\n", | |
2048 | ret); | |
42961d23 | 2049 | goto out; |
31b8006e SW |
2050 | } |
2051 | } | |
2052 | ||
c86a2930 | 2053 | do_next: |
8dacc7da | 2054 | if (con->state == CON_STATE_OPEN) { |
31b8006e SW |
2055 | /* is anything else pending? */ |
2056 | if (!list_empty(&con->out_queue)) { | |
2057 | prepare_write_message(con); | |
2058 | goto more; | |
2059 | } | |
2060 | if (con->in_seq > con->in_seq_acked) { | |
2061 | prepare_write_ack(con); | |
2062 | goto more; | |
2063 | } | |
928443cd | 2064 | if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) { |
31b8006e SW |
2065 | prepare_write_keepalive(con); |
2066 | goto more; | |
2067 | } | |
2068 | } | |
2069 | ||
2070 | /* Nothing to do! */ | |
928443cd | 2071 | clear_bit(WRITE_PENDING, &con->flags); |
31b8006e | 2072 | dout("try_write nothing else to write.\n"); |
31b8006e SW |
2073 | ret = 0; |
2074 | out: | |
42961d23 | 2075 | dout("try_write done on %p ret %d\n", con, ret); |
31b8006e SW |
2076 | return ret; |
2077 | } | |
2078 | ||
2079 | ||
2080 | ||
2081 | /* | |
2082 | * Read what we can from the socket. | |
2083 | */ | |
2084 | static int try_read(struct ceph_connection *con) | |
2085 | { | |
31b8006e SW |
2086 | int ret = -1; |
2087 | ||
8dacc7da SW |
2088 | more: |
2089 | dout("try_read start on %p state %lu\n", con, con->state); | |
2090 | if (con->state != CON_STATE_CONNECTING && | |
2091 | con->state != CON_STATE_NEGOTIATING && | |
2092 | con->state != CON_STATE_OPEN) | |
31b8006e SW |
2093 | return 0; |
2094 | ||
8dacc7da | 2095 | BUG_ON(!con->sock); |
ec302645 | 2096 | |
31b8006e SW |
2097 | dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag, |
2098 | con->in_base_pos); | |
0da5d703 | 2099 | |
8dacc7da | 2100 | if (con->state == CON_STATE_CONNECTING) { |
7593af92 AE |
2101 | dout("try_read connecting\n"); |
2102 | ret = read_partial_banner(con); | |
2103 | if (ret <= 0) | |
ab166d5a | 2104 | goto out; |
7593af92 AE |
2105 | ret = process_banner(con); |
2106 | if (ret < 0) | |
2107 | goto out; | |
2108 | ||
8dacc7da SW |
2109 | BUG_ON(con->state != CON_STATE_CONNECTING); |
2110 | con->state = CON_STATE_NEGOTIATING; | |
7593af92 AE |
2111 | |
2112 | /* Banner is good, exchange connection info */ | |
2113 | ret = prepare_write_connect(con); | |
2114 | if (ret < 0) | |
2115 | goto out; | |
2116 | prepare_read_connect(con); | |
2117 | ||
2118 | /* Send connection info before awaiting response */ | |
2119 | goto out; | |
2120 | } | |
2121 | ||
8dacc7da | 2122 | if (con->state == CON_STATE_NEGOTIATING) { |
7593af92 | 2123 | dout("try_read negotiating\n"); |
31b8006e SW |
2124 | ret = read_partial_connect(con); |
2125 | if (ret <= 0) | |
31b8006e | 2126 | goto out; |
98bdb0aa SW |
2127 | ret = process_connect(con); |
2128 | if (ret < 0) | |
2129 | goto out; | |
31b8006e SW |
2130 | goto more; |
2131 | } | |
2132 | ||
8dacc7da SW |
2133 | BUG_ON(con->state != CON_STATE_OPEN); |
2134 | ||
31b8006e SW |
2135 | if (con->in_base_pos < 0) { |
2136 | /* | |
2137 | * skipping + discarding content. | |
2138 | * | |
2139 | * FIXME: there must be a better way to do this! | |
2140 | */ | |
84495f49 AE |
2141 | static char buf[SKIP_BUF_SIZE]; |
2142 | int skip = min((int) sizeof (buf), -con->in_base_pos); | |
2143 | ||
31b8006e SW |
2144 | dout("skipping %d / %d bytes\n", skip, -con->in_base_pos); |
2145 | ret = ceph_tcp_recvmsg(con->sock, buf, skip); | |
2146 | if (ret <= 0) | |
98bdb0aa | 2147 | goto out; |
31b8006e SW |
2148 | con->in_base_pos += ret; |
2149 | if (con->in_base_pos) | |
2150 | goto more; | |
2151 | } | |
2152 | if (con->in_tag == CEPH_MSGR_TAG_READY) { | |
2153 | /* | |
2154 | * what's next? | |
2155 | */ | |
2156 | ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1); | |
2157 | if (ret <= 0) | |
98bdb0aa | 2158 | goto out; |
31b8006e SW |
2159 | dout("try_read got tag %d\n", (int)con->in_tag); |
2160 | switch (con->in_tag) { | |
2161 | case CEPH_MSGR_TAG_MSG: | |
2162 | prepare_read_message(con); | |
2163 | break; | |
2164 | case CEPH_MSGR_TAG_ACK: | |
2165 | prepare_read_ack(con); | |
2166 | break; | |
2167 | case CEPH_MSGR_TAG_CLOSE: | |
8dacc7da SW |
2168 | con_close_socket(con); |
2169 | con->state = CON_STATE_CLOSED; | |
98bdb0aa | 2170 | goto out; |
31b8006e SW |
2171 | default: |
2172 | goto bad_tag; | |
2173 | } | |
2174 | } | |
2175 | if (con->in_tag == CEPH_MSGR_TAG_MSG) { | |
2176 | ret = read_partial_message(con); | |
2177 | if (ret <= 0) { | |
2178 | switch (ret) { | |
2179 | case -EBADMSG: | |
2180 | con->error_msg = "bad crc"; | |
2181 | ret = -EIO; | |
98bdb0aa | 2182 | break; |
31b8006e SW |
2183 | case -EIO: |
2184 | con->error_msg = "io error"; | |
98bdb0aa | 2185 | break; |
31b8006e | 2186 | } |
98bdb0aa | 2187 | goto out; |
31b8006e SW |
2188 | } |
2189 | if (con->in_tag == CEPH_MSGR_TAG_READY) | |
2190 | goto more; | |
2191 | process_message(con); | |
2192 | goto more; | |
2193 | } | |
2194 | if (con->in_tag == CEPH_MSGR_TAG_ACK) { | |
2195 | ret = read_partial_ack(con); | |
2196 | if (ret <= 0) | |
98bdb0aa | 2197 | goto out; |
31b8006e SW |
2198 | process_ack(con); |
2199 | goto more; | |
2200 | } | |
2201 | ||
31b8006e | 2202 | out: |
98bdb0aa | 2203 | dout("try_read done on %p ret %d\n", con, ret); |
31b8006e SW |
2204 | return ret; |
2205 | ||
2206 | bad_tag: | |
2207 | pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag); | |
2208 | con->error_msg = "protocol error, garbage tag"; | |
2209 | ret = -1; | |
2210 | goto out; | |
2211 | } | |
2212 | ||
2213 | ||
2214 | /* | |
2215 | * Atomically queue work on a connection. Bump @con reference to | |
2216 | * avoid races with connection teardown. | |
31b8006e SW |
2217 | */ |
2218 | static void queue_con(struct ceph_connection *con) | |
2219 | { | |
31b8006e SW |
2220 | if (!con->ops->get(con)) { |
2221 | dout("queue_con %p ref count 0\n", con); | |
2222 | return; | |
2223 | } | |
2224 | ||
f363e45f | 2225 | if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) { |
31b8006e SW |
2226 | dout("queue_con %p - already queued\n", con); |
2227 | con->ops->put(con); | |
2228 | } else { | |
2229 | dout("queue_con %p\n", con); | |
2230 | } | |
2231 | } | |
2232 | ||
2233 | /* | |
2234 | * Do some work on a connection. Drop a connection ref when we're done. | |
2235 | */ | |
2236 | static void con_work(struct work_struct *work) | |
2237 | { | |
2238 | struct ceph_connection *con = container_of(work, struct ceph_connection, | |
2239 | work.work); | |
0da5d703 | 2240 | int ret; |
31b8006e | 2241 | |
9dd4658d | 2242 | mutex_lock(&con->mutex); |
0da5d703 | 2243 | restart: |
188048bc | 2244 | if (test_and_clear_bit(SOCK_CLOSED, &con->flags)) { |
8dacc7da SW |
2245 | switch (con->state) { |
2246 | case CON_STATE_CONNECTING: | |
188048bc | 2247 | con->error_msg = "connection failed"; |
8dacc7da SW |
2248 | break; |
2249 | case CON_STATE_NEGOTIATING: | |
2250 | con->error_msg = "negotiation failed"; | |
2251 | break; | |
2252 | case CON_STATE_OPEN: | |
2253 | con->error_msg = "socket closed"; | |
2254 | break; | |
2255 | default: | |
2256 | dout("unrecognized con state %d\n", (int)con->state); | |
e27947c7 | 2257 | con->error_msg = "unrecognized con state"; |
8dacc7da SW |
2258 | BUG(); |
2259 | } | |
188048bc AE |
2260 | goto fault; |
2261 | } | |
2262 | ||
928443cd | 2263 | if (test_and_clear_bit(BACKOFF, &con->flags)) { |
60bf8bf8 SW |
2264 | dout("con_work %p backing off\n", con); |
2265 | if (queue_delayed_work(ceph_msgr_wq, &con->work, | |
2266 | round_jiffies_relative(con->delay))) { | |
2267 | dout("con_work %p backoff %lu\n", con, con->delay); | |
2268 | mutex_unlock(&con->mutex); | |
2269 | return; | |
2270 | } else { | |
2271 | con->ops->put(con); | |
2272 | dout("con_work %p FAILED to back off %lu\n", con, | |
2273 | con->delay); | |
2274 | } | |
2275 | } | |
9dd4658d | 2276 | |
8dacc7da | 2277 | if (con->state == CON_STATE_STANDBY) { |
e00de341 SW |
2278 | dout("con_work %p STANDBY\n", con); |
2279 | goto done; | |
2280 | } | |
8dacc7da | 2281 | if (con->state == CON_STATE_CLOSED) { |
2e8cb100 SW |
2282 | dout("con_work %p CLOSED\n", con); |
2283 | BUG_ON(con->sock); | |
31b8006e SW |
2284 | goto done; |
2285 | } | |
8dacc7da | 2286 | if (con->state == CON_STATE_PREOPEN) { |
31b8006e | 2287 | dout("con_work OPENING\n"); |
2e8cb100 | 2288 | BUG_ON(con->sock); |
31b8006e SW |
2289 | } |
2290 | ||
0da5d703 SW |
2291 | ret = try_read(con); |
2292 | if (ret == -EAGAIN) | |
2293 | goto restart; | |
3a140a0d SW |
2294 | if (ret < 0) { |
2295 | con->error_msg = "socket error on read"; | |
0da5d703 | 2296 | goto fault; |
3a140a0d | 2297 | } |
0da5d703 SW |
2298 | |
2299 | ret = try_write(con); | |
2300 | if (ret == -EAGAIN) | |
2301 | goto restart; | |
3a140a0d SW |
2302 | if (ret < 0) { |
2303 | con->error_msg = "socket error on write"; | |
0da5d703 | 2304 | goto fault; |
3a140a0d | 2305 | } |
31b8006e SW |
2306 | |
2307 | done: | |
9dd4658d | 2308 | mutex_unlock(&con->mutex); |
9dd4658d | 2309 | done_unlocked: |
31b8006e | 2310 | con->ops->put(con); |
0da5d703 SW |
2311 | return; |
2312 | ||
2313 | fault: | |
2314 | mutex_unlock(&con->mutex); | |
2315 | ceph_fault(con); /* error/fault path */ | |
2316 | goto done_unlocked; | |
31b8006e SW |
2317 | } |
2318 | ||
2319 | ||
2320 | /* | |
2321 | * Generic error/fault handler. A retry mechanism is used with | |
2322 | * exponential backoff | |
2323 | */ | |
2324 | static void ceph_fault(struct ceph_connection *con) | |
2325 | { | |
3b5ede07 SW |
2326 | mutex_lock(&con->mutex); |
2327 | ||
31b8006e | 2328 | pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name), |
3d14c5d2 | 2329 | ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg); |
31b8006e | 2330 | dout("fault %p state %lu to peer %s\n", |
3d14c5d2 | 2331 | con, con->state, ceph_pr_addr(&con->peer_addr.in_addr)); |
31b8006e | 2332 | |
8dacc7da SW |
2333 | BUG_ON(con->state != CON_STATE_CONNECTING && |
2334 | con->state != CON_STATE_NEGOTIATING && | |
2335 | con->state != CON_STATE_OPEN); | |
ec302645 | 2336 | |
31b8006e | 2337 | con_close_socket(con); |
5e095e8b | 2338 | |
3b5ede07 | 2339 | if (test_bit(LOSSYTX, &con->flags)) { |
8dacc7da SW |
2340 | dout("fault on LOSSYTX channel, marking CLOSED\n"); |
2341 | con->state = CON_STATE_CLOSED; | |
3b5ede07 SW |
2342 | goto out_unlock; |
2343 | } | |
2344 | ||
5e095e8b | 2345 | if (con->in_msg) { |
38941f80 AE |
2346 | BUG_ON(con->in_msg->con != con); |
2347 | con->in_msg->con = NULL; | |
5e095e8b SW |
2348 | ceph_msg_put(con->in_msg); |
2349 | con->in_msg = NULL; | |
36eb71aa | 2350 | con->ops->put(con); |
5e095e8b | 2351 | } |
31b8006e | 2352 | |
e80a52d1 SW |
2353 | /* Requeue anything that hasn't been acked */ |
2354 | list_splice_init(&con->out_sent, &con->out_queue); | |
9bd2e6f8 | 2355 | |
e76661d0 SW |
2356 | /* If there are no messages queued or keepalive pending, place |
2357 | * the connection in a STANDBY state */ | |
2358 | if (list_empty(&con->out_queue) && | |
928443cd | 2359 | !test_bit(KEEPALIVE_PENDING, &con->flags)) { |
e00de341 | 2360 | dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con); |
928443cd | 2361 | clear_bit(WRITE_PENDING, &con->flags); |
8dacc7da | 2362 | con->state = CON_STATE_STANDBY; |
e80a52d1 SW |
2363 | } else { |
2364 | /* retry after a delay. */ | |
8dacc7da | 2365 | con->state = CON_STATE_PREOPEN; |
e80a52d1 SW |
2366 | if (con->delay == 0) |
2367 | con->delay = BASE_DELAY_INTERVAL; | |
2368 | else if (con->delay < MAX_DELAY_INTERVAL) | |
2369 | con->delay *= 2; | |
e80a52d1 SW |
2370 | con->ops->get(con); |
2371 | if (queue_delayed_work(ceph_msgr_wq, &con->work, | |
60bf8bf8 SW |
2372 | round_jiffies_relative(con->delay))) { |
2373 | dout("fault queued %p delay %lu\n", con, con->delay); | |
2374 | } else { | |
e80a52d1 | 2375 | con->ops->put(con); |
60bf8bf8 SW |
2376 | dout("fault failed to queue %p delay %lu, backoff\n", |
2377 | con, con->delay); | |
2378 | /* | |
2379 | * In many cases we see a socket state change | |
2380 | * while con_work is running and end up | |
2381 | * queuing (non-delayed) work, such that we | |
2382 | * can't backoff with a delay. Set a flag so | |
2383 | * that when con_work restarts we schedule the | |
2384 | * delay then. | |
2385 | */ | |
928443cd | 2386 | set_bit(BACKOFF, &con->flags); |
60bf8bf8 | 2387 | } |
31b8006e SW |
2388 | } |
2389 | ||
91e45ce3 SW |
2390 | out_unlock: |
2391 | mutex_unlock(&con->mutex); | |
161fd65a SW |
2392 | /* |
2393 | * in case we faulted due to authentication, invalidate our | |
2394 | * current tickets so that we can get new ones. | |
213c99ee | 2395 | */ |
161fd65a SW |
2396 | if (con->auth_retry && con->ops->invalidate_authorizer) { |
2397 | dout("calling invalidate_authorizer()\n"); | |
2398 | con->ops->invalidate_authorizer(con); | |
2399 | } | |
2400 | ||
31b8006e SW |
2401 | if (con->ops->fault) |
2402 | con->ops->fault(con); | |
2403 | } | |
2404 | ||
2405 | ||
2406 | ||
2407 | /* | |
15d9882c | 2408 | * initialize a new messenger instance |
31b8006e | 2409 | */ |
15d9882c AE |
2410 | void ceph_messenger_init(struct ceph_messenger *msgr, |
2411 | struct ceph_entity_addr *myaddr, | |
2412 | u32 supported_features, | |
2413 | u32 required_features, | |
2414 | bool nocrc) | |
31b8006e | 2415 | { |
3d14c5d2 YS |
2416 | msgr->supported_features = supported_features; |
2417 | msgr->required_features = required_features; | |
2418 | ||
31b8006e SW |
2419 | spin_lock_init(&msgr->global_seq_lock); |
2420 | ||
31b8006e SW |
2421 | if (myaddr) |
2422 | msgr->inst.addr = *myaddr; | |
2423 | ||
2424 | /* select a random nonce */ | |
ac8839d7 | 2425 | msgr->inst.addr.type = 0; |
103e2d3a | 2426 | get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce)); |
63f2d211 | 2427 | encode_my_addr(msgr); |
15d9882c | 2428 | msgr->nocrc = nocrc; |
31b8006e | 2429 | |
a2a32584 GH |
2430 | atomic_set(&msgr->stopping, 0); |
2431 | ||
15d9882c | 2432 | dout("%s %p\n", __func__, msgr); |
31b8006e | 2433 | } |
15d9882c | 2434 | EXPORT_SYMBOL(ceph_messenger_init); |
31b8006e | 2435 | |
e00de341 SW |
2436 | static void clear_standby(struct ceph_connection *con) |
2437 | { | |
2438 | /* come back from STANDBY? */ | |
8dacc7da | 2439 | if (con->state == CON_STATE_STANDBY) { |
e00de341 | 2440 | dout("clear_standby %p and ++connect_seq\n", con); |
8dacc7da | 2441 | con->state = CON_STATE_PREOPEN; |
e00de341 | 2442 | con->connect_seq++; |
928443cd AE |
2443 | WARN_ON(test_bit(WRITE_PENDING, &con->flags)); |
2444 | WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags)); | |
e00de341 SW |
2445 | } |
2446 | } | |
2447 | ||
31b8006e SW |
2448 | /* |
2449 | * Queue up an outgoing message on the given connection. | |
2450 | */ | |
2451 | void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg) | |
2452 | { | |
31b8006e | 2453 | /* set src+dst */ |
dbad185d | 2454 | msg->hdr.src = con->msgr->inst.name; |
3ca02ef9 | 2455 | BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len)); |
e84346b7 SW |
2456 | msg->needs_out_seq = true; |
2457 | ||
ec302645 | 2458 | mutex_lock(&con->mutex); |
92ce034b | 2459 | |
8dacc7da | 2460 | if (con->state == CON_STATE_CLOSED) { |
a59b55a6 SW |
2461 | dout("con_send %p closed, dropping %p\n", con, msg); |
2462 | ceph_msg_put(msg); | |
2463 | mutex_unlock(&con->mutex); | |
2464 | return; | |
2465 | } | |
2466 | ||
38941f80 | 2467 | BUG_ON(msg->con != NULL); |
36eb71aa | 2468 | msg->con = con->ops->get(con); |
92ce034b AE |
2469 | BUG_ON(msg->con == NULL); |
2470 | ||
31b8006e SW |
2471 | BUG_ON(!list_empty(&msg->list_head)); |
2472 | list_add_tail(&msg->list_head, &con->out_queue); | |
2473 | dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg, | |
2474 | ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type), | |
2475 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), | |
2476 | le32_to_cpu(msg->hdr.front_len), | |
2477 | le32_to_cpu(msg->hdr.middle_len), | |
2478 | le32_to_cpu(msg->hdr.data_len)); | |
00650931 SW |
2479 | |
2480 | clear_standby(con); | |
ec302645 | 2481 | mutex_unlock(&con->mutex); |
31b8006e SW |
2482 | |
2483 | /* if there wasn't anything waiting to send before, queue | |
2484 | * new work */ | |
928443cd | 2485 | if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0) |
31b8006e SW |
2486 | queue_con(con); |
2487 | } | |
3d14c5d2 | 2488 | EXPORT_SYMBOL(ceph_con_send); |
31b8006e SW |
2489 | |
2490 | /* | |
2491 | * Revoke a message that was previously queued for send | |
2492 | */ | |
6740a845 | 2493 | void ceph_msg_revoke(struct ceph_msg *msg) |
31b8006e | 2494 | { |
6740a845 AE |
2495 | struct ceph_connection *con = msg->con; |
2496 | ||
2497 | if (!con) | |
2498 | return; /* Message not in our possession */ | |
2499 | ||
ec302645 | 2500 | mutex_lock(&con->mutex); |
31b8006e | 2501 | if (!list_empty(&msg->list_head)) { |
38941f80 | 2502 | dout("%s %p msg %p - was on queue\n", __func__, con, msg); |
31b8006e | 2503 | list_del_init(&msg->list_head); |
38941f80 | 2504 | BUG_ON(msg->con == NULL); |
36eb71aa | 2505 | msg->con->ops->put(msg->con); |
38941f80 | 2506 | msg->con = NULL; |
92ce034b | 2507 | msg->hdr.seq = 0; |
38941f80 | 2508 | |
31b8006e | 2509 | ceph_msg_put(msg); |
ed98adad SW |
2510 | } |
2511 | if (con->out_msg == msg) { | |
38941f80 | 2512 | dout("%s %p msg %p - was sending\n", __func__, con, msg); |
ed98adad | 2513 | con->out_msg = NULL; |
31b8006e SW |
2514 | if (con->out_kvec_is_msg) { |
2515 | con->out_skip = con->out_kvec_bytes; | |
2516 | con->out_kvec_is_msg = false; | |
2517 | } | |
ed98adad | 2518 | msg->hdr.seq = 0; |
92ce034b AE |
2519 | |
2520 | ceph_msg_put(msg); | |
31b8006e | 2521 | } |
ec302645 | 2522 | mutex_unlock(&con->mutex); |
31b8006e SW |
2523 | } |
2524 | ||
350b1c32 | 2525 | /* |
0d59ab81 | 2526 | * Revoke a message that we may be reading data into |
350b1c32 | 2527 | */ |
8921d114 | 2528 | void ceph_msg_revoke_incoming(struct ceph_msg *msg) |
350b1c32 | 2529 | { |
8921d114 AE |
2530 | struct ceph_connection *con; |
2531 | ||
2532 | BUG_ON(msg == NULL); | |
2533 | if (!msg->con) { | |
2534 | dout("%s msg %p null con\n", __func__, msg); | |
2535 | ||
2536 | return; /* Message not in our possession */ | |
2537 | } | |
2538 | ||
2539 | con = msg->con; | |
350b1c32 | 2540 | mutex_lock(&con->mutex); |
8921d114 | 2541 | if (con->in_msg == msg) { |
95c96174 ED |
2542 | unsigned int front_len = le32_to_cpu(con->in_hdr.front_len); |
2543 | unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len); | |
2544 | unsigned int data_len = le32_to_cpu(con->in_hdr.data_len); | |
350b1c32 SW |
2545 | |
2546 | /* skip rest of message */ | |
8921d114 AE |
2547 | dout("%s %p msg %p revoked\n", __func__, con, msg); |
2548 | con->in_base_pos = con->in_base_pos - | |
350b1c32 | 2549 | sizeof(struct ceph_msg_header) - |
0d59ab81 YS |
2550 | front_len - |
2551 | middle_len - | |
2552 | data_len - | |
350b1c32 | 2553 | sizeof(struct ceph_msg_footer); |
350b1c32 SW |
2554 | ceph_msg_put(con->in_msg); |
2555 | con->in_msg = NULL; | |
2556 | con->in_tag = CEPH_MSGR_TAG_READY; | |
684be25c | 2557 | con->in_seq++; |
350b1c32 | 2558 | } else { |
8921d114 AE |
2559 | dout("%s %p in_msg %p msg %p no-op\n", |
2560 | __func__, con, con->in_msg, msg); | |
350b1c32 SW |
2561 | } |
2562 | mutex_unlock(&con->mutex); | |
2563 | } | |
2564 | ||
31b8006e SW |
2565 | /* |
2566 | * Queue a keepalive byte to ensure the tcp connection is alive. | |
2567 | */ | |
2568 | void ceph_con_keepalive(struct ceph_connection *con) | |
2569 | { | |
e00de341 | 2570 | dout("con_keepalive %p\n", con); |
00650931 | 2571 | mutex_lock(&con->mutex); |
e00de341 | 2572 | clear_standby(con); |
00650931 | 2573 | mutex_unlock(&con->mutex); |
928443cd AE |
2574 | if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 && |
2575 | test_and_set_bit(WRITE_PENDING, &con->flags) == 0) | |
31b8006e SW |
2576 | queue_con(con); |
2577 | } | |
3d14c5d2 | 2578 | EXPORT_SYMBOL(ceph_con_keepalive); |
31b8006e SW |
2579 | |
2580 | ||
2581 | /* | |
2582 | * construct a new message with given type, size | |
2583 | * the new msg has a ref count of 1. | |
2584 | */ | |
b61c2763 SW |
2585 | struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags, |
2586 | bool can_fail) | |
31b8006e SW |
2587 | { |
2588 | struct ceph_msg *m; | |
2589 | ||
34d23762 | 2590 | m = kmalloc(sizeof(*m), flags); |
31b8006e SW |
2591 | if (m == NULL) |
2592 | goto out; | |
c2e552e7 | 2593 | kref_init(&m->kref); |
38941f80 AE |
2594 | |
2595 | m->con = NULL; | |
31b8006e SW |
2596 | INIT_LIST_HEAD(&m->list_head); |
2597 | ||
45c6ceb5 | 2598 | m->hdr.tid = 0; |
31b8006e | 2599 | m->hdr.type = cpu_to_le16(type); |
45c6ceb5 SW |
2600 | m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); |
2601 | m->hdr.version = 0; | |
31b8006e SW |
2602 | m->hdr.front_len = cpu_to_le32(front_len); |
2603 | m->hdr.middle_len = 0; | |
bb257664 SW |
2604 | m->hdr.data_len = 0; |
2605 | m->hdr.data_off = 0; | |
45c6ceb5 | 2606 | m->hdr.reserved = 0; |
31b8006e SW |
2607 | m->footer.front_crc = 0; |
2608 | m->footer.middle_crc = 0; | |
2609 | m->footer.data_crc = 0; | |
45c6ceb5 | 2610 | m->footer.flags = 0; |
31b8006e SW |
2611 | m->front_max = front_len; |
2612 | m->front_is_vmalloc = false; | |
2613 | m->more_to_follow = false; | |
c0d5f9db | 2614 | m->ack_stamp = 0; |
31b8006e SW |
2615 | m->pool = NULL; |
2616 | ||
ca20892d HC |
2617 | /* middle */ |
2618 | m->middle = NULL; | |
2619 | ||
2620 | /* data */ | |
2621 | m->nr_pages = 0; | |
2622 | m->page_alignment = 0; | |
2623 | m->pages = NULL; | |
2624 | m->pagelist = NULL; | |
2625 | m->bio = NULL; | |
2626 | m->bio_iter = NULL; | |
2627 | m->bio_seg = 0; | |
2628 | m->trail = NULL; | |
2629 | ||
31b8006e SW |
2630 | /* front */ |
2631 | if (front_len) { | |
2632 | if (front_len > PAGE_CACHE_SIZE) { | |
34d23762 | 2633 | m->front.iov_base = __vmalloc(front_len, flags, |
31b8006e SW |
2634 | PAGE_KERNEL); |
2635 | m->front_is_vmalloc = true; | |
2636 | } else { | |
34d23762 | 2637 | m->front.iov_base = kmalloc(front_len, flags); |
31b8006e SW |
2638 | } |
2639 | if (m->front.iov_base == NULL) { | |
b61c2763 | 2640 | dout("ceph_msg_new can't allocate %d bytes\n", |
31b8006e SW |
2641 | front_len); |
2642 | goto out2; | |
2643 | } | |
2644 | } else { | |
2645 | m->front.iov_base = NULL; | |
2646 | } | |
2647 | m->front.iov_len = front_len; | |
2648 | ||
bb257664 | 2649 | dout("ceph_msg_new %p front %d\n", m, front_len); |
31b8006e SW |
2650 | return m; |
2651 | ||
2652 | out2: | |
2653 | ceph_msg_put(m); | |
2654 | out: | |
b61c2763 SW |
2655 | if (!can_fail) { |
2656 | pr_err("msg_new can't create type %d front %d\n", type, | |
2657 | front_len); | |
f0ed1b7c | 2658 | WARN_ON(1); |
b61c2763 SW |
2659 | } else { |
2660 | dout("msg_new can't create type %d front %d\n", type, | |
2661 | front_len); | |
2662 | } | |
a79832f2 | 2663 | return NULL; |
31b8006e | 2664 | } |
3d14c5d2 | 2665 | EXPORT_SYMBOL(ceph_msg_new); |
31b8006e | 2666 | |
31b8006e SW |
2667 | /* |
2668 | * Allocate "middle" portion of a message, if it is needed and wasn't | |
2669 | * allocated by alloc_msg. This allows us to read a small fixed-size | |
2670 | * per-type header in the front and then gracefully fail (i.e., | |
2671 | * propagate the error to the caller based on info in the front) when | |
2672 | * the middle is too large. | |
2673 | */ | |
2450418c | 2674 | static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg) |
31b8006e SW |
2675 | { |
2676 | int type = le16_to_cpu(msg->hdr.type); | |
2677 | int middle_len = le32_to_cpu(msg->hdr.middle_len); | |
2678 | ||
2679 | dout("alloc_middle %p type %d %s middle_len %d\n", msg, type, | |
2680 | ceph_msg_type_name(type), middle_len); | |
2681 | BUG_ON(!middle_len); | |
2682 | BUG_ON(msg->middle); | |
2683 | ||
b6c1d5b8 | 2684 | msg->middle = ceph_buffer_new(middle_len, GFP_NOFS); |
31b8006e SW |
2685 | if (!msg->middle) |
2686 | return -ENOMEM; | |
2687 | return 0; | |
2688 | } | |
2689 | ||
2450418c | 2690 | /* |
1c20f2d2 AE |
2691 | * Allocate a message for receiving an incoming message on a |
2692 | * connection, and save the result in con->in_msg. Uses the | |
2693 | * connection's private alloc_msg op if available. | |
2694 | * | |
2695 | * Returns true if the message should be skipped, false otherwise. | |
2696 | * If true is returned (skip message), con->in_msg will be NULL. | |
2697 | * If false is returned, con->in_msg will contain a pointer to the | |
2698 | * newly-allocated message, or NULL in case of memory exhaustion. | |
2450418c | 2699 | */ |
1c20f2d2 AE |
2700 | static bool ceph_con_in_msg_alloc(struct ceph_connection *con, |
2701 | struct ceph_msg_header *hdr) | |
2450418c YS |
2702 | { |
2703 | int type = le16_to_cpu(hdr->type); | |
2704 | int front_len = le32_to_cpu(hdr->front_len); | |
2705 | int middle_len = le32_to_cpu(hdr->middle_len); | |
2450418c YS |
2706 | int ret; |
2707 | ||
1c20f2d2 AE |
2708 | BUG_ON(con->in_msg != NULL); |
2709 | ||
2450418c | 2710 | if (con->ops->alloc_msg) { |
1c20f2d2 AE |
2711 | int skip = 0; |
2712 | ||
0547a9b3 | 2713 | mutex_unlock(&con->mutex); |
1c20f2d2 | 2714 | con->in_msg = con->ops->alloc_msg(con, hdr, &skip); |
0547a9b3 | 2715 | mutex_lock(&con->mutex); |
92ce034b | 2716 | if (con->in_msg) { |
36eb71aa | 2717 | con->in_msg->con = con->ops->get(con); |
92ce034b AE |
2718 | BUG_ON(con->in_msg->con == NULL); |
2719 | } | |
1c20f2d2 AE |
2720 | if (skip) |
2721 | con->in_msg = NULL; | |
2722 | ||
2723 | if (!con->in_msg) | |
2724 | return skip != 0; | |
2450418c | 2725 | } |
1c20f2d2 AE |
2726 | if (!con->in_msg) { |
2727 | con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false); | |
2728 | if (!con->in_msg) { | |
2450418c YS |
2729 | pr_err("unable to allocate msg type %d len %d\n", |
2730 | type, front_len); | |
1c20f2d2 | 2731 | return false; |
2450418c | 2732 | } |
36eb71aa | 2733 | con->in_msg->con = con->ops->get(con); |
92ce034b | 2734 | BUG_ON(con->in_msg->con == NULL); |
1c20f2d2 | 2735 | con->in_msg->page_alignment = le16_to_cpu(hdr->data_off); |
2450418c | 2736 | } |
1c20f2d2 | 2737 | memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr)); |
2450418c | 2738 | |
1c20f2d2 AE |
2739 | if (middle_len && !con->in_msg->middle) { |
2740 | ret = ceph_alloc_middle(con, con->in_msg); | |
2450418c | 2741 | if (ret < 0) { |
1c20f2d2 AE |
2742 | ceph_msg_put(con->in_msg); |
2743 | con->in_msg = NULL; | |
2450418c YS |
2744 | } |
2745 | } | |
9d7f0f13 | 2746 | |
1c20f2d2 | 2747 | return false; |
2450418c YS |
2748 | } |
2749 | ||
31b8006e SW |
2750 | |
2751 | /* | |
2752 | * Free a generically kmalloc'd message. | |
2753 | */ | |
2754 | void ceph_msg_kfree(struct ceph_msg *m) | |
2755 | { | |
2756 | dout("msg_kfree %p\n", m); | |
2757 | if (m->front_is_vmalloc) | |
2758 | vfree(m->front.iov_base); | |
2759 | else | |
2760 | kfree(m->front.iov_base); | |
2761 | kfree(m); | |
2762 | } | |
2763 | ||
2764 | /* | |
2765 | * Drop a msg ref. Destroy as needed. | |
2766 | */ | |
c2e552e7 SW |
2767 | void ceph_msg_last_put(struct kref *kref) |
2768 | { | |
2769 | struct ceph_msg *m = container_of(kref, struct ceph_msg, kref); | |
31b8006e | 2770 | |
c2e552e7 SW |
2771 | dout("ceph_msg_put last one on %p\n", m); |
2772 | WARN_ON(!list_empty(&m->list_head)); | |
2773 | ||
2774 | /* drop middle, data, if any */ | |
2775 | if (m->middle) { | |
2776 | ceph_buffer_put(m->middle); | |
2777 | m->middle = NULL; | |
31b8006e | 2778 | } |
c2e552e7 SW |
2779 | m->nr_pages = 0; |
2780 | m->pages = NULL; | |
2781 | ||
58bb3b37 SW |
2782 | if (m->pagelist) { |
2783 | ceph_pagelist_release(m->pagelist); | |
2784 | kfree(m->pagelist); | |
2785 | m->pagelist = NULL; | |
2786 | } | |
2787 | ||
68b4476b YS |
2788 | m->trail = NULL; |
2789 | ||
c2e552e7 SW |
2790 | if (m->pool) |
2791 | ceph_msgpool_put(m->pool, m); | |
2792 | else | |
2793 | ceph_msg_kfree(m); | |
31b8006e | 2794 | } |
3d14c5d2 | 2795 | EXPORT_SYMBOL(ceph_msg_last_put); |
9ec7cab1 SW |
2796 | |
2797 | void ceph_msg_dump(struct ceph_msg *msg) | |
2798 | { | |
2799 | pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg, | |
2800 | msg->front_max, msg->nr_pages); | |
2801 | print_hex_dump(KERN_DEBUG, "header: ", | |
2802 | DUMP_PREFIX_OFFSET, 16, 1, | |
2803 | &msg->hdr, sizeof(msg->hdr), true); | |
2804 | print_hex_dump(KERN_DEBUG, " front: ", | |
2805 | DUMP_PREFIX_OFFSET, 16, 1, | |
2806 | msg->front.iov_base, msg->front.iov_len, true); | |
2807 | if (msg->middle) | |
2808 | print_hex_dump(KERN_DEBUG, "middle: ", | |
2809 | DUMP_PREFIX_OFFSET, 16, 1, | |
2810 | msg->middle->vec.iov_base, | |
2811 | msg->middle->vec.iov_len, true); | |
2812 | print_hex_dump(KERN_DEBUG, "footer: ", | |
2813 | DUMP_PREFIX_OFFSET, 16, 1, | |
2814 | &msg->footer, sizeof(msg->footer), true); | |
2815 | } | |
3d14c5d2 | 2816 | EXPORT_SYMBOL(ceph_msg_dump); |