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