joinBridge joins the given network namespace to the bridge. It creates a veth pair between the specified NetNS and the bridge NetNS, sets the IP address on the "child" veth, and brings up the interfaces.
(opts joinBridgeOpts)
| 566 | // pair between the specified NetNS and the bridge NetNS, sets the IP address on |
| 567 | // the "child" veth, and brings up the interfaces. |
| 568 | func joinBridge(opts joinBridgeOpts) error { |
| 569 | // Create outer veth pair between the router and the bridge. |
| 570 | err := createVethPair(opts.vethPair.Outer, opts.vethPair.Inner) |
| 571 | if err != nil { |
| 572 | return xerrors.Errorf("create veth pair %q <-> %q: %w", opts.vethPair.Outer, opts.vethPair.Inner, err) |
| 573 | } |
| 574 | |
| 575 | // Move the network interfaces to the respective network namespaces. |
| 576 | err = setVethNetNS(opts.vethPair.Outer, int(opts.bridgeNetNS.Fd())) |
| 577 | if err != nil { |
| 578 | return xerrors.Errorf("set veth %q to NetNS: %w", opts.vethPair.Outer, err) |
| 579 | } |
| 580 | err = setVethNetNS(opts.vethPair.Inner, int(opts.netNS.Fd())) |
| 581 | if err != nil { |
| 582 | return xerrors.Errorf("set veth %q to NetNS: %w", opts.vethPair.Inner, err) |
| 583 | } |
| 584 | |
| 585 | // Connect the outer veth to the bridge. |
| 586 | err = setInterfaceBridge(opts.bridgeNetNS, opts.vethPair.Outer, opts.bridgeName) |
| 587 | if err != nil { |
| 588 | return xerrors.Errorf("set interface %q master to %q: %w", opts.vethPair.Outer, opts.bridgeName, err) |
| 589 | } |
| 590 | |
| 591 | // Set the bridge IP on the inner veth. |
| 592 | err = setInterfaceIP(opts.netNS, opts.vethPair.Inner, opts.ip) |
| 593 | if err != nil { |
| 594 | return xerrors.Errorf("set IP %q on interface %q: %w", opts.ip, opts.vethPair.Inner, err) |
| 595 | } |
| 596 | |
| 597 | // Bring up the interfaces. |
| 598 | err = setInterfaceUp(opts.bridgeNetNS, opts.vethPair.Outer) |
| 599 | if err != nil { |
| 600 | return xerrors.Errorf("bring up interface %q: %w", opts.vethPair.Outer, err) |
| 601 | } |
| 602 | err = setInterfaceUp(opts.netNS, opts.vethPair.Inner) |
| 603 | if err != nil { |
| 604 | return xerrors.Errorf("bring up interface %q: %w", opts.vethPair.Inner, err) |
| 605 | } |
| 606 | |
| 607 | return nil |
| 608 | } |
| 609 | |
| 610 | // createNetNS creates a new network namespace with the given name. The returned |
| 611 | // file is a file descriptor to the network namespace. |
no test coverage detected