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 <[email protected]>
> ---
>  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 :|


Reply via email to