}
if (brAddInetAddress(network->def->bridge,
- &ipdef->address, prefix) < 0) {
- networkReportError(VIR_ERR_INTERNAL_ERROR,
- _("cannot set IP address on bridge '%s'"),
- network->def->bridge);
+ &ipdef->address, prefix) < 0)
return -1;
- }
return 0;
}
networkStartNetworkVirtual(struct network_driver *driver,
virNetworkObjPtr network)
{
- int ii, err;
+ int ii;
bool v4present = false, v6present = false;
virErrorPtr save_err = NULL;
virNetworkIpDefPtr ipdef;
return -1;
/* Create and configure the bridge device */
- if ((err = brAddBridge(network->def->bridge))) {
- virReportSystemError(err,
- _("cannot create bridge '%s'"),
- network->def->bridge);
+ if (brAddBridge(network->def->bridge) < 0)
return -1;
- }
if (network->def->mac_specified) {
/* To set a mac for the bridge, we need to define a dummy tap
virReportOOMError();
goto err0;
}
- if ((err = brAddTap(network->def->bridge,
- &macTapIfName, network->def->mac, 0, false, NULL))) {
- virReportSystemError(err,
- _("cannot create dummy tap device '%s' to set mac"
- " address on bridge '%s'"),
- macTapIfName, network->def->bridge);
+ if (brAddTap(network->def->bridge,
+ &macTapIfName, network->def->mac, 0, false, NULL) < 0) {
VIR_FREE(macTapIfName);
goto err0;
}
/* Set bridge options */
if (brSetForwardDelay(network->def->bridge,
- network->def->delay)) {
- networkReportError(VIR_ERR_INTERNAL_ERROR,
- _("cannot set forward delay on bridge '%s'"),
- network->def->bridge);
+ network->def->delay) < 0)
goto err1;
- }
if (brSetEnableSTP(network->def->bridge,
- network->def->stp ? 1 : 0)) {
- networkReportError(VIR_ERR_INTERNAL_ERROR,
- _("cannot set STP '%s' on bridge '%s'"),
- network->def->stp ? "on" : "off", network->def->bridge);
+ network->def->stp ? 1 : 0) < 0)
goto err1;
- }
/* Disable IPv6 on the bridge if there are no IPv6 addresses
* defined, and set other IPv6 sysctl tunables appropriately.
}
/* Bring up the bridge interface */
- if ((err = brSetInterfaceUp(network->def->bridge, 1))) {
- virReportSystemError(err,
- _("failed to bring the bridge '%s' up"),
- network->def->bridge);
+ if (brSetInterfaceUp(network->def->bridge, 1) < 0)
goto err2;
- }
/* If forwardType != NONE, turn on global IP forwarding */
if (network->def->forwardType != VIR_NETWORK_FORWARD_NONE &&
err3:
if (!save_err)
save_err = virSaveLastError();
- if ((err = brSetInterfaceUp(network->def->bridge, 0))) {
- char ebuf[1024];
- VIR_WARN("Failed to bring down bridge '%s' : %s",
- network->def->bridge, virStrerror(err, ebuf, sizeof ebuf));
- }
+ ignore_value(brSetInterfaceUp(network->def->bridge, 0));
err2:
if (!save_err)
if (!save_err)
save_err = virSaveLastError();
- if ((err = brDeleteTap(macTapIfName))) {
- char ebuf[1024];
- VIR_WARN("Failed to delete dummy tap device '%s' on bridge '%s' : %s",
- macTapIfName, network->def->bridge,
- virStrerror(err, ebuf, sizeof ebuf));
- }
+ ignore_value(brDeleteTap(macTapIfName));
VIR_FREE(macTapIfName);
err0:
if (!save_err)
save_err = virSaveLastError();
- if ((err = brDeleteBridge(network->def->bridge))) {
- char ebuf[1024];
- VIR_WARN("Failed to delete bridge '%s' : %s",
- network->def->bridge, virStrerror(err, ebuf, sizeof ebuf));
- }
+ ignore_value(brDeleteBridge(network->def->bridge));
if (save_err) {
virSetError(save_err);
static int networkShutdownNetworkVirtual(struct network_driver *driver,
virNetworkObjPtr network)
{
- int err;
- char ebuf[1024];
-
if (virBandwidthDisable(network->def->bridge, true) < 0) {
VIR_WARN("Failed to disable QoS on %s",
network->def->name);
if (!macTapIfName) {
virReportOOMError();
} else {
- if ((err = brDeleteTap(macTapIfName))) {
- VIR_WARN("Failed to delete dummy tap device '%s' on bridge '%s' : %s",
- macTapIfName, network->def->bridge,
- virStrerror(err, ebuf, sizeof ebuf));
- }
+ ignore_value(brDeleteTap(macTapIfName));
VIR_FREE(macTapIfName);
}
}
- if ((err = brSetInterfaceUp(network->def->bridge, 0))) {
- VIR_WARN("Failed to bring down bridge '%s' : %s",
- network->def->bridge, virStrerror(err, ebuf, sizeof ebuf));
- }
+ ignore_value(brSetInterfaceUp(network->def->bridge, 0));
networkRemoveIptablesRules(driver, network);
- if ((err = brDeleteBridge(network->def->bridge))) {
- VIR_WARN("Failed to delete bridge '%s' : %s",
- network->def->bridge, virStrerror(err, ebuf, sizeof ebuf));
- }
+ ignore_value(brDeleteBridge(network->def->bridge));
/* See if its still alive and really really kill it */
if (network->dnsmasqPid > 0 &&
# include "util.h"
# include "logging.h"
# include "network.h"
+# include "virterror_internal.h"
# define JIFFIES_TO_MS(j) (((j)*1000)/HZ)
# define MS_TO_JIFFIES(ms) (((ms)*HZ)/1000)
+# define VIR_FROM_THIS VIR_FROM_NONE
static int brSetupControlFull(const char *ifname,
struct ifreq *ifr,
memset(ifr, 0, sizeof(*ifr));
if (virStrcpyStatic(ifr->ifr_name, ifname) == NULL) {
- errno = EINVAL;
+ virReportSystemError(ERANGE,
+ _("Network interface name '%s' is too long"),
+ ifname);
return -1;
}
}
- if ((fd = socket(domain, type, 0)) < 0)
+ if ((fd = socket(domain, type, 0)) < 0) {
+ virReportSystemError(errno, "%s",
+ _("Cannot open network interface control socket"));
return -1;
+ }
if (virSetInherit(fd, false) < 0) {
+ virReportSystemError(errno, "%s",
+ _("Cannot set close-on-exec flag for socket"));
VIR_FORCE_CLOSE(fd);
return -1;
}
*
* This function register a new bridge
*
- * Returns 0 in case of success or an errno code in case of failure.
+ * Returns 0 in case of success or -1 on failure
*/
# ifdef SIOCBRADDBR
int
int ret = -1;
if ((fd = brSetupControl(NULL, NULL)) < 0)
- return errno;
+ return -1;
if (ioctl(fd, SIOCBRADDBR, brname) < 0) {
- ret = errno;
+ virReportSystemError(errno,
+ _("Unable to create bridge %s"), brname);
goto cleanup;
}
return ret;
}
# else
-int brAddBridge (const char *brname ATTRIBUTE_UNUSED)
+int brAddBridge(const char *brname)
{
- return EINVAL;
+ virReportSystemError(ENOSYS,
+ _("Unable to create bridge %s"), brname);
+ return -1;
}
# endif
# ifdef SIOCBRDELBR
+/**
+ * brHasBridge:
+ * @brname
+ *
+ * Check if the bridge @brname exists
+ *
+ * Returns 1 if it exists, 0 if it does not, -1 on error
+ */
int
brHasBridge(const char *brname)
{
struct ifreq ifr;
if ((fd = brSetupControl(brname, &ifr)) < 0)
- return errno;
+ return -1;
- if (ioctl(fd, SIOCGIFFLAGS, &ifr))
+ if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
+ if (errno == ENODEV)
+ ret = 0;
+ else
+ virReportSystemError(errno,
+ _("Unable to get bridge %s flags"), brname);
goto cleanup;
+ }
- ret = 0;
+ ret = 1;
cleanup:
VIR_FORCE_CLOSE(fd);
}
# else
int
-brHasBridge(const char *brname ATTRIBUTE_UNUSED)
+brHasBridge(const char *brname)
{
- return EINVAL;
+ virReportSystemError(ENOSYS,
+ _("Unable to check bridge %s"), brname);
+ return -1;
}
# endif
int ret = -1;
if ((fd = brSetupControl(NULL, NULL)) < 0)
- return errno;
+ return -1;
if (ioctl(fd, SIOCBRDELBR, brname) < 0) {
- ret = errno;
+ virReportSystemError(errno,
+ _("Unable to delete bridge %s"), brname);
goto cleanup;
}
int
brDeleteBridge(const char *brname ATTRIBUTE_UNUSED)
{
- return EINVAL;
+ virReportSystemError(ENOSYS,
+ _("Unable to delete bridge %s"), brname);
+ return -1;
}
# endif
struct ifreq ifr;
if ((fd = brSetupControl(brname, &ifr)) < 0)
- return errno;
+ return -1;
if (!(ifr.ifr_ifindex = if_nametoindex(ifname))) {
- ret = errno;
+ virReportSystemError(ENODEV,
+ _("Unable to get interface index for %s"), ifname);
goto cleanup;
}
if (ioctl(fd, SIOCBRADDIF, &ifr) < 0) {
- ret = errno;
+ virReportSystemError(errno,
+ _("Unable to add bridge %s port %s"), brname, ifname);
goto cleanup;
}
}
# else
int
-brAddInterface(const char *brname ATTRIBUTE_UNUSED,
- const char *ifname ATTRIBUTE_UNUSED)
+brAddInterface(const char *brname,
+ const char *ifname)
{
- return EINVAL;
+ virReportSystemError(ENOSYS,
+ _("Unable to add bridge %s port %s"), brname, ifname);
+ return -1;
}
# endif
struct ifreq ifr;
if ((fd = brSetupControl(brname, &ifr)) < 0)
- return errno;
+ return -1;
if (!(ifr.ifr_ifindex = if_nametoindex(ifname))) {
- ret = errno;
+ virReportSystemError(ENODEV,
+ _("Unable to get interface index for %s"), ifname);
+
goto cleanup;
}
if (ioctl(fd, SIOCBRDELIF, &ifr) < 0) {
- ret = errno;
+ virReportSystemError(errno,
+ _("Unable to remove bridge %s port %s"), brname, ifname);
goto cleanup;
}
}
# else
int
-brDeleteInterface(const char *brname ATTRIBUTE_UNUSED,
- const char *ifname ATTRIBUTE_UNUSED)
+brDeleteInterface(const char *brname,
+ const char *ifname)
{
- return EINVAL;
+ virReportSystemError(errno,
+ _("Unable to remove bridge %s port %s"), brname, ifname);
+ return -1;
}
# endif
* This function sets the @macaddr for a given interface @ifname. This
* gets rid of the kernel's automatically assigned random MAC.
*
- * Returns 0 in case of success or an errno code in case of failure.
+ * Returns 0 in case of success or -1 on failure
*/
int
brSetInterfaceMac(const char *ifname,
struct ifreq ifr;
if ((fd = brSetupControl(ifname, &ifr)) < 0)
- return errno;
+ return -1;
/* To fill ifr.ifr_hdaddr.sa_family field */
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
- ret = errno;
+ virReportSystemError(errno,
+ _("Cannot get interface MAC on '%s'"),
+ ifname);
goto cleanup;
}
memcpy(ifr.ifr_hwaddr.sa_data, macaddr, VIR_MAC_BUFLEN);
if (ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) {
- ret = errno;
+ virReportSystemError(errno,
+ _("Cannot set interface MAC on '%s'"),
+ ifname);
goto cleanup;
}
*
* This function gets the @mtu value set for a given interface @ifname.
*
- * Returns the MTU value in case of success.
- * On error, returns -1 and sets errno accordingly
+ * Returns the MTU value in case of success, or -1 on failure.
*/
static int ifGetMtu(const char *ifname)
{
if ((fd = brSetupControl(ifname, &ifr)) < 0)
return -1;
- if (ioctl(fd, SIOCGIFMTU, &ifr) < 0)
+ if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
+ virReportSystemError(errno,
+ _("Cannot get interface MTU on '%s'"),
+ ifname);
goto cleanup;
+ }
ret = ifr.ifr_mtu;
* This function sets the @mtu for a given interface @ifname. Typically
* used on a tap device to set up for Jumbo Frames.
*
- * Returns 0 in case of success or an errno code in case of failure.
+ * Returns 0 in case of success, or -1 on failure
*/
static int ifSetMtu(const char *ifname, int mtu)
{
struct ifreq ifr;
if ((fd = brSetupControl(ifname, &ifr)) < 0)
- return errno;
+ return -1;
ifr.ifr_mtu = mtu;
if (ioctl(fd, SIOCSIFMTU, &ifr) < 0) {
- ret = errno;
+ virReportSystemError(errno,
+ _("Cannot set interface MTU on '%s'"),
+ ifname);
goto cleanup;
}
*
* Sets the interface mtu to the same MTU of the bridge
*
- * Returns 0 in case of success or an errno code in case of failure.
+ * Returns 0 in case of success, or -1 on failure
*/
static int brSetInterfaceMtu(const char *brname,
const char *ifname)
int mtu = ifGetMtu(brname);
if (mtu < 0)
- return errno;
+ return -1;
return ifSetMtu(ifname, mtu);
}
* kernel implements the TUNGETIFF ioctl(), which qemu needs to query
* the supplied tapfd.
*
- * Returns 0 in case of success or an errno code in case of failure.
+ * Returns 1 if VnetHdr is supported, 0 if not supported
*/
# ifdef IFF_VNET_HDR
static int
* persistent and closed. The caller must use brDeleteTap to remove
* a persistent TAP devices when it is no longer needed.
*
- * Returns 0 in case of success or an errno code in case of failure.
+ * Returns 0 in case of success or -1 on failure
*/
int
brAddTap(const char *brname,
bool up,
int *tapfd)
{
- errno = brCreateTap(ifname, vnet_hdr, tapfd);
-
- /* fd has been closed in brCreateTap() when it failed. */
- if (errno)
- goto error;
+ if (brCreateTap(ifname, vnet_hdr, tapfd) < 0)
+ return -1;
/* We need to set the interface MAC before adding it
* to the bridge, because the bridge assumes the lowest
* seeing the kernel allocate random MAC for the TAP
* device before we set our static MAC.
*/
- if ((errno = brSetInterfaceMac(*ifname, macaddr)))
- goto close_fd;
+ if (brSetInterfaceMac(*ifname, macaddr) < 0)
+ goto error;
+
/* We need to set the interface MTU before adding it
* to the bridge, because the bridge will have its
* MTU adjusted automatically when we add the new interface.
*/
- if ((errno = brSetInterfaceMtu(brname, *ifname)))
- goto close_fd;
- if ((errno = brAddInterface(brname, *ifname)))
- goto close_fd;
- if (up && ((errno = brSetInterfaceUp(*ifname, 1))))
- goto close_fd;
+ if (brSetInterfaceMtu(brname, *ifname) < 0)
+ goto error;
+
+ if (brAddInterface(brname, *ifname) < 0)
+ goto error;
+
+ if (brSetInterfaceUp(*ifname, up) < 0)
+ goto error;
return 0;
-close_fd:
+error:
if (tapfd)
VIR_FORCE_CLOSE(*tapfd);
-error:
- return errno;
+ return -1;
}
int brDeleteTap(const char *ifname)
{
struct ifreq try;
int fd;
+ int ret = -1;
- if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
- return errno;
+ if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
+ virReportSystemError(errno, "%s",
+ _("Unable to open /dev/net/tun, is tun module loaded?"));
+ return -1;
+ }
memset(&try, 0, sizeof(struct ifreq));
try.ifr_flags = IFF_TAP|IFF_NO_PI;
if (virStrcpyStatic(try.ifr_name, ifname) == NULL) {
- errno = EINVAL;
- goto error;
+ virReportSystemError(ERANGE,
+ _("Network interface name '%s' is too long"),
+ ifname);
+ goto cleanup;
}
- if (ioctl(fd, TUNSETIFF, &try) == 0) {
- if ((errno = ioctl(fd, TUNSETPERSIST, 0)))
- goto error;
+ if (ioctl(fd, TUNSETIFF, &try) < 0) {
+ virReportSystemError(errno, "%s",
+ _("Unable to associate TAP device"));
+ goto cleanup;
}
- error:
- VIR_FORCE_CLOSE(fd);
+ if (ioctl(fd, TUNSETPERSIST, 0) < 0) {
+ virReportSystemError(errno, "%s",
+ _("Unable to make TAP device non-persistent"));
+ goto cleanup;
+ }
- return errno;
+ ret = 0;
+
+cleanup:
+ VIR_FORCE_CLOSE(fd);
+ return ret;
}
*
* Function to control if an interface is activated (up, 1) or not (down, 0)
*
- * Returns 0 in case of success or an errno code in case of failure.
+ * Returns 0 in case of success or -1 on error.
*/
int
brSetInterfaceUp(const char *ifname,
int ifflags;
if ((fd = brSetupControl(ifname, &ifr)) < 0)
- return errno;
+ return -1;
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
- ret = errno;
+ virReportSystemError(errno,
+ _("Cannot get interface flags on '%s'"),
+ ifname);
goto cleanup;
}
if (ifr.ifr_flags != ifflags) {
ifr.ifr_flags = ifflags;
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
- ret = errno;
+ virReportSystemError(errno,
+ _("Cannot set interface flags on '%s'"),
+ ifname);
goto cleanup;
}
}
struct ifreq ifr;
if ((fd = brSetupControl(ifname, &ifr)) < 0)
- return errno;
+ return -1;
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
- ret = errno;
+ virReportSystemError(errno,
+ _("Cannot get interface flags on '%s'"),
+ ifname);
goto cleanup;
}
{
int fd;
struct ifreq ifr;
+ int ret = -1;
- if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
- return errno;
+ if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
+ virReportSystemError(errno, "%s",
+ _("Unable to open /dev/net/tun, is tun module loaded?"));
+ return -1;
+ }
memset(&ifr, 0, sizeof(ifr));
# endif
if (virStrcpyStatic(ifr.ifr_name, *ifname) == NULL) {
- errno = EINVAL;
- goto error;
+ virReportSystemError(ERANGE,
+ _("Network interface name '%s' is too long"),
+ *ifname);
+ goto cleanup;
+
}
- if (ioctl(fd, TUNSETIFF, &ifr) < 0)
- goto error;
+ if (ioctl(fd, TUNSETIFF, &ifr) < 0) {
+ virReportSystemError(errno,
+ _("Unable to create tap device %s"),
+ NULLSTR(*ifname));
+ goto cleanup;
+ }
if (!tapfd &&
- (errno = ioctl(fd, TUNSETPERSIST, 1)))
- goto error;
+ (errno = ioctl(fd, TUNSETPERSIST, 1))) {
+ virReportSystemError(errno,
+ _("Unable to set tap device %s to persistent"),
+ NULLSTR(*ifname));
+ goto cleanup;
+ }
+
VIR_FREE(*ifname);
- if (!(*ifname = strdup(ifr.ifr_name)))
- goto error;
- if(tapfd)
+ if (!(*ifname = strdup(ifr.ifr_name))) {
+ virReportOOMError();
+ goto cleanup;
+ }
+ if (tapfd)
*tapfd = fd;
else
VIR_FORCE_CLOSE(fd);
- return 0;
- error:
- VIR_FORCE_CLOSE(fd);
+ ret = 0;
- return errno;
+cleanup:
+ if (ret < 0)
+ VIR_FORCE_CLOSE(fd);
+
+ return ret;
}
#endif /* WITH_BRIDGE */