Skip to content

Commit d783017

Browse files
authored
Merge pull request #865 from AndreasAakesson/dev
Added Timer helper class and TCP improvements
2 parents 04ca28a + f1cca82 commit d783017

12 files changed

Lines changed: 297 additions & 138 deletions

File tree

api/kernel/cpuid.hpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
88
// You may obtain a copy of the License at
9-
//
9+
//
1010
// http://www.apache.org/licenses/LICENSE-2.0
11-
//
11+
//
1212
// Unless required by applicable law or agreed to in writing, software
1313
// distributed under the License is distributed on an "AS IS" BASIS,
1414
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -96,9 +96,22 @@ namespace CPUID
9696
LM, // Long mode (64-bit Architecture)
9797
};
9898

99-
bool isAmdCpu();
100-
bool isIntelCpu();
101-
bool hasFeature(Feature f);
99+
bool is_amd_cpu();
100+
bool is_intel_cpu();
101+
bool has_feature(Feature f);
102+
103+
unsigned kvm_function();
104+
bool kvm_feature(unsigned id);
102105
} //< CPUID
103106

107+
#define KVM_FEATURE_CLOCKSOURCE 0
108+
#define KVM_FEATURE_NOP_IO_DELAY 1
109+
#define KVM_FEATURE_MMU_OP 2 /* deprecated */
110+
#define KVM_FEATURE_CLOCKSOURCE2 3
111+
#define KVM_FEATURE_ASYNC_PF 4
112+
#define KVM_FEATURE_STEAL_TIME 5
113+
#define KVM_FEATURE_PV_EOI 6
114+
#define KVM_FEATURE_PV_UNHALT 7
115+
#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24
116+
104117
#endif //< KERNEL_CPUID_HPP

api/kernel/timers.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
99
// You may obtain a copy of the License at
10-
//
10+
//
1111
// http://www.apache.org/licenses/LICENSE-2.0
12-
//
12+
//
1313
// Unless required by applicable law or agreed to in writing, software
1414
// distributed under the License is distributed on an "AS IS" BASIS,
1515
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -28,28 +28,30 @@
2828
class Timers
2929
{
3030
public:
31-
using id_t = uint32_t;
31+
using id_t = int32_t;
3232
using duration_t = std::chrono::microseconds;
3333
using handler_t = delegate<void(id_t)>;
34-
34+
35+
static constexpr id_t UNUSED_ID = -1;
36+
3537
/// create a one-shot timer that triggers @when from now
3638
/// returns a timer id
3739
static id_t oneshot(duration_t when, const handler_t&);
3840
/// create a periodic timer that begins @when and repeats every @period
3941
static id_t periodic(duration_t when, duration_t period, const handler_t&);
4042
// un-schedule timer, and free it
4143
static void stop(id_t);
42-
44+
4345
/// returns the number of current, active timers
4446
static size_t active();
45-
47+
4648
/// initialization
4749
typedef delegate<void(duration_t)> start_func_t;
4850
typedef delegate<void()> stop_func_t;
4951
static void init(const start_func_t&, const stop_func_t&);
5052
/// signal from the underlying hardware that it is calibrated and ready to go
5153
static void ready();
52-
54+
5355
/// handler that processes timer interrupts
5456
static void timers_handler();
5557
};

api/net/tcp/connection.hpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "tcp_errors.hpp"
2727
#include "write_queue.hpp"
2828
#include <delegate>
29+
#include <util/timer.hpp>
2930

3031
namespace net {
3132
class TCP;
@@ -282,7 +283,7 @@ class Connection : public std::enable_shared_from_this<Connection> {
282283
virtual size_t send(Connection&, WriteBuffer&);
283284

284285
/** Read from a Connection [RECEIVE] */
285-
virtual void receive(Connection&, ReadBuffer&);
286+
virtual void receive(Connection&, ReadBuffer&&);
286287

287288
/** Close a Connection [CLOSE] */
288289
virtual void close(Connection&);
@@ -477,20 +478,11 @@ class Connection : public std::enable_shared_from_this<Connection> {
477478
/** State if connection is in TCP write queue or not. */
478479
bool queued_;
479480

480-
/** When time-wait timer was started. Used in start_time_wait_timeout */
481-
uint64_t time_wait_started;
482-
483481
/** Retransmission timer */
484-
struct {
485-
uint32_t id;
486-
bool active = false;
487-
} rtx_timer;
482+
Timer rtx_timer;
488483

489484
/** Time Wait timeout timer */
490-
struct {
491-
uint32_t id;
492-
bool active = false;
493-
} timewait_timer;
485+
Timer timewait_timer;
494486

495487
/** Number of retransmission attempts on the packet first in RT-queue */
496488
size_t rtx_attempt_ = 0;
@@ -550,8 +542,7 @@ class Connection : public std::enable_shared_from_this<Connection> {
550542
Buffer is cleared for data after every reset.
551543
*/
552544
void read(size_t n, ReadCallback callback) {
553-
ReadBuffer buffer = {new_shared_buffer(n), n};
554-
read(buffer, callback);
545+
read({new_shared_buffer(n), n}, callback);
555546
}
556547

557548
/*
@@ -561,13 +552,13 @@ class Connection : public std::enable_shared_from_this<Connection> {
561552
void read(buffer_t buffer, size_t n, ReadCallback callback)
562553
{ read({buffer, n}, callback); }
563554

564-
void read(ReadBuffer buffer, ReadCallback callback);
555+
void read(ReadBuffer&& buffer, ReadCallback callback);
565556

566557
/*
567558
Assign the read request (read buffer)
568559
*/
569-
void receive(ReadBuffer& buffer)
570-
{ read_request.buffer = {buffer}; }
560+
void receive(ReadBuffer&& buffer)
561+
{ read_request = {buffer}; }
571562

572563
/*
573564
Receive data into the current read requests buffer.
@@ -853,20 +844,20 @@ class Connection : public std::enable_shared_from_this<Connection> {
853844
/*
854845
Start retransmission timer.
855846
*/
856-
void rtx_start();
847+
void rtx_start()
848+
{ rtx_timer.start(rttm.rto_ms()); }
857849

858850
/*
859851
Stop retransmission timer.
860852
*/
861-
void rtx_stop();
853+
void rtx_stop()
854+
{ rtx_timer.stop(); }
862855

863856
/*
864857
Restart retransmission timer.
865858
*/
866-
void rtx_reset() {
867-
rtx_stop();
868-
rtx_start();
869-
}
859+
void rtx_reset()
860+
{ rtx_timer.restart(rttm.rto_ms()); }
870861

871862
/*
872863
Retransmission timeout limit reached
@@ -887,8 +878,7 @@ class Connection : public std::enable_shared_from_this<Connection> {
887878
/*
888879
When retransmission times out.
889880
*/
890-
void rtx_timeout(uint32_t);
891-
881+
void rtx_timeout();
892882

893883
/** Start the timewait timeout for 2*MSL */
894884
void timewait_start();
@@ -900,7 +890,7 @@ class Connection : public std::enable_shared_from_this<Connection> {
900890
void timewait_restart();
901891

902892
/** When timewait timer times out */
903-
void timewait_timeout(uint32_t);
893+
void timewait_timeout();
904894

905895
/*
906896
Tell the host (TCP) to delete this connection.

api/net/tcp/connection_states.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class Connection::Established : public State {
169169

170170
virtual size_t send(Connection&, WriteBuffer&) override;
171171

172-
virtual void receive(Connection&, ReadBuffer&) override;
172+
virtual void receive(Connection&, ReadBuffer&&) override;
173173

174174
virtual void close(Connection&) override;
175175

@@ -207,7 +207,7 @@ class Connection::FinWait1 : public State {
207207
return instance;
208208
}
209209

210-
virtual void receive(Connection&, ReadBuffer&) override;
210+
virtual void receive(Connection&, ReadBuffer&&) override;
211211

212212
virtual void close(Connection&) override;
213213

@@ -246,7 +246,7 @@ class Connection::FinWait2 : public State {
246246
return instance;
247247
}
248248

249-
virtual void receive(Connection&, ReadBuffer&) override;
249+
virtual void receive(Connection&, ReadBuffer&&) override;
250250

251251
virtual void close(Connection&) override;
252252

@@ -284,7 +284,7 @@ class Connection::CloseWait : public State {
284284

285285
virtual size_t send(Connection&, WriteBuffer&) override;
286286

287-
virtual void receive(Connection&, ReadBuffer&) override;
287+
virtual void receive(Connection&, ReadBuffer&&) override;
288288

289289
virtual void close(Connection&) override;
290290

api/net/tcp/read_buffer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct ReadBuffer {
7979
void renew() {
8080
remaining = capacity();
8181
offset = 0;
82-
buffer = buffer_t(new uint8_t[remaining], std::default_delete<uint8_t[]>());
82+
buffer = new_shared_buffer(remaining);
8383
push = false;
8484
}
8585
}; // < ReadBuffer

api/net/tcp/read_request.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@ struct ReadRequest {
3131
ReadBuffer buffer;
3232
ReadCallback callback;
3333

34-
/*ReadRequest()
35-
: buffer(nullptr, 0),
36-
callback({this, &ReadRequest::default_read_callback})
37-
{}*/
34+
ReadRequest()
35+
: buffer{nullptr, 0},
36+
callback{nullptr}
37+
{}
38+
39+
ReadRequest(ReadBuffer buf)
40+
: ReadRequest(buf, nullptr)
41+
{}
3842

3943
ReadRequest(ReadBuffer buf, ReadCallback cb)
4044
: buffer(buf),
4145
callback(cb)
4246
{}
4347

44-
ReadRequest(size_t n = 0)
45-
: buffer(buffer_t(new uint8_t[n], std::default_delete<uint8_t[]>()), n),
48+
ReadRequest(size_t n)
49+
: buffer({new_shared_buffer(n), n}),
4650
callback({this, &ReadRequest::default_read_callback})
4751
{}
4852

api/net/tcp/rttm.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ struct RTTM {
5757

5858
void stop(bool first = false);
5959

60+
auto rto_ms() const
61+
{ return std::chrono::milliseconds{static_cast<unsigned long>(RTO * 1000)}; }
62+
6063
/*
6164
When the first RTT measurement R is made, the host MUST set
6265
@@ -66,7 +69,7 @@ struct RTTM {
6669
6770
where K = 4.
6871
*/
69-
inline void first_rtt_measurement(duration_t R) {
72+
void first_rtt_measurement(duration_t R) {
7073
SRTT = R;
7174
RTTVAR = R/2;
7275
update_rto();
@@ -89,19 +92,18 @@ struct RTTM {
8992
After the computation, a host MUST update
9093
RTO <- SRTT + max (G, K*RTTVAR)
9194
*/
92-
inline void sub_rtt_measurement(duration_t R) {
95+
void sub_rtt_measurement(duration_t R) {
9396
RTTVAR = (1 - beta) * RTTVAR + beta * std::abs(SRTT-R);
9497
SRTT = (1 - alpha) * SRTT + alpha * R;
9598
update_rto();
9699
}
97100

98-
inline void update_rto() {
101+
void update_rto() {
99102
RTO = std::max(SRTT + std::max(CLOCK_G, K * RTTVAR), 1.0);
100103
debug2("<TCP::Connection::RTO> RTO updated: %ums\n",
101104
(uint32_t)(RTO * 1000));
102105
}
103-
104-
}; // < struct RTTM
106+
} __attribute__((packed)); // < struct RTTM
105107

106108
} // < namespace tcp
107109
} // < namespace net

0 commit comments

Comments
 (0)