Skip to content
Open
Show file tree
Hide file tree
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
42 changes: 38 additions & 4 deletions src/include/pmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ether.h"
#include "packet.h"
#include "packet_pool.h"
#include "utils.h"

namespace juggler {
namespace dpdk {
Expand Down Expand Up @@ -58,14 +59,20 @@ class PmdRing {
port_id_(port_id),
ring_id_(ring_id),
ndesc_(ndesc),
ppool_(nullptr) {}
ppool_(nullptr),
machnet_testing_(std::getenv("MACHNET_TESTING_ENABLED") != nullptr &&
std::string(std::getenv("MACHNET_TESTING_ENABLED")) ==
"1") {}
PmdRing(const PmdPort *port, uint8_t port_id, uint16_t ring_id,
uint16_t ndesc, uint32_t nmbufs, uint32_t mbuf_sz)
: pmd_port_(port),
port_id_(port_id),
ring_id_(ring_id),
ndesc_(ndesc),
ppool_(std::unique_ptr<PacketPool>(new PacketPool(nmbufs, mbuf_sz))) {}
ppool_(std::unique_ptr<PacketPool>(new PacketPool(nmbufs, mbuf_sz))),
machnet_testing_(std::getenv("MACHNET_TESTING_ENABLED") != nullptr &&
std::string(std::getenv("MACHNET_TESTING_ENABLED")) ==
"1") {}

rte_mempool *GetPacketMemPool() const { return ppool_.get()->GetMemPool(); }

Expand All @@ -75,6 +82,10 @@ class PmdRing {
const uint16_t ring_id_;
const uint16_t ndesc_;
const std::unique_ptr<PacketPool> ppool_;

public:
const bool machnet_testing_;
juggler::utils::FastRand fast_rand_;
};

/*
Expand Down Expand Up @@ -103,6 +114,25 @@ class TxRing : public PmdRing {

void Init();

/// @brief Drops a random packet from the given array of packets.
/// @param pkts Array of packet pointers.
/// @param nb_pkts Number of packets in the array.
inline void pkt_idx_to_drop(Packet **pkts, uint16_t *nb_pkts) {
static constexpr uint32_t kBillion = 1000000000;
// the value below is picked randomly as probability.
static constexpr float kDropProb = 0.000001;
if (fast_rand_.next_u32() % kBillion < kDropProb * kBillion) {
int random_packet = fast_rand_.next_u32() % *nb_pkts;
LOG(INFO) << "Dropping random packet: " << random_packet << " from "
<< *nb_pkts << " packets.";
Packet::Free(pkts[random_packet]);
for (int i = random_packet; i < *nb_pkts - 1; i++) {
pkts[i] = pkts[i + 1];
}
*nb_pkts = *nb_pkts - 1;
}
}

/**
* @brief Tries to send a burst of packets through this TX ring.
*
Expand Down Expand Up @@ -138,7 +168,11 @@ class TxRing : public PmdRing {
* @param pkts Array of packet pointers to send.
* @param nb_pkts Number of packets to send.
*/
void SendPackets(Packet **pkts, uint16_t nb_pkts) const {
void SendPackets(Packet **pkts, uint16_t nb_pkts) {
if (machnet_testing_) {
pkt_idx_to_drop(pkts, &nb_pkts);
}

uint16_t nb_remaining = nb_pkts;

do {
Expand All @@ -156,7 +190,7 @@ class TxRing : public PmdRing {
*
* @param batch Pointer to the PacketBatch.
*/
void SendPackets(PacketBatch *batch) const {
void SendPackets(PacketBatch *batch) {
SendPackets(batch->pkts(), batch->GetSize());
batch->Clear();
}
Expand Down
29 changes: 29 additions & 0 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <functional>
#include <iomanip>
#include <locale>
#include <random>
#include <sstream>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -466,6 +467,34 @@ class ChronoTimer {
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_;
};

// Derived from eRPC
class SlowRand {
std::random_device rand_dev_; // Non-pseudorandom seed for twister
std::mt19937_64 mt_;
std::uniform_int_distribution<uint64_t> dist_;

public:
SlowRand() : mt_(rand_dev_()), dist_(0, UINT64_MAX) {}

inline uint64_t next_u64() { return dist_(mt_); }
};

class FastRand {
public:
uint64_t seed_;

/// Create a FastRand using a seed from SlowRand
FastRand() {
SlowRand slow_rand;
seed_ = slow_rand.next_u64();
}

inline uint32_t next_u32() {
seed_ = seed_ * 1103515245 + 12345;
return static_cast<uint32_t>(seed_ >> 32);
}
};

} // namespace utils
} // namespace juggler

Expand Down