| 1 | // Copyright 2022 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/main/tap`/ |
| 16 | |
| 17 | import ( |
| 18 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/" |
| 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 | "os/execscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/" |
| 23 | "runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/" |
| 24 | "strconvscip-go gomod github.com/golang/go/src go1.24.2 strconv/" |
| 25 | "syscallscip-go gomod github.com/golang/go/src go1.24.2 syscall/" |
| 26 | |
| 27 | "github.com/opencontainers/selinux/go-selinuxscip-go gomod github.com/opencontainers/selinux v1.13.0 `github.com/opencontainers/selinux/go-selinux`/" |
| 28 | "github.com/vishvananda/netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/" |
| 29 | "golang.org/x/sys/unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/" |
| 30 | |
| 31 | "github.com/containernetworking/cni/pkg/skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/" |
| 32 | "github.com/containernetworking/cni/pkg/typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/" |
| 33 | currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/ "github.com/containernetworking/cni/pkg/types/100scip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/" |
| 34 | "github.com/containernetworking/cni/pkg/versionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/" |
| 35 | "github.com/containernetworking/plugins/pkg/ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/" |
| 36 | "github.com/containernetworking/plugins/pkg/ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/" |
| 37 | "github.com/containernetworking/plugins/pkg/netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/" |
| 38 | "github.com/containernetworking/plugins/pkg/nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/" |
| 39 | bvscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/ "github.com/containernetworking/plugins/pkg/utils/buildversionscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/" |
| 40 | "github.com/containernetworking/plugins/pkg/utils/sysctlscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/sysctl`/" |
| 41 | ) |
| 42 | |
| 43 | type NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf# struct { |
| 44 | typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#NetConf. |
| 45 | MultiQueuescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#MultiQueue. bool `json:"multiQueue"` |
| 46 | MTUscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#MTU. int `json:"mtu"` |
| 47 | Macscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Mac. string `json:"mac,omitempty"` |
| 48 | Ownerscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Owner. *uint32 `json:"owner,omitempty"` |
| 49 | Groupscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Group. *uint32 `json:"group,omitempty"` |
| 50 | SelinuxContextscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#SelinuxContext. string `json:"selinuxContext,omitempty"` |
| 51 | Bridgescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Bridge. string `json:"bridge,omitempty"` |
| 52 | Argsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Args. *struct{} `json:"args,omitempty"` |
| 53 | RuntimeConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#RuntimeConfig. struct { |
| 54 | Macscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#$anon_1fa12328be2dcae1#Mac. string `json:"mac,omitempty"` |
| 55 | } `json:"runtimeConfig,omitempty"` |
| 56 | } |
| 57 | |
| 58 | // MacEnvArgs represents CNI_ARG |
| 59 | type MacEnvArgsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/MacEnvArgs# struct { |
| 60 | typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.CommonArgsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/MacEnvArgs#CommonArgs. |
| 61 | MACscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/MacEnvArgs#MAC. typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.UnmarshallableStringscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/UnmarshallableString# `json:"mac,omitempty"` |
| 62 | } |
| 63 | |
| 64 | func initscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/init().() { |
| 65 | // this ensures that main runs only on main thread (thread group leader). |
| 66 | // since namespace ops (unshare, setns) are done for a single thread, we |
| 67 | // must ensure that the goroutine does not jump from OS thread to thread |
| 68 | runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/.LockOSThreadscip-go gomod github.com/golang/go/src go1.24.2 runtime/LockOSThread().() |
| 69 | } |
| 70 | |
| 71 | func loadConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/loadConf().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) (*NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#, string, error) { |
| 72 | n := &NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#{} |
| 73 | if err := jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/Unmarshal().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData., n); err != nil { |
| 74 | 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 load netconf: %v", err) |
| 75 | } |
| 76 | if args.Argsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Args. != "" { |
| 77 | e := MacEnvArgsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/MacEnvArgs#{} |
| 78 | err := typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.LoadArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/LoadArgs().(args.Argsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Args., &e) |
| 79 | if err != nil { |
| 80 | return nil, "", err |
| 81 | } |
| 82 | |
| 83 | if e.MACscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/MacEnvArgs#MAC. != "" { |
| 84 | n.Macscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Mac. = string(e.MACscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/MacEnvArgs#MAC.) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if n.RuntimeConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#RuntimeConfig..Macscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#$anon_1fa12328be2dcae1#Mac. != "" { |
| 89 | n.Macscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Mac. = n.RuntimeConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#RuntimeConfig..Macscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#$anon_1fa12328be2dcae1#Mac. |
| 90 | } |
| 91 | |
| 92 | return n, n.CNIVersionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#CNIVersion., nil |
| 93 | } |
| 94 | |
| 95 | // We want to share the parent process std{in|out|err} - fds 0 through 2. |
| 96 | // Since the FDs are inherited on fork / exec, we close on exec all others. |
| 97 | func closeFileDescriptorsOnExecscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/closeFileDescriptorsOnExec().() { |
| 98 | minFDToCloseOnExec := 3 |
| 99 | maxFDToCloseOnExec := 256 |
| 100 | for fd := minFDToCloseOnExec; fd < maxFDToCloseOnExec; fd++ { |
| 101 | syscallscip-go gomod github.com/golang/go/src go1.24.2 syscall/.CloseOnExecscip-go gomod github.com/golang/go/src go1.24.2 syscall/CloseOnExec().(fd) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Due to issues with the vishvananda/netlink library (fix pending) it is not possible to create an ownerless/groupless |
| 106 | // tap device. Until the issue is fixed, the workaround for creating a tap device with no owner/group is to use the iptool |
| 107 | func createTapWithIptoolscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/createTapWithIptool().(tmpName string, mtu int, multiqueue bool, mac string, owner *uint32, group *uint32) error { |
| 108 | closeFileDescriptorsOnExecscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/closeFileDescriptorsOnExec().() |
| 109 | |
| 110 | tapDeviceArgs := []string{"tuntap", "add", "mode", "tap", "name", tmpName} |
| 111 | if multiqueue { |
| 112 | tapDeviceArgs = append(tapDeviceArgs, "multi_queue") |
| 113 | } |
| 114 | |
| 115 | if owner != nil { |
| 116 | tapDeviceArgs = append(tapDeviceArgs, "user", 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().("%d", *owner)) |
| 117 | } |
| 118 | if group != nil { |
| 119 | tapDeviceArgs = append(tapDeviceArgs, "group", 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().("%d", *group)) |
| 120 | } |
| 121 | output, err := execscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/Command().("ip", tapDeviceArgs...).CombinedOutputscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/Cmd#CombinedOutput().() |
| 122 | if err != nil { |
| 123 | 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 run command %s: %v", output, err) |
| 124 | } |
| 125 | |
| 126 | tapDeviceArgs = []string{"link", "set", tmpName} |
| 127 | if mtu != 0 { |
| 128 | tapDeviceArgs = append(tapDeviceArgs, "mtu", strconvscip-go gomod github.com/golang/go/src go1.24.2 strconv/.Itoascip-go gomod github.com/golang/go/src go1.24.2 strconv/Itoa().(mtu)) |
| 129 | } |
| 130 | if mac != "" { |
| 131 | tapDeviceArgs = append(tapDeviceArgs, "address", mac) |
| 132 | } |
| 133 | output, err = execscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/Command().("ip", tapDeviceArgs...).CombinedOutputscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/Cmd#CombinedOutput().() |
| 134 | if err != nil { |
| 135 | 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 run command %s: %v", output, err) |
| 136 | } |
| 137 | return nil |
| 138 | } |
| 139 | |
| 140 | func createLinkWithNetlinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/createLinkWithNetlink().(tmpName string, mtu int, nsFd int, multiqueue bool, mac string, owner *uint32, group *uint32) error { |
| 141 | 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().() |
| 142 | linkAttrs.Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name. = tmpName |
| 143 | linkAttrs.Namespacescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Namespace. = 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#(nsFd) |
| 144 | if mtu != 0 { |
| 145 | linkAttrs.MTUscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#MTU. = mtu |
| 146 | } |
| 147 | mv := &netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Tuntapscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Tuntap#{ |
| 148 | LinkAttrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Tuntap#LinkAttrs.: linkAttrs, |
| 149 | Modescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Tuntap#Mode.: netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.TUNTAP_MODE_TAPscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/TUNTAP_MODE_TAP., |
| 150 | } |
| 151 | |
| 152 | if owner != nil { |
| 153 | mv.Ownerscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Tuntap#Owner. = *owner |
| 154 | } |
| 155 | if group != nil { |
| 156 | mv.Groupscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Tuntap#Group. = *group |
| 157 | } |
| 158 | |
| 159 | if mac != "" { |
| 160 | addr, 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) |
| 161 | if err != nil { |
| 162 | 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().("invalid args %v for MAC addr: %v", mac, err) |
| 163 | } |
| 164 | linkAttrs.HardwareAddrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#HardwareAddr. = addr |
| 165 | } |
| 166 | mv.Flagsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Tuntap#Flags. = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.TUNTAP_VNET_HDRscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/TUNTAP_VNET_HDR. | unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/.IFF_TAPscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/IFF_TAP. |
| 167 | if multiqueue { |
| 168 | mv.Flagsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Tuntap#Flags. = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.TUNTAP_MULTI_QUEUE_DEFAULTSscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/TUNTAP_MULTI_QUEUE_DEFAULTS. | mv.Flagsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Tuntap#Flags. |
| 169 | } |
| 170 | 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().(mv); err != nil { |
| 171 | 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 create tap: %v", err) |
| 172 | } |
| 173 | return nil |
| 174 | } |
| 175 | |
| 176 | func createLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/createLink().(tmpName string, conf *NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#, netns 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 { |
| 177 | switch { |
| 178 | case conf.SelinuxContextscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#SelinuxContext. != "": |
| 179 | if err := selinuxscip-go gomod github.com/opencontainers/selinux v1.13.0 `github.com/opencontainers/selinux/go-selinux`/.SetExecLabelscip-go gomod github.com/opencontainers/selinux v1.13.0 `github.com/opencontainers/selinux/go-selinux`/SetExecLabel().(conf.SelinuxContextscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#SelinuxContext.); err != nil { |
| 180 | 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 set socket label: %v", err) |
| 181 | } |
| 182 | return createTapWithIptoolscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/createTapWithIptool().(tmpName, conf.MTUscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#MTU., conf.MultiQueuescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#MultiQueue., conf.Macscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Mac., conf.Ownerscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Owner., conf.Groupscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Group.) |
| 183 | case conf.Ownerscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Owner. == nil || conf.Groupscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Group. == nil: |
| 184 | return createTapWithIptoolscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/createTapWithIptool().(tmpName, conf.MTUscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#MTU., conf.MultiQueuescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#MultiQueue., conf.Macscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Mac., conf.Ownerscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Owner., conf.Groupscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Group.) |
| 185 | default: |
| 186 | return createLinkWithNetlinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/createLinkWithNetlink().(tmpName, conf.MTUscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#MTU., int(netns.Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Fd.()), conf.MultiQueuescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#MultiQueue., conf.Macscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Mac., conf.Ownerscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Owner., conf.Groupscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Group.) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func createTapscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/createTap().(conf *NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#, ifName string, netns 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#) (*currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Interfacescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#, error) { |
| 191 | tap := ¤tscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Interfacescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#{} |
| 192 | // due to kernel bug we have to create with tmpName or it might |
| 193 | // collide with the name on the host and error out |
| 194 | tmpName, err := ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.RandomVethNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/RandomVethName().() |
| 195 | if err != nil { |
| 196 | return nil, err |
| 197 | } |
| 198 | |
| 199 | err = netns.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 { |
| 200 | err := createLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/createLink().(tmpName, conf, netns) |
| 201 | if err != nil { |
| 202 | return err |
| 203 | } |
| 204 | |
| 205 | if err = ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.RenameLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/RenameLink().(tmpName, ifName); err != nil { |
| 206 | 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().(tmpName) |
| 207 | if err != nil { |
| 208 | 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().(link) |
| 209 | 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 rename tap to %q: %v", ifName, err) |
| 210 | } |
| 211 | } |
| 212 | tap.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name. = ifName |
| 213 | |
| 214 | // Re-fetch link to get all properties/attributes |
| 215 | 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) |
| 216 | if err != nil { |
| 217 | 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 refetch tap %q: %v", ifName, err) |
| 218 | } |
| 219 | |
| 220 | if conf.Bridgescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Bridge. != "" { |
| 221 | bridge, 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().(conf.Bridgescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Bridge.) |
| 222 | if err != nil { |
| 223 | 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 get bridge %s: %v", conf.Bridgescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Bridge., err) |
| 224 | } |
| 225 | |
| 226 | tapDev := link |
| 227 | if 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().(tapDev, bridge); err != nil { |
| 228 | 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 tap %s as a port of bridge %s: %v", tap.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name., conf.Bridgescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#Bridge., err) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | 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().(link) |
| 233 | if err != nil { |
| 234 | 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 tap interface up: %v", err) |
| 235 | } |
| 236 | |
| 237 | tap.Macscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Mac. = link.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().HardwareAddrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#HardwareAddr..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/HardwareAddr#String().() |
| 238 | tap.Sandboxscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Sandbox. = netns.Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Path.() |
| 239 | |
| 240 | return nil |
| 241 | }) |
| 242 | if err != nil { |
| 243 | return nil, err |
| 244 | } |
| 245 | |
| 246 | return tap, nil |
| 247 | } |
| 248 | |
| 249 | func cmdAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/cmdAdd().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error { |
| 250 | n, cniVersion, err := loadConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/loadConf().(args) |
| 251 | if err != nil { |
| 252 | return err |
| 253 | } |
| 254 | |
| 255 | isLayer3 := n.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type. != "" |
| 256 | |
| 257 | netns, err := nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.GetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/GetNS().(args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns.) |
| 258 | if err != nil { |
| 259 | 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 open netns %q: %v", netns, err) |
| 260 | } |
| 261 | defer netns.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.() |
| 262 | |
| 263 | tapInterface, err := createTapscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/createTap().(n, args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName., netns) |
| 264 | if err != nil { |
| 265 | return err |
| 266 | } |
| 267 | |
| 268 | // Delete link if err to avoid link leak in this ns |
| 269 | defer func() { |
| 270 | if err != nil { |
| 271 | netns.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 { |
| 272 | return ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.DelLinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/DelLinkByName().(args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.) |
| 273 | }) |
| 274 | } |
| 275 | }() |
| 276 | |
| 277 | // Assume L2 interface only |
| 278 | result := ¤tscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#{ |
| 279 | CNIVersionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#CNIVersion.: currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.ImplementedSpecVersionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/ImplementedSpecVersion., |
| 280 | Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces.: []*currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Interfacescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#{tapInterface}, |
| 281 | } |
| 282 | |
| 283 | if isLayer3 { |
| 284 | // run the IPAM plugin and get back the config to apply |
| 285 | r, err := ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecAdd().(n.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.) |
| 286 | if err != nil { |
| 287 | return err |
| 288 | } |
| 289 | |
| 290 | // Invoke ipam del if err to avoid ip leak |
| 291 | defer func() { |
| 292 | if err != nil { |
| 293 | ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecDel().(n.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.) |
| 294 | } |
| 295 | }() |
| 296 | |
| 297 | // Convert whatever the IPAM result was into the current Result type |
| 298 | ipamResult, err := currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.NewResultFromResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/NewResultFromResult().(r) |
| 299 | if err != nil { |
| 300 | return err |
| 301 | } |
| 302 | |
| 303 | if len(ipamResult.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs.) == 0 { |
| 304 | return 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().("IPAM plugin returned missing IP config") |
| 305 | } |
| 306 | |
| 307 | result.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs. = ipamResult.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs. |
| 308 | result.Routesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Routes. = ipamResult.Routesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Routes. |
| 309 | |
| 310 | for _, ipc := range result.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs. { |
| 311 | // All addresses apply to the container tap interface |
| 312 | ipc.Interfacescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Interface. = currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Intscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Int().(0) |
| 313 | } |
| 314 | |
| 315 | err = netns.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 { |
| 316 | _, _ = 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/ipv4/conf/%s/arp_notify", args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.), "1") |
| 317 | |
| 318 | return ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ConfigureIfacescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ConfigureIface().(args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName., result) |
| 319 | }) |
| 320 | if err != nil { |
| 321 | return err |
| 322 | } |
| 323 | } else { |
| 324 | // For L2 just change interface status to up |
| 325 | err = netns.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 { |
| 326 | tapInterfaceLink, 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().(args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.) |
| 327 | if err != nil { |
| 328 | 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 find interface name %q: %v", tapInterface.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name., err) |
| 329 | } |
| 330 | 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().(tapInterfaceLink); err != nil { |
| 331 | 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", args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName., err) |
| 332 | } |
| 333 | |
| 334 | return nil |
| 335 | }) |
| 336 | if err != nil { |
| 337 | return err |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | result.DNSscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#DNS. = n.DNSscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#DNS. |
| 342 | return typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.PrintResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PrintResult().(result, cniVersion) |
| 343 | } |
| 344 | |
| 345 | func cmdDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/cmdDel().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error { |
| 346 | n, _, err := loadConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/loadConf().(args) |
| 347 | if err != nil { |
| 348 | return err |
| 349 | } |
| 350 | |
| 351 | isLayer3 := n.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type. != "" |
| 352 | |
| 353 | if isLayer3 { |
| 354 | err = ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecDel().(n.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.) |
| 355 | if err != nil { |
| 356 | return err |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns. == "" { |
| 361 | return nil |
| 362 | } |
| 363 | |
| 364 | // There is a netns so try to clean up. Delete can be called multiple times |
| 365 | // so don't return an error if the device is already removed. |
| 366 | err = nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.WithNetNSPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/WithNetNSPath().(args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns., 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 { |
| 367 | if err := ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.DelLinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/DelLinkByName().(args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.); err != nil { |
| 368 | if err != ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.ErrLinkNotFoundscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ErrLinkNotFound. { |
| 369 | return err |
| 370 | } |
| 371 | } |
| 372 | return nil |
| 373 | }) |
| 374 | if err != nil { |
| 375 | // if NetNs is passed down by the Cloud Orchestration Engine, or if it called multiple times |
| 376 | // so don't return an error if the device is already removed. |
| 377 | // https://github.com/kubernetes/kubernetes/issues/43014#issuecomment-287164444 |
| 378 | _, ok := err.(nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NSPathNotExistErrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotExistErr#) |
| 379 | if ok { |
| 380 | return nil |
| 381 | } |
| 382 | return err |
| 383 | } |
| 384 | |
| 385 | return err |
| 386 | } |
| 387 | |
| 388 | func mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/main().() { |
| 389 | skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.PluginMainFuncsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/PluginMainFuncs().(skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CNIFuncsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#{ |
| 390 | Addscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Add.: cmdAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/cmdAdd()., |
| 391 | Checkscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Check.: cmdCheckscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/cmdCheck()., |
| 392 | Delscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Del.: cmdDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/cmdDel()., |
| 393 | Statusscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Status.: cmdStatusscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/cmdStatus()., |
| 394 | /* FIXME GC */ |
| 395 | }, versionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/.Allscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/All., bvscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/.BuildStringscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/BuildString().("tap")) |
| 396 | } |
| 397 | |
| 398 | func cmdCheckscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/cmdCheck().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error { |
| 399 | n, _, err := loadConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/loadConf().(args) |
| 400 | if err != nil { |
| 401 | return err |
| 402 | } |
| 403 | isLayer3 := n.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type. != "" |
| 404 | |
| 405 | netns, err := nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.GetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/GetNS().(args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns.) |
| 406 | if err != nil { |
| 407 | 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 open netns %q: %v", args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns., err) |
| 408 | } |
| 409 | defer netns.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.() |
| 410 | |
| 411 | if isLayer3 { |
| 412 | // run the IPAM plugin and get back the config to apply |
| 413 | err = ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecCheckscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecCheck().(n.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.) |
| 414 | if err != nil { |
| 415 | return err |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Parse previous result. |
| 420 | if n.NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#NetConf..RawPrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#RawPrevResult. == nil { |
| 421 | 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().("Required prevResult missing") |
| 422 | } |
| 423 | |
| 424 | if err := versionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/.ParsePrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/ParsePrevResult().(&n.NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#NetConf.); err != nil { |
| 425 | return err |
| 426 | } |
| 427 | |
| 428 | result, err := currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.NewResultFromResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/NewResultFromResult().(n.PrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#PrevResult.) |
| 429 | if err != nil { |
| 430 | return err |
| 431 | } |
| 432 | |
| 433 | var contMap currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Interfacescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface# |
| 434 | // Find interfaces for names whe know, tap device name inside container |
| 435 | for _, intf := range result.Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces. { |
| 436 | if args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName. == intf.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name. { |
| 437 | if args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns. == intf.Sandboxscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Sandbox. { |
| 438 | contMap = *intf |
| 439 | continue |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // The namespace must be the same as what was configured |
| 445 | if args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns. != contMap.Sandboxscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Sandbox. { |
| 446 | 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().("Sandbox in prevResult %s doesn't match configured netns: %s", |
| 447 | contMap.Sandboxscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Sandbox., args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns.) |
| 448 | } |
| 449 | |
| 450 | // Check prevResults for ips, routes and dns against values found in the container |
| 451 | return netns.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 { |
| 452 | err = ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.ValidateExpectedInterfaceIPsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ValidateExpectedInterfaceIPs().(args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName., result.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs.) |
| 453 | if err != nil { |
| 454 | return err |
| 455 | } |
| 456 | |
| 457 | err = ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.ValidateExpectedRoutescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ValidateExpectedRoute().(result.Routesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Routes.) |
| 458 | if err != nil { |
| 459 | return err |
| 460 | } |
| 461 | return nil |
| 462 | }) |
| 463 | } |
| 464 | |
| 465 | func cmdStatusscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/cmdStatus().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error { |
| 466 | conf := NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/tap`/NetConf#{} |
| 467 | if err := jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/Unmarshal().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData., &conf); err != nil { |
| 468 | 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 load netconf: %w", err) |
| 469 | } |
| 470 | |
| 471 | if conf.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type. != "" { |
| 472 | if err := ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecStatusscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecStatus().(conf.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.); err != nil { |
| 473 | return err |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | return nil |
| 478 | } |