Skip to contentSkip to conversation

VSOCK: add support for MSG_PEEK

RFC In review Matias Ezequiel Vara Larsen 2 participants 5 messages Sep 22, 2019 – Sep 23, 2019

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 VSOCK: add support for MSG_PEEK

This patch adds support for MSG_PEEK. In such a case, packets are not removed from the rx_queue and credit updates are not sent.

---
 net/vmw_vsock/virtio_transport_common.c | 59 +++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +56 -3
index 94cc0fa..830e890 100644
@@ -264,6 +264,59 @@ static int virtio_transport_send_credit_update(struct vsock_sock *vsk,
 }
 
 static ssize_t
+virtio_transport_stream_do_peek(struct vsock_sock *vsk,
+				struct msghdr *msg,
+				size_t len)
+{
+	struct virtio_vsock_sock *vvs = vsk->trans;
+	struct virtio_vsock_pkt *pkt;
+	size_t bytes, off = 0, total = 0;
+	int err = -EFAULT;
+
+	spin_lock_bh(&vvs->rx_lock);
+
+	if (list_empty(&vvs->rx_queue)) {
+		spin_unlock_bh(&vvs->rx_lock);
+		return 0;
+	}
+
+	pkt = list_first_entry(&vvs->rx_queue,
+			       struct virtio_vsock_pkt, list);
+	do {
+		bytes = len - total;
+		if (bytes > pkt->len - off)
+			bytes = pkt->len - off;
+
+		/* sk_lock is held by caller so no one else can dequeue.
+		 * Unlock rx_lock since memcpy_to_msg() may sleep.
+		 */
+		spin_unlock_bh(&vvs->rx_lock);
+
+		err = memcpy_to_msg(msg, pkt->buf + off, bytes);
+		if (err)
+			goto out;
+
+		spin_lock_bh(&vvs->rx_lock);
+
+		total += bytes;
+		off += bytes;
+		if (off == pkt->len) {
+			pkt = list_next_entry(pkt, list);
+			off = 0;
+		}
+	} while ((total < len) && !list_is_first(&pkt->list, &vvs->rx_queue));
+
+	spin_unlock_bh(&vvs->rx_lock);
+
+	return total;
+
+out:
+	if (total)
+		err = total;
+	return err;
+}
+
+static ssize_t
 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
 				   struct msghdr *msg,
 				   size_t len)
@@ -330,9 +383,9 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk,
 				size_t len, int flags)
 {
 	if (flags & MSG_PEEK)
-		return -EOPNOTSUPP;
-
-	return virtio_transport_stream_do_dequeue(vsk, msg, len);
+		return virtio_transport_stream_do_peek(vsk, msg, len);
+	else
+		return virtio_transport_stream_do_dequeue(vsk, msg, len);
 }
 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
 
Signature · 1 line
2.7.4
#1 lore raw

Hi Matias,
thanks for this patch!

Since this patch only concerns virtio_transport, I'd use the 'vsock/virtio' prefix in the commit title:
"vsock/virtio: add support for MSG_PEEK"

Some comments below:

Quoted 17 lines · Matias Ezequiel Vara Larsen

This patch adds support for MSG_PEEK. In such a case, packets are not removed from the rx_queue and credit updates are not sent.

---
 net/vmw_vsock/virtio_transport_common.c | 59 +++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +1 -0
index 94cc0fa..830e890 100644
@@ -264,6 +264,59 @@ static int virtio_transport_send_credit_update(struct vsock_sock *vsk,
 }
 
 static ssize_t
+virtio_transport_stream_do_peek(struct vsock_sock *vsk,

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 277

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10 -0
+				struct msghdr *msg,
+				size_t len)
+{
+	struct virtio_vsock_sock *vvs = vsk->trans;
+	struct virtio_vsock_pkt *pkt;
+	size_t bytes, off = 0, total = 0;
+	int err = -EFAULT;
+
+	spin_lock_bh(&vvs->rx_lock);
+

What about using list_for_each_entry() to cycle through the queued packets?

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 285

Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +8 -0
+	if (list_empty(&vvs->rx_queue)) {
+		spin_unlock_bh(&vvs->rx_lock);
+		return 0;
+	}
+
+	pkt = list_first_entry(&vvs->rx_queue,
+			       struct virtio_vsock_pkt, list);
+	do {

pkt->off contains the offset inside the packet where the unread data starts. So here we should initialize 'off':

		off = pkt->off;

Or just use pkt->off later (without increasing it as in the dequeue).

Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +6 -0
+		bytes = len - total;
+		if (bytes > pkt->len - off)
+			bytes = pkt->len - off;
+
+		/* sk_lock is held by caller so no one else can dequeue.
+		 * Unlock rx_lock since memcpy_to_msg() may sleep.

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 301

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10 -0
+		 */
+		spin_unlock_bh(&vvs->rx_lock);
+
+		err = memcpy_to_msg(msg, pkt->buf + off, bytes);
+		if (err)
+			goto out;
+
+		spin_lock_bh(&vvs->rx_lock);
+
+		total += bytes;

Using list_for_each_entry(), here we can just do:
(or better, at the beginning of the cycle)

		if (total == len)
			break;

removing the next part...

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 307

Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +6 -0
+		off += bytes;
+		if (off == pkt->len) {
+			pkt = list_next_entry(pkt, list);
+			off = 0;
+		}
+	} while ((total < len) && !list_is_first(&pkt->list, &vvs->rx_queue));

...until here.

Quoted 18 lines
net/vmw_vsock/virtio_transport_common.c +12 -0
+
+	spin_unlock_bh(&vvs->rx_lock);
+
+	return total;
+
+out:
+	if (total)
+		err = total;
+	return err;
+}
+
+static ssize_t
 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
 				   struct msghdr *msg,
 				   size_t len)
@@ -330,9 +383,9 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk,
 				size_t len, int flags)
 {

net/vmw_vsock/virtio_transport_common.c virtio_transport_stream_dequeue(struct vsock_sock *vsk, · line 391

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +3 -3
 	if (flags & MSG_PEEK)
-		return -EOPNOTSUPP;
-
-	return virtio_transport_stream_do_dequeue(vsk, msg, len);
+		return virtio_transport_stream_do_peek(vsk, msg, len);
+	else
+		return virtio_transport_stream_do_dequeue(vsk, msg, len);
 }
 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
 

The rest looks good to me!

Thanks,
Stefano

#2 lore raw
In reply to Matias Ezequiel Vara Larsen

Hi Matias,
thanks for this patch!

Since this patch only concerns virtio_transport, I'd use the 'vsock/virtio' prefix in the commit title:
"vsock/virtio: add support for MSG_PEEK"

Some comments below:

Quoted 17 lines · Matias Ezequiel Vara Larsen

This patch adds support for MSG_PEEK. In such a case, packets are not removed from the rx_queue and credit updates are not sent.

---
 net/vmw_vsock/virtio_transport_common.c | 59 +++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +1 -0
index 94cc0fa..830e890 100644
@@ -264,6 +264,59 @@ static int virtio_transport_send_credit_update(struct vsock_sock *vsk,
 }
 
 static ssize_t
+virtio_transport_stream_do_peek(struct vsock_sock *vsk,

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 277

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10 -0
+				struct msghdr *msg,
+				size_t len)
+{
+	struct virtio_vsock_sock *vvs = vsk->trans;
+	struct virtio_vsock_pkt *pkt;
+	size_t bytes, off = 0, total = 0;
+	int err = -EFAULT;
+
+	spin_lock_bh(&vvs->rx_lock);
+

What about using list_for_each_entry() to cycle through the queued packets?

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 285

Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +8 -0
+	if (list_empty(&vvs->rx_queue)) {
+		spin_unlock_bh(&vvs->rx_lock);
+		return 0;
+	}
+
+	pkt = list_first_entry(&vvs->rx_queue,
+			       struct virtio_vsock_pkt, list);
+	do {

pkt->off contains the offset inside the packet where the unread data starts. So here we should initialize 'off':

		off = pkt->off;

Or just use pkt->off later (without increasing it as in the dequeue).

Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +6 -0
+		bytes = len - total;
+		if (bytes > pkt->len - off)
+			bytes = pkt->len - off;
+
+		/* sk_lock is held by caller so no one else can dequeue.
+		 * Unlock rx_lock since memcpy_to_msg() may sleep.

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 301

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10 -0
+		 */
+		spin_unlock_bh(&vvs->rx_lock);
+
+		err = memcpy_to_msg(msg, pkt->buf + off, bytes);
+		if (err)
+			goto out;
+
+		spin_lock_bh(&vvs->rx_lock);
+
+		total += bytes;

Using list_for_each_entry(), here we can just do:
(or better, at the beginning of the cycle)

		if (total == len)
			break;

removing the next part...

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 307

Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +6 -0
+		off += bytes;
+		if (off == pkt->len) {
+			pkt = list_next_entry(pkt, list);
+			off = 0;
+		}
+	} while ((total < len) && !list_is_first(&pkt->list, &vvs->rx_queue));

...until here.

Quoted 18 lines
net/vmw_vsock/virtio_transport_common.c +12 -0
+
+	spin_unlock_bh(&vvs->rx_lock);
+
+	return total;
+
+out:
+	if (total)
+		err = total;
+	return err;
+}
+
+static ssize_t
 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
 				   struct msghdr *msg,
 				   size_t len)
@@ -330,9 +383,9 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk,
 				size_t len, int flags)
 {

net/vmw_vsock/virtio_transport_common.c virtio_transport_stream_dequeue(struct vsock_sock *vsk, · line 391

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +3 -3
 	if (flags & MSG_PEEK)
-		return -EOPNOTSUPP;
-
-	return virtio_transport_stream_do_dequeue(vsk, msg, len);
+		return virtio_transport_stream_do_peek(vsk, msg, len);
+	else
+		return virtio_transport_stream_do_dequeue(vsk, msg, len);
 }
 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
 

The rest looks good to me!

Thanks,
Stefano

#3 lore raw
Quoted 121 lines · Stefano Garzarella

Hi Matias,
thanks for this patch!

Since this patch only concerns virtio_transport, I'd use the 'vsock/virtio' prefix in the commit title:
"vsock/virtio: add support for MSG_PEEK"

Some comments below:

Quoted 17 lines · Matias Ezequiel Vara Larsen

This patch adds support for MSG_PEEK. In such a case, packets are not removed from the rx_queue and credit updates are not sent.

---
 net/vmw_vsock/virtio_transport_common.c | 59 +++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +1 -0
index 94cc0fa..830e890 100644
@@ -264,6 +264,59 @@ static int virtio_transport_send_credit_update(struct vsock_sock *vsk,
 }
 
 static ssize_t
+virtio_transport_stream_do_peek(struct vsock_sock *vsk,

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 277

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10 -0
+				struct msghdr *msg,
+				size_t len)
+{
+	struct virtio_vsock_sock *vvs = vsk->trans;
+	struct virtio_vsock_pkt *pkt;
+	size_t bytes, off = 0, total = 0;
+	int err = -EFAULT;
+
+	spin_lock_bh(&vvs->rx_lock);
+

What about using list_for_each_entry() to cycle through the queued packets?

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 285

Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +8 -0
+	if (list_empty(&vvs->rx_queue)) {
+		spin_unlock_bh(&vvs->rx_lock);
+		return 0;
+	}
+
+	pkt = list_first_entry(&vvs->rx_queue,
+			       struct virtio_vsock_pkt, list);
+	do {

pkt->off contains the offset inside the packet where the unread data starts. So here we should initialize 'off':

		off = pkt->off;

Or just use pkt->off later (without increasing it as in the dequeue).

Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +6 -0
+		bytes = len - total;
+		if (bytes > pkt->len - off)
+			bytes = pkt->len - off;
+
+		/* sk_lock is held by caller so no one else can dequeue.
+		 * Unlock rx_lock since memcpy_to_msg() may sleep.

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 301

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10 -0
+		 */
+		spin_unlock_bh(&vvs->rx_lock);
+
+		err = memcpy_to_msg(msg, pkt->buf + off, bytes);
+		if (err)
+			goto out;
+
+		spin_lock_bh(&vvs->rx_lock);
+
+		total += bytes;

Using list_for_each_entry(), here we can just do:
(or better, at the beginning of the cycle)

		if (total == len)
			break;

removing the next part...

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 307

Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +6 -0
+		off += bytes;
+		if (off == pkt->len) {
+			pkt = list_next_entry(pkt, list);
+			off = 0;
+		}
+	} while ((total < len) && !list_is_first(&pkt->list, &vvs->rx_queue));

...until here.

Quoted 18 lines
net/vmw_vsock/virtio_transport_common.c +12 -0
+
+	spin_unlock_bh(&vvs->rx_lock);
+
+	return total;
+
+out:
+	if (total)
+		err = total;
+	return err;
+}
+
+static ssize_t
 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
 				   struct msghdr *msg,
 				   size_t len)
@@ -330,9 +383,9 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk,
 				size_t len, int flags)
 {

net/vmw_vsock/virtio_transport_common.c virtio_transport_stream_dequeue(struct vsock_sock *vsk, · line 391

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +3 -3
 	if (flags & MSG_PEEK)
-		return -EOPNOTSUPP;
-
-	return virtio_transport_stream_do_dequeue(vsk, msg, len);
+		return virtio_transport_stream_do_peek(vsk, msg, len);
+	else
+		return virtio_transport_stream_do_dequeue(vsk, msg, len);
 }
 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
 

The rest looks good to me!

Quoted 2 lines

Thanks,
Stefano

Thanks Stefano. Based on your comments, I will modify the patch and resubmit it.

Matias

#4 lore raw
In reply to Stefano Garzarella
Quoted 121 lines · Stefano Garzarella

Hi Matias,
thanks for this patch!

Since this patch only concerns virtio_transport, I'd use the 'vsock/virtio' prefix in the commit title:
"vsock/virtio: add support for MSG_PEEK"

Some comments below:

Quoted 17 lines · Matias Ezequiel Vara Larsen

This patch adds support for MSG_PEEK. In such a case, packets are not removed from the rx_queue and credit updates are not sent.

---
 net/vmw_vsock/virtio_transport_common.c | 59 +++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +1 -0
index 94cc0fa..830e890 100644
@@ -264,6 +264,59 @@ static int virtio_transport_send_credit_update(struct vsock_sock *vsk,
 }
 
 static ssize_t
+virtio_transport_stream_do_peek(struct vsock_sock *vsk,

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 277

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10 -0
+				struct msghdr *msg,
+				size_t len)
+{
+	struct virtio_vsock_sock *vvs = vsk->trans;
+	struct virtio_vsock_pkt *pkt;
+	size_t bytes, off = 0, total = 0;
+	int err = -EFAULT;
+
+	spin_lock_bh(&vvs->rx_lock);
+

What about using list_for_each_entry() to cycle through the queued packets?

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 285

Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +8 -0
+	if (list_empty(&vvs->rx_queue)) {
+		spin_unlock_bh(&vvs->rx_lock);
+		return 0;
+	}
+
+	pkt = list_first_entry(&vvs->rx_queue,
+			       struct virtio_vsock_pkt, list);
+	do {

pkt->off contains the offset inside the packet where the unread data starts. So here we should initialize 'off':

		off = pkt->off;

Or just use pkt->off later (without increasing it as in the dequeue).

Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +6 -0
+		bytes = len - total;
+		if (bytes > pkt->len - off)
+			bytes = pkt->len - off;
+
+		/* sk_lock is held by caller so no one else can dequeue.
+		 * Unlock rx_lock since memcpy_to_msg() may sleep.

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 301

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10 -0
+		 */
+		spin_unlock_bh(&vvs->rx_lock);
+
+		err = memcpy_to_msg(msg, pkt->buf + off, bytes);
+		if (err)
+			goto out;
+
+		spin_lock_bh(&vvs->rx_lock);
+
+		total += bytes;

Using list_for_each_entry(), here we can just do:
(or better, at the beginning of the cycle)

		if (total == len)
			break;

removing the next part...

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_credit_update(struct vsock_sock *vsk, · line 307

Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +6 -0
+		off += bytes;
+		if (off == pkt->len) {
+			pkt = list_next_entry(pkt, list);
+			off = 0;
+		}
+	} while ((total < len) && !list_is_first(&pkt->list, &vvs->rx_queue));

...until here.

Quoted 18 lines
net/vmw_vsock/virtio_transport_common.c +12 -0
+
+	spin_unlock_bh(&vvs->rx_lock);
+
+	return total;
+
+out:
+	if (total)
+		err = total;
+	return err;
+}
+
+static ssize_t
 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
 				   struct msghdr *msg,
 				   size_t len)
@@ -330,9 +383,9 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk,
 				size_t len, int flags)
 {

net/vmw_vsock/virtio_transport_common.c virtio_transport_stream_dequeue(struct vsock_sock *vsk, · line 391

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +3 -3
 	if (flags & MSG_PEEK)
-		return -EOPNOTSUPP;
-
-	return virtio_transport_stream_do_dequeue(vsk, msg, len);
+		return virtio_transport_stream_do_peek(vsk, msg, len);
+	else
+		return virtio_transport_stream_do_dequeue(vsk, msg, len);
 }
 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
 

The rest looks good to me!

Quoted 2 lines

Thanks,
Stefano

Thanks Stefano. Based on your comments, I will modify the patch and resubmit it.

Matias