Compare commits
No commits in common. "29a96409ed90f36430691b90255789b34a00fcf9" and "1ea44dfaf985670c6a5ef13c8958723daf84365e" have entirely different histories.
29a96409ed
...
1ea44dfaf9
5 changed files with 59 additions and 257 deletions
|
|
@ -5,11 +5,22 @@ package netns
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
func call(name string, fn func() error) error {
|
func call(name string, fn func() error) error {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
// sauvegarde du netns courant
|
||||||
|
orig, err := os.Open("/proc/self/ns/net")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer orig.Close()
|
||||||
|
|
||||||
// entrer dans le netns cible
|
// entrer dans le netns cible
|
||||||
f, err := os.Open(fmt.Sprintf("/var/run/netns/%s", name))
|
f, err := os.Open(fmt.Sprintf("/var/run/netns/%s", name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -17,7 +28,17 @@ func call(name string, fn func() error) error {
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
return onThread(func() error {
|
if err := unix.Setns(int(f.Fd()), unix.CLONE_NEWNET); err != nil {
|
||||||
return unix.Setns(int(f.Fd()), unix.CLONE_NEWNET)
|
return err
|
||||||
}, fn)
|
}
|
||||||
|
|
||||||
|
// exécuter la fonction dans le netns
|
||||||
|
err = fn()
|
||||||
|
|
||||||
|
// toujours revenir au netns d'origine
|
||||||
|
if restoreErr := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); restoreErr != nil {
|
||||||
|
return restoreErr
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,15 @@ package netns
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
func create(name string) error {
|
func create(name string) error {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
base := "/var/run/netns"
|
base := "/var/run/netns"
|
||||||
path := base + "/" + name
|
path := base + "/" + name
|
||||||
|
|
||||||
|
|
@ -30,20 +34,36 @@ func create(name string) error {
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
|
|
||||||
|
// sauvegarde du netns courant
|
||||||
|
orig, err := os.Open("/proc/self/ns/net")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer orig.Close()
|
||||||
|
|
||||||
// nouveau netns
|
// nouveau netns
|
||||||
return onThread(func() error {
|
if err := unix.Unshare(unix.CLONE_NEWNET); err != nil {
|
||||||
return unix.Unshare(unix.CLONE_NEWNET)
|
return err
|
||||||
}, func() error {
|
}
|
||||||
|
|
||||||
// bind mount du netns du thread 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),
|
// /proc/self/ns/net pointe vers le ns du processus (thread principal),
|
||||||
// pas du thread courant — il faut utiliser le tid explicitement
|
// pas du thread courant — il faut utiliser le tid explicitement
|
||||||
threadNsPath := fmt.Sprintf("/proc/self/task/%d/ns/net", unix.Gettid())
|
threadNsPath := fmt.Sprintf("/proc/self/task/%d/ns/net", unix.Gettid())
|
||||||
return unix.Mount(
|
if err := unix.Mount(
|
||||||
threadNsPath,
|
threadNsPath,
|
||||||
path,
|
path,
|
||||||
"",
|
"",
|
||||||
unix.MS_BIND,
|
unix.MS_BIND,
|
||||||
"",
|
"",
|
||||||
)
|
); err != nil {
|
||||||
})
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// revenir au netns original
|
||||||
|
if err := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
//go:build linux
|
|
||||||
|
|
||||||
package netns
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
func onThread(enter func() error, fn func() error) error {
|
|
||||||
runtime.LockOSThread()
|
|
||||||
|
|
||||||
orig, err := os.Open("/proc/thread-self/ns/net")
|
|
||||||
if err != nil {
|
|
||||||
runtime.UnlockOSThread()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer orig.Close()
|
|
||||||
|
|
||||||
err = enter()
|
|
||||||
if err == nil {
|
|
||||||
err = fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
if restoreErr := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); restoreErr != nil {
|
|
||||||
return restoreErr
|
|
||||||
}
|
|
||||||
|
|
||||||
runtime.UnlockOSThread()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
@ -1,199 +0,0 @@
|
||||||
//go:build linux
|
|
||||||
|
|
||||||
package netns
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
runtime.LockOSThread()
|
|
||||||
}
|
|
||||||
|
|
||||||
var onMain = make(chan func())
|
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
|
||||||
code := make(chan int)
|
|
||||||
go func() { code <- m.Run() }()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case f := <-onMain:
|
|
||||||
f()
|
|
||||||
case c := <-code:
|
|
||||||
os.Exit(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func runOnMain(f func()) {
|
|
||||||
done := make(chan struct{})
|
|
||||||
onMain <- func() {
|
|
||||||
f()
|
|
||||||
close(done)
|
|
||||||
}
|
|
||||||
<-done
|
|
||||||
}
|
|
||||||
|
|
||||||
type nsID struct{ dev, ino uint64 }
|
|
||||||
|
|
||||||
func statNS(t *testing.T, path string) nsID {
|
|
||||||
t.Helper()
|
|
||||||
var st unix.Stat_t
|
|
||||||
if err := unix.Stat(path, &st); err != nil {
|
|
||||||
t.Fatalf("stat %s : %v", path, err)
|
|
||||||
}
|
|
||||||
return nsID{uint64(st.Dev), st.Ino}
|
|
||||||
}
|
|
||||||
|
|
||||||
func threadNS(t *testing.T) nsID {
|
|
||||||
t.Helper()
|
|
||||||
return statNS(t, "/proc/thread-self/ns/net")
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupMainInOtherNS(t *testing.T) (nsID, string) {
|
|
||||||
t.Helper()
|
|
||||||
if os.Geteuid() != 0 {
|
|
||||||
t.Skip("netns : root requis")
|
|
||||||
}
|
|
||||||
runtime.LockOSThread()
|
|
||||||
root := threadNS(t)
|
|
||||||
rootFD, err := os.Open("/proc/thread-self/ns/net")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("netns racine : %v", err)
|
|
||||||
}
|
|
||||||
runtime.UnlockOSThread()
|
|
||||||
|
|
||||||
other := fmt.Sprintf("t91-main-%d", os.Getpid())
|
|
||||||
if err := Create(other); err != nil {
|
|
||||||
t.Fatalf("création de %s : %v", other, err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
if err := Delete(other); err != nil {
|
|
||||||
t.Errorf("suppression de %s : %v", other, err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
var setErr error
|
|
||||||
runOnMain(func() {
|
|
||||||
f, err := os.Open("/var/run/netns/" + other)
|
|
||||||
if err != nil {
|
|
||||||
setErr = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
setErr = unix.Setns(int(f.Fd()), unix.CLONE_NEWNET)
|
|
||||||
})
|
|
||||||
if setErr != nil {
|
|
||||||
t.Fatalf("thread principal dans %s : %v", other, setErr)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
var err error
|
|
||||||
runOnMain(func() { err = unix.Setns(int(rootFD.Fd()), unix.CLONE_NEWNET) })
|
|
||||||
rootFD.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("thread principal ramené au netns racine : %v", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return root, other
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertBackToRoot(t *testing.T, root nsID) {
|
|
||||||
t.Helper()
|
|
||||||
if got := threadNS(t); got != root {
|
|
||||||
t.Fatalf("thread laissé dans le netns %v, netns racine %v attendu", got, root)
|
|
||||||
}
|
|
||||||
runtime.UnlockOSThread()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCall_ThreadPrincipalDansUnAutreNetns(t *testing.T) {
|
|
||||||
root, other := setupMainInOtherNS(t)
|
|
||||||
target := fmt.Sprintf("t91-cible-%d", os.Getpid())
|
|
||||||
if err := Create(target); err != nil {
|
|
||||||
t.Fatalf("création de %s : %v", target, err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
if err := Delete(target); err != nil {
|
|
||||||
t.Errorf("suppression de %s : %v", target, err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
runtime.LockOSThread()
|
|
||||||
var inside nsID
|
|
||||||
if err := Call(target, func() error {
|
|
||||||
inside = threadNS(t)
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
t.Fatalf("Call : %v", err)
|
|
||||||
}
|
|
||||||
if want := statNS(t, "/var/run/netns/"+target); inside != want {
|
|
||||||
t.Errorf("fn exécutée dans %v, netns %s %v attendu", inside, target, want)
|
|
||||||
}
|
|
||||||
if inside == statNS(t, "/var/run/netns/"+other) {
|
|
||||||
t.Errorf("fn exécutée dans le netns du thread principal")
|
|
||||||
}
|
|
||||||
assertBackToRoot(t, root)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCall_ErreurDeFnRenvoyeeEtThreadRestaure(t *testing.T) {
|
|
||||||
root, _ := setupMainInOtherNS(t)
|
|
||||||
target := fmt.Sprintf("t91-err-%d", os.Getpid())
|
|
||||||
if err := Create(target); err != nil {
|
|
||||||
t.Fatalf("création de %s : %v", target, err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
if err := Delete(target); err != nil {
|
|
||||||
t.Errorf("suppression de %s : %v", target, err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
runtime.LockOSThread()
|
|
||||||
want := errors.New("échec de fn")
|
|
||||||
if err := Call(target, func() error { return want }); !errors.Is(err, want) {
|
|
||||||
t.Errorf("Call : %v, %v attendu", err, want)
|
|
||||||
}
|
|
||||||
assertBackToRoot(t, root)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCall_NetnsAbsent(t *testing.T) {
|
|
||||||
root, _ := setupMainInOtherNS(t)
|
|
||||||
|
|
||||||
runtime.LockOSThread()
|
|
||||||
called := false
|
|
||||||
err := Call(fmt.Sprintf("t91-absent-%d", os.Getpid()), func() error {
|
|
||||||
called = true
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if !errors.Is(err, os.ErrNotExist) {
|
|
||||||
t.Errorf("Call : %v, netns absent attendu", err)
|
|
||||||
}
|
|
||||||
if called {
|
|
||||||
t.Errorf("fn appelée sans netns")
|
|
||||||
}
|
|
||||||
assertBackToRoot(t, root)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreate_ThreadPrincipalDansUnAutreNetns(t *testing.T) {
|
|
||||||
root, other := setupMainInOtherNS(t)
|
|
||||||
name := fmt.Sprintf("t91-cree-%d", os.Getpid())
|
|
||||||
|
|
||||||
runtime.LockOSThread()
|
|
||||||
if err := Create(name); err != nil {
|
|
||||||
t.Fatalf("Create : %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
if err := Delete(name); err != nil {
|
|
||||||
t.Errorf("suppression de %s : %v", name, err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
created := statNS(t, "/var/run/netns/"+name)
|
|
||||||
if created == root || created == statNS(t, "/var/run/netns/"+other) {
|
|
||||||
t.Errorf("netns %s %v : nouveau netns attendu", name, created)
|
|
||||||
}
|
|
||||||
assertBackToRoot(t, root)
|
|
||||||
}
|
|
||||||
|
|
@ -80,13 +80,6 @@
|
||||||
- **La suppression d'un VPC échoue si l'un de ses subnets échoue (#79).** Elle attendait qu'aucun
|
- **La suppression d'un VPC échoue si l'un de ses subnets échoue (#79).** Elle attendait qu'aucun
|
||||||
subnet ne soit plus en `deleting`, puis supprimait le VPC même si un subnet était passé en
|
subnet ne soit plus en `deleting`, puis supprimait le VPC même si un subnet était passé en
|
||||||
`error`. Il en va de même pour un subnet dont une VM attendue passe en `error`
|
`error`. Il en va de même pour un subnet dont une VM attendue passe en `error`
|
||||||
- **Un thread de l'agent ne reste plus dans le netns d'un VPC (#91).** Après une opération dans
|
|
||||||
un netns, l'agent revenait au netns du thread principal du processus et non à celui du thread
|
|
||||||
courant. Si le thread principal était lui-même dans un VPC à cet instant, le thread restait dans
|
|
||||||
ce VPC et la goroutine suivante y travaillait : en lab, la suppression d'un subnet a échoué sur
|
|
||||||
`delete vxlan: Link not found` et le watchdog a signalé `br-000000` et des VRF absents. L'agent
|
|
||||||
revient désormais au netns du thread courant, et un thread qui ne peut pas y revenir est
|
|
||||||
détruit au lieu d'être réutilisé. La création d'un netns est corrigée de la même façon
|
|
||||||
|
|
||||||
## Mise à jour depuis la 0.2.0
|
## Mise à jour depuis la 0.2.0
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue