bmv2
Designing your own switch target with bmv2
parser_error.h
Go to the documentation of this file.
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 
22 
23 #ifndef BM_BM_SIM_PARSER_ERROR_H_
24 #define BM_BM_SIM_PARSER_ERROR_H_
25 
26 #include <exception>
27 #include <limits>
28 #include <string>
29 #include <unordered_map>
30 
31 namespace bm {
32 
35 class ErrorCode {
36  friend class ErrorCodeMap;
37  public:
38  using type_t = int;
39 
40  explicit ErrorCode(type_t v)
41  : v(v) { }
42 
43  type_t get() { return v; }
44 
46  bool operator==(const ErrorCode &other) const {
47  return (v == other.v);
48  }
49 
51  bool operator!=(const ErrorCode &other) const {
52  return !(*this == other);
53  }
54 
57  static ErrorCode make_invalid() { return ErrorCode(INVALID_V); }
58 
59  private:
60  type_t v;
61 
62  static constexpr type_t INVALID_V = std::numeric_limits<type_t>::max();
63 };
64 
66 class ErrorCodeMap {
67  public:
69  enum class Core {
71  NoError,
75  NoMatch,
85  };
86 
87  bool add(const std::string &name, ErrorCode::type_t v);
88  bool exists(const std::string &name) const;
89  bool exists(ErrorCode::type_t v) const;
90  // Add the core error codes to the map, providing they are not already
91  // present. If they need to be added, we make sure to use different integral
92  // values than for the existing codes.
93  void add_core();
94 
97  ErrorCode from_name(const std::string &name) const;
99  ErrorCode from_core(const Core &core) const;
102  const std::string &to_name(const ErrorCode &code) const;
103 
104  static ErrorCodeMap make_with_core();
105 
106  private:
107  static const char *core_to_name(const Core &core);
108 
109  std::unordered_map<ErrorCode::type_t, std::string> map_v_to_name{};
110  std::unordered_map<std::string, ErrorCode::type_t> map_name_to_v{};
111 };
112 
113 class parser_exception : public std::exception {
114  public:
115  virtual ~parser_exception() { }
116 
117  virtual ErrorCode get(const ErrorCodeMap &error_codes) const = 0;
118 };
119 
120 class parser_exception_arch : public parser_exception {
121  public:
122  explicit parser_exception_arch(const ErrorCode &code);
123 
124  ErrorCode get(const ErrorCodeMap &error_codes) const override;
125 
126  private:
127  const ErrorCode code;
128 };
129 
130 class parser_exception_core : public parser_exception {
131  public:
132  explicit parser_exception_core(ErrorCodeMap::Core core);
133 
134  ErrorCode get(const ErrorCodeMap &error_codes) const override;
135 
136  private:
137  ErrorCodeMap::Core core;
138 };
139 
140 } // namespace bm
141 
142 #endif // BM_BM_SIM_PARSER_ERROR_H_
bm::ErrorCodeMap::from_name
ErrorCode from_name(const std::string &name) const
bm::ErrorCodeMap::Core::NoError
@ NoError
No error raised in parser.
bm::ErrorCode::make_invalid
static ErrorCode make_invalid()
Definition: parser_error.h:57
bm::ErrorCode
Definition: parser_error.h:35
bm::ErrorCode::operator!=
bool operator!=(const ErrorCode &other) const
Inequality operator.
Definition: parser_error.h:51
bm::ErrorCodeMap::Core::ParserInvalidArgument
@ ParserInvalidArgument
bm::ErrorCodeMap::to_name
const std::string & to_name(const ErrorCode &code) const
bm::ErrorCodeMap::Core::ParserTimeout
@ ParserTimeout
Parser execution time limit exceeded (unused for now)
bm::ErrorCodeMap::Core::HeaderTooShort
@ HeaderTooShort
Extracting too many bits into a varbit field (unused for now)
bm::ErrorCodeMap::Core::NoMatch
@ NoMatch
Match statement has no matches (unused for now)
bm::ErrorCodeMap::Core::StackOutOfBounds
@ StackOutOfBounds
Reference to invalid element of a header stack (partial support)
bm::ErrorCodeMap::Core::PacketTooShort
@ PacketTooShort
Not enough bits in packet for extract or lookahead.
bm::ErrorCodeMap::from_core
ErrorCode from_core(const Core &core) const
Retrieve an error code for one of the core errors.
bm::ErrorCodeMap
A bi-directional map between error codes and their P4 names.
Definition: parser_error.h:66
bm::ErrorCodeMap::Core
Core
The core erros, as per core.p4.
Definition: parser_error.h:69
bm::ErrorCode::operator==
bool operator==(const ErrorCode &other) const
Equality operator.
Definition: parser_error.h:46