public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters
@ 2024-01-24 15:34 Victor Nogueira
  2024-01-24 15:34 ` [PATCH iproute2-next 1/2] tc: add NLM_F_ECHO support for actions Victor Nogueira
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Victor Nogueira @ 2024-01-24 15:34 UTC (permalink / raw)
  To: stephen, dsahern, netdev; +Cc: liuhangbin, jhs, kernel

Continuing on what Hangbin Liu started [1], this patch set adds support for
the NLM_F_ECHO flag for tc actions and filters. For qdiscs it will require
some kernel surgery, and we'll send it soon after this surgery is merged.

When user space configures the kernel with netlink messages, it can set
NLM_F_ECHO flag to request the kernel to send the applied configuration
back to the caller. This allows user space to receive back configuration
information that is populated by the kernel. Often because there are
parameters that can only be set by the kernel which become visible with the
echo, or because user space lets the kernel choose a default value.

To illustrate a use case where the kernel will give us a default value,
the example below shows the user not specifying the action index:

    tc -echo actions add action mirred egress mirror dev lo
  
    total acts 0
    Added action
          action order 1: mirred (Egress Mirror to device lo) pipe
          index 1 ref 1 bind 0
          not_in_hw

Note that the echoed response indicates that the kernel gave us a value
of index 1

[1] https://lore.kernel.org/netdev/20220916033428.400131-2-liuhangbin@gmail.com/

Victor Nogueira (2):
  tc: add NLM_F_ECHO support for actions
  tc: Add NLM_F_ECHO support for filters

 man/man8/tc.8  |  6 +++++-
 tc/m_action.c  | 25 ++++++++++++++++++++++---
 tc/tc.c        |  6 +++++-
 tc/tc_filter.c |  8 +++++++-
 4 files changed, 39 insertions(+), 6 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH iproute2-next 1/2] tc: add NLM_F_ECHO support for actions
  2024-01-24 15:34 [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters Victor Nogueira
@ 2024-01-24 15:34 ` Victor Nogueira
  2024-01-24 15:34 ` [PATCH iproute2-next 2/2] tc: Add NLM_F_ECHO support for filters Victor Nogueira
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Victor Nogueira @ 2024-01-24 15:34 UTC (permalink / raw)
  To: stephen, dsahern, netdev; +Cc: liuhangbin, jhs, kernel

This patch adds the -echo flag to tc command line and support for it in
tc actions. If the user specifies this flag for an action command, the
kernel will return the command's result back to user space.
For example:

  tc -echo actions add action mirred egress mirror dev lo

  total acts 0
  Added action
        action order 1: mirred (Egress Mirror to device lo) pipe
        index 10 ref 1 bind 0
        not_in_hw

As illustrated above, the kernel will give us an index of 10

The same can be done for other action commands (replace, change, and
delete). For example:

  tc -echo actions delete action mirred index 10

  total acts 0
  Deleted action
        action order 1: mirred (Egress Mirror to device lo) pipe
        index 10 ref 0 bind 0
        not_in_hw

Signed-off-by: Victor Nogueira <victor@mojatatu.com>
---
 man/man8/tc.8 |  6 +++++-
 tc/m_action.c | 25 ++++++++++++++++++++++---
 tc/tc.c       |  6 +++++-
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/man/man8/tc.8 b/man/man8/tc.8
index 3175454b9..dce58af17 100644
--- a/man/man8/tc.8
+++ b/man/man8/tc.8
@@ -127,7 +127,7 @@ tc \- show / manipulate traffic control settings
 \fB[ \fB-nm \fR| \fB-nam\fR[\fIes\fR] \fB] \fR|
 \fB[ \fR{ \fB-cf \fR| \fB-c\fR[\fIonf\fR] \fR} \fB[ filename ] \fB] \fR
 \fB[ -t\fR[imestamp\fR] \fB\] \fR| \fB[ -t\fR[short\fR] \fR| \fB[
--o\fR[neline\fR] \fB]\fR }
+-o\fR[neline\fR] \fB] \fR| \fB[ -echo ]\fR }
 
 .ti 8
 .IR FORMAT " := {"
@@ -743,6 +743,10 @@ When\fB\ tc monitor\fR\ runs, print timestamp before the event message in format
 When\fB\ tc monitor\fR\ runs, prints short timestamp before the event message in format:
    [<YYYY>-<MM>-<DD>T<hh:mm:ss>.<ms>]
 
+.TP
+.BR "\-echo"
+Request the kernel to send the applied configuration back.
+
 .SH FORMAT
 The show command has additional formatting options:
 
diff --git a/tc/m_action.c b/tc/m_action.c
index 16474c561..fd9621e1b 100644
--- a/tc/m_action.c
+++ b/tc/m_action.c
@@ -688,7 +688,16 @@ static int tc_action_gd(int cmd, unsigned int flags,
 
 	req.n.nlmsg_seq = rth.dump = ++rth.seq;
 
-	if (rtnl_talk(&rth, &req.n, cmd == RTM_DELACTION ? NULL : &ans) < 0) {
+	if (cmd == RTM_DELACTION) {
+		if (echo_request)
+			ret = rtnl_echo_talk(&rth, &req.n, json, print_action);
+		else
+			ret = rtnl_talk(&rth, &req.n, NULL);
+	} else {
+		ret = rtnl_talk(&rth, &req.n, &ans);
+	}
+
+	if (ret < 0) {
 		fprintf(stderr, "We have an error talking to the kernel\n");
 		return 1;
 	}
@@ -738,7 +747,12 @@ static int tc_action_modify(int cmd, unsigned int flags,
 	}
 	tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
 
-	if (rtnl_talk(&rth, &req.n, NULL) < 0) {
+	if (echo_request)
+		ret = rtnl_echo_talk(&rth, &req.n, json, print_action);
+	else
+		ret = rtnl_talk(&rth, &req.n, NULL);
+
+	if (ret < 0) {
 		fprintf(stderr, "We have an error talking to the kernel\n");
 		ret = -1;
 	}
@@ -836,7 +850,12 @@ static int tc_act_list_or_flush(int *argc_p, char ***argv_p, int event)
 		req.n.nlmsg_type = RTM_DELACTION;
 		req.n.nlmsg_flags |= NLM_F_ROOT;
 		req.n.nlmsg_flags |= NLM_F_REQUEST;
-		if (rtnl_talk(&rth, &req.n, NULL) < 0) {
+
+		if (echo_request)
+			ret = rtnl_echo_talk(&rth, &req.n, json, print_action);
+		else
+			ret = rtnl_talk(&rth, &req.n, NULL);
+		if (ret < 0) {
 			fprintf(stderr, "We have an error flushing\n");
 			return 1;
 		}
diff --git a/tc/tc.c b/tc/tc.c
index 575157a86..7a746cf51 100644
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -38,6 +38,8 @@ int json;
 int oneline;
 int brief;
 
+int echo_request;
+
 static char *conf_file;
 
 struct rtnl_handle rth;
@@ -196,7 +198,7 @@ static void usage(void)
 		"		    -o[neline] | -j[son] | -p[retty] | -c[olor]\n"
 		"		    -b[atch] [filename] | -n[etns] name | -N[umeric] |\n"
 		"		     -nm | -nam[es] | { -cf | -conf } path\n"
-		"		     -br[ief] }\n");
+		"		     -br[ief] | -echo }\n");
 }
 
 static int do_cmd(int argc, char **argv)
@@ -314,6 +316,8 @@ int main(int argc, char **argv)
 			++oneline;
 		} else if (matches(argv[1], "-brief") == 0) {
 			++brief;
+		} else if (strcmp(argv[1], "-echo") == 0) {
+			++echo_request;
 		} else {
 			fprintf(stderr,
 				"Option \"%s\" is unknown, try \"tc -help\".\n",
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH iproute2-next 2/2] tc: Add NLM_F_ECHO support for filters
  2024-01-24 15:34 [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters Victor Nogueira
  2024-01-24 15:34 ` [PATCH iproute2-next 1/2] tc: add NLM_F_ECHO support for actions Victor Nogueira
@ 2024-01-24 15:34 ` Victor Nogueira
  2024-01-25  2:37 ` [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters Hangbin Liu
  2024-01-30 15:50 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: Victor Nogueira @ 2024-01-24 15:34 UTC (permalink / raw)
  To: stephen, dsahern, netdev; +Cc: liuhangbin, jhs, kernel

If the user specifies this flag for a filter command the kernel will
return the command's result back to user space.
For example:

  tc -echo filter add dev lo parent ffff: protocol ip matchall action ok

  added filter dev lo parent ffff: protocol ip pref 49152 matchall chain 0

As illustrated above, the kernel will give us a pref of 491252

The same can be done for other filter commands (replace, delete, and
change). For example:

  tc -echo filter del dev lo parent ffff: pref 49152 protocol ip matchall

  deleted filter dev lo parent ffff: protocol ip pref 49152 matchall chain 0

Signed-off-by: Victor Nogueira <victor@mojatatu.com>
---
 tc/tc_filter.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tc/tc_filter.c b/tc/tc_filter.c
index eb45c5887..54790ddc6 100644
--- a/tc/tc_filter.c
+++ b/tc/tc_filter.c
@@ -76,6 +76,7 @@ static int tc_filter_modify(int cmd, unsigned int flags, int argc, char **argv)
 	char  d[IFNAMSIZ] = {};
 	char  k[FILTER_NAMESZ] = {};
 	struct tc_estimator est = {};
+	int ret;
 
 	if (cmd == RTM_NEWTFILTER && flags & NLM_F_CREATE)
 		protocol = htons(ETH_P_ALL);
@@ -221,7 +222,12 @@ static int tc_filter_modify(int cmd, unsigned int flags, int argc, char **argv)
 	if (est.ewma_log)
 		addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
 
-	if (rtnl_talk(&rth, &req.n, NULL) < 0) {
+	if (echo_request)
+		ret = rtnl_echo_talk(&rth, &req.n, json, print_filter);
+	else
+		ret = rtnl_talk(&rth, &req.n, NULL);
+
+	if (ret < 0) {
 		fprintf(stderr, "We have an error talking to the kernel\n");
 		return 2;
 	}
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters
  2024-01-24 15:34 [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters Victor Nogueira
  2024-01-24 15:34 ` [PATCH iproute2-next 1/2] tc: add NLM_F_ECHO support for actions Victor Nogueira
  2024-01-24 15:34 ` [PATCH iproute2-next 2/2] tc: Add NLM_F_ECHO support for filters Victor Nogueira
@ 2024-01-25  2:37 ` Hangbin Liu
  2024-01-30 15:50 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: Hangbin Liu @ 2024-01-25  2:37 UTC (permalink / raw)
  To: Victor Nogueira; +Cc: stephen, dsahern, netdev, jhs, kernel

On Wed, Jan 24, 2024 at 12:34:54PM -0300, Victor Nogueira wrote:
> Continuing on what Hangbin Liu started [1], this patch set adds support for
> the NLM_F_ECHO flag for tc actions and filters. For qdiscs it will require
> some kernel surgery, and we'll send it soon after this surgery is merged.
> 
> When user space configures the kernel with netlink messages, it can set
> NLM_F_ECHO flag to request the kernel to send the applied configuration
> back to the caller. This allows user space to receive back configuration
> information that is populated by the kernel. Often because there are
> parameters that can only be set by the kernel which become visible with the
> echo, or because user space lets the kernel choose a default value.
> 
> To illustrate a use case where the kernel will give us a default value,
> the example below shows the user not specifying the action index:
> 
>     tc -echo actions add action mirred egress mirror dev lo
>   
>     total acts 0
>     Added action
>           action order 1: mirred (Egress Mirror to device lo) pipe
>           index 1 ref 1 bind 0
>           not_in_hw
> 
> Note that the echoed response indicates that the kernel gave us a value
> of index 1
> 

Looks good to me.

Reviewed-by: Hangbin Liu <liuhangbin@gmail.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters
  2024-01-24 15:34 [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters Victor Nogueira
                   ` (2 preceding siblings ...)
  2024-01-25  2:37 ` [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters Hangbin Liu
@ 2024-01-30 15:50 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-01-30 15:50 UTC (permalink / raw)
  To: Victor Nogueira; +Cc: stephen, dsahern, netdev, liuhangbin, jhs, kernel

Hello:

This series was applied to iproute2/iproute2-next.git (main)
by David Ahern <dsahern@kernel.org>:

On Wed, 24 Jan 2024 12:34:54 -0300 you wrote:
> Continuing on what Hangbin Liu started [1], this patch set adds support for
> the NLM_F_ECHO flag for tc actions and filters. For qdiscs it will require
> some kernel surgery, and we'll send it soon after this surgery is merged.
> 
> When user space configures the kernel with netlink messages, it can set
> NLM_F_ECHO flag to request the kernel to send the applied configuration
> back to the caller. This allows user space to receive back configuration
> information that is populated by the kernel. Often because there are
> parameters that can only be set by the kernel which become visible with the
> echo, or because user space lets the kernel choose a default value.
> 
> [...]

Here is the summary with links:
  - [iproute2-next,1/2] tc: add NLM_F_ECHO support for actions
    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=071144c0bbb9
  - [iproute2-next,2/2] tc: Add NLM_F_ECHO support for filters
    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=cf0eae9a9fc4

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-01-30 15:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-24 15:34 [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters Victor Nogueira
2024-01-24 15:34 ` [PATCH iproute2-next 1/2] tc: add NLM_F_ECHO support for actions Victor Nogueira
2024-01-24 15:34 ` [PATCH iproute2-next 2/2] tc: Add NLM_F_ECHO support for filters Victor Nogueira
2024-01-25  2:37 ` [PATCH iproute2-next 0/2] tc: add NLM_F_ECHO support for actions and filters Hangbin Liu
2024-01-30 15:50 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox