OpenSDN source code
pkt_sandesh_flow.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <pkt/pkt_sandesh_flow.h>
7 #include <pkt/flow_mgmt.h>
10 #include <vector>
11 #include <boost/date_time/posix_time/posix_time.hpp>
12 #include <sstream>
13 #include <algorithm>
14 
15 #include <cmn/agent_stats.h>
16 #include <uve/agent_uve.h>
20 
21 static string InetRouteFlowMgmtKeyToString(uint16_t id,
22  InetRouteFlowMgmtKey *key) {
23  std::stringstream ss;
24  uint16_t plen = key->plen();
25  ss << id << PktSandeshFlow::kDelimiter;
26  ss << key->vrf_id() << PktSandeshFlow::kDelimiter;
27  ss << key->ip().to_string() << PktSandeshFlow::kDelimiter;
28  ss << plen;
29  return ss.str();
30 }
31 
32 #define SET_SANDESH_FLOW_DATA(agent, data, fe, info) \
33  data.set_vrf(fe->data().vrf); \
34  data.set_sip(fe->key().src_addr.to_string()); \
35  data.set_dip(fe->key().dst_addr.to_string()); \
36  data.set_src_port((unsigned)fe->key().src_port); \
37  data.set_dst_port((unsigned)fe->key().dst_port); \
38  data.set_protocol(fe->key().protocol); \
39  data.set_dest_vrf(fe->data().dest_vrf); \
40  data.set_uuid(UuidToString(fe->uuid())); \
41  data.set_action(fe->match_p().action_info.action); \
42  std::vector<ActionStr> action_str_l; \
43  SetActionStr(fe->match_p().action_info, action_str_l); \
44  if ((fe->match_p().action_info.action & TrafficAction::DROP_FLAGS) != 0) {\
45  data.set_drop_reason(fe->DropReasonStr(fe->data().drop_reason)); \
46  } \
47  data.set_action_str(action_str_l); \
48  std::vector<MirrorActionSpec>::const_iterator mait; \
49  std::vector<MirrorInfo> mirror_l; \
50  for (mait = fe->match_p().action_info.mirror_l.begin(); \
51  mait != fe->match_p().action_info.mirror_l.end(); \
52  ++mait) { \
53  MirrorInfo minfo; \
54  minfo.set_mirror_destination((*mait).ip.to_string()); \
55  minfo.set_mirror_port((*mait).port); \
56  mirror_l.push_back(minfo); \
57  } \
58  data.set_mirror_l(mirror_l); \
59  if (fe->is_flags_set(FlowEntry::IngressDir)) { \
60  data.set_direction("ingress"); \
61  } else { \
62  data.set_direction("egress"); \
63  } \
64  if (info) { \
65  data.set_stats_bytes(info->bytes()); \
66  data.set_stats_packets(info->packets()); \
67  } \
68  if (fe->is_flags_set(FlowEntry::NatFlow)) { \
69  data.set_nat("enabled"); \
70  } else { \
71  data.set_nat("disabled"); \
72  } \
73  data.set_gen_id(fe->gen_id()); \
74  data.set_flow_handle(fe->flow_handle()); \
75  data.set_refcount(fe->GetRefCount()); \
76  data.set_implicit_deny(fe->ImplicitDenyFlow() ? "yes" : "no"); \
77  data.set_short_flow( \
78  fe->is_flags_set(FlowEntry::ShortFlow) ? \
79  string("yes (") + fe->DropReasonStr(fe->short_flow_reason()) + \
80  ")": "no"); \
81  data.set_local_flow(fe->is_flags_set(FlowEntry::LocalFlow) ? "yes" : "no"); \
82  if (!fe->data().OriginVnSrcList().empty()) { \
83  data.set_src_vn_list(fe->data().OriginVnSrcList()); \
84  } else { \
85  data.set_src_vn_list(fe->data().SourceVnList()); \
86  } \
87  if (!fe->data().OriginVnDstList().empty()) { \
88  data.set_dst_vn_list(fe->data().OriginVnDstList()); \
89  } else { \
90  data.set_dst_vn_list(fe->data().DestinationVnList()); \
91  } \
92  if (!fe->data().origin_vn_src.empty()) { \
93  data.set_src_vn_match(fe->data().origin_vn_src); \
94  } else { \
95  data.set_src_vn_match(fe->data().source_vn_match); \
96  } \
97  if (!fe->data().origin_vn_dst.empty()) { \
98  data.set_dst_vn_match(fe->data().origin_vn_dst); \
99  } else { \
100  data.set_dst_vn_match(fe->data().dest_vn_match); \
101  } \
102  if (fe->is_flags_set(FlowEntry::EcmpFlow) && \
103  fe->data().component_nh_idx != CompositeNH::kInvalidComponentNHIdx) { \
104  data.set_ecmp_index(fe->data().component_nh_idx); \
105  } \
106  data.set_reverse_flow(fe->is_flags_set(FlowEntry::ReverseFlow) ? "yes" : "no"); \
107  Ip4Address fip(fe->fip()); \
108  data.set_fip(fip.to_string()); \
109  uint32_t fip_intf_id = fe->InterfaceKeyToId(agent, fe->fip_vmi()); \
110  data.set_fip_vm_interface_idx(fip_intf_id); \
111  SetAclInfo(data, fe); \
112  data.set_nh(fe->key().nh); \
113  if (fe->data().src_ip_nh.get() != NULL) { \
114  data.set_src_ip_nh(fe->data().src_ip_nh.get()->id()); \
115  } \
116  if (fe->data().rpf_nh.get() != NULL) { \
117  data.set_rpf_nh(fe->data().rpf_nh.get()->id()); \
118  } \
119  data.set_peer_vrouter(fe->peer_vrouter()); \
120  data.set_tunnel_type(fe->tunnel_type().ToString()); \
121  data.set_enable_rpf(fe->data().enable_rpf);\
122  if (fe->fsc()) {\
123  data.set_aging_protocol(fe->fsc()->flow_aging_key().proto);\
124  data.set_aging_port(fe->fsc()->flow_aging_key().port);\
125  }\
126  data.set_l3_flow(fe->l3_flow());\
127  uint16_t id = fe->flow_table()? fe->flow_table()->table_index() : 0xFFFF;\
128  data.set_table_id(id);\
129  data.set_deleted(fe->deleted());\
130  SandeshFlowIndexInfo flow_index_info;\
131  fe->SetEventSandeshData(&flow_index_info);\
132  data.set_flow_index_info(flow_index_info); \
133  data.set_fw_policy_match(fe->fw_policy_name_uuid()); \
134  FlowEntryInfo *mgmt_info = fe->flow_mgmt_info(); \
135  if (mgmt_info) {\
136  const FlowMgmtKeyTree &key_tree = mgmt_info->tree(); \
137  FlowMgmtKeyTree::const_iterator kt_it = key_tree.begin(); \
138  std::vector<SandeshInetRouteFlowMgmtEntryLink> key_list;\
139  while (kt_it != key_tree.end()) { \
140  InetRouteFlowMgmtKey *key = dynamic_cast<InetRouteFlowMgmtKey *> \
141  (kt_it->first);\
142  ++kt_it;\
143  if (!key) {\
144  continue;\
145  }\
146  if (id == 0xFFFF) {\
147  continue;\
148  }\
149  string key_str = InetRouteFlowMgmtKeyToString(id, key);\
150  SandeshInetRouteFlowMgmtEntryLink entry;\
151  entry.set_inet_route_flow_mgmt_key(key_str);\
152  key_list.push_back(entry);\
153  }\
154  data.set_inet_rt_keys(key_list);\
155  }\
156  data.set_local_tag_list(fe->local_tagset());\
157  data.set_remote_tag_list(fe->remote_tagset());\
158  data.set_remote_prefix(fe->RemotePrefix());\
159  const Interface *itfe = fe->intf_entry();\
160  if (itfe && (itfe->type() == Interface::VM_INTERFACE)) {\
161  const VmInterface *vmi_e = static_cast<const VmInterface *>(itfe);\
162  data.set_vmi(vmi_e->cfg_name());\
163  }\
164  data.set_underlay_gw_index(fe->data().underlay_gw_index_);\
165 
166 const std::string PktSandeshFlow::start_key = "0-0-0-0-0-0.0.0.0-0.0.0.0";
167 
169 
170 static void SetOneAclInfo(FlowAclInfo *policy, uint32_t action,
171  const MatchAclParamsList &acl_list) {
172  MatchAclParamsList::const_iterator it;
173  std::vector<FlowAclUuid> acl;
174 
175  for (it = acl_list.begin(); it != acl_list.end(); it++) {
176  FlowAclUuid f;
177  f.uuid = UuidToString(it->acl->GetUuid());
178  acl.push_back(f);
179  }
180  policy->set_acl(acl);
181  policy->set_action(action);
182 
183  std::vector<ActionStr> action_str_l;
184  for (it = acl_list.begin(); it != acl_list.end(); it++) {
185  FlowAction action_info = it->action_info;
186  action_info.action = action;
187  SetActionStr(action_info, action_str_l);
188  }
189  policy->set_action_str(action_str_l);
190 }
191 
192 static void SetAclInfo(SandeshFlowData &data, FlowEntry *fe) {
193  FlowAclInfo policy;
194 
195  SetOneAclInfo(&policy, fe->match_p().policy_action, fe->match_p().m_acl_l);
196  data.set_policy(policy);
197 
198  SetOneAclInfo(&policy, fe->match_p().out_policy_action,
199  fe->match_p().m_out_acl_l);
200  data.set_out_policy(policy);
201 
203  data.set_sg(policy);
204 
205  SetOneAclInfo(&policy, fe->match_p().sg_policy.out_action,
207  data.set_out_sg(policy);
208 
211  data.set_reverse_sg(policy);
212 
215  data.set_reverse_out_sg(policy);
216 
219  data.set_vrf_assign_acl(policy);
220 
221  FlowAction action_info;
222  action_info.action = fe->match_p().sg_policy.action_summary;
223  std::vector<ActionStr> action_str_l;
224  SetActionStr(action_info, action_str_l);
225  data.set_sg_action_summary(action_str_l);
226 
227  SetOneAclInfo(&policy, fe->match_p().mirror_action,
228  fe->match_p().m_mirror_acl_l);
229  data.set_mirror(policy);
230 
231  SetOneAclInfo(&policy, fe->match_p().out_mirror_action,
233  data.set_out_mirror(policy);
234 
235  SetOneAclInfo(&policy, fe->match_p().aps_policy.action,
236  fe->match_p().aps_policy.m_acl_l);
237  data.set_policy_set(policy);
238 
241  data.set_out_policy_set(policy);
242 
245  data.set_reverse_policy_set(policy);
246 
249  data.set_reverse_out_policy_set(policy);
250 
251  action_info.action = fe->match_p().aps_policy.action_summary;
252  action_str_l.clear();
253  SetActionStr(action_info, action_str_l);
254  data.set_aps_action_summary(action_str_l);
255 
256  SetOneAclInfo(&policy, fe->match_p().fwaas_policy.action,
258  data.set_fwaas_policy_set(policy);
259 
262  data.set_fwaas_out_policy_set(policy);
263 
266  data.set_fwaas_reverse_policy_set(policy);
267 
270  data.set_fwaas_reverse_out_policy_set(policy);
271 
272  action_info.action = fe->match_p().fwaas_policy.action_summary;
273  action_str_l.clear();
274  SetActionStr(action_info, action_str_l);
275  data.set_fwaas_action_summary(action_str_l);
276 
277  data.set_sg_rule_uuid(fe->sg_rule_uuid());
278  data.set_nw_ace_uuid(fe->nw_ace_uuid());
279 }
280 
282 
283 PktSandeshFlow::PktSandeshFlow(Agent *agent, FlowRecordsResp *obj,
284  std::string resp_ctx, std::string key):
285  Task((TaskScheduler::GetInstance()->GetTaskId("Agent::PktFlowResponder")),
286  0), resp_obj_(obj), resp_data_(resp_ctx),
287  flow_iteration_key_(), key_valid_(false), delete_op_(false), agent_(agent),
288  partition_id_(0) {
289  if (key != agent_->NullString()) {
290  if (SetFlowKey(key)) {
291  key_valid_ = true;
292  }
293  }
294 }
295 
297 }
298 
299 void PktSandeshFlow::SetSandeshFlowData(std::vector<SandeshFlowData> &list,
300  FlowEntry *fe, const FlowExportInfo *info) {
301  SandeshFlowData data;
302  SET_SANDESH_FLOW_DATA(agent_, data, fe, info);
303  list.push_back(data);
304 }
305 
307  resp->set_context(resp_data_);
308  resp->set_more(false);
309  resp->Response();
310 }
311 
312 string PktSandeshFlow::GetFlowKey(const FlowKey &key, uint16_t partition_id) {
313  std::stringstream ss;
314  ss << partition_id << kDelimiter;
315  ss << key.nh << kDelimiter;
316  ss << key.src_port << kDelimiter;
317  ss << key.dst_port << kDelimiter;
318  ss << (uint16_t)key.protocol << kDelimiter;
319  ss << key.src_addr.to_string() << kDelimiter;
320  ss << key.dst_addr.to_string();
321  return ss.str();
322 }
323 
324 bool PktSandeshFlow::SetFlowKey(string key) {
325  using std::istringstream;
326 
327  const char ch = kDelimiter;
328  size_t n = std::count(key.begin(), key.end(), ch);
329  if (n != 6) {
330  return false;
331  }
332  std::stringstream ss(key);
333  string item, sip, dip;
334  uint32_t proto = 0;
335 
336  if (getline(ss, item, ch)) {
337  istringstream(item) >> partition_id_;
338  }
339  if (getline(ss, item, ch)) {
340  istringstream(item) >> flow_iteration_key_.nh;
341  }
342  if (getline(ss, item, ch)) {
343  istringstream(item) >> flow_iteration_key_.src_port;
344  }
345  if (getline(ss, item, ch)) {
346  istringstream(item) >> flow_iteration_key_.dst_port;
347  }
348  if (getline(ss, item, ch)) {
349  istringstream(item) >> proto;
350  }
351  if (getline(ss, item, ch)) {
352  sip = item;
353  }
354  if (getline(ss, item, ch)) {
355  dip = item;
356  }
357  boost::system::error_code ec;
358  flow_iteration_key_.src_addr = IpAddress::from_string(sip.c_str(), ec);
359  flow_iteration_key_.dst_addr = IpAddress::from_string(dip.c_str(), ec);
360  if (flow_iteration_key_.src_addr.is_v4()) {
362  } else if (flow_iteration_key_.src_addr.is_v6()) {
364  }
366  return true;
367 }
368 
370  FlowTable::FlowEntryMap::iterator it;
371  std::vector<SandeshFlowData>& list =
372  const_cast<std::vector<SandeshFlowData>&>(resp_obj_->get_flow_list());
373  int count = 0;
374  bool flow_key_set = false;
375 
377  FlowErrorResp *resp = new FlowErrorResp();
378  SendResponse(resp);
379  return true;
380  }
381 
382  FlowTable *flow_obj = agent_->pkt()->flow_table(partition_id_);
383 
384  if (delete_op_) {
385  for (int i =0; i < agent_->flow_thread_count(); i++){
386  flow_obj = agent_->pkt()->flow_table(i);
387  flow_obj->DeleteAll();
388  }
390  return true;
391  }
392 
393  if (key_valid_) {
394  it = flow_obj->flow_entry_map_.upper_bound(flow_iteration_key_);
395  } else {
396  FlowErrorResp *resp = new FlowErrorResp();
397  SendResponse(resp);
398  return true;
399  }
400 
401  while (it == flow_obj->flow_entry_map_.end() &&
402  ++partition_id_ < agent_->flow_thread_count()) {
403  flow_obj = agent_->pkt()->flow_table(partition_id_);
404  it = flow_obj->flow_entry_map_.begin();
405  }
406 
407  while (it != flow_obj->flow_entry_map_.end()) {
408  FlowEntry *fe = it->second;
409  FlowStatsCollector *fec = fe->fsc();
410  const FlowExportInfo *info = NULL;
411  if (fec) {
412  info = fec->FindFlowExportInfo(fe);
413  }
414  SetSandeshFlowData(list, fe, info);
415  ++it;
416  count++;
417  if (count == kMaxFlowResponse) {
418  if (it != flow_obj->flow_entry_map_.end()) {
419  resp_obj_->set_flow_key(GetFlowKey(fe->key(), partition_id_));
420  flow_key_set = true;
421 
422  } else {
423  FlowKey key;
424  resp_obj_->set_flow_key(GetFlowKey(key, ++partition_id_));
425  flow_key_set = true;
426  }
427  break;
428  }
429 
430  while (it == flow_obj->flow_entry_map_.end()) {
431  if (++partition_id_ < agent_->flow_thread_count()) {
432  flow_obj = agent_->pkt()->flow_table(partition_id_);
433  it = flow_obj->flow_entry_map_.begin();
434  if (it != flow_obj->flow_entry_map_.end()) {
435  break;
436  }
437  } else {
438  break;
439  }
440  }
441  }
442 
443  if (!flow_key_set) {
444  resp_obj_->set_flow_key(PktSandeshFlow::start_key);
445  }
446 
448  return true;
449 }
450 
452 
453 void NextFlowRecordsSet::HandleRequest() const {
454  Agent *agent = Agent::GetInstance();
455  FlowRecordsResp *resp = new FlowRecordsResp();
456  PktSandeshFlow *task = new PktSandeshFlow(agent, resp, context(),
457  get_flow_key());
459  scheduler->Enqueue(task);
460 }
461 
462 void FetchAllFlowRecords::HandleRequest() const {
463  Agent *agent = Agent::GetInstance();
464  FlowRecordsResp *resp = new FlowRecordsResp();
465  PktSandeshFlow *task = new PktSandeshFlow(agent, resp, context(),
468  scheduler->Enqueue(task);
469 }
470 
471 void DeleteAllFlowRecords::HandleRequest() const {
472  FlowRecordsResp *resp = new FlowRecordsResp();
473 
475  context(),
477  task->set_delete_op(true);
479  scheduler->Enqueue(task);
480 }
481 
482 void FetchFlowRecord::HandleRequest() const {
483  FlowKey key;
484  Agent *agent = Agent::GetInstance();
485  FlowTable *flow_obj = NULL;
486 
487  key.nh = get_nh();
488  boost::system::error_code ec;
489  key.src_addr = IpAddress::from_string(get_sip(), ec);
490  key.dst_addr = IpAddress::from_string(get_dip(), ec);
491  if (key.src_addr.is_v4()) {
492  key.family = Address::INET;
493  } else if (key.src_addr.is_v6()) {
494  key.family = Address::INET6;
495  }
496  key.src_port = (unsigned)get_src_port();
497  key.dst_port = (unsigned)get_dst_port();
498  key.protocol = get_protocol();
499 
500  FlowTable::FlowEntryMap::iterator it;
501  for (int i = 0; i < agent->flow_thread_count(); i++) {
502  flow_obj = agent->pkt()->flow_table(i);
503  it = flow_obj->flow_entry_map_.find(key);
504  if (it != flow_obj->flow_entry_map_.end())
505  break;
506  }
507 
508  SandeshResponse *resp;
509  if (flow_obj && it != flow_obj->flow_entry_map_.end()) {
510  FlowRecordResp *flow_resp = new FlowRecordResp();
511  FlowEntry *fe = it->second;
512  FlowStatsCollector *fec = fe->fsc();
513  const FlowExportInfo *info = NULL;
514  if (fec) {
515  info = fec->FindFlowExportInfo(fe);
516  }
517  SandeshFlowData data;
518  SET_SANDESH_FLOW_DATA(agent, data, fe, info);
519  flow_resp->set_record(data);
520  resp = flow_resp;
521  } else {
522  resp = new FlowErrorResp();
523  }
524 
525  resp->set_context(context());
526  resp->set_more(false);
527  resp->Response();
528 }
529 
530 // Sandesh interface to modify flow aging interval
531 // Intended for use in testing only
532 void FlowAgeTimeReq::HandleRequest() const {
533  Agent *agent = Agent::GetInstance();
534  uint32_t age_time = get_new_age_time();
535 
538 
539  FlowAgeTimeResp *resp = new FlowAgeTimeResp();
540  if (obj == NULL) {
541  goto done;
542  }
543  resp->set_old_age_time(obj->GetAgeTimeInSeconds());
544 
545  if (age_time && age_time != resp->get_old_age_time()) {
546  obj->UpdateAgeTimeInSeconds(age_time);
547  resp->set_new_age_time(age_time);
548  } else {
549  resp->set_new_age_time(resp->get_old_age_time());
550  }
551 done:
552  resp->set_context(context());
553  resp->set_more(false);
554  resp->Response();
555 }
556 
557 void FetchLinkLocalFlowInfo::HandleRequest() const {
558  LinkLocalFlowInfoResp *resp = new LinkLocalFlowInfoResp();
559  std::vector<LinkLocalFlowInfo> &list =
560  const_cast<std::vector<LinkLocalFlowInfo>&>
561  (resp->get_linklocal_flow_list());
562 
563  const FlowTable::LinkLocalFlowInfoMap &flow_map =
565  FlowTable::LinkLocalFlowInfoMap::const_iterator it = flow_map.begin();
566  while (it != flow_map.end()) {
567  LinkLocalFlowInfo info;
568  info.fd = it->first;
569  info.flow_index = it->second.flow_index;
570  info.source_addr = it->second.flow_key.src_addr.to_string();
571  info.dest_addr = it->second.flow_key.dst_addr.to_string();
572  info.protocol = it->second.flow_key.protocol;
573  info.source_port = it->second.flow_key.src_port;
574  info.dest_port = it->second.flow_key.dst_port;
575  info.timestamp = integerToString(UTCUsecToPTime(it->second.timestamp));
576  list.push_back(info);
577  ++it;
578  }
579 
580  resp->set_context(context());
581  resp->set_more(false);
582  resp->Response();
583 }
584 
586  std::vector<SandeshFlowData>& list =
587  const_cast<std::vector<SandeshFlowData>&>(resp_->get_flow_list());
588  int count = 0;
589  bool flow_key_set = false;
590 
592  FlowErrorResp *resp = new FlowErrorResp();
593  SendResponse(resp);
594  return true;
595  }
596 
597  FlowTable *flow_obj = agent_->pkt()->flow_table(partition_id_);
599  const FlowStatsCollectorObject *fsc_obj = fm->Find(proto_, port_);
600  if (!fsc_obj) {
601  FlowErrorResp *resp = new FlowErrorResp();
602  SendResponse(resp);
603  return true;
604  }
605 
606  FlowTable::FlowEntryMap::iterator it;
607  if (key_valid_) {
608  it = flow_obj->flow_entry_map_.upper_bound(flow_iteration_key_);
609  } else {
610  FlowErrorResp *resp = new FlowErrorResp();
611  SendResponse(resp);
612  return true;
613  }
614 
615  while (it == flow_obj->flow_entry_map_.end() &&
616  ++partition_id_ < agent_->flow_thread_count()) {
617  flow_obj = agent_->pkt()->flow_table(partition_id_);
618  it = flow_obj->flow_entry_map_.begin();
619  }
620 
621  while (it != flow_obj->flow_entry_map_.end()) {
622  FlowEntry *fe = it->second;
623  const FlowExportInfo *info = NULL;
624  if (fe->fsc()) {
625  info = fe->fsc()->FindFlowExportInfo(fe);
626  }
627  SetSandeshFlowData(list, fe, info);
628  ++it;
629  count++;
630  if (count == kMaxFlowResponse) {
631  if (it != flow_obj->flow_entry_map_.end()) {
632  std::ostringstream ostr;
633  ostr << proto_ << ":" << port_ << ":"
634  << GetFlowKey(fe->key(), partition_id_);
635  resp_->set_flow_key(ostr.str());
636  flow_key_set = true;
637  } else {
638  std::ostringstream ostr;
639  FlowKey key;
640  ostr << proto_ << ":" << port_ << ":"
641  << GetFlowKey(key, ++partition_id_);
642  resp_->set_flow_key(ostr.str());
643  flow_key_set = true;
644  }
645  break;
646  }
647 
648  while (it == flow_obj->flow_entry_map_.end()) {
649  if (++partition_id_ < agent_->flow_thread_count()) {
650  flow_obj = agent_->pkt()->flow_table(partition_id_);
651  it = flow_obj->flow_entry_map_.begin();
652  if (it != flow_obj->flow_entry_map_.end()) {
653  break;
654  }
655  } else {
656  break;
657  }
658  }
659 
660  }
661 
662  if (!flow_key_set) {
663  std::ostringstream ostr;
664  ostr << proto_ << ":" << port_ << ":" <<PktSandeshFlow::start_key;
665  resp_->set_flow_key(ostr.str());
666  }
668  return true;
669 }
670 
671 bool PktSandeshFlowStats::SetProto(string &key) {
672  size_t n = std::count(key.begin(), key.end(), ':');
673  if (n != 2) {
674  return false;
675  }
676  std::stringstream ss(key);
677  string item;
678  if (getline(ss, item, ':')) {
679  std::istringstream(item) >> proto_;
680  }
681  if (getline(ss, item, ':')) {
682  std::istringstream(item) >> port_;
683  }
684  if (getline(ss, item)) {
685  SetFlowKey(item);
686  }
687  return true;
688 }
689 
690 PktSandeshFlowStats::PktSandeshFlowStats(Agent *agent, FlowStatsCollectorRecordsResp *obj,
691  std::string resp_ctx, std::string key):
692  PktSandeshFlow(agent, NULL, resp_ctx, key), resp_(obj) {
693  if (key != agent_->NullString()) {
694  if (SetProto(key)) {
695  key_valid_ = true;
696  }
697  }
698 }
699 
700 void ShowFlowStatsCollector::HandleRequest() const {
701  Agent *agent = Agent::GetInstance();
702  FlowStatsCollectorRecordsResp *resp = new FlowStatsCollectorRecordsResp();
703 
704  std::ostringstream ostr;
705  ostr << get_protocol() << ":" << get_port() << ":" <<
707  PktSandeshFlowStats *task = new PktSandeshFlowStats(agent, resp, context(),
708  ostr.str());
710  scheduler->Enqueue(task);
711 }
712 
713 void NextFlowStatsRecordsSet::HandleRequest() const {
714  Agent *agent = Agent::GetInstance();
715  FlowStatsCollectorRecordsResp *resp = new FlowStatsCollectorRecordsResp();
716 
717  PktSandeshFlow *task = new PktSandeshFlowStats(agent, resp, context(),
718  get_flow_key());
720  scheduler->Enqueue(task);
721 }
722 
723 
724 void SandeshFlowTableInfoRequest::HandleRequest() const {
725  Agent *agent = Agent::GetInstance();
726  FlowProto *proto = agent->pkt()->get_flow_proto();
727  SandeshFlowTableInfoResp *resp = new SandeshFlowTableInfoResp();
728  resp->set_flow_count(proto->FlowCount());
729  resp->set_total_added(agent->stats()->flow_created());
730  resp->set_max_flows(agent->stats()->max_flow_count());
731  resp->set_total_deleted(agent->stats()->flow_aged());
732  std::vector<SandeshFlowTableInfo> info_list;
733  for (uint16_t i = 0; i < proto->flow_table_count(); i++) {
734  FlowTable *table = proto->GetTable(i);
735  SandeshFlowTableInfo info;
736  info.set_index(table->table_index());
737  info.set_count(table->Size());
738  info.set_total_add(table->free_list()->total_alloc());
739  info.set_total_del(table->free_list()->total_free());
740  info.set_freelist_count(table->free_list()->free_count());
741  info_list.push_back(info);
742  }
743  resp->set_table_list(info_list);
744  resp->set_context(context());
745  resp->set_more(false);
746  resp->Response();
747 }
749 
751  uint16_t *id) {
752  using std::istringstream;
753 
754  Agent *agent = Agent::GetInstance();
755  const char ch = PktSandeshFlow::kDelimiter;
756  size_t n = std::count(key.begin(), key.end(), ch);
757  if (n != 3) {
758  return NULL;
759  }
760  std::stringstream ss(key);
761  string item, ip_str;
762  uint32_t vrf_id = 0;
763  uint16_t plen = 0, mgr_id = 0;
764 
765  if (getline(ss, item, ch)) {
766  istringstream(item) >> mgr_id;
767  }
768  if (mgr_id >= agent->pkt()->flow_mgmt_manager_list().size()) {
769  return NULL;
770  }
771  *id = mgr_id;
772  if (getline(ss, item, ch)) {
773  istringstream(item) >> vrf_id;
774  }
775  if (getline(ss, item, ch)) {
776  ip_str = item;
777  }
778  if (getline(ss, item, ch)) {
779  istringstream(item) >> plen;
780  }
781  boost::system::error_code ec;
782  IpAddress ip = IpAddress::from_string(ip_str.c_str(), ec);
783  if (ec) {
784  return NULL;
785  }
786  InetRouteFlowMgmtKey* ret = new InetRouteFlowMgmtKey(vrf_id, ip, plen);
787  return ret;
788 }
789 
790 void FlowsPerInetRouteFlowMgmtKeyReq::HandleRequest() const {
791  FlowsPerInetRouteFlowMgmtKeyResp *resp= new
792  FlowsPerInetRouteFlowMgmtKeyResp();
793  resp->set_context(context());
794  resp->set_more(false);
795  std::vector<SandeshFlowData> &resp_list =
796  const_cast<std::vector<SandeshFlowData>&>(resp->get_flow_list());
797  Agent *agent = Agent::GetInstance();
798  uint16_t mgr_id = 0;
799  FlowMgmtManager *mgr = NULL;
801  &mgr_id);
802  if (!ikey) {
803  resp->Response();
804  return;
805  }
806  mgr = agent->pkt()->flow_mgmt_manager(mgr_id);
808  FlowMgmtEntry *entry = tree->Find(ikey);
809  delete ikey;
810  if (!entry) {
811  resp->Response();
812  return;
813  }
814  if (entry->Size() == 0) {
815  resp->Response();
816  return;
817  }
818  const FlowMgmtEntry::FlowList &flow_list = entry->flow_list();
819  FlowMgmtEntry::FlowList::const_iterator it = flow_list.begin();
820  while (it != flow_list.end()) {
821  const FlowMgmtKeyNode *node = &(*it);
822  FlowEntry *fe = node->flow_entry();
823  SandeshFlowData data;
824  const FlowExportInfo *info = NULL;
825  SET_SANDESH_FLOW_DATA(agent, data, fe, info);
826  resp_list.push_back(data);
827  it++;
828  }
829  resp->Response();
830 }
831 
832 void Inet4FlowTreeReq::HandleRequest() const {
833  Agent *agent = Agent::GetInstance();
834  std::vector<FlowMgmtManager *>::const_iterator it =
836  Inet4FlowTreeResponse *resp = new Inet4FlowTreeResponse();
837  std::vector<SandeshInetRouteFlowMgmtEntryLink> &resp_list =
838  const_cast<std::vector<SandeshInetRouteFlowMgmtEntryLink>&>
839  (resp->get_keys());
840  uint16_t mgr_idx = 0;
841  while (it != agent->pkt()->flow_mgmt_manager_iterator_end()) {
842  FlowMgmtManager *mgr = *it;
843  it++;
845  FlowMgmtTree::Tree &list = tree->tree();
846  FlowMgmtTree::Tree::iterator tree_it = list.begin();
847  while(tree_it != list.end()) {
848  InetRouteFlowMgmtKey *key = static_cast<InetRouteFlowMgmtKey *>
849  (tree_it->first);
850  string key_str = InetRouteFlowMgmtKeyToString(mgr_idx, key);
851  SandeshInetRouteFlowMgmtEntryLink entry;
852  entry.set_inet_route_flow_mgmt_key(key_str);
853  resp_list.push_back(entry);
854  ++tree_it;
855  }
856  ++mgr_idx;
857  }
858  resp->set_context(context());
859  resp->set_more(false);
860  resp->Response();
861 }
862 
863 void SNatPortConfigRequest::HandleRequest() const {
864  Agent *agent = Agent::GetInstance();
865  PortTableManager *pm =
866  agent->pkt()->get_flow_proto()->port_table_manager();
867 
868  SNatPortResponse *resp = new SNatPortResponse();
869  std::vector<PortConfigData> &config_list =
870  const_cast<std::vector<PortConfigData>&>(resp->get_port_config_list());
871 
872  for(uint16_t protocol = 0; protocol < IPPROTO_MAX; protocol++) {
873  const PortTable *pt = pm->GetPortTable(protocol);
874  if (pt == NULL) {
875  continue;
876  }
877 
878  const PortConfig *pc = pt->port_config();
879  //Only count specified
880  PortConfigData spc;
881  spc.port_count = pc->port_count;
882  spc.protocol = protocol;
883 
884  std::vector<PortConfig::PortRange>::const_iterator it =
885  pc->port_range.begin();
886  for(;it != pc->port_range.end(); it++) {
887  PortConfigRange pcr;
888  pcr.port_start = it->port_start;
889  pcr.port_end = it->port_end;
890  spc.port_range.push_back(pcr);
891  }
892 
893  spc.set_bound_port_list((pt->GetPortList()));
894  config_list.push_back(spc);
895  }
896  resp->set_context(context());
897  resp->set_more(false);
898  resp->Response();
899 }
900 
901 static void HandlePortFlowReq(uint16_t protocol, uint16_t port,
902  bool match_proto, bool match_port,
903  std::string context) {
904  Agent *agent = Agent::GetInstance();
905  PortTableManager *pm =
906  agent->pkt()->get_flow_proto()->port_table_manager();
907 
908  SNatPortFlowResponse *resp = new SNatPortFlowResponse();
909  std::vector<SNatPortFlow> &list =
910  const_cast<std::vector<SNatPortFlow>&>(resp->get_port_flow_list());
911 
912  uint16_t flow_count = 0;
913  uint16_t next_port = 0;
914  uint16_t proto = 0;
915  for(; proto < IPPROTO_MAX; proto++) {
916  const PortTable *pt = pm->GetPortTable(proto);
917  next_port = 0;
918  if (match_proto && proto != protocol) {
919  continue;
920  }
921 
922  if (proto < protocol) {
923  continue;
924  }
925 
926  if (pt == NULL) {
927  continue;
928  }
929 
930  std::vector<uint16_t> ports = pt->GetPortList();
931  std::vector<uint16_t>::const_iterator it = ports.begin();
932  for(; it != ports.end(); it++) {
933  if (match_port && port != *it) {
934  continue;
935  }
936 
937  if (match_port && port > *it) {
938  break;
939  }
940 
941  if (port > *it) {
942  continue;
943  }
944 
945  std::vector<FlowKey> flow_list;
946  pt->GetFlowKeyList(*it, flow_list);
947  flow_count += flow_list.size();
948 
949  SNatPortFlow snf;
950  snf.port = *it;
951  snf.protocol = proto;
952  std::vector<FlowKey>::iterator flow_it = flow_list.begin();
953  for (; flow_it != flow_list.end(); flow_it++) {
954  SNatFlowKey flow_key;
955  flow_key.nh = flow_it->nh;
956  flow_key.sip = flow_it->src_addr.to_string();
957  flow_key.dip = flow_it->dst_addr.to_string();
958  flow_key.src_port = flow_it->src_port;
959  flow_key.dst_port = flow_it->dst_port;
960  flow_key.protocol = flow_it->protocol;
961  snf.flows.push_back(flow_key);
962  }
963 
964  list.push_back(snf);
965 
966  if (flow_count >= 1) {
967  break;
968  }
969  }
970 
971  if (it != ports.end() && match_port == false) {
972  next_port = *it + 1;
973  }
974 
975  if (match_port == false) {
976  port = 0;
977  }
978 
979  if (flow_count >= 1) {
980  break;
981  }
982  }
983 
984  bool next = true;
985  if (match_proto && next_port == 0) {
986  next = false;
987  }
988 
989  if (proto == IPPROTO_MAX) {
990  next = false;
991  }
992 
993  if (next) {
994  std::stringstream str;
995  str << proto << ":" << next_port << ":" << match_proto
996  << ":" << match_port;
997  resp->set_flow_key(str.str());
998  }
999 
1000  resp->set_context(context);
1001  resp->set_more(false);
1002  resp->Response();
1003 }
1004 
1005 void SNatPerPortFlowList::HandleRequest() const {
1006  bool match_proto = false;
1007  if (protocol != 0) {
1008  match_proto = true;
1009  }
1010 
1011  bool match_port = false;
1012  if (port != 0) {
1013  match_port = true;
1014  }
1015 
1016  HandlePortFlowReq(protocol, port, match_proto, match_port,
1017  context());
1018 }
1019 
1020 void NextPerPortFlowList::HandleRequest() const {
1021  using std::istringstream;
1022 
1023  uint16_t proto = 0;
1024  uint16_t port = 0;
1025  bool match_proto = false;
1026  bool match_port = false;
1027  std::string colon;
1028 
1029  std::stringstream ss(port_key);
1030  string item;
1031  const char ch = ':';
1032 
1033  size_t n = std::count(port_key.begin(), port_key.end(), ch);
1034  if (n != 3) {
1035  goto fail;
1036  }
1037 
1038  if (getline(ss, item, ch)) {
1039  istringstream(item) >> proto;
1040  }
1041  if (getline(ss, item, ch)) {
1042  istringstream(item) >> port;
1043  }
1044  if (getline(ss, item, ch)) {
1045  istringstream(item) >> match_proto;
1046  }
1047  if (getline(ss, item, ch)) {
1048  istringstream(item) >> match_port;
1049  }
1050 
1051  HandlePortFlowReq(proto, port, match_proto, match_port,
1052  context());
1053 
1054 fail:
1055  FlowErrorResp *resp = new FlowErrorResp();
1056  resp->set_context(context());
1057  resp->set_more(false);
1058  resp->Response();
1059 }
boost::asio::ip::address IpAddress
Definition: address.h:14
@ INET
Definition: address.h:27
@ INET6
Definition: address.h:28
uint64_t flow_created() const
Definition: agent_stats.h:101
uint64_t flow_aged() const
Definition: agent_stats.h:108
uint64_t max_flow_count() const
Definition: agent_stats.h:103
Definition: agent.h:361
FlowStatsManager * flow_stats_manager() const
Definition: agent.cc:929
uint16_t flow_thread_count() const
Definition: agent.h:1205
static const std::string & NullString()
Definition: agent.h:440
static Agent * GetInstance()
Definition: agent.h:439
AgentStats * stats() const
Definition: agent.cc:885
PktModule * pkt() const
Definition: agent.cc:969
uint32_t total_free() const
Definition: flow_table.h:124
uint32_t free_count() const
Definition: flow_table.h:121
uint32_t total_alloc() const
Definition: flow_table.h:123
const FlowKey & key() const
Definition: flow_entry.h:595
const std::string & nw_ace_uuid() const
Definition: flow_entry.h:637
const std::string & sg_rule_uuid() const
Definition: flow_entry.h:634
const MatchPolicy & match_p() const
Definition: flow_entry.h:671
FlowStatsCollector * fsc() const
Definition: flow_entry.h:729
uint32_t Size() const
boost::intrusive::list< FlowMgmtKeyNode, Node > FlowList
const FlowList & flow_list() const
FlowEntry * flow_entry() const
InetRouteFlowMgmtTree * ip4_route_flow_mgmt_tree()
Definition: flow_mgmt.h:326
Tree & tree()
FlowMgmtEntry * Find(FlowMgmtKey *key)
std::map< FlowMgmtKey *, FlowMgmtEntry *, FlowMgmtKeyCmp > Tree
FlowTable * GetTable(uint16_t index) const
Definition: flow_proto.cc:262
uint32_t flow_table_count() const
Definition: flow_proto.h:71
uint32_t FlowCount() const
Definition: flow_proto.cc:266
PortTableManager * port_table_manager()
Definition: flow_proto.h:129
void UpdateAgeTimeInSeconds(uint32_t age_time)
bool FindFlowExportInfo(const FlowEntry *fe, FlowEntryTree::iterator &it)
const FlowStatsCollectorObject * Find(uint32_t proto, uint32_t port) const
FlowStatsCollectorObject * default_flow_stats_collector_obj()
const LinkLocalFlowInfoMap & linklocal_flow_info_map()
Definition: flow_table.h:218
size_t Size()
Definition: flow_table.h:210
void DeleteAll()
Definition: flow_table.cc:415
uint16_t table_index() const
Definition: flow_table.h:209
FlowEntryFreeList * free_list()
Definition: flow_table.h:250
std::map< int, LinkLocalFlowInfo > LinkLocalFlowInfoMap
Definition: flow_table.h:181
FlowEntryMap flow_entry_map_
Definition: flow_table.h:306
IpAddress ip() const
uint8_t plen() const
FlowProto * get_flow_proto() const
Definition: pkt_init.h:44
FlowMgmtManagerList flow_mgmt_manager_list() const
Definition: pkt_init.h:37
std::vector< FlowMgmtManager * >::const_iterator flow_mgmt_manager_iterator_begin() const
Definition: pkt_init.h:50
std::vector< FlowMgmtManager * >::const_iterator flow_mgmt_manager_iterator_end() const
Definition: pkt_init.h:54
FlowTable * flow_table(uint16_t index) const
Definition: pkt_init.cc:98
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
bool SetProto(std::string &key)
FlowStatsCollectorRecordsResp * resp_
PktSandeshFlowStats(Agent *agent, FlowStatsCollectorRecordsResp *obj, std::string resp_ctx, std::string key)
static const std::string start_key
FlowKey flow_iteration_key_
static const int kMaxFlowResponse
virtual ~PktSandeshFlow()
void SetSandeshFlowData(std::vector< SandeshFlowData > &list, FlowEntry *fe, const FlowExportInfo *info)
std::string resp_data_
static std::string GetFlowKey(const FlowKey &key, uint16_t partition_id)
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
bool SetFlowKey(std::string key)
PktSandeshFlow(Agent *agent, FlowRecordsResp *obj, std::string resp_ctx, std::string key)
void SendResponse(SandeshResponse *resp)
static const char kDelimiter
FlowRecordsResp * resp_obj_
uint16_t partition_id_
const PortTable * GetPortTable(uint8_t proto)
Definition: flow_entry.h:1116
void GetFlowKeyList(uint16_t port, std::vector< FlowKey > &key) const
Definition: flow_entry.cc:4163
std::vector< uint16_t > GetPortList() const
Definition: flow_entry.h:1055
const PortConfig * port_config() const
Definition: flow_entry.h:1051
uint32_t vrf_id() const
virtual void Response()
Definition: cpp/sandesh.h:505
virtual void set_more(const bool val)=0
void set_context(std::string context)
Definition: cpp/sandesh.h:313
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
Definition: task.cc:662
static TaskScheduler * GetInstance()
Definition: task.cc:574
Task is a class to describe a computational task within OpenSDN control plane applications....
Definition: task.h:82
void SetActionStr(const FlowAction &action_info, std::vector< ActionStr > &action_str_l)
Definition: flow_entry.cc:3069
std::list< MatchAclParams > MatchAclParamsList
Definition: flow_entry.h:221
static void SetAclInfo(SandeshFlowData &data, FlowEntry *fe)
static void HandlePortFlowReq(uint16_t protocol, uint16_t port, bool match_proto, bool match_port, std::string context)
static InetRouteFlowMgmtKey * StringToInetRouteFlowMgmtKey(const string &key, uint16_t *id)
#define SET_SANDESH_FLOW_DATA(agent, data, fe, info)
static string InetRouteFlowMgmtKeyToString(uint16_t id, InetRouteFlowMgmtKey *key)
static void SetOneAclInfo(FlowAclInfo *policy, uint32_t action, const MatchAclParamsList &acl_list)
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:20
Definition: acl.h:35
uint32_t action
Definition: acl.h:44
uint8_t protocol
Definition: flow_entry.h:216
IpAddress src_addr
Definition: flow_entry.h:214
IpAddress dst_addr
Definition: flow_entry.h:215
uint16_t src_port
Definition: flow_entry.h:217
Address::Family family
Definition: flow_entry.h:212
uint32_t nh
Definition: flow_entry.h:213
uint16_t dst_port
Definition: flow_entry.h:218
uint32_t out_mirror_action
Definition: flow_entry.h:273
SessionPolicy sg_policy
Definition: flow_entry.h:265
SessionPolicy aps_policy
Definition: flow_entry.h:266
MatchAclParamsList m_mirror_acl_l
Definition: flow_entry.h:269
MatchAclParamsList m_vrf_assign_acl_l
Definition: flow_entry.h:275
SessionPolicy fwaas_policy
Definition: flow_entry.h:267
uint32_t policy_action
Definition: flow_entry.h:260
uint32_t mirror_action
Definition: flow_entry.h:270
MatchAclParamsList m_out_acl_l
Definition: flow_entry.h:262
MatchAclParamsList m_out_mirror_acl_l
Definition: flow_entry.h:272
MatchAclParamsList m_acl_l
Definition: flow_entry.h:259
uint32_t vrf_assign_acl_action
Definition: flow_entry.h:276
uint32_t out_policy_action
Definition: flow_entry.h:263
std::vector< PortRange > port_range
uint16_t port_count
uint32_t reverse_out_action
Definition: flow_entry.h:243
uint32_t action
Definition: flow_entry.h:231
uint32_t action_summary
Definition: flow_entry.h:247
MatchAclParamsList m_reverse_out_acl_l
Definition: flow_entry.h:241
uint32_t reverse_action
Definition: flow_entry.h:239
MatchAclParamsList m_reverse_acl_l
Definition: flow_entry.h:237
MatchAclParamsList m_acl_l
Definition: flow_entry.h:229
MatchAclParamsList m_out_acl_l
Definition: flow_entry.h:233
uint32_t out_action
Definition: flow_entry.h:235
Definition: task_int.h:11
static boost::posix_time::ptime UTCUsecToPTime(uint64_t tusec)
Definition: time_util.h:39