summaryrefslogtreecommitdiff
path: root/usage
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-07-06 16:13:08 +0530
committersinanmohd <sinan@sinanmohd.com>2024-07-06 16:19:26 +0530
commitf2af6ba15466f7ac39f2c12ea29fab5b0c5415c4 (patch)
tree605f2075aadd4ff81732e572f27132fff8a47de4 /usage
parent4ef5f7b379b37baeb3b6de1999be16d089e29738 (diff)
usage/Init: init
Diffstat (limited to 'usage')
-rw-r--r--usage/main.go47
1 files changed, 38 insertions, 9 deletions
diff --git a/usage/main.go b/usage/main.go
index 2293281..8be8ed3 100644
--- a/usage/main.go
+++ b/usage/main.go
@@ -28,34 +28,63 @@ type Usage struct {
Data UsageMap
Mutex sync.Mutex
objs bpfObjects
+ egressLink, ingressLink link.Link
}
-func (u *Usage) Run(iface *net.Interface, queries *db.Queries, ctxDb context.Context) {
+func (u *Usage) Init(iface *net.Interface) error {
+ var err error
+
if err := loadBpfObjects(&u.objs, nil); err != nil {
- log.Fatalf("loading objects: %s", err)
+ log.Printf("loading objects: %s", err)
+ return err;
}
- defer u.objs.Close()
+ defer func() {
+ if err != nil {
+ u.objs.Close()
+ }
+ }()
- ingressLink, err := link.AttachTCX(link.TCXOptions{
+ u.ingressLink, err = link.AttachTCX(link.TCXOptions{
Interface: iface.Index,
Program: u.objs.IngressFunc,
Attach: ebpf.AttachTCXIngress,
})
if err != nil {
- log.Fatalf("could not attach TCx program: %s", err)
+ log.Printf("could not attach TCx program: %s", err)
+ return err
}
- defer ingressLink.Close()
+ defer func() {
+ if err != nil {
+ u.ingressLink.Close()
+ }
+ }()
- egressLink, err := link.AttachTCX(link.TCXOptions{
+ u.egressLink, err = link.AttachTCX(link.TCXOptions{
Interface: iface.Index,
Program: u.objs.EgressFunc,
Attach: ebpf.AttachTCXEgress,
})
if err != nil {
- log.Fatalf("could not attach TCx program: %s", err)
+ log.Printf("could not attach TCx program: %s", err)
+ return err
}
- defer egressLink.Close()
+ u.Data = make(UsageMap)
+ return nil
+}
+
+func (u *Usage) CleanUp(queries *db.Queries, ctxDb context.Context) {
+ err := u.UpdateDb(queries, ctxDb, false)
+ if err != nil {
+ log.Printf("updating Database: %s", err)
+ }
+
+ u.objs.Close()
+ u.ingressLink.Close()
+ u.egressLink.Close()
+}
+
+func (u *Usage) Run(iface *net.Interface, queries *db.Queries, ctxDb context.Context) {
bpfTicker := time.NewTicker(time.Second)
defer bpfTicker.Stop()
dbTicker := time.NewTicker(time.Minute)