/
netns_linux.go
112 lines (96 loc) · 2.73 KB
/
netns_linux.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
// Copyright Istio Authors
//
// 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 nodeagent
import (
"fmt"
"runtime"
"sync"
netns "github.com/containernetworking/plugins/pkg/ns"
"golang.org/x/sys/unix"
)
type NetnsWrapper struct {
innerNetns netns.NetNS
inode uint64
}
func (n *NetnsWrapper) Inode() uint64 {
return n.inode
}
func (n *NetnsWrapper) Close() error {
return n.innerNetns.Close()
}
func (n *NetnsWrapper) Fd() uintptr {
return n.innerNetns.Fd()
}
func inodeForFd(n NetnsFd) (uint64, error) {
stats := &unix.Stat_t{}
err := unix.Fstat(int(n.Fd()), stats)
return stats.Ino, err
}
func OpenNetns(nspath string) (NetnsCloser, error) {
n, err := netns.GetNS(nspath)
if err != nil {
return nil, err
}
i, err := inodeForFd(n)
if err != nil {
n.Close()
return nil, err
}
return &NetnsWrapper{innerNetns: n, inode: i}, nil
}
func NetnsSet(n NetnsFd) error {
if err := unix.Setns(int(n.Fd()), unix.CLONE_NEWNET); err != nil {
return fmt.Errorf("Error switching to ns fd %v: %v", n.Fd(), err)
}
return nil
}
// inspired by netns.Do() but with an existing fd.
func NetnsDo(fdable NetnsFd, toRun func() error) error {
containedCall := func() error {
threadNS, err := netns.GetCurrentNS()
if err != nil {
return fmt.Errorf("failed to open current netns: %v", err)
}
defer threadNS.Close()
// switch to target namespace
if err = NetnsSet(fdable); err != nil {
return err
}
defer func() {
err := threadNS.Set() // switch back
if err == nil {
// Unlock the current thread only when we successfully switched back
// to the original namespace; otherwise leave the thread locked which
// will force the runtime to scrap the current thread, that is maybe
// not as optimal but at least always safe to do.
runtime.UnlockOSThread()
}
}()
return toRun()
}
var wg sync.WaitGroup
wg.Add(1)
// Start the callback in a new green thread so that if we later fail
// to switch the namespace back to the original one, we can safely
// leave the thread locked to die without a risk of the current thread
// left lingering with incorrect namespace.
var innerError error
go func() {
defer wg.Done()
runtime.LockOSThread()
innerError = containedCall()
}()
wg.Wait()
return innerError
}