-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcp-rl-env.h
190 lines (148 loc) · 5.49 KB
/
tcp-rl-env.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef TCP_RL_ENV_H
#define TCP_RL_ENV_H
#include "ns3/opengym-module.h"
#include "ns3/tcp-socket-base.h"
#include <vector>
namespace ns3 {
class Packet;
class TcpHeader;
class TcpSocketBase;
class Time;
class TcpGymEnv : public OpenGymEnv
{
public:
TcpGymEnv ();
virtual ~TcpGymEnv ();
static TypeId GetTypeId (void);
virtual void DoDispose ();
void SetNodeId(uint32_t id);
void SetSocketUuid(uint32_t id);
std::string GetTcpCongStateName(const TcpSocketState::TcpCongState_t state);
std::string GetTcpCAEventName(const TcpSocketState::TcpCAEvent_t event);
// OpenGym interface
virtual Ptr<OpenGymSpace> GetActionSpace();
virtual bool GetGameOver();
virtual float GetReward();
virtual std::string GetExtraInfo();
virtual bool ExecuteActions(Ptr<OpenGymDataContainer> action);
virtual Ptr<OpenGymSpace> GetObservationSpace() = 0;
virtual Ptr<OpenGymDataContainer> GetObservation() = 0;
// trace packets, e.g. for calculating inter tx/rx time
virtual void TxPktTrace(Ptr<const Packet>, const TcpHeader&, Ptr<const TcpSocketBase>) = 0;
virtual void RxPktTrace(Ptr<const Packet>, const TcpHeader&, Ptr<const TcpSocketBase>) = 0;
// TCP congestion control interface
virtual uint32_t GetSsThresh (Ptr<const TcpSocketState> tcb, uint32_t bytesInFlight) = 0;
virtual void IncreaseWindow (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked) = 0;
// optional functions used to collect obs
virtual void PktsAcked (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked, const Time& rtt) = 0;
virtual void CongestionStateSet (Ptr<TcpSocketState> tcb, const TcpSocketState::TcpCongState_t newState) = 0;
virtual void CwndEvent (Ptr<TcpSocketState> tcb, const TcpSocketState::TcpCAEvent_t event) = 0;
typedef enum
{
GET_SS_THRESH = 0,
INCREASE_WINDOW,
PKTS_ACKED,
CONGESTION_STATE_SET,
CWND_EVENT,
} CalledFunc_t;
protected:
uint32_t m_nodeId;
uint32_t m_socketUuid;
// state
// obs has to be implemented in child class
// game over
bool m_isGameOver;
// reward
float m_envReward;
// extra info
std::string m_info;
// actions
uint32_t m_new_ssThresh;
uint32_t m_new_cWnd;
};
class TcpEventGymEnv : public TcpGymEnv
{
public:
TcpEventGymEnv ();
virtual ~TcpEventGymEnv ();
static TypeId GetTypeId (void);
virtual void DoDispose ();
void SetReward(float value);
void SetPenalty(float value);
// OpenGym interface
virtual Ptr<OpenGymSpace> GetObservationSpace();
Ptr<OpenGymDataContainer> GetObservation();
// trace packets, e.g. for calculating inter tx/rx time
virtual void TxPktTrace(Ptr<const Packet>, const TcpHeader&, Ptr<const TcpSocketBase>);
virtual void RxPktTrace(Ptr<const Packet>, const TcpHeader&, Ptr<const TcpSocketBase>);
// TCP congestion control interface
virtual uint32_t GetSsThresh (Ptr<const TcpSocketState> tcb, uint32_t bytesInFlight);
virtual void IncreaseWindow (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked);
// optional functions used to collect obs
virtual void PktsAcked (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked, const Time& rtt);
virtual void CongestionStateSet (Ptr<TcpSocketState> tcb, const TcpSocketState::TcpCongState_t newState);
virtual void CwndEvent (Ptr<TcpSocketState> tcb, const TcpSocketState::TcpCAEvent_t event);
private:
// state
CalledFunc_t m_calledFunc;
Ptr<const TcpSocketState> m_tcb;
uint32_t m_bytesInFlight;
uint32_t m_segmentsAcked;
Time m_rtt;
TcpSocketState::TcpCongState_t m_newState;
TcpSocketState::TcpCAEvent_t m_event;
// reward
float m_reward;
float m_penalty;
};
class TcpTimeStepGymEnv : public TcpGymEnv
{
public:
TcpTimeStepGymEnv ();
virtual ~TcpTimeStepGymEnv ();
static TypeId GetTypeId (void);
virtual void DoDispose ();
void SetDuration(Time value);
void SetTimeStep(Time value);
void SetReward(float value);
void SetPenalty(float value);
// OpenGym interface
virtual Ptr<OpenGymSpace> GetObservationSpace();
Ptr<OpenGymDataContainer> GetObservation();
// trace packets, e.g. for calculating inter tx/rx time
virtual void TxPktTrace(Ptr<const Packet>, const TcpHeader&, Ptr<const TcpSocketBase>);
virtual void RxPktTrace(Ptr<const Packet>, const TcpHeader&, Ptr<const TcpSocketBase>);
// TCP congestion control interface
virtual uint32_t GetSsThresh (Ptr<const TcpSocketState> tcb, uint32_t bytesInFlight);
virtual void IncreaseWindow (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked);
// optional functions used to collect obs
virtual void PktsAcked (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked, const Time& rtt);
virtual void CongestionStateSet (Ptr<TcpSocketState> tcb, const TcpSocketState::TcpCongState_t newState);
virtual void CwndEvent (Ptr<TcpSocketState> tcb, const TcpSocketState::TcpCAEvent_t event);
private:
void ScheduleNextStateRead();
bool m_started {false};
Time m_duration;
Time m_timeStep;
// state
Ptr<const TcpSocketState> m_tcb;
std::vector<uint32_t> m_bytesInFlight;
std::vector<uint32_t> m_segmentsAcked;
uint64_t m_rttSampleNum {0};
Time m_rttSum {MicroSeconds (0.0)};
Time m_lastPktTxTime {MicroSeconds(0.0)};
Time m_lastPktRxTime {MicroSeconds(0.0)};
uint64_t m_interTxTimeNum {0};
Time m_interTxTimeSum {MicroSeconds (0.0)};
uint64_t m_interRxTimeNum {0};
Time m_interRxTimeSum {MicroSeconds (0.0)};
Time m_prevAvgRtt {MicroSeconds (0.0)};
Time m_totalAvgRttSum {MicroSeconds (0.0)};
uint64_t m_totalAvgRttNum {0};
uint32_t m_old_cWnd {0};
// reward
float m_reward;
float m_penalty;
};
} // namespace ns3
#endif /* TCP_RL_ENV_H */