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