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