blob: 48983e21b5ac3b177681ce604f72982a6ba08563 [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 Li70a2c3f2018-04-13 17:26:31 +0900146 connectTables(deviceId, Constants.DHCP_ARP_TABLE, Constants.VTAG_TABLE);
147 connectTables(deviceId, Constants.VTAG_TABLE, Constants.ACL_TABLE);
sanghodbee2332017-05-18 09:59:16 +0900148 connectTables(deviceId, Constants.ACL_TABLE, Constants.JUMP_TABLE);
sanghodbee2332017-05-18 09:59:16 +0900149 setupJumpTable(deviceId);
150 }
151
sangho3dd2a8b2017-07-19 15:54:31 +0900152 @Override
153 public void connectTables(DeviceId deviceId, int fromTable, int toTable) {
sanghodbee2332017-05-18 09:59:16 +0900154 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
155 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
156
157 treatment.transition(toTable);
158
159 FlowRule flowRule = DefaultFlowRule.builder()
160 .forDevice(deviceId)
161 .withSelector(selector.build())
162 .withTreatment(treatment.build())
163 .withPriority(DROP_PRIORITY)
164 .fromApp(appId)
165 .makePermanent()
166 .forTable(fromTable)
167 .build();
168
sanghodc375372017-06-08 10:41:30 +0900169 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900170 }
171
sangho3dd2a8b2017-07-19 15:54:31 +0900172 @Override
173 public void setUpTableMissEntry(DeviceId deviceId, int table) {
sanghodbee2332017-05-18 09:59:16 +0900174 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
175 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
176
177 treatment.drop();
178
179 FlowRule flowRule = DefaultFlowRule.builder()
180 .forDevice(deviceId)
181 .withSelector(selector.build())
182 .withTreatment(treatment.build())
183 .withPriority(DROP_PRIORITY)
184 .fromApp(appId)
185 .makePermanent()
186 .forTable(table)
187 .build();
188
sanghodc375372017-06-08 10:41:30 +0900189 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900190 }
191
192 private void setupJumpTable(DeviceId deviceId) {
193 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
194 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
195
196 selector.matchEthDst(Constants.DEFAULT_GATEWAY_MAC);
197 treatment.transition(Constants.ROUTING_TABLE);
198
199 FlowRule flowRule = DefaultFlowRule.builder()
200 .forDevice(deviceId)
201 .withSelector(selector.build())
202 .withTreatment(treatment.build())
203 .withPriority(HIGH_PRIORITY)
204 .fromApp(appId)
205 .makePermanent()
206 .forTable(Constants.JUMP_TABLE)
207 .build();
208
sanghodc375372017-06-08 10:41:30 +0900209 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900210
211 selector = DefaultTrafficSelector.builder();
212 treatment = DefaultTrafficTreatment.builder();
213
214 treatment.transition(Constants.FORWARDING_TABLE);
215
216 flowRule = DefaultFlowRule.builder()
217 .forDevice(deviceId)
218 .withSelector(selector.build())
219 .withTreatment(treatment.build())
220 .withPriority(DROP_PRIORITY)
221 .fromApp(appId)
222 .makePermanent()
223 .forTable(Constants.JUMP_TABLE)
224 .build();
225
sanghodc375372017-06-08 10:41:30 +0900226 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900227 }
228
229 private class InternalOpenstackNodeListener implements OpenstackNodeListener {
230
231 @Override
232 public void event(OpenstackNodeEvent event) {
233 OpenstackNode osNode = event.subject();
234 // TODO check leadership of the node and make only the leader process
235
236 switch (event.type()) {
Hyunsun Moon0d457362017-06-27 17:19:41 +0900237 case OPENSTACK_NODE_COMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900238 deviceEventExecutor.execute(() -> {
239 log.info("COMPLETE node {} is detected", osNode.hostname());
sanghodc375372017-06-08 10:41:30 +0900240 processCompleteNode(event.subject());
sanghodbee2332017-05-18 09:59:16 +0900241 });
242 break;
Hyunsun Moon0d457362017-06-27 17:19:41 +0900243 case OPENSTACK_NODE_CREATED:
244 case OPENSTACK_NODE_UPDATED:
245 case OPENSTACK_NODE_REMOVED:
246 case OPENSTACK_NODE_INCOMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900247 default:
Hyunsun Moon0d457362017-06-27 17:19:41 +0900248 // do nothing
sanghodbee2332017-05-18 09:59:16 +0900249 break;
250 }
251 }
252
253 private void processCompleteNode(OpenstackNode osNode) {
Hyunsun Moon0d457362017-06-27 17:19:41 +0900254 if (osNode.type().equals(OpenstackNode.NodeType.COMPUTE)) {
255 initializePipeline(osNode.intgBridge());
sanghodbee2332017-05-18 09:59:16 +0900256 }
257 }
258 }
259}