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