All of lore.kernel.org
 help / color / mirror / Atom feed
From: ggarcia@abra.uab.cat
To: netdev@vger.kernel.org
Cc: jhansen@vmware.com, stefanha@redhat.com,
	Gerard Garcia <ggarcia@abra.uab.cat>
Subject: [RFC v2 3/3] vsockmon: Add vsock hooks
Date: Wed, 22 Jun 2016 18:11:02 +0200	[thread overview]
Message-ID: <20160622161102.8250-4-ggarcia@deic.uab.cat> (raw)
In-Reply-To: <20160622161102.8250-1-ggarcia@deic.uab.cat>

From: Gerard Garcia <ggarcia@deic.uab.cat>

Signed-off-by: Gerard Garcia <ggarcia@deic.uab.cat>
---
 drivers/vhost/vsock.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 17bfe4e..e8621cc 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -14,8 +14,10 @@
 #include <net/sock.h>
 #include <linux/virtio_vsock.h>
 #include <linux/vhost.h>
+#include <linux/skbuff.h>
 
 #include <net/af_vsock.h>
+#include <uapi/linux/vsockmon.h>
 #include "vhost.h"
 
 #define VHOST_VSOCK_DEFAULT_HOST_CID	2
@@ -45,6 +47,69 @@ struct vhost_vsock {
 	u32 guest_cid;
 };
 
+static struct sk_buff *
+virtio_vsock_pkt_to_skb(struct virtio_vsock_pkt *pkt)
+{
+	struct sk_buff *skb;
+	struct af_vsockmon_hdr *hdr;
+	void *payload;
+
+	u32 skb_len = sizeof(struct af_vsockmon_hdr) + pkt->len;
+
+	skb = alloc_skb(skb_len, GFP_ATOMIC);
+	if (!skb)
+		return NULL;
+
+	skb_reserve(skb, sizeof(struct af_vsockmon_hdr));
+
+	if (pkt->len) {
+		payload = skb_put(skb, pkt->len);
+		memcpy(payload, pkt->buf, pkt->len);
+	}
+
+	hdr = (struct af_vsockmon_hdr *) skb_push(skb, sizeof(*hdr));
+
+	hdr->src_cid = pkt->hdr.src_cid;
+	hdr->src_port = pkt->hdr.src_port;
+	hdr->dst_cid = pkt->hdr.dst_cid;
+	hdr->dst_port = pkt->hdr.dst_port;
+	hdr->t = AF_VSOCK_T_VIRTIO;
+
+	switch(pkt->hdr.op) {
+		case VIRTIO_VSOCK_OP_REQUEST:
+		case VIRTIO_VSOCK_OP_RESPONSE:
+			hdr->op = AF_VSOCK_OP_CONNECT;
+			break;
+		case VIRTIO_VSOCK_OP_RST:
+		case VIRTIO_VSOCK_OP_SHUTDOWN:
+			hdr->op = AF_VSOCK_OP_DISCONNECT;
+			break;
+		case VIRTIO_VSOCK_OP_RW:
+			hdr->op = AF_VSOCK_OP_PAYLOAD;
+			break;
+		case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
+		case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
+			hdr->op = AF_VSOCK_OP_CONTROL;
+			break;
+		default:
+			hdr->op = AF_VSOCK_OP_UNKNOWN;
+			break;
+	}
+
+	hdr->t_hdr.virtio_hdr = pkt->hdr;
+
+	return skb;
+}
+
+static void vsock_deliver_tap_pkt(struct virtio_vsock_pkt *pkt)
+{
+	struct sk_buff *skb = virtio_vsock_pkt_to_skb(pkt);
+	if (skb) {
+		vsock_deliver_tap(skb);
+		kfree_skb(skb);
+	}
+}
+
 static u32 vhost_transport_get_local_cid(void)
 {
 	return VHOST_VSOCK_DEFAULT_HOST_CID;
@@ -147,6 +212,11 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
 
 		vsock->total_tx_buf -= pkt->len;
 
+		/* Deliver to monitoring devices all correctly transmitted
+		 * packets.
+		 */
+		vsock_deliver_tap_pkt(pkt);
+
 		virtio_transport_free_pkt(pkt);
 	}
 	if (added)
@@ -367,6 +437,9 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
 			continue;
 		}
 
+		/* Deliver to monitoring devices all received packets */
+		vsock_deliver_tap_pkt(pkt);
+
 		/* Only accept correctly addressed packets */
 		if (le64_to_cpu(pkt->hdr.src_cid) == vsock->guest_cid)
 			virtio_transport_recv_pkt(pkt);
-- 
2.9.0

  parent reply	other threads:[~2016-06-22 16:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-22 16:10 [RFC v2 0/3] vsockmon: virtual device to monitor AF_VSOCK sockets ggarcia
2016-06-22 16:11 ` [RFC v2 1/3] vsockmon: Add tap functions ggarcia
2016-06-30 10:49   ` Stefan Hajnoczi
2016-06-22 16:11 ` [RFC v2 2/3] vsockmon: Add vsockmon device ggarcia
2016-06-22 16:11 ` ggarcia [this message]
2016-06-23 12:09   ` [RFC v2 3/3] vsockmon: Add vsock hooks Sergei Shtylyov
2016-06-30 10:55   ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160622161102.8250-4-ggarcia@deic.uab.cat \
    --to=ggarcia@abra.uab.cat \
    --cc=jhansen@vmware.com \
    --cc=netdev@vger.kernel.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.