github.com/vishvananda/netlink@v1.3.0/link.go (about) 1 package netlink 2 3 import ( 4 "fmt" 5 "net" 6 "os" 7 "strconv" 8 ) 9 10 // Link represents a link device from netlink. Shared link attributes 11 // like name may be retrieved using the Attrs() method. Unique data 12 // can be retrieved by casting the object to the proper type. 13 type Link interface { 14 Attrs() *LinkAttrs 15 Type() string 16 } 17 18 type ( 19 NsPid int 20 NsFd int 21 ) 22 23 // LinkAttrs represents data shared by most link types 24 type LinkAttrs struct { 25 Index int 26 MTU int 27 TxQLen int // Transmit Queue Length 28 Name string 29 HardwareAddr net.HardwareAddr 30 Flags net.Flags 31 RawFlags uint32 32 ParentIndex int // index of the parent link device 33 MasterIndex int // must be the index of a bridge 34 Namespace interface{} // nil | NsPid | NsFd 35 Alias string 36 AltNames []string 37 Statistics *LinkStatistics 38 Promisc int 39 Allmulti int 40 Multi int 41 Xdp *LinkXdp 42 EncapType string 43 Protinfo *Protinfo 44 OperState LinkOperState 45 PhysSwitchID int 46 NetNsID int 47 NumTxQueues int 48 NumRxQueues int 49 TSOMaxSegs uint32 50 TSOMaxSize uint32 51 GSOMaxSegs uint32 52 GSOMaxSize uint32 53 GROMaxSize uint32 54 GSOIPv4MaxSize uint32 55 GROIPv4MaxSize uint32 56 Vfs []VfInfo // virtual functions available on link 57 Group uint32 58 PermHWAddr net.HardwareAddr 59 Slave LinkSlave 60 } 61 62 // LinkSlave represents a slave device. 63 type LinkSlave interface { 64 SlaveType() string 65 } 66 67 // VfInfo represents configuration of virtual function 68 type VfInfo struct { 69 ID int 70 Mac net.HardwareAddr 71 Vlan int 72 Qos int 73 VlanProto int 74 TxRate int // IFLA_VF_TX_RATE Max TxRate 75 Spoofchk bool 76 LinkState uint32 77 MaxTxRate uint32 // IFLA_VF_RATE Max TxRate 78 MinTxRate uint32 // IFLA_VF_RATE Min TxRate 79 RxPackets uint64 80 TxPackets uint64 81 RxBytes uint64 82 TxBytes uint64 83 Multicast uint64 84 Broadcast uint64 85 RxDropped uint64 86 TxDropped uint64 87 88 RssQuery uint32 89 Trust uint32 90 } 91 92 // LinkOperState represents the values of the IFLA_OPERSTATE link 93 // attribute, which contains the RFC2863 state of the interface. 94 type LinkOperState uint8 95 96 const ( 97 OperUnknown = iota // Status can't be determined. 98 OperNotPresent // Some component is missing. 99 OperDown // Down. 100 OperLowerLayerDown // Down due to state of lower layer. 101 OperTesting // In some test mode. 102 OperDormant // Not up but pending an external event. 103 OperUp // Up, ready to send packets. 104 ) 105 106 func (s LinkOperState) String() string { 107 switch s { 108 case OperNotPresent: 109 return "not-present" 110 case OperDown: 111 return "down" 112 case OperLowerLayerDown: 113 return "lower-layer-down" 114 case OperTesting: 115 return "testing" 116 case OperDormant: 117 return "dormant" 118 case OperUp: 119 return "up" 120 default: 121 return "unknown" 122 } 123 } 124 125 // NewLinkAttrs returns LinkAttrs structure filled with default values 126 func NewLinkAttrs() LinkAttrs { 127 return LinkAttrs{ 128 NetNsID: -1, 129 TxQLen: -1, 130 } 131 } 132 133 type LinkStatistics LinkStatistics64 134 135 /* 136 Ref: struct rtnl_link_stats {...} 137 */ 138 type LinkStatistics32 struct { 139 RxPackets uint32 140 TxPackets uint32 141 RxBytes uint32 142 TxBytes uint32 143 RxErrors uint32 144 TxErrors uint32 145 RxDropped uint32 146 TxDropped uint32 147 Multicast uint32 148 Collisions uint32 149 RxLengthErrors uint32 150 RxOverErrors uint32 151 RxCrcErrors uint32 152 RxFrameErrors uint32 153 RxFifoErrors uint32 154 RxMissedErrors uint32 155 TxAbortedErrors uint32 156 TxCarrierErrors uint32 157 TxFifoErrors uint32 158 TxHeartbeatErrors uint32 159 TxWindowErrors uint32 160 RxCompressed uint32 161 TxCompressed uint32 162 } 163 164 func (s32 LinkStatistics32) to64() *LinkStatistics64 { 165 return &LinkStatistics64{ 166 RxPackets: uint64(s32.RxPackets), 167 TxPackets: uint64(s32.TxPackets), 168 RxBytes: uint64(s32.RxBytes), 169 TxBytes: uint64(s32.TxBytes), 170 RxErrors: uint64(s32.RxErrors), 171 TxErrors: uint64(s32.TxErrors), 172 RxDropped: uint64(s32.RxDropped), 173 TxDropped: uint64(s32.TxDropped), 174 Multicast: uint64(s32.Multicast), 175 Collisions: uint64(s32.Collisions), 176 RxLengthErrors: uint64(s32.RxLengthErrors), 177 RxOverErrors: uint64(s32.RxOverErrors), 178 RxCrcErrors: uint64(s32.RxCrcErrors), 179 RxFrameErrors: uint64(s32.RxFrameErrors), 180 RxFifoErrors: uint64(s32.RxFifoErrors), 181 RxMissedErrors: uint64(s32.RxMissedErrors), 182 TxAbortedErrors: uint64(s32.TxAbortedErrors), 183 TxCarrierErrors: uint64(s32.TxCarrierErrors), 184 TxFifoErrors: uint64(s32.TxFifoErrors), 185 TxHeartbeatErrors: uint64(s32.TxHeartbeatErrors), 186 TxWindowErrors: uint64(s32.TxWindowErrors), 187 RxCompressed: uint64(s32.RxCompressed), 188 TxCompressed: uint64(s32.TxCompressed), 189 } 190 } 191 192 /* 193 Ref: struct rtnl_link_stats64 {...} 194 */ 195 type LinkStatistics64 struct { 196 RxPackets uint64 197 TxPackets uint64 198 RxBytes uint64 199 TxBytes uint64 200 RxErrors uint64 201 TxErrors uint64 202 RxDropped uint64 203 TxDropped uint64 204 Multicast uint64 205 Collisions uint64 206 RxLengthErrors uint64 207 RxOverErrors uint64 208 RxCrcErrors uint64 209 RxFrameErrors uint64 210 RxFifoErrors uint64 211 RxMissedErrors uint64 212 TxAbortedErrors uint64 213 TxCarrierErrors uint64 214 TxFifoErrors uint64 215 TxHeartbeatErrors uint64 216 TxWindowErrors uint64 217 RxCompressed uint64 218 TxCompressed uint64 219 } 220 221 type LinkXdp struct { 222 Fd int 223 Attached bool 224 AttachMode uint32 225 Flags uint32 226 ProgId uint32 227 } 228 229 // Device links cannot be created via netlink. These links 230 // are links created by udev like 'lo' and 'etho0' 231 type Device struct { 232 LinkAttrs 233 } 234 235 func (device *Device) Attrs() *LinkAttrs { 236 return &device.LinkAttrs 237 } 238 239 func (device *Device) Type() string { 240 return "device" 241 } 242 243 // Dummy links are dummy ethernet devices 244 type Dummy struct { 245 LinkAttrs 246 } 247 248 func (dummy *Dummy) Attrs() *LinkAttrs { 249 return &dummy.LinkAttrs 250 } 251 252 func (dummy *Dummy) Type() string { 253 return "dummy" 254 } 255 256 // Ifb links are advanced dummy devices for packet filtering 257 type Ifb struct { 258 LinkAttrs 259 } 260 261 func (ifb *Ifb) Attrs() *LinkAttrs { 262 return &ifb.LinkAttrs 263 } 264 265 func (ifb *Ifb) Type() string { 266 return "ifb" 267 } 268 269 // Bridge links are simple linux bridges 270 type Bridge struct { 271 LinkAttrs 272 MulticastSnooping *bool 273 AgeingTime *uint32 274 HelloTime *uint32 275 VlanFiltering *bool 276 VlanDefaultPVID *uint16 277 GroupFwdMask *uint16 278 } 279 280 func (bridge *Bridge) Attrs() *LinkAttrs { 281 return &bridge.LinkAttrs 282 } 283 284 func (bridge *Bridge) Type() string { 285 return "bridge" 286 } 287 288 // Vlan links have ParentIndex set in their Attrs() 289 type Vlan struct { 290 LinkAttrs 291 VlanId int 292 VlanProtocol VlanProtocol 293 } 294 295 func (vlan *Vlan) Attrs() *LinkAttrs { 296 return &vlan.LinkAttrs 297 } 298 299 func (vlan *Vlan) Type() string { 300 return "vlan" 301 } 302 303 type MacvlanMode uint16 304 305 const ( 306 MACVLAN_MODE_DEFAULT MacvlanMode = iota 307 MACVLAN_MODE_PRIVATE 308 MACVLAN_MODE_VEPA 309 MACVLAN_MODE_BRIDGE 310 MACVLAN_MODE_PASSTHRU 311 MACVLAN_MODE_SOURCE 312 ) 313 314 // Macvlan links have ParentIndex set in their Attrs() 315 type Macvlan struct { 316 LinkAttrs 317 Mode MacvlanMode 318 319 // MACAddrs is only populated for Macvlan SOURCE links 320 MACAddrs []net.HardwareAddr 321 322 BCQueueLen uint32 323 UsedBCQueueLen uint32 324 } 325 326 func (macvlan *Macvlan) Attrs() *LinkAttrs { 327 return &macvlan.LinkAttrs 328 } 329 330 func (macvlan *Macvlan) Type() string { 331 return "macvlan" 332 } 333 334 // Macvtap - macvtap is a virtual interfaces based on macvlan 335 type Macvtap struct { 336 Macvlan 337 } 338 339 func (macvtap Macvtap) Type() string { 340 return "macvtap" 341 } 342 343 type TuntapMode uint16 344 type TuntapFlag uint16 345 346 // Tuntap links created via /dev/tun/tap, but can be destroyed via netlink 347 type Tuntap struct { 348 LinkAttrs 349 Mode TuntapMode 350 Flags TuntapFlag 351 NonPersist bool 352 Queues int 353 Fds []*os.File 354 Owner uint32 355 Group uint32 356 } 357 358 func (tuntap *Tuntap) Attrs() *LinkAttrs { 359 return &tuntap.LinkAttrs 360 } 361 362 func (tuntap *Tuntap) Type() string { 363 return "tuntap" 364 } 365 366 type NetkitMode uint32 367 368 const ( 369 NETKIT_MODE_L2 NetkitMode = iota 370 NETKIT_MODE_L3 371 ) 372 373 type NetkitPolicy int 374 375 const ( 376 NETKIT_POLICY_FORWARD NetkitPolicy = 0 377 NETKIT_POLICY_BLACKHOLE NetkitPolicy = 2 378 ) 379 380 func (n *Netkit) IsPrimary() bool { 381 return n.isPrimary 382 } 383 384 // SetPeerAttrs will not take effect if trying to modify an existing netkit device 385 func (n *Netkit) SetPeerAttrs(Attrs *LinkAttrs) { 386 n.peerLinkAttrs = *Attrs 387 } 388 389 type Netkit struct { 390 LinkAttrs 391 Mode NetkitMode 392 Policy NetkitPolicy 393 PeerPolicy NetkitPolicy 394 isPrimary bool 395 peerLinkAttrs LinkAttrs 396 } 397 398 func (n *Netkit) Attrs() *LinkAttrs { 399 return &n.LinkAttrs 400 } 401 402 func (n *Netkit) Type() string { 403 return "netkit" 404 } 405 406 // Veth devices must specify PeerName on create 407 type Veth struct { 408 LinkAttrs 409 PeerName string // veth on create only 410 PeerHardwareAddr net.HardwareAddr 411 PeerNamespace interface{} 412 } 413 414 func (veth *Veth) Attrs() *LinkAttrs { 415 return &veth.LinkAttrs 416 } 417 418 func (veth *Veth) Type() string { 419 return "veth" 420 } 421 422 // Wireguard represent links of type "wireguard", see https://www.wireguard.com/ 423 type Wireguard struct { 424 LinkAttrs 425 } 426 427 func (wg *Wireguard) Attrs() *LinkAttrs { 428 return &wg.LinkAttrs 429 } 430 431 func (wg *Wireguard) Type() string { 432 return "wireguard" 433 } 434 435 // GenericLink links represent types that are not currently understood 436 // by this netlink library. 437 type GenericLink struct { 438 LinkAttrs 439 LinkType string 440 } 441 442 func (generic *GenericLink) Attrs() *LinkAttrs { 443 return &generic.LinkAttrs 444 } 445 446 func (generic *GenericLink) Type() string { 447 return generic.LinkType 448 } 449 450 type Vxlan struct { 451 LinkAttrs 452 VxlanId int 453 VtepDevIndex int 454 SrcAddr net.IP 455 Group net.IP 456 TTL int 457 TOS int 458 Learning bool 459 Proxy bool 460 RSC bool 461 L2miss bool 462 L3miss bool 463 UDPCSum bool 464 UDP6ZeroCSumTx bool 465 UDP6ZeroCSumRx bool 466 NoAge bool 467 GBP bool 468 FlowBased bool 469 Age int 470 Limit int 471 Port int 472 PortLow int 473 PortHigh int 474 } 475 476 func (vxlan *Vxlan) Attrs() *LinkAttrs { 477 return &vxlan.LinkAttrs 478 } 479 480 func (vxlan *Vxlan) Type() string { 481 return "vxlan" 482 } 483 484 type IPVlanMode uint16 485 486 const ( 487 IPVLAN_MODE_L2 IPVlanMode = iota 488 IPVLAN_MODE_L3 489 IPVLAN_MODE_L3S 490 IPVLAN_MODE_MAX 491 ) 492 493 type IPVlanFlag uint16 494 495 const ( 496 IPVLAN_FLAG_BRIDGE IPVlanFlag = iota 497 IPVLAN_FLAG_PRIVATE 498 IPVLAN_FLAG_VEPA 499 ) 500 501 type IPVlan struct { 502 LinkAttrs 503 Mode IPVlanMode 504 Flag IPVlanFlag 505 } 506 507 func (ipvlan *IPVlan) Attrs() *LinkAttrs { 508 return &ipvlan.LinkAttrs 509 } 510 511 func (ipvlan *IPVlan) Type() string { 512 return "ipvlan" 513 } 514 515 // IPVtap - IPVtap is a virtual interfaces based on ipvlan 516 type IPVtap struct { 517 IPVlan 518 } 519 520 func (ipvtap *IPVtap) Attrs() *LinkAttrs { 521 return &ipvtap.LinkAttrs 522 } 523 524 func (ipvtap IPVtap) Type() string { 525 return "ipvtap" 526 } 527 528 // VlanProtocol type 529 type VlanProtocol int 530 531 func (p VlanProtocol) String() string { 532 s, ok := VlanProtocolToString[p] 533 if !ok { 534 return fmt.Sprintf("VlanProtocol(%d)", p) 535 } 536 return s 537 } 538 539 // StringToVlanProtocol returns vlan protocol, or unknown is the s is invalid. 540 func StringToVlanProtocol(s string) VlanProtocol { 541 mode, ok := StringToVlanProtocolMap[s] 542 if !ok { 543 return VLAN_PROTOCOL_UNKNOWN 544 } 545 return mode 546 } 547 548 // VlanProtocol possible values 549 const ( 550 VLAN_PROTOCOL_UNKNOWN VlanProtocol = 0 551 VLAN_PROTOCOL_8021Q VlanProtocol = 0x8100 552 VLAN_PROTOCOL_8021AD VlanProtocol = 0x88A8 553 ) 554 555 var VlanProtocolToString = map[VlanProtocol]string{ 556 VLAN_PROTOCOL_8021Q: "802.1q", 557 VLAN_PROTOCOL_8021AD: "802.1ad", 558 } 559 560 var StringToVlanProtocolMap = map[string]VlanProtocol{ 561 "802.1q": VLAN_PROTOCOL_8021Q, 562 "802.1ad": VLAN_PROTOCOL_8021AD, 563 } 564 565 // BondMode type 566 type BondMode int 567 568 func (b BondMode) String() string { 569 s, ok := bondModeToString[b] 570 if !ok { 571 return fmt.Sprintf("BondMode(%d)", b) 572 } 573 return s 574 } 575 576 // StringToBondMode returns bond mode, or unknown is the s is invalid. 577 func StringToBondMode(s string) BondMode { 578 mode, ok := StringToBondModeMap[s] 579 if !ok { 580 return BOND_MODE_UNKNOWN 581 } 582 return mode 583 } 584 585 // Possible BondMode 586 const ( 587 BOND_MODE_BALANCE_RR BondMode = iota 588 BOND_MODE_ACTIVE_BACKUP 589 BOND_MODE_BALANCE_XOR 590 BOND_MODE_BROADCAST 591 BOND_MODE_802_3AD 592 BOND_MODE_BALANCE_TLB 593 BOND_MODE_BALANCE_ALB 594 BOND_MODE_UNKNOWN 595 ) 596 597 var bondModeToString = map[BondMode]string{ 598 BOND_MODE_BALANCE_RR: "balance-rr", 599 BOND_MODE_ACTIVE_BACKUP: "active-backup", 600 BOND_MODE_BALANCE_XOR: "balance-xor", 601 BOND_MODE_BROADCAST: "broadcast", 602 BOND_MODE_802_3AD: "802.3ad", 603 BOND_MODE_BALANCE_TLB: "balance-tlb", 604 BOND_MODE_BALANCE_ALB: "balance-alb", 605 } 606 var StringToBondModeMap = map[string]BondMode{ 607 "balance-rr": BOND_MODE_BALANCE_RR, 608 "active-backup": BOND_MODE_ACTIVE_BACKUP, 609 "balance-xor": BOND_MODE_BALANCE_XOR, 610 "broadcast": BOND_MODE_BROADCAST, 611 "802.3ad": BOND_MODE_802_3AD, 612 "balance-tlb": BOND_MODE_BALANCE_TLB, 613 "balance-alb": BOND_MODE_BALANCE_ALB, 614 } 615 616 // BondArpValidate type 617 type BondArpValidate int 618 619 // Possible BondArpValidate value 620 const ( 621 BOND_ARP_VALIDATE_NONE BondArpValidate = iota 622 BOND_ARP_VALIDATE_ACTIVE 623 BOND_ARP_VALIDATE_BACKUP 624 BOND_ARP_VALIDATE_ALL 625 ) 626 627 var bondArpValidateToString = map[BondArpValidate]string{ 628 BOND_ARP_VALIDATE_NONE: "none", 629 BOND_ARP_VALIDATE_ACTIVE: "active", 630 BOND_ARP_VALIDATE_BACKUP: "backup", 631 BOND_ARP_VALIDATE_ALL: "none", 632 } 633 var StringToBondArpValidateMap = map[string]BondArpValidate{ 634 "none": BOND_ARP_VALIDATE_NONE, 635 "active": BOND_ARP_VALIDATE_ACTIVE, 636 "backup": BOND_ARP_VALIDATE_BACKUP, 637 "all": BOND_ARP_VALIDATE_ALL, 638 } 639 640 func (b BondArpValidate) String() string { 641 s, ok := bondArpValidateToString[b] 642 if !ok { 643 return fmt.Sprintf("BondArpValidate(%d)", b) 644 } 645 return s 646 } 647 648 // BondPrimaryReselect type 649 type BondPrimaryReselect int 650 651 // Possible BondPrimaryReselect value 652 const ( 653 BOND_PRIMARY_RESELECT_ALWAYS BondPrimaryReselect = iota 654 BOND_PRIMARY_RESELECT_BETTER 655 BOND_PRIMARY_RESELECT_FAILURE 656 ) 657 658 var bondPrimaryReselectToString = map[BondPrimaryReselect]string{ 659 BOND_PRIMARY_RESELECT_ALWAYS: "always", 660 BOND_PRIMARY_RESELECT_BETTER: "better", 661 BOND_PRIMARY_RESELECT_FAILURE: "failure", 662 } 663 var StringToBondPrimaryReselectMap = map[string]BondPrimaryReselect{ 664 "always": BOND_PRIMARY_RESELECT_ALWAYS, 665 "better": BOND_PRIMARY_RESELECT_BETTER, 666 "failure": BOND_PRIMARY_RESELECT_FAILURE, 667 } 668 669 func (b BondPrimaryReselect) String() string { 670 s, ok := bondPrimaryReselectToString[b] 671 if !ok { 672 return fmt.Sprintf("BondPrimaryReselect(%d)", b) 673 } 674 return s 675 } 676 677 // BondArpAllTargets type 678 type BondArpAllTargets int 679 680 // Possible BondArpAllTargets value 681 const ( 682 BOND_ARP_ALL_TARGETS_ANY BondArpAllTargets = iota 683 BOND_ARP_ALL_TARGETS_ALL 684 ) 685 686 var bondArpAllTargetsToString = map[BondArpAllTargets]string{ 687 BOND_ARP_ALL_TARGETS_ANY: "any", 688 BOND_ARP_ALL_TARGETS_ALL: "all", 689 } 690 var StringToBondArpAllTargetsMap = map[string]BondArpAllTargets{ 691 "any": BOND_ARP_ALL_TARGETS_ANY, 692 "all": BOND_ARP_ALL_TARGETS_ALL, 693 } 694 695 func (b BondArpAllTargets) String() string { 696 s, ok := bondArpAllTargetsToString[b] 697 if !ok { 698 return fmt.Sprintf("BondArpAllTargets(%d)", b) 699 } 700 return s 701 } 702 703 // BondFailOverMac type 704 type BondFailOverMac int 705 706 // Possible BondFailOverMac value 707 const ( 708 BOND_FAIL_OVER_MAC_NONE BondFailOverMac = iota 709 BOND_FAIL_OVER_MAC_ACTIVE 710 BOND_FAIL_OVER_MAC_FOLLOW 711 ) 712 713 var bondFailOverMacToString = map[BondFailOverMac]string{ 714 BOND_FAIL_OVER_MAC_NONE: "none", 715 BOND_FAIL_OVER_MAC_ACTIVE: "active", 716 BOND_FAIL_OVER_MAC_FOLLOW: "follow", 717 } 718 var StringToBondFailOverMacMap = map[string]BondFailOverMac{ 719 "none": BOND_FAIL_OVER_MAC_NONE, 720 "active": BOND_FAIL_OVER_MAC_ACTIVE, 721 "follow": BOND_FAIL_OVER_MAC_FOLLOW, 722 } 723 724 func (b BondFailOverMac) String() string { 725 s, ok := bondFailOverMacToString[b] 726 if !ok { 727 return fmt.Sprintf("BondFailOverMac(%d)", b) 728 } 729 return s 730 } 731 732 // BondXmitHashPolicy type 733 type BondXmitHashPolicy int 734 735 func (b BondXmitHashPolicy) String() string { 736 s, ok := bondXmitHashPolicyToString[b] 737 if !ok { 738 return fmt.Sprintf("XmitHashPolicy(%d)", b) 739 } 740 return s 741 } 742 743 // StringToBondXmitHashPolicy returns bond lacp arte, or unknown is the s is invalid. 744 func StringToBondXmitHashPolicy(s string) BondXmitHashPolicy { 745 lacp, ok := StringToBondXmitHashPolicyMap[s] 746 if !ok { 747 return BOND_XMIT_HASH_POLICY_UNKNOWN 748 } 749 return lacp 750 } 751 752 // Possible BondXmitHashPolicy value 753 const ( 754 BOND_XMIT_HASH_POLICY_LAYER2 BondXmitHashPolicy = iota 755 BOND_XMIT_HASH_POLICY_LAYER3_4 756 BOND_XMIT_HASH_POLICY_LAYER2_3 757 BOND_XMIT_HASH_POLICY_ENCAP2_3 758 BOND_XMIT_HASH_POLICY_ENCAP3_4 759 BOND_XMIT_HASH_POLICY_VLAN_SRCMAC 760 BOND_XMIT_HASH_POLICY_UNKNOWN 761 ) 762 763 var bondXmitHashPolicyToString = map[BondXmitHashPolicy]string{ 764 BOND_XMIT_HASH_POLICY_LAYER2: "layer2", 765 BOND_XMIT_HASH_POLICY_LAYER3_4: "layer3+4", 766 BOND_XMIT_HASH_POLICY_LAYER2_3: "layer2+3", 767 BOND_XMIT_HASH_POLICY_ENCAP2_3: "encap2+3", 768 BOND_XMIT_HASH_POLICY_ENCAP3_4: "encap3+4", 769 BOND_XMIT_HASH_POLICY_VLAN_SRCMAC: "vlan+srcmac", 770 } 771 var StringToBondXmitHashPolicyMap = map[string]BondXmitHashPolicy{ 772 "layer2": BOND_XMIT_HASH_POLICY_LAYER2, 773 "layer3+4": BOND_XMIT_HASH_POLICY_LAYER3_4, 774 "layer2+3": BOND_XMIT_HASH_POLICY_LAYER2_3, 775 "encap2+3": BOND_XMIT_HASH_POLICY_ENCAP2_3, 776 "encap3+4": BOND_XMIT_HASH_POLICY_ENCAP3_4, 777 "vlan+srcmac": BOND_XMIT_HASH_POLICY_VLAN_SRCMAC, 778 } 779 780 // BondLacpRate type 781 type BondLacpRate int 782 783 func (b BondLacpRate) String() string { 784 s, ok := bondLacpRateToString[b] 785 if !ok { 786 return fmt.Sprintf("LacpRate(%d)", b) 787 } 788 return s 789 } 790 791 // StringToBondLacpRate returns bond lacp arte, or unknown is the s is invalid. 792 func StringToBondLacpRate(s string) BondLacpRate { 793 lacp, ok := StringToBondLacpRateMap[s] 794 if !ok { 795 return BOND_LACP_RATE_UNKNOWN 796 } 797 return lacp 798 } 799 800 // Possible BondLacpRate value 801 const ( 802 BOND_LACP_RATE_SLOW BondLacpRate = iota 803 BOND_LACP_RATE_FAST 804 BOND_LACP_RATE_UNKNOWN 805 ) 806 807 var bondLacpRateToString = map[BondLacpRate]string{ 808 BOND_LACP_RATE_SLOW: "slow", 809 BOND_LACP_RATE_FAST: "fast", 810 } 811 var StringToBondLacpRateMap = map[string]BondLacpRate{ 812 "slow": BOND_LACP_RATE_SLOW, 813 "fast": BOND_LACP_RATE_FAST, 814 } 815 816 // BondAdSelect type 817 type BondAdSelect int 818 819 // Possible BondAdSelect value 820 const ( 821 BOND_AD_SELECT_STABLE BondAdSelect = iota 822 BOND_AD_SELECT_BANDWIDTH 823 BOND_AD_SELECT_COUNT 824 ) 825 826 var bondAdSelectToString = map[BondAdSelect]string{ 827 BOND_AD_SELECT_STABLE: "stable", 828 BOND_AD_SELECT_BANDWIDTH: "bandwidth", 829 BOND_AD_SELECT_COUNT: "count", 830 } 831 var StringToBondAdSelectMap = map[string]BondAdSelect{ 832 "stable": BOND_AD_SELECT_STABLE, 833 "bandwidth": BOND_AD_SELECT_BANDWIDTH, 834 "count": BOND_AD_SELECT_COUNT, 835 } 836 837 func (b BondAdSelect) String() string { 838 s, ok := bondAdSelectToString[b] 839 if !ok { 840 return fmt.Sprintf("BondAdSelect(%d)", b) 841 } 842 return s 843 } 844 845 // BondAdInfo represents ad info for bond 846 type BondAdInfo struct { 847 AggregatorId int 848 NumPorts int 849 ActorKey int 850 PartnerKey int 851 PartnerMac net.HardwareAddr 852 } 853 854 // Bond representation 855 type Bond struct { 856 LinkAttrs 857 Mode BondMode 858 ActiveSlave int 859 Miimon int 860 UpDelay int 861 DownDelay int 862 UseCarrier int 863 ArpInterval int 864 ArpIpTargets []net.IP 865 ArpValidate BondArpValidate 866 ArpAllTargets BondArpAllTargets 867 Primary int 868 PrimaryReselect BondPrimaryReselect 869 FailOverMac BondFailOverMac 870 XmitHashPolicy BondXmitHashPolicy 871 ResendIgmp int 872 NumPeerNotif int 873 AllSlavesActive int 874 MinLinks int 875 LpInterval int 876 PacketsPerSlave int 877 LacpRate BondLacpRate 878 AdSelect BondAdSelect 879 // looking at iproute tool AdInfo can only be retrived. It can't be set. 880 AdInfo *BondAdInfo 881 AdActorSysPrio int 882 AdUserPortKey int 883 AdActorSystem net.HardwareAddr 884 TlbDynamicLb int 885 } 886 887 func NewLinkBond(atr LinkAttrs) *Bond { 888 return &Bond{ 889 LinkAttrs: atr, 890 Mode: -1, 891 ActiveSlave: -1, 892 Miimon: -1, 893 UpDelay: -1, 894 DownDelay: -1, 895 UseCarrier: -1, 896 ArpInterval: -1, 897 ArpIpTargets: nil, 898 ArpValidate: -1, 899 ArpAllTargets: -1, 900 Primary: -1, 901 PrimaryReselect: -1, 902 FailOverMac: -1, 903 XmitHashPolicy: -1, 904 ResendIgmp: -1, 905 NumPeerNotif: -1, 906 AllSlavesActive: -1, 907 MinLinks: -1, 908 LpInterval: -1, 909 PacketsPerSlave: -1, 910 LacpRate: -1, 911 AdSelect: -1, 912 AdActorSysPrio: -1, 913 AdUserPortKey: -1, 914 AdActorSystem: nil, 915 TlbDynamicLb: -1, 916 } 917 } 918 919 // Flag mask for bond options. Bond.Flagmask must be set to on for option to work. 920 const ( 921 BOND_MODE_MASK uint64 = 1 << (1 + iota) 922 BOND_ACTIVE_SLAVE_MASK 923 BOND_MIIMON_MASK 924 BOND_UPDELAY_MASK 925 BOND_DOWNDELAY_MASK 926 BOND_USE_CARRIER_MASK 927 BOND_ARP_INTERVAL_MASK 928 BOND_ARP_VALIDATE_MASK 929 BOND_ARP_ALL_TARGETS_MASK 930 BOND_PRIMARY_MASK 931 BOND_PRIMARY_RESELECT_MASK 932 BOND_FAIL_OVER_MAC_MASK 933 BOND_XMIT_HASH_POLICY_MASK 934 BOND_RESEND_IGMP_MASK 935 BOND_NUM_PEER_NOTIF_MASK 936 BOND_ALL_SLAVES_ACTIVE_MASK 937 BOND_MIN_LINKS_MASK 938 BOND_LP_INTERVAL_MASK 939 BOND_PACKETS_PER_SLAVE_MASK 940 BOND_LACP_RATE_MASK 941 BOND_AD_SELECT_MASK 942 ) 943 944 // Attrs implementation. 945 func (bond *Bond) Attrs() *LinkAttrs { 946 return &bond.LinkAttrs 947 } 948 949 // Type implementation fro Vxlan. 950 func (bond *Bond) Type() string { 951 return "bond" 952 } 953 954 // BondSlaveState represents the values of the IFLA_BOND_SLAVE_STATE bond slave 955 // attribute, which contains the state of the bond slave. 956 type BondSlaveState uint8 957 958 const ( 959 //BondStateActive Link is active. 960 BondStateActive BondSlaveState = iota 961 //BondStateBackup Link is backup. 962 BondStateBackup 963 ) 964 965 func (s BondSlaveState) String() string { 966 switch s { 967 case BondStateActive: 968 return "ACTIVE" 969 case BondStateBackup: 970 return "BACKUP" 971 default: 972 return strconv.Itoa(int(s)) 973 } 974 } 975 976 // BondSlaveMiiStatus represents the values of the IFLA_BOND_SLAVE_MII_STATUS bond slave 977 // attribute, which contains the status of MII link monitoring 978 type BondSlaveMiiStatus uint8 979 980 const ( 981 //BondLinkUp link is up and running. 982 BondLinkUp BondSlaveMiiStatus = iota 983 //BondLinkFail link has just gone down. 984 BondLinkFail 985 //BondLinkDown link has been down for too long time. 986 BondLinkDown 987 //BondLinkBack link is going back. 988 BondLinkBack 989 ) 990 991 func (s BondSlaveMiiStatus) String() string { 992 switch s { 993 case BondLinkUp: 994 return "UP" 995 case BondLinkFail: 996 return "GOING_DOWN" 997 case BondLinkDown: 998 return "DOWN" 999 case BondLinkBack: 1000 return "GOING_BACK" 1001 default: 1002 return strconv.Itoa(int(s)) 1003 } 1004 } 1005 1006 type BondSlave struct { 1007 State BondSlaveState 1008 MiiStatus BondSlaveMiiStatus 1009 LinkFailureCount uint32 1010 PermHardwareAddr net.HardwareAddr 1011 QueueId uint16 1012 AggregatorId uint16 1013 AdActorOperPortState uint8 1014 AdPartnerOperPortState uint16 1015 } 1016 1017 func (b *BondSlave) SlaveType() string { 1018 return "bond" 1019 } 1020 1021 type VrfSlave struct { 1022 Table uint32 1023 } 1024 1025 func (v *VrfSlave) SlaveType() string { 1026 return "vrf" 1027 } 1028 1029 // Geneve devices must specify RemoteIP and ID (VNI) on create 1030 // https://github.com/torvalds/linux/blob/47ec5303d73ea344e84f46660fff693c57641386/drivers/net/geneve.c#L1209-L1223 1031 type Geneve struct { 1032 LinkAttrs 1033 ID uint32 // vni 1034 Remote net.IP 1035 Ttl uint8 1036 Tos uint8 1037 Dport uint16 1038 UdpCsum uint8 1039 UdpZeroCsum6Tx uint8 1040 UdpZeroCsum6Rx uint8 1041 Link uint32 1042 FlowBased bool 1043 InnerProtoInherit bool 1044 Df GeneveDf 1045 } 1046 1047 func (geneve *Geneve) Attrs() *LinkAttrs { 1048 return &geneve.LinkAttrs 1049 } 1050 1051 func (geneve *Geneve) Type() string { 1052 return "geneve" 1053 } 1054 1055 type GeneveDf uint8 1056 1057 const ( 1058 GENEVE_DF_UNSET GeneveDf = iota 1059 GENEVE_DF_SET 1060 GENEVE_DF_INHERIT 1061 GENEVE_DF_MAX 1062 ) 1063 1064 // Gretap devices must specify LocalIP and RemoteIP on create 1065 type Gretap struct { 1066 LinkAttrs 1067 IKey uint32 1068 OKey uint32 1069 EncapSport uint16 1070 EncapDport uint16 1071 Local net.IP 1072 Remote net.IP 1073 IFlags uint16 1074 OFlags uint16 1075 PMtuDisc uint8 1076 Ttl uint8 1077 Tos uint8 1078 EncapType uint16 1079 EncapFlags uint16 1080 Link uint32 1081 FlowBased bool 1082 } 1083 1084 func (gretap *Gretap) Attrs() *LinkAttrs { 1085 return &gretap.LinkAttrs 1086 } 1087 1088 func (gretap *Gretap) Type() string { 1089 if gretap.Local.To4() == nil { 1090 return "ip6gretap" 1091 } 1092 return "gretap" 1093 } 1094 1095 type Iptun struct { 1096 LinkAttrs 1097 Ttl uint8 1098 Tos uint8 1099 PMtuDisc uint8 1100 Link uint32 1101 Local net.IP 1102 Remote net.IP 1103 EncapSport uint16 1104 EncapDport uint16 1105 EncapType uint16 1106 EncapFlags uint16 1107 FlowBased bool 1108 Proto uint8 1109 } 1110 1111 func (iptun *Iptun) Attrs() *LinkAttrs { 1112 return &iptun.LinkAttrs 1113 } 1114 1115 func (iptun *Iptun) Type() string { 1116 return "ipip" 1117 } 1118 1119 type Ip6tnl struct { 1120 LinkAttrs 1121 Link uint32 1122 Local net.IP 1123 Remote net.IP 1124 Ttl uint8 1125 Tos uint8 1126 Flags uint32 1127 Proto uint8 1128 FlowInfo uint32 1129 EncapLimit uint8 1130 EncapType uint16 1131 EncapFlags uint16 1132 EncapSport uint16 1133 EncapDport uint16 1134 FlowBased bool 1135 } 1136 1137 func (ip6tnl *Ip6tnl) Attrs() *LinkAttrs { 1138 return &ip6tnl.LinkAttrs 1139 } 1140 1141 func (ip6tnl *Ip6tnl) Type() string { 1142 return "ip6tnl" 1143 } 1144 1145 // from https://elixir.bootlin.com/linux/v5.15.4/source/include/uapi/linux/if_tunnel.h#L84 1146 type TunnelEncapType uint16 1147 1148 const ( 1149 None TunnelEncapType = iota 1150 FOU 1151 GUE 1152 ) 1153 1154 // from https://elixir.bootlin.com/linux/v5.15.4/source/include/uapi/linux/if_tunnel.h#L91 1155 type TunnelEncapFlag uint16 1156 1157 const ( 1158 CSum TunnelEncapFlag = 1 << 0 1159 CSum6 = 1 << 1 1160 RemCSum = 1 << 2 1161 ) 1162 1163 // from https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/ip6_tunnel.h#L12 1164 type IP6TunnelFlag uint16 1165 1166 const ( 1167 IP6_TNL_F_IGN_ENCAP_LIMIT IP6TunnelFlag = 1 // don't add encapsulation limit if one isn't present in inner packet 1168 IP6_TNL_F_USE_ORIG_TCLASS = 2 // copy the traffic class field from the inner packet 1169 IP6_TNL_F_USE_ORIG_FLOWLABEL = 4 // copy the flowlabel from the inner packet 1170 IP6_TNL_F_MIP6_DEV = 8 // being used for Mobile IPv6 1171 IP6_TNL_F_RCV_DSCP_COPY = 10 // copy DSCP from the outer packet 1172 IP6_TNL_F_USE_ORIG_FWMARK = 20 // copy fwmark from inner packet 1173 IP6_TNL_F_ALLOW_LOCAL_REMOTE = 40 // allow remote endpoint on the local node 1174 ) 1175 1176 type Sittun struct { 1177 LinkAttrs 1178 Link uint32 1179 Ttl uint8 1180 Tos uint8 1181 PMtuDisc uint8 1182 Proto uint8 1183 Local net.IP 1184 Remote net.IP 1185 EncapLimit uint8 1186 EncapType uint16 1187 EncapFlags uint16 1188 EncapSport uint16 1189 EncapDport uint16 1190 } 1191 1192 func (sittun *Sittun) Attrs() *LinkAttrs { 1193 return &sittun.LinkAttrs 1194 } 1195 1196 func (sittun *Sittun) Type() string { 1197 return "sit" 1198 } 1199 1200 type Vti struct { 1201 LinkAttrs 1202 IKey uint32 1203 OKey uint32 1204 Link uint32 1205 Local net.IP 1206 Remote net.IP 1207 } 1208 1209 func (vti *Vti) Attrs() *LinkAttrs { 1210 return &vti.LinkAttrs 1211 } 1212 1213 func (vti *Vti) Type() string { 1214 if vti.Local.To4() == nil { 1215 return "vti6" 1216 } 1217 return "vti" 1218 } 1219 1220 type Gretun struct { 1221 LinkAttrs 1222 Link uint32 1223 IFlags uint16 1224 OFlags uint16 1225 IKey uint32 1226 OKey uint32 1227 Local net.IP 1228 Remote net.IP 1229 Ttl uint8 1230 Tos uint8 1231 PMtuDisc uint8 1232 EncapType uint16 1233 EncapFlags uint16 1234 EncapSport uint16 1235 EncapDport uint16 1236 FlowBased bool 1237 } 1238 1239 func (gretun *Gretun) Attrs() *LinkAttrs { 1240 return &gretun.LinkAttrs 1241 } 1242 1243 func (gretun *Gretun) Type() string { 1244 if gretun.Local.To4() == nil { 1245 return "ip6gre" 1246 } 1247 return "gre" 1248 } 1249 1250 type Vrf struct { 1251 LinkAttrs 1252 Table uint32 1253 } 1254 1255 func (vrf *Vrf) Attrs() *LinkAttrs { 1256 return &vrf.LinkAttrs 1257 } 1258 1259 func (vrf *Vrf) Type() string { 1260 return "vrf" 1261 } 1262 1263 type GTP struct { 1264 LinkAttrs 1265 FD0 int 1266 FD1 int 1267 Role int 1268 PDPHashsize int 1269 } 1270 1271 func (gtp *GTP) Attrs() *LinkAttrs { 1272 return >p.LinkAttrs 1273 } 1274 1275 func (gtp *GTP) Type() string { 1276 return "gtp" 1277 } 1278 1279 // Virtual XFRM Interfaces 1280 // 1281 // Named "xfrmi" to prevent confusion with XFRM objects 1282 type Xfrmi struct { 1283 LinkAttrs 1284 Ifid uint32 1285 } 1286 1287 func (xfrm *Xfrmi) Attrs() *LinkAttrs { 1288 return &xfrm.LinkAttrs 1289 } 1290 1291 func (xfrm *Xfrmi) Type() string { 1292 return "xfrm" 1293 } 1294 1295 // IPoIB interface 1296 1297 type IPoIBMode uint16 1298 1299 func (m *IPoIBMode) String() string { 1300 str, ok := iPoIBModeToString[*m] 1301 if !ok { 1302 return fmt.Sprintf("mode(%d)", *m) 1303 } 1304 return str 1305 } 1306 1307 const ( 1308 IPOIB_MODE_DATAGRAM = iota 1309 IPOIB_MODE_CONNECTED 1310 ) 1311 1312 var iPoIBModeToString = map[IPoIBMode]string{ 1313 IPOIB_MODE_DATAGRAM: "datagram", 1314 IPOIB_MODE_CONNECTED: "connected", 1315 } 1316 1317 var StringToIPoIBMode = map[string]IPoIBMode{ 1318 "datagram": IPOIB_MODE_DATAGRAM, 1319 "connected": IPOIB_MODE_CONNECTED, 1320 } 1321 1322 const ( 1323 CAN_STATE_ERROR_ACTIVE = iota 1324 CAN_STATE_ERROR_WARNING 1325 CAN_STATE_ERROR_PASSIVE 1326 CAN_STATE_BUS_OFF 1327 CAN_STATE_STOPPED 1328 CAN_STATE_SLEEPING 1329 ) 1330 1331 type Can struct { 1332 LinkAttrs 1333 1334 BitRate uint32 1335 SamplePoint uint32 1336 TimeQuanta uint32 1337 PropagationSegment uint32 1338 PhaseSegment1 uint32 1339 PhaseSegment2 uint32 1340 SyncJumpWidth uint32 1341 BitRatePreScaler uint32 1342 1343 Name string 1344 TimeSegment1Min uint32 1345 TimeSegment1Max uint32 1346 TimeSegment2Min uint32 1347 TimeSegment2Max uint32 1348 SyncJumpWidthMax uint32 1349 BitRatePreScalerMin uint32 1350 BitRatePreScalerMax uint32 1351 BitRatePreScalerInc uint32 1352 1353 ClockFrequency uint32 1354 1355 State uint32 1356 1357 Mask uint32 1358 Flags uint32 1359 1360 TxError uint16 1361 RxError uint16 1362 1363 RestartMs uint32 1364 } 1365 1366 func (can *Can) Attrs() *LinkAttrs { 1367 return &can.LinkAttrs 1368 } 1369 1370 func (can *Can) Type() string { 1371 return "can" 1372 } 1373 1374 type IPoIB struct { 1375 LinkAttrs 1376 Pkey uint16 1377 Mode IPoIBMode 1378 Umcast uint16 1379 } 1380 1381 func (ipoib *IPoIB) Attrs() *LinkAttrs { 1382 return &ipoib.LinkAttrs 1383 } 1384 1385 func (ipoib *IPoIB) Type() string { 1386 return "ipoib" 1387 } 1388 1389 type BareUDP struct { 1390 LinkAttrs 1391 Port uint16 1392 EtherType uint16 1393 SrcPortMin uint16 1394 MultiProto bool 1395 } 1396 1397 func (bareudp *BareUDP) Attrs() *LinkAttrs { 1398 return &bareudp.LinkAttrs 1399 } 1400 1401 func (bareudp *BareUDP) Type() string { 1402 return "bareudp" 1403 } 1404 1405 // iproute2 supported devices; 1406 // vlan | veth | vcan | dummy | ifb | macvlan | macvtap | 1407 // bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | 1408 // gre | gretap | ip6gre | ip6gretap | vti | vti6 | nlmon | 1409 // bond_slave | ipvlan | xfrm | bareudp 1410 1411 // LinkNotFoundError wraps the various not found errors when 1412 // getting/reading links. This is intended for better error 1413 // handling by dependent code so that "not found error" can 1414 // be distinguished from other errors 1415 type LinkNotFoundError struct { 1416 error 1417 }