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