Speed up packetin throttling
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / PacketInRateLimiter.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.device;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
12 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
13 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 final class PacketInRateLimiter extends SimpleRatelimiter {
18     private static final Logger LOG = LoggerFactory.getLogger(PacketInRateLimiter.class);
19     private final float rejectedDrainFactor;
20     private final ConnectionAdapter connectionAdapter;
21     private final MessageSpy messageSpy;
22
23     PacketInRateLimiter(final ConnectionAdapter connectionAdapter, final int lowWatermark, final int highWatermark, final MessageSpy messageSpy, float rejectedDrainFactor) {
24         super(lowWatermark, highWatermark);
25         Preconditions.checkArgument(rejectedDrainFactor > 0 && rejectedDrainFactor < 1);
26         this.rejectedDrainFactor = rejectedDrainFactor;
27         this.connectionAdapter = Preconditions.checkNotNull(connectionAdapter);
28         this.messageSpy = Preconditions.checkNotNull(messageSpy);
29     }
30
31     @Override
32     protected void disableFlow() {
33         messageSpy.spyMessage(DeviceContext.class, MessageSpy.STATISTIC_GROUP.OFJ_BACKPRESSURE_ON);
34         connectionAdapter.setPacketInFiltering(true);
35         LOG.debug("PacketIn filtering on: {}", connectionAdapter.getRemoteAddress());
36     }
37
38     @Override
39     protected void enableFlow() {
40         messageSpy.spyMessage(DeviceContext.class, MessageSpy.STATISTIC_GROUP.OFJ_BACKPRESSURE_OFF);
41         connectionAdapter.setPacketInFiltering(false);
42         LOG.debug("PacketIn filtering off: {}", connectionAdapter.getRemoteAddress());
43     }
44
45     public void drainLowWaterMark() {
46         adaptLowWaterMarkAndDisableFlow((int) (getOccupiedPermits() * rejectedDrainFactor));
47     }
48 }