]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Ceph - scalable distributed file system | |
3 | * | |
4 | * Copyright (C) 2020 SUSE LINUX GmbH | |
5 | * | |
6 | * This is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License version 2.1, as published by the Free Software | |
9 | * Foundation. See file COPYING. | |
10 | * | |
11 | */ | |
12 | ||
13 | #ifndef RBD_WNBD_H | |
14 | #define RBD_WNBD_H | |
15 | ||
16 | #include <string.h> | |
17 | #include <iostream> | |
18 | #include <vector> | |
19 | ||
20 | #include "include/compat.h" | |
21 | #include "common/win32/registry.h" | |
22 | ||
23 | #include "wnbd_handler.h" | |
24 | ||
25 | #define SERVICE_REG_KEY "SYSTEM\\CurrentControlSet\\Services\\rbd-wnbd" | |
26 | #define SERVICE_PIPE_NAME "\\\\.\\pipe\\rbd-wnbd" | |
27 | #define SERVICE_PIPE_TIMEOUT_MS 5000 | |
28 | #define SERVICE_PIPE_BUFFSZ 4096 | |
29 | ||
30 | #define DEFAULT_MAP_TIMEOUT_MS 30000 | |
31 | ||
32 | #define RBD_WNBD_BLKSIZE 512UL | |
33 | ||
34 | #define DEFAULT_SERVICE_START_TIMEOUT 120 | |
35 | #define DEFAULT_IMAGE_MAP_TIMEOUT 20 | |
36 | ||
37 | #define HELP_INFO 1 | |
38 | #define VERSION_INFO 2 | |
39 | ||
40 | #define WNBD_STATUS_ACTIVE "active" | |
41 | #define WNBD_STATUS_INACTIVE "inactive" | |
42 | ||
43 | #define DEFAULT_SERVICE_THREAD_COUNT 8 | |
44 | ||
45 | static WnbdHandler* handler = nullptr; | |
46 | ceph::mutex shutdown_lock = ceph::make_mutex("RbdWnbd::ShutdownLock"); | |
47 | ||
48 | struct Config { | |
49 | bool exclusive = false; | |
50 | bool readonly = false; | |
51 | ||
52 | std::string parent_pipe; | |
53 | ||
54 | std::string poolname; | |
55 | std::string nsname; | |
56 | std::string imgname; | |
57 | std::string snapname; | |
58 | std::string devpath; | |
59 | ||
60 | std::string format; | |
61 | bool pretty_format = false; | |
62 | ||
63 | bool hard_disconnect = false; | |
64 | int soft_disconnect_timeout = DEFAULT_SOFT_REMOVE_TIMEOUT; | |
65 | bool hard_disconnect_fallback = true; | |
66 | ||
67 | int service_start_timeout = DEFAULT_SERVICE_START_TIMEOUT; | |
68 | int image_map_timeout = DEFAULT_IMAGE_MAP_TIMEOUT; | |
69 | bool remap_failure_fatal = false; | |
70 | ||
71 | // TODO: consider moving those fields to a separate structure. Those | |
72 | // provide connection information without actually being configurable. | |
73 | // The disk number is provided by Windows. | |
74 | int disk_number = -1; | |
75 | int pid = 0; | |
76 | std::string serial_number; | |
77 | bool active = false; | |
78 | bool wnbd_mapped = false; | |
79 | std::string command_line; | |
80 | std::string admin_sock_path; | |
81 | ||
82 | WnbdLogLevel wnbd_log_level = WnbdLogLevelInfo; | |
83 | int io_req_workers = DEFAULT_IO_WORKER_COUNT; | |
84 | int io_reply_workers = DEFAULT_IO_WORKER_COUNT; | |
85 | int service_thread_count = DEFAULT_SERVICE_THREAD_COUNT; | |
86 | ||
87 | // register the mapping, recreating it when the Ceph service starts. | |
88 | bool persistent = true; | |
89 | }; | |
90 | ||
91 | enum Command { | |
92 | None, | |
93 | Connect, | |
94 | Disconnect, | |
95 | List, | |
96 | Show, | |
97 | Service, | |
98 | Stats | |
99 | }; | |
100 | ||
101 | typedef struct { | |
102 | Command command; | |
103 | BYTE arguments[1]; | |
104 | } ServiceRequest; | |
105 | ||
106 | typedef struct { | |
107 | int status; | |
108 | } ServiceReply; | |
109 | ||
110 | bool is_process_running(DWORD pid); | |
111 | void unmap_at_exit(); | |
112 | ||
113 | int disconnect_all_mappings( | |
114 | bool unregister, | |
115 | bool hard_disconnect, | |
116 | int soft_disconnect_timeout, | |
117 | int worker_count); | |
118 | int restart_registered_mappings( | |
119 | int worker_count, int total_timeout, int image_map_timeout); | |
120 | int map_device_using_suprocess(std::string command_line); | |
121 | ||
122 | int construct_devpath_if_missing(Config* cfg); | |
123 | int save_config_to_registry(Config* cfg); | |
124 | int remove_config_from_registry(Config* cfg); | |
125 | int load_mapping_config_from_registry(std::string devpath, Config* cfg); | |
126 | ||
127 | BOOL WINAPI console_handler_routine(DWORD dwCtrlType); | |
128 | ||
129 | static int parse_args(std::vector<const char*>& args, | |
130 | std::ostream *err_msg, | |
131 | Command *command, Config *cfg); | |
132 | static int do_unmap(Config *cfg, bool unregister); | |
133 | ||
134 | ||
135 | class BaseIterator { | |
136 | public: | |
137 | virtual ~BaseIterator() {}; | |
138 | virtual bool get(Config *cfg) = 0; | |
139 | ||
140 | int get_error() { | |
141 | return error; | |
142 | } | |
143 | protected: | |
144 | int error = 0; | |
145 | int index = -1; | |
146 | }; | |
147 | ||
148 | // Iterate over mapped devices, retrieving info from the driver. | |
149 | class WNBDActiveDiskIterator : public BaseIterator { | |
150 | public: | |
151 | WNBDActiveDiskIterator(); | |
152 | ~WNBDActiveDiskIterator(); | |
153 | ||
154 | bool get(Config *cfg); | |
155 | ||
156 | private: | |
157 | PWNBD_CONNECTION_LIST conn_list = NULL; | |
158 | ||
159 | static DWORD fetch_list(PWNBD_CONNECTION_LIST* conn_list); | |
160 | }; | |
161 | ||
162 | ||
163 | // Iterate over the Windows registry key, retrieving registered mappings. | |
164 | class RegistryDiskIterator : public BaseIterator { | |
165 | public: | |
166 | RegistryDiskIterator(); | |
167 | ~RegistryDiskIterator() { | |
168 | delete reg_key; | |
169 | } | |
170 | ||
171 | bool get(Config *cfg); | |
172 | private: | |
173 | DWORD subkey_count = 0; | |
174 | char subkey_name[MAX_PATH]; | |
175 | ||
176 | RegistryKey* reg_key = NULL; | |
177 | }; | |
178 | ||
179 | // Iterate over all RBD mappings, getting info from the registry and driver. | |
180 | class WNBDDiskIterator : public BaseIterator { | |
181 | public: | |
182 | bool get(Config *cfg); | |
183 | ||
184 | private: | |
185 | // We'll keep track of the active devices. | |
186 | std::set<std::string> active_devices; | |
187 | ||
188 | WNBDActiveDiskIterator active_iterator; | |
189 | RegistryDiskIterator registry_iterator; | |
190 | }; | |
191 | ||
192 | #endif // RBD_WNBD_H |