Skip to contentSkip to conversation

Add a netfilter object and netbuffer filter

PATCH Superseded Yang Hongyang 5 participants 70 messages Sep 16, 2015 – Sep 28, 2015 Networking

v11 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v11 cover Add a netfilter object and netbuffer filter

This patch add an netfilter abstract object, captures all network packets on associated netdev. Also implement a concrete filter buffer based on this abstract object. the "buffer" netfilter could be used by VM FT solutions like MicroCheckpointing, to buffer/release packets. Or to simulate packet delay.

You can also get the series from:
https://github.com/macrosheep/qemu/tree/netfilter-v11

Usage:
-netdev tap,id=bn0
-device e1000,netdev=bn0
-object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

dynamically add/remove netfilters:
object_add filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000 object_del f0

NOTE:
interval's scale is microsecond.
chain is optional, and is one of in|out|all, default is "all".
"in" means this filter will receive packets sent to the @netdev "out" means this filter will receive packets sent from the @netdev "all" means this filter will receive packets both sent to/from
the @netdev

v11:
- address Jason&Daniel's comments
- add multiqueue support, the last 2 patches
- rebased to the latest master

v10:
- Reimplemented using QOM (suggested by stefan)
- Do not export NetQueue internals (suggested by stefan)
- see individual patch for detail

v9:
- squash command description and help to patch 1&3
- qapi changes according to Markus&Eric's comments
- see individual patch for detail

v8:
- some minor fixes according to Thomas's comments
- rebased to the latest master branch

v7:
- print filter info when execute 'info network'
- addressed Jason's comments

v6:
- add multiqueue support, please see individual patch for detail

v5:
- add a sent_cb param to filter receive_iov api
- squash the 4th patch into patch 3
- remove dummy sent_cb (buffer filter)
- addressed Jason's other comments, see individual patches for detail

v4:
- get rid of struct Filter
- squash the 4th patch into patch 2
- fix qemu_netfilter_pass_to_next_iov
- get rid of bh (buffer filter)
- release the packet to next filter instead of to receiver (buffer filter)

v3:
- add an api to pass the packet to next filter
- remove netfilters when delete netdev
- add qtest testcases for netfilter
- addressed comments from Jason

v2:
- add a chain option to netfilter object
- move the hook place earlier, before net_queue_send
- drop the unused api in buffer filter
- squash buffer filter patches into one
- remove receive() api from netfilter, only receive_iov() is enough
- addressed comments from Jason&Thomas

v1:
initial patch.

Yang Hongyang (12):
qmp: delete qemu opts when delete an object init/cleanup of netfilter object
netfilter: hook packets before net queue send
net: merge qemu_deliver_packet and qemu_deliver_packet_iov net/queue: introduce NetQueueDeliverFunc
netfilter: add an API to pass the packet to next filter
netfilter: print filter info associate with the netdev net/queue: export qemu_net_queue_append_iov
netfilter: add a netbuffer filter
tests: add test cases for netfilter object netfilter/multiqueue: introduce netfilter name
netfilter: add multiqueue support

 include/net/filter.h    |  76 ++++++++++++
 include/net/net.h       |   6 +-
 include/net/queue.h     |  20 +++-
 include/qemu/typedefs.h |   1 +
 net/Makefile.objs       |   2 +
 net/filter-buffer.c     | 170 +++++++++++++++++++++++++++
 net/filter.c            | 306 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/net.c               | 108 +++++++++++++----
 net/queue.c             |  24 ++--
 qapi-schema.json        |  18 +++
 qemu-options.hx         |  18 +++
 qmp.c                   |   4 +
 tests/.gitignore        |   1 +
 tests/Makefile          |   2 +
 tests/test-netfilter.c  | 200 +++++++++++++++++++++++++++++++
 vl.c                    |  18 ++-
 16 files changed, 927 insertions(+), 47 deletions(-)
 create mode 100644 include/net/filter.h
 create mode 100644 net/filter-buffer.c
 create mode 100644 net/filter.c
 create mode 100644 tests/test-netfilter.c
Signature · 1 line
1.9.1
#1 lore raw

PATCH v11 1/12 qmp: delete qemu opts when delete an object

When delete an object, we need to delete the associated qemu opts, otherwise, we can not add another object with the same name using object_add.
The case happens when we start qemu with:
-object xxx,id=aa
then we delete this object with:
object_del aa
then add the object with:
object_add xxx,id=aa

QEMU will return error with Duplicate ID error.

---
 qmp.c | 4 ++++
 1 file changed, 4 insertions(+)
qmp.c +4 -0
index 9623c80..4bd44c3 100644
@@ -686,6 +686,7 @@ void qmp_object_del(const char *id, Error **errp)
 {
     Object *container;
     Object *obj;
+    QemuOpts *opts;
 
     container = object_get_objects_root();
     obj = object_resolve_path_component(container, id);
@@ -699,6 +700,9 @@ void qmp_object_del(const char *id, Error **errp)
         return;
     }
     object_unparent(obj);
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);
 }
 
 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
Signature · 1 line
1.9.1
#2 lore raw

Re: [Qemu-devel] [PATCH v11 01/12] qmp: delete qemu opts when delete an object

This has finally reached the front of my review queue. I apologize for the loooong delay.

Copying Paolo for another pair of eyeballs (he wrote this code).

Quoted 30 lines · Yang Hongyang

When delete an object, we need to delete the associated qemu opts, otherwise, we can not add another object with the same name using object_add.
The case happens when we start qemu with:
-object xxx,id=aa
then we delete this object with:
object_del aa
then add the object with:
object_add xxx,id=aa

QEMU will return error with Duplicate ID error.

---
 qmp.c | 4 ++++
 1 file changed, 4 insertions(+)
qmp.c +1 -0
index 9623c80..4bd44c3 100644
@@ -686,6 +686,7 @@ void qmp_object_del(const char *id, Error **errp)
 {
     Object *container;
     Object *obj;
+    QemuOpts *opts;
 
     container = object_get_objects_root();
     obj = object_resolve_path_component(container, id);

qmp.c void qmp_object_del(const char *id, Error **errp) · line 705

Quoted 6 lines
qmp.c +3 -0
@@ -699,6 +700,9 @@ void qmp_object_del(const char *id, Error **errp)
         return;
     }
     object_unparent(obj);
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);

qemu_find_opts_err("object", &error_abort) please, because when it fails, we want to die right away, not when the null pointer it returns gets dereferenced.

Same sloppiness in netdev_del_completion() and qmp_netdev_del(), not your patch's fault.

Elsewhere, we store the QemuOpts in the object just so we can delete it:
DeviceState, DriveInfo. Paolo, what do you think?

Quoted 3 lines
qmp.c +0 -0
 }
 
 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
#3 lore raw

Re: [Qemu-devel] [PATCH v11 01/12] qmp: delete qemu opts when delete an object

Quoted 5 lines · Markus Armbruster

This has finally reached the front of my review queue. I apologize for the loooong delay.

Copying Paolo for another pair of eyeballs (he wrote this code).

[...]

Quoted 7 lines

qmp.c void qmp_object_del(const char *id, Error **errp) · line 705

Quoted 3 lines
qmp.c +3 -0
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);

qemu_find_opts_err("object", &error_abort) please, because when it fails, we want to die right away, not when the null pointer it returns gets dereferenced.

Thanks for the review.
Jason, do you want me to propose a fix on top of this series or simply drop this for now because this patch is an independent bug fix and won't affect the other filter patch series.

Quoted 6 lines

Same sloppiness in netdev_del_completion() and qmp_netdev_del(), not your patch's fault.

Elsewhere, we store the QemuOpts in the object just so we can delete it:
DeviceState, DriveInfo. Paolo, what do you think?

I don't get it. Currently, only objects created at the beginning through QEMU command line will be stored in the QemuOpts, objects that created with object_add won't stored in QemuOpts. Do you mean for DeviceState, DriveInfo they store there QemuOpts explicity so that they can delete it? Why don't we just delete it from objects directly instead?

Quoted 6 lines
Quoted 4 lines
  }

MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)

.

Signature · 2 lines
Thanks,
Yang.
#4 lore raw

Re: [Qemu-devel] [PATCH v11 01/12] qmp: delete qemu opts when delete an object

Quoted 24 lines · Yang Hongyang
Quoted 5 lines · Markus Armbruster

This has finally reached the front of my review queue. I apologize for the loooong delay.

Copying Paolo for another pair of eyeballs (he wrote this code).

[...]

Quoted 7 lines

qmp.c void qmp_object_del(const char *id, Error **errp) · line 705

Quoted 3 lines
qmp.c +3 -0
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);

qemu_find_opts_err("object", &error_abort) please, because when it fails, we want to die right away, not when the null pointer it returns gets dereferenced.

Thanks for the review.
Jason, do you want me to propose a fix on top of this series or simply drop this for now because this patch is an independent bug fix and won't affect the other filter patch series.

Quoted 4 lines

Same sloppiness in netdev_del_completion() and qmp_netdev_del(), not your patch's fault.

Quoted 8 lines
Quoted 2 lines

Elsewhere, we store the QemuOpts in the object just so we can delete it:
DeviceState, DriveInfo. Paolo, what do you think?

I don't get it. Currently, only objects created at the beginning through QEMU command line will be stored in the QemuOpts, objects that created with object_add won't stored in QemuOpts. Do you mean for DeviceState, DriveInfo they store there QemuOpts explicity so that they can delete it? Why don't we just delete it from objects directly instead?

Let me elaborate.

We have the same pattern in multiple places: some kind of object gets configured via QemuOpts, and an object's QemuOpts need to stay around until the object dies.

Example 1: Block device backends

DriveInfo has a member opts.

drive_new() stores the QemuOpts in dinfo->opts.

drive_info_del() destroys dinfo->opts.

Note: DriveInfo member opts is always non-null. But not every BlockBackend has a DriveInfo.

Example 2: Device frontends

DeviceState has a member opts.

qdev_device_add() stores the QemuOpts in dev->opts.

device_finalize() destroys dev->opts.

Note: DeviceState member opts may be null (not every device is created by qdev_device_add()). Fine, because qemu_opts_del(NULL) is a no-op.

Example 3: Character device backends

    CharDriverState has a member opts.

    qemu_chr_new_from_opts() stores the QemuOpts in chr->opts.

    qemu_chr_delete() destroys chr->opts.

Example 4: Network device backends

Two cases

A. netdev

qmp_netdev_add() does not store the QemuOpts.

qmp_netdev_del() still needs to destroy it. It has to find it somehow. Here's how it does it:

opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); if (!opts) {
error_setg(errp, "Device '%s' is not a netdev", id); return;
}

The !opts condition is a non-obvious way to test "not created with -netdev", see commit 645c949. Note that the commit's claim that qemu_opts_del(NULL) crashes is no longer true since commit 4782183.

B. Legacy net

hmp_host_net_add() does not store the QemuOpts.

hmp_host_net_remove() still needs to destroy it. I can't see where that happens, and I'm not sure it does.

Example 5: Generic object

object_create() does not store the QemuOpts.

It still needs to be destroyed along with the object. It isn't, and your patch fixes it.

Personally, I find the technique in example 1-3 easier to understand than the one in example 4-5.

#5 lore raw

Re: [Qemu-devel] [PATCH v11 01/12] qmp: delete qemu opts when delete an object

Quoted 29 lines · Markus Armbruster
Quoted 27 lines · Yang Hongyang
Quoted 5 lines · Markus Armbruster

This has finally reached the front of my review queue. I apologize for the loooong delay.

Copying Paolo for another pair of eyeballs (he wrote this code).

[...]

Quoted 7 lines

qmp.c void qmp_object_del(const char *id, Error **errp) · line 705

Quoted 3 lines
qmp.c +3 -0
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);

qemu_find_opts_err("object", &error_abort) please, because when it fails, we want to die right away, not when the null pointer it returns gets dereferenced.

Thanks for the review.
Jason, do you want me to propose a fix on top of this series or simply drop this for now because this patch is an independent bug fix and won't affect the other filter patch series.

Quoted 6 lines

Same sloppiness in netdev_del_completion() and qmp_netdev_del(), not your patch's fault.

Elsewhere, we store the QemuOpts in the object just so we can delete it:
DeviceState, DriveInfo. Paolo, what do you think?

Quoted 7 lines
Quoted 5 lines

I don't get it. Currently, only objects created at the beginning through QEMU command line will be stored in the QemuOpts, objects that created with object_add won't stored in QemuOpts. Do you mean for DeviceState, DriveInfo they store there QemuOpts explicity so that they can delete it? Why don't we just delete it from objects directly instead?

Let me elaborate.

Thanks very much for the elaboration.

Quoted 28 lines

We have the same pattern in multiple places: some kind of object gets configured via QemuOpts, and an object's QemuOpts need to stay around until the object dies.

Example 1: Block device backends

DriveInfo has a member opts.

drive_new() stores the QemuOpts in dinfo->opts.

drive_info_del() destroys dinfo->opts.

Note: DriveInfo member opts is always non-null. But not every BlockBackend has a DriveInfo.

Example 2: Device frontends

DeviceState has a member opts.

qdev_device_add() stores the QemuOpts in dev->opts.

device_finalize() destroys dev->opts.

Note: DeviceState member opts may be null (not every device is created by qdev_device_add()). Fine, because qemu_opts_del(NULL) is a no-op.

Quoted 7 lines

Example 3: Character device backends

     CharDriverState has a member opts.

     qemu_chr_new_from_opts() stores the QemuOpts in chr->opts.

     qemu_chr_delete() destroys chr->opts.

1-3 store there ops in there own state, not in global ops group right?

Quoted 18 lines

Example 4: Network device backends

Two cases

A. netdev

qmp_netdev_add() does not store the QemuOpts.

qmp_netdev_del() still needs to destroy it. It has to find it somehow. Here's how it does it:

opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); if (!opts) {
error_setg(errp, "Device '%s' is not a netdev", id); return;
}

Quoted 8 lines

The !opts condition is a non-obvious way to test "not created with -netdev", see commit 645c949. Note that the commit's claim that qemu_opts_del(NULL) crashes is no longer true since commit 4782183.

B. Legacy net

hmp_host_net_add() does not store the QemuOpts.

I'm afraid it does store the QemuOpts, but not in it's own state. net/net.c:
1088 qemu_opt_set(opts, "type", device, &error_abort);
This will store the QemuOpts, or am I misunderstood it?

Quoted 6 lines

hmp_host_net_remove() still needs to destroy it. I can't see where that happens, and I'm not sure it does.

Example 5: Generic object

Quoted 7 lines

object_create() does not store the QemuOpts.

It still needs to be destroyed along with the object. It isn't, and your patch fixes it.

Personally, I find the technique in example 1-3 easier to understand than the one in example 4-5.

I agree that opts should not be used to determine not created something while there's case when something created but Opts not stored.

Quoted 2 lines

.

Signature · 2 lines
Thanks,
Yang.
#6 lore raw

Re: [Qemu-devel] [PATCH v11 01/12] qmp: delete qemu opts when delete an object

Quoted 71 lines · Yang Hongyang
Quoted 30 lines · Markus Armbruster
Quoted 28 lines · Yang Hongyang
Quoted 5 lines · Markus Armbruster

This has finally reached the front of my review queue. I apologize for the loooong delay.

Copying Paolo for another pair of eyeballs (he wrote this code).

[...]

Quoted 7 lines

qmp.c void qmp_object_del(const char *id, Error **errp) · line 705

Quoted 3 lines
qmp.c +3 -0
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);

qemu_find_opts_err("object", &error_abort) please, because when it fails, we want to die right away, not when the null pointer it returns gets dereferenced.

Thanks for the review.
Jason, do you want me to propose a fix on top of this series or simply drop this for now because this patch is an independent bug fix and won't affect the
other filter patch series.

Quoted 6 lines

Same sloppiness in netdev_del_completion() and qmp_netdev_del(), not your patch's fault.

Elsewhere, we store the QemuOpts in the object just so we can delete it:
DeviceState, DriveInfo. Paolo, what do you think?

Quoted 7 lines
Quoted 5 lines

I don't get it. Currently, only objects created at the beginning through QEMU command line will be stored in the QemuOpts, objects that created with object_add won't stored in QemuOpts. Do you mean for DeviceState, DriveInfo they store there QemuOpts explicity so that they can delete it? Why don't we just delete it from objects directly instead?

Let me elaborate.

Thanks very much for the elaboration.

Quoted 30 lines

We have the same pattern in multiple places: some kind of object gets configured via QemuOpts, and an object's QemuOpts need to stay around until the object dies.

Example 1: Block device backends

DriveInfo has a member opts.

drive_new() stores the QemuOpts in dinfo->opts.

drive_info_del() destroys dinfo->opts.

Note: DriveInfo member opts is always non-null. But not every BlockBackend has a DriveInfo.

Example 2: Device frontends

DeviceState has a member opts.

qdev_device_add() stores the QemuOpts in dev->opts.

device_finalize() destroys dev->opts.

Note: DeviceState member opts may be null (not every device is created by qdev_device_add()). Fine, because qemu_opts_del(NULL) is a no-op.

Example 3: Character device backends

Quoted 7 lines
Quoted 5 lines
     CharDriverState has a member opts.

     qemu_chr_new_from_opts() stores the QemuOpts in chr->opts.

     qemu_chr_delete() destroys chr->opts.

1-3 store there ops in there own state, not in global ops group right?

Both! But keeping a pointer in their own state simplifies calling qemu_opts_del() on destruction, and also makes it more obvious what is keeping the QemuOpts alive.

Quoted 22 lines
Quoted 22 lines

Example 4: Network device backends

Two cases

A. netdev

qmp_netdev_add() does not store the QemuOpts.

qmp_netdev_del() still needs to destroy it. It has to find it somehow. Here's how it does it:

opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); if (!opts) {
error_setg(errp, "Device '%s' is not a netdev", id); return;
}

The !opts condition is a non-obvious way to test "not created with -netdev", see commit 645c949. Note that the commit's claim that qemu_opts_del(NULL) crashes is no longer true since commit 4782183.

Quoted 8 lines
Quoted 3 lines

B. Legacy net

hmp_host_net_add() does not store the QemuOpts.

I'm afraid it does store the QemuOpts, but not in it's own state. net/net.c:
1088 qemu_opt_set(opts, "type", device, &error_abort);
This will store the QemuOpts, or am I misunderstood it?

Doesn't store opts anywhere, actually. It merely modifies it (adds a parameter "type")

Quoted 16 lines
Quoted 6 lines

hmp_host_net_remove() still needs to destroy it. I can't see where that happens, and I'm not sure it does.

Example 5: Generic object

Quoted 7 lines

object_create() does not store the QemuOpts.

It still needs to be destroyed along with the object. It isn't, and your patch fixes it.

Personally, I find the technique in example 1-3 easier to understand than the one in example 4-5.

I agree that opts should not be used to determine not created something while there's case when something created but Opts not stored.

#7 lore raw

Re: [Qemu-devel] [PATCH v11 01/12] qmp: delete qemu opts when delete an object

Quoted 77 lines · Markus Armbruster
Quoted 75 lines · Yang Hongyang
Quoted 30 lines · Markus Armbruster
Quoted 28 lines · Yang Hongyang
Quoted 5 lines · Markus Armbruster

This has finally reached the front of my review queue. I apologize for the loooong delay.

Copying Paolo for another pair of eyeballs (he wrote this code).

[...]

Quoted 7 lines

qmp.c void qmp_object_del(const char *id, Error **errp) · line 705

Quoted 3 lines
qmp.c +3 -0
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);

qemu_find_opts_err("object", &error_abort) please, because when it fails, we want to die right away, not when the null pointer it returns gets dereferenced.

Thanks for the review.
Jason, do you want me to propose a fix on top of this series or simply drop this for now because this patch is an independent bug fix and won't affect the
other filter patch series.

Quoted 6 lines

Same sloppiness in netdev_del_completion() and qmp_netdev_del(), not your patch's fault.

Elsewhere, we store the QemuOpts in the object just so we can delete it:
DeviceState, DriveInfo. Paolo, what do you think?

Quoted 7 lines
Quoted 5 lines

I don't get it. Currently, only objects created at the beginning through QEMU command line will be stored in the QemuOpts, objects that created with object_add won't stored in QemuOpts. Do you mean for DeviceState, DriveInfo they store there QemuOpts explicity so that they can delete it? Why don't we just delete it from objects directly instead?

Let me elaborate.

Thanks very much for the elaboration.

Quoted 34 lines

We have the same pattern in multiple places: some kind of object gets configured via QemuOpts, and an object's QemuOpts need to stay around until the object dies.

Example 1: Block device backends

DriveInfo has a member opts.

drive_new() stores the QemuOpts in dinfo->opts.

drive_info_del() destroys dinfo->opts.

Note: DriveInfo member opts is always non-null. But not every BlockBackend has a DriveInfo.

Example 2: Device frontends

DeviceState has a member opts.

qdev_device_add() stores the QemuOpts in dev->opts.

device_finalize() destroys dev->opts.

Note: DeviceState member opts may be null (not every device is created by qdev_device_add()). Fine, because qemu_opts_del(NULL) is a no-op.

Example 3: Character device backends

CharDriverState has a member opts.

qemu_chr_new_from_opts() stores the QemuOpts in chr->opts.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line
      qemu_chr_delete() destroys chr->opts.

1-3 store there ops in there own state, not in global ops group right?

Both! But keeping a pointer in their own state simplifies calling qemu_opts_del() on destruction, and also makes it more obvious what is keeping the QemuOpts alive.

I see. Thanks.

Quoted 27 lines
Quoted 27 lines
Quoted 26 lines

Example 4: Network device backends

Two cases

A. netdev

qmp_netdev_add() does not store the QemuOpts.

qmp_netdev_del() still needs to destroy it. It has to find it somehow. Here's how it does it:

opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); if (!opts) {
error_setg(errp, "Device '%s' is not a netdev", id); return;
}

The !opts condition is a non-obvious way to test "not created with -netdev", see commit 645c949. Note that the commit's claim that qemu_opts_del(NULL) crashes is no longer true since commit 4782183.

B. Legacy net

hmp_host_net_add() does not store the QemuOpts.

Quoted 7 lines
Quoted 4 lines

I'm afraid it does store the QemuOpts, but not in it's own state. net/net.c:
1088 qemu_opt_set(opts, "type", device, &error_abort);
This will store the QemuOpts, or am I misunderstood it?

Doesn't store opts anywhere, actually. It merely modifies it (adds a parameter "type")

As you said "store" means store in there own state, then I see...thanks

Quoted 19 lines
Quoted 9 lines
Quoted 9 lines

hmp_host_net_remove() still needs to destroy it. I can't see where that happens, and I'm not sure it does.

Example 5: Generic object

object_create() does not store the QemuOpts.

Quoted 8 lines
Quoted 5 lines

It still needs to be destroyed along with the object. It isn't, and your patch fixes it.

Personally, I find the technique in example 1-3 easier to understand than the one in example 4-5.

I agree that opts should not be used to determine not created something while there's case when something created but Opts not stored.

.

Signature · 2 lines
Thanks,
Yang.
#8 lore raw

Re: [Qemu-devel] [PATCH v11 01/12] qmp: delete qemu opts when delete an object

In reply to Markus Armbruster
Quoted 72 lines · Markus Armbruster
Quoted 24 lines · Yang Hongyang
Quoted 5 lines · Markus Armbruster

This has finally reached the front of my review queue. I apologize for the loooong delay.

Copying Paolo for another pair of eyeballs (he wrote this code).

[...]

Quoted 7 lines

qmp.c void qmp_object_del(const char *id, Error **errp) · line 705

Quoted 3 lines
qmp.c +3 -0
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);

qemu_find_opts_err("object", &error_abort) please, because when it fails, we want to die right away, not when the null pointer it returns gets dereferenced.

Thanks for the review.
Jason, do you want me to propose a fix on top of this series or simply drop this for now because this patch is an independent bug fix and won't affect the other filter patch series.

Quoted 4 lines

Same sloppiness in netdev_del_completion() and qmp_netdev_del(), not your patch's fault.

Quoted 8 lines
Quoted 2 lines

Elsewhere, we store the QemuOpts in the object just so we can delete it:
DeviceState, DriveInfo. Paolo, what do you think?

I don't get it. Currently, only objects created at the beginning through QEMU command line will be stored in the QemuOpts, objects that created with object_add won't stored in QemuOpts. Do you mean for DeviceState, DriveInfo they store there QemuOpts explicity so that they can delete it? Why don't we just delete it from objects directly instead?

Let me elaborate.

We have the same pattern in multiple places: some kind of object gets configured via QemuOpts, and an object's QemuOpts need to stay around until the object dies.

Example 1: Block device backends

DriveInfo has a member opts.

drive_new() stores the QemuOpts in dinfo->opts.

drive_info_del() destroys dinfo->opts.

Note: DriveInfo member opts is always non-null. But not every BlockBackend has a DriveInfo.

Example 2: Device frontends

DeviceState has a member opts.

qdev_device_add() stores the QemuOpts in dev->opts.

device_finalize() destroys dev->opts.

Note: DeviceState member opts may be null (not every device is created by qdev_device_add()). Fine, because qemu_opts_del(NULL) is a no-op.

Example 3: Character device backends

     CharDriverState has a member opts.

     qemu_chr_new_from_opts() stores the QemuOpts in chr->opts.

     qemu_chr_delete() destroys chr->opts.
Quoted 7 lines

Example 4: Network device backends

Two cases

A. netdev

qmp_netdev_add() does not store the QemuOpts.

The QemuOpts stored by qmp_netdev_add() and also hmp_netdev_add(). through this function:
net/net.c: qmp_netdev_add()
1134 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);

hmp.c: hmp_netdev_add()
1579 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);

Quoted 33 lines

qmp_netdev_del() still needs to destroy it. It has to find it somehow. Here's how it does it:

opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); if (!opts) {
error_setg(errp, "Device '%s' is not a netdev", id); return;
}

The !opts condition is a non-obvious way to test "not created with -netdev", see commit 645c949. Note that the commit's claim that qemu_opts_del(NULL) crashes is no longer true since commit 4782183.

B. Legacy net

hmp_host_net_add() does not store the QemuOpts.

hmp_host_net_remove() still needs to destroy it. I can't see where that happens, and I'm not sure it does.

Example 5: Generic object

object_create() does not store the QemuOpts.

It still needs to be destroyed along with the object. It isn't, and your patch fixes it.

Personally, I find the technique in example 1-3 easier to understand than the one in example 4-5.
.

Signature · 2 lines
Thanks,
Yang.
#9 lore raw

Re: [Qemu-devel] [PATCH v11 01/12] qmp: delete qemu opts when delete an object

Quoted 82 lines · Yang Hongyang
Quoted 73 lines · Markus Armbruster
Quoted 25 lines · Yang Hongyang
Quoted 5 lines · Markus Armbruster

This has finally reached the front of my review queue. I apologize for the loooong delay.

Copying Paolo for another pair of eyeballs (he wrote this code).

[...]

Quoted 7 lines

qmp.c void qmp_object_del(const char *id, Error **errp) · line 705

Quoted 3 lines
qmp.c +3 -0
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);

qemu_find_opts_err("object", &error_abort) please, because when it fails, we want to die right away, not when the null pointer it returns gets dereferenced.

Thanks for the review.
Jason, do you want me to propose a fix on top of this series or simply drop this for now because this patch is an independent bug fix and won't affect the
other filter patch series.

Quoted 4 lines

Same sloppiness in netdev_del_completion() and qmp_netdev_del(), not your patch's fault.

Quoted 8 lines
Quoted 2 lines

Elsewhere, we store the QemuOpts in the object just so we can delete it:
DeviceState, DriveInfo. Paolo, what do you think?

I don't get it. Currently, only objects created at the beginning through QEMU command line will be stored in the QemuOpts, objects that created with object_add won't stored in QemuOpts. Do you mean for DeviceState, DriveInfo they store there QemuOpts explicity so that they can delete it? Why don't we just delete it from objects directly instead?

Let me elaborate.

We have the same pattern in multiple places: some kind of object gets configured via QemuOpts, and an object's QemuOpts need to stay around until the object dies.

Example 1: Block device backends

DriveInfo has a member opts.

drive_new() stores the QemuOpts in dinfo->opts.

drive_info_del() destroys dinfo->opts.

Note: DriveInfo member opts is always non-null. But not every BlockBackend has a DriveInfo.

Example 2: Device frontends

DeviceState has a member opts.

qdev_device_add() stores the QemuOpts in dev->opts.

device_finalize() destroys dev->opts.

Note: DeviceState member opts may be null (not every device is created by qdev_device_add()). Fine, because qemu_opts_del(NULL) is a no-op.

Example 3: Character device backends

     CharDriverState has a member opts.

     qemu_chr_new_from_opts() stores the QemuOpts in chr->opts.

     qemu_chr_delete() destroys chr->opts.
Quoted 7 lines

Example 4: Network device backends

Two cases

A. netdev

qmp_netdev_add() does not store the QemuOpts.

Quoted 7 lines

The QemuOpts stored by qmp_netdev_add() and also hmp_netdev_add(). through this function:
net/net.c: qmp_netdev_add()
1134 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);

hmp.c: hmp_netdev_add()
1579 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);

That's where the QemuOpts are created. By "does not store" I mean "does not store in its own state, unlike example 1-3".

Quoted 33 lines
Quoted 33 lines

qmp_netdev_del() still needs to destroy it. It has to find it somehow. Here's how it does it:

opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); if (!opts) {
error_setg(errp, "Device '%s' is not a netdev", id); return;
}

The !opts condition is a non-obvious way to test "not created with -netdev", see commit 645c949. Note that the commit's claim that qemu_opts_del(NULL) crashes is no longer true since commit 4782183.

B. Legacy net

hmp_host_net_add() does not store the QemuOpts.

hmp_host_net_remove() still needs to destroy it. I can't see where that happens, and I'm not sure it does.

Example 5: Generic object

object_create() does not store the QemuOpts.

It still needs to be destroyed along with the object. It isn't, and your patch fixes it.

Personally, I find the technique in example 1-3 easier to understand than the one in example 4-5.
.

#10 lore raw

Re: [Qemu-devel] [PATCH v11 01/12] qmp: delete qemu opts when delete an object

Quoted 89 lines · Markus Armbruster
Quoted 87 lines · Yang Hongyang
Quoted 73 lines · Markus Armbruster
Quoted 25 lines · Yang Hongyang
Quoted 5 lines · Markus Armbruster

This has finally reached the front of my review queue. I apologize for the loooong delay.

Copying Paolo for another pair of eyeballs (he wrote this code).

[...]

Quoted 7 lines

qmp.c void qmp_object_del(const char *id, Error **errp) · line 705

Quoted 3 lines
qmp.c +3 -0
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);

qemu_find_opts_err("object", &error_abort) please, because when it fails, we want to die right away, not when the null pointer it returns gets dereferenced.

Thanks for the review.
Jason, do you want me to propose a fix on top of this series or simply drop this for now because this patch is an independent bug fix and won't affect the
other filter patch series.

Quoted 4 lines

Same sloppiness in netdev_del_completion() and qmp_netdev_del(), not your patch's fault.

Quoted 8 lines
Quoted 2 lines

Elsewhere, we store the QemuOpts in the object just so we can delete it:
DeviceState, DriveInfo. Paolo, what do you think?

I don't get it. Currently, only objects created at the beginning through QEMU command line will be stored in the QemuOpts, objects that created with object_add won't stored in QemuOpts. Do you mean for DeviceState, DriveInfo they store there QemuOpts explicity so that they can delete it? Why don't we just delete it from objects directly instead?

Let me elaborate.

We have the same pattern in multiple places: some kind of object gets configured via QemuOpts, and an object's QemuOpts need to stay around until the object dies.

Example 1: Block device backends

DriveInfo has a member opts.

drive_new() stores the QemuOpts in dinfo->opts.

drive_info_del() destroys dinfo->opts.

Note: DriveInfo member opts is always non-null. But not every BlockBackend has a DriveInfo.

Example 2: Device frontends

DeviceState has a member opts.

qdev_device_add() stores the QemuOpts in dev->opts.

device_finalize() destroys dev->opts.

Note: DeviceState member opts may be null (not every device is created by qdev_device_add()). Fine, because qemu_opts_del(NULL) is a no-op.

Example 3: Character device backends

      CharDriverState has a member opts.

      qemu_chr_new_from_opts() stores the QemuOpts in chr->opts.

      qemu_chr_delete() destroys chr->opts.
Quoted 7 lines

Example 4: Network device backends

Two cases

A. netdev

qmp_netdev_add() does not store the QemuOpts.

The QemuOpts stored by qmp_netdev_add() and also hmp_netdev_add(). through this function:
net/net.c: qmp_netdev_add()
1134 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);

Quoted 5 lines
Quoted 2 lines

hmp.c: hmp_netdev_add()
1579 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);

That's where the QemuOpts are created. By "does not store" I mean "does not store in its own state, unlike example 1-3".

Understand, thank you.

Quoted 36 lines
Quoted 27 lines
Quoted 27 lines

qmp_netdev_del() still needs to destroy it. It has to find it somehow. Here's how it does it:

opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); if (!opts) {
error_setg(errp, "Device '%s' is not a netdev", id); return;
}

The !opts condition is a non-obvious way to test "not created with -netdev", see commit 645c949. Note that the commit's claim that qemu_opts_del(NULL) crashes is no longer true since commit 4782183.

B. Legacy net

hmp_host_net_add() does not store the QemuOpts.

hmp_host_net_remove() still needs to destroy it. I can't see where that happens, and I'm not sure it does.

Example 5: Generic object

object_create() does not store the QemuOpts.

Quoted 7 lines
Quoted 7 lines

It still needs to be destroyed along with the object. It isn't, and your patch fixes it.

Personally, I find the technique in example 1-3 easier to understand than the one in example 4-5.
.

.

Signature · 2 lines
Thanks,
Yang.
#11 lore raw

Re: [Qemu-devel] [PATCH v11 01/12] qmp: delete qemu opts when delete an object

In reply to Yang Hongyang
Quoted 15 lines · Yang Hongyang
Quoted 5 lines · Markus Armbruster

This has finally reached the front of my review queue. I apologize for the loooong delay.

Copying Paolo for another pair of eyeballs (he wrote this code).

[...]

Quoted 7 lines

qmp.c void qmp_object_del(const char *id, Error **errp) · line 705

Quoted 3 lines
qmp.c +3 -0
+
+    opts = qemu_opts_find(qemu_find_opts_err("object", NULL), id);
+    qemu_opts_del(opts);

qemu_find_opts_err("object", &error_abort) please, because when it fails, we want to die right away, not when the null pointer it returns gets dereferenced.

Quoted 6 lines

Thanks for the review.
Jason, do you want me to propose a fix on top of this series or simply drop
this for now because this patch is an independent bug fix and won't affect the
other filter patch series.

Will drop this patch from my tree.

Thanks

Quoted 21 lines
Quoted 7 lines

Same sloppiness in netdev_del_completion() and qmp_netdev_del(), not your patch's fault.

Elsewhere, we store the QemuOpts in the object just so we can delete it:
DeviceState, DriveInfo. Paolo, what do you think?

I don't get it. Currently, only objects created at the beginning through QEMU command line will be stored in the QemuOpts, objects that created with object_add won't stored in QemuOpts. Do you mean for DeviceState, DriveInfo they store there QemuOpts explicity so that they can delete it? Why don't we just delete it from objects directly instead?

Quoted 6 lines
Quoted 4 lines
  }

MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)

.

#12 lore raw

PATCH v11 2/12 init/cleanup of netfilter object

In reply to Yang Hongyang

Add a netfilter object based on QOM.

A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless.

QTAILQ_ENTRY next used by netdev, filter belongs to the specific netdev is in this queue.

Also init delayed object after net_init_clients, because netfilters need to be initialized after net clients initialized.

---
v11: no need to free nf->netdev_id, it will be auto freeed while object deleted
remove global_list net_filters, will add back when needed
v10: use QOM for netfilter
v9: use flat union instead of simple union in QAPI schema
v8: include vhost_net header
v7: add check for vhost
fix error propagate bug
v6: add multiqueue support (net_filter_init1)
v5: remove model from NetFilterState
add a sent_cb param to receive_iov API

---
 include/net/filter.h    |  60 +++++++++++++++++++++
 include/net/net.h       |   1 +
 include/qemu/typedefs.h |   1 +
 net/Makefile.objs       |   1 +
 net/filter.c            | 138 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/net.c               |   7 +++
 qapi-schema.json        |  18 +++++++
 vl.c                    |  13 ++---
 8 files changed, 233 insertions(+), 6 deletions(-)
 create mode 100644 include/net/filter.h
 create mode 100644 net/filter.c
include/net/filter.h new file +60 -0
new file mode 100644
index 0000000..226f2f7
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTER_H
+#define QEMU_NET_FILTER_H
+
+#include "qom/object.h"
+#include "qemu-common.h"
+#include "qemu/typedefs.h"
+#include "net/queue.h"
+
+#define TYPE_NETFILTER "netfilter"
+#define NETFILTER(obj) \
+    OBJECT_CHECK(NetFilterState, (obj), TYPE_NETFILTER)
+#define NETFILTER_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(NetFilterClass, (obj), TYPE_NETFILTER)
+#define NETFILTER_CLASS(klass) \
+    OBJECT_CLASS_CHECK(NetFilterClass, (klass), TYPE_NETFILTER)
+
+typedef void (FilterSetup) (NetFilterState *nf, Error **errp);
+typedef void (FilterCleanup) (NetFilterState *nf);
+/*
+ * Return:
+ *   0: finished handling the packet, we should continue
+ *   size: filter stolen this packet, we stop pass this packet further
+ */
+typedef ssize_t (FilterReceiveIOV)(NetFilterState *nc,
+                                   NetClientState *sender,
+                                   unsigned flags,
+                                   const struct iovec *iov,
+                                   int iovcnt,
+                                   NetPacketSent *sent_cb);
+
+struct NetFilterClass {
+    ObjectClass parent_class;
+
+    FilterSetup *setup;
+    FilterCleanup *cleanup;
+    FilterReceiveIOV *receive_iov;
+};
+typedef struct NetFilterClass NetFilterClass;
+
+
+struct NetFilterState {
+    /* private */
+    Object parent;
+
+    /* protected */
+    char *netdev_id;
+    NetClientState *netdev;
+    NetFilterChain chain;
+    QTAILQ_ENTRY(NetFilterState) next;
+};
+
+#endif /* QEMU_NET_FILTER_H */
include/net/net.h +1 -0
index 6a6cbef..36e5fab 100644
@@ -92,6 +92,7 @@ struct NetClientState {
     NetClientDestructor *destructor;
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, NetFilterState) filters;
 };
 
 typedef struct NICState {
include/qemu/typedefs.h +1 -0
index f8a9dd6..2c0648f 100644
@@ -45,6 +45,7 @@ typedef struct Monitor Monitor;
 typedef struct MouseTransformInfo MouseTransformInfo;
 typedef struct MSIMessage MSIMessage;
 typedef struct NetClientState NetClientState;
+typedef struct NetFilterState NetFilterState;
 typedef struct NICInfo NICInfo;
 typedef struct PcGuestInfo PcGuestInfo;
 typedef struct PCIBridge PCIBridge;
net/Makefile.objs +1 -0
index ec19cb3..914aec0 100644
@@ -13,3 +13,4 @@ common-obj-$(CONFIG_HAIKU) += tap-haiku.o
 common-obj-$(CONFIG_SLIRP) += slirp.o
 common-obj-$(CONFIG_VDE) += vde.o
 common-obj-$(CONFIG_NETMAP) += netmap.o
+common-obj-y += filter.o
net/filter.c new file +138 -0

138 lines not on this page · raw message

net/net.c +7 -0
index 28a5597..033f4f3 100644
@@ -44,6 +44,7 @@
 #include "qapi/opts-visitor.h"
 #include "qapi/dealloc-visitor.h"
 #include "sysemu/sysemu.h"
+#include "net/filter.h"
 
 /* Net bridge is currently not supported for W32. */
 #if !defined(_WIN32)
@@ -287,6 +288,7 @@ static void qemu_net_client_setup(NetClientState *nc,
 
     nc->incoming_queue = qemu_new_net_queue(nc);
     nc->destructor = destructor;
+    QTAILQ_INIT(&nc->filters);
 }
 
 NetClientState *qemu_new_net_client(NetClientInfo *info,
@@ -384,6 +386,7 @@ void qemu_del_net_client(NetClientState *nc)
 {
     NetClientState *ncs[MAX_QUEUE_NUM];
     int queues, i;
+    NetFilterState *nf, *next;
 
     assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
 
@@ -395,6 +398,10 @@ void qemu_del_net_client(NetClientState *nc)
                                           MAX_QUEUE_NUM);
     assert(queues != 0);
 
+    QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
+        object_unparent(OBJECT(nf));
+    }
+
     /* If there is a peer NIC, delete and cleanup client, but do not free. */
     if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
         NICState *nic = qemu_get_nic(nc->peer);
qapi-schema.json +18 -0
index 2bada60..546500a 100644
@@ -2551,6 +2551,24 @@
     'opts': 'NetClientOptions' } }
 
 ##
+# @NetFilterChain
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.
+#
+# Since 2.5
+##
+{ 'enum': 'NetFilterChain',
+  'data': [ 'all', 'in', 'out' ] }
+
+##
 # @InetSocketAddress
 #
 # Captures a socket address or address range in the Internet namespace.
vl.c +7 -6
index 066a080..ec589e2 100644
@@ -2794,6 +2794,7 @@ static bool object_create_initial(const char *type)
     if (g_str_equal(type, "rng-egd")) {
         return false;
     }
+    /* TODO: return false for concrete netfilters */
     return true;
 }
 
@@ -4313,12 +4314,6 @@ int main(int argc, char **argv, char **envp)
         exit(0);
     }
 
-    if (qemu_opts_foreach(qemu_find_opts("object"),
-                          object_create,
-                          object_create_delayed, NULL)) {
-        exit(1);
-    }
-
     machine_opts = qemu_get_machine_opts();
     if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
                          NULL)) {
@@ -4424,6 +4419,12 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
+    if (qemu_opts_foreach(qemu_find_opts("object"),
+                          object_create,
+                          object_create_delayed, NULL)) {
+        exit(1);
+    }
+
 #ifdef CONFIG_TPM
     if (tpm_init() < 0) {
         exit(1);
Signature · 1 line
1.9.1
#13 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 7 lines · Yang Hongyang

Add a netfilter object based on QOM.

A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless.

Quoted 8 lines

QTAILQ_ENTRY next used by netdev, filter belongs to the specific netdev is in this queue.

Also init delayed object after net_init_clients, because netfilters need to be initialized after net clients initialized.

---

Interface review:

qapi-schema.json line 2557

Quoted 9 lines

+++ b/qapi-schema.json

qapi-schema.json +4 -0
@@ -2551,6 +2551,24 @@
     'opts': 'NetClientOptions' } }
 
 ##
+# @NetFilterChain
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.

Grammar. Maybe:

This enum describes which packets are being tracked by a netfilter chain attached as a filter to a netdev object.

qapi-schema.json line 2569

Quoted 12 lines
qapi-schema.json +12 -0
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.
+#
+# Since 2.5
+##
+{ 'enum': 'NetFilterChain',
+  'data': [ 'all', 'in', 'out' ] }

I don't see any other QMP usage of this enum anywhere in the series. Are you planning on supporting QMP? If so, let's get that design discussion started. If not, why not?

In particular, you may want to base things on top of my work to make QMP 'netdev_add' a full-fledged introspectible command (still pending some qapi commits landing upstream):

https://lists.gnu.org/archive/html/qemu-devel/2015-09/msg02602.html

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#14 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Hi Eric,

Quoted 31 lines · Eric Blake
Quoted 7 lines · Yang Hongyang

Add a netfilter object based on QOM.

A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless.

Quoted 8 lines

QTAILQ_ENTRY next used by netdev, filter belongs to the specific netdev is in this queue.

Also init delayed object after net_init_clients, because netfilters need to be initialized after net clients initialized.

---

Interface review:

qapi-schema.json line 2557

Quoted 9 lines

+++ b/qapi-schema.json

qapi-schema.json +4 -0
@@ -2551,6 +2551,24 @@
      'opts': 'NetClientOptions' } }
 
  ##
+# @NetFilterChain
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.

Grammar. Maybe:

Quoted 2 lines

This enum describes which packets are being tracked by a netfilter chain attached as a filter to a netdev object.

Thanks!

Quoted 17 lines

qapi-schema.json line 2569

Quoted 13 lines
qapi-schema.json +12 -0
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.
+#
+# Since 2.5
+##
+{ 'enum': 'NetFilterChain',
+  'data': [ 'all', 'in', 'out' ] }

I don't see any other QMP usage of this enum anywhere in the series. Are you planning on supporting QMP? If so, let's get that design discussion started. If not, why not?

This series is based on QOM, so the QMP command for object_add will use this enum, for example:
1 { "execute": "qmp_capabilities" }
2 { "execute": "object-add",
3 "arguments": { "qom-type": "filter-buffer",
4 "id": "f0",
5 "props": { "netdev": "bn0",
6 "chain": "in",
7 "interval": 2000 } } }

for hmp:
object_add filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

command options:
-object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

Quoted 7 lines

In particular, you may want to base things on top of my work to make QMP 'netdev_add' a full-fledged introspectible command (still pending some qapi commits landing upstream):

https://lists.gnu.org/archive/html/qemu-devel/2015-09/msg02602.html

Signature · 2 lines
Thanks,
Yang.
#15 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 17 lines · Yang Hongyang
Quoted 6 lines

qapi-schema.json line 2569

Quoted 2 lines
qapi-schema.json +2 -0
+{ 'enum': 'NetFilterChain',
+  'data': [ 'all', 'in', 'out' ] }

I don't see any other QMP usage of this enum anywhere in the series. Are you planning on supporting QMP? If so, let's get that design discussion started. If not, why not?

This series is based on QOM, so the QMP command for object_add will use this enum, for example:
1 { "execute": "qmp_capabilities" }
2 { "execute": "object-add",
3 "arguments": { "qom-type": "filter-buffer",
4 "id": "f0",
5 "props": { "netdev": "bn0",
6 "chain": "in",
7 "interval": 2000 } } }

Quoted 5 lines

for hmp:
object_add filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

command options:
-object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

Do these examples appear in the documentation anywhere? If not, it is worth considering (maybe under 'object-add' in qmp-commands.hx, for example).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#16 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 22 lines · Eric Blake
Quoted 20 lines · Yang Hongyang
Quoted 6 lines

qapi-schema.json line 2569

Quoted 2 lines
qapi-schema.json +2 -0
+{ 'enum': 'NetFilterChain',
+  'data': [ 'all', 'in', 'out' ] }

I don't see any other QMP usage of this enum anywhere in the series. Are you planning on supporting QMP? If so, let's get that design discussion started. If not, why not?

This series is based on QOM, so the QMP command for object_add will use this enum, for example:
1 { "execute": "qmp_capabilities" }
2 { "execute": "object-add",
3 "arguments": { "qom-type": "filter-buffer",
4 "id": "f0",
5 "props": { "netdev": "bn0",
6 "chain": "in",
7 "interval": 2000 } } }

for hmp:
object_add filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

Quoted 6 lines
Quoted 2 lines

command options:
-object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

Do these examples appear in the documentation anywhere? If not, it is worth considering (maybe under 'object-add' in qmp-commands.hx, for example).

Yes, example is added in the 9th patch, under qemu-options.hx. Thank you.

Quoted 1 line
Signature · 2 lines
Thanks,
Yang.
#17 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

In reply to Yang Hongyang
Quoted 9 lines · Yang Hongyang

Add a netfilter object based on QOM.

A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless.

QTAILQ_ENTRY next used by netdev, filter belongs to the specific netdev is in this queue.

I don't get this paragraph. Not sure it's needed.

Quoted 2 lines

Also init delayed object after net_init_clients, because netfilters need to be initialized after net clients initialized.

A paragraph starting with "Also" in a commit message is a pretty good sign the patch should be split :)

Quoted 68 lines

---
v11: no need to free nf->netdev_id, it will be auto freeed while object deleted
remove global_list net_filters, will add back when needed
v10: use QOM for netfilter
v9: use flat union instead of simple union in QAPI schema
v8: include vhost_net header
v7: add check for vhost
fix error propagate bug
v6: add multiqueue support (net_filter_init1)
v5: remove model from NetFilterState
add a sent_cb param to receive_iov API

---
 include/net/filter.h    |  60 +++++++++++++++++++++
 include/net/net.h       |   1 +
 include/qemu/typedefs.h |   1 +
 net/Makefile.objs       |   1 +
 net/filter.c            | 138 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/net.c               |   7 +++
 qapi-schema.json        |  18 +++++++
 vl.c                    |  13 ++---
 8 files changed, 233 insertions(+), 6 deletions(-)
 create mode 100644 include/net/filter.h
 create mode 100644 net/filter.c
include/net/filter.h new file +36 -0
new file mode 100644
index 0000000..226f2f7
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTER_H
+#define QEMU_NET_FILTER_H
+
+#include "qom/object.h"
+#include "qemu-common.h"
+#include "qemu/typedefs.h"
+#include "net/queue.h"
+
+#define TYPE_NETFILTER "netfilter"
+#define NETFILTER(obj) \
+    OBJECT_CHECK(NetFilterState, (obj), TYPE_NETFILTER)
+#define NETFILTER_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(NetFilterClass, (obj), TYPE_NETFILTER)
+#define NETFILTER_CLASS(klass) \
+    OBJECT_CLASS_CHECK(NetFilterClass, (klass), TYPE_NETFILTER)
+
+typedef void (FilterSetup) (NetFilterState *nf, Error **errp);
+typedef void (FilterCleanup) (NetFilterState *nf);
+/*
+ * Return:
+ *   0: finished handling the packet, we should continue
+ *   size: filter stolen this packet, we stop pass this packet further
+ */
+typedef ssize_t (FilterReceiveIOV)(NetFilterState *nc,
+                                   NetClientState *sender,
+                                   unsigned flags,
+                                   const struct iovec *iov,
+                                   int iovcnt,

include/net/filter.h line 46

Quoted 10 lines
include/net/filter.h new file +10 -0
+                                   NetPacketSent *sent_cb);
+
+struct NetFilterClass {
+    ObjectClass parent_class;
+
+    FilterSetup *setup;
+    FilterCleanup *cleanup;
+    FilterReceiveIOV *receive_iov;
+};
+typedef struct NetFilterClass NetFilterClass;

Not splitting the declaration is more concise:

    typedef struct {
        ObjectClass parent_class;
        FilterSetup *setup;
        FilterCleanup *cleanup;
        FilterReceiveIOV *receive_iov;
    } NetFilterClass;

Are any of the methods optional? If yes, please add suitable comments.

Quoted 103 lines
include/net/filter.h new file +14 -0
+
+
+struct NetFilterState {
+    /* private */
+    Object parent;
+
+    /* protected */
+    char *netdev_id;
+    NetClientState *netdev;
+    NetFilterChain chain;
+    QTAILQ_ENTRY(NetFilterState) next;
+};
+
+#endif /* QEMU_NET_FILTER_H */
include/net/net.h +1 -0
index 6a6cbef..36e5fab 100644
@@ -92,6 +92,7 @@ struct NetClientState {
     NetClientDestructor *destructor;
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, NetFilterState) filters;
 };
 
 typedef struct NICState {
include/qemu/typedefs.h +1 -0
index f8a9dd6..2c0648f 100644
@@ -45,6 +45,7 @@ typedef struct Monitor Monitor;
 typedef struct MouseTransformInfo MouseTransformInfo;
 typedef struct MSIMessage MSIMessage;
 typedef struct NetClientState NetClientState;
+typedef struct NetFilterState NetFilterState;
 typedef struct NICInfo NICInfo;
 typedef struct PcGuestInfo PcGuestInfo;
 typedef struct PCIBridge PCIBridge;
net/Makefile.objs +1 -0
index ec19cb3..914aec0 100644
@@ -13,3 +13,4 @@ common-obj-$(CONFIG_HAIKU) += tap-haiku.o
 common-obj-$(CONFIG_SLIRP) += slirp.o
 common-obj-$(CONFIG_VDE) += vde.o
 common-obj-$(CONFIG_NETMAP) += netmap.o
+common-obj-y += filter.o
net/filter.c new file +50 -0
new file mode 100644
index 0000000..3b810c8
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#include "qemu-common.h"
+#include "qapi/qmp/qerror.h"
+#include "qemu/error-report.h"
+
+#include "net/filter.h"
+#include "net/net.h"
+#include "net/vhost_net.h"
+#include "qom/object_interfaces.h"
+
+static char *netfilter_get_netdev_id(Object *obj, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    return g_strdup(nf->netdev_id);
+}
+
+static void netfilter_set_netdev_id(Object *obj, const char *str, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    nf->netdev_id = g_strdup(str);
+}
+
+static int netfilter_get_chain(Object *obj, Error **errp G_GNUC_UNUSED)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    return nf->chain;
+}
+
+static void netfilter_set_chain(Object *obj, int chain, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    nf->chain = chain;
+}
+
+static void netfilter_init(Object *obj)
+{
+    object_property_add_str(obj, "netdev",
+                            netfilter_get_netdev_id, netfilter_set_netdev_id,
+                            NULL);
+    object_property_add_enum(obj, "chain", "NetFilterChain",
+                             NetFilterChain_lookup,

net/filter.c line 60

Quoted 10 lines
net/filter.c new file +10 -0
+                             netfilter_get_chain, netfilter_set_chain,
+                             NULL);
+}
+
+static void netfilter_finalize(Object *obj)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(obj);
+
+    if (nfc->cleanup) {

Is ->cleanup optional?

net/filter.c line 64

Quoted 4 lines
net/filter.c new file +4 -0
+        nfc->cleanup(nf);
+    }
+
+    if (nf->netdev && !QTAILQ_EMPTY(&nf->netdev->filters)) {

How can nf->netdev be null?

How can nf->netdev->filters be empty?

net/filter.c line 67

Quoted 3 lines
net/filter.c new file +3 -0
+        QTAILQ_REMOVE(&nf->netdev->filters, nf, next);
+    }
+}

Putting netfilter_finalize() after netfilter_complete() would be easier to understand, because then destruction follows creation.

Quoted 13 lines
net/filter.c new file +13 -0
+
+static void netfilter_complete(UserCreatable *uc, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(uc);
+    NetClientState *ncs[MAX_QUEUE_NUM];
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
+    int queues;
+    Error *local_err = NULL;
+
+    if (!nf->netdev_id) {
+        error_setg(errp, "Parameter 'netdev' is required");
+        return;
+    }

net/filter.c line 90

Quoted 10 lines
net/filter.c new file +10 -0
+
+    queues = qemu_find_net_clients_except(nf->netdev_id, ncs,
+                                          NET_CLIENT_OPTIONS_KIND_NIC,
+                                          MAX_QUEUE_NUM);
+    if (queues < 1) {
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
+                   "a network backend id");
+        return;
+    } else if (queues > 1) {
+        error_setg(errp, "Multi queue is not supported");

We spell this multiqueue elsewhere.

Since you're only interested in a single queue, you could save stack space by making ncs[] just one element large. Not worth it if you intend to support multiqueue soon.

net/filter.c line 101

Quoted 11 lines
net/filter.c new file +11 -0
+        return;
+    }
+
+    if (get_vhost_net(ncs[0])) {
+        error_setg(errp, "Vhost is not supported");
+        return;
+    }
+
+    nf->netdev = ncs[0];
+
+    if (nfc->setup) {

Is ->setup optional?

net/filter.c line 108

Quoted 7 lines
net/filter.c new file +7 -0
+        nfc->setup(nf, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
+    }
+    QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);

I think I'd create netdev functions to add and remove filters, so the filter code doesn't have to modify the netdev state. Right now, NetClientState member filters is initialized in net.c, but modified here. But I'm not the net maintainer :)

Quoted 78 lines
net/filter.c new file +30 -0
+}
+
+static void netfilter_class_init(ObjectClass *oc, void *data)
+{
+    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+
+    ucc->complete = netfilter_complete;
+}
+
+static const TypeInfo netfilter_info = {
+    .name = TYPE_NETFILTER,
+    .parent = TYPE_OBJECT,
+    .abstract = true,
+    .class_size = sizeof(NetFilterClass),
+    .class_init = netfilter_class_init,
+    .instance_size = sizeof(NetFilterState),
+    .instance_init = netfilter_init,
+    .instance_finalize = netfilter_finalize,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_USER_CREATABLE },
+        { }
+    }
+};
+
+static void register_types(void)
+{
+    type_register_static(&netfilter_info);
+}
+
+type_init(register_types);
net/net.c +7 -0
index 28a5597..033f4f3 100644
@@ -44,6 +44,7 @@
 #include "qapi/opts-visitor.h"
 #include "qapi/dealloc-visitor.h"
 #include "sysemu/sysemu.h"
+#include "net/filter.h"
 
 /* Net bridge is currently not supported for W32. */
 #if !defined(_WIN32)
@@ -287,6 +288,7 @@ static void qemu_net_client_setup(NetClientState *nc,
 
     nc->incoming_queue = qemu_new_net_queue(nc);
     nc->destructor = destructor;
+    QTAILQ_INIT(&nc->filters);
 }
 
 NetClientState *qemu_new_net_client(NetClientInfo *info,
@@ -384,6 +386,7 @@ void qemu_del_net_client(NetClientState *nc)
 {
     NetClientState *ncs[MAX_QUEUE_NUM];
     int queues, i;
+    NetFilterState *nf, *next;
 
     assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
 
@@ -395,6 +398,10 @@ void qemu_del_net_client(NetClientState *nc)
                                           MAX_QUEUE_NUM);
     assert(queues != 0);
 
+    QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
+        object_unparent(OBJECT(nf));
+    }
+
     /* If there is a peer NIC, delete and cleanup client, but do not free. */
     if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
         NICState *nic = qemu_get_nic(nc->peer);
qapi-schema.json +1 -0
index 2bada60..546500a 100644
@@ -2551,6 +2551,24 @@
     'opts': 'NetClientOptions' } }
 
 ##
+# @NetFilterChain

qapi-schema.json line 2564

Quoted 10 lines
qapi-schema.json +10 -0
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.

Uh, inhowfar is this a "chain"? As far as I can tell, it specifies whether a netfilter is attached to the transmit queue, the receive queue, or both.

Quoted 15 lines
qapi-schema.json +7 -0
+#
+# Since 2.5
+##
+{ 'enum': 'NetFilterChain',
+  'data': [ 'all', 'in', 'out' ] }
+
+##
 # @InetSocketAddress
 #
 # Captures a socket address or address range in the Internet namespace.
vl.c +0 -0
index 066a080..ec589e2 100644

vl.c static bool object_create_initial(const char *type) · line 2797

Quoted 4 lines
vl.c +1 -0
@@ -2794,6 +2794,7 @@ static bool object_create_initial(const char *type)
     if (g_str_equal(type, "rng-egd")) {
         return false;
     }
+    /* TODO: return false for concrete netfilters */

This confused me briefly, until I realized the context. You mean something like

    /* TODO implement netfilters */
Quoted 19 lines
vl.c +0 -6
     return true;
 }
 
@@ -4313,12 +4314,6 @@ int main(int argc, char **argv, char **envp)
         exit(0);
     }
 
-    if (qemu_opts_foreach(qemu_find_opts("object"),
-                          object_create,
-                          object_create_delayed, NULL)) {
-        exit(1);
-    }
-
     machine_opts = qemu_get_machine_opts();
     if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
                          NULL)) {
@@ -4424,6 +4419,12 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }

vl.c int main(int argc, char **argv, char **envp) · line 4430

Quoted 10 lines
vl.c +6 -0
 
+    if (qemu_opts_foreach(qemu_find_opts("object"),
+                          object_create,
+                          object_create_delayed, NULL)) {
+        exit(1);
+    }
+
 #ifdef CONFIG_TPM
     if (tpm_init() < 0) {
         exit(1);

I'd make this code motion a separate patch, to make it more prominent in git-log.

The rest of the patch is harmless: it creates a new abstact class.

The code motion isn't: it actually changes behavior.

Overall, patch is neatly done.

#18 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 101 lines · Markus Armbruster
Quoted 9 lines · Yang Hongyang

Add a netfilter object based on QOM.

A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless.

QTAILQ_ENTRY next used by netdev, filter belongs to the specific netdev is in this queue.

I don't get this paragraph. Not sure it's needed.

Quoted 2 lines

Also init delayed object after net_init_clients, because netfilters need to be initialized after net clients initialized.

A paragraph starting with "Also" in a commit message is a pretty good sign the patch should be split :)

Quoted 68 lines

---
v11: no need to free nf->netdev_id, it will be auto freeed while object deleted
remove global_list net_filters, will add back when needed
v10: use QOM for netfilter
v9: use flat union instead of simple union in QAPI schema
v8: include vhost_net header
v7: add check for vhost
fix error propagate bug
v6: add multiqueue support (net_filter_init1)
v5: remove model from NetFilterState
add a sent_cb param to receive_iov API

---
  include/net/filter.h    |  60 +++++++++++++++++++++
  include/net/net.h       |   1 +
  include/qemu/typedefs.h |   1 +
  net/Makefile.objs       |   1 +
  net/filter.c            | 138 ++++++++++++++++++++++++++++++++++++++++++++++++
  net/net.c               |   7 +++
  qapi-schema.json        |  18 +++++++
  vl.c                    |  13 ++---
  8 files changed, 233 insertions(+), 6 deletions(-)

create mode 100644 include/net/filter.h
create mode 100644 net/filter.c

include/net/filter.h new file +36 -0
new file mode 100644
index 0000000..226f2f7
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTER_H
+#define QEMU_NET_FILTER_H
+
+#include "qom/object.h"
+#include "qemu-common.h"
+#include "qemu/typedefs.h"
+#include "net/queue.h"
+
+#define TYPE_NETFILTER "netfilter"
+#define NETFILTER(obj) \
+    OBJECT_CHECK(NetFilterState, (obj), TYPE_NETFILTER)
+#define NETFILTER_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(NetFilterClass, (obj), TYPE_NETFILTER)
+#define NETFILTER_CLASS(klass) \
+    OBJECT_CLASS_CHECK(NetFilterClass, (klass), TYPE_NETFILTER)
+
+typedef void (FilterSetup) (NetFilterState *nf, Error **errp);
+typedef void (FilterCleanup) (NetFilterState *nf);
+/*
+ * Return:
+ *   0: finished handling the packet, we should continue
+ *   size: filter stolen this packet, we stop pass this packet further
+ */
+typedef ssize_t (FilterReceiveIOV)(NetFilterState *nc,
+                                   NetClientState *sender,
+                                   unsigned flags,
+                                   const struct iovec *iov,
+                                   int iovcnt,

include/net/filter.h line 46

Quoted 10 lines
include/net/filter.h new file +10 -0
+                                   NetPacketSent *sent_cb);
+
+struct NetFilterClass {
+    ObjectClass parent_class;
+
+    FilterSetup *setup;
+    FilterCleanup *cleanup;
+    FilterReceiveIOV *receive_iov;
+};
+typedef struct NetFilterClass NetFilterClass;

Not splitting the declaration is more concise:

Quoted 8 lines
     typedef struct {
         ObjectClass parent_class;
         FilterSetup *setup;
         FilterCleanup *cleanup;
         FilterReceiveIOV *receive_iov;
     } NetFilterClass;

Are any of the methods optional? If yes, please add suitable comments.

Hi Markus, I split it because the checkpatch.pl told me to do so...

Quoted 348 lines
Quoted 104 lines
include/net/filter.h new file +14 -0
+
+
+struct NetFilterState {
+    /* private */
+    Object parent;
+
+    /* protected */
+    char *netdev_id;
+    NetClientState *netdev;
+    NetFilterChain chain;
+    QTAILQ_ENTRY(NetFilterState) next;
+};
+
+#endif /* QEMU_NET_FILTER_H */
include/net/net.h +1 -0
index 6a6cbef..36e5fab 100644
@@ -92,6 +92,7 @@ struct NetClientState {
      NetClientDestructor *destructor;
      unsigned int queue_index;
      unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, NetFilterState) filters;
  };
 
  typedef struct NICState {
include/qemu/typedefs.h +1 -0
index f8a9dd6..2c0648f 100644
@@ -45,6 +45,7 @@ typedef struct Monitor Monitor;
  typedef struct MouseTransformInfo MouseTransformInfo;
  typedef struct MSIMessage MSIMessage;
  typedef struct NetClientState NetClientState;
+typedef struct NetFilterState NetFilterState;
  typedef struct NICInfo NICInfo;
  typedef struct PcGuestInfo PcGuestInfo;
  typedef struct PCIBridge PCIBridge;
net/Makefile.objs +1 -0
index ec19cb3..914aec0 100644
@@ -13,3 +13,4 @@ common-obj-$(CONFIG_HAIKU) += tap-haiku.o
  common-obj-$(CONFIG_SLIRP) += slirp.o
  common-obj-$(CONFIG_VDE) += vde.o
  common-obj-$(CONFIG_NETMAP) += netmap.o
+common-obj-y += filter.o
net/filter.c new file +50 -0
new file mode 100644
index 0000000..3b810c8
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#include "qemu-common.h"
+#include "qapi/qmp/qerror.h"
+#include "qemu/error-report.h"
+
+#include "net/filter.h"
+#include "net/net.h"
+#include "net/vhost_net.h"
+#include "qom/object_interfaces.h"
+
+static char *netfilter_get_netdev_id(Object *obj, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    return g_strdup(nf->netdev_id);
+}
+
+static void netfilter_set_netdev_id(Object *obj, const char *str, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    nf->netdev_id = g_strdup(str);
+}
+
+static int netfilter_get_chain(Object *obj, Error **errp G_GNUC_UNUSED)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    return nf->chain;
+}
+
+static void netfilter_set_chain(Object *obj, int chain, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    nf->chain = chain;
+}
+
+static void netfilter_init(Object *obj)
+{
+    object_property_add_str(obj, "netdev",
+                            netfilter_get_netdev_id, netfilter_set_netdev_id,
+                            NULL);
+    object_property_add_enum(obj, "chain", "NetFilterChain",
+                             NetFilterChain_lookup,

net/filter.c line 60

Quoted 10 lines
net/filter.c new file +10 -0
+                             netfilter_get_chain, netfilter_set_chain,
+                             NULL);
+}
+
+static void netfilter_finalize(Object *obj)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(obj);
+
+    if (nfc->cleanup) {

Is ->cleanup optional?

net/filter.c line 64

Quoted 4 lines
net/filter.c new file +4 -0
+        nfc->cleanup(nf);
+    }
+
+    if (nf->netdev && !QTAILQ_EMPTY(&nf->netdev->filters)) {

How can nf->netdev be null?

How can nf->netdev->filters be empty?

net/filter.c line 67

Quoted 3 lines
net/filter.c new file +3 -0
+        QTAILQ_REMOVE(&nf->netdev->filters, nf, next);
+    }
+}

Putting netfilter_finalize() after netfilter_complete() would be easier to understand, because then destruction follows creation.

Quoted 13 lines
net/filter.c new file +13 -0
+
+static void netfilter_complete(UserCreatable *uc, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(uc);
+    NetClientState *ncs[MAX_QUEUE_NUM];
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
+    int queues;
+    Error *local_err = NULL;
+
+    if (!nf->netdev_id) {
+        error_setg(errp, "Parameter 'netdev' is required");
+        return;
+    }

net/filter.c line 90

Quoted 10 lines
net/filter.c new file +10 -0
+
+    queues = qemu_find_net_clients_except(nf->netdev_id, ncs,
+                                          NET_CLIENT_OPTIONS_KIND_NIC,
+                                          MAX_QUEUE_NUM);
+    if (queues < 1) {
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
+                   "a network backend id");
+        return;
+    } else if (queues > 1) {
+        error_setg(errp, "Multi queue is not supported");

We spell this multiqueue elsewhere.

Since you're only interested in a single queue, you could save stack space by making ncs[] just one element large. Not worth it if you intend to support multiqueue soon.

net/filter.c line 101

Quoted 11 lines
net/filter.c new file +11 -0
+        return;
+    }
+
+    if (get_vhost_net(ncs[0])) {
+        error_setg(errp, "Vhost is not supported");
+        return;
+    }
+
+    nf->netdev = ncs[0];
+
+    if (nfc->setup) {

Is ->setup optional?

net/filter.c line 108

Quoted 7 lines
net/filter.c new file +7 -0
+        nfc->setup(nf, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
+    }
+    QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);

I think I'd create netdev functions to add and remove filters, so the filter code doesn't have to modify the netdev state. Right now, NetClientState member filters is initialized in net.c, but modified here. But I'm not the net maintainer :)

Quoted 78 lines
net/filter.c new file +30 -0
+}
+
+static void netfilter_class_init(ObjectClass *oc, void *data)
+{
+    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+
+    ucc->complete = netfilter_complete;
+}
+
+static const TypeInfo netfilter_info = {
+    .name = TYPE_NETFILTER,
+    .parent = TYPE_OBJECT,
+    .abstract = true,
+    .class_size = sizeof(NetFilterClass),
+    .class_init = netfilter_class_init,
+    .instance_size = sizeof(NetFilterState),
+    .instance_init = netfilter_init,
+    .instance_finalize = netfilter_finalize,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_USER_CREATABLE },
+        { }
+    }
+};
+
+static void register_types(void)
+{
+    type_register_static(&netfilter_info);
+}
+
+type_init(register_types);
net/net.c +7 -0
index 28a5597..033f4f3 100644
@@ -44,6 +44,7 @@
  #include "qapi/opts-visitor.h"
  #include "qapi/dealloc-visitor.h"
  #include "sysemu/sysemu.h"
+#include "net/filter.h"
 
  /* Net bridge is currently not supported for W32. */
  #if !defined(_WIN32)
@@ -287,6 +288,7 @@ static void qemu_net_client_setup(NetClientState *nc,
 
      nc->incoming_queue = qemu_new_net_queue(nc);
      nc->destructor = destructor;
+    QTAILQ_INIT(&nc->filters);
  }
 
  NetClientState *qemu_new_net_client(NetClientInfo *info,
@@ -384,6 +386,7 @@ void qemu_del_net_client(NetClientState *nc)
  {
      NetClientState *ncs[MAX_QUEUE_NUM];
      int queues, i;
+    NetFilterState *nf, *next;
 
      assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
 
@@ -395,6 +398,10 @@ void qemu_del_net_client(NetClientState *nc)
                                            MAX_QUEUE_NUM);
      assert(queues != 0);
 
+    QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
+        object_unparent(OBJECT(nf));
+    }
+
      /* If there is a peer NIC, delete and cleanup client, but do not free. */
      if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
          NICState *nic = qemu_get_nic(nc->peer);
qapi-schema.json +1 -0
index 2bada60..546500a 100644
@@ -2551,6 +2551,24 @@
      'opts': 'NetClientOptions' } }
 
  ##
+# @NetFilterChain

qapi-schema.json line 2564

Quoted 10 lines
qapi-schema.json +10 -0
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.

Uh, inhowfar is this a "chain"? As far as I can tell, it specifies whether a netfilter is attached to the transmit queue, the receive queue, or both.

Quoted 15 lines
qapi-schema.json +7 -0
+#
+# Since 2.5
+##
+{ 'enum': 'NetFilterChain',
+  'data': [ 'all', 'in', 'out' ] }
+
+##
  # @InetSocketAddress
  #
  # Captures a socket address or address range in the Internet namespace.
vl.c +0 -0
index 066a080..ec589e2 100644

vl.c static bool object_create_initial(const char *type) · line 2797

Quoted 4 lines
vl.c +1 -0
@@ -2794,6 +2794,7 @@ static bool object_create_initial(const char *type)
      if (g_str_equal(type, "rng-egd")) {
          return false;
      }
+    /* TODO: return false for concrete netfilters */

This confused me briefly, until I realized the context. You mean something like

     /* TODO implement netfilters */
Quoted 19 lines
vl.c +0 -6
      return true;
  }
 
@@ -4313,12 +4314,6 @@ int main(int argc, char **argv, char **envp)
          exit(0);
      }
 
-    if (qemu_opts_foreach(qemu_find_opts("object"),
-                          object_create,
-                          object_create_delayed, NULL)) {
-        exit(1);
-    }
-
      machine_opts = qemu_get_machine_opts();
      if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
                           NULL)) {
@@ -4424,6 +4419,12 @@ int main(int argc, char **argv, char **envp)
          exit(1);
      }

vl.c int main(int argc, char **argv, char **envp) · line 4430

Quoted 10 lines
vl.c +6 -0
 
+    if (qemu_opts_foreach(qemu_find_opts("object"),
+                          object_create,
+                          object_create_delayed, NULL)) {
+        exit(1);
+    }
+
  #ifdef CONFIG_TPM
      if (tpm_init() < 0) {
          exit(1);

I'd make this code motion a separate patch, to make it more prominent in git-log.

The rest of the patch is harmless: it creates a new abstact class.

The code motion isn't: it actually changes behavior.

Overall, patch is neatly done.
.

Signature · 2 lines
Thanks,
Yang.
#19 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 109 lines · Yang Hongyang
Quoted 108 lines · Markus Armbruster
Quoted 9 lines · Yang Hongyang

Add a netfilter object based on QOM.

A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless.

QTAILQ_ENTRY next used by netdev, filter belongs to the specific netdev is in this queue.

I don't get this paragraph. Not sure it's needed.

Quoted 2 lines

Also init delayed object after net_init_clients, because netfilters need to be initialized after net clients initialized.

A paragraph starting with "Also" in a commit message is a pretty good sign the patch should be split :)

Quoted 68 lines

---
v11: no need to free nf->netdev_id, it will be auto freeed while object deleted
remove global_list net_filters, will add back when needed
v10: use QOM for netfilter
v9: use flat union instead of simple union in QAPI schema
v8: include vhost_net header
v7: add check for vhost
fix error propagate bug
v6: add multiqueue support (net_filter_init1)
v5: remove model from NetFilterState
add a sent_cb param to receive_iov API

---
  include/net/filter.h    |  60 +++++++++++++++++++++
  include/net/net.h       |   1 +
  include/qemu/typedefs.h |   1 +
  net/Makefile.objs       |   1 +
  net/filter.c            | 138 ++++++++++++++++++++++++++++++++++++++++++++++++
  net/net.c               |   7 +++
  qapi-schema.json        |  18 +++++++
  vl.c                    |  13 ++---
  8 files changed, 233 insertions(+), 6 deletions(-)

create mode 100644 include/net/filter.h
create mode 100644 net/filter.c

include/net/filter.h new file +36 -0
new file mode 100644
index 0000000..226f2f7
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTER_H
+#define QEMU_NET_FILTER_H
+
+#include "qom/object.h"
+#include "qemu-common.h"
+#include "qemu/typedefs.h"
+#include "net/queue.h"
+
+#define TYPE_NETFILTER "netfilter"
+#define NETFILTER(obj) \
+    OBJECT_CHECK(NetFilterState, (obj), TYPE_NETFILTER)
+#define NETFILTER_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(NetFilterClass, (obj), TYPE_NETFILTER)
+#define NETFILTER_CLASS(klass) \
+    OBJECT_CLASS_CHECK(NetFilterClass, (klass), TYPE_NETFILTER)
+
+typedef void (FilterSetup) (NetFilterState *nf, Error **errp);
+typedef void (FilterCleanup) (NetFilterState *nf);
+/*
+ * Return:
+ *   0: finished handling the packet, we should continue
+ *   size: filter stolen this packet, we stop pass this packet further
+ */
+typedef ssize_t (FilterReceiveIOV)(NetFilterState *nc,
+                                   NetClientState *sender,
+                                   unsigned flags,
+                                   const struct iovec *iov,
+                                   int iovcnt,

include/net/filter.h line 46

Quoted 10 lines
include/net/filter.h new file +10 -0
+                                   NetPacketSent *sent_cb);
+
+struct NetFilterClass {
+    ObjectClass parent_class;
+
+    FilterSetup *setup;
+    FilterCleanup *cleanup;
+    FilterReceiveIOV *receive_iov;
+};
+typedef struct NetFilterClass NetFilterClass;

Not splitting the declaration is more concise:

     typedef struct {
         ObjectClass parent_class;
         FilterSetup *setup;
         FilterCleanup *cleanup;
         FilterReceiveIOV *receive_iov;
     } NetFilterClass;
Quoted 3 lines
Quoted 1 line

Are any of the methods optional? If yes, please add suitable comments.

Hi Markus, I split it because the checkpatch.pl told me to do so...

Understand. However, it's a recent change to checkpatch.pl that's going to be reverted:
Message-ID: <[email protected]>
http://lists.gnu.org/archive/html/qemu-devel/2015-09/msg04644.html

[...]

#20 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 111 lines · Markus Armbruster
Quoted 109 lines · Yang Hongyang
Quoted 108 lines · Markus Armbruster
Quoted 9 lines · Yang Hongyang

Add a netfilter object based on QOM.

A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless.

QTAILQ_ENTRY next used by netdev, filter belongs to the specific netdev is in this queue.

I don't get this paragraph. Not sure it's needed.

Quoted 2 lines

Also init delayed object after net_init_clients, because netfilters need to be initialized after net clients initialized.

A paragraph starting with "Also" in a commit message is a pretty good sign the patch should be split :)

Quoted 68 lines

---
v11: no need to free nf->netdev_id, it will be auto freeed while object deleted
remove global_list net_filters, will add back when needed
v10: use QOM for netfilter
v9: use flat union instead of simple union in QAPI schema
v8: include vhost_net header
v7: add check for vhost
fix error propagate bug
v6: add multiqueue support (net_filter_init1)
v5: remove model from NetFilterState
add a sent_cb param to receive_iov API

---
   include/net/filter.h    |  60 +++++++++++++++++++++
   include/net/net.h       |   1 +
   include/qemu/typedefs.h |   1 +
   net/Makefile.objs       |   1 +
   net/filter.c            | 138 ++++++++++++++++++++++++++++++++++++++++++++++++
   net/net.c               |   7 +++
   qapi-schema.json        |  18 +++++++
   vl.c                    |  13 ++---
   8 files changed, 233 insertions(+), 6 deletions(-)

create mode 100644 include/net/filter.h
create mode 100644 net/filter.c

include/net/filter.h new file +36 -0
new file mode 100644
index 0000000..226f2f7
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTER_H
+#define QEMU_NET_FILTER_H
+
+#include "qom/object.h"
+#include "qemu-common.h"
+#include "qemu/typedefs.h"
+#include "net/queue.h"
+
+#define TYPE_NETFILTER "netfilter"
+#define NETFILTER(obj) \
+    OBJECT_CHECK(NetFilterState, (obj), TYPE_NETFILTER)
+#define NETFILTER_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(NetFilterClass, (obj), TYPE_NETFILTER)
+#define NETFILTER_CLASS(klass) \
+    OBJECT_CLASS_CHECK(NetFilterClass, (klass), TYPE_NETFILTER)
+
+typedef void (FilterSetup) (NetFilterState *nf, Error **errp);
+typedef void (FilterCleanup) (NetFilterState *nf);
+/*
+ * Return:
+ *   0: finished handling the packet, we should continue
+ *   size: filter stolen this packet, we stop pass this packet further
+ */
+typedef ssize_t (FilterReceiveIOV)(NetFilterState *nc,
+                                   NetClientState *sender,
+                                   unsigned flags,
+                                   const struct iovec *iov,
+                                   int iovcnt,

include/net/filter.h line 46

Quoted 10 lines
include/net/filter.h new file +10 -0
+                                   NetPacketSent *sent_cb);
+
+struct NetFilterClass {
+    ObjectClass parent_class;
+
+    FilterSetup *setup;
+    FilterCleanup *cleanup;
+    FilterReceiveIOV *receive_iov;
+};
+typedef struct NetFilterClass NetFilterClass;

Not splitting the declaration is more concise:

      typedef struct {
          ObjectClass parent_class;
          FilterSetup *setup;
          FilterCleanup *cleanup;
          FilterReceiveIOV *receive_iov;
      } NetFilterClass;
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Are any of the methods optional? If yes, please add suitable comments.

Hi Markus, I split it because the checkpatch.pl told me to do so...

Understand. However, it's a recent change to checkpatch.pl that's going to be reverted:
Message-ID: <[email protected]>
http://lists.gnu.org/archive/html/qemu-devel/2015-09/msg04644.html

Thanks for the information.

Quoted 4 lines

[...]
.

Signature · 2 lines
Thanks,
Yang.
#21 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

In reply to Markus Armbruster
Quoted 217 lines · Markus Armbruster
Quoted 9 lines · Yang Hongyang

Add a netfilter object based on QOM.

A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless.

QTAILQ_ENTRY next used by netdev, filter belongs to the specific netdev is in this queue.

I don't get this paragraph. Not sure it's needed.

Quoted 2 lines

Also init delayed object after net_init_clients, because netfilters need to be initialized after net clients initialized.

A paragraph starting with "Also" in a commit message is a pretty good sign the patch should be split :)

Quoted 68 lines

---
v11: no need to free nf->netdev_id, it will be auto freeed while object deleted
remove global_list net_filters, will add back when needed
v10: use QOM for netfilter
v9: use flat union instead of simple union in QAPI schema
v8: include vhost_net header
v7: add check for vhost
fix error propagate bug
v6: add multiqueue support (net_filter_init1)
v5: remove model from NetFilterState
add a sent_cb param to receive_iov API

---
  include/net/filter.h    |  60 +++++++++++++++++++++
  include/net/net.h       |   1 +
  include/qemu/typedefs.h |   1 +
  net/Makefile.objs       |   1 +
  net/filter.c            | 138 ++++++++++++++++++++++++++++++++++++++++++++++++
  net/net.c               |   7 +++
  qapi-schema.json        |  18 +++++++
  vl.c                    |  13 ++---
  8 files changed, 233 insertions(+), 6 deletions(-)

create mode 100644 include/net/filter.h
create mode 100644 net/filter.c

include/net/filter.h new file +36 -0
new file mode 100644
index 0000000..226f2f7
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTER_H
+#define QEMU_NET_FILTER_H
+
+#include "qom/object.h"
+#include "qemu-common.h"
+#include "qemu/typedefs.h"
+#include "net/queue.h"
+
+#define TYPE_NETFILTER "netfilter"
+#define NETFILTER(obj) \
+    OBJECT_CHECK(NetFilterState, (obj), TYPE_NETFILTER)
+#define NETFILTER_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(NetFilterClass, (obj), TYPE_NETFILTER)
+#define NETFILTER_CLASS(klass) \
+    OBJECT_CLASS_CHECK(NetFilterClass, (klass), TYPE_NETFILTER)
+
+typedef void (FilterSetup) (NetFilterState *nf, Error **errp);
+typedef void (FilterCleanup) (NetFilterState *nf);
+/*
+ * Return:
+ *   0: finished handling the packet, we should continue
+ *   size: filter stolen this packet, we stop pass this packet further
+ */
+typedef ssize_t (FilterReceiveIOV)(NetFilterState *nc,
+                                   NetClientState *sender,
+                                   unsigned flags,
+                                   const struct iovec *iov,
+                                   int iovcnt,

include/net/filter.h line 46

Quoted 10 lines
include/net/filter.h new file +10 -0
+                                   NetPacketSent *sent_cb);
+
+struct NetFilterClass {
+    ObjectClass parent_class;
+
+    FilterSetup *setup;
+    FilterCleanup *cleanup;
+    FilterReceiveIOV *receive_iov;
+};
+typedef struct NetFilterClass NetFilterClass;

Not splitting the declaration is more concise:

     typedef struct {
         ObjectClass parent_class;
         FilterSetup *setup;
         FilterCleanup *cleanup;
         FilterReceiveIOV *receive_iov;
     } NetFilterClass;

Are any of the methods optional? If yes, please add suitable comments.

Quoted 107 lines
include/net/filter.h new file +14 -0
+
+
+struct NetFilterState {
+    /* private */
+    Object parent;
+
+    /* protected */
+    char *netdev_id;
+    NetClientState *netdev;
+    NetFilterChain chain;
+    QTAILQ_ENTRY(NetFilterState) next;
+};
+
+#endif /* QEMU_NET_FILTER_H */
include/net/net.h +1 -0
index 6a6cbef..36e5fab 100644
@@ -92,6 +92,7 @@ struct NetClientState {
      NetClientDestructor *destructor;
      unsigned int queue_index;
      unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, NetFilterState) filters;
  };
 
  typedef struct NICState {
include/qemu/typedefs.h +1 -0
index f8a9dd6..2c0648f 100644
@@ -45,6 +45,7 @@ typedef struct Monitor Monitor;
  typedef struct MouseTransformInfo MouseTransformInfo;
  typedef struct MSIMessage MSIMessage;
  typedef struct NetClientState NetClientState;
+typedef struct NetFilterState NetFilterState;
  typedef struct NICInfo NICInfo;
  typedef struct PcGuestInfo PcGuestInfo;
  typedef struct PCIBridge PCIBridge;
net/Makefile.objs +1 -0
index ec19cb3..914aec0 100644
@@ -13,3 +13,4 @@ common-obj-$(CONFIG_HAIKU) += tap-haiku.o
  common-obj-$(CONFIG_SLIRP) += slirp.o
  common-obj-$(CONFIG_VDE) += vde.o
  common-obj-$(CONFIG_NETMAP) += netmap.o
+common-obj-y += filter.o
net/filter.c new file +54 -0
new file mode 100644
index 0000000..3b810c8
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#include "qemu-common.h"
+#include "qapi/qmp/qerror.h"
+#include "qemu/error-report.h"
+
+#include "net/filter.h"
+#include "net/net.h"
+#include "net/vhost_net.h"
+#include "qom/object_interfaces.h"
+
+static char *netfilter_get_netdev_id(Object *obj, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    return g_strdup(nf->netdev_id);
+}
+
+static void netfilter_set_netdev_id(Object *obj, const char *str, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    nf->netdev_id = g_strdup(str);
+}
+
+static int netfilter_get_chain(Object *obj, Error **errp G_GNUC_UNUSED)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    return nf->chain;
+}
+
+static void netfilter_set_chain(Object *obj, int chain, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    nf->chain = chain;
+}
+
+static void netfilter_init(Object *obj)
+{
+    object_property_add_str(obj, "netdev",
+                            netfilter_get_netdev_id, netfilter_set_netdev_id,
+                            NULL);
+    object_property_add_enum(obj, "chain", "NetFilterChain",
+                             NetFilterChain_lookup,
+                             netfilter_get_chain, netfilter_set_chain,
+                             NULL);
+}
+
Quoted 8 lines

net/filter.c line 60

Quoted 6 lines
net/filter.c new file +6 -0
+static void netfilter_finalize(Object *obj)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(obj);
+
+    if (nfc->cleanup) {

Is ->cleanup optional?

Yes, it's optional, as well as setup, if a concrete filter do not have things to setup/cleanup.

Quoted 9 lines

net/filter.c line 64

Quoted 5 lines
net/filter.c new file +4 -0
+        nfc->cleanup(nf);
+    }
+
+    if (nf->netdev && !QTAILQ_EMPTY(&nf->netdev->filters)) {

How can nf->netdev be null?

How can nf->netdev->filters be empty?

When object initialize failed. for example, in netfilter_complete no nf->netdev_id provided, object initialize will fail, and the netfilter_finalize will be called, at the mean time, nf->netdev is null, and nf->netdev->filters is empty.

Quoted 34 lines

net/filter.c line 67

Quoted 4 lines
net/filter.c new file +3 -0
+        QTAILQ_REMOVE(&nf->netdev->filters, nf, next);
+    }
+}

Putting netfilter_finalize() after netfilter_complete() would be easier to understand, because then destruction follows creation.

Quoted 13 lines
net/filter.c new file +13 -0
+
+static void netfilter_complete(UserCreatable *uc, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(uc);
+    NetClientState *ncs[MAX_QUEUE_NUM];
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
+    int queues;
+    Error *local_err = NULL;
+
+    if (!nf->netdev_id) {
+        error_setg(errp, "Parameter 'netdev' is required");
+        return;
+    }

net/filter.c line 90

Quoted 10 lines
net/filter.c new file +10 -0
+
+    queues = qemu_find_net_clients_except(nf->netdev_id, ncs,
+                                          NET_CLIENT_OPTIONS_KIND_NIC,
+                                          MAX_QUEUE_NUM);
+    if (queues < 1) {
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
+                   "a network backend id");
+        return;
+    } else if (queues > 1) {
+        error_setg(errp, "Multi queue is not supported");

We spell this multiqueue elsewhere.

Quoted 3 lines

Since you're only interested in a single queue, you could save stack space by making ncs[] just one element large. Not worth it if you intend to support multiqueue soon.

We will support multiqueue soon.

Quoted 14 lines

net/filter.c line 101

Quoted 12 lines
net/filter.c new file +11 -0
+        return;
+    }
+
+    if (get_vhost_net(ncs[0])) {
+        error_setg(errp, "Vhost is not supported");
+        return;
+    }
+
+    nf->netdev = ncs[0];
+
+    if (nfc->setup) {

Is ->setup optional?

Yes, as said earlier.

Quoted 90 lines

net/filter.c line 108

Quoted 8 lines
net/filter.c new file +7 -0
+        nfc->setup(nf, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
+    }
+    QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);

I think I'd create netdev functions to add and remove filters, so the filter code doesn't have to modify the netdev state. Right now, NetClientState member filters is initialized in net.c, but modified here. But I'm not the net maintainer :)

Quoted 76 lines
net/filter.c new file +30 -0
+}
+
+static void netfilter_class_init(ObjectClass *oc, void *data)
+{
+    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+
+    ucc->complete = netfilter_complete;
+}
+
+static const TypeInfo netfilter_info = {
+    .name = TYPE_NETFILTER,
+    .parent = TYPE_OBJECT,
+    .abstract = true,
+    .class_size = sizeof(NetFilterClass),
+    .class_init = netfilter_class_init,
+    .instance_size = sizeof(NetFilterState),
+    .instance_init = netfilter_init,
+    .instance_finalize = netfilter_finalize,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_USER_CREATABLE },
+        { }
+    }
+};
+
+static void register_types(void)
+{
+    type_register_static(&netfilter_info);
+}
+
+type_init(register_types);
net/net.c +7 -0
index 28a5597..033f4f3 100644
@@ -44,6 +44,7 @@
  #include "qapi/opts-visitor.h"
  #include "qapi/dealloc-visitor.h"
  #include "sysemu/sysemu.h"
+#include "net/filter.h"
 
  /* Net bridge is currently not supported for W32. */
  #if !defined(_WIN32)
@@ -287,6 +288,7 @@ static void qemu_net_client_setup(NetClientState *nc,
 
      nc->incoming_queue = qemu_new_net_queue(nc);
      nc->destructor = destructor;
+    QTAILQ_INIT(&nc->filters);
  }
 
  NetClientState *qemu_new_net_client(NetClientInfo *info,
@@ -384,6 +386,7 @@ void qemu_del_net_client(NetClientState *nc)
  {
      NetClientState *ncs[MAX_QUEUE_NUM];
      int queues, i;
+    NetFilterState *nf, *next;
 
      assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
 
@@ -395,6 +398,10 @@ void qemu_del_net_client(NetClientState *nc)
                                            MAX_QUEUE_NUM);
      assert(queues != 0);
 
+    QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
+        object_unparent(OBJECT(nf));
+    }
+
      /* If there is a peer NIC, delete and cleanup client, but do not free. */
      if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
          NICState *nic = qemu_get_nic(nc->peer);
qapi-schema.json +0 -0
index 2bada60..546500a 100644
@@ -2551,6 +2551,24 @@
      'opts': 'NetClientOptions' } }
 
Quoted 16 lines

qapi-schema.json line 2564

Quoted 12 lines
qapi-schema.json +11 -0
  ##
+# @NetFilterChain
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.

Uh, inhowfar is this a "chain"? As far as I can tell, it specifies whether a netfilter is attached to the transmit queue, the receive queue, or both.

netback's input chain or output chain, queue is mostly like a word that related to the code implementation? English is not my first language, so I'm not sure about this.

Quoted 15 lines
Quoted 15 lines
qapi-schema.json +7 -0
+#
+# Since 2.5
+##
+{ 'enum': 'NetFilterChain',
+  'data': [ 'all', 'in', 'out' ] }
+
+##
  # @InetSocketAddress
  #
  # Captures a socket address or address range in the Internet namespace.
vl.c +0 -0
index 066a080..ec589e2 100644
Quoted 8 lines

vl.c static bool object_create_initial(const char *type) · line 2797

Quoted 5 lines
vl.c +1 -0
@@ -2794,6 +2794,7 @@ static bool object_create_initial(const char *type)
      if (g_str_equal(type, "rng-egd")) {
          return false;
      }
+    /* TODO: return false for concrete netfilters */

This confused me briefly, until I realized the context. You mean something like

Ah, I'm sorry for the confusing...

Quoted 43 lines
     /* TODO implement netfilters */
Quoted 19 lines
vl.c +0 -6
      return true;
  }
 
@@ -4313,12 +4314,6 @@ int main(int argc, char **argv, char **envp)
          exit(0);
      }
 
-    if (qemu_opts_foreach(qemu_find_opts("object"),
-                          object_create,
-                          object_create_delayed, NULL)) {
-        exit(1);
-    }
-
      machine_opts = qemu_get_machine_opts();
      if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
                           NULL)) {
@@ -4424,6 +4419,12 @@ int main(int argc, char **argv, char **envp)
          exit(1);
      }

vl.c int main(int argc, char **argv, char **envp) · line 4430

Quoted 10 lines
vl.c +6 -0
 
+    if (qemu_opts_foreach(qemu_find_opts("object"),
+                          object_create,
+                          object_create_delayed, NULL)) {
+        exit(1);
+    }
+
  #ifdef CONFIG_TPM
      if (tpm_init() < 0) {
          exit(1);

I'd make this code motion a separate patch, to make it more prominent in git-log.

The rest of the patch is harmless: it creates a new abstact class.

The code motion isn't: it actually changes behavior.

Overall, patch is neatly done.
.

Signature · 2 lines
Thanks,
Yang.
#22 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 227 lines · Yang Hongyang
Quoted 217 lines · Markus Armbruster
Quoted 9 lines · Yang Hongyang

Add a netfilter object based on QOM.

A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless.

QTAILQ_ENTRY next used by netdev, filter belongs to the specific netdev is in this queue.

I don't get this paragraph. Not sure it's needed.

Quoted 2 lines

Also init delayed object after net_init_clients, because netfilters need to be initialized after net clients initialized.

A paragraph starting with "Also" in a commit message is a pretty good sign the patch should be split :)

Quoted 68 lines

---
v11: no need to free nf->netdev_id, it will be auto freeed while object deleted
remove global_list net_filters, will add back when needed
v10: use QOM for netfilter
v9: use flat union instead of simple union in QAPI schema
v8: include vhost_net header
v7: add check for vhost
fix error propagate bug
v6: add multiqueue support (net_filter_init1)
v5: remove model from NetFilterState
add a sent_cb param to receive_iov API

---
  include/net/filter.h    |  60 +++++++++++++++++++++
  include/net/net.h       |   1 +
  include/qemu/typedefs.h |   1 +
  net/Makefile.objs       |   1 +
  net/filter.c            | 138 ++++++++++++++++++++++++++++++++++++++++++++++++
  net/net.c               |   7 +++
  qapi-schema.json        |  18 +++++++
  vl.c                    |  13 ++---
  8 files changed, 233 insertions(+), 6 deletions(-)

create mode 100644 include/net/filter.h
create mode 100644 net/filter.c

include/net/filter.h new file +36 -0
new file mode 100644
index 0000000..226f2f7
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTER_H
+#define QEMU_NET_FILTER_H
+
+#include "qom/object.h"
+#include "qemu-common.h"
+#include "qemu/typedefs.h"
+#include "net/queue.h"
+
+#define TYPE_NETFILTER "netfilter"
+#define NETFILTER(obj) \
+    OBJECT_CHECK(NetFilterState, (obj), TYPE_NETFILTER)
+#define NETFILTER_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(NetFilterClass, (obj), TYPE_NETFILTER)
+#define NETFILTER_CLASS(klass) \
+    OBJECT_CLASS_CHECK(NetFilterClass, (klass), TYPE_NETFILTER)
+
+typedef void (FilterSetup) (NetFilterState *nf, Error **errp);
+typedef void (FilterCleanup) (NetFilterState *nf);
+/*
+ * Return:
+ *   0: finished handling the packet, we should continue
+ *   size: filter stolen this packet, we stop pass this packet further
+ */
+typedef ssize_t (FilterReceiveIOV)(NetFilterState *nc,
+                                   NetClientState *sender,
+                                   unsigned flags,
+                                   const struct iovec *iov,
+                                   int iovcnt,

include/net/filter.h line 46

Quoted 10 lines
include/net/filter.h new file +10 -0
+                                   NetPacketSent *sent_cb);
+
+struct NetFilterClass {
+    ObjectClass parent_class;
+
+    FilterSetup *setup;
+    FilterCleanup *cleanup;
+    FilterReceiveIOV *receive_iov;
+};
+typedef struct NetFilterClass NetFilterClass;

Not splitting the declaration is more concise:

     typedef struct {
         ObjectClass parent_class;
         FilterSetup *setup;
         FilterCleanup *cleanup;
         FilterReceiveIOV *receive_iov;
     } NetFilterClass;

Are any of the methods optional? If yes, please add suitable comments.

Quoted 107 lines
include/net/filter.h new file +14 -0
+
+
+struct NetFilterState {
+    /* private */
+    Object parent;
+
+    /* protected */
+    char *netdev_id;
+    NetClientState *netdev;
+    NetFilterChain chain;
+    QTAILQ_ENTRY(NetFilterState) next;
+};
+
+#endif /* QEMU_NET_FILTER_H */
include/net/net.h +1 -0
index 6a6cbef..36e5fab 100644
@@ -92,6 +92,7 @@ struct NetClientState {
      NetClientDestructor *destructor;
      unsigned int queue_index;
      unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, NetFilterState) filters;
  };
 
  typedef struct NICState {
include/qemu/typedefs.h +1 -0
index f8a9dd6..2c0648f 100644
@@ -45,6 +45,7 @@ typedef struct Monitor Monitor;
  typedef struct MouseTransformInfo MouseTransformInfo;
  typedef struct MSIMessage MSIMessage;
  typedef struct NetClientState NetClientState;
+typedef struct NetFilterState NetFilterState;
  typedef struct NICInfo NICInfo;
  typedef struct PcGuestInfo PcGuestInfo;
  typedef struct PCIBridge PCIBridge;
net/Makefile.objs +1 -0
index ec19cb3..914aec0 100644
@@ -13,3 +13,4 @@ common-obj-$(CONFIG_HAIKU) += tap-haiku.o
  common-obj-$(CONFIG_SLIRP) += slirp.o
  common-obj-$(CONFIG_VDE) += vde.o
  common-obj-$(CONFIG_NETMAP) += netmap.o
+common-obj-y += filter.o
net/filter.c new file +54 -0
new file mode 100644
index 0000000..3b810c8
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#include "qemu-common.h"
+#include "qapi/qmp/qerror.h"
+#include "qemu/error-report.h"
+
+#include "net/filter.h"
+#include "net/net.h"
+#include "net/vhost_net.h"
+#include "qom/object_interfaces.h"
+
+static char *netfilter_get_netdev_id(Object *obj, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    return g_strdup(nf->netdev_id);
+}
+
+static void netfilter_set_netdev_id(Object *obj, const char *str, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    nf->netdev_id = g_strdup(str);
+}
+
+static int netfilter_get_chain(Object *obj, Error **errp G_GNUC_UNUSED)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    return nf->chain;
+}
+
+static void netfilter_set_chain(Object *obj, int chain, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    nf->chain = chain;
+}
+
+static void netfilter_init(Object *obj)
+{
+    object_property_add_str(obj, "netdev",
+                            netfilter_get_netdev_id, netfilter_set_netdev_id,
+                            NULL);
+    object_property_add_enum(obj, "chain", "NetFilterChain",
+                             NetFilterChain_lookup,
+                             netfilter_get_chain, netfilter_set_chain,
+                             NULL);
+}
+
Quoted 8 lines

net/filter.c line 60

Quoted 6 lines
net/filter.c new file +6 -0
+static void netfilter_finalize(Object *obj)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(obj);
+
+    if (nfc->cleanup) {

Is ->cleanup optional?

Quoted 2 lines

Yes, it's optional, as well as setup, if a concrete filter do not have things to setup/cleanup.

Please document that in NetFilterClass.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
net/filter.c new file +4 -0
+        nfc->cleanup(nf);
+    }
+
+    if (nf->netdev && !QTAILQ_EMPTY(&nf->netdev->filters)) {
Quoted 8 lines
Quoted 3 lines

How can nf->netdev be null?

How can nf->netdev->filters be empty?

When object initialize failed. for example, in netfilter_complete no nf->netdev_id provided, object initialize will fail, and the netfilter_finalize will be called, at the mean time, nf->netdev is null, and nf->netdev->filters is empty.

Okay.

Quoted 100 lines
Quoted 33 lines

net/filter.c line 67

Quoted 3 lines
net/filter.c new file +3 -0
+        QTAILQ_REMOVE(&nf->netdev->filters, nf, next);
+    }
+}

Putting netfilter_finalize() after netfilter_complete() would be easier to understand, because then destruction follows creation.

Quoted 13 lines
net/filter.c new file +13 -0
+
+static void netfilter_complete(UserCreatable *uc, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(uc);
+    NetClientState *ncs[MAX_QUEUE_NUM];
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
+    int queues;
+    Error *local_err = NULL;
+
+    if (!nf->netdev_id) {
+        error_setg(errp, "Parameter 'netdev' is required");
+        return;
+    }

net/filter.c line 90

Quoted 10 lines
net/filter.c new file +10 -0
+
+    queues = qemu_find_net_clients_except(nf->netdev_id, ncs,
+                                          NET_CLIENT_OPTIONS_KIND_NIC,
+                                          MAX_QUEUE_NUM);
+    if (queues < 1) {
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
+                   "a network backend id");
+        return;
+    } else if (queues > 1) {
+        error_setg(errp, "Multi queue is not supported");

We spell this multiqueue elsewhere.

Quoted 3 lines

Since you're only interested in a single queue, you could save stack space by making ncs[] just one element large. Not worth it if you intend to support multiqueue soon.

We will support multiqueue soon.

Quoted 14 lines

net/filter.c line 101

Quoted 12 lines
net/filter.c new file +11 -0
+        return;
+    }
+
+    if (get_vhost_net(ncs[0])) {
+        error_setg(errp, "Vhost is not supported");
+        return;
+    }
+
+    nf->netdev = ncs[0];
+
+    if (nfc->setup) {

Is ->setup optional?

Yes, as said earlier.

Quoted 44 lines

net/filter.c line 108

Quoted 8 lines
net/filter.c new file +7 -0
+        nfc->setup(nf, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
+    }
+    QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);

I think I'd create netdev functions to add and remove filters, so the filter code doesn't have to modify the netdev state. Right now, NetClientState member filters is initialized in net.c, but modified here. But I'm not the net maintainer :)

Quoted 30 lines
net/filter.c new file +30 -0
+}
+
+static void netfilter_class_init(ObjectClass *oc, void *data)
+{
+    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+
+    ucc->complete = netfilter_complete;
+}
+
+static const TypeInfo netfilter_info = {
+    .name = TYPE_NETFILTER,
+    .parent = TYPE_OBJECT,
+    .abstract = true,
+    .class_size = sizeof(NetFilterClass),
+    .class_init = netfilter_class_init,
+    .instance_size = sizeof(NetFilterState),
+    .instance_init = netfilter_init,
+    .instance_finalize = netfilter_finalize,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_USER_CREATABLE },
+        { }
+    }
+};
+
+static void register_types(void)
+{
+    type_register_static(&netfilter_info);
+}
+
+type_init(register_types);

[...]

Quoted 24 lines
Quoted 7 lines
Quoted 7 lines
qapi-schema.json +0 -0
index 2bada60..546500a 100644
@@ -2551,6 +2551,24 @@
      'opts': 'NetClientOptions' } }
 
Quoted 16 lines

qapi-schema.json line 2564

Quoted 12 lines
qapi-schema.json +11 -0
  ##
+# @NetFilterChain
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.

Uh, inhowfar is this a "chain"? As far as I can tell, it specifies whether a netfilter is attached to the transmit queue, the receive queue, or both.

Quoted 3 lines

netback's input chain or output chain, queue is mostly like a word that related to the code implementation? English is not my first language, so I'm not sure about this.

Don't worry, we'll get the language polished together :)

In PATCH 9, I learned how this is to be used:

@item -object filter-buffer,id=@var{id},netdev=@var{netdevid}[,chain=@var{all|in|out}][,interval=@var{t}]

Buffer network packets on netdev @var{netdevid}.
[...]
chain @var{all|in|out} is an option that can be applied to any netfilter, default is @option{all}.

@option{all} means this filter will receive packets both sent to/from the netdev

@option{in} means this filter will receive packets sent to the netdev

@option{out} means this filter will receive packets sent from the netdev

In that context, "chain" makes more sense. I'd still call it "queue".
Precedence: -netdev parameter "queues". If I understand correctly, queues=N asks for N rx and tx queues. A filter would either apply to all N rx queues, all N tx queues, or all 2*N queues. Correct?

[...]

#23 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 372 lines · Markus Armbruster
Quoted 221 lines · Yang Hongyang
Quoted 212 lines · Markus Armbruster
Quoted 9 lines · Yang Hongyang

Add a netfilter object based on QOM.

A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless.

QTAILQ_ENTRY next used by netdev, filter belongs to the specific netdev is in this queue.

I don't get this paragraph. Not sure it's needed.

Quoted 2 lines

Also init delayed object after net_init_clients, because netfilters need to be initialized after net clients initialized.

A paragraph starting with "Also" in a commit message is a pretty good sign the patch should be split :)

Quoted 67 lines

---
v11: no need to free nf->netdev_id, it will be auto freeed while object deleted
remove global_list net_filters, will add back when needed
v10: use QOM for netfilter
v9: use flat union instead of simple union in QAPI schema
v8: include vhost_net header
v7: add check for vhost
fix error propagate bug
v6: add multiqueue support (net_filter_init1)
v5: remove model from NetFilterState
add a sent_cb param to receive_iov API

---
  include/net/filter.h    |  60 +++++++++++++++++++++
  include/net/net.h       |   1 +
  include/qemu/typedefs.h |   1 +
  net/Makefile.objs       |   1 +
  net/filter.c            | 138 ++++++++++++++++++++++++++++++++++++++++++++++++
  net/net.c               |   7 +++
  qapi-schema.json        |  18 +++++++
  vl.c                    |  13 ++---
  8 files changed, 233 insertions(+), 6 deletions(-)

create mode 100644 include/net/filter.h
create mode 100644 net/filter.c

include/net/filter.h new file +36 -0
new file mode 100644
index 0000000..226f2f7
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTER_H
+#define QEMU_NET_FILTER_H
+
+#include "qom/object.h"
+#include "qemu-common.h"
+#include "qemu/typedefs.h"
+#include "net/queue.h"
+
+#define TYPE_NETFILTER "netfilter"
+#define NETFILTER(obj) \
+    OBJECT_CHECK(NetFilterState, (obj), TYPE_NETFILTER)
+#define NETFILTER_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(NetFilterClass, (obj), TYPE_NETFILTER)
+#define NETFILTER_CLASS(klass) \
+    OBJECT_CLASS_CHECK(NetFilterClass, (klass), TYPE_NETFILTER)
+
+typedef void (FilterSetup) (NetFilterState *nf, Error **errp);
+typedef void (FilterCleanup) (NetFilterState *nf);
+/*
+ * Return:
+ *   0: finished handling the packet, we should continue
+ *   size: filter stolen this packet, we stop pass this packet further
+ */
+typedef ssize_t (FilterReceiveIOV)(NetFilterState *nc,
+                                   NetClientState *sender,
+                                   unsigned flags,
+                                   const struct iovec *iov,
+                                   int iovcnt,

include/net/filter.h line 46

Quoted 10 lines
include/net/filter.h new file +10 -0
+                                   NetPacketSent *sent_cb);
+
+struct NetFilterClass {
+    ObjectClass parent_class;
+
+    FilterSetup *setup;
+    FilterCleanup *cleanup;
+    FilterReceiveIOV *receive_iov;
+};
+typedef struct NetFilterClass NetFilterClass;

Not splitting the declaration is more concise:

     typedef struct {
         ObjectClass parent_class;
         FilterSetup *setup;
         FilterCleanup *cleanup;
         FilterReceiveIOV *receive_iov;
     } NetFilterClass;

Are any of the methods optional? If yes, please add suitable comments.

Quoted 106 lines
include/net/filter.h new file +14 -0
+
+
+struct NetFilterState {
+    /* private */
+    Object parent;
+
+    /* protected */
+    char *netdev_id;
+    NetClientState *netdev;
+    NetFilterChain chain;
+    QTAILQ_ENTRY(NetFilterState) next;
+};
+
+#endif /* QEMU_NET_FILTER_H */
include/net/net.h +1 -0
index 6a6cbef..36e5fab 100644
@@ -92,6 +92,7 @@ struct NetClientState {
      NetClientDestructor *destructor;
      unsigned int queue_index;
      unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, NetFilterState) filters;
  };
 
  typedef struct NICState {
include/qemu/typedefs.h +1 -0
index f8a9dd6..2c0648f 100644
@@ -45,6 +45,7 @@ typedef struct Monitor Monitor;
  typedef struct MouseTransformInfo MouseTransformInfo;
  typedef struct MSIMessage MSIMessage;
  typedef struct NetClientState NetClientState;
+typedef struct NetFilterState NetFilterState;
  typedef struct NICInfo NICInfo;
  typedef struct PcGuestInfo PcGuestInfo;
  typedef struct PCIBridge PCIBridge;
net/Makefile.objs +1 -0
index ec19cb3..914aec0 100644
@@ -13,3 +13,4 @@ common-obj-$(CONFIG_HAIKU) += tap-haiku.o
  common-obj-$(CONFIG_SLIRP) += slirp.o
  common-obj-$(CONFIG_VDE) += vde.o
  common-obj-$(CONFIG_NETMAP) += netmap.o
+common-obj-y += filter.o
net/filter.c new file +53 -0
new file mode 100644
index 0000000..3b810c8
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#include "qemu-common.h"
+#include "qapi/qmp/qerror.h"
+#include "qemu/error-report.h"
+
+#include "net/filter.h"
+#include "net/net.h"
+#include "net/vhost_net.h"
+#include "qom/object_interfaces.h"
+
+static char *netfilter_get_netdev_id(Object *obj, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    return g_strdup(nf->netdev_id);
+}
+
+static void netfilter_set_netdev_id(Object *obj, const char *str, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    nf->netdev_id = g_strdup(str);
+}
+
+static int netfilter_get_chain(Object *obj, Error **errp G_GNUC_UNUSED)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    return nf->chain;
+}
+
+static void netfilter_set_chain(Object *obj, int chain, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    nf->chain = chain;
+}
+
+static void netfilter_init(Object *obj)
+{
+    object_property_add_str(obj, "netdev",
+                            netfilter_get_netdev_id, netfilter_set_netdev_id,
+                            NULL);
+    object_property_add_enum(obj, "chain", "NetFilterChain",
+                             NetFilterChain_lookup,
+                             netfilter_get_chain, netfilter_set_chain,
+                             NULL);
+}
Quoted 8 lines

net/filter.c line 60

Quoted 7 lines
net/filter.c new file +7 -0
+
+static void netfilter_finalize(Object *obj)
+{
+    NetFilterState *nf = NETFILTER(obj);
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(obj);
+
+    if (nfc->cleanup) {

Is ->cleanup optional?

Quoted 2 lines

Yes, it's optional, as well as setup, if a concrete filter do not have things to setup/cleanup.

Please document that in NetFilterClass.

Quoted 11 lines
Quoted 7 lines

net/filter.c line 64

Quoted 4 lines
net/filter.c new file +4 -0
+        nfc->cleanup(nf);
+    }
+
+    if (nf->netdev && !QTAILQ_EMPTY(&nf->netdev->filters)) {

How can nf->netdev be null?

How can nf->netdev->filters be empty?

When object initialize failed. for example, in netfilter_complete no nf->netdev_id provided, object initialize will fail, and the netfilter_finalize will be called, at the mean time, nf->netdev is null, and nf->netdev->filters is empty.

Okay.

Quoted 92 lines
Quoted 31 lines

net/filter.c line 67

Quoted 3 lines
net/filter.c new file +3 -0
+        QTAILQ_REMOVE(&nf->netdev->filters, nf, next);
+    }
+}

Putting netfilter_finalize() after netfilter_complete() would be easier to understand, because then destruction follows creation.

Quoted 13 lines
net/filter.c new file +13 -0
+
+static void netfilter_complete(UserCreatable *uc, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(uc);
+    NetClientState *ncs[MAX_QUEUE_NUM];
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
+    int queues;
+    Error *local_err = NULL;
+
+    if (!nf->netdev_id) {
+        error_setg(errp, "Parameter 'netdev' is required");
+        return;
+    }

net/filter.c line 90

Quoted 10 lines
net/filter.c new file +10 -0
+
+    queues = qemu_find_net_clients_except(nf->netdev_id, ncs,
+                                          NET_CLIENT_OPTIONS_KIND_NIC,
+                                          MAX_QUEUE_NUM);
+    if (queues < 1) {
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
+                   "a network backend id");
+        return;
+    } else if (queues > 1) {
+        error_setg(errp, "Multi queue is not supported");

We spell this multiqueue elsewhere.

Quoted 3 lines

Since you're only interested in a single queue, you could save stack space by making ncs[] just one element large. Not worth it if you intend to support multiqueue soon.

We will support multiqueue soon.

Quoted 4 lines
Quoted 4 lines
net/filter.c new file +4 -0
+        return;
+    }
+
+    if (get_vhost_net(ncs[0])) {
Quoted 8 lines

net/filter.c line 101

Quoted 7 lines
net/filter.c new file +7 -0
+        error_setg(errp, "Vhost is not supported");
+        return;
+    }
+
+    nf->netdev = ncs[0];
+
+    if (nfc->setup) {

Is ->setup optional?

Yes, as said earlier.

Quoted 42 lines

net/filter.c line 108

Quoted 7 lines
net/filter.c new file +7 -0
+        nfc->setup(nf, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
+    }
+    QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);

I think I'd create netdev functions to add and remove filters, so the filter code doesn't have to modify the netdev state. Right now, NetClientState member filters is initialized in net.c, but modified here. But I'm not the net maintainer :)

Quoted 30 lines
net/filter.c new file +30 -0
+}
+
+static void netfilter_class_init(ObjectClass *oc, void *data)
+{
+    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+
+    ucc->complete = netfilter_complete;
+}
+
+static const TypeInfo netfilter_info = {
+    .name = TYPE_NETFILTER,
+    .parent = TYPE_OBJECT,
+    .abstract = true,
+    .class_size = sizeof(NetFilterClass),
+    .class_init = netfilter_class_init,
+    .instance_size = sizeof(NetFilterState),
+    .instance_init = netfilter_init,
+    .instance_finalize = netfilter_finalize,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_USER_CREATABLE },
+        { }
+    }
+};
+
+static void register_types(void)
+{
+    type_register_static(&netfilter_info);
+}
+
+type_init(register_types);

[...]

Quoted 22 lines
Quoted 7 lines
Quoted 7 lines
qapi-schema.json +0 -0
index 2bada60..546500a 100644
@@ -2551,6 +2551,24 @@
      'opts': 'NetClientOptions' } }
 
Quoted 15 lines

qapi-schema.json line 2564

Quoted 12 lines
qapi-schema.json +11 -0
  ##
+# @NetFilterChain
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.

Uh, inhowfar is this a "chain"? As far as I can tell, it specifies whether a netfilter is attached to the transmit queue, the receive queue, or both.

Quoted 3 lines

netback's input chain or output chain, queue is mostly like a word that related to the code implementation? English is not my first language, so I'm not sure about this.

Don't worry, we'll get the language polished together :)

In PATCH 9, I learned how this is to be used:

@item -object filter-buffer,id=@var{id},netdev=@var{netdevid}[,chain=@var{all|in|out}][,interval=@var{t}]

Buffer network packets on netdev @var{netdevid}.
[...]
chain @var{all|in|out} is an option that can be applied to any netfilter, default is @option{all}.

@option{all} means this filter will receive packets both sent to/from the netdev

@option{in} means this filter will receive packets sent to the netdev

Quoted 8 lines

@option{out} means this filter will receive packets sent from the netdev

In that context, "chain" makes more sense. I'd still call it "queue".
Precedence: -netdev parameter "queues". If I understand correctly, queues=N asks for N rx and tx queues. A filter would either apply to all N rx queues, all N tx queues, or all 2*N queues. Correct?

[...]

Yes.

#24 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 5 lines · Jason Wang
Quoted 4 lines · Markus Armbruster
Quoted 2 lines · Yang Hongyang
Quoted 1 line · Markus Armbruster

Yang Hongyang <[email protected]> writes:

[...]

Quoted 44 lines
Quoted 44 lines
Quoted 22 lines
Quoted 7 lines
Quoted 7 lines
qapi-schema.json +0 -0
index 2bada60..546500a 100644
@@ -2551,6 +2551,24 @@
      'opts': 'NetClientOptions' } }
 
Quoted 15 lines

qapi-schema.json line 2564

Quoted 12 lines
qapi-schema.json +11 -0
  ##
+# @NetFilterChain
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.

Uh, inhowfar is this a "chain"? As far as I can tell, it specifies whether a netfilter is attached to the transmit queue, the receive queue, or both.

Quoted 3 lines

netback's input chain or output chain, queue is mostly like a word that related to the code implementation? English is not my first language, so I'm not sure about this.

Don't worry, we'll get the language polished together :)

In PATCH 9, I learned how this is to be used:

@item -object
filter-buffer,id=@var{id},netdev=@var{netdevid}[,chain=@var{all|in|out}][,interval=@var{t}]

Buffer network packets on netdev @var{netdevid}.
[...]
chain @var{all|in|out} is an option that can be applied to any
netfilter, default is @option{all}.

@option{all} means this filter will receive packets both sent
to/from the netdev

@option{in} means this filter will receive packets sent to the netdev

@option{out} means this filter will receive packets sent from the netdev

Quoted 8 lines
Quoted 6 lines

In that context, "chain" makes more sense. I'd still call it "queue".
Precedence: -netdev parameter "queues". If I understand correctly, queues=N asks for N rx and tx queues. A filter would either apply to all N rx queues, all N tx queues, or all 2*N queues. Correct?

[...]

Yes.

I think "queue=rx", "queue=tx" and "queue=all" would make the most sense. Unless there are more queues than rx and rx; then we'd have to reconsider "queue=all".

Call the enumeration type NetFilterDirection instead of NetFilterChain.

#25 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 57 lines · Markus Armbruster
Quoted 5 lines · Jason Wang
Quoted 4 lines · Markus Armbruster
Quoted 2 lines · Yang Hongyang
Quoted 1 line · Markus Armbruster

Yang Hongyang <[email protected]> writes:

[...]

Quoted 49 lines
Quoted 49 lines
Quoted 22 lines
Quoted 7 lines
Quoted 7 lines
qapi-schema.json +0 -0
index 2bada60..546500a 100644
@@ -2551,6 +2551,24 @@
      'opts': 'NetClientOptions' } }
 
Quoted 15 lines

qapi-schema.json line 2564

Quoted 12 lines
qapi-schema.json +11 -0
  ##
+# @NetFilterChain
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.

Uh, inhowfar is this a "chain"? As far as I can tell, it specifies whether a netfilter is attached to the transmit queue, the receive queue, or both.

Quoted 3 lines

netback's input chain or output chain, queue is mostly like a word that related to the code implementation? English is not my first language, so I'm not sure about this.

Don't worry, we'll get the language polished together :)

In PATCH 9, I learned how this is to be used:

@item -object
filter-buffer,id=@var{id},netdev=@var{netdevid}[,chain=@var{all|in|out}][,interval=@var{t}]

Buffer network packets on netdev @var{netdevid}.
[...]
chain @var{all|in|out} is an option that can be applied to any
netfilter, default is @option{all}.

@option{all} means this filter will receive packets both sent
to/from the netdev

@option{in} means this filter will receive packets sent to the netdev

@option{out} means this filter will receive packets sent from the netdev

In that context, "chain" makes more sense. I'd still call it "queue".
Precedence: -netdev parameter "queues". If I understand correctly, queues=N asks for N rx and tx queues. A filter would either apply to all N rx queues, all N tx queues, or all 2*N queues. Correct?

Quoted 7 lines
Quoted 2 lines
Quoted 1 line

[...]

Yes.

I think "queue=rx", "queue=tx" and "queue=all" would make the most sense. Unless there are more queues than rx and rx; then we'd have to reconsider "queue=all".

Call the enumeration type NetFilterDirection instead of NetFilterChain.

Ok, this sounds better.

#26 lore raw

Re: [Qemu-devel] [PATCH v11 02/12] init/cleanup of netfilter object

Quoted 61 lines · Jason Wang
Quoted 58 lines · Markus Armbruster
Quoted 5 lines · Jason Wang
Quoted 4 lines · Markus Armbruster
Quoted 2 lines · Yang Hongyang
Quoted 1 line · Markus Armbruster

Yang Hongyang <[email protected]> writes:

[...]

Quoted 50 lines
Quoted 50 lines
Quoted 22 lines
Quoted 7 lines
Quoted 7 lines
qapi-schema.json +0 -0
index 2bada60..546500a 100644
@@ -2551,6 +2551,24 @@
       'opts': 'NetClientOptions' } }
 
Quoted 15 lines

qapi-schema.json line 2564

Quoted 12 lines
qapi-schema.json +11 -0
   ##
+# @NetFilterChain
+#
+# netfilter chain, a netfilter is attached to a netdev, captures the
+# network packets of the netdev.
+#
+# @all: the filter will receive packets both sent to/from the netdev, this
+#       is the default chain.
+#
+# @in: the filter will receive packets sent to the netdev.
+#
+# @out: the filter will receive packets sent from the netdev.

Uh, inhowfar is this a "chain"? As far as I can tell, it specifies whether a netfilter is attached to the transmit queue, the receive queue, or both.

Quoted 3 lines

netback's input chain or output chain, queue is mostly like a word that related to the code implementation? English is not my first language, so I'm not sure about this.

Don't worry, we'll get the language polished together :)

In PATCH 9, I learned how this is to be used:

@item -object
filter-buffer,id=@var{id},netdev=@var{netdevid}[,chain=@var{all|in|out}][,interval=@var{t}]

Buffer network packets on netdev @var{netdevid}.
[...]
chain @var{all|in|out} is an option that can be applied to any
netfilter, default is @option{all}.

@option{all} means this filter will receive packets both sent
to/from the netdev

@option{in} means this filter will receive packets sent to the netdev

@option{out} means this filter will receive packets sent from the netdev

In that context, "chain" makes more sense. I'd still call it "queue".
Precedence: -netdev parameter "queues". If I understand correctly, queues=N asks for N rx and tx queues. A filter would either apply to all N rx queues, all N tx queues, or all 2*N queues. Correct?

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Yes.

I think "queue=rx", "queue=tx" and "queue=all" would make the most sense. Unless there are more queues than rx and rx; then we'd have to reconsider "queue=all".

Call the enumeration type NetFilterDirection instead of NetFilterChain.

Ok, this sounds better.

Ok, I will respin a v12, thanks.

Quoted 2 lines

.

Signature · 2 lines
Thanks,
Yang.
#27 lore raw

PATCH v11 3/12 netfilter: hook packets before net queue send

In reply to Yang Hongyang

Capture packets that will be sent.

---
v11: introduce qemu_netfilter_receive() a avoid dup code
v10: adjust due to QOM
v5: do not check ret against iov_size
pass sent_cb to filters

---
 include/net/filter.h |  7 ++++++
 net/filter.c         | 15 ++++++++++++
 net/net.c            | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+)
include/net/filter.h +7 -0
index 226f2f7..69902ea 100644
@@ -57,4 +57,11 @@ struct NetFilterState {
     QTAILQ_ENTRY(NetFilterState) next;
 };
 
+ssize_t qemu_netfilter_receive(NetFilterState *nf, NetFilterChain chain,
+                               NetClientState *sender,
+                               unsigned flags,
+                               const struct iovec *iov,
+                               int iovcnt,
+                               NetPacketSent *sent_cb);
+
 #endif /* QEMU_NET_FILTER_H */
net/filter.c +15 -0
index 3b810c8..905817d 100644
@@ -15,6 +15,21 @@
 #include "net/vhost_net.h"
 #include "qom/object_interfaces.h"
 
+ssize_t qemu_netfilter_receive(NetFilterState *nf, NetFilterChain chain,
+                               NetClientState *sender,
+                               unsigned flags,
+                               const struct iovec *iov,
+                               int iovcnt,
+                               NetPacketSent *sent_cb)
+{
+    if (nf->chain == chain || nf->chain == NET_FILTER_CHAIN_ALL) {
+        return NETFILTER_GET_CLASS(OBJECT(nf))->receive_iov(
+                                   nf, sender, flags, iov, iovcnt, sent_cb);
+    }
+
+    return 0;
+}
+
 static char *netfilter_get_netdev_id(Object *obj, Error **errp)
 {
     NetFilterState *nf = NETFILTER(obj);
net/net.c +64 -0

93 lines not on this page · raw message

Signature · 1 line
1.9.1
#28 lore raw

PATCH v11 4/12 net: merge qemu_deliver_packet and qemu_deliver_packet_iov

In reply to Yang Hongyang

qemu_deliver_packet_iov already have the compat delivery, we can drop qemu_deliver_packet.

---
 include/net/net.h |  5 -----
 net/net.c         | 40 +++++++---------------------------------
 net/queue.c       |  6 +++++-
 3 files changed, 12 insertions(+), 39 deletions(-)
include/net/net.h +0 -5
index 36e5fab..7af3e15 100644
@@ -152,11 +152,6 @@ void qemu_check_nic_model(NICInfo *nd, const char *model);
 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
                         const char *default_model);
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque);
 ssize_t qemu_deliver_packet_iov(NetClientState *sender,
                             unsigned flags,
                             const struct iovec *iov,
net/net.c +7 -33
index ad37419..ca35b27 100644
@@ -597,36 +597,6 @@ static ssize_t filter_receive(NetClientState *nc, NetFilterChain chain,
     return filter_receive_iov(nc, chain, sender, flags, &iov, 1, sent_cb);
 }
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque)
-{
-    NetClientState *nc = opaque;
-    ssize_t ret;
-
-    if (nc->link_down) {
-        return size;
-    }
-
-    if (nc->receive_disabled) {
-        return 0;
-    }
-
-    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
-        ret = nc->info->receive_raw(nc, data, size);
-    } else {
-        ret = nc->info->receive(nc, data, size);
-    }
-
-    if (ret == 0) {
-        nc->receive_disabled = 1;
-    }
-
-    return ret;
-}
-
 void qemu_purge_queued_packets(NetClientState *nc)
 {
     if (!nc->peer) {
@@ -717,14 +687,18 @@ ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
 }
 
 static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
-                               int iovcnt)
+                               int iovcnt, unsigned flags)
 {
     uint8_t buffer[NET_BUFSIZE];
     size_t offset;
 
     offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
 
-    return nc->info->receive(nc, buffer, offset);
+    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
+        return nc->info->receive_raw(nc, buffer, offset);
+    } else {
+        return nc->info->receive(nc, buffer, offset);
+    }
 }
 
 ssize_t qemu_deliver_packet_iov(NetClientState *sender,
@@ -747,7 +721,7 @@ ssize_t qemu_deliver_packet_iov(NetClientState *sender,
     if (nc->info->receive_iov) {
         ret = nc->info->receive_iov(nc, iov, iovcnt);
     } else {
-        ret = nc_sendv_compat(nc, iov, iovcnt);
+        ret = nc_sendv_compat(nc, iov, iovcnt, flags);
     }
 
     if (ret == 0) {
net/queue.c +5 -1
index ebbe2bb..cf8db3a 100644
@@ -152,9 +152,13 @@ static ssize_t qemu_net_queue_deliver(NetQueue *queue,
                                       size_t size)
 {
     ssize_t ret = -1;
+    struct iovec iov = {
+        .iov_base = (void *)data,
+        .iov_len = size
+    };
 
     queue->delivering = 1;
-    ret = qemu_deliver_packet(sender, flags, data, size, queue->opaque);
+    ret = qemu_deliver_packet_iov(sender, flags, &iov, 1, queue->opaque);
     queue->delivering = 0;
 
     return ret;
Signature · 1 line
1.9.1
#29 lore raw

Re: [Qemu-devel] [PATCH v11 04/12] net: merge qemu_deliver_packet and qemu_deliver_packet_iov

Quoted 75 lines · Yang Hongyang

qemu_deliver_packet_iov already have the compat delivery, we can drop qemu_deliver_packet.

---
 include/net/net.h |  5 -----
 net/net.c         | 40 +++++++---------------------------------
 net/queue.c       |  6 +++++-
 3 files changed, 12 insertions(+), 39 deletions(-)
include/net/net.h +0 -5
index 36e5fab..7af3e15 100644
@@ -152,11 +152,6 @@ void qemu_check_nic_model(NICInfo *nd, const char *model);
 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
                         const char *default_model);
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque);
 ssize_t qemu_deliver_packet_iov(NetClientState *sender,
                             unsigned flags,
                             const struct iovec *iov,
net/net.c +1 -31
index ad37419..ca35b27 100644
@@ -597,36 +597,6 @@ static ssize_t filter_receive(NetClientState *nc, NetFilterChain chain,
     return filter_receive_iov(nc, chain, sender, flags, &iov, 1, sent_cb);
 }
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque)
-{
-    NetClientState *nc = opaque;
-    ssize_t ret;
-
-    if (nc->link_down) {
-        return size;
-    }
-
-    if (nc->receive_disabled) {
-        return 0;
-    }
-
-    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
-        ret = nc->info->receive_raw(nc, data, size);
-    } else {
-        ret = nc->info->receive(nc, data, size);
-    }
-
-    if (ret == 0) {
-        nc->receive_disabled = 1;
-    }
-
-    return ret;
-}
-
 void qemu_purge_queued_packets(NetClientState *nc)
 {
     if (!nc->peer) {
@@ -717,14 +687,18 @@ ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
 }
 
 static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
-                               int iovcnt)
+                               int iovcnt, unsigned flags)
 {
     uint8_t buffer[NET_BUFSIZE];

net/net.c ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size) · line 701

Quoted 10 lines
net/net.c +5 -1
     size_t offset;
 
     offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
 
-    return nc->info->receive(nc, buffer, offset);
+    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
+        return nc->info->receive_raw(nc, buffer, offset);
+    } else {
+        return nc->info->receive(nc, buffer, offset);
+    }

Then for net clients that doesn't support receive_iov, an extra memcpy is introduced. This seems unnecessary. And this patch looks independent, so please drop this patch from the series (This can also help to reduce the iteration). I think we may need this after all net clients support receive_iov().

Quoted 31 lines
net/net.c +1 -1
 }
 
 ssize_t qemu_deliver_packet_iov(NetClientState *sender,
@@ -747,7 +721,7 @@ ssize_t qemu_deliver_packet_iov(NetClientState *sender,
     if (nc->info->receive_iov) {
         ret = nc->info->receive_iov(nc, iov, iovcnt);
     } else {
-        ret = nc_sendv_compat(nc, iov, iovcnt);
+        ret = nc_sendv_compat(nc, iov, iovcnt, flags);
     }
 
     if (ret == 0) {
net/queue.c +5 -1
index ebbe2bb..cf8db3a 100644
@@ -152,9 +152,13 @@ static ssize_t qemu_net_queue_deliver(NetQueue *queue,
                                       size_t size)
 {
     ssize_t ret = -1;
+    struct iovec iov = {
+        .iov_base = (void *)data,
+        .iov_len = size
+    };
 
     queue->delivering = 1;
-    ret = qemu_deliver_packet(sender, flags, data, size, queue->opaque);
+    ret = qemu_deliver_packet_iov(sender, flags, &iov, 1, queue->opaque);
     queue->delivering = 0;
 
     return ret;
#30 lore raw

Re: [Qemu-devel] [PATCH v11 04/12] net: merge qemu_deliver_packet and qemu_deliver_packet_iov

Hi Jason,

Thanks for the review,

Quoted 82 lines · Jason Wang
Quoted 79 lines · Yang Hongyang

qemu_deliver_packet_iov already have the compat delivery, we can drop qemu_deliver_packet.

---
  include/net/net.h |  5 -----
  net/net.c         | 40 +++++++---------------------------------
  net/queue.c       |  6 +++++-
  3 files changed, 12 insertions(+), 39 deletions(-)
include/net/net.h +0 -5
index 36e5fab..7af3e15 100644
@@ -152,11 +152,6 @@ void qemu_check_nic_model(NICInfo *nd, const char *model);
  int qemu_find_nic_model(NICInfo *nd, const char * const *models,
                          const char *default_model);
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque);
  ssize_t qemu_deliver_packet_iov(NetClientState *sender,
                              unsigned flags,
                              const struct iovec *iov,
net/net.c +1 -31
index ad37419..ca35b27 100644
@@ -597,36 +597,6 @@ static ssize_t filter_receive(NetClientState *nc, NetFilterChain chain,
      return filter_receive_iov(nc, chain, sender, flags, &iov, 1, sent_cb);
  }
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque)
-{
-    NetClientState *nc = opaque;
-    ssize_t ret;
-
-    if (nc->link_down) {
-        return size;
-    }
-
-    if (nc->receive_disabled) {
-        return 0;
-    }
-
-    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
-        ret = nc->info->receive_raw(nc, data, size);
-    } else {
-        ret = nc->info->receive(nc, data, size);
-    }
-
-    if (ret == 0) {
-        nc->receive_disabled = 1;
-    }
-
-    return ret;
-}
-
  void qemu_purge_queued_packets(NetClientState *nc)
  {
      if (!nc->peer) {
@@ -717,14 +687,18 @@ ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
  }
 
  static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
-                               int iovcnt)
+                               int iovcnt, unsigned flags)
  {
      uint8_t buffer[NET_BUFSIZE];
      size_t offset;
 
      offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
 
Quoted 12 lines

net/net.c ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size) · line 701

Quoted 6 lines
net/net.c +5 -1
-    return nc->info->receive(nc, buffer, offset);
+    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
+        return nc->info->receive_raw(nc, buffer, offset);
+    } else {
+        return nc->info->receive(nc, buffer, offset);
+    }

Then for net clients that doesn't support receive_iov, an extra memcpy is introduced. This seems unnecessary. And this patch looks independent, so please drop this patch from the series (This can also help to reduce the iteration). I think we may need this after all net clients support receive_iov().

This patch is required by the next patch which introduce a +/* Returns:
+ * >0 - success
+ * 0 - queue packet for future redelivery
+ * <0 - failure (discard packet)
+ */
+typedef ssize_t (NetQueueDeliverFunc)(NetClientState *sender,

+                                      unsigned flags,
+                                      const struct iovec *iov,
+                                      int iovcnt,
+                                      void *opaque);

If we drop this patch, we will need to introduce 2 deliver func, which seems not clean and simple.

Quoted 37 lines
Quoted 23 lines
net/net.c +1 -1
  }
 
  ssize_t qemu_deliver_packet_iov(NetClientState *sender,
@@ -747,7 +721,7 @@ ssize_t qemu_deliver_packet_iov(NetClientState *sender,
      if (nc->info->receive_iov) {
          ret = nc->info->receive_iov(nc, iov, iovcnt);
      } else {
-        ret = nc_sendv_compat(nc, iov, iovcnt);
+        ret = nc_sendv_compat(nc, iov, iovcnt, flags);
      }
 
      if (ret == 0) {
net/queue.c +1 -0
index ebbe2bb..cf8db3a 100644
@@ -152,9 +152,13 @@ static ssize_t qemu_net_queue_deliver(NetQueue *queue,
                                        size_t size)
  {
      ssize_t ret = -1;
+    struct iovec iov = {

net/queue.c static ssize_t qemu_net_queue_deliver(NetQueue *queue, · line 164

Quoted 10 lines
net/queue.c +4 -1
+        .iov_base = (void *)data,
+        .iov_len = size
+    };
 
      queue->delivering = 1;
-    ret = qemu_deliver_packet(sender, flags, data, size, queue->opaque);
+    ret = qemu_deliver_packet_iov(sender, flags, &iov, 1, queue->opaque);
      queue->delivering = 0;
 
      return ret;

.

Signature · 2 lines
Thanks,
Yang.
#31 lore raw

Re: [Qemu-devel] [PATCH v11 04/12] net: merge qemu_deliver_packet and qemu_deliver_packet_iov

Quoted 117 lines · Yang Hongyang

Hi Jason,

Thanks for the review,

Quoted 87 lines · Jason Wang
Quoted 84 lines · Yang Hongyang

qemu_deliver_packet_iov already have the compat delivery, we can drop qemu_deliver_packet.

---
  include/net/net.h |  5 -----
  net/net.c         | 40 +++++++---------------------------------
  net/queue.c       |  6 +++++-
  3 files changed, 12 insertions(+), 39 deletions(-)
include/net/net.h +0 -5
index 36e5fab..7af3e15 100644
@@ -152,11 +152,6 @@ void qemu_check_nic_model(NICInfo *nd, const
 char *model);
  int qemu_find_nic_model(NICInfo *nd, const char * const *models,
                          const char *default_model);
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque);
  ssize_t qemu_deliver_packet_iov(NetClientState *sender,
                              unsigned flags,

const struct iovec *iov,

net/net.c +0 -30
index ad37419..ca35b27 100644
@@ -597,36 +597,6 @@ static ssize_t filter_receive(NetClientState
 *nc, NetFilterChain chain,
      return filter_receive_iov(nc, chain, sender, flags, &iov, 1,
 sent_cb);
  }
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque)
-{
-    NetClientState *nc = opaque;
-    ssize_t ret;
-
-    if (nc->link_down) {
-        return size;
-    }
-
-    if (nc->receive_disabled) {
-        return 0;
-    }
-
-    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
-        ret = nc->info->receive_raw(nc, data, size);
-    } else {
-        ret = nc->info->receive(nc, data, size);
-    }
-
-    if (ret == 0) {
-        nc->receive_disabled = 1;
-    }
-
-    return ret;
-}
-
  void qemu_purge_queued_packets(NetClientState *nc)
  {
      if (!nc->peer) {
net/net.c +1 -1
@@ -717,14 +687,18 @@ ssize_t qemu_send_packet_raw(NetClientState
 *nc, const uint8_t *buf, int size)
  }
 
  static ssize_t nc_sendv_compat(NetClientState *nc, const struct
 iovec *iov,
-                               int iovcnt)
+                               int iovcnt, unsigned flags)
  {
      uint8_t buffer[NET_BUFSIZE];
      size_t offset;
 
      offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
 
Quoted 12 lines

net/net.c ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size) · line 701

Quoted 6 lines
net/net.c +5 -1
-    return nc->info->receive(nc, buffer, offset);
+    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
+        return nc->info->receive_raw(nc, buffer, offset);
+    } else {
+        return nc->info->receive(nc, buffer, offset);
+    }

Then for net clients that doesn't support receive_iov, an extra memcpy is introduced. This seems unnecessary. And this patch looks independent, so please drop this patch from the series (This can also help to reduce the iteration). I think we may need this after all net clients support receive_iov().

This patch is required by the next patch which introduce a

(quoted diff) +10 -0
+/* Returns:
+ *   >0 - success
+ *    0 - queue packet for future redelivery
+ *   <0 - failure (discard packet)
+ */
+typedef ssize_t (NetQueueDeliverFunc)(NetClientState *sender,
+                                      unsigned flags,
+                                      const struct iovec *iov,
+                                      int iovcnt,
+                                      void *opaque);
Quoted 2 lines

If we drop this patch, we will need to introduce 2 deliver func, which seems not clean and simple.

Then you can probably skip the iov_to_buf() if iov_cnt is one in the above code?

Quoted 43 lines
Quoted 42 lines
Quoted 35 lines
net/net.c +0 -0
  }

ssize_t qemu_deliver_packet_iov(NetClientState *sender,

net/net.c +1 -1
@@ -747,7 +721,7 @@ ssize_t qemu_deliver_packet_iov(NetClientState
 *sender,
      if (nc->info->receive_iov) {
          ret = nc->info->receive_iov(nc, iov, iovcnt);
      } else {
-        ret = nc_sendv_compat(nc, iov, iovcnt);
+        ret = nc_sendv_compat(nc, iov, iovcnt, flags);
      }
 
      if (ret == 0) {
net/queue.c +5 -1
index ebbe2bb..cf8db3a 100644
@@ -152,9 +152,13 @@ static ssize_t qemu_net_queue_deliver(NetQueue
 *queue,
                                        size_t size)
  {
      ssize_t ret = -1;
+    struct iovec iov = {
+        .iov_base = (void *)data,
+        .iov_len = size
+    };
 
      queue->delivering = 1;
-    ret = qemu_deliver_packet(sender, flags, data, size,
 queue->opaque);
+    ret = qemu_deliver_packet_iov(sender, flags, &iov, 1,
 queue->opaque);
Quoted 3 lines
      queue->delivering = 0;

      return ret;

.

#32 lore raw

Re: [Qemu-devel] [PATCH v11 04/12] net: merge qemu_deliver_packet and qemu_deliver_packet_iov

Quoted 120 lines · Jason Wang
Quoted 117 lines · Yang Hongyang

Hi Jason,

Thanks for the review,

Quoted 87 lines · Jason Wang
Quoted 84 lines · Yang Hongyang

qemu_deliver_packet_iov already have the compat delivery, we can drop qemu_deliver_packet.

---
   include/net/net.h |  5 -----
   net/net.c         | 40 +++++++---------------------------------
   net/queue.c       |  6 +++++-
   3 files changed, 12 insertions(+), 39 deletions(-)
include/net/net.h +0 -5
index 36e5fab..7af3e15 100644
@@ -152,11 +152,6 @@ void qemu_check_nic_model(NICInfo *nd, const
 char *model);
   int qemu_find_nic_model(NICInfo *nd, const char * const *models,
                           const char *default_model);
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque);
   ssize_t qemu_deliver_packet_iov(NetClientState *sender,
                               unsigned flags,

const struct iovec *iov,

net/net.c +0 -30
index ad37419..ca35b27 100644
@@ -597,36 +597,6 @@ static ssize_t filter_receive(NetClientState
 *nc, NetFilterChain chain,
       return filter_receive_iov(nc, chain, sender, flags, &iov, 1,
 sent_cb);
   }
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque)
-{
-    NetClientState *nc = opaque;
-    ssize_t ret;
-
-    if (nc->link_down) {
-        return size;
-    }
-
-    if (nc->receive_disabled) {
-        return 0;
-    }
-
-    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
-        ret = nc->info->receive_raw(nc, data, size);
-    } else {
-        ret = nc->info->receive(nc, data, size);
-    }
-
-    if (ret == 0) {
-        nc->receive_disabled = 1;
-    }
-
-    return ret;
-}
-
   void qemu_purge_queued_packets(NetClientState *nc)
   {
       if (!nc->peer) {
net/net.c +1 -1
@@ -717,14 +687,18 @@ ssize_t qemu_send_packet_raw(NetClientState
 *nc, const uint8_t *buf, int size)
   }
 
   static ssize_t nc_sendv_compat(NetClientState *nc, const struct
 iovec *iov,
-                               int iovcnt)
+                               int iovcnt, unsigned flags)
   {
       uint8_t buffer[NET_BUFSIZE];
       size_t offset;
 
       offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
 
Quoted 12 lines

net/net.c ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size) · line 701

Quoted 6 lines
net/net.c +5 -1
-    return nc->info->receive(nc, buffer, offset);
+    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
+        return nc->info->receive_raw(nc, buffer, offset);
+    } else {
+        return nc->info->receive(nc, buffer, offset);
+    }

Then for net clients that doesn't support receive_iov, an extra memcpy is introduced. This seems unnecessary. And this patch looks independent, so please drop this patch from the series (This can also help to reduce the iteration). I think we may need this after all net clients support receive_iov().

This patch is required by the next patch which introduce a

(quoted diff) +10 -0
+/* Returns:
+ *   >0 - success
+ *    0 - queue packet for future redelivery
+ *   <0 - failure (discard packet)
+ */
+typedef ssize_t (NetQueueDeliverFunc)(NetClientState *sender,
+                                      unsigned flags,
+                                      const struct iovec *iov,
+                                      int iovcnt,
+                                      void *opaque);
Quoted 5 lines
Quoted 2 lines

If we drop this patch, we will need to introduce 2 deliver func, which seems not clean and simple.

Then you can probably skip the iov_to_buf() if iov_cnt is one in the above code?

Seems like a good idea to avoid the extra memcpy, thank you! BTW, can I do this on top of the series if there's no other comments? so that the first 10 patch can be merged as is. if not, I will send another version v12 to fix this.

Quoted 47 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
net/net.c +0 -0
   }

ssize_t qemu_deliver_packet_iov(NetClientState *sender,

net/net.c +1 -1
@@ -747,7 +721,7 @@ ssize_t qemu_deliver_packet_iov(NetClientState
 *sender,
       if (nc->info->receive_iov) {
           ret = nc->info->receive_iov(nc, iov, iovcnt);
       } else {
-        ret = nc_sendv_compat(nc, iov, iovcnt);
+        ret = nc_sendv_compat(nc, iov, iovcnt, flags);
       }
 
       if (ret == 0) {
net/queue.c +5 -1
index ebbe2bb..cf8db3a 100644
@@ -152,9 +152,13 @@ static ssize_t qemu_net_queue_deliver(NetQueue
 *queue,
                                         size_t size)
   {
       ssize_t ret = -1;
+    struct iovec iov = {
+        .iov_base = (void *)data,
+        .iov_len = size
+    };
 
       queue->delivering = 1;
-    ret = qemu_deliver_packet(sender, flags, data, size,
 queue->opaque);
+    ret = qemu_deliver_packet_iov(sender, flags, &iov, 1,
 queue->opaque);
       queue->delivering = 0;
Quoted 6 lines
Quoted 5 lines
Quoted 1 line
       return ret;

.

.

Signature · 2 lines
Thanks,
Yang.
#33 lore raw

Re: [Qemu-devel] [PATCH v11 04/12] net: merge qemu_deliver_packet and qemu_deliver_packet_iov

Quoted 125 lines · Yang Hongyang
Quoted 122 lines · Jason Wang
Quoted 119 lines · Yang Hongyang

Hi Jason,

Thanks for the review,

Quoted 87 lines · Jason Wang
Quoted 84 lines · Yang Hongyang

qemu_deliver_packet_iov already have the compat delivery, we can drop qemu_deliver_packet.

---
   include/net/net.h |  5 -----
   net/net.c         | 40 +++++++---------------------------------
   net/queue.c       |  6 +++++-
   3 files changed, 12 insertions(+), 39 deletions(-)
include/net/net.h +0 -5
index 36e5fab..7af3e15 100644
@@ -152,11 +152,6 @@ void qemu_check_nic_model(NICInfo *nd, const
 char *model);
   int qemu_find_nic_model(NICInfo *nd, const char * const *models,
                           const char *default_model);
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque);
   ssize_t qemu_deliver_packet_iov(NetClientState *sender,
                               unsigned flags,

const struct iovec *iov,

net/net.c +0 -30
index ad37419..ca35b27 100644
@@ -597,36 +597,6 @@ static ssize_t filter_receive(NetClientState
 *nc, NetFilterChain chain,
       return filter_receive_iov(nc, chain, sender, flags, &iov, 1,
 sent_cb);
   }
 
-ssize_t qemu_deliver_packet(NetClientState *sender,
-                            unsigned flags,
-                            const uint8_t *data,
-                            size_t size,
-                            void *opaque)
-{
-    NetClientState *nc = opaque;
-    ssize_t ret;
-
-    if (nc->link_down) {
-        return size;
-    }
-
-    if (nc->receive_disabled) {
-        return 0;
-    }
-
-    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
-        ret = nc->info->receive_raw(nc, data, size);
-    } else {
-        ret = nc->info->receive(nc, data, size);
-    }
-
-    if (ret == 0) {
-        nc->receive_disabled = 1;
-    }
-
-    return ret;
-}
-
   void qemu_purge_queued_packets(NetClientState *nc)
   {
       if (!nc->peer) {
net/net.c +1 -1
@@ -717,14 +687,18 @@ ssize_t qemu_send_packet_raw(NetClientState
 *nc, const uint8_t *buf, int size)
   }
 
   static ssize_t nc_sendv_compat(NetClientState *nc, const struct
 iovec *iov,
-                               int iovcnt)
+                               int iovcnt, unsigned flags)
   {
       uint8_t buffer[NET_BUFSIZE];
       size_t offset;
 
       offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
 
Quoted 14 lines

net/net.c ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size) · line 701

Quoted 6 lines
net/net.c +5 -1
-    return nc->info->receive(nc, buffer, offset);
+    if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
+        return nc->info->receive_raw(nc, buffer, offset);
+    } else {
+        return nc->info->receive(nc, buffer, offset);
+    }

Then for net clients that doesn't support receive_iov, an extra memcpy is introduced. This seems unnecessary. And this patch looks independent,
so please drop this patch from the series (This can also help to reduce
the iteration). I think we may need this after all net clients support receive_iov().

This patch is required by the next patch which introduce a

(quoted diff) +10 -0
+/* Returns:
+ *   >0 - success
+ *    0 - queue packet for future redelivery
+ *   <0 - failure (discard packet)
+ */
+typedef ssize_t (NetQueueDeliverFunc)(NetClientState *sender,
+                                      unsigned flags,
+                                      const struct iovec *iov,
+                                      int iovcnt,
+                                      void *opaque);
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

If we drop this patch, we will need to introduce 2 deliver func, which seems not clean and simple.

Then you can probably skip the iov_to_buf() if iov_cnt is one in the above code?

Seems like a good idea to avoid the extra memcpy, thank you! BTW, can I do this on top of the series if there's no other comments?

Better squash the changes in this patch or another independent patch before this patch. (We don't want to break bisection).

Quoted 3 lines

so that the first 10 patch can be merged as is. if not, I will send another
version v12 to fix this.

Post V12 or if you wish, I can fix this myself :)

Thanks

#34 lore raw

Re: [Qemu-devel] [PATCH v11 04/12] net: merge qemu_deliver_packet and qemu_deliver_packet_iov

On 09/22/2015 05:19 PM, Jason Wang wrote:
[...]

Quoted 10 lines
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

If we drop this patch, we will need to introduce 2 deliver func, which seems not clean and simple.

Then you can probably skip the iov_to_buf() if iov_cnt is one in the above code?

Seems like a good idea to avoid the extra memcpy, thank you! BTW, can I do this on top of the series if there's no other comments?

Quoted 8 lines

Better squash the changes in this patch or another independent patch before this patch. (We don't want to break bisection).

Quoted 3 lines

so that the first 10 patch can be merged as is. if not, I will send another
version v12 to fix this.

Post V12 or if you wish, I can fix this myself :)

It's great if you can fix this, then I can focus on the multiqueue support later, thanks a lot for the help! :)

Quoted 6 lines

Thanks

.

Signature · 2 lines
Thanks,
Yang.
#35 lore raw

Re: [Qemu-devel] [PATCH v11 04/12] net: merge qemu_deliver_packet and qemu_deliver_packet_iov

Quoted 16 lines · Yang Hongyang

On 09/22/2015 05:19 PM, Jason Wang wrote:
[...]

Quoted 14 lines
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

If we drop this patch, we will need to introduce 2 deliver func, which
seems not clean and simple.

Then you can probably skip the iov_to_buf() if iov_cnt is one in the above code?

Seems like a good idea to avoid the extra memcpy, thank you! BTW, can I do this on top of the series if there's no other comments?

Better squash the changes in this patch or another independent patch before this patch. (We don't want to break bisection).

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

so that the first 10 patch can be merged as is. if not, I will send another
version v12 to fix this.

Post V12 or if you wish, I can fix this myself :)

It's great if you can fix this, then I can focus on the multiqueue support later, thanks a lot for the help! :)

Ok, will do.

#36 lore raw

PATCH v11 5/12 net/queue: introduce NetQueueDeliverFunc

In reply to Yang Hongyang

net/queue.c has logic to send/queue/flush packets but a qemu_deliver_packet_iov() call is hardcoded. Abstract this func so that we can use our own deliver function in netfilter.

Cc Stefan Hajnoczi <[email protected]>
---
 include/net/queue.h | 13 ++++++++++++-
 net/net.c           |  2 +-
 net/queue.c         |  8 +++++---
 3 files changed, 18 insertions(+), 5 deletions(-)
include/net/queue.h +12 -1
index fc02b33..b4a7183 100644
@@ -34,7 +34,18 @@ typedef void (NetPacketSent) (NetClientState *sender, ssize_t ret);
 #define QEMU_NET_PACKET_FLAG_NONE  0
 #define QEMU_NET_PACKET_FLAG_RAW  (1<<0)
 
-NetQueue *qemu_new_net_queue(void *opaque);
+/* Returns:
+ *   >0 - success
+ *    0 - queue packet for future redelivery
+ *   <0 - failure (discard packet)
+ */
+typedef ssize_t (NetQueueDeliverFunc)(NetClientState *sender,
+                                      unsigned flags,
+                                      const struct iovec *iov,
+                                      int iovcnt,
+                                      void *opaque);
+
+NetQueue *qemu_new_net_queue(NetQueueDeliverFunc *deliver, void *opaque);
 
 void qemu_del_net_queue(NetQueue *queue);
 
net/net.c +1 -1
index ca35b27..845daf2 100644
@@ -286,7 +286,7 @@ static void qemu_net_client_setup(NetClientState *nc,
     }
     QTAILQ_INSERT_TAIL(&net_clients, nc, next);
 
-    nc->incoming_queue = qemu_new_net_queue(nc);
+    nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
     nc->destructor = destructor;
     QTAILQ_INIT(&nc->filters);
 }
net/queue.c +5 -3
index cf8db3a..16dddf0 100644
@@ -52,13 +52,14 @@ struct NetQueue {
     void *opaque;
     uint32_t nq_maxlen;
     uint32_t nq_count;
+    NetQueueDeliverFunc *deliver;
 
     QTAILQ_HEAD(packets, NetPacket) packets;
 
     unsigned delivering : 1;
 };
 
-NetQueue *qemu_new_net_queue(void *opaque)
+NetQueue *qemu_new_net_queue(NetQueueDeliverFunc *deliver, void *opaque)
 {
     NetQueue *queue;
 
@@ -67,6 +68,7 @@ NetQueue *qemu_new_net_queue(void *opaque)
     queue->opaque = opaque;
     queue->nq_maxlen = 10000;
     queue->nq_count = 0;
+    queue->deliver = deliver;
 
     QTAILQ_INIT(&queue->packets);
 
@@ -158,7 +160,7 @@ static ssize_t qemu_net_queue_deliver(NetQueue *queue,
     };
 
     queue->delivering = 1;
-    ret = qemu_deliver_packet_iov(sender, flags, &iov, 1, queue->opaque);
+    ret = queue->deliver(sender, flags, &iov, 1, queue->opaque);
     queue->delivering = 0;
 
     return ret;
@@ -173,7 +175,7 @@ static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue,
     ssize_t ret = -1;
 
     queue->delivering = 1;
-    ret = qemu_deliver_packet_iov(sender, flags, iov, iovcnt, queue->opaque);
+    ret = queue->deliver(sender, flags, iov, iovcnt, queue->opaque);
     queue->delivering = 0;
 
     return ret;
Signature · 1 line
1.9.1
#37 lore raw

PATCH v11 6/12 netfilter: add an API to pass the packet to next filter

In reply to Yang Hongyang

add an API qemu_netfilter_pass_to_next() to pass the packet to next filter.

---
v11: use qemu_netfilter_receive to avoid dup code
v10: adjust as a NetQueueDeliverFunc
v9: fix a bug when curr filter chain is all
v5: fold params to NetPacket struct

---
 include/net/filter.h |  7 +++++++
 net/filter.c         | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
include/net/filter.h +7 -0
index 69902ea..90ab101 100644
@@ -64,4 +64,11 @@ ssize_t qemu_netfilter_receive(NetFilterState *nf, NetFilterChain chain,
                                int iovcnt,
                                NetPacketSent *sent_cb);
 
+/* pass the packet to the next filter */
+ssize_t qemu_netfilter_pass_to_next(NetClientState *sender,
+                                    unsigned flags,
+                                    const struct iovec *iov,
+                                    int iovcnt,
+                                    void *opaque);
+
 #endif /* QEMU_NET_FILTER_H */
net/filter.c +58 -0

70 lines not on this page · raw message

Signature · 1 line
1.9.1
#38 lore raw

PATCH v11 7/12 netfilter: print filter info associate with the netdev

In reply to Yang Hongyang

From: Yang Hongyang <[email protected]>

When execute "info network", print filter info also. add a info_str member to NetFilterState, store specific filters info.

---
v11: generate info_str in filter layer
v10: add a info_str member to NetFilterState
v9: tiny cleanup according to Tomas's comment.
v7: initial patch

---
 include/net/filter.h |  1 +
 net/filter.c         | 20 ++++++++++++++++++++
 net/net.c            | 11 +++++++++++
 3 files changed, 32 insertions(+)
include/net/filter.h +1 -0
index 90ab101..3fa80c9 100644
@@ -54,6 +54,7 @@ struct NetFilterState {
     char *netdev_id;
     NetClientState *netdev;
     NetFilterChain chain;
+    char info_str[256];
     QTAILQ_ENTRY(NetFilterState) next;
 };
 
net/filter.c +20 -0
index d6fa164..a8adc89 100644
@@ -15,6 +15,7 @@
 #include "net/vhost_net.h"
 #include "qom/object_interfaces.h"
 #include "qemu/iov.h"
+#include "qapi/string-output-visitor.h"
 
 ssize_t qemu_netfilter_receive(NetFilterState *nf, NetFilterChain chain,
                                NetClientState *sender,
@@ -146,6 +147,9 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
     NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
     int queues;
     Error *local_err = NULL;
+    char *str, *info;
+    ObjectProperty *prop;
+    StringOutputVisitor *ov;
 
     if (!nf->netdev_id) {
         error_setg(errp, "Parameter 'netdev' is required");
@@ -179,6 +183,22 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
         }
     }
     QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
+
+    /* generate info str */
+    QTAILQ_FOREACH(prop, &OBJECT(nf)->properties, node) {
+        if (!strcmp(prop->name, "type")) {
+            continue;
+        }
+        ov = string_output_visitor_new(false);
+        object_property_get(OBJECT(nf), string_output_get_visitor(ov),
+                            prop->name, errp);
+        str = string_output_get_string(ov);
+        string_output_visitor_cleanup(ov);
+        info = g_strdup_printf(",%s=%s", prop->name, str);
+        g_strlcat(nf->info_str, info, sizeof(nf->info_str));
+        g_free(str);
+        g_free(info);
+    }
 }
 
 static void netfilter_class_init(ObjectClass *oc, void *data)
net/net.c +11 -0
index 845daf2..e8f858d 100644
@@ -1170,10 +1170,21 @@ void qmp_netdev_del(const char *id, Error **errp)
 
 void print_net_client(Monitor *mon, NetClientState *nc)
 {
+    NetFilterState *nf;
+
     monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
                    nc->queue_index,
                    NetClientOptionsKind_lookup[nc->info->type],
                    nc->info_str);
+    if (!QTAILQ_EMPTY(&nc->filters)) {
+        monitor_printf(mon, "filters:\n");
+    }
+    QTAILQ_FOREACH(nf, &nc->filters, next) {
+        monitor_printf(mon, "  - %s: type=%s%s\n",
+                       object_get_canonical_path_component(OBJECT(nf)),
+                       object_get_typename(OBJECT(nf)),
+                       nf->info_str);
+    }
 }
 
 RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
Signature · 1 line
1.9.1
#39 lore raw

PATCH v11 8/12 net/queue: export qemu_net_queue_append_iov

In reply to Yang Hongyang

This will be used by buffer filter implementation later to queue packets.

---
 include/net/queue.h |  7 +++++++
 net/queue.c         | 12 ++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)
include/net/queue.h +7 -0
index b4a7183..5469fdb 100644
@@ -47,6 +47,13 @@ typedef ssize_t (NetQueueDeliverFunc)(NetClientState *sender,
 
 NetQueue *qemu_new_net_queue(NetQueueDeliverFunc *deliver, void *opaque);
 
+void qemu_net_queue_append_iov(NetQueue *queue,
+                               NetClientState *sender,
+                               unsigned flags,
+                               const struct iovec *iov,
+                               int iovcnt,
+                               NetPacketSent *sent_cb);
+
 void qemu_del_net_queue(NetQueue *queue);
 
 ssize_t qemu_net_queue_send(NetQueue *queue,
net/queue.c +6 -6
index 16dddf0..de8b9d3 100644
@@ -112,12 +112,12 @@ static void qemu_net_queue_append(NetQueue *queue,
     QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
 }
 
-static void qemu_net_queue_append_iov(NetQueue *queue,
-                                      NetClientState *sender,
-                                      unsigned flags,
-                                      const struct iovec *iov,
-                                      int iovcnt,
-                                      NetPacketSent *sent_cb)
+void qemu_net_queue_append_iov(NetQueue *queue,
+                               NetClientState *sender,
+                               unsigned flags,
+                               const struct iovec *iov,
+                               int iovcnt,
+                               NetPacketSent *sent_cb)
 {
     NetPacket *packet;
     size_t max_len = 0;
Signature · 1 line
1.9.1
#40 lore raw

PATCH v11 9/12 netfilter: add a netbuffer filter

In reply to Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you can also use it to simulate the network delay.
It has an interval option, if supplied, this filter will release packets by interval.

Usage:
-netdev tap,id=bn0
-object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

NOTE:
the scale of interval is microsecond.

---
v11: add a fixme comment from Jason
v10: use NetQueue flush api to flush packets
sent_cb can not be called when we already return size
v9: adjustment due to the qapi change
v7: use QTAILQ_FOREACH_SAFE() when flush packets
v6: move the interval check earlier and some comment adjust
v5: remove dummy sent_cb
change interval type from int64 to uint32 check interval!=0 when initialise
rename FILTERBUFFERState to FilterBufferState
v4: remove bh
pass the packet to next filter instead of receiver
v3: check packet's sender and sender->peer when flush it

---
 net/Makefile.objs   |   1 +
 net/filter-buffer.c | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-options.hx     |  18 ++++++
 vl.c                |   7 ++-
 4 files changed, 195 insertions(+), 1 deletion(-)
 create mode 100644 net/filter-buffer.c
net/Makefile.objs +1 -0
index 914aec0..5fa2f97 100644
@@ -14,3 +14,4 @@ common-obj-$(CONFIG_SLIRP) += slirp.o
 common-obj-$(CONFIG_VDE) += vde.o
 common-obj-$(CONFIG_NETMAP) += netmap.o
 common-obj-y += filter.o
+common-obj-y += filter-buffer.o
net/filter-buffer.c new file +170 -0

170 lines not on this page · raw message

qemu-options.hx +18 -0
index 7e147b8..b09f97f 100644
@@ -3642,6 +3642,24 @@ in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional),
 @var{server-cert.pem} (only servers), @var{server-key.pem} (only servers),
 @var{client-cert.pem} (only clients), and @var{client-key.pem} (only clients).
 
+@item -object filter-buffer,id=@var{id},netdev=@var{netdevid}[,chain=@var{all|in|out}][,interval=@var{t}]
+
+Buffer network packets on netdev @var{netdevid}.
+If interval @var{t} provided, will release packets by interval.
+Interval scale: microsecond.
+
+If interval @var{t} not provided, you have to make sure the packets can be
+released, either by manually remove this filter or call the release buffer API,
+otherwise, the packets will be buffered forever. Use with caution.
+
+chain @var{all|in|out} is an option that can be applied to any netfilter, default is @option{all}.
+
+@option{all} means this filter will receive packets both sent to/from the netdev
+
+@option{in} means this filter will receive packets sent to the netdev
+
+@option{out} means this filter will receive packets sent from the netdev
+
 @end table
 
 ETEXI
vl.c +6 -1
index ec589e2..3cf89d5 100644
@@ -2794,7 +2794,12 @@ static bool object_create_initial(const char *type)
     if (g_str_equal(type, "rng-egd")) {
         return false;
     }
-    /* TODO: return false for concrete netfilters */
+
+    /* return false for concrete netfilters */
+    if (g_str_equal(type, "filter-buffer")) {
+        return false;
+    }
+
     return true;
 }
 
Signature · 1 line
1.9.1
#41 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

Quoted 2 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you

What's "Remus"?

Quoted 3 lines

can also use it to simulate the network delay.
It has an interval option, if supplied, this filter will release packets by interval.

Suggest "will delay packets by that time interval."

Is interval really optional?

Quoted 7 lines

Usage:
-netdev tap,id=bn0
-object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

NOTE:
the scale of interval is microsecond.

Perhaps "interval is in microseconds".

Quoted 59 lines

---
v11: add a fixme comment from Jason
v10: use NetQueue flush api to flush packets
sent_cb can not be called when we already return size
v9: adjustment due to the qapi change
v7: use QTAILQ_FOREACH_SAFE() when flush packets
v6: move the interval check earlier and some comment adjust
v5: remove dummy sent_cb
change interval type from int64 to uint32 check interval!=0 when initialise
rename FILTERBUFFERState to FilterBufferState
v4: remove bh
pass the packet to next filter instead of receiver
v3: check packet's sender and sender->peer when flush it

---
 net/Makefile.objs   |   1 +
 net/filter-buffer.c | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-options.hx     |  18 ++++++
 vl.c                |   7 ++-
 4 files changed, 195 insertions(+), 1 deletion(-)
 create mode 100644 net/filter-buffer.c
net/Makefile.objs +1 -0
index 914aec0..5fa2f97 100644
@@ -14,3 +14,4 @@ common-obj-$(CONFIG_SLIRP) += slirp.o
 common-obj-$(CONFIG_VDE) += vde.o
 common-obj-$(CONFIG_NETMAP) += netmap.o
 common-obj-y += filter.o
+common-obj-y += filter-buffer.o
net/filter-buffer.c new file +20 -0
new file mode 100644
index 0000000..ef94e91
@@ -0,0 +1,170 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#include "net/filter.h"
+#include "net/queue.h"
+#include "qemu-common.h"
+#include "qemu/timer.h"
+#include "qemu/iov.h"
+#include "qapi/qmp/qerror.h"
+#include "qapi-visit.h"
+#include "qom/object.h"
+
+#define TYPE_FILTER_BUFFER "filter-buffer"
+
+#define FILTER_BUFFER(obj) \

net/filter-buffer.c line 30

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+    OBJECT_CHECK(FilterBufferState, (obj), TYPE_FILTER_BUFFER)
+
+struct FilterBufferState {
+    NetFilterState parent_obj;
+
+    NetQueue *incoming_queue;
+    uint32_t interval;
+    QEMUTimer release_timer;
+};
+typedef struct FilterBufferState FilterBufferState;

Again, not splitting the declaration is more concise.

net/filter-buffer.c line 40

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+
+static void filter_buffer_flush(NetFilterState *nf)
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    if (!qemu_net_queue_flush(s->incoming_queue)) {
+        /* Unable to empty the queue, purge remaining packets */
+        qemu_net_queue_purge(s->incoming_queue, nf->netdev);
+    }
+}

This either flushes or purges incoming_queue, where "purge" means dropping packets. Correct?

net/filter-buffer.c line 45

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+
+static void filter_buffer_release_timer(void *opaque)
+{
+    NetFilterState *nf = opaque;
+    FilterBufferState *s = FILTER_BUFFER(nf);

Style nit: blank line between declarations and statements, please.

net/filter-buffer.c line 46

Quoted 1 line
net/filter-buffer.c new file +1 -0
+    filter_buffer_flush(nf);

Is purging correct here?

net/filter-buffer.c line 48

Quoted 2 lines
net/filter-buffer.c new file +2 -0
+    timer_mod(&s->release_timer,
+              qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);

Timer rearmed to fire again in s->interval microseconds.

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+}
+
+/* filter APIs */
+static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
+                                         NetClientState *sender,

net/filter-buffer.c line 63

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+                                         unsigned flags,
+                                         const struct iovec *iov,
+                                         int iovcnt,
+                                         NetPacketSent *sent_cb)
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    /*
+     * we return size when buffer a packet, the sender will take it as
+     * a already sent packet, so sent_cb should not be called later

Humor me: when a comment has multiple sentences, start each one with a capital letter, and end it with punctuation.

net/filter-buffer.c line 66

Quoted 3 lines
net/filter-buffer.c new file +3 -0
+     * FIXME: even if guest can't receive packet for some reasons. Filter
+     * can still accept packet until its internal queue is full.
+     */

I'm not sure I understand the comment.

Quoted 6 lines
net/filter-buffer.c new file +6 -0
+    qemu_net_queue_append_iov(s->incoming_queue, sender, flags,
+                              iov, iovcnt, NULL);
+    return iov_size(iov, iovcnt);
+}
+
+static void filter_buffer_cleanup(NetFilterState *nf)

net/filter-buffer.c line 82

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    if (s->interval) {
+        timer_del(&s->release_timer);
+    }
+
+    /* flush packets */
+    if (s->incoming_queue) {
+        filter_buffer_flush(nf);

I guess purging is correct here.

net/filter-buffer.c line 94

Quoted 12 lines
net/filter-buffer.c new file +12 -0
+        g_free(s->incoming_queue);
+    }
+}
+
+static void filter_buffer_setup(NetFilterState *nf, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    /*
+     * this check should be dropped when there're VM FT solutions like MC
+     * or COLO use this filter to release packets on demand.
+     */

If you end a sentence with a period, you get to start it with a capital letter :)

"there're" is odd. Perhaps something like "We may want to accept zero interval when VM FT solutions like MC or COLO use this filter to release packets on demand."

net/filter-buffer.c line 97

Quoted 3 lines
net/filter-buffer.c new file +3 -0
+    if (!s->interval) {
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
+                   "a non-zero interval");

How can this happen? Doesn't filter_buffer_set_interval() catch zero intervals already?

net/filter-buffer.c line 102

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+        return;
+    }
+
+    s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
+    if (s->interval) {

Condition cannot be false. Same in filter_buffer_cleanup().

net/filter-buffer.c line 106

Quoted 4 lines
net/filter-buffer.c new file +4 -0
+        timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
+                      filter_buffer_release_timer, nf);
+        timer_mod(&s->release_timer,
+                  qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);

Timer armed to fire in s->interval microseconds.

Unless I misunderstand something, this doesn't actually delay each packet by s->interval microseconds, it batches packet delivery: all packets arriving in a given interval are delayed until the end of the interval. Correct?

Quoted 25 lines
net/filter-buffer.c new file +25 -0
+    }
+}
+
+static void filter_buffer_class_init(ObjectClass *oc, void *data)
+{
+    NetFilterClass *nfc = NETFILTER_CLASS(oc);
+
+    nfc->setup = filter_buffer_setup;
+    nfc->cleanup = filter_buffer_cleanup;
+    nfc->receive_iov = filter_buffer_receive_iov;
+}
+
+static void filter_buffer_get_interval(Object *obj, Visitor *v, void *opaque,
+                                       const char *name, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(obj);
+    uint32_t value = s->interval;
+
+    visit_type_uint32(v, &value, name, errp);
+}
+
+static void filter_buffer_set_interval(Object *obj, Visitor *v, void *opaque,
+                                       const char *name, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(obj);

net/filter-buffer.c line 141

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+    Error *local_err = NULL;
+    uint32_t value;
+
+    visit_type_uint32(v, &value, name, &local_err);
+    if (local_err) {
+        goto out;
+    }
+    if (!value) {
+        error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
+                   PRIu32 "'", object_get_typename(obj), name, value);

What does it take? That's what the user wants to know... Perhaps "Property '%s.%s' requires a positive value".

Quoted 34 lines
net/filter-buffer.c new file +29 -0
+        goto out;
+    }
+    s->interval = value;
+
+out:
+    error_propagate(errp, local_err);
+}
+
+static void filter_buffer_init(Object *obj)
+{
+    object_property_add(obj, "interval", "int",
+                        filter_buffer_get_interval,
+                        filter_buffer_set_interval, NULL, NULL, NULL);
+}
+
+static const TypeInfo filter_buffer_info = {
+    .name = TYPE_FILTER_BUFFER,
+    .parent = TYPE_NETFILTER,
+    .class_init = filter_buffer_class_init,
+    .instance_init = filter_buffer_init,
+    .instance_size = sizeof(FilterBufferState),
+};
+
+static void register_types(void)
+{
+    type_register_static(&filter_buffer_info);
+}
+
+type_init(register_types);
qemu-options.hx +0 -0
index 7e147b8..b09f97f 100644

qemu-options.hx in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), · line 3651

Quoted 10 lines
qemu-options.hx +7 -0
@@ -3642,6 +3642,24 @@ in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional),
 @var{server-cert.pem} (only servers), @var{server-key.pem} (only servers),
 @var{client-cert.pem} (only clients), and @var{client-key.pem} (only clients).
 
+@item -object filter-buffer,id=@var{id},netdev=@var{netdevid}[,chain=@var{all|in|out}][,interval=@var{t}]
+
+Buffer network packets on netdev @var{netdevid}.
+If interval @var{t} provided, will release packets by interval.
+Interval scale: microsecond.
+
+If interval @var{t} not provided, you have to make sure the packets can be

Are you sure omitting t works? filter_buffer_set_interval() rejects zero...

qemu-options.hx in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), · line 3653

Quoted 2 lines
qemu-options.hx +2 -0
+released, either by manually remove this filter or call the release buffer API,
+otherwise, the packets will be buffered forever. Use with caution.

Please wrap your lines a bit earlier.

qemu-options.hx in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), · line 3662

Quoted 9 lines
qemu-options.hx +9 -0
+
+chain @var{all|in|out} is an option that can be applied to any netfilter, default is @option{all}.
+
+@option{all} means this filter will receive packets both sent to/from the netdev
+
+@option{in} means this filter will receive packets sent to the netdev
+
+@option{out} means this filter will receive packets sent from the netdev
+

I'd describe this like "filter is inserted in the receive / transmit queue".

Quoted 8 lines
qemu-options.hx +0 -0
 @end table
 
 ETEXI
vl.c +0 -0
index ec589e2..3cf89d5 100644

vl.c static bool object_create_initial(const char *type) · line 2798

Quoted 6 lines
vl.c +2 -1
@@ -2794,7 +2794,12 @@ static bool object_create_initial(const char *type)
     if (g_str_equal(type, "rng-egd")) {
         return false;
     }
-    /* TODO: return false for concrete netfilters */
+
+    /* return false for concrete netfilters */

I find this comment useless, please drop it :)

Quoted 6 lines
vl.c +4 -0
+    if (g_str_equal(type, "filter-buffer")) {
+        return false;
+    }
+
     return true;
 }
#42 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you

What's "Remus"?

Remus is an opensource VM FT solution:
paper: http://http//www.cs.ubc.ca/~andy/papers/remus-nsdi-final.pdf
First implemented on Xen:
http://wiki.xen.org/wiki/Remus

MicroCheckpointing in QEMU is another Remus implementation.

Quoted 8 lines
Quoted 4 lines

can also use it to simulate the network delay.
It has an interval option, if supplied, this filter will release packets by interval.

Suggest "will delay packets by that time interval."

Is interval really optional?

It supposed to be optional. When the buffer filter has a user:
http://lists.nongnu.org/archive/html/qemu-devel/2015-09/msg00363.html

In this patchset, the zero interval check is removed. packets are released on demand through filter_buffer_release_all() api call.

Quoted 10 lines
Quoted 8 lines

Usage:
-netdev tap,id=bn0
-object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

NOTE:
the scale of interval is microsecond.

Perhaps "interval is in microseconds".

Better, thanks.

Quoted 73 lines
Quoted 60 lines

---
v11: add a fixme comment from Jason
v10: use NetQueue flush api to flush packets
sent_cb can not be called when we already return size
v9: adjustment due to the qapi change
v7: use QTAILQ_FOREACH_SAFE() when flush packets
v6: move the interval check earlier and some comment adjust
v5: remove dummy sent_cb
change interval type from int64 to uint32 check interval!=0 when initialise
rename FILTERBUFFERState to FilterBufferState
v4: remove bh
pass the packet to next filter instead of receiver
v3: check packet's sender and sender->peer when flush it

---
  net/Makefile.objs   |   1 +
  net/filter-buffer.c | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++
  qemu-options.hx     |  18 ++++++
  vl.c                |   7 ++-
  4 files changed, 195 insertions(+), 1 deletion(-)

create mode 100644 net/filter-buffer.c

net/Makefile.objs +1 -0
index 914aec0..5fa2f97 100644
@@ -14,3 +14,4 @@ common-obj-$(CONFIG_SLIRP) += slirp.o
  common-obj-$(CONFIG_VDE) += vde.o
  common-obj-$(CONFIG_NETMAP) += netmap.o
  common-obj-y += filter.o
+common-obj-y += filter-buffer.o
net/filter-buffer.c new file +20 -0
new file mode 100644
index 0000000..ef94e91
@@ -0,0 +1,170 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#include "net/filter.h"
+#include "net/queue.h"
+#include "qemu-common.h"
+#include "qemu/timer.h"
+#include "qemu/iov.h"
+#include "qapi/qmp/qerror.h"
+#include "qapi-visit.h"
+#include "qom/object.h"
+
+#define TYPE_FILTER_BUFFER "filter-buffer"
+
+#define FILTER_BUFFER(obj) \

net/filter-buffer.c line 30

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+    OBJECT_CHECK(FilterBufferState, (obj), TYPE_FILTER_BUFFER)
+
+struct FilterBufferState {
+    NetFilterState parent_obj;
+
+    NetQueue *incoming_queue;
+    uint32_t interval;
+    QEMUTimer release_timer;
+};
+typedef struct FilterBufferState FilterBufferState;

Again, not splitting the declaration is more concise.

Quoted 13 lines

net/filter-buffer.c line 40

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+
+static void filter_buffer_flush(NetFilterState *nf)
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    if (!qemu_net_queue_flush(s->incoming_queue)) {
+        /* Unable to empty the queue, purge remaining packets */
+        qemu_net_queue_purge(s->incoming_queue, nf->netdev);
+    }
+}

This either flushes or purges incoming_queue, where "purge" means dropping packets. Correct?

I think it is correct. Dropping packets is allowed, even on real hardware, packets may lose.

Quoted 9 lines

net/filter-buffer.c line 45

Quoted 6 lines
net/filter-buffer.c new file +5 -0
+
+static void filter_buffer_release_timer(void *opaque)
+{
+    NetFilterState *nf = opaque;
+    FilterBufferState *s = FILTER_BUFFER(nf);

Style nit: blank line between declarations and statements, please.

Quoted 8 lines

net/filter-buffer.c line 46

Quoted 1 line
net/filter-buffer.c new file +1 -0
+    filter_buffer_flush(nf);

Is purging correct here?

net/filter-buffer.c line 48

Quoted 2 lines
net/filter-buffer.c new file +2 -0
+    timer_mod(&s->release_timer,
+              qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);

Timer rearmed to fire again in s->interval microseconds.

Yes.

Quoted 11 lines
Quoted 11 lines
net/filter-buffer.c new file +10 -0
+}
+
+/* filter APIs */
+static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
+                                         NetClientState *sender,
+                                         unsigned flags,
+                                         const struct iovec *iov,
+                                         int iovcnt,
+                                         NetPacketSent *sent_cb)
+{
Quoted 8 lines

net/filter-buffer.c line 63

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    /*
+     * we return size when buffer a packet, the sender will take it as
+     * a already sent packet, so sent_cb should not be called later

Humor me: when a comment has multiple sentences, start each one with a capital letter, and end it with punctuation.

Ok.

Quoted 6 lines

net/filter-buffer.c line 66

Quoted 4 lines
net/filter-buffer.c new file +3 -0
+     * FIXME: even if guest can't receive packet for some reasons. Filter
+     * can still accept packet until its internal queue is full.
+     */

I'm not sure I understand the comment.

This is taken from Jason's comments, may be he can have a better explain?

Quoted 20 lines
Quoted 7 lines
net/filter-buffer.c new file +6 -0
+    qemu_net_queue_append_iov(s->incoming_queue, sender, flags,
+                              iov, iovcnt, NULL);
+    return iov_size(iov, iovcnt);
+}
+
+static void filter_buffer_cleanup(NetFilterState *nf)

net/filter-buffer.c line 82

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    if (s->interval) {
+        timer_del(&s->release_timer);
+    }
+
+    /* flush packets */
+    if (s->incoming_queue) {
+        filter_buffer_flush(nf);

I guess purging is correct here.

Quoted 15 lines

net/filter-buffer.c line 94

Quoted 12 lines
net/filter-buffer.c new file +12 -0
+        g_free(s->incoming_queue);
+    }
+}
+
+static void filter_buffer_setup(NetFilterState *nf, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    /*
+     * this check should be dropped when there're VM FT solutions like MC
+     * or COLO use this filter to release packets on demand.
+     */

If you end a sentence with a period, you get to start it with a capital letter :)

Ok, thanks.

Quoted 11 lines

"there're" is odd. Perhaps something like "We may want to accept zero interval when VM FT solutions like MC or COLO use this filter to release packets on demand."

net/filter-buffer.c line 97

Quoted 3 lines
net/filter-buffer.c new file +3 -0
+    if (!s->interval) {
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
+                   "a non-zero interval");

How can this happen? Doesn't filter_buffer_set_interval() catch zero intervals already?

When user do not supply an interval parameter, filter_buffer_set_interval() won't be called, and the interval is 0.
When we have actual user like I mentioned above, we should allow user to omit the interval option.

Quoted 14 lines

net/filter-buffer.c line 102

Quoted 6 lines
net/filter-buffer.c new file +5 -0
+        return;
+    }
+
+    s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
+    if (s->interval) {

Condition cannot be false. Same in filter_buffer_cleanup().

net/filter-buffer.c line 106

Quoted 4 lines
net/filter-buffer.c new file +4 -0
+        timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
+                      filter_buffer_release_timer, nf);
+        timer_mod(&s->release_timer,
+                  qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
Quoted 6 lines

Timer armed to fire in s->interval microseconds.

Unless I misunderstand something, this doesn't actually delay each packet by s->interval microseconds, it batches packet delivery: all packets arriving in a given interval are delayed until the end of the interval. Correct?

Correct.

Quoted 31 lines
Quoted 31 lines
net/filter-buffer.c new file +30 -0
+    }
+}
+
+static void filter_buffer_class_init(ObjectClass *oc, void *data)
+{
+    NetFilterClass *nfc = NETFILTER_CLASS(oc);
+
+    nfc->setup = filter_buffer_setup;
+    nfc->cleanup = filter_buffer_cleanup;
+    nfc->receive_iov = filter_buffer_receive_iov;
+}
+
+static void filter_buffer_get_interval(Object *obj, Visitor *v, void *opaque,
+                                       const char *name, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(obj);
+    uint32_t value = s->interval;
+
+    visit_type_uint32(v, &value, name, errp);
+}
+
+static void filter_buffer_set_interval(Object *obj, Visitor *v, void *opaque,
+                                       const char *name, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(obj);
+    Error *local_err = NULL;
+    uint32_t value;
+
+    visit_type_uint32(v, &value, name, &local_err);
+    if (local_err) {
Quoted 8 lines

net/filter-buffer.c line 141

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+        goto out;
+    }
+    if (!value) {
+        error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
+                   PRIu32 "'", object_get_typename(obj), name, value);

What does it take? That's what the user wants to know... Perhaps "Property '%s.%s' requires a positive value".

Maybe better, thanks.

Quoted 38 lines
Quoted 38 lines
net/filter-buffer.c new file +29 -0
+        goto out;
+    }
+    s->interval = value;
+
+out:
+    error_propagate(errp, local_err);
+}
+
+static void filter_buffer_init(Object *obj)
+{
+    object_property_add(obj, "interval", "int",
+                        filter_buffer_get_interval,
+                        filter_buffer_set_interval, NULL, NULL, NULL);
+}
+
+static const TypeInfo filter_buffer_info = {
+    .name = TYPE_FILTER_BUFFER,
+    .parent = TYPE_NETFILTER,
+    .class_init = filter_buffer_class_init,
+    .instance_init = filter_buffer_init,
+    .instance_size = sizeof(FilterBufferState),
+};
+
+static void register_types(void)
+{
+    type_register_static(&filter_buffer_info);
+}
+
+type_init(register_types);
qemu-options.hx +0 -0
index 7e147b8..b09f97f 100644
@@ -3642,6 +3642,24 @@ in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional),
  @var{server-cert.pem} (only servers), @var{server-key.pem} (only servers),
  @var{client-cert.pem} (only clients), and @var{client-key.pem} (only clients).
 
Quoted 10 lines

qemu-options.hx in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), · line 3651

Quoted 7 lines
qemu-options.hx +7 -0
+@item -object filter-buffer,id=@var{id},netdev=@var{netdevid}[,chain=@var{all|in|out}][,interval=@var{t}]
+
+Buffer network packets on netdev @var{netdevid}.
+If interval @var{t} provided, will release packets by interval.
+Interval scale: microsecond.
+
+If interval @var{t} not provided, you have to make sure the packets can be

Are you sure omitting t works? filter_buffer_set_interval() rejects zero...

When we omit t, filter_buffer_set_interval() won't be called.

Quoted 6 lines

qemu-options.hx in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), · line 3653

Quoted 3 lines
qemu-options.hx +2 -0
+released, either by manually remove this filter or call the release buffer API,
+otherwise, the packets will be buffered forever. Use with caution.

Please wrap your lines a bit earlier.

Quoted 12 lines

qemu-options.hx in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), · line 3662

Quoted 9 lines
qemu-options.hx +9 -0
+
+chain @var{all|in|out} is an option that can be applied to any netfilter, default is @option{all}.
+
+@option{all} means this filter will receive packets both sent to/from the netdev
+
+@option{in} means this filter will receive packets sent to the netdev
+
+@option{out} means this filter will receive packets sent from the netdev
+

I'd describe this like "filter is inserted in the receive / transmit queue".

thanks.

Quoted 17 lines
Quoted 9 lines
qemu-options.hx +0 -0
  @end table
 
  ETEXI
vl.c +0 -0
index ec589e2..3cf89d5 100644

vl.c static bool object_create_initial(const char *type) · line 2798

Quoted 6 lines
vl.c +2 -1
@@ -2794,7 +2794,12 @@ static bool object_create_initial(const char *type)
      if (g_str_equal(type, "rng-egd")) {
          return false;
      }
-    /* TODO: return false for concrete netfilters */
+
+    /* return false for concrete netfilters */

I find this comment useless, please drop it :)

Ok, thanks.

Quoted 9 lines

vl.c static bool object_create_initial(const char *type) · line 2804

Quoted 7 lines
vl.c +4 -0
+    if (g_str_equal(type, "filter-buffer")) {
+        return false;
+    }
+
      return true;
  }

.

Signature · 2 lines
Thanks,
Yang.
#43 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

Quoted 18 lines · Yang Hongyang
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you

What's "Remus"?

Remus is an opensource VM FT solution:
paper: http://http//www.cs.ubc.ca/~andy/papers/remus-nsdi-final.pdf
First implemented on Xen:
http://wiki.xen.org/wiki/Remus

MicroCheckpointing in QEMU is another Remus implementation.

[...]

Quoted 9 lines
Quoted 7 lines

Review

Quoted 5 lines

+ * FIXME: even if guest can't receive packet for some reasons.
Filter

(quoted diff) +2 -0
+     * can still accept packet until its internal queue is full.
+     */

I'm not sure I understand the comment.

This is taken from Jason's comments, may be he can have a better explain?

For example. For some reason, receiver could not receive more packets (.can_receive() returns zero). Without a filter, at most one packet will be queued in incoming queue and sender's poll will be disabled unit its sent_cb() was called. With a filter, it will keep receive the packets without caring about the receiver. This is suboptimal. May need more thoughts (e.g keeping sent_cb).

[...]

#44 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

Quoted 5 lines · Jason Wang
Quoted 4 lines · Yang Hongyang
Quoted 1 line · Markus Armbruster

Yang Hongyang <[email protected]> writes:

[...]

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
net/filter-buffer.c new file +12 -0
+static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
+                                         NetClientState *sender,
+                                         unsigned flags,
+                                         const struct iovec *iov,
+                                         int iovcnt,
+                                         NetPacketSent *sent_cb)
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    /*
+     * we return size when buffer a packet, the sender will take it as
+     * a already sent packet, so sent_cb should not be called later

[...]

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Review

Quoted 4 lines

+ * FIXME: even if guest can't receive packet for some reasons.
Filter

(quoted diff) +2 -0
+     * can still accept packet until its internal queue is full.
+     */

I'm not sure I understand the comment.

Quoted 8 lines
Quoted 1 line

This is taken from Jason's comments, may be he can have a better explain?

For example. For some reason, receiver could not receive more packets (.can_receive() returns zero). Without a filter, at most one packet will be queued in incoming queue and sender's poll will be disabled unit its sent_cb() was called. With a filter, it will keep receive the packets without caring about the receiver. This is suboptimal. May need more thoughts (e.g keeping sent_cb).

Aha. Perhaps you can work that explanation into the comment.

#45 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

In reply to Yang Hongyang
Quoted 22 lines · Yang Hongyang
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you

What's "Remus"?

Remus is an opensource VM FT solution:
paper: http://http//www.cs.ubc.ca/~andy/papers/remus-nsdi-final.pdf
First implemented on Xen:
http://wiki.xen.org/wiki/Remus

MicroCheckpointing in QEMU is another Remus implementation.

Quoted 7 lines
Quoted 4 lines

can also use it to simulate the network delay.
It has an interval option, if supplied, this filter will release packets by interval.

Suggest "will delay packets by that time interval."

Quoted 7 lines
Quoted 1 line

Is interval really optional?

It supposed to be optional. When the buffer filter has a user:
http://lists.nongnu.org/archive/html/qemu-devel/2015-09/msg00363.html

In this patchset, the zero interval check is removed. packets are released on demand through filter_buffer_release_all() api call.

Understand. But is interval optional right at this point in this patch series?

Quoted 100 lines
Quoted 10 lines
Quoted 8 lines

Usage:
-netdev tap,id=bn0
-object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

NOTE:
the scale of interval is microsecond.

Perhaps "interval is in microseconds".

Better, thanks.

Quoted 73 lines
Quoted 60 lines

---
v11: add a fixme comment from Jason
v10: use NetQueue flush api to flush packets
sent_cb can not be called when we already return size
v9: adjustment due to the qapi change
v7: use QTAILQ_FOREACH_SAFE() when flush packets
v6: move the interval check earlier and some comment adjust
v5: remove dummy sent_cb
change interval type from int64 to uint32 check interval!=0 when initialise
rename FILTERBUFFERState to FilterBufferState
v4: remove bh
pass the packet to next filter instead of receiver
v3: check packet's sender and sender->peer when flush it

---
  net/Makefile.objs   |   1 +
  net/filter-buffer.c | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++
  qemu-options.hx     |  18 ++++++
  vl.c                |   7 ++-
  4 files changed, 195 insertions(+), 1 deletion(-)

create mode 100644 net/filter-buffer.c

net/Makefile.objs +1 -0
index 914aec0..5fa2f97 100644
@@ -14,3 +14,4 @@ common-obj-$(CONFIG_SLIRP) += slirp.o
  common-obj-$(CONFIG_VDE) += vde.o
  common-obj-$(CONFIG_NETMAP) += netmap.o
  common-obj-y += filter.o
+common-obj-y += filter-buffer.o
net/filter-buffer.c new file +20 -0
new file mode 100644
index 0000000..ef94e91
@@ -0,0 +1,170 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#include "net/filter.h"
+#include "net/queue.h"
+#include "qemu-common.h"
+#include "qemu/timer.h"
+#include "qemu/iov.h"
+#include "qapi/qmp/qerror.h"
+#include "qapi-visit.h"
+#include "qom/object.h"
+
+#define TYPE_FILTER_BUFFER "filter-buffer"
+
+#define FILTER_BUFFER(obj) \

net/filter-buffer.c line 30

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+    OBJECT_CHECK(FilterBufferState, (obj), TYPE_FILTER_BUFFER)
+
+struct FilterBufferState {
+    NetFilterState parent_obj;
+
+    NetQueue *incoming_queue;
+    uint32_t interval;
+    QEMUTimer release_timer;
+};
+typedef struct FilterBufferState FilterBufferState;

Again, not splitting the declaration is more concise.

Quoted 13 lines

net/filter-buffer.c line 40

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+
+static void filter_buffer_flush(NetFilterState *nf)
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    if (!qemu_net_queue_flush(s->incoming_queue)) {
+        /* Unable to empty the queue, purge remaining packets */
+        qemu_net_queue_purge(s->incoming_queue, nf->netdev);
+    }
+}

This either flushes or purges incoming_queue, where "purge" means dropping packets. Correct?

Quoted 15 lines

I think it is correct. Dropping packets is allowed, even on real hardware, packets may lose.

Quoted 12 lines

net/filter-buffer.c line 45

Quoted 6 lines
net/filter-buffer.c new file +5 -0
+
+static void filter_buffer_release_timer(void *opaque)
+{
+    NetFilterState *nf = opaque;
+    FilterBufferState *s = FILTER_BUFFER(nf);

Style nit: blank line between declarations and statements, please.

net/filter-buffer.c line 46

Quoted 1 line
net/filter-buffer.c new file +1 -0
+    filter_buffer_flush(nf);

Is purging correct here?

When the timer expires, we flush as many buffered packets as we can, then throw away the rest. Why throw them away? Shouldn't we leave them in the buffer, and only throw away packets when the buffer is full?

Quoted 85 lines
Quoted 4 lines

net/filter-buffer.c line 48

Quoted 2 lines
net/filter-buffer.c new file +2 -0
+    timer_mod(&s->release_timer,
+              qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);

Timer rearmed to fire again in s->interval microseconds.

Yes.

Quoted 11 lines
Quoted 11 lines
net/filter-buffer.c new file +10 -0
+}
+
+/* filter APIs */
+static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
+                                         NetClientState *sender,
+                                         unsigned flags,
+                                         const struct iovec *iov,
+                                         int iovcnt,
+                                         NetPacketSent *sent_cb)
+{
Quoted 8 lines

net/filter-buffer.c line 63

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    /*
+     * we return size when buffer a packet, the sender will take it as
+     * a already sent packet, so sent_cb should not be called later

Humor me: when a comment has multiple sentences, start each one with a capital letter, and end it with punctuation.

Ok.

Quoted 6 lines

net/filter-buffer.c line 66

Quoted 4 lines
net/filter-buffer.c new file +3 -0
+     * FIXME: even if guest can't receive packet for some reasons. Filter
+     * can still accept packet until its internal queue is full.
+     */

I'm not sure I understand the comment.

This is taken from Jason's comments, may be he can have a better explain?

Quoted 20 lines
Quoted 7 lines
net/filter-buffer.c new file +6 -0
+    qemu_net_queue_append_iov(s->incoming_queue, sender, flags,
+                              iov, iovcnt, NULL);
+    return iov_size(iov, iovcnt);
+}
+
+static void filter_buffer_cleanup(NetFilterState *nf)

net/filter-buffer.c line 82

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    if (s->interval) {
+        timer_del(&s->release_timer);
+    }
+
+    /* flush packets */
+    if (s->incoming_queue) {
+        filter_buffer_flush(nf);

I guess purging is correct here.

Quoted 15 lines

net/filter-buffer.c line 94

Quoted 12 lines
net/filter-buffer.c new file +12 -0
+        g_free(s->incoming_queue);
+    }
+}
+
+static void filter_buffer_setup(NetFilterState *nf, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    /*
+     * this check should be dropped when there're VM FT solutions like MC
+     * or COLO use this filter to release packets on demand.
+     */

If you end a sentence with a period, you get to start it with a capital letter :)

Ok, thanks.

Quoted 9 lines

"there're" is odd. Perhaps something like "We may want to accept zero interval when VM FT solutions like MC or COLO use this filter to release packets on demand."

Quoted 3 lines
net/filter-buffer.c new file +3 -0
+    if (!s->interval) {
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
+                   "a non-zero interval");
Quoted 7 lines
Quoted 2 lines

How can this happen? Doesn't filter_buffer_set_interval() catch zero intervals already?

When user do not supply an interval parameter, filter_buffer_set_interval() won't be called, and the interval is 0.
When we have actual user like I mentioned above, we should allow user to omit the interval option.

Let's see whether I understand.

If you specify a non-zero interval, filter_buffer_set_interval() stores it.

If you specify a zero interval, filter_buffer_set_interval() rejects it.

If you specify no interval, it defaults to zero, and filter_buffer_setup() fails right here.

Therefore, interval is not optional right now.

Correct?

Quoted 13 lines
Quoted 13 lines

net/filter-buffer.c line 102

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+        return;
+    }
+
+    s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
+    if (s->interval) {

Condition cannot be false. Same in filter_buffer_cleanup().

Quoted 4 lines
net/filter-buffer.c new file +4 -0
+        timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
+                      filter_buffer_release_timer, nf);
+        timer_mod(&s->release_timer,
+                  qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
Quoted 8 lines
Quoted 6 lines

Timer armed to fire in s->interval microseconds.

Unless I misunderstand something, this doesn't actually delay each packet by s->interval microseconds, it batches packet delivery: all packets arriving in a given interval are delayed until the end of the interval. Correct?

Correct.

Documentation needs to spell that out.

Quoted 95 lines
Quoted 32 lines
Quoted 32 lines
net/filter-buffer.c new file +13 -0
+    }
+}
+
+static void filter_buffer_class_init(ObjectClass *oc, void *data)
+{
+    NetFilterClass *nfc = NETFILTER_CLASS(oc);
+
+    nfc->setup = filter_buffer_setup;
+    nfc->cleanup = filter_buffer_cleanup;
+    nfc->receive_iov = filter_buffer_receive_iov;
+}
+
+static void filter_buffer_get_interval(Object *obj, Visitor *v,

void *opaque,

net/filter-buffer.c new file +9 -0
+                                       const char *name, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(obj);
+    uint32_t value = s->interval;
+
+    visit_type_uint32(v, &value, name, errp);
+}
+
+static void filter_buffer_set_interval(Object *obj, Visitor *v,

void *opaque,

net/filter-buffer.c new file +8 -0
+                                       const char *name, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(obj);
+    Error *local_err = NULL;
+    uint32_t value;
+
+    visit_type_uint32(v, &value, name, &local_err);
+    if (local_err) {
Quoted 8 lines

net/filter-buffer.c line 141

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+        goto out;
+    }
+    if (!value) {
+        error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
+                   PRIu32 "'", object_get_typename(obj), name, value);

What does it take? That's what the user wants to know... Perhaps "Property '%s.%s' requires a positive value".

Maybe better, thanks.

Quoted 40 lines
Quoted 40 lines
net/filter-buffer.c new file +29 -0
+        goto out;
+    }
+    s->interval = value;
+
+out:
+    error_propagate(errp, local_err);
+}
+
+static void filter_buffer_init(Object *obj)
+{
+    object_property_add(obj, "interval", "int",
+                        filter_buffer_get_interval,
+                        filter_buffer_set_interval, NULL, NULL, NULL);
+}
+
+static const TypeInfo filter_buffer_info = {
+    .name = TYPE_FILTER_BUFFER,
+    .parent = TYPE_NETFILTER,
+    .class_init = filter_buffer_class_init,
+    .instance_init = filter_buffer_init,
+    .instance_size = sizeof(FilterBufferState),
+};
+
+static void register_types(void)
+{
+    type_register_static(&filter_buffer_info);
+}
+
+type_init(register_types);
qemu-options.hx +0 -0
index 7e147b8..b09f97f 100644
@@ -3642,6 +3642,24 @@ in PEM format, in filenames
 @var{ca-cert.pem}, @var{ca-crl.pem} (optional),
  @var{server-cert.pem} (only servers), @var{server-key.pem} (only servers),
  @var{client-cert.pem} (only clients), and @var{client-key.pem}

(only clients).

Quoted 11 lines

qemu-options.hx in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), · line 3651

Quoted 8 lines

+@item -object filter-buffer,id=@var{id},netdev=@var{netdevid}[,chain=@var{all|in|out}][,interval=@var{t}]

qemu-options.hx +6 -0
+
+Buffer network packets on netdev @var{netdevid}.
+If interval @var{t} provided, will release packets by interval.
+Interval scale: microsecond.
+
+If interval @var{t} not provided, you have to make sure the packets can be

Are you sure omitting t works? filter_buffer_set_interval() rejects zero...

Quoted 1 line

When we omit t, filter_buffer_set_interval() won't be called.

But filter_buffer_setup() will be, and it will fail, won't it?

Quoted 52 lines
Quoted 6 lines
Quoted 3 lines

+released, either by manually remove this filter or call the release buffer API,
+otherwise, the packets will be buffered forever. Use with caution.

Please wrap your lines a bit earlier.

Quoted 14 lines

qemu-options.hx in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), · line 3662

Quoted 11 lines
qemu-options.hx +2 -0
+
+chain @var{all|in|out} is an option that can be applied to any

netfilter, default is @option{all}.
+
+@option{all} means this filter will receive packets both sent to/from the netdev

qemu-options.hx +5 -0
+
+@option{in} means this filter will receive packets sent to the netdev
+
+@option{out} means this filter will receive packets sent from the netdev
+

I'd describe this like "filter is inserted in the receive / transmit queue".

thanks.

Quoted 17 lines
Quoted 9 lines

@end table

ETEXI

vl.c +0 -0
index ec589e2..3cf89d5 100644

vl.c static bool object_create_initial(const char *type) · line 2798

Quoted 6 lines
vl.c +2 -1
@@ -2794,7 +2794,12 @@ static bool object_create_initial(const char *type)
      if (g_str_equal(type, "rng-egd")) {
          return false;
      }
-    /* TODO: return false for concrete netfilters */
+
+    /* return false for concrete netfilters */

I find this comment useless, please drop it :)

Ok, thanks.

Quoted 9 lines

vl.c static bool object_create_initial(const char *type) · line 2804

Quoted 7 lines
vl.c +4 -0
+    if (g_str_equal(type, "filter-buffer")) {
+        return false;
+    }
+
      return true;
  }

.

#46 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

Quoted 8 lines · Markus Armbruster
Quoted 6 lines · Yang Hongyang
Quoted 5 lines · Markus Armbruster
Quoted 2 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you

What's "Remus"?

[...]

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

net/filter-buffer.c line 45

Quoted 6 lines
net/filter-buffer.c new file +5 -0
+
+static void filter_buffer_release_timer(void *opaque)
+{
+    NetFilterState *nf = opaque;
+    FilterBufferState *s = FILTER_BUFFER(nf);

Style nit: blank line between declarations and statements, please.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

+ filter_buffer_flush(nf);

Is purging correct here?

When the timer expires, we flush as many buffered packets as we can, then throw away the rest. Why throw them away? Shouldn't we leave them in the buffer, and only throw away packets when the buffer is full?

May need a "FIXME" or "TODO" here. I think this is for simplicity. We could queue the packet if the receiver or next filter could not receive packets. But currently there's no way for the next filter or recivier to notify us that it can receive more packet. This could be done in the future.

#47 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

Quoted 22 lines · Jason Wang
Quoted 8 lines · Markus Armbruster
Quoted 6 lines · Yang Hongyang
Quoted 5 lines · Markus Armbruster
Quoted 2 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you

What's "Remus"?

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

net/filter-buffer.c line 45

Quoted 6 lines
net/filter-buffer.c new file +5 -0
+
+static void filter_buffer_release_timer(void *opaque)
+{
+    NetFilterState *nf = opaque;
+    FilterBufferState *s = FILTER_BUFFER(nf);

Style nit: blank line between declarations and statements, please.

Quoted 1 line

+ filter_buffer_flush(nf);

Is purging correct here?

Quoted 8 lines
Quoted 3 lines

When the timer expires, we flush as many buffered packets as we can, then throw away the rest. Why throw them away? Shouldn't we leave them in the buffer, and only throw away packets when the buffer is full?

May need a "FIXME" or "TODO" here. I think this is for simplicity. We could queue the packet if the receiver or next filter could not receive packets. But currently there's no way for the next filter or recivier to notify us that it can receive more packet. This could be done in the future.

Good enough for me. Make it FIXME if purging packets is actually wrong, else TODO.

#48 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

In reply to Markus Armbruster
Quoted 26 lines · Markus Armbruster
Quoted 24 lines · Yang Hongyang
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you

What's "Remus"?

Remus is an opensource VM FT solution:
paper: http://http//www.cs.ubc.ca/~andy/papers/remus-nsdi-final.pdf
First implemented on Xen:
http://wiki.xen.org/wiki/Remus

MicroCheckpointing in QEMU is another Remus implementation.

Quoted 8 lines
Quoted 4 lines

can also use it to simulate the network delay.
It has an interval option, if supplied, this filter will release packets by interval.

Suggest "will delay packets by that time interval."

Is interval really optional?

Quoted 8 lines
Quoted 5 lines

It supposed to be optional. When the buffer filter has a user:
http://lists.nongnu.org/archive/html/qemu-devel/2015-09/msg00363.html

In this patchset, the zero interval check is removed. packets are released on demand through filter_buffer_release_all() api call.

Understand. But is interval optional right at this point in this patch series?

It's not, will remove this optional description although we will make it optional soon.

Quoted 405 lines
Quoted 101 lines
Quoted 11 lines
Quoted 9 lines

Usage:

   -netdev tap,id=bn0
   -object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

NOTE:
the scale of interval is microsecond.

Perhaps "interval is in microseconds".

Better, thanks.

Quoted 73 lines
Quoted 60 lines

---
v11: add a fixme comment from Jason
v10: use NetQueue flush api to flush packets
sent_cb can not be called when we already return size
v9: adjustment due to the qapi change
v7: use QTAILQ_FOREACH_SAFE() when flush packets
v6: move the interval check earlier and some comment adjust
v5: remove dummy sent_cb
change interval type from int64 to uint32 check interval!=0 when initialise
rename FILTERBUFFERState to FilterBufferState
v4: remove bh
pass the packet to next filter instead of receiver
v3: check packet's sender and sender->peer when flush it

---
   net/Makefile.objs   |   1 +
   net/filter-buffer.c | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++
   qemu-options.hx     |  18 ++++++
   vl.c                |   7 ++-
   4 files changed, 195 insertions(+), 1 deletion(-)

create mode 100644 net/filter-buffer.c

net/Makefile.objs +1 -0
index 914aec0..5fa2f97 100644
@@ -14,3 +14,4 @@ common-obj-$(CONFIG_SLIRP) += slirp.o
   common-obj-$(CONFIG_VDE) += vde.o
   common-obj-$(CONFIG_NETMAP) += netmap.o
   common-obj-y += filter.o
+common-obj-y += filter-buffer.o
net/filter-buffer.c new file +20 -0
new file mode 100644
index 0000000..ef94e91
@@ -0,0 +1,170 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#include "net/filter.h"
+#include "net/queue.h"
+#include "qemu-common.h"
+#include "qemu/timer.h"
+#include "qemu/iov.h"
+#include "qapi/qmp/qerror.h"
+#include "qapi-visit.h"
+#include "qom/object.h"
+
+#define TYPE_FILTER_BUFFER "filter-buffer"
+
+#define FILTER_BUFFER(obj) \

net/filter-buffer.c line 30

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+    OBJECT_CHECK(FilterBufferState, (obj), TYPE_FILTER_BUFFER)
+
+struct FilterBufferState {
+    NetFilterState parent_obj;
+
+    NetQueue *incoming_queue;
+    uint32_t interval;
+    QEMUTimer release_timer;
+};
+typedef struct FilterBufferState FilterBufferState;

Again, not splitting the declaration is more concise.

Quoted 13 lines

net/filter-buffer.c line 40

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+
+static void filter_buffer_flush(NetFilterState *nf)
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    if (!qemu_net_queue_flush(s->incoming_queue)) {
+        /* Unable to empty the queue, purge remaining packets */
+        qemu_net_queue_purge(s->incoming_queue, nf->netdev);
+    }
+}

This either flushes or purges incoming_queue, where "purge" means dropping packets. Correct?

Quoted 15 lines

I think it is correct. Dropping packets is allowed, even on real hardware, packets may lose.

Quoted 12 lines

net/filter-buffer.c line 45

Quoted 6 lines
net/filter-buffer.c new file +5 -0
+
+static void filter_buffer_release_timer(void *opaque)
+{
+    NetFilterState *nf = opaque;
+    FilterBufferState *s = FILTER_BUFFER(nf);

Style nit: blank line between declarations and statements, please.

net/filter-buffer.c line 46

Quoted 1 line
net/filter-buffer.c new file +1 -0
+    filter_buffer_flush(nf);

Is purging correct here?

When the timer expires, we flush as many buffered packets as we can, then throw away the rest. Why throw them away? Shouldn't we leave them in the buffer, and only throw away packets when the buffer is full?

Quoted 85 lines
Quoted 4 lines

net/filter-buffer.c line 48

Quoted 2 lines
net/filter-buffer.c new file +2 -0
+    timer_mod(&s->release_timer,
+              qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);

Timer rearmed to fire again in s->interval microseconds.

Yes.

Quoted 11 lines
Quoted 11 lines
net/filter-buffer.c new file +10 -0
+}
+
+/* filter APIs */
+static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
+                                         NetClientState *sender,
+                                         unsigned flags,
+                                         const struct iovec *iov,
+                                         int iovcnt,
+                                         NetPacketSent *sent_cb)
+{
Quoted 8 lines

net/filter-buffer.c line 63

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    /*
+     * we return size when buffer a packet, the sender will take it as
+     * a already sent packet, so sent_cb should not be called later

Humor me: when a comment has multiple sentences, start each one with a capital letter, and end it with punctuation.

Ok.

Quoted 6 lines

net/filter-buffer.c line 66

Quoted 4 lines
net/filter-buffer.c new file +3 -0
+     * FIXME: even if guest can't receive packet for some reasons. Filter
+     * can still accept packet until its internal queue is full.
+     */

I'm not sure I understand the comment.

This is taken from Jason's comments, may be he can have a better explain?

Quoted 20 lines
Quoted 7 lines
net/filter-buffer.c new file +6 -0
+    qemu_net_queue_append_iov(s->incoming_queue, sender, flags,
+                              iov, iovcnt, NULL);
+    return iov_size(iov, iovcnt);
+}
+
+static void filter_buffer_cleanup(NetFilterState *nf)

net/filter-buffer.c line 82

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    if (s->interval) {
+        timer_del(&s->release_timer);
+    }
+
+    /* flush packets */
+    if (s->incoming_queue) {
+        filter_buffer_flush(nf);

I guess purging is correct here.

Quoted 15 lines

net/filter-buffer.c line 94

Quoted 12 lines
net/filter-buffer.c new file +12 -0
+        g_free(s->incoming_queue);
+    }
+}
+
+static void filter_buffer_setup(NetFilterState *nf, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(nf);
+
+    /*
+     * this check should be dropped when there're VM FT solutions like MC
+     * or COLO use this filter to release packets on demand.
+     */

If you end a sentence with a period, you get to start it with a capital letter :)

Ok, thanks.

Quoted 9 lines

"there're" is odd. Perhaps something like "We may want to accept zero interval when VM FT solutions like MC or COLO use this filter to release packets on demand."

Quoted 3 lines
net/filter-buffer.c new file +3 -0
+    if (!s->interval) {
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
+                   "a non-zero interval");
Quoted 7 lines
Quoted 2 lines

How can this happen? Doesn't filter_buffer_set_interval() catch zero intervals already?

When user do not supply an interval parameter, filter_buffer_set_interval() won't be called, and the interval is 0.
When we have actual user like I mentioned above, we should allow user to omit the interval option.

Let's see whether I understand.

If you specify a non-zero interval, filter_buffer_set_interval() stores it.

If you specify a zero interval, filter_buffer_set_interval() rejects it.

If you specify no interval, it defaults to zero, and filter_buffer_setup() fails right here.

Therefore, interval is not optional right now.

Correct?

Quoted 13 lines
Quoted 13 lines

net/filter-buffer.c line 102

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+        return;
+    }
+
+    s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
+    if (s->interval) {

Condition cannot be false. Same in filter_buffer_cleanup().

Quoted 4 lines
net/filter-buffer.c new file +4 -0
+        timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
+                      filter_buffer_release_timer, nf);
+        timer_mod(&s->release_timer,
+                  qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
Quoted 8 lines
Quoted 6 lines

Timer armed to fire in s->interval microseconds.

Unless I misunderstand something, this doesn't actually delay each packet by s->interval microseconds, it batches packet delivery: all packets arriving in a given interval are delayed until the end of the interval. Correct?

Correct.

Documentation needs to spell that out.

Quoted 95 lines
Quoted 32 lines
Quoted 32 lines
net/filter-buffer.c new file +13 -0
+    }
+}
+
+static void filter_buffer_class_init(ObjectClass *oc, void *data)
+{
+    NetFilterClass *nfc = NETFILTER_CLASS(oc);
+
+    nfc->setup = filter_buffer_setup;
+    nfc->cleanup = filter_buffer_cleanup;
+    nfc->receive_iov = filter_buffer_receive_iov;
+}
+
+static void filter_buffer_get_interval(Object *obj, Visitor *v,

void *opaque,

net/filter-buffer.c new file +9 -0
+                                       const char *name, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(obj);
+    uint32_t value = s->interval;
+
+    visit_type_uint32(v, &value, name, errp);
+}
+
+static void filter_buffer_set_interval(Object *obj, Visitor *v,

void *opaque,

net/filter-buffer.c new file +8 -0
+                                       const char *name, Error **errp)
+{
+    FilterBufferState *s = FILTER_BUFFER(obj);
+    Error *local_err = NULL;
+    uint32_t value;
+
+    visit_type_uint32(v, &value, name, &local_err);
+    if (local_err) {
Quoted 8 lines

net/filter-buffer.c line 141

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+        goto out;
+    }
+    if (!value) {
+        error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
+                   PRIu32 "'", object_get_typename(obj), name, value);

What does it take? That's what the user wants to know... Perhaps "Property '%s.%s' requires a positive value".

Maybe better, thanks.

Quoted 40 lines
Quoted 40 lines
net/filter-buffer.c new file +29 -0
+        goto out;
+    }
+    s->interval = value;
+
+out:
+    error_propagate(errp, local_err);
+}
+
+static void filter_buffer_init(Object *obj)
+{
+    object_property_add(obj, "interval", "int",
+                        filter_buffer_get_interval,
+                        filter_buffer_set_interval, NULL, NULL, NULL);
+}
+
+static const TypeInfo filter_buffer_info = {
+    .name = TYPE_FILTER_BUFFER,
+    .parent = TYPE_NETFILTER,
+    .class_init = filter_buffer_class_init,
+    .instance_init = filter_buffer_init,
+    .instance_size = sizeof(FilterBufferState),
+};
+
+static void register_types(void)
+{
+    type_register_static(&filter_buffer_info);
+}
+
+type_init(register_types);
qemu-options.hx +0 -0
index 7e147b8..b09f97f 100644
@@ -3642,6 +3642,24 @@ in PEM format, in filenames
 @var{ca-cert.pem}, @var{ca-crl.pem} (optional),
   @var{server-cert.pem} (only servers), @var{server-key.pem} (only servers),
   @var{client-cert.pem} (only clients), and @var{client-key.pem}

(only clients).

Quoted 11 lines

qemu-options.hx in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), · line 3651

Quoted 8 lines

+@item -object filter-buffer,id=@var{id},netdev=@var{netdevid}[,chain=@var{all|in|out}][,interval=@var{t}]

qemu-options.hx +6 -0
+
+Buffer network packets on netdev @var{netdevid}.
+If interval @var{t} provided, will release packets by interval.
+Interval scale: microsecond.
+
+If interval @var{t} not provided, you have to make sure the packets can be

Are you sure omitting t works? filter_buffer_set_interval() rejects zero...

Quoted 1 line

When we omit t, filter_buffer_set_interval() won't be called.

But filter_buffer_setup() will be, and it will fail, won't it?

Quoted 41 lines
Quoted 6 lines
Quoted 3 lines

+released, either by manually remove this filter or call the release buffer API,
+otherwise, the packets will be buffered forever. Use with caution.

Please wrap your lines a bit earlier.

Quoted 14 lines

qemu-options.hx in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), · line 3662

Quoted 11 lines
qemu-options.hx +2 -0
+
+chain @var{all|in|out} is an option that can be applied to any

netfilter, default is @option{all}.
+
+@option{all} means this filter will receive packets both sent to/from the netdev

qemu-options.hx +5 -0
+
+@option{in} means this filter will receive packets sent to the netdev
+
+@option{out} means this filter will receive packets sent from the netdev
+

I'd describe this like "filter is inserted in the receive / transmit queue".

thanks.

Quoted 17 lines
Quoted 9 lines

@end table

ETEXI

vl.c +0 -0
index ec589e2..3cf89d5 100644

vl.c static bool object_create_initial(const char *type) · line 2798

Quoted 6 lines
vl.c +2 -1
@@ -2794,7 +2794,12 @@ static bool object_create_initial(const char *type)
       if (g_str_equal(type, "rng-egd")) {
           return false;
       }
-    /* TODO: return false for concrete netfilters */
+
+    /* return false for concrete netfilters */

I find this comment useless, please drop it :)

Quoted 11 lines

Ok, thanks.

Quoted 9 lines

vl.c static bool object_create_initial(const char *type) · line 2804

Quoted 7 lines
vl.c +4 -0
+    if (g_str_equal(type, "filter-buffer")) {
+        return false;
+    }
+
       return true;
   }

.

.

Signature · 2 lines
Thanks,
Yang.
#49 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

In reply to Markus Armbruster
Quoted 1 line · Markus Armbruster

Yang Hongyang <[email protected]> writes:

[...]

Quoted 5 lines
Quoted 5 lines
vl.c +0 -0
index ec589e2..3cf89d5 100644
@@ -2794,7 +2794,12 @@ static bool object_create_initial(const char *type)
Quoted 8 lines

vl.c static bool object_create_initial(const char *type) · line 2798

Quoted 6 lines
vl.c +2 -1
      if (g_str_equal(type, "rng-egd")) {
          return false;
      }
-    /* TODO: return false for concrete netfilters */
+
+    /* return false for concrete netfilters */

I find this comment useless, please drop it :)

This might be useful for reminding others who wants to implement other filters.

Quoted 9 lines

vl.c static bool object_create_initial(const char *type) · line 2804

Quoted 7 lines
vl.c +4 -0
+    if (g_str_equal(type, "filter-buffer")) {
+        return false;
+    }
+
      return true;
  }

.

Signature · 2 lines
Thanks,
Yang.
#50 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

Quoted 20 lines · Yang Hongyang
Quoted 1 line · Markus Armbruster

Yang Hongyang <[email protected]> writes:

[...]

Quoted 6 lines
Quoted 6 lines
vl.c +0 -0
index ec589e2..3cf89d5 100644
@@ -2794,7 +2794,12 @@ static bool object_create_initial(const char
 *type)
Quoted 8 lines

vl.c static bool object_create_initial(const char *type) · line 2798

Quoted 6 lines
vl.c +2 -1
      if (g_str_equal(type, "rng-egd")) {
          return false;
      }
-    /* TODO: return false for concrete netfilters */
+
+    /* return false for concrete netfilters */

I find this comment useless, please drop it :)

Quoted 2 lines

This might be useful for reminding others who wants to implement other filters.

I think the comment should explain why the code is return false here, not what the code is doing (which is obvious). So maybe something like:

/*
* netfilters require that the corresponding
* netdevs are already existing
*/

?

Thomas

#51 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

Quoted 24 lines · Thomas Huth
Quoted 20 lines · Yang Hongyang
Quoted 1 line · Markus Armbruster

Yang Hongyang <[email protected]> writes:

[...]

Quoted 6 lines
Quoted 6 lines
vl.c +0 -0
index ec589e2..3cf89d5 100644
@@ -2794,7 +2794,12 @@ static bool object_create_initial(const char
 *type)
Quoted 8 lines

vl.c static bool object_create_initial(const char *type) · line 2798

Quoted 6 lines
vl.c +2 -1
       if (g_str_equal(type, "rng-egd")) {
           return false;
       }
-    /* TODO: return false for concrete netfilters */
+
+    /* return false for concrete netfilters */

I find this comment useless, please drop it :)

Quoted 2 lines

This might be useful for reminding others who wants to implement other filters.

Quoted 7 lines

I think the comment should explain why the code is return false here, not what the code is doing (which is obvious). So maybe something like:

/*
* netfilters require that the corresponding
* netdevs are already existing
*/

Makes more sense, thanks:)

Quoted 7 lines

?

Thomas

.

Signature · 2 lines
Thanks,
Yang.
#52 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

In reply to Thomas Huth
Quoted 4 lines · Thomas Huth
Quoted 3 lines · Yang Hongyang

On 09/24/2015 05:12 PM, Markus Armbruster wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

Yang Hongyang <[email protected]> writes:

[...]

Quoted 6 lines

vl.c static bool object_create_initial(const char · line 2794

Quoted 6 lines
vl.c +0 -0
index ec589e2..3cf89d5 100644
@@ -2794,7 +2794,12 @@ static bool object_create_initial(const char
 *type)

/*
* Initial object creation happens before all other
* QEMU data types are created. The majority of objects
* can be created at this point. The rng-egd object
* cannot be created here, as it depends on the chardev
* already existing.
*/
static bool object_create_initial(const char *type) {

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

vl.c static bool object_create_initial(const char *type) · line 2798

Quoted 6 lines
vl.c +2 -1
     if (g_str_equal(type, "rng-egd")) {
         return false;
     }
-    /* TODO: return false for concrete netfilters */
+
+    /* return false for concrete netfilters */

+ if (g_str_equal(type, "filter-buffer")) {
+ return false;
+ }
+
return true;
}

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

I find this comment useless, please drop it :)

This might be useful for reminding others who wants to implement other filters.

I think as soon as there's one, how to add more is obvious enough.

Quoted 9 lines

I think the comment should explain why the code is return false here, not what the code is doing (which is obvious). So maybe something like:

/*
* netfilters require that the corresponding
* netdevs are already existing
*/

?

That explains *why* netfilters need to be initialized late. More useful.

The pre-existing case is explained in the function comment. Which becomes misleading at this point.

    /*
     * Can @type objects be created during initial object creation?
     * Initial object creation happens before all other QEMU data types
     * are created.  The majority of objects can be created at this
     * point.  Some can't, because the depend on other things already
     * existing.
     */
    static bool object_create_initial(const char *type)
    {
        /* rng-egd depends on its chardev already existing */
        if (g_str_equal(type, "rng-egd")) {
            return false;
        }

        /* Net filters depend on their netdev */
        if (g_str_equal(type, "filter-buffer")) {
            return false;
        }

        return true;
    }

Having to enumerate alll the net filter names here isn't nice. An "is subtype of TYPE_NETFILTER" test would be better. Can't say whether it's practical.

Of course, the real solution is to create objects either in topological or in command line order, but that's not in the cards right now.

#53 lore raw

Re: [Qemu-devel] [PATCH v11 09/12] netfilter: add a netbuffer filter

Quoted 81 lines · Markus Armbruster
Quoted 4 lines · Thomas Huth
Quoted 3 lines · Yang Hongyang

On 09/24/2015 05:12 PM, Markus Armbruster wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

Yang Hongyang <[email protected]> writes:

[...]

Quoted 6 lines

vl.c static bool object_create_initial(const char · line 2794

Quoted 6 lines
vl.c +0 -0
index ec589e2..3cf89d5 100644
@@ -2794,7 +2794,12 @@ static bool object_create_initial(const char
 *type)

/*
* Initial object creation happens before all other
* QEMU data types are created. The majority of objects
* can be created at this point. The rng-egd object
* cannot be created here, as it depends on the chardev
* already existing.
*/
static bool object_create_initial(const char *type) {

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

vl.c static bool object_create_initial(const char *type) · line 2798

Quoted 6 lines
vl.c +2 -1
      if (g_str_equal(type, "rng-egd")) {
          return false;
      }
-    /* TODO: return false for concrete netfilters */
+
+    /* return false for concrete netfilters */

+ if (g_str_equal(type, "filter-buffer")) {
+ return false;
+ }
+
return true;
}

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

I find this comment useless, please drop it :)

This might be useful for reminding others who wants to implement other filters.

I think as soon as there's one, how to add more is obvious enough.

Quoted 9 lines

I think the comment should explain why the code is return false here, not what the code is doing (which is obvious). So maybe something like:

/*
* netfilters require that the corresponding
* netdevs are already existing
*/

?

That explains *why* netfilters need to be initialized late. More useful.

The pre-existing case is explained in the function comment. Which becomes misleading at this point.

     /*
      * Can @type objects be created during initial object creation?
      * Initial object creation happens before all other QEMU data types
      * are created.  The majority of objects can be created at this
      * point.  Some can't, because the depend on other things already
      * existing.
      */
     static bool object_create_initial(const char *type)
     {
         /* rng-egd depends on its chardev already existing */
         if (g_str_equal(type, "rng-egd")) {
             return false;
         }

         /* Net filters depend on their netdev */
         if (g_str_equal(type, "filter-buffer")) {
             return false;
         }

         return true;
     }
Quoted 6 lines

Having to enumerate alll the net filter names here isn't nice. An "is subtype of TYPE_NETFILTER" test would be better. Can't say whether it's practical.

Of course, the real solution is to create objects either in topological or in command line order, but that's not in the cards right now.

Further work should needed for this, currently, I will remove the comment as you wish, thanks.

Quoted 2 lines

.

Signature · 2 lines
Thanks,
Yang.
#54 lore raw

PATCH v11 10/12 tests: add test cases for netfilter object

In reply to Yang Hongyang

Using qtest qmp interface to implement following cases:
1) add/remove netfilter
2) add a netfilter then delete the netdev
3) add/remove more than one netfilters
4) add more than one netfilters and then delete the netdev

---
v10: qmp command based on qom change
v9: qmp command change due to the qapi change

---
 tests/.gitignore       |   1 +
 tests/Makefile         |   2 +
 tests/test-netfilter.c | 200 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 203 insertions(+)
 create mode 100644 tests/test-netfilter.c
tests/.gitignore +1 -0
index 2c5e2c3..81230c9 100644
@@ -48,5 +48,6 @@ test-vmstate
 test-write-threshold
 test-x86-cpuid
 test-xbzrle
+test-netfilter
 *-test
 qapi-schema/*.test.*
tests/Makefile +2 -0
index 7c6025a..d369763 100644
@@ -189,6 +189,7 @@ check-qtest-i386-y += tests/pc-cpu-test$(EXESUF)
 check-qtest-i386-y += tests/q35-test$(EXESUF)
 gcov-files-i386-y += hw/pci-host/q35.c
 check-qtest-i386-$(CONFIG_LINUX) += tests/vhost-user-test$(EXESUF)
+check-qtest-i386-y += tests/test-netfilter$(EXESUF)
 check-qtest-x86_64-y = $(check-qtest-i386-y)
 gcov-files-i386-y += i386-softmmu/hw/timer/mc146818rtc.c
 gcov-files-x86_64-y = $(subst i386-softmmu/,x86_64-softmmu/,$(gcov-files-i386-y))
@@ -428,6 +429,7 @@ tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o
 tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o
 tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o $(test-util-obj-y)
 tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(test-block-obj-y)
+tests/test-netfilter$(EXESUF): tests/test-netfilter.o $(qtest-obj-y)
 
 ifeq ($(CONFIG_POSIX),y)
 LIBS += -lutil
tests/test-netfilter.c new file +200 -0

200 lines not on this page · raw message

Signature · 1 line
1.9.1
#55 lore raw

PATCH v11 11/12 netfilter/multiqueue: introduce netfilter name

In reply to Yang Hongyang

From: Yang Hongyang <[email protected]>

We will add multiple netfilter objects when using object_add id=xxx attach a filter to a multiqueue enabled netdev. The multiple objects should have different object id, but the filter name should be the same. Introduce netfilter name to identify the netfilters.

---
v11: initial patch

---
 include/net/filter.h | 1 +
 net/filter.c         | 3 +++
 net/net.c            | 2 +-
 3 files changed, 5 insertions(+), 1 deletion(-)
include/net/filter.h +1 -0
index 3fa80c9..a3060c9 100644
@@ -51,6 +51,7 @@ struct NetFilterState {
     Object parent;
 
     /* protected */
+    char *name;
     char *netdev_id;
     NetClientState *netdev;
     NetFilterChain chain;
net/filter.c +3 -0
index a8adc89..aea619a 100644
@@ -138,6 +138,8 @@ static void netfilter_finalize(Object *obj)
     if (nf->netdev && !QTAILQ_EMPTY(&nf->netdev->filters)) {
         QTAILQ_REMOVE(&nf->netdev->filters, nf, next);
     }
+
+    g_free(nf->name);
 }
 
 static void netfilter_complete(UserCreatable *uc, Error **errp)
@@ -173,6 +175,7 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
         return;
     }
 
+    nf->name = object_get_canonical_path_component(OBJECT(nf));
     nf->netdev = ncs[0];
 
     if (nfc->setup) {
net/net.c +1 -1
index e8f858d..33d4c99 100644
@@ -1181,7 +1181,7 @@ void print_net_client(Monitor *mon, NetClientState *nc)
     }
     QTAILQ_FOREACH(nf, &nc->filters, next) {
         monitor_printf(mon, "  - %s: type=%s%s\n",
-                       object_get_canonical_path_component(OBJECT(nf)),
+                       nf->name,
                        object_get_typename(OBJECT(nf)),
                        nf->info_str);
     }
Signature · 1 line
1.9.1
#56 lore raw

PATCH v11 12/12 netfilter: add multiqueue support

In reply to Yang Hongyang

From: Yang Hongyang <[email protected]>

add multiqueue support, if there's multiqueue, we add multi netfilter objects, other netfilter objects is the child of the first added netfilter object. So when we delete a netfilter, the other netfilter objects we added will be automatically deleted.

---
v11: initial patch

---
 net/filter.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 79 insertions(+), 7 deletions(-)
net/filter.c +79 -7
index aea619a..cc27528 100644
@@ -142,16 +142,25 @@ static void netfilter_finalize(Object *obj)
     g_free(nf->name);
 }
 
+static void proptb_free_val_func(gpointer data)
+{
+    g_free(data);
+}
+
 static void netfilter_complete(UserCreatable *uc, Error **errp)
 {
-    NetFilterState *nf = NETFILTER(uc);
+    NetFilterState *nf = NETFILTER(uc), *nfq = NULL;
     NetClientState *ncs[MAX_QUEUE_NUM];
-    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
-    int queues;
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc), *nfqc = NULL;
+    int queues, i;
     Error *local_err = NULL;
-    char *str, *info;
+    char *str, *info, *name;
     ObjectProperty *prop;
     StringOutputVisitor *ov;
+    Object *obj = NULL;
+    GHashTable *proptable = NULL;
+    GHashTableIter iter;
+    gpointer key, value;
 
     if (!nf->netdev_id) {
         error_setg(errp, "Parameter 'netdev' is required");
@@ -165,9 +174,6 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
                    "a network backend id");
         return;
-    } else if (queues > 1) {
-        error_setg(errp, "Multi queue is not supported");
-        return;
     }
 
     if (get_vhost_net(ncs[0])) {
@@ -187,6 +193,17 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
     }
     QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
 
+    if (queues > 1) {
+        /*
+         * Store the properties of the filter except "type" property.
+         * When there's multiqueue, we will create a new filter object
+         * of the same type and same properties. this hashtable is used
+         * to set newly created object properties.
+         */
+        proptable = g_hash_table_new_full(NULL, NULL, NULL,
+                                          proptb_free_val_func);
+    }
+
     /* generate info str */
     QTAILQ_FOREACH(prop, &OBJECT(nf)->properties, node) {
         if (!strcmp(prop->name, "type")) {
@@ -199,9 +216,64 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
         string_output_visitor_cleanup(ov);
         info = g_strdup_printf(",%s=%s", prop->name, str);
         g_strlcat(nf->info_str, info, sizeof(nf->info_str));
+        if (queues > 1) {
+            g_hash_table_insert(proptable, prop->name, g_strdup(str));
+        }
         g_free(str);
         g_free(info);
     }
+
+    for (i = 1; i < queues; i++) {
+        obj = object_new(object_get_typename(OBJECT(nf)));
+        /* set filter properties */
+        nfq = NETFILTER(obj);
+        nfq->name = g_strdup(nf->name);
+        nfq->netdev = ncs[i];
+        snprintf(nfq->info_str, sizeof(nfq->info_str), "%s", nf->info_str);
+
+        g_hash_table_iter_init(&iter, proptable);
+        while (g_hash_table_iter_next(&iter, &key, &value)) {
+            object_property_parse(obj, (char *)value, (char *)key, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }
+
+        /* add other queue's filter object as the first object's child */
+        name = g_strdup_printf("%s-queue-%d", nf->name, i);
+        object_property_add_child(OBJECT(nf), name, obj, &local_err);
+        g_free(name);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            goto out;
+        }
+
+        /* setup filter */
+        nfqc = NETFILTER_GET_CLASS(obj);
+        if (nfqc->setup) {
+            nfqc->setup(nfq, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }
+        QTAILQ_INSERT_TAIL(&nfq->netdev->filters, nfq, next);
+        object_unref(obj);
+    }
+
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    return;
+
+out:
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    if (obj) {
+        object_unref(obj);
+    }
 }
 
 static void netfilter_class_init(ObjectClass *oc, void *data)
Signature · 1 line
1.9.1
#57 lore raw

Re: [Qemu-devel] [PATCH v11 12/12] netfilter: add multiqueue support

Quoted 62 lines · Yang Hongyang

From: Yang Hongyang <[email protected]>

add multiqueue support, if there's multiqueue, we add multi netfilter objects, other netfilter objects is the child of the first added netfilter object. So when we delete a netfilter, the other netfilter objects we added will be automatically deleted.

---
v11: initial patch

---
 net/filter.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 79 insertions(+), 7 deletions(-)
net/filter.c +13 -7
index aea619a..cc27528 100644
@@ -142,16 +142,25 @@ static void netfilter_finalize(Object *obj)
     g_free(nf->name);
 }
 
+static void proptb_free_val_func(gpointer data)
+{
+    g_free(data);
+}
+
 static void netfilter_complete(UserCreatable *uc, Error **errp)
 {
-    NetFilterState *nf = NETFILTER(uc);
+    NetFilterState *nf = NETFILTER(uc), *nfq = NULL;
     NetClientState *ncs[MAX_QUEUE_NUM];
-    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
-    int queues;
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc), *nfqc = NULL;
+    int queues, i;
     Error *local_err = NULL;
-    char *str, *info;
+    char *str, *info, *name;
     ObjectProperty *prop;
     StringOutputVisitor *ov;
+    Object *obj = NULL;
+    GHashTable *proptable = NULL;
+    GHashTableIter iter;
+    gpointer key, value;
 
     if (!nf->netdev_id) {
         error_setg(errp, "Parameter 'netdev' is required");
@@ -165,9 +174,6 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
                    "a network backend id");
         return;
-    } else if (queues > 1) {
-        error_setg(errp, "Multi queue is not supported");
-        return;
     }
 
     if (get_vhost_net(ncs[0])) {
@@ -187,6 +193,17 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
     }
     QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
 

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 205

Quoted 10 lines
net/filter.c +10 -0
+    if (queues > 1) {
+        /*
+         * Store the properties of the filter except "type" property.
+         * When there's multiqueue, we will create a new filter object
+         * of the same type and same properties. this hashtable is used
+         * to set newly created object properties.
+         */
+        proptable = g_hash_table_new_full(NULL, NULL, NULL,
+                                          proptb_free_val_func);
+    }

I'm thinking whether or not duplicate all the properties in each netfilters is a good method. Maybe we can have a another ojbect with array of pointers to NetFilter objects embedded? Another question is whether or not we need to do this at this level. Maybe we can make the necessary Netfilter multiqueue aware. E.g let buffer filter to have multiqueue also? Then you may only need a single timer?

Quoted 50 lines
net/filter.c +40 -0
+
     /* generate info str */
     QTAILQ_FOREACH(prop, &OBJECT(nf)->properties, node) {
         if (!strcmp(prop->name, "type")) {
@@ -199,9 +216,64 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
         string_output_visitor_cleanup(ov);
         info = g_strdup_printf(",%s=%s", prop->name, str);
         g_strlcat(nf->info_str, info, sizeof(nf->info_str));
+        if (queues > 1) {
+            g_hash_table_insert(proptable, prop->name, g_strdup(str));
+        }
         g_free(str);
         g_free(info);
     }
+
+    for (i = 1; i < queues; i++) {
+        obj = object_new(object_get_typename(OBJECT(nf)));
+        /* set filter properties */
+        nfq = NETFILTER(obj);
+        nfq->name = g_strdup(nf->name);
+        nfq->netdev = ncs[i];
+        snprintf(nfq->info_str, sizeof(nfq->info_str), "%s", nf->info_str);
+
+        g_hash_table_iter_init(&iter, proptable);
+        while (g_hash_table_iter_next(&iter, &key, &value)) {
+            object_property_parse(obj, (char *)value, (char *)key, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }
+
+        /* add other queue's filter object as the first object's child */
+        name = g_strdup_printf("%s-queue-%d", nf->name, i);
+        object_property_add_child(OBJECT(nf), name, obj, &local_err);
+        g_free(name);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            goto out;
+        }
+
+        /* setup filter */
+        nfqc = NETFILTER_GET_CLASS(obj);
+        if (nfqc->setup) {
+            nfqc->setup(nfq, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 270

Quoted 10 lines
net/filter.c +10 -0
+        QTAILQ_INSERT_TAIL(&nfq->netdev->filters, nfq, next);
+        object_unref(obj);
+    }
+
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    return;
+
+out:

We may leak objects here.

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 279

Quoted 9 lines
net/filter.c +6 -0
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    if (obj) {
+        object_unref(obj);
+    }
 }
 
 static void netfilter_class_init(ObjectClass *oc, void *data)

To reduce the review iterations, I suggest to drop the patch also. Multiqueue support could be another series on top.

Thanks

#58 lore raw

Re: [Qemu-devel] [PATCH v11 12/12] netfilter: add multiqueue support

Quoted 74 lines · Jason Wang
Quoted 71 lines · Yang Hongyang

From: Yang Hongyang <[email protected]>

add multiqueue support, if there's multiqueue, we add multi netfilter objects, other netfilter objects is the child of the first added netfilter object. So when we delete a netfilter, the other netfilter objects we added will be automatically deleted.

---
v11: initial patch

---
  net/filter.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
  1 file changed, 79 insertions(+), 7 deletions(-)
net/filter.c +22 -7
index aea619a..cc27528 100644
@@ -142,16 +142,25 @@ static void netfilter_finalize(Object *obj)
      g_free(nf->name);
  }
 
+static void proptb_free_val_func(gpointer data)
+{
+    g_free(data);
+}
+
  static void netfilter_complete(UserCreatable *uc, Error **errp)
  {
-    NetFilterState *nf = NETFILTER(uc);
+    NetFilterState *nf = NETFILTER(uc), *nfq = NULL;
      NetClientState *ncs[MAX_QUEUE_NUM];
-    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
-    int queues;
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc), *nfqc = NULL;
+    int queues, i;
      Error *local_err = NULL;
-    char *str, *info;
+    char *str, *info, *name;
      ObjectProperty *prop;
      StringOutputVisitor *ov;
+    Object *obj = NULL;
+    GHashTable *proptable = NULL;
+    GHashTableIter iter;
+    gpointer key, value;
 
      if (!nf->netdev_id) {
          error_setg(errp, "Parameter 'netdev' is required");
@@ -165,9 +174,6 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
          error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
                     "a network backend id");
          return;
-    } else if (queues > 1) {
-        error_setg(errp, "Multi queue is not supported");
-        return;
      }
 
      if (get_vhost_net(ncs[0])) {
@@ -187,6 +193,17 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
      }
      QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
 
+    if (queues > 1) {
+        /*
+         * Store the properties of the filter except "type" property.
+         * When there's multiqueue, we will create a new filter object
+         * of the same type and same properties. this hashtable is used
+         * to set newly created object properties.
+         */
+        proptable = g_hash_table_new_full(NULL, NULL, NULL,
+                                          proptb_free_val_func);
Quoted 8 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 205

Quoted 1 line
net/filter.c +1 -0
+    }

I'm thinking whether or not duplicate all the properties in each netfilters is a good method. Maybe we can have a another ojbect with array of pointers to NetFilter objects embedded? Another question is whether or not we need to do this at this level. Maybe we can make the necessary Netfilter multiqueue aware. E.g let buffer filter to have multiqueue also? Then you may only need a single timer?

netfilter_complete() only called once when we add a netfilter. So in this func, we can create the same filter object if there's multiqueue.

Quoted 45 lines
Quoted 45 lines
net/filter.c +34 -0
+
      /* generate info str */
      QTAILQ_FOREACH(prop, &OBJECT(nf)->properties, node) {
          if (!strcmp(prop->name, "type")) {
@@ -199,9 +216,64 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
          string_output_visitor_cleanup(ov);
          info = g_strdup_printf(",%s=%s", prop->name, str);
          g_strlcat(nf->info_str, info, sizeof(nf->info_str));
+        if (queues > 1) {
+            g_hash_table_insert(proptable, prop->name, g_strdup(str));
+        }
          g_free(str);
          g_free(info);
      }
+
+    for (i = 1; i < queues; i++) {
+        obj = object_new(object_get_typename(OBJECT(nf)));
+        /* set filter properties */
+        nfq = NETFILTER(obj);
+        nfq->name = g_strdup(nf->name);
+        nfq->netdev = ncs[i];
+        snprintf(nfq->info_str, sizeof(nfq->info_str), "%s", nf->info_str);
+
+        g_hash_table_iter_init(&iter, proptable);
+        while (g_hash_table_iter_next(&iter, &key, &value)) {
+            object_property_parse(obj, (char *)value, (char *)key, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }
+
+        /* add other queue's filter object as the first object's child */
+        name = g_strdup_printf("%s-queue-%d", nf->name, i);
+        object_property_add_child(OBJECT(nf), name, obj, &local_err);
+        g_free(name);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            goto out;
+        }
+
+        /* setup filter */
+        nfqc = NETFILTER_GET_CLASS(obj);
+        if (nfqc->setup) {
Quoted 8 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 262

Quoted 8 lines
net/filter.c +8 -0
+            nfqc->setup(nfq, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }
+        QTAILQ_INSERT_TAIL(&nfq->netdev->filters, nfq, next);
+        object_unref(obj);
            ^^^^^^^^^^^^
Quoted 10 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 270

Quoted 8 lines
net/filter.c +8 -0
+    }
+
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    return;
+
+out:

We may leak objects here.

Seems not. see above object_unref(), if we come to here, object create in previous iter will be unrefed.

Quoted 9 lines
Quoted 9 lines
net/filter.c +6 -0
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    if (obj) {
+        object_unref(obj);
+    }
  }
 
Quoted 4 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 279

Quoted 1 line
net/filter.c +0 -0
  static void netfilter_class_init(ObjectClass *oc, void *data)

To reduce the review iterations, I suggest to drop the patch also. Multiqueue support could be another series on top.

I agree, thank you.

Quoted 6 lines

Thanks

.

Signature · 2 lines
Thanks,
Yang.
#59 lore raw

Re: [Qemu-devel] [PATCH v11 12/12] netfilter: add multiqueue support

Quoted 162 lines · Yang Hongyang
Quoted 79 lines · Jason Wang
Quoted 76 lines · Yang Hongyang

From: Yang Hongyang <[email protected]>

add multiqueue support, if there's multiqueue, we add multi netfilter objects, other netfilter objects is the child of the first added netfilter
object. So when we delete a netfilter, the other netfilter objects we added will be automatically deleted.

---
v11: initial patch

---
  net/filter.c | 86
+++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
  1 file changed, 79 insertions(+), 7 deletions(-)
net/filter.c +13 -7
index aea619a..cc27528 100644
@@ -142,16 +142,25 @@ static void netfilter_finalize(Object *obj)
      g_free(nf->name);
  }
 
+static void proptb_free_val_func(gpointer data)
+{
+    g_free(data);
+}
+
  static void netfilter_complete(UserCreatable *uc, Error **errp)
  {
-    NetFilterState *nf = NETFILTER(uc);
+    NetFilterState *nf = NETFILTER(uc), *nfq = NULL;
      NetClientState *ncs[MAX_QUEUE_NUM];
-    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
-    int queues;
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc), *nfqc = NULL;
+    int queues, i;
      Error *local_err = NULL;
-    char *str, *info;
+    char *str, *info, *name;
      ObjectProperty *prop;
      StringOutputVisitor *ov;
+    Object *obj = NULL;
+    GHashTable *proptable = NULL;
+    GHashTableIter iter;
+    gpointer key, value;
 
      if (!nf->netdev_id) {
          error_setg(errp, "Parameter 'netdev' is required");
@@ -165,9 +174,6 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
          error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
                     "a network backend id");
          return;
-    } else if (queues > 1) {
-        error_setg(errp, "Multi queue is not supported");
-        return;
      }
 
      if (get_vhost_net(ncs[0])) {
net/filter.c +5 -0
@@ -187,6 +193,17 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
      }
      QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
 
+    if (queues > 1) {
+        /*
+         * Store the properties of the filter except "type" property.
+         * When there's multiqueue, we will create a new filter object
+         * of the same type and same properties. this hashtable is

used

net/filter.c +4 -0
+         * to set newly created object properties.
+         */
+        proptable = g_hash_table_new_full(NULL, NULL, NULL,
+                                          proptb_free_val_func);
Quoted 8 lines
Quoted 1 line

+ }

I'm thinking whether or not duplicate all the properties in each netfilters is a good method. Maybe we can have a another ojbect with array of pointers to NetFilter objects embedded? Another question is whether or not we need to do this at this level. Maybe we can make the necessary Netfilter multiqueue aware. E.g let buffer filter to have multiqueue also? Then you may only need a single timer?

netfilter_complete() only called once when we add a netfilter. So in this func, we can create the same filter object if there's multiqueue.

Quoted 49 lines
Quoted 49 lines
net/filter.c +23 -0
+
      /* generate info str */
      QTAILQ_FOREACH(prop, &OBJECT(nf)->properties, node) {
          if (!strcmp(prop->name, "type")) {
@@ -199,9 +216,64 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
          string_output_visitor_cleanup(ov);
          info = g_strdup_printf(",%s=%s", prop->name, str);
          g_strlcat(nf->info_str, info, sizeof(nf->info_str));
+        if (queues > 1) {
+            g_hash_table_insert(proptable, prop->name, g_strdup(str));
+        }
          g_free(str);
          g_free(info);
      }
+
+    for (i = 1; i < queues; i++) {
+        obj = object_new(object_get_typename(OBJECT(nf)));
+        /* set filter properties */
+        nfq = NETFILTER(obj);
+        nfq->name = g_strdup(nf->name);
+        nfq->netdev = ncs[i];
+        snprintf(nfq->info_str, sizeof(nfq->info_str), "%s",
 nf->info_str);
+
+        g_hash_table_iter_init(&iter, proptable);
+        while (g_hash_table_iter_next(&iter, &key, &value)) {
+            object_property_parse(obj, (char *)value, (char *)key,
 &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }
+
+        /* add other queue's filter object as the first object's

child */

net/filter.c +11 -0
+        name = g_strdup_printf("%s-queue-%d", nf->name, i);
+        object_property_add_child(OBJECT(nf), name, obj, &local_err);
+        g_free(name);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            goto out;
+        }
+
+        /* setup filter */
+        nfqc = NETFILTER_GET_CLASS(obj);
+        if (nfqc->setup) {
Quoted 8 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 262

Quoted 8 lines
net/filter.c +8 -0
+            nfqc->setup(nfq, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }
+        QTAILQ_INSERT_TAIL(&nfq->netdev->filters, nfq, next);
+        object_unref(obj);
            ^^^^^^^^^^^^
Quoted 10 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 270

Quoted 8 lines
net/filter.c +8 -0
+    }
+
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    return;
+
+out:

We may leak objects here.

Quoted 2 lines

Seems not. see above object_unref(), if we come to here, object create in previous iter will be unrefed.

Maybe I miss something. But there's still refcnt for objects created in previous iters? (I mean object_property_add_child() will add another refcnt?)

Quoted 24 lines
Quoted 10 lines
Quoted 10 lines
net/filter.c +6 -0
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    if (obj) {
+        object_unref(obj);
+    }
  }
Quoted 4 lines
Quoted 1 line

static void netfilter_class_init(ObjectClass *oc, void *data)

To reduce the review iterations, I suggest to drop the patch also. Multiqueue support could be another series on top.

I agree, thank you.

Quoted 6 lines

Thanks

.

#60 lore raw

Re: [Qemu-devel] [PATCH v11 12/12] netfilter: add multiqueue support

On 09/22/2015 04:31 PM, Jason Wang wrote:
[...]

Quoted 24 lines
Quoted 24 lines
Quoted 11 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 262

Quoted 11 lines
net/filter.c +11 -0
+        /* setup filter */
+        nfqc = NETFILTER_GET_CLASS(obj);
+        if (nfqc->setup) {
+            nfqc->setup(nfq, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }
+        QTAILQ_INSERT_TAIL(&nfq->netdev->filters, nfq, next);
+        object_unref(obj);
             ^^^^^^^^^^^^
Quoted 10 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 270

Quoted 8 lines
net/filter.c +8 -0
+    }
+
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    return;
+
+out:

We may leak objects here.

Quoted 6 lines
Quoted 2 lines

Seems not. see above object_unref(), if we come to here, object create in previous iter will be unrefed.

Maybe I miss something. But there's still refcnt for objects created in previous iters? (I mean object_property_add_child() will add another refcnt?)

This is intended, parent need to hold a refcnt of it's child. When we delete the parent, it will release the child refcnt, and it's child obj will be automatically deleted because the refcnt is decreased to 0.
The object_unref() release the refcnt hold by object_new().

Quoted 28 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
net/filter.c +6 -0
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    if (obj) {
+        object_unref(obj);
+    }
   }
Quoted 14 lines
Quoted 4 lines
Quoted 1 line

static void netfilter_class_init(ObjectClass *oc, void *data)

To reduce the review iterations, I suggest to drop the patch also. Multiqueue support could be another series on top.

I agree, thank you.

Quoted 6 lines

Thanks

.

.

Signature · 2 lines
Thanks,
Yang.
#61 lore raw

Re: [Qemu-devel] [PATCH v11 12/12] netfilter: add multiqueue support

Quoted 35 lines · Yang Hongyang

On 09/22/2015 04:31 PM, Jason Wang wrote:
[...]

Quoted 24 lines
Quoted 24 lines
Quoted 11 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 262

Quoted 11 lines
net/filter.c +11 -0
+        /* setup filter */
+        nfqc = NETFILTER_GET_CLASS(obj);
+        if (nfqc->setup) {
+            nfqc->setup(nfq, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }
+        QTAILQ_INSERT_TAIL(&nfq->netdev->filters, nfq, next);
+        object_unref(obj);
             ^^^^^^^^^^^^
Quoted 10 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 270

Quoted 8 lines
net/filter.c +8 -0
+    }
+
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    return;
+
+out:

We may leak objects here.

Quoted 6 lines
Quoted 2 lines

Seems not. see above object_unref(), if we come to here, object create in previous iter will be unrefed.

Maybe I miss something. But there's still refcnt for objects created in previous iters? (I mean object_property_add_child() will add another refcnt?)

Quoted 5 lines

This is intended, parent need to hold a refcnt of it's child. When we delete the parent, it will release the child refcnt, and it's child obj will be
automatically deleted because the refcnt is decreased to 0. The object_unref() release the refcnt hold by object_new().

I see. Thanks.

#62 lore raw

Re: [Qemu-devel] [PATCH v11 12/12] netfilter: add multiqueue support

In reply to Jason Wang
Quoted 74 lines · Jason Wang
Quoted 71 lines · Yang Hongyang

From: Yang Hongyang <[email protected]>

add multiqueue support, if there's multiqueue, we add multi netfilter objects, other netfilter objects is the child of the first added netfilter object. So when we delete a netfilter, the other netfilter objects we added will be automatically deleted.

---
v11: initial patch

---
  net/filter.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
  1 file changed, 79 insertions(+), 7 deletions(-)
net/filter.c +22 -7
index aea619a..cc27528 100644
@@ -142,16 +142,25 @@ static void netfilter_finalize(Object *obj)
      g_free(nf->name);
  }
 
+static void proptb_free_val_func(gpointer data)
+{
+    g_free(data);
+}
+
  static void netfilter_complete(UserCreatable *uc, Error **errp)
  {
-    NetFilterState *nf = NETFILTER(uc);
+    NetFilterState *nf = NETFILTER(uc), *nfq = NULL;
      NetClientState *ncs[MAX_QUEUE_NUM];
-    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
-    int queues;
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc), *nfqc = NULL;
+    int queues, i;
      Error *local_err = NULL;
-    char *str, *info;
+    char *str, *info, *name;
      ObjectProperty *prop;
      StringOutputVisitor *ov;
+    Object *obj = NULL;
+    GHashTable *proptable = NULL;
+    GHashTableIter iter;
+    gpointer key, value;
 
      if (!nf->netdev_id) {
          error_setg(errp, "Parameter 'netdev' is required");
@@ -165,9 +174,6 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
          error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
                     "a network backend id");
          return;
-    } else if (queues > 1) {
-        error_setg(errp, "Multi queue is not supported");
-        return;
      }
 
      if (get_vhost_net(ncs[0])) {
@@ -187,6 +193,17 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
      }
      QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
 
+    if (queues > 1) {
+        /*
+         * Store the properties of the filter except "type" property.
+         * When there's multiqueue, we will create a new filter object
+         * of the same type and same properties. this hashtable is used
+         * to set newly created object properties.
+         */
+        proptable = g_hash_table_new_full(NULL, NULL, NULL,
+                                          proptb_free_val_func);
Quoted 8 lines

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 205

Quoted 1 line
net/filter.c +1 -0
+    }

I'm thinking whether or not duplicate all the properties in each netfilters is a good method. Maybe we can have a another ojbect with array of pointers to NetFilter objects embedded? Another question is whether or not we need to do this at this level. Maybe we can make the necessary Netfilter multiqueue aware. E.g let buffer filter to have multiqueue also? Then you may only need a single timer?

Sorry I don't get it at first. I also thought about make the buffer filter to have multiqueue, but there comes problem, how to distinguish which queue we should go in when receive the packet, we need to add a mechanism to distinguish which queue belongs to which net client's queue, that will be more complex, while current multiqueue implementation of net clients is multiple net clients with the same name, current solution is the simplest solution I can think of...

Quoted 82 lines
Quoted 51 lines
net/filter.c +40 -0
+
      /* generate info str */
      QTAILQ_FOREACH(prop, &OBJECT(nf)->properties, node) {
          if (!strcmp(prop->name, "type")) {
@@ -199,9 +216,64 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
          string_output_visitor_cleanup(ov);
          info = g_strdup_printf(",%s=%s", prop->name, str);
          g_strlcat(nf->info_str, info, sizeof(nf->info_str));
+        if (queues > 1) {
+            g_hash_table_insert(proptable, prop->name, g_strdup(str));
+        }
          g_free(str);
          g_free(info);
      }
+
+    for (i = 1; i < queues; i++) {
+        obj = object_new(object_get_typename(OBJECT(nf)));
+        /* set filter properties */
+        nfq = NETFILTER(obj);
+        nfq->name = g_strdup(nf->name);
+        nfq->netdev = ncs[i];
+        snprintf(nfq->info_str, sizeof(nfq->info_str), "%s", nf->info_str);
+
+        g_hash_table_iter_init(&iter, proptable);
+        while (g_hash_table_iter_next(&iter, &key, &value)) {
+            object_property_parse(obj, (char *)value, (char *)key, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }
+
+        /* add other queue's filter object as the first object's child */
+        name = g_strdup_printf("%s-queue-%d", nf->name, i);
+        object_property_add_child(OBJECT(nf), name, obj, &local_err);
+        g_free(name);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            goto out;
+        }
+
+        /* setup filter */
+        nfqc = NETFILTER_GET_CLASS(obj);
+        if (nfqc->setup) {
+            nfqc->setup(nfq, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                goto out;
+            }
+        }

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 270

Quoted 10 lines
net/filter.c +10 -0
+        QTAILQ_INSERT_TAIL(&nfq->netdev->filters, nfq, next);
+        object_unref(obj);
+    }
+
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    return;
+
+out:

We may leak objects here.

net/filter.c static void netfilter_complete(UserCreatable *uc, Error **errp) · line 279

Quoted 9 lines
net/filter.c +6 -0
+    if (proptable) {
+        g_hash_table_unref(proptable);
+    }
+    if (obj) {
+        object_unref(obj);
+    }
  }
 
  static void netfilter_class_init(ObjectClass *oc, void *data)

To reduce the review iterations, I suggest to drop the patch also. Multiqueue support could be another series on top.

Thanks

.

Signature · 2 lines
Thanks,
Yang.
#63 lore raw

Re: [Qemu-devel] [PATCH v11 12/12] netfilter: add multiqueue support

Quoted 89 lines · Yang Hongyang
Quoted 79 lines · Jason Wang
Quoted 76 lines · Yang Hongyang

From: Yang Hongyang <[email protected]>

add multiqueue support, if there's multiqueue, we add multi netfilter objects, other netfilter objects is the child of the first added netfilter
object. So when we delete a netfilter, the other netfilter objects we added will be automatically deleted.

---
v11: initial patch

---
  net/filter.c | 86
+++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
  1 file changed, 79 insertions(+), 7 deletions(-)
net/filter.c +13 -7
index aea619a..cc27528 100644
@@ -142,16 +142,25 @@ static void netfilter_finalize(Object *obj)
      g_free(nf->name);
  }
 
+static void proptb_free_val_func(gpointer data)
+{
+    g_free(data);
+}
+
  static void netfilter_complete(UserCreatable *uc, Error **errp)
  {
-    NetFilterState *nf = NETFILTER(uc);
+    NetFilterState *nf = NETFILTER(uc), *nfq = NULL;
      NetClientState *ncs[MAX_QUEUE_NUM];
-    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
-    int queues;
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc), *nfqc = NULL;
+    int queues, i;
      Error *local_err = NULL;
-    char *str, *info;
+    char *str, *info, *name;
      ObjectProperty *prop;
      StringOutputVisitor *ov;
+    Object *obj = NULL;
+    GHashTable *proptable = NULL;
+    GHashTableIter iter;
+    gpointer key, value;
 
      if (!nf->netdev_id) {
          error_setg(errp, "Parameter 'netdev' is required");
@@ -165,9 +174,6 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
          error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
                     "a network backend id");
          return;
-    } else if (queues > 1) {
-        error_setg(errp, "Multi queue is not supported");
-        return;
      }
 
      if (get_vhost_net(ncs[0])) {
net/filter.c +5 -0
@@ -187,6 +193,17 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
      }
      QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
 
+    if (queues > 1) {
+        /*
+         * Store the properties of the filter except "type" property.
+         * When there's multiqueue, we will create a new filter object
+         * of the same type and same properties. this hashtable is

used

net/filter.c +4 -0
+         * to set newly created object properties.
+         */
+        proptable = g_hash_table_new_full(NULL, NULL, NULL,
+                                          proptb_free_val_func);
Quoted 8 lines
Quoted 1 line

+ }

I'm thinking whether or not duplicate all the properties in each netfilters is a good method. Maybe we can have a another ojbect with array of pointers to NetFilter objects embedded? Another question is whether or not we need to do this at this level. Maybe we can make the necessary Netfilter multiqueue aware. E.g let buffer filter to have multiqueue also? Then you may only need a single timer?

Quoted 9 lines

Sorry I don't get it at first. I also thought about make the buffer filter to
have multiqueue, but there comes problem, how to distinguish which queue we should go in when receive the packet, we need to add a mechanism to distinguish which queue belongs to which net client's queue, that will be more complex, while current multiqueue implementation of net clients is multiple net clients with the same name, current solution is the simplest
solution I can think of...

I'm not sure I get this. But there's a queue_index filed in each NetClientState which may help in this case.

#64 lore raw

Re: [Qemu-devel] [PATCH v11 12/12] netfilter: add multiqueue support

Quoted 102 lines · Jason Wang
Quoted 89 lines · Yang Hongyang
Quoted 79 lines · Jason Wang
Quoted 76 lines · Yang Hongyang

From: Yang Hongyang <[email protected]>

add multiqueue support, if there's multiqueue, we add multi netfilter objects, other netfilter objects is the child of the first added netfilter
object. So when we delete a netfilter, the other netfilter objects we added will be automatically deleted.

---
v11: initial patch

---
   net/filter.c | 86
+++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
   1 file changed, 79 insertions(+), 7 deletions(-)
net/filter.c +13 -7
index aea619a..cc27528 100644
@@ -142,16 +142,25 @@ static void netfilter_finalize(Object *obj)
       g_free(nf->name);
   }
 
+static void proptb_free_val_func(gpointer data)
+{
+    g_free(data);
+}
+
   static void netfilter_complete(UserCreatable *uc, Error **errp)
   {
-    NetFilterState *nf = NETFILTER(uc);
+    NetFilterState *nf = NETFILTER(uc), *nfq = NULL;
       NetClientState *ncs[MAX_QUEUE_NUM];
-    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
-    int queues;
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc), *nfqc = NULL;
+    int queues, i;
       Error *local_err = NULL;
-    char *str, *info;
+    char *str, *info, *name;
       ObjectProperty *prop;
       StringOutputVisitor *ov;
+    Object *obj = NULL;
+    GHashTable *proptable = NULL;
+    GHashTableIter iter;
+    gpointer key, value;
 
       if (!nf->netdev_id) {
           error_setg(errp, "Parameter 'netdev' is required");
@@ -165,9 +174,6 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
           error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
                      "a network backend id");
           return;
-    } else if (queues > 1) {
-        error_setg(errp, "Multi queue is not supported");
-        return;
       }
 
       if (get_vhost_net(ncs[0])) {
net/filter.c +5 -0
@@ -187,6 +193,17 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
       }
       QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
 
+    if (queues > 1) {
+        /*
+         * Store the properties of the filter except "type" property.
+         * When there's multiqueue, we will create a new filter object
+         * of the same type and same properties. this hashtable is

used

net/filter.c +4 -0
+         * to set newly created object properties.
+         */
+        proptable = g_hash_table_new_full(NULL, NULL, NULL,
+                                          proptb_free_val_func);
Quoted 8 lines
Quoted 1 line

+ }

I'm thinking whether or not duplicate all the properties in each netfilters is a good method. Maybe we can have a another ojbect with array of pointers to NetFilter objects embedded? Another question is whether or not we need to do this at this level. Maybe we can make the necessary Netfilter multiqueue aware. E.g let buffer filter to have multiqueue also? Then you may only need a single timer?

Quoted 9 lines

Sorry I don't get it at first. I also thought about make the buffer filter to
have multiqueue, but there comes problem, how to distinguish which queue we should go in when receive the packet, we need to add a mechanism to distinguish which queue belongs to which net client's queue, that will be more complex, while current multiqueue implementation of net clients is multiple net clients with the same name, current solution is the simplest
solution I can think of...

Quoted 2 lines

I'm not sure I get this. But there's a queue_index filed in each NetClientState which may help in this case.

Ah, I see, thank you. There's another reason I do this in filter abstract layer is that the concrete filter implementation do not need to deal with the multiqueue, will simplify the filter implement a lot, otherwise, every filter need to implement it's own multiqueue support.

Quoted 4 lines

.

Signature · 2 lines
Thanks,
Yang.
#65 lore raw

Re: [Qemu-devel] [PATCH v11 12/12] netfilter: add multiqueue support

Quoted 106 lines · Yang Hongyang
Quoted 105 lines · Jason Wang
Quoted 101 lines · Yang Hongyang
Quoted 81 lines · Jason Wang
Quoted 78 lines · Yang Hongyang

From: Yang Hongyang <[email protected]>

add multiqueue support, if there's multiqueue, we add multi netfilter objects, other netfilter objects is the child of the first added netfilter
object. So when we delete a netfilter, the other netfilter objects we added will be automatically deleted.

---
v11: initial patch

---
   net/filter.c | 86
+++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
   1 file changed, 79 insertions(+), 7 deletions(-)
net/filter.c +13 -7
index aea619a..cc27528 100644
@@ -142,16 +142,25 @@ static void netfilter_finalize(Object *obj)
       g_free(nf->name);
   }
 
+static void proptb_free_val_func(gpointer data)
+{
+    g_free(data);
+}
+
   static void netfilter_complete(UserCreatable *uc, Error **errp)
   {
-    NetFilterState *nf = NETFILTER(uc);
+    NetFilterState *nf = NETFILTER(uc), *nfq = NULL;
       NetClientState *ncs[MAX_QUEUE_NUM];
-    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
-    int queues;
+    NetFilterClass *nfc = NETFILTER_GET_CLASS(uc), *nfqc = NULL;
+    int queues, i;
       Error *local_err = NULL;
-    char *str, *info;
+    char *str, *info, *name;
       ObjectProperty *prop;
       StringOutputVisitor *ov;
+    Object *obj = NULL;
+    GHashTable *proptable = NULL;
+    GHashTableIter iter;
+    gpointer key, value;
 
       if (!nf->netdev_id) {
           error_setg(errp, "Parameter 'netdev' is required");
@@ -165,9 +174,6 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
           error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
                      "a network backend id");
           return;
-    } else if (queues > 1) {
-        error_setg(errp, "Multi queue is not supported");
-        return;
       }
 
       if (get_vhost_net(ncs[0])) {
net/filter.c +3 -0
@@ -187,6 +193,17 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
       }
       QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
 
+    if (queues > 1) {
+        /*
+         * Store the properties of the filter except "type"

property.
+ * When there's multiqueue, we will create a new filter
object
+ * of the same type and same properties. this hashtable is
used

net/filter.c +4 -0
+         * to set newly created object properties.
+         */
+        proptable = g_hash_table_new_full(NULL, NULL, NULL,
+                                          proptb_free_val_func);
Quoted 8 lines
Quoted 1 line

+ }

I'm thinking whether or not duplicate all the properties in each netfilters is a good method. Maybe we can have a another ojbect with array of pointers to NetFilter objects embedded? Another question is whether or not we need to do this at this level. Maybe we can make the necessary Netfilter multiqueue aware. E.g let buffer filter to have multiqueue also? Then you may only need a single timer?

Sorry I don't get it at first. I also thought about make the buffer filter to
have multiqueue, but there comes problem, how to distinguish which queue
we should go in when receive the packet, we need to add a mechanism to distinguish which queue belongs to which net client's queue, that will be more complex, while current multiqueue implementation of net clients is multiple net clients with the same name, current solution is the simplest
solution I can think of...

Quoted 8 lines
Quoted 2 lines

I'm not sure I get this. But there's a queue_index filed in each NetClientState which may help in this case.

Ah, I see, thank you. There's another reason I do this in filter abstract layer is that the concrete filter implementation do not need to deal with the
multiqueue, will simplify the filter implement a lot, otherwise, every filter need to implement it's own multiqueue support.

Probably not, just pointing all queues to a single netfilter object may be sufficient (e.g for dump). And it's much easier for some kind of filter (e.g traffic throttling) if it was multiqueue aware.

#66 lore raw

Re: [Qemu-devel] [PATCH v11 12/12] netfilter: add multiqueue support

On 09/22/2015 05:30 PM, Jason Wang wrote:
[...]

Quoted 60 lines
Quoted 51 lines
Quoted 51 lines
Quoted 50 lines
Quoted 31 lines
Quoted 31 lines
        if (!nf->netdev_id) {
            error_setg(errp, "Parameter 'netdev' is required");
net/filter.c +0 -3
@@ -165,9 +174,6 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
            error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
                       "a network backend id");
            return;
-    } else if (queues > 1) {
-        error_setg(errp, "Multi queue is not supported");
-        return;
        }
 
        if (get_vhost_net(ncs[0])) {
net/filter.c +3 -0
@@ -187,6 +193,17 @@ static void netfilter_complete(UserCreatable
 *uc, Error **errp)
        }
        QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
 
+    if (queues > 1) {
+        /*
+         * Store the properties of the filter except "type"

property.
+ * When there's multiqueue, we will create a new filter
object
+ * of the same type and same properties. this hashtable is
used

net/filter.c +4 -0
+         * to set newly created object properties.
+         */
+        proptable = g_hash_table_new_full(NULL, NULL, NULL,
+                                          proptb_free_val_func);
Quoted 8 lines
Quoted 1 line

+ }

I'm thinking whether or not duplicate all the properties in each netfilters is a good method. Maybe we can have a another ojbect with array of pointers to NetFilter objects embedded? Another question is whether or not we need to do this at this level. Maybe we can make the necessary Netfilter multiqueue aware. E.g let buffer filter to have multiqueue also? Then you may only need a single timer?

Sorry I don't get it at first. I also thought about make the buffer filter to
have multiqueue, but there comes problem, how to distinguish which queue
we should go in when receive the packet, we need to add a mechanism to distinguish which queue belongs to which net client's queue, that will be more complex, while current multiqueue implementation of net clients is multiple net clients with the same name, current solution is the simplest
solution I can think of...

Quoted 8 lines
Quoted 2 lines

I'm not sure I get this. But there's a queue_index filed in each NetClientState which may help in this case.

Ah, I see, thank you. There's another reason I do this in filter abstract layer is that the concrete filter implementation do not need to deal with the
multiqueue, will simplify the filter implement a lot, otherwise, every filter need to implement it's own multiqueue support.

Quoted 3 lines

Probably not, just pointing all queues to a single netfilter object may be sufficient (e.g for dump). And it's much easier for some kind of filter (e.g traffic throttling) if it was multiqueue aware.

I see, will think about doing this other way, thank you!

Quoted 3 lines

.

Signature · 2 lines
Thanks,
Yang.
#67 lore raw
In reply to Yang Hongyang
Quoted 109 lines · Yang Hongyang

This patch add an netfilter abstract object, captures all network packets on associated netdev. Also implement a concrete filter buffer based on this abstract object. the "buffer" netfilter could be used by VM FT solutions like MicroCheckpointing, to buffer/release packets. Or to simulate packet delay.

You can also get the series from:
https://github.com/macrosheep/qemu/tree/netfilter-v11

Usage:
-netdev tap,id=bn0
-device e1000,netdev=bn0
-object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

dynamically add/remove netfilters:
object_add filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000 object_del f0

NOTE:
interval's scale is microsecond.
chain is optional, and is one of in|out|all, default is "all".
"in" means this filter will receive packets sent to the @netdev "out" means this filter will receive packets sent from the @netdev "all" means this filter will receive packets both sent to/from
the @netdev

v11:
- address Jason&Daniel's comments
- add multiqueue support, the last 2 patches
- rebased to the latest master

v10:
- Reimplemented using QOM (suggested by stefan)
- Do not export NetQueue internals (suggested by stefan)
- see individual patch for detail

v9:
- squash command description and help to patch 1&3
- qapi changes according to Markus&Eric's comments
- see individual patch for detail

v8:
- some minor fixes according to Thomas's comments
- rebased to the latest master branch

v7:
- print filter info when execute 'info network'
- addressed Jason's comments

v6:
- add multiqueue support, please see individual patch for detail

v5:
- add a sent_cb param to filter receive_iov api
- squash the 4th patch into patch 3
- remove dummy sent_cb (buffer filter)
- addressed Jason's other comments, see individual patches for detail

v4:
- get rid of struct Filter
- squash the 4th patch into patch 2
- fix qemu_netfilter_pass_to_next_iov
- get rid of bh (buffer filter)
- release the packet to next filter instead of to receiver (buffer filter)

v3:
- add an api to pass the packet to next filter
- remove netfilters when delete netdev
- add qtest testcases for netfilter
- addressed comments from Jason

v2:
- add a chain option to netfilter object
- move the hook place earlier, before net_queue_send
- drop the unused api in buffer filter
- squash buffer filter patches into one
- remove receive() api from netfilter, only receive_iov() is enough
- addressed comments from Jason&Thomas

v1:
initial patch.

Yang Hongyang (12):
qmp: delete qemu opts when delete an object init/cleanup of netfilter object
netfilter: hook packets before net queue send
net: merge qemu_deliver_packet and qemu_deliver_packet_iov net/queue: introduce NetQueueDeliverFunc
netfilter: add an API to pass the packet to next filter
netfilter: print filter info associate with the netdev net/queue: export qemu_net_queue_append_iov
netfilter: add a netbuffer filter
tests: add test cases for netfilter object netfilter/multiqueue: introduce netfilter name
netfilter: add multiqueue support

 include/net/filter.h    |  76 ++++++++++++
 include/net/net.h       |   6 +-
 include/net/queue.h     |  20 +++-
 include/qemu/typedefs.h |   1 +
 net/Makefile.objs       |   2 +
 net/filter-buffer.c     | 170 +++++++++++++++++++++++++++
 net/filter.c            | 306 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/net.c               | 108 +++++++++++++----
 net/queue.c             |  24 ++--
 qapi-schema.json        |  18 +++
 qemu-options.hx         |  18 +++
 qmp.c                   |   4 +
 tests/.gitignore        |   1 +
Quoted 9 lines
 tests/Makefile          |   2 +
 tests/test-netfilter.c  | 200 +++++++++++++++++++++++++++++++
 vl.c                    |  18 ++-
 16 files changed, 927 insertions(+), 47 deletions(-)
 create mode 100644 include/net/filter.h
 create mode 100644 net/filter-buffer.c
 create mode 100644 net/filter.c
 create mode 100644 tests/test-netfilter.c

Want to merge this. But have comments on patch 4 and multiqueue patches. I suggest to drop them in next version so I can merge to reduce the review iterations. Other optimizations and fixups could be done on top.

Thanks

#68 lore raw
Quoted 1 line · Jason Wang

[...]

Quoted 5 lines
Quoted 1 line

Want to merge this. But have comments on patch 4 and multiqueue patches. I suggest to drop them in next version so I can merge to reduce the review iterations. Other optimizations and fixups could be done on top.

Thanks a lot! I agree that we could drop the last 2 patches
which introduce multiqueue support, but the patch 4 is required by the following patch, so we can not simply drop it, please see my reply, thank you.

Quoted 5 lines

Thanks

.

Signature · 2 lines
Thanks,
Yang.
#69 lore raw
In reply to Yang Hongyang
Quoted 109 lines · Yang Hongyang

This patch add an netfilter abstract object, captures all network packets on associated netdev. Also implement a concrete filter buffer based on this abstract object. the "buffer" netfilter could be used by VM FT solutions like MicroCheckpointing, to buffer/release packets. Or to simulate packet delay.

You can also get the series from:
https://github.com/macrosheep/qemu/tree/netfilter-v11

Usage:
-netdev tap,id=bn0
-device e1000,netdev=bn0
-object filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000

dynamically add/remove netfilters:
object_add filter-buffer,id=f0,netdev=bn0,chain=in,interval=1000 object_del f0

NOTE:
interval's scale is microsecond.
chain is optional, and is one of in|out|all, default is "all".
"in" means this filter will receive packets sent to the @netdev "out" means this filter will receive packets sent from the @netdev "all" means this filter will receive packets both sent to/from
the @netdev

v11:
- address Jason&Daniel's comments
- add multiqueue support, the last 2 patches
- rebased to the latest master

v10:
- Reimplemented using QOM (suggested by stefan)
- Do not export NetQueue internals (suggested by stefan)
- see individual patch for detail

v9:
- squash command description and help to patch 1&3
- qapi changes according to Markus&Eric's comments
- see individual patch for detail

v8:
- some minor fixes according to Thomas's comments
- rebased to the latest master branch

v7:
- print filter info when execute 'info network'
- addressed Jason's comments

v6:
- add multiqueue support, please see individual patch for detail

v5:
- add a sent_cb param to filter receive_iov api
- squash the 4th patch into patch 3
- remove dummy sent_cb (buffer filter)
- addressed Jason's other comments, see individual patches for detail

v4:
- get rid of struct Filter
- squash the 4th patch into patch 2
- fix qemu_netfilter_pass_to_next_iov
- get rid of bh (buffer filter)
- release the packet to next filter instead of to receiver (buffer filter)

v3:
- add an api to pass the packet to next filter
- remove netfilters when delete netdev
- add qtest testcases for netfilter
- addressed comments from Jason

v2:
- add a chain option to netfilter object
- move the hook place earlier, before net_queue_send
- drop the unused api in buffer filter
- squash buffer filter patches into one
- remove receive() api from netfilter, only receive_iov() is enough
- addressed comments from Jason&Thomas

v1:
initial patch.

Yang Hongyang (12):
qmp: delete qemu opts when delete an object init/cleanup of netfilter object
netfilter: hook packets before net queue send
net: merge qemu_deliver_packet and qemu_deliver_packet_iov net/queue: introduce NetQueueDeliverFunc
netfilter: add an API to pass the packet to next filter
netfilter: print filter info associate with the netdev net/queue: export qemu_net_queue_append_iov
netfilter: add a netbuffer filter
tests: add test cases for netfilter object netfilter/multiqueue: introduce netfilter name
netfilter: add multiqueue support

 include/net/filter.h    |  76 ++++++++++++
 include/net/net.h       |   6 +-
 include/net/queue.h     |  20 +++-
 include/qemu/typedefs.h |   1 +
 net/Makefile.objs       |   2 +
 net/filter-buffer.c     | 170 +++++++++++++++++++++++++++
 net/filter.c            | 306 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/net.c               | 108 +++++++++++++----
 net/queue.c             |  24 ++--
 qapi-schema.json        |  18 +++
 qemu-options.hx         |  18 +++
 qmp.c                   |   4 +
 tests/.gitignore        |   1 +
Quoted 9 lines
 tests/Makefile          |   2 +
 tests/test-netfilter.c  | 200 +++++++++++++++++++++++++++++++
 vl.c                    |  18 ++-
 16 files changed, 927 insertions(+), 47 deletions(-)
 create mode 100644 include/net/filter.h
 create mode 100644 net/filter-buffer.c
 create mode 100644 net/filter.c
 create mode 100644 tests/test-netfilter.c

Apply patch 1 to patch 10 (with tweak on patch 4 for single iov).

https://github.com/jasowang/qemu/commits/net

Thanks a lot.