LCOV - code coverage report
Current view: top level - ksync - ksync_sock_user.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 393 1215 32.3 %
Date: 2026-11-09 02:10:49 Functions: 41 116 35.3 %
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 <sys/socket.h>
       8             : #if defined(__linux__)
       9             : #include <linux/netlink.h>
      10             : #include <linux/rtnetlink.h>
      11             : #include <linux/genetlink.h>
      12             : #include <linux/sockios.h>
      13             : #endif
      14             : 
      15             : #include <boost/bind/bind.hpp>
      16             : 
      17             : #include <base/logging.h>
      18             : #include <base/address_util.h>
      19             : #include <db/db.h>
      20             : #include <db/db_entry.h>
      21             : #include <db/db_table.h>
      22             : #include <db/db_table_partition.h>
      23             : 
      24             : #include "ksync_index.h"
      25             : #include "ksync_entry.h"
      26             : #include "ksync_object.h"
      27             : #include "ksync_sock.h"
      28             : #include "ksync_sock_user.h"
      29             : 
      30             : #include "nl_util.h"
      31             : #include "vr_genetlink.h"
      32             : #include "vr_message.h"
      33             : #include "vr_types.h"
      34             : #include "vr_defs.h"
      35             : #include "vr_interface.h"
      36             : #include <vector>
      37             : 
      38             : using namespace boost::placeholders;
      39             : 
      40             : KSyncSockTypeMap *KSyncSockTypeMap::singleton_;
      41             : vr_flow_entry *KSyncSockTypeMap::flow_table_;
      42             : int KSyncSockTypeMap::error_code_;
      43             : using namespace boost::asio;
      44             : 
      45       11251 : int EncodeVrResponse(uint8_t *buf, int buf_len, uint32_t seq_num, int code) {
      46       11251 :     vr_response encoder;
      47       11251 :     int error = 0;
      48             : 
      49       11251 :     encoder.set_h_op(sandesh_op::RESPONSE);
      50       11251 :     encoder.set_resp_code(code);
      51       22502 :     return encoder.WriteBinary(buf, buf_len, &error);
      52       11251 : }
      53             : 
      54       11251 : void KSyncSockTypeMap::AddNetlinkTxBuff(struct nl_client *cl) {
      55       11251 :     tx_buff_list_.push_back(*cl);
      56       11251 : }
      57             : 
      58             : //process sandesh messages that are being sent from the agent
      59             : //this is used to store a local copy of what is being send to kernel
      60             : //Also handles bulk request messages.
      61       11114 : void KSyncSockTypeMap::ProcessSandesh(const uint8_t *parse_buf, size_t buf_len,
      62             :                                       KSyncUserSockContext *ctx) {
      63             :     int decode_len;
      64             :     uint8_t *decode_buf;
      65             : 
      66             :     // Ensure that tx_buff_list is empty
      67       11114 :     assert(tx_buff_list_.size() == 0);
      68             : 
      69             :     //parse sandesh
      70       11114 :     int err = 0;
      71       11114 :     int decode_buf_len = buf_len;
      72       11114 :     decode_buf = (uint8_t *)(parse_buf);
      73       22365 :     while(decode_buf_len > 0) {
      74       11251 :         decode_len = Sandesh::ReceiveBinaryMsgOne(decode_buf, decode_buf_len,
      75             :                                                   &err, ctx);
      76       11251 :         if (decode_len < 0) {
      77           0 :             LOG(DEBUG, "Incorrect decode len " << decode_len);
      78           0 :             break;
      79             :         }
      80       11251 :         decode_buf += decode_len;
      81       11251 :         decode_buf_len -= decode_len;
      82             :     }
      83             : 
      84       11114 :     PurgeTxBuffer();
      85       11114 : }
      86             : 
      87       11114 : void KSyncSockTypeMap::PurgeTxBuffer() {
      88             :     // All responses are stored in tx_buff_list_
      89             :     // Make io-vector of all responses and transmit them
      90             :     // If there are more than one responses, they are sent as NETLINK MULTI
      91             :     // messages
      92       11114 :     uint32_t count = 0;
      93       11114 :     KSyncBufferList iovec;
      94       11114 :     struct nlmsghdr *last_nlh = NULL;
      95       11114 :     std::vector<struct nl_client>::iterator it = tx_buff_list_.begin();
      96             :     // Add all messages to to io-vector.
      97       22365 :     while (it != tx_buff_list_.end()) {
      98       11251 :         struct nl_client *cl = &(*it);
      99       11251 :         struct nlmsghdr *nlh = (struct nlmsghdr *)(cl->cl_buf);
     100             :         // Set MULTI flag by default. It will be reset for last buffer later
     101       11251 :         nlh->nlmsg_flags |= NLM_F_MULTI;
     102       11251 :         last_nlh = nlh;
     103       11251 :         iovec.push_back(buffer(cl->cl_buf, cl->cl_msg_len));
     104       11251 :         it++;
     105       11251 :         count++;
     106             :     }
     107             : 
     108             :     // If there are more than one NETLINK messages, we need to add
     109             :     struct nlmsghdr nlh;
     110       11114 :     if (count > 1) {
     111             :         //Send Netlink-Done message NLMSG_DONE at end
     112          63 :         InitNetlinkDoneMsg(&nlh, last_nlh->nlmsg_seq);
     113          63 :         iovec.push_back(buffer((uint8_t *)&nlh, NLMSG_HDRLEN));
     114             :     } else {
     115             :         // Single buffer. Reset the MULTI flag
     116       11051 :         if (last_nlh)
     117       11051 :             last_nlh->nlmsg_flags &= (~NLM_F_MULTI);
     118             :     }
     119             : 
     120             :     // Send a message for each entry in io-vector
     121       11114 :     KSyncBufferList::iterator iovec_it = iovec.begin();
     122       22428 :     while (iovec_it != iovec.end()) {
     123       11314 :          sock_.send_to(*iovec_it, local_ep_);
     124       11314 :          iovec_it++;
     125             :     }
     126             : 
     127             :     // Free the buffers
     128       11114 :     it = tx_buff_list_.begin();
     129       22365 :     while (it != tx_buff_list_.end()) {
     130       11251 :         struct nl_client *cl = &(*it);
     131       11251 :         nl_free(cl);
     132       11251 :         *it++;
     133             :     }
     134       11114 :     tx_buff_list_.clear();
     135       11114 : }
     136             : 
     137         378 : void KSyncSockTypeMap::FlowNatResponse(uint32_t seq_num, vr_flow_req *req , int code) {
     138         378 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     139         378 :     int flow_error = sock->GetKSyncError(KSyncSockTypeMap::KSYNC_FLOW_ENTRY_TYPE);
     140             :     struct nl_client cl;
     141         378 :     int error = 0, ret;
     142         378 :     uint8_t *buf = NULL;
     143         378 :     uint32_t buf_len = 0, encode_len = 0;
     144             :     struct nlmsghdr *nlh;
     145             : 
     146         378 :     nl_init_generic_client_req(&cl, KSyncSock::GetNetlinkFamilyId());
     147         378 :     if ((ret = nl_build_header(&cl, &buf, &buf_len)) < 0) {
     148           0 :         LOG(DEBUG, "Error creating interface DUMP message : " << ret);
     149           0 :         nl_free(&cl);
     150           0 :         return;
     151             :     }
     152             : 
     153         378 :     nlh = (struct nlmsghdr *)cl.cl_buf;
     154         378 :     nlh->nlmsg_seq = seq_num;
     155             : 
     156         378 :     uint32_t fwd_flow_idx = req->get_fr_index();
     157         378 :     bool add_error = false;
     158         378 :     if (fwd_flow_idx == 0xFFFFFFFF) {
     159           0 :         add_error = true;
     160             :     } else {
     161         378 :         if (flow_error != -ENOSPC && flow_error != 0) {
     162           0 :             add_error = true;
     163             :         }
     164         378 :         if (code != 0) {
     165           0 :             flow_error = code;
     166           0 :             add_error = true;
     167             :         }
     168             :     }
     169         378 :     if (add_error) {
     170           0 :         encode_len = EncodeVrResponse(buf, buf_len, seq_num, flow_error);
     171             :     } else {
     172         378 :         encode_len = EncodeVrResponse(buf, buf_len, seq_num, 0);
     173             :     }
     174             : 
     175         378 :     buf += encode_len;
     176         378 :     buf_len -= encode_len;
     177             : 
     178         378 :     vr_flow_response resp;
     179         378 :     resp.set_fresp_op(flow_op::FLOW_SET);
     180         378 :     resp.set_fresp_flags(req->get_fr_flags());
     181         378 :     resp.set_fresp_index(req->get_fr_index());
     182         378 :     resp.set_fresp_gen_id(req->get_fr_gen_id());
     183         378 :     resp.set_fresp_bytes(0);
     184         378 :     resp.set_fresp_packets(0);
     185         378 :     resp.set_fresp_stats_oflow(0);
     186             : 
     187         378 :     encode_len += resp.WriteBinary(buf, buf_len, &error);
     188         378 :     if (error != 0) {
     189           0 :         SimulateResponse(seq_num, -ENOENT, 0);
     190           0 :         nl_free(&cl);
     191           0 :         return;
     192             :     }
     193             : 
     194         378 :     nl_update_header(&cl, encode_len);
     195         378 :     sock->AddNetlinkTxBuff(&cl);
     196         378 : }
     197             : 
     198          63 : void KSyncSockTypeMap::InitNetlinkDoneMsg(struct nlmsghdr *nlh,
     199             :                                           uint32_t seq_num) {
     200          63 :     nlh->nlmsg_seq = seq_num;
     201          63 :     nlh->nlmsg_type = NLMSG_DONE;
     202          63 :     nlh->nlmsg_len = NLMSG_HDRLEN;
     203          63 :     nlh->nlmsg_flags = 0;
     204          63 : }
     205             : 
     206       10871 : void KSyncSockTypeMap::SimulateResponse(uint32_t seq_num, int code, int flags) {
     207             :     struct nl_client cl;
     208             :     int encode_len, ret;
     209             :     uint8_t *buf;
     210             :     uint32_t buf_len;
     211             : 
     212       10871 :     nl_init_generic_client_req(&cl, KSyncSock::GetNetlinkFamilyId());
     213       10871 :     if ((ret = nl_build_header(&cl, &buf, &buf_len)) < 0) {
     214           0 :         LOG(DEBUG, "Error creating mpls message. Error : " << ret);
     215           0 :         nl_free(&cl);
     216           0 :         return;
     217             :     }
     218             : 
     219       10871 :     struct nlmsghdr *nlh = (struct nlmsghdr *)cl.cl_buf;
     220       10871 :     nlh->nlmsg_seq = seq_num;
     221       10871 :     nlh->nlmsg_flags |= flags;
     222       10871 :     encode_len = EncodeVrResponse(buf, buf_len, seq_num, code);
     223       10871 :     nl_update_header(&cl, encode_len);
     224       10871 :     LOG(DEBUG, "SimulateResponse " << " seq " << seq_num << " code " << std::hex << code);
     225             : 
     226       10871 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     227       10871 :     sock->AddNetlinkTxBuff(&cl);
     228             : }
     229             : 
     230           0 : void KSyncSockTypeMap::DisableReceiveQueue(bool disable) {
     231           0 :     for(int i = 0; i < IoContext::MAX_WORK_QUEUES; i++) {
     232           0 :         ksync_rx_queue[i]->set_disable(disable);
     233             :     }
     234           0 : }
     235             : 
     236           0 : void KSyncSockTypeMap::SetDropStats(const vr_drop_stats_req &req) {
     237           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     238           0 :     sock->drop_stats = req;
     239           0 : }
     240             : 
     241           2 : void KSyncSockTypeMap::SetVRouterOps(const vrouter_ops &req) {
     242           2 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     243           2 :     sock->ksync_vrouter_ops = req;
     244           2 : }
     245             : 
     246           0 : void KSyncSockTypeMap::InterfaceAdd(int id, int flags, int mac_size) {
     247           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     248           0 :     KSyncSockTypeMap::ksync_map_if::const_iterator it;
     249             :     static int os_index = 10;
     250             :     char name[50];
     251           0 :     sprintf(name, "intf%d", id);
     252           0 :     vr_interface_req req;
     253           0 :     req.set_vifr_idx(id);
     254           0 :     req.set_vifr_type(VIF_TYPE_VIRTUAL);
     255           0 :     req.set_vifr_rid(0);
     256           0 :     req.set_vifr_os_idx(os_index);
     257           0 :     req.set_vifr_name(name);
     258           0 :     const std::vector<signed char> list(mac_size);
     259           0 :     req.set_vifr_mac(list);
     260           0 :     req.set_vifr_flags(flags);
     261             : 
     262           0 :     it = sock->if_map.find(id);
     263           0 :     if (it == sock->if_map.end()) {
     264           0 :         sock->if_map[id] = req;
     265           0 :         ++os_index;
     266             :     }
     267           0 : }
     268             : 
     269           0 : void KSyncSockTypeMap::InterfaceDelete(int id) {
     270           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     271           0 :     KSyncSockTypeMap::ksync_map_if::iterator it;
     272           0 :     it = sock->if_map.find(id);
     273           0 :     if (it != sock->if_map.end()) {
     274           0 :         sock->if_map.erase(it);
     275             :     }
     276           0 : }
     277             : 
     278           0 : void KSyncSockTypeMap::NHAdd(int id, int flags) {
     279           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     280           0 :     KSyncSockTypeMap::ksync_map_nh::const_iterator it;
     281           0 :     vr_nexthop_req req;
     282           0 :     req.set_nhr_id(id);
     283           0 :     req.set_nhr_flags(flags);
     284           0 :     it = sock->nh_map.find(id);
     285           0 :     if (it == sock->nh_map.end()) {
     286           0 :         sock->nh_map[id] = req;
     287             :     }
     288           0 : }
     289             : 
     290           0 : void KSyncSockTypeMap::NHDelete(int id) {
     291           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     292           0 :     KSyncSockTypeMap::ksync_map_nh::iterator it;
     293           0 :     it = sock->nh_map.find(id);
     294           0 :     if (it != sock->nh_map.end()) {
     295           0 :         sock->nh_map.erase(it);
     296             :     }
     297           0 : }
     298             : 
     299           0 : void KSyncSockTypeMap::MplsAdd(int id) {
     300           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     301           0 :     KSyncSockTypeMap::ksync_map_mpls::const_iterator it;
     302           0 :     vr_mpls_req req;
     303           0 :     req.set_mr_label(id);
     304           0 :     it = sock->mpls_map.find(id);
     305           0 :     if (it == sock->mpls_map.end()) {
     306           0 :         sock->mpls_map[id] = req;
     307             :     }
     308           0 : }
     309             : 
     310           0 : void KSyncSockTypeMap::MplsDelete(int id) {
     311           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     312           0 :     KSyncSockTypeMap::ksync_map_mpls::iterator it;
     313           0 :     it = sock->mpls_map.find(id);
     314           0 :     if (it != sock->mpls_map.end()) {
     315           0 :         sock->mpls_map.erase(it);
     316             :     }
     317           0 : }
     318             : 
     319           0 : void KSyncSockTypeMap::MirrorAdd(int id) {
     320           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     321           0 :     KSyncSockTypeMap::ksync_map_mirror::const_iterator it;
     322           0 :     vr_mirror_req req;
     323           0 :     req.set_mirr_index(id);
     324           0 :     it = sock->mirror_map.find(id);
     325           0 :     if (it == sock->mirror_map.end()) {
     326           0 :         sock->mirror_map[id] = req;
     327             :     }
     328           0 : }
     329             : 
     330           0 : void KSyncSockTypeMap::MirrorDelete(int id) {
     331           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     332           0 :     KSyncSockTypeMap::ksync_map_mirror::iterator it;
     333           0 :     it = sock->mirror_map.find(id);
     334           0 :     if (it != sock->mirror_map.end()) {
     335           0 :         sock->mirror_map.erase(it);
     336             :     }
     337           0 : }
     338             : 
     339        2622 : void KSyncSockTypeMap::RouteAdd(vr_route_req &req) {
     340        2622 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     341             :     //store in the route tree
     342        2622 :     std::pair<std::set<vr_route_req>::iterator, bool> ret;
     343        2622 :     ret = sock->rt_tree.insert(req);
     344             : 
     345             :     /* If insertion fails, remove the existing entry and add the new one */
     346        2622 :     if (ret.second == false) {
     347        1193 :         int del_count = sock->rt_tree.erase(req);
     348        1193 :         assert(del_count);
     349        1193 :         ret = sock->rt_tree.insert(req);
     350        1193 :         assert(ret.second == true);
     351             :     }
     352        2622 :     if (req.get_rtr_family() == AF_BRIDGE) {
     353         962 :         sock->SetBridgeEntry((uint32_t)req.get_rtr_index(), &req, true);
     354             :     }
     355        2622 : }
     356             : 
     357           0 : void KSyncSockTypeMap::RouteDelete(vr_route_req &req) {
     358           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     359           0 :     KSyncSockTypeMap::ksync_rt_tree::iterator it;
     360           0 :     it = sock->rt_tree.find(req);
     361           0 :     if (it != sock->rt_tree.end()) {
     362           0 :         sock->rt_tree.erase(it);
     363             :     }
     364           0 : }
     365             : 
     366           0 : void KSyncSockTypeMap::VrfAssignAdd(vr_vrf_assign_req &req) {
     367           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     368           0 :     KSyncSockTypeMap::ksync_vrf_assign_tree::const_iterator it;
     369           0 :     it = sock->vrf_assign_tree.find(req);
     370           0 :     if (it == sock->vrf_assign_tree.end()) {
     371           0 :         sock->vrf_assign_tree.insert(req);
     372             :     }
     373           0 : }
     374             : 
     375           0 : void KSyncSockTypeMap::VrfAssignDelete(vr_vrf_assign_req &req) {
     376           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     377           0 :     KSyncSockTypeMap::ksync_vrf_assign_tree::iterator it;
     378           0 :     it = sock->vrf_assign_tree.find(req);
     379           0 :     if (it != sock->vrf_assign_tree.end()) {
     380           0 :         sock->vrf_assign_tree.erase(it);
     381             :     }
     382           0 : }
     383             : 
     384           0 : void KSyncSockTypeMap::VrfStatsAdd(int vrf_id) {
     385           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     386           0 :     KSyncSockTypeMap::ksync_map_vrf_stats::const_iterator it;
     387           0 :     vr_vrf_stats_req vrf_stats;
     388           0 :     vrf_stats.set_vsr_vrf(vrf_id);
     389           0 :     vrf_stats.set_vsr_family(AF_INET);
     390           0 :     vrf_stats.set_vsr_rid(0);
     391           0 :     vrf_stats.set_vsr_discards(0);
     392           0 :     vrf_stats.set_vsr_resolves(0);
     393           0 :     vrf_stats.set_vsr_receives(0);
     394           0 :     vrf_stats.set_vsr_udp_tunnels(0);
     395           0 :     vrf_stats.set_vsr_udp_mpls_tunnels(0);
     396           0 :     vrf_stats.set_vsr_gre_mpls_tunnels(0);
     397           0 :     vrf_stats.set_vsr_ecmp_composites(0);
     398           0 :     vrf_stats.set_vsr_fabric_composites(0);
     399           0 :     vrf_stats.set_vsr_l2_mcast_composites(0);
     400           0 :     vrf_stats.set_vsr_encaps(0);
     401           0 :     vrf_stats.set_vsr_l2_encaps(0);
     402             : 
     403           0 :     it = sock->vrf_stats_map.find(vrf_id);
     404           0 :     if (it == sock->vrf_stats_map.end()) {
     405           0 :         sock->vrf_stats_map[vrf_id] = vrf_stats;
     406             :     }
     407           0 : }
     408             : 
     409           0 : void KSyncSockTypeMap::VrfStatsDelete(int vrf_id) {
     410           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     411           0 :     KSyncSockTypeMap::ksync_map_vrf_stats::iterator it;
     412           0 :     it = sock->vrf_stats_map.find(vrf_id);
     413           0 :     if (it != sock->vrf_stats_map.end()) {
     414           0 :         sock->vrf_stats_map.erase(it);
     415             :     }
     416           0 : }
     417             : 
     418           0 : void KSyncSockTypeMap::VrfStatsUpdate(int vrf_id, const vr_vrf_stats_req &req) {
     419           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     420           0 :     vr_vrf_stats_req &r = sock->vrf_stats_map[vrf_id];
     421           0 :     r.set_vsr_discards(req.get_vsr_discards());
     422           0 :     r.set_vsr_resolves(req.get_vsr_resolves());
     423           0 :     r.set_vsr_receives(req.get_vsr_receives());
     424           0 :     r.set_vsr_udp_tunnels(req.get_vsr_udp_tunnels());
     425           0 :     r.set_vsr_udp_mpls_tunnels(req.get_vsr_udp_mpls_tunnels());
     426           0 :     r.set_vsr_gre_mpls_tunnels(req.get_vsr_gre_mpls_tunnels());
     427           0 :     r.set_vsr_ecmp_composites(req.get_vsr_ecmp_composites());
     428           0 :     r.set_vsr_l2_mcast_composites(req.get_vsr_l2_mcast_composites());
     429           0 :     r.set_vsr_fabric_composites(req.get_vsr_fabric_composites());
     430           0 :     r.set_vsr_encaps(req.get_vsr_encaps());
     431           0 :     r.set_vsr_l2_encaps(req.get_vsr_l2_encaps());
     432           0 :     r.set_vsr_gros(req.get_vsr_gros());
     433           0 :     r.set_vsr_diags(req.get_vsr_diags());
     434           0 :     r.set_vsr_encap_composites(req.get_vsr_encap_composites());
     435           0 :     r.set_vsr_evpn_composites(req.get_vsr_evpn_composites());
     436           0 :     r.set_vsr_vrf_translates(req.get_vsr_vrf_translates());
     437           0 :     r.set_vsr_vxlan_tunnels(req.get_vsr_vxlan_tunnels());
     438           0 :     r.set_vsr_arp_virtual_proxy(req.get_vsr_arp_virtual_proxy());
     439           0 :     r.set_vsr_arp_virtual_stitch(req.get_vsr_arp_virtual_stitch());
     440           0 :     r.set_vsr_arp_virtual_flood(req.get_vsr_arp_virtual_flood());
     441           0 :     r.set_vsr_arp_physical_stitch(req.get_vsr_arp_physical_stitch());
     442           0 :     r.set_vsr_arp_tor_proxy(req.get_vsr_arp_tor_proxy());
     443           0 :     r.set_vsr_arp_physical_flood(req.get_vsr_arp_physical_flood());
     444           0 :     r.set_vsr_l2_receives(req.get_vsr_l2_receives());
     445           0 :     r.set_vsr_uuc_floods(req.get_vsr_uuc_floods());
     446           0 : }
     447             : 
     448           0 : void KSyncSockTypeMap::VxlanAdd(int id) {
     449           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     450           0 :     KSyncSockTypeMap::ksync_map_vxlan::const_iterator it;
     451             : 
     452           0 :     it = sock->vxlan_map.find(id);
     453           0 :     if (it == sock->vxlan_map.end()) {
     454           0 :         vr_vxlan_req vxlan;
     455           0 :         vxlan.set_vxlanr_vnid(id);
     456           0 :         vxlan.set_vxlanr_rid(0);
     457           0 :         sock->vxlan_map[id] = vxlan;
     458           0 :     }
     459           0 : }
     460             : 
     461           0 : void KSyncSockTypeMap::VxlanDelete(int id) {
     462           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     463           0 :     KSyncSockTypeMap::ksync_map_vxlan::iterator it;
     464             : 
     465           0 :     it = sock->vxlan_map.find(id);
     466           0 :     if (it != sock->vxlan_map.end()) {
     467           0 :         sock->vxlan_map.erase(it);
     468             :     }
     469           0 : }
     470             : 
     471           0 : void KSyncSockTypeMap::IfStatsUpdate(int idx, int ibytes, int ipkts, int ierrors,
     472             :                                      int obytes, int opkts, int oerrors) {
     473           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     474           0 :     vr_interface_req req = sock->if_map[idx];
     475           0 :     req.set_vifr_ibytes(ibytes+req.get_vifr_ibytes());
     476           0 :     req.set_vifr_ipackets(ipkts+req.get_vifr_ipackets());
     477           0 :     req.set_vifr_ierrors(ierrors+req.get_vifr_ierrors());
     478           0 :     req.set_vifr_obytes(obytes+req.get_vifr_obytes());
     479           0 :     req.set_vifr_opackets(opkts+req.get_vifr_opackets());
     480           0 :     req.set_vifr_oerrors(oerrors+req.get_vifr_oerrors());
     481           0 :     sock->if_map[idx] = req;
     482           0 : }
     483             : 
     484           0 : void KSyncSockTypeMap::IfStatsSet(int idx, int ibytes, int ipkts, int ierrors,
     485             :                                   int obytes, int opkts, int oerrors) {
     486           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     487           0 :     vr_interface_req req = sock->if_map[idx];
     488           0 :     req.set_vifr_ibytes(ibytes);
     489           0 :     req.set_vifr_ipackets(ipkts);
     490           0 :     req.set_vifr_ierrors(ierrors);
     491           0 :     req.set_vifr_obytes(obytes);
     492           0 :     req.set_vifr_opackets(opkts);
     493           0 :     req.set_vifr_oerrors(oerrors);
     494           0 :     sock->if_map[idx] = req;
     495           0 : }
     496             : 
     497           0 : int KSyncSockTypeMap::IfCount() {
     498           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     499           0 :     return sock->if_map.size();
     500             : }
     501             : 
     502           0 : int KSyncSockTypeMap::NHCount() {
     503           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     504           0 :     return sock->nh_map.size();
     505             : }
     506             : 
     507           0 : int KSyncSockTypeMap::MplsCount() {
     508           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     509           0 :     return sock->mpls_map.size();
     510             : }
     511             : 
     512           0 : int KSyncSockTypeMap::RouteCount() {
     513           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     514           0 :     return sock->rt_tree.size();
     515             : }
     516             : 
     517           0 : int KSyncSockTypeMap::VxLanCount() {
     518           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     519           0 :     return sock->vxlan_map.size();
     520             : }
     521             : 
     522           0 : uint32_t KSyncSockTypeMap::GetSeqno(char *data) {
     523           0 :     struct nlmsghdr *nlh = (struct nlmsghdr *)data;
     524           0 :     return nlh->nlmsg_seq;
     525             : }
     526             : 
     527       33938 : bool KSyncSockTypeMap::IsMoreData(char *data) {
     528       33938 :     struct nlmsghdr *nlh = (struct nlmsghdr *)data;
     529             : 
     530       33938 :     return (nlh->nlmsg_flags & NLM_F_MULTI);
     531             : }
     532             : 
     533       11312 : bool KSyncSockTypeMap::BulkDecoder(char *data,
     534             :                                    KSyncBulkSandeshContext *bulk_context) {
     535       11312 :     KSyncSockNetlink::NetlinkBulkDecoder(data, bulk_context, IsMoreData(data));
     536       11312 :     return true;
     537             : }
     538             : 
     539           2 : bool KSyncSockTypeMap::Decoder(char *data, AgentSandeshContext *context) {
     540           2 :     KSyncSockNetlink::NetlinkDecoder(data, context);
     541           2 :     return true;
     542             : }
     543             : 
     544       11312 : bool KSyncSockTypeMap::Validate(char *data) {
     545       11312 :     struct nlmsghdr *nlh = (struct nlmsghdr *)data;
     546       11312 :     if (nlh->nlmsg_type == NLMSG_ERROR) {
     547           0 :         LOG(ERROR, "Ignoring Netlink error for seqno " << nlh->nlmsg_seq
     548             :                         << " len " << nlh->nlmsg_len);
     549           0 :         assert(0);
     550             :         return true;
     551             :     }
     552             : 
     553       11312 :     if (nlh->nlmsg_len > kBufLen) {
     554           0 :         LOG(ERROR, "Length of " << nlh->nlmsg_len << " is more than expected "
     555             :             "length of " << kBufLen);
     556           0 :         assert(0);
     557             :         return true;
     558             :     }
     559       11312 :     return true;
     560             : }
     561             : 
     562       11114 : static void IoVectorToData(std::string& data, KSyncBufferList *iovec) {
     563       11114 :     KSyncBufferList::iterator it = iovec->begin();
     564       22365 :     while (it != iovec->end()) {
     565       11251 :         unsigned char *buf = boost::asio::buffer_cast<unsigned char *>(*it);
     566       11251 :         int buffer_size = boost::asio::buffer_size(*it);
     567       11251 :         data.append(reinterpret_cast<const char*>(buf), buffer_size);
     568       11251 :         it++;
     569             :     }
     570       11114 : }
     571             : 
     572             : //send or store in map
     573           0 : void KSyncSockTypeMap::AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no,
     574             :                                    HandlerCb cb) {
     575           0 :     std::string data = "";
     576           0 :     IoVectorToData(data, iovec);
     577             : 
     578           0 :     KSyncUserSockContext ctx(seq_no);
     579             :     //parse and store info in map [done in Process() callbacks]
     580           0 :     ProcessSandesh((const uint8_t *)(data.c_str()), data.size(), &ctx);
     581           0 : }
     582             : 
     583             : //send or store in map
     584       11114 : std::size_t KSyncSockTypeMap::SendTo(KSyncBufferList *iovec, uint32_t seq_no) {
     585       11114 :     std::string data = "";
     586       11114 :     IoVectorToData(data, iovec);
     587             : 
     588       11114 :     KSyncUserSockContext ctx(seq_no);
     589             :     //parse and store info in map [done in Process() callbacks]
     590       11114 :     ProcessSandesh((const uint8_t *)(data.c_str()), data.size(), &ctx);
     591       11114 :     return 0;
     592       11114 : }
     593             : 
     594             : //receive msgs from datapath
     595           0 : void KSyncSockTypeMap::AsyncReceive(mutable_buffers_1 buf, HandlerCb cb) {
     596           0 :     sock_.async_receive_from(buf, local_ep_, cb);
     597           0 : }
     598             : 
     599             : //receive msgs from datapath
     600       11314 : void KSyncSockTypeMap::Receive(mutable_buffers_1 buf) {
     601       11314 :     sock_.receive(buf);
     602       11314 : }
     603             : 
     604           2 : vr_flow_entry *KSyncSockTypeMap::FlowMmapAlloc(int size) {
     605           2 :     flow_table_ = (vr_flow_entry *)malloc(size);
     606           2 :     return flow_table_;
     607             : }
     608             : 
     609           2 : void KSyncSockTypeMap::FlowMmapFree() {
     610           2 :     if (flow_table_) {
     611           2 :         free(flow_table_);
     612           2 :         flow_table_ = NULL;
     613             :     }
     614           2 : }
     615             : 
     616           0 : vr_flow_entry *KSyncSockTypeMap::GetFlowEntry(int idx) {
     617           0 :     return &flow_table_[idx];
     618             : }
     619             : 
     620         378 : void KSyncSockTypeMap::SetFlowEntry(vr_flow_req *req, bool set) {
     621         378 :     uint32_t idx = req->get_fr_index();
     622             : 
     623         378 :     vr_flow_entry *f = &flow_table_[idx];
     624         378 :     if (!set) {
     625         148 :         f->fe_flags &= ~VR_FLOW_FLAG_ACTIVE;
     626         148 :         f->fe_stats.flow_bytes = 0;
     627         148 :         f->fe_stats.flow_packets = 0;
     628         148 :         f->fe_gen_id = 0;
     629         148 :         return;
     630             :     }
     631             : 
     632         230 :     int family = (req->get_fr_family() == AF_INET)? Address::INET :
     633         230 :         Address::INET6;
     634         230 :     IpAddress sip;
     635         230 :     IpAddress dip;
     636         230 :     U64ToIp(req->get_fr_flow_sip_u(), req->get_fr_flow_sip_l(),
     637             :             req->get_fr_flow_dip_u(), req->get_fr_flow_dip_l(),
     638             :             family, &sip, &dip);
     639         230 :     f->fe_flags = VR_FLOW_FLAG_ACTIVE;
     640         230 :     f->fe_stats.flow_bytes = 30;
     641         230 :     f->fe_stats.flow_packets = 1;
     642         230 :     f->fe_gen_id = req->get_fr_gen_id();
     643         230 :     if (sip.is_v4()) {
     644         230 :         f->fe_key.flow4_sip = htonl(sip.to_v4().to_ulong());
     645         230 :         f->fe_key.flow4_dip = htonl(dip.to_v4().to_ulong());
     646             :     }
     647         230 :     if (family == Address::INET) {
     648         230 :         f->fe_key.flow4_nh_id = req->get_fr_flow_nh_id();
     649             :     } else {
     650           0 :         f->fe_key.flow6_nh_id = req->get_fr_flow_nh_id();
     651             :     }
     652         230 :     f->fe_key.flow_family = req->get_fr_family();
     653         230 :     f->fe_key.flow_sport = req->get_fr_flow_sport();
     654         230 :     f->fe_key.flow_dport = req->get_fr_flow_dport();
     655         230 :     f->fe_key.flow_nh_id = req->get_fr_flow_nh_id();
     656         230 :     f->fe_key.flow_proto = req->get_fr_flow_proto();
     657             : }
     658             : 
     659           0 : void KSyncSockTypeMap::SetEvictedFlag(int idx) {
     660           0 :     vr_flow_entry *f = &flow_table_[idx];
     661           0 :     if (f->fe_flags & VR_FLOW_FLAG_ACTIVE) {
     662           0 :         f->fe_flags |= VR_FLOW_FLAG_EVICTED;
     663             :     }
     664           0 : }
     665             : 
     666           0 : void KSyncSockTypeMap::ResetEvictedFlag(int idx) {
     667           0 :     vr_flow_entry *f = &flow_table_[idx];
     668           0 :     if (f->fe_flags & VR_FLOW_FLAG_ACTIVE) {
     669           0 :         f->fe_flags &= ~VR_FLOW_FLAG_EVICTED;
     670             :     }
     671           0 : }
     672             : 
     673             : 
     674           0 : void KSyncSockTypeMap::IncrFlowStats(int idx, int pkts, int bytes) {
     675           0 :     vr_flow_entry *f = &flow_table_[idx];
     676           0 :     if (f->fe_flags & VR_FLOW_FLAG_ACTIVE) {
     677           0 :         f->fe_stats.flow_bytes += bytes;
     678           0 :         f->fe_stats.flow_packets += pkts;
     679             :     }
     680           0 : }
     681             : 
     682           0 : void KSyncSockTypeMap::SetTcpFlag(int idx, uint32_t flags) {
     683           0 :     vr_flow_entry *f = &flow_table_[idx];
     684           0 :     if (f->fe_flags & VR_FLOW_FLAG_ACTIVE) {
     685           0 :         f->fe_tcp_flags = flags;
     686             :     }
     687           0 : }
     688             : 
     689           0 : void KSyncSockTypeMap::SetFlowTcpFlags(int idx, uint16_t flags) {
     690           0 :     vr_flow_entry *f = &flow_table_[idx];
     691           0 :     if (f->fe_flags & VR_FLOW_FLAG_ACTIVE) {
     692           0 :         f->fe_tcp_flags = flags;
     693             :     }
     694           0 : }
     695             : 
     696           0 : void KSyncSockTypeMap::SetUnderlaySourcePort(int idx, int port) {
     697           0 :     vr_flow_entry *f = &flow_table_[idx];
     698           0 :     if (f->fe_flags & VR_FLOW_FLAG_ACTIVE) {
     699           0 :         f->fe_udp_src_port = port;
     700             :     }
     701           0 : }
     702             : 
     703           0 : void KSyncSockTypeMap::SetOFlowStats(int idx, uint8_t pkts, uint16_t bytes) {
     704           0 :     vr_flow_entry *f = &flow_table_[idx];
     705           0 :     if (f->fe_flags & VR_FLOW_FLAG_ACTIVE) {
     706           0 :         f->fe_stats.flow_bytes_oflow = bytes;
     707           0 :         f->fe_stats.flow_packets_oflow = pkts;
     708             :     }
     709           0 : }
     710             : 
     711           2 : vr_bridge_entry *KSyncSockTypeMap::BridgeMmapAlloc(int size) {
     712           2 :     bridge_table_ = (vr_bridge_entry *)malloc(size);
     713           2 :     memset(bridge_table_, 0, size);
     714           2 :     return bridge_table_;
     715             : }
     716             : 
     717           2 : void KSyncSockTypeMap::BridgeMmapFree() {
     718           2 :     if (bridge_table_) {
     719           2 :         free(bridge_table_);
     720           2 :         bridge_table_ = NULL;
     721             :     }
     722           2 : }
     723             : 
     724           0 : vr_bridge_entry *KSyncSockTypeMap::GetBridgeEntry(int idx) {
     725           0 :     return &bridge_table_[idx];
     726             : }
     727             : 
     728         962 : void KSyncSockTypeMap::SetBridgeEntry(uint32_t idx, vr_route_req *req,
     729             :                                       bool set) {
     730         962 :     vr_bridge_entry *be = &bridge_table_[idx];
     731         962 :     if (!set) {
     732           0 :         be->be_packets = 0;
     733           0 :         return;
     734             :     }
     735             : 
     736         962 :     if (be->be_packets == 0) {
     737           2 :         be->be_packets = 1;
     738             :     }
     739         962 :     vr_bridge_entry_key *key = &be->be_key;
     740             : 
     741             :     //Copy VRF and mac
     742         962 :     key->be_vrf_id = req->get_rtr_vrf_id();
     743             : 
     744         962 :     uint8_t i = 0;
     745         962 :     const std::vector<signed char> &prefix = req->get_rtr_mac();
     746         962 :     for(std::vector<signed char>::const_iterator it = prefix.begin();
     747        6734 :         it != prefix.end(); ++it) {
     748        5772 :         key->be_mac[i] = ((uint8_t) *it);
     749        5772 :         i++;
     750             :     }
     751             : }
     752             : 
     753         493 : void KSyncSockTypeMap::UpdateBridgeEntryInactiveFlag(int idx, bool set) {
     754         493 :     vr_bridge_entry *be = &bridge_table_[idx];
     755         493 :     if (set) {
     756           0 :         be->be_flags |= VR_BE_MAC_NEW_FLAG;
     757             :     } else {
     758         493 :         be->be_flags &= ~VR_BE_MAC_NEW_FLAG;
     759             :     }
     760         493 : }
     761             : 
     762             : //init ksync map
     763           2 : void KSyncSockTypeMap::Init(boost::asio::io_context &ios) {
     764           2 :     assert(singleton_ == NULL);
     765             : 
     766           2 :     singleton_ = new KSyncSockTypeMap(ios);
     767           2 :     KSyncSock::SetSockTableEntry(singleton_);
     768           2 :     KSyncSock::Init(true, "disabled");
     769             : 
     770           2 :     singleton_->local_ep_.address
     771           2 :         (boost::asio::ip::address::from_string("127.0.0.1"));
     772           2 :     singleton_->local_ep_.port(0);
     773           2 :     singleton_->sock_.open(boost::asio::ip::udp::v4());
     774           2 :     singleton_->sock_.bind(singleton_->local_ep_);
     775           2 :     singleton_->local_ep_ = singleton_->sock_.local_endpoint();
     776           2 : }
     777             : 
     778           0 : void KSyncSockTypeMap::Shutdown() {
     779           0 :     delete singleton_;
     780           0 :     singleton_ = NULL;
     781           0 : }
     782             : 
     783           0 : void KSyncSockTypeMap::PurgeBlockedMsg() {
     784           0 :     while (!ctx_queue_.empty()) {
     785           0 :         ctx_queue_.front()->Process();
     786           0 :         delete ctx_queue_.front();
     787           0 :         ctx_queue_.pop();
     788             :     }
     789           0 :     PurgeTxBuffer();
     790           0 : }
     791             : 
     792           0 : void KSyncSockTypeMap::SetBlockMsgProcessing(bool enable) {
     793           0 :     std::scoped_lock lock(ctx_queue_lock_);
     794           0 :     if (block_msg_processing_ != enable) {
     795           0 :         block_msg_processing_ = enable;
     796           0 :         if (!block_msg_processing_) {
     797           0 :             PurgeBlockedMsg();
     798             :         }
     799             :     }
     800           0 : }
     801             : 
     802         764 : void KSyncUserSockIfContext::Process() {
     803         764 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     804             : 
     805             :     //delete from map if command is delete
     806         764 :     if (req_->get_h_op() == sandesh_op::DEL) {
     807         175 :         sock->if_map.erase(req_->get_vifr_idx());
     808         589 :     } else if (req_->get_h_op() == sandesh_op::DUMP) {
     809           0 :         IfDumpHandler dump;
     810           0 :         dump.SendDumpResponse(GetSeqNum(), req_);
     811           0 :         return;
     812         589 :     } else if (req_->get_h_op() == sandesh_op::GET) {
     813           0 :         IfDumpHandler dump;
     814           0 :         dump.SendGetResponse(GetSeqNum(), req_->get_vifr_idx());
     815           0 :         return;
     816           0 :     } else {
     817             :         //store in the map
     818         589 :         vr_interface_req if_info(*req_);
     819         589 :         sock->if_map[req_->get_vifr_idx()] = if_info;
     820         589 :     }
     821         764 :     KSyncSockTypeMap::SimulateResponse(GetSeqNum(), 0, 0);
     822             : }
     823             : 
     824         764 : void KSyncUserSockContext::IfMsgHandler(vr_interface_req *req) {
     825         764 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     826         764 :     KSyncUserSockIfContext *ifctx = new KSyncUserSockIfContext(GetSeqNum(), req);
     827             : 
     828         764 :     if (sock->IsBlockMsgProcessing()) {
     829           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
     830           0 :         sock->ctx_queue_.push(ifctx);
     831           0 :     } else {
     832         764 :         ifctx->Process();
     833         764 :         delete ifctx;
     834             :     }
     835         764 : }
     836             : 
     837         378 : void KSyncUserSockFlowContext::Process() {
     838         378 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     839         378 :     uint16_t flags = 0;
     840         378 :     int flow_error = sock->GetKSyncError(KSyncSockTypeMap::KSYNC_FLOW_ENTRY_TYPE);
     841             : 
     842         378 :     flags = req_->get_fr_flags();
     843             :     //delete from map if command is delete
     844         378 :     if (!flags) {
     845         148 :         sock->flow_map.erase(req_->get_fr_index());
     846             :         //Deactivate the flow-entry in flow mmap
     847         148 :         KSyncSockTypeMap::SetFlowEntry(req_, false);
     848             :     } else {
     849         230 :         uint32_t fwd_flow_idx = req_->get_fr_index();
     850         230 :         if (fwd_flow_idx == 0xFFFFFFFF) {
     851          77 :             if (flow_error == 0) {
     852             :                 /* Allocate entry only of no error case */
     853          77 :                 if (sock->is_incremental_index()) {
     854             :                     /* Send reverse-flow index as one more than fwd-flow index */
     855           0 :                     fwd_flow_idx = req_->get_fr_rindex() + 1;
     856             :                 } else {
     857          77 :                     fwd_flow_idx = rand() % 20000;
     858             :                     /* Reserve first 20000 indexes for forwarding flow
     859             :                      * Reverse flow indexes will start from 20000 always
     860             :                      */
     861          77 :                     fwd_flow_idx += 20000;
     862             :                 }
     863             :                 /* If the randomly allocated index is used already then
     864             :                  * find out the next randon index which is free
     865             :                  */
     866          77 :                 if (sock->is_incremental_index()) {
     867           0 :                     if (sock->flow_map.find(fwd_flow_idx) != sock->flow_map.end()) {
     868             :                         //sock->SimulateResponse(GetSeqNum(), -EEXIST, 0);
     869           0 :                         int code  = -EEXIST;
     870           0 :                         req_->set_fr_index(fwd_flow_idx);
     871           0 :                         req_->set_fr_gen_id((fwd_flow_idx % 255));
     872           0 :                         KSyncSockTypeMap::FlowNatResponse(GetSeqNum(), req_, code);
     873           0 :                         return;
     874             :                     }
     875             :                 } else {
     876          77 :                     while (sock->flow_map.find(fwd_flow_idx) != sock->flow_map.end()) {
     877           0 :                         fwd_flow_idx = rand() % 20000;
     878             :                         /* Reserve first 20000 indexes for forwarding flow
     879             :                          * Reverse flow indexes will start from 20000 always
     880             :                          */
     881           0 :                         fwd_flow_idx += 20000;
     882             :                     }
     883             :                 }
     884          77 :                 req_->set_fr_index(fwd_flow_idx);
     885          77 :                 req_->set_fr_gen_id((fwd_flow_idx % 255));
     886             :             }
     887             :         }
     888             : 
     889         230 :         if (fwd_flow_idx != 0xFFFFFFFF) {
     890             :             //store info from binary sandesh message
     891         230 :             vr_flow_req flow_info(*req_);
     892             : 
     893         230 :             sock->flow_map[req_->get_fr_index()] = flow_info;
     894             : 
     895             :             //Activate the flow-entry in flow mmap
     896         230 :             KSyncSockTypeMap::SetFlowEntry(req_, true);
     897         230 :         }
     898             : 
     899             :         // For NAT flow, don't send vr_response, instead send
     900             :         // vr_flow_req with index of reverse_flow
     901         230 :         KSyncSockTypeMap::FlowNatResponse(GetSeqNum(), req_);
     902         230 :         return;
     903             :     }
     904         148 :     KSyncSockTypeMap::FlowNatResponse(GetSeqNum(), req_);
     905             : }
     906             : 
     907         378 : void KSyncUserSockContext::FlowMsgHandler(vr_flow_req *req) {
     908         378 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     909         378 :     KSyncUserSockFlowContext *flowctx = new KSyncUserSockFlowContext(GetSeqNum(), req);
     910             : 
     911         378 :     if (sock->IsBlockMsgProcessing()) {
     912           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
     913           0 :         sock->ctx_queue_.push(flowctx);
     914           0 :     } else {
     915         378 :         flowctx->Process();
     916         378 :         delete flowctx;
     917             :     }
     918         378 : }
     919             : 
     920        3415 : void KSyncUserSockNHContext::Process() {
     921        3415 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     922             : 
     923             :     //delete from map if command is delete
     924        3415 :     if (req_->get_h_op() == sandesh_op::DEL) {
     925        1650 :         sock->nh_map.erase(req_->get_nhr_id());
     926        1765 :     } else if (req_->get_h_op() == sandesh_op::DUMP) {
     927           0 :         NHDumpHandler dump;
     928           0 :         dump.SendDumpResponse(GetSeqNum(), req_);
     929           0 :         return;
     930        1765 :     } else if (req_->get_h_op() == sandesh_op::GET) {
     931           0 :         NHDumpHandler dump;
     932           0 :         dump.SendGetResponse(GetSeqNum(), req_->get_nhr_id());
     933           0 :         return;
     934           0 :     } else {
     935             :         //store in the map
     936        1765 :         vr_nexthop_req nh_info(*req_);
     937        1765 :         sock->nh_map[req_->get_nhr_id()] = nh_info;
     938        1765 :     }
     939        3415 :     KSyncSockTypeMap::SimulateResponse(GetSeqNum(), 0, 0);
     940             : }
     941             : 
     942        3415 : void KSyncUserSockContext::NHMsgHandler(vr_nexthop_req *req) {
     943        3415 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     944        3415 :     KSyncUserSockNHContext *nhctx = new KSyncUserSockNHContext(GetSeqNum(), req);
     945             : 
     946        3415 :     if (sock->IsBlockMsgProcessing()) {
     947           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
     948           0 :         sock->ctx_queue_.push(nhctx);
     949           0 :     } else {
     950        3415 :         nhctx->Process();
     951        3415 :         delete nhctx;
     952             :     }
     953        3415 : }
     954             : 
     955        2359 : void KSyncUserSockMplsContext::Process() {
     956        2359 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     957             : 
     958             :     //delete from map mpls command is delete
     959        2359 :     if (req_->get_h_op() == sandesh_op::DEL) {
     960        1027 :         sock->mpls_map.erase(req_->get_mr_label());
     961        1332 :     } else if (req_->get_h_op() == sandesh_op::DUMP) {
     962           0 :         MplsDumpHandler dump;
     963           0 :         dump.SendDumpResponse(GetSeqNum(), req_);
     964           0 :         return;
     965        1332 :     } else if (req_->get_h_op() == sandesh_op::GET) {
     966           0 :         MplsDumpHandler dump;
     967           0 :         dump.SendGetResponse(GetSeqNum(), req_->get_mr_label());
     968           0 :         return;
     969           0 :     } else {
     970             :         //store in the map
     971        1332 :         vr_mpls_req mpls_info(*req_);
     972        1332 :         sock->mpls_map[req_->get_mr_label()] = mpls_info;
     973        1332 :     }
     974        2359 :     KSyncSockTypeMap::SimulateResponse(GetSeqNum(), 0, 0);
     975             : }
     976             : 
     977        2359 : void KSyncUserSockContext::MplsMsgHandler(vr_mpls_req *req) {
     978        2359 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     979        2359 :     KSyncUserSockMplsContext *mplsctx = new KSyncUserSockMplsContext(GetSeqNum(), req);
     980             : 
     981        2359 :     if (sock->IsBlockMsgProcessing()) {
     982           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
     983           0 :         sock->ctx_queue_.push(mplsctx);
     984           0 :     } else {
     985        2359 :         mplsctx->Process();
     986        2359 :         delete mplsctx;
     987             :     }
     988        2359 : }
     989             : 
     990        4051 : void KSyncUserSockRouteContext::Process() {
     991        4051 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
     992             : 
     993             :     //delete from the route tree, if the command is delete
     994        4051 :     if (req_->get_h_op() == sandesh_op::DEL) {
     995        1429 :         if (req_->get_rtr_family() == AF_BRIDGE) {
     996         493 :             sock->UpdateBridgeEntryInactiveFlag(req_->get_rtr_index(), false);
     997             :         }
     998        1429 :         sock->rt_tree.erase(*req_);
     999        2622 :     } else if (req_->get_h_op() == sandesh_op::DUMP) {
    1000           0 :         RouteDumpHandler dump;
    1001           0 :         sock->SetBridgeEntry(req_->get_rtr_index(), req_, false);
    1002           0 :         dump.SendDumpResponse(GetSeqNum(), req_);
    1003           0 :         return;
    1004           0 :     } else {
    1005        2622 :         sock->RouteAdd(*req_);
    1006             :     }
    1007        4051 :     KSyncSockTypeMap::SimulateResponse(GetSeqNum(), 0, 0);
    1008             : }
    1009             : 
    1010        4051 : void KSyncUserSockContext::RouteMsgHandler(vr_route_req *req) {
    1011        4051 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1012        4051 :     KSyncUserSockRouteContext *rtctx = new KSyncUserSockRouteContext(GetSeqNum(), req);
    1013             : 
    1014        4051 :     if (sock->IsBlockMsgProcessing()) {
    1015           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
    1016           0 :         sock->ctx_queue_.push(rtctx);
    1017           0 :     } else {
    1018        4051 :         rtctx->Process();
    1019        4051 :         delete rtctx;
    1020             :     }
    1021        4051 : }
    1022             : 
    1023           0 : void KSyncUserSockContext::QosConfigMsgHandler(vr_qos_map_req *req) {
    1024           0 :     assert(0);
    1025             : }
    1026             : 
    1027           0 : void KSyncUserSockContext::ForwardingClassMsgHandler(vr_fc_map_req *req) {
    1028           0 :     assert(0);
    1029             : }
    1030             : 
    1031           0 : void KSyncUserSockContext::MirrorMsgHandler(vr_mirror_req *req) {
    1032           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1033             : 
    1034             :     //delete from map if command is delete
    1035           0 :     if (req->get_h_op() == sandesh_op::DEL) {
    1036           0 :         sock->mirror_map.erase(req->get_mirr_index());
    1037           0 :         KSyncSockTypeMap::SimulateResponse(GetSeqNum(), 0, 0);
    1038           0 :         return;
    1039             :     }
    1040             : 
    1041           0 :     if (req->get_h_op() == sandesh_op::DUMP) {
    1042           0 :         MirrorDumpHandler dump;
    1043           0 :         dump.SendDumpResponse(GetSeqNum(), req);
    1044           0 :         return;
    1045           0 :     }
    1046             : 
    1047           0 :     if (req->get_h_op() == sandesh_op::GET) {
    1048           0 :         MirrorDumpHandler dump;
    1049           0 :         dump.SendGetResponse(GetSeqNum(), req->get_mirr_index());
    1050           0 :         return;
    1051           0 :     }
    1052             : 
    1053             :     //store in the map
    1054           0 :     vr_mirror_req mirror_info(*req);
    1055           0 :     sock->mirror_map[req->get_mirr_index()] = mirror_info;
    1056           0 :     KSyncSockTypeMap::SimulateResponse(GetSeqNum(), 0, 0);
    1057           0 : }
    1058             : 
    1059         134 : void KSyncUserSockVxLanContext::Process() {
    1060         134 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1061             : 
    1062             :     //delete from map vxlan command is delete
    1063         134 :     if (req_->get_h_op() == sandesh_op::DEL) {
    1064          67 :         sock->vxlan_map.erase(req_->get_vxlanr_vnid());
    1065          67 :     } else if (req_->get_h_op() == sandesh_op::DUMP) {
    1066           0 :         VxLanDumpHandler dump;
    1067           0 :         dump.SendDumpResponse(GetSeqNum(), req_);
    1068           0 :         return;
    1069          67 :     } else if (req_->get_h_op() == sandesh_op::GET) {
    1070           0 :         VxLanDumpHandler dump;
    1071           0 :         dump.SendGetResponse(GetSeqNum(), req_->get_vxlanr_vnid());
    1072           0 :         return;
    1073           0 :     } else {
    1074             :         //store in the map
    1075          67 :         vr_vxlan_req vxlan_info(*req_);
    1076          67 :         sock->vxlan_map[req_->get_vxlanr_vnid()] = vxlan_info;
    1077          67 :     }
    1078         134 :     KSyncSockTypeMap::SimulateResponse(GetSeqNum(), 0, 0);
    1079             : }
    1080             : 
    1081         134 : void KSyncUserSockContext::VxLanMsgHandler(vr_vxlan_req *req) {
    1082         134 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1083             :     KSyncUserSockVxLanContext *vxlanctx =
    1084         134 :         new KSyncUserSockVxLanContext(GetSeqNum(), req);
    1085             : 
    1086         134 :     if (sock->IsBlockMsgProcessing()) {
    1087           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
    1088           0 :         sock->ctx_queue_.push(vxlanctx);
    1089           0 :     } else {
    1090         134 :         vxlanctx->Process();
    1091         134 :         delete vxlanctx;
    1092             :     }
    1093         134 : }
    1094             : 
    1095             : 
    1096           2 : void KSyncUserVrouterOpsContext::Process() {
    1097           2 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1098           2 :     if (req_->get_h_op() == sandesh_op::GET) {
    1099           2 :         VRouterOpsDumpHandler dump;
    1100           2 :         sock->ksync_vrouter_ops.set_vo_mpls_labels(10000);
    1101           2 :         dump.SendGetResponse(GetSeqNum(), 0);
    1102           2 :         return;
    1103           2 :     }
    1104             : }
    1105           2 : void KSyncUserSockContext::VrouterOpsMsgHandler(vrouter_ops *req) {
    1106           2 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1107             :     KSyncUserVrouterOpsContext *vrouter_ops =
    1108           2 :         new KSyncUserVrouterOpsContext(GetSeqNum(), req);
    1109             : 
    1110           2 :     if (sock->IsBlockMsgProcessing()) {
    1111           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
    1112           0 :         sock->ctx_queue_.push(vrouter_ops);
    1113           0 :     } else {
    1114           2 :         vrouter_ops->Process();
    1115           2 :         delete vrouter_ops;
    1116             :     }
    1117           2 : }
    1118             : 
    1119           0 : void KSyncUserSockVrfAssignContext::Process() {
    1120           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1121             : 
    1122             :     //delete from the vrf assign tree, if the command is delete
    1123           0 :     if (req_->get_h_op() == sandesh_op::DEL) {
    1124           0 :         sock->vrf_assign_tree.erase(*req_);
    1125           0 :     } else if (req_->get_h_op() == sandesh_op::DUMP) {
    1126           0 :         VrfAssignDumpHandler dump;
    1127           0 :         dump.SendDumpResponse(GetSeqNum(), req_);
    1128           0 :         return;
    1129           0 :     } else {
    1130             :         //store in the vrf assign tree
    1131           0 :         std::pair<std::set<vr_vrf_assign_req>::iterator, bool> ret;
    1132           0 :         ret = sock->vrf_assign_tree.insert(*req_);
    1133             : 
    1134             :         /* If insertion fails, remove the existing entry and add the new one */
    1135           0 :         if (ret.second == false) {
    1136           0 :             int del_count = sock->vrf_assign_tree.erase(*req_);
    1137           0 :             assert(del_count);
    1138           0 :             ret = sock->vrf_assign_tree.insert(*req_);
    1139           0 :             assert(ret.second == true);
    1140             :         }
    1141             :     }
    1142           0 :     KSyncSockTypeMap::SimulateResponse(GetSeqNum(), 0, 0);
    1143             : }
    1144             : 
    1145           0 : void KSyncUserSockContext::VrfAssignMsgHandler(vr_vrf_assign_req *req) {
    1146           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1147             :     KSyncUserSockVrfAssignContext *ctx =
    1148           0 :         new KSyncUserSockVrfAssignContext(GetSeqNum(), req);
    1149             : 
    1150           0 :     if (sock->IsBlockMsgProcessing()) {
    1151           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
    1152           0 :         sock->ctx_queue_.push(ctx);
    1153           0 :     } else {
    1154           0 :         ctx->Process();
    1155           0 :         delete ctx;
    1156             :     }
    1157           0 : }
    1158             : 
    1159         148 : void KSyncUserSockVrfContext::Process() {
    1160         148 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1161             : 
    1162             :     //delete from map if command is delete
    1163         148 :     if (req_->get_h_op() == sandesh_op::DEL) {
    1164          74 :         sock->vrf_map.erase(req_->get_vrf_idx());
    1165          74 :     } else if (req_->get_h_op() == sandesh_op::DUMP) {
    1166           0 :         VrfDumpHandler dump;
    1167           0 :         dump.SendDumpResponse(GetSeqNum(), req_);
    1168           0 :         return;
    1169          74 :     } else if (req_->get_h_op() == sandesh_op::GET) {
    1170           0 :         VrfDumpHandler dump;
    1171           0 :         dump.SendGetResponse(GetSeqNum(), req_->get_vrf_idx());
    1172           0 :         return;
    1173           0 :     } else {
    1174             :         //store in the map
    1175          74 :         vr_vrf_req vrf_info(*req_);
    1176          74 :         sock->vrf_map[req_->get_vrf_idx()] = vrf_info;
    1177          74 :     }
    1178         148 :     KSyncSockTypeMap::SimulateResponse(GetSeqNum(), 0, 0);
    1179             : }
    1180         148 : void KSyncUserSockContext::VrfMsgHandler(vr_vrf_req *req) {
    1181         148 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1182             :     KSyncUserSockVrfContext *ctx =
    1183         148 :         new KSyncUserSockVrfContext(GetSeqNum(), req);
    1184             : 
    1185         148 :     if (sock->IsBlockMsgProcessing()) {
    1186           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
    1187           0 :         sock->ctx_queue_.push(ctx);
    1188           0 :     } else {
    1189         148 :         ctx->Process();
    1190         148 :         delete ctx;
    1191             :     }
    1192         148 : }
    1193             : 
    1194           0 : void KSyncUserSockVrfStatsContext::Process() {
    1195           0 :     if (req_->get_h_op() == sandesh_op::DUMP) {
    1196           0 :         VrfStatsDumpHandler dump;
    1197           0 :         dump.SendDumpResponse(GetSeqNum(), req_);
    1198           0 :     } else if (req_->get_h_op() == sandesh_op::GET) {
    1199           0 :         VrfStatsDumpHandler dump;
    1200           0 :         dump.SendGetResponse(GetSeqNum(), req_->get_vsr_vrf());
    1201           0 :     }
    1202           0 : }
    1203             : 
    1204           0 : void KSyncUserSockContext::VrfStatsMsgHandler(vr_vrf_stats_req *req) {
    1205           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1206             :     KSyncUserSockVrfStatsContext *vrfctx = new KSyncUserSockVrfStatsContext(
    1207           0 :                                                           GetSeqNum(), req);
    1208             : 
    1209           0 :     if (sock->IsBlockMsgProcessing()) {
    1210           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
    1211           0 :         sock->ctx_queue_.push(vrfctx);
    1212           0 :     } else {
    1213           0 :         vrfctx->Process();
    1214           0 :         delete vrfctx;
    1215             :     }
    1216           0 : }
    1217             : 
    1218           0 : void KSyncUserSockDropStatsContext::Process() {
    1219           0 :     if (req_->get_h_op() == sandesh_op::GET) {
    1220           0 :         DropStatsDumpHandler dump;
    1221           0 :         dump.SendGetResponse(GetSeqNum(), 0);
    1222           0 :     }
    1223           0 : }
    1224             : 
    1225           0 : void KSyncUserSockContext::DropStatsMsgHandler(vr_drop_stats_req *req) {
    1226           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1227             :     KSyncUserSockDropStatsContext *dropctx = new KSyncUserSockDropStatsContext(
    1228           0 :                                                           GetSeqNum(), req);
    1229             : 
    1230           0 :     if (sock->IsBlockMsgProcessing()) {
    1231           0 :         std::scoped_lock lock(sock->ctx_queue_lock_);
    1232           0 :         sock->ctx_queue_.push(dropctx);
    1233           0 :     } else {
    1234           0 :         dropctx->Process();
    1235           0 :         delete dropctx;
    1236             :     }
    1237           0 : }
    1238             : 
    1239           0 : void MockDumpHandlerBase::SendDumpResponse(uint32_t seq_num, Sandesh *from_req) {
    1240           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1241             :     struct nl_client cl;
    1242           0 :     int error = 0, ret;
    1243           0 :     uint8_t *buf = NULL;
    1244           0 :     uint32_t buf_len = 0, encode_len = 0, tot_encode_len = 0;
    1245           0 :     struct nlmsghdr *nlh = NULL;
    1246           0 :     bool more = false;
    1247           0 :     int count = 0;
    1248           0 :     unsigned int resp_code = 0;
    1249             : 
    1250           0 :     if (KSyncSockTypeMap::error_code()) {
    1251           0 :         int ret_code = -KSyncSockTypeMap::error_code();
    1252           0 :         ret_code &= ~VR_MESSAGE_DUMP_INCOMPLETE;
    1253           0 :         KSyncSockTypeMap::SimulateResponse(seq_num, ret_code, 0);
    1254           0 :         return;
    1255             :     }
    1256           0 :     Sandesh *req = GetFirst(from_req);
    1257           0 :     if (req != NULL) {
    1258           0 :         nl_init_generic_client_req(&cl, KSyncSock::GetNetlinkFamilyId());
    1259           0 :         if ((ret = nl_build_header(&cl, &buf, &buf_len)) < 0) {
    1260           0 :             LOG(DEBUG, "Error creating interface DUMP message : " << ret);
    1261           0 :             nl_free(&cl);
    1262           0 :             return;
    1263             :         }
    1264             : 
    1265           0 :         nlh = (struct nlmsghdr *)cl.cl_buf;
    1266           0 :         nlh->nlmsg_seq = seq_num;
    1267             :     }
    1268             : 
    1269           0 :     while(req != NULL) {
    1270           0 :         encode_len = req->WriteBinary(buf, buf_len, &error);
    1271           0 :         if (error != 0) {
    1272           0 :             break;
    1273             :         }
    1274           0 :         buf += encode_len;
    1275           0 :         buf_len -= encode_len;
    1276           0 :         tot_encode_len += encode_len;
    1277           0 :         count++;
    1278             : 
    1279           0 :         req = GetNext(req);
    1280             :         //If the remaining buffer length cannot accomodate any more encoded
    1281             :         //messages, quit from the loop.
    1282           0 :         if (req != NULL && buf_len < encode_len) {
    1283           0 :             more = true;
    1284           0 :             break;
    1285             :         }
    1286             :     }
    1287             : 
    1288           0 :     if (error) {
    1289           0 :         KSyncSockTypeMap::SimulateResponse(seq_num, -ENOENT, 0);
    1290           0 :         nl_free(&cl);
    1291           0 :         return;
    1292             :     }
    1293             : 
    1294           0 :     resp_code = count;
    1295           0 :     if (count > 0) {
    1296           0 :         resp_code = count;
    1297           0 :         if (more) {
    1298           0 :             resp_code = resp_code | VR_MESSAGE_DUMP_INCOMPLETE;
    1299             :         }
    1300             :         //Send Vr-Response (with multi-flag set)
    1301           0 :         KSyncSockTypeMap::SimulateResponse(seq_num, resp_code, NLM_F_MULTI);
    1302             : 
    1303             :         //Send dump-response containing objects (with multi-flag set)
    1304           0 :         nlh->nlmsg_flags |= NLM_F_MULTI;
    1305           0 :         nl_update_header(&cl, tot_encode_len);
    1306           0 :         sock->AddNetlinkTxBuff(&cl);
    1307             :     } else {
    1308           0 :         KSyncSockTypeMap::SimulateResponse(seq_num, resp_code, 0);
    1309             :     }
    1310             : }
    1311             : 
    1312           2 : void MockDumpHandlerBase::SendGetResponse(uint32_t seq_num, int idx) {
    1313           2 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1314             :     struct nl_client cl;
    1315           2 :     int error = 0, ret;
    1316           2 :     uint8_t *buf = NULL;
    1317           2 :     uint32_t buf_len = 0, encode_len = 0;
    1318             :     struct nlmsghdr *nlh;
    1319             : 
    1320             :     /* To simulate error code return the test code has to call
    1321             :      * KSyncSockTypeMap::set_error_code() with required error code and
    1322             :      * invoke get request */
    1323           2 :     if (KSyncSockTypeMap::error_code()) {
    1324           0 :         int ret_code = -KSyncSockTypeMap::error_code();
    1325           0 :         ret_code &= ~VR_MESSAGE_DUMP_INCOMPLETE;
    1326           0 :         KSyncSockTypeMap::SimulateResponse(seq_num, ret_code, 0);
    1327           0 :         return;
    1328             :     }
    1329           2 :     Sandesh *req = Get(idx);
    1330           2 :     if (req == NULL) {
    1331           0 :         KSyncSockTypeMap::SimulateResponse(seq_num, -ENOENT, 0);
    1332           0 :         return;
    1333             :     }
    1334           2 :     nl_init_generic_client_req(&cl, KSyncSock::GetNetlinkFamilyId());
    1335           2 :     if ((ret = nl_build_header(&cl, &buf, &buf_len)) < 0) {
    1336           0 :         LOG(DEBUG, "Error creating interface DUMP message : " << ret);
    1337           0 :         nl_free(&cl);
    1338           0 :         return;
    1339             :     }
    1340             : 
    1341           2 :     nlh = (struct nlmsghdr *)cl.cl_buf;
    1342           2 :     nlh->nlmsg_seq = seq_num;
    1343             : 
    1344           2 :     int resp_len = EncodeVrResponse(buf, buf_len, seq_num, 0);
    1345           2 :     buf += resp_len;
    1346           2 :     buf_len -= resp_len;
    1347             : 
    1348           2 :     encode_len = req->WriteBinary(buf, buf_len, &error);
    1349           2 :     if (error) {
    1350           0 :         KSyncSockTypeMap::SimulateResponse(seq_num, -ENOENT, 0);
    1351           0 :         nl_free(&cl);
    1352           0 :         return;
    1353             :     }
    1354           2 :     buf += encode_len;
    1355           2 :     buf_len -= encode_len;
    1356             : 
    1357           2 :     nl_update_header(&cl, encode_len + resp_len);
    1358           2 :     sock->AddNetlinkTxBuff(&cl);
    1359             : }
    1360             : 
    1361           0 : Sandesh* IfDumpHandler::Get(int idx) {
    1362           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1363           0 :     KSyncSockTypeMap::ksync_map_if::const_iterator it;
    1364           0 :     static vr_interface_req req;
    1365             : 
    1366           0 :     it = sock->if_map.find(idx);
    1367           0 :     if (it != sock->if_map.end()) {
    1368           0 :         req = it->second;
    1369           0 :         return &req;
    1370             :     }
    1371           0 :     return NULL;
    1372             : }
    1373             : 
    1374           0 : Sandesh* IfDumpHandler::GetFirst(Sandesh *from_req) {
    1375           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1376           0 :     KSyncSockTypeMap::ksync_map_if::const_iterator it;
    1377           0 :     static vr_interface_req req;
    1378             :     int idx;
    1379             :     vr_interface_req *orig_req;
    1380           0 :     orig_req = static_cast<vr_interface_req *>(from_req);
    1381             : 
    1382           0 :     idx = orig_req->get_vifr_marker();
    1383           0 :     it = sock->if_map.upper_bound(idx);
    1384             : 
    1385           0 :     if (it != sock->if_map.end()) {
    1386           0 :         req = it->second;
    1387           0 :         req.set_vifr_flags(orig_req->get_vifr_flags());
    1388           0 :         return &req;
    1389             :     }
    1390           0 :     return NULL;
    1391             : }
    1392             : 
    1393           0 : Sandesh* IfDumpHandler::GetNext(Sandesh *input) {
    1394             :     static int last_intf_id = 0;
    1395             :     static int32_t last_if_flags = 0;
    1396           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1397           0 :     KSyncSockTypeMap::ksync_map_if::const_iterator it;
    1398           0 :     static vr_interface_req req, *r;
    1399             : 
    1400           0 :     r = dynamic_cast<vr_interface_req *>(input);
    1401           0 :     if (r != NULL) {
    1402             :         /* GetNext on vr_interface_req should return a dummy drop-stats object.
    1403             :          * We need to store the interface index which will be used during
    1404             :          * GetNext of IfDumpHandler when invoked with vr_drop_stats_req as
    1405             :          * argument */
    1406           0 :         last_intf_id = r->get_vifr_idx();
    1407           0 :         last_if_flags = r->get_vifr_flags();
    1408           0 :         if (r->get_vifr_flags() & VIF_FLAG_GET_DROP_STATS) {
    1409           0 :             return &drop_stats_req;
    1410             :         }
    1411             :     }
    1412           0 :     it = sock->if_map.upper_bound(last_intf_id);
    1413             : 
    1414           0 :     if (it != sock->if_map.end()) {
    1415           0 :         req = it->second;
    1416           0 :         req.set_vifr_flags(last_if_flags);
    1417           0 :         return &req;
    1418             :     }
    1419           0 :     return NULL;
    1420             : }
    1421             : 
    1422           0 : Sandesh* NHDumpHandler::Get(int idx) {
    1423           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1424           0 :     KSyncSockTypeMap::ksync_map_nh::const_iterator it;
    1425           0 :     static vr_nexthop_req req;
    1426             : 
    1427           0 :     it = sock->nh_map.find(idx);
    1428           0 :     if (it != sock->nh_map.end()) {
    1429           0 :         req = it->second;
    1430           0 :         return &req;
    1431             :     }
    1432           0 :     return NULL;
    1433             : }
    1434             : 
    1435           0 : Sandesh* NHDumpHandler::GetFirst(Sandesh *from_req) {
    1436           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1437           0 :     KSyncSockTypeMap::ksync_map_nh::const_iterator it;
    1438           0 :     static vr_nexthop_req req;
    1439             :     vr_nexthop_req *orig_req;
    1440           0 :     orig_req = static_cast<vr_nexthop_req *>(from_req);
    1441             :     int idx;
    1442             : 
    1443           0 :     idx = orig_req->get_nhr_marker();
    1444           0 :     it = sock->nh_map.upper_bound(idx);
    1445           0 :     if (it != sock->nh_map.end()) {
    1446           0 :         req = it->second;
    1447           0 :         return &req;
    1448             :     }
    1449           0 :     return NULL;
    1450             : }
    1451             : 
    1452           0 : Sandesh* NHDumpHandler::GetNext(Sandesh *input) {
    1453           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1454           0 :     KSyncSockTypeMap::ksync_map_nh::const_iterator it;
    1455           0 :     static vr_nexthop_req req, *r;
    1456             : 
    1457           0 :     r = static_cast<vr_nexthop_req *>(input);
    1458           0 :     it = sock->nh_map.upper_bound(r->get_nhr_id());
    1459             : 
    1460           0 :     if (it != sock->nh_map.end()) {
    1461           0 :         req = it->second;
    1462           0 :         return &req;
    1463             :     }
    1464           0 :     return NULL;
    1465             : }
    1466             : 
    1467           0 : Sandesh* MplsDumpHandler::Get(int idx) {
    1468           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1469           0 :     KSyncSockTypeMap::ksync_map_mpls::const_iterator it;
    1470           0 :     static vr_mpls_req req;
    1471             : 
    1472           0 :     it = sock->mpls_map.find(idx);
    1473           0 :     if (it != sock->mpls_map.end()) {
    1474           0 :         req = it->second;
    1475           0 :         return &req;
    1476             :     }
    1477           0 :     return NULL;
    1478             : }
    1479             : 
    1480           0 : Sandesh* MplsDumpHandler::GetFirst(Sandesh *from_req) {
    1481           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1482           0 :     KSyncSockTypeMap::ksync_map_mpls::const_iterator it;
    1483           0 :     static vr_mpls_req req;
    1484             :     vr_mpls_req *orig_req;
    1485           0 :     orig_req = static_cast<vr_mpls_req *>(from_req);
    1486             :     int idx;
    1487             : 
    1488           0 :     idx = orig_req->get_mr_marker();
    1489           0 :     it = sock->mpls_map.upper_bound(idx);
    1490             : 
    1491           0 :     if (it != sock->mpls_map.end()) {
    1492           0 :         req = it->second;
    1493           0 :         return &req;
    1494             :     }
    1495           0 :     return NULL;
    1496             : }
    1497             : 
    1498           0 : Sandesh* MplsDumpHandler::GetNext(Sandesh *input) {
    1499           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1500           0 :     KSyncSockTypeMap::ksync_map_mpls::const_iterator it;
    1501           0 :     static vr_mpls_req req, *r;
    1502             : 
    1503           0 :     r = static_cast<vr_mpls_req *>(input);
    1504           0 :     it = sock->mpls_map.upper_bound(r->get_mr_label());
    1505             : 
    1506           0 :     if (it != sock->mpls_map.end()) {
    1507           0 :         req = it->second;
    1508           0 :         return &req;
    1509             :     }
    1510           0 :     return NULL;
    1511             : }
    1512             : 
    1513           0 : Sandesh* VrfDumpHandler::Get(int idx) {
    1514           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1515           0 :     KSyncSockTypeMap::ksync_map_vrf::const_iterator it;
    1516           0 :     static vr_vrf_req req;
    1517             : 
    1518           0 :     it = sock->vrf_map.find(idx);
    1519           0 :     if (it != sock->vrf_map.end()) {
    1520           0 :         req = it->second;
    1521           0 :         return &req;
    1522             :     }
    1523           0 :     return NULL;
    1524             : }
    1525             : 
    1526           0 : Sandesh* VrfDumpHandler::GetFirst(Sandesh *from_req) {
    1527           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1528           0 :     KSyncSockTypeMap::ksync_map_vrf::const_iterator it;
    1529           0 :     static vr_vrf_req req;
    1530             :     vr_vrf_req *orig_req;
    1531           0 :     orig_req = static_cast<vr_vrf_req *>(from_req);
    1532             :     int idx;
    1533             : 
    1534           0 :     idx = orig_req->get_vrf_marker();
    1535           0 :     it = sock->vrf_map.upper_bound(idx);
    1536             : 
    1537           0 :     if (it != sock->vrf_map.end()) {
    1538           0 :         req = it->second;
    1539           0 :         return &req;
    1540             :     }
    1541           0 :     return NULL;
    1542             : }
    1543             : 
    1544           0 : Sandesh* VrfDumpHandler::GetNext(Sandesh *input) {
    1545           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1546           0 :     KSyncSockTypeMap::ksync_map_vrf::const_iterator it;
    1547           0 :     static vr_vrf_req req, *r;
    1548             : 
    1549           0 :     r = static_cast<vr_vrf_req *>(input);
    1550           0 :     it = sock->vrf_map.upper_bound(r->get_vrf_idx());
    1551             : 
    1552           0 :     if (it != sock->vrf_map.end()) {
    1553           0 :         req = it->second;
    1554           0 :         return &req;
    1555             :     }
    1556           0 :     return NULL;
    1557             : }
    1558             : 
    1559           0 : Sandesh* MirrorDumpHandler::Get(int idx) {
    1560           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1561           0 :     KSyncSockTypeMap::ksync_map_mirror::const_iterator it;
    1562           0 :     static vr_mirror_req req;
    1563             : 
    1564           0 :     it = sock->mirror_map.find(idx);
    1565           0 :     if (it != sock->mirror_map.end()) {
    1566           0 :         req = it->second;
    1567           0 :         return &req;
    1568             :     }
    1569           0 :     return NULL;
    1570             : }
    1571             : 
    1572           0 : Sandesh* MirrorDumpHandler::GetFirst(Sandesh *from_req) {
    1573           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1574           0 :     KSyncSockTypeMap::ksync_map_mirror::const_iterator it;
    1575           0 :     static vr_mirror_req req;
    1576             :     vr_mirror_req *orig_req;
    1577           0 :     orig_req = static_cast<vr_mirror_req *>(from_req);
    1578             :     int idx;
    1579             : 
    1580           0 :     idx = orig_req->get_mirr_marker();
    1581           0 :     it = sock->mirror_map.upper_bound(idx);
    1582             : 
    1583           0 :     if (it != sock->mirror_map.end()) {
    1584           0 :         req = it->second;
    1585           0 :         return &req;
    1586             :     }
    1587           0 :     return NULL;
    1588             : }
    1589             : 
    1590           0 : Sandesh* MirrorDumpHandler::GetNext(Sandesh *input) {
    1591           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1592           0 :     KSyncSockTypeMap::ksync_map_mirror::const_iterator it;
    1593           0 :     static vr_mirror_req req, *r;
    1594             : 
    1595           0 :     r = static_cast<vr_mirror_req *>(input);
    1596           0 :     it = sock->mirror_map.upper_bound(r->get_mirr_index());
    1597             : 
    1598           0 :     if (it != sock->mirror_map.end()) {
    1599           0 :         req = it->second;
    1600           0 :         return &req;
    1601             :     }
    1602           0 :     return NULL;
    1603             : }
    1604             : 
    1605           0 : Sandesh* RouteDumpHandler::GetFirst(Sandesh *from_req) {
    1606           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1607           0 :     KSyncSockTypeMap::ksync_rt_tree::const_iterator it;
    1608           0 :     static vr_route_req req;
    1609           0 :     vr_route_req *orig_req, key;
    1610           0 :     orig_req = static_cast<vr_route_req *>(from_req);
    1611             : 
    1612           0 :     key.set_rtr_family(orig_req->get_rtr_family());
    1613           0 :     key.set_rtr_vrf_id(orig_req->get_rtr_vrf_id());
    1614           0 :     if (orig_req->get_rtr_marker().size() || orig_req->get_rtr_mac().size()) {
    1615           0 :         if (orig_req->get_rtr_family() == AF_BRIDGE) {
    1616           0 :             key.set_rtr_mac(orig_req->get_rtr_mac());
    1617             :         } else {
    1618           0 :             key.set_rtr_prefix(orig_req->get_rtr_marker());
    1619           0 :             key.set_rtr_prefix_len(orig_req->get_rtr_marker_plen());
    1620             :         }
    1621           0 :         it = sock->rt_tree.upper_bound(key);
    1622             :     } else {
    1623           0 :         std::vector<int8_t> rtr_prefix;
    1624           0 :         if (orig_req->get_rtr_family() == AF_BRIDGE) {
    1625           0 :             key.set_rtr_mac(rtr_prefix);
    1626             :         } else {
    1627           0 :             key.set_rtr_prefix(rtr_prefix);
    1628           0 :             key.set_rtr_prefix_len(0);
    1629             :         }
    1630           0 :         it = sock->rt_tree.lower_bound(key);
    1631           0 :     }
    1632             : 
    1633             : 
    1634           0 :     if (it != sock->rt_tree.end()) {
    1635           0 :         if ((it->get_rtr_vrf_id() != orig_req->get_rtr_vrf_id()) ||
    1636           0 :             (it->get_rtr_family() != orig_req->get_rtr_family())) {
    1637           0 :             return NULL;
    1638             :         }
    1639           0 :         req = *it;
    1640           0 :         return &req;
    1641             :     }
    1642           0 :     return NULL;
    1643           0 : }
    1644             : 
    1645           0 : Sandesh* RouteDumpHandler::GetNext(Sandesh *input) {
    1646           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1647           0 :     KSyncSockTypeMap::ksync_rt_tree::const_iterator it;
    1648           0 :     static vr_route_req req, *r, key;
    1649             : 
    1650           0 :     r = static_cast<vr_route_req *>(input);
    1651             : 
    1652           0 :     key.set_rtr_vrf_id(r->get_rtr_vrf_id());
    1653           0 :     key.set_rtr_family(r->get_rtr_family());
    1654           0 :     if (r->get_rtr_family() == AF_BRIDGE) {
    1655           0 :         key.set_rtr_mac(r->get_rtr_mac());
    1656             :     } else {
    1657           0 :         key.set_rtr_prefix(r->get_rtr_prefix());
    1658           0 :         key.set_rtr_prefix_len(r->get_rtr_prefix_len());
    1659             :     }
    1660           0 :     it = sock->rt_tree.upper_bound(key);
    1661             : 
    1662           0 :     if (it != sock->rt_tree.end()) {
    1663           0 :         if ((it->get_rtr_vrf_id() != r->get_rtr_vrf_id()) ||
    1664           0 :             (it->get_rtr_family() != r->get_rtr_family())) {
    1665           0 :             return NULL;
    1666             :         }
    1667           0 :         req = *it;
    1668           0 :         return &req;
    1669             :     }
    1670           0 :     return NULL;
    1671             : }
    1672             : 
    1673           0 : Sandesh* VrfAssignDumpHandler::GetFirst(Sandesh *from_req) {
    1674           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1675           0 :     KSyncSockTypeMap::ksync_vrf_assign_tree::const_iterator it;
    1676           0 :     static vr_vrf_assign_req req;
    1677           0 :     vr_vrf_assign_req *orig_req, key;
    1678           0 :     orig_req = static_cast<vr_vrf_assign_req *>(from_req);
    1679             : 
    1680           0 :     key.set_var_vif_index(orig_req->get_var_vif_index());
    1681           0 :     key.set_var_vlan_id(orig_req->get_var_marker());
    1682           0 :     it = sock->vrf_assign_tree.upper_bound(key);
    1683             : 
    1684           0 :     if (it != sock->vrf_assign_tree.end()) {
    1685           0 :         req = *it;
    1686           0 :         return &req;
    1687             :     }
    1688           0 :     return NULL;
    1689           0 : }
    1690             : 
    1691           0 : Sandesh* VrfAssignDumpHandler::GetNext(Sandesh *input) {
    1692           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1693           0 :     KSyncSockTypeMap::ksync_vrf_assign_tree::const_iterator it;
    1694           0 :     static vr_vrf_assign_req req, *r, key;
    1695             : 
    1696           0 :     r = static_cast<vr_vrf_assign_req *>(input);
    1697             : 
    1698           0 :     key.set_var_vif_index(r->get_var_vif_index());
    1699           0 :     key.set_var_vlan_id(r->get_var_vlan_id());
    1700           0 :     it = sock->vrf_assign_tree.upper_bound(key);
    1701             : 
    1702           0 :     if (it != sock->vrf_assign_tree.end()) {
    1703           0 :         req = *it;
    1704           0 :         return &req;
    1705             :     }
    1706           0 :     return NULL;
    1707             : }
    1708             : 
    1709           0 : Sandesh* VrfStatsDumpHandler::Get(int idx) {
    1710           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1711           0 :     KSyncSockTypeMap::ksync_map_vrf_stats::const_iterator it;
    1712           0 :     static vr_vrf_stats_req req;
    1713             : 
    1714           0 :     it = sock->vrf_stats_map.find(idx);
    1715           0 :     if (it != sock->vrf_stats_map.end()) {
    1716           0 :         req = it->second;
    1717           0 :         return &req;
    1718             :     }
    1719           0 :     return NULL;
    1720             : }
    1721             : 
    1722           0 : Sandesh* VrfStatsDumpHandler::GetFirst(Sandesh *from_req) {
    1723           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1724           0 :     KSyncSockTypeMap::ksync_map_vrf_stats::const_iterator it;
    1725           0 :     static vr_vrf_stats_req req;
    1726             :     int idx;
    1727             :     vr_vrf_stats_req *orig_req;
    1728           0 :     orig_req = static_cast<vr_vrf_stats_req *>(from_req);
    1729             : 
    1730           0 :     idx = orig_req->get_vsr_marker();
    1731           0 :     it = sock->vrf_stats_map.upper_bound(idx);
    1732             : 
    1733           0 :     if (it != sock->vrf_stats_map.end()) {
    1734           0 :         req = it->second;
    1735           0 :         return &req;
    1736             :     }
    1737           0 :     return NULL;
    1738             : }
    1739             : 
    1740           0 : Sandesh* VrfStatsDumpHandler::GetNext(Sandesh *input) {
    1741           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1742           0 :     KSyncSockTypeMap::ksync_map_vrf_stats::const_iterator it;
    1743           0 :     static vr_vrf_stats_req req, *r;
    1744             : 
    1745           0 :     r = static_cast<vr_vrf_stats_req *>(input);
    1746           0 :     it = sock->vrf_stats_map.upper_bound(r->get_vsr_vrf());
    1747             : 
    1748           0 :     if (it != sock->vrf_stats_map.end()) {
    1749           0 :         req = it->second;
    1750           0 :         return &req;
    1751             :     }
    1752           0 :     return NULL;
    1753             : }
    1754             : 
    1755           0 : Sandesh* VxLanDumpHandler::Get(int idx) {
    1756           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1757           0 :     KSyncSockTypeMap::ksync_map_vxlan::const_iterator it;
    1758           0 :     static vr_vxlan_req req;
    1759             : 
    1760           0 :     it = sock->vxlan_map.find(idx);
    1761           0 :     if (it != sock->vxlan_map.end()) {
    1762           0 :         req = it->second;
    1763           0 :         return &req;
    1764             :     }
    1765           0 :     return NULL;
    1766             : }
    1767             : 
    1768           0 : Sandesh* VxLanDumpHandler::GetFirst(Sandesh *from_req) {
    1769           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1770           0 :     KSyncSockTypeMap::ksync_map_vxlan::const_iterator it;
    1771           0 :     static vr_vxlan_req req;
    1772             :     vr_vxlan_req *orig_req;
    1773           0 :     orig_req = static_cast<vr_vxlan_req *>(from_req);
    1774             :     int idx;
    1775             : 
    1776           0 :     idx = orig_req->get_vxlanr_vnid();
    1777           0 :     it = sock->vxlan_map.upper_bound(idx);
    1778             : 
    1779           0 :     if (it != sock->vxlan_map.end()) {
    1780           0 :         req = it->second;
    1781           0 :         return &req;
    1782             :     }
    1783           0 :     return NULL;
    1784             : }
    1785             : 
    1786           0 : Sandesh* VxLanDumpHandler::GetNext(Sandesh *input) {
    1787           0 :     KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
    1788           0 :     KSyncSockTypeMap::ksync_map_vxlan::const_iterator it;
    1789           0 :     static vr_vxlan_req req, *r;
    1790             : 
    1791           0 :     r = static_cast<vr_vxlan_req *>(input);
    1792           0 :     it = sock->vxlan_map.upper_bound(r->get_vxlanr_vnid());
    1793             : 
    1794           0 :     if (it != sock->vxlan_map.end()) {
    1795           0 :         req = it->second;
    1796           0 :         return &req;
    1797             :     }
    1798           0 :     return NULL;
    1799             : }

Generated by: LCOV version 1.14