]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/mcast-tester.py
2 # SPDX-License-Identifier: ISC
4 # Copyright (C) 2021 by
5 # Network Device Education Foundation, Inc. ("NetDEF")
8 Subscribe to a multicast group so that the kernel sends an IGMP JOIN
9 for the multicast group we subscribed to.
25 def interface_name_to_index(name
):
26 "Gets the interface index using its name. Returns None on failure."
27 interfaces
= json
.loads(subprocess
.check_output("ip -j link show", shell
=True))
29 for interface
in interfaces
:
30 if interface
["ifname"] == name
:
31 return interface
["ifindex"]
36 def multicast_join(sock
, ifindex
, group
, port
):
37 "Joins a multicast group."
39 "=4sLL", socket
.inet_aton(args
.group
), socket
.INADDR_ANY
, ifindex
42 sock
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1)
43 sock
.bind((group
, port
))
44 sock
.setsockopt(socket
.IPPROTO_IP
, socket
.IP_ADD_MEMBERSHIP
, mreq
)
50 parser
= argparse
.ArgumentParser(description
="Multicast RX utility")
51 parser
.add_argument("group", help="Multicast IP")
52 parser
.add_argument("interface", help="Interface name")
53 parser
.add_argument("--socket", help="Point to topotest UNIX socket")
55 "--send", help="Transmit instead of join with interval", type=float, default
=0
57 args
= parser
.parse_args()
62 # Get interface index/validate.
63 ifindex
= interface_name_to_index(args
.interface
)
65 sys
.stderr
.write("Interface {} does not exists\n".format(args
.interface
))
68 # We need root privileges to set up multicast.
70 sys
.stderr
.write("ERROR: You must have root privileges\n")
73 # Wait for topotest to synchronize with us.
77 toposock
= socket
.socket(socket
.AF_UNIX
, socket
.SOCK_STREAM
, 0)
80 toposock
.connect(args
.socket
)
82 except ConnectionRefusedError
:
85 # Set topotest socket non blocking so we can multiplex the main loop.
86 toposock
.setblocking(False)
88 msock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
90 # Prepare multicast bit in that interface.
94 struct
.pack("%ds" % len(args
.interface
), args
.interface
.encode("utf-8")),
97 msock
.setsockopt(socket
.IPPROTO_IP
, socket
.IP_MULTICAST_TTL
, struct
.pack("b", ttl
))
98 # Block to ensure packet send.
99 msock
.setblocking(True)
101 multicast_join(msock
, ifindex
, args
.group
, port
)
106 # If we are sending then we have slept
112 data
= toposock
.recv(1)
114 print(" -> Connection closed")
116 except BlockingIOError
:
121 while not should_exit():
123 msock
.sendto(b
"test %d" % counter
, (args
.group
, port
))
125 time
.sleep(args
.send
)