diff mbox series

[ovs-dev,v5,1/2] tests: define fmt_pkt function to construct packets with scapy

Message ID 20230414211253.1992599-1-ihrachys@redhat.com
State Accepted
Headers show
Series [ovs-dev,v5,1/2] tests: define fmt_pkt function to construct packets with scapy | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/github-robot-_ovn-kubernetes success github build: passed

Commit Message

Ihar Hrachyshka April 14, 2023, 9:12 p.m. UTC
Scapy allows to define packets in descriptive form that is easier to
digest and debug.

Signed-off-by: Ihar Hrachyshka <ihrachys@redhat.com>
Acked-by: Ales Musil <amusil@redhat.com>
Acked-by: Dumitru Ceara <dceara@redhat.com>
---
v1: initial version.
v2: use .decode() instead of sed to truncate b'...'.
v2: remove unnecessary backspaces in example of use.
v2: use $PYTHON3.
v3: <no changes>.
v4: added missing acks from previous reviews.
v5: <no changes>.
---
 tests/ovn-macros.at | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

Comments

Dumitru Ceara April 19, 2023, 2:16 p.m. UTC | #1
On 4/14/23 23:12, Ihar Hrachyshka wrote:
> Scapy allows to define packets in descriptive form that is easier to
> digest and debug.
> 
> Signed-off-by: Ihar Hrachyshka <ihrachys@redhat.com>
> Acked-by: Ales Musil <amusil@redhat.com>
> Acked-by: Dumitru Ceara <dceara@redhat.com>
> ---
> v1: initial version.
> v2: use .decode() instead of sed to truncate b'...'.
> v2: remove unnecessary backspaces in example of use.
> v2: use $PYTHON3.
> v3: <no changes>.
> v4: added missing acks from previous reviews.
> v5: <no changes>.
> ---
>  tests/ovn-macros.at | 23 +++++++++++++++++++++++
>  1 file changed, 23 insertions(+)
> 
> diff --git a/tests/ovn-macros.at b/tests/ovn-macros.at
> index ee942e8a6..6f2d085ae 100644
> --- a/tests/ovn-macros.at
> +++ b/tests/ovn-macros.at
> @@ -817,6 +817,29 @@ ovn_trace_client() {
>      ovs-appctl -t $target trace "$@" | tee trace | sed '/^# /d'
>  }
>  
> +# Receives a string with scapy python code that represents a packet.
> +# Returns a hex-string that contains bytes that reflect the packet symbolic
> +# description.
> +#
> +# Scapy docs: https://scapy.readthedocs.io/en/latest/usage.html
> +#
> +# Example of usage:
> +#
> +# packet=$(fmt_pkt "
> +#     Ether(dst='ff:ff:ff:ff:ff:ff', src='50:64:00:00:00:01') /
> +#     IPv6(src='abed::1', dst='ff02::1:ff00:2') /
> +#     ICMPv6ND_NS(tgt='abed::2')
> +# ")
> +#
> +# ovs-appctl netdev-dummy/receive $vif $packet
> +#
> +fmt_pkt() {
> +    echo "from scapy.all import *; \
> +          import binascii; \
> +          out = binascii.hexlify(raw($1)); \
> +          print(out.decode())" | $PYTHON3
> +}
> +
>  OVS_END_SHELL_HELPERS
>  
>  m4_define([OVN_POPULATE_ARP], [AT_CHECK(ovn_populate_arp__, [0], [ignore])])

This is awesome and really useful!

I applied it to main and backported it to all stable branches down to
22.03 (LTS).  To make it work properly on older branches I had to
actually install scapy via pip (like we do on main in CI) similar to
Ales' commit 97b5aa54571a ("ci: Add missing packages to the container").
 I added a note in the backports' commit log.

Thanks,
Dumitru
diff mbox series

Patch

diff --git a/tests/ovn-macros.at b/tests/ovn-macros.at
index ee942e8a6..6f2d085ae 100644
--- a/tests/ovn-macros.at
+++ b/tests/ovn-macros.at
@@ -817,6 +817,29 @@  ovn_trace_client() {
     ovs-appctl -t $target trace "$@" | tee trace | sed '/^# /d'
 }
 
+# Receives a string with scapy python code that represents a packet.
+# Returns a hex-string that contains bytes that reflect the packet symbolic
+# description.
+#
+# Scapy docs: https://scapy.readthedocs.io/en/latest/usage.html
+#
+# Example of usage:
+#
+# packet=$(fmt_pkt "
+#     Ether(dst='ff:ff:ff:ff:ff:ff', src='50:64:00:00:00:01') /
+#     IPv6(src='abed::1', dst='ff02::1:ff00:2') /
+#     ICMPv6ND_NS(tgt='abed::2')
+# ")
+#
+# ovs-appctl netdev-dummy/receive $vif $packet
+#
+fmt_pkt() {
+    echo "from scapy.all import *; \
+          import binascii; \
+          out = binascii.hexlify(raw($1)); \
+          print(out.decode())" | $PYTHON3
+}
+
 OVS_END_SHELL_HELPERS
 
 m4_define([OVN_POPULATE_ARP], [AT_CHECK(ovn_populate_arp__, [0], [ignore])])