diff options
| author | Donatas Abraitis <donatas.abraitis@gmail.com> | 2021-05-10 20:11:47 +0300 |
|---|---|---|
| committer | Donatas Abraitis <donatas.abraitis@gmail.com> | 2021-05-11 14:14:26 +0300 |
| commit | 764b81858f6c55153bc99bae3be5aeab92f59f05 (patch) | |
| tree | b0a7dab3eda4cc47a69c5e08988adf9b174be1f3 /tests/topotests/pim_basic/mcast-tx.py | |
| parent | 9ce686504743ff65ed0ca5ad2d8f10d35519041a (diff) | |
tests: Unify directory naming for topotests
Change every `-` to `_` in directory names. This is to avoid mixing _ and -.
Just for consistency and directory sorting properly.
Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
Diffstat (limited to 'tests/topotests/pim_basic/mcast-tx.py')
| -rwxr-xr-x | tests/topotests/pim_basic/mcast-tx.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/topotests/pim_basic/mcast-tx.py b/tests/topotests/pim_basic/mcast-tx.py new file mode 100755 index 0000000000..87038ad5cf --- /dev/null +++ b/tests/topotests/pim_basic/mcast-tx.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python +# +# mcast-tx.py +# +# Copyright (c) 2018 Cumulus Networks, Inc. +# +# Permission to use, copy, modify, and/or distribute this software +# for any purpose with or without fee is hereby granted, provided +# that the above copyright notice and this permission notice appear +# in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND Cumulus Networks DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY +# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS +# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +# OF THIS SOFTWARE. +# + +import argparse +import logging +import socket +import struct +import time +import sys + +logging.basicConfig( + level=logging.DEBUG, format="%(asctime)s %(levelname)5s: %(message)s" +) + +# Color the errors and warnings in red +logging.addLevelName( + logging.ERROR, "\033[91m %s\033[0m" % logging.getLevelName(logging.ERROR) +) +logging.addLevelName( + logging.WARNING, "\033[91m%s\033[0m" % logging.getLevelName(logging.WARNING) +) +log = logging.getLogger(__name__) + +parser = argparse.ArgumentParser(description="Multicast packet generator") +parser.add_argument("group", help="Multicast IP") +parser.add_argument("ifname", help="Interface name") +parser.add_argument("--port", type=int, help="UDP port number", default=1000) +parser.add_argument("--ttl", type=int, help="time-to-live", default=20) +parser.add_argument("--count", type=int, help="Packets to send", default=1) +parser.add_argument("--interval", type=int, help="ms between packets", default=100) +args = parser.parse_args() + +# Create the datagram socket +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +# IN.SO_BINDTODEVICE is not defined in some releases of python but it is 25 +# https://github.com/sivel/bonding/issues/10 +# +# Bind our socket to ifname +# +# Note ugly python version incompatibility +# +if sys.version_info[0] > 2: + sock.setsockopt( + socket.SOL_SOCKET, + 25, + struct.pack("%ds" % len(args.ifname), args.ifname.encode("utf-8")), + ) +else: + sock.setsockopt( + socket.SOL_SOCKET, 25, struct.pack("%ds" % len(args.ifname), args.ifname) + ) + +# We need to make sure our sendto() finishes before we close the socket +sock.setblocking(1) + +# Set the time-to-live +sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, struct.pack("b", args.ttl)) + +ms = args.interval / 1000.0 + +# Send data to the multicast group +for x in range(args.count): + log.info( + "TX multicast UDP packet to %s:%d on %s" % (args.group, args.port, args.ifname) + ) + + # + # Note ugly python version incompatibility + # + if sys.version_info[0] > 2: + sent = sock.sendto(b"foobar %d" % x, (args.group, args.port)) + else: + sent = sock.sendto("foobar %d" % x, (args.group, args.port)) + + if args.count > 1 and ms: + time.sleep(ms) + +sock.close() |
