diff mbox series

[RFC,v1,1/4] net: Introduce qemu_get_peer

Message ID 20200420093241.4238-2-lulu@redhat.com (mailing list archive)
State New, archived
Headers show
Series vDPA support in qemu | expand

Commit Message

Cindy Lu April 20, 2020, 9:32 a.m. UTC
This is a small function  that can get the peer from given NetClientState and queue_index

Signed-off-by: Cindy Lu <lulu@redhat.com>
---
 hw/net/vhost_net.c | 16 ++++++++++------
 include/net/net.h  |  1 +
 net/net.c          |  6 ++++++
 3 files changed, 17 insertions(+), 6 deletions(-)

Comments

Jason Wang April 21, 2020, 3:23 a.m. UTC | #1
On 2020/4/20 下午5:32, Cindy Lu wrote:
> This is a small function  that can get the peer from given NetClientState and queue_index


Unnecessary space  between 'function' and 'that'.


>
> Signed-off-by: Cindy Lu <lulu@redhat.com>


Please split this patch into two parts:

1) introduce the function
2) the actual user for this fucntion


> ---
>   hw/net/vhost_net.c | 16 ++++++++++------
>   include/net/net.h  |  1 +
>   net/net.c          |  6 ++++++
>   3 files changed, 17 insertions(+), 6 deletions(-)
>
> diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c
> index 6b82803fa7..4096d64aaf 100644
> --- a/hw/net/vhost_net.c
> +++ b/hw/net/vhost_net.c
> @@ -306,7 +306,9 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
>       BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
>       VirtioBusState *vbus = VIRTIO_BUS(qbus);
>       VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
> +    struct vhost_net *net;
>       int r, e, i;
> +    NetClientState *peer;
>   
>       if (!k->set_guest_notifiers) {
>           error_report("binding does not support guest notifiers");
> @@ -314,9 +316,9 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
>       }
>   
>       for (i = 0; i < total_queues; i++) {
> -        struct vhost_net *net;
>   
> -        net = get_vhost_net(ncs[i].peer);
> +        peer = qemu_get_peer(ncs, i);
> +        net = get_vhost_net(peer);
>           vhost_net_set_vq_index(net, i * 2);
>   
>           /* Suppress the masking guest notifiers on vhost user
> @@ -335,15 +337,16 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
>       }
>   
>       for (i = 0; i < total_queues; i++) {
> -        r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
> +        peer = qemu_get_peer(ncs, i);
> +        r = vhost_net_start_one(get_vhost_net(peer), dev);
>   
>           if (r < 0) {
>               goto err_start;
>           }
>   
> -        if (ncs[i].peer->vring_enable) {
> +        if (peer->vring_enable) {
>               /* restore vring enable state */
> -            r = vhost_set_vring_enable(ncs[i].peer, ncs[i].peer->vring_enable);
> +            r = vhost_set_vring_enable(peer, peer->vring_enable);
>   
>               if (r < 0) {
>                   goto err_start;
> @@ -355,7 +358,8 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
>   
>   err_start:
>       while (--i >= 0) {
> -        vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
> +        peer = qemu_get_peer(ncs , i);
> +        vhost_net_stop_one(get_vhost_net(peer), dev);
>       }
>       e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
>       if (e < 0) {
> diff --git a/include/net/net.h b/include/net/net.h
> index e175ba9677..0a74324ccd 100644
> --- a/include/net/net.h
> +++ b/include/net/net.h
> @@ -175,6 +175,7 @@ void hmp_info_network(Monitor *mon, const QDict *qdict);
>   void net_socket_rs_init(SocketReadState *rs,
>                           SocketReadStateFinalize *finalize,
>                           bool vnet_hdr);
> +NetClientState *qemu_get_peer(NetClientState *nc, int queue_index);
>   
>   /* NIC info */
>   
> diff --git a/net/net.c b/net/net.c
> index 84aa6d8d00..ac5080dda1 100644
> --- a/net/net.c
> +++ b/net/net.c
> @@ -324,6 +324,12 @@ void *qemu_get_nic_opaque(NetClientState *nc)
>   
>       return nic->opaque;
>   }
> +NetClientState *qemu_get_peer(NetClientState *nc, int queue_index)
> +{
> +    NetClientState *ncs  =  nc + queue_index;


Unnecessary space around '='.

Thanks


> +    assert(ncs != NULL);
> +    return ncs->peer;
> +}
>   
>   static void qemu_cleanup_net_client(NetClientState *nc)
>   {
Cindy Lu April 21, 2020, 8:10 a.m. UTC | #2
On Tue, Apr 21, 2020 at 11:23 AM Jason Wang <jasowang@redhat.com> wrote:

>
> On 2020/4/20 下午5:32, Cindy Lu wrote:
> > This is a small function  that can get the peer from given
> NetClientState and queue_index
>
>
> Unnecessary space  between 'function' and 'that'.
>
>
> >
> > Signed-off-by: Cindy Lu <lulu@redhat.com>
>
>
> Please split this patch into two parts:
>
> 1) introduce the function
> 2) the actual user for this fucntion
>
>
> sure, I will fix this problem.

> > ---
> >   hw/net/vhost_net.c | 16 ++++++++++------
> >   include/net/net.h  |  1 +
> >   net/net.c          |  6 ++++++
> >   3 files changed, 17 insertions(+), 6 deletions(-)
> >
> > diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c
> > index 6b82803fa7..4096d64aaf 100644
> > --- a/hw/net/vhost_net.c
> > +++ b/hw/net/vhost_net.c
> > @@ -306,7 +306,9 @@ int vhost_net_start(VirtIODevice *dev,
> NetClientState *ncs,
> >       BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
> >       VirtioBusState *vbus = VIRTIO_BUS(qbus);
> >       VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
> > +    struct vhost_net *net;
> >       int r, e, i;
> > +    NetClientState *peer;
> >
> >       if (!k->set_guest_notifiers) {
> >           error_report("binding does not support guest notifiers");
> > @@ -314,9 +316,9 @@ int vhost_net_start(VirtIODevice *dev,
> NetClientState *ncs,
> >       }
> >
> >       for (i = 0; i < total_queues; i++) {
> > -        struct vhost_net *net;
> >
> > -        net = get_vhost_net(ncs[i].peer);
> > +        peer = qemu_get_peer(ncs, i);
> > +        net = get_vhost_net(peer);
> >           vhost_net_set_vq_index(net, i * 2);
> >
> >           /* Suppress the masking guest notifiers on vhost user
> > @@ -335,15 +337,16 @@ int vhost_net_start(VirtIODevice *dev,
> NetClientState *ncs,
> >       }
> >
> >       for (i = 0; i < total_queues; i++) {
> > -        r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
> > +        peer = qemu_get_peer(ncs, i);
> > +        r = vhost_net_start_one(get_vhost_net(peer), dev);
> >
> >           if (r < 0) {
> >               goto err_start;
> >           }
> >
> > -        if (ncs[i].peer->vring_enable) {
> > +        if (peer->vring_enable) {
> >               /* restore vring enable state */
> > -            r = vhost_set_vring_enable(ncs[i].peer,
> ncs[i].peer->vring_enable);
> > +            r = vhost_set_vring_enable(peer, peer->vring_enable);
> >
> >               if (r < 0) {
> >                   goto err_start;
> > @@ -355,7 +358,8 @@ int vhost_net_start(VirtIODevice *dev,
> NetClientState *ncs,
> >
> >   err_start:
> >       while (--i >= 0) {
> > -        vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
> > +        peer = qemu_get_peer(ncs , i);
> > +        vhost_net_stop_one(get_vhost_net(peer), dev);
> >       }
> >       e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
> >       if (e < 0) {
> > diff --git a/include/net/net.h b/include/net/net.h
> > index e175ba9677..0a74324ccd 100644
> > --- a/include/net/net.h
> > +++ b/include/net/net.h
> > @@ -175,6 +175,7 @@ void hmp_info_network(Monitor *mon, const QDict
> *qdict);
> >   void net_socket_rs_init(SocketReadState *rs,
> >                           SocketReadStateFinalize *finalize,
> >                           bool vnet_hdr);
> > +NetClientState *qemu_get_peer(NetClientState *nc, int queue_index);
> >
> >   /* NIC info */
> >
> > diff --git a/net/net.c b/net/net.c
> > index 84aa6d8d00..ac5080dda1 100644
> > --- a/net/net.c
> > +++ b/net/net.c
> > @@ -324,6 +324,12 @@ void *qemu_get_nic_opaque(NetClientState *nc)
> >
> >       return nic->opaque;
> >   }
> > +NetClientState *qemu_get_peer(NetClientState *nc, int queue_index)
> > +{
> > +    NetClientState *ncs  =  nc + queue_index;
>
>
> Unnecessary space around '='.
>
> Thanks
>
>
> Will correct this

> +    assert(ncs != NULL);
> > +    return ncs->peer;
> > +}
> >
> >   static void qemu_cleanup_net_client(NetClientState *nc)
> >   {
>
>
Laurent Vivier April 21, 2020, 3:01 p.m. UTC | #3
On 20/04/2020 11:32, Cindy Lu wrote:
> This is a small function  that can get the peer from given NetClientState and queue_index
> 
> Signed-off-by: Cindy Lu <lulu@redhat.com>
> ---
>  hw/net/vhost_net.c | 16 ++++++++++------
>  include/net/net.h  |  1 +
>  net/net.c          |  6 ++++++
>  3 files changed, 17 insertions(+), 6 deletions(-)
> 
...
> diff --git a/net/net.c b/net/net.c
> index 84aa6d8d00..ac5080dda1 100644
> --- a/net/net.c
> +++ b/net/net.c
> @@ -324,6 +324,12 @@ void *qemu_get_nic_opaque(NetClientState *nc)
>  
>      return nic->opaque;
>  }
> +NetClientState *qemu_get_peer(NetClientState *nc, int queue_index)
> +{
> +    NetClientState *ncs  =  nc + queue_index;
> +    assert(ncs != NULL);

This will not assert if nc is NULL and queue_index != 0.

You should check value of nc and then calculate ncs.

Thanks,
Laurent
diff mbox series

Patch

diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c
index 6b82803fa7..4096d64aaf 100644
--- a/hw/net/vhost_net.c
+++ b/hw/net/vhost_net.c
@@ -306,7 +306,9 @@  int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
     VirtioBusState *vbus = VIRTIO_BUS(qbus);
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
+    struct vhost_net *net;
     int r, e, i;
+    NetClientState *peer;
 
     if (!k->set_guest_notifiers) {
         error_report("binding does not support guest notifiers");
@@ -314,9 +316,9 @@  int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     }
 
     for (i = 0; i < total_queues; i++) {
-        struct vhost_net *net;
 
-        net = get_vhost_net(ncs[i].peer);
+        peer = qemu_get_peer(ncs, i);
+        net = get_vhost_net(peer);
         vhost_net_set_vq_index(net, i * 2);
 
         /* Suppress the masking guest notifiers on vhost user
@@ -335,15 +337,16 @@  int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     }
 
     for (i = 0; i < total_queues; i++) {
-        r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
+        peer = qemu_get_peer(ncs, i);
+        r = vhost_net_start_one(get_vhost_net(peer), dev);
 
         if (r < 0) {
             goto err_start;
         }
 
-        if (ncs[i].peer->vring_enable) {
+        if (peer->vring_enable) {
             /* restore vring enable state */
-            r = vhost_set_vring_enable(ncs[i].peer, ncs[i].peer->vring_enable);
+            r = vhost_set_vring_enable(peer, peer->vring_enable);
 
             if (r < 0) {
                 goto err_start;
@@ -355,7 +358,8 @@  int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
 
 err_start:
     while (--i >= 0) {
-        vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
+        peer = qemu_get_peer(ncs , i);
+        vhost_net_stop_one(get_vhost_net(peer), dev);
     }
     e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
     if (e < 0) {
diff --git a/include/net/net.h b/include/net/net.h
index e175ba9677..0a74324ccd 100644
--- a/include/net/net.h
+++ b/include/net/net.h
@@ -175,6 +175,7 @@  void hmp_info_network(Monitor *mon, const QDict *qdict);
 void net_socket_rs_init(SocketReadState *rs,
                         SocketReadStateFinalize *finalize,
                         bool vnet_hdr);
+NetClientState *qemu_get_peer(NetClientState *nc, int queue_index);
 
 /* NIC info */
 
diff --git a/net/net.c b/net/net.c
index 84aa6d8d00..ac5080dda1 100644
--- a/net/net.c
+++ b/net/net.c
@@ -324,6 +324,12 @@  void *qemu_get_nic_opaque(NetClientState *nc)
 
     return nic->opaque;
 }
+NetClientState *qemu_get_peer(NetClientState *nc, int queue_index)
+{
+    NetClientState *ncs  =  nc + queue_index;
+    assert(ncs != NULL);
+    return ncs->peer;
+}
 
 static void qemu_cleanup_net_client(NetClientState *nc)
 {