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