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