Skip to content

Commit e5a8cba

Browse files
committed
Modifications as per feedback
Signed-off-by: David-VTUK <[email protected]>
1 parent fe5a070 commit e5a8cba

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

xdp-bpf2go-example/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
xdp-bpf2go-example
1+
xdp-bpf2go-example
2+
**bpfeb.go
3+
**bpfel.go

xdp-bpf2go-example/main.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import (
1414
)
1515

1616
func main() {
17+
// Check if running as root
18+
if os.Geteuid() != 0 {
19+
log.Fatal("This program must be run as root!")
20+
}
21+
1722
// Remove resource limits for kernels <5.11.
1823
if err := rlimit.RemoveMemlock(); err != nil {
1924
log.Fatal("Removing memlock:", err)
@@ -26,7 +31,12 @@ func main() {
2631
}
2732
defer objs.Close()
2833

29-
ifname := "eno2" // Change this to an interface on your machine.
34+
// Capture user input from command-line arguments
35+
if len(os.Args) < 2 {
36+
log.Fatal("Please provide a network interface name as an argument")
37+
}
38+
ifname := os.Args[1] // Get the interface name from the first argument
39+
3040
iface, err := net.InterfaceByName(ifname)
3141
if err != nil {
3242
log.Fatalf("Getting interface %s: %s", ifname, err)
@@ -50,7 +60,6 @@ func main() {
5060
for {
5161
select {
5262
case <-tick:
53-
// log.Print(objs.ProtocolCount)
5463
printMap(objs.ProtocolCount)
5564
if err != nil {
5665
log.Fatal("Map lookup:", err)

xdp-bpf2go-example/packetProtocol.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <bpf/bpf_helpers.h>
55
#include <linux/if_ether.h>
66
#include <linux/ip.h>
7+
#include <bpf/bpf_endian.h>
78

89
struct{
910
__uint(type, BPF_MAP_TYPE_ARRAY);
@@ -17,30 +18,28 @@ int get_packet_protocol(struct xdp_md *ctx) {
1718

1819
void *data_end = (void *)(long)ctx->data_end;
1920
void *data = (void *)(long)ctx->data;
21+
struct ethhdr *eth = data;
22+
struct iphdr *ip = data + sizeof(struct ethhdr);
23+
__u32 key = ip->protocol; // Using IP protocol as the key
24+
__u64 *count = bpf_map_lookup_elem(&protocol_count, &key);
2025

2126
// Parse Ethernet header
22-
struct ethhdr *eth = data;
23-
if ((void *)(eth + 1) > data_end) {
27+
if ((void *)(eth + 1) > data_end)
2428
return XDP_PASS;
25-
}
2629

2730
// Check if the packet is an IP packet
28-
if (eth->h_proto != __constant_htons(ETH_P_IP)) {
31+
if (eth->h_proto != bpf_htons(ETH_P_IP)) {
2932
return XDP_PASS;
3033
}
3134

3235
// Parse IP header
33-
struct iphdr *ip = data + sizeof(struct ethhdr);
3436
if ((void *)(ip + 1) > data_end) {
3537
return XDP_PASS;
3638
}
3739

38-
__u32 key = ip->protocol; // Using IP protocol as the key
39-
__u64 *count = bpf_map_lookup_elem(&protocol_count, &key);
40-
if (count) {
40+
if (count)
4141
__sync_fetch_and_add(count, 1);
42-
}
43-
42+
4443
return XDP_PASS;
4544
}
4645

0 commit comments

Comments
 (0)