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

Serge Hallyn serge.hallyn at ubuntu.com
Tue Oct 14 12:34:58 UTC 2014


Quoting Dongsheng Yang (yangds.fnst at cn.fujitsu.com):
> 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>
> Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>

Sorry, amendment below:

> ---
>  src/lxc/network.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  src/lxc/network.h |  2 ++
>  2 files changed, 79 insertions(+)
> 
> diff --git a/src/lxc/network.c b/src/lxc/network.c
> index 4c577b1..72adcaa 100644
> --- a/src/lxc/network.c
> +++ b/src/lxc/network.c
> @@ -305,6 +305,83 @@ 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;
> +

Please return an error here if name is NULL.  else strlen will
trip.

> +	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;
> +}
> +
> +/*
> + * \brief Check a interface is up or not.
> + *
> + * \param name: name for the interface.
> + *
> + * \return int.
> + * 0 means interface is down.
> + * 1 means interface is up.
> + * Others means error happened, and ret-value is the error number.
> + */
> +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 1;
> +	return 0;
> +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