From b7a3275af3bca900b66d45d7100a86ee75c6474d Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Sat, 24 Feb 2024 02:06:45 +0300 Subject: [PATCH] Make netlink optional --- src/CMakeLists.txt | 10 +++---- src/nbd_proxy.cpp | 66 +++++++++++++++------------------------------- 2 files changed, 26 insertions(+), 50 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8aff0675..58eafedd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -183,15 +183,15 @@ if (${WITH_FIO}) endif (${WITH_FIO}) # vitastor-nbd -pkg_check_modules(NL3 REQUIRED libnl-3.0 libnl-genl-3.0) -add_executable(vitastor-nbd - nbd_proxy.cpp +pkg_check_modules(NL3 libnl-3.0 libnl-genl-3.0) +add_executable(vitastor-nbd + nbd_proxy.cpp ) target_include_directories(vitastor-nbd PUBLIC ${NL3_INCLUDE_DIRS}) target_link_libraries(vitastor-nbd vitastor_client ${NL3_LIBRARIES}) -if(HAVE_NBD_NETLINK_H) +if (HAVE_NBD_NETLINK_H AND NL3_LIBRARIES) target_compile_definitions(vitastor-nbd PUBLIC HAVE_NBD_NETLINK_H) -endif() +endif (HAVE_NBD_NETLINK_H AND NL3_LIBRARIES) # libvitastor_kv.so add_library(vitastor_kv SHARED diff --git a/src/nbd_proxy.cpp b/src/nbd_proxy.cpp index b71eac27..1f1422d7 100644 --- a/src/nbd_proxy.cpp +++ b/src/nbd_proxy.cpp @@ -23,6 +23,7 @@ #include "cluster_client.h" #include "epoll_manager.h" +#ifdef HAVE_NBD_NETLINK_H #include #include #include @@ -31,9 +32,6 @@ #include #include #include - -#ifdef HAVE_NBD_NETLINK_H - #include #define fail(...) { fprintf(stderr, __VA_ARGS__); exit(1); } @@ -226,22 +224,6 @@ nla_put_failure: #undef fail -#else - -static int netlink_configure(const int *sockfd, int sock_size, int dev_num, uint64_t size, - uint64_t blocksize, uint64_t flags, uint64_t cflags, uint64_t timeout, uint64_t conn_timeout) -{ - fprintf(stderr, "netlink is not supported\n"); - exit(1); - return 0; -} - -static void netlink_disconnect(uint32_t dev_num) -{ - fprintf(stderr, "netlink is not supported\n"); - exit(1); -} - #endif #ifndef MSG_ZEROCOPY @@ -339,14 +321,18 @@ public: fprintf(stderr, "device name or number is missing\n"); exit(1); } - if (cfg["netlink"].is_null()) { unmap(cfg["dev_num"].uint64_value()); } else { +#ifdef HAVE_NBD_NETLINK_H netlink_disconnect(cfg["dev_num"].uint64_value()); +#else + fprintf(stderr, "netlink support is disabled in this build\n"); + exit(1); +#endif } } else if (cfg["command"] == "ls" || cfg["command"] == "list" || cfg["command"] == "list-mapped") @@ -364,7 +350,7 @@ public: { printf( "Vitastor NBD proxy\n" - "(c) Vitaliy Filippov, 2020-2021 (VNPL-1.1)\n\n" + "(c) Vitaliy Filippov, 2020+ (VNPL-1.1)\n\n" "USAGE:\n" " %s map [OPTIONS] (--image | --pool --inode --size )\n" " %s unmap /dev/nbd0\n" @@ -377,7 +363,7 @@ public: " won't be able to stop the device at all if vitastor-nbd process dies.\n" " --nbd_max_devices 64 --nbd_max_part 3\n" " Options for the \"nbd\" kernel module when modprobing it (nbds_max and max_part).\n" - " note that maximum allowed (nbds_max)*(1+max_part) is 256.\n" + " Maximum allowed (nbds_max)*(1+max_part) is 2^20.\n" " Note that nbd_timeout, nbd_max_devices and nbd_max_part options may also be specified\n" " in /etc/vitastor/vitastor.conf or in other configuration file specified with --config_file.\n" " --nbd_lease 60\n" @@ -518,48 +504,38 @@ public: if (!cfg["netlink"].is_null()) { +#ifdef HAVE_NBD_NETLINK_H int devnum = -1; if (!cfg["dev_num"].is_null()) { devnum = (int)cfg["dev_num"].uint64_value(); } - uint64_t flags = NBD_FLAG_SEND_FLUSH; uint64_t cflags = 0; - - #ifdef NBD_FLAG_READ_ONLY +#ifdef NBD_FLAG_READ_ONLY if (!cfg["nbd_ro"].is_null()) - { flags |= NBD_FLAG_READ_ONLY; - } - #endif - - #ifdef NBD_CFLAG_DESTROY_ON_DISCONNECT +#endif +#ifdef NBD_CFLAG_DESTROY_ON_DISCONNECT if (!cfg["nbd_destroy_on_disconnect"].is_null()) - { cflags |= NBD_CFLAG_DESTROY_ON_DISCONNECT; - } - #endif - - #ifdef NBD_CFLAG_DISCONNECT_ON_CLOSE +#endif +#ifdef NBD_CFLAG_DISCONNECT_ON_CLOSE if (!cfg["nbd_disconnect_on_close"].is_null()) - { cflags |= NBD_CFLAG_DISCONNECT_ON_CLOSE; - } - #endif - +#endif int err = netlink_configure(sockfd + 1, 1, devnum, device_size, 4096, flags, cflags, nbd_timeout, nbd_lease); if (err < 0) { - if (err == -NLE_BUSY) - errno = EBUSY; - else - errno = EIO; - perror("netlink_configure"); + errno = (err == -NLE_BUSY ? EBUSY : EIO); + fprintf(stderr, "netlink_configure failed: %s (code %d)\n", nl_geterror(err), err); exit(1); } - printf("/dev/nbd%d\n", err); +#else + fprintf(stderr, "netlink support is disabled in this build\n"); + exit(1); +#endif } else {