Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement in error handling #350

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 18 additions & 18 deletions kf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,18 @@ func LoadRootProgram(ifaceName string, direction string, progType string, conf *
if fileExists(rootProgBPF.MapNamePath) {
log.Warn().Msgf("previous instance of root program %s persisted map %s file exists", rootProgBPF.Program.Name, rootProgBPF.MapNamePath)
if err := rootProgBPF.RemoveRootProgMapFile(ifaceName); err != nil {
log.Info().Msgf("previous instance of root program %s map file removed successfully - %s ", rootProgBPF.Program.Name, rootProgBPF.MapNamePath)
log.Warn().Msgf("previous instance of root program %s map file removed unsuccessfully - %s ", rootProgBPF.Program.Name, rootProgBPF.MapNamePath)
Atul-source marked this conversation as resolved.
Show resolved Hide resolved
}
}

if progType == models.XDPType {
rlimit.RemoveMemlock()
if err := rootProgBPF.LoadXDPAttachProgram(ifaceName); err != nil {
return nil, fmt.Errorf("failed to load xdp root program on iface \"%s\" name %s direction %s", ifaceName, rootProgBPF.Program.Name, direction)
return nil, fmt.Errorf("failed to load xdp root program on iface \"%s\" name %s direction %s with err %v", ifaceName, rootProgBPF.Program.Name, direction, err)
Atul-source marked this conversation as resolved.
Show resolved Hide resolved
}
} else if progType == models.TCType {
if err := rootProgBPF.LoadTCAttachProgram(ifaceName, direction); err != nil {
return nil, fmt.Errorf("failed to load tc root program on iface \"%s\" name %s direction %s", ifaceName, rootProgBPF.Program.Name, direction)
return nil, fmt.Errorf("failed to load tc root program on iface \"%s\" name %s direction %s with err %v", ifaceName, rootProgBPF.Program.Name, direction, err)
}
}

Expand All @@ -214,7 +214,7 @@ func StopExternalRunningProcess(processName string) error {
myPid := os.Getpid()
processList, err := ps.Processes()
if err != nil {
return fmt.Errorf("failed to fetch processes list")
return fmt.Errorf("failed to fetch processes list with err %v", err)
}
log.Info().Msgf("Searching for process %s and not ppid %d", processName, myPid)
for _, process := range processList {
Expand Down Expand Up @@ -494,8 +494,8 @@ func (b *BPF) UpdateArgs(ifaceName, direction string) error {
UpdateCmd := execCommand(cmd, args...)
if err := UpdateCmd.Start(); err != nil {
stats.Incr(stats.BPFUpdateFailedCount, b.Program.Name, direction, ifaceName)
log.Info().Err(err).Msgf("user mode BPF program failed - %s", b.Program.Name)
return fmt.Errorf("failed to start : %s %v", cmd, args)
log.Warn().Err(err).Msgf("user mode BPF program failed - %s", b.Program.Name)
return fmt.Errorf("failed to start : %s %v %v", cmd, args, err)
Atul-source marked this conversation as resolved.
Show resolved Hide resolved
}

if err := UpdateCmd.Wait(); err != nil {
Expand Down Expand Up @@ -818,7 +818,7 @@ func (b *BPF) AddMetricsBPFMap(mapName, aggregator string, key, samplesLength in
var tmpMetricsBPFMap MetricsBPFMap
bpfMap, err := b.GetBPFMap(mapName)
if err != nil {
return fmt.Errorf("program %s metrics map %s not found", b.Program.Name, mapName)
return fmt.Errorf("program %s metrics map %s not found : %v", b.Program.Name, mapName, err)
}

tmpMetricsBPFMap.BPFMap = *bpfMap
Expand All @@ -841,7 +841,7 @@ func (b *BPF) MonitorMaps(ifaceName string, intervals int) error {
_, ok := b.MetricsBpfMaps[mapKey]
if !ok {
if err := b.AddMetricsBPFMap(element.Name, element.Aggregator, element.Key, intervals); err != nil {
return fmt.Errorf("not able to fetch map %s key %d aggregator %s", element.Name, element.Key, element.Aggregator)
return fmt.Errorf("not able to fetch map %s key %d aggregator %s : %v", element.Name, element.Key, element.Aggregator, err)
}
}
bpfMap := b.MetricsBpfMaps[mapKey]
Expand Down Expand Up @@ -922,7 +922,7 @@ func (b *BPF) RemoveNextProgFD() error {
key := 0

if err := ebpfMap.Delete(unsafe.Pointer(&key)); err != nil {
return fmt.Errorf("failed to delete prog fd entry")
return fmt.Errorf("failed to delete prog fd entry : %v", err)
}
return nil
}
Expand Down Expand Up @@ -1089,7 +1089,7 @@ func (b *BPF) UnloadProgram(ifaceName, direction string) error {

// remove pinned map file
if err := b.RemoveMapFiles(ifaceName); err != nil {
log.Error().Msgf("failed to remove map file for program %s => %s", ifaceName, b.Program.Name)
log.Error().Err(err).Msgf("failed to remove map file for program %s => %s", ifaceName, b.Program.Name)
}

return nil
Expand Down Expand Up @@ -1145,13 +1145,13 @@ func (b *BPF) VerifyCleanupMaps(chain bool) error {
// verify pinned map file is removed.
if err := b.VerifyPinnedProgMap(chain, false); err != nil {
log.Error().Err(err).Msgf("stop user program - failed to remove pinned file %s", b.Program.Name)
return fmt.Errorf("stop user program - failed to remove pinned file %s", b.Program.Name)
return fmt.Errorf("stop user program - failed to remove pinned file %s : %v", b.Program.Name, err)
}

// Verify all metrics map references are removed from kernel
if err := b.VerifyMetricsMapsVanish(); err != nil {
log.Error().Err(err).Msgf("stop user program - failed to remove metric map references %s", b.Program.Name)
return fmt.Errorf("stop user program - failed to remove metric map references %s", b.Program.Name)
return fmt.Errorf("stop user program - failed to remove metric map references %s : %v", b.Program.Name, err)
}

return nil
Expand All @@ -1178,7 +1178,7 @@ func (b *BPF) LoadBPFProgram(ifaceName string) error {
// Allow the current process to lock memory for eBPF resources.
if err := rlimit.RemoveMemlock(); err != nil {
log.Error().Msgf("failed to remove memory lock limits %#v", err)
return fmt.Errorf("%s: remove rlimit lock failed", b.Program.Name)
return fmt.Errorf("%s: remove rlimit lock failed : %#v", b.Program.Name, err)
}

objSpec, err := ebpf.LoadCollectionSpec(ObjectFile)
Expand Down Expand Up @@ -1218,7 +1218,7 @@ func (b *BPF) LoadBPFProgram(ifaceName string) error {

progInfo, err := bpfProg.Info()
if err != nil {
return fmt.Errorf("%s: information of bpf program failed", b.Program.Name)
return fmt.Errorf("%s: information of bpf program failed : %v", b.Program.Name, err)
}

ok := false
Expand Down Expand Up @@ -1392,7 +1392,7 @@ func (b *BPF) StartUserProgram(ifaceName, direction string, chain bool) error {
b.Cmd = execCommand(cmd, args...)
if err := b.Cmd.Start(); err != nil {
log.Info().Err(err).Msgf("user program failed - %s", b.Program.Name)
return fmt.Errorf("failed to start : %s %v", cmd, args)
return fmt.Errorf("failed to start : %s %v with err: %v", cmd, args, err)
}
if !b.Program.UserProgramDaemon {
log.Info().Msgf("no user program - %s No Pid", b.Program.Name)
Expand Down Expand Up @@ -1425,7 +1425,7 @@ func (b *BPF) CreateMapPinDirectory(ifaceName string) error {
return fmt.Errorf("%s contains relative path is not supported - %s", mapPathDir, b.Program.Name)
}
if err := os.MkdirAll(mapPathDir, 0750); err != nil {
return fmt.Errorf("%s failed to create map dir path of %s program %s", mapPathDir, b.Program.ProgType, b.Program.Name)
return fmt.Errorf("%s failed to create map dir path of %s program %s with err : %v", mapPathDir, b.Program.ProgType, b.Program.Name, err)
}
return nil
}
Expand All @@ -1434,11 +1434,11 @@ func (b *BPF) CreateMapPinDirectory(ifaceName string) error {
func (b *BPF) AttachBPFProgram(ifaceName, direction string) error {
if b.Program.ProgType == models.XDPType {
if err := b.LoadXDPAttachProgram(ifaceName); err != nil {
return fmt.Errorf("failed to attach xdp program %s to inferface %s", b.Program.Name, ifaceName)
return fmt.Errorf("failed to attach xdp program %s to inferface %s with err: %v", b.Program.Name, ifaceName, err)
}
} else if b.Program.ProgType == models.TCType {
if err := b.LoadTCAttachProgram(ifaceName, direction); err != nil {
return fmt.Errorf("failed to attach tc program %s to inferface %s direction %s", b.Program.Name, ifaceName, direction)
return fmt.Errorf("failed to attach tc program %s to inferface %s direction %s with err: %v", b.Program.Name, ifaceName, direction, err)
}
}
return nil
Expand Down
44 changes: 22 additions & 22 deletions kf/nfconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *NFConfigs) VerifyAndStartXDPRootProgram(ifaceName, direction string) er
return fmt.Errorf("failed to disable lro %v", err)
}
if err := VerifyNMountBPFFS(); err != nil {
return fmt.Errorf("failed to mount bpf file system")
return fmt.Errorf("failed to mount bpf file system with err %v", err)
}

// chaining is disabled nothing to do
Expand All @@ -161,10 +161,10 @@ func (c *NFConfigs) VerifyAndStartXDPRootProgram(ifaceName, direction string) er
func (c *NFConfigs) VerifyAndStartTCRootProgram(ifaceName, direction string) error {

if err := VerifyNMountBPFFS(); err != nil {
return fmt.Errorf("failed to mount bpf file system")
return fmt.Errorf("failed to mount bpf file system with err : %v", err)
}
if err := VerifyNCreateTCDirs(); err != nil {
return fmt.Errorf("failed to create tc/global diretories")
return fmt.Errorf("failed to create tc/global diretories with err: %v", err)
}
// Check for chaining flag
if !c.HostConfig.BpfChainingEnabled {
Expand Down Expand Up @@ -213,7 +213,7 @@ func (c *NFConfigs) PushBackAndStartBPF(bpfProg *models.BPFProgram, ifaceName, d
}

if err := c.DownloadAndStartBPFProgram(bpfList.PushBack(bpf), ifaceName, direction); err != nil {
return fmt.Errorf("failed to download and start the BPF %s iface %s direction %s", bpfProg.Name, ifaceName, direction)
return fmt.Errorf("failed to download and start the BPF %s iface %s direction %s with err: %v", bpfProg.Name, ifaceName, direction, err)
}

return nil
Expand Down Expand Up @@ -271,7 +271,7 @@ func (c *NFConfigs) StopNRemoveAllBPFPrograms(ifaceName, direction string) error
for e := bpfList.Front(); e != nil; {
data := e.Value.(*BPF)
if err := data.Stop(ifaceName, direction, c.HostConfig.BpfChainingEnabled); err != nil {
return fmt.Errorf("failed to stop program %s direction %s", data.Program.Name, direction)
return fmt.Errorf("failed to stop program %s direction %s with err :%v", data.Program.Name, direction, err)
}
nextBPF := e.Next()
bpfList.Remove(e)
Expand Down Expand Up @@ -323,7 +323,7 @@ func (c *NFConfigs) VerifyNUpdateBPFProgram(bpfProg *models.BPFProgram, ifaceNam
log.Info().Msgf("verifyNUpdateBPFProgram :admin_status change detected - disabling the program %s", data.Program.Name)
data.Program.AdminStatus = bpfProg.AdminStatus
if err := data.Stop(ifaceName, direction, c.HostConfig.BpfChainingEnabled); err != nil {
return fmt.Errorf("failed to stop to on admin_status change BPF %s iface %s direction %s admin_status %s", bpfProg.Name, ifaceName, direction, bpfProg.AdminStatus)
return fmt.Errorf("failed to stop to on admin_status change BPF %s iface %s direction %s admin_status %s with err %v", bpfProg.Name, ifaceName, direction, bpfProg.AdminStatus, err)
}
tmpNextBPF := e.Next()
tmpPreviousBPF := e.Prev()
Expand Down Expand Up @@ -355,7 +355,7 @@ func (c *NFConfigs) VerifyNUpdateBPFProgram(bpfProg *models.BPFProgram, ifaceNam
log.Info().Msg("no eBPF Programs are running, stopping root program")
if c.HostConfig.BpfChainingEnabled {
if err := c.StopRootProgram(ifaceName, direction); err != nil {
return fmt.Errorf("failed to stop to root program %s iface %s direction %s", bpfProg.Name, ifaceName, direction)
return fmt.Errorf("failed to stop to root program %s iface %s direction %s with err: %v", bpfProg.Name, ifaceName, direction, err)
}
}
}
Expand All @@ -367,13 +367,13 @@ func (c *NFConfigs) VerifyNUpdateBPFProgram(bpfProg *models.BPFProgram, ifaceNam
log.Info().Msgf("VerifyNUpdateBPFProgram : version update initiated - current version %s new version %s", data.Program.Version, bpfProg.Version)

if err := data.Stop(ifaceName, direction, c.HostConfig.BpfChainingEnabled); err != nil {
return fmt.Errorf("failed to stop older version of network function BPF %s iface %s direction %s version %s", bpfProg.Name, ifaceName, direction, bpfProg.Version)
return fmt.Errorf("failed to stop older version of network function BPF %s iface %s direction %s version %s with err: %v", bpfProg.Name, ifaceName, direction, bpfProg.Version, err)
}

data.Program = *bpfProg

if err := c.DownloadAndStartBPFProgram(e, ifaceName, direction); err != nil {
return fmt.Errorf("failed to download and start newer version of network function BPF %s version %s iface %s direction %s", bpfProg.Name, bpfProg.Version, ifaceName, direction)
return fmt.Errorf("failed to download and start newer version of network function BPF %s version %s iface %s direction %s with err: %v", bpfProg.Name, bpfProg.Version, ifaceName, direction, err)
}

// update if not a last program
Expand Down Expand Up @@ -401,7 +401,7 @@ func (c *NFConfigs) VerifyNUpdateBPFProgram(bpfProg *models.BPFProgram, ifaceNam
data.Program.SeqID = bpfProg.SeqID

if err := c.MoveToLocation(e, bpfList); err != nil {
return fmt.Errorf("failed to move to new position in the chain BPF %s version %s iface %s direction %s", bpfProg.Name, bpfProg.Version, ifaceName, direction)
return fmt.Errorf("failed to move to new position in the chain BPF %s version %s iface %s direction %s with err: %v", bpfProg.Name, bpfProg.Version, ifaceName, direction, err)
}
}

Expand All @@ -425,7 +425,7 @@ func (c *NFConfigs) VerifyNUpdateBPFProgram(bpfProg *models.BPFProgram, ifaceNam
log.Debug().Msgf("Program is not found in the list name %s", bpfProg.Name)
// if not found in the list.
if err := c.InsertAndStartBPFProgram(bpfProg, ifaceName, direction); err != nil {
return fmt.Errorf("failed to insert and start BPFProgram to new location BPF %s version %s iface %s direction %s", bpfProg.Name, bpfProg.Version, ifaceName, direction)
return fmt.Errorf("failed to insert and start BPFProgram to new location BPF %s version %s iface %s direction %s with err: %v", bpfProg.Name, bpfProg.Version, ifaceName, direction, err)
}

return nil
Expand Down Expand Up @@ -539,7 +539,7 @@ func (c *NFConfigs) InsertAndStartBPFProgram(bpfProg *models.BPFProgram, ifaceNa
if data.Program.SeqID >= bpfProg.SeqID {
tmpBPF := bpfList.InsertBefore(bpf, e)
if err := c.DownloadAndStartBPFProgram(tmpBPF, ifaceName, direction); err != nil {
return fmt.Errorf("failed to download and start network function %s version %s iface %s direction %s", bpfProg.Name, bpfProg.Version, ifaceName, direction)
return fmt.Errorf("failed to download and start network function %s version %s iface %s direction %s with err %v", bpfProg.Name, bpfProg.Version, ifaceName, direction, err)
}

if tmpBPF.Next() != nil {
Expand All @@ -554,7 +554,7 @@ func (c *NFConfigs) InsertAndStartBPFProgram(bpfProg *models.BPFProgram, ifaceNa

// insert at the end
if err := c.PushBackAndStartBPF(bpfProg, ifaceName, direction); err != nil {
return fmt.Errorf("failed to push back and start network function %s version %s iface %s direction %s", bpfProg.Name, bpfProg.Version, ifaceName, direction)
return fmt.Errorf("failed to push back and start network function %s version %s iface %s direction %s with err: %v", bpfProg.Name, bpfProg.Version, ifaceName, direction, err)
}

return nil
Expand All @@ -571,7 +571,7 @@ func (c *NFConfigs) StopRootProgram(ifaceName, direction string) error {
}

if err := c.IngressXDPBpfs[ifaceName].Front().Value.(*BPF).Stop(ifaceName, direction, c.HostConfig.BpfChainingEnabled); err != nil {
return fmt.Errorf("failed to stop xdp root program iface %s", ifaceName)
return fmt.Errorf("failed to stop xdp root program iface %s with err %v", ifaceName, err)
}
c.IngressXDPBpfs[ifaceName].Remove(c.IngressXDPBpfs[ifaceName].Front())
c.IngressXDPBpfs[ifaceName] = nil
Expand All @@ -581,7 +581,7 @@ func (c *NFConfigs) StopRootProgram(ifaceName, direction string) error {
return nil
}
if err := c.IngressTCBpfs[ifaceName].Front().Value.(*BPF).Stop(ifaceName, direction, c.HostConfig.BpfChainingEnabled); err != nil {
return fmt.Errorf("failed to stop ingress tc root program on interface %s", ifaceName)
return fmt.Errorf("failed to stop ingress tc root program on interface %s with err %v", ifaceName, err)
}
c.IngressTCBpfs[ifaceName].Remove(c.IngressTCBpfs[ifaceName].Front())
c.IngressTCBpfs[ifaceName] = nil
Expand All @@ -591,7 +591,7 @@ func (c *NFConfigs) StopRootProgram(ifaceName, direction string) error {
return nil
}
if err := c.EgressTCBpfs[ifaceName].Front().Value.(*BPF).Stop(ifaceName, direction, c.HostConfig.BpfChainingEnabled); err != nil {
return fmt.Errorf("failed to stop egress tc root program on interface %s", ifaceName)
return fmt.Errorf("failed to stop egress tc root program on interface %s with err %v", ifaceName, err)
}
c.EgressTCBpfs[ifaceName].Remove(c.EgressTCBpfs[ifaceName].Front())
c.EgressTCBpfs[ifaceName] = nil
Expand Down Expand Up @@ -918,7 +918,7 @@ func (c *NFConfigs) RemoveMissingBPFProgramsInConfig(bpfProg models.L3afBPFProgr
log.Info().Msgf("eBPF Program not found in config stopping - %s direction %s", prog.Program.Name, direction)
prog.Program.AdminStatus = models.Disabled
if err := prog.Stop(ifaceName, direction, c.HostConfig.BpfChainingEnabled); err != nil {
return fmt.Errorf("failed to stop to on removed config BPF %s iface %s direction %s", prog.Program.Name, ifaceName, models.XDPIngressType)
return fmt.Errorf("failed to stop to on removed config BPF %s iface %s direction %s with err %v", prog.Program.Name, ifaceName, models.XDPIngressType, err)
}
tmpNextBPF := e.Next()
tmpPreviousBPF := e.Prev()
Expand All @@ -934,7 +934,7 @@ func (c *NFConfigs) RemoveMissingBPFProgramsInConfig(bpfProg models.L3afBPFProgr
log.Info().Msgf("no eBPF Programs are running, stopping root program")

if err := c.StopRootProgram(ifaceName, direction); err != nil {
return fmt.Errorf("failed to stop to root program of iface %s direction XDP Ingress", ifaceName)
return fmt.Errorf("failed to stop to root program of iface %s direction XDP Ingress with err %v", ifaceName, err)
}
}
}
Expand Down Expand Up @@ -998,7 +998,7 @@ func (c *NFConfigs) AddAndStartBPF(bpfProg *models.BPFProgram, ifaceName string,
bpf := NewBpfProgram(c.ctx, *bpfProg, c.HostConfig, ifaceName)
tmpBPF := bpfList.InsertBefore(bpf, e)
if err := c.DownloadAndStartBPFProgram(tmpBPF, ifaceName, direction); err != nil {
return fmt.Errorf("failed to download and start eBPF program %s version %s iface %s direction %s", bpfProg.Name, bpfProg.Version, ifaceName, direction)
return fmt.Errorf("failed to download and start eBPF program %s version %s iface %s direction %s with err %v", bpfProg.Name, bpfProg.Version, ifaceName, direction, err)
}

if tmpBPF.Next() != nil {
Expand All @@ -1013,7 +1013,7 @@ func (c *NFConfigs) AddAndStartBPF(bpfProg *models.BPFProgram, ifaceName string,

// insert at the end
if err := c.PushBackAndStartBPF(bpfProg, ifaceName, direction); err != nil {
return fmt.Errorf("failed to push back and start eBPF Program %s version %s iface %s direction %s", bpfProg.Name, bpfProg.Version, ifaceName, direction)
return fmt.Errorf("failed to push back and start eBPF Program %s version %s iface %s direction %s with err %v", bpfProg.Name, bpfProg.Version, ifaceName, direction, err)
}

return nil
Expand Down Expand Up @@ -1271,7 +1271,7 @@ func (c *NFConfigs) DeleteProgramsOnInterfaceHelper(e *list.Element, ifaceName s
prog := e.Value.(*BPF)
prog.Program.AdminStatus = models.Disabled
if err := prog.Stop(ifaceName, direction, c.HostConfig.BpfChainingEnabled); err != nil {
return fmt.Errorf("failed to stop %s iface %s direction %s", prog.Program.Name, ifaceName, direction)
return fmt.Errorf("failed to stop %s iface %s direction %s with err %v", prog.Program.Name, ifaceName, direction, err)
}
tmpNextBPF := e.Next()
tmpPreviousBPF := e.Prev()
Expand All @@ -1291,7 +1291,7 @@ func (c *NFConfigs) DeleteProgramsOnInterfaceHelper(e *list.Element, ifaceName s
log.Info().Msgf("no ebpf programs are running, stopping root program")

if err := c.StopRootProgram(ifaceName, direction); err != nil {
return fmt.Errorf("failed to stop to root program of iface %s direction %v", ifaceName, direction)
return fmt.Errorf("failed to stop to root program of iface %s direction %v with err %v", ifaceName, direction, err)
}
}
return nil
Expand Down