utils.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Package overlayutils provides utility functions for overlay networks
  2. package overlayutils
  3. import (
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. )
  9. var (
  10. mutex sync.RWMutex
  11. vxlanUDPPort = defaultVXLANUDPPort
  12. )
  13. const defaultVXLANUDPPort uint32 = 4789
  14. // ConfigVXLANUDPPort configures the VXLAN UDP port (data path port) number.
  15. // If no port is set, the default (4789) is returned. Valid port numbers are
  16. // between 1024 and 49151.
  17. func ConfigVXLANUDPPort(vxlanPort uint32) error {
  18. if vxlanPort == 0 {
  19. vxlanPort = defaultVXLANUDPPort
  20. }
  21. // IANA procedures for each range in detail
  22. // The Well Known Ports, aka the System Ports, from 0-1023
  23. // The Registered Ports, aka the User Ports, from 1024-49151
  24. // The Dynamic Ports, aka the Private Ports, from 49152-65535
  25. // So we can allow range between 1024 to 49151
  26. if vxlanPort < 1024 || vxlanPort > 49151 {
  27. return fmt.Errorf("VXLAN UDP port number is not in valid range (1024-49151): %d", vxlanPort)
  28. }
  29. mutex.Lock()
  30. vxlanUDPPort = vxlanPort
  31. mutex.Unlock()
  32. return nil
  33. }
  34. // VXLANUDPPort returns Vxlan UDP port number
  35. func VXLANUDPPort() uint32 {
  36. mutex.RLock()
  37. defer mutex.RUnlock()
  38. return vxlanUDPPort
  39. }
  40. // AppendVNIList appends the VNI values encoded as a CSV string to slice.
  41. func AppendVNIList(vnis []uint32, csv string) ([]uint32, error) {
  42. for {
  43. var (
  44. vniStr string
  45. found bool
  46. )
  47. vniStr, csv, found = strings.Cut(csv, ",")
  48. vni, err := strconv.Atoi(vniStr)
  49. if err != nil {
  50. return vnis, fmt.Errorf("invalid vxlan id value %q passed", vniStr)
  51. }
  52. vnis = append(vnis, uint32(vni))
  53. if !found {
  54. return vnis, nil
  55. }
  56. }
  57. }