Skip to content

Commit f8c3a7c

Browse files
committed
Fix RoundRobin crash with zero output gate
If RoundRobin module has no output gate, it crashes due to division by zero. In this case, the "right" behavior should be just dropping packets. Also, this commit replaces the modulo(%) operation with a simple if branch. It saves about 22 cycles per batch or packet, depending on the operation mode of RoundRobin.
1 parent 0716603 commit f8c3a7c

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

core/modules/round_robin.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,24 @@ CommandResponse RoundRobin::CommandSetGates(
101101
void RoundRobin::ProcessBatch(bess::PacketBatch *batch) {
102102
gate_idx_t out_gates[bess::PacketBatch::kMaxBurst];
103103

104+
if (ngates_ <= 0) {
105+
bess::Packet::Free(batch);
106+
return;
107+
}
108+
104109
if (per_packet_) {
105110
for (int i = 0; i < batch->cnt(); i++) {
106111
out_gates[i] = gates_[current_gate_];
107-
current_gate_ = (current_gate_ + 1) % ngates_;
112+
if (++current_gate_ >= ngates_) {
113+
current_gate_ = 0;
114+
}
108115
}
109116
RunSplit(out_gates, batch);
110117
} else {
111118
gate_idx_t gate = gates_[current_gate_];
112-
current_gate_ = (current_gate_ + 1) % ngates_;
119+
if (++current_gate_ >= ngates_) {
120+
current_gate_ = 0;
121+
}
113122
RunChooseModule(gate, batch);
114123
}
115124
}

0 commit comments

Comments
 (0)