A Discrete-Event Network Simulator
API
csma-bridge.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 // Network topology
18 //
19 // n0 n1
20 // | |
21 // ----------
22 // | Switch |
23 // ----------
24 // | |
25 // n2 n3
26 //
27 //
28 // - CBR/UDP flows from n0 to n1 and from n3 to n0
29 // - DropTail queues
30 // - Tracing of queues and packet receptions to file "csma-bridge.tr"
31 
32 #include <iostream>
33 #include <fstream>
34 
35 #include "ns3/core-module.h"
36 #include "ns3/network-module.h"
37 #include "ns3/applications-module.h"
38 #include "ns3/bridge-module.h"
39 #include "ns3/csma-module.h"
40 #include "ns3/internet-module.h"
41 
42 using namespace ns3;
43 
44 NS_LOG_COMPONENT_DEFINE ("CsmaBridgeExample");
45 
46 int
47 main (int argc, char *argv[])
48 {
49  //
50  // Users may find it convenient to turn on explicit debugging
51  // for selected modules; the below lines suggest how to do this
52  //
53 #if 0
54  LogComponentEnable ("CsmaBridgeExample", LOG_LEVEL_INFO);
55 #endif
56 
57  //
58  // Allow the user to override any of the defaults and the above Bind() at
59  // run-time, via command-line arguments
60  //
62  cmd.Parse (argc, argv);
63 
64  //
65  // Explicitly create the nodes required by the topology (shown above).
66  //
67  NS_LOG_INFO ("Create nodes.");
68  NodeContainer terminals;
69  terminals.Create (4);
70 
71  NodeContainer csmaSwitch;
72  csmaSwitch.Create (1);
73 
74  NS_LOG_INFO ("Build Topology");
76  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
77  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
78 
79  // Create the csma links, from each terminal to the switch
80 
81  NetDeviceContainer terminalDevices;
82  NetDeviceContainer switchDevices;
83 
84  for (int i = 0; i < 4; i++)
85  {
86  NetDeviceContainer link = csma.Install (NodeContainer (terminals.Get (i), csmaSwitch));
87  terminalDevices.Add (link.Get (0));
88  switchDevices.Add (link.Get (1));
89  }
90 
91  // Create the bridge netdevice, which will do the packet switching
92  Ptr<Node> switchNode = csmaSwitch.Get (0);
93  BridgeHelper bridge;
94  bridge.Install (switchNode, switchDevices);
95 
96  // Add internet stack to the terminals
97  InternetStackHelper internet;
98  internet.Install (terminals);
99 
100  // We've got the "hardware" in place. Now we need to add IP addresses.
101  //
102  NS_LOG_INFO ("Assign IP Addresses.");
103  Ipv4AddressHelper ipv4;
104  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
105  ipv4.Assign (terminalDevices);
106 
107  //
108  // Create an OnOff application to send UDP datagrams from node zero to node 1.
109  //
110  NS_LOG_INFO ("Create Applications.");
111  uint16_t port = 9; // Discard port (RFC 863)
112 
113  OnOffHelper onoff ("ns3::UdpSocketFactory",
114  Address (InetSocketAddress (Ipv4Address ("10.1.1.2"), port)));
115  onoff.SetConstantRate (DataRate ("500kb/s"));
116 
117  ApplicationContainer app = onoff.Install (terminals.Get (0));
118  // Start the application
119  app.Start (Seconds (1.0));
120  app.Stop (Seconds (10.0));
121 
122  // Create an optional packet sink to receive these packets
123  PacketSinkHelper sink ("ns3::UdpSocketFactory",
125  app = sink.Install (terminals.Get (1));
126  app.Start (Seconds (0.0));
127 
128  //
129  // Create a similar flow from n3 to n0, starting at time 1.1 seconds
130  //
131  onoff.SetAttribute ("Remote",
132  AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.1"), port)));
133  app = onoff.Install (terminals.Get (3));
134  app.Start (Seconds (1.1));
135  app.Stop (Seconds (10.0));
136 
137  app = sink.Install (terminals.Get (0));
138  app.Start (Seconds (0.0));
139 
140  NS_LOG_INFO ("Configure Tracing.");
141 
142  //
143  // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
144  // Trace output will be sent to the file "csma-bridge.tr"
145  //
146  AsciiTraceHelper ascii;
147  csma.EnableAsciiAll (ascii.CreateFileStream ("csma-bridge.tr"));
148 
149  //
150  // Also configure some tcpdump traces; each interface will be traced.
151  // The output files will be named:
152  // csma-bridge-<nodeId>-<interfaceId>.pcap
153  // and can be read by the "tcpdump -r" command (use "-tt" option to
154  // display timestamps correctly)
155  //
156  csma.EnablePcapAll ("csma-bridge", false);
157 
158  //
159  // Now, do the actual simulation.
160  //
161  NS_LOG_INFO ("Run Simulation.");
162  Simulator::Run ();
164  NS_LOG_INFO ("Done.");
165 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:47
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
static Ipv4Address GetAny(void)
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
aggregate IP/TCP/UDP functionality to existing Nodes.
LOG_INFO and above.
Definition: log.h:103
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits. ...
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:351
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
AttributeValue implementation for Time.
Definition: nstime.h:957
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
holds a vector of ns3::NetDevice pointers
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging)
Definition: bridge-helper.h:37
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
AttributeValue implementation for Address.
Definition: address.h:278
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
tuple csma
Definition: second.py:63