Skip to content

Commit 4f6fe09

Browse files
JoshuaWattBastian-Krause
authored andcommitted
driver/rawnetworkinterfacedriver: move timeout to tcpdump
Reworks the way that timeouts are handled so that instead of terminating the process in stop_record, tcpdump will exit after a set time. This is will allow for live streaming of packets in a future patch Signed-off-by: Joshua Watt <[email protected]> Signed-off-by: Bastian Krause <[email protected]>
1 parent f31f3da commit 4f6fe09

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

helpers/labgrid-raw-interface

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def main(program, options):
6767
args.append("-c")
6868
args.append(str(options.count))
6969

70+
if options.timeout:
71+
# The timeout is implemented by specifying the number of seconds before rotating the
72+
# dump file, but limiting the number of files to 1
73+
args.append("-G")
74+
args.append(str(options.timeout))
75+
args.append("-W")
76+
args.append("1")
77+
7078
try:
7179
os.execvp(args[0], args)
7280
except FileNotFoundError as e:
@@ -82,6 +90,9 @@ if __name__ == "__main__":
8290
tcpdump_parser = subparsers.add_parser("tcpdump")
8391
tcpdump_parser.add_argument("ifname", type=str, help="interface name")
8492
tcpdump_parser.add_argument("count", type=int, default=None, help="amount of frames to capture while recording")
93+
tcpdump_parser.add_argument(
94+
"--timeout", type=int, default=None, help="Amount of time to capture while recording. 0 means capture forever"
95+
)
8596

8697
# tcpreplay
8798
tcpreplay_parser = subparsers.add_parser("tcpreplay")

labgrid/driver/rawnetworkinterfacedriver.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ def _stop(self, proc, *, timeout=None):
5353
)
5454

5555
@Driver.check_active
56-
@step(args=["filename", "count"])
57-
def start_record(self, filename, *, count=None):
56+
@step(args=["filename", "count", "timeout"])
57+
def start_record(self, filename, *, count=None, timeout=None):
5858
"""
5959
Starts tcpdump on bound network interface resource.
6060
6161
Args:
6262
filename (str): name of a file to record to
6363
count (int): optional, exit after receiving this many number of packets
64+
timeout (int): optional, number of seconds to capture packets before tcpdump exits
6465
Returns:
6566
Popen object of tcpdump process
6667
"""
@@ -69,6 +70,9 @@ def start_record(self, filename, *, count=None):
6970
cmd = ["tcpdump", self.iface.ifname]
7071
if count is not None:
7172
cmd.append(str(count))
73+
if timeout is not None:
74+
cmd.append("--timeout")
75+
cmd.append(str(timeout))
7276
cmd = self._wrap_command(cmd)
7377
with open(filename, "wb") as outdata:
7478
self._record_handle = subprocess.Popen(cmd, stdout=outdata, stderr=subprocess.PIPE)
@@ -99,15 +103,16 @@ def record(self, filename, *, count=None, timeout=None):
99103
Args:
100104
filename (str): name of a file to record to
101105
count (int): optional, exit after receiving this many number of packets
102-
timeout (int): optional, maximum number of seconds to wait for the tcpdump process to
103-
terminate
106+
timeout (int): optional, number of seconds to capture packets before tcpdump exits
107+
Returns:
108+
Popen object of tcpdump process.
104109
"""
105110
assert count or timeout
106111

107112
try:
108-
yield self.start_record(filename, count=count)
113+
yield self.start_record(filename, count=count, timeout=timeout)
109114
finally:
110-
self.stop_record(timeout=timeout)
115+
self.stop_record()
111116

112117
@Driver.check_active
113118
@step(args=["filename"])

0 commit comments

Comments
 (0)