Alexander Afanasyev | 5b43386 | 2011-10-31 15:25:07 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 UCLA |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "ns3/network-module.h" |
| 22 | #include "ns3/core-module.h" |
| 23 | #include "ns3/internet-module.h" |
| 24 | #include "ns3/ipv4-global-routing-helper.h" |
| 25 | #include "ns3/point-to-point-grid.h" |
| 26 | #include "ns3/ipv4-global-routing-helper.h" |
Alexander Afanasyev | beceb36 | 2011-11-02 16:54:12 -0700 | [diff] [blame] | 27 | #include "ns3/applications-module.h" |
Alexander Afanasyev | 5b43386 | 2011-10-31 15:25:07 -0700 | [diff] [blame] | 28 | |
| 29 | using namespace ns3; |
| 30 | |
| 31 | NS_LOG_COMPONENT_DEFINE ("SimpleRouting"); |
| 32 | |
| 33 | // Parameters |
| 34 | uint32_t nNodes = 2; |
| 35 | uint32_t stopTime = 60; |
| 36 | |
| 37 | void TestDisable (Ptr<Node> node) |
| 38 | { |
| 39 | NS_LOG_FUNCTION (node->GetId ()); |
| 40 | |
| 41 | Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> (); |
| 42 | NS_ASSERT_MSG (ipv4 != 0, "ipv4 should not be null"); |
| 43 | |
| 44 | // The thing I didn't know is that interface status (isUp/isDown) and device status (isLinkUp) are two totally different things. It is possible to set interface up and down, but there is not an easy way to alter status of device. This similar to reality: it is possible to programmatically disable interface, but to actually disable a link one needs to physically cut the cable |
| 45 | ipv4->SetDown (2); |
| 46 | } |
| 47 | |
| 48 | int main (int argc, char *argv[]) |
| 49 | { |
| 50 | CommandLine cmd; |
| 51 | cmd.AddValue ("nNodes", "Number of Router nodes", nNodes); |
| 52 | cmd.AddValue ("stopTime", "Time to stop(seconds)", stopTime); |
| 53 | cmd.Parse (argc, argv); |
| 54 | |
| 55 | PointToPointHelper p2p; |
| 56 | InternetStackHelper stack; |
| 57 | |
Alexander Afanasyev | 3ba44e5 | 2011-11-10 16:38:10 -0800 | [diff] [blame] | 58 | // Ipv4GlobalRoutingHelper ipv4RoutingHelper ("ns3::Ipv4GlobalRoutingOrderedNexthops"); |
| 59 | Ipv4GlobalRoutingHelper ipv4RoutingHelper ("ns3::Ipv4GlobalRoutingUnorderedNexthops"); |
Alexander Afanasyev | 5b43386 | 2011-10-31 15:25:07 -0700 | [diff] [blame] | 60 | stack.SetRoutingHelper (ipv4RoutingHelper); |
| 61 | |
| 62 | PointToPointGridHelper grid (nNodes, nNodes, p2p); |
Alexander Afanasyev | 3875a4b | 2011-11-10 11:55:53 -0800 | [diff] [blame] | 63 | grid.BoundingBox(100,100,270,270); |
Alexander Afanasyev | 5b43386 | 2011-10-31 15:25:07 -0700 | [diff] [blame] | 64 | grid.InstallStack (stack); |
Alexander Afanasyev | 5b43386 | 2011-10-31 15:25:07 -0700 | [diff] [blame] | 65 | |
| 66 | grid.AssignIpv4Addresses ( |
| 67 | Ipv4AddressHelper("10.1.0.0", "255.255.255.0"), |
| 68 | Ipv4AddressHelper("10.2.0.0", "255.255.255.0") |
| 69 | ); |
| 70 | |
Alexander Afanasyev | 3875a4b | 2011-11-10 11:55:53 -0800 | [diff] [blame] | 71 | for (uint32_t i=0; i<nNodes; i++) |
| 72 | for (uint32_t j=0; j<nNodes; j++) |
| 73 | grid.GetNode (i,j)->GetObject<GlobalRouter> ()->InjectRoute (Ipv4Address(grid.GetNode (i,j)->GetId ()), |
| 74 | Ipv4Mask("255.255.255.255")); |
| 75 | |
Alexander Afanasyev | beceb36 | 2011-11-02 16:54:12 -0700 | [diff] [blame] | 76 | // // Create router nodes, initialize routing database and set up the routing |
| 77 | // // tables in the nodes. |
Alexander Afanasyev | 3875a4b | 2011-11-10 11:55:53 -0800 | [diff] [blame] | 78 | // Ipv4GlobalRoutingHelper::PopulateRoutingTables (); |
Alexander Afanasyev | 3ba44e5 | 2011-11-10 16:38:10 -0800 | [diff] [blame] | 79 | // Ipv4GlobalRoutingHelper::PopulateAllPossibleRoutingTables (); |
| 80 | Ipv4GlobalRoutingHelper::PopulateRandomRoutingTables (5); |
| 81 | |
Alexander Afanasyev | beceb36 | 2011-11-02 16:54:12 -0700 | [diff] [blame] | 82 | // testing ip routing |
| 83 | UdpEchoClientHelper client (Ipv4Address ("10.2.1.1"), 1029); |
| 84 | client.SetAttribute ("MaxPackets", UintegerValue (1)); |
| 85 | client.SetAttribute ("Interval", TimeValue (Seconds(1.0))); |
| 86 | client.SetAttribute ("PacketSize", UintegerValue (100)); |
| 87 | client.Install (grid.GetNode (0,0)); |
| 88 | // bla |
Alexander Afanasyev | 3875a4b | 2011-11-10 11:55:53 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | beceb36 | 2011-11-02 16:54:12 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | 5b43386 | 2011-10-31 15:25:07 -0700 | [diff] [blame] | 91 | // apps.Stop (Seconds(150.0)); |
| 92 | |
| 93 | Simulator::ScheduleWithContext (grid.GetNode (0,0)->GetId (), |
| 94 | Seconds (80.0), TestDisable, grid.GetNode (0,0)); |
| 95 | |
| 96 | // Trace routing tables |
| 97 | Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("routes.log", std::ios::out); |
Alexander Afanasyev | beceb36 | 2011-11-02 16:54:12 -0700 | [diff] [blame] | 98 | *routingStream->GetStream () << "Node (1,1)\n"; |
| 99 | ipv4RoutingHelper.PrintRoutingTableAt (Seconds (20), grid.GetNode (1,1), routingStream); |
Alexander Afanasyev | 5b43386 | 2011-10-31 15:25:07 -0700 | [diff] [blame] | 100 | |
| 101 | Simulator::Stop (Seconds(160.0)); |
| 102 | Simulator::Run (); |
| 103 | Simulator::Destroy (); |
| 104 | |
| 105 | NS_LOG_INFO ("End of experiment"); |
| 106 | |
| 107 | return 0; |
| 108 | } |