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