<br><tt><font size=2>libvir-list-bounces@redhat.com wrote on 03/08/2010
07:29:59 PM:<br>
<br>
> [image removed] </font></tt>
<br><tt><font size=2>> <br>
> [libvirt] [RFC][PATCH v2 2/2] Automatically recreate macvtap <br>
> interface and reconnect to qemu</font></tt>
<br><tt><font size=2>> <br>
> Ed Swierk </font></tt>
<br><tt><font size=2>> <br>
> to:</font></tt>
<br><tt><font size=2>> <br>
> libvir-list</font></tt>
<br><tt><font size=2>> <br>
> 03/08/2010 07:45 PM</font></tt>
<br><tt><font size=2>> <br>
> Sent by:</font></tt>
<br><tt><font size=2>> <br>
> libvir-list-bounces@redhat.com</font></tt>
<br><tt><font size=2>> <br>
> (See the PATCH 0 email for further discussion.)<br>
> <br>
> ---<br>
> The libvirt qemu driver creates a macvtap interface for each defined<br>
> "direct" network interface, but unlike a tap interface that
is owned<br>
> by the process that creates it, a macvtap interface vanishes when
the<br>
> underlying network device goes away.  When this happens, qemu
is left<br>
> holding a file descriptor for a nonexistent tap interface.<br>
> <br>
> This patch implements dynamically recreating a macvtap interface when<br>
> the underlying network device reappears, and reconnecting it to the<br>
> still-running qemu process.<br>
> <br>
> It's a total hack job: after starting qemu, it spawns a thread</font></tt>
<br>
<br><tt><font size=2>hehe :-)</font></tt>
<br><tt><font size=2><br>
> that wakes up once per second and attempts to create a new macvtap<br>
> interface for each defined direct interface.  If the macvtap</font></tt>
<br>
<br><tt><font size=2>What if you collected all macvtap devices' names,
MAC addresses and whatever else is needed to recreate the devices in a
map/dictionary/list (using virHashMap for example) where</font></tt>
<br>
<br><tt><font size=2>- the map entry gets added upon creation of the macvtap
device</font></tt>
<br><tt><font size=2>- the map entry gets delete upon destruction of the
macvtap device</font></tt>
<br><tt><font size=2>- the thread walks the list of macvtap device names
and reads and compares for example their MAC addresses using an ioctl and
if the ioctl fails tries to recreate them until successful?</font></tt>
<br>
<br><tt><font size=2>   Stefan</font></tt>
<br>
<br><tt><font size=2><br>
> interface already exists (because it never disappeared) or can't<br>
> be created (because the underlying network device doesn't exist),<br>
> this is a no-op.  Only when the underlying network device reappears<br>
> does the reconnection complete.<br>
> <br>
> At minimum, this should be done without the once-per-second polling.<br>
> <br>
> Index: libvirt-0.7.7/src/qemu/qemu_driver.c<br>
> ===================================================================<br>
> --- libvirt-0.7.7.orig/src/qemu/qemu_driver.c<br>
> +++ libvirt-0.7.7/src/qemu/qemu_driver.c<br>
> @@ -83,6 +83,7 @@<br>
>  #include "xml.h"<br>
>  #include "cpu/cpu.h"<br>
>  #include "macvtap.h"<br>
> +#include "threads.h"<br>
>  <br>
>  <br>
>  #define VIR_FROM_THIS VIR_FROM_QEMU<br>
> @@ -107,6 +108,8 @@ struct _qemuDomainObjPrivate {<br>
>  <br>
>      qemuDomainPCIAddressSetPtr pciaddrs;<br>
>      int persistentAddrs;<br>
> +<br>
> +    pthread_t reconnectNetBackendsThread;<br>
>  };<br>
>  <br>
>  static int qemudShutdown(void);<br>
> @@ -151,6 +154,8 @@ static int qemudDomainDisconnectNetBacke<br>
>                    
                     
  virDomainObjPtr vm,<br>
>                    
                     
  virDomainNetDefPtr net);<br>
>  <br>
> +static void *qemudDomainReconnectNetBackends(void *opaque);<br>
> +<br>
>  static struct qemud_driver *qemu_driver = NULL;<br>
>  <br>
>  <br>
> @@ -2697,6 +2702,7 @@ static int qemudStartVMDaemon(virConnect<br>
>      char *pidfile = NULL;<br>
>      int logfile;<br>
>      qemuDomainObjPrivatePtr priv = vm->privateData;<br>
> +    pthread_attr_t threadAttr;<br>
>  <br>
>      struct qemudHookData hookData;<br>
>      hookData.conn = conn;<br>
> @@ -2950,6 +2956,15 @@ static int qemudStartVMDaemon(virConnect<br>
>      if (virDomainSaveStatus(driver->caps, driver->stateDir,
vm) < 0)<br>
>          goto abort;<br>
>  <br>
> +    pthread_attr_init(&threadAttr);<br>
> +    pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED);<br>
> +    if (pthread_create(&priv->reconnectNetBackendsThread,
&threadAttr,<br>
> +                    
  qemudDomainReconnectNetBackends, vm->def->uuid)) {<br>
> +        pthread_attr_destroy(&threadAttr);<br>
> +        goto abort;<br>
> +    }<br>
> +    pthread_attr_destroy(&threadAttr);<br>
> +<br>
>      return 0;<br>
>  <br>
>  cleanup:<br>
> @@ -6248,7 +6263,7 @@ static int qemudDomainConnectNetBackend(<br>
>                    
                     unsigned
long long qemuCmdFlags)<br>
>  {<br>
>      qemuDomainObjPrivatePtr priv = vm->privateData;<br>
> -    char *tapfd_name = NULL;<br>
> +    char *hostnet_name = NULL;<br>
>      int tapfd = -1;<br>
>      char *netstr = NULL;<br>
>      int ret = -1;<br>
> @@ -6286,27 +6301,30 @@ static int qemudDomainConnectNetBackend(<br>
>      if (tapfd < 0)<br>
>          return -1;<br>
>  <br>
> -    if (virAsprintf(&tapfd_name, "fd-%s",
net->info.alias) < 0) {<br>
> +    if (virAsprintf(&hostnet_name, "host%s",
net->info.alias) < 0) {<br>
>          virReportOOMError();<br>
>          close(tapfd);<br>
>          return -1;<br>
>      }<br>
>  <br>
>      if (!(netstr = qemuBuildHostNetStr(net, ' ',<br>
> -                    
                  vlan, tapfd_name)))
{<br>
> -        VIR_FREE(tapfd_name);<br>
> +                    
                  vlan, hostnet_name)))
{<br>
> +        VIR_FREE(hostnet_name);<br>
>          close(tapfd);<br>
>          return -1;<br>
>      }<br>
>  <br>
>      qemuDomainObjEnterMonitorWithDriver(driver, vm);<br>
>  <br>
> -    if (qemuMonitorSendFileHandle(priv->mon, tapfd_name,
tapfd) < 0)<br>
> +    if (qemuMonitorRemoveHostNetwork(priv->mon, vlan,
hostnet_name) < 0)<br>
> +        VIR_INFO0("Did not remove existing
host network");<br>
> +<br>
> +    if (qemuMonitorSendFileHandle(priv->mon, hostnet_name,
tapfd) < 0)<br>
>          goto out;<br>
>  <br>
>      if (qemuMonitorAddHostNetwork(priv->mon, netstr)
< 0) {<br>
> -        if (qemuMonitorCloseFileHandle(priv->mon,
tapfd_name) < 0)<br>
> -            VIR_WARN(_("Failed
to close tapfd with '%s'"), tapfd_name);<br>
> +        if (qemuMonitorCloseFileHandle(priv->mon,
hostnet_name) < 0)<br>
> +            VIR_WARN(_("Failed
to close tapfd with '%s'"), hostnet_name);<br>
>          goto out;<br>
>      }<br>
>  <br>
> @@ -6315,7 +6333,7 @@ static int qemudDomainConnectNetBackend(<br>
>  out:<br>
>      qemuDomainObjExitMonitorWithDriver(driver, vm);<br>
>      VIR_FREE(netstr);<br>
> -    VIR_FREE(tapfd_name);<br>
> +    VIR_FREE(hostnet_name);<br>
>      close(tapfd);<br>
>      return ret;<br>
>  }<br>
> @@ -9339,6 +9357,73 @@ out:<br>
>      return ret;<br>
>  }<br>
>  <br>
> +static void *qemudDomainReconnectNetBackends(void *opaque)<br>
> +{<br>
> +    virConnectPtr conn = virConnectOpen("qemu:///system");<br>
> +    struct qemud_driver *driver = conn->privateData;<br>
> +    const unsigned char *uuid = opaque;<br>
> +<br>
> +    while (1) {<br>
> +        virDomainObjPtr vm;<br>
> +        unsigned long long qemuCmdFlags;<br>
> +        int i;<br>
> +<br>
> +        usleep(1000000);<br>
> +<br>
> +        VIR_DEBUG(_("qemuDomainReconnectNetBackends
(%p %p)"),<br>
> +                  conn,
driver);<br>
> +<br>
> +        qemuDriverLock(driver);<br>
> +        vm = virDomainFindByUUID(&driver->domains,
uuid);<br>
> +        if (!vm) {<br>
> +            VIR_DEBUG0("qemuDomainReconnectNetBackends
done");<br>
> +            qemuDriverUnlock(driver);<br>
> +            break;<br>
> +        }<br>
> +<br>
> +        VIR_DEBUG(_("qemuDomainReconnectNetBackends
(%s)"),<br>
> +                  vm->def->name);<br>
> +<br>
> +        if (qemudExtractVersionInfo(vm->def->emulator,<br>
> +                    
               NULL,<br>
> +                    
               &qemuCmdFlags)
< 0)<br>
> +            goto cleanup;<br>
> +<br>
> +        if (qemuDomainObjBeginJobWithDriver(driver,
vm) < 0)<br>
> +            goto cleanup;<br>
> +<br>
> +        if (!virDomainObjIsActive(vm))<br>
> +            goto endjob;<br>
> +<br>
> +        for (i = 0 ; i < vm->def->nnets
; i++) {<br>
> +            virDomainNetDefPtr net
= vm->def->nets[i];<br>
> +<br>
> +            if (net->type != VIR_DOMAIN_NET_TYPE_DIRECT)<br>
> +                continue;<br>
> +<br>
> +            if (qemudDomainConnectNetBackend(conn,
driver, vm,<br>
> +                    
                     
  net, qemuCmdFlags) == 0) {<br>
> +                VIR_WARN(_("Reconnected
interface %s (%s) for domain %s"),<br>
> +                    
      net->data.direct.linkdev, net->ifname, <br>
> vm->def->name);<br>
> +            } else {<br>
> +                VIR_DEBUG(_("Unable
to reconnect interface %s for <br>
> domain %s"),<br>
> +                    
      net->data.direct.linkdev, vm->def->name);<br>
> +            }<br>
> +        }<br>
> +<br>
> +    endjob:<br>
> +        if (qemuDomainObjEndJob(vm) == 0)<br>
> +            vm = NULL;<br>
> +<br>
> +    cleanup:<br>
> +        if (vm)<br>
> +            virDomainObjUnlock(vm);<br>
> +        qemuDriverUnlock(driver);<br>
> +    }<br>
> +<br>
> +    return NULL;<br>
> +}<br>
> +<br>
>  static int<br>
>  qemuCPUCompare(virConnectPtr conn,<br>
>                 const char
*xmlDesc,<br>
> Index: libvirt-0.7.7/src/util/macvtap.c<br>
> ===================================================================<br>
> --- libvirt-0.7.7.orig/src/util/macvtap.c<br>
> +++ libvirt-0.7.7/src/util/macvtap.c<br>
> @@ -215,10 +215,12 @@ getIfIndex(virConnectPtr conn,<br>
>      if (ioctl(fd, SIOCGIFINDEX, &ifreq) >=
0)<br>
>          *idx = ifreq.ifr_ifindex;<br>
>      else {<br>
> +#if 0<br>
>          if (conn)<br>
>              ReportError(conn,
VIR_ERR_INTERNAL_ERROR,<br>
>                    
     _("interface %s does not exist"),<br>
>                    
     ifname);<br>
> +#endif<br>
>          rc = ENODEV;<br>
>      }<br>
>  <br>
> @@ -747,11 +749,13 @@ create_name:<br>
>  <br>
>      rc = ifUp(cr_ifname, 1);<br>
>      if (rc != 0) {<br>
> +#if 0<br>
>          virReportSystemError(errno,<br>
>                    
          _("cannot 'up' interface %s --
another "<br>
>                    
          "macvtap device may be 'up' and
have the same "<br>
>                    
          "MAC address"),<br>
>                    
          cr_ifname);<br>
> +#endif<br>
>          rc = -1;<br>
>          goto link_del_exit;<br>
>      }<br>
> Index: libvirt-0.7.7/src/qemu/qemu_conf.c<br>
> ===================================================================<br>
> --- libvirt-0.7.7.orig/src/qemu/qemu_conf.c<br>
> +++ libvirt-0.7.7/src/qemu/qemu_conf.c<br>
> @@ -3854,18 +3854,21 @@ int qemudBuildCommandLine(virConnectPtr <br>
>                    
                     
         net->data.direct.linkdev,<br>
>                    
                     
         net->data.direct.mode,<br>
>                    
                     
         qemuCmdFlags);<br>
> -                if (tapfd
< 0)<br>
> -                    goto
error;<br>
> -<br>
> -                if (VIR_REALLOC_N(*tapfds,
(*ntapfds)+1) < 0) {<br>
> -                    close(tapfd);<br>
> -                    goto
no_memory;<br>
> -                }<br>
> +                if (tapfd
< 0) {<br>
> +                    VIR_WARN(_("Unable
to connect interface %s for <br>
> domain %s"),<br>
> +                    
        net->data.direct.linkdev, def->name);<br>
> +                    tapfd_name[0]
= 0;<br>
> +                } else {<br>
> +                    if
(VIR_REALLOC_N(*tapfds, (*ntapfds)+1) < 0) {<br>
> +                    
   close(tapfd);<br>
> +                    
   goto no_memory;<br>
> +                    }<br>
>  <br>
> -                (*tapfds)[(*ntapfds)++]
= tapfd;<br>
> +                    (*tapfds)[(*ntapfds)++]
= tapfd;<br>
>  <br>
> -                if (snprintf(tapfd_name,
sizeof(tapfd_name), "%d", <br>
> tapfd) >= sizeof(tapfd_name))<br>
> -                    goto
no_memory;<br>
> +                    if
(snprintf(tapfd_name, sizeof(tapfd_name), "%<br>
> d", tapfd) >= sizeof(tapfd_name))<br>
> +                    
   goto no_memory;<br>
> +                }<br>
>              }<br>
>  <br>
>              /* Possible combinations:<br>
> @@ -3876,8 +3879,9 @@ int qemudBuildCommandLine(virConnectPtr <br>
>               *<br>
>               * NB, no support
for -netdev without use of -device<br>
>               */<br>
> -            if ((qemuCmdFlags &
QEMUD_CMD_FLAG_NETDEV) &&<br>
> -                (qemuCmdFlags
& QEMUD_CMD_FLAG_DEVICE)) {<br>
> +            if ((tapfd_name[0] != 0)
&&<br>
> +                ((qemuCmdFlags
& QEMUD_CMD_FLAG_NETDEV) &&<br>
> +                 (qemuCmdFlags
& QEMUD_CMD_FLAG_DEVICE))) {<br>
>                  ADD_ARG_LIT("-netdev");<br>
>                  if (!(host
= qemuBuildHostNetStr(net, ',',<br>
>                    
                     
        vlan, tapfd_name)))<br>
> @@ -3895,8 +3899,9 @@ int qemudBuildCommandLine(virConnectPtr <br>
>                    
 goto error;<br>
>                  ADD_ARG(nic);<br>
>              }<br>
> -            if (!((qemuCmdFlags &
QEMUD_CMD_FLAG_NETDEV) &&<br>
> -                  (qemuCmdFlags
& QEMUD_CMD_FLAG_DEVICE))) {<br>
> +            if ((tapfd_name[0] != 0)
&&<br>
> +                (!((qemuCmdFlags
& QEMUD_CMD_FLAG_NETDEV) &&<br>
> +                   (qemuCmdFlags
& QEMUD_CMD_FLAG_DEVICE)))) {<br>
>                  ADD_ARG_LIT("-net");<br>
>                  if (!(host
= qemuBuildHostNetStr(net, ',',<br>
>                    
                     
        vlan, tapfd_name)))<br>
> <br>
> <br>
> --<br>
> libvir-list mailing list<br>
> libvir-list@redhat.com<br>
> </font></tt><a href="https://www.redhat.com/mailman/listinfo/libvir-list"><tt><font size=2>https://www.redhat.com/mailman/listinfo/libvir-list</font></tt></a><tt><font size=2><br>
</font></tt>