Introduction
Eden-sim is a ns-3 simulation library for embedded TSN networks.
This book is a user guide designed to assist in the creation of simulation scripts using Eden-sim. It is organised as follows:
- Getting started part to introduce the fundamental ideas of Eden-sim
- Advanced uses part for complex projects
Please note that in addition to the examples presented in this guide, example simulation scripts are also available in the following folders:
- contrib/ethernet/examples
- contrib/real-device/examples
- contrib/trace/examples
- contrib/traffic-generator/examples
- contrib/tsn/examples
Prerequisites
To put this guide into practice, you only need to have ns-3 and Eden-sim installed. The installation procedure for these two tools is described in Eden-sim’s README.md.
Creation of a simple ethernet network
Introduction
To perform a simulation with ns-3, you need to write a C++ program that describes the simulation to be performed. In this chapter, we will create such a simulation script able to simulate an Ethernet network composed of one switch to which three end stations are connected.
First simulation script
Let’s start by creating a script that runs a 10-second simulation. To do this, create a file called chapter1.cc in the scratch folder located in the ns-3 folder. In this file, copy the following lines:
#include "ns3/simulator.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("Chapter 1");
int
main(int argc, char* argv[])
{
//Enable logging
LogComponentEnable("Chapter 1", LOG_LEVEL_INFO);
//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;
}
To run it, simply execute the following command from the folder where ns-3 is installed.
./ns3 run scratch/chapter1.cc
Congratulations, you have run your first simulation with ns-3! However, this simulation does not simulate anything.
Topology description
Before simulating our Ethernet network, we must first describe it. To do this, let’s start by adding the necessary dependencies.
#include "ns3/simulator.h"
#include "ns3/core-module.h"
#include "ns3/node.h"
#include "ns3/drop-tail-queue.h"
#include "ns3/ethernet-net-device.h"
#include "ns3/ethernet-channel.h"
#include "ns3/switch-net-device.h"
[...]
Next, the topology is described as follows:
[...]
//Enable logging
LogComponentEnable("Chapter 1", LOG_LEVEL_INFO);
//Create four nodes
Ptr<Node> n0 = CreateObject<Node>();
Names::Add("ES1", n0);
Ptr<Node> n1 = CreateObject<Node>();
Names::Add("ES2", n1);
Ptr<Node> n2 = CreateObject<Node>();
Names::Add("ES3", n2);
Ptr<Node> n3 = CreateObject<Node>();
Names::Add("SW", n3);
//Create and add a netDevice to each end-station node
Ptr<EthernetNetDevice> net0 = CreateObject<EthernetNetDevice>();
n0->AddDevice(net0);
Names::Add("ES1#01", net0);
Ptr<EthernetNetDevice> net1 = CreateObject<EthernetNetDevice>();
n1->AddDevice(net1);
Names::Add("ES2#01", net1);
Ptr<EthernetNetDevice> net2 = CreateObject<EthernetNetDevice>();
n2->AddDevice(net2);
Names::Add("ES3#01", net2);
//Create and add a netDevice to each switch port
Ptr<EthernetNetDevice> swnet0 = CreateObject<EthernetNetDevice>();
n3->AddDevice(swnet0);
Names::Add("SW#01", swnet0);
Ptr<EthernetNetDevice> swnet1 = CreateObject<EthernetNetDevice>();
n3->AddDevice(swnet1);
Names::Add("SW#02", swnet1);
Ptr<EthernetNetDevice> swnet2 = CreateObject<EthernetNetDevice>();
n3->AddDevice(swnet2);
Names::Add("SW#03", swnet2);
//Create Ethernet Channels and connect switch to the end-stations
Ptr<EthernetChannel> channel0 = CreateObject<EthernetChannel>();
net0->Attach(channel0);
swnet0->Attach(channel0);
Ptr<EthernetChannel> channel1 = CreateObject<EthernetChannel>();
net1->Attach(channel1);
swnet1->Attach(channel1);
Ptr<EthernetChannel> channel2 = CreateObject<EthernetChannel>();
net2->Attach(channel2);
swnet2->Attach(channel2);
//Create and add a switch net device to the switch node
Ptr<SwitchNetDevice> sw = CreateObject<SwitchNetDevice>();
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 two output port FIFOs for each netDevice.
for (int i=0; i<2; i++){
net0->SetQueue(CreateObject<DropTailQueue<Packet>>());
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>>());
}
[...]
In this code, the following six steps are performed:
- Creating nodes: In this example, four nodes are created.
- Creating NetDevices: A NetDevice is a network interface in ns-3 terminology. Here, one NetDevice is created and added to each end-station node, and three NetDevices are created and added to the switch node.
- Creating channels: A channel is a link in ns-3 terminology in the case of the wired network considered here. Thus, three channels are created and attached to different NetDevices to connect the three end stations to the switch.
- Creating SwitchNetDevices: The SwitchNetDevice is the object that will perform switching between the switch’s NetDevices. Here, a SwitchNetDevice object is created and attached to the switch node. The switch ports are also attached to it.
- Allocation of Mac addresses: For our example, four mac addresses are allocated using the iterator provided by ns-3.
- Creation of output port queues: For our example, two FIFOs are instantiated per NetDevices.
At this stage, it is possible to run a simulation, but the output will be the same since no traffic is simulated.
Traffic simulation
To send Ethernet frames in our network, we need to instantiate applications. To instantiate these applications, we must first add the following dependency.
#include “ns3/ethernet-generator.h”
This dependency is part of Eden-sim and is used to transmit Q-Tagged Ethernet frames.
Next, let’s add an application that sends a burst of two frames with a payload of 1400 bytes, a priority of 1, and a VLAN ID of 1 every 5 seconds. This application is hosted on node ES1. The frames are destined for ES3. Here is the code used to instantiate such an application:
[...]
swnet2->SetQueue(CreateObject<DropTailQueue<Packet>>());
}
//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(2));
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));
[...]
With this simulation script complete, you can run a simulation… And you should see nothing new.
Indeed, there is no indication for ns-3 to produce any output. So our frames are sent in our simulated network, but we have no output to confirm it. To display logs about our frames in the console, let’s add a callback for the transmission and one for the reception.
[...]
NS_LOG_COMPONENT_DEFINE("Chapter 1");
//A callback to log the pkt emission
static void
MacTxCallback(std::string context, Ptr<const Packet> p)
{
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)
{
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << " received!");
}
[...]
[...]
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));
[...]
If you run the simulation again, you should get the following output.
$ ./ns3 run scratch/book.cc
[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!
+5s ES1:ES1#01 : Pkt #2 sent!
+5s ES1:ES1#01 : Pkt #3 sent!
End of the simulation
We can see that four frames are successfully sent by ES1 but are never received by ES3. This is because, after the first hop, the frames are received by the switch but it does not know which port to send them to. It is therefore necessary to add a static forwarding configuration to our switch.
Network configuration
Unlike plug-and-play switches, the switches simulated by Eden-sim are designed for critical embedded networks. To guarantee network determinism, these switches do not implement dynamic mechanisms such as mac learning. They must therefore be configured statically before the simulation begins.
Here, the only configuration missing to transmit frames to ES3 is the configuration of the static switching table. It is possible to add an entry to this table as follows:
[...]
swnet2->SetQueue(CreateObject<DropTailQueue<Packet>>());
}
//Add a forwarding table entry
sw->AddForwardingTableEntry(Mac48Address::ConvertFrom(net2->GetAddress()), 1, {swnet2});
[...]
After this configuration, running the simulation produces the following output, which finally indicates the transmission and reception of frames.
$ ./ns3 run scratch/book.cc
[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!
+2.293e-05s ES3:ES3#01 : Pkt #0 received!
+3.4466e-05s ES3:ES3#01 : Pkt #1 received!
+5s ES1:ES1#01 : Pkt #2 sent!
+5s ES1:ES1#01 : Pkt #3 sent!
+5.00002s ES3:ES3#01 : Pkt #2 received!
+5.00003s ES3:ES3#01 : Pkt #3 received!
End of the simulation
Final simulation script
Here is the simulation script you should have 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/ethernet-net-device.h"
#include "ns3/ethernet-channel.h"
#include "ns3/switch-net-device.h"
#include "ns3/ethernet-generator.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("Chapter 1");
//A callback to log the pkt emission
static void
MacTxCallback(std::string context, Ptr<const Packet> p)
{
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)
{
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << " received!");
}
int
main(int argc, char* argv[])
{
//Enable logging
LogComponentEnable("Chapter 1", LOG_LEVEL_INFO);
//Create four nodes
Ptr<Node> n0 = CreateObject<Node>();
Names::Add("ES1", n0);
Ptr<Node> n1 = CreateObject<Node>();
Names::Add("ES2", n1);
Ptr<Node> n2 = CreateObject<Node>();
Names::Add("ES3", n2);
Ptr<Node> n3 = CreateObject<Node>();
Names::Add("SW", n3);
//Create and add a netDevice to each end-station node
Ptr<EthernetNetDevice> net0 = CreateObject<EthernetNetDevice>();
n0->AddDevice(net0);
Names::Add("ES1#01", net0);
Ptr<EthernetNetDevice> net1 = CreateObject<EthernetNetDevice>();
n1->AddDevice(net1);
Names::Add("ES2#01", net1);
Ptr<EthernetNetDevice> net2 = CreateObject<EthernetNetDevice>();
n2->AddDevice(net2);
Names::Add("ES3#01", net2);
//Create and add a netDevice to each switch port
Ptr<EthernetNetDevice> swnet0 = CreateObject<EthernetNetDevice>();
n3->AddDevice(swnet0);
Names::Add("SW#01", swnet0);
Ptr<EthernetNetDevice> swnet1 = CreateObject<EthernetNetDevice>();
n3->AddDevice(swnet1);
Names::Add("SW#02", swnet1);
Ptr<EthernetNetDevice> swnet2 = CreateObject<EthernetNetDevice>();
n3->AddDevice(swnet2);
Names::Add("SW#03", swnet2);
//Create Ethernet Channels and connect switch to the end-stations
Ptr<EthernetChannel> channel0 = CreateObject<EthernetChannel>();
net0->Attach(channel0);
swnet0->Attach(channel0);
Ptr<EthernetChannel> channel1 = CreateObject<EthernetChannel>();
net1->Attach(channel1);
swnet1->Attach(channel1);
Ptr<EthernetChannel> channel2 = CreateObject<EthernetChannel>();
net2->Attach(channel2);
swnet2->Attach(channel2);
//Create and add a switch net device to the switch node
Ptr<SwitchNetDevice> sw = CreateObject<SwitchNetDevice>();
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 2 output port FIFOs for each netDevice.
for (int i=0; i<2; i++){
net0->SetQueue(CreateObject<DropTailQueue<Packet>>());
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 a forwarding table entry
sw->AddForwardingTableEntry(Mac48Address::ConvertFrom(net2->GetAddress()), 1, {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(2));
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));
//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;
}
Network customization
Introduction
In the previous chapter, we wrote a simulation script for a simple Ethernet network. In this chapter, we will discuss how to customize it using the attributes of the different classes used.
Attributes in ns-3
To organize access and configuration of instantiated object parameters, ns-3 uses its own attribute system.
In the previous chapter, we already implemented this overlay using the “SetAttribute()” function to configure the application. This function has two arguments. The first is a string corresponding to the name of the attribute to be changed. The second is the value to be assigned to this attribute.
The various attributes available for each object are visible in the C++ implementation of each object. Here is an example of the EthernetChannel object described in contrib/ethernet/model/ethernet-channel.cc.
TypeId
EthernetChannel::GetTypeId()
{
static TypeId tid =
TypeId("ns3::EthernetChannel")
.SetParent<Channel>()
.SetGroupName("Ethernet")
.AddConstructor<EthernetChannel>()
.AddAttribute("Delay",
"Propagation delay through the channel",
TimeValue(NanoSeconds(25)),
MakeTimeAccessor(&EthernetChannel::m_delay),
MakeTimeChecker())
.AddTraceSource("TxRxEthernet",
"Trace source indicating transmission of packet "
"from the EthernetChannel, used by the Animation "
"interface.",
MakeTraceSourceAccessor(&EthernetChannel::m_txrxEthernet),
"ns3::EthernetChannel::TxRxAnimationCallback");
return tid;
}
We can see that this object has only one attribute called Delay, which allows you to configure the propagation delay. It is of type Time and has a default value of 25ns.
Thus, it is possible to configure the propagation delay in the previous simulation script as follows:
[...]
//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);
[...]
Common attributes
Here are examples of the attributes most often used in Ethernet network simulations.
[...]
//Create and add a netDevice to each end-station node
Ptr<EthernetNetDevice> net0 = CreateObject<EthernetNetDevice>();
net0->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n0->AddDevice(net0);
Names::Add("ES1#01", net0);
Ptr<EthernetNetDevice> net1 = CreateObject<EthernetNetDevice>();
net1->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n1->AddDevice(net1);
Names::Add("ES2#01", net1);
Ptr<EthernetNetDevice> net2 = CreateObject<EthernetNetDevice>();
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<EthernetNetDevice> swnet0 = CreateObject<EthernetNetDevice>();
swnet0->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n3->AddDevice(swnet0);
Names::Add("SW#01", swnet0);
Ptr<EthernetNetDevice> swnet1 = CreateObject<EthernetNetDevice>();
swnet1->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n3->AddDevice(swnet1);
Names::Add("SW#02", swnet1);
Ptr<EthernetNetDevice> swnet2 = CreateObject<EthernetNetDevice>();
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);
[...]
//Create 2 output port FIFOs for each netDevice.
for (int i=0; i<2; i++){
Ptr<DropTailQueue<Packet>> q0 = CreateObject<DropTailQueue<Packet>>();
q0->SetAttribute("MaxSize", QueueSizeValue(QueueSize("250p")));
net0->SetQueue(q0);
[...]
//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(2));
app0->SetAttribute("PayloadSize", UintegerValue(1400));
app0->SetAttribute("Period", TimeValue(Seconds(5)));
app0->SetAttribute("InterFrame", TimeValue(MilliSeconds(20)));
app0->SetAttribute("Jitter", TimeValue(MilliSeconds(50)));
app0->SetAttribute("Offset", TimeValue(Seconds(1)));
app0->SetAttribute("VlanID", UintegerValue(1));
app0->SetAttribute("PCP", UintegerValue(1));
app0->SetAttribute("DEI", UintegerValue(0));
[...]
Final simulation script
Here is the simulation script you should have 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/ethernet-net-device.h"
#include "ns3/ethernet-channel.h"
#include "ns3/switch-net-device.h"
#include "ns3/ethernet-generator.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("Chapter 2");
//A callback to log the pkt emission
static void
MacTxCallback(std::string context, Ptr<const Packet> p)
{
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)
{
NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << " received!");
}
int
main(int argc, char* argv[])
{
//Enable logging
LogComponentEnable("Chapter 2", LOG_LEVEL_INFO);
//Create four nodes
Ptr<Node> n0 = CreateObject<Node>();
Names::Add("ES1", n0);
Ptr<Node> n1 = CreateObject<Node>();
Names::Add("ES2", n1);
Ptr<Node> n2 = CreateObject<Node>();
Names::Add("ES3", n2);
Ptr<Node> n3 = CreateObject<Node>();
Names::Add("SW", n3);
//Create and add a netDevice to each end-station node
Ptr<EthernetNetDevice> net0 = CreateObject<EthernetNetDevice>();
net0->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n0->AddDevice(net0);
Names::Add("ES1#01", net0);
Ptr<EthernetNetDevice> net1 = CreateObject<EthernetNetDevice>();
net1->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n1->AddDevice(net1);
Names::Add("ES2#01", net1);
Ptr<EthernetNetDevice> net2 = CreateObject<EthernetNetDevice>();
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<EthernetNetDevice> swnet0 = CreateObject<EthernetNetDevice>();
swnet0->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n3->AddDevice(swnet0);
Names::Add("SW#01", swnet0);
Ptr<EthernetNetDevice> swnet1 = CreateObject<EthernetNetDevice>();
swnet1->SetAttribute("DataRate", DataRateValue(DataRate("100Mb/s")));
n3->AddDevice(swnet1);
Names::Add("SW#02", swnet1);
Ptr<EthernetNetDevice> swnet2 = CreateObject<EthernetNetDevice>();
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 2 output port FIFOs for each netDevice.
for (int i=0; i<2; i++){
net0->SetQueue(CreateObject<DropTailQueue<Packet>>());
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 a forwarding table entry
sw->AddForwardingTableEntry(Mac48Address::ConvertFrom(net2->GetAddress()), 1, {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(2));
app0->SetAttribute("PayloadSize", UintegerValue(1400));
app0->SetAttribute("Period", TimeValue(Seconds(5)));
app0->SetAttribute("InterFrame", TimeValue(MilliSeconds(20)));
app0->SetAttribute("Jitter", TimeValue(MilliSeconds(50)));
app0->SetAttribute("Offset", TimeValue(Seconds(1)));
app0->SetAttribute("VlanID", UintegerValue(1));
app0->SetAttribute("PCP", UintegerValue(1));
app0->SetAttribute("DEI", UintegerValue(0));
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));
//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;
}
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;
}