5G-LENA nr-v3.3-120-gdac69c56
The 5G/NR module for the ns-3 simulator
Loading...
Searching...
No Matches
traffic-generator-example.cc
1// Copyright (c) 2022 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
2// Copyright (c) 2023 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3//
4// SPDX-License-Identifier: GPL-2.0-only
5
12#include "ns3/abort.h"
13#include "ns3/config.h"
14#include "ns3/core-module.h"
15#include "ns3/inet-socket-address.h"
16#include "ns3/internet-stack-helper.h"
17#include "ns3/ipv4-address-helper.h"
18#include "ns3/ipv4-global-routing-helper.h"
19#include "ns3/log.h"
20#include "ns3/packet-sink-helper.h"
21#include "ns3/packet-sink.h"
22#include "ns3/ping-helper.h"
23#include "ns3/simple-channel.h"
24#include "ns3/simple-net-device.h"
25#include "ns3/simulator.h"
26#include "ns3/traffic-generator-ftp-single.h"
27#include "ns3/traffic-generator-helper.h"
28#include "ns3/traffic-generator-ngmn-ftp-multi.h"
29#include "ns3/traffic-generator-ngmn-gaming.h"
30#include "ns3/traffic-generator-ngmn-video.h"
31#include "ns3/traffic-generator-ngmn-voip.h"
32
33#include <fstream>
34#include <ostream>
35
36using namespace ns3;
37
38enum TrafficType
39{
40 NGMN_FTP,
41 NGMN_VIDEO,
42 NGMN_GAMING,
43 NGMN_VOIP
44};
45
46static inline std::istream&
47operator>>(std::istream& is, TrafficType& item)
48{
49 uint32_t inputValue;
50 is >> inputValue;
51 item = (TrafficType)inputValue;
52 return is;
53}
54
55TypeId
56GetTypeId(const TrafficType& item)
57{
58 switch (item)
59 {
60 case NGMN_FTP:
62 case NGMN_VIDEO:
64 case NGMN_GAMING:
66 case NGMN_VOIP:
68 default:
69 NS_ABORT_MSG("Unknown traffic type");
70 };
71}
72
73std::string
74GetName(const TrafficType& item)
75{
76 switch (item)
77 {
78 case NGMN_FTP:
79 return "ftp";
80 case NGMN_VIDEO:
81 return "video";
82 case NGMN_GAMING:
83 return "gaming";
84 case NGMN_VOIP:
85 return "voip";
86 default:
87 NS_ABORT_MSG("Unknown traffic type");
88 };
89}
90
91void
92WriteBytesSent(Ptr<TrafficGenerator> trafficGenerator,
93 uint64_t* previousBytesSent,
94 uint64_t* previousWindowBytesSent,
95 enum TrafficType trafficType,
96 std::ofstream* outFileTx)
97{
98 uint64_t totalBytesSent = trafficGenerator->GetTotalBytes();
99 (*outFileTx) << "\n"
100 << Simulator::Now().GetMilliSeconds() << "\t" << *previousWindowBytesSent
101 << std::endl;
102 (*outFileTx) << "\n"
103 << Simulator::Now().GetMilliSeconds() << "\t"
104 << totalBytesSent - *previousBytesSent << std::endl;
105
106 *previousWindowBytesSent = totalBytesSent - *previousBytesSent;
107 *previousBytesSent = totalBytesSent;
108};
109
110void
111WriteBytesReceived(Ptr<PacketSink> packetSink, uint64_t* previousBytesReceived)
112{
113 uint64_t totalBytesReceived = packetSink->GetTotalRx();
114 *previousBytesReceived = totalBytesReceived;
115};
116
117int
118main(int argc, char* argv[])
119{
120 enum TrafficType trafficType = NGMN_FTP;
121 bool useUdp = false;
122 uint32_t measWindowMs = 10;
123 uint32_t appStartMs = 500;
124 uint32_t appDurationMs = 100;
125
126 CommandLine cmd(__FILE__);
127 cmd.AddValue("trafficType",
128 "The traffic type to be configured. Currently the following options are "
129 "available: 0 (ftp), 1 (video), 2 (gaming) and 3 (voip).",
130 trafficType);
131 cmd.AddValue("useUdp",
132 "if true, the NGMN applications will run over UDP connection, otherwise a TCP "
133 "connection will be used. ",
134 useUdp);
135 cmd.AddValue("appStartMs", "Application start time in ms, greater than 500", appStartMs);
136 cmd.AddValue("appDurationMs",
137 "Application duration time in ms, greater than 100",
138 appDurationMs);
139 cmd.AddValue("measWindowMs", "Measurement window time in ms, greathen than 10", measWindowMs);
140
141 cmd.Parse(argc, argv);
142
143 NS_ASSERT(appStartMs >= 500);
144 NS_ASSERT(appDurationMs >= 100);
145 NS_ASSERT(measWindowMs >= 10);
146
147 // configure the transport protocol to be used
148 std::string transportProtocol;
149 if (useUdp)
150 {
151 transportProtocol = "ns3::UdpSocketFactory";
152 }
153 else
154 {
155 transportProtocol = "ns3::TcpSocketFactory";
156 }
157
158 NodeContainer nodes;
159 nodes.Create(2);
160 InternetStackHelper internet;
161 internet.Install(nodes);
162 // link the two nodes
163 Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice>();
164 Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice>();
165 nodes.Get(0)->AddDevice(txDev);
166 nodes.Get(1)->AddDevice(rxDev);
167 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
168 rxDev->SetChannel(channel1);
169 txDev->SetChannel(channel1);
170 NetDeviceContainer devices;
171 devices.Add(txDev);
172 devices.Add(rxDev);
173 Ipv4AddressHelper ipv4;
174 ipv4.SetBase("10.1.1.0", "255.255.255.0");
175 Ipv4InterfaceContainer ipv4Interfaces = ipv4.Assign(devices);
176
177 // install the packet sink at the receiver node
178 uint16_t port = 4000;
179 InetSocketAddress rxAddress(Ipv4Address::GetAny(), port);
180
181 PacketSinkHelper packetSinkHelper(transportProtocol, rxAddress);
182
183 // install the application on the rx device
184 ApplicationContainer sinkApplication = packetSinkHelper.Install(nodes.Get(1));
185 sinkApplication.Start(MilliSeconds(appStartMs));
186 sinkApplication.Stop(MilliSeconds(appStartMs + appDurationMs));
187
188 // install the traffic generator at the transmitter node
189 TrafficGeneratorHelper trafficGeneratorHelper(
190 transportProtocol,
191 InetSocketAddress(ipv4Interfaces.GetAddress(1, 0), port),
192 GetTypeId(trafficType));
193
194 ApplicationContainer generatorApplication = trafficGeneratorHelper.Install(nodes.Get(0));
195 generatorApplication.Start(MilliSeconds(appStartMs));
196 generatorApplication.Stop(MilliSeconds(appStartMs + appDurationMs));
197
198 // Seed the ARP cache by pinging early in the simulation
199 // This is a workaround until a static ARP capability is provided
200 PingHelper pingHelper(ipv4Interfaces.GetAddress(1, 0));
201 ApplicationContainer pingApps = pingHelper.Install(nodes.Get(0));
202 pingApps.Start(MilliSeconds(10));
203 pingApps.Stop(MilliSeconds(500));
204
205 Ptr<TrafficGenerator> trafficGenerator =
206 generatorApplication.Get(0)->GetObject<TrafficGenerator>();
207 Ptr<PacketSink> packetSink = sinkApplication.Get(0)->GetObject<PacketSink>();
208
209 uint64_t previousBytesSent = 0;
210 uint64_t previousBytesReceived = 0;
211 uint64_t previousWindowBytesSent = 0;
212
213 std::ofstream outFileTx;
214 std::string txFileName = "tx-" + GetName(trafficType) + ".csv";
215 outFileTx.open(txFileName.c_str(), std::ios_base::out);
216 ;
217 NS_ABORT_MSG_IF(!outFileTx.is_open(), "Can't open file " << txFileName);
218 outFileTx.setf(std::ios_base::fixed);
219
220 for (uint32_t i = appStartMs; i < appStartMs + appDurationMs; i = i + measWindowMs)
221 {
222 Simulator::Schedule(MilliSeconds(i),
223 &WriteBytesSent,
224 trafficGenerator,
225 &previousBytesSent,
226 &previousWindowBytesSent,
227 trafficType,
228 &outFileTx);
229 Simulator::Schedule(MilliSeconds(i),
230 &WriteBytesReceived,
231 packetSink,
232 &previousBytesReceived);
233 }
234
235 Simulator::Stop(MilliSeconds(appStartMs + appDurationMs));
236 Simulator::Run();
237 Simulator::Destroy();
238
239 outFileTx.close();
240 std::cout << "\n Traffic generator example finished. Results written into " << txFileName
241 << " file in the ns-3-dev root directory." << std::endl;
242
243 return 0;
244}
A helper to make it easier to instantiate an ns3::TrafficGenerator types of applications on a set of ...
static TypeId GetTypeId()
Get the type ID.
static TypeId GetTypeId()
Get the type ID.
static TypeId GetTypeId()
Get the type ID.