Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
readded active probing = false
[simgrid.git] / src / surf / network_ns3.cpp
index 5f72405..76215d9 100644 (file)
 #include <ns3/application-container.h>
 #include <ns3/event-id.h>
 
-#include "ns3/core-module.h"
 #include "ns3/wifi-module.h"
 #include "ns3/mobility-module.h"
 
-
 #include "network_ns3.hpp"
 #include "ns3/ns3_simulator.hpp"
 
@@ -40,6 +38,7 @@
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ns3, surf, "Logging specific to the SURF network ns-3 module");
 
 std::vector<std::string> IPV4addr;
+static std::string transformIpv4Address(ns3::Ipv4Address from);
 
 /*****************
  * Crude globals *
@@ -53,18 +52,18 @@ static ns3::NodeContainer nodes;
 static ns3::NodeContainer Cluster_nodes;
 static ns3::Ipv4InterfaceContainer interfaces;
 
-/* wifi globals */
-static ns3::YansWifiChannelHelper channel = ns3::YansWifiChannelHelper::Default ();
-static ns3::YansWifiPhyHelper phy = ns3::YansWifiPhyHelper::Default ();
-static ns3::WifiHelper wifi;
-static ns3::WifiMacHelper mac;
-static ns3::MobilityHelper mobility;
-
 static int number_of_nodes = 0;
 static int number_of_clusters_nodes = 0;
 static int number_of_links = 1;
 static int number_of_networks = 1;
 
+/* wifi globals */
+static ns3::WifiHelper wifi;
+static ns3::YansWifiPhyHelper wifiPhy = ns3::YansWifiPhyHelper::Default ();
+static ns3::YansWifiChannelHelper wifiChannel = ns3::YansWifiChannelHelper::Default ();
+static ns3::WifiMacHelper wifiMac;
+static ns3::MobilityHelper mobility;
+
 simgrid::xbt::Extension<simgrid::kernel::routing::NetPoint, NetPointNs3> NetPointNs3::EXTENSION_ID;
 
 NetPointNs3::NetPointNs3() : ns3_node_(ns3::CreateObject<ns3::Node>(0))
@@ -74,6 +73,48 @@ NetPointNs3::NetPointNs3() : ns3_node_(ns3::CreateObject<ns3::Node>(0))
   node_num = number_of_nodes++;
 }
 
+WifiZone::WifiZone(std::string name_, simgrid::s4u::Host* host_, ns3::Ptr<ns3::Node> ap_node_,
+                   ns3::Ptr<ns3::YansWifiChannel> channel_, int mcs_, int nss_, int network_, int link_) :
+    name(name_), host(host_), ap_node(ap_node_), channel(channel_), mcs(mcs_), nss(nss_),
+    network(network_), link(link_) {
+    n_sta_nodes = 0;
+    wifi_zones[name_] = this;
+}
+
+bool WifiZone::is_ap(ns3::Ptr<ns3::Node> node){
+    for (std::pair<std::string, WifiZone*> zone : wifi_zones)
+        if (zone.second->get_ap_node() == node)
+            return true;
+    return false;
+}
+
+WifiZone* WifiZone::by_name(std::string name) {
+    WifiZone* zone;
+    try {
+        zone = wifi_zones.at(name);
+    }
+    catch (const std::out_of_range& oor) {
+        return nullptr;
+    }
+    return zone;
+}
+
+std::unordered_map<std::string, WifiZone*> WifiZone::wifi_zones;
+
+static void initialize_ns3_wifi() {
+    wifi.SetStandard (ns3::WIFI_PHY_STANDARD_80211n_5GHZ);
+
+    for (auto host : simgrid::s4u::Engine::get_instance()->get_all_hosts()) {
+        const char* wifi_link = host->get_property("wifi_link");
+        const char* wifi_mcs = host->get_property("wifi_mcs");
+        const char* wifi_nss = host->get_property("wifi_nss");
+
+        if (wifi_link)
+          new WifiZone(wifi_link, host, host->get_netpoint()->extension<NetPointNs3>()->ns3_node_,
+                       wifiChannel.Create(), wifi_mcs ? atoi(wifi_mcs) : 3, wifi_nss ? atoi(wifi_nss) : 1, 0, 0);
+    }
+}
+
 /*************
  * Callbacks *
  *************/
@@ -91,7 +132,7 @@ static void clusterCreation_cb(simgrid::kernel::routing::ClusterCreationArgs con
     xbt_assert(host_src, "Cannot find a ns-3 host of name %s", host_id.c_str());
 
     // Any ns-3 route is symmetrical
-    ns3_add_direct_route(host_src, host_dst, cluster.bw, cluster.lat, cluster.sharing_policy);
+    ns3_add_direct_route(host_src, host_dst, cluster.bw, cluster.lat, cluster.id, cluster.sharing_policy);
 
     delete host_dst;
   }
@@ -105,8 +146,6 @@ static void routeCreation_cb(bool symmetrical, simgrid::kernel::routing::NetPoin
                              simgrid::kernel::routing::NetPoint* /*gw_dst*/,
                              std::vector<simgrid::kernel::resource::LinkImpl*> const& link_list)
 {
-  XBT_INFO("routeCreation_cb START");
-
   if (link_list.size() == 1) {
     auto* link = static_cast<simgrid::kernel::resource::LinkNS3*>(link_list[0]);
 
@@ -121,12 +160,13 @@ static void routeCreation_cb(bool symmetrical, simgrid::kernel::routing::NetPoin
     auto* host_src = src->extension<NetPointNs3>();
     auto* host_dst = dst->extension<NetPointNs3>();
 
+    host_src->set_name(src->get_name());
+    host_dst->set_name(dst->get_name());
+
     xbt_assert(host_src != nullptr, "Network element %s does not seem to be ns-3-ready", src->get_cname());
     xbt_assert(host_dst != nullptr, "Network element %s does not seem to be ns-3-ready", dst->get_cname());
 
-    XBT_INFO("routeCreation_cb MID");
-    ns3_add_direct_route(host_src, host_dst, link->get_bandwidth(), link->get_latency(), link->get_sharing_policy());
-    XBT_INFO("routeCreation_cb MID2");
+    ns3_add_direct_route(host_src, host_dst, link->get_bandwidth(), link->get_latency(), link->get_name(), link->get_sharing_policy());
   } else {
     static bool warned_about_long_routes = false;
 
@@ -139,14 +179,12 @@ static void routeCreation_cb(bool symmetrical, simgrid::kernel::routing::NetPoin
                src->get_cname(), dst->get_cname(), link_list.size());
     warned_about_long_routes = true;
   }
-  XBT_INFO("routeCreation_cb END");
 }
 
 /* Create the ns3 topology based on routing strategy */
 static void postparse_cb()
 {
   IPV4addr.shrink_to_fit();
-
   ns3::GlobalRouteManager::BuildGlobalRoutingDatabase();
   ns3::GlobalRouteManager::InitializeRoutes();
 }
@@ -196,9 +234,7 @@ NetworkNS3Model::~NetworkNS3Model() {
 LinkImpl* NetworkNS3Model::create_link(const std::string& name, const std::vector<double>& bandwidths, double latency,
                                        s4u::Link::SharingPolicy policy)
 {
-  XBT_INFO("create_link START");
   xbt_assert(bandwidths.size() == 1, "ns-3 links must use only 1 bandwidth.");
-  XBT_INFO("create_link END");
   return new LinkNS3(this, name, bandwidths[0], latency, policy);
 }
 
@@ -303,37 +339,63 @@ LinkNS3::LinkNS3(NetworkNS3Model* model, const std::string& name, double bandwid
                  s4u::Link::SharingPolicy policy)
     : LinkImpl(model, name, nullptr)
 {
-  XBT_INFO("LinkNS3 START");
   bandwidth_.peak = bandwidth;
   latency_.peak   = latency;
   sharing_policy_ = policy;
 
-  /* If wifi, create the wifizone now. If not, don't do anything: the links will be created in routeCreate_cb */
-  if (policy == s4u::Link::SharingPolicy::WIFI) {
-        ns3::NodeContainer ap_nodes;
-        ap_nodes.Create(1);
-
-        phy.SetChannel (channel.Create ());
-
-        wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
-
-        mac.SetType ("ns3::ApWifiMac");
-        ns3::NetDeviceContainer apDevices;
-        apDevices = wifi.Install (phy, mac, ap_nodes);
-
-        mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
-        mobility.Install (ap_nodes);
-
-        stack.Install(ap_nodes);
-
-        ns3::Ipv4AddressHelper address;
-        std::string addr = simgrid::xbt::string_printf("%d.%d.0.0", number_of_networks, number_of_links);
-        address.SetBase(addr.c_str(), "255.255.0.0");
-        interfaces.Add(address.Assign(apDevices));
+  if (policy == simgrid::s4u::Link::SharingPolicy::WIFI) {
+      static bool wifi_init = false;
+      if (!wifi_init) {
+          initialize_ns3_wifi();
+          wifi_init = true;
+      }
+
+      ns3::NetDeviceContainer netA;
+      WifiZone* zone = WifiZone::by_name(name);
+      xbt_assert(zone != 0, "Link name '%s' does not match the 'wifi_link' property of a host.", name.c_str());
+      NetPointNs3* netpoint_ns3 = zone->get_host()->get_netpoint()->extension<NetPointNs3>();     
+
+      wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
+                                    "ControlMode", ns3::StringValue ("HtMcs0"),
+                                    "DataMode", ns3::StringValue ("HtMcs" + std::to_string(zone->get_mcs())));
+
+      wifiPhy.SetChannel (zone->get_channel());
+      wifiPhy.Set("Antennas", ns3::UintegerValue(zone->get_nss()));
+      wifiPhy.Set("MaxSupportedTxSpatialStreams", ns3::UintegerValue(zone->get_nss()));
+      wifiPhy.Set("MaxSupportedRxSpatialStreams", ns3::UintegerValue(zone->get_nss()));
+      wifiMac.SetType("ns3::ApWifiMac",
+                      "Ssid", ns3::SsidValue(name));
+
+      netA.Add(wifi.Install (wifiPhy, wifiMac, zone->get_ap_node()));
+
+      ns3::Ptr<ns3::ListPositionAllocator> positionAllocS = ns3::CreateObject<ns3::ListPositionAllocator> ();
+      positionAllocS->Add(ns3::Vector(0, 0, 0));
+      mobility.SetPositionAllocator(positionAllocS);
+      mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
+      mobility.Install(zone->get_ap_node());
+
+      ns3::Ipv4AddressHelper address;
+      std::string addr = simgrid::xbt::string_printf("%d.%d.0.0", number_of_networks, number_of_links);
+      address.SetBase(addr.c_str(), "255.255.0.0");
+      XBT_DEBUG("\tInterface stack '%s'", addr.c_str());
+      interfaces.Add(address.Assign (netA));
+      zone->set_network(number_of_networks);
+      zone->set_link(number_of_links);
+
+      int nodeNum = netpoint_ns3->node_num;
+      if (IPV4addr.size() <= (unsigned)nodeNum)
+        IPV4addr.resize(nodeNum + 1);
+      IPV4addr[nodeNum] = transformIpv4Address(interfaces.GetAddress(interfaces.GetN() - 1));
+
+      if (number_of_links == 255){
+        xbt_assert(number_of_networks < 255, "Number of links and networks exceed 255*255");
+        number_of_links = 1;
+        number_of_networks++;
+      } else {
+        number_of_links++;
+      }
   }
-
   s4u::Link::on_creation(*this->get_iface());
-  XBT_INFO("LinkNS3 END");
 }
 
 LinkNS3::~LinkNS3() = default;
@@ -515,11 +577,9 @@ static std::string transformIpv4Address(ns3::Ipv4Address from)
   return sstream.str();
 }
 
-void ns3_add_direct_route(NetPointNs3* src, NetPointNs3* dst, double bw, double lat,
+void ns3_add_direct_route(NetPointNs3* src, NetPointNs3* dst, double bw, double lat, std::string link_name,
                           simgrid::s4u::Link::SharingPolicy policy)
 {
-  XBT_INFO("add_direct_route START");
-
   ns3::Ipv4AddressHelper address;
   ns3::NetDeviceContainer netA;
 
@@ -529,56 +589,75 @@ void ns3_add_direct_route(NetPointNs3* src, NetPointNs3* dst, double bw, double
   ns3::Ptr<ns3::Node> a = src->ns3_node_;
   ns3::Ptr<ns3::Node> b = dst->ns3_node_;
 
-  bool initial_ip_reserved;
-
   if (policy == simgrid::s4u::Link::SharingPolicy::WIFI) {
-      mac.SetType ("ns3::StaWifiMac",
-                   "ActiveProbing", ns3::BooleanValue (false));
-      netA.Add(wifi.Install (phy, mac, a));
-      netA.Add(wifi.Install (phy, mac, b));
-
-      mobility.Install (a);
-      mobility.Install (b);
-
-      initial_ip_reserved = true;
+      xbt_assert(WifiZone::is_ap(a) != WifiZone::is_ap(b), "A wifi route can only exist between an access point node and a station node.");
+
+      ns3::Ptr<ns3::Node> apNode = WifiZone::is_ap(a) ? a : b;
+      ns3::Ptr<ns3::Node> staNode = apNode == a ? b : a;
+
+      WifiZone* zone = WifiZone::by_name(link_name);
+
+      wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
+                                    "ControlMode", ns3::StringValue ("HtMcs0"),
+                                    "DataMode", ns3::StringValue ("HtMcs" + std::to_string(zone->get_mcs())));
+
+      wifiPhy.SetChannel (zone->get_channel());
+      wifiPhy.Set("Antennas", ns3::UintegerValue(zone->get_nss()));
+      wifiPhy.Set("MaxSupportedTxSpatialStreams", ns3::UintegerValue(zone->get_nss()));
+      wifiPhy.Set("MaxSupportedRxSpatialStreams", ns3::UintegerValue(zone->get_nss()));
+
+      wifiMac.SetType ("ns3::StaWifiMac",
+                       "Ssid", ns3::SsidValue(link_name),
+                       "ActiveProbing", ns3::BooleanValue(false));
+
+      netA.Add(wifi.Install (wifiPhy, wifiMac, staNode));
+
+      ns3::Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", ns3::UintegerValue (40));
+
+      NetPointNs3* sta_netpointNs3 = WifiZone::is_ap(src->ns3_node_) ? dst : src;
+      const char* wifi_distance = simgrid::s4u::Host::by_name(sta_netpointNs3->name_)->get_property("wifi_distance");
+      ns3::Ptr<ns3::ListPositionAllocator> positionAllocS = ns3::CreateObject<ns3::ListPositionAllocator> ();
+      positionAllocS->Add(ns3::Vector( wifi_distance ? atof(wifi_distance) : 10.0 , 0, 0));
+      mobility.SetPositionAllocator(positionAllocS);
+      mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
+      mobility.Install(staNode);
+
+      std::string addr = simgrid::xbt::string_printf("%d.%d.0.0", zone->get_network(), zone->get_link());
+      address.SetBase(addr.c_str(), "255.255.0.0", ("0.0.0." + std::to_string(zone->get_n_sta_nodes() + 2)).c_str());
+      zone->add_sta_node();
+      XBT_DEBUG("\tInterface stack '%s'", addr.c_str());
+      interfaces.Add(address.Assign (netA));
+      if (IPV4addr.size() <= (unsigned)dstNum)
+        IPV4addr.resize(dstNum + 1);
+      IPV4addr[dstNum] = transformIpv4Address(interfaces.GetAddress(interfaces.GetN() - 1));
   } else {
     ns3::PointToPointHelper pointToPoint;
-
     XBT_DEBUG("\tAdd PTP from %d to %d bw:'%f Bps' lat:'%fs'", srcNum, dstNum, bw, lat);
     pointToPoint.SetDeviceAttribute("DataRate",
                                     ns3::DataRateValue(ns3::DataRate(bw * 8))); // ns-3 takes bps, but we provide Bps
     pointToPoint.SetChannelAttribute("Delay", ns3::TimeValue(ns3::Seconds(lat)));
 
     netA.Add(pointToPoint.Install(a, b));
-    initial_ip_reserved = false;
-  }
 
-  XBT_INFO("1");
-  std::string addr = simgrid::xbt::string_printf("%d.%d.0.10", number_of_networks, number_of_links, initial_ip_reserved);
-  address.SetBase(addr.c_str(), "255.255.0.0", "0.0.0.2");
-  XBT_DEBUG("\tInterface stack '%s'", addr.c_str());
-  XBT_INFO("1.5");
-  interfaces.Add(address.Assign (netA));
+    std::string addr = simgrid::xbt::string_printf("%d.%d.0.0", number_of_networks, number_of_links);
+    address.SetBase(addr.c_str(), "255.255.0.0");
+    XBT_DEBUG("\tInterface stack '%s'", addr.c_str());
+    interfaces.Add(address.Assign (netA));
 
-  XBT_INFO("2");
+    if (IPV4addr.size() <= (unsigned)srcNum)
+        IPV4addr.resize(srcNum + 1);
+    IPV4addr[srcNum] = transformIpv4Address(interfaces.GetAddress(interfaces.GetN() - 2));
 
-  if (IPV4addr.size() <= (unsigned)srcNum)
-    IPV4addr.resize(srcNum + 1);
-  IPV4addr[srcNum] = transformIpv4Address(interfaces.GetAddress(interfaces.GetN() - 2));
+    if (IPV4addr.size() <= (unsigned)dstNum)
+        IPV4addr.resize(dstNum + 1);
+    IPV4addr[dstNum] = transformIpv4Address(interfaces.GetAddress(interfaces.GetN() - 1));
 
-  XBT_INFO("3");
-
-  if (IPV4addr.size() <= (unsigned)dstNum)
-    IPV4addr.resize(dstNum + 1);
-  IPV4addr[dstNum] = transformIpv4Address(interfaces.GetAddress(interfaces.GetN() - 1));
-
-  XBT_INFO("4");
-  if (number_of_links == 255){
-    xbt_assert(number_of_networks < 255, "Number of links and networks exceed 255*255");
-    number_of_links = 1;
-    number_of_networks++;
-  } else {
-    number_of_links++;
+    if (number_of_links == 255){
+        xbt_assert(number_of_networks < 255, "Number of links and networks exceed 255*255");
+        number_of_links = 1;
+        number_of_networks++;
+    } else {
+        number_of_links++;
+    }
   }
-  XBT_INFO("5");
 }