From nobody Thu Feb 16 13:39:58 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PHbfQ60P6z3qSGJ; Thu, 16 Feb 2023 13:39:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PHbfQ5DDCz3PmZ; Thu, 16 Feb 2023 13:39:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1676554798; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qCb70ACFU72123PFp+gR6zav2SJ3t3YLHezuXfVjJ9Q=; b=rNDZcOtYIZJYvyKvmiXq0ws7i9ng8unIKqyPFYFbjIniIcN5/+0SbxpryyUCrjpizQctkS 1ksGTejqV+DA45AYYKwhfy+lGG4vJEKAxJLdYKI0dI3q3nfe9kEryx8ODZ1m8fdn+2/0cV qqTDfsgVfL4l6SdzgaBXHU7A+aojGiIwwX/zb+asgP1Pu9urx7VqiGcdWepmjqh6DdxFbA qtz/S1bpNc13Et4Egl8j7lDwRxV7RYTGZwkumRrk7KRvWQF9j2Riviuqw8XUz0Gzqy6lvc 9db7JZNxfVMpFvkv8aDAgWZLgV2MI5fvKhZKlAA3Pdh8ukZHMNNOIoZj73bqVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1676554798; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qCb70ACFU72123PFp+gR6zav2SJ3t3YLHezuXfVjJ9Q=; b=dUUP/MwR7JasCfZWdOA5tn7jtrUyXQDo+knzuoLSSQ+0EZl1HPWTKXBQUXGsaeBaF+XWRa 7wyylwrGIJ35HN8gFWYOuHt5GmSnCkb9TT0siT7F4DXCdvloM2qAjZvN/wEcG/VY+EFOrG glXQ80K0vaOKqEXV4gqDw3JWGUAr+1AGY4CDpMekmaomYXtxk6QAId5SX6alY0bXSQ57no 7anyrbHsCzeqmqYw89nxb4ASKj9fMcc8dl+9NAUY1wDI4l4N5ah41cvLoBX5aMj+aC6ROH HMcKL9rXyhWX/FRx3yFcSOZTf8Byiu2Iti0QZemDYfFs37RniI9rezkvuRe1Pw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1676554798; a=rsa-sha256; cv=none; b=Q3HckYOcTCUTIHrdC27bNy8iIb1h69NUNDGF+mh+X3zyIWOtC678rgL7cbaV1uTus33Ae+ KjjPvurPECCKNsU1V3l1XbyuCC+fVn+Alp2xmqKub11MWpuupN6iMC3mW7nutfLykJ0q6f CCHSkSwFWJamuBauL+scvCjYbum/5rdgKqjviJE9vfDOBjFIkHMKxK1uPMINyeHp8fvm07 71ccwJjN+IRCj0NwYaM26SftmmGs8LIiwOcqSlD4PS+aHzaLfTM9sDKRPobqTKCTEzJ1SN W1U9fDPjYoCQNrPbDO/G3Bl6uucMl6QFH+g4NiP0/Z4l1Wfbr4BLZqeQ2fAAEQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4PHbfQ4J2Tz15RJ; Thu, 16 Feb 2023 13:39:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 31GDdwMN000575; Thu, 16 Feb 2023 13:39:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 31GDdw2R000574; Thu, 16 Feb 2023 13:39:58 GMT (envelope-from git) Date: Thu, 16 Feb 2023 13:39:58 GMT Message-Id: <202302161339.31GDdw2R000574@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: 86fd0bdba540 - main - netlink: fix interface dump. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 86fd0bdba540132ae298457e160b651f61d1db6b Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=86fd0bdba540132ae298457e160b651f61d1db6b commit 86fd0bdba540132ae298457e160b651f61d1db6b Author: Alexander V. Chernikov AuthorDate: 2023-02-16 13:17:58 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-02-16 13:20:45 +0000 netlink: fix interface dump. The current code missed interface addition when reallocating temporary buffer. Tweak the code to perform the reallocation first and add interface afterwards unconditionally. Reported by: Marek Zarychta MFC after: 3 days --- sys/netlink/route/iface.c | 33 ++++++++++--------- tests/sys/netlink/test_rtnl_iface.py | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/sys/netlink/route/iface.c b/sys/netlink/route/iface.c index 9cd7e6e80f3c..0eafacff4775 100644 --- a/sys/netlink/route/iface.c +++ b/sys/netlink/route/iface.c @@ -449,24 +449,23 @@ rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *n CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { wa.count++; if (match_iface(&attrs, ifp)) { - if (offset < base_count) { - if (!if_try_ref(ifp)) - continue; - match_array[offset++] = ifp; - continue; - } - /* Too many matches, need to reallocate */ - struct ifnet **new_array; - int sz = base_count * sizeof(void *); - base_count *= 2; - new_array = malloc(sz * 2, M_TEMP, M_NOWAIT); - if (new_array == NULL) { - error = ENOMEM; - break; + if (offset >= base_count) { + /* Too many matches, need to reallocate */ + struct ifnet **new_array; + int sz = base_count * sizeof(void *); + base_count *= 2; + new_array = malloc(sz * 2, M_TEMP, M_NOWAIT); + if (new_array == NULL) { + error = ENOMEM; + break; + } + memcpy(new_array, match_array, sz); + free(match_array, M_TEMP); + match_array = new_array; } - memcpy(new_array, match_array, sz); - free(match_array, M_TEMP); - match_array = new_array; + + if (if_try_ref(ifp)) + match_array[offset++] = ifp; } } NET_EPOCH_EXIT(et); diff --git a/tests/sys/netlink/test_rtnl_iface.py b/tests/sys/netlink/test_rtnl_iface.py index ecc932c6213e..cbe5aed8b8ae 100644 --- a/tests/sys/netlink/test_rtnl_iface.py +++ b/tests/sys/netlink/test_rtnl_iface.py @@ -242,6 +242,67 @@ class TestRtNlIface(NetlinkTestTemplate, SingleVnetTestTemplate): assert rx_msg.is_type(NlMsgType.NLMSG_ERROR) assert rx_msg.error_code == errno.ENODEV + @pytest.mark.require_user("root") + def test_dump_ifaces_many(self): + """Tests if interface dummp is not missing interfaces""" + + ifmap = {} + for ifname in (self.vnet.iface_alias_map["if1"].name, "lo0"): + ifindex = socket.if_nametoindex(ifname) + ifmap[ifindex] = ifname + + for i in range(40): + ifname = "lo{}".format(i + 1) + flags = NlmNewFlags.NLM_F_EXCL.value | NlmNewFlags.NLM_F_CREATE.value + msg = NetlinkIflaMessage(self.helper, NlRtMsgType.RTM_NEWLINK.value) + msg.nl_hdr.nlmsg_flags = ( + flags | NlmBaseFlags.NLM_F_ACK.value | NlmBaseFlags.NLM_F_REQUEST.value + ) + msg.add_nla(NlAttrStr(IflattrType.IFLA_IFNAME, ifname)) + msg.add_nla( + NlAttrNested( + IflattrType.IFLA_LINKINFO, + [ + NlAttrStrn(IflinkInfo.IFLA_INFO_KIND, "lo"), + ], + ) + ) + + rx_msg = self.get_reply(msg) + assert rx_msg.is_type(NlMsgType.NLMSG_ERROR) + nla_list, _ = rx_msg.parse_attrs(bytes(rx_msg.cookie)[4:], rtnl_ifla_attrs) + nla_map = {n.nla_type: n for n in nla_list} + assert nla_map[IflattrType.IFLA_IFNAME.value].text == ifname + ifindex = nla_map[IflattrType.IFLA_NEW_IFINDEX.value].u32 + assert ifindex > 0 + assert ifindex not in ifmap + ifmap[ifindex] = ifname + + # Dump all interfaces and check if the output matches ifmap + kernel_ifmap = {} + msg = NetlinkIflaMessage(self.helper, NlRtMsgType.RTM_GETLINK.value) + msg.nl_hdr.nlmsg_flags = ( + NlmBaseFlags.NLM_F_ACK.value | NlmBaseFlags.NLM_F_REQUEST.value + ) + self.write_message(msg) + while True: + rx_msg = self.read_message() + if msg.nl_hdr.nlmsg_seq != rx_msg.nl_hdr.nlmsg_seq: + raise ValueError( + "unexpected seq {}".format(rx_msg.nl_hdr.nlmsg_seq) + ) + if rx_msg.is_type(NlMsgType.NLMSG_ERROR): + raise ValueError("unexpected message {}".format(rx_msg)) + if rx_msg.is_type(NlMsgType.NLMSG_DONE): + break + if not rx_msg.is_type(NlRtMsgType.RTM_NEWLINK): + raise ValueError("unexpected message {}".format(rx_msg)) + + ifindex = rx_msg.base_hdr.ifi_index + assert ifindex == rx_msg.base_hdr.ifi_index + kernel_ifmap[ifindex] = rx_msg.get_nla(IflattrType.IFLA_IFNAME).text + assert kernel_ifmap == ifmap + # # * # * {len=76, type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1662892737, pid=0},