Skip to contentSkip to conversation

Add COLO-proxy virtio-net support

PATCH Superseded Zhang Chen 4 participants 40 messages May 12, 2017 – May 23, 2017

Subsystems (2)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Zhang Chen

author
#0 lore raw

v4 cover Add COLO-proxy virtio-net support

If user use -device virtio-net-pci, virtio-net driver will add a header to raw net packet that colo-proxy can't handle it. COLO-proxy just focus on the packet payload, so we skip the virtio-net header to compare the sent packet that primary guest's to secondary guest's.

V4:
- Add vnet_hdr option for filter-mirror, filter-redirector, filter-rewriter,colo-compare.
- Use new design to impliment virtio-net support for colo-proxy.
- Fix codestyle.
- Remove unused option for filter-rewriter.
- Add filter-rewriter virtio-net support.
- Address other comments.

Zhang Chen (12):
net: Add vnet_hdr_len related arguments in NetClientState net/filter-mirror.c: Add new option to enable vnet support for
filter-mirror
net/filter-mirror.c: Make filter_mirror_send support vnet support. net/filter-mirror.c: Add new option to enable vnet support for
filter-redirector
net/net.c: Add vnet_hdr support in SocketReadState net/colo-compare.c: Add new option to enable vnet support for
colo-compare
net/colo.c: Make vnet_hdr_len as packet property net/colo-compare.c: Make colo-compare support vnet_hdr_len net/colo.c: Add vnet packet parse feature in colo-proxy net/colo-compare.c: Add vnet packet's tcp/udp/icmp compare net/filter-rewriter.c: Add new option to enable vnet support for
filter-rewriter
net/filter-rewriter.c: Make filter-rewriter support vnet_hdr_len

 include/net/net.h     |  11 +++++-
 net/colo-compare.c    |  99 ++++++++++++++++++++++++++++++++++++++---------
 net/colo.c            |   9 +++--
 net/colo.h            |   4 +-
 net/filter-mirror.c   | 104 +++++++++++++++++++++++++++++++++++++++++++++++---
 net/filter-rewriter.c |  55 +++++++++++++++++++++++++-
 net/net.c             |  38 ++++++++++++++++--
 net/socket.c          |   2 +-
 qemu-options.hx       |  17 +++++----
 9 files changed, 296 insertions(+), 43 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v4 1/12 net: Add vnet_hdr_len related arguments in NetClientState

Add vnet_hdr_len and using_vnet_hdr arguments in NetClientState that make othermodule get real vnet_hdr_len easily.

---
 include/net/net.h | 2 ++
 net/net.c         | 2 ++
 2 files changed, 4 insertions(+)
include/net/net.h +2 -0
index 99b28d5..70edfc0 100644
@@ -100,6 +100,8 @@ struct NetClientState {
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
     int vring_enable;
+    bool using_vnet_hdr;
+    int vnet_hdr_len;
     QTAILQ_HEAD(NetFilterHead, NetFilterState) filters;
 };
 
net/net.c +2 -0
index 0ac3b9e..a00a0c9 100644
@@ -472,6 +472,7 @@ void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
         return;
     }
 
+    nc->using_vnet_hdr = enable;
     nc->info->using_vnet_hdr(nc, enable);
 }
 
@@ -491,6 +492,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
         return;
     }
 
+    nc->vnet_hdr_len = len;
     nc->info->set_vnet_hdr_len(nc, len);
 }
 
Signature · 1 line
2.7.4
Reviewed-by on v4 1/12
#2 lore raw

Re: [Qemu-devel] [PATCH V4 01/12] net: Add vnet_hdr_len related arguments in NetClientState

Hi Zhang

Quoted 35 lines · Zhang Chen

Add vnet_hdr_len and using_vnet_hdr arguments in NetClientState that make othermodule get real vnet_hdr_len easily.

---
 include/net/net.h | 2 ++
 net/net.c         | 2 ++
 2 files changed, 4 insertions(+)
include/net/net.h +2 -0
index 99b28d5..70edfc0 100644
@@ -100,6 +100,8 @@ struct NetClientState {
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
     int vring_enable;
+    bool using_vnet_hdr;
+    int vnet_hdr_len;
     QTAILQ_HEAD(NetFilterHead, NetFilterState) filters;
 };
 
net/net.c +1 -0
index 0ac3b9e..a00a0c9 100644
@@ -472,6 +472,7 @@ void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
         return;
     }
 
+    nc->using_vnet_hdr = enable;
     nc->info->using_vnet_hdr(nc, enable);
 }
 

net/net.c void qemu_set_vnet_hdr_len(NetClientState *nc, int len) · line 498

Quoted 7 lines
net/net.c +1 -0
@@ -491,6 +492,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
         return;
     }
 
+    nc->vnet_hdr_len = len;
     nc->info->set_vnet_hdr_len(nc, len);
 }
 

For what it's worth, now having those fields in NetClientState it is possible to remove a deref to NetClientInfo in qemu_has_vnet_hdr() and qemu_has_vnet_hdr_len().

Anyway,

#3 lore raw

Re: [Qemu-devel] [PATCH V4 01/12] net: Add vnet_hdr_len related arguments in NetClientState

Quoted 50 lines · Philippe Mathieu-Daudé

Hi Zhang

Quoted 37 lines · Zhang Chen

Add vnet_hdr_len and using_vnet_hdr arguments in NetClientState that make othermodule get real vnet_hdr_len easily.

---
 include/net/net.h | 2 ++
 net/net.c         | 2 ++
 2 files changed, 4 insertions(+)
include/net/net.h +2 -0
index 99b28d5..70edfc0 100644
@@ -100,6 +100,8 @@ struct NetClientState {
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
     int vring_enable;
+    bool using_vnet_hdr;
+    int vnet_hdr_len;
     QTAILQ_HEAD(NetFilterHead, NetFilterState) filters;
 };
 
net/net.c +1 -0
index 0ac3b9e..a00a0c9 100644
@@ -472,6 +472,7 @@ void qemu_using_vnet_hdr(NetClientState *nc, bool 
 enable)
         return;
     }
 
+    nc->using_vnet_hdr = enable;
     nc->info->using_vnet_hdr(nc, enable);
 }

net/net.c void qemu_set_vnet_hdr_len(NetClientState *nc, · line 498

Quoted 7 lines
net/net.c +1 -0
@@ -491,6 +492,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, 
 int len)
         return;
     }
 
+    nc->vnet_hdr_len = len;
     nc->info->set_vnet_hdr_len(nc, len);
 }

For what it's worth, now having those fields in NetClientState it is possible to remove a deref to NetClientInfo in qemu_has_vnet_hdr() and qemu_has_vnet_hdr_len().

Quoted 4 lines

Anyway,

Yes and this could be done on top with removing private e.g vnet_hdr_len.

Thanks

author
#4 lore raw

Re: [Qemu-devel] [PATCH V4 01/12] net: Add vnet_hdr_len related arguments in NetClientState

Quoted 53 lines · Jason Wang
Quoted 50 lines · Philippe Mathieu-Daudé

Hi Zhang

Quoted 37 lines · Zhang Chen

Add vnet_hdr_len and using_vnet_hdr arguments in NetClientState that make othermodule get real vnet_hdr_len easily.

---
 include/net/net.h | 2 ++
 net/net.c         | 2 ++
 2 files changed, 4 insertions(+)
include/net/net.h +2 -0
index 99b28d5..70edfc0 100644
@@ -100,6 +100,8 @@ struct NetClientState {
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
     int vring_enable;
+    bool using_vnet_hdr;
+    int vnet_hdr_len;
     QTAILQ_HEAD(NetFilterHead, NetFilterState) filters;
 };
 
net/net.c +1 -0
index 0ac3b9e..a00a0c9 100644
@@ -472,6 +472,7 @@ void qemu_using_vnet_hdr(NetClientState *nc, 
 bool enable)
         return;
     }
 
+    nc->using_vnet_hdr = enable;
     nc->info->using_vnet_hdr(nc, enable);
 }

net/net.c void qemu_set_vnet_hdr_len(NetClientState *nc, · line 498

Quoted 7 lines
net/net.c +1 -0
@@ -491,6 +492,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, 
 int len)
         return;
     }
 
+    nc->vnet_hdr_len = len;
     nc->info->set_vnet_hdr_len(nc, len);
 }

For what it's worth, now having those fields in NetClientState it is possible to remove a deref to NetClientInfo in qemu_has_vnet_hdr() and qemu_has_vnet_hdr_len().

Quoted 6 lines
Quoted 4 lines

Anyway,

Yes and this could be done on top with removing private e.g vnet_hdr_len.

Do you means we will remove the qemu_has_vnet_hdr() and qemu_has_vnet_hdr_len() in the future?

Thanks
Zhang Chen

Quoted 6 lines

Thanks

.

Signature · 2 lines
Thanks
Zhang Chen
#5 lore raw

Re: [Qemu-devel] [PATCH V4 01/12] net: Add vnet_hdr_len related arguments in NetClientState

Quoted 61 lines · Zhang Chen
Quoted 58 lines · Jason Wang
Quoted 54 lines · Philippe Mathieu-Daudé

Hi Zhang

Quoted 37 lines · Zhang Chen

Add vnet_hdr_len and using_vnet_hdr arguments in NetClientState that make othermodule get real vnet_hdr_len easily.

---
 include/net/net.h | 2 ++
 net/net.c         | 2 ++
 2 files changed, 4 insertions(+)
include/net/net.h +2 -0
index 99b28d5..70edfc0 100644
@@ -100,6 +100,8 @@ struct NetClientState {
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
     int vring_enable;
+    bool using_vnet_hdr;
+    int vnet_hdr_len;
     QTAILQ_HEAD(NetFilterHead, NetFilterState) filters;
 };
 
net/net.c +1 -0
index 0ac3b9e..a00a0c9 100644
@@ -472,6 +472,7 @@ void qemu_using_vnet_hdr(NetClientState *nc, 
 bool enable)
         return;
     }
 
+    nc->using_vnet_hdr = enable;
     nc->info->using_vnet_hdr(nc, enable);
 }

net/net.c void qemu_set_vnet_hdr_len(NetClientState *nc, · line 498

Quoted 7 lines
net/net.c +1 -0
@@ -491,6 +492,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, 
 int len)
         return;
     }
 
+    nc->vnet_hdr_len = len;
     nc->info->set_vnet_hdr_len(nc, len);
 }

For what it's worth, now having those fields in NetClientState it is possible to remove a deref to NetClientInfo in qemu_has_vnet_hdr() and qemu_has_vnet_hdr_len().

Anyway,

Quoted 6 lines
Quoted 1 line

Yes and this could be done on top with removing private e.g vnet_hdr_len.

Do you means we will remove the qemu_has_vnet_hdr() and qemu_has_vnet_hdr_len() in the future?

Thanks
Zhang Chen

Yes and e.g both tap and netmap have its private vnet header field. We can remove them too.

Thanks

Quoted 8 lines
Quoted 7 lines

Thanks

.

author
#6 lore raw

Re: [Qemu-devel] [PATCH V4 01/12] net: Add vnet_hdr_len related arguments in NetClientState

Quoted 64 lines · Jason Wang
Quoted 61 lines · Zhang Chen
Quoted 58 lines · Jason Wang
Quoted 54 lines · Philippe Mathieu-Daudé

Hi Zhang

Quoted 37 lines · Zhang Chen

Add vnet_hdr_len and using_vnet_hdr arguments in NetClientState that make othermodule get real vnet_hdr_len easily.

---
 include/net/net.h | 2 ++
 net/net.c         | 2 ++
 2 files changed, 4 insertions(+)
include/net/net.h +2 -0
index 99b28d5..70edfc0 100644
@@ -100,6 +100,8 @@ struct NetClientState {
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
     int vring_enable;
+    bool using_vnet_hdr;
+    int vnet_hdr_len;
     QTAILQ_HEAD(NetFilterHead, NetFilterState) filters;
 };
 
net/net.c +1 -0
index 0ac3b9e..a00a0c9 100644
@@ -472,6 +472,7 @@ void qemu_using_vnet_hdr(NetClientState *nc, 
 bool enable)
         return;
     }
 
+    nc->using_vnet_hdr = enable;
     nc->info->using_vnet_hdr(nc, enable);
 }

net/net.c void qemu_set_vnet_hdr_len(NetClientState *nc, · line 498

Quoted 7 lines
net/net.c +1 -0
@@ -491,6 +492,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, 
 int len)
         return;
     }
 
+    nc->vnet_hdr_len = len;
     nc->info->set_vnet_hdr_len(nc, len);
 }

For what it's worth, now having those fields in NetClientState it is possible to remove a deref to NetClientInfo in qemu_has_vnet_hdr() and qemu_has_vnet_hdr_len().

Anyway,

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Yes and this could be done on top with removing private e.g vnet_hdr_len.

Do you means we will remove the qemu_has_vnet_hdr() and qemu_has_vnet_hdr_len() in the future?

Thanks
Zhang Chen

Yes and e.g both tap and netmap have its private vnet header field. We can remove them too.

Maybe I should do this job in this series next version or a independent series?

Thanks
Zhang Chen

Quoted 16 lines

Thanks

Quoted 8 lines
Quoted 7 lines

Thanks

.

.

Signature · 2 lines
Thanks
Zhang Chen
#7 lore raw

Re: [Qemu-devel] [PATCH V4 01/12] net: Add vnet_hdr_len related arguments in NetClientState

Quoted 6 lines · Zhang Chen
Quoted 1 line

Yes and e.g both tap and netmap have its private vnet header field. We can remove them too.

Maybe I should do this job in this series next version or a independent series?

Thanks
Zhang Chen

Your call.

Thanks

author
#8 lore raw

Re: [Qemu-devel] [PATCH V4 01/12] net: Add vnet_hdr_len related arguments in NetClientState

Quoted 11 lines · Jason Wang
Quoted 6 lines · Zhang Chen
Quoted 1 line

Yes and e.g both tap and netmap have its private vnet header field. We can remove them too.

Maybe I should do this job in this series next version or a independent series?

Thanks
Zhang Chen

Your call.

I will send a independent series after this series have been merged.

Thanks
Zhang Chen

Quoted 5 lines

Thanks

Signature · 2 lines
Thanks
Zhang Chen
author
#9 lore raw

PATCH v4 2/12 net/filter-mirror.c: Add new option to enable vnet support for filter-mirror

In reply to Zhang Chen

We add the vnet_hdr option for filter-mirror, default is disable. If you use virtio-net-pci net driver, please enable it.
You can use it for example:
-object filter-mirror,id=m0,netdev=hn0,queue=tx,outdev=mirror0,vnet_hdr=on

---
 net/filter-mirror.c | 34 ++++++++++++++++++++++++++++++++++
 qemu-options.hx     |  5 +++--
 2 files changed, 37 insertions(+), 2 deletions(-)
net/filter-mirror.c +34 -0
index 72fa7c2..3766414 100644
@@ -38,6 +38,7 @@ typedef struct MirrorState {
     NetFilterState parent_obj;
     char *indev;
     char *outdev;
+    bool vnet_hdr;
     CharBackend chr_in;
     CharBackend chr_out;
     SocketReadState rs;
@@ -308,6 +309,13 @@ static char *filter_mirror_get_outdev(Object *obj, Error **errp)
     return g_strdup(s->outdev);
 }
 
+static char *filter_mirror_get_vnet_hdr(Object *obj, Error **errp)
+{
+    MirrorState *s = FILTER_MIRROR(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
 static void
 filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
 {
@@ -322,6 +330,21 @@ filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
     }
 }
 
+static void filter_mirror_set_vnet_hdr(Object *obj,
+                                       const char *value,
+                                       Error **errp)
+{
+    MirrorState *s = FILTER_MIRROR(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for filter-mirror vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
 static char *filter_redirector_get_outdev(Object *obj, Error **errp)
 {
     MirrorState *s = FILTER_REDIRECTOR(obj);
@@ -340,8 +363,19 @@ filter_redirector_set_outdev(Object *obj, const char *value, Error **errp)
 
 static void filter_mirror_init(Object *obj)
 {
+    MirrorState *s = FILTER_MIRROR(obj);
+
     object_property_add_str(obj, "outdev", filter_mirror_get_outdev,
                             filter_mirror_set_outdev, NULL);
+
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", filter_mirror_get_vnet_hdr,
+                             filter_mirror_set_vnet_hdr, NULL);
 }
 
 static void filter_redirector_init(Object *obj)
qemu-options.hx +3 -2
index 70c0ded..1e08481 100644
@@ -4024,10 +4024,11 @@ queue @var{all|rx|tx} is an option that can be applied to any netfilter.
 @option{tx}: the filter is attached to the transmit queue of the netdev,
              where it will receive packets sent by the netdev.
 
-@item -object filter-mirror,id=@var{id},netdev=@var{netdevid},outdev=@var{chardevid}[,queue=@var{all|rx|tx}]
+@item -object filter-mirror,id=@var{id},netdev=@var{netdevid},outdev=@var{chardevid},vnet_hdr=@var{on|off}[,queue=@var{all|rx|tx}]
 
 filter-mirror on netdev @var{netdevid},mirror net packet to chardev
-@var{chardevid}
+@var{chardevid}, if vnet_hdr = on, filter-mirror will mirror packet
+with vnet_hdr_len.
 
 @item -object filter-redirector,id=@var{id},netdev=@var{netdevid},indev=@var{chardevid},
 outdev=@var{chardevid}[,queue=@var{all|rx|tx}]
Signature · 1 line
2.7.4
#10 lore raw

Re: [Qemu-devel] [PATCH V4 02/12] net/filter-mirror.c: Add new option to enable vnet support for filter-mirror

Hi,

Quoted 4 lines · Zhang Chen

We add the vnet_hdr option for filter-mirror, default is disable. If you use virtio-net-pci net driver, please enable it.
You can use it for example:
-object filter-mirror,id=m0,netdev=hn0,queue=tx,outdev=mirror0,vnet_hdr=on

Is there any way to detect whether or not the vNIC using vnet_hdr ?
I don't think it is a good idea to let users to confirm it, especially for users who may not be so familiar with the vNIC realizing in qemu.

Thanks,
Hailiang

Quoted 92 lines
---
  net/filter-mirror.c | 34 ++++++++++++++++++++++++++++++++++
  qemu-options.hx     |  5 +++--
  2 files changed, 37 insertions(+), 2 deletions(-)
net/filter-mirror.c +34 -0
index 72fa7c2..3766414 100644
@@ -38,6 +38,7 @@ typedef struct MirrorState {
      NetFilterState parent_obj;
      char *indev;
      char *outdev;
+    bool vnet_hdr;
      CharBackend chr_in;
      CharBackend chr_out;
      SocketReadState rs;
@@ -308,6 +309,13 @@ static char *filter_mirror_get_outdev(Object *obj, Error **errp)
      return g_strdup(s->outdev);
  }
 
+static char *filter_mirror_get_vnet_hdr(Object *obj, Error **errp)
+{
+    MirrorState *s = FILTER_MIRROR(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
  static void
  filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
  {
@@ -322,6 +330,21 @@ filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
      }
  }
 
+static void filter_mirror_set_vnet_hdr(Object *obj,
+                                       const char *value,
+                                       Error **errp)
+{
+    MirrorState *s = FILTER_MIRROR(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for filter-mirror vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
  static char *filter_redirector_get_outdev(Object *obj, Error **errp)
  {
      MirrorState *s = FILTER_REDIRECTOR(obj);
@@ -340,8 +363,19 @@ filter_redirector_set_outdev(Object *obj, const char *value, Error **errp)
 
  static void filter_mirror_init(Object *obj)
  {
+    MirrorState *s = FILTER_MIRROR(obj);
+
      object_property_add_str(obj, "outdev", filter_mirror_get_outdev,
                              filter_mirror_set_outdev, NULL);
+
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", filter_mirror_get_vnet_hdr,
+                             filter_mirror_set_vnet_hdr, NULL);
  }
 
  static void filter_redirector_init(Object *obj)
qemu-options.hx +3 -2
index 70c0ded..1e08481 100644
@@ -4024,10 +4024,11 @@ queue @var{all|rx|tx} is an option that can be applied to any netfilter.
  @option{tx}: the filter is attached to the transmit queue of the netdev,
               where it will receive packets sent by the netdev.
 
-@item -object filter-mirror,id=@var{id},netdev=@var{netdevid},outdev=@var{chardevid}[,queue=@var{all|rx|tx}]
+@item -object filter-mirror,id=@var{id},netdev=@var{netdevid},outdev=@var{chardevid},vnet_hdr=@var{on|off}[,queue=@var{all|rx|tx}]
 
  filter-mirror on netdev @var{netdevid},mirror net packet to chardev
-@var{chardevid}
+@var{chardevid}, if vnet_hdr = on, filter-mirror will mirror packet
+with vnet_hdr_len.
 
  @item -object filter-redirector,id=@var{id},netdev=@var{netdevid},indev=@var{chardevid},
  outdev=@var{chardevid}[,queue=@var{all|rx|tx}]
author
#11 lore raw

Re: [Qemu-devel] [PATCH V4 02/12] net/filter-mirror.c: Add new option to enable vnet support for filter-mirror

Quoted 10 lines · Hailiang Zhang

Hi,

Quoted 5 lines · Zhang Chen

We add the vnet_hdr option for filter-mirror, default is disable. If you use virtio-net-pci net driver, please enable it. You can use it for example:
-object filter-mirror,id=m0,netdev=hn0,queue=tx,outdev=mirror0,vnet_hdr=on

Is there any way to detect whether or not the vNIC using vnet_hdr ?

Yes, we can. but we can't ensure the backward compatibility. Detail:
https://lists.nongnu.org/archive/html/qemu-devel/2017-05/msg01959.html

Quoted 3 lines

I don't think it is a good idea to let users to confirm it, especially for users who may not be so familiar with the vNIC realizing in qemu.

If you don't use virtio-net-pci that you need't add this option, default is false.

Thanks
Zhang Chen

Quoted 104 lines

Thanks,
Hailiang

Quoted 86 lines
---
  net/filter-mirror.c | 34 ++++++++++++++++++++++++++++++++++
  qemu-options.hx     |  5 +++--
  2 files changed, 37 insertions(+), 2 deletions(-)
net/filter-mirror.c +32 -0
index 72fa7c2..3766414 100644
@@ -38,6 +38,7 @@ typedef struct MirrorState {
      NetFilterState parent_obj;
      char *indev;
      char *outdev;
+    bool vnet_hdr;
      CharBackend chr_in;
      CharBackend chr_out;
      SocketReadState rs;
@@ -308,6 +309,13 @@ static char *filter_mirror_get_outdev(Object 
 *obj, Error **errp)
      return g_strdup(s->outdev);
  }
  +static char *filter_mirror_get_vnet_hdr(Object *obj, Error **errp)
+{
+    MirrorState *s = FILTER_MIRROR(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
  static void
  filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
  {
@@ -322,6 +330,21 @@ filter_mirror_set_outdev(Object *obj, const char 
 *value, Error **errp)
      }
  }
  +static void filter_mirror_set_vnet_hdr(Object *obj,
+                                       const char *value,
+                                       Error **errp)
+{
+    MirrorState *s = FILTER_MIRROR(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for filter-mirror vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
  static char *filter_redirector_get_outdev(Object *obj, Error **errp)
  {
      MirrorState *s = FILTER_REDIRECTOR(obj);
@@ -340,8 +363,19 @@ filter_redirector_set_outdev(Object *obj, const 
 char *value, Error **errp)
    static void filter_mirror_init(Object *obj)
  {
+    MirrorState *s = FILTER_MIRROR(obj);
+
      object_property_add_str(obj, "outdev", filter_mirror_get_outdev,
                              filter_mirror_set_outdev, NULL);
+
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", 
 filter_mirror_get_vnet_hdr,
+                             filter_mirror_set_vnet_hdr, NULL);
  }
    static void filter_redirector_init(Object *obj)
qemu-options.hx +0 -0
index 70c0ded..1e08481 100644
@@ -4024,10 +4024,11 @@ queue @var{all|rx|tx} is an option that can 

be applied to any netfilter.
@option{tx}: the filter is attached to the transmit queue of the
netdev,
where it will receive packets sent by the netdev.
-@item -object
filter-mirror,id=@var{id},netdev=@var{netdevid},outdev=@var{chardevid}[,queue=@var{all|rx|tx}] +@item -object

Quoted 8 lines

filter-mirror,id=@var{id},netdev=@var{netdevid},outdev=@var{chardevid},vnet_hdr=@var{on|off}[,queue=@var{all|rx|tx}]
filter-mirror on netdev @var{netdevid},mirror net packet to chardev
-@var{chardevid}
+@var{chardevid}, if vnet_hdr = on, filter-mirror will mirror packet
+with vnet_hdr_len.
@item -object
filter-redirector,id=@var{id},netdev=@var{netdevid},indev=@var{chardevid},
outdev=@var{chardevid}[,queue=@var{all|rx|tx}]

.

Signature · 2 lines
Thanks
Zhang Chen
#12 lore raw

Re: [Qemu-devel] [PATCH V4 02/12] net/filter-mirror.c: Add new option to enable vnet support for filter-mirror

In reply to Zhang Chen
Quoted 67 lines · Zhang Chen

We add the vnet_hdr option for filter-mirror, default is disable. If you use virtio-net-pci net driver, please enable it.
You can use it for example:
-object filter-mirror,id=m0,netdev=hn0,queue=tx,outdev=mirror0,vnet_hdr=on

---
  net/filter-mirror.c | 34 ++++++++++++++++++++++++++++++++++
  qemu-options.hx     |  5 +++--
  2 files changed, 37 insertions(+), 2 deletions(-)
net/filter-mirror.c +25 -0
index 72fa7c2..3766414 100644
@@ -38,6 +38,7 @@ typedef struct MirrorState {
      NetFilterState parent_obj;
      char *indev;
      char *outdev;
+    bool vnet_hdr;
      CharBackend chr_in;
      CharBackend chr_out;
      SocketReadState rs;
@@ -308,6 +309,13 @@ static char *filter_mirror_get_outdev(Object *obj, Error **errp)
      return g_strdup(s->outdev);
  }
 
+static char *filter_mirror_get_vnet_hdr(Object *obj, Error **errp)
+{
+    MirrorState *s = FILTER_MIRROR(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
  static void
  filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
  {
@@ -322,6 +330,21 @@ filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
      }
  }
 
+static void filter_mirror_set_vnet_hdr(Object *obj,
+                                       const char *value,
+                                       Error **errp)
+{
+    MirrorState *s = FILTER_MIRROR(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for filter-mirror vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
  static char *filter_redirector_get_outdev(Object *obj, Error **errp)
  {
      MirrorState *s = FILTER_REDIRECTOR(obj);
@@ -340,8 +363,19 @@ filter_redirector_set_outdev(Object *obj, const char *value, Error **errp)
 
  static void filter_mirror_init(Object *obj)
  {
+    MirrorState *s = FILTER_MIRROR(obj);
+
      object_property_add_str(obj, "outdev", filter_mirror_get_outdev,
                              filter_mirror_set_outdev, NULL);

net/filter-mirror.c filter_redirector_set_outdev(Object *obj, const char *value, Error **errp) · line 379

Quoted 10 lines
net/filter-mirror.c +9 -0
+
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", filter_mirror_get_vnet_hdr,
+                             filter_mirror_set_vnet_hdr, NULL);
  }

We'd better squash this into patch 3 and even after the changes for net_fill_rstate().

Quoted 7 lines
net/filter-mirror.c +0 -0
  static void filter_redirector_init(Object *obj)
qemu-options.hx +0 -0
index 70c0ded..1e08481 100644

qemu-options.hx queue @var{all|rx|tx} is an option that can be applied to any netfilter. · line 4031

Quoted 10 lines
qemu-options.hx +3 -2
@@ -4024,10 +4024,11 @@ queue @var{all|rx|tx} is an option that can be applied to any netfilter.
  @option{tx}: the filter is attached to the transmit queue of the netdev,
               where it will receive packets sent by the netdev.
 
-@item -object filter-mirror,id=@var{id},netdev=@var{netdevid},outdev=@var{chardevid}[,queue=@var{all|rx|tx}]
+@item -object filter-mirror,id=@var{id},netdev=@var{netdevid},outdev=@var{chardevid},vnet_hdr=@var{on|off}[,queue=@var{all|rx|tx}]
 
  filter-mirror on netdev @var{netdevid},mirror net packet to chardev
-@var{chardevid}
+@var{chardevid}, if vnet_hdr = on, filter-mirror will mirror packet
+with vnet_hdr_len.

As pointed by Eric, we'd better keep the long line here.

Thanks

Quoted 3 lines
qemu-options.hx +0 -0
  @item -object filter-redirector,id=@var{id},netdev=@var{netdevid},indev=@var{chardevid},
  outdev=@var{chardevid}[,queue=@var{all|rx|tx}]
author
#13 lore raw

Re: [Qemu-devel] [PATCH V4 02/12] net/filter-mirror.c: Add new option to enable vnet support for filter-mirror

Quoted 76 lines · Jason Wang
Quoted 73 lines · Zhang Chen

We add the vnet_hdr option for filter-mirror, default is disable. If you use virtio-net-pci net driver, please enable it. You can use it for example:
-object filter-mirror,id=m0,netdev=hn0,queue=tx,outdev=mirror0,vnet_hdr=on

---
  net/filter-mirror.c | 34 ++++++++++++++++++++++++++++++++++
  qemu-options.hx     |  5 +++--
  2 files changed, 37 insertions(+), 2 deletions(-)
net/filter-mirror.c +28 -0
index 72fa7c2..3766414 100644
@@ -38,6 +38,7 @@ typedef struct MirrorState {
      NetFilterState parent_obj;
      char *indev;
      char *outdev;
+    bool vnet_hdr;
      CharBackend chr_in;
      CharBackend chr_out;
      SocketReadState rs;
@@ -308,6 +309,13 @@ static char *filter_mirror_get_outdev(Object 
 *obj, Error **errp)
      return g_strdup(s->outdev);
  }
  +static char *filter_mirror_get_vnet_hdr(Object *obj, Error **errp)
+{
+    MirrorState *s = FILTER_MIRROR(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
  static void
  filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
  {
@@ -322,6 +330,21 @@ filter_mirror_set_outdev(Object *obj, const char 
 *value, Error **errp)
      }
  }
  +static void filter_mirror_set_vnet_hdr(Object *obj,
+                                       const char *value,
+                                       Error **errp)
+{
+    MirrorState *s = FILTER_MIRROR(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for filter-mirror vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
  static char *filter_redirector_get_outdev(Object *obj, Error **errp)
  {
      MirrorState *s = FILTER_REDIRECTOR(obj);
@@ -340,8 +363,19 @@ filter_redirector_set_outdev(Object *obj, const 
 char *value, Error **errp)
    static void filter_mirror_init(Object *obj)
  {
+    MirrorState *s = FILTER_MIRROR(obj);
+
      object_property_add_str(obj, "outdev", filter_mirror_get_outdev,
                              filter_mirror_set_outdev, NULL);
+
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
Quoted 8 lines

net/filter-mirror.c filter_redirector_set_outdev(Object *obj, const char *value, Error **errp) · line 379

Quoted 6 lines
net/filter-mirror.c +4 -0
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", 
 filter_mirror_get_vnet_hdr,
+                             filter_mirror_set_vnet_hdr, NULL);
  }

We'd better squash this into patch 3 and even after the changes for net_fill_rstate().

OK, I will squash it in next version.

Quoted 13 lines
Quoted 13 lines
net/filter-mirror.c +0 -0
    static void filter_redirector_init(Object *obj)
qemu-options.hx +0 -0
index 70c0ded..1e08481 100644
@@ -4024,10 +4024,11 @@ queue @var{all|rx|tx} is an option that can 

be applied to any netfilter.
@option{tx}: the filter is attached to the transmit queue of the
netdev,
where it will receive packets sent by the netdev.
-@item -object
filter-mirror,id=@var{id},netdev=@var{netdevid},outdev=@var{chardevid}[,queue=@var{all|rx|tx}]

Quoted 8 lines
Quoted 6 lines

+@item -object filter-mirror,id=@var{id},netdev=@var{netdevid},outdev=@var{chardevid},vnet_hdr=@var{on|off}[,queue=@var{all|rx|tx}]
filter-mirror on netdev @var{netdevid},mirror net packet to chardev
-@var{chardevid}
+@var{chardevid}, if vnet_hdr = on, filter-mirror will mirror packet
+with vnet_hdr_len.

As pointed by Eric, we'd better keep the long line here.

OK.

Thanks
Zhang Chen

Quoted 11 lines

Thanks

Quoted 3 lines

@item -object
filter-redirector,id=@var{id},netdev=@var{netdevid},indev=@var{chardevid},
outdev=@var{chardevid}[,queue=@var{all|rx|tx}]

.

Signature · 2 lines
Thanks
Zhang Chen
author
#14 lore raw

PATCH v4 3/12 net/filter-mirror.c: Make filter_mirror_send support vnet support.

In reply to Zhang Chen

In this patch, if vnet_hdr=on we change the send packet format from
struct {int size; const uint8_t buf[];} to {int size; int vnet_hdr_len; const uint8_t buf[];}. make other module(like colo-compare) know how to parse net packet correctly.

---
 net/filter-mirror.c | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)
net/filter-mirror.c +30 -5
index 3766414..64323fc 100644
@@ -44,10 +44,11 @@ typedef struct MirrorState {
     SocketReadState rs;
 } MirrorState;
 
-static int filter_mirror_send(CharBackend *chr_out,
+static int filter_mirror_send(MirrorState *s,
                               const struct iovec *iov,
                               int iovcnt)
 {
+    NetFilterState *nf = NETFILTER(s);
     int ret = 0;
     ssize_t size = 0;
     uint32_t len = 0;
@@ -59,14 +60,38 @@ static int filter_mirror_send(CharBackend *chr_out,
     }
 
     len = htonl(size);
-    ret = qemu_chr_fe_write_all(chr_out, (uint8_t *)&len, sizeof(len));
+    ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len));
     if (ret != sizeof(len)) {
         goto err;
     }
 
+    if (s->vnet_hdr) {
+        /*
+         * If vnet_hdr = on, we send vnet header len to make other
+         * module(like colo-compare) know how to parse net
+         * packet correctly.
+         */
+        ssize_t vnet_hdr_len;
+
+        if (nf->netdev->using_vnet_hdr) {
+            vnet_hdr_len = nf->netdev->vnet_hdr_len;
+        } else if (nf->netdev->peer->using_vnet_hdr) {
+            vnet_hdr_len = nf->netdev->peer->vnet_hdr_len;
+        } else {
+            error_report("filter get vnet_hdr_len failed");
+            goto err;
+        }
+
+        len = htonl(vnet_hdr_len);
+        ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len));
+        if (ret != sizeof(len)) {
+            goto err;
+        }
+    }
+
     buf = g_malloc(size);
     iov_to_buf(iov, iovcnt, 0, buf, size);
-    ret = qemu_chr_fe_write_all(chr_out, (uint8_t *)buf, size);
+    ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size);
     g_free(buf);
     if (ret != size) {
         goto err;
@@ -142,7 +167,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf,
     MirrorState *s = FILTER_MIRROR(nf);
     int ret;
 
-    ret = filter_mirror_send(&s->chr_out, iov, iovcnt);
+    ret = filter_mirror_send(s, iov, iovcnt);
     if (ret) {
         error_report("filter_mirror_send failed(%s)", strerror(-ret));
     }
@@ -165,7 +190,7 @@ static ssize_t filter_redirector_receive_iov(NetFilterState *nf,
     int ret;
 
     if (qemu_chr_fe_get_driver(&s->chr_out)) {
-        ret = filter_mirror_send(&s->chr_out, iov, iovcnt);
+        ret = filter_mirror_send(s, iov, iovcnt);
         if (ret) {
             error_report("filter_mirror_send failed(%s)", strerror(-ret));
         }
Signature · 1 line
2.7.4
#15 lore raw

Re: [Qemu-devel] [PATCH V4 03/12] net/filter-mirror.c: Make filter_mirror_send support vnet support.

Quoted 38 lines · Zhang Chen

In this patch, if vnet_hdr=on we change the send packet format from
struct {int size; const uint8_t buf[];} to {int size; int vnet_hdr_len; const uint8_t buf[];}. make other module(like colo-compare) know how to parse net packet correctly.

---
  net/filter-mirror.c | 35 ++++++++++++++++++++++++++++++-----
  1 file changed, 30 insertions(+), 5 deletions(-)
net/filter-mirror.c +5 -2
index 3766414..64323fc 100644
@@ -44,10 +44,11 @@ typedef struct MirrorState {
      SocketReadState rs;
  } MirrorState;
 
-static int filter_mirror_send(CharBackend *chr_out,
+static int filter_mirror_send(MirrorState *s,
                                const struct iovec *iov,
                                int iovcnt)
  {
+    NetFilterState *nf = NETFILTER(s);
      int ret = 0;
      ssize_t size = 0;
      uint32_t len = 0;
@@ -59,14 +60,38 @@ static int filter_mirror_send(CharBackend *chr_out,
      }
 
      len = htonl(size);
-    ret = qemu_chr_fe_write_all(chr_out, (uint8_t *)&len, sizeof(len));
+    ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len));
      if (ret != sizeof(len)) {
          goto err;
      }
 
+    if (s->vnet_hdr) {
+        /*

net/filter-mirror.c static int filter_mirror_send(CharBackend *chr_out, · line 79

Quoted 10 lines
net/filter-mirror.c +10 -0
+         * If vnet_hdr = on, we send vnet header len to make other
+         * module(like colo-compare) know how to parse net
+         * packet correctly.
+         */
+        ssize_t vnet_hdr_len;
+
+        if (nf->netdev->using_vnet_hdr) {
+            vnet_hdr_len = nf->netdev->vnet_hdr_len;
+        } else if (nf->netdev->peer->using_vnet_hdr) {
+            vnet_hdr_len = nf->netdev->peer->vnet_hdr_len;

Still questionable here, may need a comment to explain.

net/filter-mirror.c static int filter_mirror_send(CharBackend *chr_out, · line 81

Quoted 2 lines
net/filter-mirror.c +2 -0
+        } else {
+            error_report("filter get vnet_hdr_len failed");

Why need error here, could we just use zero?

Thanks

Quoted 35 lines
net/filter-mirror.c +13 -3
+            goto err;
+        }
+
+        len = htonl(vnet_hdr_len);
+        ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len));
+        if (ret != sizeof(len)) {
+            goto err;
+        }
+    }
+
      buf = g_malloc(size);
      iov_to_buf(iov, iovcnt, 0, buf, size);
-    ret = qemu_chr_fe_write_all(chr_out, (uint8_t *)buf, size);
+    ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size);
      g_free(buf);
      if (ret != size) {
          goto err;
@@ -142,7 +167,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf,
      MirrorState *s = FILTER_MIRROR(nf);
      int ret;
 
-    ret = filter_mirror_send(&s->chr_out, iov, iovcnt);
+    ret = filter_mirror_send(s, iov, iovcnt);
      if (ret) {
          error_report("filter_mirror_send failed(%s)", strerror(-ret));
      }
@@ -165,7 +190,7 @@ static ssize_t filter_redirector_receive_iov(NetFilterState *nf,
      int ret;
 
      if (qemu_chr_fe_get_driver(&s->chr_out)) {
-        ret = filter_mirror_send(&s->chr_out, iov, iovcnt);
+        ret = filter_mirror_send(s, iov, iovcnt);
          if (ret) {
              error_report("filter_mirror_send failed(%s)", strerror(-ret));
          }
author
#16 lore raw

Re: [Qemu-devel] [PATCH V4 03/12] net/filter-mirror.c: Make filter_mirror_send support vnet support.

Quoted 43 lines · Jason Wang
Quoted 40 lines · Zhang Chen

In this patch, if vnet_hdr=on we change the send packet format from
struct {int size; const uint8_t buf[];} to {int size; int vnet_hdr_len; const uint8_t buf[];}. make other module(like colo-compare) know how to parse net packet correctly.

---
  net/filter-mirror.c | 35 ++++++++++++++++++++++++++++++-----
  1 file changed, 30 insertions(+), 5 deletions(-)
net/filter-mirror.c +8 -1
index 3766414..64323fc 100644
@@ -44,10 +44,11 @@ typedef struct MirrorState {
      SocketReadState rs;
  } MirrorState;
  -static int filter_mirror_send(CharBackend *chr_out,
+static int filter_mirror_send(MirrorState *s,
                                const struct iovec *iov,
                                int iovcnt)
  {
+    NetFilterState *nf = NETFILTER(s);
      int ret = 0;
      ssize_t size = 0;
      uint32_t len = 0;
@@ -59,14 +60,38 @@ static int filter_mirror_send(CharBackend *chr_out,
      }
        len = htonl(size);
-    ret = qemu_chr_fe_write_all(chr_out, (uint8_t *)&len, sizeof(len));
+    ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, 
 sizeof(len));
      if (ret != sizeof(len)) {
          goto err;
      }
  +    if (s->vnet_hdr) {
+        /*
+         * If vnet_hdr = on, we send vnet header len to make other
+         * module(like colo-compare) know how to parse net
+         * packet correctly.
+         */
Quoted 9 lines

net/filter-mirror.c static int filter_mirror_send(CharBackend *chr_out, · line 79

Quoted 6 lines
net/filter-mirror.c +6 -0
+        ssize_t vnet_hdr_len;
+
+        if (nf->netdev->using_vnet_hdr) {
+            vnet_hdr_len = nf->netdev->vnet_hdr_len;
+        } else if (nf->netdev->peer->using_vnet_hdr) {
+            vnet_hdr_len = nf->netdev->peer->vnet_hdr_len;

Still questionable here, may need a comment to explain.

OK, I will add comments in next version.

Quoted 4 lines

net/filter-mirror.c static int filter_mirror_send(CharBackend *chr_out, · line 81

Quoted 2 lines
net/filter-mirror.c +2 -0
+        } else {
+            error_report("filter get vnet_hdr_len failed");

Why need error here, could we just use zero?

Yes,we can.

Thanks
Zhang Chen

Quoted 45 lines

Thanks

Quoted 36 lines
net/filter-mirror.c +13 -2
+            goto err;
+        }
+
+        len = htonl(vnet_hdr_len);
+        ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, 
 sizeof(len));
+        if (ret != sizeof(len)) {
+            goto err;
+        }
+    }
+
      buf = g_malloc(size);
      iov_to_buf(iov, iovcnt, 0, buf, size);
-    ret = qemu_chr_fe_write_all(chr_out, (uint8_t *)buf, size);
+    ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size);
      g_free(buf);
      if (ret != size) {
          goto err;
@@ -142,7 +167,7 @@ static ssize_t 
 filter_mirror_receive_iov(NetFilterState *nf,
      MirrorState *s = FILTER_MIRROR(nf);
      int ret;
  -    ret = filter_mirror_send(&s->chr_out, iov, iovcnt);
+    ret = filter_mirror_send(s, iov, iovcnt);
      if (ret) {
          error_report("filter_mirror_send failed(%s)", strerror(-ret));
      }
@@ -165,7 +190,7 @@ static ssize_t 
 filter_redirector_receive_iov(NetFilterState *nf,
      int ret;
        if (qemu_chr_fe_get_driver(&s->chr_out)) {
-        ret = filter_mirror_send(&s->chr_out, iov, iovcnt);
+        ret = filter_mirror_send(s, iov, iovcnt);
          if (ret) {
              error_report("filter_mirror_send failed(%s)", 
 strerror(-ret));
Quoted 1 line
          }

.

Signature · 2 lines
Thanks
Zhang Chen
author
#17 lore raw

PATCH v4 4/12 net/filter-mirror.c: Add new option to enable vnet support for filter-redirector

In reply to Zhang Chen

We add the vnet_hdr option for filter-redirector, default is disable. If you use virtio-net-pci net driver, please enable it.
You can use it for example:
-object filter-redirector,id=r0,netdev=hn0,queue=tx,outdev=red0,vnet_hdr=on

---
 net/filter-mirror.c | 33 +++++++++++++++++++++++++++++++++
 qemu-options.hx     |  5 +++--
 2 files changed, 36 insertions(+), 2 deletions(-)
net/filter-mirror.c +33 -0
index 64323fc..a65853c 100644
@@ -377,6 +377,13 @@ static char *filter_redirector_get_outdev(Object *obj, Error **errp)
     return g_strdup(s->outdev);
 }
 
+static char *filter_redirector_get_vnet_hdr(Object *obj, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
 static void
 filter_redirector_set_outdev(Object *obj, const char *value, Error **errp)
 {
@@ -386,6 +393,21 @@ filter_redirector_set_outdev(Object *obj, const char *value, Error **errp)
     s->outdev = g_strdup(value);
 }
 
+static void filter_redirector_set_vnet_hdr(Object *obj,
+                                           const char *value,
+                                           Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for filter-redirector vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
 static void filter_mirror_init(Object *obj)
 {
     MirrorState *s = FILTER_MIRROR(obj);
@@ -405,10 +427,21 @@ static void filter_mirror_init(Object *obj)
 
 static void filter_redirector_init(Object *obj)
 {
+    MirrorState *s = FILTER_REDIRECTOR(obj);
+
     object_property_add_str(obj, "indev", filter_redirector_get_indev,
                             filter_redirector_set_indev, NULL);
     object_property_add_str(obj, "outdev", filter_redirector_get_outdev,
                             filter_redirector_set_outdev, NULL);
+
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", filter_redirector_get_vnet_hdr,
+                            filter_redirector_set_vnet_hdr, NULL);
 }
 
 static void filter_mirror_fini(Object *obj)
qemu-options.hx +3 -2
index 1e08481..0f81c22 100644
@@ -4031,10 +4031,11 @@ filter-mirror on netdev @var{netdevid},mirror net packet to chardev
 with vnet_hdr_len.
 
 @item -object filter-redirector,id=@var{id},netdev=@var{netdevid},indev=@var{chardevid},
-outdev=@var{chardevid}[,queue=@var{all|rx|tx}]
+outdev=@var{chardevid},vnet_hdr=@var{on|off}[,queue=@var{all|rx|tx}]
 
 filter-redirector on netdev @var{netdevid},redirect filter's net packet to chardev
-@var{chardevid},and redirect indev's packet to filter.
+@var{chardevid},and redirect indev's packet to filter.if vnet_hdr = on,
+filter-redirector will redirect packet with vnet_hdr_len.
 Create a filter-redirector we need to differ outdev id from indev id, id can not
 be the same. we can just use indev or outdev, but at least one of indev or outdev
 need to be specified.
Signature · 1 line
2.7.4
#18 lore raw

Re: [Qemu-devel] [PATCH V4 04/12] net/filter-mirror.c: Add new option to enable vnet support for filter-redirector

Quoted 2 lines · Zhang Chen

We add the vnet_hdr option for filter-redirector, default is disable. If you use virtio-net-pci net driver, please enable it.

We need a better commit log here to explain e.g why we need this option.

Thanks

Quoted 89 lines

You can use it for example:
-object filter-redirector,id=r0,netdev=hn0,queue=tx,outdev=red0,vnet_hdr=on

---
  net/filter-mirror.c | 33 +++++++++++++++++++++++++++++++++
  qemu-options.hx     |  5 +++--
  2 files changed, 36 insertions(+), 2 deletions(-)
net/filter-mirror.c +33 -0
index 64323fc..a65853c 100644
@@ -377,6 +377,13 @@ static char *filter_redirector_get_outdev(Object *obj, Error **errp)
      return g_strdup(s->outdev);
  }
 
+static char *filter_redirector_get_vnet_hdr(Object *obj, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
  static void
  filter_redirector_set_outdev(Object *obj, const char *value, Error **errp)
  {
@@ -386,6 +393,21 @@ filter_redirector_set_outdev(Object *obj, const char *value, Error **errp)
      s->outdev = g_strdup(value);
  }
 
+static void filter_redirector_set_vnet_hdr(Object *obj,
+                                           const char *value,
+                                           Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for filter-redirector vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
  static void filter_mirror_init(Object *obj)
  {
      MirrorState *s = FILTER_MIRROR(obj);
@@ -405,10 +427,21 @@ static void filter_mirror_init(Object *obj)
 
  static void filter_redirector_init(Object *obj)
  {
+    MirrorState *s = FILTER_REDIRECTOR(obj);
+
      object_property_add_str(obj, "indev", filter_redirector_get_indev,
                              filter_redirector_set_indev, NULL);
      object_property_add_str(obj, "outdev", filter_redirector_get_outdev,
                              filter_redirector_set_outdev, NULL);
+
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", filter_redirector_get_vnet_hdr,
+                            filter_redirector_set_vnet_hdr, NULL);
  }
 
  static void filter_mirror_fini(Object *obj)
qemu-options.hx +3 -2
index 1e08481..0f81c22 100644
@@ -4031,10 +4031,11 @@ filter-mirror on netdev @var{netdevid},mirror net packet to chardev
  with vnet_hdr_len.
 
  @item -object filter-redirector,id=@var{id},netdev=@var{netdevid},indev=@var{chardevid},
-outdev=@var{chardevid}[,queue=@var{all|rx|tx}]
+outdev=@var{chardevid},vnet_hdr=@var{on|off}[,queue=@var{all|rx|tx}]
 
  filter-redirector on netdev @var{netdevid},redirect filter's net packet to chardev
-@var{chardevid},and redirect indev's packet to filter.
+@var{chardevid},and redirect indev's packet to filter.if vnet_hdr = on,
+filter-redirector will redirect packet with vnet_hdr_len.
  Create a filter-redirector we need to differ outdev id from indev id, id can not
  be the same. we can just use indev or outdev, but at least one of indev or outdev
  need to be specified.
author
#19 lore raw

Re: [Qemu-devel] [PATCH V4 04/12] net/filter-mirror.c: Add new option to enable vnet support for filter-redirector

Quoted 7 lines · Jason Wang
Quoted 2 lines · Zhang Chen

We add the vnet_hdr option for filter-redirector, default is disable. If you use virtio-net-pci net driver, please enable it.

We need a better commit log here to explain e.g why we need this option.

OK. I will more comments in next version.

Thanks
Zhang Chen

Quoted 104 lines

Thanks

Quoted 88 lines

You can use it for example:
-object filter-redirector,id=r0,netdev=hn0,queue=tx,outdev=red0,vnet_hdr=on

---
  net/filter-mirror.c | 33 +++++++++++++++++++++++++++++++++
  qemu-options.hx     |  5 +++--
  2 files changed, 36 insertions(+), 2 deletions(-)
net/filter-mirror.c +6 -0
index 64323fc..a65853c 100644
@@ -377,6 +377,13 @@ static char *filter_redirector_get_outdev(Object 
 *obj, Error **errp)
      return g_strdup(s->outdev);
  }
  +static char *filter_redirector_get_vnet_hdr(Object *obj, Error 
 **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
  static void
  filter_redirector_set_outdev(Object *obj, const char *value, Error 

**errp)
{

net/filter-mirror.c +7 -0
@@ -386,6 +393,21 @@ filter_redirector_set_outdev(Object *obj, const 
 char *value, Error **errp)
      s->outdev = g_strdup(value);
  }
  +static void filter_redirector_set_vnet_hdr(Object *obj,
+                                           const char *value,
+                                           Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for filter-redirector 

vnet_hdr, "

net/filter-mirror.c +18 -0
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
  static void filter_mirror_init(Object *obj)
  {
      MirrorState *s = FILTER_MIRROR(obj);
@@ -405,10 +427,21 @@ static void filter_mirror_init(Object *obj)
    static void filter_redirector_init(Object *obj)
  {
+    MirrorState *s = FILTER_REDIRECTOR(obj);
+
      object_property_add_str(obj, "indev", filter_redirector_get_indev,
                              filter_redirector_set_indev, NULL);
      object_property_add_str(obj, "outdev", 
 filter_redirector_get_outdev,
                              filter_redirector_set_outdev, NULL);
+
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", 
 filter_redirector_get_vnet_hdr,
+                            filter_redirector_set_vnet_hdr, NULL);
  }
    static void filter_mirror_fini(Object *obj)
qemu-options.hx +0 -0
index 1e08481..0f81c22 100644
@@ -4031,10 +4031,11 @@ filter-mirror on netdev @var{netdevid},mirror 

net packet to chardev
with vnet_hdr_len.
@item -object
filter-redirector,id=@var{id},netdev=@var{netdevid},indev=@var{chardevid},

qemu-options.hx +1 -1
-outdev=@var{chardevid}[,queue=@var{all|rx|tx}]
+outdev=@var{chardevid},vnet_hdr=@var{on|off}[,queue=@var{all|rx|tx}]
    filter-redirector on netdev @var{netdevid},redirect filter's net 

packet to chardev

Quoted 8 lines

-@var{chardevid},and redirect indev's packet to filter. +@var{chardevid},and redirect indev's packet to filter.if vnet_hdr = on, +filter-redirector will redirect packet with vnet_hdr_len.
Create a filter-redirector we need to differ outdev id from indev
id, id can not
be the same. we can just use indev or outdev, but at least one of
indev or outdev
need to be specified.

.

Signature · 2 lines
Thanks
Zhang Chen
author
#20 lore raw

PATCH v4 5/12 net/net.c: Add vnet_hdr support in SocketReadState

In reply to Zhang Chen

Address Jason Wang's comments add vnet header length to SocketReadState. We add a flag to dicide whether net_fill_rstate() to read struct {int size; int vnet_hdr_len; const uint8_t buf[];} or not.

---
 include/net/net.h   |  9 +++++++--
 net/colo-compare.c  |  4 ++--
 net/filter-mirror.c |  2 +-
 net/net.c           | 36 ++++++++++++++++++++++++++++++++----
 net/socket.c        |  2 +-
 5 files changed, 43 insertions(+), 10 deletions(-)
include/net/net.h +7 -2
index 70edfc0..0763636 100644
@@ -113,14 +113,19 @@ typedef struct NICState {
 } NICState;
 
 struct SocketReadState {
-    int state; /* 0 = getting length, 1 = getting data */
+    /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */
+    int state;
     uint32_t index;
     uint32_t packet_len;
+    uint32_t vnet_hdr_len;
     uint8_t buf[NET_BUFSIZE];
     SocketReadStateFinalize *finalize;
 };
 
-int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size);
+int net_fill_rstate(SocketReadState *rs,
+                    const uint8_t *buf,
+                    int size,
+                    bool vnet_hdr);
 char *qemu_mac_strdup_printf(const uint8_t *macaddr);
 NetClientState *qemu_find_netdev(const char *id);
 int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
net/colo-compare.c +2 -2
index 4ab80b1..332f57e 100644
@@ -530,7 +530,7 @@ static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
     CompareState *s = COLO_COMPARE(opaque);
     int ret;
 
-    ret = net_fill_rstate(&s->pri_rs, buf, size);
+    ret = net_fill_rstate(&s->pri_rs, buf, size, false);
     if (ret == -1) {
         qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
                                  NULL, NULL, true);
@@ -547,7 +547,7 @@ static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
     CompareState *s = COLO_COMPARE(opaque);
     int ret;
 
-    ret = net_fill_rstate(&s->sec_rs, buf, size);
+    ret = net_fill_rstate(&s->sec_rs, buf, size, false);
     if (ret == -1) {
         qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
                                  NULL, NULL, true);
net/filter-mirror.c +1 -1
index a65853c..4649416 100644
@@ -134,7 +134,7 @@ static void redirector_chr_read(void *opaque, const uint8_t *buf, int size)
     MirrorState *s = FILTER_REDIRECTOR(nf);
     int ret;
 
-    ret = net_fill_rstate(&s->rs, buf, size);
+    ret = net_fill_rstate(&s->rs, buf, size, s->vnet_hdr);
 
     if (ret == -1) {
         qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
net/net.c +32 -4
index a00a0c9..a9c97cf 100644
@@ -1618,13 +1618,20 @@ void net_socket_rs_init(SocketReadState *rs,
  * 0: success
  * -1: error occurs
  */
-int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
+int net_fill_rstate(SocketReadState *rs,
+                    const uint8_t *buf,
+                    int size,
+                    bool vnet_hdr)
 {
     unsigned int l;
 
     while (size > 0) {
-        /* reassemble a packet from the network */
-        switch (rs->state) { /* 0 = getting length, 1 = getting data */
+        /* Reassemble a packet from the network.
+         * 0 = getting length.
+         * 1 = getting vnet header length.
+         * 2 = getting data.
+         */
+        switch (rs->state) {
         case 0:
             l = 4 - rs->index;
             if (l > size) {
@@ -1638,10 +1645,31 @@ int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
                 /* got length */
                 rs->packet_len = ntohl(*(uint32_t *)rs->buf);
                 rs->index = 0;
-                rs->state = 1;
+                if (vnet_hdr) {
+                    rs->state = 1;
+                } else {
+                    rs->state = 2;
+                    rs->vnet_hdr_len = 0;
+                }
             }
             break;
         case 1:
+            l = 4 - rs->index;
+            if (l > size) {
+                l = size;
+            }
+            memcpy(rs->buf + rs->index, buf, l);
+            buf += l;
+            size -= l;
+            rs->index += l;
+            if (rs->index == 4) {
+                /* got vnet header length */
+                rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
+                rs->index = 0;
+                rs->state = 2;
+            }
+            break;
+        case 2:
             l = rs->packet_len - rs->index;
             if (l > size) {
                 l = size;
net/socket.c +1 -1
index b8c931e..4e58eff 100644
@@ -182,7 +182,7 @@ static void net_socket_send(void *opaque)
     }
     buf = buf1;
 
-    ret = net_fill_rstate(&s->rs, buf, size);
+    ret = net_fill_rstate(&s->rs, buf, size, false);
 
     if (ret == -1) {
         goto eoc;
Signature · 1 line
2.7.4
#21 lore raw

Re: [Qemu-devel] [PATCH V4 05/12] net/net.c: Add vnet_hdr support in SocketReadState

Quoted 1 line · Zhang Chen

Address Jason Wang's comments add vnet header length to SocketReadState.

Better to use "Suggested-by" instead of this :).

Quoted 2 lines

We add a flag to dicide whether net_fill_rstate() to read struct {int size; int vnet_hdr_len; const uint8_t buf[];} or not.

Few words to explain the format is better than e.g a struct.

Quoted 23 lines
---
  include/net/net.h   |  9 +++++++--
  net/colo-compare.c  |  4 ++--
  net/filter-mirror.c |  2 +-
  net/net.c           | 36 ++++++++++++++++++++++++++++++++----
  net/socket.c        |  2 +-
  5 files changed, 43 insertions(+), 10 deletions(-)
include/net/net.h +2 -1
index 70edfc0..0763636 100644
@@ -113,14 +113,19 @@ typedef struct NICState {
  } NICState;
 
  struct SocketReadState {
-    int state; /* 0 = getting length, 1 = getting data */
+    /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */
+    int state;
      uint32_t index;
      uint32_t packet_len;

include/net/net.h typedef struct NICState { · line 128

Quoted 10 lines
include/net/net.h +5 -1
+    uint32_t vnet_hdr_len;
      uint8_t buf[NET_BUFSIZE];
      SocketReadStateFinalize *finalize;
  };
 
-int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size);
+int net_fill_rstate(SocketReadState *rs,
+                    const uint8_t *buf,
+                    int size,
+                    bool vnet_hdr);

Why not just move vnet_hdr to SocketReadState?

Thanks

Quoted 112 lines
include/net/net.h +0 -0
  char *qemu_mac_strdup_printf(const uint8_t *macaddr);
  NetClientState *qemu_find_netdev(const char *id);
  int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
net/colo-compare.c +2 -2
index 4ab80b1..332f57e 100644
@@ -530,7 +530,7 @@ static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
      CompareState *s = COLO_COMPARE(opaque);
      int ret;
 
-    ret = net_fill_rstate(&s->pri_rs, buf, size);
+    ret = net_fill_rstate(&s->pri_rs, buf, size, false);
      if (ret == -1) {
          qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
                                   NULL, NULL, true);
@@ -547,7 +547,7 @@ static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
      CompareState *s = COLO_COMPARE(opaque);
      int ret;
 
-    ret = net_fill_rstate(&s->sec_rs, buf, size);
+    ret = net_fill_rstate(&s->sec_rs, buf, size, false);
      if (ret == -1) {
          qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
                                   NULL, NULL, true);
net/filter-mirror.c +1 -1
index a65853c..4649416 100644
@@ -134,7 +134,7 @@ static void redirector_chr_read(void *opaque, const uint8_t *buf, int size)
      MirrorState *s = FILTER_REDIRECTOR(nf);
      int ret;
 
-    ret = net_fill_rstate(&s->rs, buf, size);
+    ret = net_fill_rstate(&s->rs, buf, size, s->vnet_hdr);
 
      if (ret == -1) {
          qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
net/net.c +32 -4
index a00a0c9..a9c97cf 100644
@@ -1618,13 +1618,20 @@ void net_socket_rs_init(SocketReadState *rs,
   * 0: success
   * -1: error occurs
   */
-int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
+int net_fill_rstate(SocketReadState *rs,
+                    const uint8_t *buf,
+                    int size,
+                    bool vnet_hdr)
  {
      unsigned int l;
 
      while (size > 0) {
-        /* reassemble a packet from the network */
-        switch (rs->state) { /* 0 = getting length, 1 = getting data */
+        /* Reassemble a packet from the network.
+         * 0 = getting length.
+         * 1 = getting vnet header length.
+         * 2 = getting data.
+         */
+        switch (rs->state) {
          case 0:
              l = 4 - rs->index;
              if (l > size) {
@@ -1638,10 +1645,31 @@ int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
                  /* got length */
                  rs->packet_len = ntohl(*(uint32_t *)rs->buf);
                  rs->index = 0;
-                rs->state = 1;
+                if (vnet_hdr) {
+                    rs->state = 1;
+                } else {
+                    rs->state = 2;
+                    rs->vnet_hdr_len = 0;
+                }
              }
              break;
          case 1:
+            l = 4 - rs->index;
+            if (l > size) {
+                l = size;
+            }
+            memcpy(rs->buf + rs->index, buf, l);
+            buf += l;
+            size -= l;
+            rs->index += l;
+            if (rs->index == 4) {
+                /* got vnet header length */
+                rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
+                rs->index = 0;
+                rs->state = 2;
+            }
+            break;
+        case 2:
              l = rs->packet_len - rs->index;
              if (l > size) {
                  l = size;
net/socket.c +1 -1
index b8c931e..4e58eff 100644
@@ -182,7 +182,7 @@ static void net_socket_send(void *opaque)
      }
      buf = buf1;
 
-    ret = net_fill_rstate(&s->rs, buf, size);
+    ret = net_fill_rstate(&s->rs, buf, size, false);
 
      if (ret == -1) {
          goto eoc;
author
#22 lore raw

Re: [Qemu-devel] [PATCH V4 05/12] net/net.c: Add vnet_hdr support in SocketReadState

Quoted 6 lines · Jason Wang
Quoted 1 line · Zhang Chen

Address Jason Wang's comments add vnet header length to SocketReadState.

Better to use "Suggested-by" instead of this :).

I got it~

Quoted 5 lines
Quoted 3 lines

We add a flag to dicide whether net_fill_rstate() to read struct {int size; int vnet_hdr_len; const uint8_t buf[];} or not.

Few words to explain the format is better than e.g a struct.

OK, will fix it in next version.

Quoted 28 lines
Quoted 28 lines
---
  include/net/net.h   |  9 +++++++--
  net/colo-compare.c  |  4 ++--
  net/filter-mirror.c |  2 +-
  net/net.c           | 36 ++++++++++++++++++++++++++++++++----
  net/socket.c        |  2 +-
  5 files changed, 43 insertions(+), 10 deletions(-)
include/net/net.h +1 -1
index 70edfc0..0763636 100644
@@ -113,14 +113,19 @@ typedef struct NICState {
  } NICState;
    struct SocketReadState {
-    int state; /* 0 = getting length, 1 = getting data */
+    /* 0 = getting length, 1 = getting vnet header length, 2 = 

getting data */

include/net/net.h +2 -0
+    int state;
      uint32_t index;
      uint32_t packet_len;
+    uint32_t vnet_hdr_len;
      uint8_t buf[NET_BUFSIZE];
      SocketReadStateFinalize *finalize;
  };
Quoted 8 lines

include/net/net.h typedef struct NICState { · line 128

Quoted 6 lines

-int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int
size);

include/net/net.h +4 -0
+int net_fill_rstate(SocketReadState *rs,
+                    const uint8_t *buf,
+                    int size,
+                    bool vnet_hdr);

Why not just move vnet_hdr to SocketReadState?

Good idea.

Thanks
Zhang Chen

Quoted 117 lines

Thanks

Quoted 103 lines
  char *qemu_mac_strdup_printf(const uint8_t *macaddr);
  NetClientState *qemu_find_netdev(const char *id);
  int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
net/colo-compare.c +2 -0
index 4ab80b1..332f57e 100644
@@ -530,7 +530,7 @@ static void compare_pri_chr_in(void *opaque, 
 const uint8_t *buf, int size)
      CompareState *s = COLO_COMPARE(opaque);
      int ret;
  -    ret = net_fill_rstate(&s->pri_rs, buf, size);
+    ret = net_fill_rstate(&s->pri_rs, buf, size, false);
      if (ret == -1) {
          qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
                                   NULL, NULL, true);
@@ -547,7 +547,7 @@ static void compare_sec_chr_in(void *opaque, 
 const uint8_t *buf, int size)
      CompareState *s = COLO_COMPARE(opaque);
      int ret;
  -    ret = net_fill_rstate(&s->sec_rs, buf, size);
+    ret = net_fill_rstate(&s->sec_rs, buf, size, false);
      if (ret == -1) {
          qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
                                   NULL, NULL, true);
net/filter-mirror.c +1 -0
index a65853c..4649416 100644
@@ -134,7 +134,7 @@ static void redirector_chr_read(void *opaque, 
 const uint8_t *buf, int size)
      MirrorState *s = FILTER_REDIRECTOR(nf);
      int ret;
  -    ret = net_fill_rstate(&s->rs, buf, size);
+    ret = net_fill_rstate(&s->rs, buf, size, s->vnet_hdr);
        if (ret == -1) {
          qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
net/net.c +32 -4
index a00a0c9..a9c97cf 100644
@@ -1618,13 +1618,20 @@ void net_socket_rs_init(SocketReadState *rs,
   * 0: success
   * -1: error occurs
   */
-int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
+int net_fill_rstate(SocketReadState *rs,
+                    const uint8_t *buf,
+                    int size,
+                    bool vnet_hdr)
  {
      unsigned int l;
        while (size > 0) {
-        /* reassemble a packet from the network */
-        switch (rs->state) { /* 0 = getting length, 1 = getting data */
+        /* Reassemble a packet from the network.
+         * 0 = getting length.
+         * 1 = getting vnet header length.
+         * 2 = getting data.
+         */
+        switch (rs->state) {
          case 0:
              l = 4 - rs->index;
              if (l > size) {
@@ -1638,10 +1645,31 @@ int net_fill_rstate(SocketReadState *rs, 
 const uint8_t *buf, int size)
                  /* got length */
                  rs->packet_len = ntohl(*(uint32_t *)rs->buf);
                  rs->index = 0;
-                rs->state = 1;
+                if (vnet_hdr) {
+                    rs->state = 1;
+                } else {
+                    rs->state = 2;
+                    rs->vnet_hdr_len = 0;
+                }
              }
              break;
          case 1:
+            l = 4 - rs->index;
+            if (l > size) {
+                l = size;
+            }
+            memcpy(rs->buf + rs->index, buf, l);
+            buf += l;
+            size -= l;
+            rs->index += l;
+            if (rs->index == 4) {
+                /* got vnet header length */
+                rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
+                rs->index = 0;
+                rs->state = 2;
+            }
+            break;
+        case 2:
              l = rs->packet_len - rs->index;
              if (l > size) {
                  l = size;
net/socket.c +0 -0
index b8c931e..4e58eff 100644

net/socket.c static void net_socket_send(void *opaque) · line 187

Quoted 6 lines
net/socket.c +1 -0
@@ -182,7 +182,7 @@ static void net_socket_send(void *opaque)
      }
      buf = buf1;
  -    ret = net_fill_rstate(&s->rs, buf, size);
+    ret = net_fill_rstate(&s->rs, buf, size, false);
        if (ret == -1) {
          goto eoc;

.

Signature · 2 lines
Thanks
Zhang Chen
author
#23 lore raw

PATCH v4 6/12 net/colo-compare.c: Add new option to enable vnet support for colo-compare

In reply to Zhang Chen

We add the vnet_hdr option for colo-compare, default is disable.
If you use virtio-net-pci net driver, please enable it.
You can use it for example:
-object colo-compare,id=comp0,primary_in=compare0-0,secondary_in=compare1,outdev=compare_out0,vnet_hdr=on

---
 net/colo-compare.c | 34 +++++++++++++++++++++++++++++++++-
 qemu-options.hx    |  3 ++-
 2 files changed, 35 insertions(+), 2 deletions(-)
net/colo-compare.c +33 -1
index 332f57e..99a6912 100644
@@ -73,6 +73,7 @@ typedef struct CompareState {
     CharBackend chr_out;
     SocketReadState pri_rs;
     SocketReadState sec_rs;
+    bool vnet_hdr;
 
     /* connection list: the connections belonged to this NIC could be found
      * in this list.
@@ -642,6 +643,28 @@ static void compare_set_outdev(Object *obj, const char *value, Error **errp)
     s->outdev = g_strdup(value);
 }
 
+static char *compare_get_vnet_hdr(Object *obj, Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
+static void compare_set_vnet_hdr(Object *obj,
+                                 const char *value,
+                                 Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for colo-compare vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
 static void compare_pri_rs_finalize(SocketReadState *pri_rs)
 {
     CompareState *s = container_of(pri_rs, CompareState, pri_rs);
@@ -667,7 +690,6 @@ static void compare_sec_rs_finalize(SocketReadState *sec_rs)
     }
 }
 
-
 /*
  * Return 0 is success.
  * Return 1 is failed.
@@ -775,6 +797,8 @@ static void colo_compare_class_init(ObjectClass *oc, void *data)
 
 static void colo_compare_init(Object *obj)
 {
+    CompareState *s = COLO_COMPARE(obj);
+
     object_property_add_str(obj, "primary_in",
                             compare_get_pri_indev, compare_set_pri_indev,
                             NULL);
@@ -784,6 +808,14 @@ static void colo_compare_init(Object *obj)
     object_property_add_str(obj, "outdev",
                             compare_get_outdev, compare_set_outdev,
                             NULL);
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", compare_get_vnet_hdr,
+                            compare_set_vnet_hdr, NULL);
 }
 
 static void colo_compare_finalize(Object *obj)
qemu-options.hx +2 -1
index 0f81c22..115b83f 100644
@@ -4061,12 +4061,13 @@ The file format is libpcap, so it can be analyzed with tools such as tcpdump
 or Wireshark.
 
 @item -object colo-compare,id=@var{id},primary_in=@var{chardevid},secondary_in=@var{chardevid},
-outdev=@var{chardevid}
+outdev=@var{chardevid},vnet_hdr=@var{on|off}
 
 Colo-compare gets packet from primary_in@var{chardevid} and secondary_in@var{chardevid}, than compare primary packet with
 secondary packet. If the packets are same, we will output primary
 packet to outdev@var{chardevid}, else we will notify colo-frame
 do checkpoint and send primary packet to outdev@var{chardevid}.
+if vnet_hdr = on, colo compare will send/recv packet with vnet_hdr_len.
 
 we must use it with the help of filter-mirror and filter-redirector.
 
Signature · 1 line
2.7.4
#24 lore raw

Re: [Qemu-devel] [PATCH V4 06/12] net/colo-compare.c: Add new option to enable vnet support for colo-compare

Quoted 53 lines · Zhang Chen

We add the vnet_hdr option for colo-compare, default is disable.
If you use virtio-net-pci net driver, please enable it.
You can use it for example:
-object colo-compare,id=comp0,primary_in=compare0-0,secondary_in=compare1,outdev=compare_out0,vnet_hdr=on

---
  net/colo-compare.c | 34 +++++++++++++++++++++++++++++++++-
  qemu-options.hx    |  3 ++-
  2 files changed, 35 insertions(+), 2 deletions(-)
net/colo-compare.c +23 -0
index 332f57e..99a6912 100644
@@ -73,6 +73,7 @@ typedef struct CompareState {
      CharBackend chr_out;
      SocketReadState pri_rs;
      SocketReadState sec_rs;
+    bool vnet_hdr;
 
      /* connection list: the connections belonged to this NIC could be found
       * in this list.
@@ -642,6 +643,28 @@ static void compare_set_outdev(Object *obj, const char *value, Error **errp)
      s->outdev = g_strdup(value);
  }
 
+static char *compare_get_vnet_hdr(Object *obj, Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
+static void compare_set_vnet_hdr(Object *obj,
+                                 const char *value,
+                                 Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for colo-compare vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
  static void compare_pri_rs_finalize(SocketReadState *pri_rs)
  {
      CompareState *s = container_of(pri_rs, CompareState, pri_rs);

net/colo-compare.c static void compare_sec_rs_finalize(SocketReadState *sec_rs) · line 670

Quoted 4 lines
net/colo-compare.c +0 -1
@@ -667,7 +690,6 @@ static void compare_sec_rs_finalize(SocketReadState *sec_rs)
      }
  }
 
-

Unnecessary whitespace change.

Quoted 36 lines
net/colo-compare.c +10 -0
  /*
   * Return 0 is success.
   * Return 1 is failed.
@@ -775,6 +797,8 @@ static void colo_compare_class_init(ObjectClass *oc, void *data)
 
  static void colo_compare_init(Object *obj)
  {
+    CompareState *s = COLO_COMPARE(obj);
+
      object_property_add_str(obj, "primary_in",
                              compare_get_pri_indev, compare_set_pri_indev,
                              NULL);
@@ -784,6 +808,14 @@ static void colo_compare_init(Object *obj)
      object_property_add_str(obj, "outdev",
                              compare_get_outdev, compare_set_outdev,
                              NULL);
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", compare_get_vnet_hdr,
+                            compare_set_vnet_hdr, NULL);
  }
 
  static void colo_compare_finalize(Object *obj)
qemu-options.hx +0 -1
index 0f81c22..115b83f 100644
@@ -4061,12 +4061,13 @@ The file format is libpcap, so it can be analyzed with tools such as tcpdump
  or Wireshark.
 
  @item -object colo-compare,id=@var{id},primary_in=@var{chardevid},secondary_in=@var{chardevid},
-outdev=@var{chardevid}

qemu-options.hx The file format is libpcap, so it can be analyzed with tools such as tcpdump · line 4073

Quoted 10 lines
qemu-options.hx +2 -0
+outdev=@var{chardevid},vnet_hdr=@var{on|off}
 
  Colo-compare gets packet from primary_in@var{chardevid} and secondary_in@var{chardevid}, than compare primary packet with
  secondary packet. If the packets are same, we will output primary
  packet to outdev@var{chardevid}, else we will notify colo-frame
  do checkpoint and send primary packet to outdev@var{chardevid}.
+if vnet_hdr = on, colo compare will send/recv packet with vnet_hdr_len.
 
  we must use it with the help of filter-mirror and filter-redirector.
 

Please squash this into its function implementation.

Thanks

author
#25 lore raw

Re: [Qemu-devel] [PATCH V4 06/12] net/colo-compare.c: Add new option to enable vnet support for colo-compare

Quoted 55 lines · Jason Wang
Quoted 52 lines · Zhang Chen

We add the vnet_hdr option for colo-compare, default is disable.
If you use virtio-net-pci net driver, please enable it.
You can use it for example:
-object colo-compare,id=comp0,primary_in=compare0-0,secondary_in=compare1,outdev=compare_out0,vnet_hdr=on

---
  net/colo-compare.c | 34 +++++++++++++++++++++++++++++++++-
  qemu-options.hx    |  3 ++-
  2 files changed, 35 insertions(+), 2 deletions(-)
net/colo-compare.c +1 -0
index 332f57e..99a6912 100644
@@ -73,6 +73,7 @@ typedef struct CompareState {
      CharBackend chr_out;
      SocketReadState pri_rs;
      SocketReadState sec_rs;
+    bool vnet_hdr;
        /* connection list: the connections belonged to this NIC 

could be found
* in this list.

net/colo-compare.c +21 -0
@@ -642,6 +643,28 @@ static void compare_set_outdev(Object *obj, 
 const char *value, Error **errp)
      s->outdev = g_strdup(value);
  }
  +static char *compare_get_vnet_hdr(Object *obj, Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
+static void compare_set_vnet_hdr(Object *obj,
+                                 const char *value,
+                                 Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for colo-compare vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
  static void compare_pri_rs_finalize(SocketReadState *pri_rs)
  {
Quoted 8 lines

net/colo-compare.c static void · line 693

Quoted 6 lines
net/colo-compare.c +0 -0
      CompareState *s = container_of(pri_rs, CompareState, pri_rs);
@@ -667,7 +690,6 @@ static void 
 compare_sec_rs_finalize(SocketReadState *sec_rs)
      }
  }
  -

Unnecessary whitespace change.

I will remove it in next version.

Quoted 40 lines
Quoted 40 lines
net/colo-compare.c +2 -0
  /*
   * Return 0 is success.
   * Return 1 is failed.
@@ -775,6 +797,8 @@ static void colo_compare_class_init(ObjectClass 
 *oc, void *data)
    static void colo_compare_init(Object *obj)
  {
+    CompareState *s = COLO_COMPARE(obj);
+
      object_property_add_str(obj, "primary_in",
                              compare_get_pri_indev, 
 compare_set_pri_indev,
                              NULL);
net/colo-compare.c +8 -0
@@ -784,6 +808,14 @@ static void colo_compare_init(Object *obj)
      object_property_add_str(obj, "outdev",
                              compare_get_outdev, compare_set_outdev,
                              NULL);
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", compare_get_vnet_hdr,
+                            compare_set_vnet_hdr, NULL);
  }
    static void colo_compare_finalize(Object *obj)
qemu-options.hx +0 -0
index 0f81c22..115b83f 100644
@@ -4061,12 +4061,13 @@ The file format is libpcap, so it can be 

analyzed with tools such as tcpdump
or Wireshark.
@item -object
colo-compare,id=@var{id},primary_in=@var{chardevid},secondary_in=@var{chardevid},

qemu-options.hx +1 -1
-outdev=@var{chardevid}
+outdev=@var{chardevid},vnet_hdr=@var{on|off}
    Colo-compare gets packet from primary_in@var{chardevid} and 
Quoted 8 lines
Quoted 6 lines

secondary_in@var{chardevid}, than compare primary packet with
secondary packet. If the packets are same, we will output primary packet to outdev@var{chardevid}, else we will notify colo-frame do checkpoint and send primary packet to outdev@var{chardevid}.
+if vnet_hdr = on, colo compare will send/recv packet with vnet_hdr_len.
we must use it with the help of filter-mirror and filter-redirector.

Please squash this into its function implementation.

OK.
Thanks
Zhang Chen

Quoted 6 lines

Thanks

.

Signature · 2 lines
Thanks
Zhang Chen
author
#26 lore raw

PATCH v4 7/12 net/colo.c: Make vnet_hdr_len as packet property

In reply to Zhang Chen

We can use this property flush and send packet with vnet_hdr_len.

---
 net/colo-compare.c    | 8 ++++++--
 net/colo.c            | 3 ++-
 net/colo.h            | 4 +++-
 net/filter-rewriter.c | 2 +-
 4 files changed, 12 insertions(+), 5 deletions(-)
net/colo-compare.c +6 -2
index 99a6912..87a9529 100644
@@ -122,9 +122,13 @@ static int packet_enqueue(CompareState *s, int mode)
     Connection *conn;
 
     if (mode == PRIMARY_IN) {
-        pkt = packet_new(s->pri_rs.buf, s->pri_rs.packet_len);
+        pkt = packet_new(s->pri_rs.buf,
+                         s->pri_rs.packet_len,
+                         s->pri_rs.vnet_hdr_len);
     } else {
-        pkt = packet_new(s->sec_rs.buf, s->sec_rs.packet_len);
+        pkt = packet_new(s->sec_rs.buf,
+                         s->sec_rs.packet_len,
+                         s->sec_rs.vnet_hdr_len);
     }
 
     if (parse_packet_early(pkt)) {
net/colo.c +2 -1
index 8cc166b..180eaed 100644
@@ -153,13 +153,14 @@ void connection_destroy(void *opaque)
     g_slice_free(Connection, conn);
 }
 
-Packet *packet_new(const void *data, int size)
+Packet *packet_new(const void *data, int size, int vnet_hdr_len)
 {
     Packet *pkt = g_slice_new(Packet);
 
     pkt->data = g_memdup(data, size);
     pkt->size = size;
     pkt->creation_ms = qemu_clock_get_ms(QEMU_CLOCK_HOST);
+    pkt->vnet_hdr_len = vnet_hdr_len;
 
     return pkt;
 }
net/colo.h +3 -1
index 7c524f3..caedb0d 100644
@@ -43,6 +43,8 @@ typedef struct Packet {
     int size;
     /* Time of packet creation, in wall clock ms */
     int64_t creation_ms;
+    /* Get vnet_hdr_len from filter */
+    uint32_t vnet_hdr_len;
 } Packet;
 
 typedef struct ConnectionKey {
@@ -82,7 +84,7 @@ Connection *connection_get(GHashTable *connection_track_table,
                            ConnectionKey *key,
                            GQueue *conn_list);
 void connection_hashtable_reset(GHashTable *connection_track_table);
-Packet *packet_new(const void *data, int size);
+Packet *packet_new(const void *data, int size, int vnet_hdr_len);
 void packet_destroy(void *opaque, void *user_data);
 
 #endif /* QEMU_COLO_PROXY_H */
net/filter-rewriter.c +1 -1
index afa06e8..63256c7 100644
@@ -158,7 +158,7 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
     char *buf = g_malloc0(size);
 
     iov_to_buf(iov, iovcnt, 0, buf, size);
-    pkt = packet_new(buf, size);
+    pkt = packet_new(buf, size, 0);
     g_free(buf);
 
     /*
Signature · 1 line
2.7.4
#27 lore raw

Re: [Qemu-devel] [PATCH V4 07/12] net/colo.c: Make vnet_hdr_len as packet property

Quoted 3 lines · Zhang Chen

We can use this property flush and send packet with vnet_hdr_len.

Then I think it's not necessary to store vnet_hdr_len in SocketReadState?

Thanks

Quoted 82 lines
---
  net/colo-compare.c    | 8 ++++++--
  net/colo.c            | 3 ++-
  net/colo.h            | 4 +++-
  net/filter-rewriter.c | 2 +-
  4 files changed, 12 insertions(+), 5 deletions(-)
net/colo-compare.c +6 -2
index 99a6912..87a9529 100644
@@ -122,9 +122,13 @@ static int packet_enqueue(CompareState *s, int mode)
      Connection *conn;
 
      if (mode == PRIMARY_IN) {
-        pkt = packet_new(s->pri_rs.buf, s->pri_rs.packet_len);
+        pkt = packet_new(s->pri_rs.buf,
+                         s->pri_rs.packet_len,
+                         s->pri_rs.vnet_hdr_len);
      } else {
-        pkt = packet_new(s->sec_rs.buf, s->sec_rs.packet_len);
+        pkt = packet_new(s->sec_rs.buf,
+                         s->sec_rs.packet_len,
+                         s->sec_rs.vnet_hdr_len);
      }
 
      if (parse_packet_early(pkt)) {
net/colo.c +2 -1
index 8cc166b..180eaed 100644
@@ -153,13 +153,14 @@ void connection_destroy(void *opaque)
      g_slice_free(Connection, conn);
  }
 
-Packet *packet_new(const void *data, int size)
+Packet *packet_new(const void *data, int size, int vnet_hdr_len)
  {
      Packet *pkt = g_slice_new(Packet);
 
      pkt->data = g_memdup(data, size);
      pkt->size = size;
      pkt->creation_ms = qemu_clock_get_ms(QEMU_CLOCK_HOST);
+    pkt->vnet_hdr_len = vnet_hdr_len;
 
      return pkt;
  }
net/colo.h +3 -1
index 7c524f3..caedb0d 100644
@@ -43,6 +43,8 @@ typedef struct Packet {
      int size;
      /* Time of packet creation, in wall clock ms */
      int64_t creation_ms;
+    /* Get vnet_hdr_len from filter */
+    uint32_t vnet_hdr_len;
  } Packet;
 
  typedef struct ConnectionKey {
@@ -82,7 +84,7 @@ Connection *connection_get(GHashTable *connection_track_table,
                             ConnectionKey *key,
                             GQueue *conn_list);
  void connection_hashtable_reset(GHashTable *connection_track_table);
-Packet *packet_new(const void *data, int size);
+Packet *packet_new(const void *data, int size, int vnet_hdr_len);
  void packet_destroy(void *opaque, void *user_data);
 
  #endif /* QEMU_COLO_PROXY_H */
net/filter-rewriter.c +1 -1
index afa06e8..63256c7 100644
@@ -158,7 +158,7 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
      char *buf = g_malloc0(size);
 
      iov_to_buf(iov, iovcnt, 0, buf, size);
-    pkt = packet_new(buf, size);
+    pkt = packet_new(buf, size, 0);
      g_free(buf);
 
      /*
author
#28 lore raw

Re: [Qemu-devel] [PATCH V4 07/12] net/colo.c: Make vnet_hdr_len as packet property

Quoted 8 lines · Jason Wang
Quoted 3 lines · Zhang Chen

We can use this property flush and send packet with vnet_hdr_len.

Then I think it's not necessary to store vnet_hdr_len in SocketReadState?

Do you means we keep the patch 05/12 in original?

Thanks
Zhang Chen

Quoted 84 lines

Thanks

Quoted 69 lines
---
  net/colo-compare.c    | 8 ++++++--
  net/colo.c            | 3 ++-
  net/colo.h            | 4 +++-
  net/filter-rewriter.c | 2 +-
  4 files changed, 12 insertions(+), 5 deletions(-)
net/colo-compare.c +6 -2
index 99a6912..87a9529 100644
@@ -122,9 +122,13 @@ static int packet_enqueue(CompareState *s, int 
 mode)
      Connection *conn;
        if (mode == PRIMARY_IN) {
-        pkt = packet_new(s->pri_rs.buf, s->pri_rs.packet_len);
+        pkt = packet_new(s->pri_rs.buf,
+                         s->pri_rs.packet_len,
+                         s->pri_rs.vnet_hdr_len);
      } else {
-        pkt = packet_new(s->sec_rs.buf, s->sec_rs.packet_len);
+        pkt = packet_new(s->sec_rs.buf,
+                         s->sec_rs.packet_len,
+                         s->sec_rs.vnet_hdr_len);
      }
        if (parse_packet_early(pkt)) {
net/colo.c +2 -0
index 8cc166b..180eaed 100644
@@ -153,13 +153,14 @@ void connection_destroy(void *opaque)
      g_slice_free(Connection, conn);
  }
  -Packet *packet_new(const void *data, int size)
+Packet *packet_new(const void *data, int size, int vnet_hdr_len)
  {
      Packet *pkt = g_slice_new(Packet);
        pkt->data = g_memdup(data, size);
      pkt->size = size;
      pkt->creation_ms = qemu_clock_get_ms(QEMU_CLOCK_HOST);
+    pkt->vnet_hdr_len = vnet_hdr_len;
        return pkt;
  }
net/colo.h +3 -1
index 7c524f3..caedb0d 100644
@@ -43,6 +43,8 @@ typedef struct Packet {
      int size;
      /* Time of packet creation, in wall clock ms */
      int64_t creation_ms;
+    /* Get vnet_hdr_len from filter */
+    uint32_t vnet_hdr_len;
  } Packet;
    typedef struct ConnectionKey {
@@ -82,7 +84,7 @@ Connection *connection_get(GHashTable 
 *connection_track_table,
                             ConnectionKey *key,
                             GQueue *conn_list);
  void connection_hashtable_reset(GHashTable *connection_track_table);
-Packet *packet_new(const void *data, int size);
+Packet *packet_new(const void *data, int size, int vnet_hdr_len);
  void packet_destroy(void *opaque, void *user_data);
    #endif /* QEMU_COLO_PROXY_H */
net/filter-rewriter.c +0 -0
index afa06e8..63256c7 100644

net/filter-rewriter.c static ssize_t · line 163

Quoted 7 lines
net/filter-rewriter.c +1 -1
@@ -158,7 +158,7 @@ static ssize_t 
 colo_rewriter_receive_iov(NetFilterState *nf,
      char *buf = g_malloc0(size);
        iov_to_buf(iov, iovcnt, 0, buf, size);
-    pkt = packet_new(buf, size);
+    pkt = packet_new(buf, size, 0);
      g_free(buf);
        /*

.

Signature · 2 lines
Thanks
Zhang Chen
#29 lore raw

Re: [Qemu-devel] [PATCH V4 07/12] net/colo.c: Make vnet_hdr_len as packet property

Quoted 8 lines · Zhang Chen
Quoted 5 lines · Jason Wang
Quoted 2 lines · Zhang Chen

We can use this property flush and send packet with vnet_hdr_len.

Quoted 9 lines
Quoted 3 lines
Quoted 1 line

Then I think it's not necessary to store vnet_hdr_len in SocketReadState?

Do you means we keep the patch 05/12 in original?

Thanks
Zhang Chen

I mean we could fetch vnet_hdr_len from the buf directly. Or is there any advantage to store it in SocketReadState?

Thanks

author
#30 lore raw

Re: [Qemu-devel] [PATCH V4 07/12] net/colo.c: Make vnet_hdr_len as packet property

Quoted 15 lines · Jason Wang
Quoted 12 lines · Zhang Chen
Quoted 8 lines · Jason Wang
Quoted 3 lines · Zhang Chen

We can use this property flush and send packet with vnet_hdr_len.

Then I think it's not necessary to store vnet_hdr_len in SocketReadState?

Quoted 7 lines
Quoted 5 lines

Do you means we keep the patch 05/12 in original?

Thanks
Zhang Chen

I mean we could fetch vnet_hdr_len from the buf directly. Or is there any advantage to store it in SocketReadState?

No, The rs->buf did't have the vnet_hdr_len field.
In the net_fill_rstate(), when case = 2, we override the rs->buf by real net packet data, So, the vnet_hdr_len field and packet_len field didn't be include in the last rs->buf.

Thanks
Zhang Chen

Quoted 6 lines

Thanks

Signature · 2 lines
Thanks
Zhang Chen
author
#31 lore raw

PATCH v4 8/12 net/colo-compare.c: Make colo-compare support vnet_hdr_len

In reply to Zhang Chen

COLO-compare can get vnet header length from filter, Add vnet_hdr_len to struct packet and output packet with the vnet_hdr_len.

---
 net/colo-compare.c | 45 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 11 deletions(-)
net/colo-compare.c +34 -11
index 87a9529..cb0b04e 100644
@@ -98,9 +98,10 @@ enum {
     SECONDARY_IN,
 };
 
-static int compare_chr_send(CharBackend *out,
+static int compare_chr_send(CompareState *s,
                             const uint8_t *buf,
-                            uint32_t size);
+                            uint32_t size,
+                            uint32_t vnet_hdr_len);
 
 static gint seq_sorter(Packet *a, Packet *b, gpointer data)
 {
@@ -473,7 +474,10 @@ static void colo_compare_connection(void *opaque, void *user_data)
         }
 
         if (result) {
-            ret = compare_chr_send(&s->chr_out, pkt->data, pkt->size);
+            ret = compare_chr_send(s,
+                                   pkt->data,
+                                   pkt->size,
+                                   pkt->vnet_hdr_len);
             if (ret < 0) {
                 error_report("colo_send_primary_packet failed");
             }
@@ -494,9 +498,10 @@ static void colo_compare_connection(void *opaque, void *user_data)
     }
 }
 
-static int compare_chr_send(CharBackend *out,
+static int compare_chr_send(CompareState *s,
                             const uint8_t *buf,
-                            uint32_t size)
+                            uint32_t size,
+                            uint32_t vnet_hdr_len)
 {
     int ret = 0;
     uint32_t len = htonl(size);
@@ -505,12 +510,24 @@ static int compare_chr_send(CharBackend *out,
         return 0;
     }
 
-    ret = qemu_chr_fe_write_all(out, (uint8_t *)&len, sizeof(len));
+    ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len));
     if (ret != sizeof(len)) {
         goto err;
     }
 
-    ret = qemu_chr_fe_write_all(out, (uint8_t *)buf, size);
+    if (s->vnet_hdr) {
+        /*
+         * We send vnet header len make other module(like filter-redirector)
+         * know how to parse net packet correctly.
+         */
+        len = htonl(vnet_hdr_len);
+        ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len));
+        if (ret != sizeof(len)) {
+            goto err;
+        }
+    }
+
+    ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size);
     if (ret != size) {
         goto err;
     }
@@ -535,7 +552,7 @@ static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
     CompareState *s = COLO_COMPARE(opaque);
     int ret;
 
-    ret = net_fill_rstate(&s->pri_rs, buf, size, false);
+    ret = net_fill_rstate(&s->pri_rs, buf, size, s->vnet_hdr);
     if (ret == -1) {
         qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
                                  NULL, NULL, true);
@@ -552,7 +569,7 @@ static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
     CompareState *s = COLO_COMPARE(opaque);
     int ret;
 
-    ret = net_fill_rstate(&s->sec_rs, buf, size, false);
+    ret = net_fill_rstate(&s->sec_rs, buf, size, s->vnet_hdr);
     if (ret == -1) {
         qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
                                  NULL, NULL, true);
@@ -675,7 +692,10 @@ static void compare_pri_rs_finalize(SocketReadState *pri_rs)
 
     if (packet_enqueue(s, PRIMARY_IN)) {
         trace_colo_compare_main("primary: unsupported packet in");
-        compare_chr_send(&s->chr_out, pri_rs->buf, pri_rs->packet_len);
+        compare_chr_send(s,
+                         pri_rs->buf,
+                         pri_rs->packet_len,
+                         pri_rs->vnet_hdr_len);
     } else {
         /* compare connection */
         g_queue_foreach(&s->conn_list, colo_compare_connection, s);
@@ -783,7 +803,10 @@ static void colo_flush_packets(void *opaque, void *user_data)
 
     while (!g_queue_is_empty(&conn->primary_list)) {
         pkt = g_queue_pop_head(&conn->primary_list);
-        compare_chr_send(&s->chr_out, pkt->data, pkt->size);
+        compare_chr_send(s,
+                         pkt->data,
+                         pkt->size,
+                         pkt->vnet_hdr_len);
         packet_destroy(pkt, NULL);
     }
     while (!g_queue_is_empty(&conn->secondary_list)) {
Signature · 1 line
2.7.4
author
#32 lore raw

PATCH v4 9/12 net/colo.c: Add vnet packet parse feature in colo-proxy

In reply to Zhang Chen

Make colo-compare and filter-rewriter can parse vnet packet.

---
 net/colo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/colo.c +3 -3
index 180eaed..28ce7c8 100644
@@ -43,11 +43,11 @@ int parse_packet_early(Packet *pkt)
 {
     int network_length;
     static const uint8_t vlan[] = {0x81, 0x00};
-    uint8_t *data = pkt->data;
+    uint8_t *data = pkt->data + pkt->vnet_hdr_len;
     uint16_t l3_proto;
     ssize_t l2hdr_len = eth_get_l2_hdr_length(data);
 
-    if (pkt->size < ETH_HLEN) {
+    if (pkt->size < ETH_HLEN + pkt->vnet_hdr_len) {
         trace_colo_proxy_main("pkt->size < ETH_HLEN");
         return 1;
     }
@@ -73,7 +73,7 @@ int parse_packet_early(Packet *pkt)
     }
 
     network_length = pkt->ip->ip_hl * 4;
-    if (pkt->size < l2hdr_len + network_length) {
+    if (pkt->size < l2hdr_len + network_length + pkt->vnet_hdr_len) {
         trace_colo_proxy_main("pkt->size < network_header + network_length");
         return 1;
     }
Signature · 1 line
2.7.4
author
#33 lore raw

PATCH v4 10/12 net/colo-compare.c: Add vnet packet's tcp/udp/icmp compare

In reply to Zhang Chen

COLO-Proxy just focus on packet payload, So we skip vnet header.

---
 net/colo-compare.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/colo-compare.c +9 -3
index cb0b04e..bf565f3 100644
@@ -188,6 +188,8 @@ static int packet_enqueue(CompareState *s, int mode)
  */
 static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset)
 {
+    int offset_all;
+
     if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
         char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
 
@@ -201,9 +203,12 @@ static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset)
                                    sec_ip_src, sec_ip_dst);
     }
 
+    offset_all = ppkt->vnet_hdr_len + offset;
+
     if (ppkt->size == spkt->size) {
-        return memcmp(ppkt->data + offset, spkt->data + offset,
-                      spkt->size - offset);
+        return memcmp(ppkt->data + offset_all,
+                      spkt->data + offset_all,
+                      spkt->size - offset_all);
     } else {
         trace_colo_compare_main("Net packet size are not the same");
         return -1;
@@ -261,8 +266,9 @@ static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
      */
     if (ptcp->th_off > 5) {
         ptrdiff_t tcp_offset;
+
         tcp_offset = ppkt->transport_header - (uint8_t *)ppkt->data
-                     + (ptcp->th_off * 4);
+                     + (ptcp->th_off * 4) - ppkt->vnet_hdr_len;
         res = colo_packet_compare_common(ppkt, spkt, tcp_offset);
     } else if (ptcp->th_sum == stcp->th_sum) {
         res = colo_packet_compare_common(ppkt, spkt, ETH_HLEN);
Signature · 1 line
2.7.4
#34 lore raw

Re: [Qemu-devel] [PATCH V4 10/12] net/colo-compare.c: Add vnet packet's tcp/udp/icmp compare

Quoted 21 lines · Zhang Chen

COLO-Proxy just focus on packet payload, So we skip vnet header.

---
  net/colo-compare.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
net/colo-compare.c +2 -0
index cb0b04e..bf565f3 100644
@@ -188,6 +188,8 @@ static int packet_enqueue(CompareState *s, int mode)
   */
  static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset)
  {
+    int offset_all;
+
      if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
          char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
 

net/colo-compare.c static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset) · line 206

Quoted 4 lines
net/colo-compare.c +1 -0
@@ -201,9 +203,12 @@ static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset)
                                     sec_ip_src, sec_ip_dst);
      }
 
+    offset_all = ppkt->vnet_hdr_len + offset;

How about just keep using offset by increasing it with vnet_hdr_len?

Thanks

Quoted 21 lines
net/colo-compare.c +6 -3
+
      if (ppkt->size == spkt->size) {
-        return memcmp(ppkt->data + offset, spkt->data + offset,
-                      spkt->size - offset);
+        return memcmp(ppkt->data + offset_all,
+                      spkt->data + offset_all,
+                      spkt->size - offset_all);
      } else {
          trace_colo_compare_main("Net packet size are not the same");
          return -1;
@@ -261,8 +266,9 @@ static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
       */
      if (ptcp->th_off > 5) {
          ptrdiff_t tcp_offset;
+
          tcp_offset = ppkt->transport_header - (uint8_t *)ppkt->data
-                     + (ptcp->th_off * 4);
+                     + (ptcp->th_off * 4) - ppkt->vnet_hdr_len;
          res = colo_packet_compare_common(ppkt, spkt, tcp_offset);
      } else if (ptcp->th_sum == stcp->th_sum) {
          res = colo_packet_compare_common(ppkt, spkt, ETH_HLEN);
author
#35 lore raw

Re: [Qemu-devel] [PATCH V4 10/12] net/colo-compare.c: Add vnet packet's tcp/udp/icmp compare

Quoted 23 lines · Jason Wang
Quoted 20 lines · Zhang Chen

COLO-Proxy just focus on packet payload, So we skip vnet header.

---
  net/colo-compare.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
net/colo-compare.c +2 -0
index cb0b04e..bf565f3 100644
@@ -188,6 +188,8 @@ static int packet_enqueue(CompareState *s, int mode)
   */
  static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, 
 int offset)
  {
+    int offset_all;
+
      if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
          char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], 
Quoted 8 lines
Quoted 6 lines

sec_ip_dst[20];
@@ -201,9 +203,12 @@ static int colo_packet_compare_common(Packet
*ppkt, Packet *spkt, int offset)

                                     sec_ip_src, sec_ip_dst);
      }

+ offset_all = ppkt->vnet_hdr_len + offset;

How about just keep using offset by increasing it with vnet_hdr_len?

OK, I will fix it in next version.

Thanks
Zhang Chen

Quoted 30 lines

Thanks

Quoted 21 lines
net/colo-compare.c +6 -3
+
      if (ppkt->size == spkt->size) {
-        return memcmp(ppkt->data + offset, spkt->data + offset,
-                      spkt->size - offset);
+        return memcmp(ppkt->data + offset_all,
+                      spkt->data + offset_all,
+                      spkt->size - offset_all);
      } else {
          trace_colo_compare_main("Net packet size are not the same");
          return -1;
@@ -261,8 +266,9 @@ static int colo_packet_compare_tcp(Packet *spkt, 
 Packet *ppkt)
       */
      if (ptcp->th_off > 5) {
          ptrdiff_t tcp_offset;
+
          tcp_offset = ppkt->transport_header - (uint8_t *)ppkt->data
-                     + (ptcp->th_off * 4);
+                     + (ptcp->th_off * 4) - ppkt->vnet_hdr_len;
          res = colo_packet_compare_common(ppkt, spkt, tcp_offset);
      } else if (ptcp->th_sum == stcp->th_sum) {
Quoted 1 line
          res = colo_packet_compare_common(ppkt, spkt, ETH_HLEN);

.

Signature · 2 lines
Thanks
Zhang Chen
author
#36 lore raw

PATCH v4 11/12 net/filter-rewriter.c: Add new option to enable vnet support for filter-rewriter

In reply to Zhang Chen

We add the vnet_hdr option for filter-rewriter, default is disable. If you use virtio-net-pci net driver, please enable it. You can use it for example:
-object filter-rewriter,id=rew0,netdev=hn0,queue=all,vnet_hdr=on

---
 net/filter-rewriter.c | 38 ++++++++++++++++++++++++++++++++++++++
 qemu-options.hx       |  4 ++--
 2 files changed, 40 insertions(+), 2 deletions(-)
net/filter-rewriter.c +38 -0
index 63256c7..bc6d12a 100644
@@ -33,6 +33,7 @@ typedef struct RewriterState {
     NetQueue *incoming_queue;
     /* hashtable to save connection */
     GHashTable *connection_track_table;
+    bool vnet_hdr;
 } RewriterState;
 
 static void filter_rewriter_flush(NetFilterState *nf)
@@ -237,6 +238,42 @@ static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
     s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
 }
 
+static char *filter_rewriter_get_vnet_hdr(Object *obj, Error **errp)
+{
+    RewriterState *s = FILTER_COLO_REWRITER(obj);
+
+    return s->vnet_hdr ? g_strdup("on") : g_strdup("off");
+}
+
+static void filter_rewriter_set_vnet_hdr(Object *obj,
+                                         const char *value,
+                                         Error **errp)
+{
+    RewriterState *s = FILTER_COLO_REWRITER(obj);
+
+    if (strcmp(value, "on") && strcmp(value, "off")) {
+        error_setg(errp, "Invalid value for filter-rewriter vnet_hdr, "
+                         "should be 'on' or 'off'");
+        return;
+    }
+
+    s->vnet_hdr = !strcmp(value, "on");
+}
+
+static void filter_rewriter_init(Object *obj)
+{
+    RewriterState *s = FILTER_COLO_REWRITER(obj);
+
+    /*
+     * The vnet_hdr is disabled by default, if you want to enable
+     * this option, you must enable all the option on related modules
+     * (like other filter or colo-compare).
+     */
+    s->vnet_hdr = false;
+    object_property_add_str(obj, "vnet_hdr", filter_rewriter_get_vnet_hdr,
+                            filter_rewriter_set_vnet_hdr, NULL);
+}
+
 static void colo_rewriter_class_init(ObjectClass *oc, void *data)
 {
     NetFilterClass *nfc = NETFILTER_CLASS(oc);
@@ -250,6 +287,7 @@ static const TypeInfo colo_rewriter_info = {
     .name = TYPE_FILTER_REWRITER,
     .parent = TYPE_NETFILTER,
     .class_init = colo_rewriter_class_init,
+    .instance_init = filter_rewriter_init,
     .instance_size = sizeof(RewriterState),
 };
 
qemu-options.hx +2 -2
index 115b83f..d191050 100644
@@ -4040,12 +4040,12 @@ Create a filter-redirector we need to differ outdev id from indev id, id can not
 be the same. we can just use indev or outdev, but at least one of indev or outdev
 need to be specified.
 
-@item -object filter-rewriter,id=@var{id},netdev=@var{netdevid},rewriter-mode=@var{mode}[,queue=@var{all|rx|tx}]
+@item -object filter-rewriter,id=@var{id},netdev=@var{netdevid},rewriter-mode=@var{mode},vnet_hdr=@var{on|off}[,queue=@var{all|rx|tx}]
 
 Filter-rewriter is a part of COLO project.It will rewrite tcp packet to
 secondary from primary to keep secondary tcp connection,and rewrite
 tcp packet to primary from secondary make tcp packet can be handled by
-client.
+client.if vnet_hdr = on, we can parse packet with vnet header.
 
 usage:
 colo secondary:
Signature · 1 line
2.7.4
#37 lore raw

Re: [Qemu-devel] [PATCH V4 11/12] net/filter-rewriter.c: Add new option to enable vnet support for filter-rewriter

Quoted 6 lines · Zhang Chen

We add the vnet_hdr option for filter-rewriter, default is disable. If you use virtio-net-pci net driver, please enable it. You can use it for example:
-object filter-rewriter,id=rew0,netdev=hn0,queue=all,vnet_hdr=on

As has been pointed out, let's squash this into patch 12.

Thanks

author
#38 lore raw

Re: [Qemu-devel] [PATCH V4 11/12] net/filter-rewriter.c: Add new option to enable vnet support for filter-rewriter

Quoted 11 lines · Jason Wang
Quoted 6 lines · Zhang Chen

We add the vnet_hdr option for filter-rewriter, default is disable. If you use virtio-net-pci net driver, please enable it. You can use it for example:
-object filter-rewriter,id=rew0,netdev=hn0,queue=all,vnet_hdr=on

As has been pointed out, let's squash this into patch 12.

OK~

Thanks
Zhang Chen

Quoted 6 lines

Thanks

Signature · 2 lines
Thanks
Zhang Chen
author
#39 lore raw

PATCH v4 12/12 net/filter-rewriter.c: Make filter-rewriter support vnet_hdr_len

In reply to Zhang Chen

We get the vnet_hdr_len from NetClientState that make us parse net packet correctly.

---
 net/filter-rewriter.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
net/filter-rewriter.c +16 -1
index bc6d12a..be129c7 100644
@@ -17,6 +17,7 @@
 #include "qemu-common.h"
 #include "qapi/error.h"
 #include "qapi/qmp/qerror.h"
+#include "qemu/error-report.h"
 #include "qapi-visit.h"
 #include "qom/object.h"
 #include "qemu/main-loop.h"
@@ -156,10 +157,24 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
     ConnectionKey key;
     Packet *pkt;
     ssize_t size = iov_size(iov, iovcnt);
+    ssize_t vnet_hdr_len = 0;
     char *buf = g_malloc0(size);
 
     iov_to_buf(iov, iovcnt, 0, buf, size);
-    pkt = packet_new(buf, size, 0);
+
+    if (s->vnet_hdr) {
+        if (nf->netdev->using_vnet_hdr) {
+            vnet_hdr_len = nf->netdev->vnet_hdr_len;
+        } else if (nf->netdev->peer->using_vnet_hdr) {
+            vnet_hdr_len = nf->netdev->peer->vnet_hdr_len;
+        } else {
+            error_report("filter-rewriter get vnet_hdr_len failed");
+            /* When error occurred we drop the packet  */
+            return 1;
+        }
+    }
+
+    pkt = packet_new(buf, size, vnet_hdr_len);
     g_free(buf);
 
     /*
Signature · 1 line
2.7.4