Skip to content

Commit 34cc027

Browse files
committed
Add dataRate for link
1 parent c4dcfea commit 34cc027

8 files changed

Lines changed: 156 additions & 84 deletions

File tree

examples/p4-basic-tunnel.cc

Lines changed: 149 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ unsigned long start = getTickCount();
4040
double global_start_time = 1.0;
4141
double sink_start_time = global_start_time + 1.0;
4242
double client_start_time = sink_start_time + 1.0;
43-
double client_stop_time = client_start_time + 3; // sending time 30s
43+
double client_stop_time = client_start_time + 5; // sending time
4444
double sink_stop_time = client_stop_time + 5;
4545
double global_stop_time = sink_stop_time + 5;
4646

@@ -52,8 +52,10 @@ double first_packet_send_time_tx = 0.0;
5252
double last_packet_send_time_tx = 0.0;
5353
double first_packet_received_time_rx = 0.0;
5454
double last_packet_received_time_rx = 0.0;
55-
uint64_t totalTxBytes = 0;
56-
uint64_t totalRxBytes = 0;
55+
uint64_t totalTxBytes_1 = 0;
56+
uint64_t totalRxBytes_1 = 0;
57+
uint64_t totalTxBytes_2 = 0;
58+
uint64_t totalRxBytes_2 = 0;
5759

5860
// Convert IP address to hexadecimal format
5961
std::string
@@ -86,6 +88,128 @@ ConvertMacToHex(Address macAddr)
8688
return hexStream.str();
8789
}
8890

91+
void
92+
TxCallback(Ptr<const Packet> packet)
93+
{
94+
if (first_tx)
95+
{
96+
// here we just simple jump the first 10 pkts (include some of ARP packets)
97+
first_packet_send_time_tx = Simulator::Now().GetSeconds();
98+
counter_sender_10--;
99+
if (counter_sender_10 == 0)
100+
{
101+
first_tx = false;
102+
}
103+
}
104+
else
105+
{
106+
totalTxBytes_1 += packet->GetSize();
107+
last_packet_send_time_tx = Simulator::Now().GetSeconds();
108+
}
109+
}
110+
111+
void
112+
RxCallback(Ptr<const Packet> packet, const Address& addr)
113+
{
114+
if (first_rx)
115+
{
116+
// here we just simple jump the first 10 pkts (include some of ARP packets)
117+
first_packet_received_time_rx = Simulator::Now().GetSeconds();
118+
counter_receiver_10--;
119+
if (counter_receiver_10 == 0)
120+
{
121+
first_rx = false;
122+
}
123+
}
124+
else
125+
{
126+
totalRxBytes_1 += packet->GetSize();
127+
last_packet_received_time_rx = Simulator::Now().GetSeconds();
128+
}
129+
}
130+
131+
void
132+
TxCallback_2(Ptr<const Packet> packet)
133+
{
134+
if (first_tx)
135+
{
136+
// here we just simple jump the first 10 pkts (include some of ARP packets)
137+
first_packet_send_time_tx = Simulator::Now().GetSeconds();
138+
counter_sender_10--;
139+
if (counter_sender_10 == 0)
140+
{
141+
first_tx = false;
142+
}
143+
}
144+
else
145+
{
146+
totalTxBytes_2 += packet->GetSize();
147+
last_packet_send_time_tx = Simulator::Now().GetSeconds();
148+
}
149+
}
150+
151+
void
152+
RxCallback_2(Ptr<const Packet> packet, const Address& addr)
153+
{
154+
if (first_rx)
155+
{
156+
// here we just simple jump the first 10 pkts (include some of ARP packets)
157+
first_packet_received_time_rx = Simulator::Now().GetSeconds();
158+
counter_receiver_10--;
159+
if (counter_receiver_10 == 0)
160+
{
161+
first_rx = false;
162+
}
163+
}
164+
else
165+
{
166+
totalRxBytes_2 += packet->GetSize();
167+
last_packet_received_time_rx = Simulator::Now().GetSeconds();
168+
}
169+
}
170+
171+
void
172+
PrintFinalThroughput()
173+
{
174+
// 计算传输和接收的时间间隔
175+
double send_time = last_packet_send_time_tx - first_packet_send_time_tx;
176+
double elapsed_time = last_packet_received_time_rx - first_packet_received_time_rx;
177+
178+
// 计算每个传输流的总字节数
179+
uint64_t totalTxBytes = totalTxBytes_1 + totalTxBytes_2;
180+
uint64_t totalRxBytes = totalRxBytes_1 + totalRxBytes_2;
181+
182+
// 避免除零错误
183+
double finalTxThroughput = (send_time > 0) ? (totalTxBytes * 8.0) / (send_time * 1e6) : 0.0;
184+
double finalRxThroughput =
185+
(elapsed_time > 0) ? (totalRxBytes * 8.0) / (elapsed_time * 1e6) : 0.0;
186+
187+
std::cout << "======================================" << std::endl;
188+
std::cout << "Final Simulation Results:" << std::endl;
189+
std::cout << "Client Start Time: " << first_packet_send_time_tx << " s" << std::endl;
190+
std::cout << "Client Stop Time: " << last_packet_send_time_tx << " s" << std::endl;
191+
std::cout << "Sink Start Time: " << first_packet_received_time_rx << " s" << std::endl;
192+
std::cout << "Sink Stop Time: " << last_packet_received_time_rx << " s" << std::endl;
193+
194+
std::cout << "--------------------------------------" << std::endl;
195+
std::cout << "Detailed Bytes Transmitted & Received" << std::endl;
196+
std::cout << "Tx Stream 1: " << totalTxBytes_1 << " bytes" << std::endl;
197+
std::cout << "Tx Stream 2: " << totalTxBytes_2 << " bytes" << std::endl;
198+
std::cout << "Total Transmitted Bytes: " << totalTxBytes << " bytes over " << send_time << " s"
199+
<< std::endl;
200+
201+
std::cout << "Rx Stream 1: " << totalRxBytes_1 << " bytes" << std::endl;
202+
std::cout << "Rx Stream 2: " << totalRxBytes_2 << " bytes" << std::endl;
203+
std::cout << "Total Received Bytes: " << totalRxBytes << " bytes over " << elapsed_time << " s"
204+
<< std::endl;
205+
206+
std::cout << "--------------------------------------" << std::endl;
207+
std::cout << "Final Throughput Metrics" << std::endl;
208+
std::cout << "Final Transmitted Throughput: " << finalTxThroughput << " Mbps" << std::endl;
209+
std::cout << "Final Received Throughput: " << finalRxThroughput << " Mbps" << std::endl;
210+
std::cout << "======================================" << std::endl;
211+
}
212+
89213
// ============================ data struct ============================
90214
struct SwitchNodeC_t
91215
{
@@ -111,7 +235,7 @@ main(int argc, char* argv[])
111235

112236
int running_number = 0;
113237
uint16_t pktSize = 1000; // in Bytes. 1458 to prevent fragments, default 512
114-
std::string appDataRate[] = {"2Mbps", "5Mbps"}; // Default application data rate
238+
std::string appDataRate[] = {"1Mbps", "3Mbps"}; // Default application data rate
115239
std::string ns3_link_rate = "1000Mbps";
116240
bool enableTracePcap = true;
117241

@@ -121,7 +245,7 @@ main(int argc, char* argv[])
121245
"/home/p4/workdir/ns-3-dev-git/contrib/p4sim/examples/p4src/basic_tunnel/";
122246
std::string topoInput =
123247
"/home/p4/workdir/ns-3-dev-git/contrib/p4sim/examples/p4src/basic_tunnel/topo.txt";
124-
std::string topoFormat("CsmaTopo");
248+
std::string topoFormat("P2PTopo");
125249

126250
// ============================ command line ============================
127251
CommandLine cmd;
@@ -156,6 +280,7 @@ main(int argc, char* argv[])
156280

157281
// set default network link parameter
158282
P4PointToPointHelper p4p2phelper;
283+
p4p2phelper.SetDeviceAttribute("DataRate", DataRateValue(DataRate("10Mbps")));
159284
p4p2phelper.SetChannelAttribute("Delay", TimeValue(MilliSeconds(0.01)));
160285

161286
P4TopologyReader::ConstLinksIterator_t iter;
@@ -341,14 +466,19 @@ main(int argc, char* argv[])
341466
app1.Start(Seconds(client_start_time));
342467
app1.Stop(Seconds(client_stop_time));
343468

469+
// === Setup Tracing ===
470+
Ptr<OnOffApplication> ptr_app1 =
471+
DynamicCast<OnOffApplication>(terminals.Get(clientI)->GetApplication(0));
472+
ptr_app1->TraceConnectWithoutContext("Tx", MakeCallback(&TxCallback));
473+
sinkApp1.Get(0)->TraceConnectWithoutContext("Rx", MakeCallback(&RxCallback));
474+
344475
// Normal Stream == Second == send link h0 -----> h1
345-
servPort = 1200; // change the application port
476+
servPort = 1301; // change the application port
346477

347-
// node = terminals.Get (serverI);
348-
// ipv4_adder = node->GetObject<Ipv4> ();
349-
// serverAddr1 =
350-
// ipv4_adder->GetAddress (1, 0)
351-
// .GetLocal (); // Interface index 1 corresponds to the first assigned IP
478+
node = terminals.Get(serverI);
479+
ipv4_adder = node->GetObject<Ipv4>();
480+
serverAddr1 = ipv4_adder->GetAddress(1, 0)
481+
.GetLocal(); // Interface index 1 corresponds to the first assigned IP
352482
InetSocketAddress dst2 = InetSocketAddress(serverAddr1, servPort);
353483
PacketSinkHelper sink2 = PacketSinkHelper("ns3::UdpSocketFactory", dst2);
354484
ApplicationContainer sinkApp2 = sink2.Install(terminals.Get(serverI));
@@ -361,12 +491,18 @@ main(int argc, char* argv[])
361491
onOff2.SetAttribute("DataRate", StringValue(appDataRate[1]));
362492
onOff2.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
363493
onOff2.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
364-
// onOff2.SetAttribute ("MaxBytes", UintegerValue (5000));
494+
// onOff2.SetAttribute("MaxBytes", UintegerValue(500));
365495

366496
ApplicationContainer app2 = onOff2.Install(terminals.Get(clientI));
367497
app2.Start(Seconds(client_start_time));
368498
app2.Stop(Seconds(client_stop_time));
369499

500+
// === Setup Tracing ===
501+
Ptr<OnOffApplication> ptr_app2 =
502+
DynamicCast<OnOffApplication>(terminals.Get(clientI)->GetApplication(0));
503+
ptr_app2->TraceConnectWithoutContext("Tx", MakeCallback(&TxCallback_2));
504+
sinkApp2.Get(0)->TraceConnectWithoutContext("Rx", MakeCallback(&RxCallback_2));
505+
370506
// Enable pcap tracing
371507
p4p2phelper.EnablePcapAll("p4-basic-tunnel");
372508

@@ -383,5 +519,6 @@ main(int argc, char* argv[])
383519
<< std::endl
384520
<< "Run successfully!");
385521

522+
PrintFinalThroughput();
386523
return 0;
387524
}

examples/p4src/basic_tunnel/basic_tunnel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
},
168168
{
169169
"type" : "hexstr",
170-
"value" : "0x0012",
170+
"value" : "0x00dc",
171171
"mask" : null,
172172
"next_state" : "parse_myTunnel"
173173
},

examples/p4src/basic_tunnel/basic_tunnel.p4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
const bit<16> TYPE_ARP = 0x806;
1010
const bit<16> TYPE_IPV4 = 0x800;
11-
const bit<16> TYPE_MYTUNNEL = 0x12; // Same with: static constexpr uint64_t g_p4Protocol = 0x12;
11+
const bit<16> TYPE_MYTUNNEL = 0xDC; // Same with: static constexpr uint64_t g_p4Protocol = 0xDC;
1212

1313
/*************************************************************************
1414
*********************** H E A D E R S ***********************************

examples/p4src/basic_tunnel/basic_tunnel.p4i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ package V1Switch<H, M>(Parser<H, M> p,
891891

892892
const bit<16> TYPE_ARP = 0x806;
893893
const bit<16> TYPE_IPV4 = 0x800;
894-
const bit<16> TYPE_MYTUNNEL = 0x12; // Same with: static constexpr uint64_t g_p4Protocol = 0x12;
894+
const bit<16> TYPE_MYTUNNEL = 0xDC; // Same with: static constexpr uint64_t g_p4Protocol = 0xDC;
895895

896896
/*************************************************************************
897897
*********************** H E A D E R S ***********************************

helper/p4-p2p-helper.cc

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,10 @@
2424
#include "ns3/names.h"
2525
#include "ns3/net-device-queue-interface.h"
2626
#include "ns3/p4-p2p-channel.h"
27+
#include "ns3/p4-p2p-helper.h"
2728
#include "ns3/packet.h"
2829
#include "ns3/queue.h"
2930
#include "ns3/simulator.h"
30-
31-
#ifdef NS3_MPI
32-
#include "ns3/mpi-interface.h"
33-
#include "ns3/mpi-receiver.h"
34-
#include "ns3/point-to-point-remote-channel.h"
35-
#endif
36-
37-
#include "p4-p2p-helper.h"
38-
3931
#include "ns3/trace-helper.h"
4032

4133
namespace ns3
@@ -275,41 +267,7 @@ P4PointToPointHelper::Install(Ptr<Node> a, Ptr<Node> b)
275267
devB->AggregateObject(ndqiB);
276268

277269
Ptr<P4P2PChannel> channel = nullptr;
278-
279-
// If MPI is enabled, we need to see if both nodes have the same system id
280-
// (rank), and the rank is the same as this instance. If both are true,
281-
// use a normal p2p channel, otherwise use a remote channel
282-
#ifdef NS3_MPI
283-
bool useNormalChannel = true;
284-
if (MpiInterface::IsEnabled())
285-
{
286-
uint32_t n1SystemId = a->GetSystemId();
287-
uint32_t n2SystemId = b->GetSystemId();
288-
uint32_t currSystemId = MpiInterface::GetSystemId();
289-
if (n1SystemId != currSystemId || n2SystemId != currSystemId)
290-
{
291-
useNormalChannel = false;
292-
}
293-
}
294-
if (useNormalChannel)
295-
{
296-
m_channelFactory.SetTypeId("ns3::P4P2PChannel");
297-
channel = m_channelFactory.Create<P4P2PChannel>();
298-
}
299-
else
300-
{
301-
m_channelFactory.SetTypeId("ns3::PointToPointRemoteChannel");
302-
channel = m_channelFactory.Create<PointToPointRemoteChannel>();
303-
Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver>();
304-
Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver>();
305-
mpiRecA->SetReceiveCallback(MakeCallback(&CustomP2PNetDevice::Receive, devA));
306-
mpiRecB->SetReceiveCallback(MakeCallback(&CustomP2PNetDevice::Receive, devB));
307-
devA->AggregateObject(mpiRecA);
308-
devB->AggregateObject(mpiRecB);
309-
}
310-
#else
311270
channel = m_channelFactory.Create<P4P2PChannel>();
312-
#endif
313271

314272
devA->Attach(channel);
315273
devB->Attach(channel);

model/custom-p2p-net-device.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ CustomP2PNetDevice::GetDstPort(Ptr<Packet> p)
199199
uint16_t protocol_temp = ip_hd.GetProtocol();
200200
cus_hd.SetProtocolFieldNumber(0);
201201

202-
if (protocol_temp == 0x11)
202+
if (protocol_temp == 0x11) // UDP
203203
{
204204
NS_LOG_DEBUG("UDP protocol, return the dst port number");
205205
UdpHeader udp_hd;
206206
p->PeekHeader(udp_hd);
207207
dst_port = udp_hd.GetDestinationPort();
208208
}
209-
else if (protocol_temp == 0x06)
209+
else if (protocol_temp == 0x06) // TCP
210210
{
211211
NS_LOG_DEBUG("TCP protocol, return the dst port number");
212212
TcpHeader tcp_hd;

model/custom-p2p-net-device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ class CustomP2PNetDevice : public NetDevice
478478

479479
// Protocol number used to identify the custom P4 header in nested packet parsing.
480480
// The previous header's protocol field indicates that the next is a custom header
481-
static constexpr uint16_t m_p4ProtocolNumber = 0x12;
481+
static constexpr uint16_t m_p4ProtocolNumber = 0xDC;
482482

483483
/**
484484
* \brief The Maximum Transmission Unit

model/p4-p2p-channel.cc

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,27 +137,4 @@ P4P2PChannel::IsInitialized(void) const
137137
return true;
138138
}
139139

140-
// void P4P2PChannel::InstallP4Rule(uint32_t ruleId, const std::string& ruleData)
141-
// {
142-
// m_p4Rules[ruleId] = ruleData;
143-
// NS_LOG_INFO("Installed P4 rule with ID " << ruleId << ": " << ruleData);
144-
// }
145-
146-
// void P4P2PChannel::RemoveP4Rule(uint32_t ruleId)
147-
// {
148-
// if (m_p4Rules.erase(ruleId) > 0)
149-
// {
150-
// NS_LOG_INFO("Removed P4 rule with ID " << ruleId);
151-
// }
152-
// else
153-
// {
154-
// NS_LOG_WARN("P4 rule with ID " << ruleId << " not found.");
155-
// }
156-
// }
157-
158-
// std::map<std::string, uint64_t> P4P2PChannel::GetP4Statistics() const
159-
// {
160-
// return m_p4Stats; // Return collected statistics
161-
// }
162-
163140
} // namespace ns3

0 commit comments

Comments
 (0)