forked from projectcalico/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetlink_real.go
93 lines (82 loc) · 3 KB
/
netlink_real.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2017,2020-2021 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ifacemonitor
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
"github.com/projectcalico/calico/felix/environment"
"github.com/projectcalico/calico/felix/netlinkshim/handlemgr"
)
type netlinkReal struct {
handleMgr *handlemgr.HandleManager
}
func newRealNetlink(featureDetector environment.FeatureDetectorIface, timeout time.Duration) *netlinkReal {
return &netlinkReal{
handleMgr: handlemgr.NewHandleManager(featureDetector, handlemgr.WithSocketTimeout(timeout)),
}
}
func (nl *netlinkReal) Subscribe(
linkUpdates chan netlink.LinkUpdate,
routeUpdates chan netlink.RouteUpdate,
) (chan struct{}, error) {
// Note: this method doesn't use the HandleManager because each subscription gets its own
// socket under the covers.
cancel := make(chan struct{})
if err := netlink.LinkSubscribeWithOptions(linkUpdates, cancel, netlink.LinkSubscribeOptions{
ErrorCallback: func(err error) {
// Not necessarily fatal (can be an unexpected message, which the library will drop).
log.WithError(err).Warn("Netlink reported an error.")
},
}); err != nil {
log.WithError(err).Error("Failed to subscribe to link updates")
close(cancel)
return nil, err
}
if err := netlink.RouteSubscribeWithOptions(routeUpdates, cancel, netlink.RouteSubscribeOptions{
ErrorCallback: func(err error) {
// Not necessarily fatal (can be an unexpected message, which the library will drop).
log.WithError(err).Warn("Netlink reported an error.")
},
}); err != nil {
log.WithError(err).Error("Failed to subscribe to route updates")
close(cancel)
return nil, err
}
return cancel, nil
}
func (nl *netlinkReal) LinkList() ([]netlink.Link, error) {
h, err := nl.handleMgr.Handle()
if err != nil {
return nil, fmt.Errorf("failed to get netlink handle: %w", err)
}
return h.LinkList()
}
func (nl *netlinkReal) ListLocalRoutes(link netlink.Link, family int) ([]netlink.Route, error) {
h, err := nl.handleMgr.Handle()
if err != nil {
return nil, fmt.Errorf("failed to get netlink handle: %w", err)
}
routeFilter := &netlink.Route{}
filterFlags := netlink.RT_FILTER_TABLE | netlink.RT_FILTER_TYPE
routeFilter.Table = unix.RT_TABLE_LOCAL
routeFilter.Type = unix.RTN_LOCAL
if link != nil {
filterFlags |= netlink.RT_FILTER_OIF
routeFilter.LinkIndex = link.Attrs().Index
}
return h.RouteListFiltered(family, routeFilter, filterFlags)
}