Line data Source code
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>
10 : #include <vrouter/ksync/flowtable_ksync.h>
11 : #include <vrouter/ksync/ksync_init.h>
12 : #include <vrouter/ksync/ksync_flow_index_manager.h>
13 : #include "vrouter/flow_stats/flow_stats_collector.h"
14 : #include "flow_proto.h"
15 : #include "flow_mgmt.h"
16 : #include "flow_event.h"
17 :
18 : //////////////////////////////////////////////////////////////////////////////
19 : // FlowEventQueue routines
20 : //////////////////////////////////////////////////////////////////////////////
21 5 : FlowEventQueueBase::FlowEventQueueBase(FlowProto *proto,
22 : const std::string &name,
23 : uint32_t task_id, int task_instance,
24 : FlowTokenPool *pool,
25 : uint16_t latency_limit,
26 5 : uint32_t max_iterations) :
27 5 : flow_proto_(proto), token_pool_(pool), task_start_(0), count_(0),
28 5 : events_processed_(0), latency_limit_(latency_limit) {
29 10 : queue_ = new Queue(task_id, task_instance,
30 : boost::bind(&FlowEventQueueBase::Handler, this, _1),
31 5 : Queue::kMaxSize, max_iterations);
32 : char buff[100];
33 5 : sprintf(buff, "%s-%d", name.c_str(), task_instance);
34 5 : queue_->set_name(buff);
35 5 : if (token_pool_)
36 4 : queue_->SetStartRunnerFunc(boost::bind(&FlowEventQueueBase::TokenCheck,
37 : this));
38 5 : queue_->set_measure_busy_time(proto->agent()->MeasureQueueDelay());
39 5 : if (latency_limit_) {
40 0 : queue_->SetEntryCallback(boost::bind(&FlowEventQueueBase::TaskEntry,
41 : this));
42 0 : queue_->SetExitCallback(boost::bind(&FlowEventQueueBase::TaskExit,
43 : this, _1));
44 : }
45 5 : }
46 :
47 5 : FlowEventQueueBase::~FlowEventQueueBase() {
48 5 : delete queue_;
49 5 : }
50 :
51 5 : void FlowEventQueueBase::Shutdown() {
52 5 : queue_->Shutdown();
53 5 : }
54 :
55 718 : void FlowEventQueueBase::Enqueue(FlowEvent *event) {
56 718 : if (CanEnqueue(event) == false) {
57 50 : delete event;
58 50 : return;
59 : }
60 668 : queue_->Enqueue(event);
61 : }
62 :
63 346 : bool FlowEventQueueBase::TokenCheck() {
64 346 : return flow_proto_->TokenCheck(token_pool_);
65 : }
66 :
67 0 : bool FlowEventQueueBase::TaskEntry() {
68 0 : count_ = 0;
69 0 : task_start_ = ClockMonotonicUsec();
70 0 : getrusage(RUSAGE_THREAD, &rusage_);
71 0 : return true;
72 : }
73 :
74 0 : void FlowEventQueueBase::TaskExit(bool done) {
75 0 : if (task_start_ == 0)
76 0 : return;
77 :
78 0 : uint64_t t = ClockMonotonicUsec();
79 0 : if (((t - task_start_) / 1000) >= latency_limit_) {
80 : struct rusage r;
81 0 : getrusage(RUSAGE_THREAD, &r);
82 :
83 0 : uint32_t user = (r.ru_utime.tv_sec - rusage_.ru_utime.tv_sec) * 1000;
84 0 : user += ((r.ru_utime.tv_usec - rusage_.ru_utime.tv_usec) / 1000);
85 :
86 0 : uint32_t sys = (r.ru_stime.tv_sec - rusage_.ru_stime.tv_sec) * 1000;
87 0 : sys += ((r.ru_stime.tv_usec - rusage_.ru_stime.tv_usec) / 1000);
88 :
89 0 : LOG(ERROR, queue_->Description()
90 : << " Time exceeded " << ((t - task_start_) / 1000)
91 : << " Count " << count_
92 : << " User " << user << " Sys " << sys);
93 : }
94 0 : return;
95 : }
96 :
97 668 : bool FlowEventQueueBase::Handler(FlowEvent *event) {
98 668 : std::unique_ptr<FlowEvent> event_ptr(event);
99 667 : count_++;
100 667 : if (CanProcess(event) == false) {
101 28 : ProcessDone(event, false);
102 28 : return true;
103 : }
104 :
105 638 : HandleEvent(event);
106 :
107 640 : ProcessDone(event, true);
108 640 : return true;
109 668 : }
110 :
111 718 : bool FlowEventQueueBase::CanEnqueue(FlowEvent *event) {
112 718 : FlowEntry *flow = event->flow();
113 718 : bool ret = true;
114 718 : switch (event->event()) {
115 :
116 63 : case FlowEvent::DELETE_DBENTRY:
117 : case FlowEvent::DELETE_FLOW: {
118 63 : std::scoped_lock mutext(flow->mutex());
119 63 : ret = flow->GetPendingAction()->SetDelete();
120 63 : break;
121 63 : }
122 :
123 : // lock already token for the flow
124 28 : case FlowEvent::FLOW_MESSAGE: {
125 28 : ret = flow->GetPendingAction()->SetRecompute();
126 28 : break;
127 : }
128 :
129 75 : case FlowEvent::RECOMPUTE_FLOW: {
130 75 : std::scoped_lock mutext(flow->mutex());
131 75 : ret = flow->GetPendingAction()->SetRecomputeDBEntry();
132 75 : break;
133 75 : }
134 :
135 15 : case FlowEvent::REVALUATE_DBENTRY: {
136 15 : std::scoped_lock mutext(flow->mutex());
137 15 : ret = flow->GetPendingAction()->SetRevaluate();
138 15 : break;
139 15 : }
140 :
141 537 : default:
142 537 : break;
143 : }
144 :
145 718 : return ret;
146 : }
147 :
148 667 : bool FlowEventQueueBase::CanProcess(FlowEvent *event) {
149 667 : FlowEntry *flow = event->flow();
150 667 : bool ret = true;
151 667 : switch (event->event()) {
152 :
153 40 : case FlowEvent::DELETE_DBENTRY:
154 : case FlowEvent::DELETE_FLOW: {
155 40 : std::scoped_lock mutext(flow->mutex());
156 40 : events_processed_++;
157 40 : ret = flow->GetPendingAction()->CanDelete();
158 40 : break;
159 40 : }
160 :
161 26 : case FlowEvent::FLOW_MESSAGE: {
162 26 : std::scoped_lock mutext(flow->mutex());
163 26 : events_processed_++;
164 26 : ret = flow->GetPendingAction()->CanRecompute();
165 26 : break;
166 26 : }
167 :
168 62 : case FlowEvent::RECOMPUTE_FLOW: {
169 62 : std::scoped_lock mutext(flow->mutex());
170 62 : events_processed_++;
171 62 : ret = flow->GetPendingAction()->CanRecomputeDBEntry();
172 62 : break;
173 62 : }
174 :
175 3 : case FlowEvent::REVALUATE_DBENTRY: {
176 3 : events_processed_++;
177 3 : std::scoped_lock mutext(flow->mutex());
178 3 : ret = flow->GetPendingAction()->CanRevaluate();
179 3 : break;
180 3 : }
181 :
182 535 : default:
183 535 : break;
184 : }
185 :
186 666 : return ret;
187 : }
188 :
189 668 : void FlowEventQueueBase::ProcessDone(FlowEvent *event, bool update_rev_flow) {
190 668 : FlowEntry *flow = event->flow();
191 668 : FlowEntry *rflow = NULL;
192 668 : if (flow && update_rev_flow)
193 152 : rflow = flow->reverse_flow_entry();
194 :
195 668 : switch (event->event()) {
196 :
197 40 : case FlowEvent::DELETE_DBENTRY:
198 : case FlowEvent::DELETE_FLOW: {
199 80 : FLOW_LOCK(flow, rflow, event->event());
200 40 : flow->GetPendingAction()->ResetDelete();
201 40 : if (rflow)
202 0 : rflow->GetPendingAction()->ResetDelete();
203 40 : break;
204 40 : }
205 :
206 26 : case FlowEvent::FLOW_MESSAGE: {
207 26 : FLOW_LOCK(flow, rflow, event->event());
208 26 : flow->GetPendingAction()->ResetRecompute();
209 26 : if (rflow)
210 26 : rflow->GetPendingAction()->ResetRecompute();
211 26 : break;
212 26 : }
213 :
214 62 : case FlowEvent::RECOMPUTE_FLOW: {
215 62 : std::scoped_lock mutext(flow->mutex());
216 62 : flow->GetPendingAction()->ResetRecomputeDBEntry();
217 62 : break;
218 62 : }
219 :
220 3 : case FlowEvent::REVALUATE_DBENTRY: {
221 3 : FLOW_LOCK(flow, rflow, event->event());
222 3 : flow->GetPendingAction()->ResetRevaluate();
223 3 : if (rflow)
224 3 : rflow->GetPendingAction()->ResetRevaluate();
225 3 : break;
226 3 : }
227 :
228 537 : default:
229 537 : break;
230 : }
231 :
232 668 : return;
233 : }
234 :
235 2 : FlowEventQueue::FlowEventQueue(Agent *agent, FlowProto *proto,
236 : FlowTable *table, FlowTokenPool *pool,
237 : uint16_t latency_limit,
238 2 : uint32_t max_iterations) :
239 : FlowEventQueueBase(proto, "Flow Event Queue",
240 4 : agent->task_scheduler()->GetTaskId(kTaskFlowEvent),
241 2 : table->table_index(), pool, latency_limit,
242 : max_iterations),
243 4 : flow_table_(table) {
244 2 : }
245 :
246 4 : FlowEventQueue::~FlowEventQueue() {
247 4 : }
248 :
249 408 : bool FlowEventQueue::HandleEvent(FlowEvent *event) {
250 408 : return flow_proto_->FlowEventHandler(event, flow_table_);
251 : }
252 :
253 1 : DeleteFlowEventQueue::DeleteFlowEventQueue(Agent *agent, FlowProto *proto,
254 : FlowTable *table,
255 : FlowTokenPool *pool,
256 : uint16_t latency_limit,
257 1 : uint32_t max_iterations) :
258 : FlowEventQueueBase(proto, "Flow Delete Queue",
259 2 : agent->task_scheduler()->GetTaskId(kTaskFlowDelete),
260 1 : table->table_index(), pool, latency_limit,
261 : max_iterations),
262 2 : flow_table_(table) {
263 1 : }
264 :
265 2 : DeleteFlowEventQueue::~DeleteFlowEventQueue() {
266 2 : }
267 :
268 0 : bool DeleteFlowEventQueue::HandleEvent(FlowEvent *event) {
269 0 : return flow_proto_->FlowDeleteHandler(event, flow_table_);
270 : }
271 :
272 1 : KSyncFlowEventQueue::KSyncFlowEventQueue(Agent *agent, FlowProto *proto,
273 : FlowTable *table,
274 : FlowTokenPool *pool,
275 : uint16_t latency_limit,
276 1 : uint32_t max_iterations) :
277 : FlowEventQueueBase(proto, "Flow KSync Queue",
278 2 : agent->task_scheduler()->GetTaskId(kTaskFlowKSync),
279 1 : table->table_index(), pool, latency_limit,
280 : max_iterations),
281 2 : flow_table_(table) {
282 1 : }
283 :
284 2 : KSyncFlowEventQueue::~KSyncFlowEventQueue() {
285 2 : }
286 :
287 155 : bool KSyncFlowEventQueue::HandleEvent(FlowEvent *event) {
288 155 : return flow_proto_->FlowKSyncMsgHandler(event, flow_table_);
289 : }
290 :
291 1 : UpdateFlowEventQueue::UpdateFlowEventQueue(Agent *agent, FlowProto *proto,
292 : FlowTokenPool *pool,
293 : uint16_t latency_limit,
294 1 : uint32_t max_iterations) :
295 : FlowEventQueueBase(proto, "Flow Update Queue",
296 2 : agent->task_scheduler()->GetTaskId(kTaskFlowUpdate), 0,
297 2 : pool, latency_limit, max_iterations) {
298 1 : }
299 :
300 1 : UpdateFlowEventQueue::~UpdateFlowEventQueue() {
301 1 : }
302 :
303 77 : bool UpdateFlowEventQueue::HandleEvent(FlowEvent *event) {
304 77 : return flow_proto_->FlowUpdateHandler(event);
305 : }
|