bmv2
Designing your own switch target with bmv2
Loading...
Searching...
No Matches
checksums.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_CHECKSUMS_H_
22#define BM_BM_SIM_CHECKSUMS_H_
23
24#include <string>
25#include <memory>
26
27#include "named_p4object.h"
28#include "expressions.h"
29
30namespace bm {
31
32class Packet;
33
34class NamedCalculation;
35
36class Checksum : public NamedP4Object{
37 public:
38 Checksum(const std::string &name, p4object_id_t id,
39 header_id_t header_id, int field_offset);
40
41 virtual ~Checksum() = default;
42
44 Checksum(const Checksum &other) = delete;
46 Checksum &operator=(const Checksum &other) = delete;
47
48 // The following are implictly deleted otherwise because of the user-defined
49 // virtual destructor.
50
52 Checksum(Checksum &&other) = default;
54 Checksum &operator=(Checksum &&other) = default;
55
56 void update(Packet *pkt) const;
57
58 bool verify(const Packet &pkt) const;
59
60 void set_checksum_condition(std::unique_ptr<Expression> cksum_condition);
61
62 private:
63 virtual void update_(Packet *pkt) const = 0;
64 virtual bool verify_(const Packet &pkt) const = 0;
65
66 bool is_checksum_condition_met(const Packet &pkt) const;
67
68 bool is_target_field_valid(const Packet &pkt) const;
69
70 protected:
71 header_id_t header_id;
72 int field_offset;
73
74 private:
75 std::unique_ptr<Expression> condition{nullptr};
76};
77
78class CalcBasedChecksum : public Checksum {
79 public:
80 CalcBasedChecksum(const std::string &name, p4object_id_t id,
81 header_id_t header_id, int field_offset,
82 const NamedCalculation *calculation);
83
84 CalcBasedChecksum(const CalcBasedChecksum &other) = delete;
85 CalcBasedChecksum &operator=(const CalcBasedChecksum &other) = delete;
86
87 CalcBasedChecksum(CalcBasedChecksum &&other) = default;
88 CalcBasedChecksum &operator=(CalcBasedChecksum &&other) = default;
89
90 private:
91 void update_(Packet *pkt) const override;
92 bool verify_(const Packet &pkt) const override;
93
94 private:
95 const NamedCalculation *calculation{nullptr};
96};
97
98class IPv4Checksum : public Checksum {
99 public:
100 IPv4Checksum(const std::string &name, p4object_id_t id,
101 header_id_t header_id, int field_offset);
102
103 private:
104 void update_(Packet *pkt) const override;
105 bool verify_(const Packet &pkt) const override;
106};
107
108} // namespace bm
109
110#endif // BM_BM_SIM_CHECKSUMS_H_
NamedP4Object & operator=(const NamedP4Object &other)=delete
Deleted copy assignment operator.