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