Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.flow.impl; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 17 | |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 19 | |
| 20 | import org.onlab.util.SlidingWindowCounter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 22 | import org.onosproject.openflow.controller.RoleState; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 23 | import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest; |
| 24 | import org.projectfloodlight.openflow.types.OFPort; |
| 25 | import org.projectfloodlight.openflow.types.TableId; |
| 26 | import org.slf4j.Logger; |
| 27 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 28 | import java.util.TimerTask; |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 29 | import java.util.concurrent.ScheduledExecutorService; |
| 30 | import java.util.concurrent.ScheduledFuture; |
| 31 | import java.util.concurrent.TimeUnit; |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 32 | import java.util.concurrent.atomic.AtomicBoolean; |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 33 | import java.util.concurrent.atomic.AtomicInteger; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 34 | |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 35 | import static java.lang.Math.max; |
| 36 | import static java.lang.Math.min; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Collects flow statistics for the specified switch. |
| 41 | */ |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 42 | class FlowStatsCollector implements SwitchDataCollector { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 43 | |
| 44 | private final Logger log = getLogger(getClass()); |
| 45 | |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 46 | private static final int MS = 1000; |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 47 | |
| 48 | // Number of ticks which defines the pause window. |
| 49 | private static final int PAUSE_WINDOW = 2; |
| 50 | // Number of ticks which defines the high load window |
| 51 | private static final int HIGH_WINDOW = 60; |
| 52 | // Number of ticks which defines the low load window |
| 53 | private static final int LOW_WINDOW = 15; |
| 54 | // Multiplier factor of the load |
| 55 | private static final int LOAD_FACTOR = 2; |
| 56 | // Event/s defining the min load rate |
| 57 | private static final int MIN_LOAD_RATE = 50; |
| 58 | // Event/s defining the max load rate |
| 59 | private static final int MAX_LOAD_RATE = 500; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 60 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 61 | private final OpenFlowSwitch sw; |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 62 | private ScheduledExecutorService executorService; |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 63 | private TimerTask pauseTask; |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 64 | private ScheduledFuture<?> scheduledPauseTask; |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 65 | private TimerTask pollTask; |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 66 | private ScheduledFuture<?> scheduledPollTask; |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 67 | |
| 68 | private SlidingWindowCounter loadCounter; |
| 69 | // Defines whether the collector is in pause or not for high load |
| 70 | private final AtomicBoolean paused = new AtomicBoolean(); |
| 71 | // Defines whether the collector is in waiting or not for a previous stats reply |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 72 | private static final int WAITING_ATTEMPTS = 5; |
| 73 | private final AtomicInteger waiting = new AtomicInteger(0); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 74 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 75 | private int pollInterval; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 76 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Creates a new collector for the given switch and poll frequency. |
| 79 | * |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 80 | * @param executorService executor used for scheduling |
| 81 | * @param sw switch to pull |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 82 | * @param pollInterval poll frequency in seconds |
| 83 | */ |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 84 | FlowStatsCollector(ScheduledExecutorService executorService, OpenFlowSwitch sw, int pollInterval) { |
| 85 | this.executorService = executorService; |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 86 | this.sw = checkNotNull(sw, "Null switch"); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 87 | this.pollInterval = pollInterval; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Adjusts poll frequency. |
| 92 | * |
| 93 | * @param pollInterval poll frequency in seconds |
| 94 | */ |
| 95 | synchronized void adjustPollInterval(int pollInterval) { |
| 96 | this.pollInterval = pollInterval; |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 97 | if (pollTask != null) { |
| 98 | pollTask.cancel(); |
| 99 | } |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 100 | if (scheduledPollTask != null) { |
| 101 | scheduledPollTask.cancel(false); |
| 102 | } |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 103 | // If we went through start - let's schedule it |
| 104 | if (loadCounter != null) { |
| 105 | pollTask = new PollTimerTask(); |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 106 | scheduledPollTask = executorService.scheduleAtFixedRate(pollTask, pollInterval * MS, |
| 107 | pollInterval * MS, TimeUnit.MILLISECONDS); |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 108 | } |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 109 | waiting.set(0); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 110 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 111 | |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Resets the collector's event count. |
| 114 | */ |
| 115 | @Override |
| 116 | public synchronized void resetEvents() { |
| 117 | loadCounter.clear(); |
| 118 | if (paused.compareAndSet(true, false)) { |
| 119 | resume(); |
| 120 | } |
| 121 | // Let's reset also waiting, the reply can be discarded/lost |
| 122 | // during a change of mastership |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 123 | waiting.set(0); |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Records a number of flow events that have occurred. |
| 128 | * |
| 129 | * @param events the number of events that occurred |
| 130 | */ |
| 131 | @Override |
| 132 | public void recordEvents(int events) { |
| 133 | SlidingWindowCounter loadCounter = this.loadCounter; |
| 134 | if (loadCounter != null) { |
| 135 | loadCounter.incrementCount(events); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns a boolean indicating whether the switch is under high load. |
| 141 | * <p> |
| 142 | * The switch is considered under high load if the average rate over the last two seconds is |
| 143 | * greater than twice the overall rate or 50 flows/sec. |
| 144 | * |
| 145 | * @return indicates whether the switch is under high load |
| 146 | */ |
| 147 | private boolean isHighLoad() { |
| 148 | return loadCounter.getWindowRate(PAUSE_WINDOW) |
| 149 | > max(min(loadCounter.getWindowRate(HIGH_WINDOW) * LOAD_FACTOR, MAX_LOAD_RATE), MIN_LOAD_RATE); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns a boolean indicating whether the switch is under low load. |
| 154 | * <p> |
| 155 | * The switch is considered under low load if the average rate over the last 15 seconds is |
| 156 | * less than the overall rate. |
| 157 | * |
| 158 | * @return indicates whether the switch is under low load |
| 159 | */ |
| 160 | private boolean isLowLoad() { |
| 161 | return loadCounter.getWindowRate(LOW_WINDOW) < loadCounter.getWindowRate(HIGH_WINDOW); |
| 162 | } |
| 163 | |
| 164 | private class PauseTimerTask extends TimerTask { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 165 | @Override |
| 166 | public void run() { |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 167 | if (isHighLoad()) { |
| 168 | if (paused.compareAndSet(false, true)) { |
| 169 | pause(); |
| 170 | } |
| 171 | } else if (isLowLoad()) { |
| 172 | if (paused.compareAndSet(true, false)) { |
| 173 | resume(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | private class PollTimerTask extends TimerTask { |
| 180 | @Override |
| 181 | public void run() { |
| 182 | // Check whether we are still waiting a previous reply |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 183 | if (waiting.getAndDecrement() > 0) { |
| 184 | log.debug("Skipping stats collection for {} waiting for previous reply", sw.getStringId()); |
| 185 | return; |
| 186 | } |
| 187 | // Check whether we are the master of the switch |
| 188 | if (sw.getRole() == RoleState.MASTER) { |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 189 | // Check whether the switch is under high load from this master. This is done here in case a large |
| 190 | // batch was pushed immediately prior to this task running. |
| 191 | if (isHighLoad()) { |
| 192 | log.debug("Skipping stats collection for {} due to high load; rate: {}; overall: {}", |
| 193 | sw.getStringId(), |
| 194 | loadCounter.getWindowRate(PAUSE_WINDOW), |
| 195 | loadCounter.getWindowRate(HIGH_WINDOW)); |
| 196 | return; |
| 197 | } else { |
| 198 | log.debug( |
| 199 | "Permitting stats collection for {}; rate: {}; overall: {}", |
| 200 | sw.getStringId(), |
| 201 | loadCounter.getWindowRate(PAUSE_WINDOW), |
| 202 | loadCounter.getWindowRate(HIGH_WINDOW)); |
| 203 | } |
| 204 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 205 | log.trace("Collecting stats for {}", sw.getStringId()); |
| 206 | OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest() |
| 207 | .setMatch(sw.factory().matchWildcardAll()) |
| 208 | .setTableId(TableId.ALL) |
| 209 | .setOutPort(OFPort.NO_MASK) |
| 210 | .build(); |
| 211 | sw.sendMsg(request); |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 212 | // Other flow stats will not be asked |
| 213 | // if we don't see first the reply of this request |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 214 | waiting.set(WAITING_ATTEMPTS); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 215 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 216 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 219 | public synchronized void start() { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 220 | log.debug("Starting Stats collection thread for {}", sw.getStringId()); |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 221 | loadCounter = new SlidingWindowCounter(HIGH_WINDOW); |
Shibu Vijayakumar | 7f73c72 | 2019-07-04 07:13:37 -0400 | [diff] [blame^] | 222 | if (pollInterval > 0) { |
| 223 | pauseTask = new PauseTimerTask(); |
| 224 | scheduledPauseTask = executorService.scheduleAtFixedRate(pauseTask, 1 * MS, |
| 225 | 1 * MS, TimeUnit.MILLISECONDS); |
| 226 | pollTask = new PollTimerTask(); |
| 227 | // Initially start polling quickly. Then drop down to configured value |
| 228 | scheduledPollTask = executorService.scheduleAtFixedRate(pollTask, 1 * MS, |
| 229 | pollInterval * MS, TimeUnit.MILLISECONDS); |
| 230 | } else { |
| 231 | // Trigger the poll only once |
| 232 | pollTask = new PollTimerTask(); |
| 233 | executorService.schedule(pollTask, 0, TimeUnit.MILLISECONDS); |
| 234 | } |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | private synchronized void pause() { |
| 238 | if (pollTask != null) { |
| 239 | log.debug("Pausing stats collection for {}; rate: {}; overall: {}", |
| 240 | sw.getStringId(), |
| 241 | loadCounter.getWindowRate(PAUSE_WINDOW), |
| 242 | loadCounter.getWindowRate(HIGH_WINDOW)); |
| 243 | pollTask.cancel(); |
| 244 | pollTask = null; |
| 245 | } |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 246 | if (scheduledPollTask != null) { |
| 247 | scheduledPollTask.cancel(false); |
| 248 | scheduledPollTask = null; |
| 249 | } |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | private synchronized void resume() { |
| 253 | log.debug("Resuming stats collection for {}; rate: {}; overall: {}", |
| 254 | sw.getStringId(), |
| 255 | loadCounter.getWindowRate(PAUSE_WINDOW), |
| 256 | loadCounter.getWindowRate(HIGH_WINDOW)); |
| 257 | pollTask = new PollTimerTask(); |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 258 | scheduledPollTask = executorService.scheduleAtFixedRate(pollTask, pollInterval * MS, |
| 259 | pollInterval * MS, TimeUnit.MILLISECONDS); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 262 | public synchronized void stop() { |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 263 | if (pauseTask != null) { |
| 264 | pauseTask.cancel(); |
| 265 | pauseTask = null; |
Pier Luigi | 15e281d | 2018-03-04 14:30:43 +0100 | [diff] [blame] | 266 | } |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 267 | if (scheduledPauseTask != null) { |
| 268 | scheduledPauseTask.cancel(false); |
| 269 | scheduledPauseTask = null; |
| 270 | } |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 271 | if (pollTask != null) { |
| 272 | log.debug("Stopping Stats collection thread for {}", sw.getStringId()); |
| 273 | pollTask.cancel(); |
| 274 | pollTask = null; |
| 275 | } |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 276 | if (scheduledPollTask != null) { |
| 277 | scheduledPollTask.cancel(false); |
| 278 | scheduledPollTask = null; |
| 279 | } |
Jordan Halterman | df4b08a | 2019-03-05 18:44:03 -0800 | [diff] [blame] | 280 | if (loadCounter != null) { |
| 281 | loadCounter.destroy(); |
| 282 | loadCounter = null; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | public void received() { |
pier | 14e9332 | 2020-01-09 13:10:04 +0100 | [diff] [blame] | 287 | waiting.set(0); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | } |