blob: d4aa770c10f74126545428d5cd754c7cc8972331 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 Chan319d1a22015-11-03 10:42:14 -080026import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
27import org.onosproject.segmentrouting.config.DeviceConfiguration;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070028import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sangho80f11cb2015-04-01 13:05:26 -070029import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070030import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070031import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070032import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070034import org.onosproject.net.flow.TrafficSelector;
35import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070036import org.onosproject.net.flow.criteria.Criteria;
37import org.onosproject.net.flowobjective.DefaultFilteringObjective;
38import org.onosproject.net.flowobjective.DefaultForwardingObjective;
39import org.onosproject.net.flowobjective.FilteringObjective;
40import org.onosproject.net.flowobjective.ForwardingObjective;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -070041import org.onosproject.net.flowobjective.Objective;
42import org.onosproject.net.flowobjective.ObjectiveError;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070043import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070044import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -070045import org.onosproject.net.flowobjective.ObjectiveContext;
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;
52import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070053import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070054
55import static com.google.common.base.Preconditions.checkNotNull;
56
57public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070058 private static final Logger log = LoggerFactory
59 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070060
sanghofb7c7292015-04-13 15:15:58 -070061 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070062 private SegmentRoutingManager srManager;
63 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070064
65 private static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das7c305372015-10-28 12:39:42 -070066 private static final long OFPP_MAX = 0xffffff00L;
67
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 }
Charles Chanf4586112015-11-09 16:37:23 -0800117 srManager.flowObjectiveService.
118 forward(deviceId, fwdBuilder.add(new SRObjectiveContext(deviceId,
119 SRObjectiveContext.ObjectiveType.FORWARDING)));
120 rulePopulationCounter.incrementAndGet();
121 }
122
123 public void revokeIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
124 MacAddress hostMac, PortNumber outPort) {
125 log.debug("Revoke IP table entry for host {} at {}:{}",
126 hostIp, deviceId, outPort);
127 ForwardingObjective.Builder fwdBuilder;
128 try {
129 fwdBuilder = getForwardingObjectiveBuilder(
130 deviceId, hostIp, hostMac, outPort);
131 } catch (DeviceConfigNotFoundException e) {
132 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
133 return;
134 }
135 srManager.flowObjectiveService.
136 forward(deviceId, fwdBuilder.remove(new SRObjectiveContext(deviceId,
137 SRObjectiveContext.ObjectiveType.FORWARDING)));
138 }
139
140 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
141 DeviceId deviceId, Ip4Address hostIp,
142 MacAddress hostMac, PortNumber outPort)
143 throws DeviceConfigNotFoundException {
144 MacAddress deviceMac;
145 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800146
sangho80f11cb2015-04-01 13:05:26 -0700147 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
148 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
149
sangho80f11cb2015-04-01 13:05:26 -0700150 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Saurav Das2d94d312015-11-24 23:21:05 -0800151 sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH));
152 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700153
sangho27462c62015-05-14 00:39:53 -0700154 tbuilder.deferred()
155 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800156 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700157 .setOutput(outPort);
sangho80f11cb2015-04-01 13:05:26 -0700158 TrafficTreatment treatment = tbuilder.build();
Saurav Das2d94d312015-11-24 23:21:05 -0800159
160 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
161 // for switch pipelines that need it, provide outgoing vlan as metadata
162 VlanId outvlan = null;
163 Ip4Prefix subnet = srManager.deviceConfiguration.getPortSubnet(deviceId, outPort);
164 if (subnet == null) {
165 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
166 } else {
167 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
168 }
169 TrafficSelector meta = DefaultTrafficSelector.builder()
170 .matchVlanId(outvlan).build();
171 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
172 treatment, meta);
sangho80f11cb2015-04-01 13:05:26 -0700173
Charles Chanf4586112015-11-09 16:37:23 -0800174 return DefaultForwardingObjective.builder()
Saurav Das2d94d312015-11-24 23:21:05 -0800175 .withSelector(selector)
176 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800177 .fromApp(srManager.appId).makePermanent()
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700178 .withPriority(100).withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700179 }
180
181 /**
182 * Populates IP flow rules for the subnets of the destination router.
183 *
184 * @param deviceId switch ID to set the rules
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700185 * @param subnets subnet information
sangho80f11cb2015-04-01 13:05:26 -0700186 * @param destSw destination switch ID
187 * @param nextHops next hop switch ID list
188 * @return true if all rules are set successfully, false otherwise
189 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700190 public boolean populateIpRuleForSubnet(DeviceId deviceId,
Charles Chanc6ad7752015-10-29 14:58:10 -0700191 Set<Ip4Prefix> subnets,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700192 DeviceId destSw,
193 Set<DeviceId> nextHops) {
sangho80f11cb2015-04-01 13:05:26 -0700194
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700195 for (IpPrefix subnet : subnets) {
sangho80f11cb2015-04-01 13:05:26 -0700196 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
197 return false;
198 }
199 }
200
201 return true;
202 }
203
204 /**
205 * Populates IP flow rules for the router IP address.
206 *
Saurav Das88979182015-10-19 14:37:36 -0700207 * @param deviceId target device ID to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700208 * @param ipPrefix the IP address of the destination router
209 * @param destSw device ID of the destination router
210 * @param nextHops next hop switch ID list
211 * @return true if all rules are set successfully, false otherwise
212 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700213 public boolean populateIpRuleForRouter(DeviceId deviceId,
214 IpPrefix ipPrefix, DeviceId destSw,
215 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800216 int segmentId;
217 try {
218 segmentId = config.getSegmentId(destSw);
219 } catch (DeviceConfigNotFoundException e) {
220 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
221 return false;
222 }
sangho80f11cb2015-04-01 13:05:26 -0700223
224 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
sangho80f11cb2015-04-01 13:05:26 -0700225 sbuilder.matchIPDst(ipPrefix);
226 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chanf4586112015-11-09 16:37:23 -0800227 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700228
Charles Chanf4586112015-11-09 16:37:23 -0800229 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
230 NeighborSet ns;
231 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700232
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700233 // If the next hop is the same as the final destination, then MPLS label
234 // is not set.
sangho80f11cb2015-04-01 13:05:26 -0700235 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800236 tbuilder.immediate().decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700237 ns = new NeighborSet(nextHops);
Charles Chanf4586112015-11-09 16:37:23 -0800238 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700239 } else {
Charles Chan319d1a22015-11-03 10:42:14 -0800240 ns = new NeighborSet(nextHops, segmentId);
Charles Chanf4586112015-11-09 16:37:23 -0800241 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700242 }
243
Saurav Das4c35fc42015-11-20 15:27:53 -0800244 // setup metadata to pass to nextObjective - indicate the vlan on egress
245 // if needed by the switch pipeline. Since neighbor sets are always to
246 // other neighboring routers, there is no subnet assigned on those ports.
247 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
248 metabuilder.matchVlanId(
249 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
250
251 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
252 if (nextId <= 0) {
sangho2165d222015-05-01 09:38:25 -0700253 log.warn("No next objective in {} for ns: {}", deviceId, ns);
254 return false;
255 }
256
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700257 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
258 .builder()
259 .fromApp(srManager.appId)
260 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800261 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700262 .withSelector(selector)
263 .withPriority(100)
264 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800265 if (treatment != null) {
266 fwdBuilder.withTreatment(treatment);
267 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700268 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700269 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700270 ipPrefix,
271 deviceId);
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700272 srManager.flowObjectiveService.
273 forward(deviceId,
274 fwdBuilder.
275 add(new SRObjectiveContext(deviceId,
276 SRObjectiveContext.ObjectiveType.FORWARDING)));
sanghofb7c7292015-04-13 15:15:58 -0700277 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700278
279 return true;
280 }
281
sangho80f11cb2015-04-01 13:05:26 -0700282 /**
Saurav Das88979182015-10-19 14:37:36 -0700283 * Populates MPLS flow rules to all routers.
sangho80f11cb2015-04-01 13:05:26 -0700284 *
Saurav Das88979182015-10-19 14:37:36 -0700285 * @param deviceId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700286 * @param destSwId destination switch device ID
287 * @param nextHops next hops switch ID list
288 * @return true if all rules are set successfully, false otherwise
289 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700290 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
291 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800292 int segmentId;
293 try {
294 segmentId = config.getSegmentId(destSwId);
295 } catch (DeviceConfigNotFoundException e) {
296 log.warn(e.getMessage() + " Aborting populateMplsRule.");
297 return false;
298 }
sangho80f11cb2015-04-01 13:05:26 -0700299
300 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -0700301 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700302
303 // TODO Handle the case of Bos == false
sangho80f11cb2015-04-01 13:05:26 -0700304 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das4c35fc42015-11-20 15:27:53 -0800305 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
306 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700307
Saurav Das4c35fc42015-11-20 15:27:53 -0800308 // setup metadata to pass to nextObjective - indicate the vlan on egress
309 // if needed by the switch pipeline. Since mpls next-hops are always to
310 // other neighboring routers, there is no subnet assigned on those ports.
311 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
312 metabuilder.matchVlanId(
313 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
314
315 // If the next hop is the destination router for the segment, do pop
sangho80f11cb2015-04-01 13:05:26 -0700316 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700317 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800318 + "label {} in switch {} with pop", segmentId, deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700319
Saurav Das4c35fc42015-11-20 15:27:53 -0800320 // bos pop case (php)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700321 ForwardingObjective.Builder fwdObjBosBuilder =
322 getMplsForwardingObjective(deviceId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700323 nextHops,
324 true,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700325 true,
Saurav Das4c35fc42015-11-20 15:27:53 -0800326 metabuilder.build());
327 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700328 return false;
329 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800330 fwdObjBuilders.add(fwdObjBosBuilder);
331
332 // XXX not-bos pop case, SR app multi-label not implemented yet
333 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
334 getMplsForwardingObjective(deviceId,
335 nextHops,
336 true,
337 false);*/
338
sangho80f11cb2015-04-01 13:05:26 -0700339 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800340 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700341 log.debug("Installing MPLS forwarding objective for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800342 + "label {} in switch {} without pop", segmentId, deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700343
Saurav Das4c35fc42015-11-20 15:27:53 -0800344 // continue case with bos - this does get triggered in edge routers
345 // and in core routers - driver can handle depending on availability
346 // of MPLS ECMP or not
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700347 ForwardingObjective.Builder fwdObjBosBuilder =
348 getMplsForwardingObjective(deviceId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700349 nextHops,
350 false,
Saurav Das4c35fc42015-11-20 15:27:53 -0800351 true,
352 metabuilder.build());
353 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700354 return false;
355 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800356 fwdObjBuilders.add(fwdObjBosBuilder);
357
358 // XXX continue case with not-bos - SR app multi label not implemented yet
359 // also requires MPLS ECMP
360 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
361 getMplsForwardingObjective(deviceId,
362 nextHops,
363 false,
364 false); */
365
sangho80f11cb2015-04-01 13:05:26 -0700366 }
367
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700368 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
369 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
370 .makePermanent()).withSelector(selector)
371 .withPriority(100))
372 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700373 srManager.flowObjectiveService.
374 forward(deviceId,
375 fwdObjBuilder.
376 add(new SRObjectiveContext(deviceId,
Saurav Das4c35fc42015-11-20 15:27:53 -0800377 SRObjectiveContext.ObjectiveType.FORWARDING)));
sanghofb7c7292015-04-13 15:15:58 -0700378 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700379 }
380
381 return true;
382 }
383
Saurav Das4c35fc42015-11-20 15:27:53 -0800384 private ForwardingObjective.Builder getMplsForwardingObjective(
385 DeviceId deviceId,
386 Set<DeviceId> nextHops,
387 boolean phpRequired,
388 boolean isBos,
389 TrafficSelector meta) {
390
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700391 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
392 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700393
394 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
395
396 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800397 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700398 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700399 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700400 if (isBos) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800401 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
402 .decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700403 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800404 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
405 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700406 }
407 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800408 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700409 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700410 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700411 }
412
Saurav Das4c35fc42015-11-20 15:27:53 -0800413 // All forwarding is via ECMP group, the metadata informs the driver
414 // that the next-Objective will be used by MPLS flows. In other words,
415 // MPLS ECMP is requested. It is up to the driver to decide if these
416 // packets will be hashed or not.
417 fwdBuilder.withTreatment(tbuilder.build());
418 NeighborSet ns = new NeighborSet(nextHops);
419 log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}",
420 deviceId, ns);
421
422 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
423 if (nextId <= 0) {
424 log.warn("No next objective in {} for ns: {}", deviceId, ns);
425 return null;
sangho80f11cb2015-04-01 13:05:26 -0700426 }
427
Saurav Das4c35fc42015-11-20 15:27:53 -0800428 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700429 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700430 }
431
432 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700433 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700434 * dstMac corresponding to the router's MAC address. For those pipelines
435 * that need to internally assign vlans to untagged packets, this method
436 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700437 * <p>
438 * Note that the vlan assignment is only done by the master-instance for a switch.
439 * However we send the filtering objective from slave-instances as well, so
440 * that drivers can obtain other information (like Router MAC and IP).
sangho80f11cb2015-04-01 13:05:26 -0700441 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700442 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700443 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700444 public void populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700445 log.debug("Installing per-port filtering objective for untagged "
446 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800447
448 MacAddress deviceMac;
449 try {
450 deviceMac = config.getDeviceMac(deviceId);
451 } catch (DeviceConfigNotFoundException e) {
452 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
453 return;
454 }
455
Saurav Das7c305372015-10-28 12:39:42 -0700456 for (Port port : srManager.deviceService.getPorts(deviceId)) {
457 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
458 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
459 VlanId assignedVlan = (portSubnet == null)
460 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
461 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan319d1a22015-11-03 10:42:14 -0800462
Saurav Das7c305372015-10-28 12:39:42 -0700463 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
464 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan319d1a22015-11-03 10:42:14 -0800465 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasc28b3432015-10-30 17:45:38 -0700466 .addCondition(Criteria.matchVlanId(VlanId.NONE));
467 // vlan assignment is valid only if this instance is master
468 if (srManager.mastershipService.isLocalMaster(deviceId)) {
469 TrafficTreatment tt = DefaultTrafficTreatment.builder()
470 .pushVlan().setVlanId(assignedVlan).build();
Saurav Das2d94d312015-11-24 23:21:05 -0800471 fob.withMeta(tt);
Saurav Dasc28b3432015-10-30 17:45:38 -0700472 }
Saurav Das7c305372015-10-28 12:39:42 -0700473 fob.permit().fromApp(srManager.appId);
474 srManager.flowObjectiveService.
475 filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
476 SRObjectiveContext.ObjectiveType.FILTER)));
477 }
478 }
sangho80f11cb2015-04-01 13:05:26 -0700479 }
480
481 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700482 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700483 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700484 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700485 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700486 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700487 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700488 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700489 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan319d1a22015-11-03 10:42:14 -0800490 Ip4Address routerIp;
491 try {
492 routerIp = config.getRouterIp(deviceId);
493 } catch (DeviceConfigNotFoundException e) {
494 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
495 return;
496 }
497
Saurav Dasc28b3432015-10-30 17:45:38 -0700498 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
499 log.debug("Not installing port-IP punts - not the master for dev:{} ",
500 deviceId);
501 return;
502 }
Saurav Das9f1c42e2015-10-23 10:51:11 -0700503 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Saurav Dasc28b3432015-10-30 17:45:38 -0700504 Set<Ip4Address> allIps = new HashSet<Ip4Address>(config.getPortIPs(deviceId));
Charles Chan319d1a22015-11-03 10:42:14 -0800505 allIps.add(routerIp);
Saurav Dasc28b3432015-10-30 17:45:38 -0700506 for (Ip4Address ipaddr : allIps) {
Charles Chanf4586112015-11-09 16:37:23 -0800507 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
508 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
509 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
510 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das9f1c42e2015-10-23 10:51:11 -0700511 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chanf4586112015-11-09 16:37:23 -0800512 tbuilder.setOutput(PortNumber.CONTROLLER);
513 puntIp.withSelector(sbuilder.build());
514 puntIp.withTreatment(tbuilder.build());
Saurav Das9f1c42e2015-10-23 10:51:11 -0700515 puntIp.withFlag(Flag.VERSATILE)
516 .withPriority(HIGHEST_PRIORITY)
517 .makePermanent()
518 .fromApp(srManager.appId);
519 log.debug("Installing forwarding objective to punt port IP addresses");
520 srManager.flowObjectiveService.
521 forward(deviceId,
522 puntIp.add(new SRObjectiveContext(deviceId,
523 SRObjectiveContext.ObjectiveType.FORWARDING)));
524 }
sangho80f11cb2015-04-01 13:05:26 -0700525 }
526
Charles Chanf4586112015-11-09 16:37:23 -0800527 /**
Charles Chand4a99c52015-11-18 16:51:08 -0800528 * Creates a forwarding objective to punt all IP packets, destined to the
529 * router's port IP addresses, to the controller. Note that the input
530 * port should not be matched on, as these packets can come from any input.
531 * Furthermore, these are applied only by the master instance.
532 *
533 * @param deviceId the switch dpid for the router
534 */
535 public void populateArpPunts(DeviceId deviceId) {
536 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
537 log.debug("Not installing port-IP punts - not the master for dev:{} ",
538 deviceId);
539 return;
540 }
541
542 ForwardingObjective.Builder puntArp = DefaultForwardingObjective.builder();
543 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
544 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
545 sbuilder.matchEthType(Ethernet.TYPE_ARP);
546 tbuilder.setOutput(PortNumber.CONTROLLER);
547 puntArp.withSelector(sbuilder.build());
548 puntArp.withTreatment(tbuilder.build());
549 puntArp.withFlag(Flag.VERSATILE)
550 .withPriority(HIGHEST_PRIORITY)
551 .makePermanent()
552 .fromApp(srManager.appId);
553 log.debug("Installing forwarding objective to punt ARPs");
554 srManager.flowObjectiveService.
555 forward(deviceId,
556 puntArp.add(new SRObjectiveContext(deviceId,
557 SRObjectiveContext.ObjectiveType.FORWARDING)));
558 }
559
560 /**
Charles Chanf4586112015-11-09 16:37:23 -0800561 * Populates a forwarding objective to send packets that miss other high
562 * priority Bridging Table entries to a group that contains all ports of
563 * its subnet.
564 *
565 * Note: We assume that packets sending from the edge switches to the hosts
566 * have untagged VLAN.
567 * The VLAN tag will be popped later in the flooding group.
568 *
569 * @param deviceId switch ID to set the rules
570 */
571 public void populateSubnetBroadcastRule(DeviceId deviceId) {
572 config.getSubnets(deviceId).forEach(subnet -> {
573 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
574 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
575
Saurav Das2d94d312015-11-24 23:21:05 -0800576 if (nextId < 0 || vlanId == null) {
577 log.error("Cannot install subnet broadcast rule in dev:{} due"
578 + "to vlanId:{} or nextId:{}", vlanId, nextId);
579 return;
580 }
581
Charles Chanf4586112015-11-09 16:37:23 -0800582 /* Driver should treat objective with MacAddress.NONE as the
583 * subnet broadcast rule
584 */
585 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
586 sbuilder.matchVlanId(vlanId);
587 sbuilder.matchEthDst(MacAddress.NONE);
588
589 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
590 fob.withFlag(Flag.SPECIFIC)
591 .withSelector(sbuilder.build())
592 .nextStep(nextId)
593 .withPriority(5)
594 .fromApp(srManager.appId)
595 .makePermanent();
596
597 srManager.flowObjectiveService.forward(
598 deviceId,
599 fob.add(new SRObjectiveContext(
600 deviceId,
601 SRObjectiveContext.ObjectiveType.FORWARDING)
602 )
603 );
604 });
605 }
606
607
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700608 private static class SRObjectiveContext implements ObjectiveContext {
609 enum ObjectiveType {
610 FILTER,
611 FORWARDING
612 }
613 final DeviceId deviceId;
614 final ObjectiveType type;
615
616 SRObjectiveContext(DeviceId deviceId, ObjectiveType type) {
617 this.deviceId = deviceId;
618 this.type = type;
619 }
620 @Override
621 public void onSuccess(Objective objective) {
622 log.debug("{} objective operation successful in device {}",
623 type.name(), deviceId);
624 }
625
626 @Override
627 public void onError(Objective objective, ObjectiveError error) {
628 log.warn("{} objective {} operation failed with error: {} in device {}",
629 type.name(), objective, error, deviceId);
630 }
631 }
632
sangho80f11cb2015-04-01 13:05:26 -0700633}