blob: 98ee40cf065c9453c3c04a4a369718444c5c9f8e [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;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
Charles Chanf0ae41e2017-08-23 13:55:39 -070022import com.google.common.collect.Sets;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080024import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070025import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080026import org.onlab.packet.IPv6;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070027import org.onlab.packet.IpAddress;
Charles Chan77277672015-10-20 16:24:19 -070028import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070029import org.onlab.packet.VlanId;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +000030import org.onlab.packet.MacAddress;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070031import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080032import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070033import org.onosproject.cfg.ComponentConfigService;
Saurav Das00e553b2018-04-21 17:19:48 -070034import org.onosproject.cluster.ClusterEvent;
35import org.onosproject.cluster.ClusterEventListener;
Pier Luigi580fd8a2018-01-16 10:47:50 +010036import org.onosproject.cluster.ClusterService;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070037import org.onosproject.cluster.LeadershipService;
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;
96import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070098import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070099import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700100import org.onosproject.segmentrouting.grouphandler.DestinationSet;
101import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
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;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700106import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800110import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700111import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700112import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800113import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das261c3002017-06-13 15:35:54 -0700114import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700115import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700116import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800117import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000118import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
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;
Charles Chan77cdde42018-05-08 21:35:50 -0700149import java.util.concurrent.ExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800150import java.util.concurrent.Executors;
151import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800152import java.util.concurrent.TimeUnit;
153import java.util.concurrent.atomic.AtomicBoolean;
154import java.util.stream.Collectors;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700155import java.util.stream.IntStream;
sangho80f11cb2015-04-01 13:05:26 -0700156
Charles Chand6d25332016-02-26 22:19:52 -0800157import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800158import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700159import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800160import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
161import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700162import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
163import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
psneha86e60d32019-03-26 06:31:41 -0400169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_SIMPLIFICATION;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700170import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700171import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700172import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
173import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
174import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
psneha86e60d32019-03-26 06:31:41 -0400175import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_SIMPLIFICATION_DEFAULT;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700176import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700177import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chand6d25332016-02-26 22:19:52 -0800178
Charles Chanb7f75ac2016-01-11 18:28:54 -0800179/**
180 * Segment routing manager.
181 */
Ray Milkey05edbfc2018-10-23 14:23:16 -0700182@Component(
183 immediate = true,
184 service = SegmentRoutingService.class,
185 property = {
186 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
187 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
188 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
189 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
190 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
191 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
psneha86e60d32019-03-26 06:31:41 -0400192 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT,
193 PROP_ROUTE_SIMPLIFICATION + ":Boolean=" + ROUTE_SIMPLIFICATION_DEFAULT
Ray Milkey05edbfc2018-10-23 14:23:16 -0700194 }
195)
sangho27462c62015-05-14 00:39:53 -0700196public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700197
Charles Chan46fdfaf2016-11-09 20:51:44 -0800198 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700199 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
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
Ray Milkey2bd24a92018-08-17 14:54:17 -0700258 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700259 public LeadershipService leadershipService;
260
Charles Chanb4f9cd12019-02-27 11:46:32 -0800261 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
262 bind = "bindXconnectService",
263 unbind = "unbindXconnectService",
264 policy = ReferencePolicy.DYNAMIC)
Charles Chan8d316332018-06-19 20:31:57 -0700265 public XconnectService xconnectService;
266
Ray Milkey05edbfc2018-10-23 14:23:16 -0700267 /** Enable active probing to discover dual-homed hosts. */
268 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan873661e2017-11-30 15:37:50 -0800269
Ray Milkeye96d0de2018-11-02 17:12:21 -0700270 /** Enable only send probe on the same port number of the pair device. */
271 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwarif7942402018-10-29 18:27:35 -0400272
Ray Milkey05edbfc2018-10-23 14:23:16 -0700273 /** Enable administratively taking down single-homed hosts. */
274 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Dasec683dc2018-04-27 18:42:30 -0700275
Ray Milkey05edbfc2018-10-23 14:23:16 -0700276 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
277 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700278
Ray Milkey05edbfc2018-10-23 14:23:16 -0700279 /** Program flows and groups to pop and route double tagged hosts. */
280 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan4eb73bb2018-07-19 14:55:31 -0700281
Ray Milkey05edbfc2018-10-23 14:23:16 -0700282 /** internal vlan assigned by default to unconfigured ports. */
283 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700284
Ray Milkey05edbfc2018-10-23 14:23:16 -0700285 /** vlan used for transport of pseudowires between switches. */
286 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700287
psneha86e60d32019-03-26 06:31:41 -0400288 /** Enabling route simplification. */
289 boolean routeSimplification = ROUTE_SIMPLIFICATION_DEFAULT;
290
Charles Chandebfea32016-10-24 14:52:01 -0700291 ArpHandler arpHandler = null;
292 IcmpHandler icmpHandler = null;
293 IpHandler ipHandler = null;
294 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700295 ApplicationId appId;
296 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700297
Charles Chandebfea32016-10-24 14:52:01 -0700298 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700299 private TunnelHandler tunnelHandler = null;
300 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700301 private InternalPacketProcessor processor = null;
302 private InternalLinkListener linkListener = null;
303 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700304 private AppConfigHandler appCfgHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800305 McastHandler mcastHandler = null;
306 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800307 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800308 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800309 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800310 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100311 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800312 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700313 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
314 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700315 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100316 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700317 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700318 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200319 //Completable future for network configuration process to buffer config events handling during activation
320 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkey521e4392018-11-16 15:15:14 -0800321 private final Object networkConfigCompletionLock = new Object();
Charles Chan39b75522018-04-21 00:44:29 -0700322 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700323
Charles Chan52003922018-04-05 16:31:15 -0700324 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700325 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700326
Charles Chanfbcb8812018-04-18 18:41:05 -0700327 // Handles host, route and mcast events respectively
328 private ScheduledExecutorService hostEventExecutor;
329 private ScheduledExecutorService routeEventExecutor;
330 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700331 private ExecutorService packetExecutor;
Charles Chana7601c92019-01-23 15:03:17 -0800332 ExecutorService neighborExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700333
334 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800335 /**
Saurav Das261c3002017-06-13 15:35:54 -0700336 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700337 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800338 */
Charles Chan873661e2017-11-30 15:37:50 -0800339 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700340 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800341 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700342 * Per device next objective ID store with (device id + vlanid) as key.
343 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800344 */
Charles Chan873661e2017-11-30 15:37:50 -0800345 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800346 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800347 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700348 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000349 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800350 */
Charles Chan873661e2017-11-30 15:37:50 -0800351 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800352 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800353
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700354 /**
355 * Per port dummy VLAN ID store with (connect point + ip address) as key.
356 * Used to keep track on dummy VLAN ID allocation.
357 */
358 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
359 dummyVlanIdStore = null;
360
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000361 /**
362 * Per device next objective ID store with (device id + MAC address + vlan) as key.
363 * Used to keep track of L3 unicast group for indirect hosts.
364 */
365 private EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
366 macVlanNextObjStore = null;
367
Saurav Das2d94d312015-11-24 23:21:05 -0800368 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
369 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700370
Saurav Das261c3002017-06-13 15:35:54 -0700371 private AtomicBoolean programmingScheduled = new AtomicBoolean();
372
Charles Chanc91c8782016-03-30 17:54:24 -0700373 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700374 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
375 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700376 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700377 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800378 public SegmentRoutingDeviceConfig createConfig() {
379 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700380 }
381 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800382
Charles Chanc91c8782016-03-30 17:54:24 -0700383 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700384 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
385 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700386 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800387 @Override
388 public SegmentRoutingAppConfig createConfig() {
389 return new SegmentRoutingAppConfig();
390 }
391 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800392
Charles Chanc91c8782016-03-30 17:54:24 -0700393 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700394 new ConfigFactory<ApplicationId, McastConfig>(
395 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700396 McastConfig.class, "multicast") {
397 @Override
398 public McastConfig createConfig() {
399 return new McastConfig();
400 }
401 };
402
Charles Chan1963f4f2016-02-18 14:22:42 -0800403 /**
404 * Segment Routing App ID.
405 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800406 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700407
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700408 /**
409 * Minumum and maximum value of dummy VLAN ID to be allocated.
410 */
411 public static final int MIN_DUMMY_VLAN_ID = 2;
412 public static final int MAX_DUMMY_VLAN_ID = 4093;
413
Charles Chana7601c92019-01-23 15:03:17 -0800414 private static final int DEFAULT_POOL_SIZE = 32;
415
Saurav Dasec683dc2018-04-27 18:42:30 -0700416 Instant lastEdgePortEvent = Instant.EPOCH;
417
Charles Chanb4f9cd12019-02-27 11:46:32 -0800418 protected void bindXconnectService(XconnectService xconnectService) {
419 if (this.xconnectService == null) {
420 log.info("Binding XconnectService");
421 this.xconnectService = xconnectService;
422 } else {
423 log.warn("Trying to bind XconnectService but it is already bound");
424 }
425 }
426
427 protected void unbindXconnectService(XconnectService xconnectService) {
428 if (this.xconnectService == xconnectService) {
429 log.info("Unbinding XconnectService");
430 this.xconnectService = null;
431 } else {
432 log.warn("Trying to unbind XconnectService but it is already unbound");
433 }
434 }
435
sangho80f11cb2015-04-01 13:05:26 -0700436 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800437 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800438 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700439
Charles Chana7601c92019-01-23 15:03:17 -0800440 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
441 groupedThreads("onos/sr", "event-main-%d", log));
442 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
443 groupedThreads("onos/sr", "event-host-%d", log));
444 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
445 groupedThreads("onos/sr", "event-route-%d", log));
446 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
447 groupedThreads("onos/sr", "event-mcast-%d", log));
448 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
449 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
450 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700451
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700452 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700453 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700454 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700455 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700456 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700457 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700458 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700459 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700460 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700461
Charles Chan10b0fb72017-02-02 16:20:42 -0800462 log.debug("Creating EC map vlannextobjectivestore");
463 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
464 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
465 vlanNextObjStore = vlanNextObjMapBuilder
466 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700467 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700468 .withTimestampProvider((k, v) -> new WallClockTimestamp())
469 .build();
470
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000471 log.debug("Creating EC map macvlannextobjectivestore");
472 EventuallyConsistentMapBuilder<MacVlanNextObjectiveStoreKey, Integer>
473 macVlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
474 macVlanNextObjStore = macVlanNextObjMapBuilder
475 .withName("macvlannextobjectivestore")
476 .withSerializer(createSerializer())
477 .withTimestampProvider((k, v) -> new WallClockTimestamp())
478 .build();
479
Saurav Das2d94d312015-11-24 23:21:05 -0800480 log.debug("Creating EC map subnetnextobjectivestore");
481 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
482 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
483 portNextObjStore = portNextObjMapBuilder
484 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700485 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800486 .withTimestampProvider((k, v) -> new WallClockTimestamp())
487 .build();
488
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700489 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
490 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
491 dummyVlanIdStore = dummyVlanIdMapBuilder
492 .withName("dummyvlanidstore")
493 .withSerializer(createSerializer())
494 .withTimestampProvider((k, v) -> new WallClockTimestamp())
495 .build();
496
sangho4a5c42a2015-05-20 22:16:38 -0700497 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
498 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700499 tunnelStore = tunnelMapBuilder
500 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700501 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700502 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700503 .build();
504
505 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
506 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700507 policyStore = policyMapBuilder
508 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700509 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700510 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700511 .build();
512
Saurav Dasc3604f12016-03-23 11:22:49 -0700513 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700514 "purgeOnDisconnection", "true", false);
Saurav Dasc3604f12016-03-23 11:22:49 -0700515 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700516 "purgeOnDisconnection", "true", false);
Pier Luigib9632ba2017-01-12 18:14:58 -0800517 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700518 "requestInterceptsEnabled", "false", false);
Charles Chanc1909e12017-08-08 15:13:37 -0700519 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700520 "requestInterceptsEnabled", "false", false);
Charles Chan9597f8d2017-07-24 15:56:10 -0700521 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700522 "arpEnabled", "false", false);
Pier Luigi0ebeb312017-02-02 22:31:34 -0800523 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700524 "greedyLearningIpv6", "true", false);
Charles Chancf8ea472017-02-28 15:15:17 -0800525 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700526 "forceUnprovision", "true", false);
Charles Chanc7b73c72017-08-10 16:57:28 -0700527 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan0fa51db2019-04-10 17:12:20 -0700528 "distributed", "true", false);
Charles Chan804772f2017-08-14 11:42:11 -0700529 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700530 "multihomingEnabled", "true", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800531 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700532 "staleLinkAge", "15000", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800533 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700534 "allowDuplicateIps", "false", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800535 // For P4 switches
536 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700537 "fallbackFlowPollFrequency", "4", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800538 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700539 "fallbackGroupPollFrequency", "3", false);
Charles Chan873661e2017-11-30 15:37:50 -0800540 compCfgService.registerProperties(getClass());
541 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700542
Charles Chan2b078ae2015-10-14 11:24:40 -0700543 processor = new InternalPacketProcessor();
544 linkListener = new InternalLinkListener();
545 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700546 appCfgHandler = new AppConfigHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700547 mcastHandler = new McastHandler(this);
548 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800549 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700550 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800551 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800552 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100553 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700554
Charles Chand6d25332016-02-26 22:19:52 -0800555 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700556 cfgService.registerConfigFactory(deviceConfigFactory);
557 cfgService.registerConfigFactory(appConfigFactory);
558 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800559 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200560
Charles Chan6961f222018-01-04 14:26:07 -0800561 cfgListener.configureNetwork();
562
Charles Chan82ab1932016-01-30 23:22:37 -0800563 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700564 packetService.addProcessor(processor, PacketProcessor.director(2));
565 linkService.addListener(linkListener);
566 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700567 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700568 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100569 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700570 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700571 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700572
Charles Chanc12c3d02018-03-09 15:53:44 -0800573 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800574 l2TunnelHandler.init();
575
Ray Milkey521e4392018-11-16 15:15:14 -0800576 synchronized (networkConfigCompletionLock) {
577 networkConfigCompletion.whenComplete((value, ex) -> {
578 //setting to null for easier fall through
579 networkConfigCompletion = null;
580 //process all queued events
581 queuedEvents.forEach(event -> {
582 mainEventExecutor.execute(new InternalEventHandler(event));
583 });
Andrea Campanella060cad82018-04-17 12:09:34 +0200584 });
Ray Milkey521e4392018-11-16 15:15:14 -0800585 }
Andrea Campanella060cad82018-04-17 12:09:34 +0200586
sangho80f11cb2015-04-01 13:05:26 -0700587 log.info("Started");
588 }
589
Saurav Dase6c448a2018-01-18 12:07:33 -0800590 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700591 return new KryoNamespace.Builder()
592 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700593 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800594 VlanNextObjectiveStoreKey.class,
595 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700596 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800597 NextNeighbors.class,
598 Tunnel.class,
599 DefaultTunnel.class,
600 Policy.class,
601 TunnelPolicy.class,
602 Policy.Type.class,
603 PortNextObjectiveStoreKey.class,
604 XConnectStoreKey.class,
605 L2Tunnel.class,
606 L2TunnelPolicy.class,
607 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700608 DefaultL2TunnelPolicy.class,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000609 DummyVlanIdStoreKey.class,
610 MacVlanNextObjectiveStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700611 );
612 }
613
sangho80f11cb2015-04-01 13:05:26 -0700614 @Deactivate
615 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700616 mainEventExecutor.shutdown();
617 hostEventExecutor.shutdown();
618 routeEventExecutor.shutdown();
619 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700620 packetExecutor.shutdown();
Charles Chana7601c92019-01-23 15:03:17 -0800621 neighborExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700622
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700623 mainEventExecutor = null;
624 hostEventExecutor = null;
625 routeEventExecutor = null;
626 mcastEventExecutor = null;
627 packetExecutor = null;
Charles Chana7601c92019-01-23 15:03:17 -0800628 neighborExecutor = null;
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700629
Charles Chan72f556a2015-10-05 17:50:33 -0700630 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700631 cfgService.unregisterConfigFactory(deviceConfigFactory);
632 cfgService.unregisterConfigFactory(appConfigFactory);
633 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800634 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700635
Charles Chanfd48c222017-06-19 00:43:31 -0700636 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700637 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700638 linkService.removeListener(linkListener);
639 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700640 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700641 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100642 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700643 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700644 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700645
Charles Chan2e71ef32017-02-23 15:44:08 -0800646 neighbourResolutionService.unregisterNeighbourHandlers(appId);
647
sangho80f11cb2015-04-01 13:05:26 -0700648 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700649 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700650 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700651 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700652 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700653 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700654
Saurav Das261c3002017-06-13 15:35:54 -0700655 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800656 vlanNextObjStore.destroy();
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000657 macVlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700658 portNextObjStore.destroy();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700659 dummyVlanIdStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700660 tunnelStore.destroy();
661 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100662
663 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700664 log.info("Stopped");
665 }
666
Charles Chan873661e2017-11-30 15:37:50 -0800667 @Modified
668 private void modified(ComponentContext context) {
669 Dictionary<?, ?> properties = context.getProperties();
670 if (properties == null) {
671 return;
672 }
673
Ray Milkey48d10c02018-10-24 10:04:03 -0700674 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700675 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800676 if (expectActiveProbing != activeProbing) {
677 activeProbing = expectActiveProbing;
678 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
679 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700680
Ray Milkeye96d0de2018-11-02 17:12:21 -0700681
682 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
683 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwarif7942402018-10-29 18:27:35 -0400684 if (expectSymmetricProbing != symmetricProbing) {
685 symmetricProbing = expectSymmetricProbing;
686 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
687 }
688
Ray Milkey48d10c02018-10-24 10:04:03 -0700689 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700690 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
691 if (expectSingleHomedDown != singleHomedDown) {
692 singleHomedDown = expectSingleHomedDown;
693 log.info("{} downing of single homed hosts for lost uplinks",
694 singleHomedDown ? "Enabling" : "Disabling");
695 if (singleHomedDown && linkHandler != null) {
696 hostService.getHosts().forEach(host -> host.locations()
697 .forEach(loc -> {
698 if (interfaceService.isConfigured(loc)) {
699 linkHandler.checkUplinksForHost(loc);
700 }
701 }));
702 } else {
703 log.warn("Disabling singleHomedDown does not re-enable already "
704 + "downed ports for single-homed hosts");
705 }
706 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700707
Ray Milkey48d10c02018-10-24 10:04:03 -0700708 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700709 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
710 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
711 respondToUnknownHosts = expectRespondToUnknownHosts;
712 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
713 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700714
Ray Milkey48d10c02018-10-24 10:04:03 -0700715 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700716 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
717 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
718 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
719 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700720
Charles Chanfbca55e2018-07-24 16:40:35 -0700721 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700722 hostHandler.populateAllDoubleTaggedHost();
723 } else {
724 hostHandler.revokeAllDoubleTaggedHost();
725 }
726 }
Saurav Das9bf49582018-08-13 15:34:26 -0700727
Ray Milkey48d10c02018-10-24 10:04:03 -0700728 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700729 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
730 if (defIntVlan != defaultInternalVlan) {
731 if (canUseVlanId(defIntVlan)) {
732 log.warn("Default internal vlan value changed from {} to {}.. "
733 + "re-programming filtering rules, but NOT any groups already "
734 + "created with the former value", defaultInternalVlan, defIntVlan);
735 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
736 defaultInternalVlan = defIntVlan;
737 routingRulePopulator
738 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
739 VlanId.vlanId((short) defIntVlan));
740 } else {
741 log.warn("Cannot change default internal vlan to unusable "
742 + "value {}", defIntVlan);
743 }
744 }
745
Ray Milkey48d10c02018-10-24 10:04:03 -0700746 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700747 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
748 if (pwTxpVlan != pwTransportVlan) {
749 if (canUseVlanId(pwTxpVlan)) {
750 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
751 + "re-programming filtering rules, but NOT any groups already "
752 + "created with the former value", pwTransportVlan,
753 pwTxpVlan);
754 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
755 pwTransportVlan = pwTxpVlan;
756 routingRulePopulator
757 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
758 VlanId.vlanId((short) pwTxpVlan));
759 } else {
760 log.warn("Cannot change pseudowire transport vlan to unusable "
761 + "value {}", pwTxpVlan);
762 }
763 }
764
psneha86e60d32019-03-26 06:31:41 -0400765 String strRouteSimplification = Tools.get(properties, PROP_ROUTE_SIMPLIFICATION);
766 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
767 if (expectRouteSimplification != routeSimplification) {
768 routeSimplification = expectRouteSimplification;
769 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
770 }
771
Saurav Das9bf49582018-08-13 15:34:26 -0700772 }
773
774 /**
775 * Returns true if given vlan id is not being used in the system currently,
776 * either as one of the default system wide vlans or as one of the
777 * configured interface vlans.
778 *
779 * @param vlanId given vlan id
780 * @return true if vlan is not currently in use
781 */
782 public boolean canUseVlanId(int vlanId) {
783 if (vlanId >= 4095 || vlanId <= 1) {
784 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
785 vlanId);
786 return false;
787 }
788
789 VlanId vid = VlanId.vlanId((short) vlanId);
790 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
791 log.warn("Vlan id {} value is already in use system-wide. "
792 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
793 getDefaultInternalVlan(), getPwTransportVlan());
794 return false;
795 }
796
797 if (interfaceService.inUse(vid)) {
798 log.warn("Vlan id {} value is already in use on a configured "
799 + "interface in the system", vlanId);
800 return false;
801 }
802 return true;
803 }
804
805 /**
806 * Returns the VlanId assigned internally by default to unconfigured ports.
807 *
808 * @return the default internal vlan id
809 */
810 public VlanId getDefaultInternalVlan() {
811 return VlanId.vlanId((short) defaultInternalVlan);
812 }
813
814 /**
815 * Returns the Vlan id used to transport pseudowire traffic across the
816 * network.
817 *
818 * @return the pseudowire transport vlan id
819 */
820 public VlanId getPwTransportVlan() {
821 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800822 }
823
sangho27462c62015-05-14 00:39:53 -0700824 @Override
825 public List<Tunnel> getTunnels() {
826 return tunnelHandler.getTunnels();
827 }
828
829 @Override
sanghobd812f82015-06-29 14:58:47 -0700830 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
831 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700832 }
833
834 @Override
sanghobd812f82015-06-29 14:58:47 -0700835 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700836 for (Policy policy: policyHandler.getPolicies()) {
837 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
838 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
839 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
840 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700841 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700842 }
843 }
844 }
sanghobd812f82015-06-29 14:58:47 -0700845 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700846 }
847
848 @Override
sanghobd812f82015-06-29 14:58:47 -0700849 public PolicyHandler.Result removePolicy(Policy policy) {
850 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700851 }
852
853 @Override
sanghobd812f82015-06-29 14:58:47 -0700854 public PolicyHandler.Result createPolicy(Policy policy) {
855 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700856 }
857
858 @Override
859 public List<Policy> getPolicies() {
860 return policyHandler.getPolicies();
861 }
862
Saurav Das07c74602016-04-27 18:35:50 -0700863 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700864 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
865 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700866 }
867
868 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800869 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700870 return l2TunnelHandler.getL2Tunnels();
871 }
872
873 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800874 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700875 return l2TunnelHandler.getL2Policies();
876 }
877
Saurav Dasdebcf882018-04-06 20:16:01 -0700878 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700879 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700880 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
881
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700882 // get both added and pending pseudowires
883 List<L2TunnelDescription> pseudowires = new ArrayList<>();
884 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
885 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700886 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700887
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700888 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
889
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700890 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
891 L2TunnelHandler.Result res;
892 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
893 res = addPseudowire(pw);
894 if (res != L2TunnelHandler.Result.SUCCESS) {
895 log.error("Pseudowire with id {} can not be instantiated !", res);
896 retRes = res;
897 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700898 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700899
900 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700901 }
902
903 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800904 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700905 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700906 }
907
908 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800909 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700910 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700911 }
912
913 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700914 public void rerouteNetwork() {
915 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700916 }
917
Charles Chand7844e52016-10-20 17:02:44 -0700918 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800919 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
920 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800921 deviceConfiguration.getRouters().forEach(device ->
922 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700923 return deviceSubnetMap;
924 }
925
Saurav Das62ae6792017-05-15 15:34:25 -0700926
927 @Override
928 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
929 if (defaultRoutingHandler != null) {
930 return defaultRoutingHandler.getCurrentEmcpSpgMap();
931 } else {
932 return null;
933 }
934 }
935
936 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700937 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700938 if (dsNextObjStore != null) {
939 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700940 } else {
941 return ImmutableMap.of();
942 }
943 }
944
Saurav Dasfbe74572017-08-03 18:30:35 -0700945 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700946 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
947 if (vlanNextObjStore != null) {
948 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
949 } else {
950 return ImmutableMap.of();
951 }
952 }
953
954 @Override
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000955 public ImmutableMap<MacVlanNextObjectiveStoreKey, Integer> getMacVlanNextObjStore() {
956 if (macVlanNextObjStore != null) {
957 return ImmutableMap.copyOf(macVlanNextObjStore.entrySet());
958 } else {
959 return ImmutableMap.of();
960 }
961 }
962
963 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700964 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
965 if (portNextObjStore != null) {
966 return ImmutableMap.copyOf(portNextObjStore.entrySet());
967 } else {
968 return ImmutableMap.of();
969 }
970 }
971
972 @Override
973 public ImmutableMap<String, NextObjective> getPwInitNext() {
974 if (l2TunnelHandler != null) {
975 return l2TunnelHandler.getInitNext();
976 } else {
977 return ImmutableMap.of();
978 }
979 }
980
981 @Override
982 public ImmutableMap<String, NextObjective> getPwTermNext() {
983 if (l2TunnelHandler != null) {
984 return l2TunnelHandler.getTermNext();
985 } else {
986 return ImmutableMap.of();
987 }
988 }
989
990 @Override
991 public void invalidateNextObj(int nextId) {
992 if (dsNextObjStore != null) {
993 dsNextObjStore.entrySet().forEach(e -> {
994 if (e.getValue().nextId() == nextId) {
995 dsNextObjStore.remove(e.getKey());
996 }
997 });
998 }
999 if (vlanNextObjStore != null) {
1000 vlanNextObjStore.entrySet().forEach(e -> {
1001 if (e.getValue() == nextId) {
1002 vlanNextObjStore.remove(e.getKey());
1003 }
1004 });
1005 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001006 if (macVlanNextObjStore != null) {
1007 macVlanNextObjStore.entrySet().forEach(e -> {
1008 if (e.getValue() == nextId) {
1009 macVlanNextObjStore.remove(e.getKey());
1010 }
1011 });
1012 }
Charles Chan0b1dd7e2018-08-19 19:21:46 -07001013 if (portNextObjStore != null) {
1014 portNextObjStore.entrySet().forEach(e -> {
1015 if (e.getValue() == nextId) {
1016 portNextObjStore.remove(e.getKey());
1017 }
1018 });
1019 }
1020 if (mcastHandler != null) {
1021 mcastHandler.removeNextId(nextId);
1022 }
1023 if (l2TunnelHandler != null) {
1024 l2TunnelHandler.removeNextId(nextId);
1025 }
1026 if (xconnectService != null) {
1027 xconnectService.removeNextId(nextId);
1028 }
1029 }
1030
1031 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -07001032 public void verifyGroups(DeviceId id) {
1033 DefaultGroupHandler gh = groupHandlerMap.get(id);
1034 if (gh != null) {
1035 gh.triggerBucketCorrector();
1036 }
1037 }
1038
Saurav Das6430f412018-01-25 09:49:01 -08001039 @Override
1040 public ImmutableMap<Link, Boolean> getSeenLinks() {
1041 return linkHandler.getSeenLinks();
1042 }
1043
1044 @Override
1045 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1046 return linkHandler.getDownedPorts();
1047 }
1048
Pier Luigi0f9635b2018-01-15 18:06:43 +01001049 @Override
1050 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -07001051 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +01001052 }
1053
1054 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001055 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +01001056 return mcastHandler.getMcastRoles(mcastIp);
1057 }
1058
1059 @Override
Piere99511d2018-04-19 16:47:06 +02001060 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1061 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1062 }
1063
1064 @Override
Pier Luigi0f9635b2018-01-15 18:06:43 +01001065 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1066 return mcastHandler.getMcastPaths(mcastIp);
1067 }
1068
Pierdb27b8d2018-04-17 16:29:56 +02001069 @Override
Pier71c55772018-04-17 17:25:22 +02001070 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1071 ConnectPoint sourcecp) {
1072 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1073 }
1074
1075 @Override
Pierdb27b8d2018-04-17 16:29:56 +02001076 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1077 return mcastHandler.getMcastLeaders(mcastIp);
1078 }
1079
Charles Chanb13e0702018-04-17 18:56:53 -07001080 @Override
1081 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1082 return defaultRoutingHandler == null ? ImmutableMap.of() :
1083 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1084 }
1085
1086 @Override
1087 public Map<DeviceId, Boolean> getShouldProgramCache() {
1088 return defaultRoutingHandler == null ? ImmutableMap.of() :
1089 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1090 }
1091
Charles Chan8d316332018-06-19 20:31:57 -07001092 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301093 public boolean shouldProgram(DeviceId deviceId) {
1094 return defaultRoutingHandler.shouldProgram(deviceId);
1095 }
1096
1097 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001098 public ApplicationId appId() {
1099 return appId;
1100 }
1101
1102 /**
1103 * Returns the device configuration.
1104 *
1105 * @return device configuration
1106 */
1107 public DeviceConfiguration deviceConfiguration() {
1108 return deviceConfiguration;
1109 }
1110
1111 /**
Saurav Das261c3002017-06-13 15:35:54 -07001112 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001113 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001114 *
1115 * @return next objective ID store
1116 */
Saurav Das261c3002017-06-13 15:35:54 -07001117 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1118 dsNextObjStore() {
1119 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001120 }
1121
1122 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001123 * Per device next objective ID store with (device id + vlanid) as key.
1124 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001125 *
1126 * @return vlan next object store
1127 */
1128 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1129 return vlanNextObjStore;
1130 }
1131
1132 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001133 * Per device next objective ID store with (device id + MAC address + vlan) as key.
1134 * Used to keep track on L3 Unicast group information for indirect hosts.
1135 *
1136 * @return mac vlan next object store
1137 */
1138 public EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> macVlanNextObjStore() {
1139 return macVlanNextObjStore;
1140 }
1141
1142 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001143 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001144 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Ray Milkeyb85de082017-04-05 09:42:04 -07001145 *
1146 * @return port next object store.
1147 */
1148 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1149 return portNextObjStore;
1150 }
1151
1152 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001153 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1154 * Used to keep track on dummy VLAN ID allocation.
1155 *
1156 * @return dummy vlan id store.
1157 */
1158 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1159 return dummyVlanIdStore;
1160 }
1161
1162 /**
Saurav Das261c3002017-06-13 15:35:54 -07001163 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1164 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001165 *
1166 * @return MPLS-ECMP value
1167 */
1168 public boolean getMplsEcmp() {
1169 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1170 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1171 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1172 }
1173
1174 /**
sangho80f1f892015-05-19 11:57:42 -07001175 * Returns the tunnel object with the tunnel ID.
1176 *
1177 * @param tunnelId Tunnel ID
1178 * @return Tunnel reference
1179 */
sangho27462c62015-05-14 00:39:53 -07001180 public Tunnel getTunnel(String tunnelId) {
1181 return tunnelHandler.getTunnel(tunnelId);
1182 }
1183
Charles Chan8d316332018-06-19 20:31:57 -07001184 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001185 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001186 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1187 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001188 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1189 }
1190
Charles Chan8d316332018-06-19 20:31:57 -07001191 @Override
1192 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001193 SegmentRoutingDeviceConfig deviceConfig =
1194 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1195 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1196 }
Charles Chan8d316332018-06-19 20:31:57 -07001197
1198 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001199 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001200 SegmentRoutingDeviceConfig deviceConfig =
1201 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1202 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1203 }
1204
1205 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001206 * Returns locations of given resolved route.
1207 *
1208 * @param resolvedRoute resolved route
1209 * @return locations of nexthop. Might be empty if next hop is not found
1210 */
Charles Chan19b70032019-04-17 14:20:26 -07001211 public Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
Charles Chanf0ae41e2017-08-23 13:55:39 -07001212 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1213 return Optional.ofNullable(hostService.getHost(hostId))
1214 .map(Host::locations).orElse(Sets.newHashSet())
1215 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1216 }
1217
1218 /**
Charles Chan90772a72017-02-08 15:52:08 -08001219 * Returns vlan port map of given device.
1220 *
1221 * @param deviceId device id
1222 * @return vlan-port multimap
1223 */
1224 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1225 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1226
1227 interfaceService.getInterfaces().stream()
1228 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1229 .forEach(intf -> {
1230 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001231 intf.vlanTagged().forEach(vlanTagged ->
1232 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1233 );
Charles Chan90772a72017-02-08 15:52:08 -08001234 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1235 });
1236 vlanPortMap.removeAll(VlanId.NONE);
1237
1238 return vlanPortMap;
1239 }
1240
1241 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001242 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001243 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001244 *
1245 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001246 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001247 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001248 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001249 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001250 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001251 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1252 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001253 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001254 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001255 + "device {} not found", deviceId);
1256 return -1;
1257 }
1258 }
1259
1260 /**
1261 * Returns the next objective ID for the given portNumber, given the treatment.
1262 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001263 * would result in different objectives. If the next object does not exist,
1264 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001265 *
1266 * @param deviceId Device ID
1267 * @param portNum port number on device for which NextObjective is queried
1268 * @param treatment the actions to apply on the packets (should include outport)
1269 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001270 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001271 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001272 */
1273 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1274 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001275 TrafficSelector meta,
1276 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001277 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1278 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001279 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001280 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001281 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1282 + " not found", deviceId);
1283 return -1;
1284 }
1285 }
1286
Saurav Das62ae6792017-05-15 15:34:25 -07001287 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001288 * Returns the next Objective ID for the given mac and vlan, given the treatment.
1289 * There could be multiple different treatments to the same outport, which
1290 * would result in different objectives. If the next object does not exist,
1291 * and should be created, a new one is created and its id is returned.
1292 *
1293 * @param deviceId Device ID
1294 * @param macAddr mac of host for which Next ID is required.
1295 * @param vlanId vlan of host for which Next ID is required.
Ruchi Sahota07869322019-05-09 17:26:14 -04001296 * @param port port with which to create the Next Obj.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001297 * @param createIfMissing true if a next object should be created if not found
1298 * @return next objective ID or -1 if an error occurred during retrieval or creation
1299 */
1300 public int getMacVlanNextObjectiveId(DeviceId deviceId, MacAddress macAddr, VlanId vlanId,
Ruchi Sahota07869322019-05-09 17:26:14 -04001301 PortNumber port, boolean createIfMissing) {
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001302 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1303 if (ghdlr != null) {
Ruchi Sahota07869322019-05-09 17:26:14 -04001304 return ghdlr.getMacVlanNextObjectiveId(macAddr, vlanId, port, createIfMissing);
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001305 } else {
1306 log.warn("getMacVlanNextObjectiveId query - groupHandler for device {}"
1307 + " not found", deviceId);
1308 return -1;
1309 }
1310 }
1311
1312 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001313 * Updates the next objective for the given nextId .
1314 *
1315 * @param deviceId Device ID
1316 * @param hostMac mac of host for which Next obj is to be updated.
1317 * @param hostVlanId vlan of host for which Next obj is to be updated.
1318 * @param port port with which to update the Next Obj.
1319 * @param nextId of Next Obj which needs to be updated.
1320 */
1321 public void updateMacVlanTreatment(DeviceId deviceId, MacAddress hostMac,
1322 VlanId hostVlanId, PortNumber port, int nextId) {
1323 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1324 if (ghdlr != null) {
1325 ghdlr.updateL3UcastGroupBucket(hostMac, hostVlanId, port, nextId);
1326 } else {
1327 log.warn("updateL3UcastGroupBucket query - groupHandler for device {}"
1328 + " not found", deviceId);
1329 }
1330 }
1331
1332
1333 /**
Saurav Das62ae6792017-05-15 15:34:25 -07001334 * Returns the group handler object for the specified device id.
1335 *
1336 * @param devId the device identifier
1337 * @return the groupHandler object for the device id, or null if not found
1338 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001339 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001340 return groupHandlerMap.get(devId);
1341 }
1342
1343 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001344 * Returns the default routing handler object.
1345 *
1346 * @return the default routing handler object
1347 */
1348 public DefaultRoutingHandler getRoutingHandler() {
1349 return defaultRoutingHandler;
1350 }
1351
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001352 /**
1353 * Returns new dummy VLAN ID.
1354 * Dummy VLAN ID should be unique in each connect point.
1355 *
1356 * @param cp connect point
1357 * @param ipAddress IP address
1358 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1359 */
1360 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1361 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1362 dummyVlanIdStore.entrySet().stream().filter(entry ->
1363 (entry.getKey()).connectPoint().equals(cp))
1364 .map(Map.Entry::getValue)
1365 .collect(Collectors.toSet()));
1366
1367 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1368 i -> VlanId.vlanId((short) (i & 0xFFFF))
1369 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1370
1371 if (!dummyVlanId.equals(VlanId.NONE)) {
1372 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1373 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1374 } else {
1375 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1376 }
1377 return dummyVlanId;
1378 }
1379
1380
sangho80f11cb2015-04-01 13:05:26 -07001381 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001382 @Override
1383 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001384 packetExecutor.execute(() -> processPacketInternal(context));
1385 }
sangho80f11cb2015-04-01 13:05:26 -07001386
Charles Chan77cdde42018-05-08 21:35:50 -07001387 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001388 if (context.isHandled()) {
1389 return;
1390 }
1391
1392 InboundPacket pkt = context.inPacket();
1393 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001394
1395 if (ethernet == null) {
1396 return;
1397 }
1398
Saurav Das261c3002017-06-13 15:35:54 -07001399 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1400 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001401 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001402 log.warn("Received unexpected ARP packet on {}",
1403 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001404 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001405 return;
sangho80f11cb2015-04-01 13:05:26 -07001406 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001407 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1408 //ipHandler.addToPacketBuffer(ipv4Packet);
1409 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1410 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001411 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001412 // NOTE: We don't support IP learning at this moment so this
1413 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001414 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001415 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001416 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1417 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001418 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001419 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001420 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1421 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1422 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1423 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1424 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1425 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001426 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001427 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001428 }
1429 } else {
1430 // NOTE: We don't support IP learning at this moment so this
1431 // is not necessary. Also it causes duplication of DHCPv6 packets.
1432 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1433 }
sangho80f11cb2015-04-01 13:05:26 -07001434 }
1435 }
1436 }
1437
sangho80f11cb2015-04-01 13:05:26 -07001438 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001439 private Event event;
1440
1441 InternalEventHandler(Event event) {
1442 this.event = event;
1443 }
1444
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001445 @Override
sangho80f11cb2015-04-01 13:05:26 -07001446 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001447 try {
1448 // TODO We should also change SR routing and PW to listen to TopologyEvents
1449 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1450 event.type() == LinkEvent.Type.LINK_UPDATED) {
1451 linkHandler.processLinkAdded((Link) event.subject());
1452 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1453 linkHandler.processLinkRemoved((Link) event.subject());
1454 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1455 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1456 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1457 DeviceId deviceId = ((Device) event.subject()).id();
1458 if (deviceService.isAvailable(deviceId)) {
1459 log.info("** DEVICE UP Processing device event {} "
1460 + "for available device {}",
1461 event.type(), ((Device) event.subject()).id());
1462 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001463 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001464 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1465 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1466 // The device will later on being marked as available once the pipeline is pushed
1467 // to the device.
1468 log.info("** DEVICE ADDED but unavailable. Ignore");
1469 return;
1470 }
Charles Chan52003922018-04-05 16:31:15 -07001471 log.info(" ** DEVICE DOWN Processing device event {}"
1472 + " for unavailable device {}",
1473 event.type(), ((Device) event.subject()).id());
1474 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001475 }
Charles Chan52003922018-04-05 16:31:15 -07001476 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1477 // typically these calls come when device is added first time
1478 // so port filtering rules are handled at the device_added event.
1479 // port added calls represent all ports on the device,
1480 // enabled or not.
1481 log.trace("** PORT ADDED {}/{} -> {}",
1482 ((DeviceEvent) event).subject().id(),
1483 ((DeviceEvent) event).port().number(),
1484 event.type());
1485 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1486 // these calls happen for every subsequent event
1487 // ports enabled, disabled, switch goes away, comes back
1488 log.info("** PORT UPDATED {}/{} -> {}",
1489 event.subject(),
1490 ((DeviceEvent) event).port(),
1491 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001492 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001493 ((DeviceEvent) event).port());
1494 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1495 // Process topology event, needed for all modules relying on
1496 // topology service for path computation
1497 TopologyEvent topologyEvent = (TopologyEvent) event;
1498 log.info("Processing topology event {}, topology age {}, reasons {}",
1499 event.type(), topologyEvent.subject().time(),
1500 topologyEvent.reasons().size());
1501 topologyHandler.processTopologyChange(topologyEvent.reasons());
1502 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1503 hostHandler.processHostAddedEvent((HostEvent) event);
1504 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1505 hostHandler.processHostMovedEvent((HostEvent) event);
1506 routeHandler.processHostMovedEvent((HostEvent) event);
1507 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1508 hostHandler.processHostRemovedEvent((HostEvent) event);
1509 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1510 hostHandler.processHostUpdatedEvent((HostEvent) event);
1511 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1512 routeHandler.processRouteAdded((RouteEvent) event);
1513 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1514 routeHandler.processRouteUpdated((RouteEvent) event);
1515 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1516 routeHandler.processRouteRemoved((RouteEvent) event);
1517 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1518 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1519 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1520 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1521 event.type() == McastEvent.Type.SINKS_ADDED ||
1522 event.type() == McastEvent.Type.SINKS_REMOVED ||
1523 event.type() == McastEvent.Type.ROUTE_ADDED ||
1524 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1525 mcastHandler.processMcastEvent((McastEvent) event);
1526 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1527 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1528 Class configClass = netcfgEvent.configClass();
1529 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1530 appCfgHandler.processAppConfigAdded(netcfgEvent);
1531 log.info("App config event .. configuring network");
1532 cfgListener.configureNetwork();
1533 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1534 log.info("Segment Routing Device Config added for {}", event.subject());
1535 cfgListener.configureNetwork();
Charles Chan52003922018-04-05 16:31:15 -07001536 } else if (configClass.equals(InterfaceConfig.class)) {
1537 log.info("Interface Config added for {}", event.subject());
1538 cfgListener.configureNetwork();
1539 } else {
1540 log.error("Unhandled config class: {}", configClass);
1541 }
1542 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1543 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1544 Class configClass = netcfgEvent.configClass();
1545 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1546 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1547 log.info("App config event .. configuring network");
1548 cfgListener.configureNetwork();
1549 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1550 log.info("Segment Routing Device Config updated for {}", event.subject());
1551 createOrUpdateDeviceConfiguration();
Charles Chan52003922018-04-05 16:31:15 -07001552 } else if (configClass.equals(InterfaceConfig.class)) {
1553 log.info("Interface Config updated for {}", event.subject());
1554 createOrUpdateDeviceConfiguration();
1555 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1556 (InterfaceConfig) netcfgEvent.prevConfig().get());
1557 } else {
1558 log.error("Unhandled config class: {}", configClass);
1559 }
1560 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1561 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1562 Class configClass = netcfgEvent.configClass();
1563 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1564 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1565 log.info("App config event .. configuring network");
1566 cfgListener.configureNetwork();
1567 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1568 // TODO Handle sr device config removal
1569 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
Charles Chan52003922018-04-05 16:31:15 -07001570 } else if (configClass.equals(InterfaceConfig.class)) {
1571 // TODO Handle interface removal
1572 log.info("InterfaceConfig removal is not handled in current implementation");
1573 } else {
1574 log.error("Unhandled config class: {}", configClass);
1575 }
Saurav Das00e553b2018-04-21 17:19:48 -07001576 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1577 MastershipEvent me = (MastershipEvent) event;
1578 DeviceId deviceId = me.subject();
1579 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1580 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1581 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1582 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1583 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1584 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001585 } else {
1586 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001587 }
Charles Chan52003922018-04-05 16:31:15 -07001588 } catch (Exception e) {
1589 log.error("SegmentRouting event handler thread thrown an exception: {}",
1590 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001591 }
sangho80f11cb2015-04-01 13:05:26 -07001592 }
1593 }
1594
Saurav Dase6c448a2018-01-18 12:07:33 -08001595 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001596 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001597
1598 // NOTE: Punt ARP/NDP even when the device is not configured.
1599 // Host learning without network config is required for CORD config generator.
1600 routingRulePopulator.populateIpPunts(device.id());
1601 routingRulePopulator.populateArpNdpPunts(device.id());
1602
Charles Chan319d1a22015-11-03 10:42:14 -08001603 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001604 log.warn("Device configuration unavailable. Device {} will be "
1605 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001606 return;
1607 }
Charles Chan72779502016-04-23 17:36:10 -07001608 processDeviceAddedInternal(device.id());
1609 }
1610
1611 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001612 // Irrespective of whether the local is a MASTER or not for this device,
1613 // we need to create a SR-group-handler instance. This is because in a
1614 // multi-instance setup, any instance can initiate forwarding/next-objectives
1615 // for any switch (even if this instance is a SLAVE or not even connected
1616 // to the switch). To handle this, a default-group-handler instance is necessary
1617 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001618 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1619 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001620 DefaultGroupHandler groupHandler;
1621 try {
1622 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001623 createGroupHandler(deviceId,
1624 appId,
1625 deviceConfiguration,
1626 linkService,
1627 flowObjectiveService,
1628 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001629 } catch (DeviceConfigNotFoundException e) {
1630 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1631 return;
1632 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001633 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001634 deviceId);
1635 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001636 }
Saurav Dasb149be12016-06-07 10:08:06 -07001637
Charles Chan72779502016-04-23 17:36:10 -07001638 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001639 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001640 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001641 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001642 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001643 }
Charles Chan82ab1932016-01-30 23:22:37 -08001644
Charles Chandebfea32016-10-24 14:52:01 -07001645 appCfgHandler.init(deviceId);
Charles Chan074fae62018-04-29 18:11:37 -07001646 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1647 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sangho80f11cb2015-04-01 13:05:26 -07001648 }
1649
Saurav Dasc3604f12016-03-23 11:22:49 -07001650 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001651 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001652 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001653 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001654 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001655 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001656 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001657 macVlanNextObjStore.entrySet().stream()
1658 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1659 .forEach(entry -> macVlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001660 portNextObjStore.entrySet().stream()
1661 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001662 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001663 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001664
Saurav Dasfbe74572017-08-03 18:30:35 -07001665 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1666 if (gh != null) {
1667 gh.shutdown();
1668 }
Saurav Das62ae6792017-05-15 15:34:25 -07001669 // Note that a switch going down is associated with all of its links
1670 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001671 // while the link-downs are ignored. We cannot rely on the ordering of
1672 // events - i.e we cannot expect all link-downs to come before the
1673 // switch down - so we purge all seen-links for the switch before
1674 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001675 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001676 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001677 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001678
1679 // Cleanup all internal groupHandler stores for this device. Should be
1680 // done after all rerouting or rehashing has been completed
1681 groupHandlerMap.entrySet()
1682 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001683 }
1684
Saurav Das49368392018-04-23 18:42:12 -07001685 /**
1686 * Purge the destinationSet nextObjective store of entries with this device
1687 * as key. Erases app-level knowledge of hashed groups in this device.
1688 *
1689 * @param devId the device identifier
1690 */
Saurav Das00e553b2018-04-21 17:19:48 -07001691 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001692 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001693 dsNextObjStore.entrySet().stream()
1694 .filter(entry -> entry.getKey().deviceId().equals(devId))
1695 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1696 }
1697
Saurav Dasec683dc2018-04-27 18:42:30 -07001698 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001699 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1700 log.warn("Device configuration uploading. Not handling port event for"
1701 + "dev: {} port: {}", device.id(), port.number());
1702 return;
1703 }
Saurav Dasf9332192017-02-18 14:05:44 -08001704
Saurav Dasec683dc2018-04-27 18:42:30 -07001705 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1706 lastEdgePortEvent = Instant.now();
1707 }
1708
Saurav Dasf9332192017-02-18 14:05:44 -08001709 if (!mastershipService.isLocalMaster(device.id())) {
1710 log.debug("Not master for dev:{} .. not handling port updated event"
1711 + "for port {}", device.id(), port.number());
1712 return;
1713 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001714 processPortUpdated(device.id(), port);
1715 }
Saurav Dasf9332192017-02-18 14:05:44 -08001716
Saurav Dasec683dc2018-04-27 18:42:30 -07001717 /**
1718 * Adds or remove filtering rules for the given switchport. If switchport is
1719 * an edge facing port, additionally handles host probing and broadcast
1720 * rules. Must be called by local master of device.
1721 *
1722 * @param deviceId the device identifier
1723 * @param port the port to update
1724 */
1725 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001726 // first we handle filtering rules associated with the port
1727 if (port.isEnabled()) {
1728 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001729 deviceId, port.number());
1730 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001731 } else {
1732 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001733 deviceId, port.number());
1734 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001735 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001736
1737 // portUpdated calls are for ports that have gone down or up. For switch
1738 // to switch ports, link-events should take care of any re-routing or
1739 // group editing necessary for port up/down. Here we only process edge ports
1740 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001741 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001742 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1743 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1744 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001745
Saurav Das3fb28272017-03-04 16:08:47 -08001746 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001747 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001748 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001749 return;
1750 }
Saurav Das3fb28272017-03-04 16:08:47 -08001751 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001752 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001753 }
1754 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001755 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001756 }
1757 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001758 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001759 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001760 }
1761
Saurav Dasec683dc2018-04-27 18:42:30 -07001762 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001763 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001764 boolean portUp = port.isEnabled();
1765 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001766 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001767 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001768 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001769 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001770 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001771 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001772 }
1773
Saurav Dasec683dc2018-04-27 18:42:30 -07001774 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001775 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001776 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001777 } else {
1778 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001779 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001780 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001781 }
1782 }
sangho27462c62015-05-14 00:39:53 -07001783
Charles Chan8ca5a122017-10-20 16:06:55 -07001784 private void createOrUpdateDeviceConfiguration() {
1785 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001786 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001787 deviceConfiguration = new DeviceConfiguration(this);
1788 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001789 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001790 deviceConfiguration.updateConfig();
1791 }
1792 }
1793
Charles Chanfbcb8812018-04-18 18:41:05 -07001794 private void createOrUpdateDefaultRoutingHandler() {
1795 if (defaultRoutingHandler == null) {
1796 log.info("Creating new DefaultRoutingHandler");
1797 defaultRoutingHandler = new DefaultRoutingHandler(this);
1798 } else {
1799 log.info("Updating DefaultRoutingHandler");
1800 defaultRoutingHandler.update(this);
1801 }
1802 }
1803
Pier Ventreb6a7f342016-11-26 21:05:22 -08001804 /**
1805 * Registers the given connect point with the NRS, this is necessary
1806 * to receive the NDP and ARP packets from the NRS.
1807 *
1808 * @param portToRegister connect point to register
1809 */
1810 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001811 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001812 portToRegister,
1813 neighbourHandler,
1814 appId
1815 );
1816 }
1817
Charles Chan72f556a2015-10-05 17:50:33 -07001818 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001819 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001820 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001821
Charles Chanb7f75ac2016-01-11 18:28:54 -08001822 /**
1823 * Constructs the internal network config listener.
1824 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001825 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001826 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001827 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001828 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001829 }
1830
Charles Chanb7f75ac2016-01-11 18:28:54 -08001831 /**
1832 * Reads network config and initializes related data structure accordingly.
1833 */
Charles Chan873661e2017-11-30 15:37:50 -08001834 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001835 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001836
1837 // Setting handling of network configuration events completable future
1838 // The completable future is needed because of the async behaviour of the configureNetwork,
1839 // listener registration and event arrival
1840 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkey521e4392018-11-16 15:15:14 -08001841 synchronized (networkConfigCompletionLock) {
1842 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella060cad82018-04-17 12:09:34 +02001843
Ray Milkey521e4392018-11-16 15:15:14 -08001844 // add a small delay to absorb multiple network config added notifications
1845 if (!programmingScheduled.get()) {
1846 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1847 programmingScheduled.set(true);
1848 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1849 }
1850
1851 createOrUpdateDeviceConfiguration();
1852
1853 arpHandler = new ArpHandler(srManager);
1854 icmpHandler = new IcmpHandler(srManager);
1855 ipHandler = new IpHandler(srManager);
1856 routingRulePopulator = new RoutingRulePopulator(srManager);
1857 createOrUpdateDefaultRoutingHandler();
1858
1859 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1860 groupHandlerMap, tunnelStore);
1861 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1862 flowObjectiveService,
1863 tunnelHandler, policyStore);
1864 networkConfigCompletion.complete(true);
Andrea Campanella060cad82018-04-17 12:09:34 +02001865 }
1866
Charles Chan72779502016-04-23 17:36:10 -07001867 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001868
Charles Chane7c61022015-10-07 14:21:45 -07001869 }
1870
Charles Chan72f556a2015-10-05 17:50:33 -07001871 @Override
1872 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001873 if (mainEventExecutor == null) {
1874 return;
1875 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001876 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan3f4aca62018-03-21 16:57:47 -07001877 switch (event.type()) {
1878 case CONFIG_ADDED:
1879 case CONFIG_UPDATED:
1880 case CONFIG_REMOVED:
1881 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001882 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1883 mainEventExecutor.execute(new InternalEventHandler(event));
1884 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001885 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001886 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001887 break;
1888 default:
1889 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001890 }
1891 }
Saurav Das261c3002017-06-13 15:35:54 -07001892
Charles Chanc4a68c32018-01-03 16:26:32 -08001893 @Override
1894 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001895 if (event.type() == CONFIG_REGISTERED ||
1896 event.type() == CONFIG_UNREGISTERED) {
1897 log.debug("Ignore event {} due to type mismatch", event);
1898 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001899 }
Saurav Dase321cff2018-02-09 17:26:45 -08001900
1901 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1902 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
Charles Chana01d57c2019-07-19 16:25:38 -07001903 !event.configClass().equals(InterfaceConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001904 log.debug("Ignore event {} due to class mismatch", event);
1905 return false;
1906 }
1907
1908 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001909 }
1910
Saurav Das261c3002017-06-13 15:35:54 -07001911 private final class ConfigChange implements Runnable {
1912 @Override
1913 public void run() {
1914 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001915 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001916 for (Device device : deviceService.getDevices()) {
1917 processDeviceAdded(device);
1918 }
1919 defaultRoutingHandler.startPopulationProcess();
1920 }
1921 }
Charles Chan72f556a2015-10-05 17:50:33 -07001922 }
Charles Chanf4586112015-11-09 16:37:23 -08001923
Charles Chan3f4aca62018-03-21 16:57:47 -07001924 private class InternalLinkListener implements LinkListener {
1925 @Override
1926 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001927 if (mainEventExecutor == null) {
1928 return;
1929 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001930 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1931 event.type() == LinkEvent.Type.LINK_UPDATED ||
1932 event.type() == LinkEvent.Type.LINK_REMOVED) {
1933 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001934 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1935 mainEventExecutor.execute(new InternalEventHandler(event));
1936 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001937 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001938 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001939 }
1940 }
1941 }
1942
1943 private class InternalDeviceListener implements DeviceListener {
1944 @Override
1945 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001946 if (mainEventExecutor == null) {
1947 return;
1948 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001949 switch (event.type()) {
1950 case DEVICE_ADDED:
1951 case PORT_UPDATED:
1952 case PORT_ADDED:
1953 case DEVICE_UPDATED:
1954 case DEVICE_AVAILABILITY_CHANGED:
1955 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001956 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1957 mainEventExecutor.execute(new InternalEventHandler(event));
1958 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001959 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001960 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001961 break;
1962 default:
1963 }
1964 }
1965 }
1966
1967 private class InternalTopologyListener implements TopologyListener {
1968 @Override
1969 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001970 if (mainEventExecutor == null) {
1971 return;
1972 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001973 switch (event.type()) {
1974 case TOPOLOGY_CHANGED:
1975 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001976 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1977 mainEventExecutor.execute(new InternalEventHandler(event));
1978 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001979 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001980 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001981 break;
1982 default:
1983 }
1984 }
1985 }
1986
Charles Chanf4586112015-11-09 16:37:23 -08001987 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001988 @Override
1989 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001990 if (hostEventExecutor == null) {
1991 return;
1992 }
Charles Chanf4586112015-11-09 16:37:23 -08001993 switch (event.type()) {
1994 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001995 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001996 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001997 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001998 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001999 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08002000 break;
2001 default:
2002 log.warn("Unsupported host event type: {}", event.type());
2003 break;
2004 }
2005 }
2006 }
2007
Charles Chanc91c8782016-03-30 17:54:24 -07002008 private class InternalMcastListener implements McastListener {
2009 @Override
2010 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002011 if (mcastEventExecutor == null) {
2012 return;
2013 }
Charles Chanc91c8782016-03-30 17:54:24 -07002014 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07002015 case SOURCES_ADDED:
2016 case SOURCES_REMOVED:
2017 case SINKS_ADDED:
2018 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07002019 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02002020 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07002021 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07002022 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01002023 break;
Charles Chanc91c8782016-03-30 17:54:24 -07002024 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07002025 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07002026 break;
2027 }
2028 }
2029 }
Charles Chan41f5ec02016-06-13 18:54:31 -07002030
Charles Chandebfea32016-10-24 14:52:01 -07002031 private class InternalRouteEventListener implements RouteListener {
2032 @Override
2033 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002034 if (routeEventExecutor == null) {
2035 return;
2036 }
Charles Chandebfea32016-10-24 14:52:01 -07002037 switch (event.type()) {
2038 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07002039 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07002040 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07002041 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07002042 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07002043 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07002044 break;
Charles Chandebfea32016-10-24 14:52:01 -07002045 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07002046 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07002047 break;
2048 }
2049 }
2050 }
Saurav Das62ae6792017-05-15 15:34:25 -07002051
Charles Chanfbcb8812018-04-18 18:41:05 -07002052 private class InternalMastershipListener implements MastershipListener {
2053 @Override
2054 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002055 if (mainEventExecutor == null) {
2056 return;
2057 }
Charles Chanfbcb8812018-04-18 18:41:05 -07002058 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07002059 case MASTER_CHANGED:
2060 log.debug("Mastership event: {}/{}", event.subject(),
2061 event.roleInfo());
2062 mainEventExecutor.execute(new InternalEventHandler(event));
2063 break;
2064 case BACKUPS_CHANGED:
2065 case SUSPENDED:
2066 default:
2067 log.debug("Mastership event type {} not handled", event.type());
2068 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07002069 }
2070 }
2071 }
2072
Saurav Das00e553b2018-04-21 17:19:48 -07002073 class InternalClusterListener implements ClusterEventListener {
2074 private Instant lastClusterEvent = Instant.EPOCH;
2075
2076 long timeSinceLastClusterEvent() {
2077 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2078 }
2079
2080 @Override
2081 public void event(ClusterEvent event) {
2082 switch (event.type()) {
2083 case INSTANCE_ACTIVATED:
2084 case INSTANCE_ADDED:
2085 case INSTANCE_READY:
2086 log.debug("Cluster event {} ignored", event.type());
2087 break;
2088 case INSTANCE_DEACTIVATED:
2089 case INSTANCE_REMOVED:
2090 log.info("** Cluster event {}", event.type());
2091 lastClusterEvent = Instant.now();
2092 break;
2093 default:
2094 break;
2095 }
2096
2097 }
2098
2099 }
2100
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002101 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2102 try {
2103 Set<Interface> intfs = conf.getInterfaces();
2104 Set<Interface> prevIntfs = prevConf.getInterfaces();
2105
2106 // Now we only handle one interface config at each port.
2107 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2108 log.warn("Interface update aborted - one at a time is allowed, " +
2109 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2110 return;
2111 }
2112
Andrea Campanella060cad82018-04-17 12:09:34 +02002113 //The system is in an incoherent state, abort
2114 if (defaultRoutingHandler == null) {
2115 log.warn("Interface update aborted, defaultRoutingHandler is null");
2116 return;
2117 }
2118
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002119 Interface intf = intfs.stream().findFirst().get();
2120 Interface prevIntf = prevIntfs.stream().findFirst().get();
2121
2122 DeviceId deviceId = intf.connectPoint().deviceId();
2123 PortNumber portNum = intf.connectPoint().port();
2124
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002125 removeSubnetConfig(prevIntf.connectPoint(),
2126 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2127 new HashSet<>(intf.ipAddressesList())));
2128
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002129 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2130 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2131 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2132 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2133 // Update filtering objective and L2IG group bucket
2134 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2135 } else {
2136 // RemoveVlanNative
2137 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2138 }
2139 }
2140
2141 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2142 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2143 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2144 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2145 // Update filtering objective and L2IG group bucket
2146 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2147 } else {
2148 // RemoveVlanUntagged
2149 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2150 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002151 }
2152
2153 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2154 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002155 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2156 .filter(i -> !intf.vlanUntagged().equals(i))
2157 .filter(i -> !intf.vlanNative().equals(i))
2158 .forEach(vlanId -> updateVlanConfigInternal(
2159 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002160 }
2161
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002162 if (!intf.vlanNative().equals(VlanId.NONE)
2163 && !prevIntf.vlanNative().equals(intf.vlanNative())
2164 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2165 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2166 // Update filtering objective and L2IG group bucket
2167 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2168 } else {
2169 // AddVlanNative
2170 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2171 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002172 }
2173
2174 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2175 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002176 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2177 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2178 .filter(i -> !prevIntf.vlanNative().equals(i))
2179 .forEach(vlanId -> updateVlanConfigInternal(
2180 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002181 );
2182 }
2183
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002184 if (!intf.vlanUntagged().equals(VlanId.NONE)
2185 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2186 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2187 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2188 // Update filtering objective and L2IG group bucket
2189 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2190 } else {
2191 // AddVlanUntagged
2192 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2193 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002194 }
2195 addSubnetConfig(prevIntf.connectPoint(),
2196 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2197 new HashSet<>(prevIntf.ipAddressesList())));
2198 } catch (ConfigException e) {
2199 log.error("Error in configuration");
2200 }
2201 }
2202
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002203 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2204 VlanId vlanId, boolean pushVlan) {
2205 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2206 if (grpHandler == null) {
2207 log.warn("Failed to retrieve group handler for device {}", deviceId);
2208 return;
2209 }
2210
2211 // Update filtering objective for a single port
2212 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2213 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2214
2215 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2216 // Update L2IG bucket of the port
2217 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2218 } else {
2219 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2220 }
2221 }
2222
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002223 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2224 VlanId vlanId, boolean pushVlan, boolean install) {
2225 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2226 if (grpHandler == null) {
2227 log.warn("Failed to retrieve group handler for device {}", deviceId);
2228 return;
2229 }
2230
2231 // Update filtering objective for a single port
2232 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2233
2234 // Update filtering objective for multicast ingress port
2235 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2236
2237 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2238
2239 if (nextId != -1 && !install) {
2240 // Update next objective for a single port as an output port
2241 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002242 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002243 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07002244 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2245 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002246 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2247 // only if there is no port configured on that VLAN ID
2248 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2249 // Remove broadcast forwarding rule for the VLAN
2250 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2251 // Remove L2FG for VLAN
2252 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2253 } else {
2254 // Remove L2IG of the port
2255 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2256 }
2257 } else if (install) {
2258 if (nextId != -1) {
2259 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002260 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002261 } else {
2262 // Create L2FG for VLAN
2263 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2264 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2265 }
Charles Chan074fae62018-04-29 18:11:37 -07002266 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2267 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002268 } else {
2269 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2270 }
2271 }
2272
2273 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2274 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2275 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2276
2277 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2278 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2279 .filter(intf -> !intf.connectPoint().equals(cp))
2280 .flatMap(intf -> intf.ipAddressesList().stream())
2281 .collect(Collectors.toSet());
2282 // 1. Partial subnet population
2283 // Remove routing rules for removed subnet from previous configuration,
2284 // which does not also exist in other interfaces in the same device
2285 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2286 .map(InterfaceIpAddress::subnetAddress)
2287 .collect(Collectors.toSet());
2288
2289 defaultRoutingHandler.revokeSubnet(
2290 ipPrefixSet.stream()
2291 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2292 .collect(Collectors.toSet()));
2293
2294 // 2. Interface IP punts
2295 // Remove IP punts for old Intf address
2296 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2297 .map(InterfaceIpAddress::ipAddress)
2298 .collect(Collectors.toSet());
2299 ipAddressSet.stream()
2300 .map(InterfaceIpAddress::ipAddress)
2301 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2302 .forEach(interfaceIpAddress ->
2303 routingRulePopulator.revokeSingleIpPunts(
2304 cp.deviceId(), interfaceIpAddress));
2305
2306 // 3. Host unicast routing rule
2307 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002308 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002309 }
2310
2311 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2312 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2313 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2314
2315 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2316 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2317 .filter(intf -> !intf.connectPoint().equals(cp))
2318 .flatMap(intf -> intf.ipAddressesList().stream())
2319 .collect(Collectors.toSet());
2320 // 1. Partial subnet population
2321 // Add routing rules for newly added subnet, which does not also exist in
2322 // other interfaces in the same device
2323 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2324 .map(InterfaceIpAddress::subnetAddress)
2325 .collect(Collectors.toSet());
2326
2327 defaultRoutingHandler.populateSubnet(
2328 Collections.singleton(cp),
2329 ipPrefixSet.stream()
2330 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2331 .collect(Collectors.toSet()));
2332
2333 // 2. Interface IP punts
2334 // Add IP punts for new Intf address
2335 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2336 .map(InterfaceIpAddress::ipAddress)
2337 .collect(Collectors.toSet());
2338 ipAddressSet.stream()
2339 .map(InterfaceIpAddress::ipAddress)
2340 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2341 .forEach(interfaceIpAddress ->
2342 routingRulePopulator.populateSingleIpPunts(
2343 cp.deviceId(), interfaceIpAddress));
2344
2345 // 3. Host unicast routing rule
2346 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002347 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002348 }
sangho80f11cb2015-04-01 13:05:26 -07002349}