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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
|
package device
import (
"context"
"errors"
"fmt"
"io/fs"
"net"
"strings"
"time"
deviceConfig "github.com/lxc/incus/v6/internal/server/device/config"
"github.com/lxc/incus/v6/internal/server/instance"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/ip"
"github.com/lxc/incus/v6/internal/server/network"
localUtil "github.com/lxc/incus/v6/internal/server/util"
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/revert"
"github.com/lxc/incus/v6/shared/util"
"github.com/lxc/incus/v6/shared/validate"
)
var nicRoutedIPGateway = map[string]net.IP{
"ipv4": net.IPv4(169, 254, 0, 1), // 169.254.0.1
"ipv6": {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}, // fe80::1
}
type nicRouted struct {
deviceCommon
effectiveParentName string
}
// CanHotPlug returns whether the device can be managed whilst the instance is running.
func (d *nicRouted) CanHotPlug() bool {
return true
}
// UpdatableFields returns a list of fields that can be updated without triggering a device remove & add.
func (d *nicRouted) UpdatableFields(oldDevice Type) []string {
// Check old and new device types match.
_, match := oldDevice.(*nicRouted)
if !match {
return []string{}
}
return []string{"limits.ingress", "limits.egress", "limits.max", "limits.priority"}
}
// validateConfig checks the supplied config for correctness.
func (d *nicRouted) validateConfig(instConf instance.ConfigReader) error {
if !instanceSupported(instConf.Type(), instancetype.Container, instancetype.VM) {
return ErrUnsupportedDevType
}
err := d.isUniqueWithGatewayAutoMode(instConf)
if err != nil {
return err
}
requiredFields := []string{}
optionalFields := []string{
// gendoc:generate(entity=devices, group=nic_routed, key=name)
//
// ---
// type: string
// default: kernel assigned
// shortdesc: The name of the interface inside the instance
"name",
// gendoc:generate(entity=devices, group=nic_routed, key=parent)
//
// ---
// type: string
// shortdesc: The name of the parent host device to join the instance to
"parent",
// gendoc:generate(entity=devices, group=nic_routed, key=mtu)
//
// ---
// type: integer
// default: parent MTU
// shortdesc: The Maximum Transmit Unit (MTU) of the new interface
"mtu",
// gendoc:generate(entity=devices, group=nic_routed, key=queue.tx.length)
//
// ---
// type: integer
// shortdesc: The transmit queue length for the NIC
"queue.tx.length",
// gendoc:generate(entity=devices, group=nic_routed, key=hwaddr)
//
// ---
// type: string
// default: randomly assigned
// shortdesc: The MAC address of the new interface
"hwaddr",
// gendoc:generate(entity=devices, group=nic_routed, key=host_name)
//
// ---
// type: string
// default: randomly assigned
// shortdesc: The name of the interface on the host
"host_name",
// gendoc:generate(entity=devices, group=nic_routed, key=vlan)
//
// ---
// type: integer
// shortdesc: The VLAN ID to attach to
"vlan",
// gendoc:generate(entity=devices, group=nic_routed, key=limits.ingress)
//
// ---
// type: string
// shortdesc: I/O limit in bit/s for incoming traffic (various suffixes supported, see {ref}instances-limit-units)
"limits.ingress",
// gendoc:generate(entity=devices, group=nic_routed, key=limits.egress)
//
// ---
// type: string
// shortdesc: I/O limit in bit/s for outgoing traffic (various suffixes supported, see {ref}instances-limit-units)
"limits.egress",
// gendoc:generate(entity=devices, group=nic_routed, key=limits.max)
//
// ---
// type: string
// shortdesc: I/O limit in bit/s for both incoming and outgoing traffic (same as setting both limits.ingress and limits.egress)
"limits.max",
// gendoc:generate(entity=devices, group=nic_routed, key=limits.priority)
//
// ---
// type: integer
// shortdesc: The priority for outgoing traffic, to be used by the kernel queuing discipline to prioritize network packets
"limits.priority",
// gendoc:generate(entity=devices, group=nic_routed, key=ipv4.gateway)
//
// ---
// type: string
// default: auto
// shortdesc: Whether to add an automatic default IPv4 gateway (can be `auto` or `none`)
"ipv4.gateway",
// gendoc:generate(entity=devices, group=nic_routed, key=ipv6.gateway)
//
// ---
// type: string
// default: auto
// shortdesc: Whether to add an automatic default IPv6 gateway (can be `auto` or `none`)
"ipv6.gateway",
// gendoc:generate(entity=devices, group=nic_routed, key=ipv4.routes)
//
// ---
// type: string
// shortdesc: Comma-delimited list of IPv4 static routes to add on host to NIC (without L2 ARP/NDP proxy)
"ipv4.routes",
// gendoc:generate(entity=devices, group=nic_routed, key=ipv6.routes)
//
// ---
// type: string
// shortdesc: Comma-delimited list of IPv6 static routes to add on host to NIC (without L2 ARP/NDP proxy)
"ipv6.routes",
// gendoc:generate(entity=devices, group=nic_routed, key=ipv4.host_address)
//
// ---
// type: string
// default: `169.254.0.1`
// shortdesc: The IPv4 address to add to the host-side `veth` interface
"ipv4.host_address",
// gendoc:generate(entity=devices, group=nic_routed, key=ipv6.host_address)
//
// ---
// type: string
// default: `fe80::1`
// shortdesc: The IPv6 address to add to the host-side `veth` interface
"ipv6.host_address",
// gendoc:generate(entity=devices, group=nic_routed, key=ipv4.host_table)
//
// The custom policy routing table ID to add IPv4 static routes to (in addition to the main routing table)
//
// ---
// type: integer
// shortdesc: Deprecated: Use `ipv4.host_tables` instead
"ipv4.host_table",
// gendoc:generate(entity=devices, group=nic_routed, key=ipv6.host_table)
//
// The custom policy routing table ID to add IPv6 static routes to (in addition to the main routing table)
//
// ---
// type: integer
// shortdesc: Deprecated: Use `ipv6.host_tables` instead
"ipv6.host_table",
// gendoc:generate(entity=devices, group=nic_routed, key=ipv4.host_tables)
//
// ---
// type: string
// default: 254
// shortdesc: Comma-delimited list of routing tables IDs to add IPv4 static routes to
"ipv4.host_tables",
// gendoc:generate(entity=devices, group=nic_routed, key=ipv6.host_tables)
//
// ---
// type: string
// default: 254
// shortdesc: Comma-delimited list of routing tables IDs to add IPv6 static routes to
"ipv6.host_tables",
// gendoc:generate(entity=devices, group=nic_routed, key=gvrp)
//
// ---
// type: bool
// default: false
// shortdesc: Register VLAN using GARP VLAN Registration Protocol
"gvrp",
// gendoc:generate(entity=devices, group=nic_routed, key=vrf)
//
// ---
// type: string
// shortdesc: The VRF on the host in which the host-side interface and routes are created
"vrf",
// gendoc:generate(entity=devices, group=nic_routed, key=io.bus)
//
// ---
// type: string
// default: `virtio`
// shortdesc: Override the bus for the device (can be `virtio` or `usb`) (VM only)
"io.bus",
}
rules := nicValidationRules(requiredFields, optionalFields, instConf)
// gendoc:generate(entity=devices, group=nic_routed, key=ipv4.address)
//
// ---
// type: string
// shortdesc: Comma-delimited list of IPv4 static addresses to add to the instance
rules["ipv4.address"] = validate.Optional(validate.IsListOf(validate.IsNetworkAddressV4))
// gendoc:generate(entity=devices, group=nic_routed, key=ipv6.address)
//
// ---
// type: string
// shortdesc: Comma-delimited list of IPv6 static addresses to add to the instance
rules["ipv6.address"] = validate.Optional(validate.IsListOf(validate.IsNetworkAddressV6))
// gendoc:generate(entity=devices, group=nic_routed, key=ipv4.neighbor_probe)
//
// ---
// type: bool
// default: true
// shortdesc: Whether to probe the parent network for IP address availability
rules["ipv4.neighbor_probe"] = validate.Optional(validate.IsBool)
// gendoc:generate(entity=devices, group=nic_routed, key=ipv6.neighbor_probe)
//
// ---
// type: bool
// default: true
// shortdesc: Whether to probe the parent network for IP address availability
rules["ipv6.neighbor_probe"] = validate.Optional(validate.IsBool)
rules["ipv4.host_tables"] = validate.Optional(validate.IsListOf(validate.IsInRange(0, 255)))
rules["ipv6.host_tables"] = validate.Optional(validate.IsListOf(validate.IsInRange(0, 255)))
rules["gvrp"] = validate.Optional(validate.IsBool)
rules["vrf"] = validate.Optional(validate.IsAny)
err = d.config.Validate(rules)
if err != nil {
return err
}
// Detect duplicate IPs in config.
for _, key := range []string{"ipv4.address", "ipv6.address"} {
ips := make(map[string]struct{})
if d.config[key] != "" {
for _, addr := range strings.Split(d.config[key], ",") {
addr = strings.TrimSpace(addr)
_, dupe := ips[addr]
if dupe {
return fmt.Errorf("Duplicate address %q in %q", addr, key)
}
ips[addr] = struct{}{}
}
}
}
// Ensure that address is set if routes is set.
for _, keyPrefix := range []string{"ipv4", "ipv6"} {
if d.config[fmt.Sprintf("%s.routes", keyPrefix)] != "" && d.config[fmt.Sprintf("%s.address", keyPrefix)] == "" {
return fmt.Errorf("%s.routes requires %s.address to be set", keyPrefix, keyPrefix)
}
}
// Ensure that VLAN setting is only used with parent setting.
if d.config["parent"] == "" && d.config["vlan"] != "" {
return errors.New("The vlan setting can only be used when combined with a parent interface")
}
return nil
}
// validateEnvironment checks the runtime environment for correctness.
func (d *nicRouted) validateEnvironment() error {
if d.inst.Type() == instancetype.Container && d.config["name"] == "" {
return errors.New("Requires name property to start")
}
if d.config["parent"] != "" {
// Check parent interface exists (don't use d.effectiveParentName here as we want to check the
// parent of any VLAN interface exists too). The VLAN interface will be created later if needed.
if !network.InterfaceExists(d.config["parent"]) {
return fmt.Errorf("Parent device %q doesn't exist", d.config["parent"])
}
// Detect the effective parent interface that we will be using (taking into account VLAN setting).
d.effectiveParentName = network.GetHostDevice(d.config["parent"], d.config["vlan"])
// If the effective parent doesn't exist and the vlan option is specified, it means we are going to
// create the VLAN parent at start, and we will configure the needed sysctls then, so skip checks
// on the effective parent.
if d.config["vlan"] != "" && !network.InterfaceExists(d.effectiveParentName) {
return nil
}
// Check necessary "all" sysctls are configured for use with l2proxy parent for routed mode.
if d.config["ipv6.address"] != "" {
// net.ipv6.conf.all.forwarding=1 is required to enable general packet forwarding for IPv6.
ipv6FwdPath := fmt.Sprintf("net/ipv6/conf/%s/forwarding", "all")
sysctlVal, err := localUtil.SysctlGet(ipv6FwdPath)
if err != nil {
return fmt.Errorf("Error reading net sysctl %s: %w", ipv6FwdPath, err)
}
if sysctlVal != "1\n" {
return fmt.Errorf("Routed mode requires sysctl net.ipv6.conf.%s.forwarding=1", "all")
}
// net.ipv6.conf.all.proxy_ndp=1 is needed otherwise unicast neighbour solicitations are .
// rejected This causes periodic latency spikes every 15-20s as the neighbour has to resort
// to using multicast NDP resolution and expires the previous neighbour entry.
ipv6ProxyNdpPath := fmt.Sprintf("net/ipv6/conf/%s/proxy_ndp", "all")
sysctlVal, err = localUtil.SysctlGet(ipv6ProxyNdpPath)
if err != nil {
return fmt.Errorf("Error reading net sysctl %s: %w", ipv6ProxyNdpPath, err)
}
if sysctlVal != "1\n" {
return fmt.Errorf("Routed mode requires sysctl net.ipv6.conf.%s.proxy_ndp=1", "all")
}
}
// Check necessary sysctls are configured for use with l2proxy parent for routed mode.
if d.config["ipv4.address"] != "" {
ipv4FwdPath := fmt.Sprintf("net/ipv4/conf/%s/forwarding", d.effectiveParentName)
sysctlVal, err := localUtil.SysctlGet(ipv4FwdPath)
if err != nil {
return fmt.Errorf("Error reading net sysctl %s: %w", ipv4FwdPath, err)
}
if sysctlVal != "1\n" {
// Replace . in parent name with / for sysctl formatting.
return fmt.Errorf("Routed mode requires sysctl net.ipv4.conf.%s.forwarding=1", strings.ReplaceAll(d.effectiveParentName, ".", "/"))
}
}
// Check necessary device specific sysctls are configured for use with l2proxy parent for routed mode.
if d.config["ipv6.address"] != "" {
ipv6FwdPath := fmt.Sprintf("net/ipv6/conf/%s/forwarding", d.effectiveParentName)
sysctlVal, err := localUtil.SysctlGet(ipv6FwdPath)
if err != nil {
return fmt.Errorf("Error reading net sysctl %s: %w", ipv6FwdPath, err)
}
if sysctlVal != "1\n" {
// Replace . in parent name with / for sysctl formatting.
return fmt.Errorf("Routed mode requires sysctl net.ipv6.conf.%s.forwarding=1", strings.ReplaceAll(d.effectiveParentName, ".", "/"))
}
ipv6ProxyNdpPath := fmt.Sprintf("net/ipv6/conf/%s/proxy_ndp", d.effectiveParentName)
sysctlVal, err = localUtil.SysctlGet(ipv6ProxyNdpPath)
if err != nil {
return fmt.Errorf("Error reading net sysctl %s: %w", ipv6ProxyNdpPath, err)
}
if sysctlVal != "1\n" {
// Replace . in parent name with / for sysctl formatting.
return fmt.Errorf("Routed mode requires sysctl net.ipv6.conf.%s.proxy_ndp=1", strings.ReplaceAll(d.effectiveParentName, ".", "/"))
}
}
}
if d.config["vrf"] != "" {
// Check if the vrf interface exists.
if !network.InterfaceExists(d.config["vrf"]) {
return fmt.Errorf("VRF %q doesn't exist", d.config["vrf"])
}
}
return nil
}
// checkIPAvailability checks using ARP and NDP neighbour probes whether any of the NIC's IPs are already in use.
func (d *nicRouted) checkIPAvailability(parent string) error {
var addresses []net.IP
if util.IsTrueOrEmpty(d.config["ipv4.neighbor_probe"]) {
ipv4Addrs := util.SplitNTrimSpace(d.config["ipv4.address"], ",", -1, true)
for _, addr := range ipv4Addrs {
addresses = append(addresses, net.ParseIP(addr))
}
}
if util.IsTrueOrEmpty(d.config["ipv6.neighbor_probe"]) {
ipv6Addrs := util.SplitNTrimSpace(d.config["ipv6.address"], ",", -1, true)
for _, addr := range ipv6Addrs {
addresses = append(addresses, net.ParseIP(addr))
}
}
errs := make(chan error, len(addresses))
for _, address := range addresses {
go func(address net.IP) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
inUse, err := isIPAvailable(ctx, address, parent)
if err != nil {
d.logger.Warn("Failed checking IP address available on parent network", logger.Ctx{"IP": address, "parent": parent, "err": err})
}
if inUse {
errs <- fmt.Errorf("IP address %q in use on parent network %q", address, parent)
} else {
errs <- nil
}
}(address)
}
for range addresses {
err := <-errs
if err != nil {
return err
}
}
return nil
}
// Start is run when the instance is starting up (Routed mode doesn't support hot plugging).
func (d *nicRouted) Start() (*deviceConfig.RunConfig, error) {
err := d.validateEnvironment()
if err != nil {
return nil, err
}
// Lock to avoid issues with containers starting in parallel.
networkCreateSharedDeviceLock.Lock()
defer networkCreateSharedDeviceLock.Unlock()
reverter := revert.New()
defer reverter.Fail()
saveData := make(map[string]string)
// Decide which parent we should use based on VLAN setting.
if d.config["vlan"] != "" {
statusDev, err := networkCreateVlanDeviceIfNeeded(d.state, d.config["parent"], d.effectiveParentName, d.config["vlan"], util.IsTrue(d.config["gvrp"]))
if err != nil {
return nil, err
}
// Record whether we created this device or not so it can be removed on stop.
saveData["last_state.created"] = fmt.Sprintf("%t", statusDev != "existing")
// If we created a VLAN interface, we need to setup the sysctls on that interface.
if util.IsTrue(saveData["last_state.created"]) {
reverter.Add(func() {
_ = networkRemoveInterfaceIfNeeded(d.state, d.effectiveParentName, d.inst, d.config["parent"], d.config["vlan"])
})
err := d.setupParentSysctls(d.effectiveParentName)
if err != nil {
return nil, err
}
}
}
if d.effectiveParentName != "" {
err := d.checkIPAvailability(d.effectiveParentName)
if err != nil {
return nil, err
}
}
saveData["host_name"] = d.config["host_name"]
var peerName string
var mtu uint32
// Create veth pair and configure the peer end with custom hwaddr and mtu if supplied.
if d.inst.Type() == instancetype.Container {
if saveData["host_name"] == "" {
saveData["host_name"], err = d.generateHostName("veth", d.config["hwaddr"])
if err != nil {
return nil, err
}
}
peerName, mtu, err = networkCreateVethPair(saveData["host_name"], d.config)
} else if d.inst.Type() == instancetype.VM {
if saveData["host_name"] == "" {
saveData["host_name"], err = d.generateHostName("tap", d.config["hwaddr"])
if err != nil {
return nil, err
}
}
peerName = saveData["host_name"] // VMs use the host_name to link to the TAP FD.
mtu, err = networkCreateTap(saveData["host_name"], d.config)
}
if err != nil {
return nil, err
}
reverter.Add(func() { _ = network.InterfaceRemove(saveData["host_name"]) })
// Populate device config with volatile fields if needed.
networkVethFillFromVolatile(d.config, saveData)
// Apply host-side limits.
err = networkSetupHostVethLimits(&d.deviceCommon, nil, false)
if err != nil {
return nil, err
}
// Attempt to disable IPv6 router advertisement acceptance from instance.
err = localUtil.SysctlSet(fmt.Sprintf("net/ipv6/conf/%s/accept_ra", saveData["host_name"]), "0")
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
// Prevent source address spoofing by requiring a return path.
err = localUtil.SysctlSet(fmt.Sprintf("net/ipv4/conf/%s/rp_filter", saveData["host_name"]), "1")
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
// Apply firewall rules for reverse path filtering of IPv4 and IPv6.
err = d.state.Firewall.InstanceSetupRPFilter(d.inst.Project().Name, d.inst.Name(), d.name, saveData["host_name"])
if err != nil {
return nil, fmt.Errorf("Error setting up reverse path filter: %w", err)
}
// Perform host-side address configuration.
for _, keyPrefix := range []string{"ipv4", "ipv6"} {
subnetSize := 32
ipFamilyArg := ip.FamilyV4
if keyPrefix == "ipv6" {
subnetSize = 128
ipFamilyArg = ip.FamilyV6
}
addresses := util.SplitNTrimSpace(d.config[fmt.Sprintf("%s.address", keyPrefix)], ",", -1, true)
// Add host-side gateway addresses.
if len(addresses) > 0 {
// Add gateway IPs to the host end of the veth pair. This ensures that liveness detection
// of the gateways inside the instance work and ensure that traffic doesn't periodically
// halt whilst ARP/NDP is re-detected (which is what happens with just neighbour proxies).
addr := &ip.Addr{
DevName: saveData["host_name"],
Address: &net.IPNet{
IP: d.ipHostAddress(keyPrefix),
Mask: net.CIDRMask(subnetSize, subnetSize),
},
Family: ipFamilyArg,
}
err = addr.Add()
if err != nil {
return nil, fmt.Errorf("Failed adding host gateway IP %q: %w", addr.Address, err)
}
// Enable IP forwarding on host_name.
err = localUtil.SysctlSet(fmt.Sprintf("net/%s/conf/%s/forwarding", keyPrefix, saveData["host_name"]), "1")
if err != nil {
return nil, err
}
}
getTables := func() []string {
// New plural form – honour exactly what the user gives.
v := d.config[fmt.Sprintf("%s.host_tables", keyPrefix)]
if v != "" {
return util.SplitNTrimSpace(v, ",", -1, true)
}
// Legacy – single key: include it plus 254.
v = d.config[fmt.Sprintf("%s.host_table", keyPrefix)]
if v != "" {
if v == "254" {
return []string{"254"} // user asked for main only
}
return []string{v, "254"} // custom + main
}
// Default – main only.
return []string{"254"}
}
tables := getTables()
// Perform per-address host-side configuration (static routes and neighbour proxy entries).
for _, addrStr := range addresses {
// Apply host-side static routes to main routing table or VRF.
address := net.ParseIP(addrStr)
if address == nil {
return nil, fmt.Errorf("Invalid address %q", addrStr)
}
// If a VRF is set we still add a route into the VRF's own table (empty Table value).
if d.config["vrf"] != "" {
r := ip.Route{
DevName: saveData["host_name"],
Route: &net.IPNet{
IP: address,
Mask: net.CIDRMask(subnetSize, subnetSize),
},
Table: "",
Family: ipFamilyArg,
VRF: d.config["vrf"],
}
err = r.Add()
if err != nil {
return nil, fmt.Errorf("Failed adding host route %q: %w", r.Route, err)
}
}
// Add routes to all requested tables.
for _, tbl := range tables {
r := ip.Route{
DevName: saveData["host_name"],
Route: &net.IPNet{
IP: address,
Mask: net.CIDRMask(subnetSize, subnetSize),
},
Table: tbl,
Family: ipFamilyArg,
}
err = r.Add()
if err != nil {
return nil, fmt.Errorf("Failed adding host route %q to table %q: %w", r.Route, r.Table, err)
}
}
// If there is a parent interface, add neighbour proxy entry.
if d.effectiveParentName != "" {
np := ip.NeighProxy{
DevName: d.effectiveParentName,
Addr: net.ParseIP(addrStr),
}
err = np.Add()
if err != nil {
return nil, fmt.Errorf("Failed adding neighbour proxy %q to %q: %w", np.Addr.String(), np.DevName, err)
}
reverter.Add(func() { _ = np.Delete() })
}
}
if d.config[fmt.Sprintf("%s.routes", keyPrefix)] != "" {
routes := util.SplitNTrimSpace(d.config[fmt.Sprintf("%s.routes", keyPrefix)], ",", -1, true)
if len(addresses) == 0 {
return nil, fmt.Errorf("%s.routes requires %s.address to be set", keyPrefix, keyPrefix)
}
viaAddress := net.ParseIP(addresses[0])
if viaAddress == nil {
return nil, fmt.Errorf("Invalid address %q", addresses[0])
}
// Add routes
for _, routeStr := range routes {
route, err := ip.ParseIPNet(routeStr)
if err != nil {
return nil, fmt.Errorf("Invalid route %q: %w", routeStr, err)
}
// If a VRF is set we still add a route into the VRF's own table (empty Table value).
if d.config["vrf"] != "" {
r := ip.Route{
DevName: saveData["host_name"],
Route: route,
Table: "",
Family: ipFamilyArg,
Via: viaAddress,
VRF: d.config["vrf"],
}
err = r.Add()
if err != nil {
return nil, fmt.Errorf("Failed adding route %q: %w", r.Route, err)
}
}
// Add routes to all requested tables.
for _, tbl := range tables {
r := ip.Route{
DevName: saveData["host_name"],
Route: route,
Table: tbl,
Family: ipFamilyArg,
Via: viaAddress,
}
err = r.Add()
if err != nil {
return nil, fmt.Errorf("Failed adding route %q to table %q: %w", r.Route, r.Table, err)
}
}
}
}
}
err = d.volatileSet(saveData)
if err != nil {
return nil, err
}
// Perform instance NIC configuration.
runConf := deviceConfig.RunConfig{}
runConf.NetworkInterface = []deviceConfig.RunConfigItem{
{Key: "type", Value: "phys"},
{Key: "name", Value: d.config["name"]},
{Key: "flags", Value: "up"},
{Key: "link", Value: peerName},
{Key: "hwaddr", Value: d.config["hwaddr"]},
}
if d.config["io.bus"] == "usb" {
runConf.UseUSBBus = true
}
if d.inst.Type() == instancetype.Container {
for _, keyPrefix := range []string{"ipv4", "ipv6"} {
ipAddresses := util.SplitNTrimSpace(d.config[fmt.Sprintf("%s.address", keyPrefix)], ",", -1, true)
// Use a fixed address as the auto next-hop default gateway if using this IP family.
if len(ipAddresses) > 0 && nicHasAutoGateway(d.config[fmt.Sprintf("%s.gateway", keyPrefix)]) {
runConf.NetworkInterface = append(runConf.NetworkInterface,
deviceConfig.RunConfigItem{Key: fmt.Sprintf("%s.gateway", keyPrefix), Value: d.ipHostAddress(keyPrefix).String()},
)
}
for _, addrStr := range ipAddresses {
// Add addresses to instance NIC.
if keyPrefix == "ipv6" {
runConf.NetworkInterface = append(runConf.NetworkInterface,
deviceConfig.RunConfigItem{Key: "ipv6.address", Value: fmt.Sprintf("%s/128", addrStr)},
)
} else {
// Specify the broadcast address as 0.0.0.0 as there is no broadcast address on
// this link. This stops liblxc from trying to calculate a broadcast address
// (and getting it wrong) which can prevent instances communicating with each other
// using adjacent IP addresses.
runConf.NetworkInterface = append(runConf.NetworkInterface,
deviceConfig.RunConfigItem{Key: "ipv4.address", Value: fmt.Sprintf("%s/32 0.0.0.0", addrStr)},
)
}
}
}
} else if d.inst.Type() == instancetype.VM {
runConf.NetworkInterface = append(runConf.NetworkInterface, []deviceConfig.RunConfigItem{
{Key: "devName", Value: d.name},
{Key: "mtu", Value: fmt.Sprintf("%d", mtu)},
}...)
}
reverter.Success()
return &runConf, nil
}
// setupParentSysctls configures the required sysctls on the parent to allow l2proxy to work.
// Because of our policy not to modify sysctls on existing interfaces, this should only be called
// if we created the parent interface.
func (d *nicRouted) setupParentSysctls(parentName string) error {
if d.config["ipv4.address"] != "" {
// Set necessary sysctls for use with l2proxy parent in routed mode.
ipv4FwdPath := fmt.Sprintf("net/ipv4/conf/%s/forwarding", parentName)
err := localUtil.SysctlSet(ipv4FwdPath, "1")
if err != nil {
return fmt.Errorf("Error setting net sysctl %s: %w", ipv4FwdPath, err)
}
}
if d.config["ipv6.address"] != "" {
// Set necessary sysctls use with l2proxy parent in routed mode.
ipv6FwdPath := fmt.Sprintf("net/ipv6/conf/%s/forwarding", parentName)
err := localUtil.SysctlSet(ipv6FwdPath, "1")
if err != nil {
return fmt.Errorf("Error setting net sysctl %s: %w", ipv6FwdPath, err)
}
ipv6ProxyNdpPath := fmt.Sprintf("net/ipv6/conf/%s/proxy_ndp", parentName)
err = localUtil.SysctlSet(ipv6ProxyNdpPath, "1")
if err != nil {
return fmt.Errorf("Error setting net sysctl %s: %w", ipv6ProxyNdpPath, err)
}
}
return nil
}
// Update returns an error as most devices do not support live updates without being restarted.
func (d *nicRouted) Update(oldDevices deviceConfig.Devices, isRunning bool) error {
v := d.volatileGet()
// If instance is running, apply host side limits.
if isRunning {
err := d.validateEnvironment()
if err != nil {
return err
}
// Populate device config with volatile fields if needed.
networkVethFillFromVolatile(d.config, v)
// Apply host-side limits.
err = networkSetupHostVethLimits(&d.deviceCommon, oldDevices[d.name], false)
if err != nil {
return err
}
}
return nil
}
// Stop is run when the device is removed from the instance.
func (d *nicRouted) Stop() (*deviceConfig.RunConfig, error) {
// Populate device config with volatile fields (hwaddr and host_name) if needed.
networkVethFillFromVolatile(d.config, d.volatileGet())
err := networkClearHostVethLimits(&d.deviceCommon)
if err != nil {
return nil, err
}
runConf := deviceConfig.RunConfig{
PostHooks: []func() error{d.postStop},
}
return &runConf, nil
}
// postStop is run after the device is removed from the instance.
func (d *nicRouted) postStop() error {
defer func() {
_ = d.volatileSet(map[string]string{
"last_state.created": "",
"host_name": "",
})
}()
errs := []error{}
v := d.volatileGet()
networkVethFillFromVolatile(d.config, v)
if d.config["parent"] != "" {
d.effectiveParentName = network.GetHostDevice(d.config["parent"], d.config["vlan"])
}
// Delete host-side interface.
if network.InterfaceExists(d.config["host_name"]) {
// Removing host-side end of veth pair will delete the peer end too.
err := network.InterfaceRemove(d.config["host_name"])
if err != nil {
errs = append(errs, fmt.Errorf("Failed to remove interface %q: %w", d.config["host_name"], err))
}
}
// Delete IP neighbour proxy entries on the parent.
if d.effectiveParentName != "" {
for _, key := range []string{"ipv4.address", "ipv6.address"} {
for _, addr := range util.SplitNTrimSpace(d.config[key], ",", -1, true) {
neighProxy := &ip.NeighProxy{
DevName: d.effectiveParentName,
Addr: net.ParseIP(addr),
}
_ = neighProxy.Delete()
}
}
}
// This will delete the parent interface if we created it for VLAN parent.
if util.IsTrue(v["last_state.created"]) {
err := networkRemoveInterfaceIfNeeded(d.state, d.effectiveParentName, d.inst, d.config["parent"], d.config["vlan"])
if err != nil {
errs = append(errs, err)
}
}
// Remove reverse path filters.
err := d.state.Firewall.InstanceClearRPFilter(d.inst.Project().Name, d.inst.Name(), d.name)
if err != nil {
errs = append(errs, err)
}
if len(errs) > 0 {
return fmt.Errorf("%v", errs)
}
return nil
}
func (d *nicRouted) ipHostAddress(ipFamily string) net.IP {
key := fmt.Sprintf("%s.host_address", ipFamily)
if d.config[key] != "" {
return net.ParseIP(d.config[key])
}
return nicRoutedIPGateway[ipFamily]
}
func (d *nicRouted) isUniqueWithGatewayAutoMode(instConf instance.ConfigReader) error {
instDevs := instConf.ExpandedDevices()
for _, k := range []string{"ipv4.gateway", "ipv6.gateway"} {
if d.config[k] != "auto" && d.config[k] != "" {
continue // nothing to do as auto not being used.
}
// Check other routed NIC devices don't have auto set.
for nicName, nicConfig := range instDevs {
if nicName == d.name || nicConfig["nictype"] != "routed" {
continue // Skip ourselves.
}
if nicConfig[k] == "auto" || nicConfig[k] == "" {
return fmt.Errorf("Existing NIC %q already uses %q in auto mode", nicName, k)
}
}
}
return nil
}
|