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