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 // sub interface already exists
46 // interface already exists
50 return (rc_t::INVALID);
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");
59 const handle_t handle_t::INVALID(~0);
61 handle_t::handle_t(int value)
72 handle_t::to_string() const
74 return (std::to_string(m_value));
78 handle_t::operator==(const handle_t& other) const
80 return (m_value == other.m_value);
84 handle_t::operator!=(const handle_t& other) const
86 return (!(*this == other));
90 handle_t::operator<(const handle_t& other) const
92 return (m_value < other.m_value);
96 handle_t::value() const
108 operator<<(std::ostream& os, const handle_t& h)
115 mac_address_t::mac_address_t(uint8_t b[6])
117 std::copy(b, b + 6, std::begin(bytes));
120 mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
122 std::copy(i.begin(), i.end(), std::begin(bytes));
125 mac_address_t::mac_address_t(const std::string& str)
127 std::vector<std::string> parts;
129 boost::split(parts, str, boost::is_any_of(":"));
131 size_t n_bytes = std::min(bytes.size(), parts.size());
133 for (uint32_t ii = 0; ii < n_bytes; ii++) {
134 bytes[ii] = std::stoul(parts[ii], nullptr, 16);
138 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
140 const mac_address_t mac_address_t::ZERO({ 0x0 });
143 mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
145 for (int i = 0; i < 6 && i < len; i++) {
151 mac_address_t::to_string() const
153 std::ostringstream s;
158 for (auto byte : bytes) {
163 s << std::setw(2) << static_cast<unsigned int>(byte);
170 mac_address_t::operator==(const mac_address_t& mac) const
172 return (bytes == mac.bytes);
175 mac_address_t::operator<(const mac_address_t& m) const
177 return (bytes < m.bytes);
181 operator<<(std::ostream& os, const mac_address_t& mac)
183 os << mac.to_string();
188 l2_address_t::l2_address_t(const uint8_t b[8], uint8_t n_bytes)
191 std::copy_n(b, n_bytes, std::begin(bytes));
194 l2_address_t::l2_address_t(std::initializer_list<uint8_t> i)
199 l2_address_t::l2_address_t(const mac_address_t& mac)
202 std::copy(begin(mac.bytes), std::end(mac.bytes), std::begin(bytes));
205 const l2_address_t l2_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
208 const l2_address_t l2_address_t::ZERO({ 0x0 });
211 l2_address_t::to_bytes(uint8_t* array, uint8_t len) const
213 for (uint8_t i = 0; i < bytes.size() && i < len; i++) {
219 l2_address_t::to_mac() const
221 mac_address_t mac({});
223 std::copy_n(bytes.begin(), mac.bytes.size(), mac.bytes.begin());
229 l2_address_t::to_string() const
231 std::ostringstream s;
236 for (auto byte : bytes) {
241 s << std::setw(2) << static_cast<unsigned int>(byte);
248 l2_address_t::operator==(const l2_address_t& l2) const
250 return (bytes == l2.bytes);
254 l2_address_t::operator!=(const l2_address_t& l2) const
256 return (bytes != l2.bytes);
260 operator<<(std::ostream& os, const l2_address_t& l2)
262 os << l2.to_string();
267 const direction_t direction_t::INPUT(1, "input");
268 const direction_t direction_t::OUTPUT(0, "output");
270 direction_t::direction_t(int v, const std::string s)
275 operator<<(std::ostream& os, const direction_t& dir)
277 os << dir.to_string();
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");
290 ethertype_t::ethertype_t(int v, const std::string s)
296 operator<<(std::ostream& os, const ethertype_t& ether)
298 os << ether.to_string();
303 ethertype_t::from_numeric_val(uint16_t numeric)
305 if (0x0806 == numeric) {
306 return (ethertype_t::ARP);
308 if (0x8906 == numeric) {
309 return (ethertype_t::FCOE);
311 if (0x0800 == numeric) {
312 return (ethertype_t::IPV4);
314 if (0x86DD == numeric) {
315 return (ethertype_t::IPV6);
317 if (0x88E5 == numeric) {
318 return (ethertype_t::MAC_SECURITY);
320 if (0x8847 == numeric) {
321 return (ethertype_t::MPLS_UNICAST);
323 if (0x22F3 == numeric) {
324 return (ethertype_t::TRILL);
327 return (ethertype_t::UNSPECIFIED);
333 * fd.io coding-style-patch-verification: ON
336 * eval: (c-set-style "mozilla")