LEFT | RIGHT |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2011 Universita' di Firenze, Italy | 3 * Copyright (c) 2013 Universita' di Firenze, Italy |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
(...skipping 14 matching lines...) Expand all Loading... |
28 // | IPv6 | | IPv6 | IPv6 | | IPv6 | | 28 // | IPv6 | | IPv6 | IPv6 | | IPv6 | |
29 // +---------+ +---------+ | | | | 29 // +---------+ +---------+ | | | |
30 // | 6LoWPAN | | 6LoWPAN | | | | | 30 // | 6LoWPAN | | 6LoWPAN | | | | |
31 // +---------+ +---------+--------+ +--------+ | 31 // +---------+ +---------+--------+ +--------+ |
32 // | CSMA | | CSMA | CSMA | | CSMA | | 32 // | CSMA | | CSMA | CSMA | | CSMA | |
33 // +---------+ +---------+--------+ +--------+ | 33 // +---------+ +---------+--------+ +--------+ |
34 // | | | | | 34 // | | | | |
35 // ================ ================= | 35 // ================ ================= |
36 // | 36 // |
37 // - Tracing of queues and packet receptions to file "example-sixlowpan.tr" | 37 // - Tracing of queues and packet receptions to file "example-sixlowpan.tr" |
| 38 // Note that the Pcap packet dissection will not be very meaningful. |
| 39 // See the module's documentation for a discussion about this. |
38 | 40 |
39 #include <fstream> | 41 #include <fstream> |
40 #include "ns3/core-module.h" | 42 #include "ns3/core-module.h" |
41 #include "ns3/internet-module.h" | 43 #include "ns3/internet-module.h" |
42 #include "ns3/csma-module.h" | 44 #include "ns3/csma-module.h" |
43 #include "ns3/applications-module.h" | 45 #include "ns3/applications-module.h" |
44 #include "ns3/ipv6-static-routing-helper.h" | 46 #include "ns3/ipv6-static-routing-helper.h" |
45 | 47 |
46 #include "ns3/ipv6-routing-table-entry.h" | 48 #include "ns3/ipv6-routing-table-entry.h" |
47 #include "ns3/sixlowpan-module.h" | 49 #include "ns3/sixlowpan-module.h" |
48 | 50 |
49 using namespace ns3; | 51 using namespace ns3; |
50 | 52 |
51 NS_LOG_COMPONENT_DEFINE ("ExampleSixlowpan"); | 53 NS_LOG_COMPONENT_DEFINE ("ExampleSixlowpan") |
52 | 54 ; |
53 /** | |
54 * \class StackHelper | |
55 * \brief Helper to set or get some IPv6 information about nodes. | |
56 */ | |
57 class StackHelper | |
58 { | |
59 public: | |
60 /** | |
61 * \brief Add an address to a IPv6 node. | |
62 * \param n node | |
63 * \param interface interface index | |
64 * \param address IPv6 address to add | |
65 */ | |
66 inline void AddAddress (Ptr<Node>& n, uint32_t interface, Ipv6Address address) | |
67 { | |
68 Ptr<Ipv6> ipv6 = n->GetObject<Ipv6> (); | |
69 ipv6->AddAddress (interface, address); | |
70 } | |
71 | |
72 /** | |
73 * \brief Print the routing table. | |
74 * \param n the node | |
75 */ | |
76 inline void PrintRoutingTable (Ptr<Node>& n) | |
77 { | |
78 Ptr<Ipv6StaticRouting> routing = 0; | |
79 Ipv6StaticRoutingHelper routingHelper; | |
80 Ptr<Ipv6> ipv6 = n->GetObject<Ipv6> (); | |
81 uint32_t nbRoutes = 0; | |
82 Ipv6RoutingTableEntry route; | |
83 | |
84 routing = routingHelper.GetStaticRouting (ipv6); | |
85 | |
86 std::cout << "Routing table of " << n << " : " << std::endl; | |
87 std::cout << "Destination\t\t\t\t" << "Gateway\t\t\t\t\t" << "Interface\t" <
< "Prefix to use" << std::endl; | |
88 | |
89 nbRoutes = routing->GetNRoutes (); | |
90 for (uint32_t i = 0; i < nbRoutes; i++) | |
91 { | |
92 route = routing->GetRoute (i); | |
93 std::cout << route.GetDest () << "\t" | |
94 << route.GetGateway () << "\t" | |
95 << route.GetInterface () << "\t" | |
96 << route.GetPrefixToUse () << "\t" | |
97 << std::endl; | |
98 } | |
99 } | |
100 }; | |
101 | 55 |
102 int main (int argc, char** argv) | 56 int main (int argc, char** argv) |
103 { | 57 { |
104 #if 0 | 58 bool verbose = false; |
105 LogComponentEnable ("Ipv6L3Protocol", LOG_LEVEL_ALL); | |
106 LogComponentEnable ("Icmpv6L4Protocol", LOG_LEVEL_ALL); | |
107 LogComponentEnable ("Ipv6StaticRouting", LOG_LEVEL_ALL); | |
108 LogComponentEnable ("Ipv6Interface", LOG_LEVEL_ALL); | |
109 LogComponentEnable ("Ping6Application", LOG_LEVEL_ALL); | |
110 LogComponentEnable ("SixLowPanNetDevice", LOG_LEVEL_ALL); | |
111 #endif | |
112 | 59 |
113 CommandLine cmd; | 60 CommandLine cmd; |
| 61 cmd.AddValue ("verbose", "turn on some relevant log components", verbose); |
114 cmd.Parse (argc, argv); | 62 cmd.Parse (argc, argv); |
| 63 |
| 64 if (verbose) |
| 65 { |
| 66 LogComponentEnable ("SixLowPanNetDevice", LOG_LEVEL_ALL); |
| 67 LogComponentEnable ("Ping6Application", LOG_LEVEL_ALL); |
| 68 } |
115 | 69 |
116 Packet::EnablePrinting (); | 70 Packet::EnablePrinting (); |
117 Packet::EnableChecking (); | 71 Packet::EnableChecking (); |
118 | |
119 StackHelper stackHelper; | |
120 | 72 |
121 NS_LOG_INFO ("Create nodes."); | 73 NS_LOG_INFO ("Create nodes."); |
122 Ptr<Node> n0 = CreateObject<Node> (); | 74 Ptr<Node> n0 = CreateObject<Node> (); |
123 Ptr<Node> r = CreateObject<Node> (); | 75 Ptr<Node> r = CreateObject<Node> (); |
124 Ptr<Node> n1 = CreateObject<Node> (); | 76 Ptr<Node> n1 = CreateObject<Node> (); |
125 | 77 |
126 | 78 |
127 NodeContainer net1 (n0, r); | 79 NodeContainer net1 (n0, r); |
128 NodeContainer net2 (r, n1); | 80 NodeContainer net2 (r, n1); |
129 NodeContainer all (n0, r, n1); | 81 NodeContainer all (n0, r, n1); |
130 | 82 |
131 NS_LOG_INFO ("Create IPv6 Internet Stack"); | 83 NS_LOG_INFO ("Create IPv6 Internet Stack"); |
132 InternetStackHelper internetv6; | 84 InternetStackHelper internetv6; |
133 internetv6.Install (all); | 85 internetv6.Install (all); |
134 | 86 |
135 NS_LOG_INFO ("Create channels."); | 87 NS_LOG_INFO ("Create channels."); |
136 CsmaHelper csma; | 88 CsmaHelper csma; |
137 csma.SetChannelAttribute ("DataRate", DataRateValue (5000000)); | 89 csma.SetChannelAttribute ("DataRate", DataRateValue (5000000)); |
138 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); | 90 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); |
| 91 NetDeviceContainer d2 = csma.Install (net2); |
| 92 csma.SetDeviceAttribute ("Mtu", UintegerValue (150)); |
139 NetDeviceContainer d1 = csma.Install (net1); | 93 NetDeviceContainer d1 = csma.Install (net1); |
140 NetDeviceContainer d2 = csma.Install (net2); | |
141 | 94 |
142 SixLowPanHelper sixlowpan; | 95 SixLowPanHelper sixlowpan; |
143 sixlowpan.SetDeviceAttribute ("ForceEtherType", BooleanValue (true) ); | 96 sixlowpan.SetDeviceAttribute ("ForceEtherType", BooleanValue (true) ); |
144 NetDeviceContainer six1 = sixlowpan.Install (d1); | 97 NetDeviceContainer six1 = sixlowpan.Install (d1); |
145 | 98 |
146 NS_LOG_INFO ("Create networks and assign IPv6 Addresses."); | 99 NS_LOG_INFO ("Create networks and assign IPv6 Addresses."); |
147 Ipv6AddressHelper ipv6; | 100 Ipv6AddressHelper ipv6; |
148 ipv6.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64)); | 101 ipv6.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64)); |
149 Ipv6InterfaceContainer i1 = ipv6.Assign (six1); | 102 Ipv6InterfaceContainer i1 = ipv6.Assign (six1); |
150 i1.SetRouter (1, true); | 103 i1.SetForwarding (1, true); |
| 104 i1.SetDefaultRouteInAllNodes (1); |
| 105 |
151 ipv6.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64)); | 106 ipv6.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64)); |
152 Ipv6InterfaceContainer i2 = ipv6.Assign (d2); | 107 Ipv6InterfaceContainer i2 = ipv6.Assign (d2); |
153 i2.SetRouter (0, true); | 108 i2.SetForwarding (0, true); |
154 | 109 i2.SetDefaultRouteInAllNodes (0); |
155 stackHelper.PrintRoutingTable (r); | |
156 stackHelper.PrintRoutingTable (n0); | |
157 stackHelper.PrintRoutingTable (n1); | |
158 | 110 |
159 /* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r
*/ | 111 /* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r
*/ |
160 uint32_t packetSize = 100; | 112 uint32_t packetSize = 200; |
161 uint32_t maxPacketCount = 50; | 113 uint32_t maxPacketCount = 50; |
162 Time interPacketInterval = Seconds (1.); | 114 Time interPacketInterval = Seconds (1.); |
163 Ping6Helper ping6; | 115 Ping6Helper ping6; |
164 | 116 |
165 ping6.SetLocal (i1.GetAddress (0, 1)); | 117 ping6.SetLocal (i1.GetAddress (0, 1)); |
166 ping6.SetRemote (i2.GetAddress (1, 1)); | 118 ping6.SetRemote (i2.GetAddress (1, 1)); |
167 | 119 |
168 ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); | 120 ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); |
169 ping6.SetAttribute ("Interval", TimeValue (interPacketInterval)); | 121 ping6.SetAttribute ("Interval", TimeValue (interPacketInterval)); |
170 ping6.SetAttribute ("PacketSize", UintegerValue (packetSize)); | 122 ping6.SetAttribute ("PacketSize", UintegerValue (packetSize)); |
171 ApplicationContainer apps = ping6.Install (net1.Get (0)); | 123 ApplicationContainer apps = ping6.Install (net1.Get (0)); |
172 | 124 |
173 apps.Start (Seconds (5.0)); | 125 apps.Start (Seconds (5.0)); |
174 apps.Stop (Seconds (15.0)); | 126 apps.Stop (Seconds (15.0)); |
175 | 127 |
176 | 128 |
177 AsciiTraceHelper ascii; | 129 AsciiTraceHelper ascii; |
178 csma.EnableAsciiAll (ascii.CreateFileStream ("example-sixlowpan.tr")); | 130 csma.EnableAsciiAll (ascii.CreateFileStream ("example-sixlowpan.tr")); |
179 csma.EnablePcapAll (std::string ("example-sixlowpan"), true); | 131 csma.EnablePcapAll (std::string ("example-sixlowpan"), true); |
180 | 132 |
181 Simulator::Stop (Seconds (100)); | 133 Simulator::Stop (Seconds (100)); |
182 NS_LOG_INFO ("Run Simulation."); | 134 NS_LOG_INFO ("Run Simulation."); |
183 Simulator::Run (); | 135 Simulator::Run (); |
184 Simulator::Destroy (); | 136 Simulator::Destroy (); |
185 NS_LOG_INFO ("Done."); | 137 NS_LOG_INFO ("Done."); |
186 } | 138 } |
187 | 139 |
LEFT | RIGHT |