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