Skip to content

Commit 1b098e3

Browse files
committed
needs a rebase
1 parent 8bd3fd7 commit 1b098e3

File tree

5 files changed

+25
-22
lines changed

5 files changed

+25
-22
lines changed

src/bfcli/parser.y

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,24 +360,20 @@ rule_option : LOG LOG_HEADERS
360360
_cleanup_free_ char *in = $2;
361361
char *tmp = in;
362362
char *saveptr;
363+
uint32_t limit;
363364

364365
if (tmp[0] == '-')
365366
bf_parse_err("ratelimit should be positive");
366367

367368
errno = 0;
368-
uint32_t limit = strtoul(strtok_r(tmp, "/", &saveptr), NULL, 0);
369+
limit = strtoul(strtok_r(tmp, "/", &saveptr), NULL, 0);
369370
if (errno != 0)
370371
bf_parse_err("ratelimit value is too large");
371372

372373
$$ = (struct bf_rule_options){
373374
.ratelimit = limit,
374375
.flags = BF_RULE_OPTION_RATELIMIT,
375376
};
376-
377-
$$ = (struct bf_rule_options){
378-
.ratelimit = limit,
379-
.flags = BF_RULE_OPTION_RATELIMIT,
380-
};
381377
}
382378
| MARK STRING
383379
{

src/bpfilter/bpf/ratelimit.bpf.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ __u8 bf_ratelimit(void *map, __u64 key, __u64 limit)
2323
return 1;
2424
}
2525

26-
if (current_time != ratelimit->last_time) {
26+
if (current_time != ratelimit->last_time)
2727
ratelimit->current = 0;
28-
}
2928

3029
ratelimit->current++;
3130
ratelimit->last_time = current_time;

src/bpfilter/cgen/elfstub.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,18 @@ enum bf_elfstub_id
135135
*/
136136
BF_ELFSTUB_LOG,
137137

138-
// Return 0 on ACCEPT, 1 on DROP
138+
/**
139+
* Drop packets when `limit` number of packets have already been seen in the last unit of time
140+
*
141+
* `__u8 bf_ratelimit(void *map, __u64 key, __u64 limit)`
142+
*
143+
* **Parameters**
144+
* - `map`: address of the ratelimit map.
145+
* - `key`: key of the map to update.
146+
* - `limit`: number of packets allowed to pass in one unit of time.
147+
*
148+
* **Return** 0 on accept, or 1 on drop.
149+
*/
139150
BF_ELFSTUB_RATELIMIT,
140151

141152
_BF_ELFSTUB_MAX,

src/bpfilter/cgen/prog/map.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,7 @@ static struct bf_btf *_bf_map_make_btf(const struct bf_map *map)
312312
btf__add_field(kbtf, "packets", 1, 0, 0);
313313
btf__add_field(kbtf, "bytes", 1, 64, 0);
314314
break;
315-
case BF_MAP_TYPE_RATELIMIT: // I have no clue what I'm doing here
316-
btf__add_int(kbtf, "u64", 8, 0);
317-
btf->key_type_id = btf__add_int(kbtf, "u32", 4, 0);
318-
btf->value_type_id = btf__add_struct(kbtf, "bf_", 16);
319-
btf__add_field(kbtf, "limit", 1, 0, 0);
320-
break;
315+
case BF_MAP_TYPE_RATELIMIT:
321316
case BF_MAP_TYPE_PRINTER:
322317
case BF_MAP_TYPE_SET:
323318
case BF_MAP_TYPE_LOG:

src/bpfilter/cgen/program.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -643,13 +643,15 @@ static int _bf_program_generate_rule(struct bf_program *program,
643643
EMIT(program, BPF_MOV32_IMM(BPF_REG_3, rule->ratelimit));
644644
EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_RATELIMIT);
645645

646-
_clean_bf_jmpctx_ struct bf_jmpctx ctx =
647-
bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 0));
648-
649-
EMIT(program,
650-
BPF_MOV64_IMM(BPF_REG_0,
651-
program->runtime.ops->get_verdict(BF_VERDICT_DROP)));
652-
EMIT(program, BPF_EXIT_INSN());
646+
{
647+
_clean_bf_jmpctx_ struct bf_jmpctx ctx =
648+
bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 0));
649+
650+
EMIT(program,
651+
BPF_MOV64_IMM(BPF_REG_0,
652+
program->runtime.ops->get_verdict(BF_VERDICT_DROP)));
653+
EMIT(program, BPF_EXIT_INSN());
654+
}
653655
}
654656

655657
switch (rule->verdict) {

0 commit comments

Comments
 (0)