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 | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalCause; |
| 21 | import com.google.common.cache.RemovalNotification; |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import com.google.common.collect.Lists; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Maps; |
| 25 | import com.google.common.collect.Sets; |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 26 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 27 | import io.netty.buffer.ByteBuf; |
| 28 | import io.netty.buffer.Unpooled; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Modified; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 35 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.core.ApplicationId; |
| 37 | import org.onosproject.net.DeviceId; |
Rodrigo Duarte Sousa | e37d129 | 2017-08-29 17:01:24 -0300 | [diff] [blame] | 38 | import org.onosproject.net.driver.DefaultDriverData; |
| 39 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 40 | import org.onosproject.net.driver.Driver; |
| 41 | import org.onosproject.net.driver.DriverHandler; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 42 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.flow.CompletedBatchOperation; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 44 | import org.onosproject.net.flow.DefaultTableStatisticsEntry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.flow.FlowEntry; |
| 46 | import org.onosproject.net.flow.FlowRule; |
Ray Milkey | 7bf273c | 2017-09-27 16:15:15 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 48 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.FlowRuleProvider; |
| 50 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 51 | import org.onosproject.net.flow.FlowRuleProviderService; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flow.TableStatisticsEntry; |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 53 | import org.onosproject.net.flow.IndexTableId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.provider.AbstractProvider; |
| 55 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 56 | import org.onosproject.net.statistic.DefaultLoad; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 57 | import org.onosproject.openflow.controller.Dpid; |
| 58 | import org.onosproject.openflow.controller.OpenFlowController; |
| 59 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 60 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 61 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 62 | import org.onosproject.openflow.controller.RoleState; |
Thomas Vachuska | 95caba3 | 2016-04-04 10:42:05 -0700 | [diff] [blame] | 63 | import org.onosproject.provider.of.flow.util.FlowEntryBuilder; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 64 | import org.osgi.service.component.ComponentContext; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.protocol.OFBadRequestCode; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 66 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
Laszlo Papp | edadbe2 | 2017-12-14 20:05:49 +0000 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.protocol.OFCapabilities; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.protocol.OFFlowLightweightStatsReply; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 70 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 71 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 72 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 73 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 74 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 75 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 76 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 77 | import org.projectfloodlight.openflow.protocol.OFTableStatsEntry; |
| 78 | import org.projectfloodlight.openflow.protocol.OFTableStatsReply; |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 79 | import org.projectfloodlight.openflow.protocol.OFType; |
| 80 | import org.projectfloodlight.openflow.protocol.OFVersion; |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 81 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadActionErrorMsg; |
| 82 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadInstructionErrorMsg; |
| 83 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadMatchErrorMsg; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 84 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 85 | import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg; |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 86 | import org.projectfloodlight.openflow.types.U16; |
| 87 | import org.projectfloodlight.openflow.types.U64; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 88 | import org.slf4j.Logger; |
| 89 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 90 | import java.util.Collections; |
| 91 | import java.util.Dictionary; |
| 92 | import java.util.List; |
| 93 | import java.util.Map; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 94 | import java.util.Objects; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 95 | import java.util.Optional; |
| 96 | import java.util.Set; |
| 97 | import java.util.Timer; |
| 98 | import java.util.concurrent.TimeUnit; |
| 99 | import java.util.stream.Collectors; |
| 100 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 101 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 102 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 103 | import static org.onlab.util.Tools.get; |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 104 | import static org.onosproject.provider.of.flow.impl.OsgiPropertyConstants.*; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 105 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 106 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 107 | /** |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 108 | * Provider which uses an OpenFlow controller to detect network end-station |
| 109 | * hosts. |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 110 | */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 111 | @Component(immediate = true, |
| 112 | property = { |
| 113 | POLL_FREQUENCY + ":Integer=" + POLL_FREQUENCY_DEFAULT, |
| 114 | ADAPTIVE_FLOW_SAMPLING + ":Boolean=" + ADAPTIVE_FLOW_SAMPLING_DEFAULT, |
| 115 | }) |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 116 | public class OpenFlowRuleProvider extends AbstractProvider |
| 117 | implements FlowRuleProvider { |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 118 | |
| 119 | private final Logger log = getLogger(getClass()); |
| 120 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 122 | protected FlowRuleProviderRegistry providerRegistry; |
| 123 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 125 | protected OpenFlowController controller; |
| 126 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 128 | protected ComponentConfigService cfgService; |
| 129 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 131 | protected DriverService driverService; |
| 132 | |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 133 | private static final int MIN_EXPECTED_BYTE_LEN = 56; |
| 134 | private static final int SKIP_BYTES = 4; |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 135 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 136 | /** Frequency (in seconds) for polling flow statistics. */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 137 | private int flowPollFrequency = POLL_FREQUENCY_DEFAULT; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 138 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 139 | /** Adaptive Flow Sampling is on or off. */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 140 | private boolean adaptiveFlowSampling = ADAPTIVE_FLOW_SAMPLING_DEFAULT; |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 141 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 142 | private FlowRuleProviderService providerService; |
| 143 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 144 | private final InternalFlowProvider listener = new InternalFlowProvider(); |
| 145 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 146 | private Cache<Long, InternalCacheEntry> pendingBatches; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 147 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 148 | private final Timer timer = new Timer("onos-openflow-collector"); |
| 149 | |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 150 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 151 | // Old simple collector set |
Madan Jampani | 6b26610 | 2016-06-23 00:56:36 -0700 | [diff] [blame] | 152 | private final Map<Dpid, FlowStatsCollector> simpleCollectors = Maps.newConcurrentMap(); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 153 | |
| 154 | // NewAdaptiveFlowStatsCollector Set |
Madan Jampani | 6b26610 | 2016-06-23 00:56:36 -0700 | [diff] [blame] | 155 | private final Map<Dpid, NewAdaptiveFlowStatsCollector> afsCollectors = Maps.newConcurrentMap(); |
| 156 | private final Map<Dpid, TableStatisticsCollector> tableStatsCollectors = Maps.newConcurrentMap(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 157 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Creates an OpenFlow host provider. |
| 160 | */ |
| 161 | public OpenFlowRuleProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 162 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Activate |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 166 | protected void activate(ComponentContext context) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 167 | cfgService.registerProperties(getClass()); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 168 | providerService = providerRegistry.register(this); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 169 | controller.addListener(listener); |
| 170 | controller.addEventListener(listener); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 171 | |
Antonio Marsico | 1c5ae1f | 2015-12-15 15:31:56 +0100 | [diff] [blame] | 172 | modified(context); |
| 173 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 174 | pendingBatches = createBatchCache(); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 175 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 176 | createCollectors(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 177 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 178 | log.info("Started with flowPollFrequency = {}, adaptiveFlowSampling = {}", |
| 179 | flowPollFrequency, adaptiveFlowSampling); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Deactivate |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 183 | protected void deactivate(ComponentContext context) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 184 | cfgService.unregisterProperties(getClass(), false); |
| 185 | stopCollectors(); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 186 | providerRegistry.unregister(this); |
| 187 | providerService = null; |
| 188 | |
| 189 | log.info("Stopped"); |
| 190 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 191 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 192 | @Modified |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 193 | protected void modified(ComponentContext context) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 194 | Dictionary<?, ?> properties = context.getProperties(); |
| 195 | int newFlowPollFrequency; |
| 196 | try { |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 197 | String s = get(properties, POLL_FREQUENCY); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 198 | newFlowPollFrequency = isNullOrEmpty(s) ? flowPollFrequency : Integer.parseInt(s.trim()); |
| 199 | |
| 200 | } catch (NumberFormatException | ClassCastException e) { |
| 201 | newFlowPollFrequency = flowPollFrequency; |
| 202 | } |
| 203 | |
| 204 | if (newFlowPollFrequency != flowPollFrequency) { |
| 205 | flowPollFrequency = newFlowPollFrequency; |
| 206 | adjustRate(); |
| 207 | } |
| 208 | |
| 209 | log.info("Settings: flowPollFrequency={}", flowPollFrequency); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 210 | |
| 211 | boolean newAdaptiveFlowSampling; |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 212 | String s = get(properties, ADAPTIVE_FLOW_SAMPLING); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 213 | newAdaptiveFlowSampling = isNullOrEmpty(s) ? adaptiveFlowSampling : Boolean.parseBoolean(s.trim()); |
| 214 | |
| 215 | if (newAdaptiveFlowSampling != adaptiveFlowSampling) { |
| 216 | // stop previous collector |
| 217 | stopCollectors(); |
| 218 | adaptiveFlowSampling = newAdaptiveFlowSampling; |
| 219 | // create new collectors |
| 220 | createCollectors(); |
| 221 | } |
| 222 | |
| 223 | log.info("Settings: adaptiveFlowSampling={}", adaptiveFlowSampling); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | private Cache<Long, InternalCacheEntry> createBatchCache() { |
| 227 | return CacheBuilder.newBuilder() |
| 228 | .expireAfterWrite(10, TimeUnit.SECONDS) |
| 229 | .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> { |
| 230 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 231 | providerService.batchOperationCompleted(notification.getKey(), |
| 232 | notification.getValue().failedCompletion()); |
| 233 | } |
| 234 | }).build(); |
| 235 | } |
| 236 | |
| 237 | private void createCollectors() { |
| 238 | controller.getSwitches().forEach(this::createCollector); |
| 239 | } |
| 240 | |
| 241 | private void createCollector(OpenFlowSwitch sw) { |
Kavitha Alagesan | 6704df3 | 2016-08-18 15:15:31 +0530 | [diff] [blame] | 242 | if (sw == null) { |
| 243 | return; |
| 244 | } |
Laszlo Papp | 5817481 | 2018-01-16 13:29:47 +0000 | [diff] [blame] | 245 | if (sw.features().getCapabilities().contains(OFCapabilities.FLOW_STATS)) { |
| 246 | if (adaptiveFlowSampling) { |
| 247 | // NewAdaptiveFlowStatsCollector Constructor |
| 248 | NewAdaptiveFlowStatsCollector fsc = |
| 249 | new NewAdaptiveFlowStatsCollector(driverService, sw, flowPollFrequency); |
| 250 | stopCollectorIfNeeded(afsCollectors.put(new Dpid(sw.getId()), fsc)); |
| 251 | fsc.start(); |
| 252 | } else { |
| 253 | FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency); |
| 254 | stopCollectorIfNeeded(simpleCollectors.put(new Dpid(sw.getId()), fsc)); |
| 255 | fsc.start(); |
| 256 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 257 | } |
Laszlo Papp | edadbe2 | 2017-12-14 20:05:49 +0000 | [diff] [blame] | 258 | if (sw.features().getCapabilities().contains(OFCapabilities.TABLE_STATS)) { |
| 259 | TableStatisticsCollector tsc = new TableStatisticsCollector(timer, sw, flowPollFrequency); |
| 260 | stopCollectorIfNeeded(tableStatsCollectors.put(new Dpid(sw.getId()), tsc)); |
| 261 | tsc.start(); |
| 262 | } |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | private void stopCollectorIfNeeded(SwitchDataCollector collector) { |
| 266 | if (collector != null) { |
| 267 | collector.stop(); |
| 268 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | private void stopCollectors() { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 272 | if (adaptiveFlowSampling) { |
| 273 | // NewAdaptiveFlowStatsCollector Destructor |
| 274 | afsCollectors.values().forEach(NewAdaptiveFlowStatsCollector::stop); |
| 275 | afsCollectors.clear(); |
| 276 | } else { |
| 277 | simpleCollectors.values().forEach(FlowStatsCollector::stop); |
| 278 | simpleCollectors.clear(); |
| 279 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 280 | tableStatsCollectors.values().forEach(TableStatisticsCollector::stop); |
| 281 | tableStatsCollectors.clear(); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | private void adjustRate() { |
| 285 | DefaultLoad.setPollInterval(flowPollFrequency); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 286 | if (adaptiveFlowSampling) { |
| 287 | // NewAdaptiveFlowStatsCollector calAndPollInterval |
| 288 | afsCollectors.values().forEach(fsc -> fsc.adjustCalAndPollInterval(flowPollFrequency)); |
| 289 | } else { |
| 290 | simpleCollectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency)); |
| 291 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 292 | tableStatsCollectors.values().forEach(tsc -> tsc.adjustPollInterval(flowPollFrequency)); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 293 | } |
| 294 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 295 | @Override |
| 296 | public void applyFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 297 | for (FlowRule flowRule : flowRules) { |
| 298 | applyRule(flowRule); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 299 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 300 | } |
| 301 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 302 | private void applyRule(FlowRule flowRule) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 303 | Dpid dpid = Dpid.dpid(flowRule.deviceId().uri()); |
| 304 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 305 | |
Ray Milkey | 0ae473d | 2016-04-04 10:56:47 -0700 | [diff] [blame] | 306 | if (sw == null) { |
| 307 | return; |
| 308 | } |
| 309 | |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 310 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 311 | Optional.empty(), Optional.of(driverService)).buildFlowAdd()); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 312 | } |
| 313 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 314 | @Override |
| 315 | public void removeFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 316 | for (FlowRule flowRule : flowRules) { |
| 317 | removeRule(flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 318 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 319 | } |
| 320 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 321 | private void removeRule(FlowRule flowRule) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 322 | Dpid dpid = Dpid.dpid(flowRule.deviceId().uri()); |
| 323 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 324 | |
Ray Milkey | 0ae473d | 2016-04-04 10:56:47 -0700 | [diff] [blame] | 325 | if (sw == null) { |
| 326 | return; |
| 327 | } |
| 328 | |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 329 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 330 | Optional.empty(), Optional.of(driverService)).buildFlowDel()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 331 | } |
| 332 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 333 | @Override |
| 334 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 335 | // TODO: optimize using the ApplicationId |
| 336 | removeFlowRule(flowRules); |
| 337 | } |
| 338 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 339 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 340 | public void executeBatch(FlowRuleBatchOperation batch) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 341 | checkNotNull(batch); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 342 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 343 | Dpid dpid = Dpid.dpid(batch.deviceId().uri()); |
| 344 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 345 | |
| 346 | // If switch no longer exists, simply return. |
| 347 | if (sw == null) { |
| 348 | Set<FlowRule> failures = ImmutableSet.copyOf(Lists.transform(batch.getOperations(), e -> e.target())); |
| 349 | providerService.batchOperationCompleted(batch.id(), |
| 350 | new CompletedBatchOperation(false, failures, batch.deviceId())); |
| 351 | return; |
| 352 | } |
| 353 | pendingBatches.put(batch.id(), new InternalCacheEntry(batch)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 354 | OFFlowMod mod; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 355 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 356 | FlowModBuilder builder = |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 357 | FlowModBuilder.builder(fbe.target(), sw.factory(), |
| 358 | Optional.of(batch.id()), Optional.of(driverService)); |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 359 | switch (fbe.operator()) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 360 | case ADD: |
| 361 | mod = builder.buildFlowAdd(); |
| 362 | break; |
| 363 | case REMOVE: |
| 364 | mod = builder.buildFlowDel(); |
| 365 | break; |
| 366 | case MODIFY: |
| 367 | mod = builder.buildFlowMod(); |
| 368 | break; |
| 369 | default: |
| 370 | log.error("Unsupported batch operation {}; skipping flowmod {}", |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 371 | fbe.operator(), fbe); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 372 | continue; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 373 | } |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 374 | sw.sendMsg(mod); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 375 | } |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 376 | OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest() |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 377 | .setXid(batch.id()); |
| 378 | sw.sendMsg(builder.build()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 379 | } |
| 380 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 381 | private class InternalFlowProvider |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 382 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 383 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 384 | @Override |
| 385 | public void switchAdded(Dpid dpid) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 386 | createCollector(controller.getSwitch(dpid)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public void switchRemoved(Dpid dpid) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 391 | if (adaptiveFlowSampling) { |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 392 | stopCollectorIfNeeded(afsCollectors.remove(dpid)); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 393 | } else { |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 394 | stopCollectorIfNeeded(simpleCollectors.remove(dpid)); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 395 | } |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 396 | stopCollectorIfNeeded(tableStatsCollectors.remove(dpid)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Override |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 400 | public void switchChanged(Dpid dpid) { |
| 401 | } |
| 402 | |
| 403 | @Override |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 404 | public void portChanged(Dpid dpid, OFPortStatus status) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 405 | // TODO: Decide whether to evict flows internal store. |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void handleMessage(Dpid dpid, OFMessage msg) { |
Ray Milkey | ada9e2d | 2016-04-05 16:42:35 -0700 | [diff] [blame] | 410 | if (providerService == null) { |
| 411 | // We are shutting down, nothing to be done |
| 412 | return; |
| 413 | } |
Jonathan Hart | e4e74f0 | 2016-03-03 12:57:40 -0800 | [diff] [blame] | 414 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 415 | switch (msg.getType()) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 416 | case FLOW_REMOVED: |
| 417 | OFFlowRemoved removed = (OFFlowRemoved) msg; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 418 | |
Rodrigo Duarte Sousa | e37d129 | 2017-08-29 17:01:24 -0300 | [diff] [blame] | 419 | FlowEntry fr = new FlowEntryBuilder(deviceId, removed, getDriver(deviceId)).build(); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 420 | providerService.flowRemoved(fr); |
| 421 | break; |
| 422 | case STATS_REPLY: |
| 423 | if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) { |
Rodrigo Duarte Sousa | e37d129 | 2017-08-29 17:01:24 -0300 | [diff] [blame] | 424 | pushFlowMetrics(dpid, (OFFlowStatsReply) msg, getDriver(deviceId)); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 425 | } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.TABLE) { |
| 426 | pushTableStatistics(dpid, (OFTableStatsReply) msg); |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 427 | } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW_LIGHTWEIGHT) { |
| 428 | pushFlowLightWeightMetrics(dpid, (OFFlowLightweightStatsReply) msg); |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 429 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 430 | break; |
| 431 | case BARRIER_REPLY: |
| 432 | try { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 433 | InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 434 | if (entry != null) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 435 | providerService |
| 436 | .batchOperationCompleted(msg.getXid(), |
| 437 | entry.completed()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 438 | } else { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 439 | log.warn("Received unknown Barrier Reply: {}", |
| 440 | msg.getXid()); |
| 441 | } |
| 442 | } finally { |
| 443 | pendingBatches.invalidate(msg.getXid()); |
| 444 | } |
| 445 | break; |
| 446 | case ERROR: |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 447 | // TODO: This needs to get suppressed in a better way. |
| 448 | if (msg instanceof OFBadRequestErrorMsg && |
| 449 | ((OFBadRequestErrorMsg) msg).getCode() == OFBadRequestCode.BAD_TYPE) { |
| 450 | log.debug("Received error message {} from {}", msg, dpid); |
| 451 | } else { |
| 452 | log.warn("Received error message {} from {}", msg, dpid); |
| 453 | } |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 454 | handleErrorMsg(deviceId, msg); |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 455 | break; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 456 | default: |
| 457 | log.debug("Unhandled message type: {}", msg.getType()); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 458 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 461 | private void handleErrorMsg(DeviceId deviceId, OFMessage msg) { |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 462 | InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid()); |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 463 | OFErrorMsg error = (OFErrorMsg) msg; |
| 464 | OFMessage ofMessage = null; |
| 465 | switch (error.getErrType()) { |
| 466 | case BAD_ACTION: |
| 467 | OFBadActionErrorMsg baErrorMsg = (OFBadActionErrorMsg) error; |
| 468 | if (baErrorMsg.getData().getParsedMessage().isPresent()) { |
| 469 | ofMessage = baErrorMsg.getData().getParsedMessage().get(); |
| 470 | } |
| 471 | break; |
| 472 | case BAD_INSTRUCTION: |
| 473 | OFBadInstructionErrorMsg biErrorMsg = (OFBadInstructionErrorMsg) error; |
| 474 | if (biErrorMsg.getData().getParsedMessage().isPresent()) { |
| 475 | ofMessage = biErrorMsg.getData().getParsedMessage().get(); |
| 476 | } |
| 477 | break; |
| 478 | case BAD_MATCH: |
| 479 | OFBadMatchErrorMsg bmErrorMsg = (OFBadMatchErrorMsg) error; |
| 480 | if (bmErrorMsg.getData().getParsedMessage().isPresent()) { |
| 481 | ofMessage = bmErrorMsg.getData().getParsedMessage().get(); |
| 482 | } |
| 483 | break; |
| 484 | case FLOW_MOD_FAILED: |
| 485 | OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error; |
| 486 | if (fmFailed.getData().getParsedMessage().isPresent()) { |
| 487 | ofMessage = fmFailed.getData().getParsedMessage().get(); |
| 488 | } |
| 489 | break; |
| 490 | default: |
| 491 | // Do nothing. |
| 492 | return; |
| 493 | } |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 494 | |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 495 | if (ofMessage != null) { |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 496 | |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 497 | if (entry != null) { |
| 498 | OFFlowMod ofFlowMod = (OFFlowMod) ofMessage; |
Rodrigo Duarte Sousa | e37d129 | 2017-08-29 17:01:24 -0300 | [diff] [blame] | 499 | entry.appendFailure(new FlowEntryBuilder(deviceId, ofFlowMod, getDriver(deviceId)).build()); |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 500 | } else { |
| 501 | log.error("No matching batch for this error: {}", error); |
| 502 | } |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 503 | |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 504 | } else { |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 505 | |
| 506 | U64 cookieId = readCookieIdFromOFErrorMsg(error, msg.getVersion()); |
| 507 | |
| 508 | if (cookieId != null) { |
| 509 | long flowId = cookieId.getValue(); |
| 510 | |
| 511 | if (entry != null) { |
| 512 | for (FlowRuleBatchEntry fbEntry : entry.operation.getOperations()) { |
| 513 | if (fbEntry.target().id().value() == flowId) { |
| 514 | entry.appendFailure(fbEntry.target()); |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | } else { |
| 519 | log.error("No matching batch for this error: {}", error); |
| 520 | } |
| 521 | |
| 522 | } else { |
| 523 | log.error("Flow installation failed but switch " + |
| 524 | "didn't tell us which one."); |
| 525 | } |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 529 | /** |
| 530 | * Reading cookieId from OFErrorMsg. |
| 531 | * |
| 532 | * Loxigen OpenFlow API failed in parsing error messages because of |
| 533 | * 64 byte data truncation based on OpenFlow specs. The method written |
| 534 | * is a workaround to extract the cookieId from the packet till the |
| 535 | * issue is resolved in Loxigen OpenFlow code. |
| 536 | * Ref: https://groups.google.com/a/onosproject.org/forum/#!topic |
| 537 | * /onos-dev/_KwlHZDllLE |
| 538 | * |
| 539 | * @param msg OF error message |
| 540 | * @param ofVersion Openflow version |
| 541 | * @return cookieId |
| 542 | */ |
| 543 | private U64 readCookieIdFromOFErrorMsg(OFErrorMsg msg, |
| 544 | OFVersion ofVersion) { |
| 545 | |
| 546 | if (ofVersion.wireVersion < OFVersion.OF_13.wireVersion) { |
| 547 | log.debug("Unhandled error msg with OF version {} " + |
| 548 | "which is less than {}", |
| 549 | ofVersion, OFVersion.OF_13); |
| 550 | return null; |
| 551 | } |
| 552 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 553 | ByteBuf bb = Unpooled.wrappedBuffer(msg.getData().getData()); |
Prince Pereira | 141ed81 | 2016-09-02 19:03:18 +0530 | [diff] [blame] | 554 | |
| 555 | if (bb.readableBytes() < MIN_EXPECTED_BYTE_LEN) { |
| 556 | log.debug("Wrong length: Expected to be >= {}, was: {}", |
| 557 | MIN_EXPECTED_BYTE_LEN, bb.readableBytes()); |
| 558 | return null; |
| 559 | } |
| 560 | |
| 561 | byte ofVer = bb.readByte(); |
| 562 | |
| 563 | if (ofVer != ofVersion.wireVersion) { |
| 564 | log.debug("Wrong version: Expected={}, got={}", |
| 565 | ofVersion.wireVersion, ofVer); |
| 566 | return null; |
| 567 | } |
| 568 | |
| 569 | byte type = bb.readByte(); |
| 570 | |
| 571 | if (type != OFType.FLOW_MOD.ordinal()) { |
| 572 | log.debug("Wrong type: Expected={}, got={}", |
| 573 | OFType.FLOW_MOD.ordinal(), type); |
| 574 | return null; |
| 575 | } |
| 576 | |
| 577 | int length = U16.f(bb.readShort()); |
| 578 | |
| 579 | if (length < MIN_EXPECTED_BYTE_LEN) { |
| 580 | log.debug("Wrong length: Expected to be >= {}, was: {}", |
| 581 | MIN_EXPECTED_BYTE_LEN, length); |
| 582 | return null; |
| 583 | } |
| 584 | |
| 585 | bb.skipBytes(SKIP_BYTES); |
| 586 | return U64.ofRaw(bb.readLong()); |
| 587 | } |
| 588 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 589 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 590 | public void receivedRoleReply(Dpid dpid, RoleState requested, |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 591 | RoleState response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 592 | // Do nothing here for now. |
| 593 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 594 | |
Rodrigo Duarte Sousa | e37d129 | 2017-08-29 17:01:24 -0300 | [diff] [blame] | 595 | private DriverHandler getDriver(DeviceId devId) { |
| 596 | Driver driver = driverService.getDriver(devId); |
| 597 | DriverHandler handler = new DefaultDriverHandler(new DefaultDriverData(driver, devId)); |
| 598 | return handler; |
| 599 | } |
| 600 | |
| 601 | private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies, DriverHandler handler) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 602 | |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 603 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 604 | NewAdaptiveFlowStatsCollector afsc = afsCollectors.get(dpid); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 605 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 606 | if (adaptiveFlowSampling && afsc != null) { |
| 607 | List<FlowEntry> flowEntries = replies.getEntries().stream() |
Rodrigo Duarte Sousa | e37d129 | 2017-08-29 17:01:24 -0300 | [diff] [blame] | 608 | .map(entry -> new FlowEntryBuilder(did, entry, handler).withSetAfsc(afsc).build()) |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 609 | .collect(Collectors.toList()); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 610 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 611 | // Check that OFFlowStatsReply Xid is same with the one of OFFlowStatsRequest? |
| 612 | if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) { |
| 613 | log.debug("OpenFlowRuleProvider:pushFlowMetrics, flowMissingXid={}, " |
| 614 | + "OFFlowStatsReply Xid={}, for {}", |
| 615 | afsc.getFlowMissingXid(), replies.getXid(), dpid); |
| 616 | if (afsc.getFlowMissingXid() == replies.getXid()) { |
| 617 | // call entire flow stats update with flowMissing synchronization. |
| 618 | // used existing pushFlowMetrics |
| 619 | providerService.pushFlowMetrics(did, flowEntries); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 620 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 621 | // reset flowMissingXid to NO_FLOW_MISSING_XID |
| 622 | afsc.setFlowMissingXid(NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID); |
| 623 | } else { |
| 624 | // call individual flow stats update |
| 625 | providerService.pushFlowMetricsWithoutFlowMissing(did, flowEntries); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 626 | } |
| 627 | } else { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 628 | List<FlowEntry> flowEntries = replies.getEntries().stream() |
Rodrigo Duarte Sousa | e37d129 | 2017-08-29 17:01:24 -0300 | [diff] [blame] | 629 | .map(entry -> new FlowEntryBuilder(did, entry, handler).build()) |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 630 | .collect(Collectors.toList()); |
| 631 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 632 | // call existing entire flow stats update with flowMissing synchronization |
| 633 | providerService.pushFlowMetrics(did, flowEntries); |
| 634 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 635 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 636 | |
| 637 | private void pushTableStatistics(Dpid dpid, OFTableStatsReply replies) { |
| 638 | |
| 639 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
| 640 | List<TableStatisticsEntry> tableStatsEntries = replies.getEntries().stream() |
| 641 | .map(entry -> buildTableStatistics(did, entry)) |
| 642 | .filter(Objects::nonNull) |
| 643 | .collect(Collectors.toList()); |
| 644 | providerService.pushTableStatistics(did, tableStatsEntries); |
| 645 | } |
| 646 | |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 647 | private void pushFlowLightWeightMetrics(Dpid dpid, OFFlowLightweightStatsReply replies) { |
| 648 | |
| 649 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
| 650 | NewAdaptiveFlowStatsCollector afsc = afsCollectors.get(dpid); |
| 651 | if (adaptiveFlowSampling && afsc != null) { |
| 652 | List<FlowEntry> flowEntries = replies.getEntries().stream() |
| 653 | .map(entry -> new FlowEntryBuilder(did, entry, driverService).withSetAfsc(afsc).build()) |
| 654 | .collect(Collectors.toList()); |
| 655 | |
| 656 | // Check that OFFlowStatsReply Xid is same with the one of OFFlowStatsRequest? |
| 657 | if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) { |
| 658 | log.debug("OpenFlowRuleProvider:pushFlowMetrics, flowMissingXid={}, " |
| 659 | + "OFFlowStatsReply Xid={}, for {}", |
| 660 | afsc.getFlowMissingXid(), replies.getXid(), dpid); |
| 661 | if (afsc.getFlowMissingXid() == replies.getXid()) { |
| 662 | // call entire flow stats update with flowMissing synchronization. |
| 663 | // used existing pushFlowMetrics |
| 664 | providerService.pushFlowMetrics(did, flowEntries); |
| 665 | } |
| 666 | // reset flowMissingXid to NO_FLOW_MISSING_XID |
| 667 | afsc.setFlowMissingXid(NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID); |
| 668 | } else { |
| 669 | // call individual flow stats update |
| 670 | providerService.pushFlowMetricsWithoutFlowMissing(did, flowEntries); |
| 671 | } |
| 672 | } else { |
| 673 | List<FlowEntry> flowEntries = replies.getEntries().stream() |
| 674 | .map(entry -> new FlowEntryBuilder(did, entry, driverService).build()) |
| 675 | .collect(Collectors.toList()); |
| 676 | // call existing entire flow stats update with flowMissing synchronization |
| 677 | providerService.pushFlowMetrics(did, flowEntries); |
| 678 | } |
| 679 | } |
| 680 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 681 | private TableStatisticsEntry buildTableStatistics(DeviceId deviceId, |
| 682 | OFTableStatsEntry ofEntry) { |
| 683 | TableStatisticsEntry entry = null; |
| 684 | if (ofEntry != null) { |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 685 | IndexTableId tid = IndexTableId.of(ofEntry.getTableId().getValue()); |
| 686 | |
| 687 | try { |
| 688 | entry = DefaultTableStatisticsEntry.builder() |
| 689 | .withDeviceId(deviceId) |
| 690 | .withTableId(tid) |
| 691 | .withActiveFlowEntries(ofEntry.getActiveCount()) |
| 692 | .withPacketsLookedUpCount(ofEntry.getLookupCount().getValue()) |
| 693 | .withPacketsMatchedCount(ofEntry.getMatchedCount().getValue()) |
| 694 | .withMaxSize(ofEntry.getMaxEntries()).build(); |
| 695 | } catch (UnsupportedOperationException e) { |
| 696 | // The exception "UnsupportedOperationException" is thrown by "getMaxEntries()". |
| 697 | entry = DefaultTableStatisticsEntry.builder() |
| 698 | .withDeviceId(deviceId) |
| 699 | .withTableId(tid) |
| 700 | .withActiveFlowEntries(ofEntry.getActiveCount()) |
| 701 | .withPacketsLookedUpCount(ofEntry.getLookupCount().getValue()) |
| 702 | .withPacketsMatchedCount(ofEntry.getMatchedCount().getValue()).build(); |
| 703 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | return entry; |
| 707 | |
| 708 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 709 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 710 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 711 | /** |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 712 | * The internal cache entry holding the original request as well as |
| 713 | * accumulating the any failures along the way. |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 714 | * <p/> |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 715 | * If this entry is evicted from the cache then the entire operation is |
| 716 | * considered failed. Otherwise, only the failures reported by the device |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 717 | * will be propagated up. |
| 718 | */ |
| 719 | private class InternalCacheEntry { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 720 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 721 | private final FlowRuleBatchOperation operation; |
| 722 | private final Set<FlowRule> failures = Sets.newConcurrentHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 723 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 724 | public InternalCacheEntry(FlowRuleBatchOperation operation) { |
| 725 | this.operation = operation; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 728 | /** |
| 729 | * Appends a failed rule to the set of failed items. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 730 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 731 | * @param rule the failed rule |
| 732 | */ |
| 733 | public void appendFailure(FlowRule rule) { |
| 734 | failures.add(rule); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 737 | /** |
| 738 | * Fails the entire batch and returns the failed operation. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 739 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 740 | * @return the failed operation |
| 741 | */ |
| 742 | public CompletedBatchOperation failedCompletion() { |
| 743 | Set<FlowRule> fails = operation.getOperations().stream() |
| 744 | .map(op -> op.target()).collect(Collectors.toSet()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 745 | return new CompletedBatchOperation(false, |
| 746 | Collections |
| 747 | .unmodifiableSet(fails), |
| 748 | operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 751 | /** |
| 752 | * Returns the completed operation and whether the batch suceeded. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 753 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 754 | * @return the completed operation |
| 755 | */ |
| 756 | public CompletedBatchOperation completed() { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 757 | return new CompletedBatchOperation( |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 758 | failures.isEmpty(), |
| 759 | Collections |
| 760 | .unmodifiableSet(failures), |
| 761 | operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 762 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 763 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 764 | |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 765 | } |