Skip to content

Commit

Permalink
fix(net): set dev feature bits based on the tap
Browse files Browse the repository at this point in the history
TUN_F_USO4/TUN_F_USO6 were added in Linux 6.2.

There is no easy way to query the supported features from the tap
device, so similar to QEMU (tap_fd_set_offload() in net/tap-linux.c),
we try tun_set_offload() until success.

Signed-off-by: Changyuan Lyu <[email protected]>
  • Loading branch information
Lencerf committed May 24, 2024
1 parent 3571e91 commit 82e2167
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions alioth/src/virtio/dev/net/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,16 @@ impl Net {
.read(true)
.write(true)
.open(param.tap)?;

let dev_feat = NetFeature::MAC
| NetFeature::MTU
| NetFeature::CSUM
| NetFeature::HOST_TSO4
| NetFeature::HOST_TSO6
| NetFeature::HOST_ECN
| NetFeature::HOST_UFO
| NetFeature::HOST_USO
| detect_tap_offload(&file);
log::debug!("{name}: device fature: {dev_feat:x?}");
setup_tap(&mut file, param.if_name.as_deref())?;
let net = Net {
name,
Expand All @@ -148,21 +157,7 @@ impl Net {
..Default::default()
}),
tap: file,
feature: NetFeature::MAC
| NetFeature::MTU
| NetFeature::GUEST_CSUM
| NetFeature::GUEST_TSO4
| NetFeature::GUEST_TSO6
| NetFeature::GUEST_ECN
| NetFeature::GUEST_UFO
| NetFeature::GUEST_USO4
| NetFeature::GUEST_USO6
| NetFeature::CSUM
| NetFeature::HOST_TSO4
| NetFeature::HOST_TSO6
| NetFeature::HOST_ECN
| NetFeature::HOST_UFO
| NetFeature::HOST_USO,
feature: dev_feat,
};
Ok(net)
}
Expand Down Expand Up @@ -198,7 +193,7 @@ impl Virtio for Net {

fn activate(&mut self, registry: &Registry, feature: u64, _memory: &RamBus) -> Result<()> {
let feature = NetFeature::from_bits_retain(feature);
log::debug!("{}: net feature: {:?}", self.name, feature);
log::debug!("{}: driver feature: {:?}", self.name, feature);
enable_tap_offload(&mut self.tap, feature)?;
registry.register(
&mut SourceFd(&self.tap.as_raw_fd()),
Expand Down Expand Up @@ -275,6 +270,31 @@ fn setup_tap(file: &mut File, if_name: Option<&str>) -> Result<()> {
Ok(())
}

fn detect_tap_offload(tap: &impl AsRawFd) -> NetFeature {
let mut tap_feature = TunFeature::all();
let mut dev_feat = NetFeature::GUEST_CSUM
| NetFeature::GUEST_TSO4
| NetFeature::GUEST_TSO6
| NetFeature::GUEST_ECN
| NetFeature::GUEST_UFO
| NetFeature::GUEST_USO4
| NetFeature::GUEST_USO6;
if unsafe { tun_set_offload(tap, tap_feature.bits()) }.is_ok() {
return dev_feat;
}
tap_feature &= !(TunFeature::USO4 | TunFeature::USO6);
dev_feat &= !(NetFeature::GUEST_USO4 | NetFeature::GUEST_USO6);
if unsafe { tun_set_offload(tap, tap_feature.bits()) }.is_ok() {
return dev_feat;
}
tap_feature &= !(TunFeature::UFO);
dev_feat &= !NetFeature::GUEST_UFO;
if unsafe { tun_set_offload(tap, tap_feature.bits()) }.is_ok() {
return dev_feat;
}
return NetFeature::empty();
}

fn enable_tap_offload(tap: &mut File, feature: NetFeature) -> io::Result<i32> {
let mut tap_feature = TunFeature::empty();
if feature.contains(NetFeature::GUEST_CSUM) {
Expand Down

0 comments on commit 82e2167

Please sign in to comment.