/
lbr.go
89 lines (79 loc) · 3.8 KB
/
lbr.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
package lbr
import (
"encoding/hex"
"fmt"
log "github.com/golang/glog"
"net"
"os/exec"
"github.com/openshift/openshift-sdn/ovssubnet/api"
"github.com/openshift/openshift-sdn/pkg/netutils"
)
type FlowController struct {
}
func NewFlowController() *FlowController {
return &FlowController{}
}
func (c *FlowController) Setup(localSubnet, containerNetwork, servicesNetwork string, mtu uint) error {
_, ipnet, err := net.ParseCIDR(localSubnet)
subnetMaskLength, _ := ipnet.Mask.Size()
out, err := exec.Command("openshift-sdn-simple-setup-node.sh", netutils.GenerateDefaultGateway(ipnet).String(), ipnet.String(), containerNetwork, fmt.Sprint(subnetMaskLength), fmt.Sprint(mtu)).CombinedOutput()
log.Infof("Output of setup script:\n%s", out)
if err != nil {
log.Errorf("Error executing setup script. \n\tOutput: %s\n\tError: %v\n", out, err)
}
_, err = exec.Command("ovs-ofctl", "-O", "OpenFlow13", "del-flows", "br0").CombinedOutput()
return err
}
func (c *FlowController) AddOFRules(nodeIP, subnet, localIP string) error {
cookie := generateCookie(nodeIP)
if nodeIP == localIP {
// self, so add the input rules
iprule := fmt.Sprintf("table=0,cookie=0x%s,priority=200,ip,in_port=10,nw_dst=%s,actions=output:9", cookie, subnet)
arprule := fmt.Sprintf("table=0,cookie=0x%s,priority=200,arp,in_port=10,nw_dst=%s,actions=output:9", cookie, subnet)
o, e := exec.Command("ovs-ofctl", "-O", "OpenFlow13", "add-flow", "br0", iprule).CombinedOutput()
log.Infof("Output of adding %s: %s (%v)", iprule, o, e)
o, e = exec.Command("ovs-ofctl", "-O", "OpenFlow13", "add-flow", "br0", arprule).CombinedOutput()
log.Infof("Output of adding %s: %s (%v)", arprule, o, e)
return e
} else {
iprule := fmt.Sprintf("table=0,cookie=0x%s,priority=200,ip,in_port=9,nw_dst=%s,actions=set_field:%s->tun_dst,output:10", cookie, subnet, nodeIP)
arprule := fmt.Sprintf("table=0,cookie=0x%s,priority=200,arp,in_port=9,nw_dst=%s,actions=set_field:%s->tun_dst,output:10", cookie, subnet, nodeIP)
o, e := exec.Command("ovs-ofctl", "-O", "OpenFlow13", "add-flow", "br0", iprule).CombinedOutput()
log.Infof("Output of adding %s: %s (%v)", iprule, o, e)
o, e = exec.Command("ovs-ofctl", "-O", "OpenFlow13", "add-flow", "br0", arprule).CombinedOutput()
log.Infof("Output of adding %s: %s (%v)", arprule, o, e)
return e
}
return nil
}
func (c *FlowController) DelOFRules(node, localIP string) error {
log.Infof("Calling del rules for %s.", node)
cookie := generateCookie(node)
if node == localIP {
iprule := fmt.Sprintf("table=0,cookie=0x%s/0xffffffff,ip,in_port=10", cookie)
arprule := fmt.Sprintf("table=0,cookie=0x%s/0xffffffff,arp,in_port=10", cookie)
o, e := exec.Command("ovs-ofctl", "-O", "OpenFlow13", "del-flows", "br0", iprule).CombinedOutput()
log.Infof("Output of deleting local ip rules: %s (%v)", o, e)
o, e = exec.Command("ovs-ofctl", "-O", "OpenFlow13", "del-flows", "br0", arprule).CombinedOutput()
log.Infof("Output of deleting local arp rules: %s (%v)", o, e)
return e
} else {
iprule := fmt.Sprintf("table=0,cookie=0x%s/0xffffffff,ip,in_port=9", cookie)
arprule := fmt.Sprintf("table=0,cookie=0x%s/0xffffffff,arp,in_port=9", cookie)
o, e := exec.Command("ovs-ofctl", "-O", "OpenFlow13", "del-flows", "br0", iprule).CombinedOutput()
log.Infof("Output of deleting %s: %s (%v)", iprule, o, e)
o, e = exec.Command("ovs-ofctl", "-O", "OpenFlow13", "del-flows", "br0", arprule).CombinedOutput()
log.Infof("Output of deleting %s: %s (%v)", arprule, o, e)
return e
}
return nil
}
func generateCookie(ip string) string {
return hex.EncodeToString(net.ParseIP(ip).To4())
}
func (c *FlowController) AddServiceOFRules(netID uint, IP string, protocol api.ServiceProtocol, port uint) error {
return nil
}
func (c *FlowController) DelServiceOFRules(netID uint, IP string, protocol api.ServiceProtocol, port uint) error {
return nil
}