2013/5/16 Osier Yang <jyang@redhat.com>
On 16/05/13 14:07, Chunyan Liu wrote:
Write separate module for hostdev passthrough so that it could be used by all
hypervisor drivers and maintain a global hostdev state.

Signed-off-by: Chunyan Liu<cyliu@suse.com>
---
  po/POTFILES.in               |    1 +
  src/Makefile.am              |    1 +
  src/libvirt.c                |    5 +
  src/libvirt_private.syms     |   15 +
  src/util/virhostdevmanager.c | 1218 ++++++++++++++++++++++++++++++++++++++++++
  src/util/virhostdevmanager.h |   91 ++++
  src/util/virpci.c            |   17 +-
  src/util/virpci.h            |    7 +-
  src/util/virusb.c            |   19 +-
  src/util/virusb.h            |    4 +-
  10 files changed, 1362 insertions(+), 16 deletions(-)
  create mode 100644 src/util/virhostdevmanager.c
  create mode 100644 src/util/virhostdevmanager.h

diff --git a/po/POTFILES.in b/po/POTFILES.in
index f3ea4da..a7c21bf 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -188,6 +188,7 @@ src/util/viruri.c
  src/util/virusb.c
  src/util/virutil.c
  src/util/virxml.c
+src/util/virhostdevmanager.c
  src/vbox/vbox_MSCOMGlue.c
  src/vbox/vbox_XPCOMCGlue.c
  src/vbox/vbox_driver.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 4312c3c..a197c2b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -130,6 +130,7 @@ UTIL_SOURCES =                                                      \
                util/virutil.c util/virutil.h                   \
                util/viruuid.c util/viruuid.h                   \
                util/virxml.c util/virxml.h                     \
+                util/virhostdevmanager.c util/virhostdevmanager.h \
                $(NULL)
    diff --git a/src/libvirt.c b/src/libvirt.c
index 2b3515e..d9af5a6 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -65,6 +65,7 @@
  #include "virthread.h"
  #include "virstring.h"
  #include "virutil.h"
+#include "virhostdevmanager.h"
    #ifdef WITH_TEST
  # include "test/test_driver.h"
@@ -827,6 +828,7 @@ int virStateInitialize(bool privileged,
      if (virInitialize() < 0)
          return -1;
  +    virHostdevManagerInit();
      for (i = 0 ; i < virStateDriverTabCount ; i++) {
          if (virStateDriverTab[i]->stateInitialize) {
              VIR_DEBUG("Running global init for %s state driver",
@@ -858,6 +860,9 @@ int virStateCleanup(void) {
              virStateDriverTab[i]->stateCleanup() < 0)
              ret = -1;
      }
+
+    virHostdevManagerCleanup();
+
      return ret;
  }
  diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index cdd0b41..824de4e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1991,6 +1991,21 @@ virXPathULong;
  virXPathULongHex;
  virXPathULongLong;
  +#util/virhostdevmanager.h
+virHostdevManagerGetDefault;
+virHostdevManagerInit;
+virHostdevManagerCleanup;
+virHostdevManagerPrepareHostdevs;
+virHostdevManagerReAttachHostdevs;
+virHostdevManagerPreparePciHostdevs;
+virHostdevManagerPrepareUsbHostdevs;
+virHostdevManagerReAttachPciHostdevs;
+virHostdevManagerReAttachUsbHostdevs;
+virGetActivePciHostdevs;
+virGetActiveUsbHostdevs;
+virGetDomainActivePciHostdevs;
+virGetDomainActiveUsbHostdevs;
+virFreeHostdevNameList;
    # Let emacs know we want case-insensitive sorting
  # Local Variables:
diff --git a/src/util/virhostdevmanager.c b/src/util/virhostdevmanager.c
new file mode 100644
index 0000000..9034212
--- /dev/null
+++ b/src/util/virhostdevmanager.c
@@ -0,0 +1,1218 @@
+/*
+ * Copyright (C) 2013 SUSE LINUX Products GmbH, Nuernberg, Germany.
+ *
+ * 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/>.
+ *
+ * Author: Chunyan Liu<cyliu@suse.com>
+ */
+#include <config.h>
+
+#include "virhostdevmanager.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "viralloc.h"
+#include "virstring.h"
+#include "virfile.h"
+#include "virerror.h"
+#include "virlog.h"
+#include "virpci.h"
+#include "virusb.h"
+#include "virnetdev.h"
+
+/* For virReportOOMError()  and virReportSystemError() */
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+struct _virHostdevManager{
+    char *stateDir;
+
+    virPCIDeviceListPtr activePciHostdevs;
+    virPCIDeviceListPtr inactivePciHostdevs;
+    virUSBDeviceListPtr activeUsbHostdevs;
You will need locks...

[RESEND since I'm not clear if my reply is sent out or not, I didn't see it in list yet after several hours. ]

Things that need to protect are: activePciHostdevs, inactivePciHostdevs and activeUsbHostdevs, these lists are inherited from virObjectLockable, could be locked explicitly with virObjectLock(). Like in the code:
virObjectLock(mgr->activePciHostdevs);
virObjectUnlock(mgr->activePciHostdevs);

So I think virObjectLock/Unlock can achieve, extra lock is not needed.