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