| 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 main |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "fmt" |
| 20 | |
| 21 | "github.com/vishvananda/netlink" |
| 22 | |
| 23 | "github.com/containernetworking/cni/pkg/skel" |
| 24 | "github.com/containernetworking/cni/pkg/types" |
| 25 | current "github.com/containernetworking/cni/pkg/types/100" |
| 26 | "github.com/containernetworking/cni/pkg/version" |
| 27 | "github.com/containernetworking/plugins/pkg/ns" |
| 28 | bv "github.com/containernetworking/plugins/pkg/utils/buildversion" |
| 29 | ) |
| 30 | |
| 31 | // VRFNetConf represents the vrf configuration. |
| 32 | type VRFNetConf struct { |
| 33 | types.NetConf |
| 34 | |
| 35 | // VRFName is the name of the vrf to add the interface to. |
| 36 | VRFName string `json:"vrfname"` |
| 37 | // Table is the optional name of the routing table set for the vrf |
| 38 | Table uint32 `json:"table"` |
| 39 | } |
| 40 | |
| 41 | func main() { |
| 42 | skel.PluginMainFuncs(skel.CNIFuncs{ |
| 43 | Add: cmdAdd, |
| 44 | Check: cmdCheck, |
| 45 | Del: cmdDel, |
| 46 | /* FIXME GC */ |
| 47 | /* FIXME Status */ |
| 48 | }, version.VersionsStartingFrom("0.3.1"), bv.BuildString("vrf")) |
| 49 | } |
| 50 | |
| 51 | func cmdAdd(args *skel.CmdArgs) error { |
| 52 | conf, result, err := parseConf(args.StdinData) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | if conf.PrevResult == nil { |
| 58 | return fmt.Errorf("missing prevResult from earlier plugin") |
| 59 | } |
| 60 | |
| 61 | err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { |
| 62 | vrf, err := findVRF(conf.VRFName) |
| 63 | |
| 64 | // If the user set a tableid and the vrf is already in the namespace |
| 65 | // we check if the tableid is the same one already assigned to the vrf. |
| 66 | if err == nil && conf.Table != 0 && vrf.Table != conf.Table { |
| 67 | return fmt.Errorf("VRF %s already exist with different routing table %d", conf.VRFName, vrf.Table) |
| 68 | } |
| 69 | |
| 70 | if _, ok := err.(netlink.LinkNotFoundError); ok { |
| 71 | vrf, err = createVRF(conf.VRFName, conf.Table) |
| 72 | } |
| 73 | |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | err = addInterface(vrf, args.IfName) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | return nil |
| 83 | }) |
| 84 | if err != nil { |
| 85 | return fmt.Errorf("cmdAdd failed: %v", err) |
| 86 | } |
| 87 | |
| 88 | if result == nil { |
| 89 | result = ¤t.Result{} |
| 90 | } |
| 91 | |
| 92 | return types.PrintResult(result, conf.CNIVersion) |
| 93 | } |
| 94 | |
| 95 | func cmdDel(args *skel.CmdArgs) error { |
| 96 | conf, _, err := parseConf(args.StdinData) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { |
| 101 | vrf, err := findVRF(conf.VRFName) |
| 102 | if _, ok := err.(netlink.LinkNotFoundError); ok { |
| 103 | return nil |
| 104 | } |
| 105 | |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | |
| 110 | err = resetMaster(args.IfName) |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | |
| 115 | interfaces, err := assignedInterfaces(vrf) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | |
| 120 | // Meaning, we are deleting the last interface assigned to the VRF |
| 121 | if len(interfaces) == 0 { |
| 122 | err = netlink.LinkDel(vrf) |
| 123 | if err != nil { |
| 124 | return err |
| 125 | } |
| 126 | } |
| 127 | return nil |
| 128 | }) |
| 129 | if err != nil { |
| 130 | // if NetNs is passed down by the Cloud Orchestration Engine, or if it called multiple times |
| 131 | // so don't return an error if the device is already removed. |
| 132 | // https://github.com/kubernetes/kubernetes/issues/43014#issuecomment-287164444 |
| 133 | _, ok := err.(ns.NSPathNotExistErr) |
| 134 | if ok { |
| 135 | return nil |
| 136 | } |
| 137 | return err |
| 138 | } |
| 139 | |
| 140 | if err != nil { |
| 141 | return fmt.Errorf("cmdDel failed: %v", err) |
| 142 | } |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | func cmdCheck(args *skel.CmdArgs) error { |
| 147 | conf, _, err := parseConf(args.StdinData) |
| 148 | if err != nil { |
| 149 | return err |
| 150 | } |
| 151 | |
| 152 | // Ensure we have previous result. |
| 153 | if conf.PrevResult == nil { |
| 154 | return fmt.Errorf("missing prevResult from earlier plugin") |
| 155 | } |
| 156 | |
| 157 | err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { |
| 158 | vrf, err := findVRF(conf.VRFName) |
| 159 | if err != nil { |
| 160 | return err |
| 161 | } |
| 162 | vrfInterfaces, err := assignedInterfaces(vrf) |
| 163 | if err != nil { |
| 164 | return err |
| 165 | } |
| 166 | |
| 167 | found := false |
| 168 | for _, intf := range vrfInterfaces { |
| 169 | if intf.Attrs().Name == args.IfName { |
| 170 | found = true |
| 171 | break |
| 172 | } |
| 173 | } |
| 174 | if !found { |
| 175 | return fmt.Errorf("failed to find %s associated to vrf %s", args.IfName, conf.VRFName) |
| 176 | } |
| 177 | return nil |
| 178 | }) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | func parseConf(data []byte) (*VRFNetConf, *current.Result, error) { |
| 187 | conf := VRFNetConf{} |
| 188 | if err := json.Unmarshal(data, &conf); err != nil { |
| 189 | return nil, nil, fmt.Errorf("failed to load netconf: %v", err) |
| 190 | } |
| 191 | |
| 192 | if conf.VRFName == "" { |
| 193 | return nil, nil, fmt.Errorf("configuration is expected to have a valid vrf name") |
| 194 | } |
| 195 | |
| 196 | if conf.RawPrevResult == nil { |
| 197 | // return early if there was no previous result, which is allowed for DEL calls |
| 198 | return &conf, ¤t.Result{}, nil |
| 199 | } |
| 200 | |
| 201 | // Parse previous result. |
| 202 | var result *current.Result |
| 203 | var err error |
| 204 | if err = version.ParsePrevResult(&conf.NetConf); err != nil { |
| 205 | return nil, nil, fmt.Errorf("could not parse prevResult: %v", err) |
| 206 | } |
| 207 | |
| 208 | result, err = current.NewResultFromResult(conf.PrevResult) |
| 209 | if err != nil { |
| 210 | return nil, nil, fmt.Errorf("could not convert result to current version: %v", err) |
| 211 | } |
| 212 | |
| 213 | return &conf, result, nil |
| 214 | } |