Debian Codegraph @

link/xdp.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "fmt"
7
8 "github.com/cilium/ebpf"
9 "github.com/cilium/ebpf/internal/sys"
10 )
11
12 // XDPAttachFlags represents how XDP program will be attached to interface.
13 type XDPAttachFlags uint32
14
15 const (
16 // XDPGenericMode (SKB) links XDP BPF program for drivers which do
17 // not yet support native XDP.
18 XDPGenericMode XDPAttachFlags = 1 << (iota + 1)
19 // XDPDriverMode links XDP BPF program into the driver’s receive path.
20 XDPDriverMode
21 // XDPOffloadMode offloads the entire XDP BPF program into hardware.
22 XDPOffloadMode
23 )
24
25 type XDPOptions struct {
26 // Program must be an XDP BPF program.
27 Program *ebpf.Program
28
29 // Interface is the interface index to attach program to.
30 Interface int
31
32 // Flags is one of XDPAttachFlags (optional).
33 //
34 // Only one XDP mode should be set, without flag defaults
35 // to driver/generic mode (best effort).
36 Flags XDPAttachFlags
37 }
38
39 // AttachXDP links an XDP BPF program to an XDP hook.
40 func AttachXDP(opts XDPOptions) (Link, error) {
41 if t := opts.Program.Type(); t != ebpf.XDP {
42 return nil, fmt.Errorf("invalid program type %s, expected XDP", t)
43 }
44
45 if opts.Interface < 1 {
46 return nil, fmt.Errorf("invalid interface index: %d", opts.Interface)
47 }
48
49 rawLink, err := AttachRawLink(RawLinkOptions{
50 Program: opts.Program,
51 Attach: ebpf.AttachXDP,
52 Target: opts.Interface,
53 Flags: uint32(opts.Flags),
54 })
55
56 if err != nil {
57 return nil, fmt.Errorf("failed to attach link: %w", err)
58 }
59
60 return &xdpLink{*rawLink}, nil
61 }
62
63 type xdpLink struct {
64 RawLink
65 }
66
67 func (xdp *xdpLink) Info() (*Info, error) {
68 var info sys.XDPLinkInfo
69 if err := sys.ObjInfo(xdp.fd, &info); err != nil {
70 return nil, fmt.Errorf("xdp link info: %s", err)
71 }
72 extra := &XDPInfo{
73 Ifindex: info.Ifindex,
74 }
75
76 return &Info{
77 info.Type,
78 info.Id,
79 ebpf.ProgramID(info.ProgId),
80 extra,
81 }, nil
82 }