forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ns_handler.go
485 lines (424 loc) · 15.2 KB
/
ns_handler.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// 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.
package nsplugin
import (
"context"
"errors"
"fmt"
"net"
"os"
"runtime"
"sync"
"syscall"
"bytes"
"github.com/fsouza/go-dockerclient"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/vpp-agent/plugins/linux/ifplugin/linuxcalls"
intf "github.com/ligato/vpp-agent/plugins/linux/model/interfaces"
"github.com/ligato/vpp-agent/plugins/linux/model/l3"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
)
// NsHandler is a plugin to handle namespaces and microservices for other linux plugins (ifplugin, l3plugin ...).
// It does not follow the standard concept of CRUD, but provides a set of methods other plugins can use to manage
// namespaces
type NsHandler struct {
log logging.Logger
cfgLock sync.Mutex
// Default namespace
defaultNs netns.NsHandle
// docker client - used to convert microservice label into the PID and ID of the container
dockerClient *docker.Client
// Microservice label -> Microservice info
microServiceByLabel map[string]*Microservice //todo
// Microservice container ID -> Microservice info
microServiceByID map[string]*Microservice //todo
// channel to send microservice updates
microserviceChan chan *MicroserviceCtx
ifMicroserviceNotif chan *MicroserviceEvent
// config namespace, serves as a temporary namespace for VETH type interfaces where they are created and then
// moved to proper namespace
configNs *intf.LinuxInterfaces_Interface_Namespace
// Handlers
ifHandler linuxcalls.NetlinkAPI
sysHandler SystemAPI
// Context within which all goroutines are running
ctx context.Context
// Cancel can be used to cancel all goroutines and their jobs inside of the plugin.
cancel context.CancelFunc
// Wait group allows to wait until all goroutines of the plugin have finished.
wg sync.WaitGroup
}
// Init namespace handler caches and create config namespace
func (plugin *NsHandler) Init(logger logging.PluginLogger, ifHandler linuxcalls.NetlinkAPI, sysHandler SystemAPI,
msChan chan *MicroserviceCtx, ifNotif chan *MicroserviceEvent) error {
// Logger
plugin.log = logger.NewLogger("-ns-handler")
plugin.log.Infof("Initializing namespace handler plugin")
// Init channels
plugin.microserviceChan = msChan
plugin.ifMicroserviceNotif = ifNotif
plugin.ctx, plugin.cancel = context.WithCancel(context.Background())
plugin.microServiceByLabel = make(map[string]*Microservice)
plugin.microServiceByID = make(map[string]*Microservice)
// Handlers
plugin.ifHandler = ifHandler
plugin.sysHandler = sysHandler
// Default namespace
var err error
plugin.defaultNs, err = netns.Get()
if err != nil {
return fmt.Errorf("failed to init default namespace: %v", err)
}
// Docker client
plugin.dockerClient, err = docker.NewClientFromEnv()
if err != nil {
plugin.log.WithFields(logging.Fields{
"DOCKER_HOST": os.Getenv("DOCKER_HOST"),
"DOCKER_TLS_VERIFY": os.Getenv("DOCKER_TLS_VERIFY"),
"DOCKER_CERT_PATH": os.Getenv("DOCKER_CERT_PATH"),
}).Errorf("Failed to get docker client instance from the environment variables: %v", err)
return err
}
plugin.log.Debugf("Using docker client endpoint: %+v", plugin.dockerClient.Endpoint())
// Create config namespace (for VETHs)
err = plugin.prepareConfigNamespace()
// Start microservice tracker
go plugin.trackMicroservices(plugin.ctx)
return err
}
// Close pre-configured namespace
func (plugin *NsHandler) Close() error {
var wasErr error
if plugin.configNs != nil {
// Remove veth pre-configure namespace
ns := plugin.IfNsToGeneric(plugin.configNs)
wasErr = ns.deleteNamedNetNs(plugin.sysHandler, plugin.log)
plugin.cancel()
plugin.wg.Wait()
}
return wasErr
}
// GetConfigNamespace return configuration namespace object
func (plugin *NsHandler) GetConfigNamespace() *intf.LinuxInterfaces_Interface_Namespace {
return plugin.configNs
}
// GetMicroserviceByLabel returns internal microservice-by-label mapping
func (plugin *NsHandler) GetMicroserviceByLabel() map[string]*Microservice {
return plugin.microServiceByLabel
}
// GetMicroserviceByID returns internal microservice-by-id mapping
func (plugin *NsHandler) GetMicroserviceByID() map[string]*Microservice {
return plugin.microServiceByID
}
// SetInterfaceNamespace moves a given Linux interface into a specified namespace.
func (plugin *NsHandler) SetInterfaceNamespace(ctx *NamespaceMgmtCtx, ifName string, namespace *intf.LinuxInterfaces_Interface_Namespace) error {
// Convert microservice namespace
var err error
if namespace != nil && namespace.Type == intf.LinuxInterfaces_Interface_Namespace_MICROSERVICE_REF_NS {
// Convert namespace
ifNs := plugin.convertMicroserviceNsToPidNs(namespace.Microservice)
// Back to interface ns type
namespace, err = ifNs.GenericToIfaceNs()
if err != nil {
return err
}
if namespace == nil {
return &unavailableMicroserviceErr{}
}
}
ifaceNs := plugin.IfNsToGeneric(namespace)
// Get network namespace file descriptor
ns, err := plugin.getOrCreateNs(ifaceNs)
if err != nil {
return err
}
defer ns.Close()
// Get the link plugin.
link, err := plugin.ifHandler.GetLinkByName(ifName)
if err != nil {
return err
}
// When interface moves from one namespace to another, it loses all its IP addresses, admin status
// and MTU configuration -- we need to remember the interface configuration before the move
// and re-configure the interface in the new namespace.
netIntf, err := net.InterfaceByName(ifName)
if err != nil {
return err
}
addrs, err := netIntf.Addrs()
if err != nil {
return err
}
// Move the interface into the namespace.
err = netlink.LinkSetNsFd(link, int(ns))
if err != nil {
return err
}
plugin.log.WithFields(logging.Fields{"ifName": ifName, "dest-namespace": plugin.IfaceNsToString(namespace),
"dest-namespace-fd": int(ns)}).
Debug("Moved Linux interface across namespaces")
// re-configure interface in its new namespace
revertNs, err := plugin.SwitchNamespace(ifaceNs, ctx)
if err != nil {
return err
}
defer revertNs()
if netIntf.Flags&net.FlagUp == 1 {
// re-enable interface
err = plugin.ifHandler.SetInterfaceUp(ifName)
if nil != err {
return fmt.Errorf("failed to enable Linux interface `%s`: %v", ifName, err)
}
plugin.log.WithFields(logging.Fields{"ifName": ifName}).
Debug("Linux interface was re-enabled")
}
// Get all configured interface addresses
confAddresses, err := plugin.ifHandler.GetAddressList(ifName)
if err != nil {
return err
}
// re-add IP addresses
for i := range addrs {
ip, network, err := net.ParseCIDR(addrs[i].String())
network.IP = ip /* combine IP address with netmask */
if err != nil {
return fmt.Errorf("failed to parse IPv4 address of a Linux interface `%s`: %v", ifName, err)
}
// Check link local addresses which cannot be reassigned
if addressExists(confAddresses, network) {
plugin.log.Debugf("Cannot assign %s to interface %s, IP already exists",
network.IP.String(), ifName)
continue
}
err = plugin.ifHandler.AddInterfaceIP(ifName, network)
if err != nil {
if err.Error() == "file exists" {
continue
}
return fmt.Errorf("failed to assign IPv4 address to a Linux interface `%s`: %v", ifName, err)
}
plugin.log.WithFields(logging.Fields{"ifName": ifName, "addr": network}).
Debug("IP address was re-assigned to Linux interface")
}
// revert back the MTU config
err = plugin.ifHandler.SetInterfaceMTU(ifName, netIntf.MTU)
if nil != err {
return fmt.Errorf("failed to set MTU of a Linux interface `%s`: %v", ifName, err)
}
plugin.log.WithFields(logging.Fields{"ifName": ifName, "mtu": netIntf.MTU}).
Debug("MTU was reconfigured for Linux interface")
return nil
}
// switchToNamespace switches the network namespace of the current thread.
func (plugin *NsHandler) SwitchToNamespace(nsMgmtCtx *NamespaceMgmtCtx, ns *intf.LinuxInterfaces_Interface_Namespace) (revert func(), err error) {
if ns != nil && ns.Type == intf.LinuxInterfaces_Interface_Namespace_MICROSERVICE_REF_NS {
// Convert namespace
ifNs := plugin.convertMicroserviceNsToPidNs(ns.Microservice)
// Back to interface ns type
ns, err = ifNs.GenericToIfaceNs()
if err != nil {
return func() {}, err
}
if ns == nil {
return func() {}, &unavailableMicroserviceErr{}
}
}
// Prepare generic namespace object
ifaceNs := plugin.IfNsToGeneric(ns)
return plugin.SwitchNamespace(ifaceNs, nsMgmtCtx)
}
// SwitchNamespace switches the network namespace of the current thread.
// Caller should eventually call the returned "revert" function in order to get back to the original
// network namespace (for example using "defer revert()").
func (plugin *NsHandler) SwitchNamespace(ns *Namespace, ctx *NamespaceMgmtCtx) (revert func(), err error) {
var nsHandle netns.NsHandle
if ns != nil && ns.Type == MicroserviceRefNs {
ns = plugin.convertMicroserviceNsToPidNs(ns.Microservice)
if ns == nil {
return func() {}, &unavailableMicroserviceErr{}
}
}
// Save the current network namespace.
origns, err := netns.Get()
if err != nil {
return func() {}, err
}
// Get network namespace file descriptor.
nsHandle, err = plugin.getOrCreateNs(ns)
if err != nil {
return func() {}, err
}
defer nsHandle.Close()
alreadyLocked := ctx.lockedOsThread
if !alreadyLocked {
// Lock the OS Thread so we don't accidentally switch namespaces later.
runtime.LockOSThread()
ctx.lockedOsThread = true
plugin.log.Debug("Locked OS thread")
}
// Switch the namespace.
l := plugin.log.WithFields(logging.Fields{"ns": nsHandle.String(), "ns-fd": int(nsHandle)})
if err := plugin.sysHandler.SetNamespace(nsHandle); err != nil {
l.Errorf("Failed to switch Linux network namespace (%v): %v", ns.GenericNsToString(), err)
} else {
l.Debugf("Switched Linux network namespace (%v)", ns.GenericNsToString())
}
return func() {
l := plugin.log.WithFields(logging.Fields{"orig-ns": origns.String(), "orig-ns-fd": int(origns)})
if err := netns.Set(origns); err != nil {
l.Errorf("Failed to switch Linux network namespace: %v", err)
} else {
l.Debugf("Switched back to the original Linux network namespace")
}
origns.Close()
if !alreadyLocked {
runtime.UnlockOSThread()
ctx.lockedOsThread = false
plugin.log.Debug("Unlocked OS thread")
}
}, nil
}
// IsNamespaceAvailable returns true if the destination namespace is available.
func (plugin *NsHandler) IsNamespaceAvailable(ns *intf.LinuxInterfaces_Interface_Namespace) bool {
if ns != nil && ns.Type == intf.LinuxInterfaces_Interface_Namespace_MICROSERVICE_REF_NS {
if plugin.dockerClient == nil {
return false
}
_, available := plugin.microServiceByLabel[ns.Microservice]
return available
}
return true
}
// IfNsToGeneric converts interface-type namespace to generic type namespace. Such an object can be used to call common
// namespace-related methods
func (plugin *NsHandler) IfNsToGeneric(ns *intf.LinuxInterfaces_Interface_Namespace) *Namespace {
if ns == nil {
return &Namespace{}
}
return &Namespace{Type: int32(ns.Type), Pid: ns.Pid, Microservice: ns.Microservice, Name: ns.Name, FilePath: ns.Filepath}
}
// ArpNsToGeneric converts arp-type namespace to generic type namespace. Such an object can be used to call common
// namespace-related methods
func (plugin *NsHandler) ArpNsToGeneric(ns *l3.LinuxStaticArpEntries_ArpEntry_Namespace) *Namespace {
if ns == nil {
return &Namespace{}
}
return &Namespace{Type: int32(ns.Type), Pid: ns.Pid, Microservice: ns.Microservice, Name: ns.Name, FilePath: ns.Filepath}
}
// RouteNsToGeneric converts route-type namespace to generic type namespace. Such an object can be used to call common
// namespace-related methods
func (plugin *NsHandler) RouteNsToGeneric(ns *l3.LinuxStaticRoutes_Route_Namespace) *Namespace {
if ns == nil {
return &Namespace{}
}
return &Namespace{Type: int32(ns.Type), Pid: ns.Pid, Microservice: ns.Microservice, Name: ns.Name, FilePath: ns.Filepath}
}
// getOrCreateNs returns an existing Linux network namespace or creates a new one if it doesn't exist yet.
// It is, however, only possible to create "named" namespaces. For PID-based namespaces, process with
// the given PID must exists, otherwise the function returns an error.
func (plugin *NsHandler) getOrCreateNs(ns *Namespace) (netns.NsHandle, error) {
var nsHandle netns.NsHandle
var err error
if ns == nil {
return dupNsHandle(plugin.defaultNs)
}
switch ns.Type {
case PidRefNs:
if ns.Pid == 0 {
// We consider scheduler's PID as the representation of the default namespace.
return dupNsHandle(plugin.defaultNs)
}
nsHandle, err = netns.GetFromPid(int(ns.Pid))
if err != nil {
return netns.None(), err
}
case NamedNs:
if ns.Name == "" {
return dupNsHandle(plugin.defaultNs)
}
nsHandle, err = plugin.sysHandler.GetNamespaceFromName(ns.Name)
if err != nil {
// Create named namespace if it doesn't exist yet.
_, err = ns.createNamedNetNs(plugin.sysHandler, plugin.log)
if err != nil {
return netns.None(), err
}
nsHandle, err = netns.GetFromName(ns.Name)
if err != nil {
return netns.None(), errors.New("unable to get namespace by name")
}
}
case FileRefNs:
if ns.FilePath == "" {
return dupNsHandle(plugin.defaultNs)
}
nsHandle, err = netns.GetFromPath(ns.FilePath)
if err != nil {
return netns.None(), err
}
case MicroserviceRefNs:
return netns.None(), errors.New("unable to convert microservice label to PID at this level")
}
return nsHandle, nil
}
// Create named namespace used for VETH interface creation instead of the default one.
func (plugin *NsHandler) prepareConfigNamespace() error {
// Prepare namespace proto object.
ns := &Namespace{
Type: NamedNs,
Name: configNamespace,
}
// Check if namespace exists.
found, err := ns.namedNetNsExists(plugin.log)
if err != nil {
return err
}
// Remove namespace if exists.
if found {
err := ns.deleteNamedNetNs(plugin.sysHandler, plugin.log)
if err != nil {
return err
}
}
_, err = ns.createNamedNetNs(plugin.sysHandler, plugin.log)
if err != nil {
return err
}
plugin.configNs, err = ns.GenericToIfaceNs()
return err
}
// convertMicroserviceNsToPidNs converts microservice-referenced namespace into the PID-referenced namespace.
func (plugin *NsHandler) convertMicroserviceNsToPidNs(microserviceLabel string) (pidNs *Namespace) {
if microservice, ok := plugin.microServiceByLabel[microserviceLabel]; ok {
pidNamespace := &Namespace{}
pidNamespace.Type = PidRefNs
pidNamespace.Pid = uint32(microservice.Pid)
return pidNamespace
}
return nil
}
func addressExists(configured []netlink.Addr, provided *net.IPNet) bool {
for _, confAddr := range configured {
if bytes.Equal(confAddr.IP, provided.IP) {
return true
}
}
return false
}
// dupNsHandle duplicates namespace handle.
func dupNsHandle(ns netns.NsHandle) (netns.NsHandle, error) {
dup, err := syscall.Dup(int(ns))
return netns.NsHandle(dup), err
}