kerf-multikernel 0.4.0__tar.gz → 0.5.0__tar.gz

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.
Files changed (68) hide show
  1. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/PKG-INFO +1 -1
  2. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/pyproject.toml +1 -1
  3. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/__init__.py +1 -1
  4. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/cli.py +1 -1
  5. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/create/main.py +11 -10
  6. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/daxfs/__init__.py +2 -2
  7. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/daxfs/mkdaxfs.py +30 -16
  8. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/delete/main.py +1 -1
  9. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/devices.py +34 -19
  10. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/dtc/extractor.py +3 -12
  11. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/dtc/overlay.py +4 -4
  12. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/dtc/parser.py +10 -37
  13. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/dtc/validator.py +11 -11
  14. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/exec/main.py +6 -1
  15. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/init/main.py +36 -56
  16. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/load/main.py +86 -41
  17. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/metadata.py +13 -11
  18. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/models.py +4 -3
  19. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/pool_diff.py +3 -3
  20. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/resources.py +8 -4
  21. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/show/main.py +46 -31
  22. kerf_multikernel-0.5.0/src/kerf/topology.py +253 -0
  23. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/unload/main.py +17 -12
  24. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/update/main.py +2 -2
  25. kerf_multikernel-0.4.0/src/kerf/topology.py +0 -139
  26. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/LICENSE +0 -0
  27. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/Makefile +0 -0
  28. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/README.md +0 -0
  29. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/build.py +0 -0
  30. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/init/Makefile +0 -0
  31. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/init/init.c +0 -0
  32. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/init/proto.h +0 -0
  33. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/init/session.c +0 -0
  34. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/init/session.h +0 -0
  35. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/init/session_test.c +0 -0
  36. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/baseline.py +0 -0
  37. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/console/__init__.py +0 -0
  38. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/console/main.py +0 -0
  39. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/create/__init__.py +0 -0
  40. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/data/__init__.py +0 -0
  41. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/delete/__init__.py +0 -0
  42. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/docker/__init__.py +0 -0
  43. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/docker/image.py +0 -0
  44. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/dtc/__init__.py +0 -0
  45. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/dtc/cells.py +0 -0
  46. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/dtc/reporter.py +0 -0
  47. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/dump/__init__.py +0 -0
  48. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/dump/main.py +0 -0
  49. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/exceptions.py +0 -0
  50. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/exec/__init__.py +0 -0
  51. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/exec/client.py +0 -0
  52. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/exec/protocol.py +0 -0
  53. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/exec/user.py +0 -0
  54. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/init/__init__.py +0 -0
  55. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/kill/__init__.py +0 -0
  56. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/kill/main.py +0 -0
  57. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/load/__init__.py +0 -0
  58. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/runtime.py +0 -0
  59. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/show/__init__.py +0 -0
  60. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/sign/__init__.py +0 -0
  61. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/sign/main.py +0 -0
  62. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/signature.py +0 -0
  63. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/start/__init__.py +0 -0
  64. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/start/main.py +0 -0
  65. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/unload/__init__.py +0 -0
  66. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/update/__init__.py +0 -0
  67. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/utils.py +0 -0
  68. {kerf_multikernel-0.4.0 → kerf_multikernel-0.5.0}/src/kerf/vmlinuz.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kerf-multikernel
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: Multikernel Management CLI tool
5
5
  Author: Cong Wang
6
6
  Requires-Python: >=3.9,<4.0
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
4
4
 
5
5
  [tool.poetry]
6
6
  name = "kerf-multikernel"
7
- version = "0.4.0"
7
+ version = "0.5.0"
8
8
  description = "Multikernel Management CLI tool"
9
9
  authors = ["Cong Wang"]
10
10
  readme = "README.md"
@@ -19,7 +19,7 @@ A comprehensive multikernel management system designed to orchestrate
19
19
  and manage multiple kernel instances on a single host.
20
20
  """
21
21
 
22
- __version__ = "0.4.0"
22
+ __version__ = "0.5.0"
23
23
  __author__ = "Cong Wang"
24
24
 
25
25
  # Export main runtime components for easy access
@@ -33,7 +33,7 @@ from .sign.main import sign
33
33
 
34
34
 
35
35
  @click.group()
36
- @click.version_option(version="0.4.0", prog_name="kerf")
36
+ @click.version_option(version="0.5.0", prog_name="kerf")
37
37
  @click.option("--debug", is_flag=True, help="Enable debug mode")
38
38
  @click.pass_context
39
39
  def main(ctx, debug):
@@ -35,6 +35,7 @@ from ..resources import (
35
35
  get_available_cpus,
36
36
  )
37
37
  from ..exceptions import ValidationError, KernelInterfaceError, ResourceError, ParseError
38
+ from ..topology import cpu_id_name
38
39
 
39
40
 
40
41
  def parse_cpu_spec(cpu_spec: str) -> List[int]:
@@ -137,7 +138,7 @@ def _allocate_spread(
137
138
  numa_cpu_lists[numa_node_id] = sorted(numa_cpus)
138
139
 
139
140
  if not numa_cpu_lists:
140
- raise ResourceError(f"No available APIC IDs in specified NUMA nodes: {numa_nodes}")
141
+ raise ResourceError(f"No available {cpu_id_name()}s in specified NUMA nodes: {numa_nodes}")
141
142
 
142
143
  allocated = []
143
144
  numa_indices = {node_id: 0 for node_id in numa_cpu_lists}
@@ -187,7 +188,7 @@ def _allocate_local(
187
188
  if len(numa_cpus) >= count:
188
189
  return sorted(numa_cpus[:count])
189
190
  raise ResourceError(
190
- f"Not enough APIC IDs in NUMA node {numa_node_id}: "
191
+ f"Not enough {cpu_id_name()}s in NUMA node {numa_node_id}: "
191
192
  f"requested {count}, but only {len(numa_cpus)} available"
192
193
  )
193
194
 
@@ -200,7 +201,7 @@ def _allocate_local(
200
201
  if len(numa_cpus) >= count:
201
202
  return sorted(numa_cpus[:count])
202
203
 
203
- raise ResourceError(f"No single NUMA node has {count} available APIC IDs for 'local' affinity")
204
+ raise ResourceError(f"No single NUMA node has {count} available {cpu_id_name()}s for 'local' affinity")
204
205
 
205
206
 
206
207
  def allocate_cpus_from_pool(
@@ -236,11 +237,11 @@ def allocate_cpus_from_pool(
236
237
  if len(available) < count:
237
238
  if numa_nodes:
238
239
  raise ResourceError(
239
- f"Not enough APIC IDs available in NUMA nodes {numa_nodes}: "
240
+ f"Not enough {cpu_id_name()}s available in NUMA nodes {numa_nodes}: "
240
241
  f"requested {count}, but only {len(available)} available"
241
242
  )
242
243
  raise ResourceError(
243
- f"Not enough APIC IDs available: requested {count}, "
244
+ f"Not enough {cpu_id_name()}s available: requested {count}, "
244
245
  f"but only {len(available)} available in pool"
245
246
  )
246
247
 
@@ -306,7 +307,7 @@ def parse_device_list(device_spec: Optional[str]) -> List[str]:
306
307
  """
307
308
  Parse device specification string into list of device references.
308
309
 
309
- A reference is an alias ("nvme0"), a PCI address ("0000:4f:01.0") or a
310
+ A reference is a name ("nvme0", "enP1s1f1"), a PCI address ("0000:4f:01.0") or a
310
311
  device node name ("pci_0000_4f_01_0") from the baseline, comma-separated.
311
312
 
312
313
  Args:
@@ -364,8 +365,8 @@ def dump_overlay_for_debug(
364
365
  @click.option(
365
366
  "--cpus",
366
367
  "-c",
367
- help='Explicit APIC ID allocation (e.g., "128" for APIC ID 128, "128-134" for range, '
368
- '"128,130,132" for list). Use physical APIC IDs, not logical CPU numbers. '
368
+ help='Explicit CPU allocation by physical ID, the APIC ID on x86 or the MPIDR on arm64 '
369
+ '(e.g., "128", "128-134" for range, "128,130,132" for list), not logical CPU numbers. '
369
370
  'Mutually exclusive with --cpu-count',
370
371
  )
371
372
  @click.option(
@@ -398,7 +399,7 @@ def dump_overlay_for_debug(
398
399
  @click.option(
399
400
  "--devices",
400
401
  "-d",
401
- help='Pool devices to assign, by alias, PCI address or node name '
402
+ help='Pool devices to assign, by name, PCI address or node name '
402
403
  '(comma-separated, e.g. "nvme0,0000:09:00.0"), as listed by kerf show.',
403
404
  )
404
405
  @click.option(
@@ -561,7 +562,7 @@ def create( # pylint: disable=too-many-arguments,too-many-positional-arguments
561
562
  try:
562
563
  cpu_spec_value = parse_cpu_spec(cpus)
563
564
  except ValueError as e:
564
- click.echo(f"Error: Invalid APIC ID specification '{cpus}': {e}", err=True)
565
+ click.echo(f"Error: Invalid {cpu_id_name()} specification '{cpus}': {e}", err=True)
565
566
  sys.exit(2)
566
567
 
567
568
  # Parse memory specification
@@ -19,7 +19,7 @@ from .mkdaxfs import (
19
19
  unmount_daxfs,
20
20
  DaxfsError,
21
21
  DaxfsImage,
22
- inject_kerf_init,
22
+ KERF_INIT_PATH,
23
23
  )
24
24
 
25
25
  __all__ = [
@@ -27,5 +27,5 @@ __all__ = [
27
27
  "unmount_daxfs",
28
28
  "DaxfsError",
29
29
  "DaxfsImage",
30
- "inject_kerf_init",
30
+ "KERF_INIT_PATH",
31
31
  ]
@@ -92,14 +92,18 @@ class FileEntry:
92
92
  data_offset: int = 0
93
93
  child_count: int = 0
94
94
  is_hardlink: bool = False
95
+ source: Optional[Path] = None
95
96
 
96
97
 
97
98
  class DaxfsBuilder:
98
99
  """Builds a daxfs v8 filesystem image: base snapshot plus write overlay."""
99
100
 
100
101
  def __init__(self, src_dir: str,
101
- overlay_pool_size: int = DAXFS_DEFAULT_OVERLAY_POOL):
102
+ overlay_pool_size: int = DAXFS_DEFAULT_OVERLAY_POOL,
103
+ overrides: Optional[Dict[str, Path]] = None):
102
104
  self.src_dir = Path(src_dir)
105
+ # Image path -> host file placed there instead of the rootfs entry
106
+ self.overrides = overrides or {}
103
107
  self.files: List[FileEntry] = []
104
108
  self.files_by_path: Dict[str, FileEntry] = {}
105
109
  # First entry seen for each hardlinked (st_dev, st_ino)
@@ -126,11 +130,13 @@ class DaxfsBuilder:
126
130
  buckets <<= 1
127
131
  return max(buckets, DAXFS_DEFAULT_BUCKET_COUNT)
128
132
 
129
- def _add_file(self, relpath: str, stat_result: os.stat_result) -> FileEntry:
133
+ def _add_file(self, relpath: str, stat_result: os.stat_result,
134
+ source: Optional[Path] = None) -> FileEntry:
130
135
  entry = FileEntry(
131
136
  path=relpath,
132
137
  name=os.path.basename(relpath)[:DAXFS_NAME_MAX] if relpath else "",
133
138
  stat=stat_result,
139
+ source=source,
134
140
  )
135
141
 
136
142
  # Hardlinked regular files share one inode and one data extent
@@ -162,6 +168,8 @@ class DaxfsBuilder:
162
168
 
163
169
  for entry in sorted(entries, key=lambda e: e.name):
164
170
  newrel = f"{relpath}/{entry.name}" if relpath else entry.name
171
+ if newrel in self.overrides:
172
+ continue
165
173
 
166
174
  try:
167
175
  file_stat = entry.lstat()
@@ -178,6 +186,16 @@ class DaxfsBuilder:
178
186
  root_stat = self.src_dir.lstat()
179
187
  self._add_file("", root_stat)
180
188
  self._scan_directory_recursive("")
189
+ for relpath, source in self.overrides.items():
190
+ self._add_file(relpath, self._override_stat(source), source)
191
+
192
+ @staticmethod
193
+ def _override_stat(source: Path) -> os.stat_result:
194
+ st = os.stat(source)
195
+ return os.stat_result((
196
+ stat_mod.S_IFREG | 0o755, st.st_ino, st.st_dev, 1, 0, 0,
197
+ st.st_size, int(st.st_atime), int(st.st_mtime), int(st.st_ctime),
198
+ ))
181
199
 
182
200
  def build_tree(self) -> None:
183
201
  """Assign parent inodes and count directory children."""
@@ -324,7 +342,7 @@ class DaxfsBuilder:
324
342
  def _write_file_data(self, mem: mmap.mmap, offset: int,
325
343
  entry: FileEntry) -> None:
326
344
  try:
327
- with open(self.src_dir / entry.path, 'rb') as f:
345
+ with open(entry.source or self.src_dir / entry.path, 'rb') as f:
328
346
  mem.seek(offset)
329
347
  shutil.copyfileobj(f, mem)
330
348
  except (PermissionError, FileNotFoundError, IsADirectoryError):
@@ -442,25 +460,18 @@ MOVE_MOUNT_F_EMPTY_PATH = 0x00000004
442
460
 
443
461
  AT_FDCWD = -100
444
462
 
463
+ # Off /init so that an image's own /init stays usable as an entrypoint
464
+ KERF_INIT_PATH = "/.kerf-init"
445
465
 
446
- def inject_kerf_init(rootfs_path: str) -> None:
447
- """Inject /init binary into rootfs.
448
466
 
449
- The entrypoint is passed via kernel cmdline as kerf.entrypoint=<path>.
450
- """
451
- rootfs = Path(rootfs_path)
452
-
453
- # Copy the pre-built init binary
467
+ def _kerf_init_binary() -> Path:
454
468
  init_binary = get_init_binary_path()
455
469
  if not init_binary.exists():
456
470
  raise DaxfsError(
457
471
  f"Init binary not found at {init_binary}. "
458
472
  "Run 'make' to build it first."
459
473
  )
460
-
461
- init_path = rootfs / "init"
462
- shutil.copy2(init_binary, init_path)
463
- os.chmod(init_path, 0o755)
474
+ return init_binary
464
475
 
465
476
 
466
477
  def _mount_daxfs(instance_name: str, dmabuf_fd: int) -> None:
@@ -521,7 +532,9 @@ def create_daxfs_image(
521
532
  size: Optional[int] = None,
522
533
  ) -> DaxfsImage:
523
534
  """
524
- Create a daxfs filesystem image from a directory.
535
+ Create a daxfs filesystem image from a directory, with kerf-init at
536
+ KERF_INIT_PATH. The entrypoint is passed via kernel cmdline as
537
+ kerf.entrypoint=<path>.
525
538
 
526
539
  Args:
527
540
  rootfs_path: Path to the root filesystem directory
@@ -538,7 +551,8 @@ def create_daxfs_image(
538
551
  if not os.path.isdir(rootfs_path):
539
552
  raise DaxfsError(f"Rootfs directory '{rootfs_path}' does not exist")
540
553
 
541
- builder = DaxfsBuilder(rootfs_path)
554
+ builder = DaxfsBuilder(rootfs_path,
555
+ overrides={KERF_INIT_PATH[1:]: _kerf_init_binary()})
542
556
  builder.scan()
543
557
  builder.build_tree()
544
558
  builder.calculate_offsets()
@@ -193,7 +193,7 @@ def delete(
193
193
  try:
194
194
  from ..metadata import delete_instance_metadata
195
195
 
196
- delete_instance_metadata(instance_name)
196
+ delete_instance_metadata(instance_id)
197
197
  except OSError:
198
198
  pass
199
199
 
@@ -18,10 +18,9 @@ Naming of pool PCI devices.
18
18
  A pool device is identified by its PCI address and nothing else: host
19
19
  names such as nvme0n1 or enp9s0 are probe-order or udev artifacts that
20
20
  differ between kernels, while the address is fixed by the fabric. The
21
- device node is named after the address, and a standard device tree
22
- /aliases entry gives the device a stable name: the name the device has in
23
- the kernel that owns it, which a spawned kernel then applies ("nvme0"
24
- stays nvme0 and its namespaces nvme0n1, "enp9s0" stays enp9s0).
21
+ device node is named after the address, and its label carries the name
22
+ the device has in the kernel that pooled it, verbatim, which every
23
+ spawned kernel then applies ("enP1s1f1" stays enP1s1f1).
25
24
  """
26
25
 
27
26
  import re
@@ -29,8 +28,8 @@ from typing import Optional, Tuple
29
28
 
30
29
  PCI_ID_RE = re.compile(r'^([0-9a-fA-F]{1,4}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2})\.([0-9a-fA-F])$')
31
30
  PCI_NODE_RE = re.compile(r'^pci_([0-9a-fA-F]{1,4})_([0-9a-fA-F]{1,2})_([0-9a-fA-F]{1,2})_([0-9a-fA-F])$')
32
- ALIAS_RE = re.compile(r'^[a-z0-9-]{1,31}$')
33
31
  NVME_NAME_RE = re.compile(r'^(nvme\d+)(?:n\d+(p\d+)?)?$')
32
+ LABEL_MAX = 31
34
33
  IFNAMSIZ = 16
35
34
 
36
35
 
@@ -51,17 +50,33 @@ def pci_node_name(pci_id: str) -> str:
51
50
  return "pci_" + normalized.replace(":", "_").replace(".", "_")
52
51
 
53
52
 
54
- def split_alias(entry: str) -> Tuple[str, Optional[str]]:
55
- """Split a "device" or "device=alias" request into its parts."""
56
- name, sep, alias = entry.partition("=")
57
- if not sep:
58
- return name, None
59
- if not ALIAS_RE.match(alias):
53
+ def check_label(label: str, compatible: Optional[str] = None) -> None:
54
+ """
55
+ Reject a name the kernels cannot carry or, for a network device, apply.
56
+
57
+ The rules are the kernel's own for an interface name, so any name the
58
+ host gave a device passes.
59
+ """
60
+ if (not label or len(label) > LABEL_MAX or label in (".", "..")
61
+ or any(c in "/:,=" or c.isspace() for c in label)):
60
62
  raise ValueError(
61
- f"Invalid alias '{alias}' for device '{name}': "
62
- "use 1 to 31 characters from a-z, 0-9 and '-', like nvme0 or enp9s0"
63
+ f"Invalid name '{label}': use 1 to {LABEL_MAX} characters "
64
+ "without whitespace, '/', ':', ',' or '='"
63
65
  )
64
- return name, alias
66
+ if compatible == "pci-network" and len(label) >= IFNAMSIZ:
67
+ raise ValueError(
68
+ f"Invalid name '{label}' for a network device: "
69
+ f"an interface name is at most {IFNAMSIZ - 1} characters"
70
+ )
71
+
72
+
73
+ def split_name(entry: str) -> Tuple[str, Optional[str]]:
74
+ """Split a "device" or "device=name" request into its parts."""
75
+ device, sep, name = entry.partition("=")
76
+ if not sep:
77
+ return device, None
78
+ check_label(name)
79
+ return device, name
65
80
 
66
81
 
67
82
  def is_partition(host_name: str) -> bool:
@@ -69,19 +84,19 @@ def is_partition(host_name: str) -> bool:
69
84
  return bool(match and match.group(2))
70
85
 
71
86
 
72
- def default_alias(host_name: str, compatible: Optional[str]) -> Optional[str]:
87
+ def default_label(host_name: str, compatible: Optional[str]) -> Optional[str]:
73
88
  """
74
- The alias a host device name implies, None when it implies nothing.
89
+ The name a host device name implies, None when it implies nothing.
75
90
 
76
91
  An NVMe name carries its controller name, which the namespaces follow.
77
- A network interface name is the alias itself, when the kernel can
78
- apply it. A raw PCI address implies no name at all.
92
+ A network interface name is the name itself. A raw PCI address, or a
93
+ name of something other than the PCI function, implies no name at all.
79
94
  """
80
95
  if normalize_pci_id(host_name):
81
96
  return None
82
97
  match = NVME_NAME_RE.match(host_name)
83
98
  if match:
84
99
  return match.group(1)
85
- if compatible == "pci-network" and ALIAS_RE.match(host_name) and len(host_name) < IFNAMSIZ:
100
+ if compatible == "pci-network":
86
101
  return host_name
87
102
  return None
@@ -109,8 +109,6 @@ class InstanceExtractor:
109
109
 
110
110
  fdt_sw.end_node() # End resources
111
111
 
112
- self._add_aliases_sw(fdt_sw, tree.hardware.devices)
113
-
114
112
  if tree.instances:
115
113
  self._add_instances_section_sw(fdt_sw, tree.instances)
116
114
 
@@ -156,6 +154,9 @@ class InstanceExtractor:
156
154
  if device_info.pci_id:
157
155
  fdt_sw.property_string("pci-id", device_info.pci_id)
158
156
 
157
+ if device_info.label:
158
+ fdt_sw.property_string("label", device_info.label)
159
+
159
160
  if device_info.vendor_id is not None:
160
161
  fdt_sw.property_u32("vendor-id", device_info.vendor_id)
161
162
 
@@ -194,16 +195,6 @@ class InstanceExtractor:
194
195
 
195
196
  fdt_sw.end_node()
196
197
 
197
- def _add_aliases_sw(self, fdt_sw, devices):
198
- aliases = {info.alias: name for name, info in (devices or {}).items() if info.alias}
199
- if not aliases:
200
- return
201
-
202
- fdt_sw.begin_node("aliases")
203
- for alias, name in aliases.items():
204
- fdt_sw.property_string(alias, f"/resources/devices/{name}")
205
- fdt_sw.end_node()
206
-
207
198
  def _add_instances_section_sw(self, fdt_sw, instances):
208
199
  """Add instances section using FdtSw."""
209
200
  fdt_sw.begin_node("instances")
@@ -236,7 +236,7 @@ class OverlayGenerator:
236
236
  self._cpu_op(fdt_sw, "cpu-remove", diff.cpus_to_host)
237
237
  self._cpu_op(fdt_sw, "cpu-add", diff.cpus_to_pool)
238
238
  self._device_op(fdt_sw, "device-remove", diff.devices_to_host)
239
- self._device_op(fdt_sw, "device-add", diff.devices_to_pool, diff.device_aliases)
239
+ self._device_op(fdt_sw, "device-add", diff.devices_to_pool, diff.device_labels)
240
240
 
241
241
  fdt_sw.end_node() # End __overlay__
242
242
  fdt_sw.end_node() # End fragment@0
@@ -269,7 +269,7 @@ class OverlayGenerator:
269
269
  fdt_sw.end_node()
270
270
  fdt_sw.end_node()
271
271
 
272
- def _device_op(self, fdt_sw, operation, pci_ids, aliases=None):
272
+ def _device_op(self, fdt_sw, operation, pci_ids, labels=None):
273
273
  """Write a device operation node, or nothing when there are no devices."""
274
274
  if not pci_ids:
275
275
  return
@@ -278,8 +278,8 @@ class OverlayGenerator:
278
278
  for idx, pci_id in enumerate(pci_ids):
279
279
  fdt_sw.begin_node(f"pci@{idx}")
280
280
  fdt_sw.property_string("pci-id", pci_id)
281
- if aliases and pci_id in aliases:
282
- fdt_sw.property_string("alias", aliases[pci_id])
281
+ if labels and pci_id in labels:
282
+ fdt_sw.property_string("label", labels[pci_id])
283
283
  fdt_sw.end_node()
284
284
  fdt_sw.end_node()
285
285
 
@@ -108,14 +108,7 @@ class DeviceTreeParser:
108
108
  self.fdt = libfdt.Fdt(dtb_data)
109
109
  except Exception as e:
110
110
  raise ParseError(f"Failed to parse instance DTB: {e}") from e
111
- devices = {}
112
- offsets = {}
113
- for offset, (name, device_info) in self._parse_pci_tree().items():
114
- devices[name] = device_info
115
- offsets[offset] = name
116
- for alias, name in self._parse_aliases(offsets).items():
117
- devices[name].alias = alias
118
- return devices
111
+ return dict(self._parse_pci_tree().values())
119
112
 
120
113
  def _build_global_tree(self) -> GlobalDeviceTree:
121
114
  """Build GlobalDeviceTree from parsed FDT."""
@@ -241,14 +234,10 @@ class DeviceTreeParser:
241
234
  if free_prop is not None:
242
235
  available_free = unpack_cpu_ids(free_prop)
243
236
 
244
- if available:
245
- total = max(available) + 1
246
- else:
247
- total = 0
248
237
  host_reserved = []
249
238
 
250
239
  return CPUAllocation(
251
- total=total,
240
+ total=len(available),
252
241
  host_reserved=host_reserved,
253
242
  available=available,
254
243
  available_free=available_free
@@ -325,7 +314,6 @@ class DeviceTreeParser:
325
314
  def _parse_devices(self, resources_node: int) -> Dict[str, DeviceInfo]:
326
315
  """Parse device information from resources node."""
327
316
  devices = {}
328
- offsets = {}
329
317
 
330
318
  devices_node = self.fdt.subnode_offset(resources_node, 'devices',
331
319
  libfdt.QUIET_NOTFOUND)
@@ -333,16 +321,11 @@ class DeviceTreeParser:
333
321
  name = self.fdt.get_name(offset)
334
322
  try:
335
323
  devices[name] = self._parse_device_info(offset, name)
336
- offsets[offset] = name
337
324
  except ParseError:
338
325
  pass
339
326
 
340
- for offset, (name, device_info) in self._parse_pci_tree().items():
327
+ for name, device_info in self._parse_pci_tree().values():
341
328
  devices[name] = device_info
342
- offsets[offset] = name
343
-
344
- for alias, name in self._parse_aliases(offsets).items():
345
- devices[name].alias = alias
346
329
 
347
330
  return devices
348
331
 
@@ -387,25 +370,11 @@ class DeviceTreeParser:
387
370
  device_info = self._parse_device_info(node, name)
388
371
  device_info.device_type = 'pci'
389
372
  device_info.pci_id = pci_id
373
+ iommu = self.fdt.getprop(node, 'multikernel,iommu-base', libfdt.QUIET_NOTFOUND)
374
+ if iommu != -libfdt.NOTFOUND:
375
+ device_info.iommu_base = iommu.as_uint64()
390
376
  found[node] = (name, device_info)
391
377
 
392
- def _parse_aliases(self, offsets: Dict[int, str]) -> Dict[str, str]:
393
- """Map each /aliases entry to the device node it points at."""
394
- aliases = {}
395
- aliases_node = self.fdt.path_offset('/aliases', libfdt.QUIET_NOTFOUND)
396
- if aliases_node < 0:
397
- return aliases
398
-
399
- prop_offset = self.fdt.first_property_offset(aliases_node, libfdt.QUIET_NOTFOUND)
400
- while prop_offset >= 0:
401
- prop = self.fdt.get_property_by_offset(prop_offset)
402
- target = self.fdt.path_offset(prop.as_str(), libfdt.QUIET_NOTFOUND)
403
- if target in offsets:
404
- aliases[prop.name] = offsets[target]
405
- prop_offset = self.fdt.next_property_offset(prop_offset, libfdt.QUIET_NOTFOUND)
406
-
407
- return aliases
408
-
409
378
  def _parse_device_info(self, node_offset: int, name: str) -> DeviceInfo:
410
379
  """Parse individual device information."""
411
380
  compatible = ""
@@ -442,6 +411,9 @@ class DeviceTreeParser:
442
411
  except libfdt.FdtException:
443
412
  pass
444
413
 
414
+ label = self.fdt.getprop(node_offset, 'label', libfdt.QUIET_NOTFOUND)
415
+ label = None if label == -libfdt.NOTFOUND else label.as_str()
416
+
445
417
  try:
446
418
  vendor_id = self.fdt.getprop(node_offset, 'vendor-id').as_uint32()
447
419
  except libfdt.FdtException:
@@ -488,6 +460,7 @@ class DeviceTreeParser:
488
460
  device_type=device_type,
489
461
  device_name=device_name,
490
462
  pci_id=pci_id,
463
+ label=label,
491
464
  vendor_id=vendor_id,
492
465
  device_id=device_id,
493
466
  sriov_vfs=sriov_vfs,
@@ -288,17 +288,17 @@ class MultikernelValidator:
288
288
  cpus = tree.hardware.cpus
289
289
  instance_cpus = set(instance.resources.cpus)
290
290
 
291
- for cpu in instance_cpus:
292
- if cpu < 0 or cpu >= cpus.total:
293
- error_msg = self._format_error_with_context(
294
- error_type="CPU allocation error",
295
- instance_name=instance.name,
296
- problem=f"CPU {cpu} does not exist in hardware inventory",
297
- current_state=f"Hardware has CPUs 0-{cpus.total-1}",
298
- suggestion=f"Use CPUs in range 0-{cpus.total-1}",
299
- alternative="Check hardware inventory configuration",
300
- )
301
- self.errors.append(error_msg)
291
+ known = sorted(set(cpus.host_reserved) | set(cpus.available))
292
+ for cpu in sorted(instance_cpus - set(known)):
293
+ error_msg = self._format_error_with_context(
294
+ error_type="CPU allocation error",
295
+ instance_name=instance.name,
296
+ problem=f"CPU {cpu} does not exist in hardware inventory",
297
+ current_state=f"Hardware has CPUs {known}",
298
+ suggestion="Use a CPU from the hardware inventory",
299
+ alternative="Check hardware inventory configuration",
300
+ )
301
+ self.errors.append(error_msg)
302
302
 
303
303
  host_reserved = set(cpus.host_reserved)
304
304
  reserved_cpus = instance_cpus.intersection(host_reserved)
@@ -18,6 +18,7 @@ kerf exec: run a command inside a running instance.
18
18
 
19
19
  import errno
20
20
  import os
21
+ import random
21
22
  import signal
22
23
  import socket
23
24
  import sys
@@ -76,7 +77,11 @@ def connect_agent(instance_id: int, name: str) -> socket.socket:
76
77
  try:
77
78
  sock.setsockopt(socket.AF_VSOCK, SO_VM_SOCKETS_TRANSPORT, VSOCK_TRANSPORT_MULTIKERNEL)
78
79
  # kerf-init only trusts reserved ports, which only root can bind.
79
- for port in range(protocol.AGENT_PORT, 511, -1):
80
+ # Start at a random one: reusing the port of an exec that just gave
81
+ # up can pick up the late answer to that exec's connect.
82
+ ports = list(range(protocol.AGENT_PORT, 511, -1))
83
+ start = random.randrange(len(ports))
84
+ for port in ports[start:] + ports[:start]:
80
85
  try:
81
86
  sock.bind((socket.VMADDR_CID_ANY, port))
82
87
  break