From nobody Thu Nov 03 17:06:43 2022 X-Original-To: dev-commits-src-all@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 4N39CR31bfz4hRv8; Thu, 3 Nov 2022 17:06:43 +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 4N39CR2LNkz3vsC; Thu, 3 Nov 2022 17:06:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1667495203; 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=ff32TlYcLfUuK+2OsJFVsn+SlqVNCMbMc4Is72yNu6A=; b=knQAHMWKen9V8phHmM3JMHj+QEC4HCshL1PHHoJIpsQw+XzbaprnO1RSVEYmUOiv46VseF kxMg6KhdfpgclION8B3yGkGKqyKjlIhI7FYIFypc/Gu4CBfawpKF96y0jRTOYaL/9Ur4HC 6Wq2ZAOxUuEYuRmVQcj1nCJaC8949ZYxqbudroZm7uLw+K/ngF2uZ3eWjxRGFW0lTfCT1d alQ8e8bOXoNpdan7bVLPttY3Lr7VS7476yWgU9DSv+pVIzJDoZOOwjvmpxrAzoR7Ir+Z9C Izv4CVCaVIsWvfcOmkOLx0dA0XRsPBl5NwYKyukhg4hUW229ukenNhqcJEOk6Q== 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 4N39CR0vtdzk0f; Thu, 3 Nov 2022 17:06:43 +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 2A3H6hfs000796; Thu, 3 Nov 2022 17:06:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2A3H6hqj000795; Thu, 3 Nov 2022 17:06:43 GMT (envelope-from git) Date: Thu, 3 Nov 2022 17:06:43 GMT Message-Id: <202211031706.2A3H6hqj000795@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: 4dfd380e06c5 - main - netlink: allow more than 64 groups per netlink socket. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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: 4dfd380e06c515349c5cc55a1f05effbf3a44ba1 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1667495203; 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=ff32TlYcLfUuK+2OsJFVsn+SlqVNCMbMc4Is72yNu6A=; b=r0uSzDu5oju5WLi7Rm0ZkSmpccS/sd1ju3FUKYcWzV7LCTkoMS1W8kxxro7LXZV1qNpqcj Y1WP6qYo2F5R+eASEh6HsoTw28FNW1iMoZsuMlMRXswIsIEe+JRnBJRDu2gdxgbN7iNZoA ZWcZhWBkMxiWLaxpaKG7oT1Vq9w+usHhru9ZhQiTVTiD2H8nhg6kbdACk4RSpHIAejKMHF hGhqjgSLkmraz+AXFx5Vnvu7k/PHx0/d2Oo6WRLnQPHz4CtIC2vhvCGYRfmTZd6NeFnku9 hDiDnWjv3KzJrKSixvMXEC6RbnTAyZoLjudEK9jfDYeSk2ppdjGbDKjrcKnl6g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1667495203; a=rsa-sha256; cv=none; b=Ei8auqu5+h4pxtnfJY+OhCv/XAIW+o+0nSqh3/zbF5m5x0QnRfKzcTJy2yB/sJRKKP/bks n2x/8FOpesz/wa5UYNc1cM7eNnzctlkZ7VxT4AMlBmsUt5ijhxMEcFVVXQ6LYLGZDSJbll AusalD4DP5B8Pk3E2bRPCf+76+K93IVR2hNEa9bLXuDYvBv36MatptVnmBs9tahgGQpvzv bIjoQ0yXr3RvfT010TbJ3diK/gO+uLqc+3cZEFFK9TIvAlkQMY8JW4GdhK3XG1WvK36d9s BxCRzSjQD2yh4lr/WScTPy6fUIvm+Y/c/YIe0dgzt6H0GQ7NbL+x7BYN6A+NwA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=4dfd380e06c515349c5cc55a1f05effbf3a44ba1 commit 4dfd380e06c515349c5cc55a1f05effbf3a44ba1 Author: Alexander V. Chernikov AuthorDate: 2022-11-03 16:44:07 +0000 Commit: Alexander V. Chernikov CommitDate: 2022-11-03 17:05:34 +0000 netlink: allow more than 64 groups per netlink socket. --- sys/netlink/netlink_domain.c | 71 ++++++++++++++++++++++++++++++++----------- sys/netlink/netlink_generic.c | 2 +- sys/netlink/netlink_var.h | 4 ++- 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/sys/netlink/netlink_domain.c b/sys/netlink/netlink_domain.c index 44b5fb732896..b0ff84401c84 100644 --- a/sys/netlink/netlink_domain.c +++ b/sys/netlink/netlink_domain.c @@ -55,6 +55,10 @@ #include _DECLARE_DEBUG(LOG_DEBUG); +_Static_assert((NLP_MAX_GROUPS % 64) == 0, + "NLP_MAX_GROUPS has to be multiple of 64"); +_Static_assert(NLP_MAX_GROUPS >= 64, + "NLP_MAX_GROUPS has to be at least 64"); #define NLCTL_TRACKER struct rm_priotracker nl_tracker #define NLCTL_RLOCK(_ctl) rm_rlock(&((_ctl)->ctl_lock), &nl_tracker) @@ -97,12 +101,43 @@ nl_port_lookup(uint32_t port_id) } static void -nl_update_groups_locked(struct nlpcb *nlp, uint64_t nl_groups) +nl_add_group_locked(struct nlpcb *nlp, unsigned int group_id) { - /* Update group mask */ - NL_LOG(LOG_DEBUG2, "socket %p, groups 0x%X -> 0x%X", - nlp->nl_socket, (uint32_t)nlp->nl_groups, (uint32_t)nl_groups); - nlp->nl_groups = nl_groups; + MPASS(group_id <= NLP_MAX_GROUPS); + --group_id; + + nlp->nl_groups[group_id / 64] |= (uint64_t)1 << (group_id % 64); +} + +static void +nl_del_group_locked(struct nlpcb *nlp, unsigned int group_id) +{ + MPASS(group_id <= NLP_MAX_GROUPS); + --group_id; + + nlp->nl_groups[group_id / 64] &= ~((uint64_t)1 << (group_id % 64)); +} + +static bool +nl_isset_group_locked(struct nlpcb *nlp, unsigned int group_id) +{ + MPASS(group_id <= NLP_MAX_GROUPS); + --group_id; + + return (nlp->nl_groups[group_id / 64] & ((uint64_t)1 << (group_id % 64))); +} + +static uint32_t +nl_get_groups_compat(struct nlpcb *nlp) +{ + uint32_t groups_mask = 0; + + for (int i = 0; i < 32; i++) { + if (nl_isset_group_locked(nlp, i + 1)) + groups_mask |= (1 << i); + } + + return (groups_mask); } /* @@ -134,10 +169,9 @@ nl_send_group(struct mbuf *m, int num_messages, int proto, int group_id) NLCTL_RLOCK(ctl); int io_flags = NL_IOF_UNTRANSLATED; - uint64_t groups_mask = 1 << ((uint64_t)group_id - 1); CK_LIST_FOREACH(nlp, &ctl->ctl_pcb_head, nl_next) { - if (nlp->nl_groups & groups_mask && nlp->nl_proto == proto) { + if (nl_isset_group_locked(nlp, group_id) && nlp->nl_proto == proto) { if (nlp_last != NULL) { struct mbuf *m_copy; m_copy = m_copym(m, 0, M_COPYALL, M_NOWAIT); @@ -213,7 +247,12 @@ nl_bind_locked(struct nlpcb *nlp, struct sockaddr_nl *snl) nlp->nl_bound = true; CK_LIST_INSERT_HEAD(&V_nl_ctl->ctl_port_head, nlp, nl_port_next); } - nl_update_groups_locked(nlp, snl->nl_groups); + for (int i = 0; i < 32; i++) { + if (snl->nl_groups & ((uint32_t)1 << i)) + nl_add_group_locked(nlp, i + 1); + else + nl_del_group_locked(nlp, i + 1); + } return (0); } @@ -324,7 +363,7 @@ nl_assign_port(struct nlpcb *nlp, uint32_t port_id) NLCTL_WLOCK(ctl); NLP_LOCK(nlp); - snl.nl_groups = nlp->nl_groups; + snl.nl_groups = nl_get_groups_compat(nlp); error = nl_bind_locked(nlp, &snl); NLP_UNLOCK(nlp); NLCTL_WUNLOCK(ctl); @@ -562,7 +601,6 @@ nl_ctloutput(struct socket *so, struct sockopt *sopt) struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl); struct nlpcb *nlp = sotonlpcb(so); uint32_t flag; - uint64_t groups, group_mask; int optval, error = 0; NLCTL_TRACKER; @@ -575,20 +613,17 @@ nl_ctloutput(struct socket *so, struct sockopt *sopt) case NETLINK_ADD_MEMBERSHIP: case NETLINK_DROP_MEMBERSHIP: sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); - if (optval <= 0 || optval >= 64) { + if (optval <= 0 || optval >= NLP_MAX_GROUPS) { error = ERANGE; break; } - group_mask = (uint64_t)1 << (optval - 1); - NL_LOG(LOG_DEBUG2, "ADD/DEL group %d mask (%X)", - (uint32_t)optval, (uint32_t)group_mask); + NL_LOG(LOG_DEBUG2, "ADD/DEL group %d", (uint32_t)optval); NLCTL_WLOCK(ctl); if (sopt->sopt_name == NETLINK_ADD_MEMBERSHIP) - groups = nlp->nl_groups | group_mask; + nl_add_group_locked(nlp, optval); else - groups = nlp->nl_groups & ~group_mask; - nl_update_groups_locked(nlp, groups); + nl_del_group_locked(nlp, optval); NLCTL_WUNLOCK(ctl); break; case NETLINK_CAP_ACK: @@ -613,7 +648,7 @@ nl_ctloutput(struct socket *so, struct sockopt *sopt) switch (sopt->sopt_name) { case NETLINK_LIST_MEMBERSHIPS: NLCTL_RLOCK(ctl); - optval = nlp->nl_groups; + optval = nl_get_groups_compat(nlp); NLCTL_RUNLOCK(ctl); error = sooptcopyout(sopt, &optval, sizeof(optval)); break; diff --git a/sys/netlink/netlink_generic.c b/sys/netlink/netlink_generic.c index 5d074640ad60..de45048ff519 100644 --- a/sys/netlink/netlink_generic.c +++ b/sys/netlink/netlink_generic.c @@ -47,7 +47,7 @@ __FBSDID("$FreeBSD$"); _DECLARE_DEBUG(LOG_DEBUG3); #define MAX_FAMILIES 20 -#define MAX_GROUPS 20 +#define MAX_GROUPS 64 #define MIN_GROUP_NUM 48 diff --git a/sys/netlink/netlink_var.h b/sys/netlink/netlink_var.h index 130f3d40a1a3..ed19008248e9 100644 --- a/sys/netlink/netlink_var.h +++ b/sys/netlink/netlink_var.h @@ -47,9 +47,11 @@ struct nl_io_queue { int hiwat; }; +#define NLP_MAX_GROUPS 128 + struct nlpcb { struct socket *nl_socket; - uint64_t nl_groups; + uint64_t nl_groups[NLP_MAX_GROUPS / 64]; uint32_t nl_port; uint32_t nl_flags; uint32_t nl_process_id;