Refactor the negotiate() and connected functions
[nbd.git] / nbd-helper.h
blob984ab1fba810b6d3c8d69f87bf478f14b326934a
1 #ifndef NBD_HELPER_H
2 #define NBD_HELPER_H
4 #include "nbd.h"
6 /* Constants and macros */
8 /*
9 * Constants for nbd_request.magic == NBD_TRACELOG_MAGIC
11 /* 1) stored in nbd_req.type */
12 enum {
13 /* enable/disable logging actual data.
14 * nbd_request.len is the new value (true/false)
16 NBD_TRACELOG_SET_DATALOG = 1
19 /* 2) Must be in nbd_req.from */
20 #define NBD_TRACELOG_FROM_MAGIC 0x4A93BA39A54F31B6ULL
22 /* Functions */
24 /**
25 * Translate a command name into human readable form
27 * @param command The command number (after applying NBD_CMD_MASK_COMMAND)
28 * @return pointer to the command name
29 **/
30 #define ENUM2STR(x) case x: return #x
31 static inline const char * getcommandname(uint32_t command) {
32 switch (command) {
33 ENUM2STR(NBD_CMD_READ);
34 ENUM2STR(NBD_CMD_WRITE);
35 ENUM2STR(NBD_CMD_DISC);
36 ENUM2STR(NBD_CMD_FLUSH);
37 ENUM2STR(NBD_CMD_TRIM);
38 ENUM2STR(NBD_CMD_CACHE);
39 ENUM2STR(NBD_CMD_WRITE_ZEROES);
40 ENUM2STR(NBD_CMD_BLOCK_STATUS);
41 ENUM2STR(NBD_CMD_RESIZE);
42 default:
43 return "UNKNOWN";
47 /**
48 * Translate a tracelog parameter name into human readable form
50 * @type tracelog parameter number from struct nbd_req.type
51 * @return pointer to the name
52 **/
53 static inline const char * gettracelogname(uint32_t type) {
54 switch (type) {
55 ENUM2STR(NBD_TRACELOG_SET_DATALOG);
56 default:
57 return "UNKNOWN";
61 static inline const char *getstructreplname(uint16_t type) {
62 switch(type) {
63 ENUM2STR(NBD_REPLY_TYPE_NONE);
64 ENUM2STR(NBD_REPLY_TYPE_OFFSET_DATA);
65 ENUM2STR(NBD_REPLY_TYPE_OFFSET_HOLE);
66 ENUM2STR(NBD_REPLY_TYPE_BLOCK_STATUS);
68 ENUM2STR(NBD_REPLY_TYPE_ERROR);
69 ENUM2STR(NBD_REPLY_TYPE_ERROR_OFFSET);
70 default:
71 return "UNKNOWN";
75 #undef ENUM2STR
77 #endif //NBD_HELPER_H