Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +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.sdnip; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [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; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 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; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 32 | import org.onosproject.core.CoreService; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 33 | import org.onosproject.incubator.net.intf.Interface; |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 34 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 35 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 36 | import org.onosproject.incubator.net.intf.InterfaceService; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [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; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +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; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 50 | import org.onosproject.routing.IntentSynchronizationService; |
| 51 | import org.slf4j.Logger; |
| 52 | import org.slf4j.LoggerFactory; |
| 53 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +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 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 59 | /** |
| 60 | * FIB component of SDN-IP. |
| 61 | */ |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 62 | @Component(immediate = true, enabled = false) |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 63 | public class SdnIpFib { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 64 | private Logger log = LoggerFactory.getLogger(getClass()); |
| 65 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 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) |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 76 | protected RouteService routeService; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 77 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 78 | private final InternalRouteListener routeListener = new InternalRouteListener(); |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 79 | private final InternalInterfaceListener interfaceListener = new InternalInterfaceListener(); |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 80 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 81 | 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 Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 86 | private final Map<IpPrefix, MultiPointToSinglePointIntent> routeIntents |
| 87 | = new ConcurrentHashMap<>(); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 88 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 89 | private ApplicationId appId; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 90 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 91 | @Activate |
| 92 | public void activate() { |
| 93 | appId = coreService.getAppId(SdnIp.SDN_IP_APP); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 94 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 95 | interfaceService.addListener(interfaceListener); |
| 96 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 97 | routeService.addListener(routeListener); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 100 | @Deactivate |
| 101 | public void deactivate() { |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 102 | interfaceService.removeListener(interfaceListener); |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 103 | routeService.removeListener(routeListener); |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 104 | } |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 105 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 106 | private void update(ResolvedRoute route) { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 107 | synchronized (this) { |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 108 | IpPrefix prefix = route.prefix(); |
| 109 | MultiPointToSinglePointIntent intent = |
| 110 | generateRouteIntent(prefix, route.nextHop(), route.nextHopMac()); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 111 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 112 | if (intent == null) { |
| 113 | log.debug("SDN-IP no interface found for route {}", route); |
| 114 | return; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 117 | routeIntents.put(prefix, intent); |
| 118 | intentSynchronizer.submit(intent); |
| 119 | } |
| 120 | } |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 121 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 122 | 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 Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 130 | } |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 131 | intentSynchronizer.withdraw(intent); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 132 | } |
| 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 Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 154 | Interface egressInterface = |
| 155 | interfaceService.getMatchingInterface(nextHopIpAddress); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 156 | if (egressInterface == null) { |
| 157 | log.warn("No outgoing interface found for {}", |
| 158 | nextHopIpAddress); |
| 159 | return null; |
| 160 | } |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 161 | ConnectPoint egressPort = egressInterface.connectPoint(); |
Luca Prete | e951151 | 2016-05-13 10:30:19 -0700 | [diff] [blame] | 162 | |
| 163 | Set<Interface> ingressInterfaces = new HashSet<>(); |
| 164 | Set<ConnectPoint> ingressPorts = new HashSet<>(); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 165 | log.debug("Generating intent for prefix {}, next hop mac {}", |
| 166 | prefix, nextHopMacAddress); |
| 167 | |
Luca Prete | e951151 | 2016-05-13 10:30:19 -0700 | [diff] [blame] | 168 | 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 Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | // Match the destination IP prefix at the first hop |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 193 | if (prefix.isIp4()) { |
| 194 | selector.matchEthType(Ethernet.TYPE_IPV4); |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 195 | // 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 Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 200 | } else { |
| 201 | selector.matchEthType(Ethernet.TYPE_IPV6); |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 202 | // 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 Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Rewrite the destination MAC address |
| 210 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
| 211 | .setEthDst(nextHopMacAddress); |
Luca Prete | e951151 | 2016-05-13 10:30:19 -0700 | [diff] [blame] | 212 | |
| 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 Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Luca Prete | e951151 | 2016-05-13 10:30:19 -0700 | [diff] [blame] | 223 | // Set priority |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 224 | int priority = |
| 225 | prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
Luca Prete | e951151 | 2016-05-13 10:30:19 -0700 | [diff] [blame] | 226 | |
| 227 | // Set key |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 228 | Key key = Key.of(prefix.toString(), appId); |
Luca Prete | e951151 | 2016-05-13 10:30:19 -0700 | [diff] [blame] | 229 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 230 | 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 Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 242 | 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 Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 287 | private class InternalRouteListener implements RouteListener { |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 288 | @Override |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 289 | 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 Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 304 | 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 Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 323 | } |