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