alshabib | aebe775 | 2015-04-07 17:45:42 -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 | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalCause; |
| 21 | import com.google.common.cache.RemovalNotification; |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 22 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 23 | import org.onlab.osgi.ServiceDirectory; |
| 24 | import org.onlab.packet.Ethernet; |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IPv4; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 28 | import org.onlab.util.KryoNamespace; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.net.DeviceId; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 32 | import org.onosproject.net.behaviour.NextGroup; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 33 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 34 | import org.onosproject.net.behaviour.PipelinerContext; |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 35 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flow.DefaultFlowRule; |
| 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 39 | import org.onosproject.net.flow.FlowRule; |
| 40 | import org.onosproject.net.flow.FlowRuleOperations; |
| 41 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 42 | import org.onosproject.net.flow.FlowRuleService; |
| 43 | import org.onosproject.net.flow.TrafficSelector; |
| 44 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 45 | import org.onosproject.net.flow.criteria.Criteria; |
| 46 | import org.onosproject.net.flow.criteria.Criterion; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 48 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 49 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 50 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 51 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 52 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 53 | import org.onosproject.net.flowobjective.FilteringObjective; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 54 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 55 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 56 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 57 | import org.onosproject.net.flowobjective.Objective; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 58 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 59 | import org.onosproject.net.group.DefaultGroupBucket; |
| 60 | import org.onosproject.net.group.DefaultGroupDescription; |
| 61 | import org.onosproject.net.group.DefaultGroupKey; |
| 62 | import org.onosproject.net.group.Group; |
| 63 | import org.onosproject.net.group.GroupBucket; |
| 64 | import org.onosproject.net.group.GroupBuckets; |
| 65 | import org.onosproject.net.group.GroupDescription; |
| 66 | import org.onosproject.net.group.GroupEvent; |
| 67 | import org.onosproject.net.group.GroupKey; |
| 68 | import org.onosproject.net.group.GroupListener; |
| 69 | import org.onosproject.net.group.GroupService; |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 70 | import org.onosproject.net.meter.MeterService; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 71 | import org.slf4j.Logger; |
| 72 | |
| 73 | import java.util.Collection; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 74 | import java.util.Collections; |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 75 | import java.util.Objects; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 76 | import java.util.Set; |
| 77 | import java.util.concurrent.Executors; |
| 78 | import java.util.concurrent.ScheduledExecutorService; |
| 79 | import java.util.concurrent.TimeUnit; |
| 80 | import java.util.stream.Collectors; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 81 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 82 | import static org.onlab.util.Tools.groupedThreads; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 83 | import static org.slf4j.LoggerFactory.getLogger; |
| 84 | |
| 85 | /** |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 86 | * OpenvSwitch emulation of the Corsa pipeline handler. |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 87 | */ |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 88 | public class OVSCorsaPipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 89 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 90 | protected static final int MAC_TABLE = 0; |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 91 | protected static final int VLAN_MPLS_TABLE = 1; |
| 92 | protected static final int VLAN_TABLE = 2; |
| 93 | //protected static final int MPLS_TABLE = 3; |
| 94 | protected static final int ETHER_TABLE = 4; |
| 95 | protected static final int COS_MAP_TABLE = 5; |
| 96 | protected static final int FIB_TABLE = 6; |
| 97 | protected static final int LOCAL_TABLE = 9; |
| 98 | |
| 99 | |
Saurav Das | d8b9700 | 2015-05-14 23:42:49 -0700 | [diff] [blame] | 100 | protected static final int CONTROLLER_PRIORITY = 255; |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 101 | protected static final int DROP_PRIORITY = 0; |
| 102 | protected static final int HIGHEST_PRIORITY = 0xffff; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 103 | |
| 104 | private final Logger log = getLogger(getClass()); |
| 105 | |
| 106 | private ServiceDirectory serviceDirectory; |
Saurav Das | d8b9700 | 2015-05-14 23:42:49 -0700 | [diff] [blame] | 107 | protected FlowRuleService flowRuleService; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 108 | private CoreService coreService; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 109 | private GroupService groupService; |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 110 | protected MeterService meterService; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 111 | private FlowObjectiveStore flowObjectiveStore; |
Saurav Das | d8b9700 | 2015-05-14 23:42:49 -0700 | [diff] [blame] | 112 | protected DeviceId deviceId; |
| 113 | protected ApplicationId appId; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 114 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 115 | private KryoNamespace appKryo = new KryoNamespace.Builder() |
| 116 | .register(GroupKey.class) |
| 117 | .register(DefaultGroupKey.class) |
| 118 | .register(CorsaGroup.class) |
| 119 | .register(byte[].class) |
| 120 | .build(); |
| 121 | |
| 122 | private Cache<GroupKey, NextObjective> pendingGroups; |
| 123 | |
| 124 | private ScheduledExecutorService groupChecker = |
| 125 | Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", |
| 126 | "ovs-corsa-%d")); |
| 127 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 128 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 129 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 130 | this.serviceDirectory = context.directory(); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 131 | this.deviceId = deviceId; |
| 132 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 133 | pendingGroups = CacheBuilder.newBuilder() |
| 134 | .expireAfterWrite(20, TimeUnit.SECONDS) |
| 135 | .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> { |
| 136 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 137 | fail(notification.getValue(), ObjectiveError.GROUPINSTALLATIONFAILED); |
| 138 | } |
| 139 | }).build(); |
| 140 | |
| 141 | groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 142 | |
| 143 | coreService = serviceDirectory.get(CoreService.class); |
| 144 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 145 | groupService = serviceDirectory.get(GroupService.class); |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 146 | meterService = serviceDirectory.get(MeterService.class); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 147 | flowObjectiveStore = context.store(); |
| 148 | |
| 149 | groupService.addListener(new InnerGroupListener()); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 150 | |
| 151 | appId = coreService.registerApplication( |
| 152 | "org.onosproject.driver.OVSCorsaPipeline"); |
| 153 | |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 154 | initializePipeline(); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 158 | public void filter(FilteringObjective filteringObjective) { |
| 159 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 160 | processFilter(filteringObjective, |
| 161 | filteringObjective.op() == Objective.Operation.ADD, |
| 162 | filteringObjective.appId()); |
| 163 | } else { |
| 164 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 165 | } |
| 166 | } |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 167 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public void forward(ForwardingObjective fwd) { |
| 170 | Collection<FlowRule> rules; |
| 171 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 172 | |
| 173 | rules = processForward(fwd); |
| 174 | switch (fwd.op()) { |
| 175 | case ADD: |
| 176 | rules.stream() |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 177 | .filter(Objects::nonNull) |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 178 | .forEach(flowBuilder::add); |
| 179 | break; |
| 180 | case REMOVE: |
| 181 | rules.stream() |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 182 | .filter(Objects::nonNull) |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 183 | .forEach(flowBuilder::remove); |
| 184 | break; |
| 185 | default: |
| 186 | fail(fwd, ObjectiveError.UNKNOWN); |
| 187 | log.warn("Unknown forwarding type {}", fwd.op()); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 192 | @Override |
| 193 | public void onSuccess(FlowRuleOperations ops) { |
| 194 | pass(fwd); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void onError(FlowRuleOperations ops) { |
| 199 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 200 | } |
| 201 | })); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 202 | |
| 203 | } |
| 204 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 205 | @Override |
| 206 | public void next(NextObjective nextObjective) { |
| 207 | switch (nextObjective.type()) { |
| 208 | case SIMPLE: |
| 209 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 210 | if (treatments.size() == 1) { |
| 211 | TrafficTreatment treatment = treatments.iterator().next(); |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 212 | treatment = processNextTreatment(treatment); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 213 | GroupBucket bucket = |
| 214 | DefaultGroupBucket.createIndirectGroupBucket(treatment); |
| 215 | final GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id())); |
| 216 | GroupDescription groupDescription |
| 217 | = new DefaultGroupDescription(deviceId, |
| 218 | GroupDescription.Type.INDIRECT, |
| 219 | new GroupBuckets(Collections |
| 220 | .singletonList(bucket)), |
| 221 | key, |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 222 | null, // let group service determine group id |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 223 | nextObjective.appId()); |
| 224 | groupService.addGroup(groupDescription); |
| 225 | pendingGroups.put(key, nextObjective); |
| 226 | } |
| 227 | break; |
| 228 | case HASHED: |
| 229 | case BROADCAST: |
| 230 | case FAILOVER: |
| 231 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
| 232 | log.warn("Unsupported next objective type {}", nextObjective.type()); |
| 233 | break; |
| 234 | default: |
| 235 | fail(nextObjective, ObjectiveError.UNKNOWN); |
| 236 | log.warn("Unknown next objective type {}", nextObjective.type()); |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 241 | /* Hook for altering the NextObjective treatment */ |
| 242 | protected TrafficTreatment processNextTreatment(TrafficTreatment treatment) { |
| 243 | return treatment; /* Keep treatment as is for OVSCorsaPipeline */ |
| 244 | } |
| 245 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 246 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 247 | switch (fwd.flag()) { |
| 248 | case SPECIFIC: |
| 249 | return processSpecific(fwd); |
| 250 | case VERSATILE: |
| 251 | return processVersatile(fwd); |
| 252 | default: |
| 253 | fail(fwd, ObjectiveError.UNKNOWN); |
| 254 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 255 | } |
| 256 | return Collections.emptySet(); |
| 257 | } |
| 258 | |
| 259 | private Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 260 | log.debug("Processing versatile forwarding objective"); |
| 261 | TrafficSelector selector = fwd.selector(); |
| 262 | |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 263 | EthTypeCriterion ethType = |
| 264 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 265 | if (ethType == null) { |
| 266 | log.error("Versatile forwarding objective must include ethType"); |
| 267 | fail(fwd, ObjectiveError.UNKNOWN); |
| 268 | return Collections.emptySet(); |
| 269 | } |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 270 | if (ethType.ethType().toShort() == Ethernet.TYPE_ARP) { |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 271 | log.warn("Driver automatically handles ARP packets by punting to controller " |
| 272 | + " from ETHER table"); |
| 273 | pass(fwd); |
| 274 | return Collections.emptySet(); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 275 | } else if (ethType.ethType().toShort() == Ethernet.TYPE_LLDP || |
| 276 | ethType.ethType().toShort() == Ethernet.TYPE_BSN) { |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 277 | log.warn("Driver currently does not currently handle LLDP packets"); |
| 278 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 279 | return Collections.emptySet(); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 280 | } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 281 | IPCriterion ipSrc = (IPCriterion) selector |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 282 | .getCriterion(Criterion.Type.IPV4_SRC); |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 283 | IPCriterion ipDst = (IPCriterion) selector |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 284 | .getCriterion(Criterion.Type.IPV4_DST); |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 285 | IPProtocolCriterion ipProto = (IPProtocolCriterion) selector |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 286 | .getCriterion(Criterion.Type.IP_PROTO); |
| 287 | if (ipSrc != null) { |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 288 | log.warn("Driver does not currently handle matching Src IP"); |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 289 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 290 | return Collections.emptySet(); |
| 291 | } |
| 292 | if (ipDst != null) { |
| 293 | log.error("Driver handles Dst IP matching as specific forwarding " |
| 294 | + "objective, not versatile"); |
| 295 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 296 | return Collections.emptySet(); |
| 297 | } |
| 298 | if (ipProto != null && ipProto.protocol() == IPv4.PROTOCOL_TCP) { |
| 299 | log.warn("Driver automatically punts all packets reaching the " |
| 300 | + "LOCAL table to the controller"); |
| 301 | pass(fwd); |
| 302 | return Collections.emptySet(); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | log.warn("Driver does not support given versatile forwarding objective"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 307 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 308 | return Collections.emptySet(); |
| 309 | } |
| 310 | |
| 311 | private Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 312 | log.debug("Processing specific forwarding objective"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 313 | TrafficSelector selector = fwd.selector(); |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 314 | |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 315 | EthTypeCriterion ethType = |
| 316 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 317 | if (ethType != null) { |
| 318 | short et = ethType.ethType().toShort(); |
| 319 | if (et == Ethernet.TYPE_IPV4) { |
| 320 | return processSpecificRoute(fwd); |
| 321 | } else if (et == Ethernet.TYPE_VLAN) { |
| 322 | /* The ForwardingObjective must specify VLAN ethtype in order to use the Transit Circuit */ |
| 323 | return processSpecificSwitch(fwd); |
| 324 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 327 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 328 | return Collections.emptySet(); |
| 329 | } |
| 330 | |
| 331 | private Collection<FlowRule> processSpecificRoute(ForwardingObjective fwd) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 332 | TrafficSelector filteredSelector = |
| 333 | DefaultTrafficSelector.builder() |
| 334 | .matchEthType(Ethernet.TYPE_IPV4) |
| 335 | .matchIPDst( |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 336 | ((IPCriterion) fwd.selector().getCriterion(Criterion.Type.IPV4_DST)).ip()) |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 337 | .build(); |
| 338 | |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 339 | TrafficTreatment.Builder tb = processSpecificRoutingTreatment(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 340 | |
sangho | dde53d1 | 2015-04-30 10:34:41 -0700 | [diff] [blame] | 341 | if (fwd.nextId() != null) { |
| 342 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 343 | GroupKey key = appKryo.deserialize(next.data()); |
| 344 | Group group = groupService.getGroup(deviceId, key); |
| 345 | if (group == null) { |
| 346 | log.warn("The group left!"); |
| 347 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 348 | return Collections.emptySet(); |
| 349 | } |
| 350 | tb.group(group.id()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 351 | } |
| 352 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 353 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 354 | .fromApp(fwd.appId()) |
| 355 | .withPriority(fwd.priority()) |
| 356 | .forDevice(deviceId) |
| 357 | .withSelector(filteredSelector) |
sangho | dde53d1 | 2015-04-30 10:34:41 -0700 | [diff] [blame] | 358 | .withTreatment(tb.build()); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 359 | |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 360 | ruleBuilder = processSpecificRoutingRule(ruleBuilder); |
| 361 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 362 | if (fwd.permanent()) { |
| 363 | ruleBuilder.makePermanent(); |
| 364 | } else { |
| 365 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 366 | } |
| 367 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 368 | return Collections.singletonList(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 371 | /* Hook for modifying Route traffic treatment */ |
| 372 | protected TrafficTreatment.Builder processSpecificRoutingTreatment() { |
| 373 | return DefaultTrafficTreatment.builder(); |
| 374 | } |
| 375 | |
| 376 | /* Hook for modifying Route flow rule */ |
| 377 | protected FlowRule.Builder processSpecificRoutingRule(FlowRule.Builder rb) { |
| 378 | return rb.forTable(FIB_TABLE); |
| 379 | } |
| 380 | |
| 381 | protected Collection<FlowRule> processSpecificSwitch(ForwardingObjective fwd) { |
| 382 | /* Not supported by until CorsaPipelineV3 */ |
| 383 | log.warn("Vlan switching not supported in ovs-corsa driver"); |
| 384 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 385 | return Collections.emptySet(); |
| 386 | } |
| 387 | |
| 388 | protected void processFilter(FilteringObjective filt, boolean install, |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 389 | ApplicationId applicationId) { |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 390 | // This driver only processes filtering criteria defined with switch |
| 391 | // ports as the key |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 392 | PortCriterion p; |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 393 | if (!filt.key().equals(Criteria.dummy()) && |
| 394 | filt.key().type() == Criterion.Type.IN_PORT) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 395 | p = (PortCriterion) filt.key(); |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 396 | } else { |
| 397 | log.warn("No key defined in filtering objective from app: {}. Not" |
| 398 | + "processing filtering objective", applicationId); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 399 | fail(filt, ObjectiveError.UNKNOWN); |
| 400 | return; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 401 | } |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 402 | // convert filtering conditions for switch-intfs into flowrules |
| 403 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 404 | for (Criterion c : filt.conditions()) { |
| 405 | if (c.type() == Criterion.Type.ETH_DST) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 406 | EthCriterion e = (EthCriterion) c; |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 407 | log.debug("adding rule for MAC: {}", e.mac()); |
| 408 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 409 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 410 | selector.matchEthDst(e.mac()); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 411 | treatment.transition(VLAN_MPLS_TABLE); |
| 412 | FlowRule rule = DefaultFlowRule.builder() |
| 413 | .forDevice(deviceId) |
| 414 | .withSelector(selector.build()) |
| 415 | .withTreatment(treatment.build()) |
| 416 | .withPriority(CONTROLLER_PRIORITY) |
| 417 | .fromApp(applicationId) |
| 418 | .makePermanent() |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 419 | .forTable(MAC_TABLE).build(); |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 420 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 421 | } else if (c.type() == Criterion.Type.VLAN_VID) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 422 | VlanIdCriterion v = (VlanIdCriterion) c; |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 423 | log.debug("adding rule for VLAN: {}", v.vlanId()); |
| 424 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 425 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 426 | selector.matchVlanId(v.vlanId()); |
| 427 | selector.matchInPort(p.port()); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 428 | treatment.transition(ETHER_TABLE); |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 429 | treatment.deferred().popVlan(); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 430 | FlowRule rule = DefaultFlowRule.builder() |
| 431 | .forDevice(deviceId) |
| 432 | .withSelector(selector.build()) |
| 433 | .withTreatment(treatment.build()) |
| 434 | .withPriority(CONTROLLER_PRIORITY) |
| 435 | .fromApp(applicationId) |
| 436 | .makePermanent() |
| 437 | .forTable(VLAN_TABLE).build(); |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 438 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 439 | } else if (c.type() == Criterion.Type.IPV4_DST) { |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 440 | IPCriterion ip = (IPCriterion) c; |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 441 | log.debug("adding rule for IP: {}", ip.ip()); |
| 442 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 443 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 444 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 445 | selector.matchIPDst(ip.ip()); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 446 | treatment.transition(LOCAL_TABLE); |
| 447 | FlowRule rule = DefaultFlowRule.builder() |
| 448 | .forDevice(deviceId) |
| 449 | .withSelector(selector.build()) |
| 450 | .withTreatment(treatment.build()) |
| 451 | .withPriority(HIGHEST_PRIORITY) |
| 452 | .fromApp(applicationId) |
| 453 | .makePermanent() |
| 454 | .forTable(FIB_TABLE).build(); |
| 455 | |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 456 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 457 | } else { |
| 458 | log.warn("Driver does not currently process filtering condition" |
| 459 | + " of type: {}", c.type()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 460 | fail(filt, ObjectiveError.UNSUPPORTED); |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | // apply filtering flow rules |
| 464 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 465 | @Override |
| 466 | public void onSuccess(FlowRuleOperations ops) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 467 | pass(filt); |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 468 | log.info("Applied filtering rules"); |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public void onError(FlowRuleOperations ops) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 473 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 474 | log.info("Failed to apply filtering rules"); |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 475 | } |
| 476 | })); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 479 | protected void pass(Objective obj) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 480 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 483 | protected void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 484 | obj.context().ifPresent(context -> context.onError(obj, error)); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Jonathan Gravel | 8c9f7bc | 2015-12-03 15:46:20 -0500 | [diff] [blame] | 487 | protected void initializePipeline() { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 488 | processMacTable(true); |
| 489 | processVlanMplsTable(true); |
| 490 | processVlanTable(true); |
| 491 | processEtherTable(true); |
| 492 | processCosTable(true); |
| 493 | processFibTable(true); |
| 494 | processLocalTable(true); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 497 | private void processMacTable(boolean install) { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 498 | TrafficSelector.Builder selector; |
| 499 | TrafficTreatment.Builder treatment; |
| 500 | |
| 501 | // Bcast rule |
| 502 | selector = DefaultTrafficSelector.builder(); |
| 503 | treatment = DefaultTrafficTreatment.builder(); |
| 504 | |
| 505 | selector.matchEthDst(MacAddress.BROADCAST); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 506 | treatment.transition(VLAN_MPLS_TABLE); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 507 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 508 | FlowRule rule = DefaultFlowRule.builder() |
| 509 | .forDevice(deviceId) |
| 510 | .withSelector(selector.build()) |
| 511 | .withTreatment(treatment.build()) |
| 512 | .withPriority(CONTROLLER_PRIORITY) |
| 513 | .fromApp(appId) |
| 514 | .makePermanent() |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 515 | .forTable(MAC_TABLE).build(); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 516 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 517 | |
| 518 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 519 | |
| 520 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 521 | |
| 522 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 523 | //Drop rule |
| 524 | selector = DefaultTrafficSelector.builder(); |
| 525 | treatment = DefaultTrafficTreatment.builder(); |
| 526 | |
| 527 | treatment.drop(); |
| 528 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 529 | rule = DefaultFlowRule.builder() |
| 530 | .forDevice(deviceId) |
| 531 | .withSelector(selector.build()) |
| 532 | .withTreatment(treatment.build()) |
| 533 | .withPriority(DROP_PRIORITY) |
| 534 | .fromApp(appId) |
| 535 | .makePermanent() |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 536 | .forTable(MAC_TABLE).build(); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 537 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 538 | |
| 539 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 540 | |
| 541 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 542 | @Override |
| 543 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 544 | log.info("Provisioned mac table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public void onError(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 549 | log.info("Failed to provision mac table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 550 | } |
| 551 | })); |
| 552 | |
| 553 | } |
| 554 | |
Saurav Das | d8b9700 | 2015-05-14 23:42:49 -0700 | [diff] [blame] | 555 | protected void processVlanMplsTable(boolean install) { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 556 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 557 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 558 | .builder(); |
| 559 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 560 | FlowRule rule; |
| 561 | |
| 562 | selector.matchVlanId(VlanId.ANY); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 563 | treatment.transition(VLAN_TABLE); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 564 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 565 | rule = DefaultFlowRule.builder() |
| 566 | .forDevice(deviceId) |
| 567 | .withSelector(selector.build()) |
| 568 | .withTreatment(treatment.build()) |
| 569 | .withPriority(CONTROLLER_PRIORITY) |
| 570 | .fromApp(appId) |
| 571 | .makePermanent() |
| 572 | .forTable(VLAN_MPLS_TABLE).build(); |
| 573 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 574 | |
| 575 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 576 | |
| 577 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 578 | @Override |
| 579 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 580 | log.info("Provisioned vlan/mpls table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | @Override |
| 584 | public void onError(FlowRuleOperations ops) { |
| 585 | log.info( |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 586 | "Failed to provision vlan/mpls table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 587 | } |
| 588 | })); |
| 589 | |
| 590 | } |
| 591 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 592 | private void processVlanTable(boolean install) { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 593 | TrafficSelector.Builder selector; |
| 594 | TrafficTreatment.Builder treatment; |
| 595 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 596 | FlowRule rule; |
| 597 | |
| 598 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 599 | //Drop rule |
| 600 | selector = DefaultTrafficSelector.builder(); |
| 601 | treatment = DefaultTrafficTreatment.builder(); |
| 602 | |
| 603 | treatment.drop(); |
| 604 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 605 | rule = DefaultFlowRule.builder() |
| 606 | .forDevice(deviceId) |
| 607 | .withSelector(selector.build()) |
| 608 | .withTreatment(treatment.build()) |
| 609 | .withPriority(DROP_PRIORITY) |
| 610 | .fromApp(appId) |
| 611 | .makePermanent() |
| 612 | .forTable(VLAN_TABLE).build(); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 613 | |
| 614 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 615 | |
| 616 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 617 | @Override |
| 618 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 619 | log.info("Provisioned vlan table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public void onError(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 624 | log.info("Failed to provision vlan table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 625 | } |
| 626 | })); |
| 627 | } |
| 628 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 629 | private void processEtherTable(boolean install) { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 630 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 631 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 632 | .builder(); |
| 633 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 634 | FlowRule rule; |
| 635 | |
| 636 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 637 | treatment.punt(); |
| 638 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 639 | rule = DefaultFlowRule.builder() |
| 640 | .forDevice(deviceId) |
| 641 | .withSelector(selector.build()) |
| 642 | .withTreatment(treatment.build()) |
| 643 | .withPriority(CONTROLLER_PRIORITY) |
| 644 | .fromApp(appId) |
| 645 | .makePermanent() |
| 646 | .forTable(ETHER_TABLE).build(); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 647 | |
| 648 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 649 | |
| 650 | selector = DefaultTrafficSelector.builder(); |
| 651 | treatment = DefaultTrafficTreatment.builder(); |
| 652 | |
| 653 | selector.matchEthType(Ethernet.TYPE_IPV4); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 654 | treatment.transition(COS_MAP_TABLE); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 655 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 656 | rule = DefaultFlowRule.builder() |
| 657 | .forDevice(deviceId) |
| 658 | .withPriority(CONTROLLER_PRIORITY) |
| 659 | .withSelector(selector.build()) |
| 660 | .withTreatment(treatment.build()) |
| 661 | .fromApp(appId) |
| 662 | .makePermanent() |
| 663 | .forTable(ETHER_TABLE).build(); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 664 | |
| 665 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 666 | |
| 667 | //Drop rule |
| 668 | selector = DefaultTrafficSelector.builder(); |
| 669 | treatment = DefaultTrafficTreatment.builder(); |
| 670 | |
| 671 | treatment.drop(); |
| 672 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 673 | rule = DefaultFlowRule.builder() |
| 674 | .forDevice(deviceId) |
| 675 | .withSelector(selector.build()) |
| 676 | .withTreatment(treatment.build()) |
| 677 | .withPriority(DROP_PRIORITY) |
| 678 | .fromApp(appId) |
| 679 | .makePermanent() |
| 680 | .forTable(ETHER_TABLE).build(); |
| 681 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 682 | |
| 683 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 684 | |
| 685 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 686 | @Override |
| 687 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 688 | log.info("Provisioned ether table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | @Override |
| 692 | public void onError(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 693 | log.info("Failed to provision ether table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 694 | } |
| 695 | })); |
| 696 | |
| 697 | } |
| 698 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 699 | private void processCosTable(boolean install) { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 700 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 701 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 702 | .builder(); |
| 703 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 704 | FlowRule rule; |
| 705 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 706 | treatment.transition(FIB_TABLE); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 707 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 708 | rule = DefaultFlowRule.builder() |
| 709 | .forDevice(deviceId) |
| 710 | .withSelector(selector.build()) |
| 711 | .withTreatment(treatment.build()) |
| 712 | .withPriority(DROP_PRIORITY) |
| 713 | .fromApp(appId) |
| 714 | .makePermanent() |
| 715 | .forTable(COS_MAP_TABLE).build(); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 716 | |
| 717 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 718 | |
| 719 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 720 | @Override |
| 721 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 722 | log.info("Provisioned cos table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | @Override |
| 726 | public void onError(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 727 | log.info("Failed to provision cos table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 728 | } |
| 729 | })); |
| 730 | |
| 731 | } |
| 732 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 733 | private void processFibTable(boolean install) { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 734 | TrafficSelector.Builder selector; |
| 735 | TrafficTreatment.Builder treatment; |
| 736 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 737 | FlowRule rule; |
| 738 | |
| 739 | //Drop rule |
| 740 | selector = DefaultTrafficSelector.builder(); |
| 741 | treatment = DefaultTrafficTreatment.builder(); |
| 742 | |
| 743 | treatment.drop(); |
| 744 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 745 | rule = DefaultFlowRule.builder() |
| 746 | .forDevice(deviceId) |
| 747 | .withSelector(selector.build()) |
| 748 | .withTreatment(treatment.build()) |
| 749 | .withPriority(DROP_PRIORITY) |
| 750 | .fromApp(appId) |
| 751 | .makePermanent() |
| 752 | .forTable(FIB_TABLE).build(); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 753 | |
| 754 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 755 | |
| 756 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 757 | @Override |
| 758 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 759 | log.info("Provisioned FIB table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | @Override |
| 763 | public void onError(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 764 | log.info("Failed to provision FIB table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 765 | } |
| 766 | })); |
| 767 | } |
| 768 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 769 | private void processLocalTable(boolean install) { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 770 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 771 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 772 | .builder(); |
| 773 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 774 | FlowRule rule; |
| 775 | |
| 776 | treatment.punt(); |
| 777 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 778 | rule = DefaultFlowRule.builder() |
| 779 | .forDevice(deviceId) |
| 780 | .withSelector(selector.build()) |
| 781 | .withTreatment(treatment.build()) |
| 782 | .withPriority(CONTROLLER_PRIORITY) |
| 783 | .fromApp(appId) |
| 784 | .makePermanent() |
| 785 | .forTable(LOCAL_TABLE).build(); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 786 | |
| 787 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 788 | |
| 789 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 790 | @Override |
| 791 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 792 | log.info("Provisioned Local table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | @Override |
| 796 | public void onError(FlowRuleOperations ops) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 797 | log.info("Failed to provision Local table"); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 798 | } |
| 799 | })); |
| 800 | } |
| 801 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 802 | private class InnerGroupListener implements GroupListener { |
| 803 | @Override |
| 804 | public void event(GroupEvent event) { |
| 805 | if (event.type() == GroupEvent.Type.GROUP_ADDED) { |
| 806 | GroupKey key = event.subject().appCookie(); |
| 807 | |
| 808 | NextObjective obj = pendingGroups.getIfPresent(key); |
| 809 | if (obj != null) { |
| 810 | flowObjectiveStore.putNextGroup(obj.id(), new CorsaGroup(key)); |
| 811 | pass(obj); |
| 812 | pendingGroups.invalidate(key); |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | |
| 819 | private class GroupChecker implements Runnable { |
| 820 | |
| 821 | @Override |
| 822 | public void run() { |
| 823 | Set<GroupKey> keys = pendingGroups.asMap().keySet().stream() |
| 824 | .filter(key -> groupService.getGroup(deviceId, key) != null) |
| 825 | .collect(Collectors.toSet()); |
| 826 | |
| 827 | keys.stream().forEach(key -> { |
| 828 | NextObjective obj = pendingGroups.getIfPresent(key); |
| 829 | if (obj == null) { |
| 830 | return; |
| 831 | } |
| 832 | pass(obj); |
| 833 | pendingGroups.invalidate(key); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 834 | log.info("Heard back from group service for group {}. " |
| 835 | + "Applying pending forwarding objectives", obj.id()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 836 | flowObjectiveStore.putNextGroup(obj.id(), new CorsaGroup(key)); |
| 837 | }); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | private class CorsaGroup implements NextGroup { |
| 842 | |
| 843 | private final GroupKey key; |
| 844 | |
| 845 | public CorsaGroup(GroupKey key) { |
| 846 | this.key = key; |
| 847 | } |
| 848 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 849 | @SuppressWarnings("unused") |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 850 | public GroupKey key() { |
| 851 | return key; |
| 852 | } |
| 853 | |
| 854 | @Override |
| 855 | public byte[] data() { |
| 856 | return appKryo.serialize(key); |
| 857 | } |
| 858 | |
| 859 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 860 | } |