From patchwork Thu Nov 23 18:15:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13466526 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="E2K7V/Rt" Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4C841A4 for ; Thu, 23 Nov 2023 10:15:50 -0800 (PST) Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-9ffef4b2741so152824766b.3 for ; Thu, 23 Nov 2023 10:15:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1700763349; x=1701368149; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GeAdk1AG9hSINhpR6YsiiDYoZ7W2jtuFfmUK14lu3AU=; b=E2K7V/RtxFuJeLOxnamJQxH4PVuAfgo0Ex2v5iIY0DSP3Mt+Dla/r7N2OOlatrYBaY eRdrsLRrzEUyCNQabCoCCQpGk5y7lZZausvxUfga28yo4KB81PmdPMNoa813KcvF+kGV 9SNojmFmifZcV+WVRKQfsBqsOv9EVWwUGnSex4Bkj3C2zXUYY+HzIW4JBCRlNo/hEnA8 dSao08iwOAcp6rPvm1raaKaMUgHlAXC2hnzPh3HZVfIcDwWJi4HAoJmfZfcYbTZYQr2d ssiyBuDHncfX1wkM8+36EO8Ms8I7drQkbzj8OO8X0MG/acEk3m47I3uSFaHSgxiRzzCa 4RQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700763349; x=1701368149; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GeAdk1AG9hSINhpR6YsiiDYoZ7W2jtuFfmUK14lu3AU=; b=QxqiSpguefsZTuVW4am2fMxkUOPO7a3HNivhH2zEsfkzOv1BldLkm63cNsUC1ASQyA wXyK3Ss1eqjqMcrbizsJumICbbKMovrQ7zTlYFRW169ZZmv3PjRh8JGWUwB6Z1BiqoKA WlCmKDXrcrrHSLDkkJiqsEqiNoe/MwmNQSmEOy2EL3/bbziiwsOMy8Dso20jyWaWByWR DJrM+/2wffhMeAUxXSka2bb0X45IA3MowjVPThqnc4uVZ3VTg4fTowX5nk0UL90SsSCF X2qxA9aNaL4rY3ov1g7zwq7xBp55aECu6WAqFgltW2Kpx0XW/J4158ZWU9nr0p+MiNFa rpHg== X-Gm-Message-State: AOJu0Yw5tFUBWg+2HscGMgpAryThkNodvJ2wCPZ2LTuPc4T+wJynsSxr n4XE7RNW5a0A3+ESNQ5NmcYMLszkSi1nDx4Y1+Y= X-Google-Smtp-Source: AGHT+IEYTMO/88m1Ov7YlTUTEeGbA/dEMTVK+wSYb+COjO2jhVgA7NPBKXifPMNQrmieHAPDcnMQtw== X-Received: by 2002:a17:906:6809:b0:9fe:42aa:425d with SMTP id k9-20020a170906680900b009fe42aa425dmr68598ejr.76.1700763349377; Thu, 23 Nov 2023 10:15:49 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id gr19-20020a170906e2d300b009de11bcbbcasm1068704ejb.175.2023.11.23.10.15.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:15:48 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org Subject: [patch net-next v4 1/9] devlink: use devl_is_registered() helper instead xa_get_mark() Date: Thu, 23 Nov 2023 19:15:38 +0100 Message-ID: <20231123181546.521488-2-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231123181546.521488-1-jiri@resnulli.us> References: <20231123181546.521488-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko Instead of checking the xarray mark directly using xa_get_mark() helper use devl_is_registered() helper which wraps it up. Note that there are couple more users of xa_get_mark() left which are going to be handled by the next patch. Signed-off-by: Jiri Pirko --- net/devlink/dev.c | 4 ++-- net/devlink/rate.c | 2 +- net/devlink/trap.c | 9 ++++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/net/devlink/dev.c b/net/devlink/dev.c index ea6a92f2e6a2..7c7517e26862 100644 --- a/net/devlink/dev.c +++ b/net/devlink/dev.c @@ -202,7 +202,7 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) int err; WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL); - WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)); + WARN_ON(!devl_is_registered(devlink)); msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); if (!msg) @@ -985,7 +985,7 @@ static void __devlink_flash_update_notify(struct devlink *devlink, cmd != DEVLINK_CMD_FLASH_UPDATE_END && cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + if (!devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/rate.c b/net/devlink/rate.c index 94b289b93ff2..e2190cf22beb 100644 --- a/net/devlink/rate.c +++ b/net/devlink/rate.c @@ -146,7 +146,7 @@ static void devlink_rate_notify(struct devlink_rate *devlink_rate, WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + if (!devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/trap.c b/net/devlink/trap.c index c26313e7ca08..908085e2c990 100644 --- a/net/devlink/trap.c +++ b/net/devlink/trap.c @@ -1173,7 +1173,8 @@ devlink_trap_group_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW && cmd != DEVLINK_CMD_TRAP_GROUP_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + + if (!devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); @@ -1234,7 +1235,8 @@ static void devlink_trap_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW && cmd != DEVLINK_CMD_TRAP_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + + if (!devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); @@ -1710,7 +1712,8 @@ devlink_trap_policer_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW && cmd != DEVLINK_CMD_TRAP_POLICER_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + + if (!devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); From patchwork Thu Nov 23 18:15:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13466527 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="D/vDwItC" Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D19718B for ; Thu, 23 Nov 2023 10:15:52 -0800 (PST) Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-5446c9f3a77so1544885a12.0 for ; Thu, 23 Nov 2023 10:15:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1700763351; x=1701368151; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GqAP4zVW3tr3kQv/U3Y5QSJhXtJi2tdSQ5AvLLkkNsk=; b=D/vDwItCt7d1D+aNCm60a5jHecf7CoeKdFJL56986PwlEWG7AqvursK/QgUWFmEjtf zA8N/5WZpJlK8snmDJ2ESg4ij0oy6pRTJzp4KI33XhitinU8V37hynqBPeHIGxvnbnJl Ekxr7U6WFel9YYT3NZ5cU7ep/+DY034tn8wPsmot70P7IBdZcR6sk1s4hGTrRF4kuZkS GMmwEh+fGz79lJdgnH/8oQY+uR4GpWlLyFhQvYstN1HLdA5Co4cDdLSMqt9OR4v8rzfZ t14Nd10sZg/Fdeqsx+BTCNgNM1DEr4Pewiw2/+SlMadPewwxj7kc6zuSptvqgiUkVPRN BGpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700763351; x=1701368151; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GqAP4zVW3tr3kQv/U3Y5QSJhXtJi2tdSQ5AvLLkkNsk=; b=c9y/CgW+5jVoBgXf0qbr0NVaJH6JfIRN2LLf48f6GUMgmWXvIzUGdx7N+RXlDZQZQi ImyxKyiOVZahr8zrbWECpFgqIcYl5/9H0Rfy5BrijL1+HJ6XE+/MOmpXZSZYvdi5cL4y Z++D2D3BShcfW0robpmf10FnOVsHSx0h0taWxU3rcC8PEdV1pU1NuOrFEcQfxw/Ph+c4 zCt9JlUzLhaNvzFz3X09K1J0T7ooP3OdWpJ5Q2uxmML0x3NtXxzJmnkr4Obc5m1oeRbw 4dsEWMC5xBu3AmmISzeBy6InmCFjTRKgTc7N32mDzEG0To1wsgyHw/wHZ+2rhz+j4wt+ v6KA== X-Gm-Message-State: AOJu0YzNlJZJsZayE7uqHRfzP4XkANd14YB4D94Fy6emK38R9pr4JEa9 ZOUT1U+/x0RUV5m1ZylV8h9L6nDD795Rw0KbCZY= X-Google-Smtp-Source: AGHT+IFft39K42cm+cRmvoL4Q+3Ru+JmB5TgyIwpiz2EsqC/JTQryYLVQOmL+WfdTvut/foFLeAM1g== X-Received: by 2002:a50:875e:0:b0:548:4afe:c58b with SMTP id 30-20020a50875e000000b005484afec58bmr29820edv.34.1700763350984; Thu, 23 Nov 2023 10:15:50 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id q1-20020a056402040100b00536ad96f867sm900617edv.11.2023.11.23.10.15.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:15:50 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org Subject: [patch net-next v4 2/9] devlink: introduce __devl_is_registered() helper and use it instead of xa_get_mark() Date: Thu, 23 Nov 2023 19:15:39 +0100 Message-ID: <20231123181546.521488-3-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231123181546.521488-1-jiri@resnulli.us> References: <20231123181546.521488-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko Introduce __devl_is_registered() which does not assert on devlink instance lock and use it in notifications which may be called without devlink instance lock held. Signed-off-by: Jiri Pirko --- net/devlink/devl_internal.h | 7 ++++++- net/devlink/linecard.c | 2 +- net/devlink/port.c | 2 +- net/devlink/region.c | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 5ea2e2012e93..59ae4761d10a 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -91,10 +91,15 @@ extern struct genl_family devlink_nl_family; struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp); +static inline bool __devl_is_registered(struct devlink *devlink) +{ + return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED); +} + static inline bool devl_is_registered(struct devlink *devlink) { devl_assert_locked(devlink); - return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED); + return __devl_is_registered(devlink); } static inline void devl_dev_lock(struct devlink *devlink, bool dev_lock) diff --git a/net/devlink/linecard.c b/net/devlink/linecard.c index 2f1c317b64cd..9d080ac1734b 100644 --- a/net/devlink/linecard.c +++ b/net/devlink/linecard.c @@ -136,7 +136,7 @@ static void devlink_linecard_notify(struct devlink_linecard *linecard, WARN_ON(cmd != DEVLINK_CMD_LINECARD_NEW && cmd != DEVLINK_CMD_LINECARD_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + if (!__devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/port.c b/net/devlink/port.c index 7634f187fa50..f229a8699214 100644 --- a/net/devlink/port.c +++ b/net/devlink/port.c @@ -512,7 +512,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port, WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + if (!__devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/region.c b/net/devlink/region.c index e3bab458db94..b65181aa269a 100644 --- a/net/devlink/region.c +++ b/net/devlink/region.c @@ -234,7 +234,8 @@ static void devlink_nl_region_notify(struct devlink_region *region, struct sk_buff *msg; WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + + if (!__devl_is_registered(devlink)) return; msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0); From patchwork Thu Nov 23 18:15:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13466528 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="C7n/o6kt" Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36535D41 for ; Thu, 23 Nov 2023 10:15:54 -0800 (PST) Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-507a29c7eefso1426813e87.1 for ; Thu, 23 Nov 2023 10:15:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1700763352; x=1701368152; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+qZQpcrwaO0fKr36nlIRFsQdhK07pBunnGGo+UTRz3Q=; b=C7n/o6kteYaLDftMzdwhu1dGTRbkVrgUF1LeyA+QMODb3MwIn+0dxyOsrUadIQ+xzH HRWZq4T6hsNcpypnHa3pjy+r+kVgpYiHn+yxHT/ZFbnQRY250noVuqiAkrh6KhCoGlJj l/PPVqiL3QUFrJmQNM/vDEs3v0b77IXhVxiEb4nsJob6JpK+CYSHW12N2JWHHd5yjN8U vgdPRoOqQc0QtW5uZ3nVFYwIAE8/Ba8TMSqoSo4VpIxoYqaXJybw7y4txkOyiEfPzPtb Prp60+tbV2kjKdWWcx5zJbgkRmEk5A+1hEyt7YKndAaBGbvqXydu0z4xb4NCSEoEs6iV oQXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700763352; x=1701368152; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+qZQpcrwaO0fKr36nlIRFsQdhK07pBunnGGo+UTRz3Q=; b=WwPf6FzyZz+e0UrlF5VZ+nGQQdexyZBnavXKbJEZnEuLtwp3rPQkC7oEJ8ijSOC2m3 1k6m/HX15yvxoO53reBqAUfe4LEhiWnh04tV5JG0AZlOKPuua7xVsSO47qrnalPCamMI qWlfKGgOdQtHheb8WFJDZkf5Gk7VQxBApL10Pv+XErW74PxBRpEoxMa8LpyMEv5Hdq/Z brRCdODsTB9bI1qGco62ldQ5bJjpOJHkEWALFWYYPLzvtUATuNb+UNCyF3ITv7aHOb1m CovIh+qHhYypx4zdlfZkRkpBYnk5SlX0ZqegjE9zB333HxRFTgQPCZY5ZN4QL/Q/HzbJ k/BA== X-Gm-Message-State: AOJu0Yw+7SHx3tne0alB49dibGsJO1B8e4H874c3EnNpanLQQq/pmOxS YlxaSpYfayXtgz5+7SEMXGINpLGR3ov4hKU9grs= X-Google-Smtp-Source: AGHT+IE/6DsKlXI5Q/tlPKYMuET+1Ern0s/LzpWNBHDV9sEolM7W1Ib5r3OE3eejEXnwbZyzminfng== X-Received: by 2002:a05:6512:3605:b0:503:343a:829f with SMTP id f5-20020a056512360500b00503343a829fmr44593lfs.23.1700763352564; Thu, 23 Nov 2023 10:15:52 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id a21-20020a1709064a5500b009eff65e6942sm1064929ejv.197.2023.11.23.10.15.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:15:52 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org Subject: [patch net-next v4 3/9] devlink: send notifications only if there are listeners Date: Thu, 23 Nov 2023 19:15:40 +0100 Message-ID: <20231123181546.521488-4-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231123181546.521488-1-jiri@resnulli.us> References: <20231123181546.521488-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko Introduce devlink_nl_notify_need() helper and using it to check at the beginning of notification functions to avoid overhead of composing notification messages in case nobody listens. Signed-off-by: Jiri Pirko --- net/devlink/dev.c | 5 ++++- net/devlink/devl_internal.h | 6 ++++++ net/devlink/health.c | 3 +++ net/devlink/linecard.c | 2 +- net/devlink/param.c | 2 +- net/devlink/port.c | 2 +- net/devlink/rate.c | 2 +- net/devlink/region.c | 2 +- net/devlink/trap.c | 6 +++--- 9 files changed, 21 insertions(+), 9 deletions(-) diff --git a/net/devlink/dev.c b/net/devlink/dev.c index 7c7517e26862..46407689ef70 100644 --- a/net/devlink/dev.c +++ b/net/devlink/dev.c @@ -204,6 +204,9 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL); WARN_ON(!devl_is_registered(devlink)); + if (!devlink_nl_notify_need(devlink)) + return; + msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); if (!msg) return; @@ -985,7 +988,7 @@ static void __devlink_flash_update_notify(struct devlink *devlink, cmd != DEVLINK_CMD_FLASH_UPDATE_END && cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS); - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 59ae4761d10a..510990de094e 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -185,6 +185,12 @@ int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net, struct devlink *devlink, int attrtype); int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info); +static inline bool devlink_nl_notify_need(struct devlink *devlink) +{ + return genl_has_listeners(&devlink_nl_family, devlink_net(devlink), + DEVLINK_MCGRP_CONFIG); +} + /* Notify */ void devlink_notify_register(struct devlink *devlink); void devlink_notify_unregister(struct devlink *devlink); diff --git a/net/devlink/health.c b/net/devlink/health.c index 71ae121dc739..0795dcf22ca8 100644 --- a/net/devlink/health.c +++ b/net/devlink/health.c @@ -496,6 +496,9 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter, WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER); ASSERT_DEVLINK_REGISTERED(devlink); + if (!devlink_nl_notify_need(devlink)) + return; + msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); if (!msg) return; diff --git a/net/devlink/linecard.c b/net/devlink/linecard.c index 9d080ac1734b..45b36975ee6f 100644 --- a/net/devlink/linecard.c +++ b/net/devlink/linecard.c @@ -136,7 +136,7 @@ static void devlink_linecard_notify(struct devlink_linecard *linecard, WARN_ON(cmd != DEVLINK_CMD_LINECARD_NEW && cmd != DEVLINK_CMD_LINECARD_DEL); - if (!__devl_is_registered(devlink)) + if (!__devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/param.c b/net/devlink/param.c index d74df09311a9..6bb6aee5d937 100644 --- a/net/devlink/param.c +++ b/net/devlink/param.c @@ -343,7 +343,7 @@ static void devlink_param_notify(struct devlink *devlink, * will replay the notifications if the params are added/removed * outside of the lifetime of the instance. */ - if (!devl_is_registered(devlink)) + if (!devlink_nl_notify_need(devlink) || !devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/port.c b/net/devlink/port.c index f229a8699214..32f4d0331e63 100644 --- a/net/devlink/port.c +++ b/net/devlink/port.c @@ -512,7 +512,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port, WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); - if (!__devl_is_registered(devlink)) + if (!__devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/rate.c b/net/devlink/rate.c index e2190cf22beb..0371a2dd3e0a 100644 --- a/net/devlink/rate.c +++ b/net/devlink/rate.c @@ -146,7 +146,7 @@ static void devlink_rate_notify(struct devlink_rate *devlink_rate, WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL); - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/region.c b/net/devlink/region.c index b65181aa269a..bf61312f64bd 100644 --- a/net/devlink/region.c +++ b/net/devlink/region.c @@ -235,7 +235,7 @@ static void devlink_nl_region_notify(struct devlink_region *region, WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL); - if (!__devl_is_registered(devlink)) + if (!__devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0); diff --git a/net/devlink/trap.c b/net/devlink/trap.c index 908085e2c990..3ca1ca7e2e64 100644 --- a/net/devlink/trap.c +++ b/net/devlink/trap.c @@ -1174,7 +1174,7 @@ devlink_trap_group_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW && cmd != DEVLINK_CMD_TRAP_GROUP_DEL); - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); @@ -1236,7 +1236,7 @@ static void devlink_trap_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW && cmd != DEVLINK_CMD_TRAP_DEL); - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); @@ -1713,7 +1713,7 @@ devlink_trap_policer_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW && cmd != DEVLINK_CMD_TRAP_POLICER_DEL); - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); From patchwork Thu Nov 23 18:15:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13466529 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="wwwqRKBn" Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF292189 for ; Thu, 23 Nov 2023 10:15:55 -0800 (PST) Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-5482df11e73so1618226a12.0 for ; Thu, 23 Nov 2023 10:15:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1700763354; x=1701368154; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y7/qvj3bNwIJMnLyXZaJbnb+/l/Lk8gMTI+iXShTOMY=; b=wwwqRKBnixtR3Af/hg3w9Sn/ncLIMrB1PqVRuYz+YlyfaMQwNmlNzi6+pHrTZTP8HU 51+lXVU6kJwt1y/FmdJ8e0lvNcZcJALmPnUZqWUT+DdmV8HX7d7UnmR4iKoPEq3BfnN0 cP/N8nUjcRJ7D38G+AIcIB83V4XEjI2v38hgodWOvR12ikegJZnxdXV0iZI1uBCTmrSw qKy8eV6HGrEAVGUMnTBU+T8YFE4dOzezeaMS/YFwHMUeNDp0tYme8xI8ArxpiBNHzViL s5d1NW2dpcPdpCYXVn/6MRv96sO4HWADRL5d2RSgVgcF17TpxpsGiRvOPI759TublGmV n++A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700763354; x=1701368154; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y7/qvj3bNwIJMnLyXZaJbnb+/l/Lk8gMTI+iXShTOMY=; b=M7iZ5xnAhOVkLN5xiNAtNn0A9fwzESHBvyErLTmmfLPCaKXZTMsV0N8JzXM8iRGHK9 p38EkOWZhTU17DOtEpFHg/x7gYe0w2iNrXpQjYYN3fQ0tNDaMPWxYSFMhp7xN/ZVPDxU 0TIwHApbPEWObPIL2piHZi7WW94JK9chErOC83lRAjeIvuou33X2fUOFSIIF+Fsbh6jz w6Zdr5dImC2khoCZqbIG0i0qV2Zz865xGuoxJcGtDv1jj7W777vsmwIkntHQWqhaw6nq xdUdfEOdVRzXsDbLvZq7o//E8gXW8R7GdNrpinGGHJtlrUZ8/WuoJASRs47qgWMrt2Y0 Uowg== X-Gm-Message-State: AOJu0YzjaxbgtpDtYL5EZIr3JvBwEh40zZjWvsuFoHiTh0wqhR7zzbtB /ZZZ/PcV/ErXxYk7OAfEG+iNz9Hp6oX8ugmsazg= X-Google-Smtp-Source: AGHT+IGMrM/1FJZP8HzKjn9WnRO0l5Qj1kokT6AiVkUcDi3Sj1KtIWtAGuQkCohLgSdMDD3gOTc1hA== X-Received: by 2002:a17:906:b796:b0:9fe:57b3:bc08 with SMTP id dt22-20020a170906b79600b009fe57b3bc08mr78453ejb.41.1700763354170; Thu, 23 Nov 2023 10:15:54 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id w16-20020a170906131000b009dd701bb916sm1061958ejb.213.2023.11.23.10.15.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:15:53 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org Subject: [patch net-next v4 4/9] devlink: introduce a helper for netlink multicast send Date: Thu, 23 Nov 2023 19:15:41 +0100 Message-ID: <20231123181546.521488-5-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231123181546.521488-1-jiri@resnulli.us> References: <20231123181546.521488-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko Introduce a helper devlink_nl_notify_send() so each object notification function does not have to call genlmsg_multicast_netns() with the same arguments. Signed-off-by: Jiri Pirko --- net/devlink/dev.c | 6 ++---- net/devlink/devl_internal.h | 7 +++++++ net/devlink/health.c | 3 +-- net/devlink/linecard.c | 3 +-- net/devlink/param.c | 3 +-- net/devlink/port.c | 3 +-- net/devlink/rate.c | 3 +-- net/devlink/region.c | 3 +-- net/devlink/trap.c | 9 +++------ 9 files changed, 18 insertions(+), 22 deletions(-) diff --git a/net/devlink/dev.c b/net/devlink/dev.c index 46407689ef70..4e54700409d1 100644 --- a/net/devlink/dev.c +++ b/net/devlink/dev.c @@ -217,8 +217,7 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } int devlink_nl_get_doit(struct sk_buff *skb, struct genl_info *info) @@ -999,8 +998,7 @@ static void __devlink_flash_update_notify(struct devlink *devlink, if (err) goto out_free_msg; - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); return; out_free_msg: diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 510990de094e..84dc9628d3f2 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -191,6 +191,13 @@ static inline bool devlink_nl_notify_need(struct devlink *devlink) DEVLINK_MCGRP_CONFIG); } +static inline void devlink_nl_notify_send(struct devlink *devlink, + struct sk_buff *msg) +{ + genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), + msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); +} + /* Notify */ void devlink_notify_register(struct devlink *devlink); void devlink_notify_unregister(struct devlink *devlink); diff --git a/net/devlink/health.c b/net/devlink/health.c index 0795dcf22ca8..1d59ec0202f6 100644 --- a/net/devlink/health.c +++ b/net/devlink/health.c @@ -509,8 +509,7 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, - 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void diff --git a/net/devlink/linecard.c b/net/devlink/linecard.c index 45b36975ee6f..67f70a621d27 100644 --- a/net/devlink/linecard.c +++ b/net/devlink/linecard.c @@ -150,8 +150,7 @@ static void devlink_linecard_notify(struct devlink_linecard *linecard, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_linecards_notify_register(struct devlink *devlink) diff --git a/net/devlink/param.c b/net/devlink/param.c index 6bb6aee5d937..854a3af65db9 100644 --- a/net/devlink/param.c +++ b/net/devlink/param.c @@ -356,8 +356,7 @@ static void devlink_param_notify(struct devlink *devlink, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } static void devlink_params_notify(struct devlink *devlink, diff --git a/net/devlink/port.c b/net/devlink/port.c index 32f4d0331e63..758df3000a1b 100644 --- a/net/devlink/port.c +++ b/net/devlink/port.c @@ -525,8 +525,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, - 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } static void devlink_ports_notify(struct devlink *devlink, diff --git a/net/devlink/rate.c b/net/devlink/rate.c index 0371a2dd3e0a..7139e67e93ae 100644 --- a/net/devlink/rate.c +++ b/net/devlink/rate.c @@ -159,8 +159,7 @@ static void devlink_rate_notify(struct devlink_rate *devlink_rate, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, - 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_rates_notify_register(struct devlink *devlink) diff --git a/net/devlink/region.c b/net/devlink/region.c index bf61312f64bd..7319127c5913 100644 --- a/net/devlink/region.c +++ b/net/devlink/region.c @@ -242,8 +242,7 @@ static void devlink_nl_region_notify(struct devlink_region *region, if (IS_ERR(msg)) return; - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, - 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_regions_notify_register(struct devlink *devlink) diff --git a/net/devlink/trap.c b/net/devlink/trap.c index 3ca1ca7e2e64..5d18c7424df1 100644 --- a/net/devlink/trap.c +++ b/net/devlink/trap.c @@ -1188,8 +1188,7 @@ devlink_trap_group_notify(struct devlink *devlink, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_trap_groups_notify_register(struct devlink *devlink) @@ -1249,8 +1248,7 @@ static void devlink_trap_notify(struct devlink *devlink, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_traps_notify_register(struct devlink *devlink) @@ -1727,8 +1725,7 @@ devlink_trap_policer_notify(struct devlink *devlink, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_trap_policers_notify_register(struct devlink *devlink) From patchwork Thu Nov 23 18:15:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13466530 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="X/SF3aNF" Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B254018B for ; Thu, 23 Nov 2023 10:15:57 -0800 (PST) Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-a02ba1f500fso169021966b.0 for ; Thu, 23 Nov 2023 10:15:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1700763356; x=1701368156; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Mwwy3+g0/2vUJo5CWrvXTd3PHNIA9OhF9t50ZvzUSNU=; b=X/SF3aNFyVTIqC+pmzXtYT8HOR0UQ/Xd6riJ5M5tqosqHfLRAdc5DR9ESY8t1Syn1F yO59uTGD0OvbQ7OxYfH2MRyCsIvuJYnox9VTtPreqIzpSuUHMcVvvDrdd3TR1T2AZcEL uRfJQdOAIO/QKphMUd4tMFJWECvfV97q3S8KwV0KGlszcEylh3q+r/VVCU0YTevxx7Ej PEAtdZ94RVoSAdMkamBKh38PsACw5ZAJTe1N0xF+gM52Rttch2Ybm9+Iq7n/OeTqHhbx CX/LZUfENE59NVcyf0tCHZwd1Ih5BYPdqZQYmIqrzkaSuraGQtiH/nUYAwElr38iAYk4 JUxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700763356; x=1701368156; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Mwwy3+g0/2vUJo5CWrvXTd3PHNIA9OhF9t50ZvzUSNU=; b=XwwgNO9qXuyblVUNCe4XJeUPwk848EKMraYFFs/YgZ1URMiPVjLEP/QE6gofTAEOqK Ww5pBdbtHoewIX/LO2COR2kKrB6jqBeWi6/XhWtkja0p5gNb4SbKMa/zka333LSjF7Og tS91yJBFPXtqB6xcATEfy6RHNEaEg2iTozpDzGtTpb/a8Jk7nDCITYhVZ9tJo+14RRoU DDQ+nE1a/pL6qpE1TTcwNpp2VXT3G1TVM++dDHGx/vIaI2N+gWIVSJtCoeVCCHEWPYNT 1jktp2hxlDnOvNO7dAJNIg4psE1FefdY5UnBIkbWUc4lh6cUqXdV3jDrffAEBkVsUjDL WZvw== X-Gm-Message-State: AOJu0YwseOzZ3VWvQYky/OMR3v1nQlntE3DKQJcVxUyR8m+p2+0MpaBN 3HsDY6ncyazNQnQ1xJVYY5NhmtTW7VtIW0cuyZM= X-Google-Smtp-Source: AGHT+IEO/sV/u7A6u53y0ur8lxUWuHEWJ9X2YtGpGlbUM78NrGoyYLvLYcJ31WwDjKH23Goz3YDuDw== X-Received: by 2002:a17:906:de:b0:a00:35d9:eb0 with SMTP id 30-20020a17090600de00b00a0035d90eb0mr94194eji.1.1700763355998; Thu, 23 Nov 2023 10:15:55 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id t24-20020a17090616d800b009ffb4af0505sm1065515ejd.104.2023.11.23.10.15.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:15:55 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org Subject: [patch net-next v4 5/9] genetlink: introduce per-sock family private pointer storage Date: Thu, 23 Nov 2023 19:15:42 +0100 Message-ID: <20231123181546.521488-6-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231123181546.521488-1-jiri@resnulli.us> References: <20231123181546.521488-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko Introduce a priv pointer into struct netlink_sock. Use it to store a per socket xarray that contains family->id indexed priv pointer storage. Note I used xarray instead of suggested linked list as it is more convenient, without need to have a container struct that would contain struct list_head item. Introduce genl_sk_priv_store() to store the priv pointer. Introduce genl_sk_priv_get() to obtain the priv pointer under RCU read lock. Assume that kfree() is good for free of privs for now, as the only user introduced by the follow-up patch (devlink) will use kzalloc() for the allocation of the memory of the stored pointer. If later on this needs to be made custom, a callback is going to be needed. Until then (if ever), do this in a simple way. Signed-off-by: Jiri Pirko --- v3->v4: - new patch --- include/net/genetlink.h | 3 ++ net/netlink/af_netlink.h | 1 + net/netlink/genetlink.c | 98 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) diff --git a/include/net/genetlink.h b/include/net/genetlink.h index e18a4c0d69ee..66c1e50415e0 100644 --- a/include/net/genetlink.h +++ b/include/net/genetlink.h @@ -300,6 +300,9 @@ int genl_register_family(struct genl_family *family); int genl_unregister_family(const struct genl_family *family); void genl_notify(const struct genl_family *family, struct sk_buff *skb, struct genl_info *info, u32 group, gfp_t flags); +void *genl_sk_priv_get(struct sock *sk, struct genl_family *family); +void *genl_sk_priv_store(struct sock *sk, struct genl_family *family, + void *priv); void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, const struct genl_family *family, int flags, u8 cmd); diff --git a/net/netlink/af_netlink.h b/net/netlink/af_netlink.h index 2145979b9986..5d96135a4cf3 100644 --- a/net/netlink/af_netlink.h +++ b/net/netlink/af_netlink.h @@ -51,6 +51,7 @@ struct netlink_sock { struct rhash_head node; struct rcu_head rcu; struct work_struct work; + void __rcu *priv; }; static inline struct netlink_sock *nlk_sk(struct sock *sk) diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c index 92ef5ed2e7b0..aae5e63fa50b 100644 --- a/net/netlink/genetlink.c +++ b/net/netlink/genetlink.c @@ -21,6 +21,7 @@ #include #include #include +#include "af_netlink.h" static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */ static DECLARE_RWSEM(cb_lock); @@ -1699,12 +1700,109 @@ static int genl_bind(struct net *net, int group) return ret; } +struct genl_sk_ctx { + struct xarray family_privs; +}; + +static struct genl_sk_ctx *genl_sk_ctx_alloc(void) +{ + struct genl_sk_ctx *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + xa_init_flags(&ctx->family_privs, XA_FLAGS_ALLOC); + return ctx; +} + +static void genl_sk_ctx_free(struct genl_sk_ctx *ctx) +{ + unsigned long family_id; + void *priv; + + xa_for_each(&ctx->family_privs, family_id, priv) { + xa_erase(&ctx->family_privs, family_id); + kfree(priv); + } + xa_destroy(&ctx->family_privs); + kfree(ctx); +} + +/** + * genl_sk_priv_get - Get per-socket private pointer for family + * + * @sk: socket + * @family: family + * + * Lookup a private pointer stored per-socket by a specified + * Generic netlink family. + * + * Caller should make sure this is called in RCU read locked section. + * + * Returns: valid pointer on success, otherwise NULL. + */ +void *genl_sk_priv_get(struct sock *sk, struct genl_family *family) +{ + struct genl_sk_ctx *ctx; + + ctx = rcu_dereference(nlk_sk(sk)->priv); + if (!ctx) + return NULL; + return xa_load(&ctx->family_privs, family->id); +} + +/** + * genl_sk_priv_store - Store per-socket private pointer for family + * + * @sk: socket + * @family: family + * @priv: private pointer + * + * Store a private pointer per-socket for a specified + * Generic netlink family. + * + * Caller has to make sure this is not called in parallel multiple times + * for the same sock and also in parallel to genl_release() for the same sock. + * + * Returns: previously stored private pointer for the family (could be NULL) + * on success, otherwise negative error value encoded by ERR_PTR(). + */ +void *genl_sk_priv_store(struct sock *sk, struct genl_family *family, + void *priv) +{ + struct genl_sk_ctx *ctx; + void *old_priv; + + ctx = rcu_dereference_raw(nlk_sk(sk)->priv); + if (!ctx) { + ctx = genl_sk_ctx_alloc(); + if (!ctx) + return ERR_PTR(-ENOMEM); + rcu_assign_pointer(nlk_sk(sk)->priv, ctx); + } + + old_priv = xa_store(&ctx->family_privs, family->id, priv, GFP_KERNEL); + if (xa_is_err(old_priv)) + return ERR_PTR(xa_err(old_priv)); + return old_priv; +} + +static void genl_release(struct sock *sk, unsigned long *groups) +{ + struct genl_sk_ctx *ctx; + + ctx = rcu_dereference_raw(nlk_sk(sk)->priv); + if (ctx) + genl_sk_ctx_free(ctx); +} + static int __net_init genl_pernet_init(struct net *net) { struct netlink_kernel_cfg cfg = { .input = genl_rcv, .flags = NL_CFG_F_NONROOT_RECV, .bind = genl_bind, + .release = genl_release, }; /* we'll bump the group number right afterwards */ From patchwork Thu Nov 23 18:15:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13466531 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="t1ry1g+U" Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24C0E1A5 for ; Thu, 23 Nov 2023 10:15:59 -0800 (PST) Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-548f6f3cdc9so1728572a12.2 for ; Thu, 23 Nov 2023 10:15:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1700763357; x=1701368157; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mUVpumJEMKlIV3v0TPxPq6FHGtEDKA9wZkXmeuh2z+w=; b=t1ry1g+U/jyyAtPfzvTVrjmnhkfh+4nQj/p43dWaVeZ19PbFb4WmHixgA83EDyywcj pdwGg0axA7WA6u8yXpksuqqu3qjpkP2aRqRyQlJAenyrNvMfCw4roqmzIVqz4n1u3tr6 yhoZaJy6o1RutWjwXB5CACIg0a8bDY1xmYZEb2oXVM0NQi8wBd7AWhmjkcMixc6E2M7y PZLX8GY6MDtq56iShtWUr9rq2Gqfq5D2oJCwmFQd0WqfZDy7dkHzQlQVAuM//aRkvE4z KIUkyLE+bncFfCP/ZmjmLnuXO/VIUiAB+fBhQsS5BsqUx04peqVlTGWaLSZUa4dOBK1W yhDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700763357; x=1701368157; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mUVpumJEMKlIV3v0TPxPq6FHGtEDKA9wZkXmeuh2z+w=; b=LqMffB0oE8B9GlnykrZ8rpAPOUYrrkKnUDGlIEd/k9m+mh8GWzI1wjVRg2guUGMhhY bnE7H2L0uWTEJniwSn09I8NbIpTKcNLpdO/TMwAXFzjQVrH7Pay5XYyLMivOw7Mq6rJG faBIRL62A5DAICNbtwWPBNjJQoU/DKv09eYqq4/2y7MrVxO/DpeWdRCrsGzMu6dODByE HNvi6/7Q/LAN3cjjvsm/3jWrNNd88Yie5OeYIoo1qvkKZQx/3oBmIs3AlOxbGXgVP0Vg /QPJlSjkemgtV5ZxKdy97imtFqPXqiRlOeIBFVleNu4jKEA7WgWzysPmLFNr7inGKy1w pb5Q== X-Gm-Message-State: AOJu0YwOR6WFy7SHI2sE4tsrd5lEpIuB+VERobtsyvLjGfbRB5PFDmlX ceBmj43RGuGjlWdh8Hg6FCTuEUAghsfsj4RqNHQ= X-Google-Smtp-Source: AGHT+IGmhlH922ntZry+TKbfC2tC9fNCjNrjru5g4Ja1R8pG6dUp0ZWq32RvD2Dki+h0aAhW320ciQ== X-Received: by 2002:a17:906:3053:b0:9f8:a07b:4adb with SMTP id d19-20020a170906305300b009f8a07b4adbmr89355ejd.18.1700763357648; Thu, 23 Nov 2023 10:15:57 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id rv4-20020a17090710c400b00a047ef6f2c6sm1072158ejb.135.2023.11.23.10.15.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:15:57 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org Subject: [patch net-next v4 6/9] netlink: introduce typedef for filter function Date: Thu, 23 Nov 2023 19:15:43 +0100 Message-ID: <20231123181546.521488-7-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231123181546.521488-1-jiri@resnulli.us> References: <20231123181546.521488-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko Make the code using filter function a bit nicer by consolidating the filter function arguments using typedef. Suggested-by: Andy Shevchenko Signed-off-by: Jiri Pirko --- v2->v3: - left the original .c and .h arg names and types inconsistencies for cn_netlink_send_mult() and netlink_broadcast_filtered() v1->v2: - new patch --- drivers/connector/connector.c | 5 ++--- include/linux/connector.h | 3 +-- include/linux/netlink.h | 6 ++++-- net/netlink/af_netlink.c | 3 +-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/drivers/connector/connector.c b/drivers/connector/connector.c index 7f7b94f616a6..4028e8eeba82 100644 --- a/drivers/connector/connector.c +++ b/drivers/connector/connector.c @@ -59,9 +59,8 @@ static int cn_already_initialized; * both, or if both are zero then the group is looked up and sent there. */ int cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 __group, - gfp_t gfp_mask, - int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data), - void *filter_data) + gfp_t gfp_mask, netlink_filter_fn filter, + void *filter_data) { struct cn_callback_entry *__cbq; unsigned int size; diff --git a/include/linux/connector.h b/include/linux/connector.h index cec2d99ae902..70bc1160f3d8 100644 --- a/include/linux/connector.h +++ b/include/linux/connector.h @@ -100,8 +100,7 @@ void cn_del_callback(const struct cb_id *id); */ int cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 group, gfp_t gfp_mask, - int (*filter)(struct sock *dsk, struct sk_buff *skb, - void *data), + netlink_filter_fn filter, void *filter_data); /** diff --git a/include/linux/netlink.h b/include/linux/netlink.h index abe91ed6b9aa..1a4445bf2ab9 100644 --- a/include/linux/netlink.h +++ b/include/linux/netlink.h @@ -228,10 +228,12 @@ bool netlink_strict_get_check(struct sk_buff *skb); int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock); int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid, __u32 group, gfp_t allocation); + +typedef int (*netlink_filter_fn)(struct sock *dsk, struct sk_buff *skb, void *data); + int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, __u32 portid, __u32 group, gfp_t allocation, - int (*filter)(struct sock *dsk, - struct sk_buff *skb, void *data), + netlink_filter_fn filter, void *filter_data); int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code); int netlink_register_notifier(struct notifier_block *nb); diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index 177126fb0484..4ed8ffd58ff3 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -1519,8 +1519,7 @@ static void do_one_broadcast(struct sock *sk, int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid, u32 group, gfp_t allocation, - int (*filter)(struct sock *dsk, - struct sk_buff *skb, void *data), + netlink_filter_fn filter, void *filter_data) { struct net *net = sock_net(ssk); From patchwork Thu Nov 23 18:15:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13466532 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="NDwvz2X/" Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6AD01B3 for ; Thu, 23 Nov 2023 10:16:00 -0800 (PST) Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-a002562bd8bso224929766b.0 for ; Thu, 23 Nov 2023 10:16:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1700763359; x=1701368159; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=peEH7/aKHtykJGwoCx00LRwUqswO9g4wbyqhvIC9pTM=; b=NDwvz2X//8EWJmLBiDyqtT8eFzSNGqdMkStCUeWFAoIXsJkM2uXxJkAIObBb2S9ChE lkgwLMnoyItmIML8cqGLE3ACvdvND3juzNnFM69CigBmy/O191X4bhERUCJag8mVrkCx n+a/b2GYS+h+0Tv4ZDx8u9XhY3RWUnszAMaolaaeZd22ZN/pnTqnzrYC9/aMB/jirSU7 HB3VhGOWvi4CX2ow/vmHmbVPW50Hm8on6UL1IejL4b0iZgJkEPC/hGfaksnR+SfkA8YY 88MTg2/U5HeIcDngh3ey3dUXwCyIVh7dpmQFx+UJB2yu4utx+mwY2v+MnM/T6bCknORk s4kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700763359; x=1701368159; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=peEH7/aKHtykJGwoCx00LRwUqswO9g4wbyqhvIC9pTM=; b=uFX4dUWcyRpj2699PGs/SaONVsdVpzrRPB0S/gJxIHko1WYs2W2cbWg8WCIbZwzym0 x46DGW4ZfpFdaNaQ6PXpFfnOK5BRMozXX+bT5cqJKEhq3AdI86zUa3nO7NRzU1a1h9M2 QQF2qaAy8FYQ2hR4rT3w1Q/DdhA6s81WshK271+1yOjYPv9EzQo0Yj+F8GfyFnSddBud fWxdKpE1dSX7p2x6E73IfKH+Gs2EF/YzQVRkv4aRL/fw+6Kk4B98DfbGVP2gDwNuqK47 BNh5MW2wplIPLprT36KlqnASN0CRtcjZ+s/70IZTQu3iUNI3uSSdqE/jBZ4HXGftnuq5 7Ynw== X-Gm-Message-State: AOJu0YwmQTeQ0Wzdfe+B6eStnEKuAVptILM9OUjVAYpt0afQ0HfLEbay dtWK5kayZLnqeK6n0SVwrGVmz3+kf0ByrzMiZmI= X-Google-Smtp-Source: AGHT+IHMVEgeV34xuxw7T4e/Bq3Tpvdu2TKeSex15aKE/PhzOFR6hHMPB6T2wMuDbtpjW+mFnUgOEQ== X-Received: by 2002:a17:906:ca49:b0:9b2:b37d:17ff with SMTP id jx9-20020a170906ca4900b009b2b37d17ffmr2737907ejb.19.1700763359161; Thu, 23 Nov 2023 10:15:59 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id mf12-20020a170906cb8c00b009a13fdc139fsm1064534ejb.183.2023.11.23.10.15.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:15:58 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org Subject: [patch net-next v4 7/9] genetlink: introduce helpers to do filtered multicast Date: Thu, 23 Nov 2023 19:15:44 +0100 Message-ID: <20231123181546.521488-8-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231123181546.521488-1-jiri@resnulli.us> References: <20231123181546.521488-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko Currently it is possible for netlink kernel user to pass custom filter function to broadcast send function netlink_broadcast_filtered(). However, this is not exposed to multicast send and to generic netlink users. Extend the api and introduce a netlink helper nlmsg_multicast_filtered() and a generic netlink helper genlmsg_multicast_netns_filtered() to allow generic netlink families to specify filter function while sending multicast messages. Signed-off-by: Jiri Pirko --- v1->v2: - used netlink_filter_fn introduce by the previous patch - added return comments to silence scripts/kernel-doc warnings --- include/net/genetlink.h | 35 +++++++++++++++++++++++++++++++---- include/net/netlink.h | 31 +++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/include/net/genetlink.h b/include/net/genetlink.h index 66c1e50415e0..f1173e9ea50b 100644 --- a/include/net/genetlink.h +++ b/include/net/genetlink.h @@ -438,6 +438,35 @@ static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); } +/** + * genlmsg_multicast_netns_filtered - multicast a netlink message + * to a specific netns with filter + * function + * @family: the generic netlink family + * @net: the net namespace + * @skb: netlink message as socket buffer + * @portid: own netlink portid to avoid sending to yourself + * @group: offset of multicast group in groups array + * @flags: allocation flags + * @filter: filter function + * @filter_data: filter function private data + * + * Return: 0 on success, negative error code for failure. + */ +static inline int +genlmsg_multicast_netns_filtered(const struct genl_family *family, + struct net *net, struct sk_buff *skb, + u32 portid, unsigned int group, gfp_t flags, + netlink_filter_fn filter, + void *filter_data) +{ + if (WARN_ON_ONCE(group >= family->n_mcgrps)) + return -EINVAL; + group = family->mcgrp_offset + group; + return nlmsg_multicast_filtered(net->genl_sock, skb, portid, group, + flags, filter, filter_data); +} + /** * genlmsg_multicast_netns - multicast a netlink message to a specific netns * @family: the generic netlink family @@ -451,10 +480,8 @@ static inline int genlmsg_multicast_netns(const struct genl_family *family, struct net *net, struct sk_buff *skb, u32 portid, unsigned int group, gfp_t flags) { - if (WARN_ON_ONCE(group >= family->n_mcgrps)) - return -EINVAL; - group = family->mcgrp_offset + group; - return nlmsg_multicast(net->genl_sock, skb, portid, group, flags); + return genlmsg_multicast_netns_filtered(family, net, skb, portid, + group, flags, NULL, NULL); } /** diff --git a/include/net/netlink.h b/include/net/netlink.h index 167b91348e57..2ba1438b7066 100644 --- a/include/net/netlink.h +++ b/include/net/netlink.h @@ -1087,27 +1087,50 @@ static inline void nlmsg_free(struct sk_buff *skb) } /** - * nlmsg_multicast - multicast a netlink message + * nlmsg_multicast_filtered - multicast a netlink message with filter function * @sk: netlink socket to spread messages to * @skb: netlink message as socket buffer * @portid: own netlink portid to avoid sending to yourself * @group: multicast group id * @flags: allocation flags + * @filter: filter function + * @filter_data: filter function private data + * + * Return: 0 on success, negative error code for failure. */ -static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, - u32 portid, unsigned int group, gfp_t flags) +static inline int nlmsg_multicast_filtered(struct sock *sk, struct sk_buff *skb, + u32 portid, unsigned int group, + gfp_t flags, + netlink_filter_fn filter, + void *filter_data) { int err; NETLINK_CB(skb).dst_group = group; - err = netlink_broadcast(sk, skb, portid, group, flags); + err = netlink_broadcast_filtered(sk, skb, portid, group, flags, + filter, filter_data); if (err > 0) err = 0; return err; } +/** + * nlmsg_multicast - multicast a netlink message + * @sk: netlink socket to spread messages to + * @skb: netlink message as socket buffer + * @portid: own netlink portid to avoid sending to yourself + * @group: multicast group id + * @flags: allocation flags + */ +static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, + u32 portid, unsigned int group, gfp_t flags) +{ + return nlmsg_multicast_filtered(sk, skb, portid, group, flags, + NULL, NULL); +} + /** * nlmsg_unicast - unicast a netlink message * @sk: netlink socket to spread message to From patchwork Thu Nov 23 18:15:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13466533 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="Bmr+1B1q" Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24221B9 for ; Thu, 23 Nov 2023 10:16:02 -0800 (PST) Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-543c3756521so1561873a12.2 for ; Thu, 23 Nov 2023 10:16:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1700763360; x=1701368160; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NODG5PlUPrTUD/QwLTfFhuuDAv4N66rE+qGJjhhGduM=; b=Bmr+1B1qpARCSluGWgU/HgnJCrujdqDpdCawr6EoWCTpvpMDKuDBLVbzpu0BT7tSVg 1OF1lD2G9Fte8jZEHYgmy2nPsDvUFyyt6M/MQcDOgvqgMqyBmdYESx5FQDxKfppYav2S jPDO6QF7a3RL8F+7i1uWppiSEvGkoVwQZrkRzgI4Lg/swIoueDSdXq+9+VarYeldvWGo IkksV/vSdOHqDoWTqcB6tOP1Es8sHMBr2hdKavcPK07e+RkDcmZG8zNFf4DTXTjUc/bj 2DYxZZHFqWmREJ6veJx1TCi3fMQIBlqqvawzafH95izUmeXitiCiQXnt26YVk+EX29Mz g0Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700763360; x=1701368160; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NODG5PlUPrTUD/QwLTfFhuuDAv4N66rE+qGJjhhGduM=; b=LoLkExS48zNRTaI7s7OaoolYAfpuyhjx6NJaw8xwIGsTI5DRUVXBnYodfPJRbeU56k Uo4jfppiWpGLTtjqReIzmmkLdT5NUoozqy2YehAAHZBsgI9AGblDVhw2xlftJTL0okRp MOQ89tpIP//Mc8HcP8gdzes7E0f3h3dRzv4GzcjTGbssiHnJG7sJTTFUP/h9i/hVQXtT tGa8x+UgSRha4GUPudz+WIPKfuGIsZyJYd40M8Nf77BwuZhRlmMMKJw9B9seIu/z7DPe xxEbGQD0ChD6kK2G/MjJTSSc1U29Vv7OsDnTV+9pZhN8M4LLqA0vhQ9KgGCD5VIKxi7Y U3iA== X-Gm-Message-State: AOJu0YzbnZNi/8b8lKnrG/Ea0rCryImd5EtE6GeMBJ12AnaoDGTrjSYH Xe/3eeGgjFXx8NvQae0pkcp2L082IA4SKCktAmU= X-Google-Smtp-Source: AGHT+IF7Vru7TCIJ1oIBGHosXm7Nk9rJMNazd0nzJxJtD8gZRVlfMAtpMIQBBu3C/NJGBZq1gLzRVA== X-Received: by 2002:a50:cd11:0:b0:548:657c:9110 with SMTP id z17-20020a50cd11000000b00548657c9110mr24698edi.38.1700763360636; Thu, 23 Nov 2023 10:16:00 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id f21-20020a056402069500b00548699fa102sm881897edy.83.2023.11.23.10.15.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:16:00 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org Subject: [patch net-next v4 8/9] devlink: add a command to set notification filter and use it for multicasts Date: Thu, 23 Nov 2023 19:15:45 +0100 Message-ID: <20231123181546.521488-9-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231123181546.521488-1-jiri@resnulli.us> References: <20231123181546.521488-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko Currently the user listening on a socket for devlink notifications gets always all messages for all existing instances, even if he is interested only in one of those. That may cause unnecessary overhead on setups with thousands of instances present. User is currently able to narrow down the devlink objects replies to dump commands by specifying select attributes. Allow similar approach for notifications. Introduce a new devlink NOTIFY_FILTER_SET which the user passes the select attributes. Store these per-socket and use them for filtering messages during multicast send. Signed-off-by: Jiri Pirko --- v3->v4: - rebased on top of genl_sk_priv_*() introduction --- Documentation/netlink/specs/devlink.yaml | 10 ++++ include/uapi/linux/devlink.h | 2 + net/devlink/devl_internal.h | 34 ++++++++++- net/devlink/netlink.c | 73 ++++++++++++++++++++++++ net/devlink/netlink_gen.c | 15 ++++- net/devlink/netlink_gen.h | 4 +- tools/net/ynl/generated/devlink-user.c | 31 ++++++++++ tools/net/ynl/generated/devlink-user.h | 47 +++++++++++++++ 8 files changed, 212 insertions(+), 4 deletions(-) diff --git a/Documentation/netlink/specs/devlink.yaml b/Documentation/netlink/specs/devlink.yaml index 43067e1f63aa..6bad1d3454b7 100644 --- a/Documentation/netlink/specs/devlink.yaml +++ b/Documentation/netlink/specs/devlink.yaml @@ -2055,3 +2055,13 @@ operations: - bus-name - dev-name - selftests + + - + name: notify-filter-set + doc: Set notification messages socket filter. + attribute-set: devlink + do: + request: + attributes: + - bus-name + - dev-name diff --git a/include/uapi/linux/devlink.h b/include/uapi/linux/devlink.h index b3c8383d342d..130cae0d3e20 100644 --- a/include/uapi/linux/devlink.h +++ b/include/uapi/linux/devlink.h @@ -139,6 +139,8 @@ enum devlink_command { DEVLINK_CMD_SELFTESTS_GET, /* can dump */ DEVLINK_CMD_SELFTESTS_RUN, + DEVLINK_CMD_NOTIFY_FILTER_SET, + /* add new commands above here */ __DEVLINK_CMD_MAX, DEVLINK_CMD_MAX = __DEVLINK_CMD_MAX - 1 diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 84dc9628d3f2..82e0fb3bbebf 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -191,11 +191,41 @@ static inline bool devlink_nl_notify_need(struct devlink *devlink) DEVLINK_MCGRP_CONFIG); } +struct devlink_obj_desc { + struct rcu_head rcu; + const char *bus_name; + const char *dev_name; + long data[]; +}; + +static inline void devlink_nl_obj_desc_init(struct devlink_obj_desc *desc, + struct devlink *devlink) +{ + memset(desc, 0, sizeof(*desc)); + desc->bus_name = devlink->dev->bus->name; + desc->dev_name = dev_name(devlink->dev); +} + +int devlink_nl_notify_filter(struct sock *dsk, struct sk_buff *skb, void *data); + +static inline void devlink_nl_notify_send_desc(struct devlink *devlink, + struct sk_buff *msg, + struct devlink_obj_desc *desc) +{ + genlmsg_multicast_netns_filtered(&devlink_nl_family, + devlink_net(devlink), + msg, 0, DEVLINK_MCGRP_CONFIG, + GFP_KERNEL, + devlink_nl_notify_filter, desc); +} + static inline void devlink_nl_notify_send(struct devlink *devlink, struct sk_buff *msg) { - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + struct devlink_obj_desc desc; + + devlink_nl_obj_desc_init(&desc, devlink); + devlink_nl_notify_send_desc(devlink, msg, &desc); } /* Notify */ diff --git a/net/devlink/netlink.c b/net/devlink/netlink.c index fa9afe3e6d9b..33a8e51dea68 100644 --- a/net/devlink/netlink.c +++ b/net/devlink/netlink.c @@ -17,6 +17,79 @@ static const struct genl_multicast_group devlink_nl_mcgrps[] = { [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME }, }; +int devlink_nl_notify_filter_set_doit(struct sk_buff *skb, + struct genl_info *info) +{ + struct nlattr **attrs = info->attrs; + struct devlink_obj_desc *flt; + size_t data_offset = 0; + size_t data_size = 0; + char *pos; + + if (attrs[DEVLINK_ATTR_BUS_NAME]) + data_size += nla_len(attrs[DEVLINK_ATTR_BUS_NAME]) + 1; + if (attrs[DEVLINK_ATTR_DEV_NAME]) + data_size += nla_len(attrs[DEVLINK_ATTR_DEV_NAME]) + 1; + + flt = kzalloc(sizeof(*flt) + data_size, GFP_KERNEL); + if (!flt) + return -ENOMEM; + + pos = (char *) flt->data; + if (attrs[DEVLINK_ATTR_BUS_NAME]) { + data_offset += nla_strscpy(pos, + attrs[DEVLINK_ATTR_BUS_NAME], + data_size) + 1; + flt->bus_name = pos; + pos += data_offset; + } + if (attrs[DEVLINK_ATTR_DEV_NAME]) { + nla_strscpy(pos, attrs[DEVLINK_ATTR_DEV_NAME], + data_size - data_offset); + flt->dev_name = pos; + } + + /* Don't attach empty filter. */ + if (!flt->bus_name && !flt->dev_name) { + kfree(flt); + flt = NULL; + } + + flt = genl_sk_priv_store(NETLINK_CB(skb).sk, &devlink_nl_family, flt); + if (IS_ERR(flt)) + return PTR_ERR(flt); + else if (flt) + kfree_rcu(flt, rcu); + + return 0; +} + +static bool devlink_obj_desc_match(const struct devlink_obj_desc *desc, + const struct devlink_obj_desc *flt) +{ + if (desc->bus_name && flt->bus_name && + strcmp(desc->bus_name, flt->bus_name)) + return false; + if (desc->dev_name && flt->dev_name && + strcmp(desc->dev_name, flt->dev_name)) + return false; + return true; +} + +int devlink_nl_notify_filter(struct sock *dsk, struct sk_buff *skb, void *data) +{ + struct devlink_obj_desc *desc = data; + struct devlink_obj_desc *flt; + int ret = 0; + + rcu_read_lock(); + flt = genl_sk_priv_get(dsk, &devlink_nl_family); + if (flt) + ret = !devlink_obj_desc_match(desc, flt); + rcu_read_unlock(); + return ret; +} + int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net, struct devlink *devlink, int attrtype) { diff --git a/net/devlink/netlink_gen.c b/net/devlink/netlink_gen.c index 95f9b4350ab7..1cb0e05305d2 100644 --- a/net/devlink/netlink_gen.c +++ b/net/devlink/netlink_gen.c @@ -560,8 +560,14 @@ static const struct nla_policy devlink_selftests_run_nl_policy[DEVLINK_ATTR_SELF [DEVLINK_ATTR_SELFTESTS] = NLA_POLICY_NESTED(devlink_dl_selftest_id_nl_policy), }; +/* DEVLINK_CMD_NOTIFY_FILTER_SET - do */ +static const struct nla_policy devlink_notify_filter_set_nl_policy[DEVLINK_ATTR_DEV_NAME + 1] = { + [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING, }, + [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING, }, +}; + /* Ops table for devlink */ -const struct genl_split_ops devlink_nl_ops[73] = { +const struct genl_split_ops devlink_nl_ops[74] = { { .cmd = DEVLINK_CMD_GET, .validate = GENL_DONT_VALIDATE_STRICT, @@ -1233,4 +1239,11 @@ const struct genl_split_ops devlink_nl_ops[73] = { .maxattr = DEVLINK_ATTR_SELFTESTS, .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO, }, + { + .cmd = DEVLINK_CMD_NOTIFY_FILTER_SET, + .doit = devlink_nl_notify_filter_set_doit, + .policy = devlink_notify_filter_set_nl_policy, + .maxattr = DEVLINK_ATTR_DEV_NAME, + .flags = GENL_CMD_CAP_DO, + }, }; diff --git a/net/devlink/netlink_gen.h b/net/devlink/netlink_gen.h index 02f3c0bfae0e..8f2bd50ddf5e 100644 --- a/net/devlink/netlink_gen.h +++ b/net/devlink/netlink_gen.h @@ -16,7 +16,7 @@ extern const struct nla_policy devlink_dl_port_function_nl_policy[DEVLINK_PORT_F extern const struct nla_policy devlink_dl_selftest_id_nl_policy[DEVLINK_ATTR_SELFTEST_ID_FLASH + 1]; /* Ops table for devlink */ -extern const struct genl_split_ops devlink_nl_ops[73]; +extern const struct genl_split_ops devlink_nl_ops[74]; int devlink_nl_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb, struct genl_info *info); @@ -142,5 +142,7 @@ int devlink_nl_selftests_get_doit(struct sk_buff *skb, struct genl_info *info); int devlink_nl_selftests_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb); int devlink_nl_selftests_run_doit(struct sk_buff *skb, struct genl_info *info); +int devlink_nl_notify_filter_set_doit(struct sk_buff *skb, + struct genl_info *info); #endif /* _LINUX_DEVLINK_GEN_H */ diff --git a/tools/net/ynl/generated/devlink-user.c b/tools/net/ynl/generated/devlink-user.c index bc5065bd99b2..cd5f70eadf5b 100644 --- a/tools/net/ynl/generated/devlink-user.c +++ b/tools/net/ynl/generated/devlink-user.c @@ -6830,6 +6830,37 @@ int devlink_selftests_run(struct ynl_sock *ys, return 0; } +/* ============== DEVLINK_CMD_NOTIFY_FILTER_SET ============== */ +/* DEVLINK_CMD_NOTIFY_FILTER_SET - do */ +void +devlink_notify_filter_set_req_free(struct devlink_notify_filter_set_req *req) +{ + free(req->bus_name); + free(req->dev_name); + free(req); +} + +int devlink_notify_filter_set(struct ynl_sock *ys, + struct devlink_notify_filter_set_req *req) +{ + struct nlmsghdr *nlh; + int err; + + nlh = ynl_gemsg_start_req(ys, ys->family_id, DEVLINK_CMD_NOTIFY_FILTER_SET, 1); + ys->req_policy = &devlink_nest; + + if (req->_present.bus_name_len) + mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, req->bus_name); + if (req->_present.dev_name_len) + mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, req->dev_name); + + err = ynl_exec(ys, nlh, NULL); + if (err < 0) + return -1; + + return 0; +} + const struct ynl_family ynl_devlink_family = { .name = "devlink", }; diff --git a/tools/net/ynl/generated/devlink-user.h b/tools/net/ynl/generated/devlink-user.h index 1db4edc36eaa..e5d79b824a67 100644 --- a/tools/net/ynl/generated/devlink-user.h +++ b/tools/net/ynl/generated/devlink-user.h @@ -5252,4 +5252,51 @@ devlink_selftests_run_req_set_selftests_flash(struct devlink_selftests_run_req * int devlink_selftests_run(struct ynl_sock *ys, struct devlink_selftests_run_req *req); +/* ============== DEVLINK_CMD_NOTIFY_FILTER_SET ============== */ +/* DEVLINK_CMD_NOTIFY_FILTER_SET - do */ +struct devlink_notify_filter_set_req { + struct { + __u32 bus_name_len; + __u32 dev_name_len; + } _present; + + char *bus_name; + char *dev_name; +}; + +static inline struct devlink_notify_filter_set_req * +devlink_notify_filter_set_req_alloc(void) +{ + return calloc(1, sizeof(struct devlink_notify_filter_set_req)); +} +void +devlink_notify_filter_set_req_free(struct devlink_notify_filter_set_req *req); + +static inline void +devlink_notify_filter_set_req_set_bus_name(struct devlink_notify_filter_set_req *req, + const char *bus_name) +{ + free(req->bus_name); + req->_present.bus_name_len = strlen(bus_name); + req->bus_name = malloc(req->_present.bus_name_len + 1); + memcpy(req->bus_name, bus_name, req->_present.bus_name_len); + req->bus_name[req->_present.bus_name_len] = 0; +} +static inline void +devlink_notify_filter_set_req_set_dev_name(struct devlink_notify_filter_set_req *req, + const char *dev_name) +{ + free(req->dev_name); + req->_present.dev_name_len = strlen(dev_name); + req->dev_name = malloc(req->_present.dev_name_len + 1); + memcpy(req->dev_name, dev_name, req->_present.dev_name_len); + req->dev_name[req->_present.dev_name_len] = 0; +} + +/* + * Set notification messages socket filter. + */ +int devlink_notify_filter_set(struct ynl_sock *ys, + struct devlink_notify_filter_set_req *req); + #endif /* _LINUX_DEVLINK_GEN_H */ From patchwork Thu Nov 23 18:15:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13466534 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="kOpQcz+W" Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1BFA189 for ; Thu, 23 Nov 2023 10:16:03 -0800 (PST) Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-a06e59384b6so111269966b.1 for ; Thu, 23 Nov 2023 10:16:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1700763362; x=1701368162; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BB+i87Epg6iQjagr/qaX8WtvurbfKry95yo/SWXW2MA=; b=kOpQcz+WHaRuvXgJmzapCeVeu3BX2wGqaWO1IzP0RATeN+JG/QBCzRHHjxR48hmzua JC7V6epEF48s3UIDssN81Au4RQfaMKF7KZqgky/4VnR8KSr0NdioeZMQlVgSFsmhRqMt 22BWl690W3qboUArWHLK3LkhZBxRv5pdup6L6obQxEbNKfY2nEnCaAZu6sqcjdcBa7k/ +B2PakFduIh1cwS87ZRRUFbszED4Nc8vLF3+HzrhaXliFJsAW7AfaANnboOcutV+Goyt e3nynEQztMmXzggp5FjmABrgAL0IH3r4pRKJdgfwXmAEhhPf/4Ao2E7sn6Lz9AHkpC2J UnYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700763362; x=1701368162; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BB+i87Epg6iQjagr/qaX8WtvurbfKry95yo/SWXW2MA=; b=V/XU4JP/zR4dLJgRg7wlQDC36giBpIxU7Hv2ntMd3N6+YdH3xrVEwNwKTZ/9pUfkMf iOyk7mwXGXszgNuFXxuDF8tanwAsqXo8EUPOtVxhtf0/BL+Tie4rCtZCU1LGPtSAyzLi psGbtwSXwZn2bjkScKValVgbXYzZRVgF7CiTOuAqb3LMz2F92P2C6JXr9QpOGHr0gRJN GLNERxM8QcCiIE69CGwntN9vvjeINCX0bxKLVZ7uXz+QViCMHebEP/J9XHxTAW72NxIX BtggLovji5n91bKBXH2YL9A6TqTq378XraCGBuxawzULHnrB9uvJND0DWAl71QibebGP SroQ== X-Gm-Message-State: AOJu0Yzk0B26LoMzOey7vMgEXpE4hpQPncoCo2SkZOJQHM4hOrOSjS9X vbaR4tEqBkbvnakYa11n1J+QkwUnMHerQmNiKww= X-Google-Smtp-Source: AGHT+IEh0b+/o+FW7TXGPWgTE+BnTfqQiG3UmKAe+ye7uySHSNTUiiq802AX4GpN/YatarEE0xLiAQ== X-Received: by 2002:a17:906:a202:b0:9ae:41db:c27f with SMTP id r2-20020a170906a20200b009ae41dbc27fmr96648ejy.10.1700763362115; Thu, 23 Nov 2023 10:16:02 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id n20-20020a170906119400b009fbdacf9363sm1063763eja.21.2023.11.23.10.16.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:16:01 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org Subject: [patch net-next v4 9/9] devlink: extend multicast filtering by port index Date: Thu, 23 Nov 2023 19:15:46 +0100 Message-ID: <20231123181546.521488-10-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231123181546.521488-1-jiri@resnulli.us> References: <20231123181546.521488-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko Expose the previously introduced notification multicast messages filtering infrastructure and allow the user to select messages using port index. Signed-off-by: Jiri Pirko --- v3->v4: - rebased on top of genl_sk_priv_*() introduction --- Documentation/netlink/specs/devlink.yaml | 1 + net/devlink/devl_internal.h | 9 +++++++++ net/devlink/health.c | 6 +++++- net/devlink/netlink.c | 10 +++++++++- net/devlink/netlink_gen.c | 5 +++-- net/devlink/port.c | 5 ++++- tools/net/ynl/generated/devlink-user.c | 2 ++ tools/net/ynl/generated/devlink-user.h | 9 +++++++++ 8 files changed, 42 insertions(+), 5 deletions(-) diff --git a/Documentation/netlink/specs/devlink.yaml b/Documentation/netlink/specs/devlink.yaml index 6bad1d3454b7..4996ff7e09b6 100644 --- a/Documentation/netlink/specs/devlink.yaml +++ b/Documentation/netlink/specs/devlink.yaml @@ -2065,3 +2065,4 @@ operations: attributes: - bus-name - dev-name + - port-index diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 82e0fb3bbebf..c7a8e13f917c 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -195,6 +195,8 @@ struct devlink_obj_desc { struct rcu_head rcu; const char *bus_name; const char *dev_name; + unsigned int port_index; + bool port_index_valid; long data[]; }; @@ -206,6 +208,13 @@ static inline void devlink_nl_obj_desc_init(struct devlink_obj_desc *desc, desc->dev_name = dev_name(devlink->dev); } +static inline void devlink_nl_obj_desc_port_set(struct devlink_obj_desc *desc, + struct devlink_port *devlink_port) +{ + desc->port_index = devlink_port->index; + desc->port_index_valid = true; +} + int devlink_nl_notify_filter(struct sock *dsk, struct sk_buff *skb, void *data); static inline void devlink_nl_notify_send_desc(struct devlink *devlink, diff --git a/net/devlink/health.c b/net/devlink/health.c index 1d59ec0202f6..acb8c0e174bb 100644 --- a/net/devlink/health.c +++ b/net/devlink/health.c @@ -490,6 +490,7 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter, enum devlink_command cmd) { struct devlink *devlink = reporter->devlink; + struct devlink_obj_desc desc; struct sk_buff *msg; int err; @@ -509,7 +510,10 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter, return; } - devlink_nl_notify_send(devlink, msg); + devlink_nl_obj_desc_init(&desc, devlink); + if (reporter->devlink_port) + devlink_nl_obj_desc_port_set(&desc, reporter->devlink_port); + devlink_nl_notify_send_desc(devlink, msg, &desc); } void diff --git a/net/devlink/netlink.c b/net/devlink/netlink.c index 33a8e51dea68..7d3635b30725 100644 --- a/net/devlink/netlink.c +++ b/net/devlink/netlink.c @@ -49,8 +49,13 @@ int devlink_nl_notify_filter_set_doit(struct sk_buff *skb, flt->dev_name = pos; } + if (attrs[DEVLINK_ATTR_PORT_INDEX]) { + flt->port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); + flt->port_index_valid = true; + } + /* Don't attach empty filter. */ - if (!flt->bus_name && !flt->dev_name) { + if (!flt->bus_name && !flt->dev_name && !flt->port_index_valid) { kfree(flt); flt = NULL; } @@ -73,6 +78,9 @@ static bool devlink_obj_desc_match(const struct devlink_obj_desc *desc, if (desc->dev_name && flt->dev_name && strcmp(desc->dev_name, flt->dev_name)) return false; + if (desc->port_index_valid && flt->port_index_valid && + desc->port_index != flt->port_index) + return false; return true; } diff --git a/net/devlink/netlink_gen.c b/net/devlink/netlink_gen.c index 1cb0e05305d2..c81cf2dd154f 100644 --- a/net/devlink/netlink_gen.c +++ b/net/devlink/netlink_gen.c @@ -561,9 +561,10 @@ static const struct nla_policy devlink_selftests_run_nl_policy[DEVLINK_ATTR_SELF }; /* DEVLINK_CMD_NOTIFY_FILTER_SET - do */ -static const struct nla_policy devlink_notify_filter_set_nl_policy[DEVLINK_ATTR_DEV_NAME + 1] = { +static const struct nla_policy devlink_notify_filter_set_nl_policy[DEVLINK_ATTR_PORT_INDEX + 1] = { [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING, }, [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING, }, + [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32, }, }; /* Ops table for devlink */ @@ -1243,7 +1244,7 @@ const struct genl_split_ops devlink_nl_ops[74] = { .cmd = DEVLINK_CMD_NOTIFY_FILTER_SET, .doit = devlink_nl_notify_filter_set_doit, .policy = devlink_notify_filter_set_nl_policy, - .maxattr = DEVLINK_ATTR_DEV_NAME, + .maxattr = DEVLINK_ATTR_PORT_INDEX, .flags = GENL_CMD_CAP_DO, }, }; diff --git a/net/devlink/port.c b/net/devlink/port.c index 758df3000a1b..62e54e152ecf 100644 --- a/net/devlink/port.c +++ b/net/devlink/port.c @@ -507,6 +507,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port, enum devlink_command cmd) { struct devlink *devlink = devlink_port->devlink; + struct devlink_obj_desc desc; struct sk_buff *msg; int err; @@ -525,7 +526,9 @@ static void devlink_port_notify(struct devlink_port *devlink_port, return; } - devlink_nl_notify_send(devlink, msg); + devlink_nl_obj_desc_init(&desc, devlink); + devlink_nl_obj_desc_port_set(&desc, devlink_port); + devlink_nl_notify_send_desc(devlink, msg, &desc); } static void devlink_ports_notify(struct devlink *devlink, diff --git a/tools/net/ynl/generated/devlink-user.c b/tools/net/ynl/generated/devlink-user.c index cd5f70eadf5b..86392da0b52c 100644 --- a/tools/net/ynl/generated/devlink-user.c +++ b/tools/net/ynl/generated/devlink-user.c @@ -6853,6 +6853,8 @@ int devlink_notify_filter_set(struct ynl_sock *ys, mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, req->bus_name); if (req->_present.dev_name_len) mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, req->dev_name); + if (req->_present.port_index) + mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_INDEX, req->port_index); err = ynl_exec(ys, nlh, NULL); if (err < 0) diff --git a/tools/net/ynl/generated/devlink-user.h b/tools/net/ynl/generated/devlink-user.h index e5d79b824a67..b96837663e6e 100644 --- a/tools/net/ynl/generated/devlink-user.h +++ b/tools/net/ynl/generated/devlink-user.h @@ -5258,10 +5258,12 @@ struct devlink_notify_filter_set_req { struct { __u32 bus_name_len; __u32 dev_name_len; + __u32 port_index:1; } _present; char *bus_name; char *dev_name; + __u32 port_index; }; static inline struct devlink_notify_filter_set_req * @@ -5292,6 +5294,13 @@ devlink_notify_filter_set_req_set_dev_name(struct devlink_notify_filter_set_req memcpy(req->dev_name, dev_name, req->_present.dev_name_len); req->dev_name[req->_present.dev_name_len] = 0; } +static inline void +devlink_notify_filter_set_req_set_port_index(struct devlink_notify_filter_set_req *req, + __u32 port_index) +{ + req->_present.port_index = 1; + req->port_index = port_index; +} /* * Set notification messages socket filter.