Skip to content

Commit 17db0a6

Browse files
committed
[Example] Finish Tunnel example
1 parent e3851c2 commit 17db0a6

9 files changed

Lines changed: 237 additions & 430 deletions

File tree

examples/p4-basic-test-p2p.cc

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,60 @@
1414

1515
#include <iomanip>
1616

17+
/**
18+
(p4dev-python-venv) p4@p4:~/workdir/ns3.35$ ./waf --run "p4-basic-test-p2p"
19+
/home/p4/workdir/ns3.35/bindings/python/wscript:321: SyntaxWarning: invalid escape sequence '\d'
20+
m = re.match( "^castxml version (\d\.\d)(-)?(\w+)?", castxml_version_line)
21+
Waf: Entering directory `/home/p4/workdir/ns3.35/build'
22+
[1975/2937] Compiling contrib/p4sim/model/bridge-p4-net-device.cc
23+
[2884/2937] Linking build/lib/libns3.35-p4sim-debug.so
24+
[2885/2937] Linking build/lib/libns3.35-p4sim-test-debug.so
25+
[2886/2937] Linking build/contrib/p4sim/examples/ns3.35-p4sim-example-debug
26+
[2887/2937] Linking build/contrib/p4sim/examples/ns3.35-p4-basic-test-debug
27+
[2888/2937] Linking build/contrib/p4sim/examples/ns3.35-p4-basic-test-p2p-debug
28+
[2889/2937] Linking build/contrib/p4sim/examples/ns3.35-p4-custom-header-test-debug
29+
[2890/2937] Linking build/contrib/p4sim/examples/ns3.35-p4-basic-example-debug
30+
[2891/2937] Linking build/contrib/p4sim/examples/ns3.35-p4-basic-tunnel-example-debug
31+
[2892/2937] Linking build/contrib/p4sim/examples/ns3.35-p4-p2p-custom-header-test-debug
32+
[2893/2937] Linking build/contrib/p4sim/examples/ns3.35-p4-custom-test-debug
33+
[2894/2937] Linking build/scratch/subdir/subdir
34+
[2895/2937] Linking build/scratch/simple-on-off
35+
[2896/2937] Linking build/scratch/scratch-simulator
36+
[2897/2937] Linking build/utils/ns3.35-test-runner-debug
37+
Waf: Leaving directory `/home/p4/workdir/ns3.35/build'
38+
Build commands will be stored in build/compile_commands.json
39+
'build' finished successfully (10.030s)
40+
*** Reading topology from file: /home/p4/workdir/ns3.35/contrib/p4sim/test/custom_header/topo.txt with format: P2PTopo
41+
*** Host number: 2, Switch number: 2
42+
*** Link from host 2 to switch0 with data rate and delay
43+
*** Link from switch 0 to switch 1 with data rate and delay
44+
*** Link from host 3 to switch1 with data rate and delay
45+
host 0: connect with switch 0 port 0
46+
host 1: connect with switch 1 port 1
47+
switch 0 connect with: h0 s1_0
48+
switch 1 connect with: s0_1 h1
49+
Node IP and MAC addresses:
50+
Node 0: IP = 10.1.1.1, MAC = 00:00:00:00:00:01
51+
Node 0: IP = 0x0a010101, MAC = 0x000000000001
52+
Node 1: IP = 10.1.1.2, MAC = 00:00:00:00:00:05
53+
Node 1: IP = 0x0a010102, MAC = 0x000000000005
54+
Host 0 NetDevice is CustomP2PNetDevice, Setting for the Tunnel Header!
55+
Host 1 NetDevice is CustomP2PNetDevice, Setting for the Tunnel Header!
56+
*** P4Simulator mode
57+
*** Installing P4 bridge [ 0 ] device with configuration:
58+
P4JsonPath = /home/p4/workdir/ns3.35/contrib/p4sim/test/custom_header/custom_header.json,
59+
FlowTablePath = /home/p4/workdir/ns3.35/contrib/p4sim/test/custom_header/flowtable_0.txt,
60+
ViewFlowTablePath = /home/p4/workdir/ns3.35/contrib/p4sim/test/custom_header/flowtable_0.txt
61+
*** Installing P4 bridge [ 1 ] device with configuration:
62+
P4JsonPath = /home/p4/workdir/ns3.35/contrib/p4sim/test/custom_header/custom_header.json,
63+
FlowTablePath = /home/p4/workdir/ns3.35/contrib/p4sim/test/custom_header/flowtable_1.txt,
64+
ViewFlowTablePath = /home/p4/workdir/ns3.35/contrib/p4sim/test/custom_header/flowtable_1.txt
65+
Running simulation...
66+
Simulate Running time: 99ms
67+
Total Running time: 10299ms
68+
Run successfully!
69+
*/
70+
1771
using namespace ns3;
1872

1973
NS_LOG_COMPONENT_DEFINE ("P4BasicTestP2P");
@@ -73,11 +127,11 @@ main (int argc, char *argv[])
73127

74128
// ============================ parameters ============================
75129

76-
ns3::PacketMetadata::Enable (); // 开启数据包元数据追踪
130+
// ns3::PacketMetadata::Enable (); // 开启数据包元数据追踪
77131

78132
LogComponentEnable ("P4BasicTestP2P", LOG_LEVEL_INFO);
79-
LogComponentEnable ("CustomP2PNetDevice", LOG_LEVEL_DEBUG);
80-
LogComponentEnable ("BridgeP4NetDevice", LOG_LEVEL_DEBUG);
133+
// LogComponentEnable ("CustomP2PNetDevice", LOG_LEVEL_DEBUG);
134+
// LogComponentEnable ("BridgeP4NetDevice", LOG_LEVEL_DEBUG);
81135

82136
// ============================ parameters ============================
83137

examples/p4-basic-tunnel-example.cc

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
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...");

examples/p4-custom-test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ main (int argc, char *argv[])
7575

7676
// ============================ parameters ============================
7777

78-
ns3::PacketMetadata::Enable (); // 开启数据包元数据追踪
78+
// ns3::PacketMetadata::Enable (); // 开启数据包元数据追踪
7979

8080
// Simulation parameters
8181
uint16_t pktSize = 1000; //in Bytes. 1458 to prevent fragments, default 512

helper/global.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,20 @@ class P4GlobalVar : public Object
6363
static std::string g_flowTablePath;
6464
static std::string g_viewFlowTablePath;
6565
static std::string g_p4JsonPath;
66+
67+
// Protocol number used to identify the custom P4 header in nested packet parsing.
68+
// The previous header's protocol field indicates that the next is a custom header
6669
static constexpr uint64_t g_p4Protocol = 0x12;
6770

68-
// Here if dst_port of the udp/tcp header in [g_portRangeMin, g_portRangeMax],
69-
// the packet will add the custom header.
71+
// Port range used to determine whether a packet should include the custom header.
72+
// In NS-3, the same port may handle different types of packets.
73+
// If the destination port in the UDP/TCP header falls within [g_portRangeMin, g_portRangeMax],
74+
// the packet will be assigned a custom header.
7075
static constexpr uint64_t g_portRangeMin = 10000;
7176
static constexpr uint64_t g_portRangeMax = 20000;
7277

73-
// Custom header fields
78+
// Defines the structure and field lengths of the custom header.
79+
// Users should modify this based on simulation requirements.
7480
static constexpr std::array<std::pair<const char *, uint32_t>, 2> g_templateHeaderFields = {
7581
{{"proto_id", 16}, {"dst_id", 16}}};
7682

0 commit comments

Comments
 (0)