<p>laforge <strong>submitted</strong> this change.</p><p><a href="https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/21674">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">gbproxy: Properly capture HDLC/FR traffic in addition to ethernet<br><br>We cannot use "-i all" but must list each interface separately,<br>which is only supported by dumpcap.  We also must write pcapng<br>files.<br><br>Change-Id: Id412af3bb6bcad5e0f2cf40a6dc497d7e4f3d948<br>---<br>M gbproxy/GBProxy_Tests.default<br>A ttcn3-dumpcap-start.sh<br>A ttcn3-dumpcap-stop.sh<br>3 files changed, 152 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/gbproxy/GBProxy_Tests.default b/gbproxy/GBProxy_Tests.default</span><br><span>index 799ac32..8634b49 100644</span><br><span>--- a/gbproxy/GBProxy_Tests.default</span><br><span>+++ b/gbproxy/GBProxy_Tests.default</span><br><span>@@ -1,3 +1,7 @@</span><br><span style="color: hsl(120, 100%, 40%);">+[DEFINE]</span><br><span style="color: hsl(120, 100%, 40%);">+TCPDUMP_START := $TTCN3_HACKS_PATH"/ttcn3-dumpcap-start.sh"</span><br><span style="color: hsl(120, 100%, 40%);">+TCPDUMP_STOP := $TTCN3_HACKS_PATH"/ttcn3-dumpcap-stop.sh"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> [LOGGING]</span><br><span> FileMask := LOG_ALL | TTCN_MATCHING;</span><br><span> </span><br><span>diff --git a/ttcn3-dumpcap-start.sh b/ttcn3-dumpcap-start.sh</span><br><span>new file mode 100755</span><br><span>index 0000000..aa3814a</span><br><span>--- /dev/null</span><br><span>+++ b/ttcn3-dumpcap-start.sh</span><br><span>@@ -0,0 +1,96 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#!/bin/bash</span><br><span style="color: hsl(120, 100%, 40%);">+#</span><br><span style="color: hsl(120, 100%, 40%);">+# contrary to ttcn3-tcpdump-start.sh, this version is dumpcap-only and</span><br><span style="color: hsl(120, 100%, 40%);">+# needed when we want to capture from interfaces of different link</span><br><span style="color: hsl(120, 100%, 40%);">+# types. It will also store the results as pcap-ng, not plain old pcap.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+PIDFILE_PCAP=/tmp/pcap.pid</span><br><span style="color: hsl(120, 100%, 40%);">+DUMPCAP=/usr/bin/dumpcap</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+PIDFILE_NETCAT=/tmp/netcat.pid</span><br><span style="color: hsl(120, 100%, 40%);">+NETCAT=/bin/nc</span><br><span style="color: hsl(120, 100%, 40%);">+GSMTAP_PORT=4729</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+TESTCASE=$1</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+kill_rm_pidfile() {</span><br><span style="color: hsl(120, 100%, 40%);">+      if [ -e $1 ]; then</span><br><span style="color: hsl(120, 100%, 40%);">+            kill "$(cat "$1")"</span><br><span style="color: hsl(120, 100%, 40%);">+                rm $1</span><br><span style="color: hsl(120, 100%, 40%);">+ fi</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+echo "------ $TESTCASE ------"</span><br><span style="color: hsl(120, 100%, 40%);">+date</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+if [ "z$TTCN3_PCAP_PATH" = "z" ]; then</span><br><span style="color: hsl(120, 100%, 40%);">+ TTCN3_PCAP_PATH=/tmp</span><br><span style="color: hsl(120, 100%, 40%);">+fi</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+kill_rm_pidfile $PIDFILE_NETCAT</span><br><span style="color: hsl(120, 100%, 40%);">+kill_rm_pidfile $PIDFILE_PCAP</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+if [ -x $DUMPCAP ]; then</span><br><span style="color: hsl(120, 100%, 40%);">+    CAP_ERR="1"</span><br><span style="color: hsl(120, 100%, 40%);">+    if [ -x /sbin/setcap ]; then</span><br><span style="color: hsl(120, 100%, 40%);">+   # N. B: this check requires libcap2-bin package</span><br><span style="color: hsl(120, 100%, 40%);">+       /sbin/setcap -q -v 'cap_net_admin,cap_net_raw=pie' $DUMPCAP</span><br><span style="color: hsl(120, 100%, 40%);">+   CAP_ERR="$?"</span><br><span style="color: hsl(120, 100%, 40%);">+    fi</span><br><span style="color: hsl(120, 100%, 40%);">+    if [ -u $DUMPCAP -o "$CAP_ERR" = "0" ]; then</span><br><span style="color: hsl(120, 100%, 40%);">+  CMD="$DUMPCAP -q"</span><br><span style="color: hsl(120, 100%, 40%);">+    else</span><br><span style="color: hsl(120, 100%, 40%);">+         echo "NOTE: unable to use dumpcap due to missing capabilities or suid bit"</span><br><span style="color: hsl(120, 100%, 40%);">+  exit 32</span><br><span style="color: hsl(120, 100%, 40%);">+    fi</span><br><span style="color: hsl(120, 100%, 40%);">+fi</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+# Create a dummy sink for GSMTAP packets</span><br><span style="color: hsl(120, 100%, 40%);">+$NETCAT -l -u -k -p $GSMTAP_PORT >/dev/null 2>$TESTCASE.netcat.stderr &</span><br><span style="color: hsl(120, 100%, 40%);">+PID=$!</span><br><span style="color: hsl(120, 100%, 40%);">+echo $PID > $PIDFILE_NETCAT</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+# generate the list of interface arguments.  For capturing from</span><br><span style="color: hsl(120, 100%, 40%);">+# interfaces of different link-layer types, we cannot use "-i all"</span><br><span style="color: hsl(120, 100%, 40%);">+# but must use dumpcap with each individual interface name.  We also</span><br><span style="color: hsl(120, 100%, 40%);">+# must write pcapng files, as only those can record the interface of</span><br><span style="color: hsl(120, 100%, 40%);">+# each packet</span><br><span style="color: hsl(120, 100%, 40%);">+ADDL_ARGS=""</span><br><span style="color: hsl(120, 100%, 40%);">+for f in /sys/class/net/*; do</span><br><span style="color: hsl(120, 100%, 40%);">+ DEV=`basename $f`</span><br><span style="color: hsl(120, 100%, 40%);">+     if [[ "$DEV" == "hdlcnet"* ]]; then</span><br><span style="color: hsl(120, 100%, 40%);">+               # skip these as we only want the hdlcX devices, avoid capturing twice on both sides</span><br><span style="color: hsl(120, 100%, 40%);">+           continue</span><br><span style="color: hsl(120, 100%, 40%);">+      elif [[ "$DEV" == "hdlc"* ]]; then</span><br><span style="color: hsl(120, 100%, 40%);">+                # these are the user-side of the FR links, which is</span><br><span style="color: hsl(120, 100%, 40%);">+           # what we interface with from our test suite, emulating</span><br><span style="color: hsl(120, 100%, 40%);">+               # a BSS.</span><br><span style="color: hsl(120, 100%, 40%);">+              ADDL_ARGS="${ADDL_ARGS} -i ${DEV}"</span><br><span style="color: hsl(120, 100%, 40%);">+  elif [[ "$DEV" == "eth"* ]]; then</span><br><span style="color: hsl(120, 100%, 40%);">+         # we blindly assume that "normal" docker network devices</span><br><span style="color: hsl(120, 100%, 40%);">+            # are called ethXXX</span><br><span style="color: hsl(120, 100%, 40%);">+           ADDL_ARGS="${ADDL_ARGS} -i ${DEV}"</span><br><span style="color: hsl(120, 100%, 40%);">+  fi</span><br><span style="color: hsl(120, 100%, 40%);">+done</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+$CMD -s 1500 -n ${ADDL_ARGS} -w "$TTCN3_PCAP_PATH/$TESTCASE.pcapng" >$TTCN3_PCAP_PATH/$TESTCASE.pcapng.stdout 2>&1 &</span><br><span style="color: hsl(120, 100%, 40%);">+PID=$!</span><br><span style="color: hsl(120, 100%, 40%);">+echo $PID > $PIDFILE_PCAP</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+# Wait until packet dumper creates the pcap file and starts recording.</span><br><span style="color: hsl(120, 100%, 40%);">+# We generate some traffic until we see packet dumper catches it.</span><br><span style="color: hsl(120, 100%, 40%);">+# Timeout is 10 seconds.</span><br><span style="color: hsl(120, 100%, 40%);">+ping 127.0.0.1 >/dev/null 2>&1 &</span><br><span style="color: hsl(120, 100%, 40%);">+PID=$!</span><br><span style="color: hsl(120, 100%, 40%);">+i=0</span><br><span style="color: hsl(120, 100%, 40%);">+while [ ! -f "$TTCN3_PCAP_PATH/$TESTCASE.pcapng" ] ||</span><br><span style="color: hsl(120, 100%, 40%);">+      [ "$(stat -c '%s' "$TTCN3_PCAP_PATH/$TESTCASE.pcapng")" -eq 32 ]</span><br><span style="color: hsl(120, 100%, 40%);">+do</span><br><span style="color: hsl(120, 100%, 40%);">+   echo "Waiting for packet dumper to start... $i"</span><br><span style="color: hsl(120, 100%, 40%);">+     sleep 1</span><br><span style="color: hsl(120, 100%, 40%);">+       i=$((i+1))</span><br><span style="color: hsl(120, 100%, 40%);">+    if [ $i -eq 10 ]; then</span><br><span style="color: hsl(120, 100%, 40%);">+                break</span><br><span style="color: hsl(120, 100%, 40%);">+ fi</span><br><span style="color: hsl(120, 100%, 40%);">+done</span><br><span style="color: hsl(120, 100%, 40%);">+kill $PID</span><br><span>diff --git a/ttcn3-dumpcap-stop.sh b/ttcn3-dumpcap-stop.sh</span><br><span>new file mode 100755</span><br><span>index 0000000..e13fbc0</span><br><span>--- /dev/null</span><br><span>+++ b/ttcn3-dumpcap-stop.sh</span><br><span>@@ -0,0 +1,52 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#!/bin/sh</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+PIDFILE_PCAP=/tmp/pcap.pid</span><br><span style="color: hsl(120, 100%, 40%);">+PIDFILE_NETCAT=/tmp/netcat.pid</span><br><span style="color: hsl(120, 100%, 40%);">+TESTCASE=$1</span><br><span style="color: hsl(120, 100%, 40%);">+VERDICT="$2"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+kill_rm_pidfile() {</span><br><span style="color: hsl(120, 100%, 40%);">+if [ -e $1 ]; then</span><br><span style="color: hsl(120, 100%, 40%);">+        PSNAME="$(ps -q "$(cat "$1")" -o comm=)"</span><br><span style="color: hsl(120, 100%, 40%);">+    if [ "$PSNAME" != "sudo" ]; then</span><br><span style="color: hsl(120, 100%, 40%);">+          kill "$(cat "$1")"</span><br><span style="color: hsl(120, 100%, 40%);">+        else</span><br><span style="color: hsl(120, 100%, 40%);">+  # NOTE: This requires you to be root or something like</span><br><span style="color: hsl(120, 100%, 40%);">+        # "laforge ALL=NOPASSWD: /usr/sbin/tcpdump, /bin/kill" in your sudoers file</span><br><span style="color: hsl(120, 100%, 40%);">+         sudo kill "$(cat "$1")"</span><br><span style="color: hsl(120, 100%, 40%);">+   fi</span><br><span style="color: hsl(120, 100%, 40%);">+    rm $1</span><br><span style="color: hsl(120, 100%, 40%);">+fi</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+date</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+if [ x"$VERDICT" = x"pass" ]; then</span><br><span style="color: hsl(120, 100%, 40%);">+        echo -e "\033[1;32m====== $TESTCASE $VERDICT ======\033[0m"</span><br><span style="color: hsl(120, 100%, 40%);">+else</span><br><span style="color: hsl(120, 100%, 40%);">+   echo -e "\033[1;31m------ $TESTCASE $VERDICT ------\033[0m"</span><br><span style="color: hsl(120, 100%, 40%);">+fi</span><br><span style="color: hsl(120, 100%, 40%);">+echo</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+if [ "z$TTCN3_PCAP_PATH" = "z" ]; then</span><br><span style="color: hsl(120, 100%, 40%);">+ TTCN3_PCAP_PATH=/tmp</span><br><span style="color: hsl(120, 100%, 40%);">+fi</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+# Wait for up to 2 seconds if we keep receiving traffinc from packet dumper,</span><br><span style="color: hsl(120, 100%, 40%);">+# otherwise we might lose last packets from test.</span><br><span style="color: hsl(120, 100%, 40%);">+i=0</span><br><span style="color: hsl(120, 100%, 40%);">+prev_count=-1</span><br><span style="color: hsl(120, 100%, 40%);">+count=$(stat --format="%s" "$TTCN3_PCAP_PATH/$TESTCASE.pcapng")</span><br><span style="color: hsl(120, 100%, 40%);">+while [ $count -gt $prev_count ] && [ $i -lt 2 ]</span><br><span style="color: hsl(120, 100%, 40%);">+do</span><br><span style="color: hsl(120, 100%, 40%);">+    echo "Waiting for packet dumper to finish... $i (prev_count=$prev_count, count=$count)"</span><br><span style="color: hsl(120, 100%, 40%);">+     sleep 1</span><br><span style="color: hsl(120, 100%, 40%);">+       prev_count=$count</span><br><span style="color: hsl(120, 100%, 40%);">+     count=$(stat --format="%s" "$TTCN3_PCAP_PATH/$TESTCASE.pcapng")</span><br><span style="color: hsl(120, 100%, 40%);">+   i=$((i+1))</span><br><span style="color: hsl(120, 100%, 40%);">+done</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+kill_rm_pidfile "$PIDFILE_PCAP"</span><br><span style="color: hsl(120, 100%, 40%);">+kill_rm_pidfile "$PIDFILE_NETCAT"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+gzip -f "$TTCN3_PCAP_PATH/$TESTCASE.pcapng"</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/21674">change 21674</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/21674"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-ttcn3-hacks </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Id412af3bb6bcad5e0f2cf40a6dc497d7e4f3d948 </div>
<div style="display:none"> Gerrit-Change-Number: 21674 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder </div>
<div style="display:none"> Gerrit-Reviewer: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>