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)
33 rc_t::from_vpp_retval(int32_t rv)
39 // sub interface already exists
43 // interface already exists
47 return (rc_t::INVALID);
50 const rc_t rc_t::UNSET(0, "un-set");
51 const rc_t rc_t::NOOP(1, "no-op");
52 const rc_t rc_t::OK(2, "ok");
53 const rc_t rc_t::INVALID(3, "invalid");
54 const rc_t rc_t::TIMEOUT(4, "timeout");
56 const handle_t handle_t::INVALID(~0);
58 handle_t::handle_t(int value)
69 handle_t::to_string() const
71 return (std::to_string(m_value));
75 handle_t::operator==(const handle_t& other) const
77 return (m_value == other.m_value);
81 handle_t::operator!=(const handle_t& other) const
83 return (!(*this == other));
87 handle_t::operator<(const handle_t& other) const
89 return (m_value < other.m_value);
93 handle_t::value() const
105 operator<<(std::ostream& os, const handle_t& h)
112 mac_address_t::mac_address_t(const uint8_t b[6])
114 std::copy(b, b + 6, std::begin(bytes));
117 mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
119 std::copy(i.begin(), i.end(), std::begin(bytes));
122 mac_address_t::mac_address_t(const std::string& str)
124 std::vector<std::string> parts;
126 boost::split(parts, str, boost::is_any_of(":"));
128 size_t n_bytes = std::min(bytes.size(), parts.size());
130 for (uint32_t ii = 0; ii < n_bytes; ii++) {
131 bytes[ii] = std::stoul(parts[ii], nullptr, 16);
135 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
137 const mac_address_t mac_address_t::ZERO({ 0x0 });
140 mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
142 for (int i = 0; i < 6 && i < len; i++) {
148 mac_address_t::to_string() const
150 std::ostringstream s;
155 for (auto byte : bytes) {
160 s << std::setw(2) << static_cast<unsigned int>(byte);
167 mac_address_t::operator==(const mac_address_t& mac) const
169 return (bytes == mac.bytes);
172 mac_address_t::operator<(const mac_address_t& m) const
174 return (bytes < m.bytes);
178 operator<<(std::ostream& os, const mac_address_t& mac)
180 os << mac.to_string();
185 l2_address_t::l2_address_t(const uint8_t b[8], uint8_t n_bytes)
188 std::copy_n(b, n_bytes, std::begin(bytes));
191 l2_address_t::l2_address_t(std::initializer_list<uint8_t> i)
196 l2_address_t::l2_address_t(const mac_address_t& mac)
199 std::copy(begin(mac.bytes), std::end(mac.bytes), std::begin(bytes));
202 const l2_address_t l2_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
205 const l2_address_t l2_address_t::ZERO({ 0x0 });
208 l2_address_t::to_bytes(uint8_t* array, uint8_t len) const
210 for (uint8_t i = 0; i < bytes.size() && i < len; i++) {
216 l2_address_t::to_mac() const
218 mac_address_t mac({});
220 std::copy_n(bytes.begin(), mac.bytes.size(), mac.bytes.begin());
226 l2_address_t::to_string() const
228 std::ostringstream s;
233 for (auto byte : bytes) {
238 s << std::setw(2) << static_cast<unsigned int>(byte);
245 l2_address_t::operator==(const l2_address_t& l2) const
247 return (bytes == l2.bytes);
251 l2_address_t::operator!=(const l2_address_t& l2) const
253 return (bytes != l2.bytes);
257 operator<<(std::ostream& os, const l2_address_t& l2)
259 os << l2.to_string();
264 const direction_t direction_t::INPUT(1, "input");
265 const direction_t direction_t::OUTPUT(0, "output");
267 direction_t::direction_t(int v, const std::string s)
272 operator<<(std::ostream& os, const direction_t& dir)
274 os << dir.to_string();
278 const ethertype_t ethertype_t::ARP(0x0806, "arp");
279 const ethertype_t ethertype_t::FCOE(0x8906, "fcoe");
280 const ethertype_t ethertype_t::IPV4(0x0800, "ipv4");
281 const ethertype_t ethertype_t::IPV6(0x86DD, "ipv6");
282 const ethertype_t ethertype_t::MAC_SECURITY(0x88E5, "mac-security");
283 const ethertype_t ethertype_t::MPLS_UNICAST(0x8847, "mpls-unicast");
284 const ethertype_t ethertype_t::TRILL(0x22F3, "trill");
285 const ethertype_t ethertype_t::UNSPECIFIED(0x0, "unspecified");
287 ethertype_t::ethertype_t(int v, const std::string s)
293 operator<<(std::ostream& os, const ethertype_t& ether)
295 os << ether.to_string();
300 ethertype_t::from_numeric_val(uint16_t numeric)
302 if (0x0806 == numeric) {
303 return (ethertype_t::ARP);
305 if (0x8906 == numeric) {
306 return (ethertype_t::FCOE);
308 if (0x0800 == numeric) {
309 return (ethertype_t::IPV4);
311 if (0x86DD == numeric) {
312 return (ethertype_t::IPV6);
314 if (0x88E5 == numeric) {
315 return (ethertype_t::MAC_SECURITY);
317 if (0x8847 == numeric) {
318 return (ethertype_t::MPLS_UNICAST);
320 if (0x22F3 == numeric) {
321 return (ethertype_t::TRILL);
324 return (ethertype_t::UNSPECIFIED);
328 operator<<(std::ostream& os, const counter_t& c)
330 os << "[packets: " << c.packets << " bytes:" << c.bytes << "]";
337 * fd.io coding-style-patch-verification: ON
340 * eval: (c-set-style "mozilla")