blob: e2a040a49cbaf9c8057abe4bc0fda5adfffba154 [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;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070031import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sangho80f11cb2015-04-01 13:05:26 -070032import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070033import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070034import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070035import org.onosproject.net.flow.DefaultTrafficSelector;
36import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070037import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070039import org.onosproject.net.flow.criteria.Criteria;
40import org.onosproject.net.flowobjective.DefaultFilteringObjective;
41import org.onosproject.net.flowobjective.DefaultForwardingObjective;
42import org.onosproject.net.flowobjective.FilteringObjective;
43import org.onosproject.net.flowobjective.ForwardingObjective;
44import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070045import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
49import java.util.ArrayList;
Saurav Dasc28b3432015-10-30 17:45:38 -070050import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070051import java.util.List;
Charles Chanb7f75ac2016-01-11 18:28:54 -080052import java.util.Map;
sangho80f11cb2015-04-01 13:05:26 -070053import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070054import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070055
56import static com.google.common.base.Preconditions.checkNotNull;
57
Charles Chanb7f75ac2016-01-11 18:28:54 -080058/**
59 * Populator of segment routing flow rules.
60 */
sangho80f11cb2015-04-01 13:05:26 -070061public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070062 private static final Logger log = LoggerFactory
63 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070064
sanghofb7c7292015-04-13 15:15:58 -070065 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070066 private SegmentRoutingManager srManager;
67 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070068
sangho80f11cb2015-04-01 13:05:26 -070069 /**
70 * Creates a RoutingRulePopulator object.
71 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070072 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070073 */
74 public RoutingRulePopulator(SegmentRoutingManager srManager) {
75 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070076 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-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 Vachuska7cfc6202015-04-30 18:13:25 -070089 *
90 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -070091 */
92 public long getCounter() {
93 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -070094 }
95
96 /**
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070097 * Populates IP flow rules for specific hosts directly connected to the
98 * switch.
sangho80f11cb2015-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 Chanf4586112015-11-09 16:37:23 -0800107 log.debug("Populate IP table entry for host {} at {}:{}",
108 hostIp, deviceId, outPort);
109 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800110 try {
Charles Chanf4586112015-11-09 16:37:23 -0800111 fwdBuilder = getForwardingObjectiveBuilder(
112 deviceId, hostIp, hostMac, outPort);
Charles Chan319d1a22015-11-03 10:42:14 -0800113 } catch (DeviceConfigNotFoundException e) {
114 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
115 return;
116 }
Saurav Das07c74602016-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 Chan1eaf4802016-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 Chanf4586112015-11-09 16:37:23 -0800127 rulePopulationCounter.incrementAndGet();
128 }
129
Charles Chanb7f75ac2016-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 Chanf4586112015-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 Chan1eaf4802016-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 Chanf4586112015-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 Chan82ab1932016-01-30 23:22:37 -0800163 int priority;
Charles Chan319d1a22015-11-03 10:42:14 -0800164
sangho80f11cb2015-04-01 13:05:26 -0700165 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
166 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
167
sangho80f11cb2015-04-01 13:05:26 -0700168 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan82ab1932016-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 Das2d94d312015-11-24 23:21:05 -0800178 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700179
sangho27462c62015-05-14 00:39:53 -0700180 tbuilder.deferred()
181 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800182 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700183 .setOutput(outPort);
sangho80f11cb2015-04-01 13:05:26 -0700184 TrafficTreatment treatment = tbuilder.build();
Saurav Das2d94d312015-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 Das07c74602016-04-27 18:35:50 -0700199 if (portNextObjId == -1) {
200 // warning log will come from getPortNextObjective method
201 return null;
202 }
Charles Chanf4586112015-11-09 16:37:23 -0800203 return DefaultForwardingObjective.builder()
Saurav Das2d94d312015-11-24 23:21:05 -0800204 .withSelector(selector)
205 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800206 .fromApp(srManager.appId).makePermanent()
Charles Chan82ab1932016-01-30 23:22:37 -0800207 .withPriority(priority)
208 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-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
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700215 * @param subnets subnet information
sangho80f11cb2015-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 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700220 public boolean populateIpRuleForSubnet(DeviceId deviceId,
Charles Chanc6ad7752015-10-29 14:58:10 -0700221 Set<Ip4Prefix> subnets,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700222 DeviceId destSw,
223 Set<DeviceId> nextHops) {
sangho80f11cb2015-04-01 13:05:26 -0700224
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700225 for (IpPrefix subnet : subnets) {
sangho80f11cb2015-04-01 13:05:26 -0700226 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
227 return false;
228 }
229 }
230
231 return true;
232 }
233
234 /**
235 * Populates IP flow rules for the router IP address.
236 *
Saurav Das88979182015-10-19 14:37:36 -0700237 * @param deviceId target device ID to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700238 * @param ipPrefix the IP address of the destination router
239 * @param destSw device ID of the destination router
240 * @param nextHops next hop switch ID list
241 * @return true if all rules are set successfully, false otherwise
242 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700243 public boolean populateIpRuleForRouter(DeviceId deviceId,
244 IpPrefix ipPrefix, DeviceId destSw,
245 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800246 int segmentId;
247 try {
248 segmentId = config.getSegmentId(destSw);
249 } catch (DeviceConfigNotFoundException e) {
250 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
251 return false;
252 }
sangho80f11cb2015-04-01 13:05:26 -0700253
254 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan3ef31e72016-02-05 13:33:54 -0800255 sbuilder.matchIPDst(ipPrefix);
sangho80f11cb2015-04-01 13:05:26 -0700256 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chanf4586112015-11-09 16:37:23 -0800257 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700258
Charles Chanf4586112015-11-09 16:37:23 -0800259 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
260 NeighborSet ns;
261 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700262
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700263 // If the next hop is the same as the final destination, then MPLS label
264 // is not set.
sangho80f11cb2015-04-01 13:05:26 -0700265 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800266 tbuilder.immediate().decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700267 ns = new NeighborSet(nextHops);
Charles Chanf4586112015-11-09 16:37:23 -0800268 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700269 } else {
Charles Chan319d1a22015-11-03 10:42:14 -0800270 ns = new NeighborSet(nextHops, segmentId);
Charles Chanf4586112015-11-09 16:37:23 -0800271 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700272 }
273
Saurav Das4c35fc42015-11-20 15:27:53 -0800274 // setup metadata to pass to nextObjective - indicate the vlan on egress
275 // if needed by the switch pipeline. Since neighbor sets are always to
276 // other neighboring routers, there is no subnet assigned on those ports.
277 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
278 metabuilder.matchVlanId(
279 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
280
281 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
282 if (nextId <= 0) {
sangho2165d222015-05-01 09:38:25 -0700283 log.warn("No next objective in {} for ns: {}", deviceId, ns);
284 return false;
285 }
286
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700287 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
288 .builder()
289 .fromApp(srManager.appId)
290 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800291 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700292 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800293 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700294 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800295 if (treatment != null) {
296 fwdBuilder.withTreatment(treatment);
297 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700298 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700299 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700300 ipPrefix,
301 deviceId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700302 ObjectiveContext context = new DefaultObjectiveContext(
303 (objective) -> log.debug("IP rule for router {} populated", ipPrefix),
304 (objective, error) ->
305 log.warn("Failed to populate IP rule for router {}: {}", ipPrefix, error));
306 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700307 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700308
309 return true;
310 }
311
sangho80f11cb2015-04-01 13:05:26 -0700312 /**
Saurav Das88979182015-10-19 14:37:36 -0700313 * Populates MPLS flow rules to all routers.
sangho80f11cb2015-04-01 13:05:26 -0700314 *
Saurav Das88979182015-10-19 14:37:36 -0700315 * @param deviceId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700316 * @param destSwId destination switch device ID
317 * @param nextHops next hops switch ID list
318 * @return true if all rules are set successfully, false otherwise
319 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700320 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
321 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800322 int segmentId;
323 try {
324 segmentId = config.getSegmentId(destSwId);
325 } catch (DeviceConfigNotFoundException e) {
326 log.warn(e.getMessage() + " Aborting populateMplsRule.");
327 return false;
328 }
sangho80f11cb2015-04-01 13:05:26 -0700329
330 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -0700331 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700332
333 // TODO Handle the case of Bos == false
sangho80f11cb2015-04-01 13:05:26 -0700334 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das4c35fc42015-11-20 15:27:53 -0800335 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chande6655c2015-12-23 00:15:11 -0800336 sbuilder.matchMplsBos(true);
Saurav Das4c35fc42015-11-20 15:27:53 -0800337 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700338
Saurav Das4c35fc42015-11-20 15:27:53 -0800339 // setup metadata to pass to nextObjective - indicate the vlan on egress
340 // if needed by the switch pipeline. Since mpls next-hops are always to
341 // other neighboring routers, there is no subnet assigned on those ports.
342 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
343 metabuilder.matchVlanId(
344 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
345
346 // If the next hop is the destination router for the segment, do pop
sangho80f11cb2015-04-01 13:05:26 -0700347 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700348 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800349 + "label {} in switch {} with pop", segmentId, deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700350
Saurav Das4c35fc42015-11-20 15:27:53 -0800351 // bos pop case (php)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700352 ForwardingObjective.Builder fwdObjBosBuilder =
353 getMplsForwardingObjective(deviceId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700354 nextHops,
355 true,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700356 true,
Saurav Das4c35fc42015-11-20 15:27:53 -0800357 metabuilder.build());
358 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700359 return false;
360 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800361 fwdObjBuilders.add(fwdObjBosBuilder);
362
363 // XXX not-bos pop case, SR app multi-label not implemented yet
364 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
365 getMplsForwardingObjective(deviceId,
366 nextHops,
367 true,
368 false);*/
369
sangho80f11cb2015-04-01 13:05:26 -0700370 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800371 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700372 log.debug("Installing MPLS forwarding objective for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800373 + "label {} in switch {} without pop", segmentId, deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700374
Saurav Das4c35fc42015-11-20 15:27:53 -0800375 // continue case with bos - this does get triggered in edge routers
376 // and in core routers - driver can handle depending on availability
377 // of MPLS ECMP or not
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700378 ForwardingObjective.Builder fwdObjBosBuilder =
379 getMplsForwardingObjective(deviceId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700380 nextHops,
381 false,
Saurav Das4c35fc42015-11-20 15:27:53 -0800382 true,
383 metabuilder.build());
384 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700385 return false;
386 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800387 fwdObjBuilders.add(fwdObjBosBuilder);
388
389 // XXX continue case with not-bos - SR app multi label not implemented yet
390 // also requires MPLS ECMP
391 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
392 getMplsForwardingObjective(deviceId,
393 nextHops,
394 false,
395 false); */
396
sangho80f11cb2015-04-01 13:05:26 -0700397 }
398
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700399 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
400 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
401 .makePermanent()).withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800402 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700403 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan1eaf4802016-04-18 13:44:03 -0700404 ObjectiveContext context = new DefaultObjectiveContext(
405 (objective) -> log.debug("MPLS rule for SID {} populated", segmentId),
406 (objective, error) ->
407 log.warn("Failed to populate MPLS rule for SID {}: {}", segmentId, error));
408 srManager.flowObjectiveService.forward(deviceId, fwdObjBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700409 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700410 }
411
412 return true;
413 }
414
Saurav Das4c35fc42015-11-20 15:27:53 -0800415 private ForwardingObjective.Builder getMplsForwardingObjective(
416 DeviceId deviceId,
417 Set<DeviceId> nextHops,
418 boolean phpRequired,
419 boolean isBos,
420 TrafficSelector meta) {
421
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700422 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
423 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700424
425 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
426
427 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800428 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700429 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700430 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700431 if (isBos) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800432 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
433 .decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700434 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800435 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
436 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700437 }
438 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800439 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700440 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700441 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700442 }
443
Saurav Das4c35fc42015-11-20 15:27:53 -0800444 // All forwarding is via ECMP group, the metadata informs the driver
445 // that the next-Objective will be used by MPLS flows. In other words,
446 // MPLS ECMP is requested. It is up to the driver to decide if these
447 // packets will be hashed or not.
448 fwdBuilder.withTreatment(tbuilder.build());
449 NeighborSet ns = new NeighborSet(nextHops);
450 log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}",
451 deviceId, ns);
452
453 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
454 if (nextId <= 0) {
455 log.warn("No next objective in {} for ns: {}", deviceId, ns);
456 return null;
sangho80f11cb2015-04-01 13:05:26 -0700457 }
458
Saurav Das4c35fc42015-11-20 15:27:53 -0800459 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700460 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700461 }
462
463 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700464 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700465 * dstMac corresponding to the router's MAC address. For those pipelines
466 * that need to internally assign vlans to untagged packets, this method
467 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700468 * <p>
469 * Note that the vlan assignment is only done by the master-instance for a switch.
470 * However we send the filtering objective from slave-instances as well, so
471 * that drivers can obtain other information (like Router MAC and IP).
sangho80f11cb2015-04-01 13:05:26 -0700472 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700473 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700474 */
Saurav Das07c74602016-04-27 18:35:50 -0700475 public boolean populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700476 log.debug("Installing per-port filtering objective for untagged "
477 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800478
479 MacAddress deviceMac;
480 try {
481 deviceMac = config.getDeviceMac(deviceId);
482 } catch (DeviceConfigNotFoundException e) {
483 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
Saurav Das07c74602016-04-27 18:35:50 -0700484 return false;
Charles Chan319d1a22015-11-03 10:42:14 -0800485 }
486
Saurav Das07c74602016-04-27 18:35:50 -0700487 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
488 if (devPorts != null && devPorts.size() == 0) {
489 log.warn("Device {} ports not available. Unable to add MacVlan filters",
490 deviceId);
491 return false;
492 }
493
494 for (Port port : devPorts) {
Charles Chan1eaf4802016-04-18 13:44:03 -0700495 ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number());
Charles Chan43547ca2016-02-10 20:46:58 -0800496 // TODO: Handles dynamic port events when we are ready for dynamic config
Charles Chan1eaf4802016-04-18 13:44:03 -0700497 if (!srManager.deviceConfiguration.suppressSubnet().contains(connectPoint) &&
Charles Chan82ab1932016-01-30 23:22:37 -0800498 port.isEnabled()) {
Saurav Das7c305372015-10-28 12:39:42 -0700499 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
500 VlanId assignedVlan = (portSubnet == null)
501 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
502 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan319d1a22015-11-03 10:42:14 -0800503
Saurav Das7c305372015-10-28 12:39:42 -0700504 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
505 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan319d1a22015-11-03 10:42:14 -0800506 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chanb7f75ac2016-01-11 18:28:54 -0800507 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan82ab1932016-01-30 23:22:37 -0800508 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Dasc28b3432015-10-30 17:45:38 -0700509 // vlan assignment is valid only if this instance is master
510 if (srManager.mastershipService.isLocalMaster(deviceId)) {
511 TrafficTreatment tt = DefaultTrafficTreatment.builder()
512 .pushVlan().setVlanId(assignedVlan).build();
Saurav Das2d94d312015-11-24 23:21:05 -0800513 fob.withMeta(tt);
Saurav Dasc28b3432015-10-30 17:45:38 -0700514 }
Saurav Das7c305372015-10-28 12:39:42 -0700515 fob.permit().fromApp(srManager.appId);
Saurav Das07c74602016-04-27 18:35:50 -0700516 log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port);
Charles Chan1eaf4802016-04-18 13:44:03 -0700517 ObjectiveContext context = new DefaultObjectiveContext(
518 (objective) -> log.debug("Filter for {} populated", connectPoint),
519 (objective, error) ->
520 log.warn("Failed to populate filter for {}: {}", connectPoint, error));
521 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Saurav Das7c305372015-10-28 12:39:42 -0700522 }
523 }
Saurav Das07c74602016-04-27 18:35:50 -0700524 return true;
sangho80f11cb2015-04-01 13:05:26 -0700525 }
526
527 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700528 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700529 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700530 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700531 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700532 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700533 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700534 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700535 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan319d1a22015-11-03 10:42:14 -0800536 Ip4Address routerIp;
537 try {
538 routerIp = config.getRouterIp(deviceId);
539 } catch (DeviceConfigNotFoundException e) {
540 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
541 return;
542 }
543
Saurav Dasc28b3432015-10-30 17:45:38 -0700544 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
545 log.debug("Not installing port-IP punts - not the master for dev:{} ",
546 deviceId);
547 return;
548 }
Saurav Das9f1c42e2015-10-23 10:51:11 -0700549 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Charles Chan82ab1932016-01-30 23:22:37 -0800550 Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId));
Charles Chan319d1a22015-11-03 10:42:14 -0800551 allIps.add(routerIp);
Saurav Dasc28b3432015-10-30 17:45:38 -0700552 for (Ip4Address ipaddr : allIps) {
Charles Chanf4586112015-11-09 16:37:23 -0800553 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
554 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
555 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
556 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das9f1c42e2015-10-23 10:51:11 -0700557 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chanf4586112015-11-09 16:37:23 -0800558 tbuilder.setOutput(PortNumber.CONTROLLER);
559 puntIp.withSelector(sbuilder.build());
560 puntIp.withTreatment(tbuilder.build());
Saurav Das9f1c42e2015-10-23 10:51:11 -0700561 puntIp.withFlag(Flag.VERSATILE)
Charles Chan82ab1932016-01-30 23:22:37 -0800562 .withPriority(SegmentRoutingService.HIGHEST_PRIORITY)
Saurav Das9f1c42e2015-10-23 10:51:11 -0700563 .makePermanent()
564 .fromApp(srManager.appId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700565 ObjectiveContext context = new DefaultObjectiveContext(
566 (objective) -> log.debug("IP punt rule for {} populated", ipaddr),
567 (objective, error) ->
568 log.warn("Failed to populate IP punt rule for {}: {}", ipaddr, error));
569 srManager.flowObjectiveService.forward(deviceId, puntIp.add(context));
Saurav Das9f1c42e2015-10-23 10:51:11 -0700570 }
sangho80f11cb2015-04-01 13:05:26 -0700571 }
572
Charles Chanf4586112015-11-09 16:37:23 -0800573 /**
574 * Populates a forwarding objective to send packets that miss other high
575 * priority Bridging Table entries to a group that contains all ports of
576 * its subnet.
577 *
578 * Note: We assume that packets sending from the edge switches to the hosts
579 * have untagged VLAN.
580 * The VLAN tag will be popped later in the flooding group.
581 *
582 * @param deviceId switch ID to set the rules
583 */
584 public void populateSubnetBroadcastRule(DeviceId deviceId) {
585 config.getSubnets(deviceId).forEach(subnet -> {
Charles Chanbbd004c2016-02-16 23:14:49 -0800586 if (subnet.prefixLength() == 0 ||
587 subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
588 return;
589 }
Charles Chanf4586112015-11-09 16:37:23 -0800590 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
591 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
592
Saurav Das2d94d312015-11-24 23:21:05 -0800593 if (nextId < 0 || vlanId == null) {
Charles Chanbbd004c2016-02-16 23:14:49 -0800594 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
595 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das2d94d312015-11-24 23:21:05 -0800596 return;
597 }
598
Charles Chanf4586112015-11-09 16:37:23 -0800599 /* Driver should treat objective with MacAddress.NONE as the
600 * subnet broadcast rule
601 */
602 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
603 sbuilder.matchVlanId(vlanId);
604 sbuilder.matchEthDst(MacAddress.NONE);
605
606 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
607 fob.withFlag(Flag.SPECIFIC)
608 .withSelector(sbuilder.build())
609 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800610 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chanf4586112015-11-09 16:37:23 -0800611 .fromApp(srManager.appId)
612 .makePermanent();
Charles Chan1eaf4802016-04-18 13:44:03 -0700613 ObjectiveContext context = new DefaultObjectiveContext(
614 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
615 (objective, error) ->
616 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
617 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800618 });
619 }
620
Charles Chanb7f75ac2016-01-11 18:28:54 -0800621 /**
622 * Creates a filtering objective to permit VLAN cross-connect traffic.
623 *
624 * @param deviceId the DPID of the switch
625 */
626 public void populateXConnectVlanFilters(DeviceId deviceId) {
627 Map<VlanId, List<ConnectPoint>> xConnectsForDevice =
628 config.getXConnects();
629 xConnectsForDevice.forEach((vlanId, connectPoints) -> {
630 // Only proceed the xConnect for given device
631 for (ConnectPoint connectPoint : connectPoints) {
632 if (!connectPoint.deviceId().equals(deviceId)) {
633 return;
634 }
635 }
636
637 connectPoints.forEach(connectPoint -> {
638 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
639 fob.withKey(Criteria.matchInPort(connectPoint.port()))
640 .addCondition(Criteria.matchVlanId(vlanId))
641 .addCondition(Criteria.matchEthDst(MacAddress.NONE))
Charles Chan82ab1932016-01-30 23:22:37 -0800642 .withPriority(SegmentRoutingService.XCONNECT_PRIORITY);
Charles Chanb7f75ac2016-01-11 18:28:54 -0800643 fob.permit().fromApp(srManager.appId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700644 ObjectiveContext context = new DefaultObjectiveContext(
645 (objective) -> log.debug("XConnect filter for {} populated", connectPoint),
646 (objective, error) ->
647 log.warn("Failed to populate xconnect filter for {}: {}", connectPoint, error));
648 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Charles Chanb7f75ac2016-01-11 18:28:54 -0800649 });
650 });
651 }
652
653 /**
654 * Populates a forwarding objective that points the VLAN cross-connect
655 * packets to a broadcast group.
656 *
657 * @param deviceId switch ID to set the rules
658 */
659 public void populateXConnectBroadcastRule(DeviceId deviceId) {
660 Map<VlanId, List<ConnectPoint>> xConnects =
661 config.getXConnects();
662 xConnects.forEach((vlanId, connectPoints) -> {
663 // Only proceed the xConnect for given device
664 for (ConnectPoint connectPoint : connectPoints) {
665 if (!connectPoint.deviceId().equals(deviceId)) {
666 return;
667 }
668 }
669
670 int nextId = srManager.getXConnectNextObjectiveId(deviceId, vlanId);
671 if (nextId < 0) {
672 log.error("Cannot install cross-connect broadcast rule in dev:{} " +
673 "due to missing nextId:{}", deviceId, nextId);
674 return;
675 }
676
677 /*
678 * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE
679 * as the VLAN cross-connect broadcast rules
680 */
681 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
682 sbuilder.matchVlanId(vlanId);
683 sbuilder.matchEthDst(MacAddress.NONE);
684
685 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
686 fob.withFlag(Flag.SPECIFIC)
687 .withSelector(sbuilder.build())
688 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800689 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)
Charles Chanb7f75ac2016-01-11 18:28:54 -0800690 .fromApp(srManager.appId)
691 .makePermanent();
Charles Chan1eaf4802016-04-18 13:44:03 -0700692 ObjectiveContext context = new DefaultObjectiveContext(
693 (objective) -> log.debug("XConnect rule for {} populated", xConnects),
694 (objective, error) ->
695 log.warn("Failed to populate xconnect rule for {}: {}", xConnects, error));
696 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chanb7f75ac2016-01-11 18:28:54 -0800697 });
698 }
Charles Chanf4586112015-11-09 16:37:23 -0800699
Charles Chan82ab1932016-01-30 23:22:37 -0800700 private int getPriorityFromPrefix(IpPrefix prefix) {
701 return (prefix.isIp4()) ?
702 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
703 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700704 }
sangho80f11cb2015-04-01 13:05:26 -0700705}