sangho | b35a619 | 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 | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 25 | import org.onlab.packet.MacAddress; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 26 | import org.onlab.packet.VlanId; |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 27 | import org.onlab.packet.IPv4; |
| 28 | import org.onlab.packet.Ip4Address; |
| 29 | import org.onlab.packet.Ip4Prefix; |
| 30 | import org.onlab.packet.IpAddress; |
| 31 | import org.onlab.packet.IpPrefix; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 32 | import org.onlab.util.KryoNamespace; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
| 35 | import org.onosproject.event.Event; |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 41 | import org.onosproject.net.config.NetworkConfigListener; |
| 42 | import org.onosproject.net.config.basics.SubjectFactories; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 43 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 44 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 45 | import org.onosproject.net.flow.TrafficSelector; |
| 46 | import org.onosproject.net.flow.TrafficTreatment; |
| 47 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 48 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 49 | import org.onosproject.net.flowobjective.Objective; |
| 50 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 51 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 52 | import org.onosproject.net.host.HostEvent; |
| 53 | import org.onosproject.net.host.HostListener; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 54 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 55 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 56 | import org.onosproject.segmentrouting.config.SegmentRoutingConfig; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 57 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
| 58 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 59 | import org.onosproject.segmentrouting.grouphandler.NeighborSetNextObjectiveStoreKey; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 60 | import org.onosproject.mastership.MastershipService; |
| 61 | import org.onosproject.net.Device; |
| 62 | import org.onosproject.net.DeviceId; |
| 63 | import org.onosproject.net.Link; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 64 | import org.onosproject.net.Port; |
| 65 | import org.onosproject.net.device.DeviceEvent; |
| 66 | import org.onosproject.net.device.DeviceListener; |
| 67 | import org.onosproject.net.device.DeviceService; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 68 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 69 | import org.onosproject.net.host.HostService; |
| 70 | import org.onosproject.net.intent.IntentService; |
| 71 | import org.onosproject.net.link.LinkEvent; |
| 72 | import org.onosproject.net.link.LinkListener; |
| 73 | import org.onosproject.net.link.LinkService; |
| 74 | import org.onosproject.net.packet.InboundPacket; |
| 75 | import org.onosproject.net.packet.PacketContext; |
| 76 | import org.onosproject.net.packet.PacketProcessor; |
| 77 | import org.onosproject.net.packet.PacketService; |
| 78 | import org.onosproject.net.topology.TopologyService; |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 79 | import org.onosproject.segmentrouting.grouphandler.SubnetNextObjectiveStoreKey; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 80 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 81 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 82 | import org.onosproject.store.service.StorageService; |
| 83 | import org.onosproject.store.service.WallClockTimestamp; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 84 | import org.slf4j.Logger; |
| 85 | import org.slf4j.LoggerFactory; |
| 86 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 87 | import java.net.URI; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 88 | import java.util.Collections; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 89 | import java.util.HashSet; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 90 | import java.util.List; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 91 | import java.util.Map; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 92 | import java.util.Set; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 93 | import java.util.concurrent.ConcurrentHashMap; |
| 94 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 95 | import java.util.concurrent.Executors; |
| 96 | import java.util.concurrent.ScheduledExecutorService; |
| 97 | import java.util.concurrent.ScheduledFuture; |
| 98 | import java.util.concurrent.TimeUnit; |
| 99 | |
| 100 | @SuppressWarnings("ALL") |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 101 | @Service |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 102 | @Component(immediate = true) |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 103 | public class SegmentRoutingManager implements SegmentRoutingService { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 104 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 105 | private static Logger log = LoggerFactory |
| 106 | .getLogger(SegmentRoutingManager.class); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 107 | |
| 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected CoreService coreService; |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | protected TopologyService topologyService; |
| 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected PacketService packetService; |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 118 | protected IntentService intentService; |
| 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 121 | protected HostService hostService; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected DeviceService deviceService; |
| 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 127 | protected FlowObjectiveService flowObjectiveService; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 130 | protected LinkService linkService; |
| 131 | |
| 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 133 | protected MastershipService mastershipService; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 134 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 135 | protected ArpHandler arpHandler = null; |
| 136 | protected IcmpHandler icmpHandler = null; |
| 137 | protected IpHandler ipHandler = null; |
| 138 | protected RoutingRulePopulator routingRulePopulator = null; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 139 | protected ApplicationId appId; |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 140 | protected DeviceConfiguration deviceConfiguration = null; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 141 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 142 | private DefaultRoutingHandler defaultRoutingHandler = null; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 143 | private TunnelHandler tunnelHandler = null; |
| 144 | private PolicyHandler policyHandler = null; |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 145 | private InternalPacketProcessor processor = null; |
| 146 | private InternalLinkListener linkListener = null; |
| 147 | private InternalDeviceListener deviceListener = null; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 148 | private InternalEventHandler eventHandler = new InternalEventHandler(); |
| 149 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 150 | private ScheduledExecutorService executorService = Executors |
| 151 | .newScheduledThreadPool(1); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 152 | |
| 153 | private static ScheduledFuture<?> eventHandlerFuture = null; |
| 154 | private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<Event>(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 155 | private Map<DeviceId, DefaultGroupHandler> groupHandlerMap = |
| 156 | new ConcurrentHashMap<DeviceId, DefaultGroupHandler>(); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 157 | // Per device next objective ID store with (device id + neighbor set) as key |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 158 | private EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer> |
| 159 | nsNextObjStore = null; |
| 160 | private EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer> |
| 161 | subnetNextObjStore = null; |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 162 | private EventuallyConsistentMap<String, Tunnel> tunnelStore = null; |
| 163 | private EventuallyConsistentMap<String, Policy> policyStore = null; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 164 | // Per device, per-subnet assigned-vlans store, with (device id + subnet |
| 165 | // IPv4 prefix) as key |
| 166 | private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId> |
| 167 | subnetVidStore = null; |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 168 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 169 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 170 | protected StorageService storageService; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 171 | |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 172 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 173 | protected NetworkConfigRegistry cfgService; |
| 174 | |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 175 | private final InternalConfigListener cfgListener = |
| 176 | new InternalConfigListener(this); |
| 177 | |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 178 | private final ConfigFactory cfgFactory = |
| 179 | new ConfigFactory(SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 180 | SegmentRoutingConfig.class, |
| 181 | "segmentrouting") { |
| 182 | @Override |
| 183 | public SegmentRoutingConfig createConfig() { |
| 184 | return new SegmentRoutingConfig(); |
| 185 | } |
| 186 | }; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 187 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame^] | 188 | private final InternalHostListener hostListener = new InternalHostListener(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 189 | |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 190 | private Object threadSchedulerLock = new Object(); |
| 191 | private static int numOfEventsQueued = 0; |
| 192 | private static int numOfEventsExecuted = 0; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 193 | private static int numOfHandlerExecution = 0; |
| 194 | private static int numOfHandlerScheduled = 0; |
| 195 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 196 | private KryoNamespace.Builder kryoBuilder = null; |
| 197 | |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 198 | private static final short ASSIGNED_VLAN_START = 4093; |
| 199 | public static final short ASSIGNED_VLAN_NO_SUBNET = 4094; |
| 200 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 201 | @Activate |
| 202 | protected void activate() { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 203 | appId = coreService |
| 204 | .registerApplication("org.onosproject.segmentrouting"); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 205 | |
| 206 | kryoBuilder = new KryoNamespace.Builder() |
| 207 | .register(NeighborSetNextObjectiveStoreKey.class, |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 208 | SubnetNextObjectiveStoreKey.class, |
| 209 | SubnetAssignedVidStoreKey.class, |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 210 | NeighborSet.class, |
| 211 | DeviceId.class, |
| 212 | URI.class, |
| 213 | WallClockTimestamp.class, |
| 214 | org.onosproject.cluster.NodeId.class, |
| 215 | HashSet.class, |
| 216 | Tunnel.class, |
| 217 | DefaultTunnel.class, |
| 218 | Policy.class, |
| 219 | TunnelPolicy.class, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 220 | Policy.Type.class, |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 221 | VlanId.class, |
| 222 | Ip4Address.class, |
| 223 | Ip4Prefix.class, |
| 224 | IpAddress.Version.class, |
| 225 | ConnectPoint.class |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 226 | ); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 227 | |
| 228 | log.debug("Creating EC map nsnextobjectivestore"); |
| 229 | EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer> |
| 230 | nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 231 | |
| 232 | nsNextObjStore = nsNextObjMapBuilder |
| 233 | .withName("nsnextobjectivestore") |
| 234 | .withSerializer(kryoBuilder) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 235 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 236 | .build(); |
| 237 | log.trace("Current size {}", nsNextObjStore.size()); |
| 238 | |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 239 | log.debug("Creating EC map subnetnextobjectivestore"); |
| 240 | EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer> |
| 241 | subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 242 | |
| 243 | subnetNextObjStore = subnetNextObjMapBuilder |
| 244 | .withName("subnetnextobjectivestore") |
| 245 | .withSerializer(kryoBuilder) |
| 246 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 247 | .build(); |
| 248 | |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 249 | EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder = |
| 250 | storageService.eventuallyConsistentMapBuilder(); |
| 251 | |
| 252 | tunnelStore = tunnelMapBuilder |
| 253 | .withName("tunnelstore") |
| 254 | .withSerializer(kryoBuilder) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 255 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 256 | .build(); |
| 257 | |
| 258 | EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder = |
| 259 | storageService.eventuallyConsistentMapBuilder(); |
| 260 | |
| 261 | policyStore = policyMapBuilder |
| 262 | .withName("policystore") |
| 263 | .withSerializer(kryoBuilder) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 264 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 265 | .build(); |
| 266 | |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 267 | EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId> |
| 268 | subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 269 | |
| 270 | subnetVidStore = subnetVidStoreMapBuilder |
| 271 | .withName("subnetvidstore") |
| 272 | .withSerializer(kryoBuilder) |
| 273 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 274 | .build(); |
| 275 | |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 276 | cfgService.addListener(cfgListener); |
| 277 | cfgService.registerConfigFactory(cfgFactory); |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 278 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 279 | hostService.addListener(hostListener); |
| 280 | |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 281 | processor = new InternalPacketProcessor(); |
| 282 | linkListener = new InternalLinkListener(); |
| 283 | deviceListener = new InternalDeviceListener(); |
| 284 | |
| 285 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 286 | linkService.addListener(linkListener); |
| 287 | deviceService.addListener(deviceListener); |
| 288 | |
| 289 | cfgListener.configureNetwork(); |
| 290 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 291 | log.info("Started"); |
| 292 | } |
| 293 | |
| 294 | @Deactivate |
| 295 | protected void deactivate() { |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 296 | cfgService.removeListener(cfgListener); |
| 297 | cfgService.unregisterConfigFactory(cfgFactory); |
| 298 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 299 | packetService.removeProcessor(processor); |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 300 | linkService.removeListener(linkListener); |
| 301 | deviceService.removeListener(deviceListener); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 302 | processor = null; |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 303 | linkListener = null; |
| 304 | deviceService = null; |
| 305 | |
| 306 | groupHandlerMap.clear(); |
| 307 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 308 | log.info("Stopped"); |
| 309 | } |
| 310 | |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 311 | |
| 312 | @Override |
| 313 | public List<Tunnel> getTunnels() { |
| 314 | return tunnelHandler.getTunnels(); |
| 315 | } |
| 316 | |
| 317 | @Override |
sangho | 71abe1b | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 318 | public TunnelHandler.Result createTunnel(Tunnel tunnel) { |
| 319 | return tunnelHandler.createTunnel(tunnel); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | @Override |
sangho | 71abe1b | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 323 | public TunnelHandler.Result removeTunnel(Tunnel tunnel) { |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 324 | for (Policy policy: policyHandler.getPolicies()) { |
| 325 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 326 | TunnelPolicy tunnelPolicy = (TunnelPolicy) policy; |
| 327 | if (tunnelPolicy.tunnelId().equals(tunnel.id())) { |
| 328 | log.warn("Cannot remove the tunnel used by a policy"); |
sangho | 71abe1b | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 329 | return TunnelHandler.Result.TUNNEL_IN_USE; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | } |
sangho | 71abe1b | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 333 | return tunnelHandler.removeTunnel(tunnel); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | @Override |
sangho | 71abe1b | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 337 | public PolicyHandler.Result removePolicy(Policy policy) { |
| 338 | return policyHandler.removePolicy(policy); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | @Override |
sangho | 71abe1b | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 342 | public PolicyHandler.Result createPolicy(Policy policy) { |
| 343 | return policyHandler.createPolicy(policy); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public List<Policy> getPolicies() { |
| 348 | return policyHandler.getPolicies(); |
| 349 | } |
| 350 | |
sangho | f9d0bf1 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 351 | /** |
| 352 | * Returns the tunnel object with the tunnel ID. |
| 353 | * |
| 354 | * @param tunnelId Tunnel ID |
| 355 | * @return Tunnel reference |
| 356 | */ |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 357 | public Tunnel getTunnel(String tunnelId) { |
| 358 | return tunnelHandler.getTunnel(tunnelId); |
| 359 | } |
| 360 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 361 | /** |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 362 | * Returns the vlan-id assigned to the subnet configured for a device. |
| 363 | * If no vlan-id has been assigned, a new one is assigned out of a pool of ids, |
| 364 | * if and only if this controller instance is the master for the device. |
| 365 | * <p> |
| 366 | * USAGE: The assigned vlans are meant to be applied to untagged packets on those |
| 367 | * switches/pipelines that need this functionality. These vids are meant |
| 368 | * to be used internally within a switch, and thus need to be unique only |
| 369 | * on a switch level. Note that packets never go out on the wire with these |
| 370 | * vlans. Currently, vlan ids are assigned from value 4093 down. |
| 371 | * Vlan id 4094 expected to be used for all ports that are not assigned subnets. |
| 372 | * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned |
| 373 | * per subnet. |
| 374 | * XXX This method should avoid any vlans configured on the ports, but |
| 375 | * currently the app works only on untagged packets and as a result |
| 376 | * ignores any vlan configuration. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 377 | * |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 378 | * @param deviceId switch dpid |
| 379 | * @param subnet IPv4 prefix for which assigned vlan is desired |
| 380 | * @return VlanId assigned for the subnet on the device, or |
| 381 | * null if no vlan assignment was found and this instance is not |
| 382 | * the master for the device. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 383 | */ |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 384 | public VlanId getSubnetAssignedVlanId(DeviceId deviceId, Ip4Prefix subnet) { |
| 385 | VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey( |
| 386 | deviceId, subnet)); |
| 387 | if (assignedVid != null) { |
| 388 | log.debug("Query for subnet:{} on device:{} returned assigned-vlan " |
| 389 | + "{}", subnet, deviceId, assignedVid); |
| 390 | return assignedVid; |
| 391 | } |
| 392 | //check mastership for the right to assign a vlan |
| 393 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 394 | log.warn("This controller instance is not the master for device {}. " |
| 395 | + "Cannot assign vlan-id for subnet {}", deviceId, subnet); |
| 396 | return null; |
| 397 | } |
| 398 | // vlan assignment is expensive but done only once |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 399 | Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId); |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 400 | Set<Short> assignedVlans = new HashSet<>(); |
| 401 | Set<Ip4Prefix> unassignedSubnets = new HashSet<>(); |
| 402 | for (Ip4Prefix sub : configuredSubnets) { |
| 403 | VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, |
| 404 | sub)); |
| 405 | if (v != null) { |
| 406 | assignedVlans.add(v.toShort()); |
| 407 | } else { |
| 408 | unassignedSubnets.add(sub); |
| 409 | } |
| 410 | } |
| 411 | short nextAssignedVlan = ASSIGNED_VLAN_START; |
| 412 | if (!assignedVlans.isEmpty()) { |
| 413 | nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1); |
| 414 | } |
| 415 | for (Ip4Prefix unsub : unassignedSubnets) { |
| 416 | subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub), |
| 417 | VlanId.vlanId(nextAssignedVlan--)); |
| 418 | log.info("Assigned vlan: {} to subnet: {} on device: {}", |
| 419 | nextAssignedVlan + 1, unsub, deviceId); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 422 | return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet)); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 423 | } |
| 424 | |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 425 | /** |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 426 | * Returns the next objective ID for the given NeighborSet. |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 427 | * If the nextObjective does not exist, a new one is created and |
| 428 | * it's id is returned. |
| 429 | * TODO move the side-effect creation of a Next Objective into a new method |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 430 | * |
sangho | f9d0bf1 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 431 | * @param deviceId Device ID |
| 432 | * @param ns NegighborSet |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 433 | * @param meta metadata passed into the creation of a Next Objective |
| 434 | * @return next objective ID or -1 if an error was encountered during the |
| 435 | * creation of the nextObjective |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 436 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 437 | public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns, |
| 438 | TrafficSelector meta) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 439 | if (groupHandlerMap.get(deviceId) != null) { |
| 440 | log.trace("getNextObjectiveId query in device {}", deviceId); |
| 441 | return groupHandlerMap |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 442 | .get(deviceId).getNextObjectiveId(ns, meta); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 443 | } else { |
| 444 | log.warn("getNextObjectiveId query in device {} not found", deviceId); |
| 445 | return -1; |
| 446 | } |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 449 | /** |
| 450 | * Returns the next objective ID for the Subnet given. If the nextObjectiveID does not exist, |
| 451 | * a new one is created and returned. |
| 452 | * |
| 453 | * @param deviceId Device ID |
| 454 | * @param prefix Subnet |
| 455 | * @return next objective ID |
| 456 | */ |
| 457 | public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) { |
| 458 | if (groupHandlerMap.get(deviceId) != null) { |
| 459 | log.trace("getSubnetNextObjectiveId query in device {}", deviceId); |
| 460 | return groupHandlerMap |
| 461 | .get(deviceId).getSubnetNextObjectiveId(prefix); |
| 462 | } else { |
| 463 | log.warn("getSubnetNextObjectiveId query in device {} not found", deviceId); |
| 464 | return -1; |
| 465 | } |
| 466 | } |
| 467 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 468 | private class InternalPacketProcessor implements PacketProcessor { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 469 | @Override |
| 470 | public void process(PacketContext context) { |
| 471 | |
| 472 | if (context.isHandled()) { |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | InboundPacket pkt = context.inPacket(); |
| 477 | Ethernet ethernet = pkt.parsed(); |
| 478 | |
| 479 | if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
| 480 | arpHandler.processPacketIn(pkt); |
| 481 | } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 482 | IPv4 ipPacket = (IPv4) ethernet.getPayload(); |
| 483 | ipHandler.addToPacketBuffer(ipPacket); |
| 484 | if (ipPacket.getProtocol() == IPv4.PROTOCOL_ICMP) { |
| 485 | icmpHandler.processPacketIn(pkt); |
| 486 | } else { |
| 487 | ipHandler.processPacketIn(pkt); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | private class InternalLinkListener implements LinkListener { |
| 494 | @Override |
| 495 | public void event(LinkEvent event) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 496 | if (event.type() == LinkEvent.Type.LINK_ADDED |
| 497 | || event.type() == LinkEvent.Type.LINK_REMOVED) { |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 498 | log.debug("Event {} received from Link Service", event.type()); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 499 | scheduleEventHandlerIfNotScheduled(event); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | private class InternalDeviceListener implements DeviceListener { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 505 | @Override |
| 506 | public void event(DeviceEvent event) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 507 | switch (event.type()) { |
| 508 | case DEVICE_ADDED: |
| 509 | case PORT_REMOVED: |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 510 | case DEVICE_UPDATED: |
| 511 | case DEVICE_AVAILABILITY_CHANGED: |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 512 | log.debug("Event {} received from Device Service", event.type()); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 513 | scheduleEventHandlerIfNotScheduled(event); |
| 514 | break; |
| 515 | default: |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 520 | private void scheduleEventHandlerIfNotScheduled(Event event) { |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 521 | synchronized (threadSchedulerLock) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 522 | eventQueue.add(event); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 523 | numOfEventsQueued++; |
| 524 | |
| 525 | if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) { |
| 526 | //No pending scheduled event handling threads. So start a new one. |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 527 | eventHandlerFuture = executorService |
| 528 | .schedule(eventHandler, 100, TimeUnit.MILLISECONDS); |
| 529 | numOfHandlerScheduled++; |
| 530 | } |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 531 | log.trace("numOfEventsQueued {}, numOfEventHanlderScheduled {}", |
| 532 | numOfEventsQueued, |
| 533 | numOfHandlerScheduled); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 534 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | private class InternalEventHandler implements Runnable { |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 538 | @Override |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 539 | public void run() { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 540 | try { |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 541 | while (true) { |
| 542 | Event event = null; |
| 543 | synchronized (threadSchedulerLock) { |
| 544 | if (!eventQueue.isEmpty()) { |
| 545 | event = eventQueue.poll(); |
| 546 | numOfEventsExecuted++; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 547 | } else { |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 548 | numOfHandlerExecution++; |
| 549 | log.debug("numOfHandlerExecution {} numOfEventsExecuted {}", |
| 550 | numOfHandlerExecution, numOfEventsExecuted); |
| 551 | break; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 552 | } |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 553 | } |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 554 | if (event.type() == LinkEvent.Type.LINK_ADDED) { |
| 555 | processLinkAdded((Link) event.subject()); |
| 556 | } else if (event.type() == LinkEvent.Type.LINK_REMOVED) { |
| 557 | processLinkRemoved((Link) event.subject()); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 558 | } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED || |
| 559 | event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 560 | event.type() == DeviceEvent.Type.DEVICE_UPDATED) { |
| 561 | if (deviceService.isAvailable(((Device) event.subject()).id())) { |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 562 | log.info("Processing device event {} for available device {}", |
| 563 | event.type(), ((Device) event.subject()).id()); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 564 | processDeviceAdded((Device) event.subject()); |
| 565 | } |
| 566 | } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) { |
| 567 | processPortRemoved((Device) event.subject(), |
| 568 | ((DeviceEvent) event).port()); |
| 569 | } else { |
| 570 | log.warn("Unhandled event type: {}", event.type()); |
| 571 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 572 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 573 | } catch (Exception e) { |
| 574 | log.error("SegmentRouting event handler " |
| 575 | + "thread thrown an exception: {}", e); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 576 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 580 | private void processLinkAdded(Link link) { |
| 581 | log.debug("A new link {} was added", link.toString()); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 582 | if (!deviceConfiguration.isConfigured(link.src().deviceId())) { |
| 583 | log.warn("Source device of this link is not configured."); |
| 584 | return; |
| 585 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 586 | //Irrespective whether the local is a MASTER or not for this device, |
| 587 | //create group handler instance and push default TTP flow rules. |
| 588 | //Because in a multi-instance setup, instances can initiate |
| 589 | //groups for any devices. Also the default TTP rules are needed |
| 590 | //to be pushed before inserting any IP table entries for any device |
| 591 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src() |
| 592 | .deviceId()); |
| 593 | if (groupHandler != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 594 | groupHandler.linkUp(link, mastershipService.isLocalMaster( |
| 595 | link.src().deviceId())); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 596 | } else { |
| 597 | Device device = deviceService.getDevice(link.src().deviceId()); |
| 598 | if (device != null) { |
| 599 | log.warn("processLinkAdded: Link Added " |
| 600 | + "Notification without Device Added " |
| 601 | + "event, still handling it"); |
| 602 | processDeviceAdded(device); |
| 603 | groupHandler = groupHandlerMap.get(link.src() |
| 604 | .deviceId()); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 605 | groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id())); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 608 | |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 609 | log.trace("Starting optimized route population process"); |
| 610 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null); |
| 611 | //log.trace("processLinkAdded: re-starting route population process"); |
| 612 | //defaultRoutingHandler.startPopulationProcess(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | private void processLinkRemoved(Link link) { |
| 616 | log.debug("A link {} was removed", link.toString()); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 617 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId()); |
| 618 | if (groupHandler != null) { |
| 619 | groupHandler.portDown(link.src().port()); |
| 620 | } |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 621 | log.trace("Starting optimized route population process"); |
| 622 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link); |
| 623 | //log.trace("processLinkRemoved: re-starting route population process"); |
| 624 | //defaultRoutingHandler.startPopulationProcess(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | private void processDeviceAdded(Device device) { |
| 628 | log.debug("A new device with ID {} was added", device.id()); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 629 | if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) { |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 630 | log.warn("Device configuration uploading. Device {} will be " |
| 631 | + "processed after config completes.", device.id()); |
| 632 | return; |
| 633 | } |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 634 | // Irrespective of whether the local is a MASTER or not for this device, |
| 635 | // we need to create a SR-group-handler instance. This is because in a |
| 636 | // multi-instance setup, any instance can initiate forwarding/next-objectives |
| 637 | // for any switch (even if this instance is a SLAVE or not even connected |
| 638 | // to the switch). To handle this, a default-group-handler instance is necessary |
| 639 | // per switch. |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 640 | if (groupHandlerMap.get(device.id()) == null) { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 641 | DefaultGroupHandler groupHandler; |
| 642 | try { |
| 643 | groupHandler = DefaultGroupHandler. |
| 644 | createGroupHandler(device.id(), |
| 645 | appId, |
| 646 | deviceConfiguration, |
| 647 | linkService, |
| 648 | flowObjectiveService, |
| 649 | nsNextObjStore, |
| 650 | subnetNextObjStore); |
| 651 | } catch (DeviceConfigNotFoundException e) { |
| 652 | log.warn(e.getMessage() + " Aborting processDeviceAdded."); |
| 653 | return; |
| 654 | } |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 655 | groupHandlerMap.put(device.id(), groupHandler); |
| 656 | // Also, in some cases, drivers may need extra |
| 657 | // information to process rules (eg. Router IP/MAC); and so, we send |
| 658 | // port addressing rules to the driver as well irrespective of whether |
| 659 | // this instance is the master or not. |
| 660 | defaultRoutingHandler.populatePortAddressingRules(device.id()); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame^] | 661 | hostListener.readInitialHosts(); |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 662 | } |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 663 | if (mastershipService.isLocalMaster(device.id())) { |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 664 | DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id()); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 665 | groupHandler.createGroupsFromSubnetConfig(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 666 | routingRulePopulator.populateSubnetBroadcastRule(device.id()); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 667 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | private void processPortRemoved(Device device, Port port) { |
| 671 | log.debug("Port {} was removed", port.toString()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 672 | DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id()); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 673 | if (groupHandler != null) { |
| 674 | groupHandler.portDown(port.number()); |
| 675 | } |
| 676 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 677 | |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 678 | private class InternalConfigListener implements NetworkConfigListener { |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 679 | SegmentRoutingManager segmentRoutingManager; |
| 680 | |
| 681 | public InternalConfigListener(SegmentRoutingManager srMgr) { |
| 682 | this.segmentRoutingManager = srMgr; |
| 683 | } |
| 684 | |
| 685 | public void configureNetwork() { |
| 686 | deviceConfiguration = new DeviceConfiguration(segmentRoutingManager.cfgService); |
| 687 | |
| 688 | arpHandler = new ArpHandler(segmentRoutingManager); |
| 689 | icmpHandler = new IcmpHandler(segmentRoutingManager); |
| 690 | ipHandler = new IpHandler(segmentRoutingManager); |
| 691 | routingRulePopulator = new RoutingRulePopulator(segmentRoutingManager); |
| 692 | defaultRoutingHandler = new DefaultRoutingHandler(segmentRoutingManager); |
| 693 | |
| 694 | tunnelHandler = new TunnelHandler(linkService, deviceConfiguration, |
| 695 | groupHandlerMap, tunnelStore); |
| 696 | policyHandler = new PolicyHandler(appId, deviceConfiguration, |
| 697 | flowObjectiveService, |
| 698 | tunnelHandler, policyStore); |
| 699 | |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 700 | for (Device device : deviceService.getDevices()) { |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 701 | // Irrespective of whether the local is a MASTER or not for this device, |
| 702 | // we need to create a SR-group-handler instance. This is because in a |
| 703 | // multi-instance setup, any instance can initiate forwarding/next-objectives |
| 704 | // for any switch (even if this instance is a SLAVE or not even connected |
| 705 | // to the switch). To handle this, a default-group-handler instance is necessary |
| 706 | // per switch. |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 707 | if (groupHandlerMap.get(device.id()) == null) { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 708 | DefaultGroupHandler groupHandler; |
| 709 | try { |
| 710 | groupHandler = DefaultGroupHandler. |
| 711 | createGroupHandler(device.id(), |
| 712 | appId, |
| 713 | deviceConfiguration, |
| 714 | linkService, |
| 715 | flowObjectiveService, |
| 716 | nsNextObjStore, |
| 717 | subnetNextObjStore); |
| 718 | } catch (DeviceConfigNotFoundException e) { |
| 719 | log.warn(e.getMessage() + " Aborting configureNetwork."); |
| 720 | return; |
| 721 | } |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 722 | groupHandlerMap.put(device.id(), groupHandler); |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 723 | |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 724 | // Also, in some cases, drivers may need extra |
| 725 | // information to process rules (eg. Router IP/MAC); and so, we send |
| 726 | // port addressing rules to the driver as well, irrespective of whether |
| 727 | // this instance is the master or not. |
| 728 | defaultRoutingHandler.populatePortAddressingRules(device.id()); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame^] | 729 | hostListener.readInitialHosts(); |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 730 | } |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 731 | if (mastershipService.isLocalMaster(device.id())) { |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 732 | DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id()); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 733 | groupHandler.createGroupsFromSubnetConfig(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 734 | routingRulePopulator.populateSubnetBroadcastRule(device.id()); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 735 | } |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | defaultRoutingHandler.startPopulationProcess(); |
| 739 | } |
| 740 | |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 741 | @Override |
| 742 | public void event(NetworkConfigEvent event) { |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 743 | if (event.configClass().equals(SegmentRoutingConfig.class)) { |
| 744 | if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) { |
| 745 | log.info("Network configuration added."); |
| 746 | configureNetwork(); |
| 747 | } |
| 748 | if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) { |
| 749 | log.info("Network configuration updated."); |
| 750 | // TODO support dynamic configuration |
| 751 | } |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | } |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 755 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame^] | 756 | // TODO Move bridging table population to a separate class |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 757 | private class InternalHostListener implements HostListener { |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame^] | 758 | private void readInitialHosts() { |
| 759 | hostService.getHosts().forEach(host -> { |
| 760 | MacAddress mac = host.mac(); |
| 761 | VlanId vlanId = host.vlan(); |
| 762 | DeviceId deviceId = host.location().deviceId(); |
| 763 | PortNumber port = host.location().port(); |
| 764 | Set<IpAddress> ips = host.ipAddresses(); |
| 765 | log.debug("Host {}/{} is added at {}:{}", mac, vlanId, deviceId, port); |
| 766 | |
| 767 | // Populate bridging table entry |
| 768 | ForwardingObjective.Builder fob = |
| 769 | getForwardingObjectiveBuilder(mac, vlanId, port); |
| 770 | flowObjectiveService.forward(deviceId, fob.add( |
| 771 | new BridgingTableObjectiveContext(mac, vlanId) |
| 772 | )); |
| 773 | |
| 774 | // Populate IP table entry |
| 775 | ips.forEach(ip -> { |
| 776 | if (ip.isIp4()) { |
| 777 | routingRulePopulator.populateIpRuleForHost( |
| 778 | deviceId, ip.getIp4Address(), mac, port); |
| 779 | } |
| 780 | }); |
| 781 | }); |
| 782 | } |
| 783 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 784 | private ForwardingObjective.Builder getForwardingObjectiveBuilder( |
| 785 | MacAddress mac, VlanId vlanId, PortNumber port) { |
| 786 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 787 | sbuilder.matchEthDst(mac); |
| 788 | sbuilder.matchVlanId(vlanId); |
| 789 | |
| 790 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 791 | // TODO Move popVlan from flow action to group action |
| 792 | tbuilder.immediate().popVlan(); |
| 793 | tbuilder.immediate().setOutput(port); |
| 794 | |
| 795 | return DefaultForwardingObjective.builder() |
| 796 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 797 | .withSelector(sbuilder.build()) |
| 798 | .withTreatment(tbuilder.build()) |
| 799 | .withPriority(100) |
| 800 | .fromApp(appId) |
| 801 | .makePermanent(); |
| 802 | } |
| 803 | |
| 804 | private void processHostAddedEvent(HostEvent event) { |
| 805 | MacAddress mac = event.subject().mac(); |
| 806 | VlanId vlanId = event.subject().vlan(); |
| 807 | DeviceId deviceId = event.subject().location().deviceId(); |
| 808 | PortNumber port = event.subject().location().port(); |
| 809 | Set<IpAddress> ips = event.subject().ipAddresses(); |
| 810 | log.debug("Host {}/{} is added at {}:{}", mac, vlanId, deviceId, port); |
| 811 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 812 | // Populate bridging table entry |
| 813 | ForwardingObjective.Builder fob = |
| 814 | getForwardingObjectiveBuilder(mac, vlanId, port); |
| 815 | flowObjectiveService.forward(deviceId, fob.add( |
| 816 | new BridgingTableObjectiveContext(mac, vlanId) |
| 817 | )); |
| 818 | |
| 819 | // Populate IP table entry |
| 820 | ips.forEach(ip -> { |
| 821 | if (ip.isIp4()) { |
| 822 | routingRulePopulator.populateIpRuleForHost( |
| 823 | deviceId, ip.getIp4Address(), mac, port); |
| 824 | } |
| 825 | }); |
| 826 | } |
| 827 | |
| 828 | private void processHostRemoveEvent(HostEvent event) { |
| 829 | MacAddress mac = event.subject().mac(); |
| 830 | VlanId vlanId = event.subject().vlan(); |
| 831 | DeviceId deviceId = event.subject().location().deviceId(); |
| 832 | PortNumber port = event.subject().location().port(); |
| 833 | Set<IpAddress> ips = event.subject().ipAddresses(); |
| 834 | log.debug("Host {}/{} is removed from {}:{}", mac, vlanId, deviceId, port); |
| 835 | |
| 836 | // Revoke bridging table entry |
| 837 | ForwardingObjective.Builder fob = |
| 838 | getForwardingObjectiveBuilder(mac, vlanId, port); |
| 839 | flowObjectiveService.forward(deviceId, fob.remove( |
| 840 | new BridgingTableObjectiveContext(mac, vlanId) |
| 841 | )); |
| 842 | |
| 843 | // Revoke IP table entry |
| 844 | ips.forEach(ip -> { |
| 845 | if (ip.isIp4()) { |
| 846 | routingRulePopulator.revokeIpRuleForHost( |
| 847 | deviceId, ip.getIp4Address(), mac, port); |
| 848 | } |
| 849 | }); |
| 850 | } |
| 851 | |
| 852 | private void processHostMovedEvent(HostEvent event) { |
| 853 | MacAddress mac = event.subject().mac(); |
| 854 | VlanId vlanId = event.subject().vlan(); |
| 855 | DeviceId prevDeviceId = event.prevSubject().location().deviceId(); |
| 856 | PortNumber prevPort = event.prevSubject().location().port(); |
| 857 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
| 858 | DeviceId newDeviceId = event.subject().location().deviceId(); |
| 859 | PortNumber newPort = event.subject().location().port(); |
| 860 | Set<IpAddress> newIps = event.subject().ipAddresses(); |
| 861 | log.debug("Host {}/{} is moved from {}:{} to {}:{}", |
| 862 | mac, vlanId, prevDeviceId, prevPort, newDeviceId, newPort); |
| 863 | |
| 864 | // Revoke previous bridging table entry |
| 865 | ForwardingObjective.Builder prevFob = |
| 866 | getForwardingObjectiveBuilder(mac, vlanId, prevPort); |
| 867 | flowObjectiveService.forward(prevDeviceId, prevFob.remove( |
| 868 | new BridgingTableObjectiveContext(mac, vlanId) |
| 869 | )); |
| 870 | |
| 871 | // Revoke previous IP table entry |
| 872 | prevIps.forEach(ip -> { |
| 873 | if (ip.isIp4()) { |
| 874 | routingRulePopulator.revokeIpRuleForHost( |
| 875 | prevDeviceId, ip.getIp4Address(), mac, prevPort); |
| 876 | } |
| 877 | }); |
| 878 | |
| 879 | // Populate new bridging table entry |
| 880 | ForwardingObjective.Builder newFob = |
| 881 | getForwardingObjectiveBuilder(mac, vlanId, prevPort); |
| 882 | flowObjectiveService.forward(newDeviceId, newFob.add( |
| 883 | new BridgingTableObjectiveContext(mac, vlanId) |
| 884 | )); |
| 885 | |
| 886 | // Populate new IP table entry |
| 887 | newIps.forEach(ip -> { |
| 888 | if (ip.isIp4()) { |
| 889 | routingRulePopulator.populateIpRuleForHost( |
| 890 | newDeviceId, ip.getIp4Address(), mac, newPort); |
| 891 | } |
| 892 | }); |
| 893 | } |
| 894 | |
| 895 | private void processHostUpdatedEvent(HostEvent event) { |
| 896 | MacAddress mac = event.subject().mac(); |
| 897 | VlanId vlanId = event.subject().vlan(); |
| 898 | DeviceId prevDeviceId = event.prevSubject().location().deviceId(); |
| 899 | PortNumber prevPort = event.prevSubject().location().port(); |
| 900 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
| 901 | DeviceId newDeviceId = event.subject().location().deviceId(); |
| 902 | PortNumber newPort = event.subject().location().port(); |
| 903 | Set<IpAddress> newIps = event.subject().ipAddresses(); |
| 904 | log.debug("Host {}/{} is updated", mac, vlanId); |
| 905 | |
| 906 | // Revoke previous IP table entry |
| 907 | prevIps.forEach(ip -> { |
| 908 | if (ip.isIp4()) { |
| 909 | routingRulePopulator.revokeIpRuleForHost( |
| 910 | prevDeviceId, ip.getIp4Address(), mac, prevPort); |
| 911 | } |
| 912 | }); |
| 913 | |
| 914 | // Populate new IP table entry |
| 915 | newIps.forEach(ip -> { |
| 916 | if (ip.isIp4()) { |
| 917 | routingRulePopulator.populateIpRuleForHost( |
| 918 | newDeviceId, ip.getIp4Address(), mac, newPort); |
| 919 | } |
| 920 | }); |
| 921 | } |
| 922 | |
| 923 | @Override |
| 924 | public void event(HostEvent event) { |
| 925 | // Do not proceed without mastership |
| 926 | DeviceId deviceId = event.subject().location().deviceId(); |
| 927 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | switch (event.type()) { |
| 932 | case HOST_ADDED: |
| 933 | processHostAddedEvent(event); |
| 934 | break; |
| 935 | case HOST_MOVED: |
| 936 | processHostMovedEvent(event); |
| 937 | break; |
| 938 | case HOST_REMOVED: |
| 939 | processHostRemoveEvent(event); |
| 940 | break; |
| 941 | case HOST_UPDATED: |
| 942 | processHostUpdatedEvent(event); |
| 943 | break; |
| 944 | default: |
| 945 | log.warn("Unsupported host event type: {}", event.type()); |
| 946 | break; |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | private static class BridgingTableObjectiveContext implements ObjectiveContext { |
| 952 | final MacAddress mac; |
| 953 | final VlanId vlanId; |
| 954 | |
| 955 | BridgingTableObjectiveContext(MacAddress mac, VlanId vlanId) { |
| 956 | this.mac = mac; |
| 957 | this.vlanId = vlanId; |
| 958 | } |
| 959 | |
| 960 | @Override |
| 961 | public void onSuccess(Objective objective) { |
| 962 | if (objective.op() == Objective.Operation.ADD) { |
| 963 | log.debug("Successfully populate bridging table entry for {}/{}", |
| 964 | mac, vlanId); |
| 965 | } else { |
| 966 | log.debug("Successfully revoke bridging table entry for {}/{}", |
| 967 | mac, vlanId); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | @Override |
| 972 | public void onError(Objective objective, ObjectiveError error) { |
| 973 | if (objective.op() == Objective.Operation.ADD) { |
| 974 | log.debug("Fail to populate bridging table entry for {}/{}. {}", |
| 975 | mac, vlanId, error); |
| 976 | } else { |
| 977 | log.debug("Fail to revoke bridging table entry for {}/{}. {}", |
| 978 | mac, vlanId, error); |
| 979 | } |
| 980 | } |
| 981 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 982 | } |