bmv2
Designing your own switch target with bmv2
Loading...
Searching...
No Matches
entries.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 * Antonin Bas (antonin@barefootnetworks.com)
18 *
19 */
20
21#ifndef BM_BM_SIM_ENTRIES_H_
22#define BM_BM_SIM_ENTRIES_H_
23
24#include <memory>
25#include <utility>
26
27#include "actions.h"
28#include "bytecontainer.h"
29#include "control_flow.h"
30
31namespace bm {
32
33class ControlFlowNode;
34
35struct MatchEntry {
36 ByteContainer key{};
37 ActionFnEntry action_entry{}; // includes action data
38 const ControlFlowNode *next_table{nullptr};
39
40 MatchEntry() {}
41
42 MatchEntry(ByteContainer key,
43 ActionFnEntry action_entry,
44 const ControlFlowNode *next_table)
45 : key(std::move(key)), action_entry(std::move(action_entry)),
46 next_table(next_table) {}
47
48 MatchEntry(const MatchEntry &other) = delete;
49 MatchEntry &operator=(const MatchEntry &other) = delete;
50
51 MatchEntry(MatchEntry &&other) = default;
52 MatchEntry &operator=(MatchEntry &&other) = default;
53};
54
55struct ExactMatchEntry: MatchEntry {
56 ExactMatchEntry()
57 : MatchEntry() {}
58
59 ExactMatchEntry(ByteContainer key,
60 ActionFnEntry action_entry,
61 const ControlFlowNode *next_table)
62 : MatchEntry(std::move(key), std::move(action_entry), next_table) {}
63};
64
65struct LongestPrefixMatchEntry : MatchEntry {
66 int prefix_length{0};
67
68 LongestPrefixMatchEntry()
69 : MatchEntry() {}
70
71 LongestPrefixMatchEntry(ByteContainer key,
72 ActionFnEntry action_entry,
73 unsigned int prefix_length,
74 const ControlFlowNode *next_table)
75 : MatchEntry(std::move(key), std::move(action_entry), next_table),
76 prefix_length(prefix_length) {
77 unsigned byte_index = prefix_length / 8;
78 unsigned mod = prefix_length % 8;
79 if (mod > 0) {
80 byte_index++;
81 this->key[byte_index] &= ~(0xFF >> mod);
82 }
83 for (; byte_index < key.size(); byte_index++) {
84 this->key[byte_index] = 0;
85 }
86 }
87};
88
89struct TernaryMatchEntry : MatchEntry {
90 ByteContainer mask{};
91 int priority{0};
92
93 TernaryMatchEntry()
94 : MatchEntry() {}
95
96 TernaryMatchEntry(ByteContainer key, ActionFnEntry action_entry,
97 ByteContainer mask, int priority,
98 const ControlFlowNode *next_table)
99 : MatchEntry(std::move(key), std::move(action_entry), next_table),
100 mask(std::move(mask)), priority(priority) {
101 for (unsigned byte_index = 0; byte_index < key.size(); byte_index++) {
102 this->key[byte_index] &= mask[byte_index];
103 }
104 }
105};
106
107} // namespace bm
108
109#endif // BM_BM_SIM_ENTRIES_H_