A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-system-test-suite.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// This is not a test of CsmaNetDevice model behavior per-se, but
6// instead is a roll up of several end-to-end examples in examples/csma
7// directory, converted into system tests. Writing a test suite
8// to test Csma itself is for further study.
9
10#include "ns3/address.h"
11#include "ns3/application-container.h"
12#include "ns3/bridge-helper.h"
13#include "ns3/callback.h"
14#include "ns3/config.h"
15#include "ns3/csma-helper.h"
16#include "ns3/csma-star-helper.h"
17#include "ns3/data-rate.h"
18#include "ns3/inet-socket-address.h"
19#include "ns3/internet-stack-helper.h"
20#include "ns3/ipv4-address-helper.h"
21#include "ns3/ipv4-global-routing-helper.h"
22#include "ns3/ipv4-static-routing-helper.h"
23#include "ns3/node-container.h"
24#include "ns3/node.h"
25#include "ns3/on-off-helper.h"
26#include "ns3/packet-sink-helper.h"
27#include "ns3/packet-socket-address.h"
28#include "ns3/packet-socket-helper.h"
29#include "ns3/packet.h"
30#include "ns3/ping-helper.h"
31#include "ns3/pointer.h"
32#include "ns3/simple-channel.h"
33#include "ns3/simulator.h"
34#include "ns3/string.h"
35#include "ns3/test.h"
36#include "ns3/uinteger.h"
37
38#include <string>
39
40using namespace ns3;
41
42/**
43 * \ingroup system-tests-csma
44 *
45 * \brief CSMA Bridge mode test.
46 */
48{
49 public:
51 ~CsmaBridgeTestCase() override;
52
53 private:
54 void DoRun() override;
55
56 /**
57 * Sink called when a packet is received.
58 * \param p Received packet (unused).
59 * \param ad Sender's address (uused).
60 */
61 void SinkRx(Ptr<const Packet> p, const Address& ad);
62 uint32_t m_count; //!< Counter of received packets.
63};
64
65// Add some help text to this case to describe what it is intended to test
67 : TestCase("Bridge example for Carrier Sense Multiple Access (CSMA) networks"),
68 m_count(0)
69{
70}
71
75
76void
81
82// Network topology
83//
84// n0 n1
85// | |
86// ----------
87// | Switch |
88// ----------
89// | |
90// n2 n3
91//
92// - CBR/UDP test flow from n0 to n1; test that packets received on n1
93//
94void
96{
97 NodeContainer terminals;
98 terminals.Create(4);
99
100 NodeContainer csmaSwitch;
101 csmaSwitch.Create(1);
102
103 CsmaHelper csma;
104 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
105 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
106
107 NetDeviceContainer terminalDevices;
108 NetDeviceContainer switchDevices;
109
110 for (int i = 0; i < 4; i++)
111 {
112 NetDeviceContainer link = csma.Install(NodeContainer(terminals.Get(i), csmaSwitch));
113 terminalDevices.Add(link.Get(0));
114 switchDevices.Add(link.Get(1));
115 }
116
117 // Create the bridge netdevice, which will do the packet switching
118 Ptr<Node> switchNode = csmaSwitch.Get(0);
119 BridgeHelper bridge;
120 bridge.Install(switchNode, switchDevices);
121
122 InternetStackHelper internet;
123 internet.Install(terminals);
124
126 ipv4.SetBase("10.1.1.0", "255.255.255.0");
127 ipv4.Assign(terminalDevices);
128
129 uint16_t port = 9; // Discard port (RFC 863)
130
131 // Create the OnOff application to send UDP datagrams from n0 to n1.
132 //
133 // Make packets be sent about every DefaultPacketSize / DataRate =
134 // 4096 bits / (5000 bits/second) = 0.82 second.
135 OnOffHelper onoff("ns3::UdpSocketFactory",
137 onoff.SetConstantRate(DataRate(5000));
138
139 ApplicationContainer app = onoff.Install(terminals.Get(0));
140 app.Start(Seconds(1.0));
141 app.Stop(Seconds(10.0));
142
143 PacketSinkHelper sink("ns3::UdpSocketFactory",
145 app = sink.Install(terminals.Get(1));
146 app.Start(Seconds(0.0));
147
148 // Trace receptions
149 Config::ConnectWithoutContext("/NodeList/1/ApplicationList/0/$ns3::PacketSink/Rx",
151
154
155 // We should have sent and received 10 packets
156 NS_TEST_ASSERT_MSG_EQ(m_count, 10, "Bridge should have passed 10 packets");
157}
158
159/**
160 * \ingroup system-tests-csma
161 *
162 * \brief CSMA Broadcast mode test.
163 */
165{
166 public:
168 ~CsmaBroadcastTestCase() override;
169
170 private:
171 void DoRun() override;
172
173 /**
174 * Sink called when a packet is received by a node.
175 * \param p Received packet (unused).
176 * \param ad Sender's address (uused).
177 * @{
178 */
179 void SinkRxNode1(Ptr<const Packet> p, const Address& ad);
180 void SinkRxNode2(Ptr<const Packet> p, const Address& ad);
181 /** @} */
182
183 /**
184 * Sink called when a packet is dropped.
185 * \param p Received packet (unused).
186 */
188
189 uint32_t m_countNode1; //!< Counter of received packets on node 1.
190 uint32_t m_countNode2; //!< Counter of received packets on node 2.
191 uint32_t m_drops; //!< Counter of dropped packets.
192};
193
194// Add some help text to this case to describe what it is intended to test
196 : TestCase("Broadcast example for Carrier Sense Multiple Access (CSMA) networks"),
197 m_countNode1(0),
198 m_countNode2(0),
199 m_drops(0)
200{
201}
202
206
207void
212
213void
218
219void
224
225//
226// Example of the sending of a datagram to a broadcast address
227//
228// Network topology
229// ==============
230// | |
231// n0 n1 n2
232// | |
233// ==========
234//
235// n0 originates UDP broadcast to 255.255.255.255/discard port, which
236// is replicated and received on both n1 and n2
237//
238void
240{
242 c.Create(3);
243 NodeContainer c0 = NodeContainer(c.Get(0), c.Get(1));
244 NodeContainer c1 = NodeContainer(c.Get(0), c.Get(2));
245
246 CsmaHelper csma;
247 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
248 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
249
250 NetDeviceContainer n0 = csma.Install(c0);
251 NetDeviceContainer n1 = csma.Install(c1);
252
253 InternetStackHelper internet;
254 internet.Install(c);
255
257 ipv4.SetBase("10.1.0.0", "255.255.255.0");
258 ipv4.Assign(n0);
259 ipv4.SetBase("192.168.1.0", "255.255.255.0");
260 ipv4.Assign(n1);
261
262 // RFC 863 discard port ("9") indicates packet should be thrown away
263 // by the system. We allow this silent discard to be overridden
264 // by the PacketSink application.
265 uint16_t port = 9;
266
267 // Create the OnOff application to send UDP datagrams from n0.
268 //
269 // Make packets be sent about every DefaultPacketSize / DataRate =
270 // 4096 bits / (5000 bits/second) = 0.82 second.
271 OnOffHelper onoff("ns3::UdpSocketFactory",
272 Address(InetSocketAddress(Ipv4Address("255.255.255.255"), port)));
273 onoff.SetConstantRate(DataRate(5000));
274
275 ApplicationContainer app = onoff.Install(c0.Get(0));
276 // Start the application
277 app.Start(Seconds(1.0));
278 app.Stop(Seconds(10.0));
279
280 // Create an optional packet sink to receive these packets
281 PacketSinkHelper sink("ns3::UdpSocketFactory",
283 app = sink.Install(c0.Get(1));
284 app.Add(sink.Install(c1.Get(1)));
285 app.Start(Seconds(1.0));
286 app.Stop(Seconds(10.0));
287
288 // Trace receptions
289 Config::ConnectWithoutContext("/NodeList/1/ApplicationList/0/$ns3::PacketSink/Rx",
291 Config::ConnectWithoutContext("/NodeList/2/ApplicationList/0/$ns3::PacketSink/Rx",
293
296
297 // We should have sent and received 10 packets
298 NS_TEST_ASSERT_MSG_EQ(m_countNode1, 10, "Node 1 should have received 10 packets");
299 NS_TEST_ASSERT_MSG_EQ(m_countNode2, 10, "Node 2 should have received 10 packets");
300}
301
302/**
303 * \ingroup system-tests-csma
304 *
305 * \brief CSMA Multicast mode test.
306 */
308{
309 public:
311 ~CsmaMulticastTestCase() override;
312
313 private:
314 void DoRun() override;
315
316 /**
317 * Sink called when a packet is received by a node.
318 * \param p Received packet (unused).
319 * \param ad Sender's address (uused).
320 */
321 void SinkRx(Ptr<const Packet> p, const Address& ad);
322
323 /**
324 * Sink called when a packet is dropped.
325 * \param p Received packet (unused).
326 */
328
329 uint32_t m_count; //!< Counter of received packets.
330 uint32_t m_drops; //!< Counter of dropped packets.
331};
332
333// Add some help text to this case to describe what it is intended to test
335 : TestCase("Multicast example for Carrier Sense Multiple Access (CSMA) networks"),
336 m_count(0),
337 m_drops(0)
338{
339}
340
344
345void
350
351void
356
357// Network topology
358//
359// Lan1
360// ===========
361// | | |
362// n0 n1 n2 n3 n4
363// | | |
364// ===========
365// Lan0
366//
367// - Multicast source is at node n0;
368// - Multicast forwarded by node n2 onto LAN1;
369// - Nodes n0, n1, n2, n3, and n4 receive the multicast frame.
370// - Node n4 listens for the data
371//
372void
374{
375 //
376 // Set up default values for the simulation.
377 //
378 // Select DIX/Ethernet II-style encapsulation (no LLC/Snap header)
379 Config::SetDefault("ns3::CsmaNetDevice::EncapsulationMode", StringValue("Dix"));
380
382 c.Create(5);
383 // We will later want two subcontainers of these nodes, for the two LANs
384 NodeContainer c0 = NodeContainer(c.Get(0), c.Get(1), c.Get(2));
385 NodeContainer c1 = NodeContainer(c.Get(2), c.Get(3), c.Get(4));
386
387 CsmaHelper csma;
388 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
389 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
390
391 // We will use these NetDevice containers later, for IP addressing
392 NetDeviceContainer nd0 = csma.Install(c0); // First LAN
393 NetDeviceContainer nd1 = csma.Install(c1); // Second LAN
394
395 InternetStackHelper internet;
396 internet.Install(c);
397
398 Ipv4AddressHelper ipv4Addr;
399 ipv4Addr.SetBase("10.1.1.0", "255.255.255.0");
400 ipv4Addr.Assign(nd0);
401 ipv4Addr.SetBase("10.1.2.0", "255.255.255.0");
402 ipv4Addr.Assign(nd1);
403
404 //
405 // Now we can configure multicasting. As described above, the multicast
406 // source is at node zero, which we assigned the IP address of 10.1.1.1
407 // earlier. We need to define a multicast group to send packets to. This
408 // can be any multicast address from 224.0.0.0 through 239.255.255.255
409 // (avoiding the reserved routing protocol addresses).
410 //
411
412 Ipv4Address multicastSource("10.1.1.1");
413 Ipv4Address multicastGroup("225.1.2.4");
414
415 // Now, we will set up multicast routing. We need to do three things:
416 // 1) Configure a (static) multicast route on node n2
417 // 2) Set up a default multicast route on the sender n0
418 // 3) Have node n4 join the multicast group
419 // We have a helper that can help us with static multicast
420 Ipv4StaticRoutingHelper multicast;
421
422 // 1) Configure a (static) multicast route on node n2 (multicastRouter)
423 Ptr<Node> multicastRouter = c.Get(2); // The node in question
424 Ptr<NetDevice> inputIf = nd0.Get(2); // The input NetDevice
425 NetDeviceContainer outputDevices; // A container of output NetDevices
426 outputDevices.Add(nd1.Get(0)); // (we only need one NetDevice here)
427
428 multicast.AddMulticastRoute(multicastRouter,
429 multicastSource,
430 multicastGroup,
431 inputIf,
432 outputDevices);
433
434 // 2) Set up a default multicast route on the sender n0
435 Ptr<Node> sender = c.Get(0);
436 Ptr<NetDevice> senderIf = nd0.Get(0);
437 multicast.SetDefaultMulticastRoute(sender, senderIf);
438
439 //
440 // Create an OnOff application to send UDP datagrams from node zero to the
441 // multicast group (node four will be listening).
442 //
443
444 uint16_t multicastPort = 9; // Discard port (RFC 863)
445
446 // Configure a multicast packet generator.
447 //
448 // Make packets be sent about every defaultPacketSize / dataRate =
449 // 4096 bits / (5000 bits/second) = 0.82 second.
450 OnOffHelper onoff("ns3::UdpSocketFactory",
451 Address(InetSocketAddress(multicastGroup, multicastPort)));
452 onoff.SetConstantRate(DataRate(5000));
453
454 ApplicationContainer srcC = onoff.Install(c0.Get(0));
455
456 //
457 // Tell the application when to start and stop.
458 //
459 srcC.Start(Seconds(1.));
460 srcC.Stop(Seconds(10.));
461
462 // Create an optional packet sink to receive these packets
463 PacketSinkHelper sink("ns3::UdpSocketFactory",
464 InetSocketAddress(Ipv4Address::GetAny(), multicastPort));
465
466 ApplicationContainer sinkC = sink.Install(c1.Get(2)); // Node n4
467 // Start the sink
468 sinkC.Start(Seconds(1.0));
469 sinkC.Stop(Seconds(10.0));
470
471 // Trace receptions
472 Config::ConnectWithoutContext("/NodeList/4/ApplicationList/0/$ns3::PacketSink/Rx",
474
475 //
476 // Now, do the actual simulation.
477 //
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
485/**
486 * \ingroup system-tests-csma
487 *
488 * \brief CSMA One Subnet mode test.
489 */
491{
492 public:
494 ~CsmaOneSubnetTestCase() override;
495
496 private:
497 void DoRun() override;
498
499 /**
500 * Sink called when a packet is received by a node.
501 * \param p Received packet (unused).
502 * \param ad Sender's address (uused).
503 * @{
504 */
505 void SinkRxNode0(Ptr<const Packet> p, const Address& ad);
506 void SinkRxNode1(Ptr<const Packet> p, const Address& ad);
507 /** @} */
508
509 /**
510 * Sink called when a packet is dropped.
511 * \param p Received packet (unused).
512 */
514 uint32_t m_countNode0; //!< Counter of received packets on node 0.
515 uint32_t m_countNode1; //!< Counter of received packets on node 1.
516 uint32_t m_drops; //!< Counter of dropped packets.
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"),
522 m_countNode0(0),
523 m_countNode1(0),
524 m_drops(0)
525{
526}
527
531
532void
537
538void
543
544void
549
550// Network topology
551//
552// n0 n1 n2 n3
553// | | | |
554// =================
555// LAN
556//
557// - CBR/UDP flows from n0 to n1 and from n3 to n0
558// - DropTail queues
559//
560void
562{
564 nodes.Create(4);
565
566 CsmaHelper csma;
567 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
568 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
569 //
570 // Now fill out the topology by creating the net devices required to connect
571 // the nodes to the channels and hooking them up.
572 //
573 NetDeviceContainer devices = csma.Install(nodes);
574
575 InternetStackHelper internet;
576 internet.Install(nodes);
577
578 // We've got the "hardware" in place. Now we need to add IP addresses.
579 //
581 ipv4.SetBase("10.1.1.0", "255.255.255.0");
582 Ipv4InterfaceContainer interfaces = ipv4.Assign(devices);
583
584 uint16_t port = 9; // Discard port (RFC 863)
585
586 //
587 // Create an OnOff application to send UDP datagrams from node zero
588 // to node 1.
589 //
590 // Make packets be sent about every defaultPacketSize / dataRate =
591 // 4096 bits / (5000 bits/second) = 0.82 second.
592 OnOffHelper onoff("ns3::UdpSocketFactory",
593 Address(InetSocketAddress(interfaces.GetAddress(1), port)));
594 onoff.SetConstantRate(DataRate(5000));
595
596 ApplicationContainer app = onoff.Install(nodes.Get(0));
597 // Start the application
598 app.Start(Seconds(1.0));
599 app.Stop(Seconds(10.0));
600
601 // Create an optional packet sink to receive these packets
602 PacketSinkHelper sink("ns3::UdpSocketFactory",
604 app = sink.Install(nodes.Get(1));
605 app.Start(Seconds(0.0));
606
607 //
608 // Create a similar flow from n3 to n0, starting at time 1.1 seconds
609 //
610 onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(interfaces.GetAddress(0), port)));
611 app = onoff.Install(nodes.Get(3));
612 app.Start(Seconds(1.1));
613 app.Stop(Seconds(10.0));
614
615 app = sink.Install(nodes.Get(0));
616 app.Start(Seconds(0.0));
617
618 // Trace receptions
619 Config::ConnectWithoutContext("/NodeList/0/ApplicationList/1/$ns3::PacketSink/Rx",
621 Config::ConnectWithoutContext("/NodeList/1/ApplicationList/0/$ns3::PacketSink/Rx",
623
624 //
625 // Now, do the actual simulation.
626 //
629
630 // We should have sent and received 10 packets
631 NS_TEST_ASSERT_MSG_EQ(m_countNode0, 10, "Node 0 should have received 10 packets");
632 NS_TEST_ASSERT_MSG_EQ(m_countNode1, 10, "Node 1 should have received 10 packets");
633}
634
635/**
636 * \ingroup system-tests-csma
637 *
638 * \brief CSMA PacketSocket test.
639 */
641{
642 public:
644 ~CsmaPacketSocketTestCase() override;
645
646 private:
647 void DoRun() override;
648 /**
649 * Sink called when a packet is received by a node.
650 * \param path Sink path.
651 * \param p Received packet (unused).
652 * \param ad Sender's address (uused).
653 */
654 void SinkRx(std::string path, Ptr<const Packet> p, const Address& ad);
655
656 /**
657 * Sink called when a packet is dropped.
658 * \param p Received packet (unused).
659 */
661
662 uint32_t m_count; //!< Counter of received packets.
663 uint32_t m_drops; //!< Counter of dropped packets.
664};
665
666// Add some help text to this case to describe what it is intended to test
668 : TestCase("Packet socket example for Carrier Sense Multiple Access (CSMA) networks"),
669 m_count(0),
670 m_drops(0)
671{
672}
673
677
678void
680{
681 m_count++;
682}
683
684void
689
690//
691// Network topology
692//
693// n0 n1 n2 n3
694// | | | |
695// =====================
696//
697// - Packet socket flow from n0 to n1 and from node n3 to n0
698// -- We will test reception at node n0
699// - Default 512 byte packets generated by traffic generator
700//
701void
703{
704 // Here, we will explicitly create four nodes.
706 nodes.Create(4);
707
708 PacketSocketHelper packetSocket;
709 packetSocket.Install(nodes);
710
711 // create the shared medium used by all csma devices.
712 Ptr<CsmaChannel> channel =
714 DataRateValue(DataRate(5000000)),
715 "Delay",
717
718 // use a helper function to connect our nodes to the shared channel.
719 CsmaHelper csma;
720 csma.SetDeviceAttribute("EncapsulationMode", StringValue("Llc"));
721 NetDeviceContainer devs = csma.Install(nodes, channel);
722
723 // Create the OnOff application to send raw datagrams
724 //
725 // Make packets be sent about every DefaultPacketSize / DataRate =
726 // 4096 bits / (5000 bits/second) = 0.82 second.
727 PacketSocketAddress socket;
728 socket.SetSingleDevice(devs.Get(0)->GetIfIndex());
729 socket.SetPhysicalAddress(devs.Get(1)->GetAddress());
730 socket.SetProtocol(2);
731 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
732 onoff.SetConstantRate(DataRate(5000));
733 ApplicationContainer apps = onoff.Install(nodes.Get(0));
734 apps.Start(Seconds(1.0));
735 apps.Stop(Seconds(10.0));
736
737 socket.SetSingleDevice(devs.Get(3)->GetIfIndex());
738 socket.SetPhysicalAddress(devs.Get(0)->GetAddress());
739 socket.SetProtocol(3);
740 onoff.SetAttribute("Remote", AddressValue(socket));
741 apps = onoff.Install(nodes.Get(3));
742 apps.Start(Seconds(1.0));
743 apps.Stop(Seconds(10.0));
744
745 PacketSinkHelper sink = PacketSinkHelper("ns3::PacketSocketFactory", socket);
746 apps = sink.Install(nodes.Get(0));
747 apps.Start(Seconds(0.0));
748 apps.Stop(Seconds(20.0));
749
750 // Trace receptions
751 Config::Connect("/NodeList/0/ApplicationList/*/$ns3::PacketSink/Rx",
753
756
757 // We should have received 10 packets on node 0
758 NS_TEST_ASSERT_MSG_EQ(m_count, 10, "Node 0 should have received 10 packets");
759}
760
761/**
762 * \ingroup system-tests-csma
763 *
764 * \brief CSMA PING test.
765 */
767{
768 public:
770 ~CsmaPingTestCase() override;
771
772 private:
773 void DoRun() override;
774 /**
775 * Sink called when a packet is received by a node.
776 */
777 void SinkRx(Ptr<const Packet>, const Address&);
778
779 /**
780 * Sink called when a PING is received.
781 */
782 void PingRtt(std::string, uint16_t, Time);
783
784 /**
785 * Sink called when a packet is dropped.
786 */
788
789 uint32_t m_countSinkRx; //!< Counter of received packets.
790 uint32_t m_countPingRtt; //!< Counter of PING received.
791 uint32_t m_drops; //!< Counter of dropped packets.
792};
793
794// Add some help text to this case to describe what it is intended to test
796 : TestCase("Ping example for Carrier Sense Multiple Access (CSMA) networks"),
797 m_countSinkRx(0),
798 m_countPingRtt(0),
799 m_drops(0)
800{
801}
802
806
807void
812
813void
814CsmaPingTestCase::PingRtt(std::string, uint16_t, Time)
815{
817}
818
819void
824
825// Network topology
826//
827// n0 n1 n2 n3
828// | | | |
829// =====================
830//
831// node n0,n1,n3 pings to node n2
832// node n0 generates protocol 2 (IGMP) to node n3
833//
834void
836{
837 // Here, we will explicitly create four nodes.
839 c.Create(4);
840
841 // connect all our nodes to a shared channel.
842 CsmaHelper csma;
843 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
844 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
845 csma.SetDeviceAttribute("EncapsulationMode", StringValue("Llc"));
846 NetDeviceContainer devs = csma.Install(c);
847
848 // add an ip stack to all nodes.
849 InternetStackHelper ipStack;
850 ipStack.Install(c);
851
852 // assign ip addresses
854 ip.SetBase("192.168.1.0", "255.255.255.0");
855 Ipv4InterfaceContainer addresses = ip.Assign(devs);
856
857 // Create the OnOff application to send UDP datagrams from n0 to n1.
858 //
859 // Make packets be sent about every DefaultPacketSize / DataRate =
860 // 4096 bits / (5000 bits/second) = 0.82 second.
861 Config::SetDefault("ns3::Ipv4RawSocketImpl::Protocol", StringValue("2"));
862 InetSocketAddress dst(addresses.GetAddress(3));
863 OnOffHelper onoff = OnOffHelper("ns3::Ipv4RawSocketFactory", dst);
864 onoff.SetConstantRate(DataRate(5000));
865
866 ApplicationContainer apps = onoff.Install(c.Get(0));
867 apps.Start(Seconds(1.0));
868 apps.Stop(Seconds(10.0));
869
870 PacketSinkHelper sink = PacketSinkHelper("ns3::Ipv4RawSocketFactory", dst);
871 apps = sink.Install(c.Get(3));
872 apps.Start(Seconds(0.0));
873 apps.Stop(Seconds(11.0));
874
875 PingHelper ping(addresses.GetAddress(2));
876 NodeContainer pingers;
877 pingers.Add(c.Get(0));
878 pingers.Add(c.Get(1));
879 pingers.Add(c.Get(3));
880 apps = ping.Install(pingers);
881 apps.Start(Seconds(2.0));
882 apps.Stop(Seconds(5.0));
883
884 // Trace receptions
885 Config::ConnectWithoutContext("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
887
888 // Trace pings
889 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::Ping/Rtt",
891
894
895 // We should have sent and received 10 packets
896 NS_TEST_ASSERT_MSG_EQ(m_countSinkRx, 10, "Node 3 should have received 10 packets");
897
898 // We should have 3 pingers that ping every second for 3 seconds.
899 NS_TEST_ASSERT_MSG_EQ(m_countPingRtt, 9, "Node 2 should have been pinged 9 times");
900}
901
902/**
903 * \ingroup system-tests-csma
904 *
905 * \brief CSMA Raw IP test.
906 */
908{
909 public:
911 ~CsmaRawIpSocketTestCase() override;
912
913 private:
914 void DoRun() override;
915
916 /**
917 * Sink called when a packet is received by a node.
918 * \param p Received packet (unused).
919 * \param ad Sender's address (uused).
920 */
921 void SinkRx(Ptr<const Packet> p, const Address& ad);
922
923 /**
924 * Sink called when a packet is dropped.
925 * \param p Received packet (unused).
926 */
928
929 uint32_t m_count; //!< Counter of received packets.
930 uint32_t m_drops; //!< Counter of dropped packets.
931};
932
933// Add some help text to this case to describe what it is intended to test
935 : TestCase(
936 "Raw internet protocol socket example for Carrier Sense Multiple Access (CSMA) networks"),
937 m_count(0),
938 m_drops(0)
939{
940}
941
945
946void
951
952void
957
958//
959// Network topology
960// (sender) (receiver)
961// n0 n1 n2 n3
962// | | | |
963// =====================
964//
965// Node n0 sends data to node n3 over a raw IP socket. The protocol
966// number used is 2.
967//
968void
970{
971 // Here, we will explicitly create four nodes.
973 c.Create(4);
974
975 // connect all our nodes to a shared channel.
976 CsmaHelper csma;
977 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
978 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
979 csma.SetDeviceAttribute("EncapsulationMode", StringValue("Llc"));
980 NetDeviceContainer devs = csma.Install(c);
981
982 // add an ip stack to all nodes.
983 InternetStackHelper ipStack;
984 ipStack.Install(c);
985
986 // assign ip addresses
988 ip.SetBase("192.168.1.0", "255.255.255.0");
989 Ipv4InterfaceContainer addresses = ip.Assign(devs);
990
991 // IP protocol configuration
992 //
993 // Make packets be sent about every DefaultPacketSize / DataRate =
994 // 4096 bits / (5000 bits/second) = 0.82 second.
995 Config::SetDefault("ns3::Ipv4RawSocketImpl::Protocol", StringValue("2"));
996 InetSocketAddress dst(addresses.GetAddress(3));
997 OnOffHelper onoff = OnOffHelper("ns3::Ipv4RawSocketFactory", dst);
998 onoff.SetConstantRate(DataRate(5000));
999
1000 ApplicationContainer apps = onoff.Install(c.Get(0));
1001 apps.Start(Seconds(1.0));
1002 apps.Stop(Seconds(10.0));
1003
1004 PacketSinkHelper sink = PacketSinkHelper("ns3::Ipv4RawSocketFactory", dst);
1005 apps = sink.Install(c.Get(3));
1006 apps.Start(Seconds(0.0));
1007 apps.Stop(Seconds(12.0));
1008
1009 // Trace receptions
1010 Config::ConnectWithoutContext("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
1012
1015
1016 // We should have sent and received 10 packets
1017 NS_TEST_ASSERT_MSG_EQ(m_count, 10, "Node 3 should have received 10 packets");
1018}
1019
1020/**
1021 * \ingroup system-tests-csma
1022 *
1023 * \brief CSMA star mode test.
1024 */
1026{
1027 public:
1029 ~CsmaStarTestCase() override;
1030
1031 private:
1032 void DoRun() override;
1033
1034 /**
1035 * Sink called when a packet is received by a node.
1036 * \param p Received packet (unused).
1037 * \param ad Sender's address (uused).
1038 */
1039 void SinkRx(Ptr<const Packet> p, const Address& ad);
1040
1041 /**
1042 * Sink called when a packet is dropped.
1043 * \param p Received packet (unused).
1044 */
1046
1047 uint32_t m_count; //!< Counter of received packets.
1048 uint32_t m_drops; //!< Counter of dropped packets.
1049};
1050
1051// Add some help text to this case to describe what it is intended to test
1053 : TestCase("Star example for Carrier Sense Multiple Access (CSMA) networks"),
1054 m_count(0),
1055 m_drops(0)
1056{
1057}
1058
1062
1063void
1068
1069void
1074
1075// Network topology (default)
1076//
1077// n2 + + n3 .
1078// | ... |\ /| ... | .
1079// ======= \ / ======= .
1080// CSMA \ / CSMA .
1081// \ / .
1082// n1 +--- n0 ---+ n4 .
1083// | ... | / \ | ... | .
1084// ======= / \ ======= .
1085// CSMA / \ CSMA .
1086// / \ .
1087// n6 + + n5 .
1088// | ... | | ... | .
1089// ======= ======= .
1090// CSMA CSMA .
1091//
1092void
1094{
1095 //
1096 // Default number of nodes in the star.
1097 //
1098 uint32_t nSpokes = 7;
1099
1100 CsmaHelper csma;
1101 csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
1102 csma.SetChannelAttribute("Delay", StringValue("1ms"));
1103 CsmaStarHelper star(nSpokes, csma);
1104
1105 NodeContainer fillNodes;
1106
1107 //
1108 // Just to be nasy, hang some more nodes off of the CSMA channel for each
1109 // spoke, so that there are a total of 16 nodes on each channel. Stash
1110 // all of these new devices into a container.
1111 //
1112 NetDeviceContainer fillDevices;
1113
1114 uint32_t nFill = 14;
1115 for (uint32_t i = 0; i < star.GetSpokeDevices().GetN(); ++i)
1116 {
1117 Ptr<Channel> channel = star.GetSpokeDevices().Get(i)->GetChannel();
1118 Ptr<CsmaChannel> csmaChannel = channel->GetObject<CsmaChannel>();
1119 NodeContainer newNodes;
1120 newNodes.Create(nFill);
1121 fillNodes.Add(newNodes);
1122 fillDevices.Add(csma.Install(newNodes, csmaChannel));
1123 }
1124
1125 InternetStackHelper internet;
1126 star.InstallStack(internet);
1127 internet.Install(fillNodes);
1128
1129 star.AssignIpv4Addresses(Ipv4AddressHelper("10.1.0.0", "255.255.255.0"));
1130
1131 //
1132 // We assigned addresses to the logical hub and the first "drop" of the
1133 // CSMA network that acts as the spoke, but we also have a number of fill
1134 // devices (nFill) also hanging off the CSMA network. We have got to
1135 // assign addresses to them as well. We put all of the fill devices into
1136 // a single device container, so the first nFill devices are associated
1137 // with the channel connected to spokeDevices.Get (0), the second nFill
1138 // devices are associated with the channel connected to spokeDevices.Get (1)
1139 // etc.
1140 //
1141 Ipv4AddressHelper address;
1142 for (uint32_t i = 0; i < star.SpokeCount(); ++i)
1143 {
1144 std::ostringstream subnet;
1145 subnet << "10.1." << i << ".0";
1146 address.SetBase(subnet.str().c_str(), "255.255.255.0", "0.0.0.3");
1147
1148 for (uint32_t j = 0; j < nFill; ++j)
1149 {
1150 address.Assign(fillDevices.Get(i * nFill + j));
1151 }
1152 }
1153
1154 //
1155 // Create a packet sink on the star "hub" to receive packets.
1156 //
1157 uint16_t port = 50000;
1159 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", hubLocalAddress);
1160 ApplicationContainer hubApp = packetSinkHelper.Install(star.GetHub());
1161 hubApp.Start(Seconds(1.0));
1162 hubApp.Stop(Seconds(10.0));
1163
1164 //
1165 // Create OnOff applications to send TCP to the hub, one on each spoke node.
1166 //
1167 // Make packets be sent about every DefaultPacketSize / DataRate =
1168 // 4096 bits / (5000 bits/second) = 0.82 second.
1169 OnOffHelper onOffHelper("ns3::TcpSocketFactory", Address());
1170 onOffHelper.SetConstantRate(DataRate(5000));
1171
1172 ApplicationContainer spokeApps;
1173
1174 for (uint32_t i = 0; i < star.SpokeCount(); ++i)
1175 {
1176 AddressValue remoteAddress(InetSocketAddress(star.GetHubIpv4Address(i), port));
1177 onOffHelper.SetAttribute("Remote", remoteAddress);
1178 spokeApps.Add(onOffHelper.Install(star.GetSpokeNode(i)));
1179 }
1180
1181 spokeApps.Start(Seconds(1.0));
1182 spokeApps.Stop(Seconds(10.0));
1183
1184 //
1185 // Because we are evil, we also add OnOff applications to send TCP to the hub
1186 // from the fill devices on each CSMA link. The first nFill nodes in the
1187 // fillNodes container are on the CSMA network talking to the zeroth device
1188 // on the hub node. The next nFill nodes are on the CSMA network talking to
1189 // the first device on the hub node, etc. So the ith fillNode is associated
1190 // with the hub address found on the (i / nFill)th device on the hub node.
1191 //
1192 ApplicationContainer fillApps;
1193
1194 for (uint32_t i = 0; i < fillNodes.GetN(); ++i)
1195 {
1196 AddressValue remoteAddress(InetSocketAddress(star.GetHubIpv4Address(i / nFill), port));
1197 onOffHelper.SetAttribute("Remote", remoteAddress);
1198 fillApps.Add(onOffHelper.Install(fillNodes.Get(i)));
1199 }
1200
1201 fillApps.Start(Seconds(1.0));
1202 fillApps.Stop(Seconds(10.0));
1203
1204 //
1205 // Turn on global static routing so we can actually be routed across the star.
1206 //
1208
1209 // Trace receptions
1210 Config::ConnectWithoutContext("/NodeList/0/ApplicationList/*/$ns3::PacketSink/Rx",
1212
1215
1216 // The hub node should have received 10 packets from the nFill + 1
1217 // nodes on each spoke.
1219 10 * (nSpokes * (nFill + 1)),
1220 "Hub node did not receive the proper number of packets");
1221}
1222
1223/**
1224 * \ingroup system-tests-csma
1225 *
1226 * \brief CSMA TestSuite.
1227 */
1229{
1230 public:
1232};
1233
1235 : TestSuite("csma-system", Type::UNIT)
1236{
1237 AddTestCase(new CsmaBridgeTestCase, TestCase::Duration::QUICK);
1238 AddTestCase(new CsmaBroadcastTestCase, TestCase::Duration::QUICK);
1239 AddTestCase(new CsmaMulticastTestCase, TestCase::Duration::QUICK);
1240 AddTestCase(new CsmaOneSubnetTestCase, TestCase::Duration::QUICK);
1241 AddTestCase(new CsmaPacketSocketTestCase, TestCase::Duration::QUICK);
1242 AddTestCase(new CsmaPingTestCase, TestCase::Duration::QUICK);
1243 AddTestCase(new CsmaRawIpSocketTestCase, TestCase::Duration::QUICK);
1244 AddTestCase(new CsmaStarTestCase, TestCase::Duration::QUICK);
1245}
1246
1247/// Do not forget to allocate an instance of this TestSuite
CSMA Bridge mode test.
uint32_t m_count
Counter of received packets.
void DoRun() override
Implementation to actually run this TestCase.
void SinkRx(Ptr< const Packet > p, const Address &ad)
Sink called when a packet is received.
CSMA Broadcast mode test.
void DoRun() override
Implementation to actually run this TestCase.
uint32_t m_countNode2
Counter of received packets on node 2.
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.
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 DoRun() override
Implementation to actually run this TestCase.
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.
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.
void DoRun() override
Implementation to actually run this TestCase.
uint32_t m_drops
Counter of dropped packets.
uint32_t m_count
Counter of received packets.
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 DoRun() override
Implementation to actually run this TestCase.
void DropEvent(Ptr< const Packet >)
Sink called when a packet is dropped.
void DoRun() override
Implementation to actually run this TestCase.
void PingRtt(std::string, uint16_t, Time)
Sink called when a PING is received.
void SinkRx(Ptr< const Packet >, const Address &)
Sink called when a packet is received by a node.
uint32_t m_drops
Counter of dropped packets.
uint32_t m_countSinkRx
Counter of received packets.
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 DoRun() override
Implementation to actually run this TestCase.
uint32_t m_drops
Counter of dropped packets.
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.
uint32_t m_drops
Counter of dropped packets.
void DoRun() override
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.
a polymophic address class
Definition address.h:90
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
ApplicationContainer Install(NodeContainer c)
Install an application on each node of the input container configured with all the attributes set wit...
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging)
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
Csma Channel.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
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:78
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.
static Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
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.
keep track of a set of node pointers.
uint32_t GetN() 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(const NodeContainer &nc)
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.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
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.
Create a ping application and associate it to a node.
Definition ping-helper.h:31
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold variables of type string.
Definition string.h:45
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
static CsmaSystemTestSuite csmaSystemTestSuite
Do not forget to allocate an instance of this TestSuite.
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:967
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:943
Ptr< T > CreateObjectWithAttributes(Args... args)
Allocate an Object on the heap and initialize with a set of attributes.
#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:134
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44