25#include <unordered_map>
32#include <system_error>
33#include <gnutls/crypto.h>
35#include <seastar/core/shared_ptr.hh>
36#include <seastar/core/queue.hh>
37#include <seastar/core/semaphore.hh>
38#include <seastar/core/byteorder.hh>
40#include <seastar/net/net.hh>
41#include <seastar/net/ip_checksum.hh>
42#include <seastar/net/ip.hh>
43#include <seastar/net/const.hh>
44#include <seastar/net/packet-util.hh>
45#include <seastar/util/assert.hh>
46#include <seastar/util/std-compat.hh>
50using namespace std::chrono_literals;
56inline auto tcp_error(
int err) {
57 return std::system_error(err, std::system_category());
60inline auto tcp_reset_error() {
61 return tcp_error(ECONNRESET);
64inline auto tcp_connect_error() {
65 return tcp_error(ECONNABORTED);
68inline auto tcp_refused_error() {
69 return tcp_error(ECONNREFUSED);
72enum class tcp_state : uint16_t {
76 SYN_RECEIVED = (1 << 3),
77 ESTABLISHED = (1 << 4),
78 FIN_WAIT_1 = (1 << 5),
79 FIN_WAIT_2 = (1 << 6),
80 CLOSE_WAIT = (1 << 7),
86inline tcp_state operator|(tcp_state s1, tcp_state s2) {
87 return tcp_state(uint16_t(s1) | uint16_t(s2));
90template <
typename... Args>
91void tcp_debug(
const char* fmt, Args&&... args) {
93 print(fmt, std::forward<Args>(args)...);
101 static void write(
char* p, option_kind kind, option_len len) {
102 p[0] =
static_cast<uint8_t
>(kind);
103 if (
static_cast<uint8_t
>(len) > 1) {
104 p[1] =
static_cast<uint8_t
>(len);
108 static constexpr option_kind kind = option_kind::mss;
109 static constexpr option_len len = option_len::mss;
113 x.mss = read_be<uint16_t>(p + 2);
116 void write(
char* p)
const {
117 tcp_option::write(p, kind, len);
118 write_be<uint16_t>(p + 2,
mss);
122 static constexpr option_kind kind = option_kind::win_scale;
123 static constexpr option_len len = option_len::win_scale;
130 void write(
char* p)
const {
131 tcp_option::write(p, kind, len);
136 static constexpr option_kind kind = option_kind::sack;
137 static constexpr option_len len = option_len::sack;
141 void write(
char* p)
const {
142 tcp_option::write(p, kind, len);
146 static constexpr option_kind kind = option_kind::timestamps;
147 static constexpr option_len len = option_len::timestamps;
152 ts.t1 = read_be<uint32_t>(p + 2);
153 ts.t2 = read_be<uint32_t>(p + 6);
156 void write(
char* p)
const {
157 tcp_option::write(p, kind, len);
158 write_be<uint32_t>(p + 2, t1);
159 write_be<uint32_t>(p + 6, t2);
163 static constexpr option_kind kind = option_kind::nop;
164 static constexpr option_len len = option_len::nop;
165 void write(
char* p)
const {
166 tcp_option::write(p, kind, len);
170 static constexpr option_kind kind = option_kind::eol;
171 static constexpr option_len len = option_len::eol;
172 void write(
char* p)
const {
173 tcp_option::write(p, kind, len);
176 static const uint8_t align = 4;
178 void parse(uint8_t* beg, uint8_t* end);
179 uint8_t fill(
void* h,
const tcp_hdr* th, uint8_t option_size);
180 uint8_t get_size(
bool syn_on,
bool ack_on);
183 bool _mss_received =
false;
184 bool _win_scale_received =
false;
185 bool _timestamps_received =
false;
186 bool _sack_received =
false;
189 uint16_t _remote_mss = 536;
191 uint8_t _remote_win_scale = 0;
192 uint8_t _local_win_scale = 0;
194inline char*& operator+=(
char*& x, tcp_option::option_len len) { x += uint8_t(len);
return x; }
195inline const char*& operator+=(
const char*& x, tcp_option::option_len len) { x += uint8_t(len);
return x; }
196inline uint8_t& operator+=(uint8_t& x, tcp_option::option_len len) { x += uint8_t(len);
return x; }
203 return tcp_seq { ntoh(s.raw) };
206inline tcp_seq hton(tcp_seq s) {
207 return tcp_seq { hton(s.raw) };
211std::ostream& operator<<(std::ostream& os, tcp_seq s) {
215inline tcp_seq make_seq(uint32_t raw) {
return tcp_seq{raw}; }
216inline tcp_seq& operator+=(tcp_seq& s, int32_t n) { s.raw += n;
return s; }
217inline tcp_seq& operator-=(tcp_seq& s, int32_t n) { s.raw -= n;
return s; }
218inline tcp_seq operator+(tcp_seq s, int32_t n) {
return s += n; }
219inline tcp_seq operator-(tcp_seq s, int32_t n) {
return s -= n; }
220inline int32_t operator-(tcp_seq s, tcp_seq q) {
return s.raw - q.raw; }
221inline bool operator==(tcp_seq s, tcp_seq q) {
return s.raw == q.raw; }
222inline bool operator!=(tcp_seq s, tcp_seq q) {
return !(s == q); }
223inline bool operator<(tcp_seq s, tcp_seq q) {
return s - q < 0; }
224inline bool operator>(tcp_seq s, tcp_seq q) {
return q < s; }
225inline bool operator<=(tcp_seq s, tcp_seq q) {
return !(s > q); }
226inline bool operator>=(tcp_seq s, tcp_seq q) {
return !(s < q); }
229 static constexpr size_t len = 20;
235 uint8_t data_offset : 4;
246 static tcp_hdr read(
const char* p) {
248 h.src_port = read_be<uint16_t>(p + 0);
249 h.dst_port = read_be<uint16_t>(p + 2);
250 h.seq =
tcp_seq{read_be<uint32_t>(p + 4)};
251 h.ack =
tcp_seq{read_be<uint32_t>(p + 8)};
252 h.rsvd1 = p[12] & 15;
253 h.data_offset = uint8_t(p[12]) >> 4;
254 h.f_fin = (uint8_t(p[13]) >> 0) & 1;
255 h.f_syn = (uint8_t(p[13]) >> 1) & 1;
256 h.f_rst = (uint8_t(p[13]) >> 2) & 1;
257 h.f_psh = (uint8_t(p[13]) >> 3) & 1;
258 h.f_ack = (uint8_t(p[13]) >> 4) & 1;
259 h.f_urg = (uint8_t(p[13]) >> 5) & 1;
260 h.rsvd2 = (uint8_t(p[13]) >> 6) & 3;
261 h.window = read_be<uint16_t>(p + 14);
262 h.checksum = read_be<uint16_t>(p + 16);
263 h.urgent = read_be<uint16_t>(p + 18);
266 void write(
char* p)
const {
267 write_be<uint16_t>(p + 0, src_port);
268 write_be<uint16_t>(p + 2, dst_port);
269 write_be<uint32_t>(p + 4, seq.raw);
270 write_be<uint32_t>(p + 8, ack.raw);
271 p[12] = rsvd1 | (data_offset << 4);
279 write_be<uint16_t>(p + 14, window);
280 write_be<uint16_t>(p + 16, checksum);
281 write_be<uint16_t>(p + 18, urgent);
283 static void write_nbo_checksum(
char* p, uint16_t checksum_in_network_byte_order) {
284 std::copy_n(
reinterpret_cast<const char*
>(&checksum_in_network_byte_order), 2, p + 16);
291template <
typename InetTraits>
294 using ipaddr =
typename InetTraits::address_type;
295 using inet_type =
typename InetTraits::inet_type;
305 static constexpr tcp_state CLOSED = tcp_state::CLOSED;
306 static constexpr tcp_state LISTEN = tcp_state::LISTEN;
307 static constexpr tcp_state SYN_SENT = tcp_state::SYN_SENT;
308 static constexpr tcp_state SYN_RECEIVED = tcp_state::SYN_RECEIVED;
309 static constexpr tcp_state ESTABLISHED = tcp_state::ESTABLISHED;
310 static constexpr tcp_state FIN_WAIT_1 = tcp_state::FIN_WAIT_1;
311 static constexpr tcp_state FIN_WAIT_2 = tcp_state::FIN_WAIT_2;
312 static constexpr tcp_state CLOSE_WAIT = tcp_state::CLOSE_WAIT;
313 static constexpr tcp_state CLOSING = tcp_state::CLOSING;
314 static constexpr tcp_state LAST_ACK = tcp_state::LAST_ACK;
315 static constexpr tcp_state TIME_WAIT = tcp_state::TIME_WAIT;
316 tcp_state _state = CLOSED;
320 std::optional<promise<>> _fin_recvd_promise =
promise<>();
323 uint16_t _local_port;
324 uint16_t _foreign_port;
325 struct unacked_segment {
328 unsigned nr_transmits;
329 clock_type::time_point tx_time;
335 uint8_t window_scale;
341 std::deque<unacked_segment> data;
342 std::deque<packet> unsent;
343 uint32_t unsent_len = 0;
347 std::optional<promise<>> _all_data_acked_promise;
349 size_t max_queue_space = 212992;
350 size_t current_queue_space = 0;
352 std::optional<promise<>> _send_available_promise;
354 std::chrono::milliseconds rttvar;
356 std::chrono::milliseconds srtt;
357 bool first_rto_sample =
true;
358 clock_type::time_point syn_tx_time;
364 uint16_t dupacks = 0;
365 unsigned syn_retransmit = 0;
366 unsigned fin_retransmit = 0;
367 uint32_t limited_transfer = 0;
368 uint32_t partial_ack = 0;
370 bool window_probe =
false;
371 uint8_t zero_window_probing_out = 0;
376 uint8_t window_scale;
380 std::deque<packet> data;
382 size_t data_size = 0;
384 std::optional<promise<>> _data_received_promise;
387 size_t max_receive_buf_size = 3737600;
392 std::chrono::milliseconds _rto{1000};
393 std::chrono::milliseconds _persist_time_out{1000};
394 static constexpr std::chrono::milliseconds _rto_min{1000};
395 static constexpr std::chrono::milliseconds _rto_max{60000};
397 static constexpr std::chrono::milliseconds _rto_clk_granularity{1};
398 static constexpr uint16_t _max_nr_retransmit{5};
401 uint16_t _nr_full_seg_received = 0;
406 std::random_device rd;
407 std::default_random_engine e(rd());
408 std::uniform_int_distribution<uint32_t> dist{};
409 for (
auto& k : key) {
414 static isn_secret _isn_secret;
417 bool _poll_active =
false;
418 uint32_t get_default_receive_window_size() {
420 constexpr uint32_t size = 29200;
421 return size << _rcv.window_scale;
424 uint32_t get_modified_receive_window_size() {
425 uint32_t left = _rcv.data_size > _rcv.max_receive_buf_size ? 0 : _rcv.max_receive_buf_size - _rcv.data_size;
426 return std::min(left, get_default_receive_window_size());
433 void output_one(
bool data_retransmit =
false);
436 void abort_reader()
noexcept;
442 void close()
noexcept;
443 void remove_from_tcbs() {
444 auto id =
connid{_local_ip, _foreign_ip, _local_port, _foreign_port};
445 _tcp._tcbs.erase(
id);
447 std::optional<typename InetTraits::l4packet> get_packet();
452 (void)_tcp.poll_tcb(_foreign_ip, this->shared_from_this()).then_wrapped([
this] (
auto&& f) {
457 _poll_active =
false;
458 this->start_retransmit_timer();
460 if (this->in_state(SYN_SENT)) {
476 void respond_with_reset(
tcp_hdr* th);
477 bool merge_out_of_order();
479 void trim_receive_data_after_window();
480 bool should_send_ack(uint16_t seg_len);
481 void clear_delayed_ack()
noexcept;
482 packet get_transmit_packet();
483 void retransmit_one() {
484 bool data_retransmit =
true;
485 output_one(data_retransmit);
487 void start_retransmit_timer() {
489 start_retransmit_timer(
now);
491 void start_retransmit_timer(clock_type::time_point
now) {
492 auto tp =
now + _rto;
493 _retransmit.
rearm(tp);
495 void stop_retransmit_timer()
noexcept {
498 void start_persist_timer() {
500 start_persist_timer(
now);
502 void start_persist_timer(clock_type::time_point
now) {
503 auto tp =
now + _persist_time_out;
506 void stop_persist_timer() {
511 void fast_retransmit();
512 void update_rto(clock_type::time_point tx_time);
513 void update_cwnd(uint32_t acked_bytes);
515 uint32_t can_send() {
516 if (_snd.window_probe) {
521 if (_snd.window == 0) {
526 auto window_used = uint32_t(_snd.next - _snd.unacknowledged);
527 if (window_used > _snd.window) {
532 auto x = std::min(_snd.window - window_used, _snd.unsent_len);
535 x = std::min(_snd.cwnd, x);
536 if (_snd.dupacks == 1 || _snd.dupacks == 2) {
539 auto flight = flight_size();
540 auto max = _snd.cwnd + 2 * _snd.mss;
541 x = flight <= max ? std::min(x, max - flight) : 0;
542 _snd.limited_transfer += x;
543 }
else if (_snd.dupacks >= 3) {
546 x = std::min(uint32_t(_snd.mss), x);
550 uint32_t flight_size() {
552 std::for_each(_snd.data.begin(), _snd.data.end(), [&] (unacked_segment& seg) { size += seg.p.len(); });
555 uint16_t local_mss() {
556 return _tcp.hw_features().mtu - net::tcp_hdr_len_min - InetTraits::ip_hdr_len_min;
558 void queue_packet(
packet p) {
559 _packetq.emplace_back(
typename InetTraits::l4packet{_foreign_ip, std::move(p)});
561 void signal_data_received() {
562 if (_rcv._data_received_promise) {
563 _rcv._data_received_promise->set_value();
564 _rcv._data_received_promise = {};
567 void signal_all_data_acked() {
568 if (_snd._all_data_acked_promise && _snd.unsent_len == 0) {
569 _snd._all_data_acked_promise->set_value();
570 _snd._all_data_acked_promise = {};
573 void signal_send_available() {
574 if (_snd._send_available_promise && _snd.max_queue_space > _snd.current_queue_space) {
575 _snd._send_available_promise->set_value();
576 _snd._send_available_promise = {};
585 void do_syn_received() {
586 _state = SYN_RECEIVED;
591 void do_established() {
592 _state = ESTABLISHED;
593 update_rto(_snd.syn_tx_time);
599 if (_rcv._data_received_promise) {
600 _rcv._data_received_promise->set_exception(tcp_reset_error());
601 _rcv._data_received_promise = std::nullopt;
603 if (_snd._all_data_acked_promise) {
604 _snd._all_data_acked_promise->set_exception(tcp_reset_error());
605 _snd._all_data_acked_promise = std::nullopt;
607 if (_snd._send_available_promise) {
608 _snd._send_available_promise->set_exception(tcp_reset_error());
609 _snd._send_available_promise = std::nullopt;
612 void do_time_wait() {
621 void do_setup_isn() {
622 _snd.initial = get_isn();
623 _snd.unacknowledged = _snd.initial;
624 _snd.next = _snd.initial + 1;
625 _snd.recover = _snd.initial;
627 void do_local_fin_acked() {
628 _snd.unacknowledged += 1;
631 bool syn_needs_on()
const noexcept {
632 return in_state(SYN_SENT | SYN_RECEIVED);
634 bool fin_needs_on()
const noexcept {
635 return in_state(FIN_WAIT_1 | CLOSING | LAST_ACK) && _snd.closed &&
636 _snd.unsent_len == 0;
638 bool ack_needs_on()
const noexcept {
639 return !in_state(CLOSED | LISTEN | SYN_SENT);
641 bool foreign_will_not_send()
const noexcept {
642 return in_state(CLOSING | TIME_WAIT | CLOSE_WAIT | LAST_ACK | CLOSED);
644 bool in_state(tcp_state state)
const noexcept {
645 return uint16_t(_state) & uint16_t(state);
647 void exit_fast_recovery() {
649 _snd.limited_transfer = 0;
650 _snd.partial_ack = 0;
652 uint32_t data_segment_acked(
tcp_seq seg_ack);
653 bool segment_acceptable(
tcp_seq seg_seq,
unsigned seg_len);
654 void init_from_options(
tcp_hdr* th, uint8_t* opt_start, uint8_t* opt_end);
658 std::unordered_map<connid, lw_shared_ptr<tcb>, connid_hash> _tcbs;
659 std::unordered_map<uint16_t, listener*> _listening;
660 std::random_device _rd;
661 std::default_random_engine _e;
662 std::uniform_int_distribution<uint16_t> _port_dist{41952, 65535};
669 const inet_type& inet()
const {
690 return _tcb->connect_done();
693 return _tcb->send(std::move(p));
696 return _tcb->wait_for_data();
699 return _tcb->wait_input_shutdown();
704 ipaddr foreign_ip() {
705 return _tcb->_foreign_ip;
707 uint16_t foreign_port() {
708 return _tcb->_foreign_port;
711 return _tcb->_local_ip;
713 uint16_t local_port() {
714 return _tcb->_local_port;
716 void shutdown_connect();
717 void close_read()
noexcept;
718 void close_write()
noexcept;
726 listener(
tcp& t, uint16_t port,
size_t queue_length)
727 : _tcp(t), _port(port), _q(queue_length) {
728 _tcp._listening.emplace(_port,
this);
732 : _tcp(x._tcp), _port(x._port), _q(std::move(x._q)) {
733 _tcp._listening[_port] =
this;
738 _tcp._listening.erase(_port);
744 void abort_accept() {
745 _q.
abort(std::make_exception_ptr(std::system_error(ECONNABORTED, std::system_category())));
747 bool full() {
return _pending + _q.
size() >= _q.
max_size(); }
748 void inc_pending() { _pending++; }
749 void dec_pending() { _pending--; }
751 const tcp& get_tcp()
const {
754 uint16_t port()
const {
760 explicit tcp(inet_type& inet);
761 void received(
packet p, ipaddr from, ipaddr to);
768 auto it = _listening.find(local_port);
769 if (it != _listening.end()) {
770 it->second->_q.push(connection(tcbp));
771 it->second->dec_pending();
775 void send_packet_without_tcb(ipaddr from, ipaddr to, packet p);
776 void respond_with_reset(tcp_hdr* rth, ipaddr local_ip, ipaddr foreign_ip);
777 friend class listener;
780template <
typename InetTraits>
781tcp<InetTraits>::tcp(inet_type& inet)
784 namespace sm = metrics;
787 sm::make_counter(
"linearizations", [] {
return tcp_packet_merger::linearizations(); },
788 sm::description(
"Counts a number of times a buffer linearization was invoked during the buffers merge process. "
789 "Divide it by a total TCP receive packet rate to get an everage number of lineraizations per TCP packet."))
792 _inet.register_packet_provider([
this, tcb_polled = 0u] ()
mutable {
793 std::optional<typename InetTraits::l4packet> l4p;
794 auto c = _poll_tcbs.size();
795 if (!_packetq.empty() && (!(tcb_polled % 128) || c == 0)) {
796 l4p = std::move(_packetq.front());
797 _packetq.pop_front();
798 _queue_space.
signal(l4p.value().p.len());
802 lw_shared_ptr<tcb> tcb;
803 ethernet_address dst;
804 std::tie(tcb, dst) = std::move(_poll_tcbs.front());
805 _poll_tcbs.pop_front();
806 l4p = tcb->get_packet();
808 l4p.value().e_dst = dst;
817template <
typename InetTraits>
818future<> tcp<InetTraits>::poll_tcb(ipaddr to, lw_shared_ptr<tcb> tcb) {
819 return _inet.get_l2_dst_address(to).then([
this, tcb = std::move(tcb)] (ethernet_address dst) {
820 _poll_tcbs.emplace_back(std::move(tcb), dst);
824template <
typename InetTraits>
826 return listener(*
this, port, queue_length);
829template <
typename InetTraits>
832 auto src_ip = _inet._inet.host_address();
833 auto dst_ip = ipv4_address(sa);
834 auto dst_port = net::ntoh(sa.u.in.sin_port);
836 if (smp::count > 1) {
838 id = connid{src_ip, dst_ip, _port_dist(_e), dst_port};
839 }
while (_inet._inet.netif()->hash2cpu(
id.hash(_inet._inet.netif()->rss_key())) !=
this_shard_id()
840 || _tcbs.find(
id) != _tcbs.end());
842 id = connid{src_ip, dst_ip, _port_dist(_e), dst_port};
845 auto tcbp = make_lw_shared<tcb>(*
this,
id);
846 _tcbs.insert({id, tcbp});
848 return connection(tcbp);
851template <
typename InetTraits>
852bool tcp<InetTraits>::forward(forward_hash& out_hash_data, packet& p,
size_t off) {
853 auto th = p.get_header(off, tcp_hdr::len);
856 out_hash_data.push_back(uint8_t(th[0]));
857 out_hash_data.push_back(uint8_t(th[1]));
858 out_hash_data.push_back(uint8_t(th[2]));
859 out_hash_data.push_back(uint8_t(th[3]));
864template <
typename InetTraits>
865void tcp<InetTraits>::received(packet p, ipaddr from, ipaddr to) {
866 auto th = p.get_header(0, tcp_hdr::len);
871 auto data_offset = uint8_t(th[12]) >> 4;
872 if (
size_t(data_offset * 4) < tcp_hdr::len) {
876 if (!hw_features().rx_csum_offload) {
878 InetTraits::tcp_pseudo_header_checksum(csum, from, to, p.len());
880 if (csum.get() != 0) {
884 auto h = tcp_hdr::read(th);
885 auto id = connid{to, from, h.dst_port, h.src_port};
886 auto tcbi = _tcbs.find(
id);
887 lw_shared_ptr<tcb> tcbp;
888 if (tcbi == _tcbs.end()) {
889 auto listener = _listening.find(
id.local_port);
890 if (listener == _listening.end() || listener->second->full()) {
898 return respond_with_reset(&h,
id.local_ip,
id.foreign_ip);
911 return respond_with_reset(&h,
id.local_ip,
id.foreign_ip);
917 tcbp = make_lw_shared<tcb>(*
this,
id);
918 _tcbs.insert({id, tcbp});
921 listener->second->inc_pending();
923 return tcbp->input_handle_listen_state(&h, std::move(p));
932 if (tcbp->state() == tcp_state::SYN_SENT) {
934 return tcbp->input_handle_syn_sent_state(&h, std::move(p));
939 return tcbp->input_handle_other_state(&h, std::move(p));
945template <
typename InetTraits>
946void tcp<InetTraits>::send_packet_without_tcb(ipaddr from, ipaddr to, packet p) {
947 if (_queue_space.
try_wait(p.len())) {
949 (void)_inet.get_l2_dst_address(to).then([
this, to, p = std::move(p)] (ethernet_address e_dst)
mutable {
950 _packetq.emplace_back(ipv4_traits::l4packet{to, std::move(p), e_dst, ip_protocol_num::tcp});
955template <
typename InetTraits>
956tcp<InetTraits>::connection::~connection() {
958 _tcb->_conn =
nullptr;
964template <
typename InetTraits>
965tcp<InetTraits>::tcb::tcb(tcp& t, connid
id)
967 , _local_ip(id.local_ip)
968 , _foreign_ip(id.foreign_ip)
969 , _local_port(id.local_port)
970 , _foreign_port(id.foreign_port)
971 , _delayed_ack([this] { _nr_full_seg_received = 0; output(); })
972 , _retransmit([
this] { retransmit(); })
973 , _persist([
this] { persist(); }) {
976template <
typename InetTraits>
977void tcp<InetTraits>::tcb::respond_with_reset(tcp_hdr* rth) {
978 _tcp.respond_with_reset(rth, _local_ip, _foreign_ip);
981template <
typename InetTraits>
982void tcp<InetTraits>::respond_with_reset(tcp_hdr* rth, ipaddr local_ip, ipaddr foreign_ip) {
987 auto th = p.prepend_uninitialized_header(tcp_hdr::len);
989 h.src_port = rth->dst_port;
990 h.dst_port = rth->src_port;
996 h.ack = rth->seq + 1;
1000 h.data_offset = tcp_hdr::len / 4;
1006 InetTraits::tcp_pseudo_header_checksum(csum, local_ip, foreign_ip, tcp_hdr::len);
1008 if (hw_features().tx_csum_l4_offload) {
1009 checksum = ~csum.get();
1010 oi.needs_csum =
true;
1013 checksum = csum.get();
1014 oi.needs_csum =
false;
1016 tcp_hdr::write_nbo_checksum(th, checksum);
1018 oi.protocol = ip_protocol_num::tcp;
1019 oi.tcp_hdr_len = tcp_hdr::len;
1020 p.set_offload_info(oi);
1022 send_packet_without_tcb(local_ip, foreign_ip, std::move(p));
1025template <
typename InetTraits>
1026uint32_t tcp<InetTraits>::tcb::data_segment_acked(tcp_seq seg_ack) {
1027 uint32_t total_acked_bytes = 0;
1029 while (!_snd.data.empty()
1030 && (_snd.unacknowledged + _snd.data.front().p.len() <= seg_ack)) {
1031 auto acked_bytes = _snd.data.front().p.len();
1032 _snd.unacknowledged += acked_bytes;
1034 if (_snd.data.front().nr_transmits == 0) {
1035 update_rto(_snd.data.front().tx_time);
1037 update_cwnd(acked_bytes);
1038 total_acked_bytes += acked_bytes;
1039 _snd.current_queue_space -= _snd.data.front().data_len;
1040 signal_send_available();
1041 _snd.data.pop_front();
1044 if (_snd.unacknowledged < seg_ack) {
1045 auto acked_bytes = seg_ack - _snd.unacknowledged;
1046 if (!_snd.data.empty()) {
1047 auto& unacked_seg = _snd.data.front();
1048 unacked_seg.p.trim_front(acked_bytes);
1050 _snd.unacknowledged = seg_ack;
1051 update_cwnd(acked_bytes);
1052 total_acked_bytes += acked_bytes;
1054 return total_acked_bytes;
1057template <
typename InetTraits>
1058bool tcp<InetTraits>::tcb::segment_acceptable(tcp_seq seg_seq,
unsigned seg_len) {
1059 if (seg_len == 0 && _rcv.window == 0) {
1061 return seg_seq == _rcv.next;
1062 }
else if (seg_len == 0 && _rcv.window > 0) {
1064 return (_rcv.next <= seg_seq) && (seg_seq < _rcv.next + _rcv.window);
1065 }
else if (seg_len > 0 && _rcv.window > 0) {
1069 bool x = (_rcv.next <= seg_seq) && seg_seq < (_rcv.next + _rcv.window);
1070 bool y = (_rcv.next <= seg_seq + seg_len - 1) && (seg_seq + seg_len - 1 < _rcv.next + _rcv.window);
1078template <
typename InetTraits>
1079void tcp<InetTraits>::tcb::init_from_options(tcp_hdr* th, uint8_t* opt_start, uint8_t* opt_end) {
1081 _option.parse(opt_start, opt_end);
1084 _snd.window_scale = _option._remote_win_scale;
1086 _rcv.window_scale = _option._local_win_scale;
1089 _snd.mss = _option._remote_mss;
1091 _rcv.mss = _option._local_mss = local_mss();
1093 _rcv.window = get_default_receive_window_size();
1094 _snd.window = th->window << _snd.window_scale;
1102 if (2190 < _snd.mss) {
1103 _snd.cwnd = 2 * _snd.mss;
1104 }
else if (1095 < _snd.mss && _snd.mss <= 2190) {
1105 _snd.cwnd = 3 * _snd.mss;
1107 _snd.cwnd = 4 * _snd.mss;
1111 _snd.ssthresh = th->window << _snd.window_scale;
1114template <
typename InetTraits>
1115void tcp<InetTraits>::tcb::input_handle_listen_state(tcp_hdr* th, packet p) {
1116 auto opt_len = th->data_offset * 4 - tcp_hdr::len;
1117 auto opt_start =
reinterpret_cast<uint8_t*
>(p.get_header(0, th->data_offset * 4)) + tcp_hdr::len;
1118 auto opt_end = opt_start + opt_len;
1119 p.trim_front(th->data_offset * 4);
1120 tcp_seq seg_seq = th->seq;
1123 _rcv.next = seg_seq + 1;
1124 _rcv.initial = seg_seq;
1136 _rcv.urgent = _rcv.next;
1138 tcp_debug(
"listen: LISTEN -> SYN_RECEIVED\n");
1139 init_from_options(th, opt_start, opt_end);
1143template <
typename InetTraits>
1144void tcp<InetTraits>::tcb::input_handle_syn_sent_state(tcp_hdr* th, packet p) {
1145 auto opt_len = th->data_offset * 4 - tcp_hdr::len;
1146 auto opt_start =
reinterpret_cast<uint8_t*
>(p.get_header(0, th->data_offset * 4)) + tcp_hdr::len;
1147 auto opt_end = opt_start + opt_len;
1148 p.trim_front(th->data_offset * 4);
1149 tcp_seq seg_seq = th->seq;
1150 auto seg_ack = th->ack;
1152 bool acceptable =
false;
1157 if (seg_ack <= _snd.initial || seg_ack > _snd.next) {
1158 return respond_with_reset(th);
1162 acceptable = _snd.unacknowledged <= seg_ack && seg_ack <= _snd.next;
1171 _connect_done.set_exception(tcp_refused_error());
1187 _rcv.next = seg_seq + 1;
1188 _rcv.initial = seg_seq;
1191 _snd.unacknowledged = seg_ack;
1193 if (_snd.unacknowledged > _snd.initial) {
1197 tcp_debug(
"syn: SYN_SENT -> ESTABLISHED\n");
1198 init_from_options(th, opt_start, opt_end);
1204 tcp_debug(
"syn: SYN_SENT -> SYN_RECEIVED\n");
1214template <
typename InetTraits>
1215void tcp<InetTraits>::tcb::input_handle_other_state(tcp_hdr* th, packet p) {
1216 p.trim_front(th->data_offset * 4);
1217 bool do_output =
false;
1218 bool do_output_data =
false;
1219 tcp_seq seg_seq = th->seq;
1220 auto seg_ack = th->ack;
1221 auto seg_len = p.len();
1224 if (!segment_acceptable(seg_seq, seg_len)) {
1231 if (seg_seq < _rcv.next) {
1233 auto dup = std::min(uint32_t(_rcv.next - seg_seq), seg_len);
1240 if (seg_seq != _rcv.next) {
1241 insert_out_of_order(seg_seq, std::move(p));
1249 if (in_state(SYN_RECEIVED)) {
1259 _connect_done.set_exception(tcp_refused_error());
1262 if (in_state(ESTABLISHED | FIN_WAIT_1 | FIN_WAIT_2 | CLOSE_WAIT)) {
1270 if (in_state(CLOSING | LAST_ACK | TIME_WAIT)) {
1290 respond_with_reset(th);
1304 if (in_state(SYN_RECEIVED)) {
1307 if (_snd.unacknowledged <= seg_ack && seg_ack <= _snd.next) {
1308 tcp_debug(
"SYN_RECEIVED -> ESTABLISHED\n");
1310 _tcp.add_connected_tcb(this->shared_from_this(), _local_port);
1313 return respond_with_reset(th);
1316 auto update_window = [
this, th, seg_seq, seg_ack] {
1317 tcp_debug(
"window update seg_seq=%d, seg_ack=%d, old window=%d new window=%d\n",
1318 seg_seq, seg_ack, _snd.window, th->window << _snd.window_scale);
1319 _snd.window = th->window << _snd.window_scale;
1322 _snd.zero_window_probing_out = 0;
1323 if (_snd.window == 0) {
1324 _persist_time_out = _rto;
1325 start_persist_timer();
1327 stop_persist_timer();
1332 if (in_state(ESTABLISHED | CLOSE_WAIT)){
1334 auto packets_out = _snd.next - _snd.unacknowledged - _snd.zero_window_probing_out;
1336 if (_snd.unacknowledged < seg_ack && seg_ack <= _snd.next) {
1338 auto acked_bytes = data_segment_acked(seg_ack);
1341 if (_snd.wl1 < seg_seq || (_snd.wl1 == seg_seq && _snd.wl2 <= seg_ack)) {
1346 do_output_data =
true;
1348 auto set_retransmit_timer = [
this] {
1349 if (_snd.data.empty()) {
1351 stop_retransmit_timer();
1353 signal_all_data_acked();
1356 start_retransmit_timer();
1360 if (_snd.dupacks >= 3) {
1362 uint32_t smss = _snd.mss;
1363 if (seg_ack > _snd.recover) {
1364 tcp_debug(
"ack: full_ack\n");
1366 _snd.cwnd = std::min(_snd.ssthresh, std::max(flight_size(), smss) + smss);
1368 exit_fast_recovery();
1369 set_retransmit_timer();
1371 tcp_debug(
"ack: partial_ack\n");
1376 _snd.cwnd -= acked_bytes;
1379 if (acked_bytes >= smss) {
1386 if (++_snd.partial_ack == 1) {
1387 start_retransmit_timer();
1398 exit_fast_recovery();
1399 set_retransmit_timer();
1401 }
else if ((packets_out > 0) && !_snd.data.empty() && seg_len == 0 &&
1402 th->f_fin == 0 && th->f_syn == 0 &&
1403 th->ack == _snd.unacknowledged &&
1404 uint32_t(th->window << _snd.window_scale) == _snd.window) {
1413 uint32_t smss = _snd.mss;
1415 if (_snd.dupacks == 1 || _snd.dupacks == 2) {
1418 do_output_data =
true;
1419 }
else if (_snd.dupacks == 3) {
1421 if (seg_ack - 1 > _snd.recover) {
1422 _snd.recover = _snd.next - 1;
1424 _snd.ssthresh = std::max((flight_size() - _snd.limited_transfer) / 2, 2 * smss);
1430 _snd.cwnd = _snd.ssthresh + 3 * smss;
1431 }
else if (_snd.dupacks > 3) {
1435 do_output_data =
true;
1437 }
else if (seg_ack > _snd.next) {
1441 }
else if (_snd.window == 0 && th->window > 0) {
1443 do_output_data =
true;
1447 if (in_state(FIN_WAIT_1)) {
1451 if (seg_ack == _snd.next + 1) {
1452 tcp_debug(
"ack: FIN_WAIT_1 -> FIN_WAIT_2\n");
1453 _state = FIN_WAIT_2;
1454 do_local_fin_acked();
1458 if (in_state(FIN_WAIT_2)) {
1465 if (in_state(CLOSING)) {
1466 if (seg_ack == _snd.next + 1) {
1467 tcp_debug(
"ack: CLOSING -> TIME_WAIT\n");
1468 do_local_fin_acked();
1469 return do_time_wait();
1475 if (in_state(LAST_ACK)) {
1476 if (seg_ack == _snd.next + 1) {
1477 tcp_debug(
"ack: LAST_ACK -> CLOSED\n");
1478 do_local_fin_acked();
1483 if (in_state(TIME_WAIT)) {
1497 if (in_state(ESTABLISHED | FIN_WAIT_1 | FIN_WAIT_2)) {
1503 _rcv.data_size += p.len();
1504 _rcv.data.push_back(std::move(p));
1505 _rcv.next += seg_len;
1506 auto merged = merge_out_of_order();
1507 _rcv.window = get_modified_receive_window_size();
1508 signal_data_received();
1519 do_output = should_send_ack(seg_len);
1522 }
else if (in_state(CLOSE_WAIT | CLOSING | LAST_ACK | TIME_WAIT)) {
1530 if (_fin_recvd_promise) {
1531 _fin_recvd_promise->set_value();
1532 _fin_recvd_promise.reset();
1534 if (in_state(CLOSED | LISTEN | SYN_SENT)) {
1539 auto fin_seq = seg_seq + seg_len;
1540 if (fin_seq == _rcv.next) {
1541 _rcv.next = fin_seq + 1;
1542 signal_data_received();
1546 clear_delayed_ack();
1551 if (in_state(SYN_RECEIVED | ESTABLISHED)) {
1552 tcp_debug(
"fin: SYN_RECEIVED or ESTABLISHED -> CLOSE_WAIT\n");
1553 _state = CLOSE_WAIT;
1555 if (in_state(FIN_WAIT_1)) {
1561 tcp_debug(
"fin: FIN_WAIT_1 -> CLOSING\n");
1564 if (in_state(FIN_WAIT_2)) {
1565 tcp_debug(
"fin: FIN_WAIT_2 -> TIME_WAIT\n");
1566 return do_time_wait();
1570 if (do_output || (do_output_data && can_send())) {
1572 clear_delayed_ack();
1577template <
typename InetTraits>
1578packet tcp<InetTraits>::tcb::get_transmit_packet() {
1580 if (_snd.unsent.empty()) {
1583 auto can_send = this->can_send();
1586 if (_tcp.hw_features().tx_tso) {
1588 len = _tcp.hw_features().max_packet_len - net::tcp_hdr_len_min - InetTraits::ip_hdr_len_min;
1590 len = std::min(uint16_t(_tcp.hw_features().mtu - net::tcp_hdr_len_min - InetTraits::ip_hdr_len_min), _snd.mss);
1592 can_send = std::min(can_send, len);
1594 if (_snd.unsent.size() == 1 && _snd.unsent.front().len() <= can_send) {
1595 auto p = std::move(_snd.unsent.front());
1596 _snd.unsent.pop_front();
1597 _snd.unsent_len -= p.len();
1601 if (_snd.unsent.front().len() > can_send) {
1602 auto p = _snd.unsent.front().share(0, can_send);
1603 _snd.unsent.front().trim_front(can_send);
1604 _snd.unsent_len -= p.len();
1608 auto p = std::move(_snd.unsent.front());
1609 _snd.unsent.pop_front();
1610 can_send -= p.len();
1611 while (!_snd.unsent.empty()
1612 && _snd.unsent.front().len() <= can_send) {
1613 can_send -= _snd.unsent.front().len();
1614 p.append(std::move(_snd.unsent.front()));
1615 _snd.unsent.pop_front();
1617 if (!_snd.unsent.empty() && can_send) {
1618 auto& q = _snd.unsent.front();
1619 p.append(q.share(0, can_send));
1620 q.trim_front(can_send);
1622 _snd.unsent_len -= p.len();
1626template <
typename InetTraits>
1627void tcp<InetTraits>::tcb::output_one(
bool data_retransmit) {
1628 if (in_state(CLOSED)) {
1632 packet p = data_retransmit ? _snd.data.front().p.share() : get_transmit_packet();
1633 packet clone = p.share();
1634 uint16_t len = p.len();
1635 bool syn_on = syn_needs_on();
1636 bool ack_on = ack_needs_on();
1638 auto options_size = _option.get_size(syn_on, ack_on);
1639 auto th = p.prepend_uninitialized_header(tcp_hdr::len + options_size);
1642 h.src_port = _local_port;
1643 h.dst_port = _foreign_port;
1648 clear_delayed_ack();
1654 if (data_retransmit) {
1655 seq = _snd.unacknowledged;
1657 seq = syn_on ? _snd.initial : _snd.next;
1662 h.data_offset = (tcp_hdr::len + options_size) / 4;
1663 h.window = _rcv.window >> _rcv.window_scale;
1667 bool fin_on = fin_needs_on();
1671 _option.fill(th, &h, options_size);
1676 uint16_t pseudo_hdr_seg_len = 0;
1678 oi.tcp_hdr_len = tcp_hdr::len + options_size;
1680 if (_tcp.hw_features().tx_csum_l4_offload) {
1681 oi.needs_csum =
true;
1692 if (_tcp.hw_features().tx_tso && len > _snd.mss) {
1693 oi.tso_seg_size = _snd.mss;
1695 pseudo_hdr_seg_len = tcp_hdr::len + options_size + len;
1698 pseudo_hdr_seg_len = tcp_hdr::len + options_size + len;
1699 oi.needs_csum =
false;
1702 InetTraits::tcp_pseudo_header_checksum(csum, _local_ip, _foreign_ip,
1703 pseudo_hdr_seg_len);
1706 if (_tcp.hw_features().tx_csum_l4_offload) {
1707 checksum = ~csum.get();
1710 checksum = csum.get();
1712 tcp_hdr::write_nbo_checksum(th, checksum);
1714 oi.protocol = ip_protocol_num::tcp;
1716 p.set_offload_info(oi);
1718 if (!data_retransmit && (len || syn_on || fin_on)) {
1721 unsigned nr_transmits = 0;
1722 _snd.data.emplace_back(unacked_segment{std::move(clone),
1723 len, nr_transmits,
now});
1725 if (!_retransmit.armed()) {
1726 start_retransmit_timer(
now);
1734 SEASTAR_ASSERT((_snd.window > 0) || ((_snd.window == 0) && (len <= 1)));
1735 queue_packet(std::move(p));
1738template <
typename InetTraits>
1739future<> tcp<InetTraits>::tcb::wait_for_data() {
1740 if (!_rcv.data.empty() || foreign_will_not_send()) {
1741 return make_ready_future<>();
1743 _rcv._data_received_promise =
promise<>();
1744 return _rcv._data_received_promise->get_future();
1747template <
typename InetTraits>
1748future<> tcp<InetTraits>::tcb::wait_input_shutdown() {
1749 if (!_fin_recvd_promise) {
1750 return make_ready_future<>();
1752 return _fin_recvd_promise->get_future();
1755template <
typename InetTraits>
1757tcp<InetTraits>::tcb::abort_reader() noexcept {
1758 if (_rcv._data_received_promise) {
1759 _rcv._data_received_promise->set_exception(
1760 std::make_exception_ptr(std::system_error(ECONNABORTED, std::system_category())));
1761 _rcv._data_received_promise = std::nullopt;
1763 if (_fin_recvd_promise) {
1764 _fin_recvd_promise->set_value();
1765 _fin_recvd_promise.reset();
1769template <
typename InetTraits>
1770future<> tcp<InetTraits>::tcb::wait_for_all_data_acked() {
1771 if (_snd.data.empty() && _snd.unsent_len == 0) {
1772 return make_ready_future<>();
1774 _snd._all_data_acked_promise =
promise<>();
1775 return _snd._all_data_acked_promise->get_future();
1778template <
typename InetTraits>
1786 _rcv.window_scale = _option._local_win_scale = 7;
1788 _rcv.mss = _option._local_mss = local_mss();
1789 _rcv.window = get_default_receive_window_size();
1794template <
typename InetTraits>
1795packet tcp<InetTraits>::tcb::read() {
1797 for (
auto&& q : _rcv.data) {
1798 p.append(std::move(q));
1802 _rcv.window = get_default_receive_window_size();
1806template <
typename InetTraits>
1807future<> tcp<InetTraits>::tcb::wait_send_available() {
1808 if (_snd.max_queue_space > _snd.current_queue_space) {
1809 return make_ready_future<>();
1811 _snd._send_available_promise =
promise<>();
1812 return _snd._send_available_promise->get_future();
1815template <
typename InetTraits>
1816future<> tcp<InetTraits>::tcb::send(packet p) {
1818 if (_snd.closed || in_state(CLOSED)) {
1819 return make_exception_future<>(tcp_reset_error());
1823 _snd.current_queue_space += len;
1824 _snd.unsent_len += len;
1825 _snd.unsent.push_back(std::move(p));
1827 if (can_send() > 0) {
1831 return wait_send_available();
1834template <
typename InetTraits>
1835void tcp<InetTraits>::tcb::close() noexcept {
1836 if (in_state(CLOSED) || _snd.closed) {
1840 (void)wait_for_all_data_acked().then([
this, zis = this->shared_from_this()] ()
mutable {
1842 tcp_debug(
"close: unsent_len=%d\n", _snd.unsent_len);
1843 if (in_state(CLOSE_WAIT)) {
1844 tcp_debug(
"close: CLOSE_WAIT -> LAST_ACK\n");
1846 }
else if (in_state(ESTABLISHED)) {
1847 tcp_debug(
"close: ESTABLISHED -> FIN_WAIT_1\n");
1848 _state = FIN_WAIT_1;
1859template <
typename InetTraits>
1860bool tcp<InetTraits>::tcb::should_send_ack(uint16_t seg_len) {
1862 if (seg_len > _rcv.mss) {
1863 _nr_full_seg_received = 0;
1864 _delayed_ack.cancel();
1869 if (seg_len == _rcv.mss) {
1870 if (_nr_full_seg_received++ >= 1) {
1871 _nr_full_seg_received = 0;
1872 _delayed_ack.cancel();
1878 if (_delayed_ack.armed()) {
1885 _delayed_ack.arm(200ms);
1889template <
typename InetTraits>
1890void tcp<InetTraits>::tcb::clear_delayed_ack() noexcept {
1891 _delayed_ack.cancel();
1894template <
typename InetTraits>
1895bool tcp<InetTraits>::tcb::merge_out_of_order() {
1896 bool merged =
false;
1897 if (_rcv.out_of_order.map.empty()) {
1900 for (
auto it = _rcv.out_of_order.map.begin(); it != _rcv.out_of_order.map.end();) {
1901 auto& p = it->second;
1902 auto seg_beg = it->first;
1903 auto seg_len = p.len();
1904 auto seg_end = seg_beg + seg_len;
1905 if (seg_beg <= _rcv.next && _rcv.next < seg_end) {
1908 auto trim = _rcv.next - seg_beg;
1913 _rcv.next += seg_len;
1914 _rcv.data_size += p.len();
1915 _rcv.data.push_back(std::move(p));
1917 it = _rcv.out_of_order.map.erase(it);
1919 }
else if (_rcv.next >= seg_end) {
1921 it = _rcv.out_of_order.map.erase(it);
1932template <
typename InetTraits>
1933void tcp<InetTraits>::tcb::insert_out_of_order(tcp_seq seg, packet p) {
1934 _rcv.out_of_order.merge(seg, std::move(p));
1937template <
typename InetTraits>
1938void tcp<InetTraits>::tcb::trim_receive_data_after_window() {
1942template <
typename InetTraits>
1943void tcp<InetTraits>::tcb::persist() {
1944 tcp_debug(
"persist timer fired\n");
1946 _snd.window_probe =
true;
1947 _snd.zero_window_probing_out++;
1949 _snd.window_probe =
false;
1953 _persist_time_out = std::min(_persist_time_out * 2, _rto_max);
1954 start_persist_timer();
1957template <
typename InetTraits>
1958void tcp<InetTraits>::tcb::retransmit() {
1959 auto output_update_rto = [
this] {
1962 this->_rto = std::min(this->_rto * 2, this->_rto_max);
1963 start_retransmit_timer();
1967 if (syn_needs_on()) {
1968 if (_snd.syn_retransmit++ < _max_nr_retransmit) {
1969 output_update_rto();
1971 _connect_done.set_exception(tcp_connect_error());
1978 if (fin_needs_on()) {
1979 if (_snd.fin_retransmit++ < _max_nr_retransmit) {
1980 output_update_rto();
1988 if (_snd.data.empty()) {
1993 auto& unacked_seg = _snd.data.front();
1997 uint32_t smss = _snd.mss;
1998 if (unacked_seg.nr_transmits == 0) {
1999 _snd.ssthresh = std::max(flight_size() / 2, 2 * smss);
2002 _snd.recover = _snd.next - 1;
2006 exit_fast_recovery();
2008 if (unacked_seg.nr_transmits < _max_nr_retransmit) {
2009 unacked_seg.nr_transmits++;
2017 output_update_rto();
2020template <
typename InetTraits>
2021void tcp<InetTraits>::tcb::fast_retransmit() {
2022 if (!_snd.data.empty()) {
2023 auto& unacked_seg = _snd.data.front();
2024 unacked_seg.nr_transmits++;
2030template <
typename InetTraits>
2031void tcp<InetTraits>::tcb::update_rto(clock_type::time_point tx_time) {
2033 auto R = std::chrono::duration_cast<std::chrono::milliseconds>(
clock_type::now() - tx_time);
2034 if (_snd.first_rto_sample) {
2035 _snd.first_rto_sample =
false;
2038 _snd.rttvar = R / 2;
2044 auto delta = _snd.srtt > R ? (_snd.srtt - R) : (R - _snd.srtt);
2045 _snd.rttvar = _snd.rttvar * 3 / 4 + delta / 4;
2046 _snd.srtt = _snd.srtt * 7 / 8 + R / 8;
2049 _rto = _snd.srtt + std::max(_rto_clk_granularity, 4 * _snd.rttvar);
2052 _rto = std::max(_rto, _rto_min);
2053 _rto = std::min(_rto, _rto_max);
2056template <
typename InetTraits>
2057void tcp<InetTraits>::tcb::update_cwnd(uint32_t acked_bytes) {
2058 uint32_t smss = _snd.mss;
2059 if (_snd.cwnd < _snd.ssthresh) {
2061 _snd.cwnd += std::min(acked_bytes, smss);
2064 uint32_t round_up = 1;
2065 _snd.cwnd += std::max(round_up, smss * smss / _snd.cwnd);
2069template <
typename InetTraits>
2070void tcp<InetTraits>::tcb::cleanup() {
2071 _snd.unsent.clear();
2073 _rcv.out_of_order.map.clear();
2076 stop_retransmit_timer();
2077 clear_delayed_ack();
2081template <
typename InetTraits>
2082tcp_seq tcp<InetTraits>::tcb::get_isn() {
2087 using namespace std::chrono;
2089 hash[0] = _local_ip.ip;
2090 hash[1] = _foreign_ip.ip;
2091 hash[2] = (_local_port << 16) + _foreign_port;
2092 gnutls_hash_hd_t md5_hash_handle;
2094 gnutls_hash_init(&md5_hash_handle, GNUTLS_DIG_MD5);
2095 gnutls_hash(md5_hash_handle, hash, 3 *
sizeof(hash[0]));
2096 gnutls_hash(md5_hash_handle, _isn_secret.key,
sizeof(_isn_secret.key));
2098 SEASTAR_ASSERT(
sizeof(hash) == gnutls_hash_get_len(GNUTLS_DIG_MD5));
2099 gnutls_hash_deinit(md5_hash_handle, hash);
2101 auto m = duration_cast<microseconds>(
clock_type::now().time_since_epoch());
2102 seq += m.count() / 4;
2103 return make_seq(seq);
2106template <
typename InetTraits>
2107std::optional<typename InetTraits::l4packet> tcp<InetTraits>::tcb::get_packet() {
2108 _poll_active =
false;
2109 if (_packetq.empty()) {
2113 if (in_state(CLOSED)) {
2114 return std::optional<typename InetTraits::l4packet>();
2117 SEASTAR_ASSERT(!_packetq.empty());
2119 auto p = std::move(_packetq.front());
2120 _packetq.pop_front();
2121 if (!_packetq.empty() || (_snd.dupacks < 3 && can_send() > 0 && (_snd.window > 0))) {
2131template <
typename InetTraits>
2132void tcp<InetTraits>::connection::close_read() noexcept {
2133 _tcb->abort_reader();
2136template <
typename InetTraits>
2137void tcp<InetTraits>::connection::close_write() noexcept {
2141template <
typename InetTraits>
2142void tcp<InetTraits>::connection::shutdown_connect() {
2143 if (_tcb->syn_needs_on()) {
2144 _tcb->_connect_done.set_exception(tcp_refused_error());
2152template <
typename InetTraits>
2153typename tcp<InetTraits>::tcb::isn_secret tcp<InetTraits>::tcb::_isn_secret;
bool try_wait(size_t nr=1) noexcept
Definition: semaphore.hh:448
void signal(size_t nr=1) noexcept
Definition: semaphore.hh:410
Definition: shared_ptr.hh:148
A representation of a possibly not-yet-computed value.
Definition: future.hh:1197
Low-resolution and efficient steady clock.
Definition: lowres_clock.hh:56
static time_point now() noexcept
Definition: lowres_clock.hh:74
holds the metric definition.
Definition: metrics_registration.hh:94
metric_groups & add_group(const group_name_type &name, const std::initializer_list< metric_definition > &l)
Add metrics belonging to the same group.
void set_value(A &&... a) noexcept
Sets the promises value.
Definition: future.hh:968
void set_exception(std::exception_ptr &&ex) noexcept
Marks the promise as failed.
Definition: future.hh:976
size_t size() const noexcept
Returns the number of items currently in the queue.
Definition: queue.hh:110
future< T > pop_eventually() noexcept
Definition: queue.hh:226
size_t max_size() const noexcept
Definition: queue.hh:118
void abort(std::exception_ptr ex) noexcept
Definition: queue.hh:132
Definition: socket_defs.hh:47
void rearm(time_point until, std::optional< duration > period={}) noexcept
Definition: timer.hh:168
future< T > get_future() noexcept
Gets the promise's associated future.
Definition: future.hh:1852
future now()
Returns a ready future.
Definition: later.hh:35
impl::metric_definition_impl make_counter(metric_name_type name, T &&val, description d=description(), std::vector< label_instance > labels={})
create a counter metric
Definition: metrics.hh:528
server_socket listen(socket_address sa)
future< connected_socket > connect(socket_address sa)
header for metrics creation.
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
shard_id this_shard_id() noexcept
Returns shard_id of the of the current shard.
Definition: shard_id.hh:52
Definition: ethernet.hh:37