Skip to content

Commit

Permalink
Fix a multithreading issue in writing pcap files (#204)
Browse files Browse the repository at this point in the history
When there are separate threads that are sending and receiving packets,
both threads may try to write the packet data to the pcap file. This
causes the packet to get interleaved, thus basically making it corrupted
and unable to be read by tcpdump, wireshark, etc.

This is happening due to a missing lock on `self.cvar`. Fix it by
wrapping the write call in the `send` method and the initial assignment
in `start_pcap` method with a lock on `self.cvar`.

Signed-off-by: Saikrishna Arcot <[email protected]>
  • Loading branch information
saiarcot895 authored Jan 24, 2024
1 parent 252facb commit e8b545f
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/ptf/dataplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,9 @@ def send(self, device_number, port_number, packet):
self.logger.warn(
"The %s kernel may not send packets smaller than 15 bytes", sys.platform
)
if self.pcap_writer:
self.pcap_writer.write(packet, time.time(), device_number, port_number)
with self.cvar:
if self.pcap_writer:
self.pcap_writer.write(packet, time.time(), device_number, port_number)
bytes = self.ports[(device_number, port_number)].send(packet)
self.tx_counters[(device_number, port_number)] += 1
if bytes != len(packet):
Expand Down Expand Up @@ -1020,8 +1021,9 @@ def flush(self):
self.packet_queues[port_id] = []

def start_pcap(self, filename):
assert self.pcap_writer == None
self.pcap_writer = PcapWriter(filename)
with self.cvar:
assert self.pcap_writer == None
self.pcap_writer = PcapWriter(filename)

def stop_pcap(self):
if self.pcap_writer:
Expand Down

0 comments on commit e8b545f

Please sign in to comment.