Skip to content

Commit

Permalink
Packet dropped counting
Browse files Browse the repository at this point in the history
  • Loading branch information
charliebarber committed Apr 8, 2024
1 parent 23ec141 commit d555c96
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 99 deletions.
Binary file removed .gitignore.swp
Binary file not shown.
220 changes: 121 additions & 99 deletions pcap_analysis/process_pcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)


0 comments on commit d555c96

Please sign in to comment.