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