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/std-compat.hh>
49using namespace std::chrono_literals;
55inline auto tcp_error(
int err) {
56 return std::system_error(err, std::system_category());
59inline auto tcp_reset_error() {
60 return tcp_error(ECONNRESET);
63inline auto tcp_connect_error() {
64 return tcp_error(ECONNABORTED);
67inline auto tcp_refused_error() {
68 return tcp_error(ECONNREFUSED);
71enum class tcp_state : uint16_t {
75 SYN_RECEIVED = (1 << 3),
76 ESTABLISHED = (1 << 4),
77 FIN_WAIT_1 = (1 << 5),
78 FIN_WAIT_2 = (1 << 6),
79 CLOSE_WAIT = (1 << 7),
85inline tcp_state operator|(tcp_state s1, tcp_state s2) {
86 return tcp_state(uint16_t(s1) | uint16_t(s2));
89template <
typename... Args>
90void tcp_debug(
const char* fmt, Args&&... args) {
92 print(fmt, std::forward<Args>(args)...);
100 static void write(
char* p, option_kind kind, option_len len) {
101 p[0] =
static_cast<uint8_t
>(kind);
102 if (
static_cast<uint8_t
>(len) > 1) {
103 p[1] =
static_cast<uint8_t
>(len);
107 static constexpr option_kind kind = option_kind::mss;
108 static constexpr option_len len = option_len::mss;
112 x.mss = read_be<uint16_t>(p + 2);
115 void write(
char* p)
const {
116 tcp_option::write(p, kind, len);
117 write_be<uint16_t>(p + 2,
mss);
121 static constexpr option_kind kind = option_kind::win_scale;
122 static constexpr option_len len = option_len::win_scale;
129 void write(
char* p)
const {
130 tcp_option::write(p, kind, len);
135 static constexpr option_kind kind = option_kind::sack;
136 static constexpr option_len len = option_len::sack;
140 void write(
char* p)
const {
141 tcp_option::write(p, kind, len);
145 static constexpr option_kind kind = option_kind::timestamps;
146 static constexpr option_len len = option_len::timestamps;
151 ts.t1 = read_be<uint32_t>(p + 2);
152 ts.t2 = read_be<uint32_t>(p + 6);
155 void write(
char* p)
const {
156 tcp_option::write(p, kind, len);
157 write_be<uint32_t>(p + 2, t1);
158 write_be<uint32_t>(p + 6, t2);
162 static constexpr option_kind kind = option_kind::nop;
163 static constexpr option_len len = option_len::nop;
164 void write(
char* p)
const {
165 tcp_option::write(p, kind, len);
169 static constexpr option_kind kind = option_kind::eol;
170 static constexpr option_len len = option_len::eol;
171 void write(
char* p)
const {
172 tcp_option::write(p, kind, len);
175 static const uint8_t align = 4;
177 void parse(uint8_t* beg, uint8_t* end);
178 uint8_t fill(
void* h,
const tcp_hdr* th, uint8_t option_size);
179 uint8_t get_size(
bool syn_on,
bool ack_on);
182 bool _mss_received =
false;
183 bool _win_scale_received =
false;
184 bool _timestamps_received =
false;
185 bool _sack_received =
false;
188 uint16_t _remote_mss = 536;
190 uint8_t _remote_win_scale = 0;
191 uint8_t _local_win_scale = 0;
193inline char*& operator+=(
char*& x, tcp_option::option_len len) { x += uint8_t(len);
return x; }
194inline const char*& operator+=(
const char*& x, tcp_option::option_len len) { x += uint8_t(len);
return x; }
195inline uint8_t& operator+=(uint8_t& x, tcp_option::option_len len) { x += uint8_t(len);
return x; }
202 return tcp_seq { ntoh(s.raw) };
205inline tcp_seq hton(tcp_seq s) {
206 return tcp_seq { hton(s.raw) };
210std::ostream& operator<<(std::ostream& os, tcp_seq s) {
214inline tcp_seq make_seq(uint32_t raw) {
return tcp_seq{raw}; }
215inline tcp_seq& operator+=(tcp_seq& s, int32_t n) { s.raw += n;
return s; }
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) {
return s += n; }
218inline tcp_seq operator-(tcp_seq s, int32_t n) {
return s -= n; }
219inline int32_t operator-(tcp_seq s, tcp_seq q) {
return s.raw - q.raw; }
220inline bool operator==(tcp_seq s, tcp_seq q) {
return s.raw == q.raw; }
221inline bool operator!=(tcp_seq s, tcp_seq q) {
return !(s == q); }
222inline bool operator<(tcp_seq s, tcp_seq q) {
return s - q < 0; }
223inline bool operator>(tcp_seq s, tcp_seq q) {
return q < s; }
224inline bool operator<=(tcp_seq s, tcp_seq q) {
return !(s > q); }
225inline bool operator>=(tcp_seq s, tcp_seq q) {
return !(s < q); }
228 static constexpr size_t len = 20;
234 uint8_t data_offset : 4;
245 static tcp_hdr read(
const char* p) {
247 h.src_port = read_be<uint16_t>(p + 0);
248 h.dst_port = read_be<uint16_t>(p + 2);
249 h.seq =
tcp_seq{read_be<uint32_t>(p + 4)};
250 h.ack =
tcp_seq{read_be<uint32_t>(p + 8)};
251 h.rsvd1 = p[12] & 15;
252 h.data_offset = uint8_t(p[12]) >> 4;
253 h.f_fin = (uint8_t(p[13]) >> 0) & 1;
254 h.f_syn = (uint8_t(p[13]) >> 1) & 1;
255 h.f_rst = (uint8_t(p[13]) >> 2) & 1;
256 h.f_psh = (uint8_t(p[13]) >> 3) & 1;
257 h.f_ack = (uint8_t(p[13]) >> 4) & 1;
258 h.f_urg = (uint8_t(p[13]) >> 5) & 1;
259 h.rsvd2 = (uint8_t(p[13]) >> 6) & 3;
260 h.window = read_be<uint16_t>(p + 14);
261 h.checksum = read_be<uint16_t>(p + 16);
262 h.urgent = read_be<uint16_t>(p + 18);
265 void write(
char* p)
const {
266 write_be<uint16_t>(p + 0, src_port);
267 write_be<uint16_t>(p + 2, dst_port);
268 write_be<uint32_t>(p + 4, seq.raw);
269 write_be<uint32_t>(p + 8, ack.raw);
270 p[12] = rsvd1 | (data_offset << 4);
278 write_be<uint16_t>(p + 14, window);
279 write_be<uint16_t>(p + 16, checksum);
280 write_be<uint16_t>(p + 18, urgent);
282 static void write_nbo_checksum(
char* p, uint16_t checksum_in_network_byte_order) {
283 std::copy_n(
reinterpret_cast<const char*
>(&checksum_in_network_byte_order), 2, p + 16);
290template <
typename InetTraits>
293 using ipaddr =
typename InetTraits::address_type;
294 using inet_type =
typename InetTraits::inet_type;
304 static constexpr tcp_state CLOSED = tcp_state::CLOSED;
305 static constexpr tcp_state LISTEN = tcp_state::LISTEN;
306 static constexpr tcp_state SYN_SENT = tcp_state::SYN_SENT;
307 static constexpr tcp_state SYN_RECEIVED = tcp_state::SYN_RECEIVED;
308 static constexpr tcp_state ESTABLISHED = tcp_state::ESTABLISHED;
309 static constexpr tcp_state FIN_WAIT_1 = tcp_state::FIN_WAIT_1;
310 static constexpr tcp_state FIN_WAIT_2 = tcp_state::FIN_WAIT_2;
311 static constexpr tcp_state CLOSE_WAIT = tcp_state::CLOSE_WAIT;
312 static constexpr tcp_state CLOSING = tcp_state::CLOSING;
313 static constexpr tcp_state LAST_ACK = tcp_state::LAST_ACK;
314 static constexpr tcp_state TIME_WAIT = tcp_state::TIME_WAIT;
315 tcp_state _state = CLOSED;
319 std::optional<promise<>> _fin_recvd_promise =
promise<>();
322 uint16_t _local_port;
323 uint16_t _foreign_port;
324 struct unacked_segment {
327 unsigned nr_transmits;
328 clock_type::time_point tx_time;
334 uint8_t window_scale;
340 std::deque<unacked_segment> data;
341 std::deque<packet> unsent;
342 uint32_t unsent_len = 0;
346 std::optional<promise<>> _all_data_acked_promise;
348 size_t max_queue_space = 212992;
349 size_t current_queue_space = 0;
351 std::optional<promise<>> _send_available_promise;
353 std::chrono::milliseconds rttvar;
355 std::chrono::milliseconds srtt;
356 bool first_rto_sample =
true;
357 clock_type::time_point syn_tx_time;
363 uint16_t dupacks = 0;
364 unsigned syn_retransmit = 0;
365 unsigned fin_retransmit = 0;
366 uint32_t limited_transfer = 0;
367 uint32_t partial_ack = 0;
369 bool window_probe =
false;
370 uint8_t zero_window_probing_out = 0;
375 uint8_t window_scale;
379 std::deque<packet> data;
381 size_t data_size = 0;
383 std::optional<promise<>> _data_received_promise;
386 size_t max_receive_buf_size = 3737600;
391 std::chrono::milliseconds _rto{1000};
392 std::chrono::milliseconds _persist_time_out{1000};
393 static constexpr std::chrono::milliseconds _rto_min{1000};
394 static constexpr std::chrono::milliseconds _rto_max{60000};
396 static constexpr std::chrono::milliseconds _rto_clk_granularity{1};
397 static constexpr uint16_t _max_nr_retransmit{5};
400 uint16_t _nr_full_seg_received = 0;
405 std::random_device rd;
406 std::default_random_engine e(rd());
407 std::uniform_int_distribution<uint32_t> dist{};
408 for (
auto& k : key) {
413 static isn_secret _isn_secret;
416 bool _poll_active =
false;
417 uint32_t get_default_receive_window_size() {
419 constexpr uint32_t size = 29200;
420 return size << _rcv.window_scale;
423 uint32_t get_modified_receive_window_size() {
424 uint32_t left = _rcv.data_size > _rcv.max_receive_buf_size ? 0 : _rcv.max_receive_buf_size - _rcv.data_size;
425 return std::min(left, get_default_receive_window_size());
432 void output_one(
bool data_retransmit =
false);
435 void abort_reader()
noexcept;
441 void close()
noexcept;
442 void remove_from_tcbs() {
443 auto id =
connid{_local_ip, _foreign_ip, _local_port, _foreign_port};
444 _tcp._tcbs.erase(
id);
446 std::optional<typename InetTraits::l4packet> get_packet();
451 (void)_tcp.poll_tcb(_foreign_ip, this->shared_from_this()).then_wrapped([
this] (
auto&& f) {
456 _poll_active =
false;
457 this->start_retransmit_timer();
459 if (this->in_state(SYN_SENT)) {
475 void respond_with_reset(
tcp_hdr* th);
476 bool merge_out_of_order();
478 void trim_receive_data_after_window();
479 bool should_send_ack(uint16_t seg_len);
480 void clear_delayed_ack()
noexcept;
481 packet get_transmit_packet();
482 void retransmit_one() {
483 bool data_retransmit =
true;
484 output_one(data_retransmit);
486 void start_retransmit_timer() {
488 start_retransmit_timer(
now);
490 void start_retransmit_timer(clock_type::time_point
now) {
491 auto tp =
now + _rto;
492 _retransmit.
rearm(tp);
494 void stop_retransmit_timer()
noexcept {
497 void start_persist_timer() {
499 start_persist_timer(
now);
501 void start_persist_timer(clock_type::time_point
now) {
502 auto tp =
now + _persist_time_out;
505 void stop_persist_timer() {
510 void fast_retransmit();
511 void update_rto(clock_type::time_point tx_time);
512 void update_cwnd(uint32_t acked_bytes);
514 uint32_t can_send() {
515 if (_snd.window_probe) {
520 if (_snd.window == 0) {
525 auto window_used = uint32_t(_snd.next - _snd.unacknowledged);
526 if (window_used > _snd.window) {
531 auto x = std::min(_snd.window - window_used, _snd.unsent_len);
534 x = std::min(_snd.cwnd, x);
535 if (_snd.dupacks == 1 || _snd.dupacks == 2) {
538 auto flight = flight_size();
539 auto max = _snd.cwnd + 2 * _snd.mss;
540 x = flight <= max ? std::min(x, max - flight) : 0;
541 _snd.limited_transfer += x;
542 }
else if (_snd.dupacks >= 3) {
545 x = std::min(uint32_t(_snd.mss), x);
549 uint32_t flight_size() {
551 std::for_each(_snd.data.begin(), _snd.data.end(), [&] (unacked_segment& seg) { size += seg.p.len(); });
554 uint16_t local_mss() {
555 return _tcp.hw_features().mtu - net::tcp_hdr_len_min - InetTraits::ip_hdr_len_min;
557 void queue_packet(
packet p) {
558 _packetq.emplace_back(
typename InetTraits::l4packet{_foreign_ip, std::move(p)});
560 void signal_data_received() {
561 if (_rcv._data_received_promise) {
562 _rcv._data_received_promise->set_value();
563 _rcv._data_received_promise = {};
566 void signal_all_data_acked() {
567 if (_snd._all_data_acked_promise && _snd.unsent_len == 0) {
568 _snd._all_data_acked_promise->set_value();
569 _snd._all_data_acked_promise = {};
572 void signal_send_available() {
573 if (_snd._send_available_promise && _snd.max_queue_space > _snd.current_queue_space) {
574 _snd._send_available_promise->set_value();
575 _snd._send_available_promise = {};
584 void do_syn_received() {
585 _state = SYN_RECEIVED;
590 void do_established() {
591 _state = ESTABLISHED;
592 update_rto(_snd.syn_tx_time);
598 if (_rcv._data_received_promise) {
599 _rcv._data_received_promise->set_exception(tcp_reset_error());
600 _rcv._data_received_promise = std::nullopt;
602 if (_snd._all_data_acked_promise) {
603 _snd._all_data_acked_promise->set_exception(tcp_reset_error());
604 _snd._all_data_acked_promise = std::nullopt;
606 if (_snd._send_available_promise) {
607 _snd._send_available_promise->set_exception(tcp_reset_error());
608 _snd._send_available_promise = std::nullopt;
611 void do_time_wait() {
620 void do_setup_isn() {
621 _snd.initial = get_isn();
622 _snd.unacknowledged = _snd.initial;
623 _snd.next = _snd.initial + 1;
624 _snd.recover = _snd.initial;
626 void do_local_fin_acked() {
627 _snd.unacknowledged += 1;
630 bool syn_needs_on()
const noexcept {
631 return in_state(SYN_SENT | SYN_RECEIVED);
633 bool fin_needs_on()
const noexcept {
634 return in_state(FIN_WAIT_1 | CLOSING | LAST_ACK) && _snd.closed &&
635 _snd.unsent_len == 0;
637 bool ack_needs_on()
const noexcept {
638 return !in_state(CLOSED | LISTEN | SYN_SENT);
640 bool foreign_will_not_send()
const noexcept {
641 return in_state(CLOSING | TIME_WAIT | CLOSE_WAIT | LAST_ACK | CLOSED);
643 bool in_state(tcp_state state)
const noexcept {
644 return uint16_t(_state) & uint16_t(state);
646 void exit_fast_recovery() {
648 _snd.limited_transfer = 0;
649 _snd.partial_ack = 0;
651 uint32_t data_segment_acked(
tcp_seq seg_ack);
652 bool segment_acceptable(
tcp_seq seg_seq,
unsigned seg_len);
653 void init_from_options(
tcp_hdr* th, uint8_t* opt_start, uint8_t* opt_end);
657 std::unordered_map<connid, lw_shared_ptr<tcb>, connid_hash> _tcbs;
658 std::unordered_map<uint16_t, listener*> _listening;
659 std::random_device _rd;
660 std::default_random_engine _e;
661 std::uniform_int_distribution<uint16_t> _port_dist{41952, 65535};
668 const inet_type& inet()
const {
689 return _tcb->connect_done();
692 return _tcb->send(std::move(p));
695 return _tcb->wait_for_data();
698 return _tcb->wait_input_shutdown();
703 ipaddr foreign_ip() {
704 return _tcb->_foreign_ip;
706 uint16_t foreign_port() {
707 return _tcb->_foreign_port;
710 return _tcb->_local_ip;
712 uint16_t local_port() {
713 return _tcb->_local_port;
715 void shutdown_connect();
716 void close_read()
noexcept;
717 void close_write()
noexcept;
725 listener(
tcp& t, uint16_t port,
size_t queue_length)
726 : _tcp(t), _port(port), _q(queue_length) {
727 _tcp._listening.emplace(_port,
this);
731 : _tcp(x._tcp), _port(x._port), _q(std::move(x._q)) {
732 _tcp._listening[_port] =
this;
737 _tcp._listening.erase(_port);
743 void abort_accept() {
744 _q.
abort(std::make_exception_ptr(std::system_error(ECONNABORTED, std::system_category())));
746 bool full() {
return _pending + _q.
size() >= _q.
max_size(); }
747 void inc_pending() { _pending++; }
748 void dec_pending() { _pending--; }
750 const tcp& get_tcp()
const {
753 uint16_t port()
const {
759 explicit tcp(inet_type& inet);
760 void received(
packet p, ipaddr from, ipaddr to);
767 auto it = _listening.find(local_port);
768 if (it != _listening.end()) {
769 it->second->_q.push(connection(tcbp));
770 it->second->dec_pending();
774 void send_packet_without_tcb(ipaddr from, ipaddr to, packet p);
775 void respond_with_reset(tcp_hdr* rth, ipaddr local_ip, ipaddr foreign_ip);
776 friend class listener;
779template <
typename InetTraits>
780tcp<InetTraits>::tcp(inet_type& inet)
783 namespace sm = metrics;
786 sm::make_counter(
"linearizations", [] {
return tcp_packet_merger::linearizations(); },
787 sm::description(
"Counts a number of times a buffer linearization was invoked during the buffers merge process. "
788 "Divide it by a total TCP receive packet rate to get an everage number of lineraizations per TCP packet."))
791 _inet.register_packet_provider([
this, tcb_polled = 0u] ()
mutable {
792 std::optional<typename InetTraits::l4packet> l4p;
793 auto c = _poll_tcbs.size();
794 if (!_packetq.empty() && (!(tcb_polled % 128) || c == 0)) {
795 l4p = std::move(_packetq.front());
796 _packetq.pop_front();
797 _queue_space.
signal(l4p.value().p.len());
801 lw_shared_ptr<tcb> tcb;
802 ethernet_address dst;
803 std::tie(tcb, dst) = std::move(_poll_tcbs.front());
804 _poll_tcbs.pop_front();
805 l4p = tcb->get_packet();
807 l4p.value().e_dst = dst;
816template <
typename InetTraits>
817future<> tcp<InetTraits>::poll_tcb(ipaddr to, lw_shared_ptr<tcb> tcb) {
818 return _inet.get_l2_dst_address(to).then([
this, tcb = std::move(tcb)] (ethernet_address dst) {
819 _poll_tcbs.emplace_back(std::move(tcb), dst);
823template <
typename InetTraits>
825 return listener(*
this, port, queue_length);
828template <
typename InetTraits>
831 auto src_ip = _inet._inet.host_address();
832 auto dst_ip = ipv4_address(sa);
833 auto dst_port = net::ntoh(sa.u.in.sin_port);
835 if (smp::count > 1) {
837 id = connid{src_ip, dst_ip, _port_dist(_e), dst_port};
838 }
while (_inet._inet.netif()->hash2cpu(
id.hash(_inet._inet.netif()->rss_key())) !=
this_shard_id()
839 || _tcbs.find(
id) != _tcbs.end());
841 id = connid{src_ip, dst_ip, _port_dist(_e), dst_port};
844 auto tcbp = make_lw_shared<tcb>(*
this,
id);
845 _tcbs.insert({id, tcbp});
847 return connection(tcbp);
850template <
typename InetTraits>
851bool tcp<InetTraits>::forward(forward_hash& out_hash_data, packet& p,
size_t off) {
852 auto th = p.get_header(off, tcp_hdr::len);
855 out_hash_data.push_back(uint8_t(th[0]));
856 out_hash_data.push_back(uint8_t(th[1]));
857 out_hash_data.push_back(uint8_t(th[2]));
858 out_hash_data.push_back(uint8_t(th[3]));
863template <
typename InetTraits>
864void tcp<InetTraits>::received(packet p, ipaddr from, ipaddr to) {
865 auto th = p.get_header(0, tcp_hdr::len);
870 auto data_offset = uint8_t(th[12]) >> 4;
871 if (
size_t(data_offset * 4) < tcp_hdr::len) {
875 if (!hw_features().rx_csum_offload) {
877 InetTraits::tcp_pseudo_header_checksum(csum, from, to, p.len());
879 if (csum.get() != 0) {
883 auto h = tcp_hdr::read(th);
884 auto id = connid{to, from, h.dst_port, h.src_port};
885 auto tcbi = _tcbs.find(
id);
886 lw_shared_ptr<tcb> tcbp;
887 if (tcbi == _tcbs.end()) {
888 auto listener = _listening.find(
id.local_port);
889 if (listener == _listening.end() || listener->second->full()) {
897 return respond_with_reset(&h,
id.local_ip,
id.foreign_ip);
910 return respond_with_reset(&h,
id.local_ip,
id.foreign_ip);
916 tcbp = make_lw_shared<tcb>(*
this,
id);
917 _tcbs.insert({id, tcbp});
920 listener->second->inc_pending();
922 return tcbp->input_handle_listen_state(&h, std::move(p));
931 if (tcbp->state() == tcp_state::SYN_SENT) {
933 return tcbp->input_handle_syn_sent_state(&h, std::move(p));
938 return tcbp->input_handle_other_state(&h, std::move(p));
944template <
typename InetTraits>
945void tcp<InetTraits>::send_packet_without_tcb(ipaddr from, ipaddr to, packet p) {
946 if (_queue_space.
try_wait(p.len())) {
948 (void)_inet.get_l2_dst_address(to).then([
this, to, p = std::move(p)] (ethernet_address e_dst)
mutable {
949 _packetq.emplace_back(ipv4_traits::l4packet{to, std::move(p), e_dst, ip_protocol_num::tcp});
954template <
typename InetTraits>
955tcp<InetTraits>::connection::~connection() {
957 _tcb->_conn =
nullptr;
963template <
typename InetTraits>
964tcp<InetTraits>::tcb::tcb(tcp& t, connid
id)
966 , _local_ip(id.local_ip)
967 , _foreign_ip(id.foreign_ip)
968 , _local_port(id.local_port)
969 , _foreign_port(id.foreign_port)
970 , _delayed_ack([this] { _nr_full_seg_received = 0; output(); })
971 , _retransmit([
this] { retransmit(); })
972 , _persist([
this] { persist(); }) {
975template <
typename InetTraits>
976void tcp<InetTraits>::tcb::respond_with_reset(tcp_hdr* rth) {
977 _tcp.respond_with_reset(rth, _local_ip, _foreign_ip);
980template <
typename InetTraits>
981void tcp<InetTraits>::respond_with_reset(tcp_hdr* rth, ipaddr local_ip, ipaddr foreign_ip) {
986 auto th = p.prepend_uninitialized_header(tcp_hdr::len);
988 h.src_port = rth->dst_port;
989 h.dst_port = rth->src_port;
995 h.ack = rth->seq + 1;
999 h.data_offset = tcp_hdr::len / 4;
1005 InetTraits::tcp_pseudo_header_checksum(csum, local_ip, foreign_ip, tcp_hdr::len);
1007 if (hw_features().tx_csum_l4_offload) {
1008 checksum = ~csum.get();
1009 oi.needs_csum =
true;
1012 checksum = csum.get();
1013 oi.needs_csum =
false;
1015 tcp_hdr::write_nbo_checksum(th, checksum);
1017 oi.protocol = ip_protocol_num::tcp;
1018 oi.tcp_hdr_len = tcp_hdr::len;
1019 p.set_offload_info(oi);
1021 send_packet_without_tcb(local_ip, foreign_ip, std::move(p));
1024template <
typename InetTraits>
1025uint32_t tcp<InetTraits>::tcb::data_segment_acked(tcp_seq seg_ack) {
1026 uint32_t total_acked_bytes = 0;
1028 while (!_snd.data.empty()
1029 && (_snd.unacknowledged + _snd.data.front().p.len() <= seg_ack)) {
1030 auto acked_bytes = _snd.data.front().p.len();
1031 _snd.unacknowledged += acked_bytes;
1033 if (_snd.data.front().nr_transmits == 0) {
1034 update_rto(_snd.data.front().tx_time);
1036 update_cwnd(acked_bytes);
1037 total_acked_bytes += acked_bytes;
1038 _snd.current_queue_space -= _snd.data.front().data_len;
1039 signal_send_available();
1040 _snd.data.pop_front();
1043 if (_snd.unacknowledged < seg_ack) {
1044 auto acked_bytes = seg_ack - _snd.unacknowledged;
1045 if (!_snd.data.empty()) {
1046 auto& unacked_seg = _snd.data.front();
1047 unacked_seg.p.trim_front(acked_bytes);
1049 _snd.unacknowledged = seg_ack;
1050 update_cwnd(acked_bytes);
1051 total_acked_bytes += acked_bytes;
1053 return total_acked_bytes;
1056template <
typename InetTraits>
1057bool tcp<InetTraits>::tcb::segment_acceptable(tcp_seq seg_seq,
unsigned seg_len) {
1058 if (seg_len == 0 && _rcv.window == 0) {
1060 return seg_seq == _rcv.next;
1061 }
else if (seg_len == 0 && _rcv.window > 0) {
1063 return (_rcv.next <= seg_seq) && (seg_seq < _rcv.next + _rcv.window);
1064 }
else if (seg_len > 0 && _rcv.window > 0) {
1068 bool x = (_rcv.next <= seg_seq) && seg_seq < (_rcv.next + _rcv.window);
1069 bool y = (_rcv.next <= seg_seq + seg_len - 1) && (seg_seq + seg_len - 1 < _rcv.next + _rcv.window);
1077template <
typename InetTraits>
1078void tcp<InetTraits>::tcb::init_from_options(tcp_hdr* th, uint8_t* opt_start, uint8_t* opt_end) {
1080 _option.parse(opt_start, opt_end);
1083 _snd.window_scale = _option._remote_win_scale;
1085 _rcv.window_scale = _option._local_win_scale;
1088 _snd.mss = _option._remote_mss;
1090 _rcv.mss = _option._local_mss = local_mss();
1092 _rcv.window = get_default_receive_window_size();
1093 _snd.window = th->window << _snd.window_scale;
1101 if (2190 < _snd.mss) {
1102 _snd.cwnd = 2 * _snd.mss;
1103 }
else if (1095 < _snd.mss && _snd.mss <= 2190) {
1104 _snd.cwnd = 3 * _snd.mss;
1106 _snd.cwnd = 4 * _snd.mss;
1110 _snd.ssthresh = th->window << _snd.window_scale;
1113template <
typename InetTraits>
1114void tcp<InetTraits>::tcb::input_handle_listen_state(tcp_hdr* th, packet p) {
1115 auto opt_len = th->data_offset * 4 - tcp_hdr::len;
1116 auto opt_start =
reinterpret_cast<uint8_t*
>(p.get_header(0, th->data_offset * 4)) + tcp_hdr::len;
1117 auto opt_end = opt_start + opt_len;
1118 p.trim_front(th->data_offset * 4);
1119 tcp_seq seg_seq = th->seq;
1122 _rcv.next = seg_seq + 1;
1123 _rcv.initial = seg_seq;
1135 _rcv.urgent = _rcv.next;
1137 tcp_debug(
"listen: LISTEN -> SYN_RECEIVED\n");
1138 init_from_options(th, opt_start, opt_end);
1142template <
typename InetTraits>
1143void tcp<InetTraits>::tcb::input_handle_syn_sent_state(tcp_hdr* th, packet p) {
1144 auto opt_len = th->data_offset * 4 - tcp_hdr::len;
1145 auto opt_start =
reinterpret_cast<uint8_t*
>(p.get_header(0, th->data_offset * 4)) + tcp_hdr::len;
1146 auto opt_end = opt_start + opt_len;
1147 p.trim_front(th->data_offset * 4);
1148 tcp_seq seg_seq = th->seq;
1149 auto seg_ack = th->ack;
1151 bool acceptable =
false;
1156 if (seg_ack <= _snd.initial || seg_ack > _snd.next) {
1157 return respond_with_reset(th);
1161 acceptable = _snd.unacknowledged <= seg_ack && seg_ack <= _snd.next;
1170 _connect_done.set_exception(tcp_refused_error());
1186 _rcv.next = seg_seq + 1;
1187 _rcv.initial = seg_seq;
1190 _snd.unacknowledged = seg_ack;
1192 if (_snd.unacknowledged > _snd.initial) {
1196 tcp_debug(
"syn: SYN_SENT -> ESTABLISHED\n");
1197 init_from_options(th, opt_start, opt_end);
1203 tcp_debug(
"syn: SYN_SENT -> SYN_RECEIVED\n");
1213template <
typename InetTraits>
1214void tcp<InetTraits>::tcb::input_handle_other_state(tcp_hdr* th, packet p) {
1215 p.trim_front(th->data_offset * 4);
1216 bool do_output =
false;
1217 bool do_output_data =
false;
1218 tcp_seq seg_seq = th->seq;
1219 auto seg_ack = th->ack;
1220 auto seg_len = p.len();
1223 if (!segment_acceptable(seg_seq, seg_len)) {
1230 if (seg_seq < _rcv.next) {
1232 auto dup = std::min(uint32_t(_rcv.next - seg_seq), seg_len);
1239 if (seg_seq != _rcv.next) {
1240 insert_out_of_order(seg_seq, std::move(p));
1248 if (in_state(SYN_RECEIVED)) {
1258 _connect_done.set_exception(tcp_refused_error());
1261 if (in_state(ESTABLISHED | FIN_WAIT_1 | FIN_WAIT_2 | CLOSE_WAIT)) {
1269 if (in_state(CLOSING | LAST_ACK | TIME_WAIT)) {
1289 respond_with_reset(th);
1303 if (in_state(SYN_RECEIVED)) {
1306 if (_snd.unacknowledged <= seg_ack && seg_ack <= _snd.next) {
1307 tcp_debug(
"SYN_RECEIVED -> ESTABLISHED\n");
1309 _tcp.add_connected_tcb(this->shared_from_this(), _local_port);
1312 return respond_with_reset(th);
1315 auto update_window = [
this, th, seg_seq, seg_ack] {
1316 tcp_debug(
"window update seg_seq=%d, seg_ack=%d, old window=%d new window=%d\n",
1317 seg_seq, seg_ack, _snd.window, th->window << _snd.window_scale);
1318 _snd.window = th->window << _snd.window_scale;
1321 _snd.zero_window_probing_out = 0;
1322 if (_snd.window == 0) {
1323 _persist_time_out = _rto;
1324 start_persist_timer();
1326 stop_persist_timer();
1331 if (in_state(ESTABLISHED | CLOSE_WAIT)){
1333 auto packets_out = _snd.next - _snd.unacknowledged - _snd.zero_window_probing_out;
1335 if (_snd.unacknowledged < seg_ack && seg_ack <= _snd.next) {
1337 auto acked_bytes = data_segment_acked(seg_ack);
1340 if (_snd.wl1 < seg_seq || (_snd.wl1 == seg_seq && _snd.wl2 <= seg_ack)) {
1345 do_output_data =
true;
1347 auto set_retransmit_timer = [
this] {
1348 if (_snd.data.empty()) {
1350 stop_retransmit_timer();
1352 signal_all_data_acked();
1355 start_retransmit_timer();
1359 if (_snd.dupacks >= 3) {
1361 uint32_t smss = _snd.mss;
1362 if (seg_ack > _snd.recover) {
1363 tcp_debug(
"ack: full_ack\n");
1365 _snd.cwnd = std::min(_snd.ssthresh, std::max(flight_size(), smss) + smss);
1367 exit_fast_recovery();
1368 set_retransmit_timer();
1370 tcp_debug(
"ack: partial_ack\n");
1375 _snd.cwnd -= acked_bytes;
1378 if (acked_bytes >= smss) {
1385 if (++_snd.partial_ack == 1) {
1386 start_retransmit_timer();
1397 exit_fast_recovery();
1398 set_retransmit_timer();
1400 }
else if ((packets_out > 0) && !_snd.data.empty() && seg_len == 0 &&
1401 th->f_fin == 0 && th->f_syn == 0 &&
1402 th->ack == _snd.unacknowledged &&
1403 uint32_t(th->window << _snd.window_scale) == _snd.window) {
1412 uint32_t smss = _snd.mss;
1414 if (_snd.dupacks == 1 || _snd.dupacks == 2) {
1417 do_output_data =
true;
1418 }
else if (_snd.dupacks == 3) {
1420 if (seg_ack - 1 > _snd.recover) {
1421 _snd.recover = _snd.next - 1;
1423 _snd.ssthresh = std::max((flight_size() - _snd.limited_transfer) / 2, 2 * smss);
1429 _snd.cwnd = _snd.ssthresh + 3 * smss;
1430 }
else if (_snd.dupacks > 3) {
1434 do_output_data =
true;
1436 }
else if (seg_ack > _snd.next) {
1440 }
else if (_snd.window == 0 && th->window > 0) {
1442 do_output_data =
true;
1446 if (in_state(FIN_WAIT_1)) {
1450 if (seg_ack == _snd.next + 1) {
1451 tcp_debug(
"ack: FIN_WAIT_1 -> FIN_WAIT_2\n");
1452 _state = FIN_WAIT_2;
1453 do_local_fin_acked();
1457 if (in_state(FIN_WAIT_2)) {
1464 if (in_state(CLOSING)) {
1465 if (seg_ack == _snd.next + 1) {
1466 tcp_debug(
"ack: CLOSING -> TIME_WAIT\n");
1467 do_local_fin_acked();
1468 return do_time_wait();
1474 if (in_state(LAST_ACK)) {
1475 if (seg_ack == _snd.next + 1) {
1476 tcp_debug(
"ack: LAST_ACK -> CLOSED\n");
1477 do_local_fin_acked();
1482 if (in_state(TIME_WAIT)) {
1496 if (in_state(ESTABLISHED | FIN_WAIT_1 | FIN_WAIT_2)) {
1502 _rcv.data_size += p.len();
1503 _rcv.data.push_back(std::move(p));
1504 _rcv.next += seg_len;
1505 auto merged = merge_out_of_order();
1506 _rcv.window = get_modified_receive_window_size();
1507 signal_data_received();
1518 do_output = should_send_ack(seg_len);
1521 }
else if (in_state(CLOSE_WAIT | CLOSING | LAST_ACK | TIME_WAIT)) {
1529 if (_fin_recvd_promise) {
1530 _fin_recvd_promise->set_value();
1531 _fin_recvd_promise.reset();
1533 if (in_state(CLOSED | LISTEN | SYN_SENT)) {
1538 auto fin_seq = seg_seq + seg_len;
1539 if (fin_seq == _rcv.next) {
1540 _rcv.next = fin_seq + 1;
1541 signal_data_received();
1545 clear_delayed_ack();
1550 if (in_state(SYN_RECEIVED | ESTABLISHED)) {
1551 tcp_debug(
"fin: SYN_RECEIVED or ESTABLISHED -> CLOSE_WAIT\n");
1552 _state = CLOSE_WAIT;
1554 if (in_state(FIN_WAIT_1)) {
1560 tcp_debug(
"fin: FIN_WAIT_1 -> CLOSING\n");
1563 if (in_state(FIN_WAIT_2)) {
1564 tcp_debug(
"fin: FIN_WAIT_2 -> TIME_WAIT\n");
1565 return do_time_wait();
1569 if (do_output || (do_output_data && can_send())) {
1571 clear_delayed_ack();
1576template <
typename InetTraits>
1577packet tcp<InetTraits>::tcb::get_transmit_packet() {
1579 if (_snd.unsent.empty()) {
1582 auto can_send = this->can_send();
1585 if (_tcp.hw_features().tx_tso) {
1587 len = _tcp.hw_features().max_packet_len - net::tcp_hdr_len_min - InetTraits::ip_hdr_len_min;
1589 len = std::min(uint16_t(_tcp.hw_features().mtu - net::tcp_hdr_len_min - InetTraits::ip_hdr_len_min), _snd.mss);
1591 can_send = std::min(can_send, len);
1593 if (_snd.unsent.size() == 1 && _snd.unsent.front().len() <= can_send) {
1594 auto p = std::move(_snd.unsent.front());
1595 _snd.unsent.pop_front();
1596 _snd.unsent_len -= p.len();
1600 if (_snd.unsent.front().len() > can_send) {
1601 auto p = _snd.unsent.front().share(0, can_send);
1602 _snd.unsent.front().trim_front(can_send);
1603 _snd.unsent_len -= p.len();
1607 auto p = std::move(_snd.unsent.front());
1608 _snd.unsent.pop_front();
1609 can_send -= p.len();
1610 while (!_snd.unsent.empty()
1611 && _snd.unsent.front().len() <= can_send) {
1612 can_send -= _snd.unsent.front().len();
1613 p.append(std::move(_snd.unsent.front()));
1614 _snd.unsent.pop_front();
1616 if (!_snd.unsent.empty() && can_send) {
1617 auto& q = _snd.unsent.front();
1618 p.append(q.share(0, can_send));
1619 q.trim_front(can_send);
1621 _snd.unsent_len -= p.len();
1625template <
typename InetTraits>
1626void tcp<InetTraits>::tcb::output_one(
bool data_retransmit) {
1627 if (in_state(CLOSED)) {
1631 packet p = data_retransmit ? _snd.data.front().p.share() : get_transmit_packet();
1632 packet clone = p.share();
1633 uint16_t len = p.len();
1634 bool syn_on = syn_needs_on();
1635 bool ack_on = ack_needs_on();
1637 auto options_size = _option.get_size(syn_on, ack_on);
1638 auto th = p.prepend_uninitialized_header(tcp_hdr::len + options_size);
1641 h.src_port = _local_port;
1642 h.dst_port = _foreign_port;
1647 clear_delayed_ack();
1653 if (data_retransmit) {
1654 seq = _snd.unacknowledged;
1656 seq = syn_on ? _snd.initial : _snd.next;
1661 h.data_offset = (tcp_hdr::len + options_size) / 4;
1662 h.window = _rcv.window >> _rcv.window_scale;
1666 bool fin_on = fin_needs_on();
1670 _option.fill(th, &h, options_size);
1675 uint16_t pseudo_hdr_seg_len = 0;
1677 oi.tcp_hdr_len = tcp_hdr::len + options_size;
1679 if (_tcp.hw_features().tx_csum_l4_offload) {
1680 oi.needs_csum =
true;
1691 if (_tcp.hw_features().tx_tso && len > _snd.mss) {
1692 oi.tso_seg_size = _snd.mss;
1694 pseudo_hdr_seg_len = tcp_hdr::len + options_size + len;
1697 pseudo_hdr_seg_len = tcp_hdr::len + options_size + len;
1698 oi.needs_csum =
false;
1701 InetTraits::tcp_pseudo_header_checksum(csum, _local_ip, _foreign_ip,
1702 pseudo_hdr_seg_len);
1705 if (_tcp.hw_features().tx_csum_l4_offload) {
1706 checksum = ~csum.get();
1709 checksum = csum.get();
1711 tcp_hdr::write_nbo_checksum(th, checksum);
1713 oi.protocol = ip_protocol_num::tcp;
1715 p.set_offload_info(oi);
1717 if (!data_retransmit && (len || syn_on || fin_on)) {
1720 unsigned nr_transmits = 0;
1721 _snd.data.emplace_back(unacked_segment{std::move(clone),
1722 len, nr_transmits,
now});
1724 if (!_retransmit.armed()) {
1725 start_retransmit_timer(
now);
1733 assert((_snd.window > 0) || ((_snd.window == 0) && (len <= 1)));
1734 queue_packet(std::move(p));
1737template <
typename InetTraits>
1738future<> tcp<InetTraits>::tcb::wait_for_data() {
1739 if (!_rcv.data.empty() || foreign_will_not_send()) {
1740 return make_ready_future<>();
1742 _rcv._data_received_promise =
promise<>();
1743 return _rcv._data_received_promise->get_future();
1746template <
typename InetTraits>
1747future<> tcp<InetTraits>::tcb::wait_input_shutdown() {
1748 if (!_fin_recvd_promise) {
1749 return make_ready_future<>();
1751 return _fin_recvd_promise->get_future();
1754template <
typename InetTraits>
1756tcp<InetTraits>::tcb::abort_reader() noexcept {
1757 if (_rcv._data_received_promise) {
1758 _rcv._data_received_promise->set_exception(
1759 std::make_exception_ptr(std::system_error(ECONNABORTED, std::system_category())));
1760 _rcv._data_received_promise = std::nullopt;
1762 if (_fin_recvd_promise) {
1763 _fin_recvd_promise->set_value();
1764 _fin_recvd_promise.reset();
1768template <
typename InetTraits>
1769future<> tcp<InetTraits>::tcb::wait_for_all_data_acked() {
1770 if (_snd.data.empty() && _snd.unsent_len == 0) {
1771 return make_ready_future<>();
1773 _snd._all_data_acked_promise =
promise<>();
1774 return _snd._all_data_acked_promise->get_future();
1777template <
typename InetTraits>
1785 _rcv.window_scale = _option._local_win_scale = 7;
1787 _rcv.mss = _option._local_mss = local_mss();
1788 _rcv.window = get_default_receive_window_size();
1793template <
typename InetTraits>
1794packet tcp<InetTraits>::tcb::read() {
1796 for (
auto&& q : _rcv.data) {
1797 p.append(std::move(q));
1801 _rcv.window = get_default_receive_window_size();
1805template <
typename InetTraits>
1806future<> tcp<InetTraits>::tcb::wait_send_available() {
1807 if (_snd.max_queue_space > _snd.current_queue_space) {
1808 return make_ready_future<>();
1810 _snd._send_available_promise =
promise<>();
1811 return _snd._send_available_promise->get_future();
1814template <
typename InetTraits>
1815future<> tcp<InetTraits>::tcb::send(packet p) {
1817 if (_snd.closed || in_state(CLOSED)) {
1818 return make_exception_future<>(tcp_reset_error());
1822 _snd.current_queue_space += len;
1823 _snd.unsent_len += len;
1824 _snd.unsent.push_back(std::move(p));
1826 if (can_send() > 0) {
1830 return wait_send_available();
1833template <
typename InetTraits>
1834void tcp<InetTraits>::tcb::close() noexcept {
1835 if (in_state(CLOSED) || _snd.closed) {
1839 (void)wait_for_all_data_acked().then([
this, zis = this->shared_from_this()] ()
mutable {
1841 tcp_debug(
"close: unsent_len=%d\n", _snd.unsent_len);
1842 if (in_state(CLOSE_WAIT)) {
1843 tcp_debug(
"close: CLOSE_WAIT -> LAST_ACK\n");
1845 }
else if (in_state(ESTABLISHED)) {
1846 tcp_debug(
"close: ESTABLISHED -> FIN_WAIT_1\n");
1847 _state = FIN_WAIT_1;
1858template <
typename InetTraits>
1859bool tcp<InetTraits>::tcb::should_send_ack(uint16_t seg_len) {
1861 if (seg_len > _rcv.mss) {
1862 _nr_full_seg_received = 0;
1863 _delayed_ack.cancel();
1868 if (seg_len == _rcv.mss) {
1869 if (_nr_full_seg_received++ >= 1) {
1870 _nr_full_seg_received = 0;
1871 _delayed_ack.cancel();
1877 if (_delayed_ack.armed()) {
1884 _delayed_ack.arm(200ms);
1888template <
typename InetTraits>
1889void tcp<InetTraits>::tcb::clear_delayed_ack() noexcept {
1890 _delayed_ack.cancel();
1893template <
typename InetTraits>
1894bool tcp<InetTraits>::tcb::merge_out_of_order() {
1895 bool merged =
false;
1896 if (_rcv.out_of_order.map.empty()) {
1899 for (
auto it = _rcv.out_of_order.map.begin(); it != _rcv.out_of_order.map.end();) {
1900 auto& p = it->second;
1901 auto seg_beg = it->first;
1902 auto seg_len = p.len();
1903 auto seg_end = seg_beg + seg_len;
1904 if (seg_beg <= _rcv.next && _rcv.next < seg_end) {
1907 auto trim = _rcv.next - seg_beg;
1912 _rcv.next += seg_len;
1913 _rcv.data_size += p.len();
1914 _rcv.data.push_back(std::move(p));
1916 it = _rcv.out_of_order.map.erase(it);
1918 }
else if (_rcv.next >= seg_end) {
1920 it = _rcv.out_of_order.map.erase(it);
1931template <
typename InetTraits>
1932void tcp<InetTraits>::tcb::insert_out_of_order(tcp_seq seg, packet p) {
1933 _rcv.out_of_order.merge(seg, std::move(p));
1936template <
typename InetTraits>
1937void tcp<InetTraits>::tcb::trim_receive_data_after_window() {
1941template <
typename InetTraits>
1942void tcp<InetTraits>::tcb::persist() {
1943 tcp_debug(
"persist timer fired\n");
1945 _snd.window_probe =
true;
1946 _snd.zero_window_probing_out++;
1948 _snd.window_probe =
false;
1952 _persist_time_out = std::min(_persist_time_out * 2, _rto_max);
1953 start_persist_timer();
1956template <
typename InetTraits>
1957void tcp<InetTraits>::tcb::retransmit() {
1958 auto output_update_rto = [
this] {
1961 this->_rto = std::min(this->_rto * 2, this->_rto_max);
1962 start_retransmit_timer();
1966 if (syn_needs_on()) {
1967 if (_snd.syn_retransmit++ < _max_nr_retransmit) {
1968 output_update_rto();
1970 _connect_done.set_exception(tcp_connect_error());
1977 if (fin_needs_on()) {
1978 if (_snd.fin_retransmit++ < _max_nr_retransmit) {
1979 output_update_rto();
1987 if (_snd.data.empty()) {
1992 auto& unacked_seg = _snd.data.front();
1996 uint32_t smss = _snd.mss;
1997 if (unacked_seg.nr_transmits == 0) {
1998 _snd.ssthresh = std::max(flight_size() / 2, 2 * smss);
2001 _snd.recover = _snd.next - 1;
2005 exit_fast_recovery();
2007 if (unacked_seg.nr_transmits < _max_nr_retransmit) {
2008 unacked_seg.nr_transmits++;
2016 output_update_rto();
2019template <
typename InetTraits>
2020void tcp<InetTraits>::tcb::fast_retransmit() {
2021 if (!_snd.data.empty()) {
2022 auto& unacked_seg = _snd.data.front();
2023 unacked_seg.nr_transmits++;
2029template <
typename InetTraits>
2030void tcp<InetTraits>::tcb::update_rto(clock_type::time_point tx_time) {
2032 auto R = std::chrono::duration_cast<std::chrono::milliseconds>(
clock_type::now() - tx_time);
2033 if (_snd.first_rto_sample) {
2034 _snd.first_rto_sample =
false;
2037 _snd.rttvar = R / 2;
2043 auto delta = _snd.srtt > R ? (_snd.srtt - R) : (R - _snd.srtt);
2044 _snd.rttvar = _snd.rttvar * 3 / 4 + delta / 4;
2045 _snd.srtt = _snd.srtt * 7 / 8 + R / 8;
2048 _rto = _snd.srtt + std::max(_rto_clk_granularity, 4 * _snd.rttvar);
2051 _rto = std::max(_rto, _rto_min);
2052 _rto = std::min(_rto, _rto_max);
2055template <
typename InetTraits>
2056void tcp<InetTraits>::tcb::update_cwnd(uint32_t acked_bytes) {
2057 uint32_t smss = _snd.mss;
2058 if (_snd.cwnd < _snd.ssthresh) {
2060 _snd.cwnd += std::min(acked_bytes, smss);
2063 uint32_t round_up = 1;
2064 _snd.cwnd += std::max(round_up, smss * smss / _snd.cwnd);
2068template <
typename InetTraits>
2069void tcp<InetTraits>::tcb::cleanup() {
2070 _snd.unsent.clear();
2072 _rcv.out_of_order.map.clear();
2075 stop_retransmit_timer();
2076 clear_delayed_ack();
2080template <
typename InetTraits>
2081tcp_seq tcp<InetTraits>::tcb::get_isn() {
2086 using namespace std::chrono;
2088 hash[0] = _local_ip.ip;
2089 hash[1] = _foreign_ip.ip;
2090 hash[2] = (_local_port << 16) + _foreign_port;
2091 gnutls_hash_hd_t md5_hash_handle;
2093 gnutls_hash_init(&md5_hash_handle, GNUTLS_DIG_MD5);
2094 gnutls_hash(md5_hash_handle, hash, 3 *
sizeof(hash[0]));
2095 gnutls_hash(md5_hash_handle, _isn_secret.key,
sizeof(_isn_secret.key));
2097 assert(
sizeof(hash) == gnutls_hash_get_len(GNUTLS_DIG_MD5));
2098 gnutls_hash_deinit(md5_hash_handle, hash);
2100 auto m = duration_cast<microseconds>(
clock_type::now().time_since_epoch());
2101 seq += m.count() / 4;
2102 return make_seq(seq);
2105template <
typename InetTraits>
2106std::optional<typename InetTraits::l4packet> tcp<InetTraits>::tcb::get_packet() {
2107 _poll_active =
false;
2108 if (_packetq.empty()) {
2112 if (in_state(CLOSED)) {
2113 return std::optional<typename InetTraits::l4packet>();
2116 assert(!_packetq.empty());
2118 auto p = std::move(_packetq.front());
2119 _packetq.pop_front();
2120 if (!_packetq.empty() || (_snd.dupacks < 3 && can_send() > 0 && (_snd.window > 0))) {
2130template <
typename InetTraits>
2131void tcp<InetTraits>::connection::close_read() noexcept {
2132 _tcb->abort_reader();
2135template <
typename InetTraits>
2136void tcp<InetTraits>::connection::close_write() noexcept {
2140template <
typename InetTraits>
2141void tcp<InetTraits>::connection::shutdown_connect() {
2142 if (_tcb->syn_needs_on()) {
2143 _tcb->_connect_done.set_exception(tcp_refused_error());
2151template <
typename InetTraits>
2152typename tcp<InetTraits>::tcb::isn_secret tcp<InetTraits>::tcb::_isn_secret;
bool try_wait(size_t nr=1) noexcept
Definition: semaphore.hh:434
void signal(size_t nr=1) noexcept
Definition: semaphore.hh:396
Definition: shared_ptr.hh:148
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
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:990
void set_exception(std::exception_ptr &&ex) noexcept
Marks the promise as failed.
Definition: future.hh:998
size_t size() const noexcept
Returns the number of items currently in the queue.
Definition: queue.hh:109
future< T > pop_eventually() noexcept
Definition: queue.hh:225
size_t max_size() const noexcept
Definition: queue.hh:117
void abort(std::exception_ptr ex) noexcept
Definition: queue.hh:131
Definition: socket_defs.hh:47
void rearm(time_point until, std::optional< duration > period={}) noexcept
Definition: timer.hh:167
future< T > get_future() noexcept
Gets the promise's associated future.
Definition: future.hh:1926
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