| 1 | // Copyright 2017 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "log" |
| 20 | "net" |
| 21 | |
| 22 | . "github.com/onsi/ginkgo/v2" |
| 23 | . "github.com/onsi/gomega" |
| 24 | "github.com/vishvananda/netlink" |
| 25 | "golang.org/x/sys/unix" |
| 26 | |
| 27 | "github.com/containernetworking/cni/pkg/skel" |
| 28 | "github.com/containernetworking/plugins/pkg/netlinksafe" |
| 29 | "github.com/containernetworking/plugins/pkg/ns" |
| 30 | "github.com/containernetworking/plugins/pkg/testutils" |
| 31 | ) |
| 32 | |
| 33 | // Structures specifying state at start. |
| 34 | type Device struct { |
| 35 | Name string |
| 36 | Addrs []net.IPNet |
| 37 | Routes []netlink.Route |
| 38 | } |
| 39 | |
| 40 | type Rule struct { |
| 41 | Src string |
| 42 | Table int |
| 43 | } |
| 44 | |
| 45 | type netStatus struct { |
| 46 | Devices []Device |
| 47 | Rules []netlink.Rule |
| 48 | } |
| 49 | |
| 50 | // Create a link, shove some IP addresses on it, and push it into the network |
| 51 | // namespace. |
| 52 | func setup(targetNs ns.NetNS, status netStatus) error { |
| 53 | // Get the status right |
| 54 | err := targetNs.Do(func(_ ns.NetNS) error { |
| 55 | for _, dev := range status.Devices { |
| 56 | log.Printf("Adding dev %s\n", dev.Name) |
| 57 | linkAttrs := netlink.NewLinkAttrs() |
| 58 | linkAttrs.Name = dev.Name |
| 59 | link := &netlink.Dummy{LinkAttrs: linkAttrs} |
| 60 | err := netlink.LinkAdd(link) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | err = netlink.LinkSetUp(link) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | for _, addr := range dev.Addrs { |
| 71 | log.Printf("Adding address %v to device %s\n", addr, dev.Name) |
| 72 | err = netlink.AddrAdd(link, &netlink.Addr{IPNet: &addr}) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | for _, route := range dev.Routes { |
| 79 | log.Printf("Adding route %v to device %s\n", route, dev.Name) |
| 80 | route.LinkIndex = link.Attrs().Index |
| 81 | err = netlink.RouteAdd(&route) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return nil |
| 88 | }) |
| 89 | |
| 90 | return err |
| 91 | } |
| 92 | |
| 93 | // Readback the routes and rules. |
| 94 | func readback(targetNs ns.NetNS, devNames []string) (netStatus, error) { |
| 95 | // Get the status right. |
| 96 | var retVal netStatus |
| 97 | |
| 98 | err := targetNs.Do(func(_ ns.NetNS) error { |
| 99 | retVal.Devices = make([]Device, 2) |
| 100 | |
| 101 | for i, name := range devNames { |
| 102 | log.Printf("Checking device %s", name) |
| 103 | retVal.Devices[i].Name = name |
| 104 | |
| 105 | link, err := netlinksafe.LinkByName(name) |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | |
| 110 | // Need to read all tables, so cannot use RouteList |
| 111 | routeFilter := &netlink.Route{ |
| 112 | LinkIndex: link.Attrs().Index, |
| 113 | Table: unix.RT_TABLE_UNSPEC, |
| 114 | } |
| 115 | |
| 116 | routes, err := netlinksafe.RouteListFiltered(netlink.FAMILY_ALL, |
| 117 | routeFilter, |
| 118 | netlink.RT_FILTER_OIF|netlink.RT_FILTER_TABLE) |
| 119 | if err != nil { |
| 120 | return err |
| 121 | } |
| 122 | |
| 123 | routesNoLinkLocal := []netlink.Route{} |
| 124 | for _, route := range routes { |
| 125 | if route.Dst.IP.IsLinkLocalMulticast() || route.Dst.IP.IsLinkLocalUnicast() { |
| 126 | continue |
| 127 | } |
| 128 | log.Printf("Got %s route %v", name, route) |
| 129 | routesNoLinkLocal = append(routesNoLinkLocal, route) |
| 130 | } |
| 131 | |
| 132 | retVal.Devices[i].Routes = routesNoLinkLocal |
| 133 | } |
| 134 | |
| 135 | rules, err := netlinksafe.RuleList(netlink.FAMILY_ALL) |
| 136 | if err != nil { |
| 137 | return err |
| 138 | } |
| 139 | |
| 140 | retVal.Rules = make([]netlink.Rule, 0, len(rules)) |
| 141 | |
| 142 | for _, rule := range rules { |
| 143 | // Rules over 250 are the kernel defaults, that we ignore. |
| 144 | if rule.Table < 250 { |
| 145 | log.Printf("Got interesting rule %v", rule) |
| 146 | retVal.Rules = append(retVal.Rules, rule) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return nil |
| 151 | }) |
| 152 | |
| 153 | return retVal, err |
| 154 | } |
| 155 | |
| 156 | func equalRoutes(expected, actual []netlink.Route) bool { |
| 157 | // Compare two sets of routes, comparing only destination, gateway and |
| 158 | // table. Return true if equal. |
| 159 | match := true |
| 160 | |
| 161 | // Map used to make comparisons easy |
| 162 | expMap := make(map[string]bool) |
| 163 | |
| 164 | for _, route := range expected { |
| 165 | expMap[route.String()] = true |
| 166 | } |
| 167 | |
| 168 | for _, route := range actual { |
| 169 | routeString := route.String() |
| 170 | if expMap[routeString] { |
| 171 | log.Printf("Route %s expected and found", routeString) |
| 172 | delete(expMap, routeString) |
| 173 | } else { |
| 174 | log.Printf("Route %s found, not expected", routeString) |
| 175 | match = false |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | for expRoute := range expMap { |
| 180 | log.Printf("Route %s expected, not found", expRoute) |
| 181 | match = false |
| 182 | } |
| 183 | |
| 184 | return match |
| 185 | } |
| 186 | |
| 187 | func createDefaultStatus() netStatus { |
| 188 | // Useful default status. |
| 189 | devs := make([]Device, 2) |
| 190 | rules := make([]netlink.Rule, 0) |
| 191 | |
| 192 | devs[0] = Device{Name: "eth0"} |
| 193 | devs[0].Addrs = make([]net.IPNet, 1) |
| 194 | devs[0].Addrs[0] = net.IPNet{ |
| 195 | IP: net.IPv4(10, 0, 0, 2), |
| 196 | Mask: net.IPv4Mask(255, 255, 255, 0), |
| 197 | } |
| 198 | devs[0].Routes = make([]netlink.Route, 2) |
| 199 | devs[0].Routes[0] = netlink.Route{ |
| 200 | Dst: &net.IPNet{ |
| 201 | IP: net.IPv4(10, 2, 0, 0), |
| 202 | Mask: net.IPv4Mask(255, 255, 255, 0), |
| 203 | }, |
| 204 | Gw: net.IPv4(10, 0, 0, 5), |
| 205 | } |
| 206 | devs[0].Routes[1] = netlink.Route{ |
| 207 | Gw: net.IPv4(10, 0, 0, 1), |
| 208 | } |
| 209 | |
| 210 | devs[1] = Device{Name: "net1"} |
| 211 | devs[1].Addrs = make([]net.IPNet, 1) |
| 212 | devs[1].Addrs[0] = net.IPNet{ |
| 213 | IP: net.IPv4(192, 168, 1, 209), |
| 214 | Mask: net.IPv4Mask(255, 255, 255, 0), |
| 215 | } |
| 216 | devs[1].Routes = make([]netlink.Route, 1) |
| 217 | devs[1].Routes[0] = netlink.Route{ |
| 218 | Dst: &net.IPNet{ |
| 219 | IP: net.IPv4(192, 168, 2, 0), |
| 220 | Mask: net.IPv4Mask(255, 255, 255, 0), |
| 221 | }, |
| 222 | Gw: net.IPv4(192, 168, 1, 2), |
| 223 | } |
| 224 | |
| 225 | return netStatus{ |
| 226 | Devices: devs, |
| 227 | Rules: rules, |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | var _ = Describe("sbr test", func() { |
| 232 | var targetNs ns.NetNS |
| 233 | |
| 234 | BeforeEach(func() { |
| 235 | var err error |
| 236 | targetNs, err = testutils.NewNS() |
| 237 | Expect(err).NotTo(HaveOccurred()) |
| 238 | }) |
| 239 | |
| 240 | AfterEach(func() { |
| 241 | targetNs.Close() |
| 242 | }) |
| 243 | |
| 244 | It("Works with a 0.3.0 config", func() { |
| 245 | ifname := "net1" |
| 246 | conf := `{ |
| 247 | "cniVersion": "0.3.0", |
| 248 | "name": "cni-plugin-sbr-test", |
| 249 | "type": "sbr", |
| 250 | "prevResult": { |
| 251 | "cniVersion": "0.3.0", |
| 252 | "interfaces": [ |
| 253 | { |
| 254 | "name": "%s", |
| 255 | "sandbox": "%s" |
| 256 | } |
| 257 | ], |
| 258 | "ips": [ |
| 259 | { |
| 260 | "version": "4", |
| 261 | "address": "192.168.1.209/24", |
| 262 | "gateway": "192.168.1.1", |
| 263 | "interface": 0 |
| 264 | } |
| 265 | ], |
| 266 | "routes": [] |
| 267 | } |
| 268 | }` |
| 269 | conf = fmt.Sprintf(conf, ifname, targetNs.Path()) |
| 270 | args := &skel.CmdArgs{ |
| 271 | ContainerID: "dummy", |
| 272 | Netns: targetNs.Path(), |
| 273 | IfName: ifname, |
| 274 | StdinData: []byte(conf), |
| 275 | } |
| 276 | |
| 277 | err := setup(targetNs, createDefaultStatus()) |
| 278 | Expect(err).NotTo(HaveOccurred()) |
| 279 | |
| 280 | oldStatus, err := readback(targetNs, []string{"net1", "eth0"}) |
| 281 | Expect(err).NotTo(HaveOccurred()) |
| 282 | |
| 283 | _, _, err = testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) }) |
| 284 | Expect(err).NotTo(HaveOccurred()) |
| 285 | |
| 286 | newStatus, err := readback(targetNs, []string{"net1", "eth0"}) |
| 287 | Expect(err).NotTo(HaveOccurred()) |
| 288 | |
| 289 | // Check results. We expect all the routes on net1 to have moved to |
| 290 | // table 100 except for local routes (table 255); a new default gateway |
| 291 | // route to have been created; and a single rule to exist. |
| 292 | expNet1 := oldStatus.Devices[0] |
| 293 | expEth0 := oldStatus.Devices[1] |
| 294 | for i := range expNet1.Routes { |
| 295 | if expNet1.Routes[i].Table != 255 { |
| 296 | expNet1.Routes[i].Table = 100 |
| 297 | } |
| 298 | } |
| 299 | expNet1.Routes = append(expNet1.Routes, |
| 300 | netlink.Route{ |
| 301 | Gw: net.IPv4(192, 168, 1, 1), |
| 302 | Dst: &net.IPNet{IP: net.IPv4zero, Mask: net.IPMask(net.IPv4zero)}, |
| 303 | Table: 100, |
| 304 | LinkIndex: expNet1.Routes[0].LinkIndex, |
| 305 | }) |
| 306 | |
| 307 | Expect(newStatus.Rules).To(HaveLen(1)) |
| 308 | Expect(newStatus.Rules[0].Table).To(Equal(100)) |
| 309 | Expect(newStatus.Rules[0].Src.String()).To(Equal("192.168.1.209/32")) |
| 310 | devNet1 := newStatus.Devices[0] |
| 311 | devEth0 := newStatus.Devices[1] |
| 312 | Expect(equalRoutes(expNet1.Routes, devNet1.Routes)).To(BeTrue()) |
| 313 | Expect(equalRoutes(expEth0.Routes, devEth0.Routes)).To(BeTrue()) |
| 314 | |
| 315 | conf = `{ |
| 316 | "cniVersion": "0.3.0", |
| 317 | "name": "cni-plugin-sbr-test", |
| 318 | "type": "sbr" |
| 319 | }` |
| 320 | |
| 321 | // And now check that we can back it all out. |
| 322 | args = &skel.CmdArgs{ |
| 323 | ContainerID: "dummy", |
| 324 | Netns: targetNs.Path(), |
| 325 | IfName: ifname, |
| 326 | StdinData: []byte(conf), |
| 327 | } |
| 328 | err = testutils.CmdDelWithArgs(args, func() error { return cmdDel(args) }) |
| 329 | Expect(err).NotTo(HaveOccurred()) |
| 330 | |
| 331 | retVal, err := readback(targetNs, []string{"net1", "eth0"}) |
| 332 | Expect(err).NotTo(HaveOccurred()) |
| 333 | |
| 334 | // Check results. We expect the rule to have been removed. |
| 335 | Expect(retVal.Rules).To(BeEmpty()) |
| 336 | }) |
| 337 | |
| 338 | It("Works with a default route already set", func() { |
| 339 | ifname := "net1" |
| 340 | conf := `{ |
| 341 | "cniVersion": "0.3.0", |
| 342 | "name": "cni-plugin-sbr-test", |
| 343 | "type": "sbr", |
| 344 | "prevResult": { |
| 345 | "cniVersion": "0.3.0", |
| 346 | "interfaces": [ |
| 347 | { |
| 348 | "name": "%s", |
| 349 | "sandbox": "%s" |
| 350 | } |
| 351 | ], |
| 352 | "ips": [ |
| 353 | { |
| 354 | "version": "4", |
| 355 | "address": "192.168.1.209/24", |
| 356 | "gateway": "192.168.1.1", |
| 357 | "interface": 0 |
| 358 | } |
| 359 | ], |
| 360 | "routes": [] |
| 361 | } |
| 362 | }` |
| 363 | conf = fmt.Sprintf(conf, ifname, targetNs.Path()) |
| 364 | args := &skel.CmdArgs{ |
| 365 | ContainerID: "dummy", |
| 366 | Netns: targetNs.Path(), |
| 367 | IfName: ifname, |
| 368 | StdinData: []byte(conf), |
| 369 | } |
| 370 | |
| 371 | preStatus := createDefaultStatus() |
| 372 | // Remove final (default) route from eth0, then add another default |
| 373 | // route to net1 |
| 374 | preStatus.Devices[0].Routes = preStatus.Devices[0].Routes[:0] |
| 375 | routes := preStatus.Devices[1].Routes |
| 376 | preStatus.Devices[1].Routes = append(preStatus.Devices[1].Routes, |
| 377 | netlink.Route{ |
| 378 | Gw: net.IPv4(192, 168, 1, 1), |
| 379 | LinkIndex: routes[0].LinkIndex, |
| 380 | }) |
| 381 | |
| 382 | err := setup(targetNs, preStatus) |
| 383 | Expect(err).NotTo(HaveOccurred()) |
| 384 | |
| 385 | oldStatus, err := readback(targetNs, []string{"net1", "eth0"}) |
| 386 | Expect(err).NotTo(HaveOccurred()) |
| 387 | |
| 388 | _, _, err = testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) }) |
| 389 | Expect(err).NotTo(HaveOccurred()) |
| 390 | |
| 391 | newStatus, err := readback(targetNs, []string{"net1", "eth0"}) |
| 392 | Expect(err).NotTo(HaveOccurred()) |
| 393 | |
| 394 | // Check results. We expect all the routes on net1 to have moved to |
| 395 | // table 100 except for local routes (table 255); a new default gateway |
| 396 | // route to have been created; and a single rule to exist. |
| 397 | expNet1 := oldStatus.Devices[0] |
| 398 | expEth0 := oldStatus.Devices[1] |
| 399 | for i := range expNet1.Routes { |
| 400 | if expNet1.Routes[i].Table != 255 { |
| 401 | expNet1.Routes[i].Table = 100 |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | Expect(newStatus.Rules).To(HaveLen(1)) |
| 406 | Expect(newStatus.Rules[0].Table).To(Equal(100)) |
| 407 | Expect(newStatus.Rules[0].Src.String()).To(Equal("192.168.1.209/32")) |
| 408 | devNet1 := newStatus.Devices[0] |
| 409 | devEth0 := newStatus.Devices[1] |
| 410 | Expect(equalRoutes(expEth0.Routes, devEth0.Routes)).To(BeTrue()) |
| 411 | Expect(equalRoutes(expNet1.Routes, devNet1.Routes)).To(BeTrue()) |
| 412 | }) |
| 413 | |
| 414 | It("Works with multiple IPs", func() { |
| 415 | ifname := "net1" |
| 416 | conf := `{ |
| 417 | "cniVersion": "0.3.0", |
| 418 | "name": "cni-plugin-sbr-test", |
| 419 | "type": "sbr", |
| 420 | "prevResult": { |
| 421 | "cniVersion": "0.3.0", |
| 422 | "interfaces": [ |
| 423 | { |
| 424 | "name": "%s", |
| 425 | "sandbox": "%s" |
| 426 | } |
| 427 | ], |
| 428 | "ips": [ |
| 429 | { |
| 430 | "version": "4", |
| 431 | "address": "192.168.1.209/24", |
| 432 | "gateway": "192.168.1.1", |
| 433 | "interface": 0 |
| 434 | }, |
| 435 | { |
| 436 | "version": "4", |
| 437 | "address": "192.168.101.209/24", |
| 438 | "gateway": "192.168.101.1", |
| 439 | "interface": 1 |
| 440 | } |
| 441 | ], |
| 442 | "routes": [] |
| 443 | } |
| 444 | }` |
| 445 | conf = fmt.Sprintf(conf, ifname, targetNs.Path()) |
| 446 | args := &skel.CmdArgs{ |
| 447 | ContainerID: "dummy", |
| 448 | Netns: targetNs.Path(), |
| 449 | IfName: ifname, |
| 450 | StdinData: []byte(conf), |
| 451 | } |
| 452 | |
| 453 | preStatus := createDefaultStatus() |
| 454 | |
| 455 | // Add the second IP on net1 interface |
| 456 | preStatus.Devices[1].Addrs = append(preStatus.Devices[1].Addrs, |
| 457 | net.IPNet{ |
| 458 | IP: net.IPv4(192, 168, 101, 209), |
| 459 | Mask: net.IPv4Mask(255, 255, 255, 0), |
| 460 | }) |
| 461 | |
| 462 | err := setup(targetNs, preStatus) |
| 463 | Expect(err).NotTo(HaveOccurred()) |
| 464 | |
| 465 | oldStatus, err := readback(targetNs, []string{"net1", "eth0"}) |
| 466 | Expect(err).NotTo(HaveOccurred()) |
| 467 | |
| 468 | _, _, err = testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) }) |
| 469 | Expect(err).NotTo(HaveOccurred()) |
| 470 | |
| 471 | newStatus, err := readback(targetNs, []string{"net1", "eth0"}) |
| 472 | Expect(err).NotTo(HaveOccurred()) |
| 473 | |
| 474 | // Check results. We expect all the routes on net1 to have moved to |
| 475 | // table 100 except for local routes (table 255); a new default gateway |
| 476 | // route to have been created; and 2 rules to exist. |
| 477 | expNet1 := oldStatus.Devices[0] |
| 478 | expEth0 := oldStatus.Devices[1] |
| 479 | |
| 480 | for i := range expNet1.Routes { |
| 481 | if expNet1.Routes[i].Table != 255 { |
| 482 | if expNet1.Routes[i].Src.String() == "192.168.101.209" { |
| 483 | // All 192.168.101.x routes expected in table 101 |
| 484 | expNet1.Routes[i].Table = 101 |
| 485 | } else if expNet1.Routes[i].Src == nil && expNet1.Routes[i].Gw == nil { |
| 486 | // Generic Link Local Addresses assigned. They should exist in all |
| 487 | // route tables |
| 488 | expNet1.Routes[i].Table = 100 |
| 489 | expNet1.Routes = append(expNet1.Routes, |
| 490 | netlink.Route{ |
| 491 | Dst: expNet1.Routes[i].Dst, |
| 492 | Table: 101, |
| 493 | LinkIndex: expNet1.Routes[i].LinkIndex, |
| 494 | }) |
| 495 | } else { |
| 496 | // All 192.168.1.x routes expected in table 100 |
| 497 | expNet1.Routes[i].Table = 100 |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | expNet1.Routes = append(expNet1.Routes, |
| 502 | netlink.Route{ |
| 503 | Dst: &net.IPNet{IP: net.IPv4zero, Mask: net.IPMask(net.IPv4zero)}, |
| 504 | Gw: net.IPv4(192, 168, 1, 1), |
| 505 | Table: 100, |
| 506 | LinkIndex: expNet1.Routes[0].LinkIndex, |
| 507 | }) |
| 508 | |
| 509 | expNet1.Routes = append(expNet1.Routes, |
| 510 | netlink.Route{ |
| 511 | Dst: &net.IPNet{IP: net.IPv4zero, Mask: net.IPMask(net.IPv4zero)}, |
| 512 | Gw: net.IPv4(192, 168, 101, 1), |
| 513 | Table: 101, |
| 514 | LinkIndex: expNet1.Routes[0].LinkIndex, |
| 515 | }) |
| 516 | |
| 517 | // 2 Rules will be created for each IP address. (100, 101) |
| 518 | Expect(newStatus.Rules).To(HaveLen(2)) |
| 519 | |
| 520 | // First entry corresponds to last table |
| 521 | Expect(newStatus.Rules[0].Table).To(Equal(101)) |
| 522 | Expect(newStatus.Rules[0].Src.String()).To(Equal("192.168.101.209/32")) |
| 523 | |
| 524 | // Second entry corresponds to first table (100) |
| 525 | Expect(newStatus.Rules[1].Table).To(Equal(100)) |
| 526 | Expect(newStatus.Rules[1].Src.String()).To(Equal("192.168.1.209/32")) |
| 527 | |
| 528 | devNet1 := newStatus.Devices[0] |
| 529 | devEth0 := newStatus.Devices[1] |
| 530 | Expect(equalRoutes(expNet1.Routes, devNet1.Routes)).To(BeTrue()) |
| 531 | Expect(equalRoutes(expEth0.Routes, devEth0.Routes)).To(BeTrue()) |
| 532 | }) |
| 533 | |
| 534 | It("fails with CNI spec versions that don't support plugin chaining", func() { |
| 535 | conf := `{ |
| 536 | "cniVersion": "0.2.0", |
| 537 | "name": "cni-plugin-sbr-test", |
| 538 | "type": "sbr", |
| 539 | "anotherAwesomeArg": "foo" |
| 540 | }` |
| 541 | |
| 542 | args := &skel.CmdArgs{ |
| 543 | ContainerID: "dummy", |
| 544 | Netns: targetNs.Path(), |
| 545 | IfName: "net1", |
| 546 | StdinData: []byte(conf), |
| 547 | } |
| 548 | err := setup(targetNs, createDefaultStatus()) |
| 549 | Expect(err).NotTo(HaveOccurred()) |
| 550 | |
| 551 | _, _, err = testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) }) |
| 552 | Expect(err).To(MatchError("This plugin must be called as chained plugin")) |
| 553 | }) |
| 554 | |
| 555 | It("Works with Table ID", func() { |
| 556 | ifname := "net1" |
| 557 | tableID := 5000 |
| 558 | conf := `{ |
| 559 | "cniVersion": "0.3.0", |
| 560 | "name": "cni-plugin-sbr-test", |
| 561 | "type": "sbr", |
| 562 | "table": %d, |
| 563 | "prevResult": { |
| 564 | "cniVersion": "0.3.0", |
| 565 | "interfaces": [ |
| 566 | { |
| 567 | "name": "%s", |
| 568 | "sandbox": "%s" |
| 569 | } |
| 570 | ], |
| 571 | "ips": [ |
| 572 | { |
| 573 | "address": "192.168.1.209/24", |
| 574 | "interface": 0 |
| 575 | }, |
| 576 | { |
| 577 | "address": "192.168.101.209/24", |
| 578 | "interface": 0 |
| 579 | } |
| 580 | ], |
| 581 | "routes": [] |
| 582 | } |
| 583 | }` |
| 584 | conf = fmt.Sprintf(conf, tableID, ifname, targetNs.Path()) |
| 585 | args := &skel.CmdArgs{ |
| 586 | ContainerID: "dummy", |
| 587 | Netns: targetNs.Path(), |
| 588 | IfName: ifname, |
| 589 | StdinData: []byte(conf), |
| 590 | } |
| 591 | |
| 592 | preStatus := createDefaultStatus() |
| 593 | |
| 594 | err := setup(targetNs, preStatus) |
| 595 | Expect(err).NotTo(HaveOccurred()) |
| 596 | |
| 597 | oldStatus, err := readback(targetNs, []string{"net1", "eth0"}) |
| 598 | Expect(err).NotTo(HaveOccurred()) |
| 599 | |
| 600 | _, _, err = testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) }) |
| 601 | Expect(err).NotTo(HaveOccurred()) |
| 602 | |
| 603 | newStatus, err := readback(targetNs, []string{"net1", "eth0"}) |
| 604 | Expect(err).NotTo(HaveOccurred()) |
| 605 | |
| 606 | // Routes have not been moved. |
| 607 | Expect(newStatus).To(Equal(oldStatus)) |
| 608 | |
| 609 | // Fetch all rules for the requested table ID. |
| 610 | var rules []netlink.Rule |
| 611 | err = targetNs.Do(func(_ ns.NetNS) error { |
| 612 | var err error |
| 613 | rules, err = netlinksafe.RuleListFiltered( |
| 614 | netlink.FAMILY_ALL, &netlink.Rule{ |
| 615 | Table: tableID, |
| 616 | }, |
| 617 | netlink.RT_FILTER_TABLE, |
| 618 | ) |
| 619 | return err |
| 620 | }) |
| 621 | |
| 622 | Expect(err).NotTo(HaveOccurred()) |
| 623 | Expect(rules).To(HaveLen(2)) |
| 624 | |
| 625 | // Both IPs have been added as source based routes with requested table ID. |
| 626 | Expect(rules[0].Table).To(Equal(tableID)) |
| 627 | Expect(rules[0].Src.String()).To(Equal("192.168.101.209/32")) |
| 628 | Expect(rules[1].Table).To(Equal(tableID)) |
| 629 | Expect(rules[1].Src.String()).To(Equal("192.168.1.209/32")) |
| 630 | }) |
| 631 | }) |