...
1 package link
2
3 import (
4 "fmt"
5
6 "github.com/cilium/ebpf"
7 )
8
9
10 type XDPAttachFlags uint32
11
12 const (
13
14
15 XDPGenericMode XDPAttachFlags = 1 << (iota + 1)
16
17 XDPDriverMode
18
19 XDPOffloadMode
20 )
21
22 type XDPOptions struct {
23
24 Program *ebpf.Program
25
26
27 Interface int
28
29
30
31
32
33 Flags XDPAttachFlags
34 }
35
36
37 func AttachXDP(opts XDPOptions) (Link, error) {
38 if t := opts.Program.Type(); t != ebpf.XDP {
39 return nil, fmt.Errorf("invalid program type %s, expected XDP", t)
40 }
41
42 if opts.Interface < 1 {
43 return nil, fmt.Errorf("invalid interface index: %d", opts.Interface)
44 }
45
46 rawLink, err := AttachRawLink(RawLinkOptions{
47 Program: opts.Program,
48 Attach: ebpf.AttachXDP,
49 Target: opts.Interface,
50 Flags: uint32(opts.Flags),
51 })
52
53 return rawLink, err
54 }
55
View as plain text