Creation of a simple tsn network
Introduction
In this chapter, we will explore how to transform the Ethernet network simulation script from the previous chapter into a TSN network simulation script. Then, we will discuss how to instantiate and configure the various TSN mechanisms supported by the simulation library.
Note that the operation and configuration of TSN mechanisms will not be explained in depth. This chapter assumes that the reader is familiar with the details of the mechanisms implemented.
From Ethernet to TSN simulation
Let’s start by importing two new objects: TsnNode and TsnNetDevice.
[...]
#include "ns3/tsn-node.h"
#include "ns3/tsn-net-device.h"
[...]
Next, it is necessary to replace the node and EthernetNetDevice objects with their TSN versions in order to instantiate the various TSN mechanisms described below.
[...]
//Create four nodes
Ptr<TsnNode> n0 = CreateObject<TsnNode>();
Names::Add("ES1", n0);
Ptr<TsnNode> n1 = CreateObject<TsnNode>();
Names::Add("ES2", n1);
Ptr<TsnNode> n2 = CreateObject<TsnNode>();
Names::Add("ES3", n2);
Ptr<TsnNode> n3 = CreateObject<TsnNode>();
Names::Add("SW", n3);
//Create and add a netDevice to each end-station node
Ptr<TsnNetDevice> net0 = CreateObject<TsnNetDevice>();
n0->AddDevice(net0);
Names::Add("ES1#01", net0);
Ptr<TsnNetDevice> net1 = CreateObject<TsnNetDevice>();
n1->AddDevice(net1);
Names::Add("ES2#01", net1);
Ptr<TsnNetDevice> net2 = CreateObject<TsnNetDevice>();
n2->AddDevice(net2);
Names::Add("ES3#01", net2);
//Create and add a netDevice to each switch port
Ptr<TsnNetDevice> swnet0 = CreateObject<TsnNetDevice>();
n3->AddDevice(swnet0);
Names::Add("SW#01", swnet0);
Ptr<TsnNetDevice> swnet1 = CreateObject<TsnNetDevice>();
n3->AddDevice(swnet1);
Names::Add("SW#02", swnet1);
Ptr<TsnNetDevice> swnet2 = CreateObject<TsnNetDevice>();
n3->AddDevice(swnet2);
Names::Add("SW#03", swnet2);
[...]
At this stage, it is possible to run a simulation, but the result will be identical to a simple Ethernet network.
To simplify the illustration of the latency control mechanisms (CBS and TAS), we can make the following modifications to the application:
[...]
//Application description
//ES1 -> ES3 with priority 1
Ptr<EthernetGenerator> app0 = CreateObject<EthernetGenerator>();
app0->Setup(net0);
app0->SetAttribute("Address", AddressValue(net2->GetAddress()));
app0->SetAttribute("BurstSize", UintegerValue(5));
app0->SetAttribute("PayloadSize", UintegerValue(1400));
app0->SetAttribute("Period", TimeValue(Seconds(5)));
app0->SetAttribute("VlanID", UintegerValue(1));
app0->SetAttribute("PCP", UintegerValue(1));
n0->AddApplication(app0);
app0->SetStartTime(Seconds(0));
app0->SetStopTime(Seconds(10));
[...]
The output of a simulation should look like this:
[0/2] Re-checking globbed directories...
[2/2] Linking CXX executable ../build/scratch/ns3.40-book-default
Start of the simulation
+0s ES1:ES1#01 : Pkt #0 sent!
+0s ES1:ES1#01 : Pkt #1 sent!
+0s ES1:ES1#01 : Pkt #2 sent!
+0s ES1:ES1#01 : Pkt #3 sent!
+0s ES1:ES1#01 : Pkt #4 sent!
+0.000233905s ES3:ES3#01 : Pkt #0 received!
+0.000349265s ES3:ES3#01 : Pkt #1 received!
+0.000464625s ES3:ES3#01 : Pkt #2 received!
+0.000579985s ES3:ES3#01 : Pkt #3 received!
+0.000695345s ES3:ES3#01 : Pkt #4 received!
+5s ES1:ES1#01 : Pkt #5 sent!
+5s ES1:ES1#01 : Pkt #6 sent!
+5s ES1:ES1#01 : Pkt #7 sent!
+5s ES1:ES1#01 : Pkt #8 sent!
+5s ES1:ES1#01 : Pkt #9 sent!
+5.00023s ES3:ES3#01 : Pkt #5 received!
+5.00035s ES3:ES3#01 : Pkt #6 received!
+5.00046s ES3:ES3#01 : Pkt #7 received!
+5.00058s ES3:ES3#01 : Pkt #8 received!
+5.0007s ES3:ES3#01 : Pkt #9 received!
End of the simulation
CBS
To instantiate a CBS, we must first add its dependency.
#include "ns3/cbs.h"
It is then possible to instantiate a CBS and link it to an output port queue. In the following example, we instantiate the CBS on the output port of the transmitting end station.
[...]
//Create 2 output port FIFOs for each netDevice.
Ptr<Cbs> cbs = CreateObject<Cbs>();
cbs->SetTsnNetDevice(net0);
cbs->SetAttribute("IdleSlope", DataRateValue(DataRate("20Kb/s")));
cbs->SetAttribute("portTransmitRate", DataRateValue(DataRate("100Mb/s")));
net0->SetQueue(CreateObject<DropTailQueue<Packet>>()); //FIFO 0
net0->SetQueue(CreateObject<DropTailQueue<Packet>>(), cbs); //FIFO 1
for (int i=0; i<2; i++){
net1->SetQueue(CreateObject<DropTailQueue<Packet>>());
net2->SetQueue(CreateObject<DropTailQueue<Packet>>());
swnet0->SetQueue(CreateObject<DropTailQueue<Packet>>());
swnet1->SetQueue(CreateObject<DropTailQueue<Packet>>());
swnet2->SetQueue(CreateObject<DropTailQueue<Packet>>());
}
[...]
When launching the simulation, we can now see that the reception of burst frames is spread out over time due to the waiting time imposed by the CBS credit reload on the emission port.
[0/2] Re-checking globbed directories...
[2/2] Linking CXX executable ../build/scratch/ns3.40-book-default
Start of the simulation
+0s ES1:ES1#01 : Pkt #0 sent!
+0s ES1:ES1#01 : Pkt #1 sent!
+0s ES1:ES1#01 : Pkt #2 sent!
+0s ES1:ES1#01 : Pkt #3 sent!
+0s ES1:ES1#01 : Pkt #4 sent!
+0.000233905s ES3:ES3#01 : Pkt #0 received!
+0.577031s ES3:ES3#01 : Pkt #1 received!
+1.15383s ES3:ES3#01 : Pkt #2 received!
+1.73063s ES3:ES3#01 : Pkt #3 received!
+2.30743s ES3:ES3#01 : Pkt #4 received!
+5s ES1:ES1#01 : Pkt #5 sent!
+5s ES1:ES1#01 : Pkt #6 sent!
+5s ES1:ES1#01 : Pkt #7 sent!
+5s ES1:ES1#01 : Pkt #8 sent!
+5s ES1:ES1#01 : Pkt #9 sent!
+5.00023s ES3:ES3#01 : Pkt #5 received!
+5.57703s ES3:ES3#01 : Pkt #6 received!
+6.15383s ES3:ES3#01 : Pkt #7 received!
+6.73063s ES3:ES3#01 : Pkt #8 received!
+7.30743s ES3:ES3#01 : Pkt #9 received!
End of the simulation
TAS
To instanciate TAS, we need to add a clock to the TsnNode, add GCL entries to the net device and add 8 FIFOs to the output ports. It can be done as follows:
[...]
//Add a perfect clock to the SW node
n3->AddClock(CreateObject<Clock>());
[...]
//Create 8 output port FIFOs for each netDevice.
Ptr<Cbs> cbs = CreateObject<Cbs>();
cbs->SetTsnNetDevice(net0);
cbs->SetAttribute("IdleSlope", DataRateValue(DataRate("20Kb/s")));
cbs->SetAttribute("portTransmitRate", DataRateValue(DataRate("100Mb/s")));
net0->SetQueue(CreateObject<DropTailQueue<Packet>>()); //FIFO 0
net0->SetQueue(CreateObject<DropTailQueue<Packet>>(), cbs); //FIFO 1
for (int i=0; i<6; i++){
net0->SetQueue(CreateObject<DropTailQueue<Packet>>()); //FIFO 0
}
for (int i=0; i<8; i++){
net1->SetQueue(CreateObject<DropTailQueue<Packet>>());
net2->SetQueue(CreateObject<DropTailQueue<Packet>>());
swnet0->SetQueue(CreateObject<DropTailQueue<Packet>>());
swnet1->SetQueue(CreateObject<DropTailQueue<Packet>>());
swnet2->SetQueue(CreateObject<DropTailQueue<Packet>>());
}
[...]
//Configure TAS schedule
swnet2->AddGclEntry(Time(Seconds(2)), 0); //All gates are close
swnet2->AddGclEntry(Time(Seconds(3)), 2); //Only the gate of the FIFO 1 is open
swnet2->StartTas();
[...]
In this example, a TAS schedule is added to the switch’s output port, used by the flow. This schedule is designed to delay the frames of the two bursts by approximately two seconds compared to the previous example. The result obtained is as follows.
[0/2] Re-checking globbed directories...
[2/2] Linking CXX executable ../build/scratch/ns3.40-book-default
Start of the simulation
+0s ES1:ES1#01 : Pkt #0 sent!
+0s ES1:ES1#01 : Pkt #1 sent!
+0s ES1:ES1#01 : Pkt #2 sent!
+0s ES1:ES1#01 : Pkt #3 sent!
+0s ES1:ES1#01 : Pkt #4 sent!
+2.00011s ES3:ES3#01 : Pkt #0 received!
+2.00023s ES3:ES3#01 : Pkt #1 received!
+2.00035s ES3:ES3#01 : Pkt #2 received!
+2.00046s ES3:ES3#01 : Pkt #3 received!
+2.30743s ES3:ES3#01 : Pkt #4 received!
+5s ES1:ES1#01 : Pkt #5 sent!
+5s ES1:ES1#01 : Pkt #6 sent!
+5s ES1:ES1#01 : Pkt #7 sent!
+5s ES1:ES1#01 : Pkt #8 sent!
+5s ES1:ES1#01 : Pkt #9 sent!
+7.00011s ES3:ES3#01 : Pkt #5 received!
+7.00023s ES3:ES3#01 : Pkt #6 received!
+7.00035s ES3:ES3#01 : Pkt #7 received!
+7.00046s ES3:ES3#01 : Pkt #8 received!
+7.30743s ES3:ES3#01 : Pkt #9 received!
End of the simulation
gPTP
To synchronize the nodes in our network using gPTP, several changes must be made. Let’s start with the includes.
[...]
#include "ns3/clock-constant-drift.h"
#include "ns3/gPTP.h"
#include "ns3/ethernet-header2.h"
[...]
Next, let’s modify the callbacks that log packet transmission and reception so that events are only logged if the VLAN ID matches the VLAN ID of the flows going from ES1 to ES3. This change prevents the output from being flooded with logs concerning the transmission or reception of synchronization frames. We also add a callback to log the difference between the perfect clock (the simulation time) and the device clock after each correction to verify that gPTP is working properly.
//A callback to log the pkt emission
static void
MacTxCallback(std::string context, Ptr<const Packet> p)
{
Ptr<Packet> pkt = p->Copy();
EthernetHeader2 ethHeader;
pkt->RemoveHeader(ethHeader);
if (ethHeader.GetVid() == 1) {
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << " sent!");
}
}
//A callback to log the pkt reception
static void
MacRxCallback(std::string context, Ptr<const Packet> p)
{
Ptr<Packet> pkt = p->Copy();
EthernetHeader2 ethHeader;
pkt->RemoveHeader(ethHeader);
if (ethHeader.GetVid() == 1) {
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << " received!");
}
}
//A callback to log clock offset after correction
static void
ClockAfterCorrectionCallback(std::string context, Time clockValue)
{
NS_LOG_INFO("[GPTP] At " << Simulator::Now() << " on "<< context << " clock value after correction = " << clockValue.GetNanoSeconds() << "ns (error = "<< (Simulator::Now()-clockValue).GetNanoSeconds() << "ns)");
}
Next, we replace the perfect clock previously instantiated on n3(SW) in the TAS section with clock instantiation on the various network devices. Note that the clock instantiated on ES1(n0) is a perfect clock (i.e., it corresponds to the simulation time) since it acts as the Grandmaster.
[...]
Ptr<TsnNode> n3 = CreateObject<TsnNode>();
Names::Add("SW", n3);
//Create and add clocks to TsnNodes
Ptr<Clock> c0 = CreateObject<Clock>(); //perfect clock because Grandmaster
n0->SetMainClock(c0);
Ptr<ConstantDriftClock> c1 = CreateObject<ConstantDriftClock>();
c1->SetAttribute("InitialOffset", TimeValue(Seconds(20)));
c1->SetAttribute("DriftRate", DoubleValue(-50));
c1->SetAttribute("Granularity", TimeValue(NanoSeconds(10)));
n1->SetMainClock(c1);
Ptr<ConstantDriftClock> c2 = CreateObject<ConstantDriftClock>();
c2->SetAttribute("InitialOffset", TimeValue(Seconds(3)));
c2->SetAttribute("DriftRate", DoubleValue(2));
c2->SetAttribute("Granularity", TimeValue(NanoSeconds(10)));
n2->SetMainClock(c2);
Ptr<ConstantDriftClock> c3 = CreateObject<ConstantDriftClock>();
c3->SetAttribute("InitialOffset", TimeValue(Seconds(0.5)));
c3->SetAttribute("DriftRate", DoubleValue(-25));
c3->SetAttribute("Granularity", TimeValue(NanoSeconds(10)));
n3->SetMainClock(c3);
[...]
Then, we add and configure the gPTP instances on the different nodes. Note that the implementation of gPTP in Eden-sim only supports static configuration of gPTP (i.e. no BTCA).
[...]
swnet2->SetQueue(CreateObject<DropTailQueue<Packet>>());
}
Ptr<GPTP> gPTP0 = CreateObject<GPTP>();
gPTP0->SetNode(n0);
gPTP0->SetMainClock(c0);
gPTP0->AddDomain(0);
gPTP0->AddPort(net0, GPTP::MASTER, 0);
gPTP0->SetAttribute("SyncInterval", TimeValue(Seconds(0.125))); //This line is not mandatory because 0.125s is the default value
gPTP0->SetAttribute("PdelayInterval", TimeValue(Seconds(1))); //This line is not mandatory because 1s is the default value
gPTP0->SetAttribute("Priority", UintegerValue(7));
n0->AddApplication(gPTP0);
gPTP0->SetStartTime(Seconds(0));
Ptr<GPTP> gPTP1 = CreateObject<GPTP>();
gPTP1->SetNode(n1);
gPTP1->SetMainClock(c1);
gPTP1->AddDomain(0);
gPTP1->AddPort(net1, GPTP::SLAVE, 0);
gPTP1->SetAttribute("Priority", UintegerValue(7));
n1->AddApplication(gPTP1);
gPTP1->SetStartTime(Seconds(0));
Ptr<GPTP> gPTP2 = CreateObject<GPTP>();
gPTP2->SetNode(n2);
gPTP2->SetMainClock(c2);
gPTP2->AddDomain(0);
gPTP2->AddPort(net2, GPTP::SLAVE, 0);
gPTP2->SetAttribute("Priority", UintegerValue(7));
n2->AddApplication(gPTP2);
gPTP2->SetStartTime(Seconds(0));
Ptr<GPTP> gPTP3 = CreateObject<GPTP>();
gPTP3->SetNode(n3);
gPTP3->SetMainClock(c3);
gPTP3->AddDomain(0);
gPTP3->AddPort(swnet0, GPTP::SLAVE, 0);
gPTP3->AddPort(swnet1, GPTP::MASTER, 0);
gPTP3->AddPort(swnet2, GPTP::MASTER, 0);
gPTP3->SetAttribute("Priority", UintegerValue(7));
n3->AddApplication(gPTP3);
gPTP3->SetStartTime(Seconds(0));
And finally, we add the callbacks.
[...]
net2->TraceConnectWithoutContext("MacRx", MakeBoundCallback(&MacRxCallback, context));
//Callback to display clock offset after correction
gPTP1->TraceConnectWithoutContext("ClockAfterCorrection", MakeBoundCallback(&ClockAfterCorrectionCallback, Names::FindName(n1)));
gPTP2->TraceConnectWithoutContext("ClockAfterCorrection", MakeBoundCallback(&ClockAfterCorrectionCallback, Names::FindName(n2)));
gPTP3->TraceConnectWithoutContext("ClockAfterCorrection", MakeBoundCallback(&ClockAfterCorrectionCallback, Names::FindName(n3)));
[...]
When running the simulation, we can observe that gPTP effectively mitigates clock drift thanks to periodic synchronization on SW and ES2. However, we note that there is no gPTP log for ES3. This is due to the TAS configuration, which never opens the FIFO7 gate used by synchronization messages. Of course, such a configuration has no place in a network that is intended to be functional, but in this example it illustrates the impact of the TAS configuration on gPTP packets.
Stream Identification
Before using PSFP or FRER, it is necessary to implement an identification function. In this section, we will use a null Stream identification function on the switch port connected to ES1. This function will then be used in the following two sections to implement PSFP in order to validate the flow contract and to replicate frames using FRER.
Let’s start by importing this function.
[...]
#include "ns3/stream-identification-function-null.h"
[...]
Next, let’s create and add the identification function on port swnet0 in input and outfacing mode.
[...]
swnet2->StartTas();
//Add a stream identification function
Ptr<NullStreamIdentificationFunction> sif0 = CreateObject<NullStreamIdentificationFunction>();
uint16_t StreamHandle = 10;
sif0->SetAttribute("VlanID", UintegerValue(1));
sif0->SetAttribute("Address", AddressValue(net2->GetAddress()));
n3->AddStreamIdentificationFunction(StreamHandle, sif0, {swnet0}, {}, {}, {});
[...]
PSFP
Now that we have an identification function capable of identifying frames going from ES1 to ES3 with VLAN ID 1, we can set up a PSFP instance to validate compliance with a network usage contract. Currently, the simulator implements the stream filter and the flow meter. They can be added and configured as follows.
[...]
n3->AddStreamIdentificationFunction(StreamHandle, sif0, {swnet0}, {}, {}, {});
//PSFP configuration
Ptr<StreamFilterInstance> sfi0 = CreateObject<StreamFilterInstance>();
sfi0->SetAttribute("StreamHandle", IntegerValue(StreamHandle));
sfi0->SetAttribute("Priority", IntegerValue(-1)); //-1 = wildcard
sfi0->SetAttribute("MaxSDUSize", UintegerValue(1422));
n3->AddStreamFilter(sfi0);
Ptr<FlowMeterInstance> fm0 = CreateObject<FlowMeterInstance>();
fm0->SetAttribute("CIR", DataRateValue(DataRate("20Kb/s")));
fm0->SetAttribute("CBS", UintegerValue(1400));
fm0->SetAttribute("DropOnYellow", BooleanValue(true));
fm0->SetAttribute("MarkAllFramesRedEnable", BooleanValue(false));
uint16_t fmid = n3->AddFlowMeter(fm0);
sfi0->AddFlowMeterInstanceId(fmid);
[...]
After these changes, running the simulation does not produce a different output from previous runs. However, by changing the parameters of the emitting application to increase its bandwidth consumption (e.g., increasing the packet size) or by increasing the idle slope of the CBS, it is possible to observe that packets that do not comply with the contract are never received as they are discarded.
FRER
And finally, let’s instantiate FRER. However, the topology we are working with in this chapter does not have multiple paths between ES1 and ES3. So in this section, we will replicate the frames on the output ports of the switch connected to ES2 and ES3 to illustrate the philosophy behind FRER instantiation. We will only detail the replication part. The elimination part is detailed in the example contrib/tsn/examples/tsn-switched-withFRER.cc, which has a topology much more suited to the use of FRER.
Replication with FRER is based on two functions: Sequence Generation Function and Sequence Encode/Decode Function. These two functions are created and configured as follows.
[...]
sfi0->AddFlowMeterInstanceId(fmid);
//Sequencing : Sequence generation
Ptr<SequenceGenerationFunction> seqf0 = CreateObject<SequenceGenerationFunction>();
seqf0->SetAttribute("Direction", BooleanValue(false)); //in-facing
seqf0->SetStreamHandle({StreamHandle});
n3->AddSequenceGenerationFunction(seqf0);
//Sequence encode
Ptr<SequenceEncodeDecodeFunction> seqEnc0 = CreateObject<SequenceEncodeDecodeFunction>();
seqEnc0->SetAttribute("Direction", BooleanValue(false)); //in-facing
seqEnc0->SetAttribute("Active", BooleanValue(true));
seqEnc0->SetStreamHandle({StreamHandle});
seqEnc0->SetPort(swnet0);
n3->AddSequenceEncodeDecodeFunction(seqEnc0);
//Add a forwarding table entry
sw->AddForwardingTableEntry(Mac48Address::ConvertFrom(net2->GetAddress()), 1, {swnet1, swnet2});
[...]
With this configuration, replication is achieved by forwarding to two different ports. This simplifies configuration by not using the FRER splitting function. However, it is necessary to change the configuration of the forwarding table as shown in the previous listing.
In order to validate the correct operation of the replication (i.e., the addition of the R-TAG), modify the callbacks as follows to display the frame size at transmission and reception.
[...]
//A callback to log the pkt emission
static void
MacTxCallback(std::string context, Ptr<const Packet> p)
{
Ptr<Packet> pkt = p->Copy();
EthernetHeader2 ethHeader;
pkt->RemoveHeader(ethHeader);
if (ethHeader.GetVid() == 1) {
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << "(" << p->GetSize() << "bytes) sent!");
}
}
//A callback to log the pkt reception
static void
MacRxCallback(std::string context, Ptr<const Packet> p)
{
Ptr<Packet> pkt = p->Copy();
EthernetHeader2 ethHeader;
pkt->RemoveHeader(ethHeader);
if (ethHeader.GetVid() == 1) {
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << "(" << p->GetSize() << "bytes) received!");
}
}
[...]
And now, when running the simulation script, we observe an increase in frame size of 6 bytes (the size of R-TAG) between transmission and reception, as illustrated below.
[...]
+5s ES1:ES1#01 : Pkt #215(1422bytes) sent!
[...]
+7.30743s ES3:ES3#01 : Pkt #215(1428bytes) received!
[...]
Conclusion and final simulation script
In this section, we have implemented the various TSN mechanisms of Eden-sim.
Note that the examples found in contrib/tsn/examples/ illustrate more complicated configurations and implement different traces to log information about the mechanism’s operation. These examples are a good means of further exploring the uses of these TSN mechanisms.
Here is the script at the end of this chapter:
#include "ns3/simulator.h"
#include "ns3/core-module.h"
#include "ns3/node.h"
#include "ns3/drop-tail-queue.h"
#include "ns3/tsn-node.h"
#include "ns3/tsn-net-device.h"
#include "ns3/cbs.h"
#include "ns3/ethernet-channel.h"
#include "ns3/switch-net-device.h"
#include "ns3/ethernet-generator.h"
#include "ns3/clock-constant-drift.h"
#include "ns3/gPTP.h"
#include "ns3/ethernet-header2.h"
#include "ns3/stream-identification-function-null.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("Chapter 3");
//A callback to log the pkt emission
static void
MacTxCallback(std::string context, Ptr<const Packet> p)
{
Ptr<Packet> pkt = p->Copy();
EthernetHeader2 ethHeader;
pkt->RemoveHeader(ethHeader);
if (ethHeader.GetVid() == 1) {
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << "(" << p->GetSize() << "bytes) sent!");
}
}
//A callback to log the pkt reception
static void
MacRxCallback(std::string context, Ptr<const Packet> p)
{
Ptr<Packet> pkt = p->Copy();
EthernetHeader2 ethHeader;
pkt->RemoveHeader(ethHeader);
if (ethHeader.GetVid() == 1) {
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << "(" << p->GetSize() << "bytes) received!");
}
}
//A callback to log clock offset after correction
static void
ClockAfterCorrectionCallback(std::string context, Time clockValue)
{
NS_LOG_INFO("[GPTP] At " << Simulator::Now() << " on "<< context << " clock value after correction = " << clockValue.GetNanoSeconds() << "ns (error = "<< (Simulator::Now()-clockValue).GetNanoSeconds() << "ns)");
}
int
main(int argc, char* argv[])
{
//Enable logging
LogComponentEnable("Chapter 3", LOG_LEVEL_INFO);
//Create four nodes
Ptr<TsnNode> n0 = CreateObject<TsnNode>();
Names::Add("ES1", n0);
Ptr<TsnNode> n1 = CreateObject<TsnNode>();
Names::Add("ES2", n1);
Ptr<TsnNode> n2 = CreateObject<TsnNode>();
Names::Add("ES3", n2);
Ptr<TsnNode> n3 = CreateObject<TsnNode>();
Names::Add("SW", n3);
//Create and add clocks to TsnNodes
Ptr<Clock> c0 = CreateObject<Clock>(); //perfect clock because Grandmaster
n0->SetMainClock(c0);
Ptr<ConstantDriftClock> c1 = CreateObject<ConstantDriftClock>();
c1->SetAttribute("InitialOffset", TimeValue(Seconds(20)));
c1->SetAttribute("DriftRate", DoubleValue(-50));
c1->SetAttribute("Granularity", TimeValue(NanoSeconds(10)));
n1->SetMainClock(c1);
Ptr<ConstantDriftClock> c2 = CreateObject<ConstantDriftClock>();
c2->SetAttribute("InitialOffset", TimeValue(Seconds(3)));
c2->SetAttribute("DriftRate", DoubleValue(2));
c2->SetAttribute("Granularity", TimeValue(NanoSeconds(10)));
n2->SetMainClock(c2);
Ptr<ConstantDriftClock> c3 = CreateObject<ConstantDriftClock>();
c3->SetAttribute("InitialOffset", TimeValue(Seconds(0.5)));
c3->SetAttribute("DriftRate", DoubleValue(-25));
c3->SetAttribute("Granularity", TimeValue(NanoSeconds(10)));
n3->SetMainClock(c3);
//Create and add a netDevice to each end-station node
Ptr<TsnNetDevice> net0 = CreateObject<TsnNetDevice>();
net0->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n0->AddDevice(net0);
Names::Add("ES1#01", net0);
Ptr<TsnNetDevice> net1 = CreateObject<TsnNetDevice>();
net1->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n1->AddDevice(net1);
Names::Add("ES2#01", net1);
Ptr<TsnNetDevice> net2 = CreateObject<TsnNetDevice>();
net2->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n2->AddDevice(net2);
Names::Add("ES3#01", net2);
//Create and add a netDevice to each switch port
Ptr<TsnNetDevice> swnet0 = CreateObject<TsnNetDevice>();
swnet0->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n3->AddDevice(swnet0);
Names::Add("SW#01", swnet0);
Ptr<TsnNetDevice> swnet1 = CreateObject<TsnNetDevice>();
swnet1->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n3->AddDevice(swnet1);
Names::Add("SW#02", swnet1);
Ptr<TsnNetDevice> swnet2 = CreateObject<TsnNetDevice>();
swnet2->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n3->AddDevice(swnet2);
Names::Add("SW#03", swnet2);
//Create Ethernet Channels and connect switch to the end-stations
Ptr<EthernetChannel> channel0 = CreateObject<EthernetChannel>();
channel0->SetAttribute("Delay", TimeValue(NanoSeconds(50)));
net0->Attach(channel0);
swnet0->Attach(channel0);
Ptr<EthernetChannel> channel1 = CreateObject<EthernetChannel>();
channel1->SetAttribute("Delay", TimeValue(NanoSeconds(75)));
net1->Attach(channel1);
swnet1->Attach(channel1);
Ptr<EthernetChannel> channel2 = CreateObject<EthernetChannel>();
channel2->SetAttribute("Delay", TimeValue(NanoSeconds(100)));
net2->Attach(channel2);
swnet2->Attach(channel2);
//Create and add a switch net device to the switch node
Ptr<SwitchNetDevice> sw = CreateObject<SwitchNetDevice>();
sw->SetAttribute("MinForwardingLatency", TimeValue(MicroSeconds(2)));
sw->SetAttribute("MaxForwardingLatency", TimeValue(MicroSeconds(5)));
n3->AddDevice(sw);
sw->AddSwitchPort(swnet0);
sw->AddSwitchPort(swnet1);
sw->AddSwitchPort(swnet2);
//Allocate Mac addresses to the netDevices
net0->SetAddress(Mac48Address::Allocate());
net1->SetAddress(Mac48Address::Allocate());
net2->SetAddress(Mac48Address::Allocate());
sw->SetAddress(Mac48Address::Allocate());
//Create 8 output port FIFOs for each netDevice.
Ptr<Cbs> cbs = CreateObject<Cbs>();
cbs->SetTsnNetDevice(net0);
cbs->SetAttribute("IdleSlope", DataRateValue(DataRate("20Kb/s")));
cbs->SetAttribute("portTransmitRate", DataRateValue(DataRate("100Mb/s")));
net0->SetQueue(CreateObject<DropTailQueue<Packet>>()); //FIFO 0
net0->SetQueue(CreateObject<DropTailQueue<Packet>>(), cbs); //FIFO 1
for (int i=0; i<6; i++){
net0->SetQueue(CreateObject<DropTailQueue<Packet>>()); //FIFO 0
}
for (int i=0; i<8; i++){
net1->SetQueue(CreateObject<DropTailQueue<Packet>>());
net2->SetQueue(CreateObject<DropTailQueue<Packet>>());
swnet0->SetQueue(CreateObject<DropTailQueue<Packet>>());
swnet1->SetQueue(CreateObject<DropTailQueue<Packet>>());
swnet2->SetQueue(CreateObject<DropTailQueue<Packet>>());
}
//Add and configure gPTP
Ptr<GPTP> gPTP0 = CreateObject<GPTP>();
gPTP0->SetNode(n0);
gPTP0->SetMainClock(c0);
gPTP0->AddDomain(0);
gPTP0->AddPort(net0, GPTP::MASTER, 0);
gPTP0->SetAttribute("SyncInterval", TimeValue(Seconds(0.125))); //This line is not mandatory because 0.125s is the default value
gPTP0->SetAttribute("PdelayInterval", TimeValue(Seconds(1))); //This line is not mandatory because 1s is the default value
gPTP0->SetAttribute("Priority", UintegerValue(7));
n0->AddApplication(gPTP0);
gPTP0->SetStartTime(Seconds(0));
Ptr<GPTP> gPTP1 = CreateObject<GPTP>();
gPTP1->SetNode(n1);
gPTP1->SetMainClock(c1);
gPTP1->AddDomain(0);
gPTP1->AddPort(net1, GPTP::SLAVE, 0);
gPTP1->SetAttribute("Priority", UintegerValue(7));
n1->AddApplication(gPTP1);
gPTP1->SetStartTime(Seconds(0));
Ptr<GPTP> gPTP2 = CreateObject<GPTP>();
gPTP2->SetNode(n2);
gPTP2->SetMainClock(c2);
gPTP2->AddDomain(0);
gPTP2->AddPort(net2, GPTP::SLAVE, 0);
gPTP2->SetAttribute("Priority", UintegerValue(7));
n2->AddApplication(gPTP2);
gPTP2->SetStartTime(Seconds(0));
Ptr<GPTP> gPTP3 = CreateObject<GPTP>();
gPTP3->SetNode(n3);
gPTP3->SetMainClock(c3);
gPTP3->AddDomain(0);
gPTP3->AddPort(swnet0, GPTP::SLAVE, 0);
gPTP3->AddPort(swnet1, GPTP::MASTER, 0);
gPTP3->AddPort(swnet2, GPTP::MASTER, 0);
gPTP3->SetAttribute("Priority", UintegerValue(7));
n3->AddApplication(gPTP3);
gPTP3->SetStartTime(Seconds(0));
//Configure TAS schedule
swnet2->AddGclEntry(Time(Seconds(2)), 0); //All gates are close
swnet2->AddGclEntry(Time(Seconds(3)), 2); //Only the gate of the FIFO 1 is open
swnet2->StartTas();
//Add a stream identification function
Ptr<NullStreamIdentificationFunction> sif0 = CreateObject<NullStreamIdentificationFunction>();
uint16_t StreamHandle = 10;
sif0->SetAttribute("VlanID", UintegerValue(1));
sif0->SetAttribute("Address", AddressValue(net2->GetAddress()));
n3->AddStreamIdentificationFunction(StreamHandle, sif0, {swnet0}, {}, {}, {});
//PSFP configuration
Ptr<StreamFilterInstance> sfi0 = CreateObject<StreamFilterInstance>();
sfi0->SetAttribute("StreamHandle", IntegerValue(StreamHandle));
sfi0->SetAttribute("Priority", IntegerValue(-1)); //-1 = wildcard
sfi0->SetAttribute("MaxSDUSize", UintegerValue(1422));
n3->AddStreamFilter(sfi0);
Ptr<FlowMeterInstance> fm0 = CreateObject<FlowMeterInstance>();
fm0->SetAttribute("CIR", DataRateValue(DataRate("20Kb/s")));
fm0->SetAttribute("CBS", UintegerValue(1400));
fm0->SetAttribute("DropOnYellow", BooleanValue(true));
fm0->SetAttribute("MarkAllFramesRedEnable", BooleanValue(false));
uint16_t fmid = n3->AddFlowMeter(fm0);
sfi0->AddFlowMeterInstanceId(fmid);
//Sequencing : Sequence generation
Ptr<SequenceGenerationFunction> seqf0 = CreateObject<SequenceGenerationFunction>();
seqf0->SetAttribute("Direction", BooleanValue(false)); //in-facing
seqf0->SetStreamHandle({StreamHandle});
n3->AddSequenceGenerationFunction(seqf0);
//Sequence encode
Ptr<SequenceEncodeDecodeFunction> seqEnc0 = CreateObject<SequenceEncodeDecodeFunction>();
seqEnc0->SetAttribute("Direction", BooleanValue(false)); //in-facing
seqEnc0->SetAttribute("Active", BooleanValue(true));
seqEnc0->SetStreamHandle({StreamHandle});
seqEnc0->SetPort(swnet0);
n3->AddSequenceEncodeDecodeFunction(seqEnc0);
//Add a forwarding table entry
sw->AddForwardingTableEntry(Mac48Address::ConvertFrom(net2->GetAddress()), 1, {swnet1, swnet2});
//Application description
//ES1 -> ES3 with priority 1
Ptr<EthernetGenerator> app0 = CreateObject<EthernetGenerator>();
app0->Setup(net0);
app0->SetAttribute("Address", AddressValue(net2->GetAddress()));
app0->SetAttribute("BurstSize", UintegerValue(5));
app0->SetAttribute("PayloadSize", UintegerValue(1400));
app0->SetAttribute("Period", TimeValue(Seconds(5)));
app0->SetAttribute("VlanID", UintegerValue(1));
app0->SetAttribute("PCP", UintegerValue(1));
n0->AddApplication(app0);
app0->SetStartTime(Seconds(0));
app0->SetStopTime(Seconds(10));
//Callback declarations
//Callback to display the packet sent log
std::string context = Names::FindName(n0) + ":" + Names::FindName(net0);
net0->TraceConnectWithoutContext("MacTx", MakeBoundCallback(&MacTxCallback, context));
//Callback to display the packet received log
context = Names::FindName(n2) + ":" + Names::FindName(net2);
net2->TraceConnectWithoutContext("MacRx", MakeBoundCallback(&MacRxCallback, context));
//Callback to display clock offset after correction
gPTP1->TraceConnectWithoutContext("ClockAfterCorrection", MakeBoundCallback(&ClockAfterCorrectionCallback, Names::FindName(n1)));
gPTP2->TraceConnectWithoutContext("ClockAfterCorrection", MakeBoundCallback(&ClockAfterCorrectionCallback, Names::FindName(n2)));
gPTP3->TraceConnectWithoutContext("ClockAfterCorrection", MakeBoundCallback(&ClockAfterCorrectionCallback, Names::FindName(n3)));
//Execute the simulation
NS_LOG_INFO("Start of the simulation");
Simulator::Stop(Seconds(10));
Simulator::Run();
Simulator::Destroy();
NS_LOG_INFO("End of the simulation");
return 0;
}