sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onlab.packet.IPv4; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 26 | import org.onlab.packet.Ip4Prefix; |
| 27 | import org.onlab.packet.VlanId; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 28 | import org.onlab.util.KryoNamespace; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.event.Event; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 32 | import org.onosproject.net.config.ConfigFactory; |
| 33 | import org.onosproject.net.config.NetworkConfigEvent; |
| 34 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 35 | import org.onosproject.net.config.NetworkConfigListener; |
| 36 | import org.onosproject.net.config.basics.SubjectFactories; |
| 37 | import org.onosproject.segmentrouting.config.SegmentRoutingConfig; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 38 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
| 39 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 40 | import org.onosproject.segmentrouting.grouphandler.NeighborSetNextObjectiveStoreKey; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 41 | import org.onosproject.mastership.MastershipService; |
| 42 | import org.onosproject.net.Device; |
| 43 | import org.onosproject.net.DeviceId; |
| 44 | import org.onosproject.net.Link; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 45 | import org.onosproject.net.Port; |
| 46 | import org.onosproject.net.device.DeviceEvent; |
| 47 | import org.onosproject.net.device.DeviceListener; |
| 48 | import org.onosproject.net.device.DeviceService; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 49 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 50 | import org.onosproject.net.host.HostService; |
| 51 | import org.onosproject.net.intent.IntentService; |
| 52 | import org.onosproject.net.link.LinkEvent; |
| 53 | import org.onosproject.net.link.LinkListener; |
| 54 | import org.onosproject.net.link.LinkService; |
| 55 | import org.onosproject.net.packet.InboundPacket; |
| 56 | import org.onosproject.net.packet.PacketContext; |
| 57 | import org.onosproject.net.packet.PacketProcessor; |
| 58 | import org.onosproject.net.packet.PacketService; |
| 59 | import org.onosproject.net.topology.TopologyService; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 60 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 61 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 62 | import org.onosproject.store.service.StorageService; |
| 63 | import org.onosproject.store.service.WallClockTimestamp; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 64 | import org.slf4j.Logger; |
| 65 | import org.slf4j.LoggerFactory; |
| 66 | |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 67 | import java.net.URI; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 68 | import java.util.Collections; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 69 | import java.util.HashSet; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 70 | import java.util.List; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 71 | import java.util.Map; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 72 | import java.util.Set; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 73 | import java.util.concurrent.ConcurrentHashMap; |
| 74 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 75 | import java.util.concurrent.Executors; |
| 76 | import java.util.concurrent.ScheduledExecutorService; |
| 77 | import java.util.concurrent.ScheduledFuture; |
| 78 | import java.util.concurrent.TimeUnit; |
| 79 | |
| 80 | @SuppressWarnings("ALL") |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 81 | @Service |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 82 | @Component(immediate = true) |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 83 | public class SegmentRoutingManager implements SegmentRoutingService { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 84 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 85 | private static Logger log = LoggerFactory |
| 86 | .getLogger(SegmentRoutingManager.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected CoreService coreService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected TopologyService topologyService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected PacketService packetService; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected IntentService intentService; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected HostService hostService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected DeviceService deviceService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 107 | protected FlowObjectiveService flowObjectiveService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected LinkService linkService; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 113 | protected MastershipService mastershipService; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 114 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 115 | protected ArpHandler arpHandler = null; |
| 116 | protected IcmpHandler icmpHandler = null; |
| 117 | protected IpHandler ipHandler = null; |
| 118 | protected RoutingRulePopulator routingRulePopulator = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 119 | protected ApplicationId appId; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 120 | protected DeviceConfiguration deviceConfiguration = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 121 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 122 | private DefaultRoutingHandler defaultRoutingHandler = null; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 123 | private TunnelHandler tunnelHandler = null; |
| 124 | private PolicyHandler policyHandler = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 125 | private InternalPacketProcessor processor = null; |
| 126 | private InternalLinkListener linkListener = null; |
| 127 | private InternalDeviceListener deviceListener = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 128 | private InternalEventHandler eventHandler = new InternalEventHandler(); |
| 129 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 130 | private ScheduledExecutorService executorService = Executors |
| 131 | .newScheduledThreadPool(1); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 132 | |
| 133 | private static ScheduledFuture<?> eventHandlerFuture = null; |
| 134 | private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<Event>(); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 135 | private Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<DeviceId, DefaultGroupHandler>(); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 136 | // Per device next objective ID store with (device id + neighbor set) as key |
| 137 | private EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, |
| 138 | Integer> nsNextObjStore = null; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 139 | private EventuallyConsistentMap<String, Tunnel> tunnelStore = null; |
| 140 | private EventuallyConsistentMap<String, Policy> policyStore = null; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 141 | // Per device, per-subnet assigned-vlans store, with (device id + subnet |
| 142 | // IPv4 prefix) as key |
| 143 | private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId> |
| 144 | subnetVidStore = null; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 145 | |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 147 | protected StorageService storageService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 148 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 149 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 150 | protected NetworkConfigRegistry cfgService; |
| 151 | |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 152 | private final InternalConfigListener cfgListener = |
| 153 | new InternalConfigListener(this); |
| 154 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 155 | private final ConfigFactory cfgFactory = |
| 156 | new ConfigFactory(SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 157 | SegmentRoutingConfig.class, |
| 158 | "segmentrouting") { |
| 159 | @Override |
| 160 | public SegmentRoutingConfig createConfig() { |
| 161 | return new SegmentRoutingConfig(); |
| 162 | } |
| 163 | }; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 164 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 165 | private Object threadSchedulerLock = new Object(); |
| 166 | private static int numOfEventsQueued = 0; |
| 167 | private static int numOfEventsExecuted = 0; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 168 | private static int numOfHandlerExecution = 0; |
| 169 | private static int numOfHandlerScheduled = 0; |
| 170 | |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 171 | private KryoNamespace.Builder kryoBuilder = null; |
| 172 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 173 | private static final short ASSIGNED_VLAN_START = 4093; |
| 174 | public static final short ASSIGNED_VLAN_NO_SUBNET = 4094; |
| 175 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 176 | @Activate |
| 177 | protected void activate() { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 178 | appId = coreService |
| 179 | .registerApplication("org.onosproject.segmentrouting"); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 180 | |
| 181 | kryoBuilder = new KryoNamespace.Builder() |
| 182 | .register(NeighborSetNextObjectiveStoreKey.class, |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 183 | NeighborSet.class, |
| 184 | DeviceId.class, |
| 185 | URI.class, |
| 186 | WallClockTimestamp.class, |
| 187 | org.onosproject.cluster.NodeId.class, |
| 188 | HashSet.class, |
| 189 | Tunnel.class, |
| 190 | DefaultTunnel.class, |
| 191 | Policy.class, |
| 192 | TunnelPolicy.class, |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 193 | Policy.Type.class, |
| 194 | SubnetAssignedVidStoreKey.class, |
| 195 | VlanId.class |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 196 | ); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 197 | |
| 198 | log.debug("Creating EC map nsnextobjectivestore"); |
| 199 | EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer> |
| 200 | nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 201 | |
| 202 | nsNextObjStore = nsNextObjMapBuilder |
| 203 | .withName("nsnextobjectivestore") |
| 204 | .withSerializer(kryoBuilder) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 205 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 206 | .build(); |
| 207 | log.trace("Current size {}", nsNextObjStore.size()); |
| 208 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 209 | EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder = |
| 210 | storageService.eventuallyConsistentMapBuilder(); |
| 211 | |
| 212 | tunnelStore = tunnelMapBuilder |
| 213 | .withName("tunnelstore") |
| 214 | .withSerializer(kryoBuilder) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 215 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 216 | .build(); |
| 217 | |
| 218 | EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder = |
| 219 | storageService.eventuallyConsistentMapBuilder(); |
| 220 | |
| 221 | policyStore = policyMapBuilder |
| 222 | .withName("policystore") |
| 223 | .withSerializer(kryoBuilder) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 224 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 225 | .build(); |
| 226 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 227 | EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId> |
| 228 | subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 229 | |
| 230 | subnetVidStore = subnetVidStoreMapBuilder |
| 231 | .withName("subnetvidstore") |
| 232 | .withSerializer(kryoBuilder) |
| 233 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 234 | .build(); |
| 235 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 236 | cfgService.addListener(cfgListener); |
| 237 | cfgService.registerConfigFactory(cfgFactory); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 238 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 239 | processor = new InternalPacketProcessor(); |
| 240 | linkListener = new InternalLinkListener(); |
| 241 | deviceListener = new InternalDeviceListener(); |
| 242 | |
| 243 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 244 | linkService.addListener(linkListener); |
| 245 | deviceService.addListener(deviceListener); |
| 246 | |
| 247 | cfgListener.configureNetwork(); |
| 248 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 249 | log.info("Started"); |
| 250 | } |
| 251 | |
| 252 | @Deactivate |
| 253 | protected void deactivate() { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 254 | cfgService.removeListener(cfgListener); |
| 255 | cfgService.unregisterConfigFactory(cfgFactory); |
| 256 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 257 | packetService.removeProcessor(processor); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 258 | linkService.removeListener(linkListener); |
| 259 | deviceService.removeListener(deviceListener); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 260 | processor = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 261 | linkListener = null; |
| 262 | deviceService = null; |
| 263 | |
| 264 | groupHandlerMap.clear(); |
| 265 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 266 | log.info("Stopped"); |
| 267 | } |
| 268 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 269 | |
| 270 | @Override |
| 271 | public List<Tunnel> getTunnels() { |
| 272 | return tunnelHandler.getTunnels(); |
| 273 | } |
| 274 | |
| 275 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 276 | public TunnelHandler.Result createTunnel(Tunnel tunnel) { |
| 277 | return tunnelHandler.createTunnel(tunnel); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 281 | public TunnelHandler.Result removeTunnel(Tunnel tunnel) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 282 | for (Policy policy: policyHandler.getPolicies()) { |
| 283 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 284 | TunnelPolicy tunnelPolicy = (TunnelPolicy) policy; |
| 285 | if (tunnelPolicy.tunnelId().equals(tunnel.id())) { |
| 286 | log.warn("Cannot remove the tunnel used by a policy"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 287 | return TunnelHandler.Result.TUNNEL_IN_USE; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 291 | return tunnelHandler.removeTunnel(tunnel); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 295 | public PolicyHandler.Result removePolicy(Policy policy) { |
| 296 | return policyHandler.removePolicy(policy); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 300 | public PolicyHandler.Result createPolicy(Policy policy) { |
| 301 | return policyHandler.createPolicy(policy); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public List<Policy> getPolicies() { |
| 306 | return policyHandler.getPolicies(); |
| 307 | } |
| 308 | |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 309 | /** |
| 310 | * Returns the tunnel object with the tunnel ID. |
| 311 | * |
| 312 | * @param tunnelId Tunnel ID |
| 313 | * @return Tunnel reference |
| 314 | */ |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 315 | public Tunnel getTunnel(String tunnelId) { |
| 316 | return tunnelHandler.getTunnel(tunnelId); |
| 317 | } |
| 318 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 319 | /** |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 320 | * Returns the vlan-id assigned to the subnet configured for a device. |
| 321 | * If no vlan-id has been assigned, a new one is assigned out of a pool of ids, |
| 322 | * if and only if this controller instance is the master for the device. |
| 323 | * <p> |
| 324 | * USAGE: The assigned vlans are meant to be applied to untagged packets on those |
| 325 | * switches/pipelines that need this functionality. These vids are meant |
| 326 | * to be used internally within a switch, and thus need to be unique only |
| 327 | * on a switch level. Note that packets never go out on the wire with these |
| 328 | * vlans. Currently, vlan ids are assigned from value 4093 down. |
| 329 | * Vlan id 4094 expected to be used for all ports that are not assigned subnets. |
| 330 | * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned |
| 331 | * per subnet. |
| 332 | * XXX This method should avoid any vlans configured on the ports, but |
| 333 | * currently the app works only on untagged packets and as a result |
| 334 | * ignores any vlan configuration. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 335 | * |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 336 | * @param deviceId switch dpid |
| 337 | * @param subnet IPv4 prefix for which assigned vlan is desired |
| 338 | * @return VlanId assigned for the subnet on the device, or |
| 339 | * null if no vlan assignment was found and this instance is not |
| 340 | * the master for the device. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 341 | */ |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 342 | public VlanId getSubnetAssignedVlanId(DeviceId deviceId, Ip4Prefix subnet) { |
| 343 | VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey( |
| 344 | deviceId, subnet)); |
| 345 | if (assignedVid != null) { |
| 346 | log.debug("Query for subnet:{} on device:{} returned assigned-vlan " |
| 347 | + "{}", subnet, deviceId, assignedVid); |
| 348 | return assignedVid; |
| 349 | } |
| 350 | //check mastership for the right to assign a vlan |
| 351 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 352 | log.warn("This controller instance is not the master for device {}. " |
| 353 | + "Cannot assign vlan-id for subnet {}", deviceId, subnet); |
| 354 | return null; |
| 355 | } |
| 356 | // vlan assignment is expensive but done only once |
| 357 | List<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId); |
| 358 | Set<Short> assignedVlans = new HashSet<>(); |
| 359 | Set<Ip4Prefix> unassignedSubnets = new HashSet<>(); |
| 360 | for (Ip4Prefix sub : configuredSubnets) { |
| 361 | VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, |
| 362 | sub)); |
| 363 | if (v != null) { |
| 364 | assignedVlans.add(v.toShort()); |
| 365 | } else { |
| 366 | unassignedSubnets.add(sub); |
| 367 | } |
| 368 | } |
| 369 | short nextAssignedVlan = ASSIGNED_VLAN_START; |
| 370 | if (!assignedVlans.isEmpty()) { |
| 371 | nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1); |
| 372 | } |
| 373 | for (Ip4Prefix unsub : unassignedSubnets) { |
| 374 | subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub), |
| 375 | VlanId.vlanId(nextAssignedVlan--)); |
| 376 | log.info("Assigned vlan: {} to subnet: {} on device: {}", |
| 377 | nextAssignedVlan + 1, unsub, deviceId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 380 | return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 381 | } |
| 382 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 383 | /** |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 384 | * Returns the next objective ID for the given NeighborSet. |
| 385 | * If the nextObjectiveID does not exist, a new one is created and returned. |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 386 | * |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 387 | * @param deviceId Device ID |
| 388 | * @param ns NegighborSet |
| 389 | * @return next objective ID |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 390 | */ |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 391 | public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 392 | if (groupHandlerMap.get(deviceId) != null) { |
| 393 | log.trace("getNextObjectiveId query in device {}", deviceId); |
| 394 | return groupHandlerMap |
| 395 | .get(deviceId).getNextObjectiveId(ns); |
| 396 | } else { |
| 397 | log.warn("getNextObjectiveId query in device {} not found", deviceId); |
| 398 | return -1; |
| 399 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 400 | } |
| 401 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 402 | private class InternalPacketProcessor implements PacketProcessor { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 403 | @Override |
| 404 | public void process(PacketContext context) { |
| 405 | |
| 406 | if (context.isHandled()) { |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | InboundPacket pkt = context.inPacket(); |
| 411 | Ethernet ethernet = pkt.parsed(); |
| 412 | |
| 413 | if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
| 414 | arpHandler.processPacketIn(pkt); |
| 415 | } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 416 | IPv4 ipPacket = (IPv4) ethernet.getPayload(); |
| 417 | ipHandler.addToPacketBuffer(ipPacket); |
| 418 | if (ipPacket.getProtocol() == IPv4.PROTOCOL_ICMP) { |
| 419 | icmpHandler.processPacketIn(pkt); |
| 420 | } else { |
| 421 | ipHandler.processPacketIn(pkt); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | private class InternalLinkListener implements LinkListener { |
| 428 | @Override |
| 429 | public void event(LinkEvent event) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 430 | if (event.type() == LinkEvent.Type.LINK_ADDED |
| 431 | || event.type() == LinkEvent.Type.LINK_REMOVED) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 432 | log.debug("Event {} received from Link Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 433 | scheduleEventHandlerIfNotScheduled(event); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | private class InternalDeviceListener implements DeviceListener { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 439 | @Override |
| 440 | public void event(DeviceEvent event) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 441 | switch (event.type()) { |
| 442 | case DEVICE_ADDED: |
| 443 | case PORT_REMOVED: |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 444 | case DEVICE_UPDATED: |
| 445 | case DEVICE_AVAILABILITY_CHANGED: |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 446 | log.debug("Event {} received from Device Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 447 | scheduleEventHandlerIfNotScheduled(event); |
| 448 | break; |
| 449 | default: |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 454 | private void scheduleEventHandlerIfNotScheduled(Event event) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 455 | synchronized (threadSchedulerLock) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 456 | eventQueue.add(event); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 457 | numOfEventsQueued++; |
| 458 | |
| 459 | if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) { |
| 460 | //No pending scheduled event handling threads. So start a new one. |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 461 | eventHandlerFuture = executorService |
| 462 | .schedule(eventHandler, 100, TimeUnit.MILLISECONDS); |
| 463 | numOfHandlerScheduled++; |
| 464 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 465 | log.trace("numOfEventsQueued {}, numOfEventHanlderScheduled {}", |
| 466 | numOfEventsQueued, |
| 467 | numOfHandlerScheduled); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 468 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | private class InternalEventHandler implements Runnable { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 472 | @Override |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 473 | public void run() { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 474 | try { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 475 | while (true) { |
| 476 | Event event = null; |
| 477 | synchronized (threadSchedulerLock) { |
| 478 | if (!eventQueue.isEmpty()) { |
| 479 | event = eventQueue.poll(); |
| 480 | numOfEventsExecuted++; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 481 | } else { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 482 | numOfHandlerExecution++; |
| 483 | log.debug("numOfHandlerExecution {} numOfEventsExecuted {}", |
| 484 | numOfHandlerExecution, numOfEventsExecuted); |
| 485 | break; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 486 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 487 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 488 | if (event.type() == LinkEvent.Type.LINK_ADDED) { |
| 489 | processLinkAdded((Link) event.subject()); |
| 490 | } else if (event.type() == LinkEvent.Type.LINK_REMOVED) { |
| 491 | processLinkRemoved((Link) event.subject()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 492 | } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED || |
| 493 | event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 494 | event.type() == DeviceEvent.Type.DEVICE_UPDATED) { |
| 495 | if (deviceService.isAvailable(((Device) event.subject()).id())) { |
| 496 | processDeviceAdded((Device) event.subject()); |
| 497 | } |
| 498 | } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) { |
| 499 | processPortRemoved((Device) event.subject(), |
| 500 | ((DeviceEvent) event).port()); |
| 501 | } else { |
| 502 | log.warn("Unhandled event type: {}", event.type()); |
| 503 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 504 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 505 | } catch (Exception e) { |
| 506 | log.error("SegmentRouting event handler " |
| 507 | + "thread thrown an exception: {}", e); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 508 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 512 | private void processLinkAdded(Link link) { |
| 513 | log.debug("A new link {} was added", link.toString()); |
| 514 | |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 515 | //Irrespective whether the local is a MASTER or not for this device, |
| 516 | //create group handler instance and push default TTP flow rules. |
| 517 | //Because in a multi-instance setup, instances can initiate |
| 518 | //groups for any devices. Also the default TTP rules are needed |
| 519 | //to be pushed before inserting any IP table entries for any device |
| 520 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src() |
| 521 | .deviceId()); |
| 522 | if (groupHandler != null) { |
| 523 | groupHandler.linkUp(link); |
| 524 | } else { |
| 525 | Device device = deviceService.getDevice(link.src().deviceId()); |
| 526 | if (device != null) { |
| 527 | log.warn("processLinkAdded: Link Added " |
| 528 | + "Notification without Device Added " |
| 529 | + "event, still handling it"); |
| 530 | processDeviceAdded(device); |
| 531 | groupHandler = groupHandlerMap.get(link.src() |
| 532 | .deviceId()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 533 | groupHandler.linkUp(link); |
| 534 | } |
| 535 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 536 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 537 | log.trace("Starting optimized route population process"); |
| 538 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null); |
| 539 | //log.trace("processLinkAdded: re-starting route population process"); |
| 540 | //defaultRoutingHandler.startPopulationProcess(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | private void processLinkRemoved(Link link) { |
| 544 | log.debug("A link {} was removed", link.toString()); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 545 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId()); |
| 546 | if (groupHandler != null) { |
| 547 | groupHandler.portDown(link.src().port()); |
| 548 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 549 | log.trace("Starting optimized route population process"); |
| 550 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link); |
| 551 | //log.trace("processLinkRemoved: re-starting route population process"); |
| 552 | //defaultRoutingHandler.startPopulationProcess(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | private void processDeviceAdded(Device device) { |
| 556 | log.debug("A new device with ID {} was added", device.id()); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 557 | //Irrespective whether the local is a MASTER or not for this device, |
| 558 | //create group handler instance and push default TTP flow rules. |
| 559 | //Because in a multi-instance setup, instances can initiate |
| 560 | //groups for any devices. Also the default TTP rules are needed |
| 561 | //to be pushed before inserting any IP table entries for any device |
| 562 | DefaultGroupHandler dgh = DefaultGroupHandler. |
| 563 | createGroupHandler(device.id(), |
| 564 | appId, |
| 565 | deviceConfiguration, |
| 566 | linkService, |
| 567 | flowObjectiveService, |
| 568 | nsNextObjStore); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 569 | groupHandlerMap.put(device.id(), dgh); |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 570 | defaultRoutingHandler.populatePortAddressingRules(device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | private void processPortRemoved(Device device, Port port) { |
| 574 | log.debug("Port {} was removed", port.toString()); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 575 | DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 576 | if (groupHandler != null) { |
| 577 | groupHandler.portDown(port.number()); |
| 578 | } |
| 579 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 580 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 581 | private class InternalConfigListener implements NetworkConfigListener { |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 582 | SegmentRoutingManager segmentRoutingManager; |
| 583 | |
| 584 | public InternalConfigListener(SegmentRoutingManager srMgr) { |
| 585 | this.segmentRoutingManager = srMgr; |
| 586 | } |
| 587 | |
| 588 | public void configureNetwork() { |
| 589 | deviceConfiguration = new DeviceConfiguration(segmentRoutingManager.cfgService); |
| 590 | |
| 591 | arpHandler = new ArpHandler(segmentRoutingManager); |
| 592 | icmpHandler = new IcmpHandler(segmentRoutingManager); |
| 593 | ipHandler = new IpHandler(segmentRoutingManager); |
| 594 | routingRulePopulator = new RoutingRulePopulator(segmentRoutingManager); |
| 595 | defaultRoutingHandler = new DefaultRoutingHandler(segmentRoutingManager); |
| 596 | |
| 597 | tunnelHandler = new TunnelHandler(linkService, deviceConfiguration, |
| 598 | groupHandlerMap, tunnelStore); |
| 599 | policyHandler = new PolicyHandler(appId, deviceConfiguration, |
| 600 | flowObjectiveService, |
| 601 | tunnelHandler, policyStore); |
| 602 | |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 603 | for (Device device : deviceService.getDevices()) { |
| 604 | //Irrespective whether the local is a MASTER or not for this device, |
| 605 | //create group handler instance and push default TTP flow rules. |
| 606 | //Because in a multi-instance setup, instances can initiate |
| 607 | //groups for any devices. Also the default TTP rules are needed |
| 608 | //to be pushed before inserting any IP table entries for any device |
| 609 | DefaultGroupHandler groupHandler = DefaultGroupHandler |
| 610 | .createGroupHandler(device.id(), appId, |
| 611 | deviceConfiguration, linkService, |
| 612 | flowObjectiveService, |
| 613 | nsNextObjStore); |
| 614 | groupHandlerMap.put(device.id(), groupHandler); |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 615 | defaultRoutingHandler.populatePortAddressingRules(device.id()); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | defaultRoutingHandler.startPopulationProcess(); |
| 619 | } |
| 620 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 621 | @Override |
| 622 | public void event(NetworkConfigEvent event) { |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 623 | if (event.configClass().equals(SegmentRoutingConfig.class)) { |
| 624 | if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) { |
| 625 | log.info("Network configuration added."); |
| 626 | configureNetwork(); |
| 627 | } |
| 628 | if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) { |
| 629 | log.info("Network configuration updated."); |
| 630 | // TODO support dynamic configuration |
| 631 | } |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 635 | } |