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