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// ./ns3 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
68using namespace ns3;
69
70NS_LOG_COMPONENT_DEFINE ("TapCsmaExample");
71
72int
73main (int argc, char *argv[])
74{
75 std::string mode = "ConfigureLocal";
76 std::string tapName = "thetap";
77
78 CommandLine cmd (__FILE__);
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
94
96 stack.Install (nodes);
97
98 Ipv4AddressHelper addresses;
99 addresses.SetBase ("10.1.1.0", "255.255.255.0");
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);
108 Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
109
110 Simulator::Stop (Seconds (60.));
111 Simulator::Run ();
112 Simulator::Destroy ();
113}
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:229
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
AttributeValue implementation for DataRate.
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...
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.
Hold variables of type string.
Definition: string.h:41
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:1308
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
devices
Definition: first.py:39
stack
Definition: first.py:41
nodes
Definition: first.py:32
interfaces
Definition: first.py:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
csma
Definition: second.py:63
cmd
Definition: second.py:35