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