blob: fc1674aeedf668bb7f7d067a9ee1c6ac03c8e591 [file] [log] [blame]
Pierre Pfisterf588f352016-10-07 16:31:57 +01001import socket
Pierre Pfisterf588f352016-10-07 16:31:57 +01002
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -07003import scapy.compat
Pierre Pfisterf588f352016-10-07 16:31:57 +01004from scapy.layers.inet import IP, UDP
Klement Sekera65cc8c02016-12-18 15:49:54 +01005from scapy.layers.inet6 import IPv6
Klement Sekeraf62ae122016-10-11 11:47:09 +02006from scapy.layers.l2 import Ether, GRE
7from scapy.packet import Raw
Hongjun Ni6fb0d9b2018-06-08 07:12:05 +08008from scapy.data import IP_PROTOS
Pierre Pfisterf588f352016-10-07 16:31:57 +01009
Klement Sekeraf62ae122016-10-11 11:47:09 +020010from framework import VppTestCase
Klement Sekera7bb873a2016-11-18 07:38:42 +010011from util import ppp
Neale Ranns097fa662018-05-01 05:17:55 -070012from vpp_ip_route import VppIpRoute, VppRoutePath
13from vpp_ip import INVALID_INDEX
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000014from config import config
15import unittest
Klement Sekeraf62ae122016-10-11 11:47:09 +020016
17""" TestLB is a subclass of VPPTestCase classes.
18
19 TestLB class defines Load Balancer test cases for:
Hongjun Ni219cc902018-06-28 20:14:19 +080020 - IP4 to GRE4 encap on per-port vip case
21 - IP4 to GRE6 encap on per-port vip case
22 - IP6 to GRE4 encap on per-port vip case
23 - IP6 to GRE6 encap on per-port vip case
24 - IP4 to L3DSR encap on vip case
25 - IP4 to L3DSR encap on per-port vip case
Nobuhiro MIKI613e6dc2022-09-28 15:53:17 +090026 - IP4 to L3DSR encap on per-port vip with src_ip_sticky case
Hongjun Ni219cc902018-06-28 20:14:19 +080027 - IP4 to NAT4 encap on per-port vip case
28 - IP6 to NAT6 encap on per-port vip case
Klement Sekeraf62ae122016-10-11 11:47:09 +020029
30 As stated in comments below, GRE has issues with IPv6.
31 All test cases involving IPv6 are executed, but
32 received packets are not parsed and checked.
33
34"""
35
36
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000037@unittest.skipIf("lb" in config.excluded_plugins, "Exclude LB plugin tests")
Klement Sekeraf62ae122016-10-11 11:47:09 +020038class TestLB(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020039 """Load Balancer Test Case"""
Pierre Pfisterf588f352016-10-07 16:31:57 +010040
41 @classmethod
42 def setUpClass(cls):
43 super(TestLB, cls).setUpClass()
44
45 cls.ass = range(5)
Nobuhiro MIKI613e6dc2022-09-28 15:53:17 +090046 cls.packets = range(100)
Pierre Pfisterf588f352016-10-07 16:31:57 +010047
48 try:
Klement Sekeraf62ae122016-10-11 11:47:09 +020049 cls.create_pg_interfaces(range(2))
50 cls.interfaces = list(cls.pg_interfaces)
Pierre Pfisterf588f352016-10-07 16:31:57 +010051
Klement Sekeraf62ae122016-10-11 11:47:09 +020052 for i in cls.interfaces:
53 i.admin_up()
54 i.config_ip4()
55 i.config_ip6()
56 i.disable_ipv6_ra()
57 i.resolve_arp()
58 i.resolve_ndp()
Neale Ranns097fa662018-05-01 05:17:55 -070059
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020060 dst4 = VppIpRoute(
61 cls,
62 "10.0.0.0",
63 24,
64 [VppRoutePath(cls.pg1.remote_ip4, INVALID_INDEX)],
65 register=False,
66 )
Neale Ranns097fa662018-05-01 05:17:55 -070067 dst4.add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020068 dst6 = VppIpRoute(
69 cls,
70 "2002::",
71 16,
72 [VppRoutePath(cls.pg1.remote_ip6, INVALID_INDEX)],
73 register=False,
74 )
Neale Ranns097fa662018-05-01 05:17:55 -070075 dst6.add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 cls.vapi.lb_conf(ip4_src_address="39.40.41.42", ip6_src_address="2004::1")
Klement Sekeraf62ae122016-10-11 11:47:09 +020077 except Exception:
Pierre Pfisterf588f352016-10-07 16:31:57 +010078 super(TestLB, cls).tearDownClass()
79 raise
80
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070081 @classmethod
82 def tearDownClass(cls):
83 super(TestLB, cls).tearDownClass()
84
Pierre Pfisterf588f352016-10-07 16:31:57 +010085 def tearDown(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020086 super(TestLB, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -070087
88 def show_commands_at_teardown(self):
89 self.logger.info(self.vapi.cli("show lb vip verbose"))
Pierre Pfisterf588f352016-10-07 16:31:57 +010090
91 def getIPv4Flow(self, id):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020092 return IP(
93 dst="90.0.%u.%u" % (id / 255, id % 255),
94 src="40.0.%u.%u" % (id / 255, id % 255),
95 ) / UDP(sport=10000 + id, dport=20000)
Pierre Pfisterf588f352016-10-07 16:31:57 +010096
97 def getIPv6Flow(self, id):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020098 return IPv6(dst="2001::%u" % (id), src="fd00:f00d:ffff::%u" % (id)) / UDP(
99 sport=10000 + id, dport=20000
100 )
Pierre Pfisterf588f352016-10-07 16:31:57 +0100101
Klement Sekeraf62ae122016-10-11 11:47:09 +0200102 def generatePackets(self, src_if, isv4):
Klement Sekeradab231a2016-12-21 08:50:14 +0100103 self.reset_packet_infos()
Pierre Pfisterf588f352016-10-07 16:31:57 +0100104 pkts = []
105 for pktid in self.packets:
Klement Sekeradab231a2016-12-21 08:50:14 +0100106 info = self.create_packet_info(src_if, self.pg1)
Pierre Pfisterf588f352016-10-07 16:31:57 +0100107 payload = self.info_to_payload(info)
108 ip = self.getIPv4Flow(pktid) if isv4 else self.getIPv6Flow(pktid)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200109 packet = (
110 Ether(dst=src_if.local_mac, src=src_if.remote_mac) / ip / Raw(payload)
111 )
Pierre Pfisterf588f352016-10-07 16:31:57 +0100112 self.extend_packet(packet, 128)
113 info.data = packet.copy()
114 pkts.append(packet)
115 return pkts
116
117 def checkInner(self, gre, isv4):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200118 IPver = IP if isv4 else IPv6
Pierre Pfisterf588f352016-10-07 16:31:57 +0100119 self.assertEqual(gre.proto, 0x0800 if isv4 else 0x86DD)
120 self.assertEqual(gre.flags, 0)
121 self.assertEqual(gre.version, 0)
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700122 inner = IPver(scapy.compat.raw(gre.payload))
Paul Vinciguerraeaea4212019-03-06 11:58:06 -0800123 payload_info = self.payload_to_info(inner[Raw])
Klement Sekeradab231a2016-12-21 08:50:14 +0100124 self.info = self.packet_infos[payload_info.index]
125 self.assertEqual(payload_info.src, self.pg0.sw_if_index)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200126 self.assertEqual(
127 scapy.compat.raw(inner), scapy.compat.raw(self.info.data[IPver])
128 )
Pierre Pfisterf588f352016-10-07 16:31:57 +0100129
Nobuhiro MIKI613e6dc2022-09-28 15:53:17 +0900130 def checkCapture(self, encap, isv4, src_ip_sticky=False):
Klement Sekera65cc8c02016-12-18 15:49:54 +0100131 self.pg0.assert_nothing_captured()
Klement Sekeradab231a2016-12-21 08:50:14 +0100132 out = self.pg1.get_capture(len(self.packets))
Pierre Pfisterf588f352016-10-07 16:31:57 +0100133
134 load = [0] * len(self.ass)
Nobuhiro MIKI613e6dc2022-09-28 15:53:17 +0900135 sticky_as = {}
Pierre Pfisterf588f352016-10-07 16:31:57 +0100136 self.info = None
137 for p in out:
138 try:
139 asid = 0
140 gre = None
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200141 if encap == "gre4":
Pierre Pfisterf588f352016-10-07 16:31:57 +0100142 ip = p[IP]
Pierre Pfisterf588f352016-10-07 16:31:57 +0100143 asid = int(ip.dst.split(".")[3])
144 self.assertEqual(ip.version, 4)
145 self.assertEqual(ip.flags, 0)
146 self.assertEqual(ip.src, "39.40.41.42")
147 self.assertEqual(ip.dst, "10.0.0.%u" % asid)
148 self.assertEqual(ip.proto, 47)
149 self.assertEqual(len(ip.options), 0)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200150 gre = p[GRE]
Hongjun Ni647f6092018-01-23 19:17:23 +0800151 self.checkInner(gre, isv4)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200152 elif encap == "gre6":
Pierre Pfisterf588f352016-10-07 16:31:57 +0100153 ip = p[IPv6]
Pierre Pfisterf588f352016-10-07 16:31:57 +0100154 asid = ip.dst.split(":")
155 asid = asid[len(asid) - 1]
Klement Sekeraf62ae122016-10-11 11:47:09 +0200156 asid = 0 if asid == "" else int(asid)
Pierre Pfisterf588f352016-10-07 16:31:57 +0100157 self.assertEqual(ip.version, 6)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200158 self.assertEqual(ip.tc, 0)
159 self.assertEqual(ip.fl, 0)
160 self.assertEqual(ip.src, "2004::1")
161 self.assertEqual(
162 socket.inet_pton(socket.AF_INET6, ip.dst),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200163 socket.inet_pton(socket.AF_INET6, "2002::%u" % asid),
Klement Sekeraf62ae122016-10-11 11:47:09 +0200164 )
165 self.assertEqual(ip.nh, 47)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200166 # self.assertEqual(len(ip.options), 0)
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700167 gre = GRE(scapy.compat.raw(p[IPv6].payload))
Hongjun Ni647f6092018-01-23 19:17:23 +0800168 self.checkInner(gre, isv4)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200169 elif encap == "l3dsr":
Hongjun Ni647f6092018-01-23 19:17:23 +0800170 ip = p[IP]
171 asid = int(ip.dst.split(".")[3])
172 self.assertEqual(ip.version, 4)
173 self.assertEqual(ip.flags, 0)
174 self.assertEqual(ip.dst, "10.0.0.%u" % asid)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200175 self.assertEqual(ip.tos, 0x1C)
Hongjun Ni647f6092018-01-23 19:17:23 +0800176 self.assertEqual(len(ip.options), 0)
Hongjun Ni6fb0d9b2018-06-08 07:12:05 +0800177 self.assert_ip_checksum_valid(p)
178 if ip.proto == IP_PROTOS.tcp:
179 self.assert_tcp_checksum_valid(p)
180 elif ip.proto == IP_PROTOS.udp:
181 self.assert_udp_checksum_valid(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200182 elif encap == "nat4":
Hongjun Nid92a0b52018-02-06 23:00:22 +0800183 ip = p[IP]
184 asid = int(ip.dst.split(".")[3])
185 self.assertEqual(ip.version, 4)
186 self.assertEqual(ip.flags, 0)
187 self.assertEqual(ip.dst, "10.0.0.%u" % asid)
188 self.assertEqual(ip.proto, 17)
189 self.assertEqual(len(ip.options), 0)
Hongjun Nid92a0b52018-02-06 23:00:22 +0800190 udp = p[UDP]
191 self.assertEqual(udp.dport, 3307)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200192 elif encap == "nat6":
Hongjun Nid92a0b52018-02-06 23:00:22 +0800193 ip = p[IPv6]
194 asid = ip.dst.split(":")
195 asid = asid[len(asid) - 1]
196 asid = 0 if asid == "" else int(asid)
197 self.assertEqual(ip.version, 6)
198 self.assertEqual(ip.tc, 0)
199 self.assertEqual(ip.fl, 0)
200 self.assertEqual(
201 socket.inet_pton(socket.AF_INET6, ip.dst),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200202 socket.inet_pton(socket.AF_INET6, "2002::%u" % asid),
Hongjun Nid92a0b52018-02-06 23:00:22 +0800203 )
204 self.assertEqual(ip.nh, 17)
205 self.assertGreaterEqual(ip.hlim, 63)
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700206 udp = UDP(scapy.compat.raw(p[IPv6].payload))
Hongjun Nid92a0b52018-02-06 23:00:22 +0800207 self.assertEqual(udp.dport, 3307)
Pierre Pfisterf588f352016-10-07 16:31:57 +0100208 load[asid] += 1
Nobuhiro MIKI613e6dc2022-09-28 15:53:17 +0900209
210 # In case of source ip sticky, check that packets with same
211 # src_ip are routed to same as.
212 if src_ip_sticky and sticky_as.get(ip.src, asid) != asid:
213 raise Exception("Packets with same src_ip are routed to another as")
214 sticky_as[ip.src] = asid
215
Pierre Pfisterf588f352016-10-07 16:31:57 +0100216 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100217 self.logger.error(ppp("Unexpected or invalid packet:", p))
Pierre Pfisterf588f352016-10-07 16:31:57 +0100218 raise
219
Paul Vinciguerraf7f13342019-03-19 11:54:39 -0700220 # This is just to roughly check that the balancing algorithm
221 # is not completely biased.
Pierre Pfisterf588f352016-10-07 16:31:57 +0100222 for asid in self.ass:
snaramreec44e262019-10-16 22:36:47 +0000223 if load[asid] < int(len(self.packets) / (len(self.ass) * 2)):
Gabriel Ganne8e66b9b2017-12-14 16:20:37 +0100224 self.logger.error(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200225 "ASS is not balanced: load[%d] = %d" % (asid, load[asid])
226 )
Pierre Pfisterf588f352016-10-07 16:31:57 +0100227 raise Exception("Load Balancer algorithm is biased")
228
Pierre Pfisterf588f352016-10-07 16:31:57 +0100229 def test_lb_ip4_gre4(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200230 """Load Balancer IP4 GRE4 on vip case"""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200231 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200232 self.vapi.cli("lb vip 90.0.0.0/8 encap gre4")
Klement Sekeraf62ae122016-10-11 11:47:09 +0200233 for asid in self.ass:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200234 self.vapi.cli("lb as 90.0.0.0/8 10.0.0.%u" % (asid))
Pierre Pfisterf588f352016-10-07 16:31:57 +0100235
Klement Sekeraf62ae122016-10-11 11:47:09 +0200236 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=True))
237 self.pg_enable_capture(self.pg_interfaces)
238 self.pg_start()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200239 self.checkCapture(encap="gre4", isv4=True)
Pierre Pfisterf588f352016-10-07 16:31:57 +0100240
Klement Sekeraf62ae122016-10-11 11:47:09 +0200241 finally:
242 for asid in self.ass:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200243 self.vapi.cli("lb as 90.0.0.0/8 10.0.0.%u del" % (asid))
244 self.vapi.cli("lb vip 90.0.0.0/8 encap gre4 del")
Gabriel Ganneb3d1b202017-10-30 15:44:31 +0100245 self.vapi.cli("test lb flowtable flush")
Pierre Pfisterf588f352016-10-07 16:31:57 +0100246
247 def test_lb_ip6_gre4(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200248 """Load Balancer IP6 GRE4 on vip case"""
Pierre Pfisterf588f352016-10-07 16:31:57 +0100249
Klement Sekeraf62ae122016-10-11 11:47:09 +0200250 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200251 self.vapi.cli("lb vip 2001::/16 encap gre4")
Klement Sekeraf62ae122016-10-11 11:47:09 +0200252 for asid in self.ass:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200253 self.vapi.cli("lb as 2001::/16 10.0.0.%u" % (asid))
Pierre Pfisterf588f352016-10-07 16:31:57 +0100254
Klement Sekeraf62ae122016-10-11 11:47:09 +0200255 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=False))
256 self.pg_enable_capture(self.pg_interfaces)
257 self.pg_start()
Pierre Pfisterf588f352016-10-07 16:31:57 +0100258
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200259 self.checkCapture(encap="gre4", isv4=False)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200260 finally:
261 for asid in self.ass:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200262 self.vapi.cli("lb as 2001::/16 10.0.0.%u del" % (asid))
263 self.vapi.cli("lb vip 2001::/16 encap gre4 del")
Gabriel Ganneb3d1b202017-10-30 15:44:31 +0100264 self.vapi.cli("test lb flowtable flush")
Pierre Pfisterf588f352016-10-07 16:31:57 +0100265
266 def test_lb_ip4_gre6(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200267 """Load Balancer IP4 GRE6 on vip case"""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200268 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200269 self.vapi.cli("lb vip 90.0.0.0/8 encap gre6")
Klement Sekeraf62ae122016-10-11 11:47:09 +0200270 for asid in self.ass:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200271 self.vapi.cli("lb as 90.0.0.0/8 2002::%u" % (asid))
Pierre Pfisterf588f352016-10-07 16:31:57 +0100272
Klement Sekeraf62ae122016-10-11 11:47:09 +0200273 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=True))
274 self.pg_enable_capture(self.pg_interfaces)
275 self.pg_start()
Pierre Pfisterf588f352016-10-07 16:31:57 +0100276
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200277 self.checkCapture(encap="gre6", isv4=True)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200278 finally:
279 for asid in self.ass:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200280 self.vapi.cli("lb as 90.0.0.0/8 2002::%u del" % (asid))
281 self.vapi.cli("lb vip 90.0.0.0/8 encap gre6 del")
Gabriel Ganneb3d1b202017-10-30 15:44:31 +0100282 self.vapi.cli("test lb flowtable flush")
Pierre Pfisterf588f352016-10-07 16:31:57 +0100283
284 def test_lb_ip6_gre6(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200285 """Load Balancer IP6 GRE6 on vip case"""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200286 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200287 self.vapi.cli("lb vip 2001::/16 encap gre6")
Klement Sekeraf62ae122016-10-11 11:47:09 +0200288 for asid in self.ass:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200289 self.vapi.cli("lb as 2001::/16 2002::%u" % (asid))
Pierre Pfisterf588f352016-10-07 16:31:57 +0100290
Klement Sekeraf62ae122016-10-11 11:47:09 +0200291 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=False))
292 self.pg_enable_capture(self.pg_interfaces)
293 self.pg_start()
Pierre Pfisterf588f352016-10-07 16:31:57 +0100294
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200295 self.checkCapture(encap="gre6", isv4=False)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200296 finally:
297 for asid in self.ass:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200298 self.vapi.cli("lb as 2001::/16 2002::%u del" % (asid))
299 self.vapi.cli("lb vip 2001::/16 encap gre6 del")
Hongjun Ni219cc902018-06-28 20:14:19 +0800300 self.vapi.cli("test lb flowtable flush")
301
302 def test_lb_ip4_gre4_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200303 """Load Balancer IP4 GRE4 on per-port-vip case"""
Hongjun Ni219cc902018-06-28 20:14:19 +0800304 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200305 self.vapi.cli("lb vip 90.0.0.0/8 protocol udp port 20000 encap gre4")
Hongjun Ni219cc902018-06-28 20:14:19 +0800306 for asid in self.ass:
307 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200308 "lb as 90.0.0.0/8 protocol udp port 20000 10.0.0.%u" % (asid)
309 )
Hongjun Ni219cc902018-06-28 20:14:19 +0800310
311 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=True))
312 self.pg_enable_capture(self.pg_interfaces)
313 self.pg_start()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200314 self.checkCapture(encap="gre4", isv4=True)
Hongjun Ni219cc902018-06-28 20:14:19 +0800315
316 finally:
317 for asid in self.ass:
318 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200319 "lb as 90.0.0.0/8 protocol udp port 20000 10.0.0.%u del" % (asid)
320 )
321 self.vapi.cli("lb vip 90.0.0.0/8 protocol udp port 20000 encap gre4 del")
Hongjun Ni219cc902018-06-28 20:14:19 +0800322 self.vapi.cli("test lb flowtable flush")
323
324 def test_lb_ip6_gre4_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200325 """Load Balancer IP6 GRE4 on per-port-vip case"""
Hongjun Ni219cc902018-06-28 20:14:19 +0800326
327 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200328 self.vapi.cli("lb vip 2001::/16 protocol udp port 20000 encap gre4")
Hongjun Ni219cc902018-06-28 20:14:19 +0800329 for asid in self.ass:
330 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200331 "lb as 2001::/16 protocol udp port 20000 10.0.0.%u" % (asid)
332 )
Hongjun Ni219cc902018-06-28 20:14:19 +0800333
334 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=False))
335 self.pg_enable_capture(self.pg_interfaces)
336 self.pg_start()
337
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200338 self.checkCapture(encap="gre4", isv4=False)
Hongjun Ni219cc902018-06-28 20:14:19 +0800339 finally:
340 for asid in self.ass:
341 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200342 "lb as 2001::/16 protocol udp port 20000 10.0.0.%u del" % (asid)
343 )
344 self.vapi.cli("lb vip 2001::/16 protocol udp port 20000 encap gre4 del")
Hongjun Ni219cc902018-06-28 20:14:19 +0800345 self.vapi.cli("test lb flowtable flush")
346
347 def test_lb_ip4_gre6_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200348 """Load Balancer IP4 GRE6 on per-port-vip case"""
Hongjun Ni219cc902018-06-28 20:14:19 +0800349 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200350 self.vapi.cli("lb vip 90.0.0.0/8 protocol udp port 20000 encap gre6")
Hongjun Ni219cc902018-06-28 20:14:19 +0800351 for asid in self.ass:
352 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200353 "lb as 90.0.0.0/8 protocol udp port 20000 2002::%u" % (asid)
354 )
Hongjun Ni219cc902018-06-28 20:14:19 +0800355
356 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=True))
357 self.pg_enable_capture(self.pg_interfaces)
358 self.pg_start()
359
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200360 self.checkCapture(encap="gre6", isv4=True)
Hongjun Ni219cc902018-06-28 20:14:19 +0800361 finally:
362 for asid in self.ass:
363 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200364 "lb as 90.0.0.0/8 protocol udp port 20000 2002::%u del" % (asid)
365 )
366 self.vapi.cli("lb vip 90.0.0.0/8 protocol udp port 20000 encap gre6 del")
Hongjun Ni219cc902018-06-28 20:14:19 +0800367 self.vapi.cli("test lb flowtable flush")
368
369 def test_lb_ip6_gre6_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200370 """Load Balancer IP6 GRE6 on per-port-vip case"""
Hongjun Ni219cc902018-06-28 20:14:19 +0800371 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200372 self.vapi.cli("lb vip 2001::/16 protocol udp port 20000 encap gre6")
Hongjun Ni219cc902018-06-28 20:14:19 +0800373 for asid in self.ass:
374 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200375 "lb as 2001::/16 protocol udp port 20000 2002::%u" % (asid)
376 )
Hongjun Ni219cc902018-06-28 20:14:19 +0800377
378 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=False))
379 self.pg_enable_capture(self.pg_interfaces)
380 self.pg_start()
381
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200382 self.checkCapture(encap="gre6", isv4=False)
Hongjun Ni219cc902018-06-28 20:14:19 +0800383 finally:
384 for asid in self.ass:
385 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200386 "lb as 2001::/16 protocol udp port 20000 2002::%u del" % (asid)
387 )
388 self.vapi.cli("lb vip 2001::/16 protocol udp port 20000 encap gre6 del")
Gabriel Ganneb3d1b202017-10-30 15:44:31 +0100389 self.vapi.cli("test lb flowtable flush")
Hongjun Ni647f6092018-01-23 19:17:23 +0800390
391 def test_lb_ip4_l3dsr(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200392 """Load Balancer IP4 L3DSR on vip case"""
Hongjun Ni647f6092018-01-23 19:17:23 +0800393 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200394 self.vapi.cli("lb vip 90.0.0.0/8 encap l3dsr dscp 7")
Hongjun Ni647f6092018-01-23 19:17:23 +0800395 for asid in self.ass:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200396 self.vapi.cli("lb as 90.0.0.0/8 10.0.0.%u" % (asid))
Hongjun Ni647f6092018-01-23 19:17:23 +0800397
398 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=True))
399 self.pg_enable_capture(self.pg_interfaces)
400 self.pg_start()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200401 self.checkCapture(encap="l3dsr", isv4=True)
Hongjun Ni647f6092018-01-23 19:17:23 +0800402
403 finally:
404 for asid in self.ass:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200405 self.vapi.cli("lb as 90.0.0.0/8 10.0.0.%u del" % (asid))
406 self.vapi.cli("lb vip 90.0.0.0/8 encap l3dsr dscp 7 del")
Hongjun Ni647f6092018-01-23 19:17:23 +0800407 self.vapi.cli("test lb flowtable flush")
Hongjun Nid92a0b52018-02-06 23:00:22 +0800408
Nobuhiro MIKI95c2da72023-06-28 15:15:58 +0900409 def test_lb_ip4_l3dsr_src_ip_sticky(self):
410 """Load Balancer IP4 L3DSR on vip with src_ip_sticky case"""
411 try:
412 self.vapi.cli("lb vip 90.0.0.0/8 encap l3dsr dscp 7 src_ip_sticky")
413 for asid in self.ass:
414 self.vapi.cli("lb as 90.0.0.0/8 10.0.0.%u" % (asid))
415
416 # Generate duplicated packets
417 pkts = self.generatePackets(self.pg0, isv4=True)
418 pkts = pkts[: len(pkts) // 2]
419 pkts = pkts + pkts
420
421 self.pg0.add_stream(pkts)
422 self.pg_enable_capture(self.pg_interfaces)
423 self.pg_start()
424 self.checkCapture(encap="l3dsr", isv4=True, src_ip_sticky=True)
425
426 finally:
427 for asid in self.ass:
428 self.vapi.cli("lb as 90.0.0.0/8 10.0.0.%u del" % (asid))
429 self.vapi.cli("lb vip 90.0.0.0/8 encap l3dsr dscp 7 src_ip_sticky del")
430 self.vapi.cli("test lb flowtable flush")
431
Hongjun Ni219cc902018-06-28 20:14:19 +0800432 def test_lb_ip4_l3dsr_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200433 """Load Balancer IP4 L3DSR on per-port-vip case"""
Hongjun Nid92a0b52018-02-06 23:00:22 +0800434 try:
Hongjun Ni219cc902018-06-28 20:14:19 +0800435 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200436 "lb vip 90.0.0.0/8 protocol udp port 20000 encap l3dsr dscp 7"
437 )
Hongjun Nid92a0b52018-02-06 23:00:22 +0800438 for asid in self.ass:
Hongjun Ni219cc902018-06-28 20:14:19 +0800439 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200440 "lb as 90.0.0.0/8 protocol udp port 20000 10.0.0.%u" % (asid)
441 )
Hongjun Ni219cc902018-06-28 20:14:19 +0800442
443 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=True))
444 self.pg_enable_capture(self.pg_interfaces)
445 self.pg_start()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200446 self.checkCapture(encap="l3dsr", isv4=True)
Hongjun Ni219cc902018-06-28 20:14:19 +0800447
448 finally:
449 for asid in self.ass:
450 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200451 "lb as 90.0.0.0/8 protocol udp port 20000 10.0.0.%u del" % (asid)
452 )
Hongjun Ni219cc902018-06-28 20:14:19 +0800453 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200454 "lb vip 90.0.0.0/8 protocol udp port 20000 encap l3dsr dscp 7 del"
455 )
Hongjun Ni219cc902018-06-28 20:14:19 +0800456 self.vapi.cli("test lb flowtable flush")
457
Nobuhiro MIKI613e6dc2022-09-28 15:53:17 +0900458 def test_lb_ip4_l3dsr_port_src_ip_sticky(self):
459 """Load Balancer IP4 L3DSR on per-port-vip with src_ip_sticky case"""
460 try:
Nobuhiro MIKI95c2da72023-06-28 15:15:58 +0900461 # This VIP at port 1000 does not receive packets, but is defined
462 # as a dummy to verify that the src_ip_sticky flag can be set
463 # independently for each port.
464 self.vapi.cli(
465 "lb vip 90.0.0.0/8 protocol udp port 10000 encap l3dsr dscp 7"
466 )
Nobuhiro MIKI613e6dc2022-09-28 15:53:17 +0900467 self.vapi.cli(
468 "lb vip 90.0.0.0/8 protocol udp port 20000 encap l3dsr dscp 7 src_ip_sticky"
469 )
470 for asid in self.ass:
471 self.vapi.cli(
472 "lb as 90.0.0.0/8 protocol udp port 20000 10.0.0.%u" % (asid)
473 )
474
475 # Generate duplicated packets
476 pkts = self.generatePackets(self.pg0, isv4=True)
477 pkts = pkts[: len(pkts) // 2]
478 pkts = pkts + pkts
479
480 self.pg0.add_stream(pkts)
481 self.pg_enable_capture(self.pg_interfaces)
482 self.pg_start()
483 self.checkCapture(encap="l3dsr", isv4=True, src_ip_sticky=True)
484
485 finally:
486 for asid in self.ass:
487 self.vapi.cli(
488 "lb as 90.0.0.0/8 protocol udp port 20000 10.0.0.%u del" % (asid)
489 )
490 self.vapi.cli(
491 "lb vip 90.0.0.0/8 protocol udp port 20000 encap l3dsr dscp 7 src_ip_sticky del"
492 )
Nobuhiro MIKI95c2da72023-06-28 15:15:58 +0900493 self.vapi.cli(
494 "lb vip 90.0.0.0/8 protocol udp port 10000 encap l3dsr dscp 7 del"
495 )
Nobuhiro MIKI613e6dc2022-09-28 15:53:17 +0900496 self.vapi.cli("test lb flowtable flush")
497
Hongjun Ni219cc902018-06-28 20:14:19 +0800498 def test_lb_ip4_nat4_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200499 """Load Balancer IP4 NAT4 on per-port-vip case"""
Hongjun Ni219cc902018-06-28 20:14:19 +0800500 try:
501 self.vapi.cli(
502 "lb vip 90.0.0.0/8 protocol udp port 20000 encap nat4"
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200503 " type clusterip target_port 3307"
504 )
Hongjun Ni219cc902018-06-28 20:14:19 +0800505 for asid in self.ass:
506 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200507 "lb as 90.0.0.0/8 protocol udp port 20000 10.0.0.%u" % (asid)
508 )
Hongjun Nid92a0b52018-02-06 23:00:22 +0800509
510 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=True))
511 self.pg_enable_capture(self.pg_interfaces)
512 self.pg_start()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200513 self.checkCapture(encap="nat4", isv4=True)
Hongjun Nid92a0b52018-02-06 23:00:22 +0800514
515 finally:
516 for asid in self.ass:
Hongjun Ni219cc902018-06-28 20:14:19 +0800517 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200518 "lb as 90.0.0.0/8 protocol udp port 20000 10.0.0.%u del" % (asid)
519 )
Hongjun Ni219cc902018-06-28 20:14:19 +0800520 self.vapi.cli(
521 "lb vip 90.0.0.0/8 protocol udp port 20000 encap nat4"
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200522 " type clusterip target_port 3307 del"
523 )
Hongjun Nid92a0b52018-02-06 23:00:22 +0800524 self.vapi.cli("test lb flowtable flush")
525
Hongjun Ni219cc902018-06-28 20:14:19 +0800526 def test_lb_ip6_nat6_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200527 """Load Balancer IP6 NAT6 on per-port-vip case"""
Hongjun Nid92a0b52018-02-06 23:00:22 +0800528 try:
Hongjun Ni219cc902018-06-28 20:14:19 +0800529 self.vapi.cli(
530 "lb vip 2001::/16 protocol udp port 20000 encap nat6"
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200531 " type clusterip target_port 3307"
532 )
Hongjun Nid92a0b52018-02-06 23:00:22 +0800533 for asid in self.ass:
Hongjun Ni219cc902018-06-28 20:14:19 +0800534 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200535 "lb as 2001::/16 protocol udp port 20000 2002::%u" % (asid)
536 )
Hongjun Nid92a0b52018-02-06 23:00:22 +0800537
538 self.pg0.add_stream(self.generatePackets(self.pg0, isv4=False))
539 self.pg_enable_capture(self.pg_interfaces)
540 self.pg_start()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200541 self.checkCapture(encap="nat6", isv4=False)
Hongjun Nid92a0b52018-02-06 23:00:22 +0800542
543 finally:
544 for asid in self.ass:
Hongjun Ni219cc902018-06-28 20:14:19 +0800545 self.vapi.cli(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200546 "lb as 2001::/16 protocol udp port 20000 2002::%u del" % (asid)
547 )
Hongjun Ni219cc902018-06-28 20:14:19 +0800548 self.vapi.cli(
549 "lb vip 2001::/16 protocol udp port 20000 encap nat6"
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200550 " type clusterip target_port 3307 del"
551 )
Hongjun Nid92a0b52018-02-06 23:00:22 +0800552 self.vapi.cli("test lb flowtable flush")