blob: f326d70122d6be5912c4f270465e1aeb432a603a [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;
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;
Jonathan Hart9a426f82015-09-03 15:43:13 +020050import org.onosproject.routing.IntentSynchronizationService;
51import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
Jonathan Hart9a426f82015-09-03 15:43:13 +020054import java.util.HashSet;
55import 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
163 Set<Interface> ingressInterfaces = new HashSet<>();
164 Set<ConnectPoint> ingressPorts = new HashSet<>();
Jonathan Hart9a426f82015-09-03 15:43:13 +0200165 log.debug("Generating intent for prefix {}, next hop mac {}",
166 prefix, nextHopMacAddress);
167
Luca Pretee9511512016-05-13 10:30:19 -0700168 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
169
170 // Get ingress interfaces and ports
171 // TODO this should be only peering interfaces
172 interfaceService.getInterfaces().stream()
173 .filter(intf -> !intf.equals(egressInterface))
174 .forEach(intf -> {
175 ingressInterfaces.add(intf);
176 ConnectPoint ingressPort = intf.connectPoint();
177 ingressPorts.add(ingressPort);
178 });
179
180 // By default the ingress traffic is not tagged
181 VlanId ingressVlanId = VlanId.NONE;
182
183 // Match VLAN Id ANY if the source VLAN Id is not null
184 // TODO need to be able to set a different VLAN Id per ingress interface
185 for (Interface intf : ingressInterfaces) {
186 if (!intf.vlan().equals(VlanId.NONE)) {
187 selector.matchVlanId(VlanId.ANY);
188 ingressVlanId = intf.vlan();
Jonathan Hart9a426f82015-09-03 15:43:13 +0200189 }
190 }
191
192 // Match the destination IP prefix at the first hop
Jonathan Hart9a426f82015-09-03 15:43:13 +0200193 if (prefix.isIp4()) {
194 selector.matchEthType(Ethernet.TYPE_IPV4);
Pingping Lin92ca4912015-11-19 16:41:54 -0800195 // if it is default route, then we do not need match destination
196 // IP address
197 if (prefix.prefixLength() != 0) {
198 selector.matchIPDst(prefix);
199 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200200 } else {
201 selector.matchEthType(Ethernet.TYPE_IPV6);
Pingping Lin92ca4912015-11-19 16:41:54 -0800202 // if it is default route, then we do not need match destination
203 // IP address
204 if (prefix.prefixLength() != 0) {
205 selector.matchIPv6Dst(prefix);
206 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200207 }
208
209 // Rewrite the destination MAC address
210 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
211 .setEthDst(nextHopMacAddress);
Luca Pretee9511512016-05-13 10:30:19 -0700212
213 // Set egress VLAN Id
214 // TODO need to make the comparison with different ingress VLAN Ids
215 if (!ingressVlanId.equals(egressInterface.vlan())) {
216 if (egressInterface.vlan().equals(VlanId.NONE)) {
217 treatment.popVlan();
218 } else {
219 treatment.setVlanId(egressInterface.vlan());
220 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200221 }
222
Luca Pretee9511512016-05-13 10:30:19 -0700223 // Set priority
Jonathan Hart9a426f82015-09-03 15:43:13 +0200224 int priority =
225 prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
Luca Pretee9511512016-05-13 10:30:19 -0700226
227 // Set key
Jonathan Hart9a426f82015-09-03 15:43:13 +0200228 Key key = Key.of(prefix.toString(), appId);
Luca Pretee9511512016-05-13 10:30:19 -0700229
Jonathan Hart9a426f82015-09-03 15:43:13 +0200230 return MultiPointToSinglePointIntent.builder()
231 .appId(appId)
232 .key(key)
233 .selector(selector.build())
234 .treatment(treatment.build())
235 .ingressPoints(ingressPorts)
236 .egressPoint(egressPort)
237 .priority(priority)
238 .constraints(CONSTRAINTS)
239 .build();
240 }
241
Jonathan Hartb14221c2016-03-07 09:55:50 -0800242 private void updateInterface(Interface intf) {
243 synchronized (this) {
244 for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : routeIntents.entrySet()) {
245 MultiPointToSinglePointIntent intent = entry.getValue();
246 Set<ConnectPoint> ingress = Sets.newHashSet(intent.ingressPoints());
247 ingress.add(intf.connectPoint());
248
249 MultiPointToSinglePointIntent newIntent =
250 MultiPointToSinglePointIntent.builder(intent)
251 .ingressPoints(ingress)
252 .build();
253
254 routeIntents.put(entry.getKey(), newIntent);
255 intentSynchronizer.submit(newIntent);
256 }
257 }
258 }
259
260 private void removeInterface(Interface intf) {
261 synchronized (this) {
262 for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : routeIntents.entrySet()) {
263 MultiPointToSinglePointIntent intent = entry.getValue();
264 if (intent.egressPoint().equals(intf.connectPoint())) {
265 // This intent just lost its head. Remove it and let
266 // higher layer routing reroute.
267 intentSynchronizer.withdraw(routeIntents.remove(entry.getKey()));
268 } else {
269 if (intent.ingressPoints().contains(intf.connectPoint())) {
270
271 Set<ConnectPoint> ingress = Sets.newHashSet(intent.ingressPoints());
272 ingress.remove(intf.connectPoint());
273
274 MultiPointToSinglePointIntent newIntent =
275 MultiPointToSinglePointIntent.builder(intent)
276 .ingressPoints(ingress)
277 .build();
278
279 routeIntents.put(entry.getKey(), newIntent);
280 intentSynchronizer.submit(newIntent);
281 }
282 }
283 }
284 }
285 }
286
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700287 private class InternalRouteListener implements RouteListener {
Jonathan Hart365335e2015-12-10 11:09:53 -0800288 @Override
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700289 public void event(RouteEvent event) {
290 switch (event.type()) {
291 case ROUTE_ADDED:
292 case ROUTE_UPDATED:
293 update(event.subject());
294 break;
295 case ROUTE_REMOVED:
296 withdraw(event.subject());
297 break;
298 default:
299 break;
300 }
Jonathan Hart365335e2015-12-10 11:09:53 -0800301 }
302 }
303
Jonathan Hartb14221c2016-03-07 09:55:50 -0800304 private class InternalInterfaceListener implements InterfaceListener {
305
306 @Override
307 public void event(InterfaceEvent event) {
308 switch (event.type()) {
309 case INTERFACE_ADDED:
310 updateInterface(event.subject());
311 break;
312 case INTERFACE_UPDATED:
313 break;
314 case INTERFACE_REMOVED:
315 removeInterface(event.subject());
316 break;
317 default:
318 break;
319 }
320 }
321 }
322
Jonathan Hart9a426f82015-09-03 15:43:13 +0200323}