18#include "nlohmann/json.hpp"
20#include "ns3/address.h"
21#include "ns3/application-container.h"
22#include "ns3/boolean.h"
23#include "ns3/ipv4-l3-protocol.h"
24#include "ns3/loopback-net-device.h"
25#include "ns3/string.h"
26#include "ns3/tcp-socket-factory.h"
27#include "ns3/trace-helper.h"
28#include "ns3/uinteger.h"
61 TypeId(
"ns3::FlentApplication")
63 .SetGroupName(
"Applications")
65 .AddAttribute(
"TestName",
66 "Name of the Flent test to be run",
70 .AddAttribute(
"Length",
75 .AddAttribute(
"HostAddress",
76 "The address of the remote host",
80 .AddAttribute(
"LocalBindAddress",
81 "The address of the local host",
85 .AddAttribute(
"ImageText",
86 "Text to be included in the plot",
90 .AddAttribute(
"OutputFilename",
91 "Path to the .flent output file. "
92 "The path can be absolute or relative to the working directory. "
93 "If it is empty (the default), the file is written as "
94 "'TestName.flent' in the working directory.",
99 "Absolute start time, as an offset from the Unix epoch, used to "
100 "anchor all timestamps in the output file. The default "
101 "corresponds to 2026-01-01T00:00:00Z. Any fixed value keeps the "
102 "output byte-for-byte reproducible across runs. Ignored if "
103 "UseWallClockT0 is true.",
107 .AddAttribute(
"UseWallClockT0",
108 "If true, read the system wall clock once at application start "
109 "and use it in place of the T0 attribute as the timestamp "
110 "anchor. Output files are then not reproducible across runs.",
114 .AddAttribute(
"StepSize",
115 "Measurement data point size",
139 for (
uint32_t deviceId = 0; deviceId <
m_node->GetNDevices(); deviceId++)
149 int32_t interfaceIndex = (ip)->GetInterfaceForDevice(device);
151 if (interfaceIndex != -1)
210 std::chrono::duration<double> duration(
m_currTime);
211 auto wholeSeconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
213 std::chrono::duration_cast<std::chrono::microseconds>(duration - wholeSeconds);
215 std::chrono::system_clock::time_point tp(wholeSeconds);
216 std::time_t t = std::chrono::system_clock::to_time_t(tp);
218 std::ostringstream oss;
219 oss << std::put_time(std::gmtime(&t),
"%Y-%m-%dT%H:%M:%S") <<
"." << std::setfill(
'0')
220 << std::setw(6) << microseconds.count() <<
"Z";
230 std::string dataFilename = std::filesystem::path(outputPath).filename().string();
232 std::ostringstream oss;
243 NS_FATAL_ERROR(
"HostAddress must be an Ipv4Address or InetSocketAddress");
245 std::string hostName = oss.str();
247 std::ostringstream ossLocal;
249 std::string localHost = ossLocal.str();
253 nlohmann::json title =
nullptr;
260 {
"BATCH_NAME",
nullptr},
261 {
"BATCH_TIME",
nullptr},
262 {
"BATCH_TITLE",
nullptr},
263 {
"BATCH_UUID",
nullptr},
264 {
"DATA_FILENAME", dataFilename},
265 {
"EGRESS_INFO", {{
"bql", {{
"tx-0",
""}}}}},
266 {
"classes",
nullptr},
269 {
"link_params", {{
"qlen",
nullptr}}},
271 {{
"generic-receive-offload",
nullptr},
272 {
"generic-segmentation-offload",
nullptr},
273 {
"large-receive-offload",
nullptr},
274 {
"tcp-segmentation",
nullptr},
275 {
"udp-fragmentation",
nullptr}}},
282 {
"interval",
nullptr},
284 {
"memory_limit",
nullptr},
285 {
"quantum",
nullptr},
287 {
"target",
nullptr}}},
288 {
"parent",
nullptr}}},
289 {
"FAILED_RUNNERS",
nullptr},
290 {
"FLENT_VERSION",
nullptr},
292 {
"HOSTS", nlohmann::json::array({hostName})},
293 {
"HTTP_GETTER_DNS",
nullptr},
294 {
"HTTP_GETTER_URLLIST",
nullptr},
295 {
"HTTP_GETTER_WORKERS",
nullptr},
297 {
"KERNEL_NAME",
"ns-3"},
298 {
"KERNEL_RELEASE",
"ns-3"},
300 {
"LOCAL_HOST", localHost},
301 {
"MODULE_VERSIONS",
nullptr},
304 {
"REMOTE_METADATA",
nullptr},
308 {
"TEST_PARAMETERS", nlohmann::json::object()},
331 for (
uint32_t deviceId = 0; deviceId < node->GetNDevices(); deviceId++)
333 int32_t interfaceIndex = (ip)->GetInterfaceForDevice(node->GetDevice(deviceId));
335 if (interfaceIndex == -1)
340 uint32_t numberOfAddresses = ip->GetNAddresses(interfaceIndex);
341 for (
uint32_t addressIndex = 0; addressIndex < numberOfAddresses; addressIndex++)
346 if (addr == hostAddress)
378 m_output->json[
"raw_values"][
"Ping (ms) ICMP"].push_back(
data);
388 constexpr std::array<const char*, 3> udpFlows = {
"Ping (ms) UDP BE",
398 m_output->json[
"raw_values"][udpFlows[idx]].push_back(
data);
408 data[
"val"] = goodput;
424 data[
"val"] = goodput;
440 int totalSteps = int(std::ceil(
m_stopTime.GetSeconds() / stepSize));
442 for (
int step = 0; step < totalSteps; step += 1)
445 m_output->json[
"x_values"].push_back(step * stepSize);
455 for (
int s = 0; s < steps; s++)
459 for (
auto itrName =
m_output->json[
"raw_values"].begin();
460 itrName !=
m_output->json[
"raw_values"].end();
463 const std::string& rawValueName = itrName.key();
464 double maxDist =
m_stepSize.GetSeconds() * 5.0;
466 if (itrName.value().empty() || itrName.value().is_null())
468 m_output->json[
"results"][rawValueName].push_back(
nullptr);
472 bool hasPrev =
false;
473 bool hasNext =
false;
479 auto& arr =
m_output->json[
"raw_values"][rawValueName];
480 auto prev = arr.end();
482 for (
auto itrValues = arr.begin(); itrValues != arr.end();
483 prev = itrValues, itrValues++)
485 if ((*itrValues)[
"t"].get<double>() > t)
487 if (
prev != arr.end())
489 tPrev = (*prev)[
"t"].get<
double>();
490 vPrev = (*prev)[
"val"].get<
double>();
498 tNext = (*itrValues)[
"t"].get<
double>();
499 vNext = (*itrValues)[
"val"].get<
double>();
508 auto lastItr = std::prev(arr.end());
509 tNext = (*lastItr)[
"t"].get<
double>();
510 vNext = (*lastItr)[
"val"].get<
double>();
515 if (std::fabs(t - tNext) <= maxDist)
522 m_output->json[
"results"][rawValueName].push_back(
nullptr);
526 auto& resultArr =
m_output->json[
"results"][rawValueName];
527 bool alreadyRecorded = !resultArr.empty() && (resultArr.back() == vNext ||
528 resultArr.back().is_null());
531 m_output->json[
"results"][rawValueName].push_back(
nullptr);
535 m_output->json[
"results"][rawValueName].push_back(vNext);
540 m_output->json[
"results"][rawValueName].push_back(vNext);
546 if (vPrev == 0.0 || vNext == 0.0)
548 m_output->json[
"results"][rawValueName].push_back(
nullptr);
552 double dvDt = (vNext - vPrev) / (tNext - tPrev);
553 m_output->json[
"results"][rawValueName].push_back(vPrev +
560 m_output->json[
"results"][rawValueName].push_back(
nullptr);
573 auto sinceEpoch = std::chrono::system_clock::now().time_since_epoch();
574 m_currTime = std::chrono::duration_cast<std::chrono::nanoseconds>(sinceEpoch).count() / 1e9;
588 hostIpv4Address = inetAddr.
GetIpv4();
597 NS_FATAL_ERROR(
"HostAddress must be an Ipv4Address or InetSocketAddress");
604 NS_FATAL_ERROR(
"Couldn't find dest node given the IP " << hostIpv4Address);
607 m_output->json[
"raw_values"][
"Ping (ms) ICMP"] = nlohmann::json::array();
608 m_output->json[
"results"][
"Ping (ms) ICMP"] = nlohmann::json::array();
609 m_output->json[
"x_values"] = nlohmann::json::array();
617 hostNode->AddApplication(
m_ping);
630 m_ping->TraceConnectWithoutContext(
"Rtt",
643 m_output->json[
"results"][
"TCP upload"] = nlohmann::json::array();
644 m_output->json[
"raw_values"][
"TCP upload"] = nlohmann::json::array();
649 m_output->json[
"raw_values"][
"TCP upload"].push_back(
data);
681 m_output->json[
"results"][
"TCP download"] = nlohmann::json::array();
682 m_output->json[
"raw_values"][
"TCP download"] = nlohmann::json::array();
687 m_output->json[
"raw_values"][
"TCP download"].push_back(
data);
707 uint16_t
port = basePort - 20;
727 m_output->json[
"raw_values"][
"Ping (ms) UDP BE"] = nlohmann::json::array();
728 m_output->json[
"results"][
"Ping (ms) UDP BE"] = nlohmann::json::array();
730 "RxWithSeqTsEchoHeader",
733 port = basePort - 19;
751 m_output->json[
"raw_values"][
"Ping (ms) UDP BK"] = nlohmann::json::array();
752 m_output->json[
"results"][
"Ping (ms) UDP BK"] = nlohmann::json::array();
754 "RxWithSeqTsEchoHeader",
757 port = basePort - 18;
775 m_output->json[
"raw_values"][
"Ping (ms) UDP EF"] = nlohmann::json::array();
776 m_output->json[
"results"][
"Ping (ms) UDP EF"] = nlohmann::json::array();
778 "RxWithSeqTsEchoHeader",
794 m_output->json[
"results"][
"TCP download BE"] = nlohmann::json::array();
795 m_output->json[
"raw_values"][
"TCP download BE"] = nlohmann::json::array();
800 m_output->json[
"raw_values"][
"TCP download BE"].push_back(
data);
827 m_output->json[
"results"][
"TCP upload BE"] = nlohmann::json::array();
828 m_output->json[
"raw_values"][
"TCP upload BE"] = nlohmann::json::array();
829 nlohmann::json data_up;
833 m_output->json[
"raw_values"][
"TCP upload BE"].push_back(data_up);
865 m_output->json[
"results"][
"TCP download BK"] = nlohmann::json::array();
866 m_output->json[
"raw_values"][
"TCP download BK"] = nlohmann::json::array();
867 nlohmann::json data2;
871 m_output->json[
"raw_values"][
"TCP download BK"].push_back(data2);
897 m_output->json[
"results"][
"TCP upload BK"] = nlohmann::json::array();
898 m_output->json[
"raw_values"][
"TCP upload BK"] = nlohmann::json::array();
899 nlohmann::json data_up2;
903 m_output->json[
"raw_values"][
"TCP upload BK"].push_back(data_up2);
934 m_output->json[
"results"][
"TCP download CS5"] = nlohmann::json::array();
935 m_output->json[
"raw_values"][
"TCP download CS5"] = nlohmann::json::array();
936 nlohmann::json data3;
940 m_output->json[
"raw_values"][
"TCP download CS5"].push_back(data3);
966 m_output->json[
"results"][
"TCP upload CS5"] = nlohmann::json::array();
967 m_output->json[
"raw_values"][
"TCP upload CS5"] = nlohmann::json::array();
968 nlohmann::json data_up3;
972 m_output->json[
"raw_values"][
"TCP upload CS5"].push_back(data_up3);
1002 m_output->json[
"results"][
"TCP download EF"] = nlohmann::json::array();
1003 m_output->json[
"raw_values"][
"TCP download EF"] = nlohmann::json::array();
1004 nlohmann::json data4;
1008 m_output->json[
"raw_values"][
"TCP download EF"].push_back(data4);
1034 m_output->json[
"results"][
"TCP upload EF"] = nlohmann::json::array();
1035 m_output->json[
"raw_values"][
"TCP upload EF"] = nlohmann::json::array();
1036 nlohmann::json data_up4;
1039 data_up4[
"val"] = 0;
1040 m_output->json[
"raw_values"][
"TCP upload EF"].push_back(data_up4);
1068 std::ofstream out(outputPath);
1074 out <<
m_output->json.dump(1) << std::endl;
a polymophic address class
void DoInitialize() override
Initialize() implementation.
void DoDispose() override
Destructor implementation.
Time m_startTime
The simulation time that the application will start.
Time m_stopTime
The simulation time that the application will end.
Ptr< Node > m_node
The node that this application is installed on.
Flent is a network benchmarking tool.
std::string GetUtcFormatTime() const
Calculate the Time in UTC time format from the number of seconds.
double m_currTime
Derived epoch anchor in seconds.
~FlentApplication() override
std::array< Ptr< BulkSendApplication >, 4 > m_bulkSendUp
BulkSend Applications for Upload flows (tcp_upload, rrul tests).
void StopApplication() override
Application specific shutdown code.
std::array< uint32_t, 4 > m_bytesReceived
receive data counters
std::string m_imageText
Text to be included in plot.
std::array< Ptr< UdpEchoClient >, 3 > m_udpClient
UdpEchoClient Applications.
Time m_t0
Epoch anchor for output timestamps (see T0 attribute).
std::array< uint32_t, 4 > m_bytesSent
sent data counters
void FillXValues()
Fill x_values parameter in Flent file.
void ProcessRawValues()
Process raw values and add the processed result in the Flent file.
void TraceReceivedUdpPing(std::size_t idx, Ptr< const Packet > packet, const Address &address, const Address &localAddress, const SeqTsEchoHeader &header)
UDP Ping RTT Trace function.
std::array< Ptr< UdpEchoServer >, 3 > m_udpServer
UdpEchoServer Applications.
void TraceSentPacket(std::size_t idx, Ptr< const Packet > packet)
sink for packet transmissions.
bool m_useWallClockT0
Use the wall clock instead of m_t0 (see UseWallClockT0 attribute).
static TypeId GetTypeId()
Get the type ID.
std::unique_ptr< Output > m_output
Opaque JSON output document (defined in the .cc file).
std::array< Ptr< PacketSink >, 4 > m_packetSinkUp
PacketSink Applications for Upload flows (tcp_upload, rrul tests).
Time m_length
Test duration.
Time m_stepSize
Measurement data point step size.
void GoodputSamplingUpload(std::string name, int i)
Goodput calculation for the i-th flow based on the bytes sent.
std::string m_testName
Flent test name.
Address m_hostAddress
Host address.
Address m_localBindAddress
Local bind address.
void TraceReceivedPing(uint16_t seq, Time rtt)
Ping RTT Trace function.
std::array< Ptr< PacketSink >, 4 > m_packetSinkDown
PacketSink Applications for Download flows (tcp_download, rrul tests).
void DoDispose() override
std::string m_outputFilename
Output file path (see OutputFilename attribute).
void DoInitialize() override
In this method, the following tasks are performed: 1) set the host node, which is derived from the ho...
Ptr< Ping > m_ping
Ping Application for latency measurement (ping, rrul tests).
void AddMetadata()
Add Flent metadata to the output document.
Ptr< Node > GetHostNode(Ipv4Address hostAddress) const
Iterates through the node list and finds the node pointer for the given hostAddress.
void TraceReceivedPacket(std::size_t idx, Ptr< const Packet > packet, const Address &address)
sink for packet received.
void StartApplication() override
Application specific startup code.
void GoodputSamplingDownload(std::string name, int i)
Goodput calculation for the i-th flow based on the bytes received.
std::array< Ptr< BulkSendApplication >, 4 > m_bulkSendDown
BulkSend Applications for Download flows (tcp_download, rrul tests).
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address ConvertFrom(const Address &address)
static bool IsMatchingType(const Address &address)
static Ipv4Address GetAny()
a class to store IPv4 address information on an interface
Ipv4Address GetAddress() const
Get the local address.
Implement the IPv4 layer.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static Time Now()
Return the current simulation virtual time.
Hold variables of type string.
Simulation virtual time values and global simulation resolution.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Hold an unsigned integer type.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Ptr< T > CreateObjectWithAttributes(Args... args)
Allocate an Object on the heap and initialize with a set of attributes.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Time Seconds(double value)
Construct a Time in the indicated unit.
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Ptr< const AttributeChecker > MakeAddressChecker()
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Ptr< const AttributeChecker > MakeStringChecker()
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
constexpr uint16_t DEFAULT_PORT
Default base port used by the FlentApplication to configure and bind remote peer sinks.
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Flent output document, in JSON format.
nlohmann::json json
Flent output document.
static const uint32_t packetSize
Packet size generated at the AP.