File: utils.go

package info (click to toggle)
docker.io 28.5.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 69,048 kB
  • sloc: sh: 5,867; makefile: 863; ansic: 184; python: 162; asm: 159
file content (64 lines) | stat: -rw-r--r-- 1,629 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Package overlayutils provides utility functions for overlay networks
package overlayutils

import (
	"fmt"
	"strconv"
	"strings"
	"sync"
)

var (
	mutex        sync.RWMutex
	vxlanUDPPort = defaultVXLANUDPPort
)

const defaultVXLANUDPPort uint32 = 4789

// ConfigVXLANUDPPort configures the VXLAN UDP port (data path port) number.
// If no port is set, the default (4789) is returned. Valid port numbers are
// between 1024 and 49151.
func ConfigVXLANUDPPort(vxlanPort uint32) error {
	if vxlanPort == 0 {
		vxlanPort = defaultVXLANUDPPort
	}
	// IANA procedures for each range in detail
	// The Well Known Ports, aka the System Ports, from 0-1023
	// The Registered Ports, aka the User Ports, from 1024-49151
	// The Dynamic Ports, aka the Private Ports, from 49152-65535
	// So we can allow range between 1024 to 49151
	if vxlanPort < 1024 || vxlanPort > 49151 {
		return fmt.Errorf("VXLAN UDP port number is not in valid range (1024-49151): %d", vxlanPort)
	}
	mutex.Lock()
	vxlanUDPPort = vxlanPort
	mutex.Unlock()
	return nil
}

// VXLANUDPPort returns Vxlan UDP port number
func VXLANUDPPort() uint32 {
	mutex.RLock()
	defer mutex.RUnlock()
	return vxlanUDPPort
}

// AppendVNIList appends the VNI values encoded as a CSV string to slice.
func AppendVNIList(vnis []uint32, csv string) ([]uint32, error) {
	for {
		var (
			vniStr string
			found  bool
		)
		vniStr, csv, found = strings.Cut(csv, ",")
		vni, err := strconv.ParseUint(vniStr, 10, 32)
		if err != nil {
			return vnis, fmt.Errorf("invalid vxlan id value %q passed", vniStr)
		}

		vnis = append(vnis, uint32(vni))
		if !found {
			return vnis, nil
		}
	}
}