Skip to content

Commit 062b3fc

Browse files
Marcelo Guerrerosqueed
authored andcommitted
Enable KeepAddrOnDown for ipv6 addresses
This enables the keep_addr_on_down sysctl parameter for IPV6 addresses configured via the ConfigureIface function. This prevents IPAM confiuration to be lost when users need to refresh the link state of an interface that has IPV6 addresses. Signed-off-by: Marcelo Guerrero <marguerr@redhat.com>
1 parent b088cc3 commit 062b3fc

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

pkg/ipam/ipam_linux.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import (
2929

3030
const (
3131
// Note: use slash as separator so we can have dots in interface name (VLANs)
32-
DisableIPv6SysctlTemplate = "net/ipv6/conf/%s/disable_ipv6"
32+
DisableIPv6SysctlTemplate = "net/ipv6/conf/%s/disable_ipv6"
33+
KeepAddrOnDownSysctlTemplate = "net/ipv6/conf/%s/keep_addr_on_down"
3334
)
3435

3536
// ConfigureIface takes the result of IPAM plugin and
@@ -56,8 +57,8 @@ func ConfigureIface(ifName string, res *current.Result) error {
5657
return fmt.Errorf("failed to add IP addr %v to %q: invalid interface index", ipc, ifName)
5758
}
5859

59-
// Make sure sysctl "disable_ipv6" is 0 if we are about to add
60-
// an IPv6 address to the interface
60+
// Make sure sysctl "disable_ipv6" is 0 and "keep_addr_on_down" is 1
61+
// if we are about to add an IPv6 address to the interface
6162
if !hasEnabledIpv6 && ipc.Address.IP.To4() == nil {
6263
// Enabled IPv6 for loopback "lo" and the interface
6364
// being configured
@@ -80,6 +81,15 @@ func ConfigureIface(ifName string, res *current.Result) error {
8081
return fmt.Errorf("failed to enable IPv6 for interface %q (%s=%s): %v", iface, ipv6SysctlValueName, value, err)
8182
}
8283
}
84+
85+
// Enable "keep_addr_on_down" for the interface being configured
86+
// This prevents the kernel from removing the address when the interface is brought down
87+
keepAddrOnDownSysctlValueName := fmt.Sprintf(KeepAddrOnDownSysctlTemplate, ifName)
88+
_, err = sysctl.Sysctl(keepAddrOnDownSysctlValueName, "1")
89+
if err != nil {
90+
return fmt.Errorf("failed to enable keep_addr_on_down for interface %q: %v", ifName, err)
91+
}
92+
8393
hasEnabledIpv6 = true
8494
}
8595

pkg/ipam/ipam_linux_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,49 @@ var _ = Describe("ConfigureIface", func() {
201201
Expect(err).NotTo(HaveOccurred())
202202
})
203203

204+
It("keeps IPV6 addresses after the interface is brought down", func() {
205+
err := originalNS.Do(func(ns.NetNS) error {
206+
defer GinkgoRecover()
207+
208+
By("Configuring the interface")
209+
210+
err := ConfigureIface(LINK_NAME, result)
211+
Expect(err).NotTo(HaveOccurred())
212+
213+
By("Verifying the IPV6 address is present")
214+
215+
link, err := netlinksafe.LinkByName(LINK_NAME)
216+
Expect(err).NotTo(HaveOccurred())
217+
Expect(link.Attrs().Name).To(Equal(LINK_NAME))
218+
219+
v6addrs, err := netlinksafe.AddrList(link, syscall.AF_INET6)
220+
Expect(err).NotTo(HaveOccurred())
221+
Expect(v6addrs).To(HaveLen(2))
222+
223+
var found bool
224+
for _, a := range v6addrs {
225+
if ipNetEqual(a.IPNet, ipv6) {
226+
found = true
227+
break
228+
}
229+
}
230+
Expect(found).To(BeTrue())
231+
232+
By("Bringing the interface down")
233+
err = netlink.LinkSetDown(link)
234+
Expect(err).NotTo(HaveOccurred())
235+
236+
By("Verifying the IPV6 address is still present")
237+
v6addrs, err = netlinksafe.AddrList(link, syscall.AF_INET6)
238+
Expect(err).NotTo(HaveOccurred())
239+
Expect(v6addrs).To(HaveLen(1))
240+
Expect(ipNetEqual(v6addrs[0].IPNet, ipv6)).To(BeTrue())
241+
242+
return nil
243+
})
244+
Expect(err).NotTo(HaveOccurred())
245+
})
246+
204247
It("configures a link with routes using address gateways", func() {
205248
result.Routes[0].GW = nil
206249
result.Routes[1].GW = nil

0 commit comments

Comments
 (0)