GithubHelp home page GithubHelp logo

Comments (2)

lmb avatar lmb commented on June 20, 2024

You're not checking srcTrafficIter.Err(). Otherwise you'd see the following error:

look up next key: per-cpu value requires a slice or a pointer to slice

Here is a diff to one of the examples which shows how to use it:

diff --git a/examples/xdp/bpf_bpfeb.o b/examples/xdp/bpf_bpfeb.o
index f90c8b99..9ab3a59f 100644
Binary files a/examples/xdp/bpf_bpfeb.o and b/examples/xdp/bpf_bpfeb.o differ
diff --git a/examples/xdp/bpf_bpfel.o b/examples/xdp/bpf_bpfel.o
index 35dba4b8..e708d827 100644
Binary files a/examples/xdp/bpf_bpfel.o and b/examples/xdp/bpf_bpfel.o differ
diff --git a/examples/xdp/main.go b/examples/xdp/main.go
index 123513b1..99796157 100644
--- a/examples/xdp/main.go
+++ b/examples/xdp/main.go
@@ -72,10 +72,10 @@ func formatMapContents(m *ebpf.Map) (string, error) {
 	var (
 		sb  strings.Builder
 		key netip.Addr
-		val uint32
+		val = make([]uint32, ebpf.MustPossibleCPU())
 	)
 	iter := m.Iterate()
-	for iter.Next(&key, &val) {
+	for iter.Next(&key, val) {
 		sourceIP := key // IPv4 source address in network byte order.
 		packetCount := val
 		sb.WriteString(fmt.Sprintf("\t%s => %d\n", sourceIP, packetCount))
diff --git a/examples/xdp/xdp.c b/examples/xdp/xdp.c
index 2f36c161..ac414b66 100644
--- a/examples/xdp/xdp.c
+++ b/examples/xdp/xdp.c
@@ -9,7 +9,7 @@ char __license[] SEC("license") = "Dual MIT/GPL";
 
 /* Define an LRU hash map for storing packet count by source IPv4 address */
 struct {
-	__uint(type, BPF_MAP_TYPE_LRU_HASH);
+	__uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH);
 	__uint(max_entries, MAX_MAP_ENTRIES);
 	__type(key, __u32); // source IPv4 address
 	__type(value, __u32); // packet count

Output:

2024/01/08 12:15:10 Attached XDP program to iface "lo" (index 1)
2024/01/08 12:15:10 Press Ctrl-C to exit and remove the program
2024/01/08 12:15:11 Map contents:
2024/01/08 12:15:12 Map contents:
2024/01/08 12:15:13 Map contents:
	127.0.0.1 => [0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0]
2024/01/08 12:15:14 Map contents:
	127.0.0.1 => [2 2 4 0 5 0 0 0 0 0 0 0 0 2 0 0]
2024/01/08 12:15:15 Map contents:
	127.0.0.1 => [2 2 4 0 5 0 0 0 0 0 0 0 0 2 0 0]

from ebpf.

HouzuoGuo avatar HouzuoGuo commented on June 20, 2024

That's very cool, thank you!

from ebpf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.