blob: 5d86487fa7c2683e2ae5900c9a8b0fadd53e1425 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-04-01 13:05:26 -07003 *
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.segmentrouting;
17
Jonathan Hart61e24e12017-11-30 18:23:42 -080018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.ImmutableMap;
pierventrec80a5052021-05-20 12:56:38 +020020import com.google.common.collect.Lists;
Jonathan Hart61e24e12017-11-30 18:23:42 -080021import com.google.common.collect.Maps;
22import com.google.common.collect.Multimap;
Charles Chanf0ae41e2017-08-23 13:55:39 -070023import com.google.common.collect.Sets;
sangho80f11cb2015-04-01 13:05:26 -070024import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080025import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070026import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080027import org.onlab.packet.IPv6;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070028import org.onlab.packet.IpAddress;
Charles Chan77277672015-10-20 16:24:19 -070029import org.onlab.packet.IpPrefix;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +000030import org.onlab.packet.MacAddress;
Carmelo Cascone61004262020-01-16 13:36:02 -080031import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070032import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080033import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070034import org.onosproject.cfg.ComponentConfigService;
Saurav Das00e553b2018-04-21 17:19:48 -070035import org.onosproject.cluster.ClusterEvent;
36import org.onosproject.cluster.ClusterEventListener;
Pier Luigi580fd8a2018-01-16 10:47:50 +010037import org.onosproject.cluster.ClusterService;
Pierdb27b8d2018-04-17 16:29:56 +020038import org.onosproject.cluster.NodeId;
sangho80f11cb2015-04-01 13:05:26 -070039import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
41import org.onosproject.event.Event;
Charles Chanfbcb8812018-04-18 18:41:05 -070042import org.onosproject.mastership.MastershipEvent;
43import org.onosproject.mastership.MastershipListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070044import org.onosproject.mastership.MastershipService;
Pier1f87aca2018-03-14 16:47:32 -070045import org.onosproject.mcast.api.McastEvent;
46import org.onosproject.mcast.api.McastListener;
47import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080048import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070049import org.onosproject.net.Device;
50import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070051import org.onosproject.net.Host;
52import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070053import org.onosproject.net.Link;
54import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080055import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070056import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070057import org.onosproject.net.config.ConfigFactory;
58import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070059import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070060import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070061import org.onosproject.net.config.basics.InterfaceConfig;
62import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070063import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase6c448a2018-01-18 12:07:33 -080064import org.onosproject.net.device.DeviceAdminService;
Jonathan Hart54541d12016-04-12 15:39:44 -070065import org.onosproject.net.device.DeviceEvent;
66import org.onosproject.net.device.DeviceListener;
67import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080068import org.onosproject.net.flow.TrafficSelector;
69import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070070import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan0b1dd7e2018-08-19 19:21:46 -070071import org.onosproject.net.flowobjective.NextObjective;
Charles Chanf4586112015-11-09 16:37:23 -080072import org.onosproject.net.host.HostEvent;
73import org.onosproject.net.host.HostListener;
Charles Chanc6bcdf92018-06-01 16:33:48 -070074import org.onosproject.net.host.HostProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080075import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070076import org.onosproject.net.host.InterfaceIpAddress;
Pierdb27b8d2018-04-17 16:29:56 +020077import org.onosproject.net.intent.WorkPartitionService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070078import org.onosproject.net.intf.Interface;
79import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080080import org.onosproject.net.link.LinkEvent;
81import org.onosproject.net.link.LinkListener;
82import org.onosproject.net.link.LinkService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070083import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080084import org.onosproject.net.packet.InboundPacket;
85import org.onosproject.net.packet.PacketContext;
86import org.onosproject.net.packet.PacketProcessor;
87import org.onosproject.net.packet.PacketService;
Pier Luigid8a15162018-02-15 16:33:08 +010088import org.onosproject.net.topology.TopologyEvent;
89import org.onosproject.net.topology.TopologyListener;
Charles Chanc91c8782016-03-30 17:54:24 -070090import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070091import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070092import org.onosproject.routeservice.RouteEvent;
93import org.onosproject.routeservice.RouteListener;
94import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070095import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
Pier Ventre6b19e482016-11-07 16:21:04 -080096import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070097import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070098import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -070099import org.onosproject.segmentrouting.grouphandler.DestinationSet;
100import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Harshada Chaundkar9204f312019-07-02 16:01:24 +0000101import org.onosproject.segmentrouting.mcast.McastFilteringObjStoreKey;
Pier Luigi69f774d2018-02-28 12:10:50 +0100102import org.onosproject.segmentrouting.mcast.McastHandler;
103import org.onosproject.segmentrouting.mcast.McastRole;
Piere99511d2018-04-19 16:47:06 +0200104import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700105import org.onosproject.segmentrouting.mcast.McastStoreKey;
Charles Chan12a8a842020-02-14 13:23:57 -0800106import org.onosproject.segmentrouting.phasedrecovery.api.PhasedRecoveryService;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700110import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800111import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700112import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700113import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800114import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das261c3002017-06-13 15:35:54 -0700115import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Carmelo Cascone61004262020-01-16 13:36:02 -0800116import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700117import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800118import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700119import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chan8d316332018-06-19 20:31:57 -0700120import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hart54541d12016-04-12 15:39:44 -0700121import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700122import org.onosproject.store.service.EventuallyConsistentMap;
123import org.onosproject.store.service.EventuallyConsistentMapBuilder;
124import org.onosproject.store.service.StorageService;
125import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800126import org.osgi.service.component.ComponentContext;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700127import org.osgi.service.component.annotations.Activate;
128import org.osgi.service.component.annotations.Component;
129import org.osgi.service.component.annotations.Deactivate;
130import org.osgi.service.component.annotations.Modified;
131import org.osgi.service.component.annotations.Reference;
132import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chanb4f9cd12019-02-27 11:46:32 -0800133import org.osgi.service.component.annotations.ReferencePolicy;
sangho80f11cb2015-04-01 13:05:26 -0700134import org.slf4j.Logger;
135import org.slf4j.LoggerFactory;
136
Saurav Das00e553b2018-04-21 17:19:48 -0700137import java.time.Instant;
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700138import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800139import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800140import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800141import java.util.HashSet;
142import java.util.List;
143import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800144import java.util.Optional;
145import java.util.Set;
Andrea Campanella060cad82018-04-17 12:09:34 +0200146import java.util.concurrent.CompletableFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800147import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella060cad82018-04-17 12:09:34 +0200148import java.util.concurrent.CopyOnWriteArrayList;
pierventrea3989be2021-01-08 16:43:17 +0100149import java.util.concurrent.ExecutionException;
Charles Chan77cdde42018-05-08 21:35:50 -0700150import java.util.concurrent.ExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800151import java.util.concurrent.Executors;
152import java.util.concurrent.ScheduledExecutorService;
pierventrea3989be2021-01-08 16:43:17 +0100153import java.util.concurrent.ScheduledFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800154import java.util.concurrent.TimeUnit;
155import java.util.concurrent.atomic.AtomicBoolean;
156import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700157
Charles Chand6d25332016-02-26 22:19:52 -0800158import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800159import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700160import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800161import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
162import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700163import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
psneha86e60d32019-03-26 06:31:41 -0400170import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_SIMPLIFICATION;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700171import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700172import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700173import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
174import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
175import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
psneha86e60d32019-03-26 06:31:41 -0400176import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_SIMPLIFICATION_DEFAULT;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700177import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700178import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chand6d25332016-02-26 22:19:52 -0800179
Charles Chanb7f75ac2016-01-11 18:28:54 -0800180/**
181 * Segment routing manager.
182 */
Ray Milkey05edbfc2018-10-23 14:23:16 -0700183@Component(
184 immediate = true,
185 service = SegmentRoutingService.class,
186 property = {
187 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
188 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
189 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
190 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
191 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
192 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
psneha86e60d32019-03-26 06:31:41 -0400193 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT,
194 PROP_ROUTE_SIMPLIFICATION + ":Boolean=" + ROUTE_SIMPLIFICATION_DEFAULT
Ray Milkey05edbfc2018-10-23 14:23:16 -0700195 }
196)
sangho27462c62015-05-14 00:39:53 -0700197public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700198
Charles Chan46fdfaf2016-11-09 20:51:44 -0800199 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sangho80f11cb2015-04-01 13:05:26 -0700200
Ray Milkey2bd24a92018-08-17 14:54:17 -0700201 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700202 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700203
Ray Milkey2bd24a92018-08-17 14:54:17 -0700204 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chancf789822019-03-22 10:04:27 -0700205 public NeighbourResolutionService neighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800206
Ray Milkey2bd24a92018-08-17 14:54:17 -0700207 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100208 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700209
Ray Milkey2bd24a92018-08-17 14:54:17 -0700210 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700211 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700212
Ray Milkey2bd24a92018-08-17 14:54:17 -0700213 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700214 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700215
Ray Milkey2bd24a92018-08-17 14:54:17 -0700216 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanc6bcdf92018-06-01 16:33:48 -0700217 HostProbingService probingService;
Charles Chan873661e2017-11-30 15:37:50 -0800218
Ray Milkey2bd24a92018-08-17 14:54:17 -0700219 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100220 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700221
Ray Milkey2bd24a92018-08-17 14:54:17 -0700222 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800223 DeviceAdminService deviceAdminService;
224
Ray Milkey2bd24a92018-08-17 14:54:17 -0700225 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800226 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700227
Ray Milkey2bd24a92018-08-17 14:54:17 -0700228 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100229 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700230
Ray Milkey2bd24a92018-08-17 14:54:17 -0700231 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800232 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700233
Ray Milkey2bd24a92018-08-17 14:54:17 -0700234 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800235 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700236
Ray Milkey2bd24a92018-08-17 14:54:17 -0700237 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100238 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700239
Ray Milkey2bd24a92018-08-17 14:54:17 -0700240 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan83163812018-01-09 13:45:07 -0800241 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700242
Ray Milkey2bd24a92018-08-17 14:54:17 -0700243 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan19b70032019-04-17 14:20:26 -0700244 public RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800245
Ray Milkey2bd24a92018-08-17 14:54:17 -0700246 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800247 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800248
Ray Milkey2bd24a92018-08-17 14:54:17 -0700249 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800250 public InterfaceService interfaceService;
251
Ray Milkey2bd24a92018-08-17 14:54:17 -0700252 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi580fd8a2018-01-16 10:47:50 +0100253 public ClusterService clusterService;
254
Ray Milkey2bd24a92018-08-17 14:54:17 -0700255 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pierdb27b8d2018-04-17 16:29:56 +0200256 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100257
Charles Chanb4f9cd12019-02-27 11:46:32 -0800258 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
Charles Chanb4f9cd12019-02-27 11:46:32 -0800259 policy = ReferencePolicy.DYNAMIC)
Carmelo Cascone61004262020-01-16 13:36:02 -0800260 public volatile XconnectService xconnectService;
Charles Chan8d316332018-06-19 20:31:57 -0700261
Charles Chan12a8a842020-02-14 13:23:57 -0800262 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
263 policy = ReferencePolicy.DYNAMIC)
264 volatile PhasedRecoveryService phasedRecoveryService;
265
Ray Milkey05edbfc2018-10-23 14:23:16 -0700266 /** Enable active probing to discover dual-homed hosts. */
267 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan873661e2017-11-30 15:37:50 -0800268
Ray Milkeye96d0de2018-11-02 17:12:21 -0700269 /** Enable only send probe on the same port number of the pair device. */
270 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwarif7942402018-10-29 18:27:35 -0400271
Ray Milkey05edbfc2018-10-23 14:23:16 -0700272 /** Enable administratively taking down single-homed hosts. */
273 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Dasec683dc2018-04-27 18:42:30 -0700274
Ray Milkey05edbfc2018-10-23 14:23:16 -0700275 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
276 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700277
Ray Milkey05edbfc2018-10-23 14:23:16 -0700278 /** Program flows and groups to pop and route double tagged hosts. */
279 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan4eb73bb2018-07-19 14:55:31 -0700280
Ray Milkey05edbfc2018-10-23 14:23:16 -0700281 /** internal vlan assigned by default to unconfigured ports. */
282 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700283
Ray Milkey05edbfc2018-10-23 14:23:16 -0700284 /** vlan used for transport of pseudowires between switches. */
285 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700286
psneha86e60d32019-03-26 06:31:41 -0400287 /** Enabling route simplification. */
288 boolean routeSimplification = ROUTE_SIMPLIFICATION_DEFAULT;
289
Charles Chandebfea32016-10-24 14:52:01 -0700290 ArpHandler arpHandler = null;
291 IcmpHandler icmpHandler = null;
292 IpHandler ipHandler = null;
293 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700294 ApplicationId appId;
295 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700296
Charles Chandebfea32016-10-24 14:52:01 -0700297 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700298 private TunnelHandler tunnelHandler = null;
299 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700300 private InternalPacketProcessor processor = null;
301 private InternalLinkListener linkListener = null;
302 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700303 private AppConfigHandler appCfgHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800304 McastHandler mcastHandler = null;
305 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800306 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800307 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800308 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800309 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100310 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800311 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700312 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
313 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700314 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100315 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700316 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700317 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200318 //Completable future for network configuration process to buffer config events handling during activation
319 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkey521e4392018-11-16 15:15:14 -0800320 private final Object networkConfigCompletionLock = new Object();
Charles Chan39b75522018-04-21 00:44:29 -0700321 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700322
Charles Chan52003922018-04-05 16:31:15 -0700323 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700324 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700325
Charles Chanfbcb8812018-04-18 18:41:05 -0700326 // Handles host, route and mcast events respectively
327 private ScheduledExecutorService hostEventExecutor;
328 private ScheduledExecutorService routeEventExecutor;
329 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700330 private ExecutorService packetExecutor;
Charles Chana7601c92019-01-23 15:03:17 -0800331 ExecutorService neighborExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700332
333 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800334 /**
Saurav Das261c3002017-06-13 15:35:54 -0700335 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700336 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800337 */
Charles Chan873661e2017-11-30 15:37:50 -0800338 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700339 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800340 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700341 * Per device next objective ID store with (device id + vlanid) as key.
342 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800343 */
Charles Chan873661e2017-11-30 15:37:50 -0800344 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800345 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800346 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700347 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000348 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800349 */
Charles Chan873661e2017-11-30 15:37:50 -0800350 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800351 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800352
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700353 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000354 * Per device next objective ID store with (device id + MAC address + vlan) as key.
355 * Used to keep track of L3 unicast group for indirect hosts.
356 */
357 private EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
358 macVlanNextObjStore = null;
359
Saurav Das2d94d312015-11-24 23:21:05 -0800360 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
361 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700362
Saurav Das261c3002017-06-13 15:35:54 -0700363 private AtomicBoolean programmingScheduled = new AtomicBoolean();
364
Charles Chanc91c8782016-03-30 17:54:24 -0700365 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700366 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
367 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700368 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700369 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800370 public SegmentRoutingDeviceConfig createConfig() {
371 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700372 }
373 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800374
Charles Chanc91c8782016-03-30 17:54:24 -0700375 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700376 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
377 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700378 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800379 @Override
380 public SegmentRoutingAppConfig createConfig() {
381 return new SegmentRoutingAppConfig();
382 }
383 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800384
Charles Chanc91c8782016-03-30 17:54:24 -0700385 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700386 new ConfigFactory<ApplicationId, McastConfig>(
387 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700388 McastConfig.class, "multicast") {
389 @Override
390 public McastConfig createConfig() {
391 return new McastConfig();
392 }
393 };
394
Charles Chan1963f4f2016-02-18 14:22:42 -0800395 /**
396 * Segment Routing App ID.
397 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800398 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700399
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700400 /**
401 * Minumum and maximum value of dummy VLAN ID to be allocated.
402 */
403 public static final int MIN_DUMMY_VLAN_ID = 2;
404 public static final int MAX_DUMMY_VLAN_ID = 4093;
405
Charles Chana7601c92019-01-23 15:03:17 -0800406 private static final int DEFAULT_POOL_SIZE = 32;
407
Saurav Dasec683dc2018-04-27 18:42:30 -0700408 Instant lastEdgePortEvent = Instant.EPOCH;
409
sangho80f11cb2015-04-01 13:05:26 -0700410 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800411 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800412 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700413
Charles Chana7601c92019-01-23 15:03:17 -0800414 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
415 groupedThreads("onos/sr", "event-main-%d", log));
416 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
417 groupedThreads("onos/sr", "event-host-%d", log));
418 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
419 groupedThreads("onos/sr", "event-route-%d", log));
420 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
421 groupedThreads("onos/sr", "event-mcast-%d", log));
422 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
423 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
424 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700425
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700426 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700427 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700428 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700429 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700430 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700431 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700432 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700433 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700434 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700435
Charles Chan10b0fb72017-02-02 16:20:42 -0800436 log.debug("Creating EC map vlannextobjectivestore");
437 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
438 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
439 vlanNextObjStore = vlanNextObjMapBuilder
440 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700441 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700442 .withTimestampProvider((k, v) -> new WallClockTimestamp())
443 .build();
444
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000445 log.debug("Creating EC map macvlannextobjectivestore");
446 EventuallyConsistentMapBuilder<MacVlanNextObjectiveStoreKey, Integer>
447 macVlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
448 macVlanNextObjStore = macVlanNextObjMapBuilder
449 .withName("macvlannextobjectivestore")
450 .withSerializer(createSerializer())
451 .withTimestampProvider((k, v) -> new WallClockTimestamp())
452 .build();
453
Saurav Das2d94d312015-11-24 23:21:05 -0800454 log.debug("Creating EC map subnetnextobjectivestore");
455 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
456 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
457 portNextObjStore = portNextObjMapBuilder
458 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700459 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800460 .withTimestampProvider((k, v) -> new WallClockTimestamp())
461 .build();
462
sangho4a5c42a2015-05-20 22:16:38 -0700463 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
464 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700465 tunnelStore = tunnelMapBuilder
466 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700467 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700468 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700469 .build();
470
471 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
472 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700473 policyStore = policyMapBuilder
474 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700475 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700476 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700477 .build();
478
Charles Chan97979802020-02-26 15:04:44 -0800479 processor = new InternalPacketProcessor();
480 linkListener = new InternalLinkListener();
481 deviceListener = new InternalDeviceListener();
482 appCfgHandler = new AppConfigHandler(this);
483 mcastHandler = new McastHandler(this);
484 hostHandler = new HostHandler(this);
485 linkHandler = new LinkHandler(this);
486 routeHandler = new RouteHandler(this);
487 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
488 l2TunnelHandler = new DefaultL2TunnelHandler(this);
489 topologyHandler = new TopologyHandler(this);
490
Pier Luigib9632ba2017-01-12 18:14:58 -0800491 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700492 "requestInterceptsEnabled", "false", false);
Charles Chanc1909e12017-08-08 15:13:37 -0700493 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700494 "requestInterceptsEnabled", "false", false);
Charles Chan9597f8d2017-07-24 15:56:10 -0700495 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700496 "arpEnabled", "false", false);
Pier Luigi0ebeb312017-02-02 22:31:34 -0800497 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700498 "greedyLearningIpv6", "true", false);
Charles Chancf8ea472017-02-28 15:15:17 -0800499 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700500 "forceUnprovision", "true", false);
Charles Chanc7b73c72017-08-10 16:57:28 -0700501 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan0fa51db2019-04-10 17:12:20 -0700502 "distributed", "true", false);
Charles Chan804772f2017-08-14 11:42:11 -0700503 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700504 "multihomingEnabled", "true", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800505 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700506 "staleLinkAge", "15000", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800507 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700508 "allowDuplicateIps", "false", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800509 // For P4 switches
510 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700511 "fallbackFlowPollFrequency", "4", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800512 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700513 "fallbackGroupPollFrequency", "3", false);
pierventre4582cf12021-09-14 20:42:59 +0200514 compCfgService.preSetProperty("org.onosproject.net.meter.impl.MeterManager",
515 "fallbackMeterPollFrequency", "3", false);
Charles Chan873661e2017-11-30 15:37:50 -0800516 compCfgService.registerProperties(getClass());
517 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700518
Charles Chand6d25332016-02-26 22:19:52 -0800519 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700520 cfgService.registerConfigFactory(deviceConfigFactory);
521 cfgService.registerConfigFactory(appConfigFactory);
522 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800523 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200524
Charles Chan6961f222018-01-04 14:26:07 -0800525 cfgListener.configureNetwork();
526
Charles Chan82ab1932016-01-30 23:22:37 -0800527 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700528 packetService.addProcessor(processor, PacketProcessor.director(2));
529 linkService.addListener(linkListener);
530 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700531 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700532 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100533 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700534 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700535 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700536
Charles Chanc12c3d02018-03-09 15:53:44 -0800537 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800538 l2TunnelHandler.init();
539
pierventrec80a5052021-05-20 12:56:38 +0200540 drainEvents();
Andrea Campanella060cad82018-04-17 12:09:34 +0200541
sangho80f11cb2015-04-01 13:05:26 -0700542 log.info("Started");
543 }
544
pierventrec80a5052021-05-20 12:56:38 +0200545 // Drain buffered events
546 private void drainEvents() {
547 synchronized (networkConfigCompletionLock) {
548 final List<Event> processedEvents = Lists.newArrayList();
549 if (networkConfigCompletion != null) {
550 networkConfigCompletion.whenComplete((value, ex) -> {
551 //setting to null for easier fall through
552 networkConfigCompletion = null;
553 //process all queued events
554 queuedEvents.forEach(event -> {
555 mainEventExecutor.execute(new InternalEventHandler(event));
556 processedEvents.add(event);
557 });
558 // Removes all
559 queuedEvents.removeAll(processedEvents);
560 processedEvents.clear();
561 });
562 }
563 }
564 }
565
Saurav Dase6c448a2018-01-18 12:07:33 -0800566 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700567 return new KryoNamespace.Builder()
568 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700569 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800570 VlanNextObjectiveStoreKey.class,
571 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700572 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800573 NextNeighbors.class,
574 Tunnel.class,
575 DefaultTunnel.class,
576 Policy.class,
577 TunnelPolicy.class,
578 Policy.Type.class,
579 PortNextObjectiveStoreKey.class,
580 XConnectStoreKey.class,
581 L2Tunnel.class,
582 L2TunnelPolicy.class,
583 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700584 DefaultL2TunnelPolicy.class,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000585 MacVlanNextObjectiveStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700586 );
587 }
588
sangho80f11cb2015-04-01 13:05:26 -0700589 @Deactivate
590 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700591 mainEventExecutor.shutdown();
592 hostEventExecutor.shutdown();
593 routeEventExecutor.shutdown();
594 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700595 packetExecutor.shutdown();
Charles Chana7601c92019-01-23 15:03:17 -0800596 neighborExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700597
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700598 mainEventExecutor = null;
599 hostEventExecutor = null;
600 routeEventExecutor = null;
601 mcastEventExecutor = null;
602 packetExecutor = null;
Charles Chana7601c92019-01-23 15:03:17 -0800603 neighborExecutor = null;
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700604
Danny Chang44a7b952020-09-08 15:27:04 -0700605 cancelPackets();
606
Charles Chan72f556a2015-10-05 17:50:33 -0700607 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700608 cfgService.unregisterConfigFactory(deviceConfigFactory);
609 cfgService.unregisterConfigFactory(appConfigFactory);
610 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800611 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700612
Charles Chanfd48c222017-06-19 00:43:31 -0700613 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700614 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700615 linkService.removeListener(linkListener);
616 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700617 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700618 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100619 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700620 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700621 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700622
Charles Chan2e71ef32017-02-23 15:44:08 -0800623 neighbourResolutionService.unregisterNeighbourHandlers(appId);
624
sangho80f11cb2015-04-01 13:05:26 -0700625 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700626 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700627 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700628 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700629 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700630 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700631
Saurav Das261c3002017-06-13 15:35:54 -0700632 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800633 vlanNextObjStore.destroy();
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000634 macVlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700635 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700636 tunnelStore.destroy();
637 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100638
639 mcastHandler.terminate();
piercf557922019-05-17 20:47:06 +0200640 hostHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700641 log.info("Stopped");
642 }
643
Charles Chan873661e2017-11-30 15:37:50 -0800644 @Modified
645 private void modified(ComponentContext context) {
646 Dictionary<?, ?> properties = context.getProperties();
647 if (properties == null) {
648 return;
649 }
650
Ray Milkey48d10c02018-10-24 10:04:03 -0700651 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700652 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800653 if (expectActiveProbing != activeProbing) {
654 activeProbing = expectActiveProbing;
655 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
656 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700657
Ray Milkeye96d0de2018-11-02 17:12:21 -0700658
659 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
660 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwarif7942402018-10-29 18:27:35 -0400661 if (expectSymmetricProbing != symmetricProbing) {
662 symmetricProbing = expectSymmetricProbing;
663 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
664 }
665
Ray Milkey48d10c02018-10-24 10:04:03 -0700666 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700667 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
668 if (expectSingleHomedDown != singleHomedDown) {
669 singleHomedDown = expectSingleHomedDown;
670 log.info("{} downing of single homed hosts for lost uplinks",
671 singleHomedDown ? "Enabling" : "Disabling");
672 if (singleHomedDown && linkHandler != null) {
673 hostService.getHosts().forEach(host -> host.locations()
674 .forEach(loc -> {
675 if (interfaceService.isConfigured(loc)) {
676 linkHandler.checkUplinksForHost(loc);
677 }
678 }));
679 } else {
680 log.warn("Disabling singleHomedDown does not re-enable already "
681 + "downed ports for single-homed hosts");
682 }
683 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700684
Ray Milkey48d10c02018-10-24 10:04:03 -0700685 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700686 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
687 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
688 respondToUnknownHosts = expectRespondToUnknownHosts;
689 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
690 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700691
Ray Milkey48d10c02018-10-24 10:04:03 -0700692 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700693 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
694 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
695 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
696 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700697
Charles Chanfbca55e2018-07-24 16:40:35 -0700698 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700699 hostHandler.populateAllDoubleTaggedHost();
700 } else {
701 hostHandler.revokeAllDoubleTaggedHost();
702 }
703 }
Saurav Das9bf49582018-08-13 15:34:26 -0700704
Ray Milkey48d10c02018-10-24 10:04:03 -0700705 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700706 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
707 if (defIntVlan != defaultInternalVlan) {
708 if (canUseVlanId(defIntVlan)) {
709 log.warn("Default internal vlan value changed from {} to {}.. "
710 + "re-programming filtering rules, but NOT any groups already "
711 + "created with the former value", defaultInternalVlan, defIntVlan);
712 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
713 defaultInternalVlan = defIntVlan;
714 routingRulePopulator
715 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
716 VlanId.vlanId((short) defIntVlan));
717 } else {
718 log.warn("Cannot change default internal vlan to unusable "
719 + "value {}", defIntVlan);
720 }
721 }
722
Ray Milkey48d10c02018-10-24 10:04:03 -0700723 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700724 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
725 if (pwTxpVlan != pwTransportVlan) {
726 if (canUseVlanId(pwTxpVlan)) {
727 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
728 + "re-programming filtering rules, but NOT any groups already "
729 + "created with the former value", pwTransportVlan,
730 pwTxpVlan);
731 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
732 pwTransportVlan = pwTxpVlan;
733 routingRulePopulator
734 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
735 VlanId.vlanId((short) pwTxpVlan));
736 } else {
737 log.warn("Cannot change pseudowire transport vlan to unusable "
738 + "value {}", pwTxpVlan);
739 }
740 }
741
psneha86e60d32019-03-26 06:31:41 -0400742 String strRouteSimplification = Tools.get(properties, PROP_ROUTE_SIMPLIFICATION);
743 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
744 if (expectRouteSimplification != routeSimplification) {
745 routeSimplification = expectRouteSimplification;
746 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
747 }
748
Saurav Das9bf49582018-08-13 15:34:26 -0700749 }
750
751 /**
Danny Chang44a7b952020-09-08 15:27:04 -0700752 * Removes all packet requests.
753 */
754 private void cancelPackets() {
755 if (routingRulePopulator == null) {
756 return;
757 }
758 log.info("Removing packet requests.");
759 routingRulePopulator.revokePacketsPunts();
760 }
761
762 /**
Saurav Das9bf49582018-08-13 15:34:26 -0700763 * Returns true if given vlan id is not being used in the system currently,
764 * either as one of the default system wide vlans or as one of the
765 * configured interface vlans.
766 *
767 * @param vlanId given vlan id
768 * @return true if vlan is not currently in use
769 */
770 public boolean canUseVlanId(int vlanId) {
771 if (vlanId >= 4095 || vlanId <= 1) {
772 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
773 vlanId);
774 return false;
775 }
776
777 VlanId vid = VlanId.vlanId((short) vlanId);
778 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
779 log.warn("Vlan id {} value is already in use system-wide. "
780 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
781 getDefaultInternalVlan(), getPwTransportVlan());
782 return false;
783 }
784
785 if (interfaceService.inUse(vid)) {
786 log.warn("Vlan id {} value is already in use on a configured "
787 + "interface in the system", vlanId);
788 return false;
789 }
790 return true;
791 }
792
pierventre4f68ffa2021-03-09 22:52:14 +0100793 @Override
Saurav Das9bf49582018-08-13 15:34:26 -0700794 public VlanId getDefaultInternalVlan() {
795 return VlanId.vlanId((short) defaultInternalVlan);
796 }
797
798 /**
799 * Returns the Vlan id used to transport pseudowire traffic across the
800 * network.
801 *
802 * @return the pseudowire transport vlan id
803 */
804 public VlanId getPwTransportVlan() {
805 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800806 }
807
sangho27462c62015-05-14 00:39:53 -0700808 @Override
809 public List<Tunnel> getTunnels() {
810 return tunnelHandler.getTunnels();
811 }
812
813 @Override
sanghobd812f82015-06-29 14:58:47 -0700814 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
815 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700816 }
817
818 @Override
sanghobd812f82015-06-29 14:58:47 -0700819 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700820 for (Policy policy: policyHandler.getPolicies()) {
821 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
822 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
823 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
824 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700825 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700826 }
827 }
828 }
sanghobd812f82015-06-29 14:58:47 -0700829 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700830 }
831
832 @Override
sanghobd812f82015-06-29 14:58:47 -0700833 public PolicyHandler.Result removePolicy(Policy policy) {
834 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700835 }
836
837 @Override
sanghobd812f82015-06-29 14:58:47 -0700838 public PolicyHandler.Result createPolicy(Policy policy) {
839 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700840 }
841
842 @Override
843 public List<Policy> getPolicies() {
844 return policyHandler.getPolicies();
845 }
846
Saurav Das07c74602016-04-27 18:35:50 -0700847 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700848 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
849 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700850 }
851
852 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800853 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700854 return l2TunnelHandler.getL2Tunnels();
855 }
856
857 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800858 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700859 return l2TunnelHandler.getL2Policies();
860 }
861
Saurav Dasdebcf882018-04-06 20:16:01 -0700862 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700863 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700864 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
865
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700866 // get both added and pending pseudowires
867 List<L2TunnelDescription> pseudowires = new ArrayList<>();
868 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
869 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700870 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700871
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700872 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
873
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700874 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
875 L2TunnelHandler.Result res;
876 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
877 res = addPseudowire(pw);
878 if (res != L2TunnelHandler.Result.SUCCESS) {
879 log.error("Pseudowire with id {} can not be instantiated !", res);
880 retRes = res;
881 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700882 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700883
884 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700885 }
886
887 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800888 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700889 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700890 }
891
892 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800893 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700894 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700895 }
896
897 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700898 public void rerouteNetwork() {
pierventrec80a5052021-05-20 12:56:38 +0200899 cfgListener.configureNetworkAndDrainEvents();
Saurav Das07c74602016-04-27 18:35:50 -0700900 }
901
Charles Chand7844e52016-10-20 17:02:44 -0700902 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800903 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
904 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800905 deviceConfiguration.getRouters().forEach(device ->
906 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700907 return deviceSubnetMap;
908 }
909
Saurav Das62ae6792017-05-15 15:34:25 -0700910
911 @Override
912 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
913 if (defaultRoutingHandler != null) {
914 return defaultRoutingHandler.getCurrentEmcpSpgMap();
915 } else {
916 return null;
917 }
918 }
919
920 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700921 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700922 if (dsNextObjStore != null) {
923 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700924 } else {
925 return ImmutableMap.of();
926 }
927 }
928
Saurav Dasfbe74572017-08-03 18:30:35 -0700929 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700930 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
931 if (vlanNextObjStore != null) {
932 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
933 } else {
934 return ImmutableMap.of();
935 }
936 }
937
938 @Override
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000939 public ImmutableMap<MacVlanNextObjectiveStoreKey, Integer> getMacVlanNextObjStore() {
940 if (macVlanNextObjStore != null) {
941 return ImmutableMap.copyOf(macVlanNextObjStore.entrySet());
942 } else {
943 return ImmutableMap.of();
944 }
945 }
946
947 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700948 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
949 if (portNextObjStore != null) {
950 return ImmutableMap.copyOf(portNextObjStore.entrySet());
951 } else {
952 return ImmutableMap.of();
953 }
954 }
955
956 @Override
957 public ImmutableMap<String, NextObjective> getPwInitNext() {
958 if (l2TunnelHandler != null) {
959 return l2TunnelHandler.getInitNext();
960 } else {
961 return ImmutableMap.of();
962 }
963 }
964
965 @Override
966 public ImmutableMap<String, NextObjective> getPwTermNext() {
967 if (l2TunnelHandler != null) {
968 return l2TunnelHandler.getTermNext();
969 } else {
970 return ImmutableMap.of();
971 }
972 }
973
974 @Override
975 public void invalidateNextObj(int nextId) {
976 if (dsNextObjStore != null) {
977 dsNextObjStore.entrySet().forEach(e -> {
978 if (e.getValue().nextId() == nextId) {
979 dsNextObjStore.remove(e.getKey());
980 }
981 });
982 }
983 if (vlanNextObjStore != null) {
984 vlanNextObjStore.entrySet().forEach(e -> {
985 if (e.getValue() == nextId) {
986 vlanNextObjStore.remove(e.getKey());
987 }
988 });
989 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000990 if (macVlanNextObjStore != null) {
991 macVlanNextObjStore.entrySet().forEach(e -> {
992 if (e.getValue() == nextId) {
993 macVlanNextObjStore.remove(e.getKey());
994 }
995 });
996 }
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700997 if (portNextObjStore != null) {
998 portNextObjStore.entrySet().forEach(e -> {
999 if (e.getValue() == nextId) {
1000 portNextObjStore.remove(e.getKey());
1001 }
1002 });
1003 }
1004 if (mcastHandler != null) {
1005 mcastHandler.removeNextId(nextId);
1006 }
1007 if (l2TunnelHandler != null) {
1008 l2TunnelHandler.removeNextId(nextId);
1009 }
1010 if (xconnectService != null) {
1011 xconnectService.removeNextId(nextId);
1012 }
1013 }
1014
1015 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -07001016 public void verifyGroups(DeviceId id) {
1017 DefaultGroupHandler gh = groupHandlerMap.get(id);
1018 if (gh != null) {
1019 gh.triggerBucketCorrector();
1020 }
1021 }
1022
Saurav Das6430f412018-01-25 09:49:01 -08001023 @Override
1024 public ImmutableMap<Link, Boolean> getSeenLinks() {
1025 return linkHandler.getSeenLinks();
1026 }
1027
1028 @Override
1029 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1030 return linkHandler.getDownedPorts();
1031 }
1032
Pier Luigi0f9635b2018-01-15 18:06:43 +01001033 @Override
1034 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -07001035 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +01001036 }
1037
1038 @Override
Piere99511d2018-04-19 16:47:06 +02001039 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1040 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1041 }
1042
1043 @Override
Pier71c55772018-04-17 17:25:22 +02001044 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1045 ConnectPoint sourcecp) {
1046 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1047 }
1048
1049 @Override
Pierdb27b8d2018-04-17 16:29:56 +02001050 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1051 return mcastHandler.getMcastLeaders(mcastIp);
1052 }
1053
Charles Chanb13e0702018-04-17 18:56:53 -07001054 @Override
Harshada Chaundkar9204f312019-07-02 16:01:24 +00001055 public Map<DeviceId, List<McastFilteringObjStoreKey>> getMcastFilters() {
1056 return mcastHandler.getMcastFilters();
1057 }
1058
1059 @Override
Charles Chanb13e0702018-04-17 18:56:53 -07001060 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
pierventre37dcf4c2021-09-16 18:43:06 +02001061 return ImmutableMap.of();
1062 }
1063
1064 @Override
1065 public Map<DeviceId, Boolean> getShouldProgramCache() {
1066 return ImmutableMap.of();
1067 }
1068
1069 @Override
1070 public Map<DeviceId, NodeId> getShouldProgramLeaders() {
Charles Chanb13e0702018-04-17 18:56:53 -07001071 return defaultRoutingHandler == null ? ImmutableMap.of() :
1072 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1073 }
1074
1075 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301076 public boolean shouldProgram(DeviceId deviceId) {
pierventre37dcf4c2021-09-16 18:43:06 +02001077 return defaultRoutingHandler != null && defaultRoutingHandler.shouldProgram(deviceId);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301078 }
1079
1080 @Override
Charles Chan12a8a842020-02-14 13:23:57 -08001081 public boolean isRoutingStable() {
1082 return defaultRoutingHandler.isRoutingStable();
1083 }
1084
1085 @Override
1086 public void initHost(DeviceId deviceId) {
1087 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1088 }
1089
1090 @Override
1091 public void initRoute(DeviceId deviceId) {
1092 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
1093 }
1094
1095 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001096 public ApplicationId appId() {
1097 return appId;
1098 }
1099
1100 /**
1101 * Returns the device configuration.
1102 *
1103 * @return device configuration
1104 */
1105 public DeviceConfiguration deviceConfiguration() {
1106 return deviceConfiguration;
1107 }
1108
1109 /**
Saurav Das261c3002017-06-13 15:35:54 -07001110 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001111 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001112 *
1113 * @return next objective ID store
1114 */
Saurav Das261c3002017-06-13 15:35:54 -07001115 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1116 dsNextObjStore() {
1117 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001118 }
1119
1120 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001121 * Per device next objective ID store with (device id + vlanid) as key.
1122 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001123 *
1124 * @return vlan next object store
1125 */
1126 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1127 return vlanNextObjStore;
1128 }
1129
1130 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001131 * Per device next objective ID store with (device id + MAC address + vlan) as key.
1132 * Used to keep track on L3 Unicast group information for indirect hosts.
1133 *
1134 * @return mac vlan next object store
1135 */
1136 public EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> macVlanNextObjStore() {
1137 return macVlanNextObjStore;
1138 }
1139
1140 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001141 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001142 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Ray Milkeyb85de082017-04-05 09:42:04 -07001143 *
1144 * @return port next object store.
1145 */
1146 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1147 return portNextObjStore;
1148 }
1149
1150 /**
Saurav Das261c3002017-06-13 15:35:54 -07001151 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1152 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001153 *
1154 * @return MPLS-ECMP value
1155 */
1156 public boolean getMplsEcmp() {
1157 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1158 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1159 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1160 }
1161
1162 /**
sangho80f1f892015-05-19 11:57:42 -07001163 * Returns the tunnel object with the tunnel ID.
1164 *
1165 * @param tunnelId Tunnel ID
1166 * @return Tunnel reference
1167 */
sangho27462c62015-05-14 00:39:53 -07001168 public Tunnel getTunnel(String tunnelId) {
1169 return tunnelHandler.getTunnel(tunnelId);
1170 }
1171
Charles Chan8d316332018-06-19 20:31:57 -07001172 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001173 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001174 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1175 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001176 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1177 }
1178
Charles Chan8d316332018-06-19 20:31:57 -07001179 @Override
1180 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001181 SegmentRoutingDeviceConfig deviceConfig =
1182 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1183 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1184 }
Charles Chan8d316332018-06-19 20:31:57 -07001185
1186 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001187 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001188 SegmentRoutingDeviceConfig deviceConfig =
1189 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1190 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1191 }
1192
Charles Chan12a8a842020-02-14 13:23:57 -08001193 @Override
1194 public Set<PortNumber> getInfraPorts(DeviceId deviceId) {
1195 return deviceService.getPorts(deviceId).stream()
1196 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1197 .filter(cp -> interfaceService.getInterfacesByPort(cp).isEmpty())
1198 .map(ConnectPoint::port)
1199 .collect(Collectors.toSet());
1200 }
1201
1202 @Override
1203 public Set<PortNumber> getEdgePorts(DeviceId deviceId) {
1204 return deviceService.getPorts(deviceId).stream()
1205 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1206 .filter(cp -> !interfaceService.getInterfacesByPort(cp).isEmpty() &&
1207 !cp.port().equals(getPairLocalPort(deviceId).orElse(null)))
1208 .map(ConnectPoint::port)
1209 .collect(Collectors.toSet());
1210 }
1211
pierventre30368ab2021-02-24 23:23:22 +01001212 @Override
1213 public List<DeviceId> getEdgeDeviceIds() {
1214 return deviceConfiguration.getEdgeDeviceIds();
1215 }
1216
pierventre4f68ffa2021-03-09 22:52:14 +01001217 @Override
Charles Chana2401452021-06-03 15:57:48 -07001218 public List<DeviceId> getInfraDeviceIds() {
1219 return deviceConfiguration.getInfraDeviceIds();
1220 }
1221
1222 @Override
pierventre4f68ffa2021-03-09 22:52:14 +01001223 public MacAddress getDeviceMacAddress(DeviceId deviceId) throws DeviceConfigNotFoundException {
1224 return deviceConfiguration.getDeviceMac(deviceId);
1225 }
1226
Charles Chan3ed34d82017-06-22 18:03:14 -07001227 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001228 * Returns locations of given resolved route.
1229 *
1230 * @param resolvedRoute resolved route
1231 * @return locations of nexthop. Might be empty if next hop is not found
1232 */
Charles Chan19b70032019-04-17 14:20:26 -07001233 public Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
Charles Chanf0ae41e2017-08-23 13:55:39 -07001234 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1235 return Optional.ofNullable(hostService.getHost(hostId))
1236 .map(Host::locations).orElse(Sets.newHashSet())
1237 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1238 }
1239
1240 /**
Charles Chan90772a72017-02-08 15:52:08 -08001241 * Returns vlan port map of given device.
1242 *
1243 * @param deviceId device id
1244 * @return vlan-port multimap
1245 */
1246 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1247 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1248
1249 interfaceService.getInterfaces().stream()
1250 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1251 .forEach(intf -> {
1252 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001253 intf.vlanTagged().forEach(vlanTagged ->
1254 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1255 );
Charles Chan90772a72017-02-08 15:52:08 -08001256 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1257 });
1258 vlanPortMap.removeAll(VlanId.NONE);
1259
1260 return vlanPortMap;
1261 }
1262
1263 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001264 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001265 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001266 *
1267 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001268 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001269 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001270 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001271 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001272 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001273 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1274 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001275 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001276 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001277 + "device {} not found", deviceId);
1278 return -1;
1279 }
1280 }
1281
1282 /**
1283 * Returns the next objective ID for the given portNumber, given the treatment.
1284 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001285 * would result in different objectives. If the next object does not exist,
1286 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001287 *
1288 * @param deviceId Device ID
1289 * @param portNum port number on device for which NextObjective is queried
1290 * @param treatment the actions to apply on the packets (should include outport)
1291 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001292 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001293 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001294 */
1295 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1296 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001297 TrafficSelector meta,
1298 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001299 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1300 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001301 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001302 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001303 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1304 + " not found", deviceId);
1305 return -1;
1306 }
1307 }
1308
Saurav Das62ae6792017-05-15 15:34:25 -07001309 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001310 * Returns the next Objective ID for the given mac and vlan, given the treatment.
1311 * There could be multiple different treatments to the same outport, which
1312 * would result in different objectives. If the next object does not exist,
1313 * and should be created, a new one is created and its id is returned.
1314 *
1315 * @param deviceId Device ID
1316 * @param macAddr mac of host for which Next ID is required.
1317 * @param vlanId vlan of host for which Next ID is required.
Ruchi Sahota07869322019-05-09 17:26:14 -04001318 * @param port port with which to create the Next Obj.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001319 * @param createIfMissing true if a next object should be created if not found
1320 * @return next objective ID or -1 if an error occurred during retrieval or creation
1321 */
1322 public int getMacVlanNextObjectiveId(DeviceId deviceId, MacAddress macAddr, VlanId vlanId,
Ruchi Sahota07869322019-05-09 17:26:14 -04001323 PortNumber port, boolean createIfMissing) {
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001324 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1325 if (ghdlr != null) {
Ruchi Sahota07869322019-05-09 17:26:14 -04001326 return ghdlr.getMacVlanNextObjectiveId(macAddr, vlanId, port, createIfMissing);
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001327 } else {
1328 log.warn("getMacVlanNextObjectiveId query - groupHandler for device {}"
1329 + " not found", deviceId);
1330 return -1;
1331 }
1332 }
1333
1334 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001335 * Updates the next objective for the given nextId .
1336 *
1337 * @param deviceId Device ID
1338 * @param hostMac mac of host for which Next obj is to be updated.
1339 * @param hostVlanId vlan of host for which Next obj is to be updated.
1340 * @param port port with which to update the Next Obj.
1341 * @param nextId of Next Obj which needs to be updated.
1342 */
1343 public void updateMacVlanTreatment(DeviceId deviceId, MacAddress hostMac,
pierventrea3989be2021-01-08 16:43:17 +01001344 VlanId hostVlanId, PortNumber port, int nextId) {
pierventre37dcf4c2021-09-16 18:43:06 +02001345 // Check if we are the leader of this device
pierf0e37ec2019-11-26 11:48:48 +01001346 // just one instance should perform this update
1347 if (!defaultRoutingHandler.shouldProgram(deviceId)) {
1348 log.debug("This instance is not handling the routing towards the "
1349 + "device {}", deviceId);
1350 return;
1351 }
1352 // Get the handler and perform the update
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001353 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1354 if (ghdlr != null) {
1355 ghdlr.updateL3UcastGroupBucket(hostMac, hostVlanId, port, nextId);
1356 } else {
1357 log.warn("updateL3UcastGroupBucket query - groupHandler for device {}"
1358 + " not found", deviceId);
1359 }
1360 }
1361
1362
1363 /**
Saurav Das62ae6792017-05-15 15:34:25 -07001364 * Returns the group handler object for the specified device id.
1365 *
1366 * @param devId the device identifier
1367 * @return the groupHandler object for the device id, or null if not found
1368 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001369 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001370 return groupHandlerMap.get(devId);
1371 }
1372
1373 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001374 * Returns the default routing handler object.
1375 *
1376 * @return the default routing handler object
1377 */
1378 public DefaultRoutingHandler getRoutingHandler() {
1379 return defaultRoutingHandler;
1380 }
1381
sangho80f11cb2015-04-01 13:05:26 -07001382 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001383 @Override
1384 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001385 packetExecutor.execute(() -> processPacketInternal(context));
1386 }
sangho80f11cb2015-04-01 13:05:26 -07001387
Charles Chan77cdde42018-05-08 21:35:50 -07001388 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001389 if (context.isHandled()) {
1390 return;
1391 }
1392
1393 InboundPacket pkt = context.inPacket();
1394 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001395
1396 if (ethernet == null) {
1397 return;
1398 }
1399
Saurav Das261c3002017-06-13 15:35:54 -07001400 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1401 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001402 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001403 log.warn("Received unexpected ARP packet on {}",
1404 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001405 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001406 return;
sangho80f11cb2015-04-01 13:05:26 -07001407 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001408 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1409 //ipHandler.addToPacketBuffer(ipv4Packet);
1410 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1411 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001412 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001413 // NOTE: We don't support IP learning at this moment so this
1414 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001415 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001416 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001417 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1418 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001419 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001420 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001421 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1422 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1423 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1424 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1425 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1426 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001427 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001428 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001429 }
1430 } else {
1431 // NOTE: We don't support IP learning at this moment so this
1432 // is not necessary. Also it causes duplication of DHCPv6 packets.
1433 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1434 }
sangho80f11cb2015-04-01 13:05:26 -07001435 }
1436 }
1437 }
1438
sangho80f11cb2015-04-01 13:05:26 -07001439 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001440 private Event event;
1441
1442 InternalEventHandler(Event event) {
1443 this.event = event;
1444 }
1445
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001446 @Override
sangho80f11cb2015-04-01 13:05:26 -07001447 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001448 try {
1449 // TODO We should also change SR routing and PW to listen to TopologyEvents
1450 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1451 event.type() == LinkEvent.Type.LINK_UPDATED) {
1452 linkHandler.processLinkAdded((Link) event.subject());
1453 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1454 linkHandler.processLinkRemoved((Link) event.subject());
1455 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1456 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1457 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1458 DeviceId deviceId = ((Device) event.subject()).id();
1459 if (deviceService.isAvailable(deviceId)) {
1460 log.info("** DEVICE UP Processing device event {} "
1461 + "for available device {}",
1462 event.type(), ((Device) event.subject()).id());
1463 processDeviceAdded((Device) event.subject());
pierventre37dcf4c2021-09-16 18:43:06 +02001464 /*
1465 * This is a mere heuristic as there is not yet stable mastership in ONOS, and it is based on
1466 * the fact that DEVICE is marked online only if there is a master around. processDeviceAdded
1467 * can be called on config change and link events and there is no check of the availability.
1468 * In this scenarios, we could not have a master and pr is broken as nobody can admin enable
1469 * the ports. We keep in processDeviceAdded the code that is already idempotent
1470 */
1471 if (event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED) {
1472 phasedRecoveryService.init(deviceId);
1473 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001474 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001475 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1476 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1477 // The device will later on being marked as available once the pipeline is pushed
1478 // to the device.
1479 log.info("** DEVICE ADDED but unavailable. Ignore");
1480 return;
1481 }
Charles Chan52003922018-04-05 16:31:15 -07001482 log.info(" ** DEVICE DOWN Processing device event {}"
1483 + " for unavailable device {}",
1484 event.type(), ((Device) event.subject()).id());
1485 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001486 }
Charles Chan52003922018-04-05 16:31:15 -07001487 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1488 // typically these calls come when device is added first time
1489 // so port filtering rules are handled at the device_added event.
1490 // port added calls represent all ports on the device,
1491 // enabled or not.
1492 log.trace("** PORT ADDED {}/{} -> {}",
1493 ((DeviceEvent) event).subject().id(),
1494 ((DeviceEvent) event).port().number(),
1495 event.type());
1496 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1497 // these calls happen for every subsequent event
1498 // ports enabled, disabled, switch goes away, comes back
1499 log.info("** PORT UPDATED {}/{} -> {}",
1500 event.subject(),
1501 ((DeviceEvent) event).port(),
1502 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001503 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001504 ((DeviceEvent) event).port());
Harshada Chaundkar9204f312019-07-02 16:01:24 +00001505 mcastHandler.processPortUpdate(((Device) event.subject()),
1506 ((DeviceEvent) event).port());
Charles Chan52003922018-04-05 16:31:15 -07001507 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1508 // Process topology event, needed for all modules relying on
1509 // topology service for path computation
1510 TopologyEvent topologyEvent = (TopologyEvent) event;
1511 log.info("Processing topology event {}, topology age {}, reasons {}",
1512 event.type(), topologyEvent.subject().time(),
1513 topologyEvent.reasons().size());
1514 topologyHandler.processTopologyChange(topologyEvent.reasons());
1515 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1516 hostHandler.processHostAddedEvent((HostEvent) event);
1517 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1518 hostHandler.processHostMovedEvent((HostEvent) event);
1519 routeHandler.processHostMovedEvent((HostEvent) event);
Charles Chana2ccb582019-11-25 09:47:22 -08001520 } else if (event.type() == HostEvent.Type.HOST_AUX_MOVED) {
1521 hostHandler.processHostMovedEvent((HostEvent) event);
1522 // TODO RouteHandler also needs to process this event in order to
1523 // support nexthops that has auxLocations
Charles Chan52003922018-04-05 16:31:15 -07001524 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1525 hostHandler.processHostRemovedEvent((HostEvent) event);
1526 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1527 hostHandler.processHostUpdatedEvent((HostEvent) event);
1528 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1529 routeHandler.processRouteAdded((RouteEvent) event);
1530 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1531 routeHandler.processRouteUpdated((RouteEvent) event);
1532 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1533 routeHandler.processRouteRemoved((RouteEvent) event);
1534 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1535 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1536 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1537 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1538 event.type() == McastEvent.Type.SINKS_ADDED ||
1539 event.type() == McastEvent.Type.SINKS_REMOVED ||
1540 event.type() == McastEvent.Type.ROUTE_ADDED ||
1541 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1542 mcastHandler.processMcastEvent((McastEvent) event);
1543 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1544 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1545 Class configClass = netcfgEvent.configClass();
1546 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1547 appCfgHandler.processAppConfigAdded(netcfgEvent);
1548 log.info("App config event .. configuring network");
pierventrec80a5052021-05-20 12:56:38 +02001549 cfgListener.configureNetworkAndDrainEvents();
Charles Chan52003922018-04-05 16:31:15 -07001550 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1551 log.info("Segment Routing Device Config added for {}", event.subject());
pierventrec80a5052021-05-20 12:56:38 +02001552 cfgListener.configureNetworkAndDrainEvents();
Charles Chan52003922018-04-05 16:31:15 -07001553 } else if (configClass.equals(InterfaceConfig.class)) {
1554 log.info("Interface Config added for {}", event.subject());
pierventrec80a5052021-05-20 12:56:38 +02001555 cfgListener.configureNetworkAndDrainEvents();
Charles Chan52003922018-04-05 16:31:15 -07001556 } else {
1557 log.error("Unhandled config class: {}", configClass);
1558 }
1559 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1560 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1561 Class configClass = netcfgEvent.configClass();
1562 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1563 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1564 log.info("App config event .. configuring network");
pierventrec80a5052021-05-20 12:56:38 +02001565 cfgListener.configureNetworkAndDrainEvents();
Charles Chan52003922018-04-05 16:31:15 -07001566 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1567 log.info("Segment Routing Device Config updated for {}", event.subject());
1568 createOrUpdateDeviceConfiguration();
Charles Chan52003922018-04-05 16:31:15 -07001569 } else if (configClass.equals(InterfaceConfig.class)) {
1570 log.info("Interface Config updated for {}", event.subject());
1571 createOrUpdateDeviceConfiguration();
1572 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1573 (InterfaceConfig) netcfgEvent.prevConfig().get());
1574 } else {
1575 log.error("Unhandled config class: {}", configClass);
1576 }
1577 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1578 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1579 Class configClass = netcfgEvent.configClass();
1580 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1581 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1582 log.info("App config event .. configuring network");
pierventrec80a5052021-05-20 12:56:38 +02001583 cfgListener.configureNetworkAndDrainEvents();
Charles Chan52003922018-04-05 16:31:15 -07001584 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1585 // TODO Handle sr device config removal
1586 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
Charles Chan52003922018-04-05 16:31:15 -07001587 } else if (configClass.equals(InterfaceConfig.class)) {
1588 // TODO Handle interface removal
1589 log.info("InterfaceConfig removal is not handled in current implementation");
1590 } else {
1591 log.error("Unhandled config class: {}", configClass);
1592 }
Saurav Das00e553b2018-04-21 17:19:48 -07001593 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1594 MastershipEvent me = (MastershipEvent) event;
1595 DeviceId deviceId = me.subject();
pierventre37dcf4c2021-09-16 18:43:06 +02001596 log.info(" ** Mastership changed check full reroute for {} due to change", deviceId);
Saurav Das00e553b2018-04-21 17:19:48 -07001597 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chana2401452021-06-03 15:57:48 -07001598
pierventre37dcf4c2021-09-16 18:43:06 +02001599 } else if (event.type() == ClusterEvent.Type.INSTANCE_DEACTIVATED ||
1600 event.type() == ClusterEvent.Type.INSTANCE_REMOVED) {
1601 log.info(" ** Cluster event invalidating shouldProgram");
1602 defaultRoutingHandler.invalidateShouldProgram();
Charles Chan52003922018-04-05 16:31:15 -07001603 } else {
1604 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001605 }
Charles Chan52003922018-04-05 16:31:15 -07001606 } catch (Exception e) {
1607 log.error("SegmentRouting event handler thread thrown an exception: {}",
1608 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001609 }
sangho80f11cb2015-04-01 13:05:26 -07001610 }
1611 }
1612
Saurav Dase6c448a2018-01-18 12:07:33 -08001613 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001614 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001615
1616 // NOTE: Punt ARP/NDP even when the device is not configured.
1617 // Host learning without network config is required for CORD config generator.
1618 routingRulePopulator.populateIpPunts(device.id());
1619 routingRulePopulator.populateArpNdpPunts(device.id());
1620
Charles Chan319d1a22015-11-03 10:42:14 -08001621 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001622 log.warn("Device configuration unavailable. Device {} will be "
1623 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001624 return;
1625 }
Charles Chan72779502016-04-23 17:36:10 -07001626 processDeviceAddedInternal(device.id());
1627 }
1628
1629 private void processDeviceAddedInternal(DeviceId deviceId) {
pierventre37dcf4c2021-09-16 18:43:06 +02001630 // Irrespective of whether the local is leading the programming or not for this device,
Saurav Dasc28b3432015-10-30 17:45:38 -07001631 // we need to create a SR-group-handler instance. This is because in a
1632 // multi-instance setup, any instance can initiate forwarding/next-objectives
1633 // for any switch (even if this instance is a SLAVE or not even connected
1634 // to the switch). To handle this, a default-group-handler instance is necessary
1635 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001636 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1637 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001638 DefaultGroupHandler groupHandler;
1639 try {
1640 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001641 createGroupHandler(deviceId,
1642 appId,
1643 deviceConfiguration,
1644 linkService,
1645 flowObjectiveService,
1646 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001647 } catch (DeviceConfigNotFoundException e) {
1648 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1649 return;
1650 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001651 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001652 deviceId);
1653 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001654 }
Saurav Dasb149be12016-06-07 10:08:06 -07001655
pierventre37dcf4c2021-09-16 18:43:06 +02001656 if (shouldProgram(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001657 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001658 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001659 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001660 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001661 }
Charles Chan82ab1932016-01-30 23:22:37 -08001662
Charles Chandebfea32016-10-24 14:52:01 -07001663 appCfgHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001664 }
1665
Saurav Dasc3604f12016-03-23 11:22:49 -07001666 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001667 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001668 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001669 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001670 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001671 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001672 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001673 macVlanNextObjStore.entrySet().stream()
1674 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1675 .forEach(entry -> macVlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001676 portNextObjStore.entrySet().stream()
1677 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001678 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001679 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001680
Saurav Dasfbe74572017-08-03 18:30:35 -07001681 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1682 if (gh != null) {
1683 gh.shutdown();
1684 }
Saurav Das62ae6792017-05-15 15:34:25 -07001685 // Note that a switch going down is associated with all of its links
1686 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001687 // while the link-downs are ignored. We cannot rely on the ordering of
1688 // events - i.e we cannot expect all link-downs to come before the
1689 // switch down - so we purge all seen-links for the switch before
1690 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001691 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001692 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001693 defaultRoutingHandler.purgeEcmpGraph(device.id());
pierventre37dcf4c2021-09-16 18:43:06 +02001694 // Removes routes having as target the device down
1695 defaultRoutingHandler.purgeSeenBeforeRoutes(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001696
1697 // Cleanup all internal groupHandler stores for this device. Should be
1698 // done after all rerouting or rehashing has been completed
1699 groupHandlerMap.entrySet()
1700 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Charles Chan12a8a842020-02-14 13:23:57 -08001701
1702 phasedRecoveryService.reset(device.id());
Daniele Moro23fbbed2021-07-23 16:31:49 +02001703
1704 // Trigger purgeAll only on devices managed by SR, other devices rely on
1705 // the purgeOnDisconnection device property.
1706 if (deviceConfiguration.isConfigured(device.id())) {
1707 flowObjectiveService.purgeAll(device.id(), appId);
1708 }
Saurav Dasc3604f12016-03-23 11:22:49 -07001709 }
1710
Saurav Das49368392018-04-23 18:42:12 -07001711 /**
1712 * Purge the destinationSet nextObjective store of entries with this device
1713 * as key. Erases app-level knowledge of hashed groups in this device.
1714 *
1715 * @param devId the device identifier
1716 */
Saurav Das00e553b2018-04-21 17:19:48 -07001717 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001718 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001719 dsNextObjStore.entrySet().stream()
1720 .filter(entry -> entry.getKey().deviceId().equals(devId))
1721 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1722 }
1723
Saurav Dasec683dc2018-04-27 18:42:30 -07001724 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001725 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Jon Hall5e41a7b2021-01-28 19:42:45 -08001726 log.warn("Device configuration uploading. Not handling port event for "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001727 + "dev: {} port: {}", device.id(), port.number());
1728 return;
1729 }
Saurav Dasf9332192017-02-18 14:05:44 -08001730
Saurav Dasec683dc2018-04-27 18:42:30 -07001731 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1732 lastEdgePortEvent = Instant.now();
1733 }
1734
pierventre37dcf4c2021-09-16 18:43:06 +02001735 if (shouldProgram(device.id())) {
1736 log.debug("Should not program dev:{} .. not handling port updated event "
Saurav Dasf9332192017-02-18 14:05:44 -08001737 + "for port {}", device.id(), port.number());
1738 return;
1739 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001740 processPortUpdated(device.id(), port);
1741 }
Saurav Dasf9332192017-02-18 14:05:44 -08001742
Saurav Dasec683dc2018-04-27 18:42:30 -07001743 /**
1744 * Adds or remove filtering rules for the given switchport. If switchport is
1745 * an edge facing port, additionally handles host probing and broadcast
pierventre37dcf4c2021-09-16 18:43:06 +02001746 * rules. Must be called by the instance leading the programming of the device.
Saurav Dasec683dc2018-04-27 18:42:30 -07001747 *
1748 * @param deviceId the device identifier
1749 * @param port the port to update
1750 */
1751 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001752 // first we handle filtering rules associated with the port
1753 if (port.isEnabled()) {
1754 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001755 deviceId, port.number());
1756 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001757 } else {
1758 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001759 deviceId, port.number());
1760 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001761 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001762
1763 // portUpdated calls are for ports that have gone down or up. For switch
1764 // to switch ports, link-events should take care of any re-routing or
1765 // group editing necessary for port up/down. Here we only process edge ports
1766 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001767 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001768 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1769 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1770 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001771
Saurav Das3fb28272017-03-04 16:08:47 -08001772 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001773 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001774 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001775 return;
1776 }
Saurav Das3fb28272017-03-04 16:08:47 -08001777 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001778 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001779 }
1780 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001781 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001782 }
1783 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001784 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001785 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001786 }
1787
Saurav Dasec683dc2018-04-27 18:42:30 -07001788 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001789 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001790 boolean portUp = port.isEnabled();
1791 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001792 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001793 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001794 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001795 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001796 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001797 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001798 }
1799
Saurav Dasec683dc2018-04-27 18:42:30 -07001800 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001801 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001802 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001803 } else {
1804 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001805 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001806 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001807 }
1808 }
sangho27462c62015-05-14 00:39:53 -07001809
Charles Chan8ca5a122017-10-20 16:06:55 -07001810 private void createOrUpdateDeviceConfiguration() {
1811 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001812 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001813 deviceConfiguration = new DeviceConfiguration(this);
1814 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001815 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001816 deviceConfiguration.updateConfig();
1817 }
1818 }
1819
Charles Chanfbcb8812018-04-18 18:41:05 -07001820 private void createOrUpdateDefaultRoutingHandler() {
1821 if (defaultRoutingHandler == null) {
1822 log.info("Creating new DefaultRoutingHandler");
1823 defaultRoutingHandler = new DefaultRoutingHandler(this);
1824 } else {
1825 log.info("Updating DefaultRoutingHandler");
1826 defaultRoutingHandler.update(this);
1827 }
1828 }
1829
Pier Ventreb6a7f342016-11-26 21:05:22 -08001830 /**
1831 * Registers the given connect point with the NRS, this is necessary
1832 * to receive the NDP and ARP packets from the NRS.
1833 *
1834 * @param portToRegister connect point to register
1835 */
1836 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001837 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001838 portToRegister,
1839 neighbourHandler,
1840 appId
1841 );
1842 }
1843
Charles Chan72f556a2015-10-05 17:50:33 -07001844 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001845 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001846 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001847
Charles Chanb7f75ac2016-01-11 18:28:54 -08001848 /**
1849 * Constructs the internal network config listener.
1850 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001851 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001852 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001853 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001854 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001855 }
1856
Charles Chanb7f75ac2016-01-11 18:28:54 -08001857 /**
1858 * Reads network config and initializes related data structure accordingly.
1859 */
Charles Chan873661e2017-11-30 15:37:50 -08001860 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001861 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001862
1863 // Setting handling of network configuration events completable future
1864 // The completable future is needed because of the async behaviour of the configureNetwork,
1865 // listener registration and event arrival
1866 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkey521e4392018-11-16 15:15:14 -08001867 synchronized (networkConfigCompletionLock) {
1868 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella060cad82018-04-17 12:09:34 +02001869
Ray Milkey521e4392018-11-16 15:15:14 -08001870 // add a small delay to absorb multiple network config added notifications
1871 if (!programmingScheduled.get()) {
1872 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1873 programmingScheduled.set(true);
1874 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1875 }
1876
1877 createOrUpdateDeviceConfiguration();
1878
1879 arpHandler = new ArpHandler(srManager);
1880 icmpHandler = new IcmpHandler(srManager);
1881 ipHandler = new IpHandler(srManager);
1882 routingRulePopulator = new RoutingRulePopulator(srManager);
1883 createOrUpdateDefaultRoutingHandler();
1884
1885 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1886 groupHandlerMap, tunnelStore);
1887 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1888 flowObjectiveService,
1889 tunnelHandler, policyStore);
1890 networkConfigCompletion.complete(true);
Andrea Campanella060cad82018-04-17 12:09:34 +02001891 }
1892
Charles Chan72779502016-04-23 17:36:10 -07001893 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001894
Charles Chane7c61022015-10-07 14:21:45 -07001895 }
1896
pierventrec80a5052021-05-20 12:56:38 +02001897 /**
1898 * Reads network config, initializes related data structure accordingly
1899 * and drain buffered events if needed.
1900 */
1901 void configureNetworkAndDrainEvents() {
1902 configureNetwork();
1903 drainEvents();
1904 }
1905
Charles Chan72f556a2015-10-05 17:50:33 -07001906 @Override
1907 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001908 if (mainEventExecutor == null) {
1909 return;
1910 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001911 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan3f4aca62018-03-21 16:57:47 -07001912 switch (event.type()) {
1913 case CONFIG_ADDED:
1914 case CONFIG_UPDATED:
1915 case CONFIG_REMOVED:
Andrea Campanella060cad82018-04-17 12:09:34 +02001916 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
Charles Chan152dd942020-07-21 15:33:31 -07001917 log.debug("Process netcfg {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001918 mainEventExecutor.execute(new InternalEventHandler(event));
1919 } else {
Charles Chan152dd942020-07-21 15:33:31 -07001920 log.debug("Queue netcfg {}", event);
Charles Chan39b75522018-04-21 00:44:29 -07001921 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001922 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001923 break;
1924 default:
1925 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001926 }
1927 }
Saurav Das261c3002017-06-13 15:35:54 -07001928
Charles Chanc4a68c32018-01-03 16:26:32 -08001929 @Override
1930 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001931 if (event.type() == CONFIG_REGISTERED ||
1932 event.type() == CONFIG_UNREGISTERED) {
1933 log.debug("Ignore event {} due to type mismatch", event);
1934 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001935 }
Saurav Dase321cff2018-02-09 17:26:45 -08001936
1937 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1938 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
Charles Chana01d57c2019-07-19 16:25:38 -07001939 !event.configClass().equals(InterfaceConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001940 log.debug("Ignore event {} due to class mismatch", event);
1941 return false;
1942 }
1943
1944 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001945 }
1946
Saurav Das261c3002017-06-13 15:35:54 -07001947 private final class ConfigChange implements Runnable {
1948 @Override
1949 public void run() {
1950 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001951 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001952 for (Device device : deviceService.getDevices()) {
1953 processDeviceAdded(device);
1954 }
1955 defaultRoutingHandler.startPopulationProcess();
1956 }
1957 }
Charles Chan72f556a2015-10-05 17:50:33 -07001958 }
Charles Chanf4586112015-11-09 16:37:23 -08001959
Charles Chan3f4aca62018-03-21 16:57:47 -07001960 private class InternalLinkListener implements LinkListener {
1961 @Override
1962 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001963 if (mainEventExecutor == null) {
1964 return;
1965 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001966 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1967 event.type() == LinkEvent.Type.LINK_UPDATED ||
1968 event.type() == LinkEvent.Type.LINK_REMOVED) {
1969 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001970 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1971 mainEventExecutor.execute(new InternalEventHandler(event));
1972 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001973 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001974 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001975 }
1976 }
1977 }
1978
1979 private class InternalDeviceListener implements DeviceListener {
1980 @Override
1981 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001982 if (mainEventExecutor == null) {
1983 return;
1984 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001985 switch (event.type()) {
1986 case DEVICE_ADDED:
1987 case PORT_UPDATED:
1988 case PORT_ADDED:
1989 case DEVICE_UPDATED:
1990 case DEVICE_AVAILABILITY_CHANGED:
1991 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001992 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1993 mainEventExecutor.execute(new InternalEventHandler(event));
1994 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001995 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001996 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001997 break;
1998 default:
1999 }
2000 }
2001 }
2002
2003 private class InternalTopologyListener implements TopologyListener {
2004 @Override
2005 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002006 if (mainEventExecutor == null) {
2007 return;
2008 }
Charles Chan3f4aca62018-03-21 16:57:47 -07002009 switch (event.type()) {
2010 case TOPOLOGY_CHANGED:
2011 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02002012 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
2013 mainEventExecutor.execute(new InternalEventHandler(event));
2014 } else {
Charles Chan39b75522018-04-21 00:44:29 -07002015 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02002016 }
Charles Chan3f4aca62018-03-21 16:57:47 -07002017 break;
2018 default:
2019 }
2020 }
2021 }
2022
Charles Chanf4586112015-11-09 16:37:23 -08002023 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08002024 @Override
2025 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002026 if (hostEventExecutor == null) {
2027 return;
2028 }
Charles Chanf4586112015-11-09 16:37:23 -08002029 switch (event.type()) {
2030 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08002031 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08002032 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08002033 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07002034 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07002035 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08002036 break;
2037 default:
2038 log.warn("Unsupported host event type: {}", event.type());
2039 break;
2040 }
2041 }
2042 }
2043
Charles Chanc91c8782016-03-30 17:54:24 -07002044 private class InternalMcastListener implements McastListener {
2045 @Override
2046 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002047 if (mcastEventExecutor == null) {
2048 return;
2049 }
Charles Chanc91c8782016-03-30 17:54:24 -07002050 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07002051 case SOURCES_ADDED:
2052 case SOURCES_REMOVED:
2053 case SINKS_ADDED:
2054 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07002055 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02002056 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07002057 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07002058 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01002059 break;
Charles Chanc91c8782016-03-30 17:54:24 -07002060 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07002061 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07002062 break;
2063 }
2064 }
2065 }
Charles Chan41f5ec02016-06-13 18:54:31 -07002066
Charles Chandebfea32016-10-24 14:52:01 -07002067 private class InternalRouteEventListener implements RouteListener {
2068 @Override
2069 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002070 if (routeEventExecutor == null) {
2071 return;
2072 }
Charles Chandebfea32016-10-24 14:52:01 -07002073 switch (event.type()) {
2074 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07002075 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07002076 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07002077 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07002078 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07002079 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07002080 break;
Charles Chandebfea32016-10-24 14:52:01 -07002081 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07002082 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07002083 break;
2084 }
2085 }
2086 }
Saurav Das62ae6792017-05-15 15:34:25 -07002087
Charles Chanfbcb8812018-04-18 18:41:05 -07002088 private class InternalMastershipListener implements MastershipListener {
2089 @Override
2090 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002091 if (mainEventExecutor == null) {
2092 return;
2093 }
Charles Chanfbcb8812018-04-18 18:41:05 -07002094 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07002095 case MASTER_CHANGED:
2096 log.debug("Mastership event: {}/{}", event.subject(),
2097 event.roleInfo());
2098 mainEventExecutor.execute(new InternalEventHandler(event));
2099 break;
2100 case BACKUPS_CHANGED:
2101 case SUSPENDED:
2102 default:
2103 log.debug("Mastership event type {} not handled", event.type());
2104 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07002105 }
2106 }
2107 }
2108
Saurav Das00e553b2018-04-21 17:19:48 -07002109 class InternalClusterListener implements ClusterEventListener {
2110 private Instant lastClusterEvent = Instant.EPOCH;
2111
2112 long timeSinceLastClusterEvent() {
2113 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2114 }
2115
2116 @Override
2117 public void event(ClusterEvent event) {
2118 switch (event.type()) {
2119 case INSTANCE_ACTIVATED:
2120 case INSTANCE_ADDED:
2121 case INSTANCE_READY:
2122 log.debug("Cluster event {} ignored", event.type());
2123 break;
2124 case INSTANCE_DEACTIVATED:
2125 case INSTANCE_REMOVED:
2126 log.info("** Cluster event {}", event.type());
2127 lastClusterEvent = Instant.now();
pierventre37dcf4c2021-09-16 18:43:06 +02002128 mainEventExecutor.execute(new InternalEventHandler(event));
Saurav Das00e553b2018-04-21 17:19:48 -07002129 break;
2130 default:
2131 break;
2132 }
2133
2134 }
2135
2136 }
2137
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002138 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2139 try {
2140 Set<Interface> intfs = conf.getInterfaces();
2141 Set<Interface> prevIntfs = prevConf.getInterfaces();
2142
2143 // Now we only handle one interface config at each port.
2144 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2145 log.warn("Interface update aborted - one at a time is allowed, " +
2146 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2147 return;
2148 }
2149
Andrea Campanella060cad82018-04-17 12:09:34 +02002150 //The system is in an incoherent state, abort
2151 if (defaultRoutingHandler == null) {
2152 log.warn("Interface update aborted, defaultRoutingHandler is null");
2153 return;
2154 }
2155
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002156 Interface intf = intfs.stream().findFirst().get();
2157 Interface prevIntf = prevIntfs.stream().findFirst().get();
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002158 DeviceId deviceId = intf.connectPoint().deviceId();
2159 PortNumber portNum = intf.connectPoint().port();
pierventre0dcbf0e2021-10-11 13:07:09 +02002160
2161 if (!shouldProgram(deviceId)) {
2162 log.debug("Not leading the programming of {} skip update interface {}",
2163 deviceId, intf);
2164 return;
2165 }
2166
pierventrea3989be2021-01-08 16:43:17 +01002167 // We need to do nexthop update al least one time for each
2168 // interface config change. There is no difference when it is done;
2169 boolean updateNexthop = false;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002170
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002171 removeSubnetConfig(prevIntf.connectPoint(),
2172 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2173 new HashSet<>(intf.ipAddressesList())));
2174
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002175 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2176 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2177 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2178 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2179 // Update filtering objective and L2IG group bucket
2180 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2181 } else {
pierventrea3989be2021-01-08 16:43:17 +01002182 // RemoveVlanNative - affected scenarios:
2183 // (T,N)->U; (T*,N)->U; (T,N)->(T,N); (T,N)->T
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002184 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
pierventrea3989be2021-01-08 16:43:17 +01002185 // Update the nexthops of the indirect routes
2186 updateNexthop = true;
2187 routeEventExecutor.execute(() -> routeHandler.processIntfVlanUpdatedEvent(deviceId, portNum));
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002188 }
2189 }
2190
2191 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2192 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2193 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2194 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
pierventrea3989be2021-01-08 16:43:17 +01002195 // Update filtering objective and L2IG group bucket - affected scenarios:
2196 // U->(T*,N); U->T*
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002197 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
pierventrea3989be2021-01-08 16:43:17 +01002198 if (!updateNexthop) {
2199 updateNexthop = true;
2200 routeEventExecutor.execute(() -> routeHandler.processIntfVlanUpdatedEvent(deviceId, portNum));
2201 }
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002202 } else {
pierventrea3989be2021-01-08 16:43:17 +01002203 // RemoveVlanUntagged - affected scenarios:
2204 // U->U; U->(T,N); U->T
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002205 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
pierventrea3989be2021-01-08 16:43:17 +01002206 if (!updateNexthop) {
2207 updateNexthop = true;
2208 routeEventExecutor.execute(() -> routeHandler.processIntfVlanUpdatedEvent(deviceId, portNum));
2209 }
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002210 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002211 }
2212
2213 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
pierventrea3989be2021-01-08 16:43:17 +01002214 // RemoveVlanTagged - affected scenarios:
2215 // T->U; T->T; (T,N*)->U; (T,N)->(T,N)
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002216 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2217 .filter(i -> !intf.vlanUntagged().equals(i))
2218 .filter(i -> !intf.vlanNative().equals(i))
2219 .forEach(vlanId -> updateVlanConfigInternal(
2220 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002221 }
2222
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002223 if (!intf.vlanNative().equals(VlanId.NONE)
2224 && !prevIntf.vlanNative().equals(intf.vlanNative())
2225 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2226 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2227 // Update filtering objective and L2IG group bucket
2228 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2229 } else {
pierventrea3989be2021-01-08 16:43:17 +01002230 // AddVlanNative - affected scenarios
2231 // U->(T,N); U->(T*,N); T->(T,N)
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002232 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
pierventrea3989be2021-01-08 16:43:17 +01002233 if (!updateNexthop) {
2234 updateNexthop = true;
2235 routeEventExecutor.execute(() -> routeHandler.processIntfVlanUpdatedEvent(deviceId, portNum));
2236 }
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002237 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002238 }
2239
2240 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
pierventrea3989be2021-01-08 16:43:17 +01002241 // AddVlanTagged - affected scenarios
2242 // U->T; U->(T,N*); T->T; (T,N)->(T,N)
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002243 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2244 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2245 .filter(i -> !prevIntf.vlanNative().equals(i))
2246 .forEach(vlanId -> updateVlanConfigInternal(
2247 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002248 );
pierventrea3989be2021-01-08 16:43:17 +01002249
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002250 }
2251
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002252 if (!intf.vlanUntagged().equals(VlanId.NONE)
2253 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2254 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2255 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
pierventrea3989be2021-01-08 16:43:17 +01002256 // Update filtering objective and L2IG group bucket - affected scenarios
2257 // (T*,N)->U; T*->U
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002258 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
pierventrea3989be2021-01-08 16:43:17 +01002259 if (!updateNexthop) {
2260 routeEventExecutor.execute(() -> routeHandler.processIntfVlanUpdatedEvent(deviceId, portNum));
2261 }
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002262 } else {
pierventrea3989be2021-01-08 16:43:17 +01002263 // AddVlanUntagged - affected scenarios
2264 // U->U; (T,N)->U; T->U
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002265 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
pierventrea3989be2021-01-08 16:43:17 +01002266 if (!updateNexthop) {
2267 routeEventExecutor.execute(() -> routeHandler.processIntfVlanUpdatedEvent(deviceId, portNum));
2268 }
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002269 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002270 }
2271 addSubnetConfig(prevIntf.connectPoint(),
2272 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2273 new HashSet<>(prevIntf.ipAddressesList())));
2274 } catch (ConfigException e) {
2275 log.error("Error in configuration");
2276 }
2277 }
2278
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002279 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2280 VlanId vlanId, boolean pushVlan) {
2281 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2282 if (grpHandler == null) {
2283 log.warn("Failed to retrieve group handler for device {}", deviceId);
2284 return;
2285 }
2286
2287 // Update filtering objective for a single port
2288 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2289 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2290
2291 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2292 // Update L2IG bucket of the port
2293 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2294 } else {
2295 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2296 }
2297 }
2298
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002299 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2300 VlanId vlanId, boolean pushVlan, boolean install) {
2301 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2302 if (grpHandler == null) {
2303 log.warn("Failed to retrieve group handler for device {}", deviceId);
2304 return;
2305 }
2306
2307 // Update filtering objective for a single port
2308 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2309
2310 // Update filtering objective for multicast ingress port
2311 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2312
2313 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2314
2315 if (nextId != -1 && !install) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002316 // Remove L2 Bridging rule and L3 Unicast rule to the host
pierventrea3989be2021-01-08 16:43:17 +01002317 ScheduledFuture<?> future = hostEventExecutor.schedule(
2318 () -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install),
2319 0, TimeUnit.SECONDS);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002320 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2321 // only if there is no port configured on that VLAN ID
2322 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2323 // Remove broadcast forwarding rule for the VLAN
2324 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2325 // Remove L2FG for VLAN
2326 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2327 } else {
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002328 // Remove a single port from L2FG
2329 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002330 }
pierventrea3989be2021-01-08 16:43:17 +01002331 // Before moving forward we have to be sure that processIntfVlanUpdatedEvent completed;
2332 try {
2333 future.get();
2334 } catch (InterruptedException | ExecutionException e) {
2335 log.warn("Exception caught when executing updateVlanConfigInternal future");
2336 }
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002337 // Remove L2IG of the port
2338 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002339 } else if (install) {
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002340 // Create L2IG of the port
2341 grpHandler.createPortNextObjective(deviceId, portNum, vlanId, pushVlan);
2342 // Create L2 Bridging rule and L3 Unicast rule to the host
2343 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2344 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002345 if (nextId != -1) {
2346 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002347 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002348 } else {
2349 // Create L2FG for VLAN
2350 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2351 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2352 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002353 } else {
2354 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2355 }
2356 }
2357
2358 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2359 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2360 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2361
2362 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2363 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2364 .filter(intf -> !intf.connectPoint().equals(cp))
2365 .flatMap(intf -> intf.ipAddressesList().stream())
2366 .collect(Collectors.toSet());
2367 // 1. Partial subnet population
2368 // Remove routing rules for removed subnet from previous configuration,
2369 // which does not also exist in other interfaces in the same device
2370 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2371 .map(InterfaceIpAddress::subnetAddress)
2372 .collect(Collectors.toSet());
2373
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002374 Set<IpPrefix> subnetsToBeRevoked = ipPrefixSet.stream()
2375 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2376 .collect(Collectors.toSet());
2377
2378 // Check if any of the subnets to be revoked is configured in the pairDevice.
2379 // If any, repopulate the subnet with pairDevice connectPoint instead of revoking.
2380 Optional<DeviceId> pairDevice = getPairDeviceId(cp.deviceId());
2381 if (pairDevice.isPresent()) {
2382 Set<IpPrefix> pairDeviceIpPrefix = getDeviceSubnetMap().get(pairDevice.get());
2383
2384 Set<IpPrefix> subnetsExistingInPairDevice = subnetsToBeRevoked.stream()
2385 .filter(ipPrefix -> pairDeviceIpPrefix.contains(ipPrefix))
2386 .collect(Collectors.toSet());
2387
2388 // Update the subnets existing in pair device with pair device connect point.
2389 if (!subnetsExistingInPairDevice.isEmpty()) {
2390 // PortNumber of connect point is not relevant in populate subnet and hence providing as ANY.
2391 ConnectPoint pairDeviceCp = new ConnectPoint(pairDevice.get(), PortNumber.ANY);
2392 log.debug("Updating the subnets: {} with pairDevice connectPoint as it exists in the Pair device: {}",
2393 subnetsExistingInPairDevice, pairDeviceCp);
2394 defaultRoutingHandler.populateSubnet(Collections.singleton(pairDeviceCp), subnetsExistingInPairDevice);
2395 }
2396
2397 // Remove only the subnets that are not configured in the pairDevice.
2398 subnetsToBeRevoked = Sets.difference(subnetsToBeRevoked, subnetsExistingInPairDevice);
2399 }
2400
2401 if (!subnetsToBeRevoked.isEmpty()) {
2402 log.debug("Removing subnets for connectPoint: {}, subnets: {}", cp, subnetsToBeRevoked);
pierventre0dcbf0e2021-10-11 13:07:09 +02002403 defaultRoutingHandler.revokeSubnet(subnetsToBeRevoked, cp.deviceId());
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002404 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002405
2406 // 2. Interface IP punts
2407 // Remove IP punts for old Intf address
2408 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2409 .map(InterfaceIpAddress::ipAddress)
2410 .collect(Collectors.toSet());
2411 ipAddressSet.stream()
2412 .map(InterfaceIpAddress::ipAddress)
2413 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2414 .forEach(interfaceIpAddress ->
2415 routingRulePopulator.revokeSingleIpPunts(
2416 cp.deviceId(), interfaceIpAddress));
2417
2418 // 3. Host unicast routing rule
2419 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002420 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002421 }
2422
2423 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2424 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2425 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2426
2427 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2428 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2429 .filter(intf -> !intf.connectPoint().equals(cp))
2430 .flatMap(intf -> intf.ipAddressesList().stream())
2431 .collect(Collectors.toSet());
2432 // 1. Partial subnet population
2433 // Add routing rules for newly added subnet, which does not also exist in
2434 // other interfaces in the same device
2435 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2436 .map(InterfaceIpAddress::subnetAddress)
2437 .collect(Collectors.toSet());
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002438 Set<IpPrefix> subnetsToBePopulated = ipPrefixSet.stream()
2439 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2440 .collect(Collectors.toSet());
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002441
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002442 if (!subnetsToBePopulated.isEmpty()) {
2443 log.debug("Adding subnets for connectPoint: {}, subnets: {}", cp, subnetsToBePopulated);
2444
pierventre0dcbf0e2021-10-11 13:07:09 +02002445 // check if pair-device has the same subnet configured
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002446 Optional<DeviceId> pairDevice = getPairDeviceId(cp.deviceId());
2447 if (pairDevice.isPresent()) {
2448 Set<IpPrefix> pairDeviceIpPrefix = getDeviceSubnetMap().get(pairDevice.get());
2449
2450 Set<IpPrefix> subnetsToBePopulatedAsDualHomed = subnetsToBePopulated.stream()
pierventre0dcbf0e2021-10-11 13:07:09 +02002451 .filter(pairDeviceIpPrefix::contains)
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002452 .collect(Collectors.toSet());
2453 Set<IpPrefix> subnetsToBePopulatedAsSingleHomed = Sets.difference(subnetsToBePopulated,
2454 subnetsToBePopulatedAsDualHomed);
2455
2456 if (!subnetsToBePopulatedAsSingleHomed.isEmpty()) {
2457 defaultRoutingHandler.populateSubnet(
2458 Collections.singleton(cp),
2459 subnetsToBePopulatedAsSingleHomed);
2460 }
2461
2462 if (!subnetsToBePopulatedAsDualHomed.isEmpty()) {
2463 Set<ConnectPoint> cpts = new HashSet<>();
2464 cpts.add(cp);
2465 // As Subnets is DualHomed adding the pairDevice also as ConnectPoint.
2466 // PortNumber of connect point is not relevant in populate subnet and hence providing as ANY.
2467 ConnectPoint pairCp = new ConnectPoint(pairDevice.get(), PortNumber.ANY);
2468 cpts.add(pairCp);
2469
2470 log.debug("Adding DualHomed subnets for connectPoint: {} and its pair device: {}, subnets: {}",
2471 cp, pairCp, subnetsToBePopulatedAsDualHomed);
2472
2473 // populating the subnets as DualHomed
2474 defaultRoutingHandler.populateSubnet(
2475 cpts,
2476 subnetsToBePopulated);
2477
2478 // revoking the subnets populated in the device as it is now Dualhomed.
2479 defaultRoutingHandler.revokeSubnet(Collections.singleton(cp.deviceId()),
2480 subnetsToBePopulatedAsDualHomed);
2481 }
2482 } else {
2483 defaultRoutingHandler.populateSubnet(
2484 Collections.singleton(cp),
2485 subnetsToBePopulated);
2486 }
2487 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002488
2489 // 2. Interface IP punts
2490 // Add IP punts for new Intf address
2491 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2492 .map(InterfaceIpAddress::ipAddress)
2493 .collect(Collectors.toSet());
2494 ipAddressSet.stream()
2495 .map(InterfaceIpAddress::ipAddress)
2496 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2497 .forEach(interfaceIpAddress ->
2498 routingRulePopulator.populateSingleIpPunts(
2499 cp.deviceId(), interfaceIpAddress));
2500
2501 // 3. Host unicast routing rule
2502 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002503 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002504 }
sangho80f11cb2015-04-01 13:05:26 -07002505}