Saurav Das | e3274c8 | 2015-05-24 17:21:56 -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 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 18 | import org.onlab.osgi.ServiceDirectory; |
| 19 | import org.onlab.packet.Ethernet; |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.VlanId; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.net.DeviceId; |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.NextGroup; |
| 28 | import org.onosproject.net.behaviour.Pipeliner; |
| 29 | import org.onosproject.net.behaviour.PipelinerContext; |
| 30 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 31 | import org.onosproject.net.flow.DefaultFlowRule; |
| 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onosproject.net.flow.FlowRule; |
| 35 | import org.onosproject.net.flow.FlowRuleOperations; |
| 36 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 37 | import org.onosproject.net.flow.FlowRuleService; |
| 38 | import org.onosproject.net.flow.TrafficSelector; |
| 39 | import org.onosproject.net.flow.TrafficTreatment; |
| 40 | import org.onosproject.net.flow.criteria.Criteria; |
| 41 | import org.onosproject.net.flow.criteria.Criterion; |
| 42 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 43 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 44 | import org.onosproject.net.flow.criteria.IPCriterion; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 45 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 46 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flow.instructions.Instruction; |
| 48 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 49 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 50 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 51 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 52 | import org.onosproject.net.flowobjective.NextObjective; |
| 53 | import org.onosproject.net.flowobjective.Objective; |
| 54 | import org.onosproject.net.flowobjective.ObjectiveError; |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 55 | import org.onosproject.store.serializers.KryoNamespaces; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 56 | import org.slf4j.Logger; |
| 57 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 58 | import java.util.ArrayList; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 59 | import java.util.Collection; |
| 60 | import java.util.Collections; |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 61 | import java.util.List; |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 62 | import java.util.Objects; |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 63 | import java.util.concurrent.ConcurrentHashMap; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 64 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
| 67 | /** |
| 68 | * Pica pipeline handler. |
| 69 | */ |
| 70 | public class PicaPipeline extends AbstractHandlerBehaviour implements Pipeliner { |
| 71 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 72 | protected static final int IP_UNICAST_TABLE = 252; |
| 73 | protected static final int ACL_TABLE = 0; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 74 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 75 | //private static final int CONTROLLER_PRIORITY = 255; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 76 | private static final int DROP_PRIORITY = 0; |
| 77 | private static final int HIGHEST_PRIORITY = 0xffff; |
| 78 | |
| 79 | private final Logger log = getLogger(getClass()); |
| 80 | |
| 81 | private ServiceDirectory serviceDirectory; |
| 82 | private FlowRuleService flowRuleService; |
| 83 | private CoreService coreService; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 84 | private FlowObjectiveStore flowObjectiveStore; |
| 85 | private DeviceId deviceId; |
| 86 | private ApplicationId appId; |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 87 | private Collection<Filter> filters; |
| 88 | private Collection<ForwardingObjective> pendingVersatiles; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 89 | |
| 90 | private KryoNamespace appKryo = new KryoNamespace.Builder() |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 91 | .register(KryoNamespaces.API) |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 92 | .register(PicaGroup.class) |
| 93 | .register(byte[].class) |
| 94 | .build(); |
| 95 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 96 | @Override |
| 97 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 98 | this.serviceDirectory = context.directory(); |
| 99 | this.deviceId = deviceId; |
| 100 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 101 | coreService = serviceDirectory.get(CoreService.class); |
| 102 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 103 | flowObjectiveStore = context.store(); |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 104 | filters = Collections.newSetFromMap(new ConcurrentHashMap<Filter, Boolean>()); |
| 105 | pendingVersatiles = Collections.newSetFromMap( |
| 106 | new ConcurrentHashMap<ForwardingObjective, Boolean>()); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 107 | appId = coreService.registerApplication( |
| 108 | "org.onosproject.driver.OVSPicaPipeline"); |
| 109 | |
| 110 | initializePipeline(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void filter(FilteringObjective filteringObjective) { |
| 115 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 116 | processFilter(filteringObjective, |
| 117 | filteringObjective.op() == Objective.Operation.ADD, |
| 118 | filteringObjective.appId()); |
| 119 | } else { |
| 120 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void forward(ForwardingObjective fwd) { |
| 126 | Collection<FlowRule> rules; |
| 127 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 128 | |
| 129 | rules = processForward(fwd); |
| 130 | switch (fwd.op()) { |
| 131 | case ADD: |
| 132 | rules.stream() |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 133 | .filter(Objects::nonNull) |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 134 | .forEach(flowBuilder::add); |
| 135 | break; |
| 136 | case REMOVE: |
| 137 | rules.stream() |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 138 | .filter(Objects::nonNull) |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 139 | .forEach(flowBuilder::remove); |
| 140 | break; |
| 141 | default: |
| 142 | fail(fwd, ObjectiveError.UNKNOWN); |
| 143 | log.warn("Unknown forwarding type {}", fwd.op()); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 148 | @Override |
| 149 | public void onSuccess(FlowRuleOperations ops) { |
| 150 | pass(fwd); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void onError(FlowRuleOperations ops) { |
| 155 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 156 | } |
| 157 | })); |
| 158 | |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void next(NextObjective nextObjective) { |
| 163 | switch (nextObjective.type()) { |
| 164 | case SIMPLE: |
| 165 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 166 | if (treatments.size() != 1) { |
| 167 | log.error("Next Objectives of type Simple should only have a " |
| 168 | + "single Traffic Treatment. Next Objective Id:{}", nextObjective.id()); |
| 169 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 170 | return; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 171 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 172 | TrafficTreatment treatment = treatments.iterator().next(); |
| 173 | TrafficTreatment.Builder filteredTreatment = DefaultTrafficTreatment.builder(); |
| 174 | VlanId modVlanId; |
| 175 | for (Instruction ins : treatment.allInstructions()) { |
| 176 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 177 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 178 | switch (l2ins.subtype()) { |
| 179 | case ETH_DST: |
| 180 | filteredTreatment.setEthDst( |
| 181 | ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac()); |
| 182 | break; |
| 183 | case ETH_SRC: |
| 184 | filteredTreatment.setEthSrc( |
| 185 | ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac()); |
| 186 | break; |
| 187 | case VLAN_ID: |
| 188 | modVlanId = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId(); |
| 189 | filteredTreatment.setVlanId(modVlanId); |
| 190 | break; |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 195 | //long portNum = ((Instructions.OutputInstruction) ins).port().toLong(); |
| 196 | filteredTreatment.add(ins); |
| 197 | } else { |
| 198 | // Ignore the vlan_pcp action since it's does matter much. |
| 199 | log.warn("Driver does not handle this type of TrafficTreatment" |
| 200 | + " instruction in nextObjectives: {}", ins.type()); |
| 201 | } |
| 202 | } |
| 203 | // store for future use |
| 204 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
| 205 | new PicaGroup(filteredTreatment.build())); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 206 | break; |
| 207 | case HASHED: |
| 208 | case BROADCAST: |
| 209 | case FAILOVER: |
| 210 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
| 211 | log.warn("Unsupported next objective type {}", nextObjective.type()); |
| 212 | break; |
| 213 | default: |
| 214 | fail(nextObjective, ObjectiveError.UNKNOWN); |
| 215 | log.warn("Unknown next objective type {}", nextObjective.type()); |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | |
| 220 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 221 | switch (fwd.flag()) { |
| 222 | case SPECIFIC: |
| 223 | return processSpecific(fwd); |
| 224 | case VERSATILE: |
| 225 | return processVersatile(fwd); |
| 226 | default: |
| 227 | fail(fwd, ObjectiveError.UNKNOWN); |
| 228 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 229 | } |
| 230 | return Collections.emptySet(); |
| 231 | } |
| 232 | |
| 233 | private Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
| 234 | log.debug("Processing versatile forwarding objective"); |
| 235 | TrafficSelector selector = fwd.selector(); |
Saurav Das | 598a02f | 2015-08-31 18:19:30 -0700 | [diff] [blame] | 236 | TrafficTreatment treatment = fwd.treatment(); |
| 237 | Collection<FlowRule> flowrules = new ArrayList<FlowRule>(); |
| 238 | |
| 239 | // first add this rule for basic single-table operation |
| 240 | // or non-ARP related multi-table operation |
| 241 | FlowRule rule = DefaultFlowRule.builder() |
| 242 | .forDevice(deviceId) |
| 243 | .withSelector(selector) |
| 244 | .withTreatment(treatment) |
| 245 | .withPriority(fwd.priority()) |
| 246 | .fromApp(fwd.appId()) |
| 247 | .makePermanent() |
| 248 | .forTable(ACL_TABLE).build(); |
| 249 | flowrules.add(rule); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 250 | |
| 251 | EthTypeCriterion ethType = |
| 252 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
| 253 | if (ethType == null) { |
Saurav Das | 598a02f | 2015-08-31 18:19:30 -0700 | [diff] [blame] | 254 | log.warn("No ethType in versatile forwarding obj. Not processing further."); |
| 255 | return flowrules; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 256 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 257 | |
Saurav Das | 598a02f | 2015-08-31 18:19:30 -0700 | [diff] [blame] | 258 | // now deal with possible mix of ARP with filtering objectives |
| 259 | // in multi-table scenarios |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 260 | if (ethType.ethType().toShort() == Ethernet.TYPE_ARP) { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 261 | if (filters.isEmpty()) { |
| 262 | pendingVersatiles.add(fwd); |
Saurav Das | 598a02f | 2015-08-31 18:19:30 -0700 | [diff] [blame] | 263 | return flowrules; |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 264 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 265 | for (Filter filter : filters) { |
| 266 | flowrules.addAll(processVersatilesWithFilters(filter, fwd)); |
| 267 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 268 | } |
Saurav Das | 598a02f | 2015-08-31 18:19:30 -0700 | [diff] [blame] | 269 | return flowrules; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 272 | private Collection<FlowRule> processVersatilesWithFilters( |
| 273 | Filter filt, ForwardingObjective fwd) { |
| 274 | Collection<FlowRule> flows = new ArrayList<FlowRule>(); |
| 275 | |
| 276 | // rule for ARP replies |
| 277 | log.debug("adding ARP rule in ACL table"); |
| 278 | TrafficSelector.Builder sel = DefaultTrafficSelector.builder(); |
| 279 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 280 | sel.matchInPort(filt.port()); |
| 281 | sel.matchVlanId(filt.vlanId()); |
| 282 | sel.matchEthDst(filt.mac()); |
| 283 | sel.matchEthType(Ethernet.TYPE_ARP); |
| 284 | treat.setOutput(PortNumber.CONTROLLER); |
| 285 | FlowRule rule = DefaultFlowRule.builder() |
| 286 | .forDevice(deviceId) |
| 287 | .withSelector(sel.build()) |
| 288 | .withTreatment(treat.build()) |
| 289 | .withPriority(HIGHEST_PRIORITY) |
| 290 | .fromApp(appId) |
| 291 | .makePermanent() |
| 292 | .forTable(ACL_TABLE).build(); |
| 293 | flows.add(rule); |
| 294 | |
| 295 | // rule for ARP Broadcast |
| 296 | sel = DefaultTrafficSelector.builder(); |
| 297 | treat = DefaultTrafficTreatment.builder(); |
| 298 | sel.matchInPort(filt.port()); |
| 299 | sel.matchVlanId(filt.vlanId()); |
| 300 | sel.matchEthDst(MacAddress.BROADCAST); |
| 301 | sel.matchEthType(Ethernet.TYPE_ARP); |
| 302 | treat.setOutput(PortNumber.CONTROLLER); |
| 303 | rule = DefaultFlowRule.builder() |
| 304 | .forDevice(deviceId) |
| 305 | .withSelector(sel.build()) |
| 306 | .withTreatment(treat.build()) |
| 307 | .withPriority(HIGHEST_PRIORITY) |
| 308 | .fromApp(appId) |
| 309 | .makePermanent() |
| 310 | .forTable(ACL_TABLE).build(); |
| 311 | flows.add(rule); |
| 312 | |
| 313 | return flows; |
| 314 | } |
| 315 | |
| 316 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 317 | private Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
| 318 | log.debug("Processing specific forwarding objective"); |
| 319 | TrafficSelector selector = fwd.selector(); |
| 320 | EthTypeCriterion ethType = |
| 321 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 322 | if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) { |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 323 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 324 | return Collections.emptySet(); |
| 325 | } |
| 326 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 327 | List<FlowRule> ipflows = new ArrayList<FlowRule>(); |
| 328 | for (Filter f: filters) { |
| 329 | TrafficSelector filteredSelector = |
| 330 | DefaultTrafficSelector.builder() |
| 331 | .matchEthType(Ethernet.TYPE_IPV4) |
| 332 | .matchIPDst( |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 333 | ((IPCriterion) |
| 334 | selector.getCriterion(Criterion.Type.IPV4_DST)).ip()) |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 335 | .matchEthDst(f.mac()) |
| 336 | .matchVlanId(f.vlanId()) |
| 337 | .build(); |
| 338 | TrafficTreatment tt = null; |
| 339 | if (fwd.nextId() != null) { |
| 340 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 341 | if (next == null) { |
| 342 | log.error("next-id {} does not exist in store", fwd.nextId()); |
| 343 | return Collections.emptySet(); |
| 344 | } |
| 345 | tt = appKryo.deserialize(next.data()); |
| 346 | if (tt == null) { |
| 347 | log.error("Error in deserializing next-id {}", fwd.nextId()); |
| 348 | return Collections.emptySet(); |
| 349 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 350 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 351 | |
| 352 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 353 | .fromApp(fwd.appId()) |
| 354 | .withPriority(fwd.priority()) |
| 355 | .forDevice(deviceId) |
| 356 | .withSelector(filteredSelector) |
| 357 | .withTreatment(tt); |
| 358 | if (fwd.permanent()) { |
| 359 | ruleBuilder.makePermanent(); |
| 360 | } else { |
| 361 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 362 | } |
| 363 | ruleBuilder.forTable(IP_UNICAST_TABLE); |
| 364 | ipflows.add(ruleBuilder.build()); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 367 | return ipflows; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | private void processFilter(FilteringObjective filt, boolean install, |
| 371 | ApplicationId applicationId) { |
| 372 | // This driver only processes filtering criteria defined with switch |
| 373 | // ports as the key |
| 374 | PortCriterion p; |
| 375 | if (!filt.key().equals(Criteria.dummy()) && |
| 376 | filt.key().type() == Criterion.Type.IN_PORT) { |
| 377 | p = (PortCriterion) filt.key(); |
| 378 | } else { |
| 379 | log.warn("No key defined in filtering objective from app: {}. Not" |
| 380 | + "processing filtering objective", applicationId); |
| 381 | fail(filt, ObjectiveError.UNKNOWN); |
| 382 | return; |
| 383 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 384 | |
| 385 | EthCriterion e = null; VlanIdCriterion v = null; |
| 386 | Collection<IPCriterion> ips = new ArrayList<IPCriterion>(); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 387 | // convert filtering conditions for switch-intfs into flowrules |
| 388 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 389 | for (Criterion c : filt.conditions()) { |
| 390 | if (c.type() == Criterion.Type.ETH_DST) { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 391 | e = (EthCriterion) c; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 392 | } else if (c.type() == Criterion.Type.VLAN_VID) { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 393 | v = (VlanIdCriterion) c; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 394 | } else if (c.type() == Criterion.Type.IPV4_DST) { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 395 | ips.add((IPCriterion) c); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 396 | } else { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 397 | log.error("Unsupported filter {}", c); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 398 | fail(filt, ObjectiveError.UNSUPPORTED); |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 399 | return; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 402 | |
| 403 | // cache for later use |
| 404 | Filter filter = new Filter(p, e, v, ips); |
| 405 | filters.add(filter); |
| 406 | |
| 407 | // apply any pending versatile forwarding objectives |
| 408 | for (ForwardingObjective fwd : pendingVersatiles) { |
| 409 | Collection<FlowRule> ret = processVersatilesWithFilters(filter, fwd); |
| 410 | for (FlowRule fr : ret) { |
| 411 | ops.add(fr); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | for (IPCriterion ipaddr : ips) { |
| 416 | log.debug("adding IP filtering rules in ACL table: {}", ipaddr.ip()); |
| 417 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 418 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 419 | selector.matchInPort(p.port()); |
| 420 | selector.matchVlanId(v.vlanId()); |
| 421 | selector.matchEthDst(e.mac()); |
| 422 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 423 | selector.matchIPDst(ipaddr.ip()); // router IPs to the controller |
| 424 | treatment.setOutput(PortNumber.CONTROLLER); |
| 425 | FlowRule rule = DefaultFlowRule.builder() |
| 426 | .forDevice(deviceId) |
| 427 | .withSelector(selector.build()) |
| 428 | .withTreatment(treatment.build()) |
| 429 | .withPriority(HIGHEST_PRIORITY) |
| 430 | .fromApp(applicationId) |
| 431 | .makePermanent() |
| 432 | .forTable(ACL_TABLE).build(); |
| 433 | ops = ops.add(rule); |
| 434 | } |
| 435 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 436 | // apply filtering flow rules |
| 437 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 438 | @Override |
| 439 | public void onSuccess(FlowRuleOperations ops) { |
| 440 | pass(filt); |
| 441 | log.info("Applied filtering rules"); |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public void onError(FlowRuleOperations ops) { |
| 446 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 447 | log.info("Failed to apply filtering rules"); |
| 448 | } |
| 449 | })); |
| 450 | } |
| 451 | |
| 452 | private void pass(Objective obj) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 453 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | private void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 457 | obj.context().ifPresent(context -> context.onError(obj, error)); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | private void initializePipeline() { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 461 | //processIpUnicastTable(true); |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 462 | processAclTable(true); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 465 | private void processAclTable(boolean install) { |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 466 | TrafficSelector.Builder selector; |
| 467 | TrafficTreatment.Builder treatment; |
| 468 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 469 | FlowRule rule; |
| 470 | |
| 471 | //Drop rule |
| 472 | selector = DefaultTrafficSelector.builder(); |
| 473 | treatment = DefaultTrafficTreatment.builder(); |
| 474 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 475 | rule = DefaultFlowRule.builder() |
| 476 | .forDevice(deviceId) |
| 477 | .withSelector(selector.build()) |
| 478 | .withTreatment(treatment.build()) |
| 479 | .withPriority(DROP_PRIORITY) |
| 480 | .fromApp(appId) |
| 481 | .makePermanent() |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 482 | .forTable(ACL_TABLE).build(); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 483 | |
| 484 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 485 | |
| 486 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 487 | @Override |
| 488 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 489 | log.info("Provisioned ACL table"); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public void onError(FlowRuleOperations ops) { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 494 | log.info("Failed to provision ACL table"); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 495 | } |
| 496 | })); |
| 497 | } |
| 498 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 499 | private class Filter { |
| 500 | private PortCriterion port; |
| 501 | private VlanIdCriterion vlan; |
| 502 | private EthCriterion eth; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 503 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 504 | @SuppressWarnings("unused") |
| 505 | private Collection<IPCriterion> ips; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 506 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 507 | public Filter(PortCriterion p, EthCriterion e, VlanIdCriterion v, |
| 508 | Collection<IPCriterion> ips) { |
| 509 | this.eth = e; |
| 510 | this.port = p; |
| 511 | this.vlan = v; |
| 512 | this.ips = ips; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 513 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 514 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 515 | public PortNumber port() { |
| 516 | return port.port(); |
| 517 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 518 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 519 | public VlanId vlanId() { |
| 520 | return vlan.vlanId(); |
| 521 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 522 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 523 | public MacAddress mac() { |
| 524 | return eth.mac(); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | private class PicaGroup implements NextGroup { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 529 | TrafficTreatment nextActions; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 530 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 531 | public PicaGroup(TrafficTreatment next) { |
| 532 | this.nextActions = next; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | @Override |
| 536 | public byte[] data() { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 537 | return appKryo.serialize(nextActions); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 538 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 539 | } |
| 540 | } |