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 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
8 * Modified (2019): Jared Dulmage <jared.dulmage@caliola.com>
9 * Demonstrates dissemination of multicast packets across a mesh
10 * network to all nodes over multiple hops.
11 */
12
13#include "ns3/boolean.h"
14#include "ns3/config.h"
15#include "ns3/data-rate.h"
16#include "ns3/double.h"
17#include "ns3/inet-socket-address.h"
18#include "ns3/internet-stack-helper.h"
19#include "ns3/ipv4-address-helper.h"
20#include "ns3/ipv4-l3-protocol.h"
21#include "ns3/ipv4-list-routing-helper.h"
22#include "ns3/ipv4-static-routing-helper.h"
23#include "ns3/ipv4-static-routing.h"
24#include "ns3/log.h"
25#include "ns3/names.h"
26#include "ns3/node.h"
27#include "ns3/on-off-helper.h"
28#include "ns3/packet-sink-helper.h"
29#include "ns3/packet-sink.h"
30#include "ns3/random-variable-stream.h"
31#include "ns3/simple-channel.h"
32#include "ns3/simple-net-device-helper.h"
33#include "ns3/simple-net-device.h"
34#include "ns3/simulator.h"
35#include "ns3/socket.h"
36#include "ns3/string.h"
37#include "ns3/test.h"
38#include "ns3/trace-helper.h"
39#include "ns3/traffic-control-layer.h"
40#include "ns3/udp-socket-factory.h"
41#include "ns3/udp-socket.h"
42#include "ns3/uinteger.h"
43
44#include <functional>
45#include <limits>
46#include <string>
47
48using namespace ns3;
49
50/**
51 * Network topology:
52 *
53 * /---- B ----\
54 * A ---- | ---- D ---- E
55 * \---- C ----/
56 *
57 * This example demonstrates configuration of
58 * static routing to realize broadcast-like
59 * flooding of packets from node A
60 * across the illustrated topology.
61 */
62int
63main(int argc, char* argv[])
64{
65 // multicast target
66 const std::string targetAddr = "239.192.100.1";
67 Config::SetDefault("ns3::Ipv4L3Protocol::EnableDuplicatePacketDetection", BooleanValue(true));
68 Config::SetDefault("ns3::Ipv4L3Protocol::DuplicateExpire", TimeValue(Seconds(10)));
69
70 // Create topology
71
72 // Create nodes
73 auto nodes = NodeContainer();
74 nodes.Create(5);
75
76 // Name nodes
77 Names::Add("A", nodes.Get(0));
78 Names::Add("B", nodes.Get(1));
79 Names::Add("C", nodes.Get(2));
80 Names::Add("D", nodes.Get(3));
81 Names::Add("E", nodes.Get(4));
82
83 SimpleNetDeviceHelper simplenet;
84 auto devices = simplenet.Install(nodes);
85 // name devices
86 Names::Add("A/dev", devices.Get(0));
87 Names::Add("B/dev", devices.Get(1));
88 Names::Add("C/dev", devices.Get(2));
89 Names::Add("D/dev", devices.Get(3));
90 Names::Add("E/dev", devices.Get(4));
91
92 // setup static routes to facilitate multicast flood
93 Ipv4ListRoutingHelper listRouting;
94 Ipv4StaticRoutingHelper staticRouting;
95 listRouting.Add(staticRouting, 0);
96
98 internet.SetIpv6StackInstall(false);
99 internet.SetIpv4ArpJitter(true);
100 internet.SetRoutingHelper(listRouting);
101 internet.Install(nodes);
102
103 Ipv4AddressHelper ipv4address;
104 ipv4address.SetBase("10.0.0.0", "255.255.255.0");
105 ipv4address.Assign(devices);
106
107 // add static routes for each node / device
108 for (auto diter = devices.Begin(); diter != devices.End(); ++diter)
109 {
110 Ptr<Node> node = (*diter)->GetNode();
111
112 if (Names::FindName(node) == "A")
113 {
114 // route for host
115 // Use host routing entry according to note in Ipv4StaticRouting::RouteOutput:
116 //// Note: Multicast routes for outbound packets are stored in the
117 //// normal unicast table. An implication of this is that it is not
118 //// possible to source multicast datagrams on multiple interfaces.
119 //// This is a well-known property of sockets implementation on
120 //// many Unix variants.
121 //// So, we just log it and fall through to LookupStatic ()
122 auto ipv4 = node->GetObject<Ipv4>();
123 NS_ASSERT_MSG((bool)ipv4,
124 "Node " << Names::FindName(node) << " does not have Ipv4 aggregate");
125 auto routing = staticRouting.GetStaticRouting(ipv4);
126 routing->AddHostRouteTo(targetAddr.c_str(), ipv4->GetInterfaceForDevice(*diter), 0);
127 }
128 else
129 {
130 // route for forwarding
131 staticRouting.AddMulticastRoute(node,
133 targetAddr.c_str(),
134 *diter,
135 NetDeviceContainer(*diter));
136 }
137 }
138
139 // set the topology, by default fully-connected
140 auto channel = devices.Get(0)->GetChannel();
141 auto simplechannel = channel->GetObject<SimpleChannel>();
142 simplechannel->BlackList(Names::Find<SimpleNetDevice>("A/dev"),
144 simplechannel->BlackList(Names::Find<SimpleNetDevice>("D/dev"),
146
147 simplechannel->BlackList(Names::Find<SimpleNetDevice>("A/dev"),
149 simplechannel->BlackList(Names::Find<SimpleNetDevice>("E/dev"),
151
152 simplechannel->BlackList(Names::Find<SimpleNetDevice>("B/dev"),
154 simplechannel->BlackList(Names::Find<SimpleNetDevice>("E/dev"),
156
157 simplechannel->BlackList(Names::Find<SimpleNetDevice>("C/dev"),
159 simplechannel->BlackList(Names::Find<SimpleNetDevice>("E/dev"),
161 // ensure some time progress between re-transmissions
162 simplechannel->SetAttribute("Delay", TimeValue(MilliSeconds(1)));
163
164 // sinks
165 PacketSinkHelper sinkHelper("ns3::UdpSocketFactory",
167 auto sinks = sinkHelper.Install("B");
168 sinks.Add(sinkHelper.Install("C"));
169 sinks.Add(sinkHelper.Install("D"));
170 sinks.Add(sinkHelper.Install("E"));
171 sinks.Start(Seconds(1));
172
173 // source
174 OnOffHelper onoffHelper("ns3::UdpSocketFactory", InetSocketAddress(targetAddr.c_str(), 9));
175 onoffHelper.SetAttribute("DataRate", DataRateValue(DataRate("8Mbps")));
176 onoffHelper.SetAttribute("MaxBytes", UintegerValue(10 * 1024));
177 auto source = onoffHelper.Install("A");
178 source.Start(Seconds(1.1));
179
180 // pcap traces
181 for (auto end = nodes.End(), iter = nodes.Begin(); iter != end; ++iter)
182 {
183 internet.EnablePcapIpv4("smf-trace", (*iter)->GetId(), 1, false);
184 }
185
186 // run simulation
188
189 std::cout << "Node A sent " << 10 * 1024 << " bytes" << std::endl;
190 for (auto end = sinks.End(), iter = sinks.Begin(); iter != end; ++iter)
191 {
192 auto node = (*iter)->GetNode();
193 auto sink = (*iter)->GetObject<PacketSink>();
194 std::cout << "Node " << Names::FindName(node) << " received " << sink->GetTotalRx()
195 << " bytes" << std::endl;
196 }
197
199
200 Names::Clear();
201 return 0;
202}
Class for representing data rates.
Definition data-rate.h:78
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:69
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:764
static Ptr< T > Find(std::string path)
Given a name path string, look to see if there's an object in the system with that associated to it.
Definition names.h:443
static void Clear()
Clear the list of objects associated with names.
Definition names.cc:832
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:818
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
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:64
Smart pointer class similar to boost::intrusive_ptr.
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:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold an unsigned integer type.
Definition uinteger.h:34
#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:75
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
NodeContainer nodes
devices
Definition first.py:31
Every class exported by the ns3 library is enclosed in the ns3 namespace.
channel
Definition third.py:77
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44