GBP V2
[vpp.git] / src / vpp-api / vom / types.cpp
index c362a60..c6093eb 100644 (file)
@@ -18,6 +18,8 @@
 #include <iostream>
 #include <sstream>
 
+#include <boost/algorithm/string.hpp>
+
 #include "vom/types.hpp"
 
 namespace VOM {
@@ -26,9 +28,6 @@ rc_t::rc_t(int v, const std::string s)
   : enum_base<rc_t>(v, s)
 {
 }
-rc_t::~rc_t()
-{
-}
 
 const rc_t&
 rc_t::from_vpp_retval(int32_t rv)
@@ -37,7 +36,11 @@ rc_t::from_vpp_retval(int32_t rv)
     return (rc_t::OK);
   }
   if (-68 == rv) {
-    // interface laready exists
+    // sub interface already exists
+    return (rc_t::OK);
+  }
+  if (-79 == rv) {
+    // interface already exists
     return (rc_t::OK);
   }
 
@@ -47,9 +50,8 @@ rc_t::from_vpp_retval(int32_t rv)
 const rc_t rc_t::UNSET(0, "un-set");
 const rc_t rc_t::NOOP(1, "no-op");
 const rc_t rc_t::OK(2, "ok");
-const rc_t rc_t::INPROGRESS(3, "in-progess");
-const rc_t rc_t::INVALID(4, "invalid");
-const rc_t rc_t::TIMEOUT(5, "timeout");
+const rc_t rc_t::INVALID(3, "invalid");
+const rc_t rc_t::TIMEOUT(4, "timeout");
 
 const handle_t handle_t::INVALID(~0);
 
@@ -93,6 +95,12 @@ handle_t::value() const
   return (m_value);
 }
 
+void
+handle_t::reset()
+{
+  m_value = ~0;
+}
+
 std::ostream&
 operator<<(std::ostream& os, const handle_t& h)
 {
@@ -101,16 +109,6 @@ operator<<(std::ostream& os, const handle_t& h)
   return (os);
 }
 
-mac_address_t::mac_address_t(uint64_t address)
-{
-  uint8_t mac[6];
-
-  std::memcpy(mac, &address, 6);
-  for (int i = 0; i < 6; i++) {
-    bytes[i] = mac[5 - i];
-  }
-}
-
 mac_address_t::mac_address_t(uint8_t b[6])
 {
   std::copy(b, b + 6, std::begin(bytes));
@@ -121,6 +119,19 @@ mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
   std::copy(i.begin(), i.end(), std::begin(bytes));
 }
 
+mac_address_t::mac_address_t(const std::string& str)
+{
+  std::vector<std::string> parts;
+
+  boost::split(parts, str, boost::is_any_of(":"));
+
+  size_t n_bytes = std::min(bytes.size(), parts.size());
+
+  for (uint32_t ii = 0; ii < n_bytes; ii++) {
+    bytes[ii] = std::stoul(parts[ii], nullptr, 16);
+  }
+}
+
 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
 
 const mac_address_t mac_address_t::ZERO({ 0x0 });
@@ -133,23 +144,6 @@ mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
   }
 }
 
-uint64_t
-mac_address_t::to_u64() const
-{
-  uint64_t mac6 = 0;
-  uint8_t* b = reinterpret_cast<uint8_t*>(&mac6);
-
-  // whack hack. the vapi will byte swap.
-  b[2] = bytes[5];
-  b[3] = bytes[4];
-  b[4] = bytes[3];
-  b[5] = bytes[2];
-  b[6] = bytes[1];
-  b[7] = bytes[0];
-
-  return (mac6);
-}
-
 std::string
 mac_address_t::to_string() const
 {
@@ -158,7 +152,6 @@ mac_address_t::to_string() const
 
   s.fill('0');
   s << std::hex;
-  s << "mac:[";
   for (auto byte : bytes) {
     if (first)
       first = false;
@@ -166,7 +159,6 @@ mac_address_t::to_string() const
       s << ":";
     s << std::setw(2) << static_cast<unsigned int>(byte);
   }
-  s << "]";
 
   return (s.str());
 }
@@ -276,8 +268,64 @@ direction_t::direction_t(int v, const std::string s)
   : enum_base(v, s)
 {
 }
+std::ostream&
+operator<<(std::ostream& os, const direction_t& dir)
+{
+  os << dir.to_string();
+  return os;
+}
+
+const ethertype_t ethertype_t::ARP(0x0806, "arp");
+const ethertype_t ethertype_t::FCOE(0x8906, "fcoe");
+const ethertype_t ethertype_t::IPV4(0x0800, "ipv4");
+const ethertype_t ethertype_t::IPV6(0x86DD, "ipv6");
+const ethertype_t ethertype_t::MAC_SECURITY(0x88E5, "mac-security");
+const ethertype_t ethertype_t::MPLS_UNICAST(0x8847, "mpls-unicast");
+const ethertype_t ethertype_t::TRILL(0x22F3, "trill");
+const ethertype_t ethertype_t::UNSPECIFIED(0x0, "unspecified");
+
+ethertype_t::ethertype_t(int v, const std::string s)
+  : enum_base(v, s)
+{
+}
+
+std::ostream&
+operator<<(std::ostream& os, const ethertype_t& ether)
+{
+  os << ether.to_string();
+  return os;
+}
+
+const ethertype_t&
+ethertype_t::from_numeric_val(uint16_t numeric)
+{
+  if (0x0806 == numeric) {
+    return (ethertype_t::ARP);
+  }
+  if (0x8906 == numeric) {
+    return (ethertype_t::FCOE);
+  }
+  if (0x0800 == numeric) {
+    return (ethertype_t::IPV4);
+  }
+  if (0x86DD == numeric) {
+    return (ethertype_t::IPV6);
+  }
+  if (0x88E5 == numeric) {
+    return (ethertype_t::MAC_SECURITY);
+  }
+  if (0x8847 == numeric) {
+    return (ethertype_t::MPLS_UNICAST);
+  }
+  if (0x22F3 == numeric) {
+    return (ethertype_t::TRILL);
+  }
+
+  return (ethertype_t::UNSPECIFIED);
 }
 
+}; // namespace VOM
+
 /*
  * fd.io coding-style-patch-verification: ON
  *