Speed up packetin throttling
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceContextImpl.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.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import io.netty.util.HashedWheelTimer;
16 import io.netty.util.Timeout;
17 import java.math.BigInteger;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
26 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
27 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
30 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
31 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
32 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
35 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
36 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
37 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
38 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
39 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextClosedHandler;
40 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
41 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
42 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
43 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
44 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
45 import org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry;
46 import org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry;
47 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
48 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
49 import org.opendaylight.openflowplugin.impl.device.listener.MultiMsgCollectorImpl;
50 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
51 import org.opendaylight.openflowplugin.impl.registry.group.DeviceGroupRegistryImpl;
52 import org.opendaylight.openflowplugin.impl.registry.meter.DeviceMeterRegistryImpl;
53 import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketIn;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
72 import org.opendaylight.yangtools.yang.binding.ChildOf;
73 import org.opendaylight.yangtools.yang.binding.DataObject;
74 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
75 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78
79 /**
80  *
81  */
82 public class DeviceContextImpl implements DeviceContext {
83
84     private static final Logger LOG = LoggerFactory.getLogger(DeviceContextImpl.class);
85
86     // TODO: watermarks should be derived from effective rpc limit (75%|95%)
87     private static final int PACKETIN_LOW_WATERMARK = 15000;
88     private static final int PACKETIN_HIGH_WATERMARK = 19000;
89     // TODO: drain factor should be parametrized
90     public static final float REJECTED_DRAIN_FACTOR = 0.25f;
91
92     private final ConnectionContext primaryConnectionContext;
93     private final DeviceState deviceState;
94     private final DataBroker dataBroker;
95     private final HashedWheelTimer hashedWheelTimer;
96     private final Map<SwitchConnectionDistinguisher, ConnectionContext> auxiliaryConnectionContexts;
97     private final TransactionChainManager txChainManager;
98     private final DeviceFlowRegistry deviceFlowRegistry;
99     private final DeviceGroupRegistry deviceGroupRegistry;
100     private final DeviceMeterRegistry deviceMeterRegistry;
101     private final Collection<DeviceContextClosedHandler> closeHandlers = new HashSet<>();
102     private final PacketInRateLimiter packetInLimiter;
103     private final MessageSpy messageSpy;
104     private NotificationPublishService notificationPublishService;
105     private DeviceDisconnectedHandler deviceDisconnectedHandler;
106     private NotificationService notificationService;
107     private TranslatorLibrary translatorLibrary;
108     private OutboundQueue outboundQueueProvider;
109     private Timeout barrierTaskTimeout;
110
111     @VisibleForTesting
112     DeviceContextImpl(@Nonnull final ConnectionContext primaryConnectionContext,
113                       @Nonnull final DeviceState deviceState,
114                       @Nonnull final DataBroker dataBroker,
115                       @Nonnull final HashedWheelTimer hashedWheelTimer,
116                       @Nonnull final MessageSpy _messageSpy, OutboundQueueProvider outboundQueueProvider) {
117         this.primaryConnectionContext = Preconditions.checkNotNull(primaryConnectionContext);
118         this.deviceState = Preconditions.checkNotNull(deviceState);
119         this.dataBroker = Preconditions.checkNotNull(dataBroker);
120         this.hashedWheelTimer = Preconditions.checkNotNull(hashedWheelTimer);
121         this.outboundQueueProvider = Preconditions.checkNotNull(outboundQueueProvider);
122         txChainManager = new TransactionChainManager(dataBroker, deviceState);
123         auxiliaryConnectionContexts = new HashMap<>();
124         deviceFlowRegistry = new DeviceFlowRegistryImpl();
125         deviceGroupRegistry = new DeviceGroupRegistryImpl();
126         deviceMeterRegistry = new DeviceMeterRegistryImpl();
127         messageSpy = _messageSpy;
128
129         this.packetInLimiter = new PacketInRateLimiter(primaryConnectionContext.getConnectionAdapter(),
130                 PACKETIN_LOW_WATERMARK, PACKETIN_HIGH_WATERMARK, messageSpy, REJECTED_DRAIN_FACTOR);
131     }
132
133     /**
134      * This method is called from {@link DeviceManagerImpl} only. So we could say "posthandshake process finish"
135      * and we are able to set a scheduler for an automatic transaction submitting by time (0,5sec).
136      */
137     void initialSubmitTransaction() {
138         txChainManager.initialSubmitWriteTransaction();
139     }
140
141     @Override
142     public Long getReservedXid() {
143         return outboundQueueProvider.reserveEntry();
144     }
145
146     @Override
147     public <M extends ChildOf<DataObject>> void onMessage(final M message, final RequestContext<?> requestContext) {
148         // TODO Auto-generated method stub
149     }
150
151     @Override
152     public void addAuxiliaryConenctionContext(final ConnectionContext connectionContext) {
153         final SwitchConnectionDistinguisher connectionDistinguisher = createConnectionDistinguisher(connectionContext);
154         auxiliaryConnectionContexts.put(connectionDistinguisher, connectionContext);
155     }
156
157     private static SwitchConnectionDistinguisher createConnectionDistinguisher(final ConnectionContext connectionContext) {
158         return new SwitchConnectionCookieOFImpl(connectionContext.getFeatures().getAuxiliaryId());
159     }
160
161     @Override
162     public void removeAuxiliaryConenctionContext(final ConnectionContext connectionContext) {
163         // TODO Auto-generated method stub
164     }
165
166     @Override
167     public DeviceState getDeviceState() {
168         return deviceState;
169     }
170
171     @Override
172     public ReadTransaction getReadTransaction() {
173         return dataBroker.newReadOnlyTransaction();
174     }
175
176     @Override
177     public <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store,
178                                                           final InstanceIdentifier<T> path, final T data) {
179         txChainManager.writeToTransaction(store, path, data);
180     }
181
182     @Override
183     public <T extends DataObject> void addDeleteToTxChain(final LogicalDatastoreType store, final InstanceIdentifier<T> path) {
184         txChainManager.addDeleteOperationTotTxChain(store, path);
185     }
186
187     @Override
188     public boolean submitTransaction() {
189         return txChainManager.submitWriteTransaction();
190     }
191
192     @Override
193     public ConnectionContext getPrimaryConnectionContext() {
194         return primaryConnectionContext;
195     }
196
197     @Override
198     public ConnectionContext getAuxiliaryConnectiobContexts(final BigInteger cookie) {
199         return auxiliaryConnectionContexts.get(new SwitchConnectionCookieOFImpl(cookie.longValue()));
200     }
201
202     @Override
203     public DeviceFlowRegistry getDeviceFlowRegistry() {
204         return deviceFlowRegistry;
205     }
206
207     @Override
208     public DeviceGroupRegistry getDeviceGroupRegistry() {
209         return deviceGroupRegistry;
210     }
211
212     @Override
213     public DeviceMeterRegistry getDeviceMeterRegistry() {
214         return deviceMeterRegistry;
215     }
216
217     @Override
218     public void processReply(final OfHeader ofHeader) {
219         if (ofHeader instanceof Error) {
220             messageSpy.spyMessage(ofHeader.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
221         } else {
222             messageSpy.spyMessage(ofHeader.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
223         }
224     }
225
226     @Override
227     public void processReply(final Xid xid, final List<MultipartReply> ofHeaderList) {
228         for (final MultipartReply multipartReply : ofHeaderList) {
229             messageSpy.spyMessage(multipartReply.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
230         }
231     }
232
233     @Override
234     public void processFlowRemovedMessage(final FlowRemoved flowRemoved) {
235         //TODO: will be defined later
236     }
237
238     @Override
239     public void processPortStatusMessage(final PortStatusMessage portStatus) {
240         messageSpy.spyMessage(portStatus.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
241         final TranslatorKey translatorKey = new TranslatorKey(portStatus.getVersion(), PortGrouping.class.getName());
242         final MessageTranslator<PortGrouping, FlowCapableNodeConnector> messageTranslator = translatorLibrary.lookupTranslator(translatorKey);
243         final FlowCapableNodeConnector flowCapableNodeConnector = messageTranslator.translate(portStatus, this, null);
244
245         final KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> iiToNodeConnector = provideIIToNodeConnector(portStatus.getPortNo(), portStatus.getVersion());
246         if (portStatus.getReason().equals(PortReason.OFPPRADD) || portStatus.getReason().equals(PortReason.OFPPRMODIFY)) {
247             // because of ADD status node connector has to be created
248             final NodeConnectorBuilder nConnectorBuilder = new NodeConnectorBuilder().setKey(iiToNodeConnector.getKey());
249             nConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
250             nConnectorBuilder.addAugmentation(FlowCapableNodeConnector.class, flowCapableNodeConnector);
251             writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToNodeConnector, nConnectorBuilder.build());
252         } else if (portStatus.getReason().equals(PortReason.OFPPRDELETE)) {
253             addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, iiToNodeConnector);
254         }
255         submitTransaction();
256     }
257
258     private KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> provideIIToNodeConnector(final long portNo, final short version) {
259         final InstanceIdentifier<Node> iiToNodes = deviceState.getNodeInstanceIdentifier();
260         final BigInteger dataPathId = deviceState.getFeatures().getDatapathId();
261         final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), portNo, version);
262         return iiToNodes.child(NodeConnector.class, new NodeConnectorKey(nodeConnectorId));
263     }
264
265     @Override
266     public void processPacketInMessage(final PacketInMessage packetInMessage) {
267         messageSpy.spyMessage(packetInMessage.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH);
268         final ConnectionAdapter connectionAdapter = getPrimaryConnectionContext().getConnectionAdapter();
269
270         final TranslatorKey translatorKey = new TranslatorKey(packetInMessage.getVersion(), PacketIn.class.getName());
271         final MessageTranslator<PacketInMessage, PacketReceived> messageTranslator = translatorLibrary.lookupTranslator(translatorKey);
272         final PacketReceived packetReceived = messageTranslator.translate(packetInMessage, this, null);
273
274         if (packetReceived == null) {
275             LOG.debug("Received a null packet from switch {}", connectionAdapter.getRemoteAddress());
276             return;
277         }
278
279         if (!packetInLimiter.acquirePermit()) {
280             LOG.debug("Packet limited");
281             // TODO: save packet into emergency slot if possible
282             // FIXME: some other counter
283             messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_SRC_FAILURE);
284             return;
285         }
286
287         final ListenableFuture<? extends Object> offerNotification = notificationPublishService.offerNotification(packetReceived);
288         if (NotificationPublishService.REJECTED.equals(offerNotification)) {
289             LOG.debug("notification offer rejected");
290             messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_SRC_FAILURE);
291             packetInLimiter.drainLowWaterMark();
292             packetInLimiter.releasePermit();
293             return;
294         }
295
296         Futures.addCallback(offerNotification, new FutureCallback<Object>() {
297             @Override
298             public void onSuccess(final Object result) {
299                 messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
300                 packetInLimiter.releasePermit();
301             }
302
303             @Override
304             public void onFailure(final Throwable t) {
305                 messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_NOTIFICATION_REJECTED);
306                 LOG.debug("notification offer failed: {}", t.getMessage());
307                 LOG.trace("notification offer failed..", t);
308                 packetInLimiter.releasePermit();
309             }
310         });
311     }
312
313     @Override
314     public TranslatorLibrary oook() {
315         return translatorLibrary;
316     }
317
318     @Override
319     public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
320         this.translatorLibrary = translatorLibrary;
321     }
322
323     @Override
324     public HashedWheelTimer getTimer() {
325         return hashedWheelTimer;
326     }
327
328     @Override
329     public void close() throws Exception {
330         deviceState.setValid(false);
331
332         deviceGroupRegistry.close();
333         deviceFlowRegistry.close();
334         deviceMeterRegistry.close();
335
336         primaryConnectionContext.close();
337         for (final ConnectionContext connectionContext : auxiliaryConnectionContexts.values()) {
338             connectionContext.close();
339         }
340
341         for (final DeviceContextClosedHandler deviceContextClosedHandler : closeHandlers) {
342             deviceContextClosedHandler.onDeviceContextClosed(this);
343         }
344
345         txChainManager.close();
346     }
347
348     @Override
349     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
350         if (getPrimaryConnectionContext().equals(connectionContext)) {
351             try {
352                 close();
353             } catch (final Exception e) {
354                 LOG.trace("Error closing device context.");
355             }
356             if (null != deviceDisconnectedHandler) {
357                 deviceDisconnectedHandler.onDeviceDisconnected(connectionContext);
358             }
359         } else {
360             final SwitchConnectionDistinguisher connectionDistinguisher = createConnectionDistinguisher(connectionContext);
361             auxiliaryConnectionContexts.remove(connectionDistinguisher);
362         }
363     }
364
365     @Override
366     public void setCurrentBarrierTimeout(final Timeout timeout) {
367         barrierTaskTimeout = timeout;
368     }
369
370     @Override
371     public Timeout getBarrierTaskTimeout() {
372         return barrierTaskTimeout;
373     }
374
375     @Override
376     public void setNotificationService(final NotificationService notificationServiceParam) {
377         notificationService = notificationServiceParam;
378     }
379
380     @Override
381     public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
382         this.notificationPublishService = notificationPublishService;
383     }
384
385     @Override
386     public MessageSpy getMessageSpy() {
387         return messageSpy;
388     }
389
390     @Override
391     public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
392         this.deviceDisconnectedHandler = deviceDisconnectedHandler;
393     }
394
395     @Override
396     public void addDeviceContextClosedHandler(final DeviceContextClosedHandler deviceContextClosedHandler) {
397         closeHandlers.add(deviceContextClosedHandler);
398     }
399
400     @Override
401     public void onPublished() {
402         primaryConnectionContext.getConnectionAdapter().setPacketInFiltering(false);
403         for (final ConnectionContext switchAuxConnectionContext : auxiliaryConnectionContexts.values()) {
404             switchAuxConnectionContext.getConnectionAdapter().setPacketInFiltering(false);
405         }
406     }
407
408     @Override
409     public MultiMsgCollector getMultiMsgCollector(final RequestContext<List<MultipartReply>> requestContext) {
410         return new MultiMsgCollectorImpl(this, requestContext);
411     }
412 }