-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
portmap plugin should flush previous udp connections
conntrack does not have any way to track UDP connections, so it relies on timers to delete a connection. The problem is that UDP is connectionless, so a client will keep sending traffic despite the server has gone, thus renewing the conntrack entries. Pods that use portmaps to expose UDP services need to flush the existing conntrack entries on the port exposed when they are created, otherwise conntrack will keep sending the traffic to the previous IP until the connection age (the client stops sending traffic) Signed-off-by: Antonio Ojea <aojea@redhat.com>
- Loading branch information
Antonio Ojea
committed
Nov 18, 2020
1 parent
c41c78b
commit 3ae998e
Showing
3 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2020 CNI authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
"github.com/vishvananda/netlink" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// Assigned Internet Protocol Numbers | ||
// https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml | ||
const ( | ||
PROTOCOL_TCP = 6 | ||
PROTOCOL_UDP = 17 | ||
PROTOCOL_SCTP = 132 | ||
) | ||
|
||
// getNetlinkFamily returns the Netlink IP family constant | ||
func getNetlinkFamily(isIPv6 bool) netlink.InetFamily { | ||
if isIPv6 { | ||
return unix.AF_INET6 | ||
} | ||
return unix.AF_INET | ||
} | ||
|
||
// DeleteConntrackEntriesForDstIP delete the conntrack entries for the connections | ||
// specified by the given destination IP and protocol | ||
func DeleteConntrackEntriesForDstIP(dstIP string, protocol uint8) error { | ||
ip := net.ParseIP(dstIP) | ||
if ip == nil { | ||
return fmt.Errorf("error deleting connection tracking state, bad IP %s", ip) | ||
} | ||
family := getNetlinkFamily(ip.To4() == nil) | ||
|
||
filter := &netlink.ConntrackFilter{} | ||
filter.AddIP(netlink.ConntrackOrigDstIP, ip) | ||
filter.AddProtocol(protocol) | ||
|
||
n, err := netlink.ConntrackDeleteFilter(netlink.ConntrackTable, family, filter) | ||
if err != nil { | ||
// TODO: Better handling for deletion failure. When failure occur, stale udp connection may not get flushed. | ||
// These stale udp connection will keep black hole traffic. Making this a best effort operation for now. | ||
return fmt.Errorf("error deleting connection tracking state for protocol: %d IP: %s, error: %v", protocol, ip, err) | ||
} | ||
if n == 0 { | ||
log.Printf("no entries found for protocol: %d IP: %s", protocol, ip) | ||
} | ||
return nil | ||
} | ||
|
||
// DeleteConntrackEntriesForDstPort delete the conntrack entries for the connections specified | ||
// by the given destination port, protocol and IP family | ||
func DeleteConntrackEntriesForDstPort(port uint16, protocol uint8, family netlink.InetFamily) error { | ||
filter := &netlink.ConntrackFilter{} | ||
filter.AddPort(netlink.ConntrackOrigDstPort, port) | ||
filter.AddProtocol(protocol) | ||
|
||
n, err := netlink.ConntrackDeleteFilter(netlink.ConntrackTable, family, filter) | ||
if err != nil { | ||
// TODO: Better handling for deletion failure. When failure occur, stale udp connection may not get flushed. | ||
// These stale udp connection will keep black hole traffic. Making this a best effort operation for now. | ||
return fmt.Errorf("error deleting connection tracking state for protocol: %d Port: %d, error: %v", protocol, port, err) | ||
} | ||
if n == 0 { | ||
log.Printf("no entries found for protocol: %d Port: %d", protocol, port) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters