forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovmanager.go
216 lines (177 loc) · 5.61 KB
/
ovmanager.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
package ovmanager
import (
"context"
"fmt"
"net"
"strconv"
"sync"
"github.com/containerd/log"
"github.com/docker/docker/libnetwork/bitmap"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/drivers/overlay/overlayutils"
"github.com/docker/docker/libnetwork/netlabel"
"github.com/docker/docker/libnetwork/scope"
"github.com/docker/docker/libnetwork/types"
)
const (
networkType = "overlay"
// The lowest VNI value to auto-assign. Windows does not support VXLAN IDs
// which overlap the range of 802.1Q VLAN IDs [0, 4095].
vxlanIDStart = 4096
// The largest VNI value permitted by RFC 7348.
vxlanIDEnd = (1 << 24) - 1
)
type networkTable map[string]*network
type driver struct {
mu sync.Mutex
networks networkTable
vxlanIdm *bitmap.Bitmap
}
type subnet struct {
subnetIP *net.IPNet
gwIP *net.IPNet
vni uint32
}
type network struct {
id string
driver *driver
subnets []*subnet
}
// Register registers a new instance of the overlay driver.
func Register(r driverapi.Registerer) error {
return r.RegisterDriver(networkType, newDriver(), driverapi.Capability{
DataScope: scope.Global,
ConnectivityScope: scope.Global,
})
}
func newDriver() *driver {
return &driver{
networks: networkTable{},
vxlanIdm: bitmap.New(vxlanIDEnd + 1), // The full range of valid vxlan IDs: [0, 2^24).
}
}
func (d *driver) NetworkAllocate(id string, option map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
if id == "" {
return nil, fmt.Errorf("invalid network id for overlay network")
}
if ipV4Data == nil {
return nil, fmt.Errorf("empty ipv4 data passed during overlay network creation")
}
n := &network{
id: id,
driver: d,
subnets: []*subnet{},
}
opts := make(map[string]string)
vxlanIDList := make([]uint32, 0, len(ipV4Data))
for key, val := range option {
if key == netlabel.OverlayVxlanIDList {
log.G(context.TODO()).Debugf("overlay network option: %s", val)
var err error
vxlanIDList, err = overlayutils.AppendVNIList(vxlanIDList, val)
if err != nil {
return nil, err
}
} else {
opts[key] = val
}
}
d.mu.Lock()
defer d.mu.Unlock()
for i, ipd := range ipV4Data {
s := &subnet{
subnetIP: ipd.Pool,
gwIP: ipd.Gateway,
}
if len(vxlanIDList) > i { // The VNI for this subnet was specified in the network options.
s.vni = vxlanIDList[i]
err := d.vxlanIdm.Set(uint64(s.vni)) // Mark VNI as in-use.
if err != nil {
// The VNI is already in use by another subnet/network.
n.releaseVxlanID()
return nil, fmt.Errorf("could not assign vxlan id %v to pool %s: %v", s.vni, s.subnetIP, err)
}
} else {
// Allocate an available VNI for the subnet, outside the range of 802.1Q VLAN IDs.
vni, err := d.vxlanIdm.SetAnyInRange(vxlanIDStart, vxlanIDEnd, true)
if err != nil {
n.releaseVxlanID()
return nil, fmt.Errorf("could not obtain vxlan id for pool %s: %v", s.subnetIP, err)
}
s.vni = uint32(vni)
}
n.subnets = append(n.subnets, s)
}
val := strconv.FormatUint(uint64(n.subnets[0].vni), 10)
for _, s := range n.subnets[1:] {
val = val + "," + strconv.FormatUint(uint64(s.vni), 10)
}
opts[netlabel.OverlayVxlanIDList] = val
if _, ok := d.networks[id]; ok {
n.releaseVxlanID()
return nil, fmt.Errorf("network %s already exists", id)
}
d.networks[id] = n
return opts, nil
}
func (d *driver) NetworkFree(id string) error {
if id == "" {
return fmt.Errorf("invalid network id passed while freeing overlay network")
}
d.mu.Lock()
defer d.mu.Unlock()
n, ok := d.networks[id]
if !ok {
return fmt.Errorf("overlay network with id %s not found", id)
}
// Release all vxlan IDs in one shot.
n.releaseVxlanID()
delete(d.networks, id)
return nil
}
func (n *network) releaseVxlanID() {
for _, s := range n.subnets {
n.driver.vxlanIdm.Unset(uint64(s.vni))
s.vni = 0
}
}
func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) DeleteNetwork(nid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, epOptions map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) DeleteEndpoint(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
return nil, types.NotImplementedErrorf("not implemented")
}
// Join method is invoked when a Sandbox is attached to an endpoint.
func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
// Leave method is invoked when a Sandbox detaches from an endpoint.
func (d *driver) Leave(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) Type() string {
return networkType
}
func (d *driver) IsBuiltIn() bool {
return true
}
func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}