]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: Introduce virNetDevBandwidthValidate()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 13 Aug 2024 11:15:50 +0000 (13:15 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 20 Aug 2024 07:19:25 +0000 (09:19 +0200)
This function validates whether parsed limits are within range as
defined by 'tc' sources (since we use tc to set QoS; or OVS which
then uses tc too). The 'tc' program stores speeds in 64bit
integers (unit is bytes per second) and sizes in uints (unit is
bytes). We use different units: kilobytes per second and
kibibytes and therefore we can parse values larger than 'tc' can
handle and thus need a function to check if values still fit.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
src/conf/netdev_bandwidth_conf.c
src/conf/netdev_bandwidth_conf.h
src/libvirt_private.syms

index 9faa46a27fb3cdc082ec7654c9bb227c8eeff4b7..43a2c62240bce2d455b49b09f68fec9b79b6a686 100644 (file)
@@ -200,6 +200,48 @@ virNetDevBandwidthFormat(const virNetDevBandwidth *def,
 }
 
 
+#define CHECK_LIMIT(val, limit, name) \
+    do { \
+        if ((val) > (limit)) { \
+            virReportError(VIR_ERR_OVERFLOW, \
+                           _("value '%1$llu' is too big for '%2$s' parameter, maximum is '%3$llu'"), \
+                           val, name, (unsigned long long) limit); \
+            return false; \
+        } \
+    } while (0)
+
+static bool
+virNetDevBandwidthRateValidate(const virNetDevBandwidthRate *rate)
+{
+    const unsigned long long speedLimit = 1ULL << 54;
+    const unsigned int sizeLimit = UINT_MAX >> 10;
+
+    /* These limits are taken straight from 'tc' sources. */
+
+    if (!rate)
+        return true;
+
+    CHECK_LIMIT(rate->average, speedLimit, "average");
+    CHECK_LIMIT(rate->peak, speedLimit, "peak");
+    CHECK_LIMIT(rate->burst, sizeLimit, "burst");
+    CHECK_LIMIT(rate->floor, speedLimit, "floor");
+
+    return true;
+}
+
+#undef CHECK_LIMIT
+
+bool
+virNetDevBandwidthValidate(const virNetDevBandwidth *def)
+{
+    if (!def)
+        return true;
+
+    return virNetDevBandwidthRateValidate(def->in) &&
+        virNetDevBandwidthRateValidate(def->out);
+}
+
+
 bool virNetDevSupportsBandwidth(virDomainNetType type)
 {
     switch ((virDomainNetType) type) {
index b679b0f51f2e294bbeaf17913b069a2375dfccff..6dbe0298f6167218b52e778a3b427e49ecb94c35 100644 (file)
@@ -34,6 +34,8 @@ int virNetDevBandwidthFormat(const virNetDevBandwidth *def,
                              unsigned int class_id,
                              virBuffer *buf);
 
+bool virNetDevBandwidthValidate(const virNetDevBandwidth *def);
+
 bool virNetDevSupportsBandwidth(virDomainNetType type);
 bool virNetDevBandwidthHasFloor(const virNetDevBandwidth *b);
 bool virNetDevBandwidthSupportsFloor(virNetworkForwardType type);
index 0accca442ae02b5901f618f70be88b865233030f..7a5df5a6a45edc06f9d385e4b220afc9787d050c 100644 (file)
@@ -825,6 +825,7 @@ virNetDevBandwidthFormat;
 virNetDevBandwidthHasFloor;
 virNetDevBandwidthParse;
 virNetDevBandwidthSupportsFloor;
+virNetDevBandwidthValidate;
 virNetDevSupportsBandwidth;