Steven Luong | c4b5d10 | 2024-07-30 13:44:01 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import unittest |
| 4 | |
| 5 | from framework import VppTestCase |
| 6 | from asfframework import VppTestRunner, tag_fixme_vpp_workers |
| 7 | from ipaddress import IPv4Network, IPv6Network |
| 8 | |
| 9 | from vpp_ip_route import ( |
| 10 | VppIpRoute, |
| 11 | VppRoutePath, |
| 12 | VppIpTable, |
| 13 | ) |
| 14 | |
| 15 | from vpp_papi import VppEnum |
| 16 | |
| 17 | |
| 18 | from vpp_session_sdl import VppSessionSdl |
| 19 | from vpp_session_sdl import SessionSdl |
| 20 | |
| 21 | |
| 22 | @tag_fixme_vpp_workers |
| 23 | class TestSessionSDL(VppTestCase): |
| 24 | """Session SDL Test Case""" |
| 25 | |
| 26 | @classmethod |
| 27 | def setUpClass(cls): |
| 28 | super(TestSessionSDL, cls).setUpClass() |
| 29 | |
| 30 | @classmethod |
| 31 | def tearDownClass(cls): |
| 32 | super(TestSessionSDL, cls).tearDownClass() |
| 33 | |
| 34 | def setUp(self): |
| 35 | super(TestSessionSDL, self).setUp() |
| 36 | self.create_loopback_interfaces(2) |
| 37 | |
| 38 | table_id = 0 |
| 39 | |
| 40 | for i in self.lo_interfaces: |
| 41 | i.admin_up() |
| 42 | |
| 43 | if table_id != 0: |
| 44 | tbl = VppIpTable(self, table_id) |
| 45 | tbl.add_vpp_config() |
| 46 | tbl = VppIpTable(self, table_id, is_ip6=1) |
| 47 | tbl.add_vpp_config() |
| 48 | |
| 49 | i.set_table_ip4(table_id) |
| 50 | i.set_table_ip6(table_id) |
| 51 | i.config_ip4() |
| 52 | i.config_ip6() |
| 53 | table_id += 1 |
| 54 | |
| 55 | def tearDown(self): |
| 56 | for i in self.lo_interfaces: |
| 57 | i.unconfig_ip4() |
| 58 | i.set_table_ip4(0) |
| 59 | i.unconfig_ip6() |
| 60 | i.set_table_ip6(0) |
| 61 | i.admin_down() |
| 62 | self.loop0.remove_vpp_config() |
| 63 | self.loop1.remove_vpp_config() |
| 64 | super(TestSessionSDL, self).tearDown() |
| 65 | |
| 66 | def create_rule(self, lcl, action_index, tag): |
| 67 | return SessionSdl(lcl=lcl, action_index=action_index, tag=tag) |
| 68 | |
| 69 | def apply_rules(self, rules, is_add, appns_index): |
| 70 | r = VppSessionSdl(self, rules, is_add=is_add, appns_index=appns_index) |
| 71 | r.add_vpp_config() |
| 72 | |
| 73 | def test_session_sdl_ip4(self): |
| 74 | """Session SDL IP4 test""" |
| 75 | |
| 76 | self.vapi.session_enable_disable_v2( |
| 77 | rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_SDL |
| 78 | ) |
| 79 | |
| 80 | # Configure namespaces |
| 81 | self.vapi.app_namespace_add_del_v4( |
| 82 | namespace_id="0", sw_if_index=self.loop0.sw_if_index |
| 83 | ) |
| 84 | self.vapi.app_namespace_add_del_v4( |
| 85 | namespace_id="1", sw_if_index=self.loop1.sw_if_index |
| 86 | ) |
| 87 | |
| 88 | # Add inter-table routes |
| 89 | uri = "tcp://" + self.loop0.local_ip4 + "/1234" |
| 90 | server_cmd = "test echo server appns 0 fifo-size 4k " + "uri " + uri |
| 91 | client_cmd = ( |
| 92 | "test echo client bytes 100000 appns 1 " |
| 93 | + "fifo-size 4k " |
| 94 | + "syn-timeout 2 uri " |
| 95 | + uri |
| 96 | ) |
| 97 | ip_t01 = VppIpRoute( |
| 98 | self, |
| 99 | self.loop1.local_ip4, |
| 100 | 32, |
| 101 | [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=1)], |
| 102 | ) |
| 103 | ip_t10 = VppIpRoute( |
| 104 | self, |
| 105 | self.loop0.local_ip4, |
| 106 | 32, |
| 107 | [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=0)], |
| 108 | table_id=1, |
| 109 | ) |
| 110 | ip_t01.add_vpp_config() |
| 111 | ip_t10.add_vpp_config() |
| 112 | |
| 113 | # Start builtin server for ip4 |
| 114 | self.logger.info(self.vapi.cli(server_cmd)) |
| 115 | |
| 116 | # Add session filter to block loop0 |
| 117 | rules = [] |
| 118 | rules.append( |
| 119 | self.create_rule(lcl=self.loop0.local_ip4 + "/32", action_index=0, tag="") |
| 120 | ) |
| 121 | self.apply_rules(rules, is_add=1, appns_index=0) |
| 122 | |
| 123 | filter = self.vapi.session_sdl_dump() |
| 124 | self.assertEqual(filter[0].lcl, IPv4Network(self.loop0.local_ip4 + "/32")) |
| 125 | |
| 126 | # irrelevant rules - add 64k entries in one API call |
| 127 | rules = [] |
| 128 | for i in range(255): |
| 129 | for j in range(255): |
| 130 | prefix = "10.1.{0}.{1}/32".format(i, j) |
| 131 | rules.append(self.create_rule(lcl=prefix, action_index=0, tag="")) |
| 132 | self.apply_rules(rules, is_add=1, appns_index=0) |
| 133 | |
| 134 | error = self.vapi.cli_return_response(server_cmd) |
| 135 | # Expecting an error because loop0 is blocked |
| 136 | self.assertEqual(-1, error.retval) |
| 137 | |
| 138 | # Remove the session filter |
| 139 | rules = [] |
| 140 | rules.append( |
| 141 | self.create_rule(lcl=self.loop0.local_ip4 + "/32", action_index=0, tag="") |
| 142 | ) |
| 143 | self.apply_rules(rules, is_add=0, appns_index=0) |
| 144 | |
| 145 | # Not expecting an error |
| 146 | self.logger.info(self.vapi.cli(client_cmd)) |
| 147 | |
| 148 | # Add a session filter not matching loop0 |
| 149 | rules = [] |
| 150 | rules.append(self.create_rule(lcl="172.100.1.0/24", action_index=0, tag="")) |
| 151 | self.apply_rules(rules, is_add=1, appns_index=0) |
| 152 | |
| 153 | # Not expecting an error |
| 154 | self.logger.info(self.vapi.cli(client_cmd)) |
| 155 | |
| 156 | self.logger.info(self.vapi.cli(server_cmd + " stop")) |
| 157 | |
| 158 | self.vapi.app_namespace_add_del_v4( |
| 159 | is_add=0, namespace_id="0", sw_if_index=self.loop0.sw_if_index |
| 160 | ) |
| 161 | self.vapi.app_namespace_add_del_v4( |
| 162 | is_add=0, namespace_id="1", sw_if_index=self.loop1.sw_if_index |
| 163 | ) |
| 164 | # Delete inter-table routes |
| 165 | ip_t01.remove_vpp_config() |
| 166 | ip_t10.remove_vpp_config() |
| 167 | self.vapi.session_enable_disable_v2( |
| 168 | rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_DISABLE |
| 169 | ) |
| 170 | |
| 171 | def test_session_sdl_ip6(self): |
| 172 | """Session SDL IP6 test""" |
| 173 | |
| 174 | self.vapi.session_enable_disable_v2( |
| 175 | rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_SDL |
| 176 | ) |
| 177 | |
| 178 | # Configure namespaces |
| 179 | self.vapi.app_namespace_add_del_v4( |
| 180 | namespace_id="0", sw_if_index=self.loop0.sw_if_index |
| 181 | ) |
| 182 | self.vapi.app_namespace_add_del_v4( |
| 183 | namespace_id="1", sw_if_index=self.loop1.sw_if_index |
| 184 | ) |
| 185 | |
| 186 | # IP6 Test |
| 187 | # Add inter-table routes |
| 188 | uri = "tcp://" + self.loop0.local_ip6 + "/1235" |
| 189 | client_cmd = ( |
| 190 | "test echo client bytes 100000 appns 1 " |
| 191 | + "fifo-size 4k " |
| 192 | + "syn-timeout 2 uri " |
| 193 | + uri |
| 194 | ) |
| 195 | server_cmd = "test echo server appns 0 fifo-size 4k " + "uri " + uri |
| 196 | |
| 197 | ip_t01 = VppIpRoute( |
| 198 | self, |
| 199 | self.loop1.local_ip6, |
| 200 | 128, |
| 201 | [VppRoutePath("0::0", 0xFFFFFFFF, nh_table_id=1)], |
| 202 | ) |
| 203 | ip_t10 = VppIpRoute( |
| 204 | self, |
| 205 | self.loop0.local_ip6, |
| 206 | 128, |
| 207 | [VppRoutePath("0::0", 0xFFFFFFFF, nh_table_id=0)], |
| 208 | table_id=1, |
| 209 | ) |
| 210 | ip_t01.add_vpp_config() |
| 211 | ip_t10.add_vpp_config() |
| 212 | |
| 213 | # Start builtin server for ip6 |
| 214 | self.logger.info(self.vapi.cli(server_cmd)) |
| 215 | |
| 216 | # case 1: No filter |
| 217 | |
| 218 | # Not expecting an error |
| 219 | self.logger.info(self.vapi.cli(client_cmd)) |
| 220 | |
| 221 | # case 2: filter to block |
| 222 | # Add session filter to block loop0 |
| 223 | rules = [] |
| 224 | rules.append( |
| 225 | self.create_rule(lcl=self.loop0.local_ip6 + "/128", action_index=0, tag="") |
| 226 | ) |
| 227 | self.apply_rules(rules, is_add=1, appns_index=0) |
| 228 | filter = self.vapi.session_sdl_dump() |
| 229 | self.assertEqual(filter[0].lcl, IPv6Network(self.loop0.local_ip6 + "/128")) |
| 230 | |
| 231 | error = self.vapi.cli_return_response(client_cmd) |
| 232 | # Expecting an error because loop0 is blocked |
| 233 | self.assertEqual(-1, error.retval) |
| 234 | |
| 235 | # case 3: remove to block |
| 236 | # Add session filter to block loop0 |
| 237 | rules = [] |
| 238 | rules.append( |
| 239 | self.create_rule(lcl=self.loop0.local_ip6 + "/128", action_index=0, tag="") |
| 240 | ) |
| 241 | self.apply_rules(rules, is_add=0, appns_index=0) |
| 242 | # Not expecting an error |
| 243 | self.logger.info(self.vapi.cli(client_cmd)) |
| 244 | |
| 245 | # stop the server |
| 246 | self.logger.info(self.vapi.cli(server_cmd + " stop")) |
| 247 | |
| 248 | self.vapi.app_namespace_add_del_v4( |
| 249 | is_add=0, namespace_id="0", sw_if_index=self.loop0.sw_if_index |
| 250 | ) |
| 251 | self.vapi.app_namespace_add_del_v4( |
| 252 | is_add=0, namespace_id="1", sw_if_index=self.loop1.sw_if_index |
| 253 | ) |
| 254 | # Delete inter-table routes |
| 255 | ip_t01.remove_vpp_config() |
| 256 | ip_t10.remove_vpp_config() |
| 257 | self.vapi.session_enable_disable_v2( |
| 258 | rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_DISABLE |
| 259 | ) |
| 260 | |
| 261 | def test_session_enable_disable(self): |
| 262 | """Session SDL enable/disable test""" |
| 263 | |
| 264 | for i in range(10): |
| 265 | # Enable sdl |
| 266 | self.vapi.session_enable_disable_v2( |
| 267 | rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_SDL |
| 268 | ) |
| 269 | |
| 270 | # Disable |
| 271 | self.vapi.session_enable_disable_v2( |
| 272 | rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_DISABLE |
| 273 | ) |
| 274 | |
| 275 | # Enable rule-table |
| 276 | self.vapi.session_enable_disable_v2( |
| 277 | rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_RULE_TABLE |
| 278 | ) |
| 279 | |
| 280 | # Disable |
| 281 | self.vapi.session_enable_disable_v2( |
| 282 | rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_DISABLE |
| 283 | ) |
| 284 | |
| 285 | # Enable sdl |
| 286 | self.vapi.session_enable_disable_v2( |
| 287 | rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_SDL |
| 288 | ) |
| 289 | |
| 290 | # Disable |
| 291 | self.vapi.session_enable_disable_v2( |
| 292 | rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_DISABLE |
| 293 | ) |
| 294 | |
| 295 | |
| 296 | if __name__ == "__main__": |
| 297 | unittest.main(testRunner=VppTestRunner) |