-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils_linux.go
225 lines (190 loc) · 5.39 KB
/
utils_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
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
package main
import (
"fmt"
"net"
"strings"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
)
var runuAuxFileDir = "/usr/lib/runu"
func getVethHost(spec *specs.Spec) *net.Interface {
var netnsPath string
if spec.Linux != nil {
for _, v := range spec.Linux.Namespaces {
if v.Type == specs.NetworkNamespace {
netnsPath = v.Path
break
}
}
}
// should look like '/var/run/netns/cni-6d46d1b2-c836-3b4e-87a0-88641242aa5e'
if strings.Index(netnsPath, "/var/run/netns/") == -1 {
return nil
}
origns, _ := netns.Get()
defer origns.Close()
netnsPath = strings.Replace(netnsPath, "/var/run/netns/", "", 1)
nsh, err := netns.GetFromName(netnsPath)
if err != nil {
logrus.Errorf("unable to get netns handle %s(%s)",
netnsPath, err)
return nil
}
if err := netns.Set(nsh); err != nil {
logrus.Errorf("unable to get set netns %s", err)
return nil
}
// Look for the ifindex of veth pair of the host interface
vEth, err := netlink.LinkByName("eth0")
if err != nil {
logrus.Errorf("unable to get guest veth %s", err)
return nil
}
ifIndex, err := netlink.VethPeerIndex(&netlink.Veth{LinkAttrs: *vEth.Attrs()})
if err != nil {
logrus.Errorf("unable to get ifindex of veth pair %s", err)
return nil
}
// Switch back to the original namespace
netns.Set(origns)
iface, err := net.InterfaceByIndex(ifIndex)
if err != nil {
logrus.Errorf("unable to get interface of veth pair (idx=%d)(%s)",
ifIndex, err)
return nil
}
return iface
}
func retrieveRouteInfo() ([]lklRoute, error) {
var routeInfo []lklRoute
// listing routes
// We only care about eth0
link, err := netlink.LinkByName("eth0")
if err != nil {
return nil, fmt.Errorf("cannot find link eth0: (%v)", err)
}
routes, err := netlink.RouteList(link, netlink.FAMILY_V4)
if err != nil {
return nil, fmt.Errorf("cannot list routes: (%v)", err)
}
for _, route := range routes {
var r lklRoute
// skip default gateway
if route.Dst == nil {
continue
}
logrus.Debugf("route= %+v", route)
r.Destination = route.Dst.String()
if route.Gw != nil {
r.Nexthop = route.Gw.String()
} else {
// onlink route
ifgw, err := netlink.LinkByIndex(route.LinkIndex)
if err != nil {
logrus.Infof("incomplete route (%+v)", route)
}
r.Nexthop = ifgw.Attrs().Name
}
routeInfo = append(routeInfo, r)
}
return routeInfo, nil
}
// XXX: Only treat ipv4 information
func getVethInfo(spec *specs.Spec) (*lklIfInfo, []lklRoute, error) {
ifInfo := new(lklIfInfo)
defer func() {
logrus.Infof("ifInfo %+v", ifInfo)
}()
// default gateway
v4gw, err := netlink.RouteGet(net.ParseIP("8.8.8.8"))
if err != nil {
return nil, nil, fmt.Errorf("Could not determine single default route (got %v)",
len(v4gw))
}
ifInfo.v4Gw = v4gw[0].Gw
logrus.Infof("gw %v", v4gw)
routeInfo, err := retrieveRouteInfo()
if err != nil {
return nil, nil, fmt.Errorf("Error during route retrival (got %v)", err)
}
ifaces, _ := net.Interfaces()
logrus.Debugf("ifaces= %+v", ifaces)
for _, iface := range ifaces {
if iface.Name != "eth0" {
continue
}
allAddrs, _ := iface.Addrs()
for _, ifaddr := range allAddrs {
ipNet, ok := ifaddr.(*net.IPNet)
if !ok {
return nil, nil, fmt.Errorf("address is not IPNet: %+v", ifaddr)
}
logrus.Infof("ifaddr= %s, ipnet=%s, gw=%s", ifaddr, ipNet, ifInfo.v4Gw)
// XXX: We only treat IPv4 at the moment
// may need to work for IPv6 support
if ipNet.IP.To4() == nil {
continue
}
ifInfo.ifAddrs = append(ifInfo.ifAddrs, *ipNet)
ifInfo.ifName = "eth0"
// Get the link for the interface.
ifaceLink, err := netlink.LinkByName(iface.Name)
if err != nil {
return nil, nil, fmt.Errorf("getting link for interface %q: %v", iface.Name, err)
}
// Steal IP address from NIC.
r4addr, err := netlink.ParseAddr(ipNet.String())
if err != nil {
return nil, nil, fmt.Errorf("parse address error %v: %v", iface.Name, err)
}
logrus.Debugf("r4addr= %s", r4addr)
// XXX: delete only the main container (works fine but dunno ?)
if spec.Process.Args[0] != "/pause" {
if err := netlink.AddrDel(ifaceLink, r4addr); err != nil {
return nil, nil, fmt.Errorf("removing address %v from device %q: %v",
iface.Name, ipNet, err)
}
}
break
}
break
}
return ifInfo, routeInfo, nil
}
func setupNetwork(spec *specs.Spec) (*lklIfInfo, []lklRoute, error) {
var netnsPath string
// disable HW offload if raw socket
vethHostIf := getVethHost(spec)
// only pause container on k8s retures non-nil value
if vethHostIf != nil {
// XXX: this disable can be eliminated when vnethdr on raw sock
// is implemented.
disableTxCsumOffloadForRawsock(vethHostIf.Name)
}
if spec.Linux != nil {
for _, v := range spec.Linux.Namespaces {
if v.Type == specs.NetworkNamespace {
netnsPath = v.Path
break
}
}
}
// if there is no path, then runu assumes
// it runs on docker (not on k8s)
if netnsPath == "" {
logrus.Infof("no netns detected: no addr configuration, skipped")
return nil, nil, nil
}
logrus.Infof("nspath= %s", netnsPath)
nsh, err := netns.GetFromPath(netnsPath)
if err != nil {
return nil, nil, fmt.Errorf("unable to get netns handle %s", err)
}
if err := netns.Set(nsh); err != nil {
return nil, nil, fmt.Errorf("unable to get set netns %s", err)
}
// now traverse in netns
return getVethInfo(spec)
}