[sbr]: Use different tableID for every ipCfg
Check tableID not in use for every ipCfg This allows SBR plugin to accommodate for multi-ip interfaces Fixes #581 Signed-off-by: Anurag Dwivedi <anuragensemble1@gmail.com>
This commit is contained in:
parent
f34c600ea4
commit
f72aa98629
@ -176,23 +176,9 @@ func cmdAdd(args *skel.CmdArgs) error {
|
|||||||
return types.PrintResult(conf.PrevResult, conf.CNIVersion)
|
return types.PrintResult(conf.PrevResult, conf.CNIVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
// doRoutes does all the work to set up routes and rules during an add.
|
// getNextTableID picks the first free table id from a giveen candidate id
|
||||||
func doRoutes(ipCfgs []*current.IPConfig, origRoutes []*types.Route, iface string) error {
|
func getNextTableID(rules []netlink.Rule, routes []netlink.Route, candidateID int) int {
|
||||||
// Get a list of rules and routes ready.
|
table := candidateID
|
||||||
rules, err := netlink.RuleList(netlink.FAMILY_ALL)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Failed to list all rules: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
routes, err := netlink.RouteList(nil, netlink.FAMILY_ALL)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Failed to list all routes: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pick a table ID to use. We pick the first table ID from firstTableID
|
|
||||||
// on that has no existing rules mapping to it and no existing routes in
|
|
||||||
// it.
|
|
||||||
table := firstTableID
|
|
||||||
for {
|
for {
|
||||||
foundExisting := false
|
foundExisting := false
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
@ -215,7 +201,26 @@ func doRoutes(ipCfgs []*current.IPConfig, origRoutes []*types.Route, iface strin
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return table
|
||||||
|
}
|
||||||
|
|
||||||
|
// doRoutes does all the work to set up routes and rules during an add.
|
||||||
|
func doRoutes(ipCfgs []*current.IPConfig, origRoutes []*types.Route, iface string) error {
|
||||||
|
// Get a list of rules and routes ready.
|
||||||
|
rules, err := netlink.RuleList(netlink.FAMILY_ALL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to list all rules: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
routes, err := netlink.RouteList(nil, netlink.FAMILY_ALL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to list all routes: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pick a table ID to use. We pick the first table ID from firstTableID
|
||||||
|
// on that has no existing rules mapping to it and no existing routes in
|
||||||
|
// it.
|
||||||
|
table := getNextTableID(rules, routes, firstTableID)
|
||||||
log.Printf("First unreferenced table: %d", table)
|
log.Printf("First unreferenced table: %d", table)
|
||||||
|
|
||||||
link, err := netlink.LinkByName(iface)
|
link, err := netlink.LinkByName(iface)
|
||||||
@ -287,7 +292,7 @@ func doRoutes(ipCfgs []*current.IPConfig, origRoutes []*types.Route, iface strin
|
|||||||
// to the table we want them in.
|
// to the table we want them in.
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if ipCfg.Address.Contains(r.Src) || ipCfg.Address.Contains(r.Gw) ||
|
if ipCfg.Address.Contains(r.Src) || ipCfg.Address.Contains(r.Gw) ||
|
||||||
(r.Src == nil && r.Gw == nil) {
|
(r.Src == nil && r.Gw == nil) {
|
||||||
// (r.Src == nil && r.Gw == nil) is inferred as a generic route
|
// (r.Src == nil && r.Gw == nil) is inferred as a generic route
|
||||||
log.Printf("Copying route %s from table %d to %d",
|
log.Printf("Copying route %s from table %d to %d",
|
||||||
r.String(), r.Table, table)
|
r.String(), r.Table, table)
|
||||||
@ -309,6 +314,7 @@ func doRoutes(ipCfgs []*current.IPConfig, origRoutes []*types.Route, iface strin
|
|||||||
|
|
||||||
// Use a different table for each ipCfg
|
// Use a different table for each ipCfg
|
||||||
table++
|
table++
|
||||||
|
table = getNextTableID(rules, routes, table)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete all the interface routes in the default routing table, which were
|
// Delete all the interface routes in the default routing table, which were
|
||||||
|
@ -445,7 +445,7 @@ var _ = Describe("sbr test", func() {
|
|||||||
// Add the second IP on net1 interface
|
// Add the second IP on net1 interface
|
||||||
preStatus.Devices[1].Addrs = append(preStatus.Devices[1].Addrs,
|
preStatus.Devices[1].Addrs = append(preStatus.Devices[1].Addrs,
|
||||||
net.IPNet{
|
net.IPNet{
|
||||||
IP: net.IPv4(192, 168, 101, 209),
|
IP: net.IPv4(192, 168, 101, 209),
|
||||||
Mask: net.IPv4Mask(255, 255, 255, 0),
|
Mask: net.IPv4Mask(255, 255, 255, 0),
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -472,17 +472,17 @@ var _ = Describe("sbr test", func() {
|
|||||||
if expNet1.Routes[i].Src.String() == "192.168.101.209" {
|
if expNet1.Routes[i].Src.String() == "192.168.101.209" {
|
||||||
// All 192.168.101.x routes expected in table 101
|
// All 192.168.101.x routes expected in table 101
|
||||||
expNet1.Routes[i].Table = 101
|
expNet1.Routes[i].Table = 101
|
||||||
} else if (expNet1.Routes[i].Src == nil && expNet1.Routes[i].Gw == nil) {
|
} else if expNet1.Routes[i].Src == nil && expNet1.Routes[i].Gw == nil {
|
||||||
// Generic Link Local Addresses assigned. They should exist in all
|
// Generic Link Local Addresses assigned. They should exist in all
|
||||||
// route tables
|
// route tables
|
||||||
expNet1.Routes[i].Table = 100
|
expNet1.Routes[i].Table = 100
|
||||||
expNet1.Routes = append(expNet1.Routes,
|
expNet1.Routes = append(expNet1.Routes,
|
||||||
netlink.Route{
|
netlink.Route{
|
||||||
Dst: expNet1.Routes[i].Dst,
|
Dst: expNet1.Routes[i].Dst,
|
||||||
Table: 101,
|
Table: 101,
|
||||||
LinkIndex: expNet1.Routes[i].LinkIndex})
|
LinkIndex: expNet1.Routes[i].LinkIndex})
|
||||||
} else {
|
} else {
|
||||||
// All 192.168.1.x routes expected in tabele 100
|
// All 192.168.1.x routes expected in table 100
|
||||||
expNet1.Routes[i].Table = 100
|
expNet1.Routes[i].Table = 100
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -499,7 +499,7 @@ var _ = Describe("sbr test", func() {
|
|||||||
Table: 101,
|
Table: 101,
|
||||||
LinkIndex: expNet1.Routes[0].LinkIndex})
|
LinkIndex: expNet1.Routes[0].LinkIndex})
|
||||||
|
|
||||||
// 2 Rules will ve created for each IP address. (100, 101)
|
// 2 Rules will be created for each IP address. (100, 101)
|
||||||
Expect(len(newStatus.Rules)).To(Equal(2))
|
Expect(len(newStatus.Rules)).To(Equal(2))
|
||||||
|
|
||||||
// First entry corresponds to last table
|
// First entry corresponds to last table
|
||||||
|
Loading…
x
Reference in New Issue
Block a user