summaryrefslogtreecommitdiff
path: root/libpod/network/devices.go
blob: fc9aff337d188790bb684e3acb2025103f4d03fa (plain)
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
package network

import (
	"fmt"

	"github.com/containers/common/pkg/config"
	"github.com/containers/podman/v3/pkg/util"
	"github.com/sirupsen/logrus"
	"github.com/vishvananda/netlink"
)

// GetFreeDeviceName returns a device name that is unused; used when no network
// name is provided by user
func GetFreeDeviceName(config *config.Config) (string, error) {
	var (
		deviceNum  uint
		deviceName string
	)
	networkNames, err := GetNetworkNamesFromFileSystem(config)
	if err != nil {
		return "", err
	}
	liveNetworksNames, err := GetLiveNetworkNames()
	if err != nil {
		return "", err
	}
	bridgeNames, err := GetBridgeNamesFromFileSystem(config)
	if err != nil {
		return "", err
	}
	for {
		deviceName = fmt.Sprintf("%s%d", CNIDeviceName, deviceNum)
		logrus.Debugf("checking if device name %q exists in other cni networks", deviceName)
		if util.StringInSlice(deviceName, networkNames) {
			deviceNum++
			continue
		}
		logrus.Debugf("checking if device name %q exists in live networks", deviceName)
		if util.StringInSlice(deviceName, liveNetworksNames) {
			deviceNum++
			continue
		}
		logrus.Debugf("checking if device name %q already exists as a bridge name ", deviceName)
		if !util.StringInSlice(deviceName, bridgeNames) {
			break
		}
		deviceNum++
	}
	return deviceName, nil
}

// RemoveInterface removes an interface by the given name
func RemoveInterface(interfaceName string) error {
	link, err := netlink.LinkByName(interfaceName)
	if err != nil {
		return err
	}
	return netlink.LinkDel(link)
}