| 1 | // Copyright 2015 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 ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ |
| 16 | |
| 17 | import ( |
| 18 | "crypto/randscip-go gomod github.com/golang/go/src go1.24.2 `crypto/rand`/" |
| 19 | "errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/" |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/" |
| 21 | "netscip-go gomod github.com/golang/go/src go1.24.2 net/" |
| 22 | "osscip-go gomod github.com/golang/go/src go1.24.2 os/" |
| 23 | |
| 24 | "github.com/safchain/ethtoolscip-go gomod github.com/safchain/ethtool v0.6.2 `github.com/safchain/ethtool`/" |
| 25 | "github.com/vishvananda/netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/" |
| 26 | |
| 27 | "github.com/containernetworking/plugins/pkg/netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/" |
| 28 | "github.com/containernetworking/plugins/pkg/nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/" |
| 29 | "github.com/containernetworking/plugins/pkg/utils/sysctlscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/sysctl`/" |
| 30 | ) |
| 31 | |
| 32 | var ErrLinkNotFoundscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ErrLinkNotFound. = errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/.Newscip-go gomod github.com/golang/go/src go1.24.2 errors/New().("link not found") |
| 33 | |
| 34 | // makeVethPair is called from within the container's network namespace |
| 35 | func makeVethPairscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/makeVethPair().(name, peer string, mtu int, mac string, hostNS nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) (netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#, error) { |
| 36 | linkAttrs := netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.NewLinkAttrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/NewLinkAttrs().() |
| 37 | linkAttrs.Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name. = name |
| 38 | linkAttrs.MTUscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#MTU. = mtu |
| 39 | |
| 40 | veth := &netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Vethscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Veth#{ |
| 41 | LinkAttrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Veth#LinkAttrs.: linkAttrs, |
| 42 | PeerNamescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Veth#PeerName.: peer, |
| 43 | PeerNamespacescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Veth#PeerNamespace.: netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.NsFdscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/NsFd#(int(hostNS.Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Fd.())), |
| 44 | } |
| 45 | if mac != "" { |
| 46 | m, err := netscip-go gomod github.com/golang/go/src go1.24.2 net/.ParseMACscip-go gomod github.com/golang/go/src go1.24.2 net/ParseMAC().(mac) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | veth.LinkAttrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Veth#LinkAttrs..HardwareAddrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#HardwareAddr. = m |
| 51 | } |
| 52 | if err := netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkAddscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAdd().(veth); err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | // Re-fetch the container link to get its creation-time parameters, e.g. index and mac |
| 56 | veth2, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(name) |
| 57 | if err != nil { |
| 58 | netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkDelscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkDel().(veth) // try and clean up the link if possible. |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | return veth2, nil |
| 63 | } |
| 64 | |
| 65 | func peerExistsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/peerExists().(name string) bool { |
| 66 | if _, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(name); err != nil { |
| 67 | return false |
| 68 | } |
| 69 | return true |
| 70 | } |
| 71 | |
| 72 | func makeVethscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/makeVeth().(name, vethPeerName string, mtu int, mac string, hostNS nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) (string, netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#, error) { |
| 73 | var peerName string |
| 74 | var veth netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link# |
| 75 | var err error |
| 76 | for i := 0; i < 10; i++ { |
| 77 | if vethPeerName != "" { |
| 78 | peerName = vethPeerName |
| 79 | } else { |
| 80 | peerName, err = RandomVethNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/RandomVethName().() |
| 81 | if err != nil { |
| 82 | return peerName, nil, err |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | veth, err = makeVethPairscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/makeVethPair().(name, peerName, mtu, mac, hostNS) |
| 87 | switch { |
| 88 | case err == nil: |
| 89 | return peerName, veth, nil |
| 90 | |
| 91 | case osscip-go gomod github.com/golang/go/src go1.24.2 os/.IsExistscip-go gomod github.com/golang/go/src go1.24.2 os/IsExist().(err): |
| 92 | if peerExistsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/peerExists().(peerName) && vethPeerName == "" { |
| 93 | continue |
| 94 | } |
| 95 | return peerName, veth, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("container veth name (%q) peer provided (%q) already exists", name, peerName) |
| 96 | default: |
| 97 | return peerName, veth, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to make veth pair: %v", err) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // should really never be hit |
| 102 | return peerName, nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to find a unique veth name") |
| 103 | } |
| 104 | |
| 105 | // RandomVethName returns string "veth" with random prefix (hashed from entropy) |
| 106 | func RandomVethNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/RandomVethName().() (string, error) { |
| 107 | entropy := make([]byte, 4) |
| 108 | _, err := randscip-go gomod github.com/golang/go/src go1.24.2 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.24.2 `crypto/rand`/Read().(entropy) |
| 109 | if err != nil { |
| 110 | return "", fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to generate random veth name: %v", err) |
| 111 | } |
| 112 | |
| 113 | // NetworkManager (recent versions) will ignore veth devices that start with "veth" |
| 114 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("veth%x", entropy), nil |
| 115 | } |
| 116 | |
| 117 | func RenameLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/RenameLink().(curName, newName string) error { |
| 118 | link, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(curName) |
| 119 | if err == nil { |
| 120 | err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNamescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetName().(link, newName) |
| 121 | } |
| 122 | return err |
| 123 | } |
| 124 | |
| 125 | func ifaceFromNetlinkLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ifaceFromNetlinkLink().(l netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#) netscip-go gomod github.com/golang/go/src go1.24.2 net/.Interfacescip-go gomod github.com/golang/go/src go1.24.2 net/Interface# { |
| 126 | a := l.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().() |
| 127 | return netscip-go gomod github.com/golang/go/src go1.24.2 net/.Interfacescip-go gomod github.com/golang/go/src go1.24.2 net/Interface#{ |
| 128 | Indexscip-go gomod github.com/golang/go/src go1.24.2 net/Interface#Index.: a.Indexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Index., |
| 129 | MTUscip-go gomod github.com/golang/go/src go1.24.2 net/Interface#MTU.: a.MTUscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#MTU., |
| 130 | Namescip-go gomod github.com/golang/go/src go1.24.2 net/Interface#Name.: a.Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name., |
| 131 | HardwareAddrscip-go gomod github.com/golang/go/src go1.24.2 net/Interface#HardwareAddr.: a.HardwareAddrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#HardwareAddr., |
| 132 | Flagsscip-go gomod github.com/golang/go/src go1.24.2 net/Interface#Flags.: a.Flagsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Flags., |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // SetupVethWithName sets up a pair of virtual ethernet devices. |
| 137 | // Call SetupVethWithName from inside the container netns. It will create both veth |
| 138 | // devices and move the host-side veth into the provided hostNS namespace. |
| 139 | // hostVethName: If hostVethName is not specified, the host-side veth name will use a random string. |
| 140 | // On success, SetupVethWithName returns (hostVeth, containerVeth, nil) |
| 141 | func SetupVethWithNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/SetupVethWithName().(contVethName, hostVethName string, mtu int, contVethMac string, hostNS nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) (netscip-go gomod github.com/golang/go/src go1.24.2 net/.Interfacescip-go gomod github.com/golang/go/src go1.24.2 net/Interface#, netscip-go gomod github.com/golang/go/src go1.24.2 net/.Interfacescip-go gomod github.com/golang/go/src go1.24.2 net/Interface#, error) { |
| 142 | hostVethName, contVeth, err := makeVethscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/makeVeth().(contVethName, hostVethName, mtu, contVethMac, hostNS) |
| 143 | if err != nil { |
| 144 | return netscip-go gomod github.com/golang/go/src go1.24.2 net/.Interfacescip-go gomod github.com/golang/go/src go1.24.2 net/Interface#{}, netscip-go gomod github.com/golang/go/src go1.24.2 net/.Interfacescip-go gomod github.com/golang/go/src go1.24.2 net/Interface#{}, err |
| 145 | } |
| 146 | |
| 147 | var hostVeth netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link# |
| 148 | err = hostNS.Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Do.(func(_ nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error { |
| 149 | hostVeth, err = netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(hostVethName) |
| 150 | if err != nil { |
| 151 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to lookup %q in %q: %v", hostVethName, hostNS.Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Path.(), err) |
| 152 | } |
| 153 | |
| 154 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetUpscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetUp().(hostVeth); err != nil { |
| 155 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to set %q up: %v", hostVethName, err) |
| 156 | } |
| 157 | |
| 158 | // we want to own the routes for this interface |
| 159 | _, _ = sysctlscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/sysctl`/.Sysctlscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/sysctl`/Sysctl().(fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("net/ipv6/conf/%s/accept_ra", hostVethName), "0") |
| 160 | return nil |
| 161 | }) |
| 162 | if err != nil { |
| 163 | return netscip-go gomod github.com/golang/go/src go1.24.2 net/.Interfacescip-go gomod github.com/golang/go/src go1.24.2 net/Interface#{}, netscip-go gomod github.com/golang/go/src go1.24.2 net/.Interfacescip-go gomod github.com/golang/go/src go1.24.2 net/Interface#{}, err |
| 164 | } |
| 165 | return ifaceFromNetlinkLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ifaceFromNetlinkLink().(hostVeth), ifaceFromNetlinkLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ifaceFromNetlinkLink().(contVeth), nil |
| 166 | } |
| 167 | |
| 168 | // SetupVeth sets up a pair of virtual ethernet devices. |
| 169 | // Call SetupVeth from inside the container netns. It will create both veth |
| 170 | // devices and move the host-side veth into the provided hostNS namespace. |
| 171 | // On success, SetupVeth returns (hostVeth, containerVeth, nil) |
| 172 | func SetupVethscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/SetupVeth().(contVethName string, mtu int, contVethMac string, hostNS nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) (netscip-go gomod github.com/golang/go/src go1.24.2 net/.Interfacescip-go gomod github.com/golang/go/src go1.24.2 net/Interface#, netscip-go gomod github.com/golang/go/src go1.24.2 net/.Interfacescip-go gomod github.com/golang/go/src go1.24.2 net/Interface#, error) { |
| 173 | return SetupVethWithNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/SetupVethWithName().(contVethName, "", mtu, contVethMac, hostNS) |
| 174 | } |
| 175 | |
| 176 | // DelLinkByName removes an interface link. |
| 177 | func DelLinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/DelLinkByName().(ifName string) error { |
| 178 | iface, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(ifName) |
| 179 | if err != nil { |
| 180 | if _, ok := err.(netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkNotFoundErrorscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkNotFoundError#); ok { |
| 181 | return ErrLinkNotFoundscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ErrLinkNotFound. |
| 182 | } |
| 183 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to lookup %q: %v", ifName, err) |
| 184 | } |
| 185 | |
| 186 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkDelscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkDel().(iface); err != nil { |
| 187 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to delete %q: %v", ifName, err) |
| 188 | } |
| 189 | |
| 190 | return nil |
| 191 | } |
| 192 | |
| 193 | // DelLinkByNameAddr remove an interface and returns its addresses |
| 194 | func DelLinkByNameAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/DelLinkByNameAddr().(ifName string) ([]*netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#, error) { |
| 195 | iface, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(ifName) |
| 196 | if err != nil { |
| 197 | if _, ok := err.(netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkNotFoundErrorscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkNotFoundError#); ok { |
| 198 | return nil, ErrLinkNotFoundscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ErrLinkNotFound. |
| 199 | } |
| 200 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to lookup %q: %v", ifName, err) |
| 201 | } |
| 202 | |
| 203 | addrs, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.AddrListscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/AddrList().(iface, netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.FAMILY_ALLscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/FAMILY_ALL.) |
| 204 | if err != nil { |
| 205 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to get IP addresses for %q: %v", ifName, err) |
| 206 | } |
| 207 | |
| 208 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkDelscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkDel().(iface); err != nil { |
| 209 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to delete %q: %v", ifName, err) |
| 210 | } |
| 211 | |
| 212 | out := []*netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#{} |
| 213 | for _, addr := range addrs { |
| 214 | if addr.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP..IsGlobalUnicastscip-go gomod github.com/golang/go/src go1.24.2 net/IP#IsGlobalUnicast().() { |
| 215 | out = append(out, addr.IPNetscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Addr#IPNet.) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return out, nil |
| 220 | } |
| 221 | |
| 222 | // GetVethPeerIfindex returns the veth link object, the peer ifindex of the |
| 223 | // veth, or an error. This peer ifindex will only be valid in the peer's |
| 224 | // network namespace. |
| 225 | func GetVethPeerIfindexscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/GetVethPeerIfindex().(ifName string) (netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#, int, error) { |
| 226 | link, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(ifName) |
| 227 | if err != nil { |
| 228 | return nil, -1, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("could not look up %q: %v", ifName, err) |
| 229 | } |
| 230 | if _, ok := link.(*netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Vethscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Veth#); !ok { |
| 231 | return nil, -1, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("interface %q was not a veth interface", ifName) |
| 232 | } |
| 233 | |
| 234 | // veth supports IFLA_LINK (what vishvananda/netlink calls ParentIndex) |
| 235 | // on 4.1 and higher kernels |
| 236 | peerIndex := link.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().ParentIndexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#ParentIndex. |
| 237 | if peerIndex <= 0 { |
| 238 | // Fall back to ethtool for 4.0 and earlier kernels |
| 239 | e, err := ethtoolscip-go gomod github.com/safchain/ethtool v0.6.2 `github.com/safchain/ethtool`/.NewEthtoolscip-go gomod github.com/safchain/ethtool v0.6.2 `github.com/safchain/ethtool`/NewEthtool().() |
| 240 | if err != nil { |
| 241 | return nil, -1, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to initialize ethtool: %v", err) |
| 242 | } |
| 243 | defer e.Closescip-go gomod github.com/safchain/ethtool v0.6.2 `github.com/safchain/ethtool`/Ethtool#Close().() |
| 244 | |
| 245 | stats, err := e.Statsscip-go gomod github.com/safchain/ethtool v0.6.2 `github.com/safchain/ethtool`/Ethtool#Stats().(link.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name.) |
| 246 | if err != nil { |
| 247 | return nil, -1, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to request ethtool stats: %v", err) |
| 248 | } |
| 249 | n, ok := stats["peer_ifindex"] |
| 250 | if !ok { |
| 251 | return nil, -1, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to find 'peer_ifindex' in ethtool stats") |
| 252 | } |
| 253 | if n > 32767 || n == 0 { |
| 254 | return nil, -1, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("invalid 'peer_ifindex' %d", n) |
| 255 | } |
| 256 | peerIndex = int(n) |
| 257 | } |
| 258 | |
| 259 | return link, peerIndex, nil |
| 260 | } |