pause frame stats
Subsystems (2)
Jakub Kicinski
maintainerv3 cover pause frame stats
Hi!
Sorry about the delay from v2.
This set adds support for pause frame statistics.
First pause frame info is extended to support --json.
Pause stats are first of this kind of statistics so add support for a new flag (--include-statistics).
Next add support for dumping policy to check if the statistics flag is supported for a given subcommand.
Last but not least - display statistics.
v3:
- rename the ctx variable to policy_ctx
- instead of returning the flags policy save it to a member of struct nl_context, for potential reuse. Also we don't have to worry about returning flags and negative errors from the read helper this way :)
Jakub Kicinski (7):
update UAPI header copies
pause: add --json support
separate FLAGS out in -h
add support for stats in subcommands
netlink: prepare for more per-op info
netlink: use policy dumping to check if stats flag is supported
pause: add support for dumping statistics
ethtool.8.in | 7 ++ ethtool.c | 17 +++- internal.h | 1 + netlink/coalesce.c | 6 +- netlink/msgbuff.h | 6 ++ netlink/netlink.c | 179 ++++++++++++++++++++++++++++++++++++++--- netlink/netlink.h | 31 +++++-- netlink/pause.c | 111 ++++++++++++++++++++++--- uapi/linux/genetlink.h | 11 +++ uapi/linux/netlink.h | 4 + 10 files changed, 336 insertions(+), 37 deletions(-)
Signature · 1 line
2.26.2
Jakub Kicinski
maintainerPATCH v3 1/7 update UAPI header copies
Update to kernel commit 9453b2d4694c.
--- uapi/linux/genetlink.h | 11 +++++++++++ uapi/linux/netlink.h | 4 ++++ 2 files changed, 15 insertions(+)
uapi/linux/genetlink.h +11 -0
@@ -64,6 +64,8 @@ enum {CTRL_ATTR_OPS ,CTRL_ATTR_MCAST_GROUPS ,CTRL_ATTR_POLICY , +CTRL_ATTR_OP_POLICY , +CTRL_ATTR_OP , __CTRL_ATTR_MAX, };
@@ -85,6 +87,15 @@ enum { __CTRL_ATTR_MCAST_GRP_MAX, }; +enum { +CTRL_ATTR_POLICY_UNSPEC , +CTRL_ATTR_POLICY_DO , +CTRL_ATTR_POLICY_DUMP , + + __CTRL_ATTR_POLICY_DUMP_MAX, +CTRL_ATTR_POLICY_DUMP_MAX = __CTRL_ATTR_POLICY_DUMP_MAX -1 +}; +#define CTRL_ATTR_MCAST_GRP_MAX (__CTRL_ATTR_MCAST_GRP_MAX - 1)
uapi/linux/netlink.h +4 -0
@@ -129,6 +129,7 @@ struct nlmsgerr {* @NLMSGERR_ATTR_COOKIE: arbitrary subsystem specific cookie to * be used - in the success case - to identify a created * object or operation or similar (binary) +* @NLMSGERR_ATTR_POLICY: policy for a rejected attribute * @__NLMSGERR_ATTR_MAX: number of attributes * @NLMSGERR_ATTR_MAX: highest attribute number */
@@ -137,6 +138,7 @@ enum nlmsgerr_attrs {NLMSGERR_ATTR_MSG ,NLMSGERR_ATTR_OFFS ,NLMSGERR_ATTR_COOKIE , +NLMSGERR_ATTR_POLICY , __NLMSGERR_ATTR_MAX,NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX -1
@@ -327,6 +329,7 @@ enum netlink_attribute_type {* the index, if limited inside the nesting (U32) * @NL_POLICY_TYPE_ATTR_BITFIELD32_MASK: valid mask for the * bitfield32 type (U32) +* @NL_POLICY_TYPE_ATTR_MASK: mask of valid bits for unsigned integers (U64) * @NL_POLICY_TYPE_ATTR_PAD: pad attribute for 64-bit alignment */ enum netlink_policy_type_attr {
@@ -342,6 +345,7 @@ enum netlink_policy_type_attr {NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE ,NL_POLICY_TYPE_ATTR_BITFIELD32_MASK ,NL_POLICY_TYPE_ATTR_PAD , +NL_POLICY_TYPE_ATTR_MASK ,/* keep last */ __NL_POLICY_TYPE_ATTR_MAX,
Signature · 1 line
2.26.2
Jakub Kicinski
maintainerPATCH v3 2/7 pause: add --json support
In reply to Jakub KicinskiNo change in normal text output:
# ./ethtool -a eth0 Pause parameters for eth0: Autonegotiate: on RX: on TX: on RX negotiated: on TX negotiated: on
JSON:
# ./ethtool --json -a eth0
[ {
"ifname": "eth0",
"autonegotiate": true,
"rx": true,
"tx": true,
"negotiated": {
"rx": true,
"tx": true
}
} ]v2:
- restructure show_bool() so we can use its logic for show_bool_val()
--- netlink/coalesce.c | 6 +++--- netlink/netlink.h | 21 ++++++++++++++++----- netlink/pause.c | 44 ++++++++++++++++++++++++++++++++------------ 3 files changed, 51 insertions(+), 20 deletions(-)
netlink/coalesce.c +3 -3
@@ -36,9 +36,9 @@ int coalesce_reply_cb(const struct nlmsghdr *nlhdr, void *data)if (silent)putchar ('\n' );printf ("Coalesce parameters for %s:\n" , nlctx->devname); -printf ("Adaptive RX: %s TX: %s\n" , -u8_to_bool (tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX ]), -u8_to_bool (tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX ])); +show_bool ("rx" ,"Adaptive RX: %s " , + tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX ]); +show_bool ("tx" ,"TX: %s\n" , tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX ]);show_u32 (tb[ETHTOOL_A_COALESCE_STATS_BLOCK_USECS ],"stats-block-usecs: " ); show_u32(tb[ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL],
netlink/netlink.h +16 -5
@@ -98,17 +98,28 @@ static inline void show_u32(const struct nlattr *attr, const char *label)printf ("%sn/a\n" , label); } -static inline const char *u8_to_bool (const struct nlattr *attr) +static inline const char *u8_to_bool (const uint8_t *val) { -if (attr) -return mnl_attr_get_u8 (attr) ?"on" :"off" ; +if (val) +return *val ?"on" :"off" ;else return "n/a" ; } -static inline void show_bool (const struct nlattr *attr,const char *label) +static inline void show_bool_val (const char *key,const char *fmt,uint8_t *val) +{ +if (is_json_context ()) { +if (val) +print_bool (PRINT_JSON , key,NULL , val); + }else { +print_string (PRINT_FP ,NULL , fmt,u8_to_bool (val)); + } +} + +static inline void show_bool (const char *key,const char *fmt, +const struct nlattr *attr) { -printf ("%s%s\n" , label,u8_to_bool (attr)); +show_bool_val (key, fmt, attr ?mnl_attr_get_payload (attr) :NULL ); }/* misc */
netlink/pause.c +32 -12
76 lines not on this page · raw message
Signature · 1 line
2.26.2
Jakub Kicinski
maintainerPATCH v3 3/7 separate FLAGS out in -h
In reply to Jakub KicinskiHelp output is quite crowded already with every command being prefixed by --debug and --json options, and we're about to add a third one.
Add an indirection.
--- ethtool.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
ethtool.c +6 -3
@@ -6015,10 +6015,10 @@ static int show_usage(struct cmd_context *ctx __maybe_unused)fprintf (stdout,PACKAGE " version " VERSION "\n" );fprintf (stdout,"Usage:\n" -" ethtool [ --debug MASK ][ --json ] DEVNAME\t" +" ethtool [ FLAGS ] DEVNAME\t" "Display standard information about device\n" );for (i =0 ; args[i].opts; i++) { -fputs (" ethtool [ --debug MASK ][ --json ] " , stdout); +fputs (" ethtool [ FLAGS ] " , stdout); fprintf(stdout, "%s %s\t%s\n", args[i].opts, args[i].no_dev ? "\t" : "DEVNAME",
@@ -6027,7 +6027,10 @@ static int show_usage(struct cmd_context *ctx __maybe_unused)fputs (args[i].xhelp, stdout); }nl_monitor_usage (); -fprintf (stdout,"Not all options support JSON output\n" ); +fprintf (stdout,"\n" ); +fprintf (stdout,"FLAGS:\n" ); +fprintf (stdout," --debug MASK turn on debugging messages\n" ); +fprintf (stdout," --json enable JSON output format (not supported by all commands)\n" );return 0 ; }
Signature · 1 line
2.26.2
Jakub Kicinski
maintainerPATCH v3 4/7 add support for stats in subcommands
In reply to Jakub KicinskiAdd new parameters (-I | --include-statistics) which will control requesting statistic dumps from the kernel.
--- ethtool.8.in | 7 +++++++ ethtool.c | 8 ++++++++ internal.h | 1 + 3 files changed, 16 insertions(+)
ethtool.8.in +7 -0
@@ -140,6 +140,9 @@ ethtool \- query or control network driver and hardware settings .B ethtool [--json] .I args .HP +.B ethtool [-I | --include-statistics] +.I args +.HP .B ethtool \-\-monitor [ .I command
@@ -499,6 +502,10 @@ Output results in JavaScript Object Notation (JSON). Only a subset of options support this. Those which do not will continue to output plain text in the presence of this option. .TP +.B \-I \-\-include\-statistics +Include command-related statistics in the output. This option allows +displaying relevant device statistics for selected get commands. +.TP .B \-a \-\-show\-pause Queries the specified Ethernet device for pause parameter information. .TP
ethtool.c +8 -0
@@ -6031,6 +6031,7 @@ static int show_usage(struct cmd_context *ctx __maybe_unused)fprintf (stdout,"FLAGS:\n" );fprintf (stdout," --debug MASK turn on debugging messages\n" );fprintf (stdout," --json enable JSON output format (not supported by all commands)\n" ); +fprintf (stdout," -I|--include-statistics request device statistics related to the command (not supported by all commands)\n" );return 0 ; }
@@ -6291,6 +6292,13 @@ int main(int argc, char **argp) argc -=1 ;continue ; } +if (*argp && (!strcmp (*argp,"--include-statistics" ) || + !strcmp (*argp,"-I" ))) { + ctx.show_stats = true; + argp +=1 ; + argc -=1 ; +continue ; + }break ; }if (*argp && !strcmp (*argp,"--monitor" )) {
internal.h +1 -0
@@ -225,6 +225,7 @@ struct cmd_context {char **argp;/* arguments to the sub-command */ unsigned long debug;/* debugging mask */ bool json;/* Output JSON, if supported */ +bool show_stats;/* include command-specific stats */ #ifdef ETHTOOL_ENABLE_NETLINK struct nl_context *nlctx;/* netlink context (opaque) */ #endif
Signature · 1 line
2.26.2
Jakub Kicinski
maintainerPATCH v3 5/7 netlink: prepare for more per-op info
In reply to Jakub KicinskiWe stored an array of op flags, to check if operations are supported. Make that array a structure rather than plain uint32_t in preparation for storing more state.
v3: new patch
--- netlink/netlink.c | 25 +++++++++++++------------ netlink/netlink.h | 6 +++++- 2 files changed, 18 insertions(+), 13 deletions(-)
netlink/netlink.c +13 -12
@@ -120,19 +120,19 @@ bool netlink_cmd_check(struct cmd_context *ctx, unsigned int cmd, nlctx->wildcard_unsupported = true;return true; } -if (!nlctx->ops_flags) { +if (!nlctx->ops_info) { nlctx->ioctl_fallback = true;return false; } -if (cmd >ETHTOOL_MSG_USER_MAX || !nlctx->ops_flags[cmd]) { +if (cmd >ETHTOOL_MSG_USER_MAX || !nlctx->ops_info[cmd].op_flags) { nlctx->ioctl_fallback = true;return true; } -if (is_dump && !(nlctx->ops_flags[cmd] &GENL_CMD_CAP_DUMP )) +if (is_dump && !(nlctx->ops_info[cmd].op_flags &GENL_CMD_CAP_DUMP )) nlctx->wildcard_unsupported = true; -return !(nlctx->ops_flags[cmd] & cap); +return !(nlctx->ops_info[cmd].op_flags & cap); }/* initialization */
@@ -140,12 +140,12 @@ bool netlink_cmd_check(struct cmd_context *ctx, unsigned int cmd,static int genl_read_ops (struct nl_context *nlctx,const struct nlattr *ops_attr) { +struct nl_op_info *ops_info;struct nlattr *op_attr; -uint32_t *ops_flags;int ret; - ops_flags =calloc (__ETHTOOL_MSG_USER_CNT,sizeof (ops_flags[0 ])); -if (!ops_flags) + ops_info =calloc (__ETHTOOL_MSG_USER_CNT,sizeof (ops_info[0 ])); +if (!ops_info)return -ENOMEM ;mnl_attr_for_each_nested (op_attr, ops_attr) {
@@ -163,13 +163,14 @@ static int genl_read_ops(struct nl_context *nlctx,if (op_id >= __ETHTOOL_MSG_USER_CNT)continue ; - ops_flags[op_id] =mnl_attr_get_u32 (tb[CTRL_ATTR_OP_FLAGS ]); + ops_info[op_id].op_flags = +mnl_attr_get_u32 (tb[CTRL_ATTR_OP_FLAGS ]); } - nlctx->ops_flags = ops_flags; + nlctx->ops_info = ops_info;return 0 ; err: -free (ops_flags); +free (ops_info);return ret; }
@@ -273,7 +274,7 @@ int netlink_init(struct cmd_context *ctx) out_nlsk:nlsock_done (nlctx->ethnl_socket); out_free: -free (nlctx->ops_flags); +free (nlctx->ops_info);free (nlctx);return ret; }
@@ -283,7 +284,7 @@ static void netlink_done(struct cmd_context *ctx)if (!ctx->nlctx)return ; -free (ctx->nlctx->ops_flags); +free (ctx->nlctx->ops_info);free (ctx->nlctx); ctx->nlctx =NULL ;cleanup_all_strings ();
netlink/netlink.h +5 -1
@@ -25,6 +25,10 @@ enum link_mode_class { LM_CLASS_FEC, }; +struct nl_op_info { +uint32_t op_flags; +}; +struct nl_context {struct cmd_context *ctx;void *cmd_private;
@@ -34,7 +38,7 @@ struct nl_context {unsigned int suppress_nlerr;uint16_t ethnl_fam;uint32_t ethnl_mongrp; -uint32_t *ops_flags; +struct nl_op_info *ops_info;struct nl_socket *ethnl_socket;struct nl_socket *ethnl2_socket;struct nl_socket *rtnl_socket;
Signature · 1 line
2.26.2
Jakub Kicinski
maintainerPATCH v3 6/7 netlink: use policy dumping to check if stats flag is supported
In reply to Jakub KicinskiOlder kernels don't support statistics, to avoid retries make use of netlink policy dumps to figure out which flags kernel actually supports.
v3:
- s/ctx/policy_ctx/
- save the flags in nl_context to be able to reuse them, and not have to return errors and values from the policy get function
--- netlink/msgbuff.h | 6 ++ netlink/netlink.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++ netlink/netlink.h | 4 ++ 3 files changed, 164 insertions(+)
netlink/msgbuff.h +6 -0
@@ -81,6 +81,12 @@ static inline bool ethnla_put_u32(struct nl_msg_buff *msgbuff, uint16_t type,return ethnla_put (msgbuff, type,sizeof (uint32_t ), &data); } +static inline bool ethnla_put_u16 (struct nl_msg_buff *msgbuff,uint16_t type, +uint16_t data) +{ +return ethnla_put (msgbuff, type,sizeof (uint16_t ), &data); +} +static inline bool ethnla_put_u8 (struct nl_msg_buff *msgbuff,uint16_t type,uint8_t data) {
netlink/netlink.c +154 -0
160 lines not on this page · raw message
netlink/netlink.h +4 -0
@@ -27,6 +27,8 @@ enum link_mode_class {struct nl_op_info {uint32_t op_flags; +uint32_t hdr_flags; +uint8_t hdr_policy_loaded:1 ; };struct nl_context {
@@ -70,6 +72,8 @@ bool netlink_cmd_check(struct cmd_context *ctx, unsigned int cmd, bool allow_wildcard);const char *get_dev_name (const struct nlattr *nest);int get_dev_info (const struct nlattr *nest,int *ifindex,char *ifname); +u32 get_stats_flag (struct nl_context *nlctx,unsigned int nlcmd, +unsigned int hdrattr);int linkmodes_reply_cb (const struct nlmsghdr *nlhdr,void *data);int linkinfo_reply_cb (const struct nlmsghdr *nlhdr,void *data);
Signature · 1 line
2.26.2
Jakub Kicinski
maintainerPATCH v3 7/7 pause: add support for dumping statistics
In reply to Jakub KicinskiAdd support for requesting pause frame stats from the kernel.
# ./ethtool -I -a eth0 Pause parameters for eth0: Autonegotiate: on RX: on TX: on Statistics: tx_pause_frames: 1 rx_pause_frames: 1
# ./ethtool -I --json -a eth0
[ {
"ifname": "eth0",
"autonegotiate": true,
"rx": true,
"tx": true,
"statistics": {
"tx_pause_frames": 1,
"rx_pause_frames": 1
}
} ]v2: - correct print format for u64
--- netlink/pause.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-)
netlink/pause.c +66 -1
@@ -5,6 +5,7 @@*/ #include <errno.h> +#include <inttypes.h> #include <string.h> #include <stdio.h>
@@ -105,6 +106,62 @@ static int show_pause_autoneg_status(struct nl_context *nlctx)return ret; } +static int show_pause_stats (const struct nlattr *nest) +{ +const struct nlattr *tb[ETHTOOL_A_PAUSE_STAT_MAX +1 ] = {}; +DECLARE_ATTR_TB_INFO (tb); +static const struct { +unsigned int attr; +char *name; + } stats[] = { + {ETHTOOL_A_PAUSE_STAT_TX_FRAMES ,"tx_pause_frames" }, + {ETHTOOL_A_PAUSE_STAT_RX_FRAMES ,"rx_pause_frames" }, + }; +bool header = false; +unsigned int i; +size_t n; +int ret; + + ret =mnl_attr_parse_nested (nest, attr_cb, &tb_info); +if (ret <0 ) +return ret; + +open_json_object ("statistics" ); +for (i =0 ; i <ARRAY_SIZE (stats); i++) { +char fmt[32 ]; + +if (!tb[stats[i].attr]) +continue ; + +if (!header && !is_json_context ()) { +printf ("Statistics:\n" ); + header = true; + } + +if (mnl_attr_validate (tb[stats[i].attr],MNL_TYPE_U64 )) { +fprintf (stderr,"malformed netlink message (statistic)\n" ); + goto err_close_stats; + } + + n =snprintf (fmt,sizeof (fmt)," %s: %%" PRIu64"\n" , + stats[i].name); +if (n >=sizeof (fmt)) { +fprintf (stderr,"internal error - malformed label\n" ); + goto err_close_stats; + } + +print_u64 (PRINT_ANY , stats[i].name, fmt, +mnl_attr_get_u64 (tb[stats[i].attr])); + } +close_json_object (); + +return 0 ; + +err_close_stats: +close_json_object (); +return -1 ; +} +int pause_reply_cb (const struct nlmsghdr *nlhdr,void *data) {const struct nlattr *tb[ETHTOOL_A_PAUSE_MAX +1 ] = {};
@@ -142,6 +199,11 @@ int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data)if (ret <0 ) goto err_close_dev; } +if (tb[ETHTOOL_A_PAUSE_STATS ]) { + ret =show_pause_stats (tb[ETHTOOL_A_PAUSE_STATS ]); +if (ret <0 ) + goto err_close_dev; + }if (!silent)print_nl ();
@@ -158,6 +220,7 @@ int nl_gpause(struct cmd_context *ctx) {struct nl_context *nlctx = ctx->nlctx;struct nl_socket *nlsk = nlctx->ethnl_socket; +u32 flags;int ret;if (netlink_cmd_check (ctx,ETHTOOL_MSG_PAUSE_GET , true))
@@ -168,8 +231,10 @@ int nl_gpause(struct cmd_context *ctx)return 1 ; } + flags =get_stats_flag (nlctx,ETHTOOL_MSG_PAUSE_GET , +ETHTOOL_A_PAUSE_HEADER ); ret =nlsock_prep_get_request (nlsk,ETHTOOL_MSG_PAUSE_GET , -ETHTOOL_A_PAUSE_HEADER ,0 ); +ETHTOOL_A_PAUSE_HEADER , flags);if (ret <0 )return ret;
Signature · 1 line
2.26.2
Quoted 3 lines · Jakub Kicinski
Hi!
Sorry about the delay from v2.
Actually, I'm rather surprised you were able to get back to this so early, given the situation.
Quoted 28 lines
This set adds support for pause frame statistics.
First pause frame info is extended to support --json.
Pause stats are first of this kind of statistics so add support for a new flag (--include-statistics).
Next add support for dumping policy to check if the statistics flag is supported for a given subcommand.
Last but not least - display statistics.
v3:
- rename the ctx variable topolicy_ctx
- instead of returning the flags policy save it to a member of structnl_context, for potential reuse. Also we don't have to worry about returning flags and negative errors from the read helper this way :)Jakub Kicinski (7):
update UAPI header copies
pause: add --json support
separate FLAGS out in -h
add support for stats in subcommands
netlink: prepare for more per-op info
netlink: use policy dumping to check if stats flag is supported
pause: add support for dumping statistics
Quoted 11 lines
ethtool.8.in | 7 ++ ethtool.c | 17 +++- internal.h | 1 + netlink/coalesce.c | 6 +- netlink/msgbuff.h | 6 ++ netlink/netlink.c | 179 ++++++++++++++++++++++++++++++++++++++--- netlink/netlink.h | 31 +++++-- netlink/pause.c | 111 ++++++++++++++++++++++--- uapi/linux/genetlink.h | 11 +++ uapi/linux/netlink.h | 4 + 10 files changed, 336 insertions(+), 37 deletions(-)
Series applied, thank you.
Michal