-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss-model-analysis.py
72 lines (58 loc) · 2.57 KB
/
loss-model-analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pandas as pd
from scapy.all import rdpcap, TCP, IP
def extract_packets(pcap_file, src_ip, dst_ip):
packets = rdpcap(pcap_file)
packet_list = []
for pkt in packets:
if IP in pkt and TCP in pkt:
if pkt[IP].src == src_ip and pkt[IP].dst == dst_ip:
seq = pkt[TCP].seq
ip_id = pkt[IP].id
timestamp = pkt.time
packet_list.append({
'seq': seq,
'ip_id': ip_id,
'timestamp': timestamp
})
return pd.DataFrame(packet_list)
def main():
# Define IP addresses
sender_ip = '10.0.1.1' # IP of sender
receiver_ip = '10.0.2.2' # IP of receiver
# Extract packets sent from sender to receiver
sender_packets = extract_packets('RED-bw1Mb-dlay100-b45p.pcap', sender_ip, receiver_ip)
# Read packet_drop file
drop_df = pd.read_csv('RED-bw1Mb-dlay100-b45p-drp.tr', sep='\t', header=None, names=['time','seq'])
# Get drop counts for each sequence number
drop_counts = drop_df['seq'].value_counts().to_dict()
# Sort sender_packets by timestamp
sender_packets = sender_packets.sort_values('timestamp').reset_index(drop=True)
sender_packets['dropped'] = False
# Copy drop_counts to avoid modifying the original
current_drop_counts = drop_counts.copy()
# Process packets to mark drops
for idx, row in sender_packets.iterrows():
seq = row['seq']
if seq in current_drop_counts and current_drop_counts[seq] > 0:
# Mark packet as dropped
sender_packets.at[idx, 'dropped'] = True
# Decrement drop count for this sequence number
current_drop_counts[seq] -= 1
# Calculate unconditional probability of drop
P_uncond = sender_packets['dropped'].mean()
# Calculate conditional probability of drop given previous packet was dropped
sender_packets['prev_dropped'] = sender_packets['dropped'].shift(1).fillna(False)
both_dropped = sender_packets['dropped'] & sender_packets['prev_dropped']
N_prev_dropped = sender_packets['prev_dropped'].sum()
N_both_dropped = both_dropped.sum()
if N_prev_dropped > 0:
P_cond = N_both_dropped / N_prev_dropped
else:
P_cond = 0
print(f"Unconditional probability: {P_uncond:.4f}")
print(f"Conditional probability: {P_cond:.4f}")
# Optionally, save dropped packets to a CSV file
# dropped_packets = sender_packets[sender_packets['dropped']]
# dropped_packets.to_csv('dropped_packets.csv', index=False)
if __name__ == '__main__':
main()