1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "include/compat.h"
#include "include/sock_compat.h"
#include <iterator>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <sys/uio.h>
#include <limits.h>
#include <poll.h>
#include "msg/msg_types.h"
#include "msg/Message.h"
#include "Accepter.h"
#include "Pipe.h"
#include "SimpleMessenger.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/safe_io.h"
#define dout_subsys ceph_subsys_ms
#undef dout_prefix
#define dout_prefix *_dout << "accepter."
/********************************************
* Accepter
*/
int Accepter::create_selfpipe(int *pipe_rd, int *pipe_wr) {
int selfpipe[2];
if (pipe_cloexec(selfpipe) < 0) {
int e = errno;
lderr(msgr->cct) << __func__ << " unable to create the selfpipe: "
<< cpp_strerror(e) << dendl;
return -e;
}
for (size_t i = 0; i < std::size(selfpipe); i++) {
int rc = fcntl(selfpipe[i], F_GETFL);
ceph_assert(rc != -1);
rc = fcntl(selfpipe[i], F_SETFL, rc | O_NONBLOCK);
ceph_assert(rc != -1);
}
*pipe_rd = selfpipe[0];
*pipe_wr = selfpipe[1];
return 0;
}
int Accepter::bind(const entity_addr_t &bind_addr, const set<int>& avoid_ports)
{
const auto& conf = msgr->cct->_conf;
// bind to a socket
ldout(msgr->cct,10) << __func__ << dendl;
int family;
switch (bind_addr.get_family()) {
case AF_INET:
case AF_INET6:
family = bind_addr.get_family();
break;
default:
// bind_addr is empty
family = conf->ms_bind_ipv6 ? AF_INET6 : AF_INET;
}
/* socket creation */
listen_sd = socket_cloexec(family, SOCK_STREAM, 0);
if (listen_sd < 0) {
int e = errno;
lderr(msgr->cct) << __func__ << " unable to create socket: "
<< cpp_strerror(e) << dendl;
return -e;
}
ldout(msgr->cct,10) << __func__ << " socket sd: " << listen_sd << dendl;
// use whatever user specified (if anything)
entity_addr_t listen_addr = bind_addr;
if (listen_addr.get_type() == entity_addr_t::TYPE_NONE) {
listen_addr.set_type(entity_addr_t::TYPE_LEGACY);
}
listen_addr.set_family(family);
/* bind to port */
int rc = -1;
int r = -1;
for (int i = 0; i < conf->ms_bind_retry_count; i++) {
if (i > 0) {
lderr(msgr->cct) << __func__ << " was unable to bind. Trying again in "
<< conf->ms_bind_retry_delay << " seconds " << dendl;
sleep(conf->ms_bind_retry_delay);
}
if (listen_addr.get_port()) {
// specific port
// reuse addr+port when possible
int on = 1;
rc = ::setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (rc < 0) {
lderr(msgr->cct) << __func__ << " unable to setsockopt: "
<< cpp_strerror(errno) << dendl;
r = -errno;
continue;
}
rc = ::bind(listen_sd, listen_addr.get_sockaddr(),
listen_addr.get_sockaddr_len());
if (rc < 0) {
lderr(msgr->cct) << __func__ << " unable to bind to " << listen_addr
<< ": " << cpp_strerror(errno) << dendl;
r = -errno;
continue;
}
} else {
// try a range of ports
for (int port = msgr->cct->_conf->ms_bind_port_min;
port <= msgr->cct->_conf->ms_bind_port_max; port++) {
if (avoid_ports.count(port))
continue;
listen_addr.set_port(port);
rc = ::bind(listen_sd, listen_addr.get_sockaddr(),
listen_addr.get_sockaddr_len());
if (rc == 0)
break;
}
if (rc < 0) {
lderr(msgr->cct) << __func__ << " unable to bind to " << listen_addr
<< " on any port in range " << msgr->cct->_conf->ms_bind_port_min
<< "-" << msgr->cct->_conf->ms_bind_port_max
<< ": " << cpp_strerror(errno)
<< dendl;
r = -errno;
// Clear port before retry, otherwise we shall fail again.
listen_addr.set_port(0);
continue;
}
ldout(msgr->cct,10) << __func__ << " bound on random port "
<< listen_addr << dendl;
}
if (rc == 0)
break;
}
// It seems that binding completely failed, return with that exit status
if (rc < 0) {
lderr(msgr->cct) << __func__ << " was unable to bind after "
<< conf->ms_bind_retry_count << " attempts: "
<< cpp_strerror(errno) << dendl;
::close(listen_sd);
listen_sd = -1;
return r;
}
// what port did we get?
sockaddr_storage ss;
socklen_t llen = sizeof(ss);
rc = getsockname(listen_sd, (sockaddr*)&ss, &llen);
if (rc < 0) {
rc = -errno;
lderr(msgr->cct) << __func__ << " failed getsockname: "
<< cpp_strerror(rc) << dendl;
::close(listen_sd);
listen_sd = -1;
return rc;
}
listen_addr.set_sockaddr((sockaddr*)&ss);
if (msgr->cct->_conf->ms_tcp_rcvbuf) {
int size = msgr->cct->_conf->ms_tcp_rcvbuf;
rc = ::setsockopt(listen_sd, SOL_SOCKET, SO_RCVBUF,
(void*)&size, sizeof(size));
if (rc < 0) {
rc = -errno;
lderr(msgr->cct) << __func__ << " failed to set SO_RCVBUF to "
<< size << ": " << cpp_strerror(rc) << dendl;
::close(listen_sd);
listen_sd = -1;
return rc;
}
}
ldout(msgr->cct,10) << __func__ << " bound to " << listen_addr << dendl;
// listen!
rc = ::listen(listen_sd, msgr->cct->_conf->ms_tcp_listen_backlog);
if (rc < 0) {
rc = -errno;
lderr(msgr->cct) << __func__ << " unable to listen on " << listen_addr
<< ": " << cpp_strerror(rc) << dendl;
::close(listen_sd);
listen_sd = -1;
return rc;
}
msgr->set_myaddrs(entity_addrvec_t(bind_addr));
if (bind_addr != entity_addr_t() &&
!bind_addr.is_blank_ip())
msgr->learned_addr(bind_addr);
else
ceph_assert(msgr->get_need_addr()); // should still be true.
if (msgr->get_myaddr_legacy().get_port() == 0) {
msgr->set_myaddrs(entity_addrvec_t(listen_addr));
}
entity_addr_t addr = msgr->get_myaddr_legacy();
addr.nonce = nonce;
msgr->set_myaddrs(entity_addrvec_t(addr));
msgr->init_local_connection();
rc = create_selfpipe(&shutdown_rd_fd, &shutdown_wr_fd);
if (rc < 0) {
lderr(msgr->cct) << __func__ << " unable to create signalling pipe " << listen_addr
<< ": " << cpp_strerror(rc) << dendl;
return rc;
}
ldout(msgr->cct,1) << __func__ << " my_addrs " << *msgr->my_addrs
<< " my_addr " << msgr->my_addr
<< " need_addr=" << msgr->get_need_addr() << dendl;
return 0;
}
int Accepter::rebind(const set<int>& avoid_ports)
{
ldout(msgr->cct,1) << __func__ << " avoid " << avoid_ports << dendl;
entity_addr_t addr = msgr->get_myaddr_legacy();
set<int> new_avoid = avoid_ports;
new_avoid.insert(addr.get_port());
addr.set_port(0);
// adjust the nonce; we want our entity_addr_t to be truly unique.
nonce += 1000000;
entity_addrvec_t newaddrs = *msgr->my_addrs;
newaddrs.v[0].nonce = nonce;
msgr->set_myaddrs(newaddrs);
ldout(msgr->cct,10) << __func__ << " new nonce " << nonce << " and addr "
<< msgr->my_addr << dendl;
ldout(msgr->cct,10) << " will try " << addr << " and avoid ports " << new_avoid << dendl;
int r = bind(addr, new_avoid);
if (r == 0)
start();
return r;
}
int Accepter::start()
{
ldout(msgr->cct,1) << __func__ << dendl;
// start thread
create("ms_accepter");
return 0;
}
void *Accepter::entry()
{
ldout(msgr->cct,1) << __func__ << " start" << dendl;
int errors = 0;
struct pollfd pfd[2];
memset(pfd, 0, sizeof(pfd));
pfd[0].fd = listen_sd;
pfd[0].events = POLLIN | POLLERR | POLLNVAL | POLLHUP;
pfd[1].fd = shutdown_rd_fd;
pfd[1].events = POLLIN | POLLERR | POLLNVAL | POLLHUP;
while (!done) {
ldout(msgr->cct,20) << __func__ << " calling poll for sd:" << listen_sd << dendl;
int r = poll(pfd, 2, -1);
if (r < 0) {
if (errno == EINTR) {
continue;
}
ldout(msgr->cct,1) << __func__ << " poll got error"
<< " errno " << errno << " " << cpp_strerror(errno) << dendl;
ceph_abort();
}
ldout(msgr->cct,10) << __func__ << " poll returned oke: " << r << dendl;
ldout(msgr->cct,20) << __func__ << " pfd.revents[0]=" << pfd[0].revents << dendl;
ldout(msgr->cct,20) << __func__ << " pfd.revents[1]=" << pfd[1].revents << dendl;
if (pfd[0].revents & (POLLERR | POLLNVAL | POLLHUP)) {
ldout(msgr->cct,1) << __func__ << " poll got errors in revents "
<< pfd[0].revents << dendl;
ceph_abort();
}
if (pfd[1].revents & (POLLIN | POLLERR | POLLNVAL | POLLHUP)) {
// We got "signaled" to exit the poll
// clean the selfpipe
char ch;
if (::read(shutdown_rd_fd, &ch, sizeof(ch)) == -1) {
if (errno != EAGAIN)
ldout(msgr->cct,1) << __func__ << " Cannot read selfpipe: "
<< " errno " << errno << " " << cpp_strerror(errno) << dendl;
}
break;
}
if (done) break;
// accept
sockaddr_storage ss;
socklen_t slen = sizeof(ss);
int sd = accept_cloexec(listen_sd, (sockaddr*)&ss, &slen);
if (sd >= 0) {
errors = 0;
ldout(msgr->cct,10) << __func__ << " incoming on sd " << sd << dendl;
msgr->add_accept_pipe(sd);
} else {
int e = errno;
ldout(msgr->cct,0) << __func__ << " no incoming connection? sd = " << sd
<< " errno " << e << " " << cpp_strerror(e) << dendl;
if (++errors > msgr->cct->_conf->ms_max_accept_failures) {
lderr(msgr->cct) << "accetper has encoutered enough errors, just do ceph_abort()." << dendl;
ceph_abort();
}
}
}
ldout(msgr->cct,20) << __func__ << " closing" << dendl;
// socket is closed right after the thread has joined.
// closing it here might race
if (shutdown_rd_fd >= 0) {
::close(shutdown_rd_fd);
shutdown_rd_fd = -1;
}
ldout(msgr->cct,10) << __func__ << " stopping" << dendl;
return 0;
}
void Accepter::stop()
{
done = true;
ldout(msgr->cct,10) << __func__ << " accept listening on: " << listen_sd << dendl;
if (shutdown_wr_fd < 0)
return;
// Send a byte to the shutdown pipe that the thread is listening to
char ch = 0x0;
int ret = safe_write(shutdown_wr_fd, &ch, sizeof(ch));
if (ret < 0) {
ldout(msgr->cct,1) << __func__ << " close failed: "
<< " errno " << errno << " " << cpp_strerror(errno) << dendl;
} else {
ldout(msgr->cct,15) << __func__ << " signaled poll" << dendl;
}
VOID_TEMP_FAILURE_RETRY(close(shutdown_wr_fd));
shutdown_wr_fd = -1;
// wait for thread to stop before closing the socket, to avoid
// racing against fd re-use.
if (is_started()) {
ldout(msgr->cct,5) << __func__ << " wait for thread to join." << dendl;
join();
}
if (listen_sd >= 0) {
if (::close(listen_sd) < 0) {
ldout(msgr->cct,1) << __func__ << " close listen_sd failed: "
<< " errno " << errno << " " << cpp_strerror(errno) << dendl;
}
listen_sd = -1;
}
if (shutdown_rd_fd >= 0) {
if (::close(shutdown_rd_fd) < 0) {
ldout(msgr->cct,1) << __func__ << " close shutdown_rd_fd failed: "
<< " errno " << errno << " " << cpp_strerror(errno) << dendl;
}
shutdown_rd_fd = -1;
}
done = false;
}
|