Commit graph

360 commits

Author SHA1 Message Date
Daniel Verkamp
7b4822aa38 devices: fix fuse compilation on 32-bit platforms
struct stat64 uses different types on 32-bit platforms like arm; cast to
the types used there to allow compilation on both x86-64 and arm.

In addition, a 64-bit offset was being passed to libc::ftruncate, but
this API takes a 32-bit off_t on 32-bit platforms; switch to ftruncate64
to allow the full range of offsets.

BUG=b:136128319
TEST=emerge-kevin crosvm
TEST=emerge-nami crosvm

Change-Id: I382aef8509ca723efcf5024b22e140265636dc10
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1899218
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2019-11-06 18:31:05 +00:00
Stephen Barber
1104a730a7 devices: net: use FileReadWriteVolatile trait for tx
This removes the unnecessary copy on the tx path. On nami, this increases
tx throughput by ~60%.

BUG=chromium:753630
TEST=crostini.NetworkPerf

Cq-Depend: chromium:1873142
Change-Id: I58be5a7acef5d118d34b3c42ffb5a34e90070bf4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1881419
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Stephen Barber <smbarber@chromium.org>
Commit-Queue: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2019-11-05 19:49:41 +00:00
Daniel Verkamp
102278e944 devices: add into_iovec() for DescriptorUtils
This allows the conversion of (part of) a descriptor chain into an iovec
suitable for use with sys_util functions like send_with_fds().

BUG=None
TEST=./build_test.py

Change-Id: I4e3f7d9c1175c1173661b0661d3fa15d1da72d1a
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1815417
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
2019-11-05 19:26:37 +00:00
Chirantan Ekbote
c2d221804f devices: fs: Fix available_bytes() call
This call was changed to not return a Result in "7f64f50
descriptor_utils: check for size overflow in new()".

BUG=b:136128319
TEST=build and run pjdfstests

Change-Id: Ibdc786b26ff35977723ba61c51e8cdf1b631edc8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1890581
Auto-Submit: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
2019-11-01 06:20:29 +00:00
Chirantan Ekbote
2569b20f0f devices: fs: Add a passthrough file system
Add a "passthrough" file system implementation that just forwards it's
requests to the appropriate system call.

BUG=b:136128319
TEST=`tast run vm.VirtioFs`

Change-Id: I802c91dd0af8cdd8b9e761d9f04f874ae41ec033
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1758103
Tested-by: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
2019-11-01 06:20:27 +00:00
Daniel Verkamp
85858f580e devices: virtio: disable MSI-X for block and net
Temporarily turn off MSI-X support in the block and net devices since it
seems this is responsible for some test flakiness that manifests as
timeouts/hangs in the ProxyDevice read handler, e.g.:

  [devices/src/proxy.rs:238] failed read from child device process
  virtio-pci (virtio-block): failed to receive request or response:
  Resource temporarily unavailable (os error 11)

This is a minimally-invasive change to disable MSI-X without a full
revert of the relevant patches by just changing the relevant devices so
that they no longer request MSI-X vectors.

BUG=chromium:1019986
BUG=chromium:854765
TEST=check /proc/interrupts inside crosvm does not contain "PCI-MSI"

Change-Id: Ib37b503e609e2b9e22265370bcfe5804f04057ef
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1891643
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Zide Chen <zide.chen@intel.corp-partner.google.com>
2019-10-31 19:14:13 +00:00
Chirantan Ekbote
232ada7187 virtio: Convert ring index to u64 earlier in Queue::pop
The `next_avail` field is a Wrapping<u16> but we pull out the underlying
u16 when calculating the descriptor index address offset in Queue::pop
and only convert the result to a u64 after applying all the operations.

This can cause a u16 overflow if the queue size is the max
allowed (2^15).  Instead, convert to a u64 immediately after calculating
the index so that the rest of the operations are carried out as u64s and
will not overflow.

BUG=chromium:1018319
TEST=`cros_fuzz reproduce` and unit tests

Change-Id: I49743e239e2a407498d862c5137930f3f0cdf72a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1884404
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Auto-Submit: Chirantan Ekbote <chirantan@chromium.org>
2019-10-29 23:37:22 +00:00
Daniel Verkamp
ec5916daa0 devices: virtio: block: use FileReadWriteAtVolatile
Use the "at" variants of the read/write functions in the block device.
This reduces the number of syscalls on the host per I/O to one
(pread64/pwrite64) rather than two (lseek + read/write).

The CompositeDiskFile implementation is also updated in this commit,
since it's both a producer and consumer of DiskFile, and it isn't
trivial to update it in a separate commit without breaking compilation.

BUG=None
TEST=Start Crostini on kevin, banon, and nami

Change-Id: I031e7e87cd6c99504db8c56b1725ea51c1e27a53
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1845948
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
2019-10-29 22:06:22 +00:00
Xiong Zhang
4b5bb3a4ed vfio: Add vfio msi routing information into kvm
When vfio device msi is enabled, use VmIrqRequest->AllocateOneMsi() to
allocate one gsi for a msi vector, and link gsi with irqfd through
vm->register_irqfd, use VmIrqRequest->AddMsiRoute() to add msi routing
info into kvm route table.

BUG=chromium:992270
TEST=none

Change-Id: I5e2d2347e5e26f0ef6e12554dae4b12934b65e82
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1581146
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2019-10-29 11:10:47 +00:00
Chirantan Ekbote
46ab05d1e9 descriptor_utils: Use copy_nonoverlapping
Replace the copy_{to,from} calls for VolatileSlice with
ptr::copy_nonoverlapping.  The copy_{to,from} implementations were doing
a volatile read/write per byte, which is significantly slower than just
using a memcpy.

Using copy_nonoverlapping should be safe here as that's how this was
implemented before the refactor.

BUG=chromium:1014999
TEST=unit tests

Change-Id: Iad29e76056ff3064a5fe7e816b517b4ac75eaaef
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1866894
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2019-10-28 09:07:36 +00:00
Xiong Zhang
b19987d93f vfio: Intx support
When hw reports it could support INTX, this patch enable it by passing
irqfd into vfio kernel.

Then once hw intx happens, the vfio kernel irq handler receives and
handles it, the handler will trigger irqfd and kvm injects the interrupt
into guest.

BUG=chromium:992270
TEST=None

Change-Id: I8b200174a91183b7324b0044fde13b44c751d4d7
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1813457
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2019-10-28 06:10:01 +00:00
Chirantan Ekbote
f9815ee26f devices: fs: Add multikey module
The multikey module provides a BTreeMap implementation that can use one
of 2 different kinds of keys to look up a value.  This is needed by the
virtio-fs server since it needs to be able to look up keys either by u64
or by a (ino_t, dev_t) pair.

BUG=b:136127316
TEST=`tast run vm.VirtioFs`

Change-Id: I3a22331e7a15b2316c31ac803bf2813a14bf948f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1837025
Auto-Submit: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Tested-by: Chirantan Ekbote <chirantan@chromium.org>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
2019-10-27 14:26:02 +00:00
Chirantan Ekbote
0399e18235 devices: fs: Implement virtio-fs server
Add a `Server` type that links the FUSE protocol with the virtio
transport.  It parses messages sent on the virtio queue and then calls
the appropriate method of the `Filesystem` trait.

BUG=b:136128319
TEST=`tast run vm.VirtioFs`

Change-Id: I7d6fb521f6c620efe1bdb4fa0fa8fb8c42a82f45
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1757242
Auto-Submit: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: Chirantan Ekbote <chirantan@chromium.org>
2019-10-27 14:26:01 +00:00
Chirantan Ekbote
a24f033593 devices: fs: Add Filesystem trait
Add the `Filesystem` trait, which is the main interface between the
transport and the actual file system implementation.

BUG=b:136128319
TEST=`tast run vm.VirtioFs`

Change-Id: Ic8bc9e231652020501e10ad0be810a9f66e90b8e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1757241
Auto-Submit: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Tested-by: Chirantan Ekbote <chirantan@chromium.org>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
2019-10-27 14:25:58 +00:00
Zide Chen
8a7e4e902a devices: implement dedicated Interrupt struct for virtio Worker
The code to inject interrupt to the guest can be generic to all
virtio devices. This patch:

- move those guest interrupt related fields out of Worker structure and
  put in a separate file, making the worker code cleaner.
- remove redandant functions across virtio devices: signal_used_queue(),
  signal_config_changed(), etc.

BUG=chromium:854765
TEST=sanity test on eve and Linux
TEST=cargo test -p devices

Change-Id: I8e9f760f2057f192fdc74d16a59fea2e6b08c194
Signed-off-by: Zide Chen <zide.chen@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1869553
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2019-10-25 23:59:45 +00:00
Xiong Zhang
3185ae95dd devices: enable MSI-X for virtio-net and viotio-block devices
- signal_used_queue(): trigger MSI-X interrupts to the guest if MSI-X is
  enabled, otherwise trigger INTx interrupts
- enable MSI-X on vhost-net: allocate one vhost_interrupt for every
  MSI-X vector.

Performance wise, fio random R/W test on eve pixelbook:

           INTx          MSI-X      delta
fio write  8.13MiB/s    9.79MiB/s   +1.66MiB/s (+20%)
fio read   24.35MiB/s   29.3MiB/s   +4.95MiB/s (+20%)

For networking performance (TCP stream), test results on eve pixelbook:

             INTx            MSI-X          delta
iperf3       5.93Gbits/s     6.57Gbits/s    +0.64Gbits/s (+10.7%)
iperf3 -R    5.68Gbits/s     7.37Gbits/s    +1.30Gbits/s (+22.8%)

iperf test results on VM launched from Ubuntu host (client sends only):

             INTx            MSI-X          delta
virtio-net   9.53Gbits/s     11.4 Gbits/s   +1.87Gbits/s (+19.5%)
vhost        28.34Gbits/s    44.43Gbits/s   +16.09Gbits/s (+56.7%)

BUG=chromium:854765
TEST=cargo test -p devices
TEST=tested virtio-net and block on Linux VM and eve pixelbook

Change-Id: Ic4952a094327e6b977f446def8209ea2f796878c
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Signed-off-by: Zide Chen <zide.chen@intel.corp-partner.google.com>
Signed-off-by: Sainath Grandhi <sainath.grandhi@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1828340
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2019-10-25 23:59:43 +00:00
Daniel Verkamp
7f64f5030b descriptor_utils: check for size overflow in new()
Move the check for length overflow that was in available_bytes() into
Reader::new() and Writer::new().  This simplifies callers, since they
can assume that once a valid Reader or Writer has been constructed,
available_bytes() cannot fail.  Since we are walking the descriptor
chain during new() anyway, this extra check should be essentially free.

BUG=None
TEST=cargo test -p devices descriptor_utils

Change-Id: Ibeb1defd3728e7b71356650094b0885f3419ed47
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1873142
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Stephen Barber <smbarber@chromium.org>
2019-10-25 17:49:40 +00:00
Xiong Zhang
a5d248c863 devices: implement MSI control socket
Allocate per device VmMsi msg_socket for communication between virtio
devices and main VM process, which owns the KVM fd and issues ioctl to
KVM for KVM_IRQFD and KVM_SET_GSI_ROUTING.

BUG=chromium:854765
TEST=None

Change-Id: Ie1c81534912eaab7fbf05b5edef7dca343db301c
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Signed-off-by: Zide Chen <zide.chen@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1828339
Tested-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2019-10-24 20:46:41 +00:00
Zide Chen
d6be9614ba devices: finish the functions to enable MSI-X
- add a new field "vector" to struct Queue, which represents the entry
  number to the MSI-X Table. This can be used to find out the desired irqfd
  to inject MSI-X interrupts to the guest.
- enable MSI-X when MSI-X Enable bit of the Message Control word is
  being set: allocate irqfd per MSI-X vector; register the irqfd to KVM;
  update GSI routing to KVM.
- update GSI routing if the Message Data or Message Addr of individual
  MSI-X table Entry is being changed in run time.

BUG=chromium:854765
TEST=cargo test -p devices

Change-Id: I81533999ab6cd9ec5f111b256caf34077a4a7d1a
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Signed-off-by: Zide Chen <zide.chen@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1828338
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Stephen Barber <smbarber@chromium.org>
2019-10-24 20:46:40 +00:00
Zide Chen
1f20497b86 devices: implement MsixConfig struct and generic MSI-X functions
The MsixConfig struct is responsible for all the operations of MSI-X
Capability Structure and MSI-X Table.

A msix_config object is created for each virtio device.

BUG=chromium:854765
TEST=cargo test -p devices

Change-Id: Ide7c34d335d49a201f20b0a4307bcda97d1d61b7
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Signed-off-by: Zide Chen <zide.chen@intel.corp-partner.google.com>
Signed-off-by: Sainath Grandhi <sainath.grandhi@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1828337
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Stephen Barber <smbarber@chromium.org>
2019-10-24 20:46:39 +00:00
Zide Chen
1d15851b27 devices: implement msix capability structure
The MSI-X feature is ported from Cloud-hypervisor commit 69e27288a2e.
(https://github.com/intel/cloud-hypervisor.git)

In this commit:

- add a new "msix" module to the pci crate.
- implement the MSI-X Capability Structure.
- implement per virtio device msix_vectors() function which represents the
  supported MSI-X vector for this device.

BUG=chromium:854765
TEST=launch Crosvm on eve and Linux
TEST=cargo test -p devices
TEST=./bin/clippy
TEST=./build_test.py --x86_64-sysroot /build/eve

Change-Id: I5498b15a3bf115e34764e6610407b3ba204dae7f
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
Signed-off-by: Zide Chen <zide.chen@intel.corp-partner.google.com>
Signed-off-by: Sainath Grandhi <sainath.grandhi@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1873356
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Stephen Barber <smbarber@chromium.org>
2019-10-24 20:46:37 +00:00
Chirantan Ekbote
4fad33b679 descriptor_utils: Consume all buffers when reading or writing
The consume function in both the read and write methods should consume
all the VolatileSlices that are given to it rather than just the first
one.  The previous implementation was not wrong, just inefficient.  This
should fix that.

Also add a test to make sure that this doesn't regress in the future.

BUG=none
TEST=unit tests

Change-Id: I02ec22269cdd6cdc329dd62367b99352a4dc1245
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1865271
Tested-by: Chirantan Ekbote <chirantan@chromium.org>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
2019-10-23 03:22:13 +00:00
Zide Chen
aef6e53ed9 virtio-net: reduce number of virtual interrupts to the guest
It's quite costly to inject virtual interrupt to the guest, especially
in INTx case.

To reduce the number of interrupts, in process_rx(), we don't have to
inject interrupt on every frame, but wait until process_rx() finishes
processing all frames.

On eve, iperf3 gets ~15% improvement, "iperf3 -R" gets ~30% improvement.

BUG=chromium:854765
TEST=iperf3 on eve and Linux

Change-Id: Ie0560d8f42235d2371addb6de34c5f93d11a405f
Signed-off-by: Zide Chen <zide.chen@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1865021
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Commit-Queue: Stephen Barber <smbarber@chromium.org>
Tested-by: Stephen Barber <smbarber@chromium.org>
2019-10-22 21:09:33 +00:00
Fletcher Woodruff
95dbad3db6 ac97: add sample format to create_*_stream
The audio_streams interface now supports specifying a sample format.
Update call sites to indicate that the desired format is S16LE.

BUG=chromium:1010667
TEST=aplay within vm

Cq-Depend: chromium:1856646
Change-Id: Ib69ff9b39196905f0f429eaf771f6f92901bfc71
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1856586
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: Fletcher Woodruff <fletcherw@chromium.org>
Commit-Queue: Fletcher Woodruff <fletcherw@chromium.org>
2019-10-22 20:57:08 +00:00
Daniel Verkamp
3be01bd505 devices: vhost: simplify vhost imports
Rather than using `use ::vhost::...` to disambiguate the imports, remove
the conflicting `use virtio_sys::vhost` and add `virtio_sys::` to each
location that used `vhost::...` previously.

The `use ::vhost::...` syntax confuses rustfmt when run directly on
these two files, causing it to rewrite the imports into something that
doesn't actually compile.

BUG=None
TEST=rustfmt --check devices/src/virtio/vhost/net.rs
TEST=rustfmt --check devices/src/virtio/vhost/vsock.rs

Change-Id: I8483f5327a1e2b3ae4887f0b3cef20a917d7410e
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1865370
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
2019-10-21 19:26:03 +00:00
Daniel Verkamp
92568c9c27 Reformat with rustfmt from Rust 1.38.0
This just removes a few extraneous blank lines that the new rustfmt
doesn't like.

BUG=None
TEST=bin/fmt --check

Change-Id: I4482f873bdfe19f2f73f86cfdd99d6cce873593c
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1863000
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2019-10-17 03:35:36 +00:00
Daniel Verkamp
130fbbe71d Add explicit dyn for trait objects
Fix "trait objects without an explicit `dyn` are deprecated" warnings
introduced in Rust 1.38.

BUG=None
TEST=emerge-nami crosvm

Change-Id: I8ca6aa747475268ae898adddd5d091d401326ceb
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1862999
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2019-10-17 03:35:35 +00:00
Daniel Verkamp
6494117e17 usb: replace libusb with Rust usb_util library
Drop the dependency on libusb and reimplement the host USB backend using
usb_sys to wrap the Linux usbdevfs ioctls.

This allows sandboxing to work without any dependency on libusb patches,
and it gives us the flexibility to modify and update the USB backend
without depending on an external third-party library.

BUG=chromium:987833
TEST=`adb logcat` on nami with Nexus 5 attached
TEST=deploy app to phone with Android Studio
TEST=Run EdgeTPU USB accelerator demo (including DFU mode transition)

Cq-Depend: chromium:1773695
Change-Id: I4321c2b6142caac15f48f197795a37d59d268831
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1783601
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2019-10-17 00:20:24 +00:00
Xiong Zhang
bed8b0017d vfio: Add msi support
crosvm doesn't support MSI/MSI-x, but kvmgt vgpu support MSI only
through cfg msi capability. This is a simple msi implementation, it
detects msi capability and track msi control, data and address info, then
call vfio kernel to enable / disable msi interrupt.

Currently it supports one vetor per MSI. It could extend to multi vetors and
MSI-x.

BUG=chromium:992270
TEST=none

Change-Id: I04fc95f23a07f9698237c014d9f909d011f447ef
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1581142
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2019-10-17 00:17:07 +00:00
Daniel Verkamp
04a82c7be1 devices: vfio: fix clippy warnings
Fix boxed_local, const_static_lifetime, useless_format, and
redundant_closure clippy warnings in the VFIO code.

This fixes all clippy warnings except a single instance of
let_and_return in VfioPciDevice::keep_fds(), since that code is modified
in an upcoming patch.

BUG=None
TEST=./build_test.py
TEST=bin/clippy

Change-Id: I548adbc6b92448fc0db82ed72214d73b0eabaf5c
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1822697
Reviewed-by: Xiong  Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: Xiong  Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
2019-10-17 00:17:05 +00:00
Chirantan Ekbote
99e6a6fb1f devices: fs: Add fuse constants and struct definitions
Add the constants and struct definitions from the kernel fuse interface.
These bindings are manually generated from `include/uapi/linux/fuse.h`
in the kernel repo.

BUG=b:136128319
TEST=none;  these aren't used anywhere yet

Change-Id: I03d11bc55eca6b8269f1e63a1187ef458ee16f28
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1705655
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
2019-10-16 06:53:47 +00:00
Chirantan Ekbote
b5964164c4 devices: Refactor DescriptorChainConsumer, Reader, and Writer
Refactor the Reader and Writer implementations for DescriptorChains.
This has several changes:

  * Change the DescriptorChainConsumer to keep a
    VecDeque<VolatileSlice> instead of an iterator.  This delegates the
    fiddly business of sub-slicing chunks of memory to the VolatileSlice
    implementation.
  * Read in the entire DescriptorChain once when the Reader or Writer is
    first constructed.  This allows us to validate the DescriptorChain
    in the beginning rather than having to deal with an invalid
    DescriptorChain in the middle of the device operating on it.
    Combined with the check that enforces the ordering of read/write
    descriptors in a previous change we can be sure that the entire
    descriptor chain that we have copied in is valid.
  * Add a new `split_at` method so that we can split the Reader/Writer
    into multiple pieces, each responsible for reading/writing a
    separate part of the DescriptorChain.  This is particularly useful
    for implementing zero-copy data transfer as we sometimes need to
    write the data first and then update an earlier part of the buffer
    with the number of bytes written.
  * Stop caching the available bytes in the DescriptorChain.  The
    previous implementation iterated over the remaining descriptors in
    the chain and then only updated the cached value.  If a mis-behaving
    guest then changed one of the later descriptors, the cached value
    would no longer be valid.
  * Check for integer overflow when calculating the number of bytes
    available in the chain.  A guest could fill a chain with five 1GB
    descriptors and cause an integer overflow on a 32-bit machine.
    This would previously crash the device process since we compile with
    integer overflow checks enabled but it would be better to return an
    error instead.
  * Clean up the Read/Write impls.  Having 2 different functions called
    `read`, with different behavior is just confusing.  Consolidate on
    the Read/Write traits from `std::io`.
  * Change the `read_to` and `write_from` functions to be generic over
    types that implement `FileReadWriteVolatile` since we are not
    allowed to assume that it's safe to call read or write on something
    just because it implements `AsRawFd`.  Also add `*at` variants that
    read or write to a particular offset rather than the kernel offset.
  * Change the callback passed to the `consume` function of
    `DescriptorChainConsumer` to take a `&[VolatileSlice]` instead.
    This way we can use the `*vectored` versions of some methods to
    reduce the number of I/O syscalls we need to make.
  * Change the `Result` types that are returned.  Functions that perform
    I/O return an `io::Result`.  Functions that only work on guest
    memory return a `guest_memory::Result`.  This makes it easier to
    inter-operate with the functions from `std::io`.
  * Change some u64/u32 parameters to usize to avoid having to convert
    back and forth between the two in various places.

BUG=b:136128319
TEST=unit tests

Change-Id: I15102f7b4035d66b5ce0891df42b656411e8279f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1757240
Auto-Submit: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2019-10-15 18:26:29 +00:00
Daniel Verkamp
8eb944b480 devices: proxy: do not acknowledge write commands
Write accessess cannot fail (in the CommandResult sense) and the result
did not carry any data, so remove the response from the Write command.

This should improve the speed of write requests for sandboxed devices.

For example, with the sandboxed serial device, boot time with a release
build of crosvm on my workstation goes from 1.7 seconds to 1.2 seconds,
measured by timing a boot with a missing init so that the kernel panics
and shuts down immediately.

BUG=None
TEST=time crosvm run -p init=bogus vm_kernel

Change-Id: I125bb831235ca741ae1cc6c86a02a5d863d1a211
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1853970
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2019-10-14 23:59:49 +00:00
Zach Reizner
a8adff0ff1 devices: jail serial device
This change plumbs the jail throughout the arch specific device creation
process. It also adds a custom callback support for the ProxyDevice so
that the main process can interrupt the child serial process when it has
incoming bytes.

TEST=crosvm run
BUG=None

Change-Id: I6af7d2cb0acbba9bf42eaeeb294cee2bce4a1f36
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1752589
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Zach Reizner <zachr@chromium.org>
Commit-Queue: Zach Reizner <zachr@chromium.org>
2019-10-10 02:09:13 +00:00
paulhsia
0b6f02fea7 device: ac97: Cleanup interrupt logic
- Send only one event while re-sampling.
- Don't sent event if the new sr is identical to the old one

This can reduce the rate to trigger the issue.

BUG=chromium:937977
TEST=Build and run lots of aplay and arecord in guest vm

Change-Id: Ibd21f363076c977ae256079e2615094b7ed2408b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1840752
Tested-by: Chih-Yang Hsia <paulhsia@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Commit-Queue: Chih-Yang Hsia <paulhsia@chromium.org>
2019-10-08 11:53:24 +00:00
Stephen Barber
7595d80248 devices: virtio_net: handle errors in tapfd poll adding/removal
Add error handling for adding/removing the tapfd to epoll.

We only remove the tap fd from the poll context if the tap is
readable, i.e. it would busy loop, so don't assume it's removed
from the poll context when there's a deferred rx frame.

BUG=chromium:1010742
TEST=arcvm network works

Change-Id: I84aab2dbe7ea31d724f04d3b3fb0a6916f232300
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1842399
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Commit-Queue: Stephen Barber <smbarber@chromium.org>
2019-10-08 08:55:59 +00:00
Daniel Verkamp
04e9c03f03 devices: ac97: fix interrupt triggering logic
Do not write to the irq_evt eventfd when interrupt_high is false; the
value written to the eventfd is ignored, so despite the '0' in the call,
this would re-trigger the interrupt even when it should not have been
asserted.  Since ac97 is a PCI device, its interrupt is level triggered,
and re-asserting it on EOI is handled by the irq_resample_thread code.

BUG=None
TEST=`aplay /dev/urandom` from Crostini on nami

Change-Id: I6ad8e40b818e0495ad58b6902d88dd61103aed9d
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1838762
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Chih-Yang Hsia <paulhsia@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2019-10-08 01:57:20 +00:00
Chirantan Ekbote
41e61d1fc3 virtio: queue: Enforce DescriptorChain ordering
The virtio spec requires that all read-only descriptors appear in the
chain before any write-only descriptors.  Enforce this in the
`checked_new` function by adding a new `required_flags` parameter.  The
`next_descriptor` function will set this to `VIRTQ_DESC_F_WRITE` if the
current descriptor is write-only.  This ensures that once we see a
write-only descriptor, all following descriptors must be write-only.

BUG=b:136127316
TEST=none

Change-Id: Id8f942a4236a20f62f35439f3648dbec17e14c00
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1757239
Auto-Submit: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
2019-10-06 05:35:47 +00:00
Stephen Barber
28a9d2b70d devices: virtio_net: avoid busylooping when no rx buffers available
If the guest is unable to return rx queue buffers to the device, we should
temporarily stop polling for reads on the tap fd. Otherwise, we'll spin and
burn CPU needlessly.

BUG=chromium:1010742
TEST=repro from b/141940546

Change-Id: Iac004e870779a8dd39004f44b44e17a2b45bcfa1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1836914
Tested-by: Stephen Barber <smbarber@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Stephen Barber <smbarber@chromium.org>
2019-10-03 19:44:37 +00:00
Daniel Verkamp
b5237bbcf0 devices: virtio: pmem: use descriptor reader/writer
Convert the virtio pmem device to use the descriptor_utils Reader/Writer
helpers to simplify the code and allow support of arbitrary descriptor
layouts.

BUG=chromium:966258
TEST=./build_test.py

Change-Id: I9ccbdf2833980e4c44e19975f9091f9aea56c94b
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1811713
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
2019-10-01 22:48:41 +00:00
Daniel Verkamp
42cd8b1472 devices: virtio: input: use descriptor reader/writer
Convert the virtio input device to use the descriptor_utils
Reader/Writer helpers to simplify the code and allow support of
arbitrary descriptor layouts.

BUG=chromium:966258
TEST=./build_test.py

Change-Id: Ia9272496dc59b29ea9cde9f6454099c881242d4c
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1811712
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
2019-10-01 22:48:40 +00:00
Jakub Staron
d9c4398a31 crosvm: Use Reader/Writer interfaces in various virtio devices.
Switching the devices to the new interface reduces code duplication and
will ease fuzzing the devices as they now have a common input and output
interface for descriptors.

BUG=chromium:966258
TEST=vm.CrostiniStartEverything

Change-Id: I823c04dfc24e017433f8e8ab167bbd5dfafd338b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1647371
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2019-10-01 22:48:38 +00:00
Gurchetan Singh
719f2831ed virtio-gpu: handle VIRTIO_GPU_CMD_RESOURCE_CREATE_V2
BUG=chromium:924405
TEST=compile

Change-Id: I57379452f6805aaf429c268b95ddd3aecd07e90e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1591463
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Commit-Queue: Gurchetan Singh <gurchetansingh@chromium.org>
2019-10-01 07:53:34 +00:00
Gurchetan Singh
5b636babc1 virtio-gpu: handle VIRTIO_GPU_CMD_ALLOCATION_METADATA
Designed to return metadata to the guest.

BUG=chromium:924405
TEST=compile

Change-Id: Ic04a0bbb6b0a5bb6d08314371181f256eb7230df
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1591464
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Commit-Queue: Gurchetan Singh <gurchetansingh@chromium.org>
2019-10-01 07:53:33 +00:00
Xiong Zhang
c554fff206 vfio: Setup dma map for vfio device
For each guest memory region, setup the corresponding gpa to hva map
in the kernel vfio iommu table. Then the kernel vfio driver could
get the hpa through gpa. Device could use this gpa for dma also.

BUG=chromium:992270
TEST=none

Change-Id: I04008d68ab2ed182a789d6ee8c97a0ed9e1e4756
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1581141
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2019-10-01 03:59:49 +00:00
Xiong Zhang
17b0daf88c vfio: Integrate VFIO device into pci device model
Create VFIO device and VFIO PCI device in create_devices() function, and
intergrate it into PciRootBridge, so guest could see this vfio device.

Add a vfio config parameter, this config point to passthrough or mdev
device sysfs path.

For passthrough case, first user unbind host device from its driver,
then bind host device to vfio-pci. Like:
echo 0000:00:02.0 > /sys/bus/pci/devices/0000:00:02.0/driver/unbind
ech0 8086 1912 > /sys/bus/pci/drivers/vfio-pci/new_id
Finally pass the sysfs to crosvm through
--vfio=/sys/bus/pci/devices/0000:00:02.0

For mdev case, user create a mdev device through
echo $UUID > mdev_type/create, then pass this mdev device to crosvm like
--vfio=/sys/bus/pci/devices/0000:00:02.0/$UUID

BUG=chromium:992270
TEST=none

Change-Id: I0f59d6e93f62f9ab0727ad3a867d204f4ff6ad2d
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1581140
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2019-10-01 03:59:47 +00:00
Daniel Verkamp
71a6f0a790 sys_util: add write_zeroes_all() function
In the same spirit as write_all() for the standard io::Write::write()
function, add a write_zeroes_all() function with a default
implementation that calls write_zeroes() in a loop until the requested
length is met.  This will allow write_zeroes implementations that don't
necessarily fulfill the entire requested length.

BUG=None
TEST=cargo test -p sys_util write_zeroes

Change-Id: I0fc3a4b3fe8904946e253ab8a2687555b12657be
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1811466
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Cody Schuffelen <schuffelen@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
2019-09-25 00:06:27 +00:00
Gurchetan Singh
b2d4e11579 devices: modify get_device_bars(..) interface
Alloc::PciBar {..} is used as a key in the AddressAllocator's
hashmap, so inform the device about the pci bus/dev numbers.

BUG=chromium:924405
TEST=compile

Change-Id: Ib9d94e516269c1dc9a375c2ceb9775cf5a421156
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1811585
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Commit-Queue: Gurchetan Singh <gurchetansingh@chromium.org>
2019-09-20 16:55:31 +00:00
Gurchetan Singh
aa781bafdf virtio-gpu: Use new + proposed virtio-gpu features
BUG=chromium:924405
TEST=compile

Change-Id: If7f2d35f66664a1a4e9a6e0883ea981b0ac3e87c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1591462
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: David Riley <davidriley@chromium.org>
2019-09-20 16:55:30 +00:00
Gurchetan Singh
45f86fd548 virtio-gpu: add new + proposed kernel protocols
BUG=chromium:924405
TEST=compile

Change-Id: I550d5e60e8bde1378c226fc9da61eb60f1293811
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1591461
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: David Riley <davidriley@chromium.org>
2019-09-20 16:55:29 +00:00