blob: 0a060ffefe297f127ee05ab4cce8d22aa79be5d5 [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) {
Jian Li8abf2fe2018-06-12 18:42:30 +0900146 connectTables(deviceId, Constants.STAT_INBOUND_TABLE, Constants.DHCP_ARP_TABLE);
Jian Li70a2c3f2018-04-13 17:26:31 +0900147 connectTables(deviceId, Constants.DHCP_ARP_TABLE, Constants.VTAG_TABLE);
148 connectTables(deviceId, Constants.VTAG_TABLE, Constants.ACL_TABLE);
sanghodbee2332017-05-18 09:59:16 +0900149 connectTables(deviceId, Constants.ACL_TABLE, Constants.JUMP_TABLE);
sanghodbee2332017-05-18 09:59:16 +0900150 setupJumpTable(deviceId);
Jian Li8abf2fe2018-06-12 18:42:30 +0900151 connectTables(deviceId, Constants.STAT_OUTBOUND_TABLE, Constants.FORWARDING_TABLE);
sanghodbee2332017-05-18 09:59:16 +0900152 }
153
sangho3dd2a8b2017-07-19 15:54:31 +0900154 @Override
155 public void connectTables(DeviceId deviceId, int fromTable, int toTable) {
sanghodbee2332017-05-18 09:59:16 +0900156 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
157 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
158
159 treatment.transition(toTable);
160
161 FlowRule flowRule = DefaultFlowRule.builder()
162 .forDevice(deviceId)
163 .withSelector(selector.build())
164 .withTreatment(treatment.build())
165 .withPriority(DROP_PRIORITY)
166 .fromApp(appId)
167 .makePermanent()
168 .forTable(fromTable)
169 .build();
170
sanghodc375372017-06-08 10:41:30 +0900171 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900172 }
173
sangho3dd2a8b2017-07-19 15:54:31 +0900174 @Override
175 public void setUpTableMissEntry(DeviceId deviceId, int table) {
sanghodbee2332017-05-18 09:59:16 +0900176 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
177 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
178
179 treatment.drop();
180
181 FlowRule flowRule = DefaultFlowRule.builder()
182 .forDevice(deviceId)
183 .withSelector(selector.build())
184 .withTreatment(treatment.build())
185 .withPriority(DROP_PRIORITY)
186 .fromApp(appId)
187 .makePermanent()
188 .forTable(table)
189 .build();
190
sanghodc375372017-06-08 10:41:30 +0900191 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900192 }
193
194 private void setupJumpTable(DeviceId deviceId) {
195 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
196 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
197
198 selector.matchEthDst(Constants.DEFAULT_GATEWAY_MAC);
199 treatment.transition(Constants.ROUTING_TABLE);
200
201 FlowRule flowRule = DefaultFlowRule.builder()
202 .forDevice(deviceId)
203 .withSelector(selector.build())
204 .withTreatment(treatment.build())
205 .withPriority(HIGH_PRIORITY)
206 .fromApp(appId)
207 .makePermanent()
208 .forTable(Constants.JUMP_TABLE)
209 .build();
210
sanghodc375372017-06-08 10:41:30 +0900211 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900212
213 selector = DefaultTrafficSelector.builder();
214 treatment = DefaultTrafficTreatment.builder();
215
Jian Li8abf2fe2018-06-12 18:42:30 +0900216 treatment.transition(Constants.STAT_OUTBOUND_TABLE);
sanghodbee2332017-05-18 09:59:16 +0900217
218 flowRule = DefaultFlowRule.builder()
219 .forDevice(deviceId)
220 .withSelector(selector.build())
221 .withTreatment(treatment.build())
222 .withPriority(DROP_PRIORITY)
223 .fromApp(appId)
224 .makePermanent()
225 .forTable(Constants.JUMP_TABLE)
226 .build();
227
sanghodc375372017-06-08 10:41:30 +0900228 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900229 }
230
231 private class InternalOpenstackNodeListener implements OpenstackNodeListener {
232
233 @Override
234 public void event(OpenstackNodeEvent event) {
235 OpenstackNode osNode = event.subject();
236 // TODO check leadership of the node and make only the leader process
237
238 switch (event.type()) {
Hyunsun Moon0d457362017-06-27 17:19:41 +0900239 case OPENSTACK_NODE_COMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900240 deviceEventExecutor.execute(() -> {
241 log.info("COMPLETE node {} is detected", osNode.hostname());
sanghodc375372017-06-08 10:41:30 +0900242 processCompleteNode(event.subject());
sanghodbee2332017-05-18 09:59:16 +0900243 });
244 break;
Hyunsun Moon0d457362017-06-27 17:19:41 +0900245 case OPENSTACK_NODE_CREATED:
246 case OPENSTACK_NODE_UPDATED:
247 case OPENSTACK_NODE_REMOVED:
248 case OPENSTACK_NODE_INCOMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900249 default:
Hyunsun Moon0d457362017-06-27 17:19:41 +0900250 // do nothing
sanghodbee2332017-05-18 09:59:16 +0900251 break;
252 }
253 }
254
255 private void processCompleteNode(OpenstackNode osNode) {
Hyunsun Moon0d457362017-06-27 17:19:41 +0900256 if (osNode.type().equals(OpenstackNode.NodeType.COMPUTE)) {
257 initializePipeline(osNode.intgBridge());
sanghodbee2332017-05-18 09:59:16 +0900258 }
259 }
260 }
261}