Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [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 | */ |
| 16 | |
| 17 | package org.onosproject.drivers.hp; |
| 18 | |
| 19 | import com.google.common.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
| 21 | import com.google.common.cache.RemovalCause; |
| 22 | import com.google.common.cache.RemovalNotification; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 23 | import org.onlab.osgi.ServiceDirectory; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 24 | import org.onlab.util.KryoNamespace; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.behaviour.NextGroup; |
| 30 | import org.onosproject.net.behaviour.Pipeliner; |
| 31 | import org.onosproject.net.behaviour.PipelinerContext; |
| 32 | import org.onosproject.net.device.DeviceService; |
| 33 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 34 | import org.onosproject.net.flow.FlowRule; |
| 35 | import org.onosproject.net.flow.FlowRuleOperations; |
| 36 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 37 | import org.onosproject.net.flow.FlowRuleService; |
| 38 | import org.onosproject.net.flow.TrafficSelector; |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 39 | import org.onosproject.net.flow.DefaultFlowRule; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 40 | import org.onosproject.net.flow.TrafficTreatment; |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 41 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 43 | import org.onosproject.net.flow.criteria.Criteria; |
| 44 | import org.onosproject.net.flow.criteria.Criterion; |
| 45 | import org.onosproject.net.flow.criteria.EthCriterion; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 46 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 47 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 48 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 49 | import org.onosproject.net.flow.instructions.Instruction; |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 50 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 51 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 52 | import org.onosproject.net.flow.instructions.L4ModificationInstruction; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 53 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 54 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 55 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 56 | import org.onosproject.net.flowobjective.NextObjective; |
| 57 | import org.onosproject.net.flowobjective.Objective; |
| 58 | import org.onosproject.net.flowobjective.ObjectiveError; |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 59 | import org.onosproject.net.group.Group; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 60 | import org.onosproject.net.group.GroupService; |
| 61 | import org.onosproject.net.meter.MeterService; |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 62 | import org.onosproject.store.serializers.KryoNamespaces; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 63 | import org.slf4j.Logger; |
| 64 | |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 65 | import java.util.Set; |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 66 | import java.util.HashSet; |
| 67 | import java.util.Collection; |
| 68 | import java.util.Collections; |
| 69 | import java.util.List; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 70 | import java.util.concurrent.TimeUnit; |
| 71 | |
| 72 | import static org.onosproject.net.flow.FlowRule.Builder; |
| 73 | import static org.onosproject.net.flowobjective.Objective.Operation.ADD; |
| 74 | import static org.slf4j.LoggerFactory.getLogger; |
| 75 | |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 76 | /** |
| 77 | * Abstraction of the HP pipeline handler. |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 78 | * Possibly compliant with all HP switches: tested with HP3800 (v2 module) and HP3500 (v1 module). |
| 79 | * |
| 80 | * These switches supports multiple OpenFlow instances. |
| 81 | * Each instance can be created with different pipeline models. |
| 82 | * This driver refers to OpenFlow instances created with "pipeline-model standard-match" |
| 83 | * |
| 84 | * With this model Table 100 is used for all entries that can be processed in hardware, |
| 85 | * whereas table 200 is used for entries processed in software. |
| 86 | * |
| 87 | * Trying to install a flow entries only supported in software in table 100 generates |
| 88 | * an OpenFlow error message from the switch. |
| 89 | * |
| 90 | * Installation of a flow entry supported in hardware in table 200 is allowed. But it strongly |
| 91 | * degrades forwarding performance. |
| 92 | * |
| 93 | * --------------------------------------------- |
| 94 | * --- SELECTION OF PROPER TABLE ID --- |
| 95 | * --- from device manual OpenFlow v1.3 for firmware 16.04 - (Appendix A) |
| 96 | * --------------------------------------------- |
| 97 | * --- Hardware differences between v1, v2 and v3 modules affect which features are supported |
| 98 | * in hardware/software. In this driver "TableIdForForwardingObjective()" function selects the |
| 99 | * proper table ID considering the hardware features of the device and the actual FlowObjective. |
| 100 | * |
| 101 | * --------------------------------------------- |
| 102 | * --- HPE switches support OpenFlow version 1.3.1 with following limitations. |
| 103 | * --- from device manual OpenFlow v1.3 for firmware 16.04 - (Appendix A) |
| 104 | * |
| 105 | *** UNSUPPORTED FLOW MATCHES --- (implemented using unsupported_criteria) |
| 106 | * --------------------------------------------- |
| 107 | * - METADATA - DONE |
| 108 | * - IP_ECN - DONE |
| 109 | * - SCTP_SRC, SCTP_DST - DONE |
| 110 | * - IPV6_ND_SLL, IPV6_ND_TLL - DONE |
| 111 | * - MPLS_LABEL, MPLS_TC, MPLS_BOS - DONE |
| 112 | * - PBB_ISID - DONE |
| 113 | * - TUNNEL_ID - DONE |
| 114 | * - IPV6_EXTHDR - DONE |
| 115 | * |
| 116 | *** UNSUPPORTED ACTIONS --- |
| 117 | * --------------------------------------------- |
| 118 | * - METADATA - DONE |
| 119 | * - QUEUE - DONE |
| 120 | * - OFPP_TABLE action - TODO |
| 121 | * - MPLS actions: Push-MPLS, Pop-MPLS, Set-MPLS TTL, Decrement MPLS TTL - DONE |
| 122 | * - Push-PBB, Pop-PBB actions - TODO |
| 123 | * - Copy TTL inwards/outwards actions - DONE |
| 124 | * - Decrement IP TTL - DONE |
| 125 | * |
| 126 | * --------------------------------------------- |
| 127 | * --- OTHER UNSUPPORTED FEATURES --- |
| 128 | * --- from device manual OpenFlow v1.3 for firmware 16.04 |
| 129 | * --------------------------------------------- |
| 130 | * - Port commands: OFPPC_NO_STP, OFPPC_NO_RECV, OFPPC_NO_RECV_STP, OFPPC_NO_FWD - TODO |
| 131 | * - Handling of IP Fragments: OFPC_IP_REASM, OFPC_FRAG_REASM - TODO |
| 132 | * |
| 133 | * TODO MINOR: include above actions in the lists of unsupported features |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 134 | * TODO MINOR: check pre-requites in flow match |
| 135 | * |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 136 | * |
| 137 | * With current implementation, in case of unsupported features a WARNING message in generated |
| 138 | * in the ONOS log, but FlowRule is sent anyway to the device. |
| 139 | * The device will reply with an OFP_ERROR message. |
| 140 | * Use "debug openflow events" and "debug openflow errors" on the device to locally |
| 141 | * visualize detailed information on the specific error. |
| 142 | * |
| 143 | * TODO MAJOR: use OFP_TABLE_FEATURE messages to automate learning of unsupported features |
| 144 | * |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 145 | */ |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 146 | |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 147 | public abstract class AbstractHPPipeline extends AbstractHandlerBehaviour implements Pipeliner { |
| 148 | |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 149 | protected static final String APPLICATION_ID = "org.onosproject.drivers.hp.HPPipeline"; |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 150 | |
| 151 | protected static final int HP_TABLE_ZERO = 0; |
| 152 | protected static final int HP_HARDWARE_TABLE = 100; |
| 153 | protected static final int HP_SOFTWARE_TABLE = 200; |
| 154 | |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 155 | public static final int CACHE_ENTRY_EXPIRATION_PERIOD = 20; |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 156 | |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 157 | private final Logger log = getLogger(getClass()); |
| 158 | protected FlowRuleService flowRuleService; |
| 159 | protected GroupService groupService; |
| 160 | protected MeterService meterService; |
| 161 | protected FlowObjectiveStore flowObjectiveStore; |
| 162 | protected DeviceId deviceId; |
| 163 | protected ApplicationId appId; |
| 164 | protected DeviceService deviceService; |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 165 | protected Device device; |
| 166 | protected String deviceHwVersion; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 167 | protected KryoNamespace appKryo = new KryoNamespace.Builder() |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 168 | .register(KryoNamespaces.API) |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 169 | .build("AbstractHPPipeline"); |
| 170 | private ServiceDirectory serviceDirectory; |
| 171 | private CoreService coreService; |
| 172 | private Cache<Integer, NextObjective> pendingAddNext = CacheBuilder.newBuilder() |
| 173 | .expireAfterWrite(CACHE_ENTRY_EXPIRATION_PERIOD, TimeUnit.SECONDS) |
| 174 | .removalListener((RemovalNotification<Integer, NextObjective> notification) -> { |
| 175 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 176 | notification.getValue().context() |
| 177 | .ifPresent(c -> c.onError(notification.getValue(), |
| 178 | ObjectiveError.FLOWINSTALLATIONFAILED)); |
| 179 | } |
| 180 | }).build(); |
| 181 | |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 182 | /** Lists of unsupported features (firmware version K 16.04) |
| 183 | * If a FlowObjective uses one of these features a warning log message is generated. |
| 184 | */ |
| 185 | protected Set<Criterion.Type> unsupportedCriteria = new HashSet<>(); |
| 186 | protected Set<Instruction.Type> unsupportedInstructions = new HashSet<>(); |
| 187 | protected Set<L2ModificationInstruction.L2SubType> unsupportedL2mod = new HashSet<>(); |
| 188 | protected Set<L3ModificationInstruction.L3SubType> unsupportedL3mod = new HashSet<>(); |
| 189 | |
| 190 | /** Lists of Criteria and Instructions supported in hardware |
| 191 | * If a FlowObjective uses one of these features the FlowRule is intalled in HP_SOFTWARE_TABLE. |
| 192 | */ |
| 193 | protected Set<Criterion.Type> hardwareCriteria = new HashSet<>(); |
| 194 | protected Set<Instruction.Type> hardwareInstructions = new HashSet<>(); |
| 195 | protected Set<L2ModificationInstruction.L2SubType> hardwareInstructionsL2mod = new HashSet<>(); |
| 196 | protected Set<L3ModificationInstruction.L3SubType> hardwareInstructionsL3mod = new HashSet<>(); |
| 197 | protected Set<L4ModificationInstruction.L4SubType> hardwareInstructionsL4mod = new HashSet<>(); |
| 198 | protected Set<Group.Type> hardwareGroups = new HashSet<>(); |
| 199 | |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 200 | /** |
| 201 | * Sets default table id. |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 202 | * Using this solution all flow rules are installed on the "default" table |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 203 | * |
| 204 | * @param ruleBuilder flow rule builder to be set table id |
| 205 | * @return flow rule builder with set table id for flow |
| 206 | */ |
| 207 | protected abstract FlowRule.Builder setDefaultTableIdForFlowObjective(Builder ruleBuilder); |
| 208 | |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 209 | /** |
| 210 | * Return the proper table ID depending on the specific ForwardingObjective. |
| 211 | * |
| 212 | * HP switches supporting openflow have 3 tables (Pipeline Model: Standard Match) |
| 213 | * Table 0 is just a shortcut to table 100 |
| 214 | * Table 100/200 are respectively used for rules processed in HARDWARE/SOFTWARE |
| 215 | * |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 216 | * @param selector TrafficSelector including flow match |
| 217 | * @param treatment TrafficTreatment including instructions/actions |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 218 | * @return table id |
| 219 | */ |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 220 | protected abstract int tableIdForForwardingObjective(TrafficSelector selector, TrafficTreatment treatment); |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * Return TRUE if ForwardingObjective fwd includes unsupported features. |
| 224 | * |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 225 | * @param selector TrafficSelector including flow match |
| 226 | * @param treatment TrafficTreatment including instructions/actions |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 227 | * @return boolean |
| 228 | */ |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 229 | protected abstract boolean checkUnSupportedFeatures(TrafficSelector selector, TrafficTreatment treatment); |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 230 | |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 231 | @Override |
| 232 | public void init(DeviceId deviceId, PipelinerContext context) { |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 233 | this.deviceId = deviceId; |
| 234 | |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 235 | serviceDirectory = context.directory(); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 236 | coreService = serviceDirectory.get(CoreService.class); |
| 237 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 238 | groupService = serviceDirectory.get(GroupService.class); |
| 239 | meterService = serviceDirectory.get(MeterService.class); |
| 240 | deviceService = serviceDirectory.get(DeviceService.class); |
| 241 | flowObjectiveStore = context.store(); |
| 242 | |
| 243 | appId = coreService.registerApplication(APPLICATION_ID); |
| 244 | |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 245 | device = deviceService.getDevice(deviceId); |
| 246 | deviceHwVersion = device.hwVersion(); |
| 247 | |
| 248 | //Initialization of model specific features |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 249 | log.debug("HP Driver - Initializing unsupported features for switch {}", deviceHwVersion); |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 250 | initUnSupportedFeatures(); |
| 251 | |
| 252 | log.debug("HP Driver - Initializing features supported in hardware"); |
| 253 | initHardwareCriteria(); |
| 254 | initHardwareInstructions(); |
| 255 | |
| 256 | log.debug("HP Driver - Initializing pipeline"); |
| 257 | installHPTableZero(); |
| 258 | installHPHardwareTable(); |
| 259 | installHPSoftwareTable(); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 263 | * UnSupported features are specific of each model. |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 264 | */ |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 265 | protected abstract void initUnSupportedFeatures(); |
| 266 | |
| 267 | /** |
| 268 | * Criteria supported in hardware are specific of each model. |
| 269 | */ |
| 270 | protected abstract void initHardwareCriteria(); |
| 271 | |
| 272 | /** |
| 273 | * Instructions supported in hardware are specific of each model. |
| 274 | */ |
| 275 | protected abstract void initHardwareInstructions(); |
| 276 | |
| 277 | /** |
| 278 | * HP Table 0 initialization. |
| 279 | * Installs rule goto HP_HARDWARE_TABLE in HP_TABLE_ZERO |
| 280 | */ |
| 281 | private void installHPTableZero() { |
| 282 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 283 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 284 | |
| 285 | treatment.transition(HP_HARDWARE_TABLE); |
| 286 | |
| 287 | FlowRule rule = DefaultFlowRule.builder().forDevice(this.deviceId) |
| 288 | .withSelector(selector.build()) |
| 289 | .withTreatment(treatment.build()) |
| 290 | .withPriority(0) |
| 291 | .fromApp(appId) |
| 292 | .makePermanent() |
| 293 | .forTable(HP_TABLE_ZERO) |
| 294 | .build(); |
| 295 | |
| 296 | this.applyRules(true, rule); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * HP hardware table initialization. |
| 301 | * Installs rule goto HP_SOFTWARE_TABLE in HP_HARDWARE_TABLE |
| 302 | */ |
| 303 | private void installHPHardwareTable() { |
| 304 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 305 | |
| 306 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 307 | //treatment.setOutput(PortNumber.NORMAL); |
| 308 | treatment.transition(HP_SOFTWARE_TABLE); |
| 309 | |
| 310 | FlowRule rule = DefaultFlowRule.builder().forDevice(this.deviceId) |
| 311 | .withSelector(selector.build()) |
| 312 | .withTreatment(treatment.build()) |
| 313 | .withPriority(0) |
| 314 | .fromApp(appId) |
| 315 | .makePermanent() |
| 316 | .forTable(HP_HARDWARE_TABLE) |
| 317 | .build(); |
| 318 | |
| 319 | this.applyRules(true, rule); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Applies FlowRule. |
| 324 | * Installs or removes FlowRule. |
| 325 | * |
| 326 | * @param install - whether to install or remove rule |
| 327 | * @param rule - the rule to be installed or removed |
| 328 | */ |
| 329 | private void applyRules(boolean install, FlowRule rule) { |
| 330 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 331 | |
| 332 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 333 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 334 | @Override |
| 335 | public void onSuccess(FlowRuleOperations ops) { |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 336 | log.trace("HP Driver: - applyRules onSuccess rule {}", rule); |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public void onError(FlowRuleOperations ops) { |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 341 | log.trace("HP Driver: applyRules onError rule: " + rule); |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 342 | } |
| 343 | })); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * HP software table initialization. |
| 348 | * No rules required. |
| 349 | */ |
| 350 | private void installHPSoftwareTable() {} |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 351 | |
| 352 | protected void pass(Objective obj) { |
| 353 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
| 354 | } |
| 355 | |
| 356 | protected void fail(Objective obj, ObjectiveError error) { |
| 357 | obj.context().ifPresent(context -> context.onError(obj, error)); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void forward(ForwardingObjective fwd) { |
| 362 | |
| 363 | if (fwd.treatment() != null) { |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 364 | /* If UNSUPPORTED features included in ForwardingObjective a warning message is generated. |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 365 | * FlowRule is anyway sent to the device, device will reply with an OFP_ERROR. |
| 366 | * Moreover, checkUnSupportedFeatures function generates further warnings specifying |
| 367 | * each unsupported feature. |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 368 | */ |
| 369 | if (checkUnSupportedFeatures(fwd.selector(), fwd.treatment())) { |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 370 | log.warn("HP Driver - specified ForwardingObjective contains UNSUPPORTED FEATURES"); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 371 | } |
| 372 | |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 373 | // Deal with SPECIFIC and VERSATILE in the same manner. |
| 374 | // Create the FlowRule starting from the ForwardingObjective |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 375 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 376 | .forDevice(deviceId) |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 377 | .withSelector(fwd.selector()) |
| 378 | .withTreatment(fwd.treatment()) |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 379 | .withPriority(fwd.priority()) |
| 380 | .fromApp(fwd.appId()); |
| 381 | |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 382 | // Determine the table to be used depends on selector, treatment and specific switch hardware |
| 383 | ruleBuilder.forTable(tableIdForForwardingObjective(fwd.selector(), fwd.treatment())); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 384 | |
| 385 | if (fwd.permanent()) { |
| 386 | ruleBuilder.makePermanent(); |
| 387 | } else { |
| 388 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 389 | } |
| 390 | |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 391 | log.debug("HP Driver - installing ForwadingObjective arrived with treatment {}", fwd); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 392 | installObjective(ruleBuilder, fwd); |
| 393 | |
| 394 | } else { |
| 395 | NextObjective nextObjective; |
| 396 | NextGroup next; |
| 397 | TrafficTreatment treatment; |
| 398 | if (fwd.op() == ADD) { |
| 399 | // Give a try to the cache. Doing an operation |
| 400 | // on the store seems to be very expensive. |
| 401 | nextObjective = pendingAddNext.getIfPresent(fwd.nextId()); |
| 402 | // If the next objective is not present |
| 403 | // We will try with the store |
| 404 | if (nextObjective == null) { |
| 405 | next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 406 | // We verify that next was in the store and then de-serialize |
| 407 | // the treatment in order to re-build the flow rule. |
| 408 | if (next == null) { |
| 409 | fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.GROUPMISSING)); |
| 410 | return; |
| 411 | } |
| 412 | treatment = appKryo.deserialize(next.data()); |
| 413 | } else { |
| 414 | pendingAddNext.invalidate(fwd.nextId()); |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 415 | treatment = getTreatment(nextObjective); |
| 416 | if (treatment == null) { |
| 417 | fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.UNSUPPORTED)); |
| 418 | return; |
| 419 | } |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 420 | } |
| 421 | } else { |
| 422 | // We get the NextGroup from the remove operation. |
| 423 | // Doing an operation on the store seems to be very expensive. |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 424 | next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 425 | treatment = (next != null) ? appKryo.deserialize(next.data()) : null; |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 426 | } |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 427 | |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 428 | // If the treatment is null we cannot re-build the original flow |
| 429 | if (treatment == null) { |
| 430 | fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.GROUPMISSING)); |
| 431 | return; |
| 432 | } |
| 433 | // Finally we build the flow rule and push to the flowrule subsystem. |
| 434 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 435 | .forDevice(deviceId) |
| 436 | .withSelector(fwd.selector()) |
| 437 | .fromApp(fwd.appId()) |
| 438 | .withPriority(fwd.priority()) |
| 439 | .withTreatment(treatment); |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 440 | |
| 441 | /* If UNSUPPORTED features included in ForwardingObjective a warning message is generated. |
| 442 | * FlowRule is anyway sent to the device, device will reply with an OFP_ERROR. |
| 443 | * Moreover, checkUnSupportedFeatures function generates further warnings specifying |
| 444 | * each unsupported feature. |
| 445 | */ |
| 446 | if (checkUnSupportedFeatures(fwd.selector(), treatment)) { |
| 447 | log.warn("HP Driver - specified ForwardingObjective contains UNSUPPORTED FEATURES"); |
| 448 | } |
| 449 | |
| 450 | //Table to be used depends on the specific switch hardware and ForwardingObjective |
| 451 | ruleBuilder.forTable(tableIdForForwardingObjective(fwd.selector(), treatment)); |
| 452 | |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 453 | if (fwd.permanent()) { |
| 454 | ruleBuilder.makePermanent(); |
| 455 | } else { |
| 456 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 457 | } |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 458 | |
| 459 | log.debug("HP Driver - installing ForwadingObjective arrived with NULL treatment (intent)"); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 460 | installObjective(ruleBuilder, fwd); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Installs objective. |
| 466 | * |
| 467 | * @param ruleBuilder flow rule builder used to build rule from objective |
| 468 | * @param objective objective to be installed |
| 469 | */ |
| 470 | protected void installObjective(FlowRule.Builder ruleBuilder, Objective objective) { |
| 471 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 472 | |
| 473 | switch (objective.op()) { |
| 474 | case ADD: |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 475 | log.trace("HP Driver - Requested ADD of objective to device " + deviceId); |
| 476 | flowBuilder.add(ruleBuilder.build()); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 477 | break; |
| 478 | case REMOVE: |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 479 | log.trace("HP Driver - Requested REMOVE of objective to device " + deviceId); |
| 480 | flowBuilder.remove(ruleBuilder.build()); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 481 | break; |
| 482 | default: |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 483 | log.debug("HP Driver - Unknown operation {}", objective.op()); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 487 | @Override |
| 488 | public void onSuccess(FlowRuleOperations ops) { |
| 489 | objective.context().ifPresent(context -> context.onSuccess(objective)); |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 490 | log.trace("HP Driver - Installed objective " + objective.toString()); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public void onError(FlowRuleOperations ops) { |
| 495 | objective.context() |
| 496 | .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED)); |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 497 | log.trace("HP Driver - Objective installation failed" + objective.toString()); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 498 | } |
| 499 | })); |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public void next(NextObjective nextObjective) { |
| 504 | switch (nextObjective.op()) { |
| 505 | case ADD: |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 506 | log.debug("HP driver: NextObjective ADDED"); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 507 | // We insert the value in the cache |
| 508 | pendingAddNext.put(nextObjective.id(), nextObjective); |
| 509 | // Then in the store, this will unblock the queued fwd obj |
| 510 | flowObjectiveStore.putNextGroup( |
| 511 | nextObjective.id(), |
| 512 | new SingleGroup(nextObjective.next().iterator().next()) |
| 513 | ); |
| 514 | break; |
| 515 | case REMOVE: |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 516 | log.debug("HP driver: NextObjective REMOVED"); |
| 517 | NextGroup next = flowObjectiveStore.removeNextGroup(nextObjective.id()); |
| 518 | if (next == null) { |
| 519 | nextObjective.context().ifPresent(context -> context.onError(nextObjective, |
| 520 | ObjectiveError.GROUPMISSING)); |
| 521 | return; |
| 522 | } |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 523 | break; |
| 524 | default: |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 525 | log.debug("Unsupported operation {}", nextObjective.op()); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 526 | } |
| 527 | nextObjective.context().ifPresent(context -> context.onSuccess(nextObjective)); |
| 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 532 | //TODO: to be implemented |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 533 | return Collections.emptyList(); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | @Override |
| 537 | public void filter(FilteringObjective filteringObjective) { |
| 538 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 539 | processFilter(filteringObjective, |
| 540 | filteringObjective.op() == Objective.Operation.ADD, |
| 541 | filteringObjective.appId()); |
| 542 | } else { |
| 543 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /** |
alessio | ff124ad | 2018-11-13 13:25:51 +0100 | [diff] [blame] | 548 | * Gets traffic treatment from a next objective. |
| 549 | * Merge traffic treatments from next objective if the next objective is |
| 550 | * BROADCAST type and contains multiple traffic treatments. |
| 551 | * Returns first treatment from next objective if the next objective is |
| 552 | * SIMPLE type and it contains only one treatment. |
| 553 | * |
| 554 | * @param nextObjective the next objective |
| 555 | * @return the treatment from next objective; null if not supported |
| 556 | */ |
| 557 | private TrafficTreatment getTreatment(NextObjective nextObjective) { |
| 558 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 559 | switch (nextObjective.type()) { |
| 560 | case SIMPLE: |
| 561 | if (treatments.size() != 1) { |
| 562 | log.error("Next Objectives of type SIMPLE should have only " + |
| 563 | "one traffic treatment. NexObjective: {}", |
| 564 | nextObjective.toString()); |
| 565 | return null; |
| 566 | } |
| 567 | return treatments.iterator().next(); |
| 568 | case BROADCAST: |
| 569 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 570 | treatments.forEach(builder::addTreatment); |
| 571 | return builder.build(); |
| 572 | default: |
| 573 | log.error("Unsupported next objective type {}.", nextObjective.type()); |
| 574 | return null; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | /** |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 579 | * Filter processing and installation. |
| 580 | * Processes and installs filtering rules. |
| 581 | * |
| 582 | * @param filt |
| 583 | * @param install |
| 584 | * @param applicationId |
| 585 | */ |
| 586 | private void processFilter(FilteringObjective filt, boolean install, |
| 587 | ApplicationId applicationId) { |
| 588 | // This driver only processes filtering criteria defined with switch |
| 589 | // ports as the key |
| 590 | PortCriterion port; |
| 591 | if (!filt.key().equals(Criteria.dummy()) && |
| 592 | filt.key().type() == Criterion.Type.IN_PORT) { |
| 593 | port = (PortCriterion) filt.key(); |
| 594 | } else { |
| 595 | log.warn("No key defined in filtering objective from app: {}. Not" |
| 596 | + "processing filtering objective", applicationId); |
| 597 | fail(filt, ObjectiveError.UNKNOWN); |
| 598 | return; |
| 599 | } |
| 600 | // convert filtering conditions for switch-intfs into flowrules |
| 601 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 602 | for (Criterion c : filt.conditions()) { |
| 603 | if (c.type() == Criterion.Type.ETH_DST) { |
| 604 | EthCriterion eth = (EthCriterion) c; |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 605 | FlowRule.Builder rule = processEthFilter(filt, eth, port); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 606 | rule.forDevice(deviceId) |
| 607 | .fromApp(applicationId); |
| 608 | ops = install ? ops.add(rule.build()) : ops.remove(rule.build()); |
| 609 | |
| 610 | } else if (c.type() == Criterion.Type.VLAN_VID) { |
| 611 | VlanIdCriterion vlan = (VlanIdCriterion) c; |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 612 | FlowRule.Builder rule = processVlanFilter(filt, vlan, port); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 613 | rule.forDevice(deviceId) |
| 614 | .fromApp(applicationId); |
| 615 | ops = install ? ops.add(rule.build()) : ops.remove(rule.build()); |
| 616 | |
| 617 | } else if (c.type() == Criterion.Type.IPV4_DST) { |
| 618 | IPCriterion ip = (IPCriterion) c; |
| 619 | FlowRule.Builder rule = processIpFilter(filt, ip, port); |
| 620 | rule.forDevice(deviceId) |
| 621 | .fromApp(applicationId); |
| 622 | ops = install ? ops.add(rule.build()) : ops.remove(rule.build()); |
| 623 | |
| 624 | } else { |
| 625 | log.warn("Driver does not currently process filtering condition" |
| 626 | + " of type: {}", c.type()); |
| 627 | fail(filt, ObjectiveError.UNSUPPORTED); |
| 628 | } |
| 629 | } |
| 630 | // apply filtering flow rules |
| 631 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 632 | @Override |
| 633 | public void onSuccess(FlowRuleOperations ops) { |
| 634 | pass(filt); |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 635 | log.trace("HP Driver - Applied filtering rules"); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | @Override |
| 639 | public void onError(FlowRuleOperations ops) { |
| 640 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 641 | log.trace("HP Driver - Failed to apply filtering rules"); |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 642 | } |
| 643 | })); |
| 644 | } |
| 645 | |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 646 | protected abstract Builder processEthFilter(FilteringObjective filt, |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 647 | EthCriterion eth, PortCriterion port); |
| 648 | |
Alessio Giorgetti | c1518bb | 2017-11-17 17:01:26 +0100 | [diff] [blame] | 649 | protected abstract Builder processVlanFilter(FilteringObjective filt, |
Jovana Vuleta | 1de6126 | 2017-06-14 11:10:29 +0200 | [diff] [blame] | 650 | VlanIdCriterion vlan, PortCriterion port); |
| 651 | |
| 652 | protected abstract Builder processIpFilter(FilteringObjective filt, |
| 653 | IPCriterion ip, PortCriterion port); |
| 654 | |
| 655 | private class SingleGroup implements NextGroup { |
| 656 | |
| 657 | private TrafficTreatment nextActions; |
| 658 | |
| 659 | SingleGroup(TrafficTreatment next) { |
| 660 | this.nextActions = next; |
| 661 | } |
| 662 | |
| 663 | @Override |
| 664 | public byte[] data() { |
| 665 | return appKryo.serialize(nextActions); |
| 666 | } |
| 667 | |
| 668 | public TrafficTreatment treatment() { |
| 669 | return nextActions; |
| 670 | } |
| 671 | |
| 672 | } |
| 673 | |
| 674 | |
| 675 | } |