samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [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 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.Collection; |
| 21 | import java.util.Collections; |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 22 | import java.util.Objects; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 23 | |
| 24 | import org.onlab.osgi.ServiceDirectory; |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 25 | import org.onlab.packet.EthType.EtherType; |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.packet.IpPrefix; |
lishuai | 4d6a0ee | 2015-10-19 19:05:48 +0800 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.net.DeviceId; |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 31 | import org.onosproject.net.PortNumber; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 32 | import org.onosproject.net.behaviour.Pipeliner; |
| 33 | import org.onosproject.net.behaviour.PipelinerContext; |
| 34 | import org.onosproject.net.device.DeviceService; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.DefaultFlowRule; |
lishuai | 4d6a0ee | 2015-10-19 19:05:48 +0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.DefaultTrafficSelector; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.FlowRule; |
| 39 | import org.onosproject.net.flow.FlowRuleOperations; |
| 40 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 41 | import org.onosproject.net.flow.FlowRuleService; |
| 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.criteria.Criteria; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 45 | import org.onosproject.net.flow.criteria.Criterion.Type; |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 46 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 47 | import org.onosproject.net.flow.instructions.Instructions; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 48 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 49 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 50 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 51 | import org.onosproject.net.flowobjective.NextObjective; |
| 52 | import org.onosproject.net.flowobjective.Objective; |
| 53 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 54 | import org.slf4j.Logger; |
| 55 | |
| 56 | /** |
| 57 | * Driver for standard OpenVSwitch. |
| 58 | */ |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 59 | public class OpenVSwitchPipeline extends DefaultSingleTablePipeline |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 60 | implements Pipeliner { |
| 61 | |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 62 | private static final String VTN_APP_ID = "org.onosproject.app.vtn"; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 63 | private final Logger log = getLogger(getClass()); |
| 64 | private CoreService coreService; |
| 65 | private ServiceDirectory serviceDirectory; |
| 66 | protected FlowObjectiveStore flowObjectiveStore; |
| 67 | protected DeviceId deviceId; |
lishuai | 4d6a0ee | 2015-10-19 19:05:48 +0800 | [diff] [blame] | 68 | protected ApplicationId appId; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 69 | protected FlowRuleService flowRuleService; |
| 70 | protected DeviceService deviceService; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 71 | private static final int TIME_OUT = 0; |
lishuai | 4d6a0ee | 2015-10-19 19:05:48 +0800 | [diff] [blame] | 72 | private static final int CLASSIFIER_TABLE = 0; |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 73 | private static final int ENCAP_OUTPUT_TABLE = 4; |
| 74 | private static final int TUN_SEND_TABLE = 7; |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 75 | private static final int ARP_TABLE = 10; |
| 76 | private static final int DNAT_TABLE = 20; |
| 77 | private static final int L3FWD_TABLE = 30; |
| 78 | private static final int SNAT_TABLE = 40; |
lishuai | 4d6a0ee | 2015-10-19 19:05:48 +0800 | [diff] [blame] | 79 | private static final int MAC_TABLE = 50; |
| 80 | private static final int TABLE_MISS_PRIORITY = 0; |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 81 | private static final String USERDATA_IP = "169.254.169.254"; |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 82 | |
| 83 | @Override |
| 84 | public void init(DeviceId deviceId, PipelinerContext context) { |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 85 | super.init(deviceId, context); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 86 | this.serviceDirectory = context.directory(); |
| 87 | this.deviceId = deviceId; |
| 88 | |
| 89 | coreService = serviceDirectory.get(CoreService.class); |
| 90 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 91 | flowObjectiveStore = context.store(); |
lishuai | 4d6a0ee | 2015-10-19 19:05:48 +0800 | [diff] [blame] | 92 | appId = coreService |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 93 | .registerApplication("org.onosproject.driver.OpenVSwitchPipeline"); |
lishuai | 4d6a0ee | 2015-10-19 19:05:48 +0800 | [diff] [blame] | 94 | initializePipeline(); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void filter(FilteringObjective filteringObjective) { |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 99 | super.filter(filteringObjective); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void forward(ForwardingObjective fwd) { |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 104 | if (!VTN_APP_ID.equals(fwd.appId().name())) { |
| 105 | super.forward(fwd); |
| 106 | return; |
| 107 | } |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 108 | Collection<FlowRule> rules; |
| 109 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations |
| 110 | .builder(); |
| 111 | |
| 112 | rules = processForward(fwd); |
| 113 | switch (fwd.op()) { |
| 114 | case ADD: |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 115 | rules.stream().filter(Objects::nonNull) |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 116 | .forEach(flowOpsBuilder::add); |
| 117 | break; |
| 118 | case REMOVE: |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 119 | rules.stream().filter(Objects::nonNull) |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 120 | .forEach(flowOpsBuilder::remove); |
| 121 | break; |
| 122 | default: |
| 123 | fail(fwd, ObjectiveError.UNKNOWN); |
| 124 | log.warn("Unknown forwarding type {}", fwd.op()); |
| 125 | } |
| 126 | |
| 127 | flowRuleService.apply(flowOpsBuilder |
| 128 | .build(new FlowRuleOperationsContext() { |
| 129 | @Override |
| 130 | public void onSuccess(FlowRuleOperations ops) { |
| 131 | pass(fwd); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void onError(FlowRuleOperations ops) { |
| 136 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 137 | } |
| 138 | })); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void next(NextObjective nextObjective) { |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 143 | super.next(nextObjective); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 144 | } |
| 145 | |
lishuai | 4d6a0ee | 2015-10-19 19:05:48 +0800 | [diff] [blame] | 146 | private void initializePipeline() { |
| 147 | processClassifierTable(true); |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 148 | processArpTable(true); |
| 149 | processDnatTable(true); |
| 150 | processL3fwdTable(true); |
| 151 | processSnatTable(true); |
lishuai | 4d6a0ee | 2015-10-19 19:05:48 +0800 | [diff] [blame] | 152 | processMacTable(true); |
| 153 | } |
| 154 | |
| 155 | private void processClassifierTable(boolean install) { |
| 156 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 157 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 158 | |
| 159 | treatment.transition(MAC_TABLE); |
| 160 | |
| 161 | FlowRule rule; |
| 162 | rule = DefaultFlowRule.builder().forDevice(deviceId) |
| 163 | .withSelector(selector.build()) |
| 164 | .withTreatment(treatment.build()) |
| 165 | .withPriority(TABLE_MISS_PRIORITY).fromApp(appId) |
| 166 | .makePermanent().forTable(CLASSIFIER_TABLE).build(); |
| 167 | |
| 168 | applyRules(install, rule); |
| 169 | } |
| 170 | |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 171 | private void processArpTable(boolean install) { |
| 172 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 173 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 174 | |
| 175 | treatment.transition(MAC_TABLE); |
| 176 | |
| 177 | FlowRule rule; |
| 178 | rule = DefaultFlowRule.builder().forDevice(deviceId) |
| 179 | .withSelector(selector.build()) |
| 180 | .withTreatment(treatment.build()) |
| 181 | .withPriority(TABLE_MISS_PRIORITY).fromApp(appId) |
| 182 | .makePermanent().forTable(ARP_TABLE).build(); |
| 183 | |
| 184 | applyRules(install, rule); |
| 185 | } |
| 186 | |
| 187 | private void processDnatTable(boolean install) { |
| 188 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 189 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 190 | |
| 191 | treatment.transition(MAC_TABLE); |
| 192 | |
| 193 | FlowRule rule; |
| 194 | rule = DefaultFlowRule.builder().forDevice(deviceId) |
| 195 | .withSelector(selector.build()) |
| 196 | .withTreatment(treatment.build()) |
| 197 | .withPriority(TABLE_MISS_PRIORITY).fromApp(appId) |
| 198 | .makePermanent().forTable(DNAT_TABLE).build(); |
| 199 | |
| 200 | applyRules(install, rule); |
| 201 | } |
| 202 | |
| 203 | private void processL3fwdTable(boolean install) { |
| 204 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 205 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 206 | |
| 207 | treatment.transition(SNAT_TABLE); |
| 208 | |
| 209 | FlowRule rule; |
| 210 | rule = DefaultFlowRule.builder().forDevice(deviceId) |
| 211 | .withSelector(selector.build()) |
| 212 | .withTreatment(treatment.build()) |
| 213 | .withPriority(TABLE_MISS_PRIORITY).fromApp(appId) |
| 214 | .makePermanent().forTable(L3FWD_TABLE).build(); |
| 215 | |
| 216 | applyRules(install, rule); |
| 217 | } |
| 218 | |
| 219 | private void processSnatTable(boolean install) { |
| 220 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 221 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 222 | |
| 223 | treatment.transition(MAC_TABLE); |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 224 | treatment.add(Instructions.createOutput(PortNumber.CONTROLLER)); |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 225 | FlowRule rule; |
| 226 | rule = DefaultFlowRule.builder().forDevice(deviceId) |
| 227 | .withSelector(selector.build()) |
| 228 | .withTreatment(treatment.build()) |
| 229 | .withPriority(TABLE_MISS_PRIORITY).fromApp(appId) |
| 230 | .makePermanent().forTable(SNAT_TABLE).build(); |
| 231 | |
| 232 | applyRules(install, rule); |
| 233 | } |
| 234 | |
lishuai | 4d6a0ee | 2015-10-19 19:05:48 +0800 | [diff] [blame] | 235 | private void processMacTable(boolean install) { |
| 236 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 237 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 238 | |
| 239 | treatment.drop(); |
| 240 | |
| 241 | FlowRule rule; |
| 242 | rule = DefaultFlowRule.builder().forDevice(deviceId) |
| 243 | .withSelector(selector.build()) |
| 244 | .withTreatment(treatment.build()) |
| 245 | .withPriority(TABLE_MISS_PRIORITY).fromApp(appId) |
| 246 | .makePermanent().forTable(MAC_TABLE).build(); |
| 247 | |
| 248 | applyRules(install, rule); |
| 249 | } |
| 250 | |
| 251 | private void applyRules(boolean install, FlowRule rule) { |
| 252 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 253 | |
| 254 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 255 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 256 | @Override |
| 257 | public void onSuccess(FlowRuleOperations ops) { |
| 258 | log.info("ONOSW provisioned " + rule.tableId() + " table"); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void onError(FlowRuleOperations ops) { |
| 263 | log.info("ONOSW failed to provision " + rule.tableId() + " table"); |
| 264 | } |
| 265 | })); |
| 266 | } |
| 267 | |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 268 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 269 | switch (fwd.flag()) { |
| 270 | case SPECIFIC: |
| 271 | return processSpecific(fwd); |
| 272 | case VERSATILE: |
| 273 | return processVersatile(fwd); |
| 274 | default: |
| 275 | fail(fwd, ObjectiveError.UNKNOWN); |
| 276 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 277 | } |
| 278 | return Collections.emptySet(); |
| 279 | } |
| 280 | |
| 281 | private Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
| 282 | log.debug("Processing versatile forwarding objective"); |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 283 | TrafficSelector selector = fwd.selector(); |
| 284 | TrafficTreatment tb = fwd.treatment(); |
| 285 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder().fromApp(fwd.appId()).withPriority(fwd.priority()) |
| 286 | .forDevice(deviceId).withSelector(selector).withTreatment(tb).makeTemporary(TIME_OUT); |
| 287 | ruleBuilder.withPriority(fwd.priority()); |
| 288 | if (fwd.priority() == 100) { |
| 289 | ruleBuilder.forTable(ENCAP_OUTPUT_TABLE); |
| 290 | } else if (fwd.priority() == 200) { |
| 291 | ruleBuilder.forTable(TUN_SEND_TABLE); |
| 292 | } else { |
| 293 | ruleBuilder.forTable(CLASSIFIER_TABLE); |
| 294 | } |
| 295 | |
| 296 | if (fwd.permanent()) { |
| 297 | ruleBuilder.makePermanent(); |
| 298 | } |
| 299 | return Collections.singletonList(ruleBuilder.build()); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | private Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
| 303 | log.debug("Processing specific forwarding objective"); |
| 304 | TrafficSelector selector = fwd.selector(); |
| 305 | TrafficTreatment tb = fwd.treatment(); |
| 306 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 307 | .fromApp(fwd.appId()).withPriority(fwd.priority()) |
| 308 | .forDevice(deviceId).withSelector(selector) |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 309 | .withTreatment(tb).makeTemporary(TIME_OUT); |
| 310 | ruleBuilder.withPriority(fwd.priority()); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 311 | if (fwd.permanent()) { |
| 312 | ruleBuilder.makePermanent(); |
| 313 | } |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 314 | Integer transition = null; |
| 315 | Integer forTable = null; |
| 316 | // MAC table flow rules |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 317 | if (selector.getCriterion(Type.TUNNEL_ID) != null |
| 318 | && (selector.getCriterion(Type.ETH_DST) != null |
| 319 | || selector.getCriterion(Type.ETH_SRC) != null)) { |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 320 | forTable = MAC_TABLE; |
| 321 | return reassemblyFlowRule(ruleBuilder, tb, transition, forTable); |
| 322 | } |
| 323 | // CLASSIFIER table flow rules |
| 324 | if (selector.getCriterion(Type.IN_PORT) != null) { |
| 325 | forTable = CLASSIFIER_TABLE; |
| 326 | if (selector.getCriterion(Type.ETH_SRC) != null |
| 327 | && selector.getCriterion(Type.ETH_DST) != null) { |
| 328 | transition = L3FWD_TABLE; |
| 329 | } else if (selector.getCriterion(Type.ETH_SRC) != null |
| 330 | || selector.getCriterion(Type.TUNNEL_ID) != null) { |
| 331 | transition = MAC_TABLE; |
| 332 | } else if (selector.getCriterion(Type.IPV4_DST) != null) { |
| 333 | transition = DNAT_TABLE; |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 334 | } else if (selector.getCriterion(Type.ETH_TYPE) != null |
| 335 | && selector.getCriterion(Type.ETH_TYPE).equals(Criteria |
| 336 | .matchEthType(EtherType.ARP.ethType().toShort()))) { |
| 337 | transition = ARP_TABLE; |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 338 | } |
| 339 | return reassemblyFlowRule(ruleBuilder, tb, transition, forTable); |
| 340 | } |
| 341 | // ARP table flow rules |
| 342 | if (selector.getCriterion(Type.ETH_TYPE) != null |
| 343 | && selector.getCriterion(Type.ETH_TYPE).equals(Criteria |
| 344 | .matchEthType(EtherType.ARP.ethType().toShort()))) { |
| 345 | // CLASSIFIER table arp flow rules |
| 346 | if (selector.getCriterion(Type.TUNNEL_ID) == null) { |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 347 | if (selector.getCriterion(Type.ARP_OP) != null) { |
| 348 | forTable = CLASSIFIER_TABLE; |
| 349 | return reassemblyFlowRule(ruleBuilder, tb, null, forTable); |
| 350 | } |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 351 | transition = ARP_TABLE; |
| 352 | forTable = CLASSIFIER_TABLE; |
| 353 | return reassemblyFlowRule(ruleBuilder, tb, transition, forTable); |
| 354 | } |
| 355 | forTable = ARP_TABLE; |
| 356 | return reassemblyFlowRule(ruleBuilder, tb, transition, forTable); |
| 357 | } |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 358 | // SNAT table flow rules |
| 359 | if (selector.getCriterion(Type.TUNNEL_ID) != null |
| 360 | && selector.getCriterion(Type.IPV4_SRC) != null) { |
| 361 | transition = MAC_TABLE; |
| 362 | forTable = SNAT_TABLE; |
| 363 | return reassemblyFlowRule(ruleBuilder, tb, transition, forTable); |
| 364 | } |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 365 | // L3FWD table flow rules |
| 366 | if (selector.getCriterion(Type.TUNNEL_ID) != null |
| 367 | && selector.getCriterion(Type.IPV4_DST) != null) { |
| 368 | transition = MAC_TABLE; |
| 369 | forTable = L3FWD_TABLE; |
| 370 | return reassemblyFlowRule(ruleBuilder, tb, transition, forTable); |
| 371 | } |
| 372 | // DNAT table flow rules |
| 373 | if (selector.getCriterion(Type.IPV4_DST) != null) { |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 374 | IPCriterion ipCriterion = (IPCriterion) selector.getCriterion(Type.IPV4_DST); |
| 375 | IpPrefix ipPrefix = ipCriterion.ip(); |
| 376 | // specific CLASSIFIER table flow rules for userdata |
| 377 | if (ipPrefix.address().equals(IpAddress.valueOf(USERDATA_IP))) { |
| 378 | forTable = CLASSIFIER_TABLE; |
| 379 | transition = MAC_TABLE; |
| 380 | return reassemblyFlowRule(ruleBuilder, tb, transition, forTable); |
| 381 | } |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 382 | transition = L3FWD_TABLE; |
| 383 | forTable = DNAT_TABLE; |
| 384 | return reassemblyFlowRule(ruleBuilder, tb, transition, forTable); |
| 385 | } |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 386 | return Collections.singletonList(ruleBuilder.build()); |
| 387 | } |
| 388 | |
| 389 | private Collection<FlowRule> reassemblyFlowRule(FlowRule.Builder ruleBuilder, |
| 390 | TrafficTreatment tb, |
| 391 | Integer transition, |
| 392 | Integer forTable) { |
| 393 | if (transition != null) { |
| 394 | TrafficTreatment.Builder newTraffic = DefaultTrafficTreatment |
| 395 | .builder(); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 396 | tb.allInstructions().forEach(t -> newTraffic.add(t)); |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 397 | newTraffic.transition(transition); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 398 | ruleBuilder.withTreatment(newTraffic.build()); |
lishuai | 4fa2634 | 2015-12-03 13:01:39 +0800 | [diff] [blame] | 399 | } else { |
| 400 | ruleBuilder.withTreatment(tb); |
| 401 | } |
| 402 | if (forTable != null) { |
| 403 | ruleBuilder.forTable(forTable); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 404 | } |
| 405 | return Collections.singletonList(ruleBuilder.build()); |
| 406 | } |
| 407 | |
| 408 | private void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 409 | obj.context().ifPresent(context -> context.onError(obj, error)); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | private void pass(Objective obj) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 413 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
samuel | 3b2743d | 2015-07-29 10:44:30 +0800 | [diff] [blame] | 414 | } |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 415 | } |