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