]>
git.proxmox.com Git - ceph.git/blob - ceph/src/crimson/net/SocketConnection.cc
57e5c12c1aed433e89df4c3d4b7c15348b226e08
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2017 Red Hat, Inc
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "SocketConnection.h"
17 #include "ProtocolV2.h"
18 #include "SocketMessenger.h"
20 #ifdef UNIT_TESTS_BUILT
21 #include "Interceptor.h"
25 using crimson::common::local_conf
;
27 namespace crimson::net
{
29 SocketConnection::SocketConnection(SocketMessenger
& messenger
,
30 ChainedDispatchers
& dispatchers
)
31 : msgr_sid
{messenger
.get_shard_id()}, messenger(messenger
)
33 auto ret
= create_handlers(dispatchers
, *this);
34 io_handler
= std::move(ret
.io_handler
);
35 protocol
= std::move(ret
.protocol
);
36 #ifdef UNIT_TESTS_BUILT
37 if (messenger
.interceptor
) {
38 interceptor
= messenger
.interceptor
;
39 interceptor
->register_conn(this->get_local_shared_foreign_from_this());
44 SocketConnection::~SocketConnection() {}
46 bool SocketConnection::is_connected() const
48 return io_handler
->is_connected();
51 #ifdef UNIT_TESTS_BUILT
52 bool SocketConnection::is_protocol_ready() const
54 assert(seastar::this_shard_id() == msgr_sid
);
55 return protocol
->is_ready();
58 bool SocketConnection::is_protocol_standby() const {
59 assert(seastar::this_shard_id() == msgr_sid
);
60 return protocol
->is_standby();
63 bool SocketConnection::is_protocol_closed() const
65 assert(seastar::this_shard_id() == msgr_sid
);
66 return protocol
->is_closed();
69 bool SocketConnection::is_protocol_closed_clean() const
71 assert(seastar::this_shard_id() == msgr_sid
);
72 return protocol
->is_closed_clean();
76 bool SocketConnection::peer_wins() const
78 assert(seastar::this_shard_id() == msgr_sid
);
79 return (messenger
.get_myaddr() > peer_addr
|| policy
.server
);
82 seastar::future
<> SocketConnection::send(MessageURef _msg
)
84 // may be invoked from any core
85 MessageFRef msg
= seastar::make_foreign(std::move(_msg
));
86 return io_handler
->send(std::move(msg
));
89 seastar::future
<> SocketConnection::send_keepalive()
91 // may be invoked from any core
92 return io_handler
->send_keepalive();
95 SocketConnection::clock_t::time_point
96 SocketConnection::get_last_keepalive() const
98 return io_handler
->get_last_keepalive();
101 SocketConnection::clock_t::time_point
102 SocketConnection::get_last_keepalive_ack() const
104 return io_handler
->get_last_keepalive_ack();
107 void SocketConnection::set_last_keepalive_ack(clock_t::time_point when
)
109 io_handler
->set_last_keepalive_ack(when
);
112 void SocketConnection::mark_down()
114 io_handler
->mark_down();
118 SocketConnection::start_connect(const entity_addr_t
& _peer_addr
,
119 const entity_name_t
& _peer_name
)
121 assert(seastar::this_shard_id() == msgr_sid
);
122 protocol
->start_connect(_peer_addr
, _peer_name
);
126 SocketConnection::start_accept(SocketFRef
&& sock
,
127 const entity_addr_t
& _peer_addr
)
129 assert(seastar::this_shard_id() == msgr_sid
);
130 protocol
->start_accept(std::move(sock
), _peer_addr
);
134 SocketConnection::close_clean_yielded()
136 assert(seastar::this_shard_id() == msgr_sid
);
137 return protocol
->close_clean_yielded();
140 seastar::socket_address
SocketConnection::get_local_address() const {
141 assert(seastar::this_shard_id() == msgr_sid
);
142 return socket
->get_local_address();
146 SocketConnection::get_local_shared_foreign_from_this()
148 assert(seastar::this_shard_id() == msgr_sid
);
149 return make_local_shared_foreign(
150 seastar::make_foreign(shared_from_this()));
154 SocketConnection::get_messenger() const
156 assert(seastar::this_shard_id() == msgr_sid
);
161 SocketConnection::get_messenger_shard_id() const
166 void SocketConnection::set_peer_type(entity_type_t peer_type
) {
167 assert(seastar::this_shard_id() == msgr_sid
);
168 // it is not allowed to assign an unknown value when the current
170 assert(!(peer_type
== 0 &&
171 peer_name
.type() != 0));
172 // it is not allowed to assign a different known value when the
173 // current value is also known.
174 assert(!(peer_type
!= 0 &&
175 peer_name
.type() != 0 &&
176 peer_type
!= peer_name
.type()));
177 peer_name
._type
= peer_type
;
180 void SocketConnection::set_peer_id(int64_t peer_id
) {
181 assert(seastar::this_shard_id() == msgr_sid
);
182 // it is not allowed to assign an unknown value when the current
184 assert(!(peer_id
== entity_name_t::NEW
&&
185 peer_name
.num() != entity_name_t::NEW
));
186 // it is not allowed to assign a different known value when the
187 // current value is also known.
188 assert(!(peer_id
!= entity_name_t::NEW
&&
189 peer_name
.num() != entity_name_t::NEW
&&
190 peer_id
!= peer_name
.num()));
191 peer_name
._num
= peer_id
;
194 void SocketConnection::set_features(uint64_t f
) {
195 assert(seastar::this_shard_id() == msgr_sid
);
199 void SocketConnection::set_socket(Socket
*s
) {
200 assert(seastar::this_shard_id() == msgr_sid
);
204 void SocketConnection::print(ostream
& out
) const {
205 out
<< (void*)this << " ";
206 messenger
.print(out
);
207 if (seastar::this_shard_id() != msgr_sid
) {
208 out
<< " >> " << get_peer_name() << " " << peer_addr
;
209 } else if (!socket
) {
210 out
<< " >> " << get_peer_name() << " " << peer_addr
;
211 } else if (socket
->get_side() == Socket::side_t::acceptor
) {
212 out
<< " >> " << get_peer_name() << " " << peer_addr
213 << "@" << socket
->get_ephemeral_port();
214 } else { // socket->get_side() == Socket::side_t::connector
215 out
<< "@" << socket
->get_ephemeral_port()
216 << " >> " << get_peer_name() << " " << peer_addr
;
220 } // namespace crimson::net