LCOV - code coverage report
Current view: top level - vnsw/agent/vrouter/ksync - nexthop_ksync.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 662 1170 56.6 %
Date: 2026-11-30 02:00:48 Functions: 23 27 85.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include <sys/types.h>
       7             : #include <net/ethernet.h>
       8             : #include <boost/asio.hpp>
       9             : #include <boost/bind/bind.hpp>
      10             : 
      11             : #include <base/logging.h>
      12             : #include <db/db_entry.h>
      13             : #include <db/db_table.h>
      14             : #include <db/db_table_partition.h>
      15             : #include <ksync/ksync_index.h>
      16             : #include <ksync/ksync_entry.h>
      17             : #include <ksync/ksync_object.h>
      18             : #include <ksync/ksync_netlink.h>
      19             : #include <ksync/ksync_sock.h>
      20             : 
      21             : #include "oper/interface_common.h"
      22             : #include "oper/nexthop.h"
      23             : #include "oper/mirror_table.h"
      24             : #include "oper/tunnel_nh.h"
      25             : #include "vrouter/ksync/nexthop_ksync.h"
      26             : #include "vrouter/ksync/ksync_init.h"
      27             : #include "vr_types.h"
      28             : #include "oper/ecmp_load_balance.h"
      29             : #include "vrouter/ksync/agent_ksync_types.h"
      30             : #include <vrouter/ksync/ksync_agent_sandesh.h>
      31             : 
      32             : using namespace boost::placeholders;
      33             : 
      34         140 : NHKSyncEntry::NHKSyncEntry(NHKSyncObject *obj, const NHKSyncEntry *entry,
      35         140 :                            uint32_t index) :
      36         140 :     KSyncNetlinkDBEntry(index), ksync_obj_(obj), type_(entry->type_),
      37         140 :     vrf_id_(entry->vrf_id_), label_(entry->label_),
      38         140 :     interface_(entry->interface_), sip_(entry->sip_), dip_(entry->dip_),
      39         140 :     sport_(entry->sport_), dport_(entry->dport_), smac_(entry->smac_),
      40         140 :     dmac_(entry->dmac_), rewrite_dmac_(entry->rewrite_dmac_),
      41         140 :     valid_(entry->valid_), policy_(entry->policy_),
      42         140 :     is_mcast_nh_(entry->is_mcast_nh_),
      43         140 :     defer_(entry->defer_), component_nh_list_(entry->component_nh_list_),
      44         140 :     nh_(entry->nh_), vlan_tag_(entry->vlan_tag_),
      45         140 :     is_local_ecmp_nh_(entry->is_local_ecmp_nh_),
      46         140 :     is_bridge_(entry->is_bridge_),
      47         140 :     is_vxlan_routing_(entry->is_vxlan_routing_),
      48         140 :     comp_type_(entry->comp_type_),
      49         140 :     validate_mcast_src_(entry->validate_mcast_src_),
      50         140 :     tunnel_type_(entry->tunnel_type_), prefix_len_(entry->prefix_len_),
      51         140 :     nh_id_(entry->nh_id()),
      52         140 :     component_nh_key_list_(entry->component_nh_key_list_),
      53         140 :     bridge_nh_(entry->bridge_nh_),
      54         140 :     flood_unknown_unicast_(entry->flood_unknown_unicast_),
      55         140 :     ecmp_hash_fieds_(entry->ecmp_hash_fieds_.HashFieldsToUse()),
      56         140 :     pbb_child_nh_(entry->pbb_child_nh_), isid_(entry->isid_),
      57         140 :     pbb_label_(entry->pbb_label_), learning_enabled_(entry->learning_enabled_),
      58         140 :     need_pbb_tunnel_(entry->need_pbb_tunnel_), etree_leaf_(entry->etree_leaf_),
      59         140 :     layer2_control_word_(entry->layer2_control_word_),
      60         140 :     crypt_(entry->crypt_), crypt_path_available_(entry->crypt_path_available_),
      61         140 :     crypt_interface_(entry->crypt_interface_),
      62         140 :     transport_tunnel_type_(entry->transport_tunnel_type_),
      63         140 :     interface_list_(entry->interface_list_),
      64         140 :     encap_valid_list_(entry->encap_valid_list_),
      65         280 :     dmac_list_(entry->dmac_list_) {
      66         140 :     }
      67             : 
      68        1001 : NHKSyncEntry::NHKSyncEntry(NHKSyncObject *obj, const NextHop *nh) :
      69        2002 :     KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj), type_(nh->GetType()),
      70        3003 :     vrf_id_(0), interface_(NULL), dmac_(), valid_(nh->IsValid()),
      71        1001 :     policy_(nh->PolicyEnabled()), is_mcast_nh_(false), nh_(nh),
      72        1001 :     vlan_tag_(VmInterface::kInvalidVlanId), is_bridge_(false),
      73        1001 :     is_vxlan_routing_(false),
      74        1001 :     tunnel_type_(TunnelType::INVALID), prefix_len_(32), nh_id_(nh->id()),
      75        1001 :     bridge_nh_(false), flood_unknown_unicast_(false),
      76        1001 :     learning_enabled_(nh->learning_enabled()), need_pbb_tunnel_(false),
      77        1001 :     etree_leaf_ (false), layer2_control_word_(false),
      78        1001 :     crypt_(false), crypt_path_available_(false), crypt_interface_(NULL),
      79        6006 :     interface_list_(), encap_valid_list_(), dmac_list_() {
      80             : 
      81        1001 :     switch (type_) {
      82          13 :     case NextHop::ARP: {
      83          13 :         const ArpNH *arp = static_cast<const ArpNH *>(nh);
      84          13 :         vrf_id_ = arp->vrf_id();
      85          13 :         sip_ = *(arp->GetIp());
      86          13 :         break;
      87             :     }
      88             : 
      89           0 :     case NextHop::NDP: {
      90           0 :         const NdpNH *ndp = static_cast<const NdpNH *>(nh);
      91           0 :         vrf_id_ = ndp->vrf_id();
      92           0 :         sip_ = *(ndp->GetIp());
      93           0 :         break;
      94             :     }
      95             : 
      96         640 :     case NextHop::INTERFACE: {
      97             :         InterfaceKSyncObject *interface_object =
      98         640 :             ksync_obj_->ksync()->interface_ksync_obj();
      99         640 :         const InterfaceNH *if_nh = static_cast<const InterfaceNH *>(nh);
     100         640 :         InterfaceKSyncEntry if_ksync(interface_object, if_nh->GetInterface());
     101         640 :         interface_ = interface_object->GetReference(&if_ksync);
     102         640 :         assert(interface_);
     103         640 :         is_mcast_nh_ = if_nh->is_multicastNH();
     104         640 :         is_bridge_ = if_nh->IsBridge();
     105         640 :         is_vxlan_routing_ = if_nh->IsVxlanRouting();
     106         640 :         const VrfEntry * vrf = if_nh->GetVrf();
     107         640 :         vrf_id_ = (vrf != NULL) ? vrf->vrf_id() : VrfEntry::kInvalidIndex;
     108         640 :         dmac_ = if_nh->GetDMac();
     109             :         // VmInterface can potentially have vlan-tags. Get tag in such case
     110         640 :         if (if_nh->GetInterface()->type() == Interface::VM_INTERFACE) {
     111         573 :             vlan_tag_ = (static_cast<const VmInterface *>
     112         573 :                          (if_nh->GetInterface()))->tx_vlan_id();
     113             :         }
     114         640 :         break;
     115         640 :     }
     116             : 
     117           0 :     case NextHop::VLAN: {
     118             :         InterfaceKSyncObject *interface_object =
     119           0 :             ksync_obj_->ksync()->interface_ksync_obj();
     120           0 :         const VlanNH *vlan_nh = static_cast<const VlanNH *>(nh);
     121           0 :         InterfaceKSyncEntry if_ksync(interface_object, vlan_nh->GetInterface());
     122           0 :         interface_ = interface_object->GetReference(&if_ksync);
     123           0 :         assert(interface_);
     124           0 :         vlan_tag_ = vlan_nh->GetVlanTag();
     125           0 :         vrf_id_ = vlan_nh->GetVrf()->vrf_id();
     126           0 :         break;
     127           0 :     }
     128             : 
     129          31 :     case NextHop::TUNNEL: {
     130          31 :         const TunnelNH *tunnel = static_cast<const TunnelNH *>(nh);
     131          31 :         vrf_id_ = tunnel->vrf_id();
     132          31 :         sip_ = *(tunnel->GetSip());
     133          31 :         dip_ = *(tunnel->GetDip());
     134          31 :         tunnel_type_ = tunnel->GetTunnelType();
     135          31 :         crypt_ = tunnel->GetCrypt();
     136          31 :         crypt_path_available_ = tunnel->GetCryptTunnelAvailable();
     137             : 
     138          31 :         if (tunnel->GetCryptInterface()) {
     139             :             InterfaceKSyncObject *crypt_interface_object =
     140           0 :                     ksync_obj_->ksync()->interface_ksync_obj();
     141           0 :             InterfaceKSyncEntry if_ksync(crypt_interface_object, tunnel->GetCryptInterface());
     142           0 :             crypt_interface_ = crypt_interface_object->GetReference(&if_ksync);
     143           0 :         }
     144          31 :         rewrite_dmac_ = tunnel->rewrite_dmac();
     145          31 :         if (tunnel_type_.GetType() == TunnelType::MPLS_OVER_MPLS) {
     146           0 :             const LabelledTunnelNH *labelled_tunnel =
     147             :                     static_cast<const LabelledTunnelNH *>(nh);
     148           0 :             label_ = labelled_tunnel->GetTransportLabel();
     149           0 :             transport_tunnel_type_ =
     150           0 :                     labelled_tunnel->GetTransportTunnelType();
     151             :         }
     152          31 :         break;
     153             :     }
     154             : 
     155          32 :     case NextHop::DISCARD: {
     156          32 :         break;
     157             :     }
     158             : 
     159          38 :     case NextHop::L2_RECEIVE: {
     160          38 :         break;
     161             :     }
     162             : 
     163           4 :     case NextHop::RESOLVE: {
     164             :         InterfaceKSyncObject *interface_object =
     165           4 :             ksync_obj_->ksync()->interface_ksync_obj();
     166           4 :         const ResolveNH *rsl_nh = static_cast<const ResolveNH *>(nh);
     167           4 :         InterfaceKSyncEntry if_ksync(interface_object, rsl_nh->get_interface());
     168           4 :         interface_ = interface_object->GetReference(&if_ksync);
     169           4 :         vrf_id_ = rsl_nh->get_interface()->vrf_id();
     170           4 :         if (rsl_nh->get_interface()->type() == Interface::VM_INTERFACE) {
     171             :             const VmInterface *vm_intf =
     172           4 :                 static_cast<const VmInterface *>(rsl_nh->get_interface());
     173           4 :             if (vm_intf->vrf()->forwarding_vrf()) {
     174           4 :                 vrf_id_ = vm_intf->vrf()->forwarding_vrf()->vrf_id();
     175             :             }
     176             :         }
     177           4 :         break;
     178           4 :     }
     179             : 
     180          13 :     case NextHop::VRF: {
     181          13 :         const VrfNH *vrf_nh = static_cast<const VrfNH *>(nh);
     182          13 :         vrf_id_ = vrf_nh->GetVrf()->vrf_id();
     183          13 :         bridge_nh_ = vrf_nh->bridge_nh();
     184          13 :         break;
     185             :     }
     186             : 
     187          13 :     case NextHop::RECEIVE: {
     188             :         InterfaceKSyncObject *interface_object =
     189          13 :             ksync_obj_->ksync()->interface_ksync_obj();
     190          13 :         const ReceiveNH *rcv_nh = static_cast<const ReceiveNH *>(nh);
     191          13 :         InterfaceKSyncEntry if_ksync(interface_object, rcv_nh->GetInterface());
     192          13 :         interface_ = interface_object->GetReference(&if_ksync);
     193          13 :         vrf_id_ = rcv_nh->GetInterface()->vrf_id();
     194          13 :         assert(interface_);
     195          13 :         break;
     196          13 :     }
     197             : 
     198           0 :     case NextHop::MIRROR: {
     199           0 :         const MirrorNH *mirror_nh = static_cast<const MirrorNH *>(nh);
     200           0 :         vrf_id_ = mirror_nh->vrf_id();
     201           0 :         sip_ = *(mirror_nh->GetSip());
     202           0 :         sport_ = mirror_nh->GetSPort();
     203           0 :         dip_ = *(mirror_nh->GetDip());
     204           0 :         dport_ = mirror_nh->GetDPort();
     205           0 :         break;
     206             :     }
     207             : 
     208           0 :     case NextHop::PBB: {
     209           0 :         const PBBNH *pbb_nh = static_cast<const PBBNH *>(nh);
     210           0 :         vrf_id_ = pbb_nh->vrf_id();
     211           0 :         dmac_ = pbb_nh->dest_bmac();
     212           0 :         isid_ = pbb_nh->isid();
     213           0 :         break;
     214             :     }
     215             : 
     216         217 :     case NextHop::COMPOSITE: {
     217         217 :         const CompositeNH *comp_nh = static_cast<const CompositeNH *>(nh);
     218         217 :         component_nh_list_.clear();
     219         217 :         vrf_id_ = comp_nh->vrf()->vrf_id();
     220         217 :         comp_type_ = comp_nh->composite_nh_type();
     221         217 :         validate_mcast_src_ = comp_nh->validate_mcast_src();
     222         217 :         ecmp_hash_fieds_ =
     223         217 :             const_cast<CompositeNH *>(comp_nh)->CompEcmpHashFields().HashFieldsToUse();
     224         217 :         component_nh_key_list_ = comp_nh->component_nh_key_list();
     225             :         ComponentNHList::const_iterator component_nh_it =
     226         217 :             comp_nh->begin();
     227         532 :         while (component_nh_it != comp_nh->end()) {
     228         315 :             const NextHop *component_nh = NULL;
     229         315 :             uint32_t label = 0;
     230         315 :             if (*component_nh_it) {
     231         315 :                 component_nh =  (*component_nh_it)->nh();
     232         315 :                 label = (*component_nh_it)->label();
     233             :             }
     234         315 :             KSyncEntry *ksync_nh = NULL;
     235         315 :             if (component_nh != NULL) {
     236             :                 NHKSyncObject *nh_object =
     237         315 :                     ksync_obj_->ksync()->nh_ksync_obj();
     238         315 :                 NHKSyncEntry nhksync(nh_object, component_nh);
     239         315 :                 ksync_nh = nh_object->GetReference(&nhksync);
     240         315 :             }
     241         315 :             KSyncComponentNH ksync_component_nh(label, ksync_nh);
     242         315 :             component_nh_list_.push_back(ksync_component_nh);
     243         315 :             component_nh_it++;
     244         315 :         }
     245         217 :         break;
     246             :     }
     247             : 
     248           0 :     default:
     249           0 :         assert(0);
     250             :         break;
     251             :     }
     252        1001 : }
     253             : 
     254        1421 : NHKSyncEntry::~NHKSyncEntry() {
     255        1421 : }
     256             : 
     257        1823 : KSyncDBObject *NHKSyncEntry::GetObject() const {
     258        1823 :     return ksync_obj_;
     259             : }
     260             : 
     261        8672 : bool NHKSyncEntry::IsLess(const KSyncEntry &rhs) const {
     262        8672 :     const NHKSyncEntry &entry = static_cast<const NHKSyncEntry &>(rhs);
     263             : 
     264        8672 :     if (type_ != entry.type_) {
     265        2114 :         return type_ < entry.type_;
     266             :     }
     267             : 
     268        6558 :     if (type_ == NextHop::DISCARD) {
     269          64 :         return false;
     270             :     }
     271             : 
     272        6494 :     if (type_ == NextHop::L2_RECEIVE) {
     273          76 :         return false;
     274             :     }
     275             : 
     276        6418 :     if (type_ == NextHop::ARP || type_ == NextHop::NDP) {
     277             :         //Policy is ignored for ARP NH
     278          26 :         if (vrf_id_ != entry.vrf_id_) {
     279           0 :             return vrf_id_ < entry.vrf_id_;
     280             :         }
     281          26 :         return sip_ < entry.sip_;
     282             :     }
     283             : 
     284        6392 :     if (policy_ != entry.policy_) {
     285         831 :         return policy_ < entry.policy_;
     286             :     }
     287             : 
     288        5561 :     if (type_ == NextHop::VRF) {
     289          26 :         if ( vrf_id_ != entry.vrf_id_) {
     290           0 :             return vrf_id_ < entry.vrf_id_;
     291             :         }
     292          26 :         return bridge_nh_ < entry.bridge_nh_;
     293             :     }
     294             : 
     295        5535 :     if (type_ == NextHop::INTERFACE) {
     296        4395 :         if (is_mcast_nh_ != entry.is_mcast_nh_) {
     297         561 :             return is_mcast_nh_ < entry.is_mcast_nh_;
     298             :         }
     299        3834 :         if(is_bridge_ != entry.is_bridge_) {
     300         997 :             return is_bridge_ < entry.is_bridge_;
     301             :         }
     302             : 
     303        2837 :         if(is_vxlan_routing_ != entry.is_vxlan_routing_) {
     304           0 :             return is_vxlan_routing_ < entry.is_vxlan_routing_;
     305             :         }
     306             : 
     307        2837 :         if (dmac_ != entry.dmac_) {
     308        1399 :             return dmac_ < entry.dmac_;
     309             :         }
     310             : 
     311        1438 :         return interface() < entry.interface();
     312             :     }
     313             : 
     314        1140 :     if (type_ == NextHop::RECEIVE || type_ == NextHop::RESOLVE) {
     315          34 :         return interface() < entry.interface();
     316             :     }
     317             : 
     318             : 
     319        1106 :     if (type_ == NextHop::TUNNEL) {
     320         115 :         if (vrf_id_ != entry.vrf_id_) {
     321           0 :             return vrf_id_ < entry.vrf_id_;
     322             :         }
     323             : 
     324         115 :         if (sip_ != entry.sip_) {
     325           0 :             return sip_ < entry.sip_;
     326             :         }
     327             : 
     328         115 :         if (dip_ != entry.dip_) {
     329          37 :             return dip_ < entry.dip_;
     330             :         }
     331             : 
     332          78 :         if (crypt_ != entry.crypt_) {
     333           0 :             return crypt_ < entry.crypt_;
     334             :         }
     335             : 
     336          78 :         if (crypt_path_available_ != entry.crypt_path_available_) {
     337           0 :             return crypt_path_available_ < entry.crypt_path_available_;
     338             :         }
     339             : 
     340          78 :         if (crypt_interface() != entry.crypt_interface()) {
     341           0 :             return crypt_interface() < entry.crypt_interface();
     342             :         }
     343             : 
     344          78 :         if (rewrite_dmac_ != entry.rewrite_dmac_) {
     345           0 :             return rewrite_dmac_ < entry.rewrite_dmac_;
     346             :         }
     347          78 :         if (tunnel_type_.Compare(entry.tunnel_type_) == false) {
     348          16 :             return tunnel_type_.IsLess(entry.tunnel_type_);
     349             :         }
     350          62 :         if (tunnel_type_.GetType() == TunnelType::MPLS_OVER_MPLS) {
     351           0 :             return label_ < entry.label_;
     352             :         }
     353             :     }
     354             : 
     355        1053 :     if (type_ == NextHop::MIRROR) {
     356           0 :         if (vrf_id_ != entry.vrf_id_) {
     357           0 :             return vrf_id_ < entry.vrf_id_;
     358             :         }
     359             : 
     360           0 :         if (dip_ != entry.dip_) {
     361           0 :             return dip_ < entry.dip_;
     362             :         }
     363             : 
     364           0 :         return dport_ < entry.dport_;
     365             :     }
     366             : 
     367        1053 :     if (type_ == NextHop::PBB) {
     368           0 :         if (vrf_id_ != entry.vrf_id_) {
     369           0 :             return vrf_id_ < entry.vrf_id_;
     370             :         }
     371             : 
     372           0 :         if (dmac_ != entry.dmac_) {
     373           0 :             return dmac_ < entry.dmac_;
     374             :         }
     375             : 
     376           0 :         return isid_ < entry.isid_;
     377             :     }
     378             : 
     379        1053 :     if (type_ == NextHop::COMPOSITE) {
     380         991 :         if (comp_type_ != entry.comp_type_) {
     381         281 :             return comp_type_ < entry.comp_type_;
     382             :         }
     383             : 
     384         710 :         if (vrf_id_ != entry.vrf_id_) {
     385           0 :             return vrf_id_ < entry.vrf_id_;
     386             :         }
     387             : 
     388             :         ComponentNHKeyList::const_iterator it =
     389         710 :             component_nh_key_list_.begin();
     390             :         ComponentNHKeyList::const_iterator entry_it =
     391         710 :             entry.component_nh_key_list_.begin();
     392        2458 :         for (;it != component_nh_key_list_.end() &&
     393        3241 :              entry_it != entry.component_nh_key_list_.end(); it++, entry_it++) {
     394         926 :             if (*it == NULL &&
     395           0 :                     *entry_it == NULL) {
     396           0 :                 continue;
     397             :             }
     398             :             //One of the component NH is NULL
     399        1852 :             if ((*it) == NULL ||
     400         926 :                     (*entry_it) == NULL) {
     401           0 :                 return (*it) < (*entry_it);
     402             :             }
     403             : 
     404             :             //Check if the label is different
     405        1852 :             if ((*it)->label() !=
     406         926 :                     (*entry_it)->label()) {
     407           0 :                 return (*it)->label() <
     408           0 :                     (*entry_it)->label();
     409             :             }
     410             : 
     411             :             //Check if the nexthop key is different
     412             :             //Ideally we could find the nexthop and compare pointer alone
     413             :             //it wont work because this is called from Find context itself,
     414             :             //and it would result in deadlock
     415             :             //Hence compare nexthop key alone
     416         926 :             const NextHopKey *left_nh = (*it)->nh_key();
     417         926 :             const NextHopKey *right_nh = (*entry_it)->nh_key();
     418             : 
     419         926 :             if (left_nh->IsEqual(*right_nh) == false) {
     420         143 :                 if (left_nh->GetType() != right_nh->GetType()) {
     421           0 :                     return left_nh->GetType() < right_nh->GetType();
     422             :                 }
     423         143 :                 return left_nh->IsLess(*right_nh);
     424             :             }
     425             : 
     426             :         }
     427             : 
     428        1095 :         if (it == component_nh_key_list_.end() &&
     429        1095 :             entry_it == entry.component_nh_key_list_.end()) {
     430         434 :             return false;
     431             :         }
     432             : 
     433         133 :         if (it == component_nh_key_list_.end()) {
     434          94 :             return true;
     435             :         }
     436          39 :         return false;
     437             :     }
     438             : 
     439          62 :     if (type_ == NextHop::VLAN) {
     440           0 :         if (interface() != entry.interface()) {
     441           0 :             return interface() < entry.interface();
     442             : 
     443             :         }
     444             : 
     445           0 :         return vlan_tag_ < entry.vlan_tag_;
     446             :     }
     447             : 
     448          62 :     return false;
     449             : 
     450             :     assert(0);
     451             : }
     452             : 
     453        3098 : std::string NHKSyncEntry::ToString() const {
     454        3098 :     std::stringstream s;
     455        3098 :     s << "NextHop Index: " << nh_id() << " Type: ";
     456        3098 :     switch(type_) {
     457          54 :     case NextHop::DISCARD: {
     458          54 :         s << "Discard";
     459          54 :         break;
     460             :     }
     461          81 :     case NextHop::L2_RECEIVE: {
     462          81 :         s << "L2-Receive ";
     463          81 :         break;
     464             :     }
     465          30 :     case NextHop::RECEIVE: {
     466          30 :         s << "Receive ";
     467          30 :         break;
     468             :     }
     469           8 :     case NextHop::RESOLVE: {
     470           8 :         s << "Resolve ";
     471           8 :         break;
     472             :     }
     473          25 :     case NextHop::ARP: {
     474          25 :         s << "ARP ";
     475          25 :         break;
     476             :     }
     477           0 :     case NextHop::NDP: {
     478           0 :         s << "NDP ";
     479           0 :         break;
     480             :     }
     481          60 :     case NextHop::VRF: {
     482          60 :         s << "VRF assign to ";
     483             :         const VrfEntry* vrf =
     484          60 :             ksync_obj_->ksync()->agent()->vrf_table()->
     485          60 :             FindVrfFromId(vrf_id_);
     486          60 :         if (vrf) {
     487          53 :             s << vrf->GetName() << " ";
     488             :         } else {
     489           7 :             s << "Invalid ";
     490             :         }
     491          60 :         break;
     492             :     }
     493        1943 :     case NextHop::INTERFACE: {
     494        1943 :         s << "Interface ";
     495        1943 :         break;
     496             :     }
     497         112 :     case NextHop::TUNNEL: {
     498         112 :         s << "Tunnel to ";
     499         112 :         s << dip_.to_string();
     500         112 :         s << "rewrite_dmac";
     501         112 :         s << rewrite_dmac_.ToString();
     502         112 :         break;
     503             :     }
     504           0 :     case NextHop::MIRROR: {
     505           0 :         s << "Mirror to ";
     506           0 :         s << dip_.to_string() << ": " << dport_;
     507           0 :         break;
     508             :     }
     509           0 :     case NextHop::VLAN: {
     510           0 :         s << "VLAN interface ";
     511           0 :         break;
     512             :     }
     513         785 :     case NextHop::COMPOSITE: {
     514         785 :         s << "Composite Child members: ";
     515         785 :         if (component_nh_list_.size() == 0) {
     516          52 :             s << "Empty ";
     517             :         }
     518         785 :         for (KSyncComponentNHList::const_iterator it = component_nh_list_.begin();
     519        1888 :              it != component_nh_list_.end(); it++) {
     520        1103 :             KSyncComponentNH component_nh = *it;
     521        1103 :             if (component_nh.nh()) {
     522        1103 :                 s << component_nh.nh()->ToString();
     523             :             }
     524        1103 :         }
     525         785 :         break;
     526             :     }
     527           0 :     case NextHop::INVALID: {
     528           0 :         s << "Invalid ";
     529             :     }
     530             : 
     531           0 :     case NextHop::PBB: {
     532           0 :         s << "PBB";
     533           0 :         break;
     534             :     }
     535             :     }
     536             : 
     537        3098 :     if (interface_) {
     538        1981 :         s << "<" << interface_->ToString() << ">";
     539             :     }
     540        6196 :     return s.str();
     541        3098 : }
     542             : 
     543         141 : bool NHKSyncEntry::Sync(DBEntry *e) {
     544         141 :     bool ret = false;
     545         141 :     const NextHop *nh = static_cast<NextHop *>(e);
     546             : 
     547         141 :     if (valid_ != nh->IsValid()) {
     548           0 :         valid_ = nh->IsValid();
     549           0 :         ret = true;
     550             :     }
     551             : 
     552         141 :     if (learning_enabled_ != nh->learning_enabled()) {
     553           0 :         learning_enabled_ = nh->learning_enabled();
     554           0 :         ret = true;
     555             :     }
     556             : 
     557         141 :     if (etree_leaf_ != nh->etree_leaf()) {
     558           0 :         etree_leaf_ = nh->etree_leaf();
     559           0 :         ret = true;
     560             :     }
     561             : 
     562         141 :     switch (nh->GetType()) {
     563           2 :     case NextHop::ARP: {
     564             :         InterfaceKSyncObject *interface_object =
     565           2 :             ksync_obj_->ksync()->interface_ksync_obj();
     566           2 :         ArpNH *arp_nh = static_cast<ArpNH *>(e);
     567           2 :         if (dmac_ != arp_nh->GetMac()) {
     568           0 :             dmac_ = arp_nh->GetMac();
     569           0 :             ret = true;
     570             :         }
     571             : 
     572           2 :         if (valid_) {
     573             :             InterfaceKSyncEntry if_ksync(interface_object,
     574           0 :                                          arp_nh->GetInterface());
     575           0 :             if (interface_ != interface_object->GetReference(&if_ksync)) {
     576           0 :                 interface_ = interface_object->GetReference(&if_ksync);
     577           0 :                 ret = true;
     578             :             }
     579           0 :         } else {
     580           2 :             if (interface_ != NULL) {
     581           0 :                 interface_ = NULL;
     582           0 :                 dmac_.Zero();
     583           0 :                 ret = true;
     584             :             }
     585             :         }
     586           2 :         break;
     587             :     }
     588             : 
     589           0 :     case NextHop::NDP: {
     590             :         InterfaceKSyncObject *interface_object =
     591           0 :             ksync_obj_->ksync()->interface_ksync_obj();
     592           0 :         NdpNH *ndp_nh = static_cast<NdpNH *>(e);
     593           0 :         if (dmac_ != ndp_nh->GetMac()) {
     594           0 :             dmac_ = ndp_nh->GetMac();
     595           0 :             ret = true;
     596             :         }
     597             : 
     598           0 :         if (valid_) {
     599             :             InterfaceKSyncEntry if_ksync(interface_object,
     600           0 :                                          ndp_nh->GetInterface());
     601           0 :             if (interface_ != interface_object->GetReference(&if_ksync)) {
     602           0 :                 interface_ = interface_object->GetReference(&if_ksync);
     603           0 :                 ret = true;
     604             :             }
     605           0 :         } else {
     606           0 :             if (interface_ != NULL) {
     607           0 :                 interface_ = NULL;
     608           0 :                 dmac_.Zero();
     609           0 :                 ret = true;
     610             :             }
     611             :         }
     612           0 :         break;
     613             :     }
     614             : 
     615          74 :     case NextHop::INTERFACE: {
     616          74 :         InterfaceNH *intf_nh = static_cast<InterfaceNH *>(e);
     617          74 :         dmac_ = intf_nh->GetDMac();
     618          74 :         uint16_t vlan_tag = VmInterface::kInvalidVlanId;
     619          74 :         if (intf_nh->GetInterface()->type() == Interface::VM_INTERFACE) {
     620          70 :             vlan_tag = vlan_tag_;
     621          70 :             vlan_tag_ = (static_cast<const VmInterface *>
     622          70 :                          (intf_nh->GetInterface()))->tx_vlan_id();
     623          70 :             if (vlan_tag != vlan_tag_) {
     624           0 :                 ret = true;
     625             :             }
     626             :         }
     627          74 :         if (layer2_control_word_ != intf_nh->layer2_control_word()) {
     628           0 :             layer2_control_word_ = intf_nh->layer2_control_word();
     629           0 :             ret = true;
     630             :         }
     631             : 
     632          74 :         const VrfEntry * vrf = intf_nh->GetVrf();
     633          74 :         uint32_t vrf_id = (vrf != NULL)? vrf->vrf_id() :VrfEntry::kInvalidIndex;
     634          74 :         if (vrf_id_ != vrf_id) {
     635           0 :             vrf_id_ = vrf_id;
     636           0 :             ret = true;
     637             :         }
     638          74 :         break;
     639             :     }
     640             : 
     641           1 :     case NextHop::DISCARD: {
     642           1 :         ret = false;
     643           1 :         break;
     644             :     }
     645             : 
     646           1 :     case NextHop::L2_RECEIVE: {
     647           1 :         ret = false;
     648           1 :         break;
     649             :     }
     650             : 
     651          10 :     case NextHop::TUNNEL: {
     652          10 :         ret = true;
     653             : 
     654             :         // If the nh IDs of KSyncEntry and NextHop objects dont match, sync them
     655             :         // along with nh_ pointer. It can happen if the NHKSyncEntry got
     656             :         // created in TEMP state with previous incarnation of this Tunnel NH
     657          10 :         if (nh_id_ != nh->id()) {
     658           0 :             nh_id_ = nh->id();
     659           0 :             nh_ = nh;
     660           0 :             ret = true;
     661             :         }
     662             : 
     663             :         // Invalid nexthop, no valid interface or mac info
     664             :         // present, just return
     665          10 :         if (valid_ == false) {
     666          10 :             if (interface_ != NULL) {
     667           0 :                 interface_ = NULL;
     668           0 :                 dmac_.Zero();
     669           0 :                 ret = true;
     670             :             }
     671          10 :             break;
     672             :         }
     673             : 
     674           0 :         KSyncEntryPtr interface = NULL;
     675           0 :         MacAddress dmac;
     676           0 :         const TunnelNH *tun_nh = static_cast<TunnelNH *>(e);
     677           0 :         const NextHop *active_nh = tun_nh->GetRt()->GetActiveNextHop();
     678             :         // active nexthop can be NULL as when arp rt is delete marked and
     679             :         //  is enqueued for notify.
     680           0 :         if (active_nh == NULL) {
     681           0 :             if (interface_ != NULL) {
     682           0 :                 interface_ = NULL;
     683           0 :                 dmac_.Zero();
     684           0 :                 ret = true;
     685             :             }
     686           0 :             break;
     687             :         }
     688             : 
     689           0 :         if (active_nh->GetType() == NextHop::ARP) {
     690           0 :             const ArpNH *arp_nh = static_cast<const ArpNH *>(active_nh);
     691             :             InterfaceKSyncObject *interface_object =
     692           0 :                 ksync_obj_->ksync()->interface_ksync_obj();
     693           0 :             InterfaceKSyncEntry if_ksync(interface_object, arp_nh->GetInterface());
     694           0 :             interface = interface_object->GetReference(&if_ksync);
     695           0 :             dmac = arp_nh->GetMac();
     696           0 :         } else if (active_nh->GetType() == NextHop::NDP) {
     697           0 :             const NdpNH *ndp_nh = static_cast<const NdpNH *>(active_nh);
     698             :             InterfaceKSyncObject *interface_object =
     699           0 :                 ksync_obj_->ksync()->interface_ksync_obj();
     700           0 :             InterfaceKSyncEntry if_ksync(interface_object, ndp_nh->GetInterface());
     701           0 :             interface = interface_object->GetReference(&if_ksync);
     702           0 :             dmac = ndp_nh->GetMac();
     703           0 :         } else if (active_nh->GetType() == NextHop::INTERFACE) {
     704           0 :             const InterfaceNH *intf_nh =
     705             :                 static_cast<const InterfaceNH *>(active_nh);
     706           0 :             const Interface *oper_intf = intf_nh->GetInterface();
     707             :             InterfaceKSyncObject *interface_object =
     708           0 :                 ksync_obj_->ksync()->interface_ksync_obj();
     709           0 :             InterfaceKSyncEntry if_ksync(interface_object, oper_intf);
     710           0 :             interface = interface_object->GetReference(&if_ksync);
     711           0 :             dmac = oper_intf->mac();
     712           0 :         } else if (active_nh->GetType() == NextHop::COMPOSITE) {
     713           0 :             const CompositeNH *cnh =
     714             :                 static_cast<const CompositeNH *>(active_nh);
     715           0 :             interface_list_.clear();
     716           0 :             encap_valid_list_.clear();
     717           0 :             dmac_list_.clear();
     718             :             ComponentNHList::const_iterator component_nh_it =
     719           0 :                 cnh->begin();
     720           0 :             while (component_nh_it != cnh->end()) {
     721           0 :                 const NextHop *component_nh = NULL;
     722           0 :                 if (*component_nh_it) {
     723           0 :                     component_nh =  (*component_nh_it)->nh();
     724           0 :                     if (component_nh->GetType() == NextHop::ARP) {
     725           0 :                         const ArpNH *arp_nh = dynamic_cast<const ArpNH*>(component_nh);
     726             :                         InterfaceKSyncObject *interface_object =
     727           0 :                             ksync_obj_->ksync()->interface_ksync_obj();
     728           0 :                         InterfaceKSyncEntry if_ksync(interface_object, arp_nh->GetInterface());
     729           0 :                         interface_list_.push_back(interface_object->GetReference(&if_ksync));
     730           0 :                         encap_valid_list_.push_back(arp_nh->IsValid());
     731           0 :                         dmac_list_.push_back(arp_nh->GetMac());
     732           0 :                     }
     733             :                 }
     734           0 :                 component_nh_it++;
     735             :             }
     736             : 
     737             :             /* vRouter expects encap_valid vector should always be size = 3.
     738             :              * It helps in accessing valid enacp_data and oif */
     739           0 :             while (encap_valid_list_.size() < MAX_VR_PHY_INTF) {
     740           0 :                 encap_valid_list_.push_back(false);
     741             :             }
     742             :         }
     743             : 
     744           0 :         bool crypt = tun_nh->GetCrypt();
     745           0 :         if (crypt != crypt_) {
     746           0 :             crypt_ = crypt;
     747           0 :             ret = true;
     748             :         }
     749             : 
     750           0 :         bool crypt_path_available = tun_nh->GetCryptTunnelAvailable();
     751           0 :         if (crypt_path_available != crypt_path_available_) {
     752           0 :             crypt_path_available_ = crypt_path_available;
     753           0 :             ret = true;
     754             :         }
     755             : 
     756           0 :         const Interface *oper_crypt_intf = tun_nh->GetCryptInterface();
     757             : 
     758           0 :         if (tun_nh->GetCryptInterface()) {
     759             :             InterfaceKSyncObject *crypt_interface_object =
     760           0 :                     ksync_obj_->ksync()->interface_ksync_obj();
     761           0 :             InterfaceKSyncEntry if_ksync(crypt_interface_object, oper_crypt_intf);
     762           0 :             KSyncEntryPtr crypt_interface = crypt_interface_object->GetReference(&if_ksync);
     763             : 
     764           0 :             if (crypt_interface != crypt_interface_) {
     765           0 :                 crypt_interface_ = crypt_interface;
     766           0 :                 ret = true;
     767             :             }
     768           0 :         }
     769           0 :         if (tunnel_type_.GetType() == TunnelType::MPLS_OVER_MPLS) {
     770           0 :             const LabelledTunnelNH *labelled_tunnel =
     771             :                     static_cast<const LabelledTunnelNH *>(nh);
     772           0 :             if (label_ != labelled_tunnel->GetTransportLabel()) {
     773           0 :                 label_ = labelled_tunnel->GetTransportLabel();
     774           0 :                 ret = true;
     775             :             }
     776           0 :             if (transport_tunnel_type_ !=
     777           0 :                     labelled_tunnel->GetTransportTunnelType()) {
     778           0 :                 transport_tunnel_type_ =
     779           0 :                     labelled_tunnel->GetTransportTunnelType();
     780           0 :                 ret = true;
     781             :             }
     782             :         }
     783             : 
     784           0 :         if (dmac != dmac_) {
     785           0 :             dmac_ = dmac;
     786           0 :             ret = true;
     787             :         }
     788           0 :         if (interface_ != interface) {
     789           0 :             interface_ = interface;
     790           0 :             ret = true;
     791             :         }
     792           0 :         break;
     793           0 :     }
     794             : 
     795           0 :     case NextHop::MIRROR: {
     796           0 :         ret = true;
     797             :         // Invalid nexthop, no valid interface or mac info
     798             :         // present, just return
     799           0 :         if (valid_ == false || (vrf_id_ == (uint32_t)-1)) {
     800           0 :             if (interface_ != NULL) {
     801           0 :                 interface_ = NULL;
     802           0 :                 dmac_.Zero();
     803           0 :                 ret = true;
     804             :             }
     805           0 :             break;
     806             :         }
     807             : 
     808           0 :         KSyncEntryPtr interface = NULL;
     809           0 :         MacAddress dmac;
     810           0 :         bool valid = valid_;
     811             : 
     812           0 :         const MirrorNH *mirror_nh = static_cast<MirrorNH *>(e);
     813           0 :         const NextHop *active_nh = mirror_nh->GetRt()->GetActiveNextHop();
     814           0 :         if (active_nh) {
     815           0 :             if (active_nh->GetType() == NextHop::ARP) {
     816           0 :                 const ArpNH *arp_nh = static_cast<const ArpNH *>(active_nh);
     817             :                 InterfaceKSyncObject *interface_object =
     818           0 :                     ksync_obj_->ksync()->interface_ksync_obj();
     819             :                 InterfaceKSyncEntry if_ksync(interface_object,
     820           0 :                                             arp_nh->GetInterface());
     821           0 :                 interface = interface_object->GetReference(&if_ksync);
     822           0 :                 dmac = arp_nh->GetMac();
     823           0 :             } else if (active_nh->GetType() == NextHop::NDP) {
     824           0 :                 const NdpNH *ndp_nh = static_cast<const NdpNH *>(active_nh);
     825             :                 InterfaceKSyncObject *interface_object =
     826           0 :                     ksync_obj_->ksync()->interface_ksync_obj();
     827             :                 InterfaceKSyncEntry if_ksync(interface_object,
     828           0 :                                             ndp_nh->GetInterface());
     829           0 :                 interface = interface_object->GetReference(&if_ksync);
     830           0 :                 dmac = ndp_nh->GetMac();
     831           0 :             } else if (active_nh->GetType() == NextHop::RECEIVE) {
     832           0 :                 const ReceiveNH *rcv_nh = static_cast<const ReceiveNH *>(active_nh);
     833             :                 InterfaceKSyncObject *interface_object =
     834           0 :                     ksync_obj_->ksync()->interface_ksync_obj();
     835             :                 InterfaceKSyncEntry if_ksync(interface_object,
     836           0 :                                             rcv_nh->GetInterface());
     837           0 :                 interface = interface_object->GetReference(&if_ksync);
     838           0 :                 Agent *agent = ksync_obj_->ksync()->agent();
     839           0 :                 dmac = agent->vhost_interface()->mac();
     840           0 :             } else if (active_nh->GetType() == NextHop::DISCARD) {
     841           0 :                 valid = false;
     842           0 :                 interface = NULL;
     843             :             }
     844             :         } else {
     845           0 :             valid = false;
     846           0 :             interface = NULL;
     847             :         }
     848             : 
     849           0 :         if (valid_ != valid) {
     850           0 :             valid_ = valid;
     851           0 :             ret = true;
     852             :         }
     853           0 :         if (dmac_ != dmac) {
     854           0 :             dmac_ = dmac;
     855           0 :             ret = true;
     856             :         }
     857           0 :         if (interface_ != interface) {
     858           0 :             interface_ = interface;
     859           0 :             ret = true;
     860             :         }
     861           0 :         break;
     862           0 :     }
     863             : 
     864           0 :     case NextHop::PBB: {
     865           0 :         PBBNH *pbb_nh = static_cast<PBBNH *>(e);
     866           0 :         if (pbb_label_ != pbb_nh->label()) {
     867           0 :             pbb_label_ = pbb_nh->label();
     868           0 :             ret = true;
     869             :         }
     870             : 
     871             :         NHKSyncObject *nh_object =
     872           0 :             ksync_obj_->ksync()->nh_ksync_obj();
     873           0 :         NHKSyncEntry nhksync(nh_object, pbb_nh->child_nh());
     874           0 :         KSyncEntry *ksync_nh = nh_object->GetReference(&nhksync);
     875           0 :         if (ksync_nh != pbb_child_nh_) {
     876           0 :             pbb_child_nh_ = ksync_nh;
     877           0 :             ret = true;
     878             :         }
     879           0 :         break;
     880           0 :     }
     881             : 
     882          46 :     case NextHop::COMPOSITE: {
     883          46 :         ret = true;
     884          46 :         CompositeNH *comp_nh = static_cast<CompositeNH *>(e);
     885          46 :         component_nh_list_.clear();
     886             :         ComponentNHList::const_iterator component_nh_it =
     887          46 :             comp_nh->begin();
     888         107 :         while (component_nh_it != comp_nh->end()) {
     889          61 :             const NextHop *component_nh = NULL;
     890          61 :             uint32_t label = 0;
     891          61 :             if (*component_nh_it) {
     892          61 :                 component_nh =  (*component_nh_it)->nh();
     893          61 :                 label = (*component_nh_it)->label();
     894             :             }
     895          61 :             KSyncEntry *ksync_nh = NULL;
     896          61 :             if (component_nh != NULL) {
     897             :                 NHKSyncObject *nh_object =
     898          61 :                     ksync_obj_->ksync()->nh_ksync_obj();
     899          61 :                 NHKSyncEntry nhksync(nh_object, component_nh);
     900          61 :                 ksync_nh = nh_object->GetReference(&nhksync);
     901          61 :             }
     902          61 :             KSyncComponentNH ksync_component_nh(label, ksync_nh);
     903          61 :             component_nh_list_.push_back(ksync_component_nh);
     904          61 :             component_nh_it++;
     905          61 :         }
     906             : 
     907          46 :         if (comp_nh->EcmpHashFieldInUse() != ecmp_hash_fieds_.HashFieldsToUse()) {
     908           0 :             ecmp_hash_fieds_ = comp_nh->CompEcmpHashFields().HashFieldsToUse();
     909           0 :             ret = true;
     910             :         }
     911             : 
     912          46 :         if (need_pbb_tunnel_ != comp_nh->pbb_nh()) {
     913           0 :             need_pbb_tunnel_ = comp_nh->pbb_nh();
     914           0 :             ret = true;
     915             :         }
     916             : 
     917          46 :         if (layer2_control_word_ != comp_nh->layer2_control_word()) {
     918           0 :             layer2_control_word_ = comp_nh->layer2_control_word();
     919           0 :             ret = true;
     920             :         }
     921          46 :         break;
     922             :     }
     923             : 
     924           0 :     case NextHop::VLAN: {
     925           0 :         VlanNH *vlan_nh = static_cast<VlanNH *>(e);
     926           0 :         smac_ = vlan_nh->GetSMac();
     927           0 :         dmac_ = vlan_nh->GetDMac();
     928           0 :         ret = false;
     929           0 :         break;
     930             :     }
     931             : 
     932           4 :     case NextHop::VRF: {
     933           4 :         VrfNH *vrf_nh = static_cast<VrfNH *>(e);
     934           4 :         vrf_id_ = vrf_nh->GetVrf()->vrf_id();
     935           4 :         ret = false;
     936           4 :         if (bridge_nh_ != vrf_nh->bridge_nh()) {
     937           0 :             bridge_nh_ = vrf_nh->bridge_nh();
     938           0 :             ret = true;
     939             :         }
     940             : 
     941           4 :         if (flood_unknown_unicast_ != vrf_nh->flood_unknown_unicast()) {
     942           0 :             flood_unknown_unicast_ = vrf_nh->flood_unknown_unicast();
     943           0 :             ret = true;
     944             :         }
     945             : 
     946           4 :         if (layer2_control_word_ != vrf_nh->layer2_control_word()) {
     947           0 :             layer2_control_word_ = vrf_nh->layer2_control_word();
     948           0 :             ret = true;
     949             :         }
     950           4 :         break;
     951             :     }
     952           3 :     case NextHop::RECEIVE:
     953             :     case NextHop::RESOLVE: {
     954           3 :         valid_ = true;
     955           3 :         ret = true;
     956           3 :         break;
     957             :     }
     958             : 
     959           0 :     default:
     960           0 :         assert(0);
     961             :     }
     962             : 
     963         141 :     return ret;
     964             : };
     965         280 : int NHKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
     966         280 :     vr_nexthop_req encoder;
     967             :     int encode_len;
     968         280 :     uint32_t crypt_intf_id = kInvalidIndex;
     969         280 :     uint32_t intf_id = kInvalidIndex;
     970         280 :     std::vector<int8_t> encap;
     971         280 :     InterfaceKSyncEntry *if_ksync = NULL;
     972         280 :     InterfaceKSyncEntry *crypt_if_ksync = NULL;
     973         280 :     std::vector<KSyncEntryPtr> if_ksync_list;
     974         280 :     std::vector<int32_t> nhr_encap_valid_list;
     975         280 :     std::vector<int32_t> intf_id_list;
     976         280 :     Agent *agent = ksync_obj_->ksync()->agent();
     977             : 
     978         280 :     encoder.set_h_op(op);
     979         280 :     encoder.set_nhr_id(nh_id());
     980         280 :     if (op == sandesh_op::DEL) {
     981             :         /* For delete only NH-index is required by vrouter */
     982         140 :         int error = 0;
     983         140 :         encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
     984         140 :         assert(error == 0);
     985         140 :         assert(encode_len <= buf_len);
     986         140 :         return encode_len;
     987             :     }
     988         140 :     encoder.set_nhr_rid(0);
     989         140 :     encoder.set_nhr_vrf(vrf_id_);
     990         140 :     encoder.set_nhr_family(AF_INET);
     991         140 :     uint32_t flags = 0;
     992         140 :     if (valid_) {
     993         129 :         flags |= NH_FLAG_VALID;
     994             :     }
     995             : 
     996         140 :     if (policy_) {
     997          31 :         flags |= NH_FLAG_POLICY_ENABLED;
     998             :     }
     999             : 
    1000         140 :     if (etree_leaf_ == false) {
    1001         140 :         flags |= NH_FLAG_ETREE_ROOT;
    1002             :     }
    1003             : 
    1004         140 :     if (learning_enabled_) {
    1005           0 :         flags |= NH_FLAG_MAC_LEARN;
    1006             :     }
    1007             : 
    1008         140 :     if (layer2_control_word_) {
    1009           0 :         flags |= NH_FLAG_L2_CONTROL_DATA;
    1010             :     }
    1011             : 
    1012         140 :     if (crypt_) {
    1013           0 :         flags |= NH_FLAG_CRYPT_TRAFFIC;
    1014             :     }
    1015             : 
    1016         140 :     if_ksync = interface();
    1017         140 :     if_ksync_list = interface_list();
    1018         140 :     nhr_encap_valid_list = encap_valid_list();
    1019         140 :     crypt_if_ksync = crypt_interface();
    1020             : 
    1021         140 :     switch (type_) {
    1022          75 :         case NextHop::VLAN:
    1023             :         case NextHop::ARP:
    1024             :         case NextHop::NDP:
    1025             :         case NextHop::INTERFACE :
    1026          75 :             encoder.set_nhr_type(NH_ENCAP);
    1027          75 :             if (if_ksync) {
    1028          74 :                 intf_id = if_ksync->interface_id();
    1029             :             }
    1030          75 :             encoder.set_nhr_encap_oif_id(std::vector<int32_t>(1, intf_id));
    1031          75 :             encoder.set_nhr_encap_family(ETHERTYPE_ARP);
    1032             : 
    1033          75 :             SetEncap(if_ksync, encap);
    1034          75 :             encoder.set_nhr_encap(encap);
    1035          75 :             encoder.set_nhr_tun_sip(0);
    1036          75 :             encoder.set_nhr_tun_dip(0);
    1037          75 :             if (is_vxlan_routing_) {
    1038             :                 //Set vxlan routing flag
    1039           0 :                 flags |= NH_FLAG_L3_VXLAN;
    1040             :             }
    1041          75 :             if (is_bridge_) {
    1042          28 :                 encoder.set_nhr_family(AF_BRIDGE);
    1043             :             }
    1044          75 :             if (is_mcast_nh_) {
    1045          14 :                 flags |= NH_FLAG_MCAST;
    1046             :             }
    1047          75 :             encoder.set_nhr_flags(flags);
    1048          75 :             break;
    1049             : 
    1050          10 :         case NextHop::TUNNEL : {
    1051          10 :             encoder.set_nhr_type(NH_TUNNEL);
    1052          10 :             encoder.set_nhr_tun_sip(htonl(sip_.to_v4().to_ulong()));
    1053          10 :             encoder.set_nhr_tun_dip(htonl(dip_.to_v4().to_ulong()));
    1054          10 :             encoder.set_nhr_encap_family(ETHERTYPE_ARP);
    1055             : 
    1056          10 :             if (if_ksync_list.empty() == false) {
    1057           0 :                 flags |= NH_FLAG_TUNNEL_UNDERLAY_ECMP;
    1058           0 :                 uint32_t encap_len = 0;
    1059           0 :                 for (size_t i = 0; i < if_ksync_list.size(); ++i) {
    1060             :                     InterfaceKSyncEntry *if_ksync_entry =
    1061           0 :                         static_cast<InterfaceKSyncEntry *>(if_ksync_list[i].get());
    1062           0 :                     intf_id_list.push_back(if_ksync_entry->interface_id());
    1063           0 :                     encap_len = encap.size();
    1064           0 :                     SetEncap(if_ksync_entry, encap, i);
    1065           0 :                     encap_len = encap.size() - encap_len;
    1066             :                 }
    1067           0 :                 encoder.set_nhr_encap_len(encap_len);
    1068             :             } else {
    1069          10 :                 SetEncap(if_ksync, encap);
    1070          10 :                 intf_id_list.push_back(0);
    1071             :             }
    1072          10 :             encoder.set_nhr_encap_oif_id(intf_id_list);
    1073          10 :             encoder.set_nhr_encap_valid(nhr_encap_valid_list);
    1074          10 :             encoder.set_nhr_crypt_traffic(crypt_);
    1075          10 :             encoder.set_nhr_crypt_path_available(crypt_path_available_);
    1076          10 :             if (crypt_if_ksync && crypt_path_available_) {
    1077           0 :                 crypt_intf_id = crypt_if_ksync->interface_id();
    1078             :             }
    1079          10 :             encoder.set_nhr_encap_crypt_oif_id(crypt_intf_id);
    1080          10 :             encoder.set_nhr_encap(encap);
    1081          10 :             if (tunnel_type_.GetType() == TunnelType::MPLS_UDP) {
    1082           0 :                 flags |= NH_FLAG_TUNNEL_UDP_MPLS;
    1083          10 :             } else if (tunnel_type_.GetType() == TunnelType::MPLS_GRE) {
    1084           5 :                 flags |= NH_FLAG_TUNNEL_GRE;
    1085           5 :             } else if (tunnel_type_.GetType() == TunnelType::NATIVE) {
    1086             :                 //Ideally we should have created a new type of
    1087             :                 //indirect nexthop to handle NATIVE encap
    1088             :                 //reusing tunnel NH as it provides most of
    1089             :                 //functionality now
    1090           0 :                 encoder.set_nhr_type(NH_ENCAP);
    1091           0 :                 encoder.set_nhr_encap_oif_id(intf_id_list);
    1092           0 :                 encoder.set_nhr_encap_family(ETHERTYPE_ARP);
    1093           0 :                 encoder.set_nhr_tun_sip(0);
    1094           0 :                 encoder.set_nhr_tun_dip(0);
    1095           5 :             } else if (tunnel_type_.GetType() == TunnelType::MPLS_OVER_MPLS) {
    1096           0 :                 flags |= NH_FLAG_TUNNEL_MPLS_O_MPLS;
    1097           0 :                 if (transport_tunnel_type_ == TunnelType::MPLS_UDP) {
    1098           0 :                     flags |= NH_FLAG_TUNNEL_UDP_MPLS;
    1099             :                 } else {
    1100           0 :                     flags |= NH_FLAG_TUNNEL_GRE;
    1101             :                 }
    1102             : 
    1103           0 :                 encoder.set_nhr_transport_label(label_);
    1104             :             } else {
    1105           5 :                 flags |= NH_FLAG_TUNNEL_VXLAN;
    1106             :             }
    1107          10 :             std::vector<int8_t> rewrite_dmac;
    1108          70 :             for (size_t i = 0 ; i < rewrite_dmac_.size(); i++) {
    1109          60 :                 rewrite_dmac.push_back(rewrite_dmac_[i]);
    1110             :             }
    1111          10 :             encoder.set_nhr_rw_dst_mac(rewrite_dmac);
    1112             :             //TODO remove set_nhr_pbb_mac
    1113          10 :             encoder.set_nhr_pbb_mac(rewrite_dmac);
    1114          10 :             if (rewrite_dmac_.IsZero() == false) {
    1115           0 :                 flags |= NH_FLAG_L3_VXLAN;
    1116             :             }
    1117          10 :             break;
    1118          10 :         }
    1119           0 :         case NextHop::MIRROR :
    1120           0 :             encoder.set_nhr_type(NH_TUNNEL);
    1121           0 :             if (sip_.is_v4() && dip_.is_v4()) {
    1122           0 :                 encoder.set_nhr_tun_sip(htonl(sip_.to_v4().to_ulong()));
    1123           0 :                 encoder.set_nhr_tun_dip(htonl(dip_.to_v4().to_ulong()));
    1124           0 :             } else if (sip_.is_v6() && dip_.is_v6()) {
    1125           0 :                 encoder.set_nhr_family(AF_INET6);
    1126           0 :                 Ip6Address::bytes_type bytes = sip_.to_v6().to_bytes();
    1127           0 :                 std::vector<int8_t> sip_vector(bytes.begin(), bytes.end());
    1128           0 :                 bytes = dip_.to_v6().to_bytes();
    1129           0 :                 std::vector<int8_t> dip_vector(bytes.begin(), bytes.end());
    1130           0 :                 encoder.set_nhr_tun_sip6(sip_vector);
    1131           0 :                 encoder.set_nhr_tun_dip6(dip_vector);
    1132           0 :             }
    1133           0 :             encoder.set_nhr_tun_sport(htons(sport_));
    1134           0 :             encoder.set_nhr_tun_dport(htons(dport_));
    1135           0 :             encoder.set_nhr_encap_family(ETHERTYPE_ARP);
    1136             : 
    1137           0 :             if (if_ksync) {
    1138           0 :                 intf_id = if_ksync->interface_id();
    1139             :             }
    1140           0 :             encoder.set_nhr_encap_oif_id(std::vector<int32_t>(1, intf_id));
    1141           0 :             SetEncap(NULL,encap);
    1142           0 :             encoder.set_nhr_encap(encap);
    1143           0 :             flags |= NH_FLAG_TUNNEL_UDP;
    1144           0 :             if (vrf_id_ != agent->fabric_vrf()->vrf_id()) {
    1145           0 :                 flags |= NH_FLAG_TUNNEL_SIP_COPY;
    1146             :             }
    1147           0 :             break;
    1148             : 
    1149           1 :         case NextHop::L2_RECEIVE:
    1150           1 :             encoder.set_nhr_type(NH_L2_RCV);
    1151           1 :             break;
    1152             : 
    1153           1 :         case NextHop::DISCARD:
    1154           1 :             encoder.set_nhr_type(NH_DISCARD);
    1155           1 :             break;
    1156             : 
    1157           2 :         case NextHop::RECEIVE:
    1158           2 :             if (!policy_) {
    1159           1 :                 flags |= NH_FLAG_RELAXED_POLICY;
    1160             :             }
    1161           2 :             intf_id = if_ksync->interface_id();
    1162           2 :             encoder.set_nhr_encap_oif_id(std::vector<int32_t>(1, intf_id));
    1163           2 :             encoder.set_nhr_type(NH_RCV);
    1164           2 :             break;
    1165             : 
    1166           1 :         case NextHop::RESOLVE:
    1167           1 :             if (policy_) {
    1168             :                 //Policy bit is used in agent to copy over the
    1169             :                 //field to ARP nexthop that gets created from
    1170             :                 //resolve NH in case of gateway interface,
    1171             :                 //but the same is not needed in vrouter.
    1172             :                 //If policy bit is enabled then first packet
    1173             :                 //resulting flow with key NH of resolve NH
    1174             :                 //followed by next packet with ARP NH as key
    1175             :                 //resulting in flow drops
    1176           0 :                 flags &= ~NH_FLAG_POLICY_ENABLED;
    1177             :             }
    1178           1 :             encoder.set_nhr_type(NH_RESOLVE);
    1179           1 :             break;
    1180             : 
    1181           4 :         case NextHop::VRF:
    1182           4 :             encoder.set_nhr_type(NH_VXLAN_VRF);
    1183           4 :             if (bridge_nh_ == true) {
    1184           4 :                 encoder.set_nhr_family(AF_BRIDGE);
    1185             :             }
    1186           4 :             if (flood_unknown_unicast_) {
    1187           0 :                 flags |= NH_FLAG_UNKNOWN_UC_FLOOD;
    1188             :             }
    1189           4 :             break;
    1190             : 
    1191           0 :         case NextHop::PBB: {
    1192           0 :             encoder.set_nhr_family(AF_BRIDGE);
    1193           0 :             encoder.set_nhr_type(NH_TUNNEL);
    1194           0 :             flags |= (NH_FLAG_TUNNEL_PBB | NH_FLAG_INDIRECT);
    1195           0 :             std::vector<int8_t> bmac;
    1196           0 :             for (size_t i = 0 ; i < dmac_.size(); i++) {
    1197           0 :                 bmac.push_back(dmac_[i]);
    1198             :             }
    1199           0 :             encoder.set_nhr_pbb_mac(bmac);
    1200           0 :             if (pbb_child_nh_.get()) {
    1201           0 :                 std::vector<int> sub_nh_list;
    1202             :                 NHKSyncEntry *child_nh =
    1203           0 :                     static_cast<NHKSyncEntry *>(pbb_child_nh_.get());
    1204           0 :                 sub_nh_list.push_back(child_nh->nh_id());
    1205           0 :                 encoder.set_nhr_nh_list(sub_nh_list);
    1206             : 
    1207           0 :                 std::vector<int> sub_label_list;
    1208           0 :                 sub_label_list.push_back(pbb_label_);
    1209           0 :                 encoder.set_nhr_label_list(sub_label_list);
    1210           0 :             }
    1211             :             /*
    1212             :              * This is temp fix to handle CEM-25471
    1213             :              * Currently, vRouter doesn't differentiate PBB and MPLSoGRE/MPLSoUDP
    1214             :              * correctly and it uses encap_oif_id for PBB also which is not needed
    1215             :              * So, sending encap_oif_id as ZERO here in case of PBB so that vRouter
    1216             :              * can work in same way as it was working earlier.
    1217             :              */
    1218           0 :             encoder.set_nhr_encap_oif_id(std::vector<int32_t>(1, 0));
    1219           0 :             break;
    1220           0 :         }
    1221             : 
    1222          46 :         case NextHop::COMPOSITE: {
    1223          46 :             std::vector<int> sub_nh_id;
    1224          46 :             std::vector<int> sub_label_list;
    1225          46 :             std::vector<int> sub_flag_list;
    1226          46 :             encoder.set_nhr_type(NH_COMPOSITE);
    1227          46 :             assert(sip_.is_v4());
    1228          46 :             assert(dip_.is_v4());
    1229             :             /* TODO encoding */
    1230          46 :             encoder.set_nhr_tun_sip(htonl(sip_.to_v4().to_ulong()));
    1231          46 :             encoder.set_nhr_tun_dip(htonl(dip_.to_v4().to_ulong()));
    1232          46 :             encoder.set_nhr_encap_family(ETHERTYPE_ARP);
    1233          46 :             encoder.set_nhr_ecmp_config_hash(SetEcmpFieldsToUse());
    1234             :             /* Proto encode in Network byte order */
    1235          46 :             switch (comp_type_) {
    1236          20 :             case Composite::L2INTERFACE: {
    1237          20 :                 flags |= NH_FLAG_COMPOSITE_ENCAP;
    1238          20 :                 encoder.set_nhr_family(AF_BRIDGE);
    1239          20 :                 break;
    1240             :             }
    1241           0 :             case Composite::L3INTERFACE: {
    1242           0 :                 flags |= NH_FLAG_COMPOSITE_ENCAP;
    1243           0 :                 break;
    1244             :             }
    1245           0 :             case Composite::EVPN: {
    1246           0 :                 flags |= NH_FLAG_COMPOSITE_EVPN;
    1247           0 :                 break;
    1248             :             }
    1249           0 :             case Composite::TOR: {
    1250           0 :                 flags |= NH_FLAG_COMPOSITE_TOR;
    1251           0 :                 break;
    1252             :             }
    1253           0 :             case Composite::FABRIC: {
    1254           0 :                 encoder.set_nhr_family(AF_BRIDGE);
    1255           0 :                 flags |= NH_FLAG_COMPOSITE_FABRIC;
    1256           0 :                 break;
    1257             :             }
    1258           0 :             case Composite::L3FABRIC: {
    1259           0 :                 flags |= NH_FLAG_COMPOSITE_FABRIC;
    1260           0 :                 break;
    1261             :             }
    1262          24 :             case Composite::L2COMP: {
    1263          24 :                 encoder.set_nhr_family(AF_BRIDGE);
    1264          24 :                 flags |= NH_FLAG_MCAST;
    1265          24 :                 if (validate_mcast_src_) {
    1266          24 :                     flags |= NH_FLAG_VALIDATE_MCAST_SRC;
    1267             :                 }
    1268          24 :                 break;
    1269             :             }
    1270           0 :             case Composite::L3COMP: {
    1271           0 :                 flags |= NH_FLAG_MCAST;
    1272           0 :                 if (validate_mcast_src_) {
    1273           0 :                     flags |= NH_FLAG_VALIDATE_MCAST_SRC;
    1274             :                 }
    1275           0 :                 break;
    1276             :             }
    1277           0 :             case Composite::MULTIPROTO: {
    1278           0 :                 encoder.set_nhr_family(AF_UNSPEC);
    1279           0 :                 break;
    1280             :             }
    1281           2 :             case Composite::ECMP:
    1282             :             case Composite::LOCAL_ECMP: {
    1283           2 :                 flags |= NH_FLAG_COMPOSITE_ECMP;
    1284           2 :                 break;
    1285             :             }
    1286           0 :             case Composite::LU_ECMP: {
    1287           0 :                 flags |= NH_FLAG_COMPOSITE_ECMP;
    1288           0 :                 flags |= NH_FLAG_COMPOSITE_LU_ECMP;
    1289           0 :                 break;
    1290             :             }
    1291           0 :             case Composite::INVALID: {
    1292           0 :                 assert(0);
    1293             :             }
    1294             :             }
    1295          46 :             if (need_pbb_tunnel_) {
    1296           0 :                 flags |= NH_FLAG_TUNNEL_PBB;
    1297             :             }
    1298             : 
    1299          46 :             encoder.set_nhr_flags(flags);
    1300          46 :             for (KSyncComponentNHList::iterator it = component_nh_list_.begin();
    1301         107 :                     it != component_nh_list_.end(); it++) {
    1302          61 :                 KSyncComponentNH component_nh = *it;
    1303          61 :                 if (component_nh.nh()) {
    1304          61 :                     sub_nh_id.push_back(component_nh.nh()->nh_id());
    1305          61 :                     sub_label_list.push_back(component_nh.label());
    1306             :                 } else {
    1307           0 :                     sub_nh_id.push_back(CompositeNH::kInvalidComponentNHIdx);
    1308           0 :                     sub_label_list.push_back(MplsTable::kInvalidLabel);
    1309             :                 }
    1310          61 :             }
    1311          46 :             encoder.set_nhr_nh_list(sub_nh_id);
    1312          46 :             encoder.set_nhr_label_list(sub_label_list);
    1313          46 :             break;
    1314          46 :         }
    1315           0 :         default:
    1316           0 :             assert(0);
    1317             :     }
    1318         140 :     encoder.set_nhr_flags(flags);
    1319         140 :     int error = 0;
    1320         140 :     encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
    1321         140 :     assert(error == 0);
    1322         140 :     assert(encode_len <= buf_len);
    1323         140 :     return encode_len;
    1324         280 : }
    1325             : 
    1326         280 : void NHKSyncEntry::FillObjectLog(sandesh_op::type op, KSyncNhInfo &info)
    1327             :     const {
    1328         280 :     info.set_index(nh_id());
    1329             : 
    1330         280 :     if (op == sandesh_op::ADD) {
    1331         140 :         info.set_operation("ADD/CHANGE");
    1332             :     } else {
    1333         140 :         info.set_operation("DELETE");
    1334             :     }
    1335             : 
    1336         280 :     switch(type_) {
    1337           2 :     case NextHop::L2_RECEIVE: {
    1338           2 :         info.set_type("L2-RECEIVE");
    1339           2 :         break;
    1340             :     }
    1341           2 :     case NextHop::DISCARD: {
    1342           2 :         info.set_type("DISCARD");
    1343           2 :         break;
    1344             :     }
    1345             : 
    1346           4 :     case NextHop::RECEIVE: {
    1347           4 :         info.set_type("RECEIVE");
    1348           4 :         break;
    1349             :     }
    1350             : 
    1351           2 :     case NextHop::RESOLVE: {
    1352           2 :         info.set_type("RESOLVE");
    1353           2 :         break;
    1354             :     }
    1355             : 
    1356           2 :     case NextHop::ARP: {
    1357           2 :         info.set_type("ARP");
    1358             :         //Fill dmac and smac???
    1359           2 :         break;
    1360             :     }
    1361             : 
    1362           0 :     case NextHop::NDP: {
    1363           0 :         info.set_type("NDP");
    1364             :         //Fill dmac and smac???
    1365           0 :         break;
    1366             :     }
    1367             : 
    1368           8 :     case NextHop::VRF: {
    1369           8 :         info.set_type("VRF");
    1370           8 :         info.set_vrf(vrf_id_);
    1371           8 :         break;
    1372             :     }
    1373             : 
    1374         148 :     case NextHop::INTERFACE: {
    1375         148 :         info.set_type("INTERFACE");
    1376         148 :         break;
    1377             :     }
    1378             : 
    1379          20 :     case NextHop::TUNNEL: {
    1380          20 :         info.set_type("TUNNEL");
    1381          20 :         info.set_sip(sip_.to_string());
    1382          20 :         info.set_dip(dip_.to_string());
    1383          20 :         info.set_crypt_path_available(crypt_);
    1384             : 
    1385          20 :         if (crypt_interface()) {
    1386           0 :             info.set_crypt_intf_name(crypt_interface()->interface_name());
    1387           0 :             info.set_crypt_out_if_index(crypt_interface()->interface_id());
    1388             :         } else {
    1389          20 :             info.set_crypt_intf_name("NULL");
    1390          20 :             info.set_crypt_out_if_index(kInvalidIndex);
    1391             :         }
    1392          20 :         info.set_rewrite_dmac(rewrite_dmac_.ToString());
    1393          20 :         break;
    1394             :     }
    1395             : 
    1396           0 :     case NextHop::MIRROR: {
    1397           0 :         info.set_type("MIRROR");
    1398           0 :         info.set_vrf(vrf_id_);
    1399           0 :         info.set_sip(sip_.to_string());
    1400           0 :         info.set_dip(dip_.to_string());
    1401           0 :         info.set_sport(sport_);
    1402           0 :         info.set_dport(dport_);
    1403           0 :         break;
    1404             :     }
    1405             : 
    1406           0 :     case NextHop::VLAN: {
    1407           0 :         info.set_type("VLAN");
    1408           0 :         info.set_vlan_tag(vlan_tag_);
    1409           0 :         break;
    1410             :     }
    1411          92 :     case NextHop::COMPOSITE: {
    1412          92 :         info.set_type("Composite");
    1413          92 :         info.set_sub_type(comp_type_);
    1414          92 :         info.set_dip(dip_.to_string());
    1415          92 :         info.set_vrf(vrf_id_);
    1416          92 :         std::vector<KSyncComponentNHLog> sub_nh_list;
    1417          92 :         for (KSyncComponentNHList::const_iterator it =
    1418          92 :                 component_nh_list_.begin();
    1419         214 :              it != component_nh_list_.end(); it++) {
    1420         122 :             KSyncComponentNH component_nh = *it;
    1421         122 :             if (component_nh.nh()) {
    1422         122 :                 KSyncComponentNHLog sub_nh;
    1423         122 :                 sub_nh.set_nh_idx(component_nh.nh()->nh_id());
    1424         122 :                 sub_nh.set_label(component_nh.label());
    1425         122 :                 sub_nh_list.push_back(sub_nh);
    1426         122 :             }
    1427         122 :         }
    1428          92 :         info.set_sub_nh_list(sub_nh_list);
    1429          92 :         break;
    1430          92 :     }
    1431           0 :     default: {
    1432           0 :         return;
    1433             :     }
    1434             :     }
    1435             : 
    1436         280 :     info.set_policy(policy_);
    1437         280 :     info.set_valid(valid_);
    1438         280 :     if (interface()) {
    1439         154 :         info.set_intf_name(interface()->interface_name());
    1440         154 :         info.set_out_if_index(interface()->interface_id());
    1441             :     } else {
    1442         126 :         info.set_intf_name("NULL");
    1443         126 :         info.set_out_if_index(kInvalidIndex);
    1444             :     }
    1445             : }
    1446             : 
    1447         140 : int NHKSyncEntry::AddMsg(char *buf, int buf_len) {
    1448         140 :     KSyncNhInfo info;
    1449         140 :     FillObjectLog(sandesh_op::ADD, info);
    1450         140 :     KSYNC_TRACE(NH, GetObject(), info);
    1451             : 
    1452         280 :     return Encode(sandesh_op::ADD, buf, buf_len);
    1453         140 : }
    1454             : 
    1455           0 : int NHKSyncEntry::ChangeMsg(char *buf, int buf_len){
    1456           0 :     KSyncNhInfo info;
    1457           0 :     FillObjectLog(sandesh_op::ADD, info);
    1458           0 :     KSYNC_TRACE(NH, GetObject(), info);
    1459             : 
    1460           0 :     return Encode(sandesh_op::ADD, buf, buf_len);
    1461           0 : }
    1462             : 
    1463         140 : int NHKSyncEntry::DeleteMsg(char *buf, int buf_len) {
    1464         140 :     KSyncNhInfo info;
    1465         140 :     FillObjectLog(sandesh_op::DEL, info);
    1466         140 :     KSYNC_TRACE(NH, GetObject(), info);
    1467             : 
    1468         280 :     return Encode(sandesh_op::DEL, buf, buf_len);
    1469         140 : }
    1470             : 
    1471         196 : KSyncEntry *NHKSyncEntry::UnresolvedReference() {
    1472         196 :     KSyncEntry *entry = NULL;
    1473         196 :     InterfaceKSyncEntry *if_ksync = interface();
    1474             : 
    1475         196 :     if (valid_ == false) {
    1476             :         //Invalid nexthop has no reference dependency
    1477          11 :         return NULL;
    1478             :     }
    1479             : 
    1480         185 :     switch (type_) {
    1481           0 :     case NextHop::ARP: {
    1482           0 :         assert(if_ksync);
    1483           0 :         if (!if_ksync->IsResolvedAndInSync()) {
    1484           0 :             entry = if_ksync;
    1485             :         }
    1486           0 :         break;
    1487             :     }
    1488             : 
    1489           0 :     case NextHop::NDP: {
    1490           0 :         assert(if_ksync);
    1491           0 :         if (!if_ksync->IsResolvedAndInSync()) {
    1492           0 :             entry = if_ksync;
    1493             :         }
    1494           0 :         break;
    1495             :     }
    1496             : 
    1497          91 :     case NextHop::VLAN:
    1498             :     case NextHop::INTERFACE: {
    1499          91 :         assert(if_ksync);
    1500          91 :         if (!if_ksync->IsResolvedAndInSync()) {
    1501          17 :             entry = if_ksync;
    1502             :         }
    1503          91 :         break;
    1504             :     }
    1505             : 
    1506           1 :     case NextHop::L2_RECEIVE: {
    1507           1 :         assert(if_ksync == NULL);
    1508           1 :         break;
    1509             :     }
    1510             : 
    1511           1 :     case NextHop::DISCARD: {
    1512           1 :         assert(if_ksync == NULL);
    1513           1 :         break;
    1514             :     }
    1515             : 
    1516           0 :     case NextHop::TUNNEL: {
    1517           0 :         if (crypt_path_available_) {
    1518           0 :             InterfaceKSyncEntry *crypt_if_ksync = crypt_interface();
    1519           0 :             assert(crypt_if_ksync);
    1520           0 :             if (!crypt_if_ksync->IsResolvedAndInSync()) {
    1521           0 :                 entry = crypt_if_ksync;
    1522             :             }
    1523             :         }
    1524           0 :         break;
    1525             :     }
    1526             : 
    1527           0 :     case NextHop::MIRROR: {
    1528           0 :         break;
    1529             :     }
    1530             : 
    1531           6 :     case NextHop::RECEIVE: {
    1532           6 :         assert(if_ksync);
    1533           6 :         if (!if_ksync->IsResolvedAndInSync()) {
    1534           4 :             entry = if_ksync;
    1535             :         }
    1536           6 :         break;
    1537             :     }
    1538             : 
    1539           4 :     case NextHop::VRF: {
    1540           4 :         break;
    1541             :     }
    1542             : 
    1543           1 :     case NextHop::RESOLVE: {
    1544           1 :         break;
    1545             :     }
    1546             : 
    1547           0 :     case NextHop::PBB: {
    1548           0 :         if (pbb_child_nh_ && !pbb_child_nh_->IsResolvedAndInSync()) {
    1549           0 :              entry = pbb_child_nh_.get();
    1550             :         }
    1551           0 :         break;
    1552             :     }
    1553             : 
    1554          81 :     case NextHop::COMPOSITE: {
    1555          81 :         for (KSyncComponentNHList::const_iterator it =
    1556          81 :                                 component_nh_list_.begin();
    1557         152 :              it != component_nh_list_.end(); it++) {
    1558         106 :             KSyncComponentNH component_nh = *it;
    1559         106 :             if (component_nh.nh() && !(component_nh.nh()->IsResolvedAndInSync())) {
    1560          35 :                 entry = component_nh.nh();
    1561          35 :                 break;
    1562             :             }
    1563         106 :         }
    1564          81 :         break;
    1565             :     }
    1566             : 
    1567           0 :     default:
    1568           0 :         assert(0);
    1569             :         break;
    1570             :     }
    1571         185 :     return entry;
    1572             : }
    1573             : 
    1574          85 : static bool NeedRewrite(NHKSyncEntry *entry, InterfaceKSyncEntry *if_ksync) {
    1575             : 
    1576             :     // If interface has RAW-IP encapsulation, it doesnt need any rewrite
    1577             :     // information
    1578          85 :     if (if_ksync && if_ksync->encap_type() == PhysicalInterface::RAW_IP) {
    1579           0 :         return false;
    1580             :     }
    1581             : 
    1582             :     // For bridge nexthops, only INTERFACE nexthop has rewrite NH
    1583          85 :     if (entry->is_bridge() == true) {
    1584          28 :         if (entry->type() == NextHop::INTERFACE)
    1585          28 :             return true;
    1586           0 :         return false;
    1587             :     }
    1588             : 
    1589          57 :     return true;
    1590             : }
    1591             : 
    1592          85 : void NHKSyncEntry::SetEncap(InterfaceKSyncEntry *if_ksync,
    1593             :                             std::vector<int8_t> &encap,
    1594             :                             const int32_t index) {
    1595             : 
    1596          85 :     if (NeedRewrite(this, if_ksync) == false)
    1597           0 :         return;
    1598             : 
    1599          85 :     const MacAddress *smac = &MacAddress::ZeroMac();
    1600             :     /* DMAC encode */
    1601          85 :     if (type_ == NextHop::TUNNEL && index != -1) {
    1602           0 :         for (size_t i = 0; i < dmac_list_[index].size(); i++) {
    1603           0 :             encap.push_back(dmac_list_[index][i]);
    1604             :         }
    1605           0 :     } else {
    1606         595 :         for (size_t i = 0 ; i < dmac_.size(); i++) {
    1607         510 :             encap.push_back(dmac_[i]);
    1608             :         }
    1609             :     }
    1610             :     /* SMAC encode */
    1611          85 :     if (type_ == NextHop::VLAN) {
    1612           0 :         smac = &smac_;
    1613          85 :     } else if ((type_ == NextHop::INTERFACE || type_ == NextHop::ARP ||
    1614          85 :                 type_ == NextHop::NDP) && if_ksync) {
    1615          74 :         smac = &if_ksync->mac();
    1616             : 
    1617             :     } else {
    1618          11 :         Agent *agent = ksync_obj_->ksync()->agent();
    1619          11 :         smac = &agent->vhost_interface()->mac();
    1620             :     }
    1621         595 :     for (size_t i = 0 ; i < smac->size(); i++) {
    1622         510 :         encap.push_back((*smac)[i]);
    1623             :     }
    1624             : 
    1625             :     // Add 802.1q header if
    1626             :     //  - Nexthop is of type VLAN
    1627             :     //  - Nexthop is of type INTERFACE and VLAN configured for it
    1628          85 :     if (type_ == NextHop::VLAN ||
    1629          85 :             (type_ == NextHop::INTERFACE &&
    1630          74 :              vlan_tag_ != VmInterface::kInvalidVlanId)) {
    1631           0 :         encap.push_back(0x81);
    1632           0 :         encap.push_back(0x00);
    1633           0 :         encap.push_back((vlan_tag_ & 0xFF00) >> 8);
    1634           0 :         encap.push_back(vlan_tag_ & 0xFF);
    1635             :     }
    1636             :     /* Proto encode in Network byte order */
    1637          85 :     encap.push_back(0x08);
    1638          85 :     encap.push_back(0x00);
    1639             : }
    1640             : 
    1641          46 : uint8_t NHKSyncEntry::SetEcmpFieldsToUse() {
    1642          46 :     uint8_t ecmp_hash_fields_in_use = VR_FLOW_KEY_NONE;
    1643          46 :     uint8_t fields_in_byte = ecmp_hash_fieds_.HashFieldsToUse();
    1644          46 :     if (fields_in_byte & 1 << EcmpLoadBalance::SOURCE_IP) {
    1645          46 :         ecmp_hash_fields_in_use |= VR_FLOW_KEY_SRC_IP;
    1646             :     }
    1647          46 :     if (fields_in_byte & 1 << EcmpLoadBalance::DESTINATION_IP) {
    1648          46 :         ecmp_hash_fields_in_use |= VR_FLOW_KEY_DST_IP;
    1649             :     }
    1650          46 :     if (fields_in_byte & 1 << EcmpLoadBalance::IP_PROTOCOL) {
    1651          46 :         ecmp_hash_fields_in_use |= VR_FLOW_KEY_PROTO;
    1652             :     }
    1653          46 :     if (fields_in_byte & 1 << EcmpLoadBalance::SOURCE_PORT) {
    1654          46 :         ecmp_hash_fields_in_use |= VR_FLOW_KEY_SRC_PORT;
    1655             :     }
    1656          46 :     if (fields_in_byte & 1 << EcmpLoadBalance::DESTINATION_PORT) {
    1657          46 :         ecmp_hash_fields_in_use |= VR_FLOW_KEY_DST_PORT;
    1658             :     }
    1659             : 
    1660          46 :     return ecmp_hash_fields_in_use;
    1661             : 
    1662             : }
    1663             : 
    1664           1 : NHKSyncObject::NHKSyncObject(KSync *ksync) :
    1665           1 :     KSyncDBObject("KSync Nexthop"), ksync_(ksync) {
    1666           1 : }
    1667             : 
    1668           2 : NHKSyncObject::~NHKSyncObject() {
    1669           2 : }
    1670             : 
    1671           1 : void NHKSyncObject::RegisterDBClients() {
    1672           1 :     RegisterDb(ksync_->agent()->nexthop_table());
    1673           1 : }
    1674             : 
    1675         140 : KSyncEntry *NHKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
    1676         140 :     const NHKSyncEntry *nh = static_cast<const NHKSyncEntry *>(entry);
    1677         140 :     NHKSyncEntry *ksync = new NHKSyncEntry(this, nh, index);
    1678         140 :     return static_cast<KSyncEntry *>(ksync);
    1679             : }
    1680             : 
    1681         140 : KSyncEntry *NHKSyncObject::DBToKSyncEntry(const DBEntry *e) {
    1682         140 :     const NextHop *nh = static_cast<const NextHop *>(e);
    1683         140 :     NHKSyncEntry *key = new NHKSyncEntry(this, nh);
    1684         140 :     return static_cast<KSyncEntry *>(key);
    1685             : }
    1686             : 
    1687         280 : void vr_nexthop_req::Process(SandeshContext *context) {
    1688         280 :     AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
    1689         280 :     ioc->NHMsgHandler(this);
    1690         280 : }
    1691             : 
    1692           0 : void NHKSyncEntry::SetKSyncNhListSandeshData(KSyncNhListSandeshData *data) const {
    1693           0 :     data->set_vrf_id(vrf_id_);
    1694           0 :     data->set_label(label_);
    1695           0 :     if (valid_) {
    1696           0 :         data->set_valid("Enable");
    1697             :     } else {
    1698           0 :         data->set_valid("Disable");
    1699             :     }
    1700             : 
    1701           0 :     if (policy_) {
    1702           0 :         data->set_policy("Enable");
    1703             :     } else {
    1704           0 :         data->set_policy("Disable");
    1705             :     }
    1706             : 
    1707           0 :     if (is_mcast_nh_) {
    1708           0 :         data->set_is_mcast_nh("True");
    1709             :     } else {
    1710           0 :         data->set_is_mcast_nh("False");
    1711             :     }
    1712             : 
    1713           0 :     if (defer_) {
    1714           0 :         data->set_defer("Enable");
    1715             :     } else {
    1716           0 :         data->set_defer("Disable");
    1717             :     }
    1718             : 
    1719           0 :     data->set_vlan_tag(vlan_tag_);
    1720             : 
    1721           0 :     if (is_local_ecmp_nh_) {
    1722           0 :         data->set_is_local_ecmp_nh("True");
    1723             :     } else {
    1724           0 :         data->set_is_local_ecmp_nh("False");
    1725             :     }
    1726             : 
    1727           0 :     if (is_bridge_) {
    1728           0 :         data->set_is_bridge("True");
    1729             :     } else {
    1730           0 :         data->set_is_bridge("False");
    1731             :     }
    1732             : 
    1733           0 :     if (bridge_nh_) {
    1734           0 :         data->set_bridge_nh("Enable");
    1735             :     } else {
    1736           0 :         data->set_bridge_nh("Disable");
    1737             :     }
    1738             : 
    1739           0 :     if (flood_unknown_unicast_) {
    1740           0 :         data->set_flood_unknown_unicast("Enable");
    1741             :     } else {
    1742           0 :         data->set_flood_unknown_unicast("Disable");
    1743             :     }
    1744             : 
    1745           0 :     data->set_nh_id(nh_id_);
    1746           0 :     data->set_isid(isid_);
    1747           0 :     return;
    1748             : }
    1749             : 
    1750           0 : bool NHKSyncEntry::KSyncEntrySandesh(Sandesh *sresp) {
    1751           0 :     KSyncNhListResp *resp = static_cast<KSyncNhListResp *> (sresp);
    1752             : 
    1753           0 :     KSyncNhListSandeshData data;
    1754           0 :     SetKSyncNhListSandeshData(&data);
    1755             :     std::vector<KSyncNhListSandeshData> &list =
    1756           0 :             const_cast<std::vector<KSyncNhListSandeshData>&>(resp->get_KSyncNhList_list());
    1757           0 :     list.push_back(data);
    1758             : 
    1759           0 :     return true;
    1760           0 : }
    1761             : 
    1762           0 : void KSyncNhListReq::HandleRequest() const {
    1763           0 :     AgentKsyncSandeshPtr sand(new AgentKsyncNhListSandesh(context()));
    1764             : 
    1765           0 :     sand->DoKsyncSandesh(sand);
    1766             : 
    1767           0 : }

Generated by: LCOV version 1.14