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