[PATCH v2 1/8] net/tap: add some trace points

Vladimir Sementsov-Ogievskiy posted 8 patches 3 days, 1 hour ago
There is a newer version of this series
[PATCH v2 1/8] net/tap: add some trace points
Posted by Vladimir Sementsov-Ogievskiy 3 days, 1 hour ago
Add trace points to simplify debugging migration.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---
 net/tap.c        | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/trace-events |  7 ++++++
 2 files changed, 65 insertions(+)

diff --git a/net/tap.c b/net/tap.c
index 2530627a9a..224fb37310 100644
--- a/net/tap.c
+++ b/net/tap.c
@@ -43,6 +43,7 @@
 #include "qemu/main-loop.h"
 #include "qemu/sockets.h"
 #include "hw/virtio/vhost.h"
+#include "trace.h"
 
 #include "net/tap.h"
 
@@ -148,6 +149,9 @@ static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
     g_autofree struct iovec *iov_copy = NULL;
     struct virtio_net_hdr hdr = { };
 
+    trace_tap_receive_iov(s->using_vnet_hdr, s->host_vnet_hdr_len, iovcnt,
+                          iov->iov_len);
+
     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
         iov_copy = g_new(struct iovec, iovcnt + 1);
         iov_copy[0].iov_base = &hdr;
@@ -183,6 +187,49 @@ static void tap_send_completed(NetClientState *nc, ssize_t len)
     tap_read_poll(s, true);
 }
 
+static char *tap_dump_packet(const uint8_t *buf, int size)
+{
+    int i, j;
+    char hex_line[80];  /* Enough space for hex pairs + spaces */
+    char ascii_line[17]; /* 16 + 1 for null terminator */
+    GString *dump_str = g_string_new(NULL);
+
+    g_string_append_printf(dump_str, "Packet dump (%d bytes):\n", size);
+
+    for (i = 0; i < size; i += 16) {
+        memset(hex_line, 0, sizeof(hex_line));
+        memset(ascii_line, 0, sizeof(ascii_line));
+
+        /* Build hex line in groups of 2 bytes (4 hex chars) */
+        int hex_pos = 0;
+        for (j = 0; j < 16 && (i + j) < size; j += 2) {
+            if (i + j + 1 < size) {
+                /* Two bytes available */
+                hex_pos += snprintf(hex_line + hex_pos,
+                                    sizeof(hex_line) - hex_pos,
+                                    "%02x%02x ", buf[i + j], buf[i + j + 1]);
+            } else {
+                /* Only one byte left */
+                hex_pos += snprintf(hex_line + hex_pos,
+                                    sizeof(hex_line) - hex_pos,
+                                    "%02x   ", buf[i + j]);
+            }
+        }
+
+        /* Build ASCII line */
+        for (j = 0; j < 16 && (i + j) < size; j++) {
+            uint8_t byte = buf[i + j];
+            ascii_line[j] = (byte >= 32 && byte <= 126) ? byte : '.';
+        }
+
+        /* Add the line in tcpdump-like format */
+        g_string_append_printf(dump_str, "\t0x%04x:  %-40s %s\n",
+                               i, hex_line, ascii_line);
+    }
+
+    return g_string_free(dump_str, false);
+}
+
 static void tap_send(void *opaque)
 {
     TAPState *s = opaque;
@@ -199,6 +246,13 @@ static void tap_send(void *opaque)
             break;
         }
 
+        if (trace_event_get_state_backends(TRACE_TAP_PACKET_DUMP)) {
+            g_autofree char *dump = tap_dump_packet(s->buf, size);
+            trace_tap_packet_dump(dump);
+        }
+
+        trace_tap_send_packet(s->using_vnet_hdr, s->host_vnet_hdr_len, size);
+
         if (s->host_vnet_hdr_len && size <= s->host_vnet_hdr_len) {
             /* Invalid packet */
             break;
@@ -980,6 +1034,8 @@ int tap_enable(NetClientState *nc)
     TAPState *s = DO_UPCAST(TAPState, nc, nc);
     int ret;
 
+    trace_tap_enable();
+
     if (s->enabled) {
         return 0;
     } else {
@@ -997,6 +1053,8 @@ int tap_disable(NetClientState *nc)
     TAPState *s = DO_UPCAST(TAPState, nc, nc);
     int ret;
 
+    trace_tap_disable();
+
     if (s->enabled == 0) {
         return 0;
     } else {
diff --git a/net/trace-events b/net/trace-events
index cda960f42b..b51427f539 100644
--- a/net/trace-events
+++ b/net/trace-events
@@ -29,3 +29,10 @@ vhost_vdpa_set_address_space_id(void *v, unsigned vq_group, unsigned asid_num) "
 vhost_vdpa_net_load_cmd(void *s, uint8_t class, uint8_t cmd, int data_num, int data_size) "vdpa state: %p class: %u cmd: %u sg_num: %d size: %d"
 vhost_vdpa_net_load_cmd_retval(void *s, uint8_t class, uint8_t cmd, int r) "vdpa state: %p class: %u cmd: %u retval: %d"
 vhost_vdpa_net_load_mq(void *s, int ncurqps) "vdpa state: %p current_qpairs: %d"
+
+# tap.c
+tap_receive_iov(bool using_vnet_hdr, uint32_t host_vnet_hdr_len, int iovcnt, size_t iov_len) "using_vnet_hdr:%d host_vnet_hdr_len:%u iovcnt:%d iov_len:%zu"
+tap_send_packet(bool using_vnet_hdr, uint32_t host_vnet_hdr_len, int size) "using_vnet_hdr:%d host_vnet_hdr_len:%u size:%d"
+tap_enable(void) "tap enabled"
+tap_disable(void) "tap disabled"
+tap_packet_dump(const char *dump_str) "%s"
-- 
2.48.1
Re: [PATCH v2 1/8] net/tap: add some trace points
Posted by Daniel P. Berrangé 3 days ago
On Wed, Sep 03, 2025 at 04:36:58PM +0300, Vladimir Sementsov-Ogievskiy wrote:
> Add trace points to simplify debugging migration.
> 
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> ---
>  net/tap.c        | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
>  net/trace-events |  7 ++++++
>  2 files changed, 65 insertions(+)
> 
> diff --git a/net/tap.c b/net/tap.c
> index 2530627a9a..224fb37310 100644
> --- a/net/tap.c
> +++ b/net/tap.c
> @@ -43,6 +43,7 @@
>  #include "qemu/main-loop.h"
>  #include "qemu/sockets.h"
>  #include "hw/virtio/vhost.h"
> +#include "trace.h"
>  
>  #include "net/tap.h"
>  
> @@ -148,6 +149,9 @@ static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
>      g_autofree struct iovec *iov_copy = NULL;
>      struct virtio_net_hdr hdr = { };
>  
> +    trace_tap_receive_iov(s->using_vnet_hdr, s->host_vnet_hdr_len, iovcnt,
> +                          iov->iov_len);
> +
>      if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
>          iov_copy = g_new(struct iovec, iovcnt + 1);
>          iov_copy[0].iov_base = &hdr;
> @@ -183,6 +187,49 @@ static void tap_send_completed(NetClientState *nc, ssize_t len)
>      tap_read_poll(s, true);
>  }
>  
> +static char *tap_dump_packet(const uint8_t *buf, int size)
> +{
> +    int i, j;
> +    char hex_line[80];  /* Enough space for hex pairs + spaces */
> +    char ascii_line[17]; /* 16 + 1 for null terminator */
> +    GString *dump_str = g_string_new(NULL);
> +
> +    g_string_append_printf(dump_str, "Packet dump (%d bytes):\n", size);

It occurrs to me that this ability to dump a buffer is likely useful for
other areas of QEMU. How about moving this to somewhere in util/ and
renaming it to something like "dump_buffer".

You can drop the 'Packet dump' prefix, and bytes count, as you can
include that info in the trace-events format string instead.

> +
> +    for (i = 0; i < size; i += 16) {
> +        memset(hex_line, 0, sizeof(hex_line));
> +        memset(ascii_line, 0, sizeof(ascii_line));
> +
> +        /* Build hex line in groups of 2 bytes (4 hex chars) */
> +        int hex_pos = 0;
> +        for (j = 0; j < 16 && (i + j) < size; j += 2) {
> +            if (i + j + 1 < size) {
> +                /* Two bytes available */
> +                hex_pos += snprintf(hex_line + hex_pos,
> +                                    sizeof(hex_line) - hex_pos,
> +                                    "%02x%02x ", buf[i + j], buf[i + j + 1]);
> +            } else {
> +                /* Only one byte left */
> +                hex_pos += snprintf(hex_line + hex_pos,
> +                                    sizeof(hex_line) - hex_pos,
> +                                    "%02x   ", buf[i + j]);
> +            }
> +        }
> +
> +        /* Build ASCII line */
> +        for (j = 0; j < 16 && (i + j) < size; j++) {
> +            uint8_t byte = buf[i + j];
> +            ascii_line[j] = (byte >= 32 && byte <= 126) ? byte : '.';
> +        }
> +
> +        /* Add the line in tcpdump-like format */
> +        g_string_append_printf(dump_str, "\t0x%04x:  %-40s %s\n",
> +                               i, hex_line, ascii_line);
> +    }
> +
> +    return g_string_free(dump_str, false);
> +}

> diff --git a/net/trace-events b/net/trace-events
> index cda960f42b..b51427f539 100644
> --- a/net/trace-events
> +++ b/net/trace-events
> @@ -29,3 +29,10 @@ vhost_vdpa_set_address_space_id(void *v, unsigned vq_group, unsigned asid_num) "
>  vhost_vdpa_net_load_cmd(void *s, uint8_t class, uint8_t cmd, int data_num, int data_size) "vdpa state: %p class: %u cmd: %u sg_num: %d size: %d"
>  vhost_vdpa_net_load_cmd_retval(void *s, uint8_t class, uint8_t cmd, int r) "vdpa state: %p class: %u cmd: %u retval: %d"
>  vhost_vdpa_net_load_mq(void *s, int ncurqps) "vdpa state: %p current_qpairs: %d"
> +
> +# tap.c
> +tap_receive_iov(bool using_vnet_hdr, uint32_t host_vnet_hdr_len, int iovcnt, size_t iov_len) "using_vnet_hdr:%d host_vnet_hdr_len:%u iovcnt:%d iov_len:%zu"
> +tap_send_packet(bool using_vnet_hdr, uint32_t host_vnet_hdr_len, int size) "using_vnet_hdr:%d host_vnet_hdr_len:%u size:%d"
> +tap_enable(void) "tap enabled"
> +tap_disable(void) "tap disabled"
> +tap_packet_dump(const char *dump_str) "%s"

This could be 

     tap_packet_dump(size_t bytes, const char *dump_str) "Package dump (%zu bytes) %s"

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Re: [PATCH v2 1/8] net/tap: add some trace points
Posted by Vladimir Sementsov-Ogievskiy 1 day, 2 hours ago
On 03.09.25 17:11, Daniel P. Berrangé wrote:
> It occurrs to me that this ability to dump a buffer is likely useful for
> other areas of QEMU. How about moving this to somewhere in util/ and
> renaming it to something like "dump_buffer".

Ha, I started to do it, and found that we already have util/hexdump.c :)

-- 
Best regards,
Vladimir

Re: [PATCH v2 1/8] net/tap: add some trace points
Posted by Vladimir Sementsov-Ogievskiy 3 days ago
On 03.09.25 17:11, Daniel P. Berrangé wrote:
> On Wed, Sep 03, 2025 at 04:36:58PM +0300, Vladimir Sementsov-Ogievskiy wrote:
>> Add trace points to simplify debugging migration.
>>
>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
>> ---
>>   net/tap.c        | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
>>   net/trace-events |  7 ++++++
>>   2 files changed, 65 insertions(+)
>>
>> diff --git a/net/tap.c b/net/tap.c
>> index 2530627a9a..224fb37310 100644
>> --- a/net/tap.c
>> +++ b/net/tap.c
>> @@ -43,6 +43,7 @@
>>   #include "qemu/main-loop.h"
>>   #include "qemu/sockets.h"
>>   #include "hw/virtio/vhost.h"
>> +#include "trace.h"
>>   
>>   #include "net/tap.h"
>>   
>> @@ -148,6 +149,9 @@ static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
>>       g_autofree struct iovec *iov_copy = NULL;
>>       struct virtio_net_hdr hdr = { };
>>   
>> +    trace_tap_receive_iov(s->using_vnet_hdr, s->host_vnet_hdr_len, iovcnt,
>> +                          iov->iov_len);
>> +
>>       if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
>>           iov_copy = g_new(struct iovec, iovcnt + 1);
>>           iov_copy[0].iov_base = &hdr;
>> @@ -183,6 +187,49 @@ static void tap_send_completed(NetClientState *nc, ssize_t len)
>>       tap_read_poll(s, true);
>>   }
>>   
>> +static char *tap_dump_packet(const uint8_t *buf, int size)
>> +{
>> +    int i, j;
>> +    char hex_line[80];  /* Enough space for hex pairs + spaces */
>> +    char ascii_line[17]; /* 16 + 1 for null terminator */
>> +    GString *dump_str = g_string_new(NULL);
>> +
>> +    g_string_append_printf(dump_str, "Packet dump (%d bytes):\n", size);
> 
> It occurrs to me that this ability to dump a buffer is likely useful for
> other areas of QEMU. How about moving this to somewhere in util/ and
> renaming it to something like "dump_buffer".

Agreed, will do.

> 
> You can drop the 'Packet dump' prefix, and bytes count, as you can
> include that info in the trace-events format string instead.
> 
>> +
>> +    for (i = 0; i < size; i += 16) {
>> +        memset(hex_line, 0, sizeof(hex_line));
>> +        memset(ascii_line, 0, sizeof(ascii_line));
>> +
>> +        /* Build hex line in groups of 2 bytes (4 hex chars) */
>> +        int hex_pos = 0;
>> +        for (j = 0; j < 16 && (i + j) < size; j += 2) {
>> +            if (i + j + 1 < size) {
>> +                /* Two bytes available */
>> +                hex_pos += snprintf(hex_line + hex_pos,
>> +                                    sizeof(hex_line) - hex_pos,
>> +                                    "%02x%02x ", buf[i + j], buf[i + j + 1]);
>> +            } else {
>> +                /* Only one byte left */
>> +                hex_pos += snprintf(hex_line + hex_pos,
>> +                                    sizeof(hex_line) - hex_pos,
>> +                                    "%02x   ", buf[i + j]);
>> +            }
>> +        }
>> +
>> +        /* Build ASCII line */
>> +        for (j = 0; j < 16 && (i + j) < size; j++) {
>> +            uint8_t byte = buf[i + j];
>> +            ascii_line[j] = (byte >= 32 && byte <= 126) ? byte : '.';
>> +        }
>> +
>> +        /* Add the line in tcpdump-like format */
>> +        g_string_append_printf(dump_str, "\t0x%04x:  %-40s %s\n",
>> +                               i, hex_line, ascii_line);
>> +    }
>> +
>> +    return g_string_free(dump_str, false);
>> +}
> 
>> diff --git a/net/trace-events b/net/trace-events
>> index cda960f42b..b51427f539 100644
>> --- a/net/trace-events
>> +++ b/net/trace-events
>> @@ -29,3 +29,10 @@ vhost_vdpa_set_address_space_id(void *v, unsigned vq_group, unsigned asid_num) "
>>   vhost_vdpa_net_load_cmd(void *s, uint8_t class, uint8_t cmd, int data_num, int data_size) "vdpa state: %p class: %u cmd: %u sg_num: %d size: %d"
>>   vhost_vdpa_net_load_cmd_retval(void *s, uint8_t class, uint8_t cmd, int r) "vdpa state: %p class: %u cmd: %u retval: %d"
>>   vhost_vdpa_net_load_mq(void *s, int ncurqps) "vdpa state: %p current_qpairs: %d"
>> +
>> +# tap.c
>> +tap_receive_iov(bool using_vnet_hdr, uint32_t host_vnet_hdr_len, int iovcnt, size_t iov_len) "using_vnet_hdr:%d host_vnet_hdr_len:%u iovcnt:%d iov_len:%zu"
>> +tap_send_packet(bool using_vnet_hdr, uint32_t host_vnet_hdr_len, int size) "using_vnet_hdr:%d host_vnet_hdr_len:%u size:%d"
>> +tap_enable(void) "tap enabled"
>> +tap_disable(void) "tap disabled"
>> +tap_packet_dump(const char *dump_str) "%s"
> 
> This could be
> 
>       tap_packet_dump(size_t bytes, const char *dump_str) "Package dump (%zu bytes) %s"
> 
> With regards,
> Daniel


-- 
Best regards,
Vladimir