blob: 050bb97a509dcc9fd845de2bd769f7d1e7ecff77 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho80f11cb2015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Pier Ventre229fd0b2016-10-31 16:49:19 -070018import com.google.common.collect.Lists;
Saurav Das4c35fc42015-11-20 15:27:53 -080019import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070020import org.onlab.packet.Ethernet;
21import org.onlab.packet.Ip4Address;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070022import org.onlab.packet.Ip4Prefix;
Pier Ventreadb4ae62016-11-23 09:57:42 -080023import org.onlab.packet.Ip6Address;
24import org.onlab.packet.IpAddress;
sangho80f11cb2015-04-01 13:05:26 -070025import org.onlab.packet.IpPrefix;
26import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070027import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070028import org.onlab.packet.VlanId;
Charles Chanb7f75ac2016-01-11 18:28:54 -080029import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070030import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080031import org.onosproject.net.flowobjective.Objective;
Charles Chan1eaf4802016-04-18 13:44:03 -070032import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080033import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080034import org.onosproject.net.packet.PacketPriority;
Saurav Dasd1872b02016-12-02 15:43:47 -080035import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan319d1a22015-11-03 10:42:14 -080036import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
37import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chan370a65b2016-05-10 17:29:47 -070038import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070039import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sangho80f11cb2015-04-01 13:05:26 -070040import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070041import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070042import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070045import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-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 Das9f1c42e2015-10-23 10:51:11 -070053import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56
57import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070058import java.util.Collection;
59import java.util.Collections;
Saurav Dasc28b3432015-10-30 17:45:38 -070060import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070061import java.util.List;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080062import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070063import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070064import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070065
66import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-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;
sangho80f11cb2015-04-01 13:05:26 -070071
Charles Chanb7f75ac2016-01-11 18:28:54 -080072/**
73 * Populator of segment routing flow rules.
74 */
sangho80f11cb2015-04-01 13:05:26 -070075public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070076 private static final Logger log = LoggerFactory
77 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070078
sanghofb7c7292015-04-13 15:15:58 -070079 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070080 private SegmentRoutingManager srManager;
81 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070082
sangho80f11cb2015-04-01 13:05:26 -070083 /**
84 * Creates a RoutingRulePopulator object.
85 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070086 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070087 */
88 public RoutingRulePopulator(SegmentRoutingManager srManager) {
89 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070090 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-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 Vachuska7cfc6202015-04-30 18:13:25 -0700103 *
104 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700105 */
106 public long getCounter() {
107 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700108 }
109
110 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700111 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700112 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700113 *
Charles Chanddac7fd2016-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
sangho80f11cb2015-04-01 13:05:26 -0700118 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700119 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
sangho80f11cb2015-04-01 13:05:26 -0700120 MacAddress hostMac, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700121 log.debug("Populate IP table entry for route {} at {}:{}",
122 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800123 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800124 try {
Charles Chanf4586112015-11-09 16:37:23 -0800125 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700126 deviceId, prefix, hostMac, outPort);
Charles Chan319d1a22015-11-03 10:42:14 -0800127 } catch (DeviceConfigNotFoundException e) {
128 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
129 return;
130 }
Saurav Das07c74602016-04-27 18:35:50 -0700131 if (fwdBuilder == null) {
132 log.warn("Aborting host routing table entries due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700133 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700134 return;
135 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700136 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700137 (objective) -> log.debug("IP rule for route {} populated", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700138 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700139 log.warn("Failed to populate IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700140 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800141 rulePopulationCounter.incrementAndGet();
142 }
143
Charles Chanb7f75ac2016-01-11 18:28:54 -0800144 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700145 * Removes IP rules for a route when the next hop is gone.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800146 *
Charles Chanddac7fd2016-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 Chanb7f75ac2016-01-11 18:28:54 -0800151 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700152 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chanf4586112015-11-09 16:37:23 -0800153 MacAddress hostMac, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700154 log.debug("Revoke IP table entry for route {} at {}:{}",
155 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800156 ForwardingObjective.Builder fwdBuilder;
157 try {
158 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700159 deviceId, prefix, hostMac, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800160 } catch (DeviceConfigNotFoundException e) {
161 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
162 return;
163 }
Charles Chanea702b12016-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 Chan1eaf4802016-04-18 13:44:03 -0700169 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700170 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700171 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700172 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700173 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800174 }
175
Charles Chanddac7fd2016-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 Chanf4586112015-11-09 16:37:23 -0800187 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700188 DeviceId deviceId, IpPrefix prefix,
Charles Chanf4586112015-11-09 16:37:23 -0800189 MacAddress hostMac, PortNumber outPort)
190 throws DeviceConfigNotFoundException {
191 MacAddress deviceMac;
192 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800193
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800194 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
Saurav Das2d94d312015-11-24 23:21:05 -0800195 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700196
Charles Chanddac7fd2016-10-27 14:19:48 -0700197 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700198 tbuilder.deferred()
199 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800200 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700201 .setOutput(outPort);
sangho80f11cb2015-04-01 13:05:26 -0700202 TrafficTreatment treatment = tbuilder.build();
Saurav Das2d94d312015-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 Ventre6b2c1b32016-12-09 17:26:04 -0800206 VlanId outvlan;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800207 Ip4Prefix subnet = srManager.deviceConfiguration.getPortIPv4Subnet(deviceId, outPort);
Saurav Das2d94d312015-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 Das07c74602016-04-27 18:35:50 -0700217 if (portNextObjId == -1) {
218 // warning log will come from getPortNextObjective method
219 return null;
220 }
Charles Chanf4586112015-11-09 16:37:23 -0800221 return DefaultForwardingObjective.builder()
Saurav Das2d94d312015-11-24 23:21:05 -0800222 .withSelector(selector)
223 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800224 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700225 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800226 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-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 Chanc22cef32016-04-29 14:38:22 -0700233 * @param subnets subnet being added
sangho80f11cb2015-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 Ventreb6a7f342016-11-26 21:05:22 -0800238 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<IpPrefix> subnets,
Charles Chanc22cef32016-04-29 14:38:22 -0700239 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700240 for (IpPrefix subnet : subnets) {
sangho80f11cb2015-04-01 13:05:26 -0700241 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
242 return false;
243 }
244 }
Charles Chanc22cef32016-04-29 14:38:22 -0700245 return true;
246 }
sangho80f11cb2015-04-01 13:05:26 -0700247
Charles Chanc22cef32016-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 Ventreb6a7f342016-11-26 21:05:22 -0800254 public boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700255 for (IpPrefix subnet : subnets) {
256 if (!revokeIpRuleForRouter(subnet)) {
257 return false;
258 }
259 }
sangho80f11cb2015-04-01 13:05:26 -0700260 return true;
261 }
262
263 /**
Saurav Dase0237a32016-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.
sangho80f11cb2015-04-01 13:05:26 -0700266 *
Saurav Das88979182015-10-19 14:37:36 -0700267 * @param deviceId target device ID to set the rules
Pier Ventreadb4ae62016-11-23 09:57:42 -0800268 * @param ipPrefix the destination IP prefix
sangho80f11cb2015-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 Vavilapalli64505482015-04-21 13:04:13 -0700273 public boolean populateIpRuleForRouter(DeviceId deviceId,
274 IpPrefix ipPrefix, DeviceId destSw,
275 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800276 int segmentId;
277 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800278 if (ipPrefix.isIp4()) {
279 segmentId = config.getIPv4SegmentId(destSw);
280 } else {
281 segmentId = config.getIPv6SegmentId(destSw);
282 }
Charles Chan319d1a22015-11-03 10:42:14 -0800283 } catch (DeviceConfigNotFoundException e) {
284 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
285 return false;
286 }
sangho80f11cb2015-04-01 13:05:26 -0700287
Pier Ventreadb4ae62016-11-23 09:57:42 -0800288 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800289 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700290
Charles Chanf4586112015-11-09 16:37:23 -0800291 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
292 NeighborSet ns;
293 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700294
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700295 // If the next hop is the same as the final destination, then MPLS label
296 // is not set.
sangho80f11cb2015-04-01 13:05:26 -0700297 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800298 tbuilder.immediate().decNwTtl();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700299 ns = new NeighborSet(nextHops, false);
Charles Chanf4586112015-11-09 16:37:23 -0800300 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700301 } else {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700302 ns = new NeighborSet(nextHops, false, segmentId);
Charles Chanf4586112015-11-09 16:37:23 -0800303 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700304 }
305
Saurav Das4c35fc42015-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) {
sangho2165d222015-05-01 09:38:25 -0700315 log.warn("No next objective in {} for ns: {}", deviceId, ns);
316 return false;
317 }
318
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700319 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
320 .builder()
321 .fromApp(srManager.appId)
322 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800323 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700324 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800325 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700326 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800327 if (treatment != null) {
328 fwdBuilder.withTreatment(treatment);
329 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700330 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700331 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700332 ipPrefix,
333 deviceId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700334 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700335 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
336 ipPrefix, deviceId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700337 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700338 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
339 ipPrefix, error, deviceId));
Charles Chan1eaf4802016-04-18 13:44:03 -0700340 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700341 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700342
343 return true;
344 }
345
sangho80f11cb2015-04-01 13:05:26 -0700346 /**
Charles Chanc22cef32016-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 Ventre6b2c1b32016-12-09 17:26:04 -0800353 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-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 Ventre229fd0b2016-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();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800397 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700398 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 Dase0237a32016-05-27 13:54:07 -0700478 * Populates MPLS flow rules in the target device to point towards the
479 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700480 *
Saurav Dase0237a32016-05-27 13:54:07 -0700481 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700482 * @param destSwId destination switch device ID
483 * @param nextHops next hops switch ID list
Pier Ventre229fd0b2016-10-31 16:49:19 -0700484 * @param routerIp the router ip
sangho80f11cb2015-04-01 13:05:26 -0700485 * @return true if all rules are set successfully, false otherwise
486 */
Saurav Dase0237a32016-05-27 13:54:07 -0700487 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800488 Set<DeviceId> nextHops, IpAddress routerIp) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700489
Charles Chan319d1a22015-11-03 10:42:14 -0800490 int segmentId;
491 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800492 if (routerIp.isIp4()) {
493 segmentId = config.getIPv4SegmentId(destSwId);
494 } else {
495 segmentId = config.getIPv6SegmentId(destSwId);
496 }
Charles Chan319d1a22015-11-03 10:42:14 -0800497 } catch (DeviceConfigNotFoundException e) {
498 log.warn(e.getMessage() + " Aborting populateMplsRule.");
499 return false;
500 }
sangho80f11cb2015-04-01 13:05:26 -0700501
Pier Ventre229fd0b2016-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;
sangho80f11cb2015-04-01 13:05:26 -0700508 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700509 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800510 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre229fd0b2016-10-31 16:49:19 -0700511 // the only case !BoS supported.
Saurav Dasf9332192017-02-18 14:05:44 -0800512 /*fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700513 if (fwdObjsMpls.isEmpty()) {
514 return false;
515 }
Saurav Dasf9332192017-02-18 14:05:44 -0800516 fwdObjs.addAll(fwdObjsMpls);*/
Pier Ventre229fd0b2016-10-31 16:49:19 -0700517
518 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700519 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700520 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
521 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700522 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700523 }
524
525 return true;
526 }
527
Saurav Das4c35fc42015-11-20 15:27:53 -0800528 private ForwardingObjective.Builder getMplsForwardingObjective(
529 DeviceId deviceId,
530 Set<DeviceId> nextHops,
531 boolean phpRequired,
532 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800533 TrafficSelector meta,
534 IpAddress routerIp) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800535
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700536 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
537 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700538
539 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
540
541 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800542 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700543 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700544 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700545 if (isBos) {
Pier Ventreadb4ae62016-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();
sangho80f11cb2015-04-01 13:05:26 -0700552 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800553 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
554 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700555 }
556 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800557 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700558 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700559 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700560 }
561
Saurav Das4c35fc42015-11-20 15:27:53 -0800562 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-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 Das4c35fc42015-11-20 15:27:53 -0800580 if (nextId <= 0) {
581 log.warn("No next objective in {} for ns: {}", deviceId, ns);
582 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700583 } else {
584 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
585 nextId, deviceId, ns);
sangho80f11cb2015-04-01 13:05:26 -0700586 }
587
Saurav Das4c35fc42015-11-20 15:27:53 -0800588 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700589 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700590 }
591
592 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700593 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-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 Dasc28b3432015-10-30 17:45:38 -0700597 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800598 * Note that the vlan assignment and filter programming should only be done by
599 * the master for a switch. This method is typically called at deviceAdd and
600 * programs filters only for the enabled ports of the device. For port-updates,
601 * that enable/disable ports after device add, singlePortFilter methods should
602 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700603 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700604 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800605 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700606 */
Saurav Dasd1872b02016-12-02 15:43:47 -0800607 public PortFilterInfo populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700608 log.debug("Installing per-port filtering objective for untagged "
609 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800610
Saurav Das07c74602016-04-27 18:35:50 -0700611 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800612 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700613 log.warn("Device {} ports not available. Unable to add MacVlan filters",
614 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800615 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700616 }
Saurav Dasf9332192017-02-18 14:05:44 -0800617 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700618 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700619 if (!port.isEnabled()) {
620 disabledPorts++;
621 continue;
622 }
Saurav Dasf9332192017-02-18 14:05:44 -0800623 if (populateSinglePortFilters(deviceId, port.number())) {
624 filteredPorts++;
625 } else {
626 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700627 }
Saurav Das7c305372015-10-28 12:39:42 -0700628 }
Saurav Dasf9332192017-02-18 14:05:44 -0800629 log.info("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
630 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800631 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800632 errorPorts, filteredPorts);
633 }
634
635 /**
636 * Creates filtering objectives for a single port. Should only be called
637 * by the master for a switch.
638 *
639 * @param deviceId device identifier
640 * @param portnum port identifier for port to be filtered
641 * @return true if no errors occurred during the build of the filtering objective
642 */
643 public boolean populateSinglePortFilters(DeviceId deviceId, PortNumber portnum) {
644 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum);
645 if (fob == null) {
646 // error encountered during build
647 return false;
648 }
649 log.info("Sending filtering objectives for dev/port:{}/{}", deviceId, portnum);
650 ObjectiveContext context = new DefaultObjectiveContext(
651 (objective) -> log.debug("Filter for {}/{} populated", deviceId, portnum),
652 (objective, error) ->
653 log.warn("Failed to populate filter for {}/{}: {}",
654 deviceId, portnum, error));
655 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
656 return true;
657 }
658
659 /**
660 * Removes filtering objectives for a single port. Should only be called
661 * by the master for a switch.
662 *
663 * @param deviceId device identifier
664 * @param portnum port identifier
665 */
666 public void revokeSinglePortFilters(DeviceId deviceId, PortNumber portnum) {
667 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum);
668 if (fob == null) {
669 // error encountered during build
670 return;
671 }
672 log.info("Removing filtering objectives for dev/port:{}/{}", deviceId, portnum);
673 ObjectiveContext context = new DefaultObjectiveContext(
674 (objective) -> log.debug("Filter for {}/{} removed", deviceId, portnum),
675 (objective, error) ->
676 log.warn("Failed to remove filter for {}/{}: {}",
677 deviceId, portnum, error));
678 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
679 return;
680 }
681
682
683 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId,
684 PortNumber portnum) {
685 MacAddress deviceMac;
686 try {
687 deviceMac = config.getDeviceMac(deviceId);
688 } catch (DeviceConfigNotFoundException e) {
689 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
690 return null;
691 }
692 // suppressed ports still have filtering rules pushed by SR using default vlan
693 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
694 boolean isSuppressed = false;
695 SegmentRoutingAppConfig appConfig = srManager.cfgService
696 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
697 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
698 isSuppressed = true;
699 }
700
701 Ip4Prefix portSubnet = config.getPortIPv4Subnet(deviceId, portnum);
702 VlanId assignedVlan = (portSubnet == null || isSuppressed)
703 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
704 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
705
706 if (assignedVlan == null) {
707 log.warn("Assigned vlan is null for {} in {} - Processing "
708 + "SinglePortFilters aborted", portnum, deviceId);
709 return null;
710 }
711
712 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
713 fob.withKey(Criteria.matchInPort(portnum))
714 .addCondition(Criteria.matchEthDst(deviceMac))
715 .addCondition(Criteria.matchVlanId(VlanId.NONE))
716 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
717 TrafficTreatment tt = DefaultTrafficTreatment.builder()
718 .pushVlan().setVlanId(assignedVlan).build();
719 fob.withMeta(tt);
720 fob.permit().fromApp(srManager.appId);
721 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700722 }
723
724 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700725 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700726 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700727 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700728 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700729 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700730 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700731 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700732 public void populateRouterIpPunts(DeviceId deviceId) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800733 Ip4Address routerIpv4;
734 Ip6Address routerIpv6;
Charles Chan319d1a22015-11-03 10:42:14 -0800735 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800736 routerIpv4 = config.getRouterIpv4(deviceId);
737 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800738 } catch (DeviceConfigNotFoundException e) {
739 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
740 return;
741 }
742
Saurav Dasc28b3432015-10-30 17:45:38 -0700743 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
744 log.debug("Not installing port-IP punts - not the master for dev:{} ",
745 deviceId);
746 return;
747 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800748 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
749 allIps.add(routerIpv4);
750 if (routerIpv6 != null) {
751 allIps.add(routerIpv6);
752 }
753 for (IpAddress ipaddr : allIps) {
754 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr);
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800755 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
756
757 srManager.packetService.requestPackets(sbuilder.build(),
758 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das9f1c42e2015-10-23 10:51:11 -0700759 }
sangho80f11cb2015-04-01 13:05:26 -0700760 }
761
Charles Chanf4586112015-11-09 16:37:23 -0800762 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800763 * Method to build IPv4 or IPv6 selector.
764 *
765 * @param addressToMatch the address to match
766 */
767 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
768 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
769 }
770
771 /**
772 * Method to build IPv4 or IPv6 selector.
773 *
774 * @param prefixToMatch the prefix to match
775 */
776 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
777 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700778 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800779 if (prefixToMatch.isIp4()) {
780 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
781 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
782 return selectorBuilder;
783 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700784 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800785 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
786 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
787 return selectorBuilder;
788 }
789
790 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800791 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
792 * Furthermore, these are applied only by the master instance. Deferred actions
793 * are not cleared such that packets can be flooded in the cross connect use case
794 *
795 * @param deviceId the switch dpid for the router
796 */
797 public void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700798 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800799 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
800 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
801 deviceId);
802 return;
803 }
804
805 // We punt all ARP packets towards the controller.
806 ForwardingObjective puntFwd = puntArpFwdObjective()
807 .add(new ObjectiveContext() {
808 @Override
809 public void onError(Objective objective, ObjectiveError error) {
810 log.warn("Failed to install packet request for ARP to {}: {}",
811 deviceId, error);
812 }
813 });
814 srManager.flowObjectiveService.forward(deviceId, puntFwd);
815
Pier Luigib9632ba2017-01-12 18:14:58 -0800816 // We punt all NDP packets towards the controller.
Pier Luigib9632ba2017-01-12 18:14:58 -0800817 puntFwd = puntNdpFwdObjective()
818 .add(new ObjectiveContext() {
819 @Override
820 public void onError(Objective objective, ObjectiveError error) {
821 log.warn("Failed to install packet request for NDP to {}: {}",
822 deviceId, error);
823 }
824 });
825 srManager.flowObjectiveService.forward(deviceId, puntFwd);
Pier Luigib9632ba2017-01-12 18:14:58 -0800826 }
827
828 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) {
829
830 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
831 tBuilder.punt();
832
833 return DefaultForwardingObjective.builder()
834 .withPriority(PacketPriority.CONTROL.priorityValue())
835 .withSelector(selector)
836 .fromApp(srManager.appId)
837 .withFlag(ForwardingObjective.Flag.VERSATILE)
838 .withTreatment(tBuilder.build())
839 .makePermanent();
840 }
841
842 private ForwardingObjective.Builder puntArpFwdObjective() {
843
844 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
845 sBuilder.matchEthType(TYPE_ARP);
846
847 return fwdObjBuilder(sBuilder.build());
848 }
849
850 private ForwardingObjective.Builder puntNdpFwdObjective() {
851
852 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
853 sBuilder.matchEthType(TYPE_IPV6)
854 .matchIPProtocol(PROTOCOL_ICMP6)
855 .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
856 .build();
857
858 return fwdObjBuilder(sBuilder.build());
859 }
860
861 /**
Charles Chanf4586112015-11-09 16:37:23 -0800862 * Populates a forwarding objective to send packets that miss other high
863 * priority Bridging Table entries to a group that contains all ports of
864 * its subnet.
865 *
866 * Note: We assume that packets sending from the edge switches to the hosts
867 * have untagged VLAN.
868 * The VLAN tag will be popped later in the flooding group.
869 *
870 * @param deviceId switch ID to set the rules
871 */
872 public void populateSubnetBroadcastRule(DeviceId deviceId) {
873 config.getSubnets(deviceId).forEach(subnet -> {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800874 if (subnet.isIp4()) {
875 if (subnet.prefixLength() == 0 || subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
876 return;
877 }
878 } else {
879 if (subnet.prefixLength() == 0 || subnet.prefixLength() == IpPrefix.MAX_INET6_MASK_LENGTH) {
880 return;
881 }
Charles Chanbbd004c2016-02-16 23:14:49 -0800882 }
Charles Chanf4586112015-11-09 16:37:23 -0800883 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
884 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
885
Saurav Das2d94d312015-11-24 23:21:05 -0800886 if (nextId < 0 || vlanId == null) {
Charles Chanbbd004c2016-02-16 23:14:49 -0800887 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
888 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das2d94d312015-11-24 23:21:05 -0800889 return;
890 }
891
Pier Ventre229fd0b2016-10-31 16:49:19 -0700892 // Driver should treat objective with MacAddress.NONE as the
893 // subnet broadcast rule
Charles Chanf4586112015-11-09 16:37:23 -0800894 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
895 sbuilder.matchVlanId(vlanId);
896 sbuilder.matchEthDst(MacAddress.NONE);
897
898 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
899 fob.withFlag(Flag.SPECIFIC)
900 .withSelector(sbuilder.build())
901 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800902 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chanf4586112015-11-09 16:37:23 -0800903 .fromApp(srManager.appId)
904 .makePermanent();
Charles Chan1eaf4802016-04-18 13:44:03 -0700905 ObjectiveContext context = new DefaultObjectiveContext(
906 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
907 (objective, error) ->
908 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
909 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800910 });
911 }
912
Charles Chan82ab1932016-01-30 23:22:37 -0800913 private int getPriorityFromPrefix(IpPrefix prefix) {
914 return (prefix.isIp4()) ?
915 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
916 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700917 }
sangho80f11cb2015-04-01 13:05:26 -0700918}