FD.io VPP  v18.01.2-1-g9b554f3
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  // interface laready exists
43  return (rc_t::OK);
44  }
45 
46  return (rc_t::INVALID);
47 }
48 
49 const rc_t rc_t::UNSET(0, "un-set");
50 const rc_t rc_t::NOOP(1, "no-op");
51 const rc_t rc_t::OK(2, "ok");
52 const rc_t rc_t::INVALID(3, "invalid");
53 const rc_t rc_t::TIMEOUT(4, "timeout");
54 
55 const handle_t handle_t::INVALID(~0);
56 
58  : m_value(value)
59 {
60 }
61 
63  : m_value(~0)
64 {
65 }
66 
67 std::string
69 {
70  return (std::to_string(m_value));
71 }
72 
73 bool
74 handle_t::operator==(const handle_t& other) const
75 {
76  return (m_value == other.m_value);
77 }
78 
79 bool
80 handle_t::operator!=(const handle_t& other) const
81 {
82  return (!(*this == other));
83 }
84 
85 bool
86 handle_t::operator<(const handle_t& other) const
87 {
88  return (m_value < other.m_value);
89 }
90 
91 uint32_t
93 {
94  return (m_value);
95 }
96 
97 std::ostream&
98 operator<<(std::ostream& os, const handle_t& h)
99 {
100  os << h.value();
101 
102  return (os);
103 }
104 
106 {
107  std::copy(b, b + 6, std::begin(bytes));
108 }
109 
110 mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
111 {
112  std::copy(i.begin(), i.end(), std::begin(bytes));
113 }
114 
115 mac_address_t::mac_address_t(const std::string& str)
116 {
117  std::vector<std::string> parts;
118 
119  boost::split(parts, str, boost::is_any_of(":"));
120 
121  size_t n_bytes = std::min(bytes.size(), parts.size());
122 
123  for (uint32_t ii = 0; ii < n_bytes; ii++) {
124  bytes[ii] = std::stoul(parts[ii], nullptr, 16);
125  }
126 }
127 
128 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
129 
130 const mac_address_t mac_address_t::ZERO({ 0x0 });
131 
132 void
133 mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
134 {
135  for (int i = 0; i < 6 && i < len; i++) {
136  array[i] = bytes[i];
137  }
138 }
139 
140 std::string
142 {
143  std::ostringstream s;
144  bool first = true;
145 
146  s.fill('0');
147  s << std::hex;
148  for (auto byte : bytes) {
149  if (first)
150  first = false;
151  else
152  s << ":";
153  s << std::setw(2) << static_cast<unsigned int>(byte);
154  }
155 
156  return (s.str());
157 }
158 
159 bool
161 {
162  return (bytes == mac.bytes);
163 }
164 bool
166 {
167  return (bytes < m.bytes);
168 }
169 
170 std::ostream&
171 operator<<(std::ostream& os, const mac_address_t& mac)
172 {
173  os << mac.to_string();
174 
175  return (os);
176 }
177 
178 l2_address_t::l2_address_t(const uint8_t b[8], uint8_t n_bytes)
179  : bytes(n_bytes)
180 {
181  std::copy_n(b, n_bytes, std::begin(bytes));
182 }
183 
184 l2_address_t::l2_address_t(std::initializer_list<uint8_t> i)
185  : bytes(i)
186 {
187 }
188 
190  : bytes(6)
191 {
192  std::copy(begin(mac.bytes), std::end(mac.bytes), std::begin(bytes));
193 }
194 
195 const l2_address_t l2_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
196  0xff });
197 
198 const l2_address_t l2_address_t::ZERO({ 0x0 });
199 
200 void
201 l2_address_t::to_bytes(uint8_t* array, uint8_t len) const
202 {
203  for (uint8_t i = 0; i < bytes.size() && i < len; i++) {
204  array[i] = bytes[i];
205  }
206 }
207 
210 {
211  mac_address_t mac({});
212 
213  std::copy_n(bytes.begin(), mac.bytes.size(), mac.bytes.begin());
214 
215  return (mac);
216 }
217 
218 std::string
220 {
221  std::ostringstream s;
222  bool first = true;
223 
224  s.fill('0');
225  s << std::hex;
226  for (auto byte : bytes) {
227  if (first)
228  first = false;
229  else
230  s << ":";
231  s << std::setw(2) << static_cast<unsigned int>(byte);
232  }
233 
234  return (s.str());
235 }
236 
237 bool
239 {
240  return (bytes == l2.bytes);
241 }
242 
243 bool
245 {
246  return (bytes != l2.bytes);
247 }
248 
249 std::ostream&
250 operator<<(std::ostream& os, const l2_address_t& l2)
251 {
252  os << l2.to_string();
253 
254  return (os);
255 }
256 
257 const direction_t direction_t::INPUT(1, "input");
258 const direction_t direction_t::OUTPUT(0, "output");
259 
260 direction_t::direction_t(int v, const std::string s)
261  : enum_base(v, s)
262 {
263 }
264 std::ostream&
265 operator<<(std::ostream& os, const direction_t& dir)
266 {
267  os << dir.to_string();
268  return os;
269 }
270 
271 }; // namespace VOM
272 
273 /*
274  * fd.io coding-style-patch-verification: ON
275  *
276  * Local Variables:
277  * eval: (c-set-style "mozilla")
278  * End:
279  */
static const rc_t NOOP
The HW write/update action was/has not been attempted.
Definition: types.hpp:101
rc_t(const rc_t &rc)=default
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
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:260
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:92
void to_bytes(uint8_t *array, uint8_t len) const
Convert to byte array.
Definition: types.cpp:201
bool operator<(const handle_t &other) const
less than operator
Definition: types.cpp:86
std::string to_string() const
String conversion.
Definition: types.cpp:141
bool operator==(const mac_address_t &m) const
Comparison operator.
Definition: types.cpp:160
Error codes that VPP will return during a HW write.
Definition: types.hpp:84
static const l2_address_t ZERO
An all 0&#39;s L2 address.
Definition: types.hpp:284
static const direction_t INPUT
Permit Direction.
Definition: types.hpp:148
static const handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:199
Type def of a L2 address as read from VPP.
Definition: types.hpp:265
static const l2_address_t ONE
An all 1&#39;s L2 address.
Definition: types.hpp:279
Feature Directions.
Definition: types.hpp:133
std::string to_string() const
convert to string format for debug purposes
Definition: types.cpp:68
bool operator!=(const handle_t &other) const
Comparison operator.
Definition: types.cpp:80
static heap_elt_t * first(heap_header_t *h)
Definition: heap.c:59
static const mac_address_t ONE
An all 1&#39;s MAC address.
Definition: types.hpp:234
#define v
Definition: acl.c:341
std::vector< uint8_t > bytes
Underlying bytes array - filled from least to most significant.
Definition: types.hpp:309
std::string to_string() const
String conversion.
Definition: types.cpp:219
vec_header_t h
Definition: buffer.c:282
A type declaration of an interface handle in VPP.
Definition: types.hpp:164
std::array< uint8_t, 6 > bytes
Underlying bytes array.
Definition: types.hpp:259
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:106
void to_bytes(uint8_t *array, uint8_t len) const
Convert to byte array.
Definition: types.cpp:133
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:111
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:165
handle_t()
Constructor.
Definition: types.cpp:62
static const rc_t UNSET
The value un-set.
Definition: types.hpp:96
bool operator==(const handle_t &other) const
Comparison operator.
Definition: types.cpp:74
bool operator==(const l2_address_t &m) const
Comparison operator.
Definition: types.cpp:238
static const direction_t OUTPUT
Deny Direction.
Definition: types.hpp:153
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:209
mac_address_t(uint8_t bytes[6])
Definition: types.cpp:105
Type def of a Ethernet address.
Definition: types.hpp:221
static const mac_address_t ZERO
An all 0&#39;s MAC address.
Definition: types.hpp:239
bool operator!=(const l2_address_t &m) const
Comparison operator.
Definition: types.cpp:244
static const rc_t TIMEOUT
HW write timedout - VPP did not respond within a timely manner.
Definition: types.hpp:116
l2_address_t(const uint8_t bytes[8], uint8_t n_bytes)
Definition: types.cpp:178