diff mbox

[PATCHv4] net: delay freeing peer host device

Message ID 20101006160405.GA12265@redhat.com
State New
Headers show

Commit Message

Michael S. Tsirkin Oct. 6, 2010, 4:04 p.m. UTC
With -netdev, virtio devices present offload
features to guest, depending on the backend used.
Thus, removing host netdev peer while guest is
active leads to guest-visible inconsistency and/or crashes.

As a solution, while guest (NIC) peer device exists,
we prevent the host peer from being deleted.
This patch does this by adding peer_deleted flag in nic state:
if host device is going away while guest device
is around, set this flag and keep a shell of
the host device around for as long as guest device exists.

The link is put down so all packets will get discarded.

At the moment, management can detect that device deletion
is delayed by doing info net. As a next step, we shall add
commands that control hotplug/unplug without
removing the device, and an event to report that
guest has responded to the hotplug event.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---

 Changes from v3:
   make sure ->cleanup is called for NICs
 Changes from v2:                                                                                                
   fix crash on repeated netdev_del                                                                              

 net.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
 net.h |    1 +
 2 files changed, 43 insertions(+), 7 deletions(-)

Comments

Alex Williamson Oct. 6, 2010, 4:15 p.m. UTC | #1
On Wed, 2010-10-06 at 18:04 +0200, Michael S. Tsirkin wrote:
> With -netdev, virtio devices present offload
> features to guest, depending on the backend used.
> Thus, removing host netdev peer while guest is
> active leads to guest-visible inconsistency and/or crashes.
> 
> As a solution, while guest (NIC) peer device exists,
> we prevent the host peer from being deleted.
> This patch does this by adding peer_deleted flag in nic state:
> if host device is going away while guest device
> is around, set this flag and keep a shell of
> the host device around for as long as guest device exists.
> 
> The link is put down so all packets will get discarded.
> 
> At the moment, management can detect that device deletion
> is delayed by doing info net. As a next step, we shall add
> commands that control hotplug/unplug without
> removing the device, and an event to report that
> guest has responded to the hotplug event.
> 
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

Acked-by: Alex Williamson <alex.williamson@redhat.com>

> ---
> 
>  Changes from v3:
>    make sure ->cleanup is called for NICs
>  Changes from v2:                                                                                                
>    fix crash on repeated netdev_del                                                                              
> 
>  net.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
>  net.h |    1 +
>  2 files changed, 43 insertions(+), 7 deletions(-)
> 
> diff --git a/net.c b/net.c
> index 3d0fde7..ed74c7f 100644
> --- a/net.c
> +++ b/net.c
> @@ -281,29 +281,64 @@ NICState *qemu_new_nic(NetClientInfo *info,
>      return nic;
>  }
>  
> -void qemu_del_vlan_client(VLANClientState *vc)
> +static void qemu_cleanup_vlan_client(VLANClientState *vc)
>  {
>      if (vc->vlan) {
>          QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
>      } else {
> -        if (vc->send_queue) {
> -            qemu_del_net_queue(vc->send_queue);
> -        }
>          QTAILQ_REMOVE(&non_vlan_clients, vc, next);
> -        if (vc->peer) {
> -            vc->peer->peer = NULL;
> -        }
>      }
>  
>      if (vc->info->cleanup) {
>          vc->info->cleanup(vc);
>      }
> +}
>  
> +static void qemu_free_vlan_client(VLANClientState *vc)
> +{
> +    if (!vc->vlan) {
> +        if (vc->send_queue) {
> +            qemu_del_net_queue(vc->send_queue);
> +        }
> +        if (vc->peer) {
> +            vc->peer->peer = NULL;
> +        }
> +    }
>      qemu_free(vc->name);
>      qemu_free(vc->model);
>      qemu_free(vc);
>  }
>  
> +void qemu_del_vlan_client(VLANClientState *vc)
> +{
> +    /* If there is a peer NIC, delete and cleanup client, but do not free. */
> +    if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
> +        NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
> +        if (nic->peer_deleted) {
> +            return;
> +        }
> +        nic->peer_deleted = true;
> +        /* Let NIC know peer is gone. */
> +        vc->peer->link_down = true;
> +        if (vc->peer->info->link_status_changed) {
> +            vc->peer->info->link_status_changed(vc->peer);
> +        }
> +        qemu_cleanup_vlan_client(vc);
> +        return;
> +    }
> +
> +    /* If this is a peer NIC and peer has already been deleted, free it now. */
> +    if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
> +        NICState *nic = DO_UPCAST(NICState, nc, vc);
> +        if (nic->peer_deleted) {
> +            qemu_free_vlan_client(vc->peer);
> +        }
> +    }
> +
> +    qemu_cleanup_vlan_client(vc);
> +    qemu_free_vlan_client(vc);
> +}
> +
>  VLANClientState *
>  qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
>                                const char *client_str)
> diff --git a/net.h b/net.h
> index 518cf9c..44c31a9 100644
> --- a/net.h
> +++ b/net.h
> @@ -72,6 +72,7 @@ typedef struct NICState {
>      VLANClientState nc;
>      NICConf *conf;
>      void *opaque;
> +    bool peer_deleted;
>  } NICState;
>  
>  struct VLANState {
Anthony Liguori Oct. 6, 2010, 4:35 p.m. UTC | #2
On 10/06/2010 11:04 AM, Michael S. Tsirkin wrote:
> With -netdev, virtio devices present offload
> features to guest, depending on the backend used.
> Thus, removing host netdev peer while guest is
> active leads to guest-visible inconsistency and/or crashes.
>
> As a solution, while guest (NIC) peer device exists,
> we prevent the host peer from being deleted.
> This patch does this by adding peer_deleted flag in nic state:
> if host device is going away while guest device
> is around, set this flag and keep a shell of
> the host device around for as long as guest device exists.
>
> The link is put down so all packets will get discarded.
>
> At the moment, management can detect that device deletion
> is delayed by doing info net. As a next step, we shall add
> commands that control hotplug/unplug without
> removing the device, and an event to report that
> guest has responded to the hotplug event.
>
> Signed-off-by: Michael S. Tsirkin<mst@redhat.com>
>    

I don't think this is a very good idea.  Making netdev_del conditionally 
succeed is going to break management tools and is very non-intuitive.

Regards,

Anthony Liguori

> ---
>
>   Changes from v3:
>     make sure ->cleanup is called for NICs
>   Changes from v2:
>     fix crash on repeated netdev_del
>
>   net.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
>   net.h |    1 +
>   2 files changed, 43 insertions(+), 7 deletions(-)
>
> diff --git a/net.c b/net.c
> index 3d0fde7..ed74c7f 100644
> --- a/net.c
> +++ b/net.c
> @@ -281,29 +281,64 @@ NICState *qemu_new_nic(NetClientInfo *info,
>       return nic;
>   }
>
> -void qemu_del_vlan_client(VLANClientState *vc)
> +static void qemu_cleanup_vlan_client(VLANClientState *vc)
>   {
>       if (vc->vlan) {
>           QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
>       } else {
> -        if (vc->send_queue) {
> -            qemu_del_net_queue(vc->send_queue);
> -        }
>           QTAILQ_REMOVE(&non_vlan_clients, vc, next);
> -        if (vc->peer) {
> -            vc->peer->peer = NULL;
> -        }
>       }
>
>       if (vc->info->cleanup) {
>           vc->info->cleanup(vc);
>       }
> +}
>
> +static void qemu_free_vlan_client(VLANClientState *vc)
> +{
> +    if (!vc->vlan) {
> +        if (vc->send_queue) {
> +            qemu_del_net_queue(vc->send_queue);
> +        }
> +        if (vc->peer) {
> +            vc->peer->peer = NULL;
> +        }
> +    }
>       qemu_free(vc->name);
>       qemu_free(vc->model);
>       qemu_free(vc);
>   }
>
> +void qemu_del_vlan_client(VLANClientState *vc)
> +{
> +    /* If there is a peer NIC, delete and cleanup client, but do not free. */
> +    if (!vc->vlan&&  vc->peer&&  vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
> +        NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
> +        if (nic->peer_deleted) {
> +            return;
> +        }
> +        nic->peer_deleted = true;
> +        /* Let NIC know peer is gone. */
> +        vc->peer->link_down = true;
> +        if (vc->peer->info->link_status_changed) {
> +            vc->peer->info->link_status_changed(vc->peer);
> +        }
> +        qemu_cleanup_vlan_client(vc);
> +        return;
> +    }
> +
> +    /* If this is a peer NIC and peer has already been deleted, free it now. */
> +    if (!vc->vlan&&  vc->peer&&  vc->info->type == NET_CLIENT_TYPE_NIC) {
> +        NICState *nic = DO_UPCAST(NICState, nc, vc);
> +        if (nic->peer_deleted) {
> +            qemu_free_vlan_client(vc->peer);
> +        }
> +    }
> +
> +    qemu_cleanup_vlan_client(vc);
> +    qemu_free_vlan_client(vc);
> +}
> +
>   VLANClientState *
>   qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
>                                 const char *client_str)
> diff --git a/net.h b/net.h
> index 518cf9c..44c31a9 100644
> --- a/net.h
> +++ b/net.h
> @@ -72,6 +72,7 @@ typedef struct NICState {
>       VLANClientState nc;
>       NICConf *conf;
>       void *opaque;
> +    bool peer_deleted;
>   } NICState;
>
>   struct VLANState {
>
Michael S. Tsirkin Oct. 6, 2010, 4:58 p.m. UTC | #3
On Wed, Oct 06, 2010 at 11:35:08AM -0500, Anthony Liguori wrote:
> On 10/06/2010 11:04 AM, Michael S. Tsirkin wrote:
> >With -netdev, virtio devices present offload
> >features to guest, depending on the backend used.
> >Thus, removing host netdev peer while guest is
> >active leads to guest-visible inconsistency and/or crashes.
> >
> >As a solution, while guest (NIC) peer device exists,
> >we prevent the host peer from being deleted.
> >This patch does this by adding peer_deleted flag in nic state:
> >if host device is going away while guest device
> >is around, set this flag and keep a shell of
> >the host device around for as long as guest device exists.
> >
> >The link is put down so all packets will get discarded.
> >
> >At the moment, management can detect that device deletion
> >is delayed by doing info net. As a next step, we shall add
> >commands that control hotplug/unplug without
> >removing the device, and an event to report that
> >guest has responded to the hotplug event.
> >
> >Signed-off-by: Michael S. Tsirkin<mst@redhat.com>
> 
> I don't think this is a very good idea.  Making netdev_del
> conditionally succeed is going to break management tools and is very
> non-intuitive.
> 
> Regards,
> 
> Anthony Liguori

But this is not what this does. With this patch netdev_del always
succeeds, and clears the device state leaving just an empty shell around.
The shell goes away together with the NIC.

> >---
> >
> >  Changes from v3:
> >    make sure ->cleanup is called for NICs
> >  Changes from v2:
> >    fix crash on repeated netdev_del
> >
> >  net.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
> >  net.h |    1 +
> >  2 files changed, 43 insertions(+), 7 deletions(-)
> >
> >diff --git a/net.c b/net.c
> >index 3d0fde7..ed74c7f 100644
> >--- a/net.c
> >+++ b/net.c
> >@@ -281,29 +281,64 @@ NICState *qemu_new_nic(NetClientInfo *info,
> >      return nic;
> >  }
> >
> >-void qemu_del_vlan_client(VLANClientState *vc)
> >+static void qemu_cleanup_vlan_client(VLANClientState *vc)
> >  {
> >      if (vc->vlan) {
> >          QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
> >      } else {
> >-        if (vc->send_queue) {
> >-            qemu_del_net_queue(vc->send_queue);
> >-        }
> >          QTAILQ_REMOVE(&non_vlan_clients, vc, next);
> >-        if (vc->peer) {
> >-            vc->peer->peer = NULL;
> >-        }
> >      }
> >
> >      if (vc->info->cleanup) {
> >          vc->info->cleanup(vc);
> >      }
> >+}
> >
> >+static void qemu_free_vlan_client(VLANClientState *vc)
> >+{
> >+    if (!vc->vlan) {
> >+        if (vc->send_queue) {
> >+            qemu_del_net_queue(vc->send_queue);
> >+        }
> >+        if (vc->peer) {
> >+            vc->peer->peer = NULL;
> >+        }
> >+    }
> >      qemu_free(vc->name);
> >      qemu_free(vc->model);
> >      qemu_free(vc);
> >  }
> >
> >+void qemu_del_vlan_client(VLANClientState *vc)
> >+{
> >+    /* If there is a peer NIC, delete and cleanup client, but do not free. */
> >+    if (!vc->vlan&&  vc->peer&&  vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
> >+        NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
> >+        if (nic->peer_deleted) {
> >+            return;
> >+        }
> >+        nic->peer_deleted = true;
> >+        /* Let NIC know peer is gone. */
> >+        vc->peer->link_down = true;
> >+        if (vc->peer->info->link_status_changed) {
> >+            vc->peer->info->link_status_changed(vc->peer);
> >+        }
> >+        qemu_cleanup_vlan_client(vc);
> >+        return;
> >+    }
> >+
> >+    /* If this is a peer NIC and peer has already been deleted, free it now. */
> >+    if (!vc->vlan&&  vc->peer&&  vc->info->type == NET_CLIENT_TYPE_NIC) {
> >+        NICState *nic = DO_UPCAST(NICState, nc, vc);
> >+        if (nic->peer_deleted) {
> >+            qemu_free_vlan_client(vc->peer);
> >+        }
> >+    }
> >+
> >+    qemu_cleanup_vlan_client(vc);
> >+    qemu_free_vlan_client(vc);
> >+}
> >+
> >  VLANClientState *
> >  qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
> >                                const char *client_str)
> >diff --git a/net.h b/net.h
> >index 518cf9c..44c31a9 100644
> >--- a/net.h
> >+++ b/net.h
> >@@ -72,6 +72,7 @@ typedef struct NICState {
> >      VLANClientState nc;
> >      NICConf *conf;
> >      void *opaque;
> >+    bool peer_deleted;
> >  } NICState;
> >
> >  struct VLANState {
Michael S. Tsirkin Oct. 6, 2010, 5:09 p.m. UTC | #4
On Wed, Oct 06, 2010 at 12:13:35PM -0500, Anthony Liguori wrote:
> On 10/06/2010 11:58 AM, Michael S. Tsirkin wrote:
> >On Wed, Oct 06, 2010 at 11:35:08AM -0500, Anthony Liguori wrote:
> >>On 10/06/2010 11:04 AM, Michael S. Tsirkin wrote:
> >>>With -netdev, virtio devices present offload
> >>>features to guest, depending on the backend used.
> >>>Thus, removing host netdev peer while guest is
> >>>active leads to guest-visible inconsistency and/or crashes.
> >>>
> >>>As a solution, while guest (NIC) peer device exists,
> >>>we prevent the host peer from being deleted.
> >>>This patch does this by adding peer_deleted flag in nic state:
> >>>if host device is going away while guest device
> >>>is around, set this flag and keep a shell of
> >>>the host device around for as long as guest device exists.
> >>>
> >>>The link is put down so all packets will get discarded.
> >>>
> >>>At the moment, management can detect that device deletion
> >>>is delayed by doing info net. As a next step, we shall add
> >>>commands that control hotplug/unplug without
> >>>removing the device, and an event to report that
> >>>guest has responded to the hotplug event.
> >>>
> >>>Signed-off-by: Michael S. Tsirkin<mst@redhat.com>
> >>I don't think this is a very good idea.  Making netdev_del
> >>conditionally succeed is going to break management tools and is very
> >>non-intuitive.
> >>
> >>Regards,
> >>
> >>Anthony Liguori
> >But this is not what this does. With this patch netdev_del always
> >succeeds, and clears the device state leaving just an empty shell around.
> >The shell goes away together with the NIC.
> 
> Sorry, I confused myself.  I'm a little confused by the statement:
> 
> "At the moment, management can detect that device deletion is delayed by doing info net."
> 
> Is this still the case since you remove the netdev from the list?
> 
> Regards,
> 
> Anthony Liguori

The device in question here is the PCI device removed with device_del.
The unplug there is asynchronous and I did not change this.

> 
> >>>---
> >>>
> >>>  Changes from v3:
> >>>    make sure ->cleanup is called for NICs
> >>>  Changes from v2:
> >>>    fix crash on repeated netdev_del
> >>>
> >>>  net.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
> >>>  net.h |    1 +
> >>>  2 files changed, 43 insertions(+), 7 deletions(-)
> >>>
> >>>diff --git a/net.c b/net.c
> >>>index 3d0fde7..ed74c7f 100644
> >>>--- a/net.c
> >>>+++ b/net.c
> >>>@@ -281,29 +281,64 @@ NICState *qemu_new_nic(NetClientInfo *info,
> >>>      return nic;
> >>>  }
> >>>
> >>>-void qemu_del_vlan_client(VLANClientState *vc)
> >>>+static void qemu_cleanup_vlan_client(VLANClientState *vc)
> >>>  {
> >>>      if (vc->vlan) {
> >>>          QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
> >>>      } else {
> >>>-        if (vc->send_queue) {
> >>>-            qemu_del_net_queue(vc->send_queue);
> >>>-        }
> >>>          QTAILQ_REMOVE(&non_vlan_clients, vc, next);
> >>>-        if (vc->peer) {
> >>>-            vc->peer->peer = NULL;
> >>>-        }
> >>>      }
> >>>
> >>>      if (vc->info->cleanup) {
> >>>          vc->info->cleanup(vc);
> >>>      }
> >>>+}
> >>>
> >>>+static void qemu_free_vlan_client(VLANClientState *vc)
> >>>+{
> >>>+    if (!vc->vlan) {
> >>>+        if (vc->send_queue) {
> >>>+            qemu_del_net_queue(vc->send_queue);
> >>>+        }
> >>>+        if (vc->peer) {
> >>>+            vc->peer->peer = NULL;
> >>>+        }
> >>>+    }
> >>>      qemu_free(vc->name);
> >>>      qemu_free(vc->model);
> >>>      qemu_free(vc);
> >>>  }
> >>>
> >>>+void qemu_del_vlan_client(VLANClientState *vc)
> >>>+{
> >>>+    /* If there is a peer NIC, delete and cleanup client, but do not free. */
> >>>+    if (!vc->vlan&&   vc->peer&&   vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
> >>>+        NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
> >>>+        if (nic->peer_deleted) {
> >>>+            return;
> >>>+        }
> >>>+        nic->peer_deleted = true;
> >>>+        /* Let NIC know peer is gone. */
> >>>+        vc->peer->link_down = true;
> >>>+        if (vc->peer->info->link_status_changed) {
> >>>+            vc->peer->info->link_status_changed(vc->peer);
> >>>+        }
> >>>+        qemu_cleanup_vlan_client(vc);
> >>>+        return;
> >>>+    }
> >>>+
> >>>+    /* If this is a peer NIC and peer has already been deleted, free it now. */
> >>>+    if (!vc->vlan&&   vc->peer&&   vc->info->type == NET_CLIENT_TYPE_NIC) {
> >>>+        NICState *nic = DO_UPCAST(NICState, nc, vc);
> >>>+        if (nic->peer_deleted) {
> >>>+            qemu_free_vlan_client(vc->peer);
> >>>+        }
> >>>+    }
> >>>+
> >>>+    qemu_cleanup_vlan_client(vc);
> >>>+    qemu_free_vlan_client(vc);
> >>>+}
> >>>+
> >>>  VLANClientState *
> >>>  qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
> >>>                                const char *client_str)
> >>>diff --git a/net.h b/net.h
> >>>index 518cf9c..44c31a9 100644
> >>>--- a/net.h
> >>>+++ b/net.h
> >>>@@ -72,6 +72,7 @@ typedef struct NICState {
> >>>      VLANClientState nc;
> >>>      NICConf *conf;
> >>>      void *opaque;
> >>>+    bool peer_deleted;
> >>>  } NICState;
> >>>
> >>>  struct VLANState {
Anthony Liguori Oct. 6, 2010, 5:13 p.m. UTC | #5
On 10/06/2010 11:58 AM, Michael S. Tsirkin wrote:
> On Wed, Oct 06, 2010 at 11:35:08AM -0500, Anthony Liguori wrote:
>    
>> On 10/06/2010 11:04 AM, Michael S. Tsirkin wrote:
>>      
>>> With -netdev, virtio devices present offload
>>> features to guest, depending on the backend used.
>>> Thus, removing host netdev peer while guest is
>>> active leads to guest-visible inconsistency and/or crashes.
>>>
>>> As a solution, while guest (NIC) peer device exists,
>>> we prevent the host peer from being deleted.
>>> This patch does this by adding peer_deleted flag in nic state:
>>> if host device is going away while guest device
>>> is around, set this flag and keep a shell of
>>> the host device around for as long as guest device exists.
>>>
>>> The link is put down so all packets will get discarded.
>>>
>>> At the moment, management can detect that device deletion
>>> is delayed by doing info net. As a next step, we shall add
>>> commands that control hotplug/unplug without
>>> removing the device, and an event to report that
>>> guest has responded to the hotplug event.
>>>
>>> Signed-off-by: Michael S. Tsirkin<mst@redhat.com>
>>>        
>> I don't think this is a very good idea.  Making netdev_del
>> conditionally succeed is going to break management tools and is very
>> non-intuitive.
>>
>> Regards,
>>
>> Anthony Liguori
>>      
> But this is not what this does. With this patch netdev_del always
> succeeds, and clears the device state leaving just an empty shell around.
> The shell goes away together with the NIC.
>    

Sorry, I confused myself.  I'm a little confused by the statement:

"At the moment, management can detect that device deletion is delayed by doing info net."


Is this still the case since you remove the netdev from the list?

Regards,

Anthony Liguori

>>> ---
>>>
>>>   Changes from v3:
>>>     make sure ->cleanup is called for NICs
>>>   Changes from v2:
>>>     fix crash on repeated netdev_del
>>>
>>>   net.c |   49 ++++++++++++++++++++++++++++++++++++++++++-------
>>>   net.h |    1 +
>>>   2 files changed, 43 insertions(+), 7 deletions(-)
>>>
>>> diff --git a/net.c b/net.c
>>> index 3d0fde7..ed74c7f 100644
>>> --- a/net.c
>>> +++ b/net.c
>>> @@ -281,29 +281,64 @@ NICState *qemu_new_nic(NetClientInfo *info,
>>>       return nic;
>>>   }
>>>
>>> -void qemu_del_vlan_client(VLANClientState *vc)
>>> +static void qemu_cleanup_vlan_client(VLANClientState *vc)
>>>   {
>>>       if (vc->vlan) {
>>>           QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
>>>       } else {
>>> -        if (vc->send_queue) {
>>> -            qemu_del_net_queue(vc->send_queue);
>>> -        }
>>>           QTAILQ_REMOVE(&non_vlan_clients, vc, next);
>>> -        if (vc->peer) {
>>> -            vc->peer->peer = NULL;
>>> -        }
>>>       }
>>>
>>>       if (vc->info->cleanup) {
>>>           vc->info->cleanup(vc);
>>>       }
>>> +}
>>>
>>> +static void qemu_free_vlan_client(VLANClientState *vc)
>>> +{
>>> +    if (!vc->vlan) {
>>> +        if (vc->send_queue) {
>>> +            qemu_del_net_queue(vc->send_queue);
>>> +        }
>>> +        if (vc->peer) {
>>> +            vc->peer->peer = NULL;
>>> +        }
>>> +    }
>>>       qemu_free(vc->name);
>>>       qemu_free(vc->model);
>>>       qemu_free(vc);
>>>   }
>>>
>>> +void qemu_del_vlan_client(VLANClientState *vc)
>>> +{
>>> +    /* If there is a peer NIC, delete and cleanup client, but do not free. */
>>> +    if (!vc->vlan&&   vc->peer&&   vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
>>> +        NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
>>> +        if (nic->peer_deleted) {
>>> +            return;
>>> +        }
>>> +        nic->peer_deleted = true;
>>> +        /* Let NIC know peer is gone. */
>>> +        vc->peer->link_down = true;
>>> +        if (vc->peer->info->link_status_changed) {
>>> +            vc->peer->info->link_status_changed(vc->peer);
>>> +        }
>>> +        qemu_cleanup_vlan_client(vc);
>>> +        return;
>>> +    }
>>> +
>>> +    /* If this is a peer NIC and peer has already been deleted, free it now. */
>>> +    if (!vc->vlan&&   vc->peer&&   vc->info->type == NET_CLIENT_TYPE_NIC) {
>>> +        NICState *nic = DO_UPCAST(NICState, nc, vc);
>>> +        if (nic->peer_deleted) {
>>> +            qemu_free_vlan_client(vc->peer);
>>> +        }
>>> +    }
>>> +
>>> +    qemu_cleanup_vlan_client(vc);
>>> +    qemu_free_vlan_client(vc);
>>> +}
>>> +
>>>   VLANClientState *
>>>   qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
>>>                                 const char *client_str)
>>> diff --git a/net.h b/net.h
>>> index 518cf9c..44c31a9 100644
>>> --- a/net.h
>>> +++ b/net.h
>>> @@ -72,6 +72,7 @@ typedef struct NICState {
>>>       VLANClientState nc;
>>>       NICConf *conf;
>>>       void *opaque;
>>> +    bool peer_deleted;
>>>   } NICState;
>>>
>>>   struct VLANState {
>>>
diff mbox

Patch

diff --git a/net.c b/net.c
index 3d0fde7..ed74c7f 100644
--- a/net.c
+++ b/net.c
@@ -281,29 +281,64 @@  NICState *qemu_new_nic(NetClientInfo *info,
     return nic;
 }
 
-void qemu_del_vlan_client(VLANClientState *vc)
+static void qemu_cleanup_vlan_client(VLANClientState *vc)
 {
     if (vc->vlan) {
         QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
     } else {
-        if (vc->send_queue) {
-            qemu_del_net_queue(vc->send_queue);
-        }
         QTAILQ_REMOVE(&non_vlan_clients, vc, next);
-        if (vc->peer) {
-            vc->peer->peer = NULL;
-        }
     }
 
     if (vc->info->cleanup) {
         vc->info->cleanup(vc);
     }
+}
 
+static void qemu_free_vlan_client(VLANClientState *vc)
+{
+    if (!vc->vlan) {
+        if (vc->send_queue) {
+            qemu_del_net_queue(vc->send_queue);
+        }
+        if (vc->peer) {
+            vc->peer->peer = NULL;
+        }
+    }
     qemu_free(vc->name);
     qemu_free(vc->model);
     qemu_free(vc);
 }
 
+void qemu_del_vlan_client(VLANClientState *vc)
+{
+    /* If there is a peer NIC, delete and cleanup client, but do not free. */
+    if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
+        NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
+        if (nic->peer_deleted) {
+            return;
+        }
+        nic->peer_deleted = true;
+        /* Let NIC know peer is gone. */
+        vc->peer->link_down = true;
+        if (vc->peer->info->link_status_changed) {
+            vc->peer->info->link_status_changed(vc->peer);
+        }
+        qemu_cleanup_vlan_client(vc);
+        return;
+    }
+
+    /* If this is a peer NIC and peer has already been deleted, free it now. */
+    if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
+        NICState *nic = DO_UPCAST(NICState, nc, vc);
+        if (nic->peer_deleted) {
+            qemu_free_vlan_client(vc->peer);
+        }
+    }
+
+    qemu_cleanup_vlan_client(vc);
+    qemu_free_vlan_client(vc);
+}
+
 VLANClientState *
 qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
                               const char *client_str)
diff --git a/net.h b/net.h
index 518cf9c..44c31a9 100644
--- a/net.h
+++ b/net.h
@@ -72,6 +72,7 @@  typedef struct NICState {
     VLANClientState nc;
     NICConf *conf;
     void *opaque;
+    bool peer_deleted;
 } NICState;
 
 struct VLANState {