blob: 85d6e1e3c9a31f432f9e34a7ffe8a9c8639f31a4 [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);
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800592 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre229fd0b2016-10-31 16:49:19 -0700593 // the only case !BoS supported.
Charles Chan3ed34d82017-06-22 18:03:14 -0700594 /*
595 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700596 if (fwdObjsMpls.isEmpty()) {
597 return false;
598 }
Charles Chan3ed34d82017-06-22 18:03:14 -0700599 fwdObjs.addAll(fwdObjsMpls);
600 */
Pier Ventre229fd0b2016-10-31 16:49:19 -0700601
602 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700603 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700604 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
605 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700606 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700607 }
608
609 return true;
610 }
611
Saurav Das4c35fc42015-11-20 15:27:53 -0800612 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700613 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800614 Set<DeviceId> nextHops,
615 boolean phpRequired,
616 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800617 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700618 IpAddress routerIp,
619 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800620
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700621 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
622 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700623
624 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
625
626 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800627 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700628 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700629 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700630 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800631 if (routerIp.isIp4()) {
632 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
633 } else {
634 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
635 }
636 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700637 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800638 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
639 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700640 }
641 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800642 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700643 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700644 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700645 }
646
Saurav Das4c35fc42015-11-20 15:27:53 -0800647 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700648 // if MPLS-ECMP == True we will build a standard NeighborSet.
649 // Otherwise a RandomNeighborSet.
Saurav Das261c3002017-06-13 15:35:54 -0700650 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700651 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700652 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700653 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700654 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700655 }
Saurav Das261c3002017-06-13 15:35:54 -0700656
Saurav Das9455d702017-03-24 19:03:58 -0700657 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700658 targetSw, ns);
659 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
660 if (gh == null) {
661 log.warn("getNextObjectiveId query - groupHandler for device {} "
662 + "not found", targetSw);
663 return null;
664 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700665 // If BoS == True, all forwarding is via L3 ECMP group.
666 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
667 // MPLS-Interface group. This depends on the configuration of the option
668 // MPLS-ECMP.
669 // The metadata informs the driver that the next-Objective will be used
670 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das261c3002017-06-13 15:35:54 -0700671 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
672 dstNextHops.put(destSw, nextHops);
673 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das4c35fc42015-11-20 15:27:53 -0800674 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700675 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das4c35fc42015-11-20 15:27:53 -0800676 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700677 } else {
678 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700679 nextId, targetSw, ns);
sangho80f11cb2015-04-01 13:05:26 -0700680 }
681
Saurav Das4c35fc42015-11-20 15:27:53 -0800682 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700683 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700684 }
685
686 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700687 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700688 * dstMac corresponding to the router's MAC address. For those pipelines
689 * that need to internally assign vlans to untagged packets, this method
690 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700691 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800692 * Note that the vlan assignment and filter programming should only be done by
693 * the master for a switch. This method is typically called at deviceAdd and
694 * programs filters only for the enabled ports of the device. For port-updates,
695 * that enable/disable ports after device add, singlePortFilter methods should
696 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700697 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700698 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800699 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700700 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700701 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700702 log.debug("Installing per-port filtering objective for untagged "
703 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800704
Saurav Das07c74602016-04-27 18:35:50 -0700705 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800706 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700707 log.warn("Device {} ports not available. Unable to add MacVlan filters",
708 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800709 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700710 }
Saurav Dasf9332192017-02-18 14:05:44 -0800711 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700712 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700713 if (!port.isEnabled()) {
714 disabledPorts++;
715 continue;
716 }
Charles Chan43be46b2017-02-26 22:59:35 -0800717 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800718 filteredPorts++;
719 } else {
720 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700721 }
Saurav Das7c305372015-10-28 12:39:42 -0700722 }
Charles Chan077314e2017-06-22 14:27:17 -0700723 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800724 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800725 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800726 errorPorts, filteredPorts);
727 }
728
729 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800730 * Creates or removes filtering objectives for a single port. Should only be
731 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800732 *
733 * @param deviceId device identifier
734 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800735 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800736 * @return true if no errors occurred during the build of the filtering objective
737 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700738 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800739 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
740 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
741 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
742 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
743
744 if (taggedVlans.size() != 0) {
745 // Filter for tagged vlans
746 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800747 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800748 return false;
749 }
750 if (nativeVlan != null) {
751 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800752 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800753 return false;
754 }
755 }
756 } else if (untaggedVlan != null) {
757 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800758 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800759 return false;
760 }
761 } else {
Saurav Dasfbe74572017-08-03 18:30:35 -0700762 // Unconfigured port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800763 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800764 return false;
765 }
766 }
767 return true;
768 }
769
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700770 /**
771 * Updates filtering objectives for a single port. Should only be called by
772 * the master for a switch
773 * @param deviceId device identifier
774 * @param portNum port identifier for port to be filtered
775 * @param pushVlan true to push vlan, false otherwise
776 * @param vlanId vlan identifier
777 * @param install true to install the filtering objective, false to remove
778 */
779 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
780 boolean pushVlan, VlanId vlanId, boolean install) {
781 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
782 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
783 deviceId, portNum, vlanId);
784 }
785 }
786
Charles Chan43be46b2017-02-26 22:59:35 -0800787 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
788 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800789 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800790 if (fob == null) {
791 // error encountered during build
792 return false;
793 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700794 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -0700795 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800796 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800797 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -0700798 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -0800799 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -0700800 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -0800801 if (install) {
802 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
803 } else {
804 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -0800805 }
Charles Chan90772a72017-02-08 15:52:08 -0800806 return true;
Saurav Dasf9332192017-02-18 14:05:44 -0800807 }
808
Charles Chan90772a72017-02-08 15:52:08 -0800809 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
810 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -0800811 MacAddress deviceMac;
812 try {
813 deviceMac = config.getDeviceMac(deviceId);
814 } catch (DeviceConfigNotFoundException e) {
815 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
816 return null;
817 }
Saurav Dasf9332192017-02-18 14:05:44 -0800818 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
819 fob.withKey(Criteria.matchInPort(portnum))
820 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -0800821 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -0800822
823 if (pushVlan) {
824 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
825 TrafficTreatment tt = DefaultTrafficTreatment.builder()
826 .pushVlan().setVlanId(vlanId).build();
827 fob.withMeta(tt);
828 } else {
829 fob.addCondition(Criteria.matchVlanId(vlanId));
830 }
831
Saurav Dasf9332192017-02-18 14:05:44 -0800832 fob.permit().fromApp(srManager.appId);
833 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700834 }
835
836 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700837 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700838 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700839 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700840 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700841 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700842 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700843 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700844 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -0700845 Ip4Address routerIpv4, pairRouterIpv4 = null;
846 Ip6Address routerIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -0800847 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800848 routerIpv4 = config.getRouterIpv4(deviceId);
849 routerIpv6 = config.getRouterIpv6(deviceId);
Saurav Das261c3002017-06-13 15:35:54 -0700850 if (config.isPairedEdge(deviceId)) {
851 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
852 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
853 }
Charles Chan319d1a22015-11-03 10:42:14 -0800854 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -0800855 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -0800856 return;
857 }
858
Saurav Dasc28b3432015-10-30 17:45:38 -0700859 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
860 log.debug("Not installing port-IP punts - not the master for dev:{} ",
861 deviceId);
862 return;
863 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800864 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
865 allIps.add(routerIpv4);
866 if (routerIpv6 != null) {
867 allIps.add(routerIpv6);
868 }
Saurav Das261c3002017-06-13 15:35:54 -0700869 if (pairRouterIpv4 != null) {
870 allIps.add(pairRouterIpv4);
871 }
872 if (pairRouterIpv6 != null) {
873 allIps.add(pairRouterIpv6);
874 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800875 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700876 populateSingleIpPunts(deviceId, ipaddr);
877 }
878 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800879
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700880 /**
881 * Creates a forwarding objective to punt all IP packets, destined to the
882 * specified IP address, which should be router's port IP address.
883 *
884 * @param deviceId the switch dpid for the router
885 * @param ipAddress the IP address of the router's port
886 */
887 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
888 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
889 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
890
891 srManager.packetService.requestPackets(sbuilder.build(),
892 PacketPriority.CONTROL, srManager.appId, optDeviceId);
893 }
894
895 /**
896 * Removes a forwarding objective to punt all IP packets, destined to the
897 * specified IP address, which should be router's port IP address.
898 *
899 * @param deviceId the switch dpid for the router
900 * @param ipAddress the IP address of the router's port
901 */
902 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
903 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
904 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
905
906 try {
907 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
908 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
909 srManager.packetService.cancelPackets(sbuilder.build(),
910 PacketPriority.CONTROL, srManager.appId, optDeviceId);
911 }
912 } catch (DeviceConfigNotFoundException e) {
913 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -0700914 }
sangho80f11cb2015-04-01 13:05:26 -0700915 }
916
Charles Chanf4586112015-11-09 16:37:23 -0800917 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800918 * Method to build IPv4 or IPv6 selector.
919 *
920 * @param addressToMatch the address to match
921 */
922 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
923 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
924 }
925
926 /**
927 * Method to build IPv4 or IPv6 selector.
928 *
929 * @param prefixToMatch the prefix to match
930 */
931 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
932 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700933 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800934 if (prefixToMatch.isIp4()) {
935 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
936 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
937 return selectorBuilder;
938 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700939 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800940 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
941 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
942 return selectorBuilder;
943 }
944
945 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800946 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
947 * Furthermore, these are applied only by the master instance. Deferred actions
948 * are not cleared such that packets can be flooded in the cross connect use case
949 *
950 * @param deviceId the switch dpid for the router
951 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700952 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700953 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800954 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
955 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
956 deviceId);
957 return;
958 }
959
Charles Chan3ed34d82017-06-22 18:03:14 -0700960 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -0800961 // We punt all ARP packets towards the controller.
Charles Chan3ed34d82017-06-22 18:03:14 -0700962 fwdObj = arpFwdObjective(null, true, PacketPriority.CONTROL.priorityValue())
Pier Luigib9632ba2017-01-12 18:14:58 -0800963 .add(new ObjectiveContext() {
964 @Override
965 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700966 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -0800967 deviceId, error);
968 }
969 });
Charles Chan3ed34d82017-06-22 18:03:14 -0700970 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -0800971
Pier Luigib9632ba2017-01-12 18:14:58 -0800972 // We punt all NDP packets towards the controller.
Charles Chan3ed34d82017-06-22 18:03:14 -0700973 fwdObj = ndpFwdObjective(null, true, PacketPriority.CONTROL.priorityValue())
Pier Luigib9632ba2017-01-12 18:14:58 -0800974 .add(new ObjectiveContext() {
975 @Override
976 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700977 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -0800978 deviceId, error);
979 }
980 });
Charles Chan3ed34d82017-06-22 18:03:14 -0700981 srManager.flowObjectiveService.forward(deviceId, fwdObj);
982
983 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
984 ForwardingObjective pairFwdObj;
985 // Do not punt ARP packets from pair port
986 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
987 .add(new ObjectiveContext() {
988 @Override
989 public void onError(Objective objective, ObjectiveError error) {
990 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
991 deviceId, error);
992 }
993 });
994 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
995
996 // Do not punt NDP packets from pair port
997 pairFwdObj = ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
998 .add(new ObjectiveContext() {
999 @Override
1000 public void onError(Objective objective, ObjectiveError error) {
1001 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1002 deviceId, error);
1003 }
1004 });
1005 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1006
1007 // Do not forward DAD packets from pair port
1008 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1009 .add(new ObjectiveContext() {
1010 @Override
1011 public void onError(Objective objective, ObjectiveError error) {
1012 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1013 deviceId, error);
1014 }
1015 });
1016 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1017 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001018 }
1019
Charles Chan3ed34d82017-06-22 18:03:14 -07001020 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1021 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001022 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001023 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001024 .withSelector(selector)
1025 .fromApp(srManager.appId)
1026 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001027 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001028 .makePermanent();
1029 }
1030
Charles Chan3ed34d82017-06-22 18:03:14 -07001031 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001032 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1033 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001034 if (port != null) {
1035 sBuilder.matchInPort(port);
1036 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001037
Charles Chan3ed34d82017-06-22 18:03:14 -07001038 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1039 if (punt) {
1040 tBuilder.punt();
1041 }
1042 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001043 }
1044
Charles Chan3ed34d82017-06-22 18:03:14 -07001045 private ForwardingObjective.Builder ndpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001046 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1047 sBuilder.matchEthType(TYPE_IPV6)
1048 .matchIPProtocol(PROTOCOL_ICMP6)
Charles Chan3ed34d82017-06-22 18:03:14 -07001049 .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
1050 if (port != null) {
1051 sBuilder.matchInPort(port);
1052 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001053
Charles Chan3ed34d82017-06-22 18:03:14 -07001054 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1055 if (punt) {
1056 tBuilder.punt();
1057 }
1058 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
1059 }
1060
1061 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1062 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1063 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001064 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1065 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1066 // .matchIPProtocol(PROTOCOL_ICMP6)
1067 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001068 if (port != null) {
1069 sBuilder.matchInPort(port);
1070 }
1071
1072 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1073 tBuilder.wipeDeferred();
1074 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001075 }
1076
1077 /**
Charles Chanf4586112015-11-09 16:37:23 -08001078 * Populates a forwarding objective to send packets that miss other high
1079 * priority Bridging Table entries to a group that contains all ports of
1080 * its subnet.
1081 *
Charles Chanf4586112015-11-09 16:37:23 -08001082 * @param deviceId switch ID to set the rules
1083 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001084 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001085 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001086 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001087 });
1088 }
1089
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001090 /**
1091 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1092 * @param deviceId switch ID to set the rule
1093 * @param vlanId vlan ID to specify the subnet
1094 * @param install true to install the rule, false to revoke the rule
1095 */
1096 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1097 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1098
1099 if (nextId < 0) {
1100 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1101 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1102 return;
1103 }
1104
1105 // Driver should treat objective with MacAddress.NONE as the
1106 // subnet broadcast rule
1107 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1108 sbuilder.matchVlanId(vlanId);
1109 sbuilder.matchEthDst(MacAddress.NONE);
1110
1111 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1112 fob.withFlag(Flag.SPECIFIC)
1113 .withSelector(sbuilder.build())
1114 .nextStep(nextId)
1115 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1116 .fromApp(srManager.appId)
1117 .makePermanent();
1118 ObjectiveContext context = new DefaultObjectiveContext(
1119 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1120 (objective, error) ->
1121 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1122
1123 if (install) {
1124 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1125 } else {
1126 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1127 }
1128 }
1129
Charles Chan82ab1932016-01-30 23:22:37 -08001130 private int getPriorityFromPrefix(IpPrefix prefix) {
1131 return (prefix.isIp4()) ?
1132 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1133 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001134 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001135
1136 /**
1137 * Update Forwarding objective for each host and IP address connected to given port.
1138 * And create corresponding Simple Next objective if it does not exist.
1139 * Applied only when populating Forwarding objective
1140 * @param deviceId switch ID to set the rule
1141 * @param portNumber port number
1142 * @param prefix IP prefix of the route
1143 * @param hostMac MAC address of the next hop
1144 * @param vlanId Vlan ID of the port
1145 * @param popVlan true to pop vlan tag in TrafficTreatment
1146 * @param install true to populate the forwarding objective, false to revoke
1147 */
1148 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1149 VlanId vlanId, boolean popVlan, boolean install) {
1150 ForwardingObjective.Builder fob;
1151 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1152 MacAddress deviceMac;
1153 try {
1154 deviceMac = config.getDeviceMac(deviceId);
1155 } catch (DeviceConfigNotFoundException e) {
1156 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1157 return;
1158 }
1159
1160 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1161 tbuilder.deferred()
1162 .setEthDst(hostMac)
1163 .setEthSrc(deviceMac)
1164 .setOutput(portNumber);
1165
1166 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1167
1168 if (!popVlan) {
1169 tbuilder.setVlanId(vlanId);
1170 } else {
1171 mbuilder.matchVlanId(vlanId);
1172 }
1173
1174 // if the objective is to revoke an existing rule, and for some reason
1175 // the next-objective does not exist, then a new one should not be created
1176 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1177 tbuilder.build(), mbuilder.build(), install);
1178 if (portNextObjId == -1) {
1179 // Warning log will come from getPortNextObjective method
1180 return;
1181 }
1182
1183 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1184 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1185 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1186
1187 ObjectiveContext context = new DefaultObjectiveContext(
1188 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1189 (objective, error) ->
1190 log.warn("Failed to {} IP rule for route {}: {}",
1191 install ? "install" : "revoke", prefix, error));
1192 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1193
1194 if (!install) {
1195 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1196 if (grpHandler == null) {
1197 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1198 } else {
1199 // Remove L3UG for the given port and host
1200 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1201 }
1202 }
1203 }
sangho80f11cb2015-04-01 13:05:26 -07001204}