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