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