blob: d4a5b65218515e9dda37ae0a97e7641d25fd208c [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;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070055import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
sangho80f11cb2015-04-01 13:05:26 -070056import org.slf4j.Logger;
57import org.slf4j.LoggerFactory;
58
59import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070060import java.util.Collection;
61import java.util.Collections;
Saurav Das261c3002017-06-13 15:35:54 -070062import java.util.HashMap;
Saurav Dasc28b3432015-10-30 17:45:38 -070063import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070064import java.util.List;
Saurav Das261c3002017-06-13 15:35:54 -070065import java.util.Map;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080066import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070067import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070068import java.util.concurrent.atomic.AtomicLong;
Charles Chanf17f66b2018-02-26 21:33:25 -080069import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -070070
71import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080072import static org.onlab.packet.Ethernet.TYPE_ARP;
73import static org.onlab.packet.Ethernet.TYPE_IPV6;
Charles Chan051490d2018-01-11 11:48:18 -080074import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT;
Pier Luigib9632ba2017-01-12 18:14:58 -080075import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
Charles Chan051490d2018-01-11 11:48:18 -080076import static org.onlab.packet.ICMP6.ROUTER_ADVERTISEMENT;
77import static org.onlab.packet.ICMP6.ROUTER_SOLICITATION;
Pier Luigib9632ba2017-01-12 18:14:58 -080078import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan10b0fb72017-02-02 16:20:42 -080079import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070080import static org.onosproject.segmentrouting.SegmentRoutingService.DEFAULT_PRIORITY;
sangho80f11cb2015-04-01 13:05:26 -070081
Charles Chanb7f75ac2016-01-11 18:28:54 -080082/**
83 * Populator of segment routing flow rules.
84 */
sangho80f11cb2015-04-01 13:05:26 -070085public class RoutingRulePopulator {
Charles Chanef8d12e2017-12-05 21:07:38 -080086 private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class);
87
88 private static final int ARP_NDP_PRIORITY = 30000;
sangho80f11cb2015-04-01 13:05:26 -070089
sanghofb7c7292015-04-13 15:15:58 -070090 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070091 private SegmentRoutingManager srManager;
92 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070093
sangho80f11cb2015-04-01 13:05:26 -070094 /**
95 * Creates a RoutingRulePopulator object.
96 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070097 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070098 */
Charles Chan3ed34d82017-06-22 18:03:14 -070099 RoutingRulePopulator(SegmentRoutingManager srManager) {
sangho80f11cb2015-04-01 13:05:26 -0700100 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -0700101 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -0700102 this.rulePopulationCounter = new AtomicLong(0);
103 }
104
105 /**
106 * Resets the population counter.
107 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700108 void resetCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700109 rulePopulationCounter.set(0);
110 }
111
112 /**
113 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700114 *
115 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700116 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700117 long getCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700118 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700119 }
120
121 /**
Charles Chand66d6712018-03-29 16:03:41 -0700122 * Populate a bridging rule on given deviceId that matches given mac, given vlan and
123 * output to given port.
124 *
125 * @param deviceId device ID
126 * @param port port
127 * @param mac mac address
128 * @param vlanId VLAN ID
129 */
130 void populateBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
131 ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, false);
132 if (fob == null) {
133 log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId);
134 return;
135 }
136
137 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das5ff8a3c2018-04-07 16:51:09 -0700138 (objective) -> log.debug("Brigding rule for {}/{} populated", mac, vlanId),
139 (objective, error) -> log.warn("Failed to populate bridging rule for {}/{}: {}", mac, vlanId, error));
Charles Chand66d6712018-03-29 16:03:41 -0700140 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
141 }
142
143 /**
144 * Revoke a bridging rule on given deviceId that matches given mac, given vlan and
145 * output to given port.
146 *
147 * @param deviceId device ID
148 * @param port port
149 * @param mac mac address
150 * @param vlanId VLAN ID
151 */
152 void revokeBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
153 ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, true);
154 if (fob == null) {
155 log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId);
156 return;
157 }
158
159 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das5ff8a3c2018-04-07 16:51:09 -0700160 (objective) -> log.debug("Brigding rule for {}/{} revoked", mac, vlanId),
161 (objective, error) -> log.warn("Failed to revoke bridging rule for {}/{}: {}", mac, vlanId, error));
Charles Chand66d6712018-03-29 16:03:41 -0700162 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
163 }
164
165 /**
166 * Generates a forwarding objective builder for bridging rules.
167 * <p>
168 * The forwarding objective bridges packets destined to a given MAC to
169 * given port on given device.
170 *
171 * @param deviceId Device that host attaches to
172 * @param mac MAC address of the host
173 * @param hostVlanId VLAN ID of the host
174 * @param outport Port that host attaches to
175 * @param revoke true if forwarding objective is meant to revoke forwarding rule
176 * @return Forwarding objective builder
177 */
178 private ForwardingObjective.Builder bridgingFwdObjBuilder(
179 DeviceId deviceId, MacAddress mac, VlanId hostVlanId, PortNumber outport, boolean revoke) {
180 ConnectPoint connectPoint = new ConnectPoint(deviceId, outport);
181 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
182 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
183 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
184
185 // Create host selector
186 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
187 sbuilder.matchEthDst(mac);
188
189 // Create host treatment
190 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
191 tbuilder.immediate().setOutput(outport);
192
193 // Create host meta
194 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
195
196 // Adjust the selector, treatment and meta according to VLAN configuration
197 if (taggedVlans.contains(hostVlanId)) {
198 sbuilder.matchVlanId(hostVlanId);
199 mbuilder.matchVlanId(hostVlanId);
200 } else if (hostVlanId.equals(VlanId.NONE)) {
201 if (untaggedVlan != null) {
202 sbuilder.matchVlanId(untaggedVlan);
203 mbuilder.matchVlanId(untaggedVlan);
204 tbuilder.immediate().popVlan();
205 } else if (nativeVlan != null) {
206 sbuilder.matchVlanId(nativeVlan);
207 mbuilder.matchVlanId(nativeVlan);
208 tbuilder.immediate().popVlan();
209 } else {
210 log.warn("Untagged host {}/{} is not allowed on {} without untagged or native" +
211 "vlan config", mac, hostVlanId, connectPoint);
212 return null;
213 }
214 } else {
215 log.warn("Tagged host {}/{} is not allowed on {} without VLAN listed in tagged vlan",
216 mac, hostVlanId, connectPoint);
217 return null;
218 }
219
220 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
221 // If the objective is to revoke an existing rule, and for some reason
222 // the next-objective does not exist, then a new one should not be created
223 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outport,
224 tbuilder.build(), mbuilder.build(), !revoke);
225 if (portNextObjId == -1) {
226 // Warning log will come from getPortNextObjective method
227 return null;
228 }
229
230 return DefaultForwardingObjective.builder()
231 .withFlag(ForwardingObjective.Flag.SPECIFIC)
232 .withSelector(sbuilder.build())
233 .nextStep(portNextObjId)
234 .withPriority(100)
235 .fromApp(srManager.appId)
236 .makePermanent();
237 }
238
239 /**
240 * Populate or revoke a bridging rule on given deviceId that matches given vlanId,
241 * and hostMAC connected to given port, and output to given port only when
242 * vlan information is valid.
243 *
244 * @param deviceId device ID that host attaches to
245 * @param portNum port number that host attaches to
246 * @param hostMac mac address of the host connected to the switch port
247 * @param vlanId Vlan ID configured on the switch port
248 * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise
249 * @param install true to populate the objective, false to revoke
250 */
251 // TODO Refactor. There are a lot of duplications between this method, populateBridging,
252 // revokeBridging and bridgingFwdObjBuilder.
253 void updateBridging(DeviceId deviceId, PortNumber portNum, MacAddress hostMac,
254 VlanId vlanId, boolean popVlan, boolean install) {
255 // Create host selector
256 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
257 sbuilder.matchEthDst(hostMac);
258
259 // Create host meta
260 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
261
262 sbuilder.matchVlanId(vlanId);
263 mbuilder.matchVlanId(vlanId);
264
265 // Create host treatment
266 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
267 tbuilder.immediate().setOutput(portNum);
268
269 if (popVlan) {
270 tbuilder.immediate().popVlan();
271 }
272
273 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
274 tbuilder.build(), mbuilder.build(), install);
275 if (portNextObjId != -1) {
276 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder()
277 .withFlag(ForwardingObjective.Flag.SPECIFIC)
278 .withSelector(sbuilder.build())
279 .nextStep(portNextObjId)
280 .withPriority(100)
281 .fromApp(srManager.appId)
282 .makePermanent();
283
284 ObjectiveContext context = new DefaultObjectiveContext(
285 (objective) -> log.debug("Brigding rule for {}/{} {}", hostMac, vlanId,
286 install ? "populated" : "revoked"),
287 (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}",
288 install ? "populate" : "revoke", hostMac, vlanId, error));
289 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
290 } else {
291 log.warn("Failed to retrieve next objective for {}/{}", hostMac, vlanId);
292 }
293 }
294
295 /**
296 * Populates IP rules for a route that has direct connection to the switch.
297 * This method should not be invoked directly without going through DefaultRoutingHandler.
sangho80f11cb2015-04-01 13:05:26 -0700298 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700299 * @param deviceId device ID of the device that next hop attaches to
300 * @param prefix IP prefix of the route
301 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800302 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700303 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700304 */
Charles Chan910be6a2017-08-23 14:46:43 -0700305 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800306 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das261c3002017-06-13 15:35:54 -0700307 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700308 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800309 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800310 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700311 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
312 hostVlanId, outPort, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800313 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700314 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan319d1a22015-11-03 10:42:14 -0800315 return;
316 }
Saurav Das07c74602016-04-27 18:35:50 -0700317 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700318 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700319 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700320 return;
321 }
Charles Chan910be6a2017-08-23 14:46:43 -0700322
323 int nextId = fwdBuilder.add().nextId();
Charles Chan1eaf4802016-04-18 13:44:03 -0700324 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan910be6a2017-08-23 14:46:43 -0700325 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
326 prefix, nextId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700327 (objective, error) ->
Saurav Das261c3002017-06-13 15:35:54 -0700328 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das368cf212017-03-15 15:15:14 -0700329 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700330 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800331 rulePopulationCounter.incrementAndGet();
332 }
333
Charles Chanb7f75ac2016-01-11 18:28:54 -0800334 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700335 * Removes IP rules for a route when the next hop is gone.
Charles Chand66d6712018-03-29 16:03:41 -0700336 * This method should not be invoked directly without going through DefaultRoutingHandler.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800337 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700338 * @param deviceId device ID of the device that next hop attaches to
339 * @param prefix IP prefix of the route
340 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800341 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700342 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800343 */
Charles Chan910be6a2017-08-23 14:46:43 -0700344 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800345 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700346 log.debug("Revoke IP table entry for route {} at {}:{}",
347 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800348 ForwardingObjective.Builder fwdBuilder;
349 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700350 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
351 hostVlanId, outPort, true);
Charles Chanf4586112015-11-09 16:37:23 -0800352 } catch (DeviceConfigNotFoundException e) {
353 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
354 return;
355 }
Charles Chanea702b12016-11-30 11:55:05 -0800356 if (fwdBuilder == null) {
357 log.warn("Aborting host routing table entries due "
358 + "to error for dev:{} route:{}", deviceId, prefix);
359 return;
360 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700361 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700362 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700363 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700364 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700365 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800366 }
367
Charles Chanddac7fd2016-10-27 14:19:48 -0700368 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800369 * Returns a forwarding objective builder for routing rules.
370 * <p>
371 * The forwarding objective routes packets destined to a given prefix to
372 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700373 *
374 * @param deviceId device ID
375 * @param prefix prefix that need to be routed
376 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800377 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700378 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700379 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chanddac7fd2016-10-27 14:19:48 -0700380 * @return forwarding objective builder
381 * @throws DeviceConfigNotFoundException if given device is not configured
382 */
Charles Chan18fa4252017-02-08 16:10:40 -0800383 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700384 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700385 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
386 boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800387 throws DeviceConfigNotFoundException {
388 MacAddress deviceMac;
389 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800390
Charles Chan90772a72017-02-08 15:52:08 -0800391 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
392 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
393 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
394 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700395
Charles Chan90772a72017-02-08 15:52:08 -0800396 // Create route selector
397 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
398
399 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700400 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700401 tbuilder.deferred()
402 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800403 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700404 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800405
Charles Chan90772a72017-02-08 15:52:08 -0800406 // Create route meta
407 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800408
Charles Chan90772a72017-02-08 15:52:08 -0800409 // Adjust the meta according to VLAN configuration
410 if (taggedVlans.contains(hostVlanId)) {
411 tbuilder.setVlanId(hostVlanId);
412 } else if (hostVlanId.equals(VlanId.NONE)) {
413 if (untaggedVlan != null) {
414 mbuilder.matchVlanId(untaggedVlan);
415 } else if (nativeVlan != null) {
416 mbuilder.matchVlanId(nativeVlan);
417 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700418 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
419 hostMac, hostVlanId, connectPoint);
420 return null;
Charles Chan90772a72017-02-08 15:52:08 -0800421 }
422 } else {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700423 // Internally-assigned dummy VLAN id will be given as hostVlanId
424 // when destination is double-tagged.
425 VlanId vlanId = srManager.dummyVlanIdStore().get(
426 new DummyVlanIdStoreKey(connectPoint, prefix.address()));
427 if (vlanId != null && vlanId.equals(hostVlanId)) {
428 tbuilder.setVlanId(hostVlanId);
429 mbuilder.matchVlanId(VlanId.ANY);
430 } else {
431 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
432 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
433 return null;
434 }
Saurav Das07c74602016-04-27 18:35:50 -0700435 }
Saurav Das2cb38292017-03-29 19:09:17 -0700436 // if the objective is to revoke an existing rule, and for some reason
437 // the next-objective does not exist, then a new one should not be created
Charles Chan90772a72017-02-08 15:52:08 -0800438 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das2cb38292017-03-29 19:09:17 -0700439 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan90772a72017-02-08 15:52:08 -0800440 if (portNextObjId == -1) {
441 // Warning log will come from getPortNextObjective method
442 return null;
443 }
444
Charles Chanf4586112015-11-09 16:37:23 -0800445 return DefaultForwardingObjective.builder()
Charles Chan90772a72017-02-08 15:52:08 -0800446 .withSelector(sbuilder.build())
Saurav Das2d94d312015-11-24 23:21:05 -0800447 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800448 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700449 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800450 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700451 }
452
453 /**
Saurav Das261c3002017-06-13 15:35:54 -0700454 * Populates IP flow rules for all the given prefixes reachable from the
455 * destination switch(es).
sangho80f11cb2015-04-01 13:05:26 -0700456 *
Saurav Das261c3002017-06-13 15:35:54 -0700457 * @param targetSw switch where rules are to be programmed
458 * @param subnets subnets/prefixes being added
459 * @param destSw1 destination switch where the prefixes are reachable
460 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
461 * Should be null if there is no paired destination switch (by config)
462 * or if the given prefixes are reachable only via destSw1
463 * @param nextHops a map containing a set of next-hops for each destination switch.
464 * If destSw2 is not null, then this map must contain an
465 * entry for destSw2 with its next-hops from the targetSw
466 * (although the next-hop set may be empty in certain scenarios).
467 * If destSw2 is null, there should not be an entry in this
468 * map for destSw2.
sangho80f11cb2015-04-01 13:05:26 -0700469 * @return true if all rules are set successfully, false otherwise
470 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700471 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das261c3002017-06-13 15:35:54 -0700472 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700473 for (IpPrefix subnet : subnets) {
Saurav Das261c3002017-06-13 15:35:54 -0700474 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sangho80f11cb2015-04-01 13:05:26 -0700475 return false;
476 }
477 }
Charles Chanc22cef32016-04-29 14:38:22 -0700478 return true;
479 }
sangho80f11cb2015-04-01 13:05:26 -0700480
Charles Chanc22cef32016-04-29 14:38:22 -0700481 /**
Charles Chand66d6712018-03-29 16:03:41 -0700482 * Revokes IP flow rules for the subnets from given device.
Charles Chanc22cef32016-04-29 14:38:22 -0700483 *
Charles Chand66d6712018-03-29 16:03:41 -0700484 * @param targetSw target switch from which the subnets need to be removed
Charles Chanc22cef32016-04-29 14:38:22 -0700485 * @param subnets subnet being removed
486 * @return true if all rules are removed successfully, false otherwise
487 */
Charles Chand66d6712018-03-29 16:03:41 -0700488 boolean revokeIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700489 for (IpPrefix subnet : subnets) {
Charles Chand66d6712018-03-29 16:03:41 -0700490 if (!revokeIpRuleForRouter(targetSw, subnet)) {
Charles Chanc22cef32016-04-29 14:38:22 -0700491 return false;
492 }
493 }
sangho80f11cb2015-04-01 13:05:26 -0700494 return true;
495 }
496
497 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700498 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das261c3002017-06-13 15:35:54 -0700499 * is reachable via destination device(s).
sangho80f11cb2015-04-01 13:05:26 -0700500 *
Saurav Das261c3002017-06-13 15:35:54 -0700501 * @param targetSw target device ID to set the rules
502 * @param ipPrefix the IP prefix
503 * @param destSw1 destination switch where the prefixes are reachable
504 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
505 * Should be null if there is no paired destination switch (by config)
506 * or if the given prefixes are reachable only via destSw1
507 * @param nextHops map of destination switches and their next-hops.
508 * Should only contain destination switches that are
509 * actually meant to be routed to. If destSw2 is null, there
510 * should not be an entry for destSw2 in this map.
sangho80f11cb2015-04-01 13:05:26 -0700511 * @return true if all rules are set successfully, false otherwise
512 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700513 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das261c3002017-06-13 15:35:54 -0700514 IpPrefix ipPrefix, DeviceId destSw1,
515 DeviceId destSw2,
516 Map<DeviceId, Set<DeviceId>> nextHops) {
517 int segmentId1, segmentId2 = -1;
Charles Chan319d1a22015-11-03 10:42:14 -0800518 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800519 if (ipPrefix.isIp4()) {
Saurav Das261c3002017-06-13 15:35:54 -0700520 segmentId1 = config.getIPv4SegmentId(destSw1);
521 if (destSw2 != null) {
522 segmentId2 = config.getIPv4SegmentId(destSw2);
523 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800524 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700525 segmentId1 = config.getIPv6SegmentId(destSw1);
526 if (destSw2 != null) {
527 segmentId2 = config.getIPv6SegmentId(destSw2);
528 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800529 }
Charles Chan319d1a22015-11-03 10:42:14 -0800530 } catch (DeviceConfigNotFoundException e) {
531 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
532 return false;
533 }
sangho80f11cb2015-04-01 13:05:26 -0700534
Pier Ventreadb4ae62016-11-23 09:57:42 -0800535 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800536 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700537
Charles Chanf4586112015-11-09 16:37:23 -0800538 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das261c3002017-06-13 15:35:54 -0700539 DestinationSet ds;
Charles Chanf4586112015-11-09 16:37:23 -0800540 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700541
Saurav Das261c3002017-06-13 15:35:54 -0700542 if (destSw2 == null) {
543 // single dst - create destination set based on next-hop
Saurav Das97241862018-02-14 14:14:54 -0800544 // If the next hop is the same as the final destination, then MPLS
545 // label is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700546 Set<DeviceId> nhd1 = nextHops.get(destSw1);
547 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
548 tbuilder.immediate().decNwTtl();
Saurav Das97241862018-02-14 14:14:54 -0800549 ds = new DestinationSet(false, false, destSw1);
Saurav Das261c3002017-06-13 15:35:54 -0700550 treatment = tbuilder.build();
551 } else {
Saurav Das97241862018-02-14 14:14:54 -0800552 ds = new DestinationSet(false, false, segmentId1, destSw1);
Saurav Das261c3002017-06-13 15:35:54 -0700553 treatment = null;
554 }
555 } else {
556 // dst pair - IP rules for dst-pairs are always from other edge nodes
557 // the destination set needs to have both destinations, even if there
558 // are no next hops to one of them
Saurav Das97241862018-02-14 14:14:54 -0800559 ds = new DestinationSet(false, false, segmentId1, destSw1,
560 segmentId2, destSw2);
Charles Chanf4586112015-11-09 16:37:23 -0800561 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700562 }
563
Saurav Das4c35fc42015-11-20 15:27:53 -0800564 // setup metadata to pass to nextObjective - indicate the vlan on egress
565 // if needed by the switch pipeline. Since neighbor sets are always to
566 // other neighboring routers, there is no subnet assigned on those ports.
567 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800568 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das261c3002017-06-13 15:35:54 -0700569 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700570 if (grpHandler == null) {
571 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das261c3002017-06-13 15:35:54 -0700572 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700573 return false;
574 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800575
Saurav Das261c3002017-06-13 15:35:54 -0700576 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
Saurav Das97241862018-02-14 14:14:54 -0800577 metabuilder.build(), false);
Saurav Das4c35fc42015-11-20 15:27:53 -0800578 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700579 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho2165d222015-05-01 09:38:25 -0700580 return false;
581 }
582
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700583 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
584 .builder()
585 .fromApp(srManager.appId)
586 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800587 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700588 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800589 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700590 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800591 if (treatment != null) {
592 fwdBuilder.withTreatment(treatment);
593 }
Saurav Das62ae6792017-05-15 15:34:25 -0700594 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das261c3002017-06-13 15:35:54 -0700595 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700596 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700597 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700598 ipPrefix, targetSw),
Charles Chan1eaf4802016-04-18 13:44:03 -0700599 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700600 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700601 ipPrefix, error, targetSw));
602 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700603 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700604
605 return true;
606 }
607
sangho80f11cb2015-04-01 13:05:26 -0700608 /**
Charles Chand66d6712018-03-29 16:03:41 -0700609 * Revokes IP flow rules for the router IP address from given device.
Charles Chanc22cef32016-04-29 14:38:22 -0700610 *
Charles Chand66d6712018-03-29 16:03:41 -0700611 * @param targetSw target switch from which the ipPrefix need to be removed
Charles Chanc22cef32016-04-29 14:38:22 -0700612 * @param ipPrefix the IP address of the destination router
613 * @return true if all rules are removed successfully, false otherwise
614 */
Charles Chand66d6712018-03-29 16:03:41 -0700615 private boolean revokeIpRuleForRouter(DeviceId targetSw, IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800616 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700617 TrafficSelector selector = sbuilder.build();
618 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
619
620 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
621 .builder()
622 .fromApp(srManager.appId)
623 .makePermanent()
624 .withSelector(selector)
625 .withTreatment(dummyTreatment)
626 .withPriority(getPriorityFromPrefix(ipPrefix))
627 .withFlag(ForwardingObjective.Flag.SPECIFIC);
628
Charles Chand66d6712018-03-29 16:03:41 -0700629 ObjectiveContext context = new DefaultObjectiveContext(
630 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, targetSw),
631 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
632 ipPrefix, targetSw, error));
633 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.remove(context));
Charles Chanc22cef32016-04-29 14:38:22 -0700634
635 return true;
636 }
637
638 /**
Saurav Das97241862018-02-14 14:14:54 -0800639 * Populates MPLS flow rules in the target device to point towards the
640 * destination device.
641 *
642 * @param targetSwId target device ID of the switch to set the rules
643 * @param destSwId destination switch device ID
644 * @param nextHops next hops switch ID list
645 * @param routerIp the router ip of the destination switch
646 * @return true if all rules are set successfully, false otherwise
647 */
648 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
649 Set<DeviceId> nextHops, IpAddress routerIp) {
650 int segmentId;
651 try {
652 if (routerIp.isIp4()) {
653 segmentId = config.getIPv4SegmentId(destSwId);
654 } else {
655 segmentId = config.getIPv6SegmentId(destSwId);
656 }
657 } catch (DeviceConfigNotFoundException e) {
658 log.warn(e.getMessage() + " Aborting populateMplsRule.");
659 return false;
660 }
661
662 List<ForwardingObjective> fwdObjs = new ArrayList<>();
663 Collection<ForwardingObjective> fwdObjsMpls;
664 // Generates the transit rules used by the standard "routing".
665 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId,
666 routerIp, true);
667 if (fwdObjsMpls.isEmpty()) {
668 return false;
669 }
670 fwdObjs.addAll(fwdObjsMpls);
671
672 // Generates the transit rules used by the MPLS Pwaas.
673 int pwSrLabel;
674 try {
675 pwSrLabel = config.getPWRoutingLabel(destSwId);
676 } catch (DeviceConfigNotFoundException e) {
677 log.warn(e.getMessage()
678 + " Aborting populateMplsRule. No label for PseudoWire traffic.");
679 return false;
680 }
681 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel,
682 routerIp, false);
683 if (fwdObjsMpls.isEmpty()) {
684 return false;
685 }
686 fwdObjs.addAll(fwdObjsMpls);
687
688 for (ForwardingObjective fwdObj : fwdObjs) {
689 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
690 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
691 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
692 rulePopulationCounter.incrementAndGet();
693 }
694
695 return true;
696 }
697
698 /**
699 * Differentiates between popping and swapping labels when building an MPLS
700 * forwarding objective.
Pier Ventre229fd0b2016-10-31 16:49:19 -0700701 *
702 * @param targetSwId the target sw
703 * @param destSwId the destination sw
704 * @param nextHops the set of next hops
Saurav Das97241862018-02-14 14:14:54 -0800705 * @param segmentId the segmentId to match representing the destination
706 * switch
707 * @param routerIp the router ip representing the destination switch
Pier Ventre229fd0b2016-10-31 16:49:19 -0700708 * @return a collection of fwdobjective
709 */
Saurav Das261c3002017-06-13 15:35:54 -0700710 private Collection<ForwardingObjective> handleMpls(
711 DeviceId targetSwId,
712 DeviceId destSwId,
713 Set<DeviceId> nextHops,
714 int segmentId,
715 IpAddress routerIp,
716 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700717
718 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
719 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800720 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700721 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
722 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
723 sbuilder.matchMplsBos(isMplsBos);
724 TrafficSelector selector = sbuilder.build();
725
726 // setup metadata to pass to nextObjective - indicate the vlan on egress
727 // if needed by the switch pipeline. Since mpls next-hops are always to
728 // other neighboring routers, there is no subnet assigned on those ports.
729 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800730 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700731
732 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
733 // If the next hop is the destination router for the segment, do pop
734 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700735 + "label {} in switch {} with pop to next-hops {}",
736 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700737 ForwardingObjective.Builder fwdObjNoBosBuilder =
738 getMplsForwardingObjective(targetSwId,
739 nextHops,
740 true,
741 isMplsBos,
742 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700743 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800744 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700745 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700746 // Error case, we cannot handle, exit.
747 if (fwdObjNoBosBuilder == null) {
748 return Collections.emptyList();
749 }
750 fwdObjBuilders.add(fwdObjNoBosBuilder);
751
752 } else {
Saurav Das97241862018-02-14 14:14:54 -0800753 // next hop is not destination, irrespective of the number of next
754 // hops (1 or more) -- SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700755 log.debug("Installing MPLS forwarding objective for "
756 + "label {} in switch {} without pop to next-hops {}",
757 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700758 ForwardingObjective.Builder fwdObjNoBosBuilder =
759 getMplsForwardingObjective(targetSwId,
760 nextHops,
761 false,
762 isMplsBos,
763 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700764 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800765 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700766 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700767 // Error case, we cannot handle, exit.
768 if (fwdObjNoBosBuilder == null) {
769 return Collections.emptyList();
770 }
771 fwdObjBuilders.add(fwdObjNoBosBuilder);
772
773 }
774
775 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
776 // We add the final property to the fwdObjs.
777 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700778 ((Builder) ((Builder) fwdObjBuilder
779 .fromApp(srManager.appId)
780 .makePermanent())
781 .withSelector(selector)
782 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
783 .withFlag(ForwardingObjective.Flag.SPECIFIC);
784
785 ObjectiveContext context = new DefaultObjectiveContext(
786 (objective) ->
787 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
788 objective.id(), segmentId, targetSwId),
789 (objective, error) ->
790 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
791 objective.id(), segmentId, error, targetSwId));
792
793 ForwardingObjective fob = fwdObjBuilder.add(context);
794 fwdObjs.add(fob);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700795 }
796
797 return fwdObjs;
798 }
799
800 /**
Saurav Das97241862018-02-14 14:14:54 -0800801 * Returns a Forwarding Objective builder for the MPLS rule that references
802 * the desired Next Objective. Creates a DestinationSet that allows the
803 * groupHandler to create or find the required next objective.
sangho80f11cb2015-04-01 13:05:26 -0700804 *
Saurav Das97241862018-02-14 14:14:54 -0800805 * @param targetSw the target sw
806 * @param nextHops the set of next hops
807 * @param phpRequired true if penultimate-hop-popping is required
808 * @param isBos true if matched label is bottom-of-stack
809 * @param meta metadata for creating next objective
810 * @param routerIp the router ip representing the destination switch
811 * @param destSw the destination sw
812 * @return the mpls forwarding objective builder
sangho80f11cb2015-04-01 13:05:26 -0700813 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800814 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700815 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800816 Set<DeviceId> nextHops,
817 boolean phpRequired,
818 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800819 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700820 IpAddress routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800821 int segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700822 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800823
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700824 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
825 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700826
827 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das97241862018-02-14 14:14:54 -0800828 DestinationSet ds = null;
829 boolean simple = false;
sangho80f11cb2015-04-01 13:05:26 -0700830 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800831 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700832 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700833 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700834 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800835 if (routerIp.isIp4()) {
836 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
837 } else {
838 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
839 }
840 tbuilder.decNwTtl();
Saurav Das97241862018-02-14 14:14:54 -0800841 // standard case -> BoS == True; pop results in IP packet and forwarding
842 // is via an ECMP group
843 ds = new DestinationSet(false, false, destSw);
sangho80f11cb2015-04-01 13:05:26 -0700844 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800845 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
846 .decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800847 // double-label case -> BoS == False, pop results in MPLS packet
848 // depending on configuration we can ECMP this packet or choose one output
849 if (srManager.getMplsEcmp()) {
850 ds = new DestinationSet(true, false, destSw);
851 } else {
852 ds = new DestinationSet(true, false, destSw);
853 simple = true;
854 }
sangho80f11cb2015-04-01 13:05:26 -0700855 }
856 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800857 // swap with self case - SR CONTINUE
Saurav Das97241862018-02-14 14:14:54 -0800858 log.debug("getMplsForwardingObjective: swap with self");
sangho27462c62015-05-14 00:39:53 -0700859 tbuilder.deferred().decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800860 // swap results in MPLS packet with same BoS bit regardless of bit value
861 // depending on configuration we can ECMP this packet or choose one output
Saurav Dascea556f2018-03-05 14:37:16 -0800862 // XXX reconsider types
Saurav Das97241862018-02-14 14:14:54 -0800863 if (srManager.getMplsEcmp()) {
864 ds = new DestinationSet(false, true, segmentId, destSw);
865 } else {
866 ds = new DestinationSet(false, true, segmentId, destSw);
867 simple = true;
868 }
sangho80f11cb2015-04-01 13:05:26 -0700869 }
870
Saurav Das4c35fc42015-11-20 15:27:53 -0800871 fwdBuilder.withTreatment(tbuilder.build());
Saurav Das97241862018-02-14 14:14:54 -0800872 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ds:{}",
873 targetSw, ds);
Saurav Das261c3002017-06-13 15:35:54 -0700874 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
875 if (gh == null) {
876 log.warn("getNextObjectiveId query - groupHandler for device {} "
877 + "not found", targetSw);
878 return null;
879 }
Saurav Das97241862018-02-14 14:14:54 -0800880
Saurav Das261c3002017-06-13 15:35:54 -0700881 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
882 dstNextHops.put(destSw, nextHops);
Saurav Das97241862018-02-14 14:14:54 -0800883 int nextId = gh.getNextObjectiveId(ds, dstNextHops, meta, simple);
Saurav Das4c35fc42015-11-20 15:27:53 -0800884 if (nextId <= 0) {
Saurav Das97241862018-02-14 14:14:54 -0800885 log.warn("No next objective in {} for ds: {}", targetSw, ds);
Saurav Das4c35fc42015-11-20 15:27:53 -0800886 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700887 } else {
Saurav Das97241862018-02-14 14:14:54 -0800888 log.debug("nextObjId found:{} for mpls rule on device:{} to ds:{}",
889 nextId, targetSw, ds);
sangho80f11cb2015-04-01 13:05:26 -0700890 }
891
Saurav Das4c35fc42015-11-20 15:27:53 -0800892 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700893 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700894 }
895
896 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700897 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700898 * dstMac corresponding to the router's MAC address. For those pipelines
899 * that need to internally assign vlans to untagged packets, this method
900 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700901 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800902 * Note that the vlan assignment and filter programming should only be done by
903 * the master for a switch. This method is typically called at deviceAdd and
904 * programs filters only for the enabled ports of the device. For port-updates,
905 * that enable/disable ports after device add, singlePortFilter methods should
906 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700907 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700908 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800909 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700910 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700911 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700912 log.debug("Installing per-port filtering objective for untagged "
913 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800914
Saurav Das07c74602016-04-27 18:35:50 -0700915 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800916 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700917 log.warn("Device {} ports not available. Unable to add MacVlan filters",
918 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800919 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700920 }
Saurav Dasf9332192017-02-18 14:05:44 -0800921 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700922 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700923 if (!port.isEnabled()) {
924 disabledPorts++;
925 continue;
926 }
Charles Chan43be46b2017-02-26 22:59:35 -0800927 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800928 filteredPorts++;
929 } else {
930 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700931 }
Saurav Das7c305372015-10-28 12:39:42 -0700932 }
Charles Chan077314e2017-06-22 14:27:17 -0700933 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800934 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800935 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800936 errorPorts, filteredPorts);
937 }
938
939 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800940 * Creates or removes filtering objectives for a single port. Should only be
941 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800942 *
943 * @param deviceId device identifier
944 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800945 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800946 * @return true if no errors occurred during the build of the filtering objective
947 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700948 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800949 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
950 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
951 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
952 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
953
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700954 // Do not configure filter for edge ports where double-tagged hosts are connected.
Charles Chan90772a72017-02-08 15:52:08 -0800955 if (taggedVlans.size() != 0) {
956 // Filter for tagged vlans
957 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800958 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800959 return false;
960 }
961 if (nativeVlan != null) {
962 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800963 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800964 return false;
965 }
966 }
967 } else if (untaggedVlan != null) {
968 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800969 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800970 return false;
971 }
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700972 } else if (srManager.linkService.getLinks(connectPoint).size() == 0) {
973 // Filter for unconfigured upstream port, using INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800974 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800975 return false;
976 }
977 }
978 return true;
979 }
980
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700981 /**
982 * Updates filtering objectives for a single port. Should only be called by
983 * the master for a switch
984 * @param deviceId device identifier
985 * @param portNum port identifier for port to be filtered
986 * @param pushVlan true to push vlan, false otherwise
987 * @param vlanId vlan identifier
988 * @param install true to install the filtering objective, false to remove
989 */
990 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
991 boolean pushVlan, VlanId vlanId, boolean install) {
992 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
993 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
994 deviceId, portNum, vlanId);
995 }
996 }
997
Charles Chan43be46b2017-02-26 22:59:35 -0800998 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
999 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -08001000 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -08001001 if (fob == null) {
1002 // error encountered during build
1003 return false;
1004 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001005 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -07001006 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -08001007 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -08001008 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -07001009 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -08001010 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -07001011 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -08001012 if (install) {
1013 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
1014 } else {
1015 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -08001016 }
Charles Chan90772a72017-02-08 15:52:08 -08001017 return true;
Saurav Dasf9332192017-02-18 14:05:44 -08001018 }
1019
Charles Chan90772a72017-02-08 15:52:08 -08001020 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
1021 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -08001022 MacAddress deviceMac;
1023 try {
1024 deviceMac = config.getDeviceMac(deviceId);
1025 } catch (DeviceConfigNotFoundException e) {
1026 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
1027 return null;
1028 }
Saurav Dasf9332192017-02-18 14:05:44 -08001029 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
1030 fob.withKey(Criteria.matchInPort(portnum))
1031 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -08001032 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -08001033
Charles Chan17ca2202017-12-19 19:55:57 -08001034 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1035
Charles Chan90772a72017-02-08 15:52:08 -08001036 if (pushVlan) {
1037 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan17ca2202017-12-19 19:55:57 -08001038 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan90772a72017-02-08 15:52:08 -08001039 } else {
1040 fob.addCondition(Criteria.matchVlanId(vlanId));
1041 }
1042
Charles Chan17ca2202017-12-19 19:55:57 -08001043 // NOTE: Some switch hardware share the same filtering flow among different ports.
1044 // We use this metadata to let the driver know that there is no more enabled port
1045 // within the same VLAN on this device.
Charles Chanf17f66b2018-02-26 21:33:25 -08001046 if (noMoreEnabledPort(deviceId, vlanId)) {
Charles Chan17ca2202017-12-19 19:55:57 -08001047 tBuilder.wipeDeferred();
1048 }
1049
1050 fob.withMeta(tBuilder.build());
1051
Saurav Dasf9332192017-02-18 14:05:44 -08001052 fob.permit().fromApp(srManager.appId);
1053 return fob;
sangho80f11cb2015-04-01 13:05:26 -07001054 }
1055
1056 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001057 * Creates or removes filtering objectives for a double-tagged host on a port.
1058 *
1059 * @param deviceId device identifier
1060 * @param portNum port identifier for port to be filtered
1061 * @param outerVlan outer VLAN ID
1062 * @param innerVlan inner VLAN ID
1063 * @param install true to install the filtering objective, false to remove
1064 */
1065 void processDoubleTaggedFilter(DeviceId deviceId, PortNumber portNum, VlanId outerVlan,
1066 VlanId innerVlan, boolean install) {
1067 FilteringObjective.Builder fob = buildDoubleTaggedFilteringObj(deviceId, portNum, outerVlan, innerVlan);
1068 if (fob == null) {
1069 // error encountered during build
1070 return;
1071 }
1072 log.debug("{} double-tagged filtering objectives for dev/port: {}/{}",
1073 install ? "Installing" : "Removing", deviceId, portNum);
1074 ObjectiveContext context = new DefaultObjectiveContext(
1075 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portNum,
1076 install ? "installed" : "removed"),
1077 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
1078 install ? "install" : "remove", deviceId, portNum, error));
1079 if (install) {
1080 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
1081 } else {
1082 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
1083 }
1084 }
1085
1086 private FilteringObjective.Builder buildDoubleTaggedFilteringObj(DeviceId deviceId, PortNumber portNum,
1087 VlanId outerVlan, VlanId innerVlan) {
1088 MacAddress deviceMac;
1089 try {
1090 deviceMac = config.getDeviceMac(deviceId);
1091 } catch (DeviceConfigNotFoundException e) {
1092 log.warn(e.getMessage() + " Processing DoubleTaggedFilters aborted");
1093 return null;
1094 }
1095 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
1096 // Outer vlan id match should be appeared before inner vlan id match.
1097 fob.withKey(Criteria.matchInPort(portNum))
1098 .addCondition(Criteria.matchEthDst(deviceMac))
1099 .addCondition(Criteria.matchVlanId(outerVlan))
1100 .addCondition(Criteria.matchVlanId(innerVlan))
1101 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
1102
1103 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1104 // Pop outer vlan
1105 tBuilder.popVlan();
1106
1107 // NOTE: Some switch hardware share the same filtering flow among different ports.
1108 // We use this metadata to let the driver know that there is no more enabled port
1109 // within the same VLAN on this device.
1110 if (noMoreEnabledPort(deviceId, outerVlan)) {
1111 tBuilder.wipeDeferred();
1112 }
1113
1114 fob.withMeta(tBuilder.build());
1115
1116 fob.permit().fromApp(srManager.appId);
1117 return fob;
1118 }
1119
1120 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -07001121 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -07001122 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -07001123 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -07001124 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -07001125 *
Saurav Das9f1c42e2015-10-23 10:51:11 -07001126 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -07001127 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001128 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -07001129 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chanef8d12e2017-12-05 21:07:38 -08001130 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -08001131 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -08001132 routerIpv4 = config.getRouterIpv4(deviceId);
1133 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chanef8d12e2017-12-05 21:07:38 -08001134 routerLinkLocalIpv6 = Ip6Address.valueOf(
1135 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
1136
Saurav Das261c3002017-06-13 15:35:54 -07001137 if (config.isPairedEdge(deviceId)) {
1138 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
1139 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
1140 }
Charles Chan319d1a22015-11-03 10:42:14 -08001141 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -08001142 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -08001143 return;
1144 }
1145
Saurav Dasc28b3432015-10-30 17:45:38 -07001146 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1147 log.debug("Not installing port-IP punts - not the master for dev:{} ",
1148 deviceId);
1149 return;
1150 }
Pier Ventreadb4ae62016-11-23 09:57:42 -08001151 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
1152 allIps.add(routerIpv4);
Charles Chanef8d12e2017-12-05 21:07:38 -08001153 allIps.add(routerLinkLocalIpv6);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001154 if (routerIpv6 != null) {
1155 allIps.add(routerIpv6);
1156 }
Saurav Das261c3002017-06-13 15:35:54 -07001157 if (pairRouterIpv4 != null) {
1158 allIps.add(pairRouterIpv4);
1159 }
1160 if (pairRouterIpv6 != null) {
1161 allIps.add(pairRouterIpv6);
1162 }
Pier Ventreadb4ae62016-11-23 09:57:42 -08001163 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001164 populateSingleIpPunts(deviceId, ipaddr);
1165 }
1166 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -08001167
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001168 /**
1169 * Creates a forwarding objective to punt all IP packets, destined to the
1170 * specified IP address, which should be router's port IP address.
1171 *
1172 * @param deviceId the switch dpid for the router
1173 * @param ipAddress the IP address of the router's port
1174 */
1175 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
1176 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
1177 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
1178
1179 srManager.packetService.requestPackets(sbuilder.build(),
1180 PacketPriority.CONTROL, srManager.appId, optDeviceId);
1181 }
1182
1183 /**
1184 * Removes a forwarding objective to punt all IP packets, destined to the
1185 * specified IP address, which should be router's port IP address.
1186 *
1187 * @param deviceId the switch dpid for the router
1188 * @param ipAddress the IP address of the router's port
1189 */
1190 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
1191 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
1192 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
1193
1194 try {
1195 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
1196 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
1197 srManager.packetService.cancelPackets(sbuilder.build(),
1198 PacketPriority.CONTROL, srManager.appId, optDeviceId);
1199 }
1200 } catch (DeviceConfigNotFoundException e) {
1201 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -07001202 }
sangho80f11cb2015-04-01 13:05:26 -07001203 }
1204
Charles Chanf4586112015-11-09 16:37:23 -08001205 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -08001206 * Method to build IPv4 or IPv6 selector.
1207 *
1208 * @param addressToMatch the address to match
1209 */
1210 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
1211 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
1212 }
1213
1214 /**
1215 * Method to build IPv4 or IPv6 selector.
1216 *
1217 * @param prefixToMatch the prefix to match
1218 */
1219 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
1220 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -07001221 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -08001222 if (prefixToMatch.isIp4()) {
1223 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
1224 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
1225 return selectorBuilder;
1226 }
Pier Ventre229fd0b2016-10-31 16:49:19 -07001227 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -08001228 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
1229 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
1230 return selectorBuilder;
1231 }
1232
1233 /**
Pier Luigib9632ba2017-01-12 18:14:58 -08001234 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
1235 * Furthermore, these are applied only by the master instance. Deferred actions
1236 * are not cleared such that packets can be flooded in the cross connect use case
1237 *
1238 * @param deviceId the switch dpid for the router
1239 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001240 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -07001241 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -08001242 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1243 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
1244 deviceId);
1245 return;
1246 }
1247
Charles Chan3ed34d82017-06-22 18:03:14 -07001248 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -08001249 // We punt all ARP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -08001250 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigib9632ba2017-01-12 18:14:58 -08001251 .add(new ObjectiveContext() {
1252 @Override
1253 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001254 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -08001255 deviceId, error);
1256 }
1257 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001258 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -08001259
Pier Luigib9632ba2017-01-12 18:14:58 -08001260 // We punt all NDP packets towards the controller.
Charles Chan051490d2018-01-11 11:48:18 -08001261 ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> {
1262 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1263 @Override
1264 public void onError(Objective objective, ObjectiveError error) {
1265 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
1266 deviceId, error);
1267 }
1268 });
1269 srManager.flowObjectiveService.forward(deviceId, obj);
1270 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001271
1272 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
1273 ForwardingObjective pairFwdObj;
1274 // Do not punt ARP packets from pair port
1275 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1276 .add(new ObjectiveContext() {
1277 @Override
1278 public void onError(Objective objective, ObjectiveError error) {
1279 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1280 deviceId, error);
1281 }
1282 });
1283 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1284
1285 // Do not punt NDP packets from pair port
Charles Chan051490d2018-01-11 11:48:18 -08001286 ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> {
1287 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1288 @Override
1289 public void onError(Objective objective, ObjectiveError error) {
1290 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1291 deviceId, error);
1292 }
1293 });
1294 srManager.flowObjectiveService.forward(deviceId, obj);
1295 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001296
1297 // Do not forward DAD packets from pair port
1298 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1299 .add(new ObjectiveContext() {
1300 @Override
1301 public void onError(Objective objective, ObjectiveError error) {
1302 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1303 deviceId, error);
1304 }
1305 });
1306 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1307 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001308 }
1309
Charles Chan3ed34d82017-06-22 18:03:14 -07001310 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1311 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001312 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001313 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001314 .withSelector(selector)
1315 .fromApp(srManager.appId)
1316 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001317 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001318 .makePermanent();
1319 }
1320
Charles Chan3ed34d82017-06-22 18:03:14 -07001321 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001322 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1323 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001324 if (port != null) {
1325 sBuilder.matchInPort(port);
1326 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001327
Charles Chan3ed34d82017-06-22 18:03:14 -07001328 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1329 if (punt) {
1330 tBuilder.punt();
1331 }
1332 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001333 }
1334
Charles Chan051490d2018-01-11 11:48:18 -08001335 private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) {
1336 Set<ForwardingObjective.Builder> result = Sets.newHashSet();
Pier Luigib9632ba2017-01-12 18:14:58 -08001337
Charles Chan051490d2018-01-11 11:48:18 -08001338 Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT)
1339 .forEach(type -> {
1340 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1341 sBuilder.matchEthType(TYPE_IPV6)
1342 .matchIPProtocol(PROTOCOL_ICMP6)
1343 .matchIcmpv6Type(type);
1344 if (port != null) {
1345 sBuilder.matchInPort(port);
1346 }
1347
1348 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1349 if (punt) {
1350 tBuilder.punt();
1351 }
1352
1353 result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority));
1354 });
1355
1356 return result;
Charles Chan3ed34d82017-06-22 18:03:14 -07001357 }
1358
1359 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1360 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1361 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001362 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1363 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1364 // .matchIPProtocol(PROTOCOL_ICMP6)
1365 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001366 if (port != null) {
1367 sBuilder.matchInPort(port);
1368 }
1369
1370 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1371 tBuilder.wipeDeferred();
1372 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001373 }
1374
1375 /**
Charles Chanf4586112015-11-09 16:37:23 -08001376 * Populates a forwarding objective to send packets that miss other high
1377 * priority Bridging Table entries to a group that contains all ports of
1378 * its subnet.
1379 *
Charles Chanf4586112015-11-09 16:37:23 -08001380 * @param deviceId switch ID to set the rules
1381 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001382 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001383 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001384 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001385 });
1386 }
1387
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001388 /**
1389 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1390 * @param deviceId switch ID to set the rule
1391 * @param vlanId vlan ID to specify the subnet
1392 * @param install true to install the rule, false to revoke the rule
1393 */
1394 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1395 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1396
1397 if (nextId < 0) {
1398 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1399 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1400 return;
1401 }
1402
1403 // Driver should treat objective with MacAddress.NONE as the
1404 // subnet broadcast rule
1405 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1406 sbuilder.matchVlanId(vlanId);
1407 sbuilder.matchEthDst(MacAddress.NONE);
1408
1409 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1410 fob.withFlag(Flag.SPECIFIC)
1411 .withSelector(sbuilder.build())
1412 .nextStep(nextId)
1413 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1414 .fromApp(srManager.appId)
1415 .makePermanent();
1416 ObjectiveContext context = new DefaultObjectiveContext(
1417 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1418 (objective, error) ->
1419 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1420
1421 if (install) {
1422 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1423 } else {
1424 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1425 }
1426 }
1427
Charles Chan82ab1932016-01-30 23:22:37 -08001428 private int getPriorityFromPrefix(IpPrefix prefix) {
1429 return (prefix.isIp4()) ?
1430 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1431 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001432 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001433
1434 /**
1435 * Update Forwarding objective for each host and IP address connected to given port.
1436 * And create corresponding Simple Next objective if it does not exist.
1437 * Applied only when populating Forwarding objective
1438 * @param deviceId switch ID to set the rule
1439 * @param portNumber port number
1440 * @param prefix IP prefix of the route
1441 * @param hostMac MAC address of the next hop
1442 * @param vlanId Vlan ID of the port
1443 * @param popVlan true to pop vlan tag in TrafficTreatment
1444 * @param install true to populate the forwarding objective, false to revoke
1445 */
1446 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1447 VlanId vlanId, boolean popVlan, boolean install) {
1448 ForwardingObjective.Builder fob;
1449 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1450 MacAddress deviceMac;
1451 try {
1452 deviceMac = config.getDeviceMac(deviceId);
1453 } catch (DeviceConfigNotFoundException e) {
1454 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1455 return;
1456 }
1457
1458 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1459 tbuilder.deferred()
1460 .setEthDst(hostMac)
1461 .setEthSrc(deviceMac)
1462 .setOutput(portNumber);
1463
1464 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1465
1466 if (!popVlan) {
1467 tbuilder.setVlanId(vlanId);
1468 } else {
1469 mbuilder.matchVlanId(vlanId);
1470 }
1471
1472 // if the objective is to revoke an existing rule, and for some reason
1473 // the next-objective does not exist, then a new one should not be created
1474 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1475 tbuilder.build(), mbuilder.build(), install);
1476 if (portNextObjId == -1) {
1477 // Warning log will come from getPortNextObjective method
1478 return;
1479 }
1480
1481 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1482 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1483 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1484
1485 ObjectiveContext context = new DefaultObjectiveContext(
1486 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1487 (objective, error) ->
1488 log.warn("Failed to {} IP rule for route {}: {}",
1489 install ? "install" : "revoke", prefix, error));
1490 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1491
1492 if (!install) {
1493 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1494 if (grpHandler == null) {
1495 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1496 } else {
1497 // Remove L3UG for the given port and host
1498 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1499 }
1500 }
1501 }
Charles Chanf17f66b2018-02-26 21:33:25 -08001502
1503 /**
1504 * Checks if there is other enabled port within the given VLAN on the given device.
1505 *
1506 * @param deviceId device ID
1507 * @param vlanId VLAN ID
1508 * @return true if there is no more port enabled within the given VLAN on the given device
1509 */
1510 boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) {
1511 Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream()
1512 .filter(Port::isEnabled)
1513 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1514 .collect(Collectors.toSet());
1515
1516 return enabledPorts.stream().noneMatch(cp ->
1517 // Given vlanId is included in the vlan-tagged configuration
1518 srManager.getTaggedVlanId(cp).contains(vlanId) ||
1519 // Given vlanId is INTERNAL_VLAN and the interface is not configured
1520 (srManager.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null &&
1521 vlanId.equals(INTERNAL_VLAN)) ||
1522 // interface is configured and either vlan-untagged or vlan-native matches given vlanId
1523 (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId))
1524 );
1525 }
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001526
1527 /**
1528 * Returns a forwarding objective builder for egress forwarding rules.
1529 * <p>
1530 * The forwarding objective installs flow rules to egress pipeline to push
1531 * two vlan headers with given inner, outer vlan ids and outer tpid.
1532 *
1533 * @param portNumber port where the next hop attaches to
1534 * @param dummyVlanId vlan ID of the packet to match
1535 * @param innerVlan inner vlan ID of the next hop
1536 * @param outerVlan outer vlan ID of the next hop
1537 * @param outerTpid outer TPID of the next hop
1538 * @return forwarding objective builder
1539 */
1540 private ForwardingObjective.Builder egressFwdObjBuilder(PortNumber portNumber, VlanId dummyVlanId,
1541 VlanId innerVlan, VlanId outerVlan, EthType outerTpid) {
1542 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1543 sbuilder.matchVlanId(dummyVlanId);
1544 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1545 tbuilder.setOutput(portNumber).setVlanId(innerVlan);
1546
1547 if (outerTpid.equals(EthType.EtherType.QINQ.ethType())) {
1548 tbuilder.pushVlan(outerTpid);
1549 } else {
1550 tbuilder.pushVlan();
1551 }
1552
1553 tbuilder.setVlanId(outerVlan);
1554 return DefaultForwardingObjective.builder()
1555 .withSelector(sbuilder.build())
1556 .withTreatment(tbuilder.build())
1557 .fromApp(srManager.appId)
1558 .makePermanent()
1559 .withPriority(DEFAULT_PRIORITY)
1560 .withFlag(ForwardingObjective.Flag.EGRESS);
1561 }
1562
1563 /**
1564 * Populates IP rules for a route that has double-tagged next hop.
1565 *
1566 * @param deviceId device ID of the device that next hop attaches to
1567 * @param prefix IP prefix of the route
1568 * @param hostMac MAC address of the next hop
1569 * @param dummyVlan Dummy Vlan ID allocated for this route
1570 * @param innerVlan inner Vlan ID of the next hop
1571 * @param outerVlan outer Vlan ID of the next hop
1572 * @param outerTpid outer TPID of the next hop
1573 * @param outPort port where the next hop attaches to
1574 */
1575 void populateDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac, VlanId dummyVlan,
1576 VlanId innerVlan, VlanId outerVlan, EthType outerTpid, PortNumber outPort) {
1577 ForwardingObjective.Builder fwdBuilder;
1578 log.debug("Populate direct routing entry for double-tagged host route {} at {}:{}",
1579 prefix, deviceId, outPort);
1580
1581 ForwardingObjective.Builder egressFwdBuilder = egressFwdObjBuilder(
1582 outPort, dummyVlan, innerVlan, outerVlan, outerTpid);
1583 DefaultObjectiveContext egressFwdContext = new DefaultObjectiveContext(
1584 objective -> log.debug("Egress rule for IP {} is populated", prefix.address()),
1585 (objective, error) -> {
1586 log.warn("Failed to populate egress rule for IP {}: {}", prefix.address(), error);
1587 srManager.dummyVlanIdStore().remove(new DummyVlanIdStoreKey(
1588 new ConnectPoint(deviceId, outPort), prefix.address()
1589 ));
1590 });
1591 try {
1592 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, dummyVlan, outPort, false);
1593 } catch (DeviceConfigNotFoundException e) {
1594 log.error(e.getMessage() + " Aborting populateDoubleTaggedRoute");
1595 return;
1596 }
1597 if (fwdBuilder == null) {
1598 log.error("Aborting double-tagged host routing table entry due to error for dev:{} route:{}",
1599 deviceId, prefix);
1600 return;
1601 }
1602
1603 // Egress forwarding objective should be installed after the nextObjective for the output port is installed.
1604 // Installation of routingFwdObj will ensure the installation of the nextObjective.
1605 int nextId = fwdBuilder.add().nextId();
1606 DefaultObjectiveContext context = new DefaultObjectiveContext(objective -> {
1607 log.debug("Direct routing rule for double-tagged host route {} populated. nextId={}", prefix, nextId);
1608 srManager.flowObjectiveService.forward(deviceId, egressFwdBuilder.add(egressFwdContext));
1609 }, (objective, error) ->
1610 log.warn("Failed to populate direct routing rule for double-tagged host route {}: {}", prefix, error)
1611 );
1612 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
1613 rulePopulationCounter.incrementAndGet();
1614 }
1615
1616 /**
1617 * Revokes IP rules for a route that has double-tagged next hop.
1618 *
1619 * @param deviceId device ID of the device that next hop attaches to
1620 * @param prefix IP prefix of the route
1621 * @param hostMac MAC address of the next hop
1622 * @param hostVlan Vlan ID of the next hop
1623 * @param innerVlan inner Vlan ID of the next hop
1624 * @param outerVlan outer Vlan ID of the next hop
1625 * @param outerTpid outer TPID of the next hop
1626 * @param outPort port where the next hop attaches to
1627 */
1628 void revokeDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac,
1629 VlanId hostVlan, VlanId innerVlan, VlanId outerVlan,
1630 EthType outerTpid, PortNumber outPort) {
1631 revokeRoute(deviceId, prefix, hostMac, hostVlan, outPort);
1632
1633 DummyVlanIdStoreKey key = new DummyVlanIdStoreKey(
1634 new ConnectPoint(deviceId, outPort), prefix.address());
1635 VlanId dummyVlanId = srManager.dummyVlanIdStore().get(key);
1636 if (dummyVlanId == null) {
1637 log.warn("Failed to retrieve dummy VLAN ID for {}/{} and {}",
1638 deviceId, outPort, prefix.address());
1639 return;
1640 }
1641 ForwardingObjective.Builder fob = egressFwdObjBuilder(
1642 outPort, dummyVlanId, innerVlan, outerVlan, outerTpid);
1643 DefaultObjectiveContext context = new DefaultObjectiveContext(objective -> {
1644 log.debug("Egress rule for IP {} revoked", prefix.address());
1645 srManager.dummyVlanIdStore().remove(key);
1646 }, (objective, error) -> {
1647 log.warn("Failed to revoke egress rule for IP {}: {}", prefix.address(), error);
1648 });
1649 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1650 }
1651
sangho80f11cb2015-04-01 13:05:26 -07001652}