Skip to content

Commit

Permalink
net-test: packetdrill: run_all: add --capture option
Browse files Browse the repository at this point in the history
This new option is useful to capture packet traces per test for debug
purposes.

With the new parameter, we can specify to capture packets with TCPDump
and write the .pcap files in the given directory without modifying
in_netns.sh or re-launching the tests manually outside the netns.

e.g. to write .pcap files in the home directory:

  $ run_all.py -c ~

Files will be generated using the name of the test and its variant.

Note that this adds some delay: one second before starting not to miss
the first packets and one second at the end to make sure the last
packets will be captured by TCPDump.

Signed-off-by: Matthieu Baerts <[email protected]>
  • Loading branch information
matttbe authored and nealcardwell committed Sep 29, 2023
1 parent 48d5352 commit 90e6d74
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 12 additions & 0 deletions gtests/net/packetdrill/in_netns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ set -e

readonly NETNS="ns-$(mktemp -u XXXXXX)"

TCPDUMP_PID=

setup() {
ip netns add "${NETNS}"
ip -netns "${NETNS}" link set lo up
if [ -n "${TCPDUMP_OUTPUT}" ]; then
ip netns exec "${NETNS}" tcpdump -i any -s 150 -w "${TCPDUMP_OUTPUT}" &
TCPDUMP_PID=$!
sleep 1 # give some time to TCPDump to start
fi
}

cleanup() {
if [ -n "${TCPDUMP_PID}" ]; then
sleep 1 # give some time to TCPDump to process the packets
kill "${TCPDUMP_PID}"
wait "${TCPDUMP_PID}" 2>/dev/null || true
fi
ip netns del "${NETNS}"
}

Expand Down
14 changes: 11 additions & 3 deletions gtests/net/packetdrill/run_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def CmdTest(self, path, variant, extra_args=None):
cmd.append('-' + 'v' * (self.args['verbose'] - 1))
cmd.append(basename)

return (cmd, execdir, path, variant)
return (cmd, execdir, path, variant, basename)

def CmdTestIPv4(self, path):
"""Return a command to run a packetdrill test over ipv4."""
Expand Down Expand Up @@ -116,13 +116,19 @@ def Log(self, outfile, errfile):
errfile.seek(0)
sys.stderr.write(errfile.read())

def StartTest(self, cmd, execdir, path, variant):
def StartTest(self, cmd, execdir, path, variant, basename):
"""Run a packetdrill test"""
outfile = tempfile.TemporaryFile(mode='w+')
errfile = tempfile.TemporaryFile(mode='w+')

env = os.environ
if self.args['capture'] is not None:
fname = os.path.splitext(basename)[0] + "_" + variant + ".pcap"
env = dict(env, TCPDUMP_OUTPUT=os.path.join(self.args['capture'], fname))

time_start = time.time()
process = subprocess.Popen(cmd, stdout=outfile, stderr=errfile, cwd=execdir)
process = subprocess.Popen(cmd, stdout=outfile, stderr=errfile, cwd=execdir,
env=env)

return (process, path, variant, outfile, errfile, time_start)

Expand Down Expand Up @@ -254,6 +260,8 @@ def ParseArgs():
"""Parse commandline arguments."""
args = argparse.ArgumentParser()
args.add_argument('path', default='.', nargs='?')
args.add_argument('-c', '--capture', metavar='DIR',
help='capture packets in the specified directory')
args.add_argument('-l', '--log_on_error', action='store_true',
help='requires verbose')
args.add_argument('-L', '--log_on_success', action='store_true',
Expand Down

0 comments on commit 90e6d74

Please sign in to comment.