-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix * add tests * export program info * delete ebpfapi post build * fix tests * code cleanup * cr comment
- Loading branch information
1 parent
f026933
commit c10f37f
Showing
8 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
// | ||
|
||
#include "bpf_endian.h" | ||
#include "bpf_helpers.h" | ||
|
||
// Map configured by user mode to drop packets from a specific interface. | ||
struct | ||
{ | ||
__uint(type, BPF_MAP_TYPE_ARRAY); | ||
__type(key, uint32_t); | ||
__type(value, uint32_t); | ||
__uint(max_entries, 1); | ||
} interface_map SEC(".maps"); | ||
|
||
// Map to track the number of dropped packets. | ||
struct | ||
{ | ||
__uint(type, BPF_MAP_TYPE_ARRAY); | ||
__type(key, uint32_t); | ||
__type(value, uint64_t); | ||
__uint(max_entries, 1); | ||
} dropped_packet_map SEC(".maps"); | ||
|
||
SEC("xdp/selective_drop") | ||
int | ||
selective_drop(xdp_md_t *ctx) | ||
{ | ||
int action = XDP_PASS; | ||
int zero = 0; | ||
|
||
uint32_t *interface_index = bpf_map_lookup_elem(&interface_map, &zero); | ||
if (!interface_index) { | ||
bpf_printk("selective_drop: interface_map lookup failed."); | ||
goto Exit; | ||
} | ||
|
||
if (*interface_index == ctx->ingress_ifindex) { | ||
action = XDP_DROP; | ||
bpf_printk("selective_drop: interface_map lookup found matching interface %d", *interface_index); | ||
|
||
uint64_t *dropped_packet_count = bpf_map_lookup_elem(&dropped_packet_map, &zero); | ||
if (dropped_packet_count) { | ||
*dropped_packet_count += 1; | ||
} else { | ||
uint64_t dropped_packet_count = 1; | ||
bpf_map_update_elem(&dropped_packet_map, &zero, &dropped_packet_count, BPF_ANY); | ||
} | ||
} else { | ||
bpf_printk("selective_drop: interface_map lookup found non-matching interface %d, expected %d", *interface_index, ctx->ingress_ifindex); | ||
} | ||
|
||
Exit: | ||
return action; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters