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 | |
| 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.Ip4Address; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 20 | import org.onlab.packet.Ip4Prefix; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onlab.packet.MacAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 23 | import org.onlab.packet.MplsLabel; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 25 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Link; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 28 | import org.onosproject.net.Port; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 29 | import org.onosproject.net.PortNumber; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.criteria.Criteria; |
| 35 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 36 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 37 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 38 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flowobjective.Objective; |
| 40 | import org.onosproject.net.flowobjective.ObjectiveError; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.ForwardingObjective.Builder; |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 42 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flowobjective.ObjectiveContext; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
| 47 | import java.util.ArrayList; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 48 | import java.util.List; |
| 49 | import java.util.Set; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 50 | import java.util.concurrent.atomic.AtomicLong; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 51 | |
| 52 | import static com.google.common.base.Preconditions.checkNotNull; |
| 53 | |
| 54 | public class RoutingRulePopulator { |
| 55 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 56 | private static final Logger log = LoggerFactory |
| 57 | .getLogger(RoutingRulePopulator.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 58 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 59 | private AtomicLong rulePopulationCounter; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 60 | private SegmentRoutingManager srManager; |
| 61 | private DeviceConfiguration config; |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 62 | |
| 63 | private static final int HIGHEST_PRIORITY = 0xffff; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 64 | private static final long OFPP_MAX = 0xffffff00L; |
| 65 | |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 66 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Creates a RoutingRulePopulator object. |
| 69 | * |
Thomas Vachuska | 8a07509 | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 70 | * @param srManager segment routing manager reference |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 71 | */ |
| 72 | public RoutingRulePopulator(SegmentRoutingManager srManager) { |
| 73 | this.srManager = srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 74 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 75 | this.rulePopulationCounter = new AtomicLong(0); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Resets the population counter. |
| 80 | */ |
| 81 | public void resetCounter() { |
| 82 | rulePopulationCounter.set(0); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the number of rules populated. |
Thomas Vachuska | 7cfc620 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 87 | * |
| 88 | * @return number of rules |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 89 | */ |
| 90 | public long getCounter() { |
| 91 | return rulePopulationCounter.get(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 95 | * Populates IP flow rules for specific hosts directly connected to the |
| 96 | * switch. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 97 | * |
| 98 | * @param deviceId switch ID to set the rules |
| 99 | * @param hostIp host IP address |
| 100 | * @param hostMac host MAC address |
| 101 | * @param outPort port where the host is connected |
| 102 | */ |
| 103 | public void populateIpRuleForHost(DeviceId deviceId, Ip4Address hostIp, |
| 104 | MacAddress hostMac, PortNumber outPort) { |
| 105 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 106 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 107 | |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 108 | sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 109 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 110 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 111 | tbuilder.deferred() |
| 112 | .setEthDst(hostMac) |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 113 | .setEthSrc(config.getDeviceMac(deviceId)) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 114 | .setOutput(outPort); |
| 115 | |
| 116 | TrafficTreatment treatment = tbuilder.build(); |
| 117 | TrafficSelector selector = sbuilder.build(); |
| 118 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 119 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 120 | .builder().fromApp(srManager.appId).makePermanent() |
| 121 | .withSelector(selector).withTreatment(treatment) |
| 122 | .withPriority(100).withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 123 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 124 | log.debug("Installing IPv4 forwarding objective " |
| 125 | + "for host {} in switch {}", hostIp, deviceId); |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 126 | srManager.flowObjectiveService. |
| 127 | forward(deviceId, |
| 128 | fwdBuilder. |
| 129 | add(new SRObjectiveContext(deviceId, |
| 130 | SRObjectiveContext.ObjectiveType.FORWARDING))); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 131 | rulePopulationCounter.incrementAndGet(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Populates IP flow rules for the subnets of the destination router. |
| 136 | * |
| 137 | * @param deviceId switch ID to set the rules |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 138 | * @param subnets subnet information |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 139 | * @param destSw destination switch ID |
| 140 | * @param nextHops next hop switch ID list |
| 141 | * @return true if all rules are set successfully, false otherwise |
| 142 | */ |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 143 | public boolean populateIpRuleForSubnet(DeviceId deviceId, |
| 144 | List<Ip4Prefix> subnets, |
| 145 | DeviceId destSw, |
| 146 | Set<DeviceId> nextHops) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 147 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 148 | for (IpPrefix subnet : subnets) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 149 | if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) { |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Populates IP flow rules for the router IP address. |
| 159 | * |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 160 | * @param deviceId target device ID to set the rules |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 161 | * @param ipPrefix the IP address of the destination router |
| 162 | * @param destSw device ID of the destination router |
| 163 | * @param nextHops next hop switch ID list |
| 164 | * @return true if all rules are set successfully, false otherwise |
| 165 | */ |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 166 | public boolean populateIpRuleForRouter(DeviceId deviceId, |
| 167 | IpPrefix ipPrefix, DeviceId destSw, |
| 168 | Set<DeviceId> nextHops) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 169 | |
| 170 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 171 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 172 | |
| 173 | sbuilder.matchIPDst(ipPrefix); |
| 174 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 175 | |
| 176 | NeighborSet ns = null; |
| 177 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 178 | // If the next hop is the same as the final destination, then MPLS label |
| 179 | // is not set. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 180 | if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 181 | tbuilder.deferred().decNwTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 182 | ns = new NeighborSet(nextHops); |
| 183 | } else { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 184 | tbuilder.deferred().copyTtlOut(); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 185 | ns = new NeighborSet(nextHops, config.getSegmentId(destSw)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 186 | } |
| 187 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 188 | TrafficTreatment treatment = tbuilder.build(); |
| 189 | TrafficSelector selector = sbuilder.build(); |
| 190 | |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 191 | if (srManager.getNextObjectiveId(deviceId, ns) <= 0) { |
| 192 | log.warn("No next objective in {} for ns: {}", deviceId, ns); |
| 193 | return false; |
| 194 | } |
| 195 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 196 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 197 | .builder() |
| 198 | .fromApp(srManager.appId) |
| 199 | .makePermanent() |
| 200 | .nextStep(srManager.getNextObjectiveId(deviceId, ns)) |
| 201 | .withTreatment(treatment) |
| 202 | .withSelector(selector) |
| 203 | .withPriority(100) |
| 204 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 205 | log.debug("Installing IPv4 forwarding objective " |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 206 | + "for router IP/subnet {} in switch {}", |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 207 | ipPrefix, |
| 208 | deviceId); |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 209 | srManager.flowObjectiveService. |
| 210 | forward(deviceId, |
| 211 | fwdBuilder. |
| 212 | add(new SRObjectiveContext(deviceId, |
| 213 | SRObjectiveContext.ObjectiveType.FORWARDING))); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 214 | rulePopulationCounter.incrementAndGet(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 219 | /** |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 220 | * Populates MPLS flow rules to all routers. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 221 | * |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 222 | * @param deviceId target device ID of the switch to set the rules |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 223 | * @param destSwId destination switch device ID |
| 224 | * @param nextHops next hops switch ID list |
| 225 | * @return true if all rules are set successfully, false otherwise |
| 226 | */ |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 227 | public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId, |
| 228 | Set<DeviceId> nextHops) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 229 | |
| 230 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 231 | List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 232 | |
| 233 | // TODO Handle the case of Bos == false |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 234 | sbuilder.matchMplsLabel(MplsLabel.mplsLabel(config.getSegmentId(destSwId))); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 235 | sbuilder.matchEthType(Ethernet.MPLS_UNICAST); |
| 236 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 237 | // If the next hop is the destination router, do PHP |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 238 | if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 239 | log.debug("populateMplsRule: Installing MPLS forwarding objective for " |
| 240 | + "label {} in switch {} with PHP", |
| 241 | config.getSegmentId(destSwId), |
| 242 | deviceId); |
| 243 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 244 | ForwardingObjective.Builder fwdObjBosBuilder = |
| 245 | getMplsForwardingObjective(deviceId, |
| 246 | destSwId, |
| 247 | nextHops, |
| 248 | true, |
| 249 | true); |
| 250 | // TODO: Check with Sangho on why we need this |
| 251 | ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 252 | getMplsForwardingObjective(deviceId, |
| 253 | destSwId, |
| 254 | nextHops, |
| 255 | true, |
| 256 | false); |
| 257 | if (fwdObjBosBuilder != null) { |
| 258 | fwdObjBuilders.add(fwdObjBosBuilder); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 259 | } else { |
| 260 | log.warn("Failed to set MPLS rules."); |
| 261 | return false; |
| 262 | } |
| 263 | } else { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 264 | log.debug("Installing MPLS forwarding objective for " |
| 265 | + "label {} in switch {} without PHP", |
| 266 | config.getSegmentId(destSwId), |
| 267 | deviceId); |
| 268 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 269 | ForwardingObjective.Builder fwdObjBosBuilder = |
| 270 | getMplsForwardingObjective(deviceId, |
| 271 | destSwId, |
| 272 | nextHops, |
| 273 | false, |
| 274 | true); |
| 275 | // TODO: Check with Sangho on why we need this |
| 276 | ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 277 | getMplsForwardingObjective(deviceId, |
| 278 | destSwId, |
| 279 | nextHops, |
| 280 | false, |
| 281 | false); |
| 282 | if (fwdObjBosBuilder != null) { |
| 283 | fwdObjBuilders.add(fwdObjBosBuilder); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 284 | } else { |
| 285 | log.warn("Failed to set MPLS rules."); |
| 286 | return false; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | TrafficSelector selector = sbuilder.build(); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 291 | for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) { |
| 292 | ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId) |
| 293 | .makePermanent()).withSelector(selector) |
| 294 | .withPriority(100)) |
| 295 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 296 | srManager.flowObjectiveService. |
| 297 | forward(deviceId, |
| 298 | fwdObjBuilder. |
| 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 | |
| 304 | return true; |
| 305 | } |
| 306 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 307 | private ForwardingObjective.Builder getMplsForwardingObjective(DeviceId deviceId, |
| 308 | DeviceId destSw, |
| 309 | Set<DeviceId> nextHops, |
| 310 | boolean phpRequired, |
| 311 | boolean isBos) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 312 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 313 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 314 | .builder().withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 315 | |
| 316 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 317 | |
| 318 | if (phpRequired) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 319 | log.debug("getMplsForwardingObjective: php required"); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 320 | tbuilder.deferred().copyTtlIn(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 321 | if (isBos) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 322 | tbuilder.deferred().popMpls(Ethernet.TYPE_IPV4).decNwTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 323 | } else { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 324 | tbuilder.deferred().popMpls(Ethernet.MPLS_UNICAST).decMplsTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 325 | } |
| 326 | } else { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 327 | log.debug("getMplsForwardingObjective: php not required"); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 328 | tbuilder.deferred().decMplsTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 329 | } |
| 330 | |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 331 | if (!isECMPSupportedInTransitRouter() && !config.isEdgeDevice(deviceId)) { |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 332 | PortNumber port = selectOnePort(deviceId, nextHops); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 333 | DeviceId nextHop = (DeviceId) nextHops.toArray()[0]; |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 334 | if (port == null) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 335 | log.warn("No link from {} to {}", deviceId, nextHops); |
| 336 | return null; |
| 337 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 338 | tbuilder.deferred() |
| 339 | .setEthSrc(config.getDeviceMac(deviceId)) |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 340 | .setEthDst(config.getDeviceMac(nextHop)) |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 341 | .setOutput(port); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 342 | fwdBuilder.withTreatment(tbuilder.build()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 343 | } else { |
| 344 | NeighborSet ns = new NeighborSet(nextHops); |
sangho | 3cd8db6 | 2015-05-06 11:42:31 -0700 | [diff] [blame] | 345 | fwdBuilder.withTreatment(tbuilder.build()); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 346 | fwdBuilder.nextStep(srManager |
| 347 | .getNextObjectiveId(deviceId, ns)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 350 | return fwdBuilder; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 351 | } |
| 352 | |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 353 | private boolean isECMPSupportedInTransitRouter() { |
| 354 | |
| 355 | // TODO: remove this function when objectives subsystem is supported. |
| 356 | return false; |
| 357 | } |
| 358 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 359 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 360 | * Creates a filtering objective to permit all untagged packets with a |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 361 | * dstMac corresponding to the router's MAC address. For those pipelines |
| 362 | * that need to internally assign vlans to untagged packets, this method |
| 363 | * provides per-subnet vlan-ids as metadata. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 364 | * |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 365 | * @param deviceId the switch dpid for the router |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 366 | */ |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 367 | public void populateRouterMacVlanFilters(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 368 | log.debug("Installing per-port filtering objective for untagged " |
| 369 | + "packets in device {}", deviceId); |
| 370 | for (Port port : srManager.deviceService.getPorts(deviceId)) { |
| 371 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 372 | Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number()); |
| 373 | VlanId assignedVlan = (portSubnet == null) |
| 374 | ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET) |
| 375 | : srManager.getSubnetAssignedVlanId(deviceId, portSubnet); |
| 376 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
| 377 | .pushVlan().setVlanId(assignedVlan).build(); |
| 378 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 379 | fob.withKey(Criteria.matchInPort(port.number())) |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 380 | .addCondition(Criteria.matchEthDst(config.getDeviceMac(deviceId))) |
| 381 | .addCondition(Criteria.matchVlanId(VlanId.NONE)) |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame^] | 382 | .setMeta(tt) |
| 383 | .addCondition(Criteria.matchIPDst(IpPrefix.valueOf( |
| 384 | config.getRouterIp(deviceId), |
| 385 | IpPrefix.MAX_INET_MASK_LENGTH))); |
| 386 | fob.permit().fromApp(srManager.appId); |
| 387 | srManager.flowObjectiveService. |
| 388 | filter(deviceId, fob.add(new SRObjectiveContext(deviceId, |
| 389 | SRObjectiveContext.ObjectiveType.FILTER))); |
| 390 | } |
| 391 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 395 | * Creates a forwarding objective to punt all IP packets, destined to the |
| 396 | * router's port IP addresses, to the controller. Note that it the input |
| 397 | * port should not be matched on, as these packets can come from any input. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 398 | * |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 399 | * @param deviceId the switch dpid for the router |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 400 | */ |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 401 | public void populateRouterIpPunts(DeviceId deviceId) { |
| 402 | ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 403 | |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 404 | List<Ip4Address> gws = config.getPortIPs(deviceId); |
| 405 | for (Ip4Address ipaddr : gws) { |
| 406 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 407 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 408 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 409 | selector.matchIPDst(IpPrefix.valueOf(ipaddr, |
| 410 | IpPrefix.MAX_INET_MASK_LENGTH)); |
| 411 | treatment.setOutput(PortNumber.CONTROLLER); |
| 412 | puntIp.withSelector(selector.build()); |
| 413 | puntIp.withTreatment(treatment.build()); |
| 414 | puntIp.withFlag(Flag.VERSATILE) |
| 415 | .withPriority(HIGHEST_PRIORITY) |
| 416 | .makePermanent() |
| 417 | .fromApp(srManager.appId); |
| 418 | log.debug("Installing forwarding objective to punt port IP addresses"); |
| 419 | srManager.flowObjectiveService. |
| 420 | forward(deviceId, |
| 421 | puntIp.add(new SRObjectiveContext(deviceId, |
| 422 | SRObjectiveContext.ObjectiveType.FORWARDING))); |
| 423 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 424 | } |
| 425 | |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 426 | private PortNumber selectOnePort(DeviceId srcId, Set<DeviceId> destIds) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 427 | |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 428 | Set<Link> links = srManager.linkService.getDeviceLinks(srcId); |
| 429 | for (DeviceId destId: destIds) { |
| 430 | for (Link link : links) { |
| 431 | if (link.dst().deviceId().equals(destId)) { |
| 432 | return link.src().port(); |
| 433 | } else if (link.src().deviceId().equals(destId)) { |
| 434 | return link.dst().port(); |
| 435 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | return null; |
| 440 | } |
| 441 | |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 442 | private static class SRObjectiveContext implements ObjectiveContext { |
| 443 | enum ObjectiveType { |
| 444 | FILTER, |
| 445 | FORWARDING |
| 446 | } |
| 447 | final DeviceId deviceId; |
| 448 | final ObjectiveType type; |
| 449 | |
| 450 | SRObjectiveContext(DeviceId deviceId, ObjectiveType type) { |
| 451 | this.deviceId = deviceId; |
| 452 | this.type = type; |
| 453 | } |
| 454 | @Override |
| 455 | public void onSuccess(Objective objective) { |
| 456 | log.debug("{} objective operation successful in device {}", |
| 457 | type.name(), deviceId); |
| 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public void onError(Objective objective, ObjectiveError error) { |
| 462 | log.warn("{} objective {} operation failed with error: {} in device {}", |
| 463 | type.name(), objective, error, deviceId); |
| 464 | } |
| 465 | } |
| 466 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 467 | } |