Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "kstate.h" 6 : #include "interface_kstate.h" 7 : #include <iomanip> 8 : #include <sstream> 9 : #include "vr_interface.h" 10 : 11 : using namespace std; 12 : 13 0 : InterfaceKState::InterfaceKState(KInterfaceResp *obj, const std::string &ctx, 14 0 : vr_interface_req &req, int id) : 15 0 : KState(ctx, obj) { 16 0 : if (id >= 0) { 17 0 : req.set_h_op(sandesh_op::GET); 18 0 : req.set_vifr_idx(id); 19 : } else { 20 0 : InitDumpRequest(req); 21 0 : req.set_vifr_marker(-1); 22 : } 23 0 : } 24 : 25 0 : void InterfaceKState::InitDumpRequest(vr_interface_req &req) const { 26 0 : req.set_h_op(sandesh_op::DUMP); 27 0 : req.set_vifr_idx(0); 28 0 : } 29 : 30 0 : void InterfaceKState::Handler() { 31 0 : KInterfaceResp *resp = static_cast<KInterfaceResp *>(response_object_); 32 0 : if (resp) { 33 0 : if (MoreData()) { 34 : /* There are more interfaces in Kernel. We need to query them from 35 : * Kernel and send it to Sandesh. 36 : */ 37 0 : SendResponse(); 38 0 : SendNextRequest(); 39 : } else { 40 0 : resp->set_context(response_context_); 41 0 : resp->Response(); 42 0 : more_context_ = boost::any(); 43 : } 44 : } 45 0 : } 46 : 47 0 : void InterfaceKState::SendNextRequest() { 48 0 : vr_interface_req req; 49 0 : InitDumpRequest(req); 50 0 : int32_t idx = boost::any_cast<int32_t>(more_context_); 51 0 : req.set_vifr_marker(idx); 52 0 : EncodeAndSend(req); 53 0 : } 54 : 55 0 : void InterfaceKState::SendResponse() { 56 : 57 0 : KInterfaceResp *resp = static_cast<KInterfaceResp *>(response_object_); 58 0 : resp->set_context(response_context_); 59 0 : resp->set_more(true); 60 0 : resp->Response(); 61 : 62 0 : response_object_ = new KInterfaceResp(); 63 0 : } 64 : 65 0 : const string InterfaceKState::TypeToString(int if_type) const { 66 0 : unsigned short type = if_type; 67 0 : switch(type) { 68 0 : case VIF_TYPE_HOST: 69 0 : return "HOST"; 70 0 : case VIF_TYPE_AGENT: 71 0 : return "AGENT"; 72 0 : case VIF_TYPE_PHYSICAL: 73 0 : return "PHYSICAL"; 74 0 : case VIF_TYPE_VIRTUAL: 75 0 : return "VIRTUAL"; 76 0 : default: 77 0 : return "INVALID"; 78 : } 79 : } 80 : 81 0 : const string InterfaceKState::FlagsToString(int flags) const { 82 0 : string str("");; 83 0 : if (flags == 0) { 84 0 : return "NIL"; 85 : } 86 0 : if (flags & VIF_FLAG_POLICY_ENABLED) { 87 0 : str += "POLICY "; 88 : } 89 0 : if (flags & VIF_FLAG_MIRROR_RX) { 90 0 : str += "MIRR_RX "; 91 : } 92 0 : if (flags & VIF_FLAG_MIRROR_TX) { 93 0 : str += "MIRR_TX "; 94 : } 95 0 : return str; 96 0 : }