Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include "ns3/core-module.h"
- #include "ns3/internet-module.h"
- #include "ns3/internet-apps-module.h"
- #include "ns3/mobility-module.h"
- #include "ns3/spectrum-module.h"
- #include "ns3/propagation-module.h"
- #include "ns3/sixlowpan-module.h"
- #include "ns3/lr-wpan-module.h"
- #include "ns3/csma-module.h"
- #include "ns3/applications-module.h"
- #include "ns3/ipv6-flow-classifier.h"
- #include "ns3/flow-monitor-helper.h"
- #include <ns3/lr-wpan-error-model.h>
- #include "ns3/point-to-point-module.h"
- using namespace ns3;
- // static Ptr<OutputStreamWrapper> cWndStream; //an output stream
- // static Ptr<OutputStreamWrapper> ssThreshStream;
- // static bool firstCwnd = true;
- // static bool firstSshThr = true;
- // static uint32_t cWndValue;
- // static uint32_t ssThreshValue;
- // int tracedNode=1;
- //https://www.nsnam.org/docs/release/3.27/doxygen/tcp-variants-comparison_8cc_source.html#l00069
- // static void CwndTracer (uint32_t oldval, uint32_t newval) {
- // if (firstCwnd) {
- // *cWndStream->GetStream () << "0.0 " << oldval << std::endl;
- // firstCwnd = false;
- // }
- // *cWndStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval << std::endl;
- // cWndValue = newval;
- // if (!firstSshThr) *ssThreshStream->GetStream () << Simulator::Now ().GetSeconds () << " " << ssThreshValue << std::endl;
- // }
- // static void SsThreshTracer (uint32_t oldval, uint32_t newval) {
- // if (firstSshThr) {
- // *ssThreshStream->GetStream () << "0.0 " << oldval << std::endl;
- // firstSshThr = false;
- // }
- // *ssThreshStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval << std::endl;
- // ssThreshValue = newval;
- // if (!firstCwnd) *cWndStream->GetStream () << Simulator::Now ().GetSeconds () << " " << cWndValue << std::endl;
- // }
- // static void TraceCwnd (std::string cwnd_tr_file_name) {
- // AsciiTraceHelper ascii;
- // cWndStream = ascii.CreateFileStream (cwnd_tr_file_name.c_str ());
- // Config::ConnectWithoutContext ("/NodeList/"+std::to_string(tracedNode)+"/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow", MakeCallback (&CwndTracer));
- // }
- // static void TraceSsThresh (std::string ssthresh_tr_file_name) {
- // AsciiTraceHelper ascii;
- // ssThreshStream = ascii.CreateFileStream (ssthresh_tr_file_name.c_str ());
- // Config::ConnectWithoutContext ("/NodeList/"+std::to_string(tracedNode)+"/$ns3::TcpL4Protocol/SocketList/0/SlowStartThreshold", MakeCallback (&SsThreshTracer));
- // }
- bool tracing = true;
- uint16_t nSourceNodes=4;
- uint32_t nWsnNodes;
- uint16_t sinkPort=9;
- uint32_t mtu_bytes = 180;
- uint32_t tcp_adu_size;
- uint64_t data_mbytes = 0;
- double start_time = 0;
- double duration = 100.0;
- double stop_time;
- bool sack = true;
- std::string recovery = "ns3::TcpClassicRecovery";
- std::string congestionAlgo = "TcpNewReno";
- std::string filePrefix;
- Ptr<LrWpanErrorModel> lrWpanError;
- void initialize(int argc, char** argv) {
- CommandLine cmd (__FILE__);
- cmd.AddValue ("tracing", "turn on log components", tracing);
- cmd.AddValue ("nSourceNodes", "turn on log components", nSourceNodes);
- // cmd.AddValue ("tracedNode", "turn on log components", tracedNode);
- cmd.Parse (argc, argv);
- if( tracing ) {
- LogComponentEnable("PacketSink", LOG_LEVEL_INFO);
- }
- nWsnNodes = nSourceNodes + 1;
- // tracedNode = std::max(1, tracedNode);
- filePrefix = "wpan-sourceCount" + std::to_string(nSourceNodes);
- Config::SetDefault ("ns3::TcpL4Protocol::RecoveryType",
- TypeIdValue (TypeId::LookupByName (recovery)));
- congestionAlgo = "ns3::" + congestionAlgo;
- Config::SetDefault ("ns3::TcpL4Protocol::SocketType",
- TypeIdValue (TypeId::LookupByName (congestionAlgo)));
- // 2 MB of TCP buffer
- Config::SetDefault ("ns3::TcpSocket::RcvBufSize", UintegerValue (1 << 21));
- Config::SetDefault ("ns3::TcpSocket::SndBufSize", UintegerValue (1 << 21));
- Config::SetDefault ("ns3::TcpSocketBase::Sack", BooleanValue (sack));
- // Calculate the ADU size
- Header* temp_header = new Ipv6Header ();
- uint32_t ip_header = temp_header->GetSerializedSize ();
- delete temp_header;
- temp_header = new TcpHeader ();
- uint32_t tcp_header = temp_header->GetSerializedSize ();
- delete temp_header;
- tcp_adu_size = mtu_bytes - 20 - (ip_header + tcp_header);
- Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (tcp_adu_size));
- stop_time = start_time + duration;
- lrWpanError = CreateObject<LrWpanErrorModel> ();
- std::cout << "------------------------------------------------------\n";
- std::cout << "Source Count: " << nSourceNodes << "\n";
- std::cout << "------------------------------------------------------\n";
- }
- int main (int argc, char** argv) {
- initialize(argc, argv);
- Packet::EnablePrinting ();
- NodeContainer wsnNodes;
- wsnNodes.Create (nWsnNodes);
- NodeContainer wsnNodesR;
- wsnNodesR.Create (nWsnNodes);
- NodeContainer p2pNodes;
- p2pNodes.Add(wsnNodesR.Get(0));
- p2pNodes.Add(wsnNodes.Get(0));
- MobilityHelper mobility;
- mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
- mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
- "MinX", DoubleValue (0.0),
- "MinY", DoubleValue (0.0),
- "DeltaX", DoubleValue (80),
- "DeltaY", DoubleValue (80),
- "GridWidth", UintegerValue (10),
- "LayoutType", StringValue ("RowFirst"));
- mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
- mobility.Install (wsnNodes);
- mobility.Install(wsnNodesR);
- // mobility.Install(wiredNodes);
- LrWpanHelper lrWpanHelper;
- NetDeviceContainer lrwpanDevices = lrWpanHelper.Install (wsnNodes);
- NetDeviceContainer lrwpanDevicesR = lrWpanHelper.Install (wsnNodesR);
- PointToPointHelper pointToPoint;
- pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
- pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
- NetDeviceContainer p2pDevices;
- p2pDevices = pointToPoint.Install (p2pNodes);
- lrWpanHelper.AssociateToPan (lrwpanDevices, 0); //Associate the nodes to the same PAN.Parameters:a set of nodes,the PAN Id
- lrWpanHelper.AssociateToPan (lrwpanDevicesR, 1);
- InternetStackHelper internetv6;
- internetv6.Install (wsnNodes);
- internetv6.Install (wsnNodesR);
- SixLowPanHelper sixLowPanHelper; //Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
- NetDeviceContainer sixLowPanDevices = sixLowPanHelper.Install (lrwpanDevices); //Install the SixLoWPAN stack on top of an existing NetDevice.
- SixLowPanHelper sixLowPanHelperR;
- NetDeviceContainer sixLowPanDevicesR = sixLowPanHelperR.Install (lrwpanDevicesR);
- // CsmaHelper csmaHelper;
- // NetDeviceContainer csmaDevices = csmaHelper.Install (wiredNodes);
- Ipv6AddressHelper ipv6; //Helper class to auto-assign global IPv6 unicast addresses
- ipv6.SetBase (Ipv6Address ("2001:cafe::"), Ipv6Prefix (64)); //Set the base network number, network prefix, and base interface ID.
- //1st param:Constructs an Ipv6Address by parsing the input C-string.
- //2nd param:Constructs an Ipv6Prefix by using the input string.
- Ipv6InterfaceContainer wiredDeviceInterfaces; //Keep track of a set of IPv6 interfaces
- wiredDeviceInterfaces = ipv6.Assign (p2pDevices); //Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
- wiredDeviceInterfaces.SetForwarding (0, true); //Set the state of the stack (act as a router or as an host) for the specified index.
- //params:index,state(true : is a router, false : is an host)
- wiredDeviceInterfaces.SetDefaultRouteInAllNodes (1);//Set the default route for all the devices (except the router itself). param:router index
- ipv6.SetBase (Ipv6Address ("2001:f00d::"), Ipv6Prefix (64));
- Ipv6InterfaceContainer wsnDeviceInterfaces;
- wsnDeviceInterfaces = ipv6.Assign (sixLowPanDevices);
- wsnDeviceInterfaces.SetForwarding (0, true);
- wsnDeviceInterfaces.SetDefaultRouteInAllNodes (1);
- ipv6.SetBase (Ipv6Address ("2001:f00e::"), Ipv6Prefix (64));
- Ipv6InterfaceContainer wsnDeviceInterfacesR;
- wsnDeviceInterfacesR = ipv6.Assign (sixLowPanDevicesR);
- wsnDeviceInterfacesR.SetForwarding (0, true);
- wsnDeviceInterfacesR.SetDefaultRouteInAllNodes (1);
- for (uint32_t i = 0; i < sixLowPanDevices.GetN (); i++) { //GetN():Get the number of Ptr<NetDevice> stored in this container.
- Ptr<NetDevice> dev = sixLowPanDevices.Get (i); //Get(i):Get the Ptr<NetDevice> stored in this container at a given index.
- dev->SetAttribute ("UseMeshUnder", BooleanValue (true)); //UseMeshUnder (boolean, default false), it enables mesh-under flood routing.
- dev->SetAttribute ("MeshUnderRadius", UintegerValue (10));//the maximum number of hops that a packet will be forwarded
- }
- for (uint32_t i = 0; i < sixLowPanDevicesR.GetN (); i++) { //GetN():Get the number of Ptr<NetDevice> stored in this container.
- Ptr<NetDevice> dev = sixLowPanDevicesR.Get (i); //Get(i):Get the Ptr<NetDevice> stored in this container at a given index.
- dev->SetAttribute ("UseMeshUnder", BooleanValue (true)); //UseMeshUnder (boolean, default false), it enables mesh-under flood routing.
- dev->SetAttribute ("MeshUnderRadius", UintegerValue (10));//the maximum number of hops that a packet will be forwarded
- }
- for( uint32_t i=0; i<nSourceNodes; i++ ) {
- BulkSendHelper sourceApp ("ns3::TcpSocketFactory",
- Inet6SocketAddress (wiredDeviceInterfaces.GetAddress(0,1),
- sinkPort)); //params:protocol,the address of the remote node to send traffic to.
- //GetAddress() params: interface index,address index, generally index 0 is the link-local address
- //InetSocketAddress() params: string which represents an IPv6 address,the port
- sourceApp.SetAttribute ("SendSize", UintegerValue (tcp_adu_size)); //The amount of data to send each time
- sourceApp.SetAttribute ("MaxBytes", UintegerValue (data_mbytes * 1000000)); //The total number of bytes to send.
- ApplicationContainer sourceApps = sourceApp.Install (wsnNodes.Get (i)); //Install an ns3::BulkSendApplication on each node of the input container configured with all the attributes set with SetAttribute.
- sourceApps.Start (Seconds (start_time));
- sourceApps.Stop (Seconds (stop_time));
- PacketSinkHelper sinkApp ("ns3::TcpSocketFactory",
- Inet6SocketAddress (Ipv6Address::GetAny (), sinkPort)); //Get the "any" (::) Ipv6Address.
- sinkApp.SetAttribute ("Protocol", TypeIdValue (TcpSocketFactory::GetTypeId ()));
- ApplicationContainer sinkApps = sinkApp.Install (p2pNodes.Get(1)); //Install an ns3::PacketSinkApplication on each node of the input container configured with all the attributes set with SetAttribute.
- sinkApps.Start (Seconds (0.0));
- sinkApps.Stop (Seconds (stop_time));
- sinkPort++;
- }
- // if (tracing) {
- // // AsciiTraceHelper ascii;
- // // lrWpanHelper.EnableAsciiAll (ascii.CreateFileStream (filePrefix + ".tr")); //Enable ascii trace output on the indicated net device.
- // // lrWpanHelper.EnablePcapAll (filePrefix, false); //Enable pcap output on the indicated net device.
- // // .EnableAsciiAll (ascii.CreateFileStream (filePrefix + ".tr"));
- // // csmaHelper.EnablePcapAll (filePrefix, false);
- // Simulator::Schedule (Seconds (0.00001), &TraceCwnd, filePrefix + "-cwnd.data");
- // // Simulator::Schedule (Seconds (0.00001), &TraceSsThresh, filePrefix + "-ssth.data");
- // }
- FlowMonitorHelper flowHelper;
- flowHelper.InstallAll (); //Enable flow monitoring on all nodes.
- Simulator::Stop (Seconds (stop_time));
- Simulator::Run ();
- flowHelper.SerializeToXmlFile ("lowRate.flowmonitor", false,false); //Serializes the results to a file in XML format.
- Simulator::Destroy ();
- return 0;
- }
- // #include <fstream>
- // #include "ns3/core-module.h"
- // #include "ns3/internet-module.h"
- // #include "ns3/internet-apps-module.h"
- // #include "ns3/mobility-module.h"
- // #include "ns3/spectrum-module.h"
- // #include "ns3/propagation-module.h"
- // #include "ns3/sixlowpan-module.h"
- // #include "ns3/lr-wpan-module.h"
- // #include "ns3/csma-module.h"
- // #include "ns3/applications-module.h"
- // #include "ns3/ipv6-flow-classifier.h"
- // #include "ns3/flow-monitor-helper.h"
- // #include <ns3/lr-wpan-error-model.h>
- // using namespace ns3;
- // int main (int argc, char** argv) {
- // uint16_t nFlow=3;
- // uint32_t nWifiNodes=10;
- // uint16_t sinkPort=9;
- // uint32_t mtu_bytes = 180;
- // uint32_t tcp_adu_size;
- // // uint64_t data_mbytes = 10;
- // std::string congestionAlgo = "TcpBbr";
- // uint32_t packetSize = 1000;
- // congestionAlgo = "ns3::" + congestionAlgo;
- // Config::SetDefault ("ns3::TcpL4Protocol::SocketType",
- // TypeIdValue (TypeId::LookupByName (congestionAlgo)));
- // // Calculate the ADU size
- // Header* temp_header = new Ipv6Header ();
- // uint32_t ip_header = temp_header->GetSerializedSize ();
- // delete temp_header;
- // temp_header = new TcpHeader ();
- // uint32_t tcp_header = temp_header->GetSerializedSize ();
- // delete temp_header;
- // tcp_adu_size = mtu_bytes - 20 - (ip_header + tcp_header);
- // Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (tcp_adu_size));
- // NodeContainer wifiNodes;
- // wifiNodes.Create (nWifiNodes);
- // NodeContainer csmaNodes;
- // csmaNodes.Create (1);
- // csmaNodes.Add (wifiNodes.Get (0));
- // MobilityHelper mobility;
- // mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
- // mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
- // "MinX", DoubleValue (0.0),
- // "MinY", DoubleValue (0.0),
- // "DeltaX", DoubleValue (80),
- // "DeltaY", DoubleValue (80),
- // "GridWidth", UintegerValue (10),
- // "LayoutType", StringValue ("RowFirst"));
- // mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
- // mobility.Install (wifiNodes);
- // LrWpanHelper lrWpanHelper;
- // NetDeviceContainer lrwpanDevices = lrWpanHelper.Install (wifiNodes);
- // lrWpanHelper.AssociateToPan (lrwpanDevices, 0); //Associate the nodes to the same PAN.Parameters:a set of nodes,the PAN Id
- // InternetStackHelper internetv6;
- // internetv6.Install (wifiNodes);
- // internetv6.Install (csmaNodes.Get (0));
- // SixLowPanHelper sixLowPanHelper; //Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
- // NetDeviceContainer sixLowPanDevices = sixLowPanHelper.Install (lrwpanDevices); //Install the SixLoWPAN stack on top of an existing NetDevice.
- // CsmaHelper csmaHelper;
- // NetDeviceContainer csmaDevices = csmaHelper.Install (csmaNodes);
- // Ipv6AddressHelper ipv6; //Helper class to auto-assign global IPv6 unicast addresses
- // ipv6.SetBase (Ipv6Address ("2001:cafe::"), Ipv6Prefix (64)); //Set the base network number, network prefix, and base interface ID.
- // //1st param:Constructs an Ipv6Address by parsing the input C-string.
- // //2nd param:Constructs an Ipv6Prefix by using the input string.
- // Ipv6InterfaceContainer csmaDeviceInterfaces; //Keep track of a set of IPv6 interfaces
- // csmaDeviceInterfaces = ipv6.Assign (csmaDevices); //Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
- // csmaDeviceInterfaces.SetForwarding (1, true); //Set the state of the stack (act as a router or as an host) for the specified index.
- // //params:index,state(true : is a router, false : is an host)
- // csmaDeviceInterfaces.SetDefaultRouteInAllNodes (1);//Set the default route for all the devices (except the router itself). param:router index
- // ipv6.SetBase (Ipv6Address ("2001:f00d::"), Ipv6Prefix (64));
- // Ipv6InterfaceContainer wifiDeviceInterfaces;
- // wifiDeviceInterfaces = ipv6.Assign (sixLowPanDevices);
- // wifiDeviceInterfaces.SetForwarding (0, true);
- // wifiDeviceInterfaces.SetDefaultRouteInAllNodes (0);
- // for (uint32_t i = 0; i < sixLowPanDevices.GetN (); i++) { //GetN():Get the number of Ptr<NetDevice> stored in this container.
- // Ptr<NetDevice> dev = sixLowPanDevices.Get (i); //Get(i):Get the Ptr<NetDevice> stored in this container at a given index.
- // dev->SetAttribute ("UseMeshUnder", BooleanValue (true)); //UseMeshUnder (boolean, default false), it enables mesh-under flood routing.
- // dev->SetAttribute ("MeshUnderRadius", UintegerValue (10));//the maximum number of hops that a packet will be forwarded
- // }
- // for( int i=1; i<=nFlow; i++ ) {
- // OnOffHelper server ("ns3::TcpSocketFactory", Inet6SocketAddress (csmaDeviceInterfaces.GetAddress (0, 1),sinkPort));
- // server.SetAttribute ("PacketSize", UintegerValue (packetSize));
- // server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
- // server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
- // server.SetAttribute ("DataRate", DataRateValue (DataRate ("2Mbps")));
- // ApplicationContainer serverApp = server.Install (wifiNodes.Get(i));
- // serverApp.Start(Seconds(1.0));
- // PacketSinkHelper sinkApp ("ns3::TcpSocketFactory",
- // Inet6SocketAddress (Ipv6Address::GetAny (), sinkPort)); //Get the "any" (::) Ipv6Address.
- // sinkApp.SetAttribute ("Protocol", TypeIdValue (TcpSocketFactory::GetTypeId ()));
- // ApplicationContainer sinkApps = sinkApp.Install (csmaNodes.Get(0)); //Install an ns3::PacketSinkApplication on each node of the input container configured with all the attributes set with SetAttribute.
- // sinkApps.Start (Seconds (0.0));
- // sinkApps.Stop (Seconds (50.0));
- // sinkPort++;
- // }
- // FlowMonitorHelper flowHelper;
- // flowHelper.InstallAll (); //Enable flow monitoring on all nodes.
- // Simulator::Stop (Seconds (100.0));
- // Simulator::Run ();
- // flowHelper.SerializeToXmlFile ("lowRate.flowmonitor", false,false); //Serializes the results to a file in XML format.
- // Simulator::Destroy ();
- // return 0;
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement