blob: 4265d0cbd7d1787f56178ddd425363035edc0761 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sanghob35a6192015-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 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 Chane849c192016-01-11 18:28:54 -080026import org.onosproject.net.ConnectPoint;
Charles Chand2990362016-04-18 13:44:03 -070027import org.onosproject.net.flowobjective.DefaultObjectiveContext;
28import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan0b4e6182015-11-03 10:42:14 -080029import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
30import org.onosproject.segmentrouting.config.DeviceConfiguration;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070031import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sanghob35a6192015-04-01 13:05:26 -070032import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070033import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070034import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070035import org.onosproject.net.flow.DefaultTrafficSelector;
36import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070037import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-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 Das822c4e22015-10-23 10:51:11 -070045import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
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;
Charles Chane849c192016-01-11 18:28:54 -080052import java.util.Map;
sanghob35a6192015-04-01 13:05:26 -070053import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070054import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070055
56import static com.google.common.base.Preconditions.checkNotNull;
57
Charles Chane849c192016-01-11 18:28:54 -080058/**
59 * Populator of segment routing flow rules.
60 */
sanghob35a6192015-04-01 13:05:26 -070061public class RoutingRulePopulator {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070062 private static final Logger log = LoggerFactory
63 .getLogger(RoutingRulePopulator.class);
sanghob35a6192015-04-01 13:05:26 -070064
sangho20eff1d2015-04-13 15:15:58 -070065 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070066 private SegmentRoutingManager srManager;
67 private DeviceConfiguration config;
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 Chand2990362016-04-18 13:44:03 -0700117 ObjectiveContext context = new DefaultObjectiveContext(
118 (objective) -> log.debug("IP rule for host {} populated", hostIp),
119 (objective, error) ->
120 log.warn("Failed to populate IP rule for host {}: {}", hostIp, error));
121 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800122 rulePopulationCounter.incrementAndGet();
123 }
124
Charles Chane849c192016-01-11 18:28:54 -0800125 /**
126 * Removes IP rules for host when the host is gone.
127 *
128 * @param deviceId device ID of the device that host attaches to
129 * @param hostIp IP address of the host
130 * @param hostMac MAC address of the host
131 * @param outPort port that host attaches to
132 */
Charles Chan68aa62d2015-11-09 16:37:23 -0800133 public void revokeIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
134 MacAddress hostMac, PortNumber outPort) {
135 log.debug("Revoke IP table entry for host {} at {}:{}",
136 hostIp, deviceId, outPort);
137 ForwardingObjective.Builder fwdBuilder;
138 try {
139 fwdBuilder = getForwardingObjectiveBuilder(
140 deviceId, hostIp, hostMac, outPort);
141 } catch (DeviceConfigNotFoundException e) {
142 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
143 return;
144 }
Charles Chand2990362016-04-18 13:44:03 -0700145 ObjectiveContext context = new DefaultObjectiveContext(
146 (objective) -> log.debug("IP rule for host {} revoked", hostIp),
147 (objective, error) ->
148 log.warn("Failed to revoke IP rule for host {}: {}", hostIp, error));
149 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800150 }
151
152 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
153 DeviceId deviceId, Ip4Address hostIp,
154 MacAddress hostMac, PortNumber outPort)
155 throws DeviceConfigNotFoundException {
156 MacAddress deviceMac;
157 deviceMac = config.getDeviceMac(deviceId);
Charles Chan5270ed02016-01-30 23:22:37 -0800158 int priority;
Charles Chan0b4e6182015-11-03 10:42:14 -0800159
sanghob35a6192015-04-01 13:05:26 -0700160 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
161 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
162
sanghob35a6192015-04-01 13:05:26 -0700163 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan5270ed02016-01-30 23:22:37 -0800164 // Special case for default route
165 if (hostIp.isZero()) {
166 sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, 0));
167 priority = SegmentRoutingService.MIN_IP_PRIORITY;
168 } else {
169 Ip4Prefix hostIpPrefix = Ip4Prefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH);
170 sbuilder.matchIPDst(hostIpPrefix);
171 priority = getPriorityFromPrefix(hostIpPrefix);
172 }
Saurav Das4ce45962015-11-24 23:21:05 -0800173 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700174
sangho1e575652015-05-14 00:39:53 -0700175 tbuilder.deferred()
176 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800177 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700178 .setOutput(outPort);
sanghob35a6192015-04-01 13:05:26 -0700179 TrafficTreatment treatment = tbuilder.build();
Saurav Das4ce45962015-11-24 23:21:05 -0800180
181 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
182 // for switch pipelines that need it, provide outgoing vlan as metadata
183 VlanId outvlan = null;
184 Ip4Prefix subnet = srManager.deviceConfiguration.getPortSubnet(deviceId, outPort);
185 if (subnet == null) {
186 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
187 } else {
188 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
189 }
190 TrafficSelector meta = DefaultTrafficSelector.builder()
191 .matchVlanId(outvlan).build();
192 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
193 treatment, meta);
sanghob35a6192015-04-01 13:05:26 -0700194
Charles Chan68aa62d2015-11-09 16:37:23 -0800195 return DefaultForwardingObjective.builder()
Saurav Das4ce45962015-11-24 23:21:05 -0800196 .withSelector(selector)
197 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800198 .fromApp(srManager.appId).makePermanent()
Charles Chan5270ed02016-01-30 23:22:37 -0800199 .withPriority(priority)
200 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700201 }
202
203 /**
204 * Populates IP flow rules for the subnets of the destination router.
205 *
206 * @param deviceId switch ID to set the rules
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700207 * @param subnets subnet information
sanghob35a6192015-04-01 13:05:26 -0700208 * @param destSw destination switch ID
209 * @param nextHops next hop switch ID list
210 * @return true if all rules are set successfully, false otherwise
211 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700212 public boolean populateIpRuleForSubnet(DeviceId deviceId,
Charles Chan9f676b62015-10-29 14:58:10 -0700213 Set<Ip4Prefix> subnets,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700214 DeviceId destSw,
215 Set<DeviceId> nextHops) {
sanghob35a6192015-04-01 13:05:26 -0700216
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700217 for (IpPrefix subnet : subnets) {
sanghob35a6192015-04-01 13:05:26 -0700218 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
219 return false;
220 }
221 }
222
223 return true;
224 }
225
226 /**
227 * Populates IP flow rules for the router IP address.
228 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700229 * @param deviceId target device ID to set the rules
sanghob35a6192015-04-01 13:05:26 -0700230 * @param ipPrefix the IP address of the destination router
231 * @param destSw device ID of the destination router
232 * @param nextHops next hop switch ID list
233 * @return true if all rules are set successfully, false otherwise
234 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700235 public boolean populateIpRuleForRouter(DeviceId deviceId,
236 IpPrefix ipPrefix, DeviceId destSw,
237 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800238 int segmentId;
239 try {
240 segmentId = config.getSegmentId(destSw);
241 } catch (DeviceConfigNotFoundException e) {
242 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
243 return false;
244 }
sanghob35a6192015-04-01 13:05:26 -0700245
246 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan17d38f42016-02-05 13:33:54 -0800247 sbuilder.matchIPDst(ipPrefix);
sanghob35a6192015-04-01 13:05:26 -0700248 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan68aa62d2015-11-09 16:37:23 -0800249 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700250
Charles Chan68aa62d2015-11-09 16:37:23 -0800251 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
252 NeighborSet ns;
253 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700254
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700255 // If the next hop is the same as the final destination, then MPLS label
256 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700257 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800258 tbuilder.immediate().decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700259 ns = new NeighborSet(nextHops);
Charles Chan68aa62d2015-11-09 16:37:23 -0800260 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700261 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800262 ns = new NeighborSet(nextHops, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800263 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700264 }
265
Saurav Das8a0732e2015-11-20 15:27:53 -0800266 // setup metadata to pass to nextObjective - indicate the vlan on egress
267 // if needed by the switch pipeline. Since neighbor sets are always to
268 // other neighboring routers, there is no subnet assigned on those ports.
269 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
270 metabuilder.matchVlanId(
271 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
272
273 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
274 if (nextId <= 0) {
sangho834e4b02015-05-01 09:38:25 -0700275 log.warn("No next objective in {} for ns: {}", deviceId, ns);
276 return false;
277 }
278
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700279 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
280 .builder()
281 .fromApp(srManager.appId)
282 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800283 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700284 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800285 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700286 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800287 if (treatment != null) {
288 fwdBuilder.withTreatment(treatment);
289 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700290 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700291 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700292 ipPrefix,
293 deviceId);
Charles Chand2990362016-04-18 13:44:03 -0700294 ObjectiveContext context = new DefaultObjectiveContext(
295 (objective) -> log.debug("IP rule for router {} populated", ipPrefix),
296 (objective, error) ->
297 log.warn("Failed to populate IP rule for router {}: {}", ipPrefix, error));
298 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700299 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700300
301 return true;
302 }
303
sanghob35a6192015-04-01 13:05:26 -0700304 /**
Saurav Dasa07f2032015-10-19 14:37:36 -0700305 * Populates MPLS flow rules to all routers.
sanghob35a6192015-04-01 13:05:26 -0700306 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700307 * @param deviceId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700308 * @param destSwId destination switch device ID
309 * @param nextHops next hops switch ID list
310 * @return true if all rules are set successfully, false otherwise
311 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700312 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
313 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800314 int segmentId;
315 try {
316 segmentId = config.getSegmentId(destSwId);
317 } catch (DeviceConfigNotFoundException e) {
318 log.warn(e.getMessage() + " Aborting populateMplsRule.");
319 return false;
320 }
sanghob35a6192015-04-01 13:05:26 -0700321
322 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700323 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700324
325 // TODO Handle the case of Bos == false
sanghob35a6192015-04-01 13:05:26 -0700326 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das8a0732e2015-11-20 15:27:53 -0800327 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chan188ebf52015-12-23 00:15:11 -0800328 sbuilder.matchMplsBos(true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800329 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700330
Saurav Das8a0732e2015-11-20 15:27:53 -0800331 // setup metadata to pass to nextObjective - indicate the vlan on egress
332 // if needed by the switch pipeline. Since mpls next-hops are always to
333 // other neighboring routers, there is no subnet assigned on those ports.
334 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
335 metabuilder.matchVlanId(
336 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
337
338 // If the next hop is the destination router for the segment, do pop
sanghob35a6192015-04-01 13:05:26 -0700339 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700340 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800341 + "label {} in switch {} with pop", segmentId, deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700342
Saurav Das8a0732e2015-11-20 15:27:53 -0800343 // bos pop case (php)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700344 ForwardingObjective.Builder fwdObjBosBuilder =
345 getMplsForwardingObjective(deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700346 nextHops,
347 true,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700348 true,
Saurav Das8a0732e2015-11-20 15:27:53 -0800349 metabuilder.build());
350 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700351 return false;
352 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800353 fwdObjBuilders.add(fwdObjBosBuilder);
354
355 // XXX not-bos pop case, SR app multi-label not implemented yet
356 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
357 getMplsForwardingObjective(deviceId,
358 nextHops,
359 true,
360 false);*/
361
sanghob35a6192015-04-01 13:05:26 -0700362 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800363 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700364 log.debug("Installing MPLS forwarding objective for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800365 + "label {} in switch {} without pop", segmentId, deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700366
Saurav Das8a0732e2015-11-20 15:27:53 -0800367 // continue case with bos - this does get triggered in edge routers
368 // and in core routers - driver can handle depending on availability
369 // of MPLS ECMP or not
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700370 ForwardingObjective.Builder fwdObjBosBuilder =
371 getMplsForwardingObjective(deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700372 nextHops,
373 false,
Saurav Das8a0732e2015-11-20 15:27:53 -0800374 true,
375 metabuilder.build());
376 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700377 return false;
378 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800379 fwdObjBuilders.add(fwdObjBosBuilder);
380
381 // XXX continue case with not-bos - SR app multi label not implemented yet
382 // also requires MPLS ECMP
383 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
384 getMplsForwardingObjective(deviceId,
385 nextHops,
386 false,
387 false); */
388
sanghob35a6192015-04-01 13:05:26 -0700389 }
390
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700391 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
392 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
393 .makePermanent()).withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800394 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700395 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chand2990362016-04-18 13:44:03 -0700396 ObjectiveContext context = new DefaultObjectiveContext(
397 (objective) -> log.debug("MPLS rule for SID {} populated", segmentId),
398 (objective, error) ->
399 log.warn("Failed to populate MPLS rule for SID {}: {}", segmentId, error));
400 srManager.flowObjectiveService.forward(deviceId, fwdObjBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700401 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700402 }
403
404 return true;
405 }
406
Saurav Das8a0732e2015-11-20 15:27:53 -0800407 private ForwardingObjective.Builder getMplsForwardingObjective(
408 DeviceId deviceId,
409 Set<DeviceId> nextHops,
410 boolean phpRequired,
411 boolean isBos,
412 TrafficSelector meta) {
413
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700414 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
415 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700416
417 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
418
419 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800420 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700421 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700422 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700423 if (isBos) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800424 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
425 .decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700426 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800427 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
428 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700429 }
430 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800431 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700432 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700433 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700434 }
435
Saurav Das8a0732e2015-11-20 15:27:53 -0800436 // All forwarding is via ECMP group, the metadata informs the driver
437 // that the next-Objective will be used by MPLS flows. In other words,
438 // MPLS ECMP is requested. It is up to the driver to decide if these
439 // packets will be hashed or not.
440 fwdBuilder.withTreatment(tbuilder.build());
441 NeighborSet ns = new NeighborSet(nextHops);
442 log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}",
443 deviceId, ns);
444
445 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
446 if (nextId <= 0) {
447 log.warn("No next objective in {} for ns: {}", deviceId, ns);
448 return null;
sanghob35a6192015-04-01 13:05:26 -0700449 }
450
Saurav Das8a0732e2015-11-20 15:27:53 -0800451 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700452 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700453 }
454
455 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700456 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700457 * dstMac corresponding to the router's MAC address. For those pipelines
458 * that need to internally assign vlans to untagged packets, this method
459 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700460 * <p>
461 * Note that the vlan assignment is only done by the master-instance for a switch.
462 * However we send the filtering objective from slave-instances as well, so
463 * that drivers can obtain other information (like Router MAC and IP).
sanghob35a6192015-04-01 13:05:26 -0700464 *
Saurav Das822c4e22015-10-23 10:51:11 -0700465 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700466 */
Saurav Das822c4e22015-10-23 10:51:11 -0700467 public void populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700468 log.debug("Installing per-port filtering objective for untagged "
469 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800470
471 MacAddress deviceMac;
472 try {
473 deviceMac = config.getDeviceMac(deviceId);
474 } catch (DeviceConfigNotFoundException e) {
475 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
476 return;
477 }
478
Saurav Das0e99e2b2015-10-28 12:39:42 -0700479 for (Port port : srManager.deviceService.getPorts(deviceId)) {
Charles Chand2990362016-04-18 13:44:03 -0700480 ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number());
Charles Chanf2565a92016-02-10 20:46:58 -0800481 // TODO: Handles dynamic port events when we are ready for dynamic config
Charles Chand2990362016-04-18 13:44:03 -0700482 if (!srManager.deviceConfiguration.suppressSubnet().contains(connectPoint) &&
Charles Chan5270ed02016-01-30 23:22:37 -0800483 port.isEnabled()) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700484 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
485 VlanId assignedVlan = (portSubnet == null)
486 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
487 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan0b4e6182015-11-03 10:42:14 -0800488
Saurav Das0e99e2b2015-10-28 12:39:42 -0700489 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
490 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan0b4e6182015-11-03 10:42:14 -0800491 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chane849c192016-01-11 18:28:54 -0800492 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800493 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Das837e0bb2015-10-30 17:45:38 -0700494 // vlan assignment is valid only if this instance is master
495 if (srManager.mastershipService.isLocalMaster(deviceId)) {
496 TrafficTreatment tt = DefaultTrafficTreatment.builder()
497 .pushVlan().setVlanId(assignedVlan).build();
Saurav Das4ce45962015-11-24 23:21:05 -0800498 fob.withMeta(tt);
Saurav Das837e0bb2015-10-30 17:45:38 -0700499 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700500 fob.permit().fromApp(srManager.appId);
Charles Chand2990362016-04-18 13:44:03 -0700501 ObjectiveContext context = new DefaultObjectiveContext(
502 (objective) -> log.debug("Filter for {} populated", connectPoint),
503 (objective, error) ->
504 log.warn("Failed to populate filter for {}: {}", connectPoint, error));
505 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Saurav Das0e99e2b2015-10-28 12:39:42 -0700506 }
507 }
sanghob35a6192015-04-01 13:05:26 -0700508 }
509
510 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700511 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700512 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700513 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700514 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700515 *
Saurav Das822c4e22015-10-23 10:51:11 -0700516 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700517 */
Saurav Das822c4e22015-10-23 10:51:11 -0700518 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800519 Ip4Address routerIp;
520 try {
521 routerIp = config.getRouterIp(deviceId);
522 } catch (DeviceConfigNotFoundException e) {
523 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
524 return;
525 }
526
Saurav Das837e0bb2015-10-30 17:45:38 -0700527 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
528 log.debug("Not installing port-IP punts - not the master for dev:{} ",
529 deviceId);
530 return;
531 }
Saurav Das822c4e22015-10-23 10:51:11 -0700532 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Charles Chan5270ed02016-01-30 23:22:37 -0800533 Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId));
Charles Chan0b4e6182015-11-03 10:42:14 -0800534 allIps.add(routerIp);
Saurav Das837e0bb2015-10-30 17:45:38 -0700535 for (Ip4Address ipaddr : allIps) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800536 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
537 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
538 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
539 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das822c4e22015-10-23 10:51:11 -0700540 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chan68aa62d2015-11-09 16:37:23 -0800541 tbuilder.setOutput(PortNumber.CONTROLLER);
542 puntIp.withSelector(sbuilder.build());
543 puntIp.withTreatment(tbuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700544 puntIp.withFlag(Flag.VERSATILE)
Charles Chan5270ed02016-01-30 23:22:37 -0800545 .withPriority(SegmentRoutingService.HIGHEST_PRIORITY)
Saurav Das822c4e22015-10-23 10:51:11 -0700546 .makePermanent()
547 .fromApp(srManager.appId);
Charles Chand2990362016-04-18 13:44:03 -0700548 ObjectiveContext context = new DefaultObjectiveContext(
549 (objective) -> log.debug("IP punt rule for {} populated", ipaddr),
550 (objective, error) ->
551 log.warn("Failed to populate IP punt rule for {}: {}", ipaddr, error));
552 srManager.flowObjectiveService.forward(deviceId, puntIp.add(context));
Saurav Das822c4e22015-10-23 10:51:11 -0700553 }
sanghob35a6192015-04-01 13:05:26 -0700554 }
555
Charles Chan68aa62d2015-11-09 16:37:23 -0800556 /**
557 * Populates a forwarding objective to send packets that miss other high
558 * priority Bridging Table entries to a group that contains all ports of
559 * its subnet.
560 *
561 * Note: We assume that packets sending from the edge switches to the hosts
562 * have untagged VLAN.
563 * The VLAN tag will be popped later in the flooding group.
564 *
565 * @param deviceId switch ID to set the rules
566 */
567 public void populateSubnetBroadcastRule(DeviceId deviceId) {
568 config.getSubnets(deviceId).forEach(subnet -> {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800569 if (subnet.prefixLength() == 0 ||
570 subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
571 return;
572 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800573 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
574 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
575
Saurav Das4ce45962015-11-24 23:21:05 -0800576 if (nextId < 0 || vlanId == null) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800577 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
578 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -0800579 return;
580 }
581
Charles Chan68aa62d2015-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)
Charles Chan5270ed02016-01-30 23:22:37 -0800593 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800594 .fromApp(srManager.appId)
595 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700596 ObjectiveContext context = new DefaultObjectiveContext(
597 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
598 (objective, error) ->
599 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
600 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800601 });
602 }
603
Charles Chane849c192016-01-11 18:28:54 -0800604 /**
605 * Creates a filtering objective to permit VLAN cross-connect traffic.
606 *
607 * @param deviceId the DPID of the switch
608 */
609 public void populateXConnectVlanFilters(DeviceId deviceId) {
610 Map<VlanId, List<ConnectPoint>> xConnectsForDevice =
611 config.getXConnects();
612 xConnectsForDevice.forEach((vlanId, connectPoints) -> {
613 // Only proceed the xConnect for given device
614 for (ConnectPoint connectPoint : connectPoints) {
615 if (!connectPoint.deviceId().equals(deviceId)) {
616 return;
617 }
618 }
619
620 connectPoints.forEach(connectPoint -> {
621 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
622 fob.withKey(Criteria.matchInPort(connectPoint.port()))
623 .addCondition(Criteria.matchVlanId(vlanId))
624 .addCondition(Criteria.matchEthDst(MacAddress.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800625 .withPriority(SegmentRoutingService.XCONNECT_PRIORITY);
Charles Chane849c192016-01-11 18:28:54 -0800626 fob.permit().fromApp(srManager.appId);
Charles Chand2990362016-04-18 13:44:03 -0700627 ObjectiveContext context = new DefaultObjectiveContext(
628 (objective) -> log.debug("XConnect filter for {} populated", connectPoint),
629 (objective, error) ->
630 log.warn("Failed to populate xconnect filter for {}: {}", connectPoint, error));
631 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Charles Chane849c192016-01-11 18:28:54 -0800632 });
633 });
634 }
635
636 /**
637 * Populates a forwarding objective that points the VLAN cross-connect
638 * packets to a broadcast group.
639 *
640 * @param deviceId switch ID to set the rules
641 */
642 public void populateXConnectBroadcastRule(DeviceId deviceId) {
643 Map<VlanId, List<ConnectPoint>> xConnects =
644 config.getXConnects();
645 xConnects.forEach((vlanId, connectPoints) -> {
646 // Only proceed the xConnect for given device
647 for (ConnectPoint connectPoint : connectPoints) {
648 if (!connectPoint.deviceId().equals(deviceId)) {
649 return;
650 }
651 }
652
653 int nextId = srManager.getXConnectNextObjectiveId(deviceId, vlanId);
654 if (nextId < 0) {
655 log.error("Cannot install cross-connect broadcast rule in dev:{} " +
656 "due to missing nextId:{}", deviceId, nextId);
657 return;
658 }
659
660 /*
661 * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE
662 * as the VLAN cross-connect broadcast rules
663 */
664 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
665 sbuilder.matchVlanId(vlanId);
666 sbuilder.matchEthDst(MacAddress.NONE);
667
668 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
669 fob.withFlag(Flag.SPECIFIC)
670 .withSelector(sbuilder.build())
671 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800672 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)
Charles Chane849c192016-01-11 18:28:54 -0800673 .fromApp(srManager.appId)
674 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700675 ObjectiveContext context = new DefaultObjectiveContext(
676 (objective) -> log.debug("XConnect rule for {} populated", xConnects),
677 (objective, error) ->
678 log.warn("Failed to populate xconnect rule for {}: {}", xConnects, error));
679 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chane849c192016-01-11 18:28:54 -0800680 });
681 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800682
Charles Chan5270ed02016-01-30 23:22:37 -0800683 private int getPriorityFromPrefix(IpPrefix prefix) {
684 return (prefix.isIp4()) ?
685 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
686 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700687 }
sanghob35a6192015-04-01 13:05:26 -0700688}