A Discrete-Event Network Simulator
API
csma-system-test-suite.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation;
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17// This is not a test of CsmaNetDevice model behavior per-se, but
18// instead is a roll up of several end-to-end examples in examples/csma
19// directory, converted into system tests. Writing a test suite
20// to test Csma itself is for further study.
21
22#include <string>
23
24#include "ns3/address.h"
25#include "ns3/application-container.h"
26#include "ns3/bridge-helper.h"
27#include "ns3/callback.h"
28#include "ns3/config.h"
29#include "ns3/csma-helper.h"
30#include "ns3/csma-star-helper.h"
31#include "ns3/inet-socket-address.h"
32#include "ns3/internet-stack-helper.h"
33#include "ns3/ipv4-address-helper.h"
34#include "ns3/ipv4-global-routing-helper.h"
35#include "ns3/ipv4-static-routing-helper.h"
36#include "ns3/node.h"
37#include "ns3/data-rate.h"
38#include "ns3/node-container.h"
39#include "ns3/on-off-helper.h"
40#include "ns3/packet.h"
41#include "ns3/packet-sink-helper.h"
42#include "ns3/packet-socket-helper.h"
43#include "ns3/packet-socket-address.h"
44#include "ns3/pointer.h"
45#include "ns3/simple-channel.h"
46#include "ns3/simulator.h"
47#include "ns3/string.h"
48#include "ns3/test.h"
49#include "ns3/uinteger.h"
50#include "ns3/v4ping-helper.h"
51
52using namespace ns3;
53
60{
61public:
63 virtual ~CsmaBridgeTestCase ();
64
65private:
66 virtual void DoRun (void);
67
73 void SinkRx (Ptr<const Packet> p, const Address &ad);
75};
76
77// Add some help text to this case to describe what it is intended to test
79 : TestCase ("Bridge example for Carrier Sense Multiple Access (CSMA) networks"), m_count (0)
80{
81}
82
84{
85}
86
87void
89{
90 m_count++;
91}
92
93// Network topology
94//
95// n0 n1
96// | |
97// ----------
98// | Switch |
99// ----------
100// | |
101// n2 n3
102//
103// - CBR/UDP test flow from n0 to n1; test that packets received on n1
104//
105void
107{
108 NodeContainer terminals;
109 terminals.Create (4);
110
111 NodeContainer csmaSwitch;
112 csmaSwitch.Create (1);
113
115 csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
116 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
117
118 NetDeviceContainer terminalDevices;
119 NetDeviceContainer switchDevices;
120
121 for (int i = 0; i < 4; i++)
122 {
123 NetDeviceContainer link = csma.Install (NodeContainer (terminals.Get (i), csmaSwitch));
124 terminalDevices.Add (link.Get (0));
125 switchDevices.Add (link.Get (1));
126 }
127
128 // Create the bridge netdevice, which will do the packet switching
129 Ptr<Node> switchNode = csmaSwitch.Get (0);
130 BridgeHelper bridge;
131 bridge.Install (switchNode, switchDevices);
132
133 InternetStackHelper internet;
134 internet.Install (terminals);
135
137 ipv4.SetBase ("10.1.1.0", "255.255.255.0");
138 ipv4.Assign (terminalDevices);
139
140 uint16_t port = 9; // Discard port (RFC 863)
141
142 // Create the OnOff application to send UDP datagrams from n0 to n1.
143 //
144 // Make packets be sent about every DefaultPacketSize / DataRate =
145 // 4096 bits / (5000 bits/second) = 0.82 second.
146 OnOffHelper onoff ("ns3::UdpSocketFactory",
147 Address (InetSocketAddress (Ipv4Address ("10.1.1.2"), port)));
148 onoff.SetConstantRate (DataRate (5000));
149
150 ApplicationContainer app = onoff.Install (terminals.Get (0));
151 app.Start (Seconds (1.0));
152 app.Stop (Seconds (10.0));
153
154 PacketSinkHelper sink ("ns3::UdpSocketFactory",
155 Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
156 app = sink.Install (terminals.Get (1));
157 app.Start (Seconds (0.0));
158
159 // Trace receptions
160 Config::ConnectWithoutContext ("/NodeList/1/ApplicationList/0/$ns3::PacketSink/Rx", MakeCallback (&CsmaBridgeTestCase::SinkRx, this));
161
162 Simulator::Run ();
163 Simulator::Destroy ();
164
165 // We should have sent and received 10 packets
166 NS_TEST_ASSERT_MSG_EQ (m_count, 10, "Bridge should have passed 10 packets");
167}
168
175{
176public:
178 virtual ~CsmaBroadcastTestCase ();
179
180private:
181 virtual void DoRun (void);
182
189 void SinkRxNode1 (Ptr<const Packet> p, const Address &ad);
190 void SinkRxNode2 (Ptr<const Packet> p, const Address &ad);
198
202};
203
204// Add some help text to this case to describe what it is intended to test
206 : TestCase ("Broadcast example for Carrier Sense Multiple Access (CSMA) networks"), m_countNode1 (0), m_countNode2 (0), m_drops (0)
207{
208}
209
211{
212}
213
214void
216{
217 m_countNode1++;
218}
219
220void
222{
223 m_countNode2++;
224}
225
226void
228{
229 m_drops++;
230}
231
232//
233// Example of the sending of a datagram to a broadcast address
234//
235// Network topology
236// ==============
237// | |
238// n0 n1 n2
239// | |
240// ==========
241//
242// n0 originates UDP broadcast to 255.255.255.255/discard port, which
243// is replicated and received on both n1 and n2
244//
245void
247{
249 c.Create (3);
250 NodeContainer c0 = NodeContainer (c.Get (0), c.Get (1));
251 NodeContainer c1 = NodeContainer (c.Get (0), c.Get (2));
252
254 csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
255 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
256
257 NetDeviceContainer n0 = csma.Install (c0);
258 NetDeviceContainer n1 = csma.Install (c1);
259
260 InternetStackHelper internet;
261 internet.Install (c);
262
264 ipv4.SetBase ("10.1.0.0", "255.255.255.0");
265 ipv4.Assign (n0);
266 ipv4.SetBase ("192.168.1.0", "255.255.255.0");
267 ipv4.Assign (n1);
268
269
270 // RFC 863 discard port ("9") indicates packet should be thrown away
271 // by the system. We allow this silent discard to be overridden
272 // by the PacketSink application.
273 uint16_t port = 9;
274
275 // Create the OnOff application to send UDP datagrams from n0.
276 //
277 // Make packets be sent about every DefaultPacketSize / DataRate =
278 // 4096 bits / (5000 bits/second) = 0.82 second.
279 OnOffHelper onoff ("ns3::UdpSocketFactory",
280 Address (InetSocketAddress (Ipv4Address ("255.255.255.255"), port)));
281 onoff.SetConstantRate (DataRate (5000));
282
283 ApplicationContainer app = onoff.Install (c0.Get (0));
284 // Start the application
285 app.Start (Seconds (1.0));
286 app.Stop (Seconds (10.0));
287
288 // Create an optional packet sink to receive these packets
289 PacketSinkHelper sink ("ns3::UdpSocketFactory",
290 Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
291 app = sink.Install (c0.Get (1));
292 app.Add (sink.Install (c1.Get (1)));
293 app.Start (Seconds (1.0));
294 app.Stop (Seconds (10.0));
295
296 // Trace receptions
297 Config::ConnectWithoutContext ("/NodeList/1/ApplicationList/0/$ns3::PacketSink/Rx", MakeCallback (&CsmaBroadcastTestCase::SinkRxNode1, this));
298 Config::ConnectWithoutContext ("/NodeList/2/ApplicationList/0/$ns3::PacketSink/Rx", MakeCallback (&CsmaBroadcastTestCase::SinkRxNode2, this));
299
300 Simulator::Run ();
301 Simulator::Destroy ();
302
303 // We should have sent and received 10 packets
304 NS_TEST_ASSERT_MSG_EQ (m_countNode1, 10, "Node 1 should have received 10 packets");
305 NS_TEST_ASSERT_MSG_EQ (m_countNode2, 10, "Node 2 should have received 10 packets");
306}
307
314{
315public:
317 virtual ~CsmaMulticastTestCase ();
318
319private:
320 virtual void DoRun (void);
321
327 void SinkRx (Ptr<const Packet> p, const Address &ad);
328
334
337};
338
339// Add some help text to this case to describe what it is intended to test
341 : TestCase ("Multicast example for Carrier Sense Multiple Access (CSMA) networks"), m_count (0), m_drops (0)
342{
343}
344
346{
347}
348
349void
351{
352 m_count++;
353}
354
355void
357{
358 m_drops++;
359}
360
361// Network topology
362//
363// Lan1
364// ===========
365// | | |
366// n0 n1 n2 n3 n4
367// | | |
368// ===========
369// Lan0
370//
371// - Multicast source is at node n0;
372// - Multicast forwarded by node n2 onto LAN1;
373// - Nodes n0, n1, n2, n3, and n4 receive the multicast frame.
374// - Node n4 listens for the data
375//
376void
378{
379 //
380 // Set up default values for the simulation.
381 //
382 // Select DIX/Ethernet II-style encapsulation (no LLC/Snap header)
383 Config::SetDefault ("ns3::CsmaNetDevice::EncapsulationMode", StringValue ("Dix"));
384
386 c.Create (5);
387 // We will later want two subcontainers of these nodes, for the two LANs
388 NodeContainer c0 = NodeContainer (c.Get (0), c.Get (1), c.Get (2));
389 NodeContainer c1 = NodeContainer (c.Get (2), c.Get (3), c.Get (4));
390
392 csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
393 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
394
395 // We will use these NetDevice containers later, for IP addressing
396 NetDeviceContainer nd0 = csma.Install (c0); // First LAN
397 NetDeviceContainer nd1 = csma.Install (c1); // Second LAN
398
399 InternetStackHelper internet;
400 internet.Install (c);
401
402 Ipv4AddressHelper ipv4Addr;
403 ipv4Addr.SetBase ("10.1.1.0", "255.255.255.0");
404 ipv4Addr.Assign (nd0);
405 ipv4Addr.SetBase ("10.1.2.0", "255.255.255.0");
406 ipv4Addr.Assign (nd1);
407
408 //
409 // Now we can configure multicasting. As described above, the multicast
410 // source is at node zero, which we assigned the IP address of 10.1.1.1
411 // earlier. We need to define a multicast group to send packets to. This
412 // can be any multicast address from 224.0.0.0 through 239.255.255.255
413 // (avoiding the reserved routing protocol addresses).
414 //
415
416 Ipv4Address multicastSource ("10.1.1.1");
417 Ipv4Address multicastGroup ("225.1.2.4");
418
419 // Now, we will set up multicast routing. We need to do three things:
420 // 1) Configure a (static) multicast route on node n2
421 // 2) Set up a default multicast route on the sender n0
422 // 3) Have node n4 join the multicast group
423 // We have a helper that can help us with static multicast
424 Ipv4StaticRoutingHelper multicast;
425
426 // 1) Configure a (static) multicast route on node n2 (multicastRouter)
427 Ptr<Node> multicastRouter = c.Get (2); // The node in question
428 Ptr<NetDevice> inputIf = nd0.Get (2); // The input NetDevice
429 NetDeviceContainer outputDevices; // A container of output NetDevices
430 outputDevices.Add (nd1.Get (0)); // (we only need one NetDevice here)
431
432 multicast.AddMulticastRoute (multicastRouter, multicastSource,
433 multicastGroup, inputIf, outputDevices);
434
435 // 2) Set up a default multicast route on the sender n0
436 Ptr<Node> sender = c.Get (0);
437 Ptr<NetDevice> senderIf = nd0.Get (0);
438 multicast.SetDefaultMulticastRoute (sender, senderIf);
439
440 //
441 // Create an OnOff application to send UDP datagrams from node zero to the
442 // multicast group (node four will be listening).
443 //
444
445 uint16_t multicastPort = 9; // Discard port (RFC 863)
446
447 // Configure a multicast packet generator.
448 //
449 // Make packets be sent about every defaultPacketSize / dataRate =
450 // 4096 bits / (5000 bits/second) = 0.82 second.
451 OnOffHelper onoff ("ns3::UdpSocketFactory",
452 Address (InetSocketAddress (multicastGroup, multicastPort)));
453 onoff.SetConstantRate (DataRate (5000));
454
455 ApplicationContainer srcC = onoff.Install (c0.Get (0));
456
457 //
458 // Tell the application when to start and stop.
459 //
460 srcC.Start (Seconds (1.));
461 srcC.Stop (Seconds (10.));
462
463 // Create an optional packet sink to receive these packets
464 PacketSinkHelper sink ("ns3::UdpSocketFactory",
465 InetSocketAddress (Ipv4Address::GetAny (), multicastPort));
466
467 ApplicationContainer sinkC = sink.Install (c1.Get (2)); // Node n4
468 // Start the sink
469 sinkC.Start (Seconds (1.0));
470 sinkC.Stop (Seconds (10.0));
471
472 // Trace receptions
473 Config::ConnectWithoutContext ("/NodeList/4/ApplicationList/0/$ns3::PacketSink/Rx", MakeCallback (&CsmaMulticastTestCase::SinkRx, this));
474
475 //
476 // Now, do the actual simulation.
477 //
478 Simulator::Run ();
479 Simulator::Destroy ();
480
481 // We should have sent and received 10 packets
482 NS_TEST_ASSERT_MSG_EQ (m_count, 10, "Node 4 should have received 10 packets");
483}
484
491{
492public:
494 virtual ~CsmaOneSubnetTestCase ();
495
496private:
497 virtual void DoRun (void);
498
505 void SinkRxNode0 (Ptr<const Packet> p, const Address &ad);
506 void SinkRxNode1 (Ptr<const Packet> p, const Address &ad);
517};
518
519// Add some help text to this case to describe what it is intended to test
521 : TestCase ("One subnet example for Carrier Sense Multiple Access (CSMA) networks"), m_countNode0 (0), m_countNode1 (0), m_drops (0)
522{
523}
524
526{
527}
528
529void
531{
532 m_countNode0++;
533}
534
535void
537{
538 m_countNode1++;
539}
540
541void
543{
544 m_drops++;
545}
546
547// Network topology
548//
549// n0 n1 n2 n3
550// | | | |
551// =================
552// LAN
553//
554// - CBR/UDP flows from n0 to n1 and from n3 to n0
555// - DropTail queues
556//
557void
559{
561 nodes.Create (4);
562
564 csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
565 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
566 //
567 // Now fill out the topology by creating the net devices required to connect
568 // the nodes to the channels and hooking them up.
569 //
571
572 InternetStackHelper internet;
573 internet.Install (nodes);
574
575 // We've got the "hardware" in place. Now we need to add IP addresses.
576 //
578 ipv4.SetBase ("10.1.1.0", "255.255.255.0");
580
581 uint16_t port = 9; // Discard port (RFC 863)
582
583 //
584 // Create an OnOff application to send UDP datagrams from node zero
585 // to node 1.
586 //
587 // Make packets be sent about every defaultPacketSize / dataRate =
588 // 4096 bits / (5000 bits/second) = 0.82 second.
589 OnOffHelper onoff ("ns3::UdpSocketFactory",
590 Address (InetSocketAddress (interfaces.GetAddress (1), port)));
591 onoff.SetConstantRate (DataRate (5000));
592
593 ApplicationContainer app = onoff.Install (nodes.Get (0));
594 // Start the application
595 app.Start (Seconds (1.0));
596 app.Stop (Seconds (10.0));
597
598 // Create an optional packet sink to receive these packets
599 PacketSinkHelper sink ("ns3::UdpSocketFactory",
600 Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
601 app = sink.Install (nodes.Get (1));
602 app.Start (Seconds (0.0));
603
604 //
605 // Create a similar flow from n3 to n0, starting at time 1.1 seconds
606 //
607 onoff.SetAttribute ("Remote",
608 AddressValue (InetSocketAddress (interfaces.GetAddress (0), port)));
609 app = onoff.Install (nodes.Get (3));
610 app.Start (Seconds (1.1));
611 app.Stop (Seconds (10.0));
612
613 app = sink.Install (nodes.Get (0));
614 app.Start (Seconds (0.0));
615
616 // Trace receptions
617 Config::ConnectWithoutContext ("/NodeList/0/ApplicationList/1/$ns3::PacketSink/Rx", MakeCallback (&CsmaOneSubnetTestCase::SinkRxNode0, this));
618 Config::ConnectWithoutContext ("/NodeList/1/ApplicationList/0/$ns3::PacketSink/Rx", MakeCallback (&CsmaOneSubnetTestCase::SinkRxNode1, this));
619
620 //
621 // Now, do the actual simulation.
622 //
623 Simulator::Run ();
624 Simulator::Destroy ();
625
626 // We should have sent and received 10 packets
627 NS_TEST_ASSERT_MSG_EQ (m_countNode0, 10, "Node 0 should have received 10 packets");
628 NS_TEST_ASSERT_MSG_EQ (m_countNode1, 10, "Node 1 should have received 10 packets");
629}
630
637{
638public:
640 virtual ~CsmaPacketSocketTestCase ();
641
642private:
643 virtual void DoRun (void);
650 void SinkRx (std::string path, Ptr<const Packet> p, const Address &ad);
651
657
660};
661
662// Add some help text to this case to describe what it is intended to test
664 : TestCase ("Packet socket example for Carrier Sense Multiple Access (CSMA) networks"), m_count (0), m_drops (0)
665{
666}
667
669{
670}
671
672void
674{
675 m_count++;
676}
677
678void
680{
681 m_drops++;
682}
683
684//
685// Network topology
686//
687// n0 n1 n2 n3
688// | | | |
689// =====================
690//
691// - Packet socket flow from n0 to n1 and from node n3 to n0
692// -- We will test reception at node n0
693// - Default 512 byte packets generated by traffic generator
694//
695void
697{
698 // Here, we will explicitly create four nodes.
700 nodes.Create (4);
701
702 PacketSocketHelper packetSocket;
703 packetSocket.Install (nodes);
704
705 // create the shared medium used by all csma devices.
706 Ptr<CsmaChannel> channel = CreateObjectWithAttributes<CsmaChannel> (
707 "DataRate", DataRateValue (DataRate (5000000)),
708 "Delay", TimeValue (MilliSeconds (2)));
709
710 // use a helper function to connect our nodes to the shared channel.
712 csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
713 NetDeviceContainer devs = csma.Install (nodes, channel);
714
715 // Create the OnOff application to send raw datagrams
716 //
717 // Make packets be sent about every DefaultPacketSize / DataRate =
718 // 4096 bits / (5000 bits/second) = 0.82 second.
719 PacketSocketAddress socket;
720 socket.SetSingleDevice (devs.Get (0)->GetIfIndex ());
721 socket.SetPhysicalAddress (devs.Get (1)->GetAddress ());
722 socket.SetProtocol (2);
723 OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
724 onoff.SetConstantRate (DataRate (5000));
725 ApplicationContainer apps = onoff.Install (nodes.Get (0));
726 apps.Start (Seconds (1.0));
727 apps.Stop (Seconds (10.0));
728
729 socket.SetSingleDevice (devs.Get (3)->GetIfIndex ());
730 socket.SetPhysicalAddress (devs.Get (0)->GetAddress ());
731 socket.SetProtocol (3);
732 onoff.SetAttribute ("Remote", AddressValue (socket));
733 apps = onoff.Install (nodes.Get (3));
734 apps.Start (Seconds (1.0));
735 apps.Stop (Seconds (10.0));
736
737 PacketSinkHelper sink = PacketSinkHelper ("ns3::PacketSocketFactory",
738 socket);
739 apps = sink.Install (nodes.Get (0));
740 apps.Start (Seconds (0.0));
741 apps.Stop (Seconds (20.0));
742
743 // Trace receptions
744 Config::Connect ("/NodeList/0/ApplicationList/*/$ns3::PacketSink/Rx",
746
747 Simulator::Run ();
748 Simulator::Destroy ();
749
750 // We should have received 10 packets on node 0
751 NS_TEST_ASSERT_MSG_EQ (m_count, 10, "Node 0 should have received 10 packets");
752}
753
760{
761public:
763 virtual ~CsmaPingTestCase ();
764
765private:
766 virtual void DoRun (void);
772 void SinkRx (Ptr<const Packet> p, const Address &ad);
773
779 void PingRtt (std::string context, Time rtt);
780
786
790};
791
792// Add some help text to this case to describe what it is intended to test
794 : TestCase ("Ping example for Carrier Sense Multiple Access (CSMA) networks"), m_countSinkRx (0), m_countPingRtt (0), m_drops (0)
795{
796}
797
799{
800}
801
802void
804{
806}
807
808void
809CsmaPingTestCase::PingRtt (std::string context, Time rtt)
810{
812}
813
814void
816{
817 m_drops++;
818}
819
820// Network topology
821//
822// n0 n1 n2 n3
823// | | | |
824// =====================
825//
826// node n0,n1,n3 pings to node n2
827// node n0 generates protocol 2 (IGMP) to node n3
828//
829void
831{
832 // Here, we will explicitly create four nodes.
834 c.Create (4);
835
836 // connect all our nodes to a shared channel.
838 csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
839 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
840 csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
841 NetDeviceContainer devs = csma.Install (c);
842
843 // add an ip stack to all nodes.
844 InternetStackHelper ipStack;
845 ipStack.Install (c);
846
847 // assign ip addresses
849 ip.SetBase ("192.168.1.0", "255.255.255.0");
850 Ipv4InterfaceContainer addresses = ip.Assign (devs);
851
852 // Create the OnOff application to send UDP datagrams from n0 to n1.
853 //
854 // Make packets be sent about every DefaultPacketSize / DataRate =
855 // 4096 bits / (5000 bits/second) = 0.82 second.
856 Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
857 InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3));
858 OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
859 onoff.SetConstantRate (DataRate (5000));
860
861 ApplicationContainer apps = onoff.Install (c.Get (0));
862 apps.Start (Seconds (1.0));
863 apps.Stop (Seconds (10.0));
864
865 PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
866 apps = sink.Install (c.Get (3));
867 apps.Start (Seconds (0.0));
868 apps.Stop (Seconds (11.0));
869
870 V4PingHelper ping = V4PingHelper (addresses.GetAddress (2));
871 NodeContainer pingers;
872 pingers.Add (c.Get (0));
873 pingers.Add (c.Get (1));
874 pingers.Add (c.Get (3));
875 apps = ping.Install (pingers);
876 apps.Start (Seconds (2.0));
877 apps.Stop (Seconds (5.0));
878
879 // Trace receptions
880 Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
882
883 // Trace pings
884 Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt",
886
887 Simulator::Run ();
888 Simulator::Destroy ();
889
890 // We should have sent and received 10 packets
891 NS_TEST_ASSERT_MSG_EQ (m_countSinkRx, 10, "Node 3 should have received 10 packets");
892
893 // We should have 3 pingers that ping every second for 3 seconds.
894 NS_TEST_ASSERT_MSG_EQ (m_countPingRtt, 9, "Node 2 should have been pinged 9 times");
895}
896
903{
904public:
906 virtual ~CsmaRawIpSocketTestCase ();
907
908private:
909 virtual void DoRun (void);
910
916 void SinkRx (Ptr<const Packet> p, const Address &ad);
917
923
926};
927
928// Add some help text to this case to describe what it is intended to test
930 : TestCase ("Raw internet protocol socket example for Carrier Sense Multiple Access (CSMA) networks"), m_count (0), m_drops (0)
931{
932}
933
935{
936}
937
938void
940{
941 m_count++;
942}
943
944void
946{
947 m_drops++;
948}
949
950//
951// Network topology
952// (sender) (receiver)
953// n0 n1 n2 n3
954// | | | |
955// =====================
956//
957// Node n0 sends data to node n3 over a raw IP socket. The protocol
958// number used is 2.
959//
960void
962{
963 // Here, we will explicitly create four nodes.
965 c.Create (4);
966
967 // connect all our nodes to a shared channel.
969 csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
970 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
971 csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
972 NetDeviceContainer devs = csma.Install (c);
973
974 // add an ip stack to all nodes.
975 InternetStackHelper ipStack;
976 ipStack.Install (c);
977
978 // assign ip addresses
980 ip.SetBase ("192.168.1.0", "255.255.255.0");
981 Ipv4InterfaceContainer addresses = ip.Assign (devs);
982
983 // IP protocol configuration
984 //
985 // Make packets be sent about every DefaultPacketSize / DataRate =
986 // 4096 bits / (5000 bits/second) = 0.82 second.
987 Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
988 InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3));
989 OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
990 onoff.SetConstantRate (DataRate (5000));
991
992 ApplicationContainer apps = onoff.Install (c.Get (0));
993 apps.Start (Seconds (1.0));
994 apps.Stop (Seconds (10.0));
995
996 PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
997 apps = sink.Install (c.Get (3));
998 apps.Start (Seconds (0.0));
999 apps.Stop (Seconds (12.0));
1000
1001 // Trace receptions
1002 Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
1004
1005 Simulator::Run ();
1006 Simulator::Destroy ();
1007
1008 // We should have sent and received 10 packets
1009 NS_TEST_ASSERT_MSG_EQ (m_count, 10, "Node 3 should have received 10 packets");
1010}
1011
1018{
1019public:
1021 virtual ~CsmaStarTestCase ();
1022
1023private:
1024 virtual void DoRun (void);
1025
1031 void SinkRx (Ptr<const Packet> p, const Address &ad);
1032
1038
1041};
1042
1043// Add some help text to this case to describe what it is intended to test
1045 : TestCase ("Star example for Carrier Sense Multiple Access (CSMA) networks"), m_count (0), m_drops (0)
1046{
1047}
1048
1050{
1051}
1052
1053void
1055{
1056 m_count++;
1057}
1058
1059void
1061{
1062 m_drops++;
1063}
1064
1065// Network topology (default)
1066//
1067// n2 + + n3 .
1068// | ... |\ /| ... | .
1069// ======= \ / ======= .
1070// CSMA \ / CSMA .
1071// \ / .
1072// n1 +--- n0 ---+ n4 .
1073// | ... | / \ | ... | .
1074// ======= / \ ======= .
1075// CSMA / \ CSMA .
1076// / \ .
1077// n6 + + n5 .
1078// | ... | | ... | .
1079// ======= ======= .
1080// CSMA CSMA .
1081//
1082void
1084{
1085 //
1086 // Default number of nodes in the star.
1087 //
1088 uint32_t nSpokes = 7;
1089
1091 csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
1092 csma.SetChannelAttribute ("Delay", StringValue ("1ms"));
1093 CsmaStarHelper star (nSpokes, csma);
1094
1095 NodeContainer fillNodes;
1096
1097 //
1098 // Just to be nasy, hang some more nodes off of the CSMA channel for each
1099 // spoke, so that there are a total of 16 nodes on each channel. Stash
1100 // all of these new devices into a container.
1101 //
1102 NetDeviceContainer fillDevices;
1103
1104 uint32_t nFill = 14;
1105 for (uint32_t i = 0; i < star.GetSpokeDevices ().GetN (); ++i)
1106 {
1108 Ptr<CsmaChannel> csmaChannel = channel->GetObject<CsmaChannel> ();
1109 NodeContainer newNodes;
1110 newNodes.Create (nFill);
1111 fillNodes.Add (newNodes);
1112 fillDevices.Add (csma.Install (newNodes, csmaChannel));
1113 }
1114
1115 InternetStackHelper internet;
1116 star.InstallStack (internet);
1117 internet.Install (fillNodes);
1118
1119 star.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.0.0", "255.255.255.0"));
1120
1121 //
1122 // We assigned addresses to the logical hub and the first "drop" of the
1123 // CSMA network that acts as the spoke, but we also have a number of fill
1124 // devices (nFill) also hanging off the CSMA network. We have got to
1125 // assign addresses to them as well. We put all of the fill devices into
1126 // a single device container, so the first nFill devices are associated
1127 // with the channel connected to spokeDevices.Get (0), the second nFill
1128 // devices afe associated with the channel connected to spokeDevices.Get (1)
1129 // etc.
1130 //
1132 for(uint32_t i = 0; i < star.SpokeCount (); ++i)
1133 {
1134 std::ostringstream subnet;
1135 subnet << "10.1." << i << ".0";
1136 address.SetBase (subnet.str ().c_str (), "255.255.255.0", "0.0.0.3");
1137
1138 for (uint32_t j = 0; j < nFill; ++j)
1139 {
1140 address.Assign (fillDevices.Get (i * nFill + j));
1141 }
1142 }
1143
1144 //
1145 // Create a packet sink on the star "hub" to receive packets.
1146 //
1147 uint16_t port = 50000;
1148 Address hubLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
1149 PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress);
1150 ApplicationContainer hubApp = packetSinkHelper.Install (star.GetHub ());
1151 hubApp.Start (Seconds (1.0));
1152 hubApp.Stop (Seconds (10.0));
1153
1154 //
1155 // Create OnOff applications to send TCP to the hub, one on each spoke node.
1156 //
1157 // Make packets be sent about every DefaultPacketSize / DataRate =
1158 // 4096 bits / (5000 bits/second) = 0.82 second.
1159 OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
1160 onOffHelper.SetConstantRate (DataRate (5000));
1161
1162 ApplicationContainer spokeApps;
1163
1164 for (uint32_t i = 0; i < star.SpokeCount (); ++i)
1165 {
1166 AddressValue remoteAddress (InetSocketAddress (star.GetHubIpv4Address (i), port));
1167 onOffHelper.SetAttribute ("Remote", remoteAddress);
1168 spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (i)));
1169 }
1170
1171 spokeApps.Start (Seconds (1.0));
1172 spokeApps.Stop (Seconds (10.0));
1173
1174 //
1175 // Because we are evil, we also add OnOff applications to send TCP to the hub
1176 // from the fill devices on each CSMA link. The first nFill nodes in the
1177 // fillNodes container are on the CSMA network talking to the zeroth device
1178 // on the hub node. The next nFill nodes are on the CSMA network talking to
1179 // the first device on the hub node, etc. So the ith fillNode is associated
1180 // with the hub address found on the (i / nFill)th device on the hub node.
1181 //
1182 ApplicationContainer fillApps;
1183
1184 for (uint32_t i = 0; i < fillNodes.GetN (); ++i)
1185 {
1186 AddressValue remoteAddress (InetSocketAddress (star.GetHubIpv4Address (i / nFill), port));
1187 onOffHelper.SetAttribute ("Remote", remoteAddress);
1188 fillApps.Add (onOffHelper.Install (fillNodes.Get (i)));
1189 }
1190
1191 fillApps.Start (Seconds (1.0));
1192 fillApps.Stop (Seconds (10.0));
1193
1194 //
1195 // Turn on global static routing so we can actually be routed across the star.
1196 //
1197 Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
1198
1199 // Trace receptions
1200 Config::ConnectWithoutContext ("/NodeList/0/ApplicationList/*/$ns3::PacketSink/Rx",
1202
1203 Simulator::Run ();
1204 Simulator::Destroy ();
1205
1206 // The hub node should have received 10 packets from the nFill + 1
1207 // nodes on each spoke.
1208 NS_TEST_ASSERT_MSG_EQ (m_count, 10 * ( nSpokes * (nFill + 1)), "Hub node did not receive the proper number of packets");
1209}
1210
1217{
1218public:
1220};
1221
1223 : TestSuite ("csma-system", UNIT)
1224{
1225 AddTestCase (new CsmaBridgeTestCase, TestCase::QUICK);
1226 AddTestCase (new CsmaBroadcastTestCase, TestCase::QUICK);
1227 AddTestCase (new CsmaMulticastTestCase, TestCase::QUICK);
1228 AddTestCase (new CsmaOneSubnetTestCase, TestCase::QUICK);
1229 AddTestCase (new CsmaPacketSocketTestCase, TestCase::QUICK);
1230 AddTestCase (new CsmaPingTestCase, TestCase::QUICK);
1231 AddTestCase (new CsmaRawIpSocketTestCase, TestCase::QUICK);
1232 AddTestCase (new CsmaStarTestCase, TestCase::QUICK);
1233}
1234
CSMA Bridge mode test.
virtual void DoRun(void)
Implementation to actually run this TestCase.
uint32_t m_count
Counter of received packets.
void SinkRx(Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received.
CSMA Broadcast mode test.
uint32_t m_countNode2
Counter of received packets on node 2.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void DropEvent(Ptr< const Packet > p)
Sink called when a packet is dropped.
void SinkRxNode1(Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received by a node.
void SinkRxNode2(Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received by a node.
uint32_t m_countNode1
Counter of received packets on node 1.
uint32_t m_drops
Counter of dropped packets.
CSMA Multicast mode test.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void SinkRx(Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received by a node.
void DropEvent(Ptr< const Packet > p)
Sink called when a packet is dropped.
uint32_t m_count
Counter of received packets.
uint32_t m_drops
Counter of dropped packets.
CSMA One Subnet mode test.
uint32_t m_countNode1
Counter of received packets on node 1.
void SinkRxNode0(Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received by a node.
uint32_t m_countNode0
Counter of received packets on node 0.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void SinkRxNode1(Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received by a node.
void DropEvent(Ptr< const Packet > p)
Sink called when a packet is dropped.
uint32_t m_drops
Counter of dropped packets.
uint32_t m_count
Counter of received packets.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void SinkRx(std::string path, Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received by a node.
uint32_t m_drops
Counter of dropped packets.
void DropEvent(Ptr< const Packet > p)
Sink called when a packet is dropped.
void PingRtt(std::string context, Time rtt)
Sink called when a PING ois received.
uint32_t m_drops
Counter of dropped packets.
uint32_t m_countSinkRx
Counter of received packets.
virtual void DoRun(void)
Implementation to actually run this TestCase.
uint32_t m_countPingRtt
Counter of PING received.
void DropEvent(Ptr< const Packet > p)
Sink called when a packet is dropped.
void SinkRx(Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received by a node.
void DropEvent(Ptr< const Packet > p)
Sink called when a packet is dropped.
void SinkRx(Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received by a node.
uint32_t m_drops
Counter of dropped packets.
virtual void DoRun(void)
Implementation to actually run this TestCase.
uint32_t m_count
Counter of received packets.
CSMA star mode test.
void DropEvent(Ptr< const Packet > p)
Sink called when a packet is dropped.
uint32_t m_count
Counter of received packets.
virtual void DoRun(void)
Implementation to actually run this TestCase.
uint32_t m_drops
Counter of dropped packets.
void SinkRx(Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received by a node.
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Address.
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging)
Definition: bridge-helper.h:44
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
Csma Channel.
Definition: csma-channel.h:91
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
A helper to make it easier to create a star topology with Csma links.
void InstallStack(InternetStackHelper stack)
Ipv4Address GetHubIpv4Address(uint32_t i) const
void AssignIpv4Addresses(Ipv4AddressHelper address)
Ptr< Node > GetSpokeNode(uint32_t i) const
Ptr< Node > GetHub() const
NetDeviceContainer GetSpokeDevices() const
uint32_t SpokeCount() const
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class that adds ns3::Ipv4StaticRouting objects.
void AddMulticastRoute(Ptr< Node > n, Ipv4Address source, Ipv4Address group, Ptr< NetDevice > input, NetDeviceContainer output)
Add a multicast route to a node and net device using explicit Ptr<Node> and Ptr<NetDevice>
void SetDefaultMulticastRoute(Ptr< Node > n, Ptr< NetDevice > nd)
Add a default route to the static routing protocol to forward packets out a particular interface.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
virtual Address GetAddress(void) const =0
virtual uint32_t GetIfIndex(void) const =0
virtual Ptr< Channel > GetChannel(void) const =0
keep track of a set of node pointers.
uint32_t GetN(void) const
Get the number of Ptr<Node> stored in this container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Hold variables of type string.
Definition: string.h:41
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
AttributeValue implementation for Time.
Definition: nstime.h:1308
Create a IPv4 ping application and associate it to a node.
Definition: v4ping-helper.h:38
ApplicationContainer Install(NodeContainer nodes) const
Install a Ping application on each Node in the provided NodeContainer.
static CsmaSystemTestSuite csmaSystemTestSuite
Do not forget to allocate an instance of this TestSuite.
uint16_t port
Definition: dsdv-manet.cc:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:901
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:141
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
address
Definition: first.py:44
devices
Definition: first.py:39
nodes
Definition: first.py:32
interfaces
Definition: first.py:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
csma
Definition: second.py:63
channel
Definition: third.py:92
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56