Rich Lane | 284dc4d | 2014-01-06 15:24:07 -0800 | [diff] [blame] | 1 | # Distributed under the OpenFlow Software License (see LICENSE) |
| 2 | # Copyright (c) 2012, 2013 Big Switch Networks, Inc. |
| 3 | """ |
| 4 | BSN gentable extension test cases |
| 5 | """ |
| 6 | |
| 7 | import logging |
Rich Lane | 232d2ab | 2014-01-07 12:23:16 -0800 | [diff] [blame] | 8 | import math |
Rich Lane | 284dc4d | 2014-01-06 15:24:07 -0800 | [diff] [blame] | 9 | |
| 10 | from oftest import config |
| 11 | import oftest.base_tests as base_tests |
| 12 | import ofp |
| 13 | |
| 14 | from oftest.testutils import * |
| 15 | |
| 16 | # Hardcoded in the switch to ease testing |
| 17 | TABLE_ID = 0 |
| 18 | |
Rich Lane | b2c5bf6 | 2014-01-06 17:18:40 -0800 | [diff] [blame] | 19 | def tlv_dict(tlvs): |
| 20 | d = {} |
| 21 | for tlv in tlvs: |
| 22 | d[tlv.__class__] = tlv.value |
| 23 | return d |
| 24 | |
Rich Lane | 64c4e60 | 2014-01-07 11:27:06 -0800 | [diff] [blame] | 25 | def make_checksum(hi, lo): |
| 26 | """ |
| 27 | Place 'hi' in the upper 8 bits and 'lo' in the lower bits. |
| 28 | """ |
| 29 | return ((hi & 0xff) << 120) | lo |
| 30 | |
| 31 | assert make_checksum(0xab, 0xcd) == 0xab0000000000000000000000000000cd |
| 32 | |
Rich Lane | 284dc4d | 2014-01-06 15:24:07 -0800 | [diff] [blame] | 33 | class BaseGenTableTest(base_tests.SimpleProtocol): |
| 34 | def setUp(self): |
| 35 | base_tests.SimpleProtocol.setUp(self) |
Rich Lane | b2c5bf6 | 2014-01-06 17:18:40 -0800 | [diff] [blame] | 36 | self.do_clear() |
Rich Lane | 284dc4d | 2014-01-06 15:24:07 -0800 | [diff] [blame] | 37 | |
| 38 | def tearDown(self): |
Rich Lane | b2c5bf6 | 2014-01-06 17:18:40 -0800 | [diff] [blame] | 39 | self.do_clear() |
Rich Lane | 284dc4d | 2014-01-06 15:24:07 -0800 | [diff] [blame] | 40 | base_tests.SimpleProtocol.tearDown(self) |
| 41 | |
Rich Lane | b2c5bf6 | 2014-01-06 17:18:40 -0800 | [diff] [blame] | 42 | def do_clear(self, checksum=0, checksum_mask=0): |
| 43 | request = ofp.message.bsn_gentable_clear_request( |
| 44 | table_id=TABLE_ID, |
| 45 | checksum=0, |
| 46 | checksum_mask=0) |
Rich Lane | 284dc4d | 2014-01-06 15:24:07 -0800 | [diff] [blame] | 47 | response, _ = self.controller.transact(request) |
| 48 | self.assertIsInstance(response, ofp.message.bsn_gentable_clear_reply) |
| 49 | self.assertEquals(response.error_count, 0) |
| 50 | |
Rich Lane | b2c5bf6 | 2014-01-06 17:18:40 -0800 | [diff] [blame] | 51 | def do_add(self, vlan_vid, ipv4, mac, idle_notification=False, checksum=0): |
Rich Lane | 284dc4d | 2014-01-06 15:24:07 -0800 | [diff] [blame] | 52 | msg = ofp.message.bsn_gentable_entry_add( |
| 53 | table_id=TABLE_ID, |
| 54 | key=[ |
| 55 | ofp.bsn_tlv.vlan_vid(vlan_vid), |
| 56 | ofp.bsn_tlv.ipv4(ipv4)], |
| 57 | value=[ |
| 58 | ofp.bsn_tlv.mac(mac)], |
Rich Lane | b2c5bf6 | 2014-01-06 17:18:40 -0800 | [diff] [blame] | 59 | checksum=checksum) |
Rich Lane | 284dc4d | 2014-01-06 15:24:07 -0800 | [diff] [blame] | 60 | if idle_notification: |
| 61 | msg.value.append(ofp.bsn_tlv.idle_notification()) |
| 62 | self.controller.message_send(msg) |
| 63 | |
| 64 | def do_delete(self, vlan_vid, ipv4): |
| 65 | msg = ofp.message.bsn_gentable_entry_delete( |
| 66 | table_id=TABLE_ID, |
| 67 | key=[ |
| 68 | ofp.bsn_tlv.vlan_vid(vlan_vid), |
| 69 | ofp.bsn_tlv.ipv4(ipv4)]) |
| 70 | self.controller.message_send(msg) |
| 71 | |
Rich Lane | b2c5bf6 | 2014-01-06 17:18:40 -0800 | [diff] [blame] | 72 | def do_entry_stats(self, checksum=0, checksum_mask=0): |
| 73 | request = ofp.message.bsn_gentable_entry_stats_request( |
| 74 | table_id=TABLE_ID, |
| 75 | checksum=checksum, |
| 76 | checksum_mask=checksum_mask) |
| 77 | return get_stats(self, request) |
| 78 | |
| 79 | def do_entry_desc_stats(self, checksum=0, checksum_mask=0): |
| 80 | request = ofp.message.bsn_gentable_entry_desc_stats_request( |
| 81 | table_id=TABLE_ID, |
| 82 | checksum=checksum, |
| 83 | checksum_mask=checksum_mask) |
| 84 | return get_stats(self, request) |
| 85 | |
Rich Lane | 4e691ad | 2014-01-06 17:45:20 -0800 | [diff] [blame] | 86 | def do_table_desc_stats(self): |
| 87 | request = ofp.message.bsn_gentable_desc_stats_request() |
| 88 | return get_stats(self, request) |
| 89 | |
Rich Lane | a8f5667 | 2014-01-06 17:50:39 -0800 | [diff] [blame] | 90 | def do_table_stats(self): |
| 91 | request = ofp.message.bsn_gentable_stats_request() |
| 92 | return get_stats(self, request) |
| 93 | |
Rich Lane | 64c4e60 | 2014-01-07 11:27:06 -0800 | [diff] [blame] | 94 | def do_test_table_stats(self): |
| 95 | entries = self.do_table_stats() |
| 96 | for entry in entries: |
| 97 | if entry.table_id == TABLE_ID: |
| 98 | return entry |
| 99 | raise AssertionError("did not find test table") |
| 100 | |
Rich Lane | 232d2ab | 2014-01-07 12:23:16 -0800 | [diff] [blame] | 101 | def do_bucket_stats(self): |
| 102 | request = ofp.message.bsn_gentable_bucket_stats_request(table_id=TABLE_ID) |
| 103 | return get_stats(self, request) |
| 104 | |
Rich Lane | 78bd377 | 2014-01-08 11:51:13 -0800 | [diff] [blame^] | 105 | def do_set_buckets_size(self, buckets_size): |
| 106 | msg = ofp.message.bsn_gentable_set_buckets_size( |
| 107 | table_id=TABLE_ID, |
| 108 | buckets_size=buckets_size) |
| 109 | self.controller.message_send(msg) |
| 110 | |
Rich Lane | 284dc4d | 2014-01-06 15:24:07 -0800 | [diff] [blame] | 111 | class ClearAll(BaseGenTableTest): |
| 112 | """ |
| 113 | Test clearing entire table |
| 114 | """ |
| 115 | def runTest(self): |
| 116 | # Add a few entries |
| 117 | for i in range(0, 3): |
| 118 | self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i)) |
| 119 | |
| 120 | do_barrier(self.controller) |
| 121 | verify_no_errors(self.controller) |
| 122 | |
| 123 | # Delete all entries |
| 124 | request = ofp.message.bsn_gentable_clear_request(table_id=TABLE_ID) |
| 125 | response, _ = self.controller.transact(request) |
| 126 | self.assertIsInstance(response, ofp.message.bsn_gentable_clear_reply) |
| 127 | self.assertEquals(response.error_count, 0) |
| 128 | self.assertEquals(response.deleted_count, 3) |
| 129 | |
| 130 | class AddDelete(BaseGenTableTest): |
| 131 | """ |
| 132 | Test adding and deleting entries |
| 133 | """ |
| 134 | def runTest(self): |
| 135 | # Add a few entries |
| 136 | for i in range(0, 3): |
| 137 | self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i)) |
| 138 | |
| 139 | do_barrier(self.controller) |
| 140 | verify_no_errors(self.controller) |
| 141 | |
| 142 | # Delete each entry |
| 143 | for i in range(0, 3): |
| 144 | self.do_delete(vlan_vid=i, ipv4=0x12345678) |
| 145 | |
| 146 | do_barrier(self.controller) |
| 147 | verify_no_errors(self.controller) |
| 148 | |
| 149 | # Clear table, but expect it to have already been empty |
| 150 | request = ofp.message.bsn_gentable_clear_request(table_id=TABLE_ID) |
| 151 | response, _ = self.controller.transact(request) |
| 152 | self.assertIsInstance(response, ofp.message.bsn_gentable_clear_reply) |
| 153 | self.assertEquals(response.error_count, 0) |
| 154 | self.assertEquals(response.deleted_count, 0) |
Rich Lane | b2c5bf6 | 2014-01-06 17:18:40 -0800 | [diff] [blame] | 155 | |
| 156 | class EntryStats(BaseGenTableTest): |
| 157 | """ |
| 158 | Test retrieving entry stats |
| 159 | """ |
| 160 | def runTest(self): |
| 161 | # Add a few entries |
| 162 | for i in range(0, 3): |
| 163 | self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i)) |
| 164 | |
| 165 | do_barrier(self.controller) |
| 166 | verify_no_errors(self.controller) |
| 167 | |
| 168 | entries = self.do_entry_stats() |
| 169 | seen = set() |
| 170 | for entry in entries: |
| 171 | logging.debug(entry.show()) |
| 172 | key = tlv_dict(entry.key) |
| 173 | stats = tlv_dict(entry.stats) |
| 174 | self.assertIn(ofp.bsn_tlv.vlan_vid, key) |
| 175 | self.assertIn(ofp.bsn_tlv.ipv4, key) |
| 176 | self.assertIn(ofp.bsn_tlv.rx_packets, stats) |
| 177 | self.assertIn(ofp.bsn_tlv.tx_packets, stats) |
| 178 | vlan_vid = key[ofp.bsn_tlv.vlan_vid] |
| 179 | seen.add(vlan_vid) |
| 180 | self.assertEqual(key[ofp.bsn_tlv.ipv4], 0x12345678) |
| 181 | self.assertEqual(stats[ofp.bsn_tlv.rx_packets], 100 * vlan_vid) |
| 182 | self.assertEqual(stats[ofp.bsn_tlv.tx_packets], 101 * vlan_vid) |
| 183 | |
| 184 | self.assertEquals(seen, set([0, 1, 2])) |
| 185 | |
| 186 | class EntryDescStats(BaseGenTableTest): |
| 187 | """ |
| 188 | Test retrieving entry desc stats |
| 189 | """ |
| 190 | def runTest(self): |
| 191 | # Add a few entries |
| 192 | for i in range(0, 3): |
| 193 | self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i), |
| 194 | checksum=0xfedcba9876543210fedcba9876543210 + i) |
| 195 | |
| 196 | do_barrier(self.controller) |
| 197 | verify_no_errors(self.controller) |
| 198 | |
| 199 | entries = self.do_entry_desc_stats() |
| 200 | seen = set() |
| 201 | for entry in entries: |
| 202 | logging.debug(entry.show()) |
| 203 | key = tlv_dict(entry.key) |
| 204 | value = tlv_dict(entry.value) |
| 205 | self.assertIn(ofp.bsn_tlv.vlan_vid, key) |
| 206 | self.assertIn(ofp.bsn_tlv.ipv4, key) |
| 207 | self.assertIn(ofp.bsn_tlv.mac, value) |
| 208 | vlan_vid = key[ofp.bsn_tlv.vlan_vid] |
| 209 | seen.add(vlan_vid) |
| 210 | self.assertEqual(key[ofp.bsn_tlv.ipv4], 0x12345678) |
| 211 | self.assertEqual(value[ofp.bsn_tlv.mac], [0, 1, 2, 3, 4, vlan_vid]) |
| 212 | self.assertEqual(entry.checksum, 0xfedcba9876543210fedcba9876543210 + vlan_vid) |
| 213 | |
| 214 | self.assertEquals(seen, set([0, 1, 2])) |
Rich Lane | 4e691ad | 2014-01-06 17:45:20 -0800 | [diff] [blame] | 215 | |
| 216 | class TableDescStats(BaseGenTableTest): |
| 217 | """ |
| 218 | Test retrieving table desc stats |
| 219 | """ |
| 220 | def runTest(self): |
| 221 | entries = self.do_table_desc_stats() |
| 222 | seen = set() |
| 223 | for entry in entries: |
| 224 | logging.debug(entry.show()) |
| 225 | self.assertNotIn(entry.table_id, seen) |
| 226 | self.assertNotIn(entry.name, seen) |
| 227 | seen.add(entry.table_id) |
| 228 | seen.add(entry.name) |
| 229 | if entry.table_id == TABLE_ID: |
| 230 | self.assertEqual(entry.name, "test") |
| 231 | self.assertEqual(entry.buckets_size, 64) |
| 232 | self.assertEqual(entry.max_entries, 1000) |
| 233 | |
| 234 | self.assertIn(TABLE_ID, seen) |
Rich Lane | a8f5667 | 2014-01-06 17:50:39 -0800 | [diff] [blame] | 235 | |
| 236 | class TableStats(BaseGenTableTest): |
| 237 | """ |
| 238 | Test retrieving table stats |
| 239 | """ |
| 240 | def runTest(self): |
Rich Lane | 64c4e60 | 2014-01-07 11:27:06 -0800 | [diff] [blame] | 241 | # Verify we have the test table and no duplicates |
Rich Lane | a8f5667 | 2014-01-06 17:50:39 -0800 | [diff] [blame] | 242 | entries = self.do_table_stats() |
| 243 | seen = set() |
| 244 | for entry in entries: |
| 245 | logging.debug(entry.show()) |
| 246 | self.assertNotIn(entry.table_id, seen) |
| 247 | seen.add(entry.table_id) |
| 248 | if entry.table_id == TABLE_ID: |
| 249 | self.assertEqual(entry.entry_count, 0) |
| 250 | self.assertEqual(entry.checksum, 0) |
Rich Lane | a8f5667 | 2014-01-06 17:50:39 -0800 | [diff] [blame] | 251 | self.assertIn(TABLE_ID, seen) |
Rich Lane | 64c4e60 | 2014-01-07 11:27:06 -0800 | [diff] [blame] | 252 | |
| 253 | table_checksum = 0 |
| 254 | |
| 255 | # Add a bunch of entries, spread among the checksum buckets |
| 256 | for i in range(0, 256): |
| 257 | table_checksum ^= make_checksum(i, i*31) |
| 258 | self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i), |
| 259 | checksum=make_checksum(i, i*31)) |
| 260 | |
| 261 | do_barrier(self.controller) |
| 262 | verify_no_errors(self.controller) |
| 263 | |
| 264 | table_stats = self.do_test_table_stats() |
| 265 | self.assertEqual(table_stats.entry_count, 256) |
| 266 | self.assertEqual(table_stats.checksum, table_checksum) |
| 267 | |
| 268 | # Modify an entry, changing its checksum |
| 269 | i = 30 |
| 270 | table_checksum ^= make_checksum(i, i*31) # subtract old checksum |
| 271 | table_checksum ^= make_checksum(i, i*37) # add new checksum |
| 272 | self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 4, 3, 2, 1, i), |
| 273 | checksum=make_checksum(i, i*37)) |
| 274 | |
| 275 | do_barrier(self.controller) |
| 276 | verify_no_errors(self.controller) |
| 277 | |
| 278 | table_stats = self.do_test_table_stats() |
| 279 | self.assertEqual(table_stats.entry_count, 256) |
| 280 | self.assertEqual(table_stats.checksum, table_checksum) |
| 281 | |
| 282 | # Delete an entry |
| 283 | i = 87 |
| 284 | table_checksum ^= make_checksum(i, i*31) |
| 285 | self.do_delete(vlan_vid=i, ipv4=0x12345678) |
| 286 | |
| 287 | do_barrier(self.controller) |
| 288 | verify_no_errors(self.controller) |
| 289 | |
| 290 | table_stats = self.do_test_table_stats() |
| 291 | self.assertEqual(table_stats.entry_count, 255) |
| 292 | self.assertEqual(table_stats.checksum, table_checksum) |
Rich Lane | 232d2ab | 2014-01-07 12:23:16 -0800 | [diff] [blame] | 293 | |
| 294 | class BucketStats(BaseGenTableTest): |
| 295 | """ |
| 296 | Test retrieving checksum bucket stats |
| 297 | """ |
| 298 | def runTest(self): |
| 299 | # Verify initial state |
| 300 | entries = self.do_bucket_stats() |
| 301 | self.assertEquals(len(entries), 64) |
| 302 | for entry in entries: |
| 303 | self.assertEquals(entry.checksum, 0) |
| 304 | |
| 305 | buckets = [0] * len(entries) |
| 306 | checksum_bits = int(math.log(len(buckets), 2)) |
| 307 | |
| 308 | def update_bucket(checksum): |
| 309 | index = checksum >> (128 - checksum_bits) |
| 310 | buckets[index] ^= checksum |
| 311 | |
| 312 | # Add a bunch of entries, spread among the checksum buckets |
| 313 | for i in range(0, 256): |
| 314 | update_bucket(make_checksum(i, i*31)) |
| 315 | self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i), |
| 316 | checksum=make_checksum(i, i*31)) |
| 317 | |
| 318 | entries = self.do_bucket_stats() |
| 319 | self.assertEquals(len(entries), 64) |
| 320 | for i, entry in enumerate(entries): |
| 321 | self.assertEquals(entry.checksum, buckets[i]) |
| 322 | |
| 323 | # Modify an entry, changing its checksum |
| 324 | i = 30 |
| 325 | update_bucket(make_checksum(i, i*31)) # subtract old checksum |
| 326 | update_bucket(make_checksum(i, i*37)) # add new checksum |
| 327 | self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 4, 3, 2, 1, i), |
| 328 | checksum=make_checksum(i, i*37)) |
| 329 | |
| 330 | do_barrier(self.controller) |
| 331 | verify_no_errors(self.controller) |
| 332 | |
| 333 | entries = self.do_bucket_stats() |
| 334 | self.assertEquals(len(entries), 64) |
| 335 | for i, entry in enumerate(entries): |
| 336 | self.assertEquals(entry.checksum, buckets[i]) |
| 337 | |
| 338 | # Delete an entry |
| 339 | i = 87 |
| 340 | update_bucket(make_checksum(i, i*31)) |
| 341 | self.do_delete(vlan_vid=i, ipv4=0x12345678) |
| 342 | |
| 343 | do_barrier(self.controller) |
| 344 | verify_no_errors(self.controller) |
| 345 | |
| 346 | entries = self.do_bucket_stats() |
| 347 | self.assertEquals(len(entries), 64) |
| 348 | for i, entry in enumerate(entries): |
| 349 | self.assertEquals(entry.checksum, buckets[i]) |
Rich Lane | 78bd377 | 2014-01-08 11:51:13 -0800 | [diff] [blame^] | 350 | |
| 351 | class SetBucketsSize(BaseGenTableTest): |
| 352 | """ |
| 353 | Test setting the checksum buckets size |
| 354 | """ |
| 355 | def setUp(self): |
| 356 | BaseGenTableTest.setUp(self) |
| 357 | self.do_set_buckets_size(64) |
| 358 | do_barrier(self.controller) |
| 359 | |
| 360 | def tearDown(self): |
| 361 | self.do_set_buckets_size(64) |
| 362 | do_barrier(self.controller) |
| 363 | BaseGenTableTest.tearDown(self) |
| 364 | |
| 365 | def runTest(self): |
| 366 | # Verify initial state |
| 367 | entries = self.do_bucket_stats() |
| 368 | self.assertEquals(len(entries), 64) |
| 369 | for entry in entries: |
| 370 | self.assertEquals(entry.checksum, 0) |
| 371 | |
| 372 | buckets32 = [0] * 32 |
| 373 | buckets64 = [0] * 64 |
| 374 | |
| 375 | def update_bucket(checksum): |
| 376 | buckets32[checksum >> (128 - int(math.log(32, 2)))] ^= checksum |
| 377 | buckets64[checksum >> (128 - int(math.log(64, 2)))] ^= checksum |
| 378 | |
| 379 | # Add a bunch of entries, spread among the checksum buckets |
| 380 | for i in range(0, 256): |
| 381 | update_bucket(make_checksum(i, i*31)) |
| 382 | self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i), |
| 383 | checksum=make_checksum(i, i*31)) |
| 384 | |
| 385 | entries = self.do_bucket_stats() |
| 386 | self.assertEquals(len(entries), 64) |
| 387 | for i, entry in enumerate(entries): |
| 388 | self.assertEquals(entry.checksum, buckets64[i]) |
| 389 | |
| 390 | self.do_set_buckets_size(32) |
| 391 | do_barrier(self.controller) |
| 392 | |
| 393 | entries = self.do_bucket_stats() |
| 394 | self.assertEquals(len(entries), 32) |
| 395 | for i, entry in enumerate(entries): |
| 396 | self.assertEquals(entry.checksum, buckets32[i]) |
| 397 | |
| 398 | self.do_set_buckets_size(64) |
| 399 | do_barrier(self.controller) |
| 400 | |
| 401 | entries = self.do_bucket_stats() |
| 402 | self.assertEquals(len(entries), 64) |
| 403 | for i, entry in enumerate(entries): |
| 404 | self.assertEquals(entry.checksum, buckets64[i]) |