forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifplugin.go
188 lines (161 loc) · 6.19 KB
/
ifplugin.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// 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.
//go:generate descriptor-adapter --descriptor-name Interface --value-type *linux_interfaces.Interface --meta-type *ifaceidx.LinuxIfMetadata --import "go.ligato.io/vpp-agent/v3/proto/ligato/linux/interfaces" --import "go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin/ifaceidx" --output-dir "descriptor"
//go:generate descriptor-adapter --descriptor-name InterfaceVrf --value-type *linux_interfaces.Interface --import "go.ligato.io/vpp-agent/v3/proto/ligato/linux/interfaces" --output-dir "descriptor"
//go:generate descriptor-adapter --descriptor-name InterfaceAddress --value-type *linux_interfaces.Interface --import "go.ligato.io/vpp-agent/v3/proto/ligato/linux/interfaces" --output-dir "descriptor"
package ifplugin
import (
"github.com/pkg/errors"
"go.ligato.io/cn-infra/v2/infra"
"go.ligato.io/cn-infra/v2/servicelabel"
kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
"go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin/descriptor"
"go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin/ifaceidx"
"go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin/linuxcalls"
"go.ligato.io/vpp-agent/v3/plugins/linux/nsplugin"
"go.ligato.io/vpp-agent/v3/plugins/netalloc"
"go.ligato.io/vpp-agent/v3/proto/ligato/linux"
linux_interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/linux/interfaces"
)
const (
// by default, at most 10 go routines will split the configured namespaces
// to execute the Retrieve operation in parallel.
defaultGoRoutinesCnt = 10
)
// IfPlugin configures Linux VETH and TAP interfaces using Netlink API.
type IfPlugin struct {
Deps
// From configuration file
disabled bool
// system handlers
ifHandler linuxcalls.NetlinkAPI
// descriptors
ifDescriptor *descriptor.InterfaceDescriptor
ifWatcher *descriptor.InterfaceWatcher
ifAddrDescriptor *descriptor.InterfaceAddressDescriptor
ifVrfDescriptor *descriptor.InterfaceVrfDescriptor
// index map
ifIndex ifaceidx.LinuxIfMetadataIndex
PushNotification func(notification *linux.Notification)
}
// Deps lists dependencies of the interface plugin.
type Deps struct {
infra.PluginDeps
ServiceLabel servicelabel.ReaderAPI
KVScheduler kvs.KVScheduler
NsPlugin nsplugin.API
AddrAlloc netalloc.AddressAllocator
VppIfPlugin descriptor.VPPIfPluginAPI /* mandatory if TAP_TO_VPP interfaces are used */
}
// Config holds the ifplugin configuration.
type Config struct {
Disabled bool `json:"disabled"`
GoRoutinesCnt int `json:"go-routines-count"`
}
// Init registers interface-related descriptors and starts watching of the default
// network namespace for interface changes.
func (p *IfPlugin) Init() error {
// parse configuration file
config, err := p.retrieveConfig()
if err != nil {
return err
}
p.Log.Debugf("Linux interface plugin config: %+v", config)
if config.Disabled {
p.disabled = true
p.Log.Infof("Disabling Linux Interface plugin")
return nil
}
// init & register interface descriptor
var ifDescriptor *kvs.KVDescriptor
ifDescriptor, p.ifDescriptor = descriptor.NewInterfaceDescriptor(p.ServiceLabel, p.NsPlugin, p.VppIfPlugin,
p.AddrAlloc, p.Log)
err = p.Deps.KVScheduler.RegisterKVDescriptor(ifDescriptor)
if err != nil {
return err
}
// obtain read-only reference to index map
var withIndex bool
metadataMap := p.Deps.KVScheduler.GetMetadataMap(ifDescriptor.Name)
p.ifIndex, withIndex = metadataMap.(ifaceidx.LinuxIfMetadataIndex)
if !withIndex {
return errors.New("missing index with interface metadata")
}
// init handler and pass it to the interface descriptor
p.ifHandler = linuxcalls.NewNetLinkHandler(p.NsPlugin, p.ifIndex, p.ServiceLabel.GetAgentPrefix(),
config.GoRoutinesCnt, p.Log)
p.ifDescriptor.SetInterfaceHandler(p.ifHandler)
var addrDescriptor, vrfDescriptor *kvs.KVDescriptor
addrDescriptor, p.ifAddrDescriptor = descriptor.NewInterfaceAddressDescriptor(p.NsPlugin,
p.AddrAlloc, p.ifHandler, p.Log)
vrfDescriptor, p.ifVrfDescriptor = descriptor.NewInterfaceVrfDescriptor(p.NsPlugin, p.ifHandler, p.Log)
err = p.Deps.KVScheduler.RegisterKVDescriptor(addrDescriptor, vrfDescriptor)
if err != nil {
return err
}
notifyInterface := func(notif *linux_interfaces.InterfaceNotification) {
if p.PushNotification != nil {
p.PushNotification(&linux.Notification{
Interface: notif,
})
}
}
p.ifWatcher = descriptor.NewInterfaceWatcher(p.KVScheduler, p.ifHandler, notifyInterface, p.Log)
err = p.Deps.KVScheduler.RegisterKVDescriptor(p.ifWatcher.GetDescriptor())
if err != nil {
return err
}
// pass read-only index map to descriptors
p.ifDescriptor.SetInterfaceIndex(p.ifIndex)
p.ifAddrDescriptor.SetInterfaceIndex(p.ifIndex)
p.ifVrfDescriptor.SetInterfaceIndex(p.ifIndex)
// start interface watching
if err = p.ifWatcher.StartWatching(); err != nil {
return err
}
return nil
}
// Close stops watching of the default network namespace.
func (p *IfPlugin) Close() error {
if p.disabled {
return nil
}
p.ifWatcher.StopWatching()
return nil
}
// GetInterfaceIndex gives read-only access to map with metadata of all configured
// linux interfaces.
func (p *IfPlugin) GetInterfaceIndex() ifaceidx.LinuxIfMetadataIndex {
return p.ifIndex
}
func (p *IfPlugin) SetNotifyService(notify func(notification *linux.Notification)) {
p.PushNotification = notify
}
// retrieveConfig loads IfPlugin configuration file.
func (p *IfPlugin) retrieveConfig() (*Config, error) {
config := &Config{
// default configuration
GoRoutinesCnt: defaultGoRoutinesCnt,
}
found, err := p.Cfg.LoadValue(config)
if !found {
p.Log.Debug("Linux IfPlugin config not found")
return config, nil
}
if err != nil {
return nil, err
}
p.Log.Debug("Linux IfPlugin config found")
return config, err
}