bmv2
Designing your own switch target with bmv2
Macros
calculations.h File Reference

Go to the source code of this file.

Macros

#define REGISTER_HASH(hash_name)
 

Detailed Description

Each hash algorithm supported by the target needs to be declared as a functor. This is how you declare one:

struct hash_ex {
uint32_t operator()(const char *buf, size_t s) const {
const int p = 16777619;
int hash = 2166136261;
for (size_t i = 0; i < s; i++)
hash = (hash ^ buf[i]) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return static_cast<uint32_t>(hash);
}
};

Note that the signature of the functor must exactly match the one of the example: uint32_t(const char *buf, size_t s) const. Otherwise, you will get a compilation error.

You can then register your hash function like this:

REGISTER_HASH(hash_ex)

In P4 v1.0.2, hash algorithms are used by field_list_calculation objects

Macro Definition Documentation

◆ REGISTER_HASH

#define REGISTER_HASH (   hash_name)
Value:
bool hash_name##_create_ = \
bm::CalculationsMap::get_instance()->register_one( \
#hash_name, \
std::unique_ptr<bm::CalculationsMap::MyC>( \
new bm::RawCalculation<uint64_t, hash_name>()));

When implementing an hash operation for a target, this macro needs to be called to make this module aware of the hash existence. When calling this macro from an anonymous namespace, some compiler may give you a warning.

REGISTER_HASH
#define REGISTER_HASH(hash_name)
Definition: calculations.h:336