bmv2
Designing your own switch target with bmv2
Classes
lookup_structures.h File Reference

Go to the source code of this file.

Classes

class  bm::LookupStructure< K >
 
class  bm::LookupStructureFactory
 

Detailed Description

This file contains 2 classes: bm::LookupStructure and bm::LookupStructureFactory. When implementing your target, you may wish to provide custom implementations of the data structures used to perform matches. This can be done by inheriting from one or more of bm::ExactLookupStructure, bm::LPMMatchStructure, and bm::TernaryMatchStructure. Each of these is a specialization of the bm::LookupStructure template for the corresponding match key type.

Once the implementation of the new lookup structure is complete, you must extend the bm::LookupStructureFactory class to build instances of it. This is relatively simple. An (abridged) example of creating a new bm::Switch using a custom lookup structure is as follows

// Extend the appropriate LookupStructure type
class MyExactLookupStructure : public bm::ExactLookupStructure {
public:
virtual ~MyExactLookupStructure() = default;
bool lookup(const bm::ByteContainer &key_data,
bm::internal_handle_t *handle) const override {
// ...
}
bool entry_exists(const bm::ExactMatchKey &key) const override {
// ...
}
void add_entry(const bm::ExactMatchKey &key,
bm::internal_handle_t handle) override {
// ...
}
void delete_entry(const bm::ExactMatchKey &key) override {
// ...
}
void clear() override {
// ...
}
private:
// implementation details ...
};
// Create a factory subclass which creates your new structure
class MyFactory : public bm::LookupStructureFactory {
public:
virtual ~MyFactory() = default;
// Note that it's only necessary to override the create function for the
// particular match types you are interested in, the others will use the
// default implementations.
std::unique_ptr<bm::ExactLookupStructure>
create_for_exact(size_t size, size_t nbytes_key) override {
return std::unique_ptr<bm::ExactLookupStructure>(
new MyExactLookupStructure(/* parameters */));
}
};
// Override bm::Switch to create your target
class MySwitch : public bm::Switch {
// ...
};
int main(int argc, const char *argv[]) {
// Create an instance of your switch object
auto switch = new MySwitch();
// Pass it an instance of your factory class.
switch->set_lookup_factory(std::shared_ptr<bm::LookupStructureFactory>(
new MyFactory()));
// Initialize the switch. Note that this must be done *after* calling
// `set_lookup_factory`, otherwise the default lookup structures will
// be used.
int status = switch->init_from_command_line(argc, argv);
// ...
}
bm::Switch
Definition: switch.h:973
bm::LookupStructure
Definition: lookup_structures.h:120
bm::LookupStructure::delete_entry
virtual void delete_entry(const K &key)=0
bm::ByteContainer
Definition: bytecontainer.h:39
bm::LookupStructure::entry_exists
virtual bool entry_exists(const K &key) const =0
bm::LookupStructureFactory
Definition: lookup_structures.h:167
bm::LookupStructure::add_entry
virtual void add_entry(const K &key, internal_handle_t handle)=0
bm::LookupStructureFactory::create_for_exact
virtual std::unique_ptr< ExactLookupStructure > create_for_exact(size_t size, size_t nbytes_key)
Create a lookup structure for exact matches.
bm::LookupStructure::lookup
virtual bool lookup(const ByteContainer &key_data, internal_handle_t *handle) const =0
bm::LookupStructure::clear
virtual void clear()=0
Completely remove all entries from the data structure.