blob: d00a258a7fe3f5f136bdf7ce8116bd6202efbb75 [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;
Jonghwan Hyun671a7ab2018-04-30 09:27:21 -070035import org.onosproject.net.intf.Interface;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080036import org.onosproject.net.packet.PacketPriority;
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
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -070082import static org.onosproject.segmentrouting.SegmentRoutingManager.PSEUDOWIRE_VLAN;
83
Charles Chanb7f75ac2016-01-11 18:28:54 -080084/**
85 * Populator of segment routing flow rules.
86 */
sangho80f11cb2015-04-01 13:05:26 -070087public class RoutingRulePopulator {
Charles Chanef8d12e2017-12-05 21:07:38 -080088 private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class);
89
90 private static final int ARP_NDP_PRIORITY = 30000;
sangho80f11cb2015-04-01 13:05:26 -070091
sanghofb7c7292015-04-13 15:15:58 -070092 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070093 private SegmentRoutingManager srManager;
94 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070095
sangho80f11cb2015-04-01 13:05:26 -070096 /**
97 * Creates a RoutingRulePopulator object.
98 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070099 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -0700100 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700101 RoutingRulePopulator(SegmentRoutingManager srManager) {
sangho80f11cb2015-04-01 13:05:26 -0700102 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -0700103 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -0700104 this.rulePopulationCounter = new AtomicLong(0);
105 }
106
107 /**
108 * Resets the population counter.
109 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700110 void resetCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700111 rulePopulationCounter.set(0);
112 }
113
114 /**
115 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700116 *
117 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700118 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700119 long getCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700120 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700121 }
122
123 /**
Charles Chand66d6712018-03-29 16:03:41 -0700124 * Populate a bridging rule on given deviceId that matches given mac, given vlan and
125 * output to given port.
126 *
127 * @param deviceId device ID
128 * @param port port
129 * @param mac mac address
130 * @param vlanId VLAN ID
131 */
132 void populateBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
133 ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, false);
134 if (fob == null) {
135 log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId);
136 return;
137 }
138
139 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das5ff8a3c2018-04-07 16:51:09 -0700140 (objective) -> log.debug("Brigding rule for {}/{} populated", mac, vlanId),
141 (objective, error) -> log.warn("Failed to populate bridging rule for {}/{}: {}", mac, vlanId, error));
Charles Chand66d6712018-03-29 16:03:41 -0700142 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
143 }
144
145 /**
146 * Revoke a bridging rule on given deviceId that matches given mac, given vlan and
147 * output to given port.
148 *
149 * @param deviceId device ID
150 * @param port port
151 * @param mac mac address
152 * @param vlanId VLAN ID
153 */
154 void revokeBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
155 ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, true);
156 if (fob == null) {
157 log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId);
158 return;
159 }
160
161 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das5ff8a3c2018-04-07 16:51:09 -0700162 (objective) -> log.debug("Brigding rule for {}/{} revoked", mac, vlanId),
163 (objective, error) -> log.warn("Failed to revoke bridging rule for {}/{}: {}", mac, vlanId, error));
Charles Chand66d6712018-03-29 16:03:41 -0700164 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
165 }
166
167 /**
168 * Generates a forwarding objective builder for bridging rules.
169 * <p>
170 * The forwarding objective bridges packets destined to a given MAC to
171 * given port on given device.
172 *
173 * @param deviceId Device that host attaches to
174 * @param mac MAC address of the host
175 * @param hostVlanId VLAN ID of the host
176 * @param outport Port that host attaches to
177 * @param revoke true if forwarding objective is meant to revoke forwarding rule
178 * @return Forwarding objective builder
179 */
180 private ForwardingObjective.Builder bridgingFwdObjBuilder(
181 DeviceId deviceId, MacAddress mac, VlanId hostVlanId, PortNumber outport, boolean revoke) {
182 ConnectPoint connectPoint = new ConnectPoint(deviceId, outport);
Charles Chan098ca202018-05-01 11:50:20 -0700183 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
184 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
185 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
Charles Chand66d6712018-03-29 16:03:41 -0700186
187 // Create host selector
188 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
189 sbuilder.matchEthDst(mac);
190
191 // Create host treatment
192 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
193 tbuilder.immediate().setOutput(outport);
194
195 // Create host meta
196 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
197
198 // Adjust the selector, treatment and meta according to VLAN configuration
199 if (taggedVlans.contains(hostVlanId)) {
200 sbuilder.matchVlanId(hostVlanId);
201 mbuilder.matchVlanId(hostVlanId);
202 } else if (hostVlanId.equals(VlanId.NONE)) {
203 if (untaggedVlan != null) {
204 sbuilder.matchVlanId(untaggedVlan);
205 mbuilder.matchVlanId(untaggedVlan);
206 tbuilder.immediate().popVlan();
207 } else if (nativeVlan != null) {
208 sbuilder.matchVlanId(nativeVlan);
209 mbuilder.matchVlanId(nativeVlan);
210 tbuilder.immediate().popVlan();
211 } else {
212 log.warn("Untagged host {}/{} is not allowed on {} without untagged or native" +
213 "vlan config", mac, hostVlanId, connectPoint);
214 return null;
215 }
216 } else {
217 log.warn("Tagged host {}/{} is not allowed on {} without VLAN listed in tagged vlan",
218 mac, hostVlanId, connectPoint);
219 return null;
220 }
221
222 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
223 // If the objective is to revoke an existing rule, and for some reason
224 // the next-objective does not exist, then a new one should not be created
225 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outport,
226 tbuilder.build(), mbuilder.build(), !revoke);
227 if (portNextObjId == -1) {
228 // Warning log will come from getPortNextObjective method
229 return null;
230 }
231
232 return DefaultForwardingObjective.builder()
233 .withFlag(ForwardingObjective.Flag.SPECIFIC)
234 .withSelector(sbuilder.build())
235 .nextStep(portNextObjId)
236 .withPriority(100)
237 .fromApp(srManager.appId)
238 .makePermanent();
239 }
240
241 /**
242 * Populate or revoke a bridging rule on given deviceId that matches given vlanId,
243 * and hostMAC connected to given port, and output to given port only when
244 * vlan information is valid.
245 *
246 * @param deviceId device ID that host attaches to
247 * @param portNum port number that host attaches to
248 * @param hostMac mac address of the host connected to the switch port
249 * @param vlanId Vlan ID configured on the switch port
250 * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise
251 * @param install true to populate the objective, false to revoke
252 */
253 // TODO Refactor. There are a lot of duplications between this method, populateBridging,
254 // revokeBridging and bridgingFwdObjBuilder.
255 void updateBridging(DeviceId deviceId, PortNumber portNum, MacAddress hostMac,
256 VlanId vlanId, boolean popVlan, boolean install) {
257 // Create host selector
258 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
259 sbuilder.matchEthDst(hostMac);
260
261 // Create host meta
262 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
263
264 sbuilder.matchVlanId(vlanId);
265 mbuilder.matchVlanId(vlanId);
266
267 // Create host treatment
268 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
269 tbuilder.immediate().setOutput(portNum);
270
271 if (popVlan) {
272 tbuilder.immediate().popVlan();
273 }
274
275 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
276 tbuilder.build(), mbuilder.build(), install);
277 if (portNextObjId != -1) {
278 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder()
279 .withFlag(ForwardingObjective.Flag.SPECIFIC)
280 .withSelector(sbuilder.build())
281 .nextStep(portNextObjId)
282 .withPriority(100)
283 .fromApp(srManager.appId)
284 .makePermanent();
285
286 ObjectiveContext context = new DefaultObjectiveContext(
287 (objective) -> log.debug("Brigding rule for {}/{} {}", hostMac, vlanId,
288 install ? "populated" : "revoked"),
289 (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}",
290 install ? "populate" : "revoke", hostMac, vlanId, error));
291 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
292 } else {
293 log.warn("Failed to retrieve next objective for {}/{}", hostMac, vlanId);
294 }
295 }
296
297 /**
298 * Populates IP rules for a route that has direct connection to the switch.
299 * This method should not be invoked directly without going through DefaultRoutingHandler.
sangho80f11cb2015-04-01 13:05:26 -0700300 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700301 * @param deviceId device ID of the device that next hop attaches to
302 * @param prefix IP prefix of the route
303 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800304 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700305 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700306 */
Charles Chan910be6a2017-08-23 14:46:43 -0700307 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800308 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das261c3002017-06-13 15:35:54 -0700309 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700310 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800311 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800312 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700313 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
314 hostVlanId, outPort, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800315 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700316 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan319d1a22015-11-03 10:42:14 -0800317 return;
318 }
Saurav Das07c74602016-04-27 18:35:50 -0700319 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700320 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700321 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700322 return;
323 }
Charles Chan910be6a2017-08-23 14:46:43 -0700324
325 int nextId = fwdBuilder.add().nextId();
Charles Chan1eaf4802016-04-18 13:44:03 -0700326 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan910be6a2017-08-23 14:46:43 -0700327 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
328 prefix, nextId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700329 (objective, error) ->
Saurav Das261c3002017-06-13 15:35:54 -0700330 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das368cf212017-03-15 15:15:14 -0700331 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700332 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800333 rulePopulationCounter.incrementAndGet();
334 }
335
Charles Chanb7f75ac2016-01-11 18:28:54 -0800336 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700337 * Removes IP rules for a route when the next hop is gone.
Charles Chand66d6712018-03-29 16:03:41 -0700338 * This method should not be invoked directly without going through DefaultRoutingHandler.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800339 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700340 * @param deviceId device ID of the device that next hop attaches to
341 * @param prefix IP prefix of the route
342 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800343 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700344 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800345 */
Charles Chan910be6a2017-08-23 14:46:43 -0700346 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800347 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700348 log.debug("Revoke IP table entry for route {} at {}:{}",
349 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800350 ForwardingObjective.Builder fwdBuilder;
351 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700352 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
353 hostVlanId, outPort, true);
Charles Chanf4586112015-11-09 16:37:23 -0800354 } catch (DeviceConfigNotFoundException e) {
355 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
356 return;
357 }
Charles Chanea702b12016-11-30 11:55:05 -0800358 if (fwdBuilder == null) {
359 log.warn("Aborting host routing table entries due "
360 + "to error for dev:{} route:{}", deviceId, prefix);
361 return;
362 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700363 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700364 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700365 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700366 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700367 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800368 }
369
Charles Chanddac7fd2016-10-27 14:19:48 -0700370 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800371 * Returns a forwarding objective builder for routing rules.
372 * <p>
373 * The forwarding objective routes packets destined to a given prefix to
374 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700375 *
376 * @param deviceId device ID
377 * @param prefix prefix that need to be routed
378 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800379 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700380 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700381 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chanddac7fd2016-10-27 14:19:48 -0700382 * @return forwarding objective builder
383 * @throws DeviceConfigNotFoundException if given device is not configured
384 */
Charles Chan18fa4252017-02-08 16:10:40 -0800385 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700386 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700387 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
388 boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800389 throws DeviceConfigNotFoundException {
390 MacAddress deviceMac;
391 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800392
Charles Chan90772a72017-02-08 15:52:08 -0800393 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
Charles Chan098ca202018-05-01 11:50:20 -0700394 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
395 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
396 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700397
Charles Chan90772a72017-02-08 15:52:08 -0800398 // Create route selector
399 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
400
401 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700402 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700403 tbuilder.deferred()
404 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800405 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700406 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800407
Charles Chan90772a72017-02-08 15:52:08 -0800408 // Create route meta
409 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800410
Charles Chan90772a72017-02-08 15:52:08 -0800411 // Adjust the meta according to VLAN configuration
412 if (taggedVlans.contains(hostVlanId)) {
413 tbuilder.setVlanId(hostVlanId);
414 } else if (hostVlanId.equals(VlanId.NONE)) {
415 if (untaggedVlan != null) {
416 mbuilder.matchVlanId(untaggedVlan);
417 } else if (nativeVlan != null) {
418 mbuilder.matchVlanId(nativeVlan);
419 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700420 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
421 hostMac, hostVlanId, connectPoint);
422 return null;
Charles Chan90772a72017-02-08 15:52:08 -0800423 }
424 } else {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700425 // Internally-assigned dummy VLAN id will be given as hostVlanId
426 // when destination is double-tagged.
427 VlanId vlanId = srManager.dummyVlanIdStore().get(
428 new DummyVlanIdStoreKey(connectPoint, prefix.address()));
429 if (vlanId != null && vlanId.equals(hostVlanId)) {
430 tbuilder.setVlanId(hostVlanId);
431 mbuilder.matchVlanId(VlanId.ANY);
432 } else {
433 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
434 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
435 return null;
436 }
Saurav Das07c74602016-04-27 18:35:50 -0700437 }
Saurav Das2cb38292017-03-29 19:09:17 -0700438 // if the objective is to revoke an existing rule, and for some reason
439 // the next-objective does not exist, then a new one should not be created
Charles Chan90772a72017-02-08 15:52:08 -0800440 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das2cb38292017-03-29 19:09:17 -0700441 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan90772a72017-02-08 15:52:08 -0800442 if (portNextObjId == -1) {
443 // Warning log will come from getPortNextObjective method
444 return null;
445 }
446
Charles Chanf4586112015-11-09 16:37:23 -0800447 return DefaultForwardingObjective.builder()
Charles Chan90772a72017-02-08 15:52:08 -0800448 .withSelector(sbuilder.build())
Saurav Das2d94d312015-11-24 23:21:05 -0800449 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800450 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700451 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800452 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700453 }
454
455 /**
Saurav Das261c3002017-06-13 15:35:54 -0700456 * Populates IP flow rules for all the given prefixes reachable from the
457 * destination switch(es).
sangho80f11cb2015-04-01 13:05:26 -0700458 *
Saurav Das261c3002017-06-13 15:35:54 -0700459 * @param targetSw switch where rules are to be programmed
460 * @param subnets subnets/prefixes being added
461 * @param destSw1 destination switch where the prefixes are reachable
462 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
463 * Should be null if there is no paired destination switch (by config)
464 * or if the given prefixes are reachable only via destSw1
465 * @param nextHops a map containing a set of next-hops for each destination switch.
466 * If destSw2 is not null, then this map must contain an
467 * entry for destSw2 with its next-hops from the targetSw
468 * (although the next-hop set may be empty in certain scenarios).
469 * If destSw2 is null, there should not be an entry in this
470 * map for destSw2.
sangho80f11cb2015-04-01 13:05:26 -0700471 * @return true if all rules are set successfully, false otherwise
472 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700473 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das261c3002017-06-13 15:35:54 -0700474 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700475 for (IpPrefix subnet : subnets) {
Saurav Das261c3002017-06-13 15:35:54 -0700476 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sangho80f11cb2015-04-01 13:05:26 -0700477 return false;
478 }
479 }
Charles Chanc22cef32016-04-29 14:38:22 -0700480 return true;
481 }
sangho80f11cb2015-04-01 13:05:26 -0700482
Charles Chanc22cef32016-04-29 14:38:22 -0700483 /**
Charles Chand66d6712018-03-29 16:03:41 -0700484 * Revokes IP flow rules for the subnets from given device.
Charles Chanc22cef32016-04-29 14:38:22 -0700485 *
Charles Chand66d6712018-03-29 16:03:41 -0700486 * @param targetSw target switch from which the subnets need to be removed
Charles Chanc22cef32016-04-29 14:38:22 -0700487 * @param subnets subnet being removed
488 * @return true if all rules are removed successfully, false otherwise
489 */
Charles Chand66d6712018-03-29 16:03:41 -0700490 boolean revokeIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700491 for (IpPrefix subnet : subnets) {
Charles Chand66d6712018-03-29 16:03:41 -0700492 if (!revokeIpRuleForRouter(targetSw, subnet)) {
Charles Chanc22cef32016-04-29 14:38:22 -0700493 return false;
494 }
495 }
sangho80f11cb2015-04-01 13:05:26 -0700496 return true;
497 }
498
499 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700500 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das261c3002017-06-13 15:35:54 -0700501 * is reachable via destination device(s).
sangho80f11cb2015-04-01 13:05:26 -0700502 *
Saurav Das261c3002017-06-13 15:35:54 -0700503 * @param targetSw target device ID to set the rules
504 * @param ipPrefix the IP prefix
505 * @param destSw1 destination switch where the prefixes are reachable
506 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
507 * Should be null if there is no paired destination switch (by config)
508 * or if the given prefixes are reachable only via destSw1
509 * @param nextHops map of destination switches and their next-hops.
510 * Should only contain destination switches that are
511 * actually meant to be routed to. If destSw2 is null, there
512 * should not be an entry for destSw2 in this map.
sangho80f11cb2015-04-01 13:05:26 -0700513 * @return true if all rules are set successfully, false otherwise
514 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700515 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das261c3002017-06-13 15:35:54 -0700516 IpPrefix ipPrefix, DeviceId destSw1,
517 DeviceId destSw2,
518 Map<DeviceId, Set<DeviceId>> nextHops) {
519 int segmentId1, segmentId2 = -1;
Charles Chan319d1a22015-11-03 10:42:14 -0800520 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800521 if (ipPrefix.isIp4()) {
Saurav Das261c3002017-06-13 15:35:54 -0700522 segmentId1 = config.getIPv4SegmentId(destSw1);
523 if (destSw2 != null) {
524 segmentId2 = config.getIPv4SegmentId(destSw2);
525 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800526 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700527 segmentId1 = config.getIPv6SegmentId(destSw1);
528 if (destSw2 != null) {
529 segmentId2 = config.getIPv6SegmentId(destSw2);
530 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800531 }
Charles Chan319d1a22015-11-03 10:42:14 -0800532 } catch (DeviceConfigNotFoundException e) {
533 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
534 return false;
535 }
sangho80f11cb2015-04-01 13:05:26 -0700536
Pier Ventreadb4ae62016-11-23 09:57:42 -0800537 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800538 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700539
Charles Chanf4586112015-11-09 16:37:23 -0800540 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das261c3002017-06-13 15:35:54 -0700541 DestinationSet ds;
Charles Chanf4586112015-11-09 16:37:23 -0800542 TrafficTreatment treatment;
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700543 DestinationSet.DestinationSetType dsType;
sangho80f11cb2015-04-01 13:05:26 -0700544
Saurav Das261c3002017-06-13 15:35:54 -0700545 if (destSw2 == null) {
546 // single dst - create destination set based on next-hop
Saurav Das97241862018-02-14 14:14:54 -0800547 // If the next hop is the same as the final destination, then MPLS
548 // label is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700549 Set<DeviceId> nhd1 = nextHops.get(destSw1);
550 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
551 tbuilder.immediate().decNwTtl();
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700552 ds = DestinationSet.createTypePushNone(destSw1);
Saurav Das261c3002017-06-13 15:35:54 -0700553 treatment = tbuilder.build();
554 } else {
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700555 ds = DestinationSet.createTypePushBos(segmentId1, destSw1);
Saurav Das261c3002017-06-13 15:35:54 -0700556 treatment = null;
557 }
558 } else {
559 // dst pair - IP rules for dst-pairs are always from other edge nodes
560 // the destination set needs to have both destinations, even if there
561 // are no next hops to one of them
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700562 ds = DestinationSet.createTypePushBos(segmentId1, destSw1, segmentId2, destSw2);
Charles Chanf4586112015-11-09 16:37:23 -0800563 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700564 }
565
Saurav Das4c35fc42015-11-20 15:27:53 -0800566 // setup metadata to pass to nextObjective - indicate the vlan on egress
567 // if needed by the switch pipeline. Since neighbor sets are always to
568 // other neighboring routers, there is no subnet assigned on those ports.
569 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800570 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das261c3002017-06-13 15:35:54 -0700571 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700572 if (grpHandler == null) {
573 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das261c3002017-06-13 15:35:54 -0700574 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700575 return false;
576 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800577
Saurav Das261c3002017-06-13 15:35:54 -0700578 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
Saurav Das97241862018-02-14 14:14:54 -0800579 metabuilder.build(), false);
Saurav Das4c35fc42015-11-20 15:27:53 -0800580 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700581 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho2165d222015-05-01 09:38:25 -0700582 return false;
583 }
584
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700585 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
586 .builder()
587 .fromApp(srManager.appId)
588 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800589 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700590 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800591 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700592 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800593 if (treatment != null) {
594 fwdBuilder.withTreatment(treatment);
595 }
Saurav Das62ae6792017-05-15 15:34:25 -0700596 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das261c3002017-06-13 15:35:54 -0700597 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700598 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700599 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700600 ipPrefix, targetSw),
Charles Chan1eaf4802016-04-18 13:44:03 -0700601 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700602 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700603 ipPrefix, error, targetSw));
604 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700605 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700606
607 return true;
608 }
609
sangho80f11cb2015-04-01 13:05:26 -0700610 /**
Charles Chand66d6712018-03-29 16:03:41 -0700611 * Revokes IP flow rules for the router IP address from given device.
Charles Chanc22cef32016-04-29 14:38:22 -0700612 *
Charles Chand66d6712018-03-29 16:03:41 -0700613 * @param targetSw target switch from which the ipPrefix need to be removed
Charles Chanc22cef32016-04-29 14:38:22 -0700614 * @param ipPrefix the IP address of the destination router
615 * @return true if all rules are removed successfully, false otherwise
616 */
Charles Chand66d6712018-03-29 16:03:41 -0700617 private boolean revokeIpRuleForRouter(DeviceId targetSw, IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800618 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700619 TrafficSelector selector = sbuilder.build();
620 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
621
622 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
623 .builder()
624 .fromApp(srManager.appId)
625 .makePermanent()
626 .withSelector(selector)
627 .withTreatment(dummyTreatment)
628 .withPriority(getPriorityFromPrefix(ipPrefix))
629 .withFlag(ForwardingObjective.Flag.SPECIFIC);
630
Charles Chand66d6712018-03-29 16:03:41 -0700631 ObjectiveContext context = new DefaultObjectiveContext(
632 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, targetSw),
633 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
634 ipPrefix, targetSw, error));
635 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.remove(context));
Charles Chanc22cef32016-04-29 14:38:22 -0700636
637 return true;
638 }
639
640 /**
Saurav Das97241862018-02-14 14:14:54 -0800641 * Populates MPLS flow rules in the target device to point towards the
642 * destination device.
643 *
644 * @param targetSwId target device ID of the switch to set the rules
645 * @param destSwId destination switch device ID
646 * @param nextHops next hops switch ID list
647 * @param routerIp the router ip of the destination switch
648 * @return true if all rules are set successfully, false otherwise
649 */
650 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
651 Set<DeviceId> nextHops, IpAddress routerIp) {
652 int segmentId;
653 try {
654 if (routerIp.isIp4()) {
655 segmentId = config.getIPv4SegmentId(destSwId);
656 } else {
657 segmentId = config.getIPv6SegmentId(destSwId);
658 }
659 } catch (DeviceConfigNotFoundException e) {
660 log.warn(e.getMessage() + " Aborting populateMplsRule.");
661 return false;
662 }
663
664 List<ForwardingObjective> fwdObjs = new ArrayList<>();
665 Collection<ForwardingObjective> fwdObjsMpls;
666 // Generates the transit rules used by the standard "routing".
667 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId,
668 routerIp, true);
669 if (fwdObjsMpls.isEmpty()) {
670 return false;
671 }
672 fwdObjs.addAll(fwdObjsMpls);
673
674 // Generates the transit rules used by the MPLS Pwaas.
675 int pwSrLabel;
676 try {
677 pwSrLabel = config.getPWRoutingLabel(destSwId);
678 } catch (DeviceConfigNotFoundException e) {
679 log.warn(e.getMessage()
680 + " Aborting populateMplsRule. No label for PseudoWire traffic.");
681 return false;
682 }
683 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel,
684 routerIp, false);
685 if (fwdObjsMpls.isEmpty()) {
686 return false;
687 }
688 fwdObjs.addAll(fwdObjsMpls);
689
690 for (ForwardingObjective fwdObj : fwdObjs) {
691 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
692 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
693 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
694 rulePopulationCounter.incrementAndGet();
695 }
696
697 return true;
698 }
699
700 /**
701 * Differentiates between popping and swapping labels when building an MPLS
702 * forwarding objective.
Pier Ventre229fd0b2016-10-31 16:49:19 -0700703 *
704 * @param targetSwId the target sw
705 * @param destSwId the destination sw
706 * @param nextHops the set of next hops
Saurav Das97241862018-02-14 14:14:54 -0800707 * @param segmentId the segmentId to match representing the destination
708 * switch
709 * @param routerIp the router ip representing the destination switch
Pier Ventre229fd0b2016-10-31 16:49:19 -0700710 * @return a collection of fwdobjective
711 */
Saurav Das261c3002017-06-13 15:35:54 -0700712 private Collection<ForwardingObjective> handleMpls(
713 DeviceId targetSwId,
714 DeviceId destSwId,
715 Set<DeviceId> nextHops,
716 int segmentId,
717 IpAddress routerIp,
718 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700719
720 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
721 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800722 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700723 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
724 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
725 sbuilder.matchMplsBos(isMplsBos);
726 TrafficSelector selector = sbuilder.build();
727
728 // setup metadata to pass to nextObjective - indicate the vlan on egress
729 // if needed by the switch pipeline. Since mpls next-hops are always to
730 // other neighboring routers, there is no subnet assigned on those ports.
731 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800732 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700733
734 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
735 // If the next hop is the destination router for the segment, do pop
736 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700737 + "label {} in switch {} with pop to next-hops {}",
738 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700739 ForwardingObjective.Builder fwdObjNoBosBuilder =
740 getMplsForwardingObjective(targetSwId,
741 nextHops,
742 true,
743 isMplsBos,
744 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700745 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800746 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700747 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700748 // Error case, we cannot handle, exit.
749 if (fwdObjNoBosBuilder == null) {
750 return Collections.emptyList();
751 }
752 fwdObjBuilders.add(fwdObjNoBosBuilder);
753
754 } else {
Saurav Das97241862018-02-14 14:14:54 -0800755 // next hop is not destination, irrespective of the number of next
756 // hops (1 or more) -- SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700757 log.debug("Installing MPLS forwarding objective for "
758 + "label {} in switch {} without pop to next-hops {}",
759 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700760 ForwardingObjective.Builder fwdObjNoBosBuilder =
761 getMplsForwardingObjective(targetSwId,
762 nextHops,
763 false,
764 isMplsBos,
765 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700766 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800767 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700768 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700769 // Error case, we cannot handle, exit.
770 if (fwdObjNoBosBuilder == null) {
771 return Collections.emptyList();
772 }
773 fwdObjBuilders.add(fwdObjNoBosBuilder);
774
775 }
776
777 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
778 // We add the final property to the fwdObjs.
779 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700780 ((Builder) ((Builder) fwdObjBuilder
781 .fromApp(srManager.appId)
782 .makePermanent())
783 .withSelector(selector)
784 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
785 .withFlag(ForwardingObjective.Flag.SPECIFIC);
786
787 ObjectiveContext context = new DefaultObjectiveContext(
788 (objective) ->
789 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
790 objective.id(), segmentId, targetSwId),
791 (objective, error) ->
792 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
793 objective.id(), segmentId, error, targetSwId));
794
795 ForwardingObjective fob = fwdObjBuilder.add(context);
796 fwdObjs.add(fob);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700797 }
798
799 return fwdObjs;
800 }
801
802 /**
Saurav Das97241862018-02-14 14:14:54 -0800803 * Returns a Forwarding Objective builder for the MPLS rule that references
804 * the desired Next Objective. Creates a DestinationSet that allows the
805 * groupHandler to create or find the required next objective.
sangho80f11cb2015-04-01 13:05:26 -0700806 *
Saurav Das97241862018-02-14 14:14:54 -0800807 * @param targetSw the target sw
808 * @param nextHops the set of next hops
809 * @param phpRequired true if penultimate-hop-popping is required
810 * @param isBos true if matched label is bottom-of-stack
811 * @param meta metadata for creating next objective
812 * @param routerIp the router ip representing the destination switch
813 * @param destSw the destination sw
814 * @return the mpls forwarding objective builder
sangho80f11cb2015-04-01 13:05:26 -0700815 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800816 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700817 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800818 Set<DeviceId> nextHops,
819 boolean phpRequired,
820 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800821 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700822 IpAddress routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800823 int segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700824 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800825
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700826 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
827 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700828
829 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das97241862018-02-14 14:14:54 -0800830 DestinationSet ds = null;
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700831 DestinationSet.DestinationSetType dstType = null;
Saurav Das97241862018-02-14 14:14:54 -0800832 boolean simple = false;
sangho80f11cb2015-04-01 13:05:26 -0700833 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800834 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700835 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700836 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700837 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800838 if (routerIp.isIp4()) {
839 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
840 } else {
841 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
842 }
843 tbuilder.decNwTtl();
Saurav Das97241862018-02-14 14:14:54 -0800844 // standard case -> BoS == True; pop results in IP packet and forwarding
845 // is via an ECMP group
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700846 ds = DestinationSet.createTypePopBos(destSw);
sangho80f11cb2015-04-01 13:05:26 -0700847 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800848 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
849 .decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800850 // double-label case -> BoS == False, pop results in MPLS packet
851 // depending on configuration we can ECMP this packet or choose one output
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700852 ds = DestinationSet.createTypePopNotBos(destSw);
853 if (!srManager.getMplsEcmp()) {
854 simple = true;
Saurav Das97241862018-02-14 14:14:54 -0800855 }
sangho80f11cb2015-04-01 13:05:26 -0700856 }
857 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800858 // swap with self case - SR CONTINUE
Saurav Das97241862018-02-14 14:14:54 -0800859 log.debug("getMplsForwardingObjective: swap with self");
sangho27462c62015-05-14 00:39:53 -0700860 tbuilder.deferred().decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800861 // swap results in MPLS packet with same BoS bit regardless of bit value
862 // depending on configuration we can ECMP this packet or choose one output
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700863 // differentiate here between swap with not bos or swap with bos
864 ds = isBos ? DestinationSet.createTypeSwapBos(segmentId, destSw) :
865 DestinationSet.createTypeSwapNotBos(segmentId, destSw);
866 if (!srManager.getMplsEcmp()) {
Saurav Das97241862018-02-14 14:14:54 -0800867 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);
Charles Chan9d2dd552018-06-19 20:56:33 -0700935 return new PortFilterInfo(disabledPorts, errorPorts, filteredPorts);
Saurav Dasf9332192017-02-18 14:05:44 -0800936 }
937
938 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800939 * Creates or removes filtering objectives for a single port. Should only be
940 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800941 *
942 * @param deviceId device identifier
943 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800944 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800945 * @return true if no errors occurred during the build of the filtering objective
946 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700947 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800948 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
Charles Chan098ca202018-05-01 11:50:20 -0700949 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
950 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
951 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
Charles Chan90772a72017-02-08 15:52:08 -0800952
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700953 // Do not configure filter for edge ports where double-tagged hosts are connected.
Charles Chan90772a72017-02-08 15:52:08 -0800954 if (taggedVlans.size() != 0) {
955 // Filter for tagged vlans
Charles Chan098ca202018-05-01 11:50:20 -0700956 if (!srManager.interfaceService.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800957 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800958 return false;
959 }
960 if (nativeVlan != null) {
961 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800962 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800963 return false;
964 }
965 }
966 } else if (untaggedVlan != null) {
967 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800968 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800969 return false;
970 }
Jonghwan Hyun671a7ab2018-04-30 09:27:21 -0700971 } else if (!hasIPConfiguration(connectPoint)) {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700972 // Filter for unconfigured upstream port, using INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800973 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800974 return false;
975 }
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700976 // Filter for receiveing pseudowire traffic
977 if (!processSinglePortFiltersInternal(deviceId, portnum, false, PSEUDOWIRE_VLAN, install)) {
978 return false;
979 }
Charles Chan90772a72017-02-08 15:52:08 -0800980 }
981 return true;
982 }
983
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700984 /**
985 * Updates filtering objectives for a single port. Should only be called by
986 * the master for a switch
987 * @param deviceId device identifier
988 * @param portNum port identifier for port to be filtered
989 * @param pushVlan true to push vlan, false otherwise
990 * @param vlanId vlan identifier
991 * @param install true to install the filtering objective, false to remove
992 */
993 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
994 boolean pushVlan, VlanId vlanId, boolean install) {
995 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
996 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
997 deviceId, portNum, vlanId);
998 }
999 }
1000
Charles Chan43be46b2017-02-26 22:59:35 -08001001 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
1002 boolean pushVlan, VlanId vlanId, boolean install) {
Daniel Ginsburg776789f2018-04-30 19:27:19 -04001003 boolean doTMAC = true;
1004
1005 if (!pushVlan) {
1006 // Skip the tagged vlans belonging to an interface without an IP address
1007 Set<Interface> ifaces = srManager.interfaceService
1008 .getInterfacesByPort(new ConnectPoint(deviceId, portnum))
1009 .stream()
1010 .filter(intf -> intf.vlanTagged().contains(vlanId) && intf.ipAddressesList().isEmpty())
1011 .collect(Collectors.toSet());
1012 if (!ifaces.isEmpty()) {
1013 log.debug("processSinglePortFiltersInternal: skipping TMAC for vlan {} at {}/{} - no IP",
1014 vlanId, deviceId, portnum);
1015 doTMAC = false;
1016 }
1017 }
1018
1019 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId, doTMAC);
Saurav Dasf9332192017-02-18 14:05:44 -08001020 if (fob == null) {
1021 // error encountered during build
1022 return false;
1023 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001024 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -07001025 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -08001026 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -08001027 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -07001028 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -08001029 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -07001030 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -08001031 if (install) {
1032 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
1033 } else {
1034 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -08001035 }
Charles Chan90772a72017-02-08 15:52:08 -08001036 return true;
Saurav Dasf9332192017-02-18 14:05:44 -08001037 }
1038
Charles Chan90772a72017-02-08 15:52:08 -08001039 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
Daniel Ginsburg776789f2018-04-30 19:27:19 -04001040 boolean pushVlan, VlanId vlanId, boolean doTMAC) {
Saurav Dasf9332192017-02-18 14:05:44 -08001041 MacAddress deviceMac;
1042 try {
1043 deviceMac = config.getDeviceMac(deviceId);
1044 } catch (DeviceConfigNotFoundException e) {
1045 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
1046 return null;
1047 }
Saurav Dasf9332192017-02-18 14:05:44 -08001048 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
Daniel Ginsburg776789f2018-04-30 19:27:19 -04001049
1050 if (doTMAC) {
1051 fob.withKey(Criteria.matchInPort(portnum))
1052 .addCondition(Criteria.matchEthDst(deviceMac))
1053 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
1054 } else {
1055 fob.withKey(Criteria.matchInPort(portnum))
1056 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
1057 }
Charles Chan90772a72017-02-08 15:52:08 -08001058
Charles Chan17ca2202017-12-19 19:55:57 -08001059 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1060
Charles Chan90772a72017-02-08 15:52:08 -08001061 if (pushVlan) {
1062 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan17ca2202017-12-19 19:55:57 -08001063 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan90772a72017-02-08 15:52:08 -08001064 } else {
1065 fob.addCondition(Criteria.matchVlanId(vlanId));
1066 }
1067
Charles Chan17ca2202017-12-19 19:55:57 -08001068 // NOTE: Some switch hardware share the same filtering flow among different ports.
1069 // We use this metadata to let the driver know that there is no more enabled port
1070 // within the same VLAN on this device.
Charles Chanf17f66b2018-02-26 21:33:25 -08001071 if (noMoreEnabledPort(deviceId, vlanId)) {
Charles Chan17ca2202017-12-19 19:55:57 -08001072 tBuilder.wipeDeferred();
1073 }
1074
1075 fob.withMeta(tBuilder.build());
1076
Saurav Dasf9332192017-02-18 14:05:44 -08001077 fob.permit().fromApp(srManager.appId);
1078 return fob;
sangho80f11cb2015-04-01 13:05:26 -07001079 }
1080
1081 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001082 * Creates or removes filtering objectives for a double-tagged host on a port.
1083 *
1084 * @param deviceId device identifier
1085 * @param portNum port identifier for port to be filtered
1086 * @param outerVlan outer VLAN ID
1087 * @param innerVlan inner VLAN ID
1088 * @param install true to install the filtering objective, false to remove
1089 */
1090 void processDoubleTaggedFilter(DeviceId deviceId, PortNumber portNum, VlanId outerVlan,
1091 VlanId innerVlan, boolean install) {
1092 FilteringObjective.Builder fob = buildDoubleTaggedFilteringObj(deviceId, portNum, outerVlan, innerVlan);
1093 if (fob == null) {
1094 // error encountered during build
1095 return;
1096 }
1097 log.debug("{} double-tagged filtering objectives for dev/port: {}/{}",
1098 install ? "Installing" : "Removing", deviceId, portNum);
1099 ObjectiveContext context = new DefaultObjectiveContext(
1100 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portNum,
1101 install ? "installed" : "removed"),
1102 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
1103 install ? "install" : "remove", deviceId, portNum, error));
1104 if (install) {
1105 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
1106 } else {
1107 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
1108 }
1109 }
1110
1111 private FilteringObjective.Builder buildDoubleTaggedFilteringObj(DeviceId deviceId, PortNumber portNum,
1112 VlanId outerVlan, VlanId innerVlan) {
1113 MacAddress deviceMac;
1114 try {
1115 deviceMac = config.getDeviceMac(deviceId);
1116 } catch (DeviceConfigNotFoundException e) {
1117 log.warn(e.getMessage() + " Processing DoubleTaggedFilters aborted");
1118 return null;
1119 }
1120 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
1121 // Outer vlan id match should be appeared before inner vlan id match.
1122 fob.withKey(Criteria.matchInPort(portNum))
1123 .addCondition(Criteria.matchEthDst(deviceMac))
1124 .addCondition(Criteria.matchVlanId(outerVlan))
1125 .addCondition(Criteria.matchVlanId(innerVlan))
1126 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
1127
1128 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1129 // Pop outer vlan
1130 tBuilder.popVlan();
1131
1132 // NOTE: Some switch hardware share the same filtering flow among different ports.
1133 // We use this metadata to let the driver know that there is no more enabled port
1134 // within the same VLAN on this device.
1135 if (noMoreEnabledPort(deviceId, outerVlan)) {
1136 tBuilder.wipeDeferred();
1137 }
1138
1139 fob.withMeta(tBuilder.build());
1140
1141 fob.permit().fromApp(srManager.appId);
1142 return fob;
1143 }
1144
1145 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -07001146 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -07001147 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -07001148 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -07001149 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -07001150 *
Saurav Das9f1c42e2015-10-23 10:51:11 -07001151 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -07001152 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001153 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -07001154 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chanef8d12e2017-12-05 21:07:38 -08001155 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -08001156 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -08001157 routerIpv4 = config.getRouterIpv4(deviceId);
1158 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chanef8d12e2017-12-05 21:07:38 -08001159 routerLinkLocalIpv6 = Ip6Address.valueOf(
1160 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
1161
Saurav Das261c3002017-06-13 15:35:54 -07001162 if (config.isPairedEdge(deviceId)) {
1163 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
1164 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
1165 }
Charles Chan319d1a22015-11-03 10:42:14 -08001166 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -08001167 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -08001168 return;
1169 }
1170
Saurav Dasc28b3432015-10-30 17:45:38 -07001171 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1172 log.debug("Not installing port-IP punts - not the master for dev:{} ",
1173 deviceId);
1174 return;
1175 }
Pier Ventreadb4ae62016-11-23 09:57:42 -08001176 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
1177 allIps.add(routerIpv4);
Charles Chanef8d12e2017-12-05 21:07:38 -08001178 allIps.add(routerLinkLocalIpv6);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001179 if (routerIpv6 != null) {
1180 allIps.add(routerIpv6);
1181 }
Saurav Das261c3002017-06-13 15:35:54 -07001182 if (pairRouterIpv4 != null) {
1183 allIps.add(pairRouterIpv4);
1184 }
1185 if (pairRouterIpv6 != null) {
1186 allIps.add(pairRouterIpv6);
1187 }
Pier Ventreadb4ae62016-11-23 09:57:42 -08001188 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001189 populateSingleIpPunts(deviceId, ipaddr);
1190 }
1191 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -08001192
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001193 /**
1194 * Creates a forwarding objective to punt all IP packets, destined to the
1195 * specified IP address, which should be router's port IP address.
1196 *
1197 * @param deviceId the switch dpid for the router
1198 * @param ipAddress the IP address of the router's port
1199 */
1200 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
1201 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
1202 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
1203
1204 srManager.packetService.requestPackets(sbuilder.build(),
1205 PacketPriority.CONTROL, srManager.appId, optDeviceId);
1206 }
1207
1208 /**
1209 * Removes a forwarding objective to punt all IP packets, destined to the
1210 * specified IP address, which should be router's port IP address.
1211 *
1212 * @param deviceId the switch dpid for the router
1213 * @param ipAddress the IP address of the router's port
1214 */
1215 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
1216 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
1217 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
1218
1219 try {
1220 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
1221 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
1222 srManager.packetService.cancelPackets(sbuilder.build(),
1223 PacketPriority.CONTROL, srManager.appId, optDeviceId);
1224 }
1225 } catch (DeviceConfigNotFoundException e) {
1226 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -07001227 }
sangho80f11cb2015-04-01 13:05:26 -07001228 }
1229
Charles Chanf4586112015-11-09 16:37:23 -08001230 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -08001231 * Method to build IPv4 or IPv6 selector.
1232 *
1233 * @param addressToMatch the address to match
1234 */
1235 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
1236 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
1237 }
1238
1239 /**
1240 * Method to build IPv4 or IPv6 selector.
1241 *
1242 * @param prefixToMatch the prefix to match
1243 */
1244 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
1245 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -07001246 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -08001247 if (prefixToMatch.isIp4()) {
1248 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
1249 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
1250 return selectorBuilder;
1251 }
Pier Ventre229fd0b2016-10-31 16:49:19 -07001252 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -08001253 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
1254 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
1255 return selectorBuilder;
1256 }
1257
1258 /**
Pier Luigib9632ba2017-01-12 18:14:58 -08001259 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
1260 * Furthermore, these are applied only by the master instance. Deferred actions
1261 * are not cleared such that packets can be flooded in the cross connect use case
1262 *
1263 * @param deviceId the switch dpid for the router
1264 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001265 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -07001266 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -08001267 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1268 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
1269 deviceId);
1270 return;
1271 }
1272
Charles Chan3ed34d82017-06-22 18:03:14 -07001273 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -08001274 // We punt all ARP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -08001275 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigib9632ba2017-01-12 18:14:58 -08001276 .add(new ObjectiveContext() {
1277 @Override
1278 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001279 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -08001280 deviceId, error);
1281 }
1282 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001283 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -08001284
Pier Luigib9632ba2017-01-12 18:14:58 -08001285 // We punt all NDP packets towards the controller.
Charles Chan051490d2018-01-11 11:48:18 -08001286 ndpFwdObjective(null, true, ARP_NDP_PRIORITY).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 punt NDP to {}: {}",
1291 deviceId, error);
1292 }
1293 });
1294 srManager.flowObjectiveService.forward(deviceId, obj);
1295 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001296
Saurav Dasec683dc2018-04-27 18:42:30 -07001297 srManager.getPairLocalPort(deviceId).ifPresent(port -> {
Charles Chan3ed34d82017-06-22 18:03:14 -07001298 ForwardingObjective pairFwdObj;
1299 // Do not punt ARP packets from pair port
1300 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1301 .add(new ObjectiveContext() {
1302 @Override
1303 public void onError(Objective objective, ObjectiveError error) {
1304 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1305 deviceId, error);
1306 }
1307 });
1308 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1309
1310 // Do not punt NDP packets from pair port
Charles Chan051490d2018-01-11 11:48:18 -08001311 ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> {
1312 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1313 @Override
1314 public void onError(Objective objective, ObjectiveError error) {
1315 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1316 deviceId, error);
1317 }
1318 });
1319 srManager.flowObjectiveService.forward(deviceId, obj);
1320 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001321
1322 // Do not forward DAD packets from pair port
1323 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1324 .add(new ObjectiveContext() {
1325 @Override
1326 public void onError(Objective objective, ObjectiveError error) {
1327 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1328 deviceId, error);
1329 }
1330 });
1331 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1332 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001333 }
1334
Charles Chan3ed34d82017-06-22 18:03:14 -07001335 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1336 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001337 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001338 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001339 .withSelector(selector)
1340 .fromApp(srManager.appId)
1341 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001342 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001343 .makePermanent();
1344 }
1345
Charles Chan3ed34d82017-06-22 18:03:14 -07001346 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001347 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1348 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001349 if (port != null) {
1350 sBuilder.matchInPort(port);
1351 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001352
Charles Chan3ed34d82017-06-22 18:03:14 -07001353 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1354 if (punt) {
1355 tBuilder.punt();
1356 }
1357 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001358 }
1359
Charles Chan051490d2018-01-11 11:48:18 -08001360 private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) {
1361 Set<ForwardingObjective.Builder> result = Sets.newHashSet();
Pier Luigib9632ba2017-01-12 18:14:58 -08001362
Charles Chan051490d2018-01-11 11:48:18 -08001363 Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT)
1364 .forEach(type -> {
1365 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1366 sBuilder.matchEthType(TYPE_IPV6)
1367 .matchIPProtocol(PROTOCOL_ICMP6)
1368 .matchIcmpv6Type(type);
1369 if (port != null) {
1370 sBuilder.matchInPort(port);
1371 }
1372
1373 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1374 if (punt) {
1375 tBuilder.punt();
1376 }
1377
1378 result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority));
1379 });
1380
1381 return result;
Charles Chan3ed34d82017-06-22 18:03:14 -07001382 }
1383
1384 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1385 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1386 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001387 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1388 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1389 // .matchIPProtocol(PROTOCOL_ICMP6)
1390 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001391 if (port != null) {
1392 sBuilder.matchInPort(port);
1393 }
1394
1395 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1396 tBuilder.wipeDeferred();
1397 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001398 }
1399
1400 /**
Charles Chanf4586112015-11-09 16:37:23 -08001401 * Populates a forwarding objective to send packets that miss other high
1402 * priority Bridging Table entries to a group that contains all ports of
1403 * its subnet.
1404 *
Andrea Campanella60ce2222018-04-30 11:48:55 +02001405 * @param address the address to block
1406 * @param deviceId switch ID to set the rules
1407 */
1408 void populateDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address) {
1409 updateDefaultRouteBlackhole(deviceId, address, true);
1410 }
1411
1412 /**
1413 * Populates a forwarding objective to send packets that miss other high
1414 * priority Bridging Table entries to a group that contains all ports of
1415 * its subnet.
1416 *
1417 * @param address the address to block
1418 * @param deviceId switch ID to set the rules
1419 */
1420 void removeDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address) {
1421 updateDefaultRouteBlackhole(deviceId, address, false);
1422 }
1423
1424 private void updateDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address, boolean install) {
1425 try {
1426 if (srManager.deviceConfiguration.isEdgeDevice(deviceId)) {
1427
1428 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1429 if (address.isIp4()) {
1430 sbuilder.matchIPDst(address);
1431 sbuilder.matchEthType(EthType.EtherType.IPV4.ethType().toShort());
1432 } else {
1433 sbuilder.matchIPv6Dst(address);
1434 sbuilder.matchEthType(EthType.EtherType.IPV6.ethType().toShort());
1435 }
1436
1437 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1438
1439 tBuilder.transition(60);
1440 tBuilder.wipeDeferred();
1441
1442 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1443 fob.withFlag(Flag.SPECIFIC)
1444 .withSelector(sbuilder.build())
1445 .withTreatment(tBuilder.build())
1446 .withPriority(getPriorityFromPrefix(address))
1447 .fromApp(srManager.appId)
1448 .makePermanent();
1449
1450 log.debug("{} blackhole forwarding objectives for dev: {}",
1451 install ? "Installing" : "Removing", deviceId);
1452 ObjectiveContext context = new DefaultObjectiveContext(
1453 (objective) -> log.debug("Forward for {} {}", deviceId,
1454 install ? "installed" : "removed"),
1455 (objective, error) -> log.warn("Failed to {} forward for {}: {}",
1456 install ? "install" : "remove", deviceId, error));
1457 if (install) {
1458 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1459 } else {
1460 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1461 }
1462 }
1463 } catch (DeviceConfigNotFoundException e) {
1464 log.info("Not populating blackhole for un-configured device {}", deviceId);
1465 }
1466
1467 }
1468
1469 /**
1470 * Populates a forwarding objective to send packets that miss other high
1471 * priority Bridging Table entries to a group that contains all ports of
1472 * its subnet.
1473 *
Charles Chanf4586112015-11-09 16:37:23 -08001474 * @param deviceId switch ID to set the rules
1475 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001476 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001477 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001478 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001479 });
1480 }
1481
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001482 /**
1483 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1484 * @param deviceId switch ID to set the rule
1485 * @param vlanId vlan ID to specify the subnet
1486 * @param install true to install the rule, false to revoke the rule
1487 */
1488 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1489 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1490
1491 if (nextId < 0) {
1492 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1493 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1494 return;
1495 }
1496
1497 // Driver should treat objective with MacAddress.NONE as the
1498 // subnet broadcast rule
1499 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1500 sbuilder.matchVlanId(vlanId);
1501 sbuilder.matchEthDst(MacAddress.NONE);
1502
1503 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1504 fob.withFlag(Flag.SPECIFIC)
1505 .withSelector(sbuilder.build())
1506 .nextStep(nextId)
1507 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1508 .fromApp(srManager.appId)
1509 .makePermanent();
1510 ObjectiveContext context = new DefaultObjectiveContext(
1511 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1512 (objective, error) ->
1513 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1514
1515 if (install) {
1516 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1517 } else {
1518 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1519 }
1520 }
1521
Charles Chan82ab1932016-01-30 23:22:37 -08001522 private int getPriorityFromPrefix(IpPrefix prefix) {
1523 return (prefix.isIp4()) ?
1524 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1525 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001526 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001527
1528 /**
1529 * Update Forwarding objective for each host and IP address connected to given port.
1530 * And create corresponding Simple Next objective if it does not exist.
1531 * Applied only when populating Forwarding objective
1532 * @param deviceId switch ID to set the rule
1533 * @param portNumber port number
1534 * @param prefix IP prefix of the route
1535 * @param hostMac MAC address of the next hop
1536 * @param vlanId Vlan ID of the port
1537 * @param popVlan true to pop vlan tag in TrafficTreatment
1538 * @param install true to populate the forwarding objective, false to revoke
1539 */
1540 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1541 VlanId vlanId, boolean popVlan, boolean install) {
1542 ForwardingObjective.Builder fob;
1543 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1544 MacAddress deviceMac;
1545 try {
1546 deviceMac = config.getDeviceMac(deviceId);
1547 } catch (DeviceConfigNotFoundException e) {
1548 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1549 return;
1550 }
1551
1552 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1553 tbuilder.deferred()
1554 .setEthDst(hostMac)
1555 .setEthSrc(deviceMac)
1556 .setOutput(portNumber);
1557
1558 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1559
1560 if (!popVlan) {
1561 tbuilder.setVlanId(vlanId);
1562 } else {
1563 mbuilder.matchVlanId(vlanId);
1564 }
1565
1566 // if the objective is to revoke an existing rule, and for some reason
1567 // the next-objective does not exist, then a new one should not be created
1568 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1569 tbuilder.build(), mbuilder.build(), install);
1570 if (portNextObjId == -1) {
1571 // Warning log will come from getPortNextObjective method
1572 return;
1573 }
1574
1575 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1576 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1577 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1578
1579 ObjectiveContext context = new DefaultObjectiveContext(
1580 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1581 (objective, error) ->
1582 log.warn("Failed to {} IP rule for route {}: {}",
1583 install ? "install" : "revoke", prefix, error));
1584 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1585
1586 if (!install) {
1587 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1588 if (grpHandler == null) {
1589 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1590 } else {
1591 // Remove L3UG for the given port and host
1592 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1593 }
1594 }
1595 }
Charles Chanf17f66b2018-02-26 21:33:25 -08001596
1597 /**
1598 * Checks if there is other enabled port within the given VLAN on the given device.
1599 *
1600 * @param deviceId device ID
1601 * @param vlanId VLAN ID
1602 * @return true if there is no more port enabled within the given VLAN on the given device
1603 */
1604 boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) {
1605 Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream()
1606 .filter(Port::isEnabled)
1607 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1608 .collect(Collectors.toSet());
1609
1610 return enabledPorts.stream().noneMatch(cp ->
1611 // Given vlanId is included in the vlan-tagged configuration
Charles Chan098ca202018-05-01 11:50:20 -07001612 srManager.interfaceService.getTaggedVlanId(cp).contains(vlanId) ||
Charles Chanf17f66b2018-02-26 21:33:25 -08001613 // Given vlanId is INTERNAL_VLAN and the interface is not configured
Charles Chan098ca202018-05-01 11:50:20 -07001614 (srManager.interfaceService.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null &&
Charles Chanf17f66b2018-02-26 21:33:25 -08001615 vlanId.equals(INTERNAL_VLAN)) ||
1616 // interface is configured and either vlan-untagged or vlan-native matches given vlanId
1617 (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId))
1618 );
1619 }
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001620
1621 /**
1622 * Returns a forwarding objective builder for egress forwarding rules.
1623 * <p>
1624 * The forwarding objective installs flow rules to egress pipeline to push
1625 * two vlan headers with given inner, outer vlan ids and outer tpid.
1626 *
1627 * @param portNumber port where the next hop attaches to
1628 * @param dummyVlanId vlan ID of the packet to match
1629 * @param innerVlan inner vlan ID of the next hop
1630 * @param outerVlan outer vlan ID of the next hop
1631 * @param outerTpid outer TPID of the next hop
1632 * @return forwarding objective builder
1633 */
1634 private ForwardingObjective.Builder egressFwdObjBuilder(PortNumber portNumber, VlanId dummyVlanId,
1635 VlanId innerVlan, VlanId outerVlan, EthType outerTpid) {
1636 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1637 sbuilder.matchVlanId(dummyVlanId);
1638 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1639 tbuilder.setOutput(portNumber).setVlanId(innerVlan);
1640
1641 if (outerTpid.equals(EthType.EtherType.QINQ.ethType())) {
1642 tbuilder.pushVlan(outerTpid);
1643 } else {
1644 tbuilder.pushVlan();
1645 }
1646
1647 tbuilder.setVlanId(outerVlan);
1648 return DefaultForwardingObjective.builder()
1649 .withSelector(sbuilder.build())
1650 .withTreatment(tbuilder.build())
1651 .fromApp(srManager.appId)
1652 .makePermanent()
1653 .withPriority(DEFAULT_PRIORITY)
1654 .withFlag(ForwardingObjective.Flag.EGRESS);
1655 }
1656
1657 /**
1658 * Populates IP rules for a route that has double-tagged next hop.
1659 *
1660 * @param deviceId device ID of the device that next hop attaches to
1661 * @param prefix IP prefix of the route
1662 * @param hostMac MAC address of the next hop
1663 * @param dummyVlan Dummy Vlan ID allocated for this route
1664 * @param innerVlan inner Vlan ID of the next hop
1665 * @param outerVlan outer Vlan ID of the next hop
1666 * @param outerTpid outer TPID of the next hop
1667 * @param outPort port where the next hop attaches to
1668 */
1669 void populateDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac, VlanId dummyVlan,
1670 VlanId innerVlan, VlanId outerVlan, EthType outerTpid, PortNumber outPort) {
1671 ForwardingObjective.Builder fwdBuilder;
1672 log.debug("Populate direct routing entry for double-tagged host route {} at {}:{}",
1673 prefix, deviceId, outPort);
1674
1675 ForwardingObjective.Builder egressFwdBuilder = egressFwdObjBuilder(
1676 outPort, dummyVlan, innerVlan, outerVlan, outerTpid);
1677 DefaultObjectiveContext egressFwdContext = new DefaultObjectiveContext(
1678 objective -> log.debug("Egress rule for IP {} is populated", prefix.address()),
1679 (objective, error) -> {
1680 log.warn("Failed to populate egress rule for IP {}: {}", prefix.address(), error);
1681 srManager.dummyVlanIdStore().remove(new DummyVlanIdStoreKey(
1682 new ConnectPoint(deviceId, outPort), prefix.address()
1683 ));
1684 });
1685 try {
1686 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, dummyVlan, outPort, false);
1687 } catch (DeviceConfigNotFoundException e) {
1688 log.error(e.getMessage() + " Aborting populateDoubleTaggedRoute");
1689 return;
1690 }
1691 if (fwdBuilder == null) {
1692 log.error("Aborting double-tagged host routing table entry due to error for dev:{} route:{}",
1693 deviceId, prefix);
1694 return;
1695 }
1696
1697 // Egress forwarding objective should be installed after the nextObjective for the output port is installed.
1698 // Installation of routingFwdObj will ensure the installation of the nextObjective.
1699 int nextId = fwdBuilder.add().nextId();
1700 DefaultObjectiveContext context = new DefaultObjectiveContext(objective -> {
1701 log.debug("Direct routing rule for double-tagged host route {} populated. nextId={}", prefix, nextId);
1702 srManager.flowObjectiveService.forward(deviceId, egressFwdBuilder.add(egressFwdContext));
1703 }, (objective, error) ->
1704 log.warn("Failed to populate direct routing rule for double-tagged host route {}: {}", prefix, error)
1705 );
1706 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
1707 rulePopulationCounter.incrementAndGet();
1708 }
1709
1710 /**
1711 * Revokes IP rules for a route that has double-tagged next hop.
1712 *
1713 * @param deviceId device ID of the device that next hop attaches to
1714 * @param prefix IP prefix of the route
1715 * @param hostMac MAC address of the next hop
1716 * @param hostVlan Vlan ID of the next hop
1717 * @param innerVlan inner Vlan ID of the next hop
1718 * @param outerVlan outer Vlan ID of the next hop
1719 * @param outerTpid outer TPID of the next hop
1720 * @param outPort port where the next hop attaches to
1721 */
1722 void revokeDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac,
1723 VlanId hostVlan, VlanId innerVlan, VlanId outerVlan,
1724 EthType outerTpid, PortNumber outPort) {
1725 revokeRoute(deviceId, prefix, hostMac, hostVlan, outPort);
1726
1727 DummyVlanIdStoreKey key = new DummyVlanIdStoreKey(
1728 new ConnectPoint(deviceId, outPort), prefix.address());
1729 VlanId dummyVlanId = srManager.dummyVlanIdStore().get(key);
1730 if (dummyVlanId == null) {
1731 log.warn("Failed to retrieve dummy VLAN ID for {}/{} and {}",
1732 deviceId, outPort, prefix.address());
1733 return;
1734 }
1735 ForwardingObjective.Builder fob = egressFwdObjBuilder(
1736 outPort, dummyVlanId, innerVlan, outerVlan, outerTpid);
1737 DefaultObjectiveContext context = new DefaultObjectiveContext(objective -> {
1738 log.debug("Egress rule for IP {} revoked", prefix.address());
1739 srManager.dummyVlanIdStore().remove(key);
1740 }, (objective, error) -> {
1741 log.warn("Failed to revoke egress rule for IP {}: {}", prefix.address(), error);
1742 });
1743 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1744 }
1745
Jonghwan Hyun671a7ab2018-04-30 09:27:21 -07001746 /**
1747 * Checks whether the specified port has IP configuration or not.
1748 *
1749 * @param cp ConnectPoint to check the existance of IP configuration
1750 * @return true if the port has IP configuration; false otherwise.
1751 */
1752 private boolean hasIPConfiguration(ConnectPoint cp) {
1753 Set<Interface> interfaces = srManager.interfaceService.getInterfacesByPort(cp);
1754 return interfaces.stream().anyMatch(intf -> intf.ipAddressesList().size() > 0);
1755 }
sangho80f11cb2015-04-01 13:05:26 -07001756}