#include "ns3/core-module.h" #include "ns3/applications-module.h" #include "ns3/command-line.h" #include "ns3/simulator.h" #include "ns3/node.h" #include "ns3/drop-tail-queue.h" #include "ns3/tsn-net-device.h" #include "ns3/ethernet-channel.h" #include "ns3/ethernet-generator.h" #include "ns3/ethernet-header2.h" #include "ns3/stream-identification-function-null.h" /** * \file * * Example of the use of tsn-node.cc tsn-net-device.cc ethernet-channel.cc * on a network composed of two end-stations connected by a 100Mb/s * full duplex link * ES1 ====== ES2 */ using namespace ns3; NS_LOG_COMPONENT_DEFINE("Example"); //A callback to log the pkt transmission static void MacTxCallback(std::string context, Ptr p) { NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << " entered the transmission FIFO !"); } //A callback to log the pkt reception static void MacRxCallback(std::string context, Ptr p) { NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << context << " : Pkt #" << p->GetUid() << " received !"); } //Callbacks to log pkt drop static void MaxSDUSizeFilterDrop(std::string context, Ptr p) { NS_LOG_INFO(context << " : Packet #"<< p->GetUid() <<" was dropped by MaxSDUSizeFilter"); } static void REDFrameDrop(std::string context, Ptr p) { NS_LOG_INFO(context << " : Packet #"<< p->GetUid() <<" was dropped by Flow Meter"); } int main(int argc, char* argv[]) { //Enable logging LogComponentEnable("Example", LOG_LEVEL_INFO); LogComponentEnable("TsnNetDevice", LOG_LEVEL_INFO); LogComponentEnable("StreamFilterInstance", LOG_LEVEL_INFO); LogComponentEnable("FlowMeterInstance", LOG_LEVEL_INFO); CommandLine cmd(__FILE__); cmd.Parse(argc, argv); //Create two nodes Ptr n0 = CreateObject(); Names::Add("ES1", n0); Ptr n1 = CreateObject(); Names::Add("ES2", n1); //Create and add a netDevice to each node Ptr net0 = CreateObject(); net0->SetAttribute("DataRate", DataRateValue(DataRate("1Gb/s"))); n0->AddDevice(net0); Names::Add("ES1#01", net0); Ptr net1 = CreateObject(); net1->SetAttribute("DataRate", DataRateValue(DataRate("1Gb/s"))); n1->AddDevice(net1); Names::Add("ES2#01", net1); //Create a Ethernet Channel and attach it two the two netDevices Ptr channel = CreateObject(); net0->Attach(channel); net1->Attach(channel); //Allocate a Mac address and create a FIFO (for the output port) //for each netDevice. net0->SetAddress(Mac48Address::Allocate()); net0->SetQueue(CreateObject>()); net1->SetAddress(Mac48Address::Allocate()); net1->SetQueue(CreateObject>()); //Stream identification Ptr sif0 = CreateObject(); uint16_t StreamHandle = 10; sif0->SetAttribute("VlanID", UintegerValue(100)); sif0->SetAttribute("Address", AddressValue(Mac48Address("ff:ff:ff:ff:ff:ff"))); n1->AddStreamIdentificationFunction(StreamHandle, sif0, {net1}, {}, {}, {}); //PSFP configuration Ptr sfi0 = CreateObject(); sfi0->SetAttribute("StreamHandle", IntegerValue(StreamHandle)); sfi0->SetAttribute("Priority", IntegerValue(-1)); //-1 = wildcard (like in PSFP MIB) sfi0->SetAttribute("MaxSDUSize", UintegerValue(0)); n1->AddStreamFilter(sfi0); Ptr fm0 = CreateObject(); fm0->SetAttribute("CIR", DataRateValue(DataRate("40Mb/s"))); fm0->SetAttribute("CBS", UintegerValue(1000)); fm0->SetAttribute("DropOnYellow", BooleanValue(true)); fm0->SetAttribute("MarkAllFramesRedEnable", BooleanValue(false)); uint16_t fmid = n1->AddFlowMeter(fm0); sfi0->AddFlowMeterInstanceId(fmid); //Application description Ptr app0 = CreateObject(); app0->Setup(net0); app0->SetAttribute("BurstSize", UintegerValue(4)); app0->SetAttribute("PayloadSize", UintegerValue(500)); app0->SetAttribute("Period", TimeValue(Seconds(5))); app0->SetAttribute("PCP", UintegerValue(0)); app0->SetAttribute("VlanID", UintegerValue(100)); n0->AddApplication(app0); app0->SetStartTime(Seconds(5)); app0->SetStopTime(Seconds(12)); //Callback to display the packet log std::string context = Names::FindName(n0) + ":" + Names::FindName(net0); net0->TraceConnectWithoutContext("MacTx", MakeBoundCallback(&MacTxCallback, context)); context = Names::FindName(n1) + ":" + Names::FindName(net1); net1->TraceConnectWithoutContext("MacRx", MakeBoundCallback(&MacRxCallback, context)); net1->TraceConnectWithoutContext("MaxSDUSizeFilterDrop", MakeBoundCallback(&MaxSDUSizeFilterDrop, Names::FindName(net1))); net1->TraceConnectWithoutContext("REDFrameDrop", MakeBoundCallback(&REDFrameDrop, Names::FindName(net1))); //Execute the simulation Simulator::Stop(Seconds(15)); Simulator::Run(); Simulator::Destroy(); return 0; }