// // Network topology // // n0 n1 n2 n3 // | | | | // ===================== // // - Packet socket flow from n0 to n1 and from node n3 to n0 // -- We will test reception at node n0 // - Default 512 byte packets generated by traffic generator // void CsmaPacketSocketTestCase::DoRun (void) { // Here, we will explicitly create four nodes. NodeContainer nodes; nodes.Create (4); PacketSocketHelper packetSocket; packetSocket.Install (nodes); // create the shared medium used by all csma devices. Ptr<CsmaChannel> channel = CreateObjectWithAttributes<CsmaChannel> ( "DataRate", DataRateValue (DataRate (5000000)), "Delay", TimeValue (MilliSeconds (2))); // use a helper function to connect our nodes to the shared channel. CsmaHelper csma; csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc")); NetDeviceContainer devs = csma.Install (nodes, channel); // Create the OnOff application to send raw datagrams // // Make packets be sent about every DefaultPacketSize / DataRate = // 4096 bits / (5000 bits/second) = 0.82 second. PacketSocketAddress socket; socket.SetSingleDevice (devs.Get (0)->GetIfIndex ()); socket.SetPhysicalAddress (devs.Get (1)->GetAddress ()); socket.SetProtocol (2); OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket)); onoff.SetConstantRate (DataRate (5000)); ApplicationContainer apps = onoff.Install (nodes.Get (0)); apps.Start (Seconds (1.0)); apps.Stop (Seconds (10.0)); socket.SetSingleDevice (devs.Get (3)->GetIfIndex ()); socket.SetPhysicalAddress (devs.Get (0)->GetAddress ()); socket.SetProtocol (3); onoff.SetAttribute ("Remote", AddressValue (socket)); apps = onoff.Install (nodes.Get (3)); apps.Start (Seconds (1.0)); apps.Stop (Seconds (10.0)); PacketSinkHelper sink = PacketSinkHelper ("ns3::PacketSocketFactory", socket); apps = sink.Install (nodes.Get (0)); apps.Start (Seconds (0.0)); apps.Stop (Seconds (20.0)); // Trace receptions Config::Connect ("/NodeList/0/ApplicationList/*/$ns3::PacketSink/Rx", MakeCallback (&CsmaPacketSocketTestCase::SinkRx, this)); Simulator::Run (); Simulator::Destroy (); // We should have received 10 packets on node 0 NS_TEST_ASSERT_MSG_EQ (m_count, 10, "Node 0 should have received 10 packets"); }
// // Network topology // (sender) (receiver) // n0 n1 n2 n3 // | | | | // ===================== // // Node n0 sends data to node n3 over a raw IP socket. The protocol // number used is 2. // void CsmaRawIpSocketTestCase::DoRun (void) { // Here, we will explicitly create four nodes. NodeContainer c; c.Create (4); // connect all our nodes to a shared channel. CsmaHelper csma; csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc")); NetDeviceContainer devs = csma.Install (c); // add an ip stack to all nodes. InternetStackHelper ipStack; ipStack.Install (c); // assign ip addresses Ipv4AddressHelper ip; ip.SetBase ("192.168.1.0", "255.255.255.0"); Ipv4InterfaceContainer addresses = ip.Assign (devs); // IP protocol configuration // // Make packets be sent about every DefaultPacketSize / DataRate = // 4096 bits / (5000 bits/second) = 0.82 second. Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2")); InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3)); OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst); onoff.SetConstantRate (DataRate (5000)); ApplicationContainer apps = onoff.Install (c.Get (0)); apps.Start (Seconds (1.0)); apps.Stop (Seconds (10.0)); PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst); apps = sink.Install (c.Get (3)); apps.Start (Seconds (0.0)); apps.Stop (Seconds (12.0)); // Trace receptions Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx", MakeCallback (&CsmaRawIpSocketTestCase::SinkRx, this)); Simulator::Run (); Simulator::Destroy (); // We should have sent and received 10 packets NS_TEST_ASSERT_MSG_EQ (m_count, 10, "Node 3 should have received 10 packets"); }
int main (int argc, char *argv[]) { // // Users may find it convenient to turn on explicit debugging // for selected modules; the below lines suggest how to do this // #if 0 LogComponentEnable ("UdpEchoExample", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_ALL); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_ALL); #endif // // Allow the user to override any of the defaults and the above Bind() at // run-time, via command-line arguments // bool useV6 = false; Address serverAddress; CommandLine cmd; cmd.AddValue ("useIpv6", "Use Ipv6", useV6); cmd.Parse (argc, argv); // // Explicitly create the nodes required by the topology (shown above). // NS_LOG_INFO ("Create nodes."); NodeContainer n; n.Create (4); InternetStackHelper internet; internet.Install (n); NS_LOG_INFO ("Create channels."); // // Explicitly create the channels required by the topology (shown above). // CsmaHelper csma; csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); csma.SetDeviceAttribute ("Mtu", UintegerValue (1400)); NetDeviceContainer d = csma.Install (n); // // We've got the "hardware" in place. Now we need to add IP addresses. // NS_LOG_INFO ("Assign IP Addresses."); if (useV6 == false) { Ipv4AddressHelper ipv4; ipv4.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer i = ipv4.Assign (d); serverAddress = Address(i.GetAddress (1)); } else { Ipv6AddressHelper ipv6; ipv6.SetBase ("2001:0000:f00d:cafe::", Ipv6Prefix (64)); Ipv6InterfaceContainer i6 = ipv6.Assign (d); serverAddress = Address(i6.GetAddress (1,1)); } NS_LOG_INFO ("Create Applications."); // // Create a UdpEchoServer application on node one. // uint16_t port = 9; // well-known echo port number UdpEchoServerHelper server (port); ApplicationContainer apps = server.Install (n.Get (1)); apps.Start (Seconds (1.0)); apps.Stop (Seconds (10.0)); // // Create a UdpEchoClient application to send UDP datagrams from node zero to // node one. // uint32_t packetSize = 1024; uint32_t maxPacketCount = 1; Time interPacketInterval = Seconds (1.); UdpEchoClientHelper client (serverAddress, port); client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); client.SetAttribute ("Interval", TimeValue (interPacketInterval)); client.SetAttribute ("PacketSize", UintegerValue (packetSize)); apps = client.Install (n.Get (0)); apps.Start (Seconds (2.0)); apps.Stop (Seconds (10.0)); #if 0 // // Users may find it convenient to initialize echo packets with actual data; // the below lines suggest how to do this // client.SetFill (apps.Get (0), "Hello World"); client.SetFill (apps.Get (0), 0xa5, 1024); uint8_t fill[] = { 0, 1, 2, 3, 4, 5, 6}; client.SetFill (apps.Get (0), fill, sizeof(fill), 1024); #endif AsciiTraceHelper ascii; csma.EnableAsciiAll (ascii.CreateFileStream ("udp-echo.tr")); csma.EnablePcapAll ("udp-echo", false); // // Now, do the actual simulation. // NS_LOG_INFO ("Run Simulation."); Simulator::Run (); Simulator::Destroy (); NS_LOG_INFO ("Done."); }
int main (int argc, char *argv[]) { CommandLine cmd; cmd.Parse (argc, argv); // Here, we will explicitly create four nodes. NS_LOG_INFO ("Create nodes."); NodeContainer c; c.Create (4); // connect all our nodes to a shared channel. NS_LOG_INFO ("Build Topology."); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc")); NetDeviceContainer devs = csma.Install (c); // add an ip stack to all nodes. NS_LOG_INFO ("Add ip stack."); InternetStackHelper ipStack; ipStack.Install (c); // assign ip addresses NS_LOG_INFO ("Assign ip addresses."); Ipv4AddressHelper ip; ip.SetBase ("192.168.1.0", "255.255.255.0"); Ipv4InterfaceContainer addresses = ip.Assign (devs); NS_LOG_INFO ("Create Source"); Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2")); InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3)); OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst); onoff.SetConstantRate (DataRate (15000)); onoff.SetAttribute ("PacketSize", UintegerValue (1200)); ApplicationContainer apps = onoff.Install (c.Get (0)); apps.Start (Seconds (1.0)); apps.Stop (Seconds (10.0)); NS_LOG_INFO ("Create Sink."); PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst); apps = sink.Install (c.Get (3)); apps.Start (Seconds (0.0)); apps.Stop (Seconds (11.0)); NS_LOG_INFO ("Create pinger"); V4PingHelper ping = V4PingHelper (addresses.GetAddress (2)); NodeContainer pingers; pingers.Add (c.Get (0)); pingers.Add (c.Get (1)); pingers.Add (c.Get (3)); apps = ping.Install (pingers); apps.Start (Seconds (2.0)); apps.Stop (Seconds (5.0)); NS_LOG_INFO ("Configure Tracing."); // first, pcap tracing in non-promiscuous mode csma.EnablePcapAll ("csma-ping", false); // then, print what the packet sink receives. Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx", MakeCallback (&SinkRx)); // finally, print the ping rtts. Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt", MakeCallback (&PingRtt)); Packet::EnablePrinting (); NS_LOG_INFO ("Run Simulation."); Simulator::Run (); Simulator::Destroy (); NS_LOG_INFO ("Done."); }