blob: bb9378ec224b41ef59d6f7bbec0c0044603ec27d [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-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
Pier Ventre229fd0b2016-10-31 16:49:19 -070018import com.google.common.collect.Lists;
Charles Chan051490d2018-01-11 11:48:18 -080019import com.google.common.collect.Sets;
Saurav Das4c35fc42015-11-20 15:27:53 -080020import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070021import org.onlab.packet.Ethernet;
Charles Chanef8d12e2017-12-05 21:07:38 -080022import org.onlab.packet.IPv6;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080024import org.onlab.packet.Ip6Address;
25import org.onlab.packet.IpAddress;
sangho80f11cb2015-04-01 13:05:26 -070026import org.onlab.packet.IpPrefix;
27import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070028import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070029import org.onlab.packet.VlanId;
Charles Chanb7f75ac2016-01-11 18:28:54 -080030import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070031import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080032import org.onosproject.net.flowobjective.Objective;
Charles Chan1eaf4802016-04-18 13:44:03 -070033import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080034import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080035import org.onosproject.net.packet.PacketPriority;
Saurav Dasd1872b02016-12-02 15:43:47 -080036import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan319d1a22015-11-03 10:42:14 -080037import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
38import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Das62ae6792017-05-15 15:34:25 -070039import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -070040import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sangho80f11cb2015-04-01 13:05:26 -070041import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070042import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070044import org.onosproject.net.flow.DefaultTrafficSelector;
45import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070046import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070048import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flowobjective.DefaultFilteringObjective;
50import org.onosproject.net.flowobjective.DefaultForwardingObjective;
51import org.onosproject.net.flowobjective.FilteringObjective;
52import org.onosproject.net.flowobjective.ForwardingObjective;
53import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070054import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070055import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
57
58import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070059import java.util.Collection;
60import java.util.Collections;
Saurav Das261c3002017-06-13 15:35:54 -070061import java.util.HashMap;
Saurav Dasc28b3432015-10-30 17:45:38 -070062import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070063import java.util.List;
Saurav Das261c3002017-06-13 15:35:54 -070064import java.util.Map;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080065import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070066import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070067import java.util.concurrent.atomic.AtomicLong;
Charles Chanf17f66b2018-02-26 21:33:25 -080068import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -070069
70import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080071import static org.onlab.packet.Ethernet.TYPE_ARP;
72import static org.onlab.packet.Ethernet.TYPE_IPV6;
Charles Chan051490d2018-01-11 11:48:18 -080073import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT;
Pier Luigib9632ba2017-01-12 18:14:58 -080074import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
Charles Chan051490d2018-01-11 11:48:18 -080075import static org.onlab.packet.ICMP6.ROUTER_ADVERTISEMENT;
76import static org.onlab.packet.ICMP6.ROUTER_SOLICITATION;
Pier Luigib9632ba2017-01-12 18:14:58 -080077import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan10b0fb72017-02-02 16:20:42 -080078import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sangho80f11cb2015-04-01 13:05:26 -070079
Charles Chanb7f75ac2016-01-11 18:28:54 -080080/**
81 * Populator of segment routing flow rules.
82 */
sangho80f11cb2015-04-01 13:05:26 -070083public class RoutingRulePopulator {
Charles Chanef8d12e2017-12-05 21:07:38 -080084 private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class);
85
86 private static final int ARP_NDP_PRIORITY = 30000;
sangho80f11cb2015-04-01 13:05:26 -070087
sanghofb7c7292015-04-13 15:15:58 -070088 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070089 private SegmentRoutingManager srManager;
90 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070091
sangho80f11cb2015-04-01 13:05:26 -070092 /**
93 * Creates a RoutingRulePopulator object.
94 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070095 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070096 */
Charles Chan3ed34d82017-06-22 18:03:14 -070097 RoutingRulePopulator(SegmentRoutingManager srManager) {
sangho80f11cb2015-04-01 13:05:26 -070098 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070099 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -0700100 this.rulePopulationCounter = new AtomicLong(0);
101 }
102
103 /**
104 * Resets the population counter.
105 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700106 void resetCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700107 rulePopulationCounter.set(0);
108 }
109
110 /**
111 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700112 *
113 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700114 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700115 long getCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700116 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700117 }
118
119 /**
Charles Chand66d6712018-03-29 16:03:41 -0700120 * Populate a bridging rule on given deviceId that matches given mac, given vlan and
121 * output to given port.
122 *
123 * @param deviceId device ID
124 * @param port port
125 * @param mac mac address
126 * @param vlanId VLAN ID
127 */
128 void populateBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
129 ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, false);
130 if (fob == null) {
131 log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId);
132 return;
133 }
134
135 ObjectiveContext context = new DefaultObjectiveContext(
136 (objective) -> log.debug("Brigding rule for {}/{} revoked", mac, vlanId),
137 (objective, error) -> log.warn("Failed to revoke bridging rule for {}/{}: {}", mac, vlanId, error));
138 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
139 }
140
141 /**
142 * Revoke a bridging rule on given deviceId that matches given mac, given vlan and
143 * output to given port.
144 *
145 * @param deviceId device ID
146 * @param port port
147 * @param mac mac address
148 * @param vlanId VLAN ID
149 */
150 void revokeBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
151 ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, true);
152 if (fob == null) {
153 log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId);
154 return;
155 }
156
157 ObjectiveContext context = new DefaultObjectiveContext(
158 (objective) -> log.debug("Brigding rule for {}/{} populated", mac, vlanId),
159 (objective, error) -> log.warn("Failed to populate bridging rule for {}/{}: {}", mac, vlanId, error));
160 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
161 }
162
163 /**
164 * Generates a forwarding objective builder for bridging rules.
165 * <p>
166 * The forwarding objective bridges packets destined to a given MAC to
167 * given port on given device.
168 *
169 * @param deviceId Device that host attaches to
170 * @param mac MAC address of the host
171 * @param hostVlanId VLAN ID of the host
172 * @param outport Port that host attaches to
173 * @param revoke true if forwarding objective is meant to revoke forwarding rule
174 * @return Forwarding objective builder
175 */
176 private ForwardingObjective.Builder bridgingFwdObjBuilder(
177 DeviceId deviceId, MacAddress mac, VlanId hostVlanId, PortNumber outport, boolean revoke) {
178 ConnectPoint connectPoint = new ConnectPoint(deviceId, outport);
179 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
180 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
181 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
182
183 // Create host selector
184 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
185 sbuilder.matchEthDst(mac);
186
187 // Create host treatment
188 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
189 tbuilder.immediate().setOutput(outport);
190
191 // Create host meta
192 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
193
194 // Adjust the selector, treatment and meta according to VLAN configuration
195 if (taggedVlans.contains(hostVlanId)) {
196 sbuilder.matchVlanId(hostVlanId);
197 mbuilder.matchVlanId(hostVlanId);
198 } else if (hostVlanId.equals(VlanId.NONE)) {
199 if (untaggedVlan != null) {
200 sbuilder.matchVlanId(untaggedVlan);
201 mbuilder.matchVlanId(untaggedVlan);
202 tbuilder.immediate().popVlan();
203 } else if (nativeVlan != null) {
204 sbuilder.matchVlanId(nativeVlan);
205 mbuilder.matchVlanId(nativeVlan);
206 tbuilder.immediate().popVlan();
207 } else {
208 log.warn("Untagged host {}/{} is not allowed on {} without untagged or native" +
209 "vlan config", mac, hostVlanId, connectPoint);
210 return null;
211 }
212 } else {
213 log.warn("Tagged host {}/{} is not allowed on {} without VLAN listed in tagged vlan",
214 mac, hostVlanId, connectPoint);
215 return null;
216 }
217
218 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
219 // If the objective is to revoke an existing rule, and for some reason
220 // the next-objective does not exist, then a new one should not be created
221 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outport,
222 tbuilder.build(), mbuilder.build(), !revoke);
223 if (portNextObjId == -1) {
224 // Warning log will come from getPortNextObjective method
225 return null;
226 }
227
228 return DefaultForwardingObjective.builder()
229 .withFlag(ForwardingObjective.Flag.SPECIFIC)
230 .withSelector(sbuilder.build())
231 .nextStep(portNextObjId)
232 .withPriority(100)
233 .fromApp(srManager.appId)
234 .makePermanent();
235 }
236
237 /**
238 * Populate or revoke a bridging rule on given deviceId that matches given vlanId,
239 * and hostMAC connected to given port, and output to given port only when
240 * vlan information is valid.
241 *
242 * @param deviceId device ID that host attaches to
243 * @param portNum port number that host attaches to
244 * @param hostMac mac address of the host connected to the switch port
245 * @param vlanId Vlan ID configured on the switch port
246 * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise
247 * @param install true to populate the objective, false to revoke
248 */
249 // TODO Refactor. There are a lot of duplications between this method, populateBridging,
250 // revokeBridging and bridgingFwdObjBuilder.
251 void updateBridging(DeviceId deviceId, PortNumber portNum, MacAddress hostMac,
252 VlanId vlanId, boolean popVlan, boolean install) {
253 // Create host selector
254 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
255 sbuilder.matchEthDst(hostMac);
256
257 // Create host meta
258 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
259
260 sbuilder.matchVlanId(vlanId);
261 mbuilder.matchVlanId(vlanId);
262
263 // Create host treatment
264 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
265 tbuilder.immediate().setOutput(portNum);
266
267 if (popVlan) {
268 tbuilder.immediate().popVlan();
269 }
270
271 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
272 tbuilder.build(), mbuilder.build(), install);
273 if (portNextObjId != -1) {
274 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder()
275 .withFlag(ForwardingObjective.Flag.SPECIFIC)
276 .withSelector(sbuilder.build())
277 .nextStep(portNextObjId)
278 .withPriority(100)
279 .fromApp(srManager.appId)
280 .makePermanent();
281
282 ObjectiveContext context = new DefaultObjectiveContext(
283 (objective) -> log.debug("Brigding rule for {}/{} {}", hostMac, vlanId,
284 install ? "populated" : "revoked"),
285 (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}",
286 install ? "populate" : "revoke", hostMac, vlanId, error));
287 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
288 } else {
289 log.warn("Failed to retrieve next objective for {}/{}", hostMac, vlanId);
290 }
291 }
292
293 /**
294 * Populates IP rules for a route that has direct connection to the switch.
295 * This method should not be invoked directly without going through DefaultRoutingHandler.
sangho80f11cb2015-04-01 13:05:26 -0700296 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700297 * @param deviceId device ID of the device that next hop attaches to
298 * @param prefix IP prefix of the route
299 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800300 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700301 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700302 */
Charles Chan910be6a2017-08-23 14:46:43 -0700303 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800304 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das261c3002017-06-13 15:35:54 -0700305 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700306 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800307 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800308 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700309 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
310 hostVlanId, outPort, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800311 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700312 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan319d1a22015-11-03 10:42:14 -0800313 return;
314 }
Saurav Das07c74602016-04-27 18:35:50 -0700315 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700316 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700317 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700318 return;
319 }
Charles Chan910be6a2017-08-23 14:46:43 -0700320
321 int nextId = fwdBuilder.add().nextId();
Charles Chan1eaf4802016-04-18 13:44:03 -0700322 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan910be6a2017-08-23 14:46:43 -0700323 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
324 prefix, nextId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700325 (objective, error) ->
Saurav Das261c3002017-06-13 15:35:54 -0700326 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das368cf212017-03-15 15:15:14 -0700327 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700328 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800329 rulePopulationCounter.incrementAndGet();
330 }
331
Charles Chanb7f75ac2016-01-11 18:28:54 -0800332 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700333 * Removes IP rules for a route when the next hop is gone.
Charles Chand66d6712018-03-29 16:03:41 -0700334 * This method should not be invoked directly without going through DefaultRoutingHandler.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800335 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700336 * @param deviceId device ID of the device that next hop attaches to
337 * @param prefix IP prefix of the route
338 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800339 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700340 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800341 */
Charles Chan910be6a2017-08-23 14:46:43 -0700342 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800343 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700344 log.debug("Revoke IP table entry for route {} at {}:{}",
345 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800346 ForwardingObjective.Builder fwdBuilder;
347 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700348 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
349 hostVlanId, outPort, true);
Charles Chanf4586112015-11-09 16:37:23 -0800350 } catch (DeviceConfigNotFoundException e) {
351 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
352 return;
353 }
Charles Chanea702b12016-11-30 11:55:05 -0800354 if (fwdBuilder == null) {
355 log.warn("Aborting host routing table entries due "
356 + "to error for dev:{} route:{}", deviceId, prefix);
357 return;
358 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700359 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700360 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700361 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700362 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700363 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800364 }
365
Charles Chanddac7fd2016-10-27 14:19:48 -0700366 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800367 * Returns a forwarding objective builder for routing rules.
368 * <p>
369 * The forwarding objective routes packets destined to a given prefix to
370 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700371 *
372 * @param deviceId device ID
373 * @param prefix prefix that need to be routed
374 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800375 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700376 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700377 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chanddac7fd2016-10-27 14:19:48 -0700378 * @return forwarding objective builder
379 * @throws DeviceConfigNotFoundException if given device is not configured
380 */
Charles Chan18fa4252017-02-08 16:10:40 -0800381 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700382 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700383 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
384 boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800385 throws DeviceConfigNotFoundException {
386 MacAddress deviceMac;
387 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800388
Charles Chan90772a72017-02-08 15:52:08 -0800389 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
390 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
391 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
392 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700393
Charles Chan90772a72017-02-08 15:52:08 -0800394 // Create route selector
395 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
396
397 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700398 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700399 tbuilder.deferred()
400 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800401 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700402 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800403
Charles Chan90772a72017-02-08 15:52:08 -0800404 // Create route meta
405 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800406
Charles Chan90772a72017-02-08 15:52:08 -0800407 // Adjust the meta according to VLAN configuration
408 if (taggedVlans.contains(hostVlanId)) {
409 tbuilder.setVlanId(hostVlanId);
410 } else if (hostVlanId.equals(VlanId.NONE)) {
411 if (untaggedVlan != null) {
412 mbuilder.matchVlanId(untaggedVlan);
413 } else if (nativeVlan != null) {
414 mbuilder.matchVlanId(nativeVlan);
415 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700416 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
417 hostMac, hostVlanId, connectPoint);
418 return null;
Charles Chan90772a72017-02-08 15:52:08 -0800419 }
420 } else {
Saurav Das2cb38292017-03-29 19:09:17 -0700421 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
422 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das07c74602016-04-27 18:35:50 -0700423 return null;
424 }
Saurav Das2cb38292017-03-29 19:09:17 -0700425 // if the objective is to revoke an existing rule, and for some reason
426 // the next-objective does not exist, then a new one should not be created
Charles Chan90772a72017-02-08 15:52:08 -0800427 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das2cb38292017-03-29 19:09:17 -0700428 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan90772a72017-02-08 15:52:08 -0800429 if (portNextObjId == -1) {
430 // Warning log will come from getPortNextObjective method
431 return null;
432 }
433
Charles Chanf4586112015-11-09 16:37:23 -0800434 return DefaultForwardingObjective.builder()
Charles Chan90772a72017-02-08 15:52:08 -0800435 .withSelector(sbuilder.build())
Saurav Das2d94d312015-11-24 23:21:05 -0800436 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800437 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700438 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800439 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700440 }
441
442 /**
Saurav Das261c3002017-06-13 15:35:54 -0700443 * Populates IP flow rules for all the given prefixes reachable from the
444 * destination switch(es).
sangho80f11cb2015-04-01 13:05:26 -0700445 *
Saurav Das261c3002017-06-13 15:35:54 -0700446 * @param targetSw switch where rules are to be programmed
447 * @param subnets subnets/prefixes being added
448 * @param destSw1 destination switch where the prefixes are reachable
449 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
450 * Should be null if there is no paired destination switch (by config)
451 * or if the given prefixes are reachable only via destSw1
452 * @param nextHops a map containing a set of next-hops for each destination switch.
453 * If destSw2 is not null, then this map must contain an
454 * entry for destSw2 with its next-hops from the targetSw
455 * (although the next-hop set may be empty in certain scenarios).
456 * If destSw2 is null, there should not be an entry in this
457 * map for destSw2.
sangho80f11cb2015-04-01 13:05:26 -0700458 * @return true if all rules are set successfully, false otherwise
459 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700460 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das261c3002017-06-13 15:35:54 -0700461 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700462 for (IpPrefix subnet : subnets) {
Saurav Das261c3002017-06-13 15:35:54 -0700463 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sangho80f11cb2015-04-01 13:05:26 -0700464 return false;
465 }
466 }
Charles Chanc22cef32016-04-29 14:38:22 -0700467 return true;
468 }
sangho80f11cb2015-04-01 13:05:26 -0700469
Charles Chanc22cef32016-04-29 14:38:22 -0700470 /**
Charles Chand66d6712018-03-29 16:03:41 -0700471 * Revokes IP flow rules for the subnets from given device.
Charles Chanc22cef32016-04-29 14:38:22 -0700472 *
Charles Chand66d6712018-03-29 16:03:41 -0700473 * @param targetSw target switch from which the subnets need to be removed
Charles Chanc22cef32016-04-29 14:38:22 -0700474 * @param subnets subnet being removed
475 * @return true if all rules are removed successfully, false otherwise
476 */
Charles Chand66d6712018-03-29 16:03:41 -0700477 boolean revokeIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700478 for (IpPrefix subnet : subnets) {
Charles Chand66d6712018-03-29 16:03:41 -0700479 if (!revokeIpRuleForRouter(targetSw, subnet)) {
Charles Chanc22cef32016-04-29 14:38:22 -0700480 return false;
481 }
482 }
sangho80f11cb2015-04-01 13:05:26 -0700483 return true;
484 }
485
486 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700487 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das261c3002017-06-13 15:35:54 -0700488 * is reachable via destination device(s).
sangho80f11cb2015-04-01 13:05:26 -0700489 *
Saurav Das261c3002017-06-13 15:35:54 -0700490 * @param targetSw target device ID to set the rules
491 * @param ipPrefix the IP prefix
492 * @param destSw1 destination switch where the prefixes are reachable
493 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
494 * Should be null if there is no paired destination switch (by config)
495 * or if the given prefixes are reachable only via destSw1
496 * @param nextHops map of destination switches and their next-hops.
497 * Should only contain destination switches that are
498 * actually meant to be routed to. If destSw2 is null, there
499 * should not be an entry for destSw2 in this map.
sangho80f11cb2015-04-01 13:05:26 -0700500 * @return true if all rules are set successfully, false otherwise
501 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700502 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das261c3002017-06-13 15:35:54 -0700503 IpPrefix ipPrefix, DeviceId destSw1,
504 DeviceId destSw2,
505 Map<DeviceId, Set<DeviceId>> nextHops) {
506 int segmentId1, segmentId2 = -1;
Charles Chan319d1a22015-11-03 10:42:14 -0800507 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800508 if (ipPrefix.isIp4()) {
Saurav Das261c3002017-06-13 15:35:54 -0700509 segmentId1 = config.getIPv4SegmentId(destSw1);
510 if (destSw2 != null) {
511 segmentId2 = config.getIPv4SegmentId(destSw2);
512 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800513 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700514 segmentId1 = config.getIPv6SegmentId(destSw1);
515 if (destSw2 != null) {
516 segmentId2 = config.getIPv6SegmentId(destSw2);
517 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800518 }
Charles Chan319d1a22015-11-03 10:42:14 -0800519 } catch (DeviceConfigNotFoundException e) {
520 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
521 return false;
522 }
sangho80f11cb2015-04-01 13:05:26 -0700523
Pier Ventreadb4ae62016-11-23 09:57:42 -0800524 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800525 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700526
Charles Chanf4586112015-11-09 16:37:23 -0800527 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das261c3002017-06-13 15:35:54 -0700528 DestinationSet ds;
Charles Chanf4586112015-11-09 16:37:23 -0800529 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700530
Saurav Das261c3002017-06-13 15:35:54 -0700531 if (destSw2 == null) {
532 // single dst - create destination set based on next-hop
Saurav Das97241862018-02-14 14:14:54 -0800533 // If the next hop is the same as the final destination, then MPLS
534 // label is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700535 Set<DeviceId> nhd1 = nextHops.get(destSw1);
536 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
537 tbuilder.immediate().decNwTtl();
Saurav Das97241862018-02-14 14:14:54 -0800538 ds = new DestinationSet(false, false, destSw1);
Saurav Das261c3002017-06-13 15:35:54 -0700539 treatment = tbuilder.build();
540 } else {
Saurav Das97241862018-02-14 14:14:54 -0800541 ds = new DestinationSet(false, false, segmentId1, destSw1);
Saurav Das261c3002017-06-13 15:35:54 -0700542 treatment = null;
543 }
544 } else {
545 // dst pair - IP rules for dst-pairs are always from other edge nodes
546 // the destination set needs to have both destinations, even if there
547 // are no next hops to one of them
Saurav Das97241862018-02-14 14:14:54 -0800548 ds = new DestinationSet(false, false, segmentId1, destSw1,
549 segmentId2, destSw2);
Charles Chanf4586112015-11-09 16:37:23 -0800550 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700551 }
552
Saurav Das4c35fc42015-11-20 15:27:53 -0800553 // setup metadata to pass to nextObjective - indicate the vlan on egress
554 // if needed by the switch pipeline. Since neighbor sets are always to
555 // other neighboring routers, there is no subnet assigned on those ports.
556 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800557 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das261c3002017-06-13 15:35:54 -0700558 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700559 if (grpHandler == null) {
560 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das261c3002017-06-13 15:35:54 -0700561 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700562 return false;
563 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800564
Saurav Das261c3002017-06-13 15:35:54 -0700565 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
Saurav Das97241862018-02-14 14:14:54 -0800566 metabuilder.build(), false);
Saurav Das4c35fc42015-11-20 15:27:53 -0800567 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700568 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho2165d222015-05-01 09:38:25 -0700569 return false;
570 }
571
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700572 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
573 .builder()
574 .fromApp(srManager.appId)
575 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800576 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700577 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800578 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700579 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800580 if (treatment != null) {
581 fwdBuilder.withTreatment(treatment);
582 }
Saurav Das62ae6792017-05-15 15:34:25 -0700583 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das261c3002017-06-13 15:35:54 -0700584 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700585 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700586 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700587 ipPrefix, targetSw),
Charles Chan1eaf4802016-04-18 13:44:03 -0700588 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700589 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700590 ipPrefix, error, targetSw));
591 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700592 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700593
594 return true;
595 }
596
sangho80f11cb2015-04-01 13:05:26 -0700597 /**
Charles Chand66d6712018-03-29 16:03:41 -0700598 * Revokes IP flow rules for the router IP address from given device.
Charles Chanc22cef32016-04-29 14:38:22 -0700599 *
Charles Chand66d6712018-03-29 16:03:41 -0700600 * @param targetSw target switch from which the ipPrefix need to be removed
Charles Chanc22cef32016-04-29 14:38:22 -0700601 * @param ipPrefix the IP address of the destination router
602 * @return true if all rules are removed successfully, false otherwise
603 */
Charles Chand66d6712018-03-29 16:03:41 -0700604 private boolean revokeIpRuleForRouter(DeviceId targetSw, IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800605 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700606 TrafficSelector selector = sbuilder.build();
607 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
608
609 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
610 .builder()
611 .fromApp(srManager.appId)
612 .makePermanent()
613 .withSelector(selector)
614 .withTreatment(dummyTreatment)
615 .withPriority(getPriorityFromPrefix(ipPrefix))
616 .withFlag(ForwardingObjective.Flag.SPECIFIC);
617
Charles Chand66d6712018-03-29 16:03:41 -0700618 ObjectiveContext context = new DefaultObjectiveContext(
619 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, targetSw),
620 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
621 ipPrefix, targetSw, error));
622 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.remove(context));
Charles Chanc22cef32016-04-29 14:38:22 -0700623
624 return true;
625 }
626
627 /**
Saurav Das97241862018-02-14 14:14:54 -0800628 * Populates MPLS flow rules in the target device to point towards the
629 * destination device.
630 *
631 * @param targetSwId target device ID of the switch to set the rules
632 * @param destSwId destination switch device ID
633 * @param nextHops next hops switch ID list
634 * @param routerIp the router ip of the destination switch
635 * @return true if all rules are set successfully, false otherwise
636 */
637 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
638 Set<DeviceId> nextHops, IpAddress routerIp) {
639 int segmentId;
640 try {
641 if (routerIp.isIp4()) {
642 segmentId = config.getIPv4SegmentId(destSwId);
643 } else {
644 segmentId = config.getIPv6SegmentId(destSwId);
645 }
646 } catch (DeviceConfigNotFoundException e) {
647 log.warn(e.getMessage() + " Aborting populateMplsRule.");
648 return false;
649 }
650
651 List<ForwardingObjective> fwdObjs = new ArrayList<>();
652 Collection<ForwardingObjective> fwdObjsMpls;
653 // Generates the transit rules used by the standard "routing".
654 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId,
655 routerIp, true);
656 if (fwdObjsMpls.isEmpty()) {
657 return false;
658 }
659 fwdObjs.addAll(fwdObjsMpls);
660
661 // Generates the transit rules used by the MPLS Pwaas.
662 int pwSrLabel;
663 try {
664 pwSrLabel = config.getPWRoutingLabel(destSwId);
665 } catch (DeviceConfigNotFoundException e) {
666 log.warn(e.getMessage()
667 + " Aborting populateMplsRule. No label for PseudoWire traffic.");
668 return false;
669 }
670 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel,
671 routerIp, false);
672 if (fwdObjsMpls.isEmpty()) {
673 return false;
674 }
675 fwdObjs.addAll(fwdObjsMpls);
676
677 for (ForwardingObjective fwdObj : fwdObjs) {
678 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
679 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
680 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
681 rulePopulationCounter.incrementAndGet();
682 }
683
684 return true;
685 }
686
687 /**
688 * Differentiates between popping and swapping labels when building an MPLS
689 * forwarding objective.
Pier Ventre229fd0b2016-10-31 16:49:19 -0700690 *
691 * @param targetSwId the target sw
692 * @param destSwId the destination sw
693 * @param nextHops the set of next hops
Saurav Das97241862018-02-14 14:14:54 -0800694 * @param segmentId the segmentId to match representing the destination
695 * switch
696 * @param routerIp the router ip representing the destination switch
Pier Ventre229fd0b2016-10-31 16:49:19 -0700697 * @return a collection of fwdobjective
698 */
Saurav Das261c3002017-06-13 15:35:54 -0700699 private Collection<ForwardingObjective> handleMpls(
700 DeviceId targetSwId,
701 DeviceId destSwId,
702 Set<DeviceId> nextHops,
703 int segmentId,
704 IpAddress routerIp,
705 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700706
707 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
708 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800709 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700710 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
711 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
712 sbuilder.matchMplsBos(isMplsBos);
713 TrafficSelector selector = sbuilder.build();
714
715 // setup metadata to pass to nextObjective - indicate the vlan on egress
716 // if needed by the switch pipeline. Since mpls next-hops are always to
717 // other neighboring routers, there is no subnet assigned on those ports.
718 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800719 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700720
721 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
722 // If the next hop is the destination router for the segment, do pop
723 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700724 + "label {} in switch {} with pop to next-hops {}",
725 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700726 ForwardingObjective.Builder fwdObjNoBosBuilder =
727 getMplsForwardingObjective(targetSwId,
728 nextHops,
729 true,
730 isMplsBos,
731 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700732 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800733 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700734 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700735 // Error case, we cannot handle, exit.
736 if (fwdObjNoBosBuilder == null) {
737 return Collections.emptyList();
738 }
739 fwdObjBuilders.add(fwdObjNoBosBuilder);
740
741 } else {
Saurav Das97241862018-02-14 14:14:54 -0800742 // next hop is not destination, irrespective of the number of next
743 // hops (1 or more) -- SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700744 log.debug("Installing MPLS forwarding objective for "
745 + "label {} in switch {} without pop to next-hops {}",
746 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700747 ForwardingObjective.Builder fwdObjNoBosBuilder =
748 getMplsForwardingObjective(targetSwId,
749 nextHops,
750 false,
751 isMplsBos,
752 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700753 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800754 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700755 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700756 // Error case, we cannot handle, exit.
757 if (fwdObjNoBosBuilder == null) {
758 return Collections.emptyList();
759 }
760 fwdObjBuilders.add(fwdObjNoBosBuilder);
761
762 }
763
764 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
765 // We add the final property to the fwdObjs.
766 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700767 ((Builder) ((Builder) fwdObjBuilder
768 .fromApp(srManager.appId)
769 .makePermanent())
770 .withSelector(selector)
771 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
772 .withFlag(ForwardingObjective.Flag.SPECIFIC);
773
774 ObjectiveContext context = new DefaultObjectiveContext(
775 (objective) ->
776 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
777 objective.id(), segmentId, targetSwId),
778 (objective, error) ->
779 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
780 objective.id(), segmentId, error, targetSwId));
781
782 ForwardingObjective fob = fwdObjBuilder.add(context);
783 fwdObjs.add(fob);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700784 }
785
786 return fwdObjs;
787 }
788
789 /**
Saurav Das97241862018-02-14 14:14:54 -0800790 * Returns a Forwarding Objective builder for the MPLS rule that references
791 * the desired Next Objective. Creates a DestinationSet that allows the
792 * groupHandler to create or find the required next objective.
sangho80f11cb2015-04-01 13:05:26 -0700793 *
Saurav Das97241862018-02-14 14:14:54 -0800794 * @param targetSw the target sw
795 * @param nextHops the set of next hops
796 * @param phpRequired true if penultimate-hop-popping is required
797 * @param isBos true if matched label is bottom-of-stack
798 * @param meta metadata for creating next objective
799 * @param routerIp the router ip representing the destination switch
800 * @param destSw the destination sw
801 * @return the mpls forwarding objective builder
sangho80f11cb2015-04-01 13:05:26 -0700802 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800803 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700804 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800805 Set<DeviceId> nextHops,
806 boolean phpRequired,
807 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800808 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700809 IpAddress routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800810 int segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700811 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800812
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700813 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
814 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700815
816 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das97241862018-02-14 14:14:54 -0800817 DestinationSet ds = null;
818 boolean simple = false;
sangho80f11cb2015-04-01 13:05:26 -0700819 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800820 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700821 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700822 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700823 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800824 if (routerIp.isIp4()) {
825 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
826 } else {
827 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
828 }
829 tbuilder.decNwTtl();
Saurav Das97241862018-02-14 14:14:54 -0800830 // standard case -> BoS == True; pop results in IP packet and forwarding
831 // is via an ECMP group
832 ds = new DestinationSet(false, false, destSw);
sangho80f11cb2015-04-01 13:05:26 -0700833 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800834 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
835 .decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800836 // double-label case -> BoS == False, pop results in MPLS packet
837 // depending on configuration we can ECMP this packet or choose one output
838 if (srManager.getMplsEcmp()) {
839 ds = new DestinationSet(true, false, destSw);
840 } else {
841 ds = new DestinationSet(true, false, destSw);
842 simple = true;
843 }
sangho80f11cb2015-04-01 13:05:26 -0700844 }
845 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800846 // swap with self case - SR CONTINUE
Saurav Das97241862018-02-14 14:14:54 -0800847 log.debug("getMplsForwardingObjective: swap with self");
sangho27462c62015-05-14 00:39:53 -0700848 tbuilder.deferred().decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800849 // swap results in MPLS packet with same BoS bit regardless of bit value
850 // depending on configuration we can ECMP this packet or choose one output
Saurav Dascea556f2018-03-05 14:37:16 -0800851 // XXX reconsider types
Saurav Das97241862018-02-14 14:14:54 -0800852 if (srManager.getMplsEcmp()) {
853 ds = new DestinationSet(false, true, segmentId, destSw);
854 } else {
855 ds = new DestinationSet(false, true, segmentId, destSw);
856 simple = true;
857 }
sangho80f11cb2015-04-01 13:05:26 -0700858 }
859
Saurav Das4c35fc42015-11-20 15:27:53 -0800860 fwdBuilder.withTreatment(tbuilder.build());
Saurav Das97241862018-02-14 14:14:54 -0800861 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ds:{}",
862 targetSw, ds);
Saurav Das261c3002017-06-13 15:35:54 -0700863 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
864 if (gh == null) {
865 log.warn("getNextObjectiveId query - groupHandler for device {} "
866 + "not found", targetSw);
867 return null;
868 }
Saurav Das97241862018-02-14 14:14:54 -0800869
Saurav Das261c3002017-06-13 15:35:54 -0700870 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
871 dstNextHops.put(destSw, nextHops);
Saurav Das97241862018-02-14 14:14:54 -0800872 int nextId = gh.getNextObjectiveId(ds, dstNextHops, meta, simple);
Saurav Das4c35fc42015-11-20 15:27:53 -0800873 if (nextId <= 0) {
Saurav Das97241862018-02-14 14:14:54 -0800874 log.warn("No next objective in {} for ds: {}", targetSw, ds);
Saurav Das4c35fc42015-11-20 15:27:53 -0800875 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700876 } else {
Saurav Das97241862018-02-14 14:14:54 -0800877 log.debug("nextObjId found:{} for mpls rule on device:{} to ds:{}",
878 nextId, targetSw, ds);
sangho80f11cb2015-04-01 13:05:26 -0700879 }
880
Saurav Das4c35fc42015-11-20 15:27:53 -0800881 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700882 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700883 }
884
885 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700886 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700887 * dstMac corresponding to the router's MAC address. For those pipelines
888 * that need to internally assign vlans to untagged packets, this method
889 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700890 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800891 * Note that the vlan assignment and filter programming should only be done by
892 * the master for a switch. This method is typically called at deviceAdd and
893 * programs filters only for the enabled ports of the device. For port-updates,
894 * that enable/disable ports after device add, singlePortFilter methods should
895 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700896 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700897 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800898 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700899 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700900 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700901 log.debug("Installing per-port filtering objective for untagged "
902 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800903
Saurav Das07c74602016-04-27 18:35:50 -0700904 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800905 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700906 log.warn("Device {} ports not available. Unable to add MacVlan filters",
907 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800908 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700909 }
Saurav Dasf9332192017-02-18 14:05:44 -0800910 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700911 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700912 if (!port.isEnabled()) {
913 disabledPorts++;
914 continue;
915 }
Charles Chan43be46b2017-02-26 22:59:35 -0800916 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800917 filteredPorts++;
918 } else {
919 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700920 }
Saurav Das7c305372015-10-28 12:39:42 -0700921 }
Charles Chan077314e2017-06-22 14:27:17 -0700922 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800923 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800924 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800925 errorPorts, filteredPorts);
926 }
927
928 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800929 * Creates or removes filtering objectives for a single port. Should only be
930 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800931 *
932 * @param deviceId device identifier
933 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800934 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800935 * @return true if no errors occurred during the build of the filtering objective
936 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700937 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800938 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
939 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
940 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
941 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
942
943 if (taggedVlans.size() != 0) {
944 // Filter for tagged vlans
945 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800946 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800947 return false;
948 }
949 if (nativeVlan != null) {
950 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800951 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800952 return false;
953 }
954 }
955 } else if (untaggedVlan != null) {
956 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800957 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800958 return false;
959 }
960 } else {
Saurav Dasfbe74572017-08-03 18:30:35 -0700961 // Unconfigured port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800962 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800963 return false;
964 }
965 }
966 return true;
967 }
968
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700969 /**
970 * Updates filtering objectives for a single port. Should only be called by
971 * the master for a switch
972 * @param deviceId device identifier
973 * @param portNum port identifier for port to be filtered
974 * @param pushVlan true to push vlan, false otherwise
975 * @param vlanId vlan identifier
976 * @param install true to install the filtering objective, false to remove
977 */
978 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
979 boolean pushVlan, VlanId vlanId, boolean install) {
980 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
981 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
982 deviceId, portNum, vlanId);
983 }
984 }
985
Charles Chan43be46b2017-02-26 22:59:35 -0800986 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
987 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800988 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800989 if (fob == null) {
990 // error encountered during build
991 return false;
992 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700993 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -0700994 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800995 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800996 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -0700997 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -0800998 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -0700999 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -08001000 if (install) {
1001 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
1002 } else {
1003 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -08001004 }
Charles Chan90772a72017-02-08 15:52:08 -08001005 return true;
Saurav Dasf9332192017-02-18 14:05:44 -08001006 }
1007
Charles Chan90772a72017-02-08 15:52:08 -08001008 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
1009 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -08001010 MacAddress deviceMac;
1011 try {
1012 deviceMac = config.getDeviceMac(deviceId);
1013 } catch (DeviceConfigNotFoundException e) {
1014 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
1015 return null;
1016 }
Saurav Dasf9332192017-02-18 14:05:44 -08001017 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
1018 fob.withKey(Criteria.matchInPort(portnum))
1019 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -08001020 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -08001021
Charles Chan17ca2202017-12-19 19:55:57 -08001022 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1023
Charles Chan90772a72017-02-08 15:52:08 -08001024 if (pushVlan) {
1025 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan17ca2202017-12-19 19:55:57 -08001026 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan90772a72017-02-08 15:52:08 -08001027 } else {
1028 fob.addCondition(Criteria.matchVlanId(vlanId));
1029 }
1030
Charles Chan17ca2202017-12-19 19:55:57 -08001031 // NOTE: Some switch hardware share the same filtering flow among different ports.
1032 // We use this metadata to let the driver know that there is no more enabled port
1033 // within the same VLAN on this device.
Charles Chanf17f66b2018-02-26 21:33:25 -08001034 if (noMoreEnabledPort(deviceId, vlanId)) {
Charles Chan17ca2202017-12-19 19:55:57 -08001035 tBuilder.wipeDeferred();
1036 }
1037
1038 fob.withMeta(tBuilder.build());
1039
Saurav Dasf9332192017-02-18 14:05:44 -08001040 fob.permit().fromApp(srManager.appId);
1041 return fob;
sangho80f11cb2015-04-01 13:05:26 -07001042 }
1043
1044 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -07001045 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -07001046 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -07001047 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -07001048 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -07001049 *
Saurav Das9f1c42e2015-10-23 10:51:11 -07001050 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -07001051 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001052 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -07001053 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chanef8d12e2017-12-05 21:07:38 -08001054 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -08001055 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -08001056 routerIpv4 = config.getRouterIpv4(deviceId);
1057 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chanef8d12e2017-12-05 21:07:38 -08001058 routerLinkLocalIpv6 = Ip6Address.valueOf(
1059 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
1060
Saurav Das261c3002017-06-13 15:35:54 -07001061 if (config.isPairedEdge(deviceId)) {
1062 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
1063 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
1064 }
Charles Chan319d1a22015-11-03 10:42:14 -08001065 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -08001066 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -08001067 return;
1068 }
1069
Saurav Dasc28b3432015-10-30 17:45:38 -07001070 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1071 log.debug("Not installing port-IP punts - not the master for dev:{} ",
1072 deviceId);
1073 return;
1074 }
Pier Ventreadb4ae62016-11-23 09:57:42 -08001075 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
1076 allIps.add(routerIpv4);
Charles Chanef8d12e2017-12-05 21:07:38 -08001077 allIps.add(routerLinkLocalIpv6);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001078 if (routerIpv6 != null) {
1079 allIps.add(routerIpv6);
1080 }
Saurav Das261c3002017-06-13 15:35:54 -07001081 if (pairRouterIpv4 != null) {
1082 allIps.add(pairRouterIpv4);
1083 }
1084 if (pairRouterIpv6 != null) {
1085 allIps.add(pairRouterIpv6);
1086 }
Pier Ventreadb4ae62016-11-23 09:57:42 -08001087 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001088 populateSingleIpPunts(deviceId, ipaddr);
1089 }
1090 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -08001091
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001092 /**
1093 * Creates a forwarding objective to punt all IP packets, destined to the
1094 * specified IP address, which should be router's port IP address.
1095 *
1096 * @param deviceId the switch dpid for the router
1097 * @param ipAddress the IP address of the router's port
1098 */
1099 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
1100 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
1101 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
1102
1103 srManager.packetService.requestPackets(sbuilder.build(),
1104 PacketPriority.CONTROL, srManager.appId, optDeviceId);
1105 }
1106
1107 /**
1108 * Removes a forwarding objective to punt all IP packets, destined to the
1109 * specified IP address, which should be router's port IP address.
1110 *
1111 * @param deviceId the switch dpid for the router
1112 * @param ipAddress the IP address of the router's port
1113 */
1114 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
1115 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
1116 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
1117
1118 try {
1119 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
1120 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
1121 srManager.packetService.cancelPackets(sbuilder.build(),
1122 PacketPriority.CONTROL, srManager.appId, optDeviceId);
1123 }
1124 } catch (DeviceConfigNotFoundException e) {
1125 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -07001126 }
sangho80f11cb2015-04-01 13:05:26 -07001127 }
1128
Charles Chanf4586112015-11-09 16:37:23 -08001129 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -08001130 * Method to build IPv4 or IPv6 selector.
1131 *
1132 * @param addressToMatch the address to match
1133 */
1134 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
1135 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
1136 }
1137
1138 /**
1139 * Method to build IPv4 or IPv6 selector.
1140 *
1141 * @param prefixToMatch the prefix to match
1142 */
1143 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
1144 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -07001145 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -08001146 if (prefixToMatch.isIp4()) {
1147 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
1148 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
1149 return selectorBuilder;
1150 }
Pier Ventre229fd0b2016-10-31 16:49:19 -07001151 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -08001152 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
1153 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
1154 return selectorBuilder;
1155 }
1156
1157 /**
Pier Luigib9632ba2017-01-12 18:14:58 -08001158 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
1159 * Furthermore, these are applied only by the master instance. Deferred actions
1160 * are not cleared such that packets can be flooded in the cross connect use case
1161 *
1162 * @param deviceId the switch dpid for the router
1163 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001164 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -07001165 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -08001166 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1167 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
1168 deviceId);
1169 return;
1170 }
1171
Charles Chan3ed34d82017-06-22 18:03:14 -07001172 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -08001173 // We punt all ARP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -08001174 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigib9632ba2017-01-12 18:14:58 -08001175 .add(new ObjectiveContext() {
1176 @Override
1177 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001178 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -08001179 deviceId, error);
1180 }
1181 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001182 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -08001183
Pier Luigib9632ba2017-01-12 18:14:58 -08001184 // We punt all NDP packets towards the controller.
Charles Chan051490d2018-01-11 11:48:18 -08001185 ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> {
1186 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1187 @Override
1188 public void onError(Objective objective, ObjectiveError error) {
1189 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
1190 deviceId, error);
1191 }
1192 });
1193 srManager.flowObjectiveService.forward(deviceId, obj);
1194 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001195
1196 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
1197 ForwardingObjective pairFwdObj;
1198 // Do not punt ARP packets from pair port
1199 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1200 .add(new ObjectiveContext() {
1201 @Override
1202 public void onError(Objective objective, ObjectiveError error) {
1203 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1204 deviceId, error);
1205 }
1206 });
1207 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1208
1209 // Do not punt NDP packets from pair port
Charles Chan051490d2018-01-11 11:48:18 -08001210 ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> {
1211 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1212 @Override
1213 public void onError(Objective objective, ObjectiveError error) {
1214 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1215 deviceId, error);
1216 }
1217 });
1218 srManager.flowObjectiveService.forward(deviceId, obj);
1219 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001220
1221 // Do not forward DAD packets from pair port
1222 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1223 .add(new ObjectiveContext() {
1224 @Override
1225 public void onError(Objective objective, ObjectiveError error) {
1226 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1227 deviceId, error);
1228 }
1229 });
1230 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1231 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001232 }
1233
Charles Chan3ed34d82017-06-22 18:03:14 -07001234 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1235 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001236 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001237 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001238 .withSelector(selector)
1239 .fromApp(srManager.appId)
1240 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001241 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001242 .makePermanent();
1243 }
1244
Charles Chan3ed34d82017-06-22 18:03:14 -07001245 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001246 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1247 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001248 if (port != null) {
1249 sBuilder.matchInPort(port);
1250 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001251
Charles Chan3ed34d82017-06-22 18:03:14 -07001252 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1253 if (punt) {
1254 tBuilder.punt();
1255 }
1256 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001257 }
1258
Charles Chan051490d2018-01-11 11:48:18 -08001259 private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) {
1260 Set<ForwardingObjective.Builder> result = Sets.newHashSet();
Pier Luigib9632ba2017-01-12 18:14:58 -08001261
Charles Chan051490d2018-01-11 11:48:18 -08001262 Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT)
1263 .forEach(type -> {
1264 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1265 sBuilder.matchEthType(TYPE_IPV6)
1266 .matchIPProtocol(PROTOCOL_ICMP6)
1267 .matchIcmpv6Type(type);
1268 if (port != null) {
1269 sBuilder.matchInPort(port);
1270 }
1271
1272 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1273 if (punt) {
1274 tBuilder.punt();
1275 }
1276
1277 result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority));
1278 });
1279
1280 return result;
Charles Chan3ed34d82017-06-22 18:03:14 -07001281 }
1282
1283 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1284 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1285 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001286 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1287 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1288 // .matchIPProtocol(PROTOCOL_ICMP6)
1289 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001290 if (port != null) {
1291 sBuilder.matchInPort(port);
1292 }
1293
1294 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1295 tBuilder.wipeDeferred();
1296 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001297 }
1298
1299 /**
Charles Chanf4586112015-11-09 16:37:23 -08001300 * Populates a forwarding objective to send packets that miss other high
1301 * priority Bridging Table entries to a group that contains all ports of
1302 * its subnet.
1303 *
Charles Chanf4586112015-11-09 16:37:23 -08001304 * @param deviceId switch ID to set the rules
1305 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001306 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001307 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001308 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001309 });
1310 }
1311
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001312 /**
1313 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1314 * @param deviceId switch ID to set the rule
1315 * @param vlanId vlan ID to specify the subnet
1316 * @param install true to install the rule, false to revoke the rule
1317 */
1318 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1319 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1320
1321 if (nextId < 0) {
1322 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1323 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1324 return;
1325 }
1326
1327 // Driver should treat objective with MacAddress.NONE as the
1328 // subnet broadcast rule
1329 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1330 sbuilder.matchVlanId(vlanId);
1331 sbuilder.matchEthDst(MacAddress.NONE);
1332
1333 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1334 fob.withFlag(Flag.SPECIFIC)
1335 .withSelector(sbuilder.build())
1336 .nextStep(nextId)
1337 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1338 .fromApp(srManager.appId)
1339 .makePermanent();
1340 ObjectiveContext context = new DefaultObjectiveContext(
1341 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1342 (objective, error) ->
1343 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1344
1345 if (install) {
1346 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1347 } else {
1348 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1349 }
1350 }
1351
Charles Chan82ab1932016-01-30 23:22:37 -08001352 private int getPriorityFromPrefix(IpPrefix prefix) {
1353 return (prefix.isIp4()) ?
1354 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1355 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001356 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001357
1358 /**
1359 * Update Forwarding objective for each host and IP address connected to given port.
1360 * And create corresponding Simple Next objective if it does not exist.
1361 * Applied only when populating Forwarding objective
1362 * @param deviceId switch ID to set the rule
1363 * @param portNumber port number
1364 * @param prefix IP prefix of the route
1365 * @param hostMac MAC address of the next hop
1366 * @param vlanId Vlan ID of the port
1367 * @param popVlan true to pop vlan tag in TrafficTreatment
1368 * @param install true to populate the forwarding objective, false to revoke
1369 */
1370 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1371 VlanId vlanId, boolean popVlan, boolean install) {
1372 ForwardingObjective.Builder fob;
1373 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1374 MacAddress deviceMac;
1375 try {
1376 deviceMac = config.getDeviceMac(deviceId);
1377 } catch (DeviceConfigNotFoundException e) {
1378 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1379 return;
1380 }
1381
1382 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1383 tbuilder.deferred()
1384 .setEthDst(hostMac)
1385 .setEthSrc(deviceMac)
1386 .setOutput(portNumber);
1387
1388 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1389
1390 if (!popVlan) {
1391 tbuilder.setVlanId(vlanId);
1392 } else {
1393 mbuilder.matchVlanId(vlanId);
1394 }
1395
1396 // if the objective is to revoke an existing rule, and for some reason
1397 // the next-objective does not exist, then a new one should not be created
1398 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1399 tbuilder.build(), mbuilder.build(), install);
1400 if (portNextObjId == -1) {
1401 // Warning log will come from getPortNextObjective method
1402 return;
1403 }
1404
1405 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1406 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1407 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1408
1409 ObjectiveContext context = new DefaultObjectiveContext(
1410 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1411 (objective, error) ->
1412 log.warn("Failed to {} IP rule for route {}: {}",
1413 install ? "install" : "revoke", prefix, error));
1414 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1415
1416 if (!install) {
1417 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1418 if (grpHandler == null) {
1419 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1420 } else {
1421 // Remove L3UG for the given port and host
1422 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1423 }
1424 }
1425 }
Charles Chanf17f66b2018-02-26 21:33:25 -08001426
1427 /**
1428 * Checks if there is other enabled port within the given VLAN on the given device.
1429 *
1430 * @param deviceId device ID
1431 * @param vlanId VLAN ID
1432 * @return true if there is no more port enabled within the given VLAN on the given device
1433 */
1434 boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) {
1435 Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream()
1436 .filter(Port::isEnabled)
1437 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1438 .collect(Collectors.toSet());
1439
1440 return enabledPorts.stream().noneMatch(cp ->
1441 // Given vlanId is included in the vlan-tagged configuration
1442 srManager.getTaggedVlanId(cp).contains(vlanId) ||
1443 // Given vlanId is INTERNAL_VLAN and the interface is not configured
1444 (srManager.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null &&
1445 vlanId.equals(INTERNAL_VLAN)) ||
1446 // interface is configured and either vlan-untagged or vlan-native matches given vlanId
1447 (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId))
1448 );
1449 }
sangho80f11cb2015-04-01 13:05:26 -07001450}