blob: 54e43d5fffd353dcca829de83a8de3c2b9001ea6 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho80f11cb2015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Pier Ventre229fd0b2016-10-31 16:49:19 -070018import com.google.common.collect.Lists;
Saurav Das4c35fc42015-11-20 15:27:53 -080019import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070020import org.onlab.packet.Ethernet;
21import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080022import org.onlab.packet.Ip6Address;
23import org.onlab.packet.IpAddress;
sangho80f11cb2015-04-01 13:05:26 -070024import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070026import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070027import org.onlab.packet.VlanId;
Charles Chan10b0fb72017-02-02 16:20:42 -080028import org.onosproject.incubator.net.intf.Interface;
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;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070038import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sangho80f11cb2015-04-01 13:05:26 -070039import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070040import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070041import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070042import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070044import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070046import org.onosproject.net.flow.criteria.Criteria;
47import org.onosproject.net.flowobjective.DefaultFilteringObjective;
48import org.onosproject.net.flowobjective.DefaultForwardingObjective;
49import org.onosproject.net.flowobjective.FilteringObjective;
50import org.onosproject.net.flowobjective.ForwardingObjective;
51import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070052import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
56import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070057import java.util.Collection;
58import java.util.Collections;
Saurav Dasc28b3432015-10-30 17:45:38 -070059import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070060import java.util.List;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080061import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070062import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070063import java.util.concurrent.atomic.AtomicLong;
Charles Chan10b0fb72017-02-02 16:20:42 -080064import java.util.stream.Collectors;
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;
Charles Chan10b0fb72017-02-02 16:20:42 -080071import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sangho80f11cb2015-04-01 13:05:26 -070072
Charles Chanb7f75ac2016-01-11 18:28:54 -080073/**
74 * Populator of segment routing flow rules.
75 */
sangho80f11cb2015-04-01 13:05:26 -070076public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070077 private static final Logger log = LoggerFactory
78 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070079
sanghofb7c7292015-04-13 15:15:58 -070080 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070081 private SegmentRoutingManager srManager;
82 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070083
sangho80f11cb2015-04-01 13:05:26 -070084 /**
85 * Creates a RoutingRulePopulator object.
86 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070087 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070088 */
89 public RoutingRulePopulator(SegmentRoutingManager srManager) {
90 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070091 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -070092 this.rulePopulationCounter = new AtomicLong(0);
93 }
94
95 /**
96 * Resets the population counter.
97 */
98 public void resetCounter() {
99 rulePopulationCounter.set(0);
100 }
101
102 /**
103 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700104 *
105 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700106 */
107 public long getCounter() {
108 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700109 }
110
111 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700112 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700113 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700114 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700115 * @param deviceId device ID of the device that next hop attaches to
116 * @param prefix IP prefix of the route
117 * @param hostMac MAC address of the next hop
118 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700119 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700120 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
sangho80f11cb2015-04-01 13:05:26 -0700121 MacAddress hostMac, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700122 log.debug("Populate IP table entry for route {} at {}:{}",
123 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800124 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800125 try {
Charles Chanf4586112015-11-09 16:37:23 -0800126 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700127 deviceId, prefix, hostMac, outPort);
Charles Chan319d1a22015-11-03 10:42:14 -0800128 } catch (DeviceConfigNotFoundException e) {
129 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
130 return;
131 }
Saurav Das07c74602016-04-27 18:35:50 -0700132 if (fwdBuilder == null) {
133 log.warn("Aborting host routing table entries due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700134 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700135 return;
136 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700137 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700138 (objective) -> log.debug("IP rule for route {} populated", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700139 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700140 log.warn("Failed to populate IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700141 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800142 rulePopulationCounter.incrementAndGet();
143 }
144
Charles Chanb7f75ac2016-01-11 18:28:54 -0800145 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700146 * Removes IP rules for a route when the next hop is gone.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800147 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700148 * @param deviceId device ID of the device that next hop attaches to
149 * @param prefix IP prefix of the route
150 * @param hostMac MAC address of the next hop
151 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800152 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700153 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chanf4586112015-11-09 16:37:23 -0800154 MacAddress hostMac, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700155 log.debug("Revoke IP table entry for route {} at {}:{}",
156 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800157 ForwardingObjective.Builder fwdBuilder;
158 try {
159 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700160 deviceId, prefix, hostMac, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800161 } catch (DeviceConfigNotFoundException e) {
162 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
163 return;
164 }
Charles Chanea702b12016-11-30 11:55:05 -0800165 if (fwdBuilder == null) {
166 log.warn("Aborting host routing table entries due "
167 + "to error for dev:{} route:{}", deviceId, prefix);
168 return;
169 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700170 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700171 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700172 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700173 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700174 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800175 }
176
Charles Chanddac7fd2016-10-27 14:19:48 -0700177 /**
178 * Returns a forwarding objective that points packets destined to a
179 * given prefix to given port on given device with given destination MAC.
180 *
181 * @param deviceId device ID
182 * @param prefix prefix that need to be routed
183 * @param hostMac MAC address of the nexthop
184 * @param outPort port where the nexthop attaches to
185 * @return forwarding objective builder
186 * @throws DeviceConfigNotFoundException if given device is not configured
187 */
Charles Chanf4586112015-11-09 16:37:23 -0800188 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700189 DeviceId deviceId, IpPrefix prefix,
Charles Chanf4586112015-11-09 16:37:23 -0800190 MacAddress hostMac, PortNumber outPort)
191 throws DeviceConfigNotFoundException {
192 MacAddress deviceMac;
193 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800194
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800195 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
Saurav Das2d94d312015-11-24 23:21:05 -0800196 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700197
Charles Chanddac7fd2016-10-27 14:19:48 -0700198 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700199 tbuilder.deferred()
200 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800201 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700202 .setOutput(outPort);
sangho80f11cb2015-04-01 13:05:26 -0700203 TrafficTreatment treatment = tbuilder.build();
Saurav Das2d94d312015-11-24 23:21:05 -0800204
205 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
206 // for switch pipelines that need it, provide outgoing vlan as metadata
Charles Chan10b0fb72017-02-02 16:20:42 -0800207 VlanId untaggedVlan = srManager.getUntaggedVlanId(new ConnectPoint(deviceId, outPort));
208 VlanId outvlan = (untaggedVlan != null) ? untaggedVlan : INTERNAL_VLAN;
209
Saurav Das2d94d312015-11-24 23:21:05 -0800210 TrafficSelector meta = DefaultTrafficSelector.builder()
211 .matchVlanId(outvlan).build();
212 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
213 treatment, meta);
Saurav Das07c74602016-04-27 18:35:50 -0700214 if (portNextObjId == -1) {
215 // warning log will come from getPortNextObjective method
216 return null;
217 }
Charles Chanf4586112015-11-09 16:37:23 -0800218 return DefaultForwardingObjective.builder()
Saurav Das2d94d312015-11-24 23:21:05 -0800219 .withSelector(selector)
220 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800221 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700222 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800223 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700224 }
225
226 /**
227 * Populates IP flow rules for the subnets of the destination router.
228 *
229 * @param deviceId switch ID to set the rules
Charles Chanc22cef32016-04-29 14:38:22 -0700230 * @param subnets subnet being added
sangho80f11cb2015-04-01 13:05:26 -0700231 * @param destSw destination switch ID
232 * @param nextHops next hop switch ID list
233 * @return true if all rules are set successfully, false otherwise
234 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800235 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<IpPrefix> subnets,
Charles Chanc22cef32016-04-29 14:38:22 -0700236 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700237 for (IpPrefix subnet : subnets) {
sangho80f11cb2015-04-01 13:05:26 -0700238 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
239 return false;
240 }
241 }
Charles Chanc22cef32016-04-29 14:38:22 -0700242 return true;
243 }
sangho80f11cb2015-04-01 13:05:26 -0700244
Charles Chanc22cef32016-04-29 14:38:22 -0700245 /**
246 * Revokes IP flow rules for the subnets.
247 *
248 * @param subnets subnet being removed
249 * @return true if all rules are removed successfully, false otherwise
250 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800251 public boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700252 for (IpPrefix subnet : subnets) {
253 if (!revokeIpRuleForRouter(subnet)) {
254 return false;
255 }
256 }
sangho80f11cb2015-04-01 13:05:26 -0700257 return true;
258 }
259
260 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700261 * Populates IP flow rules for an IP prefix in the target device. The prefix
262 * is reachable via destination device.
sangho80f11cb2015-04-01 13:05:26 -0700263 *
Saurav Das88979182015-10-19 14:37:36 -0700264 * @param deviceId target device ID to set the rules
Pier Ventreadb4ae62016-11-23 09:57:42 -0800265 * @param ipPrefix the destination IP prefix
sangho80f11cb2015-04-01 13:05:26 -0700266 * @param destSw device ID of the destination router
267 * @param nextHops next hop switch ID list
268 * @return true if all rules are set successfully, false otherwise
269 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700270 public boolean populateIpRuleForRouter(DeviceId deviceId,
271 IpPrefix ipPrefix, DeviceId destSw,
272 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800273 int segmentId;
274 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800275 if (ipPrefix.isIp4()) {
276 segmentId = config.getIPv4SegmentId(destSw);
277 } else {
278 segmentId = config.getIPv6SegmentId(destSw);
279 }
Charles Chan319d1a22015-11-03 10:42:14 -0800280 } catch (DeviceConfigNotFoundException e) {
281 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
282 return false;
283 }
sangho80f11cb2015-04-01 13:05:26 -0700284
Pier Ventreadb4ae62016-11-23 09:57:42 -0800285 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800286 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700287
Charles Chanf4586112015-11-09 16:37:23 -0800288 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
289 NeighborSet ns;
290 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700291
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700292 // If the next hop is the same as the final destination, then MPLS label
293 // is not set.
sangho80f11cb2015-04-01 13:05:26 -0700294 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800295 tbuilder.immediate().decNwTtl();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700296 ns = new NeighborSet(nextHops, false);
Charles Chanf4586112015-11-09 16:37:23 -0800297 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700298 } else {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700299 ns = new NeighborSet(nextHops, false, segmentId);
Charles Chanf4586112015-11-09 16:37:23 -0800300 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700301 }
302
Saurav Das4c35fc42015-11-20 15:27:53 -0800303 // setup metadata to pass to nextObjective - indicate the vlan on egress
304 // if needed by the switch pipeline. Since neighbor sets are always to
305 // other neighboring routers, there is no subnet assigned on those ports.
306 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800307 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das4c35fc42015-11-20 15:27:53 -0800308
309 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
310 if (nextId <= 0) {
sangho2165d222015-05-01 09:38:25 -0700311 log.warn("No next objective in {} for ns: {}", deviceId, ns);
312 return false;
313 }
314
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700315 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
316 .builder()
317 .fromApp(srManager.appId)
318 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800319 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700320 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800321 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700322 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800323 if (treatment != null) {
324 fwdBuilder.withTreatment(treatment);
325 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700326 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700327 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700328 ipPrefix,
329 deviceId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700330 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700331 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
332 ipPrefix, deviceId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700333 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700334 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
335 ipPrefix, error, deviceId));
Charles Chan1eaf4802016-04-18 13:44:03 -0700336 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700337 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700338
339 return true;
340 }
341
sangho80f11cb2015-04-01 13:05:26 -0700342 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700343 * Revokes IP flow rules for the router IP address.
344 *
345 * @param ipPrefix the IP address of the destination router
346 * @return true if all rules are removed successfully, false otherwise
347 */
348 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800349 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700350 TrafficSelector selector = sbuilder.build();
351 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
352
353 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
354 .builder()
355 .fromApp(srManager.appId)
356 .makePermanent()
357 .withSelector(selector)
358 .withTreatment(dummyTreatment)
359 .withPriority(getPriorityFromPrefix(ipPrefix))
360 .withFlag(ForwardingObjective.Flag.SPECIFIC);
361
362 ObjectiveContext context = new DefaultObjectiveContext(
363 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
364 (objective, error) ->
365 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
366
367 srManager.deviceService.getAvailableDevices().forEach(device -> {
368 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
369 });
370
371 return true;
372 }
373
374 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700375 * Deals with !MPLS Bos use case.
376 *
377 * @param targetSwId the target sw
378 * @param destSwId the destination sw
379 * @param nextHops the set of next hops
380 * @param segmentId the segmentId to match
381 * @param routerIp the router ip
382 * @return a collection of fwdobjective
383 */
384 private Collection<ForwardingObjective> handleMpls(DeviceId targetSwId,
385 DeviceId destSwId,
386 Set<DeviceId> nextHops,
387 int segmentId,
388 IpAddress routerIp,
389 boolean isMplsBos) {
390
391 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
392 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800393 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700394 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
395 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
396 sbuilder.matchMplsBos(isMplsBos);
397 TrafficSelector selector = sbuilder.build();
398
399 // setup metadata to pass to nextObjective - indicate the vlan on egress
400 // if needed by the switch pipeline. Since mpls next-hops are always to
401 // other neighboring routers, there is no subnet assigned on those ports.
402 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800403 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700404
405 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
406 // If the next hop is the destination router for the segment, do pop
407 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
408 + "label {} in switch {} with pop", segmentId, targetSwId);
409 // Not-bos pop case (php for the current label). If MPLS-ECMP
410 // has been configured, the application we will request the
411 // installation for an MPLS-ECMP group.
412 ForwardingObjective.Builder fwdObjNoBosBuilder = getMplsForwardingObjective(targetSwId,
413 nextHops,
414 true,
415 isMplsBos,
416 metabuilder.build(),
417 routerIp);
418 // Error case, we cannot handle, exit.
419 if (fwdObjNoBosBuilder == null) {
420 return Collections.emptyList();
421 }
422 fwdObjBuilders.add(fwdObjNoBosBuilder);
423
424 } else {
425 // next hop is not destination, SR CONTINUE case (swap with self)
426 log.debug("Installing MPLS forwarding objective for "
427 + "label {} in switch {} without pop", segmentId, targetSwId);
428 // Not-bos pop case. If MPLS-ECMP has been configured, the
429 // application we will request the installation for an MPLS-ECMP
430 // group.
431 ForwardingObjective.Builder fwdObjNoBosBuilder = getMplsForwardingObjective(targetSwId,
432 nextHops,
433 false,
434 isMplsBos,
435 metabuilder.build(),
436 routerIp);
437 // Error case, we cannot handle, exit.
438 if (fwdObjNoBosBuilder == null) {
439 return Collections.emptyList();
440 }
441 fwdObjBuilders.add(fwdObjNoBosBuilder);
442
443 }
444
445 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
446 // We add the final property to the fwdObjs.
447 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
448
449 ((Builder) ((Builder) fwdObjBuilder
450 .fromApp(srManager.appId)
451 .makePermanent())
452 .withSelector(selector)
453 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
454 .withFlag(ForwardingObjective.Flag.SPECIFIC);
455
456 ObjectiveContext context = new DefaultObjectiveContext(
457 (objective) ->
458 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
459 objective.id(), segmentId, targetSwId),
460 (objective, error) ->
461 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
462 objective.id(), segmentId, error, targetSwId));
463
464 ForwardingObjective fob = fwdObjBuilder.add(context);
465 fwdObjs.add(fob);
466
467 }
468
469 return fwdObjs;
470 }
471
472 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700473 * Populates MPLS flow rules in the target device to point towards the
474 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700475 *
Saurav Dase0237a32016-05-27 13:54:07 -0700476 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700477 * @param destSwId destination switch device ID
478 * @param nextHops next hops switch ID list
Pier Ventre229fd0b2016-10-31 16:49:19 -0700479 * @param routerIp the router ip
sangho80f11cb2015-04-01 13:05:26 -0700480 * @return true if all rules are set successfully, false otherwise
481 */
Saurav Dase0237a32016-05-27 13:54:07 -0700482 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800483 Set<DeviceId> nextHops, IpAddress routerIp) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700484
Charles Chan319d1a22015-11-03 10:42:14 -0800485 int segmentId;
486 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800487 if (routerIp.isIp4()) {
488 segmentId = config.getIPv4SegmentId(destSwId);
489 } else {
490 segmentId = config.getIPv6SegmentId(destSwId);
491 }
Charles Chan319d1a22015-11-03 10:42:14 -0800492 } catch (DeviceConfigNotFoundException e) {
493 log.warn(e.getMessage() + " Aborting populateMplsRule.");
494 return false;
495 }
sangho80f11cb2015-04-01 13:05:26 -0700496
Pier Ventre229fd0b2016-10-31 16:49:19 -0700497 List<ForwardingObjective> fwdObjs = new ArrayList<>();
498 Collection<ForwardingObjective> fwdObjsMpls = Collections.emptyList();
499 // Generates the transit rules used by the standard "routing".
500 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
501 if (fwdObjsMpls.isEmpty()) {
502 return false;
sangho80f11cb2015-04-01 13:05:26 -0700503 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700504 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800505 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre229fd0b2016-10-31 16:49:19 -0700506 // the only case !BoS supported.
Saurav Dasf9332192017-02-18 14:05:44 -0800507 /*fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700508 if (fwdObjsMpls.isEmpty()) {
509 return false;
510 }
Saurav Dasf9332192017-02-18 14:05:44 -0800511 fwdObjs.addAll(fwdObjsMpls);*/
Pier Ventre229fd0b2016-10-31 16:49:19 -0700512
513 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700514 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700515 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
516 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700517 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700518 }
519
520 return true;
521 }
522
Saurav Das4c35fc42015-11-20 15:27:53 -0800523 private ForwardingObjective.Builder getMplsForwardingObjective(
524 DeviceId deviceId,
525 Set<DeviceId> nextHops,
526 boolean phpRequired,
527 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800528 TrafficSelector meta,
529 IpAddress routerIp) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800530
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700531 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
532 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700533
534 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
535
536 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800537 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700538 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700539 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700540 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800541 if (routerIp.isIp4()) {
542 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
543 } else {
544 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
545 }
546 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700547 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800548 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
549 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700550 }
551 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800552 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700553 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700554 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700555 }
556
Saurav Das4c35fc42015-11-20 15:27:53 -0800557 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700558 // if MPLS-ECMP == True we will build a standard NeighborSet.
559 // Otherwise a RandomNeighborSet.
560 NeighborSet ns = NeighborSet.neighborSet(false, nextHops, false);
561 if (!isBos && this.srManager.getMplsEcmp()) {
562 ns = NeighborSet.neighborSet(false, nextHops, true);
563 } else if (!isBos && !this.srManager.getMplsEcmp()) {
564 ns = NeighborSet.neighborSet(true, nextHops, true);
565 }
566 log.info("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
567 deviceId, ns);
568 // If BoS == True, all forwarding is via L3 ECMP group.
569 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
570 // MPLS-Interface group. This depends on the configuration of the option
571 // MPLS-ECMP.
572 // The metadata informs the driver that the next-Objective will be used
573 // by MPLS flows and if Bos == False the driver will use MPLS groups.
574 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta, isBos);
Saurav Das4c35fc42015-11-20 15:27:53 -0800575 if (nextId <= 0) {
576 log.warn("No next objective in {} for ns: {}", deviceId, ns);
577 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700578 } else {
579 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
580 nextId, deviceId, ns);
sangho80f11cb2015-04-01 13:05:26 -0700581 }
582
Saurav Das4c35fc42015-11-20 15:27:53 -0800583 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700584 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700585 }
586
587 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700588 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700589 * dstMac corresponding to the router's MAC address. For those pipelines
590 * that need to internally assign vlans to untagged packets, this method
591 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700592 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800593 * Note that the vlan assignment and filter programming should only be done by
594 * the master for a switch. This method is typically called at deviceAdd and
595 * programs filters only for the enabled ports of the device. For port-updates,
596 * that enable/disable ports after device add, singlePortFilter methods should
597 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700598 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700599 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800600 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700601 */
Saurav Dasd1872b02016-12-02 15:43:47 -0800602 public PortFilterInfo populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700603 log.debug("Installing per-port filtering objective for untagged "
604 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800605
Saurav Das07c74602016-04-27 18:35:50 -0700606 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800607 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700608 log.warn("Device {} ports not available. Unable to add MacVlan filters",
609 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800610 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700611 }
Saurav Dasf9332192017-02-18 14:05:44 -0800612 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700613 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700614 if (!port.isEnabled()) {
615 disabledPorts++;
616 continue;
617 }
Saurav Dasf9332192017-02-18 14:05:44 -0800618 if (populateSinglePortFilters(deviceId, port.number())) {
619 filteredPorts++;
620 } else {
621 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700622 }
Saurav Das7c305372015-10-28 12:39:42 -0700623 }
Saurav Dasf9332192017-02-18 14:05:44 -0800624 log.info("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
625 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800626 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800627 errorPorts, filteredPorts);
628 }
629
630 /**
631 * Creates filtering objectives for a single port. Should only be called
632 * by the master for a switch.
633 *
634 * @param deviceId device identifier
635 * @param portnum port identifier for port to be filtered
636 * @return true if no errors occurred during the build of the filtering objective
637 */
638 public boolean populateSinglePortFilters(DeviceId deviceId, PortNumber portnum) {
639 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum);
640 if (fob == null) {
641 // error encountered during build
642 return false;
643 }
644 log.info("Sending filtering objectives for dev/port:{}/{}", deviceId, portnum);
645 ObjectiveContext context = new DefaultObjectiveContext(
646 (objective) -> log.debug("Filter for {}/{} populated", deviceId, portnum),
647 (objective, error) ->
648 log.warn("Failed to populate filter for {}/{}: {}",
649 deviceId, portnum, error));
650 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
651 return true;
652 }
653
654 /**
655 * Removes filtering objectives for a single port. Should only be called
656 * by the master for a switch.
657 *
658 * @param deviceId device identifier
659 * @param portnum port identifier
660 */
661 public void revokeSinglePortFilters(DeviceId deviceId, PortNumber portnum) {
662 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum);
663 if (fob == null) {
664 // error encountered during build
665 return;
666 }
667 log.info("Removing filtering objectives for dev/port:{}/{}", deviceId, portnum);
668 ObjectiveContext context = new DefaultObjectiveContext(
669 (objective) -> log.debug("Filter for {}/{} removed", deviceId, portnum),
670 (objective, error) ->
671 log.warn("Failed to remove filter for {}/{}: {}",
672 deviceId, portnum, error));
673 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
674 return;
675 }
676
Saurav Dasf9332192017-02-18 14:05:44 -0800677 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId,
678 PortNumber portnum) {
679 MacAddress deviceMac;
680 try {
681 deviceMac = config.getDeviceMac(deviceId);
682 } catch (DeviceConfigNotFoundException e) {
683 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
684 return null;
685 }
686 // suppressed ports still have filtering rules pushed by SR using default vlan
687 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800688
Charles Chan10b0fb72017-02-02 16:20:42 -0800689 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
690 VlanId assignedVlan = (untaggedVlan != null) ? untaggedVlan : INTERNAL_VLAN;
Saurav Dasf9332192017-02-18 14:05:44 -0800691
692 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
693 fob.withKey(Criteria.matchInPort(portnum))
694 .addCondition(Criteria.matchEthDst(deviceMac))
695 .addCondition(Criteria.matchVlanId(VlanId.NONE))
696 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
697 TrafficTreatment tt = DefaultTrafficTreatment.builder()
698 .pushVlan().setVlanId(assignedVlan).build();
699 fob.withMeta(tt);
700 fob.permit().fromApp(srManager.appId);
701 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700702 }
703
704 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700705 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700706 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700707 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700708 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700709 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700710 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700711 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700712 public void populateRouterIpPunts(DeviceId deviceId) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800713 Ip4Address routerIpv4;
714 Ip6Address routerIpv6;
Charles Chan319d1a22015-11-03 10:42:14 -0800715 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800716 routerIpv4 = config.getRouterIpv4(deviceId);
717 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800718 } catch (DeviceConfigNotFoundException e) {
719 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
720 return;
721 }
722
Saurav Dasc28b3432015-10-30 17:45:38 -0700723 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
724 log.debug("Not installing port-IP punts - not the master for dev:{} ",
725 deviceId);
726 return;
727 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800728 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
729 allIps.add(routerIpv4);
730 if (routerIpv6 != null) {
731 allIps.add(routerIpv6);
732 }
733 for (IpAddress ipaddr : allIps) {
734 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr);
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800735 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
736
737 srManager.packetService.requestPackets(sbuilder.build(),
738 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das9f1c42e2015-10-23 10:51:11 -0700739 }
sangho80f11cb2015-04-01 13:05:26 -0700740 }
741
Charles Chanf4586112015-11-09 16:37:23 -0800742 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800743 * Method to build IPv4 or IPv6 selector.
744 *
745 * @param addressToMatch the address to match
746 */
747 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
748 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
749 }
750
751 /**
752 * Method to build IPv4 or IPv6 selector.
753 *
754 * @param prefixToMatch the prefix to match
755 */
756 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
757 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700758 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800759 if (prefixToMatch.isIp4()) {
760 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
761 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
762 return selectorBuilder;
763 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700764 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800765 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
766 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
767 return selectorBuilder;
768 }
769
770 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800771 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
772 * Furthermore, these are applied only by the master instance. Deferred actions
773 * are not cleared such that packets can be flooded in the cross connect use case
774 *
775 * @param deviceId the switch dpid for the router
776 */
777 public void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700778 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800779 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
780 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
781 deviceId);
782 return;
783 }
784
785 // We punt all ARP packets towards the controller.
786 ForwardingObjective puntFwd = puntArpFwdObjective()
787 .add(new ObjectiveContext() {
788 @Override
789 public void onError(Objective objective, ObjectiveError error) {
790 log.warn("Failed to install packet request for ARP to {}: {}",
791 deviceId, error);
792 }
793 });
794 srManager.flowObjectiveService.forward(deviceId, puntFwd);
795
Pier Luigib9632ba2017-01-12 18:14:58 -0800796 // We punt all NDP packets towards the controller.
Pier Luigib9632ba2017-01-12 18:14:58 -0800797 puntFwd = puntNdpFwdObjective()
798 .add(new ObjectiveContext() {
799 @Override
800 public void onError(Objective objective, ObjectiveError error) {
801 log.warn("Failed to install packet request for NDP to {}: {}",
802 deviceId, error);
803 }
804 });
805 srManager.flowObjectiveService.forward(deviceId, puntFwd);
Pier Luigib9632ba2017-01-12 18:14:58 -0800806 }
807
808 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) {
809
810 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
811 tBuilder.punt();
812
813 return DefaultForwardingObjective.builder()
814 .withPriority(PacketPriority.CONTROL.priorityValue())
815 .withSelector(selector)
816 .fromApp(srManager.appId)
817 .withFlag(ForwardingObjective.Flag.VERSATILE)
818 .withTreatment(tBuilder.build())
819 .makePermanent();
820 }
821
822 private ForwardingObjective.Builder puntArpFwdObjective() {
823
824 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
825 sBuilder.matchEthType(TYPE_ARP);
826
827 return fwdObjBuilder(sBuilder.build());
828 }
829
830 private ForwardingObjective.Builder puntNdpFwdObjective() {
831
832 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
833 sBuilder.matchEthType(TYPE_IPV6)
834 .matchIPProtocol(PROTOCOL_ICMP6)
835 .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
836 .build();
837
838 return fwdObjBuilder(sBuilder.build());
839 }
840
841 /**
Charles Chanf4586112015-11-09 16:37:23 -0800842 * Populates a forwarding objective to send packets that miss other high
843 * priority Bridging Table entries to a group that contains all ports of
844 * its subnet.
845 *
846 * Note: We assume that packets sending from the edge switches to the hosts
847 * have untagged VLAN.
848 * The VLAN tag will be popped later in the flooding group.
849 *
850 * @param deviceId switch ID to set the rules
851 */
852 public void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800853 Set<Interface> interfaces = srManager.interfaceService.getInterfaces();
854 Set<VlanId> vlans =
855 interfaces.stream()
856 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId) &&
857 !intf.vlanUntagged().equals(VlanId.NONE))
858 .map(Interface::vlanUntagged)
859 .collect(Collectors.toSet());
Charles Chanf4586112015-11-09 16:37:23 -0800860
Charles Chan10b0fb72017-02-02 16:20:42 -0800861 vlans.forEach(vlanId -> {
862 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
863
864 if (nextId < 0) {
865 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
866 + "to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
Saurav Das2d94d312015-11-24 23:21:05 -0800867 return;
868 }
869
Pier Ventre229fd0b2016-10-31 16:49:19 -0700870 // Driver should treat objective with MacAddress.NONE as the
871 // subnet broadcast rule
Charles Chanf4586112015-11-09 16:37:23 -0800872 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
873 sbuilder.matchVlanId(vlanId);
874 sbuilder.matchEthDst(MacAddress.NONE);
875
876 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
877 fob.withFlag(Flag.SPECIFIC)
878 .withSelector(sbuilder.build())
879 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800880 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chanf4586112015-11-09 16:37:23 -0800881 .fromApp(srManager.appId)
882 .makePermanent();
Charles Chan1eaf4802016-04-18 13:44:03 -0700883 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan10b0fb72017-02-02 16:20:42 -0800884 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700885 (objective, error) ->
Charles Chan10b0fb72017-02-02 16:20:42 -0800886 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700887 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800888 });
889 }
890
Charles Chan82ab1932016-01-30 23:22:37 -0800891 private int getPriorityFromPrefix(IpPrefix prefix) {
892 return (prefix.isIp4()) ?
893 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
894 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700895 }
sangho80f11cb2015-04-01 13:05:26 -0700896}