OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nh_kstate.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include "kstate.h"
6 #include "nh_kstate.h"
7 #include "vr_nexthop.h"
8 #include <net/ethernet.h>
9 #include <netinet/if_ether.h>
10 #include <iomanip>
11 #include <sstream>
12 
13 using namespace std;
14 
15 NHKState::NHKState(KNHResp *obj, const std::string &resp_ctx,
16  vr_nexthop_req &req, int id)
17  : KState(resp_ctx, obj) {
18 
19  req.set_nhr_id(id);
20  if (id >= 0) {
21  req.set_h_op(sandesh_op::GET);
22  } else {
23  req.set_h_op(sandesh_op::DUMP);
24  req.set_nhr_marker(-1);
25  }
26 }
27 
29  vr_nexthop_req req;
30  req.set_nhr_id(0);
31  req.set_h_op(sandesh_op::DUMP);
32  int32_t idx = boost::any_cast<int32_t>(more_context_);
33  req.set_nhr_marker(idx);
34  EncodeAndSend(req);
35 }
36 
38  KNHResp *resp = static_cast<KNHResp *>(response_object_);
39  if (resp) {
40  if (MoreData()) {
41  /* There are more nexthops in Kernel. We need to query them from
42  * Kernel and send it to Sandesh.
43  */
44  SendResponse();
46  } else {
47  resp->set_context(response_context_);
48  resp->Response();
49  more_context_ = boost::any();
50  }
51  }
52 }
53 
55 
56  KNHResp *resp = static_cast<KNHResp *>(response_object_);
57  resp->set_context(response_context_);
58  resp->set_more(true);
59  resp->Response();
60 
61  response_object_ = new KNHResp();
62 }
63 
64 
65 const string NHKState::TypeToString(int nh_type) const {
66  unsigned short type = nh_type;
67  switch(type) {
68  case NH_ENCAP:
69  return "ENCAP";
70  case NH_TUNNEL:
71  return "TUNNEL";
72  case NH_DISCARD:
73  return "DISCARD";
74  case NH_RCV:
75  return "RECEIVE";
76  case NH_RESOLVE:
77  return "RESOLVE";
78  case NH_COMPOSITE:
79  return "COMPOSITE";
80  case NH_DEAD:
81  return "DEAD";
82  default:
83  return "INVALID";
84  }
85 }
86 
87 const string NHKState::FamilyToString(int nh_family) const {
88  unsigned family = nh_family;
89  switch(family) {
90  case AF_INET:
91  return "AF_INET";
92  case AF_INET6:
93  return "AF_INET6";
94  default:
95  return "INVALID";
96  }
97 }
98 
99 const string NHKState::EncapFamilyToString(int nh_family) const {
100  unsigned family = nh_family;
101  switch(family) {
102  case ETHERTYPE_ARP:
103  return "ETH_P_ARP";
104  case 0:
105  return "NO_ENCAP";
106  default:
107  return "INVALID";
108  }
109 }
110 
111 const string NHKState::EncapToString(const vector<signed char> &encap) const {
112  ostringstream strm;
113  uint8_t ubyte;
114  vector<signed char>::const_iterator it = encap.begin();
115  strm << hex << setfill('0');
116  while(it != encap.end()) {
117  ubyte = (uint8_t) *it;
118  strm << setw(2) << (int)ubyte;
119  ++it;
120  }
121  return strm.str();
122 }
123 
124 const string NHKState::FlagsToString(uint32_t flags) const {
125  string flag_str, policy_str("POLICY "), gre_str("TUNNEL_GRE ");
126  string fabric_multicast("FABRIC_MULTICAST");
127  string l2_multicast("L2_MULTICAST");
128  string l3_multicast("L3_MULTICAST");
129  string multi_proto_multicast("MULTI_PROTO_MULTICAST");
130  string ecmp("ECMP");
131  string multicast_encap("MULTICAST_ENCAP");
132  string mpls_udp_str("TUNNEL_MPLS_UDP ");
133  string udp_str("TUNNEL_UDP "), sip_copy("TUNNEL_SIP_COPY ");
134  bool assigned = false;
135 
136  if (flags & NH_FLAG_VALID) {
137  flag_str.assign("VALID ");
138  assigned = true;
139  }
140  if (flags & NH_FLAG_POLICY_ENABLED) {
141  if (assigned) {
142  flag_str.append("| " + policy_str);
143  } else {
144  flag_str.assign(policy_str);
145  assigned = true;
146  }
147  }
148  if (flags & NH_FLAG_TUNNEL_GRE) {
149  if (assigned) {
150  flag_str.append("| " + gre_str);
151  } else {
152  flag_str.assign(gre_str);
153  assigned = true;
154  }
155  }
156  if (flags & NH_FLAG_TUNNEL_UDP) {
157  if (assigned) {
158  flag_str.append("| " + udp_str);
159  } else {
160  flag_str.assign(udp_str);
161  assigned = true;
162  }
163  }
164  if (flags & NH_FLAG_TUNNEL_UDP_MPLS) {
165  if (assigned) {
166  flag_str.append("| " + mpls_udp_str);
167  } else {
168  flag_str.assign(mpls_udp_str);
169  assigned = true;
170  }
171  }
172 
173  if (flags & NH_FLAG_COMPOSITE_ECMP) {
174  if (assigned) {
175  flag_str.append("| " + ecmp);
176  } else {
177  flag_str.assign(ecmp);
178  assigned = true;
179  }
180  }
181 
182  if (flags & NH_FLAG_COMPOSITE_FABRIC) {
183  if (assigned) {
184  flag_str.append("| " + fabric_multicast);
185  } else {
186  flag_str.assign(fabric_multicast);
187  assigned = true;
188  }
189  }
190 
191  if (flags & NH_FLAG_TUNNEL_SIP_COPY) {
192  if (assigned) {
193  flag_str.append("| " + sip_copy);
194  } else {
195  flag_str.assign(sip_copy);
196  assigned = true;
197  }
198  }
199 
200  if (!assigned) {
201  return "NIL";
202  }
203  return flag_str;
204 }
205 
206 void NHKState::SetComponentNH(vr_nexthop_req *req, KNHInfo &info) {
207  std::vector<KComponentNH> comp_nh_list;
208  KComponentNH comp_nh;
209 
210  if (req->get_nhr_type() != NH_COMPOSITE) {
211  return;
212  }
213 
214  const std::vector<int32_t> nh_list = req->get_nhr_nh_list();
215  const std::vector<int32_t> label_list = req->get_nhr_label_list();
216 
217  for (uint32_t i = 0; i < nh_list.size(); i++) {
218  comp_nh.set_nh_id(nh_list[i]);
219  comp_nh.set_label(label_list[i]);
220  comp_nh_list.push_back(comp_nh);
221  }
222  info.set_component_nh(comp_nh_list);
223 }
const std::string EncapToString(const std::vector< signed char > &encap) const
Definition: nh_kstate.cc:111
std::string response_context_
Definition: kstate.h:63
const std::string FamilyToString(int family) const
Definition: nh_kstate.cc:87
Definition: kstate.h:24
virtual void Handler()
Definition: nh_kstate.cc:37
boost::any more_context_
Definition: kstate.h:66
const std::string EncapFamilyToString(int family) const
Definition: nh_kstate.cc:99
virtual void SendResponse()
Definition: nh_kstate.cc:54
Sandesh * response_object_
Definition: kstate.h:64
const std::string TypeToString(int type) const
Definition: nh_kstate.cc:65
uint8_t type
Definition: load_balance.h:109
NHKState(KNHResp *obj, const std::string &resp_ctx, vr_nexthop_req &encoder, int id)
Definition: nh_kstate.cc:15
virtual void SendNextRequest()
Definition: nh_kstate.cc:28
const std::string FlagsToString(uint32_t flags) const
Definition: nh_kstate.cc:124
bool MoreData() const
Definition: kstate.cc:29
void EncodeAndSend(Sandesh &encoder)
Definition: kstate.cc:63
void SetComponentNH(vr_nexthop_req *req, KNHInfo &info)
Definition: nh_kstate.cc:206