Skip to content

Commit

Permalink
feat: add retry mechanism for attaching eBPF program
Browse files Browse the repository at this point in the history
  • Loading branch information
boratanrikulu committed Jul 27, 2024
1 parent 7fa1511 commit c2a318e
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions internal/ebpf/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ebpf
import (
"fmt"
"net"
"time"

"github.com/cilium/ebpf/link"
)
Expand All @@ -14,18 +15,31 @@ var (
ErrNoAttach = fmt.Errorf("not attached to the interface")
)

// Attach attaches eBPF program to the kernel.
// Attach attaches the eBPF program to the kernel with retry logic.
func (e *EBPF) Attach(iface *net.Interface) error {
const maxRetries = 3
const retryDelay = 100 * time.Millisecond

if e.L != nil {
return fmt.Errorf(
"%w: %s", ErrAlreadyAttached, iface.Name,
)
}

l, err := link.AttachXDP(link.XDPOptions{
Program: e.Objects.XdpDurdurFunc,
Interface: iface.Index,
})
var l link.Link
var err error
for i := 0; i < maxRetries; i++ {
l, err = link.AttachXDP(link.XDPOptions{
Program: e.Objects.XdpDurdurFunc,
Interface: iface.Index,
})
if err == nil {
break
}
if i < maxRetries-1 {
time.Sleep(retryDelay)
}
}
if err != nil {
return fmt.Errorf("attach: %w", err)
}
Expand Down

0 comments on commit c2a318e

Please sign in to comment.