Skip to content

Commit ca3bfa9

Browse files
committed
Add ARP Responder module
Address code review comments
1 parent 374d956 commit ca3bfa9

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

core/modules/arp_responder.cc

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ CommandResponse ArpResponder::CommandAdd(const bess::pb::ArpResponderArg &arg) {
1919
return CommandFailure(EINVAL, "Invalid IP Address: %s", arg.ip().c_str());
2020
}
2121

22-
entry.mac_addr.FromString(arg.mac_addr());
22+
if (!entry.mac_addr.FromString(arg.mac_addr())) {
23+
return CommandFailure(EINVAL, "Invalid MAC Address: %s", arg.mac_addr().c_str());
24+
}
25+
2326
entry.ip_addr = ip_addr;
24-
entries[ip_addr] = entry;
27+
entries_[ip_addr] = entry;
2528
return CommandSuccess();
2629
}
2730

@@ -44,27 +47,27 @@ void ArpResponder::ProcessBatch(bess::PacketBatch *batch) {
4447

4548
Arp *arp = reinterpret_cast<Arp *>(eth + 1);
4649
if (arp->opcode == be16_t(Arp::Opcode::kRequest)) {
47-
// TODO When learn is added, learn SRC MAC here
50+
// TODO(galsagie) When learn is added, learn SRC MAC here
4851

4952
// Try to find target IP in cache, if exists convert request to reply
50-
auto it = entries.find(arp->target_ip_addr);
51-
if (it != entries.end()) {
52-
struct arp_entry *entry = &it->second;
53+
auto it = entries_.find(arp->target_ip_addr);
54+
if (it != entries_.end()) {
55+
const struct arp_entry &entry = it->second;
5356
arp->opcode = be16_t(Arp::Opcode::kReply);
5457

5558
eth->dst_addr = eth->src_addr;
56-
eth->src_addr = entry->mac_addr;
59+
eth->src_addr = entry.mac_addr;
5760

5861
arp->target_hw_addr = arp->sender_hw_addr;
59-
arp->sender_hw_addr = entry->mac_addr;
62+
arp->sender_hw_addr = entry.mac_addr;
6063

6164
arp->target_ip_addr = arp->sender_ip_addr;
62-
arp->sender_ip_addr = entry->ip_addr;
65+
arp->sender_ip_addr = entry.ip_addr;
6366
}
6467
} else if (arp->opcode == be16_t(Arp::Opcode::kReply)) {
65-
// TODO When learn is added, learn SRC MAC here
68+
// TODO(galsagie) When learn is added, learn SRC MAC here
6669
} else {
67-
// TODO Other opcodes are not handled yet.
70+
// TODO(galsagie) Other opcodes are not handled yet.
6871
}
6972
}
7073

core/modules/arp_responder.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
using bess::utils::Ethernet;
1313
using bess::utils::be32_t;
1414

15+
// ARP cache entry struct which keeps mapping between IP and MAC
1516
struct arp_entry {
1617
Ethernet::Address mac_addr;
1718
be32_t ip_addr;
19+
// timestamp used to expire cache entries
1820
uint64_t time;
1921
};
2022

23+
// ARP Responder module
24+
// Answer ARP requests from an internal configurable cache
25+
// Currently drops non ARP packets
2126
class ArpResponder final : public Module {
2227
public:
2328
static const gate_idx_t kNumIGates = 1;
@@ -30,7 +35,8 @@ class ArpResponder final : public Module {
3035
CommandResponse CommandAdd(const bess::pb::ArpResponderArg &arg);
3136

3237
private:
33-
std::map<be32_t, arp_entry> entries;
38+
// Mapping between IP (key) and its ARP entry (MAC Address)
39+
std::map<be32_t, struct arp_entry> entries_;
3440
};
3541

3642
#endif // BESS_MODULES_ARP_RESPONDER_H_

0 commit comments

Comments
 (0)