Skip to content

Commit 79b8a55

Browse files
authored
Merge pull request #69 from plumgrid/nsparalel
Better NetNS parallel access
2 parents 926d2f3 + 2734f2a commit 79b8a55

2 files changed

Lines changed: 18 additions & 38 deletions

File tree

plugin/driver/driver.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os/exec"
2626
"runtime"
2727
"strings"
28+
"time"
2829

2930
Log "github.com/Sirupsen/logrus"
3031
"github.com/docker/libnetwork/drivers/remote/api"
@@ -352,20 +353,22 @@ func (driver *driver) joinEndpoint(w http.ResponseWriter, r *http.Request) {
352353
}
353354

354355
objectResponse(w, res)
355-
Log.Infof("Join endpoint %s:%s to %s", j.NetworkID, j.EndpointID, j.SandboxKey)
356+
Log.Infof("Join endpoint %s: %s to %s", j.NetworkID, j.EndpointID, j.SandboxKey)
356357

357358
if auto_arp {
358359
go func(net_ns_path string, pg_ifc_prefix string) {
359360

360361
// Disallow this goroutine to work on any other thread than this one
361362
// since namespace ops (unshare, setns) are done for a single thread, we
362363
// must ensure that the goroutine does not jump from OS thread to thread
363-
runtime.LockOSThread()
364-
defer runtime.UnlockOSThread()
365-
366-
err := RunContainerArping(net_ns_path, pg_ifc_prefix)
367-
if err != nil {
368-
Log.Printf("Error while running arping : %v", err)
364+
for i := 0; i <= 4; i++ {
365+
runtime.LockOSThread()
366+
err := RunContainerArping(net_ns_path, pg_ifc_prefix)
367+
if err != nil {
368+
Log.Printf("Arping failed : %v", err)
369+
}
370+
runtime.UnlockOSThread()
371+
time.Sleep(1 * time.Second)
369372
}
370373

371374
}(j.SandboxKey, ifname.DstPrefix)

plugin/driver/network_utils.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"fmt"
1919
"net"
2020
"strings"
21-
"time"
2221

2322
"github.com/google/gopacket"
2423
"github.com/google/gopacket/layers"
@@ -32,27 +31,12 @@ import (
3231
// that match with the prefix given to libnetwork
3332
func RunContainerArping(net_ns_path string, pg_ifc_prefix string) error {
3433

35-
Log.Printf("Attempting to acces the container %s NetNS", net_ns_path)
3634
var netns ns.NetNS
3735
var err error
38-
success := false
3936

40-
for attempts := 0; attempts <= 5; attempts++ {
41-
42-
netns, err = ns.GetNS(net_ns_path)
43-
44-
if err != nil {
45-
Log.Printf("Attempt %d: failed to open netns %s: %v", attempts, net_ns_path, err)
46-
time.Sleep(1 * time.Second)
47-
} else {
48-
success = true
49-
break
50-
}
51-
attempts += 1
52-
}
53-
54-
if !success {
55-
return fmt.Errorf("Attempts exhaused, could not access %s, exiting", net_ns_path)
37+
netns, err = ns.GetNS(net_ns_path)
38+
if err != nil {
39+
return fmt.Errorf("Failed to open netns %s: %v", net_ns_path, err)
5640
}
5741
defer netns.Close()
5842

@@ -70,8 +54,6 @@ func RunContainerArping(net_ns_path string, pg_ifc_prefix string) error {
7054
if strings.HasPrefix(ifc.Name, pg_ifc_prefix) {
7155
// Valid PG-created ifc
7256

73-
// TODO we might want to add a retry here to make sure,
74-
// in case the IFC is up but still has no IP, we retry
7557
address_slice, err := ifc.Addrs()
7658
if err != nil {
7759
Log.Errorf("Failed to get Address slice for ifc %s: %v", ifc.Name, err)
@@ -83,7 +65,6 @@ func RunContainerArping(net_ns_path string, pg_ifc_prefix string) error {
8365
continue
8466
}
8567

86-
Log.Printf("Attempting raw socket open on %s", ifc.Name)
8768
handle, err := pcap.OpenLive(ifc.Name, 65536, true, pcap.BlockForever)
8869
if err != nil {
8970
return fmt.Errorf("Failed to open raw socket %s : %v", ifc.Name, err)
@@ -105,18 +86,14 @@ func RunContainerArping(net_ns_path string, pg_ifc_prefix string) error {
10586
// Get ARP packet
10687
arp_pkt := getGratuitousArp(ifc.HardwareAddr, ip)
10788

108-
for i := 0; i <= 3; i++ {
109-
Log.Printf("Sending %d GARP on %s : %s - %s",
110-
i, ifc.Name, ifc.HardwareAddr.String(), ip.String())
89+
Log.Printf("Sending %s GARP on %s : %s",
90+
ifc.Name, ifc.HardwareAddr.String(), ip.String())
11191

112-
if err := handle.WritePacketData(arp_pkt); err != nil {
113-
handle.Close()
114-
return fmt.Errorf("Failed to write ARP packet data on %s : %v", ifc.Name, err)
115-
}
116-
time.Sleep(750 * time.Millisecond)
92+
if err := handle.WritePacketData(arp_pkt); err != nil {
93+
Log.Printf("Failed to write ARP packet data on %s : %v", ifc.Name, err)
94+
continue
11795
}
11896
}
119-
12097
// Close the socket
12198
handle.Close()
12299
}

0 commit comments

Comments
 (0)