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