forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovs.go
304 lines (256 loc) · 9.13 KB
/
ovs.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
package ovs
import (
"fmt"
"strconv"
"strings"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/util/exec"
utilversion "k8s.io/kubernetes/pkg/util/version"
)
// Interface represents an interface to OVS
type Interface interface {
// AddBridge creates the bridge associated with the interface, optionally setting
// properties on it (as with "ovs-vsctl set Bridge ..."). If the bridge already
// existed, it will be destroyed and recreated.
AddBridge(properties ...string) error
// DeleteBridge deletes the bridge associated with the interface. (It is an
// error if the bridge does not exist.)
DeleteBridge() error
// AddPort adds an interface to the bridge, requesting the indicated port
// number, and optionally setting properties on it (as with "ovs-vsctl set
// Interface ..."). Returns the allocated port number (or an error).
AddPort(port string, ofportRequest int, properties ...string) (int, error)
// DeletePort removes an interface from the bridge. (It is not an
// error if the interface is not currently a bridge port.)
DeletePort(port string) error
// GetOFPort returns the OpenFlow port number of a given network interface
// attached to a bridge.
GetOFPort(port string) (int, error)
// SetFrags sets the fragmented-packet-handling mode (as with
// "ovs-ofctl set-frags")
SetFrags(mode string) error
// Create creates a record in the OVS database, as with "ovs-vsctl create" and
// returns the UUID of the newly-created item.
// NOTE: This only works for QoS; for all other tables the created object will
// immediately be garbage-collected; we'd need an API that calls "create" and "set"
// in the same "ovs-vsctl" call.
Create(table string, values ...string) (string, error)
// Destroy deletes the indicated record in the OVS database. It is not an error if
// the record does not exist
Destroy(table, record string) error
// Get gets the indicated value from the OVS database. For multi-valued or
// map-valued columns, the data is returned in the same format as "ovs-vsctl get".
Get(table, record, column string) (string, error)
// Set sets one or more columns on a record in the OVS database, as with
// "ovs-vsctl set"
Set(table, record string, values ...string) error
// Clear unsets the indicated columns in the OVS database. It is not an error if
// the value is already unset
Clear(table, record string, columns ...string) error
// DumpFlows dumps the flow table for the bridge and returns it as an array of
// strings, one per flow.
DumpFlows() ([]string, error)
// NewTransaction begins a new OVS transaction. If an error occurs at
// any step in the transaction, it will be recorded until
// EndTransaction(), and any further calls on the transaction will be
// ignored.
NewTransaction() Transaction
}
// Transaction manages a single set of OVS flow modifications
type Transaction interface {
// AddFlow adds a flow to the bridge. The arguments are passed to fmt.Sprintf().
AddFlow(flow string, args ...interface{})
// DeleteFlows deletes all matching flows from the bridge. The arguments are
// passed to fmt.Sprintf().
DeleteFlows(flow string, args ...interface{})
// EndTransaction ends an OVS transaction and returns any error that occurred
// during the transaction. You should not use the transaction again after
// calling this function.
EndTransaction() error
}
const (
OVS_OFCTL = "ovs-ofctl"
OVS_VSCTL = "ovs-vsctl"
)
// ovsExec implements ovs.Interface via calls to ovs-ofctl and ovs-vsctl
type ovsExec struct {
execer exec.Interface
bridge string
}
// New returns a new ovs.Interface
func New(execer exec.Interface, bridge string, minVersion string) (Interface, error) {
if _, err := execer.LookPath(OVS_OFCTL); err != nil {
return nil, fmt.Errorf("OVS is not installed")
}
if _, err := execer.LookPath(OVS_VSCTL); err != nil {
return nil, fmt.Errorf("OVS is not installed")
}
ovsif := &ovsExec{execer: execer, bridge: bridge}
if minVersion != "" {
minVer := utilversion.MustParseGeneric(minVersion)
out, err := ovsif.exec(OVS_VSCTL, "--version")
if err != nil {
return nil, fmt.Errorf("could not check OVS version is %s or higher", minVersion)
}
// First output line should end with version
lines := strings.Split(out, "\n")
spc := strings.LastIndex(lines[0], " ")
instVer, err := utilversion.ParseGeneric(lines[0][spc+1:])
if err != nil {
return nil, fmt.Errorf("could not find OVS version in %q", lines[0])
}
if !instVer.AtLeast(minVer) {
return nil, fmt.Errorf("found OVS %v, need %s or later", instVer, minVersion)
}
}
return ovsif, nil
}
func (ovsif *ovsExec) exec(cmd string, args ...string) (string, error) {
if cmd == OVS_OFCTL {
args = append([]string{"-O", "OpenFlow13"}, args...)
}
glog.V(5).Infof("Executing: %s %s", cmd, strings.Join(args, " "))
output, err := ovsif.execer.Command(cmd, args...).CombinedOutput()
if err != nil {
glog.V(5).Infof("Error executing %s: %s", cmd, string(output))
return "", err
}
outStr := string(output)
if outStr != "" {
// If output is a single line, strip the trailing newline
nl := strings.Index(outStr, "\n")
if nl == len(outStr)-1 {
outStr = outStr[:nl]
}
}
return outStr, nil
}
func (ovsif *ovsExec) AddBridge(properties ...string) error {
args := []string{"--if-exists", "del-br", ovsif.bridge, "--", "add-br", ovsif.bridge}
if len(properties) > 0 {
args = append(args, "--", "set", "Bridge", ovsif.bridge)
args = append(args, properties...)
}
_, err := ovsif.exec(OVS_VSCTL, args...)
return err
}
func (ovsif *ovsExec) DeleteBridge() error {
_, err := ovsif.exec(OVS_VSCTL, "del-br", ovsif.bridge)
return err
}
func (ovsif *ovsExec) GetOFPort(port string) (int, error) {
ofportStr, err := ovsif.exec(OVS_VSCTL, "get", "Interface", port, "ofport")
if err != nil {
return -1, fmt.Errorf("failed to get OVS port for %s: %v", port, err)
}
ofport, err := strconv.Atoi(ofportStr)
if err != nil {
return -1, fmt.Errorf("could not parse allocated ofport %q: %v", ofportStr, err)
}
if ofport == -1 {
errStr, err := ovsif.exec(OVS_VSCTL, "get", "Interface", port, "error")
if err != nil || errStr == "" {
errStr = "unknown error"
}
return -1, fmt.Errorf("error on port %s: %s", port, errStr)
}
return ofport, nil
}
func (ovsif *ovsExec) AddPort(port string, ofportRequest int, properties ...string) (int, error) {
args := []string{"--may-exist", "add-port", ovsif.bridge, port}
if ofportRequest > 0 || len(properties) > 0 {
args = append(args, "--", "set", "Interface", port)
if ofportRequest > 0 {
args = append(args, fmt.Sprintf("ofport_request=%d", ofportRequest))
}
if len(properties) > 0 {
args = append(args, properties...)
}
}
_, err := ovsif.exec(OVS_VSCTL, args...)
if err != nil {
return -1, err
}
ofport, err := ovsif.GetOFPort(port)
if err != nil {
return -1, err
}
if ofportRequest > 0 && ofportRequest != ofport {
return -1, fmt.Errorf("allocated ofport (%d) did not match request (%d)", ofport, ofportRequest)
}
return ofport, nil
}
func (ovsif *ovsExec) DeletePort(port string) error {
_, err := ovsif.exec(OVS_VSCTL, "--if-exists", "del-port", ovsif.bridge, port)
return err
}
func (ovsif *ovsExec) SetFrags(mode string) error {
_, err := ovsif.exec(OVS_OFCTL, "set-frags", ovsif.bridge, mode)
return err
}
func (ovsif *ovsExec) Create(table string, values ...string) (string, error) {
args := append([]string{"create", table}, values...)
return ovsif.exec(OVS_VSCTL, args...)
}
func (ovsif *ovsExec) Destroy(table, record string) error {
_, err := ovsif.exec(OVS_VSCTL, "--if-exists", "destroy", table, record)
return err
}
func (ovsif *ovsExec) Get(table, record, column string) (string, error) {
return ovsif.exec(OVS_VSCTL, "get", table, record, column)
}
func (ovsif *ovsExec) Set(table, record string, values ...string) error {
args := append([]string{"set", table, record}, values...)
_, err := ovsif.exec(OVS_VSCTL, args...)
return err
}
func (ovsif *ovsExec) Clear(table, record string, columns ...string) error {
args := append([]string{"--if-exists", "clear", table, record}, columns...)
_, err := ovsif.exec(OVS_VSCTL, args...)
return err
}
type ovsExecTx struct {
ovsif *ovsExec
err error
}
func (tx *ovsExecTx) exec(cmd string, args ...string) (string, error) {
out := ""
if tx.err == nil {
out, tx.err = tx.ovsif.exec(cmd, args...)
}
return out, tx.err
}
func (ovsif *ovsExec) NewTransaction() Transaction {
return &ovsExecTx{ovsif: ovsif}
}
func (tx *ovsExecTx) AddFlow(flow string, args ...interface{}) {
if len(args) > 0 {
flow = fmt.Sprintf(flow, args...)
}
tx.exec(OVS_OFCTL, "add-flow", tx.ovsif.bridge, flow)
}
func (tx *ovsExecTx) DeleteFlows(flow string, args ...interface{}) {
if len(args) > 0 {
flow = fmt.Sprintf(flow, args...)
}
tx.exec(OVS_OFCTL, "del-flows", tx.ovsif.bridge, flow)
}
func (tx *ovsExecTx) EndTransaction() error {
err := tx.err
tx.err = nil
return err
}
func (ovsif *ovsExec) DumpFlows() ([]string, error) {
out, err := ovsif.exec(OVS_OFCTL, "dump-flows", ovsif.bridge)
if err != nil {
return nil, err
}
lines := strings.Split(out, "\n")
flows := make([]string, 0, len(lines))
for _, line := range lines {
if strings.Contains(line, "cookie=") {
flows = append(flows, line)
}
}
return flows, nil
}