From e5e71e5c0ef9130e23c89c070e95c577a27f3a24 Mon Sep 17 00:00:00 2001 From: Atul-source Date: Fri, 30 Aug 2024 21:15:37 +0530 Subject: [PATCH] addition fixes Signed-off-by: Atul-source --- apis/configwatch.go | 11 +- apis/handlers/restart_linux.go | 20 +- bpfprogs/bpfdebug.go | 8 +- bpfprogs/nfconfig.go | 355 ++++++++++++++++++--------------- main.go | 8 +- models/l3afd.go | 3 +- restart/restart.go | 65 +++--- stats/metrics.go | 8 +- 8 files changed, 264 insertions(+), 214 deletions(-) diff --git a/apis/configwatch.go b/apis/configwatch.go index fb705b16..dcbbc476 100644 --- a/apis/configwatch.go +++ b/apis/configwatch.go @@ -60,7 +60,7 @@ func StartConfigWatcher(ctx context.Context, hostname, daemonName string, conf * }, SANMatchRules: conf.MTLSSANMatchRules, } - if _, ok := models.AllNetListeners["main_http"]; !ok { + if _, ok := models.AllNetListeners.Load("main_http"); !ok { tcpAddr, err := net.ResolveTCPAddr("tcp", conf.L3afConfigsRestAPIAddr) if err != nil { return fmt.Errorf("error resolving TCP address:%w", err) @@ -69,7 +69,7 @@ func StartConfigWatcher(ctx context.Context, hostname, daemonName string, conf * if err != nil { return fmt.Errorf("creating tcp listner failed with %w", err) } - models.AllNetListeners["main_http"] = listener + models.AllNetListeners.Store("main_http", listener) } term := make(chan os.Signal, 1) signal.Notify(term, signals.ShutdownSignals...) @@ -91,7 +91,8 @@ func StartConfigWatcher(ctx context.Context, hostname, daemonName string, conf * if !conf.MTLSEnabled && !isLoopback(conf.L3afConfigsRestAPIAddr) && conf.Environment == config.ENV_PROD { conf.MTLSEnabled = true } - + val, _ := models.AllNetListeners.Load("main_http") + l, _ := val.(*net.TCPListener) if conf.MTLSEnabled { log.Info().Msgf("l3afd server listening with mTLS - %s ", conf.L3afConfigsRestAPIAddr) // Create a CA certificate pool and add client ca's to it @@ -148,12 +149,12 @@ func StartConfigWatcher(ctx context.Context, hostname, daemonName string, conf * } } }() - if err := s.l3afdServer.ServeTLS(models.AllNetListeners["main_http"], serverCertFile, serverKeyFile); !errors.Is(err, http.ErrServerClosed) { + if err := s.l3afdServer.ServeTLS(l, serverCertFile, serverKeyFile); !errors.Is(err, http.ErrServerClosed) { log.Fatal().Err(err).Msgf("failed to start L3AFD server with mTLS enabled") } } else { log.Info().Msgf("l3afd server listening - %s ", conf.L3afConfigsRestAPIAddr) - if err := s.l3afdServer.Serve(models.AllNetListeners["main_http"]); !errors.Is(err, http.ErrServerClosed) { + if err := s.l3afdServer.Serve(l); !errors.Is(err, http.ErrServerClosed) { log.Fatal().Err(err).Msgf("failed to start L3AFD server") } } diff --git a/apis/handlers/restart_linux.go b/apis/handlers/restart_linux.go index 6cc2914b..27d04a1b 100644 --- a/apis/handlers/restart_linux.go +++ b/apis/handlers/restart_linux.go @@ -26,9 +26,9 @@ import ( "github.com/l3af-project/l3afd/v2/restart" ) -// HandleRestart Store meta data about ebpf programs and exit -// @Summary Store meta data about ebpf programs and exit -// @Description Store meta data about ebpf programs and exit +// HandleRestart will start new instance of l3afd provided by payload +// @Summary this api will start new instance of l3afd provided by payload +// @Description this api will start new instance of l3afd provided by payload // @Accept json // @Produce json // @Param cfgs body []models.L3afBPFPrograms true "BPF programs" @@ -145,7 +145,10 @@ func HandleRestart(bpfcfg *bpfprogs.NFConfigs) http.HandlerFunc { srvToIndex["stat_http"] = 0 srvToIndex["main_http"] = 1 srvToIndex["debug_http"] = 2 - for srv, lis := range models.AllNetListeners { + isErr := false + models.AllNetListeners.Range(func(srvr, listr interface{}) bool { // iterate over the map + srv, _ := srvr.(string) + lis, _ := listr.(*net.TCPListener) idx := srvToIndex[srv] lf, err := lis.File() if err != nil { @@ -153,10 +156,15 @@ func HandleRestart(bpfcfg *bpfprogs.NFConfigs) http.HandlerFunc { err = restart.RollBackSymlink(oldCfgPath, oldBinPath, oldVersion, t.Version, bpfcfg.HostConfig) mesg = mesg + fmt.Sprintf("rollback of symlink failed: %v", err) statusCode = http.StatusInternalServerError - return + isErr = true + return false } newFile := os.NewFile(uintptr(lf.Fd()), "dupFdlistner"+strconv.Itoa(idx)) files[idx] = newFile + return true + }) + if isErr { + return } // we have added cmd := exec.Command(bpfcfg.HostConfig.BasePath+"/latest/l3afd", "--config", bpfcfg.HostConfig.BasePath+"/latest/l3afd.cfg") @@ -251,7 +259,7 @@ func HandleRestart(bpfcfg *bpfprogs.NFConfigs) http.HandlerFunc { mesg = mesg + fmt.Sprintf("rollback of symlink failed: %v", err) } statusCode = http.StatusInternalServerError - log.Err(<-srverror) + log.Err(terr) return } break diff --git a/bpfprogs/bpfdebug.go b/bpfprogs/bpfdebug.go index 227acba6..eeb5d3d9 100644 --- a/bpfprogs/bpfdebug.go +++ b/bpfprogs/bpfdebug.go @@ -20,7 +20,7 @@ var bpfcfgs *NFConfigs func SetupBPFDebug(ebpfChainDebugAddr string, BPFConfigs *NFConfigs) { bpfcfgs = BPFConfigs go func() { - if _, ok := models.AllNetListeners["debug_http"]; !ok { + if _, ok := models.AllNetListeners.Load("debug_http"); !ok { tcpAddr, err := net.ResolveTCPAddr("tcp", ebpfChainDebugAddr) if err != nil { fmt.Println("Error resolving TCP address:", err) @@ -30,12 +30,14 @@ func SetupBPFDebug(ebpfChainDebugAddr string, BPFConfigs *NFConfigs) { if err != nil { log.Fatal().Err(err).Msgf("Not able to create net Listen") } - models.AllNetListeners["debug_http"] = listener + models.AllNetListeners.Store("debug_http", listener) } http.HandleFunc("/bpfs/", ViewHandler) // We just need to start a server. log.Info().Msg("Starting BPF debug server") - if err := http.Serve(models.AllNetListeners["debug_http"], nil); !errors.Is(err, http.ErrServerClosed) { + val, _ := models.AllNetListeners.Load("debug_http") + l, _ := val.(*net.TCPListener) + if err := http.Serve(l, nil); !errors.Is(err, http.ErrServerClosed) { log.Fatal().Err(err).Msg("failed to start BPF chain debug server") } }() diff --git a/bpfprogs/nfconfig.go b/bpfprogs/nfconfig.go index f72a44b2..aaba97c1 100644 --- a/bpfprogs/nfconfig.go +++ b/bpfprogs/nfconfig.go @@ -1489,8 +1489,10 @@ func SerilazeProgram(e *list.Element) *models.L3AFMetaData { tmp := &models.L3AFMetaData{} bpf := e.Value.(*BPF) tmp.BpfMaps = make([]string, 0) - for _, v := range bpf.BpfMaps { - tmp.BpfMaps = append(tmp.BpfMaps, v.Name) + if bpf.BpfMaps != nil { + for _, v := range bpf.BpfMaps { + tmp.BpfMaps = append(tmp.BpfMaps, v.Name) + } } tmp.FilePath = bpf.FilePath tmp.MapNamePath = bpf.MapNamePath @@ -1504,30 +1506,34 @@ func SerilazeProgram(e *list.Element) *models.L3AFMetaData { Programs: make([]string, 0), Maps: make([]string, 0), } - for k, v := range bpf.ProgMapCollection.Programs { - if v.Type() == ebpf.XDP || v.Type() == ebpf.SchedACT || v.Type() == ebpf.SchedCLS { - tmp.ProgMapCollection.Programs = append(tmp.ProgMapCollection.Programs, k) + if bpf.ProgMapCollection != nil { + for k, v := range bpf.ProgMapCollection.Programs { + if v.Type() == ebpf.XDP || v.Type() == ebpf.SchedACT || v.Type() == ebpf.SchedCLS { + tmp.ProgMapCollection.Programs = append(tmp.ProgMapCollection.Programs, k) + } + } + for k := range bpf.ProgMapCollection.Maps { + tmp.ProgMapCollection.Maps = append(tmp.ProgMapCollection.Maps, k) } - } - for k := range bpf.ProgMapCollection.Maps { - tmp.ProgMapCollection.Maps = append(tmp.ProgMapCollection.Maps, k) } tmp.MetricsBpfMaps = make(map[string]models.MetaMetricsBPFMap) - for k1, v1 := range bpf.MetricsBpfMaps { - values := make([]float64, 0) - tmpval := v1.Values - for i := 0; i < v1.Values.Len(); i++ { - if tmpval.Value != nil { - values = append(values, tmpval.Value.(float64)) + if bpf.MetricsBpfMaps != nil { + for k1, v1 := range bpf.MetricsBpfMaps { + values := make([]float64, 0) + tmpval := v1.Values + for i := 0; i < v1.Values.Len(); i++ { + if tmpval.Value != nil { + values = append(values, tmpval.Value.(float64)) + } + tmpval = tmpval.Next() + } + tmp.MetricsBpfMaps[k1] = models.MetaMetricsBPFMap{ + MapName: v1.Name, + Key: v1.Key, + Values: values, + Aggregator: v1.Aggregator, + LastValue: float64(v1.LastValue), } - tmpval = tmpval.Next() - } - tmp.MetricsBpfMaps[k1] = models.MetaMetricsBPFMap{ - MapName: v1.Name, - Key: v1.Key, - Values: values, - Aggregator: v1.Aggregator, - LastValue: float64(v1.LastValue), } } tmp.XDPLink = false @@ -1546,31 +1552,36 @@ func (c *NFConfigs) GetL3AFHOSTDATA() models.L3AFALLHOSTDATA { result.IngressTCBpfs = make(map[string][]*models.L3AFMetaData) result.EgressTCBpfs = make(map[string][]*models.L3AFMetaData) result.ProbesBpfs = make([]models.L3AFMetaData, 0) - for k, v := range c.IngressXDPBpfs { - ls := make([]*models.L3AFMetaData, 0) - for e := v.Front(); e != nil; e = e.Next() { - ls = append(ls, SerilazeProgram(e)) + if c.IngressXDPBpfs != nil { + for k, v := range c.IngressXDPBpfs { + ls := make([]*models.L3AFMetaData, 0) + for e := v.Front(); e != nil; e = e.Next() { + ls = append(ls, SerilazeProgram(e)) + } + result.IngressXDPBpfs[k] = ls } - result.IngressXDPBpfs[k] = ls } - for k, v := range c.IngressTCBpfs { - ls := make([]*models.L3AFMetaData, 0) - for e := v.Front(); e != nil; e = e.Next() { - ls = append(ls, SerilazeProgram(e)) + if c.IngressTCBpfs != nil { + for k, v := range c.IngressTCBpfs { + ls := make([]*models.L3AFMetaData, 0) + for e := v.Front(); e != nil; e = e.Next() { + ls = append(ls, SerilazeProgram(e)) + } + result.IngressTCBpfs[k] = ls } - result.IngressTCBpfs[k] = ls } - for k, v := range c.EgressTCBpfs { - ls := make([]*models.L3AFMetaData, 0) - for e := v.Front(); e != nil; e = e.Next() { - ls = append(ls, SerilazeProgram(e)) + if c.EgressTCBpfs != nil { + for k, v := range c.EgressTCBpfs { + ls := make([]*models.L3AFMetaData, 0) + for e := v.Front(); e != nil; e = e.Next() { + ls = append(ls, SerilazeProgram(e)) + } + result.EgressTCBpfs[k] = ls } - result.EgressTCBpfs[k] = ls } for e := c.ProbesBpfs.Front(); e != nil; e = e.Next() { result.ProbesBpfs = append(result.ProbesBpfs, *SerilazeProgram(e)) } - metrics, _ := prometheus.DefaultGatherer.Gather() result.AllStats = make([]models.MetricVec, 0) listofMetrics := []string{"l3afd_BPFStartCount", "l3afd_BPFStopCount", "l3afd_BPFUpdateCount", "l3afd_BPFUpdateFailedCount", "l3afd_BPFRunning", "l3afd_BPFStartTime", "l3afd_BPFMonitorMap"} @@ -1602,125 +1613,135 @@ func (c *NFConfigs) GetL3AFHOSTDATA() models.L3AFALLHOSTDATA { } func (c *NFConfigs) StartAllUserProgramsAndProbes() error { - for iface, v := range c.IngressXDPBpfs { - for e := v.Front(); e != nil; e = e.Next() { - // Starting Probes - b := e.Value.(*BPF) - ef := b.Program.EntryFunctionName - b.Program.EntryFunctionName = "" - prg := b.ProgMapCollection - b.ProgMapCollection = nil - if err := b.LoadBPFProgram(iface); err != nil { - return fmt.Errorf("not able to load probes %w", err) - } - b.Program.EntryFunctionName = ef - for fk, vf := range b.ProgMapCollection.Programs { - if _, ok := prg.Programs[fk]; !ok { - prg.Programs[fk] = vf - } else { - vf.Close() + if c.IngressXDPBpfs != nil { + for iface, v := range c.IngressXDPBpfs { + for e := v.Front(); e != nil; e = e.Next() { + // Starting Probes + b := e.Value.(*BPF) + ef := b.Program.EntryFunctionName + b.Program.EntryFunctionName = "" + prg := b.ProgMapCollection + b.ProgMapCollection = nil + if err := b.LoadBPFProgram(iface); err != nil { + return fmt.Errorf("not able to load probes %w", err) } - } - for fk, vf := range b.ProgMapCollection.Maps { - if _, ok := prg.Maps[fk]; !ok { - prg.Maps[fk] = vf - } else { - vf.Close() + b.Program.EntryFunctionName = ef + if b.ProgMapCollection != nil { + for fk, vf := range b.ProgMapCollection.Programs { + if _, ok := prg.Programs[fk]; !ok { + prg.Programs[fk] = vf + } else { + vf.Close() + } + } + for fk, vf := range b.ProgMapCollection.Maps { + if _, ok := prg.Maps[fk]; !ok { + prg.Maps[fk] = vf + } else { + vf.Close() + } + } } - } - b.ProgMapCollection = prg - if len(b.Program.CmdStart) > 0 { - // Verify other instance is running - if err := StopExternalRunningProcess(b.Program.CmdStart); err != nil { - return fmt.Errorf("failed to stop external instance of the program %s with error : %w", b.Program.CmdStart, err) + b.ProgMapCollection = prg + if len(b.Program.CmdStart) > 0 { + // Verify other instance is running + if err := StopExternalRunningProcess(b.Program.CmdStart); err != nil { + return fmt.Errorf("failed to stop external instance of the program %s with error : %w", b.Program.CmdStart, err) + } } - } - if b.Program.UserProgramDaemon { - // Starting User Program - if err := b.StartUserProgram(iface, models.XDPIngressType, c.HostConfig.BpfChainingEnabled); err != nil { - return err + if b.Program.UserProgramDaemon { + // Starting User Program + if err := b.StartUserProgram(iface, models.XDPIngressType, c.HostConfig.BpfChainingEnabled); err != nil { + return err + } } } } } - - for iface, v := range c.IngressTCBpfs { - for e := v.Front(); e != nil; e = e.Next() { - b := e.Value.(*BPF) - ef := b.Program.EntryFunctionName - b.Program.EntryFunctionName = "" - prg := b.ProgMapCollection - b.ProgMapCollection = nil - if err := b.LoadBPFProgram(iface); err != nil { - return fmt.Errorf("not able to load probes %w", err) - } - b.Program.EntryFunctionName = ef - for fk, vf := range b.ProgMapCollection.Programs { - if _, ok := prg.Programs[fk]; !ok { - prg.Programs[fk] = vf - } else { - vf.Close() + if c.IngressTCBpfs != nil { + for iface, v := range c.IngressTCBpfs { + for e := v.Front(); e != nil; e = e.Next() { + b := e.Value.(*BPF) + ef := b.Program.EntryFunctionName + b.Program.EntryFunctionName = "" + prg := b.ProgMapCollection + b.ProgMapCollection = nil + if err := b.LoadBPFProgram(iface); err != nil { + return fmt.Errorf("not able to load probes %w", err) } - } - for fk, vf := range b.ProgMapCollection.Maps { - if _, ok := prg.Maps[fk]; !ok { - prg.Maps[fk] = vf - } else { - vf.Close() + b.Program.EntryFunctionName = ef + if b.ProgMapCollection != nil { + for fk, vf := range b.ProgMapCollection.Programs { + if _, ok := prg.Programs[fk]; !ok { + prg.Programs[fk] = vf + } else { + vf.Close() + } + } + for fk, vf := range b.ProgMapCollection.Maps { + if _, ok := prg.Maps[fk]; !ok { + prg.Maps[fk] = vf + } else { + vf.Close() + } + } } - } - b.ProgMapCollection = prg - if len(b.Program.CmdStart) > 0 { - // Verify other instance is running - if err := StopExternalRunningProcess(b.Program.CmdStart); err != nil { - return fmt.Errorf("failed to stop external instance of the program %s with error : %w", b.Program.CmdStart, err) + b.ProgMapCollection = prg + if len(b.Program.CmdStart) > 0 { + // Verify other instance is running + if err := StopExternalRunningProcess(b.Program.CmdStart); err != nil { + return fmt.Errorf("failed to stop external instance of the program %s with error : %w", b.Program.CmdStart, err) + } } - } - if b.Program.UserProgramDaemon { - // Starting User Program - if err := b.StartUserProgram(iface, models.XDPIngressType, c.HostConfig.BpfChainingEnabled); err != nil { - return err + if b.Program.UserProgramDaemon { + // Starting User Program + if err := b.StartUserProgram(iface, models.XDPIngressType, c.HostConfig.BpfChainingEnabled); err != nil { + return err + } } } } } - - for iface, v := range c.EgressTCBpfs { - for e := v.Front(); e != nil; e = e.Next() { - b := e.Value.(*BPF) - ef := b.Program.EntryFunctionName - b.Program.EntryFunctionName = "" - prg := b.ProgMapCollection - b.ProgMapCollection = nil - if err := b.LoadBPFProgram(iface); err != nil { - return fmt.Errorf("not able to load probes %w", err) - } - b.Program.EntryFunctionName = ef - for fk, vf := range b.ProgMapCollection.Programs { - if _, ok := prg.Programs[fk]; !ok { - prg.Programs[fk] = vf - } else { - vf.Close() + if c.EgressTCBpfs != nil { + for iface, v := range c.EgressTCBpfs { + for e := v.Front(); e != nil; e = e.Next() { + b := e.Value.(*BPF) + ef := b.Program.EntryFunctionName + b.Program.EntryFunctionName = "" + prg := b.ProgMapCollection + b.ProgMapCollection = nil + if err := b.LoadBPFProgram(iface); err != nil { + return fmt.Errorf("not able to load probes %w", err) } - } - for fk, vf := range b.ProgMapCollection.Maps { - if _, ok := prg.Maps[fk]; !ok { - prg.Maps[fk] = vf - } else { - vf.Close() + b.Program.EntryFunctionName = ef + if b.ProgMapCollection != nil { + for fk, vf := range b.ProgMapCollection.Programs { + if _, ok := prg.Programs[fk]; !ok { + prg.Programs[fk] = vf + } else { + vf.Close() + } + } + for fk, vf := range b.ProgMapCollection.Maps { + if _, ok := prg.Maps[fk]; !ok { + prg.Maps[fk] = vf + } else { + vf.Close() + } + } } - } - b.ProgMapCollection = prg - if len(b.Program.CmdStart) > 0 { - // Verify other instance is running - if err := StopExternalRunningProcess(b.Program.CmdStart); err != nil { - return fmt.Errorf("failed to stop external instance of the program %s with error : %w", b.Program.CmdStart, err) + b.ProgMapCollection = prg + if len(b.Program.CmdStart) > 0 { + // Verify other instance is running + if err := StopExternalRunningProcess(b.Program.CmdStart); err != nil { + return fmt.Errorf("failed to stop external instance of the program %s with error : %w", b.Program.CmdStart, err) + } } - } - if b.Program.UserProgramDaemon { - // Starting User Program - if err := b.StartUserProgram(iface, models.XDPIngressType, c.HostConfig.BpfChainingEnabled); err != nil { - return err + if b.Program.UserProgramDaemon { + // Starting User Program + if err := b.StartUserProgram(iface, models.XDPIngressType, c.HostConfig.BpfChainingEnabled); err != nil { + return err + } } } } @@ -1729,34 +1750,46 @@ func (c *NFConfigs) StartAllUserProgramsAndProbes() error { } func (c *NFConfigs) StopAllProbes() { - for _, v := range c.IngressXDPBpfs { - for e := v.Front(); e != nil; e = e.Next() { - // Starting Probes - b := e.Value.(*BPF) - for _, pb := range b.ProbeLinks { - (*pb).Close() + if c.IngressXDPBpfs != nil { + for _, v := range c.IngressXDPBpfs { + for e := v.Front(); e != nil; e = e.Next() { + // Starting Probes + b := e.Value.(*BPF) + if b.ProbeLinks != nil { + for _, pb := range b.ProbeLinks { + (*pb).Close() + } + } + b.ProbeLinks = make([]*link.Link, 0) } - b.ProbeLinks = make([]*link.Link, 0) } } - for _, v := range c.IngressTCBpfs { - for e := v.Front(); e != nil; e = e.Next() { - // Starting Probes - b := e.Value.(*BPF) - for _, pb := range b.ProbeLinks { - (*pb).Close() + if c.IngressTCBpfs != nil { + for _, v := range c.IngressTCBpfs { + for e := v.Front(); e != nil; e = e.Next() { + // Starting Probes + b := e.Value.(*BPF) + if b.ProbeLinks != nil { + for _, pb := range b.ProbeLinks { + (*pb).Close() + } + } + b.ProbeLinks = make([]*link.Link, 0) } - b.ProbeLinks = make([]*link.Link, 0) } } - for _, v := range c.EgressTCBpfs { - for e := v.Front(); e != nil; e = e.Next() { - // Starting Probes - b := e.Value.(*BPF) - for _, pb := range b.ProbeLinks { - (*pb).Close() + if c.EgressTCBpfs != nil { + for _, v := range c.EgressTCBpfs { + for e := v.Front(); e != nil; e = e.Next() { + // Starting Probes + b := e.Value.(*BPF) + if b.ProbeLinks != nil { + for _, pb := range b.ProbeLinks { + (*pb).Close() + } + } + b.ProbeLinks = make([]*link.Link, 0) } - b.ProbeLinks = make([]*link.Link, 0) } } } diff --git a/main.go b/main.go index d8e15cd4..23b37f96 100644 --- a/main.go +++ b/main.go @@ -83,7 +83,6 @@ func main() { models.IsReadOnly = false models.CurrentWriteReq = 0 models.StateLock = sync.Mutex{} - models.AllNetListeners = make(map[string]*net.TCPListener) setupLogging() ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -273,19 +272,18 @@ func setupForRestart(ctx context.Context, conf *config.Config) error { machineHostname, err := os.Hostname() HandleErr(err, "not able to fetch the hostname") - models.AllNetListeners = make(map[string]*net.TCPListener) l, err := restart.Getnetlistener(3, "stat_server") HandleErr(err, "getting stat_server listener failed") - models.AllNetListeners["stat_http"] = l + models.AllNetListeners.Store("stat_http", l) l, err = restart.Getnetlistener(4, "main_server") HandleErr(err, "getting main_server listener failed") - models.AllNetListeners["main_http"] = l + models.AllNetListeners.Store("main_http", l) if conf.EBPFChainDebugEnabled { l, err = restart.Getnetlistener(5, "debug_server") HandleErr(err, "getting main_server listener failed") - models.AllNetListeners["debug_http"] = l + models.AllNetListeners.Store("debug_http", l) } // setup Metrics endpoint stats.SetupMetrics(machineHostname, daemonName, conf.MetricsAddr) diff --git a/models/l3afd.go b/models/l3afd.go index c6312460..eece2acb 100644 --- a/models/l3afd.go +++ b/models/l3afd.go @@ -4,7 +4,6 @@ package models import ( - "net" "sync" ) @@ -165,7 +164,7 @@ type L3AFALLHOSTDATA struct { var CloseForRestart chan struct{} -var AllNetListeners map[string]*net.TCPListener +var AllNetListeners sync.Map var CurrentWriteReq int var StateLock sync.Mutex diff --git a/restart/restart.go b/restart/restart.go index 1a51efd6..9d86f1b2 100644 --- a/restart/restart.go +++ b/restart/restart.go @@ -77,6 +77,9 @@ func getCollection(input models.MetaColl, output **ebpf.Collection, b *bpfprogs. } func getMetricsMaps(input map[string]models.MetaMetricsBPFMap, b *bpfprogs.BPF, conf *config.Config, output *map[string]*bpfprogs.MetricsBPFMap, iface string) error { + if input == nil { + return nil + } for k, v := range input { fg := &bpfprogs.MetricsBPFMap{} var pinnedPath string @@ -200,43 +203,47 @@ func Convert(ctx context.Context, t models.L3AFALLHOSTDATA, hostconfig *config.C D.ProbesBpfs = *list.New() D.HostConfig = hostconfig D.Mu = new(sync.Mutex) - for k, v := range t.IngressXDPBpfs { - l := list.New() - for _, r := range v { - f, err := DeserilazeProgram(ctx, r, hostconfig, k) - if err != nil { - log.Err(err).Msg("Deserilization failed for xdpingress") - return nil, err + if t.IngressXDPBpfs != nil { + for k, v := range t.IngressXDPBpfs { + l := list.New() + for _, r := range v { + f, err := DeserilazeProgram(ctx, r, hostconfig, k) + if err != nil { + log.Err(err).Msg("Deserilization failed for xdpingress") + return nil, err + } + l.PushBack(f) } - l.PushBack(f) + D.IngressXDPBpfs[k] = l } - D.IngressXDPBpfs[k] = l } - - for k, v := range t.IngressTCBpfs { - l := list.New() - for _, r := range v { - f, err := DeserilazeProgram(ctx, r, hostconfig, k) - if err != nil { - log.Err(err).Msg("Deserilization failed for tcingress") - return nil, err + if t.IngressTCBpfs != nil { + for k, v := range t.IngressTCBpfs { + l := list.New() + for _, r := range v { + f, err := DeserilazeProgram(ctx, r, hostconfig, k) + if err != nil { + log.Err(err).Msg("Deserilization failed for tcingress") + return nil, err + } + l.PushBack(f) } - l.PushBack(f) + D.IngressTCBpfs[k] = l } - D.IngressTCBpfs[k] = l } - - for k, v := range t.EgressTCBpfs { - l := list.New() - for _, r := range v { - f, err := DeserilazeProgram(ctx, r, hostconfig, k) - if err != nil { - log.Err(err).Msg("Deserilization failed for tcegress") - return nil, err + if t.EgressTCBpfs != nil { + for k, v := range t.EgressTCBpfs { + l := list.New() + for _, r := range v { + f, err := DeserilazeProgram(ctx, r, hostconfig, k) + if err != nil { + log.Err(err).Msg("Deserilization failed for tcegress") + return nil, err + } + l.PushBack(f) } - l.PushBack(f) + D.EgressTCBpfs[k] = l } - D.EgressTCBpfs[k] = l } return D, nil } diff --git a/stats/metrics.go b/stats/metrics.go index dd05a296..99ac560b 100644 --- a/stats/metrics.go +++ b/stats/metrics.go @@ -122,7 +122,7 @@ func SetupMetrics(hostname, daemonName, metricsAddr string) { // Adding web endpoint go func() { // Expose the registered metrics via HTTP. - if _, ok := models.AllNetListeners["stat_http"]; !ok { + if _, ok := models.AllNetListeners.Load("stat_http"); !ok { tcpAddr, err := net.ResolveTCPAddr("tcp", metricsAddr) if err != nil { fmt.Println("Error resolving TCP address:", err) @@ -132,10 +132,12 @@ func SetupMetrics(hostname, daemonName, metricsAddr string) { if err != nil { log.Fatal().Err(err).Msgf("Not able to create net Listen") } - models.AllNetListeners["stat_http"] = listener + models.AllNetListeners.Store("stat_http", listener) } http.Handle("/metrics", metricsHandler) - if err := http.Serve(models.AllNetListeners["stat_http"], nil); !errors.Is(err, http.ErrServerClosed) { + val, _ := models.AllNetListeners.Load("stat_http") + l, _ := val.(*net.TCPListener) + if err := http.Serve(l, nil); !errors.Is(err, http.ErrServerClosed) { log.Fatal().Err(err).Msgf("Failed to launch prometheus metrics endpoint") } }()