Skip to content

Commit

Permalink
Add support for color-aware meters (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoninbas authored Feb 13, 2017
1 parent 8a15692 commit dd0fa9d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 4 additions & 2 deletions include/bm/bm_sim/meters.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ class Meter {
//! - `0 <-> GREEN`
//! - `1 <-> YELLOW`
//! - `2 <-> RED`
color_t execute(const Packet &pkt);
//! You can optionally provide a pre-color for the meter. If the computed
//! color is less than the pre-color, the pre-color will be returned instead.
color_t execute(const Packet &pkt, color_t pre_color = 0);

void serialize(std::ostream *out) const;
void deserialize(std::istream *in);
Expand Down Expand Up @@ -220,7 +222,7 @@ class MeterArray : public NamedP4Object {
//! Executes the meter at index \p idx on the given packet and returns the
//! correct integral color value.
//! See Meter::execute() for more information.
color_t execute_meter(const Packet &pkt, size_t idx);
color_t execute_meter(const Packet &pkt, size_t idx, color_t pre_color = 0);

template<class RAIt>
MeterErrorCode set_rates(const RAIt first, const RAIt last) {
Expand Down
8 changes: 4 additions & 4 deletions src/bm_sim/meters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Meter::reset_rates() {
}

Meter::color_t
Meter::execute(const Packet &pkt) {
Meter::execute(const Packet &pkt, color_t pre_color) {
color_t packet_color = 0;

if (!configured) return packet_color;
Expand Down Expand Up @@ -101,7 +101,7 @@ Meter::execute(const Packet &pkt) {
}
}

return packet_color;
return std::max(pre_color, packet_color);
}

void
Expand Down Expand Up @@ -155,9 +155,9 @@ MeterArray::MeterArray(const std::string &name, p4object_id_t id,
}

MeterArray::color_t
MeterArray::execute_meter(const Packet &pkt, size_t idx) {
MeterArray::execute_meter(const Packet &pkt, size_t idx, color_t pre_color) {
BMLOG_DEBUG_PKT(pkt, "Executing meter {}[{}]", get_name(), idx);
return meters[idx].execute(pkt);
return meters[idx].execute(pkt, pre_color);
}

MeterArray::MeterErrorCode
Expand Down
16 changes: 16 additions & 0 deletions tests/test_meters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,19 @@ TEST_F(MetersTest, GetRates) {
ASSERT_EQ(rates[i].burst_size, retrieved_rates[i].burst_size);
}
}

TEST_F(MetersTest, PreColor) {
const color_t GREEN = 0;
const color_t RED = 1;

Meter meter(MeterType::PACKETS, 1);
// 2 packets per second, burst size of 3
Meter::rate_config_t rate = {0.000002, 3};
meter.set_rates({rate});

Packet pkt = get_pkt(128);

ASSERT_EQ(GREEN, meter.execute(pkt));
ASSERT_EQ(RED, meter.execute(pkt, RED));
ASSERT_EQ(GREEN, meter.execute(pkt));
}

0 comments on commit dd0fa9d

Please sign in to comment.