-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
197 lines (160 loc) · 4.29 KB
/
main.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"math/rand"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/Sirupsen/logrus"
"github.com/vishvananda/netlink"
)
func init() {
rand.Seed(time.Now().Unix())
}
const (
defaultVNI = 1
iptablesResyncSeconds = 5
encapOverhead = 50
vxlanNetwork = "10.5.0.0/16"
subNetworkTpl = "10.5.%v.0"
)
type config struct {
etcdEndpoint string
}
func main() {
cfg := config{}
flag.StringVar(&cfg.etcdEndpoint, "etcdEndpoint", "http://127.0.0.1:2379", "etcd endpoint")
flag.Parse()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
extIface, err := lookupExtIface()
if err != nil {
panic(fmt.Sprintf("lookupExtIface err: %v", err))
}
devAttrs := vxlanDeviceAttrs{
vni: defaultVNI,
name: fmt.Sprintf("vxlan.%v", defaultVNI),
vtepIndex: extIface.Iface.Index,
vtepAddr: extIface.IfaceAddr,
vtepPort: 0,
gbp: false,
}
dev, err := newVxlanDevice(&devAttrs)
if err != nil {
panic(fmt.Sprintf("newVXLANDevice err: %v", err))
}
dev.directRouting = false
publicIP := FromIP(extIface.ExtAddr)
snIP := FromIP(net.ParseIP(fmt.Sprintf(subNetworkTpl, 1+rand.Intn(254))))
sn := IP4Net{
IP: snIP,
PrefixLen: 24,
}
attrs := Attrs{
PublicIP: publicIP,
Subnet: sn,
HardwareAddr: dev.link.HardwareAddr,
}
ctx := context.Background()
sm := newManager(cfg)
if err := sm.createSubnet(ctx, sn, attrs); err != nil {
panic(fmt.Errorf("create subnet fail: %v", err))
}
logrus.Infof("create subnet: %v, net mask: %v", sn.IP.ToIP(), sn.PrefixLen)
go handleSubnets(ctx, sn, &sm, dev)
if err := dev.configure(fmt.Sprintf("%v/32", snIP.ToIP())); err != nil {
panic(fmt.Errorf("failed to configure interface %s: %s", dev.link.Attrs().Name, err))
}
go setupAndEnsureIPTables(forwardRules(vxlanNetwork), iptablesResyncSeconds)
logrus.Infof("MTU: %v", extIface.Iface.MTU-encapOverhead)
logrus.Infof("VXLan HardwareAddr: %v", dev.link.HardwareAddr)
logrus.Info("Running backend.")
<-sigs
logrus.Info("shutdownHandler sent cancel signal...")
}
type externalInterface struct {
Iface *net.Interface
IfaceAddr net.IP
ExtAddr net.IP
}
func lookupExtIface() (*externalInterface, error) {
var iface *net.Interface
var ifaceAddr net.IP
var err error
logrus.Info("Determining IP address of default interface")
if iface, err = getDefaultGatewayIface(); err != nil {
return nil, fmt.Errorf("failed to get default interface: %s", err)
}
if ifaceAddr == nil {
ifaceAddr, err = getIfaceIP4Addr(iface)
if err != nil {
return nil, fmt.Errorf("failed to find IPv4 address for interface %s", iface.Name)
}
}
logrus.Infof("Using interface with name %s and address %s", iface.Name, ifaceAddr)
if iface.MTU == 0 {
return nil, fmt.Errorf("failed to determine MTU for %s interface", ifaceAddr)
}
var extAddr net.IP
if extAddr == nil {
logrus.Infof("Defaulting external address to interface address (%s)", ifaceAddr)
extAddr = ifaceAddr
}
return &externalInterface{
Iface: iface,
IfaceAddr: ifaceAddr,
ExtAddr: extAddr,
}, nil
}
func getDefaultGatewayIface() (*net.Interface, error) {
routes, err := netlink.RouteList(nil, syscall.AF_INET)
if err != nil {
return nil, err
}
for _, route := range routes {
if route.Dst == nil || route.Dst.String() == "0.0.0.0/0" {
if route.LinkIndex <= 0 {
return nil, errors.New("Found default route but could not determine interface")
}
return net.InterfaceByIndex(route.LinkIndex)
}
}
return nil, errors.New("Unable to find default route")
}
func getIfaceAddrs(iface *net.Interface) ([]netlink.Addr, error) {
link := &netlink.Device{
netlink.LinkAttrs{
Index: iface.Index,
},
}
return netlink.AddrList(link, syscall.AF_INET)
}
func getIfaceIP4Addr(iface *net.Interface) (net.IP, error) {
addrs, err := getIfaceAddrs(iface)
if err != nil {
return nil, err
}
// prefer non link-local addr
var ll net.IP
for _, addr := range addrs {
if addr.IP.To4() == nil {
continue
}
if addr.IP.IsGlobalUnicast() {
return addr.IP, nil
}
if addr.IP.IsLinkLocalUnicast() {
ll = addr.IP
}
}
if ll != nil {
// didn't find global but found link-local. it'll do.
return ll, nil
}
return nil, errors.New("No IPv4 address found for given interface")
}