Skip to contentSkip to conversation

mptcp: add support for event monitoring

MPTCP Florian Westphal 2 participants 4 messages Jan 14, 2021 – Jan 16, 2021

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author
#0 lore raw

Output will look similar to this:

[ CREATED] token=83f3a692 remid=0 locid=0 saddr4=10.0.1.2 daddr4=10.0.1.1 sport=58710 dport=10011
[ ESTABLISHED] token=83f3a692 remid=0 locid=0 saddr4=10.0.1.2 daddr4=10.0.1.1 sport=58710 dport=10011
[SF_ESTABLISHED] token=83f3a692 remid=0 locid=1 saddr4=10.0.2.2 daddr4=10.0.1.1 sport=40195 dport=10011 backup=0
[ CLOSED] token=83f3a692

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 include/libgenl.h          |   1 +
 include/uapi/linux/mptcp.h |  76 ++++++++++++++++++++++++++
 ip/ipmptcp.c               | 106 +++++++++++++++++++++++++++++++++++++
 lib/libgenl.c              |  66 +++++++++++++++++++++++
 4 files changed, 249 insertions(+)
include/libgenl.h +1 -0
index 656493a2c3c4..97281cc1103f 100644
@@ -21,6 +21,7 @@ struct {								\
 	},								\
 }
 
+int genl_add_mcast_grp(struct rtnl_handle *grth, __u16 genl_family, const char *group);
 int genl_resolve_family(struct rtnl_handle *grth, const char *family);
 int genl_init_handle(struct rtnl_handle *grth, const char *family,
 		     int *genl_family);
include/uapi/linux/mptcp.h +76 -0

88 lines not on this page · raw message

ip/ipmptcp.c +106 -0

118 lines not on this page · raw message

lib/libgenl.c +66 -0

72 lines not on this page · raw message

Signature · 1 line
2.26.2
#1 lore raw

Hi Florian,

Quoted 6 lines · Florian Westphal

Output will look similar to this:

[ CREATED] token=83f3a692 remid=0 locid=0 saddr4=10.0.1.2 daddr4=10.0.1.1 sport=58710 dport=10011
[ ESTABLISHED] token=83f3a692 remid=0 locid=0 saddr4=10.0.1.2 daddr4=10.0.1.1 sport=58710 dport=10011
[SF_ESTABLISHED] token=83f3a692 remid=0 locid=1 saddr4=10.0.2.2 daddr4=10.0.1.1 sport=40195 dport=10011 backup=0
[ CLOSED] token=83f3a692

Thank you for this new feature, that's a good idea!

It looks good! Just the usage/man page to update I think! I just asked a few questions below if you don't mind looking :)

(...)

include/uapi/linux/mptcp.h enum { · line 39

Quoted 9 lines
include/uapi/linux/mptcp.h +1 -0
index 321812307718..199b0c46ba70 100644
@@ -36,6 +36,7 @@ enum {
  /* netlink interface */
  #define MPTCP_PM_NAME		"mptcp_pm"
  #define MPTCP_PM_CMD_GRP_NAME	"mptcp_pm_cmds"
+#define MPTCP_PM_EV_GRP_NAME	"mptcp_events"

(just to be sure: we don't want to prefix with "_pm" to keep compatibility with mptcp.org kernel, right?)

include/uapi/linux/mptcp.h enum { · line 40

Quoted 1 line
include/uapi/linux/mptcp.h +0 -0
  #define MPTCP_PM_VER		0x1

I guess we don't need to change that, on older version of the kernel we can just check if "mptcp_events" has been added.

(...)

Quoted 7 lines
ip/ipmptcp.c +0 -0
index e1ffafb3c658..4716aa9e62e8 100644
@@ -385,6 +385,104 @@ static int mptcp_limit_get_set(int argc, char **argv, int cmd)
  	return 0;
  }

(...)

ip/ipmptcp.c static int mptcp_limit_get_set(int argc, char **argv, int cmd) · line 405

Quoted 7 lines
ip/ipmptcp.c +7 -0
+static void print_addr(const char *key, int af, struct rtattr *value)
+{
+	void *data = RTA_DATA(value);
+	char str[INET6_ADDRSTRLEN];
+
+	if (inet_ntop(af, data, str, sizeof(str)))
+		printf(" %s=%s", key, str);

Should we display an error in case of issue to parse the address? Or no need because it should not happen?

Quoted 10 lines
ip/ipmptcp.c +10 -0
+}
+
+static int mptcp_monitor_msg(struct rtnl_ctrl_data *ctrl,
+			     struct nlmsghdr *n, void *arg)
+{
+	const struct genlmsghdr *ghdr = NLMSG_DATA(n);
+	struct rtattr *tb[MPTCP_ATTR_MAX + 1];
+	int len = n->nlmsg_len;
+
+	len -= NLMSG_LENGTH(GENL_HDRLEN);

ip/ipmptcp.c static int mptcp_limit_get_set(int argc, char **argv, int cmd) · line 425

Quoted 10 lines
ip/ipmptcp.c +10 -0
+	if (len < 0)
+		return -1;
+
+	if (n->nlmsg_type != genl_family)
+		return 0;
+
+	if (ghdr->cmd >= ARRAY_SIZE(event_to_str)) {
+		printf("[UNKNOWN %u]\n", ghdr->cmd);
+		fflush(stdout);
+		return 0;

(detail: maybe good to have a goto here and below to do the fflush + return (+puts?) mainly for later not to forget that? Up to you, a detail)

(...)

ip/ipmptcp.c static int mptcp_limit_get_set(int argc, char **argv, int cmd) · line 438

Quoted 1 line
ip/ipmptcp.c +1 -0
+	printf(" token=%08x", rta_getattr_u32(tb[MPTCP_ATTR_TOKEN]));

If we are not the user ID 0, we cannot do the monitoring, right?

ip/ipmptcp.c int do_mptcp(int argc, char **argv) · line 530

Quoted 6 lines

if (argc == 0)

ip/ipmptcp.c +1 -0
@@ -429,6 +527,14 @@ int do_mptcp(int argc, char **argv)
  						   MPTCP_PM_CMD_GET_LIMITS);
  	}
 
+	if (matches(*argv, "monitor") == 0) {

May you update the "usage()" function here above and the man page to reflect this new parameter please?

(...)

Quoted 9 lines
lib/libgenl.c +1 -0
index f2ce698fc711..4c51d47af46b 100644
@@ -67,6 +67,72 @@ int genl_resolve_family(struct rtnl_handle *grth, const char *family)
  	return fnum;
  }
 
+static int genl_parse_grps(struct rtattr *attr, const char *name, unsigned int *id)

lib/libgenl.c int genl_resolve_family(struct rtnl_handle *grth, const char *family) · line 80

Quoted 10 lines
lib/libgenl.c +10 -0
+{
+	const struct rtattr *pos;
+
+	rtattr_for_each_nested(pos, attr) {
+		struct rtattr *tb[CTRL_ATTR_MCAST_GRP_MAX + 1];
+
+		parse_rtattr_nested(tb, CTRL_ATTR_MCAST_GRP_MAX, pos);
+
+		if (tb[CTRL_ATTR_MCAST_GRP_NAME] && tb[CTRL_ATTR_MCAST_GRP_ID]) {
+			if (strcmp(name, rta_getattr_str(tb[CTRL_ATTR_MCAST_GRP_NAME])) == 0) {

Should we use strncmp()? (But not sure with which limit so probably no)

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net
author
#2 lore raw
Quoted 10 lines · Matthieu Baerts

Hi Florian,

Quoted 6 lines · Florian Westphal

Output will look similar to this:

[ CREATED] token=83f3a692 remid=0 locid=0 saddr4=10.0.1.2 daddr4=10.0.1.1 sport=58710 dport=10011
[ ESTABLISHED] token=83f3a692 remid=0 locid=0 saddr4=10.0.1.2 daddr4=10.0.1.1 sport=58710 dport=10011
[SF_ESTABLISHED] token=83f3a692 remid=0 locid=1 saddr4=10.0.2.2 daddr4=10.0.1.1 sport=40195 dport=10011 backup=0
[ CLOSED] token=83f3a692

Quoted 4 lines

Thank you for this new feature, that's a good idea!

It looks good! Just the usage/man page to update I think! I just asked a few questions below if you don't mind looking :)

Right, will update this for the netdev submission.

Quoted 4 lines
Quoted 4 lines
include/uapi/linux/mptcp.h +0 -0
index 321812307718..199b0c46ba70 100644
Quoted 8 lines

include/uapi/linux/mptcp.h enum { · line 39

Quoted 5 lines
include/uapi/linux/mptcp.h +1 -0
@@ -36,6 +36,7 @@ enum {
  /* netlink interface */
  #define MPTCP_PM_NAME		"mptcp_pm"
  #define MPTCP_PM_CMD_GRP_NAME	"mptcp_pm_cmds"
+#define MPTCP_PM_EV_GRP_NAME	"mptcp_events"

(just to be sure: we don't want to prefix with "_pm" to keep compatibility with mptcp.org kernel, right?)

Only change needed on userspace side is use of "mptcp_pm" instead of plain "mptcp" for the genl name.

Mandation use of "mptcp_pm_events" would mean a second change and I do not see a good reason for it.

Quoted 4 lines

include/uapi/linux/mptcp.h enum { · line 40

Quoted 1 line
include/uapi/linux/mptcp.h +0 -0
  #define MPTCP_PM_VER		0x1

I guess we don't need to change that, on older version of the kernel we can just check if "mptcp_events" has been added.

On old kernel the subscription request will fail.
I would only increment this in case of incompatible changes, which should be avoided anyway.

Quoted 10 lines

include/uapi/linux/mptcp.h enum {

Quoted 7 lines
include/uapi/linux/mptcp.h +7 -0
+static void print_addr(const char *key, int af, struct rtattr *value)
+{
+	void *data = RTA_DATA(value);
+	char str[INET6_ADDRSTRLEN];
+
+	if (inet_ntop(af, data, str, sizeof(str)))
+		printf(" %s=%s", key, str);

Should we display an error in case of issue to parse the address? Or no need because it should not happen?

I don't think it makes sense to handle errors here.
The only error message we could display here would be something like "kernel netlink abi is broken".

I can do that (plus abort()) in the next iteration.

Quoted 7 lines

include/uapi/linux/mptcp.h

Quoted 4 lines
include/uapi/linux/mptcp.h +4 -0
+	if (ghdr->cmd >= ARRAY_SIZE(event_to_str)) {
+		printf("[UNKNOWN %u]\n", ghdr->cmd);
+		fflush(stdout);
+		return 0;

(detail: maybe good to have a goto here and below to do the fflush + return (+puts?) mainly for later not to forget that? Up to you, a detail)

Ok, I can change it.

Quoted 3 lines

include/uapi/linux/mptcp.h

Quoted 1 line
include/uapi/linux/mptcp.h +1 -0
+	printf(" token=%08x", rta_getattr_u32(tb[MPTCP_ATTR_TOKEN]));

If we are not the user ID 0, we cannot do the monitoring, right?

No, netlink mcast subscription will fail with -EPERM in that case.

Quoted 8 lines

include/uapi/linux/mptcp.h int do_mptcp(int argc, char **argv) · line 529

Quoted 5 lines

if (argc == 0)

include/uapi/linux/mptcp.h +1 -0
@@ -429,6 +527,14 @@ int do_mptcp(int argc, char **argv)
  						   MPTCP_PM_CMD_GET_LIMITS);
  	}
+	if (matches(*argv, "monitor") == 0) {

May you update the "usage()" function here above and the man page to reflect this new parameter please?

Yes, sorry I forgot.

Quoted 5 lines
Quoted 5 lines
include/uapi/linux/mptcp.h +5 -0
+static int genl_parse_grps(struct rtattr *attr, const char *name, unsigned int *id)
+{
+	const struct rtattr *pos;
+
+	rtattr_for_each_nested(pos, attr) {
Quoted 8 lines

include/uapi/linux/mptcp.h int do_mptcp(int argc, char **argv) · line 540

Quoted 6 lines
include/uapi/linux/mptcp.h +6 -0
+		struct rtattr *tb[CTRL_ATTR_MCAST_GRP_MAX + 1];
+
+		parse_rtattr_nested(tb, CTRL_ATTR_MCAST_GRP_MAX, pos);
+
+		if (tb[CTRL_ATTR_MCAST_GRP_NAME] && tb[CTRL_ATTR_MCAST_GRP_ID]) {
+			if (strcmp(name, rta_getattr_str(tb[CTRL_ATTR_MCAST_GRP_NAME])) == 0) {

Should we use strncmp()? (But not sure with which limit so probably no)

No, the string is part of the abi, so it will always match "mptcp_event" in case the feature is present.

#3 lore raw

Hi Florian,

Quoted 9 lines · Florian Westphal
Quoted 8 lines · Matthieu Baerts
Quoted 6 lines · Florian Westphal

Output will look similar to this:

[ CREATED] token=83f3a692 remid=0 locid=0 saddr4=10.0.1.2 daddr4=10.0.1.1 sport=58710 dport=10011
[ ESTABLISHED] token=83f3a692 remid=0 locid=0 saddr4=10.0.1.2 daddr4=10.0.1.1 sport=58710 dport=10011
[SF_ESTABLISHED] token=83f3a692 remid=0 locid=1 saddr4=10.0.2.2 daddr4=10.0.1.1 sport=40195 dport=10011 backup=0
[ CLOSED] token=83f3a692

Quoted 6 lines
Quoted 4 lines

Thank you for this new feature, that's a good idea!

It looks good! Just the usage/man page to update I think! I just asked a few questions below if you don't mind looking :)

Right, will update this for the netdev submission.

Thanks!

And thank you for the other answers!

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net