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); |
| 132 | setUpTableMissEntry(ACL_TABLE); |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 133 | setupJumpTable(); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 134 | } |
| 135 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 136 | private void connectTables(int fromTable, int toTable) { |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 137 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 138 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 139 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 140 | treatment.transition(toTable); |
| 141 | |
| 142 | FlowRule flowRule = DefaultFlowRule.builder() |
| 143 | .forDevice(deviceId) |
| 144 | .withSelector(selector.build()) |
| 145 | .withTreatment(treatment.build()) |
| 146 | .withPriority(DROP_PRIORITY) |
| 147 | .fromApp(appId) |
| 148 | .makePermanent() |
| 149 | .forTable(fromTable) |
| 150 | .build(); |
| 151 | |
| 152 | applyRules(true, flowRule); |
| 153 | } |
| 154 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 155 | private void setUpTableMissEntry(int table) { |
| 156 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 157 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 158 | |
| 159 | treatment.drop(); |
| 160 | |
| 161 | FlowRule flowRule = DefaultFlowRule.builder() |
| 162 | .forDevice(deviceId) |
| 163 | .withSelector(selector.build()) |
| 164 | .withTreatment(treatment.build()) |
| 165 | .withPriority(DROP_PRIORITY) |
| 166 | .fromApp(appId) |
| 167 | .makePermanent() |
| 168 | .forTable(table) |
| 169 | .build(); |
| 170 | |
| 171 | applyRules(true, flowRule); |
| 172 | } |
| 173 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 174 | private void setupJumpTable() { |
| 175 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 176 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 177 | |
| 178 | selector.matchEthDst(MacAddress.valueOf(VIRTUAL_GATEWAY_MAC)); |
| 179 | treatment.transition(ROUTING_TABLE); |
| 180 | |
| 181 | FlowRule flowRule = DefaultFlowRule.builder() |
| 182 | .forDevice(deviceId) |
| 183 | .withSelector(selector.build()) |
| 184 | .withTreatment(treatment.build()) |
| 185 | .withPriority(HIGH_PRIORITY) |
| 186 | .fromApp(appId) |
| 187 | .makePermanent() |
| 188 | .forTable(JUMP_TABLE) |
| 189 | .build(); |
| 190 | |
| 191 | applyRules(true, flowRule); |
| 192 | |
| 193 | selector = DefaultTrafficSelector.builder(); |
| 194 | treatment = DefaultTrafficTreatment.builder(); |
| 195 | |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 196 | treatment.transition(FORWARDING_TABLE); |
| 197 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 198 | flowRule = DefaultFlowRule.builder() |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 199 | .forDevice(deviceId) |
| 200 | .withSelector(selector.build()) |
| 201 | .withTreatment(treatment.build()) |
| 202 | .withPriority(DROP_PRIORITY) |
| 203 | .fromApp(appId) |
| 204 | .makePermanent() |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 205 | .forTable(JUMP_TABLE) |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 206 | .build(); |
| 207 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 208 | applyRules(true, flowRule); |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 209 | } |
| 210 | |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 211 | private void applyRules(boolean install, FlowRule flowRule) { |
| 212 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
| 213 | |
| 214 | flowOpsBuilder = install ? flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule); |
| 215 | |
| 216 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 217 | @Override |
| 218 | public void onSuccess(FlowRuleOperations ops) { |
| 219 | log.debug("Provisioned vni or forwarding table"); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public void onError(FlowRuleOperations ops) { |
| 224 | log.debug("Failed to privision vni or forwarding table"); |
| 225 | } |
| 226 | })); |
| 227 | } |
| 228 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 229 | private FlowRule processVersatile(ForwardingObjective forwardingObjective) { |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 230 | log.debug("Processing versatile forwarding objective"); |
| 231 | |
| 232 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 233 | .forDevice(deviceId) |
| 234 | .withSelector(forwardingObjective.selector()) |
| 235 | .withTreatment(forwardingObjective.treatment()) |
| 236 | .withPriority(forwardingObjective.priority()) |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 237 | .fromApp(forwardingObjective.appId()) |
| 238 | .forTable(SRC_VNI_TABLE); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 239 | |
| 240 | if (forwardingObjective.permanent()) { |
| 241 | ruleBuilder.makePermanent(); |
| 242 | } else { |
| 243 | ruleBuilder.makeTemporary(TIME_OUT); |
| 244 | } |
| 245 | |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 246 | return ruleBuilder.build(); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 247 | } |
| 248 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 249 | private FlowRule processSpecific(ForwardingObjective forwardingObjective) { |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 250 | log.debug("Processing specific forwarding objective"); |
| 251 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 252 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 253 | Optional<Instruction> group = forwardingObjective.treatment().immediate().stream() |
| 254 | .filter(i -> i.type() == Instruction.Type.GROUP).findAny(); |
| 255 | int tableType = tableType(forwardingObjective); |
| 256 | if (tableType != LAST_TABLE && !group.isPresent()) { |
| 257 | treatment.transition(nextTable(tableType)); |
| 258 | } |
| 259 | forwardingObjective.treatment().allInstructions().stream() |
| 260 | .filter(i -> i.type() != Instruction.Type.NOACTION).forEach(treatment::add); |
| 261 | |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 262 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 263 | .forDevice(deviceId) |
| 264 | .withSelector(forwardingObjective.selector()) |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 265 | .withTreatment(treatment.build()) |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 266 | .withPriority(forwardingObjective.priority()) |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 267 | .fromApp(forwardingObjective.appId()) |
| 268 | .forTable(tableType); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 269 | |
| 270 | if (forwardingObjective.permanent()) { |
| 271 | ruleBuilder.makePermanent(); |
| 272 | } else { |
| 273 | ruleBuilder.makeTemporary(TIME_OUT); |
| 274 | } |
| 275 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 276 | return ruleBuilder.build(); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 277 | } |
| 278 | |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 279 | private int tableType(ForwardingObjective fo) { |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 280 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 281 | IPCriterion ipSrc = (IPCriterion) fo.selector().getCriterion(Criterion.Type.IPV4_SRC); |
| 282 | IPCriterion ipDst = (IPCriterion) fo.selector().getCriterion(Criterion.Type.IPV4_DST); |
| 283 | TunnelIdCriterion tunnelId = |
| 284 | (TunnelIdCriterion) fo.selector().getCriterion(Criterion.Type.TUNNEL_ID); |
daniel park | ac34890 | 2017-04-26 13:49:05 +0900 | [diff] [blame] | 285 | VlanIdCriterion vlanId = (VlanIdCriterion) fo.selector().getCriterion(Criterion.Type.VLAN_VID); |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 286 | PortCriterion inPort = (PortCriterion) fo.selector().getCriterion(Criterion.Type.IN_PORT); |
| 287 | Optional<Instruction> output = fo.treatment().immediate().stream() |
| 288 | .filter(i -> i.type() == Instruction.Type.OUTPUT).findAny(); |
| 289 | Optional<Instruction> group = fo.treatment().immediate().stream() |
| 290 | .filter(i -> i.type() == Instruction.Type.GROUP).findAny(); |
| 291 | |
| 292 | // TODO: Add the Connection Tracking Table |
| 293 | if (inPort != null) { |
| 294 | return SRC_VNI_TABLE; |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 295 | } else if ((tunnelId != null && ipSrc != null && ipDst != null) || |
daniel park | ac34890 | 2017-04-26 13:49:05 +0900 | [diff] [blame] | 296 | (vlanId != null && ipSrc != null && ipDst != null) || |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 297 | (ipSrc != null && group.isPresent())) { |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 298 | return ROUTING_TABLE; |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 299 | } else if (output.isPresent() || (ipDst != null && group.isPresent())) { |
| 300 | return FORWARDING_TABLE; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 301 | } else if ((ipSrc != null && ipSrc.ip().prefixLength() == 32 && |
| 302 | ipDst != null && ipDst.ip().prefixLength() == 32) || |
| 303 | (ipSrc != null && ipSrc.ip().prefixLength() == 32 && ipDst == null) || |
| 304 | (ipDst != null && ipDst.ip().prefixLength() == 32 && ipSrc == null) || |
| 305 | (ipDst != null && ipDst.ip().prefixLength() == 32 && ipSrc != null) || |
| 306 | (ipSrc != null && ipSrc.ip().prefixLength() == 32 && ipDst != null)) { |
| 307 | return ACL_TABLE; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | return DUMMY_TABLE; |
| 311 | } |
| 312 | |
Hyunsun Moon | 4e252f2 | 2017-02-18 02:07:49 +0900 | [diff] [blame] | 313 | private int nextTable(int baseTable) { |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 314 | return baseTable + 1; |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | private void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 318 | obj.context().ifPresent(context -> context.onError(obj, error)); |
daniel | c8620b1 | 2015-11-30 15:50:59 +0900 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |