sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.segmentrouting; |
| 17 | |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Charles Chan | 09bf269 | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 20 | import org.onlab.packet.EthType; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ethernet; |
Charles Chan | 78ec505 | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IPv6; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Ip4Address; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ip6Address; |
| 25 | import org.onlab.packet.IpAddress; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onlab.packet.MacAddress; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 28 | import org.onlab.packet.MplsLabel; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 29 | import org.onlab.packet.VlanId; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flowobjective.Objective; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flowobjective.ObjectiveError; |
Charles Chan | 2df0e8a | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 35 | import org.onosproject.net.packet.PacketPriority; |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 36 | import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 37 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 38 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 39 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 40 | import org.onosproject.segmentrouting.grouphandler.DestinationSet; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 41 | import org.onosproject.net.DeviceId; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 42 | import org.onosproject.net.Port; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 43 | import org.onosproject.net.PortNumber; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 44 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 45 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flow.TrafficSelector; |
| 47 | import org.onosproject.net.flow.TrafficTreatment; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flow.criteria.Criteria; |
| 49 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 50 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 51 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 52 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 53 | import org.onosproject.net.flowobjective.ForwardingObjective.Builder; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 54 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | import org.slf4j.LoggerFactory; |
| 57 | |
| 58 | import java.util.ArrayList; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 59 | import java.util.Collection; |
| 60 | import java.util.Collections; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 61 | import java.util.HashMap; |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 62 | import java.util.HashSet; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 63 | import java.util.List; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 64 | import java.util.Map; |
Charles Chan | 2df0e8a | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 65 | import java.util.Optional; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 66 | import java.util.Set; |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 67 | import java.util.concurrent.atomic.AtomicLong; |
Charles Chan | dd33be5 | 2018-02-26 21:33:25 -0800 | [diff] [blame] | 68 | import java.util.stream.Collectors; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 69 | |
| 70 | import static com.google.common.base.Preconditions.checkNotNull; |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 71 | import static org.onlab.packet.Ethernet.TYPE_ARP; |
| 72 | import static org.onlab.packet.Ethernet.TYPE_IPV6; |
Charles Chan | 09bf269 | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 73 | import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT; |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 74 | import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION; |
Charles Chan | 09bf269 | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 75 | import static org.onlab.packet.ICMP6.ROUTER_ADVERTISEMENT; |
| 76 | import static org.onlab.packet.ICMP6.ROUTER_SOLICITATION; |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 77 | import static org.onlab.packet.IPv6.PROTOCOL_ICMP6; |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 78 | import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 79 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Populator of segment routing flow rules. |
| 82 | */ |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 83 | public class RoutingRulePopulator { |
Charles Chan | 78ec505 | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 84 | private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class); |
| 85 | |
| 86 | private static final int ARP_NDP_PRIORITY = 30000; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 87 | |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 88 | private AtomicLong rulePopulationCounter; |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 89 | private SegmentRoutingManager srManager; |
| 90 | private DeviceConfiguration config; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 91 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Creates a RoutingRulePopulator object. |
| 94 | * |
Thomas Vachuska | e10f56b | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 95 | * @param srManager segment routing manager reference |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 96 | */ |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 97 | RoutingRulePopulator(SegmentRoutingManager srManager) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 98 | this.srManager = srManager; |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 99 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 100 | this.rulePopulationCounter = new AtomicLong(0); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Resets the population counter. |
| 105 | */ |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 106 | void resetCounter() { |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 107 | rulePopulationCounter.set(0); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the number of rules populated. |
Thomas Vachuska | 266b443 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 112 | * |
| 113 | * @return number of rules |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 114 | */ |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 115 | long getCounter() { |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 116 | return rulePopulationCounter.get(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 120 | * Populate a bridging rule on given deviceId that matches given mac, given vlan and |
| 121 | * output to given port. |
| 122 | * |
| 123 | * @param deviceId device ID |
| 124 | * @param port port |
| 125 | * @param mac mac address |
| 126 | * @param vlanId VLAN ID |
| 127 | */ |
| 128 | void populateBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) { |
| 129 | ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, false); |
| 130 | if (fob == null) { |
| 131 | log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 2f2c9d0 | 2018-04-07 16:51:09 -0700 | [diff] [blame] | 136 | (objective) -> log.debug("Brigding rule for {}/{} populated", mac, vlanId), |
| 137 | (objective, error) -> log.warn("Failed to populate bridging rule for {}/{}: {}", mac, vlanId, error)); |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 138 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Revoke a bridging rule on given deviceId that matches given mac, given vlan and |
| 143 | * output to given port. |
| 144 | * |
| 145 | * @param deviceId device ID |
| 146 | * @param port port |
| 147 | * @param mac mac address |
| 148 | * @param vlanId VLAN ID |
| 149 | */ |
| 150 | void revokeBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) { |
| 151 | ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, true); |
| 152 | if (fob == null) { |
| 153 | log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 2f2c9d0 | 2018-04-07 16:51:09 -0700 | [diff] [blame] | 158 | (objective) -> log.debug("Brigding rule for {}/{} revoked", mac, vlanId), |
| 159 | (objective, error) -> log.warn("Failed to revoke bridging rule for {}/{}: {}", mac, vlanId, error)); |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 160 | srManager.flowObjectiveService.forward(deviceId, fob.remove(context)); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Generates a forwarding objective builder for bridging rules. |
| 165 | * <p> |
| 166 | * The forwarding objective bridges packets destined to a given MAC to |
| 167 | * given port on given device. |
| 168 | * |
| 169 | * @param deviceId Device that host attaches to |
| 170 | * @param mac MAC address of the host |
| 171 | * @param hostVlanId VLAN ID of the host |
| 172 | * @param outport Port that host attaches to |
| 173 | * @param revoke true if forwarding objective is meant to revoke forwarding rule |
| 174 | * @return Forwarding objective builder |
| 175 | */ |
| 176 | private ForwardingObjective.Builder bridgingFwdObjBuilder( |
| 177 | DeviceId deviceId, MacAddress mac, VlanId hostVlanId, PortNumber outport, boolean revoke) { |
| 178 | ConnectPoint connectPoint = new ConnectPoint(deviceId, outport); |
| 179 | VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint); |
| 180 | Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint); |
| 181 | VlanId nativeVlan = srManager.getNativeVlanId(connectPoint); |
| 182 | |
| 183 | // Create host selector |
| 184 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 185 | sbuilder.matchEthDst(mac); |
| 186 | |
| 187 | // Create host treatment |
| 188 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 189 | tbuilder.immediate().setOutput(outport); |
| 190 | |
| 191 | // Create host meta |
| 192 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 193 | |
| 194 | // Adjust the selector, treatment and meta according to VLAN configuration |
| 195 | if (taggedVlans.contains(hostVlanId)) { |
| 196 | sbuilder.matchVlanId(hostVlanId); |
| 197 | mbuilder.matchVlanId(hostVlanId); |
| 198 | } else if (hostVlanId.equals(VlanId.NONE)) { |
| 199 | if (untaggedVlan != null) { |
| 200 | sbuilder.matchVlanId(untaggedVlan); |
| 201 | mbuilder.matchVlanId(untaggedVlan); |
| 202 | tbuilder.immediate().popVlan(); |
| 203 | } else if (nativeVlan != null) { |
| 204 | sbuilder.matchVlanId(nativeVlan); |
| 205 | mbuilder.matchVlanId(nativeVlan); |
| 206 | tbuilder.immediate().popVlan(); |
| 207 | } else { |
| 208 | log.warn("Untagged host {}/{} is not allowed on {} without untagged or native" + |
| 209 | "vlan config", mac, hostVlanId, connectPoint); |
| 210 | return null; |
| 211 | } |
| 212 | } else { |
| 213 | log.warn("Tagged host {}/{} is not allowed on {} without VLAN listed in tagged vlan", |
| 214 | mac, hostVlanId, connectPoint); |
| 215 | return null; |
| 216 | } |
| 217 | |
| 218 | // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed. |
| 219 | // If the objective is to revoke an existing rule, and for some reason |
| 220 | // the next-objective does not exist, then a new one should not be created |
| 221 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outport, |
| 222 | tbuilder.build(), mbuilder.build(), !revoke); |
| 223 | if (portNextObjId == -1) { |
| 224 | // Warning log will come from getPortNextObjective method |
| 225 | return null; |
| 226 | } |
| 227 | |
| 228 | return DefaultForwardingObjective.builder() |
| 229 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 230 | .withSelector(sbuilder.build()) |
| 231 | .nextStep(portNextObjId) |
| 232 | .withPriority(100) |
| 233 | .fromApp(srManager.appId) |
| 234 | .makePermanent(); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Populate or revoke a bridging rule on given deviceId that matches given vlanId, |
| 239 | * and hostMAC connected to given port, and output to given port only when |
| 240 | * vlan information is valid. |
| 241 | * |
| 242 | * @param deviceId device ID that host attaches to |
| 243 | * @param portNum port number that host attaches to |
| 244 | * @param hostMac mac address of the host connected to the switch port |
| 245 | * @param vlanId Vlan ID configured on the switch port |
| 246 | * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise |
| 247 | * @param install true to populate the objective, false to revoke |
| 248 | */ |
| 249 | // TODO Refactor. There are a lot of duplications between this method, populateBridging, |
| 250 | // revokeBridging and bridgingFwdObjBuilder. |
| 251 | void updateBridging(DeviceId deviceId, PortNumber portNum, MacAddress hostMac, |
| 252 | VlanId vlanId, boolean popVlan, boolean install) { |
| 253 | // Create host selector |
| 254 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 255 | sbuilder.matchEthDst(hostMac); |
| 256 | |
| 257 | // Create host meta |
| 258 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 259 | |
| 260 | sbuilder.matchVlanId(vlanId); |
| 261 | mbuilder.matchVlanId(vlanId); |
| 262 | |
| 263 | // Create host treatment |
| 264 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 265 | tbuilder.immediate().setOutput(portNum); |
| 266 | |
| 267 | if (popVlan) { |
| 268 | tbuilder.immediate().popVlan(); |
| 269 | } |
| 270 | |
| 271 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum, |
| 272 | tbuilder.build(), mbuilder.build(), install); |
| 273 | if (portNextObjId != -1) { |
| 274 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder() |
| 275 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 276 | .withSelector(sbuilder.build()) |
| 277 | .nextStep(portNextObjId) |
| 278 | .withPriority(100) |
| 279 | .fromApp(srManager.appId) |
| 280 | .makePermanent(); |
| 281 | |
| 282 | ObjectiveContext context = new DefaultObjectiveContext( |
| 283 | (objective) -> log.debug("Brigding rule for {}/{} {}", hostMac, vlanId, |
| 284 | install ? "populated" : "revoked"), |
| 285 | (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}", |
| 286 | install ? "populate" : "revoke", hostMac, vlanId, error)); |
| 287 | srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context)); |
| 288 | } else { |
| 289 | log.warn("Failed to retrieve next objective for {}/{}", hostMac, vlanId); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Populates IP rules for a route that has direct connection to the switch. |
| 295 | * This method should not be invoked directly without going through DefaultRoutingHandler. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 296 | * |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 297 | * @param deviceId device ID of the device that next hop attaches to |
| 298 | * @param prefix IP prefix of the route |
| 299 | * @param hostMac MAC address of the next hop |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 300 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 301 | * @param outPort port where the next hop attaches to |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 302 | */ |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 303 | void populateRoute(DeviceId deviceId, IpPrefix prefix, |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 304 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 305 | log.debug("Populate direct routing entry for route {} at {}:{}", |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 306 | prefix, deviceId, outPort); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 307 | ForwardingObjective.Builder fwdBuilder; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 308 | try { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 309 | fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, |
| 310 | hostVlanId, outPort, false); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 311 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 312 | log.warn(e.getMessage() + " Aborting direct populateRoute"); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 313 | return; |
| 314 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 315 | if (fwdBuilder == null) { |
Saurav Das | 76ae681 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 316 | log.warn("Aborting host routing table entry due " |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 317 | + "to error for dev:{} route:{}", deviceId, prefix); |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 318 | return; |
| 319 | } |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 320 | |
| 321 | int nextId = fwdBuilder.add().nextId(); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 322 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 323 | (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}", |
| 324 | prefix, nextId), |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 325 | (objective, error) -> |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 326 | log.warn("Failed to populate direct routing rule for route {}: {}", |
Saurav Das | 76ae681 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 327 | prefix, error)); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 328 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context)); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 329 | rulePopulationCounter.incrementAndGet(); |
| 330 | } |
| 331 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 332 | /** |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 333 | * Removes IP rules for a route when the next hop is gone. |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 334 | * This method should not be invoked directly without going through DefaultRoutingHandler. |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 335 | * |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 336 | * @param deviceId device ID of the device that next hop attaches to |
| 337 | * @param prefix IP prefix of the route |
| 338 | * @param hostMac MAC address of the next hop |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 339 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 340 | * @param outPort port that next hop attaches to |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 341 | */ |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 342 | void revokeRoute(DeviceId deviceId, IpPrefix prefix, |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 343 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) { |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 344 | log.debug("Revoke IP table entry for route {} at {}:{}", |
| 345 | prefix, deviceId, outPort); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 346 | ForwardingObjective.Builder fwdBuilder; |
| 347 | try { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 348 | fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, |
| 349 | hostVlanId, outPort, true); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 350 | } catch (DeviceConfigNotFoundException e) { |
| 351 | log.warn(e.getMessage() + " Aborting revokeIpRuleForHost."); |
| 352 | return; |
| 353 | } |
Charles Chan | 458b826 | 2016-11-30 11:55:05 -0800 | [diff] [blame] | 354 | if (fwdBuilder == null) { |
| 355 | log.warn("Aborting host routing table entries due " |
| 356 | + "to error for dev:{} route:{}", deviceId, prefix); |
| 357 | return; |
| 358 | } |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 359 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 360 | (objective) -> log.debug("IP rule for route {} revoked", prefix), |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 361 | (objective, error) -> |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 362 | log.warn("Failed to revoke IP rule for route {}: {}", prefix, error)); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 363 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context)); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 366 | /** |
Charles Chan | f6ec153 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 367 | * Returns a forwarding objective builder for routing rules. |
| 368 | * <p> |
| 369 | * The forwarding objective routes packets destined to a given prefix to |
| 370 | * given port on given device with given destination MAC. |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 371 | * |
| 372 | * @param deviceId device ID |
| 373 | * @param prefix prefix that need to be routed |
| 374 | * @param hostMac MAC address of the nexthop |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 375 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 376 | * @param outPort port where the nexthop attaches to |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 377 | * @param revoke true if forwarding objective is meant to revoke forwarding rule |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 378 | * @return forwarding objective builder |
| 379 | * @throws DeviceConfigNotFoundException if given device is not configured |
| 380 | */ |
Charles Chan | f6ec153 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 381 | private ForwardingObjective.Builder routingFwdObjBuilder( |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 382 | DeviceId deviceId, IpPrefix prefix, |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 383 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, |
| 384 | boolean revoke) |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 385 | throws DeviceConfigNotFoundException { |
| 386 | MacAddress deviceMac; |
| 387 | deviceMac = config.getDeviceMac(deviceId); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 388 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 389 | ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort); |
| 390 | VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint); |
| 391 | Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint); |
| 392 | VlanId nativeVlan = srManager.getNativeVlanId(connectPoint); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 393 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 394 | // Create route selector |
| 395 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix); |
| 396 | |
| 397 | // Create route treatment |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 398 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 399 | tbuilder.deferred() |
| 400 | .setEthDst(hostMac) |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 401 | .setEthSrc(deviceMac) |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 402 | .setOutput(outPort); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 403 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 404 | // Create route meta |
| 405 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 406 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 407 | // Adjust the meta according to VLAN configuration |
| 408 | if (taggedVlans.contains(hostVlanId)) { |
| 409 | tbuilder.setVlanId(hostVlanId); |
| 410 | } else if (hostVlanId.equals(VlanId.NONE)) { |
| 411 | if (untaggedVlan != null) { |
| 412 | mbuilder.matchVlanId(untaggedVlan); |
| 413 | } else if (nativeVlan != null) { |
| 414 | mbuilder.matchVlanId(nativeVlan); |
| 415 | } else { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 416 | log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan", |
| 417 | hostMac, hostVlanId, connectPoint); |
| 418 | return null; |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 419 | } |
| 420 | } else { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 421 | log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed" |
| 422 | + " in tagged vlan", hostMac, hostVlanId, connectPoint); |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 423 | return null; |
| 424 | } |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 425 | // if the objective is to revoke an existing rule, and for some reason |
| 426 | // the next-objective does not exist, then a new one should not be created |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 427 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort, |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 428 | tbuilder.build(), mbuilder.build(), !revoke); |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 429 | if (portNextObjId == -1) { |
| 430 | // Warning log will come from getPortNextObjective method |
| 431 | return null; |
| 432 | } |
| 433 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 434 | return DefaultForwardingObjective.builder() |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 435 | .withSelector(sbuilder.build()) |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 436 | .nextStep(portNextObjId) |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 437 | .fromApp(srManager.appId).makePermanent() |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 438 | .withPriority(getPriorityFromPrefix(prefix)) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 439 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /** |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 443 | * Populates IP flow rules for all the given prefixes reachable from the |
| 444 | * destination switch(es). |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 445 | * |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 446 | * @param targetSw switch where rules are to be programmed |
| 447 | * @param subnets subnets/prefixes being added |
| 448 | * @param destSw1 destination switch where the prefixes are reachable |
| 449 | * @param destSw2 paired destination switch if one exists for the subnets/prefixes. |
| 450 | * Should be null if there is no paired destination switch (by config) |
| 451 | * or if the given prefixes are reachable only via destSw1 |
| 452 | * @param nextHops a map containing a set of next-hops for each destination switch. |
| 453 | * If destSw2 is not null, then this map must contain an |
| 454 | * entry for destSw2 with its next-hops from the targetSw |
| 455 | * (although the next-hop set may be empty in certain scenarios). |
| 456 | * If destSw2 is null, there should not be an entry in this |
| 457 | * map for destSw2. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 458 | * @return true if all rules are set successfully, false otherwise |
| 459 | */ |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 460 | boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets, |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 461 | DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 462 | for (IpPrefix subnet : subnets) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 463 | if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 464 | return false; |
| 465 | } |
| 466 | } |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 467 | return true; |
| 468 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 469 | |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 470 | /** |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 471 | * Revokes IP flow rules for the subnets from given device. |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 472 | * |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 473 | * @param targetSw target switch from which the subnets need to be removed |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 474 | * @param subnets subnet being removed |
| 475 | * @return true if all rules are removed successfully, false otherwise |
| 476 | */ |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 477 | boolean revokeIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets) { |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 478 | for (IpPrefix subnet : subnets) { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 479 | if (!revokeIpRuleForRouter(targetSw, subnet)) { |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 480 | return false; |
| 481 | } |
| 482 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 483 | return true; |
| 484 | } |
| 485 | |
| 486 | /** |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 487 | * Populates IP flow rules for an IP prefix in the target device. The prefix |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 488 | * is reachable via destination device(s). |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 489 | * |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 490 | * @param targetSw target device ID to set the rules |
| 491 | * @param ipPrefix the IP prefix |
| 492 | * @param destSw1 destination switch where the prefixes are reachable |
| 493 | * @param destSw2 paired destination switch if one exists for the subnets/prefixes. |
| 494 | * Should be null if there is no paired destination switch (by config) |
| 495 | * or if the given prefixes are reachable only via destSw1 |
| 496 | * @param nextHops map of destination switches and their next-hops. |
| 497 | * Should only contain destination switches that are |
| 498 | * actually meant to be routed to. If destSw2 is null, there |
| 499 | * should not be an entry for destSw2 in this map. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 500 | * @return true if all rules are set successfully, false otherwise |
| 501 | */ |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 502 | private boolean populateIpRuleForRouter(DeviceId targetSw, |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 503 | IpPrefix ipPrefix, DeviceId destSw1, |
| 504 | DeviceId destSw2, |
| 505 | Map<DeviceId, Set<DeviceId>> nextHops) { |
| 506 | int segmentId1, segmentId2 = -1; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 507 | try { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 508 | if (ipPrefix.isIp4()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 509 | segmentId1 = config.getIPv4SegmentId(destSw1); |
| 510 | if (destSw2 != null) { |
| 511 | segmentId2 = config.getIPv4SegmentId(destSw2); |
| 512 | } |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 513 | } else { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 514 | segmentId1 = config.getIPv6SegmentId(destSw1); |
| 515 | if (destSw2 != null) { |
| 516 | segmentId2 = config.getIPv6SegmentId(destSw2); |
| 517 | } |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 518 | } |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 519 | } catch (DeviceConfigNotFoundException e) { |
| 520 | log.warn(e.getMessage() + " Aborting populateIpRuleForRouter."); |
| 521 | return false; |
| 522 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 523 | |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 524 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 525 | TrafficSelector selector = sbuilder.build(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 526 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 527 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 528 | DestinationSet ds; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 529 | TrafficTreatment treatment; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 530 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 531 | if (destSw2 == null) { |
| 532 | // single dst - create destination set based on next-hop |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 533 | // If the next hop is the same as the final destination, then MPLS |
| 534 | // label is not set. |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 535 | Set<DeviceId> nhd1 = nextHops.get(destSw1); |
| 536 | if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) { |
| 537 | tbuilder.immediate().decNwTtl(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 538 | ds = new DestinationSet(false, false, destSw1); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 539 | treatment = tbuilder.build(); |
| 540 | } else { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 541 | ds = new DestinationSet(false, false, segmentId1, destSw1); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 542 | treatment = null; |
| 543 | } |
| 544 | } else { |
| 545 | // dst pair - IP rules for dst-pairs are always from other edge nodes |
| 546 | // the destination set needs to have both destinations, even if there |
| 547 | // are no next hops to one of them |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 548 | ds = new DestinationSet(false, false, segmentId1, destSw1, |
| 549 | segmentId2, destSw2); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 550 | treatment = null; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 553 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 554 | // if needed by the switch pipeline. Since neighbor sets are always to |
| 555 | // other neighboring routers, there is no subnet assigned on those ports. |
| 556 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector); |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 557 | metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 558 | DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 559 | if (grpHandler == null) { |
| 560 | log.warn("populateIPRuleForRouter: groupHandler for device {} " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 561 | + "not found", targetSw); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 562 | return false; |
| 563 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 564 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 565 | int nextId = grpHandler.getNextObjectiveId(ds, nextHops, |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 566 | metabuilder.build(), false); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 567 | if (nextId <= 0) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 568 | log.warn("No next objective in {} for ds: {}", targetSw, ds); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 572 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 573 | .builder() |
| 574 | .fromApp(srManager.appId) |
| 575 | .makePermanent() |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 576 | .nextStep(nextId) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 577 | .withSelector(selector) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 578 | .withPriority(getPriorityFromPrefix(ipPrefix)) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 579 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 580 | if (treatment != null) { |
| 581 | fwdBuilder.withTreatment(treatment); |
| 582 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 583 | log.debug("Installing IPv4 forwarding objective for router IP/subnet {} " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 584 | + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 585 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 586 | (objective) -> log.debug("IP rule for router {} populated in dev:{}", |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 587 | ipPrefix, targetSw), |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 588 | (objective, error) -> |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 589 | log.warn("Failed to populate IP rule for router {}: {} in dev:{}", |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 590 | ipPrefix, error, targetSw)); |
| 591 | srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context)); |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 592 | rulePopulationCounter.incrementAndGet(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 593 | |
| 594 | return true; |
| 595 | } |
| 596 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 597 | /** |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 598 | * Revokes IP flow rules for the router IP address from given device. |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 599 | * |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 600 | * @param targetSw target switch from which the ipPrefix need to be removed |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 601 | * @param ipPrefix the IP address of the destination router |
| 602 | * @return true if all rules are removed successfully, false otherwise |
| 603 | */ |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 604 | private boolean revokeIpRuleForRouter(DeviceId targetSw, IpPrefix ipPrefix) { |
Pier Ventre | 968da12 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 605 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 606 | TrafficSelector selector = sbuilder.build(); |
| 607 | TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build(); |
| 608 | |
| 609 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 610 | .builder() |
| 611 | .fromApp(srManager.appId) |
| 612 | .makePermanent() |
| 613 | .withSelector(selector) |
| 614 | .withTreatment(dummyTreatment) |
| 615 | .withPriority(getPriorityFromPrefix(ipPrefix)) |
| 616 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 617 | |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 618 | ObjectiveContext context = new DefaultObjectiveContext( |
| 619 | (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, targetSw), |
| 620 | (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}", |
| 621 | ipPrefix, targetSw, error)); |
| 622 | srManager.flowObjectiveService.forward(targetSw, fwdBuilder.remove(context)); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 623 | |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | /** |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 628 | * Populates MPLS flow rules in the target device to point towards the |
| 629 | * destination device. |
| 630 | * |
| 631 | * @param targetSwId target device ID of the switch to set the rules |
| 632 | * @param destSwId destination switch device ID |
| 633 | * @param nextHops next hops switch ID list |
| 634 | * @param routerIp the router ip of the destination switch |
| 635 | * @return true if all rules are set successfully, false otherwise |
| 636 | */ |
| 637 | boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId, |
| 638 | Set<DeviceId> nextHops, IpAddress routerIp) { |
| 639 | int segmentId; |
| 640 | try { |
| 641 | if (routerIp.isIp4()) { |
| 642 | segmentId = config.getIPv4SegmentId(destSwId); |
| 643 | } else { |
| 644 | segmentId = config.getIPv6SegmentId(destSwId); |
| 645 | } |
| 646 | } catch (DeviceConfigNotFoundException e) { |
| 647 | log.warn(e.getMessage() + " Aborting populateMplsRule."); |
| 648 | return false; |
| 649 | } |
| 650 | |
| 651 | List<ForwardingObjective> fwdObjs = new ArrayList<>(); |
| 652 | Collection<ForwardingObjective> fwdObjsMpls; |
| 653 | // Generates the transit rules used by the standard "routing". |
| 654 | fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, |
| 655 | routerIp, true); |
| 656 | if (fwdObjsMpls.isEmpty()) { |
| 657 | return false; |
| 658 | } |
| 659 | fwdObjs.addAll(fwdObjsMpls); |
| 660 | |
| 661 | // Generates the transit rules used by the MPLS Pwaas. |
| 662 | int pwSrLabel; |
| 663 | try { |
| 664 | pwSrLabel = config.getPWRoutingLabel(destSwId); |
| 665 | } catch (DeviceConfigNotFoundException e) { |
| 666 | log.warn(e.getMessage() |
| 667 | + " Aborting populateMplsRule. No label for PseudoWire traffic."); |
| 668 | return false; |
| 669 | } |
| 670 | fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel, |
| 671 | routerIp, false); |
| 672 | if (fwdObjsMpls.isEmpty()) { |
| 673 | return false; |
| 674 | } |
| 675 | fwdObjs.addAll(fwdObjsMpls); |
| 676 | |
| 677 | for (ForwardingObjective fwdObj : fwdObjs) { |
| 678 | log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}", |
| 679 | fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId); |
| 680 | srManager.flowObjectiveService.forward(targetSwId, fwdObj); |
| 681 | rulePopulationCounter.incrementAndGet(); |
| 682 | } |
| 683 | |
| 684 | return true; |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Differentiates between popping and swapping labels when building an MPLS |
| 689 | * forwarding objective. |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 690 | * |
| 691 | * @param targetSwId the target sw |
| 692 | * @param destSwId the destination sw |
| 693 | * @param nextHops the set of next hops |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 694 | * @param segmentId the segmentId to match representing the destination |
| 695 | * switch |
| 696 | * @param routerIp the router ip representing the destination switch |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 697 | * @return a collection of fwdobjective |
| 698 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 699 | private Collection<ForwardingObjective> handleMpls( |
| 700 | DeviceId targetSwId, |
| 701 | DeviceId destSwId, |
| 702 | Set<DeviceId> nextHops, |
| 703 | int segmentId, |
| 704 | IpAddress routerIp, |
| 705 | boolean isMplsBos) { |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 706 | |
| 707 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 708 | List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList(); |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 709 | // For the transport of Pwaas we can use two or three MPLS label |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 710 | sbuilder.matchEthType(Ethernet.MPLS_UNICAST); |
| 711 | sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId)); |
| 712 | sbuilder.matchMplsBos(isMplsBos); |
| 713 | TrafficSelector selector = sbuilder.build(); |
| 714 | |
| 715 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 716 | // if needed by the switch pipeline. Since mpls next-hops are always to |
| 717 | // other neighboring routers, there is no subnet assigned on those ports. |
| 718 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector); |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 719 | metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 720 | |
| 721 | if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) { |
| 722 | // If the next hop is the destination router for the segment, do pop |
| 723 | log.debug("populateMplsRule: Installing MPLS forwarding objective for " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 724 | + "label {} in switch {} with pop to next-hops {}", |
| 725 | segmentId, targetSwId, nextHops); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 726 | ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 727 | getMplsForwardingObjective(targetSwId, |
| 728 | nextHops, |
| 729 | true, |
| 730 | isMplsBos, |
| 731 | metabuilder.build(), |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 732 | routerIp, |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 733 | segmentId, |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 734 | destSwId); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 735 | // Error case, we cannot handle, exit. |
| 736 | if (fwdObjNoBosBuilder == null) { |
| 737 | return Collections.emptyList(); |
| 738 | } |
| 739 | fwdObjBuilders.add(fwdObjNoBosBuilder); |
| 740 | |
| 741 | } else { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 742 | // next hop is not destination, irrespective of the number of next |
| 743 | // hops (1 or more) -- SR CONTINUE case (swap with self) |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 744 | log.debug("Installing MPLS forwarding objective for " |
| 745 | + "label {} in switch {} without pop to next-hops {}", |
| 746 | segmentId, targetSwId, nextHops); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 747 | ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 748 | getMplsForwardingObjective(targetSwId, |
| 749 | nextHops, |
| 750 | false, |
| 751 | isMplsBos, |
| 752 | metabuilder.build(), |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 753 | routerIp, |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 754 | segmentId, |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 755 | destSwId); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 756 | // Error case, we cannot handle, exit. |
| 757 | if (fwdObjNoBosBuilder == null) { |
| 758 | return Collections.emptyList(); |
| 759 | } |
| 760 | fwdObjBuilders.add(fwdObjNoBosBuilder); |
| 761 | |
| 762 | } |
| 763 | |
| 764 | List<ForwardingObjective> fwdObjs = Lists.newArrayList(); |
| 765 | // We add the final property to the fwdObjs. |
| 766 | for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) { |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 767 | ((Builder) ((Builder) fwdObjBuilder |
| 768 | .fromApp(srManager.appId) |
| 769 | .makePermanent()) |
| 770 | .withSelector(selector) |
| 771 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)) |
| 772 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 773 | |
| 774 | ObjectiveContext context = new DefaultObjectiveContext( |
| 775 | (objective) -> |
| 776 | log.debug("MPLS rule {} for SID {} populated in dev:{} ", |
| 777 | objective.id(), segmentId, targetSwId), |
| 778 | (objective, error) -> |
| 779 | log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}", |
| 780 | objective.id(), segmentId, error, targetSwId)); |
| 781 | |
| 782 | ForwardingObjective fob = fwdObjBuilder.add(context); |
| 783 | fwdObjs.add(fob); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | return fwdObjs; |
| 787 | } |
| 788 | |
| 789 | /** |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 790 | * Returns a Forwarding Objective builder for the MPLS rule that references |
| 791 | * the desired Next Objective. Creates a DestinationSet that allows the |
| 792 | * groupHandler to create or find the required next objective. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 793 | * |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 794 | * @param targetSw the target sw |
| 795 | * @param nextHops the set of next hops |
| 796 | * @param phpRequired true if penultimate-hop-popping is required |
| 797 | * @param isBos true if matched label is bottom-of-stack |
| 798 | * @param meta metadata for creating next objective |
| 799 | * @param routerIp the router ip representing the destination switch |
| 800 | * @param destSw the destination sw |
| 801 | * @return the mpls forwarding objective builder |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 802 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 803 | private ForwardingObjective.Builder getMplsForwardingObjective( |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 804 | DeviceId targetSw, |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 805 | Set<DeviceId> nextHops, |
| 806 | boolean phpRequired, |
| 807 | boolean isBos, |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 808 | TrafficSelector meta, |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 809 | IpAddress routerIp, |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 810 | int segmentId, |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 811 | DeviceId destSw) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 812 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 813 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 814 | .builder().withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 815 | |
| 816 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 817 | DestinationSet ds = null; |
| 818 | boolean simple = false; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 819 | if (phpRequired) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 820 | // php case - pop should always be flow-action |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 821 | log.debug("getMplsForwardingObjective: php required"); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 822 | tbuilder.deferred().copyTtlIn(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 823 | if (isBos) { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 824 | if (routerIp.isIp4()) { |
| 825 | tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType()); |
| 826 | } else { |
| 827 | tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType()); |
| 828 | } |
| 829 | tbuilder.decNwTtl(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 830 | // standard case -> BoS == True; pop results in IP packet and forwarding |
| 831 | // is via an ECMP group |
| 832 | ds = new DestinationSet(false, false, destSw); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 833 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 834 | tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType()) |
| 835 | .decMplsTtl(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 836 | // double-label case -> BoS == False, pop results in MPLS packet |
| 837 | // depending on configuration we can ECMP this packet or choose one output |
| 838 | if (srManager.getMplsEcmp()) { |
| 839 | ds = new DestinationSet(true, false, destSw); |
| 840 | } else { |
| 841 | ds = new DestinationSet(true, false, destSw); |
| 842 | simple = true; |
| 843 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 844 | } |
| 845 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 846 | // swap with self case - SR CONTINUE |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 847 | log.debug("getMplsForwardingObjective: swap with self"); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 848 | tbuilder.deferred().decMplsTtl(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 849 | // swap results in MPLS packet with same BoS bit regardless of bit value |
| 850 | // depending on configuration we can ECMP this packet or choose one output |
Saurav Das | e0d4c87 | 2018-03-05 14:37:16 -0800 | [diff] [blame] | 851 | // XXX reconsider types |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 852 | if (srManager.getMplsEcmp()) { |
| 853 | ds = new DestinationSet(false, true, segmentId, destSw); |
| 854 | } else { |
| 855 | ds = new DestinationSet(false, true, segmentId, destSw); |
| 856 | simple = true; |
| 857 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 860 | fwdBuilder.withTreatment(tbuilder.build()); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 861 | log.debug("Trying to get a nextObjId for mpls rule on device:{} to ds:{}", |
| 862 | targetSw, ds); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 863 | DefaultGroupHandler gh = srManager.getGroupHandler(targetSw); |
| 864 | if (gh == null) { |
| 865 | log.warn("getNextObjectiveId query - groupHandler for device {} " |
| 866 | + "not found", targetSw); |
| 867 | return null; |
| 868 | } |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 869 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 870 | Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>(); |
| 871 | dstNextHops.put(destSw, nextHops); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 872 | int nextId = gh.getNextObjectiveId(ds, dstNextHops, meta, simple); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 873 | if (nextId <= 0) { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 874 | log.warn("No next objective in {} for ds: {}", targetSw, ds); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 875 | return null; |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 876 | } else { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 877 | log.debug("nextObjId found:{} for mpls rule on device:{} to ds:{}", |
| 878 | nextId, targetSw, ds); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 881 | fwdBuilder.nextStep(nextId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 882 | return fwdBuilder; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 886 | * Creates a filtering objective to permit all untagged packets with a |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 887 | * dstMac corresponding to the router's MAC address. For those pipelines |
| 888 | * that need to internally assign vlans to untagged packets, this method |
| 889 | * provides per-subnet vlan-ids as metadata. |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 890 | * <p> |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 891 | * Note that the vlan assignment and filter programming should only be done by |
| 892 | * the master for a switch. This method is typically called at deviceAdd and |
| 893 | * programs filters only for the enabled ports of the device. For port-updates, |
| 894 | * that enable/disable ports after device add, singlePortFilter methods should |
| 895 | * be called. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 896 | * |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 897 | * @param deviceId the switch dpid for the router |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 898 | * @return PortFilterInfo information about the processed ports |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 899 | */ |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 900 | PortFilterInfo populateVlanMacFilters(DeviceId deviceId) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 901 | log.debug("Installing per-port filtering objective for untagged " |
| 902 | + "packets in device {}", deviceId); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 903 | |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 904 | List<Port> devPorts = srManager.deviceService.getPorts(deviceId); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 905 | if (devPorts == null || devPorts.isEmpty()) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 906 | log.warn("Device {} ports not available. Unable to add MacVlan filters", |
| 907 | deviceId); |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 908 | return null; |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 909 | } |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 910 | int disabledPorts = 0, errorPorts = 0, filteredPorts = 0; |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 911 | for (Port port : devPorts) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 912 | if (!port.isEnabled()) { |
| 913 | disabledPorts++; |
| 914 | continue; |
| 915 | } |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 916 | if (processSinglePortFilters(deviceId, port.number(), true)) { |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 917 | filteredPorts++; |
| 918 | } else { |
| 919 | errorPorts++; |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 920 | } |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 921 | } |
Charles Chan | 7f9737b | 2017-06-22 14:27:17 -0700 | [diff] [blame] | 922 | log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}", |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 923 | deviceId, disabledPorts, errorPorts, filteredPorts); |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 924 | return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts, |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 925 | errorPorts, filteredPorts); |
| 926 | } |
| 927 | |
| 928 | /** |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 929 | * Creates or removes filtering objectives for a single port. Should only be |
| 930 | * called by the master for a switch. |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 931 | * |
| 932 | * @param deviceId device identifier |
| 933 | * @param portnum port identifier for port to be filtered |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 934 | * @param install true to install the filtering objective, false to remove |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 935 | * @return true if no errors occurred during the build of the filtering objective |
| 936 | */ |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 937 | boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 938 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum); |
| 939 | VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint); |
| 940 | Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint); |
| 941 | VlanId nativeVlan = srManager.getNativeVlanId(connectPoint); |
| 942 | |
| 943 | if (taggedVlans.size() != 0) { |
| 944 | // Filter for tagged vlans |
| 945 | if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId -> |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 946 | processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 947 | return false; |
| 948 | } |
| 949 | if (nativeVlan != null) { |
| 950 | // Filter for native vlan |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 951 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 952 | return false; |
| 953 | } |
| 954 | } |
| 955 | } else if (untaggedVlan != null) { |
| 956 | // Filter for untagged vlan |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 957 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 958 | return false; |
| 959 | } |
| 960 | } else { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 961 | // Unconfigured port, use INTERNAL_VLAN |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 962 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 963 | return false; |
| 964 | } |
| 965 | } |
| 966 | return true; |
| 967 | } |
| 968 | |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 969 | /** |
| 970 | * Updates filtering objectives for a single port. Should only be called by |
| 971 | * the master for a switch |
| 972 | * @param deviceId device identifier |
| 973 | * @param portNum port identifier for port to be filtered |
| 974 | * @param pushVlan true to push vlan, false otherwise |
| 975 | * @param vlanId vlan identifier |
| 976 | * @param install true to install the filtering objective, false to remove |
| 977 | */ |
| 978 | void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum, |
| 979 | boolean pushVlan, VlanId vlanId, boolean install) { |
| 980 | if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) { |
| 981 | log.warn("Failed to update FilteringObjective for {}/{} with vlan {}", |
| 982 | deviceId, portNum, vlanId); |
| 983 | } |
| 984 | } |
| 985 | |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 986 | private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum, |
| 987 | boolean pushVlan, VlanId vlanId, boolean install) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 988 | FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId); |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 989 | if (fob == null) { |
| 990 | // error encountered during build |
| 991 | return false; |
| 992 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 993 | log.debug("{} filtering objectives for dev/port: {}/{}", |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 994 | install ? "Installing" : "Removing", deviceId, portnum); |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 995 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 996 | (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum, |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 997 | install ? "installed" : "removed"), |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 998 | (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}", |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 999 | install ? "install" : "remove", deviceId, portnum, error)); |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 1000 | if (install) { |
| 1001 | srManager.flowObjectiveService.filter(deviceId, fob.add(context)); |
| 1002 | } else { |
| 1003 | srManager.flowObjectiveService.filter(deviceId, fob.remove(context)); |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1004 | } |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1005 | return true; |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1008 | private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum, |
| 1009 | boolean pushVlan, VlanId vlanId) { |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1010 | MacAddress deviceMac; |
| 1011 | try { |
| 1012 | deviceMac = config.getDeviceMac(deviceId); |
| 1013 | } catch (DeviceConfigNotFoundException e) { |
| 1014 | log.warn(e.getMessage() + " Processing SinglePortFilters aborted"); |
| 1015 | return null; |
| 1016 | } |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1017 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 1018 | fob.withKey(Criteria.matchInPort(portnum)) |
| 1019 | .addCondition(Criteria.matchEthDst(deviceMac)) |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1020 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1021 | |
Charles Chan | c550f2e | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 1022 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1023 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1024 | if (pushVlan) { |
| 1025 | fob.addCondition(Criteria.matchVlanId(VlanId.NONE)); |
Charles Chan | c550f2e | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 1026 | tBuilder.pushVlan().setVlanId(vlanId); |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1027 | } else { |
| 1028 | fob.addCondition(Criteria.matchVlanId(vlanId)); |
| 1029 | } |
| 1030 | |
Charles Chan | c550f2e | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 1031 | // NOTE: Some switch hardware share the same filtering flow among different ports. |
| 1032 | // We use this metadata to let the driver know that there is no more enabled port |
| 1033 | // within the same VLAN on this device. |
Charles Chan | dd33be5 | 2018-02-26 21:33:25 -0800 | [diff] [blame] | 1034 | if (noMoreEnabledPort(deviceId, vlanId)) { |
Charles Chan | c550f2e | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 1035 | tBuilder.wipeDeferred(); |
| 1036 | } |
| 1037 | |
| 1038 | fob.withMeta(tBuilder.build()); |
| 1039 | |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1040 | fob.permit().fromApp(srManager.appId); |
| 1041 | return fob; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1045 | * Creates a forwarding objective to punt all IP packets, destined to the |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 1046 | * router's port IP addresses, to the controller. Note that the input |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1047 | * port should not be matched on, as these packets can come from any input. |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 1048 | * Furthermore, these are applied only by the master instance. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1049 | * |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1050 | * @param deviceId the switch dpid for the router |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1051 | */ |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1052 | void populateIpPunts(DeviceId deviceId) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1053 | Ip4Address routerIpv4, pairRouterIpv4 = null; |
Charles Chan | 78ec505 | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 1054 | Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1055 | try { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1056 | routerIpv4 = config.getRouterIpv4(deviceId); |
| 1057 | routerIpv6 = config.getRouterIpv6(deviceId); |
Charles Chan | 78ec505 | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 1058 | routerLinkLocalIpv6 = Ip6Address.valueOf( |
| 1059 | IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes())); |
| 1060 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1061 | if (config.isPairedEdge(deviceId)) { |
| 1062 | pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId)); |
| 1063 | pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId)); |
| 1064 | } |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1065 | } catch (DeviceConfigNotFoundException e) { |
Charles Chan | f6ec153 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 1066 | log.warn(e.getMessage() + " Aborting populateIpPunts."); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1067 | return; |
| 1068 | } |
| 1069 | |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 1070 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 1071 | log.debug("Not installing port-IP punts - not the master for dev:{} ", |
| 1072 | deviceId); |
| 1073 | return; |
| 1074 | } |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1075 | Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId)); |
| 1076 | allIps.add(routerIpv4); |
Charles Chan | 78ec505 | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 1077 | allIps.add(routerLinkLocalIpv6); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1078 | if (routerIpv6 != null) { |
| 1079 | allIps.add(routerIpv6); |
| 1080 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1081 | if (pairRouterIpv4 != null) { |
| 1082 | allIps.add(pairRouterIpv4); |
| 1083 | } |
| 1084 | if (pairRouterIpv6 != null) { |
| 1085 | allIps.add(pairRouterIpv6); |
| 1086 | } |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1087 | for (IpAddress ipaddr : allIps) { |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1088 | populateSingleIpPunts(deviceId, ipaddr); |
| 1089 | } |
| 1090 | } |
Charles Chan | 2df0e8a | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 1091 | |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1092 | /** |
| 1093 | * Creates a forwarding objective to punt all IP packets, destined to the |
| 1094 | * specified IP address, which should be router's port IP address. |
| 1095 | * |
| 1096 | * @param deviceId the switch dpid for the router |
| 1097 | * @param ipAddress the IP address of the router's port |
| 1098 | */ |
| 1099 | void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) { |
| 1100 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress); |
| 1101 | Optional<DeviceId> optDeviceId = Optional.of(deviceId); |
| 1102 | |
| 1103 | srManager.packetService.requestPackets(sbuilder.build(), |
| 1104 | PacketPriority.CONTROL, srManager.appId, optDeviceId); |
| 1105 | } |
| 1106 | |
| 1107 | /** |
| 1108 | * Removes a forwarding objective to punt all IP packets, destined to the |
| 1109 | * specified IP address, which should be router's port IP address. |
| 1110 | * |
| 1111 | * @param deviceId the switch dpid for the router |
| 1112 | * @param ipAddress the IP address of the router's port |
| 1113 | */ |
| 1114 | void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) { |
| 1115 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress); |
| 1116 | Optional<DeviceId> optDeviceId = Optional.of(deviceId); |
| 1117 | |
| 1118 | try { |
| 1119 | if (!ipAddress.equals(config.getRouterIpv4(deviceId)) && |
| 1120 | !ipAddress.equals(config.getRouterIpv6(deviceId))) { |
| 1121 | srManager.packetService.cancelPackets(sbuilder.build(), |
| 1122 | PacketPriority.CONTROL, srManager.appId, optDeviceId); |
| 1123 | } |
| 1124 | } catch (DeviceConfigNotFoundException e) { |
| 1125 | log.warn(e.getMessage() + " Aborting revokeSingleIpPunts"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1126 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1129 | /** |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1130 | * Method to build IPv4 or IPv6 selector. |
| 1131 | * |
| 1132 | * @param addressToMatch the address to match |
| 1133 | */ |
| 1134 | private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) { |
| 1135 | return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix()); |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * Method to build IPv4 or IPv6 selector. |
| 1140 | * |
| 1141 | * @param prefixToMatch the prefix to match |
| 1142 | */ |
| 1143 | private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) { |
| 1144 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1145 | // If the prefix is IPv4 |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1146 | if (prefixToMatch.isIp4()) { |
| 1147 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 1148 | selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix()); |
| 1149 | return selectorBuilder; |
| 1150 | } |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1151 | // If the prefix is IPv6 |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1152 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 1153 | selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix()); |
| 1154 | return selectorBuilder; |
| 1155 | } |
| 1156 | |
| 1157 | /** |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1158 | * Creates forwarding objectives to punt ARP and NDP packets, to the controller. |
| 1159 | * Furthermore, these are applied only by the master instance. Deferred actions |
| 1160 | * are not cleared such that packets can be flooded in the cross connect use case |
| 1161 | * |
| 1162 | * @param deviceId the switch dpid for the router |
| 1163 | */ |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1164 | void populateArpNdpPunts(DeviceId deviceId) { |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1165 | // We are not the master just skip. |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1166 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 1167 | log.debug("Not installing ARP/NDP punts - not the master for dev:{} ", |
| 1168 | deviceId); |
| 1169 | return; |
| 1170 | } |
| 1171 | |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1172 | ForwardingObjective fwdObj; |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1173 | // We punt all ARP packets towards the controller. |
Charles Chan | 78ec505 | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 1174 | fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY) |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1175 | .add(new ObjectiveContext() { |
| 1176 | @Override |
| 1177 | public void onError(Objective objective, ObjectiveError error) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1178 | log.warn("Failed to install forwarding objective to punt ARP to {}: {}", |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1179 | deviceId, error); |
| 1180 | } |
| 1181 | }); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1182 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1183 | |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1184 | // We punt all NDP packets towards the controller. |
Charles Chan | 09bf269 | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 1185 | ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> { |
| 1186 | ForwardingObjective obj = builder.add(new ObjectiveContext() { |
| 1187 | @Override |
| 1188 | public void onError(Objective objective, ObjectiveError error) { |
| 1189 | log.warn("Failed to install forwarding objective to punt NDP to {}: {}", |
| 1190 | deviceId, error); |
| 1191 | } |
| 1192 | }); |
| 1193 | srManager.flowObjectiveService.forward(deviceId, obj); |
| 1194 | }); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1195 | |
| 1196 | srManager.getPairLocalPorts(deviceId).ifPresent(port -> { |
| 1197 | ForwardingObjective pairFwdObj; |
| 1198 | // Do not punt ARP packets from pair port |
| 1199 | pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1) |
| 1200 | .add(new ObjectiveContext() { |
| 1201 | @Override |
| 1202 | public void onError(Objective objective, ObjectiveError error) { |
| 1203 | log.warn("Failed to install forwarding objective to ignore ARP to {}: {}", |
| 1204 | deviceId, error); |
| 1205 | } |
| 1206 | }); |
| 1207 | srManager.flowObjectiveService.forward(deviceId, pairFwdObj); |
| 1208 | |
| 1209 | // Do not punt NDP packets from pair port |
Charles Chan | 09bf269 | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 1210 | ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> { |
| 1211 | ForwardingObjective obj = builder.add(new ObjectiveContext() { |
| 1212 | @Override |
| 1213 | public void onError(Objective objective, ObjectiveError error) { |
| 1214 | log.warn("Failed to install forwarding objective to ignore ARP to {}: {}", |
| 1215 | deviceId, error); |
| 1216 | } |
| 1217 | }); |
| 1218 | srManager.flowObjectiveService.forward(deviceId, obj); |
| 1219 | }); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1220 | |
| 1221 | // Do not forward DAD packets from pair port |
| 1222 | pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2) |
| 1223 | .add(new ObjectiveContext() { |
| 1224 | @Override |
| 1225 | public void onError(Objective objective, ObjectiveError error) { |
| 1226 | log.warn("Failed to install forwarding objective to drop DAD to {}: {}", |
| 1227 | deviceId, error); |
| 1228 | } |
| 1229 | }); |
| 1230 | srManager.flowObjectiveService.forward(deviceId, pairFwdObj); |
| 1231 | }); |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1234 | private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector, |
| 1235 | TrafficTreatment treatment, int priority) { |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1236 | return DefaultForwardingObjective.builder() |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1237 | .withPriority(priority) |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1238 | .withSelector(selector) |
| 1239 | .fromApp(srManager.appId) |
| 1240 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1241 | .withTreatment(treatment) |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1242 | .makePermanent(); |
| 1243 | } |
| 1244 | |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1245 | private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) { |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1246 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 1247 | sBuilder.matchEthType(TYPE_ARP); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1248 | if (port != null) { |
| 1249 | sBuilder.matchInPort(port); |
| 1250 | } |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1251 | |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1252 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1253 | if (punt) { |
| 1254 | tBuilder.punt(); |
| 1255 | } |
| 1256 | return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority); |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1257 | } |
| 1258 | |
Charles Chan | 09bf269 | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 1259 | private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) { |
| 1260 | Set<ForwardingObjective.Builder> result = Sets.newHashSet(); |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1261 | |
Charles Chan | 09bf269 | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 1262 | Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT) |
| 1263 | .forEach(type -> { |
| 1264 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 1265 | sBuilder.matchEthType(TYPE_IPV6) |
| 1266 | .matchIPProtocol(PROTOCOL_ICMP6) |
| 1267 | .matchIcmpv6Type(type); |
| 1268 | if (port != null) { |
| 1269 | sBuilder.matchInPort(port); |
| 1270 | } |
| 1271 | |
| 1272 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1273 | if (punt) { |
| 1274 | tBuilder.punt(); |
| 1275 | } |
| 1276 | |
| 1277 | result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority)); |
| 1278 | }); |
| 1279 | |
| 1280 | return result; |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) { |
| 1284 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 1285 | sBuilder.matchEthType(TYPE_IPV6) |
Charles Chan | c7e36aa | 2017-08-07 12:39:03 -0700 | [diff] [blame] | 1286 | .matchIPv6Src(Ip6Address.ZERO.toIpPrefix()); |
| 1287 | // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly |
| 1288 | // .matchIPProtocol(PROTOCOL_ICMP6) |
| 1289 | // .matchIcmpv6Type(NEIGHBOR_SOLICITATION); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1290 | if (port != null) { |
| 1291 | sBuilder.matchInPort(port); |
| 1292 | } |
| 1293 | |
| 1294 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1295 | tBuilder.wipeDeferred(); |
| 1296 | return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority); |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /** |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1300 | * Populates a forwarding objective to send packets that miss other high |
| 1301 | * priority Bridging Table entries to a group that contains all ports of |
| 1302 | * its subnet. |
| 1303 | * |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1304 | * @param deviceId switch ID to set the rules |
| 1305 | */ |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1306 | void populateSubnetBroadcastRule(DeviceId deviceId) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1307 | srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> { |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1308 | updateSubnetBroadcastRule(deviceId, vlanId, true); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1309 | }); |
| 1310 | } |
| 1311 | |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1312 | /** |
| 1313 | * Creates or removes a forwarding objective to broadcast packets to its subnet. |
| 1314 | * @param deviceId switch ID to set the rule |
| 1315 | * @param vlanId vlan ID to specify the subnet |
| 1316 | * @param install true to install the rule, false to revoke the rule |
| 1317 | */ |
| 1318 | void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) { |
| 1319 | int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId); |
| 1320 | |
| 1321 | if (nextId < 0) { |
| 1322 | log.error("Cannot install vlan {} broadcast rule in dev:{} due" |
| 1323 | + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId); |
| 1324 | return; |
| 1325 | } |
| 1326 | |
| 1327 | // Driver should treat objective with MacAddress.NONE as the |
| 1328 | // subnet broadcast rule |
| 1329 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 1330 | sbuilder.matchVlanId(vlanId); |
| 1331 | sbuilder.matchEthDst(MacAddress.NONE); |
| 1332 | |
| 1333 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 1334 | fob.withFlag(Flag.SPECIFIC) |
| 1335 | .withSelector(sbuilder.build()) |
| 1336 | .nextStep(nextId) |
| 1337 | .withPriority(SegmentRoutingService.FLOOD_PRIORITY) |
| 1338 | .fromApp(srManager.appId) |
| 1339 | .makePermanent(); |
| 1340 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1341 | (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId), |
| 1342 | (objective, error) -> |
| 1343 | log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error)); |
| 1344 | |
| 1345 | if (install) { |
| 1346 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
| 1347 | } else { |
| 1348 | srManager.flowObjectiveService.forward(deviceId, fob.remove(context)); |
| 1349 | } |
| 1350 | } |
| 1351 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1352 | private int getPriorityFromPrefix(IpPrefix prefix) { |
| 1353 | return (prefix.isIp4()) ? |
| 1354 | 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY : |
| 1355 | 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY; |
Srikanth Vavilapalli | f3a8bc0 | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 1356 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1357 | |
| 1358 | /** |
| 1359 | * Update Forwarding objective for each host and IP address connected to given port. |
| 1360 | * And create corresponding Simple Next objective if it does not exist. |
| 1361 | * Applied only when populating Forwarding objective |
| 1362 | * @param deviceId switch ID to set the rule |
| 1363 | * @param portNumber port number |
| 1364 | * @param prefix IP prefix of the route |
| 1365 | * @param hostMac MAC address of the next hop |
| 1366 | * @param vlanId Vlan ID of the port |
| 1367 | * @param popVlan true to pop vlan tag in TrafficTreatment |
| 1368 | * @param install true to populate the forwarding objective, false to revoke |
| 1369 | */ |
| 1370 | void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac, |
| 1371 | VlanId vlanId, boolean popVlan, boolean install) { |
| 1372 | ForwardingObjective.Builder fob; |
| 1373 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix); |
| 1374 | MacAddress deviceMac; |
| 1375 | try { |
| 1376 | deviceMac = config.getDeviceMac(deviceId); |
| 1377 | } catch (DeviceConfigNotFoundException e) { |
| 1378 | log.warn(e.getMessage() + " Aborting updateFwdObj."); |
| 1379 | return; |
| 1380 | } |
| 1381 | |
| 1382 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 1383 | tbuilder.deferred() |
| 1384 | .setEthDst(hostMac) |
| 1385 | .setEthSrc(deviceMac) |
| 1386 | .setOutput(portNumber); |
| 1387 | |
| 1388 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 1389 | |
| 1390 | if (!popVlan) { |
| 1391 | tbuilder.setVlanId(vlanId); |
| 1392 | } else { |
| 1393 | mbuilder.matchVlanId(vlanId); |
| 1394 | } |
| 1395 | |
| 1396 | // if the objective is to revoke an existing rule, and for some reason |
| 1397 | // the next-objective does not exist, then a new one should not be created |
| 1398 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber, |
| 1399 | tbuilder.build(), mbuilder.build(), install); |
| 1400 | if (portNextObjId == -1) { |
| 1401 | // Warning log will come from getPortNextObjective method |
| 1402 | return; |
| 1403 | } |
| 1404 | |
| 1405 | fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build()) |
| 1406 | .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent() |
| 1407 | .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 1408 | |
| 1409 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1410 | (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"), |
| 1411 | (objective, error) -> |
| 1412 | log.warn("Failed to {} IP rule for route {}: {}", |
| 1413 | install ? "install" : "revoke", prefix, error)); |
| 1414 | srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context)); |
| 1415 | |
| 1416 | if (!install) { |
| 1417 | DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId); |
| 1418 | if (grpHandler == null) { |
| 1419 | log.warn("updateFwdObj: groupHandler for device {} not found", deviceId); |
| 1420 | } else { |
| 1421 | // Remove L3UG for the given port and host |
| 1422 | grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build()); |
| 1423 | } |
| 1424 | } |
| 1425 | } |
Charles Chan | dd33be5 | 2018-02-26 21:33:25 -0800 | [diff] [blame] | 1426 | |
| 1427 | /** |
| 1428 | * Checks if there is other enabled port within the given VLAN on the given device. |
| 1429 | * |
| 1430 | * @param deviceId device ID |
| 1431 | * @param vlanId VLAN ID |
| 1432 | * @return true if there is no more port enabled within the given VLAN on the given device |
| 1433 | */ |
| 1434 | boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) { |
| 1435 | Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream() |
| 1436 | .filter(Port::isEnabled) |
| 1437 | .map(port -> new ConnectPoint(port.element().id(), port.number())) |
| 1438 | .collect(Collectors.toSet()); |
| 1439 | |
| 1440 | return enabledPorts.stream().noneMatch(cp -> |
| 1441 | // Given vlanId is included in the vlan-tagged configuration |
| 1442 | srManager.getTaggedVlanId(cp).contains(vlanId) || |
| 1443 | // Given vlanId is INTERNAL_VLAN and the interface is not configured |
| 1444 | (srManager.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null && |
| 1445 | vlanId.equals(INTERNAL_VLAN)) || |
| 1446 | // interface is configured and either vlan-untagged or vlan-native matches given vlanId |
| 1447 | (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId)) |
| 1448 | ); |
| 1449 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1450 | } |