Merge into master from pull request #94:
gentable tests (https://github.com/floodlight/oftest/pull/94)
diff --git a/src/python/oftest/controller.py b/src/python/oftest/controller.py
index 56fd9fb..0272b70 100644
--- a/src/python/oftest/controller.py
+++ b/src/python/oftest/controller.py
@@ -231,10 +231,6 @@
 
             # Check that supported version on switch is as least as recent as
             # the configured Openflow version in oftests
-            self.logger.debug("Msg in: version %d type %s (%d) len %d xid %d",
-                              hdr_version,
-                              cfg_ofp.ofp_type_map.get(hdr_type, "unknown"), hdr_type,
-                              hdr_length, hdr_version)
             if hdr_version < cfg_ofp.OFP_VERSION:
                 self.logger.error("Switch only supports up to OpenFlow version %d (OFTest version is %d)",
                                   hdr_version, cfg_ofp.OFP_VERSION)
@@ -249,6 +245,9 @@
                 self.logger.warn("Could not parse message")
                 continue
 
+            self.logger.debug("Msg in: version %d class %s len %d xid %d",
+                              hdr_version, type(msg).__name__, hdr_length, hdr_xid)
+
             with self.sync:
                 # Check if transaction is waiting
                 with self.xid_cv:
@@ -266,7 +265,7 @@
                         rep = ofp.message.echo_reply()
                         rep.xid = hdr_xid
                         # Ignoring additional data
-                        self.message_send(rep.pack())
+                        self.message_send(rep)
                         continue
 
                 # Generalize to counters for all packet types?
@@ -661,7 +660,7 @@
 
             self.xid = msg.xid
             self.xid_response = None
-            self.message_send(msg.pack())
+            self.message_send(msg)
 
             self.logger.debug("Waiting for transaction %d" % msg.xid)
             ofutils.timed_wait(self.xid_cv, lambda: self.xid_response, timeout=timeout)
@@ -687,21 +686,14 @@
         if not self.switch_socket:
             # Sending a string indicates the message is ready to go
             raise Exception("no socket")
-        #@todo If not string, try to pack
-        if type(msg) != type(""):
-            if msg.xid == None:
-                msg.xid = ofutils.gen_xid()
-            outpkt = msg.pack()
-        else:
-            outpkt = msg
 
-        msg_version, msg_type, msg_len, msg_xid = struct.unpack_from("!BBHL", outpkt)
-        self.logger.debug("Msg out: buf len %d. hdr.type %s. hdr.len %d hdr.version %d hdr.xid %d",
-                          len(outpkt),
-                          cfg_ofp.ofp_type_map.get(msg_type, "unknown (%d)" % msg_type),
-                          msg_len,
-                          msg_version,
-                          msg_xid)
+        if msg.xid == None:
+            msg.xid = ofutils.gen_xid()
+
+        outpkt = msg.pack()
+
+        self.logger.debug("Msg out: version %d class %s len %d xid %d",
+                          msg.version, type(msg).__name__, len(outpkt), msg.xid)
 
         with self.tx_lock:
             if self.switch_socket.sendall(outpkt) is not None:
diff --git a/tests-1.3/bsn_gentable.py b/tests-1.3/bsn_gentable.py
new file mode 100644
index 0000000..e9f3145
--- /dev/null
+++ b/tests-1.3/bsn_gentable.py
@@ -0,0 +1,779 @@
+# Distributed under the OpenFlow Software License (see LICENSE)
+# Copyright (c) 2012, 2013 Big Switch Networks, Inc.
+"""
+BSN gentable extension test cases
+"""
+
+import logging
+import math
+import random
+
+from oftest import config
+import oftest.base_tests as base_tests
+import ofp
+
+from oftest.testutils import *
+
+# Hardcoded in the switch to ease testing
+TABLE_ID = 0
+
+def tlv_dict(tlvs):
+    d = {}
+    for tlv in tlvs:
+        d[tlv.__class__] = tlv.value
+    return d
+
+def make_checksum(hi, lo):
+    """
+    Place 'hi' in the upper 8 bits and 'lo' in the lower bits.
+    """
+    return ((hi & 0xff) << 120) | lo
+
+assert make_checksum(0xab, 0xcd) == 0xab0000000000000000000000000000cd
+
+class BaseGenTableTest(base_tests.SimpleProtocol):
+    def setUp(self):
+        base_tests.SimpleProtocol.setUp(self)
+        self.do_clear()
+
+    def tearDown(self):
+        self.do_clear()
+        base_tests.SimpleProtocol.tearDown(self)
+
+    def do_clear(self, checksum=0, checksum_mask=0):
+        request = ofp.message.bsn_gentable_clear_request(
+            table_id=TABLE_ID,
+            checksum=checksum,
+            checksum_mask=checksum_mask)
+        response, _ = self.controller.transact(request)
+        self.assertIsInstance(response, ofp.message.bsn_gentable_clear_reply)
+        return response.deleted_count, response.error_count
+
+    def do_add(self, vlan_vid, ipv4, mac, idle_notification=False, checksum=0):
+        msg = ofp.message.bsn_gentable_entry_add(
+            table_id=TABLE_ID,
+            key=[
+                ofp.bsn_tlv.vlan_vid(vlan_vid),
+                ofp.bsn_tlv.ipv4(ipv4)],
+            value=[
+                ofp.bsn_tlv.mac(mac)],
+            checksum=checksum)
+        if idle_notification:
+            msg.value.append(ofp.bsn_tlv.idle_notification())
+        self.controller.message_send(msg)
+
+    def do_delete(self, vlan_vid, ipv4):
+        msg = ofp.message.bsn_gentable_entry_delete(
+            table_id=TABLE_ID,
+            key=[
+                ofp.bsn_tlv.vlan_vid(vlan_vid),
+                ofp.bsn_tlv.ipv4(ipv4)])
+        self.controller.message_send(msg)
+
+    def do_entry_stats(self, checksum=0, checksum_mask=0):
+        request = ofp.message.bsn_gentable_entry_stats_request(
+            table_id=TABLE_ID,
+            checksum=checksum,
+            checksum_mask=checksum_mask)
+        return get_stats(self, request)
+
+    def do_entry_desc_stats(self, checksum=0, checksum_mask=0):
+        request = ofp.message.bsn_gentable_entry_desc_stats_request(
+            table_id=TABLE_ID,
+            checksum=checksum,
+            checksum_mask=checksum_mask)
+        return get_stats(self, request)
+
+    def do_table_desc_stats(self):
+        request = ofp.message.bsn_gentable_desc_stats_request()
+        return get_stats(self, request)
+
+    def do_table_stats(self):
+        request = ofp.message.bsn_gentable_stats_request()
+        return get_stats(self, request)
+
+    def do_test_table_stats(self):
+        entries = self.do_table_stats()
+        for entry in entries:
+            if entry.table_id == TABLE_ID:
+                return entry
+        raise AssertionError("did not find test table")
+
+    def do_bucket_stats(self):
+        request = ofp.message.bsn_gentable_bucket_stats_request(table_id=TABLE_ID)
+        return get_stats(self, request)
+
+    def do_set_buckets_size(self, buckets_size):
+        msg = ofp.message.bsn_gentable_set_buckets_size(
+            table_id=TABLE_ID,
+            buckets_size=buckets_size)
+        self.controller.message_send(msg)
+
+class ClearAll(BaseGenTableTest):
+    """
+    Test clearing entire table
+    """
+    def runTest(self):
+        # Add a few entries
+        for i in range(0, 3):
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        # Delete all entries
+        deleted, errors = self.do_clear(checksum=0, checksum_mask=0)
+        self.assertEquals(deleted, 3)
+        self.assertEquals(errors, 0)
+
+class ClearMasked(BaseGenTableTest):
+    """
+    Test clearing with a checksum mask
+    """
+    def runTest(self):
+        # Add 4 entries to each checksum bucket
+        for i in range(0, 256):
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i),
+                        checksum=make_checksum(i, i*31))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        # Delete bucket 0
+        deleted, errors = self.do_clear(make_checksum(0, 0), make_checksum(0xFC, 0))
+        self.assertEquals(deleted, 4)
+        self.assertEquals(errors, 0)
+
+        # Delete buckets 0 (already cleared), 1, 2, 3
+        deleted, errors = self.do_clear(make_checksum(0, 0), make_checksum(0xF0, 0))
+        self.assertEquals(deleted, 12)
+        self.assertEquals(errors, 0)
+
+        # Delete second half of bucket 4
+        deleted, errors = self.do_clear(make_checksum(0x10, 0), make_checksum(0xFE, 0))
+        self.assertEquals(deleted, 2)
+        self.assertEquals(errors, 0)
+
+class AddDelete(BaseGenTableTest):
+    """
+    Test adding and deleting entries
+    """
+    def runTest(self):
+        # Add a few entries
+        for i in range(0, 3):
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        # Delete each entry
+        for i in range(0, 3):
+            self.do_delete(vlan_vid=i, ipv4=0x12345678)
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        # Clear table, but expect it to have already been empty
+        request = ofp.message.bsn_gentable_clear_request(table_id=TABLE_ID)
+        response, _ = self.controller.transact(request)
+        self.assertIsInstance(response, ofp.message.bsn_gentable_clear_reply)
+        self.assertEquals(response.error_count, 0)
+        self.assertEquals(response.deleted_count, 0)
+
+class EntryStats(BaseGenTableTest):
+    """
+    Test retrieving entry stats
+    """
+    def runTest(self):
+        # Add a few entries
+        for i in range(0, 3):
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        entries = self.do_entry_stats()
+        seen = set()
+        for entry in entries:
+            logging.debug(entry.show())
+            key = tlv_dict(entry.key)
+            stats = tlv_dict(entry.stats)
+            self.assertIn(ofp.bsn_tlv.vlan_vid, key)
+            self.assertIn(ofp.bsn_tlv.ipv4, key)
+            self.assertIn(ofp.bsn_tlv.rx_packets, stats)
+            self.assertIn(ofp.bsn_tlv.tx_packets, stats)
+            vlan_vid = key[ofp.bsn_tlv.vlan_vid]
+            seen.add(vlan_vid)
+            self.assertEqual(key[ofp.bsn_tlv.ipv4], 0x12345678)
+            self.assertEqual(stats[ofp.bsn_tlv.rx_packets], 100 * vlan_vid)
+            self.assertEqual(stats[ofp.bsn_tlv.tx_packets], 101 * vlan_vid)
+
+        self.assertEquals(seen, set([0, 1, 2]))
+
+class EntryStatsMasked(BaseGenTableTest):
+    """
+    Test retrieving entry stats with a checksum mask
+    """
+    def runTest(self):
+        def get_range(checksum, checksum_mask):
+            entries = self.do_entry_stats(checksum, checksum_mask)
+            vlan_vids = []
+            for entry in entries:
+                key = tlv_dict(entry.key)
+                vlan_vids.append(key[ofp.bsn_tlv.vlan_vid])
+            return sorted(vlan_vids)
+
+        # Add 4 entries to each checksum bucket
+        for i in range(0, 256):
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i),
+                        checksum=make_checksum(i, i))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        # Check first bucket
+        self.assertEquals(get_range(make_checksum(0, 0), make_checksum(0xFC, 0)),
+                          [0, 1, 2, 3])
+
+        # Check last bucket
+        self.assertEquals(get_range(make_checksum(0xFC, 0), make_checksum(0xFC, 0)),
+                          [252, 253, 254, 255])
+
+        # Check first half of first bucket
+        self.assertEquals(get_range(make_checksum(0x00, 0), make_checksum(0xFE, 0)),
+                          [0, 1])
+
+        # Check second half of first bucket
+        self.assertEquals(get_range(make_checksum(0x02, 0), make_checksum(0xFE, 0)),
+                          [2, 3])
+
+        # Check first half of last bucket
+        self.assertEquals(get_range(make_checksum(0xFC, 0), make_checksum(0xFE, 0)),
+                          [252, 253])
+
+        # Check second half of last bucket
+        self.assertEquals(get_range(make_checksum(0xFE, 0), make_checksum(0xFE, 0)),
+                          [254, 255])
+
+        # Check first two buckets
+        self.assertEquals(get_range(make_checksum(0, 0), make_checksum(0xF8, 0)),
+                          [0, 1, 2, 3, 4, 5, 6, 7])
+
+        # Check last two buckets
+        self.assertEquals(get_range(make_checksum(0xF8, 0), make_checksum(0xF8, 0)),
+                          [248, 249, 250, 251, 252, 253, 254, 255])
+
+        # Check matching on low bits
+        self.assertEquals(get_range(make_checksum(0x00, 0x00), ~1), [0])
+        self.assertEquals(get_range(make_checksum(0x01, 0x00), ~1), [1])
+        self.assertEquals(get_range(make_checksum(0x01, 0x02), ~1), [])
+
+class EntryStatsFragmented(BaseGenTableTest):
+    """
+    Test retrieving entry stats in mutiple replies
+    """
+    def runTest(self):
+        # Add a bunch of entries
+        # Enough for 3 stats replies
+        for i in range(0, 4095):
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, 5))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        entries = self.do_entry_stats()
+        seen = set()
+        for entry in entries:
+            key = tlv_dict(entry.key)
+            vlan_vid = key[ofp.bsn_tlv.vlan_vid]
+            self.assertNotIn(vlan_vid, seen)
+            seen.add(vlan_vid)
+
+        self.assertEquals(seen, set(range(0, 4095)))
+
+class EntryDescStats(BaseGenTableTest):
+    """
+    Test retrieving entry desc stats
+    """
+    def runTest(self):
+        # Add a few entries
+        for i in range(0, 3):
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i),
+                        checksum=0xfedcba9876543210fedcba9876543210 + i)
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        entries = self.do_entry_desc_stats()
+        seen = set()
+        for entry in entries:
+            logging.debug(entry.show())
+            key = tlv_dict(entry.key)
+            value = tlv_dict(entry.value)
+            self.assertIn(ofp.bsn_tlv.vlan_vid, key)
+            self.assertIn(ofp.bsn_tlv.ipv4, key)
+            self.assertIn(ofp.bsn_tlv.mac, value)
+            vlan_vid = key[ofp.bsn_tlv.vlan_vid]
+            seen.add(vlan_vid)
+            self.assertEqual(key[ofp.bsn_tlv.ipv4], 0x12345678)
+            self.assertEqual(value[ofp.bsn_tlv.mac], [0, 1, 2, 3, 4, vlan_vid])
+            self.assertEqual(entry.checksum, 0xfedcba9876543210fedcba9876543210 + vlan_vid)
+
+        self.assertEquals(seen, set([0, 1, 2]))
+
+class EntryDescStatsMasked(BaseGenTableTest):
+    """
+    Test retrieving entry desc stats with a checksum mask
+    """
+    def runTest(self):
+        def get_range(checksum, checksum_mask):
+            entries = self.do_entry_desc_stats(checksum, checksum_mask)
+            vlan_vids = []
+            for entry in entries:
+                key = tlv_dict(entry.key)
+                vlan_vids.append(key[ofp.bsn_tlv.vlan_vid])
+            return sorted(vlan_vids)
+
+        # Add 4 entries to each checksum bucket
+        for i in range(0, 256):
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i),
+                        checksum=make_checksum(i, i))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        # Check first bucket
+        self.assertEquals(get_range(make_checksum(0, 0), make_checksum(0xFC, 0)),
+                          [0, 1, 2, 3])
+
+        # Check last bucket
+        self.assertEquals(get_range(make_checksum(0xFC, 0), make_checksum(0xFC, 0)),
+                          [252, 253, 254, 255])
+
+        # Check first half of first bucket
+        self.assertEquals(get_range(make_checksum(0x00, 0), make_checksum(0xFE, 0)),
+                          [0, 1])
+
+        # Check second half of first bucket
+        self.assertEquals(get_range(make_checksum(0x02, 0), make_checksum(0xFE, 0)),
+                          [2, 3])
+
+        # Check first half of last bucket
+        self.assertEquals(get_range(make_checksum(0xFC, 0), make_checksum(0xFE, 0)),
+                          [252, 253])
+
+        # Check second half of last bucket
+        self.assertEquals(get_range(make_checksum(0xFE, 0), make_checksum(0xFE, 0)),
+                          [254, 255])
+
+        # Check first two buckets
+        self.assertEquals(get_range(make_checksum(0, 0), make_checksum(0xF8, 0)),
+                          [0, 1, 2, 3, 4, 5, 6, 7])
+
+        # Check last two buckets
+        self.assertEquals(get_range(make_checksum(0xF8, 0), make_checksum(0xF8, 0)),
+                          [248, 249, 250, 251, 252, 253, 254, 255])
+
+        # Check matching on low bits
+        self.assertEquals(get_range(make_checksum(0x00, 0x00), ~1), [0])
+        self.assertEquals(get_range(make_checksum(0x01, 0x00), ~1), [1])
+        self.assertEquals(get_range(make_checksum(0x01, 0x02), ~1), [])
+
+class EntryDescStatsFragmented(BaseGenTableTest):
+    """
+    Test retrieving entry stats in mutiple replies
+    """
+    def runTest(self):
+        # Add a bunch of entries
+        # Enough for 3 stats replies
+        for i in range(0, 4095):
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, 5))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        entries = self.do_entry_desc_stats()
+        seen = set()
+        for entry in entries:
+            key = tlv_dict(entry.key)
+            vlan_vid = key[ofp.bsn_tlv.vlan_vid]
+            self.assertNotIn(vlan_vid, seen)
+            seen.add(vlan_vid)
+
+        self.assertEquals(seen, set(range(0, 4095)))
+
+class TableDescStats(BaseGenTableTest):
+    """
+    Test retrieving table desc stats
+    """
+    def runTest(self):
+        entries = self.do_table_desc_stats()
+        seen = set()
+        for entry in entries:
+            logging.debug(entry.show())
+            self.assertNotIn(entry.table_id, seen)
+            self.assertNotIn(entry.name, seen)
+            seen.add(entry.table_id)
+            seen.add(entry.name)
+            if entry.table_id == TABLE_ID:
+                self.assertEqual(entry.name, "test")
+                self.assertEqual(entry.buckets_size, 64)
+                self.assertEqual(entry.max_entries, 1000)
+
+        self.assertIn(TABLE_ID, seen)
+
+class TableStats(BaseGenTableTest):
+    """
+    Test retrieving table stats
+    """
+    def runTest(self):
+        # Verify we have the test table and no duplicates
+        entries = self.do_table_stats()
+        seen = set()
+        for entry in entries:
+            logging.debug(entry.show())
+            self.assertNotIn(entry.table_id, seen)
+            seen.add(entry.table_id)
+            if entry.table_id == TABLE_ID:
+                self.assertEqual(entry.entry_count, 0)
+                self.assertEqual(entry.checksum, 0)
+        self.assertIn(TABLE_ID, seen)
+
+        table_checksum = 0
+
+        # Add a bunch of entries, spread among the checksum buckets
+        for i in range(0, 256):
+            table_checksum ^= make_checksum(i, i*31)
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i),
+                        checksum=make_checksum(i, i*31))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        table_stats = self.do_test_table_stats()
+        self.assertEqual(table_stats.entry_count, 256)
+        self.assertEqual(table_stats.checksum, table_checksum)
+
+        # Modify an entry, changing its checksum
+        i = 30
+        table_checksum ^= make_checksum(i, i*31) # subtract old checksum
+        table_checksum ^= make_checksum(i, i*37) # add new checksum
+        self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 4, 3, 2, 1, i),
+                    checksum=make_checksum(i, i*37))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        table_stats = self.do_test_table_stats()
+        self.assertEqual(table_stats.entry_count, 256)
+        self.assertEqual(table_stats.checksum, table_checksum)
+
+        # Delete an entry
+        i = 87
+        table_checksum ^= make_checksum(i, i*31)
+        self.do_delete(vlan_vid=i, ipv4=0x12345678)
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        table_stats = self.do_test_table_stats()
+        self.assertEqual(table_stats.entry_count, 255)
+        self.assertEqual(table_stats.checksum, table_checksum)
+
+class BucketStats(BaseGenTableTest):
+    """
+    Test retrieving checksum bucket stats
+    """
+    def runTest(self):
+        # Verify initial state
+        entries = self.do_bucket_stats()
+        self.assertEquals(len(entries), 64)
+        for entry in entries:
+            self.assertEquals(entry.checksum, 0)
+
+        buckets = [0] * len(entries)
+        checksum_bits = int(math.log(len(buckets), 2))
+
+        def update_bucket(checksum):
+            index = checksum >> (128 - checksum_bits)
+            buckets[index] ^= checksum
+
+        # Add a bunch of entries, spread among the checksum buckets
+        for i in range(0, 256):
+            update_bucket(make_checksum(i, i*31))
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i),
+                        checksum=make_checksum(i, i*31))
+
+        entries = self.do_bucket_stats()
+        self.assertEquals(len(entries), 64)
+        for i, entry in enumerate(entries):
+            self.assertEquals(entry.checksum, buckets[i])
+
+        # Modify an entry, changing its checksum
+        i = 30
+        update_bucket(make_checksum(i, i*31)) # subtract old checksum
+        update_bucket(make_checksum(i, i*37)) # add new checksum
+        self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 4, 3, 2, 1, i),
+                    checksum=make_checksum(i, i*37))
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        entries = self.do_bucket_stats()
+        self.assertEquals(len(entries), 64)
+        for i, entry in enumerate(entries):
+            self.assertEquals(entry.checksum, buckets[i])
+
+        # Delete an entry
+        i = 87
+        update_bucket(make_checksum(i, i*31))
+        self.do_delete(vlan_vid=i, ipv4=0x12345678)
+
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        entries = self.do_bucket_stats()
+        self.assertEquals(len(entries), 64)
+        for i, entry in enumerate(entries):
+            self.assertEquals(entry.checksum, buckets[i])
+
+class BucketStatsFragmented(BaseGenTableTest):
+    """
+    Test retrieving checksum bucket stats in multiple replies
+    """
+
+    def tearDown(self):
+        self.do_set_buckets_size(64)
+        do_barrier(self.controller)
+        BaseGenTableTest.tearDown(self)
+
+    def runTest(self):
+        # Enough for 3 stats messages
+        self.do_set_buckets_size(8192)
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        entries = self.do_bucket_stats()
+        self.assertEquals(len(entries), 8192)
+
+class SetBucketsSize(BaseGenTableTest):
+    """
+    Test setting the checksum buckets size
+    """
+    def setUp(self):
+        BaseGenTableTest.setUp(self)
+        self.do_set_buckets_size(64)
+        do_barrier(self.controller)
+
+    def tearDown(self):
+        self.do_set_buckets_size(64)
+        do_barrier(self.controller)
+        BaseGenTableTest.tearDown(self)
+
+    def runTest(self):
+        # Verify initial state
+        entries = self.do_bucket_stats()
+        self.assertEquals(len(entries), 64)
+        for entry in entries:
+            self.assertEquals(entry.checksum, 0)
+
+        buckets32 = [0] * 32
+        buckets64 = [0] * 64
+
+        def update_bucket(checksum):
+            buckets32[checksum >> (128 - int(math.log(32, 2)))] ^= checksum
+            buckets64[checksum >> (128 - int(math.log(64, 2)))] ^= checksum
+
+        # Add a bunch of entries, spread among the checksum buckets
+        for i in range(0, 256):
+            update_bucket(make_checksum(i, i*31))
+            self.do_add(vlan_vid=i, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, i),
+                        checksum=make_checksum(i, i*31))
+
+        entries = self.do_bucket_stats()
+        self.assertEquals(len(entries), 64)
+        for i, entry in enumerate(entries):
+            self.assertEquals(entry.checksum, buckets64[i])
+
+        self.do_set_buckets_size(32)
+        do_barrier(self.controller)
+
+        entries = self.do_bucket_stats()
+        self.assertEquals(len(entries), 32)
+        for i, entry in enumerate(entries):
+            self.assertEquals(entry.checksum, buckets32[i])
+
+        self.do_set_buckets_size(64)
+        do_barrier(self.controller)
+
+        entries = self.do_bucket_stats()
+        self.assertEquals(len(entries), 64)
+        for i, entry in enumerate(entries):
+            self.assertEquals(entry.checksum, buckets64[i])
+
+class SetBucketsSizeError(BaseGenTableTest):
+    """
+    Test error cases in setting the checksum buckets size
+    """
+    def setUp(self):
+        BaseGenTableTest.setUp(self)
+        self.do_set_buckets_size(64)
+        do_barrier(self.controller)
+
+    def tearDown(self):
+        self.do_set_buckets_size(64)
+        do_barrier(self.controller)
+        BaseGenTableTest.tearDown(self)
+
+    def runTest(self):
+        # Zero buckets size
+        self.do_set_buckets_size(0)
+        do_barrier(self.controller)
+
+        error, _ = self.controller.poll(ofp.OFPT_ERROR, 0)
+        self.assertIsInstance(error, ofp.message.bad_request_error_msg)
+        self.assertEquals(error.code, ofp.OFPBRC_EPERM)
+
+        # Non power of 2 buckets size
+        self.do_set_buckets_size(7)
+        do_barrier(self.controller)
+
+        error, _ = self.controller.poll(ofp.OFPT_ERROR, 0)
+        self.assertIsInstance(error, ofp.message.bad_request_error_msg)
+        self.assertEquals(error.code, ofp.OFPBRC_EPERM)
+
+class AddError(BaseGenTableTest):
+    """
+    Test failure adding entries
+    """
+    def runTest(self):
+        # Invalid key
+        self.do_add(vlan_vid=10000, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, 5))
+        do_barrier(self.controller)
+
+        error, _ = self.controller.poll(ofp.OFPT_ERROR, 0)
+        self.assertIsInstance(error, ofp.message.bad_request_error_msg)
+        self.assertEquals(error.code, ofp.OFPBRC_EPERM)
+
+        self.assertEquals(len(self.do_entry_desc_stats()), 0)
+
+        # Invalid value
+        self.do_add(vlan_vid=100, ipv4=0x12345678, mac=(1, 1, 2, 3, 4, 5))
+        do_barrier(self.controller)
+
+        error, _ = self.controller.poll(ofp.OFPT_ERROR, 0)
+        self.assertIsInstance(error, ofp.message.bad_request_error_msg)
+        self.assertEquals(error.code, ofp.OFPBRC_EPERM)
+
+        self.assertEquals(len(self.do_entry_desc_stats()), 0)
+
+class ModifyError(BaseGenTableTest):
+    """
+    Test failure modifying entries
+    """
+    def runTest(self):
+        # Add a valid entry we'll try to modify
+        self.do_add(vlan_vid=100, ipv4=0x12345678, mac=(0, 1, 2, 3, 4, 5))
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        orig_entries = self.do_entry_desc_stats()
+        self.assertEquals(len(orig_entries), 1)
+
+        # Invalid value
+        self.do_add(vlan_vid=100, ipv4=0x12345678, mac=(1, 1, 2, 3, 4, 5))
+        do_barrier(self.controller)
+
+        error, _ = self.controller.poll(ofp.OFPT_ERROR, 0)
+        self.assertIsInstance(error, ofp.message.bad_request_error_msg)
+        self.assertEquals(error.code, ofp.OFPBRC_EPERM)
+
+        # Check that the table wasn't modified
+        new_entries = self.do_entry_desc_stats()
+        self.assertEquals(len(new_entries), 1)
+        self.assertEquals(new_entries, orig_entries)
+
+class DeleteNonexistentError(BaseGenTableTest):
+    """
+    Test failure deleting a nonexistent entry
+    """
+    def runTest(self):
+        self.do_delete(vlan_vid=1000, ipv4=0x12345678)
+        do_barrier(self.controller)
+
+        error, _ = self.controller.poll(ofp.OFPT_ERROR, 0)
+        self.assertIsInstance(error, ofp.message.bad_request_error_msg)
+        self.assertEquals(error.code, ofp.OFPBRC_EPERM)
+
+class DeleteFailureError(BaseGenTableTest):
+    """
+    Test failure deleting a nonexistent entry
+
+    The very special idle_notification TLV will cause the entry to fail
+    being deleted the first time. This behavior is only there to help
+    test this error path.
+    """
+    def runTest(self):
+        self.do_add(vlan_vid=1000, ipv4=0x12345678,
+                    mac=(0, 1, 2, 3, 4, 5), idle_notification=True)
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+        orig_entries = self.do_entry_desc_stats()
+        self.assertEquals(len(orig_entries), 1)
+
+        # This will fail
+        self.do_delete(vlan_vid=1000, ipv4=0x12345678)
+        do_barrier(self.controller)
+
+        error, _ = self.controller.poll(ofp.OFPT_ERROR, 0)
+        self.assertIsInstance(error, ofp.message.bad_request_error_msg)
+        self.assertEquals(error.code, ofp.OFPBRC_EPERM)
+
+        # Check that the table wasn't modified
+        new_entries = self.do_entry_desc_stats()
+        self.assertEquals(len(new_entries), 1)
+        self.assertEquals(new_entries, orig_entries)
+
+        # This will succeed
+        self.do_delete(vlan_vid=1000, ipv4=0x12345678)
+        do_barrier(self.controller)
+        verify_no_errors(self.controller)
+
+class BadTableIdError(BaseGenTableTest):
+    """
+    Test failure of each message when specifying a nonexistent table id
+    """
+    def runTest(self):
+        def check_error(msg):
+            reply, _ = self.controller.transact(msg)
+            self.assertIsInstance(reply, ofp.message.bad_request_error_msg)
+            self.assertEquals(reply.code, ofp.OFPBRC_BAD_TABLE_ID)
+
+        valid_table_ids = set([x.table_id for x in self.do_table_desc_stats()])
+        invalid_table_id = TABLE_ID
+        while invalid_table_id in valid_table_ids:
+            invalid_table_id = random.randrange(65536)
+
+        logging.debug("Using invalid table id %d", invalid_table_id)
+
+        check_error(ofp.message.bsn_gentable_clear_request(
+            table_id=invalid_table_id))
+
+        check_error(ofp.message.bsn_gentable_entry_add(
+            table_id=invalid_table_id))
+
+        check_error(ofp.message.bsn_gentable_entry_delete(
+            table_id=invalid_table_id))
+
+        check_error(ofp.message.bsn_gentable_entry_stats_request(
+            table_id=invalid_table_id))
+
+        check_error(ofp.message.bsn_gentable_entry_desc_stats_request(
+            table_id=invalid_table_id))
+
+        check_error(ofp.message.bsn_gentable_bucket_stats_request(
+            table_id=invalid_table_id))
+
+        check_error(ofp.message.bsn_gentable_set_buckets_size(
+            table_id=invalid_table_id))