This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
187 lines (155 loc) · 4.42 KB
/
main.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
// Copyright 2019 Kinvolk GmbH
//
// 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 main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
)
// getNSIno takes namespace type and a pid and returns the namespace inode
// number
func getNSIno(nsType, pid string) (int, error) {
nsPath := filepath.Join("/proc", pid, "ns", nsType)
i, err := os.Readlink(nsPath)
if err != nil {
return -1, err
}
// net:[4026532431] -> 4026531837
inoStr := strings.Split(i, ":")[1]
inoStr = inoStr[1 : len(inoStr)-1]
ino, err := strconv.Atoi(inoStr)
if err != nil {
return -1, err
}
return ino, nil
}
// getAllNetNS goes through /proc/* and returns a map from network namespace
// inode number to its path for all the network namespaces in all processes
func getAllNetNS() (map[int]string, error) {
netNamespaces := make(map[int]string)
procDirs, _ := filepath.Glob("/proc/[0-9]*")
for _, f := range procDirs { // each file representing a process
_, pid := filepath.Split(f)
ns, err := getNSIno("net", pid)
if err != nil {
fmt.Fprintf(os.Stderr, "error resolving %v: %v\n", pid, err)
continue
}
// inode number -> path
netNamespaces[ns] = filepath.Join(f, "ns", "net")
}
return netNamespaces, nil
}
type ifInfo struct {
Ifindex int `json:"ifindex"`
IFName string `json:"ifname"`
LinkNetnsId *int `json:"link_netnsid"`
}
// getLinkNetNSID gets the link-netnsid of vethName by calling the ip command
func getLinkNetNSID(vethName string) (int, error) {
prog := "ip"
args := []string{
"--json",
"link",
"show",
vethName}
output, err := exec.Command(prog, args...).CombinedOutput()
if err != nil {
return -1, fmt.Errorf("failed to get link-netnsid from veth %v: %v\n%v", vethName, err, string(output))
}
m := []ifInfo{}
err = json.Unmarshal(output, &m)
if err != nil {
return -1, fmt.Errorf("cannot parse json output: %v\n%s", err, output)
}
if m[0].LinkNetnsId == nil {
return -1, fmt.Errorf("iface %v doesn't have link-netnsid, is it in a different network namespace?", vethName)
}
return *m[0].LinkNetnsId, nil
}
// getLocalNetNSID gets the local network namespace ID of netNSPath through
// netlink from the point of view of the current network namespace
func getLocalNetNSID(netNSPath string) (int, error) {
f, err := os.Open(netNSPath)
if err != nil {
return -1, err
}
defer f.Close()
fd := f.Fd()
localNetNSID, err := netlink.GetNetNsIdByFd(int(fd))
if err != nil {
return -1, err
}
return localNetNSID, nil
}
// getNetNSFromVeth takes a veth name and returns the network namespace inode
// number of the other end of the veth pair
func getNetNSFromVeth(vethName string) (int, error) {
linkNetNSID, err := getLinkNetNSID(vethName)
if err != nil {
return -1, err
}
fmt.Fprintf(os.Stdout, "link-netnsid of %s: %d\n", vethName, linkNetNSID)
netNSs, err := getAllNetNS()
if err != nil {
return -1, err
}
origNS, err := netns.Get()
if err != nil {
return -1, err
}
defer origNS.Close()
for _, netNSPath := range netNSs {
nsHandle, err := netns.GetFromPath(netNSPath)
if err != nil {
return -1, err
}
defer nsHandle.Close()
if err := netns.Set(nsHandle); err != nil {
return -1, err
}
defer netns.Set(origNS)
for netNSIno, netNSPath2 := range netNSs {
localNetNSID, err := getLocalNetNSID(netNSPath2)
if err != nil {
return -1, err
}
if localNetNSID == linkNetNSID {
return netNSIno, nil
}
}
}
return -1, fmt.Errorf("netns inode for %q not found", vethName)
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "usage: %s VETH_NAME\n", os.Args[0])
os.Exit(1)
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
netNS, err := getNetNSFromVeth(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "netns ino ID for %v: %v\n", os.Args[1], netNS)
}