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