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