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