Skip to content

Commit

Permalink
selftests/net: Add selftest for IPv4 RTM_GETMULTICAST support
Browse files Browse the repository at this point in the history
This change introduces a new selftest case to verify the functionality
of dumping IPv4 multicast addresses using the RTM_GETMULTICAST netlink
message. The test utilizes the ynl library to interact with the
netlink interface and validate that the kernel correctly reports the
joined IPv4 multicast addresses.

To run the test, execute the following command:

$ vng -v --user root --cpus 16 -- \
    make -C tools/testing/selftests TARGETS=net \
    TEST_PROGS=rtnetlink.py TEST_GEN_PROGS="" run_tests

Cc: Maciej Żenczykowski <maze@google.com>
Cc: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: Yuyang Huang <yuyanghuang@google.com>
Link: https://patch.msgid.link/20250207110836.2407224-2-yuyanghuang@google.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Yuyang Huang authored and Paolo Abeni committed Feb 11, 2025
1 parent eb4e17a commit 4f28037
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Documentation/netlink/specs/rt_addr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,29 @@ operations:
reply:
value: 20
attributes: *ifaddr-all
-
name: getmaddrs
doc: Get / dump IPv4/IPv6 multicast addresses.
attribute-set: addr-attrs
fixed-header: ifaddrmsg
do:
request:
value: 58
attributes:
- ifa-family
- ifa-index
reply:
value: 58
attributes: &mcaddr-attrs
- ifa-multicast
- ifa-cacheinfo
dump:
request:
value: 58
- ifa-family
reply:
value: 58
attributes: *mcaddr-attrs

mcast-groups:
list:
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/net/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ TEST_PROGS += cmsg_so_priority.sh
TEST_PROGS += cmsg_time.sh cmsg_ipv6.sh
TEST_PROGS += netns-name.sh
TEST_PROGS += nl_netdev.py
TEST_PROGS += rtnetlink.py
TEST_PROGS += srv6_end_dt46_l3vpn_test.sh
TEST_PROGS += srv6_end_dt4_l3vpn_test.sh
TEST_PROGS += srv6_end_dt6_l3vpn_test.sh
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/net/lib/py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
from .netns import NetNS
from .nsim import *
from .utils import *
from .ynl import NlError, YnlFamily, EthtoolFamily, NetdevFamily, RtnlFamily
from .ynl import NlError, YnlFamily, EthtoolFamily, NetdevFamily, RtnlFamily, RtnlAddrFamily
from .ynl import NetshaperFamily
4 changes: 4 additions & 0 deletions tools/testing/selftests/net/lib/py/ynl.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def __init__(self, recv_size=0):
super().__init__((SPEC_PATH / Path('rt_link.yaml')).as_posix(),
schema='', recv_size=recv_size)

class RtnlAddrFamily(YnlFamily):
def __init__(self, recv_size=0):
super().__init__((SPEC_PATH / Path('rt_addr.yaml')).as_posix(),
schema='', recv_size=recv_size)

class NetdevFamily(YnlFamily):
def __init__(self, recv_size=0):
Expand Down
30 changes: 30 additions & 0 deletions tools/testing/selftests/net/rtnetlink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0

from lib.py import ksft_exit, ksft_run, ksft_ge, RtnlAddrFamily
import socket

IPV4_ALL_HOSTS_MULTICAST = b'\xe0\x00\x00\x01'

def dump_mcaddr_check(rtnl: RtnlAddrFamily) -> None:
"""
Verify that at least one interface has the IPv4 all-hosts multicast address.
At least the loopback interface should have this address.
"""

addresses = rtnl.getmaddrs({"ifa-family": socket.AF_INET}, dump=True)

all_host_multicasts = [
addr for addr in addresses if addr['ifa-multicast'] == IPV4_ALL_HOSTS_MULTICAST
]

ksft_ge(len(all_host_multicasts), 1,
"No interface found with the IPv4 all-hosts multicast address")

def main() -> None:
rtnl = RtnlAddrFamily()
ksft_run([dump_mcaddr_check], args=(rtnl, ))
ksft_exit()

if __name__ == "__main__":
main()

0 comments on commit 4f28037

Please sign in to comment.