Skip to content

Commit 83617be

Browse files
committed
Refactored ebtables to remove redundant code
1 parent f17e20a commit 83617be

File tree

3 files changed

+32
-63
lines changed

3 files changed

+32
-63
lines changed

ebtables/ebtables.go

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ package ebtables
66
import (
77
"fmt"
88
"io/ioutil"
9+
"net"
910
"os/exec"
1011
"strings"
1112

1213
"github.com/Azure/azure-container-networking/log"
1314
)
1415

15-
// Init initializes the ebtables module.
16-
func init() {
17-
installEbtables()
18-
}
16+
const (
17+
// Ebtables actions.
18+
Append = "-A"
19+
Delete = "-D"
20+
)
1921

2022
// InstallEbtables installs the ebtables package.
2123
func installEbtables() {
@@ -31,64 +33,31 @@ func installEbtables() {
3133
}
3234
}
3335

34-
// SetupSnatForOutgoingPackets sets up snat
35-
func SetupSnatForOutgoingPackets(interfaceName string, snatAddress string) error {
36-
command := fmt.Sprintf("ebtables -t nat -A POSTROUTING -o %s -j snat --to-source %s --snat-arp", interfaceName, snatAddress)
37-
err := executeShellCommand(command)
38-
if err != nil {
39-
return err
40-
}
41-
return nil
42-
}
36+
// SetSnatForInterface sets a MAC SNAT rule for an interface.
37+
func SetSnatForInterface(interfaceName string, macAddress net.HardwareAddr, action string) error {
38+
command := fmt.Sprintf(
39+
"ebtables -t nat %s POSTROUTING -o %s -j snat --to-src %s --snat-arp",
40+
action, interfaceName, macAddress.String())
4341

44-
// CleanupSnatForOutgoingPackets cleans up snat
45-
func CleanupSnatForOutgoingPackets(interfaceName string, snatAddress string) error {
46-
command := fmt.Sprintf("ebtables -t nat -D POSTROUTING -o %s -j snat --to-source %s --snat-arp", interfaceName, snatAddress)
47-
err := executeShellCommand(command)
48-
if err != nil {
49-
return err
50-
}
51-
return nil
42+
return executeShellCommand(command)
5243
}
5344

54-
// SetupDnatForArpReplies sets up dnat
55-
func SetupDnatForArpReplies(interfaceName string) error {
56-
command := fmt.Sprintf("ebtables -t nat -A PREROUTING -i %s -p arp -j dnat --to-destination ff:ff:ff:ff:ff:ff", interfaceName)
57-
err := executeShellCommand(command)
58-
if err != nil {
59-
return err
60-
}
61-
return nil
62-
}
45+
// SetDnatForArpReplies sets a MAC DNAT rule for ARP replies received on an interface.
46+
func SetDnatForArpReplies(interfaceName string, action string) error {
47+
command := fmt.Sprintf(
48+
"ebtables -t nat %s PREROUTING -p ARP -i %s -j dnat --to-dst ff:ff:ff:ff:ff:ff",
49+
action, interfaceName)
6350

64-
// CleanupDnatForArpReplies cleans up dnat
65-
func CleanupDnatForArpReplies(interfaceName string) error {
66-
command := fmt.Sprintf("ebtables -t nat -D PREROUTING -i %s -p arp -j dnat --to-destination ff:ff:ff:ff:ff:ff", interfaceName)
67-
err := executeShellCommand(command)
68-
if err != nil {
69-
return err
70-
}
71-
return nil
51+
return executeShellCommand(command)
7252
}
7353

74-
// SetupDnatBasedOnIPV4Address sets up dnat
75-
func SetupDnatBasedOnIPV4Address(ipv4Address string, macAddress string) error {
76-
command := fmt.Sprintf("ebtables -t nat -A PREROUTING -p IPv4 --ip-dst %s -j dnat --to-dst %s --dnat-target ACCEPT", ipv4Address, macAddress)
77-
err := executeShellCommand(command)
78-
if err != nil {
79-
return err
80-
}
81-
return nil
82-
}
54+
// SetDnatForIPAddress sets a MAC DNAT rule for an IP address.
55+
func SetDnatForIPAddress(ipAddress net.IP, macAddress net.HardwareAddr, action string) error {
56+
command := fmt.Sprintf(
57+
"ebtables -t nat %s PREROUTING -p IPv4 --ip-dst %s -j dnat --to-dst %s",
58+
action, ipAddress.String(), macAddress.String())
8359

84-
// RemoveDnatBasedOnIPV4Address cleans up dnat
85-
func RemoveDnatBasedOnIPV4Address(ipv4Address string, macAddress string) error {
86-
command := fmt.Sprintf("ebtables -t nat -D PREROUTING -p IPv4 --ip-dst %s -j dnat --to-dst %s --dnat-target ACCEPT", ipv4Address, macAddress)
87-
err := executeShellCommand(command)
88-
if err != nil {
89-
return err
90-
}
91-
return nil
60+
return executeShellCommand(command)
9261
}
9362

9463
func executeShellCommand(command string) error {

network/endpoint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
101101
// Setup MAC address translation rules for container interface.
102102
log.Printf("[net] Setting up MAC address translation rules for endpoint %v.", contIfName)
103103
for _, ipAddr := range epInfo.IPAddresses {
104-
err = ebtables.SetupDnatBasedOnIPV4Address(ipAddr.IP.String(), containerIf.HardwareAddr.String())
104+
err = ebtables.SetDnatForIPAddress(ipAddr.IP, containerIf.HardwareAddr, ebtables.Append)
105105
if err != nil {
106106
goto cleanup
107107
}
@@ -240,7 +240,7 @@ func (nw *network) deleteEndpoint(endpointId string) error {
240240
// Delete MAC address translation rule.
241241
log.Printf("[net] Deleting MAC address translation rules for endpoint %v.", endpointId)
242242
for _, ipAddr := range ep.IPAddresses {
243-
err = ebtables.RemoveDnatBasedOnIPV4Address(ipAddr.IP.String(), ep.MacAddress.String())
243+
err = ebtables.SetDnatForIPAddress(ipAddr.IP, ep.MacAddress, ebtables.Delete)
244244
if err != nil {
245245
goto cleanup
246246
}

network/network.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ func (nm *networkManager) connectExternalInterface(extIf *externalInterface, bri
172172

173173
// Setup MAC address translation rules for external interface.
174174
log.Printf("[net] Setting up MAC address translation rules for %v.", hostIf.Name)
175-
err = ebtables.SetupSnatForOutgoingPackets(hostIf.Name, hostIf.HardwareAddr.String())
175+
err = ebtables.SetSnatForInterface(hostIf.Name, hostIf.HardwareAddr, ebtables.Append)
176176
if err != nil {
177177
goto cleanup
178178
}
179179

180-
err = ebtables.SetupDnatForArpReplies(hostIf.Name)
180+
err = ebtables.SetDnatForArpReplies(hostIf.Name, ebtables.Append)
181181
if err != nil {
182182
goto cleanup
183183
}
@@ -240,8 +240,8 @@ cleanup:
240240
log.Printf("[net] Connecting interface %v failed, err:%v.", extIf.Name, err)
241241

242242
// Roll back the changes for the network.
243-
ebtables.CleanupDnatForArpReplies(extIf.Name)
244-
ebtables.CleanupSnatForOutgoingPackets(extIf.Name, extIf.MacAddress.String())
243+
ebtables.SetDnatForArpReplies(extIf.Name, ebtables.Delete)
244+
ebtables.SetSnatForInterface(extIf.Name, extIf.MacAddress, ebtables.Delete)
245245

246246
netlink.DeleteLink(bridgeName)
247247

@@ -253,8 +253,8 @@ func (nm *networkManager) disconnectExternalInterface(extIf *externalInterface)
253253
log.Printf("[net] Disconnecting interface %v.", extIf.Name)
254254

255255
// Cleanup MAC address translation rules.
256-
ebtables.CleanupDnatForArpReplies(extIf.Name)
257-
ebtables.CleanupSnatForOutgoingPackets(extIf.Name, extIf.MacAddress.String())
256+
ebtables.SetDnatForArpReplies(extIf.Name, ebtables.Delete)
257+
ebtables.SetSnatForInterface(extIf.Name, extIf.MacAddress, ebtables.Delete)
258258

259259
// Disconnect external interface from its bridge.
260260
err := netlink.SetLinkMaster(extIf.Name, "")

0 commit comments

Comments
 (0)