utils.go 1.1 KB

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