[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v2 05/17] vircgroup: introduce virCgroupV2DevicesCreateProg



This function creates new BPF program with new empty BPF map with the
default size and attaches it to the guest cgroup.

Signed-off-by: Pavel Hrdina <phrdina redhat com>
---
 src/libvirt_private.syms      |  1 +
 src/util/vircgroupv2devices.c | 53 +++++++++++++++++++++++++++++++++++
 src/util/vircgroupv2devices.h |  3 ++
 3 files changed, 57 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7f1050ef5a..8ec188e9cd 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1614,6 +1614,7 @@ virCgroupV2Register;
 # util/vircgroupv2devices.h
 virCgroupV2DevicesAttachProg;
 virCgroupV2DevicesAvailable;
+virCgroupV2DevicesCreateProg;
 virCgroupV2DevicesDetectProg;
 
 # util/virclosecallbacks.h
diff --git a/src/util/vircgroupv2devices.c b/src/util/vircgroupv2devices.c
index e609faa210..cd369ef6ab 100644
--- a/src/util/vircgroupv2devices.c
+++ b/src/util/vircgroupv2devices.c
@@ -299,6 +299,49 @@ virCgroupV2DevicesDetectProg(virCgroupPtr group)
     VIR_FORCE_CLOSE(cgroupfd);
     return ret;
 }
+
+
+# define VIR_CGROUP_V2_INITIAL_BPF_MAP_SIZE 64
+
+static int
+virCgroupV2DevicesCreateMap(size_t size)
+{
+    int mapfd = virBPFCreateMap(BPF_MAP_TYPE_HASH, sizeof(uint64_t),
+                                sizeof(uint32_t), size);
+
+    if (mapfd < 0) {
+        virReportSystemError(errno, "%s",
+                             _("failed to initialize device BPF map"));
+        return -1;
+    }
+
+    return mapfd;
+}
+
+
+int
+virCgroupV2DevicesCreateProg(virCgroupPtr group)
+{
+    int mapfd;
+
+    if (group->unified.devices.progfd > 0 && group->unified.devices.mapfd > 0)
+        return 0;
+
+    mapfd = virCgroupV2DevicesCreateMap(VIR_CGROUP_V2_INITIAL_BPF_MAP_SIZE);
+    if (mapfd < 0)
+        return -1;
+
+    if (virCgroupV2DevicesAttachProg(group, mapfd,
+                                     VIR_CGROUP_V2_INITIAL_BPF_MAP_SIZE) < 0) {
+        goto error;
+    }
+
+    return 0;
+
+ error:
+    VIR_FORCE_CLOSE(mapfd);
+    return -1;
+}
 #else /* !HAVE_DECL_BPF_CGROUP_DEVICE */
 bool
 virCgroupV2DevicesAvailable(virCgroupPtr group ATTRIBUTE_UNUSED)
@@ -327,4 +370,14 @@ virCgroupV2DevicesDetectProg(virCgroupPtr group ATTRIBUTE_UNUSED)
                            "with this kernel"));
     return -1;
 }
+
+
+int
+virCgroupV2DevicesCreateProg(virCgroupPtr group ATTRIBUTE_UNUSED)
+{
+    virReportSystemError(ENOSYS, "%s",
+                         _("cgroups v2 BPF devices not supported "
+                           "with this kernel"));
+    return -1;
+}
 #endif /* !HAVE_DECL_BPF_CGROUP_DEVICE */
diff --git a/src/util/vircgroupv2devices.h b/src/util/vircgroupv2devices.h
index a8e50dcca5..bcbd761537 100644
--- a/src/util/vircgroupv2devices.h
+++ b/src/util/vircgroupv2devices.h
@@ -32,4 +32,7 @@ virCgroupV2DevicesAttachProg(virCgroupPtr group,
 int
 virCgroupV2DevicesDetectProg(virCgroupPtr group);
 
+int
+virCgroupV2DevicesCreateProg(virCgroupPtr group);
+
 #endif /* LIBVIRT_VIRCGROUPV2DEVICES_H */
-- 
2.20.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]