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