Mininet Host talks to NS3 Node

 

[tap-csma.cc] (put this file under scratch)

/* -*- 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 (600.));

 

  Simulator::Run ();

 

  Simulator::Destroy ();

 

}

 

[test.py]

#!/usr/bin/python

from mininet.net import Mininet

from mininet.node import Controller

from mininet.cli import CLI

from mininet.link import Intf

from mininet.log import setLogLevel, info

 

def myNetwork():

 

    net = Mininet( topo=None, build=False)

 

    info( '*** Adding controller\n' )

    net.addController(name='c0')

 

    info( '*** Add switches\n')

    s1 = net.addSwitch('s1')

    Intf( 'mytap', node=s1 )

 

    info( '*** Add hosts\n')

    h1 = net.addHost('h1', ip='10.1.1.10')

    h2 = net.addHost('h2', ip='10.1.1.11')

 

    info( '*** Add links\n')

    net.addLink(h1, s1)

    net.addLink(h2, s1)

 

    info( '*** Starting network\n')

    net.start()

    #h1.cmdPrint('dhclient '+h1.defaultIntf().name)

    CLI(net)

    net.stop()

 

if __name__ == '__main__':

    setLogLevel( 'info' )

    myNetwork()

 

[Execution]

Open one terminal

 

Open another terminal

 

Open the third terminal. (h1:10.1.1.10, the NS3 node: 10.1.1.2 …)

 

Back to NS3 Learning Guide

Last Modified: 2022/9/6 done

 

[Author]

Dr. Chih-Heng Ke

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

Email: smallko@gmail.com