blob: af22d6ac7f088ab79ebbbd91d279b3b62777f971 [file] [log] [blame]
Andreas Papazoisa9964ea2016-01-08 15:58:22 +02001/*
Andreas Papazoise6aebaa2016-05-26 15:25:51 +03002 * Copyright 2016-present Open Networking Laboratory
Andreas Papazoisa9964ea2016-01-08 15:58:22 +02003 *
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.sdxl3;
18
19import com.google.common.collect.ImmutableList;
Andreas Papazoiseb518932016-06-21 13:46:20 +030020import com.google.common.collect.Sets;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.onlab.packet.Ethernet;
27import org.onlab.packet.IpAddress;
28import org.onlab.packet.IpPrefix;
29import org.onlab.packet.MacAddress;
30import org.onlab.packet.VlanId;
31import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
33import org.onosproject.incubator.net.intf.Interface;
Andreas Papazoiseb518932016-06-21 13:46:20 +030034import org.onosproject.incubator.net.intf.InterfaceEvent;
35import org.onosproject.incubator.net.intf.InterfaceListener;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020036import org.onosproject.incubator.net.intf.InterfaceService;
Andreas Papazoiseb518932016-06-21 13:46:20 +030037import org.onosproject.incubator.net.routing.ResolvedRoute;
38import org.onosproject.incubator.net.routing.RouteEvent;
39import org.onosproject.incubator.net.routing.RouteListener;
40import org.onosproject.incubator.net.routing.RouteService;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020041import org.onosproject.net.ConnectPoint;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
44import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
46import org.onosproject.net.intent.Constraint;
47import org.onosproject.net.intent.Key;
48import org.onosproject.net.intent.MultiPointToSinglePointIntent;
49import org.onosproject.net.intent.constraint.PartialFailureConstraint;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020050import org.onosproject.routing.IntentSynchronizationService;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020051import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020054import java.util.HashSet;
55import java.util.Map;
56import java.util.Set;
57import java.util.concurrent.ConcurrentHashMap;
58
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020059/**
60 * FIB component of SDX-L3.
61 */
62@Component(immediate = true, enabled = false)
63public class SdxL3Fib {
64 private Logger log = LoggerFactory.getLogger(getClass());
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected InterfaceService interfaceService;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected IntentSynchronizationService intentSynchronizer;
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected CoreService coreService;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andreas Papazoiseb518932016-06-21 13:46:20 +030076 protected RouteService routeService;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020077
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andreas Papazoisc2c45012016-01-20 14:26:11 +020079 protected SdxL3PeerService peerService;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020080
Andreas Papazoiseb518932016-06-21 13:46:20 +030081 private final InternalRouteListener routeListener = new InternalRouteListener();
82 private final InternalInterfaceListener interfaceListener = new InternalInterfaceListener();
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020083
84 private static final int PRIORITY_OFFSET = 100;
85 private static final int PRIORITY_MULTIPLIER = 5;
86 protected static final ImmutableList<Constraint> CONSTRAINTS
87 = ImmutableList.of(new PartialFailureConstraint());
88
89 private final Map<IpPrefix, MultiPointToSinglePointIntent> routeIntents
90 = new ConcurrentHashMap<>();
91
92 private ApplicationId appId;
93
94 @Activate
95 public void activate() {
96 appId = coreService.getAppId(SdxL3.SDX_L3_APP);
97
Andreas Papazoiseb518932016-06-21 13:46:20 +030098 interfaceService.addListener(interfaceListener);
99
100 routeService.addListener(routeListener);
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200101 }
102
103 @Deactivate
104 public void deactivate() {
Andreas Papazoiseb518932016-06-21 13:46:20 +0300105 interfaceService.removeListener(interfaceListener);
106 routeService.removeListener(routeListener);
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200107 }
108
Andreas Papazoiseb518932016-06-21 13:46:20 +0300109 private void update(ResolvedRoute route) {
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200110 synchronized (this) {
Andreas Papazoiseb518932016-06-21 13:46:20 +0300111 IpPrefix prefix = route.prefix();
112 MultiPointToSinglePointIntent intent =
113 generateRouteIntent(prefix, route.nextHop(), route.nextHopMac());
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200114
Andreas Papazoiseb518932016-06-21 13:46:20 +0300115 if (intent == null) {
116 log.debug("SDX-L3 no interface found for route {}", route);
117 return;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200118 }
119
Andreas Papazoiseb518932016-06-21 13:46:20 +0300120 routeIntents.put(prefix, intent);
121 intentSynchronizer.submit(intent);
122 }
123 }
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200124
Andreas Papazoiseb518932016-06-21 13:46:20 +0300125 private void withdraw(ResolvedRoute route) {
126 synchronized (this) {
127 IpPrefix prefix = route.prefix();
128 MultiPointToSinglePointIntent intent = routeIntents.remove(prefix);
129 if (intent == null) {
130 log.trace("SDX-L3 no intent in routeIntents to delete " +
131 "for prefix: {}", prefix);
132 return;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200133 }
Andreas Papazoiseb518932016-06-21 13:46:20 +0300134 intentSynchronizer.withdraw(intent);
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200135 }
136 }
137
138 /**
139 * Generates a route intent for a prefix, the next hop IP address, and
140 * the next hop MAC address.
141 * <p/>
142 * This method will find the egress interface for the intent.
143 * Intent will match dst IP prefix and rewrite dst MAC address at all other
144 * border switches, then forward packets according to dst MAC address.
145 *
146 * @param prefix IP prefix of the route to add
147 * @param nextHopIpAddress IP address of the next hop
148 * @param nextHopMacAddress MAC address of the next hop
149 * @return the generated intent, or null if no intent should be submitted
150 */
151 private MultiPointToSinglePointIntent generateRouteIntent(
152 IpPrefix prefix,
153 IpAddress nextHopIpAddress,
154 MacAddress nextHopMacAddress) {
155
156 // Find the attachment point (egress interface) of the next hop
Andreas Papazoisc2c45012016-01-20 14:26:11 +0200157 Interface egressInterface = peerService.getInterfaceForPeer(nextHopIpAddress);
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200158
159 if (egressInterface == null) {
160 log.warn("No outgoing interface found for {}",
Andreas Papazoiseb518932016-06-21 13:46:20 +0300161 nextHopIpAddress);
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200162 return null;
163 }
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200164 ConnectPoint egressPort = egressInterface.connectPoint();
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200165
Andreas Papazoiseb518932016-06-21 13:46:20 +0300166 Set<Interface> ingressInterfaces = new HashSet<>();
167 Set<ConnectPoint> ingressPorts = new HashSet<>();
168 log.debug("Generating intent for prefix {}, next hop mac {}",
169 prefix, nextHopMacAddress);
170
171 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
172
173 // Get ingress interfaces and ports
174 // TODO this should be only peering interfaces
175 interfaceService.getInterfaces().stream()
176 .filter(intf -> !intf.equals(egressInterface))
177 .forEach(intf -> {
178 ingressInterfaces.add(intf);
179 ConnectPoint ingressPort = intf.connectPoint();
180 ingressPorts.add(ingressPort);
181 });
182
183 // By default the ingress traffic is not tagged
184 VlanId ingressVlanId = VlanId.NONE;
185
186 // Match VLAN Id ANY if the source VLAN Id is not null
187 // TODO need to be able to set a different VLAN Id per ingress interface
188 for (Interface intf : ingressInterfaces) {
189 if (!intf.vlan().equals(VlanId.NONE)) {
190 selector.matchVlanId(VlanId.ANY);
191 ingressVlanId = intf.vlan();
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200192 }
193 }
194
195 // Match the destination IP prefix at the first hop
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200196 if (prefix.isIp4()) {
197 selector.matchEthType(Ethernet.TYPE_IPV4);
198 // if it is default route, then we do not need match destination
199 // IP address
200 if (prefix.prefixLength() != 0) {
201 selector.matchIPDst(prefix);
202 }
203 } else {
204 selector.matchEthType(Ethernet.TYPE_IPV6);
205 // if it is default route, then we do not need match destination
206 // IP address
207 if (prefix.prefixLength() != 0) {
208 selector.matchIPv6Dst(prefix);
209 }
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200210 }
211
212 // Rewrite the destination MAC address
213 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
214 .setEthDst(nextHopMacAddress);
Andreas Papazoiseb518932016-06-21 13:46:20 +0300215
216 // Set egress VLAN Id
217 // TODO need to make the comparison with different ingress VLAN Ids
218 if (!ingressVlanId.equals(egressInterface.vlan())) {
219 if (egressInterface.vlan().equals(VlanId.NONE)) {
220 treatment.popVlan();
221 } else {
222 treatment.setVlanId(egressInterface.vlan());
223 }
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200224 }
225
Andreas Papazoiseb518932016-06-21 13:46:20 +0300226 // Set priority
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200227 int priority =
228 prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
Andreas Papazoiseb518932016-06-21 13:46:20 +0300229
230 // Set key
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200231 Key key = Key.of(prefix.toString(), appId);
Andreas Papazoiseb518932016-06-21 13:46:20 +0300232
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200233 return MultiPointToSinglePointIntent.builder()
234 .appId(appId)
235 .key(key)
236 .selector(selector.build())
237 .treatment(treatment.build())
238 .ingressPoints(ingressPorts)
239 .egressPoint(egressPort)
240 .priority(priority)
241 .constraints(CONSTRAINTS)
242 .build();
243 }
244
Andreas Papazoiseb518932016-06-21 13:46:20 +0300245 private void updateInterface(Interface intf) {
246 synchronized (this) {
247 for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : routeIntents.entrySet()) {
248 MultiPointToSinglePointIntent intent = entry.getValue();
249 Set<ConnectPoint> ingress = Sets.newHashSet(intent.ingressPoints());
250 ingress.add(intf.connectPoint());
251
252 MultiPointToSinglePointIntent newIntent =
253 MultiPointToSinglePointIntent.builder(intent)
254 .ingressPoints(ingress)
255 .build();
256
257 routeIntents.put(entry.getKey(), newIntent);
258 intentSynchronizer.submit(newIntent);
259 }
260 }
261 }
262
263 private void removeInterface(Interface intf) {
264 synchronized (this) {
265 for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : routeIntents.entrySet()) {
266 MultiPointToSinglePointIntent intent = entry.getValue();
267 if (intent.egressPoint().equals(intf.connectPoint())) {
268 // This intent just lost its head. Remove it and let
269 // higher layer routing reroute.
270 intentSynchronizer.withdraw(routeIntents.remove(entry.getKey()));
271 } else {
272 if (intent.ingressPoints().contains(intf.connectPoint())) {
273
274 Set<ConnectPoint> ingress = Sets.newHashSet(intent.ingressPoints());
275 ingress.remove(intf.connectPoint());
276
277 MultiPointToSinglePointIntent newIntent =
278 MultiPointToSinglePointIntent.builder(intent)
279 .ingressPoints(ingress)
280 .build();
281
282 routeIntents.put(entry.getKey(), newIntent);
283 intentSynchronizer.submit(newIntent);
284 }
285 }
286 }
287 }
288 }
289
290 private class InternalRouteListener implements RouteListener {
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200291 @Override
Andreas Papazoiseb518932016-06-21 13:46:20 +0300292 public void event(RouteEvent event) {
293 switch (event.type()) {
294 case ROUTE_ADDED:
295 case ROUTE_UPDATED:
296 update(event.subject());
297 break;
298 case ROUTE_REMOVED:
299 withdraw(event.subject());
300 break;
301 default:
302 break;
303 }
304 }
305 }
306
307 private class InternalInterfaceListener implements InterfaceListener {
308
309 @Override
310 public void event(InterfaceEvent event) {
311 switch (event.type()) {
312 case INTERFACE_ADDED:
313 updateInterface(event.subject());
314 break;
315 case INTERFACE_UPDATED:
316 break;
317 case INTERFACE_REMOVED:
318 removeInterface(event.subject());
319 break;
320 default:
321 break;
322 }
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200323 }
324 }
325
326}