blob: d4a8b6886abe974e0eac0c26e71ee39eed3d33a3 [file] [log] [blame]
sanghodbee2332017-05-18 09:59:16 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
sanghodbee2332017-05-18 09:59:16 +09003 *
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
17package org.onosproject.openstacknetworking.impl;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.flow.DefaultFlowRule;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.FlowRule;
32import org.onosproject.net.flow.FlowRuleOperations;
33import org.onosproject.net.flow.FlowRuleOperationsContext;
34import org.onosproject.net.flow.FlowRuleService;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
sanghodbee2332017-05-18 09:59:16 +090037import org.onosproject.openstacknetworking.api.Constants;
38import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService;
Hyunsun Moon0d457362017-06-27 17:19:41 +090039import org.onosproject.openstacknode.api.OpenstackNode;
40import org.onosproject.openstacknode.api.OpenstackNodeEvent;
41import org.onosproject.openstacknode.api.OpenstackNodeListener;
42import org.onosproject.openstacknode.api.OpenstackNodeService;
sanghodbee2332017-05-18 09:59:16 +090043import org.slf4j.Logger;
44
45import java.util.concurrent.ExecutorService;
46import java.util.concurrent.Executors;
47
48import static org.onlab.util.Tools.groupedThreads;
49import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID;
50import static org.slf4j.LoggerFactory.getLogger;
51
52/**
53 * Sets flow rules directly using FlowRuleService.
54 */
55@Service
56@Component(immediate = true)
57public 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;
sanghodc375372017-06-08 10:41:30 +090063 private static final int TIMEOUT_SNAT_RULE = 60;
sanghodbee2332017-05-18 09:59:16 +090064
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 Moon0d457362017-06-27 17:19:41 +090076 private final OpenstackNodeListener internalNodeListener = new InternalOpenstackNodeListener();
sanghodbee2332017-05-18 09:59:16 +090077
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
sanghoc395d0f2017-07-11 10:05:25 +090086 osNodeService.completeNodes(OpenstackNode.NodeType.COMPUTE)
87 .forEach(node -> initializePipeline(node.intgBridge()));
88
sanghodbee2332017-05-18 09:59:16 +090089 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
sanghodc375372017-06-08 10:41:30 +0900100
sanghodbee2332017-05-18 09:59:16 +0900101 @Override
sanghodc375372017-06-08 10:41:30 +0900102 public void setRule(ApplicationId appId,
103 DeviceId deviceId,
104 TrafficSelector selector,
105 TrafficTreatment treatment,
106 int priority,
107 int tableType,
108 boolean install) {
sanghodbee2332017-05-18 09:59:16 +0900109
sanghodc375372017-06-08 10:41:30 +0900110 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder()
sanghodbee2332017-05-18 09:59:16 +0900111 .forDevice(deviceId)
sanghodc375372017-06-08 10:41:30 +0900112 .withSelector(selector)
113 .withTreatment(treatment)
114 .withPriority(priority)
115 .fromApp(appId)
sanghodbee2332017-05-18 09:59:16 +0900116 .forTable(tableType);
117
sanghodc375372017-06-08 10:41:30 +0900118 if (priority == Constants.PRIORITY_SNAT_RULE) {
119 flowRuleBuilder.makeTemporary(TIMEOUT_SNAT_RULE);
sanghodbee2332017-05-18 09:59:16 +0900120 } else {
sanghodc375372017-06-08 10:41:30 +0900121 flowRuleBuilder.makePermanent();
sanghodbee2332017-05-18 09:59:16 +0900122 }
123
sanghodc375372017-06-08 10:41:30 +0900124 applyRule(flowRuleBuilder.build(), install);
sanghodbee2332017-05-18 09:59:16 +0900125 }
126
sanghodc375372017-06-08 10:41:30 +0900127 private void applyRule(FlowRule flowRule, boolean install) {
sanghodbee2332017-05-18 09:59:16 +0900128 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) {
140 log.debug("Failed to privision vni or forwarding table");
141 }
142 }));
143 }
144
145 private void initializePipeline(DeviceId deviceId) {
146 connectTables(deviceId, Constants.SRC_VNI_TABLE, Constants.ACL_TABLE);
147 connectTables(deviceId, Constants.ACL_TABLE, Constants.JUMP_TABLE);
sanghodbee2332017-05-18 09:59:16 +0900148 setupJumpTable(deviceId);
149 }
150
sangho3dd2a8b2017-07-19 15:54:31 +0900151 @Override
152 public void connectTables(DeviceId deviceId, int fromTable, int toTable) {
sanghodbee2332017-05-18 09:59:16 +0900153 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
154 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
155
156 treatment.transition(toTable);
157
158 FlowRule flowRule = DefaultFlowRule.builder()
159 .forDevice(deviceId)
160 .withSelector(selector.build())
161 .withTreatment(treatment.build())
162 .withPriority(DROP_PRIORITY)
163 .fromApp(appId)
164 .makePermanent()
165 .forTable(fromTable)
166 .build();
167
sanghodc375372017-06-08 10:41:30 +0900168 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900169 }
170
sangho3dd2a8b2017-07-19 15:54:31 +0900171 @Override
172 public void setUpTableMissEntry(DeviceId deviceId, int table) {
sanghodbee2332017-05-18 09:59:16 +0900173 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
174 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
175
176 treatment.drop();
177
178 FlowRule flowRule = DefaultFlowRule.builder()
179 .forDevice(deviceId)
180 .withSelector(selector.build())
181 .withTreatment(treatment.build())
182 .withPriority(DROP_PRIORITY)
183 .fromApp(appId)
184 .makePermanent()
185 .forTable(table)
186 .build();
187
sanghodc375372017-06-08 10:41:30 +0900188 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900189 }
190
191 private void setupJumpTable(DeviceId deviceId) {
192 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
193 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
194
195 selector.matchEthDst(Constants.DEFAULT_GATEWAY_MAC);
196 treatment.transition(Constants.ROUTING_TABLE);
197
198 FlowRule flowRule = DefaultFlowRule.builder()
199 .forDevice(deviceId)
200 .withSelector(selector.build())
201 .withTreatment(treatment.build())
202 .withPriority(HIGH_PRIORITY)
203 .fromApp(appId)
204 .makePermanent()
205 .forTable(Constants.JUMP_TABLE)
206 .build();
207
sanghodc375372017-06-08 10:41:30 +0900208 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900209
210 selector = DefaultTrafficSelector.builder();
211 treatment = DefaultTrafficTreatment.builder();
212
213 treatment.transition(Constants.FORWARDING_TABLE);
214
215 flowRule = DefaultFlowRule.builder()
216 .forDevice(deviceId)
217 .withSelector(selector.build())
218 .withTreatment(treatment.build())
219 .withPriority(DROP_PRIORITY)
220 .fromApp(appId)
221 .makePermanent()
222 .forTable(Constants.JUMP_TABLE)
223 .build();
224
sanghodc375372017-06-08 10:41:30 +0900225 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900226 }
227
228 private class InternalOpenstackNodeListener implements OpenstackNodeListener {
229
230 @Override
231 public void event(OpenstackNodeEvent event) {
232 OpenstackNode osNode = event.subject();
233 // TODO check leadership of the node and make only the leader process
234
235 switch (event.type()) {
Hyunsun Moon0d457362017-06-27 17:19:41 +0900236 case OPENSTACK_NODE_COMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900237 deviceEventExecutor.execute(() -> {
238 log.info("COMPLETE node {} is detected", osNode.hostname());
sanghodc375372017-06-08 10:41:30 +0900239 processCompleteNode(event.subject());
sanghodbee2332017-05-18 09:59:16 +0900240 });
241 break;
Hyunsun Moon0d457362017-06-27 17:19:41 +0900242 case OPENSTACK_NODE_CREATED:
243 case OPENSTACK_NODE_UPDATED:
244 case OPENSTACK_NODE_REMOVED:
245 case OPENSTACK_NODE_INCOMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900246 default:
Hyunsun Moon0d457362017-06-27 17:19:41 +0900247 // do nothing
sanghodbee2332017-05-18 09:59:16 +0900248 break;
249 }
250 }
251
252 private void processCompleteNode(OpenstackNode osNode) {
Hyunsun Moon0d457362017-06-27 17:19:41 +0900253 if (osNode.type().equals(OpenstackNode.NodeType.COMPUTE)) {
254 initializePipeline(osNode.intgBridge());
sanghodbee2332017-05-18 09:59:16 +0900255 }
256 }
257 }
258}