LCOV - code coverage report
Current view: top level - vnsw/agent/vrouter/ksync - vrf_assign_ksync.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 8 117 6.8 %
Date: 2026-08-03 02:19:58 Functions: 4 22 18.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #include <boost/asio.hpp>
       6             : #include <boost/bind/bind.hpp>
       7             : 
       8             : #include <base/logging.h>
       9             : #include <db/db_entry.h>
      10             : #include <db/db_table.h>
      11             : #include <db/db_table_partition.h>
      12             : #include <ksync/ksync_index.h>
      13             : #include <ksync/ksync_entry.h>
      14             : #include <ksync/ksync_object.h>
      15             : #include <ksync/ksync_netlink.h>
      16             : #include <ksync/ksync_sock.h>
      17             : #include "oper/interface_common.h"
      18             : #include "oper/vrf.h"
      19             : #include "oper/vrf_assign.h"
      20             : #include "oper/mirror_table.h"
      21             : #include "vrouter/ksync/interface_ksync.h"
      22             : #include "vrouter/ksync/nexthop_ksync.h"
      23             : #include "vrouter/ksync/vrf_assign_ksync.h"
      24             : #include "ksync_init.h"
      25             : 
      26             : using namespace boost::placeholders;
      27             : 
      28           0 : VrfAssignKSyncEntry::VrfAssignKSyncEntry(VrfAssignKSyncObject* obj,
      29             :                                          const VrfAssignKSyncEntry *entry,
      30           0 :                                          uint32_t index) :
      31           0 :     KSyncNetlinkDBEntry(index), ksync_obj_(obj), interface_(entry->interface_),
      32           0 :     vlan_tag_(entry->vlan_tag_), vrf_id_(entry->vrf_id_),
      33           0 :     nh_(entry->nh_) {
      34           0 : }
      35             : 
      36           0 : VrfAssignKSyncEntry::VrfAssignKSyncEntry(VrfAssignKSyncObject* obj,
      37           0 :                                          const VrfAssign *vassign) :
      38           0 :     KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj) {
      39             : 
      40             :     InterfaceKSyncObject *intf_object =
      41           0 :         ksync_obj_->ksync()->interface_ksync_obj();
      42           0 :     InterfaceKSyncEntry intf(intf_object, vassign->GetInterface());
      43             : 
      44             :     interface_ = static_cast<InterfaceKSyncEntry *>
      45           0 :         (intf_object->GetReference(&intf));
      46           0 :     assert(interface_);
      47             : 
      48           0 :     assert(vassign->GetType() == VrfAssign::VLAN);
      49           0 :     const VlanVrfAssign *vlan = static_cast<const VlanVrfAssign *>(vassign);
      50           0 :     vlan_tag_ = vlan->GetVlanTag();
      51             : 
      52           0 :     if (vassign->GetVrf()) {
      53           0 :         vrf_id_ = vassign->GetVrf()->vrf_id();
      54             :     } else {
      55           0 :         vrf_id_ = VIF_VRF_INVALID;
      56             :     }
      57           0 : }
      58             : 
      59           0 : VrfAssignKSyncEntry::~VrfAssignKSyncEntry() {
      60           0 : }
      61             : 
      62           0 : KSyncDBObject *VrfAssignKSyncEntry::GetObject() const {
      63           0 :     return ksync_obj_;
      64             : }
      65             : 
      66           0 : bool VrfAssignKSyncEntry::IsLess(const KSyncEntry &rhs) const {
      67           0 :     const VrfAssignKSyncEntry &entry =
      68             :         static_cast<const VrfAssignKSyncEntry &>(rhs);
      69             : 
      70           0 :     if (interface() != entry.interface()) {
      71           0 :         return interface() < entry.interface();
      72             :     }
      73             : 
      74           0 :     return vlan_tag() < entry.vlan_tag();
      75             : }
      76             : 
      77           0 : std::string VrfAssignKSyncEntry::ToString() const {
      78           0 :     std::stringstream s;
      79           0 :     InterfaceKSyncEntry *intf = interface();
      80             : 
      81           0 :     s << "VRF Assign : ";
      82           0 :     if (intf) {
      83           0 :         s << "Interface : " << intf->interface_name() <<
      84             :              " Intf-Service-Vlan : " <<
      85           0 :              (intf->has_service_vlan() == true ? "Enable" : "Disable");
      86             :     } else {
      87           0 :         s << "Interface : <NULL> ";
      88             :     }
      89             : 
      90           0 :     s << " Tag : " << vlan_tag();
      91             :     const VrfEntry* vrf =
      92           0 :         ksync_obj_->ksync()->agent()->vrf_table()->FindVrfFromId(vrf_id_);
      93           0 :     if (vrf) {
      94           0 :         s << " Vrf : " << vrf->GetName();
      95             :     }
      96             : 
      97           0 :     return s.str();
      98           0 : }
      99             : 
     100           0 : bool VrfAssignKSyncEntry::Sync(DBEntry *e) {
     101           0 :     bool ret = false;
     102           0 :     const VrfAssign *vassign = static_cast<VrfAssign *>(e);
     103           0 :     uint32_t vrf_id = 0;
     104             : 
     105           0 :     if (vassign->GetVrf()) {
     106           0 :         vrf_id = vassign->GetVrf()->vrf_id();
     107             :     } else {
     108           0 :         vrf_id = VIF_VRF_INVALID;
     109             :     }
     110             : 
     111           0 :     if (vrf_id_ != vrf_id) {
     112           0 :         vrf_id_ = vrf_id;
     113           0 :         ret = true;
     114             :     }
     115             : 
     116           0 :     const VlanVrfAssign *vlan = static_cast<const VlanVrfAssign *>(vassign);
     117           0 :     vlan_tag_ = vlan->GetVlanTag();
     118             :     NHKSyncObject *nh_object =
     119           0 :         ksync_obj_->ksync()->nh_ksync_obj();
     120           0 :     NHKSyncEntry nh(nh_object, vlan->nh());
     121           0 :     if (nh_ != static_cast<NHKSyncEntry*>(nh_object->GetReference(&nh))) {
     122           0 :         nh_ = static_cast<NHKSyncEntry*>(nh_object->GetReference(&nh));
     123           0 :         ret = true;
     124             :     }
     125             : 
     126           0 :     return ret;
     127           0 : }
     128             : 
     129           0 : InterfaceKSyncEntry *VrfAssignKSyncEntry::interface() const {
     130           0 :     return static_cast<InterfaceKSyncEntry *>(interface_.get());
     131             : }
     132             : 
     133           0 : NHKSyncEntry *VrfAssignKSyncEntry::nh() const {
     134           0 :     return static_cast<NHKSyncEntry *>(nh_.get());
     135             : }
     136             : 
     137           0 : int VrfAssignKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
     138           0 :     vr_vrf_assign_req encoder;
     139             :     int encode_len;
     140           0 :     InterfaceKSyncEntry *intf = interface();
     141             : 
     142           0 :     encoder.set_h_op(op);
     143           0 :     encoder.set_var_vif_index(intf->interface_id());
     144           0 :     encoder.set_var_vlan_id(vlan_tag_);
     145           0 :     encoder.set_var_vif_vrf(vrf_id_);
     146           0 :     encoder.set_var_nh_id(nh()->nh_id());
     147           0 :     int error = 0;
     148           0 :     encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
     149           0 :     assert(error == 0);
     150           0 :     assert(encode_len <= buf_len);
     151           0 :     LOG(DEBUG, "VRF Assign for Interface <" << intf->interface_name() <<
     152             :         "> Tag <" << vlan_tag() << "> Vrf <" << vrf_id_ << ">");
     153           0 :     return encode_len;
     154           0 : }
     155             : 
     156           0 : int VrfAssignKSyncEntry::AddMsg(char *buf, int buf_len) {
     157           0 :     LOG(DEBUG, "VrfAssign: Add");
     158           0 :     return Encode(sandesh_op::ADD, buf, buf_len);
     159             : }
     160             : 
     161           0 : int VrfAssignKSyncEntry::ChangeMsg(char *buf, int buf_len){
     162           0 :     return AddMsg(buf, buf_len);
     163             : }
     164             : 
     165           0 : int VrfAssignKSyncEntry::DeleteMsg(char *buf, int buf_len) {
     166           0 :     LOG(DEBUG, "VrfAssign: Delete");
     167           0 :     return Encode(sandesh_op::DEL, buf, buf_len);
     168             : }
     169             : 
     170           0 : KSyncEntry *VrfAssignKSyncEntry::UnresolvedReference() {
     171           0 :     InterfaceKSyncEntry *intf = interface();
     172           0 :     if (!intf->IsResolvedAndInSync()) {
     173           0 :         return intf;
     174             :     }
     175           0 :     NHKSyncEntry *ksync_nh = nh();
     176           0 :     if (!ksync_nh->IsResolvedAndInSync()) {
     177           0 :         return ksync_nh;
     178             :     }
     179           0 :     return NULL;
     180             : }
     181             : 
     182           3 : VrfAssignKSyncObject::VrfAssignKSyncObject(KSync *ksync)
     183           3 :     : KSyncDBObject("KSync VrfAssign"), ksync_(ksync) {
     184           3 : }
     185             : 
     186           6 : VrfAssignKSyncObject::~VrfAssignKSyncObject() {
     187           6 : }
     188             : 
     189           3 : void VrfAssignKSyncObject::RegisterDBClients() {
     190           3 :     RegisterDb(ksync_->agent()->vrf_assign_table());
     191           3 : }
     192             : 
     193           0 : KSyncEntry *VrfAssignKSyncObject::Alloc(const KSyncEntry *ke, uint32_t index) {
     194           0 :     const VrfAssignKSyncEntry *rule =
     195             :         static_cast<const VrfAssignKSyncEntry *>(ke);
     196           0 :     VrfAssignKSyncEntry *ksync = new VrfAssignKSyncEntry(this, rule, index);
     197           0 :     return static_cast<KSyncEntry *>(ksync);
     198             : }
     199             : 
     200           0 : KSyncEntry *VrfAssignKSyncObject::DBToKSyncEntry(const DBEntry *e) {
     201           0 :     const VrfAssign *rule = static_cast<const VrfAssign *>(e);
     202           0 :     VrfAssignKSyncEntry *key = new VrfAssignKSyncEntry(this, rule);
     203           0 :     return static_cast<KSyncEntry *>(key);
     204             : }
     205             : 
     206           0 : void vr_vrf_assign_req::Process(SandeshContext *context) {
     207           0 :     AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
     208           0 :     ioc->VrfAssignMsgHandler(this);
     209           0 : }

Generated by: LCOV version 1.14