From 969496f15e1e0359e26c2c6e995ad4ef82720f86 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Fri, 16 Oct 2020 17:15:49 +0200 Subject: [BUILD] rewrite CMake build system to track changes of source files This change restructures the source code directories, separates shared form non-shared application code and adds CMake dependencies. These dependencies allow the tracking of changes and trigger a rebuild of those build targets where changed files are involved. WARNING: Note that the support of the DNBD3_SERVER_AFL build option is not supported yet. Thus, the option should be never turned on. --- src/kernel/CMakeLists.txt | 48 +++++++++++++++++++++++ src/kernel/Kbuild | 5 +++ src/kernel/blk.c | 2 +- src/kernel/blk.h | 2 +- src/kernel/core.c | 91 -------------------------------------------- src/kernel/dnbd3.h | 89 ------------------------------------------- src/kernel/dnbd3_main.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ src/kernel/dnbd3_main.h | 88 ++++++++++++++++++++++++++++++++++++++++++ src/kernel/net.c | 4 +- src/kernel/net.h | 2 +- src/kernel/serialize.c | 1 + src/kernel/serialize_kmod.c | 5 --- src/kernel/sysfs.h | 2 +- 13 files changed, 241 insertions(+), 191 deletions(-) create mode 100644 src/kernel/CMakeLists.txt create mode 100644 src/kernel/Kbuild delete mode 100644 src/kernel/core.c delete mode 100644 src/kernel/dnbd3.h create mode 100644 src/kernel/dnbd3_main.c create mode 100644 src/kernel/dnbd3_main.h create mode 120000 src/kernel/serialize.c delete mode 100644 src/kernel/serialize_kmod.c (limited to 'src/kernel') diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt new file mode 100644 index 0000000..bf1eb3a --- /dev/null +++ b/src/kernel/CMakeLists.txt @@ -0,0 +1,48 @@ +cmake_minimum_required(VERSION 3.10) + +# set the project name +project(dnbd3-kernel-module) + +# include macros to define Linux kernel build targets +include(Kernel) + +# set C flags for a Linux kernel module +set(KERNEL_C_FLAGS "-DDNBD3_KERNEL_MODULE -I ${PROJECT_INCLUDE_TMP_DIR}" + CACHE STRING "C flags to be used for building the dnbd3 kernel module") +# set C flags for the debug mode of a Linux kernel module +set(KERNEL_C_FLAGS_DEBUG "-g -DDEBUG" + CACHE STRING "Additional C flags to be used for building the dnbd3 kernel module in debug mode") + +# append include directories to the C flags +get_property(KERNEL_INCLUDE_DIRS DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES) +foreach(KERNEL_INCLUDE_DIR ${KERNEL_INCLUDE_DIRS}) + set(KERNEL_C_FLAGS "${KERNEL_C_FLAGS} -I ${KERNEL_INCLUDE_DIR}") +endforeach(KERNEL_INCLUDE_DIR ${KERNEL_INCLUDE_DIRS}) + +# append debug C flags if debug mode is enabled +if(CMAKE_BUILD_TYPE MATCHES Debug) + set(KERNEL_C_FLAGS "${KERNEL_C_FLAGS} ${KERNEL_C_FLAGS_DEBUG}") +endif(CMAKE_BUILD_TYPE MATCHES Debug) + +# dnbd3 Linux kernel module +set(KERNEL_MODULE_DNBD3_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/blk.c + ${CMAKE_CURRENT_SOURCE_DIR}/dnbd3_main.c + ${CMAKE_CURRENT_SOURCE_DIR}/net.c + ${CMAKE_CURRENT_SOURCE_DIR}/serialize.c + ${CMAKE_CURRENT_SOURCE_DIR}/sysfs.c + ${CMAKE_CURRENT_SOURCE_DIR}/utils.c + ${PROJECT_VERSION_FILE}) +set(KERNEL_MODULE_DNBD3_HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/blk.h + ${CMAKE_CURRENT_SOURCE_DIR}/dnbd3_main.h + ${CMAKE_CURRENT_SOURCE_DIR}/net.h + ${CMAKE_CURRENT_SOURCE_DIR}/sysfs.h + ${CMAKE_CURRENT_SOURCE_DIR}/utils.h) +add_kernel_module(dnbd3 "${KERNEL_BUILD_DIR}" + "${KERNEL_INSTALL_DIR}" + "CONFIG_BLK_DEV_DNBD3=m" + "${KERNEL_MODULE_DNBD3_SOURCE_FILES}" + "${KERNEL_MODULE_DNBD3_HEADER_FILES}" + ${CMAKE_CURRENT_SOURCE_DIR}/Kbuild) + +# add dependency to generate ${PROJECT_VERSION_FILE} before dnbd3.ko is built +add_dependencies(dnbd3 dnbd3-generate-version) diff --git a/src/kernel/Kbuild b/src/kernel/Kbuild new file mode 100644 index 0000000..385a5ff --- /dev/null +++ b/src/kernel/Kbuild @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 + +# Linux kernel module dnbd3 +obj-$(CONFIG_BLK_DEV_DNBD3) := dnbd3.o +dnbd3-y += dnbd3_main.o blk.o net.o serialize.o sysfs.o utils.o diff --git a/src/kernel/blk.c b/src/kernel/blk.c index 2c46b67..00c3f8f 100644 --- a/src/kernel/blk.c +++ b/src/kernel/blk.c @@ -18,7 +18,7 @@ * */ -#include "clientconfig.h" +#include #include "blk.h" #include "net.h" #include "sysfs.h" diff --git a/src/kernel/blk.h b/src/kernel/blk.h index 3377406..5f7f2db 100644 --- a/src/kernel/blk.h +++ b/src/kernel/blk.h @@ -21,7 +21,7 @@ #ifndef BLK_H_ #define BLK_H_ -#include "dnbd3.h" +#include "dnbd3_main.h" #define REQ_TYPE_SPECIAL REQ_TYPE_DRV_PRIV diff --git a/src/kernel/core.c b/src/kernel/core.c deleted file mode 100644 index 6e7d052..0000000 --- a/src/kernel/core.c +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the Distributed Network Block Device 3 - * - * Copyright(c) 2011-2012 Johann Latocha - * - * This file may be licensed under the terms of of the - * GNU General Public License Version 2 (the ``GPL''). - * - * Software distributed under the License is distributed - * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either - * express or implied. See the GPL for the specific language - * governing rights and limitations. - * - * You should have received a copy of the GPL along with this - * program. If not, go to http://www.gnu.org/licenses/gpl.html - * or write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt - -#include "clientconfig.h" -#include "dnbd3.h" -#include "blk.h" - -int major; -static unsigned int max_devs = NUMBER_DEVICES; -static dnbd3_device_t *dnbd3_devices; - -struct device *dnbd3_device_to_dev(dnbd3_device_t *dev) -{ - return disk_to_dev(dev->disk); -} - -static int __init dnbd3_init(void) -{ - int i; - - dnbd3_devices = kcalloc(max_devs, sizeof(*dnbd3_devices), GFP_KERNEL); - if (!dnbd3_devices) - return -ENOMEM; - - // initialize block device - if ((major = register_blkdev(0, "dnbd3")) == 0) - { - pr_err("register_blkdev failed\n"); - return -EIO; - } - - pr_info("kernel module loaded\n"); - pr_debug("machine type " ENDIAN_MODE "\n"); - - // add MAX_NUMBER_DEVICES devices - for (i = 0; i < max_devs; i++) - { - if (dnbd3_blk_add_device(&dnbd3_devices[i], i) != 0) - { - pr_err("dnbd3_blk_add_device failed\n"); - return -EIO; // TODO: delete all devices added so far. it could happen that it's not the first one that fails. also call unregister_blkdev and free memory - } - } - - pr_info("init successful (%i devices)\n", max_devs); - - return 0; -} - -static void __exit dnbd3_exit(void) -{ - int i; - - for (i = 0; i < max_devs; i++) - { - dnbd3_blk_del_device(&dnbd3_devices[i]); - } - - unregister_blkdev(major, "dnbd3"); - kfree(dnbd3_devices); - - pr_info("exit kernel module\n"); -} - -module_init(dnbd3_init); -module_exit(dnbd3_exit); - -MODULE_DESCRIPTION("Distributed Network Block Device 3"); -MODULE_LICENSE("GPL"); - -module_param(max_devs, int, 0444); -MODULE_PARM_DESC(max_devs, "number of network block devices to initialize (default: 8)"); diff --git a/src/kernel/dnbd3.h b/src/kernel/dnbd3.h deleted file mode 100644 index 4e06d70..0000000 --- a/src/kernel/dnbd3.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * This file is part of the Distributed Network Block Device 3 - * - * Copyright(c) 2011-2012 Johann Latocha - * - * This file may be licensed under the terms of of the - * GNU General Public License Version 2 (the ``GPL''). - * - * Software distributed under the License is distributed - * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either - * express or implied. See the GPL for the specific language - * governing rights and limitations. - * - * You should have received a copy of the GPL along with this - * program. If not, go to http://www.gnu.org/licenses/gpl.html - * or write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -#ifndef DNBD_H_ -#define DNBD_H_ - -#include -#include -#include -#include -#include -#include - -#define KERNEL_MODULE -#include "config.h" -#include "types.h" -#include "serialize.h" - -extern int major; - -typedef struct -{ - dnbd3_host_t host; - unsigned long rtts[4]; // Last four round trip time measurements in µs - uint16_t protocol_version; // dnbd3 protocol version of this server - uint8_t failures; // How many times the server was unreachable -} dnbd3_server_t; - -typedef struct -{ - // block - struct gendisk *disk; - struct blk_mq_tag_set tag_set; - struct request_queue *queue; - spinlock_t blk_lock; - - // sysfs - struct kobject kobj; - - // network - char *imgname; - struct socket *sock; - dnbd3_server_t cur_server, initial_server; - unsigned long cur_rtt; - serialized_buffer_t payload_buffer; - dnbd3_server_t alt_servers[NUMBER_SERVERS]; // array of alt servers - int new_servers_num; // number of new alt servers that are waiting to be copied to above array - dnbd3_server_entry_t new_servers[NUMBER_SERVERS]; // pending new alt servers - uint8_t discover, panic, disconnecting, update_available, panic_count; - uint8_t use_server_provided_alts; - uint16_t rid; - uint32_t heartbeat_count; - uint64_t reported_size; - // server switch - struct socket *better_sock; - - // process - struct task_struct * thread_send; - struct task_struct * thread_receive; - struct task_struct *thread_discover; - struct timer_list hb_timer; - wait_queue_head_t process_queue_send; - wait_queue_head_t process_queue_receive; - wait_queue_head_t process_queue_discover; - struct list_head request_queue_send; - struct list_head request_queue_receive; - -} dnbd3_device_t; - -extern inline struct device *dnbd3_device_to_dev(dnbd3_device_t *dev); - -#endif /* DNBD_H_ */ diff --git a/src/kernel/dnbd3_main.c b/src/kernel/dnbd3_main.c new file mode 100644 index 0000000..27e8eed --- /dev/null +++ b/src/kernel/dnbd3_main.c @@ -0,0 +1,93 @@ +/* + * This file is part of the Distributed Network Block Device 3 + * + * Copyright(c) 2011-2012 Johann Latocha + * + * This file may be licensed under the terms of of the + * GNU General Public License Version 2 (the ``GPL''). + * + * Software distributed under the License is distributed + * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either + * express or implied. See the GPL for the specific language + * governing rights and limitations. + * + * You should have received a copy of the GPL along with this + * program. If not, go to http://www.gnu.org/licenses/gpl.html + * or write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include "dnbd3_main.h" +#include "blk.h" + +int major; +static unsigned int max_devs = NUMBER_DEVICES; +static dnbd3_device_t *dnbd3_devices; + +struct device *dnbd3_device_to_dev(dnbd3_device_t *dev) +{ + return disk_to_dev(dev->disk); +} + +static int __init dnbd3_init(void) +{ + int i; + + dnbd3_devices = kcalloc(max_devs, sizeof(*dnbd3_devices), GFP_KERNEL); + if (!dnbd3_devices) + return -ENOMEM; + + // initialize block device + if ((major = register_blkdev(0, "dnbd3")) == 0) + { + pr_err("register_blkdev failed\n"); + return -EIO; + } + + pr_info("kernel module in version %s loaded\n", DNBD3_VERSION); + pr_debug("machine type " ENDIAN_MODE "\n"); + + // add MAX_NUMBER_DEVICES devices + for (i = 0; i < max_devs; i++) + { + if (dnbd3_blk_add_device(&dnbd3_devices[i], i) != 0) + { + pr_err("dnbd3_blk_add_device failed\n"); + return -EIO; // TODO: delete all devices added so far. it could happen that it's not the first one that fails. also call unregister_blkdev and free memory + } + } + + pr_info("init successful (%i devices)\n", max_devs); + + return 0; +} + +static void __exit dnbd3_exit(void) +{ + int i; + + for (i = 0; i < max_devs; i++) + { + dnbd3_blk_del_device(&dnbd3_devices[i]); + } + + unregister_blkdev(major, "dnbd3"); + kfree(dnbd3_devices); + + pr_info("exit kernel module\n"); +} + +module_init(dnbd3_init); +module_exit(dnbd3_exit); + +MODULE_DESCRIPTION("Distributed Network Block Device 3"); +MODULE_LICENSE("GPL"); +MODULE_VERSION(DNBD3_VERSION); + +module_param(max_devs, int, 0444); +MODULE_PARM_DESC(max_devs, "number of network block devices to initialize (default: 8)"); diff --git a/src/kernel/dnbd3_main.h b/src/kernel/dnbd3_main.h new file mode 100644 index 0000000..124426b --- /dev/null +++ b/src/kernel/dnbd3_main.h @@ -0,0 +1,88 @@ +/* + * This file is part of the Distributed Network Block Device 3 + * + * Copyright(c) 2011-2012 Johann Latocha + * + * This file may be licensed under the terms of of the + * GNU General Public License Version 2 (the ``GPL''). + * + * Software distributed under the License is distributed + * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either + * express or implied. See the GPL for the specific language + * governing rights and limitations. + * + * You should have received a copy of the GPL along with this + * program. If not, go to http://www.gnu.org/licenses/gpl.html + * or write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef DNBD_H_ +#define DNBD_H_ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +extern int major; + +typedef struct +{ + dnbd3_host_t host; + unsigned long rtts[4]; // Last four round trip time measurements in µs + uint16_t protocol_version; // dnbd3 protocol version of this server + uint8_t failures; // How many times the server was unreachable +} dnbd3_server_t; + +typedef struct +{ + // block + struct gendisk *disk; + struct blk_mq_tag_set tag_set; + struct request_queue *queue; + spinlock_t blk_lock; + + // sysfs + struct kobject kobj; + + // network + char *imgname; + struct socket *sock; + dnbd3_server_t cur_server, initial_server; + unsigned long cur_rtt; + serialized_buffer_t payload_buffer; + dnbd3_server_t alt_servers[NUMBER_SERVERS]; // array of alt servers + int new_servers_num; // number of new alt servers that are waiting to be copied to above array + dnbd3_server_entry_t new_servers[NUMBER_SERVERS]; // pending new alt servers + uint8_t discover, panic, disconnecting, update_available, panic_count; + uint8_t use_server_provided_alts; + uint16_t rid; + uint32_t heartbeat_count; + uint64_t reported_size; + // server switch + struct socket *better_sock; + + // process + struct task_struct * thread_send; + struct task_struct * thread_receive; + struct task_struct *thread_discover; + struct timer_list hb_timer; + wait_queue_head_t process_queue_send; + wait_queue_head_t process_queue_receive; + wait_queue_head_t process_queue_discover; + struct list_head request_queue_send; + struct list_head request_queue_receive; + +} dnbd3_device_t; + +extern inline struct device *dnbd3_device_to_dev(dnbd3_device_t *dev); + +#endif /* DNBD_H_ */ diff --git a/src/kernel/net.c b/src/kernel/net.c index b387c1d..46c369a 100644 --- a/src/kernel/net.c +++ b/src/kernel/net.c @@ -18,12 +18,12 @@ * */ -#include "clientconfig.h" +#include #include "net.h" #include "blk.h" #include "utils.h" -#include "serialize.h" +#include #include #include diff --git a/src/kernel/net.h b/src/kernel/net.h index 8a2bc22..e7accd0 100644 --- a/src/kernel/net.h +++ b/src/kernel/net.h @@ -21,7 +21,7 @@ #ifndef NET_H_ #define NET_H_ -#include "dnbd3.h" +#include "dnbd3_main.h" #define init_msghdr(h) do { \ h.msg_name = NULL; \ diff --git a/src/kernel/serialize.c b/src/kernel/serialize.c new file mode 120000 index 0000000..5a4e4ac --- /dev/null +++ b/src/kernel/serialize.c @@ -0,0 +1 @@ +../shared/serialize.c \ No newline at end of file diff --git a/src/kernel/serialize_kmod.c b/src/kernel/serialize_kmod.c deleted file mode 100644 index 50746df..0000000 --- a/src/kernel/serialize_kmod.c +++ /dev/null @@ -1,5 +0,0 @@ -#include -#include - -#define KERNEL_MODULE -#include "serialize.c" diff --git a/src/kernel/sysfs.h b/src/kernel/sysfs.h index 0a747a5..a90840b 100644 --- a/src/kernel/sysfs.h +++ b/src/kernel/sysfs.h @@ -21,7 +21,7 @@ #ifndef SYSFS_H_ #define SYSFS_H_ -#include "dnbd3.h" +#include "dnbd3_main.h" void dnbd3_sysfs_init(dnbd3_device_t *dev); -- cgit v1.2.3-55-g7522