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