GBP V2
[vpp.git] / src / vpp-api / vom / nat_static.cpp
index 619cc3b..bf8573d 100644 (file)
 
 #include "vom/nat_static.hpp"
 #include "vom/nat_static_cmds.hpp"
+#include "vom/singular_db_funcs.hpp"
 
 namespace VOM {
 singular_db<nat_static::key_t, nat_static> nat_static::m_db;
 nat_static::event_handler nat_static::m_evh;
 
 nat_static::nat_static(const boost::asio::ip::address& inside,
-                       const boost::asio::ip::address_v4& outside)
+                       const boost::asio::ip::address& outside)
   : m_hw(false)
   , m_rd(route_domain::get_default())
   , m_inside(inside)
@@ -31,7 +32,7 @@ nat_static::nat_static(const boost::asio::ip::address& inside,
 
 nat_static::nat_static(const route_domain& rd,
                        const boost::asio::ip::address& inside,
-                       const boost::asio::ip::address_v4& outside)
+                       const boost::asio::ip::address& outside)
   : m_hw(false)
   , m_rd(rd.singular())
   , m_inside(inside)
@@ -52,7 +53,19 @@ nat_static::~nat_static()
   sweep();
 
   // not in the DB anymore.
-  m_db.release(std::make_pair(m_rd->key(), m_outside), this);
+  m_db.release(key(), this);
+}
+
+const nat_static::key_t
+nat_static::key() const
+{
+  return (std::make_pair(m_rd->key(), m_outside));
+}
+
+bool
+nat_static::operator==(const nat_static& n) const
+{
+  return ((key() == n.key()) && (m_inside == n.m_inside));
 }
 
 void
@@ -61,7 +74,10 @@ nat_static::sweep()
   if (m_hw) {
     if (m_inside.is_v4()) {
       HW::enqueue(new nat_static_cmds::delete_44_cmd(
-        m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside));
+        m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside.to_v4()));
+    } else {
+      HW::enqueue(new nat_static_cmds::delete_66_cmd(
+        m_hw, m_rd->table_id(), m_inside.to_v6(), m_outside.to_v6()));
     }
   }
   HW::write();
@@ -73,7 +89,10 @@ nat_static::replay()
   if (m_hw) {
     if (m_inside.is_v4()) {
       HW::enqueue(new nat_static_cmds::create_44_cmd(
-        m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside));
+        m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside.to_v4()));
+    } else {
+      HW::enqueue(new nat_static_cmds::create_66_cmd(
+        m_hw, m_rd->table_id(), m_inside.to_v6(), m_outside.to_v6()));
     }
   }
 }
@@ -87,7 +106,10 @@ nat_static::update(const nat_static& r)
   if (rc_t::OK != m_hw.rc()) {
     if (m_inside.is_v4()) {
       HW::enqueue(new nat_static_cmds::create_44_cmd(
-        m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside));
+        m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside.to_v4()));
+    } else {
+      HW::enqueue(new nat_static_cmds::create_66_cmd(
+        m_hw, m_rd->table_id(), m_inside.to_v6(), m_outside.to_v6()));
     }
   }
 }
@@ -97,8 +119,8 @@ nat_static::to_string() const
 {
   std::ostringstream s;
   s << "nat-static:["
-    << "table:" << m_rd->to_string() << " inside: " << m_inside.to_string()
-    << " outside " << m_outside.to_string() << "]";
+    << "table:" << m_rd->to_string() << " inside:" << m_inside.to_string()
+    << " outside:" << m_outside.to_string() << "]";
 
   return (s.str());
 }
@@ -106,8 +128,13 @@ nat_static::to_string() const
 std::shared_ptr<nat_static>
 nat_static::find_or_add(const nat_static& temp)
 {
-  return (
-    m_db.find_or_add(std::make_pair(temp.m_rd->key(), temp.m_outside), temp));
+  return (m_db.find_or_add(temp.key(), temp));
+}
+
+std::shared_ptr<nat_static>
+nat_static::find(const key_t& key)
+{
+  return (m_db.find(key));
 }
 
 std::shared_ptr<nat_static>
@@ -119,15 +146,7 @@ nat_static::singular() const
 void
 nat_static::dump(std::ostream& os)
 {
-  m_db.dump(os);
-}
-
-std::ostream&
-operator<<(std::ostream& os, const nat_static::key_t& key)
-{
-  os << "[" << key.first << ", " << key.second << "]";
-
-  return (os);
+  db_dump(m_db, os);
 }
 
 nat_static::event_handler::event_handler()
@@ -142,61 +161,57 @@ nat_static::event_handler::handle_replay()
   m_db.replay();
 }
 
-/* void nat_static::populate_i(const client_db::key_t &key, */
-/*                            std::shared_ptr<interface> itf, */
-/*                            const l3_proto_t &proto) */
-/* { */
-/*     /\* */
-/*      * dump VPP current states */
-/*      *\/ */
-/*     std::shared_ptr<nat_static::dump_cmd> cmd = */
-/*         std::make_shared<nat_static::dump_cmd>(nat_static::dump_cmd(itf->handle(),
- * proto)); */
-
-/*     HW::enqueue(cmd); */
-/*     HW::write(); */
-
-/*     for (auto & record : *cmd) */
-/*     { */
-/*         /\* */
-/*          * construct a nat_static from each recieved record. */
-/*          *\/ */
-/*         auto &payload = record.get_payload(); */
-
-/*      mac_address_t mac(payload.mac_address); */
-/*         boost::asio::ip::address ip_addr = from_bytes(payload.is_ipv6, */
-/*                                                       payload.ip_address);
- */
-/*         nat_static n(*itf, mac, ip_addr); */
-
-/*         VOM_LOG(log_level_t::DEBUG) << "nat_static-dump: " */
-/*                                                << itf->to_string() */
-/*                                                << mac.to_string() */
-/*                                                << ip_addr.to_string(); */
-
-/*         /\* */
-/*          * Write each of the discovered interfaces into the OM, */
-/*          * but disable the HW Command q whilst we do, so that no */
-/*          * commands are sent to VPP */
-/*          *\/ */
-/*         OM::commit(key, n); */
-/*     } */
-/* } */
-
 void
 nat_static::event_handler::handle_populate(const client_db::key_t& key)
 {
-  /* auto it = interface::cbegin(); */
+  /*
+   * dump VPP current states
+   */
+  std::shared_ptr<nat_static_cmds::dump_44_cmd> cmd44 =
+    std::make_shared<nat_static_cmds::dump_44_cmd>();
 
-  /* while (it != interface::cend()) */
-  /* { */
-  /*     nat_static::populate_i(key, it->second.lock(), l3_proto_t::IPV4);
- */
-  /*     nat_static::populate_i(key, it->second.lock(), l3_proto_t::IPV6);
- */
+  HW::enqueue(cmd44);
+  HW::write();
+
+  for (auto& record : *cmd44) {
+
+    auto& payload = record.get_payload();
+
+    boost::asio::ip::address inside = from_bytes(0, payload.local_ip_address);
+    boost::asio::ip::address outside =
+      from_bytes(0, payload.external_ip_address);
+    nat_static n(route_domain(payload.vrf_id), inside, outside);
+
+    /*
+     * Write each of the discovered mappings into the OM,
+     * but disable the HW Command q whilst we do, so that no
+     * commands are sent to VPP
+     */
+    OM::commit(key, n);
+  }
+
+  std::shared_ptr<nat_static_cmds::dump_66_cmd> cmd66 =
+    std::make_shared<nat_static_cmds::dump_66_cmd>();
+
+  HW::enqueue(cmd66);
+  HW::write();
 
-  /*     ++it; */
-  /* } */
+  for (auto& record : *cmd66) {
+
+    auto& payload = record.get_payload();
+
+    boost::asio::ip::address inside = from_bytes(1, payload.local_ip_address);
+    boost::asio::ip::address outside =
+      from_bytes(1, payload.external_ip_address);
+    nat_static n(route_domain(payload.vrf_id), inside, outside);
+
+    /*
+     * Write each of the discovered mappings into the OM,
+     * but disable the HW Command q whilst we do, so that no
+     * commands are sent to VPP
+     */
+    OM::commit(key, n);
+  }
 }
 
 dependency_t
@@ -208,7 +223,7 @@ nat_static::event_handler::order() const
 void
 nat_static::event_handler::show(std::ostream& os)
 {
-  m_db.dump(os);
+  db_dump(m_db, os);
 }
 }