mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-08-13 15:47:07 +00:00
network: introduce a interface named lxc_netdev_isup().
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. Changelog: 10/15/2015: Return failure if name==NULL to avoid later strlen fun Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com> Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
parent
8d357196c4
commit
efa1cf45a2
@ -305,6 +305,86 @@ 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;
|
||||
|
||||
if (!name)
|
||||
return -EINVAL;
|
||||
|
||||
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;
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user