-
Notifications
You must be signed in to change notification settings - Fork 95
/
vswitch.go
319 lines (263 loc) · 9.77 KB
/
vswitch.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2017 DigitalOcean.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ovs
import (
"encoding/json"
"fmt"
"strings"
)
const (
// DefaultIngressRatePolicing is used to disable the ingress policing,
// which is the default behavior.
DefaultIngressRatePolicing = int64(-1)
// DefaultIngressBurstPolicing is to change the ingress policing
// burst to the default size, 1000 kb.
DefaultIngressBurstPolicing = int64(-1)
)
// A VSwitchService is used in a Client to execute 'ovs-vsctl' commands.
type VSwitchService struct {
// Get wraps functionality of the 'ovs-vsctl get' subcommand.
Get *VSwitchGetService
// Set wraps functionality of the 'ovs-vsctl set' subcommand.
Set *VSwitchSetService
// Wrapped Client for ExecFunc and debugging.
c *Client
}
// AddBridge attaches a bridge to Open vSwitch. The bridge may or may
// not already exist.
func (v *VSwitchService) AddBridge(bridge string) error {
_, err := v.exec("--may-exist", "add-br", bridge)
return err
}
// AddPort attaches a port to a bridge on Open vSwitch. The port may or may
// not already exist.
func (v *VSwitchService) AddPort(bridge string, port string) error {
_, err := v.exec("--may-exist", "add-port", bridge, string(port))
return err
}
// DeleteBridge detaches a bridge from Open vSwitch. The bridge may or may
// not already exist.
func (v *VSwitchService) DeleteBridge(bridge string) error {
_, err := v.exec("--if-exists", "del-br", bridge)
return err
}
// DeletePort detaches a port from a bridge on Open vSwitch. The port may or may
// not already exist.
func (v *VSwitchService) DeletePort(bridge string, port string) error {
_, err := v.exec("--if-exists", "del-port", bridge, string(port))
return err
}
// ListPorts lists the ports in Open vSwitch.
func (v *VSwitchService) ListPorts(bridge string) ([]string, error) {
output, err := v.exec("list-ports", bridge)
if err != nil {
return nil, err
}
// Do no ports exist?
if len(output) == 0 {
return nil, nil
}
ports := strings.Split(strings.TrimSpace(string(output)), "\n")
return ports, nil
}
// ListBridges lists the bridges in Open vSwitch.
func (v *VSwitchService) ListBridges() ([]string, error) {
output, err := v.exec("list-br")
if err != nil {
return nil, err
}
// Do no bridges exist?
if len(output) == 0 {
return nil, nil
}
bridges := strings.Split(strings.TrimSpace(string(output)), "\n")
return bridges, nil
}
// PortToBridge attempts to determine which bridge a port is attached to.
// If port does not exist, an error will be returned, which can be checked
// using IsPortNotExist.
func (v *VSwitchService) PortToBridge(port string) (string, error) {
out, err := v.exec("port-to-br", string(port))
if err != nil {
return "", err
}
return string(out), nil
}
// GetFailMode gets the FailMode for the specified bridge.
func (v *VSwitchService) GetFailMode(bridge string) (FailMode, error) {
out, err := v.exec("get-fail-mode", bridge)
if err != nil {
return "", err
}
return FailMode(out), nil
}
// SetFailMode sets the specified FailMode for the specified bridge.
func (v *VSwitchService) SetFailMode(bridge string, mode FailMode) error {
_, err := v.exec("set-fail-mode", bridge, string(mode))
return err
}
// SetController sets the controller for this bridge so that ovs-ofctl
// can use this address to communicate.
func (v *VSwitchService) SetController(bridge string, address string) error {
_, err := v.exec("set-controller", bridge, address)
return err
}
// GetController gets the controller address for this bridge.
func (v *VSwitchService) GetController(bridge string) (string, error) {
address, err := v.exec("get-controller", bridge)
if err != nil {
return "", err
}
return strings.TrimSpace(string(address)), nil
}
// exec executes an ExecFunc using 'ovs-vsctl'.
func (v *VSwitchService) exec(args ...string) ([]byte, error) {
return v.c.exec("ovs-vsctl", args...)
}
// A VSwitchGetService is used in a VSwitchService to execute 'ovs-vsctl get'
// subcommands.
type VSwitchGetService struct {
// v provides the required exec method.
v *VSwitchService
}
// Bridge gets configuration for a bridge and returns the values through
// a BridgeOptions struct.
func (v *VSwitchGetService) Bridge(bridge string) (BridgeOptions, error) {
// We only support the protocol option at this point.
args := []string{"--format=json", "get", "bridge", bridge, "protocols"}
out, err := v.v.exec(args...)
if err != nil {
return BridgeOptions{}, err
}
var protocols []string
if err := json.Unmarshal(out, &protocols); err != nil {
return BridgeOptions{}, err
}
return BridgeOptions{
Protocols: protocols,
}, nil
}
// A VSwitchSetService is used in a VSwitchService to execute 'ovs-vsctl set'
// subcommands.
type VSwitchSetService struct {
// v provides the required exec method.
v *VSwitchService
}
// Bridge sets configuration for a bridge using the values from a BridgeOptions
// struct.
func (v *VSwitchSetService) Bridge(bridge string, options BridgeOptions) error {
// Prepend command line arguments before expanding options slice
// and appending it
args := []string{"set", "bridge", bridge}
args = append(args, options.slice()...)
_, err := v.v.exec(args...)
return err
}
// An BridgeOptions enables configuration of a bridge.
type BridgeOptions struct {
// Protocols specifies the OpenFlow protocols the bridge should use.
Protocols []string
}
// slice creates a string slice containing any non-zero option values from the
// struct in the format expected by Open vSwitch.
func (o BridgeOptions) slice() []string {
var s []string
if len(o.Protocols) > 0 {
s = append(s, fmt.Sprintf("protocols=%s", strings.Join(o.Protocols, ",")))
}
return s
}
// Interface sets configuration for an interface using the values from an
// InterfaceOptions struct.
func (v *VSwitchSetService) Interface(ifi string, options InterfaceOptions) error {
// Prepend command line arguments before expanding options slice
// and appending it
args := []string{"set", "interface", ifi}
args = append(args, options.slice()...)
_, err := v.v.exec(args...)
return err
}
// An InterfaceOptions struct enables configuration of an Interface.
type InterfaceOptions struct {
// Type specifies the Open vSwitch interface type.
Type InterfaceType
// Peer specifies an interface to peer with when creating a patch interface.
Peer string
// MTURequest specifies the maximum transmission unit associated with an
// interface.
MTURequest int
// Ingress Policing
//
// These settings control ingress policing for packets received on this
// interface. On a physical interface, this limits the rate at which
// traffic is allowed into the system from the outside; on a virtual
// interface (one connected to a virtual machine), this limits the rate
// at which the VM is able to transmit.
// IngressRatePolicing specifies the maximum rate for data received on
// this interface in kbps. Data received faster than this rate is dropped.
// Set to 0 (the default) to disable policing.
IngressRatePolicing int64
// IngressBurstPolicing specifies the maximum burst size for data received on
// this interface in kb. The default burst size if set to 0 is 1000 kb.
// This value has no effect if IngressRatePolicing is set to 0. Specifying
// a larger burst size lets the algorithm be more forgiving, which is important
// for protocols like TCP that react severely to dropped packets. The burst
// size should be at least the size of the interface's MTU. Specifying a
// value that is numerically at least as large as 10% of IngressRatePolicing
// helps TCP come closer to achieving the full rate.
IngressBurstPolicing int64
// RemoteIP can be populated when the interface is a tunnel interface type
// for example "stt" or "vxlan". It specifies the remote IP address with which to
// form tunnels when traffic is sent to this port. Optionally it could be set to
// "flow" which expects the flow to set tunnel destination.
RemoteIP string
// Key can be populated when the interface is a tunnel interface type
// for example "stt" or "vxlan". It specifies the tunnel ID to attach to
// tunneled traffic leaving this interface. Optionally it could be set to
// "flow" which expects the flow to set tunnel ID.
Key string
}
// slice creates a string slice containing any non-zero option values from the
// struct in the format expected by Open vSwitch.
func (i InterfaceOptions) slice() []string {
var s []string
if i.Type != "" {
s = append(s, fmt.Sprintf("type=%s", i.Type))
}
if i.Peer != "" {
s = append(s, fmt.Sprintf("options:peer=%s", i.Peer))
}
if i.MTURequest > 0 {
s = append(s, fmt.Sprintf("mtu_request=%d", i.MTURequest))
}
if i.IngressRatePolicing == DefaultIngressRatePolicing {
// Set to 0 (the default) to disable policing.
s = append(s, "ingress_policing_rate=0")
} else if i.IngressRatePolicing > 0 {
s = append(s, fmt.Sprintf("ingress_policing_rate=%d", i.IngressRatePolicing))
}
if i.IngressBurstPolicing == DefaultIngressBurstPolicing {
// Set to 0 (the default) to the default burst size.
s = append(s, "ingress_policing_burst=0")
} else if i.IngressBurstPolicing > 0 {
s = append(s, fmt.Sprintf("ingress_policing_burst=%d", i.IngressBurstPolicing))
}
if i.RemoteIP != "" {
s = append(s, fmt.Sprintf("options:remote_ip=%s", i.RemoteIP))
}
if i.Key != "" {
s = append(s, fmt.Sprintf("options:key=%s", i.Key))
}
return s
}