# \[PATCH 4/6\] virtio\_net: allow specifying context for rx

- Subsystems: NETWORKING DRIVERS, VIRTIO NET DRIVER, THE REST
- Maintainers: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Michael S. Tsirkin, Jason Wang, Eugenio Pérez, Linus Torvalds
- List: netdev
- 4 messages, 2 authors
- Page: https://ratatoskr.run/netdev/2017/03/4966632/t

## \[PATCH 4/6\] virtio\_net: allow specifying context for rx

- From: Michael S. Tsirkin \<mst@redhat.com\>
- Date: 2017-03-29 20:50:28 
- Message: https://ratatoskr.run/netdev/2017/03/4966632.md

```text
With mergeable buffers we never use s/g for rx,
so allow specifying context in that case.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 drivers/net/virtio_net.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 6802169..340f737 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -2044,6 +2044,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 	int ret = -ENOMEM;
 	int i, total_vqs;
 	const char **names;
+	const bool *ctx;
 
 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
@@ -2062,6 +2063,13 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
 	if (!names)
 		goto err_names;
+	if (vi->mergeable_rx_bufs) {
+		ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL);
+		if (!ctx)
+			goto err_ctx;
+	} else {
+		ctx = NULL;
+	}
 
 	/* Parameters for control virtqueue, if any */
 	if (vi->has_cvq) {
@@ -2077,9 +2085,12 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 		sprintf(vi->sq[i].name, "output.%d", i);
 		names[rxq2vq(i)] = vi->rq[i].name;
 		names[txq2vq(i)] = vi->sq[i].name;
+		if (ctx)
+			ctx[rxq2vq(i)] = true;
 	}
 
-	ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, callbacks, names, NULL);
+	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
+					 names, ctx, NULL);
 	if (ret)
 		goto err_find;
 
@@ -2101,6 +2112,8 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 	return 0;
 
 err_find:
+	kfree(ctx);
+err_ctx:
 	kfree(names);
 err_names:
 	kfree(callbacks);
-- 
MST
```

## Re: \[PATCH 4/6\] virtio\_net: allow specifying context for rx

- From: Cornelia Huck \<hidden\>
- Date: 2017-03-30 07:27:02 
- Message: https://ratatoskr.run/netdev/2017/03/4966976.md

```text
On Wed, 29 Mar 2017 23:48:54 +0300
"Michael S. Tsirkin" [ref] wrote:

> With mergeable buffers we never use s/g for rx,
> so allow specifying context in that case.
> 
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> ---
>  drivers/net/virtio_net.c | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 6802169..340f737 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -2044,6 +2044,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
>  	int ret = -ENOMEM;
>  	int i, total_vqs;
>  	const char **names;
> +	const bool *ctx;
>  
>  	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
>  	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
> @@ -2062,6 +2063,13 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
>  	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
>  	if (!names)
>  		goto err_names;
> +	if (vi->mergeable_rx_bufs) {
> +		ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL);
> +		if (!ctx)
> +			goto err_ctx;
> +	} else {
> +		ctx = NULL;
> +	}
>  
>  	/* Parameters for control virtqueue, if any */
>  	if (vi->has_cvq) {
> @@ -2077,9 +2085,12 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
>  		sprintf(vi->sq[i].name, "output.%d", i);
>  		names[rxq2vq(i)] = vi->rq[i].name;
>  		names[txq2vq(i)] = vi->sq[i].name;
> +		if (ctx)
> +			ctx[rxq2vq(i)] = true;
>  	}
>  
> -	ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, callbacks, names, NULL);
> +	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
> +					 names, ctx, NULL);

virtio_find_vqs_ctx()? (Needs to be exported, obviously.)

>  	if (ret)
>  		goto err_find;
>  
> @@ -2101,6 +2112,8 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
>  	return 0;
>  
>  err_find:
> +	kfree(ctx);
> +err_ctx:
>  	kfree(names);
>  err_names:
>  	kfree(callbacks);
```

## Re: \[PATCH 4/6\] virtio\_net: allow specifying context for rx

- From: Michael S. Tsirkin \<mst@redhat.com\>
- Date: 2017-03-30 14:31:41 
- Message: https://ratatoskr.run/netdev/2017/03/4967454.md

```text
On Thu, Mar 30, 2017 at 09:26:51AM +0200, Cornelia Huck wrote:
> On Wed, 29 Mar 2017 23:48:54 +0300
> "Michael S. Tsirkin" [ref] wrote:
> 
> > With mergeable buffers we never use s/g for rx,
> > so allow specifying context in that case.
> > 
> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > ---
> >  drivers/net/virtio_net.c | 15 ++++++++++++++-
> >  1 file changed, 14 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> > index 6802169..340f737 100644
> > --- a/drivers/net/virtio_net.c
> > +++ b/drivers/net/virtio_net.c
> > @@ -2044,6 +2044,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
> >  	int ret = -ENOMEM;
> >  	int i, total_vqs;
> >  	const char **names;
> > +	const bool *ctx;
> >  
> >  	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
> >  	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
> > @@ -2062,6 +2063,13 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
> >  	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
> >  	if (!names)
> >  		goto err_names;
> > +	if (vi->mergeable_rx_bufs) {
> > +		ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL);
> > +		if (!ctx)
> > +			goto err_ctx;
> > +	} else {
> > +		ctx = NULL;
> > +	}
> >  
> >  	/* Parameters for control virtqueue, if any */
> >  	if (vi->has_cvq) {
> > @@ -2077,9 +2085,12 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
> >  		sprintf(vi->sq[i].name, "output.%d", i);
> >  		names[rxq2vq(i)] = vi->rq[i].name;
> >  		names[txq2vq(i)] = vi->sq[i].name;
> > +		if (ctx)
> > +			ctx[rxq2vq(i)] = true;
> >  	}
> >  
> > -	ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, callbacks, names, NULL);
> > +	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
> > +					 names, ctx, NULL);
> 
> virtio_find_vqs_ctx()? (Needs to be exported, obviously.)

I guess I can do that but there's a single user ATM.
Do you think it's worth doing right now, or wait until
it gets more users?


> >  	if (ret)
> >  		goto err_find;
> >  
> > @@ -2101,6 +2112,8 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
> >  	return 0;
> >  
> >  err_find:
> > +	kfree(ctx);
> > +err_ctx:
> >  	kfree(names);
> >  err_names:
> >  	kfree(callbacks);
```

## Re: \[PATCH 4/6\] virtio\_net: allow specifying context for rx

- From: Cornelia Huck \<hidden\>
- Date: 2017-03-30 14:46:17 
- Message: https://ratatoskr.run/netdev/2017/03/4967469.md

```text
On Thu, 30 Mar 2017 17:31:37 +0300
"Michael S. Tsirkin" [ref] wrote:

> On Thu, Mar 30, 2017 at 09:26:51AM +0200, Cornelia Huck wrote:
> > On Wed, 29 Mar 2017 23:48:54 +0300
> > "Michael S. Tsirkin" [ref] wrote:
> > 
> > > With mergeable buffers we never use s/g for rx,
> > > so allow specifying context in that case.
> > > 
> > > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > > ---
> > >  drivers/net/virtio_net.c | 15 ++++++++++++++-
> > >  1 file changed, 14 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> > > index 6802169..340f737 100644
> > > --- a/drivers/net/virtio_net.c
> > > +++ b/drivers/net/virtio_net.c
> > > @@ -2044,6 +2044,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
> > >  	int ret = -ENOMEM;
> > >  	int i, total_vqs;
> > >  	const char **names;
> > > +	const bool *ctx;
> > >  
> > >  	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
> > >  	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
> > > @@ -2062,6 +2063,13 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
> > >  	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
> > >  	if (!names)
> > >  		goto err_names;
> > > +	if (vi->mergeable_rx_bufs) {
> > > +		ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL);
> > > +		if (!ctx)
> > > +			goto err_ctx;
> > > +	} else {
> > > +		ctx = NULL;
> > > +	}
> > >  
> > >  	/* Parameters for control virtqueue, if any */
> > >  	if (vi->has_cvq) {
> > > @@ -2077,9 +2085,12 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
> > >  		sprintf(vi->sq[i].name, "output.%d", i);
> > >  		names[rxq2vq(i)] = vi->rq[i].name;
> > >  		names[txq2vq(i)] = vi->sq[i].name;
> > > +		if (ctx)
> > > +			ctx[rxq2vq(i)] = true;
> > >  	}
> > >  
> > > -	ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, callbacks, names, NULL);
> > > +	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
> > > +					 names, ctx, NULL);
> > 
> > virtio_find_vqs_ctx()? (Needs to be exported, obviously.)
> 
> I guess I can do that but there's a single user ATM.
> Do you think it's worth doing right now, or wait until
> it gets more users?

Given that you introduce it in patch 2, I'd just make it an exported
function from the start. (It also looks nicer IMO.)
```
