|
| 1 | +#!/usr/bin/env python3 |
| 2 | +r"""Ling Aggregation Basic |
| 3 | +
|
| 4 | +Verify communication over a link aggregate in static and LACP operating |
| 5 | +modes during basic failure scenarios. |
| 6 | +
|
| 7 | +.Internal network setup, PC verifies connectivity with dut2 via dut1 |
| 8 | +image::lag-basic.svg[Internal networks] |
| 9 | +
|
| 10 | +The host verifies connectivity with dut2 via dut1 over the aggregate for |
| 11 | +each test step using the `mon` interface. |
| 12 | +
|
| 13 | +""" |
| 14 | +from time import sleep, time |
| 15 | +from datetime import datetime |
| 16 | +import infamy |
| 17 | +from infamy.util import parallel, until |
| 18 | + |
| 19 | + |
| 20 | +class DumbLinkBreaker: |
| 21 | + """Encapsulates basic, dumb link-breaking ops over SSH.""" |
| 22 | + |
| 23 | + def __init__(self, sys, dut, netns): |
| 24 | + self.env = sys |
| 25 | + self.dut = dut |
| 26 | + self.net = netns |
| 27 | + self.tgt = {} |
| 28 | + for i, (name, _) in dut.items(): |
| 29 | + self.tgt[i] = env.attach(name, "mgmt", "ssh") |
| 30 | + |
| 31 | + def set_link(self, link, updown): |
| 32 | + """Set link up or down, verify before returning.""" |
| 33 | + def set_and_verify(i): |
| 34 | + name, dut = self.dut[i] |
| 35 | + |
| 36 | + cmd = self.tgt[i].runsh(f"sudo ip link set {dut[link]} {updown}") |
| 37 | + if cmd.returncode: |
| 38 | + for out in [cmd.stdout, cmd.stderr]: |
| 39 | + if out: |
| 40 | + print(f"{name}: {out.rstrip()}") |
| 41 | + raise RuntimeError(f"{name}: failed setting {link} {updown}") |
| 42 | + |
| 43 | + for _ in range(10): |
| 44 | + sleep(0.1) |
| 45 | + check = self.tgt[i].runsh(f"ip link show {dut[link]}") |
| 46 | + if f"state {updown.upper()}" in check.stdout: |
| 47 | + break |
| 48 | + else: |
| 49 | + raise RuntimeError(f"{name}: {dut[link]} did not go {updown}") |
| 50 | + |
| 51 | + parallel(*[lambda i=i: set_and_verify(i) for i in self.dut]) |
| 52 | + |
| 53 | + def fail_check(self, peer): |
| 54 | + """Verify connectivity with peer during link failure.""" |
| 55 | + sequence = [ |
| 56 | + [("link1", "up"), ("link2", "up")], |
| 57 | + [("link1", "down"), ("link2", "up")], |
| 58 | + [("link1", "up"), ("link2", "down")], |
| 59 | + [("link1", "up"), ("link2", "up")] |
| 60 | + ] |
| 61 | + |
| 62 | + total_start = time() |
| 63 | + for state in sequence: |
| 64 | + state_start = time() |
| 65 | + print(f"{datetime.now().strftime('%H:%M:%S.%f')[:-3]} {state}") |
| 66 | + |
| 67 | + for link, updown in state: |
| 68 | + self.set_link(link, updown) |
| 69 | + self.net.must_reach(peer, timeout=10) |
| 70 | + |
| 71 | + print(f"Completed in {time() - state_start:.2f}s") |
| 72 | + |
| 73 | + print(f"Total time: {time() - total_start:.2f}s") |
| 74 | + |
| 75 | + |
| 76 | +def lag_init(mode): |
| 77 | + """Set up mode specific attributes for the LAG""" |
| 78 | + if mode == "lacp": |
| 79 | + lag = [{ |
| 80 | + "name": "lag0", |
| 81 | + "lag": {"lacp": {"rate": "fast"}} |
| 82 | + }] |
| 83 | + else: |
| 84 | + lag = [] |
| 85 | + return lag |
| 86 | + |
| 87 | + |
| 88 | +def net_init(host, addr): |
| 89 | + """Set up DUT network, dut1 bridges host port with lag0""" |
| 90 | + if host: |
| 91 | + net = [{ |
| 92 | + "name": "br0", |
| 93 | + "type": "infix-if-type:bridge", |
| 94 | + }, { |
| 95 | + "name": host, |
| 96 | + "bridge-port": {"bridge": "br0"} |
| 97 | + }, { |
| 98 | + "name": "lag0", |
| 99 | + "bridge-port": {"bridge": "br0"} |
| 100 | + }] |
| 101 | + else: |
| 102 | + net = [{ |
| 103 | + "name": "lag0", |
| 104 | + "ipv4": { |
| 105 | + "address": [{"ip": addr, "prefix-length": 24}] |
| 106 | + } |
| 107 | + }] |
| 108 | + return net |
| 109 | + |
| 110 | + |
| 111 | +def dut_init(dut, mode, addr): |
| 112 | + """Set up link aggregate on dut""" |
| 113 | + net = net_init(dut["mon"], addr) |
| 114 | + lag = lag_init(mode) |
| 115 | + |
| 116 | + dut.put_config_dict("ietf-interfaces", { |
| 117 | + "interfaces": { |
| 118 | + "interface": [{ |
| 119 | + "name": "lag0", |
| 120 | + "type": "infix-if-type:lag", |
| 121 | + "lag": { |
| 122 | + "mode": mode, |
| 123 | + "link-monitor": {"interval": 100} |
| 124 | + } |
| 125 | + }, { |
| 126 | + "name": dut["link1"], |
| 127 | + "lag-port": {"lag": "lag0"} |
| 128 | + }, { |
| 129 | + "name": dut["link2"], |
| 130 | + "lag-port": {"lag": "lag0"} |
| 131 | + }] + net + lag |
| 132 | + } |
| 133 | + }) |
| 134 | + |
| 135 | + |
| 136 | +with infamy.Test() as test: |
| 137 | + with test.step("Set up topology and attach to target DUTs"): |
| 138 | + env = infamy.Env() |
| 139 | + dut1 = env.attach("dut1", "mgmt") |
| 140 | + dut2 = env.attach("dut2", "mgmt") |
| 141 | + |
| 142 | + _, mon = env.ltop.xlate("host", "mon") |
| 143 | + with infamy.IsolatedMacVlan(mon) as ns: |
| 144 | + dm = { |
| 145 | + '1': ("dut1", dut1), |
| 146 | + '2': ("dut2", dut2) |
| 147 | + } |
| 148 | + lb = DumbLinkBreaker(env, dm, ns) |
| 149 | + ns.addip("192.168.2.1") |
| 150 | + |
| 151 | + with test.step("Set up LACP link aggregate, lag0, on dut1 and dut2"): |
| 152 | + parallel(lambda: dut_init(dut1, "lacp", None), |
| 153 | + lambda: dut_init(dut2, "lacp", "192.168.2.42")) |
| 154 | + |
| 155 | + with test.step("Verify failure modes for lacp mode"): |
| 156 | + lb.fail_check("192.168.2.42") |
| 157 | + |
| 158 | + with test.step("Set up static link aggregate, lag0, on dut1 and dut2"): |
| 159 | + parallel(lambda: dut_init(dut1, "static", None), |
| 160 | + lambda: dut_init(dut2, "static", "192.168.2.42")) |
| 161 | + |
| 162 | + with test.step("Verify failure modes for static mode"): |
| 163 | + lb.fail_check("192.168.2.42") |
| 164 | + |
| 165 | + test.succeed() |
0 commit comments