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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
import socket
from scapy.layers.inet import IP, UDP
from scapy.layers.inet6 import IPv6
from scapy.layers.l2 import Ether, GRE
from scapy.packet import Raw
from scapy.data import IP_PROTOS
from framework import VppTestCase
from util import ppp
""" TestLB is a subclass of VPPTestCase classes.
TestLB class defines Load Balancer test cases for:
- IP4 to GRE4 encap on per-port vip case
- IP4 to GRE6 encap on per-port vip case
- IP6 to GRE4 encap on per-port vip case
- IP6 to GRE6 encap on per-port vip case
- IP4 to L3DSR encap on vip case
- IP4 to L3DSR encap on per-port vip case
- IP4 to NAT4 encap on per-port vip case
- IP6 to NAT6 encap on per-port vip case
As stated in comments below, GRE has issues with IPv6.
All test cases involving IPv6 are executed, but
received packets are not parsed and checked.
"""
class TestLB(VppTestCase):
""" Load Balancer Test Case """
@classmethod
def setUpClass(cls):
super(TestLB, cls).setUpClass()
cls.ass = range(5)
cls.packets = range(1)
try:
cls.create_pg_interfaces(range(2))
cls.interfaces = list(cls.pg_interfaces)
for i in cls.interfaces:
i.admin_up()
i.config_ip4()
i.config_ip6()
i.disable_ipv6_ra()
i.resolve_arp()
i.resolve_ndp()
dst4 = socket.inet_pton(socket.AF_INET, "10.0.0.0")
dst6 = socket.inet_pton(socket.AF_INET6, "2002::")
cls.vapi.ip_add_del_route(dst4, 24, cls.pg1.remote_ip4n)
cls.vapi.ip_add_del_route(dst6, 16, cls.pg1.remote_ip6n, is_ipv6=1)
cls.vapi.cli("lb conf ip4-src-address 39.40.41.42")
cls.vapi.cli("lb conf ip6-src-address 2004::1")
except Exception:
super(TestLB, cls).tearDownClass()
raise
def tearDown(self):
super(TestLB, self).tearDown()
if not self.vpp_dead:
self.logger.info(self.vapi.cli("show lb vip verbose"))
def getIPv4Flow(self, id):
return (IP(dst="90.0.%u.%u" % (id / 255, id % 255),
src="40.0.%u.%u" % (id / 255, id % 255)) /
UDP(sport=10000 + id, dport=20000))
def getIPv6Flow(self, id):
return (IPv6(dst="2001::%u" % (id), src="fd00:f00d:ffff::%u" % (id)) /
UDP(sport=10000 + id, dport=20000))
def generatePackets(self, src_if, isv4):
self.reset_packet_infos()
pkts = []
for pktid in self.packets:
info = self.create_packet_info(src_if, self.pg1)
payload = self.info_to_payload(info)
ip = self.getIPv4Flow(pktid) if isv4 else self.getIPv6Flow(pktid)
packet = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
ip /
Raw(payload))
self.extend_packet(packet, 128)
info.data = packet.copy()
pkts.append(packet)
return pkts
def checkInner(self, gre, isv4):
IPver = IP if isv4 else IPv6
self.assertEqual(gre.proto, 0x0800 if isv4 else 0x86DD)
self.assertEqual(gre.flags, 0)
self.assertEqual(gre.version, 0)
inner = IPver(str(gre.payload))
payload_info = self.payload_to_info(str(inner[Raw]))
self.info = self.packet_infos[payload_info.index]
self.assertEqual(payload_info.src, self.pg0.sw_if_index)
self.assertEqual(str(inner), str(self.info.data[IPver]))
def checkCapture(self, encap, isv4):
self.pg0.assert_nothing_captured()
out = self.pg1.get_capture(len(self.packets))
load = [0] * len(self.ass)
self.info = None
for p in out:
try:
asid = 0
gre = None
if (encap == 'gre4'):
ip = p[IP]
asid = int(ip.dst.split(".")[3])
self.assertEqual(ip.version, 4)
self./*
* Copyright (c) 2016 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.
*/
#include <vnet/lisp-cp/gid_dictionary.h>
typedef struct
{
void *arg;
ip_prefix_t src;
foreach_subprefix_match_cb_t cb;
union
{
gid_ip4_table_t *ip4_table;
gid_ip6_table_t *ip6_table;
};
} sfib_entry_arg_t;
static u32 ip4_lookup (gid_ip4_table_t * db, u32 vni, ip_prefix_t * key);
static u32 ip6_lookup (gid_ip6_table_t * db, u32 vni, ip_prefix_t * key);
static void
foreach_sfib4_subprefix (BVT (clib_bihash_kv) * kvp, void *arg)
{
sfib_entry_arg_t *a = arg;
u32 ip = (u32) kvp->key[0];
ip4_address_t *mask;
u8 plen = ip_prefix_len (&a->src);
ASSERT (plen <= 32);
mask = &a->ip4_table->ip4_fib_masks[plen];
u32 src_ip = ip_prefix_v4 (&a->src).as_u32;
src_ip &= mask->as_u32;
ip &= mask->as_u32;
if (src_ip == ip)
{
/* found sub-prefix of src prefix */
(a->cb) (kvp->value, a->arg);
}
}
static void
gid_dict_foreach_ip4_subprefix (gid_dictionary_t * db, u32 vni,
ip_prefix_t * src, ip_prefix_t * dst,
foreach_subprefix_match_cb_t cb, void *arg)
{
u32 sfi;
gid_ip4_table_t *sfib4;
sfib_entry_arg_t a;
sfi = ip4_lookup (&db->dst_ip4_table, vni, dst);
if (GID_LOOKUP_MISS == sfi)
return;
sfib4 = pool_elt_at_index (db->src_ip4_table_pool, sfi);
a.arg = arg;
a.cb = cb;
a.src = src[0];
a.ip4_table = sfib4;
BV (clib_bihash_foreach_key_value_pair) (&sfib4->ip4_lookup_table,
foreach_sfib4_subprefix, &a);
}
static void
foreach_sfib6_subprefix (BVT (clib_bihash_kv) * kvp, void *arg)
{
sfib_entry_arg_t *a = arg;
ip6_address_t ip;
ip6_address_t *mask;
u8 plen = ip_prefix_len (&a->src);
mask = &a->ip6_table->ip6_fib_masks[plen];
ip.as_u64[0] = kvp->key[0];
ip.as_u64[1] = kvp->key[1];
if (ip6_address_is_equal_masked (&ip_prefix_v6 (&a->src), &ip, mask))
{
/* found sub-prefix of src prefix */
(a->cb) (kvp->value, a->arg);
}
}
static void
gid_dict_foreach_ip6_subprefix (gid_dictionary_t * db, u32 vni,
ip_prefix_t * src, ip_prefix_t * dst,
foreach_subprefix_match_cb_t cb, void *arg)
{
u32 sfi;
gid_ip6_table_t *sfib6;
sfib_entry_arg_t a;
sfi = ip6_lookup (&db->dst_ip6_table, vni, dst);
if (GID_LOOKUP_MISS == sfi)
return;
sfib6 = pool_elt_at_index (db->src_ip6_table_pool, sfi);
a.arg = arg;
a.cb = cb;
a.src = src[0];
a.ip6_table = sfib6;
BV (clib_bihash_foreach_key_value_pair) (&sfib6->ip6_lookup_table,
foreach_sfib6_subprefix, &a);
}
void
gid_dict_foreach_subprefix (gid_dictionary_t * db, gid_address_t * eid,
foreach_subprefix_match_cb_t cb, void *arg)
{
ip_prefix_t *ippref = &gid_address_sd_dst_ippref (eid);
if (IP4 == ip_prefix_version (ippref))
gid_dict_foreach_ip4_subprefix (db, gid_address_vni (eid),
&gid_address_sd_src_ippref (eid),
&gid_address_sd_dst_ippref (eid), cb,
arg);
else
gid_dict_foreach_ip6_subprefix (db, gid_address_vni (eid),
&gid_address_sd_src_ippref (eid),
&gid_address_sd_dst_ippref (eid), cb,
arg);
}
void
gid_dict_foreach_l2_arp_ndp_entry (gid_dictionary_t * db, void (*cb)
(BVT (clib_bihash_kv) * kvp, void *arg),
void *ht)
{
gid_l2_arp_ndp_table_t *tab = &db->arp_ndp_table;
BV (clib_bihash_foreach_key_value_pair) (&tab->arp_ndp_lookup_table, cb,
ht);
}
static void
make_mac_sd_key (BVT (clib_bihash_kv) * kv, u32 vni, u8 src_mac[6],
u8 dst_mac[6])
{
kv->key[0] = (u64) vni;
kv->key[1] = mac_to_u64 (dst_mac);
kv->key[2] = src_mac ? mac_to_u64 (src_mac) : (u64) 0;
}
static u32
mac_sd_lookup (gid_mac_table_t * db, u32 vni, u8 * dst, u8 * src)
{
int rv;
BVT (clib_bihash_kv) kv, value;
make_mac_sd_key (&kv, vni, src, dst);
rv = BV (clib_bihash_search_inline_2) (&db->mac_lookup_table, &kv, &value);
/* no match, try with src 0, catch all for dst */
if (rv != 0)
{
kv.key[2] = 0;
rv = BV (clib_bihash_search_inline_2) (&db->mac_lookup_table, &kv,
&value);
if (rv == 0)
return value.value;
}
else
return value.value;
return GID_LOOKUP_MISS;
}
static u32
ip4_lookup_exact_match (gid_ip4_table_t * db, u32 vni, ip_prefix_t * key)
{
int rv;
BVT (clib_bihash_kv) kv, value;
ip4_address_t *mask;
mask = &db->ip4_fib_masks[ip_prefix_len (key)];
kv.key[0] = ((u64) vni << 32) | (ip_prefix_v4 (key).as_u32 & mask->as_u32);
kv.key[1] = 0;
kv.key[2] = 0;
rv = BV (clib_bihash_search_inline_2) (&db->ip4_lookup_table, &kv, &value);
if (rv == 0)
return value.value;
return GID_LOOKUP_MISS;
}
static u32
ip4_lookup (gid_ip4_table_t * db, u32 vni, ip_prefix_t * key)
{
int i, len;
int rv;
BVT (clib_bihash_kv) kv, value;
len = vec_len (db->ip4_prefix_lengths_in_search_order);
for (i = 0; i < len; i++)
{
int dst_address_length = db->ip4_prefix_lengths_in_search_order[i];
ip4_address_t *mask;
ASSERT (dst_address_length >= 0 && dst_address_length <= 32);
mask = &db->ip4_fib_masks[dst_address_length];
kv.key[0] =
((u64) vni << 32) | (ip_prefix_v4 (key).as_u32 & mask->as_u32);
kv.key[1] = 0;
kv.key[2] = 0;
rv =
BV (clib_bihash_search_inline_2) (&db->ip4_lookup_table, &kv, &value);
if (rv == 0)
return value.value;
}
return GID_LOOKUP_MISS;
}
static u32
ip6_lookup_exact_match (gid_ip6_table_t * db, u32 vni, ip_prefix_t * key)
{
int rv;
BVT (clib_bihash_kv) kv, value;
ip6_address_t *mask;
mask = &db->ip6_fib_masks[ip_prefix_len (key)];
kv.key[0] = ip_prefix_v6 (key).as_u64[0] & mask->as_u64[0];
kv.key[1] = ip_prefix_v6 (key).as_u64[1] & mask->as_u64[1];
kv.key[2] = (u64) vni;
rv = BV (clib_bihash_search_inline_2) (&db->ip6_lookup_table, &kv, &value);
if (rv == 0)
return value.value;
return GID_LOOKUP_MISS;
}
static u32
ip6_lookup (gid_ip6_table_t * db, u32 vni, ip_prefix_t * key)
{
int i, len;
int rv;
BVT (clib_bihash_kv) kv, value;
len = vec_len (db->ip6_prefix_lengths_in_search_order);
for (i = 0; i < len; i++)
{
int dst_address_length = db->ip6_prefix_lengths_in_search_order[i];
ip6_address_t *mask;
ASSERT (dst_address_length >= 0 && dst_address_length <= 128);
mask = &db->ip6_fib_masks[dst_address_length];
kv.key[0] = ip_prefix_v6 (key).as_u64[0] & mask->as_u64[0];
kv.key[1] = ip_prefix_v6 (key).as_u64[1] & mask->as_u64[1];
kv.key[2] = (u64) vni;
rv =
BV (clib_bihash_search_inline_2) (&db->ip6_lookup_table, &kv, &value);
if (rv == 0)
return value.value;
}
return GID_LOOKUP_MISS;
}
static u32
ip_sd_lookup (gid_dictionary_t * db, u32 vni, ip_prefix_t * dst,
ip_prefix_t * src)
{
u32 sfi;
gid_ip4_table_t *sfib4;
gid_ip6_table_t *sfib6;
switch (ip_prefix_version (dst))
{
case IP4:
sfi = ip4_lookup (&db->dst_ip4_table, vni, dst);
if (GID_LOOKUP_MISS != sfi)
sfib4 = pool_elt_at_index (db->src_ip4_table_pool, sfi);
else
return GID_LOOKUP_MISS;
if (!src)
{
ip_prefix_t sp;
clib_memset (&sp, 0, sizeof (sp));
return ip4_lookup_exact_match (sfib4, 0, &sp);
}
else
return ip4_lookup (sfib4, 0, src);
break;
case IP6:
sfi = ip6_lookup (&db->dst_ip6_table, vni, dst);
if (GID_LOOKUP_MISS != sfi)
sfib6 = pool_elt_at_index (db->src_ip6_table_pool, sfi);
else
return GID_LOOKUP_MISS;
if (!src)
{
ip_prefix_t sp;
clib_memset (&sp, 0, sizeof (sp));
ip_prefix_version (&sp) = IP6;
return ip6_lookup_exact_match (sfib6, 0, &sp);
}
else
return ip6_lookup (sfib6, 0, src);
break;
default:
clib_warning ("address type %d not supported!",
ip_prefix_version (dst));
break;
}
return GID_LOOKUP_MISS;
}
static void
make_arp_ndp_key (BVT (clib_bihash_kv) * kv, u32 bd, ip_address_t * addr)
{
kv->key[0] = ((u64) bd << 32) | (u32) ip_addr_version (addr);
if (ip_addr_version (addr) == IP4)
{
kv->key[1] = (u64) addr->ip.v4.as_u32;
kv->key[2] = (u64) 0;
}
else
{
kv->key[1] = (u64) addr->ip.v6.as_u64[0];
kv->key[2] = (u64) addr->ip.v6.as_u64[1];
}
}
static void
make_nsh_key (BVT (clib_bihash_kv) * kv, u32 vni, u32 spi, u8 si)
{
kv->key[0] = (u64) vni;
kv->key[1] = (u64) spi;
kv->key[2] = (u64) si;
}
static u64
arp_ndp_lookup (gid_l2_arp_ndp_table_t * db, u32 bd, ip_address_t * key)
{
int rv;
BVT (clib_bihash_kv) kv, value;
make_arp_ndp_key (&kv, bd, key);
rv = BV (clib_bihash_search_inline_2) (&db->arp_ndp_lookup_table, &kv,
&value);
if (rv == 0)
return value.value;
return GID_LOOKUP_MISS_L2;
}
static u32
nsh_lookup (gid_nsh_table_t * db, u32 vni, u32 spi, u8 si)
{
int rv;
BVT (clib_bihash_kv) kv, value;
make_nsh_key (&kv, vni, spi, si);
rv = BV (clib_bihash_search_inline_2) (&db->nsh_lookup_table, &kv, &value);
if (rv == 0)
return value.value;
return GID_LOOKUP_MISS;
}
u64
gid_dictionary_lookup (gid_dictionary_t * db, gid_address_t * key)
{
switch (gid_address_type (key))
{
case GID_ADDR_IP_PREFIX:
return ip_sd_lookup (db, gid_address_vni (key),
&gid_address_ippref (key), 0);
case GID_ADDR_MAC:
return mac_sd_lookup (&db->sd_mac_table, gid_address_vni (key),
gid_address_mac (key), 0);
case GID_ADDR_SRC_DST:
switch (gid_address_sd_dst_type (key))
{
case FID_ADDR_IP_PREF:
return ip_sd_lookup (db, gid_address_vni (key),
&gid_address_sd_dst_ippref (key),
&gid_address_sd_src_ippref (key));
break;
case FID_ADDR_MAC:
return mac_sd_lookup (&db->sd_mac_table, gid_address_vni (key),
gid_address_sd_dst_mac (key),
gid_address_sd_src_mac (key));
break;
default:
clib_warning ("Source/Dest address type %d not supported!",
gid_address_sd_dst_type (key));
break;
}
break;
case GID_ADDR_ARP:
case GID_ADDR_NDP:
return arp_ndp_lookup (&db->arp_ndp_table, gid_address_arp_ndp_bd (key),
&gid_address_arp_ndp_ip (key));
case GID_ADDR_NSH:
return nsh_lookup (&db->nsh_table, gid_address_vni (key),
gid_address_nsh_spi (key), gid_address_nsh_si (key));
default:
clib_warning ("address type %d not supported!", gid_address_type (key));
break;
}
return GID_LOOKUP_MISS;
}
u32
gid_dictionary_sd_lookup (gid_dictionary_t * db, gid_address_t * dst,
gid_address_t * src)
{
switch (gid_address_type (dst))
{
case GID_ADDR_IP_PREFIX:
return ip_sd_lookup (db, gid_address_vni (dst),
&gid_address_ippref (dst),
&gid_address_ippref (src));
case GID_ADDR_MAC:
return mac_sd_lookup (&db->sd_mac_table, gid_address_vni (dst),
gid_address_mac (dst), gid_address_mac (src));
case GID_ADDR_SRC_DST:
|