Skip to content

Commit 9e81874

Browse files
authored
add flowlogs-dump example (#21)
1 parent 2527326 commit 9e81874

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export FLOWS_TARGET_HOST=...
2626
export FLOWS_TARGET_PORT=...
2727
sudo -E bin/netobserv-ebpf-agent
2828
```
29-
29+
To deploy locally, use instructions from [flowlogs-dump (like tcpdump)](./examples/flowlogs-dump/README.md).
3030
To deploy it as a Pod, you can check the [deployment example](./examples/performance/deployment.yml).
3131

3232
## Where is the collector?

examples/flowlogs-dump/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# flowlogs-dump (like tcpdump)
2+
3+
## How to run
4+
5+
From the root directory of the project:
6+
7+
Build the agent (the flowlogs client that uses ebpf) using:
8+
```bash
9+
make build
10+
```
11+
Build the flowlogs-dump-collector (the server that receives logs from the agent and dumps to screen) using:
12+
```bash
13+
go build -mod vendor -o bin/flowlogs-dump-collector examples/flowlogs-dump/server/flowlogs-dump-collector.go
14+
```
15+
Start the agent using:
16+
```bash
17+
sudo FLOWS_TARGET_HOST=127.0.0.1 FLOWS_TARGET_PORT=9999 ./bin/netobserv-ebpf-agent
18+
```
19+
20+
Start the flowlogs-dump-collector using: (in a secondary shell)
21+
```bash
22+
./bin/flowlogs-dump-collector -listen_port=9999
23+
```
24+
25+
You should see output such as:
26+
```bash
27+
starting flowlogs-dump-collector on port 9999
28+
13:31:38.857689 eth0 IP 192.168.50.88:5353 > 224.0.0.251:5353: proto:2048 dir:0 bytes:384 packets:2 ends: 13:31:38.859561
29+
13:31:38.858447 eth0 IP 0.0.0.0:0 > 0.0.0.0:0: proto:34525 dir:0 bytes:424 packets:2 ends: 13:31:38.860284
30+
13:31:37.409071 eth0 IP 192.168.50.16:2221 > 192.168.50.88:59239: proto:2048 dir:1 bytes:371806 packets:403 ends: 13:31:42.342690
31+
13:31:37.408148 eth0 IP 192.168.50.88:59239 > 192.168.50.16:2221: proto:2048 dir:0 bytes:16926 packets:277 ends: 13:31:42.390777
32+
...
33+
```
34+
35+
36+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (C) 2022 IBM, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package main
19+
20+
import (
21+
"flag"
22+
"log"
23+
"net"
24+
25+
"github.com/netobserv/netobserv-ebpf-agent/pkg/grpc"
26+
"github.com/netobserv/netobserv-ebpf-agent/pkg/pbflow"
27+
)
28+
29+
var (
30+
port = flag.Int("listen_port", 9999, "TCP port to listen for flows")
31+
)
32+
33+
var protocolByNumber = map[uint32]string{
34+
1: "icmp",
35+
2: "igmp",
36+
6: "tcp",
37+
17: "udp",
38+
58: "ipv6-icmp",
39+
}
40+
41+
func ipIntToNetIP(ipAsInt uint32) net.IP {
42+
var bytes [4]byte
43+
bytes[0] = byte(ipAsInt & 0xFF)
44+
bytes[1] = byte((ipAsInt >> 8) & 0xFF)
45+
bytes[2] = byte((ipAsInt >> 16) & 0xFF)
46+
bytes[3] = byte((ipAsInt >> 24) & 0xFF)
47+
48+
return net.IPv4(bytes[3], bytes[2], bytes[1], bytes[0])
49+
}
50+
51+
// tcpdump flow collector
52+
func main() {
53+
log.SetFlags(0)
54+
flag.Parse()
55+
56+
receivedRecords := make(chan *pbflow.Records, 100)
57+
log.Println("starting flowlogs-dump-collector on port", *port)
58+
go func() {
59+
_, err := grpc.StartCollector(*port, receivedRecords)
60+
if err != nil {
61+
panic(err)
62+
}
63+
}()
64+
for records := range receivedRecords {
65+
for _, record := range records.Entries {
66+
log.Printf("%v %s IP %s:%d > %s:%d: protocol:%s dir:%d bytes:%d packets:%d ends: %v\n",
67+
record.TimeFlowStart.AsTime().Local().Format("15:04:05.000000"),
68+
record.Interface,
69+
ipIntToNetIP(record.Network.GetSrcAddr().GetIpv4()).String(),
70+
record.Transport.SrcPort,
71+
ipIntToNetIP(record.Network.GetDstAddr().GetIpv4()).String(),
72+
record.Transport.DstPort,
73+
protocolByNumber[record.Transport.Protocol],
74+
record.Direction,
75+
record.Bytes,
76+
record.Packets,
77+
record.TimeFlowEnd.AsTime().Local().Format("15:04:05.000000"),
78+
)
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)