A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tap-csma.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// Network topology
6//
7// Packets sent to the device "thetap" on the Linux host will be sent to the
8// tap bridge on node zero and then emitted onto the ns-3 simulated CSMA
9// network. ARP will be used on the CSMA network to resolve MAC addresses.
10// Packets destined for the CSMA device on node zero will be sent to the
11// device "thetap" on the linux Host.
12//
13// +----------+
14// | external |
15// | Linux |
16// | Host |
17// | |
18// | "thetap" |
19// +----------+
20// | n0 n1 n2 n3
21// | +--------+ +--------+ +--------+ +--------+
22// +-------| tap | | | | | | |
23// | bridge | | | | | | |
24// +--------+ +--------+ +--------+ +--------+
25// | CSMA | | CSMA | | CSMA | | CSMA |
26// +--------+ +--------+ +--------+ +--------+
27// | | | |
28// | | | |
29// | | | |
30// ===========================================
31// CSMA LAN 10.1.1
32//
33// The CSMA device on node zero is: 10.1.1.1
34// The CSMA device on node one is: 10.1.1.2
35// The CSMA device on node two is: 10.1.1.3
36// The CSMA device on node three is: 10.1.1.4
37//
38// Some simple things to do:
39//
40// 1) Ping one of the simulated nodes
41//
42// ./ns3 run tap-csma&
43// ping 10.1.1.2
44//
45#include "ns3/core-module.h"
46#include "ns3/csma-module.h"
47#include "ns3/internet-module.h"
48#include "ns3/ipv4-global-routing-helper.h"
49#include "ns3/network-module.h"
50#include "ns3/tap-bridge-module.h"
51#include "ns3/wifi-module.h"
52
53#include <fstream>
54#include <iostream>
55
56using namespace ns3;
57
58NS_LOG_COMPONENT_DEFINE("TapCsmaExample");
59
60int
61main(int argc, char* argv[])
62{
63 std::string mode = "ConfigureLocal";
64 std::string tapName = "thetap";
65
66 CommandLine cmd(__FILE__);
67 cmd.AddValue("mode", "Mode setting of TapBridge", mode);
68 cmd.AddValue("tapName", "Name of the OS tap device", tapName);
69 cmd.Parse(argc, argv);
70
71 GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
72 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
73
75 nodes.Create(4);
76
78 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
79 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
80
82
84 stack.Install(nodes);
85
86 Ipv4AddressHelper addresses;
87 addresses.SetBase("10.1.1.0", "255.255.255.0");
88 Ipv4InterfaceContainer interfaces = addresses.Assign(devices);
89
90 TapBridgeHelper tapBridge;
91 tapBridge.SetAttribute("Mode", StringValue(mode));
92 tapBridge.SetAttribute("DeviceName", StringValue(tapName));
93 tapBridge.Install(nodes.Get(0), devices.Get(0));
94
95 csma.EnablePcapAll("tap-csma", false);
97
101
102 return 0;
103}
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
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:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
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
stack
Definition first.py:33
interfaces
Definition first.py:39
Every class exported by the ns3 library is enclosed in the ns3 namespace.