sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.segmentrouting; |
| 17 | |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 19 | import org.onlab.packet.EthType; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 20 | import org.onlab.packet.Ethernet; |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame^] | 21 | import org.onlab.packet.IPv6; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Address; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 23 | import org.onlab.packet.Ip6Address; |
| 24 | import org.onlab.packet.IpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.packet.MacAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 27 | import org.onlab.packet.MplsLabel; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 28 | import org.onlab.packet.VlanId; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flowobjective.Objective; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flowobjective.ObjectiveError; |
Charles Chan | 2d0bbcd | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 34 | import org.onosproject.net.packet.PacketPriority; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 35 | import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo; |
Charles Chan | 319d1a2 | 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 | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 38 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 39 | import org.onosproject.segmentrouting.grouphandler.DestinationSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 40 | import org.onosproject.net.DeviceId; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 41 | import org.onosproject.net.Port; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import org.onosproject.net.PortNumber; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 44 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
sangho | 80f11cb | 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 | 6450548 | 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 | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 53 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
sangho | 80f11cb | 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 | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 58 | import java.util.Collection; |
| 59 | import java.util.Collections; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 60 | import java.util.HashMap; |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 61 | import java.util.HashSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 62 | import java.util.List; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 63 | import java.util.Map; |
Charles Chan | 2d0bbcd | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 64 | import java.util.Optional; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 65 | import java.util.Set; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 66 | import java.util.concurrent.atomic.AtomicLong; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 67 | |
| 68 | import static com.google.common.base.Preconditions.checkNotNull; |
Pier Luigi | b9632ba | 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 | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 73 | import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 74 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Populator of segment routing flow rules. |
| 77 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 78 | public class RoutingRulePopulator { |
Charles Chan | ef8d12e | 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 | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 82 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 83 | private AtomicLong rulePopulationCounter; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 84 | private SegmentRoutingManager srManager; |
| 85 | private DeviceConfiguration config; |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 86 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Creates a RoutingRulePopulator object. |
| 89 | * |
Thomas Vachuska | 8a07509 | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 90 | * @param srManager segment routing manager reference |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 91 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 92 | RoutingRulePopulator(SegmentRoutingManager srManager) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 93 | this.srManager = srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 94 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | fb7c729 | 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 | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 101 | void resetCounter() { |
sangho | fb7c729 | 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 | 7cfc620 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 107 | * |
| 108 | * @return number of rules |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 109 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 110 | long getCounter() { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 111 | return rulePopulationCounter.get(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 115 | * Populates IP rules for a route that has direct connection to the |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 116 | * switch. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 117 | * |
Charles Chan | ddac7fd | 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 | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 121 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 122 | * @param outPort port where the next hop attaches to |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 123 | */ |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 124 | void populateRoute(DeviceId deviceId, IpPrefix prefix, |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 125 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 126 | log.debug("Populate direct routing entry for route {} at {}:{}", |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 127 | prefix, deviceId, outPort); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 128 | ForwardingObjective.Builder fwdBuilder; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 129 | try { |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 130 | fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, |
| 131 | hostVlanId, outPort, false); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 132 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 133 | log.warn(e.getMessage() + " Aborting direct populateRoute"); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 134 | return; |
| 135 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 136 | if (fwdBuilder == null) { |
Saurav Das | 368cf21 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 137 | log.warn("Aborting host routing table entry due " |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 138 | + "to error for dev:{} route:{}", deviceId, prefix); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 139 | return; |
| 140 | } |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 141 | |
| 142 | int nextId = fwdBuilder.add().nextId(); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 143 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 910be6a | 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 | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 146 | (objective, error) -> |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 147 | log.warn("Failed to populate direct routing rule for route {}: {}", |
Saurav Das | 368cf21 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 148 | prefix, error)); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 149 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context)); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 150 | rulePopulationCounter.incrementAndGet(); |
| 151 | } |
| 152 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 153 | /** |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 154 | * Removes IP rules for a route when the next hop is gone. |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 155 | * |
Charles Chan | ddac7fd | 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 | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 159 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 160 | * @param outPort port that next hop attaches to |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 161 | */ |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 162 | void revokeRoute(DeviceId deviceId, IpPrefix prefix, |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 163 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) { |
Charles Chan | ddac7fd | 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 | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 166 | ForwardingObjective.Builder fwdBuilder; |
| 167 | try { |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 168 | fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, |
| 169 | hostVlanId, outPort, true); |
Charles Chan | f458611 | 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 | ea702b1 | 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 | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 179 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 180 | (objective) -> log.debug("IP rule for route {} revoked", prefix), |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 181 | (objective, error) -> |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 182 | log.warn("Failed to revoke IP rule for route {}: {}", prefix, error)); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 183 | srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context)); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 186 | /** |
Charles Chan | 18fa425 | 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 | ddac7fd | 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 | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 195 | * @param hostVlanId Vlan ID of the nexthop |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 196 | * @param outPort port where the nexthop attaches to |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 197 | * @param revoke true if forwarding objective is meant to revoke forwarding rule |
Charles Chan | ddac7fd | 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 | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 201 | private ForwardingObjective.Builder routingFwdObjBuilder( |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 202 | DeviceId deviceId, IpPrefix prefix, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 203 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, |
| 204 | boolean revoke) |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 205 | throws DeviceConfigNotFoundException { |
| 206 | MacAddress deviceMac; |
| 207 | deviceMac = config.getDeviceMac(deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 208 | |
Charles Chan | 90772a7 | 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 | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 213 | |
Charles Chan | 90772a7 | 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 | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 218 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 219 | tbuilder.deferred() |
| 220 | .setEthDst(hostMac) |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 221 | .setEthSrc(deviceMac) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 222 | .setOutput(outPort); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 223 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 224 | // Create route meta |
| 225 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 226 | |
Charles Chan | 90772a7 | 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 | 3ed34d8 | 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 | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 239 | } |
| 240 | } else { |
Saurav Das | 2cb3829 | 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 | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 243 | return null; |
| 244 | } |
Saurav Das | 2cb3829 | 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 | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 247 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 248 | tbuilder.build(), mbuilder.build(), !revoke); |
Charles Chan | 90772a7 | 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 | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 254 | return DefaultForwardingObjective.builder() |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 255 | .withSelector(sbuilder.build()) |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 256 | .nextStep(portNextObjId) |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 257 | .fromApp(srManager.appId).makePermanent() |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 258 | .withPriority(getPriorityFromPrefix(prefix)) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 259 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
Saurav Das | 261c300 | 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 | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 265 | * |
Saurav Das | 261c300 | 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 | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 278 | * @return true if all rules are set successfully, false otherwise |
| 279 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 280 | boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets, |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 281 | DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 282 | for (IpPrefix subnet : subnets) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 283 | if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 287 | return true; |
| 288 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 289 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 290 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 291 | * Revokes IP flow rules for the subnets in each edge switch. |
Charles Chan | c22cef3 | 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 | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 296 | boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 297 | for (IpPrefix subnet : subnets) { |
| 298 | if (!revokeIpRuleForRouter(subnet)) { |
| 299 | return false; |
| 300 | } |
| 301 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 302 | return true; |
| 303 | } |
| 304 | |
| 305 | /** |
Saurav Das | e0237a3 | 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 | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 307 | * is reachable via destination device(s). |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 308 | * |
Saurav Das | 261c300 | 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 | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 319 | * @return true if all rules are set successfully, false otherwise |
| 320 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 321 | private boolean populateIpRuleForRouter(DeviceId targetSw, |
Saurav Das | 261c300 | 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 | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 326 | try { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 327 | if (ipPrefix.isIp4()) { |
Saurav Das | 261c300 | 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 | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 332 | } else { |
Saurav Das | 261c300 | 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 | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 337 | } |
Charles Chan | 319d1a2 | 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 | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 342 | |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 343 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 344 | TrafficSelector selector = sbuilder.build(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 345 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 346 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 347 | DestinationSet ds; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 348 | TrafficTreatment treatment; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 349 | |
Srikanth Vavilapalli | 6450548 | 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 | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 352 | /*if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 353 | tbuilder.immediate().decNwTtl(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 354 | ds = new DestinationSet(false, destSw); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 355 | treatment = tbuilder.build(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 356 | } else { |
Saurav Das | 261c300 | 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 | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 376 | treatment = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Saurav Das | 4c35fc4 | 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 | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 383 | metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 384 | DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 385 | if (grpHandler == null) { |
| 386 | log.warn("populateIPRuleForRouter: groupHandler for device {} " |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 387 | + "not found", targetSw); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 388 | return false; |
| 389 | } |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 390 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 391 | int nextId = grpHandler.getNextObjectiveId(ds, nextHops, |
| 392 | metabuilder.build(), true); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 393 | if (nextId <= 0) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 394 | log.warn("No next objective in {} for ds: {}", targetSw, ds); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 395 | return false; |
| 396 | } |
| 397 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 398 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 399 | .builder() |
| 400 | .fromApp(srManager.appId) |
| 401 | .makePermanent() |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 402 | .nextStep(nextId) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 403 | .withSelector(selector) |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 404 | .withPriority(getPriorityFromPrefix(ipPrefix)) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 405 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 406 | if (treatment != null) { |
| 407 | fwdBuilder.withTreatment(treatment); |
| 408 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 409 | log.debug("Installing IPv4 forwarding objective for router IP/subnet {} " |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 410 | + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 411 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 412 | (objective) -> log.debug("IP rule for router {} populated in dev:{}", |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 413 | ipPrefix, targetSw), |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 414 | (objective, error) -> |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 415 | log.warn("Failed to populate IP rule for router {}: {} in dev:{}", |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 416 | ipPrefix, error, targetSw)); |
| 417 | srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 418 | rulePopulationCounter.incrementAndGet(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 419 | |
| 420 | return true; |
| 421 | } |
| 422 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 423 | /** |
Charles Chan | c22cef3 | 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 | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 429 | private boolean revokeIpRuleForRouter(IpPrefix ipPrefix) { |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 430 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix); |
Charles Chan | c22cef3 | 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 | 3ed34d8 | 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 | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 451 | |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | /** |
Pier Ventre | 229fd0b | 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 | 261c300 | 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 | 229fd0b | 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 | 0be3f0f | 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 | 229fd0b | 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 | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 485 | metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN); |
Pier Ventre | 229fd0b | 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 | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 490 | + "label {} in switch {} with pop to next-hops {}", |
| 491 | segmentId, targetSwId, nextHops); |
Pier Ventre | 229fd0b | 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 | 2cb3829 | 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 | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 501 | routerIp, |
| 502 | destSwId); |
Pier Ventre | 229fd0b | 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 | 261c300 | 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 | 229fd0b | 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 | 2cb3829 | 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 | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 523 | routerIp, |
| 524 | destSwId); |
Pier Ventre | 229fd0b | 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 | e0237a3 | 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 | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 563 | * |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 564 | * @param targetSwId target device ID of the switch to set the rules |
sangho | 80f11cb | 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 | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 567 | * @param routerIp the router ip |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 568 | * @return true if all rules are set successfully, false otherwise |
| 569 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 570 | boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId, |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 571 | Set<DeviceId> nextHops, |
| 572 | IpAddress routerIp) { |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 573 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 574 | int segmentId; |
| 575 | try { |
Pier Ventre | adb4ae6 | 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 | 319d1a2 | 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 | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 585 | |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 586 | List<ForwardingObjective> fwdObjs = new ArrayList<>(); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 587 | Collection<ForwardingObjective> fwdObjsMpls; |
Pier Ventre | 229fd0b | 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 | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 592 | } |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 593 | fwdObjs.addAll(fwdObjsMpls); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 594 | |
| 595 | // Generates the transit rules used by the MPLS Pwaas. |
| 596 | int pwSrLabel; |
| 597 | try { |
| 598 | pwSrLabel = config.getPWRoutingLabel(destSwId); |
| 599 | } catch (DeviceConfigNotFoundException e) { |
| 600 | log.warn(e.getMessage() + " Aborting populateMplsRule. No label for PseudoWire traffic."); |
| 601 | return false; |
| 602 | } |
| 603 | fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel, routerIp, false); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 604 | if (fwdObjsMpls.isEmpty()) { |
| 605 | return false; |
| 606 | } |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 607 | fwdObjs.addAll(fwdObjsMpls); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 608 | |
| 609 | for (ForwardingObjective fwdObj : fwdObjs) { |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 610 | log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}", |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 611 | fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId); |
| 612 | srManager.flowObjectiveService.forward(targetSwId, fwdObj); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 613 | rulePopulationCounter.incrementAndGet(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | return true; |
| 617 | } |
| 618 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 619 | private ForwardingObjective.Builder getMplsForwardingObjective( |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 620 | DeviceId targetSw, |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 621 | Set<DeviceId> nextHops, |
| 622 | boolean phpRequired, |
| 623 | boolean isBos, |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 624 | TrafficSelector meta, |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 625 | IpAddress routerIp, |
| 626 | DeviceId destSw) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 627 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 628 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 629 | .builder().withFlag(ForwardingObjective.Flag.SPECIFIC); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 630 | |
| 631 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 632 | |
| 633 | if (phpRequired) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 634 | // php case - pop should always be flow-action |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 635 | log.debug("getMplsForwardingObjective: php required"); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 636 | tbuilder.deferred().copyTtlIn(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 637 | if (isBos) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 638 | if (routerIp.isIp4()) { |
| 639 | tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType()); |
| 640 | } else { |
| 641 | tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType()); |
| 642 | } |
| 643 | tbuilder.decNwTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 644 | } else { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 645 | tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType()) |
| 646 | .decMplsTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 647 | } |
| 648 | } else { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 649 | // swap with self case - SR CONTINUE |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 650 | log.debug("getMplsForwardingObjective: php not required"); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 651 | tbuilder.deferred().decMplsTtl(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 654 | fwdBuilder.withTreatment(tbuilder.build()); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 655 | // if MPLS-ECMP == True we will build a standard NeighborSet. |
| 656 | // Otherwise a RandomNeighborSet. |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 657 | DestinationSet ns = DestinationSet.destinationSet(false, false, destSw); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 658 | if (!isBos && this.srManager.getMplsEcmp()) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 659 | ns = DestinationSet.destinationSet(false, true, destSw); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 660 | } else if (!isBos && !this.srManager.getMplsEcmp()) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 661 | ns = DestinationSet.destinationSet(true, true, destSw); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 662 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 663 | |
Saurav Das | 9455d70 | 2017-03-24 19:03:58 -0700 | [diff] [blame] | 664 | log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}", |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 665 | targetSw, ns); |
| 666 | DefaultGroupHandler gh = srManager.getGroupHandler(targetSw); |
| 667 | if (gh == null) { |
| 668 | log.warn("getNextObjectiveId query - groupHandler for device {} " |
| 669 | + "not found", targetSw); |
| 670 | return null; |
| 671 | } |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 672 | // If BoS == True, all forwarding is via L3 ECMP group. |
| 673 | // If Bos == False, the forwarding can be via MPLS-ECMP group or through |
| 674 | // MPLS-Interface group. This depends on the configuration of the option |
| 675 | // MPLS-ECMP. |
| 676 | // The metadata informs the driver that the next-Objective will be used |
| 677 | // by MPLS flows and if Bos == False the driver will use MPLS groups. |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 678 | Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>(); |
| 679 | dstNextHops.put(destSw, nextHops); |
| 680 | int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 681 | if (nextId <= 0) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 682 | log.warn("No next objective in {} for ns: {}", targetSw, ns); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 683 | return null; |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 684 | } else { |
| 685 | log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}", |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 686 | nextId, targetSw, ns); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 689 | fwdBuilder.nextStep(nextId); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 690 | return fwdBuilder; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 694 | * Creates a filtering objective to permit all untagged packets with a |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 695 | * dstMac corresponding to the router's MAC address. For those pipelines |
| 696 | * that need to internally assign vlans to untagged packets, this method |
| 697 | * provides per-subnet vlan-ids as metadata. |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 698 | * <p> |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 699 | * Note that the vlan assignment and filter programming should only be done by |
| 700 | * the master for a switch. This method is typically called at deviceAdd and |
| 701 | * programs filters only for the enabled ports of the device. For port-updates, |
| 702 | * that enable/disable ports after device add, singlePortFilter methods should |
| 703 | * be called. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 704 | * |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 705 | * @param deviceId the switch dpid for the router |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 706 | * @return PortFilterInfo information about the processed ports |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 707 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 708 | PortFilterInfo populateVlanMacFilters(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 709 | log.debug("Installing per-port filtering objective for untagged " |
| 710 | + "packets in device {}", deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 711 | |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 712 | List<Port> devPorts = srManager.deviceService.getPorts(deviceId); |
Jon Hall | 31d8478 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 713 | if (devPorts == null || devPorts.isEmpty()) { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 714 | log.warn("Device {} ports not available. Unable to add MacVlan filters", |
| 715 | deviceId); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 716 | return null; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 717 | } |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 718 | int disabledPorts = 0, errorPorts = 0, filteredPorts = 0; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 719 | for (Port port : devPorts) { |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 720 | if (!port.isEnabled()) { |
| 721 | disabledPorts++; |
| 722 | continue; |
| 723 | } |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 724 | if (processSinglePortFilters(deviceId, port.number(), true)) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 725 | filteredPorts++; |
| 726 | } else { |
| 727 | errorPorts++; |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 728 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 729 | } |
Charles Chan | 077314e | 2017-06-22 14:27:17 -0700 | [diff] [blame] | 730 | log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}", |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 731 | deviceId, disabledPorts, errorPorts, filteredPorts); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 732 | return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts, |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 733 | errorPorts, filteredPorts); |
| 734 | } |
| 735 | |
| 736 | /** |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 737 | * Creates or removes filtering objectives for a single port. Should only be |
| 738 | * called by the master for a switch. |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 739 | * |
| 740 | * @param deviceId device identifier |
| 741 | * @param portnum port identifier for port to be filtered |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 742 | * @param install true to install the filtering objective, false to remove |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 743 | * @return true if no errors occurred during the build of the filtering objective |
| 744 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 745 | boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 746 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum); |
| 747 | VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint); |
| 748 | Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint); |
| 749 | VlanId nativeVlan = srManager.getNativeVlanId(connectPoint); |
| 750 | |
| 751 | if (taggedVlans.size() != 0) { |
| 752 | // Filter for tagged vlans |
| 753 | if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId -> |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 754 | processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 755 | return false; |
| 756 | } |
| 757 | if (nativeVlan != null) { |
| 758 | // Filter for native vlan |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 759 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 760 | return false; |
| 761 | } |
| 762 | } |
| 763 | } else if (untaggedVlan != null) { |
| 764 | // Filter for untagged vlan |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 765 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 766 | return false; |
| 767 | } |
| 768 | } else { |
Saurav Das | fbe7457 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 769 | // Unconfigured port, use INTERNAL_VLAN |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 770 | if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 771 | return false; |
| 772 | } |
| 773 | } |
| 774 | return true; |
| 775 | } |
| 776 | |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 777 | /** |
| 778 | * Updates filtering objectives for a single port. Should only be called by |
| 779 | * the master for a switch |
| 780 | * @param deviceId device identifier |
| 781 | * @param portNum port identifier for port to be filtered |
| 782 | * @param pushVlan true to push vlan, false otherwise |
| 783 | * @param vlanId vlan identifier |
| 784 | * @param install true to install the filtering objective, false to remove |
| 785 | */ |
| 786 | void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum, |
| 787 | boolean pushVlan, VlanId vlanId, boolean install) { |
| 788 | if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) { |
| 789 | log.warn("Failed to update FilteringObjective for {}/{} with vlan {}", |
| 790 | deviceId, portNum, vlanId); |
| 791 | } |
| 792 | } |
| 793 | |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 794 | private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum, |
| 795 | boolean pushVlan, VlanId vlanId, boolean install) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 796 | FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 797 | if (fob == null) { |
| 798 | // error encountered during build |
| 799 | return false; |
| 800 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 801 | log.debug("{} filtering objectives for dev/port: {}/{}", |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 802 | install ? "Installing" : "Removing", deviceId, portnum); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 803 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 804 | (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum, |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 805 | install ? "installed" : "removed"), |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 806 | (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}", |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 807 | install ? "install" : "remove", deviceId, portnum, error)); |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 808 | if (install) { |
| 809 | srManager.flowObjectiveService.filter(deviceId, fob.add(context)); |
| 810 | } else { |
| 811 | srManager.flowObjectiveService.filter(deviceId, fob.remove(context)); |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 812 | } |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 813 | return true; |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 816 | private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum, |
| 817 | boolean pushVlan, VlanId vlanId) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 818 | MacAddress deviceMac; |
| 819 | try { |
| 820 | deviceMac = config.getDeviceMac(deviceId); |
| 821 | } catch (DeviceConfigNotFoundException e) { |
| 822 | log.warn(e.getMessage() + " Processing SinglePortFilters aborted"); |
| 823 | return null; |
| 824 | } |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 825 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 826 | fob.withKey(Criteria.matchInPort(portnum)) |
| 827 | .addCondition(Criteria.matchEthDst(deviceMac)) |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 828 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 829 | |
| 830 | if (pushVlan) { |
| 831 | fob.addCondition(Criteria.matchVlanId(VlanId.NONE)); |
| 832 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
| 833 | .pushVlan().setVlanId(vlanId).build(); |
| 834 | fob.withMeta(tt); |
| 835 | } else { |
| 836 | fob.addCondition(Criteria.matchVlanId(vlanId)); |
| 837 | } |
| 838 | |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 839 | fob.permit().fromApp(srManager.appId); |
| 840 | return fob; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 844 | * Creates a forwarding objective to punt all IP packets, destined to the |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 845 | * router's port IP addresses, to the controller. Note that the input |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 846 | * port should not be matched on, as these packets can come from any input. |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 847 | * Furthermore, these are applied only by the master instance. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 848 | * |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 849 | * @param deviceId the switch dpid for the router |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 850 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 851 | void populateIpPunts(DeviceId deviceId) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 852 | Ip4Address routerIpv4, pairRouterIpv4 = null; |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame^] | 853 | Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 854 | try { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 855 | routerIpv4 = config.getRouterIpv4(deviceId); |
| 856 | routerIpv6 = config.getRouterIpv6(deviceId); |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame^] | 857 | routerLinkLocalIpv6 = Ip6Address.valueOf( |
| 858 | IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes())); |
| 859 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 860 | if (config.isPairedEdge(deviceId)) { |
| 861 | pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId)); |
| 862 | pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId)); |
| 863 | } |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 864 | } catch (DeviceConfigNotFoundException e) { |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 865 | log.warn(e.getMessage() + " Aborting populateIpPunts."); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 866 | return; |
| 867 | } |
| 868 | |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 869 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 870 | log.debug("Not installing port-IP punts - not the master for dev:{} ", |
| 871 | deviceId); |
| 872 | return; |
| 873 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 874 | Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId)); |
| 875 | allIps.add(routerIpv4); |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame^] | 876 | allIps.add(routerLinkLocalIpv6); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 877 | if (routerIpv6 != null) { |
| 878 | allIps.add(routerIpv6); |
| 879 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 880 | if (pairRouterIpv4 != null) { |
| 881 | allIps.add(pairRouterIpv4); |
| 882 | } |
| 883 | if (pairRouterIpv6 != null) { |
| 884 | allIps.add(pairRouterIpv6); |
| 885 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 886 | for (IpAddress ipaddr : allIps) { |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 887 | populateSingleIpPunts(deviceId, ipaddr); |
| 888 | } |
| 889 | } |
Charles Chan | 2d0bbcd | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 890 | |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 891 | /** |
| 892 | * Creates a forwarding objective to punt all IP packets, destined to the |
| 893 | * specified IP address, which should be router's port IP address. |
| 894 | * |
| 895 | * @param deviceId the switch dpid for the router |
| 896 | * @param ipAddress the IP address of the router's port |
| 897 | */ |
| 898 | void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) { |
| 899 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress); |
| 900 | Optional<DeviceId> optDeviceId = Optional.of(deviceId); |
| 901 | |
| 902 | srManager.packetService.requestPackets(sbuilder.build(), |
| 903 | PacketPriority.CONTROL, srManager.appId, optDeviceId); |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Removes a forwarding objective to punt all IP packets, destined to the |
| 908 | * specified IP address, which should be router's port IP address. |
| 909 | * |
| 910 | * @param deviceId the switch dpid for the router |
| 911 | * @param ipAddress the IP address of the router's port |
| 912 | */ |
| 913 | void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) { |
| 914 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress); |
| 915 | Optional<DeviceId> optDeviceId = Optional.of(deviceId); |
| 916 | |
| 917 | try { |
| 918 | if (!ipAddress.equals(config.getRouterIpv4(deviceId)) && |
| 919 | !ipAddress.equals(config.getRouterIpv6(deviceId))) { |
| 920 | srManager.packetService.cancelPackets(sbuilder.build(), |
| 921 | PacketPriority.CONTROL, srManager.appId, optDeviceId); |
| 922 | } |
| 923 | } catch (DeviceConfigNotFoundException e) { |
| 924 | log.warn(e.getMessage() + " Aborting revokeSingleIpPunts"); |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 925 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 928 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 929 | * Method to build IPv4 or IPv6 selector. |
| 930 | * |
| 931 | * @param addressToMatch the address to match |
| 932 | */ |
| 933 | private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) { |
| 934 | return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix()); |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * Method to build IPv4 or IPv6 selector. |
| 939 | * |
| 940 | * @param prefixToMatch the prefix to match |
| 941 | */ |
| 942 | private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) { |
| 943 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 944 | // If the prefix is IPv4 |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 945 | if (prefixToMatch.isIp4()) { |
| 946 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 947 | selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix()); |
| 948 | return selectorBuilder; |
| 949 | } |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 950 | // If the prefix is IPv6 |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 951 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 952 | selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix()); |
| 953 | return selectorBuilder; |
| 954 | } |
| 955 | |
| 956 | /** |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 957 | * Creates forwarding objectives to punt ARP and NDP packets, to the controller. |
| 958 | * Furthermore, these are applied only by the master instance. Deferred actions |
| 959 | * are not cleared such that packets can be flooded in the cross connect use case |
| 960 | * |
| 961 | * @param deviceId the switch dpid for the router |
| 962 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 963 | void populateArpNdpPunts(DeviceId deviceId) { |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 964 | // We are not the master just skip. |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 965 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 966 | log.debug("Not installing ARP/NDP punts - not the master for dev:{} ", |
| 967 | deviceId); |
| 968 | return; |
| 969 | } |
| 970 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 971 | ForwardingObjective fwdObj; |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 972 | // We punt all ARP packets towards the controller. |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame^] | 973 | fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY) |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 974 | .add(new ObjectiveContext() { |
| 975 | @Override |
| 976 | public void onError(Objective objective, ObjectiveError error) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 977 | log.warn("Failed to install forwarding objective to punt ARP to {}: {}", |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 978 | deviceId, error); |
| 979 | } |
| 980 | }); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 981 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 982 | |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 983 | // We punt all NDP packets towards the controller. |
Charles Chan | ef8d12e | 2017-12-05 21:07:38 -0800 | [diff] [blame^] | 984 | fwdObj = ndpFwdObjective(null, true, ARP_NDP_PRIORITY) |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 985 | .add(new ObjectiveContext() { |
| 986 | @Override |
| 987 | public void onError(Objective objective, ObjectiveError error) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 988 | log.warn("Failed to install forwarding objective to punt NDP to {}: {}", |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 989 | deviceId, error); |
| 990 | } |
| 991 | }); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 992 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
| 993 | |
| 994 | srManager.getPairLocalPorts(deviceId).ifPresent(port -> { |
| 995 | ForwardingObjective pairFwdObj; |
| 996 | // Do not punt ARP packets from pair port |
| 997 | pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1) |
| 998 | .add(new ObjectiveContext() { |
| 999 | @Override |
| 1000 | public void onError(Objective objective, ObjectiveError error) { |
| 1001 | log.warn("Failed to install forwarding objective to ignore ARP to {}: {}", |
| 1002 | deviceId, error); |
| 1003 | } |
| 1004 | }); |
| 1005 | srManager.flowObjectiveService.forward(deviceId, pairFwdObj); |
| 1006 | |
| 1007 | // Do not punt NDP packets from pair port |
| 1008 | pairFwdObj = ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1) |
| 1009 | .add(new ObjectiveContext() { |
| 1010 | @Override |
| 1011 | public void onError(Objective objective, ObjectiveError error) { |
| 1012 | log.warn("Failed to install forwarding objective to ignore ARP to {}: {}", |
| 1013 | deviceId, error); |
| 1014 | } |
| 1015 | }); |
| 1016 | srManager.flowObjectiveService.forward(deviceId, pairFwdObj); |
| 1017 | |
| 1018 | // Do not forward DAD packets from pair port |
| 1019 | pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2) |
| 1020 | .add(new ObjectiveContext() { |
| 1021 | @Override |
| 1022 | public void onError(Objective objective, ObjectiveError error) { |
| 1023 | log.warn("Failed to install forwarding objective to drop DAD to {}: {}", |
| 1024 | deviceId, error); |
| 1025 | } |
| 1026 | }); |
| 1027 | srManager.flowObjectiveService.forward(deviceId, pairFwdObj); |
| 1028 | }); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1031 | private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector, |
| 1032 | TrafficTreatment treatment, int priority) { |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1033 | return DefaultForwardingObjective.builder() |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1034 | .withPriority(priority) |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1035 | .withSelector(selector) |
| 1036 | .fromApp(srManager.appId) |
| 1037 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1038 | .withTreatment(treatment) |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1039 | .makePermanent(); |
| 1040 | } |
| 1041 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1042 | private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) { |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1043 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 1044 | sBuilder.matchEthType(TYPE_ARP); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1045 | if (port != null) { |
| 1046 | sBuilder.matchInPort(port); |
| 1047 | } |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1048 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1049 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1050 | if (punt) { |
| 1051 | tBuilder.punt(); |
| 1052 | } |
| 1053 | return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1056 | private ForwardingObjective.Builder ndpFwdObjective(PortNumber port, boolean punt, int priority) { |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1057 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 1058 | sBuilder.matchEthType(TYPE_IPV6) |
| 1059 | .matchIPProtocol(PROTOCOL_ICMP6) |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1060 | .matchIcmpv6Type(NEIGHBOR_SOLICITATION); |
| 1061 | if (port != null) { |
| 1062 | sBuilder.matchInPort(port); |
| 1063 | } |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1064 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1065 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1066 | if (punt) { |
| 1067 | tBuilder.punt(); |
| 1068 | } |
| 1069 | return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority); |
| 1070 | } |
| 1071 | |
| 1072 | private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) { |
| 1073 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 1074 | sBuilder.matchEthType(TYPE_IPV6) |
Charles Chan | e6bda75 | 2017-08-07 12:39:03 -0700 | [diff] [blame] | 1075 | .matchIPv6Src(Ip6Address.ZERO.toIpPrefix()); |
| 1076 | // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly |
| 1077 | // .matchIPProtocol(PROTOCOL_ICMP6) |
| 1078 | // .matchIcmpv6Type(NEIGHBOR_SOLICITATION); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1079 | if (port != null) { |
| 1080 | sBuilder.matchInPort(port); |
| 1081 | } |
| 1082 | |
| 1083 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1084 | tBuilder.wipeDeferred(); |
| 1085 | return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | /** |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1089 | * Populates a forwarding objective to send packets that miss other high |
| 1090 | * priority Bridging Table entries to a group that contains all ports of |
| 1091 | * its subnet. |
| 1092 | * |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1093 | * @param deviceId switch ID to set the rules |
| 1094 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1095 | void populateSubnetBroadcastRule(DeviceId deviceId) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1096 | srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> { |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1097 | updateSubnetBroadcastRule(deviceId, vlanId, true); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1098 | }); |
| 1099 | } |
| 1100 | |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1101 | /** |
| 1102 | * Creates or removes a forwarding objective to broadcast packets to its subnet. |
| 1103 | * @param deviceId switch ID to set the rule |
| 1104 | * @param vlanId vlan ID to specify the subnet |
| 1105 | * @param install true to install the rule, false to revoke the rule |
| 1106 | */ |
| 1107 | void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) { |
| 1108 | int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId); |
| 1109 | |
| 1110 | if (nextId < 0) { |
| 1111 | log.error("Cannot install vlan {} broadcast rule in dev:{} due" |
| 1112 | + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId); |
| 1113 | return; |
| 1114 | } |
| 1115 | |
| 1116 | // Driver should treat objective with MacAddress.NONE as the |
| 1117 | // subnet broadcast rule |
| 1118 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 1119 | sbuilder.matchVlanId(vlanId); |
| 1120 | sbuilder.matchEthDst(MacAddress.NONE); |
| 1121 | |
| 1122 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 1123 | fob.withFlag(Flag.SPECIFIC) |
| 1124 | .withSelector(sbuilder.build()) |
| 1125 | .nextStep(nextId) |
| 1126 | .withPriority(SegmentRoutingService.FLOOD_PRIORITY) |
| 1127 | .fromApp(srManager.appId) |
| 1128 | .makePermanent(); |
| 1129 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1130 | (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId), |
| 1131 | (objective, error) -> |
| 1132 | log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error)); |
| 1133 | |
| 1134 | if (install) { |
| 1135 | srManager.flowObjectiveService.forward(deviceId, fob.add(context)); |
| 1136 | } else { |
| 1137 | srManager.flowObjectiveService.forward(deviceId, fob.remove(context)); |
| 1138 | } |
| 1139 | } |
| 1140 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1141 | private int getPriorityFromPrefix(IpPrefix prefix) { |
| 1142 | return (prefix.isIp4()) ? |
| 1143 | 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY : |
| 1144 | 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY; |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 1145 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1146 | |
| 1147 | /** |
| 1148 | * Update Forwarding objective for each host and IP address connected to given port. |
| 1149 | * And create corresponding Simple Next objective if it does not exist. |
| 1150 | * Applied only when populating Forwarding objective |
| 1151 | * @param deviceId switch ID to set the rule |
| 1152 | * @param portNumber port number |
| 1153 | * @param prefix IP prefix of the route |
| 1154 | * @param hostMac MAC address of the next hop |
| 1155 | * @param vlanId Vlan ID of the port |
| 1156 | * @param popVlan true to pop vlan tag in TrafficTreatment |
| 1157 | * @param install true to populate the forwarding objective, false to revoke |
| 1158 | */ |
| 1159 | void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac, |
| 1160 | VlanId vlanId, boolean popVlan, boolean install) { |
| 1161 | ForwardingObjective.Builder fob; |
| 1162 | TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix); |
| 1163 | MacAddress deviceMac; |
| 1164 | try { |
| 1165 | deviceMac = config.getDeviceMac(deviceId); |
| 1166 | } catch (DeviceConfigNotFoundException e) { |
| 1167 | log.warn(e.getMessage() + " Aborting updateFwdObj."); |
| 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 1172 | tbuilder.deferred() |
| 1173 | .setEthDst(hostMac) |
| 1174 | .setEthSrc(deviceMac) |
| 1175 | .setOutput(portNumber); |
| 1176 | |
| 1177 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 1178 | |
| 1179 | if (!popVlan) { |
| 1180 | tbuilder.setVlanId(vlanId); |
| 1181 | } else { |
| 1182 | mbuilder.matchVlanId(vlanId); |
| 1183 | } |
| 1184 | |
| 1185 | // if the objective is to revoke an existing rule, and for some reason |
| 1186 | // the next-objective does not exist, then a new one should not be created |
| 1187 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber, |
| 1188 | tbuilder.build(), mbuilder.build(), install); |
| 1189 | if (portNextObjId == -1) { |
| 1190 | // Warning log will come from getPortNextObjective method |
| 1191 | return; |
| 1192 | } |
| 1193 | |
| 1194 | fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build()) |
| 1195 | .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent() |
| 1196 | .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 1197 | |
| 1198 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1199 | (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"), |
| 1200 | (objective, error) -> |
| 1201 | log.warn("Failed to {} IP rule for route {}: {}", |
| 1202 | install ? "install" : "revoke", prefix, error)); |
| 1203 | srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context)); |
| 1204 | |
| 1205 | if (!install) { |
| 1206 | DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId); |
| 1207 | if (grpHandler == null) { |
| 1208 | log.warn("updateFwdObj: groupHandler for device {} not found", deviceId); |
| 1209 | } else { |
| 1210 | // Remove L3UG for the given port and host |
| 1211 | grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build()); |
| 1212 | } |
| 1213 | } |
| 1214 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1215 | } |