Skip to content

Commit cad34be

Browse files
committed
pcn-iptables: fix horus module
Signed-off-by: Matteo Bertrone <m.bertrone@gmail.com>
1 parent b64e0fa commit cad34be

5 files changed

Lines changed: 27 additions & 22 deletions

File tree

src/services/pcn-iptables/src/Chain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class Chain : public ChainInterface {
127127
std::map<struct HorusRule, struct HorusValue> &horus,
128128
const std::vector<std::shared_ptr<ChainRule>> &rules);
129129

130-
static void fromRuleToHorusKeyValue(std::shared_ptr<ChainRule> rule,
130+
static bool fromRuleToHorusKeyValue(std::shared_ptr<ChainRule> rule,
131131
struct HorusRule &key,
132132
struct HorusValue &value);
133133
};

src/services/pcn-iptables/src/Utils.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,16 @@ bool Chain::interfaceFromRulesToMap(
534534
return brk;
535535
}
536536

537-
void Chain::fromRuleToHorusKeyValue(std::shared_ptr<ChainRule> rule,
537+
bool Chain::fromRuleToHorusKeyValue(std::shared_ptr<ChainRule> rule,
538538
struct HorusRule &key,
539539
struct HorusValue &value) {
540540
key.setFields = 0;
541541

542+
try {
543+
rule->getConntrack();
544+
return false;
545+
} catch (std::runtime_error) {}
546+
542547
try {
543548
IpAddr ips;
544549
ips.fromString(rule->getSrc());
@@ -590,7 +595,7 @@ void Chain::fromRuleToHorusKeyValue(std::shared_ptr<ChainRule> rule,
590595
value.action = 1;
591596

592597
value.ruleID = rule->getId();
593-
return;
598+
return true;
594599
}
595600

596601
void Chain::horusFromRulesToMap(
@@ -599,30 +604,28 @@ void Chain::horusFromRulesToMap(
599604
struct HorusRule key;
600605
struct HorusValue value;
601606

602-
bool first_rule = true;
603607
uint64_t set_fields = 0;
604608

605609
// find match pattern for first rule (e.g. 01101 means ips proto ports set)
606610

607611
int i = 0;
608612

609613
for (auto const &rule : rules) {
610-
i++;
611-
fromRuleToHorusKeyValue(rule, key, value);
612-
613-
if (i == 1) {
614+
if (i >= HorusConst::MAX_RULE_SIZE_FOR_HORUS)
615+
break;
616+
if(!fromRuleToHorusKeyValue(rule, key, value))
617+
return;
618+
if (i == 0) {
614619
if (key.setFields == 0) {
615620
break;
616621
}
617-
618622
set_fields = key.setFields;
619623
}
620-
621624
if (key.setFields != set_fields) {
622625
break;
623626
}
624-
625627
horus.insert(std::pair<struct HorusRule, struct HorusValue>(key, value));
628+
i++;
626629
}
627630
}
628631

src/services/pcn-iptables/src/datapaths/Iptables_Horus_dp.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct packetHeaders {
3030
uint32_t seqN;
3131
uint32_t ackN;
3232
uint8_t connStatus;
33-
};
33+
} __attribute__((packed));
3434

3535
struct horusKey {
3636
#if _SRCIP
@@ -69,14 +69,13 @@ enum {
6969

7070
BPF_TABLE("extern", int, struct packetHeaders, packet, 1);
7171

72-
// TODO 1024 -> const
73-
BPF_TABLE("hash", struct horusKey, struct horusValue, horusTable, 1024);
72+
BPF_TABLE("hash", struct horusKey, struct horusValue, horusTable, _MAX_RULE_SIZE_FOR_HORUS);
7473

7574
BPF_TABLE("extern", int, int, forwardingDecision, 1);
7675

7776
// Per-CPU maps used to keep counter of HORUS rules
78-
BPF_TABLE("percpu_array", int, u64, pkts_horus, 1024);
79-
BPF_TABLE("percpu_array", int, u64, bytes_horus, 1024);
77+
BPF_TABLE("percpu_array", int, u64, pkts_horus, _MAX_RULE_SIZE_FOR_HORUS);
78+
BPF_TABLE("percpu_array", int, u64, bytes_horus, _MAX_RULE_SIZE_FOR_HORUS);
8079

8180
static __always_inline void incrementHorusCounters(u32 ruleID, u32 bytes) {
8281
u64 *value;
@@ -133,18 +132,18 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
133132
struct horusValue *value;
134133
value = horusTable.lookup(&key);
135134

136-
pcn_log(ctx, LOG_DEBUG, "HORUS key: %x. pkt: %x", key.srcIp, pkt->srcIp);
135+
pcn_log(ctx, LOG_DEBUG, "Horus srcIp: %I dstIp: %I", pkt->srcIp, pkt->dstIp);
137136
if (value == NULL) {
138137
// Miss, goto pipleline
139138
goto PIPELINE;
140139
} else {
141140
// Independently from the final action (ACCEPT or DROP)
142141
// I have to update the counters
143-
if (value->ruleID <= 1024) {
142+
if (value->ruleID < _MAX_RULE_SIZE_FOR_HORUS) {
144143
pcn_log(ctx, LOG_DEBUG, "HORUS RuleID: %d", value->ruleID);
145144
incrementHorusCounters(value->ruleID, md->packet_len);
146145
} else {
147-
pcn_log(ctx, LOG_DEBUG, "HORUS RuleID is greater than 1024");
146+
pcn_log(ctx, LOG_DEBUG, "HORUS RuleID is greater than _MAX_RULE_SIZE_FOR_HORUS");
148147
goto PIPELINE;
149148
}
150149

@@ -165,4 +164,4 @@ PIPELINE:;
165164
pcn_log(ctx, LOG_DEBUG, "HORUS Lookup MISS. Goto PIPELINE. ");
166165
call_bpf_program(ctx, _CHAINSELECTOR);
167166
return RX_DROP;
168-
}
167+
}

src/services/pcn-iptables/src/defines.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const uint8_t DSTPORT = 4;
124124
// apply Horus mitigator optimization if there are at least # rules
125125
// matching the pattern, at ruleset begin
126126
const uint8_t MIN_RULE_SIZE_FOR_HORUS = 1;
127-
const uint8_t MAX_RULE_SIZE_FOR_HORUS = -1; // not used
127+
const uint8_t MAX_RULE_SIZE_FOR_HORUS = 2048;
128128

129129
// Enable Horus optimization
130130
// We want to disable Horus by default while we decide a policy to apply it or

src/services/pcn-iptables/src/modules/Horus.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ std::string Iptables::Horus::getCode() {
131131
replaceAll(no_macro_code, "_CONNTRACK_LABEL_INGRESS",
132132
std::to_string(ModulesConstants::CONNTRACKLABEL_INGRESS));
133133

134+
replaceAll(no_macro_code, "_MAX_RULE_SIZE_FOR_HORUS",
135+
std::to_string(HorusConst::MAX_RULE_SIZE_FOR_HORUS));
136+
134137
if (program_type_ == ProgramType::INGRESS) {
135138
replaceAll(no_macro_code, "call_bpf_program", "call_ingress_program");
136139
} else if (program_type_ == ProgramType::EGRESS) {
@@ -224,7 +227,7 @@ void Iptables::Horus::updateTableValue(struct HorusRule horus_key,
224227
}
225228

226229
char buffer[2 * sizeof(uint32_t) +
227-
2 * sizeof(uint16_t) /*+ sizeof(uint8_t)*/] = {0};
230+
2 * sizeof(uint16_t) + sizeof(uint8_t)] = {0};
228231
auto table = iptables_.get_raw_table("horusTable", index_);
229232
table.set(key.toData(buffer), &horus_value);
230233
}

0 commit comments

Comments
 (0)