conntrack.go 2.7 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 app
  14. import (
  15. "errors"
  16. "io/ioutil"
  17. "strconv"
  18. "github.com/golang/glog"
  19. "k8s.io/kubernetes/pkg/util/mount"
  20. "k8s.io/kubernetes/pkg/util/sysctl"
  21. )
  22. type Conntracker interface {
  23. SetMax(max int) error
  24. SetTCPEstablishedTimeout(seconds int) error
  25. }
  26. type realConntracker struct{}
  27. var readOnlySysFSError = errors.New("ReadOnlySysFS")
  28. func (realConntracker) SetMax(max int) error {
  29. glog.Infof("Setting nf_conntrack_max to %d", max)
  30. if err := sysctl.New().SetSysctl("net/netfilter/nf_conntrack_max", max); err != nil {
  31. return err
  32. }
  33. // sysfs is expected to be mounted as 'rw'. However, it may be unexpectedly mounted as
  34. // 'ro' by docker because of a known docker issue (https://github.com/docker/docker/issues/24000).
  35. // Setting conntrack will fail when sysfs is readonly. When that happens, we don't set conntrack
  36. // hashsize and return a special error readOnlySysFSError here. The caller should deal with
  37. // readOnlySysFSError differently.
  38. writable, err := isSysFSWritable()
  39. if err != nil {
  40. return err
  41. }
  42. if !writable {
  43. return readOnlySysFSError
  44. }
  45. // TODO: generify this and sysctl to a new sysfs.WriteInt()
  46. glog.Infof("Setting conntrack hashsize to %d", max/4)
  47. return ioutil.WriteFile("/sys/module/nf_conntrack/parameters/hashsize", []byte(strconv.Itoa(max/4)), 0640)
  48. }
  49. func (realConntracker) SetTCPEstablishedTimeout(seconds int) error {
  50. glog.Infof("Setting nf_conntrack_tcp_timeout_established to %d", seconds)
  51. return sysctl.New().SetSysctl("net/netfilter/nf_conntrack_tcp_timeout_established", seconds)
  52. }
  53. // isSysFSWritable checks /proc/mounts to see whether sysfs is 'rw' or not.
  54. func isSysFSWritable() (bool, error) {
  55. const permWritable = "rw"
  56. const sysfsDevice = "sysfs"
  57. m := mount.New()
  58. mountPoints, err := m.List()
  59. if err != nil {
  60. glog.Errorf("failed to list mount points: %v", err)
  61. return false, err
  62. }
  63. for _, mountPoint := range mountPoints {
  64. if mountPoint.Device != sysfsDevice {
  65. continue
  66. }
  67. // Check whether sysfs is 'rw'
  68. if len(mountPoint.Opts) > 0 && mountPoint.Opts[0] == permWritable {
  69. return true, nil
  70. }
  71. glog.Errorf("sysfs is not writable: %+v", mountPoint)
  72. break
  73. }
  74. return false, nil
  75. }