|
| 1 | +import pytest |
| 2 | + |
| 3 | +import logging |
| 4 | +from contextlib import contextmanager |
| 5 | + |
| 6 | +from lib.bond import Bond |
| 7 | +from lib.host import Host |
| 8 | +from lib.network import Network |
| 9 | +from lib.vm import VM |
| 10 | + |
| 11 | +# Requirements: |
| 12 | +# From --hosts parameter: |
| 13 | +# - host(A1): first XCP-ng host, with at least 3 NICs (1 management, 2 unused), with tcpdump |
| 14 | +# From --vm parameter |
| 15 | +# - A VM to import (alpine expected) |
| 16 | + |
| 17 | +@contextmanager |
| 18 | +def managed_bond(host: Host, network: Network, devices: list[str], mode: str): |
| 19 | + pifs = [] |
| 20 | + for name in devices: |
| 21 | + [pif] = host.PIFs(device=name) |
| 22 | + pifs.append(pif) |
| 23 | + |
| 24 | + bond = Bond.create(host, network, pifs, mode=mode) |
| 25 | + try: |
| 26 | + yield bond |
| 27 | + finally: |
| 28 | + bond.destroy() |
| 29 | + |
| 30 | +@pytest.mark.small_vm |
| 31 | +class TestNetwork: |
| 32 | + @pytest.mark.no_vm |
| 33 | + def test_basic(self, host: Host, empty_network: Network): |
| 34 | + assert len(empty_network.PIF_uuids()) == 0, "unexpected number of PIFs in network" |
| 35 | + assert len(empty_network.VIF_uuids()) == 0, "unexpected number of VIFs in network" |
| 36 | + assert empty_network.is_private(), "empty_network isn't private" |
| 37 | + assert empty_network.MTU() == 1500, "unexpected MTU" |
| 38 | + |
| 39 | + def test_private_network(self, host: Host, empty_network: Network, imported_vm: VM): |
| 40 | + network = empty_network |
| 41 | + |
| 42 | + try: |
| 43 | + vm1 = imported_vm.clone() |
| 44 | + vm2 = imported_vm.clone() |
| 45 | + |
| 46 | + vif_1_1 = vm1.create_vif(1, network_uuid=network.uuid) |
| 47 | + vif_2_1 = vm2.create_vif(1, network_uuid=network.uuid) |
| 48 | + |
| 49 | + assert len(vm1.vifs()) == 2, "VM1 should have 2 NICs" |
| 50 | + assert len(vm2.vifs()) == 2, "VM2 should have 2 NICs" |
| 51 | + assert len(network.VIF_uuids()) == 2, "unexpected number of VIFs in network" |
| 52 | + |
| 53 | + vm1.start() |
| 54 | + vm2.start() |
| 55 | + |
| 56 | + vm1.wait_for_vm_running_and_ssh_up() |
| 57 | + vm2.wait_for_vm_running_and_ssh_up() |
| 58 | + |
| 59 | + logging.info("Configuring local address on private network") |
| 60 | + vm1.ssh(f"ifconfig eth{vif_1_1.param_get('device')} inet 169.254.1.1 broadcast 169.254.0.0 up") |
| 61 | + vm2.ssh(f"ifconfig eth{vif_2_1.param_get('device')} inet 169.254.2.1 broadcast 169.254.0.0 up") |
| 62 | + |
| 63 | + logging.info("Ping VMs") |
| 64 | + assert vm1.ssh_with_result(["ping", "-c3", "-w5", "169.254.2.1"]).returncode == 0 |
| 65 | + assert vm2.ssh_with_result(["ping", "-c3", "-w5", "169.254.1.1"]).returncode == 0 |
| 66 | + |
| 67 | + finally: |
| 68 | + # VIFs are destroyed by VM.destroy() |
| 69 | + vm2.destroy() |
| 70 | + vm1.destroy() |
| 71 | + |
| 72 | +def _wait_for_packet(host: Host | VM, interface: str, sfilter: str, timeout: int = 30) -> None: |
| 73 | + ret = host.ssh_with_result(f"timeout {timeout} tcpdump -i {interface} -n -c1 '{sfilter}'") |
| 74 | + if ret.returncode != 0: |
| 75 | + pytest.fail(f"tcpdump error: code={ret.returncode} stdout={ret.stdout}") |
| 76 | + return None |
| 77 | + |
| 78 | +@pytest.mark.complex_prerequisites |
| 79 | +@pytest.mark.small_vm |
| 80 | +@pytest.mark.parametrize("mode", ["lacp", "active-backup", "balance-slb"]) |
| 81 | +def test_bond(host: Host, empty_network: Network, imported_vm: VM, mode): |
| 82 | + network = empty_network |
| 83 | + assert len(network.PIF_uuids()) == 0 |
| 84 | + |
| 85 | + assert host.ssh_with_result("which tcpdump").returncode == 0, \ |
| 86 | + "host should have tcpdump installed" |
| 87 | + |
| 88 | + # expect host with eth1 and eth2 NICs free for use |
| 89 | + with managed_bond(host, network, ["eth1", "eth2"], mode) as bond: |
| 90 | + logging.info(f"Bond = {bond.uuid} mode={bond.mode()} slaves={bond.slaves()}") |
| 91 | + |
| 92 | + # disable LACP fallback (make Bond to require LACP negociation first) |
| 93 | + if mode == "lacp": |
| 94 | + bond.param_set("properties", key="lacp-fallback-ab", value=False) |
| 95 | + |
| 96 | + # check bond0 on the host |
| 97 | + ret = host.ssh_with_result("ovs-appctl bond/show bond0") |
| 98 | + if ret.returncode != 0: |
| 99 | + pytest.fail(f"ovs-appctl bond/show failed: exitcode={ret.returncode} stdout={ret.stdout}") |
| 100 | + |
| 101 | + if mode == 'lacp': |
| 102 | + bond_mode = 'balance-tcp' |
| 103 | + else: |
| 104 | + bond_mode = mode |
| 105 | + |
| 106 | + if f"bond_mode: {bond_mode}" not in ret.stdout: |
| 107 | + pytest.fail(f"unexpected bond_mode: {mode}: stdout={ret.stdout}") |
| 108 | + |
| 109 | + if mode == 'lacp': |
| 110 | + if "lacp_fallback_ab: false" not in ret.stdout: |
| 111 | + pytest.fail(f"unexpected lacp_fallback_ab: stdout={ret.stdout}") |
| 112 | + elif "lacp_status: configured" not in ret.stdout: |
| 113 | + pytest.fail(f"unexpected lacp_status: stdout={ret.stdout}") |
| 114 | + else: |
| 115 | + if "lacp_status: off" not in ret.stdout: |
| 116 | + pytest.fail(f"unexpected lacp_status: stdout={ret.stdout}") |
| 117 | + |
| 118 | + try: |
| 119 | + # on the VM, add a new NIC using the bond |
| 120 | + vm = imported_vm.clone() |
| 121 | + vm.create_vif(1, network_uuid=bond.master().network_uuid()) |
| 122 | + vm.start() |
| 123 | + vm.wait_for_vm_running_and_ssh_up() |
| 124 | + |
| 125 | + vm.ssh("apk add tcpdump") |
| 126 | + vm.ssh("ip link set eth1 up") |
| 127 | + |
| 128 | + if mode == 'lacp': |
| 129 | + # we are checking if we are seeing LACP packet on VM side. |
| 130 | + # |
| 131 | + # OpenvSwitch will send LACP negociation on host.eth1 and host.eth2 |
| 132 | + # to etablish LACP link. As we don't have other side, it will keep |
| 133 | + # sending them. |
| 134 | + # On VM side, we could see such packets. So we are checking the VM.eth1 |
| 135 | + # is properly connected to the Bond just created (but we don't check that |
| 136 | + # OpenvSwitch is properly setup) |
| 137 | + logging.info("Waiting for LACP packet") |
| 138 | + _wait_for_packet(vm, "eth1", "ether proto 0x8809") |
| 139 | + |
| 140 | + else: |
| 141 | + # just check if we see a packet on the host side. |
| 142 | + # theVM kernel is expected to send IPv6 packet for Router Solicitation |
| 143 | + # as the VM interface is UP. |
| 144 | + logging.info("Waiting for some IPv6 packet") |
| 145 | + _wait_for_packet(host, "eth1", "ether proto 0x86dd") |
| 146 | + |
| 147 | + finally: |
| 148 | + vm.destroy() |
0 commit comments