blob: 919642468c43371f404c7a6403125969a73a5e60 [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;
Charles Chan0b4e6182015-11-03 10:42:14 -080029import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
30import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chan6ea94fc2016-05-10 17:29:47 -070031import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070032import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sanghob35a6192015-04-01 13:05:26 -070033import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070034import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070035import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070036import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070038import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070040import org.onosproject.net.flow.criteria.Criteria;
41import org.onosproject.net.flowobjective.DefaultFilteringObjective;
42import org.onosproject.net.flowobjective.DefaultForwardingObjective;
43import org.onosproject.net.flowobjective.FilteringObjective;
44import org.onosproject.net.flowobjective.ForwardingObjective;
45import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das822c4e22015-10-23 10:51:11 -070046import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sanghob35a6192015-04-01 13:05:26 -070047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
50import java.util.ArrayList;
Saurav Das837e0bb2015-10-30 17:45:38 -070051import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070052import java.util.List;
53import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070054import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070055
56import static com.google.common.base.Preconditions.checkNotNull;
57
Charles Chane849c192016-01-11 18:28:54 -080058/**
59 * Populator of segment routing flow rules.
60 */
sanghob35a6192015-04-01 13:05:26 -070061public class RoutingRulePopulator {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070062 private static final Logger log = LoggerFactory
63 .getLogger(RoutingRulePopulator.class);
sanghob35a6192015-04-01 13:05:26 -070064
sangho20eff1d2015-04-13 15:15:58 -070065 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070066 private SegmentRoutingManager srManager;
67 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070068
sanghob35a6192015-04-01 13:05:26 -070069 /**
70 * Creates a RoutingRulePopulator object.
71 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070072 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070073 */
74 public RoutingRulePopulator(SegmentRoutingManager srManager) {
75 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070076 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-04-13 15:15:58 -070077 this.rulePopulationCounter = new AtomicLong(0);
78 }
79
80 /**
81 * Resets the population counter.
82 */
83 public void resetCounter() {
84 rulePopulationCounter.set(0);
85 }
86
87 /**
88 * Returns the number of rules populated.
Thomas Vachuska266b4432015-04-30 18:13:25 -070089 *
90 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -070091 */
92 public long getCounter() {
93 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -070094 }
95
96 /**
Charles Chan1cdecff2016-10-27 14:19:48 -070097 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070098 * switch.
sanghob35a6192015-04-01 13:05:26 -070099 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700100 * @param deviceId device ID of the device that next hop attaches to
101 * @param prefix IP prefix of the route
102 * @param hostMac MAC address of the next hop
103 * @param outPort port where the next hop attaches to
sanghob35a6192015-04-01 13:05:26 -0700104 */
Charles Chan1cdecff2016-10-27 14:19:48 -0700105 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
sanghob35a6192015-04-01 13:05:26 -0700106 MacAddress hostMac, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700107 log.debug("Populate IP table entry for route {} at {}:{}",
108 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800109 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800110 try {
Charles Chan68aa62d2015-11-09 16:37:23 -0800111 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700112 deviceId, prefix, hostMac, outPort);
Charles Chan0b4e6182015-11-03 10:42:14 -0800113 } catch (DeviceConfigNotFoundException e) {
114 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
115 return;
116 }
Saurav Das59232cf2016-04-27 18:35:50 -0700117 if (fwdBuilder == null) {
118 log.warn("Aborting host routing table entries due "
Charles Chan1cdecff2016-10-27 14:19:48 -0700119 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das59232cf2016-04-27 18:35:50 -0700120 return;
121 }
Charles Chand2990362016-04-18 13:44:03 -0700122 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700123 (objective) -> log.debug("IP rule for route {} populated", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700124 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700125 log.warn("Failed to populate IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700126 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800127 rulePopulationCounter.incrementAndGet();
128 }
129
Charles Chane849c192016-01-11 18:28:54 -0800130 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700131 * Removes IP rules for a route when the next hop is gone.
Charles Chane849c192016-01-11 18:28:54 -0800132 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700133 * @param deviceId device ID of the device that next hop attaches to
134 * @param prefix IP prefix of the route
135 * @param hostMac MAC address of the next hop
136 * @param outPort port that next hop attaches to
Charles Chane849c192016-01-11 18:28:54 -0800137 */
Charles Chan1cdecff2016-10-27 14:19:48 -0700138 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan68aa62d2015-11-09 16:37:23 -0800139 MacAddress hostMac, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700140 log.debug("Revoke IP table entry for route {} at {}:{}",
141 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800142 ForwardingObjective.Builder fwdBuilder;
143 try {
144 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700145 deviceId, prefix, hostMac, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800146 } catch (DeviceConfigNotFoundException e) {
147 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
148 return;
149 }
Charles Chand2990362016-04-18 13:44:03 -0700150 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700151 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700152 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700153 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700154 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800155 }
156
Charles Chan1cdecff2016-10-27 14:19:48 -0700157 /**
158 * Returns a forwarding objective that points packets destined to a
159 * given prefix to given port on given device with given destination MAC.
160 *
161 * @param deviceId device ID
162 * @param prefix prefix that need to be routed
163 * @param hostMac MAC address of the nexthop
164 * @param outPort port where the nexthop attaches to
165 * @return forwarding objective builder
166 * @throws DeviceConfigNotFoundException if given device is not configured
167 */
Charles Chan68aa62d2015-11-09 16:37:23 -0800168 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700169 DeviceId deviceId, IpPrefix prefix,
Charles Chan68aa62d2015-11-09 16:37:23 -0800170 MacAddress hostMac, PortNumber outPort)
171 throws DeviceConfigNotFoundException {
172 MacAddress deviceMac;
173 deviceMac = config.getDeviceMac(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800174
sanghob35a6192015-04-01 13:05:26 -0700175 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
sanghob35a6192015-04-01 13:05:26 -0700176 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan1cdecff2016-10-27 14:19:48 -0700177 sbuilder.matchIPDst(prefix);
Saurav Das4ce45962015-11-24 23:21:05 -0800178 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700179
Charles Chan1cdecff2016-10-27 14:19:48 -0700180 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho1e575652015-05-14 00:39:53 -0700181 tbuilder.deferred()
182 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800183 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700184 .setOutput(outPort);
sanghob35a6192015-04-01 13:05:26 -0700185 TrafficTreatment treatment = tbuilder.build();
Saurav Das4ce45962015-11-24 23:21:05 -0800186
187 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
188 // for switch pipelines that need it, provide outgoing vlan as metadata
189 VlanId outvlan = null;
190 Ip4Prefix subnet = srManager.deviceConfiguration.getPortSubnet(deviceId, outPort);
191 if (subnet == null) {
192 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
193 } else {
194 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
195 }
196 TrafficSelector meta = DefaultTrafficSelector.builder()
197 .matchVlanId(outvlan).build();
198 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
199 treatment, meta);
Saurav Das59232cf2016-04-27 18:35:50 -0700200 if (portNextObjId == -1) {
201 // warning log will come from getPortNextObjective method
202 return null;
203 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800204 return DefaultForwardingObjective.builder()
Saurav Das4ce45962015-11-24 23:21:05 -0800205 .withSelector(selector)
206 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800207 .fromApp(srManager.appId).makePermanent()
Charles Chan1cdecff2016-10-27 14:19:48 -0700208 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan5270ed02016-01-30 23:22:37 -0800209 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700210 }
211
212 /**
213 * Populates IP flow rules for the subnets of the destination router.
214 *
215 * @param deviceId switch ID to set the rules
Charles Chan93e71ba2016-04-29 14:38:22 -0700216 * @param subnets subnet being added
sanghob35a6192015-04-01 13:05:26 -0700217 * @param destSw destination switch ID
218 * @param nextHops next hop switch ID list
219 * @return true if all rules are set successfully, false otherwise
220 */
Charles Chan93e71ba2016-04-29 14:38:22 -0700221 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<Ip4Prefix> subnets,
222 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700223 for (IpPrefix subnet : subnets) {
sanghob35a6192015-04-01 13:05:26 -0700224 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
225 return false;
226 }
227 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700228 return true;
229 }
sanghob35a6192015-04-01 13:05:26 -0700230
Charles Chan93e71ba2016-04-29 14:38:22 -0700231 /**
232 * Revokes IP flow rules for the subnets.
233 *
234 * @param subnets subnet being removed
235 * @return true if all rules are removed successfully, false otherwise
236 */
237 public boolean revokeIpRuleForSubnet(Set<Ip4Prefix> subnets) {
238 for (IpPrefix subnet : subnets) {
239 if (!revokeIpRuleForRouter(subnet)) {
240 return false;
241 }
242 }
sanghob35a6192015-04-01 13:05:26 -0700243 return true;
244 }
245
246 /**
Saurav Das25190812016-05-27 13:54:07 -0700247 * Populates IP flow rules for an IP prefix in the target device. The prefix
248 * is reachable via destination device.
sanghob35a6192015-04-01 13:05:26 -0700249 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700250 * @param deviceId target device ID to set the rules
sanghob35a6192015-04-01 13:05:26 -0700251 * @param ipPrefix the IP address of the destination router
252 * @param destSw device ID of the destination router
253 * @param nextHops next hop switch ID list
254 * @return true if all rules are set successfully, false otherwise
255 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700256 public boolean populateIpRuleForRouter(DeviceId deviceId,
257 IpPrefix ipPrefix, DeviceId destSw,
258 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800259 int segmentId;
260 try {
261 segmentId = config.getSegmentId(destSw);
262 } catch (DeviceConfigNotFoundException e) {
263 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
264 return false;
265 }
sanghob35a6192015-04-01 13:05:26 -0700266
267 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan17d38f42016-02-05 13:33:54 -0800268 sbuilder.matchIPDst(ipPrefix);
sanghob35a6192015-04-01 13:05:26 -0700269 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan68aa62d2015-11-09 16:37:23 -0800270 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700271
Charles Chan68aa62d2015-11-09 16:37:23 -0800272 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
273 NeighborSet ns;
274 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700275
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700276 // If the next hop is the same as the final destination, then MPLS label
277 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700278 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800279 tbuilder.immediate().decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700280 ns = new NeighborSet(nextHops);
Charles Chan68aa62d2015-11-09 16:37:23 -0800281 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700282 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800283 ns = new NeighborSet(nextHops, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800284 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700285 }
286
Saurav Das8a0732e2015-11-20 15:27:53 -0800287 // setup metadata to pass to nextObjective - indicate the vlan on egress
288 // if needed by the switch pipeline. Since neighbor sets are always to
289 // other neighboring routers, there is no subnet assigned on those ports.
290 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
291 metabuilder.matchVlanId(
292 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
293
294 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
295 if (nextId <= 0) {
sangho834e4b02015-05-01 09:38:25 -0700296 log.warn("No next objective in {} for ns: {}", deviceId, ns);
297 return false;
298 }
299
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700300 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
301 .builder()
302 .fromApp(srManager.appId)
303 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800304 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700305 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800306 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700307 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800308 if (treatment != null) {
309 fwdBuilder.withTreatment(treatment);
310 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700311 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700312 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700313 ipPrefix,
314 deviceId);
Charles Chand2990362016-04-18 13:44:03 -0700315 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700316 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
317 ipPrefix, deviceId),
Charles Chand2990362016-04-18 13:44:03 -0700318 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700319 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
320 ipPrefix, error, deviceId));
Charles Chand2990362016-04-18 13:44:03 -0700321 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700322 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700323
324 return true;
325 }
326
sanghob35a6192015-04-01 13:05:26 -0700327 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700328 * Revokes IP flow rules for the router IP address.
329 *
330 * @param ipPrefix the IP address of the destination router
331 * @return true if all rules are removed successfully, false otherwise
332 */
333 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
334 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
335 sbuilder.matchIPDst(ipPrefix);
336 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
337 TrafficSelector selector = sbuilder.build();
338 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
339
340 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
341 .builder()
342 .fromApp(srManager.appId)
343 .makePermanent()
344 .withSelector(selector)
345 .withTreatment(dummyTreatment)
346 .withPriority(getPriorityFromPrefix(ipPrefix))
347 .withFlag(ForwardingObjective.Flag.SPECIFIC);
348
349 ObjectiveContext context = new DefaultObjectiveContext(
350 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
351 (objective, error) ->
352 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
353
354 srManager.deviceService.getAvailableDevices().forEach(device -> {
355 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
356 });
357
358 return true;
359 }
360
361 /**
Saurav Das25190812016-05-27 13:54:07 -0700362 * Populates MPLS flow rules in the target device to point towards the
363 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700364 *
Saurav Das25190812016-05-27 13:54:07 -0700365 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700366 * @param destSwId destination switch device ID
367 * @param nextHops next hops switch ID list
368 * @return true if all rules are set successfully, false otherwise
369 */
Saurav Das25190812016-05-27 13:54:07 -0700370 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700371 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800372 int segmentId;
373 try {
374 segmentId = config.getSegmentId(destSwId);
375 } catch (DeviceConfigNotFoundException e) {
376 log.warn(e.getMessage() + " Aborting populateMplsRule.");
377 return false;
378 }
sanghob35a6192015-04-01 13:05:26 -0700379
380 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700381 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700382
383 // TODO Handle the case of Bos == false
sanghob35a6192015-04-01 13:05:26 -0700384 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das8a0732e2015-11-20 15:27:53 -0800385 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chan188ebf52015-12-23 00:15:11 -0800386 sbuilder.matchMplsBos(true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800387 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700388
Saurav Das8a0732e2015-11-20 15:27:53 -0800389 // setup metadata to pass to nextObjective - indicate the vlan on egress
390 // if needed by the switch pipeline. Since mpls next-hops are always to
391 // other neighboring routers, there is no subnet assigned on those ports.
392 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
393 metabuilder.matchVlanId(
394 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
395
396 // If the next hop is the destination router for the segment, do pop
sanghob35a6192015-04-01 13:05:26 -0700397 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700398 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das25190812016-05-27 13:54:07 -0700399 + "label {} in switch {} with pop", segmentId, targetSwId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700400
Saurav Das8a0732e2015-11-20 15:27:53 -0800401 // bos pop case (php)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700402 ForwardingObjective.Builder fwdObjBosBuilder =
Saurav Das25190812016-05-27 13:54:07 -0700403 getMplsForwardingObjective(targetSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700404 nextHops,
405 true,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700406 true,
Saurav Das8a0732e2015-11-20 15:27:53 -0800407 metabuilder.build());
408 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700409 return false;
410 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800411 fwdObjBuilders.add(fwdObjBosBuilder);
412
413 // XXX not-bos pop case, SR app multi-label not implemented yet
414 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
415 getMplsForwardingObjective(deviceId,
416 nextHops,
417 true,
418 false);*/
419
sanghob35a6192015-04-01 13:05:26 -0700420 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800421 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700422 log.debug("Installing MPLS forwarding objective for "
Saurav Das25190812016-05-27 13:54:07 -0700423 + "label {} in switch {} without pop", segmentId, targetSwId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700424
Saurav Das8a0732e2015-11-20 15:27:53 -0800425 // continue case with bos - this does get triggered in edge routers
426 // and in core routers - driver can handle depending on availability
427 // of MPLS ECMP or not
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700428 ForwardingObjective.Builder fwdObjBosBuilder =
Saurav Das25190812016-05-27 13:54:07 -0700429 getMplsForwardingObjective(targetSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700430 nextHops,
431 false,
Saurav Das8a0732e2015-11-20 15:27:53 -0800432 true,
433 metabuilder.build());
434 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700435 return false;
436 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800437 fwdObjBuilders.add(fwdObjBosBuilder);
438
439 // XXX continue case with not-bos - SR app multi label not implemented yet
440 // also requires MPLS ECMP
441 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
442 getMplsForwardingObjective(deviceId,
443 nextHops,
444 false,
445 false); */
446
sanghob35a6192015-04-01 13:05:26 -0700447 }
Saurav Das25190812016-05-27 13:54:07 -0700448 // XXX when other cases above are implemented check for validity of
449 // debug messages below
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700450 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
451 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
452 .makePermanent()).withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800453 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700454 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chand2990362016-04-18 13:44:03 -0700455 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700456 (objective) -> log.debug("MPLS rule {} for SID {} populated in dev:{} ",
457 objective.id(), segmentId, targetSwId),
Charles Chand2990362016-04-18 13:44:03 -0700458 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700459 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
460 objective.id(), segmentId, error, targetSwId));
461 ForwardingObjective fob = fwdObjBuilder.add(context);
462 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
463 fob.id(), segmentId, fob.nextId(), targetSwId);
464 srManager.flowObjectiveService.forward(targetSwId, fob);
sangho20eff1d2015-04-13 15:15:58 -0700465 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700466 }
467
468 return true;
469 }
470
Saurav Das8a0732e2015-11-20 15:27:53 -0800471 private ForwardingObjective.Builder getMplsForwardingObjective(
472 DeviceId deviceId,
473 Set<DeviceId> nextHops,
474 boolean phpRequired,
475 boolean isBos,
476 TrafficSelector meta) {
477
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700478 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
479 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700480
481 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
482
483 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800484 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700485 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700486 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700487 if (isBos) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800488 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
489 .decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700490 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800491 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
492 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700493 }
494 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800495 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700496 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700497 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700498 }
499
Saurav Das8a0732e2015-11-20 15:27:53 -0800500 // All forwarding is via ECMP group, the metadata informs the driver
501 // that the next-Objective will be used by MPLS flows. In other words,
502 // MPLS ECMP is requested. It is up to the driver to decide if these
503 // packets will be hashed or not.
504 fwdBuilder.withTreatment(tbuilder.build());
505 NeighborSet ns = new NeighborSet(nextHops);
Saurav Das25190812016-05-27 13:54:07 -0700506 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das8a0732e2015-11-20 15:27:53 -0800507 deviceId, ns);
508
509 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
510 if (nextId <= 0) {
511 log.warn("No next objective in {} for ns: {}", deviceId, ns);
512 return null;
Saurav Das25190812016-05-27 13:54:07 -0700513 } else {
514 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
515 nextId, deviceId, ns);
sanghob35a6192015-04-01 13:05:26 -0700516 }
517
Saurav Das8a0732e2015-11-20 15:27:53 -0800518 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700519 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700520 }
521
522 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700523 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700524 * dstMac corresponding to the router's MAC address. For those pipelines
525 * that need to internally assign vlans to untagged packets, this method
526 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700527 * <p>
528 * Note that the vlan assignment is only done by the master-instance for a switch.
529 * However we send the filtering objective from slave-instances as well, so
530 * that drivers can obtain other information (like Router MAC and IP).
sanghob35a6192015-04-01 13:05:26 -0700531 *
Saurav Das822c4e22015-10-23 10:51:11 -0700532 * @param deviceId the switch dpid for the router
Charles Chan93e71ba2016-04-29 14:38:22 -0700533 * @return true if operation succeeds
sanghob35a6192015-04-01 13:05:26 -0700534 */
Saurav Das59232cf2016-04-27 18:35:50 -0700535 public boolean populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700536 log.debug("Installing per-port filtering objective for untagged "
537 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800538
539 MacAddress deviceMac;
540 try {
541 deviceMac = config.getDeviceMac(deviceId);
542 } catch (DeviceConfigNotFoundException e) {
543 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
Saurav Das59232cf2016-04-27 18:35:50 -0700544 return false;
Charles Chan0b4e6182015-11-03 10:42:14 -0800545 }
546
Saurav Das59232cf2016-04-27 18:35:50 -0700547 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
548 if (devPorts != null && devPorts.size() == 0) {
549 log.warn("Device {} ports not available. Unable to add MacVlan filters",
550 deviceId);
551 return false;
552 }
Saurav Das25190812016-05-27 13:54:07 -0700553 int disabledPorts = 0, suppressedPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700554 for (Port port : devPorts) {
Charles Chand2990362016-04-18 13:44:03 -0700555 ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number());
Charles Chanf2565a92016-02-10 20:46:58 -0800556 // TODO: Handles dynamic port events when we are ready for dynamic config
Charles Chan6ea94fc2016-05-10 17:29:47 -0700557 SegmentRoutingAppConfig appConfig = srManager.cfgService
558 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
Saurav Das25190812016-05-27 13:54:07 -0700559 if (!port.isEnabled()) {
560 disabledPorts++;
561 continue;
562 }
563 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
564 suppressedPorts++;
565 continue;
566 }
567 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
568 VlanId assignedVlan = (portSubnet == null)
569 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
570 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan0b4e6182015-11-03 10:42:14 -0800571
Saurav Das25190812016-05-27 13:54:07 -0700572 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
573 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan0b4e6182015-11-03 10:42:14 -0800574 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chane849c192016-01-11 18:28:54 -0800575 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800576 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Das25190812016-05-27 13:54:07 -0700577 // vlan assignment is valid only if this instance is master
578 if (srManager.mastershipService.isLocalMaster(deviceId)) {
579 TrafficTreatment tt = DefaultTrafficTreatment.builder()
580 .pushVlan().setVlanId(assignedVlan).build();
581 fob.withMeta(tt);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700582 }
Saurav Das25190812016-05-27 13:54:07 -0700583 fob.permit().fromApp(srManager.appId);
584 log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port);
585 filteredPorts++;
586 ObjectiveContext context = new DefaultObjectiveContext(
587 (objective) -> log.debug("Filter for {} populated", connectPoint),
588 (objective, error) ->
589 log.warn("Failed to populate filter for {}: {}", connectPoint, error));
590 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Saurav Das0e99e2b2015-10-28 12:39:42 -0700591 }
Saurav Das25190812016-05-27 13:54:07 -0700592 log.info("Filtering on dev:{}, disabledPorts:{}, suppressedPorts:{}, filteredPorts:{}",
593 deviceId, disabledPorts, suppressedPorts, filteredPorts);
594 // XXX With this check, there is a chance that not all the ports that
595 // should be filtered actually get filtered as long as one of them does.
596 // Note there is no PORT_UPDATED event that makes the port go from disabled
597 // to enabled state, because the ports comes enabled from the switch.
598 // Check ONOS core, where the port becoming available and being declared
599 // enabled is possibly not atomic.
600 return (filteredPorts > 0) ? true : false;
sanghob35a6192015-04-01 13:05:26 -0700601 }
602
603 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700604 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700605 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700606 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700607 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700608 *
Saurav Das822c4e22015-10-23 10:51:11 -0700609 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700610 */
Saurav Das822c4e22015-10-23 10:51:11 -0700611 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800612 Ip4Address routerIp;
613 try {
614 routerIp = config.getRouterIp(deviceId);
615 } catch (DeviceConfigNotFoundException e) {
616 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
617 return;
618 }
619
Saurav Das837e0bb2015-10-30 17:45:38 -0700620 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
621 log.debug("Not installing port-IP punts - not the master for dev:{} ",
622 deviceId);
623 return;
624 }
Saurav Das822c4e22015-10-23 10:51:11 -0700625 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Charles Chan5270ed02016-01-30 23:22:37 -0800626 Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId));
Charles Chan0b4e6182015-11-03 10:42:14 -0800627 allIps.add(routerIp);
Saurav Das837e0bb2015-10-30 17:45:38 -0700628 for (Ip4Address ipaddr : allIps) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800629 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
630 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
631 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
632 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das822c4e22015-10-23 10:51:11 -0700633 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chan68aa62d2015-11-09 16:37:23 -0800634 tbuilder.setOutput(PortNumber.CONTROLLER);
635 puntIp.withSelector(sbuilder.build());
636 puntIp.withTreatment(tbuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700637 puntIp.withFlag(Flag.VERSATILE)
Charles Chan5270ed02016-01-30 23:22:37 -0800638 .withPriority(SegmentRoutingService.HIGHEST_PRIORITY)
Saurav Das822c4e22015-10-23 10:51:11 -0700639 .makePermanent()
640 .fromApp(srManager.appId);
Charles Chand2990362016-04-18 13:44:03 -0700641 ObjectiveContext context = new DefaultObjectiveContext(
642 (objective) -> log.debug("IP punt rule for {} populated", ipaddr),
643 (objective, error) ->
644 log.warn("Failed to populate IP punt rule for {}: {}", ipaddr, error));
645 srManager.flowObjectiveService.forward(deviceId, puntIp.add(context));
Saurav Das822c4e22015-10-23 10:51:11 -0700646 }
sanghob35a6192015-04-01 13:05:26 -0700647 }
648
Charles Chan68aa62d2015-11-09 16:37:23 -0800649 /**
650 * Populates a forwarding objective to send packets that miss other high
651 * priority Bridging Table entries to a group that contains all ports of
652 * its subnet.
653 *
654 * Note: We assume that packets sending from the edge switches to the hosts
655 * have untagged VLAN.
656 * The VLAN tag will be popped later in the flooding group.
657 *
658 * @param deviceId switch ID to set the rules
659 */
660 public void populateSubnetBroadcastRule(DeviceId deviceId) {
661 config.getSubnets(deviceId).forEach(subnet -> {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800662 if (subnet.prefixLength() == 0 ||
663 subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
664 return;
665 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800666 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
667 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
668
Saurav Das4ce45962015-11-24 23:21:05 -0800669 if (nextId < 0 || vlanId == null) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800670 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
671 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -0800672 return;
673 }
674
Charles Chan68aa62d2015-11-09 16:37:23 -0800675 /* Driver should treat objective with MacAddress.NONE as the
676 * subnet broadcast rule
677 */
678 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
679 sbuilder.matchVlanId(vlanId);
680 sbuilder.matchEthDst(MacAddress.NONE);
681
682 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
683 fob.withFlag(Flag.SPECIFIC)
684 .withSelector(sbuilder.build())
685 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800686 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800687 .fromApp(srManager.appId)
688 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700689 ObjectiveContext context = new DefaultObjectiveContext(
690 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
691 (objective, error) ->
692 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
693 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800694 });
695 }
696
Charles Chan5270ed02016-01-30 23:22:37 -0800697 private int getPriorityFromPrefix(IpPrefix prefix) {
698 return (prefix.isIp4()) ?
699 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
700 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700701 }
sanghob35a6192015-04-01 13:05:26 -0700702}