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