libvirt_ffi 0.5.6 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,392 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Libvirt
4
+ module FFI
5
+ module Network
6
+ # https://libvirt.org/html/libvirt-libvirt-network.html
7
+
8
+ extend ::FFI::Library
9
+ extend Helpers
10
+ ffi_lib Util.library_path
11
+
12
+ EVENT_ID_TO_CALLBACK = {
13
+ LIFECYCLE: :virConnectNetworkEventLifecycleCallback
14
+ }.freeze
15
+
16
+ ## Enums
17
+
18
+ # enum virConnectListAllNetworksFlags
19
+ enum :list_all_flags, [
20
+ :INACTIVE, 0x1,
21
+ :ACTIVE, 0x2,
22
+ :PERSISTENT, 0x4,
23
+ :TRANSIENT, 0x8,
24
+ :AUTOSTART, 0x10,
25
+ :NO_AUTOSTART, 0x20
26
+ ]
27
+
28
+ # enum virIPAddrType
29
+ enum :ip_addr_type, [
30
+ :IPV4, 0x0,
31
+ :IPV6, 0x1
32
+ ]
33
+
34
+ # enum virNetworkEventID
35
+ enum :event_id, [
36
+ :LIFECYCLE, 0x0 # virConnectNetworkEventLifecycleCallback
37
+ ]
38
+
39
+ # enum virNetworkEventLifecycleType
40
+ enum :event_lifecycle_type, [
41
+ :DEFINED, 0x0,
42
+ :UNDEFINED, 0x1,
43
+ :STARTED, 0x2,
44
+ :STOPPED, 0x3
45
+ ]
46
+
47
+ # enum virNetworkUpdateCommand
48
+ enum :update_command, [
49
+ :NONE, 0x0, # (invalid)
50
+ :MODIFY, 0x1, # modify an existing element
51
+ :DELETE, 0x2, # delete an existing element
52
+ :ADD_LAST, 0x3, # add an element at end of list
53
+ :ADD_FIRST, 0x4 # add an element at start of list
54
+ ]
55
+
56
+ # enum virNetworkUpdateFlags
57
+ enum :update_flags, [
58
+ :AFFECT_CURRENT, 0x0, # affect live if network is active, config if its not active
59
+ :AFFECT_LIVE, 0x1, # affect live state of network only
60
+ :AFFECT_CONFIG, 0x2 # affect persistent config only
61
+ ]
62
+
63
+ # enum virNetworkUpdateSection
64
+ enum :update_section, [
65
+ :NONE, 0x0, # (invalid)
66
+ :BRIDGE, 0x1, # <bridge>
67
+ :DOMAIN, 0x2, # <domain>
68
+ :IP, 0x3, # <ip>
69
+ :IP_DHCP_HOST, 0x4, # <ip>/<dhcp>/<host>
70
+ :IP_DHCP_RANGE, 0x5, # <ip>/<dhcp>/<range>
71
+ :FORWARD, 0x6, # <forward>
72
+ :FORWARD_INTERFACE, 0x7, # <forward>/<interface>
73
+ :FORWARD_PF, 0x8, # <forward>/<pf>
74
+ :PORTGROUP, 0x9, # <portgroup>
75
+ :DNS_HOST, 0xa, # <dns>/<host>
76
+ :DNS_TXT, 0xb, # <dns>/<txt>
77
+ :DNS_SRV, 0xc # <dns>/<srv>
78
+ ]
79
+
80
+ # enum virNetworkXMLFlags
81
+ enum :xml_flags, [
82
+ :INACTIVE, 0x1 # dump inactive network information
83
+ ]
84
+
85
+ ## Structs
86
+
87
+ # struct virNetworkDHCPLease {
88
+ # char * iface # Network interface name
89
+ # long long expirytime # Seconds since epoch
90
+ # int type # virIPAddrType
91
+ # char * mac # MAC address
92
+ # char * iaid # IAID
93
+ # char * ipaddr # IP address
94
+ # unsigned int prefix # IP address prefix
95
+ # char * hostname # Hostname
96
+ # char * clientid # Client ID or DUID
97
+ # }
98
+ class DhcpLeaseStruct < ::FFI::Struct
99
+ layout :iface, :string,
100
+ :expirytime, :long_long,
101
+ :type, FFI::Network.enum_type(:ip_addr_type),
102
+ :mac, :string,
103
+ :iaid, :string,
104
+ :ipaddr, :string,
105
+ :prefix, :uint,
106
+ :hostname, :string,
107
+ :clientid, :string
108
+ end
109
+
110
+ ## Callbacks
111
+
112
+ # typedef virConnectNetworkEventGenericCallback
113
+ # void virConnectNetworkEventGenericCallback (
114
+ # virConnectPtr conn,
115
+ # virNetworkPtr net,
116
+ # void * opaque
117
+ # )
118
+ callback :virConnectNetworkEventGenericCallback,
119
+ [:pointer, :pointer, :pointer],
120
+ :void
121
+
122
+ # typedef virConnectNetworkEventLifecycleCallback
123
+ # void virConnectNetworkEventLifecycleCallback (
124
+ # virConnectPtr conn,
125
+ # virNetworkPtr net,
126
+ # int event,
127
+ # int detail,
128
+ # void * opaque
129
+ # )
130
+ callback :virConnectNetworkEventLifecycleCallback,
131
+ [:pointer, :pointer, :event_lifecycle_type, :int, :pointer],
132
+ :void
133
+
134
+ ## Functions
135
+
136
+ # int virConnectListAllNetworks (
137
+ # virConnectPtr conn,
138
+ # virNetworkPtr ** nets,
139
+ # unsigned int flags
140
+ # )
141
+ attach_function :virConnectListAllNetworks,
142
+ [:pointer, :pointer, :list_all_flags],
143
+ :int
144
+
145
+ # int virConnectListDefinedNetworks (
146
+ # virConnectPtr conn,
147
+ # char ** const names,
148
+ # int maxnames
149
+ # )
150
+ attach_function :virConnectListDefinedNetworks,
151
+ [:pointer, :pointer, :int],
152
+ :int
153
+
154
+ # int virConnectListNetworks (
155
+ # virConnectPtr conn,
156
+ # char ** const names,
157
+ # int maxnames
158
+ # )
159
+ attach_function :virConnectListNetworks,
160
+ [:pointer, :pointer, :int],
161
+ :int
162
+
163
+ # int virConnectNetworkEventDeregisterAny (
164
+ # virConnectPtr conn,
165
+ # int callbackID
166
+ # )
167
+ attach_function :virConnectNetworkEventDeregisterAny, [:pointer, :int], :int
168
+
169
+ # int virConnectNetworkEventRegisterAny (
170
+ # virConnectPtr conn,
171
+ # virNetworkPtr net,
172
+ # int eventID,
173
+ # virConnectNetworkEventGenericCallback cb,
174
+ # void * opaque,
175
+ # virFreeCallback freecb
176
+ # )
177
+ attach_function :virConnectNetworkEventRegisterAny,
178
+ [:pointer, :pointer, :event_id, :pointer, :pointer, :pointer],
179
+ :int
180
+
181
+ # int virConnectNumOfDefinedNetworks (
182
+ # virConnectPtr conn
183
+ # )
184
+ attach_function :virConnectNumOfDefinedNetworks,
185
+ [:pointer],
186
+ :int
187
+
188
+ # int virConnectNumOfNetworks (
189
+ # virConnectPtr conn
190
+ # )
191
+ attach_function :virConnectNumOfNetworks,
192
+ [:pointer],
193
+ :int
194
+
195
+ # int virNetworkCreate (
196
+ # virNetworkPtr network
197
+ # )
198
+ attach_function :virNetworkCreate,
199
+ [:pointer],
200
+ :int
201
+
202
+ # virNetworkPtr virNetworkCreateXML (
203
+ # virConnectPtr conn,
204
+ # const char * xmlDesc
205
+ # )
206
+ attach_function :virNetworkCreateXML,
207
+ [:pointer, :string],
208
+ :pointer
209
+
210
+ # void virNetworkDHCPLeaseFree (
211
+ # virNetworkDHCPLeasePtr lease
212
+ # )
213
+ attach_function :virNetworkDHCPLeaseFree,
214
+ [:pointer],
215
+ :void
216
+
217
+ # virNetworkPtr virNetworkDefineXML (
218
+ # virConnectPtr conn,
219
+ # const char * xml
220
+ # )
221
+ attach_function :virNetworkDefineXML,
222
+ [:pointer, :string],
223
+ :pointer
224
+
225
+ # int virNetworkDestroy (
226
+ # virNetworkPtr network
227
+ # )
228
+ attach_function :virNetworkDestroy,
229
+ [:pointer],
230
+ :int
231
+
232
+ # int virNetworkFree (
233
+ # virNetworkPtr network
234
+ # )
235
+ attach_function :virNetworkFree,
236
+ [:pointer],
237
+ :int
238
+
239
+ # int virNetworkGetAutostart (
240
+ # virNetworkPtr network,
241
+ # int * autostart
242
+ # )
243
+ attach_function :virNetworkGetAutostart,
244
+ [:pointer, :pointer],
245
+ :int
246
+
247
+ # char * virNetworkGetBridgeName (
248
+ # virNetworkPtr network
249
+ # )
250
+ attach_function :virNetworkGetBridgeName,
251
+ [:pointer],
252
+ :string
253
+
254
+ # virConnectPtr virNetworkGetConnect (
255
+ # virNetworkPtr net
256
+ # )
257
+ attach_function :virNetworkGetConnect,
258
+ [:pointer],
259
+ :pointer
260
+
261
+ # int virNetworkGetDHCPLeases (
262
+ # virNetworkPtr network,
263
+ # const char * mac,
264
+ # virNetworkDHCPLeasePtr ** leases,
265
+ # unsigned int flags
266
+ # )
267
+ attach_function :virNetworkGetDHCPLeases,
268
+ [:pointer, :string, :pointer, :uint],
269
+ :int
270
+
271
+ # const char * virNetworkGetName (
272
+ # virNetworkPtr network
273
+ # )
274
+ attach_function :virNetworkGetName,
275
+ [:pointer],
276
+ :string
277
+
278
+ # int virNetworkGetUUID (
279
+ # virNetworkPtr network,
280
+ # unsigned char * uuid
281
+ # )
282
+ attach_function :virNetworkGetUUID,
283
+ [:pointer, :pointer],
284
+ :int
285
+
286
+ # int virNetworkGetUUIDString (
287
+ # virNetworkPtr network,
288
+ # char * buf
289
+ # )
290
+ attach_function :virNetworkGetUUIDString,
291
+ [:pointer, :pointer],
292
+ :int
293
+
294
+ # char * virNetworkGetXMLDesc (
295
+ # virNetworkPtr network,
296
+ # unsigned int flags
297
+ # )
298
+ attach_function :virNetworkGetXMLDesc,
299
+ [:pointer, :xml_flags],
300
+ :string
301
+
302
+ # int virNetworkIsActive (
303
+ # virNetworkPtr net
304
+ # )
305
+ attach_function :virNetworkIsActive,
306
+ [:pointer],
307
+ :int
308
+
309
+ # int virNetworkIsPersistent (
310
+ # virNetworkPtr net
311
+ # )
312
+ attach_function :virNetworkIsPersistent,
313
+ [:pointer],
314
+ :int
315
+
316
+ # virNetworkPtr virNetworkLookupByName (
317
+ # virConnectPtr conn,
318
+ # const char * name
319
+ # )
320
+ attach_function :virNetworkLookupByName,
321
+ [:pointer, :string],
322
+ :pointer
323
+
324
+ # virNetworkPtr virNetworkLookupByUUID (
325
+ # virConnectPtr conn,
326
+ # const unsigned char * uuid
327
+ # )
328
+ attach_function :virNetworkLookupByUUID,
329
+ [:pointer, :pointer],
330
+ :pointer
331
+
332
+ # virNetworkPtr virNetworkLookupByUUIDString (
333
+ # virConnectPtr conn,
334
+ # const char * uuidstr
335
+ # )
336
+ attach_function :virNetworkLookupByUUIDString,
337
+ [:pointer, :string],
338
+ :pointer
339
+
340
+ # int virNetworkRef (
341
+ # virNetworkPtr network
342
+ # )
343
+ attach_function :virNetworkRef,
344
+ [:pointer],
345
+ :int
346
+
347
+ # int virNetworkSetAutostart (
348
+ # virNetworkPtr network,
349
+ # int autostart
350
+ # )
351
+ attach_function :virNetworkSetAutostart,
352
+ [:pointer, :int],
353
+ :int
354
+
355
+ # int virNetworkUndefine (
356
+ # virNetworkPtr network
357
+ # )
358
+ attach_function :virNetworkUndefine,
359
+ [:pointer],
360
+ :int
361
+
362
+ # int virNetworkUpdate (
363
+ # virNetworkPtr network,
364
+ # unsigned int command,
365
+ # unsigned int section,
366
+ # int parentIndex,
367
+ # const char * xml,
368
+ # unsigned int flags
369
+ # )
370
+ attach_function :virNetworkUpdate,
371
+ [:pointer, :update_command, :update_section, :int, :string, :update_flags],
372
+ :int
373
+
374
+ ## Helpers
375
+ module_function
376
+
377
+ # Creates event callback function for provided event_id
378
+ # @param event_id [Integer,Symbol]
379
+ # @yield connect_ptr, network_ptr, *args, opaque_ptr
380
+ # @return [FFI::Function]
381
+ def event_callback_for(event_id, &block)
382
+ event_id_sym = event_id.is_a?(Symbol) ? event_id : enum_type(:event_id)[event_id]
383
+
384
+ callback_name = EVENT_ID_TO_CALLBACK.fetch(event_id_sym)
385
+ callback_function(callback_name) do |*args|
386
+ Util.log(:debug, name) { ".event_callback_for #{event_id_sym} CALLBACK #{args.map(&:to_s).join(', ')}," }
387
+ block.call(*args)
388
+ end
389
+ end
390
+ end
391
+ end
392
+ end
@@ -9,6 +9,11 @@ module Libvirt
9
9
  extend Helpers
10
10
  ffi_lib Util.library_path
11
11
 
12
+ EVENT_ID_TO_CALLBACK = {
13
+ LIFECYCLE: :virConnectStoragePoolEventLifecycleCallback,
14
+ REFRESH: :virConnectStoragePoolEventGenericCallback
15
+ }.freeze
16
+
12
17
  # enum virStoragePoolState
13
18
  enum :pool_state, [
14
19
  :INACTIVE, 0x0, # Not running
@@ -63,6 +68,23 @@ module Libvirt
63
68
  :INACTIVE, 0x1 # dump inactive pool/volume information
64
69
  ]
65
70
 
71
+ # enum virStoragePoolEventID
72
+ enum :event_id, [
73
+ :LIFECYCLE, 0x0, # virConnectStoragePoolEventLifecycleCallback
74
+ :REFRESH, 0x1 # virConnectStoragePoolEventGenericCallback
75
+ ]
76
+
77
+ # enum virStoragePoolEventLifecycleType
78
+ enum :event_lifecycle_type, [
79
+ :DEFINED, 0x0,
80
+ :UNDEFINED, 0x1,
81
+ :STARTED, 0x2,
82
+ :STOPPED, 0x3,
83
+ :CREATED, 0x4,
84
+ :DELETED, 0x5,
85
+ :LAST, 0x6
86
+ ]
87
+
66
88
  # struct virStoragePoolInfo {
67
89
  # int state # virStoragePoolState flags
68
90
  # unsigned long long capacity # Logical size bytes
@@ -87,6 +109,26 @@ module Libvirt
87
109
  :allocation, :ulong_long
88
110
  end
89
111
 
112
+ # typedef void (*virConnectStoragePoolEventGenericCallback) (
113
+ # virConnectPtr conn,
114
+ # virStoragePoolPtr pool,
115
+ # void * opaque
116
+ # )
117
+ callback :virConnectStoragePoolEventGenericCallback,
118
+ [:pointer, :pointer, :pointer],
119
+ :void
120
+
121
+ # typedef void (*virConnectStoragePoolEventLifecycleCallback) (
122
+ # virConnectPtr conn,
123
+ # virStoragePoolPtr pool,
124
+ # int event,
125
+ # int detail,
126
+ # void * opaque
127
+ # )
128
+ callback :virConnectStoragePoolEventLifecycleCallback,
129
+ [:pointer, :pointer, :event_lifecycle_type, :int, :pointer],
130
+ :void
131
+
90
132
  # int virConnectListAllStoragePools (
91
133
  # virConnectPtr conn,
92
134
  # virStoragePoolPtr ** pools,
@@ -144,6 +186,49 @@ module Libvirt
144
186
  # unsigned int flags
145
187
  # )
146
188
  attach_function :virStorageVolGetXMLDesc, [:pointer, :xml_flags], :string
189
+
190
+ # int virConnectStoragePoolEventRegisterAny (
191
+ # virConnectPtr conn,
192
+ # virStoragePoolPtr pool,
193
+ # int eventID,
194
+ # virConnectStoragePoolEventGenericCallback cb,
195
+ # void * opaque,
196
+ # virFreeCallback freecb
197
+ # )
198
+ attach_function :virConnectStoragePoolEventRegisterAny,
199
+ [:pointer, :pointer, :event_id, :pointer, :pointer, FFI::Common::FREE_CALLBACK],
200
+ :int
201
+
202
+ # int virConnectStoragePoolEventDeregisterAny (
203
+ # virConnectPtr conn,
204
+ # int callbackID
205
+ # )
206
+ attach_function :virConnectStoragePoolEventDeregisterAny,
207
+ [:pointer, :int],
208
+ :int
209
+
210
+ # int virStoragePoolGetUUIDString (virStoragePoolPtr pool,
211
+ # char * buf)
212
+ attach_function :virStoragePoolGetUUIDString, [:pointer, :pointer], :int
213
+
214
+ # const char * virStoragePoolGetName (virStoragePoolPtr pool)
215
+ attach_function :virStoragePoolGetName, [:pointer], :string
216
+
217
+ module_function
218
+
219
+ # Creates event callback function for provided event_id
220
+ # @param event_id [Integer,Symbol]
221
+ # @yield connect_ptr, pool_ptr, *args, opaque_ptr
222
+ # @return [FFI::Function]
223
+ def event_callback_for(event_id, &block)
224
+ event_id_sym = event_id.is_a?(Symbol) ? event_id : enum_type(:event_id)[event_id]
225
+
226
+ callback_name = EVENT_ID_TO_CALLBACK.fetch(event_id_sym)
227
+ callback_function(callback_name) do |*args|
228
+ Util.log(:debug, name) { ".event_callback_for #{event_id_sym} CALLBACK #{args.map(&:to_s).join(', ')}," }
229
+ block.call(*args)
230
+ end
231
+ end
147
232
  end
148
233
  end
149
234
  end