Debian Codegraph @

plugins/main/windows/win-overlay/win-overlay_windows.go

raw symbols SCIP
1 // Copyright 2017 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 "net"
21 "runtime"
22 "strings"
23
24 "github.com/Microsoft/hcsshim"
25
26 "github.com/Microsoft/hcsshim/hcn"
27 "github.com/containernetworking/cni/pkg/skel"
28 "github.com/containernetworking/cni/pkg/types"
29 current "github.com/containernetworking/cni/pkg/types/100"
30 "github.com/containernetworking/cni/pkg/version"
31
32 "github.com/containernetworking/plugins/pkg/errors"
33 "github.com/containernetworking/plugins/pkg/hns"
34 "github.com/containernetworking/plugins/pkg/ipam"
35 bv "github.com/containernetworking/plugins/pkg/utils/buildversion"
36 )
37
38 type NetConf struct {
39 hns.NetConf
40
41 IPMasq bool `json:"ipMasq"`
42 EndpointMacPrefix string `json:"endpointMacPrefix,omitempty"`
43 }
44
45 func init() {
46 // this ensures that main runs only on main thread (thread group leader).
47 // since namespace ops (unshare, setns) are done for a single thread, we
48 // must ensure that the goroutine does not jump from OS thread to thread
49 runtime.LockOSThread()
50 }
51
52 func loadNetConf(bytes []byte) (*NetConf, string, error) {
53 n := &NetConf{}
54 if err := json.Unmarshal(bytes, n); err != nil {
55 return nil, "", fmt.Errorf("failed to load netconf: %v", err)
56 }
57 return n, n.CNIVersion, nil
58 }
59
60 func processEndpointArgs(args *skel.CmdArgs, n *NetConf) (*hns.EndpointInfo, error) {
61 epInfo := new(hns.EndpointInfo)
62 epInfo.NetworkName = n.Name
63 epInfo.EndpointName = hns.ConstructEndpointName(args.ContainerID, args.Netns, epInfo.NetworkName)
64
65 if n.IPAM.Type != "" {
66 r, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData)
67 if err != nil {
68 return nil, errors.Annotatef(err, "error while executing IPAM addition")
69 }
70
71 // convert whatever the IPAM result was into the current result
72 result, err := current.NewResultFromResult(r)
73 if err != nil {
74 return nil, errors.Annotatef(err, "error while converting the result from IPAM addition")
75 }
76 if len(result.IPs) == 0 {
77 return nil, fmt.Errorf("IPAM plugin return is missing IP config")
78 }
79 epInfo.IpAddress = result.IPs[0].Address.IP.To4()
80 if epInfo.IpAddress == nil {
81 return nil, fmt.Errorf("IPAM plugin return is missing valid IP Address")
82 }
83 epInfo.MacAddress = fmt.Sprintf("%v-%02x-%02x-%02x-%02x", n.EndpointMacPrefix, epInfo.IpAddress[0], epInfo.IpAddress[1], epInfo.IpAddress[2], epInfo.IpAddress[3])
84
85 }
86 epInfo.DNS = n.GetDNS()
87 if n.LoopbackDSR {
88 n.ApplyLoopbackDSRPolicy(&epInfo.IpAddress)
89 }
90 return epInfo, nil
91 }
92
93 func cmdHcnAdd(args *skel.CmdArgs, n *NetConf) (*current.Result, error) {
94 if len(n.EndpointMacPrefix) != 0 {
95 if len(n.EndpointMacPrefix) != 5 || n.EndpointMacPrefix[2] != '-' {
96 return nil, fmt.Errorf("endpointMacPrefix [%v] is invalid, value must be of the format xx-xx", n.EndpointMacPrefix)
97 }
98 } else {
99 n.EndpointMacPrefix = "0E-2A"
100 }
101
102 networkName := n.Name
103 hnsNetwork, err := hcsshim.GetHNSNetworkByName(networkName)
104 hcnNetwork, err := hcn.GetNetworkByName(networkName)
105 if err != nil {
106 return nil, errors.Annotatef(err, "error while hcn.GetNetworkByName(%s)", networkName)
107 }
108 if hcnNetwork == nil {
109 return nil, fmt.Errorf("network %v is not found", networkName)
110 }
111 if hnsNetwork == nil {
112 return nil, fmt.Errorf("network %v not found", networkName)
113 }
114
115 if !strings.EqualFold(string(hcnNetwork.Type), "Overlay") {
116 return nil, fmt.Errorf("network %v is of an unexpected type: %v", networkName, hcnNetwork.Type)
117 }
118
119 epName := hns.ConstructEndpointName(args.ContainerID, args.Netns, n.Name)
120
121 hcnEndpoint, err := hns.AddHcnEndpoint(epName, hcnNetwork.Id, args.Netns, func() (*hcn.HostComputeEndpoint, error) {
122 epInfo, err := processEndpointArgs(args, n)
123 if err != nil {
124 return nil, errors.Annotate(err, "error while processing endpoint args")
125 }
126 epInfo.NetworkId = hcnNetwork.Id
127 gatewayAddr := net.ParseIP(hnsNetwork.Subnets[0].GatewayAddress)
128 epInfo.Gateway = gatewayAddr.To4()
129 n.ApplyDefaultPAPolicy(hnsNetwork.ManagementIP)
130 if n.IPMasq {
131 n.ApplyOutboundNatPolicy(hnsNetwork.Subnets[0].AddressPrefix)
132 }
133 hcnEndpoint, err := hns.GenerateHcnEndpoint(epInfo, &n.NetConf)
134 if err != nil {
135 return nil, errors.Annotate(err, "error while generating HostComputeEndpoint")
136 }
137 return hcnEndpoint, nil
138 })
139 if err != nil {
140 return nil, errors.Annotate(err, "error while adding HostComputeEndpoint")
141 }
142
143 result, err := hns.ConstructHcnResult(hcnNetwork, hcnEndpoint)
144 if err != nil {
145 ipam.ExecDel(n.IPAM.Type, args.StdinData)
146 return nil, errors.Annotate(err, "error while constructing HostComputeEndpoint addition result")
147 }
148
149 return result, nil
150 }
151
152 func cmdHnsAdd(args *skel.CmdArgs, n *NetConf) (*current.Result, error) {
153 success := false
154
155 if len(n.EndpointMacPrefix) != 0 {
156 if len(n.EndpointMacPrefix) != 5 || n.EndpointMacPrefix[2] != '-' {
157 return nil, fmt.Errorf("endpointMacPrefix [%v] is invalid, value must be of the format xx-xx", n.EndpointMacPrefix)
158 }
159 } else {
160 n.EndpointMacPrefix = "0E-2A"
161 }
162
163 networkName := n.Name
164 hnsNetwork, err := hcsshim.GetHNSNetworkByName(networkName)
165 if err != nil {
166 return nil, errors.Annotatef(err, "error while GETHNSNewtorkByName(%s)", networkName)
167 }
168
169 if hnsNetwork == nil {
170 return nil, fmt.Errorf("network %v not found", networkName)
171 }
172
173 if !strings.EqualFold(hnsNetwork.Type, "Overlay") {
174 return nil, fmt.Errorf("network %v is of an unexpected type: %v", networkName, hnsNetwork.Type)
175 }
176
177 epName := hns.ConstructEndpointName(args.ContainerID, args.Netns, n.Name)
178
179 hnsEndpoint, err := hns.AddHnsEndpoint(epName, hnsNetwork.Id, args.ContainerID, args.Netns, func() (*hcsshim.HNSEndpoint, error) {
180 // run the IPAM plugin and get back the config to apply
181 r, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData)
182 if err != nil {
183 return nil, errors.Annotatef(err, "error while ipam.ExecAdd")
184 }
185
186 // Convert whatever the IPAM result was into the current Result type
187 result, err := current.NewResultFromResult(r)
188 if err != nil {
189 return nil, errors.Annotatef(err, "error while NewResultFromResult")
190 }
191
192 if len(result.IPs) == 0 {
193 return nil, fmt.Errorf("IPAM plugin return is missing IP config")
194 }
195
196 ipAddr := result.IPs[0].Address.IP.To4()
197 if ipAddr == nil {
198 return nil, fmt.Errorf("win-overlay doesn't support IPv6 now")
199 }
200
201 // conjure a MAC based on the IP for Overlay
202 macAddr := fmt.Sprintf("%v-%02x-%02x-%02x-%02x", n.EndpointMacPrefix, ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3])
203 // use the HNS network gateway
204 gw := hnsNetwork.Subnets[0].GatewayAddress
205 n.ApplyDefaultPAPolicy(hnsNetwork.ManagementIP)
206 if n.IPMasq {
207 n.ApplyOutboundNatPolicy(hnsNetwork.Subnets[0].AddressPrefix)
208 }
209
210 result.DNS = n.GetDNS()
211 if n.LoopbackDSR {
212 n.ApplyLoopbackDSRPolicy(&ipAddr)
213 }
214 hnsEndpoint := &hcsshim.HNSEndpoint{
215 Name: epName,
216 VirtualNetwork: hnsNetwork.Id,
217 DNSServerList: strings.Join(result.DNS.Nameservers, ","),
218 DNSSuffix: strings.Join(result.DNS.Search, ","),
219 GatewayAddress: gw,
220 IPAddress: ipAddr,
221 MacAddress: macAddr,
222 Policies: n.GetHNSEndpointPolicies(),
223 }
224
225 return hnsEndpoint, nil
226 })
227 defer func() {
228 if !success {
229 ipam.ExecDel(n.IPAM.Type, args.StdinData)
230 }
231 }()
232 if err != nil {
233 return nil, errors.Annotatef(err, "error while AddHnsEndpoint(%v,%v,%v)", epName, hnsNetwork.Id, args.ContainerID)
234 }
235
236 result, err := hns.ConstructHnsResult(hnsNetwork, hnsEndpoint)
237 if err != nil {
238 return nil, errors.Annotatef(err, "error while constructResult")
239 }
240
241 success = true
242 return result, nil
243 }
244
245 func cmdAdd(args *skel.CmdArgs) error {
246 n, cniVersion, err := loadNetConf(args.StdinData)
247 if err != nil {
248 return err
249 }
250
251 var result *current.Result
252 if n.ApiVersion == 2 {
253 result, err = cmdHcnAdd(args, n)
254 } else {
255 result, err = cmdHnsAdd(args, n)
256 }
257 if err != nil {
258 ipam.ExecDel(n.IPAM.Type, args.StdinData)
259 return err
260 }
261
262 return types.PrintResult(result, cniVersion)
263 }
264
265 func cmdDel(args *skel.CmdArgs) error {
266 n, _, err := loadNetConf(args.StdinData)
267 if err != nil {
268 return err
269 }
270
271 if n.IPAM.Type != "" {
272 if err := ipam.ExecDel(n.IPAM.Type, args.StdinData); err != nil {
273 return err
274 }
275 }
276 epName := hns.ConstructEndpointName(args.ContainerID, args.Netns, n.Name)
277
278 if n.ApiVersion == 2 {
279 return hns.RemoveHcnEndpoint(epName)
280 }
281 return hns.RemoveHnsEndpoint(epName, args.Netns, args.ContainerID)
282 }
283
284 func cmdCheck(_ *skel.CmdArgs) error {
285 // TODO: implement
286 return nil
287 }
288
289 func main() {
290 skel.PluginMainFuncs(skel.CNIFuncs{
291 Add: cmdAdd,
292 Check: cmdCheck,
293 Del: cmdDel,
294 Status: cmdStatus,
295 /* FIXME GC */
296 }, version.All, bv.BuildString("win-overlay"))
297 }
298
299 func cmdStatus(args *skel.CmdArgs) error {
300 conf := NetConf{}
301 if err := json.Unmarshal(args.StdinData, &conf); err != nil {
302 return fmt.Errorf("failed to load netconf: %w", err)
303 }
304
305 if err := ipam.ExecStatus(conf.IPAM.Type, args.StdinData); err != nil {
306 return err
307 }
308
309 return nil
310 }