11#include " ns3/core-module.h"
22#include " ns3/network-module.h"
33#include " ns3/applications-module.h"
4- #include " ns3/csma -helper.h"
4+ #include " ns3/p4-p2p -helper.h"
55#include " ns3/internet-module.h"
66#include " ns3/bridge-helper.h"
77#include " ns3/p4-helper.h"
88#include " ns3/global.h"
99
1010#include " ns3/custom-header.h"
11+ #include " ns3/custom-p2p-net-device.h"
1112#include " ns3/format-utils.h"
1213#include " ns3/p4-topology-reader-helper.h"
1314
@@ -74,7 +75,7 @@ main (int argc, char *argv[])
7475 // ============================ parameters ============================
7576
7677 // Simulation parameters
77- uint16_t pktSize = 512 ; // in Bytes. 1458 to prevent fragments, default 512
78+ uint16_t pktSize = 1000 ; // in Bytes. 1458 to prevent fragments, default 512
7879
7980 // h1 -> h3 with 2.0Mbps, h2 -> h4 with 1.0Mbps
8081 std::string appDataRate[] = {" 2.0Mbps" , " 1.0Mbps" };
@@ -127,9 +128,11 @@ main (int argc, char *argv[])
127128 NS_LOG_INFO (" *** Host number: " << hostNum << " , Switch number: " << switchNum);
128129
129130 // set default network link parameter
130- CsmaHelper csma;
131- csma.SetChannelAttribute (" DataRate" , StringValue (" 10Mbps" )); // @todo
132- csma.SetChannelAttribute (" Delay" , TimeValue (MilliSeconds (0.01 )));
131+ // CsmaHelper csma;
132+ // csma.SetChannelAttribute ("DataRate", StringValue ("10Mbps")); //@todo
133+ // csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (0.01)));
134+ P4PointToPointHelper p4p2phelper;
135+ p4p2phelper.SetChannelAttribute (" Delay" , TimeValue (MilliSeconds (0.01 )));
133136
134137 // ============================ init network link info ============================
135138 P4TopologyReader::ConstLinksIterator_t iter;
@@ -139,13 +142,13 @@ main (int argc, char *argv[])
139142 std::string dataRate, delay;
140143 for (iter = topoReader->LinksBegin (); iter != topoReader->LinksEnd (); iter++)
141144 {
142- if (iter->GetAttributeFailSafe (" DataRate" , dataRate))
143- csma.SetChannelAttribute (" DataRate" , StringValue (dataRate));
144- if (iter->GetAttributeFailSafe (" Delay" , delay))
145- csma.SetChannelAttribute (" Delay" , StringValue (delay));
145+ // if (iter->GetAttributeFailSafe ("DataRate", dataRate))
146+ // csma.SetChannelAttribute ("DataRate", StringValue (dataRate));
147+ // if (iter->GetAttributeFailSafe ("Delay", delay))
148+ // csma.SetChannelAttribute ("Delay", StringValue (delay));
146149
147150 NetDeviceContainer link =
148- csma .Install (NodeContainer (iter->GetFromNode (), iter->GetToNode ()));
151+ p4p2phelper .Install (NodeContainer (iter->GetFromNode (), iter->GetToNode ()));
149152 fromIndex = iter->GetFromIndex ();
150153 toIndex = iter->GetToIndex ();
151154 if (iter->GetFromType () == ' s' && iter->GetToType () == ' s' )
@@ -319,39 +322,35 @@ main (int argc, char *argv[])
319322 }
320323 }
321324
322- // // Custom defined header for P4 switch (for the p4 code)
323- //
324- // header myTunnel_t
325- // {
326- // bit<16> proto_id;
327- // bit<16> dst_id;
328- // }
329- //
330- // struct headers
331- // {
332- // ethernet_t ethernet;
333- // arp_t arp;
334- // myTunnel_t myTunnel;
335- // ipv4_t ipv4;
336- // }
337-
338- // Step 1: Create an instance of CustomHeader for myTunnel_t
325+ // ============================ add custom header for the p4 switch ============================
326+
339327 CustomHeader myTunnelHeader;
340- myTunnelHeader.SetLayer (LAYER_3); // Network Layer
328+ myTunnelHeader.SetLayer (HeaderLayer:: LAYER_3); // Network Layer
341329 myTunnelHeader.SetOperator (ADD_BEFORE); // add before the ipv4 header
342330
343- // Step 2: Define the fields for myTunnel_t
344- myTunnelHeader.AddField (" proto_id" , 16 ); // Protocol ID: 16 bits
345- myTunnelHeader.AddField (" dst_id" , 16 ); // Destination ID: 16 bits
346-
347- // Step 3: Set values for the fields
348331 myTunnelHeader.SetField (" proto_id" , 0x0800 ); // Example: IPv4 protocol
349- myTunnelHeader.SetField (" dst_id" , 42 ); // Example: Destination ID
332+ myTunnelHeader.SetField (" dst_id" , 0x22 ); // Example: Destination ID
333+
334+ // Set for the NetDevice
335+ for (unsigned int i = 0 ; i < hostNum; i++)
336+ {
337+ Ptr<NetDevice> device = hostNodes[i].hostDevice .Get (0 );
338+ if (device->GetObject <CustomP2PNetDevice> ())
339+ {
340+ NS_LOG_DEBUG (
341+ " Host " << i << " NetDevice is CustomP2PNetDevice, Setting for the Tunnel Header!" );
342+ Ptr<CustomP2PNetDevice> customDevice = DynamicCast<CustomP2PNetDevice> (device);
343+ customDevice->SetWithCustomHeader (true );
344+ customDevice->SetCustomHeader (myTunnelHeader);
345+ }
346+ }
347+
348+ // ============================ add application to the hosts ============================
350349
351- // == First == send link h0 -----> h1
350+ // TUNNEL stream == First == send link h0 -----> h1
352351 unsigned int serverI = 1 ;
353352 unsigned int clientI = 0 ;
354- uint16_t servPort = 9093 ; // setting for port
353+ uint16_t servPort = 12000 ; // setting for port
355354
356355 Ptr<Node> node = terminals.Get (serverI);
357356 Ptr<Ipv4> ipv4_adder = node->GetObject <Ipv4> ();
@@ -370,14 +369,40 @@ main (int argc, char *argv[])
370369 onOff1.SetAttribute (" DataRate" , StringValue (appDataRate[0 ]));
371370 onOff1.SetAttribute (" OnTime" , StringValue (" ns3::ConstantRandomVariable[Constant=1]" ));
372371 onOff1.SetAttribute (" OffTime" , StringValue (" ns3::ConstantRandomVariable[Constant=0]" ));
373- onOff1.SetAttribute (" MaxBytes" , UintegerValue (5000 ));
372+ onOff1.SetAttribute (" MaxBytes" , UintegerValue (3000 ));
374373
375374 ApplicationContainer app1 = onOff1.Install (terminals.Get (clientI));
376375 app1.Start (Seconds (client_start_time));
377376 app1.Stop (Seconds (client_stop_time));
378377
378+ // Normal Stream == Second == send link h0 -----> h1
379+ servPort = 1200 ; // change the application port
380+
381+ // node = terminals.Get (serverI);
382+ // ipv4_adder = node->GetObject<Ipv4> ();
383+ // serverAddr1 =
384+ // ipv4_adder->GetAddress (1, 0)
385+ // .GetLocal (); // Interface index 1 corresponds to the first assigned IP
386+ InetSocketAddress dst2 = InetSocketAddress (serverAddr1, servPort);
387+ PacketSinkHelper sink2 = PacketSinkHelper (" ns3::UdpSocketFactory" , dst2);
388+ ApplicationContainer sinkApp2 = sink2.Install (terminals.Get (serverI));
389+
390+ sinkApp2.Start (Seconds (sink_start_time));
391+ sinkApp2.Stop (Seconds (sink_stop_time));
392+
393+ OnOffHelper onOff2 (" ns3::UdpSocketFactory" , dst2);
394+ onOff2.SetAttribute (" PacketSize" , UintegerValue (pktSize));
395+ onOff2.SetAttribute (" DataRate" , StringValue (appDataRate[0 ]));
396+ onOff2.SetAttribute (" OnTime" , StringValue (" ns3::ConstantRandomVariable[Constant=1]" ));
397+ onOff2.SetAttribute (" OffTime" , StringValue (" ns3::ConstantRandomVariable[Constant=0]" ));
398+ onOff2.SetAttribute (" MaxBytes" , UintegerValue (5000 ));
399+
400+ ApplicationContainer app2 = onOff2.Install (terminals.Get (clientI));
401+ app2.Start (Seconds (client_start_time));
402+ app2.Stop (Seconds (client_stop_time));
403+
379404 // Enable pcap tracing
380- csma .EnablePcapAll (" p4-basic-tunnel-example" );
405+ p4p2phelper .EnablePcapAll (" p4-basic-tunnel-example" );
381406
382407 // Run simulation
383408 NS_LOG_INFO (" Running simulation..." );
0 commit comments