diff -r bdaf03bd656d examples/routing/olsr.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/routing/olsr.cc Mon Nov 02 16:01:25 2009 +0300 @@ -0,0 +1,214 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 IITP RAS + * + * 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 + * + * This is an example script for OLSR manet routing protocol. + * + * Authors: Pavel Boyko + */ + +#include "ns3/olsr-module.h" +#include "ns3/core-module.h" +#include "ns3/common-module.h" +#include "ns3/node-module.h" +#include "ns3/helper-module.h" +#include "ns3/mobility-module.h" +#include "ns3/contrib-module.h" +#include "ns3/wifi-module.h" +#include "ns3/v4ping-helper.h" +#include +#include + +using namespace ns3; + +/** + * \brief Test script. + * + * This script creates 1-dimensional grid topology and then ping last node from the first one: + * + * [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.04] + * + * ping 10.0.0.4 + */ +class OlsrExample +{ +public: + OlsrExample (); + /// Configure script parameters, \return true on successful configuration + bool Configure (int argc, char **argv); + /// Run simulation + void Run (); + /// Report results + void Report (std::ostream & os); + +private: + ///\name parameters + //\{ + /// Number of nodes + uint32_t size; + /// Distance between nodes, meters + double step; + /// Simulation time, seconds + double totalTime; + /// Write per-device PCAP traces if true + bool pcap; + //\} + + ///\name network + //\{ + NodeContainer nodes; + NetDeviceContainer devices; + Ipv4InterfaceContainer interfaces; + //\} + +private: + void CreateNodes (); + void CreateDevices (); + void InstallInternetStack (); + void InstallApplications (); +}; + +int main (int argc, char **argv) +{ + OlsrExample test; + if (! test.Configure(argc, argv)) + NS_FATAL_ERROR ("Configuration failed. Aborted."); + + test.Run (); + test.Report (std::cout); + return 0; +} + +//----------------------------------------------------------------------------- +OlsrExample::OlsrExample () : + size (4), + step (120), + totalTime (60), + pcap (true) +{ +} + +bool +OlsrExample::Configure (int argc, char **argv) +{ + // Enable AODV logs by default. Comment this if too noisy + // LogComponentEnable("OlsrRoutingProtocol", LOG_LEVEL_ALL); + + SeedManager::SetSeed(12345); + CommandLine cmd; + + cmd.AddValue ("pcap", "Write PCAP traces.", pcap); + cmd.AddValue ("size", "Number of nodes.", size); + cmd.AddValue ("time", "Simulation time, s.", totalTime); + cmd.AddValue ("step", "Grid step, m", step); + + cmd.Parse (argc, argv); + return true; +} + +void +OlsrExample::Run () +{ +// Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. + CreateNodes (); + CreateDevices (); + InstallInternetStack (); + InstallApplications (); + + std::cout << "Starting simulation for " << totalTime << " s ...\n"; + + Simulator::Stop (Seconds (totalTime)); + Simulator::Run (); + Simulator::Destroy (); +} + +void +OlsrExample::Report (std::ostream &) +{ +} + +void +OlsrExample::CreateNodes () +{ + std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n"; + nodes.Create (size); + // Name nodes + for (uint32_t i = 0; i < size; ++i) + { + std::ostringstream os; + os << "node-" << i; + Names::Add (os.str (), nodes.Get (i)); + } + // Create static grid + MobilityHelper mobility; + mobility.SetPositionAllocator ("ns3::GridPositionAllocator", + "MinX", DoubleValue (0.0), + "MinY", DoubleValue (0.0), + "DeltaX", DoubleValue (step), + "DeltaY", DoubleValue (0), + "GridWidth", UintegerValue (size), + "LayoutType", StringValue ("RowFirst")); + mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); + mobility.Install (nodes); +} + +void +OlsrExample::CreateDevices () +{ + NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); + wifiMac.SetType ("ns3::AdhocWifiMac"); + YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); + YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); + wifiPhy.SetChannel (wifiChannel.Create ()); + WifiHelper wifi = WifiHelper::Default (); + wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("wifia-6mbs"), "RtsCtsThreshold", UintegerValue (0)); + devices = wifi.Install (wifiPhy, wifiMac, nodes); + + if (pcap) + { + wifiPhy.EnablePcapAll (std::string ("olsr")); + } +} + +void +OlsrExample::InstallInternetStack () +{ + OlsrHelper olsr; + // you can configure OLSR attributes here using olsr.Set(name, value) + InternetStackHelper stack; + stack.SetRoutingHelper (olsr); + stack.Install (nodes); + Ipv4AddressHelper address; + address.SetBase ("10.0.0.0", "255.0.0.0"); + interfaces = address.Assign (devices); +} + +void +OlsrExample::InstallApplications () +{ + V4PingHelper ping (interfaces.GetAddress (size - 1)); + //ping.SetAttribute ("Verbose", BooleanValue (true)); Uncomment me after AODV merge + + ApplicationContainer p = ping.Install (nodes.Get (0)); + p.Start (Seconds (0)); + p.Stop (Seconds (totalTime)); + + // move node away + Ptr node = nodes.Get (size/2); + Ptr mob = node->GetObject (); + Simulator::Schedule (Seconds (totalTime/3), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5)); +} + diff -r bdaf03bd656d examples/routing/wscript --- a/examples/routing/wscript Mon Nov 02 15:55:56 2009 +0300 +++ b/examples/routing/wscript Mon Nov 02 16:01:25 2009 +0300 @@ -48,3 +48,7 @@ obj = bld.create_ns3_program('aodv', ['wifi', 'internet-stack', 'aodv']) obj.source = 'aodv.cc' + + obj = bld.create_ns3_program('olsr', + ['wifi', 'internet-stack', 'olsr']) + obj.source = 'olsr.cc' diff -r bdaf03bd656d src/routing/olsr/olsr-routing-protocol.cc --- a/src/routing/olsr/olsr-routing-protocol.cc Mon Nov 02 15:55:56 2009 +0300 +++ b/src/routing/olsr/olsr-routing-protocol.cc Mon Nov 02 16:01:25 2009 +0300 @@ -221,7 +221,7 @@ { m_ipv4 = 0; - for (std::map< Ptr, Ipv4Address >::iterator iter = m_socketAddresses.begin (); + for (std::map< Ptr, Ipv4InterfaceAddress >::iterator iter = m_socketAddresses.begin (); iter != m_socketAddresses.end (); iter++) { iter->first->Close (); @@ -280,7 +280,7 @@ NS_FATAL_ERROR ("Failed to bind() OLSR receive socket"); } socket->Connect (InetSocketAddress (Ipv4Address (0xffffffff), OLSR_PORT_NUMBER)); - m_socketAddresses[socket] = addr; + m_socketAddresses[socket] = m_ipv4->GetAddress (i, 0); } HelloTimerExpire (); @@ -307,7 +307,7 @@ InetSocketAddress inetSourceAddr = InetSocketAddress::ConvertFrom (sourceAddress); Ipv4Address senderIfaceAddr = inetSourceAddr.GetIpv4 (); - Ipv4Address receiverIfaceAddr = m_socketAddresses[socket]; + Ipv4Address receiverIfaceAddr = m_socketAddresses[socket].GetLocal (); NS_ASSERT (receiverIfaceAddr != Ipv4Address ()); NS_LOG_DEBUG ("OLSR node " << m_mainAddress << " received a OLSR packet from " << senderIfaceAddr << " to " << receiverIfaceAddr); @@ -2643,11 +2643,34 @@ } bool RoutingProtocol::RouteInput (Ptr p, - const Ipv4Header &header, Ptr idev, UnicastForwardCallback ucb, MulticastForwardCallback mcb, + const Ipv4Header &header, Ptr idev, + UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb) { NS_LOG_FUNCTION (this << " " << m_ipv4->GetObject ()->GetId() << " " << header.GetDestination ()); + Ipv4Address dst = header.GetDestination (); + Ipv4Address origin = header.GetSource (); + + if (IsMyOwnAddress (origin)) + return true; // ignore own packets + + // Local delivery + NS_ASSERT (m_ipv4->GetInterfaceForDevice (idev) >= 0); + int32_t iif = m_ipv4->GetInterfaceForDevice (idev); + for (std::map , Ipv4InterfaceAddress>::const_iterator j = m_socketAddresses.begin (); + j != m_socketAddresses.end (); ++j) + { + Ipv4InterfaceAddress iface = j->second; + if (dst == iface.GetLocal () || dst == iface.GetBroadcast ()) + { + NS_LOG_LOGIC ("Local delivery to " << iface.GetLocal () << ":" << iface.GetBroadcast ()); + lcb (p, header, iif); + return true; + } + } + + // Forwarding Ptr rtentry; RoutingTableEntry entry1, entry2; if (Lookup (header.GetDestination (), entry1)) @@ -2781,6 +2804,21 @@ return retval; } +bool +RoutingProtocol::IsMyOwnAddress (const Ipv4Address & a) const +{ + for (std::map , Ipv4InterfaceAddress>::const_iterator j = + m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) + { + Ipv4InterfaceAddress iface = j->second; + if (a == iface.GetLocal ()) + { + return true; + } + } + return false; +} + }} // namespace olsr, ns3 diff -r bdaf03bd656d src/routing/olsr/olsr-routing-protocol.h --- a/src/routing/olsr/olsr-routing-protocol.h Mon Nov 02 15:55:56 2009 +0300 +++ b/src/routing/olsr/olsr-routing-protocol.h Mon Nov 02 16:01:25 2009 +0300 @@ -216,13 +216,15 @@ const olsr::MessageHeader::Hello &hello); int Degree (NeighborTuple const &tuple); + /// Check that address is one of my interfaces + bool IsMyOwnAddress (const Ipv4Address & a) const; Ipv4Address m_mainAddress; // One socket per interface, each bound to that interface's address // (reason: for OLSR Link Sensing we need to know on which interface // HELLO messages arrive) - std::map< Ptr, Ipv4Address > m_socketAddresses; + std::map< Ptr, Ipv4InterfaceAddress > m_socketAddresses; TracedCallback m_rxPacketTrace;