blob: aa646ba4013e5697d679931bf9ca801c5ae96fd0 [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
Sho SHIMIZU8ebb04a2016-10-06 15:58:29 -0700169 interfaceService.getInterfaces().forEach(intf -> {
170 // Get ony ingress interfaces with IPs configured
171 if (validIngressIntf(intf, egressInterface)) {
172 TrafficSelector.Builder selector =
173 buildIngressTrafficSelector(intf, prefix);
174 FilteredConnectPoint ingressFilteredCP =
175 new FilteredConnectPoint(intf.connectPoint(), selector.build());
176 ingressFilteredCPs.add(ingressFilteredCP);
177 }
178 });
Luca Pretee9511512016-05-13 10:30:19 -0700179
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700180 // Build treatment: rewrite the destination MAC address
181 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
182 .setEthDst(nextHopMacAddress);
Luca Pretee9511512016-05-13 10:30:19 -0700183
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700184 // Build the egress selector for VLAN Id
185 TrafficSelector.Builder selector =
186 buildTrafficSelector(egressInterface);
187 FilteredConnectPoint egressFilteredCP =
188 new FilteredConnectPoint(egressPort, selector.build());
189
190 // Set priority
191 int priority =
192 prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
193
194 // Set key
195 Key key = Key.of(prefix.toString(), appId);
196
197 return MultiPointToSinglePointIntent.builder()
198 .appId(appId)
199 .key(key)
200 .filteredIngressPoints(ingressFilteredCPs)
201 .filteredEgressPoint(egressFilteredCP)
202 .treatment(treatment.build())
203 .priority(priority)
204 .constraints(CONSTRAINTS)
205 .build();
206 }
207
208 private void addInterface(Interface intf) {
209 synchronized (this) {
210 for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : routeIntents.entrySet()) {
211 // Retrieve the IP prefix and affected intent
212 IpPrefix prefix = entry.getKey();
213 MultiPointToSinglePointIntent intent = entry.getValue();
214
215 // Add new ingress FilteredConnectPoint
216 Set<FilteredConnectPoint> ingressFilteredCPs =
217 Sets.newHashSet(intent.filteredIngressPoints());
218
219 // Create the new traffic selector
220 TrafficSelector.Builder selector =
221 buildIngressTrafficSelector(intf, prefix);
222
223 // Create the Filtered ConnectPoint and add it to the existing set
224 FilteredConnectPoint newIngressFilteredCP =
225 new FilteredConnectPoint(intf.connectPoint(), selector.build());
226 ingressFilteredCPs.add(newIngressFilteredCP);
227
228 // Create new intent
229 MultiPointToSinglePointIntent newIntent =
230 MultiPointToSinglePointIntent.builder(intent)
231 .filteredIngressPoints(ingressFilteredCPs)
232 .build();
233
234 routeIntents.put(entry.getKey(), newIntent);
235 intentSynchronizer.submit(newIntent);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200236 }
237 }
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700238 }
239
240 /*
241 * Handles the case in which an existing interface gets removed.
242 */
243 private void removeInterface(Interface intf) {
244 synchronized (this) {
245 for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : routeIntents.entrySet()) {
246 // Retrieve the IP prefix and intent possibly affected
247 IpPrefix prefix = entry.getKey();
248 MultiPointToSinglePointIntent intent = entry.getValue();
249
250 // The interface removed might be an ingress interface, so the
251 // selector needs to match on the interface tagging params and
252 // on the prefix
253 TrafficSelector.Builder ingressSelector =
254 buildIngressTrafficSelector(intf, prefix);
255 FilteredConnectPoint removedIngressFilteredCP =
256 new FilteredConnectPoint(intf.connectPoint(),
257 ingressSelector.build());
258
259 // The interface removed might be an egress interface, so the
260 // selector needs to match only on the interface tagging params
261 TrafficSelector.Builder selector = buildTrafficSelector(intf);
262 FilteredConnectPoint removedEgressFilteredCP =
263 new FilteredConnectPoint(intf.connectPoint(), selector.build());
264
265 if (intent.filteredEgressPoint().equals(removedEgressFilteredCP)) {
266 // The interface is an egress interface for the intent.
267 // This intent just lost its head. Remove it and let higher
268 // layer routing reroute
269 intentSynchronizer.withdraw(routeIntents.remove(entry.getKey()));
270 } else {
271 if (intent.filteredIngressPoints().contains(removedIngressFilteredCP)) {
272 // The FilteredConnectPoint is an ingress
273 // FilteredConnectPoint for the intent
274 Set<FilteredConnectPoint> ingressFilteredCPs =
275 Sets.newHashSet(intent.filteredIngressPoints());
276
277 // Remove FilteredConnectPoint from the existing set
278 ingressFilteredCPs.remove(removedIngressFilteredCP);
279
280 if (!ingressFilteredCPs.isEmpty()) {
281 // There are still ingress points. Create a new
282 // intent and resubmit
283 MultiPointToSinglePointIntent newIntent =
284 MultiPointToSinglePointIntent.builder(intent)
285 .filteredIngressPoints(ingressFilteredCPs)
286 .build();
287
288 routeIntents.put(entry.getKey(), newIntent);
289 intentSynchronizer.submit(newIntent);
290 } else {
291 // No more ingress FilteredConnectPoint. Withdraw
292 //the intent
293 intentSynchronizer.withdraw(routeIntents.remove(entry.getKey()));
294 }
295 }
296 }
297 }
298 }
299 }
300
301 /*
302 * Builds an ingress traffic selector builder given an ingress interface and
303 * the IP prefix to be reached.
304 */
305 private TrafficSelector.Builder buildIngressTrafficSelector(Interface intf, IpPrefix prefix) {
306 TrafficSelector.Builder selector = buildTrafficSelector(intf);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200307
308 // Match the destination IP prefix at the first hop
Jonathan Hart9a426f82015-09-03 15:43:13 +0200309 if (prefix.isIp4()) {
310 selector.matchEthType(Ethernet.TYPE_IPV4);
Pingping Lin92ca4912015-11-19 16:41:54 -0800311 // if it is default route, then we do not need match destination
312 // IP address
313 if (prefix.prefixLength() != 0) {
314 selector.matchIPDst(prefix);
315 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200316 } else {
317 selector.matchEthType(Ethernet.TYPE_IPV6);
Pingping Lin92ca4912015-11-19 16:41:54 -0800318 // if it is default route, then we do not need match destination
319 // IP address
320 if (prefix.prefixLength() != 0) {
321 selector.matchIPv6Dst(prefix);
322 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200323 }
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700324 return selector;
Jonathan Hart9a426f82015-09-03 15:43:13 +0200325 }
326
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700327 /*
328 * Builds a traffic selector builder based on interface tagging settings.
329 */
330 private TrafficSelector.Builder buildTrafficSelector(Interface intf) {
331 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Jonathan Hartb14221c2016-03-07 09:55:50 -0800332
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700333 // TODO: Consider other tag types
334 // Match the VlanId if specified in the network interface configuration
335 VlanId vlanId = intf.vlan();
336 if (!vlanId.equals(VlanId.NONE)) {
337 selector.matchVlanId(vlanId);
Jonathan Hartb14221c2016-03-07 09:55:50 -0800338 }
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700339 return selector;
Jonathan Hartb14221c2016-03-07 09:55:50 -0800340 }
341
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700342 // Check if the interface is an ingress interface with IPs configured
343 private boolean validIngressIntf(Interface intf, Interface egressInterface) {
344 if (!intf.equals(egressInterface) &&
345 !intf.ipAddressesList().isEmpty() &&
346 // TODO: An egress point might have two routers connected on different interfaces
347 !intf.connectPoint().equals(egressInterface.connectPoint())) {
348 return true;
Jonathan Hartb14221c2016-03-07 09:55:50 -0800349 }
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700350 return false;
Jonathan Hartb14221c2016-03-07 09:55:50 -0800351 }
352
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700353 private class InternalRouteListener implements RouteListener {
Jonathan Hart365335e2015-12-10 11:09:53 -0800354 @Override
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700355 public void event(RouteEvent event) {
356 switch (event.type()) {
357 case ROUTE_ADDED:
358 case ROUTE_UPDATED:
359 update(event.subject());
360 break;
361 case ROUTE_REMOVED:
362 withdraw(event.subject());
363 break;
364 default:
365 break;
366 }
Jonathan Hart365335e2015-12-10 11:09:53 -0800367 }
368 }
369
Jonathan Hartb14221c2016-03-07 09:55:50 -0800370 private class InternalInterfaceListener implements InterfaceListener {
371
372 @Override
373 public void event(InterfaceEvent event) {
374 switch (event.type()) {
375 case INTERFACE_ADDED:
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700376 addInterface(event.subject());
Jonathan Hartb14221c2016-03-07 09:55:50 -0800377 break;
378 case INTERFACE_UPDATED:
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700379 removeInterface(event.prevSubject());
380 addInterface(event.subject());
Jonathan Hartb14221c2016-03-07 09:55:50 -0800381 break;
382 case INTERFACE_REMOVED:
383 removeInterface(event.subject());
384 break;
385 default:
386 break;
387 }
388 }
389 }
390
Jonathan Hart9a426f82015-09-03 15:43:13 +0200391}