A Discrete-Event Network Simulator
API
simple-multicast-flooding.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Universita' di Firenze
4  * Copyright (c) 2019 Caliola Engineering, LLC : RFC 6621 multicast packet de-duplication
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
20  * Modified (2019): Jared Dulmage <jared.dulmage@caliola.com>
21  * Demonstrates dissemination of multicast packets across a mesh
22  * network to all nodes over multiple hops.
23  */
24 
25 #include "ns3/test.h"
26 #include "ns3/simulator.h"
27 #include "ns3/simple-channel.h"
28 #include "ns3/simple-net-device.h"
29 #include "ns3/socket.h"
30 #include "ns3/boolean.h"
31 #include "ns3/double.h"
32 #include "ns3/string.h"
33 #include "ns3/config.h"
34 #include "ns3/data-rate.h"
35 #include "ns3/uinteger.h"
36 
37 #include "ns3/names.h"
38 #include "ns3/log.h"
39 #include "ns3/node.h"
40 #include "ns3/inet-socket-address.h"
41 #include "ns3/random-variable-stream.h"
42 
43 #include "ns3/ipv4-l3-protocol.h"
44 #include "ns3/ipv4-static-routing.h"
45 #include "ns3/udp-socket-factory.h"
46 #include "ns3/udp-socket.h"
47 #include "ns3/packet-sink.h"
48 
49 #include "ns3/internet-stack-helper.h"
50 #include "ns3/ipv4-list-routing-helper.h"
51 #include "ns3/ipv4-static-routing-helper.h"
52 #include "ns3/ipv4-address-helper.h"
53 #include "ns3/simple-net-device-helper.h"
54 #include "ns3/packet-sink-helper.h"
55 #include "ns3/on-off-helper.h"
56 #include "ns3/trace-helper.h"
57 
58 #include "ns3/traffic-control-layer.h"
59 
60 #include <string>
61 #include <limits>
62 #include <functional>
63 
64 using namespace ns3;
65 
78 int
79 main (int argc, char *argv[])
80 {
81  // multicast target
82  const std::string targetAddr = "239.192.100.1";
83  Config::SetDefault ("ns3::Ipv4L3Protocol::EnableDuplicatePacketDetection", BooleanValue (true));
84  Config::SetDefault ("ns3::Ipv4L3Protocol::DuplicateExpire", TimeValue (Seconds (10)));
85 
86  // Create topology
87 
88  // Create nodes
89  auto nodes = NodeContainer ();
90  nodes.Create (5);
91 
92  // Name nodes
93  Names::Add ("A", nodes.Get (0));
94  Names::Add ("B", nodes.Get (1));
95  Names::Add ("C", nodes.Get (2));
96  Names::Add ("D", nodes.Get (3));
97  Names::Add ("E", nodes.Get (4));
98 
99  SimpleNetDeviceHelper simplenet;
100  auto devices = simplenet.Install (nodes);
101  // name devices
102  Names::Add ("A/dev", devices.Get (0));
103  Names::Add ("B/dev", devices.Get (1));
104  Names::Add ("C/dev", devices.Get (2));
105  Names::Add ("D/dev", devices.Get (3));
106  Names::Add ("E/dev", devices.Get (4));
107 
108  // setup static routes to facilitate multicast flood
109  Ipv4ListRoutingHelper listRouting;
110  Ipv4StaticRoutingHelper staticRouting;
111  listRouting.Add (staticRouting, 0);
112 
113  InternetStackHelper internet;
114  internet.SetIpv6StackInstall (false);
115  internet.SetIpv4ArpJitter (true);
116  internet.SetRoutingHelper (listRouting);
117  internet.Install (nodes);
118 
119  Ipv4AddressHelper ipv4address;
120  ipv4address.SetBase ("10.0.0.0", "255.255.255.0");
121  ipv4address.Assign (devices);
122 
123  // add static routes for each node / device
124  for (auto diter = devices.Begin (); diter != devices.End (); ++diter)
125  {
126  Ptr<Node> node = (*diter)->GetNode ();
127 
128  if (Names::FindName (node) == "A")
129  {
130  // route for host
131  // Use host routing entry according to note in Ipv4StaticRouting::RouteOutput:
138  auto ipv4 = node->GetObject <Ipv4> ();
139  NS_ASSERT_MSG ((bool) ipv4, "Node " << Names::FindName (node) << " does not have Ipv4 aggregate");
140  auto routing = staticRouting.GetStaticRouting (ipv4);
141  routing->AddHostRouteTo (targetAddr.c_str (), ipv4->GetInterfaceForDevice (*diter), 0);
142  }
143  else
144  {
145  // route for forwarding
146  staticRouting.AddMulticastRoute (node, Ipv4Address::GetAny (), targetAddr.c_str (), *diter, NetDeviceContainer (*diter));
147  }
148  }
149 
150  // set the topology, by default fully-connected
151  auto channel = devices.Get (0)->GetChannel ();
152  auto simplechannel = channel->GetObject <SimpleChannel> ();
153  simplechannel->BlackList (Names::Find <SimpleNetDevice> ("A/dev"), Names::Find <SimpleNetDevice> ("D/dev"));
154  simplechannel->BlackList (Names::Find <SimpleNetDevice> ("D/dev"), Names::Find <SimpleNetDevice> ("A/dev"));
155 
156  simplechannel->BlackList (Names::Find <SimpleNetDevice> ("A/dev"), Names::Find <SimpleNetDevice> ("E/dev"));
157  simplechannel->BlackList (Names::Find <SimpleNetDevice> ("E/dev"), Names::Find <SimpleNetDevice> ("A/dev"));
158 
159  simplechannel->BlackList (Names::Find <SimpleNetDevice> ("B/dev"), Names::Find <SimpleNetDevice> ("E/dev"));
160  simplechannel->BlackList (Names::Find <SimpleNetDevice> ("E/dev"), Names::Find <SimpleNetDevice> ("B/dev"));
161 
162  simplechannel->BlackList (Names::Find <SimpleNetDevice> ("C/dev"), Names::Find <SimpleNetDevice> ("E/dev"));
163  simplechannel->BlackList (Names::Find <SimpleNetDevice> ("E/dev"), Names::Find <SimpleNetDevice> ("C/dev"));
164  // ensure some time progress between re-transmissions
165  simplechannel->SetAttribute ("Delay", TimeValue (MilliSeconds (1)));
166 
167  // sinks
168  PacketSinkHelper sinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 9));
169  auto sinks = sinkHelper.Install ("B");
170  sinks.Add (sinkHelper.Install ("C"));
171  sinks.Add (sinkHelper.Install ("D"));
172  sinks.Add (sinkHelper.Install ("E"));
173  sinks.Start (Seconds (1));
174 
175  // source
176  OnOffHelper onoffHelper ("ns3::UdpSocketFactory", InetSocketAddress (targetAddr.c_str (), 9));
177  onoffHelper.SetAttribute ("DataRate", DataRateValue (DataRate ("8Mbps")));
178  onoffHelper.SetAttribute ("MaxBytes", UintegerValue (10 * 1024));
179  auto source = onoffHelper.Install ("A");
180  source.Start (Seconds (1.1));
181 
182  // pcap traces
183  for (auto end = nodes.End (),
184  iter = nodes.Begin (); iter != end; ++iter)
185  {
186  internet.EnablePcapIpv4 ("smf-trace", (*iter)->GetId (), 1, false);
187  }
188 
189  // run simulation
190  Simulator::Run ();
191 
192  std::cout << "Node A sent " << 10 * 1024 << " bytes" << std::endl;
193  for (auto end = sinks.End (),
194  iter = sinks.Begin (); iter != end; ++iter)
195  {
196  auto node = (*iter)->GetNode ();
197  auto sink = (*iter)->GetObject <PacketSink> ();
198  std::cout << "Node " << Names::FindName (node)
199  << " received " << sink->GetTotalRx () << " bytes" << std::endl;
200  }
201 
203 
204  Names::Clear ();
205  return 0;
206 }
207 
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetIpv4ArpJitter(bool enable)
Enable/disable IPv4 ARP Jitter.
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
void SetIpv6StackInstall(bool enable)
Enable/disable IPv6 stack install.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
static Ipv4Address GetAny(void)
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
Helper class that adds ns3::Ipv4ListRouting objects.
void Add(const Ipv4RoutingHelper &routing, int16_t priority)
Helper class that adds ns3::Ipv4StaticRouting objects.
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
void AddMulticastRoute(Ptr< Node > n, Ipv4Address source, Ipv4Address group, Ptr< NetDevice > input, NetDeviceContainer output)
Add a multicast route to a node and net device using explicit Ptr<Node> and Ptr<NetDevice>
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition: names.cc:768
static std::string FindName(Ptr< Object > object)
Given a pointer to an object, look to see if that object has a name associated with it and,...
Definition: names.cc:817
static void Clear(void)
Clear the list of objects associated with names.
Definition: names.cc:831
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Receive and consume traffic generated to an IP address and port.
Definition: packet-sink.h:72
uint64_t GetTotalRx() const
Definition: packet-sink.cc:93
void EnablePcapIpv4(std::string prefix, Ptr< Ipv4 > ipv4, uint32_t interface, bool explicitFilename=false)
Enable pcap output the indicated Ipv4 and interface pair.
A simple channel, for simple things and testing.
virtual void BlackList(Ptr< SimpleNetDevice > from, Ptr< SimpleNetDevice > to)
Blocks the communications from a NetDevice to another NetDevice.
build a set of SimpleNetDevice objects
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
AttributeValue implementation for Time.
Definition: nstime.h:1308
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
devices
Definition: first.py:39
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
channel
Definition: third.py:92
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56