-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbridge_network.go
More file actions
66 lines (58 loc) · 1.6 KB
/
Copy pathbridge_network.go
File metadata and controls
66 lines (58 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"errors"
"fmt"
"github.com/vishvananda/netlink"
)
const managedTapName = "tap0"
// managedTap identifies the exact TAP created by this process. The identity
// check in cleanup prevents a later interface reusing the name or index from
// being removed accidentally.
type managedTap struct {
name string
index int
created bool
lookupLink func(string) (netlink.Link, error)
deleteLink func(netlink.Link) error
}
func (tap *managedTap) cleanup() error {
if tap == nil || !tap.created {
return nil
}
lookup := tap.lookupLink
if lookup == nil {
lookup = netlink.LinkByName
}
link, err := lookup(tap.name)
if err != nil {
var notFound netlink.LinkNotFoundError
if errors.As(err, ¬Found) {
tap.created = false
return nil
}
return fmt.Errorf("look up managed TAP %q for cleanup: %w", tap.name, err)
}
if link == nil || link.Attrs() == nil {
return fmt.Errorf("managed TAP %q lookup returned no link", tap.name)
}
if link.Type() != "tuntap" {
return fmt.Errorf("refusing to delete managed TAP %q: current link type is %q", tap.name, link.Type())
}
if link.Attrs().Index != tap.index {
return fmt.Errorf("refusing to delete managed TAP %q: link index changed from %d to %d", tap.name, tap.index, link.Attrs().Index)
}
deleteLink := tap.deleteLink
if deleteLink == nil {
deleteLink = netlink.LinkDel
}
if err := deleteLink(link); err != nil {
var notFound netlink.LinkNotFoundError
if errors.As(err, ¬Found) {
tap.created = false
return nil
}
return fmt.Errorf("delete managed TAP %q: %w", tap.name, err)
}
tap.created = false
return nil
}