blob: 4c56db5001ff18b0ddec616c056dbbba22a4dc0d [file] [log] [blame]
Charles Chan1eaf4802016-04-18 13:44:03 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chan1eaf4802016-04-18 13:44:03 -07003 *
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
17package org.onosproject.segmentrouting;
18
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070019import org.onlab.packet.EthType;
Charles Chan1eaf4802016-04-18 13:44:03 -070020import org.onlab.packet.IpAddress;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070021import org.onlab.packet.IpPrefix;
Charles Chan1eaf4802016-04-18 13:44:03 -070022import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Charles Chan10b0fb72017-02-02 16:20:42 -080024import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070025import org.onosproject.net.DeviceId;
Charles Chan370a65b2016-05-10 17:29:47 -070026import org.onosproject.net.Host;
Charles Chanc22cef32016-04-29 14:38:22 -070027import org.onosproject.net.HostLocation;
Charles Chan1eaf4802016-04-18 13:44:03 -070028import org.onosproject.net.PortNumber;
Charles Chan1eaf4802016-04-18 13:44:03 -070029import org.onosproject.net.host.HostEvent;
30import org.onosproject.net.host.HostService;
Charles Chanc6bcdf92018-06-01 16:33:48 -070031import org.onosproject.net.host.ProbeMode;
Charles Chan1eaf4802016-04-18 13:44:03 -070032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
Saurav Dasf9332192017-02-18 14:05:44 -080035import com.google.common.collect.Sets;
Charles Chan3ed34d82017-06-22 18:03:14 -070036
Saurav Dase6c448a2018-01-18 12:07:33 -080037import java.util.HashSet;
Charles Chan3ed34d82017-06-22 18:03:14 -070038import java.util.Optional;
Charles Chan1eaf4802016-04-18 13:44:03 -070039import java.util.Set;
Charles Chand9265a32017-06-16 15:19:24 -070040import java.util.stream.Collectors;
41
42import static com.google.common.base.Preconditions.checkArgument;
Charles Chan1eaf4802016-04-18 13:44:03 -070043
44/**
45 * Handles host-related events.
46 */
47public class HostHandler {
48 private static final Logger log = LoggerFactory.getLogger(HostHandler.class);
Charles Chan873661e2017-11-30 15:37:50 -080049
Charles Chan114aec72017-06-19 14:00:53 -070050 protected final SegmentRoutingManager srManager;
Charles Chan1eaf4802016-04-18 13:44:03 -070051 private HostService hostService;
Charles Chan1eaf4802016-04-18 13:44:03 -070052
53 /**
54 * Constructs the HostHandler.
55 *
56 * @param srManager Segment Routing manager
57 */
Charles Chand9265a32017-06-16 15:19:24 -070058 HostHandler(SegmentRoutingManager srManager) {
Charles Chan1eaf4802016-04-18 13:44:03 -070059 this.srManager = srManager;
Charles Chan1eaf4802016-04-18 13:44:03 -070060 hostService = srManager.hostService;
Charles Chan1eaf4802016-04-18 13:44:03 -070061 }
62
Charles Chandebfea32016-10-24 14:52:01 -070063 protected void init(DeviceId devId) {
Charles Chand9265a32017-06-16 15:19:24 -070064 hostService.getHosts().forEach(host ->
65 host.locations().stream()
Charles Chan873661e2017-11-30 15:37:50 -080066 .filter(location -> location.deviceId().equals(devId) ||
67 location.deviceId().equals(srManager.getPairDeviceId(devId).orElse(null)))
Charles Chand9265a32017-06-16 15:19:24 -070068 .forEach(location -> processHostAddedAtLocation(host, location))
69 );
Charles Chanc22cef32016-04-29 14:38:22 -070070 }
Charles Chan1eaf4802016-04-18 13:44:03 -070071
Charles Chand9265a32017-06-16 15:19:24 -070072 void processHostAddedEvent(HostEvent event) {
Charles Chan41f5ec02016-06-13 18:54:31 -070073 processHostAdded(event.subject());
Charles Chanc22cef32016-04-29 14:38:22 -070074 }
75
Charles Chand9265a32017-06-16 15:19:24 -070076 private void processHostAdded(Host host) {
77 host.locations().forEach(location -> processHostAddedAtLocation(host, location));
Saurav Dase6c448a2018-01-18 12:07:33 -080078 // ensure dual-homed host locations have viable uplinks
Saurav Dasec683dc2018-04-27 18:42:30 -070079 if (host.locations().size() > 1 || srManager.singleHomedDown) {
Saurav Dase6c448a2018-01-18 12:07:33 -080080 host.locations().forEach(loc -> {
81 if (srManager.mastershipService.isLocalMaster(loc.deviceId())) {
Saurav Dasec683dc2018-04-27 18:42:30 -070082 srManager.linkHandler.checkUplinksForHost(loc);
Saurav Dase6c448a2018-01-18 12:07:33 -080083 }
84 });
85 }
Charles Chan1eaf4802016-04-18 13:44:03 -070086 }
87
Charles Chand9265a32017-06-16 15:19:24 -070088 void processHostAddedAtLocation(Host host, HostLocation location) {
89 checkArgument(host.locations().contains(location), "{} is not a location of {}", location, host);
90
Charles Chanceb2a2e2017-09-12 18:57:47 -070091 MacAddress hostMac = host.mac();
92 VlanId hostVlanId = host.vlan();
Charles Chand9265a32017-06-16 15:19:24 -070093 Set<HostLocation> locations = host.locations();
94 Set<IpAddress> ips = host.ipAddresses();
Charles Chanceb2a2e2017-09-12 18:57:47 -070095 log.info("Host {}/{} is added at {}", hostMac, hostVlanId, locations);
Charles Chand9265a32017-06-16 15:19:24 -070096
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070097 if (isDoubleTaggedHost(host)) {
98 ips.forEach(ip ->
99 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
100 host.innerVlan(), hostVlanId, host.tpid(), ip, false)
101 );
102 } else {
103 processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, false);
104 ips.forEach(ip ->
Charles Chand66d6712018-03-29 16:03:41 -0700105 processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, false)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700106 );
107 }
Charles Chanceb2a2e2017-09-12 18:57:47 -0700108
109 // Use the pair link temporarily before the second location of a dual-homed host shows up.
110 // This do not affect single-homed hosts since the flow will be blocked in
111 // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively
112 srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> {
Charles Chand66d6712018-03-29 16:03:41 -0700113 if (host.locations().stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700114 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> {
Charles Chanceb2a2e2017-09-12 18:57:47 -0700115 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
116 // when the host is untagged
Charles Chan868c9572018-06-15 18:54:18 -0700117 VlanId vlanId = vlanForPairPort(hostVlanId, location);
118 if (vlanId == null) {
119 return;
120 }
Charles Chanceb2a2e2017-09-12 18:57:47 -0700121
122 processBridgingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, false);
123 ips.forEach(ip -> processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId,
124 ip, false));
Charles Chan873661e2017-11-30 15:37:50 -0800125
126 if (srManager.activeProbing) {
127 probe(host, location, pairDeviceId, pairRemotePort);
128 }
Charles Chanceb2a2e2017-09-12 18:57:47 -0700129 });
130 }
131 });
Charles Chand9265a32017-06-16 15:19:24 -0700132 }
133
134 void processHostRemovedEvent(HostEvent event) {
Charles Chan41f5ec02016-06-13 18:54:31 -0700135 processHostRemoved(event.subject());
136 }
137
Charles Chand9265a32017-06-16 15:19:24 -0700138 private void processHostRemoved(Host host) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700139 MacAddress hostMac = host.mac();
140 VlanId hostVlanId = host.vlan();
Charles Chand9265a32017-06-16 15:19:24 -0700141 Set<HostLocation> locations = host.locations();
Charles Chan41f5ec02016-06-13 18:54:31 -0700142 Set<IpAddress> ips = host.ipAddresses();
Charles Chan3ed34d82017-06-22 18:03:14 -0700143 log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations);
Charles Chanc22cef32016-04-29 14:38:22 -0700144
Charles Chan910be6a2017-08-23 14:46:43 -0700145 locations.forEach(location -> {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700146 if (isDoubleTaggedHost(host)) {
147 ips.forEach(ip ->
148 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
149 host.innerVlan(), hostVlanId, host.tpid(), ip, true)
150 );
151 } else {
152 processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, true);
153 ips.forEach(ip ->
Charles Chand66d6712018-03-29 16:03:41 -0700154 processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700155 );
156 }
Charles Chan3ed34d82017-06-22 18:03:14 -0700157
158 // Also remove redirection flows on the pair device if exists.
159 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId());
Saurav Dasec683dc2018-04-27 18:42:30 -0700160 Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId());
Charles Chand66d6712018-03-29 16:03:41 -0700161 if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700162 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
163 // when the host is untagged
Charles Chan868c9572018-06-15 18:54:18 -0700164 VlanId vlanId = vlanForPairPort(hostVlanId, location);
165 if (vlanId == null) {
166 return;
167 }
Charles Chan3ed34d82017-06-22 18:03:14 -0700168
169 processBridgingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, true);
170 ips.forEach(ip ->
171 processRoutingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId,
172 ip, true));
173 }
Charles Chand9265a32017-06-16 15:19:24 -0700174 });
Charles Chanc22cef32016-04-29 14:38:22 -0700175 }
176
Charles Chand9265a32017-06-16 15:19:24 -0700177 void processHostMovedEvent(HostEvent event) {
Charles Chan4b5769a2018-04-25 18:51:46 -0400178 Host host = event.subject();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700179 MacAddress hostMac = host.mac();
180 VlanId hostVlanId = host.vlan();
Charles Chan4b5769a2018-04-25 18:51:46 -0400181 Set<HostLocation> prevLocations = event.prevSubject().locations();
182 Set<IpAddress> prevIps = event.prevSubject().ipAddresses();
183 Set<HostLocation> newLocations = host.locations();
184 Set<IpAddress> newIps = host.ipAddresses();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700185 EthType hostTpid = host.tpid();
186 boolean doubleTaggedHost = isDoubleTaggedHost(host);
Charles Chan4b5769a2018-04-25 18:51:46 -0400187
Charles Chan873661e2017-11-30 15:37:50 -0800188 log.info("Host {}/{} is moved from {} to {}", hostMac, hostVlanId, prevLocations, newLocations);
Charles Chand9265a32017-06-16 15:19:24 -0700189 Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId)
190 .collect(Collectors.toSet());
Charles Chanc22cef32016-04-29 14:38:22 -0700191
Charles Chand9265a32017-06-16 15:19:24 -0700192 // For each old location
Charles Chand66d6712018-03-29 16:03:41 -0700193 Sets.difference(prevLocations, newLocations).forEach(prevLocation -> {
Charles Chanc6bcdf92018-06-01 16:33:48 -0700194 // First of all, verify each old location
195 srManager.probingService.probeHost(host, prevLocation, ProbeMode.VERIFY);
196
Charles Chan3ed34d82017-06-22 18:03:14 -0700197 // Remove routing rules for old IPs
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700198 Sets.difference(prevIps, newIps).forEach(ip -> {
199 if (doubleTaggedHost) {
200 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
201 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
202 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700203 processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700204 ip, true);
205 }
206 });
Charles Chan3ed34d82017-06-22 18:03:14 -0700207
208 // Redirect the flows to pair link if configured
209 // Note: Do not continue removing any rule
210 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId());
Saurav Dasec683dc2018-04-27 18:42:30 -0700211 Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(prevLocation.deviceId());
Charles Chan3ed34d82017-06-22 18:03:14 -0700212 if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && newLocations.stream()
213 .anyMatch(location -> location.deviceId().equals(pairDeviceId.get()))) {
214 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
215 // when the host is untagged
216 VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(prevLocation)).orElse(hostVlanId);
217
218 processBridgingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, false);
219 newIps.forEach(ip ->
220 processRoutingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId,
221 ip, false));
222 return;
223 }
Charles Chand9265a32017-06-16 15:19:24 -0700224
Charles Chanfbcb8812018-04-18 18:41:05 -0700225 // Remove flows for unchanged IPs only when the host moves from a switch to another.
Charles Chand9265a32017-06-16 15:19:24 -0700226 // Otherwise, do not remove and let the adding part update the old flow
227 if (!newDeviceIds.contains(prevLocation.deviceId())) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700228 processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700229 Sets.intersection(prevIps, newIps).forEach(ip -> {
230 if (doubleTaggedHost) {
231 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
232 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
233 } else {
234 processRoutingRule(prevLocation.deviceId(), prevLocation.port(),
235 hostMac, hostVlanId, ip, true);
236 }
237 });
Charles Chand9265a32017-06-16 15:19:24 -0700238 }
239
240 // Remove bridging rules if new interface vlan is different from old interface vlan
241 // Otherwise, do not remove and let the adding part update the old flow
242 if (newLocations.stream().noneMatch(newLocation -> {
243 VlanId oldAssignedVlan = srManager.getInternalVlanId(prevLocation);
244 VlanId newAssignedVlan = srManager.getInternalVlanId(newLocation);
245 // Host is tagged and the new location has the host vlan in vlan-tagged
Charles Chan098ca202018-05-01 11:50:20 -0700246 return srManager.interfaceService.getTaggedVlanId(newLocation).contains(hostVlanId) ||
Charles Chand9265a32017-06-16 15:19:24 -0700247 (oldAssignedVlan != null && newAssignedVlan != null &&
248 // Host is untagged and the new location has the same assigned vlan
249 oldAssignedVlan.equals(newAssignedVlan));
250 })) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700251 processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true);
Charles Chand9265a32017-06-16 15:19:24 -0700252 }
253
254 // Remove routing rules for unchanged IPs if none of the subnet of new location contains
255 // the IP. Otherwise, do not remove and let the adding part update the old flow
256 Sets.intersection(prevIps, newIps).forEach(ip -> {
257 if (newLocations.stream().noneMatch(newLocation ->
258 srManager.deviceConfiguration.inSameSubnet(newLocation, ip))) {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700259 if (doubleTaggedHost) {
260 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
261 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
262 } else {
263 processRoutingRule(prevLocation.deviceId(), prevLocation.port(),
264 hostMac, hostVlanId, ip, true);
265 }
Charles Chand9265a32017-06-16 15:19:24 -0700266 }
Charles Chanc22cef32016-04-29 14:38:22 -0700267 });
Charles Chand9265a32017-06-16 15:19:24 -0700268 });
269
270 // For each new location, add all new IPs.
Charles Chanfbcb8812018-04-18 18:41:05 -0700271 Sets.difference(newLocations, prevLocations).forEach(newLocation -> {
Charles Chan3ed34d82017-06-22 18:03:14 -0700272 processBridgingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, false);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700273 newIps.forEach(ip -> {
274 if (doubleTaggedHost) {
275 processDoubleTaggedRoutingRule(newLocation.deviceId(), newLocation.port(),
276 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false);
277 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700278 processRoutingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700279 ip, false);
280 }
281 });
Charles Chan4b5769a2018-04-25 18:51:46 -0400282
283 // Probe on pair device when host move
284 // Majorly for the 2nd step of [1A/x, 1B/x] -> [1A/x, 1B/y] -> [1A/y, 1B/y]
285 // But will also cover [1A/x] -> [1A/y] -> [1A/y, 1B/y]
286 if (srManager.activeProbing) {
287 srManager.getPairDeviceId(newLocation.deviceId()).ifPresent(pairDeviceId ->
288 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort ->
289 probe(host, newLocation, pairDeviceId, pairRemotePort)
290 )
291 );
292 }
Charles Chand9265a32017-06-16 15:19:24 -0700293 });
294
295 // For each unchanged location, add new IPs and remove old IPs.
Charles Chanfbcb8812018-04-18 18:41:05 -0700296 Sets.intersection(newLocations, prevLocations).forEach(unchangedLocation -> {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700297 Sets.difference(prevIps, newIps).forEach(ip -> {
298 if (doubleTaggedHost) {
299 processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
300 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
301 } else {
302 processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
303 hostMac, hostVlanId, ip, true);
304 }
305 });
Charles Chand9265a32017-06-16 15:19:24 -0700306
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700307 Sets.difference(newIps, prevIps).forEach(ip -> {
308 if (doubleTaggedHost) {
309 processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
310 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false);
311 } else {
312 processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
313 hostMac, hostVlanId, ip, false);
314 }
315 });
Charles Chand9265a32017-06-16 15:19:24 -0700316 });
Saurav Dase6c448a2018-01-18 12:07:33 -0800317
318 // ensure dual-homed host locations have viable uplinks
Saurav Dasec683dc2018-04-27 18:42:30 -0700319 if (newLocations.size() > prevLocations.size() || srManager.singleHomedDown) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800320 newLocations.forEach(loc -> {
321 if (srManager.mastershipService.isLocalMaster(loc.deviceId())) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700322 srManager.linkHandler.checkUplinksForHost(loc);
Saurav Dase6c448a2018-01-18 12:07:33 -0800323 }
324 });
325 }
Charles Chanc22cef32016-04-29 14:38:22 -0700326 }
327
Charles Chand9265a32017-06-16 15:19:24 -0700328 void processHostUpdatedEvent(HostEvent event) {
Charles Chan873661e2017-11-30 15:37:50 -0800329 Host host = event.subject();
330 MacAddress hostMac = host.mac();
331 VlanId hostVlanId = host.vlan();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700332 EthType hostTpid = host.tpid();
Charles Chan873661e2017-11-30 15:37:50 -0800333 Set<HostLocation> locations = host.locations();
Charles Chanc22cef32016-04-29 14:38:22 -0700334 Set<IpAddress> prevIps = event.prevSubject().ipAddresses();
Charles Chan873661e2017-11-30 15:37:50 -0800335 Set<IpAddress> newIps = host.ipAddresses();
Charles Chanb3c1faf2017-11-20 08:46:24 -0800336 log.info("Host {}/{} is updated", hostMac, hostVlanId);
Charles Chanc22cef32016-04-29 14:38:22 -0700337
Charles Chand66d6712018-03-29 16:03:41 -0700338 locations.forEach(location -> {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700339 Sets.difference(prevIps, newIps).forEach(ip -> {
340 if (isDoubleTaggedHost(host)) {
341 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
342 host.innerVlan(), hostVlanId, hostTpid, ip, true);
343 } else {
344 processRoutingRule(location.deviceId(), location.port(), hostMac,
345 hostVlanId, ip, true);
346 }
347 });
348 Sets.difference(newIps, prevIps).forEach(ip -> {
349 if (isDoubleTaggedHost(host)) {
350 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
351 host.innerVlan(), hostVlanId, hostTpid, ip, false);
352 } else {
353 processRoutingRule(location.deviceId(), location.port(), hostMac,
354 hostVlanId, ip, false);
355 }
356 });
Charles Chand9265a32017-06-16 15:19:24 -0700357 });
Charles Chanb3c1faf2017-11-20 08:46:24 -0800358
359 // Use the pair link temporarily before the second location of a dual-homed host shows up.
360 // This do not affect single-homed hosts since the flow will be blocked in
361 // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively
Charles Chan873661e2017-11-30 15:37:50 -0800362 locations.forEach(location ->
Charles Chanb3c1faf2017-11-20 08:46:24 -0800363 srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> {
Charles Chand66d6712018-03-29 16:03:41 -0700364 if (locations.stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) {
Charles Chan873661e2017-11-30 15:37:50 -0800365 Set<IpAddress> ipsToAdd = Sets.difference(newIps, prevIps);
366 Set<IpAddress> ipsToRemove = Sets.difference(prevIps, newIps);
367
Saurav Dasec683dc2018-04-27 18:42:30 -0700368 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> {
Charles Chanb3c1faf2017-11-20 08:46:24 -0800369 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
370 // when the host is untagged
Charles Chan868c9572018-06-15 18:54:18 -0700371 VlanId vlanId = vlanForPairPort(hostVlanId, location);
372 if (vlanId == null) {
373 return;
374 }
Charles Chanb3c1faf2017-11-20 08:46:24 -0800375
Charles Chan873661e2017-11-30 15:37:50 -0800376 ipsToRemove.forEach(ip ->
Charles Chanb3c1faf2017-11-20 08:46:24 -0800377 processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, true)
378 );
Charles Chan873661e2017-11-30 15:37:50 -0800379 ipsToAdd.forEach(ip ->
Charles Chanb3c1faf2017-11-20 08:46:24 -0800380 processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, false)
381 );
Charles Chan873661e2017-11-30 15:37:50 -0800382
383 if (srManager.activeProbing) {
384 probe(host, location, pairDeviceId, pairRemotePort);
385 }
Charles Chanb3c1faf2017-11-20 08:46:24 -0800386 });
387 }
Charles Chan873661e2017-11-30 15:37:50 -0800388 })
389 );
390 }
391
392 /**
393 * When a non-pair port comes up, probe each host on the pair device if
394 * (1) the host is tagged and the tagged vlan of current port contains host vlan; or
395 * (2) the host is untagged and the internal vlan is the same on the host port and current port.
396 *
397 * @param cp connect point
398 */
399 void processPortUp(ConnectPoint cp) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700400 if (cp.port().equals(srManager.getPairLocalPort(cp.deviceId()).orElse(null))) {
Charles Chan873661e2017-11-30 15:37:50 -0800401 return;
402 }
403 if (srManager.activeProbing) {
404 srManager.getPairDeviceId(cp.deviceId())
405 .ifPresent(pairDeviceId -> srManager.hostService.getConnectedHosts(pairDeviceId).stream()
406 .filter(host -> isHostInVlanOfPort(host, pairDeviceId, cp))
Charles Chanc6bcdf92018-06-01 16:33:48 -0700407 .forEach(host -> srManager.probingService.probeHost(host, cp, ProbeMode.DISCOVER))
Charles Chan873661e2017-11-30 15:37:50 -0800408 );
409 }
410 }
411
412 /**
413 * Checks if given host located on given device id matches VLAN config of current port.
414 *
415 * @param host host to check
416 * @param deviceId device id to check
417 * @param cp current connect point
418 * @return true if the host located at deviceId matches the VLAN config on cp
419 */
420 private boolean isHostInVlanOfPort(Host host, DeviceId deviceId, ConnectPoint cp) {
421 VlanId internalVlan = srManager.getInternalVlanId(cp);
Charles Chan098ca202018-05-01 11:50:20 -0700422 Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(cp);
Charles Chan873661e2017-11-30 15:37:50 -0800423
424 return taggedVlan.contains(host.vlan()) ||
425 (internalVlan != null && host.locations().stream()
426 .filter(l -> l.deviceId().equals(deviceId))
427 .map(srManager::getInternalVlanId)
428 .anyMatch(internalVlan::equals));
429 }
430
431 /**
432 * Send a probe on all locations with the same VLAN on pair device, excluding pair port.
433 *
434 * @param host host to probe
435 * @param location newly discovered host location
436 * @param pairDeviceId pair device id
437 * @param pairRemotePort pair remote port
438 */
439 private void probe(Host host, ConnectPoint location, DeviceId pairDeviceId, PortNumber pairRemotePort) {
440 VlanId vlanToProbe = host.vlan().equals(VlanId.NONE) ?
441 srManager.getInternalVlanId(location) : host.vlan();
442 srManager.interfaceService.getInterfaces().stream()
443 .filter(i -> i.vlanTagged().contains(vlanToProbe) ||
444 i.vlanUntagged().equals(vlanToProbe) ||
445 i.vlanNative().equals(vlanToProbe))
446 .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId))
447 .filter(i -> !i.connectPoint().port().equals(pairRemotePort))
448 .forEach(i -> {
449 log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint());
Charles Chanc6bcdf92018-06-01 16:33:48 -0700450 srManager.probingService.probeHost(host, i.connectPoint(), ProbeMode.DISCOVER);
Charles Chan873661e2017-11-30 15:37:50 -0800451 });
Charles Chanc22cef32016-04-29 14:38:22 -0700452 }
453
454 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700455 * Populates or revokes a bridging rule on given deviceId that matches given mac,
456 * given vlan and output to given port.
Charles Chan9595e6a2017-06-15 19:25:25 -0700457 *
458 * @param deviceId device ID
459 * @param port port
460 * @param mac mac address
461 * @param vlanId VLAN ID
462 * @param revoke true to revoke the rule; false to populate
463 */
464 private void processBridgingRule(DeviceId deviceId, PortNumber port, MacAddress mac,
465 VlanId vlanId, boolean revoke) {
Charles Chand66d6712018-03-29 16:03:41 -0700466 log.info("{} bridging entry for host {}/{} at {}:{}", revoke ? "Revoking" : "Populating",
Charles Chan9595e6a2017-06-15 19:25:25 -0700467 mac, vlanId, deviceId, port);
468
Charles Chand66d6712018-03-29 16:03:41 -0700469 if (!revoke) {
470 srManager.defaultRoutingHandler.populateBridging(deviceId, port, mac, vlanId);
471 } else {
472 srManager.defaultRoutingHandler.revokeBridging(deviceId, port, mac, vlanId);
Charles Chan9595e6a2017-06-15 19:25:25 -0700473 }
Charles Chan9595e6a2017-06-15 19:25:25 -0700474 }
475
476 /**
477 * Populate or revoke a routing rule on given deviceId that matches given ip,
478 * set destination mac to given mac, set vlan to given vlan and output to given port.
479 *
480 * @param deviceId device ID
481 * @param port port
482 * @param mac mac address
483 * @param vlanId VLAN ID
484 * @param ip IP address
485 * @param revoke true to revoke the rule; false to populate
486 */
487 private void processRoutingRule(DeviceId deviceId, PortNumber port, MacAddress mac,
488 VlanId vlanId, IpAddress ip, boolean revoke) {
489 ConnectPoint location = new ConnectPoint(deviceId, port);
Charles Chand9265a32017-06-16 15:19:24 -0700490 if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) {
491 log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port);
492 return;
Charles Chan9595e6a2017-06-15 19:25:25 -0700493 }
Charles Chan9595e6a2017-06-15 19:25:25 -0700494
Charles Chand9265a32017-06-16 15:19:24 -0700495 log.info("{} routing rule for {} at {}", revoke ? "Revoking" : "Populating", ip, location);
496 if (revoke) {
Charles Chan910be6a2017-08-23 14:46:43 -0700497 srManager.defaultRoutingHandler.revokeRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port);
Charles Chand9265a32017-06-16 15:19:24 -0700498 } else {
Charles Chan910be6a2017-08-23 14:46:43 -0700499 srManager.defaultRoutingHandler.populateRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port);
Charles Chan370a65b2016-05-10 17:29:47 -0700500 }
Charles Chan370a65b2016-05-10 17:29:47 -0700501 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700502
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700503 /**
504 * Populate or revoke a routing rule and egress rules on given deviceId that matches given IP,
505 * to set destination mac to given mac, set inner vlan and outer vlan to given vlans,
506 * set outer TPID, and output to given port.
507 *
508 * @param deviceId device ID
509 * @param port port
510 * @param mac mac address
511 * @param innerVlan inner VLAN ID
512 * @param outerVlan outer VLAN ID
513 * @param outerTpid outer TPID
514 * @param ip IP address
515 * @param revoke true to revoke the rule; false to populate
516 */
517 private void processDoubleTaggedRoutingRule(DeviceId deviceId, PortNumber port,
518 MacAddress mac, VlanId innerVlan,
519 VlanId outerVlan, EthType outerTpid,
520 IpAddress ip, boolean revoke) {
521 ConnectPoint location = new ConnectPoint(deviceId, port);
522 if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) {
523 log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port);
524 return;
525 }
526 log.info("{} routing rule for double-tagged host {} at {}",
527 revoke ? "Revoking" : "Populating", ip, location);
528 if (revoke) {
529 srManager.defaultRoutingHandler.revokeDoubleTaggedRoute(
530 deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port);
531 } else {
532 srManager.defaultRoutingHandler.populateDoubleTaggedRoute(
533 deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port);
534 }
535 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700536
537 /**
Charles Chan868c9572018-06-15 18:54:18 -0700538 * Returns VLAN ID to be used to program redirection flow on pair port.
539 *
540 * @param hostVlanId host VLAN ID
541 * @param location host location
542 * @return VLAN ID to be used; Or null if host VLAN does not match the interface config
543 */
544 VlanId vlanForPairPort(VlanId hostVlanId, ConnectPoint location) {
545 VlanId internalVlan = srManager.getInternalVlanId(location);
546 Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(location);
547
548 if (!hostVlanId.equals(VlanId.NONE) && taggedVlan.contains(hostVlanId)) {
549 return hostVlanId;
550 } else if (hostVlanId.equals(VlanId.NONE) && internalVlan != null) {
551 return internalVlan;
552 } else {
553 log.warn("VLAN mismatch. hostVlan={}, location={}, internalVlan={}, taggedVlan={}",
554 hostVlanId, location, internalVlan, taggedVlan);
555 return null;
556 }
557 }
558
559 /**
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700560 * Update forwarding objective for unicast bridging and unicast routing.
561 * Also check the validity of updated interface configuration on VLAN.
562 *
563 * @param deviceId device ID that host attaches to
564 * @param portNum port number that host attaches to
565 * @param vlanId Vlan ID configured on the switch port
566 * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise
567 * @param install true to populate the objective, false to revoke
568 */
569 void processIntfVlanUpdatedEvent(DeviceId deviceId, PortNumber portNum, VlanId vlanId,
570 boolean popVlan, boolean install) {
571 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNum);
572 Set<Host> hosts = hostService.getConnectedHosts(connectPoint);
573
574 if (hosts == null || hosts.size() == 0) {
Charles Chan39b75522018-04-21 00:44:29 -0700575 log.debug("processIntfVlanUpdatedEvent: No hosts connected to {}", connectPoint);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700576 return;
577 }
578
579 hosts.forEach(host -> {
580 MacAddress mac = host.mac();
581 VlanId hostVlanId = host.vlan();
582
583 // Check whether the host vlan is valid for new interface configuration
584 if ((!popVlan && hostVlanId.equals(vlanId)) ||
585 (popVlan && hostVlanId.equals(VlanId.NONE))) {
Charles Chand66d6712018-03-29 16:03:41 -0700586 srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700587 // Update Forwarding objective and corresponding simple Next objective
588 // for each host and IP address connected to given port
589 host.ipAddresses().forEach(ipAddress ->
Charles Chand66d6712018-03-29 16:03:41 -0700590 srManager.defaultRoutingHandler.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(),
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700591 mac, vlanId, popVlan, install)
592 );
593 }
594 });
595 }
596
597 /**
598 * Populate or revoke routing rule for each host, according to the updated
599 * subnet configuration on the interface.
600 * @param cp connect point of the updated interface
601 * @param ipPrefixSet IP Prefixes added or removed
602 * @param install true if IP Prefixes added, false otherwise
603 */
604 void processIntfIpUpdatedEvent(ConnectPoint cp, Set<IpPrefix> ipPrefixSet, boolean install) {
605 Set<Host> hosts = hostService.getConnectedHosts(cp);
606
607 if (hosts == null || hosts.size() == 0) {
Charles Chan39b75522018-04-21 00:44:29 -0700608 log.debug("processIntfIpUpdatedEvent: No hosts connected to {}", cp);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700609 return;
610 }
611
612 // Check whether the host IP address is in the interface's subnet
613 hosts.forEach(host ->
614 host.ipAddresses().forEach(hostIpAddress -> {
615 ipPrefixSet.forEach(ipPrefix -> {
616 if (install && ipPrefix.contains(hostIpAddress)) {
Charles Chand66d6712018-03-29 16:03:41 -0700617 srManager.defaultRoutingHandler.populateRoute(cp.deviceId(), hostIpAddress.toIpPrefix(),
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700618 host.mac(), host.vlan(), cp.port());
619 } else if (!install && ipPrefix.contains(hostIpAddress)) {
Charles Chand66d6712018-03-29 16:03:41 -0700620 srManager.defaultRoutingHandler.revokeRoute(cp.deviceId(), hostIpAddress.toIpPrefix(),
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700621 host.mac(), host.vlan(), cp.port());
622 }
623 });
624 }));
625 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800626
627 /**
628 * Returns the set of portnumbers on the given device that are part of the
629 * locations for dual-homed hosts.
630 *
631 * @param deviceId the given deviceId
632 * @return set of port numbers on given device that are dual-homed host
633 * locations. May be empty if no dual homed hosts are connected to
634 * the given device
635 */
636 Set<PortNumber> getDualHomedHostPorts(DeviceId deviceId) {
637 Set<PortNumber> dualHomedLocations = new HashSet<>();
638 srManager.hostService.getConnectedHosts(deviceId).stream()
639 .filter(host -> host.locations().size() == 2)
640 .forEach(host -> host.locations().stream()
641 .filter(loc -> loc.deviceId().equals(deviceId))
642 .forEach(loc -> dualHomedLocations.add(loc.port())));
643 return dualHomedLocations;
644 }
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700645
646 /**
647 * Checks if the given host is double-tagged or not.
648 *
649 * @param host host to check
650 * @return true if it is double-tagged, false otherwise
651 */
652 private boolean isDoubleTaggedHost(Host host) {
653 return !host.innerVlan().equals(VlanId.NONE);
654 }
655
Charles Chan1eaf4802016-04-18 13:44:03 -0700656}