[lxc-devel] [PATCH 5/9] network: introduce a interface named lxc_netdev_isup().

Dongsheng Yang yangds.fnst at cn.fujitsu.com
Tue Sep 16 11:15:29 UTC 2014


When we need to know some info about a netdev, such as is_up or not,
we need to read the flag for the netdev.

This patch introduce a interface function named lxc_netdev_isup()
to check is a netdev up or down.

And introduce a network private function named netdev_get_flag()
to get flag for netdev by netlink.

Signed-off-by: Dongsheng Yang <yangds.fnst at cn.fujitsu.com>
---
 src/lxc/network.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lxc/network.h |  2 ++
 2 files changed, 69 insertions(+)

diff --git a/src/lxc/network.c b/src/lxc/network.c
index 4c577b1..e83b786 100644
--- a/src/lxc/network.c
+++ b/src/lxc/network.c
@@ -305,6 +305,73 @@ out:
 	return err;
 }
 
+int netdev_get_flag(const char* name, int *flag)
+{
+	struct nl_handler nlh;
+	struct nlmsg *nlmsg = NULL, *answer = NULL;
+	struct link_req *link_req;
+	int index, len, err;
+	struct ifinfomsg *ifi;
+
+	err = netlink_open(&nlh, NETLINK_ROUTE);
+	if (err)
+		return err;
+
+	err = -EINVAL;
+	len = strlen(name);
+	if (len == 1 || len >= IFNAMSIZ)
+		goto out;
+
+	err = -ENOMEM;
+	nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
+	if (!nlmsg)
+		goto out;
+
+	answer = nlmsg_alloc(NLMSG_GOOD_SIZE);
+	if (!answer)
+		goto out;
+
+	err = -EINVAL;
+	index = if_nametoindex(name);
+	if (!index)
+		goto out;
+
+	link_req = (struct link_req *)nlmsg;
+	link_req->ifinfomsg.ifi_family = AF_UNSPEC;
+	link_req->ifinfomsg.ifi_index = index;
+	nlmsg->nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
+	nlmsg->nlmsghdr.nlmsg_flags = NLM_F_REQUEST;
+	nlmsg->nlmsghdr.nlmsg_type = RTM_GETLINK;
+
+	err = netlink_transaction(&nlh, nlmsg, answer);
+	if (err)
+		goto out;
+
+	ifi = NLMSG_DATA(answer);
+
+	*flag = ifi->ifi_flags;
+out:
+	netlink_close(&nlh);
+	nlmsg_free(nlmsg);
+	nlmsg_free(answer);
+	return err;
+}
+
+int lxc_netdev_isup(const char* name)
+{
+	int flag;
+	int err;
+
+	err = netdev_get_flag(name, &flag);
+	if (err)
+		goto out;
+	if (flag & IFF_UP)
+		return true;
+	return false;
+out:
+	return err;
+}
+
 int netdev_get_mtu(int ifindex)
 {
 	struct nl_handler nlh;
diff --git a/src/lxc/network.h b/src/lxc/network.h
index 768489d..cd42c8f 100644
--- a/src/lxc/network.h
+++ b/src/lxc/network.h
@@ -51,6 +51,8 @@ extern int netdev_set_flag(const char *name, int flag);
 /*
  * Set the device network up or down
  */
+
+extern int lxc_netdev_isup(const char *name);
 extern int lxc_netdev_up(const char *name);
 extern int lxc_netdev_down(const char *name);
 
-- 
1.8.4.2



More information about the lxc-devel mailing list