blob: 14ce83d1edafd93f8acd443af59eed02e14b4c5b [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;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070022import org.onlab.packet.Ip4Prefix;
Pier Ventree0ae7a32016-11-23 09:57:42 -080023import org.onlab.packet.Ip6Address;
24import org.onlab.packet.IpAddress;
sanghob35a6192015-04-01 13:05:26 -070025import org.onlab.packet.IpPrefix;
26import org.onlab.packet.MacAddress;
sanghob35a6192015-04-01 13:05:26 -070027import org.onlab.packet.MplsLabel;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070028import org.onlab.packet.VlanId;
Charles Chane849c192016-01-11 18:28:54 -080029import org.onosproject.net.ConnectPoint;
Charles Chand2990362016-04-18 13:44:03 -070030import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080031import org.onosproject.net.flowobjective.Objective;
Charles Chand2990362016-04-18 13:44:03 -070032import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080033import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2df0e8a2017-01-09 11:45:08 -080034import org.onosproject.net.packet.PacketPriority;
Saurav Dasd2fded02016-12-02 15:43:47 -080035import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan0b4e6182015-11-03 10:42:14 -080036import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
37import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chan6ea94fc2016-05-10 17:29:47 -070038import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070039import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sanghob35a6192015-04-01 13:05:26 -070040import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070041import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070042import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070043import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070045import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070047import org.onosproject.net.flow.criteria.Criteria;
48import org.onosproject.net.flowobjective.DefaultFilteringObjective;
49import org.onosproject.net.flowobjective.DefaultForwardingObjective;
50import org.onosproject.net.flowobjective.FilteringObjective;
51import org.onosproject.net.flowobjective.ForwardingObjective;
52import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das822c4e22015-10-23 10:51:11 -070053import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sanghob35a6192015-04-01 13:05:26 -070054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56
57import java.util.ArrayList;
Pier Ventre917127a2016-10-31 16:49:19 -070058import java.util.Collection;
59import java.util.Collections;
Saurav Das837e0bb2015-10-30 17:45:38 -070060import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070061import java.util.List;
Charles Chan2df0e8a2017-01-09 11:45:08 -080062import java.util.Optional;
sanghob35a6192015-04-01 13:05:26 -070063import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070064import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070065
66import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080067import static org.onlab.packet.Ethernet.TYPE_ARP;
68import static org.onlab.packet.Ethernet.TYPE_IPV6;
69import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
70import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
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
117 * @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,
sanghob35a6192015-04-01 13:05:26 -0700120 MacAddress hostMac, 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 Chan68aa62d2015-11-09 16:37:23 -0800125 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700126 deviceId, prefix, hostMac, outPort);
Charles Chan0b4e6182015-11-03 10:42:14 -0800127 } catch (DeviceConfigNotFoundException e) {
128 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
129 return;
130 }
Saurav Das59232cf2016-04-27 18:35:50 -0700131 if (fwdBuilder == null) {
132 log.warn("Aborting host routing table entries due "
Charles Chan1cdecff2016-10-27 14:19:48 -0700133 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das59232cf2016-04-27 18:35:50 -0700134 return;
135 }
Charles Chand2990362016-04-18 13:44:03 -0700136 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700137 (objective) -> log.debug("IP rule for route {} populated", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700138 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700139 log.warn("Failed to populate IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700140 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800141 rulePopulationCounter.incrementAndGet();
142 }
143
Charles Chane849c192016-01-11 18:28:54 -0800144 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700145 * Removes IP rules for a route when the next hop is gone.
Charles Chane849c192016-01-11 18:28:54 -0800146 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700147 * @param deviceId device ID of the device that next hop attaches to
148 * @param prefix IP prefix of the route
149 * @param hostMac MAC address of the next hop
150 * @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 Chan68aa62d2015-11-09 16:37:23 -0800153 MacAddress hostMac, 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 {
158 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700159 deviceId, prefix, hostMac, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800160 } catch (DeviceConfigNotFoundException e) {
161 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
162 return;
163 }
Charles Chan458b8262016-11-30 11:55:05 -0800164 if (fwdBuilder == null) {
165 log.warn("Aborting host routing table entries due "
166 + "to error for dev:{} route:{}", deviceId, prefix);
167 return;
168 }
Charles Chand2990362016-04-18 13:44:03 -0700169 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700170 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700171 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700172 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700173 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800174 }
175
Charles Chan1cdecff2016-10-27 14:19:48 -0700176 /**
177 * Returns a forwarding objective that points packets destined to a
178 * given prefix to given port on given device with given destination MAC.
179 *
180 * @param deviceId device ID
181 * @param prefix prefix that need to be routed
182 * @param hostMac MAC address of the nexthop
183 * @param outPort port where the nexthop attaches to
184 * @return forwarding objective builder
185 * @throws DeviceConfigNotFoundException if given device is not configured
186 */
Charles Chan68aa62d2015-11-09 16:37:23 -0800187 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700188 DeviceId deviceId, IpPrefix prefix,
Charles Chan68aa62d2015-11-09 16:37:23 -0800189 MacAddress hostMac, PortNumber outPort)
190 throws DeviceConfigNotFoundException {
191 MacAddress deviceMac;
192 deviceMac = config.getDeviceMac(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800193
Pier Ventre968da122016-12-09 17:26:04 -0800194 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
Saurav Das4ce45962015-11-24 23:21:05 -0800195 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700196
Charles Chan1cdecff2016-10-27 14:19:48 -0700197 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho1e575652015-05-14 00:39:53 -0700198 tbuilder.deferred()
199 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800200 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700201 .setOutput(outPort);
sanghob35a6192015-04-01 13:05:26 -0700202 TrafficTreatment treatment = tbuilder.build();
Saurav Das4ce45962015-11-24 23:21:05 -0800203
204 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
205 // for switch pipelines that need it, provide outgoing vlan as metadata
Pier Ventre968da122016-12-09 17:26:04 -0800206 VlanId outvlan;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800207 Ip4Prefix subnet = srManager.deviceConfiguration.getPortIPv4Subnet(deviceId, outPort);
Saurav Das4ce45962015-11-24 23:21:05 -0800208 if (subnet == null) {
209 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
210 } else {
211 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
212 }
213 TrafficSelector meta = DefaultTrafficSelector.builder()
214 .matchVlanId(outvlan).build();
215 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
216 treatment, meta);
Saurav Das59232cf2016-04-27 18:35:50 -0700217 if (portNextObjId == -1) {
218 // warning log will come from getPortNextObjective method
219 return null;
220 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800221 return DefaultForwardingObjective.builder()
Saurav Das4ce45962015-11-24 23:21:05 -0800222 .withSelector(selector)
223 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800224 .fromApp(srManager.appId).makePermanent()
Charles Chan1cdecff2016-10-27 14:19:48 -0700225 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan5270ed02016-01-30 23:22:37 -0800226 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700227 }
228
229 /**
230 * Populates IP flow rules for the subnets of the destination router.
231 *
232 * @param deviceId switch ID to set the rules
Charles Chan93e71ba2016-04-29 14:38:22 -0700233 * @param subnets subnet being added
sanghob35a6192015-04-01 13:05:26 -0700234 * @param destSw destination switch ID
235 * @param nextHops next hop switch ID list
236 * @return true if all rules are set successfully, false otherwise
237 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800238 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<IpPrefix> subnets,
Charles Chan93e71ba2016-04-29 14:38:22 -0700239 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700240 for (IpPrefix subnet : subnets) {
sanghob35a6192015-04-01 13:05:26 -0700241 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
242 return false;
243 }
244 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700245 return true;
246 }
sanghob35a6192015-04-01 13:05:26 -0700247
Charles Chan93e71ba2016-04-29 14:38:22 -0700248 /**
249 * Revokes IP flow rules for the subnets.
250 *
251 * @param subnets subnet being removed
252 * @return true if all rules are removed successfully, false otherwise
253 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800254 public boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700255 for (IpPrefix subnet : subnets) {
256 if (!revokeIpRuleForRouter(subnet)) {
257 return false;
258 }
259 }
sanghob35a6192015-04-01 13:05:26 -0700260 return true;
261 }
262
263 /**
Saurav Das25190812016-05-27 13:54:07 -0700264 * Populates IP flow rules for an IP prefix in the target device. The prefix
265 * is reachable via destination device.
sanghob35a6192015-04-01 13:05:26 -0700266 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700267 * @param deviceId target device ID to set the rules
Pier Ventree0ae7a32016-11-23 09:57:42 -0800268 * @param ipPrefix the destination IP prefix
sanghob35a6192015-04-01 13:05:26 -0700269 * @param destSw device ID of the destination router
270 * @param nextHops next hop switch ID list
271 * @return true if all rules are set successfully, false otherwise
272 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700273 public boolean populateIpRuleForRouter(DeviceId deviceId,
274 IpPrefix ipPrefix, DeviceId destSw,
275 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800276 int segmentId;
277 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800278 if (ipPrefix.isIp4()) {
279 segmentId = config.getIPv4SegmentId(destSw);
280 } else {
281 segmentId = config.getIPv6SegmentId(destSw);
282 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800283 } catch (DeviceConfigNotFoundException e) {
284 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
285 return false;
286 }
sanghob35a6192015-04-01 13:05:26 -0700287
Pier Ventree0ae7a32016-11-23 09:57:42 -0800288 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan68aa62d2015-11-09 16:37:23 -0800289 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700290
Charles Chan68aa62d2015-11-09 16:37:23 -0800291 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
292 NeighborSet ns;
293 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700294
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700295 // If the next hop is the same as the final destination, then MPLS label
296 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700297 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800298 tbuilder.immediate().decNwTtl();
Pier Ventre917127a2016-10-31 16:49:19 -0700299 ns = new NeighborSet(nextHops, false);
Charles Chan68aa62d2015-11-09 16:37:23 -0800300 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700301 } else {
Pier Ventre917127a2016-10-31 16:49:19 -0700302 ns = new NeighborSet(nextHops, false, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800303 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700304 }
305
Saurav Das8a0732e2015-11-20 15:27:53 -0800306 // setup metadata to pass to nextObjective - indicate the vlan on egress
307 // if needed by the switch pipeline. Since neighbor sets are always to
308 // other neighboring routers, there is no subnet assigned on those ports.
309 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
310 metabuilder.matchVlanId(
311 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
312
313 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
314 if (nextId <= 0) {
sangho834e4b02015-05-01 09:38:25 -0700315 log.warn("No next objective in {} for ns: {}", deviceId, ns);
316 return false;
317 }
318
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700319 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
320 .builder()
321 .fromApp(srManager.appId)
322 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800323 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700324 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800325 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700326 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800327 if (treatment != null) {
328 fwdBuilder.withTreatment(treatment);
329 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700330 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700331 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700332 ipPrefix,
333 deviceId);
Charles Chand2990362016-04-18 13:44:03 -0700334 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700335 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
336 ipPrefix, deviceId),
Charles Chand2990362016-04-18 13:44:03 -0700337 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700338 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
339 ipPrefix, error, deviceId));
Charles Chand2990362016-04-18 13:44:03 -0700340 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700341 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700342
343 return true;
344 }
345
sanghob35a6192015-04-01 13:05:26 -0700346 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700347 * Revokes IP flow rules for the router IP address.
348 *
349 * @param ipPrefix the IP address of the destination router
350 * @return true if all rules are removed successfully, false otherwise
351 */
352 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre968da122016-12-09 17:26:04 -0800353 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700354 TrafficSelector selector = sbuilder.build();
355 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
356
357 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
358 .builder()
359 .fromApp(srManager.appId)
360 .makePermanent()
361 .withSelector(selector)
362 .withTreatment(dummyTreatment)
363 .withPriority(getPriorityFromPrefix(ipPrefix))
364 .withFlag(ForwardingObjective.Flag.SPECIFIC);
365
366 ObjectiveContext context = new DefaultObjectiveContext(
367 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
368 (objective, error) ->
369 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
370
371 srManager.deviceService.getAvailableDevices().forEach(device -> {
372 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
373 });
374
375 return true;
376 }
377
378 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700379 * Deals with !MPLS Bos use case.
380 *
381 * @param targetSwId the target sw
382 * @param destSwId the destination sw
383 * @param nextHops the set of next hops
384 * @param segmentId the segmentId to match
385 * @param routerIp the router ip
386 * @return a collection of fwdobjective
387 */
388 private Collection<ForwardingObjective> handleMpls(DeviceId targetSwId,
389 DeviceId destSwId,
390 Set<DeviceId> nextHops,
391 int segmentId,
392 IpAddress routerIp,
393 boolean isMplsBos) {
394
395 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
396 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
397 // For the transport of VPWS we can use two or three MPLS label
398 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
399 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
400 sbuilder.matchMplsBos(isMplsBos);
401 TrafficSelector selector = sbuilder.build();
402
403 // setup metadata to pass to nextObjective - indicate the vlan on egress
404 // if needed by the switch pipeline. Since mpls next-hops are always to
405 // other neighboring routers, there is no subnet assigned on those ports.
406 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
407 metabuilder.matchVlanId(
408 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
409
410 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
411 // If the next hop is the destination router for the segment, do pop
412 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
413 + "label {} in switch {} with pop", segmentId, targetSwId);
414 // Not-bos pop case (php for the current label). If MPLS-ECMP
415 // has been configured, the application we will request the
416 // installation for an MPLS-ECMP group.
417 ForwardingObjective.Builder fwdObjNoBosBuilder = getMplsForwardingObjective(targetSwId,
418 nextHops,
419 true,
420 isMplsBos,
421 metabuilder.build(),
422 routerIp);
423 // Error case, we cannot handle, exit.
424 if (fwdObjNoBosBuilder == null) {
425 return Collections.emptyList();
426 }
427 fwdObjBuilders.add(fwdObjNoBosBuilder);
428
429 } else {
430 // next hop is not destination, SR CONTINUE case (swap with self)
431 log.debug("Installing MPLS forwarding objective for "
432 + "label {} in switch {} without pop", segmentId, targetSwId);
433 // Not-bos pop case. If MPLS-ECMP has been configured, the
434 // application we will request the installation for an MPLS-ECMP
435 // group.
436 ForwardingObjective.Builder fwdObjNoBosBuilder = getMplsForwardingObjective(targetSwId,
437 nextHops,
438 false,
439 isMplsBos,
440 metabuilder.build(),
441 routerIp);
442 // Error case, we cannot handle, exit.
443 if (fwdObjNoBosBuilder == null) {
444 return Collections.emptyList();
445 }
446 fwdObjBuilders.add(fwdObjNoBosBuilder);
447
448 }
449
450 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
451 // We add the final property to the fwdObjs.
452 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
453
454 ((Builder) ((Builder) fwdObjBuilder
455 .fromApp(srManager.appId)
456 .makePermanent())
457 .withSelector(selector)
458 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
459 .withFlag(ForwardingObjective.Flag.SPECIFIC);
460
461 ObjectiveContext context = new DefaultObjectiveContext(
462 (objective) ->
463 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
464 objective.id(), segmentId, targetSwId),
465 (objective, error) ->
466 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
467 objective.id(), segmentId, error, targetSwId));
468
469 ForwardingObjective fob = fwdObjBuilder.add(context);
470 fwdObjs.add(fob);
471
472 }
473
474 return fwdObjs;
475 }
476
477 /**
Saurav Das25190812016-05-27 13:54:07 -0700478 * Populates MPLS flow rules in the target device to point towards the
479 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700480 *
Saurav Das25190812016-05-27 13:54:07 -0700481 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700482 * @param destSwId destination switch device ID
483 * @param nextHops next hops switch ID list
Pier Ventre917127a2016-10-31 16:49:19 -0700484 * @param routerIp the router ip
sanghob35a6192015-04-01 13:05:26 -0700485 * @return true if all rules are set successfully, false otherwise
486 */
Saurav Das25190812016-05-27 13:54:07 -0700487 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800488 Set<DeviceId> nextHops, IpAddress routerIp) {
Pier Ventre917127a2016-10-31 16:49:19 -0700489
Charles Chan0b4e6182015-11-03 10:42:14 -0800490 int segmentId;
491 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800492 if (routerIp.isIp4()) {
493 segmentId = config.getIPv4SegmentId(destSwId);
494 } else {
495 segmentId = config.getIPv6SegmentId(destSwId);
496 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800497 } catch (DeviceConfigNotFoundException e) {
498 log.warn(e.getMessage() + " Aborting populateMplsRule.");
499 return false;
500 }
sanghob35a6192015-04-01 13:05:26 -0700501
Pier Ventre917127a2016-10-31 16:49:19 -0700502 List<ForwardingObjective> fwdObjs = new ArrayList<>();
503 Collection<ForwardingObjective> fwdObjsMpls = Collections.emptyList();
504 // Generates the transit rules used by the standard "routing".
505 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
506 if (fwdObjsMpls.isEmpty()) {
507 return false;
sanghob35a6192015-04-01 13:05:26 -0700508 }
Pier Ventre917127a2016-10-31 16:49:19 -0700509 fwdObjs.addAll(fwdObjsMpls);
510 // Generates the transit rules used by the MPLS VPWS. For now it is
511 // the only case !BoS supported.
512 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
513 if (fwdObjsMpls.isEmpty()) {
514 return false;
515 }
516 fwdObjs.addAll(fwdObjsMpls);
517
518 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Das25190812016-05-27 13:54:07 -0700519 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre917127a2016-10-31 16:49:19 -0700520 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
521 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sangho20eff1d2015-04-13 15:15:58 -0700522 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700523 }
524
525 return true;
526 }
527
Saurav Das8a0732e2015-11-20 15:27:53 -0800528 private ForwardingObjective.Builder getMplsForwardingObjective(
529 DeviceId deviceId,
530 Set<DeviceId> nextHops,
531 boolean phpRequired,
532 boolean isBos,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800533 TrafficSelector meta,
534 IpAddress routerIp) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800535
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700536 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
537 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700538
539 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
540
541 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800542 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700543 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700544 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700545 if (isBos) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800546 if (routerIp.isIp4()) {
547 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
548 } else {
549 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
550 }
551 tbuilder.decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700552 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800553 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
554 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700555 }
556 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800557 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700558 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700559 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700560 }
561
Saurav Das8a0732e2015-11-20 15:27:53 -0800562 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre917127a2016-10-31 16:49:19 -0700563 // if MPLS-ECMP == True we will build a standard NeighborSet.
564 // Otherwise a RandomNeighborSet.
565 NeighborSet ns = NeighborSet.neighborSet(false, nextHops, false);
566 if (!isBos && this.srManager.getMplsEcmp()) {
567 ns = NeighborSet.neighborSet(false, nextHops, true);
568 } else if (!isBos && !this.srManager.getMplsEcmp()) {
569 ns = NeighborSet.neighborSet(true, nextHops, true);
570 }
571 log.info("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
572 deviceId, ns);
573 // If BoS == True, all forwarding is via L3 ECMP group.
574 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
575 // MPLS-Interface group. This depends on the configuration of the option
576 // MPLS-ECMP.
577 // The metadata informs the driver that the next-Objective will be used
578 // by MPLS flows and if Bos == False the driver will use MPLS groups.
579 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta, isBos);
Saurav Das8a0732e2015-11-20 15:27:53 -0800580 if (nextId <= 0) {
581 log.warn("No next objective in {} for ns: {}", deviceId, ns);
582 return null;
Saurav Das25190812016-05-27 13:54:07 -0700583 } else {
584 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
585 nextId, deviceId, ns);
sanghob35a6192015-04-01 13:05:26 -0700586 }
587
Saurav Das8a0732e2015-11-20 15:27:53 -0800588 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700589 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700590 }
591
592 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700593 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700594 * dstMac corresponding to the router's MAC address. For those pipelines
595 * that need to internally assign vlans to untagged packets, this method
596 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700597 * <p>
598 * Note that the vlan assignment is only done by the master-instance for a switch.
599 * However we send the filtering objective from slave-instances as well, so
600 * that drivers can obtain other information (like Router MAC and IP).
sanghob35a6192015-04-01 13:05:26 -0700601 *
Saurav Das822c4e22015-10-23 10:51:11 -0700602 * @param deviceId the switch dpid for the router
Saurav Dasd2fded02016-12-02 15:43:47 -0800603 * @return PortFilterInfo information about the processed ports
sanghob35a6192015-04-01 13:05:26 -0700604 */
Saurav Dasd2fded02016-12-02 15:43:47 -0800605 public PortFilterInfo populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700606 log.debug("Installing per-port filtering objective for untagged "
607 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800608
609 MacAddress deviceMac;
610 try {
611 deviceMac = config.getDeviceMac(deviceId);
612 } catch (DeviceConfigNotFoundException e) {
613 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
Saurav Dasd2fded02016-12-02 15:43:47 -0800614 return null;
Charles Chan0b4e6182015-11-03 10:42:14 -0800615 }
616
Saurav Das59232cf2016-04-27 18:35:50 -0700617 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hallcbd1b392017-01-18 20:15:44 -0800618 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das59232cf2016-04-27 18:35:50 -0700619 log.warn("Device {} ports not available. Unable to add MacVlan filters",
620 deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -0800621 return null;
Saurav Das59232cf2016-04-27 18:35:50 -0700622 }
Saurav Das25190812016-05-27 13:54:07 -0700623 int disabledPorts = 0, suppressedPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700624 for (Port port : devPorts) {
Charles Chand2990362016-04-18 13:44:03 -0700625 ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number());
Charles Chanf2565a92016-02-10 20:46:58 -0800626 // TODO: Handles dynamic port events when we are ready for dynamic config
Saurav Das25190812016-05-27 13:54:07 -0700627 if (!port.isEnabled()) {
628 disabledPorts++;
629 continue;
630 }
Charles Chan03a73e02016-10-24 14:52:01 -0700631
632 boolean isSuppressed = false;
633 SegmentRoutingAppConfig appConfig = srManager.cfgService
634 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
Saurav Das25190812016-05-27 13:54:07 -0700635 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700636 isSuppressed = true;
Saurav Das25190812016-05-27 13:54:07 -0700637 suppressedPorts++;
Pier Ventre917127a2016-10-31 16:49:19 -0700638 continue;
Saurav Das25190812016-05-27 13:54:07 -0700639 }
Charles Chan03a73e02016-10-24 14:52:01 -0700640
Pier Ventre10bd8d12016-11-26 21:05:22 -0800641 Ip4Prefix portSubnet = config.getPortIPv4Subnet(deviceId, port.number());
Charles Chan03a73e02016-10-24 14:52:01 -0700642 VlanId assignedVlan = (portSubnet == null || isSuppressed)
Saurav Das25190812016-05-27 13:54:07 -0700643 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
644 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan0b4e6182015-11-03 10:42:14 -0800645
Pier Ventre735b8c82016-12-02 08:16:05 -0800646 if (assignedVlan == null) {
647 log.warn("Assigned vlan is null for {} in {} - Aborting populateRouterMacVlanFilters.",
648 port.number(), deviceId);
649 return null;
650 }
651
Saurav Das25190812016-05-27 13:54:07 -0700652 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
653 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan0b4e6182015-11-03 10:42:14 -0800654 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chane849c192016-01-11 18:28:54 -0800655 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800656 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Das25190812016-05-27 13:54:07 -0700657 // vlan assignment is valid only if this instance is master
658 if (srManager.mastershipService.isLocalMaster(deviceId)) {
659 TrafficTreatment tt = DefaultTrafficTreatment.builder()
660 .pushVlan().setVlanId(assignedVlan).build();
661 fob.withMeta(tt);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700662 }
Saurav Das25190812016-05-27 13:54:07 -0700663 fob.permit().fromApp(srManager.appId);
664 log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port);
665 filteredPorts++;
666 ObjectiveContext context = new DefaultObjectiveContext(
667 (objective) -> log.debug("Filter for {} populated", connectPoint),
668 (objective, error) ->
669 log.warn("Failed to populate filter for {}: {}", connectPoint, error));
670 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Saurav Das0e99e2b2015-10-28 12:39:42 -0700671 }
Saurav Das25190812016-05-27 13:54:07 -0700672 log.info("Filtering on dev:{}, disabledPorts:{}, suppressedPorts:{}, filteredPorts:{}",
673 deviceId, disabledPorts, suppressedPorts, filteredPorts);
Saurav Dasd2fded02016-12-02 15:43:47 -0800674 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
675 suppressedPorts, filteredPorts);
sanghob35a6192015-04-01 13:05:26 -0700676 }
677
678 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700679 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700680 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700681 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700682 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700683 *
Saurav Das822c4e22015-10-23 10:51:11 -0700684 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700685 */
Saurav Das822c4e22015-10-23 10:51:11 -0700686 public void populateRouterIpPunts(DeviceId deviceId) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800687 Ip4Address routerIpv4;
688 Ip6Address routerIpv6;
Charles Chan0b4e6182015-11-03 10:42:14 -0800689 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800690 routerIpv4 = config.getRouterIpv4(deviceId);
691 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800692 } catch (DeviceConfigNotFoundException e) {
693 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
694 return;
695 }
696
Saurav Das837e0bb2015-10-30 17:45:38 -0700697 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
698 log.debug("Not installing port-IP punts - not the master for dev:{} ",
699 deviceId);
700 return;
701 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800702 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
703 allIps.add(routerIpv4);
704 if (routerIpv6 != null) {
705 allIps.add(routerIpv6);
706 }
707 for (IpAddress ipaddr : allIps) {
708 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr);
Charles Chan2df0e8a2017-01-09 11:45:08 -0800709 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
710
711 srManager.packetService.requestPackets(sbuilder.build(),
712 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700713 }
sanghob35a6192015-04-01 13:05:26 -0700714 }
715
Charles Chan68aa62d2015-11-09 16:37:23 -0800716 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800717 * Method to build IPv4 or IPv6 selector.
718 *
719 * @param addressToMatch the address to match
720 */
721 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
722 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
723 }
724
725 /**
726 * Method to build IPv4 or IPv6 selector.
727 *
728 * @param prefixToMatch the prefix to match
729 */
730 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
731 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre917127a2016-10-31 16:49:19 -0700732 // If the prefix is IPv4
Pier Ventree0ae7a32016-11-23 09:57:42 -0800733 if (prefixToMatch.isIp4()) {
734 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
735 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
736 return selectorBuilder;
737 }
Pier Ventre917127a2016-10-31 16:49:19 -0700738 // If the prefix is IPv6
Pier Ventree0ae7a32016-11-23 09:57:42 -0800739 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
740 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
741 return selectorBuilder;
742 }
743
744 /**
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800745 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
746 * Furthermore, these are applied only by the master instance. Deferred actions
747 * are not cleared such that packets can be flooded in the cross connect use case
748 *
749 * @param deviceId the switch dpid for the router
750 */
751 public void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre917127a2016-10-31 16:49:19 -0700752 // We are not the master just skip.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800753 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
754 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
755 deviceId);
756 return;
757 }
758
759 // We punt all ARP packets towards the controller.
760 ForwardingObjective puntFwd = puntArpFwdObjective()
761 .add(new ObjectiveContext() {
762 @Override
763 public void onError(Objective objective, ObjectiveError error) {
764 log.warn("Failed to install packet request for ARP to {}: {}",
765 deviceId, error);
766 }
767 });
768 srManager.flowObjectiveService.forward(deviceId, puntFwd);
769
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800770 // We punt all NDP packets towards the controller.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800771 puntFwd = puntNdpFwdObjective()
772 .add(new ObjectiveContext() {
773 @Override
774 public void onError(Objective objective, ObjectiveError error) {
775 log.warn("Failed to install packet request for NDP to {}: {}",
776 deviceId, error);
777 }
778 });
779 srManager.flowObjectiveService.forward(deviceId, puntFwd);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800780 }
781
782 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) {
783
784 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
785 tBuilder.punt();
786
787 return DefaultForwardingObjective.builder()
788 .withPriority(PacketPriority.CONTROL.priorityValue())
789 .withSelector(selector)
790 .fromApp(srManager.appId)
791 .withFlag(ForwardingObjective.Flag.VERSATILE)
792 .withTreatment(tBuilder.build())
793 .makePermanent();
794 }
795
796 private ForwardingObjective.Builder puntArpFwdObjective() {
797
798 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
799 sBuilder.matchEthType(TYPE_ARP);
800
801 return fwdObjBuilder(sBuilder.build());
802 }
803
804 private ForwardingObjective.Builder puntNdpFwdObjective() {
805
806 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
807 sBuilder.matchEthType(TYPE_IPV6)
808 .matchIPProtocol(PROTOCOL_ICMP6)
809 .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
810 .build();
811
812 return fwdObjBuilder(sBuilder.build());
813 }
814
815 /**
Charles Chan68aa62d2015-11-09 16:37:23 -0800816 * Populates a forwarding objective to send packets that miss other high
817 * priority Bridging Table entries to a group that contains all ports of
818 * its subnet.
819 *
820 * Note: We assume that packets sending from the edge switches to the hosts
821 * have untagged VLAN.
822 * The VLAN tag will be popped later in the flooding group.
823 *
824 * @param deviceId switch ID to set the rules
825 */
826 public void populateSubnetBroadcastRule(DeviceId deviceId) {
827 config.getSubnets(deviceId).forEach(subnet -> {
Pier Ventre968da122016-12-09 17:26:04 -0800828 if (subnet.isIp4()) {
829 if (subnet.prefixLength() == 0 || subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
830 return;
831 }
832 } else {
833 if (subnet.prefixLength() == 0 || subnet.prefixLength() == IpPrefix.MAX_INET6_MASK_LENGTH) {
834 return;
835 }
Charles Chand0fd5dc2016-02-16 23:14:49 -0800836 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800837 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
838 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
839
Saurav Das4ce45962015-11-24 23:21:05 -0800840 if (nextId < 0 || vlanId == null) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800841 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
842 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -0800843 return;
844 }
845
Pier Ventre917127a2016-10-31 16:49:19 -0700846 // Driver should treat objective with MacAddress.NONE as the
847 // subnet broadcast rule
Charles Chan68aa62d2015-11-09 16:37:23 -0800848 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
849 sbuilder.matchVlanId(vlanId);
850 sbuilder.matchEthDst(MacAddress.NONE);
851
852 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
853 fob.withFlag(Flag.SPECIFIC)
854 .withSelector(sbuilder.build())
855 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800856 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800857 .fromApp(srManager.appId)
858 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700859 ObjectiveContext context = new DefaultObjectiveContext(
860 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
861 (objective, error) ->
862 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
863 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800864 });
865 }
866
Charles Chan5270ed02016-01-30 23:22:37 -0800867 private int getPriorityFromPrefix(IpPrefix prefix) {
868 return (prefix.isIp4()) ?
869 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
870 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700871 }
sanghob35a6192015-04-01 13:05:26 -0700872}