(OK) houshoul 实验

hsl-5-win7-ns3-root.sh

#!/bin/sh

#------------------------------------------------------------------------------------------
# This tool is released under GNU GPL v2,v3
# Author: Tongguang Zhang
# Date: 2016-06-18
# 
# Note, Prerequisites for using this script:  You have already installed images and NS3.
# Path:
# NS3: /opt/tools/network_simulators/ns3/ns-allinone-3.25/
#------------------------------------------------------------------------------------------

create_win7(){	

	if [ ! -d /proc/sys/net/bridge ]; then
		modprobe br_netfilter
	fi

	cd /proc/sys/net/bridge
	for f in bridge-nf-*; do echo 0 > $f; done
	cd -

	#img="win7-"
	#path="/mnt/vm_img"

	for((id=1; id<=$1; id++))
	do

		bridge="br_w_${id}"
		tap="tap_w_${id}"

		tunctl -t ${tap}
		ifconfig ${tap} up
		brctl addbr ${bridge}
		brctl addif ${bridge} ${tap}
		ifconfig ${bridge} up

		#echo "VBoxManage startvm ${img}${id}"

		#gnome-terminal -x bash -c "VBoxManage createvm --name ${img}${id} --ostype Windows7 --register; 
#VBoxManage modifyvm ${img}${id} --cpus 4 --memory 6000 --vram 128 --usb off --audio pulse --audiocontroller sb16 --acpi on --rtcuseutc off --boot1 disk --boot2 dvd --nic1 bridged --nictype1 Am79C973 --bridgeadapter1 ${bridge} --nic2 none --nic3 none --nic4 none; 
#VBoxManage storagectl ${img}${id} --name "IDE Controller" --add ide --controller PIIX4; 
#VBoxManage internalcommands sethduuid ${path}/${img}${id}.vdi; 
#VBoxManage storageattach ${img}${id} --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium ${path}/${img}${id}.vdi; 
#VBoxManage startvm ${img}${id}; 
#sleep 5"

	done
}

destroy_win7(){

	#img="win7-"

	for((id=1; id<=$1; id++))
	do
		#echo "VBoxManage controlvm ${img}${id} poweroff"

		#VBoxManage controlvm ${img}${id} poweroff &>/dev/null
		#VBoxManage unregistervm ${img}${id} &>/dev/null
		#rm "/home/hsl/VirtualBox VMs/${img}${id}" -rf &>/dev/null

		#sleep 1

		bridge="br_w_${id}"
		tap="tap_w_${id}"

		ifconfig ${bridge} down &>/dev/null
		brctl delif ${bridge} ${tap} &>/dev/null
		brctl delbr ${bridge} &>/dev/null

		ifconfig ${tap} down &>/dev/null
		tunctl -d ${tap} &>/dev/null
	done
}


start_ns3(){
	echo "RUNNING SIMULATION, press CTRL-C to stop it"
	cd /opt/tools/network_simulators/ns3/ns-allinone-3.25/ns-3.25
	./waf --run scratch/seem-manet --vis &
	cd -
}


# ./hsl-5-win7-ns3-root.sh 5 create
# ./hsl-5-win7-ns3-root.sh 5 destroy

if [ $# -eq 2 ]; then

	if !([ $2 == "create" ]||[ $2 == "destroy" ]); then
		usage
		exit
	fi

	case $2 in
		create)
			create_win7 $1
			#sleep 1
			#start_ns3
		;;
		destroy)
			destroy_win7 $1
			#pkill seem-manet
		;;
	esac
else
	echo "usage"
	echo "./hsl-5-win7-ns3-root.sh 5 create"
	echo "./hsl-5-win7-ns3-root.sh 5 destroy"
fi

# step 1
# ./hsl-5-win7-ns3-root.sh 5 create

# step 2
# ./hsl-5-win7-ns3-user.sh 5 create

# step 3
# ./hsl-5-win7-ns3-user.sh 5 destroy

# step 4
# ./hsl-5-win7-ns3-root.sh 5 destroy

# scp seem-5-win7.cc 10.109.253.80:/opt/tools/network_simulators/ns3/ns-allinone-3.25/ns-3.25/scratch/
# scp hsl-5-win7-ns3-root.sh 10.109.253.80:/root
# scp hsl-5-win7-ns3-user.sh 10.109.253.80:/home/hsl

hsl-5-win7-ns3-user.sh

#!/bin/sh

#------------------------------------------------------------------------------------------
# This tool is released under GNU GPL v2,v3
# Author: Tongguang Zhang
# Date: 2016-06-18
# 
# Note, Prerequisites for using this script:  You have already installed images and NS3.
# Path:
# NS3: /opt/tools/network_simulators/ns3/ns-allinone-3.25/
#------------------------------------------------------------------------------------------

create_win7(){	

	img="win7-"
	path="/mnt/vm_img"

	for((id=1; id<=$1; id++))
	do

		bridge="br_w_${id}"

		echo "VBoxManage startvm ${img}${id}"

		gnome-terminal -x bash -c "VBoxManage createvm --name ${img}${id} --ostype Windows7_64 --register; 
VBoxManage modifyvm ${img}${id} --cpus 4 --memory 6000 --vram 128 --usb off --audio pulse --audiocontroller sb16 --acpi on --rtcuseutc off --boot1 disk --boot2 dvd --nic1 bridged --nictype1 Am79C973 --bridgeadapter1 ${bridge} --nic2 none --nic3 none --nic4 none; 
VBoxManage storagectl ${img}${id} --name "IDE Controller" --add ide --controller PIIX4; 
VBoxManage internalcommands sethduuid ${path}/${img}${id}.vdi; 
VBoxManage storageattach ${img}${id} --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium ${path}/${img}${id}.vdi; 
VBoxManage startvm ${img}${id}; 
sleep 1"

	done
}

destroy_win7(){

	img="win7-"

	for((id=1; id<=$1; id++))
	do
		echo "VBoxManage controlvm ${img}${id} poweroff"

		VBoxManage controlvm ${img}${id} poweroff &>/dev/null
		VBoxManage unregistervm ${img}${id} &>/dev/null
		rm "/home/hsl/VirtualBox VMs/${img}${id}" -rf &>/dev/null

		sleep 1
	done
}



# ./hsl-5-win7-ns3-user.sh 5 create
# ./hsl-5-win7-ns3-user.sh 5 destroy

if [ $# -eq 2 ]; then

	if !([ $2 == "create" ]||[ $2 == "destroy" ]); then
		usage
		exit
	fi

	case $2 in
		create)
			create_win7 $1
		;;
		destroy)
			destroy_win7 $1
		;;
	esac
else
	echo "usage"
	echo "./hsl-5-win7-ns3-user.sh 5 create"
	echo "./hsl-5-win7-ns3-user.sh 5 destroy"
fi

# step 0, executed by root
# ./hsl-5-win7-ns3-root.sh 5 create

# step 1
# ./hsl-5-win7-ns3-user.sh 5 create

# step 2
#	cd /opt/tools/network_simulators/ns3/ns-allinone-3.25/ns-3.25
#	./waf --run scratch/seem-5-win7 --vis
#	cd -

# step 3
# ./hsl-5-win7-ns3-user.sh 5 destroy

# step 1
# pkill seem-5-win7

seem-5-win7.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 "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"

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

// Default Network Topology
//
//       10.1.1.0
// n0 -------------- n1   n2   n3   n4   n5
//    point-to-point  |    |    |    |    |
//                    =====================
//                      LAN 10.1.2.0


using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("TapCsmaWin7");

int 
main (int argc, char *argv[])
{

  uint32_t nCsma = 5;

  CommandLine cmd;
  cmd.AddValue ("nCsma", "Number of "extra" CSMA nodes/devices", nCsma);

  cmd.Parse (argc,argv);

  nCsma = nCsma == 0 ? 1 : nCsma;

  NodeContainer nodes;
  nodes.Create (nCsma);

  CsmaHelper csma;
  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

  NetDeviceContainer devices;
  devices = csma.Install (nodes);

  TapBridgeHelper tapBridge;
  tapBridge.SetAttribute ("Mode", StringValue ("UseBridge"));

  tapBridge.SetAttribute ("DeviceName", StringValue ("tap_w_1"));
  tapBridge.Install (nodes.Get (0), devices.Get (0));

  tapBridge.SetAttribute ("DeviceName", StringValue ("tap_w_2"));
  tapBridge.Install (nodes.Get (1), devices.Get (1));

  tapBridge.SetAttribute ("DeviceName", StringValue ("tap_w_3"));
  tapBridge.Install (nodes.Get (2), devices.Get (2));

  tapBridge.SetAttribute ("DeviceName", StringValue ("tap_w_4"));
  tapBridge.Install (nodes.Get (3), devices.Get (3));

  tapBridge.SetAttribute ("DeviceName", StringValue ("tap_w_5"));
  tapBridge.Install (nodes.Get (4), devices.Get (4));

  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

seem-5-win7-switch.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
//
//        n0     n1
//        |      |
//       ----------
//       | Switch |
//       ----------
//        |      |
//        n2     n3
//
//
// - CBR/UDP flows from n0 to n1 and from n3 to n0
// - DropTail queues
// - Tracing of queues and packet receptions to file "openflow-switch.tr"
// - If order of adding nodes and netdevices is kept:
//      n0 = 00:00:00;00:00:01, n1 = 00:00:00:00:00:03, n3 = 00:00:00:00:00:07
//	and port number corresponds to node number, so port 0 is connected to n0, for example.

#include <iostream>
#include <fstream>

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/openflow-module.h"
#include "ns3/tap-bridge-module.h"

#include "ns3/log.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("OpenFlowCsmaSwitchWin7");

bool verbose = false;
bool use_drop = false;
ns3::Time timeout = ns3::Seconds (0);

bool
SetVerbose (std::string value)
{
  verbose = true;
  return true;
}

bool
SetDrop (std::string value)
{
  use_drop = true;
  return true;
}

bool
SetTimeout (std::string value)
{
  try {
      timeout = ns3::Seconds (atof (value.c_str ()));
      return true;
    }
  catch (...) { return false; }
  return false;
}

int
main (int argc, char *argv[])
{

  int num=5;

  #ifdef NS3_OPENFLOW
  //
  // Allow the user to override any of the defaults and the above Bind() at
  // run-time, via command-line arguments
  //
  CommandLine cmd;
  cmd.AddValue ("v", "Verbose (turns on logging).", MakeCallback (&SetVerbose));
  cmd.AddValue ("verbose", "Verbose (turns on logging).", MakeCallback (&SetVerbose));
  cmd.AddValue ("d", "Use Drop Controller (Learning if not specified).", MakeCallback (&SetDrop));
  cmd.AddValue ("drop", "Use Drop Controller (Learning if not specified).", MakeCallback (&SetDrop));
  cmd.AddValue ("t", "Learning Controller Timeout (has no effect if drop controller is specified).", MakeCallback ( &SetTimeout));
  cmd.AddValue ("timeout", "Learning Controller Timeout (has no effect if drop controller is specified).", MakeCallback ( &SetTimeout));

  cmd.Parse (argc, argv);

  if (verbose)
    {
      LogComponentEnable ("OpenFlowCsmaSwitchExample", LOG_LEVEL_INFO);
      LogComponentEnable ("OpenFlowInterface", LOG_LEVEL_INFO);
      LogComponentEnable ("OpenFlowSwitchNetDevice", LOG_LEVEL_INFO);
    }

  //
  // Explicitly create the nodes required by the topology (shown above).
  //
  NS_LOG_INFO ("Create nodes.");
  NodeContainer nodes;
  nodes.Create (num);

  NodeContainer csmaSwitch;
  csmaSwitch.Create (1);

  NS_LOG_INFO ("Build Topology");
  CsmaHelper csma;
  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));

  // Create the csma links, from each terminal to the switch
  NetDeviceContainer terminalDevices;
  NetDeviceContainer switchDevices;
  for (int i = 0; i < num; i++)
    {
      NetDeviceContainer link = csma.Install (NodeContainer (nodes.Get (i), csmaSwitch));
      terminalDevices.Add (link.Get (0));
      switchDevices.Add (link.Get (1));
    }

  // Create the switch netdevice, which will do the packet switching
  Ptr<Node> switchNode = csmaSwitch.Get (0);
  OpenFlowSwitchHelper swtch;

  if (use_drop)
    {
      Ptr<ns3::ofi::DropController> controller = CreateObject<ns3::ofi::DropController> ();
      swtch.Install (switchNode, switchDevices, controller);
    }
  else
    {
      Ptr<ns3::ofi::LearningController> controller = CreateObject<ns3::ofi::LearningController> ();
      if (!timeout.IsZero ()) controller->SetAttribute ("ExpirationTime", TimeValue (timeout));
      swtch.Install (switchNode, switchDevices, controller);
    }

  //
  // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
  // Trace output will be sent to the file "openflow-switch.tr"
  //
  AsciiTraceHelper ascii;
  csma.EnableAsciiAll (ascii.CreateFileStream ("openflow-switch.tr"));

  //
  // Also configure some tcpdump traces; each interface will be traced.
  // The output files will be named:
  //     openflow-switch-<nodeId>-<interfaceId>.pcap
  // and can be read by the "tcpdump -r" command (use "-tt" option to
  // display timestamps correctly)
  //
  csma.EnablePcapAll ("openflow-switch", false);

  //
  // Use the TapBridgeHelper to connect to the pre-configured tap devices for 
  // the left side.  We go with "UseBridge" mode since the CSMA devices support
  // promiscuous mode and can therefore make it appear that the bridge is 
  // extended into ns-3.  The install method essentially bridges the specified
  // tap to the specified CSMA device.
  //
  TapBridgeHelper tapBridge;
  tapBridge.SetAttribute ("Mode", StringValue ("UseBridge"));

  tapBridge.SetAttribute ("DeviceName", StringValue ("tap_w_1"));
  tapBridge.Install (nodes.Get (0), terminalDevices.Get (0));

  tapBridge.SetAttribute ("DeviceName", StringValue ("tap_w_2"));
  tapBridge.Install (nodes.Get (1), terminalDevices.Get (1));

  tapBridge.SetAttribute ("DeviceName", StringValue ("tap_w_3"));
  tapBridge.Install (nodes.Get (2), terminalDevices.Get (2));

  tapBridge.SetAttribute ("DeviceName", StringValue ("tap_w_4"));
  tapBridge.Install (nodes.Get (3), terminalDevices.Get (3));

  tapBridge.SetAttribute ("DeviceName", StringValue ("tap_w_5"));
  tapBridge.Install (nodes.Get (4), terminalDevices.Get (4));

  //
  // Now, do the actual simulation.
  //
  NS_LOG_INFO ("Run Simulation.");
  Simulator::Run ();
  Simulator::Destroy ();
  NS_LOG_INFO ("Done.");
  #else
  NS_LOG_INFO ("NS-3 OpenFlow is not enabled. Cannot run simulation.");
  #endif // NS3_OPENFLOW
}

run VM manually

img="win7-"
path="/mnt/vm_img"

id=1
bridge="br_w_1"
bash -c "VBoxManage createvm --name ${img}${id} --ostype Windows7_64 --register; 
VBoxManage modifyvm ${img}${id} --cpus 4 --memory 6000 --vram 128 --usb off --audio pulse --audiocontroller sb16 --acpi on --rtcuseutc off --boot1 disk --boot2 dvd --nic1 bridged --nictype1 82540EM --bridgeadapter1 ${bridge} --nic2 none --nic3 none --nic4 none; 
VBoxManage storagectl ${img}${id} --name SATA --add sata --controller IntelAHCI --portcount 2 --hostiocache on; 
VBoxManage internalcommands sethduuid ${path}/${img}${id}.vdi; 
VBoxManage storageattach ${img}${id} --storagectl SATA --port 0 --device 0 --type hdd --medium ${path}/${img}${id}.vdi; 
VBoxManage startvm ${img}${id}; 
sleep 1"

rm /mnt/vm_share/VirtualBoxVMs/win7-1/win7-1.vbox











原文地址:https://www.cnblogs.com/ztguang/p/12645889.html