-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
134 lines (122 loc) · 3.01 KB
/
net.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package mdocker
import (
"fmt"
"os/exec"
"strconv"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/mistifyio/mistify-agent/client"
)
func getPortForContainerInterface(guestID, ifaceName string) (string, error) {
command := "ovs-vsctl"
args := []string{
"--data=bare",
"--no-heading",
"--columns=name",
"find",
"interface",
"external_ids:container_id=" + guestID,
"external_ids:container_iface=" + ifaceName,
}
output, err := exec.Command(command, args...).CombinedOutput()
if err != nil {
e := fmt.Errorf("failed to look up name of interface %s for guest %s",
ifaceName,
guestID,
)
log.WithFields(log.Fields{
"error": err,
"command": command,
"args": args,
"output": string(output),
}).Error(e)
return "", e
}
return strings.TrimSpace(string(output)), nil
}
func addPort(g *client.Guest, nic client.Nic) (string, error) {
command := "ovs-docker"
args := []string{"add-port",
nic.Network,
nic.Name,
g.ID,
"--macaddress=" + nic.Mac, // ovs-docker errors if separate
}
if output, err := exec.Command(command, args...).CombinedOutput(); err != nil {
e := fmt.Errorf("failed to add interface %s", nic.Name)
log.WithFields(log.Fields{
"error": err,
"command": command,
"args": args,
"output": string(output),
}).Error(e)
return "", e
}
return getPortForContainerInterface(g.ID, nic.Name)
}
func tagPort(port string, vlanInts []int) error {
command := "ovs-vsctl"
if len(vlanInts) == 0 {
return nil
}
vlans := make([]string, len(vlanInts), len(vlanInts))
for i := 0; i < len(vlanInts); i++ {
vlans[i] = strconv.Itoa(vlanInts[i])
}
args := []string{
"set",
"port",
port,
"trunks=" + strings.Join(vlans, ","),
}
if output, err := exec.Command(command, args...).CombinedOutput(); err != nil {
e := fmt.Errorf("failed to tag interface %s", port)
log.WithFields(log.Fields{
"error": err,
"command": command,
"args": args,
"output": string(output),
}).Error(e)
return e
}
return nil
}
// addInterfaces adds network interfaces to a guest container
func addInterfaces(g *client.Guest) error {
for _, nic := range g.Nics {
port, err := addPort(g, nic)
if err != nil {
return err
}
if err := tagPort(port, nic.VLANs); err != nil {
return err
}
}
return nil
}
// removeInterfaces removes network interfaces from a guest container
func removeInterfaces(g *client.Guest) error {
command := "ovs-docker"
for _, nic := range g.Nics {
args := []string{
"del-port",
nic.Network,
nic.Name,
g.ID,
}
if output, err := exec.Command(command, args...).CombinedOutput(); err != nil {
// Ignore errors when trying to remove interface that is already gone
if !strings.Contains(strings.ToLower(string(output)), "failed to find any attached port") {
e := fmt.Errorf("failed to remove interface %s", nic.Name)
log.WithFields(log.Fields{
"error": err,
"command": command,
"args": args,
"output": string(output),
}).Error(e)
return e
}
}
}
return nil
}