git: f0ffe1ce0fba - main - testing: add support for handling Netlink carp messages
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 15 May 2023 10:53:38 UTC
The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=f0ffe1ce0fba262b458399dcd4c5e8c84e024606 commit f0ffe1ce0fba262b458399dcd4c5e8c84e024606 Author: Alexander V. Chernikov <melifaro@FreeBSD.org> AuthorDate: 2023-05-15 10:48:45 +0000 Commit: Alexander V. Chernikov <melifaro@FreeBSD.org> CommitDate: 2023-05-15 10:48:45 +0000 testing: add support for handling Netlink carp messages MFC after: 2 weeks --- tests/atf_python/sys/netlink/attrs.py | 46 +++++++++++++++++++++ tests/atf_python/sys/netlink/netlink_generic.py | 55 ++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/tests/atf_python/sys/netlink/attrs.py b/tests/atf_python/sys/netlink/attrs.py index 9769ef0fc76f..36dd8191df1c 100644 --- a/tests/atf_python/sys/netlink/attrs.py +++ b/tests/atf_python/sys/netlink/attrs.py @@ -81,6 +81,13 @@ class NlAttrNested(NlAttr): super().__init__(nla_type, b"") self.nla_list = val + def get_nla(self, nla_type): + nla_type_raw = enum_or_int(nla_type) + for nla in self.nla_list: + if nla.nla_type == nla_type_raw: + return nla + return None + @property def nla_len(self): return align4(len(b"".join([bytes(nla) for nla in self.nla_list]))) + 4 @@ -130,6 +137,33 @@ class NlAttrU32(NlAttr): return self._to_bytes(struct.pack("@I", self.u32)) +class NlAttrS32(NlAttr): + def __init__(self, nla_type, val): + self.s32 = enum_or_int(val) + super().__init__(nla_type, b"") + + @property + def nla_len(self): + return 8 + + def _print_attr_value(self): + return " val={}".format(self.s32) + + @staticmethod + def _validate(data): + assert len(data) == 8 + nla_len, nla_type = struct.unpack("@HH", data[:4]) + assert nla_len == 8 + + @classmethod + def _parse(cls, data): + nla_len, nla_type, val = struct.unpack("@HHi", data) + return cls(nla_type, val) + + def __bytes__(self): + return self._to_bytes(struct.pack("@i", self.s32)) + + class NlAttrU16(NlAttr): def __init__(self, nla_type, val): self.u16 = enum_or_int(val) @@ -229,6 +263,18 @@ class NlAttrIp(NlAttr): return " addr={}".format(self.addr) +class NlAttrIp4(NlAttrIp): + def __init__(self, nla_type, addr: str): + super().__init__(nla_type, addr) + assert self.family == socket.AF_INET + + +class NlAttrIp6(NlAttrIp): + def __init__(self, nla_type, addr: str): + super().__init__(nla_type, addr) + assert self.family == socket.AF_INET6 + + class NlAttrStr(NlAttr): def __init__(self, nla_type, text): super().__init__(nla_type, b"") diff --git a/tests/atf_python/sys/netlink/netlink_generic.py b/tests/atf_python/sys/netlink/netlink_generic.py index 06dc8704fe07..b49a30c1e8e7 100644 --- a/tests/atf_python/sys/netlink/netlink_generic.py +++ b/tests/atf_python/sys/netlink/netlink_generic.py @@ -1,12 +1,15 @@ #!/usr/local/bin/python3 +import struct from ctypes import c_int64 from ctypes import c_long from ctypes import sizeof from ctypes import Structure from enum import Enum -import struct from atf_python.sys.netlink.attrs import NlAttr +from atf_python.sys.netlink.attrs import NlAttrIp4 +from atf_python.sys.netlink.attrs import NlAttrIp6 +from atf_python.sys.netlink.attrs import NlAttrS32 from atf_python.sys.netlink.attrs import NlAttrStr from atf_python.sys.netlink.attrs import NlAttrU16 from atf_python.sys.netlink.attrs import NlAttrU32 @@ -16,8 +19,8 @@ from atf_python.sys.netlink.message import NlMsgCategory from atf_python.sys.netlink.message import NlMsgProps from atf_python.sys.netlink.message import StdNetlinkMessage from atf_python.sys.netlink.utils import AttrDescr -from atf_python.sys.netlink.utils import prepare_attrs_map from atf_python.sys.netlink.utils import enum_or_int +from atf_python.sys.netlink.utils import prepare_attrs_map class NetlinkGenlMessage(StdNetlinkMessage): @@ -63,6 +66,7 @@ class NetlinkGenlMessage(StdNetlinkMessage): GenlCtrlFamilyName = "nlctrl" + class GenlCtrlMsgType(Enum): CTRL_CMD_UNSPEC = 0 CTRL_CMD_NEWFAMILY = 1 @@ -111,6 +115,52 @@ class NetlinkGenlCtrlMessage(NetlinkGenlMessage): family_name = GenlCtrlFamilyName +CarpFamilyName = "carp" + + +class CarpMsgType(Enum): + CARP_NL_CMD_UNSPEC = 0 + CARP_NL_CMD_GET = 1 + CARP_NL_CMD_SET = 2 + + +class CarpAttrType(Enum): + CARP_NL_UNSPEC = 0 + CARP_NL_VHID = 1 + CARP_NL_STATE = 2 + CARP_NL_ADVBASE = 3 + CARP_NL_ADVSKEW = 4 + CARP_NL_KEY = 5 + CARP_NL_IFINDEX = 6 + CARP_NL_ADDR = 7 + CARP_NL_ADDR6 = 8 + CARP_NL_IFNAME = 9 + + +carp_gen_attrs = prepare_attrs_map( + [ + AttrDescr(CarpAttrType.CARP_NL_VHID, NlAttrU32), + AttrDescr(CarpAttrType.CARP_NL_STATE, NlAttrU32), + AttrDescr(CarpAttrType.CARP_NL_ADVBASE, NlAttrS32), + AttrDescr(CarpAttrType.CARP_NL_ADVSKEW, NlAttrS32), + AttrDescr(CarpAttrType.CARP_NL_KEY, NlAttr), + AttrDescr(CarpAttrType.CARP_NL_IFINDEX, NlAttrU32), + AttrDescr(CarpAttrType.CARP_NL_ADDR, NlAttrIp4), + AttrDescr(CarpAttrType.CARP_NL_ADDR6, NlAttrIp6), + AttrDescr(CarpAttrType.CARP_NL_IFNAME, NlAttrStr), + ] +) + + +class CarpGenMessage(NetlinkGenlMessage): + messages = [ + NlMsgProps(CarpMsgType.CARP_NL_CMD_GET, NlMsgCategory.GET), + NlMsgProps(CarpMsgType.CARP_NL_CMD_SET, NlMsgCategory.NEW), + ] + nl_attrs_map = carp_gen_attrs + family_name = CarpFamilyName + + KtestFamilyName = "ktest" @@ -223,6 +273,7 @@ class KtestMsgMessage(NetlinkGenlMessage): handler_classes = { + CarpFamilyName: [CarpGenMessage], GenlCtrlFamilyName: [NetlinkGenlCtrlMessage], KtestFamilyName: [KtestInfoMessage, KtestMsgMessage], }