bmv2
Designing your own switch target with bmv2
Loading...
Searching...
No Matches
match_key_types.h
1/* Copyright 2013-present Barefoot Networks, Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/*
17 * Gordon Bailey (gb@gordonbailey.net)
18 *
19 */
20
21#ifndef BM_BM_SIM_MATCH_KEY_TYPES_H_
22#define BM_BM_SIM_MATCH_KEY_TYPES_H_
23
24#include <limits>
25#include <utility>
26#include <vector>
27
28#include "bytecontainer.h"
29
30namespace bm {
31
32using internal_handle_t = uintptr_t;
33using entry_handle_t = uint32_t;
34
35enum class MatchUnitType {
36 EXACT, LPM, TERNARY, RANGE
37};
38
39// Entry types.
40struct MatchKey {
41 MatchKey() {}
42 MatchKey(ByteContainer data, uint32_t version)
43 : data(std::move(data)), version(version) {}
44
45 ByteContainer data{};
46 uint32_t version{0};
47
48 protected:
49 // disabling polymorphic deletion by making the destructor protected
50 ~MatchKey() { }
51};
52
53struct ExactMatchKey : public MatchKey {
54 static constexpr MatchUnitType mut = MatchUnitType::EXACT;
55};
56
57struct LPMMatchKey : public MatchKey {
58 LPMMatchKey() {}
59 LPMMatchKey(ByteContainer data, int prefix_length, uint32_t version)
60 : MatchKey(std::move(data), version), prefix_length(prefix_length) {}
61
62 int prefix_length{0};
63
64 static constexpr MatchUnitType mut = MatchUnitType::LPM;
65};
66
67struct TernaryMatchKey : public MatchKey {
68 TernaryMatchKey() {}
69 TernaryMatchKey(ByteContainer data, ByteContainer mask, int priority,
70 uint32_t version)
71 : MatchKey(std::move(data), version), mask(std::move(mask)),
72 priority(priority) {}
73
74 ByteContainer mask{};
75 // This is initialized to `max` because lookups search for the matching
76 // key with the minimum priority, this ensures that default constructed
77 // TernaryMatchKey's will never be matched.
78 int priority{std::numeric_limits<decltype(priority)>::max()};
79
80 static constexpr MatchUnitType mut = MatchUnitType::TERNARY;
81};
82
83struct RangeMatchKey : public TernaryMatchKey {
84 RangeMatchKey() {}
85 RangeMatchKey(ByteContainer data, ByteContainer mask, int priority,
86 std::vector<size_t> range_widths, uint32_t version)
87 : TernaryMatchKey(std::move(data), std::move(mask), priority, version),
88 range_widths(std::move(range_widths)) {}
89
90 std::vector<size_t> range_widths;
91
92 static constexpr MatchUnitType mut = MatchUnitType::RANGE;
93};
94
95} // namespace bm
96
97#endif // BM_BM_SIM_MATCH_KEY_TYPES_H_