blob: 6b5aba75264eddfed4a7f922c3ce15f354b20e77 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070037import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sanghob35a6192015-04-01 13:05:26 -070038import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070039import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070040import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070041import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070043import org.onosproject.net.flow.TrafficSelector;
44import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-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 Das822c4e22015-10-23 10:51:11 -070051import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sanghob35a6192015-04-01 13:05:26 -070052import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
55import java.util.ArrayList;
Pier Ventre917127a2016-10-31 16:49:19 -070056import java.util.Collection;
57import java.util.Collections;
Saurav Das837e0bb2015-10-30 17:45:38 -070058import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070059import java.util.List;
Charles Chan2df0e8a2017-01-09 11:45:08 -080060import java.util.Optional;
sanghob35a6192015-04-01 13:05:26 -070061import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070062import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070063
64import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigi9e5c5ca2017-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 Chan59cc16d2017-02-02 16:20:42 -080069import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sanghob35a6192015-04-01 13:05:26 -070070
Charles Chane849c192016-01-11 18:28:54 -080071/**
72 * Populator of segment routing flow rules.
73 */
sanghob35a6192015-04-01 13:05:26 -070074public class RoutingRulePopulator {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070075 private static final Logger log = LoggerFactory
76 .getLogger(RoutingRulePopulator.class);
sanghob35a6192015-04-01 13:05:26 -070077
sangho20eff1d2015-04-13 15:15:58 -070078 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070079 private SegmentRoutingManager srManager;
80 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070081
sanghob35a6192015-04-01 13:05:26 -070082 /**
83 * Creates a RoutingRulePopulator object.
84 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070085 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070086 */
87 public RoutingRulePopulator(SegmentRoutingManager srManager) {
88 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070089 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-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 Vachuska266b4432015-04-30 18:13:25 -0700102 *
103 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -0700104 */
105 public long getCounter() {
106 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -0700107 }
108
109 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700110 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700111 * switch.
sanghob35a6192015-04-01 13:05:26 -0700112 *
Charles Chan1cdecff2016-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 Chan7ffd81f2017-02-08 15:52:08 -0800116 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700117 * @param outPort port where the next hop attaches to
sanghob35a6192015-04-01 13:05:26 -0700118 */
Charles Chan1cdecff2016-10-27 14:19:48 -0700119 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800120 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700121 log.debug("Populate IP table entry for route {} at {}:{}",
122 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800123 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800124 try {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800125 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, hostVlanId, outPort);
Charles Chan0b4e6182015-11-03 10:42:14 -0800126 } catch (DeviceConfigNotFoundException e) {
127 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
128 return;
129 }
Saurav Das59232cf2016-04-27 18:35:50 -0700130 if (fwdBuilder == null) {
131 log.warn("Aborting host routing table entries due "
Charles Chan1cdecff2016-10-27 14:19:48 -0700132 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das59232cf2016-04-27 18:35:50 -0700133 return;
134 }
Charles Chand2990362016-04-18 13:44:03 -0700135 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700136 (objective) -> log.debug("IP rule for route {} populated", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700137 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700138 log.warn("Failed to populate IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700139 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800140 rulePopulationCounter.incrementAndGet();
141 }
142
Charles Chane849c192016-01-11 18:28:54 -0800143 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700144 * Removes IP rules for a route when the next hop is gone.
Charles Chane849c192016-01-11 18:28:54 -0800145 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700146 * @param deviceId device ID of the device that next hop attaches to
147 * @param prefix IP prefix of the route
148 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800149 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700150 * @param outPort port that next hop attaches to
Charles Chane849c192016-01-11 18:28:54 -0800151 */
Charles Chan1cdecff2016-10-27 14:19:48 -0700152 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800153 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700154 log.debug("Revoke IP table entry for route {} at {}:{}",
155 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800156 ForwardingObjective.Builder fwdBuilder;
157 try {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800158 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, hostVlanId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800159 } catch (DeviceConfigNotFoundException e) {
160 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
161 return;
162 }
Charles Chan458b8262016-11-30 11:55:05 -0800163 if (fwdBuilder == null) {
164 log.warn("Aborting host routing table entries due "
165 + "to error for dev:{} route:{}", deviceId, prefix);
166 return;
167 }
Charles Chand2990362016-04-18 13:44:03 -0700168 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700169 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700170 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700171 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700172 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800173 }
174
Charles Chan1cdecff2016-10-27 14:19:48 -0700175 /**
Charles Chanf6ec1532017-02-08 16:10:40 -0800176 * Returns a forwarding objective builder for routing rules.
177 * <p>
178 * The forwarding objective routes packets destined to a given prefix to
179 * given port on given device with given destination MAC.
Charles Chan1cdecff2016-10-27 14:19:48 -0700180 *
181 * @param deviceId device ID
182 * @param prefix prefix that need to be routed
183 * @param hostMac MAC address of the nexthop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800184 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700185 * @param outPort port where the nexthop attaches to
186 * @return forwarding objective builder
187 * @throws DeviceConfigNotFoundException if given device is not configured
188 */
Charles Chanf6ec1532017-02-08 16:10:40 -0800189 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700190 DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800191 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort)
Charles Chan68aa62d2015-11-09 16:37:23 -0800192 throws DeviceConfigNotFoundException {
193 MacAddress deviceMac;
194 deviceMac = config.getDeviceMac(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800195
Charles Chan7ffd81f2017-02-08 15:52:08 -0800196 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
197 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
198 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
199 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sanghob35a6192015-04-01 13:05:26 -0700200
Charles Chan7ffd81f2017-02-08 15:52:08 -0800201 // Create route selector
202 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
203
204 // Create route treatment
Charles Chan1cdecff2016-10-27 14:19:48 -0700205 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho1e575652015-05-14 00:39:53 -0700206 tbuilder.deferred()
207 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800208 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700209 .setOutput(outPort);
Saurav Das4ce45962015-11-24 23:21:05 -0800210
Charles Chan7ffd81f2017-02-08 15:52:08 -0800211 // Create route meta
212 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan59cc16d2017-02-02 16:20:42 -0800213
Charles Chan7ffd81f2017-02-08 15:52:08 -0800214 // Adjust the meta according to VLAN configuration
215 if (taggedVlans.contains(hostVlanId)) {
216 tbuilder.setVlanId(hostVlanId);
217 } else if (hostVlanId.equals(VlanId.NONE)) {
218 if (untaggedVlan != null) {
219 mbuilder.matchVlanId(untaggedVlan);
220 } else if (nativeVlan != null) {
221 mbuilder.matchVlanId(nativeVlan);
222 } else {
223 // TODO: This check is turned off for now since vRouter still assumes that
224 // hosts are internally tagged with INTERNAL_VLAN.
225 // We should turn this back on when we move forward to the bridging CPR approach.
226 //
227 //log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
228 // hostMac, hostVlanId, connectPoint);
229 //return null;
230 mbuilder.matchVlanId(INTERNAL_VLAN);
231 }
232 } else {
233 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed in tagged vlan",
234 hostMac, hostVlanId, connectPoint);
Saurav Das59232cf2016-04-27 18:35:50 -0700235 return null;
236 }
Charles Chan7ffd81f2017-02-08 15:52:08 -0800237
238 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
239 tbuilder.build(), mbuilder.build());
240 if (portNextObjId == -1) {
241 // Warning log will come from getPortNextObjective method
242 return null;
243 }
244
Charles Chan68aa62d2015-11-09 16:37:23 -0800245 return DefaultForwardingObjective.builder()
Charles Chan7ffd81f2017-02-08 15:52:08 -0800246 .withSelector(sbuilder.build())
Saurav Das4ce45962015-11-24 23:21:05 -0800247 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800248 .fromApp(srManager.appId).makePermanent()
Charles Chan1cdecff2016-10-27 14:19:48 -0700249 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan5270ed02016-01-30 23:22:37 -0800250 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700251 }
252
253 /**
254 * Populates IP flow rules for the subnets of the destination router.
255 *
256 * @param deviceId switch ID to set the rules
Charles Chan93e71ba2016-04-29 14:38:22 -0700257 * @param subnets subnet being added
sanghob35a6192015-04-01 13:05:26 -0700258 * @param destSw destination switch ID
259 * @param nextHops next hop switch ID list
260 * @return true if all rules are set successfully, false otherwise
261 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800262 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<IpPrefix> subnets,
Charles Chan93e71ba2016-04-29 14:38:22 -0700263 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700264 for (IpPrefix subnet : subnets) {
sanghob35a6192015-04-01 13:05:26 -0700265 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
266 return false;
267 }
268 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700269 return true;
270 }
sanghob35a6192015-04-01 13:05:26 -0700271
Charles Chan93e71ba2016-04-29 14:38:22 -0700272 /**
273 * Revokes IP flow rules for the subnets.
274 *
275 * @param subnets subnet being removed
276 * @return true if all rules are removed successfully, false otherwise
277 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800278 public boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700279 for (IpPrefix subnet : subnets) {
280 if (!revokeIpRuleForRouter(subnet)) {
281 return false;
282 }
283 }
sanghob35a6192015-04-01 13:05:26 -0700284 return true;
285 }
286
287 /**
Saurav Das25190812016-05-27 13:54:07 -0700288 * Populates IP flow rules for an IP prefix in the target device. The prefix
289 * is reachable via destination device.
sanghob35a6192015-04-01 13:05:26 -0700290 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700291 * @param deviceId target device ID to set the rules
Pier Ventree0ae7a32016-11-23 09:57:42 -0800292 * @param ipPrefix the destination IP prefix
sanghob35a6192015-04-01 13:05:26 -0700293 * @param destSw device ID of the destination router
294 * @param nextHops next hop switch ID list
295 * @return true if all rules are set successfully, false otherwise
296 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700297 public boolean populateIpRuleForRouter(DeviceId deviceId,
298 IpPrefix ipPrefix, DeviceId destSw,
299 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800300 int segmentId;
301 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800302 if (ipPrefix.isIp4()) {
303 segmentId = config.getIPv4SegmentId(destSw);
304 } else {
305 segmentId = config.getIPv6SegmentId(destSw);
306 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800307 } catch (DeviceConfigNotFoundException e) {
308 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
309 return false;
310 }
sanghob35a6192015-04-01 13:05:26 -0700311
Pier Ventree0ae7a32016-11-23 09:57:42 -0800312 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan68aa62d2015-11-09 16:37:23 -0800313 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700314
Charles Chan68aa62d2015-11-09 16:37:23 -0800315 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
316 NeighborSet ns;
317 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700318
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700319 // If the next hop is the same as the final destination, then MPLS label
320 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700321 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800322 tbuilder.immediate().decNwTtl();
Pier Ventre917127a2016-10-31 16:49:19 -0700323 ns = new NeighborSet(nextHops, false);
Charles Chan68aa62d2015-11-09 16:37:23 -0800324 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700325 } else {
Pier Ventre917127a2016-10-31 16:49:19 -0700326 ns = new NeighborSet(nextHops, false, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800327 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700328 }
329
Saurav Das8a0732e2015-11-20 15:27:53 -0800330 // setup metadata to pass to nextObjective - indicate the vlan on egress
331 // if needed by the switch pipeline. Since neighbor sets are always to
332 // other neighboring routers, there is no subnet assigned on those ports.
333 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800334 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das8a0732e2015-11-20 15:27:53 -0800335
336 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
337 if (nextId <= 0) {
sangho834e4b02015-05-01 09:38:25 -0700338 log.warn("No next objective in {} for ns: {}", deviceId, ns);
339 return false;
340 }
341
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700342 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
343 .builder()
344 .fromApp(srManager.appId)
345 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800346 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700347 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800348 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700349 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800350 if (treatment != null) {
351 fwdBuilder.withTreatment(treatment);
352 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700353 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700354 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700355 ipPrefix,
356 deviceId);
Charles Chand2990362016-04-18 13:44:03 -0700357 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700358 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
359 ipPrefix, deviceId),
Charles Chand2990362016-04-18 13:44:03 -0700360 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700361 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
362 ipPrefix, error, deviceId));
Charles Chand2990362016-04-18 13:44:03 -0700363 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700364 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700365
366 return true;
367 }
368
sanghob35a6192015-04-01 13:05:26 -0700369 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700370 * Revokes IP flow rules for the router IP address.
371 *
372 * @param ipPrefix the IP address of the destination router
373 * @return true if all rules are removed successfully, false otherwise
374 */
375 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre968da122016-12-09 17:26:04 -0800376 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700377 TrafficSelector selector = sbuilder.build();
378 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
379
380 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
381 .builder()
382 .fromApp(srManager.appId)
383 .makePermanent()
384 .withSelector(selector)
385 .withTreatment(dummyTreatment)
386 .withPriority(getPriorityFromPrefix(ipPrefix))
387 .withFlag(ForwardingObjective.Flag.SPECIFIC);
388
389 ObjectiveContext context = new DefaultObjectiveContext(
390 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
391 (objective, error) ->
392 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
393
394 srManager.deviceService.getAvailableDevices().forEach(device -> {
395 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
396 });
397
398 return true;
399 }
400
401 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700402 * Deals with !MPLS Bos use case.
403 *
404 * @param targetSwId the target sw
405 * @param destSwId the destination sw
406 * @param nextHops the set of next hops
407 * @param segmentId the segmentId to match
408 * @param routerIp the router ip
409 * @return a collection of fwdobjective
410 */
411 private Collection<ForwardingObjective> handleMpls(DeviceId targetSwId,
412 DeviceId destSwId,
413 Set<DeviceId> nextHops,
414 int segmentId,
415 IpAddress routerIp,
416 boolean isMplsBos) {
417
418 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
419 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800420 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre917127a2016-10-31 16:49:19 -0700421 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
422 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
423 sbuilder.matchMplsBos(isMplsBos);
424 TrafficSelector selector = sbuilder.build();
425
426 // setup metadata to pass to nextObjective - indicate the vlan on egress
427 // if needed by the switch pipeline. Since mpls next-hops are always to
428 // other neighboring routers, there is no subnet assigned on those ports.
429 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800430 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre917127a2016-10-31 16:49:19 -0700431
432 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
433 // If the next hop is the destination router for the segment, do pop
434 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
435 + "label {} in switch {} with pop", segmentId, targetSwId);
436 // Not-bos pop case (php for the current label). If MPLS-ECMP
437 // has been configured, the application we will request the
438 // installation for an MPLS-ECMP group.
439 ForwardingObjective.Builder fwdObjNoBosBuilder = getMplsForwardingObjective(targetSwId,
440 nextHops,
441 true,
442 isMplsBos,
443 metabuilder.build(),
444 routerIp);
445 // Error case, we cannot handle, exit.
446 if (fwdObjNoBosBuilder == null) {
447 return Collections.emptyList();
448 }
449 fwdObjBuilders.add(fwdObjNoBosBuilder);
450
451 } else {
452 // next hop is not destination, SR CONTINUE case (swap with self)
453 log.debug("Installing MPLS forwarding objective for "
454 + "label {} in switch {} without pop", segmentId, targetSwId);
455 // Not-bos pop case. If MPLS-ECMP has been configured, the
456 // application we will request the installation for an MPLS-ECMP
457 // group.
458 ForwardingObjective.Builder fwdObjNoBosBuilder = getMplsForwardingObjective(targetSwId,
459 nextHops,
460 false,
461 isMplsBos,
462 metabuilder.build(),
463 routerIp);
464 // Error case, we cannot handle, exit.
465 if (fwdObjNoBosBuilder == null) {
466 return Collections.emptyList();
467 }
468 fwdObjBuilders.add(fwdObjNoBosBuilder);
469
470 }
471
472 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
473 // We add the final property to the fwdObjs.
474 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
475
476 ((Builder) ((Builder) fwdObjBuilder
477 .fromApp(srManager.appId)
478 .makePermanent())
479 .withSelector(selector)
480 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
481 .withFlag(ForwardingObjective.Flag.SPECIFIC);
482
483 ObjectiveContext context = new DefaultObjectiveContext(
484 (objective) ->
485 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
486 objective.id(), segmentId, targetSwId),
487 (objective, error) ->
488 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
489 objective.id(), segmentId, error, targetSwId));
490
491 ForwardingObjective fob = fwdObjBuilder.add(context);
492 fwdObjs.add(fob);
493
494 }
495
496 return fwdObjs;
497 }
498
499 /**
Saurav Das25190812016-05-27 13:54:07 -0700500 * Populates MPLS flow rules in the target device to point towards the
501 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700502 *
Saurav Das25190812016-05-27 13:54:07 -0700503 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700504 * @param destSwId destination switch device ID
505 * @param nextHops next hops switch ID list
Pier Ventre917127a2016-10-31 16:49:19 -0700506 * @param routerIp the router ip
sanghob35a6192015-04-01 13:05:26 -0700507 * @return true if all rules are set successfully, false otherwise
508 */
Saurav Das25190812016-05-27 13:54:07 -0700509 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800510 Set<DeviceId> nextHops, IpAddress routerIp) {
Pier Ventre917127a2016-10-31 16:49:19 -0700511
Charles Chan0b4e6182015-11-03 10:42:14 -0800512 int segmentId;
513 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800514 if (routerIp.isIp4()) {
515 segmentId = config.getIPv4SegmentId(destSwId);
516 } else {
517 segmentId = config.getIPv6SegmentId(destSwId);
518 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800519 } catch (DeviceConfigNotFoundException e) {
520 log.warn(e.getMessage() + " Aborting populateMplsRule.");
521 return false;
522 }
sanghob35a6192015-04-01 13:05:26 -0700523
Pier Ventre917127a2016-10-31 16:49:19 -0700524 List<ForwardingObjective> fwdObjs = new ArrayList<>();
525 Collection<ForwardingObjective> fwdObjsMpls = Collections.emptyList();
526 // Generates the transit rules used by the standard "routing".
527 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
528 if (fwdObjsMpls.isEmpty()) {
529 return false;
sanghob35a6192015-04-01 13:05:26 -0700530 }
Pier Ventre917127a2016-10-31 16:49:19 -0700531 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800532 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre917127a2016-10-31 16:49:19 -0700533 // the only case !BoS supported.
Saurav Das018605f2017-02-18 14:05:44 -0800534 /*fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre917127a2016-10-31 16:49:19 -0700535 if (fwdObjsMpls.isEmpty()) {
536 return false;
537 }
Saurav Das018605f2017-02-18 14:05:44 -0800538 fwdObjs.addAll(fwdObjsMpls);*/
Pier Ventre917127a2016-10-31 16:49:19 -0700539
540 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Das25190812016-05-27 13:54:07 -0700541 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre917127a2016-10-31 16:49:19 -0700542 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
543 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sangho20eff1d2015-04-13 15:15:58 -0700544 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700545 }
546
547 return true;
548 }
549
Saurav Das8a0732e2015-11-20 15:27:53 -0800550 private ForwardingObjective.Builder getMplsForwardingObjective(
551 DeviceId deviceId,
552 Set<DeviceId> nextHops,
553 boolean phpRequired,
554 boolean isBos,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800555 TrafficSelector meta,
556 IpAddress routerIp) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800557
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700558 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
559 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700560
561 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
562
563 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800564 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700565 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700566 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700567 if (isBos) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800568 if (routerIp.isIp4()) {
569 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
570 } else {
571 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
572 }
573 tbuilder.decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700574 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800575 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
576 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700577 }
578 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800579 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700580 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700581 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700582 }
583
Saurav Das8a0732e2015-11-20 15:27:53 -0800584 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre917127a2016-10-31 16:49:19 -0700585 // if MPLS-ECMP == True we will build a standard NeighborSet.
586 // Otherwise a RandomNeighborSet.
587 NeighborSet ns = NeighborSet.neighborSet(false, nextHops, false);
588 if (!isBos && this.srManager.getMplsEcmp()) {
589 ns = NeighborSet.neighborSet(false, nextHops, true);
590 } else if (!isBos && !this.srManager.getMplsEcmp()) {
591 ns = NeighborSet.neighborSet(true, nextHops, true);
592 }
593 log.info("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
594 deviceId, ns);
595 // If BoS == True, all forwarding is via L3 ECMP group.
596 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
597 // MPLS-Interface group. This depends on the configuration of the option
598 // MPLS-ECMP.
599 // The metadata informs the driver that the next-Objective will be used
600 // by MPLS flows and if Bos == False the driver will use MPLS groups.
601 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta, isBos);
Saurav Das8a0732e2015-11-20 15:27:53 -0800602 if (nextId <= 0) {
603 log.warn("No next objective in {} for ns: {}", deviceId, ns);
604 return null;
Saurav Das25190812016-05-27 13:54:07 -0700605 } else {
606 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
607 nextId, deviceId, ns);
sanghob35a6192015-04-01 13:05:26 -0700608 }
609
Saurav Das8a0732e2015-11-20 15:27:53 -0800610 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700611 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700612 }
613
614 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700615 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700616 * dstMac corresponding to the router's MAC address. For those pipelines
617 * that need to internally assign vlans to untagged packets, this method
618 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700619 * <p>
Saurav Das018605f2017-02-18 14:05:44 -0800620 * Note that the vlan assignment and filter programming should only be done by
621 * the master for a switch. This method is typically called at deviceAdd and
622 * programs filters only for the enabled ports of the device. For port-updates,
623 * that enable/disable ports after device add, singlePortFilter methods should
624 * be called.
sanghob35a6192015-04-01 13:05:26 -0700625 *
Saurav Das822c4e22015-10-23 10:51:11 -0700626 * @param deviceId the switch dpid for the router
Saurav Dasd2fded02016-12-02 15:43:47 -0800627 * @return PortFilterInfo information about the processed ports
sanghob35a6192015-04-01 13:05:26 -0700628 */
Charles Chanf6ec1532017-02-08 16:10:40 -0800629 public PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700630 log.debug("Installing per-port filtering objective for untagged "
631 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800632
Saurav Das59232cf2016-04-27 18:35:50 -0700633 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hallcbd1b392017-01-18 20:15:44 -0800634 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das59232cf2016-04-27 18:35:50 -0700635 log.warn("Device {} ports not available. Unable to add MacVlan filters",
636 deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -0800637 return null;
Saurav Das59232cf2016-04-27 18:35:50 -0700638 }
Saurav Das018605f2017-02-18 14:05:44 -0800639 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700640 for (Port port : devPorts) {
Saurav Das25190812016-05-27 13:54:07 -0700641 if (!port.isEnabled()) {
642 disabledPorts++;
643 continue;
644 }
Charles Chan7e4f8192017-02-26 22:59:35 -0800645 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Das018605f2017-02-18 14:05:44 -0800646 filteredPorts++;
647 } else {
648 errorPorts++;
Saurav Das25190812016-05-27 13:54:07 -0700649 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700650 }
Saurav Das018605f2017-02-18 14:05:44 -0800651 log.info("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
652 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd2fded02016-12-02 15:43:47 -0800653 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Das018605f2017-02-18 14:05:44 -0800654 errorPorts, filteredPorts);
655 }
656
657 /**
Charles Chan7e4f8192017-02-26 22:59:35 -0800658 * Creates or removes filtering objectives for a single port. Should only be
659 * called by the master for a switch.
Saurav Das018605f2017-02-18 14:05:44 -0800660 *
661 * @param deviceId device identifier
662 * @param portnum port identifier for port to be filtered
Charles Chan7e4f8192017-02-26 22:59:35 -0800663 * @param install true to install the filtering objective, false to remove
Saurav Das018605f2017-02-18 14:05:44 -0800664 * @return true if no errors occurred during the build of the filtering objective
665 */
Charles Chan7e4f8192017-02-26 22:59:35 -0800666 public boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800667 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
668 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
669 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
670 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
671
672 if (taggedVlans.size() != 0) {
673 // Filter for tagged vlans
674 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan7e4f8192017-02-26 22:59:35 -0800675 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800676 return false;
677 }
678 if (nativeVlan != null) {
679 // Filter for native vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800680 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800681 return false;
682 }
683 }
684 } else if (untaggedVlan != null) {
685 // Filter for untagged vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800686 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800687 return false;
688 }
689 } else {
690 // Unconfigure port, use INTERNAL_VLAN
Charles Chan7e4f8192017-02-26 22:59:35 -0800691 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800692 return false;
693 }
694 }
695 return true;
696 }
697
Charles Chan7e4f8192017-02-26 22:59:35 -0800698 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
699 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800700 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Das018605f2017-02-18 14:05:44 -0800701 if (fob == null) {
702 // error encountered during build
703 return false;
704 }
Charles Chan7e4f8192017-02-26 22:59:35 -0800705 log.info("{} filtering objectives for dev/port:{}/{}", (install ? "Installing" : "Removing"),
706 deviceId, portnum);
Saurav Das018605f2017-02-18 14:05:44 -0800707 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan7e4f8192017-02-26 22:59:35 -0800708 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
709 (install ? "installed" : "removed")),
710 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
711 (install ? "install" : "remove"), deviceId, portnum, error));
712 if (install) {
713 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
714 } else {
715 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan7ffd81f2017-02-08 15:52:08 -0800716 }
Charles Chan7ffd81f2017-02-08 15:52:08 -0800717 return true;
Saurav Das018605f2017-02-18 14:05:44 -0800718 }
719
Charles Chan7ffd81f2017-02-08 15:52:08 -0800720 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
721 boolean pushVlan, VlanId vlanId) {
Saurav Das018605f2017-02-18 14:05:44 -0800722 MacAddress deviceMac;
723 try {
724 deviceMac = config.getDeviceMac(deviceId);
725 } catch (DeviceConfigNotFoundException e) {
726 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
727 return null;
728 }
729 // suppressed ports still have filtering rules pushed by SR using default vlan
730 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
Saurav Das018605f2017-02-18 14:05:44 -0800731
Saurav Das018605f2017-02-18 14:05:44 -0800732 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
733 fob.withKey(Criteria.matchInPort(portnum))
734 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Das018605f2017-02-18 14:05:44 -0800735 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800736
737 if (pushVlan) {
738 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
739 TrafficTreatment tt = DefaultTrafficTreatment.builder()
740 .pushVlan().setVlanId(vlanId).build();
741 fob.withMeta(tt);
742 } else {
743 fob.addCondition(Criteria.matchVlanId(vlanId));
744 }
745
Saurav Das018605f2017-02-18 14:05:44 -0800746 fob.permit().fromApp(srManager.appId);
747 return fob;
sanghob35a6192015-04-01 13:05:26 -0700748 }
749
750 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700751 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700752 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700753 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700754 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700755 *
Saurav Das822c4e22015-10-23 10:51:11 -0700756 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700757 */
Charles Chanf6ec1532017-02-08 16:10:40 -0800758 public void populateIpPunts(DeviceId deviceId) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800759 Ip4Address routerIpv4;
760 Ip6Address routerIpv6;
Charles Chan0b4e6182015-11-03 10:42:14 -0800761 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800762 routerIpv4 = config.getRouterIpv4(deviceId);
763 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800764 } catch (DeviceConfigNotFoundException e) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800765 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800766 return;
767 }
768
Saurav Das837e0bb2015-10-30 17:45:38 -0700769 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
770 log.debug("Not installing port-IP punts - not the master for dev:{} ",
771 deviceId);
772 return;
773 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800774 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
775 allIps.add(routerIpv4);
776 if (routerIpv6 != null) {
777 allIps.add(routerIpv6);
778 }
779 for (IpAddress ipaddr : allIps) {
780 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr);
Charles Chan2df0e8a2017-01-09 11:45:08 -0800781 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
782
783 srManager.packetService.requestPackets(sbuilder.build(),
784 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700785 }
sanghob35a6192015-04-01 13:05:26 -0700786 }
787
Charles Chan68aa62d2015-11-09 16:37:23 -0800788 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800789 * Method to build IPv4 or IPv6 selector.
790 *
791 * @param addressToMatch the address to match
792 */
793 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
794 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
795 }
796
797 /**
798 * Method to build IPv4 or IPv6 selector.
799 *
800 * @param prefixToMatch the prefix to match
801 */
802 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
803 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre917127a2016-10-31 16:49:19 -0700804 // If the prefix is IPv4
Pier Ventree0ae7a32016-11-23 09:57:42 -0800805 if (prefixToMatch.isIp4()) {
806 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
807 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
808 return selectorBuilder;
809 }
Pier Ventre917127a2016-10-31 16:49:19 -0700810 // If the prefix is IPv6
Pier Ventree0ae7a32016-11-23 09:57:42 -0800811 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
812 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
813 return selectorBuilder;
814 }
815
816 /**
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800817 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
818 * Furthermore, these are applied only by the master instance. Deferred actions
819 * are not cleared such that packets can be flooded in the cross connect use case
820 *
821 * @param deviceId the switch dpid for the router
822 */
823 public void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre917127a2016-10-31 16:49:19 -0700824 // We are not the master just skip.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800825 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
826 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
827 deviceId);
828 return;
829 }
830
831 // We punt all ARP packets towards the controller.
832 ForwardingObjective puntFwd = puntArpFwdObjective()
833 .add(new ObjectiveContext() {
834 @Override
835 public void onError(Objective objective, ObjectiveError error) {
836 log.warn("Failed to install packet request for ARP to {}: {}",
837 deviceId, error);
838 }
839 });
840 srManager.flowObjectiveService.forward(deviceId, puntFwd);
841
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800842 // We punt all NDP packets towards the controller.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800843 puntFwd = puntNdpFwdObjective()
844 .add(new ObjectiveContext() {
845 @Override
846 public void onError(Objective objective, ObjectiveError error) {
847 log.warn("Failed to install packet request for NDP to {}: {}",
848 deviceId, error);
849 }
850 });
851 srManager.flowObjectiveService.forward(deviceId, puntFwd);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800852 }
853
854 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) {
855
856 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
857 tBuilder.punt();
858
859 return DefaultForwardingObjective.builder()
860 .withPriority(PacketPriority.CONTROL.priorityValue())
861 .withSelector(selector)
862 .fromApp(srManager.appId)
863 .withFlag(ForwardingObjective.Flag.VERSATILE)
864 .withTreatment(tBuilder.build())
865 .makePermanent();
866 }
867
868 private ForwardingObjective.Builder puntArpFwdObjective() {
869
870 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
871 sBuilder.matchEthType(TYPE_ARP);
872
873 return fwdObjBuilder(sBuilder.build());
874 }
875
876 private ForwardingObjective.Builder puntNdpFwdObjective() {
877
878 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
879 sBuilder.matchEthType(TYPE_IPV6)
880 .matchIPProtocol(PROTOCOL_ICMP6)
881 .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
882 .build();
883
884 return fwdObjBuilder(sBuilder.build());
885 }
886
887 /**
Charles Chan68aa62d2015-11-09 16:37:23 -0800888 * Populates a forwarding objective to send packets that miss other high
889 * priority Bridging Table entries to a group that contains all ports of
890 * its subnet.
891 *
892 * Note: We assume that packets sending from the edge switches to the hosts
893 * have untagged VLAN.
894 * The VLAN tag will be popped later in the flooding group.
895 *
896 * @param deviceId switch ID to set the rules
897 */
898 public void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800899 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan59cc16d2017-02-02 16:20:42 -0800900 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
901
902 if (nextId < 0) {
903 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
904 + "to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -0800905 return;
906 }
907
Pier Ventre917127a2016-10-31 16:49:19 -0700908 // Driver should treat objective with MacAddress.NONE as the
909 // subnet broadcast rule
Charles Chan68aa62d2015-11-09 16:37:23 -0800910 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
911 sbuilder.matchVlanId(vlanId);
912 sbuilder.matchEthDst(MacAddress.NONE);
913
914 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
915 fob.withFlag(Flag.SPECIFIC)
916 .withSelector(sbuilder.build())
917 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800918 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800919 .fromApp(srManager.appId)
920 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700921 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan59cc16d2017-02-02 16:20:42 -0800922 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
Charles Chand2990362016-04-18 13:44:03 -0700923 (objective, error) ->
Charles Chan59cc16d2017-02-02 16:20:42 -0800924 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
Charles Chand2990362016-04-18 13:44:03 -0700925 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800926 });
927 }
928
Charles Chan5270ed02016-01-30 23:22:37 -0800929 private int getPriorityFromPrefix(IpPrefix prefix) {
930 return (prefix.isIp4()) ?
931 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
932 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700933 }
sanghob35a6192015-04-01 13:05:26 -0700934}