Skip to content

Commit

Permalink
addition fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Atul-source <atulprajapati6031@gmail.com>
  • Loading branch information
Atul-source committed Aug 30, 2024
1 parent e292fbf commit e5e71e5
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 214 deletions.
11 changes: 6 additions & 5 deletions apis/configwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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...)
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
}
Expand Down
20 changes: 14 additions & 6 deletions apis/handlers/restart_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -145,18 +145,26 @@ 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 {
log.Error().Msgf("%v", err)
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")
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions bpfprogs/bpfdebug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
}
}()
Expand Down
Loading

0 comments on commit e5e71e5

Please sign in to comment.