Skip to content

Commit df4f226

Browse files
Optimize call flow to complete loopback adapter creation (#408)
Loopback adapter creation operation comprises of two operations - createInterface and setWeakHostOnInterface. These operations take place inside the lock. If there are simultaneous requests, it interleaves these calls causing every loopback adapter creation to absorb the delay due to interleaving. createInterface can take time in seconds (typically 2 to 7 seconds based on the tests) while setWeakHostOnInterface finishes very quickly ( less than a second ). This change calls setWeakHostOnInterface within the same lock if createInterface succeeds. The tests show this improves the loopback adapter creation times for simultaneous requests.
1 parent 923b7d5 commit df4f226

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ CNSFILES = \
4343
$(wildcard cns/restserver/*.go) \
4444
$(wildcard cns/routes/*.go) \
4545
$(wildcard cns/service/*.go) \
46+
$(wildcard cns/networkcontainers/*.go) \
4647
$(COREFILES) \
4748
$(CNMFILES)
4849

cns/networkcontainers/networkcontainers.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,42 @@ func NewNetPluginConfiguration(binPath, configPath string) *NetPluginConfigurati
5555
func interfaceExists(iFaceName string) (bool, error) {
5656
_, err := net.InterfaceByName(iFaceName)
5757
if err != nil {
58-
errMsg := fmt.Sprintf("[Azure CNS] Unable to get interface by name %v, %v", iFaceName, err)
58+
errMsg := fmt.Sprintf("[Azure CNS] Unable to get interface by name %s. Error: %v", iFaceName, err)
5959
log.Printf(errMsg)
6060
return false, errors.New(errMsg)
6161
}
6262

63+
log.Printf("[Azure CNS] Found interface by name %s", iFaceName)
64+
6365
return true, nil
6466
}
6567

6668
// Create creates a network container.
6769
func (cn *NetworkContainers) Create(createNetworkContainerRequest cns.CreateNetworkContainerRequest) error {
68-
log.Printf("[Azure CNS] NetworkContainers.Create called")
70+
log.Printf("[Azure CNS] NetworkContainers.Create called for NC: %s", createNetworkContainerRequest.NetworkContainerid)
6971
err := createOrUpdateInterface(createNetworkContainerRequest)
70-
if err == nil {
71-
err = setWeakHostOnInterface(createNetworkContainerRequest.PrimaryInterfaceIdentifier)
72-
}
73-
log.Printf("[Azure CNS] NetworkContainers.Create finished.")
72+
log.Printf("[Azure CNS] NetworkContainers.Create completed for NC: %s with error: %v",
73+
createNetworkContainerRequest.NetworkContainerid, err)
74+
7475
return err
7576
}
7677

7778
// Update updates a network container.
7879
func (cn *NetworkContainers) Update(createNetworkContainerRequest cns.CreateNetworkContainerRequest, netpluginConfig *NetPluginConfiguration) error {
79-
log.Printf("[Azure CNS] NetworkContainers.Update called")
80+
log.Printf("[Azure CNS] NetworkContainers.Update called for NC: %s", createNetworkContainerRequest.NetworkContainerid)
8081
err := updateInterface(createNetworkContainerRequest, netpluginConfig)
81-
log.Printf("[Azure CNS] NetworkContainers.Update finished.")
82+
log.Printf("[Azure CNS] NetworkContainers.Update completed for NC: %s with error: %v",
83+
createNetworkContainerRequest.NetworkContainerid, err)
84+
8285
return err
8386
}
8487

8588
// Delete deletes a network container.
8689
func (cn *NetworkContainers) Delete(networkContainerID string) error {
87-
log.Printf("[Azure CNS] NetworkContainers.Delete called")
90+
log.Printf("[Azure CNS] NetworkContainers.Delete called for NC: %s", networkContainerID)
8891
err := deleteInterface(networkContainerID)
89-
log.Printf("[Azure CNS] NetworkContainers.Delete finished.")
92+
log.Printf("[Azure CNS] NetworkContainers.Delete completed for NC: %s with error: %v", networkContainerID, err)
93+
9094
return err
9195
}
9296

cns/networkcontainers/networkcontainers_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func createOrUpdateInterface(createNetworkContainerRequest cns.CreateNetworkCont
1818
return nil
1919
}
2020

21-
func setWeakHostOnInterface(ipAddress string) error {
21+
func setWeakHostOnInterface(ipAddress, ncID string) error {
2222
return nil
2323
}
2424

cns/networkcontainers/networkcontainers_windows.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ func createOrUpdateInterface(createNetworkContainerRequest cns.CreateNetworkCont
2727
return nil
2828
}
2929

30-
exists, _ := interfaceExists(createNetworkContainerRequest.NetworkContainerid)
31-
32-
if !exists {
30+
if exists, _ := interfaceExists(createNetworkContainerRequest.NetworkContainerid); !exists {
3331
return createOrUpdateWithOperation(createNetworkContainerRequest, "CREATE")
3432
}
3533

@@ -40,7 +38,7 @@ func updateInterface(createNetworkContainerRequest cns.CreateNetworkContainerReq
4038
return nil
4139
}
4240

43-
func setWeakHostOnInterface(ipAddress string) error {
41+
func setWeakHostOnInterface(ipAddress, ncID string) error {
4442
interfaces, err := net.Interfaces()
4543
if err != nil {
4644
log.Printf("[Azure CNS] Unable to retrieve interfaces on machine. %+v", err)
@@ -76,7 +74,6 @@ func setWeakHostOnInterface(ipAddress string) error {
7674
}
7775

7876
ethIndexString := strconv.Itoa(targetIface.Index)
79-
log.Printf("[Azure CNS] Going to setup weak host routing for interface with index[%v, %v]\n", targetIface.Index, ethIndexString)
8077

8178
args := []string{"/C", "AzureNetworkContainer.exe", "/logpath", log.GetLogDirectory(),
8279
"/index",
@@ -88,17 +85,17 @@ func setWeakHostOnInterface(ipAddress string) error {
8885
"/weakhostreceive",
8986
"true"}
9087

91-
log.Printf("[Azure CNS] Going to enable weak host send/receive on interface: %v", args)
88+
log.Printf("[Azure CNS] Going to enable weak host send/receive on interface: %v for NC: %s", args, ncID)
9289
c := exec.Command("cmd", args...)
9390

94-
loopbackOperationLock.Lock()
9591
bytes, err := c.Output()
96-
loopbackOperationLock.Unlock()
9792

9893
if err == nil {
99-
log.Printf("[Azure CNS] Successfully updated weak host send/receive on interface %v.\n", string(bytes))
94+
log.Printf("[Azure CNS] Successfully updated weak host send/receive for NC: %s on interface %v",
95+
ncID, string(bytes))
10096
} else {
101-
log.Printf("[Azure CNS] Received error while enable weak host send/receive on interface. %v - %v", err.Error(), string(bytes))
97+
log.Printf("[Azure CNS] Failed to update weak host send/receive for NC: %s. Error: %v. Output: %v",
98+
ncID, err.Error(), string(bytes))
10299
return err
103100
}
104101

@@ -140,17 +137,23 @@ func createOrUpdateWithOperation(createNetworkContainerRequest cns.CreateNetwork
140137
"/weakhostreceive",
141138
"true"}
142139

143-
log.Printf("[Azure CNS] Going to create/update network loopback adapter: %v", args)
144140
c := exec.Command("cmd", args...)
145141

146142
loopbackOperationLock.Lock()
143+
log.Printf("[Azure CNS] Going to create/update network loopback adapter: %v", args)
147144
bytes, err := c.Output()
145+
if err == nil {
146+
err = setWeakHostOnInterface(createNetworkContainerRequest.PrimaryInterfaceIdentifier,
147+
createNetworkContainerRequest.NetworkContainerid)
148+
}
148149
loopbackOperationLock.Unlock()
149150

150151
if err == nil {
151-
log.Printf("[Azure CNS] Successfully created network loopback adapter %v.\n", string(bytes))
152+
log.Printf("[Azure CNS] Successfully created network loopback adapter for NC: %s. Output:%v.",
153+
createNetworkContainerRequest.NetworkContainerid, string(bytes))
152154
} else {
153-
log.Printf("Received error while Creating a Network Container %v %v", err.Error(), string(bytes))
155+
log.Printf("Failed to create/update Network Container: %s. Error: %v. Output: %v",
156+
createNetworkContainerRequest.NetworkContainerid, err.Error(), string(bytes))
154157
}
155158

156159
return err
@@ -174,17 +177,19 @@ func deleteInterface(networkContainerID string) error {
174177
"/operation",
175178
"DELETE"}
176179

177-
log.Printf("[Azure CNS] Going to delete network loopback adapter: %v", args)
178180
c := exec.Command("cmd", args...)
179181

180182
loopbackOperationLock.Lock()
183+
log.Printf("[Azure CNS] Going to delete network loopback adapter: %v", args)
181184
bytes, err := c.Output()
182185
loopbackOperationLock.Unlock()
183186

184187
if err == nil {
185-
log.Printf("[Azure CNS] Successfully deleted network container %v.\n", string(bytes))
188+
log.Printf("[Azure CNS] Successfully deleted network container: %s. Output: %v.",
189+
networkContainerID, string(bytes))
186190
} else {
187-
log.Printf("Received error while deleting a Network Container %v %v", err.Error(), string(bytes))
191+
log.Printf("Failed to delete Network Container: %s. Error:%v. Output:%v",
192+
networkContainerID, err.Error(), string(bytes))
188193
return err
189194
}
190195
return nil

0 commit comments

Comments
 (0)