blob: 850962a73e565a23489e2b6fca6edc7db8aaaf44 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070037import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sangho80f11cb2015-04-01 13:05:26 -070038import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070039import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070040import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070041import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.net.flow.TrafficSelector;
44import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070045import org.onosproject.net.flow.criteria.Criteria;
46import org.onosproject.net.flowobjective.DefaultFilteringObjective;
47import org.onosproject.net.flowobjective.DefaultForwardingObjective;
48import org.onosproject.net.flowobjective.FilteringObjective;
49import org.onosproject.net.flowobjective.ForwardingObjective;
50import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070051import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070052import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
55import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070056import java.util.Collection;
57import java.util.Collections;
Saurav Dasc28b3432015-10-30 17:45:38 -070058import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070059import java.util.List;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080060import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070061import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070062import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070063
64import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080065import static org.onlab.packet.Ethernet.TYPE_ARP;
66import static org.onlab.packet.Ethernet.TYPE_IPV6;
67import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
68import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan10b0fb72017-02-02 16:20:42 -080069import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sangho80f11cb2015-04-01 13:05:26 -070070
Charles Chanb7f75ac2016-01-11 18:28:54 -080071/**
72 * Populator of segment routing flow rules.
73 */
sangho80f11cb2015-04-01 13:05:26 -070074public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070075 private static final Logger log = LoggerFactory
76 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070077
sanghofb7c7292015-04-13 15:15:58 -070078 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070079 private SegmentRoutingManager srManager;
80 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070081
sangho80f11cb2015-04-01 13:05:26 -070082 /**
83 * Creates a RoutingRulePopulator object.
84 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070085 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070086 */
87 public RoutingRulePopulator(SegmentRoutingManager srManager) {
88 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070089 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -070090 this.rulePopulationCounter = new AtomicLong(0);
91 }
92
93 /**
94 * Resets the population counter.
95 */
96 public void resetCounter() {
97 rulePopulationCounter.set(0);
98 }
99
100 /**
101 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700102 *
103 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700104 */
105 public long getCounter() {
106 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700107 }
108
109 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700110 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700111 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700112 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700113 * @param deviceId device ID of the device that next hop attaches to
114 * @param prefix IP prefix of the route
115 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800116 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700117 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700118 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700119 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800120 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das368cf212017-03-15 15:15:14 -0700121 log.debug("Populate routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700122 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800123 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800124 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700125 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
126 hostVlanId, outPort, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800127 } catch (DeviceConfigNotFoundException e) {
128 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
129 return;
130 }
Saurav Das07c74602016-04-27 18:35:50 -0700131 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700132 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700133 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700134 return;
135 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700136 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das368cf212017-03-15 15:15:14 -0700137 (objective) -> log.debug("Routing rule for route {} populated", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700138 (objective, error) ->
Saurav Das368cf212017-03-15 15:15:14 -0700139 log.warn("Failed to populate routing rule for route {}: {}",
140 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700141 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800142 rulePopulationCounter.incrementAndGet();
143 }
144
Charles Chanb7f75ac2016-01-11 18:28:54 -0800145 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700146 * Removes IP rules for a route when the next hop is gone.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800147 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700148 * @param deviceId device ID of the device that next hop attaches to
149 * @param prefix IP prefix of the route
150 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800151 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700152 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800153 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700154 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800155 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700156 log.debug("Revoke IP table entry for route {} at {}:{}",
157 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800158 ForwardingObjective.Builder fwdBuilder;
159 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700160 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
161 hostVlanId, outPort, true);
Charles Chanf4586112015-11-09 16:37:23 -0800162 } catch (DeviceConfigNotFoundException e) {
163 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
164 return;
165 }
Charles Chanea702b12016-11-30 11:55:05 -0800166 if (fwdBuilder == null) {
167 log.warn("Aborting host routing table entries due "
168 + "to error for dev:{} route:{}", deviceId, prefix);
169 return;
170 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700171 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700172 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700173 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700174 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700175 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800176 }
177
Charles Chanddac7fd2016-10-27 14:19:48 -0700178 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800179 * Returns a forwarding objective builder for routing rules.
180 * <p>
181 * The forwarding objective routes packets destined to a given prefix to
182 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700183 *
184 * @param deviceId device ID
185 * @param prefix prefix that need to be routed
186 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800187 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700188 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700189 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chanddac7fd2016-10-27 14:19:48 -0700190 * @return forwarding objective builder
191 * @throws DeviceConfigNotFoundException if given device is not configured
192 */
Charles Chan18fa4252017-02-08 16:10:40 -0800193 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700194 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700195 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
196 boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800197 throws DeviceConfigNotFoundException {
198 MacAddress deviceMac;
199 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800200
Charles Chan90772a72017-02-08 15:52:08 -0800201 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
202 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
203 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
204 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700205
Charles Chan90772a72017-02-08 15:52:08 -0800206 // Create route selector
207 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
208
209 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700210 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700211 tbuilder.deferred()
212 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800213 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700214 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800215
Charles Chan90772a72017-02-08 15:52:08 -0800216 // Create route meta
217 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800218
Charles Chan90772a72017-02-08 15:52:08 -0800219 // Adjust the meta according to VLAN configuration
220 if (taggedVlans.contains(hostVlanId)) {
221 tbuilder.setVlanId(hostVlanId);
222 } else if (hostVlanId.equals(VlanId.NONE)) {
223 if (untaggedVlan != null) {
224 mbuilder.matchVlanId(untaggedVlan);
225 } else if (nativeVlan != null) {
226 mbuilder.matchVlanId(nativeVlan);
227 } else {
228 // TODO: This check is turned off for now since vRouter still assumes that
229 // hosts are internally tagged with INTERNAL_VLAN.
230 // We should turn this back on when we move forward to the bridging CPR approach.
231 //
232 //log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
233 // hostMac, hostVlanId, connectPoint);
234 //return null;
235 mbuilder.matchVlanId(INTERNAL_VLAN);
236 }
237 } else {
Saurav Das2cb38292017-03-29 19:09:17 -0700238 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
239 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das07c74602016-04-27 18:35:50 -0700240 return null;
241 }
Saurav Das2cb38292017-03-29 19:09:17 -0700242 // if the objective is to revoke an existing rule, and for some reason
243 // the next-objective does not exist, then a new one should not be created
Charles Chan90772a72017-02-08 15:52:08 -0800244 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das2cb38292017-03-29 19:09:17 -0700245 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan90772a72017-02-08 15:52:08 -0800246 if (portNextObjId == -1) {
247 // Warning log will come from getPortNextObjective method
248 return null;
249 }
250
Charles Chanf4586112015-11-09 16:37:23 -0800251 return DefaultForwardingObjective.builder()
Charles Chan90772a72017-02-08 15:52:08 -0800252 .withSelector(sbuilder.build())
Saurav Das2d94d312015-11-24 23:21:05 -0800253 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800254 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700255 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800256 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700257 }
258
259 /**
260 * Populates IP flow rules for the subnets of the destination router.
261 *
262 * @param deviceId switch ID to set the rules
Charles Chanc22cef32016-04-29 14:38:22 -0700263 * @param subnets subnet being added
sangho80f11cb2015-04-01 13:05:26 -0700264 * @param destSw destination switch ID
265 * @param nextHops next hop switch ID list
266 * @return true if all rules are set successfully, false otherwise
267 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800268 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<IpPrefix> subnets,
Charles Chanc22cef32016-04-29 14:38:22 -0700269 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700270 for (IpPrefix subnet : subnets) {
sangho80f11cb2015-04-01 13:05:26 -0700271 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
272 return false;
273 }
274 }
Charles Chanc22cef32016-04-29 14:38:22 -0700275 return true;
276 }
sangho80f11cb2015-04-01 13:05:26 -0700277
Charles Chanc22cef32016-04-29 14:38:22 -0700278 /**
279 * Revokes IP flow rules for the subnets.
280 *
281 * @param subnets subnet being removed
282 * @return true if all rules are removed successfully, false otherwise
283 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800284 public boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700285 for (IpPrefix subnet : subnets) {
286 if (!revokeIpRuleForRouter(subnet)) {
287 return false;
288 }
289 }
sangho80f11cb2015-04-01 13:05:26 -0700290 return true;
291 }
292
293 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700294 * Populates IP flow rules for an IP prefix in the target device. The prefix
295 * is reachable via destination device.
sangho80f11cb2015-04-01 13:05:26 -0700296 *
Saurav Das88979182015-10-19 14:37:36 -0700297 * @param deviceId target device ID to set the rules
Pier Ventreadb4ae62016-11-23 09:57:42 -0800298 * @param ipPrefix the destination IP prefix
sangho80f11cb2015-04-01 13:05:26 -0700299 * @param destSw device ID of the destination router
300 * @param nextHops next hop switch ID list
301 * @return true if all rules are set successfully, false otherwise
302 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700303 public boolean populateIpRuleForRouter(DeviceId deviceId,
304 IpPrefix ipPrefix, DeviceId destSw,
305 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800306 int segmentId;
307 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800308 if (ipPrefix.isIp4()) {
309 segmentId = config.getIPv4SegmentId(destSw);
310 } else {
311 segmentId = config.getIPv6SegmentId(destSw);
312 }
Charles Chan319d1a22015-11-03 10:42:14 -0800313 } catch (DeviceConfigNotFoundException e) {
314 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
315 return false;
316 }
sangho80f11cb2015-04-01 13:05:26 -0700317
Pier Ventreadb4ae62016-11-23 09:57:42 -0800318 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800319 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700320
Charles Chanf4586112015-11-09 16:37:23 -0800321 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
322 NeighborSet ns;
323 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700324
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700325 // If the next hop is the same as the final destination, then MPLS label
326 // is not set.
sangho80f11cb2015-04-01 13:05:26 -0700327 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800328 tbuilder.immediate().decNwTtl();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700329 ns = new NeighborSet(nextHops, false);
Charles Chanf4586112015-11-09 16:37:23 -0800330 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700331 } else {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700332 ns = new NeighborSet(nextHops, false, segmentId);
Charles Chanf4586112015-11-09 16:37:23 -0800333 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700334 }
335
Saurav Das4c35fc42015-11-20 15:27:53 -0800336 // setup metadata to pass to nextObjective - indicate the vlan on egress
337 // if needed by the switch pipeline. Since neighbor sets are always to
338 // other neighboring routers, there is no subnet assigned on those ports.
339 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800340 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das4c35fc42015-11-20 15:27:53 -0800341
342 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
343 if (nextId <= 0) {
sangho2165d222015-05-01 09:38:25 -0700344 log.warn("No next objective in {} for ns: {}", deviceId, ns);
345 return false;
346 }
347
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700348 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
349 .builder()
350 .fromApp(srManager.appId)
351 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800352 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700353 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800354 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700355 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800356 if (treatment != null) {
357 fwdBuilder.withTreatment(treatment);
358 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700359 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700360 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700361 ipPrefix,
362 deviceId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700363 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700364 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
365 ipPrefix, deviceId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700366 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700367 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
368 ipPrefix, error, deviceId));
Charles Chan1eaf4802016-04-18 13:44:03 -0700369 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700370 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700371
372 return true;
373 }
374
sangho80f11cb2015-04-01 13:05:26 -0700375 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700376 * Revokes IP flow rules for the router IP address.
377 *
378 * @param ipPrefix the IP address of the destination router
379 * @return true if all rules are removed successfully, false otherwise
380 */
381 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800382 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700383 TrafficSelector selector = sbuilder.build();
384 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
385
386 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
387 .builder()
388 .fromApp(srManager.appId)
389 .makePermanent()
390 .withSelector(selector)
391 .withTreatment(dummyTreatment)
392 .withPriority(getPriorityFromPrefix(ipPrefix))
393 .withFlag(ForwardingObjective.Flag.SPECIFIC);
394
395 ObjectiveContext context = new DefaultObjectiveContext(
396 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
397 (objective, error) ->
398 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
399
400 srManager.deviceService.getAvailableDevices().forEach(device -> {
401 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
402 });
403
404 return true;
405 }
406
407 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700408 * Deals with !MPLS Bos use case.
409 *
410 * @param targetSwId the target sw
411 * @param destSwId the destination sw
412 * @param nextHops the set of next hops
413 * @param segmentId the segmentId to match
414 * @param routerIp the router ip
415 * @return a collection of fwdobjective
416 */
417 private Collection<ForwardingObjective> handleMpls(DeviceId targetSwId,
418 DeviceId destSwId,
419 Set<DeviceId> nextHops,
420 int segmentId,
421 IpAddress routerIp,
422 boolean isMplsBos) {
423
424 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
425 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800426 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700427 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
428 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
429 sbuilder.matchMplsBos(isMplsBos);
430 TrafficSelector selector = sbuilder.build();
431
432 // setup metadata to pass to nextObjective - indicate the vlan on egress
433 // if needed by the switch pipeline. Since mpls next-hops are always to
434 // other neighboring routers, there is no subnet assigned on those ports.
435 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800436 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700437
438 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
439 // If the next hop is the destination router for the segment, do pop
440 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
441 + "label {} in switch {} with pop", segmentId, targetSwId);
442 // Not-bos pop case (php for the current label). If MPLS-ECMP
443 // has been configured, the application we will request the
444 // installation for an MPLS-ECMP group.
Saurav Das2cb38292017-03-29 19:09:17 -0700445 ForwardingObjective.Builder fwdObjNoBosBuilder =
446 getMplsForwardingObjective(targetSwId,
447 nextHops,
448 true,
449 isMplsBos,
450 metabuilder.build(),
451 routerIp);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700452 // Error case, we cannot handle, exit.
453 if (fwdObjNoBosBuilder == null) {
454 return Collections.emptyList();
455 }
456 fwdObjBuilders.add(fwdObjNoBosBuilder);
457
458 } else {
459 // next hop is not destination, SR CONTINUE case (swap with self)
460 log.debug("Installing MPLS forwarding objective for "
461 + "label {} in switch {} without pop", segmentId, targetSwId);
462 // Not-bos pop case. If MPLS-ECMP has been configured, the
463 // application we will request the installation for an MPLS-ECMP
464 // group.
Saurav Das2cb38292017-03-29 19:09:17 -0700465 ForwardingObjective.Builder fwdObjNoBosBuilder =
466 getMplsForwardingObjective(targetSwId,
467 nextHops,
468 false,
469 isMplsBos,
470 metabuilder.build(),
471 routerIp);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700472 // Error case, we cannot handle, exit.
473 if (fwdObjNoBosBuilder == null) {
474 return Collections.emptyList();
475 }
476 fwdObjBuilders.add(fwdObjNoBosBuilder);
477
478 }
479
480 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
481 // We add the final property to the fwdObjs.
482 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
483
484 ((Builder) ((Builder) fwdObjBuilder
485 .fromApp(srManager.appId)
486 .makePermanent())
487 .withSelector(selector)
488 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
489 .withFlag(ForwardingObjective.Flag.SPECIFIC);
490
491 ObjectiveContext context = new DefaultObjectiveContext(
492 (objective) ->
493 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
494 objective.id(), segmentId, targetSwId),
495 (objective, error) ->
496 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
497 objective.id(), segmentId, error, targetSwId));
498
499 ForwardingObjective fob = fwdObjBuilder.add(context);
500 fwdObjs.add(fob);
501
502 }
503
504 return fwdObjs;
505 }
506
507 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700508 * Populates MPLS flow rules in the target device to point towards the
509 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700510 *
Saurav Dase0237a32016-05-27 13:54:07 -0700511 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700512 * @param destSwId destination switch device ID
513 * @param nextHops next hops switch ID list
Pier Ventre229fd0b2016-10-31 16:49:19 -0700514 * @param routerIp the router ip
sangho80f11cb2015-04-01 13:05:26 -0700515 * @return true if all rules are set successfully, false otherwise
516 */
Saurav Dase0237a32016-05-27 13:54:07 -0700517 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800518 Set<DeviceId> nextHops, IpAddress routerIp) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700519
Charles Chan319d1a22015-11-03 10:42:14 -0800520 int segmentId;
521 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800522 if (routerIp.isIp4()) {
523 segmentId = config.getIPv4SegmentId(destSwId);
524 } else {
525 segmentId = config.getIPv6SegmentId(destSwId);
526 }
Charles Chan319d1a22015-11-03 10:42:14 -0800527 } catch (DeviceConfigNotFoundException e) {
528 log.warn(e.getMessage() + " Aborting populateMplsRule.");
529 return false;
530 }
sangho80f11cb2015-04-01 13:05:26 -0700531
Pier Ventre229fd0b2016-10-31 16:49:19 -0700532 List<ForwardingObjective> fwdObjs = new ArrayList<>();
533 Collection<ForwardingObjective> fwdObjsMpls = Collections.emptyList();
534 // Generates the transit rules used by the standard "routing".
535 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
536 if (fwdObjsMpls.isEmpty()) {
537 return false;
sangho80f11cb2015-04-01 13:05:26 -0700538 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700539 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800540 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre229fd0b2016-10-31 16:49:19 -0700541 // the only case !BoS supported.
Saurav Dasf9332192017-02-18 14:05:44 -0800542 /*fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700543 if (fwdObjsMpls.isEmpty()) {
544 return false;
545 }
Saurav Dasf9332192017-02-18 14:05:44 -0800546 fwdObjs.addAll(fwdObjsMpls);*/
Pier Ventre229fd0b2016-10-31 16:49:19 -0700547
548 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700549 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700550 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
551 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700552 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700553 }
554
555 return true;
556 }
557
Saurav Das4c35fc42015-11-20 15:27:53 -0800558 private ForwardingObjective.Builder getMplsForwardingObjective(
559 DeviceId deviceId,
560 Set<DeviceId> nextHops,
561 boolean phpRequired,
562 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800563 TrafficSelector meta,
564 IpAddress routerIp) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800565
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700566 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
567 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700568
569 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
570
571 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800572 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700573 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700574 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700575 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800576 if (routerIp.isIp4()) {
577 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
578 } else {
579 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
580 }
581 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700582 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800583 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
584 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700585 }
586 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800587 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700588 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700589 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700590 }
591
Saurav Das4c35fc42015-11-20 15:27:53 -0800592 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700593 // if MPLS-ECMP == True we will build a standard NeighborSet.
594 // Otherwise a RandomNeighborSet.
595 NeighborSet ns = NeighborSet.neighborSet(false, nextHops, false);
596 if (!isBos && this.srManager.getMplsEcmp()) {
597 ns = NeighborSet.neighborSet(false, nextHops, true);
598 } else if (!isBos && !this.srManager.getMplsEcmp()) {
599 ns = NeighborSet.neighborSet(true, nextHops, true);
600 }
Saurav Das9455d702017-03-24 19:03:58 -0700601 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700602 deviceId, ns);
603 // If BoS == True, all forwarding is via L3 ECMP group.
604 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
605 // MPLS-Interface group. This depends on the configuration of the option
606 // MPLS-ECMP.
607 // The metadata informs the driver that the next-Objective will be used
608 // by MPLS flows and if Bos == False the driver will use MPLS groups.
609 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta, isBos);
Saurav Das4c35fc42015-11-20 15:27:53 -0800610 if (nextId <= 0) {
611 log.warn("No next objective in {} for ns: {}", deviceId, ns);
612 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700613 } else {
614 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
615 nextId, deviceId, ns);
sangho80f11cb2015-04-01 13:05:26 -0700616 }
617
Saurav Das4c35fc42015-11-20 15:27:53 -0800618 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700619 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700620 }
621
622 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700623 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700624 * dstMac corresponding to the router's MAC address. For those pipelines
625 * that need to internally assign vlans to untagged packets, this method
626 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700627 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800628 * Note that the vlan assignment and filter programming should only be done by
629 * the master for a switch. This method is typically called at deviceAdd and
630 * programs filters only for the enabled ports of the device. For port-updates,
631 * that enable/disable ports after device add, singlePortFilter methods should
632 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700633 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700634 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800635 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700636 */
Charles Chan18fa4252017-02-08 16:10:40 -0800637 public PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700638 log.debug("Installing per-port filtering objective for untagged "
639 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800640
Saurav Das07c74602016-04-27 18:35:50 -0700641 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800642 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700643 log.warn("Device {} ports not available. Unable to add MacVlan filters",
644 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800645 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700646 }
Saurav Dasf9332192017-02-18 14:05:44 -0800647 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700648 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700649 if (!port.isEnabled()) {
650 disabledPorts++;
651 continue;
652 }
Charles Chan43be46b2017-02-26 22:59:35 -0800653 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800654 filteredPorts++;
655 } else {
656 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700657 }
Saurav Das7c305372015-10-28 12:39:42 -0700658 }
Saurav Dasf9332192017-02-18 14:05:44 -0800659 log.info("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
660 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800661 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800662 errorPorts, filteredPorts);
663 }
664
665 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800666 * Creates or removes filtering objectives for a single port. Should only be
667 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800668 *
669 * @param deviceId device identifier
670 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800671 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800672 * @return true if no errors occurred during the build of the filtering objective
673 */
Charles Chan43be46b2017-02-26 22:59:35 -0800674 public boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800675 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
676 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
677 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
678 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
679
680 if (taggedVlans.size() != 0) {
681 // Filter for tagged vlans
682 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800683 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800684 return false;
685 }
686 if (nativeVlan != null) {
687 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800688 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800689 return false;
690 }
691 }
692 } else if (untaggedVlan != null) {
693 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800694 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800695 return false;
696 }
697 } else {
698 // Unconfigure port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800699 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800700 return false;
701 }
702 }
703 return true;
704 }
705
Charles Chan43be46b2017-02-26 22:59:35 -0800706 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
707 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800708 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800709 if (fob == null) {
710 // error encountered during build
711 return false;
712 }
Charles Chan43be46b2017-02-26 22:59:35 -0800713 log.info("{} filtering objectives for dev/port:{}/{}", (install ? "Installing" : "Removing"),
714 deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800715 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800716 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
717 (install ? "installed" : "removed")),
718 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
719 (install ? "install" : "remove"), deviceId, portnum, error));
720 if (install) {
721 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
722 } else {
723 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -0800724 }
Charles Chan90772a72017-02-08 15:52:08 -0800725 return true;
Saurav Dasf9332192017-02-18 14:05:44 -0800726 }
727
Charles Chan90772a72017-02-08 15:52:08 -0800728 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
729 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -0800730 MacAddress deviceMac;
731 try {
732 deviceMac = config.getDeviceMac(deviceId);
733 } catch (DeviceConfigNotFoundException e) {
734 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
735 return null;
736 }
Saurav Dasf9332192017-02-18 14:05:44 -0800737 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
738 fob.withKey(Criteria.matchInPort(portnum))
739 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -0800740 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -0800741
742 if (pushVlan) {
743 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
744 TrafficTreatment tt = DefaultTrafficTreatment.builder()
745 .pushVlan().setVlanId(vlanId).build();
746 fob.withMeta(tt);
747 } else {
748 fob.addCondition(Criteria.matchVlanId(vlanId));
749 }
750
Saurav Dasf9332192017-02-18 14:05:44 -0800751 fob.permit().fromApp(srManager.appId);
752 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700753 }
754
755 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700756 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700757 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700758 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700759 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700760 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700761 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700762 */
Charles Chan18fa4252017-02-08 16:10:40 -0800763 public void populateIpPunts(DeviceId deviceId) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800764 Ip4Address routerIpv4;
765 Ip6Address routerIpv6;
Charles Chan319d1a22015-11-03 10:42:14 -0800766 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800767 routerIpv4 = config.getRouterIpv4(deviceId);
768 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800769 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -0800770 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -0800771 return;
772 }
773
Saurav Dasc28b3432015-10-30 17:45:38 -0700774 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
775 log.debug("Not installing port-IP punts - not the master for dev:{} ",
776 deviceId);
777 return;
778 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800779 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
780 allIps.add(routerIpv4);
781 if (routerIpv6 != null) {
782 allIps.add(routerIpv6);
783 }
784 for (IpAddress ipaddr : allIps) {
785 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr);
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800786 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
787
788 srManager.packetService.requestPackets(sbuilder.build(),
789 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das9f1c42e2015-10-23 10:51:11 -0700790 }
sangho80f11cb2015-04-01 13:05:26 -0700791 }
792
Charles Chanf4586112015-11-09 16:37:23 -0800793 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800794 * Method to build IPv4 or IPv6 selector.
795 *
796 * @param addressToMatch the address to match
797 */
798 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
799 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
800 }
801
802 /**
803 * Method to build IPv4 or IPv6 selector.
804 *
805 * @param prefixToMatch the prefix to match
806 */
807 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
808 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700809 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800810 if (prefixToMatch.isIp4()) {
811 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
812 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
813 return selectorBuilder;
814 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700815 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800816 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
817 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
818 return selectorBuilder;
819 }
820
821 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800822 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
823 * Furthermore, these are applied only by the master instance. Deferred actions
824 * are not cleared such that packets can be flooded in the cross connect use case
825 *
826 * @param deviceId the switch dpid for the router
827 */
828 public void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700829 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800830 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
831 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
832 deviceId);
833 return;
834 }
835
836 // We punt all ARP packets towards the controller.
837 ForwardingObjective puntFwd = puntArpFwdObjective()
838 .add(new ObjectiveContext() {
839 @Override
840 public void onError(Objective objective, ObjectiveError error) {
841 log.warn("Failed to install packet request for ARP to {}: {}",
842 deviceId, error);
843 }
844 });
845 srManager.flowObjectiveService.forward(deviceId, puntFwd);
846
Pier Luigib9632ba2017-01-12 18:14:58 -0800847 // We punt all NDP packets towards the controller.
Pier Luigib9632ba2017-01-12 18:14:58 -0800848 puntFwd = puntNdpFwdObjective()
849 .add(new ObjectiveContext() {
850 @Override
851 public void onError(Objective objective, ObjectiveError error) {
852 log.warn("Failed to install packet request for NDP to {}: {}",
853 deviceId, error);
854 }
855 });
856 srManager.flowObjectiveService.forward(deviceId, puntFwd);
Pier Luigib9632ba2017-01-12 18:14:58 -0800857 }
858
859 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) {
860
861 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
862 tBuilder.punt();
863
864 return DefaultForwardingObjective.builder()
865 .withPriority(PacketPriority.CONTROL.priorityValue())
866 .withSelector(selector)
867 .fromApp(srManager.appId)
868 .withFlag(ForwardingObjective.Flag.VERSATILE)
869 .withTreatment(tBuilder.build())
870 .makePermanent();
871 }
872
873 private ForwardingObjective.Builder puntArpFwdObjective() {
874
875 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
876 sBuilder.matchEthType(TYPE_ARP);
877
878 return fwdObjBuilder(sBuilder.build());
879 }
880
881 private ForwardingObjective.Builder puntNdpFwdObjective() {
882
883 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
884 sBuilder.matchEthType(TYPE_IPV6)
885 .matchIPProtocol(PROTOCOL_ICMP6)
886 .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
887 .build();
888
889 return fwdObjBuilder(sBuilder.build());
890 }
891
892 /**
Charles Chanf4586112015-11-09 16:37:23 -0800893 * Populates a forwarding objective to send packets that miss other high
894 * priority Bridging Table entries to a group that contains all ports of
895 * its subnet.
896 *
897 * Note: We assume that packets sending from the edge switches to the hosts
898 * have untagged VLAN.
899 * The VLAN tag will be popped later in the flooding group.
900 *
901 * @param deviceId switch ID to set the rules
902 */
903 public void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -0800904 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan10b0fb72017-02-02 16:20:42 -0800905 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
906
907 if (nextId < 0) {
908 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
909 + "to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
Saurav Das2d94d312015-11-24 23:21:05 -0800910 return;
911 }
912
Pier Ventre229fd0b2016-10-31 16:49:19 -0700913 // Driver should treat objective with MacAddress.NONE as the
914 // subnet broadcast rule
Charles Chanf4586112015-11-09 16:37:23 -0800915 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
916 sbuilder.matchVlanId(vlanId);
917 sbuilder.matchEthDst(MacAddress.NONE);
918
919 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
920 fob.withFlag(Flag.SPECIFIC)
921 .withSelector(sbuilder.build())
922 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800923 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chanf4586112015-11-09 16:37:23 -0800924 .fromApp(srManager.appId)
925 .makePermanent();
Charles Chan1eaf4802016-04-18 13:44:03 -0700926 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan10b0fb72017-02-02 16:20:42 -0800927 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700928 (objective, error) ->
Charles Chan10b0fb72017-02-02 16:20:42 -0800929 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700930 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800931 });
932 }
933
Charles Chan82ab1932016-01-30 23:22:37 -0800934 private int getPriorityFromPrefix(IpPrefix prefix) {
935 return (prefix.isIp4()) ?
936 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
937 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700938 }
sangho80f11cb2015-04-01 13:05:26 -0700939}