Skip to contentSkip to conversation

Add status on/off switching to filter-redirector

PATCH In review Jason Wang 2 participants 13 messages Jan 4 – Jan 15 Networking

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

cover Add status on/off switching to filter-redirector

Hi All:

This series adds status on/off switching to filter-redirector with qtests.

Please review.

Jason Wang (3):
net/filter-redirector: add support for dynamic status on/off switching
qtest: add a test to test redirector status change tests/qtest: Add test for filter-redirector rx event opened

 net/filter-mirror.c                  |  38 +++-
 tests/qtest/test-filter-redirector.c | 288 +++++++++++++++++++++++++++
 2 files changed, 321 insertions(+), 5 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/3 net/filter-redirector: add support for dynamic status on/off switching

Currently, filter-redirector does not implement the status_changed callback, which means the 'status' property cannot be used to dynamically enable/disable the filter at runtime. When status is set to 'off' via QMP/HMP, the filter still receives data from the indev chardev because the chardev handlers remain registered.

This patch adds proper support for the 'status' property:

1. Implement filter_redirector_status_changed() callback:
- When status changes to 'off': remove chardev read handlers
- When status changes to 'on': re-register chardev handlers (only if chardev is already open)

2. Update filter_redirector_setup() to respect initial status:
- If filter is created with status=off, do not register handlers
- This allows creating disabled filters via command line or QMP

3. Handle chardev OPENED/CLOSED events to re-arm handlers on reconnect:
- Keep the chr_event callback installed on CLOSE so a later OPENED

     can re-register the read handlers when nf->on

- Use qemu_chr_fe_set_handlers_full(..., set_open=false, sync_state=false) instead of qemu_chr_fe_set_handlers() because the latter forces sync_state=true and may emit CHR_EVENT_OPENED for an already-open backend. Doing that from inside the chr_event callback would cause recursive/re-entrant OPENED handling.

---
 net/filter-mirror.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
net/filter-mirror.c +33 -5
index 8e01e98f40..aa2ab452fd 100644
@@ -179,9 +179,16 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event)
     MirrorState *s = FILTER_REDIRECTOR(nf);
 
     switch (event) {
+    case CHR_EVENT_OPENED:
+        if (nf->on) {
+            qemu_chr_fe_set_handlers_full(&s->chr_in, redirector_chr_can_read,
+                                          redirector_chr_read, redirector_chr_event,
+                                          NULL, nf, NULL, false, false);
+        }
+        break;
     case CHR_EVENT_CLOSED:
-        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
-                                 NULL, NULL, NULL, true);
+        qemu_chr_fe_set_handlers_full(&s->chr_in, NULL, NULL, redirector_chr_event,
+                                      NULL, nf, NULL, false, false);
         break;
     default:
         break;
@@ -306,9 +313,11 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
             return;
         }
 
-        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
-                                 redirector_chr_read, redirector_chr_event,
-                                 NULL, nf, NULL, true);
+        if (nf->on) {
+            qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                     redirector_chr_read, redirector_chr_event,
+                                     NULL, nf, NULL, true);
+        }
     }
 
     if (s->outdev) {
@@ -324,6 +333,24 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
     }
 }
 
+static void filter_redirector_status_changed(NetFilterState *nf, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+
+    if (!s->indev) {
+        return;
+    }
+
+    if (nf->on) {
+        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                 redirector_chr_read, redirector_chr_event,
+                                 NULL, nf, NULL, true);
+    } else {
+        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
+                                 NULL, NULL, NULL, true);
+    }
+}
+
 static char *filter_redirector_get_indev(Object *obj, Error **errp)
 {
     MirrorState *s = FILTER_REDIRECTOR(obj);
@@ -440,6 +467,7 @@ static void filter_redirector_class_init(ObjectClass *oc, const void *data)
     nfc->setup = filter_redirector_setup;
     nfc->cleanup = filter_redirector_cleanup;
     nfc->receive_iov = filter_redirector_receive_iov;
+    nfc->status_changed = filter_redirector_status_changed;
 }
 
 static void filter_mirror_init(Object *obj)
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/3] net/filter-redirector: add support for dynamic status on/off switching

Quoted 71 lines · Jason Wang

Currently, filter-redirector does not implement the status_changed callback, which means the 'status' property cannot be used to dynamically enable/disable the filter at runtime. When status is set to 'off' via QMP/HMP, the filter still receives data from the indev chardev because the chardev handlers remain registered.

This patch adds proper support for the 'status' property:

1. Implement filter_redirector_status_changed() callback:
- When status changes to 'off': remove chardev read handlers
- When status changes to 'on': re-register chardev handlers (only if chardev is already open)

2. Update filter_redirector_setup() to respect initial status:
- If filter is created with status=off, do not register handlers
- This allows creating disabled filters via command line or QMP

3. Handle chardev OPENED/CLOSED events to re-arm handlers on reconnect:
- Keep the chr_event callback installed on CLOSE so a later OPENED

     can re-register the read handlers when nf->on

- Use qemu_chr_fe_set_handlers_full(..., set_open=false, sync_state=false) instead of qemu_chr_fe_set_handlers() because the latter forces sync_state=true and may emit CHR_EVENT_OPENED for an already-open backend. Doing that from inside the chr_event callback would cause recursive/re-entrant OPENED handling.

---
 net/filter-mirror.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
net/filter-mirror.c +14 -5
index 8e01e98f40..aa2ab452fd 100644
@@ -179,9 +179,16 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event)
     MirrorState *s = FILTER_REDIRECTOR(nf);
 
     switch (event) {
+    case CHR_EVENT_OPENED:
+        if (nf->on) {
+            qemu_chr_fe_set_handlers_full(&s->chr_in, redirector_chr_can_read,
+                                          redirector_chr_read, redirector_chr_event,
+                                          NULL, nf, NULL, false, false);
+        }
+        break;
     case CHR_EVENT_CLOSED:
-        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
-                                 NULL, NULL, NULL, true);
+        qemu_chr_fe_set_handlers_full(&s->chr_in, NULL, NULL, redirector_chr_event,
+                                      NULL, nf, NULL, false, false);
         break;
     default:
         break;
@@ -306,9 +313,11 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
             return;
         }
 
-        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
-                                 redirector_chr_read, redirector_chr_event,
-                                 NULL, nf, NULL, true);
+        if (nf->on) {
+            qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                     redirector_chr_read, redirector_chr_event,
+                                     NULL, nf, NULL, true);
+        }
     }
 
     if (s->outdev) {

net/filter-mirror.c static void filter_redirector_setup(NetFilterState *nf, Error **errp) · line 340

Quoted 8 lines
net/filter-mirror.c +5 -0
@@ -324,6 +333,24 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
     }
 }
 
+static void filter_redirector_status_changed(NetFilterState *nf, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+
+    if (!s->indev) {

It's better to add a error here, for example:

error_setg(errp, "filter_redirector_status_changed failed for
the s->indev cleared" );

net/filter-mirror.c static void filter_redirector_setup(NetFilterState *nf, Error **errp) · line 350

Quoted 10 lines
net/filter-mirror.c +10 -0
+        return;
+    }
+
+    if (nf->on) {
+        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                 redirector_chr_read, redirector_chr_event,
+                                 NULL, nf, NULL, true);
+    } else {
+        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
+                                 NULL, NULL, NULL, true);

It seems like we need to keep the "redirector_chr_event" here?

And another thing, this series focus on indev, should we need to do the same thing for outdev?

Thanks
Chen

Quoted 17 lines
net/filter-mirror.c +4 -1
+    }
+}
+
 static char *filter_redirector_get_indev(Object *obj, Error **errp)
 {
     MirrorState *s = FILTER_REDIRECTOR(obj);
@@ -440,6 +467,7 @@ static void filter_redirector_class_init(ObjectClass *oc, const void *data)
     nfc->setup = filter_redirector_setup;
     nfc->cleanup = filter_redirector_cleanup;
     nfc->receive_iov = filter_redirector_receive_iov;
+    nfc->status_changed = filter_redirector_status_changed;
 }
 
 static void filter_mirror_init(Object *obj)
--

2.34.1

author
#3 lore raw

Re: [PATCH 1/3] net/filter-redirector: add support for dynamic status on/off switching

Quoted 84 lines · Zhang Chen
Quoted 71 lines · Jason Wang

Currently, filter-redirector does not implement the status_changed callback, which means the 'status' property cannot be used to dynamically enable/disable the filter at runtime. When status is set to 'off' via QMP/HMP, the filter still receives data from the indev chardev because the chardev handlers remain registered.

This patch adds proper support for the 'status' property:

1. Implement filter_redirector_status_changed() callback:
- When status changes to 'off': remove chardev read handlers
- When status changes to 'on': re-register chardev handlers (only if chardev is already open)

2. Update filter_redirector_setup() to respect initial status:
- If filter is created with status=off, do not register handlers
- This allows creating disabled filters via command line or QMP

3. Handle chardev OPENED/CLOSED events to re-arm handlers on reconnect:
- Keep the chr_event callback installed on CLOSE so a later OPENED

     can re-register the read handlers when nf->on

- Use qemu_chr_fe_set_handlers_full(..., set_open=false, sync_state=false) instead of qemu_chr_fe_set_handlers() because the latter forces sync_state=true and may emit CHR_EVENT_OPENED for an already-open backend. Doing that from inside the chr_event callback would cause recursive/re-entrant OPENED handling.

---
 net/filter-mirror.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
net/filter-mirror.c +14 -5
index 8e01e98f40..aa2ab452fd 100644
@@ -179,9 +179,16 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event)
     MirrorState *s = FILTER_REDIRECTOR(nf);
 
     switch (event) {
+    case CHR_EVENT_OPENED:
+        if (nf->on) {
+            qemu_chr_fe_set_handlers_full(&s->chr_in, redirector_chr_can_read,
+                                          redirector_chr_read, redirector_chr_event,
+                                          NULL, nf, NULL, false, false);
+        }
+        break;
     case CHR_EVENT_CLOSED:
-        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
-                                 NULL, NULL, NULL, true);
+        qemu_chr_fe_set_handlers_full(&s->chr_in, NULL, NULL, redirector_chr_event,
+                                      NULL, nf, NULL, false, false);
         break;
     default:
         break;
@@ -306,9 +313,11 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
             return;
         }
 
-        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
-                                 redirector_chr_read, redirector_chr_event,
-                                 NULL, nf, NULL, true);
+        if (nf->on) {
+            qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                     redirector_chr_read, redirector_chr_event,
+                                     NULL, nf, NULL, true);
+        }
     }
 
     if (s->outdev) {

net/filter-mirror.c static void filter_redirector_setup(NetFilterState *nf, Error **errp) · line 340

Quoted 8 lines
net/filter-mirror.c +5 -0
@@ -324,6 +333,24 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
     }
 }
 
+static void filter_redirector_status_changed(NetFilterState *nf, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+
+    if (!s->indev) {

It's better to add a error here, for example:

Quoted 2 lines

error_setg(errp, "filter_redirector_status_changed failed for
the s->indev cleared" );

Will do.

Quoted 13 lines

net/filter-mirror.c static void filter_redirector_setup(NetFilterState *nf, Error **errp) · line 350

Quoted 12 lines
net/filter-mirror.c +10 -0
+        return;
+    }
+
+    if (nf->on) {
+        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                 redirector_chr_read, redirector_chr_event,
+                                 NULL, nf, NULL, true);
+    } else {
+        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
+                                 NULL, NULL, NULL, true);
Quoted 1 line

It seems like we need to keep the "redirector_chr_event" here?

Since the filter is disabled I think we probably don't care about the event in that case.

Quoted 3 lines

And another thing, this series focus on indev, should we need to do the same thing for outdev?

We don't poll outdev, if we get something to send to outdev it's the bug of other filters or networking core.

Thanks

Quoted 22 lines

Thanks
Chen

Quoted 17 lines
net/filter-mirror.c +4 -1
+    }
+}
+
 static char *filter_redirector_get_indev(Object *obj, Error **errp)
 {
     MirrorState *s = FILTER_REDIRECTOR(obj);
@@ -440,6 +467,7 @@ static void filter_redirector_class_init(ObjectClass *oc, const void *data)
     nfc->setup = filter_redirector_setup;
     nfc->cleanup = filter_redirector_cleanup;
     nfc->receive_iov = filter_redirector_receive_iov;
+    nfc->status_changed = filter_redirector_status_changed;
 }
 
 static void filter_mirror_init(Object *obj)
--

2.34.1

#4 lore raw

Re: [PATCH 1/3] net/filter-redirector: add support for dynamic status on/off switching

Quoted 104 lines · Jason Wang
Quoted 84 lines · Zhang Chen
Quoted 71 lines · Jason Wang

Currently, filter-redirector does not implement the status_changed callback, which means the 'status' property cannot be used to dynamically enable/disable the filter at runtime. When status is set to 'off' via QMP/HMP, the filter still receives data from the indev chardev because the chardev handlers remain registered.

This patch adds proper support for the 'status' property:

1. Implement filter_redirector_status_changed() callback:
- When status changes to 'off': remove chardev read handlers
- When status changes to 'on': re-register chardev handlers (only if chardev is already open)

2. Update filter_redirector_setup() to respect initial status:
- If filter is created with status=off, do not register handlers
- This allows creating disabled filters via command line or QMP

3. Handle chardev OPENED/CLOSED events to re-arm handlers on reconnect:
- Keep the chr_event callback installed on CLOSE so a later OPENED

     can re-register the read handlers when nf->on

- Use qemu_chr_fe_set_handlers_full(..., set_open=false, sync_state=false) instead of qemu_chr_fe_set_handlers() because the latter forces sync_state=true and may emit CHR_EVENT_OPENED for an already-open backend. Doing that from inside the chr_event callback would cause recursive/re-entrant OPENED handling.

---
 net/filter-mirror.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
net/filter-mirror.c +14 -5
index 8e01e98f40..aa2ab452fd 100644
@@ -179,9 +179,16 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event)
     MirrorState *s = FILTER_REDIRECTOR(nf);
 
     switch (event) {
+    case CHR_EVENT_OPENED:
+        if (nf->on) {
+            qemu_chr_fe_set_handlers_full(&s->chr_in, redirector_chr_can_read,
+                                          redirector_chr_read, redirector_chr_event,
+                                          NULL, nf, NULL, false, false);
+        }
+        break;
     case CHR_EVENT_CLOSED:
-        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
-                                 NULL, NULL, NULL, true);
+        qemu_chr_fe_set_handlers_full(&s->chr_in, NULL, NULL, redirector_chr_event,
+                                      NULL, nf, NULL, false, false);
         break;
     default:
         break;
@@ -306,9 +313,11 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
             return;
         }
 
-        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
-                                 redirector_chr_read, redirector_chr_event,
-                                 NULL, nf, NULL, true);
+        if (nf->on) {
+            qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                     redirector_chr_read, redirector_chr_event,
+                                     NULL, nf, NULL, true);
+        }
     }
 
     if (s->outdev) {

net/filter-mirror.c static void filter_redirector_setup(NetFilterState *nf, Error **errp) · line 340

Quoted 8 lines
net/filter-mirror.c +5 -0
@@ -324,6 +333,24 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
     }
 }
 
+static void filter_redirector_status_changed(NetFilterState *nf, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+
+    if (!s->indev) {

It's better to add a error here, for example:

Quoted 2 lines

error_setg(errp, "filter_redirector_status_changed failed for
the s->indev cleared" );

Will do.

Quoted 13 lines
Quoted 12 lines
net/filter-mirror.c +10 -0
+        return;
+    }
+
+    if (nf->on) {
+        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                 redirector_chr_read, redirector_chr_event,
+                                 NULL, nf, NULL, true);
+    } else {
+        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
+                                 NULL, NULL, NULL, true);
Quoted 4 lines
Quoted 1 line

It seems like we need to keep the "redirector_chr_event" here?

Since the filter is disabled I think we probably don't care about the event in that case.

OK.

Quoted 7 lines
Quoted 4 lines

And another thing, this series focus on indev, should we need to do the same thing for outdev?

We don't poll outdev, if we get something to send to outdev it's the bug of other filters or networking core.

OK.

Thanks
Chen

Quoted 26 lines

Thanks

Quoted 22 lines

Thanks
Chen

Quoted 17 lines
net/filter-mirror.c +4 -1
+    }
+}
+
 static char *filter_redirector_get_indev(Object *obj, Error **errp)
 {
     MirrorState *s = FILTER_REDIRECTOR(obj);
@@ -440,6 +467,7 @@ static void filter_redirector_class_init(ObjectClass *oc, const void *data)
     nfc->setup = filter_redirector_setup;
     nfc->cleanup = filter_redirector_cleanup;
     nfc->receive_iov = filter_redirector_receive_iov;
+    nfc->status_changed = filter_redirector_status_changed;
 }
 
 static void filter_mirror_init(Object *obj)
--

2.34.1

author
#5 lore raw

Re: [PATCH 1/3] net/filter-redirector: add support for dynamic status on/off switching

In reply to Jason Wang
Quoted 86 lines · Jason Wang
Quoted 84 lines · Zhang Chen
Quoted 71 lines · Jason Wang

Currently, filter-redirector does not implement the status_changed callback, which means the 'status' property cannot be used to dynamically enable/disable the filter at runtime. When status is set to 'off' via QMP/HMP, the filter still receives data from the indev chardev because the chardev handlers remain registered.

This patch adds proper support for the 'status' property:

1. Implement filter_redirector_status_changed() callback:
- When status changes to 'off': remove chardev read handlers
- When status changes to 'on': re-register chardev handlers (only if chardev is already open)

2. Update filter_redirector_setup() to respect initial status:
- If filter is created with status=off, do not register handlers
- This allows creating disabled filters via command line or QMP

3. Handle chardev OPENED/CLOSED events to re-arm handlers on reconnect:
- Keep the chr_event callback installed on CLOSE so a later OPENED

     can re-register the read handlers when nf->on

- Use qemu_chr_fe_set_handlers_full(..., set_open=false, sync_state=false) instead of qemu_chr_fe_set_handlers() because the latter forces sync_state=true and may emit CHR_EVENT_OPENED for an already-open backend. Doing that from inside the chr_event callback would cause recursive/re-entrant OPENED handling.

---
 net/filter-mirror.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
net/filter-mirror.c +14 -5
index 8e01e98f40..aa2ab452fd 100644
@@ -179,9 +179,16 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event)
     MirrorState *s = FILTER_REDIRECTOR(nf);
 
     switch (event) {
+    case CHR_EVENT_OPENED:
+        if (nf->on) {
+            qemu_chr_fe_set_handlers_full(&s->chr_in, redirector_chr_can_read,
+                                          redirector_chr_read, redirector_chr_event,
+                                          NULL, nf, NULL, false, false);
+        }
+        break;
     case CHR_EVENT_CLOSED:
-        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
-                                 NULL, NULL, NULL, true);
+        qemu_chr_fe_set_handlers_full(&s->chr_in, NULL, NULL, redirector_chr_event,
+                                      NULL, nf, NULL, false, false);
         break;
     default:
         break;
@@ -306,9 +313,11 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
             return;
         }
 
-        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
-                                 redirector_chr_read, redirector_chr_event,
-                                 NULL, nf, NULL, true);
+        if (nf->on) {
+            qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                     redirector_chr_read, redirector_chr_event,
+                                     NULL, nf, NULL, true);
+        }
     }
 
     if (s->outdev) {

net/filter-mirror.c static void filter_redirector_setup(NetFilterState *nf, Error **errp) · line 340

Quoted 8 lines
net/filter-mirror.c +5 -0
@@ -324,6 +333,24 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
     }
 }
 
+static void filter_redirector_status_changed(NetFilterState *nf, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+
+    if (!s->indev) {

It's better to add a error here, for example:

Quoted 5 lines
Quoted 2 lines

error_setg(errp, "filter_redirector_status_changed failed for
the s->indev cleared" );

Will do.

I think it's probably wrong to return an error here as we may only use outdev but not indev, in this case status_changed doesn't need to do anything.

Thanks

#6 lore raw

Re: [PATCH 1/3] net/filter-redirector: add support for dynamic status on/off switching

Quoted 91 lines · Jason Wang
Quoted 89 lines · Jason Wang
Quoted 86 lines · Zhang Chen
Quoted 71 lines · Jason Wang

Currently, filter-redirector does not implement the status_changed callback, which means the 'status' property cannot be used to dynamically enable/disable the filter at runtime. When status is set to 'off' via QMP/HMP, the filter still receives data from the indev chardev because the chardev handlers remain registered.

This patch adds proper support for the 'status' property:

1. Implement filter_redirector_status_changed() callback:
- When status changes to 'off': remove chardev read handlers
- When status changes to 'on': re-register chardev handlers (only if chardev is already open)

2. Update filter_redirector_setup() to respect initial status:
- If filter is created with status=off, do not register handlers
- This allows creating disabled filters via command line or QMP

3. Handle chardev OPENED/CLOSED events to re-arm handlers on reconnect:
- Keep the chr_event callback installed on CLOSE so a later OPENED

     can re-register the read handlers when nf->on

- Use qemu_chr_fe_set_handlers_full(..., set_open=false, sync_state=false) instead of qemu_chr_fe_set_handlers() because the latter forces sync_state=true and may emit CHR_EVENT_OPENED for an already-open backend. Doing that from inside the chr_event callback would cause recursive/re-entrant OPENED handling.

---
 net/filter-mirror.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
net/filter-mirror.c +14 -5
index 8e01e98f40..aa2ab452fd 100644
@@ -179,9 +179,16 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event)
     MirrorState *s = FILTER_REDIRECTOR(nf);
 
     switch (event) {
+    case CHR_EVENT_OPENED:
+        if (nf->on) {
+            qemu_chr_fe_set_handlers_full(&s->chr_in, redirector_chr_can_read,
+                                          redirector_chr_read, redirector_chr_event,
+                                          NULL, nf, NULL, false, false);
+        }
+        break;
     case CHR_EVENT_CLOSED:
-        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
-                                 NULL, NULL, NULL, true);
+        qemu_chr_fe_set_handlers_full(&s->chr_in, NULL, NULL, redirector_chr_event,
+                                      NULL, nf, NULL, false, false);
         break;
     default:
         break;
@@ -306,9 +313,11 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
             return;
         }
 
-        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
-                                 redirector_chr_read, redirector_chr_event,
-                                 NULL, nf, NULL, true);
+        if (nf->on) {
+            qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                     redirector_chr_read, redirector_chr_event,
+                                     NULL, nf, NULL, true);
+        }
     }
 
     if (s->outdev) {

net/filter-mirror.c static void filter_redirector_setup(NetFilterState *nf, Error **errp) · line 340

Quoted 8 lines
net/filter-mirror.c +5 -0
@@ -324,6 +333,24 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
     }
 }
 
+static void filter_redirector_status_changed(NetFilterState *nf, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+
+    if (!s->indev) {

It's better to add a error here, for example:

error_setg(errp, "filter_redirector_status_changed failed for
the s->indev cleared" );

Quoted 6 lines
Quoted 2 lines

Will do.

I think it's probably wrong to return an error here as we may only use outdev but not indev, in this case status_changed doesn't need to do anything.

This confuses me. The filter_redirector_status_changed code focus on how to read the the indev, it looks not affect the outdev.
If we just return without any error, the user is unsure whether the attempt was successful.

Thanks
Chen

Quoted 3 lines

Thanks

author
#7 lore raw

Re: [PATCH 1/3] net/filter-redirector: add support for dynamic status on/off switching

Quoted 96 lines · Zhang Chen
Quoted 94 lines · Jason Wang
Quoted 91 lines · Jason Wang
Quoted 84 lines · Zhang Chen
Quoted 71 lines · Jason Wang

Currently, filter-redirector does not implement the status_changed callback, which means the 'status' property cannot be used to dynamically enable/disable the filter at runtime. When status is set to 'off' via QMP/HMP, the filter still receives data from the indev chardev because the chardev handlers remain registered.

This patch adds proper support for the 'status' property:

1. Implement filter_redirector_status_changed() callback:
- When status changes to 'off': remove chardev read handlers
- When status changes to 'on': re-register chardev handlers (only if chardev is already open)

2. Update filter_redirector_setup() to respect initial status:
- If filter is created with status=off, do not register handlers
- This allows creating disabled filters via command line or QMP

3. Handle chardev OPENED/CLOSED events to re-arm handlers on reconnect:
- Keep the chr_event callback installed on CLOSE so a later OPENED

     can re-register the read handlers when nf->on

- Use qemu_chr_fe_set_handlers_full(..., set_open=false, sync_state=false) instead of qemu_chr_fe_set_handlers() because the latter forces sync_state=true and may emit CHR_EVENT_OPENED for an already-open backend. Doing that from inside the chr_event callback would cause recursive/re-entrant OPENED handling.

---
 net/filter-mirror.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
net/filter-mirror.c +14 -5
index 8e01e98f40..aa2ab452fd 100644
@@ -179,9 +179,16 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event)
     MirrorState *s = FILTER_REDIRECTOR(nf);
 
     switch (event) {
+    case CHR_EVENT_OPENED:
+        if (nf->on) {
+            qemu_chr_fe_set_handlers_full(&s->chr_in, redirector_chr_can_read,
+                                          redirector_chr_read, redirector_chr_event,
+                                          NULL, nf, NULL, false, false);
+        }
+        break;
     case CHR_EVENT_CLOSED:
-        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
-                                 NULL, NULL, NULL, true);
+        qemu_chr_fe_set_handlers_full(&s->chr_in, NULL, NULL, redirector_chr_event,
+                                      NULL, nf, NULL, false, false);
         break;
     default:
         break;
@@ -306,9 +313,11 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
             return;
         }
 
-        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
-                                 redirector_chr_read, redirector_chr_event,
-                                 NULL, nf, NULL, true);
+        if (nf->on) {
+            qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                     redirector_chr_read, redirector_chr_event,
+                                     NULL, nf, NULL, true);
+        }
     }
 
     if (s->outdev) {

net/filter-mirror.c static void filter_redirector_setup(NetFilterState *nf, Error **errp) · line 340

Quoted 8 lines
net/filter-mirror.c +5 -0
@@ -324,6 +333,24 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
     }
 }
 
+static void filter_redirector_status_changed(NetFilterState *nf, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+
+    if (!s->indev) {

It's better to add a error here, for example:

Quoted 2 lines

error_setg(errp, "filter_redirector_status_changed failed for
the s->indev cleared" );

Will do.

Quoted 9 lines
Quoted 3 lines

I think it's probably wrong to return an error here as we may only use outdev but not indev, in this case status_changed doesn't need to do anything.

This confuses me. The filter_redirector_status_changed code focus on how to read the the indev, it looks not affect the outdev.
If we just return without any error, the user is unsure whether the attempt was successful.

Consider the case where there's only outdev, in this case we don't need to do anything when status is changed:

1) we don't poll outdev
2) net core will bypass the redirector if the status is off.

I meant basically we don't need to do anything in this case and it's not an error.

Thanks

#8 lore raw

Re: [PATCH 1/3] net/filter-redirector: add support for dynamic status on/off switching

Quoted 108 lines · Jason Wang
Quoted 96 lines · Zhang Chen
Quoted 94 lines · Jason Wang
Quoted 91 lines · Jason Wang
Quoted 84 lines · Zhang Chen
Quoted 71 lines · Jason Wang

Currently, filter-redirector does not implement the status_changed callback, which means the 'status' property cannot be used to dynamically enable/disable the filter at runtime. When status is set to 'off' via QMP/HMP, the filter still receives data from the indev chardev because the chardev handlers remain registered.

This patch adds proper support for the 'status' property:

1. Implement filter_redirector_status_changed() callback:
- When status changes to 'off': remove chardev read handlers
- When status changes to 'on': re-register chardev handlers (only if chardev is already open)

2. Update filter_redirector_setup() to respect initial status:
- If filter is created with status=off, do not register handlers
- This allows creating disabled filters via command line or QMP

3. Handle chardev OPENED/CLOSED events to re-arm handlers on reconnect:
- Keep the chr_event callback installed on CLOSE so a later OPENED

     can re-register the read handlers when nf->on

- Use qemu_chr_fe_set_handlers_full(..., set_open=false, sync_state=false) instead of qemu_chr_fe_set_handlers() because the latter forces sync_state=true and may emit CHR_EVENT_OPENED for an already-open backend. Doing that from inside the chr_event callback would cause recursive/re-entrant OPENED handling.

---
 net/filter-mirror.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
net/filter-mirror.c +14 -5
index 8e01e98f40..aa2ab452fd 100644
@@ -179,9 +179,16 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event)
     MirrorState *s = FILTER_REDIRECTOR(nf);
 
     switch (event) {
+    case CHR_EVENT_OPENED:
+        if (nf->on) {
+            qemu_chr_fe_set_handlers_full(&s->chr_in, redirector_chr_can_read,
+                                          redirector_chr_read, redirector_chr_event,
+                                          NULL, nf, NULL, false, false);
+        }
+        break;
     case CHR_EVENT_CLOSED:
-        qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
-                                 NULL, NULL, NULL, true);
+        qemu_chr_fe_set_handlers_full(&s->chr_in, NULL, NULL, redirector_chr_event,
+                                      NULL, nf, NULL, false, false);
         break;
     default:
         break;
@@ -306,9 +313,11 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
             return;
         }
 
-        qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
-                                 redirector_chr_read, redirector_chr_event,
-                                 NULL, nf, NULL, true);
+        if (nf->on) {
+            qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+                                     redirector_chr_read, redirector_chr_event,
+                                     NULL, nf, NULL, true);
+        }
     }
 
     if (s->outdev) {

net/filter-mirror.c static void filter_redirector_setup(NetFilterState *nf, Error **errp) · line 340

Quoted 8 lines
net/filter-mirror.c +5 -0
@@ -324,6 +333,24 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
     }
 }
 
+static void filter_redirector_status_changed(NetFilterState *nf, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+
+    if (!s->indev) {

It's better to add a error here, for example:

Quoted 2 lines

error_setg(errp, "filter_redirector_status_changed failed for
the s->indev cleared" );

Will do.

Quoted 9 lines
Quoted 3 lines

I think it's probably wrong to return an error here as we may only use outdev but not indev, in this case status_changed doesn't need to do anything.

This confuses me. The filter_redirector_status_changed code focus on how to read the the indev, it looks not affect the outdev.
If we just return without any error, the user is unsure whether the attempt was successful.

Quoted 9 lines

Consider the case where there's only outdev, in this case we don't need to do anything when status is changed:

1) we don't poll outdev
2) net core will bypass the redirector if the status is off.

I meant basically we don't need to do anything in this case and it's not an error.

Okay, I understand now.

Thanks
Chen

Quoted 2 lines

Thanks

author
#9 lore raw

PATCH 2/3 qtest: add a test to test redirector status change

In reply to Jason Wang

This patch adds a qtest to test the status change of filter-redirector. Two subtests were added:

- test_redirector_status: tests dynamic on/off switching at runtime using qom-set QMP command

- test_redirector_init_status_off: tests creating filter-redirector with status=off from the start via command line

Both tests verify that:

1. When status is off, data from indev chardev is not received
2. When status is switched to on, data is received correctly

---
 tests/qtest/test-filter-redirector.c | 192 +++++++++++++++++++++++++++
 1 file changed, 192 insertions(+)
tests/qtest/test-filter-redirector.c +192 -0
index a996a80c1c..da0c126314 100644
@@ -196,10 +196,202 @@ static void test_redirector_rx(void)
     qtest_quit(qts);
 }
 
+/*
+ * Test filter-redirector status on/off switching.
+ *
+ * This test verifies that:
+ * 1. When status is set to "off", the filter stops receiving data from indev
+ * 2. When status is set back to "on", the filter resumes receiving data
+ */
+static void test_redirector_status(void)
+{
+    int backend_sock[2], send_sock;
+    uint32_t ret = 0, len = 0;
+    char send_buf[] = "Hello!!";
+    char sock_path0[] = "filter-redirector0.XXXXXX";
+    char *recv_buf;
+    uint32_t size = sizeof(send_buf);
+    size = htonl(size);
+    QTestState *qts;
+    struct timeval tv;
+    fd_set rfds;
+
+    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, backend_sock);
+    g_assert_cmpint(ret, !=, -1);
+
+    ret = mkstemp(sock_path0);
+    g_assert_cmpint(ret, !=, -1);
+
+    /*
+     * Setup a simple rx path:
+     * chardev (sock_path0) -> filter-redirector -> socket backend
+     */
+    qts = qtest_initf(
+        "-nic socket,id=qtest-bn0,fd=%d "
+        "-chardev socket,id=redirector0,path=%s,server=on,wait=off "
+        "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
+        "queue=rx,indev=redirector0 ",
+        backend_sock[1], sock_path0);
+
+    send_sock = unix_connect(sock_path0, NULL);
+    g_assert_cmpint(send_sock, !=, -1);
+
+    /* send a qmp command to guarantee that 'connected' is setting to true. */
+    qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
+
+    struct iovec iov[] = {
+        {
+            .iov_base = &size,
+            .iov_len = sizeof(size),
+        }, {
+            .iov_base = send_buf,
+            .iov_len = sizeof(send_buf),
+        },
+    };
+
+    /*
+     * Test 1: Set status to "off" and verify data is not received
+     */
+    qtest_qmp_assert_success(qts,
+        "{ 'execute': 'qom-set', 'arguments': "
+        "{ 'path': '/objects/qtest-f0', 'property': 'status', 'value': 'off' }}");
+
+    ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
+    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
+
+    /*
+     * Use select with timeout to check if data arrives.
+     * When status is off, no data should arrive.
+     */
+    FD_ZERO(&rfds);
+    FD_SET(backend_sock[0], &rfds);
+    tv.tv_sec = 0;
+    tv.tv_usec = 500000;  /* 500ms timeout */
+    ret = select(backend_sock[0] + 1, &rfds, NULL, NULL, &tv);
+    g_assert_cmpint(ret, ==, 0);  /* Should timeout, no data */
+
+    /*
+     * Test 2: Set status back to "on" and verify data is received
+     */
+    qtest_qmp_assert_success(qts,
+        "{ 'execute': 'qom-set', 'arguments': "
+        "{ 'path': '/objects/qtest-f0', 'property': 'status', 'value': 'on' }}");
+
+    ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
+    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
+
+    ret = recv(backend_sock[0], &len, sizeof(len), 0);
+    g_assert_cmpint(ret, ==, sizeof(len));
+    len = ntohl(len);
+
+    g_assert_cmpint(len, ==, sizeof(send_buf));
+    recv_buf = g_malloc(len);
+    ret = recv(backend_sock[0], recv_buf, len, 0);
+    g_assert_cmpint(ret, ==, len);
+    g_assert_cmpstr(recv_buf, ==, send_buf);
+
+    g_free(recv_buf);
+    close(send_sock);
+    unlink(sock_path0);
+    qtest_quit(qts);
+}
+
+/*
+ * Test filter-redirector created with status=off.
+ *
+ * This test verifies that when a filter-redirector is created with
+ * status=off, it does not receive data until status is set to on.
+ */
+static void test_redirector_init_status_off(void)
+{
+    int backend_sock[2], send_sock;
+    uint32_t ret = 0, len = 0;
+    char send_buf[] = "Hello!!";
+    char sock_path0[] = "filter-redirector0.XXXXXX";
+    char *recv_buf;
+    uint32_t size = sizeof(send_buf);
+    size = htonl(size);
+    QTestState *qts;
+    struct timeval tv;
+    fd_set rfds;
+
+    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, backend_sock);
+    g_assert_cmpint(ret, !=, -1);
+
+    ret = mkstemp(sock_path0);
+    g_assert_cmpint(ret, !=, -1);
+
+    /*
+     * Create filter-redirector with status=off from the start
+     */
+    qts = qtest_initf(
+        "-nic socket,id=qtest-bn0,fd=%d "
+        "-chardev socket,id=redirector0,path=%s,server=on,wait=off "
+        "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
+        "queue=rx,indev=redirector0,status=off ",
+        backend_sock[1], sock_path0);
+
+    send_sock = unix_connect(sock_path0, NULL);
+    g_assert_cmpint(send_sock, !=, -1);
+
+    qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
+
+    struct iovec iov[] = {
+        {
+            .iov_base = &size,
+            .iov_len = sizeof(size),
+        }, {
+            .iov_base = send_buf,
+            .iov_len = sizeof(send_buf),
+        },
+    };
+
+    /*
+     * Test 1: Filter was created with status=off, data should not be received
+     */
+    ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
+    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
+
+    FD_ZERO(&rfds);
+    FD_SET(backend_sock[0], &rfds);
+    tv.tv_sec = 0;
+    tv.tv_usec = 500000;
+    ret = select(backend_sock[0] + 1, &rfds, NULL, NULL, &tv);
+    g_assert_cmpint(ret, ==, 0);  /* Should timeout, no data */
+
+    /*
+     * Test 2: Set status to "on" and verify data is received
+     */
+    qtest_qmp_assert_success(qts,
+        "{ 'execute': 'qom-set', 'arguments': "
+        "{ 'path': '/objects/qtest-f0', 'property': 'status', 'value': 'on' }}");
+
+    ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
+    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
+
+    ret = recv(backend_sock[0], &len, sizeof(len), 0);
+    g_assert_cmpint(ret, ==, sizeof(len));
+    len = ntohl(len);
+
+    g_assert_cmpint(len, ==, sizeof(send_buf));
+    recv_buf = g_malloc(len);
+    ret = recv(backend_sock[0], recv_buf, len, 0);
+    g_assert_cmpint(ret, ==, len);
+    g_assert_cmpstr(recv_buf, ==, send_buf);
+
+    g_free(recv_buf);
+    close(send_sock);
+    unlink(sock_path0);
+    qtest_quit(qts);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/netfilter/redirector_tx", test_redirector_tx);
     qtest_add_func("/netfilter/redirector_rx", test_redirector_rx);
+    qtest_add_func("/netfilter/redirector_status", test_redirector_status);
+    qtest_add_func("/netfilter/redirector_init_status_off",
+                   test_redirector_init_status_off);
     return g_test_run();
 }
Signature · 1 line
2.34.1
Reviewed-by on v1 2/3
#10 lore raw

Re: [PATCH 2/3] qtest: add a test to test redirector status change

Quoted 10 lines · Jason Wang

This patch adds a qtest to test the status change of filter-redirector. Two subtests were added:

- test_redirector_status: tests dynamic on/off switching at runtime using qom-set QMP command

- test_redirector_init_status_off: tests creating filter-redirector with status=off from the start via command line

Quoted 6 lines

Both tests verify that:

1. When status is off, data from indev chardev is not received
2. When status is switched to on, data is received correctly

LGTM.

Thanks
Chen

Quoted 214 lines
---
 tests/qtest/test-filter-redirector.c | 192 +++++++++++++++++++++++++++
 1 file changed, 192 insertions(+)
tests/qtest/test-filter-redirector.c +192 -1
index a996a80c1c..da0c126314 100644
@@ -196,10 +196,202 @@ static void test_redirector_rx(void)
     qtest_quit(qts);
 }
 
+/*
+ * Test filter-redirector status on/off switching.
+ *
+ * This test verifies that:
+ * 1. When status is set to "off", the filter stops receiving data from indev
+ * 2. When status is set back to "on", the filter resumes receiving data
+ */
+static void test_redirector_status(void)
+{
+    int backend_sock[2], send_sock;
+    uint32_t ret = 0, len = 0;
+    char send_buf[] = "Hello!!";
+    char sock_path0[] = "filter-redirector0.XXXXXX";
+    char *recv_buf;
+    uint32_t size = sizeof(send_buf);
+    size = htonl(size);
+    QTestState *qts;
+    struct timeval tv;
+    fd_set rfds;
+
+    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, backend_sock);
+    g_assert_cmpint(ret, !=, -1);
+
+    ret = mkstemp(sock_path0);
+    g_assert_cmpint(ret, !=, -1);
+
+    /*
+     * Setup a simple rx path:
+     * chardev (sock_path0) -> filter-redirector -> socket backend
+     */
+    qts = qtest_initf(
+        "-nic socket,id=qtest-bn0,fd=%d "
+        "-chardev socket,id=redirector0,path=%s,server=on,wait=off "
+        "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
+        "queue=rx,indev=redirector0 ",
+        backend_sock[1], sock_path0);
+
+    send_sock = unix_connect(sock_path0, NULL);
+    g_assert_cmpint(send_sock, !=, -1);
+
+    /* send a qmp command to guarantee that 'connected' is setting to true. */
+    qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
+
+    struct iovec iov[] = {
+        {
+            .iov_base = &size,
+            .iov_len = sizeof(size),
+        }, {
+            .iov_base = send_buf,
+            .iov_len = sizeof(send_buf),
+        },
+    };
+
+    /*
+     * Test 1: Set status to "off" and verify data is not received
+     */
+    qtest_qmp_assert_success(qts,
+        "{ 'execute': 'qom-set', 'arguments': "
+        "{ 'path': '/objects/qtest-f0', 'property': 'status', 'value': 'off' }}");
+
+    ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
+    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
+
+    /*
+     * Use select with timeout to check if data arrives.
+     * When status is off, no data should arrive.
+     */
+    FD_ZERO(&rfds);
+    FD_SET(backend_sock[0], &rfds);
+    tv.tv_sec = 0;
+    tv.tv_usec = 500000;  /* 500ms timeout */
+    ret = select(backend_sock[0] + 1, &rfds, NULL, NULL, &tv);
+    g_assert_cmpint(ret, ==, 0);  /* Should timeout, no data */
+
+    /*
+     * Test 2: Set status back to "on" and verify data is received
+     */
+    qtest_qmp_assert_success(qts,
+        "{ 'execute': 'qom-set', 'arguments': "
+        "{ 'path': '/objects/qtest-f0', 'property': 'status', 'value': 'on' }}");
+
+    ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
+    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
+
+    ret = recv(backend_sock[0], &len, sizeof(len), 0);
+    g_assert_cmpint(ret, ==, sizeof(len));
+    len = ntohl(len);
+
+    g_assert_cmpint(len, ==, sizeof(send_buf));
+    recv_buf = g_malloc(len);
+    ret = recv(backend_sock[0], recv_buf, len, 0);
+    g_assert_cmpint(ret, ==, len);
+    g_assert_cmpstr(recv_buf, ==, send_buf);
+
+    g_free(recv_buf);
+    close(send_sock);
+    unlink(sock_path0);
+    qtest_quit(qts);
+}
+
+/*
+ * Test filter-redirector created with status=off.
+ *
+ * This test verifies that when a filter-redirector is created with
+ * status=off, it does not receive data until status is set to on.
+ */
+static void test_redirector_init_status_off(void)
+{
+    int backend_sock[2], send_sock;
+    uint32_t ret = 0, len = 0;
+    char send_buf[] = "Hello!!";
+    char sock_path0[] = "filter-redirector0.XXXXXX";
+    char *recv_buf;
+    uint32_t size = sizeof(send_buf);
+    size = htonl(size);
+    QTestState *qts;
+    struct timeval tv;
+    fd_set rfds;
+
+    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, backend_sock);
+    g_assert_cmpint(ret, !=, -1);
+
+    ret = mkstemp(sock_path0);
+    g_assert_cmpint(ret, !=, -1);
+
+    /*
+     * Create filter-redirector with status=off from the start
+     */
+    qts = qtest_initf(
+        "-nic socket,id=qtest-bn0,fd=%d "
+        "-chardev socket,id=redirector0,path=%s,server=on,wait=off "
+        "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
+        "queue=rx,indev=redirector0,status=off ",
+        backend_sock[1], sock_path0);
+
+    send_sock = unix_connect(sock_path0, NULL);
+    g_assert_cmpint(send_sock, !=, -1);
+
+    qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
+
+    struct iovec iov[] = {
+        {
+            .iov_base = &size,
+            .iov_len = sizeof(size),
+        }, {
+            .iov_base = send_buf,
+            .iov_len = sizeof(send_buf),
+        },
+    };
+
+    /*
+     * Test 1: Filter was created with status=off, data should not be received
+     */
+    ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
+    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
+
+    FD_ZERO(&rfds);
+    FD_SET(backend_sock[0], &rfds);
+    tv.tv_sec = 0;
+    tv.tv_usec = 500000;
+    ret = select(backend_sock[0] + 1, &rfds, NULL, NULL, &tv);
+    g_assert_cmpint(ret, ==, 0);  /* Should timeout, no data */
+
+    /*
+     * Test 2: Set status to "on" and verify data is received
+     */
+    qtest_qmp_assert_success(qts,
+        "{ 'execute': 'qom-set', 'arguments': "
+        "{ 'path': '/objects/qtest-f0', 'property': 'status', 'value': 'on' }}");
+
+    ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
+    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
+
+    ret = recv(backend_sock[0], &len, sizeof(len), 0);
+    g_assert_cmpint(ret, ==, sizeof(len));
+    len = ntohl(len);
+
+    g_assert_cmpint(len, ==, sizeof(send_buf));
+    recv_buf = g_malloc(len);
+    ret = recv(backend_sock[0], recv_buf, len, 0);
+    g_assert_cmpint(ret, ==, len);
+    g_assert_cmpstr(recv_buf, ==, send_buf);
+
+    g_free(recv_buf);
+    close(send_sock);
+    unlink(sock_path0);
+    qtest_quit(qts);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/netfilter/redirector_tx", test_redirector_tx);
     qtest_add_func("/netfilter/redirector_rx", test_redirector_rx);
+    qtest_add_func("/netfilter/redirector_status", test_redirector_status);
+    qtest_add_func("/netfilter/redirector_init_status_off",
+                   test_redirector_init_status_off);
     return g_test_run();
 }
--

2.34.1

author
#11 lore raw

PATCH 3/3 tests/qtest: Add test for filter-redirector rx event opened

In reply to Jason Wang

Add a new test case 'test_redirector_rx_event_opened' to verify the handling of the CHR_EVENT_OPENED event in filter-redirector.

The test simulates a scenario where the backend character device (socket) is disconnected and then reconnected. It works by:
1. Connecting to the redirector's socket (triggers CHR_EVENT_OPENED).
2. Sending a packet to verify initial connectivity.
3. Disconnecting (triggers CHR_EVENT_CLOSED).
4. Reconnecting (triggers CHR_EVENT_OPENED again).
5. Sending another packet to verify that the redirector correctly re-registers its handlers and resumes passing traffic.

This ensures that the filter-redirector can recover and function correctly after a backend reconnection.

---
 tests/qtest/test-filter-redirector.c | 96 ++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
tests/qtest/test-filter-redirector.c +96 -0
index da0c126314..5540c232c0 100644
@@ -385,6 +385,100 @@ static void test_redirector_init_status_off(void)
     qtest_quit(qts);
 }
 
+static void test_redirector_rx_event_opened(void)
+{
+    int backend_sock[2], send_sock;
+    uint32_t ret = 0, len = 0;
+    char send_buf[] = "Hello!!";
+    char send_buf2[] = "Hello2!!";
+    char sock_path0[] = "filter-redirector0.XXXXXX";
+    char *recv_buf;
+    uint32_t size = sizeof(send_buf);
+    uint32_t size2 = sizeof(send_buf2);
+    size = htonl(size);
+    size2 = htonl(size2);
+    QTestState *qts;
+
+    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, backend_sock);
+    g_assert_cmpint(ret, !=, -1);
+
+    ret = mkstemp(sock_path0);
+    g_assert_cmpint(ret, !=, -1);
+
+    qts = qtest_initf(
+        "-nic socket,id=qtest-bn0,fd=%d "
+        "-chardev socket,id=redirector0,path=%s,server=on,wait=off "
+        "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
+        "queue=rx,indev=redirector0 ",
+        backend_sock[1], sock_path0);
+
+    struct iovec iov[] = {
+        {
+            .iov_base = &size,
+            .iov_len = sizeof(size),
+        }, {
+            .iov_base = send_buf,
+            .iov_len = sizeof(send_buf),
+        },
+    };
+
+    struct iovec iov2[] = {
+        {
+            .iov_base = &size2,
+            .iov_len = sizeof(size2),
+        }, {
+            .iov_base = send_buf2,
+            .iov_len = sizeof(send_buf2),
+        },
+    };
+
+    /* First connection */
+    send_sock = unix_connect(sock_path0, NULL);
+    g_assert_cmpint(send_sock, !=, -1);
+    qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
+
+    ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
+    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
+
+    ret = recv(backend_sock[0], &len, sizeof(len), 0);
+    g_assert_cmpint(ret, ==, sizeof(len));
+    len = ntohl(len);
+    g_assert_cmpint(len, ==, sizeof(send_buf));
+    recv_buf = g_malloc(len);
+    ret = recv(backend_sock[0], recv_buf, len, 0);
+    g_assert_cmpint(ret, ==, len);
+    g_assert_cmpstr(recv_buf, ==, send_buf);
+    g_free(recv_buf);
+
+    close(send_sock);
+
+    /* Verify disconnection handling if needed, but mainly we want to test Reconnection */
+    qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
+
+    /* Second connection */
+    send_sock = unix_connect(sock_path0, NULL);
+    g_assert_cmpint(send_sock, !=, -1);
+    qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
+
+    ret = iov_send(send_sock, iov2, 2, 0, sizeof(size2) + sizeof(send_buf2));
+    g_assert_cmpint(ret, ==, sizeof(send_buf2) + sizeof(size2));
+
+    ret = recv(backend_sock[0], &len, sizeof(len), 0);
+    g_assert_cmpint(ret, ==, sizeof(len));
+    len = ntohl(len);
+    g_assert_cmpint(len, ==, sizeof(send_buf2));
+    recv_buf = g_malloc(len);
+    ret = recv(backend_sock[0], recv_buf, len, 0);
+    g_assert_cmpint(ret, ==, len);
+    g_assert_cmpstr(recv_buf, ==, send_buf2);
+    g_free(recv_buf);
+
+    close(send_sock);
+    unlink(sock_path0);
+    qtest_quit(qts);
+    close(backend_sock[0]);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -393,5 +487,7 @@ int main(int argc, char **argv)
     qtest_add_func("/netfilter/redirector_status", test_redirector_status);
     qtest_add_func("/netfilter/redirector_init_status_off",
                    test_redirector_init_status_off);
+    qtest_add_func("/netfilter/redirector_rx_event_opened",
+                   test_redirector_rx_event_opened);
     return g_test_run();
 }
Signature · 1 line
2.34.1
Reviewed-by on v1 3/3
#12 lore raw

Re: [PATCH 3/3] tests/qtest: Add test for filter-redirector rx event opened

Quoted 13 lines · Jason Wang

Add a new test case 'test_redirector_rx_event_opened' to verify the handling of the CHR_EVENT_OPENED event in filter-redirector.

The test simulates a scenario where the backend character device (socket) is disconnected and then reconnected. It works by:
1. Connecting to the redirector's socket (triggers CHR_EVENT_OPENED).
2. Sending a packet to verify initial connectivity.
3. Disconnecting (triggers CHR_EVENT_CLOSED).
4. Reconnecting (triggers CHR_EVENT_OPENED again).
5. Sending another packet to verify that the redirector correctly re-registers its handlers and resumes passing traffic.

Quoted 4 lines

This ensures that the filter-redirector can recover and function correctly after a backend reconnection.

Very meaningful detailed description.

Quoted 120 lines
---
 tests/qtest/test-filter-redirector.c | 96 ++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
tests/qtest/test-filter-redirector.c +96 -1
index da0c126314..5540c232c0 100644
@@ -385,6 +385,100 @@ static void test_redirector_init_status_off(void)
     qtest_quit(qts);
 }
 
+static void test_redirector_rx_event_opened(void)
+{
+    int backend_sock[2], send_sock;
+    uint32_t ret = 0, len = 0;
+    char send_buf[] = "Hello!!";
+    char send_buf2[] = "Hello2!!";
+    char sock_path0[] = "filter-redirector0.XXXXXX";
+    char *recv_buf;
+    uint32_t size = sizeof(send_buf);
+    uint32_t size2 = sizeof(send_buf2);
+    size = htonl(size);
+    size2 = htonl(size2);
+    QTestState *qts;
+
+    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, backend_sock);
+    g_assert_cmpint(ret, !=, -1);
+
+    ret = mkstemp(sock_path0);
+    g_assert_cmpint(ret, !=, -1);
+
+    qts = qtest_initf(
+        "-nic socket,id=qtest-bn0,fd=%d "
+        "-chardev socket,id=redirector0,path=%s,server=on,wait=off "
+        "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
+        "queue=rx,indev=redirector0 ",
+        backend_sock[1], sock_path0);
+
+    struct iovec iov[] = {
+        {
+            .iov_base = &size,
+            .iov_len = sizeof(size),
+        }, {
+            .iov_base = send_buf,
+            .iov_len = sizeof(send_buf),
+        },
+    };
+
+    struct iovec iov2[] = {
+        {
+            .iov_base = &size2,
+            .iov_len = sizeof(size2),
+        }, {
+            .iov_base = send_buf2,
+            .iov_len = sizeof(send_buf2),
+        },
+    };
+
+    /* First connection */
+    send_sock = unix_connect(sock_path0, NULL);
+    g_assert_cmpint(send_sock, !=, -1);
+    qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
+
+    ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
+    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
+
+    ret = recv(backend_sock[0], &len, sizeof(len), 0);
+    g_assert_cmpint(ret, ==, sizeof(len));
+    len = ntohl(len);
+    g_assert_cmpint(len, ==, sizeof(send_buf));
+    recv_buf = g_malloc(len);
+    ret = recv(backend_sock[0], recv_buf, len, 0);
+    g_assert_cmpint(ret, ==, len);
+    g_assert_cmpstr(recv_buf, ==, send_buf);
+    g_free(recv_buf);
+
+    close(send_sock);
+
+    /* Verify disconnection handling if needed, but mainly we want to test Reconnection */
+    qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
+
+    /* Second connection */
+    send_sock = unix_connect(sock_path0, NULL);
+    g_assert_cmpint(send_sock, !=, -1);
+    qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
+
+    ret = iov_send(send_sock, iov2, 2, 0, sizeof(size2) + sizeof(send_buf2));
+    g_assert_cmpint(ret, ==, sizeof(send_buf2) + sizeof(size2));
+
+    ret = recv(backend_sock[0], &len, sizeof(len), 0);
+    g_assert_cmpint(ret, ==, sizeof(len));
+    len = ntohl(len);
+    g_assert_cmpint(len, ==, sizeof(send_buf2));
+    recv_buf = g_malloc(len);
+    ret = recv(backend_sock[0], recv_buf, len, 0);
+    g_assert_cmpint(ret, ==, len);
+    g_assert_cmpstr(recv_buf, ==, send_buf2);
+    g_free(recv_buf);
+
+    close(send_sock);
+    unlink(sock_path0);
+    qtest_quit(qts);
+    close(backend_sock[0]);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -393,5 +487,7 @@ int main(int argc, char **argv)
     qtest_add_func("/netfilter/redirector_status", test_redirector_status);
     qtest_add_func("/netfilter/redirector_init_status_off",
                    test_redirector_init_status_off);
+    qtest_add_func("/netfilter/redirector_rx_event_opened",
+                   test_redirector_rx_event_opened);
     return g_test_run();
 }
--

2.34.1