blob: 48c988c2c9824e31121bb74034525dcc3a283fc2 [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
Saurav Das4c35fc42015-11-20 15:27:53 -080018import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070019import org.onlab.packet.Ethernet;
20import org.onlab.packet.Ip4Address;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070021import org.onlab.packet.Ip4Prefix;
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 Chanb7f75ac2016-01-11 18:28:54 -080028import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070029import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080030import org.onosproject.net.flowobjective.Objective;
Charles Chan1eaf4802016-04-18 13:44:03 -070031import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080032import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080033import org.onosproject.net.packet.PacketPriority;
Saurav Dasd1872b02016-12-02 15:43:47 -080034import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan319d1a22015-11-03 10:42:14 -080035import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
36import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chan370a65b2016-05-10 17:29:47 -070037import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
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;
Saurav Dasc28b3432015-10-30 17:45:38 -070057import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070058import java.util.List;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080059import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070060import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070061import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070062
63import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080064import static org.onlab.packet.Ethernet.TYPE_ARP;
65import static org.onlab.packet.Ethernet.TYPE_IPV6;
66import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
67import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
sangho80f11cb2015-04-01 13:05:26 -070068
Charles Chanb7f75ac2016-01-11 18:28:54 -080069/**
70 * Populator of segment routing flow rules.
71 */
sangho80f11cb2015-04-01 13:05:26 -070072public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070073 private static final Logger log = LoggerFactory
74 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070075
sanghofb7c7292015-04-13 15:15:58 -070076 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070077 private SegmentRoutingManager srManager;
78 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070079
sangho80f11cb2015-04-01 13:05:26 -070080 /**
81 * Creates a RoutingRulePopulator object.
82 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070083 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070084 */
85 public RoutingRulePopulator(SegmentRoutingManager srManager) {
86 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070087 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -070088 this.rulePopulationCounter = new AtomicLong(0);
89 }
90
91 /**
92 * Resets the population counter.
93 */
94 public void resetCounter() {
95 rulePopulationCounter.set(0);
96 }
97
98 /**
99 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700100 *
101 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700102 */
103 public long getCounter() {
104 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700105 }
106
107 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700108 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700109 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700110 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700111 * @param deviceId device ID of the device that next hop attaches to
112 * @param prefix IP prefix of the route
113 * @param hostMac MAC address of the next hop
114 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700115 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700116 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
sangho80f11cb2015-04-01 13:05:26 -0700117 MacAddress hostMac, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700118 log.debug("Populate IP table entry for route {} at {}:{}",
119 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800120 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800121 try {
Charles Chanf4586112015-11-09 16:37:23 -0800122 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700123 deviceId, prefix, hostMac, outPort);
Charles Chan319d1a22015-11-03 10:42:14 -0800124 } catch (DeviceConfigNotFoundException e) {
125 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
126 return;
127 }
Saurav Das07c74602016-04-27 18:35:50 -0700128 if (fwdBuilder == null) {
129 log.warn("Aborting host routing table entries due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700130 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700131 return;
132 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700133 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700134 (objective) -> log.debug("IP rule for route {} populated", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700135 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700136 log.warn("Failed to populate IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700137 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800138 rulePopulationCounter.incrementAndGet();
139 }
140
Charles Chanb7f75ac2016-01-11 18:28:54 -0800141 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700142 * Removes IP rules for a route when the next hop is gone.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800143 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700144 * @param deviceId device ID of the device that next hop attaches to
145 * @param prefix IP prefix of the route
146 * @param hostMac MAC address of the next hop
147 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800148 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700149 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chanf4586112015-11-09 16:37:23 -0800150 MacAddress hostMac, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700151 log.debug("Revoke IP table entry for route {} at {}:{}",
152 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800153 ForwardingObjective.Builder fwdBuilder;
154 try {
155 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700156 deviceId, prefix, hostMac, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800157 } catch (DeviceConfigNotFoundException e) {
158 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
159 return;
160 }
Charles Chanea702b12016-11-30 11:55:05 -0800161 if (fwdBuilder == null) {
162 log.warn("Aborting host routing table entries due "
163 + "to error for dev:{} route:{}", deviceId, prefix);
164 return;
165 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700166 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700167 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700168 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700169 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700170 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800171 }
172
Charles Chanddac7fd2016-10-27 14:19:48 -0700173 /**
174 * Returns a forwarding objective that points packets destined to a
175 * given prefix to given port on given device with given destination MAC.
176 *
177 * @param deviceId device ID
178 * @param prefix prefix that need to be routed
179 * @param hostMac MAC address of the nexthop
180 * @param outPort port where the nexthop attaches to
181 * @return forwarding objective builder
182 * @throws DeviceConfigNotFoundException if given device is not configured
183 */
Charles Chanf4586112015-11-09 16:37:23 -0800184 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700185 DeviceId deviceId, IpPrefix prefix,
Charles Chanf4586112015-11-09 16:37:23 -0800186 MacAddress hostMac, PortNumber outPort)
187 throws DeviceConfigNotFoundException {
188 MacAddress deviceMac;
189 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800190
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800191 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
Saurav Das2d94d312015-11-24 23:21:05 -0800192 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700193
Charles Chanddac7fd2016-10-27 14:19:48 -0700194 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700195 tbuilder.deferred()
196 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800197 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700198 .setOutput(outPort);
sangho80f11cb2015-04-01 13:05:26 -0700199 TrafficTreatment treatment = tbuilder.build();
Saurav Das2d94d312015-11-24 23:21:05 -0800200
201 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
202 // for switch pipelines that need it, provide outgoing vlan as metadata
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800203 VlanId outvlan;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800204 Ip4Prefix subnet = srManager.deviceConfiguration.getPortIPv4Subnet(deviceId, outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800205 if (subnet == null) {
206 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
207 } else {
208 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
209 }
210 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();
sangho80f11cb2015-04-01 13:05:26 -0700296 ns = new NeighborSet(nextHops);
Charles Chanf4586112015-11-09 16:37:23 -0800297 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700298 } else {
Charles Chan319d1a22015-11-03 10:42:14 -0800299 ns = new NeighborSet(nextHops, 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);
307 metabuilder.matchVlanId(
308 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
309
310 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
311 if (nextId <= 0) {
sangho2165d222015-05-01 09:38:25 -0700312 log.warn("No next objective in {} for ns: {}", deviceId, ns);
313 return false;
314 }
315
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700316 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
317 .builder()
318 .fromApp(srManager.appId)
319 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800320 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700321 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800322 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700323 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800324 if (treatment != null) {
325 fwdBuilder.withTreatment(treatment);
326 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700327 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700328 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700329 ipPrefix,
330 deviceId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700331 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700332 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
333 ipPrefix, deviceId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700334 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700335 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
336 ipPrefix, error, deviceId));
Charles Chan1eaf4802016-04-18 13:44:03 -0700337 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700338 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700339
340 return true;
341 }
342
sangho80f11cb2015-04-01 13:05:26 -0700343 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700344 * Revokes IP flow rules for the router IP address.
345 *
346 * @param ipPrefix the IP address of the destination router
347 * @return true if all rules are removed successfully, false otherwise
348 */
349 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800350 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700351 TrafficSelector selector = sbuilder.build();
352 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
353
354 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
355 .builder()
356 .fromApp(srManager.appId)
357 .makePermanent()
358 .withSelector(selector)
359 .withTreatment(dummyTreatment)
360 .withPriority(getPriorityFromPrefix(ipPrefix))
361 .withFlag(ForwardingObjective.Flag.SPECIFIC);
362
363 ObjectiveContext context = new DefaultObjectiveContext(
364 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
365 (objective, error) ->
366 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
367
368 srManager.deviceService.getAvailableDevices().forEach(device -> {
369 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
370 });
371
372 return true;
373 }
374
375 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700376 * Populates MPLS flow rules in the target device to point towards the
377 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700378 *
Saurav Dase0237a32016-05-27 13:54:07 -0700379 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700380 * @param destSwId destination switch device ID
381 * @param nextHops next hops switch ID list
Pier Ventreadb4ae62016-11-23 09:57:42 -0800382 * @param routerIp the router Ip
sangho80f11cb2015-04-01 13:05:26 -0700383 * @return true if all rules are set successfully, false otherwise
384 */
Saurav Dase0237a32016-05-27 13:54:07 -0700385 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800386 Set<DeviceId> nextHops, IpAddress routerIp) {
Charles Chan319d1a22015-11-03 10:42:14 -0800387 int segmentId;
388 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800389 if (routerIp.isIp4()) {
390 segmentId = config.getIPv4SegmentId(destSwId);
391 } else {
392 segmentId = config.getIPv6SegmentId(destSwId);
393 }
Charles Chan319d1a22015-11-03 10:42:14 -0800394 } catch (DeviceConfigNotFoundException e) {
395 log.warn(e.getMessage() + " Aborting populateMplsRule.");
396 return false;
397 }
sangho80f11cb2015-04-01 13:05:26 -0700398
399 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -0700400 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700401
402 // TODO Handle the case of Bos == false
sangho80f11cb2015-04-01 13:05:26 -0700403 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das4c35fc42015-11-20 15:27:53 -0800404 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chande6655c2015-12-23 00:15:11 -0800405 sbuilder.matchMplsBos(true);
Saurav Das4c35fc42015-11-20 15:27:53 -0800406 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700407
Saurav Das4c35fc42015-11-20 15:27:53 -0800408 // setup metadata to pass to nextObjective - indicate the vlan on egress
409 // if needed by the switch pipeline. Since mpls next-hops are always to
410 // other neighboring routers, there is no subnet assigned on those ports.
411 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
412 metabuilder.matchVlanId(
413 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
414
415 // If the next hop is the destination router for the segment, do pop
sangho80f11cb2015-04-01 13:05:26 -0700416 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700417 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Dase0237a32016-05-27 13:54:07 -0700418 + "label {} in switch {} with pop", segmentId, targetSwId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700419
Saurav Das4c35fc42015-11-20 15:27:53 -0800420 // bos pop case (php)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700421 ForwardingObjective.Builder fwdObjBosBuilder =
Saurav Dase0237a32016-05-27 13:54:07 -0700422 getMplsForwardingObjective(targetSwId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700423 nextHops,
424 true,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700425 true,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800426 metabuilder.build(),
427 routerIp);
Saurav Das4c35fc42015-11-20 15:27:53 -0800428 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700429 return false;
430 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800431 fwdObjBuilders.add(fwdObjBosBuilder);
432
433 // XXX not-bos pop case, SR app multi-label not implemented yet
434 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
435 getMplsForwardingObjective(deviceId,
436 nextHops,
437 true,
438 false);*/
439
sangho80f11cb2015-04-01 13:05:26 -0700440 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800441 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700442 log.debug("Installing MPLS forwarding objective for "
Saurav Dase0237a32016-05-27 13:54:07 -0700443 + "label {} in switch {} without pop", segmentId, targetSwId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700444
Saurav Das4c35fc42015-11-20 15:27:53 -0800445 // continue case with bos - this does get triggered in edge routers
446 // and in core routers - driver can handle depending on availability
447 // of MPLS ECMP or not
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700448 ForwardingObjective.Builder fwdObjBosBuilder =
Saurav Dase0237a32016-05-27 13:54:07 -0700449 getMplsForwardingObjective(targetSwId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700450 nextHops,
451 false,
Saurav Das4c35fc42015-11-20 15:27:53 -0800452 true,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800453 metabuilder.build(),
454 routerIp);
Saurav Das4c35fc42015-11-20 15:27:53 -0800455 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700456 return false;
457 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800458 fwdObjBuilders.add(fwdObjBosBuilder);
459
460 // XXX continue case with not-bos - SR app multi label not implemented yet
461 // also requires MPLS ECMP
462 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
463 getMplsForwardingObjective(deviceId,
464 nextHops,
465 false,
466 false); */
467
sangho80f11cb2015-04-01 13:05:26 -0700468 }
Saurav Dase0237a32016-05-27 13:54:07 -0700469 // XXX when other cases above are implemented check for validity of
470 // debug messages below
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700471 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
472 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
473 .makePermanent()).withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800474 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700475 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan1eaf4802016-04-18 13:44:03 -0700476 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700477 (objective) -> log.debug("MPLS rule {} for SID {} populated in dev:{} ",
478 objective.id(), segmentId, targetSwId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700479 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700480 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
481 objective.id(), segmentId, error, targetSwId));
482 ForwardingObjective fob = fwdObjBuilder.add(context);
483 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
484 fob.id(), segmentId, fob.nextId(), targetSwId);
485 srManager.flowObjectiveService.forward(targetSwId, fob);
sanghofb7c7292015-04-13 15:15:58 -0700486 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700487 }
488
489 return true;
490 }
491
Saurav Das4c35fc42015-11-20 15:27:53 -0800492 private ForwardingObjective.Builder getMplsForwardingObjective(
493 DeviceId deviceId,
494 Set<DeviceId> nextHops,
495 boolean phpRequired,
496 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800497 TrafficSelector meta,
498 IpAddress routerIp) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800499
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700500 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
501 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700502
503 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
504
505 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800506 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700507 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700508 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700509 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800510 if (routerIp.isIp4()) {
511 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
512 } else {
513 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
514 }
515 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700516 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800517 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
518 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700519 }
520 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800521 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700522 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700523 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700524 }
525
Saurav Das4c35fc42015-11-20 15:27:53 -0800526 // All forwarding is via ECMP group, the metadata informs the driver
527 // that the next-Objective will be used by MPLS flows. In other words,
528 // MPLS ECMP is requested. It is up to the driver to decide if these
529 // packets will be hashed or not.
530 fwdBuilder.withTreatment(tbuilder.build());
531 NeighborSet ns = new NeighborSet(nextHops);
Saurav Dase0237a32016-05-27 13:54:07 -0700532 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das4c35fc42015-11-20 15:27:53 -0800533 deviceId, ns);
534
535 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
536 if (nextId <= 0) {
537 log.warn("No next objective in {} for ns: {}", deviceId, ns);
538 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700539 } else {
540 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
541 nextId, deviceId, ns);
sangho80f11cb2015-04-01 13:05:26 -0700542 }
543
Saurav Das4c35fc42015-11-20 15:27:53 -0800544 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700545 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700546 }
547
548 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700549 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700550 * dstMac corresponding to the router's MAC address. For those pipelines
551 * that need to internally assign vlans to untagged packets, this method
552 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700553 * <p>
554 * Note that the vlan assignment is only done by the master-instance for a switch.
555 * However we send the filtering objective from slave-instances as well, so
556 * that drivers can obtain other information (like Router MAC and IP).
sangho80f11cb2015-04-01 13:05:26 -0700557 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700558 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800559 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700560 */
Saurav Dasd1872b02016-12-02 15:43:47 -0800561 public PortFilterInfo populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700562 log.debug("Installing per-port filtering objective for untagged "
563 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800564
565 MacAddress deviceMac;
566 try {
567 deviceMac = config.getDeviceMac(deviceId);
568 } catch (DeviceConfigNotFoundException e) {
569 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
Saurav Dasd1872b02016-12-02 15:43:47 -0800570 return null;
Charles Chan319d1a22015-11-03 10:42:14 -0800571 }
572
Saurav Das07c74602016-04-27 18:35:50 -0700573 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800574 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700575 log.warn("Device {} ports not available. Unable to add MacVlan filters",
576 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800577 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700578 }
Saurav Dase0237a32016-05-27 13:54:07 -0700579 int disabledPorts = 0, suppressedPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700580 for (Port port : devPorts) {
Charles Chan1eaf4802016-04-18 13:44:03 -0700581 ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number());
Charles Chan43547ca2016-02-10 20:46:58 -0800582 // TODO: Handles dynamic port events when we are ready for dynamic config
Saurav Dase0237a32016-05-27 13:54:07 -0700583 if (!port.isEnabled()) {
584 disabledPorts++;
585 continue;
586 }
Charles Chandebfea32016-10-24 14:52:01 -0700587
588 boolean isSuppressed = false;
589 SegmentRoutingAppConfig appConfig = srManager.cfgService
590 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
Saurav Dase0237a32016-05-27 13:54:07 -0700591 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
Charles Chandebfea32016-10-24 14:52:01 -0700592 isSuppressed = true;
Saurav Dase0237a32016-05-27 13:54:07 -0700593 suppressedPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700594 }
Charles Chandebfea32016-10-24 14:52:01 -0700595
Pier Ventreb6a7f342016-11-26 21:05:22 -0800596 Ip4Prefix portSubnet = config.getPortIPv4Subnet(deviceId, port.number());
Charles Chandebfea32016-10-24 14:52:01 -0700597 VlanId assignedVlan = (portSubnet == null || isSuppressed)
Saurav Dase0237a32016-05-27 13:54:07 -0700598 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
599 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan319d1a22015-11-03 10:42:14 -0800600
Pier Ventreb6b81d52016-12-02 08:16:05 -0800601 if (assignedVlan == null) {
602 log.warn("Assigned vlan is null for {} in {} - Aborting populateRouterMacVlanFilters.",
603 port.number(), deviceId);
604 return null;
605 }
606
Saurav Dase0237a32016-05-27 13:54:07 -0700607 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
608 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan319d1a22015-11-03 10:42:14 -0800609 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chanb7f75ac2016-01-11 18:28:54 -0800610 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan82ab1932016-01-30 23:22:37 -0800611 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Dase0237a32016-05-27 13:54:07 -0700612 // vlan assignment is valid only if this instance is master
613 if (srManager.mastershipService.isLocalMaster(deviceId)) {
614 TrafficTreatment tt = DefaultTrafficTreatment.builder()
615 .pushVlan().setVlanId(assignedVlan).build();
616 fob.withMeta(tt);
Saurav Das7c305372015-10-28 12:39:42 -0700617 }
Saurav Dase0237a32016-05-27 13:54:07 -0700618 fob.permit().fromApp(srManager.appId);
619 log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port);
620 filteredPorts++;
621 ObjectiveContext context = new DefaultObjectiveContext(
622 (objective) -> log.debug("Filter for {} populated", connectPoint),
623 (objective, error) ->
624 log.warn("Failed to populate filter for {}: {}", connectPoint, error));
625 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Saurav Das7c305372015-10-28 12:39:42 -0700626 }
Saurav Dase0237a32016-05-27 13:54:07 -0700627 log.info("Filtering on dev:{}, disabledPorts:{}, suppressedPorts:{}, filteredPorts:{}",
628 deviceId, disabledPorts, suppressedPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800629 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
630 suppressedPorts, filteredPorts);
sangho80f11cb2015-04-01 13:05:26 -0700631 }
632
633 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700634 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700635 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700636 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700637 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700638 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700639 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700640 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700641 public void populateRouterIpPunts(DeviceId deviceId) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800642 Ip4Address routerIpv4;
643 Ip6Address routerIpv6;
Charles Chan319d1a22015-11-03 10:42:14 -0800644 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800645 routerIpv4 = config.getRouterIpv4(deviceId);
646 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800647 } catch (DeviceConfigNotFoundException e) {
648 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
649 return;
650 }
651
Saurav Dasc28b3432015-10-30 17:45:38 -0700652 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
653 log.debug("Not installing port-IP punts - not the master for dev:{} ",
654 deviceId);
655 return;
656 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800657 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
658 allIps.add(routerIpv4);
659 if (routerIpv6 != null) {
660 allIps.add(routerIpv6);
661 }
662 for (IpAddress ipaddr : allIps) {
663 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr);
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800664 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
665
666 srManager.packetService.requestPackets(sbuilder.build(),
667 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das9f1c42e2015-10-23 10:51:11 -0700668 }
sangho80f11cb2015-04-01 13:05:26 -0700669 }
670
Charles Chanf4586112015-11-09 16:37:23 -0800671 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800672 * Method to build IPv4 or IPv6 selector.
673 *
674 * @param addressToMatch the address to match
675 */
676 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
677 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
678 }
679
680 /**
681 * Method to build IPv4 or IPv6 selector.
682 *
683 * @param prefixToMatch the prefix to match
684 */
685 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
686 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
687 /*
688 * If the prefix is IPv4
689 */
690 if (prefixToMatch.isIp4()) {
691 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
692 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
693 return selectorBuilder;
694 }
695 /*
696 * If the prefix is IPv6
697 */
698 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
699 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
700 return selectorBuilder;
701 }
702
703 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800704 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
705 * Furthermore, these are applied only by the master instance. Deferred actions
706 * are not cleared such that packets can be flooded in the cross connect use case
707 *
708 * @param deviceId the switch dpid for the router
709 */
710 public void populateArpNdpPunts(DeviceId deviceId) {
711 /*
712 * We are not the master just skip.
713 */
714 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
715 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
716 deviceId);
717 return;
718 }
719
720 // We punt all ARP packets towards the controller.
721 ForwardingObjective puntFwd = puntArpFwdObjective()
722 .add(new ObjectiveContext() {
723 @Override
724 public void onError(Objective objective, ObjectiveError error) {
725 log.warn("Failed to install packet request for ARP to {}: {}",
726 deviceId, error);
727 }
728 });
729 srManager.flowObjectiveService.forward(deviceId, puntFwd);
730
Pier Luigib9632ba2017-01-12 18:14:58 -0800731 // We punt all NDP packets towards the controller.
Pier Luigib9632ba2017-01-12 18:14:58 -0800732 puntFwd = puntNdpFwdObjective()
733 .add(new ObjectiveContext() {
734 @Override
735 public void onError(Objective objective, ObjectiveError error) {
736 log.warn("Failed to install packet request for NDP to {}: {}",
737 deviceId, error);
738 }
739 });
740 srManager.flowObjectiveService.forward(deviceId, puntFwd);
Pier Luigib9632ba2017-01-12 18:14:58 -0800741 }
742
743 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) {
744
745 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
746 tBuilder.punt();
747
748 return DefaultForwardingObjective.builder()
749 .withPriority(PacketPriority.CONTROL.priorityValue())
750 .withSelector(selector)
751 .fromApp(srManager.appId)
752 .withFlag(ForwardingObjective.Flag.VERSATILE)
753 .withTreatment(tBuilder.build())
754 .makePermanent();
755 }
756
757 private ForwardingObjective.Builder puntArpFwdObjective() {
758
759 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
760 sBuilder.matchEthType(TYPE_ARP);
761
762 return fwdObjBuilder(sBuilder.build());
763 }
764
765 private ForwardingObjective.Builder puntNdpFwdObjective() {
766
767 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
768 sBuilder.matchEthType(TYPE_IPV6)
769 .matchIPProtocol(PROTOCOL_ICMP6)
770 .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
771 .build();
772
773 return fwdObjBuilder(sBuilder.build());
774 }
775
776 /**
Charles Chanf4586112015-11-09 16:37:23 -0800777 * Populates a forwarding objective to send packets that miss other high
778 * priority Bridging Table entries to a group that contains all ports of
779 * its subnet.
780 *
781 * Note: We assume that packets sending from the edge switches to the hosts
782 * have untagged VLAN.
783 * The VLAN tag will be popped later in the flooding group.
784 *
785 * @param deviceId switch ID to set the rules
786 */
787 public void populateSubnetBroadcastRule(DeviceId deviceId) {
788 config.getSubnets(deviceId).forEach(subnet -> {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800789 if (subnet.isIp4()) {
790 if (subnet.prefixLength() == 0 || subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
791 return;
792 }
793 } else {
794 if (subnet.prefixLength() == 0 || subnet.prefixLength() == IpPrefix.MAX_INET6_MASK_LENGTH) {
795 return;
796 }
Charles Chanbbd004c2016-02-16 23:14:49 -0800797 }
Charles Chanf4586112015-11-09 16:37:23 -0800798 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
799 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
800
Saurav Das2d94d312015-11-24 23:21:05 -0800801 if (nextId < 0 || vlanId == null) {
Charles Chanbbd004c2016-02-16 23:14:49 -0800802 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
803 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das2d94d312015-11-24 23:21:05 -0800804 return;
805 }
806
Charles Chanf4586112015-11-09 16:37:23 -0800807 /* Driver should treat objective with MacAddress.NONE as the
808 * subnet broadcast rule
809 */
810 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
811 sbuilder.matchVlanId(vlanId);
812 sbuilder.matchEthDst(MacAddress.NONE);
813
814 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
815 fob.withFlag(Flag.SPECIFIC)
816 .withSelector(sbuilder.build())
817 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800818 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chanf4586112015-11-09 16:37:23 -0800819 .fromApp(srManager.appId)
820 .makePermanent();
Charles Chan1eaf4802016-04-18 13:44:03 -0700821 ObjectiveContext context = new DefaultObjectiveContext(
822 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
823 (objective, error) ->
824 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
825 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800826 });
827 }
828
Charles Chan82ab1932016-01-30 23:22:37 -0800829 private int getPriorityFromPrefix(IpPrefix prefix) {
830 return (prefix.isIp4()) ?
831 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
832 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700833 }
sangho80f11cb2015-04-01 13:05:26 -0700834}