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