diff --git a/examples/csma-ping.cc b/examples/csma-ping.cc new file mode 100644 index 000000000..ffddd9c6f --- /dev/null +++ b/examples/csma-ping.cc @@ -0,0 +1,140 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * 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 + */ + +// Port of ns-2/tcl/ex/simple.tcl to ns-3 +// +// Network topology +// +// n0 n1 n2 n3 +// | | | | +// ===================== +// +// - CBR/UDP flows from n0 to n1, and from n3 to n0 +// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec. +// (i.e., DataRate of 448,000 bps) +// - DropTail queues +// - Tracing of queues and packet receptions to file "csma-one-subnet.tr" + +#include +#include +#include +#include + +#include "ns3/core-module.h" +#include "ns3/simulator-module.h" +#include "ns3/node-module.h" +#include "ns3/helper-module.h" +#include "ns3/global-route-manager.h" + +using namespace ns3; + +NS_LOG_COMPONENT_DEFINE ("CsmaPingExample"); + +static void SinkRx (Ptr p, const Address &ad) +{ + //std::cout << *p << std::endl; +} + +static void PingRtt (std::string context, Time rtt) +{ + //std::cout << context << " " << rtt << std::endl; +} + +int +main (int argc, char *argv[]) +{ + // + // Make the random number generators generate reproducible results. + // + RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8); + + CommandLine cmd; + cmd.Parse (argc, argv); + + // Here, we will explicitly create four nodes. + NS_LOG_INFO ("Create nodes."); + NodeContainer c; + c.Create (4); + + // connect all our nodes to a shared channel. + NS_LOG_INFO ("Build Topology."); + CsmaHelper csma; + csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); + csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); + csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc")); + NetDeviceContainer devs = csma.Install (c); + + // add an ip stack to all nodes. + NS_LOG_INFO ("Add ip stack."); + InternetStackHelper ipStack; + ipStack.Install (c); + + // assign ip addresses + NS_LOG_INFO ("Assign ip addresses."); + Ipv4AddressHelper ip; + ip.SetBase ("192.168.1.0", "255.255.255.0"); + Ipv4InterfaceContainer addresses = ip.Assign (devs); + + // setup global router + GlobalRouteManager::PopulateRoutingTables (); + NS_LOG_INFO ("Create Source"); + Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2")); + InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3)); + OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst); + onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1.0))); + onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0))); + onoff.SetAttribute ("DataRate", DataRateValue (DataRate (15000))); + onoff.SetAttribute ("PacketSize", UintegerValue (1200)); + + + ApplicationContainer apps = onoff.Install (c.Get (0)); + apps.Start (Seconds (1.0)); + apps.Stop (Seconds (10.0)); + + NS_LOG_INFO ("Create Sink."); + PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst); + apps = sink.Install (c.Get (3)); + apps.Start (Seconds (0.0)); + apps.Stop (Seconds (11.0)); + + NS_LOG_INFO ("Create pinger"); + V4PingHelper ping = V4PingHelper (addresses.GetAddress (2)); + NodeContainer pingers; + pingers.Add (c.Get (0)); + pingers.Add (c.Get (1)); + pingers.Add (c.Get (3)); + apps = ping.Install (pingers); + apps.Start (Seconds (2.0)); + apps.Stop (Seconds (5.0)); + + NS_LOG_INFO ("Configure Tracing."); + // first, pcap tracing. + csma.EnablePcapAll ("csma-ping"); + // then, print what the packet sink receives. + Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx", + MakeCallback (&SinkRx)); + // finally, print the ping rtts. + Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt", + MakeCallback (&PingRtt)); + + Packet::EnablePrinting (); + + + NS_LOG_INFO ("Run Simulation."); + Simulator::Run (); + Simulator::Destroy (); + NS_LOG_INFO ("Done."); +} diff --git a/examples/csma-raw-ip-socket.cc b/examples/csma-raw-ip-socket.cc new file mode 100644 index 000000000..37f4dab51 --- /dev/null +++ b/examples/csma-raw-ip-socket.cc @@ -0,0 +1,122 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * 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 + */ + +// Port of ns-2/tcl/ex/simple.tcl to ns-3 +// +// Network topology +// +// n0 n1 n2 n3 +// | | | | +// ===================== +// +// - CBR/UDP flows from n0 to n1, and from n3 to n0 +// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec. +// (i.e., DataRate of 448,000 bps) +// - DropTail queues +// - Tracing of queues and packet receptions to file "csma-one-subnet.tr" + +#include +#include +#include +#include + +#include "ns3/core-module.h" +#include "ns3/simulator-module.h" +#include "ns3/node-module.h" +#include "ns3/helper-module.h" + +using namespace ns3; + +NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample"); + +static void SinkRx (Ptr p, const Address &ad) +{ + //std::cout << *p << std::endl; +} + +int +main (int argc, char *argv[]) +{ +#if 0 + LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO); +#endif + + // + // Make the random number generators generate reproducible results. + // + RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8); + + CommandLine cmd; + cmd.Parse (argc, argv); + + // Here, we will explicitly create four nodes. + NS_LOG_INFO ("Create nodes."); + NodeContainer c; + c.Create (4); + + // connect all our nodes to a shared channel. + NS_LOG_INFO ("Build Topology."); + CsmaHelper csma; + csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); + csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); + csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc")); + NetDeviceContainer devs = csma.Install (c); + + // add an ip stack to all nodes. + NS_LOG_INFO ("Add ip stack."); + InternetStackHelper ipStack; + ipStack.Install (c); + + // assign ip addresses + NS_LOG_INFO ("Assign ip addresses."); + Ipv4AddressHelper ip; + ip.SetBase ("192.168.1.0", "255.255.255.0"); + Ipv4InterfaceContainer addresses = ip.Assign (devs); + + NS_LOG_INFO ("Create Source"); + Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2")); + InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3)); + OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst); + onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1.0))); + onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0))); + onoff.SetAttribute ("DataRate", DataRateValue (DataRate (6000))); + onoff.SetAttribute ("PacketSize", UintegerValue (1200)); + + ApplicationContainer apps = onoff.Install (c.Get (0)); + apps.Start (Seconds (1.0)); + apps.Stop (Seconds (10.0)); + + NS_LOG_INFO ("Create Sink."); + PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst); + apps = sink.Install (c.Get (3)); + apps.Start (Seconds (0.0)); + apps.Stop (Seconds (11.0)); + + NS_LOG_INFO ("Configure Tracing."); + // first, pcap tracing. + csma.EnablePcapAll ("csma-raw-ip-socket"); + // then, print what the packet sink receives. + Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx", + MakeCallback (&SinkRx)); + + Packet::EnablePrinting (); + + + NS_LOG_INFO ("Run Simulation."); + Simulator::Run (); + Simulator::Destroy (); + NS_LOG_INFO ("Done."); +} diff --git a/examples/wscript b/examples/wscript index e68afba06..4cae9dfc4 100644 --- a/examples/wscript +++ b/examples/wscript @@ -94,3 +94,11 @@ def build(bld): ['core', 'simulator', 'mobility', 'wifi', 'csma', 'helper', 'bridge']) obj.source = 'wifi-wired-bridging.cc' + + obj = bld.create_ns3_program('csma-raw-ip-socket', + ['csma', 'internet-stack']) + obj.source = 'csma-raw-ip-socket.cc' + + obj = bld.create_ns3_program('csma-ping', + ['csma', 'internet-stack', 'v4ping']) + obj.source = 'csma-ping.cc' diff --git a/regression/tests/test-csma-ping.py b/regression/tests/test-csma-ping.py new file mode 100644 index 000000000..b1fe044b1 --- /dev/null +++ b/regression/tests/test-csma-ping.py @@ -0,0 +1,12 @@ +#! /usr/bin/env python + +"""Generic trace-comparison-type regression test.""" + +import os +import shutil +import tracediff + +def run(verbose, generate, refDirName): + """Execute a test.""" + + return tracediff.run_test(verbose, generate, refDirName, "csma-ping") diff --git a/regression/tests/test-csma-raw-ip-socket.py b/regression/tests/test-csma-raw-ip-socket.py new file mode 100644 index 000000000..e6e916be6 --- /dev/null +++ b/regression/tests/test-csma-raw-ip-socket.py @@ -0,0 +1,12 @@ +#! /usr/bin/env python + +"""Generic trace-comparison-type regression test.""" + +import os +import shutil +import tracediff + +def run(verbose, generate, refDirName): + """Execute a test.""" + + return tracediff.run_test(verbose, generate, refDirName, "csma-raw-ip-socket") diff --git a/src/applications/v4ping/v4ping.cc b/src/applications/v4ping/v4ping.cc new file mode 100644 index 000000000..e70917e60 --- /dev/null +++ b/src/applications/v4ping/v4ping.cc @@ -0,0 +1,155 @@ +#include "v4ping.h" +#include "ns3/icmpv4.h" +#include "ns3/assert.h" +#include "ns3/log.h" +#include "ns3/ipv4-address.h" +#include "ns3/socket.h" +#include "ns3/uinteger.h" +#include "ns3/inet-socket-address.h" +#include "ns3/packet.h" +#include "ns3/trace-source-accessor.h" +#include "ns3/simulator.h" + +namespace ns3 { + +NS_LOG_COMPONENT_DEFINE ("V4Ping"); +NS_OBJECT_ENSURE_REGISTERED (V4Ping); + +TypeId +V4Ping::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::V4Ping") + .SetParent () + .AddConstructor () + .AddAttribute ("Remote", + "The address of the machine we want to ping.", + Ipv4AddressValue (), + MakeIpv4AddressAccessor (&V4Ping::m_remote), + MakeIpv4AddressChecker ()) + .AddTraceSource ("Rtt", + "The rtt calculated by the ping.", + MakeTraceSourceAccessor (&V4Ping::m_traceRtt)); + ; + return tid; +} + +V4Ping::V4Ping () + : m_socket (0), + m_seq (0) +{} +V4Ping::~V4Ping () +{} + +void +V4Ping::DoDispose (void) +{ + NS_LOG_FUNCTION (this); + m_socket = 0; + Application::DoDispose (); +} + +uint32_t +V4Ping::GetApplicationId (void) const +{ + Ptr node = GetNode (); + for (uint32_t i = 0; i < node->GetNApplications (); ++i) + { + if (node->GetApplication (i) == this) + { + return i; + } + } + NS_ASSERT_MSG (false, "forgot to add application to node"); + return 0; // quiet compiler +} + +void +V4Ping::Receive (Ptr socket) +{ + NS_LOG_FUNCTION (this << socket); + while (m_socket->GetRxAvailable () > 0) + { + Address from; + Ptr p = m_socket->RecvFrom (0xffffffff, 0, from); + NS_LOG_DEBUG ("recv " << p->GetSize () << " bytes"); + NS_ASSERT (InetSocketAddress::IsMatchingType (from)); + InetSocketAddress realFrom = InetSocketAddress::ConvertFrom (from); + NS_ASSERT (realFrom.GetPort () == 1); // protocol should be icmp. + Ipv4Header ipv4; + p->RemoveHeader (ipv4); + NS_ASSERT (ipv4.GetProtocol () == 1); // protocol should be icmp. + Icmpv4Header icmp; + p->RemoveHeader (icmp); + if (icmp.GetType () == Icmpv4Header::ECHO_REPLY) + { + Icmpv4Echo echo; + p->RemoveHeader (echo); + if (echo.GetSequenceNumber () == (m_seq - 1) && + echo.GetIdentifier () == 0) + { + Ptr data = echo.GetData (); + if (data->GetSize () == 16) + { + uint32_t *buf = (uint32_t *)data->PeekData (); + if (buf[0] == GetNode ()->GetId () && + buf[1] == GetApplicationId ()) + { + int64_t ts = buf[3]; + ts <<= 32; + ts |= buf[2]; + Time sendTime = TimeStep (ts); + NS_ASSERT (Simulator::Now () > sendTime); + Time delta = Simulator::Now () - sendTime; + m_traceRtt (delta); + } + } + } + } + } +} + +void +V4Ping::StartApplication (void) +{ + NS_LOG_FUNCTION (this); + m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::Ipv4RawSocketFactory")); + NS_ASSERT (m_socket != 0); + m_socket->SetAttribute ("Protocol", UintegerValue (1)); // icmp + m_socket->SetRecvCallback (MakeCallback (&V4Ping::Receive, this)); + InetSocketAddress src = InetSocketAddress (Ipv4Address::GetAny (), 0); + int status; + status = m_socket->Bind (src); + NS_ASSERT (status != -1); + InetSocketAddress dst = InetSocketAddress (m_remote, 0); + status = m_socket->Connect (dst); + NS_ASSERT (status != -1); + Ptr p = Create (); + Icmpv4Echo echo; + echo.SetSequenceNumber (m_seq); + m_seq++; + echo.SetIdentifier (0); + uint32_t data[4]; + data[0] = GetNode ()->GetId (); + data[1] = GetApplicationId (); + int64_t now = Simulator::Now ().GetTimeStep (); + data[2] = now & 0xffffffff; + now >>= 32; + data[3] = now & 0xffffffff; + Ptr dataPacket = Create ((uint8_t *) &data, 16); + echo.SetData (dataPacket); + p->AddHeader (echo); + Icmpv4Header header; + header.SetType (Icmpv4Header::ECHO); + header.SetCode (0); + p->AddHeader (header); + m_socket->Send (p, 0); + +} +void +V4Ping::StopApplication (void) +{ + NS_LOG_FUNCTION (this); +} + + +} // namespace ns3 diff --git a/src/applications/v4ping/v4ping.h b/src/applications/v4ping/v4ping.h new file mode 100644 index 000000000..800ab1fbb --- /dev/null +++ b/src/applications/v4ping/v4ping.h @@ -0,0 +1,36 @@ +#ifndef V4PING_H +#define V4PING_H + +#include "ns3/application.h" +#include "ns3/traced-callback.h" +#include "ns3/nstime.h" + +namespace ns3 { + +class Socket; + +class V4Ping : public Application +{ +public: + static TypeId GetTypeId (void); + + V4Ping (); + virtual ~V4Ping (); + +private: + // inherited from Application base class. + virtual void StartApplication (void); + virtual void StopApplication (void); + virtual void DoDispose (void); + uint32_t GetApplicationId (void) const; + void Receive (Ptr socket); + + Ipv4Address m_remote; + Ptr m_socket; + uint16_t m_seq; + TracedCallback