8cf965
commit 8ca81014a352a125aab38fce2b1302d92c0f5ef2
8cf965
Author: Hangbin Liu <liuhangbin@gmail.com>
8cf965
Date:   Fri Mar 1 15:08:48 2019 +0800
8cf965
8cf965
    rtnl: add team activebackup support
8cf965
    
8cf965
    This patch add team interface activebackup mode support. As linux team use
8cf965
    genl netlink message, when we get a rtnl link change notify, we have to setup
8cf965
    a new genl socket and request the current active port.
8cf965
    
8cf965
    v2: check nlmsg_len before copy rta_data
8cf965
    v3: a) Do not make rtnl_buf global as it may be freed by calling rtnl_close()
8cf965
           while we are using it in rtnl_link_status()
8cf965
        b) Reorder declarations of variables as reversed Christmas tree for
8cf965
           function rtnl_link_status()
8cf965
        c) remove rtnl_len
8cf965
    v4: Remove the first !rtnl_buf check in rtnl_link_status as it's alway true
8cf965
    v5: a) Re-order {nl, rtnl}_open and add function nl_close()
8cf965
        b) revert the v3_{a,c}, v4 changes, use nl_close to close genl fd
8cf965
        c) do not use len in get_team_active_iface() as it may mislead reader
8cf965
    
8cf965
    Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
8cf965
8cf965
diff --git a/missing.h b/missing.h
8cf965
index 2f7adb9..8f92079 100644
8cf965
--- a/missing.h
8cf965
+++ b/missing.h
8cf965
@@ -118,6 +118,22 @@ enum {
8cf965
 #define IFLA_BOND_MAX   (__IFLA_BOND_MAX - 1)
8cf965
 #endif	/*IFLA_BOND_MAX*/
8cf965
 
8cf965
+#ifndef NLA_TYPE_MAX
8cf965
+enum {
8cf965
+        NLA_UNSPEC,
8cf965
+        NLA_U8,
8cf965
+        NLA_U16,
8cf965
+        NLA_U32,
8cf965
+        NLA_U64,
8cf965
+        NLA_STRING,
8cf965
+        NLA_FLAG,
8cf965
+        NLA_MSECS,
8cf965
+        NLA_NESTED,
8cf965
+        __NLA_TYPE_MAX,
8cf965
+};
8cf965
+#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
8cf965
+#endif /*NLA_TYPE_MAX*/
8cf965
+
8cf965
 #ifdef __UCLIBC__
8cf965
 
8cf965
 #if (_XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L) && \
8cf965
diff --git a/phc2sys.8 b/phc2sys.8
8cf965
index 45cb0e3..b3a3de3 100644
8cf965
--- a/phc2sys.8
8cf965
+++ b/phc2sys.8
8cf965
@@ -108,9 +108,9 @@ together with the
8cf965
 option, the master clock is used only to correct the offset by whole number of
8cf965
 seconds, which cannot be fixed with PPS alone. Not compatible with the
8cf965
 .B \-a
8cf965
-option. This option does not support bonded interface (e.g. bond0). If
8cf965
+option. This option does not support bonded interface (e.g. bond0, team0). If
8cf965
 .B ptp4l
8cf965
-has a port on an active-backup bond interface, the
8cf965
+has a port on an active-backup bond or team interface, the
8cf965
 .B \-a
8cf965
 option can be used to track the active interface.
8cf965
 .TP
8cf965
diff --git a/rtnl.c b/rtnl.c
8cf965
index f9a572b..e3758b3 100644
8cf965
--- a/rtnl.c
8cf965
+++ b/rtnl.c
8cf965
@@ -20,6 +20,8 @@
8cf965
 #include <sys/socket.h> /* Must come before linux/netlink.h on some systems. */
8cf965
 #include <linux/netlink.h>
8cf965
 #include <linux/rtnetlink.h>
8cf965
+#include <linux/genetlink.h>
8cf965
+#include <linux/if_team.h>
8cf965
 #include <net/if.h>
8cf965
 #include <stdio.h>
8cf965
 #include <stdlib.h>
8cf965
@@ -30,8 +32,39 @@
8cf965
 #include "print.h"
8cf965
 #include "rtnl.h"
8cf965
 
8cf965
+#define BUF_SIZE 4096
8cf965
+#define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
8cf965
+
8cf965
 static int rtnl_len;
8cf965
 static char *rtnl_buf;
8cf965
+static int get_team_active_iface(int master_index);
8cf965
+
8cf965
+static int nl_close(int fd)
8cf965
+{
8cf965
+	return close(fd);
8cf965
+}
8cf965
+
8cf965
+static int nl_open(int family)
8cf965
+{
8cf965
+	int fd;
8cf965
+	struct sockaddr_nl sa;
8cf965
+
8cf965
+	memset(&sa, 0, sizeof(sa));
8cf965
+	sa.nl_family = AF_NETLINK;
8cf965
+	sa.nl_groups = RTNLGRP_LINK;
8cf965
+
8cf965
+	fd = socket(AF_NETLINK, SOCK_RAW, family);
8cf965
+	if (fd < 0) {
8cf965
+		pr_err("failed to open netlink socket: %m");
8cf965
+		return -1;
8cf965
+	}
8cf965
+	if (bind(fd, (struct sockaddr *) &sa, sizeof(sa))) {
8cf965
+		pr_err("failed to bind netlink socket: %m");
8cf965
+		close(fd);
8cf965
+		return -1;
8cf965
+	}
8cf965
+	return fd;
8cf965
+}
8cf965
 
8cf965
 int rtnl_close(int fd)
8cf965
 {
8cf965
@@ -40,7 +73,12 @@ int rtnl_close(int fd)
8cf965
 		rtnl_buf = NULL;
8cf965
 		rtnl_len = 0;
8cf965
 	}
8cf965
-	return close(fd);
8cf965
+	return nl_close(fd);
8cf965
+}
8cf965
+
8cf965
+int rtnl_open(void)
8cf965
+{
8cf965
+	return nl_open(NETLINK_ROUTE);
8cf965
 }
8cf965
 
8cf965
 static void rtnl_get_ts_device_callback(void *ctx, int linkup, int ts_index)
8cf965
@@ -116,14 +154,24 @@ int rtnl_link_query(int fd, char *device)
8cf965
 	return 0;
8cf965
 }
8cf965
 
8cf965
-static inline __u32 rta_getattr_u32(const struct rtattr *rta)
8cf965
+static inline __u8 rta_getattr_u8(struct rtattr *rta)
8cf965
+{
8cf965
+	return *(__u8 *)RTA_DATA(rta);
8cf965
+}
8cf965
+
8cf965
+static inline __u16 rta_getattr_u16(struct rtattr *rta)
8cf965
+{
8cf965
+	return *(__u16 *)RTA_DATA(rta);
8cf965
+}
8cf965
+
8cf965
+static inline __u32 rta_getattr_u32(struct rtattr *rta)
8cf965
 {
8cf965
 	return *(__u32 *)RTA_DATA(rta);
8cf965
 }
8cf965
 
8cf965
-static inline const char *rta_getattr_str(const struct rtattr *rta)
8cf965
+static inline char *rta_getattr_str(struct rtattr *rta)
8cf965
 {
8cf965
-	return (const char *)RTA_DATA(rta);
8cf965
+	return (char *)RTA_DATA(rta);
8cf965
 }
8cf965
 
8cf965
 static int rtnl_rtattr_parse(struct rtattr *tb[], int max, struct rtattr *rta, int len)
8cf965
@@ -150,12 +198,12 @@ static inline int rtnl_nested_rtattr_parse(struct rtattr *tb[], int max, struct
8cf965
 	return rtnl_rtattr_parse(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta));
8cf965
 }
8cf965
 
8cf965
-static int rtnl_linkinfo_parse(struct rtattr *rta)
8cf965
+static int rtnl_linkinfo_parse(int master_index, struct rtattr *rta)
8cf965
 {
8cf965
-	int index = -1;
8cf965
-	const char *kind;
8cf965
 	struct rtattr *linkinfo[IFLA_INFO_MAX];
8cf965
 	struct rtattr *bond[IFLA_BOND_MAX];
8cf965
+	int index = -1;
8cf965
+	char *kind;
8cf965
 
8cf965
 	if (rtnl_nested_rtattr_parse(linkinfo, IFLA_INFO_MAX, rta) < 0)
8cf965
 		return -1;
8cf965
@@ -172,6 +220,8 @@ static int rtnl_linkinfo_parse(struct rtattr *rta)
8cf965
 			if (bond[IFLA_BOND_ACTIVE_SLAVE]) {
8cf965
 				index = rta_getattr_u32(bond[IFLA_BOND_ACTIVE_SLAVE]);
8cf965
 			}
8cf965
+		} else if (kind && !strncmp(kind, "team", 4)) {
8cf965
+			index = get_team_active_iface(master_index);
8cf965
 		}
8cf965
 	}
8cf965
 	return index;
8cf965
@@ -179,18 +229,18 @@ static int rtnl_linkinfo_parse(struct rtattr *rta)
8cf965
 
8cf965
 int rtnl_link_status(int fd, char *device, rtnl_callback cb, void *ctx)
8cf965
 {
8cf965
+	struct rtattr *tb[IFLA_MAX+1];
8cf965
+	struct ifinfomsg *info = NULL;
8cf965
 	int index, len, link_up;
8cf965
-	int slave_index = -1;
8cf965
-	struct iovec iov;
8cf965
 	struct sockaddr_nl sa;
8cf965
-	struct msghdr msg;
8cf965
+	int slave_index = -1;
8cf965
 	struct nlmsghdr *nh;
8cf965
-	struct ifinfomsg *info = NULL;
8cf965
-	struct rtattr *tb[IFLA_MAX+1];
8cf965
+	struct msghdr msg;
8cf965
+	struct iovec iov;
8cf965
 
8cf965
 	index = if_nametoindex(device);
8cf965
 	if (!rtnl_buf) {
8cf965
-		rtnl_len = 4096;
8cf965
+		rtnl_len = BUF_SIZE;
8cf965
 		rtnl_buf = malloc(rtnl_len);
8cf965
 		if (!rtnl_buf) {
8cf965
 			pr_err("rtnl: low memory");
8cf965
@@ -246,7 +296,7 @@ int rtnl_link_status(int fd, char *device, rtnl_callback cb, void *ctx)
8cf965
 				  IFLA_PAYLOAD(nh));
8cf965
 
8cf965
 		if (tb[IFLA_LINKINFO])
8cf965
-			slave_index = rtnl_linkinfo_parse(tb[IFLA_LINKINFO]);
8cf965
+			slave_index = rtnl_linkinfo_parse(index, tb[IFLA_LINKINFO]);
8cf965
 
8cf965
 		if (cb)
8cf965
 			cb(ctx, link_up, slave_index);
8cf965
@@ -255,24 +305,164 @@ int rtnl_link_status(int fd, char *device, rtnl_callback cb, void *ctx)
8cf965
 	return 0;
8cf965
 }
8cf965
 
8cf965
-int rtnl_open(void)
8cf965
+static int genl_send_msg(int fd, int family_id, int genl_cmd, int genl_version,
8cf965
+		  int rta_type, void *rta_data, int rta_len)
8cf965
 {
8cf965
-	int fd;
8cf965
-	struct sockaddr_nl sa;
8cf965
+	struct sockaddr_nl daddr;
8cf965
+	struct genlmsghdr *gnlh;
8cf965
+	struct nlmsghdr *nlh;
8cf965
+	struct rtattr *attr;
8cf965
+	char msg[BUF_SIZE];
8cf965
 
8cf965
-	memset(&sa, 0, sizeof(sa));
8cf965
-	sa.nl_family = AF_NETLINK;
8cf965
-	sa.nl_groups = RTNLGRP_LINK;
8cf965
+	memset(&daddr, 0, sizeof(daddr));
8cf965
+	daddr.nl_family = AF_NETLINK;
8cf965
 
8cf965
-	fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
8cf965
-	if (fd < 0) {
8cf965
-		pr_err("failed to open netlink socket: %m");
8cf965
+	memset(&msg, 0, sizeof(msg));
8cf965
+	nlh = (struct nlmsghdr *) msg;
8cf965
+	nlh->nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
8cf965
+	nlh->nlmsg_type = family_id;
8cf965
+	nlh->nlmsg_flags = NLM_F_REQUEST;
8cf965
+
8cf965
+	gnlh = (struct genlmsghdr *) NLMSG_DATA(nlh);
8cf965
+	gnlh->cmd = genl_cmd;
8cf965
+	gnlh->version = genl_version;
8cf965
+
8cf965
+	if (rta_data && rta_len > 0) {
8cf965
+		attr = (struct rtattr *) GENLMSG_DATA(msg);
8cf965
+		attr->rta_type = rta_type;
8cf965
+		attr->rta_len = RTA_LENGTH(rta_len);
8cf965
+		nlh->nlmsg_len += NLMSG_ALIGN(attr->rta_len);
8cf965
+		if (nlh->nlmsg_len < sizeof(msg))
8cf965
+			memcpy(RTA_DATA(attr), rta_data, rta_len);
8cf965
+		else
8cf965
+			return -1;
8cf965
+	}
8cf965
+
8cf965
+	return sendto(fd, &msg, nlh->nlmsg_len, 0,
8cf965
+		      (struct sockaddr *)&daddr, sizeof(daddr));
8cf965
+}
8cf965
+
8cf965
+static int genl_get_family_id(int fd, void *family_name)
8cf965
+{
8cf965
+	struct rtattr *tb[CTRL_ATTR_MAX+1];
8cf965
+	struct nlmsghdr *nlh;
8cf965
+	struct rtattr *attr;
8cf965
+	char msg[BUF_SIZE];
8cf965
+	int len, gf_id;
8cf965
+
8cf965
+	len = genl_send_msg(fd, GENL_ID_CTRL, CTRL_CMD_GETFAMILY, 1,
8cf965
+			    CTRL_ATTR_FAMILY_NAME, family_name,
8cf965
+			    strlen(family_name) + 1);
8cf965
+	if (len < 0)
8cf965
+		return len;
8cf965
+
8cf965
+	len = recv(fd, &msg, sizeof(msg), 0);
8cf965
+	if (len < 0)
8cf965
+		return len;
8cf965
+
8cf965
+	nlh = (struct nlmsghdr *) msg;
8cf965
+	if (nlh->nlmsg_type == NLMSG_ERROR || !NLMSG_OK(nlh, len))
8cf965
 		return -1;
8cf965
+
8cf965
+	attr = (struct rtattr *) GENLMSG_DATA(msg);
8cf965
+	rtnl_rtattr_parse(tb, CTRL_ATTR_MAX, attr, NLMSG_PAYLOAD(nlh, GENL_HDRLEN));
8cf965
+
8cf965
+	if (tb[CTRL_ATTR_FAMILY_ID])
8cf965
+		gf_id = rta_getattr_u16(tb[CTRL_ATTR_FAMILY_ID]);
8cf965
+	else
8cf965
+		gf_id = -1;
8cf965
+
8cf965
+	return gf_id;
8cf965
+}
8cf965
+
8cf965
+static int parase_team_list_option(struct rtattr *attr)
8cf965
+{
8cf965
+	struct rtattr *tb[TEAM_ATTR_OPTION_MAX+1];
8cf965
+	int len = RTA_PAYLOAD(attr);
8cf965
+	const char *optname = "";
8cf965
+	const char *mode = "";
8cf965
+	int active_index = -1;
8cf965
+
8cf965
+	for (attr = RTA_DATA(attr); RTA_OK(attr, len); attr = RTA_NEXT(attr, len)) {
8cf965
+		rtnl_nested_rtattr_parse(tb, TEAM_ATTR_OPTION_MAX, attr);
8cf965
+
8cf965
+		if (tb[TEAM_ATTR_OPTION_NAME])
8cf965
+			optname = rta_getattr_str(tb[TEAM_ATTR_OPTION_NAME]);
8cf965
+
8cf965
+		if (!strcmp(optname, "mode") && tb[TEAM_ATTR_OPTION_TYPE] &&
8cf965
+		    rta_getattr_u8(tb[TEAM_ATTR_OPTION_TYPE]) == NLA_STRING)
8cf965
+			mode = rta_getattr_str(tb[TEAM_ATTR_OPTION_DATA]);
8cf965
+
8cf965
+		if (!strcmp(optname, "activeport") && tb[TEAM_ATTR_OPTION_TYPE] &&
8cf965
+		    rta_getattr_u8(tb[TEAM_ATTR_OPTION_TYPE]) == NLA_U32)
8cf965
+			active_index = rta_getattr_u32(tb[TEAM_ATTR_OPTION_DATA]);
8cf965
 	}
8cf965
-	if (bind(fd, (struct sockaddr *) &sa, sizeof(sa))) {
8cf965
-		pr_err("failed to bind netlink socket: %m");
8cf965
-		close(fd);
8cf965
+
8cf965
+	if (strcmp(mode, "activebackup")) {
8cf965
+		pr_err("team supported only in activebackup mode");
8cf965
 		return -1;
8cf965
+	} else {
8cf965
+		return active_index;
8cf965
 	}
8cf965
-	return fd;
8cf965
+}
8cf965
+
8cf965
+static int get_team_active_iface(int master_index)
8cf965
+{
8cf965
+	struct rtattr *tb[TEAM_ATTR_MAX+1];
8cf965
+	struct genlmsghdr *gnlh;
8cf965
+	struct nlmsghdr *nlh;
8cf965
+	char msg[BUF_SIZE];
8cf965
+	int fd, gf_id, len;
8cf965
+	int index = -1;
8cf965
+
8cf965
+	fd = nl_open(NETLINK_GENERIC);
8cf965
+	if (fd < 0)
8cf965
+		return fd;
8cf965
+
8cf965
+	gf_id = genl_get_family_id(fd, TEAM_GENL_NAME);
8cf965
+	if (gf_id < 0) {
8cf965
+		pr_err("get genl family failed");
8cf965
+		goto no_info;
8cf965
+	}
8cf965
+
8cf965
+	len = genl_send_msg(fd, gf_id, TEAM_CMD_OPTIONS_GET,
8cf965
+			    TEAM_GENL_VERSION, TEAM_ATTR_TEAM_IFINDEX,
8cf965
+			    &master_index, sizeof(master_index));
8cf965
+	if (len < 0) {
8cf965
+		pr_err("send team info request failed: %m");
8cf965
+		goto no_info;
8cf965
+	}
8cf965
+
8cf965
+	len = recv(fd, msg, sizeof(msg), 0);
8cf965
+	if (len < 0) {
8cf965
+		pr_err("recv team info failed: %m");
8cf965
+		goto no_info;
8cf965
+	}
8cf965
+
8cf965
+	nlh = (struct nlmsghdr *) msg;
8cf965
+	for ( ; NLMSG_OK(nlh, len); nlh = NLMSG_NEXT(nlh, len)) {
8cf965
+		if (nlh->nlmsg_type != gf_id)
8cf965
+			continue;
8cf965
+
8cf965
+		gnlh = (struct genlmsghdr *) NLMSG_DATA(nlh);
8cf965
+		if (gnlh->cmd != TEAM_CMD_OPTIONS_GET)
8cf965
+			continue;
8cf965
+
8cf965
+		rtnl_rtattr_parse(tb, TEAM_ATTR_MAX, (struct rtattr *)GENLMSG_DATA(msg),
8cf965
+				  NLMSG_PAYLOAD(nlh, GENL_HDRLEN));
8cf965
+
8cf965
+		if (tb[TEAM_ATTR_TEAM_IFINDEX] &&
8cf965
+		    master_index != rta_getattr_u32(tb[TEAM_ATTR_TEAM_IFINDEX]))
8cf965
+			continue;
8cf965
+
8cf965
+		if (tb[TEAM_ATTR_LIST_OPTION]) {
8cf965
+			index = parase_team_list_option(tb[TEAM_ATTR_LIST_OPTION]);
8cf965
+			break;
8cf965
+		}
8cf965
+	}
8cf965
+
8cf965
+	return index;
8cf965
+no_info:
8cf965
+	nl_close(fd);
8cf965
+	return -1;
8cf965
 }
8cf965
commit d8eafa6a39a609597e6c1ce44d616ba6f33c27f8
8cf965
Author: Hangbin Liu <liuhangbin@gmail.com>
8cf965
Date:   Wed Mar 20 14:44:13 2019 +0800
8cf965
8cf965
    port: should check the new phc_index before switching
8cf965
    
8cf965
    In logic, when we want to switch phc, we should check if the new phc
8cf965
    index is valid instead of checking the previous one.
8cf965
    
8cf965
    In reality, if we use linux team interface with activebackup mode. As
8cf965
    teamd is a userspace tool, it sets the new slave as active port after
8cf965
    receiving link change message. If we set current active port down and
8cf965
    another slave up. There is a race that we receive the new slave's link
8cf965
    up message while active port(ts_index) is still the old one. This means
8cf965
    we may use a link down interface as ts_index and get phc_index with -1.
8cf965
    
8cf965
    If we update the p->phc_index to -1, there will be no possibility to
8cf965
    change it back to other value as we swith phc only when p->phc_index >= 0.
8cf965
    
8cf965
    With this fix, we will not switch phc_index until receiving the real
8cf965
    active port(p->iface->ts_info.phc_index >= 0) update message.
8cf965
    
8cf965
    Reported-by: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
    Fixes: 536a71031d5c ("ptp4l: use ts label to get ts info")
8cf965
    Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
8cf965
8cf965
diff --git a/port.c b/port.c
8cf965
index ad9554f..49c6f76 100644
8cf965
--- a/port.c
8cf965
+++ b/port.c
8cf965
@@ -2442,7 +2442,7 @@ void port_link_status(void *ctx, int linkup, int ts_index)
8cf965
 		sk_get_ts_info(p->iface->ts_label, &p->iface->ts_info);
8cf965
 
8cf965
 		/* Only switch phc with HW time stamping mode */
8cf965
-		if (p->phc_index >= 0 && p->iface->ts_info.valid) {
8cf965
+		if (p->iface->ts_info.valid && p->iface->ts_info.phc_index >= 0) {
8cf965
 			required_modes = clock_required_modes(p->clock);
8cf965
 			if ((p->iface->ts_info.so_timestamping & required_modes) != required_modes) {
8cf965
 				pr_err("interface '%s' does not support requested "
8cf965
commit ecfbea2dac2312cb61aa5d2fe1646ee4c6b18643
8cf965
Author: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
Date:   Thu Mar 21 15:44:32 2019 +0100
8cf965
8cf965
    rtnl: close file descriptor in get_team_active_iface()
8cf965
    
8cf965
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
8cf965
diff --git a/rtnl.c b/rtnl.c
8cf965
index e3758b3..59ed0ec 100644
8cf965
--- a/rtnl.c
8cf965
+++ b/rtnl.c
8cf965
@@ -461,8 +461,7 @@ static int get_team_active_iface(int master_index)
8cf965
 		}
8cf965
 	}
8cf965
 
8cf965
-	return index;
8cf965
 no_info:
8cf965
 	nl_close(fd);
8cf965
-	return -1;
8cf965
+	return index;
8cf965
 }