A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-multicast-flooding.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 Universita' di Firenze
3 * Copyright (c) 2019 Caliola Engineering, LLC : RFC 6621 multicast packet de-duplication
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 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19 * Modified (2019): Jared Dulmage <jared.dulmage@caliola.com>
20 * Demonstrates dissemination of multicast packets across a mesh
21 * network to all nodes over multiple hops.
22 */
23
24#include "ns3/boolean.h"
25#include "ns3/config.h"
26#include "ns3/data-rate.h"
27#include "ns3/double.h"
28#include "ns3/inet-socket-address.h"
29#include "ns3/internet-stack-helper.h"
30#include "ns3/ipv4-address-helper.h"
31#include "ns3/ipv4-l3-protocol.h"
32#include "ns3/ipv4-list-routing-helper.h"
33#include "ns3/ipv4-static-routing-helper.h"
34#include "ns3/ipv4-static-routing.h"
35#include "ns3/log.h"
36#include "ns3/names.h"
37#include "ns3/node.h"
38#include "ns3/on-off-helper.h"
39#include "ns3/packet-sink-helper.h"
40#include "ns3/packet-sink.h"
41#include "ns3/random-variable-stream.h"
42#include "ns3/simple-channel.h"
43#include "ns3/simple-net-device-helper.h"
44#include "ns3/simple-net-device.h"
45#include "ns3/simulator.h"
46#include "ns3/socket.h"
47#include "ns3/string.h"
48#include "ns3/test.h"
49#include "ns3/trace-helper.h"
50#include "ns3/traffic-control-layer.h"
51#include "ns3/udp-socket-factory.h"
52#include "ns3/udp-socket.h"
53#include "ns3/uinteger.h"
54
55#include <functional>
56#include <limits>
57#include <string>
58
59using namespace ns3;
60
61/**
62 * Network topology:
63 *
64 * /---- B ----\
65 * A ---- | ---- D ---- E
66 * \---- C ----/
67 *
68 * This example demonstrates configuration of
69 * static routing to realize broadcast-like
70 * flooding of packets from node A
71 * across the illustrated topology.
72 */
73int
74main(int argc, char* argv[])
75{
76 // multicast target
77 const std::string targetAddr = "239.192.100.1";
78 Config::SetDefault("ns3::Ipv4L3Protocol::EnableDuplicatePacketDetection", BooleanValue(true));
79 Config::SetDefault("ns3::Ipv4L3Protocol::DuplicateExpire", TimeValue(Seconds(10)));
80
81 // Create topology
82
83 // Create nodes
84 auto nodes = NodeContainer();
85 nodes.Create(5);
86
87 // Name nodes
88 Names::Add("A", nodes.Get(0));
89 Names::Add("B", nodes.Get(1));
90 Names::Add("C", nodes.Get(2));
91 Names::Add("D", nodes.Get(3));
92 Names::Add("E", nodes.Get(4));
93
94 SimpleNetDeviceHelper simplenet;
95 auto devices = simplenet.Install(nodes);
96 // name devices
97 Names::Add("A/dev", devices.Get(0));
98 Names::Add("B/dev", devices.Get(1));
99 Names::Add("C/dev", devices.Get(2));
100 Names::Add("D/dev", devices.Get(3));
101 Names::Add("E/dev", devices.Get(4));
102
103 // setup static routes to facilitate multicast flood
104 Ipv4ListRoutingHelper listRouting;
105 Ipv4StaticRoutingHelper staticRouting;
106 listRouting.Add(staticRouting, 0);
107
109 internet.SetIpv6StackInstall(false);
110 internet.SetIpv4ArpJitter(true);
111 internet.SetRoutingHelper(listRouting);
112 internet.Install(nodes);
113
114 Ipv4AddressHelper ipv4address;
115 ipv4address.SetBase("10.0.0.0", "255.255.255.0");
116 ipv4address.Assign(devices);
117
118 // add static routes for each node / device
119 for (auto diter = devices.Begin(); diter != devices.End(); ++diter)
120 {
121 Ptr<Node> node = (*diter)->GetNode();
122
123 if (Names::FindName(node) == "A")
124 {
125 // route for host
126 // Use host routing entry according to note in Ipv4StaticRouting::RouteOutput:
127 //// Note: Multicast routes for outbound packets are stored in the
128 //// normal unicast table. An implication of this is that it is not
129 //// possible to source multicast datagrams on multiple interfaces.
130 //// This is a well-known property of sockets implementation on
131 //// many Unix variants.
132 //// So, we just log it and fall through to LookupStatic ()
133 auto ipv4 = node->GetObject<Ipv4>();
134 NS_ASSERT_MSG((bool)ipv4,
135 "Node " << Names::FindName(node) << " does not have Ipv4 aggregate");
136 auto routing = staticRouting.GetStaticRouting(ipv4);
137 routing->AddHostRouteTo(targetAddr.c_str(), ipv4->GetInterfaceForDevice(*diter), 0);
138 }
139 else
140 {
141 // route for forwarding
142 staticRouting.AddMulticastRoute(node,
144 targetAddr.c_str(),
145 *diter,
146 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"),
154 Names::Find<SimpleNetDevice>("D/dev"));
155 simplechannel->BlackList(Names::Find<SimpleNetDevice>("D/dev"),
156 Names::Find<SimpleNetDevice>("A/dev"));
157
158 simplechannel->BlackList(Names::Find<SimpleNetDevice>("A/dev"),
159 Names::Find<SimpleNetDevice>("E/dev"));
160 simplechannel->BlackList(Names::Find<SimpleNetDevice>("E/dev"),
161 Names::Find<SimpleNetDevice>("A/dev"));
162
163 simplechannel->BlackList(Names::Find<SimpleNetDevice>("B/dev"),
164 Names::Find<SimpleNetDevice>("E/dev"));
165 simplechannel->BlackList(Names::Find<SimpleNetDevice>("E/dev"),
166 Names::Find<SimpleNetDevice>("B/dev"));
167
168 simplechannel->BlackList(Names::Find<SimpleNetDevice>("C/dev"),
169 Names::Find<SimpleNetDevice>("E/dev"));
170 simplechannel->BlackList(Names::Find<SimpleNetDevice>("E/dev"),
171 Names::Find<SimpleNetDevice>("C/dev"));
172 // ensure some time progress between re-transmissions
173 simplechannel->SetAttribute("Delay", TimeValue(MilliSeconds(1)));
174
175 // sinks
176 PacketSinkHelper sinkHelper("ns3::UdpSocketFactory",
178 auto sinks = sinkHelper.Install("B");
179 sinks.Add(sinkHelper.Install("C"));
180 sinks.Add(sinkHelper.Install("D"));
181 sinks.Add(sinkHelper.Install("E"));
182 sinks.Start(Seconds(1));
183
184 // source
185 OnOffHelper onoffHelper("ns3::UdpSocketFactory", InetSocketAddress(targetAddr.c_str(), 9));
186 onoffHelper.SetAttribute("DataRate", DataRateValue(DataRate("8Mbps")));
187 onoffHelper.SetAttribute("MaxBytes", UintegerValue(10 * 1024));
188 auto source = onoffHelper.Install("A");
189 source.Start(Seconds(1.1));
190
191 // pcap traces
192 for (auto end = nodes.End(), iter = nodes.Begin(); iter != end; ++iter)
193 {
194 internet.EnablePcapIpv4("smf-trace", (*iter)->GetId(), 1, false);
195 }
196
197 // run simulation
199
200 std::cout << "Node A sent " << 10 * 1024 << " bytes" << std::endl;
201 for (auto end = sinks.End(), iter = sinks.Begin(); iter != end; ++iter)
202 {
203 auto node = (*iter)->GetNode();
204 auto sink = (*iter)->GetObject<PacketSink>();
205 std::cout << "Node " << Names::FindName(node) << " received " << sink->GetTotalRx()
206 << " bytes" << std::endl;
207 }
208
210
211 Names::Clear();
212 return 0;
213}
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:296
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
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()
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
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:775
static void Clear()
Clear the list of objects associated with names.
Definition: names.cc:843
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:829
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:522
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:37
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:75
uint64_t GetTotalRx() const
Definition: packet-sink.cc:96
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
AttributeValue implementation for Time.
Definition: nstime.h:1406
Hold an unsigned integer type.
Definition: uinteger.h:45
#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:86
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
NodeContainer nodes
ns devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns channel
Definition: third.py:88
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55