hairpin.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2015 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 hairpin
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net"
  18. "os"
  19. "path"
  20. "regexp"
  21. "strconv"
  22. "k8s.io/klog"
  23. "k8s.io/utils/exec"
  24. )
  25. const (
  26. sysfsNetPath = "/sys/devices/virtual/net"
  27. brportRelativePath = "brport"
  28. hairpinModeRelativePath = "hairpin_mode"
  29. hairpinEnable = "1"
  30. )
  31. var (
  32. ethtoolOutputRegex = regexp.MustCompile(`peer_ifindex: (\d+)`)
  33. )
  34. func findPairInterfaceOfContainerInterface(e exec.Interface, containerInterfaceName, containerDesc string, nsenterArgs []string) (string, error) {
  35. nsenterPath, err := e.LookPath("nsenter")
  36. if err != nil {
  37. return "", err
  38. }
  39. ethtoolPath, err := e.LookPath("ethtool")
  40. if err != nil {
  41. return "", err
  42. }
  43. nsenterArgs = append(nsenterArgs, "-F", "--", ethtoolPath, "--statistics", containerInterfaceName)
  44. output, err := e.Command(nsenterPath, nsenterArgs...).CombinedOutput()
  45. if err != nil {
  46. return "", fmt.Errorf("unable to query interface %s of container %s: %v: %s", containerInterfaceName, containerDesc, err, string(output))
  47. }
  48. // look for peer_ifindex
  49. match := ethtoolOutputRegex.FindSubmatch(output)
  50. if match == nil {
  51. return "", fmt.Errorf("no peer_ifindex in interface statistics for %s of container %s", containerInterfaceName, containerDesc)
  52. }
  53. peerIfIndex, err := strconv.Atoi(string(match[1]))
  54. if err != nil { // seems impossible (\d+ not numeric)
  55. return "", fmt.Errorf("peer_ifindex wasn't numeric: %s: %v", match[1], err)
  56. }
  57. iface, err := net.InterfaceByIndex(peerIfIndex)
  58. if err != nil {
  59. return "", err
  60. }
  61. return iface.Name, nil
  62. }
  63. func setUpInterface(ifName string) error {
  64. klog.V(3).Infof("Enabling hairpin on interface %s", ifName)
  65. ifPath := path.Join(sysfsNetPath, ifName)
  66. if _, err := os.Stat(ifPath); err != nil {
  67. return err
  68. }
  69. brportPath := path.Join(ifPath, brportRelativePath)
  70. if _, err := os.Stat(brportPath); err != nil && os.IsNotExist(err) {
  71. // Device is not on a bridge, so doesn't need hairpin mode
  72. return nil
  73. }
  74. hairpinModeFile := path.Join(brportPath, hairpinModeRelativePath)
  75. return ioutil.WriteFile(hairpinModeFile, []byte(hairpinEnable), 0644)
  76. }