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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <boost/algorithm/string.hpp>
23 #include "vom/types.hpp"
27 rc_t::rc_t(int v, const std::string s)
28 : enum_base<rc_t>(v, s)
36 rc_t::from_vpp_retval(int32_t rv)
42 // interface laready exists
46 return (rc_t::INVALID);
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");
55 const handle_t handle_t::INVALID(~0);
57 handle_t::handle_t(int value)
68 handle_t::to_string() const
70 return (std::to_string(m_value));
74 handle_t::operator==(const handle_t& other) const
76 return (m_value == other.m_value);
80 handle_t::operator!=(const handle_t& other) const
82 return (!(*this == other));
86 handle_t::operator<(const handle_t& other) const
88 return (m_value < other.m_value);
92 handle_t::value() const
104 operator<<(std::ostream& os, const handle_t& h)
111 mac_address_t::mac_address_t(uint8_t b[6])
113 std::copy(b, b + 6, std::begin(bytes));
116 mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
118 std::copy(i.begin(), i.end(), std::begin(bytes));
121 mac_address_t::mac_address_t(const std::string& str)
123 std::vector<std::string> parts;
125 boost::split(parts, str, boost::is_any_of(":"));
127 size_t n_bytes = std::min(bytes.size(), parts.size());
129 for (uint32_t ii = 0; ii < n_bytes; ii++) {
130 bytes[ii] = std::stoul(parts[ii], nullptr, 16);
134 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
136 const mac_address_t mac_address_t::ZERO({ 0x0 });
139 mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
141 for (int i = 0; i < 6 && i < len; i++) {
147 mac_address_t::to_string() const
149 std::ostringstream s;
154 for (auto byte : bytes) {
159 s << std::setw(2) << static_cast<unsigned int>(byte);
166 mac_address_t::operator==(const mac_address_t& mac) const
168 return (bytes == mac.bytes);
171 mac_address_t::operator<(const mac_address_t& m) const
173 return (bytes < m.bytes);
177 operator<<(std::ostream& os, const mac_address_t& mac)
179 os << mac.to_string();
184 l2_address_t::l2_address_t(const uint8_t b[8], uint8_t n_bytes)
187 std::copy_n(b, n_bytes, std::begin(bytes));
190 l2_address_t::l2_address_t(std::initializer_list<uint8_t> i)
195 l2_address_t::l2_address_t(const mac_address_t& mac)
198 std::copy(begin(mac.bytes), std::end(mac.bytes), std::begin(bytes));
201 const l2_address_t l2_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
204 const l2_address_t l2_address_t::ZERO({ 0x0 });
207 l2_address_t::to_bytes(uint8_t* array, uint8_t len) const
209 for (uint8_t i = 0; i < bytes.size() && i < len; i++) {
215 l2_address_t::to_mac() const
217 mac_address_t mac({});
219 std::copy_n(bytes.begin(), mac.bytes.size(), mac.bytes.begin());
225 l2_address_t::to_string() const
227 std::ostringstream s;
232 for (auto byte : bytes) {
237 s << std::setw(2) << static_cast<unsigned int>(byte);
244 l2_address_t::operator==(const l2_address_t& l2) const
246 return (bytes == l2.bytes);
250 l2_address_t::operator!=(const l2_address_t& l2) const
252 return (bytes != l2.bytes);
256 operator<<(std::ostream& os, const l2_address_t& l2)
258 os << l2.to_string();
263 const direction_t direction_t::INPUT(1, "input");
264 const direction_t direction_t::OUTPUT(0, "output");
266 direction_t::direction_t(int v, const std::string s)
271 operator<<(std::ostream& os, const direction_t& dir)
273 os << dir.to_string();
277 const ethertype_t ethertype_t::ARP(0x0806, "arp");
278 const ethertype_t ethertype_t::FCOE(0x8906, "fcoe");
279 const ethertype_t ethertype_t::IPV4(0x0800, "ipv4");
280 const ethertype_t ethertype_t::IPV6(0x86DD, "ipv6");
281 const ethertype_t ethertype_t::MAC_SECURITY(0x88E5, "mac-security");
282 const ethertype_t ethertype_t::MPLS_UNICAST(0x8847, "mpls-unicast");
283 const ethertype_t ethertype_t::TRILL(0x22F3, "trill");
284 const ethertype_t ethertype_t::UNSPECIFIED(0x0, "unspecified");
286 ethertype_t::ethertype_t(int v, const std::string s)
292 operator<<(std::ostream& os, const ethertype_t& ether)
294 os << ether.to_string();
299 ethertype_t::from_numeric_val(uint16_t numeric)
301 if (0x0806 == numeric) {
302 return (ethertype_t::ARP);
304 if (0x8906 == numeric) {
305 return (ethertype_t::FCOE);
307 if (0x0800 == numeric) {
308 return (ethertype_t::IPV4);
310 if (0x86DD == numeric) {
311 return (ethertype_t::IPV6);
313 if (0x88E5 == numeric) {
314 return (ethertype_t::MAC_SECURITY);
316 if (0x8847 == numeric) {
317 return (ethertype_t::MPLS_UNICAST);
319 if (0x22F3 == numeric) {
320 return (ethertype_t::TRILL);
323 return (ethertype_t::UNSPECIFIED);
329 * fd.io coding-style-patch-verification: ON
332 * eval: (c-set-style "mozilla")