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 | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 175 | updateConfig(); |
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 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 184 | private RouterInterfaceManager createRouter(DeviceId deviceId, Set<String> configuredInterfaces) { |
| 185 | return new RouterInterfaceManager(deviceId, |
| 186 | configuredInterfaces, |
| 187 | interfaceService, |
| 188 | intf -> processIntfFilter(true, intf), |
| 189 | intf -> processIntfFilter(false, intf) |
| 190 | ); |
| 191 | } |
| 192 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 193 | @Deactivate |
| 194 | protected void deactivate() { |
Charles Chan | 00d8b5f | 2016-12-04 17:17:39 -0800 | [diff] [blame] | 195 | // FIXME: This will also remove flows when an instance goes down. |
| 196 | // This is a temporary solution and should be addressed in CORD-710. |
| 197 | cleanUp(); |
| 198 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 199 | asyncDeviceFetcher.shutdown(); |
Jonathan Hart | 25bd668 | 2016-06-03 10:45:47 -0700 | [diff] [blame] | 200 | networkConfigService.removeListener(configListener); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 201 | |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 202 | componentConfigService.unregisterProperties(getClass(), false); |
| 203 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 204 | log.info("Stopped"); |
| 205 | } |
| 206 | |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 207 | @Modified |
| 208 | protected void modified(ComponentContext context) { |
| 209 | Dictionary<?, ?> properties = context.getProperties(); |
| 210 | if (properties == null) { |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | String strRouteToNextHop = Tools.get(properties, "routeToNextHop"); |
| 215 | routeToNextHop = Boolean.parseBoolean(strRouteToNextHop); |
| 216 | |
| 217 | log.info("routeToNextHop set to {}", routeToNextHop); |
| 218 | } |
| 219 | |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 220 | //remove filtering objectives and routes before deactivate. |
| 221 | private void cleanUp() { |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 222 | //remove the route listener |
| 223 | routeService.removeListener(routeListener); |
| 224 | |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 225 | //clean up the routes. |
| 226 | for (Map.Entry<IpPrefix, IpAddress> routes: prefixToNextHop.entrySet()) { |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 227 | deleteRoute(new ResolvedRoute(routes.getKey(), null, null, null)); |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 228 | } |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 229 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 230 | if (interfaceManager != null) { |
| 231 | interfaceManager.cleanup(); |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 232 | } |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 233 | } |
| 234 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 235 | private void updateConfig() { |
| 236 | RouterConfig routerConfig = |
| 237 | networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 238 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 239 | if (routerConfig == null) { |
| 240 | log.info("Router config not available"); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 244 | Set<String> interfaces = Sets.newHashSet(routerConfig.getInterfaces()); |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 245 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 246 | if (deviceId == null) { |
| 247 | controlPlaneConnectPoint = routerConfig.getControlPlaneConnectPoint(); |
| 248 | log.info("Control Plane Connect Point: {}", controlPlaneConnectPoint); |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 249 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 250 | deviceId = routerConfig.getControlPlaneConnectPoint().deviceId(); |
| 251 | log.info("Router device ID is {}", deviceId); |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 252 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 253 | routeService.addListener(routeListener); |
| 254 | asyncDeviceFetcher.getDevice(deviceId).whenComplete((deviceId, e) -> |
| 255 | interfaceManager = createRouter(deviceId, interfaces)); |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 256 | } else { |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 257 | interfaceManager.changeConfiguredInterfaces(interfaces); |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 258 | } |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 259 | } |
| 260 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 261 | private void updateRoute(ResolvedRoute route) { |
| 262 | addNextHop(route); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 263 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 264 | Integer nextId; |
| 265 | synchronized (this) { |
| 266 | nextId = nextHops.get(route.nextHop()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 269 | flowObjectiveService.forward(deviceId, |
| 270 | generateRibForwardingObj(route.prefix(), nextId).add()); |
| 271 | log.trace("Sending forwarding objective {} -> nextId:{}", route, nextId); |
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 | private synchronized void deleteRoute(ResolvedRoute route) { |
| 275 | //Integer nextId = nextHops.get(route.nextHop()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 276 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 277 | /* Group group = deleteNextHop(route.prefix()); |
| 278 | if (group == null) { |
| 279 | log.warn("Group not found when deleting {}", route); |
| 280 | return; |
| 281 | }*/ |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 282 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 283 | flowObjectiveService.forward(deviceId, |
| 284 | generateRibForwardingObj(route.prefix(), null).remove()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix, |
| 288 | Integer nextId) { |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 289 | TrafficSelector selector = buildIpSelectorFromIpPrefix(prefix).build(); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 290 | int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
| 291 | |
| 292 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 293 | .fromApp(routerAppId) |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 294 | .makePermanent() |
| 295 | .withSelector(selector) |
| 296 | .withPriority(priority) |
| 297 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 298 | |
| 299 | if (nextId == null) { |
| 300 | // Route withdraws are not specified with next hops. Generating |
| 301 | // dummy treatment as there is no equivalent nextId info. |
| 302 | fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build()); |
| 303 | } else { |
| 304 | fwdBuilder.nextStep(nextId); |
| 305 | } |
| 306 | return fwdBuilder; |
| 307 | } |
| 308 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 309 | /** |
| 310 | * Method to build IPv4 or IPv6 selector. |
| 311 | * |
| 312 | * @param prefixToMatch the prefix to match |
| 313 | * @return the traffic selector builder |
| 314 | */ |
| 315 | private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) { |
| 316 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 317 | // If the prefix is IPv4 |
| 318 | if (prefixToMatch.isIp4()) { |
| 319 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 320 | selectorBuilder.matchIPDst(prefixToMatch); |
| 321 | return selectorBuilder; |
| 322 | } |
| 323 | // If the prefix is IPv6 |
| 324 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 325 | selectorBuilder.matchIPv6Dst(prefixToMatch); |
| 326 | return selectorBuilder; |
| 327 | } |
| 328 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 329 | private synchronized void addNextHop(ResolvedRoute route) { |
| 330 | prefixToNextHop.put(route.prefix(), route.nextHop()); |
| 331 | if (nextHopsCount.count(route.nextHop()) == 0) { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 332 | // There was no next hop in the multiset |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 333 | Interface egressIntf = interfaceService.getMatchingInterface(route.nextHop()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 334 | if (egressIntf == null) { |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 335 | log.warn("no egress interface found for {}", route); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 339 | NextHopGroupKey groupKey = new NextHopGroupKey(route.nextHop()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 340 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 341 | NextHop nextHop = new NextHop(route.nextHop(), route.nextHopMac(), groupKey); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 342 | |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 343 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 344 | .setEthSrc(egressIntf.mac()) |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 345 | .setEthDst(nextHop.mac()); |
| 346 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 347 | TrafficSelector.Builder metabuilder = null; |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 348 | if (!egressIntf.vlan().equals(VlanId.NONE)) { |
| 349 | treatment.pushVlan() |
| 350 | .setVlanId(egressIntf.vlan()) |
| 351 | .setVlanPcp((byte) 0); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 352 | } else { |
| 353 | // untagged outgoing port may require internal vlan in some pipelines |
| 354 | metabuilder = DefaultTrafficSelector.builder(); |
| 355 | metabuilder.matchVlanId(VlanId.vlanId(ASSIGNED_VLAN)); |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | treatment.setOutput(egressIntf.connectPoint().port()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 359 | |
| 360 | int nextId = flowObjectiveService.allocateNextId(); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 361 | NextObjective.Builder nextBuilder = DefaultNextObjective.builder() |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 362 | .withId(nextId) |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 363 | .addTreatment(treatment.build()) |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 364 | .withType(NextObjective.Type.SIMPLE) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 365 | .fromApp(routerAppId); |
| 366 | if (metabuilder != null) { |
| 367 | nextBuilder.withMeta(metabuilder.build()); |
| 368 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 369 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 370 | NextObjective nextObjective = nextBuilder.add(); // TODO add callbacks |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 371 | flowObjectiveService.next(deviceId, nextObjective); |
| 372 | |
| 373 | nextHops.put(nextHop.ip(), nextId); |
| 374 | |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 375 | if (routeToNextHop) { |
| 376 | // Install route to next hop |
| 377 | ForwardingObjective fob = |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 378 | generateRibForwardingObj(IpPrefix.valueOf(route.nextHop(), 32), nextId).add(); |
Jonathan Hart | b940190 | 2016-02-02 18:46:01 -0800 | [diff] [blame] | 379 | flowObjectiveService.forward(deviceId, fob); |
| 380 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 383 | nextHopsCount.add(route.nextHop()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /*private synchronized Group deleteNextHop(IpPrefix prefix) { |
| 387 | IpAddress nextHopIp = prefixToNextHop.remove(prefix); |
| 388 | NextHop nextHop = nextHops.get(nextHopIp); |
| 389 | if (nextHop == null) { |
| 390 | log.warn("No next hop found when removing prefix {}", prefix); |
| 391 | return null; |
| 392 | } |
| 393 | |
| 394 | Group group = groupService.getGroup(deviceId, |
| 395 | new DefaultGroupKey(appKryo. |
| 396 | serialize(nextHop.group()))); |
| 397 | |
| 398 | // FIXME disabling group deletes for now until we verify the logic is OK |
| 399 | if (nextHopsCount.remove(nextHopIp, 1) <= 1) { |
| 400 | // There was one or less next hops, so there are now none |
| 401 | |
| 402 | log.debug("removing group for next hop {}", nextHop); |
| 403 | |
| 404 | nextHops.remove(nextHopIp); |
| 405 | |
| 406 | groupService.removeGroup(deviceId, |
| 407 | new DefaultGroupKey(appKryo.build().serialize(nextHop.group())), |
| 408 | appId); |
| 409 | } |
| 410 | |
| 411 | return group; |
| 412 | }*/ |
| 413 | |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 414 | //process filtering objective for interface add/remove. |
| 415 | private void processIntfFilter(boolean install, Interface intf) { |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 416 | createFilteringObjective(install, intf); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 417 | createMcastFilteringObjective(install, intf); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | //create filtering objective for interface |
| 421 | private void createFilteringObjective(boolean install, Interface intf) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 422 | VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ? |
| 423 | VlanId.vlanId(ASSIGNED_VLAN) : |
| 424 | egressVlan(); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 425 | |
| 426 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 427 | // first add filter for the interface |
| 428 | fob.withKey(Criteria.matchInPort(intf.connectPoint().port())) |
| 429 | .addCondition(Criteria.matchEthDst(intf.mac())) |
| 430 | .addCondition(Criteria.matchVlanId(intf.vlan())); |
| 431 | fob.withPriority(PRIORITY_OFFSET); |
| 432 | if (intf.vlan() == VlanId.NONE) { |
| 433 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 434 | .pushVlan().setVlanId(assignedVlan).build(); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 435 | fob.withMeta(tt); |
| 436 | } |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 437 | fob.permit().fromApp(routerAppId); |
| 438 | sendFilteringObjective(install, fob, intf); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 439 | |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 440 | if (controlPlaneConnectPoint != null) { |
| 441 | // then add the same mac/vlan filters for control-plane connect point |
| 442 | fob.withKey(Criteria.matchInPort(controlPlaneConnectPoint.port())); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 443 | sendFilteringObjective(install, fob, intf); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 447 | //create filtering objective for multicast traffic |
| 448 | private void createMcastFilteringObjective(boolean install, Interface intf) { |
| 449 | VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ? |
| 450 | VlanId.vlanId(ASSIGNED_VLAN) : |
| 451 | egressVlan(); |
| 452 | |
| 453 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 454 | // first add filter for the interface |
| 455 | fob.withKey(Criteria.matchInPort(intf.connectPoint().port())) |
| 456 | .addCondition(Criteria.matchEthDstMasked(MacAddress.IPV4_MULTICAST, |
| 457 | MacAddress.IPV4_MULTICAST_MASK)) |
| 458 | .addCondition(Criteria.matchVlanId(ingressVlan())); |
| 459 | fob.withPriority(PRIORITY_OFFSET); |
| 460 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
| 461 | .pushVlan().setVlanId(assignedVlan).build(); |
| 462 | fob.withMeta(tt); |
| 463 | |
| 464 | fob.permit().fromApp(routerAppId); |
| 465 | sendFilteringObjective(install, fob, intf); |
| 466 | } |
| 467 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 468 | private void sendFilteringObjective(boolean install, FilteringObjective.Builder fob, |
| 469 | Interface intf) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 470 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 471 | ObjectiveContext context = new DefaultObjectiveContext( |
| 472 | (objective) -> log.info("Installed filter for interface {}", intf), |
| 473 | (objective, error) -> |
| 474 | log.error("Failed to install filter for interface {}: {}", intf, error)); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 475 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 476 | FilteringObjective filter = install ? fob.add(context) : fob.remove(context); |
| 477 | |
| 478 | flowObjectiveService.filter(deviceId, filter); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 481 | private VlanId ingressVlan() { |
| 482 | McastConfig mcastConfig = |
| 483 | networkConfigService.getConfig(coreAppId, McastConfig.class); |
| 484 | return (mcastConfig != null) ? mcastConfig.ingressVlan() : VlanId.NONE; |
| 485 | } |
| 486 | |
| 487 | private VlanId egressVlan() { |
| 488 | McastConfig mcastConfig = |
| 489 | networkConfigService.getConfig(coreAppId, McastConfig.class); |
| 490 | return (mcastConfig != null) ? mcastConfig.egressVlan() : VlanId.NONE; |
| 491 | } |
| 492 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 493 | private class InternalRouteListener implements RouteListener { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 494 | @Override |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 495 | public void event(RouteEvent event) { |
| 496 | ResolvedRoute route = event.subject(); |
| 497 | switch (event.type()) { |
| 498 | case ROUTE_ADDED: |
| 499 | case ROUTE_UPDATED: |
| 500 | updateRoute(route); |
| 501 | break; |
| 502 | case ROUTE_REMOVED: |
| 503 | deleteRoute(route); |
| 504 | break; |
| 505 | default: |
| 506 | break; |
| 507 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 511 | /** |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 512 | * Listener for network config events. |
| 513 | */ |
| 514 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 515 | @Override |
| 516 | public void event(NetworkConfigEvent event) { |
Jonathan Hart | 25bd668 | 2016-06-03 10:45:47 -0700 | [diff] [blame] | 517 | if (event.configClass().equals(RoutingService.ROUTER_CONFIG_CLASS)) { |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 518 | switch (event.type()) { |
| 519 | case CONFIG_ADDED: |
| 520 | case CONFIG_UPDATED: |
| 521 | updateConfig(); |
| 522 | break; |
| 523 | case CONFIG_REGISTERED: |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 524 | break; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 525 | case CONFIG_UNREGISTERED: |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 526 | break; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 527 | case CONFIG_REMOVED: |
gaurav | 2511889 | 2016-08-24 05:35:03 +0530 | [diff] [blame] | 528 | cleanUp(); |
| 529 | break; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 530 | default: |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 536 | } |