sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 19 | import org.onlab.packet.EthType; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 20 | import org.onlab.packet.Ethernet; |
| 21 | import org.onlab.packet.Ip4Address; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 22 | import org.onlab.packet.Ip6Address; |
| 23 | import org.onlab.packet.IpAddress; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | import org.onlab.packet.IpPrefix; |
| 25 | import org.onlab.packet.MacAddress; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 26 | import org.onlab.packet.MplsLabel; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 27 | import org.onlab.packet.VlanId; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 28 | import org.onosproject.net.ConnectPoint; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flowobjective.Objective; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flowobjective.ObjectiveError; |
Charles Chan | 2df0e8a | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 33 | import org.onosproject.net.packet.PacketPriority; |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 34 | import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 35 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 36 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 37 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 38 | import org.onosproject.segmentrouting.grouphandler.DestinationSet; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 39 | import org.onosproject.net.DeviceId; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 40 | import org.onosproject.net.Port; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 41 | import org.onosproject.net.PortNumber; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 44 | import org.onosproject.net.flow.TrafficSelector; |
| 45 | import org.onosproject.net.flow.TrafficTreatment; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flow.criteria.Criteria; |
| 47 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 48 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 49 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 50 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 51 | import org.onosproject.net.flowobjective.ForwardingObjective.Builder; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
| 55 | |
| 56 | import java.util.ArrayList; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 57 | import java.util.Collection; |
| 58 | import java.util.Collections; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 59 | import java.util.HashMap; |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 60 | import java.util.HashSet; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 61 | import java.util.List; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 62 | import java.util.Map; |
Charles Chan | 2df0e8a | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 63 | import java.util.Optional; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 64 | import java.util.Set; |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 65 | import java.util.concurrent.atomic.AtomicLong; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 66 | |
| 67 | import static com.google.common.base.Preconditions.checkNotNull; |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 68 | import static org.onlab.packet.Ethernet.TYPE_ARP; |
| 69 | import static org.onlab.packet.Ethernet.TYPE_IPV6; |
| 70 | import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION; |
| 71 | import static org.onlab.packet.IPv6.PROTOCOL_ICMP6; |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 72 | import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 73 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Populator of segment routing flow rules. |
| 76 | */ |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 77 | public class RoutingRulePopulator { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 78 | private static final Logger log = LoggerFactory |
| 79 | .getLogger(RoutingRulePopulator.class); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 80 | |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 81 | private AtomicLong rulePopulationCounter; |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 82 | private SegmentRoutingManager srManager; |
| 83 | private DeviceConfiguration config; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 84 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Creates a RoutingRulePopulator object. |
| 87 | * |
Thomas Vachuska | e10f56b | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 88 | * @param srManager segment routing manager reference |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 89 | */ |
| 90 | public RoutingRulePopulator(SegmentRoutingManager srManager) { |
| 91 | this.srManager = srManager; |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 92 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 93 | this.rulePopulationCounter = new AtomicLong(0); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Resets the population counter. |
| 98 | */ |
| 99 | public void resetCounter() { |
| 100 | rulePopulationCounter.set(0); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the number of rules populated. |
Thomas Vachuska | 266b443 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 105 | * |
| 106 | * @return number of rules |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 107 | */ |
| 108 | public long getCounter() { |
| 109 | return rulePopulationCounter.get(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 113 | * Populates IP rules for a route that has direct connection to the |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 114 | * switch. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 115 | * |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 116 | * @param deviceId device ID of the device that next hop attaches to |
| 117 | * @param prefix IP prefix of the route |
| 118 | * @param hostMac MAC address of the next hop |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 119 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 120 | * @param outPort port where the next hop attaches to |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 121 | */ |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 122 | public void populateRoute(DeviceId deviceId, IpPrefix prefix, |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 123 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 124 | log.debug("Populate direct routing entry for route {} at {}:{}", |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 125 | prefix, deviceId, outPort); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 126 | ForwardingObjective.Builder fwdBuilder; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 127 | try { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 128 | fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, |
| 129 | hostVlanId, outPort, false); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 130 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 131 | log.warn(e.getMessage() + " Aborting direct populateRoute"); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 132 | return; |
| 133 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 134 | if (fwdBuilder == null) { |
Saurav Das | 76ae681 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 135 | log.warn("Aborting host routing table entry due " |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 136 | + "to error for dev:{} route:{}", deviceId, prefix); |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 137 | return; |
| 138 | } |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 139 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 140 | (objective) -> log.debug("Direct routing rule for route {} populated", |
| 141 | prefix), |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 142 | (objective, error) -> |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 143 | log.warn("Failed to populate direct routing rule for route {}: {}", |
Saurav Das | 76ae681 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 144 | prefix, error)); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 145 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context)); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 146 | rulePopulationCounter.incrementAndGet(); |
| 147 | } |
| 148 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 149 | /** |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 150 | * Removes IP rules for a route when the next hop is gone. |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 151 | * |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 152 | * @param deviceId device ID of the device that next hop attaches to |
| 153 | * @param prefix IP prefix of the route |
| 154 | * @param hostMac MAC address of the next hop |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 155 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 156 | * @param outPort port that next hop attaches to |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 157 | */ |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 158 | public void revokeRoute(DeviceId deviceId, IpPrefix prefix, |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 159 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) { |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 160 | log.debug("Revoke IP table entry for route {} at {}:{}", |
| 161 | prefix, deviceId, outPort); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 162 | ForwardingObjective.Builder fwdBuilder; |
| 163 | try { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 164 | fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, |
| 165 | hostVlanId, outPort, true); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 166 | } catch (DeviceConfigNotFoundException e) { |
| 167 | log.warn(e.getMessage() + " Aborting revokeIpRuleForHost."); |
| 168 | return; |
| 169 | } |
Charles Chan | 458b826 | 2016-11-30 11:55:05 -0800 | [diff] [blame] | 170 | if (fwdBuilder == null) { |
| 171 | log.warn("Aborting host routing table entries due " |
| 172 | + "to error for dev:{} route:{}", deviceId, prefix); |
| 173 | return; |
| 174 | } |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 175 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 176 | (objective) -> log.debug("IP rule for route {} revoked", prefix), |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 177 | (objective, error) -> |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 178 | log.warn("Failed to revoke IP rule for route {}: {}", prefix, error)); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 179 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context)); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 182 | /** |
Charles Chan | f6ec153 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 183 | * Returns a forwarding objective builder for routing rules. |
| 184 | * <p> |
| 185 | * The forwarding objective routes packets destined to a given prefix to |
| 186 | * given port on given device with given destination MAC. |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 187 | * |
| 188 | * @param deviceId device ID |
| 189 | * @param prefix prefix that need to be routed |
| 190 | * @param hostMac MAC address of the nexthop |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 191 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 192 | * @param outPort port where the nexthop attaches to |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 193 | * @param revoke true if forwarding objective is meant to revoke forwarding rule |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 194 | * @return forwarding objective builder |
| 195 | * @throws DeviceConfigNotFoundException if given device is not configured |
| 196 | */ |
Charles Chan | f6ec153 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 197 | private ForwardingObjective.Builder routingFwdObjBuilder( |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 198 | DeviceId deviceId, IpPrefix prefix, |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 199 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, |
| 200 | boolean revoke) |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 201 | throws DeviceConfigNotFoundException { |
| 202 | MacAddress deviceMac; |
| 203 | deviceMac = config.getDeviceMac(deviceId); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 204 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 205 | ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort); |
| 206 | VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint); |
| 207 | Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint); |
| 208 | VlanId nativeVlan = srManager.getNativeVlanId(connectPoint); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 209 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 210 | // Create route selector |
| 211 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix); |
| 212 | |
| 213 | // Create route treatment |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 214 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 215 | tbuilder.deferred() |
| 216 | .setEthDst(hostMac) |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 217 | .setEthSrc(deviceMac) |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 218 | .setOutput(outPort); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 219 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 220 | // Create route meta |
| 221 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 222 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 223 | // Adjust the meta according to VLAN configuration |
| 224 | if (taggedVlans.contains(hostVlanId)) { |
| 225 | tbuilder.setVlanId(hostVlanId); |
| 226 | } else if (hostVlanId.equals(VlanId.NONE)) { |
| 227 | if (untaggedVlan != null) { |
| 228 | mbuilder.matchVlanId(untaggedVlan); |
| 229 | } else if (nativeVlan != null) { |
| 230 | mbuilder.matchVlanId(nativeVlan); |
| 231 | } else { |
| 232 | // TODO: This check is turned off for now since vRouter still assumes that |
| 233 | // hosts are internally tagged with INTERNAL_VLAN. |
| 234 | // We should turn this back on when we move forward to the bridging CPR approach. |
| 235 | // |
| 236 | //log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan", |
| 237 | // hostMac, hostVlanId, connectPoint); |
| 238 | //return null; |
| 239 | mbuilder.matchVlanId(INTERNAL_VLAN); |
| 240 | } |
| 241 | } else { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 242 | log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed" |
| 243 | + " in tagged vlan", hostMac, hostVlanId, connectPoint); |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 244 | return null; |
| 245 | } |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 246 | // if the objective is to revoke an existing rule, and for some reason |
| 247 | // the next-objective does not exist, then a new one should not be created |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 248 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort, |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 249 | tbuilder.build(), mbuilder.build(), !revoke); |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 250 | if (portNextObjId == -1) { |
| 251 | // Warning log will come from getPortNextObjective method |
| 252 | return null; |
| 253 | } |
| 254 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 255 | return DefaultForwardingObjective.builder() |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 256 | .withSelector(sbuilder.build()) |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 257 | .nextStep(portNextObjId) |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 258 | .fromApp(srManager.appId).makePermanent() |
Charles Chan | 1cdecff | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 259 | .withPriority(getPriorityFromPrefix(prefix)) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 260 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 264 | * Populates IP flow rules for all the given prefixes reachable from the |
| 265 | * destination switch(es). |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 266 | * |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 267 | * @param targetSw switch where rules are to be programmed |
| 268 | * @param subnets subnets/prefixes being added |
| 269 | * @param destSw1 destination switch where the prefixes are reachable |
| 270 | * @param destSw2 paired destination switch if one exists for the subnets/prefixes. |
| 271 | * Should be null if there is no paired destination switch (by config) |
| 272 | * or if the given prefixes are reachable only via destSw1 |
| 273 | * @param nextHops a map containing a set of next-hops for each destination switch. |
| 274 | * If destSw2 is not null, then this map must contain an |
| 275 | * entry for destSw2 with its next-hops from the targetSw |
| 276 | * (although the next-hop set may be empty in certain scenarios). |
| 277 | * If destSw2 is null, there should not be an entry in this |
| 278 | * map for destSw2. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 279 | * @return true if all rules are set successfully, false otherwise |
| 280 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 281 | public boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets, |
| 282 | DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 283 | for (IpPrefix subnet : subnets) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 284 | if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 285 | return false; |
| 286 | } |
| 287 | } |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 288 | return true; |
| 289 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 290 | |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 291 | /** |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 292 | * Revokes IP flow rules for the subnets in each edge switch. |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 293 | * |
| 294 | * @param subnets subnet being removed |
| 295 | * @return true if all rules are removed successfully, false otherwise |
| 296 | */ |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 297 | public boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) { |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 298 | for (IpPrefix subnet : subnets) { |
| 299 | if (!revokeIpRuleForRouter(subnet)) { |
| 300 | return false; |
| 301 | } |
| 302 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 303 | return true; |
| 304 | } |
| 305 | |
| 306 | /** |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 307 | * Populates IP flow rules for an IP prefix in the target device. The prefix |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 308 | * is reachable via destination device(s). |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 309 | * |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 310 | * @param targetSw target device ID to set the rules |
| 311 | * @param ipPrefix the IP prefix |
| 312 | * @param destSw1 destination switch where the prefixes are reachable |
| 313 | * @param destSw2 paired destination switch if one exists for the subnets/prefixes. |
| 314 | * Should be null if there is no paired destination switch (by config) |
| 315 | * or if the given prefixes are reachable only via destSw1 |
| 316 | * @param nextHops map of destination switches and their next-hops. |
| 317 | * Should only contain destination switches that are |
| 318 | * actually meant to be routed to. If destSw2 is null, there |
| 319 | * should not be an entry for destSw2 in this map. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 320 | * @return true if all rules are set successfully, false otherwise |
| 321 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 322 | public boolean populateIpRuleForRouter(DeviceId targetSw, |
| 323 | IpPrefix ipPrefix, DeviceId destSw1, |
| 324 | DeviceId destSw2, |
| 325 | Map<DeviceId, Set<DeviceId>> nextHops) { |
| 326 | int segmentId1, segmentId2 = -1; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 327 | try { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 328 | if (ipPrefix.isIp4()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 329 | segmentId1 = config.getIPv4SegmentId(destSw1); |
| 330 | if (destSw2 != null) { |
| 331 | segmentId2 = config.getIPv4SegmentId(destSw2); |
| 332 | } |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 333 | } else { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 334 | segmentId1 = config.getIPv6SegmentId(destSw1); |
| 335 | if (destSw2 != null) { |
| 336 | segmentId2 = config.getIPv6SegmentId(destSw2); |
| 337 | } |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 338 | } |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 339 | } catch (DeviceConfigNotFoundException e) { |
| 340 | log.warn(e.getMessage() + " Aborting populateIpRuleForRouter."); |
| 341 | return false; |
| 342 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 343 | |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 344 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 345 | TrafficSelector selector = sbuilder.build(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 346 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 347 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 348 | DestinationSet ds; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 349 | TrafficTreatment treatment; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 350 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 351 | // If the next hop is the same as the final destination, then MPLS label |
| 352 | // is not set. |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 353 | /*if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) { |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 354 | tbuilder.immediate().decNwTtl(); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 355 | ds = new DestinationSet(false, destSw); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 356 | treatment = tbuilder.build(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 357 | } else { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 358 | ds = new DestinationSet(false, segmentId, destSw); |
| 359 | treatment = null; |
| 360 | }*/ |
| 361 | if (destSw2 == null) { |
| 362 | // single dst - create destination set based on next-hop |
| 363 | Set<DeviceId> nhd1 = nextHops.get(destSw1); |
| 364 | if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) { |
| 365 | tbuilder.immediate().decNwTtl(); |
| 366 | ds = new DestinationSet(false, destSw1); |
| 367 | treatment = tbuilder.build(); |
| 368 | } else { |
| 369 | ds = new DestinationSet(false, segmentId1, destSw1); |
| 370 | treatment = null; |
| 371 | } |
| 372 | } else { |
| 373 | // dst pair - IP rules for dst-pairs are always from other edge nodes |
| 374 | // the destination set needs to have both destinations, even if there |
| 375 | // are no next hops to one of them |
| 376 | ds = new DestinationSet(false, segmentId1, destSw1, segmentId2, destSw2); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 377 | treatment = null; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 380 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 381 | // if needed by the switch pipeline. Since neighbor sets are always to |
| 382 | // other neighboring routers, there is no subnet assigned on those ports. |
| 383 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector); |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 384 | metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 385 | DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 386 | if (grpHandler == null) { |
| 387 | log.warn("populateIPRuleForRouter: groupHandler for device {} " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 388 | + "not found", targetSw); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 389 | return false; |
| 390 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 391 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 392 | int nextId = grpHandler.getNextObjectiveId(ds, nextHops, |
| 393 | metabuilder.build(), true); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 394 | if (nextId <= 0) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 395 | log.warn("No next objective in {} for ds: {}", targetSw, ds); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 396 | return false; |
| 397 | } |
| 398 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 399 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 400 | .builder() |
| 401 | .fromApp(srManager.appId) |
| 402 | .makePermanent() |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 403 | .nextStep(nextId) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 404 | .withSelector(selector) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 405 | .withPriority(getPriorityFromPrefix(ipPrefix)) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 406 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 407 | if (treatment != null) { |
| 408 | fwdBuilder.withTreatment(treatment); |
| 409 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 410 | log.debug("Installing IPv4 forwarding objective for router IP/subnet {} " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 411 | + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 412 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 413 | (objective) -> log.debug("IP rule for router {} populated in dev:{}", |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 414 | ipPrefix, targetSw), |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 415 | (objective, error) -> |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 416 | log.warn("Failed to populate IP rule for router {}: {} in dev:{}", |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 417 | ipPrefix, error, targetSw)); |
| 418 | srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context)); |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 419 | rulePopulationCounter.incrementAndGet(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 420 | |
| 421 | return true; |
| 422 | } |
| 423 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 424 | /** |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 425 | * Revokes IP flow rules for the router IP address. |
| 426 | * |
| 427 | * @param ipPrefix the IP address of the destination router |
| 428 | * @return true if all rules are removed successfully, false otherwise |
| 429 | */ |
| 430 | public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) { |
Pier Ventre | 968da12 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 431 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 432 | TrafficSelector selector = sbuilder.build(); |
| 433 | TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build(); |
| 434 | |
| 435 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 436 | .builder() |
| 437 | .fromApp(srManager.appId) |
| 438 | .makePermanent() |
| 439 | .withSelector(selector) |
| 440 | .withTreatment(dummyTreatment) |
| 441 | .withPriority(getPriorityFromPrefix(ipPrefix)) |
| 442 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 443 | |
| 444 | ObjectiveContext context = new DefaultObjectiveContext( |
| 445 | (objective) -> log.debug("IP rule for router {} revoked", ipPrefix), |
| 446 | (objective, error) -> |
| 447 | log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error)); |
| 448 | |
| 449 | srManager.deviceService.getAvailableDevices().forEach(device -> { |
| 450 | srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context)); |
| 451 | }); |
| 452 | |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | /** |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 457 | * Deals with !MPLS Bos use case. |
| 458 | * |
| 459 | * @param targetSwId the target sw |
| 460 | * @param destSwId the destination sw |
| 461 | * @param nextHops the set of next hops |
| 462 | * @param segmentId the segmentId to match |
| 463 | * @param routerIp the router ip |
| 464 | * @return a collection of fwdobjective |
| 465 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 466 | private Collection<ForwardingObjective> handleMpls( |
| 467 | DeviceId targetSwId, |
| 468 | DeviceId destSwId, |
| 469 | Set<DeviceId> nextHops, |
| 470 | int segmentId, |
| 471 | IpAddress routerIp, |
| 472 | boolean isMplsBos) { |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 473 | |
| 474 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 475 | List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList(); |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 476 | // For the transport of Pwaas we can use two or three MPLS label |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 477 | sbuilder.matchEthType(Ethernet.MPLS_UNICAST); |
| 478 | sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId)); |
| 479 | sbuilder.matchMplsBos(isMplsBos); |
| 480 | TrafficSelector selector = sbuilder.build(); |
| 481 | |
| 482 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 483 | // if needed by the switch pipeline. Since mpls next-hops are always to |
| 484 | // other neighboring routers, there is no subnet assigned on those ports. |
| 485 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector); |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 486 | metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 487 | |
| 488 | if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) { |
| 489 | // If the next hop is the destination router for the segment, do pop |
| 490 | log.debug("populateMplsRule: Installing MPLS forwarding objective for " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 491 | + "label {} in switch {} with pop to next-hops {}", |
| 492 | segmentId, targetSwId, nextHops); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 493 | // Not-bos pop case (php for the current label). If MPLS-ECMP |
| 494 | // has been configured, the application we will request the |
| 495 | // installation for an MPLS-ECMP group. |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 496 | ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 497 | getMplsForwardingObjective(targetSwId, |
| 498 | nextHops, |
| 499 | true, |
| 500 | isMplsBos, |
| 501 | metabuilder.build(), |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 502 | routerIp, |
| 503 | destSwId); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 504 | // Error case, we cannot handle, exit. |
| 505 | if (fwdObjNoBosBuilder == null) { |
| 506 | return Collections.emptyList(); |
| 507 | } |
| 508 | fwdObjBuilders.add(fwdObjNoBosBuilder); |
| 509 | |
| 510 | } else { |
| 511 | // next hop is not destination, SR CONTINUE case (swap with self) |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 512 | log.debug("Installing MPLS forwarding objective for " |
| 513 | + "label {} in switch {} without pop to next-hops {}", |
| 514 | segmentId, targetSwId, nextHops); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 515 | // Not-bos pop case. If MPLS-ECMP has been configured, the |
| 516 | // application we will request the installation for an MPLS-ECMP |
| 517 | // group. |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 518 | ForwardingObjective.Builder fwdObjNoBosBuilder = |
| 519 | getMplsForwardingObjective(targetSwId, |
| 520 | nextHops, |
| 521 | false, |
| 522 | isMplsBos, |
| 523 | metabuilder.build(), |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 524 | routerIp, |
| 525 | destSwId); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 526 | // Error case, we cannot handle, exit. |
| 527 | if (fwdObjNoBosBuilder == null) { |
| 528 | return Collections.emptyList(); |
| 529 | } |
| 530 | fwdObjBuilders.add(fwdObjNoBosBuilder); |
| 531 | |
| 532 | } |
| 533 | |
| 534 | List<ForwardingObjective> fwdObjs = Lists.newArrayList(); |
| 535 | // We add the final property to the fwdObjs. |
| 536 | for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) { |
| 537 | |
| 538 | ((Builder) ((Builder) fwdObjBuilder |
| 539 | .fromApp(srManager.appId) |
| 540 | .makePermanent()) |
| 541 | .withSelector(selector) |
| 542 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)) |
| 543 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 544 | |
| 545 | ObjectiveContext context = new DefaultObjectiveContext( |
| 546 | (objective) -> |
| 547 | log.debug("MPLS rule {} for SID {} populated in dev:{} ", |
| 548 | objective.id(), segmentId, targetSwId), |
| 549 | (objective, error) -> |
| 550 | log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}", |
| 551 | objective.id(), segmentId, error, targetSwId)); |
| 552 | |
| 553 | ForwardingObjective fob = fwdObjBuilder.add(context); |
| 554 | fwdObjs.add(fob); |
| 555 | |
| 556 | } |
| 557 | |
| 558 | return fwdObjs; |
| 559 | } |
| 560 | |
| 561 | /** |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 562 | * Populates MPLS flow rules in the target device to point towards the |
| 563 | * destination device. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 564 | * |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 565 | * @param targetSwId target device ID of the switch to set the rules |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 566 | * @param destSwId destination switch device ID |
| 567 | * @param nextHops next hops switch ID list |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 568 | * @param routerIp the router ip |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 569 | * @return true if all rules are set successfully, false otherwise |
| 570 | */ |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 571 | public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId, |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 572 | Set<DeviceId> nextHops, |
| 573 | IpAddress routerIp) { |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 574 | |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 575 | int segmentId; |
| 576 | try { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 577 | if (routerIp.isIp4()) { |
| 578 | segmentId = config.getIPv4SegmentId(destSwId); |
| 579 | } else { |
| 580 | segmentId = config.getIPv6SegmentId(destSwId); |
| 581 | } |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 582 | } catch (DeviceConfigNotFoundException e) { |
| 583 | log.warn(e.getMessage() + " Aborting populateMplsRule."); |
| 584 | return false; |
| 585 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 586 | |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 587 | List<ForwardingObjective> fwdObjs = new ArrayList<>(); |
| 588 | Collection<ForwardingObjective> fwdObjsMpls = Collections.emptyList(); |
| 589 | // Generates the transit rules used by the standard "routing". |
| 590 | fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true); |
| 591 | if (fwdObjsMpls.isEmpty()) { |
| 592 | return false; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 593 | } |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 594 | fwdObjs.addAll(fwdObjsMpls); |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 595 | // Generates the transit rules used by the MPLS Pwaas. For now it is |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 596 | // the only case !BoS supported. |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 597 | /*fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 598 | if (fwdObjsMpls.isEmpty()) { |
| 599 | return false; |
| 600 | } |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 601 | fwdObjs.addAll(fwdObjsMpls);*/ |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 602 | |
| 603 | for (ForwardingObjective fwdObj : fwdObjs) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 604 | log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}", |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 605 | fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId); |
| 606 | srManager.flowObjectiveService.forward(targetSwId, fwdObj); |
sangho | 20eff1d | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 607 | rulePopulationCounter.incrementAndGet(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | return true; |
| 611 | } |
| 612 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 613 | private ForwardingObjective.Builder getMplsForwardingObjective( |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 614 | DeviceId targetSw, |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 615 | Set<DeviceId> nextHops, |
| 616 | boolean phpRequired, |
| 617 | boolean isBos, |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 618 | TrafficSelector meta, |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 619 | IpAddress routerIp, |
| 620 | DeviceId destSw) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 621 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 622 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 623 | .builder().withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 624 | |
| 625 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 626 | |
| 627 | if (phpRequired) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 628 | // php case - pop should always be flow-action |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 629 | log.debug("getMplsForwardingObjective: php required"); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 630 | tbuilder.deferred().copyTtlIn(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 631 | if (isBos) { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 632 | if (routerIp.isIp4()) { |
| 633 | tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType()); |
| 634 | } else { |
| 635 | tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType()); |
| 636 | } |
| 637 | tbuilder.decNwTtl(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 638 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 639 | tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType()) |
| 640 | .decMplsTtl(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 641 | } |
| 642 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 643 | // swap with self case - SR CONTINUE |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 644 | log.debug("getMplsForwardingObjective: php not required"); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 645 | tbuilder.deferred().decMplsTtl(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 648 | fwdBuilder.withTreatment(tbuilder.build()); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 649 | // if MPLS-ECMP == True we will build a standard NeighborSet. |
| 650 | // Otherwise a RandomNeighborSet. |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 651 | DestinationSet ns = DestinationSet.destinationSet(false, false, destSw); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 652 | if (!isBos && this.srManager.getMplsEcmp()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 653 | ns = DestinationSet.destinationSet(false, true, destSw); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 654 | } else if (!isBos && !this.srManager.getMplsEcmp()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 655 | ns = DestinationSet.destinationSet(true, true, destSw); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 656 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 657 | |
Saurav Das | b28d5dd | 2017-03-24 19:03:58 -0700 | [diff] [blame] | 658 | log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}", |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 659 | targetSw, ns); |
| 660 | DefaultGroupHandler gh = srManager.getGroupHandler(targetSw); |
| 661 | if (gh == null) { |
| 662 | log.warn("getNextObjectiveId query - groupHandler for device {} " |
| 663 | + "not found", targetSw); |
| 664 | return null; |
| 665 | } |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 666 | // If BoS == True, all forwarding is via L3 ECMP group. |
| 667 | // If Bos == False, the forwarding can be via MPLS-ECMP group or through |
| 668 | // MPLS-Interface group. This depends on the configuration of the option |
| 669 | // MPLS-ECMP. |
| 670 | // The metadata informs the driver that the next-Objective will be used |
| 671 | // by MPLS flows and if Bos == False the driver will use MPLS groups. |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 672 | Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>(); |
| 673 | dstNextHops.put(destSw, nextHops); |
| 674 | int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 675 | if (nextId <= 0) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 676 | log.warn("No next objective in {} for ns: {}", targetSw, ns); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 677 | return null; |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 678 | } else { |
| 679 | log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}", |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 680 | nextId, targetSw, ns); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 683 | fwdBuilder.nextStep(nextId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 684 | return fwdBuilder; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 688 | * Creates a filtering objective to permit all untagged packets with a |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 689 | * dstMac corresponding to the router's MAC address. For those pipelines |
| 690 | * that need to internally assign vlans to untagged packets, this method |
| 691 | * provides per-subnet vlan-ids as metadata. |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 692 | * <p> |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 693 | * Note that the vlan assignment and filter programming should only be done by |
| 694 | * the master for a switch. This method is typically called at deviceAdd and |
| 695 | * programs filters only for the enabled ports of the device. For port-updates, |
| 696 | * that enable/disable ports after device add, singlePortFilter methods should |
| 697 | * be called. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 698 | * |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 699 | * @param deviceId the switch dpid for the router |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 700 | * @return PortFilterInfo information about the processed ports |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 701 | */ |
Charles Chan | f6ec153 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 702 | public PortFilterInfo populateVlanMacFilters(DeviceId deviceId) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 703 | log.debug("Installing per-port filtering objective for untagged " |
| 704 | + "packets in device {}", deviceId); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 705 | |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 706 | List<Port> devPorts = srManager.deviceService.getPorts(deviceId); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 707 | if (devPorts == null || devPorts.isEmpty()) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 708 | log.warn("Device {} ports not available. Unable to add MacVlan filters", |
| 709 | deviceId); |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 710 | return null; |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 711 | } |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 712 | int disabledPorts = 0, errorPorts = 0, filteredPorts = 0; |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 713 | for (Port port : devPorts) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 714 | if (!port.isEnabled()) { |
| 715 | disabledPorts++; |
| 716 | continue; |
| 717 | } |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 718 | if (processSinglePortFilters(deviceId, port.number(), true)) { |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 719 | filteredPorts++; |
| 720 | } else { |
| 721 | errorPorts++; |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 722 | } |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 723 | } |
Charles Chan | 7f9737b | 2017-06-22 14:27:17 -0700 | [diff] [blame] | 724 | log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}", |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 725 | deviceId, disabledPorts, errorPorts, filteredPorts); |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 726 | return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts, |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 727 | errorPorts, filteredPorts); |
| 728 | } |
| 729 | |
| 730 | /** |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 731 | * Creates or removes filtering objectives for a single port. Should only be |
| 732 | * called by the master for a switch. |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 733 | * |
| 734 | * @param deviceId device identifier |
| 735 | * @param portnum port identifier for port to be filtered |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 736 | * @param install true to install the filtering objective, false to remove |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 737 | * @return true if no errors occurred during the build of the filtering objective |
| 738 | */ |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 739 | public boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 740 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum); |
| 741 | VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint); |
| 742 | Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint); |
| 743 | VlanId nativeVlan = srManager.getNativeVlanId(connectPoint); |
| 744 | |
| 745 | if (taggedVlans.size() != 0) { |
| 746 | // Filter for tagged vlans |
| 747 | if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId -> |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 748 | processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 749 | return false; |
| 750 | } |
| 751 | if (nativeVlan != null) { |
| 752 | // Filter for native vlan |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 753 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 754 | return false; |
| 755 | } |
| 756 | } |
| 757 | } else if (untaggedVlan != null) { |
| 758 | // Filter for untagged vlan |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 759 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 760 | return false; |
| 761 | } |
| 762 | } else { |
| 763 | // Unconfigure port, use INTERNAL_VLAN |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 764 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 765 | return false; |
| 766 | } |
| 767 | } |
| 768 | return true; |
| 769 | } |
| 770 | |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 771 | private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum, |
| 772 | boolean pushVlan, VlanId vlanId, boolean install) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 773 | FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId); |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 774 | if (fob == null) { |
| 775 | // error encountered during build |
| 776 | return false; |
| 777 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 778 | log.debug("{} filtering objectives for dev/port:{}/{}", |
| 779 | install ? "Installing" : "Removing", deviceId, portnum); |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 780 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 781 | (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum, |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 782 | install ? "installed" : "removed"), |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 783 | (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}", |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 784 | install ? "install" : "remove", deviceId, portnum, error)); |
Charles Chan | 7e4f819 | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 785 | if (install) { |
| 786 | srManager.flowObjectiveService.filter(deviceId, fob.add(context)); |
| 787 | } else { |
| 788 | srManager.flowObjectiveService.filter(deviceId, fob.remove(context)); |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 789 | } |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 790 | return true; |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 793 | private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum, |
| 794 | boolean pushVlan, VlanId vlanId) { |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 795 | MacAddress deviceMac; |
| 796 | try { |
| 797 | deviceMac = config.getDeviceMac(deviceId); |
| 798 | } catch (DeviceConfigNotFoundException e) { |
| 799 | log.warn(e.getMessage() + " Processing SinglePortFilters aborted"); |
| 800 | return null; |
| 801 | } |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 802 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 803 | fob.withKey(Criteria.matchInPort(portnum)) |
| 804 | .addCondition(Criteria.matchEthDst(deviceMac)) |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 805 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 806 | |
| 807 | if (pushVlan) { |
| 808 | fob.addCondition(Criteria.matchVlanId(VlanId.NONE)); |
| 809 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
| 810 | .pushVlan().setVlanId(vlanId).build(); |
| 811 | fob.withMeta(tt); |
| 812 | } else { |
| 813 | fob.addCondition(Criteria.matchVlanId(vlanId)); |
| 814 | } |
| 815 | |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 816 | fob.permit().fromApp(srManager.appId); |
| 817 | return fob; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 821 | * Creates a forwarding objective to punt all IP packets, destined to the |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 822 | * router's port IP addresses, to the controller. Note that the input |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 823 | * 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] | 824 | * Furthermore, these are applied only by the master instance. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 825 | * |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 826 | * @param deviceId the switch dpid for the router |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 827 | */ |
Charles Chan | f6ec153 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 828 | public void populateIpPunts(DeviceId deviceId) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 829 | Ip4Address routerIpv4, pairRouterIpv4 = null; |
| 830 | Ip6Address routerIpv6, pairRouterIpv6 = null; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 831 | try { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 832 | routerIpv4 = config.getRouterIpv4(deviceId); |
| 833 | routerIpv6 = config.getRouterIpv6(deviceId); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 834 | if (config.isPairedEdge(deviceId)) { |
| 835 | pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId)); |
| 836 | pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId)); |
| 837 | } |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 838 | } catch (DeviceConfigNotFoundException e) { |
Charles Chan | f6ec153 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 839 | log.warn(e.getMessage() + " Aborting populateIpPunts."); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 840 | return; |
| 841 | } |
| 842 | |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 843 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 844 | log.debug("Not installing port-IP punts - not the master for dev:{} ", |
| 845 | deviceId); |
| 846 | return; |
| 847 | } |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 848 | Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId)); |
| 849 | allIps.add(routerIpv4); |
| 850 | if (routerIpv6 != null) { |
| 851 | allIps.add(routerIpv6); |
| 852 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 853 | if (pairRouterIpv4 != null) { |
| 854 | allIps.add(pairRouterIpv4); |
| 855 | } |
| 856 | if (pairRouterIpv6 != null) { |
| 857 | allIps.add(pairRouterIpv6); |
| 858 | } |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 859 | for (IpAddress ipaddr : allIps) { |
| 860 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr); |
Charles Chan | 2df0e8a | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 861 | Optional<DeviceId> optDeviceId = Optional.of(deviceId); |
| 862 | |
| 863 | srManager.packetService.requestPackets(sbuilder.build(), |
| 864 | PacketPriority.CONTROL, srManager.appId, optDeviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 865 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 868 | /** |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 869 | * Method to build IPv4 or IPv6 selector. |
| 870 | * |
| 871 | * @param addressToMatch the address to match |
| 872 | */ |
| 873 | private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) { |
| 874 | return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix()); |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * Method to build IPv4 or IPv6 selector. |
| 879 | * |
| 880 | * @param prefixToMatch the prefix to match |
| 881 | */ |
| 882 | private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) { |
| 883 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 884 | // If the prefix is IPv4 |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 885 | if (prefixToMatch.isIp4()) { |
| 886 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 887 | selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix()); |
| 888 | return selectorBuilder; |
| 889 | } |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 890 | // If the prefix is IPv6 |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 891 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 892 | selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix()); |
| 893 | return selectorBuilder; |
| 894 | } |
| 895 | |
| 896 | /** |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 897 | * Creates forwarding objectives to punt ARP and NDP packets, to the controller. |
| 898 | * Furthermore, these are applied only by the master instance. Deferred actions |
| 899 | * are not cleared such that packets can be flooded in the cross connect use case |
| 900 | * |
| 901 | * @param deviceId the switch dpid for the router |
| 902 | */ |
| 903 | public void populateArpNdpPunts(DeviceId deviceId) { |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 904 | // We are not the master just skip. |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 905 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 906 | log.debug("Not installing ARP/NDP punts - not the master for dev:{} ", |
| 907 | deviceId); |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | // We punt all ARP packets towards the controller. |
| 912 | ForwardingObjective puntFwd = puntArpFwdObjective() |
| 913 | .add(new ObjectiveContext() { |
| 914 | @Override |
| 915 | public void onError(Objective objective, ObjectiveError error) { |
| 916 | log.warn("Failed to install packet request for ARP to {}: {}", |
| 917 | deviceId, error); |
| 918 | } |
| 919 | }); |
| 920 | srManager.flowObjectiveService.forward(deviceId, puntFwd); |
| 921 | |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 922 | // We punt all NDP packets towards the controller. |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 923 | puntFwd = puntNdpFwdObjective() |
| 924 | .add(new ObjectiveContext() { |
| 925 | @Override |
| 926 | public void onError(Objective objective, ObjectiveError error) { |
| 927 | log.warn("Failed to install packet request for NDP to {}: {}", |
| 928 | deviceId, error); |
| 929 | } |
| 930 | }); |
| 931 | srManager.flowObjectiveService.forward(deviceId, puntFwd); |
Pier Luigi | 9e5c5ca | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) { |
| 935 | |
| 936 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 937 | tBuilder.punt(); |
| 938 | |
| 939 | return DefaultForwardingObjective.builder() |
| 940 | .withPriority(PacketPriority.CONTROL.priorityValue()) |
| 941 | .withSelector(selector) |
| 942 | .fromApp(srManager.appId) |
| 943 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 944 | .withTreatment(tBuilder.build()) |
| 945 | .makePermanent(); |
| 946 | } |
| 947 | |
| 948 | private ForwardingObjective.Builder puntArpFwdObjective() { |
| 949 | |
| 950 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 951 | sBuilder.matchEthType(TYPE_ARP); |
| 952 | |
| 953 | return fwdObjBuilder(sBuilder.build()); |
| 954 | } |
| 955 | |
| 956 | private ForwardingObjective.Builder puntNdpFwdObjective() { |
| 957 | |
| 958 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 959 | sBuilder.matchEthType(TYPE_IPV6) |
| 960 | .matchIPProtocol(PROTOCOL_ICMP6) |
| 961 | .matchIcmpv6Type(NEIGHBOR_SOLICITATION) |
| 962 | .build(); |
| 963 | |
| 964 | return fwdObjBuilder(sBuilder.build()); |
| 965 | } |
| 966 | |
| 967 | /** |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 968 | * Populates a forwarding objective to send packets that miss other high |
| 969 | * priority Bridging Table entries to a group that contains all ports of |
| 970 | * its subnet. |
| 971 | * |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 972 | * @param deviceId switch ID to set the rules |
| 973 | */ |
| 974 | public void populateSubnetBroadcastRule(DeviceId deviceId) { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 975 | srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> { |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 976 | int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId); |
| 977 | |
| 978 | if (nextId < 0) { |
| 979 | log.error("Cannot install vlan {} broadcast rule in dev:{} due" |
| 980 | + "to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 981 | return; |
| 982 | } |
| 983 | |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 984 | // Driver should treat objective with MacAddress.NONE as the |
| 985 | // subnet broadcast rule |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 986 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 987 | sbuilder.matchVlanId(vlanId); |
| 988 | sbuilder.matchEthDst(MacAddress.NONE); |
| 989 | |
| 990 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 991 | fob.withFlag(Flag.SPECIFIC) |
| 992 | .withSelector(sbuilder.build()) |
| 993 | .nextStep(nextId) |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 994 | .withPriority(SegmentRoutingService.FLOOD_PRIORITY) |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 995 | .fromApp(srManager.appId) |
| 996 | .makePermanent(); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 997 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 998 | (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId), |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 999 | (objective, error) -> |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1000 | log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error)); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1001 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1002 | }); |
| 1003 | } |
| 1004 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1005 | private int getPriorityFromPrefix(IpPrefix prefix) { |
| 1006 | return (prefix.isIp4()) ? |
| 1007 | 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY : |
| 1008 | 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY; |
Srikanth Vavilapalli | f3a8bc0 | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 1009 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1010 | } |