blob: 8d757aa68f55c04a13859d0e2daab6fe27bed7a3 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070037import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sangho80f11cb2015-04-01 13:05:26 -070038import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070039import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070040import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070041import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.net.flow.TrafficSelector;
44import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070045import org.onosproject.net.flow.criteria.Criteria;
46import org.onosproject.net.flowobjective.DefaultFilteringObjective;
47import org.onosproject.net.flowobjective.DefaultForwardingObjective;
48import org.onosproject.net.flowobjective.FilteringObjective;
49import org.onosproject.net.flowobjective.ForwardingObjective;
50import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070051import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070052import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
55import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070056import java.util.Collection;
57import java.util.Collections;
Saurav Dasc28b3432015-10-30 17:45:38 -070058import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070059import java.util.List;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080060import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070061import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070062import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070063
64import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080065import static org.onlab.packet.Ethernet.TYPE_ARP;
66import static org.onlab.packet.Ethernet.TYPE_IPV6;
67import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
68import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan10b0fb72017-02-02 16:20:42 -080069import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sangho80f11cb2015-04-01 13:05:26 -070070
Charles Chanb7f75ac2016-01-11 18:28:54 -080071/**
72 * Populator of segment routing flow rules.
73 */
sangho80f11cb2015-04-01 13:05:26 -070074public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070075 private static final Logger log = LoggerFactory
76 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070077
sanghofb7c7292015-04-13 15:15:58 -070078 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070079 private SegmentRoutingManager srManager;
80 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070081
sangho80f11cb2015-04-01 13:05:26 -070082 /**
83 * Creates a RoutingRulePopulator object.
84 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070085 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070086 */
87 public RoutingRulePopulator(SegmentRoutingManager srManager) {
88 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070089 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -070090 this.rulePopulationCounter = new AtomicLong(0);
91 }
92
93 /**
94 * Resets the population counter.
95 */
96 public void resetCounter() {
97 rulePopulationCounter.set(0);
98 }
99
100 /**
101 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700102 *
103 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700104 */
105 public long getCounter() {
106 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700107 }
108
109 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700110 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700111 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700112 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700113 * @param deviceId device ID of the device that next hop attaches to
114 * @param prefix IP prefix of the route
115 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800116 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700117 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700118 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700119 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800120 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das368cf212017-03-15 15:15:14 -0700121 log.debug("Populate routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700122 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800123 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800124 try {
Charles Chan90772a72017-02-08 15:52:08 -0800125 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, hostVlanId, outPort);
Charles Chan319d1a22015-11-03 10:42:14 -0800126 } catch (DeviceConfigNotFoundException e) {
127 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
128 return;
129 }
Saurav Das07c74602016-04-27 18:35:50 -0700130 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700131 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700132 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700133 return;
134 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700135 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das368cf212017-03-15 15:15:14 -0700136 (objective) -> log.debug("Routing rule for route {} populated", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700137 (objective, error) ->
Saurav Das368cf212017-03-15 15:15:14 -0700138 log.warn("Failed to populate routing rule for route {}: {}",
139 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700140 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800141 rulePopulationCounter.incrementAndGet();
142 }
143
Charles Chanb7f75ac2016-01-11 18:28:54 -0800144 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700145 * Removes IP rules for a route when the next hop is gone.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800146 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700147 * @param deviceId device ID of the device that next hop attaches to
148 * @param prefix IP prefix of the route
149 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800150 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700151 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800152 */
Charles Chanddac7fd2016-10-27 14:19:48 -0700153 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800154 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700155 log.debug("Revoke IP table entry for route {} at {}:{}",
156 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800157 ForwardingObjective.Builder fwdBuilder;
158 try {
Charles Chan90772a72017-02-08 15:52:08 -0800159 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, hostVlanId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800160 } catch (DeviceConfigNotFoundException e) {
161 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
162 return;
163 }
Charles Chanea702b12016-11-30 11:55:05 -0800164 if (fwdBuilder == null) {
165 log.warn("Aborting host routing table entries due "
166 + "to error for dev:{} route:{}", deviceId, prefix);
167 return;
168 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700169 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700170 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700171 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700172 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700173 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800174 }
175
Charles Chanddac7fd2016-10-27 14:19:48 -0700176 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800177 * Returns a forwarding objective builder for routing rules.
178 * <p>
179 * The forwarding objective routes packets destined to a given prefix to
180 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700181 *
182 * @param deviceId device ID
183 * @param prefix prefix that need to be routed
184 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800185 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700186 * @param outPort port where the nexthop attaches to
187 * @return forwarding objective builder
188 * @throws DeviceConfigNotFoundException if given device is not configured
189 */
Charles Chan18fa4252017-02-08 16:10:40 -0800190 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700191 DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800192 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort)
Charles Chanf4586112015-11-09 16:37:23 -0800193 throws DeviceConfigNotFoundException {
194 MacAddress deviceMac;
195 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800196
Charles Chan90772a72017-02-08 15:52:08 -0800197 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
198 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
199 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
200 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700201
Charles Chan90772a72017-02-08 15:52:08 -0800202 // Create route selector
203 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
204
205 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700206 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700207 tbuilder.deferred()
208 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800209 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700210 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800211
Charles Chan90772a72017-02-08 15:52:08 -0800212 // Create route meta
213 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800214
Charles Chan90772a72017-02-08 15:52:08 -0800215 // Adjust the meta according to VLAN configuration
216 if (taggedVlans.contains(hostVlanId)) {
217 tbuilder.setVlanId(hostVlanId);
218 } else if (hostVlanId.equals(VlanId.NONE)) {
219 if (untaggedVlan != null) {
220 mbuilder.matchVlanId(untaggedVlan);
221 } else if (nativeVlan != null) {
222 mbuilder.matchVlanId(nativeVlan);
223 } else {
224 // TODO: This check is turned off for now since vRouter still assumes that
225 // hosts are internally tagged with INTERNAL_VLAN.
226 // We should turn this back on when we move forward to the bridging CPR approach.
227 //
228 //log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
229 // hostMac, hostVlanId, connectPoint);
230 //return null;
231 mbuilder.matchVlanId(INTERNAL_VLAN);
232 }
233 } else {
234 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed in tagged vlan",
235 hostMac, hostVlanId, connectPoint);
Saurav Das07c74602016-04-27 18:35:50 -0700236 return null;
237 }
Charles Chan90772a72017-02-08 15:52:08 -0800238
239 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
240 tbuilder.build(), mbuilder.build());
241 if (portNextObjId == -1) {
242 // Warning log will come from getPortNextObjective method
243 return null;
244 }
245
Charles Chanf4586112015-11-09 16:37:23 -0800246 return DefaultForwardingObjective.builder()
Charles Chan90772a72017-02-08 15:52:08 -0800247 .withSelector(sbuilder.build())
Saurav Das2d94d312015-11-24 23:21:05 -0800248 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800249 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700250 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800251 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700252 }
253
254 /**
255 * Populates IP flow rules for the subnets of the destination router.
256 *
257 * @param deviceId switch ID to set the rules
Charles Chanc22cef32016-04-29 14:38:22 -0700258 * @param subnets subnet being added
sangho80f11cb2015-04-01 13:05:26 -0700259 * @param destSw destination switch ID
260 * @param nextHops next hop switch ID list
261 * @return true if all rules are set successfully, false otherwise
262 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800263 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<IpPrefix> subnets,
Charles Chanc22cef32016-04-29 14:38:22 -0700264 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700265 for (IpPrefix subnet : subnets) {
sangho80f11cb2015-04-01 13:05:26 -0700266 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
267 return false;
268 }
269 }
Charles Chanc22cef32016-04-29 14:38:22 -0700270 return true;
271 }
sangho80f11cb2015-04-01 13:05:26 -0700272
Charles Chanc22cef32016-04-29 14:38:22 -0700273 /**
274 * Revokes IP flow rules for the subnets.
275 *
276 * @param subnets subnet being removed
277 * @return true if all rules are removed successfully, false otherwise
278 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800279 public boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700280 for (IpPrefix subnet : subnets) {
281 if (!revokeIpRuleForRouter(subnet)) {
282 return false;
283 }
284 }
sangho80f11cb2015-04-01 13:05:26 -0700285 return true;
286 }
287
288 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700289 * Populates IP flow rules for an IP prefix in the target device. The prefix
290 * is reachable via destination device.
sangho80f11cb2015-04-01 13:05:26 -0700291 *
Saurav Das88979182015-10-19 14:37:36 -0700292 * @param deviceId target device ID to set the rules
Pier Ventreadb4ae62016-11-23 09:57:42 -0800293 * @param ipPrefix the destination IP prefix
sangho80f11cb2015-04-01 13:05:26 -0700294 * @param destSw device ID of the destination router
295 * @param nextHops next hop switch ID list
296 * @return true if all rules are set successfully, false otherwise
297 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700298 public boolean populateIpRuleForRouter(DeviceId deviceId,
299 IpPrefix ipPrefix, DeviceId destSw,
300 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800301 int segmentId;
302 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800303 if (ipPrefix.isIp4()) {
304 segmentId = config.getIPv4SegmentId(destSw);
305 } else {
306 segmentId = config.getIPv6SegmentId(destSw);
307 }
Charles Chan319d1a22015-11-03 10:42:14 -0800308 } catch (DeviceConfigNotFoundException e) {
309 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
310 return false;
311 }
sangho80f11cb2015-04-01 13:05:26 -0700312
Pier Ventreadb4ae62016-11-23 09:57:42 -0800313 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800314 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700315
Charles Chanf4586112015-11-09 16:37:23 -0800316 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
317 NeighborSet ns;
318 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700319
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700320 // If the next hop is the same as the final destination, then MPLS label
321 // is not set.
sangho80f11cb2015-04-01 13:05:26 -0700322 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800323 tbuilder.immediate().decNwTtl();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700324 ns = new NeighborSet(nextHops, false);
Charles Chanf4586112015-11-09 16:37:23 -0800325 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700326 } else {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700327 ns = new NeighborSet(nextHops, false, segmentId);
Charles Chanf4586112015-11-09 16:37:23 -0800328 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700329 }
330
Saurav Das4c35fc42015-11-20 15:27:53 -0800331 // setup metadata to pass to nextObjective - indicate the vlan on egress
332 // if needed by the switch pipeline. Since neighbor sets are always to
333 // other neighboring routers, there is no subnet assigned on those ports.
334 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800335 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das4c35fc42015-11-20 15:27:53 -0800336
337 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
338 if (nextId <= 0) {
sangho2165d222015-05-01 09:38:25 -0700339 log.warn("No next objective in {} for ns: {}", deviceId, ns);
340 return false;
341 }
342
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700343 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
344 .builder()
345 .fromApp(srManager.appId)
346 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800347 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700348 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800349 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700350 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800351 if (treatment != null) {
352 fwdBuilder.withTreatment(treatment);
353 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700354 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700355 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700356 ipPrefix,
357 deviceId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700358 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700359 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
360 ipPrefix, deviceId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700361 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700362 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
363 ipPrefix, error, deviceId));
Charles Chan1eaf4802016-04-18 13:44:03 -0700364 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700365 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700366
367 return true;
368 }
369
sangho80f11cb2015-04-01 13:05:26 -0700370 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700371 * Revokes IP flow rules for the router IP address.
372 *
373 * @param ipPrefix the IP address of the destination router
374 * @return true if all rules are removed successfully, false otherwise
375 */
376 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800377 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700378 TrafficSelector selector = sbuilder.build();
379 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
380
381 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
382 .builder()
383 .fromApp(srManager.appId)
384 .makePermanent()
385 .withSelector(selector)
386 .withTreatment(dummyTreatment)
387 .withPriority(getPriorityFromPrefix(ipPrefix))
388 .withFlag(ForwardingObjective.Flag.SPECIFIC);
389
390 ObjectiveContext context = new DefaultObjectiveContext(
391 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
392 (objective, error) ->
393 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
394
395 srManager.deviceService.getAvailableDevices().forEach(device -> {
396 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
397 });
398
399 return true;
400 }
401
402 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700403 * Deals with !MPLS Bos use case.
404 *
405 * @param targetSwId the target sw
406 * @param destSwId the destination sw
407 * @param nextHops the set of next hops
408 * @param segmentId the segmentId to match
409 * @param routerIp the router ip
410 * @return a collection of fwdobjective
411 */
412 private Collection<ForwardingObjective> handleMpls(DeviceId targetSwId,
413 DeviceId destSwId,
414 Set<DeviceId> nextHops,
415 int segmentId,
416 IpAddress routerIp,
417 boolean isMplsBos) {
418
419 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
420 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800421 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700422 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
423 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
424 sbuilder.matchMplsBos(isMplsBos);
425 TrafficSelector selector = sbuilder.build();
426
427 // setup metadata to pass to nextObjective - indicate the vlan on egress
428 // if needed by the switch pipeline. Since mpls next-hops are always to
429 // other neighboring routers, there is no subnet assigned on those ports.
430 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800431 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700432
433 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
434 // If the next hop is the destination router for the segment, do pop
435 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
436 + "label {} in switch {} with pop", segmentId, targetSwId);
437 // Not-bos pop case (php for the current label). If MPLS-ECMP
438 // has been configured, the application we will request the
439 // installation for an MPLS-ECMP group.
440 ForwardingObjective.Builder fwdObjNoBosBuilder = getMplsForwardingObjective(targetSwId,
441 nextHops,
442 true,
443 isMplsBos,
444 metabuilder.build(),
445 routerIp);
446 // Error case, we cannot handle, exit.
447 if (fwdObjNoBosBuilder == null) {
448 return Collections.emptyList();
449 }
450 fwdObjBuilders.add(fwdObjNoBosBuilder);
451
452 } else {
453 // next hop is not destination, SR CONTINUE case (swap with self)
454 log.debug("Installing MPLS forwarding objective for "
455 + "label {} in switch {} without pop", segmentId, targetSwId);
456 // Not-bos pop case. If MPLS-ECMP has been configured, the
457 // application we will request the installation for an MPLS-ECMP
458 // group.
459 ForwardingObjective.Builder fwdObjNoBosBuilder = getMplsForwardingObjective(targetSwId,
460 nextHops,
461 false,
462 isMplsBos,
463 metabuilder.build(),
464 routerIp);
465 // Error case, we cannot handle, exit.
466 if (fwdObjNoBosBuilder == null) {
467 return Collections.emptyList();
468 }
469 fwdObjBuilders.add(fwdObjNoBosBuilder);
470
471 }
472
473 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
474 // We add the final property to the fwdObjs.
475 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
476
477 ((Builder) ((Builder) fwdObjBuilder
478 .fromApp(srManager.appId)
479 .makePermanent())
480 .withSelector(selector)
481 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
482 .withFlag(ForwardingObjective.Flag.SPECIFIC);
483
484 ObjectiveContext context = new DefaultObjectiveContext(
485 (objective) ->
486 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
487 objective.id(), segmentId, targetSwId),
488 (objective, error) ->
489 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
490 objective.id(), segmentId, error, targetSwId));
491
492 ForwardingObjective fob = fwdObjBuilder.add(context);
493 fwdObjs.add(fob);
494
495 }
496
497 return fwdObjs;
498 }
499
500 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700501 * Populates MPLS flow rules in the target device to point towards the
502 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700503 *
Saurav Dase0237a32016-05-27 13:54:07 -0700504 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700505 * @param destSwId destination switch device ID
506 * @param nextHops next hops switch ID list
Pier Ventre229fd0b2016-10-31 16:49:19 -0700507 * @param routerIp the router ip
sangho80f11cb2015-04-01 13:05:26 -0700508 * @return true if all rules are set successfully, false otherwise
509 */
Saurav Dase0237a32016-05-27 13:54:07 -0700510 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800511 Set<DeviceId> nextHops, IpAddress routerIp) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700512
Charles Chan319d1a22015-11-03 10:42:14 -0800513 int segmentId;
514 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800515 if (routerIp.isIp4()) {
516 segmentId = config.getIPv4SegmentId(destSwId);
517 } else {
518 segmentId = config.getIPv6SegmentId(destSwId);
519 }
Charles Chan319d1a22015-11-03 10:42:14 -0800520 } catch (DeviceConfigNotFoundException e) {
521 log.warn(e.getMessage() + " Aborting populateMplsRule.");
522 return false;
523 }
sangho80f11cb2015-04-01 13:05:26 -0700524
Pier Ventre229fd0b2016-10-31 16:49:19 -0700525 List<ForwardingObjective> fwdObjs = new ArrayList<>();
526 Collection<ForwardingObjective> fwdObjsMpls = Collections.emptyList();
527 // Generates the transit rules used by the standard "routing".
528 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
529 if (fwdObjsMpls.isEmpty()) {
530 return false;
sangho80f11cb2015-04-01 13:05:26 -0700531 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700532 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800533 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre229fd0b2016-10-31 16:49:19 -0700534 // the only case !BoS supported.
Saurav Dasf9332192017-02-18 14:05:44 -0800535 /*fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700536 if (fwdObjsMpls.isEmpty()) {
537 return false;
538 }
Saurav Dasf9332192017-02-18 14:05:44 -0800539 fwdObjs.addAll(fwdObjsMpls);*/
Pier Ventre229fd0b2016-10-31 16:49:19 -0700540
541 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700542 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700543 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
544 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700545 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700546 }
547
548 return true;
549 }
550
Saurav Das4c35fc42015-11-20 15:27:53 -0800551 private ForwardingObjective.Builder getMplsForwardingObjective(
552 DeviceId deviceId,
553 Set<DeviceId> nextHops,
554 boolean phpRequired,
555 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800556 TrafficSelector meta,
557 IpAddress routerIp) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800558
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700559 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
560 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700561
562 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
563
564 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800565 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700566 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700567 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700568 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800569 if (routerIp.isIp4()) {
570 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
571 } else {
572 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
573 }
574 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700575 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800576 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
577 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700578 }
579 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800580 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700581 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700582 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700583 }
584
Saurav Das4c35fc42015-11-20 15:27:53 -0800585 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700586 // if MPLS-ECMP == True we will build a standard NeighborSet.
587 // Otherwise a RandomNeighborSet.
588 NeighborSet ns = NeighborSet.neighborSet(false, nextHops, false);
589 if (!isBos && this.srManager.getMplsEcmp()) {
590 ns = NeighborSet.neighborSet(false, nextHops, true);
591 } else if (!isBos && !this.srManager.getMplsEcmp()) {
592 ns = NeighborSet.neighborSet(true, nextHops, true);
593 }
594 log.info("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
595 deviceId, ns);
596 // If BoS == True, all forwarding is via L3 ECMP group.
597 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
598 // MPLS-Interface group. This depends on the configuration of the option
599 // MPLS-ECMP.
600 // The metadata informs the driver that the next-Objective will be used
601 // by MPLS flows and if Bos == False the driver will use MPLS groups.
602 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta, isBos);
Saurav Das4c35fc42015-11-20 15:27:53 -0800603 if (nextId <= 0) {
604 log.warn("No next objective in {} for ns: {}", deviceId, ns);
605 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700606 } else {
607 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
608 nextId, deviceId, ns);
sangho80f11cb2015-04-01 13:05:26 -0700609 }
610
Saurav Das4c35fc42015-11-20 15:27:53 -0800611 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700612 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700613 }
614
615 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700616 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700617 * dstMac corresponding to the router's MAC address. For those pipelines
618 * that need to internally assign vlans to untagged packets, this method
619 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700620 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800621 * Note that the vlan assignment and filter programming should only be done by
622 * the master for a switch. This method is typically called at deviceAdd and
623 * programs filters only for the enabled ports of the device. For port-updates,
624 * that enable/disable ports after device add, singlePortFilter methods should
625 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700626 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700627 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800628 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700629 */
Charles Chan18fa4252017-02-08 16:10:40 -0800630 public PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700631 log.debug("Installing per-port filtering objective for untagged "
632 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800633
Saurav Das07c74602016-04-27 18:35:50 -0700634 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800635 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700636 log.warn("Device {} ports not available. Unable to add MacVlan filters",
637 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800638 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700639 }
Saurav Dasf9332192017-02-18 14:05:44 -0800640 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700641 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700642 if (!port.isEnabled()) {
643 disabledPorts++;
644 continue;
645 }
Charles Chan43be46b2017-02-26 22:59:35 -0800646 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800647 filteredPorts++;
648 } else {
649 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700650 }
Saurav Das7c305372015-10-28 12:39:42 -0700651 }
Saurav Dasf9332192017-02-18 14:05:44 -0800652 log.info("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
653 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800654 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800655 errorPorts, filteredPorts);
656 }
657
658 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800659 * Creates or removes filtering objectives for a single port. Should only be
660 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800661 *
662 * @param deviceId device identifier
663 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800664 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800665 * @return true if no errors occurred during the build of the filtering objective
666 */
Charles Chan43be46b2017-02-26 22:59:35 -0800667 public boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800668 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
669 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
670 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
671 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
672
673 if (taggedVlans.size() != 0) {
674 // Filter for tagged vlans
675 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800676 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800677 return false;
678 }
679 if (nativeVlan != null) {
680 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800681 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800682 return false;
683 }
684 }
685 } else if (untaggedVlan != null) {
686 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800687 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800688 return false;
689 }
690 } else {
691 // Unconfigure port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800692 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800693 return false;
694 }
695 }
696 return true;
697 }
698
Charles Chan43be46b2017-02-26 22:59:35 -0800699 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
700 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800701 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800702 if (fob == null) {
703 // error encountered during build
704 return false;
705 }
Charles Chan43be46b2017-02-26 22:59:35 -0800706 log.info("{} filtering objectives for dev/port:{}/{}", (install ? "Installing" : "Removing"),
707 deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800708 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800709 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
710 (install ? "installed" : "removed")),
711 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
712 (install ? "install" : "remove"), deviceId, portnum, error));
713 if (install) {
714 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
715 } else {
716 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -0800717 }
Charles Chan90772a72017-02-08 15:52:08 -0800718 return true;
Saurav Dasf9332192017-02-18 14:05:44 -0800719 }
720
Charles Chan90772a72017-02-08 15:52:08 -0800721 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
722 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -0800723 MacAddress deviceMac;
724 try {
725 deviceMac = config.getDeviceMac(deviceId);
726 } catch (DeviceConfigNotFoundException e) {
727 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
728 return null;
729 }
730 // suppressed ports still have filtering rules pushed by SR using default vlan
731 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800732
Saurav Dasf9332192017-02-18 14:05:44 -0800733 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
734 fob.withKey(Criteria.matchInPort(portnum))
735 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -0800736 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -0800737
738 if (pushVlan) {
739 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
740 TrafficTreatment tt = DefaultTrafficTreatment.builder()
741 .pushVlan().setVlanId(vlanId).build();
742 fob.withMeta(tt);
743 } else {
744 fob.addCondition(Criteria.matchVlanId(vlanId));
745 }
746
Saurav Dasf9332192017-02-18 14:05:44 -0800747 fob.permit().fromApp(srManager.appId);
748 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700749 }
750
751 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700752 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700753 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700754 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700755 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700756 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700757 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700758 */
Charles Chan18fa4252017-02-08 16:10:40 -0800759 public void populateIpPunts(DeviceId deviceId) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800760 Ip4Address routerIpv4;
761 Ip6Address routerIpv6;
Charles Chan319d1a22015-11-03 10:42:14 -0800762 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800763 routerIpv4 = config.getRouterIpv4(deviceId);
764 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800765 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -0800766 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -0800767 return;
768 }
769
Saurav Dasc28b3432015-10-30 17:45:38 -0700770 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
771 log.debug("Not installing port-IP punts - not the master for dev:{} ",
772 deviceId);
773 return;
774 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800775 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
776 allIps.add(routerIpv4);
777 if (routerIpv6 != null) {
778 allIps.add(routerIpv6);
779 }
780 for (IpAddress ipaddr : allIps) {
781 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr);
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800782 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
783
784 srManager.packetService.requestPackets(sbuilder.build(),
785 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das9f1c42e2015-10-23 10:51:11 -0700786 }
sangho80f11cb2015-04-01 13:05:26 -0700787 }
788
Charles Chanf4586112015-11-09 16:37:23 -0800789 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800790 * Method to build IPv4 or IPv6 selector.
791 *
792 * @param addressToMatch the address to match
793 */
794 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
795 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
796 }
797
798 /**
799 * Method to build IPv4 or IPv6 selector.
800 *
801 * @param prefixToMatch the prefix to match
802 */
803 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
804 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700805 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800806 if (prefixToMatch.isIp4()) {
807 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
808 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
809 return selectorBuilder;
810 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700811 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800812 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
813 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
814 return selectorBuilder;
815 }
816
817 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800818 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
819 * Furthermore, these are applied only by the master instance. Deferred actions
820 * are not cleared such that packets can be flooded in the cross connect use case
821 *
822 * @param deviceId the switch dpid for the router
823 */
824 public void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700825 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800826 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
827 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
828 deviceId);
829 return;
830 }
831
832 // We punt all ARP packets towards the controller.
833 ForwardingObjective puntFwd = puntArpFwdObjective()
834 .add(new ObjectiveContext() {
835 @Override
836 public void onError(Objective objective, ObjectiveError error) {
837 log.warn("Failed to install packet request for ARP to {}: {}",
838 deviceId, error);
839 }
840 });
841 srManager.flowObjectiveService.forward(deviceId, puntFwd);
842
Pier Luigib9632ba2017-01-12 18:14:58 -0800843 // We punt all NDP packets towards the controller.
Pier Luigib9632ba2017-01-12 18:14:58 -0800844 puntFwd = puntNdpFwdObjective()
845 .add(new ObjectiveContext() {
846 @Override
847 public void onError(Objective objective, ObjectiveError error) {
848 log.warn("Failed to install packet request for NDP to {}: {}",
849 deviceId, error);
850 }
851 });
852 srManager.flowObjectiveService.forward(deviceId, puntFwd);
Pier Luigib9632ba2017-01-12 18:14:58 -0800853 }
854
855 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) {
856
857 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
858 tBuilder.punt();
859
860 return DefaultForwardingObjective.builder()
861 .withPriority(PacketPriority.CONTROL.priorityValue())
862 .withSelector(selector)
863 .fromApp(srManager.appId)
864 .withFlag(ForwardingObjective.Flag.VERSATILE)
865 .withTreatment(tBuilder.build())
866 .makePermanent();
867 }
868
869 private ForwardingObjective.Builder puntArpFwdObjective() {
870
871 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
872 sBuilder.matchEthType(TYPE_ARP);
873
874 return fwdObjBuilder(sBuilder.build());
875 }
876
877 private ForwardingObjective.Builder puntNdpFwdObjective() {
878
879 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
880 sBuilder.matchEthType(TYPE_IPV6)
881 .matchIPProtocol(PROTOCOL_ICMP6)
882 .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
883 .build();
884
885 return fwdObjBuilder(sBuilder.build());
886 }
887
888 /**
Charles Chanf4586112015-11-09 16:37:23 -0800889 * Populates a forwarding objective to send packets that miss other high
890 * priority Bridging Table entries to a group that contains all ports of
891 * its subnet.
892 *
893 * Note: We assume that packets sending from the edge switches to the hosts
894 * have untagged VLAN.
895 * The VLAN tag will be popped later in the flooding group.
896 *
897 * @param deviceId switch ID to set the rules
898 */
899 public void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -0800900 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan10b0fb72017-02-02 16:20:42 -0800901 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
902
903 if (nextId < 0) {
904 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
905 + "to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
Saurav Das2d94d312015-11-24 23:21:05 -0800906 return;
907 }
908
Pier Ventre229fd0b2016-10-31 16:49:19 -0700909 // Driver should treat objective with MacAddress.NONE as the
910 // subnet broadcast rule
Charles Chanf4586112015-11-09 16:37:23 -0800911 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
912 sbuilder.matchVlanId(vlanId);
913 sbuilder.matchEthDst(MacAddress.NONE);
914
915 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
916 fob.withFlag(Flag.SPECIFIC)
917 .withSelector(sbuilder.build())
918 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800919 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chanf4586112015-11-09 16:37:23 -0800920 .fromApp(srManager.appId)
921 .makePermanent();
Charles Chan1eaf4802016-04-18 13:44:03 -0700922 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan10b0fb72017-02-02 16:20:42 -0800923 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700924 (objective, error) ->
Charles Chan10b0fb72017-02-02 16:20:42 -0800925 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700926 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800927 });
928 }
929
Charles Chan82ab1932016-01-30 23:22:37 -0800930 private int getPriorityFromPrefix(IpPrefix prefix) {
931 return (prefix.isIp4()) ?
932 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
933 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700934 }
sangho80f11cb2015-04-01 13:05:26 -0700935}