blob: 03b29a5e5b200ef398cd0804f908ed90abd1d1ea [file] [log] [blame]
sanghob35a6192015-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 Das8a0732e2015-11-20 15:27:53 -080018import org.onlab.packet.EthType;
sanghob35a6192015-04-01 13:05:26 -070019import org.onlab.packet.Ethernet;
20import org.onlab.packet.Ip4Address;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070021import org.onlab.packet.Ip4Prefix;
sanghob35a6192015-04-01 13:05:26 -070022import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
sanghob35a6192015-04-01 13:05:26 -070024import org.onlab.packet.MplsLabel;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070025import org.onlab.packet.VlanId;
Charles Chan0b4e6182015-11-03 10:42:14 -080026import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
27import org.onosproject.segmentrouting.config.DeviceConfiguration;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070028import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sanghob35a6192015-04-01 13:05:26 -070029import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070030import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070031import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070032import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070034import org.onosproject.net.flow.TrafficSelector;
35import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-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 Vavilapallif3a8bc02015-05-22 13:47:31 -070041import org.onosproject.net.flowobjective.Objective;
42import org.onosproject.net.flowobjective.ObjectiveError;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070043import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das822c4e22015-10-23 10:51:11 -070044import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070045import org.onosproject.net.flowobjective.ObjectiveContext;
sanghob35a6192015-04-01 13:05:26 -070046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
49import java.util.ArrayList;
Saurav Das837e0bb2015-10-30 17:45:38 -070050import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070051import java.util.List;
52import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070053import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070054
55import static com.google.common.base.Preconditions.checkNotNull;
56
57public class RoutingRulePopulator {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070058 private static final Logger log = LoggerFactory
59 .getLogger(RoutingRulePopulator.class);
sanghob35a6192015-04-01 13:05:26 -070060
sangho20eff1d2015-04-13 15:15:58 -070061 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070062 private SegmentRoutingManager srManager;
63 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070064
65 private static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das0e99e2b2015-10-28 12:39:42 -070066 private static final long OFPP_MAX = 0xffffff00L;
67
Saurav Das822c4e22015-10-23 10:51:11 -070068
sanghob35a6192015-04-01 13:05:26 -070069 /**
70 * Creates a RoutingRulePopulator object.
71 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070072 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070073 */
74 public RoutingRulePopulator(SegmentRoutingManager srManager) {
75 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070076 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-04-13 15:15:58 -070077 this.rulePopulationCounter = new AtomicLong(0);
78 }
79
80 /**
81 * Resets the population counter.
82 */
83 public void resetCounter() {
84 rulePopulationCounter.set(0);
85 }
86
87 /**
88 * Returns the number of rules populated.
Thomas Vachuska266b4432015-04-30 18:13:25 -070089 *
90 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -070091 */
92 public long getCounter() {
93 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -070094 }
95
96 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070097 * Populates IP flow rules for specific hosts directly connected to the
98 * switch.
sanghob35a6192015-04-01 13:05:26 -070099 *
100 * @param deviceId switch ID to set the rules
101 * @param hostIp host IP address
102 * @param hostMac host MAC address
103 * @param outPort port where the host is connected
104 */
105 public void populateIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
106 MacAddress hostMac, PortNumber outPort) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800107 log.debug("Populate IP table entry for host {} at {}:{}",
108 hostIp, deviceId, outPort);
109 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800110 try {
Charles Chan68aa62d2015-11-09 16:37:23 -0800111 fwdBuilder = getForwardingObjectiveBuilder(
112 deviceId, hostIp, hostMac, outPort);
Charles Chan0b4e6182015-11-03 10:42:14 -0800113 } catch (DeviceConfigNotFoundException e) {
114 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
115 return;
116 }
Charles Chan68aa62d2015-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 Chan0b4e6182015-11-03 10:42:14 -0800146
sanghob35a6192015-04-01 13:05:26 -0700147 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
148 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
149
sanghob35a6192015-04-01 13:05:26 -0700150 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Saurav Das4ce45962015-11-24 23:21:05 -0800151 sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH));
152 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700153
sangho1e575652015-05-14 00:39:53 -0700154 tbuilder.deferred()
155 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800156 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700157 .setOutput(outPort);
sanghob35a6192015-04-01 13:05:26 -0700158 TrafficTreatment treatment = tbuilder.build();
Saurav Das4ce45962015-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);
sanghob35a6192015-04-01 13:05:26 -0700173
Charles Chan68aa62d2015-11-09 16:37:23 -0800174 return DefaultForwardingObjective.builder()
Saurav Das4ce45962015-11-24 23:21:05 -0800175 .withSelector(selector)
176 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800177 .fromApp(srManager.appId).makePermanent()
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700178 .withPriority(100).withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-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 Vavilapalli4db76e32015-04-07 15:12:32 -0700185 * @param subnets subnet information
sanghob35a6192015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700190 public boolean populateIpRuleForSubnet(DeviceId deviceId,
Charles Chan9f676b62015-10-29 14:58:10 -0700191 Set<Ip4Prefix> subnets,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700192 DeviceId destSw,
193 Set<DeviceId> nextHops) {
sanghob35a6192015-04-01 13:05:26 -0700194
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700195 for (IpPrefix subnet : subnets) {
sanghob35a6192015-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 Dasa07f2032015-10-19 14:37:36 -0700207 * @param deviceId target device ID to set the rules
sanghob35a6192015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700213 public boolean populateIpRuleForRouter(DeviceId deviceId,
214 IpPrefix ipPrefix, DeviceId destSw,
215 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-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 }
sanghob35a6192015-04-01 13:05:26 -0700223
224 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
sanghob35a6192015-04-01 13:05:26 -0700225 sbuilder.matchIPDst(ipPrefix);
226 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan68aa62d2015-11-09 16:37:23 -0800227 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700228
Charles Chan68aa62d2015-11-09 16:37:23 -0800229 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
230 NeighborSet ns;
231 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700232
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700233 // If the next hop is the same as the final destination, then MPLS label
234 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700235 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800236 tbuilder.immediate().decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700237 ns = new NeighborSet(nextHops);
Charles Chan68aa62d2015-11-09 16:37:23 -0800238 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700239 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800240 ns = new NeighborSet(nextHops, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800241 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700242 }
243
Saurav Das8a0732e2015-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) {
sangho834e4b02015-05-01 09:38:25 -0700253 log.warn("No next objective in {} for ns: {}", deviceId, ns);
254 return false;
255 }
256
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700257 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
258 .builder()
259 .fromApp(srManager.appId)
260 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800261 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700262 .withSelector(selector)
Flavio Castro6816f252016-01-14 14:53:33 -0800263 .withPriority(2000 * ipPrefix.prefixLength())
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700264 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800265 if (treatment != null) {
266 fwdBuilder.withTreatment(treatment);
267 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700268 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700269 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700270 ipPrefix,
271 deviceId);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700272 srManager.flowObjectiveService.
273 forward(deviceId,
274 fwdBuilder.
275 add(new SRObjectiveContext(deviceId,
276 SRObjectiveContext.ObjectiveType.FORWARDING)));
sangho20eff1d2015-04-13 15:15:58 -0700277 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700278
279 return true;
280 }
281
sanghob35a6192015-04-01 13:05:26 -0700282 /**
Saurav Dasa07f2032015-10-19 14:37:36 -0700283 * Populates MPLS flow rules to all routers.
sanghob35a6192015-04-01 13:05:26 -0700284 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700285 * @param deviceId target device ID of the switch to set the rules
sanghob35a6192015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700290 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
291 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-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 }
sanghob35a6192015-04-01 13:05:26 -0700299
300 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700301 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700302
303 // TODO Handle the case of Bos == false
sanghob35a6192015-04-01 13:05:26 -0700304 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das8a0732e2015-11-20 15:27:53 -0800305 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chan188ebf52015-12-23 00:15:11 -0800306 sbuilder.matchMplsBos(true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800307 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700308
Saurav Das8a0732e2015-11-20 15:27:53 -0800309 // setup metadata to pass to nextObjective - indicate the vlan on egress
310 // if needed by the switch pipeline. Since mpls next-hops are always to
311 // other neighboring routers, there is no subnet assigned on those ports.
312 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
313 metabuilder.matchVlanId(
314 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
315
316 // If the next hop is the destination router for the segment, do pop
sanghob35a6192015-04-01 13:05:26 -0700317 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700318 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800319 + "label {} in switch {} with pop", segmentId, deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700320
Saurav Das8a0732e2015-11-20 15:27:53 -0800321 // bos pop case (php)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700322 ForwardingObjective.Builder fwdObjBosBuilder =
323 getMplsForwardingObjective(deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700324 nextHops,
325 true,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700326 true,
Saurav Das8a0732e2015-11-20 15:27:53 -0800327 metabuilder.build());
328 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700329 return false;
330 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800331 fwdObjBuilders.add(fwdObjBosBuilder);
332
333 // XXX not-bos pop case, SR app multi-label not implemented yet
334 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
335 getMplsForwardingObjective(deviceId,
336 nextHops,
337 true,
338 false);*/
339
sanghob35a6192015-04-01 13:05:26 -0700340 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800341 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700342 log.debug("Installing MPLS forwarding objective for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800343 + "label {} in switch {} without pop", segmentId, deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700344
Saurav Das8a0732e2015-11-20 15:27:53 -0800345 // continue case with bos - this does get triggered in edge routers
346 // and in core routers - driver can handle depending on availability
347 // of MPLS ECMP or not
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700348 ForwardingObjective.Builder fwdObjBosBuilder =
349 getMplsForwardingObjective(deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700350 nextHops,
351 false,
Saurav Das8a0732e2015-11-20 15:27:53 -0800352 true,
353 metabuilder.build());
354 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700355 return false;
356 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800357 fwdObjBuilders.add(fwdObjBosBuilder);
358
359 // XXX continue case with not-bos - SR app multi label not implemented yet
360 // also requires MPLS ECMP
361 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
362 getMplsForwardingObjective(deviceId,
363 nextHops,
364 false,
365 false); */
366
sanghob35a6192015-04-01 13:05:26 -0700367 }
368
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700369 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
370 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
371 .makePermanent()).withSelector(selector)
372 .withPriority(100))
373 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700374 srManager.flowObjectiveService.
375 forward(deviceId,
376 fwdObjBuilder.
377 add(new SRObjectiveContext(deviceId,
Saurav Das8a0732e2015-11-20 15:27:53 -0800378 SRObjectiveContext.ObjectiveType.FORWARDING)));
sangho20eff1d2015-04-13 15:15:58 -0700379 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700380 }
381
382 return true;
383 }
384
Saurav Das8a0732e2015-11-20 15:27:53 -0800385 private ForwardingObjective.Builder getMplsForwardingObjective(
386 DeviceId deviceId,
387 Set<DeviceId> nextHops,
388 boolean phpRequired,
389 boolean isBos,
390 TrafficSelector meta) {
391
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700392 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
393 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700394
395 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
396
397 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800398 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700399 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700400 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700401 if (isBos) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800402 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
403 .decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700404 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800405 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
406 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700407 }
408 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800409 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700410 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700411 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700412 }
413
Saurav Das8a0732e2015-11-20 15:27:53 -0800414 // All forwarding is via ECMP group, the metadata informs the driver
415 // that the next-Objective will be used by MPLS flows. In other words,
416 // MPLS ECMP is requested. It is up to the driver to decide if these
417 // packets will be hashed or not.
418 fwdBuilder.withTreatment(tbuilder.build());
419 NeighborSet ns = new NeighborSet(nextHops);
420 log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}",
421 deviceId, ns);
422
423 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
424 if (nextId <= 0) {
425 log.warn("No next objective in {} for ns: {}", deviceId, ns);
426 return null;
sanghob35a6192015-04-01 13:05:26 -0700427 }
428
Saurav Das8a0732e2015-11-20 15:27:53 -0800429 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700430 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700431 }
432
433 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700434 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700435 * dstMac corresponding to the router's MAC address. For those pipelines
436 * that need to internally assign vlans to untagged packets, this method
437 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700438 * <p>
439 * Note that the vlan assignment is only done by the master-instance for a switch.
440 * However we send the filtering objective from slave-instances as well, so
441 * that drivers can obtain other information (like Router MAC and IP).
sanghob35a6192015-04-01 13:05:26 -0700442 *
Saurav Das822c4e22015-10-23 10:51:11 -0700443 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700444 */
Saurav Das822c4e22015-10-23 10:51:11 -0700445 public void populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700446 log.debug("Installing per-port filtering objective for untagged "
447 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800448
449 MacAddress deviceMac;
450 try {
451 deviceMac = config.getDeviceMac(deviceId);
452 } catch (DeviceConfigNotFoundException e) {
453 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
454 return;
455 }
456
Saurav Das0e99e2b2015-10-28 12:39:42 -0700457 for (Port port : srManager.deviceService.getPorts(deviceId)) {
458 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
459 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
460 VlanId assignedVlan = (portSubnet == null)
461 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
462 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan0b4e6182015-11-03 10:42:14 -0800463
Saurav Das0e99e2b2015-10-28 12:39:42 -0700464 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
465 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan0b4e6182015-11-03 10:42:14 -0800466 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Das837e0bb2015-10-30 17:45:38 -0700467 .addCondition(Criteria.matchVlanId(VlanId.NONE));
468 // vlan assignment is valid only if this instance is master
469 if (srManager.mastershipService.isLocalMaster(deviceId)) {
470 TrafficTreatment tt = DefaultTrafficTreatment.builder()
471 .pushVlan().setVlanId(assignedVlan).build();
Saurav Das4ce45962015-11-24 23:21:05 -0800472 fob.withMeta(tt);
Saurav Das837e0bb2015-10-30 17:45:38 -0700473 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700474 fob.permit().fromApp(srManager.appId);
475 srManager.flowObjectiveService.
476 filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
477 SRObjectiveContext.ObjectiveType.FILTER)));
478 }
479 }
sanghob35a6192015-04-01 13:05:26 -0700480 }
481
482 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700483 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700484 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700485 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700486 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700487 *
Saurav Das822c4e22015-10-23 10:51:11 -0700488 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700489 */
Saurav Das822c4e22015-10-23 10:51:11 -0700490 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800491 Ip4Address routerIp;
492 try {
493 routerIp = config.getRouterIp(deviceId);
494 } catch (DeviceConfigNotFoundException e) {
495 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
496 return;
497 }
498
Saurav Das837e0bb2015-10-30 17:45:38 -0700499 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
500 log.debug("Not installing port-IP punts - not the master for dev:{} ",
501 deviceId);
502 return;
503 }
Saurav Das822c4e22015-10-23 10:51:11 -0700504 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Saurav Das837e0bb2015-10-30 17:45:38 -0700505 Set<Ip4Address> allIps = new HashSet<Ip4Address>(config.getPortIPs(deviceId));
Charles Chan0b4e6182015-11-03 10:42:14 -0800506 allIps.add(routerIp);
Saurav Das837e0bb2015-10-30 17:45:38 -0700507 for (Ip4Address ipaddr : allIps) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800508 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
509 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
510 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
511 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das822c4e22015-10-23 10:51:11 -0700512 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chan68aa62d2015-11-09 16:37:23 -0800513 tbuilder.setOutput(PortNumber.CONTROLLER);
514 puntIp.withSelector(sbuilder.build());
515 puntIp.withTreatment(tbuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700516 puntIp.withFlag(Flag.VERSATILE)
517 .withPriority(HIGHEST_PRIORITY)
518 .makePermanent()
519 .fromApp(srManager.appId);
520 log.debug("Installing forwarding objective to punt port IP addresses");
521 srManager.flowObjectiveService.
522 forward(deviceId,
523 puntIp.add(new SRObjectiveContext(deviceId,
524 SRObjectiveContext.ObjectiveType.FORWARDING)));
525 }
sanghob35a6192015-04-01 13:05:26 -0700526 }
527
Charles Chan68aa62d2015-11-09 16:37:23 -0800528 /**
529 * Populates a forwarding objective to send packets that miss other high
530 * priority Bridging Table entries to a group that contains all ports of
531 * its subnet.
532 *
533 * Note: We assume that packets sending from the edge switches to the hosts
534 * have untagged VLAN.
535 * The VLAN tag will be popped later in the flooding group.
536 *
537 * @param deviceId switch ID to set the rules
538 */
539 public void populateSubnetBroadcastRule(DeviceId deviceId) {
540 config.getSubnets(deviceId).forEach(subnet -> {
541 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
542 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
543
Saurav Das4ce45962015-11-24 23:21:05 -0800544 if (nextId < 0 || vlanId == null) {
545 log.error("Cannot install subnet broadcast rule in dev:{} due"
546 + "to vlanId:{} or nextId:{}", vlanId, nextId);
547 return;
548 }
549
Charles Chan68aa62d2015-11-09 16:37:23 -0800550 /* Driver should treat objective with MacAddress.NONE as the
551 * subnet broadcast rule
552 */
553 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
554 sbuilder.matchVlanId(vlanId);
555 sbuilder.matchEthDst(MacAddress.NONE);
556
557 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
558 fob.withFlag(Flag.SPECIFIC)
559 .withSelector(sbuilder.build())
560 .nextStep(nextId)
561 .withPriority(5)
562 .fromApp(srManager.appId)
563 .makePermanent();
564
565 srManager.flowObjectiveService.forward(
566 deviceId,
567 fob.add(new SRObjectiveContext(
568 deviceId,
569 SRObjectiveContext.ObjectiveType.FORWARDING)
570 )
571 );
572 });
573 }
574
575
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700576 private static class SRObjectiveContext implements ObjectiveContext {
577 enum ObjectiveType {
578 FILTER,
579 FORWARDING
580 }
581 final DeviceId deviceId;
582 final ObjectiveType type;
583
584 SRObjectiveContext(DeviceId deviceId, ObjectiveType type) {
585 this.deviceId = deviceId;
586 this.type = type;
587 }
588 @Override
589 public void onSuccess(Objective objective) {
590 log.debug("{} objective operation successful in device {}",
591 type.name(), deviceId);
592 }
593
594 @Override
595 public void onError(Objective objective, ObjectiveError error) {
596 log.warn("{} objective {} operation failed with error: {} in device {}",
597 type.name(), objective, error, deviceId);
598 }
599 }
600
sanghob35a6192015-04-01 13:05:26 -0700601}