blob: 02a0c7188deced3544237d4bce7f7becc43587fb [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sanghob35a6192015-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 Ventre917127a2016-10-31 16:49:19 -070018import com.google.common.collect.Lists;
Saurav Das8a0732e2015-11-20 15:27:53 -080019import org.onlab.packet.EthType;
sanghob35a6192015-04-01 13:05:26 -070020import org.onlab.packet.Ethernet;
21import org.onlab.packet.Ip4Address;
Pier Ventree0ae7a32016-11-23 09:57:42 -080022import org.onlab.packet.Ip6Address;
23import org.onlab.packet.IpAddress;
sanghob35a6192015-04-01 13:05:26 -070024import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MacAddress;
sanghob35a6192015-04-01 13:05:26 -070026import org.onlab.packet.MplsLabel;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070027import org.onlab.packet.VlanId;
Charles Chane849c192016-01-11 18:28:54 -080028import org.onosproject.net.ConnectPoint;
Charles Chand2990362016-04-18 13:44:03 -070029import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080030import org.onosproject.net.flowobjective.Objective;
Charles Chand2990362016-04-18 13:44:03 -070031import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080032import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2df0e8a2017-01-09 11:45:08 -080033import org.onosproject.net.packet.PacketPriority;
Saurav Dasd2fded02016-12-02 15:43:47 -080034import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan0b4e6182015-11-03 10:42:14 -080035import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
36import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Dasc88d4662017-05-15 15:34:25 -070037import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070038import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sanghob35a6192015-04-01 13:05:26 -070039import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070040import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070041import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070042import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070044import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-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 Das822c4e22015-10-23 10:51:11 -070052import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sanghob35a6192015-04-01 13:05:26 -070053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
56import java.util.ArrayList;
Pier Ventre917127a2016-10-31 16:49:19 -070057import java.util.Collection;
58import java.util.Collections;
Saurav Das837e0bb2015-10-30 17:45:38 -070059import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070060import java.util.List;
Charles Chan2df0e8a2017-01-09 11:45:08 -080061import java.util.Optional;
sanghob35a6192015-04-01 13:05:26 -070062import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070063import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070064
65import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080066import static org.onlab.packet.Ethernet.TYPE_ARP;
67import static org.onlab.packet.Ethernet.TYPE_IPV6;
68import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
69import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan59cc16d2017-02-02 16:20:42 -080070import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sanghob35a6192015-04-01 13:05:26 -070071
Charles Chane849c192016-01-11 18:28:54 -080072/**
73 * Populator of segment routing flow rules.
74 */
sanghob35a6192015-04-01 13:05:26 -070075public class RoutingRulePopulator {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070076 private static final Logger log = LoggerFactory
77 .getLogger(RoutingRulePopulator.class);
sanghob35a6192015-04-01 13:05:26 -070078
sangho20eff1d2015-04-13 15:15:58 -070079 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070080 private SegmentRoutingManager srManager;
81 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070082
sanghob35a6192015-04-01 13:05:26 -070083 /**
84 * Creates a RoutingRulePopulator object.
85 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070086 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070087 */
88 public RoutingRulePopulator(SegmentRoutingManager srManager) {
89 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070090 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-04-13 15:15:58 -070091 this.rulePopulationCounter = new AtomicLong(0);
92 }
93
94 /**
95 * Resets the population counter.
96 */
97 public void resetCounter() {
98 rulePopulationCounter.set(0);
99 }
100
101 /**
102 * Returns the number of rules populated.
Thomas Vachuska266b4432015-04-30 18:13:25 -0700103 *
104 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -0700105 */
106 public long getCounter() {
107 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -0700108 }
109
110 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700111 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700112 * switch.
sanghob35a6192015-04-01 13:05:26 -0700113 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700114 * @param deviceId device ID of the device that next hop attaches to
115 * @param prefix IP prefix of the route
116 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800117 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700118 * @param outPort port where the next hop attaches to
sanghob35a6192015-04-01 13:05:26 -0700119 */
Charles Chan1cdecff2016-10-27 14:19:48 -0700120 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800121 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das76ae6812017-03-15 15:15:14 -0700122 log.debug("Populate routing entry for route {} at {}:{}",
Charles Chan1cdecff2016-10-27 14:19:48 -0700123 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800124 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800125 try {
Saurav Das961beb22017-03-29 19:09:17 -0700126 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
127 hostVlanId, outPort, false);
Charles Chan0b4e6182015-11-03 10:42:14 -0800128 } catch (DeviceConfigNotFoundException e) {
129 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
130 return;
131 }
Saurav Das59232cf2016-04-27 18:35:50 -0700132 if (fwdBuilder == null) {
Saurav Das76ae6812017-03-15 15:15:14 -0700133 log.warn("Aborting host routing table entry due "
Charles Chan1cdecff2016-10-27 14:19:48 -0700134 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das59232cf2016-04-27 18:35:50 -0700135 return;
136 }
Charles Chand2990362016-04-18 13:44:03 -0700137 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das76ae6812017-03-15 15:15:14 -0700138 (objective) -> log.debug("Routing rule for route {} populated", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700139 (objective, error) ->
Saurav Das76ae6812017-03-15 15:15:14 -0700140 log.warn("Failed to populate routing rule for route {}: {}",
141 prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700142 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800143 rulePopulationCounter.incrementAndGet();
144 }
145
Charles Chane849c192016-01-11 18:28:54 -0800146 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700147 * Removes IP rules for a route when the next hop is gone.
Charles Chane849c192016-01-11 18:28:54 -0800148 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700149 * @param deviceId device ID of the device that next hop attaches to
150 * @param prefix IP prefix of the route
151 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800152 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700153 * @param outPort port that next hop attaches to
Charles Chane849c192016-01-11 18:28:54 -0800154 */
Charles Chan1cdecff2016-10-27 14:19:48 -0700155 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800156 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700157 log.debug("Revoke IP table entry for route {} at {}:{}",
158 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800159 ForwardingObjective.Builder fwdBuilder;
160 try {
Saurav Das961beb22017-03-29 19:09:17 -0700161 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
162 hostVlanId, outPort, true);
Charles Chan68aa62d2015-11-09 16:37:23 -0800163 } catch (DeviceConfigNotFoundException e) {
164 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
165 return;
166 }
Charles Chan458b8262016-11-30 11:55:05 -0800167 if (fwdBuilder == null) {
168 log.warn("Aborting host routing table entries due "
169 + "to error for dev:{} route:{}", deviceId, prefix);
170 return;
171 }
Charles Chand2990362016-04-18 13:44:03 -0700172 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700173 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700174 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700175 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700176 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800177 }
178
Charles Chan1cdecff2016-10-27 14:19:48 -0700179 /**
Charles Chanf6ec1532017-02-08 16:10:40 -0800180 * Returns a forwarding objective builder for routing rules.
181 * <p>
182 * The forwarding objective routes packets destined to a given prefix to
183 * given port on given device with given destination MAC.
Charles Chan1cdecff2016-10-27 14:19:48 -0700184 *
185 * @param deviceId device ID
186 * @param prefix prefix that need to be routed
187 * @param hostMac MAC address of the nexthop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800188 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700189 * @param outPort port where the nexthop attaches to
Saurav Das961beb22017-03-29 19:09:17 -0700190 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chan1cdecff2016-10-27 14:19:48 -0700191 * @return forwarding objective builder
192 * @throws DeviceConfigNotFoundException if given device is not configured
193 */
Charles Chanf6ec1532017-02-08 16:10:40 -0800194 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700195 DeviceId deviceId, IpPrefix prefix,
Saurav Das961beb22017-03-29 19:09:17 -0700196 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
197 boolean revoke)
Charles Chan68aa62d2015-11-09 16:37:23 -0800198 throws DeviceConfigNotFoundException {
199 MacAddress deviceMac;
200 deviceMac = config.getDeviceMac(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800201
Charles Chan7ffd81f2017-02-08 15:52:08 -0800202 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
203 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
204 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
205 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sanghob35a6192015-04-01 13:05:26 -0700206
Charles Chan7ffd81f2017-02-08 15:52:08 -0800207 // Create route selector
208 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
209
210 // Create route treatment
Charles Chan1cdecff2016-10-27 14:19:48 -0700211 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho1e575652015-05-14 00:39:53 -0700212 tbuilder.deferred()
213 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800214 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700215 .setOutput(outPort);
Saurav Das4ce45962015-11-24 23:21:05 -0800216
Charles Chan7ffd81f2017-02-08 15:52:08 -0800217 // Create route meta
218 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan59cc16d2017-02-02 16:20:42 -0800219
Charles Chan7ffd81f2017-02-08 15:52:08 -0800220 // Adjust the meta according to VLAN configuration
221 if (taggedVlans.contains(hostVlanId)) {
222 tbuilder.setVlanId(hostVlanId);
223 } else if (hostVlanId.equals(VlanId.NONE)) {
224 if (untaggedVlan != null) {
225 mbuilder.matchVlanId(untaggedVlan);
226 } else if (nativeVlan != null) {
227 mbuilder.matchVlanId(nativeVlan);
228 } else {
229 // TODO: This check is turned off for now since vRouter still assumes that
230 // hosts are internally tagged with INTERNAL_VLAN.
231 // We should turn this back on when we move forward to the bridging CPR approach.
232 //
233 //log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
234 // hostMac, hostVlanId, connectPoint);
235 //return null;
236 mbuilder.matchVlanId(INTERNAL_VLAN);
237 }
238 } else {
Saurav Das961beb22017-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 Das59232cf2016-04-27 18:35:50 -0700241 return null;
242 }
Saurav Das961beb22017-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 Chan7ffd81f2017-02-08 15:52:08 -0800245 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das961beb22017-03-29 19:09:17 -0700246 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800247 if (portNextObjId == -1) {
248 // Warning log will come from getPortNextObjective method
249 return null;
250 }
251
Charles Chan68aa62d2015-11-09 16:37:23 -0800252 return DefaultForwardingObjective.builder()
Charles Chan7ffd81f2017-02-08 15:52:08 -0800253 .withSelector(sbuilder.build())
Saurav Das4ce45962015-11-24 23:21:05 -0800254 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800255 .fromApp(srManager.appId).makePermanent()
Charles Chan1cdecff2016-10-27 14:19:48 -0700256 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan5270ed02016-01-30 23:22:37 -0800257 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700258 }
259
260 /**
261 * Populates IP flow rules for the subnets of the destination router.
262 *
263 * @param deviceId switch ID to set the rules
Charles Chan93e71ba2016-04-29 14:38:22 -0700264 * @param subnets subnet being added
sanghob35a6192015-04-01 13:05:26 -0700265 * @param destSw destination switch ID
266 * @param nextHops next hop switch ID list
267 * @return true if all rules are set successfully, false otherwise
268 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800269 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<IpPrefix> subnets,
Charles Chan93e71ba2016-04-29 14:38:22 -0700270 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700271 for (IpPrefix subnet : subnets) {
sanghob35a6192015-04-01 13:05:26 -0700272 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
273 return false;
274 }
275 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700276 return true;
277 }
sanghob35a6192015-04-01 13:05:26 -0700278
Charles Chan93e71ba2016-04-29 14:38:22 -0700279 /**
280 * Revokes IP flow rules for the subnets.
281 *
282 * @param subnets subnet being removed
283 * @return true if all rules are removed successfully, false otherwise
284 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800285 public boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700286 for (IpPrefix subnet : subnets) {
287 if (!revokeIpRuleForRouter(subnet)) {
288 return false;
289 }
290 }
sanghob35a6192015-04-01 13:05:26 -0700291 return true;
292 }
293
294 /**
Saurav Das25190812016-05-27 13:54:07 -0700295 * Populates IP flow rules for an IP prefix in the target device. The prefix
296 * is reachable via destination device.
sanghob35a6192015-04-01 13:05:26 -0700297 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700298 * @param deviceId target device ID to set the rules
Pier Ventree0ae7a32016-11-23 09:57:42 -0800299 * @param ipPrefix the destination IP prefix
sanghob35a6192015-04-01 13:05:26 -0700300 * @param destSw device ID of the destination router
301 * @param nextHops next hop switch ID list
302 * @return true if all rules are set successfully, false otherwise
303 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700304 public boolean populateIpRuleForRouter(DeviceId deviceId,
305 IpPrefix ipPrefix, DeviceId destSw,
306 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800307 int segmentId;
308 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800309 if (ipPrefix.isIp4()) {
310 segmentId = config.getIPv4SegmentId(destSw);
311 } else {
312 segmentId = config.getIPv6SegmentId(destSw);
313 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800314 } catch (DeviceConfigNotFoundException e) {
315 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
316 return false;
317 }
sanghob35a6192015-04-01 13:05:26 -0700318
Pier Ventree0ae7a32016-11-23 09:57:42 -0800319 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan68aa62d2015-11-09 16:37:23 -0800320 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700321
Charles Chan68aa62d2015-11-09 16:37:23 -0800322 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
323 NeighborSet ns;
324 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700325
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700326 // If the next hop is the same as the final destination, then MPLS label
327 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700328 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800329 tbuilder.immediate().decNwTtl();
Saurav Dasc88d4662017-05-15 15:34:25 -0700330 ns = new NeighborSet(nextHops, false, destSw);
Charles Chan68aa62d2015-11-09 16:37:23 -0800331 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700332 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700333 ns = new NeighborSet(nextHops, false, segmentId, destSw);
Charles Chan68aa62d2015-11-09 16:37:23 -0800334 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700335 }
336
Saurav Das8a0732e2015-11-20 15:27:53 -0800337 // setup metadata to pass to nextObjective - indicate the vlan on egress
338 // if needed by the switch pipeline. Since neighbor sets are always to
339 // other neighboring routers, there is no subnet assigned on those ports.
340 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800341 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Dasc88d4662017-05-15 15:34:25 -0700342 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
343 if (grpHandler == null) {
344 log.warn("populateIPRuleForRouter: groupHandler for device {} "
345 + "not found", deviceId);
346 return false;
347 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800348
Saurav Dasc88d4662017-05-15 15:34:25 -0700349 int nextId = grpHandler.getNextObjectiveId(ns, metabuilder.build(), true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800350 if (nextId <= 0) {
sangho834e4b02015-05-01 09:38:25 -0700351 log.warn("No next objective in {} for ns: {}", deviceId, ns);
352 return false;
353 }
354
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700355 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
356 .builder()
357 .fromApp(srManager.appId)
358 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800359 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700360 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800361 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700362 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800363 if (treatment != null) {
364 fwdBuilder.withTreatment(treatment);
365 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700366 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
367 + "in switch {} with nextId: {}", ipPrefix, deviceId, nextId);
Charles Chand2990362016-04-18 13:44:03 -0700368 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700369 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
370 ipPrefix, deviceId),
Charles Chand2990362016-04-18 13:44:03 -0700371 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700372 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
373 ipPrefix, error, deviceId));
Charles Chand2990362016-04-18 13:44:03 -0700374 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700375 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700376
377 return true;
378 }
379
sanghob35a6192015-04-01 13:05:26 -0700380 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700381 * Revokes IP flow rules for the router IP address.
382 *
383 * @param ipPrefix the IP address of the destination router
384 * @return true if all rules are removed successfully, false otherwise
385 */
386 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre968da122016-12-09 17:26:04 -0800387 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700388 TrafficSelector selector = sbuilder.build();
389 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
390
391 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
392 .builder()
393 .fromApp(srManager.appId)
394 .makePermanent()
395 .withSelector(selector)
396 .withTreatment(dummyTreatment)
397 .withPriority(getPriorityFromPrefix(ipPrefix))
398 .withFlag(ForwardingObjective.Flag.SPECIFIC);
399
400 ObjectiveContext context = new DefaultObjectiveContext(
401 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
402 (objective, error) ->
403 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
404
405 srManager.deviceService.getAvailableDevices().forEach(device -> {
406 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
407 });
408
409 return true;
410 }
411
412 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700413 * Deals with !MPLS Bos use case.
414 *
415 * @param targetSwId the target sw
416 * @param destSwId the destination sw
417 * @param nextHops the set of next hops
418 * @param segmentId the segmentId to match
419 * @param routerIp the router ip
420 * @return a collection of fwdobjective
421 */
422 private Collection<ForwardingObjective> handleMpls(DeviceId targetSwId,
423 DeviceId destSwId,
424 Set<DeviceId> nextHops,
425 int segmentId,
426 IpAddress routerIp,
427 boolean isMplsBos) {
428
429 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
430 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800431 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre917127a2016-10-31 16:49:19 -0700432 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
433 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
434 sbuilder.matchMplsBos(isMplsBos);
435 TrafficSelector selector = sbuilder.build();
436
437 // setup metadata to pass to nextObjective - indicate the vlan on egress
438 // if needed by the switch pipeline. Since mpls next-hops are always to
439 // other neighboring routers, there is no subnet assigned on those ports.
440 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800441 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre917127a2016-10-31 16:49:19 -0700442
443 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
444 // If the next hop is the destination router for the segment, do pop
445 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Dasc88d4662017-05-15 15:34:25 -0700446 + "label {} in switch {} with pop", segmentId, targetSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700447 // Not-bos pop case (php for the current label). If MPLS-ECMP
448 // has been configured, the application we will request the
449 // installation for an MPLS-ECMP group.
Saurav Das961beb22017-03-29 19:09:17 -0700450 ForwardingObjective.Builder fwdObjNoBosBuilder =
451 getMplsForwardingObjective(targetSwId,
452 nextHops,
453 true,
454 isMplsBos,
455 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700456 routerIp,
457 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700458 // Error case, we cannot handle, exit.
459 if (fwdObjNoBosBuilder == null) {
460 return Collections.emptyList();
461 }
462 fwdObjBuilders.add(fwdObjNoBosBuilder);
463
464 } else {
465 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Dasc88d4662017-05-15 15:34:25 -0700466 log.debug("Installing MPLS forwarding objective for label {} in "
467 + "switch {} without pop", segmentId, targetSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700468 // Not-bos pop case. If MPLS-ECMP has been configured, the
469 // application we will request the installation for an MPLS-ECMP
470 // group.
Saurav Das961beb22017-03-29 19:09:17 -0700471 ForwardingObjective.Builder fwdObjNoBosBuilder =
472 getMplsForwardingObjective(targetSwId,
473 nextHops,
474 false,
475 isMplsBos,
476 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700477 routerIp,
478 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700479 // Error case, we cannot handle, exit.
480 if (fwdObjNoBosBuilder == null) {
481 return Collections.emptyList();
482 }
483 fwdObjBuilders.add(fwdObjNoBosBuilder);
484
485 }
486
487 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
488 // We add the final property to the fwdObjs.
489 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
490
491 ((Builder) ((Builder) fwdObjBuilder
492 .fromApp(srManager.appId)
493 .makePermanent())
494 .withSelector(selector)
495 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
496 .withFlag(ForwardingObjective.Flag.SPECIFIC);
497
498 ObjectiveContext context = new DefaultObjectiveContext(
499 (objective) ->
500 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
501 objective.id(), segmentId, targetSwId),
502 (objective, error) ->
503 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
504 objective.id(), segmentId, error, targetSwId));
505
506 ForwardingObjective fob = fwdObjBuilder.add(context);
507 fwdObjs.add(fob);
508
509 }
510
511 return fwdObjs;
512 }
513
514 /**
Saurav Das25190812016-05-27 13:54:07 -0700515 * Populates MPLS flow rules in the target device to point towards the
516 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700517 *
Saurav Das25190812016-05-27 13:54:07 -0700518 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700519 * @param destSwId destination switch device ID
520 * @param nextHops next hops switch ID list
Pier Ventre917127a2016-10-31 16:49:19 -0700521 * @param routerIp the router ip
sanghob35a6192015-04-01 13:05:26 -0700522 * @return true if all rules are set successfully, false otherwise
523 */
Saurav Das25190812016-05-27 13:54:07 -0700524 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800525 Set<DeviceId> nextHops, IpAddress routerIp) {
Pier Ventre917127a2016-10-31 16:49:19 -0700526
Charles Chan0b4e6182015-11-03 10:42:14 -0800527 int segmentId;
528 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800529 if (routerIp.isIp4()) {
530 segmentId = config.getIPv4SegmentId(destSwId);
531 } else {
532 segmentId = config.getIPv6SegmentId(destSwId);
533 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800534 } catch (DeviceConfigNotFoundException e) {
535 log.warn(e.getMessage() + " Aborting populateMplsRule.");
536 return false;
537 }
sanghob35a6192015-04-01 13:05:26 -0700538
Pier Ventre917127a2016-10-31 16:49:19 -0700539 List<ForwardingObjective> fwdObjs = new ArrayList<>();
540 Collection<ForwardingObjective> fwdObjsMpls = Collections.emptyList();
541 // Generates the transit rules used by the standard "routing".
542 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
543 if (fwdObjsMpls.isEmpty()) {
544 return false;
sanghob35a6192015-04-01 13:05:26 -0700545 }
Pier Ventre917127a2016-10-31 16:49:19 -0700546 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800547 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre917127a2016-10-31 16:49:19 -0700548 // the only case !BoS supported.
Saurav Das018605f2017-02-18 14:05:44 -0800549 /*fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre917127a2016-10-31 16:49:19 -0700550 if (fwdObjsMpls.isEmpty()) {
551 return false;
552 }
Saurav Das018605f2017-02-18 14:05:44 -0800553 fwdObjs.addAll(fwdObjsMpls);*/
Pier Ventre917127a2016-10-31 16:49:19 -0700554
555 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Das25190812016-05-27 13:54:07 -0700556 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre917127a2016-10-31 16:49:19 -0700557 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
558 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sangho20eff1d2015-04-13 15:15:58 -0700559 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700560 }
561
562 return true;
563 }
564
Saurav Das8a0732e2015-11-20 15:27:53 -0800565 private ForwardingObjective.Builder getMplsForwardingObjective(
566 DeviceId deviceId,
567 Set<DeviceId> nextHops,
568 boolean phpRequired,
569 boolean isBos,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800570 TrafficSelector meta,
Saurav Dasc88d4662017-05-15 15:34:25 -0700571 IpAddress routerIp,
572 DeviceId destSw) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800573
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700574 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
575 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700576
577 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
578
579 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800580 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700581 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700582 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700583 if (isBos) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800584 if (routerIp.isIp4()) {
585 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
586 } else {
587 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
588 }
589 tbuilder.decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700590 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800591 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
592 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700593 }
594 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800595 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700596 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700597 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700598 }
599
Saurav Das8a0732e2015-11-20 15:27:53 -0800600 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre917127a2016-10-31 16:49:19 -0700601 // if MPLS-ECMP == True we will build a standard NeighborSet.
602 // Otherwise a RandomNeighborSet.
Saurav Dasc88d4662017-05-15 15:34:25 -0700603 NeighborSet ns = NeighborSet.neighborSet(false, nextHops, false, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700604 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700605 ns = NeighborSet.neighborSet(false, nextHops, true, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700606 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700607 ns = NeighborSet.neighborSet(true, nextHops, true, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700608 }
Saurav Dasb28d5dd2017-03-24 19:03:58 -0700609 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Pier Ventre917127a2016-10-31 16:49:19 -0700610 deviceId, ns);
611 // If BoS == True, all forwarding is via L3 ECMP group.
612 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
613 // MPLS-Interface group. This depends on the configuration of the option
614 // MPLS-ECMP.
615 // The metadata informs the driver that the next-Objective will be used
616 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Dasc88d4662017-05-15 15:34:25 -0700617 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
618 if (grpHandler == null) {
619 log.warn("populateIPRuleForRouter: groupHandler for device {} "
620 + "not found", deviceId);
621 return null;
622 }
623 int nextId = grpHandler.getNextObjectiveId(ns, meta, isBos);
Saurav Das8a0732e2015-11-20 15:27:53 -0800624 if (nextId <= 0) {
625 log.warn("No next objective in {} for ns: {}", deviceId, ns);
626 return null;
Saurav Das25190812016-05-27 13:54:07 -0700627 } else {
628 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
629 nextId, deviceId, ns);
sanghob35a6192015-04-01 13:05:26 -0700630 }
631
Saurav Das8a0732e2015-11-20 15:27:53 -0800632 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700633 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700634 }
635
636 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700637 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700638 * dstMac corresponding to the router's MAC address. For those pipelines
639 * that need to internally assign vlans to untagged packets, this method
640 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700641 * <p>
Saurav Das018605f2017-02-18 14:05:44 -0800642 * Note that the vlan assignment and filter programming should only be done by
643 * the master for a switch. This method is typically called at deviceAdd and
644 * programs filters only for the enabled ports of the device. For port-updates,
645 * that enable/disable ports after device add, singlePortFilter methods should
646 * be called.
sanghob35a6192015-04-01 13:05:26 -0700647 *
Saurav Das822c4e22015-10-23 10:51:11 -0700648 * @param deviceId the switch dpid for the router
Saurav Dasd2fded02016-12-02 15:43:47 -0800649 * @return PortFilterInfo information about the processed ports
sanghob35a6192015-04-01 13:05:26 -0700650 */
Charles Chanf6ec1532017-02-08 16:10:40 -0800651 public PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700652 log.debug("Installing per-port filtering objective for untagged "
653 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800654
Saurav Das59232cf2016-04-27 18:35:50 -0700655 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hallcbd1b392017-01-18 20:15:44 -0800656 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das59232cf2016-04-27 18:35:50 -0700657 log.warn("Device {} ports not available. Unable to add MacVlan filters",
658 deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -0800659 return null;
Saurav Das59232cf2016-04-27 18:35:50 -0700660 }
Saurav Das018605f2017-02-18 14:05:44 -0800661 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700662 for (Port port : devPorts) {
Saurav Das25190812016-05-27 13:54:07 -0700663 if (!port.isEnabled()) {
664 disabledPorts++;
665 continue;
666 }
Charles Chan7e4f8192017-02-26 22:59:35 -0800667 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Das018605f2017-02-18 14:05:44 -0800668 filteredPorts++;
669 } else {
670 errorPorts++;
Saurav Das25190812016-05-27 13:54:07 -0700671 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700672 }
Charles Chan7f9737b2017-06-22 14:27:17 -0700673 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Das018605f2017-02-18 14:05:44 -0800674 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd2fded02016-12-02 15:43:47 -0800675 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Das018605f2017-02-18 14:05:44 -0800676 errorPorts, filteredPorts);
677 }
678
679 /**
Charles Chan7e4f8192017-02-26 22:59:35 -0800680 * Creates or removes filtering objectives for a single port. Should only be
681 * called by the master for a switch.
Saurav Das018605f2017-02-18 14:05:44 -0800682 *
683 * @param deviceId device identifier
684 * @param portnum port identifier for port to be filtered
Charles Chan7e4f8192017-02-26 22:59:35 -0800685 * @param install true to install the filtering objective, false to remove
Saurav Das018605f2017-02-18 14:05:44 -0800686 * @return true if no errors occurred during the build of the filtering objective
687 */
Charles Chan7e4f8192017-02-26 22:59:35 -0800688 public boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800689 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
690 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
691 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
692 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
693
694 if (taggedVlans.size() != 0) {
695 // Filter for tagged vlans
696 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan7e4f8192017-02-26 22:59:35 -0800697 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800698 return false;
699 }
700 if (nativeVlan != null) {
701 // Filter for native vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800702 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800703 return false;
704 }
705 }
706 } else if (untaggedVlan != null) {
707 // Filter for untagged vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800708 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800709 return false;
710 }
711 } else {
712 // Unconfigure port, use INTERNAL_VLAN
Charles Chan7e4f8192017-02-26 22:59:35 -0800713 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800714 return false;
715 }
716 }
717 return true;
718 }
719
Charles Chan7e4f8192017-02-26 22:59:35 -0800720 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
721 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800722 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Das018605f2017-02-18 14:05:44 -0800723 if (fob == null) {
724 // error encountered during build
725 return false;
726 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700727 log.debug("{} filtering objectives for dev/port:{}/{}",
728 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Das018605f2017-02-18 14:05:44 -0800729 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan7e4f8192017-02-26 22:59:35 -0800730 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Dasc88d4662017-05-15 15:34:25 -0700731 install ? "installed" : "removed"),
Charles Chan7e4f8192017-02-26 22:59:35 -0800732 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Dasc88d4662017-05-15 15:34:25 -0700733 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan7e4f8192017-02-26 22:59:35 -0800734 if (install) {
735 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
736 } else {
737 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan7ffd81f2017-02-08 15:52:08 -0800738 }
Charles Chan7ffd81f2017-02-08 15:52:08 -0800739 return true;
Saurav Das018605f2017-02-18 14:05:44 -0800740 }
741
Charles Chan7ffd81f2017-02-08 15:52:08 -0800742 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
743 boolean pushVlan, VlanId vlanId) {
Saurav Das018605f2017-02-18 14:05:44 -0800744 MacAddress deviceMac;
745 try {
746 deviceMac = config.getDeviceMac(deviceId);
747 } catch (DeviceConfigNotFoundException e) {
748 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
749 return null;
750 }
Saurav Das018605f2017-02-18 14:05:44 -0800751 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
752 fob.withKey(Criteria.matchInPort(portnum))
753 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Das018605f2017-02-18 14:05:44 -0800754 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800755
756 if (pushVlan) {
757 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
758 TrafficTreatment tt = DefaultTrafficTreatment.builder()
759 .pushVlan().setVlanId(vlanId).build();
760 fob.withMeta(tt);
761 } else {
762 fob.addCondition(Criteria.matchVlanId(vlanId));
763 }
764
Saurav Das018605f2017-02-18 14:05:44 -0800765 fob.permit().fromApp(srManager.appId);
766 return fob;
sanghob35a6192015-04-01 13:05:26 -0700767 }
768
769 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700770 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700771 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700772 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700773 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700774 *
Saurav Das822c4e22015-10-23 10:51:11 -0700775 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700776 */
Charles Chanf6ec1532017-02-08 16:10:40 -0800777 public void populateIpPunts(DeviceId deviceId) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800778 Ip4Address routerIpv4;
779 Ip6Address routerIpv6;
Charles Chan0b4e6182015-11-03 10:42:14 -0800780 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800781 routerIpv4 = config.getRouterIpv4(deviceId);
782 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800783 } catch (DeviceConfigNotFoundException e) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800784 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800785 return;
786 }
787
Saurav Das837e0bb2015-10-30 17:45:38 -0700788 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
789 log.debug("Not installing port-IP punts - not the master for dev:{} ",
790 deviceId);
791 return;
792 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800793 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
794 allIps.add(routerIpv4);
795 if (routerIpv6 != null) {
796 allIps.add(routerIpv6);
797 }
798 for (IpAddress ipaddr : allIps) {
799 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr);
Charles Chan2df0e8a2017-01-09 11:45:08 -0800800 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
801
802 srManager.packetService.requestPackets(sbuilder.build(),
803 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700804 }
sanghob35a6192015-04-01 13:05:26 -0700805 }
806
Charles Chan68aa62d2015-11-09 16:37:23 -0800807 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800808 * Method to build IPv4 or IPv6 selector.
809 *
810 * @param addressToMatch the address to match
811 */
812 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
813 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
814 }
815
816 /**
817 * Method to build IPv4 or IPv6 selector.
818 *
819 * @param prefixToMatch the prefix to match
820 */
821 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
822 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre917127a2016-10-31 16:49:19 -0700823 // If the prefix is IPv4
Pier Ventree0ae7a32016-11-23 09:57:42 -0800824 if (prefixToMatch.isIp4()) {
825 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
826 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
827 return selectorBuilder;
828 }
Pier Ventre917127a2016-10-31 16:49:19 -0700829 // If the prefix is IPv6
Pier Ventree0ae7a32016-11-23 09:57:42 -0800830 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
831 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
832 return selectorBuilder;
833 }
834
835 /**
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800836 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
837 * Furthermore, these are applied only by the master instance. Deferred actions
838 * are not cleared such that packets can be flooded in the cross connect use case
839 *
840 * @param deviceId the switch dpid for the router
841 */
842 public void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre917127a2016-10-31 16:49:19 -0700843 // We are not the master just skip.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800844 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
845 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
846 deviceId);
847 return;
848 }
849
850 // We punt all ARP packets towards the controller.
851 ForwardingObjective puntFwd = puntArpFwdObjective()
852 .add(new ObjectiveContext() {
853 @Override
854 public void onError(Objective objective, ObjectiveError error) {
855 log.warn("Failed to install packet request for ARP to {}: {}",
856 deviceId, error);
857 }
858 });
859 srManager.flowObjectiveService.forward(deviceId, puntFwd);
860
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800861 // We punt all NDP packets towards the controller.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800862 puntFwd = puntNdpFwdObjective()
863 .add(new ObjectiveContext() {
864 @Override
865 public void onError(Objective objective, ObjectiveError error) {
866 log.warn("Failed to install packet request for NDP to {}: {}",
867 deviceId, error);
868 }
869 });
870 srManager.flowObjectiveService.forward(deviceId, puntFwd);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800871 }
872
873 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) {
874
875 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
876 tBuilder.punt();
877
878 return DefaultForwardingObjective.builder()
879 .withPriority(PacketPriority.CONTROL.priorityValue())
880 .withSelector(selector)
881 .fromApp(srManager.appId)
882 .withFlag(ForwardingObjective.Flag.VERSATILE)
883 .withTreatment(tBuilder.build())
884 .makePermanent();
885 }
886
887 private ForwardingObjective.Builder puntArpFwdObjective() {
888
889 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
890 sBuilder.matchEthType(TYPE_ARP);
891
892 return fwdObjBuilder(sBuilder.build());
893 }
894
895 private ForwardingObjective.Builder puntNdpFwdObjective() {
896
897 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
898 sBuilder.matchEthType(TYPE_IPV6)
899 .matchIPProtocol(PROTOCOL_ICMP6)
900 .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
901 .build();
902
903 return fwdObjBuilder(sBuilder.build());
904 }
905
906 /**
Charles Chan68aa62d2015-11-09 16:37:23 -0800907 * Populates a forwarding objective to send packets that miss other high
908 * priority Bridging Table entries to a group that contains all ports of
909 * its subnet.
910 *
Charles Chan68aa62d2015-11-09 16:37:23 -0800911 * @param deviceId switch ID to set the rules
912 */
913 public void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800914 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan59cc16d2017-02-02 16:20:42 -0800915 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
916
917 if (nextId < 0) {
918 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
919 + "to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -0800920 return;
921 }
922
Pier Ventre917127a2016-10-31 16:49:19 -0700923 // Driver should treat objective with MacAddress.NONE as the
924 // subnet broadcast rule
Charles Chan68aa62d2015-11-09 16:37:23 -0800925 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
926 sbuilder.matchVlanId(vlanId);
927 sbuilder.matchEthDst(MacAddress.NONE);
928
929 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
930 fob.withFlag(Flag.SPECIFIC)
931 .withSelector(sbuilder.build())
932 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800933 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800934 .fromApp(srManager.appId)
935 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700936 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan59cc16d2017-02-02 16:20:42 -0800937 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
Charles Chand2990362016-04-18 13:44:03 -0700938 (objective, error) ->
Charles Chan59cc16d2017-02-02 16:20:42 -0800939 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
Charles Chand2990362016-04-18 13:44:03 -0700940 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800941 });
942 }
943
Charles Chan5270ed02016-01-30 23:22:37 -0800944 private int getPriorityFromPrefix(IpPrefix prefix) {
945 return (prefix.isIp4()) ?
946 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
947 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700948 }
sanghob35a6192015-04-01 13:05:26 -0700949}