-
Notifications
You must be signed in to change notification settings - Fork 1
/
net.go
41 lines (33 loc) · 819 Bytes
/
net.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
package main
import (
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
)
func initNet() {
}
func createRouteFromCurrentNetns(hostIfName string) error {
currentNs, err := netns.Get()
if err != nil {
return err
}
return runInBackgroundNetns(func() error {
la := netlink.NewLinkAttrs()
la.Name = "eth1"
la.Namespace = netlink.NsFd(currentNs)
veth := &netlink.Veth{
LinkAttrs: la,
PeerName: hostIfName,
}
return netlink.LinkAdd(veth)
})
}
func runInBackgroundNetns(payload func() error) error {
/* If an OS thread has non-background net namespace, it must
have been locked to a goroutine. Newly spawned goroutine
will always start in the background namespace. */
finishedch := make(chan error)
go func() {
finishedch <- payload()
}()
return <-finishedch
}