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