sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.segmentrouting; |
| 17 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 18 | import org.onlab.packet.EthType; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Ethernet; |
| 20 | import org.onlab.packet.Ip4Address; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ip4Prefix; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onlab.packet.MacAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | import org.onlab.packet.MplsLabel; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 25 | import org.onlab.packet.VlanId; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 27 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
| 28 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 29 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 30 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame^] | 31 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 32 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 34 | import org.onosproject.net.Port; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 35 | import org.onosproject.net.PortNumber; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 38 | import org.onosproject.net.flow.TrafficSelector; |
| 39 | import org.onosproject.net.flow.TrafficTreatment; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.criteria.Criteria; |
| 41 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 42 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 43 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 44 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 45 | import org.onosproject.net.flowobjective.ForwardingObjective.Builder; |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
| 49 | |
| 50 | import java.util.ArrayList; |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 51 | import java.util.HashSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 52 | import java.util.List; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 53 | import java.util.Map; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 54 | import java.util.Set; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 55 | import java.util.concurrent.atomic.AtomicLong; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 56 | |
| 57 | import static com.google.common.base.Preconditions.checkNotNull; |
| 58 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 59 | /** |
| 60 | * Populator of segment routing flow rules. |
| 61 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 62 | public class RoutingRulePopulator { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 63 | private static final Logger log = LoggerFactory |
| 64 | .getLogger(RoutingRulePopulator.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 65 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 66 | private AtomicLong rulePopulationCounter; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 67 | private SegmentRoutingManager srManager; |
| 68 | private DeviceConfiguration config; |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 69 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Creates a RoutingRulePopulator object. |
| 72 | * |
Thomas Vachuska | 8a07509 | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 73 | * @param srManager segment routing manager reference |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 74 | */ |
| 75 | public RoutingRulePopulator(SegmentRoutingManager srManager) { |
| 76 | this.srManager = srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 77 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 78 | this.rulePopulationCounter = new AtomicLong(0); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Resets the population counter. |
| 83 | */ |
| 84 | public void resetCounter() { |
| 85 | rulePopulationCounter.set(0); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the number of rules populated. |
Thomas Vachuska | 7cfc620 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 90 | * |
| 91 | * @return number of rules |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 92 | */ |
| 93 | public long getCounter() { |
| 94 | return rulePopulationCounter.get(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 98 | * Populates IP flow rules for specific hosts directly connected to the |
| 99 | * switch. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 100 | * |
| 101 | * @param deviceId switch ID to set the rules |
| 102 | * @param hostIp host IP address |
| 103 | * @param hostMac host MAC address |
| 104 | * @param outPort port where the host is connected |
| 105 | */ |
| 106 | public void populateIpRuleForHost(DeviceId deviceId, Ip4Address hostIp, |
| 107 | MacAddress hostMac, PortNumber outPort) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 108 | log.debug("Populate IP table entry for host {} at {}:{}", |
| 109 | hostIp, deviceId, outPort); |
| 110 | ForwardingObjective.Builder fwdBuilder; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 111 | try { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 112 | fwdBuilder = getForwardingObjectiveBuilder( |
| 113 | deviceId, hostIp, hostMac, outPort); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 114 | } catch (DeviceConfigNotFoundException e) { |
| 115 | log.warn(e.getMessage() + " Aborting populateIpRuleForHost."); |
| 116 | return; |
| 117 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 118 | if (fwdBuilder == null) { |
| 119 | log.warn("Aborting host routing table entries due " |
| 120 | + "to error for dev:{} host:{}", deviceId, hostIp); |
| 121 | return; |
| 122 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 123 | ObjectiveContext context = new DefaultObjectiveContext( |
| 124 | (objective) -> log.debug("IP rule for host {} populated", hostIp), |
| 125 | (objective, error) -> |
| 126 | log.warn("Failed to populate IP rule for host {}: {}", hostIp, error)); |
| 127 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context)); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 128 | rulePopulationCounter.incrementAndGet(); |
| 129 | } |
| 130 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 131 | /** |
| 132 | * Removes IP rules for host when the host is gone. |
| 133 | * |
| 134 | * @param deviceId device ID of the device that host attaches to |
| 135 | * @param hostIp IP address of the host |
| 136 | * @param hostMac MAC address of the host |
| 137 | * @param outPort port that host attaches to |
| 138 | */ |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 139 | public void revokeIpRuleForHost(DeviceId deviceId, Ip4Address hostIp, |
| 140 | MacAddress hostMac, PortNumber outPort) { |
| 141 | log.debug("Revoke IP table entry for host {} at {}:{}", |
| 142 | hostIp, deviceId, outPort); |
| 143 | ForwardingObjective.Builder fwdBuilder; |
| 144 | try { |
| 145 | fwdBuilder = getForwardingObjectiveBuilder( |
| 146 | deviceId, hostIp, hostMac, outPort); |
| 147 | } catch (DeviceConfigNotFoundException e) { |
| 148 | log.warn(e.getMessage() + " Aborting revokeIpRuleForHost."); |
| 149 | return; |
| 150 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 151 | ObjectiveContext context = new DefaultObjectiveContext( |
| 152 | (objective) -> log.debug("IP rule for host {} revoked", hostIp), |
| 153 | (objective, error) -> |
| 154 | log.warn("Failed to revoke IP rule for host {}: {}", hostIp, error)); |
| 155 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context)); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | private ForwardingObjective.Builder getForwardingObjectiveBuilder( |
| 159 | DeviceId deviceId, Ip4Address hostIp, |
| 160 | MacAddress hostMac, PortNumber outPort) |
| 161 | throws DeviceConfigNotFoundException { |
| 162 | MacAddress deviceMac; |
| 163 | deviceMac = config.getDeviceMac(deviceId); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 164 | int priority; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 165 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 166 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 167 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 168 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 169 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 170 | // Special case for default route |
| 171 | if (hostIp.isZero()) { |
| 172 | sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, 0)); |
| 173 | priority = SegmentRoutingService.MIN_IP_PRIORITY; |
| 174 | } else { |
| 175 | Ip4Prefix hostIpPrefix = Ip4Prefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH); |
| 176 | sbuilder.matchIPDst(hostIpPrefix); |
| 177 | priority = getPriorityFromPrefix(hostIpPrefix); |
| 178 | } |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 179 | TrafficSelector selector = sbuilder.build(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 180 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 181 | tbuilder.deferred() |
| 182 | .setEthDst(hostMac) |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 183 | .setEthSrc(deviceMac) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 184 | .setOutput(outPort); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 185 | TrafficTreatment treatment = tbuilder.build(); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 186 | |
| 187 | // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed. |
| 188 | // for switch pipelines that need it, provide outgoing vlan as metadata |
| 189 | VlanId outvlan = null; |
| 190 | Ip4Prefix subnet = srManager.deviceConfiguration.getPortSubnet(deviceId, outPort); |
| 191 | if (subnet == null) { |
| 192 | outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET); |
| 193 | } else { |
| 194 | outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet); |
| 195 | } |
| 196 | TrafficSelector meta = DefaultTrafficSelector.builder() |
| 197 | .matchVlanId(outvlan).build(); |
| 198 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort, |
| 199 | treatment, meta); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 200 | if (portNextObjId == -1) { |
| 201 | // warning log will come from getPortNextObjective method |
| 202 | return null; |
| 203 | } |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 204 | return DefaultForwardingObjective.builder() |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 205 | .withSelector(selector) |
| 206 | .nextStep(portNextObjId) |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 207 | .fromApp(srManager.appId).makePermanent() |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 208 | .withPriority(priority) |
| 209 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Populates IP flow rules for the subnets of the destination router. |
| 214 | * |
| 215 | * @param deviceId switch ID to set the rules |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 216 | * @param subnets subnet being added |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 217 | * @param destSw destination switch ID |
| 218 | * @param nextHops next hop switch ID list |
| 219 | * @return true if all rules are set successfully, false otherwise |
| 220 | */ |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 221 | public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<Ip4Prefix> subnets, |
| 222 | DeviceId destSw, Set<DeviceId> nextHops) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 223 | for (IpPrefix subnet : subnets) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 224 | if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) { |
| 225 | return false; |
| 226 | } |
| 227 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 228 | return true; |
| 229 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 230 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 231 | /** |
| 232 | * Revokes IP flow rules for the subnets. |
| 233 | * |
| 234 | * @param subnets subnet being removed |
| 235 | * @return true if all rules are removed successfully, false otherwise |
| 236 | */ |
| 237 | public boolean revokeIpRuleForSubnet(Set<Ip4Prefix> subnets) { |
| 238 | for (IpPrefix subnet : subnets) { |
| 239 | if (!revokeIpRuleForRouter(subnet)) { |
| 240 | return false; |
| 241 | } |
| 242 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 243 | return true; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Populates IP flow rules for the router IP address. |
| 248 | * |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 249 | * @param deviceId target device ID to set the rules |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 250 | * @param ipPrefix the IP address of the destination router |
| 251 | * @param destSw device ID of the destination router |
| 252 | * @param nextHops next hop switch ID list |
| 253 | * @return true if all rules are set successfully, false otherwise |
| 254 | */ |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 255 | public boolean populateIpRuleForRouter(DeviceId deviceId, |
| 256 | IpPrefix ipPrefix, DeviceId destSw, |
| 257 | Set<DeviceId> nextHops) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 258 | int segmentId; |
| 259 | try { |
| 260 | segmentId = config.getSegmentId(destSw); |
| 261 | } catch (DeviceConfigNotFoundException e) { |
| 262 | log.warn(e.getMessage() + " Aborting populateIpRuleForRouter."); |
| 263 | return false; |
| 264 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 265 | |
| 266 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
Charles Chan | 3ef31e7 | 2016-02-05 13:33:54 -0800 | [diff] [blame] | 267 | sbuilder.matchIPDst(ipPrefix); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 268 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 269 | TrafficSelector selector = sbuilder.build(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 270 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 271 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 272 | NeighborSet ns; |
| 273 | TrafficTreatment treatment; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 274 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 275 | // If the next hop is the same as the final destination, then MPLS label |
| 276 | // is not set. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 277 | if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 278 | tbuilder.immediate().decNwTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 279 | ns = new NeighborSet(nextHops); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 280 | treatment = tbuilder.build(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 281 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 282 | ns = new NeighborSet(nextHops, segmentId); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 283 | treatment = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 286 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 287 | // if needed by the switch pipeline. Since neighbor sets are always to |
| 288 | // other neighboring routers, there is no subnet assigned on those ports. |
| 289 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector); |
| 290 | metabuilder.matchVlanId( |
| 291 | VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)); |
| 292 | |
| 293 | int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build()); |
| 294 | if (nextId <= 0) { |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 295 | log.warn("No next objective in {} for ns: {}", deviceId, ns); |
| 296 | return false; |
| 297 | } |
| 298 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 299 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 300 | .builder() |
| 301 | .fromApp(srManager.appId) |
| 302 | .makePermanent() |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 303 | .nextStep(nextId) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 304 | .withSelector(selector) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 305 | .withPriority(getPriorityFromPrefix(ipPrefix)) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 306 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 307 | if (treatment != null) { |
| 308 | fwdBuilder.withTreatment(treatment); |
| 309 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 310 | log.debug("Installing IPv4 forwarding objective " |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 311 | + "for router IP/subnet {} in switch {}", |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 312 | ipPrefix, |
| 313 | deviceId); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 314 | ObjectiveContext context = new DefaultObjectiveContext( |
| 315 | (objective) -> log.debug("IP rule for router {} populated", ipPrefix), |
| 316 | (objective, error) -> |
| 317 | log.warn("Failed to populate IP rule for router {}: {}", ipPrefix, error)); |
| 318 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 319 | rulePopulationCounter.incrementAndGet(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 320 | |
| 321 | return true; |
| 322 | } |
| 323 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 324 | /** |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 325 | * Revokes IP flow rules for the router IP address. |
| 326 | * |
| 327 | * @param ipPrefix the IP address of the destination router |
| 328 | * @return true if all rules are removed successfully, false otherwise |
| 329 | */ |
| 330 | public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) { |
| 331 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 332 | sbuilder.matchIPDst(ipPrefix); |
| 333 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 334 | TrafficSelector selector = sbuilder.build(); |
| 335 | TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build(); |
| 336 | |
| 337 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 338 | .builder() |
| 339 | .fromApp(srManager.appId) |
| 340 | .makePermanent() |
| 341 | .withSelector(selector) |
| 342 | .withTreatment(dummyTreatment) |
| 343 | .withPriority(getPriorityFromPrefix(ipPrefix)) |
| 344 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 345 | |
| 346 | ObjectiveContext context = new DefaultObjectiveContext( |
| 347 | (objective) -> log.debug("IP rule for router {} revoked", ipPrefix), |
| 348 | (objective, error) -> |
| 349 | log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error)); |
| 350 | |
| 351 | srManager.deviceService.getAvailableDevices().forEach(device -> { |
| 352 | srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context)); |
| 353 | }); |
| 354 | |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | /** |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 359 | * Populates MPLS flow rules to all routers. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 360 | * |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 361 | * @param deviceId target device ID of the switch to set the rules |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 362 | * @param destSwId destination switch device ID |
| 363 | * @param nextHops next hops switch ID list |
| 364 | * @return true if all rules are set successfully, false otherwise |
| 365 | */ |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 366 | public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId, |
| 367 | Set<DeviceId> nextHops) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 368 | int segmentId; |
| 369 | try { |
| 370 | segmentId = config.getSegmentId(destSwId); |
| 371 | } catch (DeviceConfigNotFoundException e) { |
| 372 | log.warn(e.getMessage() + " Aborting populateMplsRule."); |
| 373 | return false; |
| 374 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 375 | |
| 376 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 377 | List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 378 | |
| 379 | // TODO Handle the case of Bos == false |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 380 | sbuilder.matchEthType(Ethernet.MPLS_UNICAST); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 381 | sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId)); |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 382 | sbuilder.matchMplsBos(true); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 383 | TrafficSelector selector = sbuilder.build(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 384 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 385 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 386 | // if needed by the switch pipeline. Since mpls next-hops are always to |
| 387 | // other neighboring routers, there is no subnet assigned on those ports. |
| 388 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector); |
| 389 | metabuilder.matchVlanId( |
| 390 | VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)); |
| 391 | |
| 392 | // If the next hop is the destination router for the segment, do pop |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 393 | if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 394 | log.debug("populateMplsRule: Installing MPLS forwarding objective for " |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 395 | + "label {} in switch {} with pop", segmentId, deviceId); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 396 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 397 | // bos pop case (php) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 398 | ForwardingObjective.Builder fwdObjBosBuilder = |
| 399 | getMplsForwardingObjective(deviceId, |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 400 | nextHops, |
| 401 | true, |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 402 | true, |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 403 | metabuilder.build()); |
| 404 | if (fwdObjBosBuilder == null) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 405 | return false; |
| 406 | } |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 407 | fwdObjBuilders.add(fwdObjBosBuilder); |
| 408 | |
| 409 | // XXX not-bos pop case, SR app multi-label not implemented yet |
| 410 | /*ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 411 | getMplsForwardingObjective(deviceId, |
| 412 | nextHops, |
| 413 | true, |
| 414 | false);*/ |
| 415 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 416 | } else { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 417 | // next hop is not destination, SR CONTINUE case (swap with self) |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 418 | log.debug("Installing MPLS forwarding objective for " |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 419 | + "label {} in switch {} without pop", segmentId, deviceId); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 420 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 421 | // continue case with bos - this does get triggered in edge routers |
| 422 | // and in core routers - driver can handle depending on availability |
| 423 | // of MPLS ECMP or not |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 424 | ForwardingObjective.Builder fwdObjBosBuilder = |
| 425 | getMplsForwardingObjective(deviceId, |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 426 | nextHops, |
| 427 | false, |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 428 | true, |
| 429 | metabuilder.build()); |
| 430 | if (fwdObjBosBuilder == null) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 431 | return false; |
| 432 | } |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 433 | fwdObjBuilders.add(fwdObjBosBuilder); |
| 434 | |
| 435 | // XXX continue case with not-bos - SR app multi label not implemented yet |
| 436 | // also requires MPLS ECMP |
| 437 | /*ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 438 | getMplsForwardingObjective(deviceId, |
| 439 | nextHops, |
| 440 | false, |
| 441 | false); */ |
| 442 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 445 | for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) { |
| 446 | ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId) |
| 447 | .makePermanent()).withSelector(selector) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 448 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 449 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 450 | ObjectiveContext context = new DefaultObjectiveContext( |
| 451 | (objective) -> log.debug("MPLS rule for SID {} populated", segmentId), |
| 452 | (objective, error) -> |
| 453 | log.warn("Failed to populate MPLS rule for SID {}: {}", segmentId, error)); |
| 454 | srManager.flowObjectiveService.forward(deviceId, fwdObjBuilder.add(context)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 455 | rulePopulationCounter.incrementAndGet(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | return true; |
| 459 | } |
| 460 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 461 | private ForwardingObjective.Builder getMplsForwardingObjective( |
| 462 | DeviceId deviceId, |
| 463 | Set<DeviceId> nextHops, |
| 464 | boolean phpRequired, |
| 465 | boolean isBos, |
| 466 | TrafficSelector meta) { |
| 467 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 468 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 469 | .builder().withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 470 | |
| 471 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 472 | |
| 473 | if (phpRequired) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 474 | // php case - pop should always be flow-action |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 475 | log.debug("getMplsForwardingObjective: php required"); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 476 | tbuilder.deferred().copyTtlIn(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 477 | if (isBos) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 478 | tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType()) |
| 479 | .decNwTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 480 | } else { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 481 | tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType()) |
| 482 | .decMplsTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 483 | } |
| 484 | } else { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 485 | // swap with self case - SR CONTINUE |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 486 | log.debug("getMplsForwardingObjective: php not required"); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 487 | tbuilder.deferred().decMplsTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 490 | // All forwarding is via ECMP group, the metadata informs the driver |
| 491 | // that the next-Objective will be used by MPLS flows. In other words, |
| 492 | // MPLS ECMP is requested. It is up to the driver to decide if these |
| 493 | // packets will be hashed or not. |
| 494 | fwdBuilder.withTreatment(tbuilder.build()); |
| 495 | NeighborSet ns = new NeighborSet(nextHops); |
| 496 | log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}", |
| 497 | deviceId, ns); |
| 498 | |
| 499 | int nextId = srManager.getNextObjectiveId(deviceId, ns, meta); |
| 500 | if (nextId <= 0) { |
| 501 | log.warn("No next objective in {} for ns: {}", deviceId, ns); |
| 502 | return null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 505 | fwdBuilder.nextStep(nextId); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 506 | return fwdBuilder; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 510 | * Creates a filtering objective to permit all untagged packets with a |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 511 | * dstMac corresponding to the router's MAC address. For those pipelines |
| 512 | * that need to internally assign vlans to untagged packets, this method |
| 513 | * provides per-subnet vlan-ids as metadata. |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 514 | * <p> |
| 515 | * Note that the vlan assignment is only done by the master-instance for a switch. |
| 516 | * However we send the filtering objective from slave-instances as well, so |
| 517 | * that drivers can obtain other information (like Router MAC and IP). |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 518 | * |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 519 | * @param deviceId the switch dpid for the router |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 520 | * @return true if operation succeeds |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 521 | */ |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 522 | public boolean populateRouterMacVlanFilters(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 523 | log.debug("Installing per-port filtering objective for untagged " |
| 524 | + "packets in device {}", deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 525 | |
| 526 | MacAddress deviceMac; |
| 527 | try { |
| 528 | deviceMac = config.getDeviceMac(deviceId); |
| 529 | } catch (DeviceConfigNotFoundException e) { |
| 530 | log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters."); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 531 | return false; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 532 | } |
| 533 | |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 534 | List<Port> devPorts = srManager.deviceService.getPorts(deviceId); |
| 535 | if (devPorts != null && devPorts.size() == 0) { |
| 536 | log.warn("Device {} ports not available. Unable to add MacVlan filters", |
| 537 | deviceId); |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | for (Port port : devPorts) { |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 542 | ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number()); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 543 | // TODO: Handles dynamic port events when we are ready for dynamic config |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame^] | 544 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
| 545 | .getConfig(srManager.appId, SegmentRoutingAppConfig.class); |
| 546 | if ((appConfig == null || !appConfig.suppressSubnet().contains(connectPoint)) && |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 547 | port.isEnabled()) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 548 | Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number()); |
| 549 | VlanId assignedVlan = (portSubnet == null) |
| 550 | ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET) |
| 551 | : srManager.getSubnetAssignedVlanId(deviceId, portSubnet); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 552 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 553 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 554 | fob.withKey(Criteria.matchInPort(port.number())) |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 555 | .addCondition(Criteria.matchEthDst(deviceMac)) |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 556 | .addCondition(Criteria.matchVlanId(VlanId.NONE)) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 557 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 558 | // vlan assignment is valid only if this instance is master |
| 559 | if (srManager.mastershipService.isLocalMaster(deviceId)) { |
| 560 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
| 561 | .pushVlan().setVlanId(assignedVlan).build(); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 562 | fob.withMeta(tt); |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 563 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 564 | fob.permit().fromApp(srManager.appId); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 565 | log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 566 | ObjectiveContext context = new DefaultObjectiveContext( |
| 567 | (objective) -> log.debug("Filter for {} populated", connectPoint), |
| 568 | (objective, error) -> |
| 569 | log.warn("Failed to populate filter for {}: {}", connectPoint, error)); |
| 570 | srManager.flowObjectiveService.filter(deviceId, fob.add(context)); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 573 | return true; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 577 | * Creates a forwarding objective to punt all IP packets, destined to the |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 578 | * router's port IP addresses, to the controller. Note that the input |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 579 | * port should not be matched on, as these packets can come from any input. |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 580 | * Furthermore, these are applied only by the master instance. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 581 | * |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 582 | * @param deviceId the switch dpid for the router |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 583 | */ |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 584 | public void populateRouterIpPunts(DeviceId deviceId) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 585 | Ip4Address routerIp; |
| 586 | try { |
| 587 | routerIp = config.getRouterIp(deviceId); |
| 588 | } catch (DeviceConfigNotFoundException e) { |
| 589 | log.warn(e.getMessage() + " Aborting populateRouterIpPunts."); |
| 590 | return; |
| 591 | } |
| 592 | |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 593 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 594 | log.debug("Not installing port-IP punts - not the master for dev:{} ", |
| 595 | deviceId); |
| 596 | return; |
| 597 | } |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 598 | ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 599 | Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId)); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 600 | allIps.add(routerIp); |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 601 | for (Ip4Address ipaddr : allIps) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 602 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 603 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 604 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 605 | sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr, |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 606 | IpPrefix.MAX_INET_MASK_LENGTH)); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 607 | tbuilder.setOutput(PortNumber.CONTROLLER); |
| 608 | puntIp.withSelector(sbuilder.build()); |
| 609 | puntIp.withTreatment(tbuilder.build()); |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 610 | puntIp.withFlag(Flag.VERSATILE) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 611 | .withPriority(SegmentRoutingService.HIGHEST_PRIORITY) |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 612 | .makePermanent() |
| 613 | .fromApp(srManager.appId); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 614 | ObjectiveContext context = new DefaultObjectiveContext( |
| 615 | (objective) -> log.debug("IP punt rule for {} populated", ipaddr), |
| 616 | (objective, error) -> |
| 617 | log.warn("Failed to populate IP punt rule for {}: {}", ipaddr, error)); |
| 618 | srManager.flowObjectiveService.forward(deviceId, puntIp.add(context)); |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 619 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 622 | /** |
| 623 | * Populates a forwarding objective to send packets that miss other high |
| 624 | * priority Bridging Table entries to a group that contains all ports of |
| 625 | * its subnet. |
| 626 | * |
| 627 | * Note: We assume that packets sending from the edge switches to the hosts |
| 628 | * have untagged VLAN. |
| 629 | * The VLAN tag will be popped later in the flooding group. |
| 630 | * |
| 631 | * @param deviceId switch ID to set the rules |
| 632 | */ |
| 633 | public void populateSubnetBroadcastRule(DeviceId deviceId) { |
| 634 | config.getSubnets(deviceId).forEach(subnet -> { |
Charles Chan | bbd004c | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 635 | if (subnet.prefixLength() == 0 || |
| 636 | subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) { |
| 637 | return; |
| 638 | } |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 639 | int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet); |
| 640 | VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet); |
| 641 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 642 | if (nextId < 0 || vlanId == null) { |
Charles Chan | bbd004c | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 643 | log.error("Cannot install subnet {} broadcast rule in dev:{} due" |
| 644 | + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 645 | return; |
| 646 | } |
| 647 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 648 | /* Driver should treat objective with MacAddress.NONE as the |
| 649 | * subnet broadcast rule |
| 650 | */ |
| 651 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 652 | sbuilder.matchVlanId(vlanId); |
| 653 | sbuilder.matchEthDst(MacAddress.NONE); |
| 654 | |
| 655 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 656 | fob.withFlag(Flag.SPECIFIC) |
| 657 | .withSelector(sbuilder.build()) |
| 658 | .nextStep(nextId) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 659 | .withPriority(SegmentRoutingService.FLOOD_PRIORITY) |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 660 | .fromApp(srManager.appId) |
| 661 | .makePermanent(); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 662 | ObjectiveContext context = new DefaultObjectiveContext( |
| 663 | (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet), |
| 664 | (objective, error) -> |
| 665 | log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error)); |
| 666 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 667 | }); |
| 668 | } |
| 669 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 670 | /** |
| 671 | * Creates a filtering objective to permit VLAN cross-connect traffic. |
| 672 | * |
| 673 | * @param deviceId the DPID of the switch |
| 674 | */ |
| 675 | public void populateXConnectVlanFilters(DeviceId deviceId) { |
| 676 | Map<VlanId, List<ConnectPoint>> xConnectsForDevice = |
| 677 | config.getXConnects(); |
| 678 | xConnectsForDevice.forEach((vlanId, connectPoints) -> { |
| 679 | // Only proceed the xConnect for given device |
| 680 | for (ConnectPoint connectPoint : connectPoints) { |
| 681 | if (!connectPoint.deviceId().equals(deviceId)) { |
| 682 | return; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | connectPoints.forEach(connectPoint -> { |
| 687 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 688 | fob.withKey(Criteria.matchInPort(connectPoint.port())) |
| 689 | .addCondition(Criteria.matchVlanId(vlanId)) |
| 690 | .addCondition(Criteria.matchEthDst(MacAddress.NONE)) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 691 | .withPriority(SegmentRoutingService.XCONNECT_PRIORITY); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 692 | fob.permit().fromApp(srManager.appId); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 693 | ObjectiveContext context = new DefaultObjectiveContext( |
| 694 | (objective) -> log.debug("XConnect filter for {} populated", connectPoint), |
| 695 | (objective, error) -> |
| 696 | log.warn("Failed to populate xconnect filter for {}: {}", connectPoint, error)); |
| 697 | srManager.flowObjectiveService.filter(deviceId, fob.add(context)); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 698 | }); |
| 699 | }); |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * Populates a forwarding objective that points the VLAN cross-connect |
| 704 | * packets to a broadcast group. |
| 705 | * |
| 706 | * @param deviceId switch ID to set the rules |
| 707 | */ |
| 708 | public void populateXConnectBroadcastRule(DeviceId deviceId) { |
| 709 | Map<VlanId, List<ConnectPoint>> xConnects = |
| 710 | config.getXConnects(); |
| 711 | xConnects.forEach((vlanId, connectPoints) -> { |
| 712 | // Only proceed the xConnect for given device |
| 713 | for (ConnectPoint connectPoint : connectPoints) { |
| 714 | if (!connectPoint.deviceId().equals(deviceId)) { |
| 715 | return; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | int nextId = srManager.getXConnectNextObjectiveId(deviceId, vlanId); |
| 720 | if (nextId < 0) { |
| 721 | log.error("Cannot install cross-connect broadcast rule in dev:{} " + |
| 722 | "due to missing nextId:{}", deviceId, nextId); |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE |
| 728 | * as the VLAN cross-connect broadcast rules |
| 729 | */ |
| 730 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 731 | sbuilder.matchVlanId(vlanId); |
| 732 | sbuilder.matchEthDst(MacAddress.NONE); |
| 733 | |
| 734 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 735 | fob.withFlag(Flag.SPECIFIC) |
| 736 | .withSelector(sbuilder.build()) |
| 737 | .nextStep(nextId) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 738 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY) |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 739 | .fromApp(srManager.appId) |
| 740 | .makePermanent(); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 741 | ObjectiveContext context = new DefaultObjectiveContext( |
| 742 | (objective) -> log.debug("XConnect rule for {} populated", xConnects), |
| 743 | (objective, error) -> |
| 744 | log.warn("Failed to populate xconnect rule for {}: {}", xConnects, error)); |
| 745 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 746 | }); |
| 747 | } |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 748 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 749 | private int getPriorityFromPrefix(IpPrefix prefix) { |
| 750 | return (prefix.isIp4()) ? |
| 751 | 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY : |
| 752 | 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY; |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 753 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 754 | } |