conntrack.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package conntrack
  14. import (
  15. "fmt"
  16. "strconv"
  17. "strings"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/utils/exec"
  20. utilnet "k8s.io/utils/net"
  21. )
  22. // Utilities for dealing with conntrack
  23. // NoConnectionToDelete is the error string returned by conntrack when no matching connections are found
  24. const NoConnectionToDelete = "0 flow entries have been deleted"
  25. func protoStr(proto v1.Protocol) string {
  26. return strings.ToLower(string(proto))
  27. }
  28. func parametersWithFamily(isIPv6 bool, parameters ...string) []string {
  29. if isIPv6 {
  30. parameters = append(parameters, "-f", "ipv6")
  31. }
  32. return parameters
  33. }
  34. // ClearEntriesForIP uses the conntrack tool to delete the conntrack entries
  35. // for the UDP connections specified by the given service IP
  36. func ClearEntriesForIP(execer exec.Interface, ip string, protocol v1.Protocol) error {
  37. parameters := parametersWithFamily(utilnet.IsIPv6String(ip), "-D", "--orig-dst", ip, "-p", protoStr(protocol))
  38. err := Exec(execer, parameters...)
  39. if err != nil && !strings.Contains(err.Error(), NoConnectionToDelete) {
  40. // TODO: Better handling for deletion failure. When failure occur, stale udp connection may not get flushed.
  41. // These stale udp connection will keep black hole traffic. Making this a best effort operation for now, since it
  42. // is expensive to baby-sit all udp connections to kubernetes services.
  43. return fmt.Errorf("error deleting connection tracking state for UDP service IP: %s, error: %v", ip, err)
  44. }
  45. return nil
  46. }
  47. // Exec executes the conntrack tool using the given parameters
  48. func Exec(execer exec.Interface, parameters ...string) error {
  49. conntrackPath, err := execer.LookPath("conntrack")
  50. if err != nil {
  51. return fmt.Errorf("error looking for path of conntrack: %v", err)
  52. }
  53. output, err := execer.Command(conntrackPath, parameters...).CombinedOutput()
  54. if err != nil {
  55. return fmt.Errorf("conntrack command returned: %q, error message: %s", string(output), err)
  56. }
  57. return nil
  58. }
  59. // Exists returns true if conntrack binary is installed.
  60. func Exists(execer exec.Interface) bool {
  61. _, err := execer.LookPath("conntrack")
  62. return err == nil
  63. }
  64. // ClearEntriesForPort uses the conntrack tool to delete the conntrack entries
  65. // for connections specified by the port.
  66. // When a packet arrives, it will not go through NAT table again, because it is not "the first" packet.
  67. // The solution is clearing the conntrack. Known issues:
  68. // https://github.com/docker/docker/issues/8795
  69. // https://github.com/kubernetes/kubernetes/issues/31983
  70. func ClearEntriesForPort(execer exec.Interface, port int, isIPv6 bool, protocol v1.Protocol) error {
  71. if port <= 0 {
  72. return fmt.Errorf("Wrong port number. The port number must be greater than zero")
  73. }
  74. parameters := parametersWithFamily(isIPv6, "-D", "-p", protoStr(protocol), "--dport", strconv.Itoa(port))
  75. err := Exec(execer, parameters...)
  76. if err != nil && !strings.Contains(err.Error(), NoConnectionToDelete) {
  77. return fmt.Errorf("error deleting conntrack entries for UDP port: %d, error: %v", port, err)
  78. }
  79. return nil
  80. }
  81. // ClearEntriesForNAT uses the conntrack tool to delete the conntrack entries
  82. // for connections specified by the {origin, dest} IP pair.
  83. func ClearEntriesForNAT(execer exec.Interface, origin, dest string, protocol v1.Protocol) error {
  84. parameters := parametersWithFamily(utilnet.IsIPv6String(origin), "-D", "--orig-dst", origin, "--dst-nat", dest,
  85. "-p", protoStr(protocol))
  86. err := Exec(execer, parameters...)
  87. if err != nil && !strings.Contains(err.Error(), NoConnectionToDelete) {
  88. // TODO: Better handling for deletion failure. When failure occur, stale udp connection may not get flushed.
  89. // These stale udp connection will keep black hole traffic. Making this a best effort operation for now, since it
  90. // is expensive to baby sit all udp connections to kubernetes services.
  91. return fmt.Errorf("error deleting conntrack entries for UDP peer {%s, %s}, error: %v", origin, dest, err)
  92. }
  93. return nil
  94. }
  95. // ClearEntriesForPortNAT uses the conntrack tool to delete the contrack entries
  96. // for connections specified by the {dest IP, port} pair.
  97. // Known issue:
  98. // https://github.com/kubernetes/kubernetes/issues/59368
  99. func ClearEntriesForPortNAT(execer exec.Interface, dest string, port int, protocol v1.Protocol) error {
  100. if port <= 0 {
  101. return fmt.Errorf("Wrong port number. The port number must be greater then zero")
  102. }
  103. parameters := parametersWithFamily(utilnet.IsIPv6String(dest), "-D", "-p", protoStr(protocol), "--dport", strconv.Itoa(port), "--dst-nat", dest)
  104. err := Exec(execer, parameters...)
  105. if err != nil && !strings.Contains(err.Error(), NoConnectionToDelete) {
  106. return fmt.Errorf("error deleting conntrack entries for UDP port: %d, error: %v", port, err)
  107. }
  108. return nil
  109. }