forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
390 lines (353 loc) · 9.52 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// 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 (
"context"
"log"
"sync"
"time"
"github.com/ligato/cn-infra/agent"
"github.com/ligato/cn-infra/datasync"
"github.com/ligato/cn-infra/datasync/kvdbsync/local"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/logging/logrus"
"github.com/ligato/vpp-agent/clientv1/vpp/localclient"
"github.com/ligato/vpp-agent/plugins/vpp"
vpp_intf "github.com/ligato/vpp-agent/plugins/vpp/model/interfaces"
"github.com/ligato/vpp-agent/plugins/vpp/model/nat"
"github.com/namsral/flag"
)
var (
timeout = flag.Int("timeout", 20, "Timeout between applying of global and DNAT configuration in seconds")
)
/* Confgiuration */
// Global NAT is a one-time configuration (single key in the etcd, but it can be modified or removed as ususally).
// Configured items are 'global' for the whole NAT44 environment. Data consists of:
// - NAT forwarding setup
// - Enabled interfaces (including output feature)
// - Enabled address pools
/* Result of test NAT global data */
/*
vpp# sh nat44 interfaces
NAT44 interfaces:
memif1/3 in
memif1/1 out
memif1/2 output-feature out
vpp# sh nat44 addresses
NAT44 pool addresses:
192.168.0.1
tenant VRF: 0
0 busy udp ports
0 busy tcp ports
0 busy icmp ports
175.124.0.1
tenant VRF: 0
0 busy udp ports
0 busy tcp ports
0 busy icmp ports
175.124.0.2
tenant VRF: 0
0 busy udp ports
0 busy tcp ports
0 busy icmp ports
175.124.0.3
tenant VRF: 0
0 busy udp ports
0 busy tcp ports
0 busy icmp ports
10.10.0.1
tenant VRF: 0
0 busy udp ports
0 busy tcp ports
0 busy icmp ports
10.10.0.2
tenant VRF: 0
0 busy udp ports
0 busy tcp ports
0 busy icmp ports
NAT44 twice-nat pool addresses:
vpp#
*/
// DNAT puts static mapping (with or without load balancer) or identity mapping entries to the VPP. Destination
// address can be translated to one or more local addresses. If more than one local address is used, load
// balancer is configured automatically.
/* Result of DNAT test data */
/*
vpp# sh nat44 static mappings
NAT44 static mappings:
udp vrf 0 external 192.168.0.1:8989 out2in-only
local 172.124.0.2:6500 probability 40
local 172.125.10.5:2300 probability 40
udp local 172.124.0.3:6501 external 192.47.21.1:8989 vrf 0 out2in-only
tcp local 10.10.0.1:2525 external 10.10.0.1:2525 vrf 0
vpp#
*/
/* Vpp-agent Init and Close*/
// Start Agent plugins selected for this example.
func main() {
//Init close channel to stop the example.
exampleFinished := make(chan struct{}, 1)
// Prepare all the dependencies for example plugin
watcher := datasync.KVProtoWatchers{
local.Get(),
}
vppPlugin := vpp.NewPlugin(vpp.UseDeps(func(deps *vpp.Deps) {
deps.Watcher = watcher
}))
// Inject dependencies to example plugin
ep := &NatExamplePlugin{}
ep.Deps.Log = logging.DefaultLogger
ep.Deps.VPP = vppPlugin
// Start Agent
a := agent.NewAgent(
agent.AllPlugins(ep),
agent.QuitOnClose(exampleFinished),
)
if err := a.Run(); err != nil {
log.Fatal()
}
go closeExample("localhost example finished", exampleFinished)
}
// Stop the agent with desired info message.
func closeExample(message string, exampleFinished chan struct{}) {
time.Sleep(time.Duration(*timeout+5) * time.Second)
logrus.DefaultLogger().Info(message)
close(exampleFinished)
}
/* NAT44 Example */
// NatExamplePlugin uses localclient to transport example global NAT and DNAT and af-packet
// configuration to NAT VPP plugin
type NatExamplePlugin struct {
Deps
wg sync.WaitGroup
cancel context.CancelFunc
}
// Deps is example plugin dependencies.
type Deps struct {
Log logging.Logger
VPP *vpp.Plugin
}
// PluginName represents name of plugin.
const PluginName = "nat-example"
// Init initializes example plugin.
func (plugin *NatExamplePlugin) Init() error {
// Logger
plugin.Log = logrus.DefaultLogger()
plugin.Log.SetLevel(logging.DebugLevel)
plugin.Log.Info("Initializing NAT44 example")
// Flags
flag.Parse()
plugin.Log.Infof("Timeout between configuring NAT global and DNAT set to %d", *timeout)
// Apply initial VPP configuration.
plugin.putGlobalConfig()
// Schedule reconfiguration.
var ctx context.Context
ctx, plugin.cancel = context.WithCancel(context.Background())
plugin.wg.Add(1)
go plugin.putDNAT(ctx, *timeout)
plugin.Log.Info("NAT example initialization done")
return nil
}
// Close cleans up the resources.
func (plugin *NatExamplePlugin) Close() error {
plugin.cancel()
plugin.wg.Wait()
logrus.DefaultLogger().Info("Closed NAT example plugin")
return nil
}
// String returns plugin name
func (plugin *NatExamplePlugin) String() string {
return PluginName
}
// Configure NAT44 Global config
func (plugin *NatExamplePlugin) putGlobalConfig() {
plugin.Log.Infof("Applying NAT44 global configuration")
err := localclient.DataResyncRequest(PluginName).
Interface(interface1()).
Interface(interface2()).
Interface(interface3()).
NAT44Global(globalNat()).
Send().ReceiveReply()
if err != nil {
plugin.Log.Errorf("NAT44 global configuration failed: %v", err)
} else {
plugin.Log.Info("NAT44 global configuration successful")
}
}
// Configure DNAT
func (plugin *NatExamplePlugin) putDNAT(ctx context.Context, timeout int) {
select {
case <-time.After(time.Duration(timeout) * time.Second):
plugin.Log.Infof("Applying DNAT configuration")
err := localclient.DataChangeRequest(PluginName).
Put().
NAT44DNat(dNat()).
Send().ReceiveReply()
if err != nil {
plugin.Log.Errorf("DNAT configuration failed: %v", err)
} else {
plugin.Log.Info("DNAT configuration successful")
}
case <-ctx.Done():
// Cancel the scheduled DNAT configuration.
plugin.Log.Info("DNAT configuration canceled")
}
plugin.wg.Done()
}
/* Example Data */
func interface1() *vpp_intf.Interfaces_Interface {
return &vpp_intf.Interfaces_Interface{
Name: "memif1",
Type: vpp_intf.InterfaceType_MEMORY_INTERFACE,
Enabled: true,
Mtu: 1478,
IpAddresses: []string{
"172.125.40.1/24",
},
Memif: &vpp_intf.Interfaces_Interface_Memif{
Id: 1,
Secret: "secret1",
Master: false,
SocketFilename: "/tmp/memif1.sock",
},
}
}
func interface2() *vpp_intf.Interfaces_Interface {
return &vpp_intf.Interfaces_Interface{
Name: "memif2",
Type: vpp_intf.InterfaceType_MEMORY_INTERFACE,
Enabled: true,
Mtu: 1478,
IpAddresses: []string{
"192.47.21.1/24",
},
Memif: &vpp_intf.Interfaces_Interface_Memif{
Id: 2,
Secret: "secret2",
Master: false,
SocketFilename: "/tmp/memif1.sock",
},
}
}
func interface3() *vpp_intf.Interfaces_Interface {
return &vpp_intf.Interfaces_Interface{
Name: "memif3",
Type: vpp_intf.InterfaceType_MEMORY_INTERFACE,
Enabled: true,
Mtu: 1478,
IpAddresses: []string{
"94.18.21.1/24",
},
Memif: &vpp_intf.Interfaces_Interface_Memif{
Id: 3,
Secret: "secret3",
Master: false,
SocketFilename: "/tmp/memif1.sock",
},
}
}
func globalNat() *nat.Nat44Global {
return &nat.Nat44Global{
Forwarding: false,
NatInterfaces: []*nat.Nat44Global_NatInterface{
{
Name: "memif1",
IsInside: false,
OutputFeature: false,
},
{
Name: "memif2",
IsInside: false,
OutputFeature: true,
},
{
Name: "memif3",
IsInside: true,
OutputFeature: false,
},
},
AddressPools: []*nat.Nat44Global_AddressPool{
{
VrfId: 0,
FirstSrcAddress: "192.168.0.1",
TwiceNat: false,
},
{
VrfId: 0,
FirstSrcAddress: "175.124.0.1",
LastSrcAddress: "175.124.0.3",
TwiceNat: false,
},
{
VrfId: 0,
FirstSrcAddress: "10.10.0.1",
LastSrcAddress: "10.10.0.2",
TwiceNat: false,
},
},
}
}
func dNat() *nat.Nat44DNat_DNatConfig {
return &nat.Nat44DNat_DNatConfig{
Label: "dnat1",
StMappings: []*nat.Nat44DNat_DNatConfig_StaticMapping{
{
// DNAT static mapping with load balancer (multiple local addresses)
ExternalInterface: "memif1",
ExternalIp: "192.168.0.1",
ExternalPort: 8989,
LocalIps: []*nat.Nat44DNat_DNatConfig_StaticMapping_LocalIP{
{
VrfId: 0,
LocalIp: "172.124.0.2",
LocalPort: 6500,
Probability: 40,
},
{
VrfId: 0,
LocalIp: "172.125.10.5",
LocalPort: 2300,
Probability: 40,
},
},
Protocol: 1,
TwiceNat: nat.TwiceNatMode_ENABLED,
},
{
// DNAT static mapping without load balancer (single local address)
ExternalInterface: "memif2",
ExternalIp: "192.168.0.2",
ExternalPort: 8989,
LocalIps: []*nat.Nat44DNat_DNatConfig_StaticMapping_LocalIP{
{
VrfId: 0,
LocalIp: "172.124.0.3",
LocalPort: 6501,
Probability: 50,
},
},
Protocol: 1,
TwiceNat: nat.TwiceNatMode_ENABLED,
},
},
IdMappings: []*nat.Nat44DNat_DNatConfig_IdentityMapping{
{
VrfId: 0,
IpAddress: "10.10.0.1",
Port: 2525,
Protocol: 0,
},
},
}
}