blob: b691f806aed84f9fc803dcc8f5a209fe100ea20b [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sanghob35a6192015-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
Saurav Das8a0732e2015-11-20 15:27:53 -080018import org.onlab.packet.EthType;
sanghob35a6192015-04-01 13:05:26 -070019import org.onlab.packet.Ethernet;
20import org.onlab.packet.Ip4Address;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070021import org.onlab.packet.Ip4Prefix;
sanghob35a6192015-04-01 13:05:26 -070022import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
sanghob35a6192015-04-01 13:05:26 -070024import org.onlab.packet.MplsLabel;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070025import org.onlab.packet.VlanId;
Charles Chane849c192016-01-11 18:28:54 -080026import org.onosproject.net.ConnectPoint;
Charles Chand2990362016-04-18 13:44:03 -070027import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080028import org.onosproject.net.flowobjective.Objective;
Charles Chand2990362016-04-18 13:44:03 -070029import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080030import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2df0e8a2017-01-09 11:45:08 -080031import org.onosproject.net.packet.PacketPriority;
Saurav Dasd2fded02016-12-02 15:43:47 -080032import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan0b4e6182015-11-03 10:42:14 -080033import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
34import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chan6ea94fc2016-05-10 17:29:47 -070035import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070036import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sanghob35a6192015-04-01 13:05:26 -070037import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070038import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070039import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070040import org.onosproject.net.flow.DefaultTrafficSelector;
41import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070042import org.onosproject.net.flow.TrafficSelector;
43import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070044import org.onosproject.net.flow.criteria.Criteria;
45import org.onosproject.net.flowobjective.DefaultFilteringObjective;
46import org.onosproject.net.flowobjective.DefaultForwardingObjective;
47import org.onosproject.net.flowobjective.FilteringObjective;
48import org.onosproject.net.flowobjective.ForwardingObjective;
49import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das822c4e22015-10-23 10:51:11 -070050import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sanghob35a6192015-04-01 13:05:26 -070051import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
54import java.util.ArrayList;
Saurav Das837e0bb2015-10-30 17:45:38 -070055import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070056import java.util.List;
Charles Chan2df0e8a2017-01-09 11:45:08 -080057import java.util.Optional;
sanghob35a6192015-04-01 13:05:26 -070058import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070059import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070060
61import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080062import static org.onlab.packet.Ethernet.TYPE_ARP;
63import static org.onlab.packet.Ethernet.TYPE_IPV6;
64import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
65import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
sanghob35a6192015-04-01 13:05:26 -070066
Charles Chane849c192016-01-11 18:28:54 -080067/**
68 * Populator of segment routing flow rules.
69 */
sanghob35a6192015-04-01 13:05:26 -070070public class RoutingRulePopulator {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070071 private static final Logger log = LoggerFactory
72 .getLogger(RoutingRulePopulator.class);
sanghob35a6192015-04-01 13:05:26 -070073
sangho20eff1d2015-04-13 15:15:58 -070074 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070075 private SegmentRoutingManager srManager;
76 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070077
sanghob35a6192015-04-01 13:05:26 -070078 /**
79 * Creates a RoutingRulePopulator object.
80 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070081 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070082 */
83 public RoutingRulePopulator(SegmentRoutingManager srManager) {
84 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070085 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-04-13 15:15:58 -070086 this.rulePopulationCounter = new AtomicLong(0);
87 }
88
89 /**
90 * Resets the population counter.
91 */
92 public void resetCounter() {
93 rulePopulationCounter.set(0);
94 }
95
96 /**
97 * Returns the number of rules populated.
Thomas Vachuska266b4432015-04-30 18:13:25 -070098 *
99 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -0700100 */
101 public long getCounter() {
102 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -0700103 }
104
105 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700106 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700107 * switch.
sanghob35a6192015-04-01 13:05:26 -0700108 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700109 * @param deviceId device ID of the device that next hop attaches to
110 * @param prefix IP prefix of the route
111 * @param hostMac MAC address of the next hop
112 * @param outPort port where the next hop attaches to
sanghob35a6192015-04-01 13:05:26 -0700113 */
Charles Chan1cdecff2016-10-27 14:19:48 -0700114 public void populateRoute(DeviceId deviceId, IpPrefix prefix,
sanghob35a6192015-04-01 13:05:26 -0700115 MacAddress hostMac, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700116 log.debug("Populate IP table entry for route {} at {}:{}",
117 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800118 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800119 try {
Charles Chan68aa62d2015-11-09 16:37:23 -0800120 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700121 deviceId, prefix, hostMac, outPort);
Charles Chan0b4e6182015-11-03 10:42:14 -0800122 } catch (DeviceConfigNotFoundException e) {
123 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
124 return;
125 }
Saurav Das59232cf2016-04-27 18:35:50 -0700126 if (fwdBuilder == null) {
127 log.warn("Aborting host routing table entries due "
Charles Chan1cdecff2016-10-27 14:19:48 -0700128 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das59232cf2016-04-27 18:35:50 -0700129 return;
130 }
Charles Chand2990362016-04-18 13:44:03 -0700131 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700132 (objective) -> log.debug("IP rule for route {} populated", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700133 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700134 log.warn("Failed to populate IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700135 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800136 rulePopulationCounter.incrementAndGet();
137 }
138
Charles Chane849c192016-01-11 18:28:54 -0800139 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700140 * Removes IP rules for a route when the next hop is gone.
Charles Chane849c192016-01-11 18:28:54 -0800141 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700142 * @param deviceId device ID of the device that next hop attaches to
143 * @param prefix IP prefix of the route
144 * @param hostMac MAC address of the next hop
145 * @param outPort port that next hop attaches to
Charles Chane849c192016-01-11 18:28:54 -0800146 */
Charles Chan1cdecff2016-10-27 14:19:48 -0700147 public void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan68aa62d2015-11-09 16:37:23 -0800148 MacAddress hostMac, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700149 log.debug("Revoke IP table entry for route {} at {}:{}",
150 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800151 ForwardingObjective.Builder fwdBuilder;
152 try {
153 fwdBuilder = getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700154 deviceId, prefix, hostMac, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800155 } catch (DeviceConfigNotFoundException e) {
156 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
157 return;
158 }
Charles Chan458b8262016-11-30 11:55:05 -0800159 if (fwdBuilder == null) {
160 log.warn("Aborting host routing table entries due "
161 + "to error for dev:{} route:{}", deviceId, prefix);
162 return;
163 }
Charles Chand2990362016-04-18 13:44:03 -0700164 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700165 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700166 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700167 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700168 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800169 }
170
Charles Chan1cdecff2016-10-27 14:19:48 -0700171 /**
172 * Returns a forwarding objective that points packets destined to a
173 * given prefix to given port on given device with given destination MAC.
174 *
175 * @param deviceId device ID
176 * @param prefix prefix that need to be routed
177 * @param hostMac MAC address of the nexthop
178 * @param outPort port where the nexthop attaches to
179 * @return forwarding objective builder
180 * @throws DeviceConfigNotFoundException if given device is not configured
181 */
Charles Chan68aa62d2015-11-09 16:37:23 -0800182 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700183 DeviceId deviceId, IpPrefix prefix,
Charles Chan68aa62d2015-11-09 16:37:23 -0800184 MacAddress hostMac, PortNumber outPort)
185 throws DeviceConfigNotFoundException {
186 MacAddress deviceMac;
187 deviceMac = config.getDeviceMac(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800188
sanghob35a6192015-04-01 13:05:26 -0700189 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
sanghob35a6192015-04-01 13:05:26 -0700190 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan1cdecff2016-10-27 14:19:48 -0700191 sbuilder.matchIPDst(prefix);
Saurav Das4ce45962015-11-24 23:21:05 -0800192 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700193
Charles Chan1cdecff2016-10-27 14:19:48 -0700194 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho1e575652015-05-14 00:39:53 -0700195 tbuilder.deferred()
196 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800197 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700198 .setOutput(outPort);
sanghob35a6192015-04-01 13:05:26 -0700199 TrafficTreatment treatment = tbuilder.build();
Saurav Das4ce45962015-11-24 23:21:05 -0800200
201 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
202 // for switch pipelines that need it, provide outgoing vlan as metadata
203 VlanId outvlan = null;
204 Ip4Prefix subnet = srManager.deviceConfiguration.getPortSubnet(deviceId, outPort);
205 if (subnet == null) {
206 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
207 } else {
208 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
209 }
210 TrafficSelector meta = DefaultTrafficSelector.builder()
211 .matchVlanId(outvlan).build();
212 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
213 treatment, meta);
Saurav Das59232cf2016-04-27 18:35:50 -0700214 if (portNextObjId == -1) {
215 // warning log will come from getPortNextObjective method
216 return null;
217 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800218 return DefaultForwardingObjective.builder()
Saurav Das4ce45962015-11-24 23:21:05 -0800219 .withSelector(selector)
220 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800221 .fromApp(srManager.appId).makePermanent()
Charles Chan1cdecff2016-10-27 14:19:48 -0700222 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan5270ed02016-01-30 23:22:37 -0800223 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700224 }
225
226 /**
227 * Populates IP flow rules for the subnets of the destination router.
228 *
229 * @param deviceId switch ID to set the rules
Charles Chan93e71ba2016-04-29 14:38:22 -0700230 * @param subnets subnet being added
sanghob35a6192015-04-01 13:05:26 -0700231 * @param destSw destination switch ID
232 * @param nextHops next hop switch ID list
233 * @return true if all rules are set successfully, false otherwise
234 */
Charles Chan93e71ba2016-04-29 14:38:22 -0700235 public boolean populateIpRuleForSubnet(DeviceId deviceId, Set<Ip4Prefix> subnets,
236 DeviceId destSw, Set<DeviceId> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700237 for (IpPrefix subnet : subnets) {
sanghob35a6192015-04-01 13:05:26 -0700238 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
239 return false;
240 }
241 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700242 return true;
243 }
sanghob35a6192015-04-01 13:05:26 -0700244
Charles Chan93e71ba2016-04-29 14:38:22 -0700245 /**
246 * Revokes IP flow rules for the subnets.
247 *
248 * @param subnets subnet being removed
249 * @return true if all rules are removed successfully, false otherwise
250 */
251 public boolean revokeIpRuleForSubnet(Set<Ip4Prefix> subnets) {
252 for (IpPrefix subnet : subnets) {
253 if (!revokeIpRuleForRouter(subnet)) {
254 return false;
255 }
256 }
sanghob35a6192015-04-01 13:05:26 -0700257 return true;
258 }
259
260 /**
Saurav Das25190812016-05-27 13:54:07 -0700261 * Populates IP flow rules for an IP prefix in the target device. The prefix
262 * is reachable via destination device.
sanghob35a6192015-04-01 13:05:26 -0700263 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700264 * @param deviceId target device ID to set the rules
sanghob35a6192015-04-01 13:05:26 -0700265 * @param ipPrefix the IP address of the destination router
266 * @param destSw device ID of the destination router
267 * @param nextHops next hop switch ID list
268 * @return true if all rules are set successfully, false otherwise
269 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700270 public boolean populateIpRuleForRouter(DeviceId deviceId,
271 IpPrefix ipPrefix, DeviceId destSw,
272 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800273 int segmentId;
274 try {
275 segmentId = config.getSegmentId(destSw);
276 } catch (DeviceConfigNotFoundException e) {
277 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
278 return false;
279 }
sanghob35a6192015-04-01 13:05:26 -0700280
281 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan17d38f42016-02-05 13:33:54 -0800282 sbuilder.matchIPDst(ipPrefix);
sanghob35a6192015-04-01 13:05:26 -0700283 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan68aa62d2015-11-09 16:37:23 -0800284 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700285
Charles Chan68aa62d2015-11-09 16:37:23 -0800286 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
287 NeighborSet ns;
288 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700289
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700290 // If the next hop is the same as the final destination, then MPLS label
291 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700292 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800293 tbuilder.immediate().decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700294 ns = new NeighborSet(nextHops);
Charles Chan68aa62d2015-11-09 16:37:23 -0800295 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700296 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800297 ns = new NeighborSet(nextHops, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800298 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700299 }
300
Saurav Das8a0732e2015-11-20 15:27:53 -0800301 // setup metadata to pass to nextObjective - indicate the vlan on egress
302 // if needed by the switch pipeline. Since neighbor sets are always to
303 // other neighboring routers, there is no subnet assigned on those ports.
304 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
305 metabuilder.matchVlanId(
306 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
307
308 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
309 if (nextId <= 0) {
sangho834e4b02015-05-01 09:38:25 -0700310 log.warn("No next objective in {} for ns: {}", deviceId, ns);
311 return false;
312 }
313
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700314 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
315 .builder()
316 .fromApp(srManager.appId)
317 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800318 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700319 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800320 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700321 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800322 if (treatment != null) {
323 fwdBuilder.withTreatment(treatment);
324 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700325 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700326 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700327 ipPrefix,
328 deviceId);
Charles Chand2990362016-04-18 13:44:03 -0700329 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700330 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
331 ipPrefix, deviceId),
Charles Chand2990362016-04-18 13:44:03 -0700332 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700333 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
334 ipPrefix, error, deviceId));
Charles Chand2990362016-04-18 13:44:03 -0700335 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700336 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700337
338 return true;
339 }
340
sanghob35a6192015-04-01 13:05:26 -0700341 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700342 * Revokes IP flow rules for the router IP address.
343 *
344 * @param ipPrefix the IP address of the destination router
345 * @return true if all rules are removed successfully, false otherwise
346 */
347 public boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
348 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
349 sbuilder.matchIPDst(ipPrefix);
350 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
351 TrafficSelector selector = sbuilder.build();
352 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
353
354 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
355 .builder()
356 .fromApp(srManager.appId)
357 .makePermanent()
358 .withSelector(selector)
359 .withTreatment(dummyTreatment)
360 .withPriority(getPriorityFromPrefix(ipPrefix))
361 .withFlag(ForwardingObjective.Flag.SPECIFIC);
362
363 ObjectiveContext context = new DefaultObjectiveContext(
364 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
365 (objective, error) ->
366 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
367
368 srManager.deviceService.getAvailableDevices().forEach(device -> {
369 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
370 });
371
372 return true;
373 }
374
375 /**
Saurav Das25190812016-05-27 13:54:07 -0700376 * Populates MPLS flow rules in the target device to point towards the
377 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700378 *
Saurav Das25190812016-05-27 13:54:07 -0700379 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700380 * @param destSwId destination switch device ID
381 * @param nextHops next hops switch ID list
382 * @return true if all rules are set successfully, false otherwise
383 */
Saurav Das25190812016-05-27 13:54:07 -0700384 public boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700385 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800386 int segmentId;
387 try {
388 segmentId = config.getSegmentId(destSwId);
389 } catch (DeviceConfigNotFoundException e) {
390 log.warn(e.getMessage() + " Aborting populateMplsRule.");
391 return false;
392 }
sanghob35a6192015-04-01 13:05:26 -0700393
394 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700395 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700396
397 // TODO Handle the case of Bos == false
sanghob35a6192015-04-01 13:05:26 -0700398 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das8a0732e2015-11-20 15:27:53 -0800399 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chan188ebf52015-12-23 00:15:11 -0800400 sbuilder.matchMplsBos(true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800401 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700402
Saurav Das8a0732e2015-11-20 15:27:53 -0800403 // setup metadata to pass to nextObjective - indicate the vlan on egress
404 // if needed by the switch pipeline. Since mpls next-hops are always to
405 // other neighboring routers, there is no subnet assigned on those ports.
406 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
407 metabuilder.matchVlanId(
408 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
409
410 // If the next hop is the destination router for the segment, do pop
sanghob35a6192015-04-01 13:05:26 -0700411 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700412 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das25190812016-05-27 13:54:07 -0700413 + "label {} in switch {} with pop", segmentId, targetSwId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700414
Saurav Das8a0732e2015-11-20 15:27:53 -0800415 // bos pop case (php)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700416 ForwardingObjective.Builder fwdObjBosBuilder =
Saurav Das25190812016-05-27 13:54:07 -0700417 getMplsForwardingObjective(targetSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700418 nextHops,
419 true,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700420 true,
Saurav Das8a0732e2015-11-20 15:27:53 -0800421 metabuilder.build());
422 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700423 return false;
424 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800425 fwdObjBuilders.add(fwdObjBosBuilder);
426
427 // XXX not-bos pop case, SR app multi-label not implemented yet
428 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
429 getMplsForwardingObjective(deviceId,
430 nextHops,
431 true,
432 false);*/
433
sanghob35a6192015-04-01 13:05:26 -0700434 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800435 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700436 log.debug("Installing MPLS forwarding objective for "
Saurav Das25190812016-05-27 13:54:07 -0700437 + "label {} in switch {} without pop", segmentId, targetSwId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700438
Saurav Das8a0732e2015-11-20 15:27:53 -0800439 // continue case with bos - this does get triggered in edge routers
440 // and in core routers - driver can handle depending on availability
441 // of MPLS ECMP or not
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700442 ForwardingObjective.Builder fwdObjBosBuilder =
Saurav Das25190812016-05-27 13:54:07 -0700443 getMplsForwardingObjective(targetSwId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700444 nextHops,
445 false,
Saurav Das8a0732e2015-11-20 15:27:53 -0800446 true,
447 metabuilder.build());
448 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700449 return false;
450 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800451 fwdObjBuilders.add(fwdObjBosBuilder);
452
453 // XXX continue case with not-bos - SR app multi label not implemented yet
454 // also requires MPLS ECMP
455 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
456 getMplsForwardingObjective(deviceId,
457 nextHops,
458 false,
459 false); */
460
sanghob35a6192015-04-01 13:05:26 -0700461 }
Saurav Das25190812016-05-27 13:54:07 -0700462 // XXX when other cases above are implemented check for validity of
463 // debug messages below
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700464 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
465 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
466 .makePermanent()).withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800467 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700468 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chand2990362016-04-18 13:44:03 -0700469 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700470 (objective) -> log.debug("MPLS rule {} for SID {} populated in dev:{} ",
471 objective.id(), segmentId, targetSwId),
Charles Chand2990362016-04-18 13:44:03 -0700472 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700473 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
474 objective.id(), segmentId, error, targetSwId));
475 ForwardingObjective fob = fwdObjBuilder.add(context);
476 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
477 fob.id(), segmentId, fob.nextId(), targetSwId);
478 srManager.flowObjectiveService.forward(targetSwId, fob);
sangho20eff1d2015-04-13 15:15:58 -0700479 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700480 }
481
482 return true;
483 }
484
Saurav Das8a0732e2015-11-20 15:27:53 -0800485 private ForwardingObjective.Builder getMplsForwardingObjective(
486 DeviceId deviceId,
487 Set<DeviceId> nextHops,
488 boolean phpRequired,
489 boolean isBos,
490 TrafficSelector meta) {
491
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700492 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
493 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700494
495 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
496
497 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800498 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700499 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700500 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700501 if (isBos) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800502 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
503 .decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700504 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800505 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
506 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700507 }
508 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800509 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700510 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700511 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700512 }
513
Saurav Das8a0732e2015-11-20 15:27:53 -0800514 // All forwarding is via ECMP group, the metadata informs the driver
515 // that the next-Objective will be used by MPLS flows. In other words,
516 // MPLS ECMP is requested. It is up to the driver to decide if these
517 // packets will be hashed or not.
518 fwdBuilder.withTreatment(tbuilder.build());
519 NeighborSet ns = new NeighborSet(nextHops);
Saurav Das25190812016-05-27 13:54:07 -0700520 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das8a0732e2015-11-20 15:27:53 -0800521 deviceId, ns);
522
523 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
524 if (nextId <= 0) {
525 log.warn("No next objective in {} for ns: {}", deviceId, ns);
526 return null;
Saurav Das25190812016-05-27 13:54:07 -0700527 } else {
528 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
529 nextId, deviceId, ns);
sanghob35a6192015-04-01 13:05:26 -0700530 }
531
Saurav Das8a0732e2015-11-20 15:27:53 -0800532 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700533 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700534 }
535
536 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700537 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700538 * dstMac corresponding to the router's MAC address. For those pipelines
539 * that need to internally assign vlans to untagged packets, this method
540 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700541 * <p>
542 * Note that the vlan assignment is only done by the master-instance for a switch.
543 * However we send the filtering objective from slave-instances as well, so
544 * that drivers can obtain other information (like Router MAC and IP).
sanghob35a6192015-04-01 13:05:26 -0700545 *
Saurav Das822c4e22015-10-23 10:51:11 -0700546 * @param deviceId the switch dpid for the router
Saurav Dasd2fded02016-12-02 15:43:47 -0800547 * @return PortFilterInfo information about the processed ports
sanghob35a6192015-04-01 13:05:26 -0700548 */
Saurav Dasd2fded02016-12-02 15:43:47 -0800549 public PortFilterInfo populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700550 log.debug("Installing per-port filtering objective for untagged "
551 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800552
553 MacAddress deviceMac;
554 try {
555 deviceMac = config.getDeviceMac(deviceId);
556 } catch (DeviceConfigNotFoundException e) {
557 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
Saurav Dasd2fded02016-12-02 15:43:47 -0800558 return null;
Charles Chan0b4e6182015-11-03 10:42:14 -0800559 }
560
Saurav Das59232cf2016-04-27 18:35:50 -0700561 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hallcbd1b392017-01-18 20:15:44 -0800562 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das59232cf2016-04-27 18:35:50 -0700563 log.warn("Device {} ports not available. Unable to add MacVlan filters",
564 deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -0800565 return null;
Saurav Das59232cf2016-04-27 18:35:50 -0700566 }
Saurav Das25190812016-05-27 13:54:07 -0700567 int disabledPorts = 0, suppressedPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700568 for (Port port : devPorts) {
Charles Chand2990362016-04-18 13:44:03 -0700569 ConnectPoint connectPoint = new ConnectPoint(deviceId, port.number());
Charles Chanf2565a92016-02-10 20:46:58 -0800570 // TODO: Handles dynamic port events when we are ready for dynamic config
Saurav Das25190812016-05-27 13:54:07 -0700571 if (!port.isEnabled()) {
572 disabledPorts++;
573 continue;
574 }
Charles Chan03a73e02016-10-24 14:52:01 -0700575
576 boolean isSuppressed = false;
577 SegmentRoutingAppConfig appConfig = srManager.cfgService
578 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
Saurav Das25190812016-05-27 13:54:07 -0700579 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700580 isSuppressed = true;
Saurav Das25190812016-05-27 13:54:07 -0700581 suppressedPorts++;
Saurav Das25190812016-05-27 13:54:07 -0700582 }
Charles Chan03a73e02016-10-24 14:52:01 -0700583
Saurav Das25190812016-05-27 13:54:07 -0700584 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
Charles Chan03a73e02016-10-24 14:52:01 -0700585 VlanId assignedVlan = (portSubnet == null || isSuppressed)
Saurav Das25190812016-05-27 13:54:07 -0700586 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
587 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan0b4e6182015-11-03 10:42:14 -0800588
Saurav Das25190812016-05-27 13:54:07 -0700589 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
590 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan0b4e6182015-11-03 10:42:14 -0800591 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chane849c192016-01-11 18:28:54 -0800592 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800593 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Das25190812016-05-27 13:54:07 -0700594 // vlan assignment is valid only if this instance is master
595 if (srManager.mastershipService.isLocalMaster(deviceId)) {
596 TrafficTreatment tt = DefaultTrafficTreatment.builder()
597 .pushVlan().setVlanId(assignedVlan).build();
598 fob.withMeta(tt);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700599 }
Saurav Das25190812016-05-27 13:54:07 -0700600 fob.permit().fromApp(srManager.appId);
601 log.debug("Sending filtering objective for dev/port:{}/{}", deviceId, port);
602 filteredPorts++;
603 ObjectiveContext context = new DefaultObjectiveContext(
604 (objective) -> log.debug("Filter for {} populated", connectPoint),
605 (objective, error) ->
606 log.warn("Failed to populate filter for {}: {}", connectPoint, error));
607 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
Saurav Das0e99e2b2015-10-28 12:39:42 -0700608 }
Saurav Das25190812016-05-27 13:54:07 -0700609 log.info("Filtering on dev:{}, disabledPorts:{}, suppressedPorts:{}, filteredPorts:{}",
610 deviceId, disabledPorts, suppressedPorts, filteredPorts);
Saurav Dasd2fded02016-12-02 15:43:47 -0800611 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
612 suppressedPorts, filteredPorts);
sanghob35a6192015-04-01 13:05:26 -0700613 }
614
615 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700616 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700617 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700618 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700619 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700620 *
Saurav Das822c4e22015-10-23 10:51:11 -0700621 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700622 */
Saurav Das822c4e22015-10-23 10:51:11 -0700623 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800624 Ip4Address routerIp;
625 try {
626 routerIp = config.getRouterIp(deviceId);
627 } catch (DeviceConfigNotFoundException e) {
628 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
629 return;
630 }
631
Saurav Das837e0bb2015-10-30 17:45:38 -0700632 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
633 log.debug("Not installing port-IP punts - not the master for dev:{} ",
634 deviceId);
635 return;
636 }
Charles Chan5270ed02016-01-30 23:22:37 -0800637 Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId));
Charles Chan0b4e6182015-11-03 10:42:14 -0800638 allIps.add(routerIp);
Saurav Das837e0bb2015-10-30 17:45:38 -0700639 for (Ip4Address ipaddr : allIps) {
Charles Chan2df0e8a2017-01-09 11:45:08 -0800640 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
641 .matchEthType(Ethernet.TYPE_IPV4)
642 .matchIPDst(IpPrefix.valueOf(ipaddr, IpPrefix.MAX_INET_MASK_LENGTH));
643 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
644
645 srManager.packetService.requestPackets(sbuilder.build(),
646 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700647 }
sanghob35a6192015-04-01 13:05:26 -0700648 }
649
Charles Chan68aa62d2015-11-09 16:37:23 -0800650 /**
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800651 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
652 * Furthermore, these are applied only by the master instance. Deferred actions
653 * are not cleared such that packets can be flooded in the cross connect use case
654 *
655 * @param deviceId the switch dpid for the router
656 */
657 public void populateArpNdpPunts(DeviceId deviceId) {
658 /*
659 * We are not the master just skip.
660 */
661 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
662 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
663 deviceId);
664 return;
665 }
666
667 // We punt all ARP packets towards the controller.
668 ForwardingObjective puntFwd = puntArpFwdObjective()
669 .add(new ObjectiveContext() {
670 @Override
671 public void onError(Objective objective, ObjectiveError error) {
672 log.warn("Failed to install packet request for ARP to {}: {}",
673 deviceId, error);
674 }
675 });
676 srManager.flowObjectiveService.forward(deviceId, puntFwd);
677
678 // TODO: The driver does not support NDP at this moment. Turn it back on later.
679 // We punt all NDP packets towards the controller.
680 /*
681 puntFwd = puntNdpFwdObjective()
682 .add(new ObjectiveContext() {
683 @Override
684 public void onError(Objective objective, ObjectiveError error) {
685 log.warn("Failed to install packet request for NDP to {}: {}",
686 deviceId, error);
687 }
688 });
689 srManager.flowObjectiveService.forward(deviceId, puntFwd);
690 */
691 }
692
693 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector) {
694
695 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
696 tBuilder.punt();
697
698 return DefaultForwardingObjective.builder()
699 .withPriority(PacketPriority.CONTROL.priorityValue())
700 .withSelector(selector)
701 .fromApp(srManager.appId)
702 .withFlag(ForwardingObjective.Flag.VERSATILE)
703 .withTreatment(tBuilder.build())
704 .makePermanent();
705 }
706
707 private ForwardingObjective.Builder puntArpFwdObjective() {
708
709 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
710 sBuilder.matchEthType(TYPE_ARP);
711
712 return fwdObjBuilder(sBuilder.build());
713 }
714
715 private ForwardingObjective.Builder puntNdpFwdObjective() {
716
717 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
718 sBuilder.matchEthType(TYPE_IPV6)
719 .matchIPProtocol(PROTOCOL_ICMP6)
720 .matchIcmpv6Type(NEIGHBOR_SOLICITATION)
721 .build();
722
723 return fwdObjBuilder(sBuilder.build());
724 }
725
726 /**
Charles Chan68aa62d2015-11-09 16:37:23 -0800727 * Populates a forwarding objective to send packets that miss other high
728 * priority Bridging Table entries to a group that contains all ports of
729 * its subnet.
730 *
731 * Note: We assume that packets sending from the edge switches to the hosts
732 * have untagged VLAN.
733 * The VLAN tag will be popped later in the flooding group.
734 *
735 * @param deviceId switch ID to set the rules
736 */
737 public void populateSubnetBroadcastRule(DeviceId deviceId) {
738 config.getSubnets(deviceId).forEach(subnet -> {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800739 if (subnet.prefixLength() == 0 ||
740 subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
741 return;
742 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800743 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
744 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
745
Saurav Das4ce45962015-11-24 23:21:05 -0800746 if (nextId < 0 || vlanId == null) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800747 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
748 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -0800749 return;
750 }
751
Charles Chan68aa62d2015-11-09 16:37:23 -0800752 /* Driver should treat objective with MacAddress.NONE as the
753 * subnet broadcast rule
754 */
755 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
756 sbuilder.matchVlanId(vlanId);
757 sbuilder.matchEthDst(MacAddress.NONE);
758
759 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
760 fob.withFlag(Flag.SPECIFIC)
761 .withSelector(sbuilder.build())
762 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800763 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800764 .fromApp(srManager.appId)
765 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -0700766 ObjectiveContext context = new DefaultObjectiveContext(
767 (objective) -> log.debug("Subnet broadcast rule for {} populated", subnet),
768 (objective, error) ->
769 log.warn("Failed to populate subnet broadcast rule for {}: {}", subnet, error));
770 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800771 });
772 }
773
Charles Chan5270ed02016-01-30 23:22:37 -0800774 private int getPriorityFromPrefix(IpPrefix prefix) {
775 return (prefix.isIp4()) ?
776 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
777 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700778 }
sanghob35a6192015-04-01 13:05:26 -0700779}