View | Details | Raw Unified | Return to bug 735
Collapse All | Expand All

(-)bdaf03bd656d (+214 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2009 IITP RAS
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
7
 * published by the Free Software Foundation;
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 * This is an example script for OLSR manet routing protocol. 
19
 *
20
 * Authors: Pavel Boyko <boyko@iitp.ru>
21
 */
22
23
#include "ns3/olsr-module.h"
24
#include "ns3/core-module.h"
25
#include "ns3/common-module.h"
26
#include "ns3/node-module.h"
27
#include "ns3/helper-module.h"
28
#include "ns3/mobility-module.h"
29
#include "ns3/contrib-module.h"
30
#include "ns3/wifi-module.h" 
31
#include "ns3/v4ping-helper.h"
32
#include <iostream>
33
#include <cmath>
34
35
using namespace ns3;
36
37
/**
38
 * \brief Test script.
39
 * 
40
 * This script creates 1-dimensional grid topology and then ping last node from the first one:
41
 * 
42
 * [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.04]
43
 * 
44
 * ping 10.0.0.4
45
 */
46
class OlsrExample 
47
{
48
public:
49
  OlsrExample ();
50
  /// Configure script parameters, \return true on successful configuration
51
  bool Configure (int argc, char **argv);
52
  /// Run simulation
53
  void Run ();
54
  /// Report results
55
  void Report (std::ostream & os);
56
  
57
private:
58
  ///\name parameters
59
  //\{
60
  /// Number of nodes
61
  uint32_t size;
62
  /// Distance between nodes, meters
63
  double step;
64
  /// Simulation time, seconds
65
  double totalTime;
66
  /// Write per-device PCAP traces if true
67
  bool pcap;
68
  //\}
69
  
70
  ///\name network
71
  //\{
72
  NodeContainer nodes;
73
  NetDeviceContainer devices;
74
  Ipv4InterfaceContainer interfaces;
75
  //\}
76
  
77
private:
78
  void CreateNodes ();
79
  void CreateDevices ();
80
  void InstallInternetStack ();
81
  void InstallApplications ();
82
};
83
84
int main (int argc, char **argv)
85
{
86
  OlsrExample test;
87
  if (! test.Configure(argc, argv)) 
88
    NS_FATAL_ERROR ("Configuration failed. Aborted.");
89
  
90
  test.Run ();
91
  test.Report (std::cout);
92
  return 0;
93
}
94
95
//-----------------------------------------------------------------------------
96
OlsrExample::OlsrExample () :
97
  size (4),
98
  step (120),
99
  totalTime (60),
100
  pcap (true)
101
{
102
}
103
104
bool
105
OlsrExample::Configure (int argc, char **argv)
106
{
107
  // Enable AODV logs by default. Comment this if too noisy
108
  // LogComponentEnable("OlsrRoutingProtocol", LOG_LEVEL_ALL);
109
  
110
  SeedManager::SetSeed(12345);
111
  CommandLine cmd;
112
  
113
  cmd.AddValue ("pcap", "Write PCAP traces.", pcap);
114
  cmd.AddValue ("size", "Number of nodes.", size);
115
  cmd.AddValue ("time", "Simulation time, s.", totalTime);
116
  cmd.AddValue ("step", "Grid step, m", step);
117
  
118
  cmd.Parse (argc, argv);
119
  return true;
120
}
121
122
void
123
OlsrExample::Run ()
124
{
125
//  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time.
126
  CreateNodes ();
127
  CreateDevices ();
128
  InstallInternetStack ();
129
  InstallApplications ();
130
  
131
  std::cout << "Starting simulation for " << totalTime << " s ...\n";
132
  
133
  Simulator::Stop (Seconds (totalTime));
134
  Simulator::Run ();
135
  Simulator::Destroy ();
136
}
137
138
void
139
OlsrExample::Report (std::ostream &)
140
{ 
141
}
142
143
void
144
OlsrExample::CreateNodes ()
145
{
146
  std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
147
  nodes.Create (size);
148
  // Name nodes
149
  for (uint32_t i = 0; i < size; ++i)
150
     {
151
       std::ostringstream os;
152
       os << "node-" << i;
153
       Names::Add (os.str (), nodes.Get (i));
154
     }
155
  // Create static grid
156
  MobilityHelper mobility;
157
  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
158
                                "MinX", DoubleValue (0.0),
159
                                "MinY", DoubleValue (0.0),
160
                                "DeltaX", DoubleValue (step),
161
                                "DeltaY", DoubleValue (0),
162
                                "GridWidth", UintegerValue (size),
163
                                "LayoutType", StringValue ("RowFirst"));
164
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
165
  mobility.Install (nodes);
166
}
167
168
void
169
OlsrExample::CreateDevices ()
170
{
171
  NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
172
  wifiMac.SetType ("ns3::AdhocWifiMac");
173
  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
174
  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
175
  wifiPhy.SetChannel (wifiChannel.Create ());
176
  WifiHelper wifi = WifiHelper::Default ();
177
  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("wifia-6mbs"), "RtsCtsThreshold", UintegerValue (0));
178
  devices = wifi.Install (wifiPhy, wifiMac, nodes); 
179
  
180
  if (pcap)
181
    {
182
      wifiPhy.EnablePcapAll (std::string ("olsr"));
183
    }
184
}
185
186
void
187
OlsrExample::InstallInternetStack ()
188
{
189
  OlsrHelper olsr;
190
  // you can configure OLSR attributes here using olsr.Set(name, value)
191
  InternetStackHelper stack;
192
  stack.SetRoutingHelper (olsr);
193
  stack.Install (nodes);
194
  Ipv4AddressHelper address;
195
  address.SetBase ("10.0.0.0", "255.0.0.0");
196
  interfaces = address.Assign (devices);
197
}
198
199
void
200
OlsrExample::InstallApplications ()
201
{
202
  V4PingHelper ping (interfaces.GetAddress (size - 1));
203
  //ping.SetAttribute ("Verbose", BooleanValue (true)); Uncomment me after AODV merge
204
  
205
  ApplicationContainer p = ping.Install (nodes.Get (0));
206
  p.Start (Seconds (0));
207
  p.Stop (Seconds (totalTime));
208
209
  // move node away
210
  Ptr<Node> node = nodes.Get (size/2);
211
  Ptr<MobilityModel> mob = node->GetObject<MobilityModel> ();
212
  Simulator::Schedule (Seconds (totalTime/3), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5));
213
}
214
(-)a/examples/routing/wscript (+4 lines)
 Lines 48-50    Link Here 
48
    obj = bld.create_ns3_program('aodv',
48
    obj = bld.create_ns3_program('aodv',
49
                                 ['wifi', 'internet-stack', 'aodv'])
49
                                 ['wifi', 'internet-stack', 'aodv'])
50
    obj.source = 'aodv.cc'
50
    obj.source = 'aodv.cc'
51
    
52
    obj = bld.create_ns3_program('olsr',
53
                                 ['wifi', 'internet-stack', 'olsr'])
54
    obj.source = 'olsr.cc'
(-)a/src/routing/olsr/olsr-routing-protocol.cc (-4 / +42 lines)
 Lines 221-227    Link Here 
221
{
221
{
222
  m_ipv4 = 0;
222
  m_ipv4 = 0;
223
223
224
  for (std::map< Ptr<Socket>, Ipv4Address >::iterator iter = m_socketAddresses.begin ();
224
  for (std::map< Ptr<Socket>, Ipv4InterfaceAddress >::iterator iter = m_socketAddresses.begin ();
225
       iter != m_socketAddresses.end (); iter++)
225
       iter != m_socketAddresses.end (); iter++)
226
    {
226
    {
227
      iter->first->Close ();
227
      iter->first->Close ();
 Lines 280-286    Link Here 
280
          NS_FATAL_ERROR ("Failed to bind() OLSR receive socket");
280
          NS_FATAL_ERROR ("Failed to bind() OLSR receive socket");
281
        }
281
        }
282
      socket->Connect (InetSocketAddress (Ipv4Address (0xffffffff), OLSR_PORT_NUMBER));
282
      socket->Connect (InetSocketAddress (Ipv4Address (0xffffffff), OLSR_PORT_NUMBER));
283
      m_socketAddresses[socket] = addr;
283
      m_socketAddresses[socket] = m_ipv4->GetAddress (i, 0);
284
    }
284
    }
285
285
286
  HelloTimerExpire ();
286
  HelloTimerExpire ();
 Lines 307-313    Link Here 
307
307
308
  InetSocketAddress inetSourceAddr = InetSocketAddress::ConvertFrom (sourceAddress);
308
  InetSocketAddress inetSourceAddr = InetSocketAddress::ConvertFrom (sourceAddress);
309
  Ipv4Address senderIfaceAddr = inetSourceAddr.GetIpv4 ();
309
  Ipv4Address senderIfaceAddr = inetSourceAddr.GetIpv4 ();
310
  Ipv4Address receiverIfaceAddr = m_socketAddresses[socket];
310
  Ipv4Address receiverIfaceAddr = m_socketAddresses[socket].GetLocal ();
311
  NS_ASSERT (receiverIfaceAddr != Ipv4Address ());
311
  NS_ASSERT (receiverIfaceAddr != Ipv4Address ());
312
  NS_LOG_DEBUG ("OLSR node " << m_mainAddress << " received a OLSR packet from "
312
  NS_LOG_DEBUG ("OLSR node " << m_mainAddress << " received a OLSR packet from "
313
                << senderIfaceAddr << " to " << receiverIfaceAddr);
313
                << senderIfaceAddr << " to " << receiverIfaceAddr);
 Lines 2643-2653    Link Here 
2643
}
2643
}
2644
2644
2645
bool RoutingProtocol::RouteInput  (Ptr<const Packet> p, 
2645
bool RoutingProtocol::RouteInput  (Ptr<const Packet> p, 
2646
  const Ipv4Header &header, Ptr<const NetDevice> idev,                            UnicastForwardCallback ucb, MulticastForwardCallback mcb,             
2646
  const Ipv4Header &header, Ptr<const NetDevice> idev,                            
2647
  UnicastForwardCallback ucb, MulticastForwardCallback mcb,             
2647
  LocalDeliverCallback lcb, ErrorCallback ecb)
2648
  LocalDeliverCallback lcb, ErrorCallback ecb)
2648
{   
2649
{   
2649
  NS_LOG_FUNCTION (this << " " << m_ipv4->GetObject<Node> ()->GetId() << " " << header.GetDestination ());
2650
  NS_LOG_FUNCTION (this << " " << m_ipv4->GetObject<Node> ()->GetId() << " " << header.GetDestination ());
2650
  
2651
  
2652
  Ipv4Address dst = header.GetDestination ();
2653
  Ipv4Address origin = header.GetSource ();
2654
2655
  if (IsMyOwnAddress (origin)) 
2656
    return true; // ignore own packets
2657
  
2658
  // Local delivery
2659
  NS_ASSERT (m_ipv4->GetInterfaceForDevice (idev) >= 0);
2660
  int32_t iif = m_ipv4->GetInterfaceForDevice (idev);
2661
  for (std::map<Ptr<Socket> , Ipv4InterfaceAddress>::const_iterator j = m_socketAddresses.begin (); 
2662
      j != m_socketAddresses.end (); ++j)
2663
    {
2664
      Ipv4InterfaceAddress iface = j->second;
2665
      if (dst == iface.GetLocal () || dst == iface.GetBroadcast ())
2666
        {
2667
          NS_LOG_LOGIC ("Local delivery to " << iface.GetLocal () << ":" << iface.GetBroadcast ());
2668
          lcb (p, header, iif);
2669
          return true;
2670
        }
2671
    }
2672
  
2673
  // Forwarding
2651
  Ptr<Ipv4Route> rtentry;
2674
  Ptr<Ipv4Route> rtentry;
2652
  RoutingTableEntry entry1, entry2; 
2675
  RoutingTableEntry entry1, entry2; 
2653
  if (Lookup (header.GetDestination (), entry1))
2676
  if (Lookup (header.GetDestination (), entry1))
 Lines 2781-2786    Link Here 
2781
  return retval;
2804
  return retval;
2782
}
2805
}
2783
2806
2807
bool
2808
RoutingProtocol::IsMyOwnAddress (const Ipv4Address & a) const
2809
{
2810
  for (std::map<Ptr<Socket> , Ipv4InterfaceAddress>::const_iterator j =
2811
      m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j)
2812
    {
2813
      Ipv4InterfaceAddress iface = j->second;
2814
      if (a == iface.GetLocal ())
2815
        {
2816
          return true;
2817
        }
2818
    }
2819
  return false;
2820
}
2821
2784
2822
2785
}} // namespace olsr, ns3
2823
}} // namespace olsr, ns3
2786
2824
(-)a/src/routing/olsr/olsr-routing-protocol.h (-1 / +3 lines)
 Lines 216-228    Link Here 
216
                               const olsr::MessageHeader::Hello &hello);
216
                               const olsr::MessageHeader::Hello &hello);
217
217
218
  int Degree (NeighborTuple const &tuple);
218
  int Degree (NeighborTuple const &tuple);
219
  /// Check that address is one of my interfaces
220
  bool IsMyOwnAddress (const Ipv4Address & a) const;
219
221
220
  Ipv4Address m_mainAddress;
222
  Ipv4Address m_mainAddress;
221
223
222
  // One socket per interface, each bound to that interface's address
224
  // One socket per interface, each bound to that interface's address
223
  // (reason: for OLSR Link Sensing we need to know on which interface
225
  // (reason: for OLSR Link Sensing we need to know on which interface
224
  // HELLO messages arrive)
226
  // HELLO messages arrive)
225
  std::map< Ptr<Socket>, Ipv4Address > m_socketAddresses;
227
  std::map< Ptr<Socket>, Ipv4InterfaceAddress > m_socketAddresses;
226
228
227
  TracedCallback <const PacketHeader &,
229
  TracedCallback <const PacketHeader &,
228
                  const MessageList &> m_rxPacketTrace;
230
                  const MessageList &> m_rxPacketTrace;

Return to bug 735