libvirt_ffi 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e90f40ae66ad4112b924b384083ffb6c4d570ef216ad5c72d7c3d20a043d626
4
- data.tar.gz: c8b476a622973d27d269d136a1040bee40b7358c334361b12a0c5a0fcc703598
3
+ metadata.gz: 5f84407d986359b92b8ad0eaf854f85d098e93835d218a69a9a346f864176c9d
4
+ data.tar.gz: c339098e1c00e91f229f814eb399e1ba1980673428d1bf78c565a79080d5456e
5
5
  SHA512:
6
- metadata.gz: 1d1f5d665e72e42bed01e86eed59201d30158a4977651667d2d6bb592381d5fdd90dc19f75ce5c47b98234408321824b50155b984ee25f08d6d399e398238d18
7
- data.tar.gz: 9bb95047323c0667af10a2d74884ffe749514303635853185f439582074226a702f7b99afa653cd76096c0bea2d7bb77a4596b07e94f3f8e34edb16ccfc886a7
6
+ metadata.gz: ef9bf3632b42bc37324dbd9cc382b908f6320cdb7a8c16c91bd05a2b5c4f402d2fb675079c3ab0fce9157ad4557caa4b5bd37044841b1ce558c7d2eaf9901502
7
+ data.tar.gz: 504c1ac8e2c3df785f8754e2abc3bc5643dc7c5c553a8f10793f9e14ea40da9c07961fb510b6a4ae701f2d9df66f689acec0cf09d593a7d23d663e52703b5309
@@ -16,6 +16,8 @@ require 'libvirt/domain'
16
16
  require 'libvirt/stream'
17
17
  require 'libvirt/storage_pool'
18
18
  require 'libvirt/storage_volume'
19
+ require 'libvirt/network'
20
+ require 'libvirt/interface'
19
21
  require 'libvirt/version'
20
22
 
21
23
  module Libvirt
@@ -16,6 +16,7 @@ module Libvirt
16
16
  end
17
17
  end
18
18
 
19
+ # @param pointer [FFI::Pointer]
19
20
  def initialize(pointer)
20
21
  raise ArgumentError, "Can't initialize base class #{self.class}" if self.class == BaseInfo
21
22
 
@@ -23,12 +24,19 @@ module Libvirt
23
24
  @struct = self.class._struct_class.new(pointer)
24
25
  end
25
26
 
27
+ # @param attr [Symbol]
28
+ # @return [Object, nil]
26
29
  def [](attr)
27
30
  @struct[attr]
28
31
  end
29
32
 
33
+ # @return [Hash]
30
34
  def to_h
31
35
  @struct.members.map { |attr| [attr, @struct[attr]] }.to_h
32
36
  end
37
+
38
+ def to_ptr
39
+ @struct.to_ptr
40
+ end
33
41
  end
34
42
  end
@@ -4,9 +4,11 @@ module Libvirt
4
4
  class Connection
5
5
  DOMAIN_EVENT_IDS = FFI::Domain.enum_type(:event_id).symbols.dup.freeze
6
6
  POOL_EVENT_IDS = FFI::Storage.enum_type(:event_id).symbols.dup.freeze
7
+ NETWORK_EVENT_IDS = FFI::Network.enum_type(:event_id).symbols.dup.freeze
7
8
 
8
9
  DOMAIN_STORAGE = HostCallbackStorage.new(:domain_event)
9
10
  POOL_STORAGE = HostCallbackStorage.new(:storage_pool_event)
11
+ NETWORK_STORAGE = HostCallbackStorage.new(:network_event)
10
12
  CLOSE_STORAGE = HostCallbackStorage.new(:close)
11
13
 
12
14
  DOMAIN_EVENT_CALLBACKS = DOMAIN_EVENT_IDS.map do |event_id_sym|
@@ -35,6 +37,19 @@ module Libvirt
35
37
  [event_id_sym, func]
36
38
  end.to_h.freeze
37
39
 
40
+ NETWORK_EVENT_CALLBACKS = NETWORK_EVENT_IDS.map do |event_id_sym|
41
+ func = FFI::Network.event_callback_for(event_id_sym) do |conn_ptr, pool_ptr, *args, op_ptr|
42
+ Util.log(:debug, "NETWORK_EVENT_CALLBACKS[#{event_id_sym}]") do
43
+ "inside callback conn_ptr=#{conn_ptr}, pool_ptr=#{pool_ptr}, args=#{args}, op_ptr=#{op_ptr}"
44
+ end
45
+ connection = Connection.load_ref(conn_ptr)
46
+ pool = Network.load_ref(pool_ptr)
47
+ block, opaque = NETWORK_STORAGE.retrieve_from_pointer(op_ptr)
48
+ block.call(connection, pool, *args, opaque)
49
+ end
50
+ [event_id_sym, func]
51
+ end.to_h.freeze
52
+
38
53
  CLOSE_CALLBACK = FFI::Host.callback_function(:virConnectCloseFunc) do |conn_ptr, reason, op_ptr|
39
54
  Util.log(:debug, 'CLOSE_CALLBACK') { "inside callback conn_ptr=#{conn_ptr}, reason=#{reason}, op_ptr=#{op_ptr}" }
40
55
  connection = Connection.load_ref(conn_ptr)
@@ -160,6 +175,60 @@ module Libvirt
160
175
  ptr.get_array_of_pointer(0, size).map { |stp_ptr| StoragePool.new(stp_ptr) }
161
176
  end
162
177
 
178
+ # @param options_or_flags [Array<Symbol>,Hash{Symbol=>Boolean},Integer,Symbol,nil]
179
+ # @return [Integer]
180
+ # @raise [Libvirt::Errors::LibError]
181
+ def list_all_networks_qty(options_or_flags = nil)
182
+ flags = Util.parse_flags options_or_flags, FFI::Network.enum_type(:list_all_flags)
183
+ result = FFI::Network.virConnectListAllNetworks(@conn_ptr, nil, flags)
184
+ raise Errors::LibError, "Couldn't retrieve networks qty with flags #{flags.to_s(16)}" if result.negative?
185
+
186
+ result
187
+ end
188
+
189
+ # @param options_or_flags [Array<Symbol>,Hash{Symbol=>Boolean},Integer,Symbol,nil]
190
+ # @return [Array<Libvirt::Network>, Array]
191
+ # @raise [Libvirt::Errors::LibError]
192
+ def list_all_networks(options_or_flags = nil)
193
+ flags = Util.parse_flags options_or_flags, FFI::Network.enum_type(:list_all_flags)
194
+ size = list_all_networks_qty(flags)
195
+ return [] if size.zero?
196
+
197
+ networks_ptr = ::FFI::MemoryPointer.new(:pointer, size)
198
+ result = FFI::Network.virConnectListAllNetworks(@conn_ptr, networks_ptr, 0)
199
+ raise Errors::LibError, "Couldn't retrieve networks list" if result.negative?
200
+
201
+ ptr = networks_ptr.read_pointer
202
+ ptr.get_array_of_pointer(0, size).map { |n_ptr| Network.new(n_ptr) }
203
+ end
204
+
205
+ # @param options_or_flags [Array<Symbol>,Hash{Symbol=>Boolean},Integer,Symbol,nil]
206
+ # @return [Integer]
207
+ # @raise [Libvirt::Errors::LibError]
208
+ def list_all_interfaces_qty(options_or_flags = nil)
209
+ flags = Util.parse_flags options_or_flags, FFI::Interface.enum_type(:list_all_flags)
210
+ result = FFI::Interface.virConnectListAllInterfaces(@conn_ptr, nil, flags)
211
+ raise Errors::LibError, "Couldn't retrieve interfaces qty with flags #{flags.to_s(16)}" if result.negative?
212
+
213
+ result
214
+ end
215
+
216
+ # @param options_or_flags [Array<Symbol>,Hash{Symbol=>Boolean},Integer,Symbol,nil]
217
+ # @return [Array<Libvirt::Interface>, Array]
218
+ # @raise [Libvirt::Errors::LibError]
219
+ def list_all_interfaces(options_or_flags = nil)
220
+ flags = Util.parse_flags options_or_flags, FFI::Interface.enum_type(:list_all_flags)
221
+ size = list_all_interfaces_qty(flags)
222
+ return [] if size.zero?
223
+
224
+ interfaces_ptr = ::FFI::MemoryPointer.new(:pointer, size)
225
+ result = FFI::Interface.virConnectListAllInterfaces(@conn_ptr, interfaces_ptr, 0)
226
+ raise Errors::LibError, "Couldn't retrieve interfaces list" if result.negative?
227
+
228
+ ptr = interfaces_ptr.read_pointer
229
+ ptr.get_array_of_pointer(0, size).map { |i_ptr| Interface.new(i_ptr) }
230
+ end
231
+
163
232
  def register_close_callback(opaque = nil, &block)
164
233
  dbg { "#register_close_callback opaque=#{opaque}" }
165
234
 
@@ -186,6 +255,17 @@ module Libvirt
186
255
  result
187
256
  end
188
257
 
258
+ def deregister_close_callback
259
+ dbg { '#deregister_close_callback' }
260
+
261
+ result = FFI::Host.virConnectUnregisterCloseCallback(@conn_ptr, CLOSE_CALLBACK)
262
+ raise Errors::LibError, "Couldn't deregister close callback" if result.negative?
263
+
264
+ # virConnectUnregisterCloseCallback will call free func
265
+ # So we don't need to remove object from CLOSE_STORAGE here.
266
+ true
267
+ end
268
+
189
269
  # @yield conn, dom, *args
190
270
  def register_domain_event_callback(event_id, domain = nil, opaque = nil, &block)
191
271
  dbg { "#register_domain_event_callback event_id=#{event_id}" }
@@ -275,14 +355,47 @@ module Libvirt
275
355
  true
276
356
  end
277
357
 
278
- def deregister_close_callback
279
- dbg { '#deregister_close_callback' }
358
+ def register_network_event_callback(event_id, network = nil, opaque = nil, &block)
359
+ dbg { "#register_network_event_callback event_id=#{event_id}" }
280
360
 
281
- result = FFI::Host.virConnectUnregisterCloseCallback(@conn_ptr, CLOSE_CALLBACK)
282
- raise Errors::LibError, "Couldn't deregister close callback" if result.negative?
361
+ enum = FFI::Network.enum_type(:event_id)
362
+ event_id, event_id_sym = Util.parse_enum(enum, event_id)
363
+ cb = NETWORK_EVENT_CALLBACKS.fetch(event_id_sym)
283
364
 
284
- # virConnectUnregisterCloseCallback will call free func
285
- # So we don't need to remove object from CLOSE_STORAGE here.
365
+ cb_data, cb_data_free_func = NETWORK_STORAGE.allocate_struct
366
+
367
+ result = FFI::Network.virConnectNetworkEventRegisterAny(
368
+ @conn_ptr,
369
+ network&.to_ptr,
370
+ event_id,
371
+ cb,
372
+ cb_data.pointer,
373
+ cb_data_free_func
374
+ )
375
+ if result.negative?
376
+ cb_data.pointer.free
377
+ raise Errors::LibError, "Couldn't register network event callback"
378
+ end
379
+
380
+ NETWORK_STORAGE.store_struct(
381
+ cb_data,
382
+ connection_pointer: @conn_ptr,
383
+ callback_id: result,
384
+ cb: block,
385
+ opaque: opaque,
386
+ free_func: cb_data_free_func
387
+ )
388
+ result
389
+ end
390
+
391
+ def deregister_network_event_callback(callback_id)
392
+ dbg { "#deregister_network_event_callback callback_id=#{callback_id}" }
393
+
394
+ result = FFI::Network.virConnectNetworkEventDeregisterAny(@conn_ptr, callback_id)
395
+ raise Errors::LibError, "Couldn't deregister network event callback" if result.negative?
396
+
397
+ # virConnectNetworkEventDeregisterAny will call free func
398
+ # So we don't need to remove object from NETWORK_STORAGE here.
286
399
  true
287
400
  end
288
401
 
@@ -331,6 +444,51 @@ module Libvirt
331
444
  Domain.new(pointer)
332
445
  end
333
446
 
447
+ # @param xml [String]
448
+ # @raise [Libvirt::Errors::LibError]
449
+ def create_network(xml)
450
+ pointer = FFI::Network.virNetworkCreateXML(@ptr, xml)
451
+ raise Errors::LibError, "Couldn't create network with xml" if pointer.null?
452
+
453
+ Network.new(pointer)
454
+ end
455
+
456
+ # @param xml [String]
457
+ # @raise [Libvirt::Errors::LibError]
458
+ def define_network(xml)
459
+ pointer = FFI::Network.virNetworkDefineXML(@ptr, xml)
460
+ raise Errors::LibError, "Couldn't define network with xml" if pointer.null?
461
+
462
+ Network.new(pointer)
463
+ end
464
+
465
+ # @param xml [String]
466
+ # @raise [Libvirt::Errors::LibError]
467
+ def define_interface(xml)
468
+ pointer = FFI::Interface.virInterfaceDefineXML(@conn_ptr, xml, 0)
469
+ raise Errors::LibError, "Couldn't define interface with xml" if pointer.null?
470
+
471
+ Interface.new(pointer)
472
+ end
473
+
474
+ # @raise [Libvirt::Errors::LibError]
475
+ def begin_interface_change
476
+ result = FFI::Interface.virInterfaceChangeBegin(@conn_ptr, 0)
477
+ raise Errors::LibError, "Couldn't begin interface change" if result.negative?
478
+ end
479
+
480
+ # @raise [Libvirt::Errors::LibError]
481
+ def commit_interface_change
482
+ result = FFI::Interface.virInterfaceChangeCommit(@conn_ptr, 0)
483
+ raise Errors::LibError, "Couldn't commit interface change" if result.negative?
484
+ end
485
+
486
+ # @raise [Libvirt::Errors::LibError]
487
+ def rollback_interface_change
488
+ result = FFI::Interface.virInterfaceChangeRollback(@conn_ptr, 0)
489
+ raise Errors::LibError, "Couldn't rollback interface change" if result.negative?
490
+ end
491
+
334
492
  private
335
493
 
336
494
  def set_connection(conn_ptr)
@@ -61,7 +61,7 @@ module Libvirt
61
61
  def auto_start
62
62
  value = ::FFI::MemoryPointer.new(:int)
63
63
  result = FFI::Domain.virDomainGetAutostart(@dom_ptr, value)
64
- raise Errors::LibError, "Couldn't get domain uuid" if result.negative?
64
+ raise Errors::LibError, "Couldn't get domain auto_start" if result.negative?
65
65
 
66
66
  value.read_int == 1
67
67
  end
@@ -71,7 +71,7 @@ module Libvirt
71
71
  def set_auto_start(value)
72
72
  value = value ? 1 : 0
73
73
  result = FFI::Domain.virDomainSetAutostart(@dom_ptr, value)
74
- raise Errors::LibError, "Couldn't get domain uuid" if result.negative?
74
+ raise Errors::LibError, "Couldn't set domain auto_start" if result.negative?
75
75
  end
76
76
 
77
77
  # def vcpus
@@ -13,5 +13,7 @@ module Libvirt
13
13
  require 'libvirt/ffi/event'
14
14
  require 'libvirt/ffi/stream'
15
15
  require 'libvirt/ffi/storage'
16
+ require 'libvirt/ffi/network'
17
+ require 'libvirt/ffi/interface'
16
18
  end
17
19
  end
@@ -0,0 +1,175 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Libvirt
4
+ module FFI
5
+ module Interface
6
+ extend ::FFI::Library
7
+ extend Helpers
8
+ ffi_lib Util.library_path
9
+
10
+ ## Variables
11
+
12
+ ## Enums
13
+
14
+ # enum virConnectListAllInterfacesFlags
15
+ enum :list_all_flags, [
16
+ :INACTIVE, 0x1,
17
+ :ACTIVE, 0x2
18
+ ]
19
+
20
+ # enum virInterfaceXMLFlags
21
+ enum :xml_flags, [
22
+ :INACTIVE, 0x1 # dump inactive interface information
23
+ ]
24
+
25
+ ## Functions
26
+
27
+ # int virConnectListAllInterfaces (
28
+ # virConnectPtr conn,
29
+ # virInterfacePtr ** ifaces,
30
+ # unsigned int flags
31
+ # )
32
+ attach_function :virConnectListAllInterfaces,
33
+ [:pointer, :pointer, :list_all_flags],
34
+ :int
35
+
36
+ # int virConnectListDefinedInterfaces (
37
+ # virConnectPtr conn,
38
+ # char ** const names,
39
+ # int maxnames
40
+ # )
41
+ attach_function :virConnectListDefinedInterfaces,
42
+ [:pointer, :pointer, :int],
43
+ :int
44
+
45
+ # int virConnectListInterfaces (
46
+ # virConnectPtr conn,
47
+ # char ** const names,
48
+ # int maxnames
49
+ # )
50
+ attach_function :virConnectListInterfaces,
51
+ [:pointer, :pointer, :int],
52
+ :int
53
+
54
+ # int virConnectNumOfDefinedInterfaces (virConnectPtr conn)
55
+ attach_function :virConnectNumOfDefinedInterfaces,
56
+ [:pointer],
57
+ :int
58
+
59
+ # int virConnectNumOfInterfaces (virConnectPtr conn)
60
+ attach_function :virConnectNumOfInterfaces,
61
+ [:pointer],
62
+ :int
63
+
64
+ # int virInterfaceChangeBegin (
65
+ # virConnectPtr conn,
66
+ # unsigned int flags
67
+ # )
68
+ attach_function :virInterfaceChangeBegin,
69
+ [:pointer, :uint],
70
+ :int
71
+
72
+ # int virInterfaceChangeCommit (
73
+ # virConnectPtr conn,
74
+ # unsigned int flags
75
+ # )
76
+ attach_function :virInterfaceChangeCommit,
77
+ [:pointer, :uint],
78
+ :int
79
+
80
+ # int virInterfaceChangeRollback (
81
+ # virConnectPtr conn,
82
+ # unsigned int flags
83
+ # )
84
+ attach_function :virInterfaceChangeRollback,
85
+ [:pointer, :uint],
86
+ :int
87
+
88
+ # int virInterfaceCreate (
89
+ # virInterfacePtr iface,
90
+ # unsigned int flags
91
+ # )
92
+ attach_function :virInterfaceCreate,
93
+ [:pointer, :uint],
94
+ :int
95
+
96
+ # virInterfacePtr virInterfaceDefineXML (
97
+ # virConnectPtr conn,
98
+ # const char * xml,
99
+ # unsigned int flags
100
+ # )
101
+ attach_function :virInterfaceDefineXML,
102
+ [:pointer, :string, :uint],
103
+ :pointer
104
+
105
+ # int virInterfaceDestroy (
106
+ # virInterfacePtr iface,
107
+ # unsigned int flags
108
+ # )
109
+ attach_function :virInterfaceDestroy,
110
+ [:pointer, :uint],
111
+ :int
112
+
113
+ # int virInterfaceFree (virInterfacePtr iface)
114
+ attach_function :virInterfaceFree,
115
+ [:pointer],
116
+ :int
117
+
118
+ # virConnectPtr virInterfaceGetConnect (virInterfacePtr iface)
119
+ attach_function :virInterfaceGetConnect,
120
+ [:pointer],
121
+ :int
122
+
123
+ # const char * virInterfaceGetMACString (virInterfacePtr iface)
124
+ attach_function :virInterfaceGetMACString,
125
+ [:pointer],
126
+ :string
127
+
128
+ # const char * virInterfaceGetName (virInterfacePtr iface)
129
+ attach_function :virInterfaceGetName,
130
+ [:pointer],
131
+ :string
132
+
133
+ # char * virInterfaceGetXMLDesc (
134
+ # virInterfacePtr iface,
135
+ # unsigned int flags
136
+ # )
137
+ attach_function :virInterfaceGetXMLDesc,
138
+ [:pointer, :uint],
139
+ :string
140
+
141
+ # int virInterfaceIsActive (virInterfacePtr iface)
142
+ attach_function :virInterfaceIsActive,
143
+ [:pointer],
144
+ :int
145
+
146
+ # virInterfacePtr virInterfaceLookupByMACString (
147
+ # virConnectPtr conn,
148
+ # const char * macstr
149
+ # )
150
+ attach_function :virInterfaceLookupByMACString,
151
+ [:pointer, :string],
152
+ :pointer
153
+
154
+ # virInterfacePtr virInterfaceLookupByName (
155
+ # virConnectPtr conn,
156
+ # const char * name
157
+ # )
158
+ attach_function :virInterfaceLookupByName,
159
+ [:pointer, :string],
160
+ :pointer
161
+
162
+ # int virInterfaceRef (virInterfacePtr iface)
163
+ attach_function :virInterfaceRef,
164
+ [:pointer],
165
+ :int
166
+
167
+ # int virInterfaceUndefine (virInterfacePtr iface)
168
+ attach_function :virInterfaceUndefine,
169
+ [:pointer],
170
+ :int
171
+
172
+ ## Helpers
173
+ end
174
+ end
175
+ end