Skip to content

Commit

Permalink
Merge pull request #41 from mewz-project/virtio-net-send-perf
Browse files Browse the repository at this point in the history
virtio net: interrupt coalescing when sending packets
  • Loading branch information
saza-ku authored Feb 5, 2024
2 parents d93a937 + 077e41b commit 890de0e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/drivers/virtio/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ pub fn VirtioMmioTransport(comptime DeviceConfigType: type) type {
}

pub fn notifyQueue(self: *Self, virtq: *Virtqueue) void {
if (virtq.not_notified_num_descs == 0) {
return;
}

self.common_config.queue_select = virtq.index;
@fence(std.builtin.AtomicOrder.SeqCst);
const offset = self.notify_off_multiplier * self.common_config.queue_notify_off;
Expand Down
22 changes: 13 additions & 9 deletions src/drivers/virtio/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ const VirtioNet = struct {
const buf = @as([*]u8, @ptrFromInt(base + @sizeOf(Header)));
@memcpy(buf, data);

var desc_buf = [_]common.VirtqDescBuffer{ common.VirtqDescBuffer{
var desc_buf = [_]common.VirtqDescBuffer{common.VirtqDescBuffer{
.addr = base,
.len = @sizeOf(Header),
.len = @sizeOf(Header) + @as(u32, @intCast(data.len)),
.type = common.VirtqDescBufferType.ReadonlyFromDevice,
}, common.VirtqDescBuffer{
.addr = base + @sizeOf(Header),
.len = @as(u32, @intCast(data.len)),
.type = common.VirtqDescBufferType.ReadonlyFromDevice,
} };
self.transmitq().enqueue(desc_buf[0..2]);
self.virtio.transport.notifyQueue(self.transmitq());
}};

self.transmitq().enqueue(desc_buf[0..1]);

if (self.transmitq().not_notified_num_descs > self.transmitq().num_descs / 2) {
self.virtio.transport.notifyQueue(self.transmitq());
}
}

pub fn receive(self: *Self) void {
Expand Down Expand Up @@ -207,3 +207,7 @@ fn handleIrq(frame: *interrupt.InterruptFrame) void {
log.debug.print("interrupt\n");
virtio_net.receive();
}

pub fn flush() void {
virtio_net.virtio.transport.notifyQueue(virtio_net.transmitq());
}
3 changes: 3 additions & 0 deletions src/timer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const heap = @import("heap.zig");
const interrupt = @import("interrupt.zig");
const sync = @import("sync.zig");
const net = @import("drivers/virtio/net.zig");

const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
Expand Down Expand Up @@ -58,6 +59,8 @@ pub fn handleIrq(frame: *interrupt.InterruptFrame) void {
}
}
timers.release();

net.flush();
}

pub fn init() void {
Expand Down

0 comments on commit 890de0e

Please sign in to comment.