Skip to content

Commit c1c6e1a

Browse files
committed
updated to support SO_BINDTODEVICE.
1 parent 1bff8cb commit c1c6e1a

File tree

9 files changed

+33
-53
lines changed

9 files changed

+33
-53
lines changed

src/brpc/channel.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ ChannelSSLOptions* ChannelOptions::mutable_ssl_options() {
7777
static ChannelSignature ComputeChannelSignature(const ChannelOptions& opt) {
7878
if (opt.auth == NULL &&
7979
!opt.has_ssl_options() &&
80-
opt.client_host.empty() &&
80+
opt.device_name.empty() &&
8181
opt.connection_group.empty() &&
8282
opt.hc_option.health_check_path.empty()) {
8383
// Returning zeroized result by default is more intuitive for users.
@@ -95,9 +95,9 @@ static ChannelSignature ComputeChannelSignature(const ChannelOptions& opt) {
9595
buf.append("|conng=");
9696
buf.append(opt.connection_group);
9797
}
98-
if (!opt.client_host.empty()) {
99-
buf.append("|clih=");
100-
buf.append(opt.client_host);
98+
if (!opt.device_name.empty()) {
99+
buf.append("|devn=");
100+
buf.append(opt.device_name);
101101
}
102102
if (opt.auth) {
103103
buf.append("|auth=");
@@ -367,13 +367,6 @@ int Channel::InitSingle(const butil::EndPoint& server_addr_and_port,
367367
LOG(ERROR) << "Invalid port=" << port;
368368
return -1;
369369
}
370-
butil::EndPoint client_endpoint;
371-
if (!_options.client_host.empty() &&
372-
butil::str2ip(_options.client_host.c_str(), &client_endpoint.ip) != 0 &&
373-
butil::hostname2ip(_options.client_host.c_str(), &client_endpoint.ip) != 0) {
374-
LOG(ERROR) << "Invalid client host=`" << _options.client_host << '\'';
375-
return -1;
376-
}
377370
_server_address = server_addr_and_port;
378371
const ChannelSignature sig = ComputeChannelSignature(_options);
379372
std::shared_ptr<SocketSSLContext> ssl_ctx;
@@ -382,7 +375,7 @@ int Channel::InitSingle(const butil::EndPoint& server_addr_and_port,
382375
}
383376
if (SocketMapInsert(SocketMapKey(server_addr_and_port, sig),
384377
&_server_id, ssl_ctx, _options.use_rdma,
385-
_options.hc_option, client_endpoint) != 0) {
378+
_options.hc_option, _options.device_name) != 0) {
386379
LOG(ERROR) << "Fail to insert into SocketMap";
387380
return -1;
388381
}
@@ -410,13 +403,6 @@ int Channel::Init(const char* ns_url,
410403
_options.mutable_ssl_options()->sni_name = _service_name;
411404
}
412405
}
413-
butil::EndPoint client_endpoint;
414-
if (!_options.client_host.empty() &&
415-
butil::str2ip(_options.client_host.c_str(), &client_endpoint.ip) != 0 &&
416-
butil::hostname2ip(_options.client_host.c_str(), &client_endpoint.ip) != 0) {
417-
LOG(ERROR) << "Invalid client host=`" << _options.client_host << '\'';
418-
return -1;
419-
}
420406
std::unique_ptr<LoadBalancerWithNaming> lb(new (std::nothrow)
421407
LoadBalancerWithNaming);
422408
if (NULL == lb) {
@@ -429,7 +415,7 @@ int Channel::Init(const char* ns_url,
429415
ns_opt.use_rdma = _options.use_rdma;
430416
ns_opt.channel_signature = ComputeChannelSignature(_options);
431417
ns_opt.hc_option = _options.hc_option;
432-
ns_opt.client_endpoint = client_endpoint;
418+
ns_opt.device_name = _options.device_name;
433419
if (CreateSocketSSLContext(_options, &ns_opt.ssl_ctx) != 0) {
434420
return -1;
435421
}

src/brpc/channel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ struct ChannelOptions {
149149
// When it is not set, FLAGS_health_check_path and FLAGS_health_check_timeout_ms will take effect.
150150
HealthCheckOption hc_option;
151151

152-
// IP address or host name of the client.
153-
// if the client_host is "", the client IP address is determined by the OS.
152+
// The device name of the client's network adapter.
153+
// if the device_name is "", the flow control is determined by the OS.
154154
// Default: ""
155-
std::string client_host;
155+
std::string device_name;
156156
private:
157157
// SSLOptions is large and not often used, allocate it on heap to
158158
// prevent ChannelOptions from being bloated in most cases.

src/brpc/details/naming_service_thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void NamingServiceThread::Actions::ResetServers(
127127
const SocketMapKey key(_added[i], _owner->_options.channel_signature);
128128
CHECK_EQ(0, SocketMapInsert(key, &tagged_id.id, _owner->_options.ssl_ctx,
129129
_owner->_options.use_rdma, _owner->_options.hc_option,
130-
_owner->_options.client_endpoint));
130+
_owner->_options.device_name));
131131
_added_sockets.push_back(tagged_id);
132132
}
133133

src/brpc/details/naming_service_thread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct GetNamingServiceThreadOptions {
5353
HealthCheckOption hc_option;
5454
ChannelSignature channel_signature;
5555
std::shared_ptr<SocketSSLContext> ssl_ctx;
56-
butil::EndPoint client_endpoint;
56+
std::string device_name;
5757
};
5858

5959
// A dedicated thread to map a name to ServerIds

src/brpc/socket.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ int Socket::OnCreated(const SocketOptions& options) {
728728
_keytable_pool = options.keytable_pool;
729729
_tos = 0;
730730
_remote_side = options.remote_side;
731-
_local_side = options.local_side;
731+
_device_name = options.device_name;
732732
_on_edge_triggered_events = options.on_edge_triggered_events;
733733
_user = options.user;
734734
_conn = options.conn;
@@ -1296,14 +1296,11 @@ int Socket::Connect(const timespec* abstime,
12961296
CHECK_EQ(0, butil::make_close_on_exec(sockfd));
12971297
// We need to do async connect (to manage the timeout by ourselves).
12981298
CHECK_EQ(0, butil::make_non_blocking(sockfd));
1299-
if (local_side().ip != butil::IP_ANY) {
1300-
struct sockaddr_storage cli_addr;
1301-
if (butil::endpoint2sockaddr(local_side(), &cli_addr, &addr_size) != 0) {
1302-
PLOG(ERROR) << "Fail to get client sockaddr";
1303-
return -1;
1304-
}
1305-
if (::bind(sockfd, (struct sockaddr*)&cli_addr, addr_size) != 0) {
1306-
PLOG(ERROR) << "Fail to bind client socket, errno=" << strerror(errno);
1299+
if (!_device_name.empty()) {
1300+
if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,
1301+
_device_name.c_str(), _device_name.size()) < 0) {
1302+
PLOG(ERROR) << "Fail to set SO_BINDTODEVICE of fd=" << sockfd
1303+
<< " to device_name=" << _device_name;
13071304
return -1;
13081305
}
13091306
}
@@ -2821,7 +2818,6 @@ int Socket::GetPooledSocket(SocketUniquePtr* pooled_socket) {
28212818
if (socket_pool == NULL) {
28222819
SocketOptions opt;
28232820
opt.remote_side = remote_side();
2824-
opt.local_side = butil::EndPoint(local_side().ip, 0);
28252821
opt.user = user();
28262822
opt.on_edge_triggered_events = _on_edge_triggered_events;
28272823
opt.initial_ssl_ctx = _ssl_ctx;
@@ -2923,7 +2919,6 @@ int Socket::GetShortSocket(SocketUniquePtr* short_socket) {
29232919
SocketId id;
29242920
SocketOptions opt;
29252921
opt.remote_side = remote_side();
2926-
opt.local_side = butil::EndPoint(local_side().ip, 0);
29272922
opt.user = user();
29282923
opt.on_edge_triggered_events = _on_edge_triggered_events;
29292924
opt.initial_ssl_ctx = _ssl_ctx;

src/brpc/socket.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ struct SocketOptions {
250250
// user->BeforeRecycle() before recycling.
251251
int fd{-1};
252252
butil::EndPoint remote_side;
253-
butil::EndPoint local_side;
253+
std::string device_name;
254254
// If `connect_on_create' is true and `fd' is less than 0,
255255
// a client connection will be established to remote_side()
256256
// regarding deadline `connect_abstime' when Socket is being created.
@@ -831,6 +831,9 @@ friend void DereferenceSocket(Socket*);
831831
// Address of self. Initialized in ResetFileDescriptor().
832832
butil::EndPoint _local_side;
833833

834+
// The device name of the client's network adapter.
835+
std::string _device_name;
836+
834837
// Called when edge-triggered events happened on `_fd'. Read comments
835838
// of EventDispatcher::AddConsumer (event_dispatcher.h)
836839
// carefully before implementing the callback.

src/brpc/socket_map.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ int SocketMapInsert(const SocketMapKey& key, SocketId* id,
9393
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
9494
bool use_rdma,
9595
const HealthCheckOption& hc_option,
96-
const butil::EndPoint& client_endpoint) {
97-
return get_or_new_client_side_socket_map()->Insert(key, id, ssl_ctx, use_rdma, hc_option, client_endpoint);
96+
const std::string& device_name) {
97+
return get_or_new_client_side_socket_map()->Insert(key, id, ssl_ctx, use_rdma, hc_option, device_name);
9898
}
9999

100100
int SocketMapFind(const SocketMapKey& key, SocketId* id) {
@@ -231,7 +231,7 @@ int SocketMap::Insert(const SocketMapKey& key, SocketId* id,
231231
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
232232
bool use_rdma,
233233
const HealthCheckOption& hc_option,
234-
const butil::EndPoint& client_endpoint) {
234+
const std::string& device_name) {
235235
ShowSocketMapInBvarIfNeed();
236236

237237
std::unique_lock<butil::Mutex> mu(_mutex);
@@ -253,7 +253,7 @@ int SocketMap::Insert(const SocketMapKey& key, SocketId* id,
253253
SocketId tmp_id;
254254
SocketOptions opt;
255255
opt.remote_side = key.peer.addr;
256-
opt.local_side = client_endpoint;
256+
opt.device_name = device_name;
257257
opt.initial_ssl_ctx = ssl_ctx;
258258
opt.use_rdma = use_rdma;
259259
opt.hc_option = hc_option;

src/brpc/socket_map.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,18 @@ int SocketMapInsert(const SocketMapKey& key, SocketId* id,
8383
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
8484
bool use_rdma,
8585
const HealthCheckOption& hc_option,
86-
const butil::EndPoint& client_endpoint);
86+
const std::string& device_name);
8787

8888
inline int SocketMapInsert(const SocketMapKey& key, SocketId* id,
8989
const std::shared_ptr<SocketSSLContext>& ssl_ctx) {
9090
HealthCheckOption hc_option;
91-
butil::EndPoint endpoint;
92-
return SocketMapInsert(key, id, ssl_ctx, false, hc_option, endpoint);
91+
return SocketMapInsert(key, id, ssl_ctx, false, hc_option, "");
9392
}
9493

9594
inline int SocketMapInsert(const SocketMapKey& key, SocketId* id) {
9695
std::shared_ptr<SocketSSLContext> empty_ptr;
9796
HealthCheckOption hc_option;
98-
butil::EndPoint endpoint;
99-
return SocketMapInsert(key, id, empty_ptr, false, hc_option, endpoint);
97+
return SocketMapInsert(key, id, empty_ptr, false, hc_option, "");
10098
}
10199

102100
// Find the SocketId associated with `key'.
@@ -159,19 +157,17 @@ class SocketMap {
159157
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
160158
bool use_rdma,
161159
const HealthCheckOption& hc_option,
162-
const butil::EndPoint& client_endpoint);
160+
const std::string& device_name);
163161

164162
int Insert(const SocketMapKey& key, SocketId* id,
165163
const std::shared_ptr<SocketSSLContext>& ssl_ctx) {
166164
HealthCheckOption hc_option;
167-
butil::EndPoint endpoint;
168-
return Insert(key, id, ssl_ctx, false, hc_option, endpoint);
165+
return Insert(key, id, ssl_ctx, false, hc_option, "");
169166
}
170167
int Insert(const SocketMapKey& key, SocketId* id) {
171168
std::shared_ptr<SocketSSLContext> empty_ptr;
172169
HealthCheckOption hc_option;
173-
butil::EndPoint endpoint;
174-
return Insert(key, id, empty_ptr, false, hc_option, endpoint);
170+
return Insert(key, id, empty_ptr, false, hc_option, "");
175171
}
176172

177173
void Remove(const SocketMapKey& key, SocketId expected_id);

test/brpc_server_unittest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,7 +2087,7 @@ void TestClientHost(const butil::EndPoint& ep,
20872087
ASSERT_EQ(cntl.ErrorCode(), error_code);
20882088
}
20892089

2090-
TEST_F(ServerTest, client_host) {
2090+
TEST_F(ServerTest, network_device_name) {
20912091
butil::EndPoint ep;
20922092
ASSERT_EQ(0, str2endpoint("127.0.0.1:8613", &ep));
20932093
brpc::Server server;
@@ -2098,7 +2098,7 @@ TEST_F(ServerTest, client_host) {
20982098

20992099
brpc::Controller cntl;
21002100
brpc::ChannelOptions copt;
2101-
copt.client_host = "localhost";
2101+
copt.device_name = "lo";
21022102
std::vector<brpc::ConnectionType> connection_types = {
21032103
brpc::CONNECTION_TYPE_SINGLE,
21042104
brpc::CONNECTION_TYPE_POOLED,

0 commit comments

Comments
 (0)