-
Notifications
You must be signed in to change notification settings - Fork 4
/
net.go
205 lines (195 loc) · 5.37 KB
/
net.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
// Copyright (c) 2017 Che Wei, Lin
//
// 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 tinynet
import (
"fmt"
log "github.com/sirupsen/logrus"
)
// HostConfig is a host network configuration
type HostConfig struct {
name string
address string
docker bool
ifaceName string
imageRef string
mtu int
}
// NewHostConfig is a constructor to initial host network configuration.
func NewHostConfig(name, addr, ifaceName string, mtu int, docker bool, imageRef string) HostConfig {
hostConfig := HostConfig{}
if name == "" {
panic("Host name is required")
}
if addr == "" {
panic("Host address is required")
}
hostConfig.name = name
hostConfig.address = addr
hostConfig.docker = docker
hostConfig.ifaceName = ifaceName
if ifaceName == "" {
hostConfig.ifaceName = "eth1"
}
hostConfig.imageRef = imageRef
if imageRef == "" {
hostConfig.imageRef = "library/busybox"
}
hostConfig.mtu = mtu
if mtu == 0 {
hostConfig.mtu = 1500
}
return hostConfig
}
// AddHostWithConf will add a host to topology.
func AddHostWithConf(hc HostConfig) (*Host, error) {
var h *Host
var err error
if hc.docker {
// Create a docker container
h, err = NewContainer(hc.name, hc.imageRef)
if err != nil {
log.Fatal("failed to NewContainer: ", err)
}
} else {
// Create a network namespace
h, err = NewHost(hc.name)
if err != nil {
log.Fatal("failed to NewHost: ", err)
}
}
// setup a veth pair
_, err = h.setupVeth(hc.ifaceName, hc.mtu)
if err != nil {
log.Fatal("failed to setup veth pair: ", err)
}
// setup a IP for host
h.setIfaceIP(hc.address)
if err != nil {
log.Fatalf("failed to setIfaceIP for %s: %v", h.Name, err)
return nil, err
}
return h, nil
}
// AddHost will add a host to topology. set docker to true can enable docker container as a host
func AddHost(name string, addr string, docker bool) (*Host, error) {
log.Warnf("AddHost will be deprecated: use AddHostWithConf instead")
var h *Host
var err error
if docker {
// Create a docker container
h, err = NewContainer(name, "library/busybox")
if err != nil {
log.Fatal("failed to NewContainer: ", err)
}
} else {
// Create a network namespace
h, err = NewHost(name)
if err != nil {
log.Fatal("failed to NewHost: ", err)
}
}
// setup a veth pair
_, err = h.setupVeth("eth2", 1500)
if err != nil {
log.Fatal("failed to setup veth pair: ", err)
}
// setup a IP for host
h.setIfaceIP(addr)
if err != nil {
log.Fatalf("failed to setIfaceIP for %s: %v", h.Name, err)
return nil, err
}
return h, nil
}
// AddSwitch will add a switch to topology.
func AddSwitch(params ...string) (*OVSSwitch, error) {
// params[0] for brName
// params[1] for controller remote IP and port
// Create a Open vSwitch bridge
sw, err := NewOVSSwitch(params[0])
if err != nil {
log.Fatal("failed to NewOVSSwitch: ", err)
}
if len(params) == 2 {
if err := sw.SetCtrl(params[1]); err != nil {
log.Warnf("failed to SetCtrl for %s: %v", sw.BridgeName, err)
return nil, err
}
} else if len(params) > 2 {
return nil, fmt.Errorf("Too many arguments")
}
return sw, nil
}
// AddLink will add a link between switch to switch or host to switch.
func AddLink(n1, n2 interface{}) error {
// log.Info(n1.(*OVSSwitch).nodeType)
// log.Info(n2.(*Host).nodeType)
var err error
switch n1.(type) {
case *OVSSwitch:
switch n2.(type) {
case *OVSSwitch:
tap0, tap1, err := makeVethPair("tap0", "tap1")
if err != nil {
log.Fatalf("failed to makeVethPair: %v", err)
}
err = n1.(*OVSSwitch).addPort(tap0.Name)
if err != nil {
log.Fatalf("failed to addPortswitch - switch: %v", err)
}
err = n2.(*OVSSwitch).addPort(tap1.Name)
if err != nil {
log.Fatalf("failed to addPort switch - switch: %v", err)
}
log.Infof("Adding a link: (%s, %s)", n1.(*OVSSwitch).BridgeName, n2.(*OVSSwitch).BridgeName)
case *Host:
err = n1.(*OVSSwitch).addPort(n2.(*Host).VethName)
if err != nil {
log.Fatalf("failed to addPort switch - host: %v", err)
}
log.Infof("Adding a link: (%s, %s)", n1.(*OVSSwitch).BridgeName, n2.(*Host).Name)
default:
log.Fatalf("Type Error")
}
case *Host:
switch n2.(type) {
case *OVSSwitch:
err = n2.(*OVSSwitch).addPort(n1.(*Host).VethName)
if err != nil {
log.Fatalf("failed to addPort host - switch : %v", err)
}
log.Infof("Adding a link: (%s, %s)", n1.(*Host).Name, n2.(*OVSSwitch).BridgeName)
case *Host:
log.Fatalf("Type Error: host can not connect to host")
default:
log.Fatalf("Type Error")
}
default:
log.Fatalf("Type Error")
}
return nil
}
// GetIPs will get IP address from a CIDR notation
// Return an array with IP address. negative num for getting all IP addresses
// (network address and broadcast address are not included)
func GetIPs(cidr string, num int) ([]string, error) {
ips, err := getAllIPsfromCIDR(cidr)
if err != nil {
return nil, err
}
if num < 0 {
return ips, nil
}
return ips[0:num], nil
}