Refactor the negotiate() and connected functions
[nbd.git] / nbd-trdump.c
blobeb02245dec38e27781463109587a6f7583ba1270
1 /*
2 * nbd-trdump.c
4 * Takes an nbd transaction log file on stdin and translates it into something
5 * comprehensible
6 */
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include <unistd.h>
16 #include "config.h"
17 /* We don't want to do syslog output in this program */
18 #undef ISSERVER
19 #include "cliserv.h"
20 #include "nbd.h"
21 #include "nbd-helper.h"
23 #define BUFSIZE 131072
24 static char tmpbuf[BUFSIZE];
26 static bool g_with_datalog = false;
28 static inline void doread(int f, void *buf, size_t len) {
29 ssize_t res;
31 while(len>0) {
32 if((res=read(f, buf, len)) <=0) {
33 if (!res)
34 exit(0);
35 perror ("Error reading transactions");
36 exit(1);
38 len-=res;
39 buf+=res;
43 int main(int argc, char**argv) {
44 struct nbd_request req;
45 struct nbd_reply rep;
46 struct nbd_structured_reply srep;
47 uint32_t magic;
48 uint64_t cookie;
49 uint32_t error;
50 uint32_t command;
51 uint32_t len;
52 uint64_t offset;
53 uint16_t flags;
54 uint16_t type;
55 uint32_t paylen;
56 const char * ctext;
57 int readfd = 0; /* stdin */
59 if(argc > 1) {
60 int retval=0;
61 if(strcmp(argv[1], "--help") && strcmp(argv[1], "-h")) {
62 printf("E: unknown option %s.\n", argv[1]);
63 retval=1;
65 printf("This is nbd-trdump, part of nbd %s.\n", PACKAGE_VERSION);
66 printf("Use: %s < transactionlog\n", argv[0]);
67 return retval;
70 while (1) {
71 /* Read a request or reply from the transaction file */
72 doread(readfd, &magic, sizeof(magic));
73 magic = ntohl(magic);
74 switch (magic) {
75 case NBD_REQUEST_MAGIC:
76 doread(readfd, sizeof(magic)+(char *)(&req), sizeof(struct nbd_request)-sizeof(magic));
77 cookie = ntohll(req.cookie);
78 offset = ntohll(req.from);
79 len = ntohl(req.len);
80 command = ntohl(req.type);
82 ctext = getcommandname(command & NBD_CMD_MASK_COMMAND);
84 printf("> H=%016llx C=0x%08x (%20s+%4s) O=%016llx L=%08x\n",
85 (long long unsigned int) cookie,
86 command,
87 ctext,
88 (command & NBD_CMD_FLAG_FUA)?"FUA":"NONE",
89 (long long unsigned int) offset,
90 len);
91 if (((command & NBD_CMD_MASK_COMMAND) == NBD_CMD_WRITE) &&
92 g_with_datalog) {
93 while (len > 0) {
94 uint32_t tmplen = len;
96 if (tmplen > BUFSIZE)
97 tmplen = BUFSIZE;
98 doread(readfd, tmpbuf, tmplen);
99 len -= tmplen;
103 break;
104 case NBD_REPLY_MAGIC:
105 doread(readfd, sizeof(magic)+(char *)(&rep), sizeof(struct nbd_reply)-sizeof(magic));
106 cookie = ntohll(rep.cookie);
107 error = ntohl(rep.error);
109 printf("< H=%016llx E=0x%08x\n",
110 (long long unsigned int) cookie,
111 error);
112 break;
114 case NBD_TRACELOG_MAGIC:
115 doread(readfd, sizeof(magic)+(char *)(&req), sizeof(struct nbd_request)-sizeof(magic));
116 cookie = ntohll(req.cookie);
117 offset = ntohll(req.from);
118 len = ntohl(req.len);
119 command = ntohl(req.type);
121 ctext = gettracelogname(command);
123 printf("TRACE_OPTION C=0x%08x (%23s) O=%016llx L=%08x\n",
124 command,
125 ctext,
126 (long long unsigned int) offset,
127 len);
128 if (offset == NBD_TRACELOG_FROM_MAGIC) {
130 switch (command) {
131 case NBD_TRACELOG_SET_DATALOG:
132 g_with_datalog = !!len;
133 printf("TRACE_OPTION DATALOG set to %d.\n", (int)g_with_datalog);
134 break;
135 default:
136 printf("TRACE_OPTION ? Unknown type\n");
138 } else {
139 printf("TRACE_OPTION ? Unknown FROM_MAGIC\n");
141 break;
142 case NBD_STRUCTURED_REPLY_MAGIC:
143 doread(readfd, sizeof(magic)+(char*)(&srep), sizeof(struct nbd_structured_reply)-sizeof(magic));
144 cookie = ntohll(srep.cookie);
145 flags = ntohs(srep.flags);
146 type = ntohs(srep.type);
147 paylen = ntohs(srep.paylen);
148 ctext = getstructreplname(type);
150 printf("<S: H=%016llx T=0x%04x (%20s) F=0x%04x L=0x%04x\n",
151 (long long unsigned int) cookie,
152 type,
153 ctext,
154 flags,
155 paylen);
157 break;
158 default:
159 printf("? Unknown transaction type %08x\n",magic);
160 break;
164 /* never reached */
165 return 0;