[Libguestfs] [libnbd PATCH 1/4] internal: Track traffic stats

Eric Blake eblake at redhat.com
Fri Sep 2 22:14:22 UTC 2022


Increment statistics counters every time we send or receive bytes, as
well as every time we send or receive a magic number delineating a
portion of the data stream (the server's initial NBD_MAGIC,
handshaking NBD_NEW_VERSION/NBD_OPT answered by one or more
NBD_REP_MAGIC replies, and transmission NBD_REQUEST_MAGIC/NBD_CMD
answered by NBD_SIMPLE_REPLY_MAGIC or one or more
NBD_STRUCTURED_REPLY_MAGIC).  A later patch will then expose these
statistics to the client application.
---
 lib/internal.h                                   | 6 ++++++
 generator/states-issue-command.c                 | 3 ++-
 generator/states-magic.c                         | 7 +++++--
 generator/states-newstyle-opt-export-name.c      | 3 ++-
 generator/states-newstyle-opt-go.c               | 1 +
 generator/states-newstyle-opt-list.c             | 3 ++-
 generator/states-newstyle-opt-meta-context.c     | 1 +
 generator/states-newstyle-opt-starttls.c         | 3 ++-
 generator/states-newstyle-opt-structured-reply.c | 1 +
 generator/states-newstyle.c                      | 1 +
 generator/states-reply.c                         | 6 ++++--
 generator/states.c                               | 2 ++
 12 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/lib/internal.h b/lib/internal.h
index 8aaff151..1e991ef2 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -158,6 +158,12 @@ struct nbd_handle {

   int64_t unique;               /* Used for generating cookie numbers. */

+  /* Traffic statistics. */
+  uint64_t bytes_sent;
+  uint64_t chunks_sent;
+  uint64_t bytes_received;
+  uint64_t chunks_received;
+
   /* For debugging. */
   bool debug;
   nbd_debug_callback debug_callback;
diff --git a/generator/states-issue-command.c b/generator/states-issue-command.c
index a8101144..df9295b5 100644
--- a/generator/states-issue-command.c
+++ b/generator/states-issue-command.c
@@ -1,5 +1,5 @@
 /* nbd client library in userspace: state machine
- * Copyright (C) 2013-2020 Red Hat Inc.
+ * Copyright (C) 2013-2022 Red Hat Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -47,6 +47,7 @@  ISSUE_COMMAND.START:
   h->request.handle = htobe64 (cmd->cookie);
   h->request.offset = htobe64 (cmd->offset);
   h->request.count = htobe32 ((uint32_t) cmd->count);
+  h->chunks_sent++;
   h->wbuf = &h->request;
   h->wlen = sizeof (h->request);
   if (cmd->type == NBD_CMD_WRITE || cmd->next)
diff --git a/generator/states-magic.c b/generator/states-magic.c
index d694a6ad..b9d4f9b2 100644
--- a/generator/states-magic.c
+++ b/generator/states-magic.c
@@ -1,5 +1,5 @@
 /* nbd client library in userspace: state machine
- * Copyright (C) 2013-2020 Red Hat Inc.
+ * Copyright (C) 2013-2022 Red Hat Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -44,10 +44,13 @@  MAGIC.CHECK_MAGIC:
   version = be64toh (h->sbuf.new_handshake.version);
   if (version == NBD_NEW_VERSION) {
     assert (h->opt_current == 0);
+    h->chunks_received++;
     SET_NEXT_STATE (%.NEWSTYLE.START);
   }
-  else if (version == NBD_OLD_VERSION)
+  else if (version == NBD_OLD_VERSION) {
+    h->chunks_received++;
     SET_NEXT_STATE (%.OLDSTYLE.START);
+  }
   else {
     SET_NEXT_STATE (%.DEAD);
     set_error (0, "handshake: server is not either an oldstyle or fixed newstyle NBD server");
diff --git a/generator/states-newstyle-opt-export-name.c b/generator/states-newstyle-opt-export-name.c
index 05975b61..398aa680 100644
--- a/generator/states-newstyle-opt-export-name.c
+++ b/generator/states-newstyle-opt-export-name.c
@@ -1,5 +1,5 @@
 /* nbd client library in userspace: state machine
- * Copyright (C) 2013-2020 Red Hat Inc.
+ * Copyright (C) 2013-2022 Red Hat Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,6 +23,7 @@  NEWSTYLE.OPT_EXPORT_NAME.START:
   h->sbuf.option.version = htobe64 (NBD_NEW_VERSION);
   h->sbuf.option.option = htobe32 (NBD_OPT_EXPORT_NAME);
   h->sbuf.option.optlen = htobe32 (strlen (h->export_name));
+  h->chunks_sent++;
   h->wbuf = &h->sbuf;
   h->wlen = sizeof h->sbuf.option;
   h->wflags = MSG_MORE;
diff --git a/generator/states-newstyle-opt-go.c b/generator/states-newstyle-opt-go.c
index b7354aed..4c6e9900 100644
--- a/generator/states-newstyle-opt-go.c
+++ b/generator/states-newstyle-opt-go.c
@@ -40,6 +40,7 @@  NEWSTYLE.OPT_GO.START:
   h->sbuf.option.optlen =
     htobe32 (/* exportnamelen */ 4 + strlen (h->export_name)
              + sizeof nrinfos + 2 * nrinfos);
+  h->chunks_sent++;
   h->wbuf = &h->sbuf;
   h->wlen = sizeof h->sbuf.option;
   h->wflags = MSG_MORE;
diff --git a/generator/states-newstyle-opt-list.c b/generator/states-newstyle-opt-list.c
index a549bdc9..7bad8afe 100644
--- a/generator/states-newstyle-opt-list.c
+++ b/generator/states-newstyle-opt-list.c
@@ -1,5 +1,5 @@
 /* nbd client library in userspace: state machine
- * Copyright (C) 2013-2020 Red Hat Inc.
+ * Copyright (C) 2013-2022 Red Hat Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -29,6 +29,7 @@  NEWSTYLE.OPT_LIST.START:
   h->sbuf.option.version = htobe64 (NBD_NEW_VERSION);
   h->sbuf.option.option = htobe32 (NBD_OPT_LIST);
   h->sbuf.option.optlen = 0;
+  h->chunks_sent++;
   h->wbuf = &h->sbuf;
   h->wlen = sizeof (h->sbuf.option);
   SET_NEXT_STATE (%SEND);
diff --git a/generator/states-newstyle-opt-meta-context.c b/generator/states-newstyle-opt-meta-context.c
index 25c1c2fd..84dbcd69 100644
--- a/generator/states-newstyle-opt-meta-context.c
+++ b/generator/states-newstyle-opt-meta-context.c
@@ -75,6 +75,7 @@  NEWSTYLE.OPT_META_CONTEXT.START:
   h->sbuf.option.version = htobe64 (NBD_NEW_VERSION);
   h->sbuf.option.option = htobe32 (opt);
   h->sbuf.option.optlen = htobe32 (len);
+  h->chunks_sent++;
   h->wbuf = &h->sbuf;
   h->wlen = sizeof (h->sbuf.option);
   h->wflags = MSG_MORE;
diff --git a/generator/states-newstyle-opt-starttls.c b/generator/states-newstyle-opt-starttls.c
index 9eab023b..a9383ced 100644
--- a/generator/states-newstyle-opt-starttls.c
+++ b/generator/states-newstyle-opt-starttls.c
@@ -1,5 +1,5 @@
 /* nbd client library in userspace: state machine
- * Copyright (C) 2013-2020 Red Hat Inc.
+ * Copyright (C) 2013-2022 Red Hat Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -30,6 +30,7 @@  NEWSTYLE.OPT_STARTTLS.START:
   h->sbuf.option.version = htobe64 (NBD_NEW_VERSION);
   h->sbuf.option.option = htobe32 (NBD_OPT_STARTTLS);
   h->sbuf.option.optlen = 0;
+  h->chunks_sent++;
   h->wbuf = &h->sbuf;
   h->wlen = sizeof (h->sbuf.option);
   SET_NEXT_STATE (%SEND);
diff --git a/generator/states-newstyle-opt-structured-reply.c b/generator/states-newstyle-opt-structured-reply.c
index 19c2ed7e..0ea0fd43 100644
--- a/generator/states-newstyle-opt-structured-reply.c
+++ b/generator/states-newstyle-opt-structured-reply.c
@@ -32,6 +32,7 @@  NEWSTYLE.OPT_STRUCTURED_REPLY.START:
   h->sbuf.option.version = htobe64 (NBD_NEW_VERSION);
   h->sbuf.option.option = htobe32 (NBD_OPT_STRUCTURED_REPLY);
   h->sbuf.option.optlen = htobe32 (0);
+  h->chunks_sent++;
   h->wbuf = &h->sbuf;
   h->wlen = sizeof h->sbuf.option;
   SET_NEXT_STATE (%SEND);
diff --git a/generator/states-newstyle.c b/generator/states-newstyle.c
index 41e7cc35..e84823e3 100644
--- a/generator/states-newstyle.c
+++ b/generator/states-newstyle.c
@@ -207,6 +207,7 @@  NEWSTYLE.PREPARE_OPT_ABORT:
   h->sbuf.option.version = htobe64 (NBD_NEW_VERSION);
   h->sbuf.option.option = htobe32 (NBD_OPT_ABORT);
   h->sbuf.option.optlen = htobe32 (0);
+  h->chunks_sent++;
   h->wbuf = &h->sbuf;
   h->wlen = sizeof h->sbuf.option;
   SET_NEXT_STATE (%SEND_OPT_ABORT);
diff --git a/generator/states-reply.c b/generator/states-reply.c
index 07363426..5a94ac1b 100644
--- a/generator/states-reply.c
+++ b/generator/states-reply.c
@@ -93,10 +93,10 @@  REPLY.START:
     return 0;
   }
 #ifdef DUMP_PACKETS
-  if (h->rbuf != NULL)
-    nbd_internal_hexdump (h->rbuf, r, stderr);
+  nbd_internal_hexdump (h->rbuf, r, stderr);
 #endif

+  h->bytes_received += r;
   h->rbuf += r;
   h->rlen -= r;
   SET_NEXT_STATE (%RECV_REPLY);
@@ -117,9 +117,11 @@  REPLY.CHECK_SIMPLE_OR_STRUCTURED_REPLY:

   magic = be32toh (h->sbuf.simple_reply.magic);
   if (magic == NBD_SIMPLE_REPLY_MAGIC) {
+    h->chunks_received++;
     SET_NEXT_STATE (%SIMPLE_REPLY.START);
   }
   else if (magic == NBD_STRUCTURED_REPLY_MAGIC) {
+    h->chunks_received++;
     SET_NEXT_STATE (%STRUCTURED_REPLY.START);
   }
   else {
diff --git a/generator/states.c b/generator/states.c
index d2aa51d3..547da753 100644
--- a/generator/states.c
+++ b/generator/states.c
@@ -89,6 +89,7 @@ recv_into_rbuf (struct nbd_handle *h)
 #ifdef DUMP_PACKETS
   nbd_internal_hexdump (rbuf, r, stderr);
 #endif
+  h->bytes_received += r;
   if (h->rbuf)
     h->rbuf += r;
   h->rlen -= r;
@@ -112,6 +113,7 @@ send_from_wbuf (struct nbd_handle *h)
     /* sock->ops->send called set_error already. */
     return -1;
   }
+  h->bytes_sent += r;
   h->wbuf += r;
   h->wlen -= r;
   if (h->wlen == 0)
-- 
2.37.2



More information about the Libguestfs mailing list