diff mbox series

[v9,03/25] net/ethtool: add ULP_DDP_{GET,SET} operations for caps and stats

Message ID 20230117153535.1945554-4-aaptel@nvidia.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series nvme-tcp receive offloads | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count fail Series longer than 15 patches (and no cover letter)
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1887 this patch: 1887
netdev/cc_maintainers warning 10 maintainers not CCed: mailhol.vincent@wanadoo.fr edumazet@google.com sudheer.mogilappagari@intel.com sbhatta@marvell.com linux@rempel-privat.de andrew@lunn.ch pabeni@redhat.com wangjie125@huawei.com lkp@intel.com piergiorgio.beruto@gmail.com
netdev/build_clang success Errors and warnings before: 580 this patch: 580
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1994 this patch: 1994
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 81 exceeds 80 columns WARNING: line length of 82 exceeds 80 columns WARNING: line length of 85 exceeds 80 columns WARNING: line length of 86 exceeds 80 columns WARNING: line length of 87 exceeds 80 columns WARNING: line length of 88 exceeds 80 columns WARNING: line length of 89 exceeds 80 columns WARNING: line length of 90 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns WARNING: line length of 92 exceeds 80 columns WARNING: line length of 95 exceeds 80 columns WARNING: line length of 96 exceeds 80 columns WARNING: line length of 97 exceeds 80 columns WARNING: line length of 98 exceeds 80 columns WARNING: line length of 99 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Aurelien Aptel Jan. 17, 2023, 3:35 p.m. UTC
This commit adds:

- 2 new netlink messages:
  * ULP_DDP_GET: returns a bitset of supported and active capabilities
  * ULP_DDP_SET: tries to activate requested bitset and returns results

- 2 new netdev ethtool_ops operations:
  * ethtool_ops->get_ulp_ddp_stats(): retrieve device statistics
  * ethtool_ops->set_ulp_ddp_capabilities(): try to apply
    capability changes

ULP DDP capabilities handling is similar to netdev features
handling.

If a ULP_DDP_GET message has requested statistics via the
ETHTOOL_FLAG_STATS header flag, then per-device statistics are
returned to userspace.

Similar to netdev features, ULP_DDP_GET capabilities and statistics
can be returned in a verbose (default) or compact form (if
ETHTOOL_FLAG_COMPACT_BITSET is set in header flags).

Verbose statistics are nested as follows:

    STATS (nest)
        COUNT (u32)
        MAP (nest)
            ITEM (nest)
                NAME (strz)
                VAL  (u64)
            ...

Compact statistics are nested as follows:

    STATS (nest)
        COUNT (u32)
        COMPACT_VALUES (array of u64)

Signed-off-by: Shai Malin <smalin@nvidia.com>
Signed-off-by: Aurelien Aptel <aaptel@nvidia.com>
---
 include/linux/ethtool.h              |   6 +
 include/uapi/linux/ethtool_netlink.h |  49 ++++
 net/ethtool/Makefile                 |   2 +-
 net/ethtool/netlink.c                |  17 ++
 net/ethtool/netlink.h                |   4 +
 net/ethtool/ulp_ddp.c                | 399 +++++++++++++++++++++++++++
 6 files changed, 476 insertions(+), 1 deletion(-)
 create mode 100644 net/ethtool/ulp_ddp.c

Comments

Jakub Kicinski Jan. 20, 2023, 2:41 a.m. UTC | #1
On Tue, 17 Jan 2023 17:35:13 +0200 Aurelien Aptel wrote:
> This commit adds:
> 
> - 2 new netlink messages:
>   * ULP_DDP_GET: returns a bitset of supported and active capabilities
>   * ULP_DDP_SET: tries to activate requested bitset and returns results
> 
> - 2 new netdev ethtool_ops operations:
>   * ethtool_ops->get_ulp_ddp_stats(): retrieve device statistics
>   * ethtool_ops->set_ulp_ddp_capabilities(): try to apply
>     capability changes
> 
> ULP DDP capabilities handling is similar to netdev features
> handling.
> 
> If a ULP_DDP_GET message has requested statistics via the
> ETHTOOL_FLAG_STATS header flag, then per-device statistics are

s/per-device// ?

> returned to userspace.
> 
> Similar to netdev features, ULP_DDP_GET capabilities and statistics
> can be returned in a verbose (default) or compact form (if
> ETHTOOL_FLAG_COMPACT_BITSET is set in header flags).
> 
> Verbose statistics are nested as follows:
> 
>     STATS (nest)
>         COUNT (u32)
>         MAP (nest)
>             ITEM (nest)
>                 NAME (strz)
>                 VAL  (u64)
>             ...
> Compact statistics are nested as follows:
> 
>     STATS (nest)
>         COUNT (u32)
>         COMPACT_VALUES (array of u64)

That's not how other per-cmd stats work, why are you inventing 
new ways..

> +	int	(*get_ulp_ddp_stats)(struct net_device *dev, struct ethtool_ulp_ddp_stats *stats);
> +	int	(*set_ulp_ddp_capabilities)(struct net_device *dev, unsigned long *bits);

Why are these two callbacks not in struct ulp_ddp_dev_ops?

Why does the ethtool API not expose limits?
Aurelien Aptel Jan. 23, 2023, 6:36 p.m. UTC | #2
Jakub Kicinski <kuba@kernel.org> writes:
>> If a ULP_DDP_GET message has requested statistics via the
>> ETHTOOL_FLAG_STATS header flag, then per-device statistics are
>
> s/per-device// ?

Will be fixed.

>> Compact statistics are nested as follows:
>>
>>     STATS (nest)
>>         COUNT (u32)
>>         COMPACT_VALUES (array of u64)
>
> That's not how other per-cmd stats work, why are you inventing
> new ways..

As we commented in patch 2, dynamic strings are used for ethtool
forward-compability (being able to list future stats, which we are
planning) without updating or recompiling.

>> +     int     (*get_ulp_ddp_stats)(struct net_device *dev, struct ethtool_ulp_ddp_stats *stats);
>> +     int     (*set_ulp_ddp_capabilities)(struct net_device *dev, unsigned long *bits);
>
> Why are these two callbacks not in struct ulp_ddp_dev_ops?

We were trying to implement these callbacks in alignment with the
existing ethtool commands, for this reason we implemented it in the
ethtool API.

> Why does the ethtool API not expose limits?

Originally, and before we started adding the netlink interface, we were
not planning to include the ability to modify the limits as part of this
series.  We do agree that it now makes sense, but we will add, some
limits reflect hardware limitations while other could be tweaked by
users.  Those limits will be per-device and per-protocol. We will
suggest how to design it.

Thanks
Jakub Kicinski Jan. 23, 2023, 10:38 p.m. UTC | #3
On Mon, 23 Jan 2023 20:36:21 +0200 Aurelien Aptel wrote:
> >> Compact statistics are nested as follows:
> >>
> >>     STATS (nest)
> >>         COUNT (u32)
> >>         COMPACT_VALUES (array of u64)  
> >
> > That's not how other per-cmd stats work, why are you inventing
> > new ways..  
> 
> As we commented in patch 2, dynamic strings are used for ethtool
> forward-compability (being able to list future stats, which we are
> planning) without updating or recompiling.

But this is not how they should be carried.

The string set is retrieved by a separate command, then you request
a string based on the attribute ID (global_stringset() + get_string() 
in ethtool CLI code).

That way long running code or code dumping muliple interfaces can load
strings once and dumps are kept smaller.

> >> +     int     (*get_ulp_ddp_stats)(struct net_device *dev, struct ethtool_ulp_ddp_stats *stats);
> >> +     int     (*set_ulp_ddp_capabilities)(struct net_device *dev, unsigned long *bits);  
> >
> > Why are these two callbacks not in struct ulp_ddp_dev_ops?  
> 
> We were trying to implement these callbacks in alignment with the
> existing ethtool commands, for this reason we implemented it in the
> ethtool API.

ethtool commands mostly talk to HW, note that the feature configuration
(ethtool -k/-K) does not use ethtool ops either.

> > Why does the ethtool API not expose limits?  
> 
> Originally, and before we started adding the netlink interface, we were
> not planning to include the ability to modify the limits as part of this
> series.  We do agree that it now makes sense, but we will add, some
> limits reflect hardware limitations while other could be tweaked by
> users.  Those limits will be per-device and per-protocol. We will
> suggest how to design it.

Alright, I was mostly curious, it's not a requirement for initial
support.
Aurelien Aptel Jan. 24, 2023, 12:07 p.m. UTC | #4
Jakub Kicinski <kuba@kernel.org> writes:
> But this is not how they should be carried.
>
> The string set is retrieved by a separate command, then you request
> a string based on the attribute ID (global_stringset() + get_string()
> in ethtool CLI code).
>
> That way long running code or code dumping muliple interfaces can load
> strings once and dumps are kept smaller.

As far as I understand, this is what our code is doing, it is aligned
with the feature bits implementation and its usage of bitsets.

Features use netlink bitsets which have a verbose (include literal
strings) and compact form (use stringset ID).

Similarly our stats have a verbose (literal strings) and compact
form (use implicit stringset ID).

In the compact form, since we always return the complete stats list the
string id is implicit: the first stat is string id 0, next one string id
1, and so on. We just return the complete stat array as a blob under
"COMPACT_VALUES".

In ethtool CLI we are using the compact form and calling
global_stringset() + get_string() as you suggested:

	stat_names = global_stringset(ETH_SS_ULP_DDP_STATS,
				      nlctx->ethnl2_socket);

Then later:

	for (i = 0; i < results.stat_count; i++) {
		const char *name = get_string(stat_names, i);
		printf("%s: %lu\n", name, results.stats[i]);
	}

See
https://github.com/aaptel/ethtool/blob/ulp-ddp-v9/netlink/ulp_ddp.c#L186-L189
https://github.com/aaptel/ethtool/blob/ulp-ddp-v9/netlink/ulp_ddp.c#L154-L157

Should we remove the verbose form?

> ethtool commands mostly talk to HW, note that the feature configuration
> (ethtool -k/-K) does not use ethtool ops either.

Ok, we will move all the ops to netdev->ulp_ddp_ops as suggested.

>> > Why does the ethtool API not expose limits?
>>
>> Originally, and before we started adding the netlink interface, we were
>> not planning to include the ability to modify the limits as part of this
>> series.  We do agree that it now makes sense, but we will add, some
>> limits reflect hardware limitations while other could be tweaked by
>> users.  Those limits will be per-device and per-protocol. We will
>> suggest how to design it.
>
> Alright, I was mostly curious, it's not a requirement for initial
> support.

Ok, thanks.
Jakub Kicinski Jan. 24, 2023, 7:55 p.m. UTC | #5
On Tue, 24 Jan 2023 14:07:12 +0200 Aurelien Aptel wrote:
> Jakub Kicinski <kuba@kernel.org> writes:
> > But this is not how they should be carried.
> >
> > The string set is retrieved by a separate command, then you request
> > a string based on the attribute ID (global_stringset() + get_string()
> > in ethtool CLI code).
> >
> > That way long running code or code dumping muliple interfaces can load
> > strings once and dumps are kept smaller.  
> 
> As far as I understand, this is what our code is doing, it is aligned
> with the feature bits implementation and its usage of bitsets.
> 
> Features use netlink bitsets which have a verbose (include literal
> strings) and compact form (use stringset ID).
> 
> Similarly our stats have a verbose (literal strings) and compact
> form (use implicit stringset ID).
> 
> In the compact form, since we always return the complete stats list the
> string id is implicit: the first stat is string id 0, next one string id
> 1, and so on. We just return the complete stat array as a blob under
> "COMPACT_VALUES".
> 
> In ethtool CLI we are using the compact form and calling
> global_stringset() + get_string() as you suggested:
> 
> 	stat_names = global_stringset(ETH_SS_ULP_DDP_STATS,
> 				      nlctx->ethnl2_socket);
> 
> Then later:
> 
> 	for (i = 0; i < results.stat_count; i++) {
> 		const char *name = get_string(stat_names, i);
> 		printf("%s: %lu\n", name, results.stats[i]);
> 	}
> 
> See
> https://github.com/aaptel/ethtool/blob/ulp-ddp-v9/netlink/ulp_ddp.c#L186-L189
> https://github.com/aaptel/ethtool/blob/ulp-ddp-v9/netlink/ulp_ddp.c#L154-L157
> 
> Should we remove the verbose form?

Yes, it just complicates the kernel code to no significant gain, IMO.
diff mbox series

Patch

diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h
index 1783a4402686..016a3a1cc3ff 100644
--- a/include/linux/ethtool.h
+++ b/include/linux/ethtool.h
@@ -692,6 +692,10 @@  enum {
  *	plugged-in.
  * @set_module_power_mode: Set the power mode policy for the plug-in module
  *	used by the network device.
+ * @get_ulp_ddp_stats: Query ULP DDP statistics. Return the number of
+ *	counters or -1 or error.
+ * @set_ulp_ddp_capabilities: Set device ULP DDP capabilities.
+ *	Returns a negative error code or zero.
  *
  * All operations are optional (i.e. the function pointer may be set
  * to %NULL) and callers must take this into account.  Callers must
@@ -830,6 +834,8 @@  struct ethtool_ops {
 	int	(*set_module_power_mode)(struct net_device *dev,
 					 const struct ethtool_module_power_mode_params *params,
 					 struct netlink_ext_ack *extack);
+	int	(*get_ulp_ddp_stats)(struct net_device *dev, struct ethtool_ulp_ddp_stats *stats);
+	int	(*set_ulp_ddp_capabilities)(struct net_device *dev, unsigned long *bits);
 };
 
 int ethtool_check_ops(const struct ethtool_ops *ops);
diff --git a/include/uapi/linux/ethtool_netlink.h b/include/uapi/linux/ethtool_netlink.h
index 83557cae0b87..3218bc1f081e 100644
--- a/include/uapi/linux/ethtool_netlink.h
+++ b/include/uapi/linux/ethtool_netlink.h
@@ -55,6 +55,8 @@  enum {
 	ETHTOOL_MSG_PLCA_GET_CFG,
 	ETHTOOL_MSG_PLCA_SET_CFG,
 	ETHTOOL_MSG_PLCA_GET_STATUS,
+	ETHTOOL_MSG_ULP_DDP_GET,
+	ETHTOOL_MSG_ULP_DDP_SET,
 
 	/* add new constants above here */
 	__ETHTOOL_MSG_USER_CNT,
@@ -105,6 +107,8 @@  enum {
 	ETHTOOL_MSG_PLCA_GET_CFG_REPLY,
 	ETHTOOL_MSG_PLCA_GET_STATUS_REPLY,
 	ETHTOOL_MSG_PLCA_NTF,
+	ETHTOOL_MSG_ULP_DDP_GET_REPLY,
+	ETHTOOL_MSG_ULP_DDP_SET_REPLY,
 
 	/* add new constants above here */
 	__ETHTOOL_MSG_KERNEL_CNT,
@@ -922,6 +926,51 @@  enum {
 	ETHTOOL_A_PLCA_MAX = (__ETHTOOL_A_PLCA_CNT - 1)
 };
 
+/* ULP DDP */
+
+enum {
+	ETHTOOL_A_ULP_DDP_UNSPEC,
+	ETHTOOL_A_ULP_DDP_HEADER,			/* nest - _A_HEADER_* */
+	ETHTOOL_A_ULP_DDP_HW,				/* bitset */
+	ETHTOOL_A_ULP_DDP_ACTIVE,			/* bitset */
+	ETHTOOL_A_ULP_DDP_WANTED,			/* bitset */
+	ETHTOOL_A_ULP_DDP_STATS,			/* nest - _A_ULP_DDP_STATS_* */
+
+	/* add new constants above here */
+	__ETHTOOL_A_ULP_DDP_CNT,
+	ETHTOOL_A_ULP_DDP_MAX = __ETHTOOL_A_ULP_DDP_CNT - 1
+};
+
+enum {
+	ETHTOOL_A_ULP_DDP_STATS_UNSPEC,
+	ETHTOOL_A_ULP_DDP_STATS_COUNT,			/* u32 */
+	ETHTOOL_A_ULP_DDP_STATS_COMPACT_VALUES,		/* array, u64 */
+	ETHTOOL_A_ULP_DDP_STATS_MAP,			/* nest - _A_ULP_DDP_STATS_MAP_* */
+
+	/* add new constants above here */
+	__ETHTOOL_A_ULP_DDP_STATS_CNT,
+	ETHTOOL_A_ULP_DDP_STATS_MAX = __ETHTOOL_A_ULP_DDP_STATS_CNT - 1
+};
+
+enum {
+	ETHTOOL_A_ULP_DDP_STATS_MAP_UNSPEC,
+	ETHTOOL_A_ULP_DDP_STATS_MAP_ITEM,		/* next - _A_ULP_DDP_STATS_MAP_ITEM_* */
+
+	/* add new constants above here */
+	__ETHTOOL_A_ULP_DDP_STATS_MAP_CNT,
+	ETHTOOL_A_ULP_DDP_STATS_MAP_MAX = __ETHTOOL_A_ULP_DDP_STATS_MAP_CNT - 1
+};
+
+enum {
+	ETHTOOL_A_ULP_DDP_STATS_MAP_ITEM_UNSPEC,
+	ETHTOOL_A_ULP_DDP_STATS_MAP_ITEM_NAME,		/* string */
+	ETHTOOL_A_ULP_DDP_STATS_MAP_ITEM_VAL,		/* u64 */
+
+	/* add new constants above here */
+	__ETHTOOL_A_ULP_DDP_STATS_MAP_ITEM_CNT,
+	ETHTOOL_A_ULP_DDP_STATS_MAP_ITEM_MAX = __ETHTOOL_A_ULP_DDP_STATS_MAP_ITEM_CNT - 1
+};
+
 /* generic netlink info */
 #define ETHTOOL_GENL_NAME "ethtool"
 #define ETHTOOL_GENL_VERSION 1
diff --git a/net/ethtool/Makefile b/net/ethtool/Makefile
index 563864c1bf5a..68a1114ec838 100644
--- a/net/ethtool/Makefile
+++ b/net/ethtool/Makefile
@@ -8,4 +8,4 @@  ethtool_nl-y	:= netlink.o bitset.o strset.o linkinfo.o linkmodes.o rss.o \
 		   linkstate.o debug.o wol.o features.o privflags.o rings.o \
 		   channels.o coalesce.o pause.o eee.o tsinfo.o cabletest.o \
 		   tunnels.o fec.o eeprom.o stats.o phc_vclocks.o module.o \
-		   pse-pd.o plca.o
+		   pse-pd.o plca.o ulp_ddp.o
diff --git a/net/ethtool/netlink.c b/net/ethtool/netlink.c
index 9f924875bba9..5cfdc989540c 100644
--- a/net/ethtool/netlink.c
+++ b/net/ethtool/netlink.c
@@ -290,6 +290,7 @@  ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
 	[ETHTOOL_MSG_RSS_GET]		= &ethnl_rss_request_ops,
 	[ETHTOOL_MSG_PLCA_GET_CFG]	= &ethnl_plca_cfg_request_ops,
 	[ETHTOOL_MSG_PLCA_GET_STATUS]	= &ethnl_plca_status_request_ops,
+	[ETHTOOL_MSG_ULP_DDP_GET]	= &ethnl_ulp_ddp_request_ops,
 };
 
 static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
@@ -1076,6 +1077,22 @@  static const struct genl_ops ethtool_genl_ops[] = {
 		.policy = ethnl_plca_get_status_policy,
 		.maxattr = ARRAY_SIZE(ethnl_plca_get_status_policy) - 1,
 	},
+	{
+		.cmd    = ETHTOOL_MSG_ULP_DDP_GET,
+		.doit   = ethnl_default_doit,
+		.start  = ethnl_default_start,
+		.dumpit = ethnl_default_dumpit,
+		.done   = ethnl_default_done,
+		.policy = ethnl_ulp_ddp_get_policy,
+		.maxattr = ARRAY_SIZE(ethnl_ulp_ddp_get_policy) - 1,
+	},
+	{
+		.cmd	= ETHTOOL_MSG_ULP_DDP_SET,
+		.flags	= GENL_UNS_ADMIN_PERM,
+		.doit	= ethnl_set_ulp_ddp,
+		.policy = ethnl_ulp_ddp_set_policy,
+		.maxattr = ARRAY_SIZE(ethnl_ulp_ddp_set_policy) - 1,
+	},
 };
 
 static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
diff --git a/net/ethtool/netlink.h b/net/ethtool/netlink.h
index f271266f6e28..6e7378c9f27f 100644
--- a/net/ethtool/netlink.h
+++ b/net/ethtool/netlink.h
@@ -349,6 +349,7 @@  extern const struct ethnl_request_ops ethnl_pse_request_ops;
 extern const struct ethnl_request_ops ethnl_rss_request_ops;
 extern const struct ethnl_request_ops ethnl_plca_cfg_request_ops;
 extern const struct ethnl_request_ops ethnl_plca_status_request_ops;
+extern const struct ethnl_request_ops ethnl_ulp_ddp_request_ops;
 
 extern const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_FLAGS + 1];
 extern const struct nla_policy ethnl_header_policy_stats[ETHTOOL_A_HEADER_FLAGS + 1];
@@ -393,6 +394,8 @@  extern const struct nla_policy ethnl_rss_get_policy[ETHTOOL_A_RSS_CONTEXT + 1];
 extern const struct nla_policy ethnl_plca_get_cfg_policy[ETHTOOL_A_PLCA_HEADER + 1];
 extern const struct nla_policy ethnl_plca_set_cfg_policy[ETHTOOL_A_PLCA_MAX + 1];
 extern const struct nla_policy ethnl_plca_get_status_policy[ETHTOOL_A_PLCA_HEADER + 1];
+extern const struct nla_policy ethnl_ulp_ddp_get_policy[ETHTOOL_A_ULP_DDP_HEADER + 1];
+extern const struct nla_policy ethnl_ulp_ddp_set_policy[ETHTOOL_A_ULP_DDP_WANTED + 1];
 
 int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info);
 int ethnl_set_linkmodes(struct sk_buff *skb, struct genl_info *info);
@@ -414,6 +417,7 @@  int ethnl_set_fec(struct sk_buff *skb, struct genl_info *info);
 int ethnl_set_module(struct sk_buff *skb, struct genl_info *info);
 int ethnl_set_pse(struct sk_buff *skb, struct genl_info *info);
 int ethnl_set_plca_cfg(struct sk_buff *skb, struct genl_info *info);
+int ethnl_set_ulp_ddp(struct sk_buff *skb, struct genl_info *info);
 
 extern const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN];
 extern const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN];
diff --git a/net/ethtool/ulp_ddp.c b/net/ethtool/ulp_ddp.c
new file mode 100644
index 000000000000..a6ef79ddcb39
--- /dev/null
+++ b/net/ethtool/ulp_ddp.c
@@ -0,0 +1,399 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ *
+ * ulp_ddp.c
+ *     Author: Aurelien Aptel <aaptel@nvidia.com>
+ *     Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES.  All rights reserved.
+ */
+
+#include "netlink.h"
+#include "common.h"
+#include "bitset.h"
+#include <net/ulp_ddp_caps.h>
+
+static struct ulp_ddp_netdev_caps *netdev_ulp_ddp_caps(struct net_device *dev)
+{
+#ifdef CONFIG_ULP_DDP
+	return &dev->ulp_ddp_caps;
+#else
+	return NULL;
+#endif
+}
+
+/* ULP_DDP_GET */
+
+struct ulp_ddp_req_info {
+	struct ethnl_req_info	base;
+};
+
+struct ulp_ddp_reply_data {
+	struct ethnl_reply_data	base;
+	DECLARE_BITMAP(hw, ULP_DDP_C_COUNT);
+	DECLARE_BITMAP(active, ULP_DDP_C_COUNT);
+	struct ethtool_ulp_ddp_stats stats;
+};
+
+#define ULP_DDP_REPDATA(__reply_base) \
+	container_of(__reply_base, struct ulp_ddp_reply_data, base)
+
+const struct nla_policy ethnl_ulp_ddp_get_policy[] = {
+	[ETHTOOL_A_ULP_DDP_HEADER]	=
+		NLA_POLICY_NESTED(ethnl_header_policy_stats),
+};
+
+/* When requested (ETHTOOL_FLAG_STATS) ULP DDP stats are appended to
+ * the response.
+ *
+ * Similar to bitsets, stats can be in a compact or verbose form.
+ *
+ * The verbose form is as follow:
+ *
+ * STATS (nest)
+ *     COUNT (u32)
+ *     MAP (nest)
+ *         ITEM (nest)
+ *             NAME (strz)
+ *             VAL  (u64)
+ *         ...
+ *
+ * The compact form is as follow:
+ *
+ * STATS (nest)
+ *     COUNT (u32)
+ *     COMPACT_VALUES (array of u64)
+ *
+ */
+static int ulp_ddp_stats64_size(const struct ethnl_req_info *req_base,
+				const struct ethnl_reply_data *reply_base,
+				ethnl_string_array_t names,
+				unsigned int count,
+				bool compact)
+{
+	unsigned int len = 0;
+	unsigned int i;
+
+	/* count */
+	len += nla_total_size(sizeof(u32));
+
+	if (compact) {
+		/* values */
+		len += nla_total_size(count * sizeof(u64));
+	} else {
+		unsigned int maplen = 0;
+
+		for (i = 0; i < count; i++) {
+			unsigned int itemlen = 0;
+
+			/* name */
+			itemlen += ethnl_strz_size(names[i]);
+			/* value */
+			itemlen += nla_total_size(sizeof(u64));
+
+			/* item nest */
+			maplen += nla_total_size(itemlen);
+		}
+
+		/* map nest */
+		len += nla_total_size(maplen);
+	}
+	/* outermost nest */
+	return nla_total_size(len);
+}
+
+static int ulp_ddp_put_stats64(struct sk_buff *skb, int attrtype, const u64 *val,
+			       unsigned int count, ethnl_string_array_t names, bool compact)
+{
+	struct nlattr *nest;
+	struct nlattr *attr;
+
+	nest = nla_nest_start(skb, attrtype);
+	if (!nest)
+		return -EMSGSIZE;
+
+	if (nla_put_u32(skb, ETHTOOL_A_ULP_DDP_STATS_COUNT, count))
+		goto nla_put_failure;
+	if (compact) {
+		unsigned int nbytes = count * sizeof(*val);
+		u64 *dst;
+
+		attr = nla_reserve(skb, ETHTOOL_A_ULP_DDP_STATS_COMPACT_VALUES, nbytes);
+		if (!attr)
+			goto nla_put_failure;
+		dst = nla_data(attr);
+		memcpy(dst, val, nbytes);
+	} else {
+		struct nlattr *map;
+		unsigned int i;
+
+		map = nla_nest_start(skb, ETHTOOL_A_ULP_DDP_STATS_MAP);
+		if (!map)
+			goto nla_put_failure;
+		for (i = 0; i < count; i++) {
+			attr = nla_nest_start(skb, ETHTOOL_A_ULP_DDP_STATS_MAP_ITEM);
+			if (!attr)
+				goto nla_put_failure;
+			if (ethnl_put_strz(skb, ETHTOOL_A_ULP_DDP_STATS_MAP_ITEM_NAME, names[i]))
+				goto nla_put_failure;
+			if (nla_put_u64_64bit(skb, ETHTOOL_A_ULP_DDP_STATS_MAP_ITEM_VAL,
+					      val[i], -1))
+				goto nla_put_failure;
+			nla_nest_end(skb, attr);
+		}
+		nla_nest_end(skb, map);
+	}
+	nla_nest_end(skb, nest);
+	return 0;
+
+nla_put_failure:
+	nla_nest_cancel(skb, nest);
+	return -EMSGSIZE;
+}
+
+static int ulp_ddp_prepare_data(const struct ethnl_req_info *req_base,
+				struct ethnl_reply_data *reply_base,
+				struct genl_info *info)
+{
+	struct ulp_ddp_reply_data *data = ULP_DDP_REPDATA(reply_base);
+	const struct ethtool_ops *ops = reply_base->dev->ethtool_ops;
+	struct net_device *dev = reply_base->dev;
+	struct ulp_ddp_netdev_caps *caps;
+
+	caps = netdev_ulp_ddp_caps(dev);
+	if (!caps)
+		return -EOPNOTSUPP;
+
+	bitmap_copy(data->hw, caps->hw, ULP_DDP_C_COUNT);
+	bitmap_copy(data->active, caps->active, ULP_DDP_C_COUNT);
+
+	if (req_base->flags & ETHTOOL_FLAG_STATS) {
+		if (!ops->get_ulp_ddp_stats)
+			return -EOPNOTSUPP;
+		ops->get_ulp_ddp_stats(dev, &data->stats);
+	}
+	return 0;
+}
+
+static int ulp_ddp_reply_size(const struct ethnl_req_info *req_base,
+			      const struct ethnl_reply_data *reply_base)
+{
+	const struct ulp_ddp_reply_data *data = ULP_DDP_REPDATA(reply_base);
+	bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
+	unsigned int len = 0;
+	int ret;
+
+	ret = ethnl_bitset_size(data->hw, NULL, ULP_DDP_C_COUNT,
+				ulp_ddp_caps_names, compact);
+	if (ret < 0)
+		return ret;
+	len += ret;
+	ret = ethnl_bitset_size(data->active, NULL, ULP_DDP_C_COUNT,
+				ulp_ddp_caps_names, compact);
+	if (ret < 0)
+		return ret;
+	len += ret;
+
+	if (req_base->flags & ETHTOOL_FLAG_STATS) {
+		ret = ulp_ddp_stats64_size(req_base, reply_base,
+					   ulp_ddp_stats_names, __ETH_ULP_DDP_STATS_CNT,
+					   compact);
+		if (ret < 0)
+			return ret;
+		len += ret;
+	}
+	return len;
+}
+
+static int ulp_ddp_fill_reply(struct sk_buff *skb,
+			      const struct ethnl_req_info *req_base,
+			      const struct ethnl_reply_data *reply_base)
+{
+	const struct ulp_ddp_reply_data *data = ULP_DDP_REPDATA(reply_base);
+	bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
+	int ret;
+
+	ret = ethnl_put_bitset(skb, ETHTOOL_A_ULP_DDP_HW, data->hw,
+			       NULL, ULP_DDP_C_COUNT,
+			       ulp_ddp_caps_names, compact);
+	if (ret < 0)
+		return ret;
+
+	ret = ethnl_put_bitset(skb, ETHTOOL_A_ULP_DDP_ACTIVE, data->active,
+			       NULL, ULP_DDP_C_COUNT,
+			       ulp_ddp_caps_names, compact);
+	if (ret < 0)
+		return ret;
+
+	if (req_base->flags & ETHTOOL_FLAG_STATS) {
+		ret = ulp_ddp_put_stats64(skb, ETHTOOL_A_ULP_DDP_STATS,
+					  (u64 *)&data->stats,
+					  __ETH_ULP_DDP_STATS_CNT,
+					  ulp_ddp_stats_names,
+					  compact);
+		if (ret < 0)
+			return ret;
+	}
+	return ret;
+}
+
+const struct ethnl_request_ops ethnl_ulp_ddp_request_ops = {
+	.request_cmd		= ETHTOOL_MSG_ULP_DDP_GET,
+	.reply_cmd		= ETHTOOL_MSG_ULP_DDP_GET_REPLY,
+	.hdr_attr		= ETHTOOL_A_ULP_DDP_HEADER,
+	.req_info_size		= sizeof(struct ulp_ddp_req_info),
+	.reply_data_size	= sizeof(struct ulp_ddp_reply_data),
+
+	.prepare_data		= ulp_ddp_prepare_data,
+	.reply_size		= ulp_ddp_reply_size,
+	.fill_reply		= ulp_ddp_fill_reply,
+};
+
+/* ULP_DDP_SET */
+
+const struct nla_policy ethnl_ulp_ddp_set_policy[] = {
+	[ETHTOOL_A_ULP_DDP_HEADER]	=
+		NLA_POLICY_NESTED(ethnl_header_policy),
+	[ETHTOOL_A_ULP_DDP_WANTED]	= { .type = NLA_NESTED },
+};
+
+static int ulp_ddp_send_reply(struct net_device *dev, struct genl_info *info,
+			      const unsigned long *wanted,
+			      const unsigned long *wanted_mask,
+			      const unsigned long *active,
+			      const unsigned long *active_mask, bool compact)
+{
+	struct sk_buff *rskb;
+	void *reply_payload;
+	int reply_len = 0;
+	int ret;
+
+	reply_len = ethnl_reply_header_size();
+	ret = ethnl_bitset_size(wanted, wanted_mask, ULP_DDP_C_COUNT,
+				ulp_ddp_caps_names, compact);
+	if (ret < 0)
+		goto err;
+	reply_len += ret;
+	ret = ethnl_bitset_size(active, active_mask, ULP_DDP_C_COUNT,
+				ulp_ddp_caps_names, compact);
+	if (ret < 0)
+		goto err;
+	reply_len += ret;
+
+	ret = -ENOMEM;
+	rskb = ethnl_reply_init(reply_len, dev, ETHTOOL_MSG_ULP_DDP_SET_REPLY,
+				ETHTOOL_A_ULP_DDP_HEADER, info,
+				&reply_payload);
+	if (!rskb)
+		goto err;
+
+	ret = ethnl_put_bitset(rskb, ETHTOOL_A_ULP_DDP_WANTED, wanted,
+			       wanted_mask, ULP_DDP_C_COUNT,
+			       ulp_ddp_caps_names, compact);
+	if (ret < 0)
+		goto nla_put_failure;
+	ret = ethnl_put_bitset(rskb, ETHTOOL_A_ULP_DDP_ACTIVE, active,
+			       active_mask, ULP_DDP_C_COUNT,
+			       ulp_ddp_caps_names, compact);
+	if (ret < 0)
+		goto nla_put_failure;
+
+	genlmsg_end(rskb, reply_payload);
+	ret = genlmsg_reply(rskb, info);
+	return ret;
+
+nla_put_failure:
+	nlmsg_free(rskb);
+	WARN_ONCE(1, "calculated message payload length (%d) not sufficient\n",
+		  reply_len);
+err:
+	GENL_SET_ERR_MSG(info, "failed to send reply message");
+	return ret;
+}
+
+int ethnl_set_ulp_ddp(struct sk_buff *skb, struct genl_info *info)
+{
+	DECLARE_BITMAP(old_active, ULP_DDP_C_COUNT);
+	DECLARE_BITMAP(new_active, ULP_DDP_C_COUNT);
+	DECLARE_BITMAP(req_wanted, ULP_DDP_C_COUNT);
+	DECLARE_BITMAP(req_mask, ULP_DDP_C_COUNT);
+	DECLARE_BITMAP(all_bits, ULP_DDP_C_COUNT);
+	DECLARE_BITMAP(tmp, ULP_DDP_C_COUNT);
+	struct ethnl_req_info req_info = {};
+	struct nlattr **tb = info->attrs;
+	struct ulp_ddp_netdev_caps *caps;
+	struct net_device *dev;
+	int ret;
+
+	if (!tb[ETHTOOL_A_ULP_DDP_WANTED])
+		return -EINVAL;
+	ret = ethnl_parse_header_dev_get(&req_info,
+					 tb[ETHTOOL_A_ULP_DDP_HEADER],
+					 genl_info_net(info), info->extack,
+					 true);
+	if (ret < 0)
+		return ret;
+
+	dev = req_info.dev;
+	rtnl_lock();
+	caps = netdev_ulp_ddp_caps(dev);
+	if (!caps) {
+		ret = -EOPNOTSUPP;
+		goto out_rtnl;
+	}
+
+	ret = ethnl_parse_bitset(req_wanted, req_mask, ULP_DDP_C_COUNT,
+				 tb[ETHTOOL_A_ULP_DDP_WANTED],
+				 ulp_ddp_caps_names, info->extack);
+	if (ret < 0)
+		goto out_rtnl;
+
+	/* if (req_mask & ~all_bits) */
+	bitmap_fill(all_bits, ULP_DDP_C_COUNT);
+	bitmap_andnot(tmp, req_mask, all_bits, ULP_DDP_C_COUNT);
+	if (!bitmap_empty(tmp, ULP_DDP_C_COUNT)) {
+		ret = -EINVAL;
+		goto out_rtnl;
+	}
+
+	/* new_active = (old_active & ~req_mask) | (wanted & req_mask)
+	 * new_active &= caps_hw
+	 */
+	bitmap_copy(old_active, caps->active, ULP_DDP_C_COUNT);
+	bitmap_and(req_wanted, req_wanted, req_mask, ULP_DDP_C_COUNT);
+	bitmap_andnot(new_active, old_active, req_mask, ULP_DDP_C_COUNT);
+	bitmap_or(new_active, new_active, req_wanted, ULP_DDP_C_COUNT);
+	bitmap_and(new_active, new_active, caps->hw, ULP_DDP_C_COUNT);
+	if (!bitmap_equal(old_active, new_active, ULP_DDP_C_COUNT)) {
+		ret = dev->ethtool_ops->set_ulp_ddp_capabilities(dev, new_active);
+		if (ret)
+			netdev_err(dev, "set_ulp_ddp_capabilities() returned error %d\n", ret);
+		bitmap_copy(new_active, caps->active, ULP_DDP_C_COUNT);
+	}
+
+	ret = 0;
+	if (!(req_info.flags & ETHTOOL_FLAG_OMIT_REPLY)) {
+		DECLARE_BITMAP(wanted_diff_mask, ULP_DDP_C_COUNT);
+		DECLARE_BITMAP(active_diff_mask, ULP_DDP_C_COUNT);
+		bool compact = req_info.flags & ETHTOOL_FLAG_COMPACT_BITSETS;
+
+		/* wanted_diff_mask = req_wanted ^ new_active
+		 * active_diff_mask = old_active ^ new_active -> mask of bits that have changed
+		 * wanted_diff_mask &= req_mask    -> mask of bits that have diff value than wanted
+		 * req_wanted &= wanted_diff_mask  -> bits that have diff value than wanted
+		 * new_active &= active_diff_mask  -> bits that have changed
+		 */
+		bitmap_xor(wanted_diff_mask, req_wanted, new_active, ULP_DDP_C_COUNT);
+		bitmap_xor(active_diff_mask, old_active, new_active, ULP_DDP_C_COUNT);
+		bitmap_and(wanted_diff_mask, wanted_diff_mask, req_mask, ULP_DDP_C_COUNT);
+		bitmap_and(req_wanted, req_wanted, wanted_diff_mask,  ULP_DDP_C_COUNT);
+		bitmap_and(new_active, new_active, active_diff_mask,  ULP_DDP_C_COUNT);
+		ret = ulp_ddp_send_reply(dev, info,
+					 req_wanted, wanted_diff_mask,
+					 new_active, active_diff_mask,
+					 compact);
+	}
+
+out_rtnl:
+	rtnl_unlock();
+	ethnl_parse_header_dev_put(&req_info);
+	return ret;
+}