sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.segmentrouting; |
| 17 | |
Charles Chan | d7844e5 | 2016-10-20 17:02:44 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Service; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IPv4; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 27 | import org.onlab.packet.Ip4Prefix; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 28 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 29 | import org.onlab.packet.VlanId; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 30 | import org.onlab.util.KryoNamespace; |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 31 | import org.onosproject.cfg.ComponentConfigService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
| 34 | import org.onosproject.event.Event; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 35 | import org.onosproject.incubator.net.config.basics.McastConfig; |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 36 | import org.onosproject.incubator.net.intf.InterfaceService; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 37 | import org.onosproject.mastership.MastershipService; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 38 | import org.onosproject.net.Device; |
| 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.onosproject.net.Link; |
| 41 | import org.onosproject.net.Port; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 42 | import org.onosproject.net.PortNumber; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 43 | import org.onosproject.net.config.ConfigFactory; |
| 44 | import org.onosproject.net.config.NetworkConfigEvent; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 45 | import org.onosproject.net.config.NetworkConfigListener; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 46 | import org.onosproject.net.config.NetworkConfigRegistry; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 47 | import org.onosproject.net.config.basics.SubjectFactories; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 48 | import org.onosproject.net.device.DeviceEvent; |
| 49 | import org.onosproject.net.device.DeviceListener; |
| 50 | import org.onosproject.net.device.DeviceService; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 52 | import org.onosproject.net.flow.TrafficSelector; |
| 53 | import org.onosproject.net.flow.TrafficTreatment; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 54 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 55 | import org.onosproject.net.host.HostEvent; |
| 56 | import org.onosproject.net.host.HostListener; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 57 | import org.onosproject.net.mcast.McastEvent; |
| 58 | import org.onosproject.net.mcast.McastListener; |
| 59 | import org.onosproject.net.mcast.MulticastRouteService; |
| 60 | import org.onosproject.net.packet.PacketPriority; |
| 61 | import org.onosproject.net.topology.TopologyService; |
| 62 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 63 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
| 64 | import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig; |
| 65 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 66 | import org.onosproject.segmentrouting.config.XConnectConfig; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 67 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
| 68 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 69 | import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey; |
| 70 | import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 71 | import org.onosproject.net.host.HostService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 72 | import org.onosproject.net.link.LinkEvent; |
| 73 | import org.onosproject.net.link.LinkListener; |
| 74 | import org.onosproject.net.link.LinkService; |
| 75 | import org.onosproject.net.packet.InboundPacket; |
| 76 | import org.onosproject.net.packet.PacketContext; |
| 77 | import org.onosproject.net.packet.PacketProcessor; |
| 78 | import org.onosproject.net.packet.PacketService; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 79 | import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey; |
| 80 | import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 81 | import org.onosproject.segmentrouting.storekey.XConnectStoreKey; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 82 | import org.onosproject.store.serializers.KryoNamespaces; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 83 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 84 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 85 | import org.onosproject.store.service.StorageService; |
| 86 | import org.onosproject.store.service.WallClockTimestamp; |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 87 | import org.opencord.cordconfig.CordConfigEvent; |
| 88 | import org.opencord.cordconfig.CordConfigListener; |
| 89 | import org.opencord.cordconfig.CordConfigService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 90 | import org.slf4j.Logger; |
| 91 | import org.slf4j.LoggerFactory; |
| 92 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 93 | import java.util.Collections; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 94 | import java.util.HashSet; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 95 | import java.util.List; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 96 | import java.util.Map; |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 97 | import java.util.Optional; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 98 | import java.util.Set; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 99 | import java.util.concurrent.ConcurrentHashMap; |
| 100 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 101 | import java.util.concurrent.Executors; |
| 102 | import java.util.concurrent.ScheduledExecutorService; |
| 103 | import java.util.concurrent.ScheduledFuture; |
| 104 | import java.util.concurrent.TimeUnit; |
| 105 | |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 106 | import static com.google.common.base.Preconditions.checkState; |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 107 | import static org.onlab.util.Tools.groupedThreads; |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 108 | |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 109 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 110 | /** |
| 111 | * Segment routing manager. |
| 112 | */ |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 113 | @Service |
| 114 | @Component(immediate = true) |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 115 | public class SegmentRoutingManager implements SegmentRoutingService { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 116 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 117 | private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 120 | protected CoreService coreService; |
| 121 | |
| 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 123 | protected PacketService packetService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 126 | protected HostService hostService; |
| 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | protected DeviceService deviceService; |
| 130 | |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 132 | protected FlowObjectiveService flowObjectiveService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 133 | |
| 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 135 | protected LinkService linkService; |
| 136 | |
| 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 138 | protected MastershipService mastershipService; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 139 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 141 | protected StorageService storageService; |
| 142 | |
| 143 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 144 | public NetworkConfigRegistry cfgService; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 145 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 147 | protected ComponentConfigService compCfgService; |
| 148 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 149 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 150 | protected MulticastRouteService multicastRouteService; |
| 151 | |
| 152 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 153 | protected TopologyService topologyService; |
| 154 | |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 155 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 156 | protected CordConfigService cordConfigService; |
| 157 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 158 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 159 | public InterfaceService interfaceService; |
| 160 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 161 | protected ArpHandler arpHandler = null; |
| 162 | protected IcmpHandler icmpHandler = null; |
| 163 | protected IpHandler ipHandler = null; |
| 164 | protected RoutingRulePopulator routingRulePopulator = null; |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 165 | public ApplicationId appId; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 166 | protected DeviceConfiguration deviceConfiguration = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 167 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 168 | protected DefaultRoutingHandler defaultRoutingHandler = null; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 169 | private TunnelHandler tunnelHandler = null; |
| 170 | private PolicyHandler policyHandler = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 171 | private InternalPacketProcessor processor = null; |
| 172 | private InternalLinkListener linkListener = null; |
| 173 | private InternalDeviceListener deviceListener = null; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 174 | private AppConfigHandler appCfgHandler = null; |
| 175 | protected XConnectHandler xConnectHandler = null; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 176 | private McastHandler mcastHandler = null; |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 177 | protected HostHandler hostHandler = null; |
| 178 | private CordConfigHandler cordConfigHandler = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 179 | private InternalEventHandler eventHandler = new InternalEventHandler(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 180 | private final InternalHostListener hostListener = new InternalHostListener(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 181 | private final InternalConfigListener cfgListener = new InternalConfigListener(this); |
| 182 | private final InternalMcastListener mcastListener = new InternalMcastListener(); |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 183 | private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 184 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 185 | private ScheduledExecutorService executorService = Executors |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 186 | .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 187 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 188 | @SuppressWarnings("unused") |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 189 | private static ScheduledFuture<?> eventHandlerFuture = null; |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 190 | @SuppressWarnings("rawtypes") |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 191 | private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>(); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 192 | private Map<DeviceId, DefaultGroupHandler> groupHandlerMap = |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 193 | new ConcurrentHashMap<>(); |
| 194 | /** |
| 195 | * Per device next objective ID store with (device id + neighbor set) as key. |
| 196 | */ |
| 197 | public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer> |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 198 | nsNextObjStore = null; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 199 | /** |
| 200 | * Per device next objective ID store with (device id + subnet) as key. |
| 201 | */ |
| 202 | public EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer> |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 203 | subnetNextObjStore = null; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 204 | /** |
| 205 | * Per device next objective ID store with (device id + port) as key. |
| 206 | */ |
| 207 | public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 208 | portNextObjStore = null; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 209 | // Per device, per-subnet assigned-vlans store, with (device id + subnet |
| 210 | // IPv4 prefix) as key |
| 211 | private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId> |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 212 | subnetVidStore = null; |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 213 | private EventuallyConsistentMap<String, Tunnel> tunnelStore = null; |
| 214 | private EventuallyConsistentMap<String, Policy> policyStore = null; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 215 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 216 | private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory = |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 217 | new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>( |
| 218 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 219 | SegmentRoutingDeviceConfig.class, "segmentrouting") { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 220 | @Override |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 221 | public SegmentRoutingDeviceConfig createConfig() { |
| 222 | return new SegmentRoutingDeviceConfig(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 223 | } |
| 224 | }; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 225 | private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory = |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 226 | new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>( |
| 227 | SubjectFactories.APP_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 228 | SegmentRoutingAppConfig.class, "segmentrouting") { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 229 | @Override |
| 230 | public SegmentRoutingAppConfig createConfig() { |
| 231 | return new SegmentRoutingAppConfig(); |
| 232 | } |
| 233 | }; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 234 | private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory = |
| 235 | new ConfigFactory<ApplicationId, XConnectConfig>( |
| 236 | SubjectFactories.APP_SUBJECT_FACTORY, |
| 237 | XConnectConfig.class, "xconnect") { |
| 238 | @Override |
| 239 | public XConnectConfig createConfig() { |
| 240 | return new XConnectConfig(); |
| 241 | } |
| 242 | }; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 243 | private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory = |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 244 | new ConfigFactory<ApplicationId, McastConfig>( |
| 245 | SubjectFactories.APP_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 246 | McastConfig.class, "multicast") { |
| 247 | @Override |
| 248 | public McastConfig createConfig() { |
| 249 | return new McastConfig(); |
| 250 | } |
| 251 | }; |
| 252 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 253 | private Object threadSchedulerLock = new Object(); |
| 254 | private static int numOfEventsQueued = 0; |
| 255 | private static int numOfEventsExecuted = 0; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 256 | private static int numOfHandlerExecution = 0; |
| 257 | private static int numOfHandlerScheduled = 0; |
| 258 | |
Charles Chan | 1963f4f | 2016-02-18 14:22:42 -0800 | [diff] [blame] | 259 | /** |
| 260 | * Segment Routing App ID. |
| 261 | */ |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 262 | public static final String APP_NAME = "org.onosproject.segmentrouting"; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 263 | /** |
| 264 | * The starting value of per-subnet VLAN ID assignment. |
| 265 | */ |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 266 | private static final short ASSIGNED_VLAN_START = 4093; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 267 | /** |
| 268 | * The default VLAN ID assigned to the interfaces without subnet config. |
| 269 | */ |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 270 | public static final short ASSIGNED_VLAN_NO_SUBNET = 4094; |
| 271 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 272 | @Activate |
| 273 | protected void activate() { |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 274 | appId = coreService.registerApplication(APP_NAME); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 275 | |
| 276 | log.debug("Creating EC map nsnextobjectivestore"); |
| 277 | EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer> |
| 278 | nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 279 | nsNextObjStore = nsNextObjMapBuilder |
| 280 | .withName("nsnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 281 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 282 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 283 | .build(); |
| 284 | log.trace("Current size {}", nsNextObjStore.size()); |
| 285 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 286 | log.debug("Creating EC map subnetnextobjectivestore"); |
| 287 | EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer> |
| 288 | subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 289 | subnetNextObjStore = subnetNextObjMapBuilder |
| 290 | .withName("subnetnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 291 | .withSerializer(createSerializer()) |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 292 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 293 | .build(); |
| 294 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 295 | log.debug("Creating EC map subnetnextobjectivestore"); |
| 296 | EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer> |
| 297 | portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 298 | portNextObjStore = portNextObjMapBuilder |
| 299 | .withName("portnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 300 | .withSerializer(createSerializer()) |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 301 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 302 | .build(); |
| 303 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 304 | EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder = |
| 305 | storageService.eventuallyConsistentMapBuilder(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 306 | tunnelStore = tunnelMapBuilder |
| 307 | .withName("tunnelstore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 308 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 309 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 310 | .build(); |
| 311 | |
| 312 | EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder = |
| 313 | storageService.eventuallyConsistentMapBuilder(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 314 | policyStore = policyMapBuilder |
| 315 | .withName("policystore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 316 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 317 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 318 | .build(); |
| 319 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 320 | EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId> |
| 321 | subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 322 | subnetVidStore = subnetVidStoreMapBuilder |
| 323 | .withName("subnetvidstore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 324 | .withSerializer(createSerializer()) |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 325 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 326 | .build(); |
| 327 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 328 | compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager", |
| 329 | "purgeOnDisconnection", "true"); |
| 330 | compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager", |
| 331 | "purgeOnDisconnection", "true"); |
| 332 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 333 | processor = new InternalPacketProcessor(); |
| 334 | linkListener = new InternalLinkListener(); |
| 335 | deviceListener = new InternalDeviceListener(); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 336 | appCfgHandler = new AppConfigHandler(this); |
| 337 | xConnectHandler = new XConnectHandler(this); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 338 | mcastHandler = new McastHandler(this); |
| 339 | hostHandler = new HostHandler(this); |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 340 | cordConfigHandler = new CordConfigHandler(this); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 341 | |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 342 | cfgService.addListener(cfgListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 343 | cfgService.registerConfigFactory(deviceConfigFactory); |
| 344 | cfgService.registerConfigFactory(appConfigFactory); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 345 | cfgService.registerConfigFactory(xConnectConfigFactory); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 346 | cfgService.registerConfigFactory(mcastConfigFactory); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 347 | hostService.addListener(hostListener); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 348 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 349 | linkService.addListener(linkListener); |
| 350 | deviceService.addListener(deviceListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 351 | multicastRouteService.addListener(mcastListener); |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 352 | cordConfigService.addListener(cordConfigListener); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 353 | |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 354 | // Request ARP packet-in |
| 355 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 356 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 357 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId, Optional.empty()); |
| 358 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 359 | cfgListener.configureNetwork(); |
| 360 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 361 | log.info("Started"); |
| 362 | } |
| 363 | |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 364 | private KryoNamespace.Builder createSerializer() { |
| 365 | return new KryoNamespace.Builder() |
| 366 | .register(KryoNamespaces.API) |
| 367 | .register(NeighborSetNextObjectiveStoreKey.class, |
| 368 | SubnetNextObjectiveStoreKey.class, |
| 369 | SubnetAssignedVidStoreKey.class, |
| 370 | NeighborSet.class, |
| 371 | Tunnel.class, |
| 372 | DefaultTunnel.class, |
| 373 | Policy.class, |
| 374 | TunnelPolicy.class, |
| 375 | Policy.Type.class, |
| 376 | PortNextObjectiveStoreKey.class, |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 377 | XConnectStoreKey.class |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 378 | ); |
| 379 | } |
| 380 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 381 | @Deactivate |
| 382 | protected void deactivate() { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 383 | cfgService.removeListener(cfgListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 384 | cfgService.unregisterConfigFactory(deviceConfigFactory); |
| 385 | cfgService.unregisterConfigFactory(appConfigFactory); |
| 386 | cfgService.unregisterConfigFactory(mcastConfigFactory); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 387 | |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 388 | // Withdraw ARP packet-in |
| 389 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 390 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 391 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId, Optional.empty()); |
| 392 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 393 | packetService.removeProcessor(processor); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 394 | linkService.removeListener(linkListener); |
| 395 | deviceService.removeListener(deviceListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 396 | multicastRouteService.removeListener(mcastListener); |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 397 | cordConfigService.removeListener(cordConfigListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 398 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 399 | processor = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 400 | linkListener = null; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 401 | deviceListener = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 402 | groupHandlerMap.clear(); |
| 403 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 404 | nsNextObjStore.destroy(); |
| 405 | subnetNextObjStore.destroy(); |
| 406 | portNextObjStore.destroy(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 407 | tunnelStore.destroy(); |
| 408 | policyStore.destroy(); |
| 409 | subnetVidStore.destroy(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 410 | log.info("Stopped"); |
| 411 | } |
| 412 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 413 | @Override |
| 414 | public List<Tunnel> getTunnels() { |
| 415 | return tunnelHandler.getTunnels(); |
| 416 | } |
| 417 | |
| 418 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 419 | public TunnelHandler.Result createTunnel(Tunnel tunnel) { |
| 420 | return tunnelHandler.createTunnel(tunnel); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 424 | public TunnelHandler.Result removeTunnel(Tunnel tunnel) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 425 | for (Policy policy: policyHandler.getPolicies()) { |
| 426 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 427 | TunnelPolicy tunnelPolicy = (TunnelPolicy) policy; |
| 428 | if (tunnelPolicy.tunnelId().equals(tunnel.id())) { |
| 429 | log.warn("Cannot remove the tunnel used by a policy"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 430 | return TunnelHandler.Result.TUNNEL_IN_USE; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 434 | return tunnelHandler.removeTunnel(tunnel); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 438 | public PolicyHandler.Result removePolicy(Policy policy) { |
| 439 | return policyHandler.removePolicy(policy); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 443 | public PolicyHandler.Result createPolicy(Policy policy) { |
| 444 | return policyHandler.createPolicy(policy); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public List<Policy> getPolicies() { |
| 449 | return policyHandler.getPolicies(); |
| 450 | } |
| 451 | |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 452 | @Override |
| 453 | public void rerouteNetwork() { |
| 454 | cfgListener.configureNetwork(); |
| 455 | for (Device device : deviceService.getDevices()) { |
| 456 | defaultRoutingHandler.populatePortAddressingRules(device.id()); |
| 457 | } |
| 458 | defaultRoutingHandler.startPopulationProcess(); |
| 459 | } |
| 460 | |
Charles Chan | d7844e5 | 2016-10-20 17:02:44 -0700 | [diff] [blame] | 461 | @Override |
| 462 | public Map<DeviceId, Set<Ip4Prefix>> getDeviceSubnetMap() { |
| 463 | Map<DeviceId, Set<Ip4Prefix>> deviceSubnetMap = Maps.newHashMap(); |
| 464 | deviceService.getAvailableDevices().forEach(device -> { |
| 465 | deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id())); |
| 466 | }); |
| 467 | return deviceSubnetMap; |
| 468 | } |
| 469 | |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 470 | /** |
Pier Ventre | 7a78de2 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 471 | * Returns the MPLS-ECMP configuration. |
| 472 | * |
| 473 | * @return MPLS-ECMP value |
| 474 | */ |
| 475 | public boolean getMplsEcmp() { |
| 476 | SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService |
| 477 | .getConfig(this.appId, SegmentRoutingAppConfig.class); |
| 478 | return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp(); |
| 479 | } |
| 480 | |
| 481 | /** |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 482 | * Returns the tunnel object with the tunnel ID. |
| 483 | * |
| 484 | * @param tunnelId Tunnel ID |
| 485 | * @return Tunnel reference |
| 486 | */ |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 487 | public Tunnel getTunnel(String tunnelId) { |
| 488 | return tunnelHandler.getTunnel(tunnelId); |
| 489 | } |
| 490 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 491 | /** |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 492 | * Returns the vlan-id assigned to the subnet configured for a device. |
| 493 | * If no vlan-id has been assigned, a new one is assigned out of a pool of ids, |
| 494 | * if and only if this controller instance is the master for the device. |
| 495 | * <p> |
| 496 | * USAGE: The assigned vlans are meant to be applied to untagged packets on those |
| 497 | * switches/pipelines that need this functionality. These vids are meant |
| 498 | * to be used internally within a switch, and thus need to be unique only |
| 499 | * on a switch level. Note that packets never go out on the wire with these |
| 500 | * vlans. Currently, vlan ids are assigned from value 4093 down. |
| 501 | * Vlan id 4094 expected to be used for all ports that are not assigned subnets. |
| 502 | * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned |
| 503 | * per subnet. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 504 | * |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 505 | * @param deviceId switch dpid |
| 506 | * @param subnet IPv4 prefix for which assigned vlan is desired |
| 507 | * @return VlanId assigned for the subnet on the device, or |
| 508 | * null if no vlan assignment was found and this instance is not |
| 509 | * the master for the device. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 510 | */ |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 511 | // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection. |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 512 | public VlanId getSubnetAssignedVlanId(DeviceId deviceId, Ip4Prefix subnet) { |
| 513 | VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey( |
| 514 | deviceId, subnet)); |
| 515 | if (assignedVid != null) { |
| 516 | log.debug("Query for subnet:{} on device:{} returned assigned-vlan " |
| 517 | + "{}", subnet, deviceId, assignedVid); |
| 518 | return assignedVid; |
| 519 | } |
| 520 | //check mastership for the right to assign a vlan |
| 521 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 522 | log.warn("This controller instance is not the master for device {}. " |
| 523 | + "Cannot assign vlan-id for subnet {}", deviceId, subnet); |
| 524 | return null; |
| 525 | } |
| 526 | // vlan assignment is expensive but done only once |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 527 | Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 528 | Set<Short> assignedVlans = new HashSet<>(); |
| 529 | Set<Ip4Prefix> unassignedSubnets = new HashSet<>(); |
| 530 | for (Ip4Prefix sub : configuredSubnets) { |
| 531 | VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, |
| 532 | sub)); |
| 533 | if (v != null) { |
| 534 | assignedVlans.add(v.toShort()); |
| 535 | } else { |
| 536 | unassignedSubnets.add(sub); |
| 537 | } |
| 538 | } |
| 539 | short nextAssignedVlan = ASSIGNED_VLAN_START; |
| 540 | if (!assignedVlans.isEmpty()) { |
| 541 | nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1); |
| 542 | } |
| 543 | for (Ip4Prefix unsub : unassignedSubnets) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 544 | // Special case for default route. Assign default VLAN ID to /32 and /0 subnets |
| 545 | if (unsub.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH || |
| 546 | unsub.prefixLength() == 0) { |
| 547 | subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub), |
| 548 | VlanId.vlanId(ASSIGNED_VLAN_NO_SUBNET)); |
| 549 | } else { |
| 550 | subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub), |
| 551 | VlanId.vlanId(nextAssignedVlan--)); |
| 552 | log.info("Assigned vlan: {} to subnet: {} on device: {}", |
| 553 | nextAssignedVlan + 1, unsub, deviceId); |
| 554 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 557 | return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 558 | } |
| 559 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 560 | /** |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 561 | * Returns the next objective ID for the given NeighborSet. |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 562 | * If the nextObjective does not exist, a new one is created and |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 563 | * its id is returned. |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 564 | * |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 565 | * @param deviceId Device ID |
| 566 | * @param ns NegighborSet |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 567 | * @param meta metadata passed into the creation of a Next Objective |
| 568 | * @return next objective ID or -1 if an error was encountered during the |
| 569 | * creation of the nextObjective |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 570 | */ |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 571 | public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns, |
| 572 | TrafficSelector meta) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 573 | if (groupHandlerMap.get(deviceId) != null) { |
| 574 | log.trace("getNextObjectiveId query in device {}", deviceId); |
| 575 | return groupHandlerMap |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 576 | .get(deviceId).getNextObjectiveId(ns, meta); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 577 | } else { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 578 | log.warn("getNextObjectiveId query - groupHandler for device {} " |
| 579 | + "not found", deviceId); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 580 | return -1; |
| 581 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 584 | /** |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 585 | * Returns the next objective ID for the given subnet prefix. It is expected |
| 586 | * that the next-objective has been pre-created from configuration. |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 587 | * |
| 588 | * @param deviceId Device ID |
| 589 | * @param prefix Subnet |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 590 | * @return next objective ID or -1 if it was not found |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 591 | */ |
| 592 | public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) { |
| 593 | if (groupHandlerMap.get(deviceId) != null) { |
| 594 | log.trace("getSubnetNextObjectiveId query in device {}", deviceId); |
| 595 | return groupHandlerMap |
| 596 | .get(deviceId).getSubnetNextObjectiveId(prefix); |
| 597 | } else { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 598 | log.warn("getSubnetNextObjectiveId query - groupHandler for " |
| 599 | + "device {} not found", deviceId); |
| 600 | return -1; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Returns the next objective ID for the given portNumber, given the treatment. |
| 606 | * There could be multiple different treatments to the same outport, which |
| 607 | * would result in different objectives. If the next object |
| 608 | * does not exist, a new one is created and its id is returned. |
| 609 | * |
| 610 | * @param deviceId Device ID |
| 611 | * @param portNum port number on device for which NextObjective is queried |
| 612 | * @param treatment the actions to apply on the packets (should include outport) |
| 613 | * @param meta metadata passed into the creation of a Next Objective if necessary |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 614 | * @return next objective ID or -1 if an error occurred during retrieval or creation |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 615 | */ |
| 616 | public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum, |
| 617 | TrafficTreatment treatment, |
| 618 | TrafficSelector meta) { |
| 619 | DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId); |
| 620 | if (ghdlr != null) { |
| 621 | return ghdlr.getPortNextObjectiveId(portNum, treatment, meta); |
| 622 | } else { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 623 | log.warn("getPortNextObjectiveId query - groupHandler for device {}" |
| 624 | + " not found", deviceId); |
| 625 | return -1; |
| 626 | } |
| 627 | } |
| 628 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 629 | private class InternalPacketProcessor implements PacketProcessor { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 630 | @Override |
| 631 | public void process(PacketContext context) { |
| 632 | |
| 633 | if (context.isHandled()) { |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | InboundPacket pkt = context.inPacket(); |
| 638 | Ethernet ethernet = pkt.parsed(); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 639 | log.trace("Rcvd pktin: {}", ethernet); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 640 | if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
| 641 | arpHandler.processPacketIn(pkt); |
| 642 | } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 643 | IPv4 ipPacket = (IPv4) ethernet.getPayload(); |
| 644 | ipHandler.addToPacketBuffer(ipPacket); |
| 645 | if (ipPacket.getProtocol() == IPv4.PROTOCOL_ICMP) { |
| 646 | icmpHandler.processPacketIn(pkt); |
| 647 | } else { |
| 648 | ipHandler.processPacketIn(pkt); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | private class InternalLinkListener implements LinkListener { |
| 655 | @Override |
| 656 | public void event(LinkEvent event) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 657 | if (event.type() == LinkEvent.Type.LINK_ADDED |
| 658 | || event.type() == LinkEvent.Type.LINK_REMOVED) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 659 | log.debug("Event {} received from Link Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 660 | scheduleEventHandlerIfNotScheduled(event); |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | private class InternalDeviceListener implements DeviceListener { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 666 | @Override |
| 667 | public void event(DeviceEvent event) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 668 | switch (event.type()) { |
| 669 | case DEVICE_ADDED: |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 670 | case PORT_UPDATED: |
| 671 | case PORT_ADDED: |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 672 | case DEVICE_UPDATED: |
| 673 | case DEVICE_AVAILABILITY_CHANGED: |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 674 | log.debug("Event {} received from Device Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 675 | scheduleEventHandlerIfNotScheduled(event); |
| 676 | break; |
| 677 | default: |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 682 | @SuppressWarnings("rawtypes") |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 683 | private void scheduleEventHandlerIfNotScheduled(Event event) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 684 | synchronized (threadSchedulerLock) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 685 | eventQueue.add(event); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 686 | numOfEventsQueued++; |
| 687 | |
| 688 | if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) { |
| 689 | //No pending scheduled event handling threads. So start a new one. |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 690 | eventHandlerFuture = executorService |
| 691 | .schedule(eventHandler, 100, TimeUnit.MILLISECONDS); |
| 692 | numOfHandlerScheduled++; |
| 693 | } |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 694 | log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}", |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 695 | numOfEventsQueued, |
| 696 | numOfHandlerScheduled); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 697 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | private class InternalEventHandler implements Runnable { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 701 | @Override |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 702 | public void run() { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 703 | try { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 704 | while (true) { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 705 | @SuppressWarnings("rawtypes") |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 706 | Event event = null; |
| 707 | synchronized (threadSchedulerLock) { |
| 708 | if (!eventQueue.isEmpty()) { |
| 709 | event = eventQueue.poll(); |
| 710 | numOfEventsExecuted++; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 711 | } else { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 712 | numOfHandlerExecution++; |
| 713 | log.debug("numOfHandlerExecution {} numOfEventsExecuted {}", |
| 714 | numOfHandlerExecution, numOfEventsExecuted); |
| 715 | break; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 716 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 717 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 718 | if (event.type() == LinkEvent.Type.LINK_ADDED) { |
| 719 | processLinkAdded((Link) event.subject()); |
| 720 | } else if (event.type() == LinkEvent.Type.LINK_REMOVED) { |
Pier Ventre | 6d59389 | 2016-09-13 21:33:40 -0700 | [diff] [blame] | 721 | Link linkRemoved = (Link) event.subject(); |
| 722 | if (linkRemoved.src().elementId() instanceof DeviceId && |
| 723 | !deviceService.isAvailable(linkRemoved.src().deviceId())) { |
| 724 | continue; |
| 725 | } |
| 726 | if (linkRemoved.dst().elementId() instanceof DeviceId && |
| 727 | !deviceService.isAvailable(linkRemoved.dst().deviceId())) { |
| 728 | continue; |
| 729 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 730 | processLinkRemoved((Link) event.subject()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 731 | } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED || |
| 732 | event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 733 | event.type() == DeviceEvent.Type.DEVICE_UPDATED) { |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 734 | DeviceId deviceId = ((Device) event.subject()).id(); |
| 735 | if (deviceService.isAvailable(deviceId)) { |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 736 | log.info("Processing device event {} for available device {}", |
| 737 | event.type(), ((Device) event.subject()).id()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 738 | processDeviceAdded((Device) event.subject()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 739 | } else { |
| 740 | log.info("Processing device event {} for unavailable device {}", |
| 741 | event.type(), ((Device) event.subject()).id()); |
| 742 | processDeviceRemoved((Device) event.subject()); |
| 743 | } |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 744 | } else if (event.type() == DeviceEvent.Type.PORT_ADDED) { |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 745 | // typically these calls come when device is added first time |
| 746 | // so port filtering rules are handled at the device_added event. |
| 747 | // port added calls represent all ports on the device, |
| 748 | // enabled or not. |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 749 | log.debug("** PORT ADDED {}/{} -> {}", |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 750 | ((DeviceEvent) event).subject().id(), |
| 751 | ((DeviceEvent) event).port().number(), |
| 752 | event.type()); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 753 | } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) { |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 754 | // these calls happen for every subsequent event |
| 755 | // ports enabled, disabled, switch goes away, comes back |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 756 | log.info("** PORT UPDATED {}/{} -> {}", |
| 757 | event.subject(), |
| 758 | ((DeviceEvent) event).port(), |
| 759 | event.type()); |
| 760 | processPortUpdated(((Device) event.subject()), |
| 761 | ((DeviceEvent) event).port()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 762 | } else { |
| 763 | log.warn("Unhandled event type: {}", event.type()); |
| 764 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 765 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 766 | } catch (Exception e) { |
| 767 | log.error("SegmentRouting event handler " |
| 768 | + "thread thrown an exception: {}", e); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 769 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 773 | private void processLinkAdded(Link link) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 774 | log.info("** LINK ADDED {}", link.toString()); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 775 | if (!deviceConfiguration.isConfigured(link.src().deviceId())) { |
| 776 | log.warn("Source device of this link is not configured."); |
| 777 | return; |
| 778 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 779 | //Irrespective whether the local is a MASTER or not for this device, |
| 780 | //create group handler instance and push default TTP flow rules. |
| 781 | //Because in a multi-instance setup, instances can initiate |
| 782 | //groups for any devices. Also the default TTP rules are needed |
| 783 | //to be pushed before inserting any IP table entries for any device |
| 784 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src() |
| 785 | .deviceId()); |
| 786 | if (groupHandler != null) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 787 | groupHandler.linkUp(link, mastershipService.isLocalMaster( |
| 788 | link.src().deviceId())); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 789 | } else { |
| 790 | Device device = deviceService.getDevice(link.src().deviceId()); |
| 791 | if (device != null) { |
| 792 | log.warn("processLinkAdded: Link Added " |
| 793 | + "Notification without Device Added " |
| 794 | + "event, still handling it"); |
| 795 | processDeviceAdded(device); |
| 796 | groupHandler = groupHandlerMap.get(link.src() |
| 797 | .deviceId()); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 798 | groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id())); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 801 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 802 | log.trace("Starting optimized route population process"); |
| 803 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null); |
| 804 | //log.trace("processLinkAdded: re-starting route population process"); |
| 805 | //defaultRoutingHandler.startPopulationProcess(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 806 | |
| 807 | mcastHandler.init(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | private void processLinkRemoved(Link link) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 811 | log.info("** LINK REMOVED {}", link.toString()); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 812 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId()); |
| 813 | if (groupHandler != null) { |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 814 | groupHandler.portDown(link.src().port(), |
| 815 | mastershipService.isLocalMaster(link.src().deviceId())); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 816 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 817 | log.trace("Starting optimized route population process"); |
| 818 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link); |
| 819 | //log.trace("processLinkRemoved: re-starting route population process"); |
| 820 | //defaultRoutingHandler.startPopulationProcess(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 821 | |
| 822 | mcastHandler.processLinkDown(link); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | private void processDeviceAdded(Device device) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 826 | log.info("** DEVICE ADDED with ID {}", device.id()); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 827 | if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) { |
Saurav Das | 8ec0ec4 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 828 | log.warn("Device configuration uploading. Device {} will be " |
| 829 | + "processed after config completes.", device.id()); |
| 830 | return; |
| 831 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 832 | processDeviceAddedInternal(device.id()); |
| 833 | } |
| 834 | |
| 835 | private void processDeviceAddedInternal(DeviceId deviceId) { |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 836 | // Irrespective of whether the local is a MASTER or not for this device, |
| 837 | // we need to create a SR-group-handler instance. This is because in a |
| 838 | // multi-instance setup, any instance can initiate forwarding/next-objectives |
| 839 | // for any switch (even if this instance is a SLAVE or not even connected |
| 840 | // to the switch). To handle this, a default-group-handler instance is necessary |
| 841 | // per switch. |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 842 | log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet()); |
| 843 | if (groupHandlerMap.get(deviceId) == null) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 844 | DefaultGroupHandler groupHandler; |
| 845 | try { |
| 846 | groupHandler = DefaultGroupHandler. |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 847 | createGroupHandler(deviceId, |
| 848 | appId, |
| 849 | deviceConfiguration, |
| 850 | linkService, |
| 851 | flowObjectiveService, |
| 852 | this); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 853 | } catch (DeviceConfigNotFoundException e) { |
| 854 | log.warn(e.getMessage() + " Aborting processDeviceAdded."); |
| 855 | return; |
| 856 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 857 | log.debug("updating groupHandlerMap with new config for device: {}", |
| 858 | deviceId); |
| 859 | groupHandlerMap.put(deviceId, groupHandler); |
Saurav Das | 8ec0ec4 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 860 | } |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 861 | // Also, in some cases, drivers may need extra |
| 862 | // information to process rules (eg. Router IP/MAC); and so, we send |
| 863 | // port addressing rules to the driver as well irrespective of whether |
| 864 | // this instance is the master or not. |
| 865 | defaultRoutingHandler.populatePortAddressingRules(deviceId); |
| 866 | |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 867 | if (mastershipService.isLocalMaster(deviceId)) { |
| 868 | hostHandler.readInitialHosts(deviceId); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 869 | xConnectHandler.init(deviceId); |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 870 | cordConfigHandler.init(deviceId); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 871 | DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 872 | groupHandler.createGroupsFromSubnetConfig(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 873 | routingRulePopulator.populateSubnetBroadcastRule(deviceId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 874 | } |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 875 | |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 876 | appCfgHandler.initVRouters(deviceId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 879 | private void processDeviceRemoved(Device device) { |
| 880 | nsNextObjStore.entrySet().stream() |
| 881 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 882 | .forEach(entry -> { |
| 883 | nsNextObjStore.remove(entry.getKey()); |
| 884 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 885 | subnetNextObjStore.entrySet().stream() |
| 886 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 887 | .forEach(entry -> { |
| 888 | subnetNextObjStore.remove(entry.getKey()); |
| 889 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 890 | portNextObjStore.entrySet().stream() |
| 891 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 892 | .forEach(entry -> { |
| 893 | portNextObjStore.remove(entry.getKey()); |
| 894 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 895 | subnetVidStore.entrySet().stream() |
| 896 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 897 | .forEach(entry -> { |
| 898 | subnetVidStore.remove(entry.getKey()); |
| 899 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 900 | groupHandlerMap.remove(device.id()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 901 | defaultRoutingHandler.purgeEcmpGraph(device.id()); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 902 | mcastHandler.removeDevice(device.id()); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 903 | xConnectHandler.removeDevice(device.id()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 906 | private void processPortUpdated(Device device, Port port) { |
| 907 | if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) { |
| 908 | log.warn("Device configuration uploading. Not handling port event for" |
| 909 | + "dev: {} port: {}", device.id(), port.number()); |
| 910 | return; |
| 911 | } |
| 912 | /* XXX create method for single port filtering rules which are needed |
| 913 | for both switch-to-switch ports and edge ports |
| 914 | if (defaultRoutingHandler != null) { |
| 915 | defaultRoutingHandler.populatePortAddressingRules( |
| 916 | ((Device) event.subject()).id()); |
| 917 | }*/ |
| 918 | |
| 919 | // portUpdated calls are for ports that have gone down or up. For switch |
| 920 | // to switch ports, link-events should take care of any re-routing or |
| 921 | // group editing necessary for port up/down. Here we only process edge ports |
| 922 | // that are already configured. |
| 923 | Ip4Prefix configuredSubnet = deviceConfiguration.getPortSubnet(device.id(), |
| 924 | port.number()); |
| 925 | if (configuredSubnet == null) { |
| 926 | log.debug("Not handling port updated event for unconfigured port " |
| 927 | + "dev/port: {}/{}", device.id(), port.number()); |
| 928 | return; |
| 929 | } |
| 930 | processEdgePort(device, port, configuredSubnet); |
| 931 | } |
| 932 | |
| 933 | private void processEdgePort(Device device, Port port, Ip4Prefix subnet) { |
| 934 | boolean portUp = port.isEnabled(); |
| 935 | if (portUp) { |
| 936 | log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(), |
| 937 | port.number(), subnet); |
| 938 | } else { |
| 939 | log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(), |
| 940 | port.number(), subnet); |
| 941 | } |
| 942 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 943 | DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 944 | if (groupHandler != null) { |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 945 | groupHandler.processEdgePort(port.number(), subnet, portUp, |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 946 | mastershipService.isLocalMaster(device.id())); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 947 | } else { |
| 948 | log.warn("Group handler not found for dev:{}. Not handling edge port" |
| 949 | + " {} event for port:{}", device.id(), |
| 950 | (portUp) ? "UP" : "DOWN", port.number()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 953 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 954 | private class InternalConfigListener implements NetworkConfigListener { |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 955 | SegmentRoutingManager srManager; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 956 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 957 | /** |
| 958 | * Constructs the internal network config listener. |
| 959 | * |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 960 | * @param srManager segment routing manager |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 961 | */ |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 962 | public InternalConfigListener(SegmentRoutingManager srManager) { |
| 963 | this.srManager = srManager; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 966 | /** |
| 967 | * Reads network config and initializes related data structure accordingly. |
| 968 | */ |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 969 | public void configureNetwork() { |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 970 | deviceConfiguration = new DeviceConfiguration(srManager); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 971 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 972 | arpHandler = new ArpHandler(srManager); |
| 973 | icmpHandler = new IcmpHandler(srManager); |
| 974 | ipHandler = new IpHandler(srManager); |
| 975 | routingRulePopulator = new RoutingRulePopulator(srManager); |
| 976 | defaultRoutingHandler = new DefaultRoutingHandler(srManager); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 977 | |
| 978 | tunnelHandler = new TunnelHandler(linkService, deviceConfiguration, |
| 979 | groupHandlerMap, tunnelStore); |
| 980 | policyHandler = new PolicyHandler(appId, deviceConfiguration, |
| 981 | flowObjectiveService, |
| 982 | tunnelHandler, policyStore); |
| 983 | |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 984 | for (Device device : deviceService.getDevices()) { |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 985 | processDeviceAddedInternal(device.id()); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | defaultRoutingHandler.startPopulationProcess(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 989 | mcastHandler.init(); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 992 | @Override |
| 993 | public void event(NetworkConfigEvent event) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 994 | // TODO move this part to NetworkConfigEventHandler |
| 995 | if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) { |
| 996 | switch (event.type()) { |
| 997 | case CONFIG_ADDED: |
| 998 | log.info("Segment Routing Config added."); |
| 999 | configureNetwork(); |
| 1000 | break; |
| 1001 | case CONFIG_UPDATED: |
| 1002 | log.info("Segment Routing Config updated."); |
| 1003 | // TODO support dynamic configuration |
| 1004 | break; |
| 1005 | default: |
| 1006 | break; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 1007 | } |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1008 | } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) { |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1009 | checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized"); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1010 | switch (event.type()) { |
| 1011 | case CONFIG_ADDED: |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1012 | appCfgHandler.processAppConfigAdded(event); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1013 | break; |
| 1014 | case CONFIG_UPDATED: |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1015 | appCfgHandler.processAppConfigUpdated(event); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1016 | break; |
| 1017 | case CONFIG_REMOVED: |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1018 | appCfgHandler.processAppConfigRemoved(event); |
| 1019 | break; |
| 1020 | default: |
| 1021 | break; |
| 1022 | } |
| 1023 | } else if (event.configClass().equals(XConnectConfig.class)) { |
| 1024 | checkState(xConnectHandler != null, "XConnectHandler is not initialized"); |
| 1025 | switch (event.type()) { |
| 1026 | case CONFIG_ADDED: |
| 1027 | xConnectHandler.processXConnectConfigAdded(event); |
| 1028 | break; |
| 1029 | case CONFIG_UPDATED: |
| 1030 | xConnectHandler.processXConnectConfigUpdated(event); |
| 1031 | break; |
| 1032 | case CONFIG_REMOVED: |
| 1033 | xConnectHandler.processXConnectConfigRemoved(event); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1034 | break; |
| 1035 | default: |
| 1036 | break; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 1037 | } |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | } |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1041 | |
| 1042 | private class InternalHostListener implements HostListener { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1043 | @Override |
| 1044 | public void event(HostEvent event) { |
| 1045 | // Do not proceed without mastership |
| 1046 | DeviceId deviceId = event.subject().location().deviceId(); |
| 1047 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 1048 | return; |
| 1049 | } |
| 1050 | |
| 1051 | switch (event.type()) { |
| 1052 | case HOST_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1053 | hostHandler.processHostAddedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1054 | break; |
| 1055 | case HOST_MOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1056 | hostHandler.processHostMovedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1057 | break; |
| 1058 | case HOST_REMOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1059 | hostHandler.processHostRemoveEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1060 | break; |
| 1061 | case HOST_UPDATED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1062 | hostHandler.processHostUpdatedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1063 | break; |
| 1064 | default: |
| 1065 | log.warn("Unsupported host event type: {}", event.type()); |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1071 | private class InternalMcastListener implements McastListener { |
| 1072 | @Override |
| 1073 | public void event(McastEvent event) { |
| 1074 | switch (event.type()) { |
| 1075 | case SOURCE_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1076 | mcastHandler.processSourceAdded(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1077 | break; |
| 1078 | case SINK_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1079 | mcastHandler.processSinkAdded(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1080 | break; |
| 1081 | case SINK_REMOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1082 | mcastHandler.processSinkRemoved(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1083 | break; |
| 1084 | case ROUTE_ADDED: |
| 1085 | case ROUTE_REMOVED: |
| 1086 | default: |
| 1087 | break; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 1091 | |
| 1092 | private class InternalCordConfigListener implements CordConfigListener { |
| 1093 | @Override |
| 1094 | public void event(CordConfigEvent event) { |
| 1095 | switch (event.type()) { |
| 1096 | case ACCESS_AGENT_ADDED: |
| 1097 | cordConfigHandler.processAccessAgentAddedEvent(event); |
| 1098 | break; |
| 1099 | case ACCESS_AGENT_UPDATED: |
| 1100 | cordConfigHandler.processAccessAgentUpdatedEvent(event); |
| 1101 | break; |
| 1102 | case ACCESS_AGENT_REMOVED: |
| 1103 | cordConfigHandler.processAccessAgentRemovedEvent(event); |
| 1104 | break; |
| 1105 | case ACCESS_DEVICE_ADDED: |
| 1106 | case ACCESS_DEVICE_UPDATED: |
| 1107 | case ACCESS_DEVICE_REMOVED: |
| 1108 | default: |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1113 | } |