blob: 6401679e2c3fb8cbd83ed63dbc6cd9f600a6b19e [file] [log] [blame]
Jonathan Hartdf207092015-12-10 11:19:25 -08001/*
Jonathan Hartf4bd0482017-01-27 15:11:18 -08002 * Copyright 2017-present Open Networking Laboratory
Jonathan Hartdf207092015-12-10 11:19:25 -08003 *
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 Hartf4bd0482017-01-27 15:11:18 -080017package org.onosproject.routing.fibinstaller;
Jonathan Hartdf207092015-12-10 11:19:25 -080018
19import com.google.common.collect.ConcurrentHashMultiset;
Jonathan Hartdf207092015-12-10 11:19:25 -080020import com.google.common.collect.Maps;
Jonathan Hartdf207092015-12-10 11:19:25 -080021import com.google.common.collect.Multiset;
Jonathan Hartf8cd0522016-10-25 07:09:55 -070022import com.google.common.collect.Sets;
Jonathan Hartdf207092015-12-10 11:19:25 -080023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Jonathan Hartb9401902016-02-02 18:46:01 -080026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
Jonathan Hartdf207092015-12-10 11:19:25 -080028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.onlab.packet.Ethernet;
31import org.onlab.packet.IpAddress;
32import org.onlab.packet.IpPrefix;
Charles Chand55e84d2016-03-30 17:54:24 -070033import org.onlab.packet.MacAddress;
Jonathan Hartca47cd72015-12-13 12:31:09 -080034import org.onlab.packet.VlanId;
Jonathan Hartb9401902016-02-02 18:46:01 -080035import org.onlab.util.Tools;
gaurava2e61a52016-05-05 03:39:31 +053036import org.onosproject.app.ApplicationService;
Jonathan Hartb9401902016-02-02 18:46:01 -080037import org.onosproject.cfg.ComponentConfigService;
Jonathan Hartdf207092015-12-10 11:19:25 -080038import org.onosproject.core.ApplicationId;
39import org.onosproject.core.CoreService;
Charles Chand55e84d2016-03-30 17:54:24 -070040import org.onosproject.incubator.net.config.basics.McastConfig;
Jonathan Hartdf207092015-12-10 11:19:25 -080041import org.onosproject.incubator.net.intf.Interface;
42import org.onosproject.incubator.net.intf.InterfaceService;
Jonathan Harta2eb9ff2016-04-13 21:27:06 -070043import org.onosproject.incubator.net.routing.ResolvedRoute;
44import org.onosproject.incubator.net.routing.RouteEvent;
45import org.onosproject.incubator.net.routing.RouteListener;
46import org.onosproject.incubator.net.routing.RouteService;
Saurav Das49cb5a12016-01-16 22:54:07 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hartdf207092015-12-10 11:19:25 -080048import org.onosproject.net.DeviceId;
Charles Chand55e84d2016-03-30 17:54:24 -070049import org.onosproject.net.config.ConfigFactory;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -080050import org.onosproject.net.config.NetworkConfigEvent;
51import org.onosproject.net.config.NetworkConfigListener;
Charles Chand55e84d2016-03-30 17:54:24 -070052import org.onosproject.net.config.NetworkConfigRegistry;
Jonathan Hartdf207092015-12-10 11:19:25 -080053import org.onosproject.net.config.NetworkConfigService;
Charles Chand55e84d2016-03-30 17:54:24 -070054import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartdf207092015-12-10 11:19:25 -080055import org.onosproject.net.device.DeviceService;
56import org.onosproject.net.flow.DefaultTrafficSelector;
57import org.onosproject.net.flow.DefaultTrafficTreatment;
58import org.onosproject.net.flow.TrafficSelector;
59import org.onosproject.net.flow.TrafficTreatment;
60import org.onosproject.net.flow.criteria.Criteria;
61import org.onosproject.net.flowobjective.DefaultFilteringObjective;
62import org.onosproject.net.flowobjective.DefaultForwardingObjective;
63import org.onosproject.net.flowobjective.DefaultNextObjective;
Jonathan Harta2eb9ff2016-04-13 21:27:06 -070064import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Jonathan Hartdf207092015-12-10 11:19:25 -080065import org.onosproject.net.flowobjective.FilteringObjective;
66import org.onosproject.net.flowobjective.FlowObjectiveService;
67import org.onosproject.net.flowobjective.ForwardingObjective;
68import org.onosproject.net.flowobjective.NextObjective;
Jonathan Hartdf207092015-12-10 11:19:25 -080069import org.onosproject.net.flowobjective.ObjectiveContext;
Jonathan Hartf4bd0482017-01-27 15:11:18 -080070import org.onosproject.routing.AsyncDeviceFetcher;
71import org.onosproject.routing.NextHop;
72import org.onosproject.routing.NextHopGroupKey;
73import org.onosproject.routing.RouterInterfaceManager;
Jonathan Hartdf207092015-12-10 11:19:25 -080074import org.onosproject.routing.RoutingService;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -080075import org.onosproject.routing.config.RouterConfig;
Jonathan Hartb9401902016-02-02 18:46:01 -080076import org.osgi.service.component.ComponentContext;
Jonathan Hartdf207092015-12-10 11:19:25 -080077import org.slf4j.Logger;
78import org.slf4j.LoggerFactory;
79
Jonathan Hartb9401902016-02-02 18:46:01 -080080import java.util.Dictionary;
Jonathan Hartdf207092015-12-10 11:19:25 -080081import java.util.Map;
Jonathan Hartdf207092015-12-10 11:19:25 -080082import java.util.Set;
83
84/**
85 * Programs routes to a single OpenFlow switch.
86 */
Jonathan Hartf4bd0482017-01-27 15:11:18 -080087@Component(immediate = true)
Jonathan Harte7327042017-02-02 13:11:25 -080088public class FibInstaller {
Jonathan Hartdf207092015-12-10 11:19:25 -080089
90 private final Logger log = LoggerFactory.getLogger(getClass());
Jonathan Harte7327042017-02-02 13:11:25 -080091 private static final String APP_NAME = "org.onosproject.fibinstaller";
Jonathan Hartdf207092015-12-10 11:19:25 -080092
93 private static final int PRIORITY_OFFSET = 100;
94 private static final int PRIORITY_MULTIPLIER = 5;
95
Charles Chan03a73e02016-10-24 14:52:01 -070096 // FIXME: This should be eliminated when we have an API in SR that
97 // programs the fabric switches for VR
Saurav Das49cb5a12016-01-16 22:54:07 -080098 public static final short ASSIGNED_VLAN = 4094;
99
Jonathan Hartdf207092015-12-10 11:19:25 -0800100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected CoreService coreService;
102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700104 protected RouteService routeService;
Jonathan Hartdf207092015-12-10 11:19:25 -0800105
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 Chand55e84d2016-03-30 17:54:24 -0700113 protected NetworkConfigRegistry networkConfigRegistry;
114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartb9401902016-02-02 18:46:01 -0800116 protected ComponentConfigService componentConfigService;
117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartdf207092015-12-10 11:19:25 -0800119 protected FlowObjectiveService flowObjectiveService;
120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
122 protected DeviceService deviceService;
123
gaurava2e61a52016-05-05 03:39:31 +0530124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
125 protected ApplicationService applicationService;
126
Jonathan Hartb9401902016-02-02 18:46:01 -0800127 @Property(name = "routeToNextHop", boolValue = false,
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530128 label = "Install a /32 or /128 route to each next hop")
Jonathan Hartb9401902016-02-02 18:46:01 -0800129 private boolean routeToNextHop = false;
130
Jonathan Hartdf207092015-12-10 11:19:25 -0800131 // Device id of data-plane switch - should be learned from config
132 private DeviceId deviceId;
133
Saurav Das49cb5a12016-01-16 22:54:07 -0800134 private ConnectPoint controlPlaneConnectPoint;
135
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700136 private RouterInterfaceManager interfaceManager;
137 private AsyncDeviceFetcher asyncDeviceFetcher;
Jonathan Hart883fd372016-02-10 14:36:15 -0800138
Charles Chand55e84d2016-03-30 17:54:24 -0700139 private ApplicationId coreAppId;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800140 private ApplicationId routerAppId;
Jonathan Harte7327042017-02-02 13:11:25 -0800141 private ApplicationId fibAppId;
Jonathan Hartdf207092015-12-10 11:19:25 -0800142
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 Hart25bd6682016-06-03 10:45:47 -0700152 private final InternalRouteListener routeListener = new InternalRouteListener();
153 private final InternalNetworkConfigListener configListener = new InternalNetworkConfigListener();
Jonathan Hartdf207092015-12-10 11:19:25 -0800154
Charles Chand55e84d2016-03-30 17:54:24 -0700155 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 Hartdf207092015-12-10 11:19:25 -0800164 @Activate
Jonathan Hartb9401902016-02-02 18:46:01 -0800165 protected void activate(ComponentContext context) {
Jonathan Hartb9401902016-02-02 18:46:01 -0800166 componentConfigService.registerProperties(getClass());
Jonathan Hart9ad777f2016-02-19 12:44:36 -0800167 modified(context);
168
Charles Chand55e84d2016-03-30 17:54:24 -0700169 coreAppId = coreService.registerApplication(CoreService.CORE_APP_NAME);
Jonathan Hart9ad777f2016-02-19 12:44:36 -0800170 routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID);
Jonathan Harte7327042017-02-02 13:11:25 -0800171 fibAppId = coreService.registerApplication(APP_NAME);
Jonathan Hartb9401902016-02-02 18:46:01 -0800172
Charles Chand55e84d2016-03-30 17:54:24 -0700173 networkConfigRegistry.registerConfigFactory(mcastConfigFactory);
174
Jonathan Hart25bd6682016-06-03 10:45:47 -0700175 networkConfigService.addListener(configListener);
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700176
177 asyncDeviceFetcher = AsyncDeviceFetcher.create(deviceService);
gaurav164cf6d2016-03-25 21:43:04 +0530178
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800179 processRouterConfig();
Jonathan Hartdf207092015-12-10 11:19:25 -0800180
Jonathan Harte7327042017-02-02 13:11:25 -0800181 applicationService.registerDeactivateHook(fibAppId, () -> cleanUp());
gaurava2e61a52016-05-05 03:39:31 +0530182
Jonathan Hartdf207092015-12-10 11:19:25 -0800183 log.info("Started");
184 }
185
186 @Deactivate
187 protected void deactivate() {
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700188 asyncDeviceFetcher.shutdown();
Jonathan Hart25bd6682016-06-03 10:45:47 -0700189 networkConfigService.removeListener(configListener);
gaurav164cf6d2016-03-25 21:43:04 +0530190
Jonathan Hartb9401902016-02-02 18:46:01 -0800191 componentConfigService.unregisterProperties(getClass(), false);
192
Jonathan Hartdf207092015-12-10 11:19:25 -0800193 log.info("Stopped");
194 }
195
Jonathan Hartb9401902016-02-02 18:46:01 -0800196 @Modified
197 protected void modified(ComponentContext context) {
198 Dictionary<?, ?> properties = context.getProperties();
199 if (properties == null) {
200 return;
201 }
202
203 String strRouteToNextHop = Tools.get(properties, "routeToNextHop");
204 routeToNextHop = Boolean.parseBoolean(strRouteToNextHop);
205
206 log.info("routeToNextHop set to {}", routeToNextHop);
207 }
208
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800209 private void processRouterConfig() {
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800210 RouterConfig routerConfig =
211 networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS);
Jonathan Hartdf207092015-12-10 11:19:25 -0800212
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800213 if (routerConfig == null) {
214 log.info("Router config not available");
Jonathan Hartdf207092015-12-10 11:19:25 -0800215 return;
216 }
217
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700218 Set<String> interfaces = Sets.newHashSet(routerConfig.getInterfaces());
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800219
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700220 if (deviceId == null) {
221 controlPlaneConnectPoint = routerConfig.getControlPlaneConnectPoint();
222 log.info("Control Plane Connect Point: {}", controlPlaneConnectPoint);
Jonathan Hart883fd372016-02-10 14:36:15 -0800223
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700224 deviceId = routerConfig.getControlPlaneConnectPoint().deviceId();
225 log.info("Router device ID is {}", deviceId);
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800226
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700227 routeService.addListener(routeListener);
228 asyncDeviceFetcher.getDevice(deviceId).whenComplete((deviceId, e) ->
229 interfaceManager = createRouter(deviceId, interfaces));
gaurava2e61a52016-05-05 03:39:31 +0530230 } else {
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700231 interfaceManager.changeConfiguredInterfaces(interfaces);
gaurava2e61a52016-05-05 03:39:31 +0530232 }
gaurav25118892016-08-24 05:35:03 +0530233 }
234
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800235 /*
236 * Removes filtering objectives and routes before deactivate.
237 */
238 private void cleanUp() {
239 //remove the route listener
240 routeService.removeListener(routeListener);
241
242 //clean up the routes.
243 for (Map.Entry<IpPrefix, IpAddress> routes: prefixToNextHop.entrySet()) {
244 deleteRoute(new ResolvedRoute(routes.getKey(), null, null, null));
245 }
246
247 if (interfaceManager != null) {
248 interfaceManager.cleanup();
249 }
250 }
251
252 private RouterInterfaceManager createRouter(DeviceId deviceId, Set<String> configuredInterfaces) {
253 return new RouterInterfaceManager(deviceId,
254 configuredInterfaces,
255 interfaceService,
256 this::provisionInterface,
257 this::unprovisionInterface);
258 }
259
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700260 private void updateRoute(ResolvedRoute route) {
261 addNextHop(route);
Jonathan Hartdf207092015-12-10 11:19:25 -0800262
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700263 Integer nextId;
264 synchronized (this) {
265 nextId = nextHops.get(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800266 }
267
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700268 flowObjectiveService.forward(deviceId,
269 generateRibForwardingObj(route.prefix(), nextId).add());
270 log.trace("Sending forwarding objective {} -> nextId:{}", route, nextId);
Jonathan Hartdf207092015-12-10 11:19:25 -0800271 }
272
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700273 private synchronized void deleteRoute(ResolvedRoute route) {
274 //Integer nextId = nextHops.get(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800275
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700276 /* Group group = deleteNextHop(route.prefix());
277 if (group == null) {
278 log.warn("Group not found when deleting {}", route);
279 return;
280 }*/
Jonathan Hartdf207092015-12-10 11:19:25 -0800281
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700282 flowObjectiveService.forward(deviceId,
283 generateRibForwardingObj(route.prefix(), null).remove());
Jonathan Hartdf207092015-12-10 11:19:25 -0800284 }
285
286 private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix,
287 Integer nextId) {
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530288 TrafficSelector selector = buildIpSelectorFromIpPrefix(prefix).build();
Jonathan Hartdf207092015-12-10 11:19:25 -0800289 int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
290
291 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder()
Jonathan Harte7327042017-02-02 13:11:25 -0800292 .fromApp(fibAppId)
Jonathan Hartdf207092015-12-10 11:19:25 -0800293 .makePermanent()
294 .withSelector(selector)
295 .withPriority(priority)
296 .withFlag(ForwardingObjective.Flag.SPECIFIC);
297
298 if (nextId == null) {
299 // Route withdraws are not specified with next hops. Generating
300 // dummy treatment as there is no equivalent nextId info.
301 fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build());
302 } else {
303 fwdBuilder.nextStep(nextId);
304 }
305 return fwdBuilder;
306 }
307
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530308 /**
309 * Method to build IPv4 or IPv6 selector.
310 *
311 * @param prefixToMatch the prefix to match
312 * @return the traffic selector builder
313 */
314 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
315 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
316 // If the prefix is IPv4
317 if (prefixToMatch.isIp4()) {
318 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
319 selectorBuilder.matchIPDst(prefixToMatch);
320 return selectorBuilder;
321 }
322 // If the prefix is IPv6
323 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
324 selectorBuilder.matchIPv6Dst(prefixToMatch);
325 return selectorBuilder;
326 }
327
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700328 private synchronized void addNextHop(ResolvedRoute route) {
329 prefixToNextHop.put(route.prefix(), route.nextHop());
330 if (nextHopsCount.count(route.nextHop()) == 0) {
Jonathan Hartdf207092015-12-10 11:19:25 -0800331 // There was no next hop in the multiset
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700332 Interface egressIntf = interfaceService.getMatchingInterface(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800333 if (egressIntf == null) {
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700334 log.warn("no egress interface found for {}", route);
Jonathan Hartdf207092015-12-10 11:19:25 -0800335 return;
336 }
337
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700338 NextHopGroupKey groupKey = new NextHopGroupKey(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800339
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700340 NextHop nextHop = new NextHop(route.nextHop(), route.nextHopMac(), groupKey);
Jonathan Hartdf207092015-12-10 11:19:25 -0800341
Jonathan Hartca47cd72015-12-13 12:31:09 -0800342 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800343 .setEthSrc(egressIntf.mac())
Jonathan Hartca47cd72015-12-13 12:31:09 -0800344 .setEthDst(nextHop.mac());
345
Saurav Das49cb5a12016-01-16 22:54:07 -0800346 TrafficSelector.Builder metabuilder = null;
Jonathan Hartca47cd72015-12-13 12:31:09 -0800347 if (!egressIntf.vlan().equals(VlanId.NONE)) {
348 treatment.pushVlan()
349 .setVlanId(egressIntf.vlan())
350 .setVlanPcp((byte) 0);
Saurav Das49cb5a12016-01-16 22:54:07 -0800351 } else {
352 // untagged outgoing port may require internal vlan in some pipelines
353 metabuilder = DefaultTrafficSelector.builder();
354 metabuilder.matchVlanId(VlanId.vlanId(ASSIGNED_VLAN));
Jonathan Hartca47cd72015-12-13 12:31:09 -0800355 }
356
357 treatment.setOutput(egressIntf.connectPoint().port());
Jonathan Hartdf207092015-12-10 11:19:25 -0800358
359 int nextId = flowObjectiveService.allocateNextId();
Saurav Das49cb5a12016-01-16 22:54:07 -0800360 NextObjective.Builder nextBuilder = DefaultNextObjective.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800361 .withId(nextId)
Jonathan Hartca47cd72015-12-13 12:31:09 -0800362 .addTreatment(treatment.build())
Jonathan Hartdf207092015-12-10 11:19:25 -0800363 .withType(NextObjective.Type.SIMPLE)
Jonathan Harte7327042017-02-02 13:11:25 -0800364 .fromApp(fibAppId);
Saurav Das49cb5a12016-01-16 22:54:07 -0800365 if (metabuilder != null) {
366 nextBuilder.withMeta(metabuilder.build());
367 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800368
Saurav Das49cb5a12016-01-16 22:54:07 -0800369 NextObjective nextObjective = nextBuilder.add(); // TODO add callbacks
Jonathan Hartdf207092015-12-10 11:19:25 -0800370 flowObjectiveService.next(deviceId, nextObjective);
371
372 nextHops.put(nextHop.ip(), nextId);
373
Jonathan Hartb9401902016-02-02 18:46:01 -0800374 if (routeToNextHop) {
375 // Install route to next hop
376 ForwardingObjective fob =
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700377 generateRibForwardingObj(IpPrefix.valueOf(route.nextHop(), 32), nextId).add();
Jonathan Hartb9401902016-02-02 18:46:01 -0800378 flowObjectiveService.forward(deviceId, fob);
379 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800380 }
381
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700382 nextHopsCount.add(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800383 }
384
385 /*private synchronized Group deleteNextHop(IpPrefix prefix) {
386 IpAddress nextHopIp = prefixToNextHop.remove(prefix);
387 NextHop nextHop = nextHops.get(nextHopIp);
388 if (nextHop == null) {
389 log.warn("No next hop found when removing prefix {}", prefix);
390 return null;
391 }
392
393 Group group = groupService.getGroup(deviceId,
394 new DefaultGroupKey(appKryo.
395 serialize(nextHop.group())));
396
397 // FIXME disabling group deletes for now until we verify the logic is OK
398 if (nextHopsCount.remove(nextHopIp, 1) <= 1) {
399 // There was one or less next hops, so there are now none
400
401 log.debug("removing group for next hop {}", nextHop);
402
403 nextHops.remove(nextHopIp);
404
405 groupService.removeGroup(deviceId,
406 new DefaultGroupKey(appKryo.build().serialize(nextHop.group())),
407 appId);
408 }
409
410 return group;
411 }*/
412
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800413 private void provisionInterface(Interface intf) {
414 updateInterfaceFilters(intf, true);
gaurav164cf6d2016-03-25 21:43:04 +0530415 }
416
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800417 private void unprovisionInterface(Interface intf) {
418 updateInterfaceFilters(intf, false);
419 }
420
421 /**
422 * Installs or removes flow objectives relating to an interface.
423 *
424 * @param intf interface to update objectives for
425 * @param install true to install the objectives, false to remove them
426 */
427 private void updateInterfaceFilters(Interface intf, boolean install) {
428 updateFilteringObjective(intf, install);
429 updateMcastFilteringObjective(intf, install);
430 }
431
432 /**
433 * Installs or removes unicast filtering objectives relating to an interface.
434 *
435 * @param intf interface to update objectives for
436 * @param install true to install the objectives, false to remove them
437 */
438 private void updateFilteringObjective(Interface intf, boolean install) {
Charles Chand55e84d2016-03-30 17:54:24 -0700439 VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ?
440 VlanId.vlanId(ASSIGNED_VLAN) :
441 egressVlan();
gaurav164cf6d2016-03-25 21:43:04 +0530442
443 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
444 // first add filter for the interface
445 fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
446 .addCondition(Criteria.matchEthDst(intf.mac()))
447 .addCondition(Criteria.matchVlanId(intf.vlan()));
448 fob.withPriority(PRIORITY_OFFSET);
449 if (intf.vlan() == VlanId.NONE) {
450 TrafficTreatment tt = DefaultTrafficTreatment.builder()
Charles Chand55e84d2016-03-30 17:54:24 -0700451 .pushVlan().setVlanId(assignedVlan).build();
gaurav164cf6d2016-03-25 21:43:04 +0530452 fob.withMeta(tt);
453 }
Jonathan Harte7327042017-02-02 13:11:25 -0800454 fob.permit().fromApp(fibAppId);
gaurav164cf6d2016-03-25 21:43:04 +0530455 sendFilteringObjective(install, fob, intf);
Charles Chand55e84d2016-03-30 17:54:24 -0700456
gaurav164cf6d2016-03-25 21:43:04 +0530457 if (controlPlaneConnectPoint != null) {
458 // then add the same mac/vlan filters for control-plane connect point
459 fob.withKey(Criteria.matchInPort(controlPlaneConnectPoint.port()));
Saurav Das49cb5a12016-01-16 22:54:07 -0800460 sendFilteringObjective(install, fob, intf);
Jonathan Hartdf207092015-12-10 11:19:25 -0800461 }
462 }
463
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800464 /**
465 * Installs or removes multicast filtering objectives relating to an interface.
466 *
467 * @param intf interface to update objectives for
468 * @param install true to install the objectives, false to remove them
469 */
470 private void updateMcastFilteringObjective(Interface intf, boolean install) {
Charles Chand55e84d2016-03-30 17:54:24 -0700471 VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ?
472 VlanId.vlanId(ASSIGNED_VLAN) :
473 egressVlan();
474
475 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
476 // first add filter for the interface
477 fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
478 .addCondition(Criteria.matchEthDstMasked(MacAddress.IPV4_MULTICAST,
479 MacAddress.IPV4_MULTICAST_MASK))
480 .addCondition(Criteria.matchVlanId(ingressVlan()));
481 fob.withPriority(PRIORITY_OFFSET);
482 TrafficTreatment tt = DefaultTrafficTreatment.builder()
483 .pushVlan().setVlanId(assignedVlan).build();
484 fob.withMeta(tt);
485
Jonathan Harte7327042017-02-02 13:11:25 -0800486 fob.permit().fromApp(fibAppId);
Charles Chand55e84d2016-03-30 17:54:24 -0700487 sendFilteringObjective(install, fob, intf);
488 }
489
Saurav Das49cb5a12016-01-16 22:54:07 -0800490 private void sendFilteringObjective(boolean install, FilteringObjective.Builder fob,
491 Interface intf) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800492
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700493 ObjectiveContext context = new DefaultObjectiveContext(
494 (objective) -> log.info("Installed filter for interface {}", intf),
495 (objective, error) ->
496 log.error("Failed to install filter for interface {}: {}", intf, error));
gaurav164cf6d2016-03-25 21:43:04 +0530497
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700498 FilteringObjective filter = install ? fob.add(context) : fob.remove(context);
499
500 flowObjectiveService.filter(deviceId, filter);
Saurav Das49cb5a12016-01-16 22:54:07 -0800501 }
502
Charles Chand55e84d2016-03-30 17:54:24 -0700503 private VlanId ingressVlan() {
504 McastConfig mcastConfig =
505 networkConfigService.getConfig(coreAppId, McastConfig.class);
506 return (mcastConfig != null) ? mcastConfig.ingressVlan() : VlanId.NONE;
507 }
508
509 private VlanId egressVlan() {
510 McastConfig mcastConfig =
511 networkConfigService.getConfig(coreAppId, McastConfig.class);
512 return (mcastConfig != null) ? mcastConfig.egressVlan() : VlanId.NONE;
513 }
514
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800515 /**
516 * Listener for route changes.
517 */
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700518 private class InternalRouteListener implements RouteListener {
Jonathan Hartdf207092015-12-10 11:19:25 -0800519 @Override
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700520 public void event(RouteEvent event) {
521 ResolvedRoute route = event.subject();
522 switch (event.type()) {
523 case ROUTE_ADDED:
524 case ROUTE_UPDATED:
525 updateRoute(route);
526 break;
527 case ROUTE_REMOVED:
528 deleteRoute(route);
529 break;
530 default:
531 break;
532 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800533 }
534 }
535
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800536 /**
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800537 * Listener for network config events.
538 */
539 private class InternalNetworkConfigListener implements NetworkConfigListener {
540 @Override
541 public void event(NetworkConfigEvent event) {
Jonathan Hart25bd6682016-06-03 10:45:47 -0700542 if (event.configClass().equals(RoutingService.ROUTER_CONFIG_CLASS)) {
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800543 switch (event.type()) {
544 case CONFIG_ADDED:
545 case CONFIG_UPDATED:
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800546 processRouterConfig();
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800547 break;
548 case CONFIG_REGISTERED:
gaurav25118892016-08-24 05:35:03 +0530549 break;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800550 case CONFIG_UNREGISTERED:
gaurav25118892016-08-24 05:35:03 +0530551 break;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800552 case CONFIG_REMOVED:
gaurav25118892016-08-24 05:35:03 +0530553 cleanUp();
554 break;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800555 default:
556 break;
557 }
558 }
559 }
560 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800561}