Actually add the nbd-trdump manpage rather than just infrastructure
[nbd.git] / nbd-trdump.c
1 /*
2  * nbd-trdump.c
3  *
4  * Takes an nbd transaction log file on stdin and translates it into something
5  * comprehensible
6  */
7
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 <unistd.h>
15 #include "config.h"
16 #include "cliserv.h"
17 #include "nbd.h"
18
19 static inline void doread(int f, void *buf, size_t len) {
20         ssize_t res;
21
22         while(len>0) {
23                 if((res=read(f, buf, len)) <=0) {
24                         if (!res)
25                                 exit(0);
26                         perror ("Error reading transactions");
27                         exit(1);
28                 }
29                 len-=res;
30                 buf+=res;
31         }
32 }
33
34 int main(int argc, char**argv) {
35         struct nbd_request req;
36         struct nbd_reply rep;
37         uint32_t magic;
38         uint64_t handle;
39         uint32_t error;
40         uint32_t command;
41         uint32_t len;
42         uint64_t offset;
43         char * ctext;
44         int readfd = 0; /* stdin */
45
46         while (1) {
47                 /* Read a request or reply from the transaction file */
48                 doread(readfd, &magic, sizeof(magic));
49                 magic = ntohl(magic);
50                 switch (magic) {
51                 case NBD_REQUEST_MAGIC:
52                         doread(readfd, sizeof(magic)+(char *)(&req), sizeof(struct nbd_request)-sizeof(magic));
53                         handle = ntohll(*((long long int *)(req.handle)));
54                         offset = ntohll(req.from);
55                         len = ntohl(req.len);
56                         command = ntohl(req.type);
57                         
58                         switch (command & NBD_CMD_MASK_COMMAND) {
59                         case NBD_CMD_READ:
60                                 ctext="NBD_CMD_READ";
61                                 break;
62                         case NBD_CMD_WRITE:
63                                 ctext="NBD_CMD_WRITE";
64                                 break;
65                         case NBD_CMD_DISC:
66                                 ctext="NBD_CMD_DISC";
67                                 break;
68                         case NBD_CMD_FLUSH:
69                                 ctext="NBD_CMD_FLUSH";
70                                 break;
71                         default:
72                                 ctext="UNKNOWN";
73                                 break;
74                         }
75                         printf("> H=%016llx C=0x%08x (%13s+%4s) O=%016llx L=%08x\n",
76                                (long long unsigned int) handle,
77                                command,
78                                ctext,
79                                (command & NBD_CMD_FLAG_FUA)?"FUA":"NONE",
80                                (long long unsigned int) offset,
81                                len);
82                         
83                         break;
84                 case NBD_REPLY_MAGIC:
85                         doread(readfd, sizeof(magic)+(char *)(&rep), sizeof(struct nbd_reply)-sizeof(magic));
86                         handle = ntohll(*((long long int *)(rep.handle)));
87                         error = ntohl(rep.error);
88                         
89                         printf("< H=%016llx E=0x%08x\n",
90                                (long long unsigned int) handle,
91                                error);
92                         break;
93                         
94                 default:
95                         printf("? Unknown transaction type %08x\n",magic);
96                         break;
97                 }
98                 
99         }
100         /* never reached */
101         return 0;
102 }