Skip to content

Commit 4fb6057

Browse files
author
Jann Chang
committed
first commit
0 parents  commit 4fb6057

File tree

7 files changed

+1441
-0
lines changed

7 files changed

+1441
-0
lines changed

Diff for: .gitignore

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.obj
8+
*.elf
9+
10+
# Linker output
11+
*.ilk
12+
*.map
13+
*.exp
14+
15+
# Precompiled Headers
16+
*.gch
17+
*.pch
18+
19+
# Libraries
20+
*.lib
21+
*.a
22+
*.la
23+
*.lo
24+
25+
# Shared objects (inc. Windows DLLs)
26+
*.dll
27+
*.so
28+
*.so.*
29+
*.dylib
30+
31+
# Executables
32+
*.exe
33+
*.out
34+
*.app
35+
*.i*86
36+
*.x86_64
37+
*.hex
38+
39+
# Debug files
40+
*.dSYM/
41+
*.su
42+
*.idb
43+
*.pdb
44+
45+
# Kernel Module Compile Results
46+
*.mod*
47+
*.cmd
48+
.tmp_versions/
49+
modules.order
50+
Module.symvers
51+
Mkfile.old
52+
dkms.conf
53+
54+
# Generated files
55+
lib/libbpf/src/build
56+
pkg/*/bin
57+
58+
main
59+
__debug_bin
60+
61+
export
62+
venv
63+
.DS_Store
64+
.vscode

Diff for: cmd/xdp.go

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"time"
9+
10+
"github.com/mintxtinm/packet-monitor/pkg/fileserver"
11+
"github.com/mintxtinm/packet-monitor/pkg/xdp"
12+
"github.com/sirupsen/logrus"
13+
"github.com/urfave/cli/v2"
14+
"golang.org/x/sync/errgroup"
15+
)
16+
17+
// LoadXdpTracerCommand load xdp tracer command
18+
var LoadXdpTracerCommand = &cli.Command{
19+
Name: "load",
20+
Usage: "Load new XDP dependencies graph exporter",
21+
Flags: []cli.Flag{
22+
&cli.StringFlag{
23+
Name: "ifindex",
24+
Value: "",
25+
Usage: "Network interface device name",
26+
},
27+
&cli.IntFlag{
28+
Name: "interval",
29+
Value: 30,
30+
Usage: "Output time interval",
31+
},
32+
33+
&cli.StringFlag{
34+
Name: "output",
35+
Value: "",
36+
Usage: "Output path",
37+
},
38+
&cli.BoolFlag{
39+
Name: "verbose",
40+
Value: false,
41+
Usage: "Verbose",
42+
},
43+
},
44+
Action: func(c *cli.Context) (err error) {
45+
var (
46+
ifIndexName = c.String("ifindex")
47+
ins *xdp.Instance
48+
g errgroup.Group
49+
)
50+
51+
if !c.IsSet("ifindex") {
52+
return fmt.Errorf("ifindex not provided")
53+
}
54+
55+
logrus.Info("XDP dependencies graph exporter loaded on: ", ifIndexName)
56+
57+
g.Go(func() error {
58+
ins = xdp.New(context.Background(), &xdp.Config{
59+
IfIndexName: ifIndexName,
60+
})
61+
return ins.Load(c)
62+
})
63+
64+
// reset graph after every cycle
65+
g.Go(func() (err error) {
66+
for {
67+
if ins == nil {
68+
continue
69+
}
70+
71+
time.Sleep(time.Duration(c.Int("interval")) * time.Second)
72+
73+
logrus.Info(ins.DepGraph.String())
74+
75+
if c.IsSet("output") {
76+
if err = ins.DepGraph.Export(); err != nil {
77+
break
78+
}
79+
}
80+
81+
ins.DepGraph.Reset()
82+
}
83+
return
84+
})
85+
86+
if c.IsSet("output") {
87+
g.Go(func() error {
88+
fileserver.Init()
89+
return nil
90+
})
91+
}
92+
93+
if err = g.Wait(); err != nil {
94+
logrus.Fatal(err)
95+
return
96+
}
97+
98+
return
99+
},
100+
}
101+
102+
// UnloadXdpTracerCommand unload xdp tracer command
103+
var UnloadXdpTracerCommand = &cli.Command{
104+
Name: "unload",
105+
Usage: "Unload XDP dependencies graph exporter",
106+
Flags: []cli.Flag{
107+
&cli.StringFlag{
108+
Name: "ifindex",
109+
Value: "",
110+
Usage: "Network interface device name",
111+
},
112+
},
113+
Action: func(c *cli.Context) (err error) {
114+
var (
115+
ifIndexName = c.String("ifindex")
116+
)
117+
118+
if !c.IsSet("ifindex") {
119+
return fmt.Errorf("ifindex not provided")
120+
}
121+
122+
// Execute bpf loader
123+
cmd := exec.Command("./main", "unload", ifIndexName)
124+
cmd.Dir = "./xdp"
125+
out, err := cmd.CombinedOutput()
126+
if err != nil {
127+
logrus.Error(string(out))
128+
logrus.Errorf("cmd.Run() failed with %s\n", err)
129+
os.Exit(-1)
130+
}
131+
132+
logrus.Info("XDP dependencies graph exporter unloaded on: ", ifIndexName)
133+
134+
return
135+
},
136+
}

0 commit comments

Comments
 (0)