blob: d26d9de974a970e4816476978c83a0af662271d2 [file] [log] [blame]
Jonathan Hart9a426f82015-09-03 15:43:13 +02001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hart9a426f82015-09-03 15:43:13 +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.sdnip;
18
19import com.google.common.collect.ImmutableList;
Jonathan Hartb14221c2016-03-07 09:55:50 -080020import com.google.common.collect.Sets;
Jonathan Hart365335e2015-12-10 11:09:53 -080021import 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;
Jonathan Hart9a426f82015-09-03 15:43:13 +020026import 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;
Jonathan Hart365335e2015-12-10 11:09:53 -080032import org.onosproject.core.CoreService;
Jonathan Hart9a426f82015-09-03 15:43:13 +020033import org.onosproject.incubator.net.intf.Interface;
Jonathan Hartb14221c2016-03-07 09:55:50 -080034import org.onosproject.incubator.net.intf.InterfaceEvent;
35import org.onosproject.incubator.net.intf.InterfaceListener;
Jonathan Hart9a426f82015-09-03 15:43:13 +020036import org.onosproject.incubator.net.intf.InterfaceService;
Jonathan Harta2eb9ff2016-04-13 21:27:06 -070037import 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;
Jonathan Hart9a426f82015-09-03 15:43:13 +020041import org.onosproject.net.ConnectPoint;
Luca Pretee4a5e1a2016-09-07 17:01:22 -070042import org.onosproject.net.FilteredConnectPoint;
Jonathan Hart9a426f82015-09-03 15:43:13 +020043import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
47import org.onosproject.net.intent.Constraint;
48import org.onosproject.net.intent.Key;
49import org.onosproject.net.intent.MultiPointToSinglePointIntent;
50import org.onosproject.net.intent.constraint.PartialFailureConstraint;
Jonathan Hart9a426f82015-09-03 15:43:13 +020051import org.onosproject.routing.IntentSynchronizationService;
52import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
Jonathan Hart9a426f82015-09-03 15:43:13 +020055import java.util.Map;
56import java.util.Set;
57import java.util.concurrent.ConcurrentHashMap;
58
Jonathan Hart9a426f82015-09-03 15:43:13 +020059/**
60 * FIB component of SDN-IP.
61 */
Jonathan Hartc22e8472015-11-17 18:25:45 -080062@Component(immediate = true, enabled = false)
Jonathan Hart365335e2015-12-10 11:09:53 -080063public class SdnIpFib {
Jonathan Hart9a426f82015-09-03 15:43:13 +020064 private Logger log = LoggerFactory.getLogger(getClass());
65
Jonathan Hart365335e2015-12-10 11:09:53 -080066 @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)
Jonathan Harta2eb9ff2016-04-13 21:27:06 -070076 protected RouteService routeService;
Jonathan Hart365335e2015-12-10 11:09:53 -080077
Jonathan Harta2eb9ff2016-04-13 21:27:06 -070078 private final InternalRouteListener routeListener = new InternalRouteListener();
Jonathan Hartb14221c2016-03-07 09:55:50 -080079 private final InternalInterfaceListener interfaceListener = new InternalInterfaceListener();
Jonathan Hart365335e2015-12-10 11:09:53 -080080
Jonathan Hart9a426f82015-09-03 15:43:13 +020081 private static final int PRIORITY_OFFSET = 100;
82 private static final int PRIORITY_MULTIPLIER = 5;
83 protected static final ImmutableList<Constraint> CONSTRAINTS
84 = ImmutableList.of(new PartialFailureConstraint());
85
Jonathan Hart365335e2015-12-10 11:09:53 -080086 private final Map<IpPrefix, MultiPointToSinglePointIntent> routeIntents
87 = new ConcurrentHashMap<>();
Jonathan Hart9a426f82015-09-03 15:43:13 +020088
Jonathan Hart365335e2015-12-10 11:09:53 -080089 private ApplicationId appId;
Jonathan Hart9a426f82015-09-03 15:43:13 +020090
Jonathan Hart365335e2015-12-10 11:09:53 -080091 @Activate
92 public void activate() {
93 appId = coreService.getAppId(SdnIp.SDN_IP_APP);
Jonathan Hart9a426f82015-09-03 15:43:13 +020094
Jonathan Hartb14221c2016-03-07 09:55:50 -080095 interfaceService.addListener(interfaceListener);
96
Jonathan Harta2eb9ff2016-04-13 21:27:06 -070097 routeService.addListener(routeListener);
Jonathan Hart9a426f82015-09-03 15:43:13 +020098 }
99
Jonathan Hart365335e2015-12-10 11:09:53 -0800100 @Deactivate
101 public void deactivate() {
Jonathan Hartb14221c2016-03-07 09:55:50 -0800102 interfaceService.removeListener(interfaceListener);
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700103 routeService.removeListener(routeListener);
Jonathan Hart365335e2015-12-10 11:09:53 -0800104 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200105
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700106 private void update(ResolvedRoute route) {
Jonathan Hart9a426f82015-09-03 15:43:13 +0200107 synchronized (this) {
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700108 IpPrefix prefix = route.prefix();
109 MultiPointToSinglePointIntent intent =
110 generateRouteIntent(prefix, route.nextHop(), route.nextHopMac());
Jonathan Hart9a426f82015-09-03 15:43:13 +0200111
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700112 if (intent == null) {
113 log.debug("SDN-IP no interface found for route {}", route);
114 return;
Jonathan Hart9a426f82015-09-03 15:43:13 +0200115 }
116
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700117 routeIntents.put(prefix, intent);
118 intentSynchronizer.submit(intent);
119 }
120 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200121
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700122 private void withdraw(ResolvedRoute route) {
123 synchronized (this) {
124 IpPrefix prefix = route.prefix();
125 MultiPointToSinglePointIntent intent = routeIntents.remove(prefix);
126 if (intent == null) {
127 log.trace("SDN-IP no intent in routeIntents to delete " +
128 "for prefix: {}", prefix);
129 return;
Jonathan Hart9a426f82015-09-03 15:43:13 +0200130 }
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700131 intentSynchronizer.withdraw(intent);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200132 }
133 }
134
135 /**
136 * Generates a route intent for a prefix, the next hop IP address, and
137 * the next hop MAC address.
138 * <p/>
139 * This method will find the egress interface for the intent.
140 * Intent will match dst IP prefix and rewrite dst MAC address at all other
141 * border switches, then forward packets according to dst MAC address.
142 *
143 * @param prefix IP prefix of the route to add
144 * @param nextHopIpAddress IP address of the next hop
145 * @param nextHopMacAddress MAC address of the next hop
146 * @return the generated intent, or null if no intent should be submitted
147 */
148 private MultiPointToSinglePointIntent generateRouteIntent(
149 IpPrefix prefix,
150 IpAddress nextHopIpAddress,
151 MacAddress nextHopMacAddress) {
152
153 // Find the attachment point (egress interface) of the next hop
Pingping Lin92ca4912015-11-19 16:41:54 -0800154 Interface egressInterface =
155 interfaceService.getMatchingInterface(nextHopIpAddress);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200156 if (egressInterface == null) {
157 log.warn("No outgoing interface found for {}",
158 nextHopIpAddress);
159 return null;
160 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200161 ConnectPoint egressPort = egressInterface.connectPoint();
Luca Pretee9511512016-05-13 10:30:19 -0700162
Jonathan Hart9a426f82015-09-03 15:43:13 +0200163 log.debug("Generating intent for prefix {}, next hop mac {}",
164 prefix, nextHopMacAddress);
165
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700166 Set<FilteredConnectPoint> ingressFilteredCPs = Sets.newHashSet();
Luca Pretee9511512016-05-13 10:30:19 -0700167
Luca Pretee9511512016-05-13 10:30:19 -0700168 // TODO this should be only peering interfaces
169 interfaceService.getInterfaces().stream()
Luca Pretee9511512016-05-13 10:30:19 -0700170 .forEach(intf -> {
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700171 // Get ony ingress interfaces with IPs configured
172 if (validIngressIntf(intf, egressInterface)) {
173 TrafficSelector.Builder selector =
174 buildIngressTrafficSelector(intf, prefix);
175 FilteredConnectPoint ingressFilteredCP =
176 new FilteredConnectPoint(intf.connectPoint(), selector.build());
177 ingressFilteredCPs.add(ingressFilteredCP);
178 }
Luca Pretee9511512016-05-13 10:30:19 -0700179 });
180
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700181 // Build treatment: rewrite the destination MAC address
182 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
183 .setEthDst(nextHopMacAddress);
Luca Pretee9511512016-05-13 10:30:19 -0700184
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700185 // Build the egress selector for VLAN Id
186 TrafficSelector.Builder selector =
187 buildTrafficSelector(egressInterface);
188 FilteredConnectPoint egressFilteredCP =
189 new FilteredConnectPoint(egressPort, selector.build());
190
191 // Set priority
192 int priority =
193 prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
194
195 // Set key
196 Key key = Key.of(prefix.toString(), appId);
197
198 return MultiPointToSinglePointIntent.builder()
199 .appId(appId)
200 .key(key)
201 .filteredIngressPoints(ingressFilteredCPs)
202 .filteredEgressPoint(egressFilteredCP)
203 .treatment(treatment.build())
204 .priority(priority)
205 .constraints(CONSTRAINTS)
206 .build();
207 }
208
209 private void addInterface(Interface intf) {
210 synchronized (this) {
211 for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : routeIntents.entrySet()) {
212 // Retrieve the IP prefix and affected intent
213 IpPrefix prefix = entry.getKey();
214 MultiPointToSinglePointIntent intent = entry.getValue();
215
216 // Add new ingress FilteredConnectPoint
217 Set<FilteredConnectPoint> ingressFilteredCPs =
218 Sets.newHashSet(intent.filteredIngressPoints());
219
220 // Create the new traffic selector
221 TrafficSelector.Builder selector =
222 buildIngressTrafficSelector(intf, prefix);
223
224 // Create the Filtered ConnectPoint and add it to the existing set
225 FilteredConnectPoint newIngressFilteredCP =
226 new FilteredConnectPoint(intf.connectPoint(), selector.build());
227 ingressFilteredCPs.add(newIngressFilteredCP);
228
229 // Create new intent
230 MultiPointToSinglePointIntent newIntent =
231 MultiPointToSinglePointIntent.builder(intent)
232 .filteredIngressPoints(ingressFilteredCPs)
233 .build();
234
235 routeIntents.put(entry.getKey(), newIntent);
236 intentSynchronizer.submit(newIntent);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200237 }
238 }
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700239 }
240
241 /*
242 * Handles the case in which an existing interface gets removed.
243 */
244 private void removeInterface(Interface intf) {
245 synchronized (this) {
246 for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : routeIntents.entrySet()) {
247 // Retrieve the IP prefix and intent possibly affected
248 IpPrefix prefix = entry.getKey();
249 MultiPointToSinglePointIntent intent = entry.getValue();
250
251 // The interface removed might be an ingress interface, so the
252 // selector needs to match on the interface tagging params and
253 // on the prefix
254 TrafficSelector.Builder ingressSelector =
255 buildIngressTrafficSelector(intf, prefix);
256 FilteredConnectPoint removedIngressFilteredCP =
257 new FilteredConnectPoint(intf.connectPoint(),
258 ingressSelector.build());
259
260 // The interface removed might be an egress interface, so the
261 // selector needs to match only on the interface tagging params
262 TrafficSelector.Builder selector = buildTrafficSelector(intf);
263 FilteredConnectPoint removedEgressFilteredCP =
264 new FilteredConnectPoint(intf.connectPoint(), selector.build());
265
266 if (intent.filteredEgressPoint().equals(removedEgressFilteredCP)) {
267 // The interface is an egress interface for the intent.
268 // This intent just lost its head. Remove it and let higher
269 // layer routing reroute
270 intentSynchronizer.withdraw(routeIntents.remove(entry.getKey()));
271 } else {
272 if (intent.filteredIngressPoints().contains(removedIngressFilteredCP)) {
273 // The FilteredConnectPoint is an ingress
274 // FilteredConnectPoint for the intent
275 Set<FilteredConnectPoint> ingressFilteredCPs =
276 Sets.newHashSet(intent.filteredIngressPoints());
277
278 // Remove FilteredConnectPoint from the existing set
279 ingressFilteredCPs.remove(removedIngressFilteredCP);
280
281 if (!ingressFilteredCPs.isEmpty()) {
282 // There are still ingress points. Create a new
283 // intent and resubmit
284 MultiPointToSinglePointIntent newIntent =
285 MultiPointToSinglePointIntent.builder(intent)
286 .filteredIngressPoints(ingressFilteredCPs)
287 .build();
288
289 routeIntents.put(entry.getKey(), newIntent);
290 intentSynchronizer.submit(newIntent);
291 } else {
292 // No more ingress FilteredConnectPoint. Withdraw
293 //the intent
294 intentSynchronizer.withdraw(routeIntents.remove(entry.getKey()));
295 }
296 }
297 }
298 }
299 }
300 }
301
302 /*
303 * Builds an ingress traffic selector builder given an ingress interface and
304 * the IP prefix to be reached.
305 */
306 private TrafficSelector.Builder buildIngressTrafficSelector(Interface intf, IpPrefix prefix) {
307 TrafficSelector.Builder selector = buildTrafficSelector(intf);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200308
309 // Match the destination IP prefix at the first hop
Jonathan Hart9a426f82015-09-03 15:43:13 +0200310 if (prefix.isIp4()) {
311 selector.matchEthType(Ethernet.TYPE_IPV4);
Pingping Lin92ca4912015-11-19 16:41:54 -0800312 // if it is default route, then we do not need match destination
313 // IP address
314 if (prefix.prefixLength() != 0) {
315 selector.matchIPDst(prefix);
316 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200317 } else {
318 selector.matchEthType(Ethernet.TYPE_IPV6);
Pingping Lin92ca4912015-11-19 16:41:54 -0800319 // if it is default route, then we do not need match destination
320 // IP address
321 if (prefix.prefixLength() != 0) {
322 selector.matchIPv6Dst(prefix);
323 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200324 }
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700325 return selector;
Jonathan Hart9a426f82015-09-03 15:43:13 +0200326 }
327
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700328 /*
329 * Builds a traffic selector builder based on interface tagging settings.
330 */
331 private TrafficSelector.Builder buildTrafficSelector(Interface intf) {
332 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Jonathan Hartb14221c2016-03-07 09:55:50 -0800333
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700334 // TODO: Consider other tag types
335 // Match the VlanId if specified in the network interface configuration
336 VlanId vlanId = intf.vlan();
337 if (!vlanId.equals(VlanId.NONE)) {
338 selector.matchVlanId(vlanId);
Jonathan Hartb14221c2016-03-07 09:55:50 -0800339 }
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700340 return selector;
Jonathan Hartb14221c2016-03-07 09:55:50 -0800341 }
342
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700343 // Check if the interface is an ingress interface with IPs configured
344 private boolean validIngressIntf(Interface intf, Interface egressInterface) {
345 if (!intf.equals(egressInterface) &&
346 !intf.ipAddressesList().isEmpty() &&
347 // TODO: An egress point might have two routers connected on different interfaces
348 !intf.connectPoint().equals(egressInterface.connectPoint())) {
349 return true;
Jonathan Hartb14221c2016-03-07 09:55:50 -0800350 }
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700351 return false;
Jonathan Hartb14221c2016-03-07 09:55:50 -0800352 }
353
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700354 private class InternalRouteListener implements RouteListener {
Jonathan Hart365335e2015-12-10 11:09:53 -0800355 @Override
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700356 public void event(RouteEvent event) {
357 switch (event.type()) {
358 case ROUTE_ADDED:
359 case ROUTE_UPDATED:
360 update(event.subject());
361 break;
362 case ROUTE_REMOVED:
363 withdraw(event.subject());
364 break;
365 default:
366 break;
367 }
Jonathan Hart365335e2015-12-10 11:09:53 -0800368 }
369 }
370
Jonathan Hartb14221c2016-03-07 09:55:50 -0800371 private class InternalInterfaceListener implements InterfaceListener {
372
373 @Override
374 public void event(InterfaceEvent event) {
375 switch (event.type()) {
376 case INTERFACE_ADDED:
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700377 addInterface(event.subject());
Jonathan Hartb14221c2016-03-07 09:55:50 -0800378 break;
379 case INTERFACE_UPDATED:
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700380 removeInterface(event.prevSubject());
381 addInterface(event.subject());
Jonathan Hartb14221c2016-03-07 09:55:50 -0800382 break;
383 case INTERFACE_REMOVED:
384 removeInterface(event.subject());
385 break;
386 default:
387 break;
388 }
389 }
390 }
391
Jonathan Hart9a426f82015-09-03 15:43:13 +0200392}