blob: bc3ce8c62e27dc2188262b48aebcdbfa0cf7e56c [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
18import org.onlab.packet.Ethernet;
19import org.onlab.packet.Ip4Address;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070020import org.onlab.packet.Ip4Prefix;
sangho80f11cb2015-04-01 13:05:26 -070021import org.onlab.packet.IpPrefix;
22import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070024import org.onlab.packet.VlanId;
Charles Chan319d1a22015-11-03 10:42:14 -080025import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
26import org.onosproject.segmentrouting.config.DeviceConfiguration;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070027import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sangho80f11cb2015-04-01 13:05:26 -070028import org.onosproject.net.DeviceId;
29import org.onosproject.net.Link;
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
Saurav Das9f1c42e2015-10-23 10:51:11 -0700150 sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH));
sangho80f11cb2015-04-01 13:05:26 -0700151 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
152
sangho27462c62015-05-14 00:39:53 -0700153 tbuilder.deferred()
154 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800155 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700156 .setOutput(outPort);
157
158 TrafficTreatment treatment = tbuilder.build();
159 TrafficSelector selector = sbuilder.build();
160
Charles Chanf4586112015-11-09 16:37:23 -0800161 return DefaultForwardingObjective.builder()
162 .fromApp(srManager.appId).makePermanent()
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700163 .withSelector(selector).withTreatment(treatment)
164 .withPriority(100).withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700165 }
166
167 /**
168 * Populates IP flow rules for the subnets of the destination router.
169 *
170 * @param deviceId switch ID to set the rules
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700171 * @param subnets subnet information
sangho80f11cb2015-04-01 13:05:26 -0700172 * @param destSw destination switch ID
173 * @param nextHops next hop switch ID list
174 * @return true if all rules are set successfully, false otherwise
175 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700176 public boolean populateIpRuleForSubnet(DeviceId deviceId,
Charles Chanc6ad7752015-10-29 14:58:10 -0700177 Set<Ip4Prefix> subnets,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700178 DeviceId destSw,
179 Set<DeviceId> nextHops) {
sangho80f11cb2015-04-01 13:05:26 -0700180
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700181 for (IpPrefix subnet : subnets) {
sangho80f11cb2015-04-01 13:05:26 -0700182 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
183 return false;
184 }
185 }
186
187 return true;
188 }
189
190 /**
191 * Populates IP flow rules for the router IP address.
192 *
Saurav Das88979182015-10-19 14:37:36 -0700193 * @param deviceId target device ID to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700194 * @param ipPrefix the IP address of the destination router
195 * @param destSw device ID of the destination router
196 * @param nextHops next hop switch ID list
197 * @return true if all rules are set successfully, false otherwise
198 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700199 public boolean populateIpRuleForRouter(DeviceId deviceId,
200 IpPrefix ipPrefix, DeviceId destSw,
201 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800202 int segmentId;
203 try {
204 segmentId = config.getSegmentId(destSw);
205 } catch (DeviceConfigNotFoundException e) {
206 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
207 return false;
208 }
sangho80f11cb2015-04-01 13:05:26 -0700209
210 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
sangho80f11cb2015-04-01 13:05:26 -0700211 sbuilder.matchIPDst(ipPrefix);
212 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chanf4586112015-11-09 16:37:23 -0800213 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700214
Charles Chanf4586112015-11-09 16:37:23 -0800215 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
216 NeighborSet ns;
217 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700218
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700219 // If the next hop is the same as the final destination, then MPLS label
220 // is not set.
sangho80f11cb2015-04-01 13:05:26 -0700221 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800222 tbuilder.immediate().decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700223 ns = new NeighborSet(nextHops);
Charles Chanf4586112015-11-09 16:37:23 -0800224 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700225 } else {
Charles Chan319d1a22015-11-03 10:42:14 -0800226 ns = new NeighborSet(nextHops, segmentId);
Charles Chanf4586112015-11-09 16:37:23 -0800227 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700228 }
229
sangho2165d222015-05-01 09:38:25 -0700230 if (srManager.getNextObjectiveId(deviceId, ns) <= 0) {
231 log.warn("No next objective in {} for ns: {}", deviceId, ns);
232 return false;
233 }
234
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700235 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
236 .builder()
237 .fromApp(srManager.appId)
238 .makePermanent()
239 .nextStep(srManager.getNextObjectiveId(deviceId, ns))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700240 .withSelector(selector)
241 .withPriority(100)
242 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800243 if (treatment != null) {
244 fwdBuilder.withTreatment(treatment);
245 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700246 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700247 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700248 ipPrefix,
249 deviceId);
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700250 srManager.flowObjectiveService.
251 forward(deviceId,
252 fwdBuilder.
253 add(new SRObjectiveContext(deviceId,
254 SRObjectiveContext.ObjectiveType.FORWARDING)));
sanghofb7c7292015-04-13 15:15:58 -0700255 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700256
257 return true;
258 }
259
sangho80f11cb2015-04-01 13:05:26 -0700260 /**
Saurav Das88979182015-10-19 14:37:36 -0700261 * Populates MPLS flow rules to all routers.
sangho80f11cb2015-04-01 13:05:26 -0700262 *
Saurav Das88979182015-10-19 14:37:36 -0700263 * @param deviceId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700264 * @param destSwId destination switch device ID
265 * @param nextHops next hops switch ID list
266 * @return true if all rules are set successfully, false otherwise
267 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700268 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
269 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800270 int segmentId;
271 try {
272 segmentId = config.getSegmentId(destSwId);
273 } catch (DeviceConfigNotFoundException e) {
274 log.warn(e.getMessage() + " Aborting populateMplsRule.");
275 return false;
276 }
sangho80f11cb2015-04-01 13:05:26 -0700277
278 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -0700279 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700280
281 // TODO Handle the case of Bos == false
Charles Chan319d1a22015-11-03 10:42:14 -0800282 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
sangho80f11cb2015-04-01 13:05:26 -0700283 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
284
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700285 // If the next hop is the destination router, do PHP
sangho80f11cb2015-04-01 13:05:26 -0700286 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700287 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
288 + "label {} in switch {} with PHP",
Charles Chan319d1a22015-11-03 10:42:14 -0800289 segmentId,
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700290 deviceId);
291
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700292 ForwardingObjective.Builder fwdObjBosBuilder =
293 getMplsForwardingObjective(deviceId,
294 destSwId,
295 nextHops,
296 true,
297 true);
298 // TODO: Check with Sangho on why we need this
299 ForwardingObjective.Builder fwdObjNoBosBuilder =
300 getMplsForwardingObjective(deviceId,
301 destSwId,
302 nextHops,
303 true,
304 false);
305 if (fwdObjBosBuilder != null) {
306 fwdObjBuilders.add(fwdObjBosBuilder);
sangho80f11cb2015-04-01 13:05:26 -0700307 } else {
308 log.warn("Failed to set MPLS rules.");
309 return false;
310 }
311 } else {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700312 log.debug("Installing MPLS forwarding objective for "
313 + "label {} in switch {} without PHP",
Charles Chan319d1a22015-11-03 10:42:14 -0800314 segmentId,
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700315 deviceId);
316
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700317 ForwardingObjective.Builder fwdObjBosBuilder =
318 getMplsForwardingObjective(deviceId,
319 destSwId,
320 nextHops,
321 false,
322 true);
323 // TODO: Check with Sangho on why we need this
324 ForwardingObjective.Builder fwdObjNoBosBuilder =
325 getMplsForwardingObjective(deviceId,
326 destSwId,
327 nextHops,
328 false,
329 false);
330 if (fwdObjBosBuilder != null) {
331 fwdObjBuilders.add(fwdObjBosBuilder);
sangho80f11cb2015-04-01 13:05:26 -0700332 } else {
333 log.warn("Failed to set MPLS rules.");
334 return false;
335 }
336 }
337
338 TrafficSelector selector = sbuilder.build();
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700339 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
340 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
341 .makePermanent()).withSelector(selector)
342 .withPriority(100))
343 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700344 srManager.flowObjectiveService.
345 forward(deviceId,
346 fwdObjBuilder.
347 add(new SRObjectiveContext(deviceId,
348 SRObjectiveContext.ObjectiveType.FORWARDING)));
sanghofb7c7292015-04-13 15:15:58 -0700349 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700350 }
351
352 return true;
353 }
354
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700355 private ForwardingObjective.Builder getMplsForwardingObjective(DeviceId deviceId,
356 DeviceId destSw,
357 Set<DeviceId> nextHops,
358 boolean phpRequired,
359 boolean isBos) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700360 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
361 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan319d1a22015-11-03 10:42:14 -0800362 DeviceId nextHop = (DeviceId) nextHops.toArray()[0];
363
364 boolean isEdge;
365 MacAddress srcMac;
366 MacAddress dstMac;
367 try {
368 isEdge = config.isEdgeDevice(deviceId);
369 srcMac = config.getDeviceMac(deviceId);
370 dstMac = config.getDeviceMac(nextHop);
371 } catch (DeviceConfigNotFoundException e) {
372 log.warn(e.getMessage() + " Aborting getMplsForwardingObjective");
373 return null;
374 }
sangho80f11cb2015-04-01 13:05:26 -0700375
376 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
377
378 if (phpRequired) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700379 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700380 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700381 if (isBos) {
sangho27462c62015-05-14 00:39:53 -0700382 tbuilder.deferred().popMpls(Ethernet.TYPE_IPV4).decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700383 } else {
sangho27462c62015-05-14 00:39:53 -0700384 tbuilder.deferred().popMpls(Ethernet.MPLS_UNICAST).decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700385 }
386 } else {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700387 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700388 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700389 }
390
Charles Chan319d1a22015-11-03 10:42:14 -0800391 if (!isECMPSupportedInTransitRouter() && !isEdge) {
sangho28d0b6d2015-05-07 13:30:57 -0700392 PortNumber port = selectOnePort(deviceId, nextHops);
sangho28d0b6d2015-05-07 13:30:57 -0700393 if (port == null) {
sangho80f11cb2015-04-01 13:05:26 -0700394 log.warn("No link from {} to {}", deviceId, nextHops);
395 return null;
396 }
sangho27462c62015-05-14 00:39:53 -0700397 tbuilder.deferred()
Charles Chan319d1a22015-11-03 10:42:14 -0800398 .setEthSrc(srcMac)
399 .setEthDst(dstMac)
sangho28d0b6d2015-05-07 13:30:57 -0700400 .setOutput(port);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700401 fwdBuilder.withTreatment(tbuilder.build());
sangho80f11cb2015-04-01 13:05:26 -0700402 } else {
403 NeighborSet ns = new NeighborSet(nextHops);
sangho3cd8db62015-05-06 11:42:31 -0700404 fwdBuilder.withTreatment(tbuilder.build());
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700405 fwdBuilder.nextStep(srManager
406 .getNextObjectiveId(deviceId, ns));
sangho80f11cb2015-04-01 13:05:26 -0700407 }
408
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700409 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700410 }
411
sangho9b169e32015-04-14 16:27:13 -0700412 private boolean isECMPSupportedInTransitRouter() {
413
414 // TODO: remove this function when objectives subsystem is supported.
415 return false;
416 }
417
sangho80f11cb2015-04-01 13:05:26 -0700418 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700419 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700420 * dstMac corresponding to the router's MAC address. For those pipelines
421 * that need to internally assign vlans to untagged packets, this method
422 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700423 * <p>
424 * Note that the vlan assignment is only done by the master-instance for a switch.
425 * However we send the filtering objective from slave-instances as well, so
426 * that drivers can obtain other information (like Router MAC and IP).
sangho80f11cb2015-04-01 13:05:26 -0700427 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700428 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700429 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700430 public void populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700431 log.debug("Installing per-port filtering objective for untagged "
432 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800433
434 MacAddress deviceMac;
435 try {
436 deviceMac = config.getDeviceMac(deviceId);
437 } catch (DeviceConfigNotFoundException e) {
438 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
439 return;
440 }
441
Saurav Das7c305372015-10-28 12:39:42 -0700442 for (Port port : srManager.deviceService.getPorts(deviceId)) {
443 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
444 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
445 VlanId assignedVlan = (portSubnet == null)
446 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
447 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan319d1a22015-11-03 10:42:14 -0800448
Saurav Das7c305372015-10-28 12:39:42 -0700449 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
450 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan319d1a22015-11-03 10:42:14 -0800451 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasc28b3432015-10-30 17:45:38 -0700452 .addCondition(Criteria.matchVlanId(VlanId.NONE));
453 // vlan assignment is valid only if this instance is master
454 if (srManager.mastershipService.isLocalMaster(deviceId)) {
455 TrafficTreatment tt = DefaultTrafficTreatment.builder()
456 .pushVlan().setVlanId(assignedVlan).build();
457 fob.setMeta(tt);
458 }
Saurav Das7c305372015-10-28 12:39:42 -0700459 fob.permit().fromApp(srManager.appId);
460 srManager.flowObjectiveService.
461 filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
462 SRObjectiveContext.ObjectiveType.FILTER)));
463 }
464 }
sangho80f11cb2015-04-01 13:05:26 -0700465 }
466
467 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700468 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700469 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700470 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700471 * Furthermore, these are applied only by the master instance.
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 Das9f1c42e2015-10-23 10:51:11 -0700475 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan319d1a22015-11-03 10:42:14 -0800476 Ip4Address routerIp;
477 try {
478 routerIp = config.getRouterIp(deviceId);
479 } catch (DeviceConfigNotFoundException e) {
480 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
481 return;
482 }
483
Saurav Dasc28b3432015-10-30 17:45:38 -0700484 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
485 log.debug("Not installing port-IP punts - not the master for dev:{} ",
486 deviceId);
487 return;
488 }
Saurav Das9f1c42e2015-10-23 10:51:11 -0700489 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Saurav Dasc28b3432015-10-30 17:45:38 -0700490 Set<Ip4Address> allIps = new HashSet<Ip4Address>(config.getPortIPs(deviceId));
Charles Chan319d1a22015-11-03 10:42:14 -0800491 allIps.add(routerIp);
Saurav Dasc28b3432015-10-30 17:45:38 -0700492 for (Ip4Address ipaddr : allIps) {
Charles Chanf4586112015-11-09 16:37:23 -0800493 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
494 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
495 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
496 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das9f1c42e2015-10-23 10:51:11 -0700497 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chanf4586112015-11-09 16:37:23 -0800498 tbuilder.setOutput(PortNumber.CONTROLLER);
499 puntIp.withSelector(sbuilder.build());
500 puntIp.withTreatment(tbuilder.build());
Saurav Das9f1c42e2015-10-23 10:51:11 -0700501 puntIp.withFlag(Flag.VERSATILE)
502 .withPriority(HIGHEST_PRIORITY)
503 .makePermanent()
504 .fromApp(srManager.appId);
505 log.debug("Installing forwarding objective to punt port IP addresses");
506 srManager.flowObjectiveService.
507 forward(deviceId,
508 puntIp.add(new SRObjectiveContext(deviceId,
509 SRObjectiveContext.ObjectiveType.FORWARDING)));
510 }
sangho80f11cb2015-04-01 13:05:26 -0700511 }
512
Charles Chanf4586112015-11-09 16:37:23 -0800513 /**
514 * Populates a forwarding objective to send packets that miss other high
515 * priority Bridging Table entries to a group that contains all ports of
516 * its subnet.
517 *
518 * Note: We assume that packets sending from the edge switches to the hosts
519 * have untagged VLAN.
520 * The VLAN tag will be popped later in the flooding group.
521 *
522 * @param deviceId switch ID to set the rules
523 */
524 public void populateSubnetBroadcastRule(DeviceId deviceId) {
525 config.getSubnets(deviceId).forEach(subnet -> {
526 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
527 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
528
529 /* Driver should treat objective with MacAddress.NONE as the
530 * subnet broadcast rule
531 */
532 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
533 sbuilder.matchVlanId(vlanId);
534 sbuilder.matchEthDst(MacAddress.NONE);
535
536 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
537 fob.withFlag(Flag.SPECIFIC)
538 .withSelector(sbuilder.build())
539 .nextStep(nextId)
540 .withPriority(5)
541 .fromApp(srManager.appId)
542 .makePermanent();
543
544 srManager.flowObjectiveService.forward(
545 deviceId,
546 fob.add(new SRObjectiveContext(
547 deviceId,
548 SRObjectiveContext.ObjectiveType.FORWARDING)
549 )
550 );
551 });
552 }
553
554
sangho28d0b6d2015-05-07 13:30:57 -0700555 private PortNumber selectOnePort(DeviceId srcId, Set<DeviceId> destIds) {
sangho80f11cb2015-04-01 13:05:26 -0700556
sangho28d0b6d2015-05-07 13:30:57 -0700557 Set<Link> links = srManager.linkService.getDeviceLinks(srcId);
558 for (DeviceId destId: destIds) {
559 for (Link link : links) {
560 if (link.dst().deviceId().equals(destId)) {
561 return link.src().port();
562 } else if (link.src().deviceId().equals(destId)) {
563 return link.dst().port();
564 }
sangho80f11cb2015-04-01 13:05:26 -0700565 }
566 }
567
568 return null;
569 }
570
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700571 private static class SRObjectiveContext implements ObjectiveContext {
572 enum ObjectiveType {
573 FILTER,
574 FORWARDING
575 }
576 final DeviceId deviceId;
577 final ObjectiveType type;
578
579 SRObjectiveContext(DeviceId deviceId, ObjectiveType type) {
580 this.deviceId = deviceId;
581 this.type = type;
582 }
583 @Override
584 public void onSuccess(Objective objective) {
585 log.debug("{} objective operation successful in device {}",
586 type.name(), deviceId);
587 }
588
589 @Override
590 public void onError(Objective objective, ObjectiveError error) {
591 log.warn("{} objective {} operation failed with error: {} in device {}",
592 type.name(), objective, error, deviceId);
593 }
594 }
595
sangho80f11cb2015-04-01 13:05:26 -0700596}