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

libbpf: Added AttachXDP wrapper #170

Merged
merged 7 commits into from Jun 14, 2022
Merged
Changes from 2 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
20 changes: 20 additions & 0 deletions libbpfgo.go
Expand Up @@ -1263,6 +1263,26 @@ func (p *BPFProg) SetAttachType(attachType BPFAttachType) {
C.bpf_program__set_expected_attach_type(p.prog, C.enum_bpf_attach_type(int(attachType)))
}

func (p *BPFProg) AttachXDP(deviceName string) (*BPFLink, error) {
iface, err := net.InterfaceByName(deviceName)
if err != nil {
return nil, fmt.Errorf("failed to attach program to device %s: %w", deviceName, err)
guyarb marked this conversation as resolved.
Show resolved Hide resolved
}
link := C.bpf_program__attach_xdp(p.prog, C.int(iface.Index))
if C.IS_ERR_OR_NULL(unsafe.Pointer(link)) {
return nil, errptrError(unsafe.Pointer(link), "failed to attach xdp on device %s to program %s", deviceName, p.name)
}

bpfLink := &BPFLink{
link: link,
prog: p,
linkType: Tracepoint,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that this is the correct link type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great catch! Added new link type XDP

eventName: fmt.Sprintf("xdp-%s-%s", p.name, deviceName),
}
p.module.links = append(p.module.links, bpfLink)
return bpfLink, nil
}

func (p *BPFProg) AttachTracepoint(category, name string) (*BPFLink, error) {
tpCategory := C.CString(category)
tpName := C.CString(name)
Expand Down