blob: 9ff5bd00c2abd258a8e399a0adfc3d306591dbee [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;
Saurav Das4c35fc42015-11-20 15:27:53 -080019import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070020import org.onlab.packet.Ethernet;
21import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080022import org.onlab.packet.Ip6Address;
23import org.onlab.packet.IpAddress;
sangho80f11cb2015-04-01 13:05:26 -070024import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070026import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070027import org.onlab.packet.VlanId;
Charles Chanb7f75ac2016-01-11 18:28:54 -080028import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070029import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080030import org.onosproject.net.flowobjective.Objective;
Charles Chan1eaf4802016-04-18 13:44:03 -070031import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080032import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080033import org.onosproject.net.packet.PacketPriority;
Saurav Dasd1872b02016-12-02 15:43:47 -080034import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan319d1a22015-11-03 10:42:14 -080035import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
36import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Das62ae6792017-05-15 15:34:25 -070037import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -070038import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sangho80f11cb2015-04-01 13:05:26 -070039import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070040import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070041import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070042import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070044import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070046import org.onosproject.net.flow.criteria.Criteria;
47import org.onosproject.net.flowobjective.DefaultFilteringObjective;
48import org.onosproject.net.flowobjective.DefaultForwardingObjective;
49import org.onosproject.net.flowobjective.FilteringObjective;
50import org.onosproject.net.flowobjective.ForwardingObjective;
51import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070052import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
56import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070057import java.util.Collection;
58import java.util.Collections;
Saurav Das261c3002017-06-13 15:35:54 -070059import java.util.HashMap;
Saurav Dasc28b3432015-10-30 17:45:38 -070060import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070061import java.util.List;
Saurav Das261c3002017-06-13 15:35:54 -070062import java.util.Map;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080063import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070064import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070065import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070066
67import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080068import static org.onlab.packet.Ethernet.TYPE_ARP;
69import static org.onlab.packet.Ethernet.TYPE_IPV6;
70import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
71import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan10b0fb72017-02-02 16:20:42 -080072import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sangho80f11cb2015-04-01 13:05:26 -070073
Charles Chanb7f75ac2016-01-11 18:28:54 -080074/**
75 * Populator of segment routing flow rules.
76 */
sangho80f11cb2015-04-01 13:05:26 -070077public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070078 private static final Logger log = LoggerFactory
79 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070080
sanghofb7c7292015-04-13 15:15:58 -070081 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070082 private SegmentRoutingManager srManager;
83 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070084
sangho80f11cb2015-04-01 13:05:26 -070085 /**
86 * Creates a RoutingRulePopulator object.
87 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070088 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070089 */
90 public RoutingRulePopulator(SegmentRoutingManager srManager) {
91 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070092 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -070093 this.rulePopulationCounter = new AtomicLong(0);
94 }
95
96 /**
97 * Resets the population counter.
98 */
99 public void resetCounter() {
100 rulePopulationCounter.set(0);
101 }
102
103 /**
104 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700105 *
106 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700107 */
108 public long getCounter() {
109 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700110 }
111
112 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700113 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700114 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700115 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700116 * @param deviceId device ID of the device that next hop attaches to
117 * @param prefix IP prefix of the route
118 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800119 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700120 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700121 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700122 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800123 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das261c3002017-06-13 15:35:54 -0700124 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700125 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800126 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800127 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700128 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
129 hostVlanId, outPort, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800130 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700131 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan319d1a22015-11-03 10:42:14 -0800132 return;
133 }
Saurav Das07c74602016-04-27 18:35:50 -0700134 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700135 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700136 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700137 return;
138 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700139 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das261c3002017-06-13 15:35:54 -0700140 (objective) -> log.debug("Direct routing rule for route {} populated",
141 prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700142 (objective, error) ->
Saurav Das261c3002017-06-13 15:35:54 -0700143 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das368cf212017-03-15 15:15:14 -0700144 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700145 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800146 rulePopulationCounter.incrementAndGet();
147 }
148
Charles Chanb7f75ac2016-01-11 18:28:54 -0800149 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700150 * Removes IP rules for a route when the next hop is gone.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800151 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700152 * @param deviceId device ID of the device that next hop attaches to
153 * @param prefix IP prefix of the route
154 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800155 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700156 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800157 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700158 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800159 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700160 log.debug("Revoke IP table entry for route {} at {}:{}",
161 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800162 ForwardingObjective.Builder fwdBuilder;
163 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700164 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
165 hostVlanId, outPort, true);
Charles Chanf4586112015-11-09 16:37:23 -0800166 } catch (DeviceConfigNotFoundException e) {
167 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
168 return;
169 }
Charles Chanea702b12016-11-30 11:55:05 -0800170 if (fwdBuilder == null) {
171 log.warn("Aborting host routing table entries due "
172 + "to error for dev:{} route:{}", deviceId, prefix);
173 return;
174 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700175 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700176 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700177 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700178 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700179 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800180 }
181
Charles Chanddac7fd2016-10-27 14:19:48 -0700182 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800183 * Returns a forwarding objective builder for routing rules.
184 * <p>
185 * The forwarding objective routes packets destined to a given prefix to
186 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700187 *
188 * @param deviceId device ID
189 * @param prefix prefix that need to be routed
190 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800191 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700192 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700193 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chanddac7fd2016-10-27 14:19:48 -0700194 * @return forwarding objective builder
195 * @throws DeviceConfigNotFoundException if given device is not configured
196 */
Charles Chan18fa4252017-02-08 16:10:40 -0800197 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700198 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700199 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
200 boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800201 throws DeviceConfigNotFoundException {
202 MacAddress deviceMac;
203 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800204
Charles Chan90772a72017-02-08 15:52:08 -0800205 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
206 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
207 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
208 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700209
Charles Chan90772a72017-02-08 15:52:08 -0800210 // Create route selector
211 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
212
213 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700214 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700215 tbuilder.deferred()
216 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800217 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700218 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800219
Charles Chan90772a72017-02-08 15:52:08 -0800220 // Create route meta
221 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800222
Charles Chan90772a72017-02-08 15:52:08 -0800223 // Adjust the meta according to VLAN configuration
224 if (taggedVlans.contains(hostVlanId)) {
225 tbuilder.setVlanId(hostVlanId);
226 } else if (hostVlanId.equals(VlanId.NONE)) {
227 if (untaggedVlan != null) {
228 mbuilder.matchVlanId(untaggedVlan);
229 } else if (nativeVlan != null) {
230 mbuilder.matchVlanId(nativeVlan);
231 } else {
232 // TODO: This check is turned off for now since vRouter still assumes that
233 // hosts are internally tagged with INTERNAL_VLAN.
234 // We should turn this back on when we move forward to the bridging CPR approach.
235 //
236 //log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
237 // hostMac, hostVlanId, connectPoint);
238 //return null;
239 mbuilder.matchVlanId(INTERNAL_VLAN);
240 }
241 } else {
Saurav Das2cb38292017-03-29 19:09:17 -0700242 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
243 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das07c74602016-04-27 18:35:50 -0700244 return null;
245 }
Saurav Das2cb38292017-03-29 19:09:17 -0700246 // if the objective is to revoke an existing rule, and for some reason
247 // the next-objective does not exist, then a new one should not be created
Charles Chan90772a72017-02-08 15:52:08 -0800248 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das2cb38292017-03-29 19:09:17 -0700249 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan90772a72017-02-08 15:52:08 -0800250 if (portNextObjId == -1) {
251 // Warning log will come from getPortNextObjective method
252 return null;
253 }
254
Charles Chanf4586112015-11-09 16:37:23 -0800255 return DefaultForwardingObjective.builder()
Charles Chan90772a72017-02-08 15:52:08 -0800256 .withSelector(sbuilder.build())
Saurav Das2d94d312015-11-24 23:21:05 -0800257 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800258 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700259 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800260 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700261 }
262
263 /**
Saurav Das261c3002017-06-13 15:35:54 -0700264 * Populates IP flow rules for all the given prefixes reachable from the
265 * destination switch(es).
sangho80f11cb2015-04-01 13:05:26 -0700266 *
Saurav Das261c3002017-06-13 15:35:54 -0700267 * @param targetSw switch where rules are to be programmed
268 * @param subnets subnets/prefixes being added
269 * @param destSw1 destination switch where the prefixes are reachable
270 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
271 * Should be null if there is no paired destination switch (by config)
272 * or if the given prefixes are reachable only via destSw1
273 * @param nextHops a map containing a set of next-hops for each destination switch.
274 * If destSw2 is not null, then this map must contain an
275 * entry for destSw2 with its next-hops from the targetSw
276 * (although the next-hop set may be empty in certain scenarios).
277 * If destSw2 is null, there should not be an entry in this
278 * map for destSw2.
sangho80f11cb2015-04-01 13:05:26 -0700279 * @return true if all rules are set successfully, false otherwise
280 */
Saurav Das261c3002017-06-13 15:35:54 -0700281 public boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
282 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700283 for (IpPrefix subnet : subnets) {
Saurav Das261c3002017-06-13 15:35:54 -0700284 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sangho80f11cb2015-04-01 13:05:26 -0700285 return false;
286 }
287 }
Charles Chanc22cef32016-04-29 14:38:22 -0700288 return true;
289 }
sangho80f11cb2015-04-01 13:05:26 -0700290
Charles Chanc22cef32016-04-29 14:38:22 -0700291 /**
Saurav Das261c3002017-06-13 15:35:54 -0700292 * Revokes IP flow rules for the subnets in each edge switch.
Charles Chanc22cef32016-04-29 14:38:22 -0700293 *
294 * @param subnets subnet being removed
295 * @return true if all rules are removed successfully, false otherwise
296 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800297 public boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700298 for (IpPrefix subnet : subnets) {
299 if (!revokeIpRuleForRouter(subnet)) {
300 return false;
301 }
302 }
sangho80f11cb2015-04-01 13:05:26 -0700303 return true;
304 }
305
306 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700307 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das261c3002017-06-13 15:35:54 -0700308 * is reachable via destination device(s).
sangho80f11cb2015-04-01 13:05:26 -0700309 *
Saurav Das261c3002017-06-13 15:35:54 -0700310 * @param targetSw target device ID to set the rules
311 * @param ipPrefix the IP prefix
312 * @param destSw1 destination switch where the prefixes are reachable
313 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
314 * Should be null if there is no paired destination switch (by config)
315 * or if the given prefixes are reachable only via destSw1
316 * @param nextHops map of destination switches and their next-hops.
317 * Should only contain destination switches that are
318 * actually meant to be routed to. If destSw2 is null, there
319 * should not be an entry for destSw2 in this map.
sangho80f11cb2015-04-01 13:05:26 -0700320 * @return true if all rules are set successfully, false otherwise
321 */
Saurav Das261c3002017-06-13 15:35:54 -0700322 public boolean populateIpRuleForRouter(DeviceId targetSw,
323 IpPrefix ipPrefix, DeviceId destSw1,
324 DeviceId destSw2,
325 Map<DeviceId, Set<DeviceId>> nextHops) {
326 int segmentId1, segmentId2 = -1;
Charles Chan319d1a22015-11-03 10:42:14 -0800327 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800328 if (ipPrefix.isIp4()) {
Saurav Das261c3002017-06-13 15:35:54 -0700329 segmentId1 = config.getIPv4SegmentId(destSw1);
330 if (destSw2 != null) {
331 segmentId2 = config.getIPv4SegmentId(destSw2);
332 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800333 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700334 segmentId1 = config.getIPv6SegmentId(destSw1);
335 if (destSw2 != null) {
336 segmentId2 = config.getIPv6SegmentId(destSw2);
337 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800338 }
Charles Chan319d1a22015-11-03 10:42:14 -0800339 } catch (DeviceConfigNotFoundException e) {
340 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
341 return false;
342 }
sangho80f11cb2015-04-01 13:05:26 -0700343
Pier Ventreadb4ae62016-11-23 09:57:42 -0800344 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800345 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700346
Charles Chanf4586112015-11-09 16:37:23 -0800347 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das261c3002017-06-13 15:35:54 -0700348 DestinationSet ds;
Charles Chanf4586112015-11-09 16:37:23 -0800349 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700350
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700351 // If the next hop is the same as the final destination, then MPLS label
352 // is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700353 /*if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800354 tbuilder.immediate().decNwTtl();
Saurav Das261c3002017-06-13 15:35:54 -0700355 ds = new DestinationSet(false, destSw);
Charles Chanf4586112015-11-09 16:37:23 -0800356 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700357 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700358 ds = new DestinationSet(false, segmentId, destSw);
359 treatment = null;
360 }*/
361 if (destSw2 == null) {
362 // single dst - create destination set based on next-hop
363 Set<DeviceId> nhd1 = nextHops.get(destSw1);
364 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
365 tbuilder.immediate().decNwTtl();
366 ds = new DestinationSet(false, destSw1);
367 treatment = tbuilder.build();
368 } else {
369 ds = new DestinationSet(false, segmentId1, destSw1);
370 treatment = null;
371 }
372 } else {
373 // dst pair - IP rules for dst-pairs are always from other edge nodes
374 // the destination set needs to have both destinations, even if there
375 // are no next hops to one of them
376 ds = new DestinationSet(false, segmentId1, destSw1, segmentId2, destSw2);
Charles Chanf4586112015-11-09 16:37:23 -0800377 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700378 }
379
Saurav Das4c35fc42015-11-20 15:27:53 -0800380 // setup metadata to pass to nextObjective - indicate the vlan on egress
381 // if needed by the switch pipeline. Since neighbor sets are always to
382 // other neighboring routers, there is no subnet assigned on those ports.
383 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800384 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das261c3002017-06-13 15:35:54 -0700385 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700386 if (grpHandler == null) {
387 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das261c3002017-06-13 15:35:54 -0700388 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700389 return false;
390 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800391
Saurav Das261c3002017-06-13 15:35:54 -0700392 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
393 metabuilder.build(), true);
Saurav Das4c35fc42015-11-20 15:27:53 -0800394 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700395 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho2165d222015-05-01 09:38:25 -0700396 return false;
397 }
398
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700399 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
400 .builder()
401 .fromApp(srManager.appId)
402 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800403 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700404 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800405 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700406 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800407 if (treatment != null) {
408 fwdBuilder.withTreatment(treatment);
409 }
Saurav Das62ae6792017-05-15 15:34:25 -0700410 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das261c3002017-06-13 15:35:54 -0700411 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700412 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700413 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700414 ipPrefix, targetSw),
Charles Chan1eaf4802016-04-18 13:44:03 -0700415 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700416 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700417 ipPrefix, error, targetSw));
418 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700419 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700420
421 return true;
422 }
423
sangho80f11cb2015-04-01 13:05:26 -0700424 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700425 * Revokes IP flow rules for the router IP address.
426 *
427 * @param ipPrefix the IP address of the destination router
428 * @return true if all rules are removed successfully, false otherwise
429 */
430 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800431 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700432 TrafficSelector selector = sbuilder.build();
433 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
434
435 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
436 .builder()
437 .fromApp(srManager.appId)
438 .makePermanent()
439 .withSelector(selector)
440 .withTreatment(dummyTreatment)
441 .withPriority(getPriorityFromPrefix(ipPrefix))
442 .withFlag(ForwardingObjective.Flag.SPECIFIC);
443
444 ObjectiveContext context = new DefaultObjectiveContext(
445 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
446 (objective, error) ->
447 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
448
449 srManager.deviceService.getAvailableDevices().forEach(device -> {
450 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
451 });
452
453 return true;
454 }
455
456 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700457 * Deals with !MPLS Bos use case.
458 *
459 * @param targetSwId the target sw
460 * @param destSwId the destination sw
461 * @param nextHops the set of next hops
462 * @param segmentId the segmentId to match
463 * @param routerIp the router ip
464 * @return a collection of fwdobjective
465 */
Saurav Das261c3002017-06-13 15:35:54 -0700466 private Collection<ForwardingObjective> handleMpls(
467 DeviceId targetSwId,
468 DeviceId destSwId,
469 Set<DeviceId> nextHops,
470 int segmentId,
471 IpAddress routerIp,
472 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700473
474 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
475 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800476 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700477 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
478 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
479 sbuilder.matchMplsBos(isMplsBos);
480 TrafficSelector selector = sbuilder.build();
481
482 // setup metadata to pass to nextObjective - indicate the vlan on egress
483 // if needed by the switch pipeline. Since mpls next-hops are always to
484 // other neighboring routers, there is no subnet assigned on those ports.
485 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800486 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700487
488 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
489 // If the next hop is the destination router for the segment, do pop
490 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700491 + "label {} in switch {} with pop to next-hops {}",
492 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700493 // Not-bos pop case (php for the current label). If MPLS-ECMP
494 // has been configured, the application we will request the
495 // installation for an MPLS-ECMP group.
Saurav Das2cb38292017-03-29 19:09:17 -0700496 ForwardingObjective.Builder fwdObjNoBosBuilder =
497 getMplsForwardingObjective(targetSwId,
498 nextHops,
499 true,
500 isMplsBos,
501 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700502 routerIp,
503 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700504 // Error case, we cannot handle, exit.
505 if (fwdObjNoBosBuilder == null) {
506 return Collections.emptyList();
507 }
508 fwdObjBuilders.add(fwdObjNoBosBuilder);
509
510 } else {
511 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700512 log.debug("Installing MPLS forwarding objective for "
513 + "label {} in switch {} without pop to next-hops {}",
514 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700515 // Not-bos pop case. If MPLS-ECMP has been configured, the
516 // application we will request the installation for an MPLS-ECMP
517 // group.
Saurav Das2cb38292017-03-29 19:09:17 -0700518 ForwardingObjective.Builder fwdObjNoBosBuilder =
519 getMplsForwardingObjective(targetSwId,
520 nextHops,
521 false,
522 isMplsBos,
523 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700524 routerIp,
525 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700526 // Error case, we cannot handle, exit.
527 if (fwdObjNoBosBuilder == null) {
528 return Collections.emptyList();
529 }
530 fwdObjBuilders.add(fwdObjNoBosBuilder);
531
532 }
533
534 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
535 // We add the final property to the fwdObjs.
536 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
537
538 ((Builder) ((Builder) fwdObjBuilder
539 .fromApp(srManager.appId)
540 .makePermanent())
541 .withSelector(selector)
542 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
543 .withFlag(ForwardingObjective.Flag.SPECIFIC);
544
545 ObjectiveContext context = new DefaultObjectiveContext(
546 (objective) ->
547 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
548 objective.id(), segmentId, targetSwId),
549 (objective, error) ->
550 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
551 objective.id(), segmentId, error, targetSwId));
552
553 ForwardingObjective fob = fwdObjBuilder.add(context);
554 fwdObjs.add(fob);
555
556 }
557
558 return fwdObjs;
559 }
560
561 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700562 * Populates MPLS flow rules in the target device to point towards the
563 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700564 *
Saurav Dase0237a32016-05-27 13:54:07 -0700565 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700566 * @param destSwId destination switch device ID
567 * @param nextHops next hops switch ID list
Pier Ventre229fd0b2016-10-31 16:49:19 -0700568 * @param routerIp the router ip
sangho80f11cb2015-04-01 13:05:26 -0700569 * @return true if all rules are set successfully, false otherwise
570 */
Saurav Dase0237a32016-05-27 13:54:07 -0700571 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Saurav Das261c3002017-06-13 15:35:54 -0700572 Set<DeviceId> nextHops,
573 IpAddress routerIp) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700574
Charles Chan319d1a22015-11-03 10:42:14 -0800575 int segmentId;
576 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800577 if (routerIp.isIp4()) {
578 segmentId = config.getIPv4SegmentId(destSwId);
579 } else {
580 segmentId = config.getIPv6SegmentId(destSwId);
581 }
Charles Chan319d1a22015-11-03 10:42:14 -0800582 } catch (DeviceConfigNotFoundException e) {
583 log.warn(e.getMessage() + " Aborting populateMplsRule.");
584 return false;
585 }
sangho80f11cb2015-04-01 13:05:26 -0700586
Pier Ventre229fd0b2016-10-31 16:49:19 -0700587 List<ForwardingObjective> fwdObjs = new ArrayList<>();
588 Collection<ForwardingObjective> fwdObjsMpls = Collections.emptyList();
589 // Generates the transit rules used by the standard "routing".
590 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
591 if (fwdObjsMpls.isEmpty()) {
592 return false;
sangho80f11cb2015-04-01 13:05:26 -0700593 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700594 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800595 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre229fd0b2016-10-31 16:49:19 -0700596 // the only case !BoS supported.
Saurav Dasf9332192017-02-18 14:05:44 -0800597 /*fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700598 if (fwdObjsMpls.isEmpty()) {
599 return false;
600 }
Saurav Dasf9332192017-02-18 14:05:44 -0800601 fwdObjs.addAll(fwdObjsMpls);*/
Pier Ventre229fd0b2016-10-31 16:49:19 -0700602
603 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700604 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700605 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
606 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700607 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700608 }
609
610 return true;
611 }
612
Saurav Das4c35fc42015-11-20 15:27:53 -0800613 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700614 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800615 Set<DeviceId> nextHops,
616 boolean phpRequired,
617 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800618 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700619 IpAddress routerIp,
620 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800621
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700622 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
623 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700624
625 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
626
627 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800628 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700629 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700630 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700631 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800632 if (routerIp.isIp4()) {
633 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
634 } else {
635 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
636 }
637 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700638 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800639 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
640 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700641 }
642 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800643 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700644 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700645 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700646 }
647
Saurav Das4c35fc42015-11-20 15:27:53 -0800648 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700649 // if MPLS-ECMP == True we will build a standard NeighborSet.
650 // Otherwise a RandomNeighborSet.
Saurav Das261c3002017-06-13 15:35:54 -0700651 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700652 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700653 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700654 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700655 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700656 }
Saurav Das261c3002017-06-13 15:35:54 -0700657
Saurav Das9455d702017-03-24 19:03:58 -0700658 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700659 targetSw, ns);
660 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
661 if (gh == null) {
662 log.warn("getNextObjectiveId query - groupHandler for device {} "
663 + "not found", targetSw);
664 return null;
665 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700666 // If BoS == True, all forwarding is via L3 ECMP group.
667 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
668 // MPLS-Interface group. This depends on the configuration of the option
669 // MPLS-ECMP.
670 // The metadata informs the driver that the next-Objective will be used
671 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das261c3002017-06-13 15:35:54 -0700672 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
673 dstNextHops.put(destSw, nextHops);
674 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das4c35fc42015-11-20 15:27:53 -0800675 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700676 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das4c35fc42015-11-20 15:27:53 -0800677 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700678 } else {
679 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700680 nextId, targetSw, ns);
sangho80f11cb2015-04-01 13:05:26 -0700681 }
682
Saurav Das4c35fc42015-11-20 15:27:53 -0800683 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700684 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700685 }
686
687 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700688 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700689 * dstMac corresponding to the router's MAC address. For those pipelines
690 * that need to internally assign vlans to untagged packets, this method
691 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700692 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800693 * Note that the vlan assignment and filter programming should only be done by
694 * the master for a switch. This method is typically called at deviceAdd and
695 * programs filters only for the enabled ports of the device. For port-updates,
696 * that enable/disable ports after device add, singlePortFilter methods should
697 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700698 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700699 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800700 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700701 */
Charles Chan18fa4252017-02-08 16:10:40 -0800702 public PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700703 log.debug("Installing per-port filtering objective for untagged "
704 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800705
Saurav Das07c74602016-04-27 18:35:50 -0700706 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800707 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700708 log.warn("Device {} ports not available. Unable to add MacVlan filters",
709 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800710 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700711 }
Saurav Dasf9332192017-02-18 14:05:44 -0800712 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700713 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700714 if (!port.isEnabled()) {
715 disabledPorts++;
716 continue;
717 }
Charles Chan43be46b2017-02-26 22:59:35 -0800718 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800719 filteredPorts++;
720 } else {
721 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700722 }
Saurav Das7c305372015-10-28 12:39:42 -0700723 }
Charles Chan077314e2017-06-22 14:27:17 -0700724 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800725 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800726 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800727 errorPorts, filteredPorts);
728 }
729
730 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800731 * Creates or removes filtering objectives for a single port. Should only be
732 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800733 *
734 * @param deviceId device identifier
735 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800736 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800737 * @return true if no errors occurred during the build of the filtering objective
738 */
Charles Chan43be46b2017-02-26 22:59:35 -0800739 public boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800740 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
741 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
742 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
743 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
744
745 if (taggedVlans.size() != 0) {
746 // Filter for tagged vlans
747 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800748 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800749 return false;
750 }
751 if (nativeVlan != null) {
752 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800753 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800754 return false;
755 }
756 }
757 } else if (untaggedVlan != null) {
758 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800759 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800760 return false;
761 }
762 } else {
763 // Unconfigure port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800764 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800765 return false;
766 }
767 }
768 return true;
769 }
770
Charles Chan43be46b2017-02-26 22:59:35 -0800771 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
772 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800773 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800774 if (fob == null) {
775 // error encountered during build
776 return false;
777 }
Saurav Das62ae6792017-05-15 15:34:25 -0700778 log.debug("{} filtering objectives for dev/port:{}/{}",
779 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800780 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800781 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -0700782 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -0800783 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -0700784 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -0800785 if (install) {
786 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
787 } else {
788 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -0800789 }
Charles Chan90772a72017-02-08 15:52:08 -0800790 return true;
Saurav Dasf9332192017-02-18 14:05:44 -0800791 }
792
Charles Chan90772a72017-02-08 15:52:08 -0800793 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
794 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -0800795 MacAddress deviceMac;
796 try {
797 deviceMac = config.getDeviceMac(deviceId);
798 } catch (DeviceConfigNotFoundException e) {
799 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
800 return null;
801 }
Saurav Dasf9332192017-02-18 14:05:44 -0800802 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
803 fob.withKey(Criteria.matchInPort(portnum))
804 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -0800805 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -0800806
807 if (pushVlan) {
808 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
809 TrafficTreatment tt = DefaultTrafficTreatment.builder()
810 .pushVlan().setVlanId(vlanId).build();
811 fob.withMeta(tt);
812 } else {
813 fob.addCondition(Criteria.matchVlanId(vlanId));
814 }
815
Saurav Dasf9332192017-02-18 14:05:44 -0800816 fob.permit().fromApp(srManager.appId);
817 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700818 }
819
820 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700821 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700822 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700823 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700824 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700825 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700826 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700827 */
Charles Chan18fa4252017-02-08 16:10:40 -0800828 public void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -0700829 Ip4Address routerIpv4, pairRouterIpv4 = null;
830 Ip6Address routerIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -0800831 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800832 routerIpv4 = config.getRouterIpv4(deviceId);
833 routerIpv6 = config.getRouterIpv6(deviceId);
Saurav Das261c3002017-06-13 15:35:54 -0700834 if (config.isPairedEdge(deviceId)) {
835 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
836 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
837 }
Charles Chan319d1a22015-11-03 10:42:14 -0800838 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -0800839 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -0800840 return;
841 }
842
Saurav Dasc28b3432015-10-30 17:45:38 -0700843 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
844 log.debug("Not installing port-IP punts - not the master for dev:{} ",
845 deviceId);
846 return;
847 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800848 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
849 allIps.add(routerIpv4);
850 if (routerIpv6 != null) {
851 allIps.add(routerIpv6);
852 }
Saurav Das261c3002017-06-13 15:35:54 -0700853 if (pairRouterIpv4 != null) {
854 allIps.add(pairRouterIpv4);
855 }
856 if (pairRouterIpv6 != null) {
857 allIps.add(pairRouterIpv6);
858 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800859 for (IpAddress ipaddr : allIps) {
860 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr);
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800861 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
862
863 srManager.packetService.requestPackets(sbuilder.build(),
864 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das9f1c42e2015-10-23 10:51:11 -0700865 }
sangho80f11cb2015-04-01 13:05:26 -0700866 }
867
Charles Chanf4586112015-11-09 16:37:23 -0800868 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800869 * Method to build IPv4 or IPv6 selector.
870 *
871 * @param addressToMatch the address to match
872 */
873 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
874 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
875 }
876
877 /**
878 * Method to build IPv4 or IPv6 selector.
879 *
880 * @param prefixToMatch the prefix to match
881 */
882 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
883 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700884 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800885 if (prefixToMatch.isIp4()) {
886 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
887 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
888 return selectorBuilder;
889 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700890 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800891 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
892 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
893 return selectorBuilder;
894 }
895
896 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800897 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
898 * Furthermore, these are applied only by the master instance. Deferred actions
899 * are not cleared such that packets can be flooded in the cross connect use case
900 *
901 * @param deviceId the switch dpid for the router
902 */
903 public void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700904 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800905 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
906 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
907 deviceId);
908 return;
909 }
910
911 // We punt all ARP packets towards the controller.
912 ForwardingObjective puntFwd = puntArpFwdObjective()
913 .add(new ObjectiveContext() {
914 @Override
915 public void onError(Objective objective, ObjectiveError error) {
916 log.warn("Failed to install packet request for ARP to {}: {}",
917 deviceId, error);
918 }
919 });
920 srManager.flowObjectiveService.forward(deviceId, puntFwd);
921
Pier Luigib9632ba2017-01-12 18:14:58 -0800922 // We punt all NDP packets towards the controller.
Pier Luigib9632ba2017-01-12 18:14:58 -0800923 puntFwd = puntNdpFwdObjective()
924 .add(new ObjectiveContext() {
925 @Override
926 public void onError(Objective objective, ObjectiveError error) {
927 log.warn("Failed to install packet request for NDP to {}: {}",
928 deviceId, error);
929 }
930 });
931 srManager.flowObjectiveService.forward(deviceId, puntFwd);
Pier Luigib9632ba2017-01-12 18:14:58 -0800932 }
933
934 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) {
935
936 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
937 tBuilder.punt();
938
939 return DefaultForwardingObjective.builder()
940 .withPriority(PacketPriority.CONTROL.priorityValue())
941 .withSelector(selector)
942 .fromApp(srManager.appId)
943 .withFlag(ForwardingObjective.Flag.VERSATILE)
944 .withTreatment(tBuilder.build())
945 .makePermanent();
946 }
947
948 private ForwardingObjective.Builder puntArpFwdObjective() {
949
950 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
951 sBuilder.matchEthType(TYPE_ARP);
952
953 return fwdObjBuilder(sBuilder.build());
954 }
955
956 private ForwardingObjective.Builder puntNdpFwdObjective() {
957
958 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
959 sBuilder.matchEthType(TYPE_IPV6)
960 .matchIPProtocol(PROTOCOL_ICMP6)
961 .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
962 .build();
963
964 return fwdObjBuilder(sBuilder.build());
965 }
966
967 /**
Charles Chanf4586112015-11-09 16:37:23 -0800968 * Populates a forwarding objective to send packets that miss other high
969 * priority Bridging Table entries to a group that contains all ports of
970 * its subnet.
971 *
Charles Chanf4586112015-11-09 16:37:23 -0800972 * @param deviceId switch ID to set the rules
973 */
974 public void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -0800975 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan10b0fb72017-02-02 16:20:42 -0800976 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
977
978 if (nextId < 0) {
979 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
980 + "to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
Saurav Das2d94d312015-11-24 23:21:05 -0800981 return;
982 }
983
Pier Ventre229fd0b2016-10-31 16:49:19 -0700984 // Driver should treat objective with MacAddress.NONE as the
985 // subnet broadcast rule
Charles Chanf4586112015-11-09 16:37:23 -0800986 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
987 sbuilder.matchVlanId(vlanId);
988 sbuilder.matchEthDst(MacAddress.NONE);
989
990 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
991 fob.withFlag(Flag.SPECIFIC)
992 .withSelector(sbuilder.build())
993 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800994 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chanf4586112015-11-09 16:37:23 -0800995 .fromApp(srManager.appId)
996 .makePermanent();
Charles Chan1eaf4802016-04-18 13:44:03 -0700997 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan10b0fb72017-02-02 16:20:42 -0800998 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700999 (objective, error) ->
Charles Chan10b0fb72017-02-02 16:20:42 -08001000 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
Charles Chan1eaf4802016-04-18 13:44:03 -07001001 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chanf4586112015-11-09 16:37:23 -08001002 });
1003 }
1004
Charles Chan82ab1932016-01-30 23:22:37 -08001005 private int getPriorityFromPrefix(IpPrefix prefix) {
1006 return (prefix.isIp4()) ?
1007 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1008 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001009 }
sangho80f11cb2015-04-01 13:05:26 -07001010}