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 0 : 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 0 : uint32_t max_iterations) :
27 0 : flow_proto_(proto), token_pool_(pool), task_start_(0), count_(0),
28 0 : events_processed_(0), latency_limit_(latency_limit) {
29 0 : queue_ = new Queue(task_id, task_instance,
30 : boost::bind(&FlowEventQueueBase::Handler, this, _1),
31 0 : Queue::kMaxSize, max_iterations);
32 : char buff[100];
33 0 : sprintf(buff, "%s-%d", name.c_str(), task_instance);
34 0 : queue_->set_name(buff);
35 0 : if (token_pool_)
36 0 : queue_->SetStartRunnerFunc(boost::bind(&FlowEventQueueBase::TokenCheck,
37 : this));
38 0 : queue_->set_measure_busy_time(proto->agent()->MeasureQueueDelay());
39 0 : 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 0 : }
46 :
47 0 : FlowEventQueueBase::~FlowEventQueueBase() {
48 0 : delete queue_;
49 0 : }
50 :
51 0 : void FlowEventQueueBase::Shutdown() {
52 0 : queue_->Shutdown();
53 0 : }
54 :
55 0 : void FlowEventQueueBase::Enqueue(FlowEvent *event) {
56 0 : if (CanEnqueue(event) == false) {
57 0 : delete event;
58 0 : return;
59 : }
60 0 : queue_->Enqueue(event);
61 : }
62 :
63 0 : bool FlowEventQueueBase::TokenCheck() {
64 0 : 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 0 : bool FlowEventQueueBase::Handler(FlowEvent *event) {
98 0 : std::unique_ptr<FlowEvent> event_ptr(event);
99 0 : count_++;
100 0 : if (CanProcess(event) == false) {
101 0 : ProcessDone(event, false);
102 0 : return true;
103 : }
104 :
105 0 : HandleEvent(event);
106 :
107 0 : ProcessDone(event, true);
108 0 : return true;
109 0 : }
110 :
111 0 : bool FlowEventQueueBase::CanEnqueue(FlowEvent *event) {
112 0 : FlowEntry *flow = event->flow();
113 0 : bool ret = true;
114 0 : switch (event->event()) {
115 :
116 0 : case FlowEvent::DELETE_DBENTRY:
117 : case FlowEvent::DELETE_FLOW: {
118 0 : std::scoped_lock mutext(flow->mutex());
119 0 : ret = flow->GetPendingAction()->SetDelete();
120 0 : break;
121 0 : }
122 :
123 : // lock already token for the flow
124 0 : case FlowEvent::FLOW_MESSAGE: {
125 0 : ret = flow->GetPendingAction()->SetRecompute();
126 0 : break;
127 : }
128 :
129 0 : case FlowEvent::RECOMPUTE_FLOW: {
130 0 : std::scoped_lock mutext(flow->mutex());
131 0 : ret = flow->GetPendingAction()->SetRecomputeDBEntry();
132 0 : break;
133 0 : }
134 :
135 0 : case FlowEvent::REVALUATE_DBENTRY: {
136 0 : std::scoped_lock mutext(flow->mutex());
137 0 : ret = flow->GetPendingAction()->SetRevaluate();
138 0 : break;
139 0 : }
140 :
141 0 : default:
142 0 : break;
143 : }
144 :
145 0 : return ret;
146 : }
147 :
148 0 : bool FlowEventQueueBase::CanProcess(FlowEvent *event) {
149 0 : FlowEntry *flow = event->flow();
150 0 : bool ret = true;
151 0 : switch (event->event()) {
152 :
153 0 : case FlowEvent::DELETE_DBENTRY:
154 : case FlowEvent::DELETE_FLOW: {
155 0 : std::scoped_lock mutext(flow->mutex());
156 0 : events_processed_++;
157 0 : ret = flow->GetPendingAction()->CanDelete();
158 0 : break;
159 0 : }
160 :
161 0 : case FlowEvent::FLOW_MESSAGE: {
162 0 : std::scoped_lock mutext(flow->mutex());
163 0 : events_processed_++;
164 0 : ret = flow->GetPendingAction()->CanRecompute();
165 0 : break;
166 0 : }
167 :
168 0 : case FlowEvent::RECOMPUTE_FLOW: {
169 0 : std::scoped_lock mutext(flow->mutex());
170 0 : events_processed_++;
171 0 : ret = flow->GetPendingAction()->CanRecomputeDBEntry();
172 0 : break;
173 0 : }
174 :
175 0 : case FlowEvent::REVALUATE_DBENTRY: {
176 0 : events_processed_++;
177 0 : std::scoped_lock mutext(flow->mutex());
178 0 : ret = flow->GetPendingAction()->CanRevaluate();
179 0 : break;
180 0 : }
181 :
182 0 : default:
183 0 : break;
184 : }
185 :
186 0 : return ret;
187 : }
188 :
189 0 : void FlowEventQueueBase::ProcessDone(FlowEvent *event, bool update_rev_flow) {
190 0 : FlowEntry *flow = event->flow();
191 0 : FlowEntry *rflow = NULL;
192 0 : if (flow && update_rev_flow)
193 0 : rflow = flow->reverse_flow_entry();
194 :
195 0 : switch (event->event()) {
196 :
197 0 : case FlowEvent::DELETE_DBENTRY:
198 : case FlowEvent::DELETE_FLOW: {
199 0 : FLOW_LOCK(flow, rflow, event->event());
200 0 : flow->GetPendingAction()->ResetDelete();
201 0 : if (rflow)
202 0 : rflow->GetPendingAction()->ResetDelete();
203 0 : break;
204 0 : }
205 :
206 0 : case FlowEvent::FLOW_MESSAGE: {
207 0 : FLOW_LOCK(flow, rflow, event->event());
208 0 : flow->GetPendingAction()->ResetRecompute();
209 0 : if (rflow)
210 0 : rflow->GetPendingAction()->ResetRecompute();
211 0 : break;
212 0 : }
213 :
214 0 : case FlowEvent::RECOMPUTE_FLOW: {
215 0 : std::scoped_lock mutext(flow->mutex());
216 0 : flow->GetPendingAction()->ResetRecomputeDBEntry();
217 0 : break;
218 0 : }
219 :
220 0 : case FlowEvent::REVALUATE_DBENTRY: {
221 0 : FLOW_LOCK(flow, rflow, event->event());
222 0 : flow->GetPendingAction()->ResetRevaluate();
223 0 : if (rflow)
224 0 : rflow->GetPendingAction()->ResetRevaluate();
225 0 : break;
226 0 : }
227 :
228 0 : default:
229 0 : break;
230 : }
231 :
232 0 : return;
233 : }
234 :
235 0 : FlowEventQueue::FlowEventQueue(Agent *agent, FlowProto *proto,
236 : FlowTable *table, FlowTokenPool *pool,
237 : uint16_t latency_limit,
238 0 : uint32_t max_iterations) :
239 : FlowEventQueueBase(proto, "Flow Event Queue",
240 0 : agent->task_scheduler()->GetTaskId(kTaskFlowEvent),
241 0 : table->table_index(), pool, latency_limit,
242 : max_iterations),
243 0 : flow_table_(table) {
244 0 : }
245 :
246 0 : FlowEventQueue::~FlowEventQueue() {
247 0 : }
248 :
249 0 : bool FlowEventQueue::HandleEvent(FlowEvent *event) {
250 0 : return flow_proto_->FlowEventHandler(event, flow_table_);
251 : }
252 :
253 0 : DeleteFlowEventQueue::DeleteFlowEventQueue(Agent *agent, FlowProto *proto,
254 : FlowTable *table,
255 : FlowTokenPool *pool,
256 : uint16_t latency_limit,
257 0 : uint32_t max_iterations) :
258 : FlowEventQueueBase(proto, "Flow Delete Queue",
259 0 : agent->task_scheduler()->GetTaskId(kTaskFlowDelete),
260 0 : table->table_index(), pool, latency_limit,
261 : max_iterations),
262 0 : flow_table_(table) {
263 0 : }
264 :
265 0 : DeleteFlowEventQueue::~DeleteFlowEventQueue() {
266 0 : }
267 :
268 0 : bool DeleteFlowEventQueue::HandleEvent(FlowEvent *event) {
269 0 : return flow_proto_->FlowDeleteHandler(event, flow_table_);
270 : }
271 :
272 0 : KSyncFlowEventQueue::KSyncFlowEventQueue(Agent *agent, FlowProto *proto,
273 : FlowTable *table,
274 : FlowTokenPool *pool,
275 : uint16_t latency_limit,
276 0 : uint32_t max_iterations) :
277 : FlowEventQueueBase(proto, "Flow KSync Queue",
278 0 : agent->task_scheduler()->GetTaskId(kTaskFlowKSync),
279 0 : table->table_index(), pool, latency_limit,
280 : max_iterations),
281 0 : flow_table_(table) {
282 0 : }
283 :
284 0 : KSyncFlowEventQueue::~KSyncFlowEventQueue() {
285 0 : }
286 :
287 0 : bool KSyncFlowEventQueue::HandleEvent(FlowEvent *event) {
288 0 : return flow_proto_->FlowKSyncMsgHandler(event, flow_table_);
289 : }
290 :
291 0 : UpdateFlowEventQueue::UpdateFlowEventQueue(Agent *agent, FlowProto *proto,
292 : FlowTokenPool *pool,
293 : uint16_t latency_limit,
294 0 : uint32_t max_iterations) :
295 : FlowEventQueueBase(proto, "Flow Update Queue",
296 0 : agent->task_scheduler()->GetTaskId(kTaskFlowUpdate), 0,
297 0 : pool, latency_limit, max_iterations) {
298 0 : }
299 :
300 0 : UpdateFlowEventQueue::~UpdateFlowEventQueue() {
301 0 : }
302 :
303 0 : bool UpdateFlowEventQueue::HandleEvent(FlowEvent *event) {
304 0 : return flow_proto_->FlowUpdateHandler(event);
305 : }
|