Update Red Hat Copyright Notices
[nbdkit.git] / plugins / vddk / vddk.h
blobfb0c79a83874f4b97138fbac6296e4acc8a0f76a
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #ifndef NBDKIT_VDDK_H
34 #define NBDKIT_VDDK_H
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <sys/time.h>
40 #include <pthread.h>
42 #include "isaligned.h"
43 #include "tvdiff.h"
44 #include "vector.h"
46 #include "vddk-structs.h"
48 enum compression_type { NONE = 0, ZLIB, FASTLZ, SKIPZ };
50 extern void *dl;
51 extern bool init_called;
52 extern __thread int error_suppression;
53 extern int library_version;
54 extern bool is_remote;
56 extern enum compression_type compression;
57 extern char *config;
58 extern const char *cookie;
59 extern bool create;
60 extern enum VixDiskLibAdapterType create_adapter_type;
61 extern uint16_t create_hwversion;
62 extern uint64_t create_size;
63 extern enum VixDiskLibDiskType create_type;
64 extern const char *filename;
65 extern char *libdir;
66 extern uint16_t nfc_host_port;
67 extern char *password;
68 extern uint16_t port;
69 extern const char *server_name;
70 extern bool single_link;
71 extern const char *snapshot_moref;
72 extern const char *thumb_print;
73 extern const char *transport_modes;
74 extern bool unbuffered;
75 extern const char *username;
76 extern const char *vmx_spec;
78 extern int vddk_debug_diskinfo;
79 extern int vddk_debug_extents;
80 extern int vddk_debug_datapath;
81 extern int vddk_debug_stats;
83 #define STUB(fn, ret, args) extern ret (*fn) args
84 #define OPTIONAL_STUB(fn, ret, args) STUB (fn, ret, args)
85 #include "vddk-stubs.h"
86 #undef STUB
87 #undef OPTIONAL_STUB
89 /* Macros to bracket each VDDK API call, for printing debugging
90 * information and collecting statistics.
92 #define VDDK_CALL_START(fn, fs, ...) \
93 do { \
94 struct timeval start_t, end_t; \
95 /* GCC can optimize this away at compile time: */ \
96 const bool datapath = \
97 strcmp (#fn, "VixDiskLib_Read") == 0 || \
98 strcmp (#fn, "VixDiskLib_ReadAsync") == 0 || \
99 strcmp (#fn, "VixDiskLib_Write") == 0 || \
100 strcmp (#fn, "VixDiskLib_WriteAsync") == 0; \
101 if (vddk_debug_stats) \
102 gettimeofday (&start_t, NULL); \
103 if (!datapath || vddk_debug_datapath) \
104 nbdkit_debug ("VDDK call: %s (" fs ")", #fn, ##__VA_ARGS__); \
106 #define VDDK_CALL_END(fn, bytes_) \
107 while (0); \
108 if (vddk_debug_stats) { \
109 gettimeofday (&end_t, NULL); \
110 ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&stats_lock); \
111 stats_##fn.usecs += tvdiff_usec (&start_t, &end_t); \
112 stats_##fn.calls++; \
113 stats_##fn.bytes += bytes_; \
115 } while (0)
117 /* Print VDDK errors. */
118 #define VDDK_ERROR(err, fs, ...) \
119 do { \
120 char *vddk_err_msg; \
121 VDDK_CALL_START (VixDiskLib_GetErrorText, "%lu", err) \
122 vddk_err_msg = VixDiskLib_GetErrorText ((err), NULL); \
123 VDDK_CALL_END (VixDiskLib_GetErrorText, 0); \
124 nbdkit_error (fs ": %s", ##__VA_ARGS__, vddk_err_msg); \
125 VDDK_CALL_START (VixDiskLib_FreeErrorText, "") \
126 VixDiskLib_FreeErrorText (vddk_err_msg); \
127 VDDK_CALL_END (VixDiskLib_FreeErrorText, 0); \
128 } while (0)
130 /* Queue of asynchronous commands sent to the background thread. */
131 enum command_type { INFO, READ, WRITE, FLUSH, CAN_EXTENTS, EXTENTS, STOP };
132 struct command {
133 /* These fields are set by the caller. */
134 enum command_type type; /* command */
135 void *ptr; /* buffer, extents list, return values */
136 uint32_t count; /* READ, WRITE, EXTENTS */
137 uint64_t offset; /* READ, WRITE, EXTENTS */
138 bool req_one; /* EXTENTS NBDKIT_FLAG_REQ_ONE */
140 /* This field is set to a unique value by send_command_and_wait. */
141 uint64_t id; /* serial number */
143 /* These fields are used by the internal implementation. */
144 pthread_mutex_t mutex; /* completion mutex */
145 pthread_cond_t cond; /* completion condition */
146 enum { SUBMITTED, SUCCEEDED, FAILED } status;
149 DEFINE_VECTOR_TYPE (command_queue, struct command *);
151 /* The per-connection handle. */
152 struct vddk_handle {
153 VixDiskLibConnectParams *params; /* connection parameters */
154 VixDiskLibConnection connection; /* connection */
155 VixDiskLibHandle handle; /* disk handle */
157 pthread_t thread; /* background thread for asynch work */
159 /* Command queue of commands sent to the background thread. Use
160 * send_command_and_wait to add a command. Only the background
161 * thread must make VDDK API calls (apart from opening and closing).
162 * The lock protects all of these fields.
164 pthread_mutex_t commands_lock; /* lock */
165 command_queue commands; /* command queue */
166 pthread_cond_t commands_cond; /* condition (queue size 0 -> 1) */
167 uint64_t id; /* next command ID */
170 /* reexec.c */
171 extern bool noreexec;
172 extern char *reexeced;
173 extern void reexec_if_needed (const char *prepend);
174 extern int restore_ld_library_path (void);
176 /* stats.c */
177 struct vddk_stat {
178 const char *name; /* function name */
179 int64_t usecs; /* total number of usecs consumed */
180 uint64_t calls; /* number of times called */
181 uint64_t bytes; /* bytes transferred, datapath calls only */
183 extern pthread_mutex_t stats_lock;
184 #define STUB(fn, ret, args) extern struct vddk_stat stats_##fn;
185 #define OPTIONAL_STUB(fn, ret, args) STUB (fn, ret, args)
186 #include "vddk-stubs.h"
187 #undef STUB
188 #undef OPTIONAL_STUB
189 extern void display_stats (void);
191 /* utils.c */
192 extern void trim (char *str);
194 /* worker.c */
195 extern const char *command_type_string (enum command_type type);
196 extern int send_command_and_wait (struct vddk_handle *h, struct command *cmd);
197 extern void *vddk_worker_thread (void *handle);
199 #endif /* NBDKIT_VDDK_H */