forked from projectcalico/felix
/
test-connection.go
254 lines (226 loc) · 7.22 KB
/
test-connection.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
// Copyright (c) 2017-2019 Tigera, 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 main
import (
"bufio"
"errors"
"fmt"
"net"
"os"
"strings"
"time"
"github.com/containernetworking/cni/pkg/ns"
docopt "github.com/docopt/docopt-go"
reuse "github.com/libp2p/go-reuseport"
log "github.com/sirupsen/logrus"
uuid "github.com/satori/go.uuid"
"github.com/projectcalico/felix/fv/utils"
)
const usage = `test-connection: test connection to some target, for Felix FV testing.
Usage:
test-connection <namespace-path> <ip-address> <port> [--source-port=<source>] [--protocol=<protocol>] [--loop-with-file=<file>]
Options:
--source-port=<source> Source port to use for the connection [default: 0].
--protocol=<protocol> Protocol to test [default: tcp].
--loop-with-file=<file> Whether to send messages repeatedly, file is used for synchronization
If connection is successful, test-connection exits successfully.
If connection is unsuccessful, test-connection panics and so exits with a failure status.`
// Note about the --loop-with-file=<FILE> flag:
//
// This flag takes a path to a file as a value. The file existence is
// used as a means of synchronization.
//
// Before this program is started, the file should exist. When the
// program establishes a long-running connection and sends the first
// message, it will remove the file. That way other process can assume
// that the connection is here when the file disappears and can
// perform some checks.
//
// If the other process creates the file again, it will tell this
// program to close the connection, remove the file and quit.
func main() {
log.SetLevel(log.DebugLevel)
arguments, err := docopt.Parse(usage, nil, true, "v0.1", false)
if err != nil {
println(usage)
log.WithError(err).Fatal("Failed to parse usage")
}
log.WithField("args", arguments).Info("Parsed arguments")
namespacePath := arguments["<namespace-path>"].(string)
ipAddress := arguments["<ip-address>"].(string)
port := arguments["<port>"].(string)
sourcePort := arguments["--source-port"].(string)
log.Infof("Test connection from %v:%v to IP %v port %v", namespacePath, sourcePort, ipAddress, port)
protocol := arguments["--protocol"].(string)
loopFile := ""
if arg, ok := arguments["--loop-with-file"]; ok && arg != nil {
loopFile = arg.(string)
}
if loopFile == "" {
// I found that configuring the timeouts on all the
// network calls was a bit fiddly. Since it leaves
// the process hung if one of them is missed, use a
// global timeout instead.
go func() {
time.Sleep(2 * time.Second)
panic("Timed out")
}()
}
if namespacePath == "-" {
// Test connection from wherever we are already running.
err = tryConnect(ipAddress, port, sourcePort, protocol, loopFile)
} else {
// Get the specified network namespace (representing a workload).
var namespace ns.NetNS
namespace, err = ns.GetNS(namespacePath)
if err != nil {
panic(err)
}
log.WithField("namespace", namespace).Debug("Got namespace")
// Now, in that namespace, try connecting to the target.
err = namespace.Do(func(_ ns.NetNS) error {
return tryConnect(ipAddress, port, sourcePort, protocol, loopFile)
})
}
if err != nil {
panic(err)
}
}
func tryConnect(ipAddress, port, sourcePort, protocol, loopFile string) error {
err := utils.RunCommand("ip", "r")
if err != nil {
return err
}
uid := uuid.NewV4().String()
testMessage := "hello," + uid
// The reuse library implements a version of net.Dialer that can reuse UDP/TCP ports, which we
// need in order to make connection retries work.
var d reuse.Dialer
var localAddr string
var remoteAddr string
if strings.Contains(ipAddress, ":") {
localAddr = "[::]:" + sourcePort
remoteAddr = "[" + ipAddress + "]:" + port
} else {
localAddr = "0.0.0.0:" + sourcePort
remoteAddr = ipAddress + ":" + port
}
ls := newLoopState(loopFile)
log.Infof("Connecting from %v to %v over %s", localAddr, remoteAddr, protocol)
if protocol == "udp" {
d.D.LocalAddr, _ = net.ResolveUDPAddr("udp", localAddr)
log.WithFields(log.Fields{
"addr": localAddr,
"resolved": d.D.LocalAddr,
}).Infof("Resolved udp addr")
conn, err := d.Dial("udp", remoteAddr)
log.Infof(`UDP "connection" established`)
if err != nil {
panic(err)
}
defer conn.Close()
for {
fmt.Fprintf(conn, testMessage+"\n")
log.WithField("message", testMessage).Info("Sent message over udp")
reply, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
panic(err)
}
reply = strings.TrimSpace(reply)
log.WithField("reply", reply).Info("Got reply")
if reply != testMessage {
panic(errors.New("Unexpected reply: " + reply))
}
if !ls.Next() {
break
}
}
} else {
d.D.LocalAddr, err = net.ResolveTCPAddr("tcp", localAddr)
if err != nil {
return err
}
log.WithFields(log.Fields{
"addr": localAddr,
"resolved": d.D.LocalAddr,
}).Infof("Resolved tcp addr")
conn, err := d.Dial("tcp", remoteAddr)
if err != nil {
return err
}
defer conn.Close()
log.Infof("TCP connection established")
for {
fmt.Fprintf(conn, testMessage+"\n")
log.WithField("message", testMessage).Info("Sent message over tcp")
reply, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
return err
}
reply = strings.TrimSpace(reply)
log.WithField("reply", reply).Info("Got reply")
if reply != testMessage {
return errors.New("Unexpected reply: " + reply)
}
if !ls.Next() {
break
}
}
}
return nil
}
type loopState struct {
sentInitial bool
loopFile string
}
func newLoopState(loopFile string) *loopState {
return &loopState{
sentInitial: false,
loopFile: loopFile,
}
}
func (l *loopState) Next() bool {
if l.loopFile == "" {
return false
}
if l.sentInitial {
// This is after the connection was established in
// previous iteration, so we wait for the loop file to
// appear (it should be created by other process). If
// the file exists, it means that the other process
// wants us to delete the file, drop the connection
// and quit.
if _, err := os.Stat(l.loopFile); err != nil {
if !os.IsNotExist(err) {
panic(fmt.Errorf("Failed to stat loop file %s: %v", l.loopFile, err))
}
} else {
if err := os.Remove(l.loopFile); err != nil {
panic(fmt.Errorf("Could not remove loop file %s: %v", l.loopFile, err))
}
return false
}
} else {
// A connection was just established and the initial
// message was sent so we set the flag to true and
// delete the loop file, so other process can continue
// with the appropriate checks
if err := os.Remove(l.loopFile); err != nil {
panic(fmt.Errorf("Could not remove loop file %s: %v", l.loopFile, err))
}
l.sentInitial = true
}
time.Sleep(500 * time.Millisecond)
return true
}