blob: 6dd60e9631c742d198490c7592ef7b949f094fa0 [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
447 ObjectiveContext context = new DefaultObjectiveContext(
448 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
449 (objective, error) ->
450 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
451
Charles Chan3ed34d82017-06-22 18:03:14 -0700452 srManager.deviceService.getAvailableDevices().forEach(device ->
453 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context))
454 );
Charles Chanc22cef32016-04-29 14:38:22 -0700455
456 return true;
457 }
458
459 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700460 * Deals with !MPLS Bos use case.
461 *
462 * @param targetSwId the target sw
463 * @param destSwId the destination sw
464 * @param nextHops the set of next hops
465 * @param segmentId the segmentId to match
466 * @param routerIp the router ip
467 * @return a collection of fwdobjective
468 */
Saurav Das261c3002017-06-13 15:35:54 -0700469 private Collection<ForwardingObjective> handleMpls(
470 DeviceId targetSwId,
471 DeviceId destSwId,
472 Set<DeviceId> nextHops,
473 int segmentId,
474 IpAddress routerIp,
475 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700476
477 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
478 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800479 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700480 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
481 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
482 sbuilder.matchMplsBos(isMplsBos);
483 TrafficSelector selector = sbuilder.build();
484
485 // setup metadata to pass to nextObjective - indicate the vlan on egress
486 // if needed by the switch pipeline. Since mpls next-hops are always to
487 // other neighboring routers, there is no subnet assigned on those ports.
488 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800489 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700490
491 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
492 // If the next hop is the destination router for the segment, do pop
493 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700494 + "label {} in switch {} with pop to next-hops {}",
495 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700496 // Not-bos pop case (php for the current label). If MPLS-ECMP
497 // has been configured, the application we will request the
498 // installation for an MPLS-ECMP group.
Saurav Das2cb38292017-03-29 19:09:17 -0700499 ForwardingObjective.Builder fwdObjNoBosBuilder =
500 getMplsForwardingObjective(targetSwId,
501 nextHops,
502 true,
503 isMplsBos,
504 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700505 routerIp,
506 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700507 // Error case, we cannot handle, exit.
508 if (fwdObjNoBosBuilder == null) {
509 return Collections.emptyList();
510 }
511 fwdObjBuilders.add(fwdObjNoBosBuilder);
512
513 } else {
514 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700515 log.debug("Installing MPLS forwarding objective for "
516 + "label {} in switch {} without pop to next-hops {}",
517 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700518 // Not-bos pop case. If MPLS-ECMP has been configured, the
519 // application we will request the installation for an MPLS-ECMP
520 // group.
Saurav Das2cb38292017-03-29 19:09:17 -0700521 ForwardingObjective.Builder fwdObjNoBosBuilder =
522 getMplsForwardingObjective(targetSwId,
523 nextHops,
524 false,
525 isMplsBos,
526 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700527 routerIp,
528 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700529 // Error case, we cannot handle, exit.
530 if (fwdObjNoBosBuilder == null) {
531 return Collections.emptyList();
532 }
533 fwdObjBuilders.add(fwdObjNoBosBuilder);
534
535 }
536
537 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
538 // We add the final property to the fwdObjs.
539 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
540
541 ((Builder) ((Builder) fwdObjBuilder
542 .fromApp(srManager.appId)
543 .makePermanent())
544 .withSelector(selector)
545 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
546 .withFlag(ForwardingObjective.Flag.SPECIFIC);
547
548 ObjectiveContext context = new DefaultObjectiveContext(
549 (objective) ->
550 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
551 objective.id(), segmentId, targetSwId),
552 (objective, error) ->
553 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
554 objective.id(), segmentId, error, targetSwId));
555
556 ForwardingObjective fob = fwdObjBuilder.add(context);
557 fwdObjs.add(fob);
558
559 }
560
561 return fwdObjs;
562 }
563
564 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700565 * Populates MPLS flow rules in the target device to point towards the
566 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700567 *
Saurav Dase0237a32016-05-27 13:54:07 -0700568 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700569 * @param destSwId destination switch device ID
570 * @param nextHops next hops switch ID list
Pier Ventre229fd0b2016-10-31 16:49:19 -0700571 * @param routerIp the router ip
sangho80f11cb2015-04-01 13:05:26 -0700572 * @return true if all rules are set successfully, false otherwise
573 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700574 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Saurav Das261c3002017-06-13 15:35:54 -0700575 Set<DeviceId> nextHops,
576 IpAddress routerIp) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700577
Charles Chan319d1a22015-11-03 10:42:14 -0800578 int segmentId;
579 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800580 if (routerIp.isIp4()) {
581 segmentId = config.getIPv4SegmentId(destSwId);
582 } else {
583 segmentId = config.getIPv6SegmentId(destSwId);
584 }
Charles Chan319d1a22015-11-03 10:42:14 -0800585 } catch (DeviceConfigNotFoundException e) {
586 log.warn(e.getMessage() + " Aborting populateMplsRule.");
587 return false;
588 }
sangho80f11cb2015-04-01 13:05:26 -0700589
Pier Ventre229fd0b2016-10-31 16:49:19 -0700590 List<ForwardingObjective> fwdObjs = new ArrayList<>();
Charles Chan3ed34d82017-06-22 18:03:14 -0700591 Collection<ForwardingObjective> fwdObjsMpls;
Pier Ventre229fd0b2016-10-31 16:49:19 -0700592 // Generates the transit rules used by the standard "routing".
593 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
594 if (fwdObjsMpls.isEmpty()) {
595 return false;
sangho80f11cb2015-04-01 13:05:26 -0700596 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700597 fwdObjs.addAll(fwdObjsMpls);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700598
599 // Generates the transit rules used by the MPLS Pwaas.
600 int pwSrLabel;
601 try {
602 pwSrLabel = config.getPWRoutingLabel(destSwId);
603 } catch (DeviceConfigNotFoundException e) {
604 log.warn(e.getMessage() + " Aborting populateMplsRule. No label for PseudoWire traffic.");
605 return false;
606 }
607 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700608 if (fwdObjsMpls.isEmpty()) {
609 return false;
610 }
Charles Chan3ed34d82017-06-22 18:03:14 -0700611 fwdObjs.addAll(fwdObjsMpls);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700612
613 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700614 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700615 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
616 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700617 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700618 }
619
620 return true;
621 }
622
Saurav Das4c35fc42015-11-20 15:27:53 -0800623 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700624 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800625 Set<DeviceId> nextHops,
626 boolean phpRequired,
627 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800628 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700629 IpAddress routerIp,
630 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800631
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700632 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
633 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700634
635 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
636
637 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800638 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700639 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700640 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700641 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800642 if (routerIp.isIp4()) {
643 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
644 } else {
645 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
646 }
647 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700648 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800649 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
650 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700651 }
652 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800653 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700654 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700655 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700656 }
657
Saurav Das4c35fc42015-11-20 15:27:53 -0800658 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700659 // if MPLS-ECMP == True we will build a standard NeighborSet.
660 // Otherwise a RandomNeighborSet.
Saurav Das261c3002017-06-13 15:35:54 -0700661 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700662 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700663 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700664 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700665 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700666 }
Saurav Das261c3002017-06-13 15:35:54 -0700667
Saurav Das9455d702017-03-24 19:03:58 -0700668 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700669 targetSw, ns);
670 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
671 if (gh == null) {
672 log.warn("getNextObjectiveId query - groupHandler for device {} "
673 + "not found", targetSw);
674 return null;
675 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700676 // If BoS == True, all forwarding is via L3 ECMP group.
677 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
678 // MPLS-Interface group. This depends on the configuration of the option
679 // MPLS-ECMP.
680 // The metadata informs the driver that the next-Objective will be used
681 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das261c3002017-06-13 15:35:54 -0700682 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
683 dstNextHops.put(destSw, nextHops);
684 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das4c35fc42015-11-20 15:27:53 -0800685 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700686 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das4c35fc42015-11-20 15:27:53 -0800687 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700688 } else {
689 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700690 nextId, targetSw, ns);
sangho80f11cb2015-04-01 13:05:26 -0700691 }
692
Saurav Das4c35fc42015-11-20 15:27:53 -0800693 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700694 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700695 }
696
697 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700698 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700699 * dstMac corresponding to the router's MAC address. For those pipelines
700 * that need to internally assign vlans to untagged packets, this method
701 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700702 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800703 * Note that the vlan assignment and filter programming should only be done by
704 * the master for a switch. This method is typically called at deviceAdd and
705 * programs filters only for the enabled ports of the device. For port-updates,
706 * that enable/disable ports after device add, singlePortFilter methods should
707 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700708 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700709 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800710 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700711 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700712 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700713 log.debug("Installing per-port filtering objective for untagged "
714 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800715
Saurav Das07c74602016-04-27 18:35:50 -0700716 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800717 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700718 log.warn("Device {} ports not available. Unable to add MacVlan filters",
719 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800720 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700721 }
Saurav Dasf9332192017-02-18 14:05:44 -0800722 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700723 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700724 if (!port.isEnabled()) {
725 disabledPorts++;
726 continue;
727 }
Charles Chan43be46b2017-02-26 22:59:35 -0800728 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800729 filteredPorts++;
730 } else {
731 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700732 }
Saurav Das7c305372015-10-28 12:39:42 -0700733 }
Charles Chan077314e2017-06-22 14:27:17 -0700734 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800735 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800736 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800737 errorPorts, filteredPorts);
738 }
739
740 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800741 * Creates or removes filtering objectives for a single port. Should only be
742 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800743 *
744 * @param deviceId device identifier
745 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800746 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800747 * @return true if no errors occurred during the build of the filtering objective
748 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700749 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800750 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
751 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
752 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
753 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
754
755 if (taggedVlans.size() != 0) {
756 // Filter for tagged vlans
757 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800758 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800759 return false;
760 }
761 if (nativeVlan != null) {
762 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800763 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800764 return false;
765 }
766 }
767 } else if (untaggedVlan != null) {
768 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800769 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800770 return false;
771 }
772 } else {
Saurav Dasfbe74572017-08-03 18:30:35 -0700773 // Unconfigured port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800774 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800775 return false;
776 }
777 }
778 return true;
779 }
780
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700781 /**
782 * Updates filtering objectives for a single port. Should only be called by
783 * the master for a switch
784 * @param deviceId device identifier
785 * @param portNum port identifier for port to be filtered
786 * @param pushVlan true to push vlan, false otherwise
787 * @param vlanId vlan identifier
788 * @param install true to install the filtering objective, false to remove
789 */
790 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
791 boolean pushVlan, VlanId vlanId, boolean install) {
792 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
793 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
794 deviceId, portNum, vlanId);
795 }
796 }
797
Charles Chan43be46b2017-02-26 22:59:35 -0800798 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
799 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800800 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800801 if (fob == null) {
802 // error encountered during build
803 return false;
804 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700805 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -0700806 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800807 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800808 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -0700809 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -0800810 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -0700811 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -0800812 if (install) {
813 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
814 } else {
815 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -0800816 }
Charles Chan90772a72017-02-08 15:52:08 -0800817 return true;
Saurav Dasf9332192017-02-18 14:05:44 -0800818 }
819
Charles Chan90772a72017-02-08 15:52:08 -0800820 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
821 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -0800822 MacAddress deviceMac;
823 try {
824 deviceMac = config.getDeviceMac(deviceId);
825 } catch (DeviceConfigNotFoundException e) {
826 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
827 return null;
828 }
Saurav Dasf9332192017-02-18 14:05:44 -0800829 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
830 fob.withKey(Criteria.matchInPort(portnum))
831 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -0800832 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -0800833
Charles Chan17ca2202017-12-19 19:55:57 -0800834 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
835
Charles Chan90772a72017-02-08 15:52:08 -0800836 if (pushVlan) {
837 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan17ca2202017-12-19 19:55:57 -0800838 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan90772a72017-02-08 15:52:08 -0800839 } else {
840 fob.addCondition(Criteria.matchVlanId(vlanId));
841 }
842
Charles Chan17ca2202017-12-19 19:55:57 -0800843 // NOTE: Some switch hardware share the same filtering flow among different ports.
844 // We use this metadata to let the driver know that there is no more enabled port
845 // within the same VLAN on this device.
846 boolean noMoreEnabledPort = srManager.interfaceService.getInterfaces().stream()
847 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
848 .filter(intf -> intf.vlanTagged().contains(vlanId) ||
849 intf.vlanUntagged().equals(vlanId) ||
850 intf.vlanNative().equals(vlanId))
851 .noneMatch(intf -> {
852 Port port = srManager.deviceService.getPort(intf.connectPoint());
853 return port != null && port.isEnabled();
854 });
855 if (noMoreEnabledPort) {
856 tBuilder.wipeDeferred();
857 }
858
859 fob.withMeta(tBuilder.build());
860
Saurav Dasf9332192017-02-18 14:05:44 -0800861 fob.permit().fromApp(srManager.appId);
862 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700863 }
864
865 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700866 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700867 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700868 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700869 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700870 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700871 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700872 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700873 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -0700874 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chanef8d12e2017-12-05 21:07:38 -0800875 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -0800876 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800877 routerIpv4 = config.getRouterIpv4(deviceId);
878 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chanef8d12e2017-12-05 21:07:38 -0800879 routerLinkLocalIpv6 = Ip6Address.valueOf(
880 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
881
Saurav Das261c3002017-06-13 15:35:54 -0700882 if (config.isPairedEdge(deviceId)) {
883 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
884 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
885 }
Charles Chan319d1a22015-11-03 10:42:14 -0800886 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -0800887 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -0800888 return;
889 }
890
Saurav Dasc28b3432015-10-30 17:45:38 -0700891 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
892 log.debug("Not installing port-IP punts - not the master for dev:{} ",
893 deviceId);
894 return;
895 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800896 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
897 allIps.add(routerIpv4);
Charles Chanef8d12e2017-12-05 21:07:38 -0800898 allIps.add(routerLinkLocalIpv6);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800899 if (routerIpv6 != null) {
900 allIps.add(routerIpv6);
901 }
Saurav Das261c3002017-06-13 15:35:54 -0700902 if (pairRouterIpv4 != null) {
903 allIps.add(pairRouterIpv4);
904 }
905 if (pairRouterIpv6 != null) {
906 allIps.add(pairRouterIpv6);
907 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800908 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700909 populateSingleIpPunts(deviceId, ipaddr);
910 }
911 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800912
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700913 /**
914 * Creates a forwarding objective to punt all IP packets, destined to the
915 * specified IP address, which should be router's port IP address.
916 *
917 * @param deviceId the switch dpid for the router
918 * @param ipAddress the IP address of the router's port
919 */
920 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
921 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
922 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
923
924 srManager.packetService.requestPackets(sbuilder.build(),
925 PacketPriority.CONTROL, srManager.appId, optDeviceId);
926 }
927
928 /**
929 * Removes a forwarding objective to punt all IP packets, destined to the
930 * specified IP address, which should be router's port IP address.
931 *
932 * @param deviceId the switch dpid for the router
933 * @param ipAddress the IP address of the router's port
934 */
935 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
936 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
937 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
938
939 try {
940 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
941 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
942 srManager.packetService.cancelPackets(sbuilder.build(),
943 PacketPriority.CONTROL, srManager.appId, optDeviceId);
944 }
945 } catch (DeviceConfigNotFoundException e) {
946 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -0700947 }
sangho80f11cb2015-04-01 13:05:26 -0700948 }
949
Charles Chanf4586112015-11-09 16:37:23 -0800950 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800951 * Method to build IPv4 or IPv6 selector.
952 *
953 * @param addressToMatch the address to match
954 */
955 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
956 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
957 }
958
959 /**
960 * Method to build IPv4 or IPv6 selector.
961 *
962 * @param prefixToMatch the prefix to match
963 */
964 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
965 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700966 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800967 if (prefixToMatch.isIp4()) {
968 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
969 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
970 return selectorBuilder;
971 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700972 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800973 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
974 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
975 return selectorBuilder;
976 }
977
978 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800979 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
980 * Furthermore, these are applied only by the master instance. Deferred actions
981 * are not cleared such that packets can be flooded in the cross connect use case
982 *
983 * @param deviceId the switch dpid for the router
984 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700985 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700986 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800987 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
988 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
989 deviceId);
990 return;
991 }
992
Charles Chan3ed34d82017-06-22 18:03:14 -0700993 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -0800994 // We punt all ARP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -0800995 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigib9632ba2017-01-12 18:14:58 -0800996 .add(new ObjectiveContext() {
997 @Override
998 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700999 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -08001000 deviceId, error);
1001 }
1002 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001003 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -08001004
Pier Luigib9632ba2017-01-12 18:14:58 -08001005 // We punt all NDP packets towards the controller.
Charles Chan051490d2018-01-11 11:48:18 -08001006 ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> {
1007 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1008 @Override
1009 public void onError(Objective objective, ObjectiveError error) {
1010 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
1011 deviceId, error);
1012 }
1013 });
1014 srManager.flowObjectiveService.forward(deviceId, obj);
1015 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001016
1017 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
1018 ForwardingObjective pairFwdObj;
1019 // Do not punt ARP packets from pair port
1020 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1021 .add(new ObjectiveContext() {
1022 @Override
1023 public void onError(Objective objective, ObjectiveError error) {
1024 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1025 deviceId, error);
1026 }
1027 });
1028 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1029
1030 // Do not punt NDP packets from pair port
Charles Chan051490d2018-01-11 11:48:18 -08001031 ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> {
1032 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1033 @Override
1034 public void onError(Objective objective, ObjectiveError error) {
1035 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1036 deviceId, error);
1037 }
1038 });
1039 srManager.flowObjectiveService.forward(deviceId, obj);
1040 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001041
1042 // Do not forward DAD packets from pair port
1043 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1044 .add(new ObjectiveContext() {
1045 @Override
1046 public void onError(Objective objective, ObjectiveError error) {
1047 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1048 deviceId, error);
1049 }
1050 });
1051 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1052 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001053 }
1054
Charles Chan3ed34d82017-06-22 18:03:14 -07001055 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1056 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001057 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001058 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001059 .withSelector(selector)
1060 .fromApp(srManager.appId)
1061 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001062 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001063 .makePermanent();
1064 }
1065
Charles Chan3ed34d82017-06-22 18:03:14 -07001066 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001067 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1068 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001069 if (port != null) {
1070 sBuilder.matchInPort(port);
1071 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001072
Charles Chan3ed34d82017-06-22 18:03:14 -07001073 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1074 if (punt) {
1075 tBuilder.punt();
1076 }
1077 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001078 }
1079
Charles Chan051490d2018-01-11 11:48:18 -08001080 private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) {
1081 Set<ForwardingObjective.Builder> result = Sets.newHashSet();
Pier Luigib9632ba2017-01-12 18:14:58 -08001082
Charles Chan051490d2018-01-11 11:48:18 -08001083 Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT)
1084 .forEach(type -> {
1085 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1086 sBuilder.matchEthType(TYPE_IPV6)
1087 .matchIPProtocol(PROTOCOL_ICMP6)
1088 .matchIcmpv6Type(type);
1089 if (port != null) {
1090 sBuilder.matchInPort(port);
1091 }
1092
1093 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1094 if (punt) {
1095 tBuilder.punt();
1096 }
1097
1098 result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority));
1099 });
1100
1101 return result;
Charles Chan3ed34d82017-06-22 18:03:14 -07001102 }
1103
1104 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1105 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1106 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001107 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1108 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1109 // .matchIPProtocol(PROTOCOL_ICMP6)
1110 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001111 if (port != null) {
1112 sBuilder.matchInPort(port);
1113 }
1114
1115 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1116 tBuilder.wipeDeferred();
1117 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001118 }
1119
1120 /**
Charles Chanf4586112015-11-09 16:37:23 -08001121 * Populates a forwarding objective to send packets that miss other high
1122 * priority Bridging Table entries to a group that contains all ports of
1123 * its subnet.
1124 *
Charles Chanf4586112015-11-09 16:37:23 -08001125 * @param deviceId switch ID to set the rules
1126 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001127 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001128 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001129 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001130 });
1131 }
1132
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001133 /**
1134 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1135 * @param deviceId switch ID to set the rule
1136 * @param vlanId vlan ID to specify the subnet
1137 * @param install true to install the rule, false to revoke the rule
1138 */
1139 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1140 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1141
1142 if (nextId < 0) {
1143 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1144 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1145 return;
1146 }
1147
1148 // Driver should treat objective with MacAddress.NONE as the
1149 // subnet broadcast rule
1150 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1151 sbuilder.matchVlanId(vlanId);
1152 sbuilder.matchEthDst(MacAddress.NONE);
1153
1154 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1155 fob.withFlag(Flag.SPECIFIC)
1156 .withSelector(sbuilder.build())
1157 .nextStep(nextId)
1158 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1159 .fromApp(srManager.appId)
1160 .makePermanent();
1161 ObjectiveContext context = new DefaultObjectiveContext(
1162 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1163 (objective, error) ->
1164 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1165
1166 if (install) {
1167 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1168 } else {
1169 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1170 }
1171 }
1172
Charles Chan82ab1932016-01-30 23:22:37 -08001173 private int getPriorityFromPrefix(IpPrefix prefix) {
1174 return (prefix.isIp4()) ?
1175 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1176 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001177 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001178
1179 /**
1180 * Update Forwarding objective for each host and IP address connected to given port.
1181 * And create corresponding Simple Next objective if it does not exist.
1182 * Applied only when populating Forwarding objective
1183 * @param deviceId switch ID to set the rule
1184 * @param portNumber port number
1185 * @param prefix IP prefix of the route
1186 * @param hostMac MAC address of the next hop
1187 * @param vlanId Vlan ID of the port
1188 * @param popVlan true to pop vlan tag in TrafficTreatment
1189 * @param install true to populate the forwarding objective, false to revoke
1190 */
1191 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1192 VlanId vlanId, boolean popVlan, boolean install) {
1193 ForwardingObjective.Builder fob;
1194 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1195 MacAddress deviceMac;
1196 try {
1197 deviceMac = config.getDeviceMac(deviceId);
1198 } catch (DeviceConfigNotFoundException e) {
1199 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1200 return;
1201 }
1202
1203 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1204 tbuilder.deferred()
1205 .setEthDst(hostMac)
1206 .setEthSrc(deviceMac)
1207 .setOutput(portNumber);
1208
1209 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1210
1211 if (!popVlan) {
1212 tbuilder.setVlanId(vlanId);
1213 } else {
1214 mbuilder.matchVlanId(vlanId);
1215 }
1216
1217 // if the objective is to revoke an existing rule, and for some reason
1218 // the next-objective does not exist, then a new one should not be created
1219 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1220 tbuilder.build(), mbuilder.build(), install);
1221 if (portNextObjId == -1) {
1222 // Warning log will come from getPortNextObjective method
1223 return;
1224 }
1225
1226 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1227 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1228 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1229
1230 ObjectiveContext context = new DefaultObjectiveContext(
1231 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1232 (objective, error) ->
1233 log.warn("Failed to {} IP rule for route {}: {}",
1234 install ? "install" : "revoke", prefix, error));
1235 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1236
1237 if (!install) {
1238 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1239 if (grpHandler == null) {
1240 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1241 } else {
1242 // Remove L3UG for the given port and host
1243 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1244 }
1245 }
1246 }
sangho80f11cb2015-04-01 13:05:26 -07001247}