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