[libvirt] [PATCH v2 1/2] virtportallocator: new function "virPortAllocatorSetUsed"

Giuseppe Scrivano gscrivan at redhat.com
Tue Jun 24 11:34:17 UTC 2014


virPortAllocatorSetUsed permits to set a port as already used and
prevent the port allocator to use it without any attempt to bind it.

Signed-off-by: Giuseppe Scrivano <gscrivan at redhat.com>
---
 src/libvirt_private.syms    |  1 +
 src/util/virportallocator.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
 src/util/virportallocator.h |  4 ++++
 3 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 2a2b9c0..95a45aa 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1770,6 +1770,7 @@ virPidFileWritePath;
 virPortAllocatorAcquire;
 virPortAllocatorNew;
 virPortAllocatorRelease;
+virPortAllocatorSetUsed;
 
 
 # util/virprocess.h
diff --git a/src/util/virportallocator.c b/src/util/virportallocator.c
index b68133a..226f386 100644
--- a/src/util/virportallocator.c
+++ b/src/util/virportallocator.c
@@ -1,7 +1,7 @@
 /*
  * virportallocator.c: Allocate & track TCP port allocations
  *
- * Copyright (C) 2013 Red Hat, Inc.
+ * Copyright (C) 2013, 2014 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -250,3 +250,45 @@ int virPortAllocatorRelease(virPortAllocatorPtr pa,
     virObjectUnlock(pa);
     return ret;
 }
+
+int virPortAllocatorSetUsed(virPortAllocatorPtr pa,
+                            unsigned short port,
+                            bool value)
+{
+    int ret = -1;
+
+    virObjectLock(pa);
+
+    if (port < pa->start ||
+        port > pa->end) {
+        ret = 0;
+        goto cleanup;
+    }
+
+    if (value) {
+        bool used = false;
+        if (virBitmapGetBit(pa->bitmap, port - pa->start, &used) < 0)
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to query port %d"), port);
+
+        if (used || virBitmapSetBit(pa->bitmap, port - pa->start) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to reserve port %d"), port);
+            goto cleanup;
+        }
+    }
+    else {
+        if (virBitmapClearBit(pa->bitmap,
+                              port - pa->start) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to release port %d"),
+                           port);
+            goto cleanup;
+        }
+    }
+
+    ret = 0;
+ cleanup:
+    virObjectUnlock(pa);
+    return ret;
+}
diff --git a/src/util/virportallocator.h b/src/util/virportallocator.h
index c8aa6de..e5ee56d 100644
--- a/src/util/virportallocator.h
+++ b/src/util/virportallocator.h
@@ -38,4 +38,8 @@ int virPortAllocatorAcquire(virPortAllocatorPtr pa,
 int virPortAllocatorRelease(virPortAllocatorPtr pa,
                             unsigned short port);
 
+int virPortAllocatorSetUsed(virPortAllocatorPtr pa,
+                            unsigned short port,
+                            bool value);
+
 #endif /* __VIR_PORT_ALLOCATOR_H__ */
-- 
1.9.3




More information about the libvir-list mailing list