Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 1 | /* |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Laboratory |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [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 | |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame^] | 17 | package org.onosproject.routing.fibinstaller; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ConcurrentHashMultiset; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Multiset; |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Modified; |
| 27 | import org.apache.felix.scr.annotations.Property; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 30 | import org.onlab.packet.Ethernet; |
| 31 | import org.onlab.packet.IpAddress; |
| 32 | import org.onlab.packet.IpPrefix; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 33 | import org.onlab.packet.MacAddress; |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 34 | import org.onlab.packet.VlanId; |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 35 | import org.onlab.util.Tools; |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 36 | import org.onosproject.app.ApplicationService; |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 37 | import org.onosproject.cfg.ComponentConfigService; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 38 | import org.onosproject.core.ApplicationId; |
| 39 | import org.onosproject.core.CoreService; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 40 | import org.onosproject.incubator.net.config.basics.McastConfig; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 41 | import org.onosproject.incubator.net.intf.Interface; |
| 42 | import org.onosproject.incubator.net.intf.InterfaceService; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 43 | import org.onosproject.incubator.net.routing.ResolvedRoute; |
| 44 | import org.onosproject.incubator.net.routing.RouteEvent; |
| 45 | import org.onosproject.incubator.net.routing.RouteListener; |
| 46 | import org.onosproject.incubator.net.routing.RouteService; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 47 | import org.onosproject.net.ConnectPoint; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 48 | import org.onosproject.net.DeviceId; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 49 | import org.onosproject.net.config.ConfigFactory; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 50 | import org.onosproject.net.config.NetworkConfigEvent; |
| 51 | import org.onosproject.net.config.NetworkConfigListener; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 52 | import org.onosproject.net.config.NetworkConfigRegistry; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 53 | import org.onosproject.net.config.NetworkConfigService; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 54 | import org.onosproject.net.config.basics.SubjectFactories; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 55 | import org.onosproject.net.device.DeviceService; |
| 56 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 57 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 58 | import org.onosproject.net.flow.TrafficSelector; |
| 59 | import org.onosproject.net.flow.TrafficTreatment; |
| 60 | import org.onosproject.net.flow.criteria.Criteria; |
| 61 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 62 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 63 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 64 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 65 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 66 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 67 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 68 | import org.onosproject.net.flowobjective.NextObjective; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 69 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame^] | 70 | import org.onosproject.routing.AsyncDeviceFetcher; |
| 71 | import org.onosproject.routing.NextHop; |
| 72 | import org.onosproject.routing.NextHopGroupKey; |
| 73 | import org.onosproject.routing.RouterInterfaceManager; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 74 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 75 | import org.onosproject.routing.config.RouterConfig; |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 76 | import org.osgi.service.component.ComponentContext; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 77 | import org.slf4j.Logger; |
| 78 | import org.slf4j.LoggerFactory; |
| 79 | |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 80 | import java.util.Dictionary; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 81 | import java.util.Map; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 82 | import java.util.Set; |
| 83 | |
| 84 | /** |
| 85 | * Programs routes to a single OpenFlow switch. |
| 86 | */ |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame^] | 87 | @Component(immediate = true) |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 88 | public class SingleSwitchFibInstaller { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 89 | |
| 90 | private final Logger log = LoggerFactory.getLogger(getClass()); |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 91 | private static final String APP_NAME = "org.onosproject.vrouter"; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 92 | |
| 93 | private static final int PRIORITY_OFFSET = 100; |
| 94 | private static final int PRIORITY_MULTIPLIER = 5; |
| 95 | |
Charles Chan | 03a73e0 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 96 | // FIXME: This should be eliminated when we have an API in SR that |
| 97 | // programs the fabric switches for VR |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 98 | public static final short ASSIGNED_VLAN = 4094; |
| 99 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected CoreService coreService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 104 | protected RouteService routeService; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected InterfaceService interfaceService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected NetworkConfigService networkConfigService; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 113 | protected NetworkConfigRegistry networkConfigRegistry; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 116 | protected ComponentConfigService componentConfigService; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 119 | protected FlowObjectiveService flowObjectiveService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected DeviceService deviceService; |
| 123 | |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected ApplicationService applicationService; |
| 126 | |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 127 | @Property(name = "routeToNextHop", boolValue = false, |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 128 | label = "Install a /32 or /128 route to each next hop") |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 129 | private boolean routeToNextHop = false; |
| 130 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 131 | // Device id of data-plane switch - should be learned from config |
| 132 | private DeviceId deviceId; |
| 133 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 134 | private ConnectPoint controlPlaneConnectPoint; |
| 135 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 136 | private RouterInterfaceManager interfaceManager; |
| 137 | private AsyncDeviceFetcher asyncDeviceFetcher; |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 138 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 139 | private ApplicationId coreAppId; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 140 | private ApplicationId routerAppId; |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 141 | private ApplicationId vrouterAppId; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 142 | |
| 143 | // Reference count for how many times a next hop is used by a route |
| 144 | private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create(); |
| 145 | |
| 146 | // Mapping from prefix to its current next hop |
| 147 | private final Map<IpPrefix, IpAddress> prefixToNextHop = Maps.newHashMap(); |
| 148 | |
| 149 | // Mapping from next hop IP to next hop object containing group info |
| 150 | private final Map<IpAddress, Integer> nextHops = Maps.newHashMap(); |
| 151 | |
Jonathan Hart | 25bd668 | 2016-06-03 10:45:47 -0700 | [diff] [blame] | 152 | private final InternalRouteListener routeListener = new InternalRouteListener(); |
| 153 | private final InternalNetworkConfigListener configListener = new InternalNetworkConfigListener(); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 154 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 155 | private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory = |
| 156 | new ConfigFactory<ApplicationId, McastConfig>(SubjectFactories.APP_SUBJECT_FACTORY, |
| 157 | McastConfig.class, "multicast") { |
| 158 | @Override |
| 159 | public McastConfig createConfig() { |
| 160 | return new McastConfig(); |
| 161 | } |
| 162 | }; |
| 163 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 164 | @Activate |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 165 | protected void activate(ComponentContext context) { |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 166 | componentConfigService.registerProperties(getClass()); |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 167 | modified(context); |
| 168 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 169 | coreAppId = coreService.registerApplication(CoreService.CORE_APP_NAME); |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 170 | routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID); |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 171 | vrouterAppId = coreService.registerApplication(APP_NAME); |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 172 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 173 | networkConfigRegistry.registerConfigFactory(mcastConfigFactory); |
| 174 | |
Jonathan Hart | 25bd668 | 2016-06-03 10:45:47 -0700 | [diff] [blame] | 175 | networkConfigService.addListener(configListener); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 176 | |
| 177 | asyncDeviceFetcher = AsyncDeviceFetcher.create(deviceService); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 178 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 179 | processRouterConfig(); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 180 | |
Charles Chan | 00d8b5f | 2016-12-04 17:17:39 -0800 | [diff] [blame] | 181 | // FIXME: There can be an issue when this component is deactivated before vRouter. |
| 182 | // This will be addressed in CORD-710. |
Jonathan Hart | f8035d3 | 2016-06-16 16:23:26 -0700 | [diff] [blame] | 183 | applicationService.registerDeactivateHook(vrouterAppId, () -> cleanUp()); |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 184 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 185 | log.info("Started"); |
| 186 | } |
| 187 | |
| 188 | @Deactivate |
| 189 | protected void deactivate() { |
Charles Chan | 00d8b5f | 2016-12-04 17:17:39 -0800 | [diff] [blame] | 190 | // FIXME: This will also remove flows when an instance goes down. |
| 191 | // This is a temporary solution and should be addressed in CORD-710. |
| 192 | cleanUp(); |
| 193 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 194 | asyncDeviceFetcher.shutdown(); |
Jonathan Hart | 25bd668 | 2016-06-03 10:45:47 -0700 | [diff] [blame] | 195 | networkConfigService.removeListener(configListener); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 196 | |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 197 | componentConfigService.unregisterProperties(getClass(), false); |
| 198 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 199 | log.info("Stopped"); |
| 200 | } |
| 201 | |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 202 | @Modified |
| 203 | protected void modified(ComponentContext context) { |
| 204 | Dictionary<?, ?> properties = context.getProperties(); |
| 205 | if (properties == null) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | String strRouteToNextHop = Tools.get(properties, "routeToNextHop"); |
| 210 | routeToNextHop = Boolean.parseBoolean(strRouteToNextHop); |
| 211 | |
| 212 | log.info("routeToNextHop set to {}", routeToNextHop); |
| 213 | } |
| 214 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 215 | private void processRouterConfig() { |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 216 | RouterConfig routerConfig = |
| 217 | networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 218 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 219 | if (routerConfig == null) { |
| 220 | log.info("Router config not available"); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 224 | Set<String> interfaces = Sets.newHashSet(routerConfig.getInterfaces()); |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 225 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 226 | if (deviceId == null) { |
| 227 | controlPlaneConnectPoint = routerConfig.getControlPlaneConnectPoint(); |
| 228 | log.info("Control Plane Connect Point: {}", controlPlaneConnectPoint); |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 229 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 230 | deviceId = routerConfig.getControlPlaneConnectPoint().deviceId(); |
| 231 | log.info("Router device ID is {}", deviceId); |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 232 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 233 | routeService.addListener(routeListener); |
| 234 | asyncDeviceFetcher.getDevice(deviceId).whenComplete((deviceId, e) -> |
| 235 | interfaceManager = createRouter(deviceId, interfaces)); |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 236 | } else { |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 237 | interfaceManager.changeConfiguredInterfaces(interfaces); |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 238 | } |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 239 | } |
| 240 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 241 | /* |
| 242 | * Removes filtering objectives and routes before deactivate. |
| 243 | */ |
| 244 | private void cleanUp() { |
| 245 | //remove the route listener |
| 246 | routeService.removeListener(routeListener); |
| 247 | |
| 248 | //clean up the routes. |
| 249 | for (Map.Entry<IpPrefix, IpAddress> routes: prefixToNextHop.entrySet()) { |
| 250 | deleteRoute(new ResolvedRoute(routes.getKey(), null, null, null)); |
| 251 | } |
| 252 | |
| 253 | if (interfaceManager != null) { |
| 254 | interfaceManager.cleanup(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | private RouterInterfaceManager createRouter(DeviceId deviceId, Set<String> configuredInterfaces) { |
| 259 | return new RouterInterfaceManager(deviceId, |
| 260 | configuredInterfaces, |
| 261 | interfaceService, |
| 262 | this::provisionInterface, |
| 263 | this::unprovisionInterface); |
| 264 | } |
| 265 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 266 | private void updateRoute(ResolvedRoute route) { |
| 267 | addNextHop(route); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 268 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 269 | Integer nextId; |
| 270 | synchronized (this) { |
| 271 | nextId = nextHops.get(route.nextHop()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 274 | flowObjectiveService.forward(deviceId, |
| 275 | generateRibForwardingObj(route.prefix(), nextId).add()); |
| 276 | log.trace("Sending forwarding objective {} -> nextId:{}", route, nextId); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 279 | private synchronized void deleteRoute(ResolvedRoute route) { |
| 280 | //Integer nextId = nextHops.get(route.nextHop()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 281 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 282 | /* Group group = deleteNextHop(route.prefix()); |
| 283 | if (group == null) { |
| 284 | log.warn("Group not found when deleting {}", route); |
| 285 | return; |
| 286 | }*/ |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 287 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 288 | flowObjectiveService.forward(deviceId, |
| 289 | generateRibForwardingObj(route.prefix(), null).remove()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix, |
| 293 | Integer nextId) { |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 294 | TrafficSelector selector = buildIpSelectorFromIpPrefix(prefix).build(); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 295 | int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
| 296 | |
| 297 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 298 | .fromApp(routerAppId) |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 299 | .makePermanent() |
| 300 | .withSelector(selector) |
| 301 | .withPriority(priority) |
| 302 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 303 | |
| 304 | if (nextId == null) { |
| 305 | // Route withdraws are not specified with next hops. Generating |
| 306 | // dummy treatment as there is no equivalent nextId info. |
| 307 | fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build()); |
| 308 | } else { |
| 309 | fwdBuilder.nextStep(nextId); |
| 310 | } |
| 311 | return fwdBuilder; |
| 312 | } |
| 313 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 314 | /** |
| 315 | * Method to build IPv4 or IPv6 selector. |
| 316 | * |
| 317 | * @param prefixToMatch the prefix to match |
| 318 | * @return the traffic selector builder |
| 319 | */ |
| 320 | private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) { |
| 321 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 322 | // If the prefix is IPv4 |
| 323 | if (prefixToMatch.isIp4()) { |
| 324 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 325 | selectorBuilder.matchIPDst(prefixToMatch); |
| 326 | return selectorBuilder; |
| 327 | } |
| 328 | // If the prefix is IPv6 |
| 329 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 330 | selectorBuilder.matchIPv6Dst(prefixToMatch); |
| 331 | return selectorBuilder; |
| 332 | } |
| 333 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 334 | private synchronized void addNextHop(ResolvedRoute route) { |
| 335 | prefixToNextHop.put(route.prefix(), route.nextHop()); |
| 336 | if (nextHopsCount.count(route.nextHop()) == 0) { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 337 | // There was no next hop in the multiset |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 338 | Interface egressIntf = interfaceService.getMatchingInterface(route.nextHop()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 339 | if (egressIntf == null) { |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 340 | log.warn("no egress interface found for {}", route); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 341 | return; |
| 342 | } |
| 343 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 344 | NextHopGroupKey groupKey = new NextHopGroupKey(route.nextHop()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 345 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 346 | NextHop nextHop = new NextHop(route.nextHop(), route.nextHopMac(), groupKey); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 347 | |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 348 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 349 | .setEthSrc(egressIntf.mac()) |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 350 | .setEthDst(nextHop.mac()); |
| 351 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 352 | TrafficSelector.Builder metabuilder = null; |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 353 | if (!egressIntf.vlan().equals(VlanId.NONE)) { |
| 354 | treatment.pushVlan() |
| 355 | .setVlanId(egressIntf.vlan()) |
| 356 | .setVlanPcp((byte) 0); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 357 | } else { |
| 358 | // untagged outgoing port may require internal vlan in some pipelines |
| 359 | metabuilder = DefaultTrafficSelector.builder(); |
| 360 | metabuilder.matchVlanId(VlanId.vlanId(ASSIGNED_VLAN)); |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | treatment.setOutput(egressIntf.connectPoint().port()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 364 | |
| 365 | int nextId = flowObjectiveService.allocateNextId(); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 366 | NextObjective.Builder nextBuilder = DefaultNextObjective.builder() |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 367 | .withId(nextId) |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 368 | .addTreatment(treatment.build()) |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 369 | .withType(NextObjective.Type.SIMPLE) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 370 | .fromApp(routerAppId); |
| 371 | if (metabuilder != null) { |
| 372 | nextBuilder.withMeta(metabuilder.build()); |
| 373 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 374 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 375 | NextObjective nextObjective = nextBuilder.add(); // TODO add callbacks |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 376 | flowObjectiveService.next(deviceId, nextObjective); |
| 377 | |
| 378 | nextHops.put(nextHop.ip(), nextId); |
| 379 | |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 380 | if (routeToNextHop) { |
| 381 | // Install route to next hop |
| 382 | ForwardingObjective fob = |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 383 | generateRibForwardingObj(IpPrefix.valueOf(route.nextHop(), 32), nextId).add(); |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 384 | flowObjectiveService.forward(deviceId, fob); |
| 385 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 388 | nextHopsCount.add(route.nextHop()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /*private synchronized Group deleteNextHop(IpPrefix prefix) { |
| 392 | IpAddress nextHopIp = prefixToNextHop.remove(prefix); |
| 393 | NextHop nextHop = nextHops.get(nextHopIp); |
| 394 | if (nextHop == null) { |
| 395 | log.warn("No next hop found when removing prefix {}", prefix); |
| 396 | return null; |
| 397 | } |
| 398 | |
| 399 | Group group = groupService.getGroup(deviceId, |
| 400 | new DefaultGroupKey(appKryo. |
| 401 | serialize(nextHop.group()))); |
| 402 | |
| 403 | // FIXME disabling group deletes for now until we verify the logic is OK |
| 404 | if (nextHopsCount.remove(nextHopIp, 1) <= 1) { |
| 405 | // There was one or less next hops, so there are now none |
| 406 | |
| 407 | log.debug("removing group for next hop {}", nextHop); |
| 408 | |
| 409 | nextHops.remove(nextHopIp); |
| 410 | |
| 411 | groupService.removeGroup(deviceId, |
| 412 | new DefaultGroupKey(appKryo.build().serialize(nextHop.group())), |
| 413 | appId); |
| 414 | } |
| 415 | |
| 416 | return group; |
| 417 | }*/ |
| 418 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 419 | private void provisionInterface(Interface intf) { |
| 420 | updateInterfaceFilters(intf, true); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 421 | } |
| 422 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 423 | private void unprovisionInterface(Interface intf) { |
| 424 | updateInterfaceFilters(intf, false); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Installs or removes flow objectives relating to an interface. |
| 429 | * |
| 430 | * @param intf interface to update objectives for |
| 431 | * @param install true to install the objectives, false to remove them |
| 432 | */ |
| 433 | private void updateInterfaceFilters(Interface intf, boolean install) { |
| 434 | updateFilteringObjective(intf, install); |
| 435 | updateMcastFilteringObjective(intf, install); |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Installs or removes unicast filtering objectives relating to an interface. |
| 440 | * |
| 441 | * @param intf interface to update objectives for |
| 442 | * @param install true to install the objectives, false to remove them |
| 443 | */ |
| 444 | private void updateFilteringObjective(Interface intf, boolean install) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 445 | VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ? |
| 446 | VlanId.vlanId(ASSIGNED_VLAN) : |
| 447 | egressVlan(); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 448 | |
| 449 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 450 | // first add filter for the interface |
| 451 | fob.withKey(Criteria.matchInPort(intf.connectPoint().port())) |
| 452 | .addCondition(Criteria.matchEthDst(intf.mac())) |
| 453 | .addCondition(Criteria.matchVlanId(intf.vlan())); |
| 454 | fob.withPriority(PRIORITY_OFFSET); |
| 455 | if (intf.vlan() == VlanId.NONE) { |
| 456 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 457 | .pushVlan().setVlanId(assignedVlan).build(); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 458 | fob.withMeta(tt); |
| 459 | } |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 460 | fob.permit().fromApp(routerAppId); |
| 461 | sendFilteringObjective(install, fob, intf); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 462 | |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 463 | if (controlPlaneConnectPoint != null) { |
| 464 | // then add the same mac/vlan filters for control-plane connect point |
| 465 | fob.withKey(Criteria.matchInPort(controlPlaneConnectPoint.port())); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 466 | sendFilteringObjective(install, fob, intf); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 470 | /** |
| 471 | * Installs or removes multicast filtering objectives relating to an interface. |
| 472 | * |
| 473 | * @param intf interface to update objectives for |
| 474 | * @param install true to install the objectives, false to remove them |
| 475 | */ |
| 476 | private void updateMcastFilteringObjective(Interface intf, boolean install) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 477 | VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ? |
| 478 | VlanId.vlanId(ASSIGNED_VLAN) : |
| 479 | egressVlan(); |
| 480 | |
| 481 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 482 | // first add filter for the interface |
| 483 | fob.withKey(Criteria.matchInPort(intf.connectPoint().port())) |
| 484 | .addCondition(Criteria.matchEthDstMasked(MacAddress.IPV4_MULTICAST, |
| 485 | MacAddress.IPV4_MULTICAST_MASK)) |
| 486 | .addCondition(Criteria.matchVlanId(ingressVlan())); |
| 487 | fob.withPriority(PRIORITY_OFFSET); |
| 488 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
| 489 | .pushVlan().setVlanId(assignedVlan).build(); |
| 490 | fob.withMeta(tt); |
| 491 | |
| 492 | fob.permit().fromApp(routerAppId); |
| 493 | sendFilteringObjective(install, fob, intf); |
| 494 | } |
| 495 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 496 | private void sendFilteringObjective(boolean install, FilteringObjective.Builder fob, |
| 497 | Interface intf) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 498 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 499 | ObjectiveContext context = new DefaultObjectiveContext( |
| 500 | (objective) -> log.info("Installed filter for interface {}", intf), |
| 501 | (objective, error) -> |
| 502 | log.error("Failed to install filter for interface {}: {}", intf, error)); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 503 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 504 | FilteringObjective filter = install ? fob.add(context) : fob.remove(context); |
| 505 | |
| 506 | flowObjectiveService.filter(deviceId, filter); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 509 | private VlanId ingressVlan() { |
| 510 | McastConfig mcastConfig = |
| 511 | networkConfigService.getConfig(coreAppId, McastConfig.class); |
| 512 | return (mcastConfig != null) ? mcastConfig.ingressVlan() : VlanId.NONE; |
| 513 | } |
| 514 | |
| 515 | private VlanId egressVlan() { |
| 516 | McastConfig mcastConfig = |
| 517 | networkConfigService.getConfig(coreAppId, McastConfig.class); |
| 518 | return (mcastConfig != null) ? mcastConfig.egressVlan() : VlanId.NONE; |
| 519 | } |
| 520 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 521 | /** |
| 522 | * Listener for route changes. |
| 523 | */ |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 524 | private class InternalRouteListener implements RouteListener { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 525 | @Override |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 526 | public void event(RouteEvent event) { |
| 527 | ResolvedRoute route = event.subject(); |
| 528 | switch (event.type()) { |
| 529 | case ROUTE_ADDED: |
| 530 | case ROUTE_UPDATED: |
| 531 | updateRoute(route); |
| 532 | break; |
| 533 | case ROUTE_REMOVED: |
| 534 | deleteRoute(route); |
| 535 | break; |
| 536 | default: |
| 537 | break; |
| 538 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 542 | /** |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 543 | * Listener for network config events. |
| 544 | */ |
| 545 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 546 | @Override |
| 547 | public void event(NetworkConfigEvent event) { |
Jonathan Hart | 25bd668 | 2016-06-03 10:45:47 -0700 | [diff] [blame] | 548 | if (event.configClass().equals(RoutingService.ROUTER_CONFIG_CLASS)) { |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 549 | switch (event.type()) { |
| 550 | case CONFIG_ADDED: |
| 551 | case CONFIG_UPDATED: |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 552 | processRouterConfig(); |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 553 | break; |
| 554 | case CONFIG_REGISTERED: |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 555 | break; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 556 | case CONFIG_UNREGISTERED: |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 557 | break; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 558 | case CONFIG_REMOVED: |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 559 | cleanUp(); |
| 560 | break; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 561 | default: |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 567 | } |