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