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 |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 215 | * @param subnets subnet information |
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 | */ |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 220 | public boolean populateIpRuleForSubnet(DeviceId deviceId, |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 221 | Set<Ip4Prefix> subnets, |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 222 | DeviceId destSw, |
| 223 | Set<DeviceId> nextHops) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 224 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 225 | for (IpPrefix subnet : subnets) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 226 | if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) { |
| 227 | return false; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Populates IP flow rules for the router IP address. |
| 236 | * |
Saurav Das | a07f203 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 237 | * @param deviceId target device ID to set the rules |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 238 | * @param ipPrefix the IP address of the destination router |
| 239 | * @param destSw device ID of the destination router |
| 240 | * @param nextHops next hop switch ID list |
| 241 | * @return true if all rules are set successfully, false otherwise |
| 242 | */ |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 243 | public boolean populateIpRuleForRouter(DeviceId deviceId, |
| 244 | IpPrefix ipPrefix, DeviceId destSw, |
| 245 | Set<DeviceId> nextHops) { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 246 | int segmentId; |
| 247 | try { |
| 248 | segmentId = config.getSegmentId(destSw); |
| 249 | } catch (DeviceConfigNotFoundException e) { |
| 250 | log.warn(e.getMessage() + " Aborting populateIpRuleForRouter."); |
| 251 | return false; |
| 252 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 253 | |
| 254 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
Charles Chan | 17d38f4 | 2016-02-05 13:33:54 -0800 | [diff] [blame] | 255 | sbuilder.matchIPDst(ipPrefix); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 256 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 257 | TrafficSelector selector = sbuilder.build(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 258 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 259 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 260 | NeighborSet ns; |
| 261 | TrafficTreatment treatment; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 262 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 263 | // If the next hop is the same as the final destination, then MPLS label |
| 264 | // is not set. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 265 | if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) { |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 266 | tbuilder.immediate().decNwTtl(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 267 | ns = new NeighborSet(nextHops); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 268 | treatment = tbuilder.build(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 269 | } else { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 270 | ns = new NeighborSet(nextHops, segmentId); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 271 | treatment = null; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 274 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 275 | // if needed by the switch pipeline. Since neighbor sets are always to |
| 276 | // other neighboring routers, there is no subnet assigned on those ports. |
| 277 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector); |
| 278 | metabuilder.matchVlanId( |
| 279 | VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)); |
| 280 | |
| 281 | int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build()); |
| 282 | if (nextId <= 0) { |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 283 | log.warn("No next objective in {} for ns: {}", deviceId, ns); |
| 284 | return false; |
| 285 | } |
| 286 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 287 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 288 | .builder() |
| 289 | .fromApp(srManager.appId) |
| 290 | .makePermanent() |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 291 | .nextStep(nextId) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 292 | .withSelector(selector) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 293 | .withPriority(getPriorityFromPrefix(ipPrefix)) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 294 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 295 | if (treatment != null) { |
| 296 | fwdBuilder.withTreatment(treatment); |
| 297 | } |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 298 | log.debug("Installing IPv4 forwarding objective " |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 299 | + "for router IP/subnet {} in switch {}", |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 300 | ipPrefix, |
| 301 | deviceId); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 302 | ObjectiveContext context = new DefaultObjectiveContext( |
| 303 | (objective) -> log.debug("IP rule for router {} populated", ipPrefix), |
| 304 | (objective, error) -> |
| 305 | log.warn("Failed to populate IP rule for router {}: {}", ipPrefix, error)); |
| 306 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context)); |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 307 | rulePopulationCounter.incrementAndGet(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 308 | |
| 309 | return true; |
| 310 | } |
| 311 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 312 | /** |
Saurav Das | a07f203 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 313 | * Populates MPLS flow rules to all routers. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 314 | * |
Saurav Das | a07f203 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 315 | * @param deviceId target device ID of the switch to set the rules |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 316 | * @param destSwId destination switch device ID |
| 317 | * @param nextHops next hops switch ID list |
| 318 | * @return true if all rules are set successfully, false otherwise |
| 319 | */ |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 320 | public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId, |
| 321 | Set<DeviceId> nextHops) { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 322 | int segmentId; |
| 323 | try { |
| 324 | segmentId = config.getSegmentId(destSwId); |
| 325 | } catch (DeviceConfigNotFoundException e) { |
| 326 | log.warn(e.getMessage() + " Aborting populateMplsRule."); |
| 327 | return false; |
| 328 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 329 | |
| 330 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 331 | List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 332 | |
| 333 | // TODO Handle the case of Bos == false |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 334 | sbuilder.matchEthType(Ethernet.MPLS_UNICAST); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 335 | sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 336 | sbuilder.matchMplsBos(true); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 337 | TrafficSelector selector = sbuilder.build(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 338 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 339 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 340 | // if needed by the switch pipeline. Since mpls next-hops are always to |
| 341 | // other neighboring routers, there is no subnet assigned on those ports. |
| 342 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector); |
| 343 | metabuilder.matchVlanId( |
| 344 | VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)); |
| 345 | |
| 346 | // If the next hop is the destination router for the segment, do pop |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 347 | if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 348 | log.debug("populateMplsRule: Installing MPLS forwarding objective for " |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 349 | + "label {} in switch {} with pop", segmentId, deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 350 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 351 | // bos pop case (php) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 352 | ForwardingObjective.Builder fwdObjBosBuilder = |
| 353 | getMplsForwardingObjective(deviceId, |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 354 | nextHops, |
| 355 | true, |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 356 | true, |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 357 | metabuilder.build()); |
| 358 | if (fwdObjBosBuilder == null) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 359 | return false; |
| 360 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 361 | fwdObjBuilders.add(fwdObjBosBuilder); |
| 362 | |
| 363 | // XXX not-bos pop case, SR app multi-label not implemented yet |
| 364 | /*ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 365 | getMplsForwardingObjective(deviceId, |
| 366 | nextHops, |
| 367 | true, |
| 368 | false);*/ |
| 369 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 370 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 371 | // next hop is not destination, SR CONTINUE case (swap with self) |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 372 | log.debug("Installing MPLS forwarding objective for " |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 373 | + "label {} in switch {} without pop", segmentId, deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 374 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 375 | // continue case with bos - this does get triggered in edge routers |
| 376 | // and in core routers - driver can handle depending on availability |
| 377 | // of MPLS ECMP or not |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 378 | ForwardingObjective.Builder fwdObjBosBuilder = |
| 379 | getMplsForwardingObjective(deviceId, |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 380 | nextHops, |
| 381 | false, |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 382 | true, |
| 383 | metabuilder.build()); |
| 384 | if (fwdObjBosBuilder == null) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 385 | return false; |
| 386 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 387 | fwdObjBuilders.add(fwdObjBosBuilder); |
| 388 | |
| 389 | // XXX continue case with not-bos - SR app multi label not implemented yet |
| 390 | // also requires MPLS ECMP |
| 391 | /*ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 392 | getMplsForwardingObjective(deviceId, |
| 393 | nextHops, |
| 394 | false, |
| 395 | false); */ |
| 396 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 399 | for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) { |
| 400 | ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId) |
| 401 | .makePermanent()).withSelector(selector) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 402 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 403 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 404 | ObjectiveContext context = new DefaultObjectiveContext( |
| 405 | (objective) -> log.debug("MPLS rule for SID {} populated", segmentId), |
| 406 | (objective, error) -> |
| 407 | log.warn("Failed to populate MPLS rule for SID {}: {}", segmentId, error)); |
| 408 | srManager.flowObjectiveService.forward(deviceId, fwdObjBuilder.add(context)); |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 409 | rulePopulationCounter.incrementAndGet(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | return true; |
| 413 | } |
| 414 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 415 | private ForwardingObjective.Builder getMplsForwardingObjective( |
| 416 | DeviceId deviceId, |
| 417 | Set<DeviceId> nextHops, |
| 418 | boolean phpRequired, |
| 419 | boolean isBos, |
| 420 | TrafficSelector meta) { |
| 421 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 422 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 423 | .builder().withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 424 | |
| 425 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 426 | |
| 427 | if (phpRequired) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 428 | // php case - pop should always be flow-action |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 429 | log.debug("getMplsForwardingObjective: php required"); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 430 | tbuilder.deferred().copyTtlIn(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 431 | if (isBos) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 432 | tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType()) |
| 433 | .decNwTtl(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 434 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 435 | tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType()) |
| 436 | .decMplsTtl(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 437 | } |
| 438 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 439 | // swap with self case - SR CONTINUE |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 440 | log.debug("getMplsForwardingObjective: php not required"); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 441 | tbuilder.deferred().decMplsTtl(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 444 | // All forwarding is via ECMP group, the metadata informs the driver |
| 445 | // that the next-Objective will be used by MPLS flows. In other words, |
| 446 | // MPLS ECMP is requested. It is up to the driver to decide if these |
| 447 | // packets will be hashed or not. |
| 448 | fwdBuilder.withTreatment(tbuilder.build()); |
| 449 | NeighborSet ns = new NeighborSet(nextHops); |
| 450 | log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}", |
| 451 | deviceId, ns); |
| 452 | |
| 453 | int nextId = srManager.getNextObjectiveId(deviceId, ns, meta); |
| 454 | if (nextId <= 0) { |
| 455 | log.warn("No next objective in {} for ns: {}", deviceId, ns); |
| 456 | return null; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 459 | fwdBuilder.nextStep(nextId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 460 | return fwdBuilder; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 464 | * Creates a filtering objective to permit all untagged packets with a |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 465 | * dstMac corresponding to the router's MAC address. For those pipelines |
| 466 | * that need to internally assign vlans to untagged packets, this method |
| 467 | * provides per-subnet vlan-ids as metadata. |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 468 | * <p> |
| 469 | * Note that the vlan assignment is only done by the master-instance for a switch. |
| 470 | * However we send the filtering objective from slave-instances as well, so |
| 471 | * that drivers can obtain other information (like Router MAC and IP). |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 472 | * |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 473 | * @param deviceId the switch dpid for the router |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 474 | */ |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame^] | 475 | public boolean populateRouterMacVlanFilters(DeviceId deviceId) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 476 | log.debug("Installing per-port filtering objective for untagged " |
| 477 | + "packets in device {}", deviceId); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 478 | |
| 479 | MacAddress deviceMac; |
| 480 | try { |
| 481 | deviceMac = config.getDeviceMac(deviceId); |
| 482 | } catch (DeviceConfigNotFoundException e) { |
| 483 | log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters."); |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame^] | 484 | return false; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame^] | 487 | List<Port> devPorts = srManager.deviceService.getPorts(deviceId); |
| 488 | if (devPorts != null && devPorts.size() == 0) { |
| 489 | log.warn("Device {} ports not available. Unable to add MacVlan filters", |
| 490 | deviceId); |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | for (Port port : devPorts) { |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 495 | ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number()); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 496 | // TODO: Handles dynamic port events when we are ready for dynamic config |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 497 | if (!srManager.deviceConfiguration.suppressSubnet().contains(connectPoint) && |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 498 | port.isEnabled()) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 499 | Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number()); |
| 500 | VlanId assignedVlan = (portSubnet == null) |
| 501 | ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET) |
| 502 | : srManager.getSubnetAssignedVlanId(deviceId, portSubnet); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 503 | |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 504 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 505 | fob.withKey(Criteria.matchInPort(port.number())) |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 506 | .addCondition(Criteria.matchEthDst(deviceMac)) |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 507 | .addCondition(Criteria.matchVlanId(VlanId.NONE)) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 508 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 509 | // vlan assignment is valid only if this instance is master |
| 510 | if (srManager.mastershipService.isLocalMaster(deviceId)) { |
| 511 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
| 512 | .pushVlan().setVlanId(assignedVlan).build(); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 513 | fob.withMeta(tt); |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 514 | } |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 515 | fob.permit().fromApp(srManager.appId); |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame^] | 516 | log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 517 | ObjectiveContext context = new DefaultObjectiveContext( |
| 518 | (objective) -> log.debug("Filter for {} populated", connectPoint), |
| 519 | (objective, error) -> |
| 520 | log.warn("Failed to populate filter for {}: {}", connectPoint, error)); |
| 521 | srManager.flowObjectiveService.filter(deviceId, fob.add(context)); |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame^] | 524 | return true; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 528 | * Creates a forwarding objective to punt all IP packets, destined to the |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 529 | * router's port IP addresses, to the controller. Note that the input |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 530 | * 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] | 531 | * Furthermore, these are applied only by the master instance. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 532 | * |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 533 | * @param deviceId the switch dpid for the router |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 534 | */ |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 535 | public void populateRouterIpPunts(DeviceId deviceId) { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 536 | Ip4Address routerIp; |
| 537 | try { |
| 538 | routerIp = config.getRouterIp(deviceId); |
| 539 | } catch (DeviceConfigNotFoundException e) { |
| 540 | log.warn(e.getMessage() + " Aborting populateRouterIpPunts."); |
| 541 | return; |
| 542 | } |
| 543 | |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 544 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 545 | log.debug("Not installing port-IP punts - not the master for dev:{} ", |
| 546 | deviceId); |
| 547 | return; |
| 548 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 549 | ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder(); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 550 | Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId)); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 551 | allIps.add(routerIp); |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 552 | for (Ip4Address ipaddr : allIps) { |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 553 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 554 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 555 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 556 | sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 557 | IpPrefix.MAX_INET_MASK_LENGTH)); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 558 | tbuilder.setOutput(PortNumber.CONTROLLER); |
| 559 | puntIp.withSelector(sbuilder.build()); |
| 560 | puntIp.withTreatment(tbuilder.build()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 561 | puntIp.withFlag(Flag.VERSATILE) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 562 | .withPriority(SegmentRoutingService.HIGHEST_PRIORITY) |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 563 | .makePermanent() |
| 564 | .fromApp(srManager.appId); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 565 | ObjectiveContext context = new DefaultObjectiveContext( |
| 566 | (objective) -> log.debug("IP punt rule for {} populated", ipaddr), |
| 567 | (objective, error) -> |
| 568 | log.warn("Failed to populate IP punt rule for {}: {}", ipaddr, error)); |
| 569 | srManager.flowObjectiveService.forward(deviceId, puntIp.add(context)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 570 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 573 | /** |
| 574 | * Populates a forwarding objective to send packets that miss other high |
| 575 | * priority Bridging Table entries to a group that contains all ports of |
| 576 | * its subnet. |
| 577 | * |
| 578 | * Note: We assume that packets sending from the edge switches to the hosts |
| 579 | * have untagged VLAN. |
| 580 | * The VLAN tag will be popped later in the flooding group. |
| 581 | * |
| 582 | * @param deviceId switch ID to set the rules |
| 583 | */ |
| 584 | public void populateSubnetBroadcastRule(DeviceId deviceId) { |
| 585 | config.getSubnets(deviceId).forEach(subnet -> { |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 586 | if (subnet.prefixLength() == 0 || |
| 587 | subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) { |
| 588 | return; |
| 589 | } |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 590 | int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet); |
| 591 | VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet); |
| 592 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 593 | if (nextId < 0 || vlanId == null) { |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 594 | log.error("Cannot install subnet {} broadcast rule in dev:{} due" |
| 595 | + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 599 | /* Driver should treat objective with MacAddress.NONE as the |
| 600 | * subnet broadcast rule |
| 601 | */ |
| 602 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 603 | sbuilder.matchVlanId(vlanId); |
| 604 | sbuilder.matchEthDst(MacAddress.NONE); |
| 605 | |
| 606 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 607 | fob.withFlag(Flag.SPECIFIC) |
| 608 | .withSelector(sbuilder.build()) |
| 609 | .nextStep(nextId) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 610 | .withPriority(SegmentRoutingService.FLOOD_PRIORITY) |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 611 | .fromApp(srManager.appId) |
| 612 | .makePermanent(); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 613 | ObjectiveContext context = new DefaultObjectiveContext( |
| 614 | (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet), |
| 615 | (objective, error) -> |
| 616 | log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error)); |
| 617 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 618 | }); |
| 619 | } |
| 620 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 621 | /** |
| 622 | * Creates a filtering objective to permit VLAN cross-connect traffic. |
| 623 | * |
| 624 | * @param deviceId the DPID of the switch |
| 625 | */ |
| 626 | public void populateXConnectVlanFilters(DeviceId deviceId) { |
| 627 | Map<VlanId, List<ConnectPoint>> xConnectsForDevice = |
| 628 | config.getXConnects(); |
| 629 | xConnectsForDevice.forEach((vlanId, connectPoints) -> { |
| 630 | // Only proceed the xConnect for given device |
| 631 | for (ConnectPoint connectPoint : connectPoints) { |
| 632 | if (!connectPoint.deviceId().equals(deviceId)) { |
| 633 | return; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | connectPoints.forEach(connectPoint -> { |
| 638 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 639 | fob.withKey(Criteria.matchInPort(connectPoint.port())) |
| 640 | .addCondition(Criteria.matchVlanId(vlanId)) |
| 641 | .addCondition(Criteria.matchEthDst(MacAddress.NONE)) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 642 | .withPriority(SegmentRoutingService.XCONNECT_PRIORITY); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 643 | fob.permit().fromApp(srManager.appId); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 644 | ObjectiveContext context = new DefaultObjectiveContext( |
| 645 | (objective) -> log.debug("XConnect filter for {} populated", connectPoint), |
| 646 | (objective, error) -> |
| 647 | log.warn("Failed to populate xconnect filter for {}: {}", connectPoint, error)); |
| 648 | srManager.flowObjectiveService.filter(deviceId, fob.add(context)); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 649 | }); |
| 650 | }); |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Populates a forwarding objective that points the VLAN cross-connect |
| 655 | * packets to a broadcast group. |
| 656 | * |
| 657 | * @param deviceId switch ID to set the rules |
| 658 | */ |
| 659 | public void populateXConnectBroadcastRule(DeviceId deviceId) { |
| 660 | Map<VlanId, List<ConnectPoint>> xConnects = |
| 661 | config.getXConnects(); |
| 662 | xConnects.forEach((vlanId, connectPoints) -> { |
| 663 | // Only proceed the xConnect for given device |
| 664 | for (ConnectPoint connectPoint : connectPoints) { |
| 665 | if (!connectPoint.deviceId().equals(deviceId)) { |
| 666 | return; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | int nextId = srManager.getXConnectNextObjectiveId(deviceId, vlanId); |
| 671 | if (nextId < 0) { |
| 672 | log.error("Cannot install cross-connect broadcast rule in dev:{} " + |
| 673 | "due to missing nextId:{}", deviceId, nextId); |
| 674 | return; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE |
| 679 | * as the VLAN cross-connect broadcast rules |
| 680 | */ |
| 681 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 682 | sbuilder.matchVlanId(vlanId); |
| 683 | sbuilder.matchEthDst(MacAddress.NONE); |
| 684 | |
| 685 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 686 | fob.withFlag(Flag.SPECIFIC) |
| 687 | .withSelector(sbuilder.build()) |
| 688 | .nextStep(nextId) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 689 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY) |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 690 | .fromApp(srManager.appId) |
| 691 | .makePermanent(); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 692 | ObjectiveContext context = new DefaultObjectiveContext( |
| 693 | (objective) -> log.debug("XConnect rule for {} populated", xConnects), |
| 694 | (objective, error) -> |
| 695 | log.warn("Failed to populate xconnect rule for {}: {}", xConnects, error)); |
| 696 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 697 | }); |
| 698 | } |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 699 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 700 | private int getPriorityFromPrefix(IpPrefix prefix) { |
| 701 | return (prefix.isIp4()) ? |
| 702 | 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY : |
| 703 | 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY; |
Srikanth Vavilapalli | f3a8bc0 | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 704 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 705 | } |