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