Add trace points to simplify debugging migration.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsement...@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 45d1bcd326..44bd6eefd8 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;
@@ -992,6 +1046,8 @@ int tap_enable(NetClientState *nc)
     TAPState *s = DO_UPCAST(TAPState, nc, nc);
     int ret;
 
+    trace_tap_enable();
+
     if (s->enabled) {
         return 0;
     } else {
@@ -1009,6 +1065,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


Reply via email to