A Discrete-Event Network Simulator
API
tap-csma.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 // Packets sent to the device "thetap" on the Linux host will be sent to the
20 // tap bridge on node zero and then emitted onto the ns-3 simulated CSMA
21 // network. ARP will be used on the CSMA network to resolve MAC addresses.
22 // Packets destined for the CSMA device on node zero will be sent to the
23 // device "thetap" on the linux Host.
24 //
25 // +----------+
26 // | external |
27 // | Linux |
28 // | Host |
29 // | |
30 // | "thetap" |
31 // +----------+
32 // | n0 n1 n2 n3
33 // | +--------+ +--------+ +--------+ +--------+
34 // +-------| tap | | | | | | |
35 // | bridge | | | | | | |
36 // +--------+ +--------+ +--------+ +--------+
37 // | CSMA | | CSMA | | CSMA | | CSMA |
38 // +--------+ +--------+ +--------+ +--------+
39 // | | | |
40 // | | | |
41 // | | | |
42 // ===========================================
43 // CSMA LAN 10.1.1
44 //
45 // The CSMA device on node zero is: 10.1.1.1
46 // The CSMA device on node one is: 10.1.1.2
47 // The CSMA device on node two is: 10.1.1.3
48 // The CSMA device on node three is: 10.1.1.4
49 //
50 // Some simple things to do:
51 //
52 // 1) Ping one of the simulated nodes
53 //
54 // ./waf --run tap-csma&
55 // ping 10.1.1.2
56 //
57 #include <iostream>
58 #include <fstream>
59 
60 #include "ns3/core-module.h"
61 #include "ns3/network-module.h"
62 #include "ns3/wifi-module.h"
63 #include "ns3/csma-module.h"
64 #include "ns3/internet-module.h"
65 #include "ns3/ipv4-global-routing-helper.h"
66 #include "ns3/tap-bridge-module.h"
67 
68 using namespace ns3;
69 
70 NS_LOG_COMPONENT_DEFINE ("TapCsmaExample");
71 
72 int
73 main (int argc, char *argv[])
74 {
75  std::string mode = "ConfigureLocal";
76  std::string tapName = "thetap";
77 
79  cmd.AddValue ("mode", "Mode setting of TapBridge", mode);
80  cmd.AddValue ("tapName", "Name of the OS tap device", tapName);
81  cmd.Parse (argc, argv);
82 
83  GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
84  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
85 
87  nodes.Create (4);
88 
90  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
91  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
92 
93  NetDeviceContainer devices = csma.Install (nodes);
94 
96  stack.Install (nodes);
97 
98  Ipv4AddressHelper addresses;
99  addresses.SetBase ("10.1.1.0", "255.255.255.0");
100  Ipv4InterfaceContainer interfaces = addresses.Assign (devices);
101 
102  TapBridgeHelper tapBridge;
103  tapBridge.SetAttribute ("Mode", StringValue (mode));
104  tapBridge.SetAttribute ("DeviceName", StringValue (tapName));
105  tapBridge.Install (nodes.Get (0), devices.Get (0));
106 
107  csma.EnablePcapAll ("tap-csma", false);
109 
110  Simulator::Stop (Seconds (60.));
111  Simulator::Run ();
113 }
void SetAttribute(std::string n1, const AttributeValue &v1)
Set an attribute in the underlying TapBridge net device when these devices are automatically created...
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:71
AttributeValue implementation for Boolean.
Definition: boolean.h:36
tuple devices
Definition: first.py:32
holds a vector of std::pair of Ptr and interface index.
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
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:226
#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:1001
aggregate IP/TCP/UDP functionality to existing Nodes.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:217
tuple cmd
Definition: second.py:35
tuple nodes
Definition: first.py:25
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:1055
tuple interfaces
Definition: first.py:41
holds a vector of ns3::NetDevice pointers
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
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...
tuple stack
Definition: first.py:34
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ptr< NetDevice > Install(Ptr< Node > node, Ptr< NetDevice > nd)
This method installs a TapBridge on the specified Node and forms the bridge with the NetDevice specif...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
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:993
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.
build TapBridge to allow ns-3 simulations to interact with Linux tap devices and processes on the Lin...
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