Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent incorrect deletion of HostSubnet OVS flows #19080

Merged
merged 2 commits into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pkg/network/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ type OsdnNode struct {
host knetwork.Host
kubeletCniPlugin knetwork.NetworkPlugin

hostSubnetMap map[string]*networkapi.HostSubnet

kubeInformers kinternalinformers.SharedInformerFactory
networkInformers networkinformers.SharedInformerFactory

Expand Down Expand Up @@ -183,7 +181,6 @@ func New(c *OsdnNodeConfig) (network.NodeInterface, error) {
mtu: c.MTU,
egressPolicies: make(map[uint32][]networkapi.EgressNetworkPolicy),
egressDNS: common.NewEgressDNS(),
hostSubnetMap: make(map[string]*networkapi.HostSubnet),
kubeInformers: c.KubeInformers,
networkInformers: c.NetworkInformers,
egressIP: newEgressIPWatcher(oc, c.SelfIP, c.MasqueradeBit),
Expand Down Expand Up @@ -333,7 +330,8 @@ func (node *OsdnNode) Start() error {
return fmt.Errorf("node SDN setup failed: %v", err)
}

node.SubnetStartNode()
hsw := newHostSubnetWatcher(node.oc, node.localIP, node.networkInfo)
hsw.Start(node.networkInformers)

if err = node.policy.Start(node); err != nil {
return err
Expand Down
27 changes: 18 additions & 9 deletions pkg/network/node/ovscontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package node

import (
"crypto/sha256"
"fmt"
"net"
"sort"
Expand Down Expand Up @@ -34,7 +35,7 @@ const (
Vxlan0 = "vxlan0"

// rule versioning; increment each time flow rules change
ruleVersion = 6
ruleVersion = 7

ruleVersionTable = 253
)
Expand Down Expand Up @@ -503,26 +504,34 @@ func (oc *ovsController) UpdateEgressNetworkPolicyRules(policies []networkapi.Eg
}
}

func hostSubnetCookie(subnet *networkapi.HostSubnet) uint32 {
hash := sha256.Sum256([]byte(subnet.UID))
return (uint32(hash[0]) << 24) | (uint32(hash[1]) << 16) | (uint32(hash[2]) << 8) | uint32(hash[3])
}

func (oc *ovsController) AddHostSubnetRules(subnet *networkapi.HostSubnet) error {
cookie := hostSubnetCookie(subnet)
otx := oc.ovs.NewTransaction()

otx.AddFlow("table=10, priority=100, tun_src=%s, actions=goto_table:30", subnet.HostIP)
otx.AddFlow("table=10, priority=100, cookie=0x%08x, tun_src=%s, actions=goto_table:30", cookie, subnet.HostIP)
if vnid, ok := subnet.Annotations[networkapi.FixedVNIDHostAnnotation]; ok {
otx.AddFlow("table=50, priority=100, arp, nw_dst=%s, actions=load:%s->NXM_NX_TUN_ID[0..31],set_field:%s->tun_dst,output:1", subnet.Subnet, vnid, subnet.HostIP)
otx.AddFlow("table=90, priority=100, ip, nw_dst=%s, actions=load:%s->NXM_NX_TUN_ID[0..31],set_field:%s->tun_dst,output:1", subnet.Subnet, vnid, subnet.HostIP)
otx.AddFlow("table=50, priority=100, cookie=0x%08x, arp, nw_dst=%s, actions=load:%s->NXM_NX_TUN_ID[0..31],set_field:%s->tun_dst,output:1", cookie, subnet.Subnet, vnid, subnet.HostIP)
otx.AddFlow("table=90, priority=100, cookie=0x%08x, ip, nw_dst=%s, actions=load:%s->NXM_NX_TUN_ID[0..31],set_field:%s->tun_dst,output:1", cookie, subnet.Subnet, vnid, subnet.HostIP)
} else {
otx.AddFlow("table=50, priority=100, arp, nw_dst=%s, actions=move:NXM_NX_REG0[]->NXM_NX_TUN_ID[0..31],set_field:%s->tun_dst,output:1", subnet.Subnet, subnet.HostIP)
otx.AddFlow("table=90, priority=100, ip, nw_dst=%s, actions=move:NXM_NX_REG0[]->NXM_NX_TUN_ID[0..31],set_field:%s->tun_dst,output:1", subnet.Subnet, subnet.HostIP)
otx.AddFlow("table=50, priority=100, cookie=0x%08x, arp, nw_dst=%s, actions=move:NXM_NX_REG0[]->NXM_NX_TUN_ID[0..31],set_field:%s->tun_dst,output:1", cookie, subnet.Subnet, subnet.HostIP)
otx.AddFlow("table=90, priority=100, cookie=0x%08x, ip, nw_dst=%s, actions=move:NXM_NX_REG0[]->NXM_NX_TUN_ID[0..31],set_field:%s->tun_dst,output:1", cookie, subnet.Subnet, subnet.HostIP)
}

return otx.EndTransaction()
}

func (oc *ovsController) DeleteHostSubnetRules(subnet *networkapi.HostSubnet) error {
cookie := hostSubnetCookie(subnet)

otx := oc.ovs.NewTransaction()
otx.DeleteFlows("table=10, tun_src=%s", subnet.HostIP)
otx.DeleteFlows("table=50, arp, nw_dst=%s", subnet.Subnet)
otx.DeleteFlows("table=90, ip, nw_dst=%s", subnet.Subnet)
otx.DeleteFlows("table=10, cookie=0x%08x, tun_src=%s", cookie, subnet.HostIP)
otx.DeleteFlows("table=50, cookie=0x%08x, arp, nw_dst=%s", cookie, subnet.Subnet)
otx.DeleteFlows("table=90, cookie=0x%08x, ip, nw_dst=%s", cookie, subnet.Subnet)
return otx.EndTransaction()
}

Expand Down
120 changes: 1 addition & 119 deletions pkg/network/node/ovscontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,62 +103,6 @@ func assertFlowChanges(origFlows, newFlows []string, changes ...flowChange) erro
return nil
}

func TestOVSHostSubnet(t *testing.T) {
ovsif, oc, origFlows := setupOVSController(t)

hs := networkapi.HostSubnet{
TypeMeta: metav1.TypeMeta{
Kind: "HostSubnet",
},
ObjectMeta: metav1.ObjectMeta{
Name: "node2",
},
Host: "node2",
HostIP: "192.168.1.2",
Subnet: "10.129.0.0/23",
}
err := oc.AddHostSubnetRules(&hs)
if err != nil {
t.Fatalf("Unexpected error adding HostSubnet rules: %v", err)
}

flows, err := ovsif.DumpFlows("")
if err != nil {
t.Fatalf("Unexpected error dumping flows: %v", err)
}
err = assertFlowChanges(origFlows, flows,
flowChange{
kind: flowAdded,
match: []string{"table=10", "tun_src=192.168.1.2"},
},
flowChange{
kind: flowAdded,
match: []string{"table=50", "arp", "arp_tpa=10.129.0.0/23", "192.168.1.2->tun_dst"},
},
flowChange{
kind: flowAdded,
match: []string{"table=90", "ip", "nw_dst=10.129.0.0/23", "192.168.1.2->tun_dst"},
},
)
if err != nil {
t.Fatalf("Unexpected flow changes: %v\nOrig: %#v\nNew: %#v", err, origFlows, flows)
}

err = oc.DeleteHostSubnetRules(&hs)
if err != nil {
t.Fatalf("Unexpected error deleting HostSubnet rules: %v", err)
}
flows, err = ovsif.DumpFlows("")
if err != nil {
t.Fatalf("Unexpected error dumping flows: %v", err)
}
err = assertFlowChanges(origFlows, flows) // no changes

if err != nil {
t.Fatalf("Unexpected flow changes: %v\nOrig: %#v\nNew: %#v", err, origFlows, flows)
}
}

func TestOVSService(t *testing.T) {
ovsif, oc, origFlows := setupOVSController(t)

Expand Down Expand Up @@ -361,10 +305,9 @@ func TestGetPodDetails(t *testing.T) {
}
}

func TestOVSMulticast(t *testing.T) {
func TestOVSLocalMulticast(t *testing.T) {
ovsif, oc, origFlows := setupOVSController(t)

// local flows
err := oc.UpdateLocalMulticastFlows(99, true, []int{4, 5, 6})
if err != nil {
t.Fatalf("Unexpected error adding multicast flows: %v", err)
Expand Down Expand Up @@ -413,67 +356,6 @@ func TestOVSMulticast(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected flow changes: %v\nOrig: %#v\nNew: %#v", err, origFlows, flows)
}

// VXLAN
err = oc.UpdateVXLANMulticastFlows([]string{"192.168.1.2", "192.168.1.5", "192.168.1.3"})
if err != nil {
t.Fatalf("Unexpected error adding multicast flows: %v", err)
}
flows, err = ovsif.DumpFlows("")
if err != nil {
t.Fatalf("Unexpected error dumping flows: %v", err)
}
err = assertFlowChanges(origFlows, flows,
flowChange{
kind: flowRemoved,
match: []string{"table=111", "goto_table:120"},
noMatch: []string{"->tun_dst"},
},
flowChange{
kind: flowAdded,
match: []string{"table=111", "192.168.1.2->tun_dst", "192.168.1.3->tun_dst", "192.168.1.5->tun_dst"},
},
)
if err != nil {
t.Fatalf("Unexpected flow changes: %v\nOrig: %#v\nNew: %#v", err, origFlows, flows)
}

err = oc.UpdateVXLANMulticastFlows([]string{"192.168.1.5", "192.168.1.3"})
if err != nil {
t.Fatalf("Unexpected error adding multicast flows: %v", err)
}
flows, err = ovsif.DumpFlows("")
if err != nil {
t.Fatalf("Unexpected error dumping flows: %v", err)
}
err = assertFlowChanges(origFlows, flows,
flowChange{
kind: flowRemoved,
match: []string{"table=111", "goto_table:120"},
noMatch: []string{"->tun_dst"},
},
flowChange{
kind: flowAdded,
match: []string{"table=111", "192.168.1.3->tun_dst", "192.168.1.5->tun_dst"},
noMatch: []string{"192.168.1.2"},
},
)
if err != nil {
t.Fatalf("Unexpected flow changes: %v\nOrig: %#v\nNew: %#v", err, origFlows, flows)
}

err = oc.UpdateVXLANMulticastFlows([]string{})
if err != nil {
t.Fatalf("Unexpected error adding multicast flows: %v", err)
}
flows, err = ovsif.DumpFlows("")
if err != nil {
t.Fatalf("Unexpected error dumping flows: %v", err)
}
err = assertFlowChanges(origFlows, flows) // no changes
if err != nil {
t.Fatalf("Unexpected flow changes: %v\nOrig: %#v\nNew: %#v", err, origFlows, flows)
}
}

var enp1 = networkapi.EgressNetworkPolicy{
Expand Down
16 changes: 0 additions & 16 deletions pkg/network/node/sdn_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

"github.com/golang/glog"

networkapi "github.com/openshift/origin/pkg/network/apis/network"
"github.com/openshift/origin/pkg/network/common"
"github.com/openshift/origin/pkg/util/netutils"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -208,20 +206,6 @@ func (plugin *OsdnNode) updateEgressNetworkPolicyRules(vnid uint32) {
}
}

func (plugin *OsdnNode) AddHostSubnetRules(subnet *networkapi.HostSubnet) {
glog.Infof("AddHostSubnetRules for %s", common.HostSubnetToString(subnet))
if err := plugin.oc.AddHostSubnetRules(subnet); err != nil {
utilruntime.HandleError(fmt.Errorf("Error adding OVS flows for subnet %q: %v", subnet.Subnet, err))
}
}

func (plugin *OsdnNode) DeleteHostSubnetRules(subnet *networkapi.HostSubnet) {
glog.Infof("DeleteHostSubnetRules for %s", common.HostSubnetToString(subnet))
if err := plugin.oc.DeleteHostSubnetRules(subnet); err != nil {
utilruntime.HandleError(fmt.Errorf("Error deleting OVS flows for subnet %q: %v", subnet.Subnet, err))
}
}

func (plugin *OsdnNode) AddServiceRules(service *kapi.Service, netID uint32) {
glog.V(5).Infof("AddServiceRules for %v", service)
if err := plugin.oc.AddServiceRules(service, netID); err != nil {
Expand Down
Loading