util: fallback to ioctl(SIOCBRDELBR) if netlink RTM_DELLINK fails

commit 09778e09 switched from using ioctl(SIOCBRDELBR) for bridge
device deletion to using a netlink RTM_DELLINK message, which is the
more modern way to delete a bridge (and also doesn't require the
bridge to be ~IFF_UP to succeed). However, although older kernels
(e.g. 2.6.32, in RHEL6/CentOS6) support deleting *some* link types
with RTM_NEWLINK, they don't support deleting bridges, and there is no
compile-time way to figure this out.

This patch moves the body of the SIOCBRDELBR version of
virNetDevBridgeDelete() into a static function, calls the new function
from the original, and also calls the new function from the
RTM_DELLINK version if the RTM_DELLINK message generates an EOPNOTSUPP
error. Since RTM_DELLINK is done from the subordinate function
virNetlinkDelLink, which is also called for other purposes (deleting a
macvtap interface), a function pointer called "fallback" has been
added to the arglist of virNetlinkDelLink() - if that arg != NULL, the
provided function will be called when (and only when) RTM_DELLINK
fails with EOPNOTSUPP.

Resolves:  https://bugzilla.redhat.com/show_bug.cgi?id=1252780 (part 2)
This commit is contained in:
Laine Stump 2015-08-25 23:19:03 -04:00
parent 66dcb40937
commit 97d26e470d
4 changed files with 50 additions and 15 deletions

View File

@ -558,20 +558,15 @@ int virNetDevBridgeCreate(const char *brname)
* *
* Returns 0 in case of success or an errno code in case of failure. * Returns 0 in case of success or an errno code in case of failure.
*/ */
#if defined(__linux__) && defined(HAVE_LIBNL) #if defined(HAVE_STRUCT_IFREQ) && defined(SIOCBRDELBR)
int virNetDevBridgeDelete(const char *brname) static int
{ virNetDevBridgeDeleteWithIoctl(const char *brname)
/* If netlink is available, use it, as it is successful at
* deleting a bridge even if it is currently IFF_UP.
*/
return virNetlinkDelLink(brname);
}
#elif defined(HAVE_STRUCT_IFREQ) && defined(SIOCBRDELBR)
int virNetDevBridgeDelete(const char *brname)
{ {
int fd = -1; int fd = -1;
int ret = -1; int ret = -1;
ignore_value(virNetDevSetOnline(brname, false));
if ((fd = virNetDevSetupControl(NULL, NULL)) < 0) if ((fd = virNetDevSetupControl(NULL, NULL)) < 0)
return -1; return -1;
@ -587,8 +582,36 @@ int virNetDevBridgeDelete(const char *brname)
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
return ret; return ret;
} }
#endif
#if defined(__linux__) && defined(HAVE_LIBNL)
int
virNetDevBridgeDelete(const char *brname)
{
/* If netlink is available, use it, as it is successful at
* deleting a bridge even if it is currently IFF_UP. fallback to
* using ioctl(SIOCBRDELBR) if netlink fails with EOPNOTSUPP.
*/
# if defined(HAVE_STRUCT_IFREQ) && defined(SIOCBRDELBR)
return virNetlinkDelLink(brname, virNetDevBridgeDeleteWithIoctl);
# else
return virNetlinkDelLink(brname, NULL);
# endif
}
#elif defined(HAVE_STRUCT_IFREQ) && defined(SIOCBRDELBR)
int
virNetDevBridgeDelete(const char *brname)
{
return virNetDevBridgeDeleteWithIoctl(brname);
}
#elif defined(HAVE_STRUCT_IFREQ) && defined(SIOCIFDESTROY) #elif defined(HAVE_STRUCT_IFREQ) && defined(SIOCIFDESTROY)
int virNetDevBridgeDelete(const char *brname) int
virNetDevBridgeDelete(const char *brname)
{ {
int s; int s;
struct ifreq ifr; struct ifreq ifr;

View File

@ -220,7 +220,7 @@ virNetDevMacVLanCreate(const char *ifname,
*/ */
int virNetDevMacVLanDelete(const char *ifname) int virNetDevMacVLanDelete(const char *ifname)
{ {
return virNetlinkDelLink(ifname); return virNetlinkDelLink(ifname, NULL);
} }

View File

@ -281,6 +281,10 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
* virNetlinkDelLink: * virNetlinkDelLink:
* *
* @ifname: Name of the link * @ifname: Name of the link
* @fallback: pointer to an alternate function that will
* be called to perform the delete if RTM_DELLINK fails
* with EOPNOTSUPP (any other error will simply be treated
* as an error).
* *
* delete a network "link" (aka interface aka device) with the given * delete a network "link" (aka interface aka device) with the given
* name. This works for many different types of network devices, * name. This works for many different types of network devices,
@ -289,7 +293,7 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
* Returns 0 on success, -1 on fatal error. * Returns 0 on success, -1 on fatal error.
*/ */
int int
virNetlinkDelLink(const char *ifname) virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
{ {
int rc = -1; int rc = -1;
struct nlmsghdr *resp = NULL; struct nlmsghdr *resp = NULL;
@ -325,6 +329,10 @@ virNetlinkDelLink(const char *ifname)
if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
goto malformed_resp; goto malformed_resp;
if (-err->error == EOPNOTSUPP && fallback) {
rc = fallback(ifname);
goto cleanup;
}
if (err->error) { if (err->error) {
virReportSystemError(-err->error, virReportSystemError(-err->error,
_("error destroying network device %s"), _("error destroying network device %s"),
@ -886,7 +894,8 @@ int virNetlinkCommand(struct nl_msg *nl_msg ATTRIBUTE_UNUSED,
int int
virNetlinkDelLink(const char *ifname ATTRIBUTE_UNUSED) virNetlinkDelLink(const char *ifname ATTRIBUTE_UNUSED,
virNetlinkDelLinkFallback fallback ATTRIBUTE_UNUSED)
{ {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported)); virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return -1; return -1;

View File

@ -51,7 +51,10 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
struct nlmsghdr **resp, unsigned int *respbuflen, struct nlmsghdr **resp, unsigned int *respbuflen,
uint32_t src_pid, uint32_t dst_pid, uint32_t src_pid, uint32_t dst_pid,
unsigned int protocol, unsigned int groups); unsigned int protocol, unsigned int groups);
int virNetlinkDelLink(const char *ifname);
typedef int (*virNetlinkDelLinkFallback)(const char *ifname);
int virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback);
int virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen); int virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen);