]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: create virvsock.c
authorJán Tomko <jtomko@redhat.com>
Tue, 22 May 2018 13:52:52 +0000 (15:52 +0200)
committerJán Tomko <jtomko@redhat.com>
Tue, 29 May 2018 13:42:03 +0000 (15:42 +0200)
A file for vsock-related helper functions.
virVsockSetGuestCid to set an already-known CID,
virVsockAcquireGuestCid that will use the first available CID

https://bugzilla.redhat.com/show_bug.cgi?id=1291851

Signed-off-by: Ján Tomko <jtomko@redhat.com>
configure.ac
src/libvirt_private.syms
src/util/Makefile.inc.am
src/util/virvsock.c [new file with mode: 0644]
src/util/virvsock.h [new file with mode: 0644]

index cc005ea9e837facccfe578fa64640444ad5bf933..b869a17afe00a98247bb26048e6662d2ed3ea96b 100644 (file)
@@ -641,6 +641,14 @@ if test "$with_linux" = "yes"; then
                    [[#include <linux/devlink.h>]])
 fi
 
+dnl
+dnl check for VHOST_VSOCK_SET_GUEST_CID
+dnl
+if test "$with_linux" = "yes"; then
+    AC_CHECK_DECLS([VHOST_VSOCK_SET_GUEST_CID], [], [],
+                   [[#include <linux/vhost.h>]])
+fi
+
 dnl Allow perl/python overrides
 AC_PATH_PROGS([PYTHON], [python3 python2 python])
 if test -z "$PYTHON"; then
index 682c9f15f315e13847cba8baabb50602573580ff..60016359165b1187cc022f81e7c18acd10cc5dd2 100644 (file)
@@ -3122,6 +3122,11 @@ virVHBAManageVport;
 virVHBAPathExists;
 
 
+# util/virvsock.h
+virVsockAcquireGuestCid;
+virVsockSetGuestCid;
+
+
 # util/virxml.h
 virXMLCheckIllegalChars;
 virXMLChildElementCount;
index ec8745da7e9a1394b7cc34949396c3d6de42d176..a22265606c51c3447c855589f72073755ceae326 100644 (file)
@@ -204,6 +204,8 @@ UTIL_SOURCES = \
        util/viruuid.h \
        util/virvhba.c \
        util/virvhba.h \
+       util/virvsock.c \
+       util/virvsock.h \
        util/virxdrdefs.h \
        util/virxml.c \
        util/virxml.h \
diff --git a/src/util/virvsock.c b/src/util/virvsock.c
new file mode 100644 (file)
index 0000000..ebb5182
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <config.h>
+
+#include <sys/ioctl.h>
+
+#if HAVE_DECL_VHOST_VSOCK_SET_GUEST_CID
+# include <linux/vhost.h>
+#endif
+
+#include "virvsock.h"
+
+#include "virerror.h"
+#include "virlog.h"
+
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+VIR_LOG_INIT("util.vsock");
+
+#if HAVE_DECL_VHOST_VSOCK_SET_GUEST_CID
+static int
+virVsockSetGuestCidQuiet(int fd,
+                         unsigned int guest_cid)
+{
+    uint64_t val = guest_cid;
+
+    return ioctl(fd, VHOST_VSOCK_SET_GUEST_CID, &val);
+}
+
+#else
+static int
+virVsockSetGuestCidQuiet(int fd ATTRIBUTE_UNUSED,
+                         unsigned int guest_cid ATTRIBUTE_UNUSED)
+{
+    errno = ENOSYS;
+    return -1;
+}
+#endif
+
+
+/**
+ * virVsockSetGuestCid:
+ * @fd: file descriptor of a vsock interface
+ * @guest_cid: guest CID to be set
+ *
+ * Wrapper for VHOST_VSOCK_SET_GUEST_CID ioctl.
+ * Returns: 0 on success, -1 on error.
+ */
+int
+virVsockSetGuestCid(int fd,
+                    unsigned int guest_cid)
+{
+    if (virVsockSetGuestCidQuiet(fd, guest_cid) < 0) {
+        virReportSystemError(errno, "%s",
+                             _("failed to set guest cid"));
+        return -1;
+    }
+
+    return 0;
+}
+
+#define VIR_VSOCK_GUEST_CID_MIN 3
+
+/**
+ * virVsockAcquireGuestCid:
+ * @fd: file descriptor of a vsock interface
+ * @guest_cid: where to store the guest CID
+ *
+ * Iterates over usable CIDs until a free one is found.
+ * Returns: 0 on success, with the acquired CID stored in guest_cid
+ *         -1 on error.
+ */
+int
+virVsockAcquireGuestCid(int fd,
+                        unsigned int *guest_cid)
+{
+    unsigned int cid = VIR_VSOCK_GUEST_CID_MIN;
+
+    for (; virVsockSetGuestCidQuiet(fd, cid) < 0; cid++) {
+        if (errno != EADDRINUSE) {
+            virReportSystemError(errno, "%s",
+                                 _("failed to acquire guest cid"));
+            return -1;
+        }
+    }
+    *guest_cid = cid;
+
+    return 0;
+}
diff --git a/src/util/virvsock.h b/src/util/virvsock.h
new file mode 100644 (file)
index 0000000..080a322
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * virvsock.h - vsock related util functions
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __VIR_VSOCK_H__
+# define __VIR_VSOCK_H__
+
+int
+virVsockSetGuestCid(int fd,
+                    unsigned int guest_cid);
+
+int
+virVsockAcquireGuestCid(int fd,
+                        unsigned int *guest_cid);
+#endif /* __VIR_VSOCK_H__ */