-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
migration-proxy.go
336 lines (279 loc) · 8.25 KB
/
migration-proxy.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2018 Red Hat, Inc.
*
*/
package migrationproxy
import (
"fmt"
"io"
"net"
"os"
"path/filepath"
"sync"
"kubevirt.io/kubevirt/pkg/log"
)
type ProxyManager interface {
StartTargetListener(key string, targetUnixFile string) error
GetTargetListenerPort(key string) int
StopTargetListener(key string)
StartSourceListener(key string, targetAddress string) error
GetSourceListenerFile(key string) string
StopSourceListener(key string)
}
type migrationProxyManager struct {
virtShareDir string
sourceProxies map[string]*migrationProxy
targetProxies map[string]*migrationProxy
managerLock sync.Mutex
}
type migrationProxy struct {
unixSocketPath string
tcpBindAddress string
tcpBindPort int
targetAddress string
targetProtocol string
stopChan chan struct{}
listenErrChan chan error
fdChan chan net.Conn
listener net.Listener
}
func NewMigrationProxyManager(virtShareDir string) ProxyManager {
return &migrationProxyManager{
virtShareDir: virtShareDir,
sourceProxies: make(map[string]*migrationProxy),
targetProxies: make(map[string]*migrationProxy),
}
}
func SourceUnixFile(virtShareDir string, key string) string {
return filepath.Join(virtShareDir, "migrationproxy", key+"-source.sock")
}
func (m *migrationProxyManager) StartTargetListener(key string, targetUnixFile string) error {
m.managerLock.Lock()
defer m.managerLock.Unlock()
curProxy, exists := m.targetProxies[key]
if exists {
if curProxy.targetAddress == targetUnixFile {
// No Op, already exists
return nil
} else {
// stop the current proxy and point it somewhere new.
curProxy.StopListening()
}
}
// 0 means random port is used
proxy := NewTargetProxy("0.0.0.0", 0, targetUnixFile)
err := proxy.StartListening()
if err != nil {
proxy.StopListening()
return err
}
log.Log.Infof("Proxy Target listening on port %d for key %s", proxy.tcpBindPort, key)
m.targetProxies[key] = proxy
return nil
}
func (m *migrationProxyManager) GetSourceListenerFile(key string) string {
m.managerLock.Lock()
defer m.managerLock.Unlock()
curProxy, exists := m.sourceProxies[key]
if exists {
return curProxy.unixSocketPath
}
return ""
}
func (m *migrationProxyManager) GetTargetListenerPort(key string) int {
m.managerLock.Lock()
defer m.managerLock.Unlock()
curProxy, exists := m.targetProxies[key]
if exists {
return curProxy.tcpBindPort
}
return 0
}
func (m *migrationProxyManager) StopTargetListener(key string) {
m.managerLock.Lock()
defer m.managerLock.Unlock()
curProxy, exists := m.targetProxies[key]
if exists {
curProxy.StopListening()
delete(m.targetProxies, key)
log.Log.Infof("Stopping proxy target %s listening on %d", key, curProxy.tcpBindPort)
}
}
func (m *migrationProxyManager) StartSourceListener(key string, targetAddress string) error {
m.managerLock.Lock()
defer m.managerLock.Unlock()
curProxy, exists := m.sourceProxies[key]
if exists {
if curProxy.targetAddress == targetAddress {
// No Op, already exists
return nil
} else {
// stop the current proxy and point it somewhere new.
curProxy.StopListening()
}
}
filePath := SourceUnixFile(m.virtShareDir, key)
os.RemoveAll(filePath)
proxy := NewSourceProxy(filePath, targetAddress)
err := proxy.StartListening()
if err != nil {
proxy.StopListening()
return err
}
log.Log.Infof("Proxy Source listening on unix file %s for key %s", filePath, key)
m.sourceProxies[key] = proxy
return nil
}
func (m *migrationProxyManager) StopSourceListener(key string) {
m.managerLock.Lock()
defer m.managerLock.Unlock()
curProxy, exists := m.sourceProxies[key]
if exists {
curProxy.StopListening()
delete(m.sourceProxies, key)
}
filePath := SourceUnixFile(m.virtShareDir, key)
os.RemoveAll(filePath)
}
// SRC POD ENV(migration unix socket) <-> HOST ENV (tcp client) <-----> HOST ENV (tcp server) <-> TARGET POD ENV (libvirtd unix socket)
// Source proxy exposes a unix socket server and pipes to an outbound TCP connection.
func NewSourceProxy(unixSocketPath string, tcpTargetAddress string) *migrationProxy {
return &migrationProxy{
unixSocketPath: unixSocketPath,
targetAddress: tcpTargetAddress,
targetProtocol: "tcp",
stopChan: make(chan struct{}),
fdChan: make(chan net.Conn, 1),
listenErrChan: make(chan error, 1),
}
}
// Target proxy listens on a tcp socket and pipes to a libvirtd unix socket
func NewTargetProxy(tcpBindAddress string, tcpBindPort int, libvirtdSocketPath string) *migrationProxy {
return &migrationProxy{
tcpBindAddress: tcpBindAddress,
tcpBindPort: tcpBindPort,
targetAddress: libvirtdSocketPath,
targetProtocol: "unix",
stopChan: make(chan struct{}),
fdChan: make(chan net.Conn, 1),
listenErrChan: make(chan error, 1),
}
}
func (m *migrationProxy) createTcpListener() error {
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", m.tcpBindAddress, m.tcpBindPort))
if err != nil {
log.Log.Reason(err).Error("failed to create unix socket for proxy service")
return err
}
if m.tcpBindPort == 0 {
// update the random port that was selected
m.tcpBindPort = listener.Addr().(*net.TCPAddr).Port
}
m.listener = listener
return nil
}
func (m *migrationProxy) createUnixListener() error {
os.RemoveAll(m.unixSocketPath)
err := os.MkdirAll(filepath.Dir(m.unixSocketPath), 0755)
if err != nil {
log.Log.Reason(err).Error("unable to create directory for unix socket")
return err
}
listener, err := net.Listen("unix", m.unixSocketPath)
if err != nil {
log.Log.Reason(err).Error("failed to create unix socket for proxy service")
return err
}
m.listener = listener
return nil
}
func (m *migrationProxy) StopListening() {
close(m.stopChan)
m.listener.Close()
}
func handleConnection(fd net.Conn, targetAddress string, targetProtocol string, stopChan chan struct{}) {
defer fd.Close()
outBoundErr := make(chan error)
inBoundErr := make(chan error)
conn, err := net.Dial(targetProtocol, targetAddress)
if err != nil {
log.Log.Reason(err).Errorf("unable to create outbound leg of proxy to host %s", targetAddress)
return
}
go func() {
//from outbound connection to proxy
n, err := io.Copy(fd, conn)
log.Log.Infof("%d bytes read from oubound connection", n)
inBoundErr <- err
}()
go func() {
//from proxy to outbound connection
n, err := io.Copy(conn, fd)
log.Log.Infof("%d bytes written oubound connection", n)
outBoundErr <- err
}()
select {
case err = <-outBoundErr:
if err != nil {
log.Log.Reason(err).Errorf("error encountered copying data to outbound proxy connection %s", targetAddress)
}
case err = <-inBoundErr:
if err != nil {
log.Log.Reason(err).Errorf("error encountered reading data to proxy connection %s", targetAddress)
}
case <-stopChan:
log.Log.Infof("stop channel terminated proxy")
}
}
func (m *migrationProxy) StartListening() error {
if m.unixSocketPath != "" {
err := m.createUnixListener()
if err != nil {
return err
}
} else {
err := m.createTcpListener()
if err != nil {
return err
}
}
go func(ln net.Listener, fdChan chan net.Conn, listenErr chan error) {
for {
fd, err := ln.Accept()
if err != nil {
listenErr <- err
log.Log.Reason(err).Error("proxy unix socket listener returned error.")
break
} else {
fdChan <- fd
}
}
}(m.listener, m.fdChan, m.listenErrChan)
go func(targetAddress string, targetProtocol string, fdChan chan net.Conn, stopChan chan struct{}, listenErrChan chan error) {
for {
select {
case fd := <-fdChan:
go handleConnection(fd, targetAddress, targetProtocol, stopChan)
case <-stopChan:
return
case <-listenErrChan:
return
}
}
}(m.targetAddress, m.targetProtocol, m.fdChan, m.stopChan, m.listenErrChan)
return nil
}