[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[PATCH 1/9] nbd-trdump: Add support for all commands



From: Manfred Spraul <manfred.spraul@de.bosch.com>

Support for pretty-printing NBD_CMD_TRIM and NBD_CMD_WRITE_ZEROES is
missing in nbd-trdump.
In addition, only the commands right now implemented in nbd-server
are supported, instead of all commands defined in the protocol.

Thus:
- move the existing getcommandname() helper function into a new
  nbd-helper.h header file.
- use the helper function in nbd-trdump
- add all commands from proto.md.
- in nbd-trdump: change ctest from "char *" to "const char *"
  and increase number of characters in printf statement.

Signed-off-by: Manfred Spraul <manfred.spraul@de.bosch.com>
---
 nbd-helper.h | 32 ++++++++++++++++++++++++++++++++
 nbd-server.c | 26 +-------------------------
 nbd-trdump.c | 24 +++++-------------------
 nbd.h        |  5 ++++-
 4 files changed, 42 insertions(+), 45 deletions(-)
 create mode 100644 nbd-helper.h

diff --git a/nbd-helper.h b/nbd-helper.h
new file mode 100644
index 0000000..86ac1c2
--- /dev/null
+++ b/nbd-helper.h
@@ -0,0 +1,32 @@
+#ifndef NBD_HELPER_H
+#define NBD_HELPER_H
+
+#include "nbd.h"
+
+/* Functions */
+
+/**
+ * Translate a command name into human readable form
+ *
+ * @param command The command number (after applying NBD_CMD_MASK_COMMAND)
+ * @return pointer to the command name
+ **/
+#define ENUM2STR(x)	case x: return #x
+static inline const char * getcommandname(uint32_t command) {
+	switch (command) {
+	ENUM2STR(NBD_CMD_READ);
+	ENUM2STR(NBD_CMD_WRITE);
+	ENUM2STR(NBD_CMD_DISC);
+	ENUM2STR(NBD_CMD_FLUSH);
+	ENUM2STR(NBD_CMD_TRIM);
+	ENUM2STR(NBD_CMD_CACHE);
+	ENUM2STR(NBD_CMD_WRITE_ZEROES);
+	ENUM2STR(NBD_CMD_BLOCK_STATUS);
+	ENUM2STR(NBD_CMD_RESIZE);
+	default:
+		return "UNKNOWN";
+	}
+}
+#undef ENUM2STR
+
+#endif //NBD_HELPER_H
diff --git a/nbd-server.c b/nbd-server.c
index 1eff99d..3c3589e 100644
--- a/nbd-server.c
+++ b/nbd-server.c
@@ -129,6 +129,7 @@
 #include "netdb-compat.h"
 #include "backend.h"
 #include "treefiles.h"
+#include "nbd-helper.h"
 
 #ifdef WITH_SDP
 #include <sdp_inet.h>
@@ -285,31 +286,6 @@ struct generic_conf {
 	gint threads;		/**< maximum number of parallel threads we want to run */
 };
 
-/**
- * Translate a command name into human readable form
- *
- * @param command The command number (after applying NBD_CMD_MASK_COMMAND)
- * @return pointer to the command name
- **/
-static inline const char * getcommandname(uint64_t command) {
-	switch (command) {
-	case NBD_CMD_READ:
-		return "NBD_CMD_READ";
-	case NBD_CMD_WRITE:
-		return "NBD_CMD_WRITE";
-	case NBD_CMD_DISC:
-		return "NBD_CMD_DISC";
-	case NBD_CMD_FLUSH:
-		return "NBD_CMD_FLUSH";
-	case NBD_CMD_TRIM:
-		return "NBD_CMD_TRIM";
-	case NBD_CMD_WRITE_ZEROES:
-		return "NBD_CMD_WRITE_ZEROES";
-	default:
-		return "UNKNOWN";
-	}
-}
-
 #if HAVE_GNUTLS
 static int writeit_tls(gnutls_session_t s, void *buf, size_t len) {
 	ssize_t res;
diff --git a/nbd-trdump.c b/nbd-trdump.c
index 1185224..d9c2ae5 100644
--- a/nbd-trdump.c
+++ b/nbd-trdump.c
@@ -17,6 +17,7 @@
 #undef ISSERVER
 #include "cliserv.h"
 #include "nbd.h"
+#include "nbd-helper.h"
 
 static inline void doread(int f, void *buf, size_t len) {
         ssize_t res;
@@ -42,7 +43,7 @@ int main(int argc, char**argv) {
 	uint32_t command;
 	uint32_t len;
 	uint64_t offset;
-	char * ctext;
+	const char * ctext;
 	int readfd = 0; /* stdin */
 
 	if(argc > 1) {
@@ -68,24 +69,9 @@ int main(int argc, char**argv) {
 			len = ntohl(req.len);
 			command = ntohl(req.type);
 			
-			switch (command & NBD_CMD_MASK_COMMAND) {
-			case NBD_CMD_READ:
-				ctext="NBD_CMD_READ";
-				break;
-			case NBD_CMD_WRITE:
-				ctext="NBD_CMD_WRITE";
-				break;
-			case NBD_CMD_DISC:
-				ctext="NBD_CMD_DISC";
-				break;
-			case NBD_CMD_FLUSH:
-				ctext="NBD_CMD_FLUSH";
-				break;
-			default:
-				ctext="UNKNOWN";
-				break;
-			}
-			printf("> H=%016llx C=0x%08x (%13s+%4s) O=%016llx L=%08x\n",
+			ctext = getcommandname(command & NBD_CMD_MASK_COMMAND);
+
+			printf("> H=%016llx C=0x%08x (%20s+%4s) O=%016llx L=%08x\n",
 			       (long long unsigned int) handle,
 			       command,
 			       ctext,
diff --git a/nbd.h b/nbd.h
index 6326b6e..1e57b3e 100644
--- a/nbd.h
+++ b/nbd.h
@@ -35,7 +35,10 @@ enum {
 	NBD_CMD_DISC = 2,
 	NBD_CMD_FLUSH = 3,
 	NBD_CMD_TRIM = 4,
-	NBD_CMD_WRITE_ZEROES = 6
+	NBD_CMD_CACHE = 5,
+	NBD_CMD_WRITE_ZEROES = 6,
+	NBD_CMD_BLOCK_STATUS = 7,
+	NBD_CMD_RESIZE = 8
 };
 
 #define NBD_CMD_MASK_COMMAND 0x0000ffff
-- 
2.34.1


Reply to: