Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow.impl; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ArrayListMultimap; |
| 19 | import com.google.common.collect.Iterables; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Multimap; |
| 23 | import com.google.common.collect.Sets; |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 24 | import org.onlab.util.PredictableExecutor; |
| 25 | import org.onlab.util.PredictableExecutor.PickyRunnable; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 26 | import org.onlab.util.Tools; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 27 | import org.onosproject.cfg.ComponentConfigService; |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 28 | import org.onosproject.cluster.ClusterService; |
| 29 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 31 | import org.onosproject.core.CoreService; |
| 32 | import org.onosproject.core.IdGenerator; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 33 | import org.onosproject.mastership.MastershipService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 32a9c0b | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 36 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 37 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceEvent; |
| 39 | import org.onosproject.net.device.DeviceListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.device.DeviceService; |
Jordan Halterman | 4f3193a | 2017-07-11 20:32:49 -0700 | [diff] [blame] | 41 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.CompletedBatchOperation; |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 43 | import org.onosproject.net.flow.DefaultFlowEntry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.FlowEntry; |
| 45 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.flow.FlowRuleEvent; |
| 47 | import org.onosproject.net.flow.FlowRuleListener; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 48 | import org.onosproject.net.flow.FlowRuleOperation; |
| 49 | import org.onosproject.net.flow.FlowRuleOperations; |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.FlowRuleProgrammable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flow.FlowRuleProvider; |
| 52 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 53 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 54 | import org.onosproject.net.flow.FlowRuleService; |
| 55 | import org.onosproject.net.flow.FlowRuleStore; |
| 56 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 57 | import org.onosproject.net.flow.TableStatisticsEntry; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 58 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 59 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent; |
| 60 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
| 61 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 62 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | import org.onosproject.net.provider.AbstractProviderService; |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 64 | import org.onosproject.net.provider.ProviderId; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 65 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 66 | import org.osgi.service.component.annotations.Activate; |
| 67 | import org.osgi.service.component.annotations.Component; |
| 68 | import org.osgi.service.component.annotations.Deactivate; |
| 69 | import org.osgi.service.component.annotations.Modified; |
| 70 | import org.osgi.service.component.annotations.Reference; |
| 71 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 72 | import org.slf4j.Logger; |
| 73 | |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 74 | import java.util.Collections; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 75 | import java.util.Dictionary; |
Sho SHIMIZU | b837cb7 | 2016-08-30 14:44:50 -0700 | [diff] [blame] | 76 | import java.util.HashSet; |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 77 | import java.util.List; |
| 78 | import java.util.Map; |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 79 | import java.util.Objects; |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 80 | import java.util.Optional; |
pierventre | 55c6f33 | 2022-03-29 15:10:39 +0200 | [diff] [blame^] | 81 | import java.util.Random; |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 82 | import java.util.Set; |
| 83 | import java.util.concurrent.ConcurrentHashMap; |
| 84 | import java.util.concurrent.ExecutorService; |
| 85 | import java.util.concurrent.Executors; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 86 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 87 | import static com.google.common.base.Preconditions.checkNotNull; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 88 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 89 | import static org.onlab.util.Tools.get; |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 90 | import static org.onlab.util.Tools.groupedThreads; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 91 | import static org.onosproject.net.OsgiPropertyConstants.ALLOW_EXTRANEOUS_RULES; |
| 92 | import static org.onosproject.net.OsgiPropertyConstants.ALLOW_EXTRANEOUS_RULES_DEFAULT; |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 93 | import static org.onosproject.net.OsgiPropertyConstants.IMPORT_EXTRANEOUS_RULES; |
| 94 | import static org.onosproject.net.OsgiPropertyConstants.IMPORT_EXTRANEOUS_RULES_DEFAULT; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 95 | import static org.onosproject.net.OsgiPropertyConstants.POLL_FREQUENCY; |
| 96 | import static org.onosproject.net.OsgiPropertyConstants.POLL_FREQUENCY_DEFAULT; |
| 97 | import static org.onosproject.net.OsgiPropertyConstants.PURGE_ON_DISCONNECTION; |
| 98 | import static org.onosproject.net.OsgiPropertyConstants.PURGE_ON_DISCONNECTION_DEFAULT; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 99 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED; |
| 100 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 101 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 102 | import static org.onosproject.security.AppPermission.Type.FLOWRULE_READ; |
| 103 | import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 104 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 105 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Provides implementation of the flow NB & SB APIs. |
| 108 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 109 | @Component( |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 110 | immediate = true, |
| 111 | service = { |
| 112 | FlowRuleService.class, |
| 113 | FlowRuleProviderRegistry.class |
| 114 | }, |
| 115 | property = { |
| 116 | ALLOW_EXTRANEOUS_RULES + ":Boolean=" + ALLOW_EXTRANEOUS_RULES_DEFAULT, |
| 117 | IMPORT_EXTRANEOUS_RULES + ":Boolean=" + IMPORT_EXTRANEOUS_RULES_DEFAULT, |
| 118 | PURGE_ON_DISCONNECTION + ":Boolean=" + PURGE_ON_DISCONNECTION_DEFAULT, |
| 119 | POLL_FREQUENCY + ":Integer=" + POLL_FREQUENCY_DEFAULT |
| 120 | } |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 121 | ) |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 122 | public class FlowRuleManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 123 | extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener, |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 124 | FlowRuleProvider, FlowRuleProviderService> |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 125 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 126 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 127 | private final Logger log = getLogger(getClass()); |
| 128 | |
Thomas Vachuska | 227943d | 2018-03-19 09:37:28 -0700 | [diff] [blame] | 129 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 130 | private static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 131 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 132 | /** Allow flow rules in switch not installed by ONOS. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 133 | private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES_DEFAULT; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 134 | |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 135 | /** Allow to import flow rules in switch not installed by ONOS. */ |
| 136 | private boolean importExtraneousRules = IMPORT_EXTRANEOUS_RULES_DEFAULT; |
| 137 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 138 | /** Purge entries associated with a device when the device goes offline. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 139 | private boolean purgeOnDisconnection = PURGE_ON_DISCONNECTION_DEFAULT; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 140 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 141 | /** Frequency (in seconds) for polling flow statistics via fallback provider. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 142 | private int fallbackFlowPollFrequency = POLL_FREQUENCY_DEFAULT; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 143 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 144 | private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 145 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 146 | |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 147 | private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider(); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 148 | |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 149 | protected ExecutorService deviceInstallers = Executors.newFixedThreadPool(32, |
| 150 | groupedThreads("onos/flowservice", "device-installer-%d", log)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 151 | |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 152 | protected ExecutorService operationsService = new PredictableExecutor(32, |
| 153 | groupedThreads("onos/flowservice", "operations-%d", log)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 154 | |
| 155 | private IdGenerator idGenerator; |
| 156 | |
Sho SHIMIZU | 9f95074 | 2016-09-01 09:55:38 -0700 | [diff] [blame] | 157 | private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 158 | |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 159 | private NodeId local; |
| 160 | |
pierventre | 55c6f33 | 2022-03-29 15:10:39 +0200 | [diff] [blame^] | 161 | private Random randomGenerator = new Random(); |
| 162 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 163 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 164 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 165 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 166 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 167 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 168 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 169 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 170 | protected CoreService coreService; |
| 171 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 172 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 173 | protected MastershipService mastershipService; |
| 174 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 175 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 176 | protected ComponentConfigService cfgService; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 177 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 178 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 4f3193a | 2017-07-11 20:32:49 -0700 | [diff] [blame] | 179 | protected DriverService driverService; |
| 180 | |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 181 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 182 | protected ClusterService clusterService; |
| 183 | |
Andrea Campanella | 32a9c0b | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 184 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 185 | protected NetworkConfigRegistry netCfgService; |
| 186 | |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 187 | @Activate |
| 188 | public void activate(ComponentContext context) { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 189 | store.setDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 190 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 191 | deviceService.addListener(deviceListener); |
| 192 | cfgService.registerProperties(getClass()); |
pier | ed2a0a4 | 2019-04-16 11:48:08 +0200 | [diff] [blame] | 193 | modified(context); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 194 | idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 195 | local = clusterService.getLocalNode().id(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 196 | log.info("Started"); |
| 197 | } |
| 198 | |
| 199 | @Deactivate |
| 200 | public void deactivate() { |
Andrea Campanella | 5a3c09c | 2017-12-01 13:57:48 +0100 | [diff] [blame] | 201 | driverProvider.terminate(); |
Andrea Campanella | 3f1c61e | 2016-04-01 17:30:12 -0700 | [diff] [blame] | 202 | deviceService.removeListener(deviceListener); |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 203 | cfgService.unregisterProperties(getClass(), false); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 204 | deviceInstallers.shutdownNow(); |
| 205 | operationsService.shutdownNow(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 206 | store.unsetDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 207 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 208 | log.info("Stopped"); |
| 209 | } |
| 210 | |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 211 | @Modified |
| 212 | public void modified(ComponentContext context) { |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 213 | if (context != null) { |
| 214 | readComponentConfiguration(context); |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 215 | } |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 216 | driverProvider.init(new InternalFlowRuleProviderService(driverProvider), |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 217 | deviceService, mastershipService, fallbackFlowPollFrequency); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
| 221 | protected FlowRuleProvider defaultProvider() { |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 222 | return driverProvider; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 223 | } |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 224 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 225 | /** |
| 226 | * Extracts properties from the component configuration context. |
| 227 | * |
| 228 | * @param context the component context |
| 229 | */ |
| 230 | private void readComponentConfiguration(ComponentContext context) { |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 231 | Dictionary<?, ?> properties = context.getProperties(); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 232 | Boolean flag; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 233 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 234 | flag = Tools.isPropertyEnabled(properties, ALLOW_EXTRANEOUS_RULES); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 235 | if (flag == null) { |
| 236 | log.info("AllowExtraneousRules is not configured, " + |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 237 | "using current value of {}", allowExtraneousRules); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 238 | } else { |
| 239 | allowExtraneousRules = flag; |
| 240 | log.info("Configured. AllowExtraneousRules is {}", |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 241 | allowExtraneousRules ? "enabled" : "disabled"); |
| 242 | } |
| 243 | |
| 244 | flag = Tools.isPropertyEnabled(properties, IMPORT_EXTRANEOUS_RULES); |
| 245 | if (flag == null) { |
| 246 | log.info("ImportExtraneousRules is not configured, " + |
| 247 | "using current value of {}", importExtraneousRules); |
| 248 | } else { |
| 249 | importExtraneousRules = flag; |
| 250 | log.info("Configured. importExtraneousRules is {}", |
| 251 | importExtraneousRules ? "enabled" : "disabled"); |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 252 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 253 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 254 | flag = Tools.isPropertyEnabled(properties, PURGE_ON_DISCONNECTION); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 255 | if (flag == null) { |
| 256 | log.info("PurgeOnDisconnection is not configured, " + |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 257 | "using current value of {}", purgeOnDisconnection); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 258 | } else { |
| 259 | purgeOnDisconnection = flag; |
| 260 | log.info("Configured. PurgeOnDisconnection is {}", |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 261 | purgeOnDisconnection ? "enabled" : "disabled"); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 262 | } |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 263 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 264 | String s = get(properties, POLL_FREQUENCY); |
Carmelo Cascone | e6a9754 | 2018-06-20 16:48:00 +0200 | [diff] [blame] | 265 | if (isNullOrEmpty(s)) { |
| 266 | log.info("fallbackFlowPollFrequency is not configured, " + |
| 267 | "using current value of {} seconds", |
| 268 | fallbackFlowPollFrequency); |
| 269 | } else { |
| 270 | try { |
| 271 | fallbackFlowPollFrequency = Integer.parseInt(s); |
| 272 | log.info("Configured. FallbackFlowPollFrequency is {} seconds", |
| 273 | fallbackFlowPollFrequency); |
| 274 | } catch (NumberFormatException e) { |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 275 | log.warn("Configured fallbackFlowPollFrequency value " + |
Carmelo Cascone | e6a9754 | 2018-06-20 16:48:00 +0200 | [diff] [blame] | 276 | "is not a number, using current value of {} seconds", |
| 277 | fallbackFlowPollFrequency); |
| 278 | } |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 279 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 280 | } |
| 281 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 282 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 283 | public int getFlowRuleCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 284 | checkPermission(FLOWRULE_READ); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 285 | return store.getFlowRuleCount(); |
| 286 | } |
| 287 | |
| 288 | @Override |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 289 | public int getFlowRuleCount(DeviceId deviceId) { |
| 290 | checkPermission(FLOWRULE_READ); |
Thomas Vachuska | 227943d | 2018-03-19 09:37:28 -0700 | [diff] [blame] | 291 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 292 | return store.getFlowRuleCount(deviceId); |
| 293 | } |
| 294 | |
| 295 | @Override |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 296 | public int getFlowRuleCount(DeviceId deviceId, FlowEntry.FlowEntryState state) { |
| 297 | checkPermission(FLOWRULE_READ); |
| 298 | checkNotNull(deviceId, "Device ID cannot be null"); |
| 299 | return store.getFlowRuleCount(deviceId, state); |
| 300 | } |
| 301 | |
| 302 | @Override |
David Glantz | 5c7fe3a | 2021-09-22 14:34:14 -0500 | [diff] [blame] | 303 | public FlowEntry getFlowEntry(FlowRule rule) { |
| 304 | checkPermission(FLOWRULE_READ); |
| 305 | checkNotNull(rule, FLOW_RULE_NULL); |
| 306 | return store.getFlowEntry(rule); |
| 307 | } |
| 308 | |
| 309 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 310 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 311 | checkPermission(FLOWRULE_READ); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 312 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 313 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 317 | public void applyFlowRules(FlowRule... flowRules) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 318 | checkPermission(FLOWRULE_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 319 | |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 320 | apply(buildFlowRuleOperations(true, null, flowRules)); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Override |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 324 | public void purgeFlowRules(DeviceId deviceId) { |
| 325 | checkPermission(FLOWRULE_WRITE); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 326 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 327 | store.purgeFlowRule(deviceId); |
| 328 | } |
| 329 | |
| 330 | @Override |
Daniele Moro | cfd7740 | 2021-07-15 17:02:59 +0200 | [diff] [blame] | 331 | public void purgeFlowRules(DeviceId deviceId, ApplicationId appId) { |
| 332 | checkPermission(FLOWRULE_WRITE); |
| 333 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 334 | checkNotNull(appId, "Application ID cannot be null!"); |
| 335 | |
| 336 | store.purgeFlowRules(deviceId, appId); |
| 337 | } |
| 338 | |
| 339 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 340 | public void removeFlowRules(FlowRule... flowRules) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 341 | checkPermission(FLOWRULE_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 342 | |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 343 | apply(buildFlowRuleOperations(false, null, flowRules)); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 344 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 345 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 346 | @Override |
| 347 | public void removeFlowRulesById(ApplicationId id) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 348 | checkPermission(FLOWRULE_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 349 | |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 350 | Set<FlowRule> flowEntries = Sets.newHashSet(); |
| 351 | for (Device d : deviceService.getDevices()) { |
| 352 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 353 | if (flowEntry.appId() == id.id()) { |
| 354 | flowEntries.add(flowEntry); |
| 355 | } |
| 356 | } |
| 357 | } |
Ray Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame] | 358 | removeFlowRules(Iterables.toArray(flowEntries, FlowRule.class)); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | @Override |
Bharath Thiruveedula | 99849dc | 2016-11-17 22:04:38 +0530 | [diff] [blame] | 362 | public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) { |
| 363 | checkPermission(FLOWRULE_READ); |
| 364 | |
| 365 | Set<FlowEntry> flowEntries = Sets.newHashSet(); |
| 366 | for (Device d : deviceService.getDevices()) { |
| 367 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 368 | if (flowEntry.appId() == id.id()) { |
| 369 | flowEntries.add(flowEntry); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | return flowEntries; |
| 374 | } |
| 375 | |
| 376 | @Override |
alshabib | aa7e7de | 2014-11-12 19:20:44 -0800 | [diff] [blame] | 377 | public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 378 | checkPermission(FLOWRULE_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 379 | |
alshabib | aa7e7de | 2014-11-12 19:20:44 -0800 | [diff] [blame] | 380 | Set<FlowRule> matches = Sets.newHashSet(); |
| 381 | long toLookUp = ((long) appId.id() << 16) | groupId; |
| 382 | for (Device d : deviceService.getDevices()) { |
| 383 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 384 | if ((flowEntry.id().value() >>> 32) == toLookUp) { |
| 385 | matches.add(flowEntry); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | return matches; |
| 390 | } |
| 391 | |
| 392 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 393 | public void apply(FlowRuleOperations ops) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 394 | checkPermission(FLOWRULE_WRITE); |
pierventre | 55c6f33 | 2022-03-29 15:10:39 +0200 | [diff] [blame^] | 395 | if (ops.stripeKey().isEmpty()) { |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 396 | // Null means that we don't care about the in-order processing |
| 397 | // this approach maximizes the throughput but it can introduce |
| 398 | // consistency issues as the original order between conflictual |
| 399 | // writes is not maintained. If conflictual writes can be easily |
| 400 | // handled using different stages, this is the approach to use. |
| 401 | operationsService.execute(new FlowOperationsProcessor(ops)); |
| 402 | } else { |
| 403 | // Following approach is suggested when it is hard to handle |
| 404 | // conflictual writes in the same FlowRuleOperations object. Apps |
| 405 | // may know there are conflictual writes but it could be hard to |
| 406 | // encapsulate them in the same object using different stages (above |
| 407 | // all if they are stimulated by different events). In this case, |
| 408 | // the probabilistic accumulation may help but it is brittle and based |
| 409 | // on the probability that a given event happens in a specific time. |
| 410 | // For this reason we have introduced PredictableFlowOperationsProcessor |
| 411 | // which uses the striped key (provided by the apps) to serialize the ops |
| 412 | // on the same executor. |
| 413 | operationsService.execute(new PredictableFlowOperationsProcessor(ops)); |
| 414 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 418 | protected FlowRuleProviderService createProviderService( |
| 419 | FlowRuleProvider provider) { |
| 420 | return new InternalFlowRuleProviderService(provider); |
| 421 | } |
| 422 | |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 423 | @Override |
| 424 | protected synchronized FlowRuleProvider getProvider(ProviderId pid) { |
| 425 | log.warn("should not be calling getProvider(ProviderId)"); |
| 426 | return super.getProvider(pid); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * {@inheritDoc} |
| 431 | * if the Device does not support {@link FlowRuleProgrammable}. |
| 432 | */ |
| 433 | @Override |
| 434 | protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) { |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 435 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 436 | // if device supports FlowRuleProgrammable, |
| 437 | // use FlowRuleProgrammable via FlowRuleDriverProvider |
| 438 | return Optional.ofNullable(deviceService.getDevice(deviceId)) |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 439 | .filter(dev -> dev.is(FlowRuleProgrammable.class)) |
| 440 | .<FlowRuleProvider>map(x -> driverProvider) |
| 441 | .orElseGet(() -> super.getProvider(deviceId)); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 442 | } |
| 443 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 444 | private class InternalFlowRuleProviderService |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 445 | extends AbstractProviderService<FlowRuleProvider> |
| 446 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 447 | |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 448 | final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 449 | final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap(); |
| 450 | |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 451 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 452 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 453 | super(provider); |
| 454 | } |
| 455 | |
| 456 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 457 | public void flowRemoved(FlowEntry flowEntry) { |
| 458 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 459 | checkValidity(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 460 | lastSeen.remove(flowEntry); |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 461 | firstSeen.remove(flowEntry); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 462 | FlowEntry stored = store.getFlowEntry(flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 463 | if (stored == null) { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 464 | log.debug("Rule already evicted from store: {}", flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 465 | return; |
| 466 | } |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 467 | if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) { |
| 468 | ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED); |
| 469 | } |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 470 | FlowRuleProvider frp = getProvider(flowEntry.deviceId()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 471 | FlowRuleEvent event = null; |
| 472 | switch (stored.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 473 | case ADDED: |
| 474 | case PENDING_ADD: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 475 | frp.applyFlowRule(stored); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 476 | break; |
| 477 | case PENDING_REMOVE: |
| 478 | case REMOVED: |
| 479 | event = store.removeFlowRule(stored); |
| 480 | break; |
| 481 | default: |
| 482 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 483 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 484 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 485 | if (event != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 486 | log.debug("Flow {} removed", flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 487 | post(event); |
| 488 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 489 | } |
| 490 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 491 | |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 492 | private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 493 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 494 | checkValidity(); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 495 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 496 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 497 | switch (flowRule.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 498 | case PENDING_REMOVE: |
| 499 | case REMOVED: |
| 500 | event = store.removeFlowRule(flowRule); |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 501 | log.debug("Flow {} removed", flowRule); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 502 | break; |
| 503 | case ADDED: |
| 504 | case PENDING_ADD: |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 505 | event = store.pendingFlowRule(flowRule); |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 506 | if (isFlowOnlyInStore) { |
| 507 | // Publishing RULE_ADD_REQUESTED event facilitates |
| 508 | // preparation of statistics for the concerned rule |
| 509 | if (event == null) { |
| 510 | event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule); |
| 511 | } |
| 512 | } |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 513 | try { |
| 514 | frp.applyFlowRule(flowRule); |
| 515 | } catch (UnsupportedOperationException e) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 516 | log.warn("Unsupported operation", e); |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 517 | if (flowRule instanceof DefaultFlowEntry) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 518 | //FIXME modification of "stored" flow entry outside of store |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 519 | ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED); |
| 520 | } |
| 521 | } |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 522 | break; |
| 523 | default: |
| 524 | log.debug("Flow {} has not been installed.", flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 525 | } |
| 526 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 527 | if (event != null) { |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 528 | post(event); |
| 529 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 530 | } |
| 531 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 532 | private void extraneousFlow(FlowRule flowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 533 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 534 | checkValidity(); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 535 | // getProvider is customized to favor driverProvider |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 536 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
| 537 | frp.removeFlowRule(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 538 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Andrea Campanella | 321f75a | 2021-12-14 14:48:54 +0100 | [diff] [blame] | 541 | private boolean handleExistingFlow(FlowEntry flowEntry) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 542 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 543 | checkValidity(); |
Andrea Campanella | 321f75a | 2021-12-14 14:48:54 +0100 | [diff] [blame] | 544 | FlowEntry storedEntry = store.getFlowEntry(flowEntry); |
pierventre | bb2a72c | 2022-01-04 19:08:46 +0100 | [diff] [blame] | 545 | if (storedEntry != null) { |
| 546 | // Flow rule is still valid, let's try to update the stats |
| 547 | if (storedEntry.state() != FlowEntry.FlowEntryState.PENDING_REMOVE && |
| 548 | checkRuleLiveness(flowEntry, storedEntry)) { |
pierventre | 2ad220f | 2022-02-18 15:12:33 -0800 | [diff] [blame] | 549 | if (!shouldHandle(flowEntry.deviceId())) { |
| 550 | return false; |
| 551 | } |
pierventre | bb2a72c | 2022-01-04 19:08:46 +0100 | [diff] [blame] | 552 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
pierventre | 965aa84 | 2022-03-21 19:15:40 +0100 | [diff] [blame] | 553 | // Something went wrong or there is no master or the device |
| 554 | // is not available better check if it is the latter cases |
pierventre | bb2a72c | 2022-01-04 19:08:46 +0100 | [diff] [blame] | 555 | if (event == null) { |
| 556 | log.debug("No flow store event generated for addOrUpdate of {}", flowEntry); |
| 557 | return false; |
| 558 | } else { |
| 559 | log.trace("Flow {} {}", flowEntry, event.type()); |
| 560 | post(event); |
| 561 | } |
pierventre | 965aa84 | 2022-03-21 19:15:40 +0100 | [diff] [blame] | 562 | } else if (storedEntry.state() == FlowEntry.FlowEntryState.PENDING_REMOVE) { |
| 563 | // Store is already in sync, let's re-issue flow removal only |
| 564 | log.debug("Removing {} from the device", flowEntry); |
| 565 | FlowRuleProvider frp = getProvider(flowEntry.deviceId()); |
| 566 | frp.removeFlowRule(flowEntry); |
| 567 | } else if (!checkRuleLiveness(flowEntry, storedEntry)) { |
| 568 | // Update store first as the flow entry is expired. Then, |
| 569 | // as consequence of this a flow removal will be sent. |
pierventre | bb2a72c | 2022-01-04 19:08:46 +0100 | [diff] [blame] | 570 | log.debug("Removing {}", flowEntry); |
| 571 | removeFlowRules(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 572 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 573 | } else { |
pierventre | 2ad220f | 2022-02-18 15:12:33 -0800 | [diff] [blame] | 574 | // It was already removed or there is no master |
| 575 | // better check if it is the latter |
pierventre | bb2a72c | 2022-01-04 19:08:46 +0100 | [diff] [blame] | 576 | return false; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 577 | } |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 578 | return true; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 579 | } |
| 580 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 581 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 582 | if (storedRule == null) { |
| 583 | return false; |
| 584 | } |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 585 | if (storedRule.isPermanent()) { |
| 586 | return true; |
| 587 | } |
| 588 | |
Ray Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 589 | final long timeout = storedRule.timeout() * 1000L; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 590 | final long currentTime = System.currentTimeMillis(); |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 591 | |
| 592 | // Checking flow with hardTimeout |
| 593 | if (storedRule.hardTimeout() != 0) { |
| 594 | if (!firstSeen.containsKey(storedRule)) { |
| 595 | // First time rule adding |
| 596 | firstSeen.put(storedRule, currentTime); |
| 597 | } else { |
| 598 | Long first = firstSeen.get(storedRule); |
Ray Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 599 | final long hardTimeout = storedRule.hardTimeout() * 1000L; |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 600 | if ((currentTime - first) > hardTimeout) { |
| 601 | return false; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
Gamze Abaka | 8d03f18 | 2017-07-20 15:09:24 +0000 | [diff] [blame] | 606 | if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 607 | lastSeen.put(storedRule, currentTime); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 608 | return true; |
| 609 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 610 | if (!lastSeen.containsKey(storedRule)) { |
| 611 | // checking for the first time |
| 612 | lastSeen.put(storedRule, storedRule.lastSeen()); |
| 613 | // Use following if lastSeen attr. was removed. |
| 614 | //lastSeen.put(storedRule, currentTime); |
| 615 | } |
| 616 | Long last = lastSeen.get(storedRule); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 617 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 618 | // concurrently removed? let the liveness check fail |
| 619 | return last != null && (currentTime - last) <= timeout; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 620 | } |
| 621 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 622 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 623 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 624 | pushFlowMetricsInternal(deviceId, flowEntries, true); |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
| 629 | pushFlowMetricsInternal(deviceId, flowEntries, false); |
| 630 | } |
| 631 | |
pierventre | 2ad220f | 2022-02-18 15:12:33 -0800 | [diff] [blame] | 632 | private boolean shouldHandle(DeviceId deviceId) { |
| 633 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 634 | return Objects.equals(local, master) && deviceService.isAvailable(deviceId); |
| 635 | } |
| 636 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 637 | private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries, |
| 638 | boolean useMissingFlow) { |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 639 | Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap(); |
| 640 | store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f)); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 641 | NodeId master; |
| 642 | boolean done; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 643 | |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 644 | // Processing flow rules |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 645 | for (FlowEntry rule : flowEntries) { |
| 646 | try { |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 647 | FlowEntry storedRule = storedRules.remove(rule); |
| 648 | if (storedRule != null) { |
| 649 | if (storedRule.exactMatch(rule)) { |
| 650 | // we both have the rule, let's update some info then. |
Andrea Campanella | 321f75a | 2021-12-14 14:48:54 +0100 | [diff] [blame] | 651 | done = handleExistingFlow(rule); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 652 | if (!done) { |
| 653 | // Mastership change can occur during this iteration |
pierventre | 2ad220f | 2022-02-18 15:12:33 -0800 | [diff] [blame] | 654 | if (!shouldHandle(deviceId)) { |
| 655 | log.warn("Tried to update the flow stats while the node was not the master" + |
| 656 | " or the device {} was not available", deviceId); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 657 | return; |
| 658 | } |
| 659 | } |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 660 | } else { |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 661 | // Mastership change can occur during this iteration |
pierventre | 2ad220f | 2022-02-18 15:12:33 -0800 | [diff] [blame] | 662 | if (!shouldHandle(deviceId)) { |
| 663 | log.warn("Tried to update the flows while the node was not the master" + |
| 664 | " or the device {} was not available", deviceId); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 665 | return; |
| 666 | } |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 667 | // the two rules are not an exact match - remove the |
| 668 | // switch's rule and install our rule |
| 669 | extraneousFlow(rule); |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 670 | flowMissing(storedRule, false); |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 671 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 672 | } else { |
| 673 | // the device has a rule the store does not have |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 674 | if (!allowExtraneousRules) { |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 675 | // Mastership change can occur during this iteration |
pierventre | 2ad220f | 2022-02-18 15:12:33 -0800 | [diff] [blame] | 676 | if (!shouldHandle(deviceId)) { |
| 677 | log.warn("Tried to remove flows while the node was not the master" + |
| 678 | " or the device {} was not available", deviceId); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 679 | return; |
| 680 | } |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 681 | extraneousFlow(rule); |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 682 | } else if (importExtraneousRules) { // Stores the rule, if so is indicated |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 683 | FlowRuleEvent flowRuleEvent = store.addOrUpdateFlowRule(rule); |
| 684 | if (flowRuleEvent == null) { |
| 685 | // Mastership change can occur during this iteration |
pierventre | 2ad220f | 2022-02-18 15:12:33 -0800 | [diff] [blame] | 686 | if (!shouldHandle(deviceId)) { |
| 687 | log.warn("Tried to import flows while the node was not the master" + |
| 688 | " or the device {} was not available", deviceId); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 689 | return; |
| 690 | } |
| 691 | } |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 692 | } |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 693 | } |
Sho SHIMIZU | 24a00d9 | 2015-05-05 11:11:13 -0700 | [diff] [blame] | 694 | } catch (Exception e) { |
Jon Hall | 274cecb | 2017-08-09 12:15:48 -0700 | [diff] [blame] | 695 | log.warn("Can't process added or extra rule {} for device {}:{}", |
| 696 | rule, deviceId, e); |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 697 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 698 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 699 | |
| 700 | // DO NOT reinstall |
| 701 | if (useMissingFlow) { |
| 702 | for (FlowEntry rule : storedRules.keySet()) { |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 703 | // Mastership change can occur during this iteration |
pierventre | 2ad220f | 2022-02-18 15:12:33 -0800 | [diff] [blame] | 704 | if (!shouldHandle(deviceId)) { |
| 705 | log.warn("Tried to install missing rules while the node was not the master" + |
| 706 | " or the device {} was not available", deviceId); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 707 | return; |
| 708 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 709 | try { |
| 710 | // there are rules in the store that aren't on the switch |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 711 | log.debug("Adding the rule that is present in store but not on switch : {}", rule); |
| 712 | flowMissing(rule, true); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 713 | } catch (Exception e) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 714 | log.warn("Can't add missing flow rule:", e); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 715 | } |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 716 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 717 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 718 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 719 | |
| 720 | @Override |
| 721 | public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) { |
| 722 | store.batchOperationComplete(FlowRuleBatchEvent.completed( |
| 723 | new FlowRuleBatchRequest(batchId, Collections.emptySet()), |
| 724 | operation |
| 725 | )); |
| 726 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 727 | |
| 728 | @Override |
| 729 | public void pushTableStatistics(DeviceId deviceId, |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 730 | List<TableStatisticsEntry> tableStats) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 731 | store.updateTableStatistics(deviceId, tableStats); |
| 732 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 733 | } |
| 734 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 735 | // Store delegate to re-post events emitted from the store. |
| 736 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 737 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 738 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 739 | // TODO: Right now we only dispatch events at individual flowEntry level. |
| 740 | // It may be more efficient for also dispatch events as a batch. |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 741 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 742 | public void notify(FlowRuleBatchEvent event) { |
| 743 | final FlowRuleBatchRequest request = event.subject(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 744 | switch (event.type()) { |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 745 | case BATCH_OPERATION_REQUESTED: |
| 746 | // Request has been forwarded to MASTER Node, and was |
| 747 | request.ops().forEach( |
| 748 | op -> { |
| 749 | switch (op.operator()) { |
| 750 | case ADD: |
| 751 | post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target())); |
| 752 | break; |
| 753 | case REMOVE: |
| 754 | post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target())); |
| 755 | break; |
| 756 | case MODIFY: |
| 757 | //TODO: do something here when the time comes. |
| 758 | break; |
| 759 | default: |
| 760 | log.warn("Unknown flow operation operator: {}", op.operator()); |
| 761 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 762 | } |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 763 | ); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 764 | |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 765 | DeviceId deviceId = event.deviceId(); |
| 766 | FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId); |
| 767 | // getProvider is customized to favor driverProvider |
| 768 | FlowRuleProvider flowRuleProvider = getProvider(deviceId); |
| 769 | if (flowRuleProvider != null) { |
| 770 | log.trace("Sending {} flow rules to {}", batchOperation.size(), deviceId); |
| 771 | flowRuleProvider.executeBatch(batchOperation); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 772 | } |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 773 | |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 774 | break; |
| 775 | |
| 776 | case BATCH_OPERATION_COMPLETED: |
| 777 | // Operation completed, let's retrieve the processor and trigger the callback |
| 778 | FlowOperationsProcessor fops = pendingFlowOperations.remove( |
| 779 | event.subject().batchId()); |
| 780 | if (fops != null) { |
| 781 | if (event.result().isSuccess()) { |
| 782 | fops.satisfy(event.deviceId()); |
| 783 | } else { |
| 784 | fops.fail(event.deviceId(), event.result().failedItems()); |
| 785 | } |
| 786 | } else { |
| 787 | log.warn("Unable to find flow operations processor for batch: {}", event.subject().batchId()); |
| 788 | } |
| 789 | break; |
| 790 | |
| 791 | default: |
| 792 | break; |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 793 | } |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 794 | } |
| 795 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 796 | |
Sho SHIMIZU | 5711ce1 | 2016-08-31 13:57:12 -0700 | [diff] [blame] | 797 | private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) { |
| 798 | switch (input) { |
| 799 | case ADD: |
| 800 | return FlowRuleBatchEntry.FlowRuleOperation.ADD; |
| 801 | case MODIFY: |
| 802 | return FlowRuleBatchEntry.FlowRuleOperation.MODIFY; |
| 803 | case REMOVE: |
| 804 | return FlowRuleBatchEntry.FlowRuleOperation.REMOVE; |
| 805 | default: |
| 806 | throw new UnsupportedOperationException("Unknown flow rule type " + input); |
| 807 | } |
| 808 | } |
| 809 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 810 | private class FlowOperationsProcessor implements Runnable { |
Sho SHIMIZU | 5f70942 | 2016-09-07 09:54:46 -0700 | [diff] [blame] | 811 | // Immutable |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 812 | protected final FlowRuleOperations fops; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 813 | |
Sho SHIMIZU | 5f70942 | 2016-09-07 09:54:46 -0700 | [diff] [blame] | 814 | // Mutable |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 815 | protected final List<Set<FlowRuleOperation>> stages; |
| 816 | protected final Set<DeviceId> pendingDevices = new HashSet<>(); |
| 817 | protected boolean hasFailed = false; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 818 | |
Sho SHIMIZU | 7c9b73a | 2016-08-30 14:08:28 -0700 | [diff] [blame] | 819 | FlowOperationsProcessor(FlowRuleOperations ops) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 820 | this.stages = Lists.newArrayList(ops.stages()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 821 | this.fops = ops; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | @Override |
Sho SHIMIZU | 2d7c539 | 2016-08-30 14:14:39 -0700 | [diff] [blame] | 825 | public synchronized void run() { |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 826 | if (!stages.isEmpty()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 827 | process(stages.remove(0)); |
Sho SHIMIZU | ad4f2cd | 2016-09-01 13:05:56 -0700 | [diff] [blame] | 828 | } else if (!hasFailed) { |
Sho SHIMIZU | c9e4bb0 | 2016-09-01 12:43:39 -0700 | [diff] [blame] | 829 | fops.callback().onSuccess(fops); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 833 | protected void process(Set<FlowRuleOperation> ops) { |
Sho SHIMIZU | 8efc896 | 2016-08-31 15:17:44 -0700 | [diff] [blame] | 834 | Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 835 | |
Sho SHIMIZU | f4fd3de | 2016-08-31 15:47:56 -0700 | [diff] [blame] | 836 | for (FlowRuleOperation op : ops) { |
| 837 | perDeviceBatches.put(op.rule().deviceId(), |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 838 | new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 839 | } |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 840 | pendingDevices.addAll(perDeviceBatches.keySet()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 841 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 842 | for (DeviceId deviceId : perDeviceBatches.keySet()) { |
Sho SHIMIZU | 3a70431 | 2015-05-27 13:36:01 -0700 | [diff] [blame] | 843 | long id = idGenerator.getNewId(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 844 | final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId), |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 845 | deviceId, id); |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 846 | pendingFlowOperations.put(id, this); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 847 | deviceInstallers.execute(() -> store.storeBatch(b)); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
Sho SHIMIZU | 2d7c539 | 2016-08-30 14:14:39 -0700 | [diff] [blame] | 851 | synchronized void satisfy(DeviceId devId) { |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 852 | pendingDevices.remove(devId); |
| 853 | if (pendingDevices.isEmpty()) { |
Sho SHIMIZU | c0bfe7c | 2016-09-14 16:54:30 -0700 | [diff] [blame] | 854 | operationsService.execute(this); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 855 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 856 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 857 | |
Sho SHIMIZU | 2d7c539 | 2016-08-30 14:14:39 -0700 | [diff] [blame] | 858 | synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) { |
Sho SHIMIZU | b9e0ab7 | 2016-09-14 16:54:13 -0700 | [diff] [blame] | 859 | hasFailed = true; |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 860 | pendingDevices.remove(devId); |
| 861 | if (pendingDevices.isEmpty()) { |
Sho SHIMIZU | c0bfe7c | 2016-09-14 16:54:30 -0700 | [diff] [blame] | 862 | operationsService.execute(this); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Sho SHIMIZU | ad4f2cd | 2016-09-01 13:05:56 -0700 | [diff] [blame] | 865 | FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder(); |
| 866 | failures.forEach(failedOpsBuilder::add); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 867 | |
Sho SHIMIZU | ad4f2cd | 2016-09-01 13:05:56 -0700 | [diff] [blame] | 868 | fops.callback().onError(failedOpsBuilder.build()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 869 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 870 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 871 | |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 872 | // Provides in-order processing in the local instance. The main difference with its |
| 873 | // ancestor is that the runnable ends when all the stages have been processed. Instead, |
| 874 | // its ancestor ends as soon as one stage has been processed and cannot guarantee in-order |
| 875 | // processing between subsequent stages and a new FlowRuleOperation (having the same key). |
| 876 | private class PredictableFlowOperationsProcessor extends FlowOperationsProcessor implements PickyRunnable { |
| 877 | |
| 878 | private static final int WAIT_TIMEOUT = 5000; |
| 879 | private static final int WAIT_ATTEMPTS = 3; |
| 880 | |
| 881 | PredictableFlowOperationsProcessor(FlowRuleOperations ops) { |
| 882 | super(ops); |
| 883 | } |
| 884 | |
| 885 | @Override |
| 886 | public void run() { |
| 887 | try { |
| 888 | while (!stages.isEmpty()) { |
| 889 | process(stages.remove(0)); |
| 890 | synchronized (this) { |
| 891 | // Batch in flights - let's wait |
| 892 | int attempts = 0; |
| 893 | while (!pendingDevices.isEmpty() && attempts < WAIT_ATTEMPTS) { |
| 894 | this.wait(WAIT_TIMEOUT); |
| 895 | attempts++; |
| 896 | } |
| 897 | // Something wrong, we cannot block all the pipeline |
| 898 | if (attempts == WAIT_ATTEMPTS) { |
| 899 | break; |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | } catch (InterruptedException e) { |
| 904 | // Interrupted case |
| 905 | if (log.isTraceEnabled()) { |
| 906 | log.trace("Interrupted while waiting for {} stages to be completed", |
| 907 | stages.size()); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | synchronized (this) { |
| 912 | if (stages.isEmpty() && !hasFailed && pendingDevices.isEmpty()) { |
| 913 | // No error and it is done, signal success to the apps |
| 914 | fops.callback().onSuccess(fops); |
| 915 | } else { |
| 916 | // It was interrupted or there is a failure - signal error. |
| 917 | // This may introduce a duplicate error in some cases but |
| 918 | // better than nothing and keeping the apps blocked forever. |
| 919 | FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder(); |
| 920 | if (!stages.isEmpty()) { |
| 921 | stages.remove(0).forEach(flowRuleOperation -> failedOpsBuilder.add( |
| 922 | flowRuleOperation.rule())); |
| 923 | } |
| 924 | fops.callback().onError(failedOpsBuilder.build()); |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | @Override |
| 930 | synchronized void satisfy(DeviceId devId) { |
| 931 | pendingDevices.remove(devId); |
| 932 | if (pendingDevices.isEmpty()) { |
| 933 | this.notifyAll(); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | @Override |
| 938 | synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) { |
| 939 | hasFailed = true; |
| 940 | pendingDevices.remove(devId); |
| 941 | if (pendingDevices.isEmpty()) { |
| 942 | this.notifyAll(); |
| 943 | } |
| 944 | |
| 945 | FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder(); |
| 946 | failures.forEach(failedOpsBuilder::add); |
| 947 | |
| 948 | fops.callback().onError(failedOpsBuilder.build()); |
| 949 | } |
| 950 | |
| 951 | @Override |
| 952 | public int hint() { |
pierventre | 55c6f33 | 2022-03-29 15:10:39 +0200 | [diff] [blame^] | 953 | return fops.stripeKey().orElse(randomGenerator.nextInt()); |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 957 | @Override |
| 958 | public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) { |
| 959 | checkPermission(FLOWRULE_READ); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 960 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 961 | return store.getTableStatistics(deviceId); |
| 962 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 963 | |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 964 | @Override |
| 965 | public long getActiveFlowRuleCount(DeviceId deviceId) { |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 966 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 967 | return store.getActiveFlowRuleCount(deviceId); |
| 968 | } |
| 969 | |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 970 | @Override |
| 971 | public void applyFlowRules(int key, FlowRule... flowRules) { |
| 972 | checkPermission(FLOWRULE_WRITE); |
| 973 | |
| 974 | apply(buildFlowRuleOperations(true, key, flowRules)); |
| 975 | } |
| 976 | |
| 977 | @Override |
| 978 | public void removeFlowRules(int key, FlowRule... flowRules) { |
| 979 | checkPermission(FLOWRULE_WRITE); |
| 980 | |
| 981 | apply(buildFlowRuleOperations(false, key, flowRules)); |
| 982 | } |
| 983 | |
| 984 | private FlowRuleOperations buildFlowRuleOperations(boolean add, Integer key, FlowRule... flowRules) { |
| 985 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
| 986 | for (FlowRule flowRule : flowRules) { |
| 987 | if (add) { |
| 988 | builder.add(flowRule); |
| 989 | } else { |
| 990 | builder.remove(flowRule); |
| 991 | } |
| 992 | } |
| 993 | if (key != null) { |
| 994 | builder.striped(key); |
| 995 | } |
| 996 | return builder.build(); |
| 997 | } |
| 998 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 999 | private class InternalDeviceListener implements DeviceListener { |
| 1000 | @Override |
| 1001 | public void event(DeviceEvent event) { |
| 1002 | switch (event.type()) { |
| 1003 | case DEVICE_REMOVED: |
| 1004 | case DEVICE_AVAILABILITY_CHANGED: |
| 1005 | DeviceId deviceId = event.subject().id(); |
| 1006 | if (!deviceService.isAvailable(deviceId)) { |
Andrea Campanella | 32a9c0b | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 1007 | BasicDeviceConfig cfg = netCfgService.getConfig(deviceId, BasicDeviceConfig.class); |
pierventre | 07af21d | 2022-03-18 10:31:04 +0100 | [diff] [blame] | 1008 | // if purgeOnDisconnection is set for the device or it's a global configuration |
Andrea Campanella | 32a9c0b | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 1009 | // lets remove the flows. Priority is given to the per device flag |
| 1010 | boolean purge = cfg != null && cfg.isPurgeOnDisconnectionConfigured() ? |
| 1011 | cfg.purgeOnDisconnection() : purgeOnDisconnection; |
| 1012 | if (purge) { |
| 1013 | log.info("PurgeOnDisconnection is requested for device {}, " + |
| 1014 | "removing flows", deviceId); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1015 | store.purgeFlowRule(deviceId); |
| 1016 | } |
| 1017 | } |
| 1018 | break; |
| 1019 | default: |
| 1020 | break; |
| 1021 | } |
| 1022 | } |
| 1023 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 1024 | } |