/
netconfig.go
191 lines (160 loc) · 3.92 KB
/
netconfig.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (2012) Sandia Corporation.
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
package main
import (
"errors"
"fmt"
"io"
log "minilog"
"net"
"strings"
"github.com/google/gopacket/macs"
)
var validMACPrefix [][3]byte
func init() {
for k, _ := range macs.ValidMACPrefixMap {
validMACPrefix = append(validMACPrefix, k)
}
}
// NetConfig contains all the network-related config for an interface. The IP
// addresses are automagically populated by snooping ARP traffic. The bandwidth
// stats and IP addresses are updated on-demand by calling the UpdateNetworks
// function of BaseConfig.
type NetConfig struct {
Alias string
VLAN int
Bridge string
Tap string
MAC string
Driver string
IP4 string
IP6 string
RxRate, TxRate float64 // Most recent bandwidth measurements for Tap
// Raw string that we used when creating this network config will be
// reparsed if we ever clone the VM that has this config.
Raw string
}
type NetConfigs []NetConfig
func NewVMConfig() VMConfig {
c := VMConfig{}
c.Clear(Wildcard)
return c
}
// ParseNetConfig processes the input specifying the bridge, VLAN alias, and
// mac for one interface to a VM and updates the vm config accordingly. The
// VLAN alias must be resolved using the active namespace. This takes a bit of
// parsing, because the entry can be in a few forms:
//
// vlan alias
//
// vlan alias,mac
// bridge,vlan alias
// vlan alias,driver
//
// bridge,vlan alias,mac
// vlan alias,mac,driver
// bridge,vlan alias,driver
//
// bridge,vlan alias,mac,driver
//
// If there are 2 or 3 fields, just the last field for the presence of a mac
func ParseNetConfig(spec string, nics map[string]bool) (*NetConfig, error) {
// example: my_bridge,100,00:00:00:00:00:00
f := strings.Split(spec, ",")
isDriver := func(d string) bool {
return nics[d]
}
var b, v, m, d string
switch len(f) {
case 1:
v = f[0]
case 2:
if isMAC(f[1]) {
// vlan, mac
v, m = f[0], f[1]
} else if isDriver(f[1]) {
// vlan, driver
v, d = f[0], f[1]
} else {
// bridge, vlan
b, v = f[0], f[1]
}
case 3:
if isMAC(f[2]) {
// bridge, vlan, mac
b, v, m = f[0], f[1], f[2]
} else if isMAC(f[1]) && isDriver(f[2]) {
// vlan, mac, driver
v, m, d = f[0], f[1], f[2]
} else if isDriver(f[2]) {
// bridge, vlan, driver
b, v, d = f[0], f[1], f[2]
} else {
return nil, errors.New("malformed netspec")
}
case 4:
if isMAC(f[2]) && isDriver(f[3]) {
b, v, m, d = f[0], f[1], f[2], f[3]
} else {
return nil, errors.New("malformed netspec")
}
default:
return nil, errors.New("malformed netspec")
}
log.Info(`got bridge="%v", alias="%v", mac="%v", driver="%v"`, b, v, m, d)
if b == "" {
b = DefaultBridge
}
if d == "" {
d = DefaultKVMDriver
}
return &NetConfig{
Alias: v,
Bridge: b,
MAC: strings.ToLower(m),
Driver: d,
}, nil
}
// String representation of NetConfig, should be able to parse back into a
// NetConfig.
func (c NetConfig) String() string {
parts := []string{}
if c.Bridge != "" && c.Bridge != DefaultBridge {
parts = append(parts, c.Bridge)
}
parts = append(parts, c.Alias)
if c.MAC != "" {
parts = append(parts, c.MAC)
}
if c.Driver != "" && c.Driver != DefaultKVMDriver {
parts = append(parts, c.Driver)
}
return strings.Join(parts, ",")
}
func (c NetConfigs) String() string {
parts := []string{}
for _, n := range c {
parts = append(parts, n.String())
}
return strings.Join(parts, " ")
}
func (c NetConfigs) WriteConfig(w io.Writer) error {
if len(c) > 0 {
_, err := fmt.Fprintf(w, "vm config networks %v\n", c)
return err
}
return nil
}
func isMAC(mac string) bool {
_, err := net.ParseMAC(mac)
return err == nil
}
func isAllocatedMAC(mac string) bool {
hw, err := net.ParseMAC(mac)
if err != nil {
return false
}
_, allocated := macs.ValidMACPrefixMap[[3]byte{hw[0], hw[1], hw[2]}]
return allocated
}