Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions fboss/cli/fboss2/test/integration_test/TunnelIpInIpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* - FBOSS agent must be running with a valid configuration
*/

#include <fmt/format.h>
#include <folly/ScopeGuard.h>
#include <folly/json/dynamic.h>
#include <folly/logging/xlog.h>
#include <gtest/gtest.h>
Expand Down Expand Up @@ -90,6 +92,131 @@ class TunnelIpInIpTest : public Fboss2IntegrationTest {
createdTunnels_.emplace_back(id, direction);
}

// Shared body of the destination/source mask tests. Creates a `direction`
// ("encap" or "decap") tunnel whose `attr` ("destination" or "source")
// carries `mask /64`, then checks where the agent put the mask, and removes
// the tunnel and the underlay VLAN it created.
//
// The CLI stores the prefix length as an IPv6 mask address in dstIpMask or
// srcIpMask. For encap the agent copies both fields straight into switch
// state; for decap it swaps them, because config dstIp is the termination
// match and is kept as srcIp in switch state. The other mask must stay unset
// in both places, so a mask written to the wrong field fails the test.
void checkTunnelMask(const std::string& direction, const std::string& attr) {
const bool encap = direction == "encap";
const bool onDst = attr == "destination";
const std::string cfgMask = onDst ? "dstIpMask" : "srcIpMask";
const std::string cfgOther = onDst ? "srcIpMask" : "dstIpMask";
// Decap swaps directions in switch state.
const bool stateOnDst = encap == onDst;
const std::string stateMask = stateOnDst ? "dstIpMask" : "srcIpMask";
const std::string stateOther = stateOnDst ? "srcIpMask" : "dstIpMask";
const std::string stateIp = stateOnDst ? "dstIp" : "srcIp";
const std::string mask = "ffff:ffff:ffff:ffff::"; // /64

int intfId = ensureUnderlayIntfId();
const std::string dstIp = findIpv6OnIntf(intfId);
const std::string srcIp = "2001:db8:cafe::5";
const std::string tunnelId = fmt::format(
"integration-test-{}-{}mask", direction, onDst ? "dst" : "src");
const std::string& maskedIp = onDst ? dstIp : srcIp;

// Drop the tunnel, then the underlay VLAN and its interface that
// ensureUnderlayIntfId created. Armed as a guard so a failed check still
// leaves the DUT as found; runs explicitly in Step 3.
auto cleanup = [&]() {
try {
if (runCli({"delete", "tunnel", "ip-in-ip", direction, tunnelId})
.exitCode == 0) {
commitConfig();
}
auto config = getRunningConfig();
for (const auto& intf : config["sw"]["interfaces"]) {
const auto* id = intf.get_ptr("intfID");
const auto* vlan = intf.get_ptr("vlanID");
if (id != nullptr && vlan != nullptr && id->asInt() == intfId) {
deleteVlanIfPresent(static_cast<int>(vlan->asInt()));
}
}
EXPECT_FALSE(findTunnelInRunningConfig(tunnelId).has_value())
<< "Tunnel '" << tunnelId << "' still present after delete";
auto after = getRunningConfig();
for (const auto& intf : after["sw"]["interfaces"]) {
const auto* id = intf.get_ptr("intfID");
EXPECT_TRUE(id == nullptr || id->asInt() != intfId)
<< "underlay interface " << intfId << " left behind";
}
} catch (const std::exception& e) {
ADD_FAILURE() << "cleanup failed: " << e.what();
}
};
auto cleanupGuard = folly::makeGuard(cleanup);

std::vector<std::string> cmd{
"config",
"tunnel",
"ip-in-ip",
direction,
tunnelId,
"destination",
dstIp};
if (onDst) {
cmd.insert(cmd.end(), {"mask", "64"});
}
if (encap || !onDst) {
cmd.insert(cmd.end(), {"source", srcIp});
}
if (!onDst) {
cmd.insert(cmd.end(), {"mask", "64"});
}
if (!encap) {
// ttl-mode pipe: see DeleteDecapTunnel.
cmd.insert(cmd.end(), {"ttl-mode", "pipe"});
}
cmd.insert(cmd.end(), {"underlay-intf-id", std::to_string(intfId)});

XLOG(INFO) << "[Step 1] Creating " << direction << " tunnel " << tunnelId
<< " with " << attr << " mask /64...";
auto result = runCli(cmd);
ASSERT_EQ(result.exitCode, 0) << "Create failed: " << result.stderr;
commitConfig();
trackTunnel(tunnelId, direction);

XLOG(INFO) << "[Step 2] Verifying the mask in running config and state...";
auto tunnel = waitForTunnelInRunningConfig(tunnelId, /*shouldExist=*/true);
ASSERT_TRUE(tunnel.has_value())
<< "Tunnel '" << tunnelId << "' not found in running config";
ASSERT_TRUE(tunnel->count(cfgMask))
<< cfgMask << " missing from running config";
EXPECT_EQ((*tunnel)[cfgMask].asString(), mask);
EXPECT_FALSE(tunnel->count(cfgOther))
<< cfgOther << " set without being configured";

// ipTunnelMaps: SwitchIdList -> tunnel id -> IpTunnelFields.
auto tunnelMaps = getSwitchState("ipTunnelMaps");
int found = 0;
for (const auto& [switchIds, tunnels] : tunnelMaps.items()) {
const auto* state = tunnels.get_ptr(tunnelId);
if (state == nullptr) {
continue;
}
++found;
EXPECT_EQ((*state)[stateIp].asString(), maskedIp);
const auto* stateMaskVal = state->get_ptr(stateMask);
ASSERT_NE(stateMaskVal, nullptr)
<< stateMask << " missing from " << direction << " switch state";
EXPECT_EQ(stateMaskVal->asString(), mask);
EXPECT_EQ(state->get_ptr(stateOther), nullptr)
<< direction << " " << attr << " mask landed in switch state "
<< stateOther;
}
EXPECT_GT(found, 0) << "tunnel '" << tunnelId << "' not in switch state";

XLOG(INFO) << "[Step 3] Deleting tunnel and underlay VLAN...";
cleanupGuard.dismiss();
cleanup();
}

private:
// (tunnelId, direction) pairs to clean up in TearDown.
std::vector<std::pair<std::string, std::string>> createdTunnels_;
Expand Down Expand Up @@ -151,6 +278,24 @@ TEST_F(TunnelIpInIpTest, CreateDecapTunnelAndVerifyRunningConfig) {
static_cast<int>(cfg::TunnelTerminationType::P2MP));
}

// ---------------------------------------------------------------------------
// Positive: 'destination <ip> mask <len>' on a DECAP tunnel lands in the
// switch state's srcIpMask (decap swaps directions).
// ---------------------------------------------------------------------------

TEST_F(TunnelIpInIpTest, DecapDestinationMask) {
checkTunnelMask("decap", "destination");
}

// ---------------------------------------------------------------------------
// Positive: 'source <ip> mask <len>' on a DECAP tunnel lands in the switch
// state's dstIpMask (decap swaps directions).
// ---------------------------------------------------------------------------

TEST_F(TunnelIpInIpTest, DecapSourceMask) {
checkTunnelMask("decap", "source");
}

// ---------------------------------------------------------------------------
// Positive: create an ENCAP tunnel and verify tunnelType == IP_IN_IP_ENCAP in
// the agent's running config.
Expand Down Expand Up @@ -193,6 +338,24 @@ TEST_F(TunnelIpInIpTest, CreateEncapTunnelAndVerifyRunningConfig) {
static_cast<int>(TunnelType::IP_IN_IP_ENCAP));
}

// ---------------------------------------------------------------------------
// Positive: 'destination <ip> mask <len>' on an ENCAP tunnel lands in the
// switch state's dstIpMask.
// ---------------------------------------------------------------------------

TEST_F(TunnelIpInIpTest, EncapDestinationMask) {
checkTunnelMask("encap", "destination");
}

// ---------------------------------------------------------------------------
// Positive: 'source <ip> mask <len>' on an ENCAP tunnel lands in the switch
// state's srcIpMask.
// ---------------------------------------------------------------------------

TEST_F(TunnelIpInIpTest, EncapSourceMask) {
checkTunnelMask("encap", "source");
}

// ---------------------------------------------------------------------------
// Positive: create then delete a decap tunnel; verify it disappears from the
// running config.
Expand Down
Loading