Skip to content

Commit

Permalink
Remove hardcoded instances of mesh interface
Browse files Browse the repository at this point in the history
Summary:
Over time, many instances of the literal `"mesh0"` made their way into `fbmeshd`. Although this is the default mesh interface name and is generally unlikely to be changed by a user, it is bad practice to have it be so tightly ingrained in the code.

Abstract it out to the pre-existing `--mesh_ifname` / `FLAGS_mesh_ifname` flag.

Reviewed By: jcard0na

Differential Revision: D16494133

fbshipit-source-id: 5c85d60f4707d2190f282f2eebb24367d68fc635
  • Loading branch information
ilyacodes authored and facebook-github-bot committed Jul 25, 2019
1 parent b53847f commit 9a60ba2
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 70 deletions.
22 changes: 12 additions & 10 deletions openr/fbmeshd/802.11s/Nl80211Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ extern "C" {
using namespace openr::fbmeshd;

DEFINE_string(mesh_id, "mesh-soma", "Mesh ID");
DEFINE_string(mesh_ifname, "mesh0", "Mesh interface name");
DEFINE_string(mesh_channel_type, "40", "Mesh channel type, in string format");
DEFINE_int32(mesh_frequency, 5805, "Mesh control frequency");
DEFINE_int32(mesh_center_freq1, 5795, "Mesh center frequency 1");
Expand Down Expand Up @@ -158,8 +157,12 @@ const auto mpath_policy_{[]() {
} // namespace

Nl80211Handler::Nl80211Handler(
fbzmq::ZmqEventLoop& zmqLoop, bool userspace_mesh_peering)
: zmqLoop_{zmqLoop}, userspace_mesh_peering_{userspace_mesh_peering} {
fbzmq::ZmqEventLoop& zmqLoop,
const std::string& interfaceName,
bool userspace_mesh_peering)
: interfaceName_{interfaceName},
zmqLoop_{zmqLoop},
userspace_mesh_peering_{userspace_mesh_peering} {
VLOG(8) << folly::sformat("Nl80211Handler::{}()", __func__);

// We expect Nl80211Handler to be treated as a singleton, and there should not
Expand Down Expand Up @@ -191,8 +194,7 @@ Nl80211Handler::printConfiguration() {
VLOG(8) << folly::sformat("Nl80211Handler::{}()", __func__);

VLOG(1)
<< "mesh id: " << FLAGS_mesh_id
<< ", interface name: " << FLAGS_mesh_ifname
<< "mesh id: " << FLAGS_mesh_id << ", interface name: " << interfaceName_
<< ", channel type: " << FLAGS_mesh_channel_type
<< ", control frequency: " << FLAGS_mesh_frequency
<< ", center freq1: " << FLAGS_mesh_center_freq1
Expand Down Expand Up @@ -225,13 +227,13 @@ Nl80211Handler::validateConfiguration() {
throw std::invalid_argument("Mesh ID is too long");
}

if (FLAGS_mesh_ifname.length() > IFNAMSIZ) {
if (interfaceName_.length() > IFNAMSIZ) {
LOG(ERROR) << folly::sformat(
"Interface '{}' for mesh '{}' is too long - interface is length {} but "
"maximum (excluding null-term) is {}",
FLAGS_mesh_ifname,
interfaceName_,
FLAGS_mesh_id,
FLAGS_mesh_ifname.length(),
interfaceName_.length(),
IFNAMSIZ);
throw std::invalid_argument("Interface name is too long");
}
Expand Down Expand Up @@ -1220,7 +1222,7 @@ Nl80211Handler::applyConfiguration() {
}

netIntf->second.meshId = FLAGS_mesh_id;
netIntf->second.maybeIfName = FLAGS_mesh_ifname;
netIntf->second.maybeIfName = interfaceName_;

netIntf->second.frequency = FLAGS_mesh_frequency;
netIntf->second.centerFreq1 = FLAGS_mesh_center_freq1;
Expand Down Expand Up @@ -1282,7 +1284,7 @@ Nl80211Handler::applyConfiguration() {
"Error creating interface {}", netIntf->second.phyIndex())
<< " error: " << this << e.what();
}
VLOG(8) << "Mapped ifName " << FLAGS_mesh_ifname << " to "
VLOG(8) << "Mapped ifName " << interfaceName_ << " to "
<< netIntf->second.phyName;

break;
Expand Down
10 changes: 7 additions & 3 deletions openr/fbmeshd/802.11s/Nl80211Handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ class Nl80211Handler final : public Nl80211HandlerInterface {
Nl80211Handler& operator=(Nl80211Handler&&) = delete;

public:
Nl80211Handler(fbzmq::ZmqEventLoop& zmqLoop, bool userspace_mesh_peering);
Nl80211Handler(
fbzmq::ZmqEventLoop& zmqLoop,
const std::string& interface,
bool userspace_mesh_peering);

~Nl80211Handler();

Expand Down Expand Up @@ -152,8 +155,8 @@ class Nl80211Handler final : public Nl80211HandlerInterface {

private:
// Configuration methods
static void printConfiguration();
static void validateConfiguration();
void printConfiguration();
void validateConfiguration();

// Initialization/cleanup methods
void initNlSockets();
Expand Down Expand Up @@ -206,6 +209,7 @@ class Nl80211Handler final : public Nl80211HandlerInterface {
static Nl80211Handler* globalNlHandler;

private:
const std::string& interfaceName_;
GenericNetlinkSocket nlEventSocket_;
std::map<uint32_t, NetInterface> netInterfaces_;
std::map<std::string, uint32_t> multicastGroups_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ getTaygaIPV6FromMacAddress(folly::MacAddress macAddress) {
}

folly::IPAddressV6
getMesh0IPV6FromMacAddress(folly::MacAddress macAddress) {
getMeshIPV6FromMacAddress(folly::MacAddress macAddress) {
return getIPV6FromMacAddress("\xfc\x00\x00\x00\x00\x00\x00\x00", macAddress);
}

} // namespace

Gateway11sRootRouteProgrammer::Gateway11sRootRouteProgrammer(
openr::fbmeshd::Nl80211Handler& nlHandler,
const std::string& interface,
std::chrono::seconds const interval,
double const gatewayChangeThresholdFactor)
: nlHandler_{nlHandler},
Expand All @@ -59,7 +60,7 @@ Gateway11sRootRouteProgrammer::Gateway11sRootRouteProgrammer(
"proxy",
getTaygaIPV6FromMacAddress(*netif.maybeMacAddress).str(),
"dev",
"mesh0"}}
interface}}
.wait();

timer_ = fbzmq::ZmqTimeout::make(
Expand Down Expand Up @@ -132,7 +133,7 @@ Gateway11sRootRouteProgrammer::determineBestRoot() {
}

openr::fbnl::NlUnicastRoutes routeDb;
std::vector<fbnl::IfAddress> mesh0Addrs;
std::vector<fbnl::IfAddress> meshAddrs;
ifIndex = netlinkSocket_.getIfIndex("tayga").get();
auto destination = std::make_pair<folly::IPAddress, uint8_t>(
folly::IPAddressV6{"fd00:ffff::"}, 96);
Expand Down Expand Up @@ -169,7 +170,7 @@ Gateway11sRootRouteProgrammer::determineBestRoot() {
.setProtocolId(98)
.addNextHop(
fbnl::NextHopBuilder{}
.setGateway(getMesh0IPV6FromMacAddress(currentRoot_->first))
.setGateway(getMeshIPV6FromMacAddress(currentRoot_->first))
.build())
.build());
}
Expand Down Expand Up @@ -205,15 +206,15 @@ Gateway11sRootRouteProgrammer::determineBestRoot() {
.addNextHop(fbnl::NextHopBuilder{}.setIfIndex(ifIndex).build())
.build());

mesh0Addrs.push_back(
meshAddrs.push_back(
fbnl::IfAddressBuilder{}
.setPrefix(folly::CIDRNetwork{
getMesh0IPV6FromMacAddress(*netif.maybeMacAddress), 64})
getMeshIPV6FromMacAddress(*netif.maybeMacAddress), 64})
.setIfIndex(netif.maybeIfIndex.value())
.build());

netlinkSocket_.syncIfAddress(
netif.maybeIfIndex.value(), mesh0Addrs, AF_INET6, RT_SCOPE_UNIVERSE);
netif.maybeIfIndex.value(), meshAddrs, AF_INET6, RT_SCOPE_UNIVERSE);
netlinkSocket_.syncUnicastRoutes(98, std::move(routeDb)).get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Gateway11sRootRouteProgrammer : public fbzmq::ZmqEventLoop {
public:
explicit Gateway11sRootRouteProgrammer(
openr::fbmeshd::Nl80211Handler& nlHandler,
const std::string& interface,
std::chrono::seconds const interval,
double const gatewayChangeThresholdFactor);

Expand Down
16 changes: 11 additions & 5 deletions openr/fbmeshd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ DEFINE_int32(fbmeshd_service_port, 30303, "fbmeshd thrift service port");

DEFINE_string(node_name, "node1", "The name of current node");

DEFINE_string(mesh_ifname, "mesh0", "Mesh interface name");

DEFINE_bool(
enable_userspace_mesh_peering,
true,
Expand Down Expand Up @@ -275,7 +277,8 @@ main(int argc, char* argv[]) {
&evl, "fbmeshd_shared_event_loop", watchdog.get());
AuthsaeCallbackHelpers::init(evl);

Nl80211Handler nlHandler{evl, FLAGS_enable_userspace_mesh_peering};
Nl80211Handler nlHandler{
evl, FLAGS_mesh_ifname, FLAGS_enable_userspace_mesh_peering};
auto returnValue = nlHandler.joinMeshes();
if (returnValue != R_SUCCESS) {
return returnValue;
Expand All @@ -295,6 +298,7 @@ main(int argc, char* argv[]) {
gateway11sRootRouteProgrammer = std::make_unique<
Gateway11sRootRouteProgrammer>(
nlHandler,
FLAGS_mesh_ifname,
std::chrono::seconds{
FLAGS_gateway_11s_root_route_programmer_interval_s},
FLAGS_gateway_11s_root_route_programmer_gateway_change_threshold_factor);
Expand Down Expand Up @@ -327,21 +331,23 @@ main(int argc, char* argv[]) {
std::chrono::milliseconds{FLAGS_routing_root_pann_interval_ms});
std::unique_ptr<UDPRoutingPacketTransport> routingPacketTransport =
std::make_unique<UDPRoutingPacketTransport>(
routingEventLoop.get(), 6668, FLAGS_routing_tos);
routingEventLoop.get(), FLAGS_mesh_ifname, 6668, FLAGS_routing_tos);
std::unique_ptr<PeriodicPinger> periodicPinger =
std::make_unique<PeriodicPinger>(
routingEventLoop.get(),
folly::IPAddressV6{"ff02::1%mesh0"},
folly::IPAddressV6{folly::sformat("ff02::1%{}", FLAGS_mesh_ifname)},
folly::IPAddressV6{
folly::IPAddressV6::LinkLocalTag::LINK_LOCAL,
nlHandler.lookupMeshNetif().maybeMacAddress.value()},
kPeriodicPingerInterval,
"mesh0");
FLAGS_mesh_ifname);
periodicPinger->scheduleTimeout(1s);

std::unique_ptr<SyncRoutes80211s> syncRoutes80211s =
std::make_unique<SyncRoutes80211s>(
routing.get(), nlHandler.lookupMeshNetif().maybeMacAddress.value());
routing.get(),
nlHandler.lookupMeshNetif().maybeMacAddress.value(),
FLAGS_mesh_ifname);

static constexpr auto syncRoutes80211sId{"SyncRoutes80211s"};
monitorEventLoopWithWatchdog(
Expand Down
2 changes: 1 addition & 1 deletion openr/fbmeshd/routing/PeriodicPinger.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PeriodicPinger : public folly::AsyncTimeout {
folly::IPAddressV6 dst,
folly::IPAddressV6 src,
std::chrono::milliseconds interval,
const std::string& inteface);
const std::string& interface);

PeriodicPinger() = delete;
~PeriodicPinger() override = default;
Expand Down
28 changes: 16 additions & 12 deletions openr/fbmeshd/routing/SyncRoutes80211s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ getTaygaIPV6FromMacAddress(folly::MacAddress macAddress) {
}

folly::IPAddressV6
getMesh0IPV6FromMacAddress(folly::MacAddress macAddress) {
getMeshIPV6FromMacAddress(folly::MacAddress macAddress) {
return getIPV6FromMacAddress("\xfc\x00\x00\x00\x00\x00\x00\x00", macAddress);
}

Expand All @@ -71,8 +71,12 @@ isInterfaceUp(std::string interface) {

} // namespace

SyncRoutes80211s::SyncRoutes80211s(Routing* routing, folly::MacAddress nodeAddr)
: routing_{routing}, nodeAddr_{nodeAddr}, netlinkSocket_{this} {
SyncRoutes80211s::SyncRoutes80211s(
Routing* routing, folly::MacAddress nodeAddr, const std::string& interface)
: routing_{routing},
nodeAddr_{nodeAddr},
interface_{interface},
netlinkSocket_{this} {
// Set timer to sync routes
syncRoutesTimer_ =
fbzmq::ZmqTimeout::make(this, [this]() noexcept { doSyncRoutes(); });
Expand All @@ -83,13 +87,13 @@ void
SyncRoutes80211s::doSyncRoutes() {
VLOG(8) << folly::sformat("SyncRoutes80211s::{}()", __func__);

auto meshIfIndex = netlinkSocket_.getIfIndex("mesh0").get();
auto meshIfIndex = netlinkSocket_.getIfIndex(interface_).get();
auto isGate = routing_->getGatewayStatus();
auto meshPaths = routing_->getMeshPaths();

openr::fbnl::NlUnicastRoutes unicastRouteDb;
openr::fbnl::NlLinkRoutes linkRouteDb;
std::vector<fbnl::IfAddress> mesh0Addrs;
std::vector<fbnl::IfAddress> meshAddrs;

const auto kTaygaIfName{"tayga"};
auto taygaIfIndex = netlinkSocket_.getIfIndex(kTaygaIfName).get();
Expand Down Expand Up @@ -122,7 +126,7 @@ SyncRoutes80211s::doSyncRoutes() {
.build());
}
destination = std::make_pair<folly::IPAddress, uint8_t>(
getMesh0IPV6FromMacAddress(mpath.dst), 128);
getMeshIPV6FromMacAddress(mpath.dst), 128);
unicastRouteDb.emplace(
destination,
fbnl::RouteBuilder{}
Expand Down Expand Up @@ -190,14 +194,14 @@ SyncRoutes80211s::doSyncRoutes() {
.buildLinkRoute());
}

mesh0Addrs.push_back(fbnl::IfAddressBuilder{}
.setPrefix(folly::CIDRNetwork{
getMesh0IPV6FromMacAddress(nodeAddr_), 64})
.setIfIndex(meshIfIndex)
.build());
meshAddrs.push_back(fbnl::IfAddressBuilder{}
.setPrefix(folly::CIDRNetwork{
getMeshIPV6FromMacAddress(nodeAddr_), 64})
.setIfIndex(meshIfIndex)
.build());

netlinkSocket_.syncIfAddress(
meshIfIndex, mesh0Addrs, AF_INET6, RT_SCOPE_UNIVERSE);
meshIfIndex, meshAddrs, AF_INET6, RT_SCOPE_UNIVERSE);

if (isGateBeforeRouteSync_ != isGate) {
netlinkSocket_.syncUnicastRoutes(98, std::move(unicastRouteDb)).get();
Expand Down
6 changes: 5 additions & 1 deletion openr/fbmeshd/routing/SyncRoutes80211s.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ namespace fbmeshd {

class SyncRoutes80211s : public fbzmq::ZmqEventLoop {
public:
SyncRoutes80211s(Routing* routing, folly::MacAddress nodeAddr);
SyncRoutes80211s(
Routing* routing,
folly::MacAddress nodeAddr,
const std::string& interface);

// This class should never be copied; remove default copy/move
SyncRoutes80211s() = delete;
Expand All @@ -34,6 +37,7 @@ class SyncRoutes80211s : public fbzmq::ZmqEventLoop {

Routing* routing_;
folly::MacAddress nodeAddr_;
const std::string& interface_;

std::unique_ptr<fbzmq::ZmqTimeout> syncRoutesTimer_;
openr::fbnl::NetlinkSocket netlinkSocket_;
Expand Down
22 changes: 15 additions & 7 deletions openr/fbmeshd/routing/UDPRoutingPacketTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@

#include "UDPRoutingPacketTransport.h"

#include <folly/Format.h>

using namespace openr::fbmeshd;

UDPRoutingPacketTransport::UDPRoutingPacketTransport(
folly::EventBase* evb, uint16_t port, int32_t tos)
: evb_{evb}, serverSocket_{evb_}, clientSocket_{evb_} {
folly::EventBase* evb,
const std::string& interface,
uint16_t port,
int32_t tos)
: evb_{evb},
interface_{interface},
serverSocket_{evb_},
clientSocket_{evb_} {
evb_->runInEventBaseThread([this, port, tos]() {
serverSocket_.bind(folly::SocketAddress{"::", port});
serverSocket_.addListener(evb_, this);
Expand Down Expand Up @@ -41,11 +49,11 @@ UDPRoutingPacketTransport::sendPacket(
evb_->runInEventBaseThread([this, da, buf = std::move(buf)]() {
const auto destSockAddr = folly::SocketAddress{
da.isBroadcast()
? folly::IPAddressV6{"ff02::1%mesh0"}
: folly::IPAddressV6{folly::IPAddressV6{
folly::IPAddressV6::LINK_LOCAL, da}
.str() +
"%mesh0"},
? folly::IPAddressV6{folly::sformat("ff02::1%{}", interface_)}
: folly::IPAddressV6{folly::sformat(
"{}%{}",
folly::IPAddressV6{folly::IPAddressV6::LINK_LOCAL, da}.str(),
interface_)},
6668};
clientSocket_.write(destSockAddr, std::move(buf));
});
Expand Down
8 changes: 7 additions & 1 deletion openr/fbmeshd/routing/UDPRoutingPacketTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ namespace fbmeshd {

class UDPRoutingPacketTransport : public folly::AsyncUDPServerSocket::Callback {
public:
UDPRoutingPacketTransport(folly::EventBase* evb, uint16_t port, int32_t tos);
UDPRoutingPacketTransport(
folly::EventBase* evb,
const std::string& interface,
uint16_t port,
int32_t tos);

void sendPacket(folly::MacAddress da, std::unique_ptr<folly::IOBuf> buf);

Expand All @@ -41,6 +45,8 @@ class UDPRoutingPacketTransport : public folly::AsyncUDPServerSocket::Callback {

folly::EventBase* evb_;

const std::string& interface_;

folly::AsyncUDPServerSocket serverSocket_;

folly::AsyncUDPSocket clientSocket_;
Expand Down
Loading

0 comments on commit 9a60ba2

Please sign in to comment.