forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-connection.go
157 lines (134 loc) · 4.38 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
// 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"
"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>]
Options:
--source-port=<source> Source port to use for the connection [default: 0].
--protocol=<protocol> Protocol to test [default: tcp].
If connection is successful, test-connection exits successfully.
If connection is unsuccessful, test-connection panics and so exits with a failure status.`
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)
// 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)
} 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)
})
}
if err != nil {
panic(err)
}
}
func tryConnect(ipAddress, port string, sourcePort string, protocol 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
}
if protocol == "udp" {
log.Infof("Connecting from %v to %v", localAddr, remoteAddr)
d.D.LocalAddr, _ = net.ResolveUDPAddr("udp", localAddr)
conn, err := d.Dial("udp", remoteAddr)
if err != nil {
panic(err)
}
defer conn.Close()
fmt.Fprintf(conn, testMessage+"\n")
reply, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
panic(err)
}
reply = strings.TrimSpace(reply)
if reply != testMessage {
panic(errors.New("Unexpected reply: " + reply))
}
} else {
d.D.LocalAddr, err = net.ResolveTCPAddr("tcp", localAddr)
if err != nil {
return err
}
conn, err := d.Dial("tcp", remoteAddr)
if err != nil {
return err
}
defer conn.Close()
fmt.Fprintf(conn, testMessage+"\n")
reply, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
return err
}
reply = strings.TrimSpace(reply)
if reply != testMessage {
return errors.New("Unexpected reply: " + reply)
}
}
return nil
}