blob: 57fd26e65aa822d8544f5d09f502604d66ef265c [file] [log] [blame]
Rusty Eddy4ae5aa82015-12-15 12:58:27 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Rusty Eddy4ae5aa82015-12-15 12:58:27 -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 */
16package org.onosproject.pim.impl;
17
Jonathan Hart5af5f142016-01-28 18:45:27 -080018import com.google.common.collect.ImmutableSet;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080019import com.google.common.collect.Maps;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Jonathan Hart54119bb2016-02-06 18:48:27 -080026import org.onlab.util.SafeRecurringTask;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080027import org.onosproject.incubator.net.intf.Interface;
Jonathan Hart5af5f142016-01-28 18:45:27 -080028import org.onosproject.incubator.net.intf.InterfaceEvent;
29import org.onosproject.incubator.net.intf.InterfaceListener;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080030import org.onosproject.incubator.net.intf.InterfaceService;
31import org.onosproject.net.ConnectPoint;
Jonathan Hart00cddda2016-02-16 10:30:37 -080032
33import org.onosproject.net.Host;
34
Jonathan Hart5af5f142016-01-28 18:45:27 -080035import org.onosproject.net.config.ConfigFactory;
36import org.onosproject.net.config.NetworkConfigEvent;
37import org.onosproject.net.config.NetworkConfigListener;
38import org.onosproject.net.config.NetworkConfigRegistry;
39import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hart00cddda2016-02-16 10:30:37 -080040import org.onosproject.net.host.HostService;
41import org.onosproject.net.mcast.McastEvent;
42import org.onosproject.net.mcast.McastListener;
43import org.onosproject.net.mcast.McastRoute;
44import org.onosproject.net.mcast.MulticastRouteService;
Jonathan Hart36fd31e2016-01-28 15:55:31 -080045import org.onosproject.net.packet.PacketService;
Jonathan Hart00cddda2016-02-16 10:30:37 -080046import org.onosproject.routing.RouteEntry;
47import org.onosproject.routing.RoutingService;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080048import org.slf4j.Logger;
Jonathan Hart5af5f142016-01-28 18:45:27 -080049
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080050import java.util.Map;
Jonathan Hart5af5f142016-01-28 18:45:27 -080051import java.util.Set;
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080052import java.util.concurrent.Executors;
53import java.util.concurrent.ScheduledExecutorService;
54import java.util.concurrent.TimeUnit;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080055
56import static org.slf4j.LoggerFactory.getLogger;
57
58/**
59 * Manages PIMInterfaces.
60 *
61 * TODO: Do we need to add a ServiceListener?
62 */
63@Component(immediate = true)
64@Service
Jonathan Hartfbfe2a82016-03-29 11:36:33 -070065public class PimInterfaceManager implements PimInterfaceService {
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080066
67 private final Logger log = getLogger(getClass());
68
Jonathan Hart5af5f142016-01-28 18:45:27 -080069 private static final Class<PimInterfaceConfig> PIM_INTERFACE_CONFIG_CLASS = PimInterfaceConfig.class;
70 private static final String PIM_INTERFACE_CONFIG_KEY = "pimInterface";
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080071
Jonathan Hart6be70952016-02-12 21:11:26 -080072 public static final int DEFAULT_HELLO_INTERVAL = 30; // seconds
73
74 private static final int DEFAULT_TASK_PERIOD_MS = 250;
Jonathan Hart54119bb2016-02-06 18:48:27 -080075
76 // Create a Scheduled Executor service for recurring tasks
77 private final ScheduledExecutorService scheduledExecutorService =
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080078 Executors.newScheduledThreadPool(1);
79
Jonathan Hart6be70952016-02-12 21:11:26 -080080 private final long initialHelloDelay = 1000;
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080081
Jonathan Hart6be70952016-02-12 21:11:26 -080082 private final long pimHelloPeriod = DEFAULT_TASK_PERIOD_MS;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080083
Jonathan Hart6be70952016-02-12 21:11:26 -080084 private final int timeoutTaskPeriod = DEFAULT_TASK_PERIOD_MS;
Jonathan Hart54119bb2016-02-06 18:48:27 -080085
Jonathan Hart00cddda2016-02-16 10:30:37 -080086 private final int joinTaskPeriod = 10000;
87
Jonathan Hart36fd31e2016-01-28 15:55:31 -080088 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected PacketService packetService;
90
Jonathan Hart5af5f142016-01-28 18:45:27 -080091 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected NetworkConfigRegistry networkConfig;
93
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 protected InterfaceService interfaceService;
96
Jonathan Hart00cddda2016-02-16 10:30:37 -080097 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected HostService hostService;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected MulticastRouteService multicastRouteService;
102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected RoutingService unicastRoutingService;
105
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800106 // Store PIM Interfaces in a map key'd by ConnectPoint
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700107 private final Map<ConnectPoint, PimInterface> pimInterfaces = Maps.newConcurrentMap();
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800108
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700109 private final Map<McastRoute, PimInterface> routes = Maps.newConcurrentMap();
Jonathan Hart00cddda2016-02-16 10:30:37 -0800110
Jonathan Hart5af5f142016-01-28 18:45:27 -0800111 private final InternalNetworkConfigListener configListener =
112 new InternalNetworkConfigListener();
113 private final InternalInterfaceListener interfaceListener =
114 new InternalInterfaceListener();
Jonathan Hart00cddda2016-02-16 10:30:37 -0800115 private final InternalMulticastListener multicastListener =
116 new InternalMulticastListener();
Jonathan Hart5af5f142016-01-28 18:45:27 -0800117
118 private final ConfigFactory<ConnectPoint, PimInterfaceConfig> pimConfigFactory
119 = new ConfigFactory<ConnectPoint, PimInterfaceConfig>(
120 SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY, PIM_INTERFACE_CONFIG_CLASS,
121 PIM_INTERFACE_CONFIG_KEY) {
122
123 @Override
124 public PimInterfaceConfig createConfig() {
125 return new PimInterfaceConfig();
126 }
127 };
128
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800129 @Activate
130 public void activate() {
Jonathan Hart5af5f142016-01-28 18:45:27 -0800131 networkConfig.registerConfigFactory(pimConfigFactory);
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800132
Jonathan Hart5af5f142016-01-28 18:45:27 -0800133 // Create PIM Interfaces for each of the existing configured interfaces.
134 Set<ConnectPoint> subjects = networkConfig.getSubjects(
135 ConnectPoint.class, PIM_INTERFACE_CONFIG_CLASS);
136 for (ConnectPoint cp : subjects) {
137 PimInterfaceConfig config = networkConfig.getConfig(cp, PIM_INTERFACE_CONFIG_CLASS);
138 updateInterface(config);
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800139 }
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800140
Jonathan Hart5af5f142016-01-28 18:45:27 -0800141 networkConfig.addListener(configListener);
142 interfaceService.addListener(interfaceListener);
Jonathan Hart00cddda2016-02-16 10:30:37 -0800143 multicastRouteService.addListener(multicastListener);
144
145 multicastRouteService.getRoutes().forEach(this::addRoute);
Jonathan Hart5af5f142016-01-28 18:45:27 -0800146
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800147 // Schedule the periodic hello sender.
Jonathan Hart54119bb2016-02-06 18:48:27 -0800148 scheduledExecutorService.scheduleAtFixedRate(
149 SafeRecurringTask.wrap(
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700150 () -> pimInterfaces.values().forEach(PimInterface::sendHello)),
Jonathan Hart6be70952016-02-12 21:11:26 -0800151 initialHelloDelay, pimHelloPeriod, TimeUnit.MILLISECONDS);
Jonathan Hart54119bb2016-02-06 18:48:27 -0800152
153 // Schedule task to periodically time out expired neighbors
154 scheduledExecutorService.scheduleAtFixedRate(
155 SafeRecurringTask.wrap(
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700156 () -> pimInterfaces.values().forEach(PimInterface::checkNeighborTimeouts)),
Jonathan Hart54119bb2016-02-06 18:48:27 -0800157 0, timeoutTaskPeriod, TimeUnit.MILLISECONDS);
Jonathan Hart5af5f142016-01-28 18:45:27 -0800158
Jonathan Hart00cddda2016-02-16 10:30:37 -0800159 scheduledExecutorService.scheduleAtFixedRate(
160 SafeRecurringTask.wrap(
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700161 () -> pimInterfaces.values().forEach(PimInterface::sendJoins)),
Jonathan Hart00cddda2016-02-16 10:30:37 -0800162 0, joinTaskPeriod, TimeUnit.MILLISECONDS);
163
Jonathan Hart5af5f142016-01-28 18:45:27 -0800164 log.info("Started");
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800165 }
166
167 @Deactivate
168 public void deactivate() {
Jonathan Hart5af5f142016-01-28 18:45:27 -0800169 interfaceService.removeListener(interfaceListener);
170 networkConfig.removeListener(configListener);
Jonathan Hart00cddda2016-02-16 10:30:37 -0800171 multicastRouteService.removeListener(multicastListener);
Jonathan Hart5af5f142016-01-28 18:45:27 -0800172 networkConfig.unregisterConfigFactory(pimConfigFactory);
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800173
174 // Shutdown the periodic hello task.
Jonathan Hart54119bb2016-02-06 18:48:27 -0800175 scheduledExecutorService.shutdown();
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800176
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800177 log.info("Stopped");
178 }
179
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800180 @Override
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700181 public PimInterface getPimInterface(ConnectPoint cp) {
182 PimInterface pi = pimInterfaces.get(cp);
Jonathan Hart7f4bc522016-02-20 11:32:43 -0800183 if (pi == null && log.isTraceEnabled()) {
184 log.trace("We have been asked for an Interface we don't have: {}", cp);
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800185 }
186 return pi;
187 }
Jonathan Hart5af5f142016-01-28 18:45:27 -0800188
189 @Override
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700190 public Set<PimInterface> getPimInterfaces() {
Jonathan Hart5af5f142016-01-28 18:45:27 -0800191 return ImmutableSet.copyOf(pimInterfaces.values());
192 }
193
194 private void updateInterface(PimInterfaceConfig config) {
195 ConnectPoint cp = config.subject();
196
197 if (!config.isEnabled()) {
198 removeInterface(cp);
199 return;
200 }
201
202 String intfName = config.getInterfaceName();
203 Interface intf = interfaceService.getInterfaceByName(cp, intfName);
204
205 if (intf == null) {
206 log.debug("Interface configuration missing: {}", config.getInterfaceName());
207 return;
208 }
209
210
211 log.debug("Updating Interface for " + intf.connectPoint().toString());
212 pimInterfaces.computeIfAbsent(cp, k -> buildPimInterface(config, intf));
213 }
214
215 private void removeInterface(ConnectPoint cp) {
216 pimInterfaces.remove(cp);
217 }
218
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700219 private PimInterface buildPimInterface(PimInterfaceConfig config, Interface intf) {
220 PimInterface.Builder builder = PimInterface.builder()
Jonathan Hart5af5f142016-01-28 18:45:27 -0800221 .withPacketService(packetService)
222 .withInterface(intf);
223
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800224 config.getHelloInterval().ifPresent(builder::withHelloInterval);
225 config.getHoldTime().ifPresent(builder::withHoldTime);
226 config.getPriority().ifPresent(builder::withPriority);
227 config.getPropagationDelay().ifPresent(builder::withPropagationDelay);
228 config.getOverrideInterval().ifPresent(builder::withOverrideInterval);
Jonathan Hart5af5f142016-01-28 18:45:27 -0800229
230 return builder.build();
231 }
232
Jonathan Hart00cddda2016-02-16 10:30:37 -0800233 private void addRoute(McastRoute route) {
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700234 PimInterface pimInterface = getSourceInterface(route);
Jonathan Hart00cddda2016-02-16 10:30:37 -0800235
236 if (pimInterface == null) {
237 return;
238 }
239
240 routes.put(route, pimInterface);
241 }
242
243 private void removeRoute(McastRoute route) {
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700244 PimInterface pimInterface = routes.remove(route);
Jonathan Hart00cddda2016-02-16 10:30:37 -0800245
246 if (pimInterface == null) {
247 return;
248 }
249
250 pimInterface.removeRoute(route);
251 }
252
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700253 private PimInterface getSourceInterface(McastRoute route) {
Jonathan Hart00cddda2016-02-16 10:30:37 -0800254 RouteEntry routeEntry = unicastRoutingService.getLongestMatchableRouteEntry(route.source());
255
256 if (routeEntry == null) {
257 log.warn("No route to source {}", route.source());
258 return null;
259 }
260
261 Interface intf = interfaceService.getMatchingInterface(routeEntry.nextHop());
262
263 if (intf == null) {
264 log.warn("No interface with route to next hop {}", routeEntry.nextHop());
265 return null;
266 }
267
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700268 PimInterface pimInterface = pimInterfaces.get(intf.connectPoint());
Jonathan Hart00cddda2016-02-16 10:30:37 -0800269
270 if (pimInterface == null) {
271 log.warn("PIM is not enabled on interface {}", intf);
272 return null;
273 }
274
275 Set<Host> hosts = hostService.getHostsByIp(routeEntry.nextHop());
276 Host host = null;
277 for (Host h : hosts) {
278 if (h.vlan().equals(intf.vlan())) {
279 host = h;
280 }
281 }
282 if (host == null) {
283 log.warn("Next hop host entry not found: {}", routeEntry.nextHop());
284 return null;
285 }
286
287 pimInterface.addRoute(route, routeEntry.nextHop(), host.mac());
288
289 return pimInterface;
290 }
291
Jonathan Hart5af5f142016-01-28 18:45:27 -0800292 /**
293 * Listener for network config events.
294 */
295 private class InternalNetworkConfigListener implements NetworkConfigListener {
296
297 @Override
298 public void event(NetworkConfigEvent event) {
299 if (event.configClass() != PIM_INTERFACE_CONFIG_CLASS) {
300 return;
301 }
302
303 switch (event.type()) {
304 case CONFIG_REGISTERED:
305 case CONFIG_UNREGISTERED:
306 break;
307 case CONFIG_ADDED:
308 case CONFIG_UPDATED:
309 ConnectPoint cp = (ConnectPoint) event.subject();
310 PimInterfaceConfig config = networkConfig.getConfig(
311 cp, PIM_INTERFACE_CONFIG_CLASS);
312
313 updateInterface(config);
314 break;
315 case CONFIG_REMOVED:
316 removeInterface((ConnectPoint) event.subject());
317 break;
318 default:
319 break;
320 }
321 }
322 }
323
324 /**
325 * Listener for interface events.
326 */
327 private class InternalInterfaceListener implements InterfaceListener {
328
329 @Override
330 public void event(InterfaceEvent event) {
331 switch (event.type()) {
332 case INTERFACE_ADDED:
333 PimInterfaceConfig config = networkConfig.getConfig(
334 event.subject().connectPoint(), PIM_INTERFACE_CONFIG_CLASS);
335
336 if (config != null) {
337 updateInterface(config);
338 }
339 break;
340 case INTERFACE_UPDATED:
341 break;
342 case INTERFACE_REMOVED:
343 removeInterface(event.subject().connectPoint());
344 break;
345 default:
346 break;
347
348 }
349 }
350 }
Jonathan Hart00cddda2016-02-16 10:30:37 -0800351
352 /**
353 * Listener for multicast route events.
354 */
355 private class InternalMulticastListener implements McastListener {
356 @Override
357 public void event(McastEvent event) {
358 switch (event.type()) {
359 case ROUTE_ADDED:
360 addRoute(event.subject().route());
361 break;
362 case ROUTE_REMOVED:
363 removeRoute(event.subject().route());
364 break;
365 case SOURCE_ADDED:
366 case SINK_ADDED:
367 case SINK_REMOVED:
368 default:
369 break;
370 }
371 }
372 }
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800373}