Factor out all rctl(8) execution code to bhyve_rctl.c.

Signed-off-by: Roman Bogorodskiy <[email protected]>
---
 src/bhyve/bhyve_process.c | 24 ++++--------------------
 src/bhyve/bhyve_rctl.c    | 34 ++++++++++++++++++++++++++++++++++
 src/bhyve/bhyve_rctl.h    |  8 ++++++++
 3 files changed, 46 insertions(+), 20 deletions(-)

diff --git a/src/bhyve/bhyve_process.c b/src/bhyve/bhyve_process.c
index 7baefeb63b..92665419d4 100644
--- a/src/bhyve/bhyve_process.c
+++ b/src/bhyve/bhyve_process.c
@@ -39,13 +39,13 @@
 #include "bhyve_firmware.h"
 #include "bhyve_monitor.h"
 #include "bhyve_process.h"
+#include "bhyve_rctl.h"
 #include "datatypes.h"
 #include "virerror.h"
 #include "virhook.h"
 #include "virlog.h"
 #include "virfile.h"
 #include "viralloc.h"
-#include "vircommand.h"
 #include "virstring.h"
 #include "virpidfile.h"
 #include "virprocess.h"
@@ -149,32 +149,16 @@ bhyveSetResourceLimits(struct _bhyveConn *driver, 
virDomainObj *vm)
         return -1;
     }
 
-#define BHYVE_APPLY_RCTL_RULE(field, type, action, format) \
-    do { \
-        if ((field)) { \
-            g_autofree char *rule = NULL; \
-            g_autoptr(virCommand) cmd = virCommandNewArgList("rctl", "-a", 
NULL); \
-            virCommandAddArgFormat(cmd, "process:%d:" type ":" action "=" 
format, \
-                                   vm->pid, (field)); \
-            if (virCommandRun(cmd, NULL) < 0) \
-                return -1; \
-         } \
-    } while (0)
-
     if (vm->def->blkio.ndevices == 1) {
         device = &vm->def->blkio.devices[0];
 
-        BHYVE_APPLY_RCTL_RULE(device->riops, "readiops", "throttle", "%u");
-        BHYVE_APPLY_RCTL_RULE(device->wiops, "writeiops", "throttle", "%u");
-        BHYVE_APPLY_RCTL_RULE(device->rbps, "readbps", "throttle", "%llu");
-        BHYVE_APPLY_RCTL_RULE(device->wbps, "writebps", "throttle", "%llu");
+        bhyveRctlSetIoLimits(vm->pid, device);
     }
 
     /* rctl(8) uses bytes for these values and def->mem.* uses kibibytes */
     if (virMemoryLimitIsSet(vm->def->mem.hard_limit))
-        BHYVE_APPLY_RCTL_RULE(vm->def->mem.hard_limit * 1024, "memoryuse", 
"deny", "%llu");
-
-#undef BHYVE_APPLY_RCTL_RULE
+        if (bhyveRctlSetMemoryHardLimit(vm->pid, vm->def->mem.hard_limit) < 0)
+            return -1;
 
     return 0;
 }
diff --git a/src/bhyve/bhyve_rctl.c b/src/bhyve/bhyve_rctl.c
index 69467d2e34..c9e8e26cc4 100644
--- a/src/bhyve/bhyve_rctl.c
+++ b/src/bhyve/bhyve_rctl.c
@@ -99,3 +99,37 @@ bhyveRctlGetMemoryHardLimit(pid_t pid, unsigned long long 
*kb)
 
     return -1;
 }
+
+#define BHYVE_APPLY_RCTL_RULE(pid, field, type, action, format) \
+    do { \
+        if ((field)) { \
+            g_autofree char *rule = NULL; \
+            g_autoptr(virCommand) cmd = virCommandNewArgList("rctl", "-a", 
NULL); \
+            virCommandAddArgFormat(cmd, "process:%d:" type ":" action "=" 
format, \
+                                   pid, (field)); \
+            if (virCommandRun(cmd, NULL) < 0) \
+                return -1; \
+         } \
+    } while (0)
+
+int
+bhyveRctlSetMemoryHardLimit(pid_t pid, unsigned long long kb)
+{
+    /* rctl(8) uses bytes for these values and def->mem.* uses kibibytes */
+    BHYVE_APPLY_RCTL_RULE(pid, kb * 1024, "memoryuse", "deny", "%llu");
+
+    return 0;
+}
+
+int
+bhyveRctlSetIoLimits(pid_t pid, const virBlkioDevice *device)
+{
+    BHYVE_APPLY_RCTL_RULE(pid, device->riops, "readiops", "throttle", "%u");
+    BHYVE_APPLY_RCTL_RULE(pid, device->wiops, "writeiops", "throttle", "%u");
+    BHYVE_APPLY_RCTL_RULE(pid, device->rbps, "readbps", "throttle", "%llu");
+    BHYVE_APPLY_RCTL_RULE(pid, device->wbps, "writebps", "throttle", "%llu");
+
+    return 0;
+}
+
+#undef BHYVE_APPLY_RCTL_RULE
diff --git a/src/bhyve/bhyve_rctl.h b/src/bhyve/bhyve_rctl.h
index 46f102c38e..83d4a32013 100644
--- a/src/bhyve/bhyve_rctl.h
+++ b/src/bhyve/bhyve_rctl.h
@@ -20,5 +20,13 @@
 
 #pragma once
 
+#include "domain_conf.h"
+
 int
 bhyveRctlGetMemoryHardLimit(pid_t pid, unsigned long long *kb);
+
+int
+bhyveRctlSetMemoryHardLimit(pid_t pid, unsigned long long kb);
+
+int
+bhyveRctlSetIoLimits(pid_t pid, const virBlkioDevice *device);
-- 
2.52.0

Reply via email to