@@ -40,7 +40,7 @@ unsigned long start = getTickCount();
4040double global_start_time = 1.0 ;
4141double sink_start_time = global_start_time + 1.0 ;
4242double 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
4444double sink_stop_time = client_stop_time + 5 ;
4545double global_stop_time = sink_stop_time + 5 ;
4646
@@ -52,8 +52,10 @@ double first_packet_send_time_tx = 0.0;
5252double last_packet_send_time_tx = 0.0 ;
5353double first_packet_received_time_rx = 0.0 ;
5454double 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
5961std::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 ============================
90214struct 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}
0 commit comments