5 from framework import VppTestCase, VppTestRunner
7 from scapy.layers.inet import IP, TCP
8 from scapy.layers.inet6 import IPv6
9 from scapy.layers.l2 import Ether
10 from scapy.packet import Raw
13 class TestMSSClamp(VppTestCase):
14 """TCP MSS Clamping Test Case"""
17 super(TestMSSClamp, self).setUp()
19 # create 2 pg interfaces
20 self.create_pg_interfaces(range(2))
22 for i in self.pg_interfaces:
30 for i in self.pg_interfaces:
34 super(TestMSSClamp, self).tearDown()
36 def verify_pkt(self, rx, expected_mss):
37 # check that the MSS size equals the expected value
38 # and the IP and TCP checksums are correct
44 ip_csum = rx[IP].chksum
48 self.assertEqual(opt[0][0], "MSS")
49 self.assertEqual(opt[0][1], expected_mss)
50 # recalculate checksums
51 rx = rx.__class__(bytes(rx))
53 self.assertEqual(tcp_csum, tcp.chksum)
55 self.assertEqual(ip_csum, rx[IP].chksum)
57 def send_and_verify_ip4(self, src_pg, dst_pg, mss, expected_mss):
58 # IPv4 TCP packet with the requested MSS option.
59 # from a host on src_pg to a host on dst_pg.
61 Ether(dst=src_pg.local_mac, src=src_pg.remote_mac)
62 / IP(src=src_pg.remote_ip4, dst=dst_pg.remote_ip4)
67 options=[("MSS", (mss)), ("EOL", None)],
72 rxs = self.send_and_expect(src_pg, p * 65, dst_pg)
75 self.verify_pkt(rx, expected_mss)
77 def send_and_verify_ip6(self, src_pg, dst_pg, mss, expected_mss):
79 # IPv6 TCP packet with the requested MSS option.
80 # from a host on src_pg to a host on dst_pg.
83 Ether(dst=src_pg.local_mac, src=src_pg.remote_mac)
84 / IPv6(src=src_pg.remote_ip6, dst=dst_pg.remote_ip6)
89 options=[("MSS", (mss)), ("EOL", None)],
94 rxs = self.send_and_expect(src_pg, p * 65, dst_pg)
97 self.verify_pkt(rx, expected_mss)
99 def test_tcp_mss_clamping_ip4_tx(self):
100 """IP4 TCP MSS Clamping TX"""
102 # enable the TCP MSS clamping feature to lower the MSS to 1424.
103 self.vapi.mss_clamp_enable_disable(
104 self.pg1.sw_if_index,
111 # Verify that the feature is enabled.
112 rv, reply = self.vapi.mss_clamp_get(sw_if_index=self.pg1.sw_if_index)
113 self.assertEqual(reply[0].ipv4_mss, 1424)
114 self.assertEqual(reply[0].ipv4_direction, 3)
116 # Send syn packets and verify that the MSS value is lowered.
117 self.send_and_verify_ip4(self.pg0, self.pg1, 1460, 1424)
120 stats = self.statistics.get_counter("/err/tcp-mss-clamping-ip4-out/clamped")
121 self.assertEqual(sum(stats), 65)
123 # Send syn packets with small enough MSS values and verify they are
125 self.send_and_verify_ip4(self.pg0, self.pg1, 1400, 1400)
127 # enable the the feature only in TX direction
128 # and change the max MSS value
129 self.vapi.mss_clamp_enable_disable(
130 self.pg1.sw_if_index,
137 # Send syn packets and verify that the MSS value is lowered.
138 self.send_and_verify_ip4(self.pg0, self.pg1, 1460, 1420)
140 # enable the the feature only in RX direction
141 self.vapi.mss_clamp_enable_disable(
142 self.pg1.sw_if_index,
149 # Send the packets again and ensure they are unchanged.
150 self.send_and_verify_ip4(self.pg0, self.pg1, 1460, 1460)
152 # disable the feature
153 self.vapi.mss_clamp_enable_disable(
154 self.pg1.sw_if_index,
161 # Send the packets again and ensure they are unchanged.
162 self.send_and_verify_ip4(self.pg0, self.pg1, 1460, 1460)
164 def test_tcp_mss_clamping_ip4_rx(self):
165 """IP4 TCP MSS Clamping RX"""
167 # enable the TCP MSS clamping feature to lower the MSS to 1424.
168 self.vapi.mss_clamp_enable_disable(
169 self.pg1.sw_if_index,
176 # Verify that the feature is enabled.
177 rv, reply = self.vapi.mss_clamp_get(sw_if_index=self.pg1.sw_if_index)
178 self.assertEqual(reply[0].ipv4_mss, 1424)
179 self.assertEqual(reply[0].ipv4_direction, 3)
181 # Send syn packets and verify that the MSS value is lowered.
182 self.send_and_verify_ip4(self.pg1, self.pg0, 1460, 1424)
185 stats = self.statistics.get_counter("/err/tcp-mss-clamping-ip4-in/clamped")
186 self.assertEqual(sum(stats), 65)
188 # Send syn packets with small enough MSS values and verify they are
190 self.send_and_verify_ip4(self.pg1, self.pg0, 1400, 1400)
192 # enable the the feature only in RX direction
193 # and change the max MSS value
194 self.vapi.mss_clamp_enable_disable(
195 self.pg1.sw_if_index,
202 # Send syn packets and verify that the MSS value is lowered.
203 self.send_and_verify_ip4(self.pg1, self.pg0, 1460, 1420)
205 # enable the the feature only in TX direction
206 self.vapi.mss_clamp_enable_disable(
207 self.pg1.sw_if_index,
214 # Send the packets again and ensure they are unchanged.
215 self.send_and_verify_ip4(self.pg1, self.pg0, 1460, 1460)
217 # disable the feature
218 self.vapi.mss_clamp_enable_disable(
219 self.pg1.sw_if_index,
226 # Send the packets again and ensure they are unchanged.
227 self.send_and_verify_ip4(self.pg1, self.pg0, 1460, 1460)
229 def test_tcp_mss_clamping_ip6_tx(self):
230 """IP6 TCP MSS Clamping TX"""
232 # enable the TCP MSS clamping feature to lower the MSS to 1424.
233 self.vapi.mss_clamp_enable_disable(
234 self.pg1.sw_if_index,
241 # Verify that the feature is enabled.
242 rv, reply = self.vapi.mss_clamp_get(sw_if_index=self.pg1.sw_if_index)
243 self.assertEqual(reply[0].ipv6_mss, 1424)
244 self.assertEqual(reply[0].ipv6_direction, 3)
246 # Send syn packets and verify that the MSS value is lowered.
247 self.send_and_verify_ip6(self.pg0, self.pg1, 1460, 1424)
250 stats = self.statistics.get_counter("/err/tcp-mss-clamping-ip6-out/clamped")
251 self.assertEqual(sum(stats), 65)
253 # Send syn packets with small enough MSS values and verify they are
255 self.send_and_verify_ip6(self.pg0, self.pg1, 1400, 1400)
257 # enable the the feature only in TX direction
258 # and change the max MSS value
259 self.vapi.mss_clamp_enable_disable(
260 self.pg1.sw_if_index,
267 # Send syn packets and verify that the MSS value is lowered.
268 self.send_and_verify_ip6(self.pg0, self.pg1, 1460, 1420)
270 # enable the the feature only in RX direction
271 self.vapi.mss_clamp_enable_disable(
272 self.pg1.sw_if_index,
279 # Send the packets again and ensure they are unchanged.
280 self.send_and_verify_ip6(self.pg0, self.pg1, 1460, 1460)
282 # disable the feature
283 self.vapi.mss_clamp_enable_disable(
284 self.pg1.sw_if_index,
291 # Send the packets again and ensure they are unchanged.
292 self.send_and_verify_ip6(self.pg0, self.pg1, 1460, 1460)
294 def test_tcp_mss_clamping_ip6_rx(self):
295 """IP6 TCP MSS Clamping RX"""
297 # enable the TCP MSS clamping feature to lower the MSS to 1424.
298 self.vapi.mss_clamp_enable_disable(
299 self.pg1.sw_if_index,
306 # Verify that the feature is enabled.
307 rv, reply = self.vapi.mss_clamp_get(sw_if_index=self.pg1.sw_if_index)
308 self.assertEqual(reply[0].ipv6_mss, 1424)
309 self.assertEqual(reply[0].ipv6_direction, 3)
311 # Send syn packets and verify that the MSS value is lowered.
312 self.send_and_verify_ip6(self.pg1, self.pg0, 1460, 1424)
315 stats = self.statistics.get_counter("/err/tcp-mss-clamping-ip6-in/clamped")
316 self.assertEqual(sum(stats), 65)
318 # Send syn packets with small enough MSS values and verify they are
320 self.send_and_verify_ip6(self.pg1, self.pg0, 1400, 1400)
322 # enable the the feature only in RX direction
323 # and change the max MSS value
324 self.vapi.mss_clamp_enable_disable(
325 self.pg1.sw_if_index,
332 # Send syn packets and verify that the MSS value is lowered.
333 self.send_and_verify_ip6(self.pg1, self.pg0, 1460, 1420)
335 # enable the the feature only in TX direction
336 self.vapi.mss_clamp_enable_disable(
337 self.pg1.sw_if_index,
344 # Send the packets again and ensure they are unchanged.
345 self.send_and_verify_ip6(self.pg1, self.pg0, 1460, 1460)
347 # disable the feature
348 self.vapi.mss_clamp_enable_disable(
349 self.pg1.sw_if_index,
356 # Send the packets again and ensure they are unchanged.
357 self.send_and_verify_ip6(self.pg1, self.pg0, 1460, 1460)
360 if __name__ == "__main__":
361 unittest.main(testRunner=VppTestRunner)