blob: e4d85de1bca78f04b81e19b25f23eb41bbd10bd7 [file] [log] [blame]
Charles Chand2990362016-04-18 13:44:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chand2990362016-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 Hyun800d9d02018-04-09 09:40:50 -070019import org.onlab.packet.EthType;
Charles Chand2990362016-04-18 13:44:03 -070020import org.onlab.packet.IpAddress;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070021import org.onlab.packet.IpPrefix;
Charles Chand2990362016-04-18 13:44:03 -070022import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Charles Chan59cc16d2017-02-02 16:20:42 -080024import org.onosproject.net.ConnectPoint;
Charles Chand2990362016-04-18 13:44:03 -070025import org.onosproject.net.DeviceId;
Charles Chan6ea94fc2016-05-10 17:29:47 -070026import org.onosproject.net.Host;
Charles Chan93e71ba2016-04-29 14:38:22 -070027import org.onosproject.net.HostLocation;
Charles Chand2990362016-04-18 13:44:03 -070028import org.onosproject.net.PortNumber;
Charles Chand2990362016-04-18 13:44:03 -070029import org.onosproject.net.host.HostEvent;
30import org.onosproject.net.host.HostService;
Charles Chanff79dd92018-06-01 16:33:48 -070031import org.onosproject.net.host.ProbeMode;
Charles Chand2990362016-04-18 13:44:03 -070032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
Saurav Das018605f2017-02-18 14:05:44 -080035import com.google.common.collect.Sets;
Charles Chan65238242017-06-22 18:03:14 -070036
Saurav Das45f48152018-01-18 12:07:33 -080037import java.util.HashSet;
Charles Chan65238242017-06-22 18:03:14 -070038import java.util.Optional;
Charles Chand2990362016-04-18 13:44:03 -070039import java.util.Set;
Charles Chanf9a52702017-06-16 15:19:24 -070040import java.util.stream.Collectors;
41
42import static com.google.common.base.Preconditions.checkArgument;
Charles Chand2990362016-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 Chan47933752017-11-30 15:37:50 -080049
Charles Chan2e2e3402017-06-19 14:00:53 -070050 protected final SegmentRoutingManager srManager;
Charles Chand2990362016-04-18 13:44:03 -070051 private HostService hostService;
Charles Chand2990362016-04-18 13:44:03 -070052
53 /**
54 * Constructs the HostHandler.
55 *
56 * @param srManager Segment Routing manager
57 */
Charles Chanf9a52702017-06-16 15:19:24 -070058 HostHandler(SegmentRoutingManager srManager) {
Charles Chand2990362016-04-18 13:44:03 -070059 this.srManager = srManager;
Charles Chand2990362016-04-18 13:44:03 -070060 hostService = srManager.hostService;
Charles Chand2990362016-04-18 13:44:03 -070061 }
62
Charles Chan03a73e02016-10-24 14:52:01 -070063 protected void init(DeviceId devId) {
Charles Chanf9a52702017-06-16 15:19:24 -070064 hostService.getHosts().forEach(host ->
65 host.locations().stream()
Charles Chan47933752017-11-30 15:37:50 -080066 .filter(location -> location.deviceId().equals(devId) ||
67 location.deviceId().equals(srManager.getPairDeviceId(devId).orElse(null)))
Charles Chanf9a52702017-06-16 15:19:24 -070068 .forEach(location -> processHostAddedAtLocation(host, location))
69 );
Charles Chan93e71ba2016-04-29 14:38:22 -070070 }
Charles Chand2990362016-04-18 13:44:03 -070071
Charles Chanf9a52702017-06-16 15:19:24 -070072 void processHostAddedEvent(HostEvent event) {
Charles Chan35fd1a72016-06-13 18:54:31 -070073 processHostAdded(event.subject());
Charles Chan93e71ba2016-04-29 14:38:22 -070074 }
75
Charles Chanf9a52702017-06-16 15:19:24 -070076 private void processHostAdded(Host host) {
77 host.locations().forEach(location -> processHostAddedAtLocation(host, location));
Saurav Das45f48152018-01-18 12:07:33 -080078 // ensure dual-homed host locations have viable uplinks
Saurav Das9a554292018-04-27 18:42:30 -070079 if (host.locations().size() > 1 || srManager.singleHomedDown) {
Saurav Das45f48152018-01-18 12:07:33 -080080 host.locations().forEach(loc -> {
81 if (srManager.mastershipService.isLocalMaster(loc.deviceId())) {
Saurav Das9a554292018-04-27 18:42:30 -070082 srManager.linkHandler.checkUplinksForHost(loc);
Saurav Das45f48152018-01-18 12:07:33 -080083 }
84 });
85 }
Charles Chand2990362016-04-18 13:44:03 -070086 }
87
Charles Chanf9a52702017-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 Chan8e786b52017-09-12 18:57:47 -070091 MacAddress hostMac = host.mac();
92 VlanId hostVlanId = host.vlan();
Charles Chanf9a52702017-06-16 15:19:24 -070093 Set<HostLocation> locations = host.locations();
94 Set<IpAddress> ips = host.ipAddresses();
Charles Chan8e786b52017-09-12 18:57:47 -070095 log.info("Host {}/{} is added at {}", hostMac, hostVlanId, locations);
Charles Chanf9a52702017-06-16 15:19:24 -070096
Jonghwan Hyun800d9d02018-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 Chan2ff1bac2018-03-29 16:03:41 -0700105 processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, false)
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700106 );
107 }
Charles Chan8e786b52017-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 Chan2ff1bac2018-03-29 16:03:41 -0700113 if (host.locations().stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) {
Saurav Das9a554292018-04-27 18:42:30 -0700114 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> {
Charles Chan8e786b52017-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 Chanf76de302018-06-15 18:54:18 -0700117 VlanId vlanId = vlanForPairPort(hostVlanId, location);
118 if (vlanId == null) {
119 return;
120 }
Charles Chan8e786b52017-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 Chan47933752017-11-30 15:37:50 -0800125
126 if (srManager.activeProbing) {
127 probe(host, location, pairDeviceId, pairRemotePort);
128 }
Charles Chan8e786b52017-09-12 18:57:47 -0700129 });
130 }
131 });
Charles Chanf9a52702017-06-16 15:19:24 -0700132 }
133
134 void processHostRemovedEvent(HostEvent event) {
Charles Chan35fd1a72016-06-13 18:54:31 -0700135 processHostRemoved(event.subject());
136 }
137
Charles Chanf9a52702017-06-16 15:19:24 -0700138 private void processHostRemoved(Host host) {
Charles Chan65238242017-06-22 18:03:14 -0700139 MacAddress hostMac = host.mac();
140 VlanId hostVlanId = host.vlan();
Charles Chanf9a52702017-06-16 15:19:24 -0700141 Set<HostLocation> locations = host.locations();
Charles Chan35fd1a72016-06-13 18:54:31 -0700142 Set<IpAddress> ips = host.ipAddresses();
Charles Chan65238242017-06-22 18:03:14 -0700143 log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations);
Charles Chan93e71ba2016-04-29 14:38:22 -0700144
Charles Chan2fde6d42017-08-23 14:46:43 -0700145 locations.forEach(location -> {
Jonghwan Hyun800d9d02018-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 Chan2ff1bac2018-03-29 16:03:41 -0700154 processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true)
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700155 );
156 }
Charles Chan65238242017-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 Das9a554292018-04-27 18:42:30 -0700160 Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId());
Charles Chan2ff1bac2018-03-29 16:03:41 -0700161 if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) {
Charles Chan65238242017-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 Chanf76de302018-06-15 18:54:18 -0700164 VlanId vlanId = vlanForPairPort(hostVlanId, location);
165 if (vlanId == null) {
166 return;
167 }
Charles Chan65238242017-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 Chan4d639392018-06-21 19:07:12 -0700174
175 // Delete prefix from sr-device-subnet when the next hop host is removed
176 srManager.routeService.getRouteTables().forEach(tableId -> {
177 srManager.routeService.getRoutes(tableId).forEach(routeInfo -> {
178 if (routeInfo.allRoutes().stream().anyMatch(rr -> ips.contains(rr.nextHop()))) {
179 log.debug("HostRemoved. removeSubnet {}, {}", location, routeInfo.prefix());
180 srManager.deviceConfiguration.removeSubnet(location, routeInfo.prefix());
181 }
182 });
183 });
Charles Chanf9a52702017-06-16 15:19:24 -0700184 });
Charles Chan93e71ba2016-04-29 14:38:22 -0700185 }
186
Charles Chanf9a52702017-06-16 15:19:24 -0700187 void processHostMovedEvent(HostEvent event) {
Charles Chan9bd0e5a2018-04-25 18:51:46 -0400188 Host host = event.subject();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700189 MacAddress hostMac = host.mac();
190 VlanId hostVlanId = host.vlan();
Charles Chan9bd0e5a2018-04-25 18:51:46 -0400191 Set<HostLocation> prevLocations = event.prevSubject().locations();
192 Set<IpAddress> prevIps = event.prevSubject().ipAddresses();
193 Set<HostLocation> newLocations = host.locations();
194 Set<IpAddress> newIps = host.ipAddresses();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700195 EthType hostTpid = host.tpid();
196 boolean doubleTaggedHost = isDoubleTaggedHost(host);
Charles Chan9bd0e5a2018-04-25 18:51:46 -0400197
Charles Chan47933752017-11-30 15:37:50 -0800198 log.info("Host {}/{} is moved from {} to {}", hostMac, hostVlanId, prevLocations, newLocations);
Charles Chanf9a52702017-06-16 15:19:24 -0700199 Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId)
200 .collect(Collectors.toSet());
Charles Chan93e71ba2016-04-29 14:38:22 -0700201
Charles Chanf9a52702017-06-16 15:19:24 -0700202 // For each old location
Charles Chan2ff1bac2018-03-29 16:03:41 -0700203 Sets.difference(prevLocations, newLocations).forEach(prevLocation -> {
Charles Chan65238242017-06-22 18:03:14 -0700204 // Remove routing rules for old IPs
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700205 Sets.difference(prevIps, newIps).forEach(ip -> {
206 if (doubleTaggedHost) {
207 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
208 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
209 } else {
Charles Chan65238242017-06-22 18:03:14 -0700210 processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId,
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700211 ip, true);
212 }
213 });
Charles Chan65238242017-06-22 18:03:14 -0700214
215 // Redirect the flows to pair link if configured
216 // Note: Do not continue removing any rule
217 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId());
Saurav Das9a554292018-04-27 18:42:30 -0700218 Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(prevLocation.deviceId());
Charles Chan65238242017-06-22 18:03:14 -0700219 if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && newLocations.stream()
220 .anyMatch(location -> location.deviceId().equals(pairDeviceId.get()))) {
221 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
222 // when the host is untagged
223 VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(prevLocation)).orElse(hostVlanId);
224
225 processBridgingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, false);
226 newIps.forEach(ip ->
227 processRoutingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId,
228 ip, false));
229 return;
230 }
Charles Chanf9a52702017-06-16 15:19:24 -0700231
Charles Chan50bb6ef2018-04-18 18:41:05 -0700232 // Remove flows for unchanged IPs only when the host moves from a switch to another.
Charles Chanf9a52702017-06-16 15:19:24 -0700233 // Otherwise, do not remove and let the adding part update the old flow
234 if (!newDeviceIds.contains(prevLocation.deviceId())) {
Charles Chan65238242017-06-22 18:03:14 -0700235 processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700236 Sets.intersection(prevIps, newIps).forEach(ip -> {
237 if (doubleTaggedHost) {
238 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
239 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
240 } else {
241 processRoutingRule(prevLocation.deviceId(), prevLocation.port(),
242 hostMac, hostVlanId, ip, true);
243 }
244 });
Charles Chanf9a52702017-06-16 15:19:24 -0700245 }
246
247 // Remove bridging rules if new interface vlan is different from old interface vlan
248 // Otherwise, do not remove and let the adding part update the old flow
249 if (newLocations.stream().noneMatch(newLocation -> {
250 VlanId oldAssignedVlan = srManager.getInternalVlanId(prevLocation);
251 VlanId newAssignedVlan = srManager.getInternalVlanId(newLocation);
252 // Host is tagged and the new location has the host vlan in vlan-tagged
Charles Chan971d7ba2018-05-01 11:50:20 -0700253 return srManager.interfaceService.getTaggedVlanId(newLocation).contains(hostVlanId) ||
Charles Chanf9a52702017-06-16 15:19:24 -0700254 (oldAssignedVlan != null && newAssignedVlan != null &&
255 // Host is untagged and the new location has the same assigned vlan
256 oldAssignedVlan.equals(newAssignedVlan));
257 })) {
Charles Chan65238242017-06-22 18:03:14 -0700258 processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true);
Charles Chanf9a52702017-06-16 15:19:24 -0700259 }
260
261 // Remove routing rules for unchanged IPs if none of the subnet of new location contains
262 // the IP. Otherwise, do not remove and let the adding part update the old flow
263 Sets.intersection(prevIps, newIps).forEach(ip -> {
264 if (newLocations.stream().noneMatch(newLocation ->
265 srManager.deviceConfiguration.inSameSubnet(newLocation, ip))) {
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700266 if (doubleTaggedHost) {
267 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
268 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
269 } else {
270 processRoutingRule(prevLocation.deviceId(), prevLocation.port(),
271 hostMac, hostVlanId, ip, true);
272 }
Charles Chanf9a52702017-06-16 15:19:24 -0700273 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700274 });
Charles Chanf9a52702017-06-16 15:19:24 -0700275 });
276
277 // For each new location, add all new IPs.
Charles Chan50bb6ef2018-04-18 18:41:05 -0700278 Sets.difference(newLocations, prevLocations).forEach(newLocation -> {
Charles Chan65238242017-06-22 18:03:14 -0700279 processBridgingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, false);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700280 newIps.forEach(ip -> {
281 if (doubleTaggedHost) {
282 processDoubleTaggedRoutingRule(newLocation.deviceId(), newLocation.port(),
283 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false);
284 } else {
Charles Chan65238242017-06-22 18:03:14 -0700285 processRoutingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId,
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700286 ip, false);
287 }
288 });
Charles Chan9bd0e5a2018-04-25 18:51:46 -0400289
290 // Probe on pair device when host move
291 // Majorly for the 2nd step of [1A/x, 1B/x] -> [1A/x, 1B/y] -> [1A/y, 1B/y]
292 // But will also cover [1A/x] -> [1A/y] -> [1A/y, 1B/y]
293 if (srManager.activeProbing) {
Charles Chanf82d1552018-07-23 15:27:34 -0700294
Charles Chan9bd0e5a2018-04-25 18:51:46 -0400295 srManager.getPairDeviceId(newLocation.deviceId()).ifPresent(pairDeviceId ->
296 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort ->
297 probe(host, newLocation, pairDeviceId, pairRemotePort)
298 )
299 );
300 }
Charles Chanf9a52702017-06-16 15:19:24 -0700301 });
302
303 // For each unchanged location, add new IPs and remove old IPs.
Charles Chan50bb6ef2018-04-18 18:41:05 -0700304 Sets.intersection(newLocations, prevLocations).forEach(unchangedLocation -> {
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700305 Sets.difference(prevIps, newIps).forEach(ip -> {
306 if (doubleTaggedHost) {
307 processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
308 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
309 } else {
310 processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
311 hostMac, hostVlanId, ip, true);
312 }
313 });
Charles Chanf9a52702017-06-16 15:19:24 -0700314
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700315 Sets.difference(newIps, prevIps).forEach(ip -> {
316 if (doubleTaggedHost) {
317 processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
318 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false);
319 } else {
320 processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
321 hostMac, hostVlanId, ip, false);
322 }
323 });
Charles Chanf82d1552018-07-23 15:27:34 -0700324
325 // Verify existing location and see if it is still valid
326 srManager.probingService.probeHost(host, unchangedLocation, ProbeMode.VERIFY);
Charles Chanf9a52702017-06-16 15:19:24 -0700327 });
Saurav Das45f48152018-01-18 12:07:33 -0800328
329 // ensure dual-homed host locations have viable uplinks
Saurav Das9a554292018-04-27 18:42:30 -0700330 if (newLocations.size() > prevLocations.size() || srManager.singleHomedDown) {
Saurav Das45f48152018-01-18 12:07:33 -0800331 newLocations.forEach(loc -> {
332 if (srManager.mastershipService.isLocalMaster(loc.deviceId())) {
Saurav Das9a554292018-04-27 18:42:30 -0700333 srManager.linkHandler.checkUplinksForHost(loc);
Saurav Das45f48152018-01-18 12:07:33 -0800334 }
335 });
336 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700337 }
338
Charles Chanf9a52702017-06-16 15:19:24 -0700339 void processHostUpdatedEvent(HostEvent event) {
Charles Chan47933752017-11-30 15:37:50 -0800340 Host host = event.subject();
341 MacAddress hostMac = host.mac();
342 VlanId hostVlanId = host.vlan();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700343 EthType hostTpid = host.tpid();
Charles Chan47933752017-11-30 15:37:50 -0800344 Set<HostLocation> locations = host.locations();
Charles Chan93e71ba2016-04-29 14:38:22 -0700345 Set<IpAddress> prevIps = event.prevSubject().ipAddresses();
Charles Chan47933752017-11-30 15:37:50 -0800346 Set<IpAddress> newIps = host.ipAddresses();
Charles Chan3d650a62017-11-20 08:46:24 -0800347 log.info("Host {}/{} is updated", hostMac, hostVlanId);
Charles Chan93e71ba2016-04-29 14:38:22 -0700348
Charles Chan2ff1bac2018-03-29 16:03:41 -0700349 locations.forEach(location -> {
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700350 Sets.difference(prevIps, newIps).forEach(ip -> {
351 if (isDoubleTaggedHost(host)) {
352 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
353 host.innerVlan(), hostVlanId, hostTpid, ip, true);
354 } else {
355 processRoutingRule(location.deviceId(), location.port(), hostMac,
356 hostVlanId, ip, true);
357 }
358 });
359 Sets.difference(newIps, prevIps).forEach(ip -> {
360 if (isDoubleTaggedHost(host)) {
361 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
362 host.innerVlan(), hostVlanId, hostTpid, ip, false);
363 } else {
364 processRoutingRule(location.deviceId(), location.port(), hostMac,
365 hostVlanId, ip, false);
366 }
367 });
Charles Chanf9a52702017-06-16 15:19:24 -0700368 });
Charles Chan3d650a62017-11-20 08:46:24 -0800369
370 // Use the pair link temporarily before the second location of a dual-homed host shows up.
371 // This do not affect single-homed hosts since the flow will be blocked in
372 // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively
Charles Chan47933752017-11-30 15:37:50 -0800373 locations.forEach(location ->
Charles Chan3d650a62017-11-20 08:46:24 -0800374 srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> {
Charles Chan2ff1bac2018-03-29 16:03:41 -0700375 if (locations.stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) {
Charles Chan47933752017-11-30 15:37:50 -0800376 Set<IpAddress> ipsToAdd = Sets.difference(newIps, prevIps);
377 Set<IpAddress> ipsToRemove = Sets.difference(prevIps, newIps);
378
Saurav Das9a554292018-04-27 18:42:30 -0700379 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> {
Charles Chan3d650a62017-11-20 08:46:24 -0800380 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
381 // when the host is untagged
Charles Chanf76de302018-06-15 18:54:18 -0700382 VlanId vlanId = vlanForPairPort(hostVlanId, location);
383 if (vlanId == null) {
384 return;
385 }
Charles Chan3d650a62017-11-20 08:46:24 -0800386
Charles Chan47933752017-11-30 15:37:50 -0800387 ipsToRemove.forEach(ip ->
Charles Chan3d650a62017-11-20 08:46:24 -0800388 processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, true)
389 );
Charles Chan47933752017-11-30 15:37:50 -0800390 ipsToAdd.forEach(ip ->
Charles Chan3d650a62017-11-20 08:46:24 -0800391 processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, false)
392 );
Charles Chan47933752017-11-30 15:37:50 -0800393
394 if (srManager.activeProbing) {
395 probe(host, location, pairDeviceId, pairRemotePort);
396 }
Charles Chan3d650a62017-11-20 08:46:24 -0800397 });
398 }
Charles Chan47933752017-11-30 15:37:50 -0800399 })
400 );
401 }
402
403 /**
404 * When a non-pair port comes up, probe each host on the pair device if
405 * (1) the host is tagged and the tagged vlan of current port contains host vlan; or
406 * (2) the host is untagged and the internal vlan is the same on the host port and current port.
407 *
408 * @param cp connect point
409 */
410 void processPortUp(ConnectPoint cp) {
Saurav Das9a554292018-04-27 18:42:30 -0700411 if (cp.port().equals(srManager.getPairLocalPort(cp.deviceId()).orElse(null))) {
Charles Chan47933752017-11-30 15:37:50 -0800412 return;
413 }
414 if (srManager.activeProbing) {
415 srManager.getPairDeviceId(cp.deviceId())
416 .ifPresent(pairDeviceId -> srManager.hostService.getConnectedHosts(pairDeviceId).stream()
417 .filter(host -> isHostInVlanOfPort(host, pairDeviceId, cp))
Charles Chanff79dd92018-06-01 16:33:48 -0700418 .forEach(host -> srManager.probingService.probeHost(host, cp, ProbeMode.DISCOVER))
Charles Chan47933752017-11-30 15:37:50 -0800419 );
420 }
421 }
422
423 /**
424 * Checks if given host located on given device id matches VLAN config of current port.
425 *
426 * @param host host to check
427 * @param deviceId device id to check
428 * @param cp current connect point
429 * @return true if the host located at deviceId matches the VLAN config on cp
430 */
431 private boolean isHostInVlanOfPort(Host host, DeviceId deviceId, ConnectPoint cp) {
432 VlanId internalVlan = srManager.getInternalVlanId(cp);
Charles Chan971d7ba2018-05-01 11:50:20 -0700433 Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(cp);
Charles Chan47933752017-11-30 15:37:50 -0800434
435 return taggedVlan.contains(host.vlan()) ||
436 (internalVlan != null && host.locations().stream()
437 .filter(l -> l.deviceId().equals(deviceId))
438 .map(srManager::getInternalVlanId)
439 .anyMatch(internalVlan::equals));
440 }
441
442 /**
443 * Send a probe on all locations with the same VLAN on pair device, excluding pair port.
444 *
445 * @param host host to probe
446 * @param location newly discovered host location
447 * @param pairDeviceId pair device id
448 * @param pairRemotePort pair remote port
449 */
450 private void probe(Host host, ConnectPoint location, DeviceId pairDeviceId, PortNumber pairRemotePort) {
451 VlanId vlanToProbe = host.vlan().equals(VlanId.NONE) ?
452 srManager.getInternalVlanId(location) : host.vlan();
453 srManager.interfaceService.getInterfaces().stream()
454 .filter(i -> i.vlanTagged().contains(vlanToProbe) ||
455 i.vlanUntagged().equals(vlanToProbe) ||
456 i.vlanNative().equals(vlanToProbe))
457 .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId))
458 .filter(i -> !i.connectPoint().port().equals(pairRemotePort))
459 .forEach(i -> {
460 log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint());
Charles Chanff79dd92018-06-01 16:33:48 -0700461 srManager.probingService.probeHost(host, i.connectPoint(), ProbeMode.DISCOVER);
Charles Chan47933752017-11-30 15:37:50 -0800462 });
Charles Chan93e71ba2016-04-29 14:38:22 -0700463 }
464
465 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700466 * Populates or revokes a bridging rule on given deviceId that matches given mac,
467 * given vlan and output to given port.
Charles Chanb7b4c932017-06-15 19:25:25 -0700468 *
469 * @param deviceId device ID
470 * @param port port
471 * @param mac mac address
472 * @param vlanId VLAN ID
473 * @param revoke true to revoke the rule; false to populate
474 */
475 private void processBridgingRule(DeviceId deviceId, PortNumber port, MacAddress mac,
476 VlanId vlanId, boolean revoke) {
Charles Chan2ff1bac2018-03-29 16:03:41 -0700477 log.info("{} bridging entry for host {}/{} at {}:{}", revoke ? "Revoking" : "Populating",
Charles Chanb7b4c932017-06-15 19:25:25 -0700478 mac, vlanId, deviceId, port);
479
Charles Chan2ff1bac2018-03-29 16:03:41 -0700480 if (!revoke) {
481 srManager.defaultRoutingHandler.populateBridging(deviceId, port, mac, vlanId);
482 } else {
483 srManager.defaultRoutingHandler.revokeBridging(deviceId, port, mac, vlanId);
Charles Chanb7b4c932017-06-15 19:25:25 -0700484 }
Charles Chanb7b4c932017-06-15 19:25:25 -0700485 }
486
487 /**
488 * Populate or revoke a routing rule on given deviceId that matches given ip,
489 * set destination mac to given mac, set vlan to given vlan and output to given port.
490 *
491 * @param deviceId device ID
492 * @param port port
493 * @param mac mac address
494 * @param vlanId VLAN ID
495 * @param ip IP address
496 * @param revoke true to revoke the rule; false to populate
497 */
498 private void processRoutingRule(DeviceId deviceId, PortNumber port, MacAddress mac,
499 VlanId vlanId, IpAddress ip, boolean revoke) {
500 ConnectPoint location = new ConnectPoint(deviceId, port);
Charles Chanf9a52702017-06-16 15:19:24 -0700501 if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) {
502 log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port);
503 return;
Charles Chanb7b4c932017-06-15 19:25:25 -0700504 }
Charles Chanb7b4c932017-06-15 19:25:25 -0700505
Charles Chanf9a52702017-06-16 15:19:24 -0700506 log.info("{} routing rule for {} at {}", revoke ? "Revoking" : "Populating", ip, location);
507 if (revoke) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700508 srManager.defaultRoutingHandler.revokeRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port);
Charles Chanf9a52702017-06-16 15:19:24 -0700509 } else {
Charles Chan2fde6d42017-08-23 14:46:43 -0700510 srManager.defaultRoutingHandler.populateRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port);
Charles Chan6ea94fc2016-05-10 17:29:47 -0700511 }
Charles Chan6ea94fc2016-05-10 17:29:47 -0700512 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700513
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700514 /**
515 * Populate or revoke a routing rule and egress rules on given deviceId that matches given IP,
516 * to set destination mac to given mac, set inner vlan and outer vlan to given vlans,
517 * set outer TPID, and output to given port.
518 *
519 * @param deviceId device ID
520 * @param port port
521 * @param mac mac address
522 * @param innerVlan inner VLAN ID
523 * @param outerVlan outer VLAN ID
524 * @param outerTpid outer TPID
525 * @param ip IP address
526 * @param revoke true to revoke the rule; false to populate
527 */
528 private void processDoubleTaggedRoutingRule(DeviceId deviceId, PortNumber port,
529 MacAddress mac, VlanId innerVlan,
530 VlanId outerVlan, EthType outerTpid,
531 IpAddress ip, boolean revoke) {
Charles Chan57465d32018-07-19 14:55:31 -0700532 if (!srManager.handleDoubleTaggedHosts) {
533 log.debug("Ignore double tagged host {}/{}/{}", mac, outerVlan, innerVlan);
534 return;
535 }
536
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700537 ConnectPoint location = new ConnectPoint(deviceId, port);
538 if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) {
539 log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port);
540 return;
541 }
542 log.info("{} routing rule for double-tagged host {} at {}",
543 revoke ? "Revoking" : "Populating", ip, location);
544 if (revoke) {
545 srManager.defaultRoutingHandler.revokeDoubleTaggedRoute(
546 deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port);
547 } else {
548 srManager.defaultRoutingHandler.populateDoubleTaggedRoute(
549 deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port);
550 }
551 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700552
Charles Chan57465d32018-07-19 14:55:31 -0700553 void populateAllDoubleTaggedHost() {
554 log.info("Populating all double tagged hosts");
555 Sets.newHashSet(srManager.hostService.getHosts()).stream().filter(this::isDoubleTaggedHost)
556 .forEach(h -> h.locations().forEach(l ->
557 h.ipAddresses().forEach(i ->
558 processDoubleTaggedRoutingRule(l.deviceId(), l.port(), h.mac(), h.innerVlan(),
559 h.vlan(), h.tpid(), i, false)
560 )
561 )
562 );
563 }
564
565 void revokeAllDoubleTaggedHost() {
566 log.info("Revoking all double tagged hosts");
567 Sets.newHashSet(srManager.hostService.getHosts()).stream().filter(this::isDoubleTaggedHost)
568 .forEach(h -> h.locations().forEach(l ->
569 h.ipAddresses().forEach(i ->
570 processDoubleTaggedRoutingRule(l.deviceId(), l.port(), h.mac(), h.innerVlan(),
571 h.vlan(), h.tpid(), i, true)
572 )
573 )
574 );
575 }
576
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700577 /**
Charles Chanf76de302018-06-15 18:54:18 -0700578 * Returns VLAN ID to be used to program redirection flow on pair port.
579 *
580 * @param hostVlanId host VLAN ID
581 * @param location host location
582 * @return VLAN ID to be used; Or null if host VLAN does not match the interface config
583 */
584 VlanId vlanForPairPort(VlanId hostVlanId, ConnectPoint location) {
585 VlanId internalVlan = srManager.getInternalVlanId(location);
586 Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(location);
587
588 if (!hostVlanId.equals(VlanId.NONE) && taggedVlan.contains(hostVlanId)) {
589 return hostVlanId;
590 } else if (hostVlanId.equals(VlanId.NONE) && internalVlan != null) {
591 return internalVlan;
592 } else {
593 log.warn("VLAN mismatch. hostVlan={}, location={}, internalVlan={}, taggedVlan={}",
594 hostVlanId, location, internalVlan, taggedVlan);
595 return null;
596 }
597 }
598
599 /**
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700600 * Update forwarding objective for unicast bridging and unicast routing.
601 * Also check the validity of updated interface configuration on VLAN.
602 *
603 * @param deviceId device ID that host attaches to
604 * @param portNum port number that host attaches to
605 * @param vlanId Vlan ID configured on the switch port
606 * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise
607 * @param install true to populate the objective, false to revoke
608 */
609 void processIntfVlanUpdatedEvent(DeviceId deviceId, PortNumber portNum, VlanId vlanId,
610 boolean popVlan, boolean install) {
611 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNum);
612 Set<Host> hosts = hostService.getConnectedHosts(connectPoint);
613
614 if (hosts == null || hosts.size() == 0) {
Charles Chan10b2fee2018-04-21 00:44:29 -0700615 log.debug("processIntfVlanUpdatedEvent: No hosts connected to {}", connectPoint);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700616 return;
617 }
618
619 hosts.forEach(host -> {
620 MacAddress mac = host.mac();
621 VlanId hostVlanId = host.vlan();
622
623 // Check whether the host vlan is valid for new interface configuration
624 if ((!popVlan && hostVlanId.equals(vlanId)) ||
625 (popVlan && hostVlanId.equals(VlanId.NONE))) {
Charles Chan2ff1bac2018-03-29 16:03:41 -0700626 srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700627 // Update Forwarding objective and corresponding simple Next objective
628 // for each host and IP address connected to given port
629 host.ipAddresses().forEach(ipAddress ->
Charles Chan2ff1bac2018-03-29 16:03:41 -0700630 srManager.defaultRoutingHandler.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(),
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700631 mac, vlanId, popVlan, install)
632 );
633 }
634 });
635 }
636
637 /**
638 * Populate or revoke routing rule for each host, according to the updated
639 * subnet configuration on the interface.
640 * @param cp connect point of the updated interface
641 * @param ipPrefixSet IP Prefixes added or removed
642 * @param install true if IP Prefixes added, false otherwise
643 */
644 void processIntfIpUpdatedEvent(ConnectPoint cp, Set<IpPrefix> ipPrefixSet, boolean install) {
645 Set<Host> hosts = hostService.getConnectedHosts(cp);
646
647 if (hosts == null || hosts.size() == 0) {
Charles Chan10b2fee2018-04-21 00:44:29 -0700648 log.debug("processIntfIpUpdatedEvent: No hosts connected to {}", cp);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700649 return;
650 }
651
652 // Check whether the host IP address is in the interface's subnet
653 hosts.forEach(host ->
654 host.ipAddresses().forEach(hostIpAddress -> {
655 ipPrefixSet.forEach(ipPrefix -> {
656 if (install && ipPrefix.contains(hostIpAddress)) {
Charles Chan2ff1bac2018-03-29 16:03:41 -0700657 srManager.defaultRoutingHandler.populateRoute(cp.deviceId(), hostIpAddress.toIpPrefix(),
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700658 host.mac(), host.vlan(), cp.port());
659 } else if (!install && ipPrefix.contains(hostIpAddress)) {
Charles Chan2ff1bac2018-03-29 16:03:41 -0700660 srManager.defaultRoutingHandler.revokeRoute(cp.deviceId(), hostIpAddress.toIpPrefix(),
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700661 host.mac(), host.vlan(), cp.port());
662 }
663 });
664 }));
665 }
Saurav Das45f48152018-01-18 12:07:33 -0800666
667 /**
668 * Returns the set of portnumbers on the given device that are part of the
669 * locations for dual-homed hosts.
670 *
671 * @param deviceId the given deviceId
672 * @return set of port numbers on given device that are dual-homed host
673 * locations. May be empty if no dual homed hosts are connected to
674 * the given device
675 */
676 Set<PortNumber> getDualHomedHostPorts(DeviceId deviceId) {
677 Set<PortNumber> dualHomedLocations = new HashSet<>();
678 srManager.hostService.getConnectedHosts(deviceId).stream()
679 .filter(host -> host.locations().size() == 2)
680 .forEach(host -> host.locations().stream()
681 .filter(loc -> loc.deviceId().equals(deviceId))
682 .forEach(loc -> dualHomedLocations.add(loc.port())));
683 return dualHomedLocations;
684 }
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700685
686 /**
687 * Checks if the given host is double-tagged or not.
688 *
689 * @param host host to check
690 * @return true if it is double-tagged, false otherwise
691 */
692 private boolean isDoubleTaggedHost(Host host) {
693 return !host.innerVlan().equals(VlanId.NONE);
694 }
695
Charles Chand2990362016-04-18 13:44:03 -0700696}