-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathovs_switch.go
104 lines (90 loc) · 2.65 KB
/
ovs_switch.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
// Copyright (c) 2017 Che Wei, Lin
//
// 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 tinynet
import (
"errors"
"net"
"strconv"
"time"
"github.com/John-Lin/ovsdb"
log "github.com/sirupsen/logrus"
)
// OVSSwitch is a bridge instance
type OVSSwitch struct {
NodeType string
BridgeName string
CtrlHostPort string
ovsdb *ovsdb.OvsDriver
}
// NewOVSSwitch for creating a ovs bridge
func NewOVSSwitch(bridgeName string) (*OVSSwitch, error) {
sw := new(OVSSwitch)
sw.NodeType = "OVSSwitch"
sw.BridgeName = bridgeName
sw.ovsdb = ovsdb.NewOvsDriverWithUnix(bridgeName)
log.Infoln("Adding a switch:", sw.BridgeName)
// Check if port is already part of the OVS and add it
if !sw.ovsdb.IsPortNamePresent(bridgeName) {
// Create an internal port in OVS
err := sw.ovsdb.CreatePort(bridgeName, "internal", 0)
if err != nil {
return nil, err
}
}
time.Sleep(300 * time.Millisecond)
// log.Infof("Waiting for OVS bridge %s setup", bridgeName)
// ip link set ovs up
_, err := ifaceUp(bridgeName)
if err != nil {
return nil, err
}
return sw, nil
}
// addPort for asking OVSDB driver to add the port
func (sw *OVSSwitch) addPort(ifName string) error {
if !sw.ovsdb.IsPortNamePresent(ifName) {
err := sw.ovsdb.CreatePort(ifName, "", 0)
if err != nil {
log.Fatalf("Error creating the port. Err: %v", err)
return err
}
}
return nil
}
// SetCtrl for seting up OpenFlow controller for ovs bridge
func (sw *OVSSwitch) SetCtrl(hostport string) error {
host, port, err := net.SplitHostPort(hostport)
if err != nil {
log.Fatalf("Invalid controller IP and port. Err: %v", err)
return err
}
uPort, err := strconv.ParseUint(port, 10, 32)
if err != nil {
log.Fatalf("Invalid controller port number. Err: %v", err)
return err
}
err = sw.ovsdb.AddController(host, uint16(uPort))
if err != nil {
log.Fatalf("Error adding controller to OVS. Err: %v", err)
return err
}
sw.CtrlHostPort = hostport
return nil
}
func (sw *OVSSwitch) Delete() error {
if exist := sw.ovsdb.IsBridgePresent(sw.BridgeName); exist != true {
return errors.New(sw.BridgeName + " doesn't exist, we can delete")
}
return sw.ovsdb.DeleteBridge(sw.BridgeName)
}