Tap Bridge Model: ConfigureLocal Mode and UseLocal mode

 

Please read https://www.nsnam.org/docs/release/3.10/doxygen/group___tap_bridge_model.html first.

 

ConfigureLocal Mode (tap-csma.cc)

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */

/*

 * This program is free software; you can redistribute it and/or modify

 * it under the terms of the GNU General Public License version 2 as

 * published by the Free Software Foundation;

 *

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with this program; if not, write to the Free Software

 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */

 

// Network topology

//

// Packets sent to the device "thetap" on the Linux host will be sent to the

// tap bridge on node zero and then emitted onto the ns-3 simulated CSMA

// network.  ARP will be used on the CSMA network to resolve MAC addresses.

// Packets destined for the CSMA device on node zero will be sent to the

// device "thetap" on the linux Host.

//

//  +----------+

//  | external |

//  |  Linux   |

//  |   Host   |

//  |          |

//  | "thetap" |

//  +----------+

//       |           n0            n1            n2            n3

//       |       +--------+    +--------+    +--------+    +--------+

//       +-------|  tap   |    |        |    |        |    |        |

//               | bridge |    |        |    |        |    |        |

//               +--------+    +--------+    +--------+    +--------+

//               |  CSMA  |    |  CSMA  |    |  CSMA  |    |  CSMA  |

//               +--------+    +--------+    +--------+    +--------+

//                   |             |             |             |

//                   |             |             |             |

//                   |             |             |             |

//                   ===========================================

//                                 CSMA LAN 10.1.1

//

// The CSMA device on node zero is:  10.1.1.1

// The CSMA device on node one is:   10.1.1.2

// The CSMA device on node two is:   10.1.1.3

// The CSMA device on node three is: 10.1.1.4

//

// Some simple things to do:

//

// 1) Ping one of the simulated nodes

//

//    ./waf --run tap-csma&

//    ping 10.1.1.2

//

#include <iostream>

#include <fstream>

 

#include "ns3/core-module.h"

#include "ns3/network-module.h"

#include "ns3/wifi-module.h"

#include "ns3/csma-module.h"

#include "ns3/internet-module.h"

#include "ns3/ipv4-global-routing-helper.h"

#include "ns3/tap-bridge-module.h"

 

using namespace ns3;

 

NS_LOG_COMPONENT_DEFINE ("TapCsmaExample");

 

int

main (int argc, char *argv[])

{

  std::string mode = "ConfigureLocal";

  std::string tapName = "thetap";

 

  CommandLine cmd (__FILE__);

  cmd.AddValue ("mode", "Mode setting of TapBridge", mode);

  cmd.AddValue ("tapName", "Name of the OS tap device", tapName);

  cmd.Parse (argc, argv);

 

  GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));

  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));

 

  NodeContainer nodes;

  nodes.Create (4);

 

  CsmaHelper csma;

  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));

  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));

 

  NetDeviceContainer devices = csma.Install (nodes);

 

  InternetStackHelper stack;

  stack.Install (nodes);

 

  Ipv4AddressHelper addresses;

  addresses.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer interfaces = addresses.Assign (devices);

 

  TapBridgeHelper tapBridge;

  tapBridge.SetAttribute ("Mode", StringValue (mode));

  tapBridge.SetAttribute ("DeviceName", StringValue (tapName));

  tapBridge.Install (nodes.Get (0), devices.Get (0));

 

  csma.EnablePcapAll ("tap-csma", false);

  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

 

  Simulator::Stop (Seconds (60.));

  Simulator::Run ();

  Simulator::Destroy ();

}

 

Execution

 

 

Open another terminal, and type “ifconfig”. You can find another interface “thetap”. IP is set to 10.1.1.1.

 

You can ping the NS3 nodes (10.1.1.2 or 10.1.1.3).

 

 

After simulation, “thetap” is gone.

 

tap-csma-uselocal.cc

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */

/*

 * This program is free software; you can redistribute it and/or modify

 * it under the terms of the GNU General Public License version 2 as

 * published by the Free Software Foundation;

 *

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with this program; if not, write to the Free Software

 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */

 

#include <iostream>

#include <fstream>

 

#include "ns3/core-module.h"

#include "ns3/network-module.h"

#include "ns3/wifi-module.h"

#include "ns3/csma-module.h"

#include "ns3/internet-module.h"

#include "ns3/ipv4-global-routing-helper.h"

#include "ns3/tap-bridge-module.h"

 

using namespace ns3;

 

NS_LOG_COMPONENT_DEFINE ("TapCsmaExample");

 

int

main (int argc, char *argv[])

{

  std::string mode = "UseLocal";

 

  CommandLine cmd (__FILE__);

  cmd.Parse (argc, argv);

 

  GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));

  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));

 

  NodeContainer nodes;

  nodes.Create (4);

 

  CsmaHelper csma;

  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));

  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));

 

  NetDeviceContainer devices = csma.Install (nodes);

 

  InternetStackHelper stack;

  stack.Install (nodes);

 

  Ipv4AddressHelper addresses;

  addresses.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer interfaces = addresses.Assign (devices);

 

  TapBridgeHelper tapBridge;

  tapBridge.SetAttribute ("Mode", StringValue (mode));

  tapBridge.SetAttribute ("DeviceName", StringValue ("mytap"));

  tapBridge.Install (nodes.Get (0), devices.Get (0));

 

  csma.EnablePcapAll ("tap-csma", false);

  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

 

  Simulator::Stop (Seconds (60.));

  Simulator::Run ();

  Simulator::Destroy ();

}

 

Execution

 

Because executing simulation, create a tap interface.

 

 

Open another terminal. You can ping NS3 nodes.

 

After simulation, “mytap” still exists.

 

Back to NS3 Learning Guide

Last Modified: 2022/2/24 done

 

[Author]

Dr. Chih-Heng Ke

Department of Computer Science and Information Engineering, National Quemoy University, Kinmen, Taiwan

Email: smallko@gmail.com