github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/libnetwork/drivers/overlay/overlayutils/utils.go (about)

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