blob: 2f07c05e0b756f0aa45333ad1e7bd8c8549092ab [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sanghob35a6192015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Saurav Das8a0732e2015-11-20 15:27:53 -080018import org.onlab.packet.EthType;
sanghob35a6192015-04-01 13:05:26 -070019import org.onlab.packet.Ethernet;
20import org.onlab.packet.Ip4Address;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070021import org.onlab.packet.Ip4Prefix;
sanghob35a6192015-04-01 13:05:26 -070022import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
sanghob35a6192015-04-01 13:05:26 -070024import org.onlab.packet.MplsLabel;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070025import org.onlab.packet.VlanId;
Charles Chane849c192016-01-11 18:28:54 -080026import org.onosproject.net.ConnectPoint;
Charles Chand2990362016-04-18 13:44:03 -070027import org.onosproject.net.flowobjective.DefaultObjectiveContext;
28import org.onosproject.net.flowobjective.ObjectiveContext;
Saurav Dasd2fded02016-12-02 15:43:47 -080029import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan0b4e6182015-11-03 10:42:14 -080030import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
31import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chan6ea94fc2016-05-10 17:29:47 -070032import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070033import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sanghob35a6192015-04-01 13:05:26 -070034import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070035import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070036import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070037import org.onosproject.net.flow.DefaultTrafficSelector;
38import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070039import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070041import org.onosproject.net.flow.criteria.Criteria;
42import org.onosproject.net.flowobjective.DefaultFilteringObjective;
43import org.onosproject.net.flowobjective.DefaultForwardingObjective;
44import org.onosproject.net.flowobjective.FilteringObjective;
45import org.onosproject.net.flowobjective.ForwardingObjective;
46import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das822c4e22015-10-23 10:51:11 -070047import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sanghob35a6192015-04-01 13:05:26 -070048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
51import java.util.ArrayList;
Saurav Das837e0bb2015-10-30 17:45:38 -070052import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070053import java.util.List;
54import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070055import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070056
57import static com.google.common.base.Preconditions.checkNotNull;
58
Charles Chane849c192016-01-11 18:28:54 -080059/**
60 * Populator of segment routing flow rules.
61 */
sanghob35a6192015-04-01 13:05:26 -070062public class RoutingRulePopulator {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070063 private static final Logger log = LoggerFactory
64 .getLogger(RoutingRulePopulator.class);
sanghob35a6192015-04-01 13:05:26 -070065
sangho20eff1d2015-04-13 15:15:58 -070066 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070067 private SegmentRoutingManager srManager;
68 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070069
sanghob35a6192015-04-01 13:05:26 -070070 /**
71 * Creates a RoutingRulePopulator object.
72 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070073 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070074 */
75 public RoutingRulePopulator(SegmentRoutingManager srManager) {
76 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070077 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-04-13 15:15:58 -070078 this.rulePopulationCounter = new AtomicLong(0);
79 }
80
81 /**
82 * Resets the population counter.
83 */
84 public void resetCounter() {
85 rulePopulationCounter.set(0);
86 }
87
88 /**
89 * Returns the number of rules populated.
Thomas Vachuska266b4432015-04-30 18:13:25 -070090 *
91 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -070092 */
93 public long getCounter() {
94 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -070095 }
96
97 /**
Charles Chan1cdecff2016-10-27 14:19:48 -070098 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070099 * switch.
sanghob35a6192015-04-01 13:05:26 -0700100 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700101 * @param deviceId device ID of the device that next hop attaches to
102 * @param prefix IP prefix of the route
103 * @param hostMac MAC address of the next hop
104 * @param outPort port where the next hop attaches to
sanghob35a6192015-04-01 13:05:26 -0700105 */
Charles Chan1cdecff2016-10-27 14:19:48 -0700106 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
sanghob35a6192015-04-01 13:05:26 -0700107 MacAddress hostMac, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700108 log.debug("Populate IP table entry for route {} at {}:{}",
109 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800110 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800111 try {
Charles Chan68aa62d2015-11-09 16:37:23 -0800112 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700113 deviceId, prefix, hostMac, outPort);
Charles Chan0b4e6182015-11-03 10:42:14 -0800114 } catch (DeviceConfigNotFoundException e) {
115 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
116 return;
117 }
Saurav Das59232cf2016-04-27 18:35:50 -0700118 if (fwdBuilder == null) {
119 log.warn("Aborting host routing table entries due "
Charles Chan1cdecff2016-10-27 14:19:48 -0700120 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das59232cf2016-04-27 18:35:50 -0700121 return;
122 }
Charles Chand2990362016-04-18 13:44:03 -0700123 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700124 (objective) -> log.debug("IP rule for route {} populated", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700125 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700126 log.warn("Failed to populate IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700127 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800128 rulePopulationCounter.incrementAndGet();
129 }
130
Charles Chane849c192016-01-11 18:28:54 -0800131 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700132 * Removes IP rules for a route when the next hop is gone.
Charles Chane849c192016-01-11 18:28:54 -0800133 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700134 * @param deviceId device ID of the device that next hop attaches to
135 * @param prefix IP prefix of the route
136 * @param hostMac MAC address of the next hop
137 * @param outPort port that next hop attaches to
Charles Chane849c192016-01-11 18:28:54 -0800138 */
Charles Chan1cdecff2016-10-27 14:19:48 -0700139 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan68aa62d2015-11-09 16:37:23 -0800140 MacAddress hostMac, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700141 log.debug("Revoke IP table entry for route {} at {}:{}",
142 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800143 ForwardingObjective.Builder fwdBuilder;
144 try {
145 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700146 deviceId, prefix, hostMac, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800147 } catch (DeviceConfigNotFoundException e) {
148 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
149 return;
150 }
Charles Chan458b8262016-11-30 11:55:05 -0800151 if (fwdBuilder == null) {
152 log.warn("Aborting host routing table entries due "
153 + "to error for dev:{} route:{}", deviceId, prefix);
154 return;
155 }
Charles Chand2990362016-04-18 13:44:03 -0700156 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700157 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700158 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700159 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700160 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800161 }
162
Charles Chan1cdecff2016-10-27 14:19:48 -0700163 /**
164 * Returns a forwarding objective that points packets destined to a
165 * given prefix to given port on given device with given destination MAC.
166 *
167 * @param deviceId device ID
168 * @param prefix prefix that need to be routed
169 * @param hostMac MAC address of the nexthop
170 * @param outPort port where the nexthop attaches to
171 * @return forwarding objective builder
172 * @throws DeviceConfigNotFoundException if given device is not configured
173 */
Charles Chan68aa62d2015-11-09 16:37:23 -0800174 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700175 DeviceId deviceId, IpPrefix prefix,
Charles Chan68aa62d2015-11-09 16:37:23 -0800176 MacAddress hostMac, PortNumber outPort)
177 throws DeviceConfigNotFoundException {
178 MacAddress deviceMac;
179 deviceMac = config.getDeviceMac(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800180
sanghob35a6192015-04-01 13:05:26 -0700181 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
sanghob35a6192015-04-01 13:05:26 -0700182 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan1cdecff2016-10-27 14:19:48 -0700183 sbuilder.matchIPDst(prefix);
Saurav Das4ce45962015-11-24 23:21:05 -0800184 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700185
Charles Chan1cdecff2016-10-27 14:19:48 -0700186 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho1e575652015-05-14 00:39:53 -0700187 tbuilder.deferred()
188 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800189 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700190 .setOutput(outPort);
sanghob35a6192015-04-01 13:05:26 -0700191 TrafficTreatment treatment = tbuilder.build();
Saurav Das4ce45962015-11-24 23:21:05 -0800192
193 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
194 // for switch pipelines that need it, provide outgoing vlan as metadata
195 VlanId outvlan = null;
196 Ip4Prefix subnet = srManager.deviceConfiguration.getPortSubnet(deviceId, outPort);
197 if (subnet == null) {
198 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
199 } else {
200 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
201 }
202 TrafficSelector meta = DefaultTrafficSelector.builder()
203 .matchVlanId(outvlan).build();
204 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
205 treatment, meta);
Saurav Das59232cf2016-04-27 18:35:50 -0700206 if (portNextObjId == -1) {
207 // warning log will come from getPortNextObjective method
208 return null;
209 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800210 return DefaultForwardingObjective.builder()
Saurav Das4ce45962015-11-24 23:21:05 -0800211 .withSelector(selector)
212 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800213 .fromApp(srManager.appId).makePermanent()
Charles Chan1cdecff2016-10-27 14:19:48 -0700214 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan5270ed02016-01-30 23:22:37 -0800215 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700216 }
217
218 /**
219 * Populates IP flow rules for the subnets of the destination router.
220 *
221 * @param deviceId switch ID to set the rules
Charles Chan93e71ba2016-04-29 14:38:22 -0700222 * @param subnets subnet being added
sanghob35a6192015-04-01 13:05:26 -0700223 * @param destSw destination switch ID
224 * @param nextHops next hop switch ID list
225 * @return true if all rules are set successfully, false otherwise
226 */
Charles Chan93e71ba2016-04-29 14:38:22 -0700227 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<Ip4Prefix> subnets,
228 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700229 for (IpPrefix subnet : subnets) {
sanghob35a6192015-04-01 13:05:26 -0700230 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
231 return false;
232 }
233 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700234 return true;
235 }
sanghob35a6192015-04-01 13:05:26 -0700236
Charles Chan93e71ba2016-04-29 14:38:22 -0700237 /**
238 * Revokes IP flow rules for the subnets.
239 *
240 * @param subnets subnet being removed
241 * @return true if all rules are removed successfully, false otherwise
242 */
243 public boolean revokeIpRuleForSubnet(Set<Ip4Prefix> subnets) {
244 for (IpPrefix subnet : subnets) {
245 if (!revokeIpRuleForRouter(subnet)) {
246 return false;
247 }
248 }
sanghob35a6192015-04-01 13:05:26 -0700249 return true;
250 }
251
252 /**
Saurav Das25190812016-05-27 13:54:07 -0700253 * Populates IP flow rules for an IP prefix in the target device. The prefix
254 * is reachable via destination device.
sanghob35a6192015-04-01 13:05:26 -0700255 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700256 * @param deviceId target device ID to set the rules
sanghob35a6192015-04-01 13:05:26 -0700257 * @param ipPrefix the IP address of the destination router
258 * @param destSw device ID of the destination router
259 * @param nextHops next hop switch ID list
260 * @return true if all rules are set successfully, false otherwise
261 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700262 public boolean populateIpRuleForRouter(DeviceId deviceId,
263 IpPrefix ipPrefix, DeviceId destSw,
264 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800265 int segmentId;
266 try {
267 segmentId = config.getSegmentId(destSw);
268 } catch (DeviceConfigNotFoundException e) {
269 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
270 return false;
271 }
sanghob35a6192015-04-01 13:05:26 -0700272
273 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan17d38f42016-02-05 13:33:54 -0800274 sbuilder.matchIPDst(ipPrefix);
sanghob35a6192015-04-01 13:05:26 -0700275 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan68aa62d2015-11-09 16:37:23 -0800276 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700277
Charles Chan68aa62d2015-11-09 16:37:23 -0800278 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
279 NeighborSet ns;
280 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700281
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700282 // If the next hop is the same as the final destination, then MPLS label
283 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700284 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800285 tbuilder.immediate().decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700286 ns = new NeighborSet(nextHops);
Charles Chan68aa62d2015-11-09 16:37:23 -0800287 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700288 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800289 ns = new NeighborSet(nextHops, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800290 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700291 }
292
Saurav Das8a0732e2015-11-20 15:27:53 -0800293 // setup metadata to pass to nextObjective - indicate the vlan on egress
294 // if needed by the switch pipeline. Since neighbor sets are always to
295 // other neighboring routers, there is no subnet assigned on those ports.
296 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
297 metabuilder.matchVlanId(
298 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
299
300 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
301 if (nextId <= 0) {
sangho834e4b02015-05-01 09:38:25 -0700302 log.warn("No next objective in {} for ns: {}", deviceId, ns);
303 return false;
304 }
305
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700306 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
307 .builder()
308 .fromApp(srManager.appId)
309 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800310 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700311 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800312 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700313 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800314 if (treatment != null) {
315 fwdBuilder.withTreatment(treatment);
316 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700317 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700318 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700319 ipPrefix,
320 deviceId);
Charles Chand2990362016-04-18 13:44:03 -0700321 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700322 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
323 ipPrefix, deviceId),
Charles Chand2990362016-04-18 13:44:03 -0700324 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700325 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
326 ipPrefix, error, deviceId));
Charles Chand2990362016-04-18 13:44:03 -0700327 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700328 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700329
330 return true;
331 }
332
sanghob35a6192015-04-01 13:05:26 -0700333 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700334 * Revokes IP flow rules for the router IP address.
335 *
336 * @param ipPrefix the IP address of the destination router
337 * @return true if all rules are removed successfully, false otherwise
338 */
339 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
340 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
341 sbuilder.matchIPDst(ipPrefix);
342 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
343 TrafficSelector selector = sbuilder.build();
344 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
345
346 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
347 .builder()
348 .fromApp(srManager.appId)
349 .makePermanent()
350 .withSelector(selector)
351 .withTreatment(dummyTreatment)
352 .withPriority(getPriorityFromPrefix(ipPrefix))
353 .withFlag(ForwardingObjective.Flag.SPECIFIC);
354
355 ObjectiveContext context = new DefaultObjectiveContext(
356 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
357 (objective, error) ->
358 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
359
360 srManager.deviceService.getAvailableDevices().forEach(device -> {
361 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
362 });
363
364 return true;
365 }
366
367 /**
Saurav Das25190812016-05-27 13:54:07 -0700368 * Populates MPLS flow rules in the target device to point towards the
369 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700370 *
Saurav Das25190812016-05-27 13:54:07 -0700371 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700372 * @param destSwId destination switch device ID
373 * @param nextHops next hops switch ID list
374 * @return true if all rules are set successfully, false otherwise
375 */
Saurav Das25190812016-05-27 13:54:07 -0700376 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700377 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800378 int segmentId;
379 try {
380 segmentId = config.getSegmentId(destSwId);
381 } catch (DeviceConfigNotFoundException e) {
382 log.warn(e.getMessage() + " Aborting populateMplsRule.");
383 return false;
384 }
sanghob35a6192015-04-01 13:05:26 -0700385
386 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700387 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700388
389 // TODO Handle the case of Bos == false
sanghob35a6192015-04-01 13:05:26 -0700390 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das8a0732e2015-11-20 15:27:53 -0800391 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chan188ebf52015-12-23 00:15:11 -0800392 sbuilder.matchMplsBos(true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800393 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700394
Saurav Das8a0732e2015-11-20 15:27:53 -0800395 // setup metadata to pass to nextObjective - indicate the vlan on egress
396 // if needed by the switch pipeline. Since mpls next-hops are always to
397 // other neighboring routers, there is no subnet assigned on those ports.
398 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
399 metabuilder.matchVlanId(
400 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
401
402 // If the next hop is the destination router for the segment, do pop
sanghob35a6192015-04-01 13:05:26 -0700403 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700404 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das25190812016-05-27 13:54:07 -0700405 + "label {} in switch {} with pop", segmentId, targetSwId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700406
Saurav Das8a0732e2015-11-20 15:27:53 -0800407 // bos pop case (php)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700408 ForwardingObjective.Builder fwdObjBosBuilder =
Saurav Das25190812016-05-27 13:54:07 -0700409 getMplsForwardingObjective(targetSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700410 nextHops,
411 true,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700412 true,
Saurav Das8a0732e2015-11-20 15:27:53 -0800413 metabuilder.build());
414 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700415 return false;
416 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800417 fwdObjBuilders.add(fwdObjBosBuilder);
418
419 // XXX not-bos pop case, SR app multi-label not implemented yet
420 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
421 getMplsForwardingObjective(deviceId,
422 nextHops,
423 true,
424 false);*/
425
sanghob35a6192015-04-01 13:05:26 -0700426 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800427 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700428 log.debug("Installing MPLS forwarding objective for "
Saurav Das25190812016-05-27 13:54:07 -0700429 + "label {} in switch {} without pop", segmentId, targetSwId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700430
Saurav Das8a0732e2015-11-20 15:27:53 -0800431 // continue case with bos - this does get triggered in edge routers
432 // and in core routers - driver can handle depending on availability
433 // of MPLS ECMP or not
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700434 ForwardingObjective.Builder fwdObjBosBuilder =
Saurav Das25190812016-05-27 13:54:07 -0700435 getMplsForwardingObjective(targetSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700436 nextHops,
437 false,
Saurav Das8a0732e2015-11-20 15:27:53 -0800438 true,
439 metabuilder.build());
440 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700441 return false;
442 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800443 fwdObjBuilders.add(fwdObjBosBuilder);
444
445 // XXX continue case with not-bos - SR app multi label not implemented yet
446 // also requires MPLS ECMP
447 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
448 getMplsForwardingObjective(deviceId,
449 nextHops,
450 false,
451 false); */
452
sanghob35a6192015-04-01 13:05:26 -0700453 }
Saurav Das25190812016-05-27 13:54:07 -0700454 // XXX when other cases above are implemented check for validity of
455 // debug messages below
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700456 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
457 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
458 .makePermanent()).withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800459 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700460 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chand2990362016-04-18 13:44:03 -0700461 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700462 (objective) -> log.debug("MPLS rule {} for SID {} populated in dev:{} ",
463 objective.id(), segmentId, targetSwId),
Charles Chand2990362016-04-18 13:44:03 -0700464 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700465 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
466 objective.id(), segmentId, error, targetSwId));
467 ForwardingObjective fob = fwdObjBuilder.add(context);
468 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
469 fob.id(), segmentId, fob.nextId(), targetSwId);
470 srManager.flowObjectiveService.forward(targetSwId, fob);
sangho20eff1d2015-04-13 15:15:58 -0700471 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700472 }
473
474 return true;
475 }
476
Saurav Das8a0732e2015-11-20 15:27:53 -0800477 private ForwardingObjective.Builder getMplsForwardingObjective(
478 DeviceId deviceId,
479 Set<DeviceId> nextHops,
480 boolean phpRequired,
481 boolean isBos,
482 TrafficSelector meta) {
483
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700484 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
485 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700486
487 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
488
489 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800490 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700491 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700492 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700493 if (isBos) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800494 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
495 .decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700496 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800497 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
498 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700499 }
500 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800501 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700502 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700503 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700504 }
505
Saurav Das8a0732e2015-11-20 15:27:53 -0800506 // All forwarding is via ECMP group, the metadata informs the driver
507 // that the next-Objective will be used by MPLS flows. In other words,
508 // MPLS ECMP is requested. It is up to the driver to decide if these
509 // packets will be hashed or not.
510 fwdBuilder.withTreatment(tbuilder.build());
511 NeighborSet ns = new NeighborSet(nextHops);
Saurav Das25190812016-05-27 13:54:07 -0700512 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das8a0732e2015-11-20 15:27:53 -0800513 deviceId, ns);
514
515 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
516 if (nextId <= 0) {
517 log.warn("No next objective in {} for ns: {}", deviceId, ns);
518 return null;
Saurav Das25190812016-05-27 13:54:07 -0700519 } else {
520 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
521 nextId, deviceId, ns);
sanghob35a6192015-04-01 13:05:26 -0700522 }
523
Saurav Das8a0732e2015-11-20 15:27:53 -0800524 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700525 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700526 }
527
528 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700529 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700530 * dstMac corresponding to the router's MAC address. For those pipelines
531 * that need to internally assign vlans to untagged packets, this method
532 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700533 * <p>
534 * Note that the vlan assignment is only done by the master-instance for a switch.
535 * However we send the filtering objective from slave-instances as well, so
536 * that drivers can obtain other information (like Router MAC and IP).
sanghob35a6192015-04-01 13:05:26 -0700537 *
Saurav Das822c4e22015-10-23 10:51:11 -0700538 * @param deviceId the switch dpid for the router
Saurav Dasd2fded02016-12-02 15:43:47 -0800539 * @return PortFilterInfo information about the processed ports
sanghob35a6192015-04-01 13:05:26 -0700540 */
Saurav Dasd2fded02016-12-02 15:43:47 -0800541 public PortFilterInfo populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700542 log.debug("Installing per-port filtering objective for untagged "
543 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800544
545 MacAddress deviceMac;
546 try {
547 deviceMac = config.getDeviceMac(deviceId);
548 } catch (DeviceConfigNotFoundException e) {
549 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
Saurav Dasd2fded02016-12-02 15:43:47 -0800550 return null;
Charles Chan0b4e6182015-11-03 10:42:14 -0800551 }
552
Saurav Das59232cf2016-04-27 18:35:50 -0700553 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
554 if (devPorts != null && devPorts.size() == 0) {
555 log.warn("Device {} ports not available. Unable to add MacVlan filters",
556 deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -0800557 return null;
Saurav Das59232cf2016-04-27 18:35:50 -0700558 }
Saurav Das25190812016-05-27 13:54:07 -0700559 int disabledPorts = 0, suppressedPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700560 for (Port port : devPorts) {
Charles Chand2990362016-04-18 13:44:03 -0700561 ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number());
Charles Chanf2565a92016-02-10 20:46:58 -0800562 // TODO: Handles dynamic port events when we are ready for dynamic config
Charles Chan6ea94fc2016-05-10 17:29:47 -0700563 SegmentRoutingAppConfig appConfig = srManager.cfgService
564 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
Saurav Das25190812016-05-27 13:54:07 -0700565 if (!port.isEnabled()) {
566 disabledPorts++;
567 continue;
568 }
569 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
570 suppressedPorts++;
571 continue;
572 }
573 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
574 VlanId assignedVlan = (portSubnet == null)
575 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
576 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan0b4e6182015-11-03 10:42:14 -0800577
Saurav Das25190812016-05-27 13:54:07 -0700578 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
579 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan0b4e6182015-11-03 10:42:14 -0800580 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chane849c192016-01-11 18:28:54 -0800581 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800582 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Das25190812016-05-27 13:54:07 -0700583 // vlan assignment is valid only if this instance is master
584 if (srManager.mastershipService.isLocalMaster(deviceId)) {
585 TrafficTreatment tt = DefaultTrafficTreatment.builder()
586 .pushVlan().setVlanId(assignedVlan).build();
587 fob.withMeta(tt);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700588 }
Saurav Das25190812016-05-27 13:54:07 -0700589 fob.permit().fromApp(srManager.appId);
590 log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port);
591 filteredPorts++;
592 ObjectiveContext context = new DefaultObjectiveContext(
593 (objective) -> log.debug("Filter for {} populated", connectPoint),
594 (objective, error) ->
595 log.warn("Failed to populate filter for {}: {}", connectPoint, error));
596 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Saurav Das0e99e2b2015-10-28 12:39:42 -0700597 }
Saurav Das25190812016-05-27 13:54:07 -0700598 log.info("Filtering on dev:{}, disabledPorts:{}, suppressedPorts:{}, filteredPorts:{}",
599 deviceId, disabledPorts, suppressedPorts, filteredPorts);
Saurav Dasd2fded02016-12-02 15:43:47 -0800600 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
601 suppressedPorts, filteredPorts);
sanghob35a6192015-04-01 13:05:26 -0700602 }
603
604 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700605 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700606 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700607 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700608 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700609 *
Saurav Das822c4e22015-10-23 10:51:11 -0700610 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700611 */
Saurav Das822c4e22015-10-23 10:51:11 -0700612 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800613 Ip4Address routerIp;
614 try {
615 routerIp = config.getRouterIp(deviceId);
616 } catch (DeviceConfigNotFoundException e) {
617 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
618 return;
619 }
620
Saurav Das837e0bb2015-10-30 17:45:38 -0700621 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
622 log.debug("Not installing port-IP punts - not the master for dev:{} ",
623 deviceId);
624 return;
625 }
Saurav Das822c4e22015-10-23 10:51:11 -0700626 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Charles Chan5270ed02016-01-30 23:22:37 -0800627 Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId));
Charles Chan0b4e6182015-11-03 10:42:14 -0800628 allIps.add(routerIp);
Saurav Das837e0bb2015-10-30 17:45:38 -0700629 for (Ip4Address ipaddr : allIps) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800630 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
631 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
632 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
633 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das822c4e22015-10-23 10:51:11 -0700634 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chan68aa62d2015-11-09 16:37:23 -0800635 tbuilder.setOutput(PortNumber.CONTROLLER);
636 puntIp.withSelector(sbuilder.build());
637 puntIp.withTreatment(tbuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700638 puntIp.withFlag(Flag.VERSATILE)
Charles Chan5270ed02016-01-30 23:22:37 -0800639 .withPriority(SegmentRoutingService.HIGHEST_PRIORITY)
Saurav Das822c4e22015-10-23 10:51:11 -0700640 .makePermanent()
641 .fromApp(srManager.appId);
Charles Chand2990362016-04-18 13:44:03 -0700642 ObjectiveContext context = new DefaultObjectiveContext(
643 (objective) -> log.debug("IP punt rule for {} populated", ipaddr),
644 (objective, error) ->
645 log.warn("Failed to populate IP punt rule for {}: {}", ipaddr, error));
646 srManager.flowObjectiveService.forward(deviceId, puntIp.add(context));
Saurav Das822c4e22015-10-23 10:51:11 -0700647 }
sanghob35a6192015-04-01 13:05:26 -0700648 }
649
Charles Chan68aa62d2015-11-09 16:37:23 -0800650 /**
651 * Populates a forwarding objective to send packets that miss other high
652 * priority Bridging Table entries to a group that contains all ports of
653 * its subnet.
654 *
655 * Note: We assume that packets sending from the edge switches to the hosts
656 * have untagged VLAN.
657 * The VLAN tag will be popped later in the flooding group.
658 *
659 * @param deviceId switch ID to set the rules
660 */
661 public void populateSubnetBroadcastRule(DeviceId deviceId) {
662 config.getSubnets(deviceId).forEach(subnet -> {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800663 if (subnet.prefixLength() == 0 ||
664 subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
665 return;
666 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800667 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
668 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
669
Saurav Das4ce45962015-11-24 23:21:05 -0800670 if (nextId < 0 || vlanId == null) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800671 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
672 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -0800673 return;
674 }
675
Charles Chan68aa62d2015-11-09 16:37:23 -0800676 /* Driver should treat objective with MacAddress.NONE as the
677 * subnet broadcast rule
678 */
679 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
680 sbuilder.matchVlanId(vlanId);
681 sbuilder.matchEthDst(MacAddress.NONE);
682
683 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
684 fob.withFlag(Flag.SPECIFIC)
685 .withSelector(sbuilder.build())
686 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800687 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800688 .fromApp(srManager.appId)
689 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700690 ObjectiveContext context = new DefaultObjectiveContext(
691 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
692 (objective, error) ->
693 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
694 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800695 });
696 }
697
Charles Chan5270ed02016-01-30 23:22:37 -0800698 private int getPriorityFromPrefix(IpPrefix prefix) {
699 return (prefix.isIp4()) ?
700 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
701 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700702 }
sanghob35a6192015-04-01 13:05:26 -0700703}