FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
types.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
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 #include <algorithm>
17 #include <iomanip>
18 #include <iostream>
19 #include <sstream>
20 
21 #include <boost/algorithm/string.hpp>
22 
23 #include "vom/types.hpp"
24 
25 namespace VOM {
26 
27 rc_t::rc_t(int v, const std::string s)
28  : enum_base<rc_t>(v, s)
29 {
30 }
32 {
33 }
34 
35 const rc_t&
37 {
38  if (0 == rv) {
39  return (rc_t::OK);
40  }
41  if (-68 == rv) {
42  // sub interface already exists
43  return (rc_t::OK);
44  }
45  if (-79 == rv) {
46  // interface already exists
47  return (rc_t::OK);
48  }
49 
50  return (rc_t::INVALID);
51 }
52 
53 const rc_t rc_t::UNSET(0, "un-set");
54 const rc_t rc_t::NOOP(1, "no-op");
55 const rc_t rc_t::OK(2, "ok");
56 const rc_t rc_t::INVALID(3, "invalid");
57 const rc_t rc_t::TIMEOUT(4, "timeout");
58 
59 const handle_t handle_t::INVALID(~0);
60 
62  : m_value(value)
63 {
64 }
65 
67  : m_value(~0)
68 {
69 }
70 
71 std::string
73 {
74  return (std::to_string(m_value));
75 }
76 
77 bool
78 handle_t::operator==(const handle_t& other) const
79 {
80  return (m_value == other.m_value);
81 }
82 
83 bool
84 handle_t::operator!=(const handle_t& other) const
85 {
86  return (!(*this == other));
87 }
88 
89 bool
90 handle_t::operator<(const handle_t& other) const
91 {
92  return (m_value < other.m_value);
93 }
94 
95 uint32_t
97 {
98  return (m_value);
99 }
100 
101 void
103 {
104  m_value = ~0;
105 }
106 
107 std::ostream&
108 operator<<(std::ostream& os, const handle_t& h)
109 {
110  os << h.value();
111 
112  return (os);
113 }
114 
116 {
117  std::copy(b, b + 6, std::begin(bytes));
118 }
119 
120 mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
121 {
122  std::copy(i.begin(), i.end(), std::begin(bytes));
123 }
124 
125 mac_address_t::mac_address_t(const std::string& str)
126 {
127  std::vector<std::string> parts;
128 
129  boost::split(parts, str, boost::is_any_of(":"));
130 
131  size_t n_bytes = std::min(bytes.size(), parts.size());
132 
133  for (uint32_t ii = 0; ii < n_bytes; ii++) {
134  bytes[ii] = std::stoul(parts[ii], nullptr, 16);
135  }
136 }
137 
138 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
139 
140 const mac_address_t mac_address_t::ZERO({ 0x0 });
141 
142 void
143 mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
144 {
145  for (int i = 0; i < 6 && i < len; i++) {
146  array[i] = bytes[i];
147  }
148 }
149 
150 std::string
152 {
153  std::ostringstream s;
154  bool first = true;
155 
156  s.fill('0');
157  s << std::hex;
158  for (auto byte : bytes) {
159  if (first)
160  first = false;
161  else
162  s << ":";
163  s << std::setw(2) << static_cast<unsigned int>(byte);
164  }
165 
166  return (s.str());
167 }
168 
169 bool
171 {
172  return (bytes == mac.bytes);
173 }
174 bool
176 {
177  return (bytes < m.bytes);
178 }
179 
180 std::ostream&
181 operator<<(std::ostream& os, const mac_address_t& mac)
182 {
183  os << mac.to_string();
184 
185  return (os);
186 }
187 
188 l2_address_t::l2_address_t(const uint8_t b[8], uint8_t n_bytes)
189  : bytes(n_bytes)
190 {
191  std::copy_n(b, n_bytes, std::begin(bytes));
192 }
193 
194 l2_address_t::l2_address_t(std::initializer_list<uint8_t> i)
195  : bytes(i)
196 {
197 }
198 
200  : bytes(6)
201 {
202  std::copy(begin(mac.bytes), std::end(mac.bytes), std::begin(bytes));
203 }
204 
205 const l2_address_t l2_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
206  0xff });
207 
208 const l2_address_t l2_address_t::ZERO({ 0x0 });
209 
210 void
211 l2_address_t::to_bytes(uint8_t* array, uint8_t len) const
212 {
213  for (uint8_t i = 0; i < bytes.size() && i < len; i++) {
214  array[i] = bytes[i];
215  }
216 }
217 
220 {
221  mac_address_t mac({});
222 
223  std::copy_n(bytes.begin(), mac.bytes.size(), mac.bytes.begin());
224 
225  return (mac);
226 }
227 
228 std::string
230 {
231  std::ostringstream s;
232  bool first = true;
233 
234  s.fill('0');
235  s << std::hex;
236  for (auto byte : bytes) {
237  if (first)
238  first = false;
239  else
240  s << ":";
241  s << std::setw(2) << static_cast<unsigned int>(byte);
242  }
243 
244  return (s.str());
245 }
246 
247 bool
249 {
250  return (bytes == l2.bytes);
251 }
252 
253 bool
255 {
256  return (bytes != l2.bytes);
257 }
258 
259 std::ostream&
260 operator<<(std::ostream& os, const l2_address_t& l2)
261 {
262  os << l2.to_string();
263 
264  return (os);
265 }
266 
267 const direction_t direction_t::INPUT(1, "input");
268 const direction_t direction_t::OUTPUT(0, "output");
269 
270 direction_t::direction_t(int v, const std::string s)
271  : enum_base(v, s)
272 {
273 }
274 std::ostream&
275 operator<<(std::ostream& os, const direction_t& dir)
276 {
277  os << dir.to_string();
278  return os;
279 }
280 
281 const ethertype_t ethertype_t::ARP(0x0806, "arp");
282 const ethertype_t ethertype_t::FCOE(0x8906, "fcoe");
283 const ethertype_t ethertype_t::IPV4(0x0800, "ipv4");
284 const ethertype_t ethertype_t::IPV6(0x86DD, "ipv6");
285 const ethertype_t ethertype_t::MAC_SECURITY(0x88E5, "mac-security");
286 const ethertype_t ethertype_t::MPLS_UNICAST(0x8847, "mpls-unicast");
287 const ethertype_t ethertype_t::TRILL(0x22F3, "trill");
288 const ethertype_t ethertype_t::UNSPECIFIED(0x0, "unspecified");
289 
290 ethertype_t::ethertype_t(int v, const std::string s)
291  : enum_base(v, s)
292 {
293 }
294 
295 std::ostream&
296 operator<<(std::ostream& os, const ethertype_t& ether)
297 {
298  os << ether.to_string();
299  return os;
300 }
301 
302 const ethertype_t&
304 {
305  if (0x0806 == numeric) {
306  return (ethertype_t::ARP);
307  }
308  if (0x8906 == numeric) {
309  return (ethertype_t::FCOE);
310  }
311  if (0x0800 == numeric) {
312  return (ethertype_t::IPV4);
313  }
314  if (0x86DD == numeric) {
315  return (ethertype_t::IPV6);
316  }
317  if (0x88E5 == numeric) {
318  return (ethertype_t::MAC_SECURITY);
319  }
320  if (0x8847 == numeric) {
321  return (ethertype_t::MPLS_UNICAST);
322  }
323  if (0x22F3 == numeric) {
324  return (ethertype_t::TRILL);
325  }
326 
327  return (ethertype_t::UNSPECIFIED);
328 }
329 
330 }; // namespace VOM
331 
332 /*
333  * fd.io coding-style-patch-verification: ON
334  *
335  * Local Variables:
336  * eval: (c-set-style "mozilla")
337  * End:
338  */
static const ethertype_t FCOE
Ethertype FCoE.
Definition: types.hpp:190
static const rc_t NOOP
The HW write/update action was/has not been attempted.
Definition: types.hpp:107
rc_t(const rc_t &rc)=default
static const rc_t & from_vpp_retval(int32_t rv)
Get the rc_t from the VPP API value.
Definition: types.cpp:36
~rc_t()
Destructor.
Definition: types.cpp:31
direction_t(int v, const std::string s)
Constructor.
Definition: types.cpp:270
A template base class for all enum types.
Definition: enum_base.hpp:30
uint32_t value() const
get the value of the handle
Definition: types.cpp:96
void to_bytes(uint8_t *array, uint8_t len) const
Convert to byte array.
Definition: types.cpp:211
bool operator<(const handle_t &other) const
less than operator
Definition: types.cpp:90
int i
std::string to_string() const
String conversion.
Definition: types.cpp:151
bool operator==(const mac_address_t &m) const
Comparison operator.
Definition: types.cpp:170
Error codes that VPP will return during a HW write.
Definition: types.hpp:90
static const ethertype_t & from_numeric_val(uint16_t numeric)
Get the ethertype from the numeric value.
Definition: types.cpp:303
static const ethertype_t IPV6
Ethertype Ipv6.
Definition: types.hpp:200
Feature Ethertype.
Definition: types.hpp:170
static const l2_address_t ZERO
An all 0&#39;s L2 address.
Definition: types.hpp:361
static const direction_t INPUT
Permit Direction.
Definition: types.hpp:154
static const handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:271
Type def of a L2 address as read from VPP.
Definition: types.hpp:342
ethertype_t(int v, const std::string s)
Constructor.
Definition: types.cpp:290
static const l2_address_t ONE
An all 1&#39;s L2 address.
Definition: types.hpp:356
Feature Directions.
Definition: types.hpp:139
std::string to_string() const
convert to string format for debug purposes
Definition: types.cpp:72
static const ethertype_t MAC_SECURITY
Ethertype MAC Security.
Definition: types.hpp:205
bool operator!=(const handle_t &other) const
Comparison operator.
Definition: types.cpp:84
static heap_elt_t * first(heap_header_t *h)
Definition: heap.c:59
static const ethertype_t IPV4
Ethertype IPv4.
Definition: types.hpp:195
static const mac_address_t ONE
An all 1&#39;s MAC address.
Definition: types.hpp:311
#define v
Definition: acl.c:495
std::vector< uint8_t > bytes
Underlying bytes array - filled from least to most significant.
Definition: types.hpp:386
std::string to_string() const
String conversion.
Definition: types.cpp:229
static const ethertype_t TRILL
Ethertype TRILL.
Definition: types.hpp:215
static const ethertype_t ARP
Ethertype Arp.
Definition: types.hpp:185
A type declaration of an interface handle in VPP.
Definition: types.hpp:236
std::array< uint8_t, 6 > bytes
Underlying bytes array.
Definition: types.hpp:336
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:112
static const ethertype_t UNSPECIFIED
Ethertype Unspecified.
Definition: types.hpp:220
void to_bytes(uint8_t *array, uint8_t len) const
Convert to byte array.
Definition: types.cpp:143
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
static const rc_t INVALID
HW write reported invalid input.
Definition: types.hpp:117
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
bool operator<(const mac_address_t &m) const
less than operator
Definition: types.cpp:175
void reset()
reset the value of the handle to ~0
Definition: types.cpp:102
handle_t()
Constructor.
Definition: types.cpp:66
static const rc_t UNSET
The value un-set.
Definition: types.hpp:102
bool operator==(const handle_t &other) const
Comparison operator.
Definition: types.cpp:78
bool operator==(const l2_address_t &m) const
Comparison operator.
Definition: types.cpp:248
static const ethertype_t MPLS_UNICAST
Ethertype MPLS unicast.
Definition: types.hpp:210
static const direction_t OUTPUT
Deny Direction.
Definition: types.hpp:159
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
mac_address_t to_mac() const
MAC address conversion.
Definition: types.cpp:219
mac_address_t(uint8_t bytes[6])
Definition: types.cpp:115
Type def of a Ethernet address.
Definition: types.hpp:298
static const mac_address_t ZERO
An all 0&#39;s MAC address.
Definition: types.hpp:316
bool operator!=(const l2_address_t &m) const
Comparison operator.
Definition: types.cpp:254
static const rc_t TIMEOUT
HW write timedout - VPP did not respond within a timely manner.
Definition: types.hpp:122
l2_address_t(const uint8_t bytes[8], uint8_t n_bytes)
Definition: types.cpp:188