From da9f8a0aae87a07e1fdc2c0ae312a2c48e312cf3 Mon Sep 17 00:00:00 2001 From: Sebastien Vincent Date: Sun, 18 Oct 2009 16:29:38 +0200 Subject: [PATCH] Add examples (loose-routing-ipv6 not working yet). --- examples/ipv6/fragmentation-ipv6.cc | 164 ++++++++++++++++++++++ examples/ipv6/loose-routing-ipv6.cc | 202 ++++++++++++++++++++++++++++ examples/ipv6/wscript | 10 +- 3 files changed, 373 insertions(+), 3 deletions(-) create mode 100644 examples/ipv6/fragmentation-ipv6.cc create mode 100644 examples/ipv6/loose-routing-ipv6.cc diff --git a/examples/ipv6/fragmentation-ipv6.cc b/examples/ipv6/fragmentation-ipv6.cc new file mode 100644 index 000000000..d195db42f --- /dev/null +++ b/examples/ipv6/fragmentation-ipv6.cc @@ -0,0 +1,164 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2008-2009 Strasbourg University + * + * 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: David Gross + * Sebastien Vincent + */ + +// Network topology +// // +// // n0 r n1 +// // | _ | +// // ====|_|==== +// // router +// // +// // - Tracing of queues and packet receptions to file "fragmentation-ipv6.tr" + +#include +#include "ns3/core-module.h" +#include "ns3/simulator-module.h" +#include "ns3/helper-module.h" + +#include "ns3/ipv6-routing-table-entry.h" + +using namespace ns3; + +NS_LOG_COMPONENT_DEFINE ("FragmentationIpv6Example"); + +/** + * \class StackHelper + * \brief Helper to set or get some IPv6 information about nodes. + */ +class StackHelper +{ + public: + + /** + * \brief Add an address to a IPv6 node. + * \param n node + * \param interface interface index + * \param address IPv6 address to add + */ + inline void AddAddress (Ptr& n, uint32_t interface, Ipv6Address address) + { + Ptr ipv6 = n->GetObject (); + ipv6->AddAddress (interface, address); + } + + /** + * \brief Print the routing table. + * \param n the node + */ + inline void PrintRoutingTable (Ptr& n) + { + Ptr routing = 0; + Ipv6StaticRoutingHelper routingHelper; + Ptr ipv6 = n->GetObject (); + uint32_t nbRoutes = 0; + Ipv6RoutingTableEntry route; + + routing = routingHelper.GetStaticRouting (ipv6); + + std::cout << "Routing table of " << n << " : " << std::endl; + std::cout << "Destination\t\t\t\t" << "Gateway\t\t\t\t\t" << "Interface\t" << "Prefix to use" << std::endl; + + nbRoutes = routing->GetNRoutes (); + for (uint32_t i = 0 ; i < nbRoutes ; i++) + { + route = routing->GetRoute (i); + std::cout << route.GetDest () << "\t" + << route.GetGateway () << "\t" + << route.GetInterface () << "\t" + << route.GetPrefixToUse () << "\t" + << std::endl; + } + } +}; + +int main (int argc, char** argv) +{ +#if 0 + LogComponentEnable ("Ipv6L3Protocol", LOG_LEVEL_ALL); + LogComponentEnable ("Icmpv6L4Protocol", LOG_LEVEL_ALL); + LogComponentEnable ("Ipv6StaticRouting", LOG_LEVEL_ALL); + LogComponentEnable ("Ipv6Interface", LOG_LEVEL_ALL); + LogComponentEnable ("Ping6Application", LOG_LEVEL_ALL); +#endif + + CommandLine cmd; + cmd.Parse (argc, argv); + + StackHelper stackHelper; + + NS_LOG_INFO ("Create nodes."); + Ptr n0 = CreateObject (); + Ptr r = CreateObject (); + Ptr n1 = CreateObject (); + + NodeContainer net1 (n0, r); + NodeContainer net2 (r, n1); + NodeContainer all (n0, r, n1); + + NS_LOG_INFO ("Create IPv6 Internet Stack"); + InternetStackHelper internetv6; + internetv6.Install (all); + + NS_LOG_INFO ("Create channels."); + CsmaHelper csma; + csma.SetChannelAttribute ("DataRate", DataRateValue (5000000)); + csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); + NetDeviceContainer d1 = csma.Install (net1); + NetDeviceContainer d2 = csma.Install (net2); + + NS_LOG_INFO ("Create networks and assign IPv6 Addresses."); + Ipv6AddressHelper ipv6; + ipv6.NewNetwork (Ipv6Address ("2001:1::"), 64); + Ipv6InterfaceContainer i1 = ipv6.Assign (d1); + i1.SetRouter (1, true); + ipv6.NewNetwork (Ipv6Address ("2001:2::"), 64); + Ipv6InterfaceContainer i2 = ipv6.Assign (d2); + i2.SetRouter (0, true); + + stackHelper.PrintRoutingTable(n0); + + /* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r */ + uint32_t packetSize = 4096; + uint32_t maxPacketCount = 5; + Time interPacketInterval = Seconds (1.); + Ping6Helper ping6; + + ping6.SetLocal (i1.GetAddress (0, 1)); + ping6.SetRemote (i2.GetAddress (1, 1)); + + ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); + ping6.SetAttribute ("Interval", TimeValue (interPacketInterval)); + ping6.SetAttribute ("PacketSize", UintegerValue (packetSize)); + ApplicationContainer apps = ping6.Install (net1.Get (0)); + apps.Start (Seconds (2.0)); + apps.Stop (Seconds (20.0)); + + std::ofstream ascii; + ascii.open ("fragmentation-ipv6.tr"); + CsmaHelper::EnablePcapAll (std::string ("fragmentation-ipv6"), true); + CsmaHelper::EnableAsciiAll (ascii); + + NS_LOG_INFO ("Run Simulation."); + Simulator::Run (); + Simulator::Destroy (); + NS_LOG_INFO ("Done."); +} + diff --git a/examples/ipv6/loose-routing-ipv6.cc b/examples/ipv6/loose-routing-ipv6.cc new file mode 100644 index 000000000..fd4fcc18c --- /dev/null +++ b/examples/ipv6/loose-routing-ipv6.cc @@ -0,0 +1,202 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 Strasbourg University + * + * 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: David Gross + */ + +// Network topology +// // +// // +// // +------------+ +// // +------------+ |---| Router 1 |---| +// // | Host 0 |--| | [------------] | +// // [------------] | | | +// // | +------------+ | +// // +--| | +------------+ +// // | Router 0 | | Router 2 | +// // +--| | [------------] +// // | [------------] | +// // +------------+ | | | +// // | Host 1 |--| | +------------+ | +// // [------------] |---| Router 3 |---| +// // [------------] +// // +// // +// // - Tracing of queues and packet receptions to file "loose-routing-ipv6.tr" + +#include +#include "ns3/core-module.h" +#include "ns3/simulator-module.h" +#include "ns3/helper-module.h" +#include "ns3/ipv6-header.h" +using namespace ns3; + +NS_LOG_COMPONENT_DEFINE ("LooseRoutingIpv6Example"); + +int main (int argc, char **argv) +{ +#if 0 + LogComponentEnable("Ipv6EndPointDemux", LOG_LEVEL_ALL); + LogComponentEnable("Udp6Socket", LOG_LEVEL_ALL); + LogComponentEnable("Udp6L4Protocol", LOG_LEVEL_ALL); + LogComponentEnable("Ipv6L3Protocol", LOG_LEVEL_ALL); + LogComponentEnable("Ipv6StaticRouting", LOG_LEVEL_ALL); + LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL); + LogComponentEnable("NdiscIpv6Interface", LOG_LEVEL_ALL); + LogComponentEnable("Ipv6LoopbackInterface", LOG_LEVEL_ALL); + LogComponentEnable("OnOffApplication", LOG_LEVEL_ALL); + LogComponentEnable("PacketSinkApplication", LOG_LEVEL_ALL); + LogComponentEnable("UdpEcho6ClientApplication", LOG_LEVEL_ALL); + LogComponentEnable("UdpEcho6ServerApplication", LOG_LEVEL_ALL); + LogComponentEnable("NdiscCache", LOG_LEVEL_ALL); + LogComponentEnable("Ipv6Socket", LOG_LEVEL_ALL); +#endif + + CommandLine cmd; + cmd.Parse (argc, argv); + + NS_LOG_INFO ("Create nodes."); + Ptr h0 = CreateObject (); + Ptr h1 = CreateObject (); + Ptr r0 = CreateObject (); + Ptr r1 = CreateObject (); + Ptr r2 = CreateObject (); + Ptr r3 = CreateObject (); + + NodeContainer net1 (h0, r0); + NodeContainer net2 (h1, r0); + NodeContainer net3 (r0, r1); + NodeContainer net4 (r1, r2); + NodeContainer net5 (r2, r3); + NodeContainer net6 (r3, r0); + NodeContainer all; + all.Add (h0); + all.Add (h1); + all.Add (r0); + all.Add (r1); + all.Add (r2); + all.Add (r3); + + NS_LOG_INFO ("Create IPv6 Internet Stack"); + InternetStackHelper internetv6; + internetv6.Install (all); + + NS_LOG_INFO ("Create channels."); + CsmaHelper csma; + csma.SetDeviceAttribute ("Mtu", UintegerValue(1500)); + csma.SetChannelAttribute ("DataRate", DataRateValue(5000000)); + csma.SetChannelAttribute ("Delay", TimeValue(MilliSeconds (2))); + NetDeviceContainer d1 = csma.Install (net1); + NetDeviceContainer d2 = csma.Install (net2); + NetDeviceContainer d3 = csma.Install (net3); + NetDeviceContainer d4 = csma.Install (net4); + NetDeviceContainer d5 = csma.Install (net5); + NetDeviceContainer d6 = csma.Install (net6); + + NS_LOG_INFO ("Create networks and assign IPv6 Addresses."); + Ipv6AddressHelper ipv6; + + ipv6.NewNetwork (Ipv6Address ("2001:1::"), 64); + Ipv6InterfaceContainer i1 = ipv6.Assign (d1); + i1.SetRouter (1, true); + + ipv6.NewNetwork (Ipv6Address ("2001:2::"), 64); + Ipv6InterfaceContainer i2 = ipv6.Assign (d2); + i2.SetRouter (1, true); + + ipv6.NewNetwork (Ipv6Address ("2001:3::"), 64); + Ipv6InterfaceContainer i3 = ipv6.Assign (d3); + i3.SetRouter (0, true); + i3.SetRouter (1, true); + + ipv6.NewNetwork (Ipv6Address ("2001:4::"), 64); + Ipv6InterfaceContainer i4 = ipv6.Assign (d4); + i4.SetRouter (0, true); + i4.SetRouter (1, true); + + ipv6.NewNetwork (Ipv6Address ("2001:5::"), 64); + Ipv6InterfaceContainer i5 = ipv6.Assign (d5); + i5.SetRouter (0, true); + i5.SetRouter (1, true); + + ipv6.NewNetwork (Ipv6Address ("2001:6::"), 64); + Ipv6InterfaceContainer i6 = ipv6.Assign (d6); + i6.SetRouter (0, true); + i6.SetRouter (1, true); +#if 0 + /** + * Network Configuration : + * - h0 : client + * - rX : router + * - h1 : UDP server (port 7) + */ + NS_LOG_INFO ("Create Applications."); + UdpEcho6ServerHelper server (7); + + ApplicationContainer apps = server.Install (h1); + apps.Start (Seconds (1.0)); + apps.Stop (Seconds (15.0)); + + /** + * UDP Echo from h0 to h1 port 7 + */ + uint32_t packetSize = 1024; + uint32_t maxPacketCount = 1; + Time interPacketInterval = Seconds (1.); + + UdpEcho6ClientHelper client (i2.GetAddress (0), 7); + client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); + client.SetAttribute ("Interval", TimeValue(interPacketInterval)); + client.SetAttribute ("PacketSize", UintegerValue (packetSize)); + apps = client.Install (h0); + apps.Start (Seconds (1.0)); + apps.Stop (Seconds (10.0)); + + /** + * UDP Echo from h0 to h1 port 7 with loose routing + */ + std::vector routersAddress; + routersAddress.push_back (i3.GetAddress (1)); + routersAddress.push_back (i4.GetAddress (1)); + routersAddress.push_back (i5.GetAddress (1)); + routersAddress.push_back (i6.GetAddress (1)); + routersAddress.push_back (i2.GetAddress (0)); + + UdpEcho6ClientHelper client2 (i1.GetAddress (1), 7); + packetSize = 10000; + client2.SetLocal (i1.GetAddress (0)); + client2.SetLooseRouting (true); + client2.SetRoutersAddress (routersAddress); + client2.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); + client2.SetAttribute ("Interval", TimeValue(interPacketInterval)); + client2.SetAttribute ("PacketSize", UintegerValue (packetSize)); + apps = client2.Install (h0); + apps.Start (Seconds (2.0)); + apps.Stop (Seconds (10.0)); +#endif + + std::ofstream ascii; + ascii.open ("loose-routing-ipv6.tr"); + CsmaHelper::EnablePcapAll ("loose-routing-ipv6", true); + CsmaHelper::EnableAsciiAll (ascii); + + NS_LOG_INFO ("Run Simulation."); + Simulator::Run (); + Simulator::Destroy (); + NS_LOG_INFO ("Done."); +} + diff --git a/examples/ipv6/wscript b/examples/ipv6/wscript index aaa47d403..e7efcacd2 100644 --- a/examples/ipv6/wscript +++ b/examples/ipv6/wscript @@ -7,9 +7,6 @@ def build(bld): obj = bld.create_ns3_program('ping6', ['csma', 'internet-stack']) obj.source = 'ping6.cc' - obj = bld.create_ns3_program('fragmentation-ipv6', ['csma', 'internet-stack']) - obj.source = 'fragmentation-ipv6.cc' - obj = bld.create_ns3_program('radvd', ['csma', 'internet-stack']) obj.source = 'radvd.cc' @@ -18,3 +15,10 @@ def build(bld): obj = bld.create_ns3_program('test-ipv6', ['point-to-point', 'internet-stack']) obj.source = 'test-ipv6.cc' + + obj = bld.create_ns3_program('fragmentation-ipv6', ['csma', 'internet-stack']) + obj.source = 'fragmentation-ipv6.cc' + + obj = bld.create_ns3_program('loose-routing-ipv6', ['csma', 'internet-stack']) + obj.source = 'loose-routing-ipv6.cc' +