blob: 00b3c011b40c0f1b0a8183b8688ea9ad45f22494 [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 }
Saurav Das59232cf2016-04-27 18:35:50 -0700117 if (fwdBuilder == null) {
118 log.warn("Aborting host routing table entries due "
119 + "to error for dev:{} host:{}", deviceId, hostIp);
120 return;
121 }
Charles Chand2990362016-04-18 13:44:03 -0700122 ObjectiveContext context = new DefaultObjectiveContext(
123 (objective) -> log.debug("IP rule for host {} populated", hostIp),
124 (objective, error) ->
125 log.warn("Failed to populate IP rule for host {}: {}", hostIp, error));
126 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800127 rulePopulationCounter.incrementAndGet();
128 }
129
Charles Chane849c192016-01-11 18:28:54 -0800130 /**
131 * Removes IP rules for host when the host is gone.
132 *
133 * @param deviceId device ID of the device that host attaches to
134 * @param hostIp IP address of the host
135 * @param hostMac MAC address of the host
136 * @param outPort port that host attaches to
137 */
Charles Chan68aa62d2015-11-09 16:37:23 -0800138 public void revokeIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
139 MacAddress hostMac, PortNumber outPort) {
140 log.debug("Revoke IP table entry for host {} at {}:{}",
141 hostIp, deviceId, outPort);
142 ForwardingObjective.Builder fwdBuilder;
143 try {
144 fwdBuilder = getForwardingObjectiveBuilder(
145 deviceId, hostIp, hostMac, outPort);
146 } catch (DeviceConfigNotFoundException e) {
147 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
148 return;
149 }
Charles Chand2990362016-04-18 13:44:03 -0700150 ObjectiveContext context = new DefaultObjectiveContext(
151 (objective) -> log.debug("IP rule for host {} revoked", hostIp),
152 (objective, error) ->
153 log.warn("Failed to revoke IP rule for host {}: {}", hostIp, error));
154 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800155 }
156
157 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
158 DeviceId deviceId, Ip4Address hostIp,
159 MacAddress hostMac, PortNumber outPort)
160 throws DeviceConfigNotFoundException {
161 MacAddress deviceMac;
162 deviceMac = config.getDeviceMac(deviceId);
Charles Chan5270ed02016-01-30 23:22:37 -0800163 int priority;
Charles Chan0b4e6182015-11-03 10:42:14 -0800164
sanghob35a6192015-04-01 13:05:26 -0700165 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
166 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
167
sanghob35a6192015-04-01 13:05:26 -0700168 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan5270ed02016-01-30 23:22:37 -0800169 // Special case for default route
170 if (hostIp.isZero()) {
171 sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, 0));
172 priority = SegmentRoutingService.MIN_IP_PRIORITY;
173 } else {
174 Ip4Prefix hostIpPrefix = Ip4Prefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH);
175 sbuilder.matchIPDst(hostIpPrefix);
176 priority = getPriorityFromPrefix(hostIpPrefix);
177 }
Saurav Das4ce45962015-11-24 23:21:05 -0800178 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700179
sangho1e575652015-05-14 00:39:53 -0700180 tbuilder.deferred()
181 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800182 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700183 .setOutput(outPort);
sanghob35a6192015-04-01 13:05:26 -0700184 TrafficTreatment treatment = tbuilder.build();
Saurav Das4ce45962015-11-24 23:21:05 -0800185
186 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
187 // for switch pipelines that need it, provide outgoing vlan as metadata
188 VlanId outvlan = null;
189 Ip4Prefix subnet = srManager.deviceConfiguration.getPortSubnet(deviceId, outPort);
190 if (subnet == null) {
191 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
192 } else {
193 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
194 }
195 TrafficSelector meta = DefaultTrafficSelector.builder()
196 .matchVlanId(outvlan).build();
197 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
198 treatment, meta);
Saurav Das59232cf2016-04-27 18:35:50 -0700199 if (portNextObjId == -1) {
200 // warning log will come from getPortNextObjective method
201 return null;
202 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800203 return DefaultForwardingObjective.builder()
Saurav Das4ce45962015-11-24 23:21:05 -0800204 .withSelector(selector)
205 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800206 .fromApp(srManager.appId).makePermanent()
Charles Chan5270ed02016-01-30 23:22:37 -0800207 .withPriority(priority)
208 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700209 }
210
211 /**
212 * Populates IP flow rules for the subnets of the destination router.
213 *
214 * @param deviceId switch ID to set the rules
Charles Chan93e71ba2016-04-29 14:38:22 -0700215 * @param subnets subnet being added
sanghob35a6192015-04-01 13:05:26 -0700216 * @param destSw destination switch ID
217 * @param nextHops next hop switch ID list
218 * @return true if all rules are set successfully, false otherwise
219 */
Charles Chan93e71ba2016-04-29 14:38:22 -0700220 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<Ip4Prefix> subnets,
221 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700222 for (IpPrefix subnet : subnets) {
sanghob35a6192015-04-01 13:05:26 -0700223 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
224 return false;
225 }
226 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700227 return true;
228 }
sanghob35a6192015-04-01 13:05:26 -0700229
Charles Chan93e71ba2016-04-29 14:38:22 -0700230 /**
231 * Revokes IP flow rules for the subnets.
232 *
233 * @param subnets subnet being removed
234 * @return true if all rules are removed successfully, false otherwise
235 */
236 public boolean revokeIpRuleForSubnet(Set<Ip4Prefix> subnets) {
237 for (IpPrefix subnet : subnets) {
238 if (!revokeIpRuleForRouter(subnet)) {
239 return false;
240 }
241 }
sanghob35a6192015-04-01 13:05:26 -0700242 return true;
243 }
244
245 /**
246 * Populates IP flow rules for the router IP address.
247 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700248 * @param deviceId target device ID to set the rules
sanghob35a6192015-04-01 13:05:26 -0700249 * @param ipPrefix the IP address of the destination router
250 * @param destSw device ID of the destination router
251 * @param nextHops next hop switch ID list
252 * @return true if all rules are set successfully, false otherwise
253 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700254 public boolean populateIpRuleForRouter(DeviceId deviceId,
255 IpPrefix ipPrefix, DeviceId destSw,
256 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800257 int segmentId;
258 try {
259 segmentId = config.getSegmentId(destSw);
260 } catch (DeviceConfigNotFoundException e) {
261 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
262 return false;
263 }
sanghob35a6192015-04-01 13:05:26 -0700264
265 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan17d38f42016-02-05 13:33:54 -0800266 sbuilder.matchIPDst(ipPrefix);
sanghob35a6192015-04-01 13:05:26 -0700267 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan68aa62d2015-11-09 16:37:23 -0800268 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700269
Charles Chan68aa62d2015-11-09 16:37:23 -0800270 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
271 NeighborSet ns;
272 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700273
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700274 // If the next hop is the same as the final destination, then MPLS label
275 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700276 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800277 tbuilder.immediate().decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700278 ns = new NeighborSet(nextHops);
Charles Chan68aa62d2015-11-09 16:37:23 -0800279 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700280 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800281 ns = new NeighborSet(nextHops, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800282 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700283 }
284
Saurav Das8a0732e2015-11-20 15:27:53 -0800285 // setup metadata to pass to nextObjective - indicate the vlan on egress
286 // if needed by the switch pipeline. Since neighbor sets are always to
287 // other neighboring routers, there is no subnet assigned on those ports.
288 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
289 metabuilder.matchVlanId(
290 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
291
292 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
293 if (nextId <= 0) {
sangho834e4b02015-05-01 09:38:25 -0700294 log.warn("No next objective in {} for ns: {}", deviceId, ns);
295 return false;
296 }
297
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700298 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
299 .builder()
300 .fromApp(srManager.appId)
301 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800302 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700303 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800304 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700305 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800306 if (treatment != null) {
307 fwdBuilder.withTreatment(treatment);
308 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700309 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700310 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700311 ipPrefix,
312 deviceId);
Charles Chand2990362016-04-18 13:44:03 -0700313 ObjectiveContext context = new DefaultObjectiveContext(
314 (objective) -> log.debug("IP rule for router {} populated", ipPrefix),
315 (objective, error) ->
316 log.warn("Failed to populate IP rule for router {}: {}", ipPrefix, error));
317 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700318 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700319
320 return true;
321 }
322
sanghob35a6192015-04-01 13:05:26 -0700323 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700324 * Revokes IP flow rules for the router IP address.
325 *
326 * @param ipPrefix the IP address of the destination router
327 * @return true if all rules are removed successfully, false otherwise
328 */
329 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
330 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
331 sbuilder.matchIPDst(ipPrefix);
332 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
333 TrafficSelector selector = sbuilder.build();
334 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
335
336 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
337 .builder()
338 .fromApp(srManager.appId)
339 .makePermanent()
340 .withSelector(selector)
341 .withTreatment(dummyTreatment)
342 .withPriority(getPriorityFromPrefix(ipPrefix))
343 .withFlag(ForwardingObjective.Flag.SPECIFIC);
344
345 ObjectiveContext context = new DefaultObjectiveContext(
346 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
347 (objective, error) ->
348 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
349
350 srManager.deviceService.getAvailableDevices().forEach(device -> {
351 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
352 });
353
354 return true;
355 }
356
357 /**
Saurav Dasa07f2032015-10-19 14:37:36 -0700358 * Populates MPLS flow rules to all routers.
sanghob35a6192015-04-01 13:05:26 -0700359 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700360 * @param deviceId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700361 * @param destSwId destination switch device ID
362 * @param nextHops next hops switch ID list
363 * @return true if all rules are set successfully, false otherwise
364 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700365 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
366 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800367 int segmentId;
368 try {
369 segmentId = config.getSegmentId(destSwId);
370 } catch (DeviceConfigNotFoundException e) {
371 log.warn(e.getMessage() + " Aborting populateMplsRule.");
372 return false;
373 }
sanghob35a6192015-04-01 13:05:26 -0700374
375 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700376 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700377
378 // TODO Handle the case of Bos == false
sanghob35a6192015-04-01 13:05:26 -0700379 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das8a0732e2015-11-20 15:27:53 -0800380 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chan188ebf52015-12-23 00:15:11 -0800381 sbuilder.matchMplsBos(true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800382 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700383
Saurav Das8a0732e2015-11-20 15:27:53 -0800384 // setup metadata to pass to nextObjective - indicate the vlan on egress
385 // if needed by the switch pipeline. Since mpls next-hops are always to
386 // other neighboring routers, there is no subnet assigned on those ports.
387 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
388 metabuilder.matchVlanId(
389 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
390
391 // If the next hop is the destination router for the segment, do pop
sanghob35a6192015-04-01 13:05:26 -0700392 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700393 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800394 + "label {} in switch {} with pop", segmentId, deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700395
Saurav Das8a0732e2015-11-20 15:27:53 -0800396 // bos pop case (php)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700397 ForwardingObjective.Builder fwdObjBosBuilder =
398 getMplsForwardingObjective(deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700399 nextHops,
400 true,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700401 true,
Saurav Das8a0732e2015-11-20 15:27:53 -0800402 metabuilder.build());
403 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700404 return false;
405 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800406 fwdObjBuilders.add(fwdObjBosBuilder);
407
408 // XXX not-bos pop case, SR app multi-label not implemented yet
409 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
410 getMplsForwardingObjective(deviceId,
411 nextHops,
412 true,
413 false);*/
414
sanghob35a6192015-04-01 13:05:26 -0700415 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800416 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700417 log.debug("Installing MPLS forwarding objective for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800418 + "label {} in switch {} without pop", segmentId, deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700419
Saurav Das8a0732e2015-11-20 15:27:53 -0800420 // continue case with bos - this does get triggered in edge routers
421 // and in core routers - driver can handle depending on availability
422 // of MPLS ECMP or not
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700423 ForwardingObjective.Builder fwdObjBosBuilder =
424 getMplsForwardingObjective(deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700425 nextHops,
426 false,
Saurav Das8a0732e2015-11-20 15:27:53 -0800427 true,
428 metabuilder.build());
429 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700430 return false;
431 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800432 fwdObjBuilders.add(fwdObjBosBuilder);
433
434 // XXX continue case with not-bos - SR app multi label not implemented yet
435 // also requires MPLS ECMP
436 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
437 getMplsForwardingObjective(deviceId,
438 nextHops,
439 false,
440 false); */
441
sanghob35a6192015-04-01 13:05:26 -0700442 }
443
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700444 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
445 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
446 .makePermanent()).withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800447 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700448 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chand2990362016-04-18 13:44:03 -0700449 ObjectiveContext context = new DefaultObjectiveContext(
450 (objective) -> log.debug("MPLS rule for SID {} populated", segmentId),
451 (objective, error) ->
452 log.warn("Failed to populate MPLS rule for SID {}: {}", segmentId, error));
453 srManager.flowObjectiveService.forward(deviceId, fwdObjBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700454 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700455 }
456
457 return true;
458 }
459
Saurav Das8a0732e2015-11-20 15:27:53 -0800460 private ForwardingObjective.Builder getMplsForwardingObjective(
461 DeviceId deviceId,
462 Set<DeviceId> nextHops,
463 boolean phpRequired,
464 boolean isBos,
465 TrafficSelector meta) {
466
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700467 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
468 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700469
470 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
471
472 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800473 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700474 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700475 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700476 if (isBos) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800477 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
478 .decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700479 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800480 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
481 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700482 }
483 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800484 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700485 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700486 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700487 }
488
Saurav Das8a0732e2015-11-20 15:27:53 -0800489 // All forwarding is via ECMP group, the metadata informs the driver
490 // that the next-Objective will be used by MPLS flows. In other words,
491 // MPLS ECMP is requested. It is up to the driver to decide if these
492 // packets will be hashed or not.
493 fwdBuilder.withTreatment(tbuilder.build());
494 NeighborSet ns = new NeighborSet(nextHops);
495 log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}",
496 deviceId, ns);
497
498 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
499 if (nextId <= 0) {
500 log.warn("No next objective in {} for ns: {}", deviceId, ns);
501 return null;
sanghob35a6192015-04-01 13:05:26 -0700502 }
503
Saurav Das8a0732e2015-11-20 15:27:53 -0800504 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700505 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700506 }
507
508 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700509 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700510 * dstMac corresponding to the router's MAC address. For those pipelines
511 * that need to internally assign vlans to untagged packets, this method
512 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700513 * <p>
514 * Note that the vlan assignment is only done by the master-instance for a switch.
515 * However we send the filtering objective from slave-instances as well, so
516 * that drivers can obtain other information (like Router MAC and IP).
sanghob35a6192015-04-01 13:05:26 -0700517 *
Saurav Das822c4e22015-10-23 10:51:11 -0700518 * @param deviceId the switch dpid for the router
Charles Chan93e71ba2016-04-29 14:38:22 -0700519 * @return true if operation succeeds
sanghob35a6192015-04-01 13:05:26 -0700520 */
Saurav Das59232cf2016-04-27 18:35:50 -0700521 public boolean populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700522 log.debug("Installing per-port filtering objective for untagged "
523 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800524
525 MacAddress deviceMac;
526 try {
527 deviceMac = config.getDeviceMac(deviceId);
528 } catch (DeviceConfigNotFoundException e) {
529 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
Saurav Das59232cf2016-04-27 18:35:50 -0700530 return false;
Charles Chan0b4e6182015-11-03 10:42:14 -0800531 }
532
Saurav Das59232cf2016-04-27 18:35:50 -0700533 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
534 if (devPorts != null && devPorts.size() == 0) {
535 log.warn("Device {} ports not available. Unable to add MacVlan filters",
536 deviceId);
537 return false;
538 }
539
540 for (Port port : devPorts) {
Charles Chand2990362016-04-18 13:44:03 -0700541 ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number());
Charles Chanf2565a92016-02-10 20:46:58 -0800542 // TODO: Handles dynamic port events when we are ready for dynamic config
Charles Chand2990362016-04-18 13:44:03 -0700543 if (!srManager.deviceConfiguration.suppressSubnet().contains(connectPoint) &&
Charles Chan5270ed02016-01-30 23:22:37 -0800544 port.isEnabled()) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700545 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
546 VlanId assignedVlan = (portSubnet == null)
547 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
548 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan0b4e6182015-11-03 10:42:14 -0800549
Saurav Das0e99e2b2015-10-28 12:39:42 -0700550 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
551 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan0b4e6182015-11-03 10:42:14 -0800552 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chane849c192016-01-11 18:28:54 -0800553 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800554 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Das837e0bb2015-10-30 17:45:38 -0700555 // vlan assignment is valid only if this instance is master
556 if (srManager.mastershipService.isLocalMaster(deviceId)) {
557 TrafficTreatment tt = DefaultTrafficTreatment.builder()
558 .pushVlan().setVlanId(assignedVlan).build();
Saurav Das4ce45962015-11-24 23:21:05 -0800559 fob.withMeta(tt);
Saurav Das837e0bb2015-10-30 17:45:38 -0700560 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700561 fob.permit().fromApp(srManager.appId);
Saurav Das59232cf2016-04-27 18:35:50 -0700562 log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port);
Charles Chand2990362016-04-18 13:44:03 -0700563 ObjectiveContext context = new DefaultObjectiveContext(
564 (objective) -> log.debug("Filter for {} populated", connectPoint),
565 (objective, error) ->
566 log.warn("Failed to populate filter for {}: {}", connectPoint, error));
567 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Saurav Das0e99e2b2015-10-28 12:39:42 -0700568 }
569 }
Saurav Das59232cf2016-04-27 18:35:50 -0700570 return true;
sanghob35a6192015-04-01 13:05:26 -0700571 }
572
573 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700574 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700575 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700576 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700577 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700578 *
Saurav Das822c4e22015-10-23 10:51:11 -0700579 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700580 */
Saurav Das822c4e22015-10-23 10:51:11 -0700581 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800582 Ip4Address routerIp;
583 try {
584 routerIp = config.getRouterIp(deviceId);
585 } catch (DeviceConfigNotFoundException e) {
586 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
587 return;
588 }
589
Saurav Das837e0bb2015-10-30 17:45:38 -0700590 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
591 log.debug("Not installing port-IP punts - not the master for dev:{} ",
592 deviceId);
593 return;
594 }
Saurav Das822c4e22015-10-23 10:51:11 -0700595 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Charles Chan5270ed02016-01-30 23:22:37 -0800596 Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId));
Charles Chan0b4e6182015-11-03 10:42:14 -0800597 allIps.add(routerIp);
Saurav Das837e0bb2015-10-30 17:45:38 -0700598 for (Ip4Address ipaddr : allIps) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800599 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
600 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
601 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
602 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das822c4e22015-10-23 10:51:11 -0700603 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chan68aa62d2015-11-09 16:37:23 -0800604 tbuilder.setOutput(PortNumber.CONTROLLER);
605 puntIp.withSelector(sbuilder.build());
606 puntIp.withTreatment(tbuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700607 puntIp.withFlag(Flag.VERSATILE)
Charles Chan5270ed02016-01-30 23:22:37 -0800608 .withPriority(SegmentRoutingService.HIGHEST_PRIORITY)
Saurav Das822c4e22015-10-23 10:51:11 -0700609 .makePermanent()
610 .fromApp(srManager.appId);
Charles Chand2990362016-04-18 13:44:03 -0700611 ObjectiveContext context = new DefaultObjectiveContext(
612 (objective) -> log.debug("IP punt rule for {} populated", ipaddr),
613 (objective, error) ->
614 log.warn("Failed to populate IP punt rule for {}: {}", ipaddr, error));
615 srManager.flowObjectiveService.forward(deviceId, puntIp.add(context));
Saurav Das822c4e22015-10-23 10:51:11 -0700616 }
sanghob35a6192015-04-01 13:05:26 -0700617 }
618
Charles Chan68aa62d2015-11-09 16:37:23 -0800619 /**
620 * Populates a forwarding objective to send packets that miss other high
621 * priority Bridging Table entries to a group that contains all ports of
622 * its subnet.
623 *
624 * Note: We assume that packets sending from the edge switches to the hosts
625 * have untagged VLAN.
626 * The VLAN tag will be popped later in the flooding group.
627 *
628 * @param deviceId switch ID to set the rules
629 */
630 public void populateSubnetBroadcastRule(DeviceId deviceId) {
631 config.getSubnets(deviceId).forEach(subnet -> {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800632 if (subnet.prefixLength() == 0 ||
633 subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
634 return;
635 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800636 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
637 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
638
Saurav Das4ce45962015-11-24 23:21:05 -0800639 if (nextId < 0 || vlanId == null) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800640 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
641 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -0800642 return;
643 }
644
Charles Chan68aa62d2015-11-09 16:37:23 -0800645 /* Driver should treat objective with MacAddress.NONE as the
646 * subnet broadcast rule
647 */
648 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
649 sbuilder.matchVlanId(vlanId);
650 sbuilder.matchEthDst(MacAddress.NONE);
651
652 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
653 fob.withFlag(Flag.SPECIFIC)
654 .withSelector(sbuilder.build())
655 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800656 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800657 .fromApp(srManager.appId)
658 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700659 ObjectiveContext context = new DefaultObjectiveContext(
660 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
661 (objective, error) ->
662 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
663 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800664 });
665 }
666
Charles Chane849c192016-01-11 18:28:54 -0800667 /**
668 * Creates a filtering objective to permit VLAN cross-connect traffic.
669 *
670 * @param deviceId the DPID of the switch
671 */
672 public void populateXConnectVlanFilters(DeviceId deviceId) {
673 Map<VlanId, List<ConnectPoint>> xConnectsForDevice =
674 config.getXConnects();
675 xConnectsForDevice.forEach((vlanId, connectPoints) -> {
676 // Only proceed the xConnect for given device
677 for (ConnectPoint connectPoint : connectPoints) {
678 if (!connectPoint.deviceId().equals(deviceId)) {
679 return;
680 }
681 }
682
683 connectPoints.forEach(connectPoint -> {
684 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
685 fob.withKey(Criteria.matchInPort(connectPoint.port()))
686 .addCondition(Criteria.matchVlanId(vlanId))
687 .addCondition(Criteria.matchEthDst(MacAddress.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800688 .withPriority(SegmentRoutingService.XCONNECT_PRIORITY);
Charles Chane849c192016-01-11 18:28:54 -0800689 fob.permit().fromApp(srManager.appId);
Charles Chand2990362016-04-18 13:44:03 -0700690 ObjectiveContext context = new DefaultObjectiveContext(
691 (objective) -> log.debug("XConnect filter for {} populated", connectPoint),
692 (objective, error) ->
693 log.warn("Failed to populate xconnect filter for {}: {}", connectPoint, error));
694 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Charles Chane849c192016-01-11 18:28:54 -0800695 });
696 });
697 }
698
699 /**
700 * Populates a forwarding objective that points the VLAN cross-connect
701 * packets to a broadcast group.
702 *
703 * @param deviceId switch ID to set the rules
704 */
705 public void populateXConnectBroadcastRule(DeviceId deviceId) {
706 Map<VlanId, List<ConnectPoint>> xConnects =
707 config.getXConnects();
708 xConnects.forEach((vlanId, connectPoints) -> {
709 // Only proceed the xConnect for given device
710 for (ConnectPoint connectPoint : connectPoints) {
711 if (!connectPoint.deviceId().equals(deviceId)) {
712 return;
713 }
714 }
715
716 int nextId = srManager.getXConnectNextObjectiveId(deviceId, vlanId);
717 if (nextId < 0) {
718 log.error("Cannot install cross-connect broadcast rule in dev:{} " +
719 "due to missing nextId:{}", deviceId, nextId);
720 return;
721 }
722
723 /*
724 * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE
725 * as the VLAN cross-connect broadcast rules
726 */
727 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
728 sbuilder.matchVlanId(vlanId);
729 sbuilder.matchEthDst(MacAddress.NONE);
730
731 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
732 fob.withFlag(Flag.SPECIFIC)
733 .withSelector(sbuilder.build())
734 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800735 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)
Charles Chane849c192016-01-11 18:28:54 -0800736 .fromApp(srManager.appId)
737 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700738 ObjectiveContext context = new DefaultObjectiveContext(
739 (objective) -> log.debug("XConnect rule for {} populated", xConnects),
740 (objective, error) ->
741 log.warn("Failed to populate xconnect rule for {}: {}", xConnects, error));
742 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chane849c192016-01-11 18:28:54 -0800743 });
744 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800745
Charles Chan5270ed02016-01-30 23:22:37 -0800746 private int getPriorityFromPrefix(IpPrefix prefix) {
747 return (prefix.isIp4()) ?
748 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
749 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700750 }
sanghob35a6192015-04-01 13:05:26 -0700751}