Apply icmp-application patch frm lacage

This commit is contained in:
Craig Dowell committed 2008-10-29 11:19:01 -07:00
1 parent 0df5a7183a
commit 6eeec4502a
12 files changed
+559

No files matched your search

+140
View File
@@ -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 <iostream>
#include <fstream>
#include <string>
#include <cassert>
#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<const Packet> 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.");
}
+122
View File
@@ -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 <iostream>
#include <fstream>
#include <string>
#include <cassert>
#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<const Packet> 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.");
}
+8
View File
@@ -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'
+12
View File
@@ -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")
@@ -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")
+155
View File
@@ -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<Application> ()
.AddConstructor<V4Ping> ()
.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> 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> socket)
{
NS_LOG_FUNCTION (this << socket);
while (m_socket->GetRxAvailable () > 0)
{
Address from;
Ptr<Packet> 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<const Packet> 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<Packet> p = Create<Packet> ();
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<Packet> dataPacket = Create<Packet> ((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
+36
View File
@@ -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> socket);
Ipv4Address m_remote;
Ptr<Socket> m_socket;
uint16_t m_seq;
TracedCallback<Time> m_traceRtt;
};
} // namespace ns3
#endif /* V4PING_H */
+13
View File
@@ -0,0 +1,13 @@
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
def build(bld):
module = bld.create_ns3_module('v4ping', ['node'])
module.source = [
'v4ping.cc',
]
headers = bld.create_obj('ns3header')
headers.module = 'v4ping'
headers.source = [
'v4ping.h',
]
+33
View File
@@ -0,0 +1,33 @@
#include "v4ping-helper.h"
#include "ns3/v4ping.h"
namespace ns3 {
V4PingHelper::V4PingHelper (Ipv4Address remote)
{
m_factory.SetTypeId ("ns3::V4Ping");
m_factory.Set ("Remote", Ipv4AddressValue (remote));
}
void
V4PingHelper::SetAttribute (std::string name, const AttributeValue &value)
{
m_factory.Set (name, value);
}
ApplicationContainer
V4PingHelper::Install (NodeContainer nodes)
{
ApplicationContainer apps;
for (NodeContainer::Iterator i = nodes.Begin (); i != nodes.End (); ++i)
{
Ptr<Node> node = *i;
Ptr<V4Ping> ping = m_factory.Create<V4Ping> ();
node->AddApplication (ping);
apps.Add (ping);
}
return apps;
}
} // namespace ns3
+25
View File
@@ -0,0 +1,25 @@
#ifndef V4PING_HELPER_H
#define V4PING_HELPER_H
#include "node-container.h"
#include "application-container.h"
#include "ns3/object-factory.h"
namespace ns3 {
class V4PingHelper
{
public:
V4PingHelper (Ipv4Address remote);
void SetAttribute (std::string name, const AttributeValue &value);
ApplicationContainer Install (NodeContainer nodes);
private:
ObjectFactory m_factory;
};
} // namespace ns3
#endif /* V4PING_HELPER_H */
+2
View File
@@ -21,6 +21,7 @@ def build(bld):
'ipv4-interface-container.cc',
'udp-echo-helper.cc',
'bridge-helper.cc',
'v4ping-helper.cc',
]
headers = bld.create_obj('ns3header')
@@ -44,4 +45,5 @@ def build(bld):
'ipv4-interface-container.h',
'udp-echo-helper.h',
'bridge-helper.h',
'v4ping-helper.h',
]
+1
View File
@@ -29,6 +29,7 @@ all_modules = (
'helper',
'devices/bridge',
'contrib/stats',
'applications/v4ping',
)
def set_options(opt):