blob: 0ddffa4ff32e68e6ac6dec1af3f9fdd86c029426 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho80f11cb2015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Saurav Das4c35fc42015-11-20 15:27:53 -080018import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070019import org.onlab.packet.Ethernet;
20import org.onlab.packet.Ip4Address;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070021import org.onlab.packet.Ip4Prefix;
sangho80f11cb2015-04-01 13:05:26 -070022import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070024import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070025import org.onlab.packet.VlanId;
Charles Chanb7f75ac2016-01-11 18:28:54 -080026import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070027import org.onosproject.net.flowobjective.DefaultObjectiveContext;
28import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan319d1a22015-11-03 10:42:14 -080029import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
30import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chan370a65b2016-05-10 17:29:47 -070031import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070032import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sangho80f11cb2015-04-01 13:05:26 -070033import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070034import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070035import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070036import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070038import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-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 Das9f1c42e2015-10-23 10:51:11 -070046import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
50import java.util.ArrayList;
Saurav Dasc28b3432015-10-30 17:45:38 -070051import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070052import java.util.List;
Charles Chanb7f75ac2016-01-11 18:28:54 -080053import java.util.Map;
sangho80f11cb2015-04-01 13:05:26 -070054import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070055import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070056
57import static com.google.common.base.Preconditions.checkNotNull;
58
Charles Chanb7f75ac2016-01-11 18:28:54 -080059/**
60 * Populator of segment routing flow rules.
61 */
sangho80f11cb2015-04-01 13:05:26 -070062public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070063 private static final Logger log = LoggerFactory
64 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070065
sanghofb7c7292015-04-13 15:15:58 -070066 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070067 private SegmentRoutingManager srManager;
68 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070069
sangho80f11cb2015-04-01 13:05:26 -070070 /**
71 * Creates a RoutingRulePopulator object.
72 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070073 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070074 */
75 public RoutingRulePopulator(SegmentRoutingManager srManager) {
76 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070077 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-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 Vachuska7cfc6202015-04-30 18:13:25 -070090 *
91 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -070092 */
93 public long getCounter() {
94 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -070095 }
96
97 /**
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070098 * Populates IP flow rules for specific hosts directly connected to the
99 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700100 *
101 * @param deviceId switch ID to set the rules
102 * @param hostIp host IP address
103 * @param hostMac host MAC address
104 * @param outPort port where the host is connected
105 */
106 public void populateIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
107 MacAddress hostMac, PortNumber outPort) {
Charles Chanf4586112015-11-09 16:37:23 -0800108 log.debug("Populate IP table entry for host {} at {}:{}",
109 hostIp, deviceId, outPort);
110 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800111 try {
Charles Chanf4586112015-11-09 16:37:23 -0800112 fwdBuilder = getForwardingObjectiveBuilder(
113 deviceId, hostIp, hostMac, outPort);
Charles Chan319d1a22015-11-03 10:42:14 -0800114 } catch (DeviceConfigNotFoundException e) {
115 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
116 return;
117 }
Saurav Das07c74602016-04-27 18:35:50 -0700118 if (fwdBuilder == null) {
119 log.warn("Aborting host routing table entries due "
120 + "to error for dev:{} host:{}", deviceId, hostIp);
121 return;
122 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700123 ObjectiveContext context = new DefaultObjectiveContext(
124 (objective) -> log.debug("IP rule for host {} populated", hostIp),
125 (objective, error) ->
126 log.warn("Failed to populate IP rule for host {}: {}", hostIp, error));
127 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800128 rulePopulationCounter.incrementAndGet();
129 }
130
Charles Chanb7f75ac2016-01-11 18:28:54 -0800131 /**
132 * Removes IP rules for host when the host is gone.
133 *
134 * @param deviceId device ID of the device that host attaches to
135 * @param hostIp IP address of the host
136 * @param hostMac MAC address of the host
137 * @param outPort port that host attaches to
138 */
Charles Chanf4586112015-11-09 16:37:23 -0800139 public void revokeIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
140 MacAddress hostMac, PortNumber outPort) {
141 log.debug("Revoke IP table entry for host {} at {}:{}",
142 hostIp, deviceId, outPort);
143 ForwardingObjective.Builder fwdBuilder;
144 try {
145 fwdBuilder = getForwardingObjectiveBuilder(
146 deviceId, hostIp, hostMac, outPort);
147 } catch (DeviceConfigNotFoundException e) {
148 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
149 return;
150 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700151 ObjectiveContext context = new DefaultObjectiveContext(
152 (objective) -> log.debug("IP rule for host {} revoked", hostIp),
153 (objective, error) ->
154 log.warn("Failed to revoke IP rule for host {}: {}", hostIp, error));
155 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800156 }
157
158 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
159 DeviceId deviceId, Ip4Address hostIp,
160 MacAddress hostMac, PortNumber outPort)
161 throws DeviceConfigNotFoundException {
162 MacAddress deviceMac;
163 deviceMac = config.getDeviceMac(deviceId);
Charles Chan82ab1932016-01-30 23:22:37 -0800164 int priority;
Charles Chan319d1a22015-11-03 10:42:14 -0800165
sangho80f11cb2015-04-01 13:05:26 -0700166 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
167 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
168
sangho80f11cb2015-04-01 13:05:26 -0700169 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan82ab1932016-01-30 23:22:37 -0800170 // Special case for default route
171 if (hostIp.isZero()) {
172 sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, 0));
173 priority = SegmentRoutingService.MIN_IP_PRIORITY;
174 } else {
175 Ip4Prefix hostIpPrefix = Ip4Prefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH);
176 sbuilder.matchIPDst(hostIpPrefix);
177 priority = getPriorityFromPrefix(hostIpPrefix);
178 }
Saurav Das2d94d312015-11-24 23:21:05 -0800179 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700180
sangho27462c62015-05-14 00:39:53 -0700181 tbuilder.deferred()
182 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800183 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700184 .setOutput(outPort);
sangho80f11cb2015-04-01 13:05:26 -0700185 TrafficTreatment treatment = tbuilder.build();
Saurav Das2d94d312015-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 Das07c74602016-04-27 18:35:50 -0700200 if (portNextObjId == -1) {
201 // warning log will come from getPortNextObjective method
202 return null;
203 }
Charles Chanf4586112015-11-09 16:37:23 -0800204 return DefaultForwardingObjective.builder()
Saurav Das2d94d312015-11-24 23:21:05 -0800205 .withSelector(selector)
206 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800207 .fromApp(srManager.appId).makePermanent()
Charles Chan82ab1932016-01-30 23:22:37 -0800208 .withPriority(priority)
209 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-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 Chanc22cef32016-04-29 14:38:22 -0700216 * @param subnets subnet being added
sangho80f11cb2015-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 Chanc22cef32016-04-29 14:38:22 -0700221 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<Ip4Prefix> subnets,
222 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700223 for (IpPrefix subnet : subnets) {
sangho80f11cb2015-04-01 13:05:26 -0700224 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
225 return false;
226 }
227 }
Charles Chanc22cef32016-04-29 14:38:22 -0700228 return true;
229 }
sangho80f11cb2015-04-01 13:05:26 -0700230
Charles Chanc22cef32016-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 }
sangho80f11cb2015-04-01 13:05:26 -0700243 return true;
244 }
245
246 /**
247 * Populates IP flow rules for the router IP address.
248 *
Saurav Das88979182015-10-19 14:37:36 -0700249 * @param deviceId target device ID to set the rules
sangho80f11cb2015-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 Vavilapalli64505482015-04-21 13:04:13 -0700255 public boolean populateIpRuleForRouter(DeviceId deviceId,
256 IpPrefix ipPrefix, DeviceId destSw,
257 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-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 }
sangho80f11cb2015-04-01 13:05:26 -0700265
266 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan3ef31e72016-02-05 13:33:54 -0800267 sbuilder.matchIPDst(ipPrefix);
sangho80f11cb2015-04-01 13:05:26 -0700268 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chanf4586112015-11-09 16:37:23 -0800269 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700270
Charles Chanf4586112015-11-09 16:37:23 -0800271 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
272 NeighborSet ns;
273 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700274
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700275 // If the next hop is the same as the final destination, then MPLS label
276 // is not set.
sangho80f11cb2015-04-01 13:05:26 -0700277 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800278 tbuilder.immediate().decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700279 ns = new NeighborSet(nextHops);
Charles Chanf4586112015-11-09 16:37:23 -0800280 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700281 } else {
Charles Chan319d1a22015-11-03 10:42:14 -0800282 ns = new NeighborSet(nextHops, segmentId);
Charles Chanf4586112015-11-09 16:37:23 -0800283 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700284 }
285
Saurav Das4c35fc42015-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) {
sangho2165d222015-05-01 09:38:25 -0700295 log.warn("No next objective in {} for ns: {}", deviceId, ns);
296 return false;
297 }
298
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700299 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
300 .builder()
301 .fromApp(srManager.appId)
302 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800303 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700304 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800305 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700306 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800307 if (treatment != null) {
308 fwdBuilder.withTreatment(treatment);
309 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700310 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700311 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700312 ipPrefix,
313 deviceId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700314 ObjectiveContext context = new DefaultObjectiveContext(
315 (objective) -> log.debug("IP rule for router {} populated", ipPrefix),
316 (objective, error) ->
317 log.warn("Failed to populate IP rule for router {}: {}", ipPrefix, error));
318 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700319 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700320
321 return true;
322 }
323
sangho80f11cb2015-04-01 13:05:26 -0700324 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700325 * Revokes IP flow rules for the router IP address.
326 *
327 * @param ipPrefix the IP address of the destination router
328 * @return true if all rules are removed successfully, false otherwise
329 */
330 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
331 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
332 sbuilder.matchIPDst(ipPrefix);
333 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
334 TrafficSelector selector = sbuilder.build();
335 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
336
337 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
338 .builder()
339 .fromApp(srManager.appId)
340 .makePermanent()
341 .withSelector(selector)
342 .withTreatment(dummyTreatment)
343 .withPriority(getPriorityFromPrefix(ipPrefix))
344 .withFlag(ForwardingObjective.Flag.SPECIFIC);
345
346 ObjectiveContext context = new DefaultObjectiveContext(
347 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
348 (objective, error) ->
349 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
350
351 srManager.deviceService.getAvailableDevices().forEach(device -> {
352 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
353 });
354
355 return true;
356 }
357
358 /**
Saurav Das88979182015-10-19 14:37:36 -0700359 * Populates MPLS flow rules to all routers.
sangho80f11cb2015-04-01 13:05:26 -0700360 *
Saurav Das88979182015-10-19 14:37:36 -0700361 * @param deviceId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700362 * @param destSwId destination switch device ID
363 * @param nextHops next hops switch ID list
364 * @return true if all rules are set successfully, false otherwise
365 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700366 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
367 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800368 int segmentId;
369 try {
370 segmentId = config.getSegmentId(destSwId);
371 } catch (DeviceConfigNotFoundException e) {
372 log.warn(e.getMessage() + " Aborting populateMplsRule.");
373 return false;
374 }
sangho80f11cb2015-04-01 13:05:26 -0700375
376 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -0700377 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700378
379 // TODO Handle the case of Bos == false
sangho80f11cb2015-04-01 13:05:26 -0700380 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das4c35fc42015-11-20 15:27:53 -0800381 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chande6655c2015-12-23 00:15:11 -0800382 sbuilder.matchMplsBos(true);
Saurav Das4c35fc42015-11-20 15:27:53 -0800383 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700384
Saurav Das4c35fc42015-11-20 15:27:53 -0800385 // setup metadata to pass to nextObjective - indicate the vlan on egress
386 // if needed by the switch pipeline. Since mpls next-hops are always to
387 // other neighboring routers, there is no subnet assigned on those ports.
388 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
389 metabuilder.matchVlanId(
390 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
391
392 // If the next hop is the destination router for the segment, do pop
sangho80f11cb2015-04-01 13:05:26 -0700393 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700394 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800395 + "label {} in switch {} with pop", segmentId, deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700396
Saurav Das4c35fc42015-11-20 15:27:53 -0800397 // bos pop case (php)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700398 ForwardingObjective.Builder fwdObjBosBuilder =
399 getMplsForwardingObjective(deviceId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700400 nextHops,
401 true,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700402 true,
Saurav Das4c35fc42015-11-20 15:27:53 -0800403 metabuilder.build());
404 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700405 return false;
406 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800407 fwdObjBuilders.add(fwdObjBosBuilder);
408
409 // XXX not-bos pop case, SR app multi-label not implemented yet
410 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
411 getMplsForwardingObjective(deviceId,
412 nextHops,
413 true,
414 false);*/
415
sangho80f11cb2015-04-01 13:05:26 -0700416 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800417 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700418 log.debug("Installing MPLS forwarding objective for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800419 + "label {} in switch {} without pop", segmentId, deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700420
Saurav Das4c35fc42015-11-20 15:27:53 -0800421 // continue case with bos - this does get triggered in edge routers
422 // and in core routers - driver can handle depending on availability
423 // of MPLS ECMP or not
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700424 ForwardingObjective.Builder fwdObjBosBuilder =
425 getMplsForwardingObjective(deviceId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700426 nextHops,
427 false,
Saurav Das4c35fc42015-11-20 15:27:53 -0800428 true,
429 metabuilder.build());
430 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700431 return false;
432 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800433 fwdObjBuilders.add(fwdObjBosBuilder);
434
435 // XXX continue case with not-bos - SR app multi label not implemented yet
436 // also requires MPLS ECMP
437 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
438 getMplsForwardingObjective(deviceId,
439 nextHops,
440 false,
441 false); */
442
sangho80f11cb2015-04-01 13:05:26 -0700443 }
444
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700445 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
446 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
447 .makePermanent()).withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800448 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700449 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan1eaf4802016-04-18 13:44:03 -0700450 ObjectiveContext context = new DefaultObjectiveContext(
451 (objective) -> log.debug("MPLS rule for SID {} populated", segmentId),
452 (objective, error) ->
453 log.warn("Failed to populate MPLS rule for SID {}: {}", segmentId, error));
454 srManager.flowObjectiveService.forward(deviceId, fwdObjBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700455 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700456 }
457
458 return true;
459 }
460
Saurav Das4c35fc42015-11-20 15:27:53 -0800461 private ForwardingObjective.Builder getMplsForwardingObjective(
462 DeviceId deviceId,
463 Set<DeviceId> nextHops,
464 boolean phpRequired,
465 boolean isBos,
466 TrafficSelector meta) {
467
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700468 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
469 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700470
471 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
472
473 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800474 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700475 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700476 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700477 if (isBos) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800478 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
479 .decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700480 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800481 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
482 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700483 }
484 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800485 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700486 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700487 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700488 }
489
Saurav Das4c35fc42015-11-20 15:27:53 -0800490 // All forwarding is via ECMP group, the metadata informs the driver
491 // that the next-Objective will be used by MPLS flows. In other words,
492 // MPLS ECMP is requested. It is up to the driver to decide if these
493 // packets will be hashed or not.
494 fwdBuilder.withTreatment(tbuilder.build());
495 NeighborSet ns = new NeighborSet(nextHops);
496 log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}",
497 deviceId, ns);
498
499 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
500 if (nextId <= 0) {
501 log.warn("No next objective in {} for ns: {}", deviceId, ns);
502 return null;
sangho80f11cb2015-04-01 13:05:26 -0700503 }
504
Saurav Das4c35fc42015-11-20 15:27:53 -0800505 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700506 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700507 }
508
509 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700510 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700511 * dstMac corresponding to the router's MAC address. For those pipelines
512 * that need to internally assign vlans to untagged packets, this method
513 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700514 * <p>
515 * Note that the vlan assignment is only done by the master-instance for a switch.
516 * However we send the filtering objective from slave-instances as well, so
517 * that drivers can obtain other information (like Router MAC and IP).
sangho80f11cb2015-04-01 13:05:26 -0700518 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700519 * @param deviceId the switch dpid for the router
Charles Chanc22cef32016-04-29 14:38:22 -0700520 * @return true if operation succeeds
sangho80f11cb2015-04-01 13:05:26 -0700521 */
Saurav Das07c74602016-04-27 18:35:50 -0700522 public boolean populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700523 log.debug("Installing per-port filtering objective for untagged "
524 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800525
526 MacAddress deviceMac;
527 try {
528 deviceMac = config.getDeviceMac(deviceId);
529 } catch (DeviceConfigNotFoundException e) {
530 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
Saurav Das07c74602016-04-27 18:35:50 -0700531 return false;
Charles Chan319d1a22015-11-03 10:42:14 -0800532 }
533
Saurav Das07c74602016-04-27 18:35:50 -0700534 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
535 if (devPorts != null && devPorts.size() == 0) {
536 log.warn("Device {} ports not available. Unable to add MacVlan filters",
537 deviceId);
538 return false;
539 }
540
541 for (Port port : devPorts) {
Charles Chan1eaf4802016-04-18 13:44:03 -0700542 ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number());
Charles Chan43547ca2016-02-10 20:46:58 -0800543 // TODO: Handles dynamic port events when we are ready for dynamic config
Charles Chan370a65b2016-05-10 17:29:47 -0700544 SegmentRoutingAppConfig appConfig = srManager.cfgService
545 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
546 if ((appConfig == null || !appConfig.suppressSubnet().contains(connectPoint)) &&
Charles Chan82ab1932016-01-30 23:22:37 -0800547 port.isEnabled()) {
Saurav Das7c305372015-10-28 12:39:42 -0700548 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
549 VlanId assignedVlan = (portSubnet == null)
550 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
551 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan319d1a22015-11-03 10:42:14 -0800552
Saurav Das7c305372015-10-28 12:39:42 -0700553 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
554 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan319d1a22015-11-03 10:42:14 -0800555 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chanb7f75ac2016-01-11 18:28:54 -0800556 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan82ab1932016-01-30 23:22:37 -0800557 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Dasc28b3432015-10-30 17:45:38 -0700558 // vlan assignment is valid only if this instance is master
559 if (srManager.mastershipService.isLocalMaster(deviceId)) {
560 TrafficTreatment tt = DefaultTrafficTreatment.builder()
561 .pushVlan().setVlanId(assignedVlan).build();
Saurav Das2d94d312015-11-24 23:21:05 -0800562 fob.withMeta(tt);
Saurav Dasc28b3432015-10-30 17:45:38 -0700563 }
Saurav Das7c305372015-10-28 12:39:42 -0700564 fob.permit().fromApp(srManager.appId);
Saurav Das07c74602016-04-27 18:35:50 -0700565 log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port);
Charles Chan1eaf4802016-04-18 13:44:03 -0700566 ObjectiveContext context = new DefaultObjectiveContext(
567 (objective) -> log.debug("Filter for {} populated", connectPoint),
568 (objective, error) ->
569 log.warn("Failed to populate filter for {}: {}", connectPoint, error));
570 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Saurav Das7c305372015-10-28 12:39:42 -0700571 }
572 }
Saurav Das07c74602016-04-27 18:35:50 -0700573 return true;
sangho80f11cb2015-04-01 13:05:26 -0700574 }
575
576 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700577 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700578 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700579 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700580 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700581 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700582 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700583 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700584 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan319d1a22015-11-03 10:42:14 -0800585 Ip4Address routerIp;
586 try {
587 routerIp = config.getRouterIp(deviceId);
588 } catch (DeviceConfigNotFoundException e) {
589 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
590 return;
591 }
592
Saurav Dasc28b3432015-10-30 17:45:38 -0700593 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
594 log.debug("Not installing port-IP punts - not the master for dev:{} ",
595 deviceId);
596 return;
597 }
Saurav Das9f1c42e2015-10-23 10:51:11 -0700598 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Charles Chan82ab1932016-01-30 23:22:37 -0800599 Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId));
Charles Chan319d1a22015-11-03 10:42:14 -0800600 allIps.add(routerIp);
Saurav Dasc28b3432015-10-30 17:45:38 -0700601 for (Ip4Address ipaddr : allIps) {
Charles Chanf4586112015-11-09 16:37:23 -0800602 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
603 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
604 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
605 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das9f1c42e2015-10-23 10:51:11 -0700606 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chanf4586112015-11-09 16:37:23 -0800607 tbuilder.setOutput(PortNumber.CONTROLLER);
608 puntIp.withSelector(sbuilder.build());
609 puntIp.withTreatment(tbuilder.build());
Saurav Das9f1c42e2015-10-23 10:51:11 -0700610 puntIp.withFlag(Flag.VERSATILE)
Charles Chan82ab1932016-01-30 23:22:37 -0800611 .withPriority(SegmentRoutingService.HIGHEST_PRIORITY)
Saurav Das9f1c42e2015-10-23 10:51:11 -0700612 .makePermanent()
613 .fromApp(srManager.appId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700614 ObjectiveContext context = new DefaultObjectiveContext(
615 (objective) -> log.debug("IP punt rule for {} populated", ipaddr),
616 (objective, error) ->
617 log.warn("Failed to populate IP punt rule for {}: {}", ipaddr, error));
618 srManager.flowObjectiveService.forward(deviceId, puntIp.add(context));
Saurav Das9f1c42e2015-10-23 10:51:11 -0700619 }
sangho80f11cb2015-04-01 13:05:26 -0700620 }
621
Charles Chanf4586112015-11-09 16:37:23 -0800622 /**
623 * Populates a forwarding objective to send packets that miss other high
624 * priority Bridging Table entries to a group that contains all ports of
625 * its subnet.
626 *
627 * Note: We assume that packets sending from the edge switches to the hosts
628 * have untagged VLAN.
629 * The VLAN tag will be popped later in the flooding group.
630 *
631 * @param deviceId switch ID to set the rules
632 */
633 public void populateSubnetBroadcastRule(DeviceId deviceId) {
634 config.getSubnets(deviceId).forEach(subnet -> {
Charles Chanbbd004c2016-02-16 23:14:49 -0800635 if (subnet.prefixLength() == 0 ||
636 subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
637 return;
638 }
Charles Chanf4586112015-11-09 16:37:23 -0800639 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
640 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
641
Saurav Das2d94d312015-11-24 23:21:05 -0800642 if (nextId < 0 || vlanId == null) {
Charles Chanbbd004c2016-02-16 23:14:49 -0800643 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
644 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das2d94d312015-11-24 23:21:05 -0800645 return;
646 }
647
Charles Chanf4586112015-11-09 16:37:23 -0800648 /* Driver should treat objective with MacAddress.NONE as the
649 * subnet broadcast rule
650 */
651 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
652 sbuilder.matchVlanId(vlanId);
653 sbuilder.matchEthDst(MacAddress.NONE);
654
655 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
656 fob.withFlag(Flag.SPECIFIC)
657 .withSelector(sbuilder.build())
658 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800659 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chanf4586112015-11-09 16:37:23 -0800660 .fromApp(srManager.appId)
661 .makePermanent();
Charles Chan1eaf4802016-04-18 13:44:03 -0700662 ObjectiveContext context = new DefaultObjectiveContext(
663 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
664 (objective, error) ->
665 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
666 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800667 });
668 }
669
Charles Chanb7f75ac2016-01-11 18:28:54 -0800670 /**
671 * Creates a filtering objective to permit VLAN cross-connect traffic.
672 *
673 * @param deviceId the DPID of the switch
674 */
675 public void populateXConnectVlanFilters(DeviceId deviceId) {
676 Map<VlanId, List<ConnectPoint>> xConnectsForDevice =
677 config.getXConnects();
678 xConnectsForDevice.forEach((vlanId, connectPoints) -> {
679 // Only proceed the xConnect for given device
680 for (ConnectPoint connectPoint : connectPoints) {
681 if (!connectPoint.deviceId().equals(deviceId)) {
682 return;
683 }
684 }
685
686 connectPoints.forEach(connectPoint -> {
687 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
688 fob.withKey(Criteria.matchInPort(connectPoint.port()))
689 .addCondition(Criteria.matchVlanId(vlanId))
690 .addCondition(Criteria.matchEthDst(MacAddress.NONE))
Charles Chan82ab1932016-01-30 23:22:37 -0800691 .withPriority(SegmentRoutingService.XCONNECT_PRIORITY);
Charles Chanb7f75ac2016-01-11 18:28:54 -0800692 fob.permit().fromApp(srManager.appId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700693 ObjectiveContext context = new DefaultObjectiveContext(
694 (objective) -> log.debug("XConnect filter for {} populated", connectPoint),
695 (objective, error) ->
696 log.warn("Failed to populate xconnect filter for {}: {}", connectPoint, error));
697 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Charles Chanb7f75ac2016-01-11 18:28:54 -0800698 });
699 });
700 }
701
702 /**
703 * Populates a forwarding objective that points the VLAN cross-connect
704 * packets to a broadcast group.
705 *
706 * @param deviceId switch ID to set the rules
707 */
708 public void populateXConnectBroadcastRule(DeviceId deviceId) {
709 Map<VlanId, List<ConnectPoint>> xConnects =
710 config.getXConnects();
711 xConnects.forEach((vlanId, connectPoints) -> {
712 // Only proceed the xConnect for given device
713 for (ConnectPoint connectPoint : connectPoints) {
714 if (!connectPoint.deviceId().equals(deviceId)) {
715 return;
716 }
717 }
718
719 int nextId = srManager.getXConnectNextObjectiveId(deviceId, vlanId);
720 if (nextId < 0) {
721 log.error("Cannot install cross-connect broadcast rule in dev:{} " +
722 "due to missing nextId:{}", deviceId, nextId);
723 return;
724 }
725
726 /*
727 * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE
728 * as the VLAN cross-connect broadcast rules
729 */
730 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
731 sbuilder.matchVlanId(vlanId);
732 sbuilder.matchEthDst(MacAddress.NONE);
733
734 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
735 fob.withFlag(Flag.SPECIFIC)
736 .withSelector(sbuilder.build())
737 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800738 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)
Charles Chanb7f75ac2016-01-11 18:28:54 -0800739 .fromApp(srManager.appId)
740 .makePermanent();
Charles Chan1eaf4802016-04-18 13:44:03 -0700741 ObjectiveContext context = new DefaultObjectiveContext(
742 (objective) -> log.debug("XConnect rule for {} populated", xConnects),
743 (objective, error) ->
744 log.warn("Failed to populate xconnect rule for {}: {}", xConnects, error));
745 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chanb7f75ac2016-01-11 18:28:54 -0800746 });
747 }
Charles Chanf4586112015-11-09 16:37:23 -0800748
Charles Chan82ab1932016-01-30 23:22:37 -0800749 private int getPriorityFromPrefix(IpPrefix prefix) {
750 return (prefix.isIp4()) ?
751 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
752 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700753 }
sangho80f11cb2015-04-01 13:05:26 -0700754}