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