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