blob: f37caea84c1ad2232ef45a48489fd16ee4c77d78 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-04-01 13:05:26 -07003 *
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 */
16package org.onosproject.segmentrouting;
17
Pier Ventre229fd0b2016-10-31 16:49:19 -070018import com.google.common.collect.Lists;
Charles Chan051490d2018-01-11 11:48:18 -080019import com.google.common.collect.Sets;
Saurav Das4c35fc42015-11-20 15:27:53 -080020import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070021import org.onlab.packet.Ethernet;
Charles Chanef8d12e2017-12-05 21:07:38 -080022import org.onlab.packet.IPv6;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080024import org.onlab.packet.Ip6Address;
25import org.onlab.packet.IpAddress;
sangho80f11cb2015-04-01 13:05:26 -070026import org.onlab.packet.IpPrefix;
27import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070028import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070029import org.onlab.packet.VlanId;
Charles Chanb7f75ac2016-01-11 18:28:54 -080030import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070031import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080032import org.onosproject.net.flowobjective.Objective;
Charles Chan1eaf4802016-04-18 13:44:03 -070033import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080034import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080035import org.onosproject.net.packet.PacketPriority;
Saurav Dasd1872b02016-12-02 15:43:47 -080036import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan319d1a22015-11-03 10:42:14 -080037import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
38import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Das62ae6792017-05-15 15:34:25 -070039import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -070040import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sangho80f11cb2015-04-01 13:05:26 -070041import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070042import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070044import org.onosproject.net.flow.DefaultTrafficSelector;
45import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070046import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070048import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flowobjective.DefaultFilteringObjective;
50import org.onosproject.net.flowobjective.DefaultForwardingObjective;
51import org.onosproject.net.flowobjective.FilteringObjective;
52import org.onosproject.net.flowobjective.ForwardingObjective;
53import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070054import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070055import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
57
58import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070059import java.util.Collection;
60import java.util.Collections;
Saurav Das261c3002017-06-13 15:35:54 -070061import java.util.HashMap;
Saurav Dasc28b3432015-10-30 17:45:38 -070062import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070063import java.util.List;
Saurav Das261c3002017-06-13 15:35:54 -070064import java.util.Map;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080065import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070066import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070067import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070068
69import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080070import static org.onlab.packet.Ethernet.TYPE_ARP;
71import static org.onlab.packet.Ethernet.TYPE_IPV6;
Charles Chan051490d2018-01-11 11:48:18 -080072import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT;
Pier Luigib9632ba2017-01-12 18:14:58 -080073import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
Charles Chan051490d2018-01-11 11:48:18 -080074import static org.onlab.packet.ICMP6.ROUTER_ADVERTISEMENT;
75import static org.onlab.packet.ICMP6.ROUTER_SOLICITATION;
Pier Luigib9632ba2017-01-12 18:14:58 -080076import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan10b0fb72017-02-02 16:20:42 -080077import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sangho80f11cb2015-04-01 13:05:26 -070078
Charles Chanb7f75ac2016-01-11 18:28:54 -080079/**
80 * Populator of segment routing flow rules.
81 */
sangho80f11cb2015-04-01 13:05:26 -070082public class RoutingRulePopulator {
Charles Chanef8d12e2017-12-05 21:07:38 -080083 private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class);
84
85 private static final int ARP_NDP_PRIORITY = 30000;
sangho80f11cb2015-04-01 13:05:26 -070086
sanghofb7c7292015-04-13 15:15:58 -070087 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070088 private SegmentRoutingManager srManager;
89 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070090
sangho80f11cb2015-04-01 13:05:26 -070091 /**
92 * Creates a RoutingRulePopulator object.
93 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070094 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070095 */
Charles Chan3ed34d82017-06-22 18:03:14 -070096 RoutingRulePopulator(SegmentRoutingManager srManager) {
sangho80f11cb2015-04-01 13:05:26 -070097 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070098 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -070099 this.rulePopulationCounter = new AtomicLong(0);
100 }
101
102 /**
103 * Resets the population counter.
104 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700105 void resetCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700106 rulePopulationCounter.set(0);
107 }
108
109 /**
110 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700111 *
112 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700113 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700114 long getCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700115 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700116 }
117
118 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700119 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700120 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700121 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700122 * @param deviceId device ID of the device that next hop attaches to
123 * @param prefix IP prefix of the route
124 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800125 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700126 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700127 */
Charles Chan910be6a2017-08-23 14:46:43 -0700128 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800129 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das261c3002017-06-13 15:35:54 -0700130 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700131 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800132 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800133 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700134 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
135 hostVlanId, outPort, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800136 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700137 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan319d1a22015-11-03 10:42:14 -0800138 return;
139 }
Saurav Das07c74602016-04-27 18:35:50 -0700140 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700141 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700142 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700143 return;
144 }
Charles Chan910be6a2017-08-23 14:46:43 -0700145
146 int nextId = fwdBuilder.add().nextId();
Charles Chan1eaf4802016-04-18 13:44:03 -0700147 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan910be6a2017-08-23 14:46:43 -0700148 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
149 prefix, nextId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700150 (objective, error) ->
Saurav Das261c3002017-06-13 15:35:54 -0700151 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das368cf212017-03-15 15:15:14 -0700152 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700153 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800154 rulePopulationCounter.incrementAndGet();
155 }
156
Charles Chanb7f75ac2016-01-11 18:28:54 -0800157 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700158 * Removes IP rules for a route when the next hop is gone.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800159 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700160 * @param deviceId device ID of the device that next hop attaches to
161 * @param prefix IP prefix of the route
162 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800163 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700164 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800165 */
Charles Chan910be6a2017-08-23 14:46:43 -0700166 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800167 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700168 log.debug("Revoke IP table entry for route {} at {}:{}",
169 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800170 ForwardingObjective.Builder fwdBuilder;
171 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700172 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
173 hostVlanId, outPort, true);
Charles Chanf4586112015-11-09 16:37:23 -0800174 } catch (DeviceConfigNotFoundException e) {
175 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
176 return;
177 }
Charles Chanea702b12016-11-30 11:55:05 -0800178 if (fwdBuilder == null) {
179 log.warn("Aborting host routing table entries due "
180 + "to error for dev:{} route:{}", deviceId, prefix);
181 return;
182 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700183 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700184 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700185 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700186 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700187 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800188 }
189
Charles Chanddac7fd2016-10-27 14:19:48 -0700190 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800191 * Returns a forwarding objective builder for routing rules.
192 * <p>
193 * The forwarding objective routes packets destined to a given prefix to
194 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700195 *
196 * @param deviceId device ID
197 * @param prefix prefix that need to be routed
198 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800199 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700200 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700201 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chanddac7fd2016-10-27 14:19:48 -0700202 * @return forwarding objective builder
203 * @throws DeviceConfigNotFoundException if given device is not configured
204 */
Charles Chan18fa4252017-02-08 16:10:40 -0800205 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700206 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700207 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
208 boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800209 throws DeviceConfigNotFoundException {
210 MacAddress deviceMac;
211 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800212
Charles Chan90772a72017-02-08 15:52:08 -0800213 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
214 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
215 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
216 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700217
Charles Chan90772a72017-02-08 15:52:08 -0800218 // Create route selector
219 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
220
221 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700222 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700223 tbuilder.deferred()
224 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800225 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700226 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800227
Charles Chan90772a72017-02-08 15:52:08 -0800228 // Create route meta
229 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800230
Charles Chan90772a72017-02-08 15:52:08 -0800231 // Adjust the meta according to VLAN configuration
232 if (taggedVlans.contains(hostVlanId)) {
233 tbuilder.setVlanId(hostVlanId);
234 } else if (hostVlanId.equals(VlanId.NONE)) {
235 if (untaggedVlan != null) {
236 mbuilder.matchVlanId(untaggedVlan);
237 } else if (nativeVlan != null) {
238 mbuilder.matchVlanId(nativeVlan);
239 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700240 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
241 hostMac, hostVlanId, connectPoint);
242 return null;
Charles Chan90772a72017-02-08 15:52:08 -0800243 }
244 } else {
Saurav Das2cb38292017-03-29 19:09:17 -0700245 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
246 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das07c74602016-04-27 18:35:50 -0700247 return null;
248 }
Saurav Das2cb38292017-03-29 19:09:17 -0700249 // if the objective is to revoke an existing rule, and for some reason
250 // the next-objective does not exist, then a new one should not be created
Charles Chan90772a72017-02-08 15:52:08 -0800251 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das2cb38292017-03-29 19:09:17 -0700252 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan90772a72017-02-08 15:52:08 -0800253 if (portNextObjId == -1) {
254 // Warning log will come from getPortNextObjective method
255 return null;
256 }
257
Charles Chanf4586112015-11-09 16:37:23 -0800258 return DefaultForwardingObjective.builder()
Charles Chan90772a72017-02-08 15:52:08 -0800259 .withSelector(sbuilder.build())
Saurav Das2d94d312015-11-24 23:21:05 -0800260 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800261 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700262 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800263 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700264 }
265
266 /**
Saurav Das261c3002017-06-13 15:35:54 -0700267 * Populates IP flow rules for all the given prefixes reachable from the
268 * destination switch(es).
sangho80f11cb2015-04-01 13:05:26 -0700269 *
Saurav Das261c3002017-06-13 15:35:54 -0700270 * @param targetSw switch where rules are to be programmed
271 * @param subnets subnets/prefixes being added
272 * @param destSw1 destination switch where the prefixes are reachable
273 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
274 * Should be null if there is no paired destination switch (by config)
275 * or if the given prefixes are reachable only via destSw1
276 * @param nextHops a map containing a set of next-hops for each destination switch.
277 * If destSw2 is not null, then this map must contain an
278 * entry for destSw2 with its next-hops from the targetSw
279 * (although the next-hop set may be empty in certain scenarios).
280 * If destSw2 is null, there should not be an entry in this
281 * map for destSw2.
sangho80f11cb2015-04-01 13:05:26 -0700282 * @return true if all rules are set successfully, false otherwise
283 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700284 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das261c3002017-06-13 15:35:54 -0700285 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700286 for (IpPrefix subnet : subnets) {
Saurav Das261c3002017-06-13 15:35:54 -0700287 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sangho80f11cb2015-04-01 13:05:26 -0700288 return false;
289 }
290 }
Charles Chanc22cef32016-04-29 14:38:22 -0700291 return true;
292 }
sangho80f11cb2015-04-01 13:05:26 -0700293
Charles Chanc22cef32016-04-29 14:38:22 -0700294 /**
Saurav Das261c3002017-06-13 15:35:54 -0700295 * Revokes IP flow rules for the subnets in each edge switch.
Charles Chanc22cef32016-04-29 14:38:22 -0700296 *
297 * @param subnets subnet being removed
298 * @return true if all rules are removed successfully, false otherwise
299 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700300 boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700301 for (IpPrefix subnet : subnets) {
302 if (!revokeIpRuleForRouter(subnet)) {
303 return false;
304 }
305 }
sangho80f11cb2015-04-01 13:05:26 -0700306 return true;
307 }
308
309 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700310 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das261c3002017-06-13 15:35:54 -0700311 * is reachable via destination device(s).
sangho80f11cb2015-04-01 13:05:26 -0700312 *
Saurav Das261c3002017-06-13 15:35:54 -0700313 * @param targetSw target device ID to set the rules
314 * @param ipPrefix the IP prefix
315 * @param destSw1 destination switch where the prefixes are reachable
316 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
317 * Should be null if there is no paired destination switch (by config)
318 * or if the given prefixes are reachable only via destSw1
319 * @param nextHops map of destination switches and their next-hops.
320 * Should only contain destination switches that are
321 * actually meant to be routed to. If destSw2 is null, there
322 * should not be an entry for destSw2 in this map.
sangho80f11cb2015-04-01 13:05:26 -0700323 * @return true if all rules are set successfully, false otherwise
324 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700325 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das261c3002017-06-13 15:35:54 -0700326 IpPrefix ipPrefix, DeviceId destSw1,
327 DeviceId destSw2,
328 Map<DeviceId, Set<DeviceId>> nextHops) {
329 int segmentId1, segmentId2 = -1;
Charles Chan319d1a22015-11-03 10:42:14 -0800330 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800331 if (ipPrefix.isIp4()) {
Saurav Das261c3002017-06-13 15:35:54 -0700332 segmentId1 = config.getIPv4SegmentId(destSw1);
333 if (destSw2 != null) {
334 segmentId2 = config.getIPv4SegmentId(destSw2);
335 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800336 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700337 segmentId1 = config.getIPv6SegmentId(destSw1);
338 if (destSw2 != null) {
339 segmentId2 = config.getIPv6SegmentId(destSw2);
340 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800341 }
Charles Chan319d1a22015-11-03 10:42:14 -0800342 } catch (DeviceConfigNotFoundException e) {
343 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
344 return false;
345 }
sangho80f11cb2015-04-01 13:05:26 -0700346
Pier Ventreadb4ae62016-11-23 09:57:42 -0800347 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800348 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700349
Charles Chanf4586112015-11-09 16:37:23 -0800350 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das261c3002017-06-13 15:35:54 -0700351 DestinationSet ds;
Charles Chanf4586112015-11-09 16:37:23 -0800352 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700353
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700354 // If the next hop is the same as the final destination, then MPLS label
355 // is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700356 /*if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800357 tbuilder.immediate().decNwTtl();
Saurav Das261c3002017-06-13 15:35:54 -0700358 ds = new DestinationSet(false, destSw);
Charles Chanf4586112015-11-09 16:37:23 -0800359 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700360 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700361 ds = new DestinationSet(false, segmentId, destSw);
362 treatment = null;
363 }*/
364 if (destSw2 == null) {
365 // single dst - create destination set based on next-hop
366 Set<DeviceId> nhd1 = nextHops.get(destSw1);
367 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
368 tbuilder.immediate().decNwTtl();
369 ds = new DestinationSet(false, destSw1);
370 treatment = tbuilder.build();
371 } else {
372 ds = new DestinationSet(false, segmentId1, destSw1);
373 treatment = null;
374 }
375 } else {
376 // dst pair - IP rules for dst-pairs are always from other edge nodes
377 // the destination set needs to have both destinations, even if there
378 // are no next hops to one of them
379 ds = new DestinationSet(false, segmentId1, destSw1, segmentId2, destSw2);
Charles Chanf4586112015-11-09 16:37:23 -0800380 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700381 }
382
Saurav Das4c35fc42015-11-20 15:27:53 -0800383 // setup metadata to pass to nextObjective - indicate the vlan on egress
384 // if needed by the switch pipeline. Since neighbor sets are always to
385 // other neighboring routers, there is no subnet assigned on those ports.
386 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800387 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das261c3002017-06-13 15:35:54 -0700388 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700389 if (grpHandler == null) {
390 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das261c3002017-06-13 15:35:54 -0700391 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700392 return false;
393 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800394
Saurav Das261c3002017-06-13 15:35:54 -0700395 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
396 metabuilder.build(), true);
Saurav Das4c35fc42015-11-20 15:27:53 -0800397 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700398 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho2165d222015-05-01 09:38:25 -0700399 return false;
400 }
401
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700402 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
403 .builder()
404 .fromApp(srManager.appId)
405 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800406 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700407 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800408 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700409 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800410 if (treatment != null) {
411 fwdBuilder.withTreatment(treatment);
412 }
Saurav Das62ae6792017-05-15 15:34:25 -0700413 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das261c3002017-06-13 15:35:54 -0700414 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700415 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700416 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700417 ipPrefix, targetSw),
Charles Chan1eaf4802016-04-18 13:44:03 -0700418 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700419 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700420 ipPrefix, error, targetSw));
421 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700422 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700423
424 return true;
425 }
426
sangho80f11cb2015-04-01 13:05:26 -0700427 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700428 * Revokes IP flow rules for the router IP address.
429 *
430 * @param ipPrefix the IP address of the destination router
431 * @return true if all rules are removed successfully, false otherwise
432 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700433 private boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800434 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700435 TrafficSelector selector = sbuilder.build();
436 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
437
438 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
439 .builder()
440 .fromApp(srManager.appId)
441 .makePermanent()
442 .withSelector(selector)
443 .withTreatment(dummyTreatment)
444 .withPriority(getPriorityFromPrefix(ipPrefix))
445 .withFlag(ForwardingObjective.Flag.SPECIFIC);
446
Charles Chana8d91532018-01-30 13:31:30 -0800447 srManager.deviceService.getAvailableDevices().forEach(device -> {
448 if (srManager.mastershipService.isLocalMaster(device.id())) {
449 ObjectiveContext context = new DefaultObjectiveContext(
450 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, device.id()),
451 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
452 ipPrefix, device.id(), error));
453 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
454 } else {
455 log.debug("Not the master of {}. Abort route {} removal", device.id(), ipPrefix);
456 }
457 });
Charles Chanc22cef32016-04-29 14:38:22 -0700458
459 return true;
460 }
461
462 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700463 * Deals with !MPLS Bos use case.
464 *
465 * @param targetSwId the target sw
466 * @param destSwId the destination sw
467 * @param nextHops the set of next hops
468 * @param segmentId the segmentId to match
469 * @param routerIp the router ip
470 * @return a collection of fwdobjective
471 */
Saurav Das261c3002017-06-13 15:35:54 -0700472 private Collection<ForwardingObjective> handleMpls(
473 DeviceId targetSwId,
474 DeviceId destSwId,
475 Set<DeviceId> nextHops,
476 int segmentId,
477 IpAddress routerIp,
478 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700479
480 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
481 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800482 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700483 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
484 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
485 sbuilder.matchMplsBos(isMplsBos);
486 TrafficSelector selector = sbuilder.build();
487
488 // setup metadata to pass to nextObjective - indicate the vlan on egress
489 // if needed by the switch pipeline. Since mpls next-hops are always to
490 // other neighboring routers, there is no subnet assigned on those ports.
491 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800492 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700493
494 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
495 // If the next hop is the destination router for the segment, do pop
496 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700497 + "label {} in switch {} with pop to next-hops {}",
498 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700499 // Not-bos pop case (php for the current label). If MPLS-ECMP
500 // has been configured, the application we will request the
501 // installation for an MPLS-ECMP group.
Saurav Das2cb38292017-03-29 19:09:17 -0700502 ForwardingObjective.Builder fwdObjNoBosBuilder =
503 getMplsForwardingObjective(targetSwId,
504 nextHops,
505 true,
506 isMplsBos,
507 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700508 routerIp,
509 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700510 // Error case, we cannot handle, exit.
511 if (fwdObjNoBosBuilder == null) {
512 return Collections.emptyList();
513 }
514 fwdObjBuilders.add(fwdObjNoBosBuilder);
515
516 } else {
517 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700518 log.debug("Installing MPLS forwarding objective for "
519 + "label {} in switch {} without pop to next-hops {}",
520 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700521 // Not-bos pop case. If MPLS-ECMP has been configured, the
522 // application we will request the installation for an MPLS-ECMP
523 // group.
Saurav Das2cb38292017-03-29 19:09:17 -0700524 ForwardingObjective.Builder fwdObjNoBosBuilder =
525 getMplsForwardingObjective(targetSwId,
526 nextHops,
527 false,
528 isMplsBos,
529 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700530 routerIp,
531 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700532 // Error case, we cannot handle, exit.
533 if (fwdObjNoBosBuilder == null) {
534 return Collections.emptyList();
535 }
536 fwdObjBuilders.add(fwdObjNoBosBuilder);
537
538 }
539
540 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
541 // We add the final property to the fwdObjs.
542 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
543
544 ((Builder) ((Builder) fwdObjBuilder
545 .fromApp(srManager.appId)
546 .makePermanent())
547 .withSelector(selector)
548 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
549 .withFlag(ForwardingObjective.Flag.SPECIFIC);
550
551 ObjectiveContext context = new DefaultObjectiveContext(
552 (objective) ->
553 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
554 objective.id(), segmentId, targetSwId),
555 (objective, error) ->
556 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
557 objective.id(), segmentId, error, targetSwId));
558
559 ForwardingObjective fob = fwdObjBuilder.add(context);
560 fwdObjs.add(fob);
561
562 }
563
564 return fwdObjs;
565 }
566
567 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700568 * Populates MPLS flow rules in the target device to point towards the
569 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700570 *
Saurav Dase0237a32016-05-27 13:54:07 -0700571 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700572 * @param destSwId destination switch device ID
573 * @param nextHops next hops switch ID list
Pier Ventre229fd0b2016-10-31 16:49:19 -0700574 * @param routerIp the router ip
sangho80f11cb2015-04-01 13:05:26 -0700575 * @return true if all rules are set successfully, false otherwise
576 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700577 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Saurav Das261c3002017-06-13 15:35:54 -0700578 Set<DeviceId> nextHops,
579 IpAddress routerIp) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700580
Charles Chan319d1a22015-11-03 10:42:14 -0800581 int segmentId;
582 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800583 if (routerIp.isIp4()) {
584 segmentId = config.getIPv4SegmentId(destSwId);
585 } else {
586 segmentId = config.getIPv6SegmentId(destSwId);
587 }
Charles Chan319d1a22015-11-03 10:42:14 -0800588 } catch (DeviceConfigNotFoundException e) {
589 log.warn(e.getMessage() + " Aborting populateMplsRule.");
590 return false;
591 }
sangho80f11cb2015-04-01 13:05:26 -0700592
Pier Ventre229fd0b2016-10-31 16:49:19 -0700593 List<ForwardingObjective> fwdObjs = new ArrayList<>();
Charles Chan3ed34d82017-06-22 18:03:14 -0700594 Collection<ForwardingObjective> fwdObjsMpls;
Pier Ventre229fd0b2016-10-31 16:49:19 -0700595 // Generates the transit rules used by the standard "routing".
596 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
597 if (fwdObjsMpls.isEmpty()) {
598 return false;
sangho80f11cb2015-04-01 13:05:26 -0700599 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700600 fwdObjs.addAll(fwdObjsMpls);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700601
602 // Generates the transit rules used by the MPLS Pwaas.
603 int pwSrLabel;
604 try {
605 pwSrLabel = config.getPWRoutingLabel(destSwId);
606 } catch (DeviceConfigNotFoundException e) {
607 log.warn(e.getMessage() + " Aborting populateMplsRule. No label for PseudoWire traffic.");
608 return false;
609 }
610 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700611 if (fwdObjsMpls.isEmpty()) {
612 return false;
613 }
Charles Chan3ed34d82017-06-22 18:03:14 -0700614 fwdObjs.addAll(fwdObjsMpls);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700615
616 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700617 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700618 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
619 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700620 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700621 }
622
623 return true;
624 }
625
Saurav Das4c35fc42015-11-20 15:27:53 -0800626 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700627 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800628 Set<DeviceId> nextHops,
629 boolean phpRequired,
630 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800631 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700632 IpAddress routerIp,
633 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800634
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700635 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
636 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700637
638 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
639
640 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800641 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700642 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700643 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700644 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800645 if (routerIp.isIp4()) {
646 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
647 } else {
648 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
649 }
650 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700651 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800652 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
653 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700654 }
655 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800656 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700657 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700658 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700659 }
660
Saurav Das4c35fc42015-11-20 15:27:53 -0800661 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700662 // if MPLS-ECMP == True we will build a standard NeighborSet.
663 // Otherwise a RandomNeighborSet.
Saurav Das261c3002017-06-13 15:35:54 -0700664 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700665 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700666 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700667 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700668 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700669 }
Saurav Das261c3002017-06-13 15:35:54 -0700670
Saurav Das9455d702017-03-24 19:03:58 -0700671 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700672 targetSw, ns);
673 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
674 if (gh == null) {
675 log.warn("getNextObjectiveId query - groupHandler for device {} "
676 + "not found", targetSw);
677 return null;
678 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700679 // If BoS == True, all forwarding is via L3 ECMP group.
680 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
681 // MPLS-Interface group. This depends on the configuration of the option
682 // MPLS-ECMP.
683 // The metadata informs the driver that the next-Objective will be used
684 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das261c3002017-06-13 15:35:54 -0700685 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
686 dstNextHops.put(destSw, nextHops);
687 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das4c35fc42015-11-20 15:27:53 -0800688 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700689 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das4c35fc42015-11-20 15:27:53 -0800690 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700691 } else {
692 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700693 nextId, targetSw, ns);
sangho80f11cb2015-04-01 13:05:26 -0700694 }
695
Saurav Das4c35fc42015-11-20 15:27:53 -0800696 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700697 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700698 }
699
700 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700701 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700702 * dstMac corresponding to the router's MAC address. For those pipelines
703 * that need to internally assign vlans to untagged packets, this method
704 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700705 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800706 * Note that the vlan assignment and filter programming should only be done by
707 * the master for a switch. This method is typically called at deviceAdd and
708 * programs filters only for the enabled ports of the device. For port-updates,
709 * that enable/disable ports after device add, singlePortFilter methods should
710 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700711 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700712 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800713 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700714 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700715 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700716 log.debug("Installing per-port filtering objective for untagged "
717 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800718
Saurav Das07c74602016-04-27 18:35:50 -0700719 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800720 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700721 log.warn("Device {} ports not available. Unable to add MacVlan filters",
722 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800723 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700724 }
Saurav Dasf9332192017-02-18 14:05:44 -0800725 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700726 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700727 if (!port.isEnabled()) {
728 disabledPorts++;
729 continue;
730 }
Charles Chan43be46b2017-02-26 22:59:35 -0800731 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800732 filteredPorts++;
733 } else {
734 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700735 }
Saurav Das7c305372015-10-28 12:39:42 -0700736 }
Charles Chan077314e2017-06-22 14:27:17 -0700737 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800738 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800739 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800740 errorPorts, filteredPorts);
741 }
742
743 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800744 * Creates or removes filtering objectives for a single port. Should only be
745 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800746 *
747 * @param deviceId device identifier
748 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800749 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800750 * @return true if no errors occurred during the build of the filtering objective
751 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700752 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800753 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
754 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
755 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
756 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
757
758 if (taggedVlans.size() != 0) {
759 // Filter for tagged vlans
760 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800761 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800762 return false;
763 }
764 if (nativeVlan != null) {
765 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800766 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800767 return false;
768 }
769 }
770 } else if (untaggedVlan != null) {
771 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800772 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800773 return false;
774 }
775 } else {
Saurav Dasfbe74572017-08-03 18:30:35 -0700776 // Unconfigured port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800777 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800778 return false;
779 }
780 }
781 return true;
782 }
783
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700784 /**
785 * Updates filtering objectives for a single port. Should only be called by
786 * the master for a switch
787 * @param deviceId device identifier
788 * @param portNum port identifier for port to be filtered
789 * @param pushVlan true to push vlan, false otherwise
790 * @param vlanId vlan identifier
791 * @param install true to install the filtering objective, false to remove
792 */
793 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
794 boolean pushVlan, VlanId vlanId, boolean install) {
795 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
796 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
797 deviceId, portNum, vlanId);
798 }
799 }
800
Charles Chan43be46b2017-02-26 22:59:35 -0800801 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
802 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800803 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800804 if (fob == null) {
805 // error encountered during build
806 return false;
807 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700808 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -0700809 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800810 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800811 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -0700812 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -0800813 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -0700814 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -0800815 if (install) {
816 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
817 } else {
818 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -0800819 }
Charles Chan90772a72017-02-08 15:52:08 -0800820 return true;
Saurav Dasf9332192017-02-18 14:05:44 -0800821 }
822
Charles Chan90772a72017-02-08 15:52:08 -0800823 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
824 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -0800825 MacAddress deviceMac;
826 try {
827 deviceMac = config.getDeviceMac(deviceId);
828 } catch (DeviceConfigNotFoundException e) {
829 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
830 return null;
831 }
Saurav Dasf9332192017-02-18 14:05:44 -0800832 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
833 fob.withKey(Criteria.matchInPort(portnum))
834 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -0800835 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -0800836
Charles Chan17ca2202017-12-19 19:55:57 -0800837 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
838
Charles Chan90772a72017-02-08 15:52:08 -0800839 if (pushVlan) {
840 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan17ca2202017-12-19 19:55:57 -0800841 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan90772a72017-02-08 15:52:08 -0800842 } else {
843 fob.addCondition(Criteria.matchVlanId(vlanId));
844 }
845
Charles Chan17ca2202017-12-19 19:55:57 -0800846 // NOTE: Some switch hardware share the same filtering flow among different ports.
847 // We use this metadata to let the driver know that there is no more enabled port
848 // within the same VLAN on this device.
849 boolean noMoreEnabledPort = srManager.interfaceService.getInterfaces().stream()
850 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
851 .filter(intf -> intf.vlanTagged().contains(vlanId) ||
852 intf.vlanUntagged().equals(vlanId) ||
853 intf.vlanNative().equals(vlanId))
854 .noneMatch(intf -> {
855 Port port = srManager.deviceService.getPort(intf.connectPoint());
856 return port != null && port.isEnabled();
857 });
858 if (noMoreEnabledPort) {
859 tBuilder.wipeDeferred();
860 }
861
862 fob.withMeta(tBuilder.build());
863
Saurav Dasf9332192017-02-18 14:05:44 -0800864 fob.permit().fromApp(srManager.appId);
865 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700866 }
867
868 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700869 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700870 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700871 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700872 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700873 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700874 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700875 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700876 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -0700877 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chanef8d12e2017-12-05 21:07:38 -0800878 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -0800879 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800880 routerIpv4 = config.getRouterIpv4(deviceId);
881 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chanef8d12e2017-12-05 21:07:38 -0800882 routerLinkLocalIpv6 = Ip6Address.valueOf(
883 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
884
Saurav Das261c3002017-06-13 15:35:54 -0700885 if (config.isPairedEdge(deviceId)) {
886 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
887 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
888 }
Charles Chan319d1a22015-11-03 10:42:14 -0800889 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -0800890 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -0800891 return;
892 }
893
Saurav Dasc28b3432015-10-30 17:45:38 -0700894 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
895 log.debug("Not installing port-IP punts - not the master for dev:{} ",
896 deviceId);
897 return;
898 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800899 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
900 allIps.add(routerIpv4);
Charles Chanef8d12e2017-12-05 21:07:38 -0800901 allIps.add(routerLinkLocalIpv6);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800902 if (routerIpv6 != null) {
903 allIps.add(routerIpv6);
904 }
Saurav Das261c3002017-06-13 15:35:54 -0700905 if (pairRouterIpv4 != null) {
906 allIps.add(pairRouterIpv4);
907 }
908 if (pairRouterIpv6 != null) {
909 allIps.add(pairRouterIpv6);
910 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800911 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700912 populateSingleIpPunts(deviceId, ipaddr);
913 }
914 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800915
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700916 /**
917 * Creates a forwarding objective to punt all IP packets, destined to the
918 * specified IP address, which should be router's port IP address.
919 *
920 * @param deviceId the switch dpid for the router
921 * @param ipAddress the IP address of the router's port
922 */
923 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
924 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
925 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
926
927 srManager.packetService.requestPackets(sbuilder.build(),
928 PacketPriority.CONTROL, srManager.appId, optDeviceId);
929 }
930
931 /**
932 * Removes a forwarding objective to punt all IP packets, destined to the
933 * specified IP address, which should be router's port IP address.
934 *
935 * @param deviceId the switch dpid for the router
936 * @param ipAddress the IP address of the router's port
937 */
938 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
939 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
940 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
941
942 try {
943 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
944 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
945 srManager.packetService.cancelPackets(sbuilder.build(),
946 PacketPriority.CONTROL, srManager.appId, optDeviceId);
947 }
948 } catch (DeviceConfigNotFoundException e) {
949 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -0700950 }
sangho80f11cb2015-04-01 13:05:26 -0700951 }
952
Charles Chanf4586112015-11-09 16:37:23 -0800953 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800954 * Method to build IPv4 or IPv6 selector.
955 *
956 * @param addressToMatch the address to match
957 */
958 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
959 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
960 }
961
962 /**
963 * Method to build IPv4 or IPv6 selector.
964 *
965 * @param prefixToMatch the prefix to match
966 */
967 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
968 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700969 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800970 if (prefixToMatch.isIp4()) {
971 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
972 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
973 return selectorBuilder;
974 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700975 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800976 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
977 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
978 return selectorBuilder;
979 }
980
981 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800982 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
983 * Furthermore, these are applied only by the master instance. Deferred actions
984 * are not cleared such that packets can be flooded in the cross connect use case
985 *
986 * @param deviceId the switch dpid for the router
987 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700988 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700989 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800990 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
991 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
992 deviceId);
993 return;
994 }
995
Charles Chan3ed34d82017-06-22 18:03:14 -0700996 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -0800997 // We punt all ARP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -0800998 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigib9632ba2017-01-12 18:14:58 -0800999 .add(new ObjectiveContext() {
1000 @Override
1001 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001002 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -08001003 deviceId, error);
1004 }
1005 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001006 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -08001007
Pier Luigib9632ba2017-01-12 18:14:58 -08001008 // We punt all NDP packets towards the controller.
Charles Chan051490d2018-01-11 11:48:18 -08001009 ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> {
1010 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1011 @Override
1012 public void onError(Objective objective, ObjectiveError error) {
1013 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
1014 deviceId, error);
1015 }
1016 });
1017 srManager.flowObjectiveService.forward(deviceId, obj);
1018 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001019
1020 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
1021 ForwardingObjective pairFwdObj;
1022 // Do not punt ARP packets from pair port
1023 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1024 .add(new ObjectiveContext() {
1025 @Override
1026 public void onError(Objective objective, ObjectiveError error) {
1027 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1028 deviceId, error);
1029 }
1030 });
1031 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1032
1033 // Do not punt NDP packets from pair port
Charles Chan051490d2018-01-11 11:48:18 -08001034 ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> {
1035 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1036 @Override
1037 public void onError(Objective objective, ObjectiveError error) {
1038 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1039 deviceId, error);
1040 }
1041 });
1042 srManager.flowObjectiveService.forward(deviceId, obj);
1043 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001044
1045 // Do not forward DAD packets from pair port
1046 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1047 .add(new ObjectiveContext() {
1048 @Override
1049 public void onError(Objective objective, ObjectiveError error) {
1050 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1051 deviceId, error);
1052 }
1053 });
1054 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1055 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001056 }
1057
Charles Chan3ed34d82017-06-22 18:03:14 -07001058 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1059 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001060 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001061 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001062 .withSelector(selector)
1063 .fromApp(srManager.appId)
1064 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001065 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001066 .makePermanent();
1067 }
1068
Charles Chan3ed34d82017-06-22 18:03:14 -07001069 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001070 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1071 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001072 if (port != null) {
1073 sBuilder.matchInPort(port);
1074 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001075
Charles Chan3ed34d82017-06-22 18:03:14 -07001076 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1077 if (punt) {
1078 tBuilder.punt();
1079 }
1080 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001081 }
1082
Charles Chan051490d2018-01-11 11:48:18 -08001083 private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) {
1084 Set<ForwardingObjective.Builder> result = Sets.newHashSet();
Pier Luigib9632ba2017-01-12 18:14:58 -08001085
Charles Chan051490d2018-01-11 11:48:18 -08001086 Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT)
1087 .forEach(type -> {
1088 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1089 sBuilder.matchEthType(TYPE_IPV6)
1090 .matchIPProtocol(PROTOCOL_ICMP6)
1091 .matchIcmpv6Type(type);
1092 if (port != null) {
1093 sBuilder.matchInPort(port);
1094 }
1095
1096 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1097 if (punt) {
1098 tBuilder.punt();
1099 }
1100
1101 result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority));
1102 });
1103
1104 return result;
Charles Chan3ed34d82017-06-22 18:03:14 -07001105 }
1106
1107 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1108 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1109 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001110 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1111 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1112 // .matchIPProtocol(PROTOCOL_ICMP6)
1113 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001114 if (port != null) {
1115 sBuilder.matchInPort(port);
1116 }
1117
1118 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1119 tBuilder.wipeDeferred();
1120 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001121 }
1122
1123 /**
Charles Chanf4586112015-11-09 16:37:23 -08001124 * Populates a forwarding objective to send packets that miss other high
1125 * priority Bridging Table entries to a group that contains all ports of
1126 * its subnet.
1127 *
Charles Chanf4586112015-11-09 16:37:23 -08001128 * @param deviceId switch ID to set the rules
1129 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001130 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001131 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001132 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001133 });
1134 }
1135
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001136 /**
1137 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1138 * @param deviceId switch ID to set the rule
1139 * @param vlanId vlan ID to specify the subnet
1140 * @param install true to install the rule, false to revoke the rule
1141 */
1142 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1143 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1144
1145 if (nextId < 0) {
1146 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1147 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1148 return;
1149 }
1150
1151 // Driver should treat objective with MacAddress.NONE as the
1152 // subnet broadcast rule
1153 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1154 sbuilder.matchVlanId(vlanId);
1155 sbuilder.matchEthDst(MacAddress.NONE);
1156
1157 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1158 fob.withFlag(Flag.SPECIFIC)
1159 .withSelector(sbuilder.build())
1160 .nextStep(nextId)
1161 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1162 .fromApp(srManager.appId)
1163 .makePermanent();
1164 ObjectiveContext context = new DefaultObjectiveContext(
1165 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1166 (objective, error) ->
1167 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1168
1169 if (install) {
1170 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1171 } else {
1172 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1173 }
1174 }
1175
Charles Chan82ab1932016-01-30 23:22:37 -08001176 private int getPriorityFromPrefix(IpPrefix prefix) {
1177 return (prefix.isIp4()) ?
1178 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1179 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001180 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001181
1182 /**
1183 * Update Forwarding objective for each host and IP address connected to given port.
1184 * And create corresponding Simple Next objective if it does not exist.
1185 * Applied only when populating Forwarding objective
1186 * @param deviceId switch ID to set the rule
1187 * @param portNumber port number
1188 * @param prefix IP prefix of the route
1189 * @param hostMac MAC address of the next hop
1190 * @param vlanId Vlan ID of the port
1191 * @param popVlan true to pop vlan tag in TrafficTreatment
1192 * @param install true to populate the forwarding objective, false to revoke
1193 */
1194 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1195 VlanId vlanId, boolean popVlan, boolean install) {
1196 ForwardingObjective.Builder fob;
1197 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1198 MacAddress deviceMac;
1199 try {
1200 deviceMac = config.getDeviceMac(deviceId);
1201 } catch (DeviceConfigNotFoundException e) {
1202 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1203 return;
1204 }
1205
1206 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1207 tbuilder.deferred()
1208 .setEthDst(hostMac)
1209 .setEthSrc(deviceMac)
1210 .setOutput(portNumber);
1211
1212 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1213
1214 if (!popVlan) {
1215 tbuilder.setVlanId(vlanId);
1216 } else {
1217 mbuilder.matchVlanId(vlanId);
1218 }
1219
1220 // if the objective is to revoke an existing rule, and for some reason
1221 // the next-objective does not exist, then a new one should not be created
1222 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1223 tbuilder.build(), mbuilder.build(), install);
1224 if (portNextObjId == -1) {
1225 // Warning log will come from getPortNextObjective method
1226 return;
1227 }
1228
1229 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1230 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1231 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1232
1233 ObjectiveContext context = new DefaultObjectiveContext(
1234 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1235 (objective, error) ->
1236 log.warn("Failed to {} IP rule for route {}: {}",
1237 install ? "install" : "revoke", prefix, error));
1238 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1239
1240 if (!install) {
1241 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1242 if (grpHandler == null) {
1243 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1244 } else {
1245 // Remove L3UG for the given port and host
1246 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1247 }
1248 }
1249 }
sangho80f11cb2015-04-01 13:05:26 -07001250}