| 1 | // Copyright 2020 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 mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/" |
| 19 | "mathscip-go gomod github.com/golang/go/src go1.24.2 math/" |
| 20 | "netscip-go gomod github.com/golang/go/src go1.24.2 net/" |
| 21 | "timescip-go gomod github.com/golang/go/src go1.24.2 time/" |
| 22 | |
| 23 | "github.com/vishvananda/netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/" |
| 24 | |
| 25 | "github.com/containernetworking/plugins/pkg/netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/" |
| 26 | ) |
| 27 | |
| 28 | // findVRF finds a VRF link with the provided name. |
| 29 | func findVRFscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/findVRF().(name string) (*netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Vrfscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#, error) { |
| 30 | 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().(name) |
| 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | vrf, ok := link.(*netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Vrfscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#) |
| 35 | if !ok { |
| 36 | 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().("Netlink %s is not a VRF", name) |
| 37 | } |
| 38 | return vrf, nil |
| 39 | } |
| 40 | |
| 41 | // createVRF creates a new VRF and sets it up. |
| 42 | func createVRFscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/createVRF().(name string, tableID uint32) (*netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Vrfscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#, error) { |
| 43 | links, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkListscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkList().() |
| 44 | if err != nil { |
| 45 | 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().("createVRF: Failed to find links %v", err) |
| 46 | } |
| 47 | |
| 48 | if tableID == 0 { |
| 49 | tableID, err = findFreeRoutingTableIDscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/findFreeRoutingTableID().(links) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | 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().() |
| 56 | linkAttrs.Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name. = name |
| 57 | vrf := &netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Vrfscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#{ |
| 58 | LinkAttrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#LinkAttrs.: linkAttrs, |
| 59 | Tablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#Table.: tableID, |
| 60 | } |
| 61 | |
| 62 | 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().(vrf) |
| 63 | if err != nil { |
| 64 | 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().("could not add VRF %s: %v", name, err) |
| 65 | } |
| 66 | 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().(vrf) |
| 67 | if err != nil { |
| 68 | 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().("could not set link up for VRF %s: %v", name, err) |
| 69 | } |
| 70 | |
| 71 | return vrf, nil |
| 72 | } |
| 73 | |
| 74 | // assignedInterfaces returns the list of interfaces associated to the given vrf. |
| 75 | func assignedInterfacesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/assignedInterfaces().(vrf *netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Vrfscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#) ([]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) { |
| 76 | links, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkListscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkList().() |
| 77 | if err != nil { |
| 78 | 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().("getAssignedInterfaces: Failed to find links %v", err) |
| 79 | } |
| 80 | res := make([]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#, 0) |
| 81 | for _, l := range links { |
| 82 | if l.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().MasterIndexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#MasterIndex. == vrf.Indexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Index. { |
| 83 | res = append(res, l) |
| 84 | } |
| 85 | } |
| 86 | return res, nil |
| 87 | } |
| 88 | |
| 89 | // addInterface adds the given interface to the VRF |
| 90 | func addInterfacescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/addInterface().(vrf *netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Vrfscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#, intf string) error { |
| 91 | i, 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().(intf) |
| 92 | if err != nil { |
| 93 | 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().("could not get link by name %s", intf) |
| 94 | } |
| 95 | |
| 96 | if i.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().MasterIndexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#MasterIndex. != 0 { |
| 97 | master, err := netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkByIndexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkByIndex().(i.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().MasterIndexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#MasterIndex.) |
| 98 | if err != nil { |
| 99 | 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().("interface %s has already a master set, could not retrieve the name: %v", intf, err) |
| 100 | } |
| 101 | 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().("interface %s has already a master set: %s", intf, master.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.) |
| 102 | } |
| 103 | |
| 104 | // Global IPV6 addresses are not maintained unless |
| 105 | // sysctl -w net.ipv6.conf.all.keep_addr_on_down=1 is called |
| 106 | // so we save it, and restore it back. |
| 107 | beforeAddresses, err := getGlobalAddressesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/getGlobalAddresses().(i, netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.FAMILY_V6scip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/FAMILY_V6.) |
| 108 | if err != nil { |
| 109 | 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 getting global ipv6 addresses before slaving interface: %w", err) |
| 110 | } |
| 111 | |
| 112 | // Save all routes that are not local and connected, before setting master, |
| 113 | // because otherwise those routes will be deleted after interface is moved. |
| 114 | filter := &netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Routescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Route#{ |
| 115 | LinkIndexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Route#LinkIndex.: i.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Indexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Index., |
| 116 | Scopescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Route#Scope.: netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.SCOPE_UNIVERSEscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/SCOPE_UNIVERSE., // Exclude local and connected routes |
| 117 | } |
| 118 | filterMask := netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.RT_FILTER_OIFscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/RT_FILTER_OIF. | netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.RT_FILTER_SCOPEscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/RT_FILTER_SCOPE. // Filter based on link index and scope |
| 119 | globalRoutes, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.RouteListFilteredscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/RouteListFiltered().(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., filter, filterMask) |
| 120 | if err != nil { |
| 121 | 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 getting all routes for %s", intf) |
| 122 | } |
| 123 | |
| 124 | err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetMasterscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetMaster().(i, vrf) |
| 125 | if err != nil { |
| 126 | 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().("could not set vrf %s as master of %s: %v", vrf.Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name., intf, err) |
| 127 | } |
| 128 | |
| 129 | // Used to identify which global IPV6 addresses are missing |
| 130 | afterAddresses, err := getGlobalAddressesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/getGlobalAddresses().(i, netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.FAMILY_V6scip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/FAMILY_V6.) |
| 131 | if err != nil { |
| 132 | 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 getting global ipv6 addresses after slaving interface: %w", err) |
| 133 | } |
| 134 | |
| 135 | // Since keeping the ipv6 address depends on net.ipv6.conf.all.keep_addr_on_down , |
| 136 | // we check if the new interface does not have them and in case we restore them. |
| 137 | CONTINUE: |
| 138 | for _, toFind := range beforeAddresses { |
| 139 | for _, current := range afterAddresses { |
| 140 | if toFind.Equalscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Addr#Equal().(current) { |
| 141 | continue CONTINUE |
| 142 | } |
| 143 | } |
| 144 | // Not found, re-adding it |
| 145 | err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.AddrAddscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/AddrAdd().(i, &toFind) |
| 146 | if err != nil { |
| 147 | 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().("could not restore address %s to %s @ %s: %v", toFind, intf, vrf.Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name., err) |
| 148 | } |
| 149 | |
| 150 | // Waits for global IPV6 addresses to be added by the kernel. |
| 151 | backoffBase := 10 * timescip-go gomod github.com/golang/go/src go1.24.2 time/.Millisecondscip-go gomod github.com/golang/go/src go1.24.2 time/Millisecond. |
| 152 | maxRetries := 8 |
| 153 | for retryCount := 0; retryCount <= maxRetries; retryCount++ { |
| 154 | routesVRFTable, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.RouteListFilteredscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/RouteListFiltered().( |
| 155 | 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., |
| 156 | &netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Routescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Route#{ |
| 157 | Dstscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Route#Dst.: &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#{ |
| 158 | IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP.: toFind.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP., |
| 159 | Maskscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#Mask.: netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPMaskscip-go gomod github.com/golang/go/src go1.24.2 net/IPMask#{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 160 | }, |
| 161 | Tablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Route#Table.: int(vrf.Tablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#Table.), |
| 162 | LinkIndexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Route#LinkIndex.: i.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Indexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Index., |
| 163 | }, |
| 164 | netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.RT_FILTER_OIFscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/RT_FILTER_OIF.|netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.RT_FILTER_TABLEscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/RT_FILTER_TABLE.|netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.RT_FILTER_DSTscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/RT_FILTER_DST., |
| 165 | ) |
| 166 | if err != nil { |
| 167 | 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 getting routes for %s table %d for dst %s: %v", intf, vrf.Tablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#Table., toFind.IPNetscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Addr#IPNet..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#String().(), err) |
| 168 | } |
| 169 | |
| 170 | if len(routesVRFTable) >= 1 { |
| 171 | break |
| 172 | } |
| 173 | |
| 174 | if retryCount == maxRetries { |
| 175 | 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 getting local/host addresses for %s in table %d with dst %s", intf, vrf.Tablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#Table., toFind.IPNetscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Addr#IPNet..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#String().()) |
| 176 | } |
| 177 | |
| 178 | // Exponential backoff - 10ms, 20m, 40ms, 80ms, 160ms, 320ms, 640ms, 1280ms |
| 179 | // Approx 2,5 seconds total |
| 180 | timescip-go gomod github.com/golang/go/src go1.24.2 time/.Sleepscip-go gomod github.com/golang/go/src go1.24.2 time/Sleep().(backoffBase * timescip-go gomod github.com/golang/go/src go1.24.2 time/.Durationscip-go gomod github.com/golang/go/src go1.24.2 time/Duration#(1<<retryCount)) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Apply all saved routes for the interface that was moved to the VRF |
| 185 | for _, route := range globalRoutes { |
| 186 | r := route |
| 187 | // Modify original table to vrf one, |
| 188 | r.Tablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Route#Table. = int(vrf.Tablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#Table.) |
| 189 | // equivalent of 'ip route replace <address> table <int>'. |
| 190 | err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.RouteReplacescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/RouteReplace().(&r) |
| 191 | if err != nil { |
| 192 | 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().("could not add route '%s': %v", r, err) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return nil |
| 197 | } |
| 198 | |
| 199 | func findFreeRoutingTableIDscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/findFreeRoutingTableID().(links []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#) (uint32, error) { |
| 200 | takenTables := make(map[uint32]struct{}, len(links)) |
| 201 | for _, l := range links { |
| 202 | if vrf, ok := l.(*netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Vrfscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#); ok { |
| 203 | takenTables[vrf.Tablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#Table.] = struct{}{} |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | for res := uint32(1); res < mathscip-go gomod github.com/golang/go/src go1.24.2 math/.MaxUint32scip-go gomod github.com/golang/go/src go1.24.2 math/MaxUint32.; res++ { |
| 208 | if _, ok := takenTables[res]; !ok { |
| 209 | return res, nil |
| 210 | } |
| 211 | } |
| 212 | return 0, 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().("findFreeRoutingTableID: Failed to find an available routing id") |
| 213 | } |
| 214 | |
| 215 | func resetMasterscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/resetMaster().(interfaceName string) error { |
| 216 | intf, 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().(interfaceName) |
| 217 | if err != nil { |
| 218 | 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().("resetMaster: could not get link by name %s", interfaceName) |
| 219 | } |
| 220 | err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNoMasterscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetNoMaster().(intf) |
| 221 | if err != nil { |
| 222 | 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().("resetMaster: could reset master to %s", interfaceName) |
| 223 | } |
| 224 | return nil |
| 225 | } |
| 226 | |
| 227 | // getGlobalAddresses returns the global addresses of the given interface |
| 228 | func getGlobalAddressesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/getGlobalAddresses().(link 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#, family int) ([]netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Addrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Addr#, error) { |
| 229 | addresses, 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().(link, family) |
| 230 | if err != nil { |
| 231 | 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 getting list of IP addresses for %s: %w", 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., err) |
| 232 | } |
| 233 | |
| 234 | globalAddresses := make([]netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Addrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Addr#, 0, len(addresses)) |
| 235 | for _, addr := range addresses { |
| 236 | if addr.Scopescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Addr#Scope. == int(netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.SCOPE_UNIVERSEscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/SCOPE_UNIVERSE.) { |
| 237 | globalAddresses = append(globalAddresses, addr) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return globalAddresses, nil |
| 242 | } |