daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [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 | */ |
Hyunsun Moon | 21d17ba | 2015-12-04 16:16:25 -0800 | [diff] [blame] | 16 | package org.onosproject.driver.pipeline; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 17 | |
| 18 | import org.onlab.osgi.ServiceDirectory; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 19 | import org.onlab.packet.MacAddress; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.behaviour.Pipeliner; |
| 24 | import org.onosproject.net.behaviour.PipelinerContext; |
| 25 | import org.onosproject.net.flow.DefaultFlowRule; |
| 26 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.FlowRule; |
| 29 | import org.onosproject.net.flow.FlowRuleOperations; |
| 30 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 31 | import org.onosproject.net.flow.FlowRuleService; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.flow.criteria.Criterion; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 35 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 36 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 37 | import org.onosproject.net.flow.criteria.TunnelIdCriterion; |
daniel park | ac34890 | 2017-04-26 13:49:05 +0900 | [diff] [blame] | 38 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 39 | import org.onosproject.net.flow.instructions.Instruction; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 40 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 41 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 42 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 43 | import org.onosproject.net.flowobjective.NextObjective; |
| 44 | import org.onosproject.net.flowobjective.Objective; |
| 45 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 46 | import org.slf4j.Logger; |
| 47 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 48 | import java.util.Optional; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 49 | |
| 50 | import static org.slf4j.LoggerFactory.getLogger; |
| 51 | |
| 52 | /** |
| 53 | * Driver for OpenstackSwitching. |
| 54 | */ |
| 55 | public class OpenstackPipeline extends DefaultSingleTablePipeline |
| 56 | implements Pipeliner { |
| 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 59 | protected FlowObjectiveStore flowObjectiveStore; |
| 60 | protected DeviceId deviceId; |
| 61 | protected ApplicationId appId; |
| 62 | protected FlowRuleService flowRuleService; |
| 63 | |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 64 | private static final int SRC_VNI_TABLE = 0; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 65 | private static final int ACL_TABLE = 1; |
| 66 | private static final int JUMP_TABLE = 2; |
| 67 | private static final int ROUTING_TABLE = 3; |
| 68 | private static final int FORWARDING_TABLE = 4; |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 69 | private static final int DUMMY_TABLE = 10; |
| 70 | private static final int LAST_TABLE = FORWARDING_TABLE; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 71 | |
| 72 | private static final int DROP_PRIORITY = 0; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 73 | private static final int HIGH_PRIORITY = 30000; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 74 | private static final int TIME_OUT = 0; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 75 | private static final String VIRTUAL_GATEWAY_MAC = "fe:00:00:00:00:02"; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 76 | |
| 77 | |
| 78 | @Override |
| 79 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 80 | super.init(deviceId, context); |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 81 | ServiceDirectory serviceDirectory = context.directory(); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 82 | this.deviceId = deviceId; |
| 83 | |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 84 | CoreService coreService = serviceDirectory.get(CoreService.class); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 85 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 86 | flowObjectiveStore = context.store(); |
| 87 | |
| 88 | appId = coreService.registerApplication( |
| 89 | "org.onosproject.driver.OpenstackPipeline"); |
| 90 | |
| 91 | initializePipeline(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void filter(FilteringObjective filteringObjective) { |
| 96 | super.filter(filteringObjective); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void next(NextObjective nextObjective) { |
| 101 | super.next(nextObjective); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void forward(ForwardingObjective forwardingObjective) { |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 106 | FlowRule flowRule; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 107 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 108 | switch (forwardingObjective.flag()) { |
| 109 | case SPECIFIC: |
| 110 | flowRule = processSpecific(forwardingObjective); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 111 | break; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 112 | case VERSATILE: |
| 113 | flowRule = processVersatile(forwardingObjective); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 114 | break; |
| 115 | default: |
| 116 | fail(forwardingObjective, ObjectiveError.UNKNOWN); |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 117 | log.warn("Unknown forwarding flag {}", forwardingObjective.flag()); |
| 118 | return; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 119 | } |
| 120 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 121 | if (forwardingObjective.op().equals(Objective.Operation.ADD)) { |
| 122 | applyRules(true, flowRule); |
| 123 | } else { |
| 124 | applyRules(false, flowRule); |
| 125 | } |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 126 | |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private void initializePipeline() { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 130 | connectTables(SRC_VNI_TABLE, ACL_TABLE); |
| 131 | connectTables(ACL_TABLE, JUMP_TABLE); |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 132 | setupJumpTable(); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 133 | } |
| 134 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 135 | private void connectTables(int fromTable, int toTable) { |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 136 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 137 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 138 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 139 | treatment.transition(toTable); |
| 140 | |
| 141 | FlowRule flowRule = DefaultFlowRule.builder() |
| 142 | .forDevice(deviceId) |
| 143 | .withSelector(selector.build()) |
| 144 | .withTreatment(treatment.build()) |
| 145 | .withPriority(DROP_PRIORITY) |
| 146 | .fromApp(appId) |
| 147 | .makePermanent() |
| 148 | .forTable(fromTable) |
| 149 | .build(); |
| 150 | |
| 151 | applyRules(true, flowRule); |
| 152 | } |
| 153 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 154 | private void setUpTableMissEntry(int table) { |
| 155 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 156 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 157 | |
| 158 | treatment.drop(); |
| 159 | |
| 160 | FlowRule flowRule = DefaultFlowRule.builder() |
| 161 | .forDevice(deviceId) |
| 162 | .withSelector(selector.build()) |
| 163 | .withTreatment(treatment.build()) |
| 164 | .withPriority(DROP_PRIORITY) |
| 165 | .fromApp(appId) |
| 166 | .makePermanent() |
| 167 | .forTable(table) |
| 168 | .build(); |
| 169 | |
| 170 | applyRules(true, flowRule); |
| 171 | } |
| 172 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 173 | private void setupJumpTable() { |
| 174 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 175 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 176 | |
| 177 | selector.matchEthDst(MacAddress.valueOf(VIRTUAL_GATEWAY_MAC)); |
| 178 | treatment.transition(ROUTING_TABLE); |
| 179 | |
| 180 | FlowRule flowRule = DefaultFlowRule.builder() |
| 181 | .forDevice(deviceId) |
| 182 | .withSelector(selector.build()) |
| 183 | .withTreatment(treatment.build()) |
| 184 | .withPriority(HIGH_PRIORITY) |
| 185 | .fromApp(appId) |
| 186 | .makePermanent() |
| 187 | .forTable(JUMP_TABLE) |
| 188 | .build(); |
| 189 | |
| 190 | applyRules(true, flowRule); |
| 191 | |
| 192 | selector = DefaultTrafficSelector.builder(); |
| 193 | treatment = DefaultTrafficTreatment.builder(); |
| 194 | |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 195 | treatment.transition(FORWARDING_TABLE); |
| 196 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 197 | flowRule = DefaultFlowRule.builder() |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 198 | .forDevice(deviceId) |
| 199 | .withSelector(selector.build()) |
| 200 | .withTreatment(treatment.build()) |
| 201 | .withPriority(DROP_PRIORITY) |
| 202 | .fromApp(appId) |
| 203 | .makePermanent() |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 204 | .forTable(JUMP_TABLE) |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 205 | .build(); |
| 206 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 207 | applyRules(true, flowRule); |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 208 | } |
| 209 | |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 210 | private void applyRules(boolean install, FlowRule flowRule) { |
| 211 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
| 212 | |
| 213 | flowOpsBuilder = install ? flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule); |
| 214 | |
| 215 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 216 | @Override |
| 217 | public void onSuccess(FlowRuleOperations ops) { |
| 218 | log.debug("Provisioned vni or forwarding table"); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void onError(FlowRuleOperations ops) { |
| 223 | log.debug("Failed to privision vni or forwarding table"); |
| 224 | } |
| 225 | })); |
| 226 | } |
| 227 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 228 | private FlowRule processVersatile(ForwardingObjective forwardingObjective) { |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 229 | log.debug("Processing versatile forwarding objective"); |
| 230 | |
| 231 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 232 | .forDevice(deviceId) |
| 233 | .withSelector(forwardingObjective.selector()) |
| 234 | .withTreatment(forwardingObjective.treatment()) |
| 235 | .withPriority(forwardingObjective.priority()) |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 236 | .fromApp(forwardingObjective.appId()) |
| 237 | .forTable(SRC_VNI_TABLE); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 238 | |
| 239 | if (forwardingObjective.permanent()) { |
| 240 | ruleBuilder.makePermanent(); |
| 241 | } else { |
| 242 | ruleBuilder.makeTemporary(TIME_OUT); |
| 243 | } |
| 244 | |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 245 | return ruleBuilder.build(); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 246 | } |
| 247 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 248 | private FlowRule processSpecific(ForwardingObjective forwardingObjective) { |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 249 | log.debug("Processing specific forwarding objective"); |
| 250 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 251 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 252 | Optional<Instruction> group = forwardingObjective.treatment().immediate().stream() |
| 253 | .filter(i -> i.type() == Instruction.Type.GROUP).findAny(); |
| 254 | int tableType = tableType(forwardingObjective); |
| 255 | if (tableType != LAST_TABLE && !group.isPresent()) { |
| 256 | treatment.transition(nextTable(tableType)); |
| 257 | } |
| 258 | forwardingObjective.treatment().allInstructions().stream() |
| 259 | .filter(i -> i.type() != Instruction.Type.NOACTION).forEach(treatment::add); |
| 260 | |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 261 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 262 | .forDevice(deviceId) |
| 263 | .withSelector(forwardingObjective.selector()) |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 264 | .withTreatment(treatment.build()) |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 265 | .withPriority(forwardingObjective.priority()) |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 266 | .fromApp(forwardingObjective.appId()) |
| 267 | .forTable(tableType); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 268 | |
| 269 | if (forwardingObjective.permanent()) { |
| 270 | ruleBuilder.makePermanent(); |
| 271 | } else { |
| 272 | ruleBuilder.makeTemporary(TIME_OUT); |
| 273 | } |
| 274 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 275 | return ruleBuilder.build(); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 276 | } |
| 277 | |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 278 | private int tableType(ForwardingObjective fo) { |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 279 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 280 | IPCriterion ipSrc = (IPCriterion) fo.selector().getCriterion(Criterion.Type.IPV4_SRC); |
| 281 | IPCriterion ipDst = (IPCriterion) fo.selector().getCriterion(Criterion.Type.IPV4_DST); |
| 282 | TunnelIdCriterion tunnelId = |
| 283 | (TunnelIdCriterion) fo.selector().getCriterion(Criterion.Type.TUNNEL_ID); |
daniel park | ac34890 | 2017-04-26 13:49:05 +0900 | [diff] [blame] | 284 | VlanIdCriterion vlanId = (VlanIdCriterion) fo.selector().getCriterion(Criterion.Type.VLAN_VID); |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 285 | PortCriterion inPort = (PortCriterion) fo.selector().getCriterion(Criterion.Type.IN_PORT); |
| 286 | Optional<Instruction> output = fo.treatment().immediate().stream() |
| 287 | .filter(i -> i.type() == Instruction.Type.OUTPUT).findAny(); |
| 288 | Optional<Instruction> group = fo.treatment().immediate().stream() |
| 289 | .filter(i -> i.type() == Instruction.Type.GROUP).findAny(); |
| 290 | |
| 291 | // TODO: Add the Connection Tracking Table |
| 292 | if (inPort != null) { |
| 293 | return SRC_VNI_TABLE; |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 294 | } else if ((tunnelId != null && ipSrc != null && ipDst != null) || |
daniel park | ac34890 | 2017-04-26 13:49:05 +0900 | [diff] [blame] | 295 | (vlanId != null && ipSrc != null && ipDst != null) || |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 296 | (ipSrc != null && group.isPresent())) { |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 297 | return ROUTING_TABLE; |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 298 | } else if (output.isPresent() || (ipDst != null && group.isPresent())) { |
| 299 | return FORWARDING_TABLE; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 300 | } else if ((ipSrc != null && ipSrc.ip().prefixLength() == 32 && |
| 301 | ipDst != null && ipDst.ip().prefixLength() == 32) || |
| 302 | (ipSrc != null && ipSrc.ip().prefixLength() == 32 && ipDst == null) || |
| 303 | (ipDst != null && ipDst.ip().prefixLength() == 32 && ipSrc == null) || |
| 304 | (ipDst != null && ipDst.ip().prefixLength() == 32 && ipSrc != null) || |
| 305 | (ipSrc != null && ipSrc.ip().prefixLength() == 32 && ipDst != null)) { |
| 306 | return ACL_TABLE; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | return DUMMY_TABLE; |
| 310 | } |
| 311 | |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 312 | private int nextTable(int baseTable) { |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 313 | return baseTable + 1; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | private void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 317 | obj.context().ifPresent(context -> context.onError(obj, error)); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |