Make sure proxy child threads are finished when proxying is done

Currently socket_vsock_proxy is a long-living process that
observes the kernel events and starts or stops a proxy thread
depending on them. This proxy thread is listening for incoming connections and spawning 2 copying threads for every
such connection:

1. Client to target copying
2. Target to client copying

But when the main proxy thread is ended there is a
possibility that copying threads are still alive and
continue work with previously connected clients which
may affect the adb / fastboot functionality.

In our case the following may happen:

1. CVD is booted into fastboot
2. Fastboot socket_vsock_proxy establish proxying
6520 <-> fastboot_ethernet
3. CVD is re-booted into Android
4. Adb socket_vsock_proxy is started to proxy
6520 <-> adb_vsock, but old fastboot copying threads
are still alive (even though fastboot proxy thread is finished),
so adb daemon may reuse the old connection, which proxying
to the wrong target.

This problem is mainly reproducible with acloud remote device
following the:

1. Boot cvd
2. Reboot into fastbootd
3. Reboot into Android again

Then adb device will be offline before we call
adb disconnect && adb connect. Just calling
adb connect wouldn't help since it will not open a new
connection because existing one is alive.

To fix that,

1. Allowed FileInstance::CopyAllFrom to be interrupted
by event fd and reused stop_fd_, so all copying threads
will be interrupted together with the main thread
2. Introduced ProxyPair entity which allows us to keep
track of active and finished copying threads

Bug: 303236495
Test: atest FastbootRebootTest --rerun-until-failure 100
with a local cvd / remote acloud
Test: 500 runs of FastbootRebootTest using abtd on acloud

Change-Id: I3bf9ead5b988f0d928681cb3d125b0111506445f
Signed-off-by: Dmitrii Merkurev <dimorinny@google.com>
3 files changed
tree: f7ea9ac50b799fbb68d4f90ea90eeae330fb0dbd
  1. .vscode/
  2. apex/
  3. build/
  4. common/
  5. guest/
  6. host/
  7. recovery/
  8. shared/
  9. tests/
  10. tools/
  11. vsoc_arm64/
  12. vsoc_arm64_minidroid/
  13. vsoc_arm64_only/
  14. vsoc_arm64_pgagnostic/
  15. vsoc_arm_minidroid/
  16. vsoc_riscv64/
  17. vsoc_riscv64_minidroid/
  18. vsoc_x86/
  19. vsoc_x86_64/
  20. vsoc_x86_64_host/
  21. vsoc_x86_64_minidroid/
  22. vsoc_x86_64_only/
  23. vsoc_x86_64_pgagnostic/
  24. vsoc_x86_only/
  25. .clang-format
  26. Android.bp
  27. Android.mk
  28. AndroidProducts.mk
  29. CleanSpec.mk
  30. default-permissions.xml
  31. dtb.img
  32. fetcher.mk
  33. host_package.mk
  34. iwyu.imp
  35. METADATA
  36. OWNERS
  37. PREUPLOAD.cfg
  38. README.md
  39. required_images
  40. TEST_MAPPING
README.md

Cuttlefish Getting Started

Try Cuttlefish

  1. Make sure virtualization with KVM is available.

    grep -c -w "vmx\|svm" /proc/cpuinfo
    

    This should return a non-zero value. If running on a cloud machine, this may take cloud-vendor-specific steps to enable. For Google Compute Engine specifically, see the GCE guide.

ARM specific steps:

  • When running on an ARM machine, the most direct way is to check for the existence of /dev/kvm. Note that this method can also be used to confirm support of KVM on any environment.
  1. Download, build, and install the host debian packages:

    sudo apt install -y git devscripts config-package-dev debhelper-compat golang curl
    git clone https://github.com/google/android-cuttlefish
    cd android-cuttlefish
    for dir in base frontend; do
      cd $dir
      debuild -i -us -uc -b -d
      cd ..
    done
    sudo dpkg -i ./cuttlefish-base_*_*64.deb || sudo apt-get install -f
    sudo dpkg -i ./cuttlefish-user_*_*64.deb || sudo apt-get install -f
    sudo usermod -aG kvm,cvdnetwork,render $USER
    sudo reboot
    

    The reboot will trigger installing additional kernel modules and applying udev rules.

  2. Go to http://ci.android.com/

  3. Enter a branch name. Start with aosp-main if you don‘t know what you’re looking for

  4. Navigate to aosp_cf_x86_64_phone and click on userdebug for the latest build

For ARM, use branch aosp-main-throttled and device target aosp_cf_arm64_only_phone-trunk_staging-userdebug
  1. Click on Artifacts

  2. Scroll down to the OTA images. These packages look like aosp_cf_x86_64_phone-img-xxxxxx.zip -- it will always have img in the name. Download this file

  3. Scroll down to cvd-host_package.tar.gz. You should always download a host package from the same build as your images.

  4. On your local system, combine the packages:

    mkdir cf
    cd cf
    tar xvf /path/to/cvd-host_package.tar.gz
    unzip /path/to/aosp_cf_x86_64_phone-img-xxxxxx.zip
    
  5. Launch cuttlefish with:

$ HOME=$PWD ./bin/launch_cvd

Debug Cuttlefish

You can use adb to debug it, just like a physical device:

$ ./bin/adb -e shell

Launch Viewer (WebRTC)

When launching with ---start_webrtc (the default), you can see a list of all available devices at https://localhost:8443 . For more information, see the WebRTC on Cuttlefish documentation.

Stop Cuttlefish

You will need to stop the virtual device within the same directory as you used to launch the device.

$ HOME=$PWD ./bin/stop_cvd