Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
protocol: Generate map functions from NBD protocol flags to printable…
… strings.

This generates small functions which map from various integer NBD
protocol flags to the string equivalent.

eg:
    name_of_nbd_cmd (NBD_CMD_READ)
      ---> "NBD_CMD_READ"

This commit uses some hairy sed scripting to ensure that we don't add
any more dependencies to nbdkit.
  • Loading branch information
rwmjones committed Dec 6, 2018
1 parent 521f17d commit d198cf9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -47,6 +47,7 @@ Makefile.in
/podwrapper.pl
/src/nbdkit
/src/nbdkit.pc
/src/protostrings.c
/src/synopsis.c
/src/test-utils
/stamp-h1
Expand Down
17 changes: 15 additions & 2 deletions src/Makefile.am
Expand Up @@ -49,6 +49,7 @@ nbdkit_SOURCES = \
options.h \
plugins.c \
protocol.h \
protostrings.c \
sockets.c \
threadlocal.c \
utils.c \
Expand Down Expand Up @@ -77,11 +78,23 @@ nbdkit_LDFLAGS = \
$(PTHREAD_LIBS) \
$(DL_LDFLAGS)

# protostrings.c is generated from the protocol.h header file where it
# is used to map NBD protocol flags to strings.

BUILT_SOURCES = protostrings.c
EXTRA_DIST = protostrings.c
CLEANFILES += protostrings.c
protostrings.c: protocol.h protostrings.sed Makefile
rm -f $@ $@-t
$(SED) -n -f protostrings.sed < $< > $@-t
mv $@-t $@
chmod 0444 $@

# synopsis.c is generated from docs/synopsis.txt where it is also
# used to generate the man page. It is included in main.c.

BUILT_SOURCES = synopsis.c
EXTRA_DIST = synopsis.c
BUILT_SOURCES += synopsis.c
EXTRA_DIST += synopsis.c
nbdkit_DEPENDENCIES = synopsis.c
CLEANFILES += synopsis.c
main.c: synopsis.c
Expand Down
9 changes: 9 additions & 0 deletions src/protocol.h
Expand Up @@ -81,10 +81,12 @@ struct fixed_new_option_reply {
#define NBD_REP_MAGIC UINT64_C(0x3e889045565a9)

/* Global flags. */
extern const char *name_of_nbd_global_flag (int);
#define NBD_FLAG_FIXED_NEWSTYLE 1
#define NBD_FLAG_NO_ZEROES 2

/* Per-export flags. */
extern const char *name_of_nbd_flag (int);
#define NBD_FLAG_HAS_FLAGS (1 << 0)
#define NBD_FLAG_READ_ONLY (1 << 1)
#define NBD_FLAG_SEND_FLUSH (1 << 2)
Expand All @@ -94,13 +96,15 @@ struct fixed_new_option_reply {
#define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6)

/* NBD options (new style handshake only). */
extern const char *name_of_nbd_opt (int);
#define NBD_OPT_EXPORT_NAME 1
#define NBD_OPT_ABORT 2
#define NBD_OPT_LIST 3
#define NBD_OPT_STARTTLS 5
#define NBD_OPT_INFO 6
#define NBD_OPT_GO 7

extern const char *name_of_nbd_rep (int);
#define NBD_REP_ACK 1
#define NBD_REP_SERVER 2
#define NBD_REP_INFO 3
Expand All @@ -110,6 +114,7 @@ struct fixed_new_option_reply {
#define NBD_REP_ERR_PLATFORM 0x80000004
#define NBD_REP_ERR_TLS_REQD 0x80000005

extern const char *name_of_nbd_info (int);
#define NBD_INFO_EXPORT 0

/* NBD_INFO_EXPORT reply (follows fixed_new_option_reply). */
Expand Down Expand Up @@ -148,19 +153,23 @@ struct reply {
#define NBD_REQUEST_MAGIC 0x25609513
#define NBD_REPLY_MAGIC 0x67446698

/* NBD commands. */
extern const char *name_of_nbd_cmd (int);
#define NBD_CMD_READ 0
#define NBD_CMD_WRITE 1
#define NBD_CMD_DISC 2 /* Disconnect. */
#define NBD_CMD_FLUSH 3
#define NBD_CMD_TRIM 4
#define NBD_CMD_WRITE_ZEROES 6

extern const char *name_of_nbd_cmd_flag (int);
#define NBD_CMD_FLAG_FUA (1<<0)
#define NBD_CMD_FLAG_NO_HOLE (1<<1)

/* Error codes (previously errno).
* See http://git.qemu.org/?p=qemu.git;a=commitdiff;h=ca4414804114fd0095b317785bc0b51862e62ebb
*/
extern const char *name_of_nbd_error (int);
#define NBD_SUCCESS 0
#define NBD_EPERM 1
#define NBD_EIO 5
Expand Down
59 changes: 59 additions & 0 deletions src/protostrings.sed
@@ -0,0 +1,59 @@
# nbdkit
# Copyright (C) 2018 Red Hat Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of Red Hat nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# Generate the protostrings.c file from protocol.h.

# Prologue.
1i\
/* Generated from protocol.h by protostrings.sed. */\
\#include "protocol.h"\

# Match the precise sections of the source file.
/^extern const char \*name_of_/,/^$/ {

# Convert extern function prototype into a definition.
s/extern \(const char \*name_of_.*\) (int);/\1 (int fl) {\
switch (fl) {/;

# Convert #define lines into cases.
s/^#define \([_A-Z]*\).*/ case \1: return "\1\";/;

# Append closing brace.
s/^$/ default: return "unknown";\
}\
}/;

# Print pattern buffer.
p;

}

0 comments on commit d198cf9

Please sign in to comment.