A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tap-csma.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
16// Network topology
17//
18// Packets sent to the device "thetap" on the Linux host will be sent to the
19// tap bridge on node zero and then emitted onto the ns-3 simulated CSMA
20// network. ARP will be used on the CSMA network to resolve MAC addresses.
21// Packets destined for the CSMA device on node zero will be sent to the
22// device "thetap" on the linux Host.
23//
24// +----------+
25// | external |
26// | Linux |
27// | Host |
28// | |
29// | "thetap" |
30// +----------+
31// | n0 n1 n2 n3
32// | +--------+ +--------+ +--------+ +--------+
33// +-------| tap | | | | | | |
34// | bridge | | | | | | |
35// +--------+ +--------+ +--------+ +--------+
36// | CSMA | | CSMA | | CSMA | | CSMA |
37// +--------+ +--------+ +--------+ +--------+
38// | | | |
39// | | | |
40// | | | |
41// ===========================================
42// CSMA LAN 10.1.1
43//
44// The CSMA device on node zero is: 10.1.1.1
45// The CSMA device on node one is: 10.1.1.2
46// The CSMA device on node two is: 10.1.1.3
47// The CSMA device on node three is: 10.1.1.4
48//
49// Some simple things to do:
50//
51// 1) Ping one of the simulated nodes
52//
53// ./ns3 run tap-csma&
54// ping 10.1.1.2
55//
56#include "ns3/core-module.h"
57#include "ns3/csma-module.h"
58#include "ns3/internet-module.h"
59#include "ns3/ipv4-global-routing-helper.h"
60#include "ns3/network-module.h"
61#include "ns3/tap-bridge-module.h"
62#include "ns3/wifi-module.h"
63
64#include <fstream>
65#include <iostream>
66
67using namespace ns3;
68
69NS_LOG_COMPONENT_DEFINE("TapCsmaExample");
70
71int
72main(int argc, char* argv[])
73{
74 std::string mode = "ConfigureLocal";
75 std::string tapName = "thetap";
76
77 CommandLine cmd(__FILE__);
78 cmd.AddValue("mode", "Mode setting of TapBridge", mode);
79 cmd.AddValue("tapName", "Name of the OS tap device", tapName);
80 cmd.Parse(argc, argv);
81
82 GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
83 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
84
86 nodes.Create(4);
87
89 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
90 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
91
93
95 stack.Install(nodes);
96
97 Ipv4AddressHelper addresses;
98 addresses.SetBase("10.1.1.0", "255.255.255.0");
99 Ipv4InterfaceContainer interfaces = addresses.Assign(devices);
100
101 TapBridgeHelper tapBridge;
102 tapBridge.SetAttribute("Mode", StringValue(mode));
103 tapBridge.SetAttribute("DeviceName", StringValue(tapName));
104 tapBridge.Install(nodes.Get(0), devices.Get(0));
105
106 csma.EnablePcapAll("tap-csma", false);
108
112
113 return 0;
114}
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
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...
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 void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
build TapBridge to allow ns-3 simulations to interact with Linux tap devices and processes on the Lin...
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...
void SetAttribute(std::string n1, const AttributeValue &v1)
Set an attribute in the underlying TapBridge net device when these devices are automatically created.
AttributeValue implementation for Time.
Definition: nstime.h:1406
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
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
ns interfaces
Definition: first.py:50
ns stack
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:40
ns csma
Definition: second.py:63