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