blob: f820bcb3e6e78879680c8cca1cf792c59ff26d07 [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;
Saurav Das4c35fc42015-11-20 15:27:53 -080019import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070020import org.onlab.packet.Ethernet;
21import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080022import org.onlab.packet.Ip6Address;
23import org.onlab.packet.IpAddress;
sangho80f11cb2015-04-01 13:05:26 -070024import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070026import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070027import org.onlab.packet.VlanId;
Charles Chanb7f75ac2016-01-11 18:28:54 -080028import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070029import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080030import org.onosproject.net.flowobjective.Objective;
Charles Chan1eaf4802016-04-18 13:44:03 -070031import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080032import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080033import org.onosproject.net.packet.PacketPriority;
Saurav Dasd1872b02016-12-02 15:43:47 -080034import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan319d1a22015-11-03 10:42:14 -080035import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
36import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Das62ae6792017-05-15 15:34:25 -070037import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -070038import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sangho80f11cb2015-04-01 13:05:26 -070039import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070040import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070041import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070042import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070044import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070046import org.onosproject.net.flow.criteria.Criteria;
47import org.onosproject.net.flowobjective.DefaultFilteringObjective;
48import org.onosproject.net.flowobjective.DefaultForwardingObjective;
49import org.onosproject.net.flowobjective.FilteringObjective;
50import org.onosproject.net.flowobjective.ForwardingObjective;
51import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070052import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
56import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070057import java.util.Collection;
58import java.util.Collections;
Saurav Das261c3002017-06-13 15:35:54 -070059import java.util.HashMap;
Saurav Dasc28b3432015-10-30 17:45:38 -070060import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070061import java.util.List;
Saurav Das261c3002017-06-13 15:35:54 -070062import java.util.Map;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080063import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070064import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070065import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070066
67import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080068import static org.onlab.packet.Ethernet.TYPE_ARP;
69import static org.onlab.packet.Ethernet.TYPE_IPV6;
70import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
71import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan10b0fb72017-02-02 16:20:42 -080072import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sangho80f11cb2015-04-01 13:05:26 -070073
Charles Chanb7f75ac2016-01-11 18:28:54 -080074/**
75 * Populator of segment routing flow rules.
76 */
sangho80f11cb2015-04-01 13:05:26 -070077public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070078 private static final Logger log = LoggerFactory
79 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070080
sanghofb7c7292015-04-13 15:15:58 -070081 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070082 private SegmentRoutingManager srManager;
83 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070084
sangho80f11cb2015-04-01 13:05:26 -070085 /**
86 * Creates a RoutingRulePopulator object.
87 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070088 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070089 */
Charles Chan3ed34d82017-06-22 18:03:14 -070090 RoutingRulePopulator(SegmentRoutingManager srManager) {
sangho80f11cb2015-04-01 13:05:26 -070091 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070092 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -070093 this.rulePopulationCounter = new AtomicLong(0);
94 }
95
96 /**
97 * Resets the population counter.
98 */
Charles Chan3ed34d82017-06-22 18:03:14 -070099 void resetCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700100 rulePopulationCounter.set(0);
101 }
102
103 /**
104 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700105 *
106 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700107 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700108 long getCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700109 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700110 }
111
112 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700113 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700114 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700115 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700116 * @param deviceId device ID of the device that next hop attaches to
117 * @param prefix IP prefix of the route
118 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800119 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700120 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700121 */
Charles Chan910be6a2017-08-23 14:46:43 -0700122 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800123 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das261c3002017-06-13 15:35:54 -0700124 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700125 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800126 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800127 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700128 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
129 hostVlanId, outPort, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800130 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700131 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan319d1a22015-11-03 10:42:14 -0800132 return;
133 }
Saurav Das07c74602016-04-27 18:35:50 -0700134 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700135 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700136 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700137 return;
138 }
Charles Chan910be6a2017-08-23 14:46:43 -0700139
140 int nextId = fwdBuilder.add().nextId();
Charles Chan1eaf4802016-04-18 13:44:03 -0700141 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan910be6a2017-08-23 14:46:43 -0700142 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
143 prefix, nextId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700144 (objective, error) ->
Saurav Das261c3002017-06-13 15:35:54 -0700145 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das368cf212017-03-15 15:15:14 -0700146 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700147 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800148 rulePopulationCounter.incrementAndGet();
149 }
150
Charles Chanb7f75ac2016-01-11 18:28:54 -0800151 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700152 * Removes IP rules for a route when the next hop is gone.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800153 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700154 * @param deviceId device ID of the device that next hop attaches to
155 * @param prefix IP prefix of the route
156 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800157 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700158 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800159 */
Charles Chan910be6a2017-08-23 14:46:43 -0700160 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800161 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700162 log.debug("Revoke IP table entry for route {} at {}:{}",
163 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800164 ForwardingObjective.Builder fwdBuilder;
165 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700166 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
167 hostVlanId, outPort, true);
Charles Chanf4586112015-11-09 16:37:23 -0800168 } catch (DeviceConfigNotFoundException e) {
169 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
170 return;
171 }
Charles Chanea702b12016-11-30 11:55:05 -0800172 if (fwdBuilder == null) {
173 log.warn("Aborting host routing table entries due "
174 + "to error for dev:{} route:{}", deviceId, prefix);
175 return;
176 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700177 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700178 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700179 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700180 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700181 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800182 }
183
Charles Chanddac7fd2016-10-27 14:19:48 -0700184 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800185 * Returns a forwarding objective builder for routing rules.
186 * <p>
187 * The forwarding objective routes packets destined to a given prefix to
188 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700189 *
190 * @param deviceId device ID
191 * @param prefix prefix that need to be routed
192 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800193 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700194 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700195 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chanddac7fd2016-10-27 14:19:48 -0700196 * @return forwarding objective builder
197 * @throws DeviceConfigNotFoundException if given device is not configured
198 */
Charles Chan18fa4252017-02-08 16:10:40 -0800199 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700200 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700201 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
202 boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800203 throws DeviceConfigNotFoundException {
204 MacAddress deviceMac;
205 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800206
Charles Chan90772a72017-02-08 15:52:08 -0800207 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
208 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
209 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
210 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700211
Charles Chan90772a72017-02-08 15:52:08 -0800212 // Create route selector
213 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
214
215 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700216 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700217 tbuilder.deferred()
218 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800219 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700220 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800221
Charles Chan90772a72017-02-08 15:52:08 -0800222 // Create route meta
223 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800224
Charles Chan90772a72017-02-08 15:52:08 -0800225 // Adjust the meta according to VLAN configuration
226 if (taggedVlans.contains(hostVlanId)) {
227 tbuilder.setVlanId(hostVlanId);
228 } else if (hostVlanId.equals(VlanId.NONE)) {
229 if (untaggedVlan != null) {
230 mbuilder.matchVlanId(untaggedVlan);
231 } else if (nativeVlan != null) {
232 mbuilder.matchVlanId(nativeVlan);
233 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700234 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
235 hostMac, hostVlanId, connectPoint);
236 return null;
Charles Chan90772a72017-02-08 15:52:08 -0800237 }
238 } else {
Saurav Das2cb38292017-03-29 19:09:17 -0700239 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
240 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das07c74602016-04-27 18:35:50 -0700241 return null;
242 }
Saurav Das2cb38292017-03-29 19:09:17 -0700243 // if the objective is to revoke an existing rule, and for some reason
244 // the next-objective does not exist, then a new one should not be created
Charles Chan90772a72017-02-08 15:52:08 -0800245 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das2cb38292017-03-29 19:09:17 -0700246 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan90772a72017-02-08 15:52:08 -0800247 if (portNextObjId == -1) {
248 // Warning log will come from getPortNextObjective method
249 return null;
250 }
251
Charles Chanf4586112015-11-09 16:37:23 -0800252 return DefaultForwardingObjective.builder()
Charles Chan90772a72017-02-08 15:52:08 -0800253 .withSelector(sbuilder.build())
Saurav Das2d94d312015-11-24 23:21:05 -0800254 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800255 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700256 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800257 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700258 }
259
260 /**
Saurav Das261c3002017-06-13 15:35:54 -0700261 * Populates IP flow rules for all the given prefixes reachable from the
262 * destination switch(es).
sangho80f11cb2015-04-01 13:05:26 -0700263 *
Saurav Das261c3002017-06-13 15:35:54 -0700264 * @param targetSw switch where rules are to be programmed
265 * @param subnets subnets/prefixes being added
266 * @param destSw1 destination switch where the prefixes are reachable
267 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
268 * Should be null if there is no paired destination switch (by config)
269 * or if the given prefixes are reachable only via destSw1
270 * @param nextHops a map containing a set of next-hops for each destination switch.
271 * If destSw2 is not null, then this map must contain an
272 * entry for destSw2 with its next-hops from the targetSw
273 * (although the next-hop set may be empty in certain scenarios).
274 * If destSw2 is null, there should not be an entry in this
275 * map for destSw2.
sangho80f11cb2015-04-01 13:05:26 -0700276 * @return true if all rules are set successfully, false otherwise
277 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700278 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das261c3002017-06-13 15:35:54 -0700279 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700280 for (IpPrefix subnet : subnets) {
Saurav Das261c3002017-06-13 15:35:54 -0700281 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sangho80f11cb2015-04-01 13:05:26 -0700282 return false;
283 }
284 }
Charles Chanc22cef32016-04-29 14:38:22 -0700285 return true;
286 }
sangho80f11cb2015-04-01 13:05:26 -0700287
Charles Chanc22cef32016-04-29 14:38:22 -0700288 /**
Saurav Das261c3002017-06-13 15:35:54 -0700289 * Revokes IP flow rules for the subnets in each edge switch.
Charles Chanc22cef32016-04-29 14:38:22 -0700290 *
291 * @param subnets subnet being removed
292 * @return true if all rules are removed successfully, false otherwise
293 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700294 boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700295 for (IpPrefix subnet : subnets) {
296 if (!revokeIpRuleForRouter(subnet)) {
297 return false;
298 }
299 }
sangho80f11cb2015-04-01 13:05:26 -0700300 return true;
301 }
302
303 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700304 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das261c3002017-06-13 15:35:54 -0700305 * is reachable via destination device(s).
sangho80f11cb2015-04-01 13:05:26 -0700306 *
Saurav Das261c3002017-06-13 15:35:54 -0700307 * @param targetSw target device ID to set the rules
308 * @param ipPrefix the IP prefix
309 * @param destSw1 destination switch where the prefixes are reachable
310 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
311 * Should be null if there is no paired destination switch (by config)
312 * or if the given prefixes are reachable only via destSw1
313 * @param nextHops map of destination switches and their next-hops.
314 * Should only contain destination switches that are
315 * actually meant to be routed to. If destSw2 is null, there
316 * should not be an entry for destSw2 in this map.
sangho80f11cb2015-04-01 13:05:26 -0700317 * @return true if all rules are set successfully, false otherwise
318 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700319 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das261c3002017-06-13 15:35:54 -0700320 IpPrefix ipPrefix, DeviceId destSw1,
321 DeviceId destSw2,
322 Map<DeviceId, Set<DeviceId>> nextHops) {
323 int segmentId1, segmentId2 = -1;
Charles Chan319d1a22015-11-03 10:42:14 -0800324 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800325 if (ipPrefix.isIp4()) {
Saurav Das261c3002017-06-13 15:35:54 -0700326 segmentId1 = config.getIPv4SegmentId(destSw1);
327 if (destSw2 != null) {
328 segmentId2 = config.getIPv4SegmentId(destSw2);
329 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800330 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700331 segmentId1 = config.getIPv6SegmentId(destSw1);
332 if (destSw2 != null) {
333 segmentId2 = config.getIPv6SegmentId(destSw2);
334 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800335 }
Charles Chan319d1a22015-11-03 10:42:14 -0800336 } catch (DeviceConfigNotFoundException e) {
337 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
338 return false;
339 }
sangho80f11cb2015-04-01 13:05:26 -0700340
Pier Ventreadb4ae62016-11-23 09:57:42 -0800341 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800342 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700343
Charles Chanf4586112015-11-09 16:37:23 -0800344 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das261c3002017-06-13 15:35:54 -0700345 DestinationSet ds;
Charles Chanf4586112015-11-09 16:37:23 -0800346 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700347
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700348 // If the next hop is the same as the final destination, then MPLS label
349 // is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700350 /*if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800351 tbuilder.immediate().decNwTtl();
Saurav Das261c3002017-06-13 15:35:54 -0700352 ds = new DestinationSet(false, destSw);
Charles Chanf4586112015-11-09 16:37:23 -0800353 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700354 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700355 ds = new DestinationSet(false, segmentId, destSw);
356 treatment = null;
357 }*/
358 if (destSw2 == null) {
359 // single dst - create destination set based on next-hop
360 Set<DeviceId> nhd1 = nextHops.get(destSw1);
361 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
362 tbuilder.immediate().decNwTtl();
363 ds = new DestinationSet(false, destSw1);
364 treatment = tbuilder.build();
365 } else {
366 ds = new DestinationSet(false, segmentId1, destSw1);
367 treatment = null;
368 }
369 } else {
370 // dst pair - IP rules for dst-pairs are always from other edge nodes
371 // the destination set needs to have both destinations, even if there
372 // are no next hops to one of them
373 ds = new DestinationSet(false, segmentId1, destSw1, segmentId2, destSw2);
Charles Chanf4586112015-11-09 16:37:23 -0800374 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700375 }
376
Saurav Das4c35fc42015-11-20 15:27:53 -0800377 // setup metadata to pass to nextObjective - indicate the vlan on egress
378 // if needed by the switch pipeline. Since neighbor sets are always to
379 // other neighboring routers, there is no subnet assigned on those ports.
380 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800381 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das261c3002017-06-13 15:35:54 -0700382 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700383 if (grpHandler == null) {
384 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das261c3002017-06-13 15:35:54 -0700385 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700386 return false;
387 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800388
Saurav Das261c3002017-06-13 15:35:54 -0700389 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
390 metabuilder.build(), true);
Saurav Das4c35fc42015-11-20 15:27:53 -0800391 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700392 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho2165d222015-05-01 09:38:25 -0700393 return false;
394 }
395
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700396 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
397 .builder()
398 .fromApp(srManager.appId)
399 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800400 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700401 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800402 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700403 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800404 if (treatment != null) {
405 fwdBuilder.withTreatment(treatment);
406 }
Saurav Das62ae6792017-05-15 15:34:25 -0700407 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das261c3002017-06-13 15:35:54 -0700408 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700409 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700410 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700411 ipPrefix, targetSw),
Charles Chan1eaf4802016-04-18 13:44:03 -0700412 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700413 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700414 ipPrefix, error, targetSw));
415 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700416 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700417
418 return true;
419 }
420
sangho80f11cb2015-04-01 13:05:26 -0700421 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700422 * Revokes IP flow rules for the router IP address.
423 *
424 * @param ipPrefix the IP address of the destination router
425 * @return true if all rules are removed successfully, false otherwise
426 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700427 private boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800428 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700429 TrafficSelector selector = sbuilder.build();
430 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
431
432 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
433 .builder()
434 .fromApp(srManager.appId)
435 .makePermanent()
436 .withSelector(selector)
437 .withTreatment(dummyTreatment)
438 .withPriority(getPriorityFromPrefix(ipPrefix))
439 .withFlag(ForwardingObjective.Flag.SPECIFIC);
440
441 ObjectiveContext context = new DefaultObjectiveContext(
442 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
443 (objective, error) ->
444 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
445
Charles Chan3ed34d82017-06-22 18:03:14 -0700446 srManager.deviceService.getAvailableDevices().forEach(device ->
447 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context))
448 );
Charles Chanc22cef32016-04-29 14:38:22 -0700449
450 return true;
451 }
452
453 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700454 * Deals with !MPLS Bos use case.
455 *
456 * @param targetSwId the target sw
457 * @param destSwId the destination sw
458 * @param nextHops the set of next hops
459 * @param segmentId the segmentId to match
460 * @param routerIp the router ip
461 * @return a collection of fwdobjective
462 */
Saurav Das261c3002017-06-13 15:35:54 -0700463 private Collection<ForwardingObjective> handleMpls(
464 DeviceId targetSwId,
465 DeviceId destSwId,
466 Set<DeviceId> nextHops,
467 int segmentId,
468 IpAddress routerIp,
469 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700470
471 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
472 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800473 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700474 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
475 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
476 sbuilder.matchMplsBos(isMplsBos);
477 TrafficSelector selector = sbuilder.build();
478
479 // setup metadata to pass to nextObjective - indicate the vlan on egress
480 // if needed by the switch pipeline. Since mpls next-hops are always to
481 // other neighboring routers, there is no subnet assigned on those ports.
482 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800483 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700484
485 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
486 // If the next hop is the destination router for the segment, do pop
487 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700488 + "label {} in switch {} with pop to next-hops {}",
489 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700490 // Not-bos pop case (php for the current label). If MPLS-ECMP
491 // has been configured, the application we will request the
492 // installation for an MPLS-ECMP group.
Saurav Das2cb38292017-03-29 19:09:17 -0700493 ForwardingObjective.Builder fwdObjNoBosBuilder =
494 getMplsForwardingObjective(targetSwId,
495 nextHops,
496 true,
497 isMplsBos,
498 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700499 routerIp,
500 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700501 // Error case, we cannot handle, exit.
502 if (fwdObjNoBosBuilder == null) {
503 return Collections.emptyList();
504 }
505 fwdObjBuilders.add(fwdObjNoBosBuilder);
506
507 } else {
508 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700509 log.debug("Installing MPLS forwarding objective for "
510 + "label {} in switch {} without pop to next-hops {}",
511 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700512 // Not-bos pop case. If MPLS-ECMP has been configured, the
513 // application we will request the installation for an MPLS-ECMP
514 // group.
Saurav Das2cb38292017-03-29 19:09:17 -0700515 ForwardingObjective.Builder fwdObjNoBosBuilder =
516 getMplsForwardingObjective(targetSwId,
517 nextHops,
518 false,
519 isMplsBos,
520 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700521 routerIp,
522 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700523 // Error case, we cannot handle, exit.
524 if (fwdObjNoBosBuilder == null) {
525 return Collections.emptyList();
526 }
527 fwdObjBuilders.add(fwdObjNoBosBuilder);
528
529 }
530
531 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
532 // We add the final property to the fwdObjs.
533 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
534
535 ((Builder) ((Builder) fwdObjBuilder
536 .fromApp(srManager.appId)
537 .makePermanent())
538 .withSelector(selector)
539 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
540 .withFlag(ForwardingObjective.Flag.SPECIFIC);
541
542 ObjectiveContext context = new DefaultObjectiveContext(
543 (objective) ->
544 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
545 objective.id(), segmentId, targetSwId),
546 (objective, error) ->
547 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
548 objective.id(), segmentId, error, targetSwId));
549
550 ForwardingObjective fob = fwdObjBuilder.add(context);
551 fwdObjs.add(fob);
552
553 }
554
555 return fwdObjs;
556 }
557
558 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700559 * Populates MPLS flow rules in the target device to point towards the
560 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700561 *
Saurav Dase0237a32016-05-27 13:54:07 -0700562 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700563 * @param destSwId destination switch device ID
564 * @param nextHops next hops switch ID list
Pier Ventre229fd0b2016-10-31 16:49:19 -0700565 * @param routerIp the router ip
sangho80f11cb2015-04-01 13:05:26 -0700566 * @return true if all rules are set successfully, false otherwise
567 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700568 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Saurav Das261c3002017-06-13 15:35:54 -0700569 Set<DeviceId> nextHops,
570 IpAddress routerIp) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700571
Charles Chan319d1a22015-11-03 10:42:14 -0800572 int segmentId;
573 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800574 if (routerIp.isIp4()) {
575 segmentId = config.getIPv4SegmentId(destSwId);
576 } else {
577 segmentId = config.getIPv6SegmentId(destSwId);
578 }
Charles Chan319d1a22015-11-03 10:42:14 -0800579 } catch (DeviceConfigNotFoundException e) {
580 log.warn(e.getMessage() + " Aborting populateMplsRule.");
581 return false;
582 }
sangho80f11cb2015-04-01 13:05:26 -0700583
Pier Ventre229fd0b2016-10-31 16:49:19 -0700584 List<ForwardingObjective> fwdObjs = new ArrayList<>();
Charles Chan3ed34d82017-06-22 18:03:14 -0700585 Collection<ForwardingObjective> fwdObjsMpls;
Pier Ventre229fd0b2016-10-31 16:49:19 -0700586 // Generates the transit rules used by the standard "routing".
587 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
588 if (fwdObjsMpls.isEmpty()) {
589 return false;
sangho80f11cb2015-04-01 13:05:26 -0700590 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700591 fwdObjs.addAll(fwdObjsMpls);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700592
593 // Generates the transit rules used by the MPLS Pwaas.
594 int pwSrLabel;
595 try {
596 pwSrLabel = config.getPWRoutingLabel(destSwId);
597 } catch (DeviceConfigNotFoundException e) {
598 log.warn(e.getMessage() + " Aborting populateMplsRule. No label for PseudoWire traffic.");
599 return false;
600 }
601 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700602 if (fwdObjsMpls.isEmpty()) {
603 return false;
604 }
Charles Chan3ed34d82017-06-22 18:03:14 -0700605 fwdObjs.addAll(fwdObjsMpls);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700606
607 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700608 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700609 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
610 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700611 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700612 }
613
614 return true;
615 }
616
Saurav Das4c35fc42015-11-20 15:27:53 -0800617 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700618 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800619 Set<DeviceId> nextHops,
620 boolean phpRequired,
621 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800622 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700623 IpAddress routerIp,
624 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800625
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700626 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
627 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700628
629 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
630
631 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800632 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700633 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700634 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700635 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800636 if (routerIp.isIp4()) {
637 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
638 } else {
639 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
640 }
641 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700642 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800643 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
644 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700645 }
646 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800647 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700648 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700649 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700650 }
651
Saurav Das4c35fc42015-11-20 15:27:53 -0800652 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700653 // if MPLS-ECMP == True we will build a standard NeighborSet.
654 // Otherwise a RandomNeighborSet.
Saurav Das261c3002017-06-13 15:35:54 -0700655 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700656 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700657 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700658 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700659 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700660 }
Saurav Das261c3002017-06-13 15:35:54 -0700661
Saurav Das9455d702017-03-24 19:03:58 -0700662 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700663 targetSw, ns);
664 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
665 if (gh == null) {
666 log.warn("getNextObjectiveId query - groupHandler for device {} "
667 + "not found", targetSw);
668 return null;
669 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700670 // If BoS == True, all forwarding is via L3 ECMP group.
671 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
672 // MPLS-Interface group. This depends on the configuration of the option
673 // MPLS-ECMP.
674 // The metadata informs the driver that the next-Objective will be used
675 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das261c3002017-06-13 15:35:54 -0700676 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
677 dstNextHops.put(destSw, nextHops);
678 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das4c35fc42015-11-20 15:27:53 -0800679 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700680 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das4c35fc42015-11-20 15:27:53 -0800681 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700682 } else {
683 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700684 nextId, targetSw, ns);
sangho80f11cb2015-04-01 13:05:26 -0700685 }
686
Saurav Das4c35fc42015-11-20 15:27:53 -0800687 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700688 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700689 }
690
691 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700692 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700693 * dstMac corresponding to the router's MAC address. For those pipelines
694 * that need to internally assign vlans to untagged packets, this method
695 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700696 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800697 * Note that the vlan assignment and filter programming should only be done by
698 * the master for a switch. This method is typically called at deviceAdd and
699 * programs filters only for the enabled ports of the device. For port-updates,
700 * that enable/disable ports after device add, singlePortFilter methods should
701 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700702 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700703 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800704 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700705 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700706 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700707 log.debug("Installing per-port filtering objective for untagged "
708 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800709
Saurav Das07c74602016-04-27 18:35:50 -0700710 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800711 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700712 log.warn("Device {} ports not available. Unable to add MacVlan filters",
713 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800714 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700715 }
Saurav Dasf9332192017-02-18 14:05:44 -0800716 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700717 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700718 if (!port.isEnabled()) {
719 disabledPorts++;
720 continue;
721 }
Charles Chan43be46b2017-02-26 22:59:35 -0800722 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800723 filteredPorts++;
724 } else {
725 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700726 }
Saurav Das7c305372015-10-28 12:39:42 -0700727 }
Charles Chan077314e2017-06-22 14:27:17 -0700728 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800729 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800730 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800731 errorPorts, filteredPorts);
732 }
733
734 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800735 * Creates or removes filtering objectives for a single port. Should only be
736 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800737 *
738 * @param deviceId device identifier
739 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800740 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800741 * @return true if no errors occurred during the build of the filtering objective
742 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700743 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800744 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
745 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
746 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
747 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
748
749 if (taggedVlans.size() != 0) {
750 // Filter for tagged vlans
751 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800752 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800753 return false;
754 }
755 if (nativeVlan != null) {
756 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800757 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800758 return false;
759 }
760 }
761 } else if (untaggedVlan != null) {
762 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800763 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800764 return false;
765 }
766 } else {
Saurav Dasfbe74572017-08-03 18:30:35 -0700767 // Unconfigured port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800768 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800769 return false;
770 }
771 }
772 return true;
773 }
774
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700775 /**
776 * Updates filtering objectives for a single port. Should only be called by
777 * the master for a switch
778 * @param deviceId device identifier
779 * @param portNum port identifier for port to be filtered
780 * @param pushVlan true to push vlan, false otherwise
781 * @param vlanId vlan identifier
782 * @param install true to install the filtering objective, false to remove
783 */
784 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
785 boolean pushVlan, VlanId vlanId, boolean install) {
786 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
787 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
788 deviceId, portNum, vlanId);
789 }
790 }
791
Charles Chan43be46b2017-02-26 22:59:35 -0800792 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
793 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800794 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800795 if (fob == null) {
796 // error encountered during build
797 return false;
798 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700799 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -0700800 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800801 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800802 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -0700803 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -0800804 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -0700805 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -0800806 if (install) {
807 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
808 } else {
809 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -0800810 }
Charles Chan90772a72017-02-08 15:52:08 -0800811 return true;
Saurav Dasf9332192017-02-18 14:05:44 -0800812 }
813
Charles Chan90772a72017-02-08 15:52:08 -0800814 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
815 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -0800816 MacAddress deviceMac;
817 try {
818 deviceMac = config.getDeviceMac(deviceId);
819 } catch (DeviceConfigNotFoundException e) {
820 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
821 return null;
822 }
Saurav Dasf9332192017-02-18 14:05:44 -0800823 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
824 fob.withKey(Criteria.matchInPort(portnum))
825 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -0800826 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -0800827
828 if (pushVlan) {
829 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
830 TrafficTreatment tt = DefaultTrafficTreatment.builder()
831 .pushVlan().setVlanId(vlanId).build();
832 fob.withMeta(tt);
833 } else {
834 fob.addCondition(Criteria.matchVlanId(vlanId));
835 }
836
Saurav Dasf9332192017-02-18 14:05:44 -0800837 fob.permit().fromApp(srManager.appId);
838 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700839 }
840
841 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700842 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700843 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700844 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700845 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700846 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700847 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700848 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700849 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -0700850 Ip4Address routerIpv4, pairRouterIpv4 = null;
851 Ip6Address routerIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -0800852 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800853 routerIpv4 = config.getRouterIpv4(deviceId);
854 routerIpv6 = config.getRouterIpv6(deviceId);
Saurav Das261c3002017-06-13 15:35:54 -0700855 if (config.isPairedEdge(deviceId)) {
856 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
857 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
858 }
Charles Chan319d1a22015-11-03 10:42:14 -0800859 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -0800860 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -0800861 return;
862 }
863
Saurav Dasc28b3432015-10-30 17:45:38 -0700864 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
865 log.debug("Not installing port-IP punts - not the master for dev:{} ",
866 deviceId);
867 return;
868 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800869 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
870 allIps.add(routerIpv4);
871 if (routerIpv6 != null) {
872 allIps.add(routerIpv6);
873 }
Saurav Das261c3002017-06-13 15:35:54 -0700874 if (pairRouterIpv4 != null) {
875 allIps.add(pairRouterIpv4);
876 }
877 if (pairRouterIpv6 != null) {
878 allIps.add(pairRouterIpv6);
879 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800880 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700881 populateSingleIpPunts(deviceId, ipaddr);
882 }
883 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800884
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700885 /**
886 * Creates a forwarding objective to punt all IP packets, destined to the
887 * specified IP address, which should be router's port IP address.
888 *
889 * @param deviceId the switch dpid for the router
890 * @param ipAddress the IP address of the router's port
891 */
892 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
893 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
894 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
895
896 srManager.packetService.requestPackets(sbuilder.build(),
897 PacketPriority.CONTROL, srManager.appId, optDeviceId);
898 }
899
900 /**
901 * Removes a forwarding objective to punt all IP packets, destined to the
902 * specified IP address, which should be router's port IP address.
903 *
904 * @param deviceId the switch dpid for the router
905 * @param ipAddress the IP address of the router's port
906 */
907 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
908 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
909 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
910
911 try {
912 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
913 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
914 srManager.packetService.cancelPackets(sbuilder.build(),
915 PacketPriority.CONTROL, srManager.appId, optDeviceId);
916 }
917 } catch (DeviceConfigNotFoundException e) {
918 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -0700919 }
sangho80f11cb2015-04-01 13:05:26 -0700920 }
921
Charles Chanf4586112015-11-09 16:37:23 -0800922 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800923 * Method to build IPv4 or IPv6 selector.
924 *
925 * @param addressToMatch the address to match
926 */
927 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
928 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
929 }
930
931 /**
932 * Method to build IPv4 or IPv6 selector.
933 *
934 * @param prefixToMatch the prefix to match
935 */
936 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
937 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700938 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800939 if (prefixToMatch.isIp4()) {
940 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
941 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
942 return selectorBuilder;
943 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700944 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800945 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
946 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
947 return selectorBuilder;
948 }
949
950 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800951 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
952 * Furthermore, these are applied only by the master instance. Deferred actions
953 * are not cleared such that packets can be flooded in the cross connect use case
954 *
955 * @param deviceId the switch dpid for the router
956 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700957 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700958 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800959 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
960 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
961 deviceId);
962 return;
963 }
964
Charles Chan3ed34d82017-06-22 18:03:14 -0700965 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -0800966 // We punt all ARP packets towards the controller.
Charles Chan3ed34d82017-06-22 18:03:14 -0700967 fwdObj = arpFwdObjective(null, true, PacketPriority.CONTROL.priorityValue())
Pier Luigib9632ba2017-01-12 18:14:58 -0800968 .add(new ObjectiveContext() {
969 @Override
970 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700971 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -0800972 deviceId, error);
973 }
974 });
Charles Chan3ed34d82017-06-22 18:03:14 -0700975 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -0800976
Pier Luigib9632ba2017-01-12 18:14:58 -0800977 // We punt all NDP packets towards the controller.
Charles Chan3ed34d82017-06-22 18:03:14 -0700978 fwdObj = ndpFwdObjective(null, true, PacketPriority.CONTROL.priorityValue())
Pier Luigib9632ba2017-01-12 18:14:58 -0800979 .add(new ObjectiveContext() {
980 @Override
981 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700982 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -0800983 deviceId, error);
984 }
985 });
Charles Chan3ed34d82017-06-22 18:03:14 -0700986 srManager.flowObjectiveService.forward(deviceId, fwdObj);
987
988 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
989 ForwardingObjective pairFwdObj;
990 // Do not punt ARP packets from pair port
991 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
992 .add(new ObjectiveContext() {
993 @Override
994 public void onError(Objective objective, ObjectiveError error) {
995 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
996 deviceId, error);
997 }
998 });
999 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1000
1001 // Do not punt NDP packets from pair port
1002 pairFwdObj = ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1003 .add(new ObjectiveContext() {
1004 @Override
1005 public void onError(Objective objective, ObjectiveError error) {
1006 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1007 deviceId, error);
1008 }
1009 });
1010 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1011
1012 // Do not forward DAD packets from pair port
1013 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1014 .add(new ObjectiveContext() {
1015 @Override
1016 public void onError(Objective objective, ObjectiveError error) {
1017 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1018 deviceId, error);
1019 }
1020 });
1021 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1022 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001023 }
1024
Charles Chan3ed34d82017-06-22 18:03:14 -07001025 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1026 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001027 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001028 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001029 .withSelector(selector)
1030 .fromApp(srManager.appId)
1031 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001032 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001033 .makePermanent();
1034 }
1035
Charles Chan3ed34d82017-06-22 18:03:14 -07001036 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001037 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1038 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001039 if (port != null) {
1040 sBuilder.matchInPort(port);
1041 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001042
Charles Chan3ed34d82017-06-22 18:03:14 -07001043 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1044 if (punt) {
1045 tBuilder.punt();
1046 }
1047 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001048 }
1049
Charles Chan3ed34d82017-06-22 18:03:14 -07001050 private ForwardingObjective.Builder ndpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001051 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1052 sBuilder.matchEthType(TYPE_IPV6)
1053 .matchIPProtocol(PROTOCOL_ICMP6)
Charles Chan3ed34d82017-06-22 18:03:14 -07001054 .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
1055 if (port != null) {
1056 sBuilder.matchInPort(port);
1057 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001058
Charles Chan3ed34d82017-06-22 18:03:14 -07001059 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1060 if (punt) {
1061 tBuilder.punt();
1062 }
1063 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
1064 }
1065
1066 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1067 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1068 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001069 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1070 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1071 // .matchIPProtocol(PROTOCOL_ICMP6)
1072 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001073 if (port != null) {
1074 sBuilder.matchInPort(port);
1075 }
1076
1077 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1078 tBuilder.wipeDeferred();
1079 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001080 }
1081
1082 /**
Charles Chanf4586112015-11-09 16:37:23 -08001083 * Populates a forwarding objective to send packets that miss other high
1084 * priority Bridging Table entries to a group that contains all ports of
1085 * its subnet.
1086 *
Charles Chanf4586112015-11-09 16:37:23 -08001087 * @param deviceId switch ID to set the rules
1088 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001089 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001090 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001091 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001092 });
1093 }
1094
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001095 /**
1096 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1097 * @param deviceId switch ID to set the rule
1098 * @param vlanId vlan ID to specify the subnet
1099 * @param install true to install the rule, false to revoke the rule
1100 */
1101 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1102 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1103
1104 if (nextId < 0) {
1105 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1106 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1107 return;
1108 }
1109
1110 // Driver should treat objective with MacAddress.NONE as the
1111 // subnet broadcast rule
1112 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1113 sbuilder.matchVlanId(vlanId);
1114 sbuilder.matchEthDst(MacAddress.NONE);
1115
1116 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1117 fob.withFlag(Flag.SPECIFIC)
1118 .withSelector(sbuilder.build())
1119 .nextStep(nextId)
1120 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1121 .fromApp(srManager.appId)
1122 .makePermanent();
1123 ObjectiveContext context = new DefaultObjectiveContext(
1124 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1125 (objective, error) ->
1126 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1127
1128 if (install) {
1129 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1130 } else {
1131 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1132 }
1133 }
1134
Charles Chan82ab1932016-01-30 23:22:37 -08001135 private int getPriorityFromPrefix(IpPrefix prefix) {
1136 return (prefix.isIp4()) ?
1137 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1138 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001139 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001140
1141 /**
1142 * Update Forwarding objective for each host and IP address connected to given port.
1143 * And create corresponding Simple Next objective if it does not exist.
1144 * Applied only when populating Forwarding objective
1145 * @param deviceId switch ID to set the rule
1146 * @param portNumber port number
1147 * @param prefix IP prefix of the route
1148 * @param hostMac MAC address of the next hop
1149 * @param vlanId Vlan ID of the port
1150 * @param popVlan true to pop vlan tag in TrafficTreatment
1151 * @param install true to populate the forwarding objective, false to revoke
1152 */
1153 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1154 VlanId vlanId, boolean popVlan, boolean install) {
1155 ForwardingObjective.Builder fob;
1156 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1157 MacAddress deviceMac;
1158 try {
1159 deviceMac = config.getDeviceMac(deviceId);
1160 } catch (DeviceConfigNotFoundException e) {
1161 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1162 return;
1163 }
1164
1165 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1166 tbuilder.deferred()
1167 .setEthDst(hostMac)
1168 .setEthSrc(deviceMac)
1169 .setOutput(portNumber);
1170
1171 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1172
1173 if (!popVlan) {
1174 tbuilder.setVlanId(vlanId);
1175 } else {
1176 mbuilder.matchVlanId(vlanId);
1177 }
1178
1179 // if the objective is to revoke an existing rule, and for some reason
1180 // the next-objective does not exist, then a new one should not be created
1181 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1182 tbuilder.build(), mbuilder.build(), install);
1183 if (portNextObjId == -1) {
1184 // Warning log will come from getPortNextObjective method
1185 return;
1186 }
1187
1188 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1189 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1190 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1191
1192 ObjectiveContext context = new DefaultObjectiveContext(
1193 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1194 (objective, error) ->
1195 log.warn("Failed to {} IP rule for route {}: {}",
1196 install ? "install" : "revoke", prefix, error));
1197 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1198
1199 if (!install) {
1200 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1201 if (grpHandler == null) {
1202 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1203 } else {
1204 // Remove L3UG for the given port and host
1205 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1206 }
1207 }
1208 }
sangho80f11cb2015-04-01 13:05:26 -07001209}