| 1 |
package link |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
|
| 8 |
"github.com/cilium/ebpf" |
| 9 |
"github.com/cilium/ebpf/btf" |
| 10 |
"github.com/cilium/ebpf/internal" |
| 11 |
"github.com/cilium/ebpf/internal/sys" |
| 12 |
"github.com/cilium/ebpf/internal/unix" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
type Type = sys.LinkType |
| 17 |
|
| 18 |
var ErrNotSupported = internal.ErrNotSupported |
| 19 |
|
| 20 |
|
| 21 |
type Link interface { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
Update(*ebpf.Program) error |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
Pin(string) error |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
Unpin() error |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
Close() error |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
Detach() error |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
Info() (*Info, error) |
| 53 |
|
| 54 |
|
| 55 |
isLink() |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
func NewFromFD(fd int) (Link, error) { |
| 62 |
sysFD, err := sys.NewFD(fd) |
| 63 |
if err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
|
| 67 |
return wrapRawLink(&RawLink{fd: sysFD}) |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
func NewFromID(id ID) (Link, error) { |
| 74 |
getFdAttr := &sys.LinkGetFdByIdAttr{Id: id} |
| 75 |
fd, err := sys.LinkGetFdById(getFdAttr) |
| 76 |
if err != nil { |
| 77 |
return nil, fmt.Errorf("get link fd from ID %d: %w", id, err) |
| 78 |
} |
| 79 |
|
| 80 |
return wrapRawLink(&RawLink{fd, ""}) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
func LoadPinnedLink(fileName string, opts *ebpf.LoadPinOptions) (Link, error) { |
| 87 |
raw, err := loadPinnedRawLink(fileName, opts) |
| 88 |
if err != nil { |
| 89 |
return nil, err |
| 90 |
} |
| 91 |
|
| 92 |
return wrapRawLink(raw) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
type ID = sys.LinkID |
| 97 |
|
| 98 |
|
| 99 |
type RawLinkOptions struct { |
| 100 |
|
| 101 |
Target int |
| 102 |
|
| 103 |
Program *ebpf.Program |
| 104 |
|
| 105 |
Attach ebpf.AttachType |
| 106 |
|
| 107 |
BTF btf.TypeID |
| 108 |
|
| 109 |
Flags uint32 |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
type Info struct { |
| 114 |
Type Type |
| 115 |
ID ID |
| 116 |
Program ebpf.ProgramID |
| 117 |
extra any |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
type RawLink struct { |
| 125 |
fd *sys.FD |
| 126 |
pinnedPath string |
| 127 |
} |
| 128 |
|
| 129 |
func loadPinnedRawLink(fileName string, opts *ebpf.LoadPinOptions) (*RawLink, error) { |
| 130 |
fd, typ, err := sys.ObjGetTyped(&sys.ObjGetAttr{ |
| 131 |
Pathname: sys.NewStringPointer(fileName), |
| 132 |
FileFlags: opts.Marshal(), |
| 133 |
}) |
| 134 |
if err != nil { |
| 135 |
return nil, fmt.Errorf("load pinned link: %w", err) |
| 136 |
} |
| 137 |
|
| 138 |
if typ != sys.BPF_TYPE_LINK { |
| 139 |
_ = fd.Close() |
| 140 |
return nil, fmt.Errorf("%s is not a Link", fileName) |
| 141 |
} |
| 142 |
|
| 143 |
return &RawLink{fd, fileName}, nil |
| 144 |
} |
| 145 |
|
| 146 |
func (l *RawLink) isLink() {} |
| 147 |
|
| 148 |
|
| 149 |
func (l *RawLink) FD() int { |
| 150 |
return l.fd.Int() |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
func (l *RawLink) Close() error { |
| 157 |
return l.fd.Close() |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
func (l *RawLink) Pin(fileName string) error { |
| 165 |
if err := sys.Pin(l.pinnedPath, fileName, l.fd); err != nil { |
| 166 |
return err |
| 167 |
} |
| 168 |
l.pinnedPath = fileName |
| 169 |
return nil |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
func (l *RawLink) Unpin() error { |
| 174 |
if err := sys.Unpin(l.pinnedPath); err != nil { |
| 175 |
return err |
| 176 |
} |
| 177 |
l.pinnedPath = "" |
| 178 |
return nil |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
func (l *RawLink) IsPinned() bool { |
| 183 |
return l.pinnedPath != "" |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
func (l *RawLink) Update(new *ebpf.Program) error { |
| 188 |
return l.UpdateArgs(RawLinkUpdateOptions{ |
| 189 |
New: new, |
| 190 |
}) |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
type RawLinkUpdateOptions struct { |
| 195 |
New *ebpf.Program |
| 196 |
Old *ebpf.Program |
| 197 |
Flags uint32 |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
func (l *RawLink) UpdateArgs(opts RawLinkUpdateOptions) error { |
| 202 |
newFd := opts.New.FD() |
| 203 |
if newFd < 0 { |
| 204 |
return fmt.Errorf("invalid program: %s", sys.ErrClosedFd) |
| 205 |
} |
| 206 |
|
| 207 |
var oldFd int |
| 208 |
if opts.Old != nil { |
| 209 |
oldFd = opts.Old.FD() |
| 210 |
if oldFd < 0 { |
| 211 |
return fmt.Errorf("invalid replacement program: %s", sys.ErrClosedFd) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
attr := sys.LinkUpdateAttr{ |
| 216 |
LinkFd: l.fd.Uint(), |
| 217 |
NewProgFd: uint32(newFd), |
| 218 |
OldProgFd: uint32(oldFd), |
| 219 |
Flags: opts.Flags, |
| 220 |
} |
| 221 |
if err := sys.LinkUpdate(&attr); err != nil { |
| 222 |
return fmt.Errorf("update link: %w", err) |
| 223 |
} |
| 224 |
return nil |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
func (l *RawLink) Detach() error { |
| 229 |
attr := sys.LinkDetachAttr{ |
| 230 |
LinkFd: l.fd.Uint(), |
| 231 |
} |
| 232 |
|
| 233 |
err := sys.LinkDetach(&attr) |
| 234 |
|
| 235 |
switch { |
| 236 |
case errors.Is(err, unix.EOPNOTSUPP): |
| 237 |
return internal.ErrNotSupported |
| 238 |
case err != nil: |
| 239 |
return fmt.Errorf("detach link: %w", err) |
| 240 |
default: |
| 241 |
return nil |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
func (l *RawLink) Info() (*Info, error) { |
| 250 |
var info sys.LinkInfo |
| 251 |
|
| 252 |
if err := sys.ObjInfo(l.fd, &info); err != nil { |
| 253 |
return nil, fmt.Errorf("link info: %s", err) |
| 254 |
} |
| 255 |
|
| 256 |
return &Info{ |
| 257 |
info.Type, |
| 258 |
info.Id, |
| 259 |
ebpf.ProgramID(info.ProgId), |
| 260 |
nil, |
| 261 |
}, nil |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
type Iterator struct { |
| 266 |
|
| 267 |
ID ID |
| 268 |
|
| 269 |
|
| 270 |
Link Link |
| 271 |
err error |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
func (it *Iterator) Next() bool { |
| 278 |
id := it.ID |
| 279 |
for { |
| 280 |
getIdAttr := &sys.LinkGetNextIdAttr{Id: id} |
| 281 |
err := sys.LinkGetNextId(getIdAttr) |
| 282 |
if errors.Is(err, os.ErrNotExist) { |
| 283 |
|
| 284 |
break |
| 285 |
} else if err != nil { |
| 286 |
it.err = fmt.Errorf("get next link ID: %w", err) |
| 287 |
break |
| 288 |
} |
| 289 |
|
| 290 |
id = getIdAttr.NextId |
| 291 |
l, err := NewFromID(id) |
| 292 |
if errors.Is(err, os.ErrNotExist) { |
| 293 |
|
| 294 |
continue |
| 295 |
} else if err != nil { |
| 296 |
it.err = fmt.Errorf("get link for ID %d: %w", id, err) |
| 297 |
break |
| 298 |
} |
| 299 |
|
| 300 |
if it.Link != nil { |
| 301 |
it.Link.Close() |
| 302 |
} |
| 303 |
it.ID, it.Link = id, l |
| 304 |
return true |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
if it.Link != nil { |
| 309 |
it.Link.Close() |
| 310 |
} |
| 311 |
it.Link = nil |
| 312 |
return false |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
func (it *Iterator) Take() Link { |
| 319 |
l := it.Link |
| 320 |
it.Link = nil |
| 321 |
return l |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
func (it *Iterator) Err() error { |
| 326 |
return it.err |
| 327 |
} |
| 328 |
|
| 329 |
func (it *Iterator) Close() { |
| 330 |
if it.Link != nil { |
| 331 |
it.Link.Close() |
| 332 |
} |
| 333 |
} |