Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[draft] lib.blit: Introduce API for optimized "blitter" for Virtio-net DMA #711

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/lib/blit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- blit.lua - offload engine for memory operations

module(..., package.seeall)

local ffi = require("ffi")

-- The blit module provides "blitter" operation to offload
-- performance-critical memory operations. The API allows scheduling a
-- series of operations, that can be performed at any time and in any
-- order, and then executing a "barrier" to wait for completion.

-- The implementation in this file is very basic but could be extended
-- in the future to take advantage of the flexibility afforded by the
-- API to perform special optimizations (for example parallel memory
-- copies to amortize cache latency, etc).

function copy (dst, src, len)
-- Trivial implementation: simply do an immediate memory copy.
ffi.copy(dst, src, len)
end

-- Wait until all copies have completed.
function barrier ()
-- No-op because the copies were already executed eagerly.
end

function selftest ()
print("selftest: blit")
-- It would be valuable to have an extensive selftest function to
-- make it easy to develop and test new optimized blitter
-- implementations.
print("selftest: ok")
end
10 changes: 7 additions & 3 deletions src/lib/virtio/net_device.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local packet = require("core.packet")
local timer = require("core.timer")
local vq = require("lib.virtio.virtq")
local checksum = require("lib.checksum")
local blit = require("lib.blit")
local ffi = require("ffi")
local C = ffi.C
local band = bit.band
Expand Down Expand Up @@ -106,6 +107,7 @@ end
function VirtioNetDevice:poll_vring_receive ()
-- RX
self:receive_packets_from_vm()
blit.barrier()
self:rx_signal_used()
end

Expand Down Expand Up @@ -139,7 +141,8 @@ function VirtioNetDevice:rx_buffer_add(rx_p, addr, len)
local addr = self:map_from_guest(addr)
local pointer = ffi.cast(char_ptr_t, addr)

packet.append(rx_p, pointer, len)
rx_p.length = rx_p.length + len
blit.copy(rx_p, pointer, len)
return len
end

Expand Down Expand Up @@ -174,6 +177,7 @@ end
function VirtioNetDevice:poll_vring_transmit ()
-- RX
self:transmit_packets_to_vm()
blit.barrier()
self:tx_signal_used()
end

Expand Down Expand Up @@ -239,7 +243,7 @@ function VirtioNetDevice:tx_buffer_add(tx_p, addr, len)
local pointer = ffi.cast(char_ptr_t, addr)

assert(tx_p.length <= len)
ffi.copy(pointer, tx_p.data, tx_p.length)
blit.copy(pointer, tx_p.data, tx_p.length)

return tx_p.length
end
Expand Down Expand Up @@ -288,7 +292,7 @@ function VirtioNetDevice:tx_buffer_add_mrg_rxbuf(tx_p, addr, len)
local to_copy = math.min(tx_p.length - self.tx.data_sent, len + adjust)

-- copy the data to the adjusted pointer
ffi.copy(pointer - adjust, tx_p.data + self.tx.data_sent, to_copy)
ffi.copy(tx_p.data + self.tx.data_sent, pointer - adjust, to_copy)

-- update the num_buffers in the first virtio header
self.tx.tx_mrg_hdr[0].num_buffers = self.tx.tx_mrg_hdr[0].num_buffers + 1
Expand Down