Skip to content

Commit

Permalink
xdp: use BPF_MAP_TYPE_HASH for the filter map (#896)
Browse files Browse the repository at this point in the history
BPF_MAP_TYPE_ARRAY size is limited cause udp4_dp_filter_fd update
fail(-7), eBPF map use locked memory.

Signed-off-by: Frank Du <frank.du@intel.com>
  • Loading branch information
frankdjx committed Jun 7, 2024
1 parent 6385771 commit 002b38e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
14 changes: 7 additions & 7 deletions manager/mtl.xdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
char LICENSE[] SEC("license") = "Dual BSD/GPL";

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 65536);
__type(key, int);
__type(value, int);
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 256); /* max 256 filters */
__type(key, __u16); /* udp port: 16bit */
__type(value, __u8); /* only 1 or 0 */
} udp4_dp_filter SEC(".maps");

struct {
Expand All @@ -25,8 +25,8 @@ struct {
__uint(XDP_DROP, 1);
} XDP_RUN_CONFIG(mtl_dp_filter);

static int __always_inline lookup_udp4_dp(int dp) {
int* value;
static int __always_inline lookup_udp4_dp(__u16 dp) {
__u8* value;

value = bpf_map_lookup_elem(&udp4_dp_filter, &dp);
if (value && *value != 0) return 1;
Expand Down Expand Up @@ -56,7 +56,7 @@ int mtl_dp_filter(struct xdp_md* ctx) {
ret = parse_udphdr(&nh, data_end, &udphdr);
if (ret < 0) return XDP_PASS;

int dst_port = bpf_ntohs(udphdr->dest);
__u16 dst_port = bpf_ntohs(udphdr->dest);
if (lookup_udp4_dp(dst_port) == 0) return XDP_PASS;

/* go to next program: xsk_def_prog */
Expand Down
11 changes: 7 additions & 4 deletions manager/mtl_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ int mtl_interface::update_udp_dp_filter(uint16_t dst_port, bool add) {
return 0;
}

int value = add ? 1 : 0;
if (bpf_map_update_elem(udp4_dp_filter_fd, &dst_port, &value, BPF_ANY) < 0) {
uint8_t value = add ? 1 : 0;
int ret = bpf_map_update_elem(udp4_dp_filter_fd, &dst_port, &value, BPF_ANY);
if (ret < 0) {
log(log_level::ERROR,
"Failed to update udp4_dp_filter map, dst_port: " + std::to_string(dst_port));
"Failed to update udp4_dp_filter map, dst_port: " + std::to_string(dst_port) +
", error: " + std::to_string(ret));
return -1;
}

Expand Down Expand Up @@ -440,7 +442,8 @@ int mtl_interface::load_xdp() {
return -1;
}

log(log_level::INFO, "Loaded xdp prog.");
log(log_level::INFO,
"Loaded xdp prog succ, udp4_dp_filter_fd: " + std::to_string(udp4_dp_filter_fd));
return 0;
}

Expand Down

0 comments on commit 002b38e

Please sign in to comment.