diff --git a/examples/traffic-control/examples-to-run.py b/examples/traffic-control/examples-to-run.py new file mode 100755 index 000000000..9399fb317 --- /dev/null +++ b/examples/traffic-control/examples-to-run.py @@ -0,0 +1,20 @@ +#! /usr/bin/env python +## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- + +# A list of C++ examples to run in order to ensure that they remain +# buildable and runnable over time. Each tuple in the list contains +# +# (example_name, do_run, do_valgrind_run). +# +# See test.py for more information. +cpp_examples = [ + ("traffic-control", "True", "True"), +] + +# A list of Python examples to run in order to ensure that they remain +# runnable over time. Each tuple in the list contains +# +# (example_name, do_run). +# +# See test.py for more information. +python_examples = [] diff --git a/examples/traffic-control/traffic-control.cc b/examples/traffic-control/traffic-control.cc new file mode 100644 index 000000000..7c8a1f03c --- /dev/null +++ b/examples/traffic-control/traffic-control.cc @@ -0,0 +1,143 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2015 Universita' degli Studi di Napoli "Federico II" + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Pasquale Imputato + * Author: Stefano Avallone + */ + +#include "ns3/core-module.h" +#include "ns3/network-module.h" +#include "ns3/internet-module.h" +#include "ns3/point-to-point-module.h" +#include "ns3/applications-module.h" +#include "ns3/traffic-control-module.h" + +// This simple example shows how to use TrafficControlHelper to install a QueueDisc on a device. +// The default QueueDisc is a pfifo_fast with max number of packets equal to 1000 (as in Linux) +// +// Network topology +// +// 10.1.1.0 +// n0 -------------- n1 +// point-to-point + +using namespace ns3; + +NS_LOG_COMPONENT_DEFINE ("TrafficControlExample"); + +void +TcPacketsInQueueTrace (uint32_t oldValue, uint32_t newValue) +{ + std::cout << "TcPacketsInQueue " << oldValue << " to " << newValue << std::endl; +} + +void +DevicePacketsInQueueTrace (uint32_t oldValue, uint32_t newValue) +{ + std::cout << "DevicePacketsInQueue " << oldValue << " to " << newValue << std::endl; +} + +int +main (int argc, char *argv[]) +{ + double simulationTime = 10; //seconds + std::string transportProt = "Tcp"; + std::string socketType; + + CommandLine cmd; + cmd.AddValue ("transportProt", "Transport protocol to use: Tcp, Udp", transportProt); + cmd.Parse (argc, argv); + + if (transportProt.compare ("Tcp") == 0) + { + socketType = "ns3::TcpSocketFactory"; + } + else + { + socketType = "ns3::UdpSocketFactory"; + } + + NodeContainer nodes; + nodes.Create (2); + + PointToPointHelper pointToPoint; + pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("10Mbps")); + pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); + pointToPoint.SetQueue ("ns3::DropTailQueue", "Mode", StringValue ("QUEUE_MODE_PACKETS"), "MaxPackets", UintegerValue (1000)); + + NetDeviceContainer devices; + devices = pointToPoint.Install (nodes); + + InternetStackHelper stack; + stack.Install (nodes); + + TrafficControlHelper tch; + uint16_t handle = tch.SetRootQueueDisc ("ns3::PfifoFastQueueDisc"); + // Add three internal queues corresponding to the three bands used by PfifoFast + tch.AddInternalQueues (handle, 3, "ns3::DropTailQueue", "MaxPackets", UintegerValue (1000)); + tch.AddPacketFilter (handle, "ns3::PfifoFastIpv4PacketFilter"); + QueueDiscContainer qdiscs = tch.Install (devices); + + Ptr q = qdiscs.Get (1); + q->TraceConnectWithoutContext ("PacketsInQueue", MakeCallback (&TcPacketsInQueueTrace)); + + Ptr nd = devices.Get (1); + Ptr ptpnd = DynamicCast (nd); + Ptr queue = ptpnd->GetQueue (); + queue->TraceConnectWithoutContext ("PacketsInQueue", MakeCallback (&DevicePacketsInQueueTrace)); + + Ipv4AddressHelper address; + address.SetBase ("10.1.1.0", "255.255.255.0"); + + Ipv4InterfaceContainer interfaces = address.Assign (devices); + + //Flow + uint16_t port = 7; + Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port)); + PacketSinkHelper packetSinkHelper (socketType, localAddress); + ApplicationContainer sinkApp = packetSinkHelper.Install (nodes.Get (0)); + + sinkApp.Start (Seconds (0.0)); + sinkApp.Stop (Seconds (simulationTime + 0.1)); + + uint32_t payloadSize = 1448; + Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize)); + + OnOffHelper onoff (socketType, Ipv4Address::GetAny ()); + onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]")); + onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); + onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize)); + onoff.SetAttribute ("DataRate", StringValue ("50Mbps")); //bit/s + ApplicationContainer apps; + + AddressValue remoteAddress (InetSocketAddress (interfaces.GetAddress (0), port)); + onoff.SetAttribute ("Remote", remoteAddress); + apps.Add (onoff.Install (nodes.Get (1))); + apps.Start (Seconds (1.0)); + apps.Stop (Seconds (simulationTime + 0.1)); + + Simulator::Stop (Seconds (simulationTime + 0.1)); + Simulator::Run (); + Simulator::Destroy (); + + double thr = 0; + uint32_t totalPacketsThr = DynamicCast (sinkApp.Get (0))->GetTotalRx (); + thr = totalPacketsThr * 8 / (simulationTime * 1000000.0); //Mbit/s + std::cout << thr << " Mbit/s" <