src/nix-vector-routing/examples/nms-p2p-nix.cc
changeset 9063 32755d0516f4
parent 8997 9222fc5291ca
child 11775 51b05c86d144
equal deleted inserted replaced
9062:d14e2430213d 9063:32755d0516f4
    46 #include "ns3/simulator.h"
    46 #include "ns3/simulator.h"
    47 #include "ns3/ipv4-static-routing-helper.h"
    47 #include "ns3/ipv4-static-routing-helper.h"
    48 #include "ns3/ipv4-list-routing-helper.h"
    48 #include "ns3/ipv4-list-routing-helper.h"
    49 #include "ns3/ipv4-nix-vector-helper.h"
    49 #include "ns3/ipv4-nix-vector-helper.h"
    50 
    50 
    51 using namespace std;
       
    52 using namespace ns3;
    51 using namespace ns3;
    53 
    52 
    54 typedef struct timeval TIMER_TYPE;
    53 typedef struct timeval TIMER_TYPE;
    55 #define TIMER_NOW(_t) gettimeofday (&_t,NULL);
    54 #define TIMER_NOW(_t) gettimeofday (&_t,NULL);
    56 #define TIMER_SECONDS(_t) ((double)(_t).tv_sec + (_t).tv_usec*1e-6)
    55 #define TIMER_SECONDS(_t) ((double)(_t).tv_sec + (_t).tv_usec*1e-6)
   124 int
   123 int
   125 main (int argc, char *argv[])
   124 main (int argc, char *argv[])
   126 {
   125 {
   127   TIMER_TYPE t0, t1, t2;
   126   TIMER_TYPE t0, t1, t2;
   128   TIMER_NOW (t0);
   127   TIMER_NOW (t0);
   129   cout << " ==== DARPA NMS CAMPUS NETWORK SIMULATION ====" << endl;
   128   std::cout << " ==== DARPA NMS CAMPUS NETWORK SIMULATION ====" << std::endl;
   130   LogComponentEnable ("OnOffApplication", LOG_LEVEL_INFO);
   129   LogComponentEnable ("OnOffApplication", LOG_LEVEL_INFO);
   131 
   130 
   132   int nCN = 2, nLANClients = 42;
   131   int nCN = 2, nLANClients = 42;
   133   bool nix = true;
   132   bool nix = true;
   134 
   133 
   138   cmd.AddValue ("NIX", "Toggle nix-vector routing", nix);
   137   cmd.AddValue ("NIX", "Toggle nix-vector routing", nix);
   139   cmd.Parse (argc,argv);
   138   cmd.Parse (argc,argv);
   140 
   139 
   141   if (nCN < 2) 
   140   if (nCN < 2) 
   142     {
   141     {
   143       cout << "Number of total CNs (" << nCN << ") lower than minimum of 2"
   142       std::cout << "Number of total CNs (" << nCN << ") lower than minimum of 2"
   144            << endl;
   143            << std::endl;
   145       return 1;
   144       return 1;
   146     }
   145     }
   147 
   146 
   148   cout << "Number of CNs: " << nCN << ", LAN nodes: " << nLANClients << endl;
   147   std::cout << "Number of CNs: " << nCN << ", LAN nodes: " << nLANClients << std::endl;
   149 
   148 
   150   Array2D<NodeContainer> nodes_net0(nCN, 3);
   149   Array2D<NodeContainer> nodes_net0(nCN, 3);
   151   Array2D<NodeContainer> nodes_net1(nCN, 6);
   150   Array2D<NodeContainer> nodes_net1(nCN, 6);
   152   NodeContainer* nodes_netLR = new NodeContainer[nCN];
   151   NodeContainer* nodes_netLR = new NodeContainer[nCN];
   153   Array2D<NodeContainer> nodes_net2(nCN, 14);
   152   Array2D<NodeContainer> nodes_net2(nCN, 14);
   188     }
   187     }
   189 
   188 
   190   // Create Campus Networks
   189   // Create Campus Networks
   191   for (int z = 0; z < nCN; ++z) 
   190   for (int z = 0; z < nCN; ++z) 
   192     {
   191     {
   193       cout << "Creating Campus Network " << z << ":" << endl;
   192       std::cout << "Creating Campus Network " << z << ":" << std::endl;
   194       // Create Net0
   193       // Create Net0
   195       cout << "  SubNet [ 0";
   194       std::cout << "  SubNet [ 0";
   196       for (int i = 0; i < 3; ++i) 
   195       for (int i = 0; i < 3; ++i) 
   197         {
   196         {
   198           nodes_net0[z][i].Create (1);
   197           nodes_net0[z][i].Create (1);
   199           stack.Install (nodes_net0[z][i]);
   198           stack.Install (nodes_net0[z][i]);
   200         }
   199         }
   205       for (int i = 0; i < 3; ++i) 
   204       for (int i = 0; i < 3; ++i) 
   206         {
   205         {
   207           ndc0[i] = p2p_1gb5ms.Install (nodes_net0[z][i]);
   206           ndc0[i] = p2p_1gb5ms.Install (nodes_net0[z][i]);
   208         }
   207         }
   209       // Create Net1
   208       // Create Net1
   210       cout << " 1";
   209       std::cout << " 1";
   211       for (int i = 0; i < 6; ++i) 
   210       for (int i = 0; i < 6; ++i) 
   212         {
   211         {
   213           nodes_net1[z][i].Create (1);
   212           nodes_net1[z][i].Create (1);
   214           stack.Install (nodes_net1[z][i]);
   213           stack.Install (nodes_net1[z][i]);
   215         }
   214         }
   236       oss.str ("");
   235       oss.str ("");
   237       oss << 10 + z << ".1.252.0";
   236       oss << 10 + z << ".1.252.0";
   238       address.SetBase (oss.str ().c_str (), "255.255.255.0");
   237       address.SetBase (oss.str ().c_str (), "255.255.255.0");
   239       ifs = address.Assign (ndc0_1);
   238       ifs = address.Assign (ndc0_1);
   240       // Create Net2
   239       // Create Net2
   241       cout << " 2";
   240       std::cout << " 2";
   242       for (int i = 0; i < 14; ++i) 
   241       for (int i = 0; i < 14; ++i) 
   243         {
   242         {
   244           nodes_net2[z][i].Create (1);
   243           nodes_net2[z][i].Create (1);
   245           stack.Install (nodes_net2[z][i]);
   244           stack.Install (nodes_net2[z][i]);
   246         }
   245         }
   278               ndc2LAN[i][j] = p2p_100mb1ms.Install (nodes_net2LAN[z][i][j]);
   277               ndc2LAN[i][j] = p2p_100mb1ms.Install (nodes_net2LAN[z][i][j]);
   279               ifs2LAN[z][i][j] = address.Assign (ndc2LAN[i][j]);
   278               ifs2LAN[z][i][j] = address.Assign (ndc2LAN[i][j]);
   280             }
   279             }
   281         }
   280         }
   282       // Create Net3
   281       // Create Net3
   283       cout << " 3 ]" << endl;
   282       std::cout << " 3 ]" << std::endl;
   284       for (int i = 0; i < 9; ++i) 
   283       for (int i = 0; i < 9; ++i) 
   285         {
   284         {
   286           nodes_net3[z][i].Create (1);
   285           nodes_net3[z][i].Create (1);
   287           stack.Install (nodes_net3[z][i]);
   286           stack.Install (nodes_net3[z][i]);
   288         }
   287         }
   314               nodes_net3LAN[z][i][j].Add (nodes_net3[z][i+4].Get (0));
   313               nodes_net3LAN[z][i][j].Add (nodes_net3[z][i+4].Get (0));
   315               ndc3LAN[i][j] = p2p_100mb1ms.Install (nodes_net3LAN[z][i][j]);
   314               ndc3LAN[i][j] = p2p_100mb1ms.Install (nodes_net3LAN[z][i][j]);
   316               ifs3LAN[z][i][j] = address.Assign (ndc3LAN[i][j]);
   315               ifs3LAN[z][i][j] = address.Assign (ndc3LAN[i][j]);
   317             }
   316             }
   318         }
   317         }
   319       cout << "  Connecting Subnets..." << endl;
   318       std::cout << "  Connecting Subnets..." << std::endl;
   320       // Create Lone Routers (Node 4 & 5) 
   319       // Create Lone Routers (Node 4 & 5) 
   321       nodes_netLR[z].Create (2);
   320       nodes_netLR[z].Create (2);
   322       stack.Install (nodes_netLR[z]);
   321       stack.Install (nodes_netLR[z]);
   323       NetDeviceContainer ndcLR;
   322       NetDeviceContainer ndcLR;
   324       ndcLR = p2p_1gb5ms.Install (nodes_netLR[z]);
   323       ndcLR = p2p_1gb5ms.Install (nodes_netLR[z]);
   366       oss.str ("");
   365       oss.str ("");
   367       oss << 10 + z << ".5.254.0";
   366       oss << 10 + z << ".5.254.0";
   368       address.SetBase (oss.str ().c_str (), "255.255.255.0");
   367       address.SetBase (oss.str ().c_str (), "255.255.255.0");
   369       ifs = address.Assign (ndc3_5b);
   368       ifs = address.Assign (ndc3_5b);
   370       // Assign IP addresses
   369       // Assign IP addresses
   371       cout << "  Assigning IP addresses..." << endl;
   370       std::cout << "  Assigning IP addresses..." << std::endl;
   372       for (int i = 0; i < 3; ++i) 
   371       for (int i = 0; i < 3; ++i) 
   373         {
   372         {
   374           oss.str ("");
   373           oss.str ("");
   375           oss << 10 + z << ".1." << 1 + i << ".0";
   374           oss << 10 + z << ".1." << 1 + i << ".0";
   376           address.SetBase (oss.str ().c_str (), "255.255.255.0");
   375           address.SetBase (oss.str ().c_str (), "255.255.255.0");
   407         }
   406         }
   408     }
   407     }
   409   // Create Ring Links
   408   // Create Ring Links
   410   if (nCN > 1) 
   409   if (nCN > 1) 
   411     {
   410     {
   412       cout << "Forming Ring Topology..." << endl;
   411       std::cout << "Forming Ring Topology..." << std::endl;
   413       NodeContainer* nodes_ring = new NodeContainer[nCN];
   412       NodeContainer* nodes_ring = new NodeContainer[nCN];
   414       for (int z = 0; z < nCN-1; ++z) 
   413       for (int z = 0; z < nCN-1; ++z) 
   415         {
   414         {
   416           nodes_ring[z].Add (nodes_net0[z][0].Get (0));
   415           nodes_ring[z].Add (nodes_net0[z][0].Get (0));
   417           nodes_ring[z].Add (nodes_net0[z+1][0].Get (0));
   416           nodes_ring[z].Add (nodes_net0[z+1][0].Get (0));
   430       delete[] ndc_ring;
   429       delete[] ndc_ring;
   431       delete[] nodes_ring;
   430       delete[] nodes_ring;
   432     }
   431     }
   433 
   432 
   434   // Create Traffic Flows
   433   // Create Traffic Flows
   435   cout << "Creating TCP Traffic Flows:" << endl;
   434   std::cout << "Creating TCP Traffic Flows:" << std::endl;
   436   Config::SetDefault ("ns3::OnOffApplication::MaxBytes", UintegerValue (500000));
   435   Config::SetDefault ("ns3::OnOffApplication::MaxBytes", UintegerValue (500000));
   437   Config::SetDefault ("ns3::OnOffApplication::OnTime",
   436   Config::SetDefault ("ns3::OnOffApplication::OnTime",
   438                       StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
   437                       StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
   439   Config::SetDefault ("ns3::OnOffApplication::OffTime",
   438   Config::SetDefault ("ns3::OnOffApplication::OffTime",
   440                       StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
   439                       StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
   449       if (z == nCN - 1) 
   448       if (z == nCN - 1) 
   450         {
   449         {
   451           x = 0;
   450           x = 0;
   452         }
   451         }
   453       // Subnet 2 LANs
   452       // Subnet 2 LANs
   454       cout << "  Campus Network " << z << " Flows [ Net2 ";
   453       std::cout << "  Campus Network " << z << " Flows [ Net2 ";
   455       for (int i = 0; i < 7; ++i) 
   454       for (int i = 0; i < 7; ++i) 
   456         {
   455         {
   457           for (int j = 0; j < nLANClients; ++j) 
   456           for (int j = 0; j < nLANClients; ++j) 
   458             {
   457             {
   459               // Sinks
   458               // Sinks
   473               clientApp.Add (client.Install (nodes_net1[x][r1].Get (0)));
   472               clientApp.Add (client.Install (nodes_net1[x][r1].Get (0)));
   474               clientApp.Start (Seconds (r2));
   473               clientApp.Start (Seconds (r2));
   475             }
   474             }
   476         }
   475         }
   477       // Subnet 3 LANs
   476       // Subnet 3 LANs
   478       cout << "Net3 ]" << endl;
   477       std::cout << "Net3 ]" << std::endl;
   479       for (int i = 0; i < 5; ++i) 
   478       for (int i = 0; i < 5; ++i) 
   480         {
   479         {
   481           for (int j = 0; j < nLANClients; ++j) 
   480           for (int j = 0; j < nLANClients; ++j) 
   482             {
   481             {
   483               // Sinks
   482               // Sinks
   498               clientApp.Start (Seconds (r2));
   497               clientApp.Start (Seconds (r2));
   499             }
   498             }
   500         }
   499         }
   501     }
   500     }
   502 
   501 
   503   cout << "Created " << NodeList::GetNNodes () << " nodes." << endl;
   502   std::cout << "Created " << NodeList::GetNNodes () << " nodes." << std::endl;
   504   TIMER_TYPE routingStart;
   503   TIMER_TYPE routingStart;
   505   TIMER_NOW (routingStart);
   504   TIMER_NOW (routingStart);
   506 
   505 
   507   if (nix)
   506   if (nix)
   508     {
   507     {
   509       // Calculate routing tables
   508       // Calculate routing tables
   510       cout << "Using Nix-vectors..." << endl;
   509       std::cout << "Using Nix-vectors..." << std::endl;
   511     }
   510     }
   512   else
   511   else
   513     {
   512     {
   514       // Calculate routing tables
   513       // Calculate routing tables
   515       cout << "Populating Global Static Routing Tables..." << endl;
   514       std::cout << "Populating Global Static Routing Tables..." << std::endl;
   516       Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
   515       Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
   517     }
   516     }
   518 
   517 
   519   TIMER_TYPE routingEnd;
   518   TIMER_TYPE routingEnd;
   520   TIMER_NOW (routingEnd);
   519   TIMER_NOW (routingEnd);
   521   cout << "Routing tables population took " 
   520   std::cout << "Routing tables population took " 
   522        << TIMER_DIFF (routingEnd, routingStart) << endl;
   521        << TIMER_DIFF (routingEnd, routingStart) << std::endl;
   523 
   522 
   524   Simulator::ScheduleNow (Progress);
   523   Simulator::ScheduleNow (Progress);
   525   cout << "Running simulator..." << endl;
   524   std::cout << "Running simulator..." << std::endl;
   526   TIMER_NOW (t1);
   525   TIMER_NOW (t1);
   527   Simulator::Stop (Seconds (100.0));
   526   Simulator::Stop (Seconds (100.0));
   528   Simulator::Run ();
   527   Simulator::Run ();
   529   TIMER_NOW (t2);
   528   TIMER_NOW (t2);
   530   cout << "Simulator finished." << endl;
   529   std::cout << "Simulator finished." << std::endl;
   531   Simulator::Destroy ();
   530   Simulator::Destroy ();
   532 
   531 
   533   double d1 = TIMER_DIFF (t1, t0), d2 = TIMER_DIFF (t2, t1);
   532   double d1 = TIMER_DIFF (t1, t0), d2 = TIMER_DIFF (t2, t1);
   534   cout << "-----" << endl << "Runtime Stats:" << endl;
   533   std::cout << "-----" << std::endl << "Runtime Stats:" << std::endl;
   535   cout << "Simulator init time: " << d1 << endl;
   534   std::cout << "Simulator init time: " << d1 << std::endl;
   536   cout << "Simulator run time: " << d2 << endl;
   535   std::cout << "Simulator run time: " << d2 << std::endl;
   537   cout << "Total elapsed time: " << d1+d2 << endl;
   536   std::cout << "Total elapsed time: " << d1+d2 << std::endl;
   538 
   537 
   539   delete[] nodes_netLR;
   538   delete[] nodes_netLR;
   540   return 0;
   539   return 0;
   541 }
   540 }