Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.driver.pipeline; |
| 17 | |
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) |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 93 | .build("PicaPipeline"); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 94 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 97 | this.serviceDirectory = context.directory(); |
| 98 | this.deviceId = deviceId; |
| 99 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 100 | coreService = serviceDirectory.get(CoreService.class); |
| 101 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 102 | flowObjectiveStore = context.store(); |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 103 | filters = Collections.newSetFromMap(new ConcurrentHashMap<Filter, Boolean>()); |
| 104 | pendingVersatiles = Collections.newSetFromMap( |
| 105 | new ConcurrentHashMap<ForwardingObjective, Boolean>()); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 106 | appId = coreService.registerApplication( |
| 107 | "org.onosproject.driver.OVSPicaPipeline"); |
| 108 | |
| 109 | initializePipeline(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void filter(FilteringObjective filteringObjective) { |
| 114 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 115 | processFilter(filteringObjective, |
| 116 | filteringObjective.op() == Objective.Operation.ADD, |
| 117 | filteringObjective.appId()); |
| 118 | } else { |
| 119 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void forward(ForwardingObjective fwd) { |
| 125 | Collection<FlowRule> rules; |
| 126 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 127 | |
| 128 | rules = processForward(fwd); |
| 129 | switch (fwd.op()) { |
| 130 | case ADD: |
| 131 | rules.stream() |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 132 | .filter(Objects::nonNull) |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 133 | .forEach(flowBuilder::add); |
| 134 | break; |
| 135 | case REMOVE: |
| 136 | rules.stream() |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 137 | .filter(Objects::nonNull) |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 138 | .forEach(flowBuilder::remove); |
| 139 | break; |
| 140 | default: |
| 141 | fail(fwd, ObjectiveError.UNKNOWN); |
| 142 | log.warn("Unknown forwarding type {}", fwd.op()); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 147 | @Override |
| 148 | public void onSuccess(FlowRuleOperations ops) { |
| 149 | pass(fwd); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void onError(FlowRuleOperations ops) { |
| 154 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 155 | } |
| 156 | })); |
| 157 | |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void next(NextObjective nextObjective) { |
| 162 | switch (nextObjective.type()) { |
| 163 | case SIMPLE: |
| 164 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 165 | if (treatments.size() != 1) { |
| 166 | log.error("Next Objectives of type Simple should only have a " |
| 167 | + "single Traffic Treatment. Next Objective Id:{}", nextObjective.id()); |
| 168 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 169 | return; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 170 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 171 | TrafficTreatment treatment = treatments.iterator().next(); |
| 172 | TrafficTreatment.Builder filteredTreatment = DefaultTrafficTreatment.builder(); |
| 173 | VlanId modVlanId; |
| 174 | for (Instruction ins : treatment.allInstructions()) { |
| 175 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 176 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 177 | switch (l2ins.subtype()) { |
| 178 | case ETH_DST: |
| 179 | filteredTreatment.setEthDst( |
| 180 | ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac()); |
| 181 | break; |
| 182 | case ETH_SRC: |
| 183 | filteredTreatment.setEthSrc( |
| 184 | ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac()); |
| 185 | break; |
| 186 | case VLAN_ID: |
| 187 | modVlanId = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId(); |
| 188 | filteredTreatment.setVlanId(modVlanId); |
| 189 | break; |
| 190 | default: |
| 191 | break; |
| 192 | } |
| 193 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 194 | //long portNum = ((Instructions.OutputInstruction) ins).port().toLong(); |
| 195 | filteredTreatment.add(ins); |
| 196 | } else { |
| 197 | // Ignore the vlan_pcp action since it's does matter much. |
| 198 | log.warn("Driver does not handle this type of TrafficTreatment" |
| 199 | + " instruction in nextObjectives: {}", ins.type()); |
| 200 | } |
| 201 | } |
| 202 | // store for future use |
| 203 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
| 204 | new PicaGroup(filteredTreatment.build())); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 205 | break; |
| 206 | case HASHED: |
| 207 | case BROADCAST: |
| 208 | case FAILOVER: |
| 209 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
| 210 | log.warn("Unsupported next objective type {}", nextObjective.type()); |
| 211 | break; |
| 212 | default: |
| 213 | fail(nextObjective, ObjectiveError.UNKNOWN); |
| 214 | log.warn("Unknown next objective type {}", nextObjective.type()); |
| 215 | } |
| 216 | |
| 217 | } |
| 218 | |
| 219 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 220 | switch (fwd.flag()) { |
| 221 | case SPECIFIC: |
| 222 | return processSpecific(fwd); |
| 223 | case VERSATILE: |
| 224 | return processVersatile(fwd); |
| 225 | default: |
| 226 | fail(fwd, ObjectiveError.UNKNOWN); |
| 227 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 228 | } |
| 229 | return Collections.emptySet(); |
| 230 | } |
| 231 | |
| 232 | private Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
| 233 | log.debug("Processing versatile forwarding objective"); |
| 234 | TrafficSelector selector = fwd.selector(); |
Saurav Das | 598a02f | 2015-08-31 18:19:30 -0700 | [diff] [blame] | 235 | TrafficTreatment treatment = fwd.treatment(); |
| 236 | Collection<FlowRule> flowrules = new ArrayList<FlowRule>(); |
| 237 | |
| 238 | // first add this rule for basic single-table operation |
| 239 | // or non-ARP related multi-table operation |
| 240 | FlowRule rule = DefaultFlowRule.builder() |
| 241 | .forDevice(deviceId) |
| 242 | .withSelector(selector) |
| 243 | .withTreatment(treatment) |
| 244 | .withPriority(fwd.priority()) |
| 245 | .fromApp(fwd.appId()) |
| 246 | .makePermanent() |
| 247 | .forTable(ACL_TABLE).build(); |
| 248 | flowrules.add(rule); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 249 | |
| 250 | EthTypeCriterion ethType = |
| 251 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
| 252 | if (ethType == null) { |
Saurav Das | 598a02f | 2015-08-31 18:19:30 -0700 | [diff] [blame] | 253 | log.warn("No ethType in versatile forwarding obj. Not processing further."); |
| 254 | return flowrules; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 255 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 256 | |
Saurav Das | 598a02f | 2015-08-31 18:19:30 -0700 | [diff] [blame] | 257 | // now deal with possible mix of ARP with filtering objectives |
| 258 | // in multi-table scenarios |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 259 | if (ethType.ethType().toShort() == Ethernet.TYPE_ARP) { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 260 | if (filters.isEmpty()) { |
| 261 | pendingVersatiles.add(fwd); |
Saurav Das | 598a02f | 2015-08-31 18:19:30 -0700 | [diff] [blame] | 262 | return flowrules; |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 263 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 264 | for (Filter filter : filters) { |
| 265 | flowrules.addAll(processVersatilesWithFilters(filter, fwd)); |
| 266 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 267 | } |
Saurav Das | 598a02f | 2015-08-31 18:19:30 -0700 | [diff] [blame] | 268 | return flowrules; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 271 | private Collection<FlowRule> processVersatilesWithFilters( |
| 272 | Filter filt, ForwardingObjective fwd) { |
| 273 | Collection<FlowRule> flows = new ArrayList<FlowRule>(); |
| 274 | |
| 275 | // rule for ARP replies |
| 276 | log.debug("adding ARP rule in ACL table"); |
| 277 | TrafficSelector.Builder sel = DefaultTrafficSelector.builder(); |
| 278 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 279 | sel.matchInPort(filt.port()); |
| 280 | sel.matchVlanId(filt.vlanId()); |
| 281 | sel.matchEthDst(filt.mac()); |
| 282 | sel.matchEthType(Ethernet.TYPE_ARP); |
| 283 | treat.setOutput(PortNumber.CONTROLLER); |
| 284 | FlowRule rule = DefaultFlowRule.builder() |
| 285 | .forDevice(deviceId) |
| 286 | .withSelector(sel.build()) |
| 287 | .withTreatment(treat.build()) |
| 288 | .withPriority(HIGHEST_PRIORITY) |
| 289 | .fromApp(appId) |
| 290 | .makePermanent() |
| 291 | .forTable(ACL_TABLE).build(); |
| 292 | flows.add(rule); |
| 293 | |
| 294 | // rule for ARP Broadcast |
| 295 | sel = DefaultTrafficSelector.builder(); |
| 296 | treat = DefaultTrafficTreatment.builder(); |
| 297 | sel.matchInPort(filt.port()); |
| 298 | sel.matchVlanId(filt.vlanId()); |
| 299 | sel.matchEthDst(MacAddress.BROADCAST); |
| 300 | sel.matchEthType(Ethernet.TYPE_ARP); |
| 301 | treat.setOutput(PortNumber.CONTROLLER); |
| 302 | rule = DefaultFlowRule.builder() |
| 303 | .forDevice(deviceId) |
| 304 | .withSelector(sel.build()) |
| 305 | .withTreatment(treat.build()) |
| 306 | .withPriority(HIGHEST_PRIORITY) |
| 307 | .fromApp(appId) |
| 308 | .makePermanent() |
| 309 | .forTable(ACL_TABLE).build(); |
| 310 | flows.add(rule); |
| 311 | |
| 312 | return flows; |
| 313 | } |
| 314 | |
| 315 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 316 | private Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
| 317 | log.debug("Processing specific forwarding objective"); |
| 318 | TrafficSelector selector = fwd.selector(); |
| 319 | EthTypeCriterion ethType = |
| 320 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 321 | if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) { |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 322 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 323 | return Collections.emptySet(); |
| 324 | } |
| 325 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 326 | List<FlowRule> ipflows = new ArrayList<FlowRule>(); |
| 327 | for (Filter f: filters) { |
| 328 | TrafficSelector filteredSelector = |
| 329 | DefaultTrafficSelector.builder() |
| 330 | .matchEthType(Ethernet.TYPE_IPV4) |
| 331 | .matchIPDst( |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 332 | ((IPCriterion) |
| 333 | selector.getCriterion(Criterion.Type.IPV4_DST)).ip()) |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 334 | .matchEthDst(f.mac()) |
| 335 | .matchVlanId(f.vlanId()) |
| 336 | .build(); |
| 337 | TrafficTreatment tt = null; |
| 338 | if (fwd.nextId() != null) { |
| 339 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 340 | if (next == null) { |
| 341 | log.error("next-id {} does not exist in store", fwd.nextId()); |
| 342 | return Collections.emptySet(); |
| 343 | } |
| 344 | tt = appKryo.deserialize(next.data()); |
| 345 | if (tt == null) { |
| 346 | log.error("Error in deserializing next-id {}", fwd.nextId()); |
| 347 | return Collections.emptySet(); |
| 348 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 349 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 350 | |
| 351 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 352 | .fromApp(fwd.appId()) |
| 353 | .withPriority(fwd.priority()) |
| 354 | .forDevice(deviceId) |
| 355 | .withSelector(filteredSelector) |
| 356 | .withTreatment(tt); |
| 357 | if (fwd.permanent()) { |
| 358 | ruleBuilder.makePermanent(); |
| 359 | } else { |
| 360 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 361 | } |
| 362 | ruleBuilder.forTable(IP_UNICAST_TABLE); |
| 363 | ipflows.add(ruleBuilder.build()); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 366 | return ipflows; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | private void processFilter(FilteringObjective filt, boolean install, |
| 370 | ApplicationId applicationId) { |
| 371 | // This driver only processes filtering criteria defined with switch |
| 372 | // ports as the key |
| 373 | PortCriterion p; |
| 374 | if (!filt.key().equals(Criteria.dummy()) && |
| 375 | filt.key().type() == Criterion.Type.IN_PORT) { |
| 376 | p = (PortCriterion) filt.key(); |
| 377 | } else { |
| 378 | log.warn("No key defined in filtering objective from app: {}. Not" |
| 379 | + "processing filtering objective", applicationId); |
| 380 | fail(filt, ObjectiveError.UNKNOWN); |
| 381 | return; |
| 382 | } |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 383 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 384 | EthCriterion e = null; |
| 385 | VlanIdCriterion v = null; |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 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 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 403 | if (v == null || e == null) { |
| 404 | log.warn("Pica Pipeline ETH_DST and/or VLAN_ID not specified"); |
| 405 | fail(filt, ObjectiveError.BADPARAMS); |
| 406 | return; |
| 407 | } |
| 408 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 409 | // cache for later use |
| 410 | Filter filter = new Filter(p, e, v, ips); |
| 411 | filters.add(filter); |
| 412 | |
| 413 | // apply any pending versatile forwarding objectives |
| 414 | for (ForwardingObjective fwd : pendingVersatiles) { |
| 415 | Collection<FlowRule> ret = processVersatilesWithFilters(filter, fwd); |
| 416 | for (FlowRule fr : ret) { |
| 417 | ops.add(fr); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | for (IPCriterion ipaddr : ips) { |
| 422 | log.debug("adding IP filtering rules in ACL table: {}", ipaddr.ip()); |
| 423 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 424 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 425 | selector.matchInPort(p.port()); |
| 426 | selector.matchVlanId(v.vlanId()); |
| 427 | selector.matchEthDst(e.mac()); |
| 428 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 429 | selector.matchIPDst(ipaddr.ip()); // router IPs to the controller |
| 430 | treatment.setOutput(PortNumber.CONTROLLER); |
| 431 | FlowRule rule = DefaultFlowRule.builder() |
| 432 | .forDevice(deviceId) |
| 433 | .withSelector(selector.build()) |
| 434 | .withTreatment(treatment.build()) |
| 435 | .withPriority(HIGHEST_PRIORITY) |
| 436 | .fromApp(applicationId) |
| 437 | .makePermanent() |
| 438 | .forTable(ACL_TABLE).build(); |
| 439 | ops = ops.add(rule); |
| 440 | } |
| 441 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 442 | // apply filtering flow rules |
| 443 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 444 | @Override |
| 445 | public void onSuccess(FlowRuleOperations ops) { |
| 446 | pass(filt); |
| 447 | log.info("Applied filtering rules"); |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public void onError(FlowRuleOperations ops) { |
| 452 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 453 | log.info("Failed to apply filtering rules"); |
| 454 | } |
| 455 | })); |
| 456 | } |
| 457 | |
| 458 | private void pass(Objective obj) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 459 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | private void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 463 | obj.context().ifPresent(context -> context.onError(obj, error)); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | private void initializePipeline() { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 467 | //processIpUnicastTable(true); |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 468 | processAclTable(true); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 471 | private void processAclTable(boolean install) { |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 472 | TrafficSelector.Builder selector; |
| 473 | TrafficTreatment.Builder treatment; |
| 474 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 475 | FlowRule rule; |
| 476 | |
| 477 | //Drop rule |
| 478 | selector = DefaultTrafficSelector.builder(); |
| 479 | treatment = DefaultTrafficTreatment.builder(); |
| 480 | |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 481 | rule = DefaultFlowRule.builder() |
| 482 | .forDevice(deviceId) |
| 483 | .withSelector(selector.build()) |
| 484 | .withTreatment(treatment.build()) |
| 485 | .withPriority(DROP_PRIORITY) |
| 486 | .fromApp(appId) |
| 487 | .makePermanent() |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 488 | .forTable(ACL_TABLE).build(); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 489 | |
| 490 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 491 | |
| 492 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 493 | @Override |
| 494 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 495 | log.info("Provisioned ACL table"); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public void onError(FlowRuleOperations ops) { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 500 | log.info("Failed to provision ACL table"); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 501 | } |
| 502 | })); |
| 503 | } |
| 504 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 505 | private class Filter { |
| 506 | private PortCriterion port; |
| 507 | private VlanIdCriterion vlan; |
| 508 | private EthCriterion eth; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 509 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 510 | @SuppressWarnings("unused") |
| 511 | private Collection<IPCriterion> ips; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 512 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 513 | public Filter(PortCriterion p, EthCriterion e, VlanIdCriterion v, |
| 514 | Collection<IPCriterion> ips) { |
| 515 | this.eth = e; |
| 516 | this.port = p; |
| 517 | this.vlan = v; |
| 518 | this.ips = ips; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 519 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 520 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 521 | public PortNumber port() { |
| 522 | return port.port(); |
| 523 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 524 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 525 | public VlanId vlanId() { |
| 526 | return vlan.vlanId(); |
| 527 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 528 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 529 | public MacAddress mac() { |
| 530 | return eth.mac(); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | private class PicaGroup implements NextGroup { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 535 | TrafficTreatment nextActions; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 536 | |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 537 | public PicaGroup(TrafficTreatment next) { |
| 538 | this.nextActions = next; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | @Override |
| 542 | public byte[] data() { |
Saurav Das | 337c7a4 | 2015-06-02 15:12:06 -0700 | [diff] [blame] | 543 | return appKryo.serialize(nextActions); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 544 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 545 | } |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 546 | |
| 547 | @Override |
| 548 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 549 | // TODO Implementation deferred to vendor |
| 550 | return null; |
| 551 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 552 | } |