Debian Codegraph @

plugins/main/windows/win-bridge/win-bridge_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 "runtime"
21 "strings"
22
23 "github.com/Microsoft/hcsshim"
24 "github.com/Microsoft/hcsshim/hcn"
25
26 "github.com/containernetworking/cni/pkg/skel"
27 "github.com/containernetworking/cni/pkg/types"
28 current "github.com/containernetworking/cni/pkg/types/100"
29 "github.com/containernetworking/cni/pkg/version"
30
31 "github.com/containernetworking/plugins/pkg/errors"
32 "github.com/containernetworking/plugins/pkg/hns"
33 "github.com/containernetworking/plugins/pkg/ipam"
34 bv "github.com/containernetworking/plugins/pkg/utils/buildversion"
35 )
36
37 type NetConf struct {
38 hns.NetConf
39
40 IPMasqNetwork string `json:"ipMasqNetwork,omitempty"`
41 }
42
43 func init() {
44 // this ensures that main runs only on main thread (thread group leader).
45 // since namespace ops (unshare, setns) are done for a single thread, we
46 // must ensure that the goroutine does not jump from OS thread to thread
47 runtime.LockOSThread()
48 }
49
50 func loadNetConf(bytes []byte) (*NetConf, string, error) {
51 n := &NetConf{}
52 if err := json.Unmarshal(bytes, n); err != nil {
53 return nil, "", fmt.Errorf("failed to load netconf: %v", err)
54 }
55 return n, n.CNIVersion, nil
56 }
57
58 func processEndpointArgs(args *skel.CmdArgs, n *NetConf) (*hns.EndpointInfo, error) {
59 epInfo := new(hns.EndpointInfo)
60 epInfo.NetworkName = n.Name
61 epInfo.EndpointName = hns.ConstructEndpointName(args.ContainerID, args.Netns, epInfo.NetworkName)
62
63 // it's not necessary to have have an IPAM in windows as HNS can provide IP/GW
64 if n.IPAM.Type != "" {
65 r, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData)
66 if err != nil {
67 return nil, errors.Annotatef(err, "error while executing IPAM addition")
68 }
69
70 // convert whatever the IPAM result was into the current result
71 result, err := current.NewResultFromResult(r)
72 if err != nil {
73 return nil, errors.Annotatef(err, "error while converting the result from IPAM addition")
74 } else {
75 if len(result.IPs) == 0 {
76 return nil, fmt.Errorf("IPAM plugin return is missing IP config")
77 }
78 epInfo.IpAddress = result.IPs[0].Address.IP
79 epInfo.Gateway = result.IPs[0].Address.IP.Mask(result.IPs[0].Address.Mask)
80
81 // Calculate gateway for bridge network (needs to be x.2)
82 epInfo.Gateway[len(epInfo.Gateway)-1] += 2
83 }
84 }
85
86 // configure sNAT exception
87 n.ApplyOutboundNatPolicy(n.IPMasqNetwork)
88
89 // add port mapping if any present
90 n.ApplyPortMappingPolicy(n.RuntimeConfig.PortMaps)
91
92 epInfo.DNS = n.GetDNS()
93
94 return epInfo, nil
95 }
96
97 func cmdHnsAdd(args *skel.CmdArgs, n *NetConf) (*current.Result, error) {
98 networkName := n.Name
99 hnsNetwork, err := hcsshim.GetHNSNetworkByName(networkName)
100 if err != nil {
101 return nil, errors.Annotatef(err, "error while getting network %v", networkName)
102 }
103 if hnsNetwork == nil {
104 return nil, fmt.Errorf("network %v is not found", networkName)
105 }
106 if !strings.EqualFold(hnsNetwork.Type, "L2Bridge") && !strings.EqualFold(hnsNetwork.Type, "L2Tunnel") {
107 return nil, fmt.Errorf("network %v is of unexpected type: %v", networkName, hnsNetwork.Type)
108 }
109
110 epName := hns.ConstructEndpointName(args.ContainerID, args.Netns, n.Name)
111 hnsEndpoint, err := hns.AddHnsEndpoint(epName, hnsNetwork.Id, args.ContainerID, args.Netns, func() (*hcsshim.HNSEndpoint, error) {
112 epInfo, err := processEndpointArgs(args, n)
113 if err != nil {
114 return nil, errors.Annotate(err, "error while processing endpoint args")
115 }
116 epInfo.NetworkId = hnsNetwork.Id
117
118 hnsEndpoint, err := hns.GenerateHnsEndpoint(epInfo, &n.NetConf)
119 if err != nil {
120 return nil, errors.Annotate(err, "error while generating HNSEndpoint")
121 }
122 return hnsEndpoint, nil
123 })
124 if err != nil {
125 return nil, errors.Annotate(err, "error while adding HNSEndpoint")
126 }
127
128 result, err := hns.ConstructHnsResult(hnsNetwork, hnsEndpoint)
129 if err != nil {
130 return nil, errors.Annotate(err, "error while constructing HNSEndpoint addition result")
131 }
132 return result, nil
133 }
134
135 func cmdHcnAdd(args *skel.CmdArgs, n *NetConf) (*current.Result, error) {
136 networkName := n.Name
137 hcnNetwork, err := hcn.GetNetworkByName(networkName)
138 if err != nil {
139 return nil, errors.Annotatef(err, "error while getting network %v", networkName)
140 }
141 if hcnNetwork == nil {
142 return nil, fmt.Errorf("network %v is not found", networkName)
143 }
144 if hcnNetwork.Type != hcn.L2Bridge && hcnNetwork.Type != hcn.L2Tunnel {
145 return nil, fmt.Errorf("network %v is of unexpected type: %v", networkName, hcnNetwork.Type)
146 }
147
148 epName := hns.ConstructEndpointName(args.ContainerID, args.Netns, n.Name)
149 hcnEndpoint, err := hns.AddHcnEndpoint(epName, hcnNetwork.Id, args.Netns, func() (*hcn.HostComputeEndpoint, error) {
150 epInfo, err := processEndpointArgs(args, n)
151 if err != nil {
152 return nil, errors.Annotate(err, "error while processing endpoint args")
153 }
154 epInfo.NetworkId = hcnNetwork.Id
155
156 hcnEndpoint, err := hns.GenerateHcnEndpoint(epInfo, &n.NetConf)
157 if err != nil {
158 return nil, errors.Annotate(err, "error while generating HostComputeEndpoint")
159 }
160 return hcnEndpoint, nil
161 })
162 if err != nil {
163 return nil, errors.Annotate(err, "error while adding HostComputeEndpoint")
164 }
165
166 result, err := hns.ConstructHcnResult(hcnNetwork, hcnEndpoint)
167 if err != nil {
168 return nil, errors.Annotate(err, "error while constructing HostComputeEndpoint addition result")
169 }
170 return result, nil
171 }
172
173 func cmdAdd(args *skel.CmdArgs) error {
174 n, cniVersion, err := loadNetConf(args.StdinData)
175 if err != nil {
176 return err
177 }
178
179 var result *current.Result
180 if n.ApiVersion == 2 {
181 result, err = cmdHcnAdd(args, n)
182 } else {
183 result, err = cmdHnsAdd(args, n)
184 }
185 if err != nil {
186 ipam.ExecDel(n.IPAM.Type, args.StdinData)
187 return err
188 }
189
190 return types.PrintResult(result, cniVersion)
191 }
192
193 func cmdDel(args *skel.CmdArgs) error {
194 n, _, err := loadNetConf(args.StdinData)
195 if err != nil {
196 return err
197 }
198
199 if n.IPAM.Type != "" {
200 if err := ipam.ExecDel(n.IPAM.Type, args.StdinData); err != nil {
201 return err
202 }
203 }
204 epName := hns.ConstructEndpointName(args.ContainerID, args.Netns, n.Name)
205
206 if n.ApiVersion == 2 {
207 return hns.RemoveHcnEndpoint(epName)
208 }
209 return hns.RemoveHnsEndpoint(epName, args.Netns, args.ContainerID)
210 }
211
212 func cmdCheck(_ *skel.CmdArgs) error {
213 // TODO: implement
214 return nil
215 }
216
217 func main() {
218 skel.PluginMainFuncs(skel.CNIFuncs{
219 Add: cmdAdd,
220 Check: cmdCheck,
221 Del: cmdDel,
222 Status: cmdStatus,
223 /* FIXME GC */
224 }, version.All, bv.BuildString("win-bridge"))
225 }
226
227 func cmdStatus(args *skel.CmdArgs) error {
228 conf := NetConf{}
229 if err := json.Unmarshal(args.StdinData, &conf); err != nil {
230 return fmt.Errorf("failed to load netconf: %w", err)
231 }
232
233 if conf.IPAM.Type != "" {
234 if err := ipam.ExecStatus(conf.IPAM.Type, args.StdinData); err != nil {
235 return err
236 }
237 }
238
239 return nil
240 }