Skip to content

Use persistent XPC connection for IP allocation management.#1395

Draft
jglogan wants to merge 3 commits intoapple:mainfrom
jglogan:network-allocate
Draft

Use persistent XPC connection for IP allocation management.#1395
jglogan wants to merge 3 commits intoapple:mainfrom
jglogan:network-allocate

Conversation

@jglogan
Copy link
Copy Markdown
Contributor

@jglogan jglogan commented Apr 6, 2026

  • Closes [Bug]: IP address exhaustion with container delete --force #1378.
  • Closes [Bug]: Unable to delete a network - IP allocator cannot be disabled with active containers #1318.
  • At present, IP allocation involves four parties: the ContainersService and NetworksService in the API server, the SandboxService running in container-runtime-linux, and the NetworkService running in container-network-vmnet.
  • Each NetworkService allocate() requires a corresponding deallocate() or an IP address gets leaked.
  • This change gets rid of deallocate() and instead changes allocate() so that the caller received an XPC connection. The caller holds this connection for as long as the network attachment is alive (essentially as long as the container is running). When the connection disconnects, the NetworkService automatically unregisters the hostname:IP association.
  • Removed IP allocation from the API server, restoring the old process by which sandboxes directly allocate from networks. To deal with the eventual possibility of more network plugins than container-network-vmnet, the bootstrap call requires an additional parameter networkPluginInfos that is the same size as the attachments array in the container configuration, and identifies the plugin for each attachment.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Motivation and Context

  • Current network plumbing is complicated and needed simplification.
  • The deallocate() call was always a leak hazard.

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

- Closes apple#1378.
- At present, IP allocation involves four parties:
  the ContainersService and NetworksService in the
  API server, the SandboxService running in
  `container-runtime-linux`, and the NetworkService
  running in `container-network-vmnet`.
- Each NetworkService `allocate()` requires a
  corresponding `deallocate()` or an IP address
  gets leaked.
- This change gets rid of `deallocate()` and
  instead changes `allocate()` so that the caller
  received an XPC connection. The caller holds
  this connection for as long as the network
  attachment is alive (essentially as long as
  the container is running). When the connection
  disconnects, the NetworkService automatically
  unregisters the hostname:IP association.
- Removed IP allocation from the API server,
  restoring the old process by which sandboxes
  directly allocate from networks. To deal
  with the eventual possibility of more network
  plugins than `container-network-vmnet`, the
  bootstrap call requires an additional parameter
  `networkPluginInfos` that is the same size
  as the attachments array in the container
  configuration, and identifies the plugin for
  each attachment.
request.set(key: NetworkKeys.macAddress.rawValue, value: macAddress.description)
}

let client = createClient()
Copy link
Copy Markdown
Member

@dcantah dcantah Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we changed this client to the model that some of our others have taken (maintain a long lived xpc connection instead of creating a new one for every request), does this screw things up with this persistent routes idea?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd probably be easier to just discuss it. I could see the API server ContainerNetworksService holding long lived XPC connections with each network. The API server uses different calls than the runtime plugin, so I imagine there's a way to make it work.

Comment on lines +52 to +54
/// Persistent XPC connections to each network plugin, one per network attachment.
/// Held for the container lifetime; deallocation happens automatically when these are released.
private var networkConnections: [XPCClient] = []
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a naive question, but could this ever cause an issue if we have a lot of running containers with a lot of networks in use?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not naive, I can do a bit of research on that.

A napkin calculation is that we can have a max of ~256 VMs, and I think maybe 4 network interfaces per VM (I think I just saw that recently but could be wrong there) so if you worst case the network interfaces per VM number you're looking at 4K XPC connections.

@jglogan jglogan marked this pull request as draft April 8, 2026 18:39
@jglogan
Copy link
Copy Markdown
Contributor Author

jglogan commented Apr 8, 2026

@katiewasnothere @dcantah Thanks for the initial comments!

I pushed a PR to fix a bug in the disconnect handler, it was canceling the server XPC connection even if the container wasn't in the running state, which caused startup errors to get handled improperly.

I still need to think through the sandbox changes more closely myself, so putting this into draft until I do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: IP address exhaustion with container delete --force [Bug]: Unable to delete a network - IP allocator cannot be disabled with active containers

4 participants