Skip to content

Commit

Permalink
Simulate Packet API
Browse files Browse the repository at this point in the history
Summary: Exposing simulator generic packet processing via API.

Reviewed By: frankfeir, tagrawal03

Differential Revision: D68521022

fbshipit-source-id: aca506c6b4c4eece63781ad2f86c22f699200f9c
  • Loading branch information
avasylev authored and facebook-github-bot committed Jan 29, 2025
1 parent da2d0f4 commit 8fa534e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
17 changes: 17 additions & 0 deletions katran/lib/KatranLb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,23 @@ std::unordered_map<uint32_t, std::string> KatranLb::getHealthcheckersDst() {
return hcs;
}

std::string KatranLb::simulatePacket(const std::string& inPacket) {
std::string result;
if (!progsLoaded_) {
LOG(ERROR) << "bpf programs are not loaded";
return result;
}
auto inBuf = folly::IOBuf::copyBuffer(inPacket);
auto sim = KatranSimulator(getKatranProgFd());
auto outBuf = sim.runSimulation(std::move(inBuf));
if (!outBuf) {
LOG(ERROR) << "simulator failed to run simulation";
return result;
}
result = outBuf->moveToFbString().toStdString();
return result;
}

const std::string KatranLb::getRealForFlow(const KatranFlow& flow) {
if (!progsLoaded_) {
LOG(ERROR) << "bpf programs are not loaded";
Expand Down
12 changes: 12 additions & 0 deletions katran/lib/KatranLb.h
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,18 @@ class KatranLb {
*/
KatranBpfMapStats getBpfMapStats(const std::string& map);

/*
* @param inPacket binary data of the packet, including Ethernet header and up
* @return binary data of the packet after it was processed by Katran
*
* Processes given packet thru Katran BPF and returns the resulting packet.
* Note that while the packet is not going thru actual networking stack, it
* is processed by the same BPF program and affects it's state (like maps,
* stats, etc).
*/
std::string simulatePacket(const std::string& inPacket);

/**
* @param KatranFlow 5 tuple which describes a flow
* @return string address of the real.
Expand Down
14 changes: 13 additions & 1 deletion katran/lib/KatranSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ KatranSimulator::~KatranSimulator() {}

std::unique_ptr<folly::IOBuf> KatranSimulator::runSimulation(
std::unique_ptr<folly::IOBuf> pckt) {
if (!pckt) {
LOG(ERROR) << "packet is empty";
return nullptr;
}
if (pckt->isChained()) {
LOG(ERROR) << "Chained buffers are not supported";
return nullptr;
}
if (pckt->length() > kMaxXdpPcktSize) {
LOG(ERROR) << "packet is too big";
return nullptr;
}
auto rpckt = folly::IOBuf::create(kMaxXdpPcktSize);
if (!rpckt) {
LOG(ERROR) << "not able to allocate memory for resulting packet";
Expand All @@ -226,7 +238,7 @@ std::unique_ptr<folly::IOBuf> KatranSimulator::runSimulation(
progFd_,
kTestRepeatCount,
pckt->writableData(),
pckt->computeChainDataLength(),
pckt->length(),
rpckt->writableData(),
&output_pckt_size,
&prog_ret_val);
Expand Down
3 changes: 2 additions & 1 deletion katran/lib/KatranSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ class KatranSimulator final {
*/
const std::string getRealForFlow(const KatranFlow& flow);

private:
// runSimulation takes packet (in iobuf represenation) and
// run it through katran bpf program. It returns a modified pckt, if the
// result was XDP_TX or nullptr otherwise.
std::unique_ptr<folly::IOBuf> runSimulation(
std::unique_ptr<folly::IOBuf> pckt);

private:
int progFd_;
};
} // namespace katran

0 comments on commit 8fa534e

Please sign in to comment.