/
hns.go
291 lines (255 loc) · 7.31 KB
/
hns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// Copyright (c) 2018 Juniper Networks, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hns
import (
"encoding/json"
"strings"
"time"
"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/local_networking/hns/win_networking"
"github.com/Juniper/contrail-windows-docker-driver/common"
"github.com/Microsoft/hcsshim"
log "github.com/sirupsen/logrus"
)
type recoverableError struct {
inner error
}
func (e *recoverableError) Error() string {
return e.inner.Error()
}
func Init(nameOfAdapterToUse common.AdapterName) error {
// HNS automatically creates a new vswitch if the first HNS network is created. We want to
// control this behaviour. That's why we create a dummy root HNS network.
if err := win_networking.WaitForValidIPReacquisition(nameOfAdapterToUse); err != nil {
return err
}
rootNetwork, err := GetHNSNetworkByName(common.RootNetworkName)
if err != nil {
return err
}
if rootNetwork == nil {
subnets := []hcsshim.Subnet{
{
AddressPrefix: "0.0.0.0/24",
},
}
configuration := &hcsshim.HNSNetwork{
Name: common.RootNetworkName,
Type: "transparent",
NetworkAdapterName: string(nameOfAdapterToUse),
Subnets: subnets,
}
rootNetID, err := CreateHNSNetwork(configuration)
if err != nil {
return err
}
log.Infoln("Created root HNS network:", rootNetID)
} else {
log.Infoln("Existing root HNS network found:", rootNetwork.Id)
}
return nil
}
func tryCreateHNSNetwork(config string) (string, error) {
response, err := hcsshim.HNSNetworkRequest("POST", "", config)
if err != nil {
log.Errorln(err)
errMsg := strings.ToLower(err.Error())
if strings.Contains(errMsg, "hns failed") && strings.Contains(errMsg, "unspecified error") {
return "", &recoverableError{inner: err}
} else {
return "", err
}
}
// When the first HNS network is created, a vswitch is also created and attached to
// specified network adapter. This adapter will temporarily lose network connectivity
// while it reacquires IPv4. We need to wait for it.
// https://github.com/Microsoft/hcsshim/issues/108
if err := win_networking.WaitForValidIPReacquisition(common.HNSTransparentInterfaceName); err != nil {
log.Errorln(err)
deleteErr := DeleteHNSNetwork(response.Id)
if deleteErr != nil {
return "", deleteErr
}
return "", &recoverableError{inner: err}
}
return response.Id, nil
}
func CreateHNSNetwork(configuration *hcsshim.HNSNetwork) (string, error) {
log.Infoln("Creating HNS network")
configBytes, err := json.Marshal(configuration)
if err != nil {
log.Errorln(err)
return "", err
}
log.Debugln("Config:", string(configBytes))
var id = ""
delay := common.CreateHNSNetworkInitialRetryDelay
creatingStart := time.Now()
for {
id, err = tryCreateHNSNetwork(string(configBytes))
if err != nil {
if recoverableErr, ok := err.(*recoverableError); ok {
err = recoverableErr.inner
if time.Since(creatingStart) < common.CreateHNSNetworkTimeout {
log.Infoln("Creating HNS network failed, retrying.")
log.Infoln("Sleeping", delay, "ms")
time.Sleep(delay)
delay *= 2
continue
}
}
return "", err
}
break
}
log.Infoln("Created HNS network with ID:", id)
return id, nil
}
func DeleteHNSNetwork(hnsID string) error {
log.Infoln("Deleting HNS network", hnsID)
toDelete, err := GetHNSNetwork(hnsID)
if err != nil {
log.Errorln(err)
return err
}
networks, err := ListHNSNetworks()
if err != nil {
log.Errorln(err)
return err
}
adapterStillInUse := false
for _, network := range networks {
if network.Id != toDelete.Id &&
network.NetworkAdapterName == toDelete.NetworkAdapterName {
adapterStillInUse = true
break
}
}
_, err = hcsshim.HNSNetworkRequest("DELETE", hnsID, "")
if err != nil {
log.Errorln(err)
return err
}
if !adapterStillInUse {
// If the last network that uses an adapter is deleted, then the underlying vswitch is
// also deleted. During this period, the adapter will temporarily lose network
// connectivity while it reacquires IPv4. We need to wait for it.
// https://github.com/Microsoft/hcsshim/issues/95
if err := win_networking.WaitForValidIPReacquisition(
common.AdapterName(toDelete.NetworkAdapterName)); err != nil {
log.Errorln(err)
return err
}
}
return nil
}
func ListHNSNetworks() ([]hcsshim.HNSNetwork, error) {
log.Infoln("Listing HNS networks")
nets, err := hcsshim.HNSListNetworkRequest("GET", "", "")
if err != nil {
log.Errorln(err)
return nil, err
}
return nets, nil
}
func GetHNSNetwork(hnsID string) (*hcsshim.HNSNetwork, error) {
log.Infoln("Getting HNS network", hnsID)
net, err := hcsshim.HNSNetworkRequest("GET", hnsID, "")
if err != nil {
log.Errorln(err)
return nil, err
}
return net, nil
}
func GetHNSNetworkByName(name string) (*hcsshim.HNSNetwork, error) {
log.Infoln("Getting HNS network by name:", name)
nets, err := hcsshim.HNSListNetworkRequest("GET", "", "")
if err != nil {
log.Errorln(err)
return nil, err
}
for _, n := range nets {
if n.Name == name {
return &n, nil
}
}
return nil, nil
}
func CreateHNSEndpoint(configuration *hcsshim.HNSEndpoint) (string, error) {
log.Infoln("Creating HNS endpoint")
configBytes, err := json.Marshal(configuration)
if err != nil {
log.Errorln(err)
return "", err
}
log.Debugln("Config: ", string(configBytes))
response, err := hcsshim.HNSEndpointRequest("POST", "", string(configBytes))
if err != nil {
return "", err
}
log.Infoln("Created HNS endpoint with ID:", response.Id)
return response.Id, nil
}
func DeleteHNSEndpoint(endpointID string) error {
log.Infoln("Deleting HNS endpoint", endpointID)
_, err := hcsshim.HNSEndpointRequest("DELETE", endpointID, "")
if err != nil {
log.Errorln(err)
return err
}
return nil
}
func GetHNSEndpoint(endpointID string) (*hcsshim.HNSEndpoint, error) {
log.Infoln("Getting HNS endpoint", endpointID)
endpoint, err := hcsshim.HNSEndpointRequest("GET", endpointID, "")
if err != nil {
log.Errorln(err)
return nil, err
}
return endpoint, nil
}
func GetHNSEndpointByName(name string) (*hcsshim.HNSEndpoint, error) {
log.Infoln("Getting HNS endpoint by name:", name)
eps, err := hcsshim.HNSListEndpointRequest()
if err != nil {
log.Errorln(err)
return nil, err
}
for _, ep := range eps {
if ep.Name == name {
return &ep, nil
}
}
return nil, nil
}
func ListHNSEndpoints() ([]hcsshim.HNSEndpoint, error) {
endpoints, err := hcsshim.HNSListEndpointRequest()
if err != nil {
return nil, err
}
return endpoints, nil
}
func ListHNSEndpointsOfNetwork(netID string) ([]hcsshim.HNSEndpoint, error) {
eps, err := ListHNSEndpoints()
if err != nil {
return nil, err
}
var epsInNetwork []hcsshim.HNSEndpoint
for _, ep := range eps {
if ep.VirtualNetwork == netID {
epsInNetwork = append(epsInNetwork, ep)
}
}
return epsInNetwork, nil
}