sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 80f11cb | 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 | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Charles Chan | 051490d | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 20 | import org.onlab.packet.EthType; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ethernet; |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IPv6; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Ip4Address; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ip6Address; |
| 25 | import org.onlab.packet.IpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onlab.packet.MacAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 28 | import org.onlab.packet.MplsLabel; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 29 | import org.onlab.packet.VlanId; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
Saurav Das | 9bf4958 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 31 | import org.onosproject.net.Device; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flowobjective.Objective; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flowobjective.ObjectiveError; |
Jonghwan Hyun | 671a7ab | 2018-04-30 09:27:21 -0700 | [diff] [blame] | 36 | import org.onosproject.net.intf.Interface; |
Charles Chan | 2d0bbcd | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 37 | import org.onosproject.net.packet.PacketPriority; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 38 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 39 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 40 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 41 | import org.onosproject.segmentrouting.grouphandler.DestinationSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import org.onosproject.net.DeviceId; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 43 | import org.onosproject.net.Port; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 44 | import org.onosproject.net.PortNumber; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 45 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 46 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flow.TrafficSelector; |
| 48 | import org.onosproject.net.flow.TrafficTreatment; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 49 | import org.onosproject.net.flow.criteria.Criteria; |
| 50 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 51 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 52 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 53 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 54 | import org.onosproject.net.flowobjective.ForwardingObjective.Builder; |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 55 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 56 | import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
| 59 | |
| 60 | import java.util.ArrayList; |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 61 | import java.util.Collection; |
| 62 | import java.util.Collections; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 63 | import java.util.HashMap; |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 64 | import java.util.HashSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 65 | import java.util.List; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 66 | import java.util.Map; |
Charles Chan | 2d0bbcd | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 67 | import java.util.Optional; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 68 | import java.util.Set; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 69 | import java.util.concurrent.atomic.AtomicLong; |
Charles Chan | f17f66b | 2018-02-26 21:33:25 -0800 | [diff] [blame] | 70 | import java.util.stream.Collectors; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 71 | |
| 72 | import static com.google.common.base.Preconditions.checkNotNull; |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 73 | import static org.onlab.packet.Ethernet.TYPE_ARP; |
| 74 | import static org.onlab.packet.Ethernet.TYPE_IPV6; |
Charles Chan | 051490d | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 75 | import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT; |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 76 | import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION; |
Charles Chan | 051490d | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 77 | import static org.onlab.packet.ICMP6.ROUTER_ADVERTISEMENT; |
| 78 | import static org.onlab.packet.ICMP6.ROUTER_SOLICITATION; |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 79 | import static org.onlab.packet.IPv6.PROTOCOL_ICMP6; |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 80 | import static org.onosproject.segmentrouting.SegmentRoutingService.DEFAULT_PRIORITY; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 81 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Populator of segment routing flow rules. |
| 84 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 85 | public class RoutingRulePopulator { |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 86 | private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class); |
| 87 | |
| 88 | private static final int ARP_NDP_PRIORITY = 30000; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 89 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 90 | private AtomicLong rulePopulationCounter; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 91 | private SegmentRoutingManager srManager; |
| 92 | private DeviceConfiguration config; |
psneha | 86e60d3 | 2019-03-26 06:31:41 -0400 | [diff] [blame] | 93 | private RouteSimplifierUtils routeSimplifierUtils; |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 94 | |
Andreas Pantelopoulos | 59bd97e | 2018-06-28 17:06:14 -0700 | [diff] [blame] | 95 | // used for signalling the driver to remove vlan table and tmac entry also |
| 96 | private static final long CLEANUP_DOUBLE_TAGGED_HOST_ENTRIES = 1; |
| 97 | private static final long DOUBLE_TAGGED_METADATA_MASK = 0xffffffffffffffffL; |
| 98 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Creates a RoutingRulePopulator object. |
| 101 | * |
Thomas Vachuska | 8a07509 | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 102 | * @param srManager segment routing manager reference |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 103 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 104 | RoutingRulePopulator(SegmentRoutingManager srManager) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 105 | this.srManager = srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 106 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 107 | this.rulePopulationCounter = new AtomicLong(0); |
psneha | 86e60d3 | 2019-03-26 06:31:41 -0400 | [diff] [blame] | 108 | this.routeSimplifierUtils = new RouteSimplifierUtils(srManager); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Resets the population counter. |
| 113 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 114 | void resetCounter() { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 115 | rulePopulationCounter.set(0); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the number of rules populated. |
Thomas Vachuska | 7cfc620 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 120 | * |
| 121 | * @return number of rules |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 122 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 123 | long getCounter() { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 124 | return rulePopulationCounter.get(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 128 | * Populate a bridging rule on given deviceId that matches given mac, given vlan and |
| 129 | * output to given port. |
| 130 | * |
| 131 | * @param deviceId device ID |
| 132 | * @param port port |
| 133 | * @param mac mac address |
| 134 | * @param vlanId VLAN ID |
| 135 | */ |
| 136 | void populateBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) { |
| 137 | ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, false); |
| 138 | if (fob == null) { |
| 139 | log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 5ff8a3c | 2018-04-07 16:51:09 -0700 | [diff] [blame] | 144 | (objective) -> log.debug("Brigding rule for {}/{} populated", mac, vlanId), |
| 145 | (objective, error) -> log.warn("Failed to populate bridging rule for {}/{}: {}", mac, vlanId, error)); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 146 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Revoke a bridging rule on given deviceId that matches given mac, given vlan and |
| 151 | * output to given port. |
| 152 | * |
| 153 | * @param deviceId device ID |
| 154 | * @param port port |
| 155 | * @param mac mac address |
| 156 | * @param vlanId VLAN ID |
| 157 | */ |
| 158 | void revokeBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) { |
| 159 | ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, true); |
| 160 | if (fob == null) { |
| 161 | log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 5ff8a3c | 2018-04-07 16:51:09 -0700 | [diff] [blame] | 166 | (objective) -> log.debug("Brigding rule for {}/{} revoked", mac, vlanId), |
| 167 | (objective, error) -> log.warn("Failed to revoke bridging rule for {}/{}: {}", mac, vlanId, error)); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 168 | srManager.flowObjectiveService.forward(deviceId, fob.remove(context)); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Generates a forwarding objective builder for bridging rules. |
| 173 | * <p> |
| 174 | * The forwarding objective bridges packets destined to a given MAC to |
| 175 | * given port on given device. |
| 176 | * |
| 177 | * @param deviceId Device that host attaches to |
| 178 | * @param mac MAC address of the host |
| 179 | * @param hostVlanId VLAN ID of the host |
| 180 | * @param outport Port that host attaches to |
| 181 | * @param revoke true if forwarding objective is meant to revoke forwarding rule |
| 182 | * @return Forwarding objective builder |
| 183 | */ |
| 184 | private ForwardingObjective.Builder bridgingFwdObjBuilder( |
| 185 | DeviceId deviceId, MacAddress mac, VlanId hostVlanId, PortNumber outport, boolean revoke) { |
| 186 | ConnectPoint connectPoint = new ConnectPoint(deviceId, outport); |
Charles Chan | 098ca20 | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 187 | VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint); |
| 188 | Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint); |
| 189 | VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 190 | |
| 191 | // Create host selector |
| 192 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 193 | sbuilder.matchEthDst(mac); |
| 194 | |
| 195 | // Create host treatment |
| 196 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 197 | tbuilder.immediate().setOutput(outport); |
| 198 | |
| 199 | // Create host meta |
| 200 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 201 | |
| 202 | // Adjust the selector, treatment and meta according to VLAN configuration |
| 203 | if (taggedVlans.contains(hostVlanId)) { |
| 204 | sbuilder.matchVlanId(hostVlanId); |
| 205 | mbuilder.matchVlanId(hostVlanId); |
| 206 | } else if (hostVlanId.equals(VlanId.NONE)) { |
| 207 | if (untaggedVlan != null) { |
| 208 | sbuilder.matchVlanId(untaggedVlan); |
| 209 | mbuilder.matchVlanId(untaggedVlan); |
| 210 | tbuilder.immediate().popVlan(); |
| 211 | } else if (nativeVlan != null) { |
| 212 | sbuilder.matchVlanId(nativeVlan); |
| 213 | mbuilder.matchVlanId(nativeVlan); |
| 214 | tbuilder.immediate().popVlan(); |
| 215 | } else { |
| 216 | log.warn("Untagged host {}/{} is not allowed on {} without untagged or native" + |
| 217 | "vlan config", mac, hostVlanId, connectPoint); |
| 218 | return null; |
| 219 | } |
| 220 | } else { |
| 221 | log.warn("Tagged host {}/{} is not allowed on {} without VLAN listed in tagged vlan", |
| 222 | mac, hostVlanId, connectPoint); |
| 223 | return null; |
| 224 | } |
| 225 | |
| 226 | // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed. |
| 227 | // If the objective is to revoke an existing rule, and for some reason |
| 228 | // the next-objective does not exist, then a new one should not be created |
| 229 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outport, |
| 230 | tbuilder.build(), mbuilder.build(), !revoke); |
| 231 | if (portNextObjId == -1) { |
| 232 | // Warning log will come from getPortNextObjective method |
| 233 | return null; |
| 234 | } |
| 235 | |
| 236 | return DefaultForwardingObjective.builder() |
| 237 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 238 | .withSelector(sbuilder.build()) |
| 239 | .nextStep(portNextObjId) |
| 240 | .withPriority(100) |
| 241 | .fromApp(srManager.appId) |
| 242 | .makePermanent(); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Populate or revoke a bridging rule on given deviceId that matches given vlanId, |
| 247 | * and hostMAC connected to given port, and output to given port only when |
| 248 | * vlan information is valid. |
| 249 | * |
| 250 | * @param deviceId device ID that host attaches to |
| 251 | * @param portNum port number that host attaches to |
| 252 | * @param hostMac mac address of the host connected to the switch port |
| 253 | * @param vlanId Vlan ID configured on the switch port |
| 254 | * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise |
| 255 | * @param install true to populate the objective, false to revoke |
| 256 | */ |
| 257 | // TODO Refactor. There are a lot of duplications between this method, populateBridging, |
| 258 | // revokeBridging and bridgingFwdObjBuilder. |
| 259 | void updateBridging(DeviceId deviceId, PortNumber portNum, MacAddress hostMac, |
| 260 | VlanId vlanId, boolean popVlan, boolean install) { |
| 261 | // Create host selector |
| 262 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 263 | sbuilder.matchEthDst(hostMac); |
| 264 | |
| 265 | // Create host meta |
| 266 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 267 | |
| 268 | sbuilder.matchVlanId(vlanId); |
| 269 | mbuilder.matchVlanId(vlanId); |
| 270 | |
| 271 | // Create host treatment |
| 272 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 273 | tbuilder.immediate().setOutput(portNum); |
| 274 | |
| 275 | if (popVlan) { |
| 276 | tbuilder.immediate().popVlan(); |
| 277 | } |
| 278 | |
| 279 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum, |
| 280 | tbuilder.build(), mbuilder.build(), install); |
| 281 | if (portNextObjId != -1) { |
| 282 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder() |
| 283 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 284 | .withSelector(sbuilder.build()) |
| 285 | .nextStep(portNextObjId) |
| 286 | .withPriority(100) |
| 287 | .fromApp(srManager.appId) |
| 288 | .makePermanent(); |
| 289 | |
| 290 | ObjectiveContext context = new DefaultObjectiveContext( |
| 291 | (objective) -> log.debug("Brigding rule for {}/{} {}", hostMac, vlanId, |
| 292 | install ? "populated" : "revoked"), |
| 293 | (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}", |
| 294 | install ? "populate" : "revoke", hostMac, vlanId, error)); |
| 295 | srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context)); |
| 296 | } else { |
| 297 | log.warn("Failed to retrieve next objective for {}/{}", hostMac, vlanId); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Populates IP rules for a route that has direct connection to the switch. |
| 303 | * This method should not be invoked directly without going through DefaultRoutingHandler. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 304 | * |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 305 | * @param deviceId device ID of the device that next hop attaches to |
| 306 | * @param prefix IP prefix of the route |
| 307 | * @param hostMac MAC address of the next hop |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 308 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 309 | * @param outPort port where the next hop attaches to |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 310 | * @param directHost host is of type direct or indirect |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 311 | */ |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 312 | void populateRoute(DeviceId deviceId, IpPrefix prefix, |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 313 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, boolean directHost) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 314 | log.debug("Populate direct routing entry for route {} at {}:{}", |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 315 | prefix, deviceId, outPort); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 316 | ForwardingObjective.Builder fwdBuilder; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 317 | try { |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 318 | fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 319 | hostVlanId, outPort, directHost, false); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 320 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 321 | log.warn(e.getMessage() + " Aborting direct populateRoute"); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 322 | return; |
| 323 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 324 | if (fwdBuilder == null) { |
Saurav Das | 368cf21 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 325 | log.warn("Aborting host routing table entry due " |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 326 | + "to error for dev:{} route:{}", deviceId, prefix); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 327 | return; |
| 328 | } |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 329 | |
| 330 | int nextId = fwdBuilder.add().nextId(); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 331 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 332 | (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}", |
| 333 | prefix, nextId), |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 334 | (objective, error) -> |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 335 | log.warn("Failed to populate direct routing rule for route {}: {}", |
Saurav Das | 368cf21 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 336 | prefix, error)); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 337 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context)); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 338 | rulePopulationCounter.incrementAndGet(); |
| 339 | } |
| 340 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 341 | /** |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 342 | * Removes IP rules for a route when the next hop is gone. |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 343 | * This method should not be invoked directly without going through DefaultRoutingHandler. |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 344 | * |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 345 | * @param deviceId device ID of the device that next hop attaches to |
| 346 | * @param prefix IP prefix of the route |
| 347 | * @param hostMac MAC address of the next hop |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 348 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 349 | * @param outPort port that next hop attaches to |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 350 | * @param directHost host is of type direct or indirect |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 351 | */ |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 352 | void revokeRoute(DeviceId deviceId, IpPrefix prefix, |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 353 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, boolean directHost) { |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 354 | log.debug("Revoke IP table entry for route {} at {}:{}", |
| 355 | prefix, deviceId, outPort); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 356 | ForwardingObjective.Builder fwdBuilder; |
| 357 | try { |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 358 | fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 359 | hostVlanId, outPort, directHost, true); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 360 | } catch (DeviceConfigNotFoundException e) { |
| 361 | log.warn(e.getMessage() + " Aborting revokeIpRuleForHost."); |
| 362 | return; |
| 363 | } |
Charles Chan | ea702b1 | 2016-11-30 11:55:05 -0800 | [diff] [blame] | 364 | if (fwdBuilder == null) { |
| 365 | log.warn("Aborting host routing table entries due " |
| 366 | + "to error for dev:{} route:{}", deviceId, prefix); |
| 367 | return; |
| 368 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 369 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 370 | (objective) -> log.debug("IP rule for route {} revoked", prefix), |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 371 | (objective, error) -> |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 372 | log.warn("Failed to revoke IP rule for route {}: {}", prefix, error)); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 373 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context)); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 376 | /** |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 377 | * Returns a forwarding objective builder for routing rules. |
| 378 | * <p> |
| 379 | * The forwarding objective routes packets destined to a given prefix to |
| 380 | * given port on given device with given destination MAC. |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 381 | * |
| 382 | * @param deviceId device ID |
| 383 | * @param prefix prefix that need to be routed |
| 384 | * @param hostMac MAC address of the nexthop |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 385 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 386 | * @param outPort port where the nexthop attaches to |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 387 | * @param revoke true if forwarding objective is meant to revoke forwarding rule |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 388 | * @param directHost host is direct or indirect |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 389 | * @return forwarding objective builder |
| 390 | * @throws DeviceConfigNotFoundException if given device is not configured |
| 391 | */ |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 392 | |
| 393 | private ForwardingObjective.Builder |
| 394 | |
| 395 | |
| 396 | routingFwdObjBuilder( |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 397 | DeviceId deviceId, IpPrefix prefix, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 398 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 399 | boolean directHost, boolean revoke) |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 400 | throws DeviceConfigNotFoundException { |
| 401 | MacAddress deviceMac; |
| 402 | deviceMac = config.getDeviceMac(deviceId); |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 403 | int nextObjId = -1; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 404 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 405 | ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort); |
Charles Chan | 098ca20 | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 406 | VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint); |
| 407 | Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint); |
| 408 | VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 409 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 410 | // Create route selector |
| 411 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix); |
| 412 | |
| 413 | // Create route treatment |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 414 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 415 | tbuilder.deferred() |
| 416 | .setEthDst(hostMac) |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 417 | .setEthSrc(deviceMac) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 418 | .setOutput(outPort); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 419 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 420 | // Create route meta |
| 421 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 422 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 423 | // Adjust the meta according to VLAN configuration |
| 424 | if (taggedVlans.contains(hostVlanId)) { |
| 425 | tbuilder.setVlanId(hostVlanId); |
| 426 | } else if (hostVlanId.equals(VlanId.NONE)) { |
| 427 | if (untaggedVlan != null) { |
| 428 | mbuilder.matchVlanId(untaggedVlan); |
| 429 | } else if (nativeVlan != null) { |
| 430 | mbuilder.matchVlanId(nativeVlan); |
| 431 | } else { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 432 | log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan", |
| 433 | hostMac, hostVlanId, connectPoint); |
| 434 | return null; |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 435 | } |
| 436 | } else { |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 437 | // Internally-assigned dummy VLAN id will be given as hostVlanId |
| 438 | // when destination is double-tagged. |
| 439 | VlanId vlanId = srManager.dummyVlanIdStore().get( |
| 440 | new DummyVlanIdStoreKey(connectPoint, prefix.address())); |
| 441 | if (vlanId != null && vlanId.equals(hostVlanId)) { |
| 442 | tbuilder.setVlanId(hostVlanId); |
| 443 | mbuilder.matchVlanId(VlanId.ANY); |
| 444 | } else { |
| 445 | log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed" |
| 446 | + " in tagged vlan", hostMac, hostVlanId, connectPoint); |
| 447 | return null; |
| 448 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 449 | } |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 450 | |
| 451 | if (directHost) { |
| 452 | // if the objective is to revoke an existing rule, and for some reason |
| 453 | // the next-objective does not exist, then a new one should not be created |
| 454 | nextObjId = srManager.getPortNextObjectiveId(deviceId, outPort, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 455 | tbuilder.build(), mbuilder.build(), !revoke); |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 456 | } else { |
| 457 | // if the objective is to revoke an existing rule, and for some reason |
| 458 | // the next-objective does not exist, then a new one should not be created |
| 459 | nextObjId = srManager.getMacVlanNextObjectiveId(deviceId, hostMac, hostVlanId, |
Ruchi Sahota | 0786932 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 460 | outPort, !revoke); |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 461 | } |
| 462 | if (nextObjId == -1) { |
| 463 | // Warning log will come from getMacVlanNextObjective method |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 464 | return null; |
| 465 | } |
| 466 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 467 | return DefaultForwardingObjective.builder() |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 468 | .withSelector(sbuilder.build()) |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 469 | .nextStep(nextObjId) |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 470 | .fromApp(srManager.appId).makePermanent() |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 471 | .withPriority(getPriorityFromPrefix(prefix)) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 472 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 476 | * Populates IP flow rules for all the given prefixes reachable from the |
| 477 | * destination switch(es). |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 478 | * |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 479 | * @param targetSw switch where rules are to be programmed |
| 480 | * @param subnets subnets/prefixes being added |
| 481 | * @param destSw1 destination switch where the prefixes are reachable |
| 482 | * @param destSw2 paired destination switch if one exists for the subnets/prefixes. |
| 483 | * Should be null if there is no paired destination switch (by config) |
| 484 | * or if the given prefixes are reachable only via destSw1 |
| 485 | * @param nextHops a map containing a set of next-hops for each destination switch. |
| 486 | * If destSw2 is not null, then this map must contain an |
| 487 | * entry for destSw2 with its next-hops from the targetSw |
| 488 | * (although the next-hop set may be empty in certain scenarios). |
| 489 | * If destSw2 is null, there should not be an entry in this |
| 490 | * map for destSw2. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 491 | * @return true if all rules are set successfully, false otherwise |
| 492 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 493 | boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets, |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 494 | DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) { |
psneha | 86e60d3 | 2019-03-26 06:31:41 -0400 | [diff] [blame] | 495 | // Get pair device of the target switch |
| 496 | Optional<DeviceId> pairDev = srManager.getPairDeviceId(targetSw); |
| 497 | // Route simplification will be off in case of the nexthop location at target switch is down |
| 498 | // (routing through spine case) |
| 499 | boolean routeSimplOff = pairDev.isPresent() && pairDev.get().equals(destSw1) && destSw2 == null; |
| 500 | // Iterates over the routes |
| 501 | // If route simplification is enabled |
| 502 | // If the target device is another leaf in the network |
| 503 | if (srManager.routeSimplification && !routeSimplOff) { |
| 504 | for (IpPrefix subnet : subnets) { |
| 505 | // Skip route programming on the target device |
| 506 | // If route simplification applies |
| 507 | if (routeSimplifierUtils.hasLeafExclusionEnabledForPrefix(subnet)) { |
| 508 | // XXX route simplification assumes that source of the traffic |
| 509 | // towards the nexthops are co-located with the nexthops. In different |
| 510 | // scenarios will not work properly. |
| 511 | continue; |
| 512 | } |
| 513 | // populate the route in the remaning scenarios |
| 514 | if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) { |
| 515 | return false; |
| 516 | } |
| 517 | } |
| 518 | } else { |
| 519 | // Populate IP flow rules for all the subnets. |
| 520 | for (IpPrefix subnet : subnets) { |
| 521 | if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) { |
| 522 | return false; |
| 523 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 526 | return true; |
| 527 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 528 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 529 | /** |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 530 | * Revokes IP flow rules for the subnets from given device. |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 531 | * |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 532 | * @param targetSw target switch from which the subnets need to be removed |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 533 | * @param subnets subnet being removed |
| 534 | * @return true if all rules are removed successfully, false otherwise |
| 535 | */ |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 536 | boolean revokeIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 537 | for (IpPrefix subnet : subnets) { |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 538 | if (!revokeIpRuleForRouter(targetSw, subnet)) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 542 | return true; |
| 543 | } |
| 544 | |
| 545 | /** |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 546 | * Populates IP flow rules for an IP prefix in the target device. The prefix |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 547 | * is reachable via destination device(s). |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 548 | * |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 549 | * @param targetSw target device ID to set the rules |
| 550 | * @param ipPrefix the IP prefix |
| 551 | * @param destSw1 destination switch where the prefixes are reachable |
| 552 | * @param destSw2 paired destination switch if one exists for the subnets/prefixes. |
| 553 | * Should be null if there is no paired destination switch (by config) |
| 554 | * or if the given prefixes are reachable only via destSw1 |
| 555 | * @param nextHops map of destination switches and their next-hops. |
| 556 | * Should only contain destination switches that are |
| 557 | * actually meant to be routed to. If destSw2 is null, there |
| 558 | * should not be an entry for destSw2 in this map. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 559 | * @return true if all rules are set successfully, false otherwise |
| 560 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 561 | private boolean populateIpRuleForRouter(DeviceId targetSw, |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 562 | IpPrefix ipPrefix, DeviceId destSw1, |
| 563 | DeviceId destSw2, |
| 564 | Map<DeviceId, Set<DeviceId>> nextHops) { |
| 565 | int segmentId1, segmentId2 = -1; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 566 | try { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 567 | if (ipPrefix.isIp4()) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 568 | segmentId1 = config.getIPv4SegmentId(destSw1); |
| 569 | if (destSw2 != null) { |
| 570 | segmentId2 = config.getIPv4SegmentId(destSw2); |
| 571 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 572 | } else { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 573 | segmentId1 = config.getIPv6SegmentId(destSw1); |
| 574 | if (destSw2 != null) { |
| 575 | segmentId2 = config.getIPv6SegmentId(destSw2); |
| 576 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 577 | } |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 578 | } catch (DeviceConfigNotFoundException e) { |
| 579 | log.warn(e.getMessage() + " Aborting populateIpRuleForRouter."); |
| 580 | return false; |
| 581 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 582 | |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 583 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 584 | TrafficSelector selector = sbuilder.build(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 585 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 586 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 587 | DestinationSet ds; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 588 | TrafficTreatment treatment; |
Andreas Pantelopoulos | b281ae2 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 589 | DestinationSet.DestinationSetType dsType; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 590 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 591 | if (destSw2 == null) { |
| 592 | // single dst - create destination set based on next-hop |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 593 | // If the next hop is the same as the final destination, then MPLS |
| 594 | // label is not set. |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 595 | Set<DeviceId> nhd1 = nextHops.get(destSw1); |
| 596 | if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) { |
| 597 | tbuilder.immediate().decNwTtl(); |
Andreas Pantelopoulos | b281ae2 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 598 | ds = DestinationSet.createTypePushNone(destSw1); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 599 | treatment = tbuilder.build(); |
| 600 | } else { |
Andreas Pantelopoulos | b281ae2 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 601 | ds = DestinationSet.createTypePushBos(segmentId1, destSw1); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 602 | treatment = null; |
| 603 | } |
| 604 | } else { |
| 605 | // dst pair - IP rules for dst-pairs are always from other edge nodes |
| 606 | // the destination set needs to have both destinations, even if there |
| 607 | // are no next hops to one of them |
Andreas Pantelopoulos | b281ae2 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 608 | ds = DestinationSet.createTypePushBos(segmentId1, destSw1, segmentId2, destSw2); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 609 | treatment = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 612 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 613 | // if needed by the switch pipeline. Since neighbor sets are always to |
| 614 | // other neighboring routers, there is no subnet assigned on those ports. |
| 615 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector); |
Saurav Das | 9bf4958 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 616 | metabuilder.matchVlanId(srManager.getDefaultInternalVlan()); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 617 | DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 618 | if (grpHandler == null) { |
| 619 | log.warn("populateIPRuleForRouter: groupHandler for device {} " |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 620 | + "not found", targetSw); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 621 | return false; |
| 622 | } |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 623 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 624 | int nextId = grpHandler.getNextObjectiveId(ds, nextHops, |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 625 | metabuilder.build(), false); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 626 | if (nextId <= 0) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 627 | log.warn("No next objective in {} for ds: {}", targetSw, ds); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 628 | return false; |
| 629 | } |
| 630 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 631 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 632 | .builder() |
| 633 | .fromApp(srManager.appId) |
| 634 | .makePermanent() |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 635 | .nextStep(nextId) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 636 | .withSelector(selector) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 637 | .withPriority(getPriorityFromPrefix(ipPrefix)) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 638 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 639 | if (treatment != null) { |
| 640 | fwdBuilder.withTreatment(treatment); |
| 641 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 642 | log.debug("Installing IPv4 forwarding objective for router IP/subnet {} " |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 643 | + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 644 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 645 | (objective) -> log.debug("IP rule for router {} populated in dev:{}", |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 646 | ipPrefix, targetSw), |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 647 | (objective, error) -> |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 648 | log.warn("Failed to populate IP rule for router {}: {} in dev:{}", |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 649 | ipPrefix, error, targetSw)); |
| 650 | srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 651 | rulePopulationCounter.incrementAndGet(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 652 | |
| 653 | return true; |
| 654 | } |
| 655 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 656 | /** |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 657 | * Revokes IP flow rules for the router IP address from given device. |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 658 | * |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 659 | * @param targetSw target switch from which the ipPrefix need to be removed |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 660 | * @param ipPrefix the IP address of the destination router |
| 661 | * @return true if all rules are removed successfully, false otherwise |
| 662 | */ |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 663 | private boolean revokeIpRuleForRouter(DeviceId targetSw, IpPrefix ipPrefix) { |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 664 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 665 | TrafficSelector selector = sbuilder.build(); |
| 666 | TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build(); |
| 667 | |
| 668 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 669 | .builder() |
| 670 | .fromApp(srManager.appId) |
| 671 | .makePermanent() |
| 672 | .withSelector(selector) |
| 673 | .withTreatment(dummyTreatment) |
| 674 | .withPriority(getPriorityFromPrefix(ipPrefix)) |
| 675 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 676 | |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 677 | ObjectiveContext context = new DefaultObjectiveContext( |
| 678 | (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, targetSw), |
| 679 | (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}", |
| 680 | ipPrefix, targetSw, error)); |
| 681 | srManager.flowObjectiveService.forward(targetSw, fwdBuilder.remove(context)); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 682 | |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | /** |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 687 | * Populates MPLS flow rules in the target device to point towards the |
| 688 | * destination device. |
| 689 | * |
| 690 | * @param targetSwId target device ID of the switch to set the rules |
| 691 | * @param destSwId destination switch device ID |
| 692 | * @param nextHops next hops switch ID list |
| 693 | * @param routerIp the router ip of the destination switch |
| 694 | * @return true if all rules are set successfully, false otherwise |
| 695 | */ |
| 696 | boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId, |
| 697 | Set<DeviceId> nextHops, IpAddress routerIp) { |
| 698 | int segmentId; |
| 699 | try { |
| 700 | if (routerIp.isIp4()) { |
| 701 | segmentId = config.getIPv4SegmentId(destSwId); |
| 702 | } else { |
| 703 | segmentId = config.getIPv6SegmentId(destSwId); |
| 704 | } |
| 705 | } catch (DeviceConfigNotFoundException e) { |
| 706 | log.warn(e.getMessage() + " Aborting populateMplsRule."); |
| 707 | return false; |
| 708 | } |
| 709 | |
| 710 | List<ForwardingObjective> fwdObjs = new ArrayList<>(); |
| 711 | Collection<ForwardingObjective> fwdObjsMpls; |
| 712 | // Generates the transit rules used by the standard "routing". |
| 713 | fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, |
| 714 | routerIp, true); |
| 715 | if (fwdObjsMpls.isEmpty()) { |
| 716 | return false; |
| 717 | } |
| 718 | fwdObjs.addAll(fwdObjsMpls); |
| 719 | |
| 720 | // Generates the transit rules used by the MPLS Pwaas. |
| 721 | int pwSrLabel; |
| 722 | try { |
| 723 | pwSrLabel = config.getPWRoutingLabel(destSwId); |
| 724 | } catch (DeviceConfigNotFoundException e) { |
| 725 | log.warn(e.getMessage() |
| 726 | + " Aborting populateMplsRule. No label for PseudoWire traffic."); |
| 727 | return false; |
| 728 | } |
| 729 | fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel, |
| 730 | routerIp, false); |
| 731 | if (fwdObjsMpls.isEmpty()) { |
| 732 | return false; |
| 733 | } |
| 734 | fwdObjs.addAll(fwdObjsMpls); |
| 735 | |
| 736 | for (ForwardingObjective fwdObj : fwdObjs) { |
| 737 | log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}", |
| 738 | fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId); |
| 739 | srManager.flowObjectiveService.forward(targetSwId, fwdObj); |
| 740 | rulePopulationCounter.incrementAndGet(); |
| 741 | } |
| 742 | |
| 743 | return true; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * Differentiates between popping and swapping labels when building an MPLS |
| 748 | * forwarding objective. |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 749 | * |
| 750 | * @param targetSwId the target sw |
| 751 | * @param destSwId the destination sw |
| 752 | * @param nextHops the set of next hops |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 753 | * @param segmentId the segmentId to match representing the destination |
| 754 | * switch |
| 755 | * @param routerIp the router ip representing the destination switch |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 756 | * @return a collection of fwdobjective |
| 757 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 758 | private Collection<ForwardingObjective> handleMpls( |
| 759 | DeviceId targetSwId, |
| 760 | DeviceId destSwId, |
| 761 | Set<DeviceId> nextHops, |
| 762 | int segmentId, |
| 763 | IpAddress routerIp, |
| 764 | boolean isMplsBos) { |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 765 | |
| 766 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 767 | List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList(); |
Pier Luigi | 0be3f0f | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 768 | // For the transport of Pwaas we can use two or three MPLS label |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 769 | sbuilder.matchEthType(Ethernet.MPLS_UNICAST); |
| 770 | sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId)); |
| 771 | sbuilder.matchMplsBos(isMplsBos); |
| 772 | TrafficSelector selector = sbuilder.build(); |
| 773 | |
| 774 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 775 | // if needed by the switch pipeline. Since mpls next-hops are always to |
| 776 | // other neighboring routers, there is no subnet assigned on those ports. |
| 777 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector); |
Saurav Das | 9bf4958 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 778 | metabuilder.matchVlanId(srManager.getDefaultInternalVlan()); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 779 | |
| 780 | if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) { |
| 781 | // If the next hop is the destination router for the segment, do pop |
| 782 | log.debug("populateMplsRule: Installing MPLS forwarding objective for " |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 783 | + "label {} in switch {} with pop to next-hops {}", |
| 784 | segmentId, targetSwId, nextHops); |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 785 | ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 786 | getMplsForwardingObjective(targetSwId, |
| 787 | nextHops, |
| 788 | true, |
| 789 | isMplsBos, |
| 790 | metabuilder.build(), |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 791 | routerIp, |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 792 | segmentId, |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 793 | destSwId); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 794 | // Error case, we cannot handle, exit. |
| 795 | if (fwdObjNoBosBuilder == null) { |
| 796 | return Collections.emptyList(); |
| 797 | } |
| 798 | fwdObjBuilders.add(fwdObjNoBosBuilder); |
| 799 | |
| 800 | } else { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 801 | // next hop is not destination, irrespective of the number of next |
| 802 | // hops (1 or more) -- SR CONTINUE case (swap with self) |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 803 | log.debug("Installing MPLS forwarding objective for " |
| 804 | + "label {} in switch {} without pop to next-hops {}", |
| 805 | segmentId, targetSwId, nextHops); |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 806 | ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 807 | getMplsForwardingObjective(targetSwId, |
| 808 | nextHops, |
| 809 | false, |
| 810 | isMplsBos, |
| 811 | metabuilder.build(), |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 812 | routerIp, |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 813 | segmentId, |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 814 | destSwId); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 815 | // Error case, we cannot handle, exit. |
| 816 | if (fwdObjNoBosBuilder == null) { |
| 817 | return Collections.emptyList(); |
| 818 | } |
| 819 | fwdObjBuilders.add(fwdObjNoBosBuilder); |
| 820 | |
| 821 | } |
| 822 | |
| 823 | List<ForwardingObjective> fwdObjs = Lists.newArrayList(); |
| 824 | // We add the final property to the fwdObjs. |
| 825 | for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) { |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 826 | ((Builder) ((Builder) fwdObjBuilder |
| 827 | .fromApp(srManager.appId) |
| 828 | .makePermanent()) |
| 829 | .withSelector(selector) |
| 830 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)) |
| 831 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 832 | |
| 833 | ObjectiveContext context = new DefaultObjectiveContext( |
| 834 | (objective) -> |
| 835 | log.debug("MPLS rule {} for SID {} populated in dev:{} ", |
| 836 | objective.id(), segmentId, targetSwId), |
| 837 | (objective, error) -> |
| 838 | log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}", |
| 839 | objective.id(), segmentId, error, targetSwId)); |
| 840 | |
| 841 | ForwardingObjective fob = fwdObjBuilder.add(context); |
| 842 | fwdObjs.add(fob); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | return fwdObjs; |
| 846 | } |
| 847 | |
| 848 | /** |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 849 | * Returns a Forwarding Objective builder for the MPLS rule that references |
| 850 | * the desired Next Objective. Creates a DestinationSet that allows the |
| 851 | * groupHandler to create or find the required next objective. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 852 | * |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 853 | * @param targetSw the target sw |
| 854 | * @param nextHops the set of next hops |
| 855 | * @param phpRequired true if penultimate-hop-popping is required |
| 856 | * @param isBos true if matched label is bottom-of-stack |
| 857 | * @param meta metadata for creating next objective |
| 858 | * @param routerIp the router ip representing the destination switch |
| 859 | * @param destSw the destination sw |
| 860 | * @return the mpls forwarding objective builder |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 861 | */ |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 862 | private ForwardingObjective.Builder getMplsForwardingObjective( |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 863 | DeviceId targetSw, |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 864 | Set<DeviceId> nextHops, |
| 865 | boolean phpRequired, |
| 866 | boolean isBos, |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 867 | TrafficSelector meta, |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 868 | IpAddress routerIp, |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 869 | int segmentId, |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 870 | DeviceId destSw) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 871 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 872 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 873 | .builder().withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 874 | |
| 875 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 876 | DestinationSet ds = null; |
Andreas Pantelopoulos | b281ae2 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 877 | DestinationSet.DestinationSetType dstType = null; |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 878 | boolean simple = false; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 879 | if (phpRequired) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 880 | // php case - pop should always be flow-action |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 881 | log.debug("getMplsForwardingObjective: php required"); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 882 | tbuilder.deferred().copyTtlIn(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 883 | if (isBos) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 884 | if (routerIp.isIp4()) { |
| 885 | tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType()); |
| 886 | } else { |
| 887 | tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType()); |
| 888 | } |
| 889 | tbuilder.decNwTtl(); |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 890 | // standard case -> BoS == True; pop results in IP packet and forwarding |
| 891 | // is via an ECMP group |
Andreas Pantelopoulos | b281ae2 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 892 | ds = DestinationSet.createTypePopBos(destSw); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 893 | } else { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 894 | tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType()) |
| 895 | .decMplsTtl(); |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 896 | // double-label case -> BoS == False, pop results in MPLS packet |
| 897 | // depending on configuration we can ECMP this packet or choose one output |
Andreas Pantelopoulos | b281ae2 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 898 | ds = DestinationSet.createTypePopNotBos(destSw); |
| 899 | if (!srManager.getMplsEcmp()) { |
| 900 | simple = true; |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 901 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 902 | } |
| 903 | } else { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 904 | // swap with self case - SR CONTINUE |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 905 | log.debug("getMplsForwardingObjective: swap with self"); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 906 | tbuilder.deferred().decMplsTtl(); |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 907 | // swap results in MPLS packet with same BoS bit regardless of bit value |
| 908 | // depending on configuration we can ECMP this packet or choose one output |
Andreas Pantelopoulos | b281ae2 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 909 | // differentiate here between swap with not bos or swap with bos |
| 910 | ds = isBos ? DestinationSet.createTypeSwapBos(segmentId, destSw) : |
| 911 | DestinationSet.createTypeSwapNotBos(segmentId, destSw); |
| 912 | if (!srManager.getMplsEcmp()) { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 913 | simple = true; |
| 914 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 917 | fwdBuilder.withTreatment(tbuilder.build()); |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 918 | log.debug("Trying to get a nextObjId for mpls rule on device:{} to ds:{}", |
| 919 | targetSw, ds); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 920 | DefaultGroupHandler gh = srManager.getGroupHandler(targetSw); |
| 921 | if (gh == null) { |
| 922 | log.warn("getNextObjectiveId query - groupHandler for device {} " |
| 923 | + "not found", targetSw); |
| 924 | return null; |
| 925 | } |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 926 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 927 | Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>(); |
| 928 | dstNextHops.put(destSw, nextHops); |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 929 | int nextId = gh.getNextObjectiveId(ds, dstNextHops, meta, simple); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 930 | if (nextId <= 0) { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 931 | log.warn("No next objective in {} for ds: {}", targetSw, ds); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 932 | return null; |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 933 | } else { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 934 | log.debug("nextObjId found:{} for mpls rule on device:{} to ds:{}", |
| 935 | nextId, targetSw, ds); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 938 | fwdBuilder.nextStep(nextId); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 939 | return fwdBuilder; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 943 | * Creates a filtering objective to permit all untagged packets with a |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 944 | * dstMac corresponding to the router's MAC address. For those pipelines |
| 945 | * that need to internally assign vlans to untagged packets, this method |
| 946 | * provides per-subnet vlan-ids as metadata. |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 947 | * <p> |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 948 | * Note that the vlan assignment and filter programming should only be done by |
| 949 | * the master for a switch. This method is typically called at deviceAdd and |
| 950 | * programs filters only for the enabled ports of the device. For port-updates, |
| 951 | * that enable/disable ports after device add, singlePortFilter methods should |
| 952 | * be called. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 953 | * |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 954 | * @param deviceId the switch dpid for the router |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 955 | * @return PortFilterInfo information about the processed ports |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 956 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 957 | PortFilterInfo populateVlanMacFilters(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 958 | log.debug("Installing per-port filtering objective for untagged " |
| 959 | + "packets in device {}", deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 960 | |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 961 | List<Port> devPorts = srManager.deviceService.getPorts(deviceId); |
Jon Hall | 31d8478 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 962 | if (devPorts == null || devPorts.isEmpty()) { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 963 | log.warn("Device {} ports not available. Unable to add MacVlan filters", |
| 964 | deviceId); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 965 | return null; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 966 | } |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 967 | int disabledPorts = 0, errorPorts = 0, filteredPorts = 0; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 968 | for (Port port : devPorts) { |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 969 | if (!port.isEnabled()) { |
| 970 | disabledPorts++; |
| 971 | continue; |
| 972 | } |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 973 | if (processSinglePortFilters(deviceId, port.number(), true)) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 974 | filteredPorts++; |
| 975 | } else { |
| 976 | errorPorts++; |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 977 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 978 | } |
Charles Chan | 077314e | 2017-06-22 14:27:17 -0700 | [diff] [blame] | 979 | log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}", |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 980 | deviceId, disabledPorts, errorPorts, filteredPorts); |
Charles Chan | 9d2dd55 | 2018-06-19 20:56:33 -0700 | [diff] [blame] | 981 | return new PortFilterInfo(disabledPorts, errorPorts, filteredPorts); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /** |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 985 | * Creates or removes filtering objectives for a single port. Should only be |
| 986 | * called by the master for a switch. |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 987 | * |
| 988 | * @param deviceId device identifier |
| 989 | * @param portnum port identifier for port to be filtered |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 990 | * @param install true to install the filtering objective, false to remove |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 991 | * @return true if no errors occurred during the build of the filtering objective |
| 992 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 993 | boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 994 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum); |
Charles Chan | 098ca20 | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 995 | VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint); |
| 996 | Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint); |
| 997 | VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint); |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 998 | |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 999 | // Do not configure filter for edge ports where double-tagged hosts are connected. |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1000 | if (taggedVlans.size() != 0) { |
| 1001 | // Filter for tagged vlans |
Charles Chan | 098ca20 | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 1002 | if (!srManager.interfaceService.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId -> |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 1003 | processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1004 | return false; |
| 1005 | } |
| 1006 | if (nativeVlan != null) { |
| 1007 | // Filter for native vlan |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 1008 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1009 | return false; |
| 1010 | } |
| 1011 | } |
| 1012 | } else if (untaggedVlan != null) { |
| 1013 | // Filter for untagged vlan |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 1014 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1015 | return false; |
| 1016 | } |
Jonghwan Hyun | 671a7ab | 2018-04-30 09:27:21 -0700 | [diff] [blame] | 1017 | } else if (!hasIPConfiguration(connectPoint)) { |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1018 | // Filter for unconfigured upstream port, using INTERNAL_VLAN |
Saurav Das | 9bf4958 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 1019 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, |
| 1020 | srManager.getDefaultInternalVlan(), |
| 1021 | install)) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1022 | return false; |
| 1023 | } |
Andreas Pantelopoulos | b281ae2 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 1024 | // Filter for receiveing pseudowire traffic |
Saurav Das | 9bf4958 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 1025 | if (!processSinglePortFiltersInternal(deviceId, portnum, false, |
| 1026 | srManager.getPwTransportVlan(), |
| 1027 | install)) { |
Andreas Pantelopoulos | b281ae2 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 1028 | return false; |
| 1029 | } |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1030 | } |
| 1031 | return true; |
| 1032 | } |
| 1033 | |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1034 | /** |
| 1035 | * Updates filtering objectives for a single port. Should only be called by |
| 1036 | * the master for a switch |
| 1037 | * @param deviceId device identifier |
| 1038 | * @param portNum port identifier for port to be filtered |
| 1039 | * @param pushVlan true to push vlan, false otherwise |
| 1040 | * @param vlanId vlan identifier |
| 1041 | * @param install true to install the filtering objective, false to remove |
| 1042 | */ |
| 1043 | void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum, |
| 1044 | boolean pushVlan, VlanId vlanId, boolean install) { |
| 1045 | if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) { |
| 1046 | log.warn("Failed to update FilteringObjective for {}/{} with vlan {}", |
| 1047 | deviceId, portNum, vlanId); |
| 1048 | } |
| 1049 | } |
| 1050 | |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 1051 | private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum, |
| 1052 | boolean pushVlan, VlanId vlanId, boolean install) { |
Daniel Ginsburg | 776789f | 2018-04-30 19:27:19 -0400 | [diff] [blame] | 1053 | boolean doTMAC = true; |
| 1054 | |
| 1055 | if (!pushVlan) { |
| 1056 | // Skip the tagged vlans belonging to an interface without an IP address |
| 1057 | Set<Interface> ifaces = srManager.interfaceService |
| 1058 | .getInterfacesByPort(new ConnectPoint(deviceId, portnum)) |
| 1059 | .stream() |
| 1060 | .filter(intf -> intf.vlanTagged().contains(vlanId) && intf.ipAddressesList().isEmpty()) |
| 1061 | .collect(Collectors.toSet()); |
| 1062 | if (!ifaces.isEmpty()) { |
| 1063 | log.debug("processSinglePortFiltersInternal: skipping TMAC for vlan {} at {}/{} - no IP", |
| 1064 | vlanId, deviceId, portnum); |
| 1065 | doTMAC = false; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId, doTMAC); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1070 | if (fob == null) { |
| 1071 | // error encountered during build |
| 1072 | return false; |
| 1073 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1074 | log.debug("{} filtering objectives for dev/port: {}/{}", |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1075 | install ? "Installing" : "Removing", deviceId, portnum); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1076 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 1077 | (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum, |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1078 | install ? "installed" : "removed"), |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 1079 | (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}", |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1080 | install ? "install" : "remove", deviceId, portnum, error)); |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 1081 | if (install) { |
| 1082 | srManager.flowObjectiveService.filter(deviceId, fob.add(context)); |
| 1083 | } else { |
| 1084 | srManager.flowObjectiveService.filter(deviceId, fob.remove(context)); |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1085 | } |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1086 | return true; |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1089 | private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum, |
Daniel Ginsburg | 776789f | 2018-04-30 19:27:19 -0400 | [diff] [blame] | 1090 | boolean pushVlan, VlanId vlanId, boolean doTMAC) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1091 | MacAddress deviceMac; |
| 1092 | try { |
| 1093 | deviceMac = config.getDeviceMac(deviceId); |
| 1094 | } catch (DeviceConfigNotFoundException e) { |
| 1095 | log.warn(e.getMessage() + " Processing SinglePortFilters aborted"); |
| 1096 | return null; |
| 1097 | } |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1098 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
Daniel Ginsburg | 776789f | 2018-04-30 19:27:19 -0400 | [diff] [blame] | 1099 | |
| 1100 | if (doTMAC) { |
| 1101 | fob.withKey(Criteria.matchInPort(portnum)) |
| 1102 | .addCondition(Criteria.matchEthDst(deviceMac)) |
| 1103 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
| 1104 | } else { |
| 1105 | fob.withKey(Criteria.matchInPort(portnum)) |
| 1106 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
| 1107 | } |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1108 | |
Charles Chan | 17ca220 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 1109 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1110 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1111 | if (pushVlan) { |
| 1112 | fob.addCondition(Criteria.matchVlanId(VlanId.NONE)); |
Charles Chan | 17ca220 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 1113 | tBuilder.pushVlan().setVlanId(vlanId); |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1114 | } else { |
| 1115 | fob.addCondition(Criteria.matchVlanId(vlanId)); |
| 1116 | } |
| 1117 | |
Charles Chan | 17ca220 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 1118 | // NOTE: Some switch hardware share the same filtering flow among different ports. |
| 1119 | // We use this metadata to let the driver know that there is no more enabled port |
| 1120 | // within the same VLAN on this device. |
Charles Chan | f17f66b | 2018-02-26 21:33:25 -0800 | [diff] [blame] | 1121 | if (noMoreEnabledPort(deviceId, vlanId)) { |
Charles Chan | 17ca220 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 1122 | tBuilder.wipeDeferred(); |
| 1123 | } |
| 1124 | |
| 1125 | fob.withMeta(tBuilder.build()); |
| 1126 | |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1127 | fob.permit().fromApp(srManager.appId); |
| 1128 | return fob; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /** |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1132 | * Creates or removes filtering objectives for a double-tagged host on a port. |
| 1133 | * |
| 1134 | * @param deviceId device identifier |
| 1135 | * @param portNum port identifier for port to be filtered |
| 1136 | * @param outerVlan outer VLAN ID |
| 1137 | * @param innerVlan inner VLAN ID |
| 1138 | * @param install true to install the filtering objective, false to remove |
| 1139 | */ |
| 1140 | void processDoubleTaggedFilter(DeviceId deviceId, PortNumber portNum, VlanId outerVlan, |
| 1141 | VlanId innerVlan, boolean install) { |
Andreas Pantelopoulos | 59bd97e | 2018-06-28 17:06:14 -0700 | [diff] [blame] | 1142 | FilteringObjective.Builder fob = buildDoubleTaggedFilteringObj(deviceId, portNum, outerVlan, innerVlan, false); |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1143 | if (fob == null) { |
| 1144 | // error encountered during build |
| 1145 | return; |
| 1146 | } |
| 1147 | log.debug("{} double-tagged filtering objectives for dev/port: {}/{}", |
| 1148 | install ? "Installing" : "Removing", deviceId, portNum); |
| 1149 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1150 | (objective) -> log.debug("Filter for {}/{} {}", deviceId, portNum, |
| 1151 | install ? "installed" : "removed"), |
| 1152 | (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}", |
| 1153 | install ? "install" : "remove", deviceId, portNum, error)); |
| 1154 | if (install) { |
| 1155 | srManager.flowObjectiveService.filter(deviceId, fob.add(context)); |
| 1156 | } else { |
| 1157 | srManager.flowObjectiveService.filter(deviceId, fob.remove(context)); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | private FilteringObjective.Builder buildDoubleTaggedFilteringObj(DeviceId deviceId, PortNumber portNum, |
Andreas Pantelopoulos | 59bd97e | 2018-06-28 17:06:14 -0700 | [diff] [blame] | 1162 | VlanId outerVlan, VlanId innerVlan, |
| 1163 | boolean cleanupDoubleTaggedRules) { |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1164 | MacAddress deviceMac; |
| 1165 | try { |
| 1166 | deviceMac = config.getDeviceMac(deviceId); |
| 1167 | } catch (DeviceConfigNotFoundException e) { |
| 1168 | log.warn(e.getMessage() + " Processing DoubleTaggedFilters aborted"); |
| 1169 | return null; |
| 1170 | } |
| 1171 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 1172 | // Outer vlan id match should be appeared before inner vlan id match. |
| 1173 | fob.withKey(Criteria.matchInPort(portNum)) |
| 1174 | .addCondition(Criteria.matchEthDst(deviceMac)) |
| 1175 | .addCondition(Criteria.matchVlanId(outerVlan)) |
Daniele Moro | 998f2df | 2019-07-12 17:58:54 -0700 | [diff] [blame^] | 1176 | .addCondition(Criteria.matchInnerVlanId(innerVlan)) |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1177 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
| 1178 | |
| 1179 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1180 | // Pop outer vlan |
| 1181 | tBuilder.popVlan(); |
| 1182 | |
Andreas Pantelopoulos | 59bd97e | 2018-06-28 17:06:14 -0700 | [diff] [blame] | 1183 | // special metadata for driver |
| 1184 | if (cleanupDoubleTaggedRules) { |
| 1185 | tBuilder.writeMetadata(CLEANUP_DOUBLE_TAGGED_HOST_ENTRIES, DOUBLE_TAGGED_METADATA_MASK); |
| 1186 | } else { |
| 1187 | tBuilder.writeMetadata(0, DOUBLE_TAGGED_METADATA_MASK); |
| 1188 | } |
| 1189 | |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1190 | // NOTE: Some switch hardware share the same filtering flow among different ports. |
| 1191 | // We use this metadata to let the driver know that there is no more enabled port |
| 1192 | // within the same VLAN on this device. |
| 1193 | if (noMoreEnabledPort(deviceId, outerVlan)) { |
| 1194 | tBuilder.wipeDeferred(); |
| 1195 | } |
| 1196 | |
| 1197 | fob.withMeta(tBuilder.build()); |
| 1198 | |
| 1199 | fob.permit().fromApp(srManager.appId); |
| 1200 | return fob; |
| 1201 | } |
| 1202 | |
| 1203 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1204 | * Creates a forwarding objective to punt all IP packets, destined to the |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 1205 | * router's port IP addresses, to the controller. Note that the input |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1206 | * port should not be matched on, as these packets can come from any input. |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 1207 | * Furthermore, these are applied only by the master instance. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1208 | * |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1209 | * @param deviceId the switch dpid for the router |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1210 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1211 | void populateIpPunts(DeviceId deviceId) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1212 | Ip4Address routerIpv4, pairRouterIpv4 = null; |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 1213 | Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1214 | try { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1215 | routerIpv4 = config.getRouterIpv4(deviceId); |
| 1216 | routerIpv6 = config.getRouterIpv6(deviceId); |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 1217 | routerLinkLocalIpv6 = Ip6Address.valueOf( |
| 1218 | IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes())); |
| 1219 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1220 | if (config.isPairedEdge(deviceId)) { |
| 1221 | pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId)); |
| 1222 | pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId)); |
| 1223 | } |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1224 | } catch (DeviceConfigNotFoundException e) { |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 1225 | log.warn(e.getMessage() + " Aborting populateIpPunts."); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1226 | return; |
| 1227 | } |
| 1228 | |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 1229 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 1230 | log.debug("Not installing port-IP punts - not the master for dev:{} ", |
| 1231 | deviceId); |
| 1232 | return; |
| 1233 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1234 | Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId)); |
| 1235 | allIps.add(routerIpv4); |
| 1236 | if (routerIpv6 != null) { |
| 1237 | allIps.add(routerIpv6); |
Charles Chan | f0f7b5c | 2018-08-29 14:55:53 -0700 | [diff] [blame] | 1238 | allIps.add(routerLinkLocalIpv6); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1239 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1240 | if (pairRouterIpv4 != null) { |
| 1241 | allIps.add(pairRouterIpv4); |
| 1242 | } |
| 1243 | if (pairRouterIpv6 != null) { |
| 1244 | allIps.add(pairRouterIpv6); |
| 1245 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1246 | for (IpAddress ipaddr : allIps) { |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1247 | populateSingleIpPunts(deviceId, ipaddr); |
| 1248 | } |
| 1249 | } |
Charles Chan | 2d0bbcd | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 1250 | |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1251 | /** |
| 1252 | * Creates a forwarding objective to punt all IP packets, destined to the |
| 1253 | * specified IP address, which should be router's port IP address. |
| 1254 | * |
| 1255 | * @param deviceId the switch dpid for the router |
| 1256 | * @param ipAddress the IP address of the router's port |
| 1257 | */ |
| 1258 | void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) { |
| 1259 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress); |
| 1260 | Optional<DeviceId> optDeviceId = Optional.of(deviceId); |
| 1261 | |
| 1262 | srManager.packetService.requestPackets(sbuilder.build(), |
| 1263 | PacketPriority.CONTROL, srManager.appId, optDeviceId); |
| 1264 | } |
| 1265 | |
| 1266 | /** |
| 1267 | * Removes a forwarding objective to punt all IP packets, destined to the |
| 1268 | * specified IP address, which should be router's port IP address. |
| 1269 | * |
| 1270 | * @param deviceId the switch dpid for the router |
| 1271 | * @param ipAddress the IP address of the router's port |
| 1272 | */ |
| 1273 | void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) { |
| 1274 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress); |
| 1275 | Optional<DeviceId> optDeviceId = Optional.of(deviceId); |
| 1276 | |
| 1277 | try { |
| 1278 | if (!ipAddress.equals(config.getRouterIpv4(deviceId)) && |
| 1279 | !ipAddress.equals(config.getRouterIpv6(deviceId))) { |
| 1280 | srManager.packetService.cancelPackets(sbuilder.build(), |
| 1281 | PacketPriority.CONTROL, srManager.appId, optDeviceId); |
| 1282 | } |
| 1283 | } catch (DeviceConfigNotFoundException e) { |
| 1284 | log.warn(e.getMessage() + " Aborting revokeSingleIpPunts"); |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1285 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1288 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1289 | * Method to build IPv4 or IPv6 selector. |
| 1290 | * |
| 1291 | * @param addressToMatch the address to match |
| 1292 | */ |
| 1293 | private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) { |
| 1294 | return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix()); |
| 1295 | } |
| 1296 | |
| 1297 | /** |
| 1298 | * Method to build IPv4 or IPv6 selector. |
| 1299 | * |
| 1300 | * @param prefixToMatch the prefix to match |
| 1301 | */ |
| 1302 | private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) { |
| 1303 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1304 | // If the prefix is IPv4 |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1305 | if (prefixToMatch.isIp4()) { |
| 1306 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 1307 | selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix()); |
| 1308 | return selectorBuilder; |
| 1309 | } |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1310 | // If the prefix is IPv6 |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1311 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 1312 | selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix()); |
| 1313 | return selectorBuilder; |
| 1314 | } |
| 1315 | |
| 1316 | /** |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1317 | * Creates forwarding objectives to punt ARP and NDP packets, to the controller. |
| 1318 | * Furthermore, these are applied only by the master instance. Deferred actions |
| 1319 | * are not cleared such that packets can be flooded in the cross connect use case |
| 1320 | * |
| 1321 | * @param deviceId the switch dpid for the router |
| 1322 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1323 | void populateArpNdpPunts(DeviceId deviceId) { |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1324 | // We are not the master just skip. |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1325 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 1326 | log.debug("Not installing ARP/NDP punts - not the master for dev:{} ", |
| 1327 | deviceId); |
| 1328 | return; |
| 1329 | } |
| 1330 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1331 | ForwardingObjective fwdObj; |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1332 | // We punt all ARP packets towards the controller. |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame] | 1333 | fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY) |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1334 | .add(new ObjectiveContext() { |
| 1335 | @Override |
| 1336 | public void onError(Objective objective, ObjectiveError error) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1337 | log.warn("Failed to install forwarding objective to punt ARP to {}: {}", |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1338 | deviceId, error); |
| 1339 | } |
| 1340 | }); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1341 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1342 | |
Charles Chan | f0f7b5c | 2018-08-29 14:55:53 -0700 | [diff] [blame] | 1343 | if (isIpv6Configured(deviceId)) { |
| 1344 | // We punt all NDP packets towards the controller. |
| 1345 | ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> { |
| 1346 | ForwardingObjective obj = builder.add(new ObjectiveContext() { |
| 1347 | @Override |
| 1348 | public void onError(Objective objective, ObjectiveError error) { |
| 1349 | log.warn("Failed to install forwarding objective to punt NDP to {}: {}", |
| 1350 | deviceId, error); |
| 1351 | } |
| 1352 | }); |
| 1353 | srManager.flowObjectiveService.forward(deviceId, obj); |
Charles Chan | 051490d | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 1354 | }); |
Charles Chan | f0f7b5c | 2018-08-29 14:55:53 -0700 | [diff] [blame] | 1355 | } |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1356 | |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 1357 | srManager.getPairLocalPort(deviceId).ifPresent(port -> { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1358 | ForwardingObjective pairFwdObj; |
| 1359 | // Do not punt ARP packets from pair port |
| 1360 | pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1) |
| 1361 | .add(new ObjectiveContext() { |
| 1362 | @Override |
| 1363 | public void onError(Objective objective, ObjectiveError error) { |
| 1364 | log.warn("Failed to install forwarding objective to ignore ARP to {}: {}", |
| 1365 | deviceId, error); |
| 1366 | } |
| 1367 | }); |
| 1368 | srManager.flowObjectiveService.forward(deviceId, pairFwdObj); |
| 1369 | |
Charles Chan | f0f7b5c | 2018-08-29 14:55:53 -0700 | [diff] [blame] | 1370 | if (isIpv6Configured(deviceId)) { |
| 1371 | // Do not punt NDP packets from pair port |
| 1372 | ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> { |
| 1373 | ForwardingObjective obj = builder.add(new ObjectiveContext() { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1374 | @Override |
| 1375 | public void onError(Objective objective, ObjectiveError error) { |
Charles Chan | f0f7b5c | 2018-08-29 14:55:53 -0700 | [diff] [blame] | 1376 | log.warn("Failed to install forwarding objective to ignore ARP to {}: {}", |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1377 | deviceId, error); |
| 1378 | } |
| 1379 | }); |
Charles Chan | f0f7b5c | 2018-08-29 14:55:53 -0700 | [diff] [blame] | 1380 | srManager.flowObjectiveService.forward(deviceId, obj); |
| 1381 | }); |
| 1382 | |
| 1383 | // Do not forward DAD packets from pair port |
| 1384 | pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2) |
| 1385 | .add(new ObjectiveContext() { |
| 1386 | @Override |
| 1387 | public void onError(Objective objective, ObjectiveError error) { |
| 1388 | log.warn("Failed to install forwarding objective to drop DAD to {}: {}", |
| 1389 | deviceId, error); |
| 1390 | } |
| 1391 | }); |
| 1392 | srManager.flowObjectiveService.forward(deviceId, pairFwdObj); |
| 1393 | } |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1394 | }); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1395 | } |
| 1396 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1397 | private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector, |
| 1398 | TrafficTreatment treatment, int priority) { |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1399 | return DefaultForwardingObjective.builder() |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1400 | .withPriority(priority) |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1401 | .withSelector(selector) |
| 1402 | .fromApp(srManager.appId) |
| 1403 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1404 | .withTreatment(treatment) |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1405 | .makePermanent(); |
| 1406 | } |
| 1407 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1408 | private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) { |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1409 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 1410 | sBuilder.matchEthType(TYPE_ARP); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1411 | if (port != null) { |
| 1412 | sBuilder.matchInPort(port); |
| 1413 | } |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1414 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1415 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1416 | if (punt) { |
| 1417 | tBuilder.punt(); |
| 1418 | } |
| 1419 | return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
Charles Chan | 051490d | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 1422 | private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) { |
| 1423 | Set<ForwardingObjective.Builder> result = Sets.newHashSet(); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1424 | |
Charles Chan | 051490d | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 1425 | Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT) |
| 1426 | .forEach(type -> { |
| 1427 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 1428 | sBuilder.matchEthType(TYPE_IPV6) |
| 1429 | .matchIPProtocol(PROTOCOL_ICMP6) |
| 1430 | .matchIcmpv6Type(type); |
| 1431 | if (port != null) { |
| 1432 | sBuilder.matchInPort(port); |
| 1433 | } |
| 1434 | |
| 1435 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1436 | if (punt) { |
| 1437 | tBuilder.punt(); |
| 1438 | } |
| 1439 | |
| 1440 | result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority)); |
| 1441 | }); |
| 1442 | |
| 1443 | return result; |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) { |
| 1447 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 1448 | sBuilder.matchEthType(TYPE_IPV6) |
Charles Chan | e6bda75 | 2017-08-07 12:39:03 -0700 | [diff] [blame] | 1449 | .matchIPv6Src(Ip6Address.ZERO.toIpPrefix()); |
| 1450 | // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly |
| 1451 | // .matchIPProtocol(PROTOCOL_ICMP6) |
| 1452 | // .matchIcmpv6Type(NEIGHBOR_SOLICITATION); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1453 | if (port != null) { |
| 1454 | sBuilder.matchInPort(port); |
| 1455 | } |
| 1456 | |
| 1457 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1458 | tBuilder.wipeDeferred(); |
| 1459 | return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | /** |
Charles Chan | 155ec44 | 2018-09-16 14:30:19 -0700 | [diff] [blame] | 1463 | * Block given prefix in routing table. |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1464 | * |
Andrea Campanella | 60ce222 | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 1465 | * @param address the address to block |
| 1466 | * @param deviceId switch ID to set the rules |
| 1467 | */ |
| 1468 | void populateDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address) { |
| 1469 | updateDefaultRouteBlackhole(deviceId, address, true); |
| 1470 | } |
| 1471 | |
| 1472 | /** |
Charles Chan | 155ec44 | 2018-09-16 14:30:19 -0700 | [diff] [blame] | 1473 | * Unblock given prefix in routing table. |
Andrea Campanella | 60ce222 | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 1474 | * |
| 1475 | * @param address the address to block |
| 1476 | * @param deviceId switch ID to set the rules |
| 1477 | */ |
| 1478 | void removeDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address) { |
| 1479 | updateDefaultRouteBlackhole(deviceId, address, false); |
| 1480 | } |
| 1481 | |
| 1482 | private void updateDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address, boolean install) { |
| 1483 | try { |
| 1484 | if (srManager.deviceConfiguration.isEdgeDevice(deviceId)) { |
| 1485 | |
| 1486 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 1487 | if (address.isIp4()) { |
| 1488 | sbuilder.matchIPDst(address); |
| 1489 | sbuilder.matchEthType(EthType.EtherType.IPV4.ethType().toShort()); |
| 1490 | } else { |
| 1491 | sbuilder.matchIPv6Dst(address); |
| 1492 | sbuilder.matchEthType(EthType.EtherType.IPV6.ethType().toShort()); |
| 1493 | } |
| 1494 | |
| 1495 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
Andrea Campanella | 60ce222 | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 1496 | tBuilder.wipeDeferred(); |
| 1497 | |
| 1498 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 1499 | fob.withFlag(Flag.SPECIFIC) |
| 1500 | .withSelector(sbuilder.build()) |
| 1501 | .withTreatment(tBuilder.build()) |
| 1502 | .withPriority(getPriorityFromPrefix(address)) |
| 1503 | .fromApp(srManager.appId) |
| 1504 | .makePermanent(); |
| 1505 | |
| 1506 | log.debug("{} blackhole forwarding objectives for dev: {}", |
| 1507 | install ? "Installing" : "Removing", deviceId); |
| 1508 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1509 | (objective) -> log.debug("Forward for {} {}", deviceId, |
| 1510 | install ? "installed" : "removed"), |
| 1511 | (objective, error) -> log.warn("Failed to {} forward for {}: {}", |
| 1512 | install ? "install" : "remove", deviceId, error)); |
| 1513 | if (install) { |
| 1514 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
| 1515 | } else { |
| 1516 | srManager.flowObjectiveService.forward(deviceId, fob.remove(context)); |
| 1517 | } |
| 1518 | } |
| 1519 | } catch (DeviceConfigNotFoundException e) { |
| 1520 | log.info("Not populating blackhole for un-configured device {}", deviceId); |
| 1521 | } |
| 1522 | |
| 1523 | } |
| 1524 | |
| 1525 | /** |
| 1526 | * Populates a forwarding objective to send packets that miss other high |
| 1527 | * priority Bridging Table entries to a group that contains all ports of |
| 1528 | * its subnet. |
| 1529 | * |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1530 | * @param deviceId switch ID to set the rules |
| 1531 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1532 | void populateSubnetBroadcastRule(DeviceId deviceId) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1533 | srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> { |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1534 | updateSubnetBroadcastRule(deviceId, vlanId, true); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1535 | }); |
| 1536 | } |
| 1537 | |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1538 | /** |
| 1539 | * Creates or removes a forwarding objective to broadcast packets to its subnet. |
| 1540 | * @param deviceId switch ID to set the rule |
| 1541 | * @param vlanId vlan ID to specify the subnet |
| 1542 | * @param install true to install the rule, false to revoke the rule |
| 1543 | */ |
| 1544 | void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) { |
| 1545 | int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId); |
| 1546 | |
| 1547 | if (nextId < 0) { |
| 1548 | log.error("Cannot install vlan {} broadcast rule in dev:{} due" |
| 1549 | + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId); |
| 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | // Driver should treat objective with MacAddress.NONE as the |
| 1554 | // subnet broadcast rule |
| 1555 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 1556 | sbuilder.matchVlanId(vlanId); |
| 1557 | sbuilder.matchEthDst(MacAddress.NONE); |
| 1558 | |
| 1559 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 1560 | fob.withFlag(Flag.SPECIFIC) |
| 1561 | .withSelector(sbuilder.build()) |
| 1562 | .nextStep(nextId) |
| 1563 | .withPriority(SegmentRoutingService.FLOOD_PRIORITY) |
| 1564 | .fromApp(srManager.appId) |
| 1565 | .makePermanent(); |
| 1566 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1567 | (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId), |
| 1568 | (objective, error) -> |
| 1569 | log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error)); |
| 1570 | |
| 1571 | if (install) { |
| 1572 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
| 1573 | } else { |
| 1574 | srManager.flowObjectiveService.forward(deviceId, fob.remove(context)); |
| 1575 | } |
| 1576 | } |
| 1577 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1578 | private int getPriorityFromPrefix(IpPrefix prefix) { |
| 1579 | return (prefix.isIp4()) ? |
| 1580 | 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY : |
| 1581 | 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY; |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 1582 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1583 | |
| 1584 | /** |
| 1585 | * Update Forwarding objective for each host and IP address connected to given port. |
| 1586 | * And create corresponding Simple Next objective if it does not exist. |
| 1587 | * Applied only when populating Forwarding objective |
| 1588 | * @param deviceId switch ID to set the rule |
| 1589 | * @param portNumber port number |
| 1590 | * @param prefix IP prefix of the route |
| 1591 | * @param hostMac MAC address of the next hop |
| 1592 | * @param vlanId Vlan ID of the port |
| 1593 | * @param popVlan true to pop vlan tag in TrafficTreatment |
| 1594 | * @param install true to populate the forwarding objective, false to revoke |
| 1595 | */ |
| 1596 | void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac, |
| 1597 | VlanId vlanId, boolean popVlan, boolean install) { |
| 1598 | ForwardingObjective.Builder fob; |
| 1599 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix); |
| 1600 | MacAddress deviceMac; |
| 1601 | try { |
| 1602 | deviceMac = config.getDeviceMac(deviceId); |
| 1603 | } catch (DeviceConfigNotFoundException e) { |
| 1604 | log.warn(e.getMessage() + " Aborting updateFwdObj."); |
| 1605 | return; |
| 1606 | } |
| 1607 | |
| 1608 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 1609 | tbuilder.deferred() |
| 1610 | .setEthDst(hostMac) |
| 1611 | .setEthSrc(deviceMac) |
| 1612 | .setOutput(portNumber); |
| 1613 | |
| 1614 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 1615 | |
| 1616 | if (!popVlan) { |
| 1617 | tbuilder.setVlanId(vlanId); |
| 1618 | } else { |
| 1619 | mbuilder.matchVlanId(vlanId); |
| 1620 | } |
| 1621 | |
| 1622 | // if the objective is to revoke an existing rule, and for some reason |
| 1623 | // the next-objective does not exist, then a new one should not be created |
| 1624 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber, |
| 1625 | tbuilder.build(), mbuilder.build(), install); |
| 1626 | if (portNextObjId == -1) { |
| 1627 | // Warning log will come from getPortNextObjective method |
| 1628 | return; |
| 1629 | } |
| 1630 | |
| 1631 | fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build()) |
| 1632 | .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent() |
| 1633 | .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 1634 | |
| 1635 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1636 | (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"), |
| 1637 | (objective, error) -> |
| 1638 | log.warn("Failed to {} IP rule for route {}: {}", |
| 1639 | install ? "install" : "revoke", prefix, error)); |
| 1640 | srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context)); |
| 1641 | |
| 1642 | if (!install) { |
| 1643 | DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId); |
| 1644 | if (grpHandler == null) { |
| 1645 | log.warn("updateFwdObj: groupHandler for device {} not found", deviceId); |
| 1646 | } else { |
| 1647 | // Remove L3UG for the given port and host |
| 1648 | grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build()); |
| 1649 | } |
| 1650 | } |
| 1651 | } |
Charles Chan | f17f66b | 2018-02-26 21:33:25 -0800 | [diff] [blame] | 1652 | |
| 1653 | /** |
| 1654 | * Checks if there is other enabled port within the given VLAN on the given device. |
| 1655 | * |
| 1656 | * @param deviceId device ID |
| 1657 | * @param vlanId VLAN ID |
| 1658 | * @return true if there is no more port enabled within the given VLAN on the given device |
| 1659 | */ |
| 1660 | boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) { |
| 1661 | Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream() |
| 1662 | .filter(Port::isEnabled) |
| 1663 | .map(port -> new ConnectPoint(port.element().id(), port.number())) |
| 1664 | .collect(Collectors.toSet()); |
| 1665 | |
| 1666 | return enabledPorts.stream().noneMatch(cp -> |
| 1667 | // Given vlanId is included in the vlan-tagged configuration |
Charles Chan | 098ca20 | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 1668 | srManager.interfaceService.getTaggedVlanId(cp).contains(vlanId) || |
Charles Chan | f17f66b | 2018-02-26 21:33:25 -0800 | [diff] [blame] | 1669 | // Given vlanId is INTERNAL_VLAN and the interface is not configured |
Charles Chan | 098ca20 | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 1670 | (srManager.interfaceService.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null && |
Saurav Das | 9bf4958 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 1671 | vlanId.equals(srManager.getDefaultInternalVlan())) || |
Charles Chan | f17f66b | 2018-02-26 21:33:25 -0800 | [diff] [blame] | 1672 | // interface is configured and either vlan-untagged or vlan-native matches given vlanId |
| 1673 | (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId)) |
| 1674 | ); |
| 1675 | } |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1676 | |
| 1677 | /** |
| 1678 | * Returns a forwarding objective builder for egress forwarding rules. |
| 1679 | * <p> |
| 1680 | * The forwarding objective installs flow rules to egress pipeline to push |
| 1681 | * two vlan headers with given inner, outer vlan ids and outer tpid. |
| 1682 | * |
| 1683 | * @param portNumber port where the next hop attaches to |
| 1684 | * @param dummyVlanId vlan ID of the packet to match |
| 1685 | * @param innerVlan inner vlan ID of the next hop |
| 1686 | * @param outerVlan outer vlan ID of the next hop |
| 1687 | * @param outerTpid outer TPID of the next hop |
| 1688 | * @return forwarding objective builder |
| 1689 | */ |
| 1690 | private ForwardingObjective.Builder egressFwdObjBuilder(PortNumber portNumber, VlanId dummyVlanId, |
| 1691 | VlanId innerVlan, VlanId outerVlan, EthType outerTpid) { |
| 1692 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 1693 | sbuilder.matchVlanId(dummyVlanId); |
| 1694 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 1695 | tbuilder.setOutput(portNumber).setVlanId(innerVlan); |
| 1696 | |
| 1697 | if (outerTpid.equals(EthType.EtherType.QINQ.ethType())) { |
| 1698 | tbuilder.pushVlan(outerTpid); |
| 1699 | } else { |
| 1700 | tbuilder.pushVlan(); |
| 1701 | } |
| 1702 | |
| 1703 | tbuilder.setVlanId(outerVlan); |
| 1704 | return DefaultForwardingObjective.builder() |
| 1705 | .withSelector(sbuilder.build()) |
| 1706 | .withTreatment(tbuilder.build()) |
| 1707 | .fromApp(srManager.appId) |
| 1708 | .makePermanent() |
| 1709 | .withPriority(DEFAULT_PRIORITY) |
| 1710 | .withFlag(ForwardingObjective.Flag.EGRESS); |
| 1711 | } |
| 1712 | |
| 1713 | /** |
| 1714 | * Populates IP rules for a route that has double-tagged next hop. |
| 1715 | * |
| 1716 | * @param deviceId device ID of the device that next hop attaches to |
| 1717 | * @param prefix IP prefix of the route |
| 1718 | * @param hostMac MAC address of the next hop |
| 1719 | * @param dummyVlan Dummy Vlan ID allocated for this route |
| 1720 | * @param innerVlan inner Vlan ID of the next hop |
| 1721 | * @param outerVlan outer Vlan ID of the next hop |
| 1722 | * @param outerTpid outer TPID of the next hop |
| 1723 | * @param outPort port where the next hop attaches to |
| 1724 | */ |
| 1725 | void populateDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac, VlanId dummyVlan, |
| 1726 | VlanId innerVlan, VlanId outerVlan, EthType outerTpid, PortNumber outPort) { |
| 1727 | ForwardingObjective.Builder fwdBuilder; |
| 1728 | log.debug("Populate direct routing entry for double-tagged host route {} at {}:{}", |
| 1729 | prefix, deviceId, outPort); |
| 1730 | |
| 1731 | ForwardingObjective.Builder egressFwdBuilder = egressFwdObjBuilder( |
| 1732 | outPort, dummyVlan, innerVlan, outerVlan, outerTpid); |
| 1733 | DefaultObjectiveContext egressFwdContext = new DefaultObjectiveContext( |
| 1734 | objective -> log.debug("Egress rule for IP {} is populated", prefix.address()), |
| 1735 | (objective, error) -> { |
| 1736 | log.warn("Failed to populate egress rule for IP {}: {}", prefix.address(), error); |
| 1737 | srManager.dummyVlanIdStore().remove(new DummyVlanIdStoreKey( |
| 1738 | new ConnectPoint(deviceId, outPort), prefix.address() |
| 1739 | )); |
| 1740 | }); |
| 1741 | try { |
Daniele Moro | dbf1a3a | 2019-07-02 17:25:25 -0700 | [diff] [blame] | 1742 | fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, dummyVlan, outPort, true, false); |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1743 | } catch (DeviceConfigNotFoundException e) { |
| 1744 | log.error(e.getMessage() + " Aborting populateDoubleTaggedRoute"); |
| 1745 | return; |
| 1746 | } |
| 1747 | if (fwdBuilder == null) { |
| 1748 | log.error("Aborting double-tagged host routing table entry due to error for dev:{} route:{}", |
| 1749 | deviceId, prefix); |
| 1750 | return; |
| 1751 | } |
| 1752 | |
| 1753 | // Egress forwarding objective should be installed after the nextObjective for the output port is installed. |
| 1754 | // Installation of routingFwdObj will ensure the installation of the nextObjective. |
| 1755 | int nextId = fwdBuilder.add().nextId(); |
| 1756 | DefaultObjectiveContext context = new DefaultObjectiveContext(objective -> { |
| 1757 | log.debug("Direct routing rule for double-tagged host route {} populated. nextId={}", prefix, nextId); |
| 1758 | srManager.flowObjectiveService.forward(deviceId, egressFwdBuilder.add(egressFwdContext)); |
| 1759 | }, (objective, error) -> |
| 1760 | log.warn("Failed to populate direct routing rule for double-tagged host route {}: {}", prefix, error) |
| 1761 | ); |
| 1762 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context)); |
| 1763 | rulePopulationCounter.incrementAndGet(); |
| 1764 | } |
| 1765 | |
| 1766 | /** |
| 1767 | * Revokes IP rules for a route that has double-tagged next hop. |
| 1768 | * |
| 1769 | * @param deviceId device ID of the device that next hop attaches to |
| 1770 | * @param prefix IP prefix of the route |
| 1771 | * @param hostMac MAC address of the next hop |
| 1772 | * @param hostVlan Vlan ID of the next hop |
| 1773 | * @param innerVlan inner Vlan ID of the next hop |
| 1774 | * @param outerVlan outer Vlan ID of the next hop |
| 1775 | * @param outerTpid outer TPID of the next hop |
| 1776 | * @param outPort port where the next hop attaches to |
| 1777 | */ |
| 1778 | void revokeDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac, |
| 1779 | VlanId hostVlan, VlanId innerVlan, VlanId outerVlan, |
| 1780 | EthType outerTpid, PortNumber outPort) { |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 1781 | revokeRoute(deviceId, prefix, hostMac, hostVlan, outPort, false); |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1782 | |
| 1783 | DummyVlanIdStoreKey key = new DummyVlanIdStoreKey( |
| 1784 | new ConnectPoint(deviceId, outPort), prefix.address()); |
| 1785 | VlanId dummyVlanId = srManager.dummyVlanIdStore().get(key); |
| 1786 | if (dummyVlanId == null) { |
| 1787 | log.warn("Failed to retrieve dummy VLAN ID for {}/{} and {}", |
| 1788 | deviceId, outPort, prefix.address()); |
| 1789 | return; |
| 1790 | } |
| 1791 | ForwardingObjective.Builder fob = egressFwdObjBuilder( |
| 1792 | outPort, dummyVlanId, innerVlan, outerVlan, outerTpid); |
| 1793 | DefaultObjectiveContext context = new DefaultObjectiveContext(objective -> { |
| 1794 | log.debug("Egress rule for IP {} revoked", prefix.address()); |
| 1795 | srManager.dummyVlanIdStore().remove(key); |
| 1796 | }, (objective, error) -> { |
| 1797 | log.warn("Failed to revoke egress rule for IP {}: {}", prefix.address(), error); |
| 1798 | }); |
| 1799 | srManager.flowObjectiveService.forward(deviceId, fob.remove(context)); |
| 1800 | } |
| 1801 | |
Jonghwan Hyun | 671a7ab | 2018-04-30 09:27:21 -0700 | [diff] [blame] | 1802 | /** |
| 1803 | * Checks whether the specified port has IP configuration or not. |
| 1804 | * |
| 1805 | * @param cp ConnectPoint to check the existance of IP configuration |
| 1806 | * @return true if the port has IP configuration; false otherwise. |
| 1807 | */ |
| 1808 | private boolean hasIPConfiguration(ConnectPoint cp) { |
| 1809 | Set<Interface> interfaces = srManager.interfaceService.getInterfacesByPort(cp); |
| 1810 | return interfaces.stream().anyMatch(intf -> intf.ipAddressesList().size() > 0); |
| 1811 | } |
Saurav Das | 9bf4958 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 1812 | |
| 1813 | /** |
| 1814 | * Updates filtering rules for unconfigured ports on all devices for which |
| 1815 | * this controller instance is master. |
| 1816 | * |
| 1817 | * @param pushVlan true if the filtering rule requires a push vlan action |
| 1818 | * @param oldVlanId the vlanId to be removed |
| 1819 | * @param newVlanId the vlanId to be added |
| 1820 | */ |
| 1821 | void updateSpecialVlanFilteringRules(boolean pushVlan, VlanId oldVlanId, |
| 1822 | VlanId newVlanId) { |
| 1823 | for (Device dev : srManager.deviceService.getAvailableDevices()) { |
| 1824 | if (srManager.mastershipService.isLocalMaster(dev.id())) { |
| 1825 | for (Port p : srManager.deviceService.getPorts(dev.id())) { |
| 1826 | if (!hasIPConfiguration(new ConnectPoint(dev.id(), p.number())) |
| 1827 | && p.isEnabled()) { |
| 1828 | updateSinglePortFilters(dev.id(), p.number(), pushVlan, |
| 1829 | oldVlanId, false); |
| 1830 | updateSinglePortFilters(dev.id(), p.number(), pushVlan, |
| 1831 | newVlanId, true); |
| 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | } |
| 1836 | } |
| 1837 | |
Charles Chan | f0f7b5c | 2018-08-29 14:55:53 -0700 | [diff] [blame] | 1838 | private boolean isIpv6Configured(DeviceId deviceId) { |
| 1839 | boolean isIpv6Configured; |
| 1840 | try { |
| 1841 | isIpv6Configured = (config.getRouterIpv6(deviceId) != null); |
| 1842 | } catch (DeviceConfigNotFoundException e) { |
| 1843 | isIpv6Configured = false; |
| 1844 | } |
| 1845 | return isIpv6Configured; |
| 1846 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1847 | } |