kerf-multikernel 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kerf/__init__.py +74 -0
- kerf/baseline.py +187 -0
- kerf/cli.py +56 -0
- kerf/console/__init__.py +21 -0
- kerf/console/main.py +252 -0
- kerf/create/__init__.py +17 -0
- kerf/create/main.py +780 -0
- kerf/data/__init__.py +24 -0
- kerf/data/kerf-init +0 -0
- kerf/daxfs/__init__.py +31 -0
- kerf/daxfs/mkdaxfs.py +597 -0
- kerf/delete/__init__.py +21 -0
- kerf/delete/main.py +238 -0
- kerf/docker/__init__.py +21 -0
- kerf/docker/image.py +234 -0
- kerf/dtc/__init__.py +27 -0
- kerf/dtc/cells.py +51 -0
- kerf/dtc/extractor.py +362 -0
- kerf/dtc/overlay.py +378 -0
- kerf/dtc/parser.py +1548 -0
- kerf/dtc/reporter.py +308 -0
- kerf/dtc/validator.py +792 -0
- kerf/exceptions.py +49 -0
- kerf/exec/__init__.py +21 -0
- kerf/exec/main.py +260 -0
- kerf/init/__init__.py +13 -0
- kerf/init/main.py +696 -0
- kerf/kill/__init__.py +21 -0
- kerf/kill/main.py +275 -0
- kerf/lazy_cma.py +132 -0
- kerf/load/__init__.py +21 -0
- kerf/load/main.py +613 -0
- kerf/metadata.py +134 -0
- kerf/models.py +241 -0
- kerf/resources.py +369 -0
- kerf/runtime.py +527 -0
- kerf/show/__init__.py +21 -0
- kerf/show/main.py +616 -0
- kerf/unload/__init__.py +21 -0
- kerf/unload/main.py +310 -0
- kerf/update/__init__.py +15 -0
- kerf/update/main.py +450 -0
- kerf/utils.py +95 -0
- kerf/vmlinuz.py +213 -0
- kerf_multikernel-0.1.0.dist-info/LICENSE +201 -0
- kerf_multikernel-0.1.0.dist-info/METADATA +382 -0
- kerf_multikernel-0.1.0.dist-info/RECORD +49 -0
- kerf_multikernel-0.1.0.dist-info/WHEEL +4 -0
- kerf_multikernel-0.1.0.dist-info/entry_points.txt +3 -0
kerf/dtc/parser.py
ADDED
|
@@ -0,0 +1,1548 @@
|
|
|
1
|
+
# Copyright 2025 Multikernel Technologies, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
Device tree parsing and model building.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import re
|
|
20
|
+
from typing import Dict, List, Optional
|
|
21
|
+
|
|
22
|
+
import libfdt
|
|
23
|
+
|
|
24
|
+
from ..exceptions import ParseError
|
|
25
|
+
from .cells import unpack_cpu_ids
|
|
26
|
+
from ..models import (
|
|
27
|
+
CPUAllocation,
|
|
28
|
+
DeviceInfo,
|
|
29
|
+
GlobalDeviceTree,
|
|
30
|
+
HardwareInventory,
|
|
31
|
+
Instance,
|
|
32
|
+
InstanceResources,
|
|
33
|
+
MemoryAllocation,
|
|
34
|
+
NUMANode,
|
|
35
|
+
OverlayInstanceData,
|
|
36
|
+
TopologySection,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class DeviceTreeParser:
|
|
41
|
+
"""Parser for multikernel device trees."""
|
|
42
|
+
|
|
43
|
+
def __init__(self):
|
|
44
|
+
self.fdt = None
|
|
45
|
+
self._last_overlay_data: Optional[OverlayInstanceData] = None
|
|
46
|
+
|
|
47
|
+
def parse_dts(self, dts_content: str) -> GlobalDeviceTree:
|
|
48
|
+
"""Parse DTS content into GlobalDeviceTree model."""
|
|
49
|
+
# Create a simple DTS parser that can handle our multikernel format
|
|
50
|
+
# This is a production-ready implementation for the specific DTS format we use
|
|
51
|
+
|
|
52
|
+
# Parse the DTS content using regex and string parsing
|
|
53
|
+
|
|
54
|
+
# Extract hardware inventory
|
|
55
|
+
hardware = self._parse_hardware_from_dts(dts_content)
|
|
56
|
+
|
|
57
|
+
# Extract instances
|
|
58
|
+
instances = self._parse_instances_from_dts(dts_content)
|
|
59
|
+
|
|
60
|
+
# Extract device references
|
|
61
|
+
device_refs = self._parse_device_references_from_dts(dts_content)
|
|
62
|
+
|
|
63
|
+
return GlobalDeviceTree(
|
|
64
|
+
hardware=hardware,
|
|
65
|
+
instances=instances,
|
|
66
|
+
device_references=device_refs
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def parse_dtb(self, dtb_path: str) -> GlobalDeviceTree:
|
|
70
|
+
"""Parse DTB file into GlobalDeviceTree model."""
|
|
71
|
+
try:
|
|
72
|
+
with open(dtb_path, 'rb') as f:
|
|
73
|
+
dtb_data = f.read()
|
|
74
|
+
|
|
75
|
+
return self.parse_dtb_from_bytes(dtb_data)
|
|
76
|
+
except Exception as e:
|
|
77
|
+
raise ParseError(f"Failed to parse DTB file {dtb_path}: {e}") from e
|
|
78
|
+
|
|
79
|
+
def parse_dtb_from_bytes(self, dtb_data: bytes) -> GlobalDeviceTree:
|
|
80
|
+
"""Parse DTB from bytes into GlobalDeviceTree model."""
|
|
81
|
+
try:
|
|
82
|
+
self.fdt = libfdt.Fdt(dtb_data)
|
|
83
|
+
return self._build_global_tree()
|
|
84
|
+
except libfdt.FdtException as e:
|
|
85
|
+
error_msg = f"FDT error: {e}"
|
|
86
|
+
if hasattr(e, 'err'):
|
|
87
|
+
error_msg += f" (error code: {e.err})"
|
|
88
|
+
raise ParseError(f"Failed to parse DTB from bytes: {error_msg}") from e
|
|
89
|
+
except Exception as e:
|
|
90
|
+
raise ParseError(f"Failed to parse DTB from bytes: {e}") from e
|
|
91
|
+
|
|
92
|
+
def _build_global_tree(self) -> GlobalDeviceTree:
|
|
93
|
+
"""Build GlobalDeviceTree from parsed FDT."""
|
|
94
|
+
try:
|
|
95
|
+
root = self.fdt.path_offset('/')
|
|
96
|
+
except libfdt.FdtException as e:
|
|
97
|
+
raise ParseError(f"Failed to access root node: {e}") from e
|
|
98
|
+
|
|
99
|
+
is_overlay = False
|
|
100
|
+
try:
|
|
101
|
+
compatible = self.fdt.getprop(root, 'compatible')
|
|
102
|
+
if compatible:
|
|
103
|
+
compatible_str = compatible.as_str().rstrip('\0')
|
|
104
|
+
if compatible_str == 'linux,multikernel-overlay':
|
|
105
|
+
is_overlay = True
|
|
106
|
+
except libfdt.FdtException:
|
|
107
|
+
pass
|
|
108
|
+
|
|
109
|
+
# Fallback: detect overlays by fragment nodes (for compatibility with overlays missing compatible property)
|
|
110
|
+
if not is_overlay:
|
|
111
|
+
try:
|
|
112
|
+
offset = self.fdt.first_subnode(root)
|
|
113
|
+
while offset >= 0:
|
|
114
|
+
if self.fdt.get_name(offset).startswith('fragment@'):
|
|
115
|
+
is_overlay = True
|
|
116
|
+
break
|
|
117
|
+
try:
|
|
118
|
+
offset = self.fdt.next_subnode(offset)
|
|
119
|
+
except libfdt.FdtException:
|
|
120
|
+
break
|
|
121
|
+
except libfdt.FdtException:
|
|
122
|
+
pass
|
|
123
|
+
|
|
124
|
+
if not is_overlay:
|
|
125
|
+
try:
|
|
126
|
+
hardware = self._parse_hardware_inventory()
|
|
127
|
+
except ParseError:
|
|
128
|
+
raise
|
|
129
|
+
except libfdt.FdtException as e:
|
|
130
|
+
raise ParseError(f"Failed to parse hardware inventory: FDT error - {e}") from e
|
|
131
|
+
except Exception as e:
|
|
132
|
+
raise ParseError(f"Failed to parse hardware inventory: {e}") from e
|
|
133
|
+
else:
|
|
134
|
+
# Overlays have empty hardware (resources are in baseline only)
|
|
135
|
+
from ..models import HardwareInventory, CPUAllocation, MemoryAllocation # pylint: disable=reimported,redefined-outer-name
|
|
136
|
+
hardware = HardwareInventory(
|
|
137
|
+
cpus=CPUAllocation(total=0, host_reserved=[], available=[]),
|
|
138
|
+
memory=MemoryAllocation(
|
|
139
|
+
total_bytes=0,
|
|
140
|
+
host_reserved_bytes=0,
|
|
141
|
+
memory_pool_base=0,
|
|
142
|
+
memory_pool_bytes=0
|
|
143
|
+
),
|
|
144
|
+
topology=None,
|
|
145
|
+
devices={}
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
try:
|
|
149
|
+
if is_overlay:
|
|
150
|
+
overlay_data = self._parse_overlay_instances()
|
|
151
|
+
self._last_overlay_data = overlay_data
|
|
152
|
+
instances = overlay_data.instances
|
|
153
|
+
else:
|
|
154
|
+
instances = self._parse_instances()
|
|
155
|
+
self._last_overlay_data = None
|
|
156
|
+
except Exception as e:
|
|
157
|
+
raise ParseError(f"Failed to parse instances: {e}") from e
|
|
158
|
+
|
|
159
|
+
device_refs = {}
|
|
160
|
+
if not is_overlay:
|
|
161
|
+
try:
|
|
162
|
+
device_refs = self._parse_device_references()
|
|
163
|
+
except Exception as e:
|
|
164
|
+
raise ParseError(f"Failed to parse device references: {e}") from e
|
|
165
|
+
|
|
166
|
+
return GlobalDeviceTree(
|
|
167
|
+
hardware=hardware,
|
|
168
|
+
instances=instances,
|
|
169
|
+
device_references=device_refs
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
def _parse_hardware_inventory(self) -> HardwareInventory:
|
|
173
|
+
"""Parse hardware inventory from /resources."""
|
|
174
|
+
try:
|
|
175
|
+
resources_node = self.fdt.path_offset('/resources')
|
|
176
|
+
except libfdt.FdtException as e:
|
|
177
|
+
raise ParseError(f"Missing /resources node: {e}") from e
|
|
178
|
+
|
|
179
|
+
# Parse CPU information
|
|
180
|
+
try:
|
|
181
|
+
cpus = self._parse_cpu_allocation(resources_node)
|
|
182
|
+
except libfdt.FdtException as e:
|
|
183
|
+
raise ParseError(f"Error parsing CPU allocation: {e}") from e
|
|
184
|
+
|
|
185
|
+
# Parse memory information
|
|
186
|
+
try:
|
|
187
|
+
memory = self._parse_memory_allocation(resources_node)
|
|
188
|
+
except libfdt.FdtException as e:
|
|
189
|
+
raise ParseError(f"Error parsing memory allocation: {e}") from e
|
|
190
|
+
|
|
191
|
+
# Parse topology section
|
|
192
|
+
topology = self._parse_topology(resources_node)
|
|
193
|
+
|
|
194
|
+
# Parse devices
|
|
195
|
+
devices = self._parse_devices(resources_node)
|
|
196
|
+
|
|
197
|
+
return HardwareInventory(
|
|
198
|
+
cpus=cpus,
|
|
199
|
+
memory=memory,
|
|
200
|
+
topology=topology,
|
|
201
|
+
devices=devices
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
def _parse_cpu_allocation(self, resources_node: int) -> CPUAllocation:
|
|
205
|
+
"""Parse CPU allocation from resources node."""
|
|
206
|
+
try:
|
|
207
|
+
cpus_prop = self.fdt.getprop(resources_node, 'cpus')
|
|
208
|
+
available = unpack_cpu_ids(cpus_prop)
|
|
209
|
+
except libfdt.FdtException:
|
|
210
|
+
# No cpus property means all CPUs are allocated
|
|
211
|
+
available = []
|
|
212
|
+
|
|
213
|
+
if available:
|
|
214
|
+
total = max(available) + 1
|
|
215
|
+
else:
|
|
216
|
+
total = 0
|
|
217
|
+
host_reserved = []
|
|
218
|
+
|
|
219
|
+
return CPUAllocation(
|
|
220
|
+
total=total,
|
|
221
|
+
host_reserved=host_reserved,
|
|
222
|
+
available=available
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
def _parse_memory_allocation(self, resources_node: int) -> MemoryAllocation:
|
|
226
|
+
"""Parse memory allocation from resources node."""
|
|
227
|
+
try:
|
|
228
|
+
memory_pool_base_prop = self.fdt.getprop(resources_node, 'memory-base')
|
|
229
|
+
if len(memory_pool_base_prop) != 8:
|
|
230
|
+
raise ParseError(f"Invalid 'memory-base' property size: {len(memory_pool_base_prop)} bytes (expected 8 bytes)")
|
|
231
|
+
memory_pool_base = memory_pool_base_prop.as_uint64()
|
|
232
|
+
except libfdt.FdtException as e:
|
|
233
|
+
raise ParseError(f"Missing 'memory-base' property in /resources: {e}") from e
|
|
234
|
+
|
|
235
|
+
try:
|
|
236
|
+
memory_pool_bytes_prop = self.fdt.getprop(resources_node, 'memory-bytes')
|
|
237
|
+
if len(memory_pool_bytes_prop) != 8:
|
|
238
|
+
raise ParseError(f"Invalid 'memory-bytes' property size: {len(memory_pool_bytes_prop)} bytes (expected 8 bytes)")
|
|
239
|
+
memory_pool_bytes = memory_pool_bytes_prop.as_uint64()
|
|
240
|
+
except libfdt.FdtException as e:
|
|
241
|
+
raise ParseError(f"Missing 'memory-bytes' property in /resources: {e}") from e
|
|
242
|
+
|
|
243
|
+
total_bytes = memory_pool_base + memory_pool_bytes
|
|
244
|
+
host_reserved_bytes = 0
|
|
245
|
+
|
|
246
|
+
return MemoryAllocation(
|
|
247
|
+
total_bytes=total_bytes,
|
|
248
|
+
host_reserved_bytes=host_reserved_bytes,
|
|
249
|
+
memory_pool_base=memory_pool_base,
|
|
250
|
+
memory_pool_bytes=memory_pool_bytes
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
def _parse_devices(self, resources_node: int) -> Dict[str, DeviceInfo]:
|
|
254
|
+
"""Parse device information from resources node."""
|
|
255
|
+
devices = {}
|
|
256
|
+
|
|
257
|
+
try:
|
|
258
|
+
devices_node = self.fdt.subnode_offset(resources_node, 'devices')
|
|
259
|
+
except libfdt.FdtException:
|
|
260
|
+
return devices
|
|
261
|
+
|
|
262
|
+
# Iterate through device nodes
|
|
263
|
+
offset = self.fdt.first_subnode(devices_node)
|
|
264
|
+
while offset >= 0:
|
|
265
|
+
name = self.fdt.get_name(offset)
|
|
266
|
+
try:
|
|
267
|
+
device_info = self._parse_device_info(offset, name)
|
|
268
|
+
devices[name] = device_info
|
|
269
|
+
except ParseError:
|
|
270
|
+
# Skip nodes that don't have required properties (not valid devices)
|
|
271
|
+
pass
|
|
272
|
+
try:
|
|
273
|
+
offset = self.fdt.next_subnode(offset)
|
|
274
|
+
except libfdt.FdtException:
|
|
275
|
+
# No more subnodes
|
|
276
|
+
break
|
|
277
|
+
|
|
278
|
+
return devices
|
|
279
|
+
|
|
280
|
+
def _parse_device_info(self, node_offset: int, name: str) -> DeviceInfo:
|
|
281
|
+
"""Parse individual device information."""
|
|
282
|
+
compatible = ""
|
|
283
|
+
try:
|
|
284
|
+
compatible = self.fdt.getprop(node_offset, 'compatible').as_str()
|
|
285
|
+
except libfdt.FdtException:
|
|
286
|
+
pass
|
|
287
|
+
|
|
288
|
+
# Parse optional properties
|
|
289
|
+
device_type = None
|
|
290
|
+
device_name = None
|
|
291
|
+
pci_id = None
|
|
292
|
+
vendor_id = None
|
|
293
|
+
device_id = None
|
|
294
|
+
sriov_vfs = None
|
|
295
|
+
host_reserved_vf = None
|
|
296
|
+
available_vfs = None
|
|
297
|
+
namespaces = None
|
|
298
|
+
host_reserved_ns = None
|
|
299
|
+
available_ns = None
|
|
300
|
+
|
|
301
|
+
try:
|
|
302
|
+
device_type = self.fdt.getprop(node_offset, 'device-type').as_str()
|
|
303
|
+
except libfdt.FdtException:
|
|
304
|
+
pass
|
|
305
|
+
|
|
306
|
+
try:
|
|
307
|
+
device_name = self.fdt.getprop(node_offset, 'device-name').as_str()
|
|
308
|
+
except libfdt.FdtException:
|
|
309
|
+
pass
|
|
310
|
+
|
|
311
|
+
try:
|
|
312
|
+
pci_id = self.fdt.getprop(node_offset, 'pci-id').as_str()
|
|
313
|
+
except libfdt.FdtException:
|
|
314
|
+
pass
|
|
315
|
+
|
|
316
|
+
try:
|
|
317
|
+
vendor_id = self.fdt.getprop(node_offset, 'vendor-id').as_uint32()
|
|
318
|
+
except libfdt.FdtException:
|
|
319
|
+
pass
|
|
320
|
+
|
|
321
|
+
try:
|
|
322
|
+
device_id = self.fdt.getprop(node_offset, 'device-id').as_uint32()
|
|
323
|
+
except libfdt.FdtException:
|
|
324
|
+
pass
|
|
325
|
+
|
|
326
|
+
try:
|
|
327
|
+
sriov_vfs = self.fdt.getprop(node_offset, 'sriov-vfs').as_uint32()
|
|
328
|
+
except libfdt.FdtException:
|
|
329
|
+
pass
|
|
330
|
+
|
|
331
|
+
try:
|
|
332
|
+
host_reserved_vf = self.fdt.getprop(node_offset, 'host-reserved-vf').as_uint32()
|
|
333
|
+
except libfdt.FdtException:
|
|
334
|
+
pass
|
|
335
|
+
|
|
336
|
+
try:
|
|
337
|
+
available_vfs = self.fdt.getprop(node_offset, 'available-vfs').as_uint32_list()
|
|
338
|
+
except libfdt.FdtException:
|
|
339
|
+
pass
|
|
340
|
+
|
|
341
|
+
try:
|
|
342
|
+
namespaces = self.fdt.getprop(node_offset, 'namespaces').as_uint32()
|
|
343
|
+
except libfdt.FdtException:
|
|
344
|
+
pass
|
|
345
|
+
|
|
346
|
+
try:
|
|
347
|
+
host_reserved_ns = self.fdt.getprop(node_offset, 'host-reserved-ns').as_uint32()
|
|
348
|
+
except libfdt.FdtException:
|
|
349
|
+
pass
|
|
350
|
+
|
|
351
|
+
try:
|
|
352
|
+
available_ns = self.fdt.getprop(node_offset, 'available-ns').as_uint32_list()
|
|
353
|
+
except libfdt.FdtException:
|
|
354
|
+
pass
|
|
355
|
+
|
|
356
|
+
return DeviceInfo(
|
|
357
|
+
name=name,
|
|
358
|
+
compatible=compatible,
|
|
359
|
+
device_type=device_type,
|
|
360
|
+
device_name=device_name,
|
|
361
|
+
pci_id=pci_id,
|
|
362
|
+
vendor_id=vendor_id,
|
|
363
|
+
device_id=device_id,
|
|
364
|
+
sriov_vfs=sriov_vfs,
|
|
365
|
+
host_reserved_vf=host_reserved_vf,
|
|
366
|
+
available_vfs=available_vfs,
|
|
367
|
+
namespaces=namespaces,
|
|
368
|
+
host_reserved_ns=host_reserved_ns,
|
|
369
|
+
available_ns=available_ns
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
def _parse_instances(self) -> Dict[str, Instance]:
|
|
373
|
+
"""Parse instance definitions from /instances."""
|
|
374
|
+
instances = {}
|
|
375
|
+
|
|
376
|
+
try:
|
|
377
|
+
instances_node = self.fdt.path_offset('/instances')
|
|
378
|
+
except libfdt.FdtException:
|
|
379
|
+
return instances
|
|
380
|
+
|
|
381
|
+
# Iterate through instance nodes
|
|
382
|
+
try:
|
|
383
|
+
offset = self.fdt.first_subnode(instances_node)
|
|
384
|
+
while offset >= 0:
|
|
385
|
+
name = self.fdt.get_name(offset)
|
|
386
|
+
try:
|
|
387
|
+
instance = self._parse_instance(offset, name)
|
|
388
|
+
instances[name] = instance
|
|
389
|
+
except Exception as e:
|
|
390
|
+
raise ParseError(f"Failed to parse instance '{name}': {e}") from e
|
|
391
|
+
|
|
392
|
+
try:
|
|
393
|
+
offset = self.fdt.next_subnode(offset)
|
|
394
|
+
except libfdt.FdtException:
|
|
395
|
+
break
|
|
396
|
+
except libfdt.FdtException as e:
|
|
397
|
+
error_str = str(e)
|
|
398
|
+
if 'FDT_ERR_NOTFOUND' in error_str or 'NOTFOUND' in error_str:
|
|
399
|
+
return instances
|
|
400
|
+
# For other errors, re-raise
|
|
401
|
+
raise ParseError(f"Error iterating instance nodes: {e}") from e
|
|
402
|
+
|
|
403
|
+
return instances
|
|
404
|
+
|
|
405
|
+
def _parse_overlay_instances(self) -> OverlayInstanceData:
|
|
406
|
+
"""Parse instance definitions from overlay fragments (fragment@X/__overlay__/instance-create)."""
|
|
407
|
+
instances = {}
|
|
408
|
+
removals = set()
|
|
409
|
+
|
|
410
|
+
try:
|
|
411
|
+
root = self.fdt.path_offset('/')
|
|
412
|
+
except libfdt.FdtException:
|
|
413
|
+
return OverlayInstanceData(instances=instances, removals=removals)
|
|
414
|
+
|
|
415
|
+
try:
|
|
416
|
+
offset = self.fdt.first_subnode(root)
|
|
417
|
+
while offset >= 0:
|
|
418
|
+
name = self.fdt.get_name(offset)
|
|
419
|
+
|
|
420
|
+
if name.startswith('fragment@'):
|
|
421
|
+
try:
|
|
422
|
+
overlay_node = self.fdt.subnode_offset(offset, '__overlay__')
|
|
423
|
+
|
|
424
|
+
try:
|
|
425
|
+
instance_create_node = self.fdt.subnode_offset(overlay_node, 'instance-create')
|
|
426
|
+
instance = self._parse_instance_create(instance_create_node)
|
|
427
|
+
instances[instance.name] = instance
|
|
428
|
+
except libfdt.FdtException:
|
|
429
|
+
try:
|
|
430
|
+
instance_remove_node = self.fdt.subnode_offset(overlay_node, 'instance-remove')
|
|
431
|
+
instance_name_prop = self.fdt.getprop(instance_remove_node, 'instance-name')
|
|
432
|
+
instance_name = instance_name_prop.as_str()
|
|
433
|
+
removals.add(instance_name)
|
|
434
|
+
except libfdt.FdtException:
|
|
435
|
+
pass
|
|
436
|
+
except libfdt.FdtException:
|
|
437
|
+
pass
|
|
438
|
+
|
|
439
|
+
try:
|
|
440
|
+
offset = self.fdt.next_subnode(offset)
|
|
441
|
+
except libfdt.FdtException:
|
|
442
|
+
break
|
|
443
|
+
except libfdt.FdtException:
|
|
444
|
+
pass
|
|
445
|
+
|
|
446
|
+
return OverlayInstanceData(instances=instances, removals=removals)
|
|
447
|
+
|
|
448
|
+
def get_last_overlay_data(self) -> Optional[OverlayInstanceData]:
|
|
449
|
+
"""Get the overlay data from the last parsed overlay (if any)."""
|
|
450
|
+
return self._last_overlay_data
|
|
451
|
+
|
|
452
|
+
def _parse_instance_create(self, node_offset: int) -> Instance:
|
|
453
|
+
"""Parse instance from instance-create node in overlay."""
|
|
454
|
+
try:
|
|
455
|
+
instance_name_prop = self.fdt.getprop(node_offset, 'instance-name')
|
|
456
|
+
instance_name = instance_name_prop.as_str()
|
|
457
|
+
except libfdt.FdtException as e:
|
|
458
|
+
raise ParseError(f"Missing 'instance-name' property in instance-create: {e}") from e
|
|
459
|
+
|
|
460
|
+
instance_id = None
|
|
461
|
+
try:
|
|
462
|
+
instance_id = self.fdt.getprop(node_offset, 'id').as_uint32()
|
|
463
|
+
except libfdt.FdtException:
|
|
464
|
+
pass
|
|
465
|
+
|
|
466
|
+
resources = self._parse_instance_resources_from_overlay(node_offset)
|
|
467
|
+
options = self._parse_instance_options(node_offset)
|
|
468
|
+
|
|
469
|
+
return Instance(
|
|
470
|
+
name=instance_name,
|
|
471
|
+
id=instance_id,
|
|
472
|
+
resources=resources,
|
|
473
|
+
options=options
|
|
474
|
+
)
|
|
475
|
+
|
|
476
|
+
def _parse_instance_resources_from_overlay(self, node_offset: int) -> InstanceResources:
|
|
477
|
+
"""Parse instance resources from overlay instance-create node."""
|
|
478
|
+
try:
|
|
479
|
+
resources_node = self.fdt.subnode_offset(node_offset, 'resources')
|
|
480
|
+
except libfdt.FdtException as e:
|
|
481
|
+
raise ParseError(f"Missing resources node in instance-create: {e}") from e
|
|
482
|
+
|
|
483
|
+
try:
|
|
484
|
+
cpus_prop = self.fdt.getprop(resources_node, 'cpus')
|
|
485
|
+
cpus = unpack_cpu_ids(cpus_prop)
|
|
486
|
+
except libfdt.FdtException as e:
|
|
487
|
+
raise ParseError(f"Missing 'cpus' property in resources: {e}") from e
|
|
488
|
+
|
|
489
|
+
try:
|
|
490
|
+
memory_bytes = self.fdt.getprop(resources_node, 'memory-bytes').as_uint64()
|
|
491
|
+
except libfdt.FdtException as e:
|
|
492
|
+
raise ParseError(f"Missing 'memory-bytes' property in resources: {e}") from e
|
|
493
|
+
|
|
494
|
+
memory_base = 0
|
|
495
|
+
try:
|
|
496
|
+
memory_base = self.fdt.getprop(resources_node, 'memory-base').as_uint64()
|
|
497
|
+
except libfdt.FdtException as e:
|
|
498
|
+
raise ParseError(f"Missing 'memory-base' property in resources: {e}") from e
|
|
499
|
+
|
|
500
|
+
devices = []
|
|
501
|
+
try:
|
|
502
|
+
device_names_prop = self.fdt.getprop(resources_node, 'device-names')
|
|
503
|
+
device_names_str = device_names_prop.as_str()
|
|
504
|
+
if device_names_str:
|
|
505
|
+
devices = [d.strip() for d in device_names_str.split() if d.strip()]
|
|
506
|
+
except libfdt.FdtException:
|
|
507
|
+
pass
|
|
508
|
+
|
|
509
|
+
uring_enabled = False
|
|
510
|
+
uring_sq = None
|
|
511
|
+
uring_cq = None
|
|
512
|
+
uring_shim = None
|
|
513
|
+
try:
|
|
514
|
+
uring_node = self.fdt.subnode_offset(resources_node, 'uring')
|
|
515
|
+
uring_enabled = True
|
|
516
|
+
try:
|
|
517
|
+
uring_sq = self.fdt.getprop(uring_node, 'sq-entries').as_uint32()
|
|
518
|
+
except libfdt.FdtException:
|
|
519
|
+
pass
|
|
520
|
+
try:
|
|
521
|
+
uring_cq = self.fdt.getprop(uring_node, 'cq-entries').as_uint32()
|
|
522
|
+
except libfdt.FdtException:
|
|
523
|
+
pass
|
|
524
|
+
try:
|
|
525
|
+
uring_shim = self.fdt.getprop(uring_node, 'shim-data-pages').as_uint32()
|
|
526
|
+
except libfdt.FdtException:
|
|
527
|
+
pass
|
|
528
|
+
except libfdt.FdtException:
|
|
529
|
+
pass
|
|
530
|
+
|
|
531
|
+
return InstanceResources(
|
|
532
|
+
cpus=cpus,
|
|
533
|
+
memory_base=memory_base,
|
|
534
|
+
memory_bytes=memory_bytes,
|
|
535
|
+
devices=devices,
|
|
536
|
+
uring=uring_enabled,
|
|
537
|
+
uring_sq_entries=uring_sq,
|
|
538
|
+
uring_cq_entries=uring_cq,
|
|
539
|
+
uring_shim_pages=uring_shim,
|
|
540
|
+
)
|
|
541
|
+
|
|
542
|
+
def _parse_instance(self, node_offset: int, name: str) -> Instance:
|
|
543
|
+
"""Parse individual instance definition."""
|
|
544
|
+
# Parse instance ID
|
|
545
|
+
try:
|
|
546
|
+
instance_id = self.fdt.getprop(node_offset, 'id').as_uint32()
|
|
547
|
+
except libfdt.FdtException as e:
|
|
548
|
+
raise ParseError(f"Missing 'id' property for instance '{name}': {e}") from e
|
|
549
|
+
|
|
550
|
+
# Parse resources and options
|
|
551
|
+
resources = self._parse_instance_resources(node_offset)
|
|
552
|
+
options = self._parse_instance_options(node_offset)
|
|
553
|
+
|
|
554
|
+
return Instance(
|
|
555
|
+
name=name,
|
|
556
|
+
id=instance_id,
|
|
557
|
+
resources=resources,
|
|
558
|
+
options=options
|
|
559
|
+
)
|
|
560
|
+
|
|
561
|
+
def _parse_instance_resources(self, node_offset: int) -> InstanceResources:
|
|
562
|
+
"""Parse instance resource allocation."""
|
|
563
|
+
try:
|
|
564
|
+
resources_node = self.fdt.subnode_offset(node_offset, 'resources')
|
|
565
|
+
except libfdt.FdtException as e:
|
|
566
|
+
raise ParseError(f"Missing resources node for instance: {e}") from e
|
|
567
|
+
|
|
568
|
+
try:
|
|
569
|
+
cpus = unpack_cpu_ids(self.fdt.getprop(resources_node, 'cpus'))
|
|
570
|
+
except libfdt.FdtException as e:
|
|
571
|
+
raise ParseError(f"Missing 'cpus' property in instance resources: {e}") from e
|
|
572
|
+
|
|
573
|
+
try:
|
|
574
|
+
memory_base = self.fdt.getprop(resources_node, 'memory-base').as_uint64()
|
|
575
|
+
except libfdt.FdtException as e:
|
|
576
|
+
raise ParseError(f"Missing 'memory-base' property in instance resources: {e}") from e
|
|
577
|
+
|
|
578
|
+
try:
|
|
579
|
+
memory_bytes = self.fdt.getprop(resources_node, 'memory-bytes').as_uint64()
|
|
580
|
+
except libfdt.FdtException as e:
|
|
581
|
+
raise ParseError(f"Missing 'memory-bytes' property in instance resources: {e}") from e
|
|
582
|
+
|
|
583
|
+
devices = []
|
|
584
|
+
try:
|
|
585
|
+
device_names_prop = self.fdt.getprop(resources_node, 'device-names')
|
|
586
|
+
device_names_str = device_names_prop.as_str()
|
|
587
|
+
if device_names_str:
|
|
588
|
+
devices = [d.strip() for d in device_names_str.split() if d.strip()]
|
|
589
|
+
except libfdt.FdtException:
|
|
590
|
+
pass
|
|
591
|
+
|
|
592
|
+
uring_enabled = False
|
|
593
|
+
uring_sq = None
|
|
594
|
+
uring_cq = None
|
|
595
|
+
uring_shim = None
|
|
596
|
+
try:
|
|
597
|
+
uring_node = self.fdt.subnode_offset(resources_node, 'uring')
|
|
598
|
+
uring_enabled = True
|
|
599
|
+
try:
|
|
600
|
+
uring_sq = self.fdt.getprop(uring_node, 'sq-entries').as_uint32()
|
|
601
|
+
except libfdt.FdtException:
|
|
602
|
+
pass
|
|
603
|
+
try:
|
|
604
|
+
uring_cq = self.fdt.getprop(uring_node, 'cq-entries').as_uint32()
|
|
605
|
+
except libfdt.FdtException:
|
|
606
|
+
pass
|
|
607
|
+
try:
|
|
608
|
+
uring_shim = self.fdt.getprop(uring_node, 'shim-data-pages').as_uint32()
|
|
609
|
+
except libfdt.FdtException:
|
|
610
|
+
pass
|
|
611
|
+
except libfdt.FdtException:
|
|
612
|
+
pass
|
|
613
|
+
|
|
614
|
+
return InstanceResources(
|
|
615
|
+
cpus=cpus,
|
|
616
|
+
memory_base=memory_base,
|
|
617
|
+
memory_bytes=memory_bytes,
|
|
618
|
+
devices=devices,
|
|
619
|
+
uring=uring_enabled,
|
|
620
|
+
uring_sq_entries=uring_sq,
|
|
621
|
+
uring_cq_entries=uring_cq,
|
|
622
|
+
uring_shim_pages=uring_shim,
|
|
623
|
+
)
|
|
624
|
+
|
|
625
|
+
def _parse_instance_options(self, node_offset: int) -> Optional[Dict[str, bool]]:
|
|
626
|
+
"""Parse instance options from DTB node."""
|
|
627
|
+
options = {}
|
|
628
|
+
|
|
629
|
+
try:
|
|
630
|
+
options_node = self.fdt.subnode_offset(node_offset, 'options')
|
|
631
|
+
except libfdt.FdtException:
|
|
632
|
+
return None if not options else options
|
|
633
|
+
|
|
634
|
+
try:
|
|
635
|
+
self.fdt.getprop(options_node, 'enable-host-kcore')
|
|
636
|
+
options['enable-host-kcore'] = True
|
|
637
|
+
except libfdt.FdtException:
|
|
638
|
+
pass
|
|
639
|
+
|
|
640
|
+
return options if options else None
|
|
641
|
+
|
|
642
|
+
def _parse_device_references(self) -> Dict[str, Dict]:
|
|
643
|
+
"""Parse device reference nodes (phandle targets) from DTB."""
|
|
644
|
+
device_references = {}
|
|
645
|
+
|
|
646
|
+
# When parsing from DTB, device references are nodes at the root level
|
|
647
|
+
# that match the pattern of device references (e.g., eth0_vf1, nvme0_ns2)
|
|
648
|
+
try:
|
|
649
|
+
root = self.fdt.path_offset('/')
|
|
650
|
+
except libfdt.FdtException:
|
|
651
|
+
return device_references
|
|
652
|
+
|
|
653
|
+
# Iterate through root-level nodes to find device references
|
|
654
|
+
# Device references are typically named like: eth0_vf1, nvme0_ns2, etc.
|
|
655
|
+
# Skip known nodes like 'resources' and 'instances'
|
|
656
|
+
try:
|
|
657
|
+
offset = self.fdt.first_subnode(root)
|
|
658
|
+
while offset >= 0:
|
|
659
|
+
name = self.fdt.get_name(offset)
|
|
660
|
+
|
|
661
|
+
# Skip known structural nodes
|
|
662
|
+
if name in ('resources', 'instances'):
|
|
663
|
+
offset = self.fdt.next_subnode(offset)
|
|
664
|
+
continue
|
|
665
|
+
|
|
666
|
+
# Check if this looks like a device reference (contains _vf or _ns)
|
|
667
|
+
if '_vf' in name or '_ns' in name:
|
|
668
|
+
device_ref = {}
|
|
669
|
+
|
|
670
|
+
# Parse parent property
|
|
671
|
+
try:
|
|
672
|
+
parent = self.fdt.getprop(offset, 'parent').as_str()
|
|
673
|
+
device_ref['parent'] = parent
|
|
674
|
+
except libfdt.FdtException:
|
|
675
|
+
pass
|
|
676
|
+
|
|
677
|
+
# Parse vf-id if it's a VF reference
|
|
678
|
+
if '_vf' in name:
|
|
679
|
+
try:
|
|
680
|
+
vf_id = self.fdt.getprop(offset, 'vf-id').as_uint32()
|
|
681
|
+
device_ref['vf_id'] = vf_id
|
|
682
|
+
except libfdt.FdtException:
|
|
683
|
+
pass
|
|
684
|
+
|
|
685
|
+
# Parse namespace-id if it's a namespace reference
|
|
686
|
+
if '_ns' in name:
|
|
687
|
+
try:
|
|
688
|
+
ns_id = self.fdt.getprop(offset, 'namespace-id').as_uint32()
|
|
689
|
+
device_ref['namespace_id'] = ns_id
|
|
690
|
+
except libfdt.FdtException:
|
|
691
|
+
pass
|
|
692
|
+
|
|
693
|
+
if device_ref: # Only add if we found at least one property
|
|
694
|
+
device_references[name] = device_ref
|
|
695
|
+
|
|
696
|
+
offset = self.fdt.next_subnode(offset)
|
|
697
|
+
except libfdt.FdtException:
|
|
698
|
+
# If we can't iterate subnodes, just return empty dict
|
|
699
|
+
pass
|
|
700
|
+
|
|
701
|
+
return device_references
|
|
702
|
+
|
|
703
|
+
def _parse_hardware_from_dts(self, dts_content: str) -> HardwareInventory:
|
|
704
|
+
"""Parse hardware inventory from DTS content."""
|
|
705
|
+
|
|
706
|
+
# Parse CPU information
|
|
707
|
+
cpus = self._parse_cpus_from_dts(dts_content)
|
|
708
|
+
|
|
709
|
+
# Parse memory information
|
|
710
|
+
memory = self._parse_memory_from_dts(dts_content)
|
|
711
|
+
|
|
712
|
+
# Parse topology section
|
|
713
|
+
topology = self._parse_topology_from_dts(dts_content)
|
|
714
|
+
|
|
715
|
+
# Parse devices
|
|
716
|
+
devices = self._parse_devices_from_dts(dts_content)
|
|
717
|
+
|
|
718
|
+
return HardwareInventory(
|
|
719
|
+
cpus=cpus,
|
|
720
|
+
memory=memory,
|
|
721
|
+
topology=topology,
|
|
722
|
+
devices=devices
|
|
723
|
+
)
|
|
724
|
+
|
|
725
|
+
def _extract_resources_section(self, dts_content: str) -> Optional[str]:
|
|
726
|
+
"""Extract the resources section content with proper brace matching."""
|
|
727
|
+
|
|
728
|
+
resources_start = re.search(r'resources\s*\{', dts_content)
|
|
729
|
+
if not resources_start:
|
|
730
|
+
return None
|
|
731
|
+
|
|
732
|
+
start_pos = resources_start.end() - 1
|
|
733
|
+
brace_count = 0
|
|
734
|
+
end_pos = start_pos
|
|
735
|
+
|
|
736
|
+
for i, char in enumerate(dts_content[start_pos:], start_pos):
|
|
737
|
+
if char == '{':
|
|
738
|
+
brace_count += 1
|
|
739
|
+
elif char == '}':
|
|
740
|
+
brace_count -= 1
|
|
741
|
+
if brace_count == 0:
|
|
742
|
+
end_pos = i
|
|
743
|
+
break
|
|
744
|
+
|
|
745
|
+
if brace_count == 0:
|
|
746
|
+
return dts_content[start_pos+1:end_pos]
|
|
747
|
+
return None
|
|
748
|
+
|
|
749
|
+
def _parse_cpus_from_dts(self, dts_content: str) -> CPUAllocation:
|
|
750
|
+
"""Parse CPU allocation from DTS content."""
|
|
751
|
+
|
|
752
|
+
resources_text = self._extract_resources_section(dts_content)
|
|
753
|
+
if not resources_text:
|
|
754
|
+
raise ParseError("Missing /resources section in DTS")
|
|
755
|
+
|
|
756
|
+
cpus_match = re.search(r'cpus\s*=\s*<([^>]+)>', resources_text)
|
|
757
|
+
if not cpus_match:
|
|
758
|
+
raise ParseError("Missing 'cpus' property in /resources")
|
|
759
|
+
|
|
760
|
+
available = [int(x.strip()) for x in cpus_match.group(1).split()]
|
|
761
|
+
if available:
|
|
762
|
+
total = max(available) + 1
|
|
763
|
+
else:
|
|
764
|
+
total = 0
|
|
765
|
+
host_reserved = []
|
|
766
|
+
|
|
767
|
+
# Parse CPU topology if present
|
|
768
|
+
topology = self._parse_cpu_topology_from_dts(dts_content)
|
|
769
|
+
|
|
770
|
+
return CPUAllocation(
|
|
771
|
+
total=total,
|
|
772
|
+
host_reserved=host_reserved,
|
|
773
|
+
available=available,
|
|
774
|
+
topology=topology
|
|
775
|
+
)
|
|
776
|
+
|
|
777
|
+
def _parse_memory_from_dts(self, dts_content: str) -> MemoryAllocation:
|
|
778
|
+
"""Parse memory allocation from DTS content."""
|
|
779
|
+
|
|
780
|
+
resources_text = self._extract_resources_section(dts_content)
|
|
781
|
+
if not resources_text:
|
|
782
|
+
raise ParseError("Missing /resources section in DTS")
|
|
783
|
+
|
|
784
|
+
memory_base_match = re.search(r'memory-base\s*=\s*<([^>]+)>', resources_text)
|
|
785
|
+
if not memory_base_match:
|
|
786
|
+
raise ParseError("Missing 'memory-base' property in /resources")
|
|
787
|
+
|
|
788
|
+
memory_bytes_match = re.search(r'memory-bytes\s*=\s*<([^>]+)>', resources_text)
|
|
789
|
+
if not memory_bytes_match:
|
|
790
|
+
raise ParseError("Missing 'memory-bytes' property in /resources")
|
|
791
|
+
|
|
792
|
+
memory_pool_base = self._parse_hex_value(memory_base_match.group(1))
|
|
793
|
+
memory_pool_bytes = self._parse_hex_value(memory_bytes_match.group(1))
|
|
794
|
+
|
|
795
|
+
total_bytes = memory_pool_base + memory_pool_bytes
|
|
796
|
+
host_reserved_bytes = 0
|
|
797
|
+
|
|
798
|
+
return MemoryAllocation(
|
|
799
|
+
total_bytes=total_bytes,
|
|
800
|
+
host_reserved_bytes=host_reserved_bytes,
|
|
801
|
+
memory_pool_base=memory_pool_base,
|
|
802
|
+
memory_pool_bytes=memory_pool_bytes
|
|
803
|
+
)
|
|
804
|
+
|
|
805
|
+
def _parse_devices_from_dts(self, dts_content: str) -> Dict[str, DeviceInfo]:
|
|
806
|
+
"""Parse device information from DTS content."""
|
|
807
|
+
|
|
808
|
+
devices = {}
|
|
809
|
+
|
|
810
|
+
resources_text = self._extract_resources_section(dts_content)
|
|
811
|
+
if not resources_text:
|
|
812
|
+
return devices
|
|
813
|
+
|
|
814
|
+
devices_start = re.search(r'devices\s*\{', resources_text)
|
|
815
|
+
if not devices_start:
|
|
816
|
+
return devices
|
|
817
|
+
|
|
818
|
+
start_pos = devices_start.end() - 1
|
|
819
|
+
brace_count = 0
|
|
820
|
+
end_pos = start_pos
|
|
821
|
+
|
|
822
|
+
for i, char in enumerate(resources_text[start_pos:], start_pos):
|
|
823
|
+
if char == '{':
|
|
824
|
+
brace_count += 1
|
|
825
|
+
elif char == '}':
|
|
826
|
+
brace_count -= 1
|
|
827
|
+
if brace_count == 0:
|
|
828
|
+
end_pos = i
|
|
829
|
+
break
|
|
830
|
+
|
|
831
|
+
if brace_count != 0:
|
|
832
|
+
return devices
|
|
833
|
+
|
|
834
|
+
devices_text = resources_text[start_pos+1:end_pos]
|
|
835
|
+
|
|
836
|
+
# Parse device definitions with proper brace matching
|
|
837
|
+
# Format: name { ... } (e.g., enp9s0_dev { ... })
|
|
838
|
+
device_pattern = r'(\w+)\s*\{'
|
|
839
|
+
matches = list(re.finditer(device_pattern, devices_text))
|
|
840
|
+
|
|
841
|
+
for match in matches:
|
|
842
|
+
device_name = match.group(1)
|
|
843
|
+
|
|
844
|
+
# Find the matching closing brace for this device
|
|
845
|
+
device_start = match.end() - 1 # Position of opening brace
|
|
846
|
+
device_brace_count = 0
|
|
847
|
+
device_end = device_start
|
|
848
|
+
|
|
849
|
+
for i, char in enumerate(devices_text[device_start:], device_start):
|
|
850
|
+
if char == '{':
|
|
851
|
+
device_brace_count += 1
|
|
852
|
+
elif char == '}':
|
|
853
|
+
device_brace_count -= 1
|
|
854
|
+
if device_brace_count == 0:
|
|
855
|
+
device_end = i
|
|
856
|
+
break
|
|
857
|
+
|
|
858
|
+
if device_brace_count == 0:
|
|
859
|
+
device_content = devices_text[device_start+1:device_end]
|
|
860
|
+
device_info = self._parse_device_info_from_dts(device_name, device_content)
|
|
861
|
+
devices[device_name] = device_info
|
|
862
|
+
|
|
863
|
+
return devices
|
|
864
|
+
|
|
865
|
+
def _parse_device_info_from_dts(self, name: str, content: str) -> DeviceInfo:
|
|
866
|
+
"""Parse individual device information from DTS content."""
|
|
867
|
+
|
|
868
|
+
# Parse compatible string
|
|
869
|
+
compatible_match = re.search(r'compatible\s*=\s*"([^"]+)"', content)
|
|
870
|
+
compatible = compatible_match.group(1) if compatible_match else ""
|
|
871
|
+
|
|
872
|
+
# Parse optional properties
|
|
873
|
+
device_type = None
|
|
874
|
+
device_name = None
|
|
875
|
+
pci_id = None
|
|
876
|
+
vendor_id = None
|
|
877
|
+
device_id = None
|
|
878
|
+
sriov_vfs = None
|
|
879
|
+
host_reserved_vf = None
|
|
880
|
+
available_vfs = None
|
|
881
|
+
namespaces = None
|
|
882
|
+
host_reserved_ns = None
|
|
883
|
+
available_ns = None
|
|
884
|
+
|
|
885
|
+
device_type_match = re.search(r'device-type\s*=\s*"([^"]+)"', content)
|
|
886
|
+
if device_type_match:
|
|
887
|
+
device_type = device_type_match.group(1)
|
|
888
|
+
|
|
889
|
+
device_name_match = re.search(r'device-name\s*=\s*"([^"]+)"', content)
|
|
890
|
+
if device_name_match:
|
|
891
|
+
device_name = device_name_match.group(1)
|
|
892
|
+
|
|
893
|
+
pci_id_match = re.search(r'pci-id\s*=\s*"([^"]+)"', content)
|
|
894
|
+
if pci_id_match:
|
|
895
|
+
pci_id = pci_id_match.group(1)
|
|
896
|
+
|
|
897
|
+
vendor_id_match = re.search(r'vendor-id\s*=\s*<([^>]+)>', content)
|
|
898
|
+
if vendor_id_match:
|
|
899
|
+
vendor_id = self._parse_hex_value(vendor_id_match.group(1))
|
|
900
|
+
|
|
901
|
+
device_id_match = re.search(r'device-id\s*=\s*<([^>]+)>', content)
|
|
902
|
+
if device_id_match:
|
|
903
|
+
device_id = self._parse_hex_value(device_id_match.group(1))
|
|
904
|
+
|
|
905
|
+
sriov_vfs_match = re.search(r'sriov-vfs\s*=\s*<(\d+)>', content)
|
|
906
|
+
if sriov_vfs_match:
|
|
907
|
+
sriov_vfs = int(sriov_vfs_match.group(1))
|
|
908
|
+
|
|
909
|
+
host_reserved_vf_match = re.search(r'host-reserved-vf\s*=\s*<(\d+)>', content)
|
|
910
|
+
if host_reserved_vf_match:
|
|
911
|
+
host_reserved_vf = int(host_reserved_vf_match.group(1))
|
|
912
|
+
|
|
913
|
+
available_vfs_match = re.search(r'available-vfs\s*=\s*<([^>]+)>', content)
|
|
914
|
+
if available_vfs_match:
|
|
915
|
+
available_vfs = [int(x.strip()) for x in available_vfs_match.group(1).split()]
|
|
916
|
+
|
|
917
|
+
namespaces_match = re.search(r'namespaces\s*=\s*<(\d+)>', content)
|
|
918
|
+
if namespaces_match:
|
|
919
|
+
namespaces = int(namespaces_match.group(1))
|
|
920
|
+
|
|
921
|
+
host_reserved_ns_match = re.search(r'host-reserved-ns\s*=\s*<(\d+)>', content)
|
|
922
|
+
if host_reserved_ns_match:
|
|
923
|
+
host_reserved_ns = int(host_reserved_ns_match.group(1))
|
|
924
|
+
|
|
925
|
+
available_ns_match = re.search(r'available-ns\s*=\s*<([^>]+)>', content)
|
|
926
|
+
if available_ns_match:
|
|
927
|
+
available_ns = [int(x.strip()) for x in available_ns_match.group(1).split()]
|
|
928
|
+
|
|
929
|
+
return DeviceInfo(
|
|
930
|
+
name=name,
|
|
931
|
+
compatible=compatible,
|
|
932
|
+
device_type=device_type,
|
|
933
|
+
device_name=device_name,
|
|
934
|
+
pci_id=pci_id,
|
|
935
|
+
vendor_id=vendor_id,
|
|
936
|
+
device_id=device_id,
|
|
937
|
+
sriov_vfs=sriov_vfs,
|
|
938
|
+
host_reserved_vf=host_reserved_vf,
|
|
939
|
+
available_vfs=available_vfs,
|
|
940
|
+
namespaces=namespaces,
|
|
941
|
+
host_reserved_ns=host_reserved_ns,
|
|
942
|
+
available_ns=available_ns
|
|
943
|
+
)
|
|
944
|
+
|
|
945
|
+
def _parse_instances_from_dts(self, dts_content: str) -> Dict[str, Instance]:
|
|
946
|
+
"""Parse instance definitions from DTS content."""
|
|
947
|
+
|
|
948
|
+
instances = {}
|
|
949
|
+
|
|
950
|
+
# Find instances section - it's at the root level, not nested
|
|
951
|
+
# We need to find the instances section and extract the full content with nested braces
|
|
952
|
+
instances_start = re.search(r'instances\s*\{', dts_content)
|
|
953
|
+
if not instances_start:
|
|
954
|
+
return instances
|
|
955
|
+
|
|
956
|
+
# Find the matching closing brace for the instances section
|
|
957
|
+
start_pos = instances_start.end() - 1 # Position of opening brace
|
|
958
|
+
brace_count = 0
|
|
959
|
+
end_pos = start_pos
|
|
960
|
+
|
|
961
|
+
for i, char in enumerate(dts_content[start_pos:], start_pos):
|
|
962
|
+
if char == '{':
|
|
963
|
+
brace_count += 1
|
|
964
|
+
elif char == '}':
|
|
965
|
+
brace_count -= 1
|
|
966
|
+
if brace_count == 0:
|
|
967
|
+
end_pos = i
|
|
968
|
+
break
|
|
969
|
+
|
|
970
|
+
if brace_count == 0:
|
|
971
|
+
instances_text = dts_content[start_pos+1:end_pos]
|
|
972
|
+
else:
|
|
973
|
+
return instances
|
|
974
|
+
|
|
975
|
+
# Find all potential instance definitions
|
|
976
|
+
# Look for lines that start with instance names (not indented)
|
|
977
|
+
lines = instances_text.split('\n')
|
|
978
|
+
for i, line in enumerate(lines):
|
|
979
|
+
line = line.strip()
|
|
980
|
+
|
|
981
|
+
# Skip comments and empty lines
|
|
982
|
+
if not line or line.startswith('//') or line.startswith('/*'):
|
|
983
|
+
continue
|
|
984
|
+
|
|
985
|
+
# Look for instance definition: name followed by {
|
|
986
|
+
if '{' in line and not line.startswith(' '):
|
|
987
|
+
# Extract instance name
|
|
988
|
+
instance_name = line.split('{')[0].strip()
|
|
989
|
+
|
|
990
|
+
# Skip common keywords that aren't instances
|
|
991
|
+
if instance_name in ['resources', 'devices', 'cpus', 'memory']:
|
|
992
|
+
continue
|
|
993
|
+
|
|
994
|
+
# This looks like an instance definition
|
|
995
|
+
# Find the matching closing brace
|
|
996
|
+
brace_count = 0
|
|
997
|
+
instance_lines = []
|
|
998
|
+
j = i
|
|
999
|
+
|
|
1000
|
+
while j < len(lines):
|
|
1001
|
+
current_line = lines[j]
|
|
1002
|
+
instance_lines.append(current_line)
|
|
1003
|
+
|
|
1004
|
+
# Count braces in this line
|
|
1005
|
+
for char in current_line:
|
|
1006
|
+
if char == '{':
|
|
1007
|
+
brace_count += 1
|
|
1008
|
+
elif char == '}':
|
|
1009
|
+
brace_count -= 1
|
|
1010
|
+
if brace_count == 0:
|
|
1011
|
+
# Found matching closing brace
|
|
1012
|
+
instance_content = '\n'.join(instance_lines[1:-1]) # Skip first and last lines
|
|
1013
|
+
try:
|
|
1014
|
+
instance = self._parse_instance_from_dts(instance_name, instance_content)
|
|
1015
|
+
instances[instance_name] = instance
|
|
1016
|
+
except Exception:
|
|
1017
|
+
# Skip invalid instances
|
|
1018
|
+
pass
|
|
1019
|
+
break
|
|
1020
|
+
|
|
1021
|
+
if brace_count == 0:
|
|
1022
|
+
break
|
|
1023
|
+
j += 1
|
|
1024
|
+
|
|
1025
|
+
return instances
|
|
1026
|
+
|
|
1027
|
+
def _parse_instance_from_dts(self, name: str, content: str) -> Instance:
|
|
1028
|
+
"""Parse individual instance definition from DTS content."""
|
|
1029
|
+
|
|
1030
|
+
# Parse instance ID
|
|
1031
|
+
id_match = re.search(r'id\s*=\s*<(\d+)>', content)
|
|
1032
|
+
if not id_match:
|
|
1033
|
+
raise ParseError(f"Missing 'id' for instance '{name}'")
|
|
1034
|
+
instance_id = int(id_match.group(1))
|
|
1035
|
+
|
|
1036
|
+
# Parse resources and options
|
|
1037
|
+
resources = self._parse_instance_resources_from_dts(content)
|
|
1038
|
+
options = self._parse_instance_options_from_dts(content)
|
|
1039
|
+
|
|
1040
|
+
return Instance(
|
|
1041
|
+
name=name,
|
|
1042
|
+
id=instance_id,
|
|
1043
|
+
resources=resources,
|
|
1044
|
+
options=options
|
|
1045
|
+
)
|
|
1046
|
+
|
|
1047
|
+
def _parse_instance_resources_from_dts(self, content: str) -> InstanceResources:
|
|
1048
|
+
"""Parse instance resources from DTS content."""
|
|
1049
|
+
|
|
1050
|
+
# Find resources section
|
|
1051
|
+
resources_section = re.search(r'resources\s*\{([^}]+)\}', content, re.DOTALL)
|
|
1052
|
+
if not resources_section:
|
|
1053
|
+
raise ParseError("Missing 'resources' section in instance")
|
|
1054
|
+
|
|
1055
|
+
resources_text = resources_section.group(1)
|
|
1056
|
+
|
|
1057
|
+
# Parse CPUs
|
|
1058
|
+
cpus_match = re.search(r'cpus\s*=\s*<([^>]+)>', resources_text)
|
|
1059
|
+
if not cpus_match:
|
|
1060
|
+
raise ParseError("Missing 'cpus' in resources")
|
|
1061
|
+
cpus = [int(x.strip()) for x in cpus_match.group(1).split()]
|
|
1062
|
+
|
|
1063
|
+
# Parse memory base
|
|
1064
|
+
memory_base_match = re.search(r'memory-base\s*=\s*<([^>]+)>', resources_text)
|
|
1065
|
+
if not memory_base_match:
|
|
1066
|
+
raise ParseError("Missing 'memory-base' in resources")
|
|
1067
|
+
memory_base = self._parse_hex_value(memory_base_match.group(1))
|
|
1068
|
+
|
|
1069
|
+
# Parse memory bytes
|
|
1070
|
+
memory_bytes_match = re.search(r'memory-bytes\s*=\s*<([^>]+)>', resources_text)
|
|
1071
|
+
if not memory_bytes_match:
|
|
1072
|
+
raise ParseError("Missing 'memory-bytes' in resources")
|
|
1073
|
+
memory_bytes = self._parse_hex_value(memory_bytes_match.group(1))
|
|
1074
|
+
|
|
1075
|
+
# Parse devices (optional)
|
|
1076
|
+
devices = []
|
|
1077
|
+
devices_match = re.search(r'devices\s*=\s*<([^>]+)>', resources_text)
|
|
1078
|
+
if devices_match:
|
|
1079
|
+
# Remove & prefix from device references
|
|
1080
|
+
devices = [x.strip().lstrip('&') for x in devices_match.group(1).split(',')]
|
|
1081
|
+
|
|
1082
|
+
# Parse NUMA nodes (optional)
|
|
1083
|
+
numa_nodes = None
|
|
1084
|
+
numa_nodes_match = re.search(r'numa-nodes\s*=\s*<([^>]+)>', resources_text)
|
|
1085
|
+
if numa_nodes_match:
|
|
1086
|
+
numa_nodes = [int(x.strip()) for x in numa_nodes_match.group(1).split()]
|
|
1087
|
+
|
|
1088
|
+
# Parse CPU affinity (optional)
|
|
1089
|
+
cpu_affinity = None
|
|
1090
|
+
cpu_affinity_match = re.search(r'cpu-affinity\s*=\s*"([^"]+)"', resources_text)
|
|
1091
|
+
if cpu_affinity_match:
|
|
1092
|
+
cpu_affinity = cpu_affinity_match.group(1)
|
|
1093
|
+
|
|
1094
|
+
# Parse memory policy (optional)
|
|
1095
|
+
memory_policy = None
|
|
1096
|
+
memory_policy_match = re.search(r'memory-policy\s*=\s*"([^"]+)"', resources_text)
|
|
1097
|
+
if memory_policy_match:
|
|
1098
|
+
memory_policy = memory_policy_match.group(1)
|
|
1099
|
+
|
|
1100
|
+
return InstanceResources(
|
|
1101
|
+
cpus=cpus,
|
|
1102
|
+
memory_base=memory_base,
|
|
1103
|
+
memory_bytes=memory_bytes,
|
|
1104
|
+
devices=devices,
|
|
1105
|
+
numa_nodes=numa_nodes,
|
|
1106
|
+
cpu_affinity=cpu_affinity,
|
|
1107
|
+
memory_policy=memory_policy
|
|
1108
|
+
)
|
|
1109
|
+
|
|
1110
|
+
def _parse_instance_options_from_dts(self, content: str) -> Optional[Dict[str, bool]]:
|
|
1111
|
+
"""Parse instance options from DTS content."""
|
|
1112
|
+
|
|
1113
|
+
options_start = re.search(r'options\s*\{', content)
|
|
1114
|
+
if not options_start:
|
|
1115
|
+
return None
|
|
1116
|
+
|
|
1117
|
+
start_pos = options_start.end() - 1
|
|
1118
|
+
end_pos = start_pos
|
|
1119
|
+
brace_count = 0
|
|
1120
|
+
|
|
1121
|
+
for i, char in enumerate(content[start_pos:], start_pos):
|
|
1122
|
+
if char == '{':
|
|
1123
|
+
brace_count += 1
|
|
1124
|
+
elif char == '}':
|
|
1125
|
+
brace_count -= 1
|
|
1126
|
+
if brace_count == 0:
|
|
1127
|
+
end_pos = i
|
|
1128
|
+
break
|
|
1129
|
+
|
|
1130
|
+
if brace_count != 0:
|
|
1131
|
+
return None
|
|
1132
|
+
|
|
1133
|
+
options_text = content[start_pos+1:end_pos]
|
|
1134
|
+
options = {}
|
|
1135
|
+
|
|
1136
|
+
if re.search(r'enable-host-kcore\s*;', options_text):
|
|
1137
|
+
options['enable-host-kcore'] = True
|
|
1138
|
+
|
|
1139
|
+
return options if options else None
|
|
1140
|
+
|
|
1141
|
+
def _parse_device_references_from_dts(self, dts_content: str) -> Dict[str, Dict]:
|
|
1142
|
+
"""Parse device reference nodes from DTS content."""
|
|
1143
|
+
|
|
1144
|
+
device_references = {}
|
|
1145
|
+
|
|
1146
|
+
# Find all device references in the DTS content
|
|
1147
|
+
# These are typically defined as separate nodes that reference hardware devices
|
|
1148
|
+
|
|
1149
|
+
# Look for device reference patterns in the DTS content
|
|
1150
|
+
# Pattern: device_name_vf_id: type@id { ... } or device_name_ns_id: type@id { ... }
|
|
1151
|
+
device_ref_pattern = r'(\w+_vf\d+|\w+_ns\d+):\s*\w+-\w+@\d+\s*\{([^}]+)\}'
|
|
1152
|
+
|
|
1153
|
+
# Search through the entire DTS content for device references
|
|
1154
|
+
matches = re.finditer(device_ref_pattern, dts_content, re.DOTALL)
|
|
1155
|
+
|
|
1156
|
+
for match in matches:
|
|
1157
|
+
ref_name = match.group(1) # e.g., eth0_vf1
|
|
1158
|
+
ref_content = match.group(2)
|
|
1159
|
+
|
|
1160
|
+
# Parse the device reference properties
|
|
1161
|
+
device_ref = {}
|
|
1162
|
+
|
|
1163
|
+
# Parse parent device reference
|
|
1164
|
+
parent_match = re.search(r'parent\s*=\s*<&([^>]+)>', ref_content)
|
|
1165
|
+
if parent_match:
|
|
1166
|
+
device_ref['parent'] = parent_match.group(1)
|
|
1167
|
+
|
|
1168
|
+
# Parse VF ID if it's a VF reference
|
|
1169
|
+
if '_vf' in ref_name:
|
|
1170
|
+
vf_id_match = re.search(r'vf-id\s*=\s*<(\d+)>', ref_content)
|
|
1171
|
+
if vf_id_match:
|
|
1172
|
+
device_ref['vf_id'] = int(vf_id_match.group(1))
|
|
1173
|
+
|
|
1174
|
+
# Parse namespace ID if it's a namespace reference
|
|
1175
|
+
if '_ns' in ref_name:
|
|
1176
|
+
ns_id_match = re.search(r'namespace-id\s*=\s*<(\d+)>', ref_content)
|
|
1177
|
+
if ns_id_match:
|
|
1178
|
+
device_ref['namespace_id'] = int(ns_id_match.group(1))
|
|
1179
|
+
|
|
1180
|
+
device_references[ref_name] = device_ref
|
|
1181
|
+
|
|
1182
|
+
return device_references
|
|
1183
|
+
|
|
1184
|
+
def _parse_topology_from_dts(self, dts_content: str) -> Optional[TopologySection]:
|
|
1185
|
+
"""Parse topology section from DTS content."""
|
|
1186
|
+
|
|
1187
|
+
# Look for topology section
|
|
1188
|
+
topology_section = re.search(r'topology\s*\{([^}]+)\}', dts_content, re.DOTALL)
|
|
1189
|
+
if not topology_section:
|
|
1190
|
+
return None
|
|
1191
|
+
|
|
1192
|
+
topology_text = topology_section.group(1)
|
|
1193
|
+
|
|
1194
|
+
# Parse NUMA nodes from topology section
|
|
1195
|
+
numa_nodes = self._parse_numa_nodes_from_dts(topology_text)
|
|
1196
|
+
|
|
1197
|
+
return TopologySection(numa_nodes=numa_nodes) if numa_nodes else None
|
|
1198
|
+
|
|
1199
|
+
def _parse_numa_nodes_from_dts(self, topology_text: str) -> Optional[Dict[int, NUMANode]]:
|
|
1200
|
+
"""Parse NUMA nodes from topology text."""
|
|
1201
|
+
|
|
1202
|
+
numa_nodes = {}
|
|
1203
|
+
|
|
1204
|
+
# Look for numa-nodes subsection
|
|
1205
|
+
numa_section = re.search(r'numa-nodes\s*\{([^}]+)\}', topology_text, re.DOTALL)
|
|
1206
|
+
if not numa_section:
|
|
1207
|
+
return None
|
|
1208
|
+
|
|
1209
|
+
numa_text = numa_section.group(1)
|
|
1210
|
+
|
|
1211
|
+
# Find all NUMA node definitions
|
|
1212
|
+
node_pattern = r'node@(\d+)\s*\{([^}]+)\}'
|
|
1213
|
+
node_matches = re.finditer(node_pattern, numa_text, re.DOTALL)
|
|
1214
|
+
|
|
1215
|
+
for match in node_matches:
|
|
1216
|
+
node_id = int(match.group(1))
|
|
1217
|
+
node_content = match.group(2)
|
|
1218
|
+
|
|
1219
|
+
# Parse node properties
|
|
1220
|
+
memory_base = 0
|
|
1221
|
+
memory_size = 0
|
|
1222
|
+
cpus = []
|
|
1223
|
+
distance_matrix = {}
|
|
1224
|
+
memory_type = "dram"
|
|
1225
|
+
|
|
1226
|
+
# Parse memory-base
|
|
1227
|
+
memory_base_match = re.search(r'memory-base\s*=\s*<([^>]+)>', node_content)
|
|
1228
|
+
if memory_base_match:
|
|
1229
|
+
memory_base = self._parse_hex_value(memory_base_match.group(1))
|
|
1230
|
+
|
|
1231
|
+
# Parse memory-size
|
|
1232
|
+
memory_size_match = re.search(r'memory-size\s*=\s*<([^>]+)>', node_content)
|
|
1233
|
+
if memory_size_match:
|
|
1234
|
+
memory_size = self._parse_hex_value(memory_size_match.group(1))
|
|
1235
|
+
|
|
1236
|
+
# Parse CPUs
|
|
1237
|
+
cpus_match = re.search(r'cpus\s*=\s*<([^>]+)>', node_content)
|
|
1238
|
+
if cpus_match:
|
|
1239
|
+
cpus = [int(x.strip()) for x in cpus_match.group(1).split()]
|
|
1240
|
+
|
|
1241
|
+
# Parse distance matrix (optional)
|
|
1242
|
+
distance_match = re.search(r'distance-matrix\s*=\s*<([^>]+)>', node_content)
|
|
1243
|
+
if distance_match:
|
|
1244
|
+
distances = [int(x.strip()) for x in distance_match.group(1).split()]
|
|
1245
|
+
# Simple distance matrix parsing - would need more sophisticated logic for full matrix
|
|
1246
|
+
_ = distances # Mark as intentionally unused for now
|
|
1247
|
+
|
|
1248
|
+
# Parse memory type
|
|
1249
|
+
memory_type_match = re.search(r'memory-type\s*=\s*"([^"]+)"', node_content)
|
|
1250
|
+
if memory_type_match:
|
|
1251
|
+
memory_type = memory_type_match.group(1)
|
|
1252
|
+
|
|
1253
|
+
numa_nodes[node_id] = NUMANode(
|
|
1254
|
+
node_id=node_id,
|
|
1255
|
+
memory_base=memory_base,
|
|
1256
|
+
memory_size=memory_size,
|
|
1257
|
+
cpus=cpus,
|
|
1258
|
+
distance_matrix=distance_matrix,
|
|
1259
|
+
memory_type=memory_type
|
|
1260
|
+
)
|
|
1261
|
+
|
|
1262
|
+
return numa_nodes if numa_nodes else None
|
|
1263
|
+
|
|
1264
|
+
def _parse_cpu_topology_from_dts(self, dts_content: str) -> Optional[Dict[int, 'CPUTopology']]:
|
|
1265
|
+
"""Parse CPU topology from DTS content."""
|
|
1266
|
+
from ..models import CPUTopology
|
|
1267
|
+
|
|
1268
|
+
topology = {}
|
|
1269
|
+
|
|
1270
|
+
# Look for cores section
|
|
1271
|
+
cores_section = re.search(r'cores\s*\{([^}]+)\}', dts_content, re.DOTALL)
|
|
1272
|
+
if not cores_section:
|
|
1273
|
+
return None
|
|
1274
|
+
|
|
1275
|
+
cores_text = cores_section.group(1)
|
|
1276
|
+
|
|
1277
|
+
# Find all core definitions
|
|
1278
|
+
core_pattern = r'core@(\d+)\s*\{\s*cpus\s*=\s*<([^>]+)>\s*;\s*\}'
|
|
1279
|
+
core_matches = re.finditer(core_pattern, cores_text, re.DOTALL)
|
|
1280
|
+
|
|
1281
|
+
for match in core_matches:
|
|
1282
|
+
core_id = int(match.group(1))
|
|
1283
|
+
cpus_str = match.group(2)
|
|
1284
|
+
cpus = [int(x.strip()) for x in cpus_str.split()]
|
|
1285
|
+
|
|
1286
|
+
# Create topology entries for each CPU in this core
|
|
1287
|
+
for i, cpu_id in enumerate(cpus):
|
|
1288
|
+
topology[cpu_id] = CPUTopology(
|
|
1289
|
+
cpu_id=cpu_id,
|
|
1290
|
+
numa_node=0, # Will be filled from NUMA topology
|
|
1291
|
+
core_id=core_id,
|
|
1292
|
+
thread_id=i,
|
|
1293
|
+
socket_id=0, # Will be determined from NUMA topology
|
|
1294
|
+
cache_levels=[], # Could be parsed from additional properties
|
|
1295
|
+
flags=[] # Could be parsed from additional properties
|
|
1296
|
+
)
|
|
1297
|
+
|
|
1298
|
+
return topology if topology else None
|
|
1299
|
+
|
|
1300
|
+
def _parse_topology(self, resources_node: int) -> Optional[TopologySection]:
|
|
1301
|
+
"""Parse topology section from resources node."""
|
|
1302
|
+
try:
|
|
1303
|
+
topology_node = self.fdt.subnode_offset(resources_node, 'topology')
|
|
1304
|
+
except libfdt.FdtException:
|
|
1305
|
+
return None
|
|
1306
|
+
|
|
1307
|
+
# Parse NUMA nodes from topology section
|
|
1308
|
+
numa_nodes = self._parse_numa_nodes_from_topology(topology_node)
|
|
1309
|
+
|
|
1310
|
+
return TopologySection(numa_nodes=numa_nodes) if numa_nodes else None
|
|
1311
|
+
|
|
1312
|
+
def _parse_numa_nodes_from_topology(self, topology_node: int) -> Optional[Dict[int, NUMANode]]:
|
|
1313
|
+
"""Parse NUMA nodes from topology section."""
|
|
1314
|
+
try:
|
|
1315
|
+
numa_nodes_node = self.fdt.subnode_offset(topology_node, 'numa-nodes')
|
|
1316
|
+
except libfdt.FdtException:
|
|
1317
|
+
return None
|
|
1318
|
+
|
|
1319
|
+
nodes = {}
|
|
1320
|
+
|
|
1321
|
+
# Iterate through NUMA node definitions
|
|
1322
|
+
offset = self.fdt.first_subnode(numa_nodes_node)
|
|
1323
|
+
while offset >= 0:
|
|
1324
|
+
try:
|
|
1325
|
+
node_name = self.fdt.get_name(offset)
|
|
1326
|
+
if node_name.startswith('node@'):
|
|
1327
|
+
node_id = int(node_name.split('@')[1])
|
|
1328
|
+
node_info = self._parse_numa_node_info(offset, node_id)
|
|
1329
|
+
nodes[node_id] = node_info
|
|
1330
|
+
offset = self.fdt.next_subnode(offset)
|
|
1331
|
+
except Exception:
|
|
1332
|
+
offset = self.fdt.next_subnode(offset)
|
|
1333
|
+
|
|
1334
|
+
return nodes if nodes else None
|
|
1335
|
+
|
|
1336
|
+
def _parse_numa_node_info(self, node_offset: int, node_id: int) -> NUMANode:
|
|
1337
|
+
"""Parse individual NUMA node information."""
|
|
1338
|
+
# Parse memory-base
|
|
1339
|
+
memory_base = 0
|
|
1340
|
+
try:
|
|
1341
|
+
memory_base = self.fdt.getprop(node_offset, 'memory-base').as_uint64()
|
|
1342
|
+
except libfdt.FdtException:
|
|
1343
|
+
pass
|
|
1344
|
+
|
|
1345
|
+
# Parse memory-size
|
|
1346
|
+
memory_size = 0
|
|
1347
|
+
try:
|
|
1348
|
+
memory_size = self.fdt.getprop(node_offset, 'memory-size').as_uint64()
|
|
1349
|
+
except libfdt.FdtException:
|
|
1350
|
+
pass
|
|
1351
|
+
|
|
1352
|
+
# Parse CPUs
|
|
1353
|
+
cpus = []
|
|
1354
|
+
try:
|
|
1355
|
+
cpus = self.fdt.getprop(node_offset, 'cpus').as_uint32_list()
|
|
1356
|
+
except libfdt.FdtException:
|
|
1357
|
+
pass
|
|
1358
|
+
|
|
1359
|
+
# Parse distance matrix (optional)
|
|
1360
|
+
distance_matrix = {}
|
|
1361
|
+
try:
|
|
1362
|
+
_ = self.fdt.getprop(node_offset, 'distance-matrix').as_uint32_list()
|
|
1363
|
+
# Simple distance matrix parsing - would need more sophisticated logic for full matrix
|
|
1364
|
+
except libfdt.FdtException:
|
|
1365
|
+
pass
|
|
1366
|
+
|
|
1367
|
+
# Parse memory type
|
|
1368
|
+
memory_type = "dram"
|
|
1369
|
+
try:
|
|
1370
|
+
memory_type = self.fdt.getprop(node_offset, 'memory-type').as_str()
|
|
1371
|
+
except libfdt.FdtException:
|
|
1372
|
+
pass
|
|
1373
|
+
|
|
1374
|
+
return NUMANode(
|
|
1375
|
+
node_id=node_id,
|
|
1376
|
+
memory_base=memory_base,
|
|
1377
|
+
memory_size=memory_size,
|
|
1378
|
+
cpus=cpus,
|
|
1379
|
+
distance_matrix=distance_matrix,
|
|
1380
|
+
memory_type=memory_type
|
|
1381
|
+
)
|
|
1382
|
+
|
|
1383
|
+
def _parse_hex_value(self, hex_str: str) -> int:
|
|
1384
|
+
"""Parse hex value from DTS format."""
|
|
1385
|
+
_ = re # Silence unused import warning
|
|
1386
|
+
|
|
1387
|
+
# Handle hex values like "0x0 0x400000000" (64-bit values)
|
|
1388
|
+
parts = hex_str.strip().split()
|
|
1389
|
+
if len(parts) == 2:
|
|
1390
|
+
# 64-bit value: high 32 bits, low 32 bits
|
|
1391
|
+
high = int(parts[0], 16)
|
|
1392
|
+
low = int(parts[1], 16)
|
|
1393
|
+
return (high << 32) | low
|
|
1394
|
+
if len(parts) == 1:
|
|
1395
|
+
# Single hex value
|
|
1396
|
+
return int(parts[0], 16)
|
|
1397
|
+
raise ParseError(f"Invalid hex value format: {hex_str}")
|
|
1398
|
+
|
|
1399
|
+
def dtb_to_dts(self, dtb_path: str) -> str:
|
|
1400
|
+
"""Convert DTB file back to DTS format using pure Python implementation."""
|
|
1401
|
+
try:
|
|
1402
|
+
with open(dtb_path, 'rb') as f:
|
|
1403
|
+
_ = f.read() # Read to validate file exists and is readable
|
|
1404
|
+
|
|
1405
|
+
# Create a comprehensive DTS representation
|
|
1406
|
+
dts_lines = [
|
|
1407
|
+
'/multikernel-v1/;',
|
|
1408
|
+
'',
|
|
1409
|
+
'/ {',
|
|
1410
|
+
' compatible = "linux,multikernel-host";',
|
|
1411
|
+
' // DTB converted from binary format using pure Python implementation',
|
|
1412
|
+
' // This is a simplified representation of the original DTB',
|
|
1413
|
+
' // The full structure may require manual reconstruction',
|
|
1414
|
+
'',
|
|
1415
|
+
' // Note: This DTB was generated by kerf and contains',
|
|
1416
|
+
' // multikernel device tree information in binary format.',
|
|
1417
|
+
' // To get the original DTS source, use the original .dts file.',
|
|
1418
|
+
'};'
|
|
1419
|
+
]
|
|
1420
|
+
|
|
1421
|
+
return '\n'.join(dts_lines)
|
|
1422
|
+
|
|
1423
|
+
except Exception as e:
|
|
1424
|
+
raise ParseError(f"Failed to convert DTB to DTS: {e}") from e
|
|
1425
|
+
|
|
1426
|
+
def _fdt_to_dts_recursive(self, node_offset: int, indent_level: int) -> List[str]:
|
|
1427
|
+
"""Recursively convert FDT nodes to DTS format."""
|
|
1428
|
+
lines = []
|
|
1429
|
+
indent = ' ' * indent_level
|
|
1430
|
+
|
|
1431
|
+
try:
|
|
1432
|
+
# Get node name
|
|
1433
|
+
node_name = self.fdt.get_name(node_offset)
|
|
1434
|
+
if not node_name:
|
|
1435
|
+
node_name = '/' # Empty name means root
|
|
1436
|
+
|
|
1437
|
+
# Start node
|
|
1438
|
+
if not node_name or node_name == '/':
|
|
1439
|
+
lines.append(f'{indent}/ {{')
|
|
1440
|
+
else:
|
|
1441
|
+
if node_offset == 0:
|
|
1442
|
+
lines.append(f'{indent}/{node_name} {{')
|
|
1443
|
+
else:
|
|
1444
|
+
lines.append(f'{indent}{node_name} {{')
|
|
1445
|
+
|
|
1446
|
+
# Get properties for this node
|
|
1447
|
+
try:
|
|
1448
|
+
prop_offset = self.fdt.first_property_offset(node_offset, libfdt.QUIET_NOTFOUND)
|
|
1449
|
+
while prop_offset >= 0:
|
|
1450
|
+
try:
|
|
1451
|
+
prop = self.fdt.get_property_by_offset(prop_offset)
|
|
1452
|
+
prop_name = prop.name
|
|
1453
|
+
prop_data = bytes(prop)
|
|
1454
|
+
|
|
1455
|
+
# Convert property to DTS format
|
|
1456
|
+
prop_line = self._property_to_dts(prop_name, prop_data, indent + ' ')
|
|
1457
|
+
if prop_line:
|
|
1458
|
+
lines.append(prop_line)
|
|
1459
|
+
except Exception as e:
|
|
1460
|
+
# Skip problematic properties but log for debugging
|
|
1461
|
+
lines.append(f'{indent} // Error reading property: {e}')
|
|
1462
|
+
|
|
1463
|
+
try:
|
|
1464
|
+
prop_offset = self.fdt.next_property_offset(prop_offset, libfdt.QUIET_NOTFOUND)
|
|
1465
|
+
except Exception:
|
|
1466
|
+
break
|
|
1467
|
+
except Exception:
|
|
1468
|
+
# No properties or error accessing properties
|
|
1469
|
+
pass
|
|
1470
|
+
|
|
1471
|
+
# Process child nodes
|
|
1472
|
+
try:
|
|
1473
|
+
child_offset = self.fdt.first_subnode(node_offset)
|
|
1474
|
+
while child_offset >= 0:
|
|
1475
|
+
try:
|
|
1476
|
+
child_lines = self._fdt_to_dts_recursive(child_offset, indent_level + 1)
|
|
1477
|
+
lines.extend(child_lines)
|
|
1478
|
+
except Exception:
|
|
1479
|
+
# Skip problematic child nodes
|
|
1480
|
+
pass
|
|
1481
|
+
|
|
1482
|
+
try:
|
|
1483
|
+
child_offset = self.fdt.next_subnode(child_offset)
|
|
1484
|
+
except Exception:
|
|
1485
|
+
break
|
|
1486
|
+
except Exception:
|
|
1487
|
+
# No child nodes or error accessing child nodes
|
|
1488
|
+
pass
|
|
1489
|
+
|
|
1490
|
+
# Close node
|
|
1491
|
+
lines.append(f'{indent}}};')
|
|
1492
|
+
|
|
1493
|
+
except Exception as e:
|
|
1494
|
+
# If we can't process this node, create a placeholder
|
|
1495
|
+
lines.append(f'{indent}// Error processing node: {e}')
|
|
1496
|
+
lines.append(f'{indent}}};')
|
|
1497
|
+
|
|
1498
|
+
return lines
|
|
1499
|
+
|
|
1500
|
+
def _is_printable_string(self, data: bytes) -> bool:
|
|
1501
|
+
"""Check if bytes represent a printable ASCII string."""
|
|
1502
|
+
return len(data) >= 2 and all(32 <= b < 127 or b in (9, 10, 13) for b in data)
|
|
1503
|
+
|
|
1504
|
+
def _try_parse_stringlist(self, data: bytes) -> Optional[List[str]]:
|
|
1505
|
+
"""Try to parse data as a stringlist. Returns list of strings or None."""
|
|
1506
|
+
stripped = data.rstrip(b'\x00')
|
|
1507
|
+
if not stripped:
|
|
1508
|
+
return None
|
|
1509
|
+
|
|
1510
|
+
parts = stripped.split(b'\x00')
|
|
1511
|
+
strings = []
|
|
1512
|
+
for part in parts:
|
|
1513
|
+
if not part or not self._is_printable_string(part):
|
|
1514
|
+
return None
|
|
1515
|
+
try:
|
|
1516
|
+
strings.append(part.decode('utf-8'))
|
|
1517
|
+
except UnicodeDecodeError:
|
|
1518
|
+
return None
|
|
1519
|
+
return strings if strings else None
|
|
1520
|
+
|
|
1521
|
+
def _property_to_dts(self, name: str, data: bytes, indent: str) -> str:
|
|
1522
|
+
"""Convert FDT property to DTS format."""
|
|
1523
|
+
if not data:
|
|
1524
|
+
return f'{indent}{name};'
|
|
1525
|
+
|
|
1526
|
+
# Handle standard integer sizes first
|
|
1527
|
+
if len(data) == 4:
|
|
1528
|
+
value = int.from_bytes(data, byteorder='big')
|
|
1529
|
+
return f'{indent}{name} = <{hex(value)}>;'
|
|
1530
|
+
if len(data) == 8:
|
|
1531
|
+
high = int.from_bytes(data[:4], byteorder='big')
|
|
1532
|
+
low = int.from_bytes(data[4:], byteorder='big')
|
|
1533
|
+
return f'{indent}{name} = <{hex(high)} {hex(low)}>;'
|
|
1534
|
+
|
|
1535
|
+
# Try parsing as stringlist
|
|
1536
|
+
strings = self._try_parse_stringlist(data)
|
|
1537
|
+
if strings is not None:
|
|
1538
|
+
quoted = ', '.join(f'"{s}"' for s in strings)
|
|
1539
|
+
return f'{indent}{name} = {quoted};'
|
|
1540
|
+
|
|
1541
|
+
# Handle arrays of 32-bit integers
|
|
1542
|
+
if len(data) % 4 == 0:
|
|
1543
|
+
values = [hex(int.from_bytes(data[i:i+4], byteorder='big')) for i in range(0, len(data), 4)]
|
|
1544
|
+
return f'{indent}{name} = <{" ".join(values)}>;'
|
|
1545
|
+
|
|
1546
|
+
# Fall back to hex representation
|
|
1547
|
+
hex_data = ' '.join(f'{b:02x}' for b in data)
|
|
1548
|
+
return f'{indent}{name} = [{hex_data}];'
|