From 66141b879f05a9a159be8943ed83c050a3ee6024 Mon Sep 17 00:00:00 2001 From: Jana Radhakrishnan Date: Mon, 21 Dec 2015 14:51:39 -0800 Subject: [PATCH] Cleanup vxlan interface by id before creating Currently we are cleaning up vxlan interfaces by name before trying to setup an interface with the same name. But this doesn't work for properly cleaning up vxlan interfaces with the same vni, if the interface has a a different name than the one expected. The fix is to delete the interface based on vni. Signed-off-by: Jana Radhakrishnan --- libnetwork/drivers/overlay/ov_network.go | 4 ++-- libnetwork/drivers/overlay/ov_utils.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/libnetwork/drivers/overlay/ov_network.go b/libnetwork/drivers/overlay/ov_network.go index a1cf5f1f19..6e558c9a3c 100644 --- a/libnetwork/drivers/overlay/ov_network.go +++ b/libnetwork/drivers/overlay/ov_network.go @@ -267,8 +267,8 @@ func (n *network) initSubnetSandbox(s *subnet) error { vxlanName := n.generateVxlanName(s) - // Try to delete the vxlan interface if already present - deleteVxlan(vxlanName) + // Try to delete the vxlan interface by vni if already present + deleteVxlanByVNI(n.vxlanID(s)) err := createVxlan(vxlanName, n.vxlanID(s)) if err != nil { diff --git a/libnetwork/drivers/overlay/ov_utils.go b/libnetwork/drivers/overlay/ov_utils.go index 0b561e7a75..211879c447 100644 --- a/libnetwork/drivers/overlay/ov_utils.go +++ b/libnetwork/drivers/overlay/ov_utils.go @@ -81,3 +81,25 @@ func deleteVxlan(name string) error { return nil } + +func deleteVxlanByVNI(vni uint32) error { + defer osl.InitOSContext()() + + links, err := netlink.LinkList() + if err != nil { + return fmt.Errorf("failed to list interfaces while deleting vxlan interface by vni: %v", err) + } + + for _, l := range links { + if l.Type() == "vxlan" && l.(*netlink.Vxlan).VxlanId == int(vni) { + err = netlink.LinkDel(l) + if err != nil { + return fmt.Errorf("error deleting vxlan interface with id %d: %v", vni, err) + } + + return nil + } + } + + return fmt.Errorf("could not find a vxlan interface to delete with id %d", vni) +}