Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 1 | /* |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.sdxl3; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onlab.packet.Ethernet; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.IpPrefix; |
| 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.packet.VlanId; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.incubator.net.intf.Interface; |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 34 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 35 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 36 | import org.onosproject.incubator.net.intf.InterfaceService; |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 37 | import org.onosproject.incubator.net.routing.ResolvedRoute; |
| 38 | import org.onosproject.incubator.net.routing.RouteEvent; |
| 39 | import org.onosproject.incubator.net.routing.RouteListener; |
| 40 | import org.onosproject.incubator.net.routing.RouteService; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 41 | import org.onosproject.net.ConnectPoint; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.TrafficSelector; |
| 45 | import org.onosproject.net.flow.TrafficTreatment; |
| 46 | import org.onosproject.net.intent.Constraint; |
| 47 | import org.onosproject.net.intent.Key; |
| 48 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 49 | import org.onosproject.net.intent.constraint.PartialFailureConstraint; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 50 | import org.onosproject.routing.IntentSynchronizationService; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | import org.slf4j.LoggerFactory; |
| 53 | |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 54 | import java.util.HashSet; |
| 55 | import java.util.Map; |
| 56 | import java.util.Set; |
| 57 | import java.util.concurrent.ConcurrentHashMap; |
| 58 | |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 59 | /** |
| 60 | * FIB component of SDX-L3. |
| 61 | */ |
| 62 | @Component(immediate = true, enabled = false) |
| 63 | public 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 Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 76 | protected RouteService routeService; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 79 | protected SdxL3PeerService peerService; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 80 | |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 81 | private final InternalRouteListener routeListener = new InternalRouteListener(); |
| 82 | private final InternalInterfaceListener interfaceListener = new InternalInterfaceListener(); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 83 | |
| 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 Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 98 | interfaceService.addListener(interfaceListener); |
| 99 | |
| 100 | routeService.addListener(routeListener); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Deactivate |
| 104 | public void deactivate() { |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 105 | interfaceService.removeListener(interfaceListener); |
| 106 | routeService.removeListener(routeListener); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 109 | private void update(ResolvedRoute route) { |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 110 | synchronized (this) { |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 111 | IpPrefix prefix = route.prefix(); |
| 112 | MultiPointToSinglePointIntent intent = |
| 113 | generateRouteIntent(prefix, route.nextHop(), route.nextHopMac()); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 114 | |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 115 | if (intent == null) { |
| 116 | log.debug("SDX-L3 no interface found for route {}", route); |
| 117 | return; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 120 | routeIntents.put(prefix, intent); |
| 121 | intentSynchronizer.submit(intent); |
| 122 | } |
| 123 | } |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 124 | |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 125 | 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 Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 133 | } |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 134 | intentSynchronizer.withdraw(intent); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 135 | } |
| 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 Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 157 | Interface egressInterface = peerService.getInterfaceForPeer(nextHopIpAddress); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 158 | |
| 159 | if (egressInterface == null) { |
| 160 | log.warn("No outgoing interface found for {}", |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 161 | nextHopIpAddress); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 162 | return null; |
| 163 | } |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 164 | ConnectPoint egressPort = egressInterface.connectPoint(); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 165 | |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 166 | 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 Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | // Match the destination IP prefix at the first hop |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 196 | 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 Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Rewrite the destination MAC address |
| 213 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
| 214 | .setEthDst(nextHopMacAddress); |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 215 | |
| 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 Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 226 | // Set priority |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 227 | int priority = |
| 228 | prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 229 | |
| 230 | // Set key |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 231 | Key key = Key.of(prefix.toString(), appId); |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 232 | |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 233 | 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 Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 245 | 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 Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 291 | @Override |
Andreas Papazois | eb51893 | 2016-06-21 13:46:20 +0300 | [diff] [blame] | 292 | 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 Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | } |