blob: 1b4819ace801ba65034d63d22083134c02090e70 [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 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070097 * Populates IP flow rules for specific hosts directly connected to the
98 * switch.
sanghob35a6192015-04-01 13:05:26 -070099 *
100 * @param deviceId switch ID to set the rules
101 * @param hostIp host IP address
102 * @param hostMac host MAC address
103 * @param outPort port where the host is connected
104 */
105 public void populateIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
106 MacAddress hostMac, PortNumber outPort) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800107 log.debug("Populate IP table entry for host {} at {}:{}",
108 hostIp, deviceId, outPort);
109 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800110 try {
Charles Chan68aa62d2015-11-09 16:37:23 -0800111 fwdBuilder = getForwardingObjectiveBuilder(
112 deviceId, hostIp, 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 "
119 + "to error for dev:{} host:{}", deviceId, hostIp);
120 return;
121 }
Charles Chand2990362016-04-18 13:44:03 -0700122 ObjectiveContext context = new DefaultObjectiveContext(
123 (objective) -> log.debug("IP rule for host {} populated", hostIp),
124 (objective, error) ->
125 log.warn("Failed to populate IP rule for host {}: {}", hostIp, error));
126 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 /**
131 * Removes IP rules for host when the host is gone.
132 *
133 * @param deviceId device ID of the device that host attaches to
134 * @param hostIp IP address of the host
135 * @param hostMac MAC address of the host
136 * @param outPort port that host attaches to
137 */
Charles Chan68aa62d2015-11-09 16:37:23 -0800138 public void revokeIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
139 MacAddress hostMac, PortNumber outPort) {
140 log.debug("Revoke IP table entry for host {} at {}:{}",
141 hostIp, deviceId, outPort);
142 ForwardingObjective.Builder fwdBuilder;
143 try {
144 fwdBuilder = getForwardingObjectiveBuilder(
145 deviceId, hostIp, hostMac, outPort);
146 } 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(
151 (objective) -> log.debug("IP rule for host {} revoked", hostIp),
152 (objective, error) ->
153 log.warn("Failed to revoke IP rule for host {}: {}", hostIp, error));
154 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800155 }
156
157 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
158 DeviceId deviceId, Ip4Address hostIp,
159 MacAddress hostMac, PortNumber outPort)
160 throws DeviceConfigNotFoundException {
161 MacAddress deviceMac;
162 deviceMac = config.getDeviceMac(deviceId);
Charles Chan5270ed02016-01-30 23:22:37 -0800163 int priority;
Charles Chan0b4e6182015-11-03 10:42:14 -0800164
sanghob35a6192015-04-01 13:05:26 -0700165 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
166 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
167
sanghob35a6192015-04-01 13:05:26 -0700168 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan5270ed02016-01-30 23:22:37 -0800169 // Special case for default route
170 if (hostIp.isZero()) {
171 sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, 0));
172 priority = SegmentRoutingService.MIN_IP_PRIORITY;
173 } else {
174 Ip4Prefix hostIpPrefix = Ip4Prefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH);
175 sbuilder.matchIPDst(hostIpPrefix);
176 priority = getPriorityFromPrefix(hostIpPrefix);
177 }
Saurav Das4ce45962015-11-24 23:21:05 -0800178 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700179
sangho1e575652015-05-14 00:39:53 -0700180 tbuilder.deferred()
181 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800182 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700183 .setOutput(outPort);
sanghob35a6192015-04-01 13:05:26 -0700184 TrafficTreatment treatment = tbuilder.build();
Saurav Das4ce45962015-11-24 23:21:05 -0800185
186 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
187 // for switch pipelines that need it, provide outgoing vlan as metadata
188 VlanId outvlan = null;
189 Ip4Prefix subnet = srManager.deviceConfiguration.getPortSubnet(deviceId, outPort);
190 if (subnet == null) {
191 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
192 } else {
193 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
194 }
195 TrafficSelector meta = DefaultTrafficSelector.builder()
196 .matchVlanId(outvlan).build();
197 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
198 treatment, meta);
Saurav Das59232cf2016-04-27 18:35:50 -0700199 if (portNextObjId == -1) {
200 // warning log will come from getPortNextObjective method
201 return null;
202 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800203 return DefaultForwardingObjective.builder()
Saurav Das4ce45962015-11-24 23:21:05 -0800204 .withSelector(selector)
205 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800206 .fromApp(srManager.appId).makePermanent()
Charles Chan5270ed02016-01-30 23:22:37 -0800207 .withPriority(priority)
208 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700209 }
210
211 /**
212 * Populates IP flow rules for the subnets of the destination router.
213 *
214 * @param deviceId switch ID to set the rules
Charles Chan93e71ba2016-04-29 14:38:22 -0700215 * @param subnets subnet being added
sanghob35a6192015-04-01 13:05:26 -0700216 * @param destSw destination switch ID
217 * @param nextHops next hop switch ID list
218 * @return true if all rules are set successfully, false otherwise
219 */
Charles Chan93e71ba2016-04-29 14:38:22 -0700220 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<Ip4Prefix> subnets,
221 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700222 for (IpPrefix subnet : subnets) {
sanghob35a6192015-04-01 13:05:26 -0700223 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
224 return false;
225 }
226 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700227 return true;
228 }
sanghob35a6192015-04-01 13:05:26 -0700229
Charles Chan93e71ba2016-04-29 14:38:22 -0700230 /**
231 * Revokes IP flow rules for the subnets.
232 *
233 * @param subnets subnet being removed
234 * @return true if all rules are removed successfully, false otherwise
235 */
236 public boolean revokeIpRuleForSubnet(Set<Ip4Prefix> subnets) {
237 for (IpPrefix subnet : subnets) {
238 if (!revokeIpRuleForRouter(subnet)) {
239 return false;
240 }
241 }
sanghob35a6192015-04-01 13:05:26 -0700242 return true;
243 }
244
245 /**
Saurav Das25190812016-05-27 13:54:07 -0700246 * Populates IP flow rules for an IP prefix in the target device. The prefix
247 * is reachable via destination device.
sanghob35a6192015-04-01 13:05:26 -0700248 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700249 * @param deviceId target device ID to set the rules
sanghob35a6192015-04-01 13:05:26 -0700250 * @param ipPrefix the IP address of the destination router
251 * @param destSw device ID of the destination router
252 * @param nextHops next hop switch ID list
253 * @return true if all rules are set successfully, false otherwise
254 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700255 public boolean populateIpRuleForRouter(DeviceId deviceId,
256 IpPrefix ipPrefix, DeviceId destSw,
257 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800258 int segmentId;
259 try {
260 segmentId = config.getSegmentId(destSw);
261 } catch (DeviceConfigNotFoundException e) {
262 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
263 return false;
264 }
sanghob35a6192015-04-01 13:05:26 -0700265
266 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan17d38f42016-02-05 13:33:54 -0800267 sbuilder.matchIPDst(ipPrefix);
sanghob35a6192015-04-01 13:05:26 -0700268 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan68aa62d2015-11-09 16:37:23 -0800269 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700270
Charles Chan68aa62d2015-11-09 16:37:23 -0800271 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
272 NeighborSet ns;
273 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700274
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700275 // If the next hop is the same as the final destination, then MPLS label
276 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700277 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800278 tbuilder.immediate().decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700279 ns = new NeighborSet(nextHops);
Charles Chan68aa62d2015-11-09 16:37:23 -0800280 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700281 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800282 ns = new NeighborSet(nextHops, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800283 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700284 }
285
Saurav Das8a0732e2015-11-20 15:27:53 -0800286 // setup metadata to pass to nextObjective - indicate the vlan on egress
287 // if needed by the switch pipeline. Since neighbor sets are always to
288 // other neighboring routers, there is no subnet assigned on those ports.
289 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
290 metabuilder.matchVlanId(
291 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
292
293 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
294 if (nextId <= 0) {
sangho834e4b02015-05-01 09:38:25 -0700295 log.warn("No next objective in {} for ns: {}", deviceId, ns);
296 return false;
297 }
298
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700299 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
300 .builder()
301 .fromApp(srManager.appId)
302 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800303 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700304 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800305 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700306 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800307 if (treatment != null) {
308 fwdBuilder.withTreatment(treatment);
309 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700310 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700311 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700312 ipPrefix,
313 deviceId);
Charles Chand2990362016-04-18 13:44:03 -0700314 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700315 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
316 ipPrefix, deviceId),
Charles Chand2990362016-04-18 13:44:03 -0700317 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700318 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
319 ipPrefix, error, deviceId));
Charles Chand2990362016-04-18 13:44:03 -0700320 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700321 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700322
323 return true;
324 }
325
sanghob35a6192015-04-01 13:05:26 -0700326 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700327 * Revokes IP flow rules for the router IP address.
328 *
329 * @param ipPrefix the IP address of the destination router
330 * @return true if all rules are removed successfully, false otherwise
331 */
332 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
333 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
334 sbuilder.matchIPDst(ipPrefix);
335 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
336 TrafficSelector selector = sbuilder.build();
337 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
338
339 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
340 .builder()
341 .fromApp(srManager.appId)
342 .makePermanent()
343 .withSelector(selector)
344 .withTreatment(dummyTreatment)
345 .withPriority(getPriorityFromPrefix(ipPrefix))
346 .withFlag(ForwardingObjective.Flag.SPECIFIC);
347
348 ObjectiveContext context = new DefaultObjectiveContext(
349 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
350 (objective, error) ->
351 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
352
353 srManager.deviceService.getAvailableDevices().forEach(device -> {
354 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
355 });
356
357 return true;
358 }
359
360 /**
Saurav Das25190812016-05-27 13:54:07 -0700361 * Populates MPLS flow rules in the target device to point towards the
362 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700363 *
Saurav Das25190812016-05-27 13:54:07 -0700364 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700365 * @param destSwId destination switch device ID
366 * @param nextHops next hops switch ID list
367 * @return true if all rules are set successfully, false otherwise
368 */
Saurav Das25190812016-05-27 13:54:07 -0700369 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700370 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800371 int segmentId;
372 try {
373 segmentId = config.getSegmentId(destSwId);
374 } catch (DeviceConfigNotFoundException e) {
375 log.warn(e.getMessage() + " Aborting populateMplsRule.");
376 return false;
377 }
sanghob35a6192015-04-01 13:05:26 -0700378
379 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700380 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700381
382 // TODO Handle the case of Bos == false
sanghob35a6192015-04-01 13:05:26 -0700383 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das8a0732e2015-11-20 15:27:53 -0800384 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chan188ebf52015-12-23 00:15:11 -0800385 sbuilder.matchMplsBos(true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800386 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700387
Saurav Das8a0732e2015-11-20 15:27:53 -0800388 // setup metadata to pass to nextObjective - indicate the vlan on egress
389 // if needed by the switch pipeline. Since mpls next-hops are always to
390 // other neighboring routers, there is no subnet assigned on those ports.
391 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
392 metabuilder.matchVlanId(
393 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
394
395 // If the next hop is the destination router for the segment, do pop
sanghob35a6192015-04-01 13:05:26 -0700396 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700397 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das25190812016-05-27 13:54:07 -0700398 + "label {} in switch {} with pop", segmentId, targetSwId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700399
Saurav Das8a0732e2015-11-20 15:27:53 -0800400 // bos pop case (php)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700401 ForwardingObjective.Builder fwdObjBosBuilder =
Saurav Das25190812016-05-27 13:54:07 -0700402 getMplsForwardingObjective(targetSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700403 nextHops,
404 true,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700405 true,
Saurav Das8a0732e2015-11-20 15:27:53 -0800406 metabuilder.build());
407 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700408 return false;
409 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800410 fwdObjBuilders.add(fwdObjBosBuilder);
411
412 // XXX not-bos pop case, SR app multi-label not implemented yet
413 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
414 getMplsForwardingObjective(deviceId,
415 nextHops,
416 true,
417 false);*/
418
sanghob35a6192015-04-01 13:05:26 -0700419 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800420 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700421 log.debug("Installing MPLS forwarding objective for "
Saurav Das25190812016-05-27 13:54:07 -0700422 + "label {} in switch {} without pop", segmentId, targetSwId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700423
Saurav Das8a0732e2015-11-20 15:27:53 -0800424 // continue case with bos - this does get triggered in edge routers
425 // and in core routers - driver can handle depending on availability
426 // of MPLS ECMP or not
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700427 ForwardingObjective.Builder fwdObjBosBuilder =
Saurav Das25190812016-05-27 13:54:07 -0700428 getMplsForwardingObjective(targetSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700429 nextHops,
430 false,
Saurav Das8a0732e2015-11-20 15:27:53 -0800431 true,
432 metabuilder.build());
433 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700434 return false;
435 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800436 fwdObjBuilders.add(fwdObjBosBuilder);
437
438 // XXX continue case with not-bos - SR app multi label not implemented yet
439 // also requires MPLS ECMP
440 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
441 getMplsForwardingObjective(deviceId,
442 nextHops,
443 false,
444 false); */
445
sanghob35a6192015-04-01 13:05:26 -0700446 }
Saurav Das25190812016-05-27 13:54:07 -0700447 // XXX when other cases above are implemented check for validity of
448 // debug messages below
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700449 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
450 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
451 .makePermanent()).withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800452 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700453 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chand2990362016-04-18 13:44:03 -0700454 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700455 (objective) -> log.debug("MPLS rule {} for SID {} populated in dev:{} ",
456 objective.id(), segmentId, targetSwId),
Charles Chand2990362016-04-18 13:44:03 -0700457 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700458 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
459 objective.id(), segmentId, error, targetSwId));
460 ForwardingObjective fob = fwdObjBuilder.add(context);
461 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
462 fob.id(), segmentId, fob.nextId(), targetSwId);
463 srManager.flowObjectiveService.forward(targetSwId, fob);
sangho20eff1d2015-04-13 15:15:58 -0700464 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700465 }
466
467 return true;
468 }
469
Saurav Das8a0732e2015-11-20 15:27:53 -0800470 private ForwardingObjective.Builder getMplsForwardingObjective(
471 DeviceId deviceId,
472 Set<DeviceId> nextHops,
473 boolean phpRequired,
474 boolean isBos,
475 TrafficSelector meta) {
476
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700477 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
478 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700479
480 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
481
482 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800483 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700484 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700485 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700486 if (isBos) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800487 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
488 .decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700489 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800490 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
491 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700492 }
493 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800494 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700495 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700496 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700497 }
498
Saurav Das8a0732e2015-11-20 15:27:53 -0800499 // All forwarding is via ECMP group, the metadata informs the driver
500 // that the next-Objective will be used by MPLS flows. In other words,
501 // MPLS ECMP is requested. It is up to the driver to decide if these
502 // packets will be hashed or not.
503 fwdBuilder.withTreatment(tbuilder.build());
504 NeighborSet ns = new NeighborSet(nextHops);
Saurav Das25190812016-05-27 13:54:07 -0700505 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das8a0732e2015-11-20 15:27:53 -0800506 deviceId, ns);
507
508 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
509 if (nextId <= 0) {
510 log.warn("No next objective in {} for ns: {}", deviceId, ns);
511 return null;
Saurav Das25190812016-05-27 13:54:07 -0700512 } else {
513 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
514 nextId, deviceId, ns);
sanghob35a6192015-04-01 13:05:26 -0700515 }
516
Saurav Das8a0732e2015-11-20 15:27:53 -0800517 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700518 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700519 }
520
521 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700522 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700523 * dstMac corresponding to the router's MAC address. For those pipelines
524 * that need to internally assign vlans to untagged packets, this method
525 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700526 * <p>
527 * Note that the vlan assignment is only done by the master-instance for a switch.
528 * However we send the filtering objective from slave-instances as well, so
529 * that drivers can obtain other information (like Router MAC and IP).
sanghob35a6192015-04-01 13:05:26 -0700530 *
Saurav Das822c4e22015-10-23 10:51:11 -0700531 * @param deviceId the switch dpid for the router
Charles Chan93e71ba2016-04-29 14:38:22 -0700532 * @return true if operation succeeds
sanghob35a6192015-04-01 13:05:26 -0700533 */
Saurav Das59232cf2016-04-27 18:35:50 -0700534 public boolean populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700535 log.debug("Installing per-port filtering objective for untagged "
536 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800537
538 MacAddress deviceMac;
539 try {
540 deviceMac = config.getDeviceMac(deviceId);
541 } catch (DeviceConfigNotFoundException e) {
542 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
Saurav Das59232cf2016-04-27 18:35:50 -0700543 return false;
Charles Chan0b4e6182015-11-03 10:42:14 -0800544 }
545
Saurav Das59232cf2016-04-27 18:35:50 -0700546 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
547 if (devPorts != null && devPorts.size() == 0) {
548 log.warn("Device {} ports not available. Unable to add MacVlan filters",
549 deviceId);
550 return false;
551 }
Saurav Das25190812016-05-27 13:54:07 -0700552 int disabledPorts = 0, suppressedPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700553 for (Port port : devPorts) {
Charles Chand2990362016-04-18 13:44:03 -0700554 ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number());
Charles Chanf2565a92016-02-10 20:46:58 -0800555 // TODO: Handles dynamic port events when we are ready for dynamic config
Charles Chan6ea94fc2016-05-10 17:29:47 -0700556 SegmentRoutingAppConfig appConfig = srManager.cfgService
557 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
Saurav Das25190812016-05-27 13:54:07 -0700558 if (!port.isEnabled()) {
559 disabledPorts++;
560 continue;
561 }
562 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
563 suppressedPorts++;
564 continue;
565 }
566 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
567 VlanId assignedVlan = (portSubnet == null)
568 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
569 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan0b4e6182015-11-03 10:42:14 -0800570
Saurav Das25190812016-05-27 13:54:07 -0700571 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
572 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan0b4e6182015-11-03 10:42:14 -0800573 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chane849c192016-01-11 18:28:54 -0800574 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800575 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Das25190812016-05-27 13:54:07 -0700576 // vlan assignment is valid only if this instance is master
577 if (srManager.mastershipService.isLocalMaster(deviceId)) {
578 TrafficTreatment tt = DefaultTrafficTreatment.builder()
579 .pushVlan().setVlanId(assignedVlan).build();
580 fob.withMeta(tt);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700581 }
Saurav Das25190812016-05-27 13:54:07 -0700582 fob.permit().fromApp(srManager.appId);
583 log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port);
584 filteredPorts++;
585 ObjectiveContext context = new DefaultObjectiveContext(
586 (objective) -> log.debug("Filter for {} populated", connectPoint),
587 (objective, error) ->
588 log.warn("Failed to populate filter for {}: {}", connectPoint, error));
589 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Saurav Das0e99e2b2015-10-28 12:39:42 -0700590 }
Saurav Das25190812016-05-27 13:54:07 -0700591 log.info("Filtering on dev:{}, disabledPorts:{}, suppressedPorts:{}, filteredPorts:{}",
592 deviceId, disabledPorts, suppressedPorts, filteredPorts);
593 // XXX With this check, there is a chance that not all the ports that
594 // should be filtered actually get filtered as long as one of them does.
595 // Note there is no PORT_UPDATED event that makes the port go from disabled
596 // to enabled state, because the ports comes enabled from the switch.
597 // Check ONOS core, where the port becoming available and being declared
598 // enabled is possibly not atomic.
599 return (filteredPorts > 0) ? true : false;
sanghob35a6192015-04-01 13:05:26 -0700600 }
601
602 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700603 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700604 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700605 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700606 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700607 *
Saurav Das822c4e22015-10-23 10:51:11 -0700608 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700609 */
Saurav Das822c4e22015-10-23 10:51:11 -0700610 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800611 Ip4Address routerIp;
612 try {
613 routerIp = config.getRouterIp(deviceId);
614 } catch (DeviceConfigNotFoundException e) {
615 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
616 return;
617 }
618
Saurav Das837e0bb2015-10-30 17:45:38 -0700619 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
620 log.debug("Not installing port-IP punts - not the master for dev:{} ",
621 deviceId);
622 return;
623 }
Saurav Das822c4e22015-10-23 10:51:11 -0700624 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Charles Chan5270ed02016-01-30 23:22:37 -0800625 Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId));
Charles Chan0b4e6182015-11-03 10:42:14 -0800626 allIps.add(routerIp);
Saurav Das837e0bb2015-10-30 17:45:38 -0700627 for (Ip4Address ipaddr : allIps) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800628 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
629 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
630 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
631 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das822c4e22015-10-23 10:51:11 -0700632 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chan68aa62d2015-11-09 16:37:23 -0800633 tbuilder.setOutput(PortNumber.CONTROLLER);
634 puntIp.withSelector(sbuilder.build());
635 puntIp.withTreatment(tbuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700636 puntIp.withFlag(Flag.VERSATILE)
Charles Chan5270ed02016-01-30 23:22:37 -0800637 .withPriority(SegmentRoutingService.HIGHEST_PRIORITY)
Saurav Das822c4e22015-10-23 10:51:11 -0700638 .makePermanent()
639 .fromApp(srManager.appId);
Charles Chand2990362016-04-18 13:44:03 -0700640 ObjectiveContext context = new DefaultObjectiveContext(
641 (objective) -> log.debug("IP punt rule for {} populated", ipaddr),
642 (objective, error) ->
643 log.warn("Failed to populate IP punt rule for {}: {}", ipaddr, error));
644 srManager.flowObjectiveService.forward(deviceId, puntIp.add(context));
Saurav Das822c4e22015-10-23 10:51:11 -0700645 }
sanghob35a6192015-04-01 13:05:26 -0700646 }
647
Charles Chan68aa62d2015-11-09 16:37:23 -0800648 /**
649 * Populates a forwarding objective to send packets that miss other high
650 * priority Bridging Table entries to a group that contains all ports of
651 * its subnet.
652 *
653 * Note: We assume that packets sending from the edge switches to the hosts
654 * have untagged VLAN.
655 * The VLAN tag will be popped later in the flooding group.
656 *
657 * @param deviceId switch ID to set the rules
658 */
659 public void populateSubnetBroadcastRule(DeviceId deviceId) {
660 config.getSubnets(deviceId).forEach(subnet -> {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800661 if (subnet.prefixLength() == 0 ||
662 subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
663 return;
664 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800665 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
666 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
667
Saurav Das4ce45962015-11-24 23:21:05 -0800668 if (nextId < 0 || vlanId == null) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800669 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
670 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -0800671 return;
672 }
673
Charles Chan68aa62d2015-11-09 16:37:23 -0800674 /* Driver should treat objective with MacAddress.NONE as the
675 * subnet broadcast rule
676 */
677 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
678 sbuilder.matchVlanId(vlanId);
679 sbuilder.matchEthDst(MacAddress.NONE);
680
681 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
682 fob.withFlag(Flag.SPECIFIC)
683 .withSelector(sbuilder.build())
684 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800685 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800686 .fromApp(srManager.appId)
687 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700688 ObjectiveContext context = new DefaultObjectiveContext(
689 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
690 (objective, error) ->
691 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
692 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800693 });
694 }
695
Charles Chan5270ed02016-01-30 23:22:37 -0800696 private int getPriorityFromPrefix(IpPrefix prefix) {
697 return (prefix.isIp4()) ?
698 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
699 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700700 }
sanghob35a6192015-04-01 13:05:26 -0700701}