Skip to content

Commit

Permalink
[virtio] support non-zero copy case
Browse files Browse the repository at this point in the history
We have to properly handle the case when the zero copy is not properly 
initialised, i.e. vhost_user:rx_buffers() is not used as a source 
of RX buffers. When this is detected just get a buffer from rx_buffers()
and copy everything in it.

Signed-off-by: Nikolay Nikolaev <n.nikolaev@virtualopensystems.com>
  • Loading branch information
Nikolay Nikolaev committed May 20, 2014
1 parent 634c069 commit f112a7a
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions src/lib/virtio/net_device.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,53 @@ end
-- Prepared argument for writing a 1 to an eventfd.
local eventfd_one = ffi.new("uint64_t[1]", {1})

function VirtioNetDevice:more_vm_buffers ()
return freelist.nfree(self.vring_transmit_buffers) > 2
end

-- return the buffer from a iovec, ensuring it originates from the vm
function VirtioNetDevice:vm_buffer (iovec)
local should_continue = true
local b = iovec.buffer
-- check if this is a zero-copy packet
if b.origin.type ~= C.BUFFER_ORIGIN_VIRTIO then
-- get buffer from the once supplied by the VM
local old_b = b
b = freelist.remove(self.vring_transmit_buffers)
--assert(iovec.offset + iovec.length <= b.size)

-- copy the whole buffer data, including offset
ffi.copy(b.pointer, old_b.pointer, iovec.offset + iovec.length)
buffer.free(old_b)
iovec.buffer = b

if not self:more_vm_buffers() then
-- no more buffers, stop the loop
should_continue = false
end
end
return should_continue, b
end

-- Transmit packets from the app input queue to the VM.
function VirtioNetDevice:transmit_packets_to_vm ()
local l = self.owner.input.rx
if not l then return end
while not link.empty(l) do
local should_continue = not self.not_enough_vm_bufers

while (not link.empty(l)) and should_continue do
local p = link.receive(l)
local iovec = p.iovecs[0]
local b = iovec.buffer
local virtio_hdr = b.origin.info.virtio.header_pointer

--assert(b.origin.type == C.BUFFER_ORIGIN_VIRTIO)
-- ensure all data is in a single buffer
if p.niovecs > 1 then
packet.coalesce(p)
end

local iovec = p.iovecs[0]
should_continue, b = self:vm_buffer(iovec)

-- fill in the virtio header
local virtio_hdr = b.origin.info.virtio.header_pointer
ffi.copy(virtio_hdr, p.info, packet_info_size)

local used = self.txring.used.ring[self.txused%self.tx_vring_num]
Expand All @@ -200,6 +235,11 @@ function VirtioNetDevice:transmit_packets_to_vm ()
packet.deref(p)
end

if not should_continue then
-- not enough buffers detected, verify once again
self.not_enough_vm_bufers = not self:more_vm_buffers()
end

if self.txring.used.idx ~= self.txused then
self.txring.used.idx = self.txused
C.write(self.callfd[0], eventfd_one, 8)
Expand Down

0 comments on commit f112a7a

Please sign in to comment.