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
main(int argc,char * argv[])73 main (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 
86   NodeContainer nodes;
87   nodes.Create (4);
88 
89   CsmaHelper csma;
90   csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
91   csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
92 
93   NetDeviceContainer devices = csma.Install (nodes);
94 
95   InternetStackHelper stack;
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);
108   Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
109 
110   Simulator::Stop (Seconds (60.));
111   Simulator::Run ();
112   Simulator::Destroy ();
113 }
114