From d555c963d98f348bd3145c988e21cdfe018c1085 Mon Sep 17 00:00:00 2001 From: charliebarber Date: Mon, 8 Apr 2024 12:13:16 +0100 Subject: [PATCH] Packet dropped counting --- .gitignore.swp | Bin 12288 -> 0 bytes pcap_analysis/process_pcap.py | 220 +++++++++++++++++++--------------- 2 files changed, 121 insertions(+), 99 deletions(-) delete mode 100644 .gitignore.swp diff --git a/.gitignore.swp b/.gitignore.swp deleted file mode 100644 index 736f4ff0473c03bda6c51da668f0a256ef62ada5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2y>HV%7>6&Y6A~Y4BqRnT(+r@9#3@-|C20i`QKJM{(b>MF7o40qpHVu{ihl#9 zPVDTIg#m^Rh!u7u7=Vch@w1akp(;biir-1kd;WOu_3KCSVA(lk>BbwJ&i$}e(a@i{{-#=Cf zCer{7&;Sk401eOp4bT7$oNNOwDTy82!m0UfT$#_ML-Tx>6AjP+4bT7$&;Sk401eOp z4bT7$(7*{aAR{48pA}*ciW z(4YtIf;G?vSHTLn1TKOySO!brH+uL5eu5w13-}B^fe+w4$oqT)IB0+dXn+Q2fCgxQ z255i=Xn+R(Cj-^`leGAayeg`79VzK!ZucMdUGe95#dB7MD)pCi)xl~&JIq+=5yJ&zS{-B?_A z;$bg~l{dhuqHGk$$ceUQxa1#rLh!wAERd~?BZ6zaRi<%}FA;o|= a4=Lh8s~f>cg>IpPSs|+)XFAmmZT$h59Hs66 diff --git a/pcap_analysis/process_pcap.py b/pcap_analysis/process_pcap.py index 7748a69b..564cf156 100644 --- a/pcap_analysis/process_pcap.py +++ b/pcap_analysis/process_pcap.py @@ -19,33 +19,6 @@ def get_IP(packets): receiver_IP = packets[0].getlayer('IP').dst return sender_IP, receiver_IP - -# def packet_transfer_time(sender_packets, receiver_packets): -# timestamp = [] -# completion_time = [] -# stream_direction = [] -# -# for sender_packet in sender_packets: -# for receiver_packet in receiver_packets: -# if (sender_packet.getlayer('TCP').seq == receiver_packet.getlayer('TCP').seq) and ( -# sender_packet.getlayer('TCP').ack == receiver_packet.getlayer('TCP').ack) and ( -# sender_packet.getlayer('IP').src == receiver_packet.getlayer('IP').src) and ( -# sender_packet.getlayer('IP').dst == receiver_packet.getlayer('IP').dst) and ( -# sender_packet.getlayer('TCP').window == receiver_packet.getlayer('TCP').window) and ( -# sender_packet.getlayer('TCP').options == receiver_packet.getlayer('TCP').options) and ( -# sender_packet.getlayer('TCP').options[0][1] == receiver_packet.getlayer('TCP').options[0][1]): -# if sender_packet.time > receiver_packet.time: -# completion_time.append(sender_packet.time - receiver_packet.time) -# stream_direction.append('down') -# else: -# completion_time.append(receiver_packet.time - sender_packet.time) -# stream_direction.append('up') -# timestamp.append(sender_packet.time) -# break -# -# return timestamp, completion_time, stream_direction - - def flow_completion_time(packets): TCP_FIN = 0x01 TCP_ACK = 0x10 @@ -58,15 +31,6 @@ def flow_completion_time(packets): return pkt.time return None - -# def save_plot(timestamp, completion_time, stream_direction): -# os.makedirs("../pcap_analysis/plots", exist_ok=True) -# plt.plot(timestamp, completion_time, label=stream_direction, marker='o') -# plt.xlabel("time stamp of the sender of the packet") -# plt.ylabel("transfer completion time") -# -# plt.savefig("../pcap_analysis/plots/completion_time-timestamp.png") - def get_avg_fc_time(source_directory, senders=1): seeds = os.listdir(source_directory) fc_time = 0.0 @@ -165,7 +129,30 @@ def plot_flow_comp_time(results, case, mode, senders): plt.savefig(f"../pcap_analysis/plots/{mode}-{case}-senders{senders}.png", dpi=300) plt.clf() +def plot_packet_loss(results, case, mode, senders): + plt.figure(figsize=(12, 6)) # Adjust figure size as needed + + for queue_size in queue_sizes: + # Get results for the current queue size + packet_loss_data = get_scaling_results(results, case, queue_size, senders) + + # Sort variables for consistent plotting + variables_sorted = sorted(packet_loss_data.keys()) + packet_losses = [packet_loss_data[var] for var in variables_sorted] + + # Plotting + plt.plot(variables_sorted, packet_losses, label=f"Senders: {senders}, Queue Size: {queue_size}", + marker='o') # Adjust marker style as desired + + plt.title(f"Packet Loss - ({case} scenario)") + plt.xlabel(mode) + plt.ylabel("Total Packet Loss") + plt.legend() + plt.grid(True) + # Save the plot + plt.savefig(f"../pcap_analysis/plots/packet_loss_{mode}_{case}_senders{senders}.png", dpi=300) + plt.close() def plot_flow_completion_time(results, mode, cases): sorted_results = sorted(results, @@ -240,72 +227,107 @@ def plot_flow_completion_time(results, mode, cases): figure.savefig(f"../pcap_analysis/plots/{mode}-{cases[0]}-{cases[1]}.png", dpi=300) +def count_packets_by_port(packets, src_port, dst_port): + count = 0 + for pkt in packets: + if UDP in pkt or TCP in pkt: + if pkt[IP].sport == src_port and pkt[IP].dport == dst_port: + count += 1 + return count + +def calculate_packet_loss(sent_count, received_count): + diff = sent_count - received_count + return diff if diff > 0 else 0 + +def get_avg_packets_dropped(source_directory, senders=1): + seeds = os.listdir(source_directory) + total_packet_loss_udp = 0 + total_packet_loss_tcp = 0 + + for seed in seeds: + # Calculate paths for UDP and TCP senders based on number of senders + udp_sender_path = os.path.join(source_directory, seed, f"{senders}", "-CongestionSender-1.pcap") + if os.path.exists(udp_sender_path): + total_packet_loss_udp += count_packets_by_port(read_pcap(udp_sender_path), 49153, 50001) + + # Loop over the number of TCP sender configurations + tcp_sender_count = 0 + for i in range(senders): + tcp_sender_path = os.path.join(source_directory, seed, f"{senders}", f"-TrafficSender{i}-1.pcap") + if os.path.exists(tcp_sender_path): + total_packet_loss_tcp += count_packets_by_port(read_pcap(tcp_sender_path), 49153, 50002) + tcp_sender_count += 1 + + # Consider received packets at receiver + receiver_path = os.path.join(source_directory, seed, f"{senders}", "-Receiver-1.pcap") + if os.path.exists(receiver_path): + packets = read_pcap(receiver_path) + received_udp = count_packets_by_port(packets, 49153, 50001) + received_tcp = count_packets_by_port(packets, 49153, 50002) + + # Update total packet loss based on received counts + total_packet_loss_udp += calculate_packet_loss(total_packet_loss_udp, received_udp) + total_packet_loss_tcp += calculate_packet_loss(total_packet_loss_tcp * tcp_sender_count, received_tcp) + + avg_udp_loss = total_packet_loss_udp / len(seeds) if seeds else 0 + avg_tcp_loss = total_packet_loss_tcp / (len(seeds) * senders) if seeds else 0 + + return avg_udp_loss + avg_tcp_loss + +def record_packet_loss(source_directory, result_directory, mode): + variables = os.listdir(source_directory) + results = {i: {case: generate_variable_dict(variables) for case in cases} for i in (1, 3)} + + for senders in (1, 3): + for variable in variables: + if os.path.isdir(os.path.join(source_directory, variable)): + queue_sizes = os.listdir(source_directory + variable) + for case in cases: + for queue_size in queue_sizes: + if os.path.isdir(os.path.join(source_directory + variable + "/" + queue_size)) and queue_size != '99': + if case == "udp": + results[senders][case][variable][queue_size] = get_avg_packets_dropped(f"{source_directory}{variable}/{queue_size}/frr", senders) + else: + results[senders][case][variable][queue_size] = get_avg_packets_dropped(f"{source_directory}{variable}/{queue_size}/frr-{case}", senders) + results[senders][case][variable]['base'] = get_avg_packets_dropped(f"{source_directory}{variable}/20/baseline-{case}", senders) + + if __name__ == '__main__': os.makedirs("../pcap_analysis/results", exist_ok=True) os.makedirs("../pcap_analysis/plots", exist_ok=True) - bandwidth_primary_results = record_flow_completion_time("../traces/bandwidth_primary/", + # bandwidth_primary_results = record_flow_completion_time("../traces/bandwidth_primary/", + # "../pcap_analysis/results", "bandwidth_primary") + # plot_flow_comp_time(bandwidth_primary_results, "udp", "bandwidth_primary", 1) + # plot_flow_comp_time(bandwidth_primary_results, "udp", "bandwidth_primary", 3) + # plot_flow_comp_time(bandwidth_primary_results, "no-udp", "bandwidth_primary", 1) + # plot_flow_comp_time(bandwidth_primary_results, "no-udp", "bandwidth_primary", 3) + + # bandwidth_alternate_results = record_flow_completion_time("../traces/bandwidth_alternate/", + # "../pcap_analysis/results", "bandwidth_alternate") + # plot_flow_comp_time(bandwidth_alternate_results, "udp", "bandwidth_alternate", 1) + # plot_flow_comp_time(bandwidth_alternate_results, "udp", "bandwidth_alternate", 3) + # plot_flow_comp_time(bandwidth_alternate_results, "no-udp", "bandwidth_alternate", 1) + # plot_flow_comp_time(bandwidth_alternate_results, "no-udp", "bandwidth_alternate", 3) + + # delay_primary_results = record_flow_completion_time("../traces/delay_primary/", + # "../pcap_analysis/results", "delay_primary") + # plot_flow_comp_time(delay_primary_results, "udp", "delay_primary", 1) + # plot_flow_comp_time(delay_primary_results, "udp", "delay_primary", 3) + # plot_flow_comp_time(delay_primary_results, "no-udp", "delay_primary", 1) + # plot_flow_comp_time(delay_primary_results, "no-udp", "delay_primary", 3) + + # delay_alternate_results = record_flow_completion_time("../traces/delay_alternate/", + # "../pcap_analysis/results", "delay_alternate") + # plot_flow_comp_time(delay_alternate_results, "udp", "delay_alternate", 1) + # plot_flow_comp_time(delay_alternate_results, "udp", "delay_alternate", 3) + # plot_flow_comp_time(delay_alternate_results, "no-udp", "delay_alternate", 1) + # plot_flow_comp_time(delay_alternate_results, "no-udp", "delay_alternate", 3) + + bandwidth_primary_packet_loss = record_packet_loss("../traces/bandwidth_primary/", "../pcap_analysis/results", "bandwidth_primary") - plot_flow_comp_time(bandwidth_primary_results, "udp", "bandwidth_primary", 1) - plot_flow_comp_time(bandwidth_primary_results, "udp", "bandwidth_primary", 3) - plot_flow_comp_time(bandwidth_primary_results, "no-udp", "bandwidth_primary", 1) - plot_flow_comp_time(bandwidth_primary_results, "no-udp", "bandwidth_primary", 3) - - #plot_flow_completion_time(bandwidth_primary_results, "bandwidth_primary", ['baseline_no_udp', 'frr_no_udp']) - # no udp - #plot_flow_completion_time(bandwidth_primary_results, "bandwidth_primary", ['baseline_udp', 'frr']) - # with udp - - bandwidth_alternate_results = record_flow_completion_time("../traces/bandwidth_alternate/", - "../pcap_analysis/results", "bandwidth_alternate") - plot_flow_comp_time(bandwidth_alternate_results, "udp", "bandwidth_alternate", 1) - plot_flow_comp_time(bandwidth_alternate_results, "udp", "bandwidth_alternate", 3) - plot_flow_comp_time(bandwidth_alternate_results, "no-udp", "bandwidth_alternate", 1) - plot_flow_comp_time(bandwidth_alternate_results, "no-udp", "bandwidth_alternate", 3) - #plot_flow_completion_time(bandwidth_alternate_results, "bandwidth_alternate", ['baseline_no_udp', 'frr_no_udp']) - #plot_flow_completion_time(bandwidth_alternate_results, "bandwidth_alternate", ['baseline_udp', 'frr']) - - #delay_all_results = record_flow_completion_time("../traces/delay_all/", - # "../pcap_analysis/results", "delay_all") - #plot_flow_completion_time(delay_all_results, "delay_all", ['baseline_no_udp', 'frr_no_udp']) - #plot_flow_completion_time(delay_all_results, "delay_all", ['baseline_udp', 'frr']) - - delay_primary_results = record_flow_completion_time("../traces/delay_primary/", - "../pcap_analysis/results", "delay_primary") - plot_flow_comp_time(delay_primary_results, "udp", "delay_primary", 1) - plot_flow_comp_time(delay_primary_results, "udp", "delay_primary", 3) - plot_flow_comp_time(delay_primary_results, "no-udp", "delay_primary", 1) - plot_flow_comp_time(delay_primary_results, "no-udp", "delay_primary", 3) - - delay_alternate_results = record_flow_completion_time("../traces/delay_alternate/", - "../pcap_analysis/results", "delay_alternate") - plot_flow_comp_time(delay_alternate_results, "udp", "delay_alternate", 1) - plot_flow_comp_time(delay_alternate_results, "udp", "delay_alternate", 3) - plot_flow_comp_time(delay_alternate_results, "no-udp", "delay_alternate", 1) - plot_flow_comp_time(delay_alternate_results, "no-udp", "delay_alternate", 3) - #delay_alternate_results = record_flow_completion_time("../traces/delay_alternate/", -# "../pcap_analysis/results", "delay_alternate") - #plot_flow_completion_time(delay_alternate_results, "delay_alternate", ['baseline_no_udp', 'frr_no_udp']) - #plot_flow_completion_time(delay_alternate_results, "delay_alternate", ['baseline_udp', 'frr']) - - # results = [] - # - # for file in files: - # if file.endswith(".pcap"): - # senderPackets = read_pcap("traces/" + file) - # senderIP, receiverIP = get_IP(senderPackets) - # - # # timestamp, completion_time, stream_direction = packet_transfer_time(sender_packets, receiver_packets) - # - # fc_time = flow_completion_time(senderPackets) - # result = file + ":" + str(fc_time) + "\n" - # print(result) - # results.append(result) - # # save_plot(timestamp, completion_time, stream_direction) - # - # os.makedirs("../pcap_analysis/results", exist_ok=True) - # - # with open("../pcap_analysis/results/results.txt", "w") as f: - # for result in results: - # f.write(result) + plot_packet_loss(bandwidth_primary_packet_loss, "udp", "bandwidth_primary", 1) + plot_packet_loss(bandwidth_primary_packet_loss, "udp", "bandwidth_primary", 3) + +