sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
sangho | dbee233 | 2017-05-18 09:59:16 +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 | */ |
| 16 | |
| 17 | package org.onosproject.openstacknetworking.impl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.flow.DefaultFlowRule; |
| 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 30 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 31 | import org.onosproject.net.flow.FlowRule; |
| 32 | import org.onosproject.net.flow.FlowRuleOperations; |
| 33 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 34 | import org.onosproject.net.flow.FlowRuleService; |
| 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 37 | import org.onosproject.openstacknetworking.api.Constants; |
| 38 | import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 39 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 40 | import org.onosproject.openstacknode.api.OpenstackNodeEvent; |
| 41 | import org.onosproject.openstacknode.api.OpenstackNodeListener; |
| 42 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 43 | import org.slf4j.Logger; |
| 44 | |
| 45 | import java.util.concurrent.ExecutorService; |
| 46 | import java.util.concurrent.Executors; |
| 47 | |
| 48 | import static org.onlab.util.Tools.groupedThreads; |
| 49 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID; |
| 50 | import static org.slf4j.LoggerFactory.getLogger; |
| 51 | |
| 52 | /** |
| 53 | * Sets flow rules directly using FlowRuleService. |
| 54 | */ |
| 55 | @Service |
| 56 | @Component(immediate = true) |
| 57 | public class OpenstackFlowRuleManager implements OpenstackFlowRuleService { |
| 58 | |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
| 61 | private static final int DROP_PRIORITY = 0; |
| 62 | private static final int HIGH_PRIORITY = 30000; |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 63 | private static final int TIMEOUT_SNAT_RULE = 60; |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected FlowRuleService flowRuleService; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected CoreService coreService; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected OpenstackNodeService osNodeService; |
| 73 | |
| 74 | private final ExecutorService deviceEventExecutor = |
| 75 | Executors.newSingleThreadExecutor(groupedThreads("openstacknetworking", "device-event")); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 76 | private final OpenstackNodeListener internalNodeListener = new InternalOpenstackNodeListener(); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 77 | |
| 78 | private ApplicationId appId; |
| 79 | |
| 80 | @Activate |
| 81 | protected void activate() { |
| 82 | appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID); |
| 83 | coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID); |
| 84 | osNodeService.addListener(internalNodeListener); |
| 85 | |
sangho | c395d0f | 2017-07-11 10:05:25 +0900 | [diff] [blame] | 86 | osNodeService.completeNodes(OpenstackNode.NodeType.COMPUTE) |
| 87 | .forEach(node -> initializePipeline(node.intgBridge())); |
| 88 | |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 89 | log.info("Started"); |
| 90 | } |
| 91 | |
| 92 | @Deactivate |
| 93 | protected void deactivate() { |
| 94 | osNodeService.removeListener(internalNodeListener); |
| 95 | deviceEventExecutor.shutdown(); |
| 96 | |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 100 | |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 101 | @Override |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 102 | public void setRule(ApplicationId appId, |
| 103 | DeviceId deviceId, |
| 104 | TrafficSelector selector, |
| 105 | TrafficTreatment treatment, |
| 106 | int priority, |
| 107 | int tableType, |
| 108 | boolean install) { |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 109 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 110 | FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder() |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 111 | .forDevice(deviceId) |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 112 | .withSelector(selector) |
| 113 | .withTreatment(treatment) |
| 114 | .withPriority(priority) |
| 115 | .fromApp(appId) |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 116 | .forTable(tableType); |
| 117 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 118 | if (priority == Constants.PRIORITY_SNAT_RULE) { |
| 119 | flowRuleBuilder.makeTemporary(TIMEOUT_SNAT_RULE); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 120 | } else { |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 121 | flowRuleBuilder.makePermanent(); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 122 | } |
| 123 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 124 | applyRule(flowRuleBuilder.build(), install); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 125 | } |
| 126 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 127 | private void applyRule(FlowRule flowRule, boolean install) { |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 128 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
| 129 | |
| 130 | flowOpsBuilder = install ? flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule); |
| 131 | |
| 132 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 133 | @Override |
| 134 | public void onSuccess(FlowRuleOperations ops) { |
| 135 | log.debug("Provisioned vni or forwarding table"); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void onError(FlowRuleOperations ops) { |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 140 | log.debug("Failed to provision vni or forwarding table"); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 141 | } |
| 142 | })); |
| 143 | } |
| 144 | |
| 145 | private void initializePipeline(DeviceId deviceId) { |
Jian Li | 87ded82 | 2018-07-02 18:31:22 +0900 | [diff] [blame] | 146 | // for inbound table transition |
| 147 | connectTables(deviceId, Constants.STAT_INBOUND_TABLE, Constants.VTAP_INBOUND_TABLE); |
| 148 | connectTables(deviceId, Constants.VTAP_INBOUND_TABLE, Constants.DHCP_ARP_TABLE); |
| 149 | |
| 150 | // for vTag and ACL table transition |
Jian Li | 70a2c3f | 2018-04-13 17:26:31 +0900 | [diff] [blame] | 151 | connectTables(deviceId, Constants.DHCP_ARP_TABLE, Constants.VTAG_TABLE); |
| 152 | connectTables(deviceId, Constants.VTAG_TABLE, Constants.ACL_TABLE); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 153 | connectTables(deviceId, Constants.ACL_TABLE, Constants.JUMP_TABLE); |
Jian Li | 87ded82 | 2018-07-02 18:31:22 +0900 | [diff] [blame] | 154 | |
| 155 | // for JUMP table transition |
| 156 | // we need JUMP table for bypassing routing table which contains large |
| 157 | // amount of flow rules which might cause performance degradation during |
| 158 | // table lookup |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 159 | setupJumpTable(deviceId); |
Jian Li | 87ded82 | 2018-07-02 18:31:22 +0900 | [diff] [blame] | 160 | |
| 161 | // for outbound table transition |
| 162 | connectTables(deviceId, Constants.STAT_OUTBOUND_TABLE, Constants.VTAP_OUTBOUND_TABLE); |
| 163 | connectTables(deviceId, Constants.VTAP_OUTBOUND_TABLE, Constants.FORWARDING_TABLE); |
| 164 | |
| 165 | // for FLAT outbound table transition |
| 166 | connectTables(deviceId, Constants.STAT_FLAT_OUTBOUND_TABLE, Constants.VTAP_FLAT_OUTBOUND_TABLE); |
| 167 | connectTables(deviceId, Constants.VTAP_FLAT_OUTBOUND_TABLE, Constants.FLAT_TABLE); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 168 | } |
| 169 | |
sangho | 3dd2a8b | 2017-07-19 15:54:31 +0900 | [diff] [blame] | 170 | @Override |
| 171 | public void connectTables(DeviceId deviceId, int fromTable, int toTable) { |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 172 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 173 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 174 | |
| 175 | treatment.transition(toTable); |
| 176 | |
| 177 | FlowRule flowRule = DefaultFlowRule.builder() |
| 178 | .forDevice(deviceId) |
| 179 | .withSelector(selector.build()) |
| 180 | .withTreatment(treatment.build()) |
| 181 | .withPriority(DROP_PRIORITY) |
| 182 | .fromApp(appId) |
| 183 | .makePermanent() |
| 184 | .forTable(fromTable) |
| 185 | .build(); |
| 186 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 187 | applyRule(flowRule, true); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 188 | } |
| 189 | |
sangho | 3dd2a8b | 2017-07-19 15:54:31 +0900 | [diff] [blame] | 190 | @Override |
| 191 | public void setUpTableMissEntry(DeviceId deviceId, int table) { |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 192 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 193 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 194 | |
| 195 | treatment.drop(); |
| 196 | |
| 197 | FlowRule flowRule = DefaultFlowRule.builder() |
| 198 | .forDevice(deviceId) |
| 199 | .withSelector(selector.build()) |
| 200 | .withTreatment(treatment.build()) |
| 201 | .withPriority(DROP_PRIORITY) |
| 202 | .fromApp(appId) |
| 203 | .makePermanent() |
| 204 | .forTable(table) |
| 205 | .build(); |
| 206 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 207 | applyRule(flowRule, true); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | private void setupJumpTable(DeviceId deviceId) { |
| 211 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 212 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 213 | |
| 214 | selector.matchEthDst(Constants.DEFAULT_GATEWAY_MAC); |
| 215 | treatment.transition(Constants.ROUTING_TABLE); |
| 216 | |
| 217 | FlowRule flowRule = DefaultFlowRule.builder() |
| 218 | .forDevice(deviceId) |
| 219 | .withSelector(selector.build()) |
| 220 | .withTreatment(treatment.build()) |
| 221 | .withPriority(HIGH_PRIORITY) |
| 222 | .fromApp(appId) |
| 223 | .makePermanent() |
| 224 | .forTable(Constants.JUMP_TABLE) |
| 225 | .build(); |
| 226 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 227 | applyRule(flowRule, true); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 228 | |
| 229 | selector = DefaultTrafficSelector.builder(); |
| 230 | treatment = DefaultTrafficTreatment.builder(); |
| 231 | |
Jian Li | 8abf2fe | 2018-06-12 18:42:30 +0900 | [diff] [blame] | 232 | treatment.transition(Constants.STAT_OUTBOUND_TABLE); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 233 | |
| 234 | flowRule = DefaultFlowRule.builder() |
| 235 | .forDevice(deviceId) |
| 236 | .withSelector(selector.build()) |
| 237 | .withTreatment(treatment.build()) |
| 238 | .withPriority(DROP_PRIORITY) |
| 239 | .fromApp(appId) |
| 240 | .makePermanent() |
| 241 | .forTable(Constants.JUMP_TABLE) |
| 242 | .build(); |
| 243 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 244 | applyRule(flowRule, true); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | private class InternalOpenstackNodeListener implements OpenstackNodeListener { |
| 248 | |
| 249 | @Override |
| 250 | public void event(OpenstackNodeEvent event) { |
| 251 | OpenstackNode osNode = event.subject(); |
| 252 | // TODO check leadership of the node and make only the leader process |
| 253 | |
| 254 | switch (event.type()) { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 255 | case OPENSTACK_NODE_COMPLETE: |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 256 | deviceEventExecutor.execute(() -> { |
| 257 | log.info("COMPLETE node {} is detected", osNode.hostname()); |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 258 | processCompleteNode(event.subject()); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 259 | }); |
| 260 | break; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 261 | case OPENSTACK_NODE_CREATED: |
| 262 | case OPENSTACK_NODE_UPDATED: |
| 263 | case OPENSTACK_NODE_REMOVED: |
| 264 | case OPENSTACK_NODE_INCOMPLETE: |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 265 | default: |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 266 | // do nothing |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | private void processCompleteNode(OpenstackNode osNode) { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 272 | if (osNode.type().equals(OpenstackNode.NodeType.COMPUTE)) { |
| 273 | initializePipeline(osNode.intgBridge()); |
sangho | dbee233 | 2017-05-18 09:59:16 +0900 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |