Compare commits
6 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
ff17645c62 |
|||
|
d9f2ec4e67 |
|||
|
74d7fc1425 |
|||
|
beed163b02 |
|||
|
049cabc489 |
|||
|
1e7575bc7d |
4 changed files with 24 additions and 10 deletions
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
package netns
|
||||
|
||||
func call(name string, fn func() error) error {
|
||||
func call(_ string, fn func() error) error {
|
||||
return fn()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,12 +3,17 @@
|
|||
package netns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func create(name string) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
base := "/var/run/netns"
|
||||
path := base + "/" + name
|
||||
|
||||
|
|
@ -16,6 +21,12 @@ func create(name string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// si le fichier existe déjà, le démonter d'abord
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
unix.Unmount(path, unix.MNT_DETACH)
|
||||
os.Remove(path)
|
||||
}
|
||||
|
||||
// fichier cible
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
|
|
@ -35,9 +46,12 @@ func create(name string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// bind mount du netns courant vers /var/run/netns/<name>
|
||||
// bind mount du netns du thread courant vers /var/run/netns/<name>
|
||||
// /proc/self/ns/net pointe vers le ns du processus (thread principal),
|
||||
// pas du thread courant — il faut utiliser le tid explicitement
|
||||
threadNsPath := fmt.Sprintf("/proc/self/task/%d/ns/net", unix.Gettid())
|
||||
if err := unix.Mount(
|
||||
"/proc/self/ns/net",
|
||||
threadNsPath,
|
||||
path,
|
||||
"",
|
||||
unix.MS_BIND,
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ func CreateVPC(db *badger.DB, name string) error {
|
|||
}
|
||||
|
||||
// create veth public for this netns
|
||||
if err := netif.CreateVethToNetns("veth"+name+"ext", "vethpublicint", "/var/run/netns/"+name, 9000); err != nil {
|
||||
if err := netif.CreateVethToNetns("vp-"+name+"-e", "vp-public-i", "/var/run/netns/"+name, 9000); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -34,24 +34,24 @@ func CreateVPC(db *badger.DB, name string) error {
|
|||
}
|
||||
|
||||
// set veth to ext public bridge
|
||||
if err := netif.BridgeSetMaster("veth"+name+"ext", "br-public"); err != nil {
|
||||
if err := netif.BridgeSetMaster("vp-"+name+"-e", "br-public"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set veth to int public bridge
|
||||
if err := netns.Call(name, func() error {
|
||||
return netif.BridgeSetMaster("vethpublicint", "br-public")
|
||||
return netif.BridgeSetMaster("vp-public-i", "br-public")
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set set ext veth up
|
||||
if err := netif.LinkSetUp("veth" + name + "ext"); err != nil {
|
||||
return nil
|
||||
if err := netif.LinkSetUp("vp-" + name + "-e"); err != nil {
|
||||
return err
|
||||
}
|
||||
// set set int veth up
|
||||
if err := netns.Call(name, func() error {
|
||||
return netif.LinkSetUp("vethpublicint")
|
||||
return netif.LinkSetUp("vp-public-i")
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ func DeleteVPC(db *badger.DB, name string) error {
|
|||
if state, err := kv.GetFromDB(db, "vpc/"+name+"/state"); err != nil {
|
||||
return err
|
||||
} else if state == "deleting" {
|
||||
if err := netif.DeleteLink(name + "-ext"); err != nil {
|
||||
if err := netif.DeleteLink("vp-" + name + "-e"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue