utils.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Package overlayutils provides utility functions for overlay networks
  2. package overlayutils
  3. import (
  4. "fmt"
  5. "sync"
  6. )
  7. var (
  8. mutex sync.RWMutex
  9. vxlanUDPPort = defaultVXLANUDPPort
  10. )
  11. const defaultVXLANUDPPort uint32 = 4789
  12. // ConfigVXLANUDPPort configures the VXLAN UDP port (data path port) number.
  13. // If no port is set, the default (4789) is returned. Valid port numbers are
  14. // between 1024 and 49151.
  15. func ConfigVXLANUDPPort(vxlanPort uint32) error {
  16. if vxlanPort == 0 {
  17. vxlanPort = defaultVXLANUDPPort
  18. }
  19. // IANA procedures for each range in detail
  20. // The Well Known Ports, aka the System Ports, from 0-1023
  21. // The Registered Ports, aka the User Ports, from 1024-49151
  22. // The Dynamic Ports, aka the Private Ports, from 49152-65535
  23. // So we can allow range between 1024 to 49151
  24. if vxlanPort < 1024 || vxlanPort > 49151 {
  25. return fmt.Errorf("VXLAN UDP port number is not in valid range (1024-49151): %d", vxlanPort)
  26. }
  27. mutex.Lock()
  28. vxlanUDPPort = vxlanPort
  29. mutex.Unlock()
  30. return nil
  31. }
  32. // VXLANUDPPort returns Vxlan UDP port number
  33. func VXLANUDPPort() uint32 {
  34. mutex.RLock()
  35. defer mutex.RUnlock()
  36. return vxlanUDPPort
  37. }