Skip to content

net: support multiple network interfaces#367

Merged
slp merged 2 commits intocontainers:mainfrom
slp:multiple-network
Jul 9, 2025
Merged

net: support multiple network interfaces#367
slp merged 2 commits intocontainers:mainfrom
slp:multiple-network

Conversation

@slp
Copy link
Copy Markdown
Collaborator

@slp slp commented Jul 1, 2025

So far, we were assuming the microVM will have a single network interface. But we have at least one new usecase (namely, running Android), that requires having multiple interfaces.

Refactor the API to support adding mulitple interfaces, while we keep compatibility with the old API.

While there, also allow the user to set up the network device flags.

Comment thread include/libkrun.h Outdated
Comment thread include/libkrun.h Outdated
Comment thread include/libkrun.h Outdated
Comment thread include/libkrun.h Outdated
Comment thread src/devices/src/virtio/net/device.rs Outdated
Comment thread src/libkrun/src/lib.rs Outdated
Comment thread include/libkrun.h Outdated
Comment thread include/libkrun.h Outdated
Comment thread include/libkrun.h Outdated
@slp slp force-pushed the multiple-network branch 2 times, most recently from 99a268f to 992fda3 Compare July 3, 2025 13:47
Copy link
Copy Markdown
Contributor

@nirs nirs left a comment

Choose a reason for hiding this comment

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

So much nicer with generic names!

Comment thread include/libkrun.h Outdated
Comment thread include/libkrun.h Outdated
Comment thread include/libkrun.h Outdated
Comment thread include/libkrun.h
Comment thread include/libkrun.h Outdated
Comment thread src/devices/src/virtio/net/unixstream.rs Outdated
Comment thread src/devices/src/virtio/net/unixstream.rs Outdated
Comment thread src/libkrun/src/lib.rs Outdated
Comment thread src/libkrun/src/lib.rs
};

if fd >= 0 && path.is_some() {
return -libc::EINVAL;
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.

EINVAL without an error message is hard to work with. We have 4 different EINVAL errors for this function. How the user can tell what is the issue?

Can we have libkrun_error() to get the last error message? Probably not in the scope of this change, and I did not check the rest of the code.

Copy link
Copy Markdown
Collaborator

@mtjhrc mtjhrc Jul 3, 2025

Choose a reason for hiding this comment

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

Can we have libkrun_error() to get the last error message? Probably not in the scope of this change, and I did not check the rest of the code.

Yeah, so our error reporting is not great and unfortunately we don't have anything like libkrun_error(), nor do we define our own error codes which would help here. Currently the only way to see what went wrong for example with krun_start_enter is to check the log (which has to be enabled). Not great I know, but that is really something we need to change in v2 API more globally (otherwise it would get really confusing how errors work).

Comment thread src/vmm/src/vmm_config/net.rs
@slp slp force-pushed the multiple-network branch from 992fda3 to 08d3eed Compare July 4, 2025 07:57
Comment thread include/libkrun.h Outdated
#define NET_FLAG_VFKIT 1 << 0

/* Taken from uapi/linux/virtio_net.h */
#define NET_VFLAG_CSUM 1 << 0
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.

If we go with 2 flag arguments let's make the constants and the flags names more clear using "features" and "flags".

#define NET_FEATURE_CSUM 1<<0
...
 *  "features"  - virtio-net features for the network interface.
 *  "flags"     - generic flags for the network interface.

Switching the order because the virtio features are more important and flags are usually the last argument.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sounds reasonable to me, I've just changed it.

slp added 2 commits July 8, 2025 04:37
So far, we were assuming the microVM will have a single network
interface. But we have at least one new usecase (namely, running
Android), that requires having multiple interfaces.

Refactor the API to support adding mulitple interfaces, while we keep
compatibility with the old API.

While there, also allow the user to set up the network device flags.

Signed-off-by: Sergio Lopez <slp@redhat.com>
Update examples to use the new network config API and avoid use of
deprecated functions.

Signed-off-by: Sergio Lopez <slp@redhat.com>
@slp
Copy link
Copy Markdown
Collaborator Author

slp commented Jul 8, 2025

@mtjhrc could you please take another look?

Comment thread examples/boot_efi.c Outdated
#define MAX_PATH 4096
#endif

#define COMPAT_NET_FLAGS NET_CSUM | NET_GUEST_CSUM | NET_GUEST_TSO4 | NET_GUEST_UFO | NET_HOST_TSO4 | NET_HOST_UFO
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You forgot to update this to the new constant names

Comment thread examples/boot_efi.c Outdated
perror("Error configuring net mode");
return -1;
uint8_t mac[] = {0x5a, 0x94, 0xef, 0xe4, 0x0c, 0xee};
if (err = krun_add_net_passt(ctx_id, passt_fd, &mac[0], COMPAT_NET_FLAGS)) {
Copy link
Copy Markdown
Collaborator

@mtjhrc mtjhrc Jul 8, 2025

Choose a reason for hiding this comment

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

You forgot to update this to the new function name and signature

Copy link
Copy Markdown
Collaborator

@mtjhrc mtjhrc left a comment

Choose a reason for hiding this comment

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

Please fix the boot_efi example to not refer to non-existant functions/constants.
Otherwise everything looks good.

@slp slp force-pushed the multiple-network branch from 9bd1af1 to 184f58c Compare July 9, 2025 11:16
@slp
Copy link
Copy Markdown
Collaborator Author

slp commented Jul 9, 2025

Please fix the boot_efi example to not refer to non-existant functions/constants. Otherwise everything looks good.

Good catch, thanks. I've fixed that and also moved COMPAT_NET_FEATURES to libkrun.h, which seems like a better place for it.

@slp slp merged commit 0ce4b28 into containers:main Jul 9, 2025
6 checks passed
nirs added a commit to nirs/krunkit that referenced this pull request Aug 15, 2025
With new libkrun if we set the mac address after setting gvproxy path,
the network device is created using a default mac address:

    let mac = cfg.mac.unwrap_or([0x5a, 0x94, 0xef, 0xe4, 0x0c, 0xee]);
    create_virtio_net(
        cfg,
        VirtioNetBackend::UnixgramPath(path, true),
        mac,
        NET_COMPAT_FEATURES,
    );

This breaks programs like vment-helper tests and minikube, assuming that
the device will use the mac address set by the tool.

This also breaks krunkit networking when using vment-helper with
offloading. I'm not sure why this happens.

With this change we can build with libkrun main and run a vm with
vment-helper example script or minikube krunkit driver.

I think this should be fixed in libkrun. This seems to be a regression
introduced by containers/libkrun#367. But the
fix in krunki is easy and safe.
nirs added a commit to nirs/krunkit that referenced this pull request Aug 16, 2025
With new libkrun if we set the mac address after setting gvproxy path,
the network device is created using a default mac address:

    let mac = cfg.mac.unwrap_or([0x5a, 0x94, 0xef, 0xe4, 0x0c, 0xee]);
    create_virtio_net(
        cfg,
        VirtioNetBackend::UnixgramPath(path, true),
        mac,
        NET_COMPAT_FEATURES,
    );

This breaks programs like vment-helper tests and minikube, assuming that
the device will use the mac address set by the tool.

This also breaks krunkit networking when using vment-helper with
offloading. I'm not sure why this happens.

With this change we can build with libkrun main and run a vm with
vment-helper example script or minikube krunkit driver.

I think this should be fixed in libkrun. This seems to be a regression
introduced by containers/libkrun#367. But the
fix in krunki is easy and safe.

Signed-off-by: Nir Soffer <nirsof@gmail.com>
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.

4 participants