OpenSDN source code
flow_event.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 #include <base/address_util.h>
6 #include <boost/functional/hash.hpp>
7 #include <init/agent_param.h>
8 #include <cmn/agent_stats.h>
9 #include <oper/agent_profile.h>
14 #include "flow_proto.h"
15 #include "flow_mgmt.h"
16 #include "flow_event.h"
17 
19 // FlowEventQueue routines
22  const std::string &name,
23  uint32_t task_id, int task_instance,
24  FlowTokenPool *pool,
25  uint16_t latency_limit,
26  uint32_t max_iterations) :
27  flow_proto_(proto), token_pool_(pool), task_start_(0), count_(0),
28  events_processed_(0), latency_limit_(latency_limit) {
29  queue_ = new Queue(task_id, task_instance,
30  boost::bind(&FlowEventQueueBase::Handler, this, _1),
31  Queue::kMaxSize, max_iterations);
32  char buff[100];
33  sprintf(buff, "%s-%d", name.c_str(), task_instance);
34  queue_->set_name(buff);
35  if (token_pool_)
37  this));
39  if (latency_limit_) {
41  this));
43  this, _1));
44  }
45 }
46 
48  delete queue_;
49 }
50 
52  queue_->Shutdown();
53 }
54 
56  if (CanEnqueue(event) == false) {
57  delete event;
58  return;
59  }
60  queue_->Enqueue(event);
61 }
62 
65 }
66 
68  count_ = 0;
70  getrusage(RUSAGE_THREAD, &rusage_);
71  return true;
72 }
73 
75  if (task_start_ == 0)
76  return;
77 
78  uint64_t t = ClockMonotonicUsec();
79  if (((t - task_start_) / 1000) >= latency_limit_) {
80  struct rusage r;
81  getrusage(RUSAGE_THREAD, &r);
82 
83  uint32_t user = (r.ru_utime.tv_sec - rusage_.ru_utime.tv_sec) * 1000;
84  user += ((r.ru_utime.tv_usec - rusage_.ru_utime.tv_usec) / 1000);
85 
86  uint32_t sys = (r.ru_stime.tv_sec - rusage_.ru_stime.tv_sec) * 1000;
87  sys += ((r.ru_stime.tv_usec - rusage_.ru_stime.tv_usec) / 1000);
88 
89  LOG(ERROR, queue_->Description()
90  << " Time exceeded " << ((t - task_start_) / 1000)
91  << " Count " << count_
92  << " User " << user << " Sys " << sys);
93  }
94  return;
95 }
96 
98  std::unique_ptr<FlowEvent> event_ptr(event);
99  count_++;
100  if (CanProcess(event) == false) {
101  ProcessDone(event, false);
102  return true;
103  }
104 
105  HandleEvent(event);
106 
107  ProcessDone(event, true);
108  return true;
109 }
110 
112  FlowEntry *flow = event->flow();
113  bool ret = true;
114  switch (event->event()) {
115 
117  case FlowEvent::DELETE_FLOW: {
118  std::scoped_lock mutext(flow->mutex());
119  ret = flow->GetPendingAction()->SetDelete();
120  break;
121  }
122 
123  // lock already token for the flow
125  ret = flow->GetPendingAction()->SetRecompute();
126  break;
127  }
128 
130  std::scoped_lock mutext(flow->mutex());
131  ret = flow->GetPendingAction()->SetRecomputeDBEntry();
132  break;
133  }
134 
136  std::scoped_lock mutext(flow->mutex());
137  ret = flow->GetPendingAction()->SetRevaluate();
138  break;
139  }
140 
141  default:
142  break;
143  }
144 
145  return ret;
146 }
147 
149  FlowEntry *flow = event->flow();
150  bool ret = true;
151  switch (event->event()) {
152 
154  case FlowEvent::DELETE_FLOW: {
155  std::scoped_lock mutext(flow->mutex());
157  ret = flow->GetPendingAction()->CanDelete();
158  break;
159  }
160 
162  std::scoped_lock mutext(flow->mutex());
164  ret = flow->GetPendingAction()->CanRecompute();
165  break;
166  }
167 
169  std::scoped_lock mutext(flow->mutex());
171  ret = flow->GetPendingAction()->CanRecomputeDBEntry();
172  break;
173  }
174 
177  std::scoped_lock mutext(flow->mutex());
178  ret = flow->GetPendingAction()->CanRevaluate();
179  break;
180  }
181 
182  default:
183  break;
184  }
185 
186  return ret;
187 }
188 
189 void FlowEventQueueBase::ProcessDone(FlowEvent *event, bool update_rev_flow) {
190  FlowEntry *flow = event->flow();
191  FlowEntry *rflow = NULL;
192  if (flow && update_rev_flow)
193  rflow = flow->reverse_flow_entry();
194 
195  switch (event->event()) {
196 
198  case FlowEvent::DELETE_FLOW: {
199  FLOW_LOCK(flow, rflow, event->event());
200  flow->GetPendingAction()->ResetDelete();
201  if (rflow)
202  rflow->GetPendingAction()->ResetDelete();
203  break;
204  }
205 
207  FLOW_LOCK(flow, rflow, event->event());
208  flow->GetPendingAction()->ResetRecompute();
209  if (rflow)
210  rflow->GetPendingAction()->ResetRecompute();
211  break;
212  }
213 
215  std::scoped_lock mutext(flow->mutex());
217  break;
218  }
219 
221  FLOW_LOCK(flow, rflow, event->event());
222  flow->GetPendingAction()->ResetRevaluate();
223  if (rflow)
224  rflow->GetPendingAction()->ResetRevaluate();
225  break;
226  }
227 
228  default:
229  break;
230  }
231 
232  return;
233 }
234 
236  FlowTable *table, FlowTokenPool *pool,
237  uint16_t latency_limit,
238  uint32_t max_iterations) :
239  FlowEventQueueBase(proto, "Flow Event Queue",
240  agent->task_scheduler()->GetTaskId(kTaskFlowEvent),
241  table->table_index(), pool, latency_limit,
242  max_iterations),
243  flow_table_(table) {
244 }
245 
247 }
248 
250  return flow_proto_->FlowEventHandler(event, flow_table_);
251 }
252 
254  FlowTable *table,
255  FlowTokenPool *pool,
256  uint16_t latency_limit,
257  uint32_t max_iterations) :
258  FlowEventQueueBase(proto, "Flow Delete Queue",
259  agent->task_scheduler()->GetTaskId(kTaskFlowDelete),
260  table->table_index(), pool, latency_limit,
261  max_iterations),
262  flow_table_(table) {
263 }
264 
266 }
267 
269  return flow_proto_->FlowDeleteHandler(event, flow_table_);
270 }
271 
273  FlowTable *table,
274  FlowTokenPool *pool,
275  uint16_t latency_limit,
276  uint32_t max_iterations) :
277  FlowEventQueueBase(proto, "Flow KSync Queue",
278  agent->task_scheduler()->GetTaskId(kTaskFlowKSync),
279  table->table_index(), pool, latency_limit,
280  max_iterations),
281  flow_table_(table) {
282 }
283 
285 }
286 
289 }
290 
292  FlowTokenPool *pool,
293  uint16_t latency_limit,
294  uint32_t max_iterations) :
295  FlowEventQueueBase(proto, "Flow Update Queue",
296  agent->task_scheduler()->GetTaskId(kTaskFlowUpdate), 0,
297  pool, latency_limit, max_iterations) {
298 }
299 
301 }
302 
304  return flow_proto_->FlowUpdateHandler(event);
305 }
#define kTaskFlowDelete
Definition: agent.h:327
#define kTaskFlowEvent
Definition: agent.h:324
#define kTaskFlowKSync
Definition: agent.h:325
#define kTaskFlowUpdate
Definition: agent.h:326
Definition: agent.h:361
bool MeasureQueueDelay()
Definition: agent.cc:1149
bool HandleEvent(FlowEvent *event)
Definition: flow_event.cc:268
DeleteFlowEventQueue(Agent *agent, FlowProto *proto, FlowTable *table, FlowTokenPool *pool, uint16_t latency_limit, uint32_t max_iterations)
Definition: flow_event.cc:253
virtual ~DeleteFlowEventQueue()
Definition: flow_event.cc:265
FlowTable * flow_table_
Definition: flow_event.h:331
FlowEntry * reverse_flow_entry()
Definition: flow_entry.h:603
std::mutex & mutex()
Definition: flow_entry.h:651
FlowPendingAction * GetPendingAction()
Definition: flow_entry.h:752
FlowEventQueueBase(FlowProto *proto, const std::string &name, uint32_t task_id, int task_instance, FlowTokenPool *pool, uint16_t latency_limit, uint32_t max_iterations)
Definition: flow_event.cc:21
void ProcessDone(FlowEvent *event, bool update_rev_flow)
Definition: flow_event.cc:189
bool CanProcess(FlowEvent *event)
Definition: flow_event.cc:148
uint16_t latency_limit_
Definition: flow_event.h:306
uint64_t task_start_
Definition: flow_event.h:300
virtual bool HandleEvent(FlowEvent *event)=0
FlowTokenPool * token_pool_
Definition: flow_event.h:299
void Enqueue(FlowEvent *event)
Definition: flow_event.cc:55
uint64_t events_processed_
Definition: flow_event.h:305
bool CanEnqueue(FlowEvent *event)
Definition: flow_event.cc:111
virtual bool Handler(FlowEvent *event)
Definition: flow_event.cc:97
struct rusage rusage_
Definition: flow_event.h:307
WorkQueue< FlowEvent * > Queue
Definition: flow_event.h:270
void TaskExit(bool done)
Definition: flow_event.cc:74
FlowProto * flow_proto_
Definition: flow_event.h:298
virtual ~FlowEventQueueBase()
Definition: flow_event.cc:47
virtual ~FlowEventQueue()
Definition: flow_event.cc:246
FlowEventQueue(Agent *agent, FlowProto *proto, FlowTable *table, FlowTokenPool *pool, uint16_t latency_limit, uint32_t max_iterations)
Definition: flow_event.cc:235
FlowTable * flow_table_
Definition: flow_event.h:319
bool HandleEvent(FlowEvent *event)
Definition: flow_event.cc:249
@ FLOW_MESSAGE
Definition: flow_event.h:24
@ DELETE_FLOW
Definition: flow_event.h:26
@ RECOMPUTE_FLOW
Definition: flow_event.h:45
@ DELETE_DBENTRY
Definition: flow_event.h:37
@ REVALUATE_DBENTRY
Definition: flow_event.h:41
Event event() const
Definition: flow_event.h:147
bool SetRecomputeDBEntry()
Definition: flow_entry.cc:3027
bool CanRecomputeDBEntry()
Definition: flow_entry.cc:3040
void ResetRecomputeDBEntry()
Definition: flow_entry.cc:3035
bool FlowDeleteHandler(FlowEvent *req, FlowTable *table)
Definition: flow_proto.cc:572
bool FlowUpdateHandler(FlowEvent *req)
Definition: flow_proto.cc:546
bool FlowEventHandler(FlowEvent *req, FlowTable *table)
Definition: flow_proto.cc:402
bool TokenCheck(const FlowTokenPool *pool) const
Definition: flow_proto.cc:750
bool FlowKSyncMsgHandler(FlowEvent *req, FlowTable *table)
Definition: flow_proto.cc:511
FlowTable * flow_table_
Definition: flow_event.h:343
virtual ~KSyncFlowEventQueue()
Definition: flow_event.cc:284
bool HandleEvent(FlowEvent *event)
Definition: flow_event.cc:287
KSyncFlowEventQueue(Agent *agent, FlowProto *proto, FlowTable *table, FlowTokenPool *pool, uint16_t latency_limit, uint32_t max_iterations)
Definition: flow_event.cc:272
bool HandleEvent(FlowEvent *event)
Definition: flow_event.cc:303
virtual ~UpdateFlowEventQueue()
Definition: flow_event.cc:300
UpdateFlowEventQueue(Agent *agent, FlowProto *proto, FlowTokenPool *pool, uint16_t latency_limit, uint32_t max_iterations)
Definition: flow_event.cc:291
void SetStartRunnerFunc(StartRunnerFunc start_runner_fn)
Definition: queue_task.h:193
bool Enqueue(QueueEntryT entry)
Definition: queue_task.h:249
void Shutdown(bool delete_entries=true)
Definition: queue_task.h:153
static const int kMaxSize
Definition: queue_task.h:112
void set_measure_busy_time(bool val) const
Definition: queue_task.h:380
std::string Description() const
Definition: queue_task.h:311
void SetExitCallback(TaskExitCallback on_exit)
Definition: queue_task.h:304
void set_name(const std::string &name)
Definition: queue_task.h:308
void SetEntryCallback(TaskEntryCallback on_entry)
Definition: queue_task.h:300
#define FLOW_LOCK(flow, rflow, flow_event)
Definition: flow_table.h:62
#define LOG(_Level, _Msg)
Definition: logging.h:35
static uint64_t ClockMonotonicUsec()
Definition: time_util.h:30