render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
9119d9
From 8d86aac0104f3a7a1f5c26ab04ac480b05b3921f Mon Sep 17 00:00:00 2001
9119d9
Message-Id: <8d86aac0104f3a7a1f5c26ab04ac480b05b3921f@dist-git>
9119d9
From: Laine Stump <laine@laine.org>
9119d9
Date: Mon, 15 Dec 2014 10:51:25 -0500
9119d9
Subject: [PATCH] util: new functions for setting bridge and bridge port
9119d9
 attributes
9119d9
9119d9
This is part of the fix for:
9119d9
9119d9
  https://bugzilla.redhat.com/show_bug.cgi?id=1099210
9119d9
9119d9
These functions all set/get items in the sysfs for a bridge device.
9119d9
9119d9
(cherry picked from commit 100b7a72a4cf209f2146610f1860b87331f1d9ad)
9119d9
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
---
9119d9
 src/libvirt_private.syms   |   6 ++
9119d9
 src/util/virnetdevbridge.c | 235 ++++++++++++++++++++++++++++++++++++++++++++-
9119d9
 src/util/virnetdevbridge.h |  28 +++++-
9119d9
 3 files changed, 266 insertions(+), 3 deletions(-)
9119d9
9119d9
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
9119d9
index 6353d9c..2b7526a 100644
9119d9
--- a/src/libvirt_private.syms
9119d9
+++ b/src/libvirt_private.syms
9119d9
@@ -1632,9 +1632,15 @@ virNetDevBridgeCreate;
9119d9
 virNetDevBridgeDelete;
9119d9
 virNetDevBridgeGetSTP;
9119d9
 virNetDevBridgeGetSTPDelay;
9119d9
+virNetDevBridgeGetVlanFiltering;
9119d9
+virNetDevBridgePortGetLearning;
9119d9
+virNetDevBridgePortGetUnicastFlood;
9119d9
+virNetDevBridgePortSetLearning;
9119d9
+virNetDevBridgePortSetUnicastFlood;
9119d9
 virNetDevBridgeRemovePort;
9119d9
 virNetDevBridgeSetSTP;
9119d9
 virNetDevBridgeSetSTPDelay;
9119d9
+virNetDevBridgeSetVlanFiltering;
9119d9
 
9119d9
 
9119d9
 # util/virnetdevmacvlan.h
9119d9
diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
9119d9
index d388358..1f3f221 100644
9119d9
--- a/src/util/virnetdevbridge.c
9119d9
+++ b/src/util/virnetdevbridge.c
9119d9
@@ -1,5 +1,5 @@
9119d9
 /*
9119d9
- * Copyright (C) 2007-2013 Red Hat, Inc.
9119d9
+ * Copyright (C) 2007-2014 Red Hat, Inc.
9119d9
  *
9119d9
  * This library is free software; you can redistribute it and/or
9119d9
  * modify it under the terms of the GNU Lesser General Public
9119d9
@@ -219,6 +219,170 @@ static int virNetDevBridgeGet(const char *brname,
9119d9
 }
9119d9
 #endif /* __linux__ */
9119d9
 
9119d9
+#if defined(__linux__)
9119d9
+static int
9119d9
+virNetDevBridgePortSet(const char *brname,
9119d9
+                       const char *ifname,
9119d9
+                       const char *paramname,
9119d9
+                       unsigned long value)
9119d9
+{
9119d9
+    char *path = NULL;
9119d9
+    char valuestr[INT_BUFSIZE_BOUND(value)];
9119d9
+    int ret = -1;
9119d9
+
9119d9
+    snprintf(valuestr, sizeof(valuestr), "%lu", value);
9119d9
+
9119d9
+    if (virAsprintf(&path, "%s/%s/brif/%s/%s",
9119d9
+                    SYSFS_NET_DIR, brname, ifname, paramname) < 0)
9119d9
+        return -1;
9119d9
+
9119d9
+    if (!virFileExists(path))
9119d9
+        errno = EINVAL;
9119d9
+    else
9119d9
+        ret = virFileWriteStr(path, valuestr, 0);
9119d9
+
9119d9
+    if (ret < 0) {
9119d9
+        virReportSystemError(errno,
9119d9
+                             _("Unable to set bridge %s port %s %s to %s"),
9119d9
+                             brname, ifname, paramname, valuestr);
9119d9
+    }
9119d9
+
9119d9
+    VIR_FREE(path);
9119d9
+    return ret;
9119d9
+}
9119d9
+
9119d9
+
9119d9
+static int
9119d9
+virNetDevBridgePortGet(const char *brname,
9119d9
+                       const char *ifname,
9119d9
+                       const char *paramname,
9119d9
+                       unsigned long *value)
9119d9
+{
9119d9
+    char *path = NULL;
9119d9
+    char *valuestr = NULL;
9119d9
+    int ret = -1;
9119d9
+
9119d9
+    if (virAsprintf(&path, "%s/%s/brif/%s/%s",
9119d9
+                    SYSFS_NET_DIR, brname, ifname, paramname) < 0)
9119d9
+        return -1;
9119d9
+
9119d9
+    if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long), &valuestr) < 0)
9119d9
+        goto cleanup;
9119d9
+
9119d9
+    if (virStrToLong_ul(valuestr, NULL, 10, value) < 0) {
9119d9
+        virReportSystemError(EINVAL,
9119d9
+                             _("Unable to get bridge %s port %s %s"),
9119d9
+                             brname, ifname, paramname);
9119d9
+        goto cleanup;
9119d9
+    }
9119d9
+
9119d9
+    ret = 0;
9119d9
+ cleanup:
9119d9
+    VIR_FREE(path);
9119d9
+    VIR_FREE(valuestr);
9119d9
+    return ret;
9119d9
+}
9119d9
+
9119d9
+
9119d9
+int
9119d9
+virNetDevBridgePortGetLearning(const char *brname,
9119d9
+                               const char *ifname,
9119d9
+                               bool *enable)
9119d9
+{
9119d9
+    int ret = -1;
9119d9
+    unsigned long value;
9119d9
+
9119d9
+    if (virNetDevBridgePortGet(brname, ifname, "learning", &value) < 0)
9119d9
+       goto cleanup;
9119d9
+
9119d9
+    *enable = !!value;
9119d9
+    ret = 0;
9119d9
+ cleanup:
9119d9
+    return ret;
9119d9
+}
9119d9
+
9119d9
+
9119d9
+int
9119d9
+virNetDevBridgePortSetLearning(const char *brname,
9119d9
+                               const char *ifname,
9119d9
+                               bool enable)
9119d9
+{
9119d9
+    return virNetDevBridgePortSet(brname, ifname, "learning", enable ? 1 : 0);
9119d9
+}
9119d9
+
9119d9
+
9119d9
+int
9119d9
+virNetDevBridgePortGetUnicastFlood(const char *brname,
9119d9
+                                   const char *ifname,
9119d9
+                                   bool *enable)
9119d9
+{
9119d9
+    int ret = -1;
9119d9
+    unsigned long value;
9119d9
+
9119d9
+    if (virNetDevBridgePortGet(brname, ifname, "unicast_flood", &value) < 0)
9119d9
+       goto cleanup;
9119d9
+
9119d9
+    *enable = !!value;
9119d9
+    ret = 0;
9119d9
+ cleanup:
9119d9
+    return ret;
9119d9
+}
9119d9
+
9119d9
+
9119d9
+int
9119d9
+virNetDevBridgePortSetUnicastFlood(const char *brname,
9119d9
+                                   const char *ifname,
9119d9
+                                   bool enable)
9119d9
+{
9119d9
+    return virNetDevBridgePortSet(brname, ifname, "unicast_flood", enable ? 1 : 0);
9119d9
+}
9119d9
+
9119d9
+
9119d9
+#else
9119d9
+int
9119d9
+virNetDevBridgePortGetLearning(const char *brname ATTRIBUTE_UNUSED,
9119d9
+                               const char *ifname ATTRIBUTE_UNUSED,
9119d9
+                               bool *enable ATTRIBUTE_UNUSED)
9119d9
+{
9119d9
+    virReportSystemError(ENOSYS, "%s",
9119d9
+                         _("Unable to get bridge port learning on this platform"));
9119d9
+    return -1;
9119d9
+}
9119d9
+
9119d9
+
9119d9
+int
9119d9
+virNetDevBridgePortSetLearning(const char *brname ATTRIBUTE_UNUSED,
9119d9
+                               const char *ifname ATTRIBUTE_UNUSED,
9119d9
+                               bool enable)
9119d9
+{
9119d9
+    virReportSystemError(ENOSYS, "%s",
9119d9
+                         _("Unable to set bridge port learning on this platform"));
9119d9
+    return -1;
9119d9
+}
9119d9
+
9119d9
+
9119d9
+int
9119d9
+virNetDevBridgePortGetUnicastFlood(const char *brname ATTRIBUTE_UNUSED,
9119d9
+                                   const char *ifname ATTRIBUTE_UNUSED,
9119d9
+                                   bool *enable ATTRIBUTE_UNUSED)
9119d9
+{
9119d9
+    virReportSystemError(ENOSYS, "%s",
9119d9
+                         _("Unable to get bridge port unicast_flood on this platform"));
9119d9
+    return -1;
9119d9
+}
9119d9
+
9119d9
+
9119d9
+int
9119d9
+virNetDevBridgePortSetUnicastFlood(const char *brname ATTRIBUTE_UNUSED,
9119d9
+                                   const char *ifname ATTRIBUTE_UNUSED,
9119d9
+                                   bool enable ATTRIBUTE_UNUSED)
9119d9
+{
9119d9
+    virReportSystemError(ENOSYS, "%s",
9119d9
+                         _("Unable to set bridge port unicast_flood on this platform"));
9119d9
+    return -1;
9119d9
+}
9119d9
+#endif
9119d9
+
9119d9
 
9119d9
 /**
9119d9
  * virNetDevBridgeCreate:
9119d9
@@ -518,7 +682,7 @@ int virNetDevBridgeSetSTPDelay(const char *brname,
9119d9
  * @brname: the bridge device name
9119d9
  * @delayms: the forward delay in milliseconds
9119d9
  *
9119d9
- * Retrives the forward delay for the bridge device @brname
9119d9
+ * Retrieves the forward delay for the bridge device @brname
9119d9
  * storing it in @delayms. The forward delay is only meaningful
9119d9
  * if STP is enabled
9119d9
  *
9119d9
@@ -683,3 +847,70 @@ int virNetDevBridgeGetSTP(const char *brname,
9119d9
     return -1;
9119d9
 }
9119d9
 #endif
9119d9
+
9119d9
+#if defined(HAVE_STRUCT_IFREQ) && defined(__linux__)
9119d9
+/**
9119d9
+ * virNetDevBridgeGetVlanFiltering:
9119d9
+ * @brname: the bridge device name
9119d9
+ * @enable: true or false
9119d9
+ *
9119d9
+ * Retrieves the vlan_filtering setting for the bridge device @brname
9119d9
+ * storing it in @enable.
9119d9
+ *
9119d9
+ * Returns 0 on success, -1 on error
9119d9
+ */
9119d9
+int
9119d9
+virNetDevBridgeGetVlanFiltering(const char *brname,
9119d9
+                                bool *enable)
9119d9
+{
9119d9
+    int ret = -1;
9119d9
+    unsigned long value;
9119d9
+
9119d9
+    if (virNetDevBridgeGet(brname, "vlan_filtering", &value, -1, NULL) < 0)
9119d9
+        goto cleanup;
9119d9
+
9119d9
+    *enable = !!value;
9119d9
+    ret = 0;
9119d9
+ cleanup:
9119d9
+    return ret;
9119d9
+}
9119d9
+
9119d9
+
9119d9
+/**
9119d9
+ * virNetDevBridgeSetVlanFiltering:
9119d9
+ * @brname: the bridge name
9119d9
+ * @enable: true or false
9119d9
+ *
9119d9
+ * Set the bridge vlan_filtering mode
9119d9
+ *
9119d9
+ * Returns 0 in case of success or -1 on failure
9119d9
+ */
9119d9
+
9119d9
+int
9119d9
+virNetDevBridgeSetVlanFiltering(const char *brname,
9119d9
+                                bool enable)
9119d9
+{
9119d9
+    return virNetDevBridgeSet(brname, "vlan_filtering", enable ? 1 : 0, -1, NULL);
9119d9
+}
9119d9
+
9119d9
+
9119d9
+#else
9119d9
+int
9119d9
+virNetDevBridgeGetVlanFiltering(const char *brname ATTRIBUTE_UNUSED,
9119d9
+                                bool *enable ATTRIBUTE_UNUSED)
9119d9
+{
9119d9
+    virReportSystemError(ENOSYS, "%s",
9119d9
+                         _("Unable to get bridge vlan_filtering on this platform"));
9119d9
+    return -1;
9119d9
+}
9119d9
+
9119d9
+
9119d9
+int
9119d9
+virNetDevBridgeSetVlanFiltering(const char *brname ATTRIBUTE_UNUSED,
9119d9
+                                bool enable ATTRIBUTE_UNUSED)
9119d9
+{
9119d9
+    virReportSystemError(ENOSYS, "%s",
9119d9
+                         _("Unable to set bridge vlan_filtering on this platform"));
9119d9
+    return -1;
9119d9
+}
9119d9
+#endif
9119d9
diff --git a/src/util/virnetdevbridge.h b/src/util/virnetdevbridge.h
9119d9
index 13776d2..8188a2f 100644
9119d9
--- a/src/util/virnetdevbridge.h
9119d9
+++ b/src/util/virnetdevbridge.h
9119d9
@@ -1,5 +1,5 @@
9119d9
 /*
9119d9
- * Copyright (C) 2007-2011 Red Hat, Inc.
9119d9
+ * Copyright (C) 2007-2012, 2014 Red Hat, Inc.
9119d9
  *
9119d9
  * This library is free software; you can redistribute it and/or
9119d9
  * modify it under the terms of the GNU Lesser General Public
9119d9
@@ -51,4 +51,30 @@ int virNetDevBridgeGetSTP(const char *brname,
9119d9
                           bool *enable)
9119d9
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
9119d9
 
9119d9
+int virNetDevBridgeSetVlanFiltering(const char *brname,
9119d9
+                                    bool enable)
9119d9
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
9119d9
+int virNetDevBridgeGetVlanFiltering(const char *brname,
9119d9
+                                    bool *enable)
9119d9
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
9119d9
+
9119d9
+int virNetDevBridgePortGetLearning(const char *brname,
9119d9
+                                   const char *ifname,
9119d9
+                                   bool *enable)
9119d9
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
9119d9
+    ATTRIBUTE_RETURN_CHECK;
9119d9
+int virNetDevBridgePortSetLearning(const char *brname,
9119d9
+                                   const char *ifname,
9119d9
+                                   bool enable)
9119d9
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
9119d9
+int virNetDevBridgePortGetUnicastFlood(const char *brname,
9119d9
+                                       const char *ifname,
9119d9
+                                       bool *enable)
9119d9
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
9119d9
+    ATTRIBUTE_RETURN_CHECK;
9119d9
+int virNetDevBridgePortSetUnicastFlood(const char *brname,
9119d9
+                                       const char *ifname,
9119d9
+                                       bool enable)
9119d9
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
9119d9
+
9119d9
 #endif /* __VIR_NETDEV_BRIDGE_H__ */
9119d9
-- 
9119d9
2.2.0
9119d9