blob: f71c1d0a309cc84d073ddd0f5874a0fcc5b5a635 [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;
Charles Chan47933752017-11-30 15:37:50 -080030import org.onosproject.net.host.HostLocationProbingService.ProbeMode;
Charles Chand2990362016-04-18 13:44:03 -070031import org.onosproject.net.host.HostService;
32import 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
117 VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(hostVlanId);
118
119 processBridgingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, false);
120 ips.forEach(ip -> processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId,
121 ip, false));
Charles Chan47933752017-11-30 15:37:50 -0800122
123 if (srManager.activeProbing) {
124 probe(host, location, pairDeviceId, pairRemotePort);
125 }
Charles Chan8e786b52017-09-12 18:57:47 -0700126 });
127 }
128 });
Charles Chanf9a52702017-06-16 15:19:24 -0700129 }
130
131 void processHostRemovedEvent(HostEvent event) {
Charles Chan35fd1a72016-06-13 18:54:31 -0700132 processHostRemoved(event.subject());
133 }
134
Charles Chanf9a52702017-06-16 15:19:24 -0700135 private void processHostRemoved(Host host) {
Charles Chan65238242017-06-22 18:03:14 -0700136 MacAddress hostMac = host.mac();
137 VlanId hostVlanId = host.vlan();
Charles Chanf9a52702017-06-16 15:19:24 -0700138 Set<HostLocation> locations = host.locations();
Charles Chan35fd1a72016-06-13 18:54:31 -0700139 Set<IpAddress> ips = host.ipAddresses();
Charles Chan65238242017-06-22 18:03:14 -0700140 log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations);
Charles Chan93e71ba2016-04-29 14:38:22 -0700141
Charles Chan2fde6d42017-08-23 14:46:43 -0700142 locations.forEach(location -> {
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700143 if (isDoubleTaggedHost(host)) {
144 ips.forEach(ip ->
145 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
146 host.innerVlan(), hostVlanId, host.tpid(), ip, true)
147 );
148 } else {
149 processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, true);
150 ips.forEach(ip ->
Charles Chan2ff1bac2018-03-29 16:03:41 -0700151 processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true)
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700152 );
153 }
Charles Chan65238242017-06-22 18:03:14 -0700154
155 // Also remove redirection flows on the pair device if exists.
156 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId());
Saurav Das9a554292018-04-27 18:42:30 -0700157 Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId());
Charles Chan2ff1bac2018-03-29 16:03:41 -0700158 if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) {
Charles Chan65238242017-06-22 18:03:14 -0700159 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
160 // when the host is untagged
161 VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(hostVlanId);
162
163 processBridgingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, true);
164 ips.forEach(ip ->
165 processRoutingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId,
166 ip, true));
167 }
Charles Chanf9a52702017-06-16 15:19:24 -0700168 });
Charles Chan93e71ba2016-04-29 14:38:22 -0700169 }
170
Charles Chanf9a52702017-06-16 15:19:24 -0700171 void processHostMovedEvent(HostEvent event) {
Charles Chan9bd0e5a2018-04-25 18:51:46 -0400172 Host host = event.subject();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700173 MacAddress hostMac = host.mac();
174 VlanId hostVlanId = host.vlan();
Charles Chan9bd0e5a2018-04-25 18:51:46 -0400175 Set<HostLocation> prevLocations = event.prevSubject().locations();
176 Set<IpAddress> prevIps = event.prevSubject().ipAddresses();
177 Set<HostLocation> newLocations = host.locations();
178 Set<IpAddress> newIps = host.ipAddresses();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700179 EthType hostTpid = host.tpid();
180 boolean doubleTaggedHost = isDoubleTaggedHost(host);
Charles Chan9bd0e5a2018-04-25 18:51:46 -0400181
Charles Chan47933752017-11-30 15:37:50 -0800182 log.info("Host {}/{} is moved from {} to {}", hostMac, hostVlanId, prevLocations, newLocations);
Charles Chanf9a52702017-06-16 15:19:24 -0700183 Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId)
184 .collect(Collectors.toSet());
Charles Chan93e71ba2016-04-29 14:38:22 -0700185
Charles Chanf9a52702017-06-16 15:19:24 -0700186 // For each old location
Charles Chan2ff1bac2018-03-29 16:03:41 -0700187 Sets.difference(prevLocations, newLocations).forEach(prevLocation -> {
Charles Chan65238242017-06-22 18:03:14 -0700188 // Remove routing rules for old IPs
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700189 Sets.difference(prevIps, newIps).forEach(ip -> {
190 if (doubleTaggedHost) {
191 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
192 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
193 } else {
Charles Chan65238242017-06-22 18:03:14 -0700194 processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId,
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700195 ip, true);
196 }
197 });
Charles Chan65238242017-06-22 18:03:14 -0700198
199 // Redirect the flows to pair link if configured
200 // Note: Do not continue removing any rule
201 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId());
Saurav Das9a554292018-04-27 18:42:30 -0700202 Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(prevLocation.deviceId());
Charles Chan65238242017-06-22 18:03:14 -0700203 if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && newLocations.stream()
204 .anyMatch(location -> location.deviceId().equals(pairDeviceId.get()))) {
205 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
206 // when the host is untagged
207 VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(prevLocation)).orElse(hostVlanId);
208
209 processBridgingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, false);
210 newIps.forEach(ip ->
211 processRoutingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId,
212 ip, false));
213 return;
214 }
Charles Chanf9a52702017-06-16 15:19:24 -0700215
Charles Chan50bb6ef2018-04-18 18:41:05 -0700216 // Remove flows for unchanged IPs only when the host moves from a switch to another.
Charles Chanf9a52702017-06-16 15:19:24 -0700217 // Otherwise, do not remove and let the adding part update the old flow
218 if (!newDeviceIds.contains(prevLocation.deviceId())) {
Charles Chan65238242017-06-22 18:03:14 -0700219 processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700220 Sets.intersection(prevIps, newIps).forEach(ip -> {
221 if (doubleTaggedHost) {
222 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
223 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
224 } else {
225 processRoutingRule(prevLocation.deviceId(), prevLocation.port(),
226 hostMac, hostVlanId, ip, true);
227 }
228 });
Charles Chanf9a52702017-06-16 15:19:24 -0700229 }
230
231 // Remove bridging rules if new interface vlan is different from old interface vlan
232 // Otherwise, do not remove and let the adding part update the old flow
233 if (newLocations.stream().noneMatch(newLocation -> {
234 VlanId oldAssignedVlan = srManager.getInternalVlanId(prevLocation);
235 VlanId newAssignedVlan = srManager.getInternalVlanId(newLocation);
236 // Host is tagged and the new location has the host vlan in vlan-tagged
Charles Chan971d7ba2018-05-01 11:50:20 -0700237 return srManager.interfaceService.getTaggedVlanId(newLocation).contains(hostVlanId) ||
Charles Chanf9a52702017-06-16 15:19:24 -0700238 (oldAssignedVlan != null && newAssignedVlan != null &&
239 // Host is untagged and the new location has the same assigned vlan
240 oldAssignedVlan.equals(newAssignedVlan));
241 })) {
Charles Chan65238242017-06-22 18:03:14 -0700242 processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true);
Charles Chanf9a52702017-06-16 15:19:24 -0700243 }
244
245 // Remove routing rules for unchanged IPs if none of the subnet of new location contains
246 // the IP. Otherwise, do not remove and let the adding part update the old flow
247 Sets.intersection(prevIps, newIps).forEach(ip -> {
248 if (newLocations.stream().noneMatch(newLocation ->
249 srManager.deviceConfiguration.inSameSubnet(newLocation, ip))) {
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700250 if (doubleTaggedHost) {
251 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
252 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
253 } else {
254 processRoutingRule(prevLocation.deviceId(), prevLocation.port(),
255 hostMac, hostVlanId, ip, true);
256 }
Charles Chanf9a52702017-06-16 15:19:24 -0700257 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700258 });
Charles Chanf9a52702017-06-16 15:19:24 -0700259 });
260
261 // For each new location, add all new IPs.
Charles Chan50bb6ef2018-04-18 18:41:05 -0700262 Sets.difference(newLocations, prevLocations).forEach(newLocation -> {
Charles Chan65238242017-06-22 18:03:14 -0700263 processBridgingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, false);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700264 newIps.forEach(ip -> {
265 if (doubleTaggedHost) {
266 processDoubleTaggedRoutingRule(newLocation.deviceId(), newLocation.port(),
267 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false);
268 } else {
Charles Chan65238242017-06-22 18:03:14 -0700269 processRoutingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId,
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700270 ip, false);
271 }
272 });
Charles Chan9bd0e5a2018-04-25 18:51:46 -0400273
274 // Probe on pair device when host move
275 // Majorly for the 2nd step of [1A/x, 1B/x] -> [1A/x, 1B/y] -> [1A/y, 1B/y]
276 // But will also cover [1A/x] -> [1A/y] -> [1A/y, 1B/y]
277 if (srManager.activeProbing) {
278 srManager.getPairDeviceId(newLocation.deviceId()).ifPresent(pairDeviceId ->
279 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort ->
280 probe(host, newLocation, pairDeviceId, pairRemotePort)
281 )
282 );
283 }
Charles Chanf9a52702017-06-16 15:19:24 -0700284 });
285
286 // For each unchanged location, add new IPs and remove old IPs.
Charles Chan50bb6ef2018-04-18 18:41:05 -0700287 Sets.intersection(newLocations, prevLocations).forEach(unchangedLocation -> {
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700288 Sets.difference(prevIps, newIps).forEach(ip -> {
289 if (doubleTaggedHost) {
290 processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
291 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
292 } else {
293 processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
294 hostMac, hostVlanId, ip, true);
295 }
296 });
Charles Chanf9a52702017-06-16 15:19:24 -0700297
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700298 Sets.difference(newIps, prevIps).forEach(ip -> {
299 if (doubleTaggedHost) {
300 processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
301 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false);
302 } else {
303 processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
304 hostMac, hostVlanId, ip, false);
305 }
306 });
Charles Chanf9a52702017-06-16 15:19:24 -0700307 });
Saurav Das45f48152018-01-18 12:07:33 -0800308
309 // ensure dual-homed host locations have viable uplinks
Saurav Das9a554292018-04-27 18:42:30 -0700310 if (newLocations.size() > prevLocations.size() || srManager.singleHomedDown) {
Saurav Das45f48152018-01-18 12:07:33 -0800311 newLocations.forEach(loc -> {
312 if (srManager.mastershipService.isLocalMaster(loc.deviceId())) {
Saurav Das9a554292018-04-27 18:42:30 -0700313 srManager.linkHandler.checkUplinksForHost(loc);
Saurav Das45f48152018-01-18 12:07:33 -0800314 }
315 });
316 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700317 }
318
Charles Chanf9a52702017-06-16 15:19:24 -0700319 void processHostUpdatedEvent(HostEvent event) {
Charles Chan47933752017-11-30 15:37:50 -0800320 Host host = event.subject();
321 MacAddress hostMac = host.mac();
322 VlanId hostVlanId = host.vlan();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700323 EthType hostTpid = host.tpid();
Charles Chan47933752017-11-30 15:37:50 -0800324 Set<HostLocation> locations = host.locations();
Charles Chan93e71ba2016-04-29 14:38:22 -0700325 Set<IpAddress> prevIps = event.prevSubject().ipAddresses();
Charles Chan47933752017-11-30 15:37:50 -0800326 Set<IpAddress> newIps = host.ipAddresses();
Charles Chan3d650a62017-11-20 08:46:24 -0800327 log.info("Host {}/{} is updated", hostMac, hostVlanId);
Charles Chan93e71ba2016-04-29 14:38:22 -0700328
Charles Chan2ff1bac2018-03-29 16:03:41 -0700329 locations.forEach(location -> {
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700330 Sets.difference(prevIps, newIps).forEach(ip -> {
331 if (isDoubleTaggedHost(host)) {
332 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
333 host.innerVlan(), hostVlanId, hostTpid, ip, true);
334 } else {
335 processRoutingRule(location.deviceId(), location.port(), hostMac,
336 hostVlanId, ip, true);
337 }
338 });
339 Sets.difference(newIps, prevIps).forEach(ip -> {
340 if (isDoubleTaggedHost(host)) {
341 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
342 host.innerVlan(), hostVlanId, hostTpid, ip, false);
343 } else {
344 processRoutingRule(location.deviceId(), location.port(), hostMac,
345 hostVlanId, ip, false);
346 }
347 });
Charles Chanf9a52702017-06-16 15:19:24 -0700348 });
Charles Chan3d650a62017-11-20 08:46:24 -0800349
350 // Use the pair link temporarily before the second location of a dual-homed host shows up.
351 // This do not affect single-homed hosts since the flow will be blocked in
352 // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively
Charles Chan47933752017-11-30 15:37:50 -0800353 locations.forEach(location ->
Charles Chan3d650a62017-11-20 08:46:24 -0800354 srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> {
Charles Chan2ff1bac2018-03-29 16:03:41 -0700355 if (locations.stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) {
Charles Chan47933752017-11-30 15:37:50 -0800356 Set<IpAddress> ipsToAdd = Sets.difference(newIps, prevIps);
357 Set<IpAddress> ipsToRemove = Sets.difference(prevIps, newIps);
358
Saurav Das9a554292018-04-27 18:42:30 -0700359 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> {
Charles Chan3d650a62017-11-20 08:46:24 -0800360 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
361 // when the host is untagged
362 VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(hostVlanId);
363
Charles Chan47933752017-11-30 15:37:50 -0800364 ipsToRemove.forEach(ip ->
Charles Chan3d650a62017-11-20 08:46:24 -0800365 processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, true)
366 );
Charles Chan47933752017-11-30 15:37:50 -0800367 ipsToAdd.forEach(ip ->
Charles Chan3d650a62017-11-20 08:46:24 -0800368 processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, false)
369 );
Charles Chan47933752017-11-30 15:37:50 -0800370
371 if (srManager.activeProbing) {
372 probe(host, location, pairDeviceId, pairRemotePort);
373 }
Charles Chan3d650a62017-11-20 08:46:24 -0800374 });
375 }
Charles Chan47933752017-11-30 15:37:50 -0800376 })
377 );
378 }
379
380 /**
381 * When a non-pair port comes up, probe each host on the pair device if
382 * (1) the host is tagged and the tagged vlan of current port contains host vlan; or
383 * (2) the host is untagged and the internal vlan is the same on the host port and current port.
384 *
385 * @param cp connect point
386 */
387 void processPortUp(ConnectPoint cp) {
Saurav Das9a554292018-04-27 18:42:30 -0700388 if (cp.port().equals(srManager.getPairLocalPort(cp.deviceId()).orElse(null))) {
Charles Chan47933752017-11-30 15:37:50 -0800389 return;
390 }
391 if (srManager.activeProbing) {
392 srManager.getPairDeviceId(cp.deviceId())
393 .ifPresent(pairDeviceId -> srManager.hostService.getConnectedHosts(pairDeviceId).stream()
394 .filter(host -> isHostInVlanOfPort(host, pairDeviceId, cp))
395 .forEach(host -> srManager.probingService.probeHostLocation(host, cp, ProbeMode.DISCOVER))
396 );
397 }
398 }
399
400 /**
401 * Checks if given host located on given device id matches VLAN config of current port.
402 *
403 * @param host host to check
404 * @param deviceId device id to check
405 * @param cp current connect point
406 * @return true if the host located at deviceId matches the VLAN config on cp
407 */
408 private boolean isHostInVlanOfPort(Host host, DeviceId deviceId, ConnectPoint cp) {
409 VlanId internalVlan = srManager.getInternalVlanId(cp);
Charles Chan971d7ba2018-05-01 11:50:20 -0700410 Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(cp);
Charles Chan47933752017-11-30 15:37:50 -0800411
412 return taggedVlan.contains(host.vlan()) ||
413 (internalVlan != null && host.locations().stream()
414 .filter(l -> l.deviceId().equals(deviceId))
415 .map(srManager::getInternalVlanId)
416 .anyMatch(internalVlan::equals));
417 }
418
419 /**
420 * Send a probe on all locations with the same VLAN on pair device, excluding pair port.
421 *
422 * @param host host to probe
423 * @param location newly discovered host location
424 * @param pairDeviceId pair device id
425 * @param pairRemotePort pair remote port
426 */
427 private void probe(Host host, ConnectPoint location, DeviceId pairDeviceId, PortNumber pairRemotePort) {
428 VlanId vlanToProbe = host.vlan().equals(VlanId.NONE) ?
429 srManager.getInternalVlanId(location) : host.vlan();
430 srManager.interfaceService.getInterfaces().stream()
431 .filter(i -> i.vlanTagged().contains(vlanToProbe) ||
432 i.vlanUntagged().equals(vlanToProbe) ||
433 i.vlanNative().equals(vlanToProbe))
434 .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId))
435 .filter(i -> !i.connectPoint().port().equals(pairRemotePort))
436 .forEach(i -> {
437 log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint());
438 srManager.probingService.probeHostLocation(host, i.connectPoint(), ProbeMode.DISCOVER);
439 });
Charles Chan93e71ba2016-04-29 14:38:22 -0700440 }
441
442 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700443 * Populates or revokes a bridging rule on given deviceId that matches given mac,
444 * given vlan and output to given port.
Charles Chanb7b4c932017-06-15 19:25:25 -0700445 *
446 * @param deviceId device ID
447 * @param port port
448 * @param mac mac address
449 * @param vlanId VLAN ID
450 * @param revoke true to revoke the rule; false to populate
451 */
452 private void processBridgingRule(DeviceId deviceId, PortNumber port, MacAddress mac,
453 VlanId vlanId, boolean revoke) {
Charles Chan2ff1bac2018-03-29 16:03:41 -0700454 log.info("{} bridging entry for host {}/{} at {}:{}", revoke ? "Revoking" : "Populating",
Charles Chanb7b4c932017-06-15 19:25:25 -0700455 mac, vlanId, deviceId, port);
456
Charles Chan2ff1bac2018-03-29 16:03:41 -0700457 if (!revoke) {
458 srManager.defaultRoutingHandler.populateBridging(deviceId, port, mac, vlanId);
459 } else {
460 srManager.defaultRoutingHandler.revokeBridging(deviceId, port, mac, vlanId);
Charles Chanb7b4c932017-06-15 19:25:25 -0700461 }
Charles Chanb7b4c932017-06-15 19:25:25 -0700462 }
463
464 /**
465 * Populate or revoke a routing rule on given deviceId that matches given ip,
466 * set destination mac to given mac, set vlan to given vlan and output to given port.
467 *
468 * @param deviceId device ID
469 * @param port port
470 * @param mac mac address
471 * @param vlanId VLAN ID
472 * @param ip IP address
473 * @param revoke true to revoke the rule; false to populate
474 */
475 private void processRoutingRule(DeviceId deviceId, PortNumber port, MacAddress mac,
476 VlanId vlanId, IpAddress ip, boolean revoke) {
477 ConnectPoint location = new ConnectPoint(deviceId, port);
Charles Chanf9a52702017-06-16 15:19:24 -0700478 if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) {
479 log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port);
480 return;
Charles Chanb7b4c932017-06-15 19:25:25 -0700481 }
Charles Chanb7b4c932017-06-15 19:25:25 -0700482
Charles Chanf9a52702017-06-16 15:19:24 -0700483 log.info("{} routing rule for {} at {}", revoke ? "Revoking" : "Populating", ip, location);
484 if (revoke) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700485 srManager.defaultRoutingHandler.revokeRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port);
Charles Chanf9a52702017-06-16 15:19:24 -0700486 } else {
Charles Chan2fde6d42017-08-23 14:46:43 -0700487 srManager.defaultRoutingHandler.populateRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port);
Charles Chan6ea94fc2016-05-10 17:29:47 -0700488 }
Charles Chan6ea94fc2016-05-10 17:29:47 -0700489 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700490
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700491 /**
492 * Populate or revoke a routing rule and egress rules on given deviceId that matches given IP,
493 * to set destination mac to given mac, set inner vlan and outer vlan to given vlans,
494 * set outer TPID, and output to given port.
495 *
496 * @param deviceId device ID
497 * @param port port
498 * @param mac mac address
499 * @param innerVlan inner VLAN ID
500 * @param outerVlan outer VLAN ID
501 * @param outerTpid outer TPID
502 * @param ip IP address
503 * @param revoke true to revoke the rule; false to populate
504 */
505 private void processDoubleTaggedRoutingRule(DeviceId deviceId, PortNumber port,
506 MacAddress mac, VlanId innerVlan,
507 VlanId outerVlan, EthType outerTpid,
508 IpAddress ip, boolean revoke) {
509 ConnectPoint location = new ConnectPoint(deviceId, port);
510 if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) {
511 log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port);
512 return;
513 }
514 log.info("{} routing rule for double-tagged host {} at {}",
515 revoke ? "Revoking" : "Populating", ip, location);
516 if (revoke) {
517 srManager.defaultRoutingHandler.revokeDoubleTaggedRoute(
518 deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port);
519 } else {
520 srManager.defaultRoutingHandler.populateDoubleTaggedRoute(
521 deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port);
522 }
523 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700524
525 /**
526 * Update forwarding objective for unicast bridging and unicast routing.
527 * Also check the validity of updated interface configuration on VLAN.
528 *
529 * @param deviceId device ID that host attaches to
530 * @param portNum port number that host attaches to
531 * @param vlanId Vlan ID configured on the switch port
532 * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise
533 * @param install true to populate the objective, false to revoke
534 */
535 void processIntfVlanUpdatedEvent(DeviceId deviceId, PortNumber portNum, VlanId vlanId,
536 boolean popVlan, boolean install) {
537 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNum);
538 Set<Host> hosts = hostService.getConnectedHosts(connectPoint);
539
540 if (hosts == null || hosts.size() == 0) {
Charles Chan10b2fee2018-04-21 00:44:29 -0700541 log.debug("processIntfVlanUpdatedEvent: No hosts connected to {}", connectPoint);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700542 return;
543 }
544
545 hosts.forEach(host -> {
546 MacAddress mac = host.mac();
547 VlanId hostVlanId = host.vlan();
548
549 // Check whether the host vlan is valid for new interface configuration
550 if ((!popVlan && hostVlanId.equals(vlanId)) ||
551 (popVlan && hostVlanId.equals(VlanId.NONE))) {
Charles Chan2ff1bac2018-03-29 16:03:41 -0700552 srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700553 // Update Forwarding objective and corresponding simple Next objective
554 // for each host and IP address connected to given port
555 host.ipAddresses().forEach(ipAddress ->
Charles Chan2ff1bac2018-03-29 16:03:41 -0700556 srManager.defaultRoutingHandler.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(),
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700557 mac, vlanId, popVlan, install)
558 );
559 }
560 });
561 }
562
563 /**
564 * Populate or revoke routing rule for each host, according to the updated
565 * subnet configuration on the interface.
566 * @param cp connect point of the updated interface
567 * @param ipPrefixSet IP Prefixes added or removed
568 * @param install true if IP Prefixes added, false otherwise
569 */
570 void processIntfIpUpdatedEvent(ConnectPoint cp, Set<IpPrefix> ipPrefixSet, boolean install) {
571 Set<Host> hosts = hostService.getConnectedHosts(cp);
572
573 if (hosts == null || hosts.size() == 0) {
Charles Chan10b2fee2018-04-21 00:44:29 -0700574 log.debug("processIntfIpUpdatedEvent: No hosts connected to {}", cp);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700575 return;
576 }
577
578 // Check whether the host IP address is in the interface's subnet
579 hosts.forEach(host ->
580 host.ipAddresses().forEach(hostIpAddress -> {
581 ipPrefixSet.forEach(ipPrefix -> {
582 if (install && ipPrefix.contains(hostIpAddress)) {
Charles Chan2ff1bac2018-03-29 16:03:41 -0700583 srManager.defaultRoutingHandler.populateRoute(cp.deviceId(), hostIpAddress.toIpPrefix(),
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700584 host.mac(), host.vlan(), cp.port());
585 } else if (!install && ipPrefix.contains(hostIpAddress)) {
Charles Chan2ff1bac2018-03-29 16:03:41 -0700586 srManager.defaultRoutingHandler.revokeRoute(cp.deviceId(), hostIpAddress.toIpPrefix(),
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700587 host.mac(), host.vlan(), cp.port());
588 }
589 });
590 }));
591 }
Saurav Das45f48152018-01-18 12:07:33 -0800592
593 /**
594 * Returns the set of portnumbers on the given device that are part of the
595 * locations for dual-homed hosts.
596 *
597 * @param deviceId the given deviceId
598 * @return set of port numbers on given device that are dual-homed host
599 * locations. May be empty if no dual homed hosts are connected to
600 * the given device
601 */
602 Set<PortNumber> getDualHomedHostPorts(DeviceId deviceId) {
603 Set<PortNumber> dualHomedLocations = new HashSet<>();
604 srManager.hostService.getConnectedHosts(deviceId).stream()
605 .filter(host -> host.locations().size() == 2)
606 .forEach(host -> host.locations().stream()
607 .filter(loc -> loc.deviceId().equals(deviceId))
608 .forEach(loc -> dualHomedLocations.add(loc.port())));
609 return dualHomedLocations;
610 }
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700611
612 /**
613 * Checks if the given host is double-tagged or not.
614 *
615 * @param host host to check
616 * @return true if it is double-tagged, false otherwise
617 */
618 private boolean isDoubleTaggedHost(Host host) {
619 return !host.innerVlan().equals(VlanId.NONE);
620 }
621
Charles Chand2990362016-04-18 13:44:03 -0700622}