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