forked from autotest/virt-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqemu_devices.py
1391 lines (1265 loc) · 50.4 KB
/
qemu_devices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Library of objects, which could represent qemu devices in order to create
complete representation of VM. There are three parts:
1) Device objects - individual devices representation
2) Bus representation - bus representation
3) Device container - qemu machine representation
@copyright: 2012-2013 Red Hat Inc.
"""
# Python imports
import itertools
import logging
import re
# Autotest imports
from autotest.client.shared import error, utils
import arch
import qemu_monitor
try:
from collections import OrderedDict
except ImportError:
class OrderedDict(dict):
"""
Dictionary which keeps the order of items when using .itervalues()
@warning: This is not the full OrderedDict implementation!
"""
def itervalues(self, *args, **kwargs):
return sorted(dict.itervalues(self, *args, **kwargs),
key=lambda item: item[0])
class DeviceError(Exception):
""" General device exception """
pass
class DeviceInsertError(DeviceError):
""" Fail to insert device """
def __init__(self, device, reason, vmdev):
self.device = device
self.reason = reason
self.vmdev = vmdev
def __str__(self):
return ("Failed to insert device:\n%s\nBecause:\n%s\nList of VM"
"devices:\n%s\n%s" % (self.device.str_long(), self.reason,
self.vmdev, self.vmdev.str_bus_long()))
def _convert_args(arg_dict):
"""
Convert monitor command arguments dict into humanmonitor string.
@param arg_dict: The dict of monitor command arguments.
@return: A string in humanmonitor's 'key=value' format, or a empty
'' when the dict is empty.
"""
return ",".join("%s=%s" % (key, val) for key, val in arg_dict.iteritems())
##############################################################################
# Device objects
##############################################################################
class QBaseDevice(object):
""" Base class of qemu objects """
def __init__(self, dev_type="QBaseDevice", params=None, aobject=None,
parent_bus=None, child_bus=None):
"""
@param dev_type: type of this component
@param params: component's parameters
@param aobject: Autotest object which is associated with this device
@param parent_bus: list of dicts specifying the parent bus
@param child_bus: list of buses, which this device provides
"""
self.aid = None # unique per VM id
self.type = dev_type # device type
self.aobject = aobject # related autotest object
if parent_bus is None:
parent_bus = tuple()
self.parent_bus = parent_bus # list of buses into which this dev fits
if child_bus is None:
child_bus = tuple()
self.child_bus = child_bus # list of buses which this dev provides
self.params = OrderedDict() # various device params (id, name, ...)
if params:
for key, value in params.iteritems():
self.set_param(key, value)
def set_param(self, option, value, option_type=None):
"""
Set device param using qemu notation ("on", "off" instead of bool...)
@param option: which option's value to set
@param value: new value
@param option_type: type of the option (bool)
"""
if option_type is bool or isinstance(value, bool):
if value in ['yes', 'on', True]:
self.params[option] = "on"
elif value in ['no', 'off', False]:
self.params[option] = "off"
elif value or value == 0:
if value == "EMPTY_STRING":
self.params[option] = '""'
else:
self.params[option] = value
elif value is None and option in self.params:
del(self.params[option])
def get_param(self, option):
""" @return: object param """
return self.params.get(option)
def __getitem__(self, option):
""" @return: object param """
return self.params[option]
def __delitem__(self, option):
""" deletes self.params[option] """
del(self.params[option])
def __len__(self):
""" length of self.params """
return len(self.params)
def __setitem__(self, option, value):
""" self.set_param(option, value, None) """
return self.set_param(option, value)
def __contains__(self, option):
""" Is the option set? """
return option in self.params
def __str__(self):
""" @return: Short string representation of this object. """
return self.str_short()
def __eq__(self, dev2):
""" @return: True when devs are similar, False when different. """
try:
for check_attr in ('cmdline', 'hotplug_hmp',
'hotplug_qmp'):
try:
_ = getattr(self, check_attr)()
except (DeviceError, NotImplementedError, AttributeError):
try:
getattr(dev2, check_attr)()
except (DeviceError, NotImplementedError, AttributeError):
pass
else:
if _ != getattr(dev2, check_attr)():
return False
except Exception:
return False
return True
def __ne__(self, dev2):
""" @return: True when devs are different, False when similar. """
return not self.__eq__(dev2)
def str_short(self):
""" Short representation (aid, qid, alternative, type) """
if self.get_qid(): # Show aid only when it's based on qid
if self.get_aid():
return self.get_aid()
else:
return "q'%s'" % self.get_qid()
elif self._get_alternative_name():
return "a'%s'" % self._get_alternative_name()
else:
return "t'%s'" % self.type
def str_long(self):
""" Full representation, multi-line with all params """
out = """%s
aid = %s
aobject = %s
parent_bus = %s
child_bus = %s
params:""" % (self.type, self.aid, self.aobject, self.parent_bus,
self.child_bus)
for key, value in self.params.iteritems():
out += "\n %s = %s" % (key, value)
return out + '\n'
def _get_alternative_name(self):
""" @return: alternative object name """
return None
def get_qid(self):
""" @return: qemu_id """
return self.params.get('id', '')
def get_aid(self):
""" @return: per VM unique autotest_id """
return self.aid
def set_aid(self, aid):
"""@param aid: new autotest id for this device"""
self.aid = aid
def cmdline(self):
""" @return: cmdline command to define this device """
raise NotImplementedError
def hotplug(self, monitor):
""" @return: the output of monitor.cmd() hotplug command """
if isinstance(monitor, qemu_monitor.QMPMonitor):
try:
cmd, args = self.hotplug_qmp()
return monitor.cmd(cmd, args)
except DeviceError: # qmp command not supported
return monitor.human_monitor_cmd(self.hotplug_hmp())
elif isinstance(monitor, qemu_monitor.HumanMonitor):
return monitor.cmd(self.hotplug_hmp())
else:
raise TypeError("Invalid monitor object: %s(%s)" % (monitor,
type(monitor)))
def hotplug_hmp(self):
""" @return: the hotplug monitor command """
raise DeviceError("Hotplug is not supported by this device %s", self)
def hotplug_qmp(self):
""" @return: tuple(hotplug qemu command, arguments)"""
raise DeviceError("Hotplug is not supported by this device %s", self)
def verify_hotplug(self, out, monitor):
"""
@param out: Output of the hotplug command
@param monitor: Monitor used for hotplug
@return: True when successful, False when unsuccessful, string/None
when can't decide.
"""
return out
class QStringDevice(QBaseDevice):
"""
General device which allows to specify methods by fixed or parametrizable
strings in this format:
"%(type)s,id=%(id)s,addr=%(addr)s" -- params will be used to subst %()s
"""
def __init__(self, dev_type, params=None, aobject=None,
parent_bus=None, child_bus=None, cmdline=""):
"""
@param dev_type: type of this component
@param params: component's parameters
@param aobject: Autotest object which is associated with this device
@param parent_bus: bus(es), in which this device is plugged in
@param child_bus: bus, which this device provides
@param cmdline: cmdline string
"""
super(QStringDevice, self).__init__(dev_type, params, aobject,
parent_bus, child_bus)
self._cmdline = cmdline
def cmdline(self):
""" @return: cmdline command to define this device """
try:
return self._cmdline % self.params
except KeyError, details:
raise KeyError("Param %s required for cmdline is not present in %s"
% (details, self.str_long()))
class QCustomDevice(QBaseDevice):
"""
Representation of the '-$option $param1=$value1,$param2...' qemu object.
This representation handles only cmdline.
"""
def __init__(self, dev_type, params=None, aobject=None,
parent_bus=None, child_bus=None):
"""
@param dev_type: The desired -$option parameter (device, chardev, ..)
"""
super(QCustomDevice, self).__init__(dev_type, params, aobject,
parent_bus, child_bus)
def cmdline(self):
""" @return: cmdline command to define this device """
out = "-%s " % self.type
for key, value in self.params.iteritems():
if value != "NO_EQUAL_STRING":
out += "%s=%s," % (key, value)
else:
out += "%s," % key
if out[-1] == ',':
out = out[:-1]
return out
class QDevice(QCustomDevice):
"""
Representation of the '-device' qemu object. It supports all methods.
@note: Use driver format in full form - 'driver' = '...' (usb-ehci, ide-hd)
"""
def __init__(self, driver=None, params=None, aobject=None,
parent_bus=None, child_bus=None):
super(QDevice, self).__init__("device", params, aobject, parent_bus,
child_bus)
if driver:
self['driver'] = driver
def _get_alternative_name(self):
""" @return: alternative object name """
if self.params.get('driver'):
return self.params.get('driver')
def hotplug_hmp(self):
""" @return: the hotplug monitor command """
return "device_add %s" % _convert_args(self.params)
def hotplug_qmp(self):
""" @return: the hotplug monitor command """
return "device_add", self.params
##############################################################################
# Bus representations
# HDA, I2C, IDE, ISA, PCI, SCSI, System, uhci, ehci, ohci, xhci, ccid,
# virtio-serial-bus
##############################################################################
class QSparseBus(object):
"""
Universal bus representation object.
It creates an abstraction of the way how buses works in qemu. Additionaly
it can store incorrect records (out-of-range addr, multiple devs, ...).
Everything with bad* prefix means it concerns the bad records (badbus).
You can insert and remove device to certain address, address ranges or let
the bus assign first free address. The order of addr_spec does matter since
the last item is incremented first.
There are 3 different address representation used:
stor_addr = stored address representation '$first-$second-...-$ZZZ'
addr = internal address representation [$first, $second, ..., $ZZZ]
device_addr = qemu address stored into separate device params (bus, port)
device{$param1:$first, $param2:$second, ..., $paramZZZ, $ZZZ}
@note: When you insert a device, it's properties might be updated (addr,..)
"""
def __init__(self, bus_item, addr_spec, busid, bus_type, aobject=None):
"""
@param bus_item: Name of the parameter which specifies bus (bus)
@param addr_spec: Bus address specification [names][lengths]
@param busid: id of the bus (pci.0)
@param bus_type: type of the bus (pci)
@param aobject: Related autotest object (image1)
"""
self.busid = busid
self.type = bus_type
self.aobject = aobject
self.bus = {} # Normal bus records
self.badbus = {} # Bad bus records
self.bus_item = bus_item # bus param name
self.addr_items = addr_spec[0] # [names][lengths]
self.addr_lengths = addr_spec[1]
def __str__(self):
""" default string representation """
return self.str_short()
def __getitem__(self, item):
"""
@param item: autotest id or QObject-like object
@return: First matching object from this bus
@raise KeyError: In case no match was found
"""
if isinstance(item, QBaseDevice):
if item in self.bus.itervalues():
return item
elif item in self.badbus.itervalues():
return item
elif item:
for device in self.bus.itervalues():
if device.get_aid() == item:
return device
for device in self.badbus.itervalues():
if device.get_aid() == item:
return device
raise KeyError("Device %s is not in %s" % (item, self))
def get(self, item):
"""
@param item: autotest id or QObject-like object
@return: First matching object from this bus or None
"""
if item in self:
return self[item]
def __delitem__(self, item):
"""
Remove device from bus
@param item: autotest id or QObject-like object
@raise KeyError: In case no match was found
"""
self.remove(self[item])
def __len__(self):
""" @return: Number of devices in this bus """
return len(self.bus) + len(self.badbus)
def __contains__(self, item):
"""
Is specified item in this bus?
@param item: autotest id or QObject-like object
@return: True - yes, False - no
"""
if isinstance(item, QBaseDevice):
if (item in self.bus.itervalues() or
item in self.badbus.itervalues()):
return True
elif item:
for device in self:
if device.get_aid() == item:
return True
return False
def __iter__(self):
""" Iterate over all defined devices. """
return itertools.chain(self.bus.itervalues(),
self.badbus.itervalues())
def str_short(self):
""" short string representation """
return "%s(%s): %s %s" % (self.busid, self.type, self._str_devices(),
self._str_bad_devices())
def _str_devices(self):
""" short string representation of the good bus """
out = '{'
for addr in sorted(self.bus.keys()):
out += "%s:" % addr
out += "%s," % self.bus[addr]
if out[-1] == ',':
out = out[:-1]
return out + '}'
def _str_bad_devices(self):
""" short string representation of the bad bus """
out = '{'
for addr in sorted(self.badbus.keys()):
out += "%s:" % addr
out += "%s," % self.badbus[addr]
if out[-1] == ',':
out = out[:-1]
return out + '}'
def str_long(self):
""" long string representation """
return "Bus %s, type=%s\nSlots:\n%s\n%s" % (self.busid, self.type,
self._str_devices_long(), self._str_bad_devices_long())
def _str_devices_long(self):
""" long string representation of devices in the good bus """
out = ""
for addr, dev in self.bus.iteritems():
out += '%s< %4s >%s\n ' % ('-' * 15, addr,
'-' * 15)
if isinstance(dev, str):
out += '"%s"\n ' % dev
else:
out += dev.str_long().replace('\n', '\n ')
out = out[:-3]
out += '\n'
return out
def _str_bad_devices_long(self):
""" long string representation of devices in the bad bus """
out = ""
for addr, dev in self.badbus.iteritems():
out += '%s< %4s >%s\n ' % ('-' * 15, addr,
'-' * 15)
if isinstance(dev, str):
out += '"%s"\n ' % dev
else:
out += dev.str_long().replace('\n', '\n ')
out = out[:-3]
out += '\n'
return out
def _increment_addr(self, addr, last_addr=None):
"""
Increment addr base of addr_pattern and last used addr
@param addr: addr_pattern
@param last_addr: previous address
@return: last_addr + 1
"""
if not last_addr:
last_addr = [0] * len(self.addr_lengths)
i = -1
while True:
if i < -len(self.addr_lengths):
return False
if addr[i] is not None:
i -= 1
continue
last_addr[i] += 1
if last_addr[i] < self.addr_lengths[i]:
return last_addr
last_addr[i] = 0
i -= 1
@staticmethod
def _addr2stor(addr):
"""
Converts internal addr to storable/hashable address
@param addr: internal address [addr1, addr2, ...]
@return: storable address "addr1-addr2-..."
"""
out = ""
for value in addr:
if value is None:
out += '*-'
else:
out += '%s-' % value
if out:
return out[:-1]
else:
return "*"
def _dev2addr(self, device):
"""
Parse the internal address out of the device
@param device: QBaseDevice device
@return: internal address [addr1, addr2, ...]
"""
addr = []
for key in self.addr_items:
value = device.get_param(key)
if value is None:
addr.append(None)
else:
addr.append(int(value))
return addr
def _set_first_addr(self, addr_pattern):
"""
@param addr_pattern: Address pattern (full qualified or with Nones)
@return: first valid address based on addr_pattern
"""
use_reserved = True
if addr_pattern is None:
addr_pattern = [None] * len(self.addr_lengths)
# set first usable addr
last_addr = addr_pattern[:]
if None in last_addr: # Address is not fully specified
use_reserved = False # Use only free address
for i in xrange(len(last_addr)):
if last_addr[i] is None:
last_addr[i] = 0
return last_addr, use_reserved
def get_free_slot(self, addr_pattern):
"""
Finds unoccupied address
@param addr_pattern: Address pattern (full qualified or with Nones)
@return: First free address when found, (free or reserved for this dev)
None when no free address is found, (all occupied)
False in case of incorrect address (oor)
"""
# init
last_addr, use_reserved = self._set_first_addr(addr_pattern)
# Check the addr_pattern ranges
for i in xrange(len(self.addr_lengths)):
if last_addr[i] < 0 or last_addr[i] >= self.addr_lengths[i]:
return False
# Increment addr until free match is found
while last_addr is not False:
if self._addr2stor(last_addr) not in self.bus:
return last_addr
if (use_reserved and
self.bus[self._addr2stor(last_addr)] == "reserved"):
return last_addr
last_addr = self._increment_addr(addr_pattern, last_addr)
return None # No free matching address found
def _check_bus(self, device):
"""
Check, whether this device can be plugged into this bus.
@param device: QBaseDevice device
@return: True in case ids are correct, False when not
"""
if (device.get_param(self.bus_item) and
device.get_param(self.bus_item) != self.busid):
return False
else:
return True
def _set_device_props(self, device, addr):
"""
Set the full device address
@param device: QBaseDevice device
@param addr: internal address [addr1, addr2, ...]
"""
device.set_param(self.bus_item, self.busid)
for i in xrange(len(self.addr_items)):
device.set_param(self.addr_items[i], addr[i])
def _update_device_props(self, device, addr):
"""
Update values of previously set address items.
@param device: QBaseDevice device
@param addr: internal address [addr1, addr2, ...]
"""
if device.get_param(self.bus_item) is not None:
device.set_param(self.bus_item, self.busid)
for i in xrange(len(self.addr_items)):
if device.get_param(self.addr_items[i]) is not None:
device.set_param(self.addr_items[i], addr[i])
def insert(self, device, strict_mode=False, force=False):
"""
Insert device into this bus representation.
@param device: QBaseDevice device
@param strict_mode: Use strict mode (set optional params)
@param force: Force insert the device even when errs occurs
@return: True on success,
False when an incorrect addr/busid is set,
None when there is no free slot,
error string when force added device with errors.
"""
err = ""
if not self._check_bus(device):
if force:
err += "BusId, "
device.set_param(self.bus_item, self.busid)
else:
return False
try:
addr_pattern = self._dev2addr(device)
except (ValueError, LookupError):
if force:
err += "BasicAddress, "
addr_pattern = [None] * len(self.addr_items)
else:
return False
addr = self.get_free_slot(addr_pattern)
if addr is None:
if force:
if None in addr_pattern:
err += "NoFreeSlot, "
# Use last valid address for inserting the device
addr = [(_ - 1) for _ in self.addr_lengths]
self._insert_used(device, self._addr2stor(addr))
else: # used slot
err += "UsedSlot, "
addr = addr_pattern # It's fully specified addr
self._insert_used(device, self._addr2stor(addr))
else:
return None
elif addr is False:
if force:
addr = addr_pattern
err += "BadAddr(%s), " % addr
self._insert_oor(device, self._addr2stor(addr))
else:
return False
else:
self._insert_good(device, self._addr2stor(addr))
if strict_mode: # Set full address in strict_mode
self._set_device_props(device, addr)
else:
self._update_device_props(device, addr)
if err:
# Device was force added with errors
err = ("Force adding device %s into %s (errors: %s)"
% (device, self, err[:-2]))
return err
return True
def _insert_good(self, device, addr):
"""
Insert device into good bus
@param device: QBaseDevice device
@param addr: internal address [addr1, addr2, ...]
"""
self.bus[addr] = device
def _insert_oor(self, device, addr):
"""
Insert device into bad bus as out-of-range (o)
@param device: QBaseDevice device
@param addr: storable address "addr1-addr2-..."
"""
addr = "o" + addr
if addr in self.badbus:
i = 2
while "%s(%dx)" % (addr, i) in self.badbus:
i += 1
addr = "%s(%dx)" % (addr, i)
self.badbus[addr] = device
def _insert_used(self, device, addr):
"""
Insert device into bad bus because address is already used
@param device: QBaseDevice device
@param addr: storable address "addr1-addr2-..."
"""
i = 2
while "%s(%dx)" % (addr, i) in self.badbus:
i += 1
self.badbus["%s(%dx)" % (addr, i)] = device
def remove(self, device):
"""
Remove device from this bus
@param device: QBaseDevice device
@return: True when removed, False when the device wasn't found
"""
if not self._remove_good(device):
return self._remove_bad(device)
return True
def _remove_good(self, device):
"""
Remove device from the good bus
@param device: QBaseDevice device
@return: True when removed, False when the device wasn't found
"""
if device in self.bus.itervalues():
remove = None
for key, item in self.bus.iteritems():
if item is device:
remove = key
break
if remove:
del(self.bus[remove])
return True
return False
def _remove_bad(self, device):
"""
Remove device from the bad bus
@param device: QBaseDevice device
@return: True when removed, False when the device wasn't found
"""
if device in self.badbus.itervalues():
remove = None
for key, item in self.badbus.iteritems():
if item is device:
remove = key
break
if remove:
del(self.badbus[remove])
return True
return False
class QDenseBus(QSparseBus):
"""
Dense bus representation. The only difference from SparseBus is the output
string format. DenseBus iterates over all addresses and show free slots
too. SparseBus on the other hand prints always the device address.
"""
def _str_devices_long(self):
""" Show all addresses even when they are unused """
out = ""
addr_pattern = [None] * len(self.addr_items)
addr = self._set_first_addr(addr_pattern)[0]
while addr:
dev = self.bus.get(self._addr2stor(addr))
out += '%s< %4s >%s\n ' % ('-' * 15, self._addr2stor(addr),
'-' * 15)
if hasattr(dev, 'str_long'):
out += dev.str_long().replace('\n', '\n ')
out = out[:-3]
elif isinstance(dev, str):
out += '"%s"' % dev
else:
out += "%s" % dev
out += '\n'
addr = self._increment_addr(addr_pattern, addr)
return out
def _str_bad_devices_long(self):
""" Show all addresses even when they are unused """
out = ""
for addr, dev in self.badbus.iteritems():
out += '%s< %4s >%s\n ' % ('-' * 15, addr,
'-' * 15)
if isinstance(dev, str):
out += '"%s"\n ' % dev
else:
out += dev.str_long().replace('\n', '\n ')
out = out[:-3]
out += '\n'
return out
def _str_devices(self):
""" Show all addresses even when they are unused, don't print addr """
out = '['
addr_pattern = [None] * len(self.addr_items)
addr = self._set_first_addr(addr_pattern)[0]
while addr:
out += "%s," % self.bus.get(self._addr2stor(addr))
addr = self._increment_addr(addr_pattern, addr)
if out[-1] == ',':
out = out[:-1]
return out + ']'
def _str_bad_devices(self):
""" Show all addresses even when they are unused """
out = '{'
for addr in sorted(self.badbus.keys()):
out += "%s:" % addr
out += "%s," % self.badbus[addr]
if out[-1] == ',':
out = out[:-1]
return out + '}'
class QPCIBus(QDenseBus):
"""
PCI Bus representation (bus&addr, uses hex digits)
"""
def __init__(self, busid, bus_type, aobject=None):
""" bus&addr, 32 slots """
super(QPCIBus, self).__init__('bus', [['addr'], [32]], busid, bus_type,
aobject)
@staticmethod
def _addr2stor(addr):
""" force all items as hexadecimal values """
out = ""
for value in addr:
if value is None:
out += '*-'
else:
out += '%s-' % hex(value)
if out:
return out[:-1]
else:
return "*"
def _dev2addr(self, device):
""" Read the values in base of 16 (hex) """
addr = []
for key in self.addr_items:
value = device.get_param(key)
if value is None:
addr.append(None)
elif isinstance(value, int):
addr.append(value)
else:
addr.append(int(value, 16))
return addr
def _set_device_props(self, device, addr):
""" Convert addr to hex """
addr = [hex(_) for _ in addr]
super(QPCIBus, self)._set_device_props(device, addr)
def _update_device_props(self, device, addr):
""" Convert addr to hex """
addr = [hex(_) for _ in addr]
super(QPCIBus, self)._update_device_props(device, addr)
###############################################################################
# Device container (device representation of VM)
# This class represents VM by storing all devices and their connections (buses)
###############################################################################
class DevContainer(object):
"""
Device container class
"""
# General methods
def __init__(self, qemu_binary, vmname, strict_mode=False):
"""
@param qemu_binary: qemu binary
@param vm: related VM
@param strict_mode: Use strict mode (set optional params)
"""
def get_hmp_cmds(qemu_binary):
""" @return: list of human monitor commands """
_ = utils.system_output("echo -e 'help\nquit' | %s -monitor "
"stdio -vnc none" % qemu_binary,
timeout=10, ignore_status=True,
verbose=False)
_ = re.findall(r'^([^\| \[\n]+\|?\w+)', _, re.M)
hmp_cmds = []
for cmd in _:
if '|' not in cmd:
if cmd != 'The':
hmp_cmds.append(cmd)
else:
hmp_cmds.extend(cmd.split('|'))
return hmp_cmds
def get_qmp_cmds(qemu_binary):
""" @return: list of qmp commands """
cmds = utils.system_output('echo -e \''
'{ "execute": "qmp_capabilities" }\n'
'{ "execute": "query-commands", "id": "RAND91" }\n'
'{ "execute": "quit" }\''
'| %s -qmp stdio -vnc none | grep return |'
' grep RAND91' % qemu_binary, timeout=10,
ignore_status=True, verbose=False).splitlines()
if not cmds:
# Some qemu versions crashes when qmp used too early; add sleep
cmds = utils.system_output('echo -e \''
'{ "execute": "qmp_capabilities" }\n'
'{ "execute": "query-commands", "id": "RAND91" }\n'
'{ "execute": "quit" }\' | (sleep 1; cat )'
'| %s -qmp stdio -vnc none | grep return |'
' grep RAND91' % qemu_binary, timeout=10,
ignore_status=True, verbose=False).splitlines()
if cmds:
cmds = re.findall(r'{\s*"name"\s*:\s*"([^"]+)"\s*}', cmds[0])
if cmds: # If no mathes, return None
return cmds
self.__state = 0 # is representation sync with VM (0 = synchronized)
self.__qemu_help = utils.system_output("%s -help" % qemu_binary,
timeout=10, ignore_status=True, verbose=False)
self.__device_help = utils.system_output("%s -device ? 2>&1"
% qemu_binary, timeout=10,
ignore_status=True, verbose=False)
self.__machine_types = utils.system_output("%s -M ?" % qemu_binary,
timeout=10, ignore_status=True, verbose=False)
self.__hmp_cmds = get_hmp_cmds(qemu_binary)
self.__qmp_cmds = get_qmp_cmds(qemu_binary)
self.vmname = vmname
self.strict_mode = strict_mode == 'yes'
self.__devices = []
self.__buses = []
def __getitem__(self, item):
"""
@param item: autotest id or QObject-like object
@return: First matching object defined in this QDevContainer
@raise KeyError: In case no match was found
"""
if isinstance(item, QBaseDevice):
if item in self.__devices:
return item
elif item:
for device in self.__devices:
if device.get_aid() == item:
return device
raise KeyError("Device %s is not in %s" % (item, self))
def get(self, item):
"""
@param item: autotest id or QObject-like object
@return: First matching object defined in this QDevContainer or None
"""
if item in self:
return self[item]
def __delitem__(self, item):
"""
Delete specified item from devices list
@param item: autotest id or QObject-like object
@raise KeyError: In case no match was found
"""
# Remove child_buses including devices
if self.remove(item):
raise KeyError(item)
def remove(self, item):
"""
Remove device from this representation
@param item: autotest id or QObject-like object
@return: None on success, -1 when the device is not present
"""
# Remove child_buses including devices
item = self.get(item)
if item is None:
return -1
for bus in item.child_bus:
remove = [dev for dev in bus]
for dev in remove:
del(self[dev])
self.__buses.remove(bus)
# Remove from parent_buses
for bus in self.__buses:
if item in bus:
del(bus[item])
# Remove from list of devices
self.__devices.remove(self[item])
def __len__(self):
""" @return: Number of inserted devices """
return len(self.__devices)
def __contains__(self, item):
"""
Is specified item defined in current devices list?
@param item: autotest id or QObject-like object
@return: True - yes, False - no
"""
if isinstance(item, QBaseDevice):
if item in self.__devices:
return True
elif item:
for device in self.__devices:
if device.get_aid() == item:
return True
return False