alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.driver.pipeline; |
| 17 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalCause; |
| 21 | import com.google.common.cache.RemovalNotification; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
| 23 | import org.apache.commons.lang3.tuple.ImmutablePair; |
| 24 | import org.apache.commons.lang3.tuple.Pair; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 25 | import org.onlab.osgi.ServiceDirectory; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 26 | import org.onlab.packet.EthType; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 27 | import org.onlab.packet.IPv4; |
| 28 | import org.onlab.packet.VlanId; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 29 | import org.onlab.util.KryoNamespace; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.PortNumber; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 34 | import org.onosproject.net.behaviour.NextGroup; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 35 | import org.onosproject.net.behaviour.Pipeliner; |
| 36 | import org.onosproject.net.behaviour.PipelinerContext; |
| 37 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 38 | import org.onosproject.net.flow.DefaultFlowRule; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.FlowRule; |
| 42 | import org.onosproject.net.flow.FlowRuleOperations; |
| 43 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 44 | import org.onosproject.net.flow.FlowRuleService; |
| 45 | import org.onosproject.net.flow.TrafficSelector; |
| 46 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 47 | import org.onosproject.net.flow.criteria.Criteria; |
| 48 | import org.onosproject.net.flow.criteria.Criterion; |
| 49 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 50 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 51 | import org.onosproject.net.flow.criteria.PortCriterion; |
alshabib | 2f74f2c | 2016-01-14 13:29:35 -0800 | [diff] [blame] | 52 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 53 | import org.onosproject.net.flow.instructions.Instruction; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 54 | import org.onosproject.net.flow.instructions.Instructions; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 55 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 56 | import org.onosproject.net.flowobjective.FilteringObjective; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 57 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 58 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 59 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 60 | import org.onosproject.net.flowobjective.Objective; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 61 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 62 | import org.onosproject.net.group.DefaultGroupBucket; |
| 63 | import org.onosproject.net.group.DefaultGroupDescription; |
| 64 | import org.onosproject.net.group.DefaultGroupKey; |
| 65 | import org.onosproject.net.group.Group; |
| 66 | import org.onosproject.net.group.GroupBucket; |
| 67 | import org.onosproject.net.group.GroupBuckets; |
| 68 | import org.onosproject.net.group.GroupDescription; |
| 69 | import org.onosproject.net.group.GroupEvent; |
| 70 | import org.onosproject.net.group.GroupKey; |
| 71 | import org.onosproject.net.group.GroupListener; |
| 72 | import org.onosproject.net.group.GroupService; |
| 73 | import org.onosproject.store.serializers.KryoNamespaces; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 74 | import org.slf4j.Logger; |
| 75 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 76 | import java.util.Collection; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 77 | import java.util.Collections; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 78 | import java.util.List; |
| 79 | import java.util.Optional; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 80 | import java.util.concurrent.TimeUnit; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 81 | import java.util.stream.Collectors; |
| 82 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 83 | import static org.slf4j.LoggerFactory.getLogger; |
| 84 | |
| 85 | /** |
Jonathan Hart | 64da69d | 2015-07-15 15:10:28 -0700 | [diff] [blame] | 86 | * Pipeliner for OLT device. |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 87 | */ |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 88 | |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 89 | public class OltPipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 90 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 91 | private static final Integer QQ_TABLE = 1; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 92 | private static final short MCAST_VLAN = 4000; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 93 | private final Logger log = getLogger(getClass()); |
| 94 | |
| 95 | private ServiceDirectory serviceDirectory; |
| 96 | private FlowRuleService flowRuleService; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 97 | private GroupService groupService; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 98 | private CoreService coreService; |
| 99 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 100 | private DeviceId deviceId; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 101 | private ApplicationId appId; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 102 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 103 | protected FlowObjectiveStore flowObjectiveStore; |
| 104 | |
| 105 | private Cache<GroupKey, NextObjective> pendingGroups; |
| 106 | |
| 107 | protected static KryoNamespace appKryo = new KryoNamespace.Builder() |
| 108 | .register(KryoNamespaces.API) |
| 109 | .register(GroupKey.class) |
| 110 | .register(DefaultGroupKey.class) |
| 111 | .register(OLTPipelineGroup.class) |
| 112 | .register(byte[].class) |
| 113 | .build(); |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 114 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 115 | @Override |
| 116 | public void init(DeviceId deviceId, PipelinerContext context) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 117 | log.debug("Initiate OLT pipeline"); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 118 | this.serviceDirectory = context.directory(); |
| 119 | this.deviceId = deviceId; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 120 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 121 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 122 | coreService = serviceDirectory.get(CoreService.class); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 123 | groupService = serviceDirectory.get(GroupService.class); |
| 124 | flowObjectiveStore = context.store(); |
| 125 | |
alshabib | b32cefe | 2015-06-08 18:15:05 -0700 | [diff] [blame] | 126 | |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 127 | appId = coreService.registerApplication( |
| 128 | "org.onosproject.driver.OLTPipeline"); |
| 129 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 130 | |
| 131 | pendingGroups = CacheBuilder.newBuilder() |
| 132 | .expireAfterWrite(20, TimeUnit.SECONDS) |
| 133 | .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> { |
| 134 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 135 | fail(notification.getValue(), ObjectiveError.GROUPINSTALLATIONFAILED); |
| 136 | } |
| 137 | }).build(); |
| 138 | |
| 139 | groupService.addListener(new InnerGroupListener()); |
| 140 | |
alshabib | b32cefe | 2015-06-08 18:15:05 -0700 | [diff] [blame] | 141 | } |
| 142 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public void filter(FilteringObjective filter) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 145 | Instructions.OutputInstruction output; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 146 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 147 | if (filter.meta() != null && !filter.meta().immediate().isEmpty()) { |
| 148 | output = (Instructions.OutputInstruction) filter.meta().immediate().stream() |
| 149 | .filter(t -> t.type().equals(Instruction.Type.OUTPUT)) |
| 150 | .limit(1) |
| 151 | .findFirst().get(); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 152 | |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 153 | if (output == null || !output.port().equals(PortNumber.CONTROLLER)) { |
| 154 | log.error("OLT can only filter packet to controller"); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 155 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 156 | return; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 157 | } |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 158 | } else { |
| 159 | fail(filter, ObjectiveError.BADPARAMS); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 163 | if (filter.key().type() != Criterion.Type.IN_PORT) { |
| 164 | fail(filter, ObjectiveError.BADPARAMS); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | EthTypeCriterion ethType = (EthTypeCriterion) |
| 169 | filterForCriterion(filter.conditions(), Criterion.Type.ETH_TYPE); |
| 170 | |
| 171 | if (ethType == null) { |
| 172 | fail(filter, ObjectiveError.BADPARAMS); |
| 173 | return; |
| 174 | } |
| 175 | |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 176 | if (ethType.ethType().equals(EthType.EtherType.EAPOL.ethType())) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 177 | provisionEapol(filter, ethType, output); |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 178 | } else if (ethType.ethType().equals(EthType.EtherType.IPV4.ethType())) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 179 | IPProtocolCriterion ipProto = (IPProtocolCriterion) |
| 180 | filterForCriterion(filter.conditions(), Criterion.Type.IP_PROTO); |
| 181 | if (ipProto.protocol() == IPv4.PROTOCOL_IGMP) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 182 | provisionIgmp(filter, ethType, ipProto, output); |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 183 | } else { |
| 184 | log.error("OLT can only filter igmp"); |
| 185 | fail(filter, ObjectiveError.UNSUPPORTED); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 186 | } |
| 187 | } else { |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 188 | log.error("OLT can only filter eapol and igmp"); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 189 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |
| 194 | |
| 195 | @Override |
| 196 | public void forward(ForwardingObjective fwd) { |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 197 | |
| 198 | if (checkForMulticast(fwd)) { |
| 199 | processMulticastRule(fwd); |
| 200 | return; |
| 201 | } |
| 202 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 203 | TrafficTreatment treatment = fwd.treatment(); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 204 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 205 | List<Instruction> instructions = treatment.allInstructions(); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 206 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 207 | Optional<Instruction> vlanIntruction = instructions.stream() |
| 208 | .filter(i -> i.type() == Instruction.Type.L2MODIFICATION) |
| 209 | .filter(i -> ((L2ModificationInstruction) i).subtype() == |
| 210 | L2ModificationInstruction.L2SubType.VLAN_PUSH || |
| 211 | ((L2ModificationInstruction) i).subtype() == |
| 212 | L2ModificationInstruction.L2SubType.VLAN_POP) |
| 213 | .findAny(); |
| 214 | |
| 215 | if (!vlanIntruction.isPresent()) { |
| 216 | fail(fwd, ObjectiveError.BADPARAMS); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | L2ModificationInstruction vlanIns = |
| 221 | (L2ModificationInstruction) vlanIntruction.get(); |
| 222 | |
| 223 | if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) { |
| 224 | installUpstreamRules(fwd); |
| 225 | } else if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP) { |
| 226 | installDownstreamRules(fwd); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 227 | } else { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 228 | log.error("Unknown OLT operation: {}", fwd); |
| 229 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 230 | return; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 233 | pass(fwd); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 234 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 237 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 238 | @Override |
| 239 | public void next(NextObjective nextObjective) { |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 240 | if (nextObjective.type() != NextObjective.Type.BROADCAST) { |
| 241 | log.error("OLT only supports broadcast groups."); |
| 242 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 243 | } |
| 244 | |
| 245 | if (nextObjective.next().size() != 1) { |
| 246 | log.error("OLT only supports singleton broadcast groups."); |
| 247 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 248 | } |
| 249 | |
| 250 | TrafficTreatment treatment = nextObjective.next().stream().findFirst().get(); |
| 251 | |
| 252 | |
| 253 | GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(treatment); |
| 254 | GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id())); |
| 255 | |
| 256 | GroupDescription groupDesc = |
| 257 | new DefaultGroupDescription(deviceId, |
| 258 | GroupDescription.Type.ALL, |
| 259 | new GroupBuckets(Collections.singletonList(bucket)), |
| 260 | key, |
| 261 | null, |
| 262 | nextObjective.appId()); |
| 263 | |
| 264 | pendingGroups.put(key, nextObjective); |
| 265 | |
| 266 | switch (nextObjective.op()) { |
| 267 | case ADD: |
| 268 | groupService.addGroup(groupDesc); |
| 269 | break; |
| 270 | case REMOVE: |
| 271 | groupService.removeGroup(deviceId, key, nextObjective.appId()); |
| 272 | break; |
| 273 | case ADD_TO_EXISTING: |
| 274 | case REMOVE_FROM_EXISTING: |
| 275 | //TODO: handle addition to group when caller signals it. |
| 276 | break; |
| 277 | default: |
| 278 | log.warn("Unknown next objective operation: {}", nextObjective.op()); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | } |
| 283 | |
| 284 | private void processMulticastRule(ForwardingObjective fwd) { |
| 285 | if (fwd.nextId() == null) { |
| 286 | log.error("Multicast objective does not have a next id"); |
| 287 | fail(fwd, ObjectiveError.BADPARAMS); |
| 288 | } |
| 289 | |
| 290 | OLTPipelineGroup next = getGroupForNextObjective(fwd.nextId()); |
| 291 | |
| 292 | if (next == null) { |
| 293 | log.error("Group for forwarding objective missing: {}", fwd); |
| 294 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 295 | } |
| 296 | |
| 297 | Group group = groupService.getGroup(deviceId, next.key()); |
| 298 | TrafficTreatment treatment = |
| 299 | buildTreatment(Instructions.createGroup(group.id())); |
| 300 | |
| 301 | FlowRule rule = DefaultFlowRule.builder() |
| 302 | .forDevice(deviceId) |
| 303 | .forTable(0) |
| 304 | .fromApp(fwd.appId()) |
| 305 | .makePermanent() |
| 306 | .withPriority(fwd.priority()) |
| 307 | .withSelector(fwd.selector()) |
| 308 | .withTreatment(treatment) |
| 309 | .build(); |
| 310 | |
| 311 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
| 312 | switch (fwd.op()) { |
| 313 | |
| 314 | case ADD: |
| 315 | builder.add(rule); |
| 316 | break; |
| 317 | case REMOVE: |
| 318 | builder.remove(rule); |
| 319 | break; |
| 320 | case ADD_TO_EXISTING: |
| 321 | case REMOVE_FROM_EXISTING: |
| 322 | break; |
| 323 | default: |
| 324 | log.warn("Unknown forwarding operation: {}", fwd.op()); |
| 325 | } |
| 326 | |
| 327 | applyFlowRules(builder, fwd); |
| 328 | |
| 329 | } |
| 330 | |
| 331 | private boolean checkForMulticast(ForwardingObjective fwd) { |
| 332 | |
| 333 | VlanIdCriterion vlan = (VlanIdCriterion) filterForCriterion(fwd.selector().criteria(), |
| 334 | Criterion.Type.VLAN_VID); |
| 335 | |
| 336 | return (vlan != null && vlan.vlanId().equals(VlanId.vlanId(MCAST_VLAN))); |
| 337 | |
| 338 | } |
| 339 | |
| 340 | private OLTPipelineGroup getGroupForNextObjective(Integer nextId) { |
| 341 | NextGroup next = flowObjectiveStore.getNextGroup(nextId); |
| 342 | return (OLTPipelineGroup) appKryo.deserialize(next.data()); |
| 343 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 346 | private void installDownstreamRules(ForwardingObjective fwd) { |
| 347 | List<Pair<Instruction, Instruction>> vlanOps = |
| 348 | vlanOps(fwd, |
| 349 | L2ModificationInstruction.L2SubType.VLAN_POP); |
| 350 | |
| 351 | if (vlanOps == null) { |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | Instruction output = fetchOutput(fwd, "downstream"); |
| 356 | |
| 357 | if (output == null) { |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | Pair<Instruction, Instruction> popAndRewrite = vlanOps.remove(0); |
| 362 | |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 363 | TrafficSelector selector = fwd.selector(); |
| 364 | |
| 365 | Criterion outerVlan = selector.getCriterion(Criterion.Type.VLAN_VID); |
| 366 | Criterion innerVlan = selector.getCriterion(Criterion.Type.INNER_VLAN_VID); |
| 367 | Criterion inport = selector.getCriterion(Criterion.Type.IN_PORT); |
| 368 | |
| 369 | if (outerVlan == null || innerVlan == null || inport == null) { |
| 370 | log.error("Forwarding objective is underspecified: {}", fwd); |
| 371 | fail(fwd, ObjectiveError.BADPARAMS); |
| 372 | return; |
| 373 | } |
| 374 | |
alshabib | 2f74f2c | 2016-01-14 13:29:35 -0800 | [diff] [blame] | 375 | Criterion innerVid = Criteria.matchVlanId(((VlanIdCriterion) innerVlan).vlanId()); |
| 376 | |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 377 | FlowRule.Builder outer = DefaultFlowRule.builder() |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 378 | .forDevice(deviceId) |
| 379 | .fromApp(appId) |
| 380 | .makePermanent() |
| 381 | .withPriority(fwd.priority()) |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 382 | .withSelector(buildSelector(inport, outerVlan)) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 383 | .withTreatment(buildTreatment(popAndRewrite.getLeft(), |
| 384 | Instructions.transition(QQ_TABLE))); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 385 | |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 386 | FlowRule.Builder inner = DefaultFlowRule.builder() |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 387 | .forDevice(deviceId) |
| 388 | .fromApp(appId) |
| 389 | .forTable(QQ_TABLE) |
| 390 | .makePermanent() |
| 391 | .withPriority(fwd.priority()) |
alshabib | 2f74f2c | 2016-01-14 13:29:35 -0800 | [diff] [blame] | 392 | .withSelector(buildSelector(inport, innerVid)) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 393 | .withTreatment(buildTreatment(popAndRewrite.getRight(), |
| 394 | output)); |
| 395 | |
| 396 | applyRules(fwd, inner, outer); |
| 397 | |
| 398 | } |
| 399 | |
| 400 | private void installUpstreamRules(ForwardingObjective fwd) { |
| 401 | List<Pair<Instruction, Instruction>> vlanOps = |
| 402 | vlanOps(fwd, |
| 403 | L2ModificationInstruction.L2SubType.VLAN_PUSH); |
| 404 | |
| 405 | if (vlanOps == null) { |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | Instruction output = fetchOutput(fwd, "upstream"); |
| 410 | |
| 411 | if (output == null) { |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | Pair<Instruction, Instruction> innerPair = vlanOps.remove(0); |
| 416 | |
| 417 | Pair<Instruction, Instruction> outerPair = vlanOps.remove(0); |
| 418 | |
| 419 | FlowRule.Builder inner = DefaultFlowRule.builder() |
| 420 | .forDevice(deviceId) |
| 421 | .fromApp(appId) |
| 422 | .makePermanent() |
| 423 | .withPriority(fwd.priority()) |
| 424 | .withSelector(fwd.selector()) |
| 425 | .withTreatment(buildTreatment(innerPair.getRight(), |
| 426 | Instructions.transition(QQ_TABLE))); |
| 427 | |
| 428 | PortCriterion inPort = (PortCriterion) |
| 429 | fwd.selector().getCriterion(Criterion.Type.IN_PORT); |
| 430 | |
| 431 | VlanId cVlanId = ((L2ModificationInstruction.ModVlanIdInstruction) |
| 432 | innerPair.getRight()).vlanId(); |
| 433 | |
| 434 | FlowRule.Builder outer = DefaultFlowRule.builder() |
| 435 | .forDevice(deviceId) |
| 436 | .fromApp(appId) |
| 437 | .forTable(QQ_TABLE) |
| 438 | .makePermanent() |
| 439 | .withPriority(fwd.priority()) |
| 440 | .withSelector(buildSelector(inPort, |
| 441 | Criteria.matchVlanId(cVlanId))) |
| 442 | .withTreatment(buildTreatment(outerPair.getLeft(), |
| 443 | outerPair.getRight(), |
| 444 | output)); |
| 445 | |
| 446 | applyRules(fwd, inner, outer); |
| 447 | |
| 448 | } |
| 449 | |
| 450 | private Instruction fetchOutput(ForwardingObjective fwd, String direction) { |
| 451 | Instruction output = fwd.treatment().allInstructions().stream() |
| 452 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 453 | .findFirst().orElse(null); |
| 454 | |
| 455 | if (output == null) { |
| 456 | log.error("OLT {} rule has no output", direction); |
| 457 | fail(fwd, ObjectiveError.BADPARAMS); |
| 458 | return null; |
| 459 | } |
| 460 | return output; |
| 461 | } |
| 462 | |
| 463 | private List<Pair<Instruction, Instruction>> vlanOps(ForwardingObjective fwd, |
| 464 | L2ModificationInstruction.L2SubType type) { |
| 465 | |
| 466 | List<Pair<Instruction, Instruction>> vlanOps = findVlanOps( |
| 467 | fwd.treatment().allInstructions(), type); |
| 468 | |
| 469 | if (vlanOps == null) { |
| 470 | String direction = type == L2ModificationInstruction.L2SubType.VLAN_POP |
| 471 | ? "downstream" : "upstream"; |
| 472 | log.error("Missing vlan operations in {} forwarding: {}", direction, fwd); |
| 473 | fail(fwd, ObjectiveError.BADPARAMS); |
| 474 | return null; |
| 475 | } |
| 476 | return vlanOps; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | private List<Pair<Instruction, Instruction>> findVlanOps(List<Instruction> instructions, |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 481 | L2ModificationInstruction.L2SubType type) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 482 | |
| 483 | List<Instruction> vlanPushs = findL2Instructions( |
| 484 | type, |
| 485 | instructions); |
| 486 | List<Instruction> vlanSets = findL2Instructions( |
| 487 | L2ModificationInstruction.L2SubType.VLAN_ID, |
| 488 | instructions); |
| 489 | |
| 490 | if (vlanPushs.size() != vlanSets.size()) { |
| 491 | return null; |
| 492 | } |
| 493 | |
| 494 | List<Pair<Instruction, Instruction>> pairs = Lists.newArrayList(); |
| 495 | |
| 496 | for (int i = 0; i < vlanPushs.size(); i++) { |
| 497 | pairs.add(new ImmutablePair<>(vlanPushs.get(i), vlanSets.get(i))); |
| 498 | } |
| 499 | return pairs; |
| 500 | } |
| 501 | |
| 502 | private List<Instruction> findL2Instructions(L2ModificationInstruction.L2SubType subType, |
| 503 | List<Instruction> actions) { |
| 504 | return actions.stream() |
| 505 | .filter(i -> i.type() == Instruction.Type.L2MODIFICATION) |
| 506 | .filter(i -> ((L2ModificationInstruction) i).subtype() == subType) |
| 507 | .collect(Collectors.toList()); |
| 508 | } |
| 509 | |
| 510 | private void provisionEapol(FilteringObjective filter, |
| 511 | EthTypeCriterion ethType, |
| 512 | Instructions.OutputInstruction output) { |
| 513 | |
| 514 | TrafficSelector selector = buildSelector(filter.key(), ethType); |
| 515 | TrafficTreatment treatment = buildTreatment(output); |
| 516 | buildAndApplyRule(filter, selector, treatment); |
| 517 | |
| 518 | } |
| 519 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 520 | private void provisionIgmp(FilteringObjective filter, EthTypeCriterion ethType, |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 521 | IPProtocolCriterion ipProto, |
| 522 | Instructions.OutputInstruction output) { |
| 523 | TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto); |
| 524 | TrafficTreatment treatment = buildTreatment(output); |
| 525 | buildAndApplyRule(filter, selector, treatment); |
| 526 | } |
| 527 | |
| 528 | private void buildAndApplyRule(FilteringObjective filter, TrafficSelector selector, |
| 529 | TrafficTreatment treatment) { |
| 530 | FlowRule rule = DefaultFlowRule.builder() |
| 531 | .forDevice(deviceId) |
| 532 | .forTable(0) |
| 533 | .fromApp(filter.appId()) |
| 534 | .makePermanent() |
| 535 | .withSelector(selector) |
| 536 | .withTreatment(treatment) |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 537 | .withPriority(filter.priority()) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 538 | .build(); |
| 539 | |
| 540 | FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder(); |
| 541 | |
| 542 | switch (filter.type()) { |
| 543 | case PERMIT: |
| 544 | opsBuilder.add(rule); |
| 545 | break; |
| 546 | case DENY: |
| 547 | opsBuilder.remove(rule); |
| 548 | break; |
| 549 | default: |
| 550 | log.warn("Unknown filter type : {}", filter.type()); |
| 551 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 552 | } |
| 553 | |
| 554 | applyFlowRules(opsBuilder, filter); |
| 555 | } |
| 556 | |
| 557 | private void applyRules(ForwardingObjective fwd, |
| 558 | FlowRule.Builder inner, FlowRule.Builder outer) { |
| 559 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
| 560 | switch (fwd.op()) { |
| 561 | case ADD: |
| 562 | builder.add(inner.build()).add(outer.build()); |
| 563 | break; |
| 564 | case REMOVE: |
| 565 | builder.remove(inner.build()).remove(outer.build()); |
| 566 | break; |
| 567 | case ADD_TO_EXISTING: |
| 568 | break; |
| 569 | case REMOVE_FROM_EXISTING: |
| 570 | break; |
| 571 | default: |
| 572 | log.warn("Unknown forwarding operation: {}", fwd.op()); |
| 573 | } |
| 574 | |
| 575 | applyFlowRules(builder, fwd); |
| 576 | } |
| 577 | |
| 578 | private void applyFlowRules(FlowRuleOperations.Builder builder, |
| 579 | Objective objective) { |
| 580 | flowRuleService.apply(builder.build(new FlowRuleOperationsContext() { |
| 581 | @Override |
| 582 | public void onSuccess(FlowRuleOperations ops) { |
| 583 | pass(objective); |
| 584 | } |
| 585 | |
| 586 | @Override |
| 587 | public void onError(FlowRuleOperations ops) { |
| 588 | fail(objective, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 589 | } |
| 590 | })); |
| 591 | } |
| 592 | |
| 593 | private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) { |
| 594 | return criteria.stream() |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 595 | .filter(c -> c.type().equals(type)) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 596 | .limit(1) |
| 597 | .findFirst().orElse(null); |
| 598 | } |
| 599 | |
| 600 | private TrafficSelector buildSelector(Criterion... criteria) { |
| 601 | |
| 602 | |
| 603 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 604 | |
| 605 | for (Criterion c : criteria) { |
| 606 | sBuilder.add(c); |
| 607 | } |
| 608 | |
| 609 | return sBuilder.build(); |
| 610 | } |
| 611 | |
| 612 | private TrafficTreatment buildTreatment(Instruction... instructions) { |
| 613 | |
| 614 | |
| 615 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 616 | |
| 617 | for (Instruction i : instructions) { |
| 618 | tBuilder.add(i); |
| 619 | } |
| 620 | |
| 621 | return tBuilder.build(); |
| 622 | } |
| 623 | |
| 624 | |
| 625 | private void fail(Objective obj, ObjectiveError error) { |
| 626 | if (obj.context().isPresent()) { |
| 627 | obj.context().get().onError(obj, error); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | private void pass(Objective obj) { |
| 632 | if (obj.context().isPresent()) { |
| 633 | obj.context().get().onSuccess(obj); |
| 634 | } |
| 635 | } |
| 636 | |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 637 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 638 | private class InnerGroupListener implements GroupListener { |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 639 | @Override |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 640 | public void event(GroupEvent event) { |
| 641 | if (event.type() == GroupEvent.Type.GROUP_ADDED) { |
| 642 | GroupKey key = event.subject().appCookie(); |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 643 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 644 | NextObjective obj = pendingGroups.getIfPresent(key); |
| 645 | if (obj != null) { |
| 646 | flowObjectiveStore.putNextGroup(obj.id(), new OLTPipelineGroup(key)); |
| 647 | pass(obj); |
| 648 | pendingGroups.invalidate(key); |
| 649 | } |
| 650 | } |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame^] | 654 | private static class OLTPipelineGroup implements NextGroup { |
| 655 | |
| 656 | private final GroupKey key; |
| 657 | |
| 658 | public OLTPipelineGroup(GroupKey key) { |
| 659 | this.key = key; |
| 660 | } |
| 661 | |
| 662 | public GroupKey key() { |
| 663 | return key; |
| 664 | } |
| 665 | |
| 666 | @Override |
| 667 | public byte[] data() { |
| 668 | return appKryo.serialize(key); |
| 669 | } |
| 670 | |
| 671 | } |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 672 | } |