blob: c6b3af402096544451f59180667674f6ddb05f4e [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 Hartc22e8472015-11-17 18:25:45 -080088public class SingleSwitchFibInstaller {
Jonathan Hartdf207092015-12-10 11:19:25 -080089
90 private final Logger log = LoggerFactory.getLogger(getClass());
gaurava2e61a52016-05-05 03:39:31 +053091 private static final String APP_NAME = "org.onosproject.vrouter";
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;
gaurava2e61a52016-05-05 03:39:31 +0530141 private ApplicationId vrouterAppId;
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);
gaurava2e61a52016-05-05 03:39:31 +0530171 vrouterAppId = 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
Charles Chan00d8b5f2016-12-04 17:17:39 -0800181 // FIXME: There can be an issue when this component is deactivated before vRouter.
182 // This will be addressed in CORD-710.
Jonathan Hartf8035d32016-06-16 16:23:26 -0700183 applicationService.registerDeactivateHook(vrouterAppId, () -> cleanUp());
gaurava2e61a52016-05-05 03:39:31 +0530184
Jonathan Hartdf207092015-12-10 11:19:25 -0800185 log.info("Started");
186 }
187
188 @Deactivate
189 protected void deactivate() {
Charles Chan00d8b5f2016-12-04 17:17:39 -0800190 // FIXME: This will also remove flows when an instance goes down.
191 // This is a temporary solution and should be addressed in CORD-710.
192 cleanUp();
193
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700194 asyncDeviceFetcher.shutdown();
Jonathan Hart25bd6682016-06-03 10:45:47 -0700195 networkConfigService.removeListener(configListener);
gaurav164cf6d2016-03-25 21:43:04 +0530196
Jonathan Hartb9401902016-02-02 18:46:01 -0800197 componentConfigService.unregisterProperties(getClass(), false);
198
Jonathan Hartdf207092015-12-10 11:19:25 -0800199 log.info("Stopped");
200 }
201
Jonathan Hartb9401902016-02-02 18:46:01 -0800202 @Modified
203 protected void modified(ComponentContext context) {
204 Dictionary<?, ?> properties = context.getProperties();
205 if (properties == null) {
206 return;
207 }
208
209 String strRouteToNextHop = Tools.get(properties, "routeToNextHop");
210 routeToNextHop = Boolean.parseBoolean(strRouteToNextHop);
211
212 log.info("routeToNextHop set to {}", routeToNextHop);
213 }
214
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800215 private void processRouterConfig() {
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800216 RouterConfig routerConfig =
217 networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS);
Jonathan Hartdf207092015-12-10 11:19:25 -0800218
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800219 if (routerConfig == null) {
220 log.info("Router config not available");
Jonathan Hartdf207092015-12-10 11:19:25 -0800221 return;
222 }
223
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700224 Set<String> interfaces = Sets.newHashSet(routerConfig.getInterfaces());
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800225
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700226 if (deviceId == null) {
227 controlPlaneConnectPoint = routerConfig.getControlPlaneConnectPoint();
228 log.info("Control Plane Connect Point: {}", controlPlaneConnectPoint);
Jonathan Hart883fd372016-02-10 14:36:15 -0800229
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700230 deviceId = routerConfig.getControlPlaneConnectPoint().deviceId();
231 log.info("Router device ID is {}", deviceId);
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800232
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700233 routeService.addListener(routeListener);
234 asyncDeviceFetcher.getDevice(deviceId).whenComplete((deviceId, e) ->
235 interfaceManager = createRouter(deviceId, interfaces));
gaurava2e61a52016-05-05 03:39:31 +0530236 } else {
Jonathan Hartf8cd0522016-10-25 07:09:55 -0700237 interfaceManager.changeConfiguredInterfaces(interfaces);
gaurava2e61a52016-05-05 03:39:31 +0530238 }
gaurav25118892016-08-24 05:35:03 +0530239 }
240
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800241 /*
242 * Removes filtering objectives and routes before deactivate.
243 */
244 private void cleanUp() {
245 //remove the route listener
246 routeService.removeListener(routeListener);
247
248 //clean up the routes.
249 for (Map.Entry<IpPrefix, IpAddress> routes: prefixToNextHop.entrySet()) {
250 deleteRoute(new ResolvedRoute(routes.getKey(), null, null, null));
251 }
252
253 if (interfaceManager != null) {
254 interfaceManager.cleanup();
255 }
256 }
257
258 private RouterInterfaceManager createRouter(DeviceId deviceId, Set<String> configuredInterfaces) {
259 return new RouterInterfaceManager(deviceId,
260 configuredInterfaces,
261 interfaceService,
262 this::provisionInterface,
263 this::unprovisionInterface);
264 }
265
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700266 private void updateRoute(ResolvedRoute route) {
267 addNextHop(route);
Jonathan Hartdf207092015-12-10 11:19:25 -0800268
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700269 Integer nextId;
270 synchronized (this) {
271 nextId = nextHops.get(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800272 }
273
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700274 flowObjectiveService.forward(deviceId,
275 generateRibForwardingObj(route.prefix(), nextId).add());
276 log.trace("Sending forwarding objective {} -> nextId:{}", route, nextId);
Jonathan Hartdf207092015-12-10 11:19:25 -0800277 }
278
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700279 private synchronized void deleteRoute(ResolvedRoute route) {
280 //Integer nextId = nextHops.get(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800281
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700282 /* Group group = deleteNextHop(route.prefix());
283 if (group == null) {
284 log.warn("Group not found when deleting {}", route);
285 return;
286 }*/
Jonathan Hartdf207092015-12-10 11:19:25 -0800287
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700288 flowObjectiveService.forward(deviceId,
289 generateRibForwardingObj(route.prefix(), null).remove());
Jonathan Hartdf207092015-12-10 11:19:25 -0800290 }
291
292 private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix,
293 Integer nextId) {
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530294 TrafficSelector selector = buildIpSelectorFromIpPrefix(prefix).build();
Jonathan Hartdf207092015-12-10 11:19:25 -0800295 int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
296
297 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder()
Saurav Das49cb5a12016-01-16 22:54:07 -0800298 .fromApp(routerAppId)
Jonathan Hartdf207092015-12-10 11:19:25 -0800299 .makePermanent()
300 .withSelector(selector)
301 .withPriority(priority)
302 .withFlag(ForwardingObjective.Flag.SPECIFIC);
303
304 if (nextId == null) {
305 // Route withdraws are not specified with next hops. Generating
306 // dummy treatment as there is no equivalent nextId info.
307 fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build());
308 } else {
309 fwdBuilder.nextStep(nextId);
310 }
311 return fwdBuilder;
312 }
313
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530314 /**
315 * Method to build IPv4 or IPv6 selector.
316 *
317 * @param prefixToMatch the prefix to match
318 * @return the traffic selector builder
319 */
320 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
321 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
322 // If the prefix is IPv4
323 if (prefixToMatch.isIp4()) {
324 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
325 selectorBuilder.matchIPDst(prefixToMatch);
326 return selectorBuilder;
327 }
328 // If the prefix is IPv6
329 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
330 selectorBuilder.matchIPv6Dst(prefixToMatch);
331 return selectorBuilder;
332 }
333
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700334 private synchronized void addNextHop(ResolvedRoute route) {
335 prefixToNextHop.put(route.prefix(), route.nextHop());
336 if (nextHopsCount.count(route.nextHop()) == 0) {
Jonathan Hartdf207092015-12-10 11:19:25 -0800337 // There was no next hop in the multiset
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700338 Interface egressIntf = interfaceService.getMatchingInterface(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800339 if (egressIntf == null) {
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700340 log.warn("no egress interface found for {}", route);
Jonathan Hartdf207092015-12-10 11:19:25 -0800341 return;
342 }
343
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700344 NextHopGroupKey groupKey = new NextHopGroupKey(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800345
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700346 NextHop nextHop = new NextHop(route.nextHop(), route.nextHopMac(), groupKey);
Jonathan Hartdf207092015-12-10 11:19:25 -0800347
Jonathan Hartca47cd72015-12-13 12:31:09 -0800348 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800349 .setEthSrc(egressIntf.mac())
Jonathan Hartca47cd72015-12-13 12:31:09 -0800350 .setEthDst(nextHop.mac());
351
Saurav Das49cb5a12016-01-16 22:54:07 -0800352 TrafficSelector.Builder metabuilder = null;
Jonathan Hartca47cd72015-12-13 12:31:09 -0800353 if (!egressIntf.vlan().equals(VlanId.NONE)) {
354 treatment.pushVlan()
355 .setVlanId(egressIntf.vlan())
356 .setVlanPcp((byte) 0);
Saurav Das49cb5a12016-01-16 22:54:07 -0800357 } else {
358 // untagged outgoing port may require internal vlan in some pipelines
359 metabuilder = DefaultTrafficSelector.builder();
360 metabuilder.matchVlanId(VlanId.vlanId(ASSIGNED_VLAN));
Jonathan Hartca47cd72015-12-13 12:31:09 -0800361 }
362
363 treatment.setOutput(egressIntf.connectPoint().port());
Jonathan Hartdf207092015-12-10 11:19:25 -0800364
365 int nextId = flowObjectiveService.allocateNextId();
Saurav Das49cb5a12016-01-16 22:54:07 -0800366 NextObjective.Builder nextBuilder = DefaultNextObjective.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800367 .withId(nextId)
Jonathan Hartca47cd72015-12-13 12:31:09 -0800368 .addTreatment(treatment.build())
Jonathan Hartdf207092015-12-10 11:19:25 -0800369 .withType(NextObjective.Type.SIMPLE)
Saurav Das49cb5a12016-01-16 22:54:07 -0800370 .fromApp(routerAppId);
371 if (metabuilder != null) {
372 nextBuilder.withMeta(metabuilder.build());
373 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800374
Saurav Das49cb5a12016-01-16 22:54:07 -0800375 NextObjective nextObjective = nextBuilder.add(); // TODO add callbacks
Jonathan Hartdf207092015-12-10 11:19:25 -0800376 flowObjectiveService.next(deviceId, nextObjective);
377
378 nextHops.put(nextHop.ip(), nextId);
379
Jonathan Hartb9401902016-02-02 18:46:01 -0800380 if (routeToNextHop) {
381 // Install route to next hop
382 ForwardingObjective fob =
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700383 generateRibForwardingObj(IpPrefix.valueOf(route.nextHop(), 32), nextId).add();
Jonathan Hartb9401902016-02-02 18:46:01 -0800384 flowObjectiveService.forward(deviceId, fob);
385 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800386 }
387
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700388 nextHopsCount.add(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800389 }
390
391 /*private synchronized Group deleteNextHop(IpPrefix prefix) {
392 IpAddress nextHopIp = prefixToNextHop.remove(prefix);
393 NextHop nextHop = nextHops.get(nextHopIp);
394 if (nextHop == null) {
395 log.warn("No next hop found when removing prefix {}", prefix);
396 return null;
397 }
398
399 Group group = groupService.getGroup(deviceId,
400 new DefaultGroupKey(appKryo.
401 serialize(nextHop.group())));
402
403 // FIXME disabling group deletes for now until we verify the logic is OK
404 if (nextHopsCount.remove(nextHopIp, 1) <= 1) {
405 // There was one or less next hops, so there are now none
406
407 log.debug("removing group for next hop {}", nextHop);
408
409 nextHops.remove(nextHopIp);
410
411 groupService.removeGroup(deviceId,
412 new DefaultGroupKey(appKryo.build().serialize(nextHop.group())),
413 appId);
414 }
415
416 return group;
417 }*/
418
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800419 private void provisionInterface(Interface intf) {
420 updateInterfaceFilters(intf, true);
gaurav164cf6d2016-03-25 21:43:04 +0530421 }
422
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800423 private void unprovisionInterface(Interface intf) {
424 updateInterfaceFilters(intf, false);
425 }
426
427 /**
428 * Installs or removes flow objectives relating to an interface.
429 *
430 * @param intf interface to update objectives for
431 * @param install true to install the objectives, false to remove them
432 */
433 private void updateInterfaceFilters(Interface intf, boolean install) {
434 updateFilteringObjective(intf, install);
435 updateMcastFilteringObjective(intf, install);
436 }
437
438 /**
439 * Installs or removes unicast filtering objectives relating to an interface.
440 *
441 * @param intf interface to update objectives for
442 * @param install true to install the objectives, false to remove them
443 */
444 private void updateFilteringObjective(Interface intf, boolean install) {
Charles Chand55e84d2016-03-30 17:54:24 -0700445 VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ?
446 VlanId.vlanId(ASSIGNED_VLAN) :
447 egressVlan();
gaurav164cf6d2016-03-25 21:43:04 +0530448
449 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
450 // first add filter for the interface
451 fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
452 .addCondition(Criteria.matchEthDst(intf.mac()))
453 .addCondition(Criteria.matchVlanId(intf.vlan()));
454 fob.withPriority(PRIORITY_OFFSET);
455 if (intf.vlan() == VlanId.NONE) {
456 TrafficTreatment tt = DefaultTrafficTreatment.builder()
Charles Chand55e84d2016-03-30 17:54:24 -0700457 .pushVlan().setVlanId(assignedVlan).build();
gaurav164cf6d2016-03-25 21:43:04 +0530458 fob.withMeta(tt);
459 }
gaurav164cf6d2016-03-25 21:43:04 +0530460 fob.permit().fromApp(routerAppId);
461 sendFilteringObjective(install, fob, intf);
Charles Chand55e84d2016-03-30 17:54:24 -0700462
gaurav164cf6d2016-03-25 21:43:04 +0530463 if (controlPlaneConnectPoint != null) {
464 // then add the same mac/vlan filters for control-plane connect point
465 fob.withKey(Criteria.matchInPort(controlPlaneConnectPoint.port()));
Saurav Das49cb5a12016-01-16 22:54:07 -0800466 sendFilteringObjective(install, fob, intf);
Jonathan Hartdf207092015-12-10 11:19:25 -0800467 }
468 }
469
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800470 /**
471 * Installs or removes multicast filtering objectives relating to an interface.
472 *
473 * @param intf interface to update objectives for
474 * @param install true to install the objectives, false to remove them
475 */
476 private void updateMcastFilteringObjective(Interface intf, boolean install) {
Charles Chand55e84d2016-03-30 17:54:24 -0700477 VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ?
478 VlanId.vlanId(ASSIGNED_VLAN) :
479 egressVlan();
480
481 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
482 // first add filter for the interface
483 fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
484 .addCondition(Criteria.matchEthDstMasked(MacAddress.IPV4_MULTICAST,
485 MacAddress.IPV4_MULTICAST_MASK))
486 .addCondition(Criteria.matchVlanId(ingressVlan()));
487 fob.withPriority(PRIORITY_OFFSET);
488 TrafficTreatment tt = DefaultTrafficTreatment.builder()
489 .pushVlan().setVlanId(assignedVlan).build();
490 fob.withMeta(tt);
491
492 fob.permit().fromApp(routerAppId);
493 sendFilteringObjective(install, fob, intf);
494 }
495
Saurav Das49cb5a12016-01-16 22:54:07 -0800496 private void sendFilteringObjective(boolean install, FilteringObjective.Builder fob,
497 Interface intf) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800498
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700499 ObjectiveContext context = new DefaultObjectiveContext(
500 (objective) -> log.info("Installed filter for interface {}", intf),
501 (objective, error) ->
502 log.error("Failed to install filter for interface {}: {}", intf, error));
gaurav164cf6d2016-03-25 21:43:04 +0530503
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700504 FilteringObjective filter = install ? fob.add(context) : fob.remove(context);
505
506 flowObjectiveService.filter(deviceId, filter);
Saurav Das49cb5a12016-01-16 22:54:07 -0800507 }
508
Charles Chand55e84d2016-03-30 17:54:24 -0700509 private VlanId ingressVlan() {
510 McastConfig mcastConfig =
511 networkConfigService.getConfig(coreAppId, McastConfig.class);
512 return (mcastConfig != null) ? mcastConfig.ingressVlan() : VlanId.NONE;
513 }
514
515 private VlanId egressVlan() {
516 McastConfig mcastConfig =
517 networkConfigService.getConfig(coreAppId, McastConfig.class);
518 return (mcastConfig != null) ? mcastConfig.egressVlan() : VlanId.NONE;
519 }
520
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800521 /**
522 * Listener for route changes.
523 */
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700524 private class InternalRouteListener implements RouteListener {
Jonathan Hartdf207092015-12-10 11:19:25 -0800525 @Override
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700526 public void event(RouteEvent event) {
527 ResolvedRoute route = event.subject();
528 switch (event.type()) {
529 case ROUTE_ADDED:
530 case ROUTE_UPDATED:
531 updateRoute(route);
532 break;
533 case ROUTE_REMOVED:
534 deleteRoute(route);
535 break;
536 default:
537 break;
538 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800539 }
540 }
541
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800542 /**
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800543 * Listener for network config events.
544 */
545 private class InternalNetworkConfigListener implements NetworkConfigListener {
546 @Override
547 public void event(NetworkConfigEvent event) {
Jonathan Hart25bd6682016-06-03 10:45:47 -0700548 if (event.configClass().equals(RoutingService.ROUTER_CONFIG_CLASS)) {
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800549 switch (event.type()) {
550 case CONFIG_ADDED:
551 case CONFIG_UPDATED:
Jonathan Hart7fc5a722017-01-26 15:54:12 -0800552 processRouterConfig();
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800553 break;
554 case CONFIG_REGISTERED:
gaurav25118892016-08-24 05:35:03 +0530555 break;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800556 case CONFIG_UNREGISTERED:
gaurav25118892016-08-24 05:35:03 +0530557 break;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800558 case CONFIG_REMOVED:
gaurav25118892016-08-24 05:35:03 +0530559 cleanUp();
560 break;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800561 default:
562 break;
563 }
564 }
565 }
566 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800567}